2 //Functions used in restore
4 //This function unzips a zip file in the same directory that it is
5 //It automatically uses pclzip or command line unzip
6 function restore_unzip ($file) {
8 return unzip_file($file, '', false);
12 //This function checks if moodle.xml seems to be a valid xml file
13 //(exists, has an xml header and a course main tag
14 function restore_check_moodle_file ($file) {
19 if ($status = is_file($file)) {
20 //Open it and read the first 200 bytes (chars)
21 $handle = fopen ($file, "r");
22 $first_chars = fread($handle,200);
23 $status = fclose ($handle);
24 //Chek if it has the requires strings
26 $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
27 if ($status !== false) {
28 $status = strpos($first_chars,"<MOODLE_BACKUP>");
36 //This function iterates over all modules in backup file, searching for a
37 //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
38 function restore_refresh_events($restore) {
43 //Take all modules in backup
44 $modules = $restore->mods
;
46 foreach($modules as $name => $module) {
47 //Only if the module is being restored
48 if (isset($module->restore
) && $module->restore
== 1) {
49 //Include module library
50 include_once("$CFG->dirroot/mod/$name/lib.php");
51 //If module_refresh_events exists
52 $function_name = $name."_refresh_events";
53 if (function_exists($function_name)) {
54 $status = $function_name($restore->course_id
);
61 //This function makes all the necessary calls to xxxx_decode_content_links_caller()
62 //function in each module, passing them the desired contents to be decoded
63 //from backup format to destination site/course in order to mantain inter-activities
64 //working in the backup/restore process
65 function restore_decode_content_links($restore) {
70 if (!defined('RESTORE_SILENTLY')) {
74 // Restore links in modules.
75 foreach ($restore->mods
as $name => $info) {
76 //If the module is being restored
77 if (isset($info->restore
) && $info->restore
== 1) {
78 //Check if the xxxx_decode_content_links_caller exists
79 include_once("$CFG->dirroot/mod/$name/restorelib.php");
80 $function_name = $name."_decode_content_links_caller";
81 if (function_exists($function_name)) {
82 if (!defined('RESTORE_SILENTLY')) {
83 echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
85 $status = $function_name($restore);
86 if (!defined('RESTORE_SILENTLY')) {
93 // TODO: process all html text also in blocks too
95 // Restore links in questions.
96 require_once("$CFG->dirroot/question/restorelib.php");
97 if (!defined('RESTORE_SILENTLY')) {
98 echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
100 $status = question_decode_content_links_caller($restore);
101 if (!defined('RESTORE_SILENTLY')) {
105 if (!defined('RESTORE_SILENTLY')) {
112 //This function is called from all xxxx_decode_content_links_caller(),
113 //its task is to ask all modules (maybe other linkable objects) to restore
115 function restore_decode_content_links_worker($content,$restore) {
116 foreach($restore->mods
as $name => $info) {
117 $function_name = $name."_decode_content_links";
118 if (function_exists($function_name)) {
119 $content = $function_name($content,$restore);
125 //This function converts all the wiki texts in the restored course
126 //to the Markdown format. Used only for backup files prior 2005041100.
127 //It calls every module xxxx_convert_wiki2markdown function
128 function restore_convert_wiki2markdown($restore) {
132 if (!defined('RESTORE_SILENTLY')) {
135 foreach ($restore->mods
as $name => $info) {
136 //If the module is being restored
137 if ($info->restore
== 1) {
138 //Check if the xxxx_restore_wiki2markdown exists
139 $function_name = $name."_restore_wiki2markdown";
140 if (function_exists($function_name)) {
141 $status = $function_name($restore);
142 if (!defined('RESTORE_SILENTLY')) {
143 echo "<li>".get_string("modulenameplural",$name);
149 if (!defined('RESTORE_SILENTLY')) {
155 //This function receives a wiki text in the restore process and
156 //return it with every link to modules " modulename:moduleid"
157 //converted if possible. See the space before modulename!!
158 function restore_decode_wiki_content($content,$restore) {
164 $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
166 preg_match_all($searchstring,$content,$foundset);
167 //If found, then we are going to look for its new id (in backup tables)
169 //print_object($foundset); //Debug
170 //Iterate over foundset[2]. They are the old_ids
171 foreach($foundset[2] as $old_id) {
172 //We get the needed variables here (course id)
173 $rec = backup_getid($restore->backup_unique_code
,"course_modules",$old_id);
174 //Personalize the searchstring
175 $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
176 //If it is a link to this course, update the link to its new location
179 $result= preg_replace($searchstring,' $1:'.$rec->new_id
.'($2)',$result);
181 //It's a foreign link so redirect it to its original URL
182 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
190 //This function read the xml file and store it data from the info zone in an object
191 function restore_read_xml_info ($xml_file) {
193 //We call the main read_xml function, with todo = INFO
194 $info = restore_read_xml ($xml_file,"INFO",false);
199 //This function read the xml file and store it data from the course header zone in an object
200 function restore_read_xml_course_header ($xml_file) {
202 //We call the main read_xml function, with todo = COURSE_HEADER
203 $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
208 //This function read the xml file and store its data from the blocks in a object
209 function restore_read_xml_blocks ($xml_file) {
211 //We call the main read_xml function, with todo = BLOCKS
212 $info = restore_read_xml ($xml_file,'BLOCKS',false);
217 //This function read the xml file and store its data from the sections in a object
218 function restore_read_xml_sections ($xml_file) {
220 //We call the main read_xml function, with todo = SECTIONS
221 $info = restore_read_xml ($xml_file,"SECTIONS",false);
226 //This function read the xml file and store its data from the course format in an object
227 function restore_read_xml_formatdata ($xml_file) {
229 //We call the main read_xml function, with todo = FORMATDATA
230 $info = restore_read_xml ($xml_file,'FORMATDATA',false);
235 //This function read the xml file and store its data from the metacourse in a object
236 function restore_read_xml_metacourse ($xml_file) {
238 //We call the main read_xml function, with todo = METACOURSE
239 $info = restore_read_xml ($xml_file,"METACOURSE",false);
244 //This function read the xml file and store its data from the gradebook in a object
245 function restore_read_xml_gradebook ($restore, $xml_file) {
247 //We call the main read_xml function, with todo = GRADEBOOK
248 $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
253 //This function read the xml file and store its data from the users in
254 //backup_ids->info db (and user's id in $info)
255 function restore_read_xml_users ($restore,$xml_file) {
257 //We call the main read_xml function, with todo = USERS
258 $info = restore_read_xml ($xml_file,"USERS",$restore);
263 //This function read the xml file and store its data from the messages in
264 //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
265 function restore_read_xml_messages ($restore,$xml_file) {
267 //We call the main read_xml function, with todo = MESSAGES
268 $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
274 //This function read the xml file and store its data from the questions in
275 //backup_ids->info db (and category's id in $info)
276 function restore_read_xml_questions ($restore,$xml_file) {
278 //We call the main read_xml function, with todo = QUESTIONS
279 $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
284 //This function read the xml file and store its data from the scales in
285 //backup_ids->info db (and scale's id in $info)
286 function restore_read_xml_scales ($restore,$xml_file) {
288 //We call the main read_xml function, with todo = SCALES
289 $info = restore_read_xml ($xml_file,"SCALES",$restore);
294 //This function read the xml file and store its data from the groups in
295 //backup_ids->info db (and group's id in $info)
296 function restore_read_xml_groups ($restore,$xml_file) {
298 //We call the main read_xml function, with todo = GROUPS
299 $info = restore_read_xml ($xml_file,"GROUPS",$restore);
304 //This function read the xml file and store its data from the groupings in
305 //backup_ids->info db (and grouping's id in $info)
306 function restore_read_xml_groupings ($restore,$xml_file) {
308 //We call the main read_xml function, with todo = GROUPINGS
309 $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
314 //This function read the xml file and store its data from the groupings in
315 //backup_ids->info db (and grouping's id in $info)
316 function restore_read_xml_groupings_groups ($restore,$xml_file) {
318 //We call the main read_xml function, with todo = GROUPINGS
319 $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
324 //This function read the xml file and store its data from the events (course) in
325 //backup_ids->info db (and event's id in $info)
326 function restore_read_xml_events ($restore,$xml_file) {
328 //We call the main read_xml function, with todo = EVENTS
329 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
334 //This function read the xml file and store its data from the modules in
336 function restore_read_xml_modules ($restore,$xml_file) {
338 //We call the main read_xml function, with todo = MODULES
339 $info = restore_read_xml ($xml_file,"MODULES",$restore);
344 //This function read the xml file and store its data from the logs in
346 function restore_read_xml_logs ($restore,$xml_file) {
348 //We call the main read_xml function, with todo = LOGS
349 $info = restore_read_xml ($xml_file,"LOGS",$restore);
354 function restore_read_xml_roles ($xml_file) {
355 //We call the main read_xml function, with todo = ROLES
356 $info = restore_read_xml ($xml_file,"ROLES",false);
361 //This function prints the contents from the info parammeter passed
362 function restore_print_info ($info) {
368 $table = new object();
369 //This is tha align to every ingo table
370 $table->align
= array ("right","left");
371 //This is the nowrap clause
372 $table->wrap
= array ("","nowrap");
374 $table->width
= "70%";
375 //Put interesting info in table
376 //The backup original name
377 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
378 $tab[0][1] = $info->backup_name
;
380 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
381 $tab[1][1] = $info->backup_moodle_release
." (".$info->backup_moodle_version
.")";
383 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
384 $tab[2][1] = $info->backup_backup_release
." (".$info->backup_backup_version
.")";
386 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
387 $tab[3][1] = userdate($info->backup_date
);
389 print_heading(get_string("backup").":");
391 //Print backup general info
394 if ($info->backup_backup_version
<= 2005070500) {
395 notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
398 //Now backup contents in another table
403 foreach ($mods as $key => $mod) {
404 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
405 if ($mod->backup
== "false") {
406 $tab[$elem][1] = get_string("notincluded");
408 if ($mod->userinfo
== "true") {
409 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
411 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
413 if (isset($mod->instances
) && is_array($mod->instances
) && count($mod->instances
)) {
414 foreach ($mod->instances
as $instance) {
415 if ($instance->backup
) {
417 $tab[$elem][0] = $instance->name
;
418 if ($instance->userinfo
== 'true') {
419 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
421 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
430 $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
431 if ($info->backup_metacourse
== "true") {
432 $tab[$elem][1] = get_string("yes");
434 $tab[$elem][1] = get_string("no");
438 $tab[$elem][0] = "<b>".get_string("users").":</b>";
439 $tab[$elem][1] = get_string($info->backup_users
);
442 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
443 if ($info->backup_logs
== "true") {
444 $tab[$elem][1] = get_string("yes");
446 $tab[$elem][1] = get_string("no");
450 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
451 if ($info->backup_user_files
== "true") {
452 $tab[$elem][1] = get_string("yes");
454 $tab[$elem][1] = get_string("no");
458 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
459 if ($info->backup_course_files
== "true") {
460 $tab[$elem][1] = get_string("yes");
462 $tab[$elem][1] = get_string("no");
466 $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
467 if ($info->backup_site_files
== "true") {
468 $tab[$elem][1] = get_string("yes");
470 $tab[$elem][1] = get_string("no");
473 //Messages info (only showed if present)
474 if ($info->backup_messages
== 'true') {
475 $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
476 $tab[$elem][1] = get_string('yes');
483 print_heading(get_string("backupdetails").":");
484 //Print backup general info
493 //This function prints the contents from the course_header parammeter passed
494 function restore_print_course_header ($course_header) {
497 if ($course_header) {
498 $table = new object();
499 //This is tha align to every ingo table
500 $table->align
= array ("right","left");
502 $table->width
= "70%";
503 //Put interesting course header in table
505 $tab[0][0] = "<b>".get_string("name").":</b>";
506 $tab[0][1] = $course_header->course_fullname
." (".$course_header->course_shortname
.")";
508 $tab[1][0] = "<b>".get_string("summary").":</b>";
509 $tab[1][1] = $course_header->course_summary
;
512 print_heading(get_string("course").":");
513 //Print backup course header info
521 //This function create a new course record.
522 //When finished, course_header contains the id of the new course
523 function restore_create_new_course($restore,&$course_header) {
529 $fullname = $course_header->course_fullname
;
530 $shortname = $course_header->course_shortname
;
531 $currentfullname = "";
532 $currentshortname = "";
534 //Iteratere while the name exists
537 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
538 $suffixshort = "_".$counter;
543 $currentfullname = $fullname.$suffixfull;
544 // Limit the size of shortname - database column accepts <= 100 chars
545 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
546 $coursefull = get_record("course","fullname",addslashes($currentfullname));
547 $courseshort = get_record("course","shortname",addslashes($currentshortname));
549 } while ($coursefull ||
$courseshort);
551 //New name = currentname
552 $course_header->course_fullname
= $currentfullname;
553 $course_header->course_shortname
= $currentshortname;
555 // first try to get it from restore
556 if ($restore->restore_restorecatto
) {
557 $category = get_record('course_categories', 'id', $restore->restore_restorecatto
);
560 // else we try to get it from the xml file
561 //Now calculate the category
563 $category = get_record("course_categories","id",$course_header->category
->id
,
564 "name",addslashes($course_header->category
->name
));
567 //If no exists, try by name only
569 $category = get_record("course_categories","name",addslashes($course_header->category
->name
));
572 //If no exists, get category id 1
574 $category = get_record("course_categories","id","1");
577 //If category 1 doesn'exists, lets create the course category (get it from backup file)
579 $ins_category = new object();
580 $ins_category->name
= addslashes($course_header->category
->name
);
581 $ins_category->parent
= 0;
582 $ins_category->sortorder
= 0;
583 $ins_category->coursecount
= 0;
584 $ins_category->visible
= 0; //To avoid interferences with the rest of the site
585 $ins_category->timemodified
= time();
586 $newid = insert_record("course_categories",$ins_category);
587 $category->id
= $newid;
588 $category->name
= $course_header->category
->name
;
590 //If exists, put new category id
592 $course_header->category
->id
= $category->id
;
593 $course_header->category
->name
= $category->name
;
594 //Error, cannot locate category
596 $course_header->category
->id
= 0;
597 $course_header->category
->name
= get_string("unknowncategory");
601 //Create the course_object
603 $course = new object();
604 $course->category
= addslashes($course_header->category
->id
);
605 $course->password
= addslashes($course_header->course_password
);
606 $course->fullname
= addslashes($course_header->course_fullname
);
607 $course->shortname
= addslashes($course_header->course_shortname
);
608 $course->idnumber
= addslashes($course_header->course_idnumber
);
609 $course->idnumber
= ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
610 $course->summary
= backup_todb($course_header->course_summary
);
611 $course->format
= addslashes($course_header->course_format
);
612 $course->showgrades
= addslashes($course_header->course_showgrades
);
613 $course->newsitems
= addslashes($course_header->course_newsitems
);
614 $course->teacher
= addslashes($course_header->course_teacher
);
615 $course->teachers
= addslashes($course_header->course_teachers
);
616 $course->student
= addslashes($course_header->course_student
);
617 $course->students
= addslashes($course_header->course_students
);
618 $course->guest
= addslashes($course_header->course_guest
);
619 $course->startdate
= addslashes($course_header->course_startdate
);
620 $course->startdate +
= $restore->course_startdateoffset
;
621 $course->numsections
= addslashes($course_header->course_numsections
);
622 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
623 $course->maxbytes
= addslashes($course_header->course_maxbytes
);
624 $course->showreports
= addslashes($course_header->course_showreports
);
625 if (isset($course_header->course_groupmode
)) {
626 $course->groupmode
= addslashes($course_header->course_groupmode
);
628 if (isset($course_header->course_groupmodeforce
)) {
629 $course->groupmodeforce
= addslashes($course_header->course_groupmodeforce
);
631 if (isset($course_header->course_defaultgroupingid
)) {
632 //keep the original now - convert after groupings restored
633 $course->defaultgroupingid
= addslashes($course_header->course_defaultgroupingid
);
635 $course->lang
= addslashes($course_header->course_lang
);
636 $course->theme
= addslashes($course_header->course_theme
);
637 $course->cost
= addslashes($course_header->course_cost
);
638 $course->currency
= isset($course_header->course_currency
)?
addslashes($course_header->course_currency
):'';
639 $course->marker
= addslashes($course_header->course_marker
);
640 $course->visible
= addslashes($course_header->course_visible
);
641 $course->hiddensections
= addslashes($course_header->course_hiddensections
);
642 $course->timecreated
= addslashes($course_header->course_timecreated
);
643 $course->timemodified
= addslashes($course_header->course_timemodified
);
644 $course->metacourse
= addslashes($course_header->course_metacourse
);
645 $course->expirynotify
= isset($course_header->course_expirynotify
) ?
addslashes($course_header->course_expirynotify
):0;
646 $course->notifystudents
= isset($course_header->course_notifystudents
) ?
addslashes($course_header->course_notifystudents
) : 0;
647 $course->expirythreshold
= isset($course_header->course_expirythreshold
) ?
addslashes($course_header->course_expirythreshold
) : 0;
648 $course->enrollable
= isset($course_header->course_enrollable
) ?
addslashes($course_header->course_enrollable
) : 1;
649 $course->enrolstartdate
= isset($course_header->course_enrolstartdate
) ?
addslashes($course_header->course_enrolstartdate
) : 0;
650 if ($course->enrolstartdate
) { //Roll course dates
651 $course->enrolstartdate +
= $restore->course_startdateoffset
;
653 $course->enrolenddate
= isset($course_header->course_enrolenddate
) ?
addslashes($course_header->course_enrolenddate
) : 0;
654 if ($course->enrolenddate
) { //Roll course dates
655 $course->enrolenddate +
= $restore->course_startdateoffset
;
657 $course->enrolperiod
= addslashes($course_header->course_enrolperiod
);
658 //Calculate sortorder field
659 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
660 FROM ' . $CFG->prefix
. 'course
661 WHERE category=' . $course->category
);
662 if (!empty($sortmax->max
)) {
663 $course->sortorder
= $sortmax->max +
1;
666 $course->sortorder
= 100;
669 //Now, recode some languages (Moodle 1.5)
670 if ($course->lang
== 'ma_nt') {
671 $course->lang
= 'mi_nt';
674 //Disable course->metacourse if avoided in restore config
675 if (!$restore->metacourse
) {
676 $course->metacourse
= 0;
679 //Check if the theme exists in destination server
680 $themes = get_list_of_themes();
681 if (!in_array($course->theme
, $themes)) {
685 //Now insert the record
686 $newid = insert_record("course",$course);
688 //save old and new course id
689 backup_putid ($restore->backup_unique_code
,"course",$course_header->course_id
,$newid);
690 //Replace old course_id in course_header
691 $course_header->course_id
= $newid;
702 //This function creates all the block stuff when restoring courses
703 //It calls selectively to restore_create_block_instances() for 1.5
704 //and above backups. Upwards compatible with old blocks.
705 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
709 delete_records('block_instance', 'pageid', $restore->course_id
, 'pagetype', PAGE_COURSE_VIEW
);
710 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
711 if (empty($blockinfo)) {
712 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
713 $newpage = page_create_object(PAGE_COURSE_VIEW
, $restore->course_id
);
714 blocks_repopulate_page($newpage);
716 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
717 $blockrecords = get_records_select('block', '', '', 'name, id');
718 $temp_blocks_l = array();
719 $temp_blocks_r = array();
720 @list
($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
721 $temp_blocks = array(BLOCK_POS_LEFT
=> explode(',', $temp_blocks_l), BLOCK_POS_RIGHT
=> explode(',', $temp_blocks_r));
722 foreach($temp_blocks as $blockposition => $blocks) {
724 foreach($blocks as $blockname) {
725 if(!isset($blockrecords[$blockname])) {
726 // We don't know anything about this block!
729 $blockinstance = new stdClass
;
730 // Remove any - prefix before doing the name-to-id mapping
731 if(substr($blockname, 0, 1) == '-') {
732 $blockname = substr($blockname, 1);
733 $blockinstance->visible
= 0;
735 $blockinstance->visible
= 1;
737 $blockinstance->blockid
= $blockrecords[$blockname]->id
;
738 $blockinstance->pageid
= $restore->course_id
;
739 $blockinstance->pagetype
= PAGE_COURSE_VIEW
;
740 $blockinstance->position
= $blockposition;
741 $blockinstance->weight
= $blockweight;
742 if(!$status = insert_record('block_instance', $blockinstance)) {
749 } else if($backup_block_format == 'instances') {
750 $status = restore_create_block_instances($restore,$xml_file);
757 //This function creates all the block_instances from xml when restoring in a
759 function restore_create_block_instances($restore,$xml_file) {
763 if (!file_exists($xml_file)) {
768 $info = restore_read_xml_blocks($xml_file);
771 if(empty($info->instances
)) {
775 // First of all, iterate over the blocks to see which distinct pages we have
776 // in our hands and arrange the blocks accordingly.
777 $pageinstances = array();
778 foreach($info->instances
as $instance) {
780 //pagetype and pageid black magic, we have to handle the case of blocks for the
781 //course, blocks from other pages in that course etc etc etc.
783 if($instance->pagetype
== PAGE_COURSE_VIEW
) {
784 // This one's easy...
785 $instance->pageid
= $restore->course_id
;
788 $parts = explode('-', $instance->pagetype
);
789 if($parts[0] == 'mod') {
790 if(!$restore->mods
[$parts[1]]->restore
) {
793 $getid = backup_getid($restore->backup_unique_code
, $parts[1], $instance->pageid
);
794 $instance->pageid
= $getid->new_id
;
797 // Not invented here ;-)
802 if(!isset($pageinstances[$instance->pagetype
])) {
803 $pageinstances[$instance->pagetype
] = array();
805 if(!isset($pageinstances[$instance->pagetype
][$instance->pageid
])) {
806 $pageinstances[$instance->pagetype
][$instance->pageid
] = array();
809 $pageinstances[$instance->pagetype
][$instance->pageid
][] = $instance;
812 $blocks = get_records_select('block', '', '', 'name, id, multiple');
814 // For each type of page we have restored
815 foreach($pageinstances as $thistypeinstances) {
817 // For each page id of that type
818 foreach($thistypeinstances as $thisidinstances) {
820 $addedblocks = array();
821 $maxweights = array();
823 // For each block instance in that page
824 foreach($thisidinstances as $instance) {
826 if(!isset($blocks[$instance->name
])) {
827 //We are trying to restore a block we don't have...
831 //If we have already added this block once and multiples aren't allowed, disregard it
832 if(!$blocks[$instance->name
]->multiple
&& isset($addedblocks[$instance->name
])) {
836 //If its the first block we add to a new position, start weight counter equal to 0.
837 if(empty($maxweights[$instance->position
])) {
838 $maxweights[$instance->position
] = 0;
841 //If the instance weight is greater than the weight counter (we skipped some earlier
842 //blocks most probably), bring it back in line.
843 if($instance->weight
> $maxweights[$instance->position
]) {
844 $instance->weight
= $maxweights[$instance->position
];
848 $instance->blockid
= $blocks[$instance->name
]->id
;
850 if ($newid = insert_record('block_instance', $instance)) {
851 if (!empty($instance->id
)) { // this will only be set if we come from 1.7 and above backups
852 backup_putid ($restore->backup_unique_code
,"block_instance",$instance->id
,$newid);
859 //Get an object for the block and tell it it's been restored so it can update dates
861 $blockobj=block_instance($instance->name
,$instance);
862 $blockobj->after_restore($restore);
864 //Now we can increment the weight counter
865 ++
$maxweights[$instance->position
];
867 //Keep track of block types we have already added
868 $addedblocks[$instance->name
] = true;
877 //This function creates all the course_sections and course_modules from xml
878 //when restoring in a new course or simply checks sections and create records
879 //in backup_ids when restoring in a existing course
880 function restore_create_sections(&$restore, $xml_file) {
886 if (!file_exists($xml_file)) {
891 $info = restore_read_xml_sections($xml_file);
893 //Put the info in the DB, recoding ids and saving the in backup tables
898 //For each, section, save it to db
899 foreach ($info->sections
as $key => $sect) {
901 $section = new object();
902 $section->course
= $restore->course_id
;
903 $section->section
= $sect->number
;
904 $section->summary
= backup_todb($sect->summary
);
905 $section->visible
= $sect->visible
;
906 $section->sequence
= "";
907 //Now calculate the section's newid
909 if ($restore->restoreto
== 2) {
910 //Save it to db (only if restoring to new course)
911 $newid = insert_record("course_sections",$section);
913 //Get section id when restoring in existing course
914 $rec = get_record("course_sections","course",$restore->course_id
,
915 "section",$section->section
);
916 //If that section doesn't exist, get section 0 (every mod will be
919 $rec = get_record("course_sections","course",$restore->course_id
,
922 //New check. If section 0 doesn't exist, insert it here !!
923 //Teorically this never should happen but, in practice, some users
924 //have reported this issue.
926 $zero_sec = new object();
927 $zero_sec->course
= $restore->course_id
;
928 $zero_sec->section
= 0;
929 $zero_sec->summary
= "";
930 $zero_sec->sequence
= "";
931 $newid = insert_record("course_sections",$zero_sec);
936 $sequence = $rec->sequence
;
939 //save old and new section id
940 backup_putid ($restore->backup_unique_code
,"course_sections",$key,$newid);
944 //If all is OK, go with associated mods
946 //If we have mods in the section
947 if (!empty($sect->mods
)) {
948 //For each mod inside section
949 foreach ($sect->mods
as $keym => $mod) {
950 // Yu: This part is called repeatedly for every instance,
951 // so it is necessary to set the granular flag and check isset()
952 // when the first instance of this type of mod is processed.
954 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
956 if (!isset($restore->mods
[$mod->type
]->granular
)) {
957 if (isset($restore->mods
[$mod->type
]->instances
) && is_array($restore->mods
[$mod->type
]->instances
)) {
958 // This defines whether we want to restore specific
959 // instances of the modules (granular restore), or
960 // whether we don't care and just want to restore
961 // all module instances (non-granular).
962 $restore->mods
[$mod->type
]->granular
= true;
964 $restore->mods
[$mod->type
]->granular
= false;
968 //Check if we've to restore this module (and instance)
969 if (!empty($restore->mods
[$mod->type
]->restore
)) {
970 if (empty($restore->mods
[$mod->type
]->granular
) // we don't care about per instance
971 ||
(array_key_exists($mod->instance
,$restore->mods
[$mod->type
]->instances
)
972 && !empty($restore->mods
[$mod->type
]->instances
[$mod->instance
]->restore
))) {
974 //Get the module id from modules
975 $module = get_record("modules","name",$mod->type
);
977 $course_module = new object();
978 $course_module->course
= $restore->course_id
;
979 $course_module->module
= $module->id
;
980 $course_module->section
= $newid;
981 $course_module->added
= $mod->added
;
982 $course_module->score
= $mod->score
;
983 $course_module->indent
= $mod->indent
;
984 $course_module->visible
= $mod->visible
;
985 $course_module->groupmode
= $mod->groupmode
;
986 if ($mod->groupingid
and $grouping = backup_getid($restore->backup_unique_code
,"groupings",$mod->groupingid
)) {
987 $course_module->groupingid
= $grouping->new_id
;
989 $course_module->groupingid
= 0;
991 $course_module->groupmembersonly
= $mod->groupmembersonly
;
992 $course_module->instance
= 0;
993 //NOTE: The instance (new) is calculated and updated in db in the
994 // final step of the restore. We don't know it yet.
995 //print_object($course_module); //Debug
998 $newidmod = insert_record("course_modules",$course_module);
1000 //save old and new module id
1001 //In the info field, we save the original instance of the module
1003 backup_putid ($restore->backup_unique_code
,"course_modules",
1004 $keym,$newidmod,$mod->instance
);
1006 $restore->mods
[$mod->type
]->instances
[$mod->instance
]->restored_as_course_module
= $newidmod;
1010 //Now, calculate the sequence field
1013 $sequence .= ",".$newidmod;
1015 $sequence = $newidmod;
1026 //If all is OK, update sequence field in course_sections
1028 if (isset($sequence)) {
1029 $update_rec = new object();
1030 $update_rec->id
= $newid;
1031 $update_rec->sequence
= $sequence;
1032 $status = update_record("course_sections",$update_rec);
1042 //Called to set up any course-format specific data that may be in the file
1043 function restore_set_format_data($restore,$xml_file) {
1048 if (!file_exists($xml_file)) {
1051 //Load data from XML to info
1052 if(!($info = restore_read_xml_formatdata($xml_file))) {
1056 //Process format data if there is any
1057 if (isset($info->format_data
)) {
1058 if(!$format=get_field('course','format','id',$restore->course_id
)) {
1061 // If there was any data then it must have a restore method
1062 $file=$CFG->dirroot
."/course/format/$format/restorelib.php";
1063 if(!file_exists($file)) {
1066 require_once($file);
1067 $function=$format.'_restore_format_data';
1068 if(!function_exists($function)) {
1071 return $function($restore,$info->format_data
);
1074 // If we got here then there's no data, but that's cool
1078 //This function creates all the metacourse data from xml, notifying
1079 //about each incidence
1080 function restore_create_metacourse($restore,$xml_file) {
1086 if (!file_exists($xml_file)) {
1091 //Load data from XML to info
1092 $info = restore_read_xml_metacourse($xml_file);
1095 //Process info about metacourse
1096 if ($status and $info) {
1097 //Process child records
1098 if (!empty($info->childs
)) {
1099 foreach ($info->childs
as $child) {
1101 $dbmetacourse = false;
1102 //Check if child course exists in destination server
1103 //(by id in the same server or by idnumber and shortname in other server)
1104 if ($restore->original_wwwroot
== $CFG->wwwroot
) {
1105 //Same server, lets see by id
1106 $dbcourse = get_record('course','id',$child->id
);
1108 //Different server, lets see by idnumber and shortname, and only ONE record
1109 $dbcount = count_records('course','idnumber',$child->idnumber
,'shortname',$child->shortname
);
1110 if ($dbcount == 1) {
1111 $dbcourse = get_record('course','idnumber',$child->idnumber
,'shortname',$child->shortname
);
1114 //If child course has been found, insert data
1116 $dbmetacourse->child_course
= $dbcourse->id
;
1117 $dbmetacourse->parent_course
= $restore->course_id
;
1118 $status = insert_record ('course_meta',$dbmetacourse);
1120 //Child course not found, notice!
1121 if (!defined('RESTORE_SILENTLY')) {
1122 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id
.'/'.$child->idnumber
.'/'.$child->shortname
.')</li></ul>';
1126 //Now, recreate student enrolments...
1127 sync_metacourse($restore->course_id
);
1129 //Process parent records
1130 if (!empty($info->parents
)) {
1131 foreach ($info->parents
as $parent) {
1133 $dbmetacourse = false;
1134 //Check if parent course exists in destination server
1135 //(by id in the same server or by idnumber and shortname in other server)
1136 if ($restore->original_wwwroot
== $CFG->wwwroot
) {
1137 //Same server, lets see by id
1138 $dbcourse = get_record('course','id',$parent->id
);
1140 //Different server, lets see by idnumber and shortname, and only ONE record
1141 $dbcount = count_records('course','idnumber',$parent->idnumber
,'shortname',$parent->shortname
);
1142 if ($dbcount == 1) {
1143 $dbcourse = get_record('course','idnumber',$parent->idnumber
,'shortname',$parent->shortname
);
1146 //If parent course has been found, insert data if it is a metacourse
1148 if ($dbcourse->metacourse
) {
1149 $dbmetacourse->parent_course
= $dbcourse->id
;
1150 $dbmetacourse->child_course
= $restore->course_id
;
1151 $status = insert_record ('course_meta',$dbmetacourse);
1152 //Now, recreate student enrolments in parent course
1153 sync_metacourse($dbcourse->id
);
1155 //Parent course isn't metacourse, notice!
1156 if (!defined('RESTORE_SILENTLY')) {
1157 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id
.'/'.$parent->idnumber
.'/'.$parent->shortname
.')</li></ul>';
1161 //Parent course not found, notice!
1162 if (!defined('RESTORE_SILENTLY')) {
1163 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id
.'/'.$parent->idnumber
.'/'.$parent->shortname
.')</li></ul>';
1173 //This function creates all the gradebook data from xml, notifying
1174 //about each incidence
1175 function restore_create_gradebook($restore,$xml_file) {
1181 if (!file_exists($xml_file)) {
1185 // Get info from xml
1186 // info will contain the number of record to process
1187 $info = restore_read_xml_gradebook($restore, $xml_file);
1189 // If we have info, then process
1194 // Count how many we have
1195 $categoriescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_categories');
1196 $itemscount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_items');
1197 $outcomecount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_outcomes');
1198 $outcomescoursescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_outcomes_courses');
1199 $gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_categories_history');
1200 $gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_grades_history');
1201 $ggthcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_grades_text_history');
1202 $gihcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_items_history');
1203 $gohcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'grade_outcomes_history');
1205 // we need to know if all grade items that were backed up are being restored
1206 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1207 // i.e. the aggregated grades of that category
1209 $restoreall = true; // set to false if any grade_item is not selected/restored
1211 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'", "old_id", "old_id, old_id")) {
1212 foreach ($recs as $rec) {
1214 if ($data = backup_getid($restore->backup_unique_code
,'grade_items',$rec->old_id
)) {
1216 $info = $data->info
;
1217 // do not restore if this grade_item is a mod, and
1218 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1221 if ($itemtype == 'mod') {
1223 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1224 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1225 if (!restore_userdata_selected($restore, $itemmodule, $iteminstance)) {
1226 // module instance not selected when restored using granular
1227 // we are not restoring all grade items, set flag to false
1228 // so that we do not process grade categories and related grade items/grades
1229 $restoreall = false;
1237 // return if nothing to restore
1238 if (!$itemscount && !$categoriescount && !$outcomecount) {
1243 if (!defined('RESTORE_SILENTLY')) {
1246 // Number of records to get in every chunk
1247 $recordset_size = 2;
1248 // Flag to mark if we must continue
1251 // Process categories
1252 if ($categoriescount && $continue && $restoreall) {
1253 if (!defined('RESTORE_SILENTLY')) {
1254 echo '<li>'.get_string('gradecategories','grades').'</li>';
1257 while ($counter < $categoriescount) {
1258 // Fetch recordset_size records in each iteration
1259 $recs = get_records_select("backup_ids","table_name = 'grade_categories' AND backup_code = '$restore->backup_unique_code'",
1265 foreach ($recs as $rec) {
1266 // Get the full record from backup_ids
1267 $data = backup_getid($restore->backup_unique_code
,'grade_categories',$rec->old_id
);
1269 // Now get completed xmlized object
1270 $info = $data->info
;
1271 //traverse_xmlize($info); //Debug
1272 //print_object ($GLOBALS['traverse_array']); //Debug
1273 //$GLOBALS['traverse_array']=""; //Debug
1274 //Now build the GRADE_PREFERENCES record structure
1276 $dbrec->courseid
= $restore->course_id
;
1278 // get the new grade category parent
1279 if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#'])) {
1280 $parent = backup_getid($restore->backup_unique_code
,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
1281 $dbrec->parent
= $parent->new_id
;
1284 $dbrec->fullname
= backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
1285 $dbrec->aggregation
= backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
1286 $dbrec->keephigh
= backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
1287 $dbrec->droplow
= backup_todb($info['GRADE_CATEGORY']['#']['DROPLOW']['0']['#']);
1288 $dbrec->aggregateoutcomes
= backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
1290 //Structure is equal to db, insert record
1291 //if the fullname doesn't exist
1292 if (!$prerec = get_record('grade_categories','courseid',$dbrec->courseid
,'fullname',$dbrec->fullname
)) {
1293 $newid = insert_record('grade_categories',$dbrec);
1294 $status = backup_putid($restore->backup_unique_code
,'grade_categories',$rec->old_id
,$newid);
1295 // update this record so we can put in the right paths
1296 // this can only be done after we got the new id
1297 $dbrec->id
= $newid;
1298 include_once($CFG->dirroot
.'/lib/grade/grade_category.php');
1299 // rebuild the path, we need only parents info
1300 // the order of restoring should ensure that the parent and grandparent(s)
1301 // are already restored
1302 $dbrec->path
= grade_category
::build_path($dbrec);
1303 // this is not needed in the xml because
1304 // given this parent and grandparent(s) we can recalculate the depth
1305 $dbrec->depth
= substr_count($dbrec->path
, '/');
1306 update_record('grade_categories', $dbrec);
1308 // if fullname already exists, we should keep the current grade category
1309 $status = backup_putid($restore->backup_unique_code
,'grade_categories',$rec->old_id
,$rec->oldid
);
1312 //Increment counters
1315 if ($counter %
1 == 0) {
1316 if (!defined('RESTORE_SILENTLY')) {
1318 if ($counter %
20 == 0) {
1330 if ($outcomecount && $continue) {
1331 if (!defined('RESTORE_SILENTLY')) {
1332 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1335 while ($counter < $outcomecount) {
1336 //Fetch recordset_size records in each iteration
1337 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1343 foreach ($recs as $rec) {
1344 //Get the full record from backup_ids
1345 $data = backup_getid($restore->backup_unique_code
,'grade_outcomes',$rec->old_id
);
1347 //Now get completed xmlized object
1348 $info = $data->info
;
1349 //traverse_xmlize($info); //Debug
1350 //print_object ($GLOBALS['traverse_array']); //Debug
1351 //$GLOBALS['traverse_array']=""; //Debug
1352 //Now build the GRADE_PREFERENCES record structure
1353 if ($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']) {
1354 $dbrec->courseid
= $restore->course_id
;
1356 $dbrec->courseid
= NULL;
1358 $dbrec->shortname
= backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1359 $dbrec->fullname
= backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#']);
1361 if ($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']) {
1362 $scale = backup_getid($restore->backup_unique_code
,"scale",backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']));
1363 $dbrec->scaleid
= $scale->new_id
;
1366 $modifier = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#']));
1367 $dbrec->usermodified
= $modifier->new_id
;
1369 // Structure is equal to db, insert record
1370 // If the shortname doesn't exist
1372 if (empty($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#'])) {
1373 $prerec = get_record_sql("SELECT * FROM {$CFG->prefix}grade_outcomes
1374 WHERE courseid IS NULL
1375 AND shortname = '$dbrec->shortname'");
1377 $prerec = get_record('grade_outcomes','courseid',$restore->course_id
,'shortname',$dbrec->shortname
);
1381 $newid = insert_record('grade_outcomes',$dbrec);
1383 $newid = $prerec->id
;
1387 backup_putid($restore->backup_unique_code
,"grade_outcomes", $rec->old_id
, $newid);
1390 //Increment counters
1393 if ($counter %
1 == 0) {
1394 if (!defined('RESTORE_SILENTLY')) {
1396 if ($counter %
20 == 0) {
1407 // process outcomescourses
1408 if ($outcomescoursescount && $continue) {
1409 if (!defined('RESTORE_SILENTLY')) {
1410 echo '<li>'.get_string('gradeoutcomescourses','grades').'</li>';
1413 while ($counter < $outcomescoursescount) {
1414 //Fetch recordset_size records in each iteration
1415 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_courses' AND backup_code = '$restore->backup_unique_code'",
1421 foreach ($recs as $rec) {
1422 //Get the full record from backup_ids
1423 $data = backup_getid($restore->backup_unique_code
,'grade_outcomes_courses',$rec->old_id
);
1425 //Now get completed xmlized object
1426 $info = $data->info
;
1427 //traverse_xmlize($info); //Debug
1428 //print_object ($GLOBALS['traverse_array']); //Debug
1429 //$GLOBALS['traverse_array']=""; //Debug
1431 $oldoutcomesid = backup_todb($info['GRADE_OUTCOMES_COURSE']['#']['OUTCOMEID']['0']['#']);
1432 $newoutcome = backup_getid($restore->backup_unique_code
,"grade_outcomes",$oldoutcomesid);
1434 $dbrec->courseid
= $restore->course_id
;
1435 $dbrec->outcomeid
= $newoutcome->new_id
;
1436 insert_record('grade_outcomes_courses', $dbrec);
1438 //Increment counters
1441 if ($counter %
1 == 0) {
1442 if (!defined('RESTORE_SILENTLY')) {
1444 if ($counter %
20 == 0) {
1455 // Process grade items (grade_raw, grade_final, and grade_text)
1456 if ($itemscount && $continue) {
1457 if (!defined('RESTORE_SILENTLY')) {
1458 echo '<li>'.get_string('gradeitems','grades').'</li>';
1462 while ($counteritems < $itemscount) {
1464 //Fetch recordset_size records in each iteration
1465 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1471 foreach ($recs as $rec) {
1472 //Get the full record from backup_ids
1473 $data = backup_getid($restore->backup_unique_code
,'grade_items',$rec->old_id
);
1476 //Now get completed xmlized object
1477 $info = $data->info
;
1478 //traverse_xmlize($info); //Debug
1479 //print_object ($GLOBALS['traverse_array']); //Debug
1480 //$GLOBALS['traverse_array']=""; //Debug
1482 $dbrec->courseid
= $restore->course_id
;
1484 if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'])) {
1485 $category = backup_getid($restore->backup_unique_code
,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
1486 $dbrec->categoryid
= $category->new_id
;
1489 $dbrec->itemname
= backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
1490 $dbrec->itemtype
= backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1491 $dbrec->itemmodule
= backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1492 /// this needs to point to either the new mod id
1493 /// or the category id
1494 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1496 // do not restore if this grade_item is a mod, and
1497 if ($dbrec->itemtype
== 'mod') {
1499 if (!restore_userdata_selected($restore, $dbrec->itemmodule
, $iteminstance)) {
1500 // module instance not selected when restored using granular
1506 // iteminstance should point to new mod
1508 $mod = backup_getid($restore->backup_unique_code
,$dbrec->itemmodule
, $iteminstance);
1509 $dbrec->iteminstance
= $mod->new_id
;
1511 } else if ($dbrec->itemtype
== 'category') {
1512 // the item instance should point to the new grade category
1514 // only proceed if we are restoring all grade items
1516 $category = backup_getid($restore->backup_unique_code
,'grade_categories', $iteminstance);
1517 $dbrec->iteminstance
= $category->new_id
;
1519 // otherwise we can safely ignore this grade item and subsequent
1520 // grade_raws, grade_finals etc
1523 } elseif ($dbrec->itemtype
== 'course') { // We don't restore course type to avoid duplicate course items
1525 // TODO any special code needed here to restore course item without duplicating it?
1526 // find the course category with depth 1, and course id = current course id
1527 // this would have been already restored
1529 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id
);
1530 $dbrec->iteminstance
= $cat->id
;
1537 $dbrec->itemnumber
= backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
1538 $dbrec->iteminfo
= backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#']);
1539 $dbrec->idnumber
= backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#']);
1540 $dbrec->calculation
= backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#']);
1541 $dbrec->grademax
= backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#']);
1542 $dbrec->grademin
= backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#']);
1543 /// needs to be restored first
1545 if ($info['GRADE_ITEM']['#']['SCALEID']['0']['#']) {
1546 $scale = backup_getid($restore->backup_unique_code
,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#']));
1547 $dbrec->scaleid
= $scale->new_id
;
1550 /// needs to be restored first
1551 $dbrec->outcomeid
= backup_getid($restore->backup_unique_code
,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']));
1553 $dbrec->gradepass
= backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#']);
1554 $dbrec->multfactor
= backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
1555 $dbrec->plusfactor
= backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
1556 $dbrec->hidden
= backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);
1557 $dbrec->locked
= backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);
1558 $dbrec->locktime
= backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);
1561 // get the current sortorder, add 1 to it and use that
1562 if ($lastitem = get_record_sql("SELECT sortorder, id FROM {$CFG->prefix}grade_items
1563 WHERE courseid = $restore->course_id
1564 ORDER BY sortorder DESC ", true)) {
1566 // we just need the first one
1567 $dbrec->sortorder
= $lastitem->sortorder +
1;
1569 // this is the first grade_item
1570 $dbrec->sortorder
= 1;
1572 // always insert, since modules restored to existing courses are always inserted
1573 $itemid = insert_record('grade_items',$dbrec);
1575 backup_putid($restore->backup_unique_code
,'grade_items', backup_todb($info['GRADE_ITEM']['#']['ID']['0']['#']), $itemid);
1577 /// now, restore grade_grades, grade_text
1578 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1579 //Iterate over items
1580 for($i = 0; $i < sizeof($grades); $i++
) {
1581 $ite_info = $grades[$i];
1582 //traverse_xmlize($ite_info);
1584 //print_object ($GLOBALS['traverse_array']); //Debug
1585 //$GLOBALS['traverse_array']=""; //Debug
1586 //Now build the GRADE_ITEM record structure
1587 $grade = new object();
1588 $grade->itemid
= $itemid;
1589 $user = backup_getid($restore->backup_unique_code
,"user", backup_todb($ite_info['#']['USERID']['0']['#']));
1590 $grade->userid
= $user->new_id
;
1591 $grade->rawgrade
= backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
1592 $grade->rawgrademax
= backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
1593 $grade->rawgrademin
= backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
1594 // need to find scaleid
1595 if ($ite_info['#']['RAWSCALEID']['0']['#']) {
1596 $scale = backup_getid($restore->backup_unique_code
,"scale",backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
1597 $grade->rawscaleid
= $scale->new_id
;
1599 $grade->finalgrade
= backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
1600 $grade->hidden
= backup_todb($ite_info['#']['HIDDEN']['0']['#']);
1601 $grade->locked
= backup_todb($ite_info['#']['LOCKED']['0']['#']);
1602 $grade->locktime
= backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
1603 $grade->exported
= backup_todb($ite_info['#']['EXPORTED']['0']['#']);
1604 $grade->overridden
= backup_todb($ite_info['#']['OVERRIDDEN']['0']['#']);
1605 $grade->excluded
= backup_todb($ite_info['#']['EXCLUDED']['0']['#']);
1607 $newid = insert_record('grade_grades', $grade);
1610 backup_putid($restore->backup_unique_code
,"grade_grades", backup_todb($ite_info['#']['ID']['0']['#']), $newid);
1613 if ($counter %
20 == 0) {
1614 if (!defined('RESTORE_SILENTLY')) {
1616 if ($counter %
400 == 0) {
1627 /// processing grade_grades_text
1628 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']) && ($texts = $info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']['GRADE_TEXT'])) {
1629 //Iterate over items
1630 for($i = 0; $i < sizeof($texts); $i++
) {
1631 $ite_info = $texts[$i];
1632 //traverse_xmlize($ite_info); //Debug
1633 //print_object ($GLOBALS['traverse_array']); //Debug
1634 //$GLOBALS['traverse_array']=""; //Debug
1635 $grade = backup_getid($restore->backup_unique_code
,"grade_grades", backup_todb($ite_info['#']['GRADEID']['0']['#']));
1637 $text = new object();
1638 $text->gradeid
= $grade->new_id
;
1639 $text->information
= backup_todb($ite_info['#']['INFORMATION']['0']['#']);
1640 $text->informationformat
= backup_todb($ite_info['#']['INFORMATIONFORMAT']['0']['#']);
1641 $text->feedback
= backup_todb($ite_info['#']['FEEDBACK']['0']['#']);
1642 $text->feedbackformat
= backup_todb($ite_info['#']['FEEDBACKFORMAT']['0']['#']);
1644 $newid = insert_record('grade_grades_text', $text);
1646 backup_putid($restore->backup_unique_code
,'grade_grades_text', backup_todb($ite_info['#']['ID']['0']['#']), $newid);
1649 if ($counter %
20 == 0) {
1650 if (!defined('RESTORE_SILENTLY')) {
1652 if ($counter %
400 == 0) {
1661 $counteritems++
; // increment item count
1668 // process histories
1669 if ($gchcount && $continue) {
1670 if (!defined('RESTORE_SILENTLY')) {
1671 echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
1674 while ($counter < $gchcount) {
1675 //Fetch recordset_size records in each iteration
1676 $recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
1682 foreach ($recs as $rec) {
1683 //Get the full record from backup_ids
1684 $data = backup_getid($restore->backup_unique_code
,'grade_categories_history',$rec->old_id
);
1686 //Now get completed xmlized object
1687 $info = $data->info
;
1688 //traverse_xmlize($info); //Debug
1689 //print_object ($GLOBALS['traverse_array']); //Debug
1690 //$GLOBALS['traverse_array']=""; //Debug
1692 $oldobj = backup_getid($restore->backup_unique_code
,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
1693 if (empty($oldobj->new_id
)) {
1694 // if the old object is not being restored, can't restoring its history
1698 $dbrec->oldid
= $oldobj->new_id
;
1699 $dbrec->action
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
1700 $dbrec->source
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
1701 $dbrec->timemodified
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1703 // loggeduser might not be restored, e.g. admin
1704 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1705 $dbrec->loggeduser
= $oldobj->new_id
;
1708 // this item might not have a parent at all, do not skip it if no parent is specified
1709 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
1710 $oldobj = backup_getid($restore->backup_unique_code
,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
1711 if (empty($oldobj->new_id
)) {
1712 // if the parent category not restored
1717 $dbrec->parent
= $oldobj->new_id
;
1718 $dbrec->depth
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
1719 // path needs to be rebuilt
1720 if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
1721 // to preserve the path and make it work, we need to replace the categories one by one
1722 // we first get the list of categories in current path
1723 if ($paths = explode("/", $path)) {
1725 foreach ($paths as $catid) {
1727 // find the new corresponding path
1728 $oldpath = backup_getid($restore->backup_unique_code
,"grade_categories", $catid);
1729 $newpath .= "/$oldpath->new_id";
1732 $dbrec->path
= $newpath;
1735 $dbrec->fullname
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
1736 $dbrec->aggregation
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
1737 $dbrec->keephigh
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
1738 $dbrec->droplow
= backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
1739 $dbrec->courseid
= $restore->course_id
;
1740 insert_record('grade_categories_history', $dbrec);
1744 //Increment counters
1747 if ($counter %
1 == 0) {
1748 if (!defined('RESTORE_SILENTLY')) {
1750 if ($counter %
20 == 0) {
1761 // process histories
1762 if ($gghcount && $continue) {
1763 if (!defined('RESTORE_SILENTLY')) {
1764 echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
1767 while ($counter < $gghcount) {
1768 //Fetch recordset_size records in each iteration
1769 $recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
1775 foreach ($recs as $rec) {
1776 //Get the full record from backup_ids
1777 $data = backup_getid($restore->backup_unique_code
,'grade_grades_history',$rec->old_id
);
1779 //Now get completed xmlized object
1780 $info = $data->info
;
1781 //traverse_xmlize($info); //Debug
1782 //print_object ($GLOBALS['traverse_array']); //Debug
1783 //$GLOBALS['traverse_array']=""; //Debug
1785 $oldobj = backup_getid($restore->backup_unique_code
,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
1786 if (empty($oldobj->new_id
)) {
1787 // if the old object is not being restored, can't restoring its history
1791 $dbrec->oldid
= $oldobj->new_id
;
1792 $dbrec->action
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
1793 $dbrec->source
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
1794 $dbrec->timemodified
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1795 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1796 $dbrec->loggeduser
= $oldobj->new_id
;
1798 $oldobj = backup_getid($restore->backup_unique_code
,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
1799 $dbrec->itemid
= $oldobj->new_id
;
1800 if (empty($dbrec->itemid
)) {
1802 continue; // grade item not being restored
1804 $oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
1805 $dbrec->userid
= $oldobj->new_id
;
1806 $dbrec->rawgrade
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
1807 $dbrec->rawgrademax
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
1808 $dbrec->rawgrademin
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
1809 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
1810 $dbrec->usermodified
= $oldobj->new_id
;
1812 $dbrec->finalgrade
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
1813 $dbrec->hidden
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
1814 $dbrec->locked
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
1815 $dbrec->locktime
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
1816 $dbrec->exported
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
1817 $dbrec->overridden
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
1818 $dbrec->excluded
= backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
1820 insert_record('grade_grades_history', $dbrec);
1824 //Increment counters
1827 if ($counter %
1 == 0) {
1828 if (!defined('RESTORE_SILENTLY')) {
1830 if ($counter %
20 == 0) {
1841 // process histories
1843 if ($ggthcount && $continue) {
1844 if (!defined('RESTORE_SILENTLY')) {
1845 echo '<li>'.get_string('gradegradestexthistory','grades').'</li>';
1848 while ($counter < $ggthcount) {
1849 //Fetch recordset_size records in each iteration
1850 $recs = get_records_select("backup_ids","table_name = 'grade_grades_text_history' AND backup_code = '$restore->backup_unique_code'",
1857 foreach ($recs as $rec) {
1858 //Get the full record from backup_ids
1859 $data = backup_getid($restore->backup_unique_code
,'grade_grades_text_history',$rec->old_id
);
1861 //Now get completed xmlized object
1862 $info = $data->info
;
1863 //traverse_xmlize($info); //Debug
1864 //print_object ($GLOBALS['traverse_array']); //Debug
1865 //$GLOBALS['traverse_array']=""; //Debug
1867 $oldobj = backup_getid($restore->backup_unique_code
,"grade_grades_text", backup_todb($info['GRADE_TEXT_HISTORY']['#']['OLDID']['0']['#']));
1868 if (empty($oldobj->new_id
)) {
1869 // if the old object is not being restored, can't restoring its history
1873 $dbrec->oldid
= $oldobj->new_id
;
1874 $dbrec->action
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['ACTION']['0']['#']);
1875 $dbrec->source
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['SOURCE']['0']['#']);
1876 $dbrec->timemodified
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1877 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1878 $dbrec->loggeduser
= $oldobj->new_id
;
1880 $oldobj = backup_getid($restore->backup_unique_code
,"grade_grades", backup_todb($info['GRADE_TEXT_HISTORY']['#']['GRADEID']['0']['#']));
1881 $dbrec->gradeid
= $oldobj->new_id
;
1882 if (empty($dbrec->gradeid
)) {
1884 continue; // grade not being restore, possibly because grade item is not restored
1886 $oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['USERID']['0']['#']));
1887 $dbrec->information
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
1888 $dbrec->informationformat
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
1889 $dbrec->feedback
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
1890 $dbrec->feedbackformat
= backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
1891 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
1892 $dbrec->usermodified
= $oldobj->new_id
;
1895 insert_record('grade_grades_text_history', $dbrec);
1899 //Increment counters
1902 if ($counter %
1 == 0) {
1903 if (!defined('RESTORE_SILENTLY')) {
1905 if ($counter %
20 == 0) {
1916 // process histories
1917 if ($gihcount && $continue) {
1918 if (!defined('RESTORE_SILENTLY')) {
1919 echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
1922 while ($counter < $gihcount) {
1923 //Fetch recordset_size records in each iteration
1924 $recs = get_records_select("backup_ids","table_name = 'grade_items_history' AND backup_code = '$restore->backup_unique_code'",
1930 foreach ($recs as $rec) {
1931 //Get the full record from backup_ids
1932 $data = backup_getid($restore->backup_unique_code
,'grade_items_history',$rec->old_id
);
1934 //Now get completed xmlized object
1935 $info = $data->info
;
1936 //traverse_xmlize($info); //Debug
1937 //print_object ($GLOBALS['traverse_array']); //Debug
1938 //$GLOBALS['traverse_array']=""; //Debug
1941 $oldobj = backup_getid($restore->backup_unique_code
,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
1942 if (empty($oldobj->new_id
)) {
1943 // if the old object is not being restored, can't restoring its history
1947 $dbrec->oldid
= $oldobj->new_id
;
1948 $dbrec->action
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
1949 $dbrec->source
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
1950 $dbrec->timemodified
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1951 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1952 $dbrec->loggeduser
= $oldobj->new_id
;
1954 $oldobj = backup_getid($restore->backup_unique_code
,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
1955 $oldobj->categoryid
= $category->new_id
;
1956 if (empty($oldobj->categoryid
)) {
1958 continue; // category not restored
1961 $dbrec->itemname
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
1962 $dbrec->itemtype
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
1963 $dbrec->itemmodule
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
1965 // code from grade_items restore
1966 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
1967 // do not restore if this grade_item is a mod, and
1968 if ($dbrec->itemtype
== 'mod') {
1970 if (!restore_userdata_selected($restore, $dbrec->itemmodule
, $iteminstance)) {
1971 // module instance not selected when restored using granular
1977 // iteminstance should point to new mod
1979 $mod = backup_getid($restore->backup_unique_code
,$dbrec->itemmodule
, $iteminstance);
1980 $dbrec->iteminstance
= $mod->new_id
;
1982 } else if ($dbrec->itemtype
== 'category') {
1983 // the item instance should point to the new grade category
1985 // only proceed if we are restoring all grade items
1987 $category = backup_getid($restore->backup_unique_code
,'grade_categories', $iteminstance);
1988 $dbrec->iteminstance
= $category->new_id
;
1990 // otherwise we can safely ignore this grade item and subsequent
1991 // grade_raws, grade_finals etc
1994 } elseif ($dbrec->itemtype
== 'course') { // We don't restore course type to avoid duplicate course items
1996 // TODO any special code needed here to restore course item without duplicating it?
1997 // find the course category with depth 1, and course id = current course id
1998 // this would have been already restored
2000 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id
);
2001 $dbrec->iteminstance
= $cat->id
;
2009 $dbrec->itemnumber
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
2010 $dbrec->iteminfo
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
2011 $dbrec->idnumber
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
2012 $dbrec->calculation
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
2013 $dbrec->gradetype
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
2014 $dbrec->grademax
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
2015 $dbrec->grademin
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
2016 if ($oldobj = backup_getid($restore->backup_unique_code
,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
2017 // scaleid is optional
2018 $dbrec->scaleid
= $oldobj->new_id
;
2020 if ($oldobj = backup_getid($restore->backup_unique_code
,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
2021 // outcome is optional
2022 $dbrec->outcomeid
= $oldobj->new_id
;
2024 $dbrec->gradepass
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
2025 $dbrec->multfactor
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
2026 $dbrec->plusfactor
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
2027 $dbrec->aggregationcoef
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
2028 $dbrec->sortorder
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
2029 $dbrec->hidden
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
2030 $dbrec->locked
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
2031 $dbrec->locktime
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
2032 $dbrec->needsupdate
= backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
2034 insert_record('grade_items_history', $dbrec);
2038 //Increment counters
2041 if ($counter %
1 == 0) {
2042 if (!defined('RESTORE_SILENTLY')) {
2044 if ($counter %
20 == 0) {
2055 // process histories
2056 if ($gohcount && $continue) {
2057 if (!defined('RESTORE_SILENTLY')) {
2058 echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
2061 while ($counter < $gohcount) {
2062 //Fetch recordset_size records in each iteration
2063 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_history' AND backup_code = '$restore->backup_unique_code'",
2069 foreach ($recs as $rec) {
2070 //Get the full record from backup_ids
2071 $data = backup_getid($restore->backup_unique_code
,'grade_outcomes_history',$rec->old_id
);
2073 //Now get completed xmlized object
2074 $info = $data->info
;
2075 //traverse_xmlize($info); //Debug
2076 //print_object ($GLOBALS['traverse_array']); //Debug
2077 //$GLOBALS['traverse_array']=""; //Debug
2079 $oldobj = backup_getid($restore->backup_unique_code
,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
2080 if (empty($oldobj->new_id
)) {
2081 // if the old object is not being restored, can't restoring its history
2085 $dbrec->oldid
= $oldobj->new_id
;
2086 $dbrec->action
= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
2087 $dbrec->source
= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
2088 $dbrec->timemodified
= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2089 if ($oldobj = backup_getid($restore->backup_unique_code
,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2090 $dbrec->loggeduser
= $oldobj->new_id
;
2092 $dbrec->shortname
= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
2093 $dbrec->fullname
= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
2094 $oldobj = backup_getid($restore->backup_unique_code
,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
2095 $dbrec->scaleid
= $oldobj->new_id
;
2096 $dbrec->description
= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
2098 insert_record('grade_outcomes_history', $dbrec);
2102 //Increment counters
2105 if ($counter %
1 == 0) {
2106 if (!defined('RESTORE_SILENTLY')) {
2108 if ($counter %
20 == 0) {
2119 if (!defined('RESTORE_SILENTLY')) {
2126 //This function creates all the user, user_students, user_teachers
2127 //user_course_creators and user_admins from xml
2128 function restore_create_users($restore,$xml_file) {
2134 if (!file_exists($xml_file)) {
2139 //info will contain the old_id of every user
2140 //in backup_ids->info will be the real info (serialized)
2141 $info = restore_read_xml_users($restore,$xml_file);
2144 //Now, get evey user_id from $info and user data from $backup_ids
2145 //and create the necessary records (users, user_students, user_teachers
2146 //user_course_creators and user_admins
2147 if (!empty($info->users
)) {
2148 // Grab mnethosts keyed by wwwroot, to map to id
2149 $mnethosts = get_records('mnet_host', '', '',
2150 'wwwroot', 'wwwroot, id');
2152 $languages = get_list_of_languages();
2154 foreach ($info->users
as $userid) {
2155 $rec = backup_getid($restore->backup_unique_code
,"user",$userid);
2158 //Now, recode some languages (Moodle 1.5)
2159 if ($user->lang
== 'ma_nt') {
2160 $user->lang
= 'mi_nt';
2164 //If language does not exist here - use site default
2165 if (!array_key_exists($user->lang
, $languages)) {
2166 $user->lang
= $CFG->lang
;
2169 //Check if it's admin and coursecreator
2170 $is_admin = !empty($user->roles
['admin']);
2171 $is_coursecreator = !empty($user->roles
['coursecreator']);
2173 //Check if it's teacher and student
2174 $is_teacher = !empty($user->roles
['teacher']);
2175 $is_student = !empty($user->roles
['student']);
2177 //Check if it's needed
2178 $is_needed = !empty($user->roles
['needed']);
2180 //Calculate if it is a course user
2181 //Has role teacher or student or needed
2182 $is_course_user = ($is_teacher or $is_student or $is_needed);
2184 //Calculate mnethostid
2185 if (empty($user->mnethosturl
) ||
$user->mnethosturl
===$CFG->wwwroot
) {
2186 $user->mnethostid
= $CFG->mnet_localhost_id
;
2188 // fast url-to-id lookups
2189 if (isset($mnethosts[$user->mnethosturl
])) {
2190 $user->mnethostid
= $mnethosts[$user->mnethosturl
]->id
;
2192 // should not happen, as we check in restore_chech.php
2193 // but handle the error if it does
2194 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
2197 unset($user->mnethosturl
);
2199 //To store new ids created
2201 //check if it exists (by username) and get its id
2202 $user_exists = true;
2203 $user_data = get_record("user","username",addslashes($user->username
),
2204 'mnethostid', $user->mnethostid
);
2206 $user_exists = false;
2208 $newid = $user_data->id
;
2210 //Flags to see if we have to create the user, roles and preferences
2211 $create_user = true;
2212 $create_roles = true;
2213 $create_preferences = true;
2215 //If we are restoring course users and it isn't a course user
2216 if ($restore->users
== 1 and !$is_course_user) {
2217 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
2218 $status = backup_putid($restore->backup_unique_code
,"user",$userid,null,'notincourse');
2219 $create_user = false;
2220 $create_roles = false;
2221 $create_preferences = false;
2224 if ($user_exists and $create_user) {
2225 //If user exists mark its newid in backup_ids (the same than old)
2226 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,'exists');
2227 $create_user = false;
2230 //Here, if create_user, do it
2232 //Unset the id because it's going to be inserted with a new one
2234 //We addslashes to necessary fields
2235 $user->username
= addslashes($user->username
);
2236 $user->firstname
= addslashes($user->firstname
);
2237 $user->lastname
= addslashes($user->lastname
);
2238 $user->email
= addslashes($user->email
);
2239 $user->institution
= addslashes($user->institution
);
2240 $user->department
= addslashes($user->department
);
2241 $user->address
= addslashes($user->address
);
2242 $user->city
= addslashes($user->city
);
2243 $user->url
= addslashes($user->url
);
2244 $user->description
= backup_todb($user->description
);
2246 //We need to analyse the AUTH field to recode it:
2247 // - if the field isn't set, we are in a pre 1.4 backup and we'll
2250 if (empty($user->auth
)) {
2251 if ($CFG->registerauth
== 'email') {
2252 $user->auth
= 'email';
2254 $user->auth
= 'manual';
2258 //We need to process the POLICYAGREED field to recalculate it:
2259 // - if the destination site is different (by wwwroot) reset it.
2260 // - if the destination site is the same (by wwwroot), leave it unmodified
2262 if ($restore->original_wwwroot
!= $CFG->wwwroot
) {
2263 $user->policyagreed
= 0;
2265 //Nothing to do, we are in the same server
2268 //Check if the theme exists in destination server
2269 $themes = get_list_of_themes();
2270 if (!in_array($user->theme
, $themes)) {
2274 //We are going to create the user
2275 //The structure is exactly as we need
2276 $newid = insert_record ("user",$user);
2278 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,"new");
2281 //Here, if create_roles, do it as necessary
2282 if ($create_roles) {
2283 //Get the newid and current info from backup_ids
2284 $data = backup_getid($restore->backup_unique_code
,"user",$userid);
2285 $newid = $data->new_id
;
2286 $currinfo = $data->info
.",";
2288 //Now, depending of the role, create records in user_studentes and user_teacher
2289 //and/or mark it in backup_ids
2292 //If the record (user_admins) doesn't exists
2293 //Only put status in backup_ids
2294 $currinfo = $currinfo."admin,";
2295 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,$currinfo);
2297 if ($is_coursecreator) {
2298 //If the record (user_coursecreators) doesn't exists
2299 //Only put status in backup_ids
2300 $currinfo = $currinfo."coursecreator,";
2301 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,$currinfo);
2304 //Only put status in backup_ids
2305 $currinfo = $currinfo."needed,";
2306 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,$currinfo);
2309 //If the record (teacher) doesn't exists
2310 //Put status in backup_ids
2311 $currinfo = $currinfo."teacher,";
2312 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,$currinfo);
2313 //Set course and user
2314 $user->roles
['teacher']->course
= $restore->course_id
;
2315 $user->roles
['teacher']->userid
= $newid;
2317 //Need to analyse the enrol field
2318 // - if it isn't set, set it to $CFG->enrol
2319 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2320 // - if we are in the same server (by wwwroot), maintain it unmodified.
2321 if (empty($user->roles
['teacher']->enrol
)) {
2322 $user->roles
['teacher']->enrol
= $CFG->enrol
;
2323 } else if ($restore->original_wwwroot
!= $CFG->wwwroot
) {
2324 $user->roles
['teacher']->enrol
= $CFG->enrol
;
2326 //Nothing to do. Leave it unmodified
2329 $rolesmapping = $restore->rolesmapping
;
2330 $context = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
2331 if ($user->roles
['teacher']->editall
) {
2332 role_assign($rolesmapping['defaultteacheredit'],
2336 $user->roles
['teacher']->timestart
,
2337 $user->roles
['teacher']->timeend
,
2339 $user->roles
['teacher']->enrol
);
2343 // non editting teacher
2344 role_assign($rolesmapping['defaultteacher'],
2348 $user->roles
['teacher']->timestart
,
2349 $user->roles
['teacher']->timeend
,
2351 $user->roles
['teacher']->enrol
);
2356 //Put status in backup_ids
2357 $currinfo = $currinfo."student,";
2358 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,$currinfo);
2359 //Set course and user
2360 $user->roles
['student']->course
= $restore->course_id
;
2361 $user->roles
['student']->userid
= $newid;
2363 //Need to analyse the enrol field
2364 // - if it isn't set, set it to $CFG->enrol
2365 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2366 // - if we are in the same server (by wwwroot), maintain it unmodified.
2367 if (empty($user->roles
['student']->enrol
)) {
2368 $user->roles
['student']->enrol
= $CFG->enrol
;
2369 } else if ($restore->original_wwwroot
!= $CFG->wwwroot
) {
2370 $user->roles
['student']->enrol
= $CFG->enrol
;
2372 //Nothing to do. Leave it unmodified
2374 $rolesmapping = $restore->rolesmapping
;
2375 $context = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
2377 role_assign($rolesmapping['defaultstudent'],
2381 $user->roles
['student']->timestart
,
2382 $user->roles
['student']->timeend
,
2384 $user->roles
['student']->enrol
);
2387 if (!$is_course_user) {
2388 //If the record (user) doesn't exists
2389 if (!record_exists("user","id",$newid)) {
2390 //Put status in backup_ids
2391 $currinfo = $currinfo."user,";
2392 $status = backup_putid($restore->backup_unique_code
,"user",$userid,$newid,$currinfo);
2397 //Here, if create_preferences, do it as necessary
2398 if ($create_preferences) {
2399 //echo "Checking for preferences of user ".$user->username."<br />"; //Debug
2400 //Get user new id from backup_ids
2401 $data = backup_getid($restore->backup_unique_code
,"user",$userid);
2402 $newid = $data->new_id
;
2403 if (isset($user->user_preferences
)) {
2404 //echo "Preferences exist in backup file<br />"; //Debug
2405 foreach($user->user_preferences
as $user_preference) {
2406 //echo $user_preference->name." = ".$user_preference->value."<br />"; //Debug
2407 //We check if that user_preference exists in DB
2408 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name
)) {
2409 //echo "Creating it<br />"; //Debug
2410 //Prepare the record and insert it
2411 $user_preference->userid
= $newid;
2412 $status = insert_record("user_preferences",$user_preference);
2423 //This function creates all the structures messages and contacts
2424 function restore_create_messages($restore,$xml_file) {
2430 if (!file_exists($xml_file)) {
2435 //info will contain the id and name of every table
2436 //(message, message_read and message_contacts)
2437 //in backup_ids->info will be the real info (serialized)
2438 $info = restore_read_xml_messages($restore,$xml_file);
2440 //If we have info, then process messages & contacts
2442 //Count how many we have
2443 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'message');
2444 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'message_read');
2445 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code
, 'table_name', 'message_contacts');
2446 if ($unreadcount ||
$readcount ||
$contactcount) {
2448 if (!defined('RESTORE_SILENTLY')) {
2451 //Number of records to get in every chunk
2452 $recordset_size = 4;
2456 if (!defined('RESTORE_SILENTLY')) {
2457 echo '<li>'.get_string('unreadmessages','message').'</li>';
2460 while ($counter < $unreadcount) {
2461 //Fetch recordset_size records in each iteration
2462 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2464 foreach ($recs as $rec) {
2465 //Get the full record from backup_ids
2466 $data = backup_getid($restore->backup_unique_code
,"message",$rec->old_id
);
2468 //Now get completed xmlized object
2469 $info = $data->info
;
2470 //traverse_xmlize($info); //Debug
2471 //print_object ($GLOBALS['traverse_array']); //Debug
2472 //$GLOBALS['traverse_array']=""; //Debug
2473 //Now build the MESSAGE record structure
2474 $dbrec = new object();
2475 $dbrec->useridfrom
= backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2476 $dbrec->useridto
= backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2477 $dbrec->message
= backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2478 $dbrec->format
= backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2479 $dbrec->timecreated
= backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2480 $dbrec->messagetype
= backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2481 //We have to recode the useridfrom field
2482 $user = backup_getid($restore->backup_unique_code
,"user",$dbrec->useridfrom
);
2484 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2485 $dbrec->useridfrom
= $user->new_id
;
2487 //We have to recode the useridto field
2488 $user = backup_getid($restore->backup_unique_code
,"user",$dbrec->useridto
);
2490 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2491 $dbrec->useridto
= $user->new_id
;
2493 //Check if the record doesn't exist in DB!
2494 $exist = get_record('message','useridfrom',$dbrec->useridfrom
,
2495 'useridto', $dbrec->useridto
,
2496 'timecreated',$dbrec->timecreated
);
2499 $status = insert_record('message',$dbrec);
2501 //Duplicate. Do nothing
2506 if ($counter %
10 == 0) {
2507 if (!defined('RESTORE_SILENTLY')) {
2509 if ($counter %
200 == 0) {
2522 if (!defined('RESTORE_SILENTLY')) {
2523 echo '<li>'.get_string('readmessages','message').'</li>';
2526 while ($counter < $readcount) {
2527 //Fetch recordset_size records in each iteration
2528 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2530 foreach ($recs as $rec) {
2531 //Get the full record from backup_ids
2532 $data = backup_getid($restore->backup_unique_code
,"message_read",$rec->old_id
);
2534 //Now get completed xmlized object
2535 $info = $data->info
;
2536 //traverse_xmlize($info); //Debug
2537 //print_object ($GLOBALS['traverse_array']); //Debug
2538 //$GLOBALS['traverse_array']=""; //Debug
2539 //Now build the MESSAGE_READ record structure
2540 $dbrec->useridfrom
= backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2541 $dbrec->useridto
= backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2542 $dbrec->message
= backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2543 $dbrec->format
= backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2544 $dbrec->timecreated
= backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2545 $dbrec->messagetype
= backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2546 $dbrec->timeread
= backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2547 $dbrec->mailed
= backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2548 //We have to recode the useridfrom field
2549 $user = backup_getid($restore->backup_unique_code
,"user",$dbrec->useridfrom
);
2551 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2552 $dbrec->useridfrom
= $user->new_id
;
2554 //We have to recode the useridto field
2555 $user = backup_getid($restore->backup_unique_code
,"user",$dbrec->useridto
);
2557 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2558 $dbrec->useridto
= $user->new_id
;
2560 //Check if the record doesn't exist in DB!
2561 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom
,
2562 'useridto', $dbrec->useridto
,
2563 'timecreated',$dbrec->timecreated
);
2566 $status = insert_record('message_read',$dbrec);
2568 //Duplicate. Do nothing
2573 if ($counter %
10 == 0) {
2574 if (!defined('RESTORE_SILENTLY')) {
2576 if ($counter %
200 == 0) {
2588 if ($contactcount) {
2589 if (!defined('RESTORE_SILENTLY')) {
2590 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2593 while ($counter < $contactcount) {
2594 //Fetch recordset_size records in each iteration
2595 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2597 foreach ($recs as $rec) {
2598 //Get the full record from backup_ids
2599 $data = backup_getid($restore->backup_unique_code
,"message_contacts",$rec->old_id
);
2601 //Now get completed xmlized object
2602 $info = $data->info
;
2603 //traverse_xmlize($info); //Debug
2604 //print_object ($GLOBALS['traverse_array']); //Debug
2605 //$GLOBALS['traverse_array']=""; //Debug
2606 //Now build the MESSAGE_CONTACTS record structure
2607 $dbrec->userid
= backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2608 $dbrec->contactid
= backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2609 $dbrec->blocked
= backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2610 //We have to recode the userid field
2611 $user = backup_getid($restore->backup_unique_code
,"user",$dbrec->userid
);
2613 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2614 $dbrec->userid
= $user->new_id
;
2616 //We have to recode the contactid field
2617 $user = backup_getid($restore->backup_unique_code
,"user",$dbrec->contactid
);
2619 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2620 $dbrec->contactid
= $user->new_id
;
2622 //Check if the record doesn't exist in DB!
2623 $exist = get_record('message_contacts','userid',$dbrec->userid
,
2624 'contactid', $dbrec->contactid
);
2627 $status = insert_record('message_contacts',$dbrec);
2629 //Duplicate. Do nothing
2634 if ($counter %
10 == 0) {
2635 if (!defined('RESTORE_SILENTLY')) {
2637 if ($counter %
200 == 0) {
2647 if (!defined('RESTORE_SILENTLY')) {
2658 //This function creates all the categories and questions
2660 function restore_create_questions($restore,$xml_file) {
2666 if (!file_exists($xml_file)) {
2671 //info will contain the old_id of every category
2672 //in backup_ids->info will be the real info (serialized)
2673 $info = restore_read_xml_questions($restore,$xml_file);
2675 //Now, if we have anything in info, we have to restore that
2676 //categories/questions
2678 if ($info !== true) {
2679 $status = $status && restore_question_categories($info, $restore);
2687 //This function creates all the scales
2688 function restore_create_scales($restore,$xml_file) {
2694 if (!file_exists($xml_file)) {
2699 //scales will contain the old_id of every scale
2700 //in backup_ids->info will be the real info (serialized)
2701 $scales = restore_read_xml_scales($restore,$xml_file);
2703 //Now, if we have anything in scales, we have to restore that
2706 //Get admin->id for later use
2707 $admin = get_admin();
2708 $adminid = $admin->id
;
2709 if ($scales !== true) {
2710 //Iterate over each scale
2711 foreach ($scales as $scale) {
2712 //Get record from backup_ids
2713 $data = backup_getid($restore->backup_unique_code
,"scale",$scale->id
);
2715 $create_scale = false;
2718 //Now get completed xmlized object
2719 $info = $data->info
;
2720 //traverse_xmlize($info); //Debug
2721 //print_object ($GLOBALS['traverse_array']); //Debug
2722 //$GLOBALS['traverse_array']=""; //Debug
2724 //Now build the SCALE record structure
2725 $sca = new object();
2726 $sca->courseid
= backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
2727 $sca->userid
= backup_todb($info['SCALE']['#']['USERID']['0']['#']);
2728 $sca->name
= backup_todb($info['SCALE']['#']['NAME']['0']['#']);
2729 $sca->scale
= backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
2730 $sca->description
= backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
2731 $sca->timemodified
= backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
2733 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
2734 //or in restore->course_id course (Personal scale)
2735 if ($sca->courseid
== 0) {
2736 $course_to_search = 0;
2738 $course_to_search = $restore->course_id
;
2741 // scale is not course unique, use get_record_sql to suppress warning
2743 $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale
2744 WHERE scale = '$sca->scale'
2745 AND courseid = $course_to_search", true);
2747 //If it doesn't exist, create
2749 $create_scale = true;
2751 //If we must create the scale
2752 if ($create_scale) {
2753 //Me must recode the courseid if it's <> 0 (common scale)
2754 if ($sca->courseid
!= 0) {
2755 $sca->courseid
= $restore->course_id
;
2757 //We must recode the userid
2758 $user = backup_getid($restore->backup_unique_code
,"user",$sca->userid
);
2760 $sca->userid
= $user->new_id
;
2762 //Assign it to admin
2763 $sca->userid
= $adminid;
2765 //The structure is equal to the db, so insert the scale
2766 $newid = insert_record ("scale",$sca);
2768 //get current scale id
2769 $newid = $sca_db->id
;
2772 //We have the newid, update backup_ids
2773 backup_putid($restore->backup_unique_code
,"scale",
2774 $scale->id
, $newid);
2785 //This function creates all the groups
2786 function restore_create_groups($restore,$xml_file) {
2789 if (!file_exists($xml_file)) {
2793 if (!$groups = restore_read_xml_groups($restore,$xml_file)) {
2794 //groups will contain the old_id of every group
2795 //in backup_ids->info will be the real info (serialized)
2798 } else if ($groups === true) {
2804 //Iterate over each group
2805 foreach ($groups as $group) {
2806 //Get record from backup_ids
2807 $data = backup_getid($restore->backup_unique_code
,"groups",$group->id
);
2810 //Now get completed xmlized object
2811 $info = $data->info
;
2812 //traverse_xmlize($info); //Debug
2813 //print_object ($GLOBALS['traverse_array']); //Debug
2814 //$GLOBALS['traverse_array']=""; //Debug
2815 //Now build the GROUP record structure
2816 $gro = new Object();
2817 $gro->courseid
= $restore->course_id
;
2818 $gro->name
= backup_todb($info['GROUP']['#']['NAME']['0']['#']);
2819 $gro->description
= backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
2820 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
2821 $gro->enrolmentkey
= backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
2823 $gro->enrolmentkey
= backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
2825 $gro->picture
= backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
2826 $gro->hidepicture
= backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
2827 $gro->timecreated
= backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
2828 $gro->timemodified
= backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
2830 //Now search if that group exists (by name and description field) in
2831 if (!$gro_db = get_record('groups', 'courseid', $restore->course_id
, 'name', $gro->name
, 'description', $gro->description
)) {
2832 //If it doesn't exist, create
2833 $newid = insert_record('groups', $gro);
2836 //get current group id
2837 $newid = $gro_db->id
;
2841 //We have the newid, update backup_ids
2842 backup_putid($restore->backup_unique_code
,"groups", $group->id
, $newid);
2849 //Now restore members in the groups_members, only if
2850 //users are included
2851 if ($restore->users
!= 2) {
2852 if (!restore_create_groups_members($newid,$info,$restore)) {
2859 //Now, restore group_files
2861 $status = restore_group_files($restore);
2867 //This function restores the groups_members
2868 function restore_create_groups_members($group_id,$info,$restore) {
2870 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
2871 //OK, some groups have no members.
2874 //Get the members array
2875 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
2879 //Iterate over members
2880 for($i = 0; $i < sizeof($members); $i++
) {
2881 $mem_info = $members[$i];
2882 //traverse_xmlize($mem_info); //Debug
2883 //print_object ($GLOBALS['traverse_array']); //Debug
2884 //$GLOBALS['traverse_array']=""; //Debug
2886 //Now, build the GROUPS_MEMBERS record structure
2887 $group_member = new Object();
2888 $group_member->groupid
= $group_id;
2889 $group_member->userid
= backup_todb($mem_info['#']['USERID']['0']['#']);
2890 $group_member->timeadded
= backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2894 //We have to recode the userid field
2895 if (!$user = backup_getid($restore->backup_unique_code
,"user",$group_member->userid
)) {
2900 $group_member->userid
= $user->new_id
;
2902 //The structure is equal to the db, so insert the groups_members
2903 if (!insert_record ("groups_members", $group_member)) {
2909 if (($i+
1) %
50 == 0) {
2910 if (!defined('RESTORE_SILENTLY')) {
2912 if (($i+
1) %
1000 == 0) {
2923 //This function creates all the groupings
2924 function restore_create_groupings($restore,$xml_file) {
2927 if (!file_exists($xml_file)) {
2931 if (!$groupings = restore_read_xml_groupings($restore,$xml_file)) {
2934 } else if ($groupings === true) {
2940 //Iterate over each group
2941 foreach ($groupings as $grouping) {
2942 if ($data = backup_getid($restore->backup_unique_code
,"groupings",$grouping->id
)) {
2943 //Now get completed xmlized object
2944 $info = $data->info
;
2945 //Now build the GROUPING record structure
2946 $gro = new Object();
2947 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
2948 $gro->courseid
= $restore->course_id
;
2949 $gro->name
= backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
2950 $gro->description
= backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
2951 $gro->configdata
= backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
2952 $gro->timecreated
= backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
2954 //Now search if that group exists (by name and description field) in
2955 if ($gro_db = get_record('groupings', 'courseid', $restore->course_id
, 'name', $gro->name
, 'description', $gro->description
)) {
2956 //get current group id
2957 $newid = $gro_db->id
;
2960 //The structure is equal to the db, so insert the grouping
2961 if (!$newid = insert_record('groupings', $gro)) {
2967 //We have the newid, update backup_ids
2968 backup_putid($restore->backup_unique_code
,"groupings",
2969 $grouping->id
, $newid);
2974 // now fix the defaultgroupingid in course
2975 $course = get_record('course', 'id', $restore->course_id
);
2976 if ($course->defaultgroupingid
) {
2977 if ($grouping = backup_getid($restore->backup_unique_code
,"groupings",$course->defaultgroupingid
)) {
2978 set_field('course', 'defaultgroupingid', $grouping->new_id
, 'id', $course->id
);
2980 set_field('course', 'defaultgroupingid', 0, 'id', $course->id
);
2987 //This function creates all the groupingsgroups
2988 function restore_create_groupings_groups($restore,$xml_file) {
2991 if (!file_exists($xml_file)) {
2995 if (!$groupingsgroups = restore_read_xml_groupings_groups($restore,$xml_file)) {
2998 } else if ($groupingsgroups === true) {
3004 //Iterate over each group
3005 foreach ($groupingsgroups as $groupinggroup) {
3006 if ($data = backup_getid($restore->backup_unique_code
,"groupingsgroups",$groupinggroup->id
)) {
3007 //Now get completed xmlized object
3008 $info = $data->info
;
3009 //Now build the GROUPING record structure
3010 $gro_member = new Object();
3011 $gro_member->groupingid
= backup_todb($info['GROUPINGGROUP']['#']['GROUPINGID']['0']['#']);
3012 $gro_member->groupid
= backup_todb($info['GROUPINGGROUP']['#']['GROUPID']['0']['#']);
3013 $gro_member->timeadded
= backup_todb($info['GROUPINGGROUP']['#']['TIMEADDED']['0']['#']);
3015 if (!$grouping = backup_getid($restore->backup_unique_code
,"groupings",$gro_member->groupingid
)) {
3020 if (!$group = backup_getid($restore->backup_unique_code
,"groups",$gro_member->groupid
)) {
3025 $gro_member->groupid
= $group->new_id
;
3026 $gro_member->groupingid
= $grouping->new_id
;
3027 if (!get_record('groupings_groups', 'groupid', $gro_member->groupid
, 'groupingid', $gro_member->groupingid
)) {
3028 if (!insert_record('groupings_groups', $gro_member)) {
3038 //This function creates all the course events
3039 function restore_create_events($restore,$xml_file) {
3045 if (!file_exists($xml_file)) {
3050 //events will contain the old_id of every event
3051 //in backup_ids->info will be the real info (serialized)
3052 $events = restore_read_xml_events($restore,$xml_file);
3055 //Get admin->id for later use
3056 $admin = get_admin();
3057 $adminid = $admin->id
;
3059 //Now, if we have anything in events, we have to restore that
3062 if ($events !== true) {
3063 //Iterate over each event
3064 foreach ($events as $event) {
3065 //Get record from backup_ids
3066 $data = backup_getid($restore->backup_unique_code
,"event",$event->id
);
3068 $create_event = false;
3071 //Now get completed xmlized object
3072 $info = $data->info
;
3073 //traverse_xmlize($info); //Debug
3074 //print_object ($GLOBALS['traverse_array']); //Debug
3075 //$GLOBALS['traverse_array']=""; //Debug
3077 //if necessary, write to restorelog and adjust date/time fields
3078 if ($restore->course_startdateoffset
) {
3079 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
3082 //Now build the EVENT record structure
3083 $eve->name
= backup_todb($info['EVENT']['#']['NAME']['0']['#']);
3084 $eve->description
= backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
3085 $eve->format
= backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
3086 $eve->courseid
= $restore->course_id
;
3087 $eve->groupid
= backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
3088 $eve->userid
= backup_todb($info['EVENT']['#']['USERID']['0']['#']);
3089 $eve->repeatid
= backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
3090 $eve->modulename
= "";
3091 if (!empty($info['EVENT']['#']['MODULENAME'])) {
3092 $eve->modulename
= backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
3095 $eve->eventtype
= backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
3096 $eve->timestart
= backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
3097 $eve->timeduration
= backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
3098 $eve->visible
= backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
3099 $eve->timemodified
= backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
3101 //Now search if that event exists (by name, description, timestart fields) in
3102 //restore->course_id course
3103 $eve_db = get_record_select("event",
3104 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
3105 //If it doesn't exist, create
3107 $create_event = true;
3109 //If we must create the event
3110 if ($create_event) {
3112 //We must recode the userid
3113 $user = backup_getid($restore->backup_unique_code
,"user",$eve->userid
);
3115 $eve->userid
= $user->new_id
;
3117 //Assign it to admin
3118 $eve->userid
= $adminid;
3121 //We must recode the repeatid if the event has it
3122 if (!empty($eve->repeatid
)) {
3123 $repeat_rec = backup_getid($restore->backup_unique_code
,"event_repeatid",$eve->repeatid
);
3124 if ($repeat_rec) { //Exists, so use it...
3125 $eve->repeatid
= $repeat_rec->new_id
;
3126 } else { //Doesn't exists, calculate the next and save it
3127 $oldrepeatid = $eve->repeatid
;
3128 $max_rec = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix
.'event');
3129 $eve->repeatid
= empty($max_rec) ?
1 : $max_rec->repeatid +
1;
3130 backup_putid($restore->backup_unique_code
,"event_repeatid", $oldrepeatid, $eve->repeatid
);
3134 //We have to recode the groupid field
3135 $group = backup_getid($restore->backup_unique_code
,"groups",$eve->groupid
);
3137 $eve->groupid
= $group->new_id
;
3139 //Assign it to group 0
3143 //The structure is equal to the db, so insert the event
3144 $newid = insert_record ("event",$eve);
3146 //get current event id
3147 $newid = $eve_db->id
;
3150 //We have the newid, update backup_ids
3151 backup_putid($restore->backup_unique_code
,"event",
3152 $event->id
, $newid);
3163 //This function decode things to make restore multi-site fully functional
3164 //It does this conversions:
3165 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
3166 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
3168 //Note: Inter-activities linking is being implemented as a final
3169 //step in the restore execution, because we need to have it
3170 //finished to know all the oldid, newid equivaleces
3171 function restore_decode_absolute_links($content) {
3173 global $CFG,$restore;
3176 // This function was replacing null with empty string
3177 // Nullity check is added in backup_todb(), this function will no longer not be called from backup_todb() if content is null
3178 // I noticed some parts of the restore code is calling this directly instead of calling backup_todb(), so just in case
3179 // 3rd party mod etc are doing the same
3180 if ($content === NULL) {
3184 //Now decode wwwroot and file.php calls
3185 $search = array ("$@FILEPHP@$");
3187 //Check for the status of the slasharguments config variable
3188 $slash = $CFG->slasharguments
;
3190 //Build the replace string as needed
3192 $replace = array ($CFG->wwwroot
."/file.php/".$restore->course_id
);
3194 $replace = array ($CFG->wwwroot
."/file.php?file=/".$restore->course_id
);
3197 $result = str_replace($search,$replace,$content);
3199 if ($result != $content && debugging()) { //Debug
3200 if (!defined('RESTORE_SILENTLY')) {
3201 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
3208 //This function restores the userfiles from the temp (user_files) directory to the
3209 //dataroot/users directory
3210 function restore_user_files($restore) {
3218 //First, we check to "users" exists and create is as necessary
3220 $dest_dir = $CFG->dataroot
."/users";
3221 $status = check_dir_exists($dest_dir,true);
3223 //Now, we iterate over "user_files" records to check if that user dir must be
3224 //copied (and renamed) to the "users" dir.
3225 $rootdir = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
."/user_files";
3226 //Check if directory exists
3227 if (is_dir($rootdir)) {
3228 $list = list_directories ($rootdir);
3232 foreach ($list as $dir) {
3233 //Look for dir like username in backup_ids
3234 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code
,
3235 "table_name","user",
3237 //If thar user exists in backup_ids
3239 //Only it user has been created now
3240 //or if it existed previously, but he hasn't image (see bug 1123)
3241 if ((strpos($data->info
,"new") !== false) or
3242 (!check_dir_exists($dest_dir."/".$data->new_id
,false))) {
3243 //Copy the old_dir to its new location (and name) !!
3244 //Only if destination doesn't exists
3245 if (!file_exists($dest_dir."/".$data->new_id
)) {
3246 $status = backup_copy_file($rootdir."/".$dir,
3247 $dest_dir."/".$data->new_id
,true);
3251 if ($counter %
2 == 0) {
3252 if (!defined('RESTORE_SILENTLY')) {
3254 if ($counter %
40 == 0) {
3265 //If status is ok and whe have dirs created, returns counter to inform
3266 if ($status and $counter) {
3273 //This function restores the groupfiles from the temp (group_files) directory to the
3274 //dataroot/groups directory
3275 function restore_group_files($restore) {
3283 //First, we check to "groups" exists and create is as necessary
3285 $dest_dir = $CFG->dataroot
.'/groups';
3286 $status = check_dir_exists($dest_dir,true);
3288 //Now, we iterate over "group_files" records to check if that user dir must be
3289 //copied (and renamed) to the "groups" dir.
3290 $rootdir = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
."/group_files";
3291 //Check if directory exists
3292 if (is_dir($rootdir)) {
3293 $list = list_directories ($rootdir);
3297 foreach ($list as $dir) {
3298 //Look for dir like groupid in backup_ids
3299 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code
,
3300 "table_name","groups",
3302 //If that group exists in backup_ids
3304 if (!file_exists($dest_dir."/".$data->new_id
)) {
3305 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id
,true);
3309 if ($counter %
2 == 0) {
3310 if (!defined('RESTORE_SILENTLY')) {
3312 if ($counter %
40 == 0) {
3322 //If status is ok and whe have dirs created, returns counter to inform
3323 if ($status and $counter) {
3330 //This function restores the course files from the temp (course_files) directory to the
3331 //dataroot/course_id directory
3332 function restore_course_files($restore) {
3340 //First, we check to "course_id" exists and create is as necessary
3342 $dest_dir = $CFG->dataroot
."/".$restore->course_id
;
3343 $status = check_dir_exists($dest_dir,true);
3345 //Now, we iterate over "course_files" records to check if that file/dir must be
3346 //copied to the "dest_dir" dir.
3347 $rootdir = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
."/course_files";
3348 //Check if directory exists
3349 if (is_dir($rootdir)) {
3350 $list = list_directories_and_files ($rootdir);
3354 foreach ($list as $dir) {
3355 //Copy the dir to its new location
3356 //Only if destination file/dir doesn exists
3357 if (!file_exists($dest_dir."/".$dir)) {
3358 $status = backup_copy_file($rootdir."/".$dir,
3359 $dest_dir."/".$dir,true);
3363 if ($counter %
2 == 0) {
3364 if (!defined('RESTORE_SILENTLY')) {
3366 if ($counter %
40 == 0) {
3375 //If status is ok and whe have dirs created, returns counter to inform
3376 if ($status and $counter) {
3383 //This function restores the site files from the temp (site_files) directory to the
3384 //dataroot/SITEID directory
3385 function restore_site_files($restore) {
3393 //First, we check to "course_id" exists and create is as necessary
3395 $dest_dir = $CFG->dataroot
."/".SITEID
;
3396 $status = check_dir_exists($dest_dir,true);
3398 //Now, we iterate over "site_files" files to check if that file/dir must be
3399 //copied to the "dest_dir" dir.
3400 $rootdir = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
."/site_files";
3401 //Check if directory exists
3402 if (is_dir($rootdir)) {
3403 $list = list_directories_and_files ($rootdir);
3407 foreach ($list as $dir) {
3408 //Copy the dir to its new location
3409 //Only if destination file/dir doesn exists
3410 if (!file_exists($dest_dir."/".$dir)) {
3411 $status = backup_copy_file($rootdir."/".$dir,
3412 $dest_dir."/".$dir,true);
3416 if ($counter %
2 == 0) {
3417 if (!defined('RESTORE_SILENTLY')) {
3419 if ($counter %
40 == 0) {
3428 //If status is ok and whe have dirs created, returns counter to inform
3429 if ($status and $counter) {
3437 //This function creates all the structures for every module in backup file
3438 //Depending what has been selected.
3439 function restore_create_modules($restore,$xml_file) {
3444 if (!file_exists($xml_file)) {
3449 //info will contain the id and modtype of every module
3450 //in backup_ids->info will be the real info (serialized)
3451 $info = restore_read_xml_modules($restore,$xml_file);
3453 //Now, if we have anything in info, we have to restore that mods
3454 //from backup_ids (calling every mod restore function)
3456 if ($info !== true) {
3457 if (!defined('RESTORE_SILENTLY')) {
3460 //Iterate over each module
3461 foreach ($info as $mod) {
3462 if (empty($restore->mods
[$mod->modtype
]->granular
) // We don't care about per instance, i.e. restore all instances.
3463 ||
(array_key_exists($mod->id
,$restore->mods
[$mod->modtype
]->instances
)
3464 && !empty($restore->mods
[$mod->modtype
]->instances
[$mod->id
]->restore
))) {
3465 $modrestore = $mod->modtype
."_restore_mods";
3466 if (function_exists($modrestore)) { //Debug
3467 $status = $status and $modrestore($mod,$restore); //bit operator & not reliable here!
3469 //Something was wrong. Function should exist.
3474 if (!defined('RESTORE_SILENTLY')) {
3484 //This function creates all the structures for every log in backup file
3485 //Depending what has been selected.
3486 function restore_create_logs($restore,$xml_file) {
3490 //Number of records to get in every chunk
3491 $recordset_size = 4;
3492 //Counter, points to current record
3494 //To count all the recods to restore
3499 if (!file_exists($xml_file)) {
3504 //count_logs will contain the number of logs entries to process
3505 //in backup_ids->info will be the real info (serialized)
3506 $count_logs = restore_read_xml_logs($restore,$xml_file);
3509 //Now, if we have records in count_logs, we have to restore that logs
3510 //from backup_ids. This piece of code makes calls to:
3511 // - restore_log_course() if it's a course log
3512 // - restore_log_user() if it's a user log
3513 // - restore_log_module() if it's a module log.
3514 //And all is segmented in chunks to allow large recordsets to be restored !!
3515 if ($count_logs > 0) {
3516 while ($counter < $count_logs) {
3517 //Get a chunk of records
3518 //Take old_id twice to avoid adodb limitation
3519 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id,old_id",$counter,$recordset_size);
3523 foreach ($logs as $log) {
3524 //Get the full record from backup_ids
3525 $data = backup_getid($restore->backup_unique_code
,"log",$log->old_id
);
3527 //Now get completed xmlized object
3528 $info = $data->info
;
3529 //traverse_xmlize($info); //Debug
3530 //print_object ($GLOBALS['traverse_array']); //Debug
3531 //$GLOBALS['traverse_array']=""; //Debug
3532 //Now build the LOG record structure
3533 $dblog = new object();
3534 $dblog->time
= backup_todb($info['LOG']['#']['TIME']['0']['#']);
3535 $dblog->userid
= backup_todb($info['LOG']['#']['USERID']['0']['#']);
3536 $dblog->ip
= backup_todb($info['LOG']['#']['IP']['0']['#']);
3537 $dblog->course
= $restore->course_id
;
3538 $dblog->module
= backup_todb($info['LOG']['#']['MODULE']['0']['#']);
3539 $dblog->cmid
= backup_todb($info['LOG']['#']['CMID']['0']['#']);
3540 $dblog->action
= backup_todb($info['LOG']['#']['ACTION']['0']['#']);
3541 $dblog->url
= backup_todb($info['LOG']['#']['URL']['0']['#']);
3542 $dblog->info
= backup_todb($info['LOG']['#']['INFO']['0']['#']);
3543 //We have to recode the userid field
3544 $user = backup_getid($restore->backup_unique_code
,"user",$dblog->userid
);
3546 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
3547 $dblog->userid
= $user->new_id
;
3549 //We have to recode the cmid field (if module isn't "course" or "user")
3550 if ($dblog->module
!= "course" and $dblog->module
!= "user") {
3551 $cm = backup_getid($restore->backup_unique_code
,"course_modules",$dblog->cmid
);
3553 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
3554 $dblog->cmid
= $cm->new_id
;
3559 //print_object ($dblog); //Debug
3560 //Now, we redirect to the needed function to make all the work
3561 if ($dblog->module
== "course") {
3562 //It's a course log,
3563 $stat = restore_log_course($restore,$dblog);
3564 } elseif ($dblog->module
== "user") {
3566 $stat = restore_log_user($restore,$dblog);
3568 //It's a module log,
3569 $stat = restore_log_module($restore,$dblog);
3575 if ($counter %
10 == 0) {
3576 if (!defined('RESTORE_SILENTLY')) {
3578 if ($counter %
200 == 0) {
3586 //We never should arrive here
3587 $counter = $count_logs;
3596 //This function inserts a course log record, calculating the URL field as necessary
3597 function restore_log_course($restore,$log) {
3602 //echo "<hr />Before transformations<br />"; //Debug
3603 //print_object($log); //Debug
3604 //Depending of the action, we recode different things
3605 switch ($log->action
) {
3607 $log->url
= "view.php?id=".$log->course
;
3608 $log->info
= $log->course
;
3612 $log->url
= "view.php?id=".$log->course
;
3616 //recode the info field (it's the user id)
3617 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3619 $log->info
= $user->new_id
;
3620 //Now, extract the mode from the url field
3621 $mode = substr(strrchr($log->url
,"="),1);
3622 $log->url
= "user.php?id=".$log->course
."&user=".$log->info
."&mode=".$mode;
3627 //Extract the course_module from the url field
3628 $cmid = substr(strrchr($log->url
,"="),1);
3629 //recode the course_module to see it it has been restored
3630 $cm = backup_getid($restore->backup_unique_code
,"course_modules",$cmid);
3632 $cmid = $cm->new_id
;
3633 //Extract the module name and the module id from the info field
3634 $modname = strtok($log->info
," ");
3635 $modid = strtok(" ");
3636 //recode the module id to see if it has been restored
3637 $mod = backup_getid($restore->backup_unique_code
,$modname,$modid);
3639 $modid = $mod->new_id
;
3640 //Now I have everything so reconstruct url and info
3641 $log->info
= $modname." ".$modid;
3642 $log->url
= "../mod/".$modname."/view.php?id=".$cmid;
3648 //Extract the course_module from the url field
3649 $cmid = substr(strrchr($log->url
,"="),1);
3650 //recode the course_module to see it it has been restored
3651 $cm = backup_getid($restore->backup_unique_code
,"course_modules",$cmid);
3653 $cmid = $cm->new_id
;
3654 //Extract the module name and the module id from the info field
3655 $modname = strtok($log->info
," ");
3656 $modid = strtok(" ");
3657 //recode the module id to see if it has been restored
3658 $mod = backup_getid($restore->backup_unique_code
,$modname,$modid);
3660 $modid = $mod->new_id
;
3661 //Now I have everything so reconstruct url and info
3662 $log->info
= $modname." ".$modid;
3663 $log->url
= "../mod/".$modname."/view.php?id=".$cmid;
3669 $log->url
= "view.php?id=".$log->course
;
3673 $log->url
= "edit.php?id=".$log->course
;
3678 //recode the info field (it's the user id)
3679 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3681 $log->info
= $user->new_id
;
3682 $log->url
= "view.php?id=".$log->course
;
3687 //recode the info field (it's the user id)
3688 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3690 $log->info
= $user->new_id
;
3691 $log->url
= "view.php?id=".$log->course
;
3696 //Extract the course_section from the url field
3697 $secid = substr(strrchr($log->url
,"="),1);
3698 //recode the course_section to see if it has been restored
3699 $sec = backup_getid($restore->backup_unique_code
,"course_sections",$secid);
3701 $secid = $sec->new_id
;
3702 //Now I have everything so reconstruct url and info
3703 $log->url
= "editsection.php?id=".$secid;
3708 $log->url
= "view.php?id=".$log->course
;
3713 $log->url
= "recent.php?id=".$log->course
;
3718 $log->url
= "report/log/index.php?id=".$log->course
;
3719 $log->info
= $log->course
;
3723 $log->url
= "report/log/live.php?id=".$log->course
;
3724 $log->info
= $log->course
;
3727 case "report outline":
3728 $log->url
= "report/outline/index.php?id=".$log->course
;
3729 $log->info
= $log->course
;
3732 case "report participation":
3733 $log->url
= "report/participation/index.php?id=".$log->course
;
3734 $log->info
= $log->course
;
3737 case "report stats":
3738 $log->url
= "report/stats/index.php?id=".$log->course
;
3739 $log->info
= $log->course
;
3743 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug
3747 //echo "After transformations<br />"; //Debug
3748 //print_object($log); //Debug
3750 //Now if $toinsert is set, insert the record
3752 //echo "Inserting record<br />"; //Debug
3753 $status = insert_record("log",$log);
3758 //This function inserts a user log record, calculating the URL field as necessary
3759 function restore_log_user($restore,$log) {
3764 //echo "<hr />Before transformations<br />"; //Debug
3765 //print_object($log); //Debug
3766 //Depending of the action, we recode different things
3767 switch ($log->action
) {
3769 //recode the info field (it's the user id)
3770 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3772 $log->info
= $user->new_id
;
3773 $log->url
= "view.php?id=".$log->info
."&course=".$log->course
;
3777 case "change password":
3778 //recode the info field (it's the user id)
3779 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3781 $log->info
= $user->new_id
;
3782 $log->url
= "view.php?id=".$log->info
."&course=".$log->course
;
3787 //recode the info field (it's the user id)
3788 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3790 $log->info
= $user->new_id
;
3791 $log->url
= "view.php?id=".$log->info
."&course=".$log->course
;
3796 //recode the info field (it's the user id)
3797 $user = backup_getid($restore->backup_unique_code
,"user",$log->info
);
3799 $log->info
= $user->new_id
;
3800 $log->url
= "view.php?id=".$log->info
."&course=".$log->course
;
3805 $log->url
= "view.php?id=".$log->course
;
3809 //We split the url by ampersand char
3810 $first_part = strtok($log->url
,"&");
3811 //Get data after the = char. It's the user being updated
3812 $userid = substr(strrchr($first_part,"="),1);
3814 $user = backup_getid($restore->backup_unique_code
,"user",$userid);
3817 $log->url
= "view.php?id=".$user->new_id
."&course=".$log->course
;
3822 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug
3826 //echo "After transformations<br />"; //Debug
3827 //print_object($log); //Debug
3829 //Now if $toinsert is set, insert the record
3831 //echo "Inserting record<br />"; //Debug
3832 $status = insert_record("log",$log);
3837 //This function inserts a module log record, calculating the URL field as necessary
3838 function restore_log_module($restore,$log) {
3843 //echo "<hr />Before transformations<br />"; //Debug
3844 //print_object($log); //Debug
3846 //Now we see if the required function in the module exists
3847 $function = $log->module
."_restore_logs";
3848 if (function_exists($function)) {
3850 $log = $function($restore,$log);
3851 //If everything is ok, mark the insert flag
3857 //echo "After transformations<br />"; //Debug
3858 //print_object($log); //Debug
3860 //Now if $toinsert is set, insert the record
3862 //echo "Inserting record<br />"; //Debug
3863 $status = insert_record("log",$log);
3868 //This function adjusts the instance field into course_modules. It's executed after
3869 //modules restore. There, we KNOW the new instance id !!
3870 function restore_check_instances($restore) {
3876 //We are going to iterate over each course_module saved in backup_ids
3877 $course_modules = get_records_sql("SELECT old_id,new_id
3878 FROM {$CFG->prefix}backup_ids
3879 WHERE backup_code = '$restore->backup_unique_code' AND
3880 table_name = 'course_modules'");
3881 if ($course_modules) {
3882 foreach($course_modules as $cm) {
3883 //Get full record, using backup_getids
3884 $cm_module = backup_getid($restore->backup_unique_code
,"course_modules",$cm->old_id
);
3885 //Now we are going to the REAL course_modules to get its type (field module)
3886 $module = get_record("course_modules","id",$cm_module->new_id
);
3888 //We know the module type id. Get the name from modules
3889 $type = get_record("modules","id",$module->module
);
3891 //We know the type name and the old_id. Get its new_id
3892 //from backup_ids. It's the instance !!!
3893 $instance = backup_getid($restore->backup_unique_code
,$type->name
,$cm_module->info
);
3895 //We have the new instance, so update the record in course_modules
3896 $module->instance
= $instance->new_id
;
3897 //print_object ($module); //Debug
3898 $status = update_record("course_modules",$module);
3915 //=====================================================================================
3917 //== XML Functions (SAX) ==
3919 //=====================================================================================
3921 //This is the class used to do all the xml parse
3922 class MoodleParser
{
3924 var $level = 0; //Level we are
3925 var $counter = 0; //Counter
3926 var $tree = array(); //Array of levels we are
3927 var $content = ""; //Content under current level
3928 var $todo = ""; //What we hav to do when parsing
3929 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
3930 var $temp = ""; //Temp storage.
3931 var $preferences = ""; //Preferences about what to load !!
3932 var $finished = false; //Flag to say xml_parse to stop
3934 //This function is used to get the current contents property value
3935 //They are trimed (and converted from utf8 if needed)
3936 function getContents() {
3937 return trim($this->content
);
3940 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3941 function startElementInfo($parser, $tagName, $attrs) {
3942 //Refresh properties
3944 $this->tree
[$this->level
] = $tagName;
3946 //Output something to avoid browser timeouts...
3949 //Check if we are into INFO zone
3950 //if ($this->tree[2] == "INFO") //Debug
3951 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
3954 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3955 function startElementRoles($parser, $tagName, $attrs) {
3956 //Refresh properties
3958 $this->tree
[$this->level
] = $tagName;
3960 //Output something to avoid browser timeouts...
3963 //Check if we are into INFO zone
3964 //if ($this->tree[2] == "INFO") //Debug
3965 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
3969 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
3970 function startElementCourseHeader($parser, $tagName, $attrs) {
3971 //Refresh properties
3973 $this->tree
[$this->level
] = $tagName;
3975 //Output something to avoid browser timeouts...
3978 //Check if we are into COURSE_HEADER zone
3979 //if ($this->tree[3] == "HEADER") //Debug
3980 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
3983 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
3984 function startElementBlocks($parser, $tagName, $attrs) {
3985 //Refresh properties
3987 $this->tree
[$this->level
] = $tagName;
3989 //Output something to avoid browser timeouts...
3992 //Check if we are into BLOCKS zone
3993 //if ($this->tree[3] == "BLOCKS") //Debug
3994 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
3997 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
3998 function startElementSections($parser, $tagName, $attrs) {
3999 //Refresh properties
4001 $this->tree
[$this->level
] = $tagName;
4003 //Output something to avoid browser timeouts...
4006 //Check if we are into SECTIONS zone
4007 //if ($this->tree[3] == "SECTIONS") //Debug
4008 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4011 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4012 function startElementFormatData($parser, $tagName, $attrs) {
4013 //Refresh properties
4015 $this->tree
[$this->level
] = $tagName;
4017 //Output something to avoid browser timeouts...
4020 //Accumulate all the data inside this tag
4021 if (isset($this->tree
[3]) && $this->tree
[3] == "FORMATDATA") {
4022 if (!isset($this->temp
)) {
4025 $this->temp
.= "<".$tagName.">";
4028 //Check if we are into FORMATDATA zone
4029 //if ($this->tree[3] == "FORMATDATA") //Debug
4030 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4033 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4034 function startElementMetacourse($parser, $tagName, $attrs) {
4036 //Refresh properties
4038 $this->tree
[$this->level
] = $tagName;
4040 //Output something to avoid browser timeouts...
4043 //Check if we are into METACOURSE zone
4044 //if ($this->tree[3] == "METACOURSE") //Debug
4045 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4048 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4049 function startElementGradebook($parser, $tagName, $attrs) {
4051 //Refresh properties
4053 $this->tree
[$this->level
] = $tagName;
4055 //Output something to avoid browser timeouts...
4058 //Check if we are into GRADEBOOK zone
4059 //if ($this->tree[3] == "GRADEBOOK") //Debug
4060 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4062 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4063 if (isset($this->tree
[5]) and isset($this->tree
[3])) {
4064 if (($this->tree
[5] == "GRADE_ITEM" ||
$this->tree
[5] == "GRADE_CATEGORY" ||
$this->tree
[5] == "GRADE_OUTCOME" ||
$this->tree
[5] == "GRADE_OUTCOMES_COURSE" ||
$this->tree
[5] == "GRADE_CATEGORIES_HISTORY" ||
$this->tree
[5] == "GRADE_GRADES_HISTORY" ||
$this->tree
[5] == "GRADE_TEXT_HISTORY" ||
$this->tree
[5] == "GRADE_ITEM_HISTORY" ||
$this->tree
[5] == "GRADE_OUTCOME_HISTORY") && ($this->tree
[3] == "GRADEBOOK")) {
4066 if (!isset($this->temp
)) {
4069 $this->temp
.= "<".$tagName.">";
4075 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
4076 function startElementUsers($parser, $tagName, $attrs) {
4077 //Refresh properties
4079 $this->tree
[$this->level
] = $tagName;
4081 //Check if we are into USERS zone
4082 //if ($this->tree[3] == "USERS") //Debug
4083 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4086 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
4087 function startElementMessages($parser, $tagName, $attrs) {
4088 //Refresh properties
4090 $this->tree
[$this->level
] = $tagName;
4092 //Output something to avoid browser timeouts...
4095 //Check if we are into MESSAGES zone
4096 //if ($this->tree[3] == "MESSAGES") //Debug
4097 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4099 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
4100 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4101 if (($this->tree
[4] == "MESSAGE" ||
$this->tree
[5] == "CONTACT" ) and ($this->tree
[3] == "MESSAGES")) {
4102 if (!isset($this->temp
)) {
4105 $this->temp
.= "<".$tagName.">";
4109 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
4110 function startElementQuestions($parser, $tagName, $attrs) {
4111 //Refresh properties
4113 $this->tree
[$this->level
] = $tagName;
4115 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
4116 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
4119 //Output something to avoid browser timeouts...
4122 //Check if we are into QUESTION_CATEGORIES zone
4123 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
4124 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4126 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
4127 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4128 if (($this->tree
[4] == "QUESTION_CATEGORY") and ($this->tree
[3] == "QUESTION_CATEGORIES")) {
4129 if (!isset($this->temp
)) {
4132 $this->temp
.= "<".$tagName.">";
4137 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
4138 function startElementScales($parser, $tagName, $attrs) {
4139 //Refresh properties
4141 $this->tree
[$this->level
] = $tagName;
4143 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
4144 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
4147 //Output something to avoid browser timeouts...
4150 //Check if we are into SCALES zone
4151 //if ($this->tree[3] == "SCALES") //Debug
4152 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4154 //If we are under a SCALE tag under a SCALES zone, accumule it
4155 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4156 if (($this->tree
[4] == "SCALE") and ($this->tree
[3] == "SCALES")) {
4157 if (!isset($this->temp
)) {
4160 $this->temp
.= "<".$tagName.">";
4165 function startElementGroups($parser, $tagName, $attrs) {
4166 //Refresh properties
4168 $this->tree
[$this->level
] = $tagName;
4170 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
4171 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
4174 //Output something to avoid browser timeouts...
4177 //Check if we are into GROUPS zone
4178 //if ($this->tree[3] == "GROUPS") //Debug
4179 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4181 //If we are under a GROUP tag under a GROUPS zone, accumule it
4182 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4183 if (($this->tree
[4] == "GROUP") and ($this->tree
[3] == "GROUPS")) {
4184 if (!isset($this->temp
)) {
4187 $this->temp
.= "<".$tagName.">";
4192 function startElementGroupings($parser, $tagName, $attrs) {
4193 //Refresh properties
4195 $this->tree
[$this->level
] = $tagName;
4197 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
4198 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
4201 //Output something to avoid browser timeouts...
4204 //Check if we are into GROUPINGS zone
4205 //if ($this->tree[3] == "GROUPINGS") //Debug
4206 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4208 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
4209 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4210 if (($this->tree
[4] == "GROUPING") and ($this->tree
[3] == "GROUPINGS")) {
4211 if (!isset($this->temp
)) {
4214 $this->temp
.= "<".$tagName.">";
4219 function startElementGroupingsGroups($parser, $tagName, $attrs) {
4220 //Refresh properties
4222 $this->tree
[$this->level
] = $tagName;
4224 //if ($tagName == "GROUPINGGROUP" && $this->tree[3] == "GROUPINGSGROUPS") { //Debug
4225 // echo "<P>GROUPINGSGROUP: ".strftime ("%X",time()),"-"; //Debug
4228 //Output something to avoid browser timeouts...
4231 //Check if we are into GROUPINGSGROUPS zone
4232 //if ($this->tree[3] == "GROUPINGSGROUPS") //Debug
4233 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4235 //If we are under a GROUPINGGROUP tag under a GROUPINGSGROUPS zone, accumule it
4236 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4237 if (($this->tree
[4] == "GROUPINGGROUP") and ($this->tree
[3] == "GROUPINGSGROUPS")) {
4238 if (!isset($this->temp
)) {
4241 $this->temp
.= "<".$tagName.">";
4246 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
4247 function startElementEvents($parser, $tagName, $attrs) {
4248 //Refresh properties
4250 $this->tree
[$this->level
] = $tagName;
4252 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
4253 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
4256 //Output something to avoid browser timeouts...
4259 //Check if we are into EVENTS zone
4260 //if ($this->tree[3] == "EVENTS") //Debug
4261 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4263 //If we are under a EVENT tag under a EVENTS zone, accumule it
4264 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4265 if (($this->tree
[4] == "EVENT") and ($this->tree
[3] == "EVENTS")) {
4266 if (!isset($this->temp
)) {
4269 $this->temp
.= "<".$tagName.">";
4274 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
4275 function startElementModules($parser, $tagName, $attrs) {
4276 //Refresh properties
4278 $this->tree
[$this->level
] = $tagName;
4280 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
4281 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
4284 //Output something to avoid browser timeouts...
4287 //Check if we are into MODULES zone
4288 //if ($this->tree[3] == "MODULES") //Debug
4289 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4291 //If we are under a MOD tag under a MODULES zone, accumule it
4292 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4293 if (($this->tree
[4] == "MOD") and ($this->tree
[3] == "MODULES")) {
4294 if (!isset($this->temp
)) {
4297 $this->temp
.= "<".$tagName.">";
4302 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
4303 function startElementLogs($parser, $tagName, $attrs) {
4304 //Refresh properties
4306 $this->tree
[$this->level
] = $tagName;
4308 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
4309 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
4312 //Output something to avoid browser timeouts...
4315 //Check if we are into LOGS zone
4316 //if ($this->tree[3] == "LOGS") //Debug
4317 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br />\n"; //Debug
4319 //If we are under a LOG tag under a LOGS zone, accumule it
4320 if (isset($this->tree
[4]) and isset($this->tree
[3])) {
4321 if (($this->tree
[4] == "LOG") and ($this->tree
[3] == "LOGS")) {
4322 if (!isset($this->temp
)) {
4325 $this->temp
.= "<".$tagName.">";
4330 //This is the startTag default handler we use when todo is undefined
4331 function startElement($parser, $tagName, $attrs) {
4333 $this->tree
[$this->level
] = $tagName;
4335 //Output something to avoid browser timeouts...
4338 echo $this->level
.str_repeat(" ",$this->level
*2)."<".$tagName."><br />\n"; //Debug
4341 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
4342 function endElementInfo($parser, $tagName) {
4343 //Check if we are into INFO zone
4344 if ($this->tree
[2] == "INFO") {
4345 //if (trim($this->content)) //Debug
4346 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4347 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
4348 //Dependig of different combinations, do different things
4349 if ($this->level
== 3) {
4352 $this->info
->backup_name
= $this->getContents();
4354 case "MOODLE_VERSION":
4355 $this->info
->backup_moodle_version
= $this->getContents();
4357 case "MOODLE_RELEASE":
4358 $this->info
->backup_moodle_release
= $this->getContents();
4360 case "BACKUP_VERSION":
4361 $this->info
->backup_backup_version
= $this->getContents();
4363 case "BACKUP_RELEASE":
4364 $this->info
->backup_backup_release
= $this->getContents();
4367 $this->info
->backup_date
= $this->getContents();
4369 case "ORIGINAL_WWWROOT":
4370 $this->info
->original_wwwroot
= $this->getContents();
4372 case "MNET_EXTERNALUSERS":
4373 $this->info
->mnet_externalusers
= $this->getContents();
4377 if ($this->tree
[3] == "DETAILS") {
4378 if ($this->level
== 4) {
4381 $this->info
->backup_metacourse
= $this->getContents();
4384 $this->info
->backup_users
= $this->getContents();
4387 $this->info
->backup_logs
= $this->getContents();
4390 $this->info
->backup_user_files
= $this->getContents();
4393 $this->info
->backup_course_files
= $this->getContents();
4396 $this->info
->backup_site_files
= $this->getContents();
4399 $this->info
->backup_messages
= $this->getContents();
4402 $this->info
->backup_block_format
= $this->getContents();
4406 if ($this->level
== 5) {
4409 $this->info
->tempName
= $this->getContents();
4412 $this->info
->mods
[$this->info
->tempName
]->backup
= $this->getContents();
4415 $this->info
->mods
[$this->info
->tempName
]->userinfo
= $this->getContents();
4419 if ($this->level
== 7) {
4422 $this->info
->tempId
= $this->getContents();
4423 $this->info
->mods
[$this->info
->tempName
]->instances
[$this->info
->tempId
]->id
= $this->info
->tempId
;
4426 $this->info
->mods
[$this->info
->tempName
]->instances
[$this->info
->tempId
]->name
= $this->getContents();
4429 $this->info
->mods
[$this->info
->tempName
]->instances
[$this->info
->tempId
]->backup
= $this->getContents();
4432 $this->info
->mods
[$this->info
->tempName
]->instances
[$this->info
->tempId
]->userinfo
= $this->getContents();
4439 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4440 //Speed up a lot (avoid parse all)
4441 if ($tagName == "INFO") {
4442 $this->finished
= true;
4446 $this->tree
[$this->level
] = "";
4448 $this->content
= "";
4452 function endElementRoles($parser, $tagName) {
4453 //Check if we are into INFO zone
4454 if ($this->tree
[2] == "ROLES") {
4456 if ($this->tree
[3] == "ROLE") {
4457 if ($this->level
== 4) {
4460 $this->info
->tempname
= $this->getContents();
4464 $this->info
->tempshortname
= $this->getContents();
4466 case "ID": // this is the old id
4467 $this->info
->tempid
= $this->getContents();
4471 if ($this->level
== 6) {
4474 $this->info
->roles
[$this->info
->tempid
]->name
= $this->info
->tempname
;
4475 $this->info
->roles
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
4477 $this->info
->tempcapname
= $this->getContents();
4478 $this->info
->roles
[$this->info
->tempid
]->capabilities
[$this->info
->tempcapname
]->name
= $this->getContents();
4481 $this->info
->roles
[$this->info
->tempid
]->capabilities
[$this->info
->tempcapname
]->permission
= $this->getContents();
4483 case "TIMEMODIFIED":
4484 $this->info
->roles
[$this->info
->tempid
]->capabilities
[$this->info
->tempcapname
]->timemodified
= $this->getContents();
4487 $this->info
->roles
[$this->info
->tempid
]->capabilities
[$this->info
->tempcapname
]->modifierid
= $this->getContents();
4494 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4495 //Speed up a lot (avoid parse all)
4496 if ($tagName == "ROLES") {
4497 $this->finished
= true;
4501 $this->tree
[$this->level
] = "";
4503 $this->content
= "";
4507 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
4508 function endElementCourseHeader($parser, $tagName) {
4509 //Check if we are into COURSE_HEADER zone
4510 if ($this->tree
[3] == "HEADER") {
4511 //if (trim($this->content)) //Debug
4512 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4513 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
4514 //Dependig of different combinations, do different things
4515 if ($this->level
== 4) {
4518 $this->info
->course_id
= $this->getContents();
4521 $this->info
->course_password
= $this->getContents();
4524 $this->info
->course_fullname
= $this->getContents();
4527 $this->info
->course_shortname
= $this->getContents();
4530 $this->info
->course_idnumber
= $this->getContents();
4533 $this->info
->course_summary
= $this->getContents();
4536 $this->info
->course_format
= $this->getContents();
4539 $this->info
->course_showgrades
= $this->getContents();
4542 $this->info
->blockinfo
= $this->getContents();
4545 $this->info
->course_newsitems
= $this->getContents();
4548 $this->info
->course_teacher
= $this->getContents();
4551 $this->info
->course_teachers
= $this->getContents();
4554 $this->info
->course_student
= $this->getContents();
4557 $this->info
->course_students
= $this->getContents();
4560 $this->info
->course_guest
= $this->getContents();
4563 $this->info
->course_startdate
= $this->getContents();
4566 $this->info
->course_numsections
= $this->getContents();
4568 //case "SHOWRECENT": INFO: This is out in 1.3
4569 // $this->info->course_showrecent = $this->getContents();
4572 $this->info
->course_maxbytes
= $this->getContents();
4575 $this->info
->course_showreports
= $this->getContents();
4578 $this->info
->course_groupmode
= $this->getContents();
4580 case "GROUPMODEFORCE":
4581 $this->info
->course_groupmodeforce
= $this->getContents();
4583 case "DEFAULTGROUPINGID":
4584 $this->info
->course_defaultgroupingid
= $this->getContents();
4587 $this->info
->course_lang
= $this->getContents();
4590 $this->info
->course_theme
= $this->getContents();
4593 $this->info
->course_cost
= $this->getContents();
4596 $this->info
->course_currency
= $this->getContents();
4599 $this->info
->course_marker
= $this->getContents();
4602 $this->info
->course_visible
= $this->getContents();
4604 case "HIDDENSECTIONS":
4605 $this->info
->course_hiddensections
= $this->getContents();
4608 $this->info
->course_timecreated
= $this->getContents();
4610 case "TIMEMODIFIED":
4611 $this->info
->course_timemodified
= $this->getContents();
4614 $this->info
->course_metacourse
= $this->getContents();
4616 case "EXPIRENOTIFY":
4617 $this->info
->course_expirynotify
= $this->getContents();
4619 case "NOTIFYSTUDENTS":
4620 $this->info
->course_notifystudents
= $this->getContents();
4622 case "EXPIRYTHRESHOLD":
4623 $this->info
->course_expirythreshold
= $this->getContents();
4626 $this->info
->course_enrollable
= $this->getContents();
4628 case "ENROLSTARTDATE":
4629 $this->info
->course_enrolstartdate
= $this->getContents();
4631 case "ENROLENDDATE":
4632 $this->info
->course_enrolenddate
= $this->getContents();
4635 $this->info
->course_enrolperiod
= $this->getContents();
4639 if ($this->tree
[4] == "CATEGORY") {
4640 if ($this->level
== 5) {
4643 $this->info
->category
->id
= $this->getContents();
4646 $this->info
->category
->name
= $this->getContents();
4652 if ($this->tree
[4] == "ROLES_ASSIGNMENTS") {
4653 if ($this->level
== 6) {
4656 $this->info
->tempname
= $this->getContents();
4659 $this->info
->tempshortname
= $this->getContents();
4662 $this->info
->tempid
= $this->getContents();
4667 if ($this->level
== 8) {
4670 $this->info
->roleassignments
[$this->info
->tempid
]->name
= $this->info
->tempname
;
4671 $this->info
->roleassignments
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
4672 $this->info
->tempuser
= $this->getContents();
4673 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->userid
= $this->getContents();
4676 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->hidden
= $this->getContents();
4679 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timestart
= $this->getContents();
4682 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timeend
= $this->getContents();
4684 case "TIMEMODIFIED":
4685 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timemodified
= $this->getContents();
4688 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->modifierid
= $this->getContents();
4691 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->enrol
= $this->getContents();
4694 $this->info
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->sortorder
= $this->getContents();
4699 } /// ends role_assignments
4701 if ($this->tree
[4] == "ROLES_OVERRIDES") {
4702 if ($this->level
== 6) {
4705 $this->info
->tempname
= $this->getContents();
4708 $this->info
->tempshortname
= $this->getContents();
4711 $this->info
->tempid
= $this->getContents();
4716 if ($this->level
== 8) {
4719 $this->info
->roleoverrides
[$this->info
->tempid
]->name
= $this->info
->tempname
;
4720 $this->info
->roleoverrides
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
4721 $this->info
->tempname
= $this->getContents(); // change to name of capability
4722 $this->info
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->name
= $this->getContents();
4725 $this->info
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->permission
= $this->getContents();
4727 case "TIMEMODIFIED":
4728 $this->info
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->timemodified
= $this->getContents();
4731 $this->info
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->modifierid
= $this->getContents();
4735 } /// ends role_overrides
4738 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
4739 //Speed up a lot (avoid parse all)
4740 if ($tagName == "HEADER") {
4741 $this->finished
= true;
4745 $this->tree
[$this->level
] = "";
4747 $this->content
= "";
4751 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
4752 function endElementBlocks($parser, $tagName) {
4753 //Check if we are into BLOCKS zone
4754 if ($this->tree
[3] == 'BLOCKS') {
4755 //if (trim($this->content)) //Debug
4756 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4757 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
4758 //Dependig of different combinations, do different things
4759 if ($this->level
== 4) {
4762 //We've finalized a block, get it
4763 $this->info
->instances
[] = $this->info
->tempinstance
;
4764 unset($this->info
->tempinstance
);
4770 if ($this->level
== 5) {
4773 $this->info
->tempinstance
->id
= $this->getContents();
4775 $this->info
->tempinstance
->name
= $this->getContents();
4778 $this->info
->tempinstance
->pageid
= $this->getContents();
4781 $this->info
->tempinstance
->pagetype
= $this->getContents();
4784 $this->info
->tempinstance
->position
= $this->getContents();
4787 $this->info
->tempinstance
->weight
= $this->getContents();
4790 $this->info
->tempinstance
->visible
= $this->getContents();
4793 $this->info
->tempinstance
->configdata
= $this->getContents();
4800 if ($this->tree
[5] == "ROLES_ASSIGNMENTS") {
4801 if ($this->level
== 7) {
4804 $this->info
->tempname
= $this->getContents();
4807 $this->info
->tempshortname
= $this->getContents();
4810 $this->info
->tempid
= $this->getContents(); // temp roleid
4815 if ($this->level
== 9) {
4819 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->name
= $this->info
->tempname
;
4821 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
4823 $this->info
->tempuser
= $this->getContents();
4825 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->userid
= $this->getContents();
4828 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->hidden
= $this->getContents();
4831 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timestart
= $this->getContents();
4834 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timeend
= $this->getContents();
4836 case "TIMEMODIFIED":
4837 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timemodified
= $this->getContents();
4840 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->modifierid
= $this->getContents();
4843 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->enrol
= $this->getContents();
4846 $this->info
->tempinstance
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->sortorder
= $this->getContents();
4851 } /// ends role_assignments
4853 if ($this->tree
[5] == "ROLES_OVERRIDES") {
4854 if ($this->level
== 7) {
4857 $this->info
->tempname
= $this->getContents();
4860 $this->info
->tempshortname
= $this->getContents();
4863 $this->info
->tempid
= $this->getContents(); // temp roleid
4868 if ($this->level
== 9) {
4872 $this->info
->tempinstance
->roleoverrides
[$this->info
->tempid
]->name
= $this->info
->tempname
;
4873 $this->info
->tempinstance
->roleoverrides
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
4874 $this->info
->tempname
= $this->getContents(); // change to name of capability
4875 $this->info
->tempinstance
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->name
= $this->getContents();
4878 $this->info
->tempinstance
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->permission
= $this->getContents();
4880 case "TIMEMODIFIED":
4881 $this->info
->tempinstance
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->timemodified
= $this->getContents();
4884 $this->info
->tempinstance
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->modifierid
= $this->getContents();
4888 } /// ends role_overrides
4891 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
4892 //Speed up a lot (avoid parse all)
4893 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
4894 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
4895 if ($this->tree
[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
4896 $this->finished
= true;
4900 $this->tree
[$this->level
] = '';
4902 $this->content
= "";
4905 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
4906 function endElementSections($parser, $tagName) {
4907 //Check if we are into SECTIONS zone
4908 if ($this->tree
[3] == "SECTIONS") {
4909 //if (trim($this->content)) //Debug
4910 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4911 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
4912 //Dependig of different combinations, do different things
4913 if ($this->level
== 4) {
4916 //We've finalized a section, get it
4917 $this->info
->sections
[$this->info
->tempsection
->id
] = $this->info
->tempsection
;
4918 unset($this->info
->tempsection
);
4921 if ($this->level
== 5) {
4924 $this->info
->tempsection
->id
= $this->getContents();
4927 $this->info
->tempsection
->number
= $this->getContents();
4930 $this->info
->tempsection
->summary
= $this->getContents();
4933 $this->info
->tempsection
->visible
= $this->getContents();
4937 if ($this->level
== 6) {
4940 if (!isset($this->info
->tempmod
->groupmode
)) {
4941 $this->info
->tempmod
->groupmode
= 0;
4943 if (!isset($this->info
->tempmod
->groupingid
)) {
4944 $this->info
->tempmod
->groupingid
= 0;
4946 if (!isset($this->info
->tempmod
->groupmembersonly
)) {
4947 $this->info
->tempmod
->groupmembersonly
= 0;
4950 //We've finalized a mod, get it
4951 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->type
=
4952 $this->info
->tempmod
->type
;
4953 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->instance
=
4954 $this->info
->tempmod
->instance
;
4955 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->added
=
4956 $this->info
->tempmod
->added
;
4957 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->score
=
4958 $this->info
->tempmod
->score
;
4959 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->indent
=
4960 $this->info
->tempmod
->indent
;
4961 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->visible
=
4962 $this->info
->tempmod
->visible
;
4963 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->groupmode
=
4964 $this->info
->tempmod
->groupmode
;
4965 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->groupingid
=
4966 $this->info
->tempmod
->groupingid
;
4967 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->groupmembersonly
=
4968 $this->info
->tempmod
->groupmembersonly
;
4970 unset($this->info
->tempmod
);
4973 if ($this->level
== 7) {
4976 $this->info
->tempmod
->id
= $this->getContents();
4979 $this->info
->tempmod
->type
= $this->getContents();
4982 $this->info
->tempmod
->instance
= $this->getContents();
4985 $this->info
->tempmod
->added
= $this->getContents();
4988 $this->info
->tempmod
->score
= $this->getContents();
4991 $this->info
->tempmod
->indent
= $this->getContents();
4994 $this->info
->tempmod
->visible
= $this->getContents();
4997 $this->info
->tempmod
->groupmode
= $this->getContents();
5000 $this->info
->tempmod
->groupingid
= $this->getContents();
5002 case "GROUPMEMBERSONLY":
5003 $this->info
->tempmod
->groupmembersonly
= $this->getContents();
5010 if (isset($this->tree
[7]) && $this->tree
[7] == "ROLES_ASSIGNMENTS") {
5012 if ($this->level
== 9) {
5015 $this->info
->tempname
= $this->getContents();
5018 $this->info
->tempshortname
= $this->getContents();
5021 $this->info
->tempid
= $this->getContents(); // temp roleid
5026 if ($this->level
== 11) {
5029 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->name
= $this->info
->tempname
;
5031 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
5033 $this->info
->tempuser
= $this->getContents();
5035 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->userid
= $this->getContents();
5038 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->hidden
= $this->getContents();
5041 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timestart
= $this->getContents();
5044 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timeend
= $this->getContents();
5046 case "TIMEMODIFIED":
5047 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->timemodified
= $this->getContents();
5050 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->modifierid
= $this->getContents();
5053 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->enrol
= $this->getContents();
5056 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuser
]->sortorder
= $this->getContents();
5061 } /// ends role_assignments
5063 if (isset($this->tree
[7]) && $this->tree
[7] == "ROLES_OVERRIDES") {
5064 if ($this->level
== 9) {
5067 $this->info
->tempname
= $this->getContents();
5070 $this->info
->tempshortname
= $this->getContents();
5073 $this->info
->tempid
= $this->getContents(); // temp roleid
5078 if ($this->level
== 11) {
5082 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleoverrides
[$this->info
->tempid
]->name
= $this->info
->tempname
;
5083 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleoverrides
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
5084 $this->info
->tempname
= $this->getContents(); // change to name of capability
5085 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->name
= $this->getContents();
5088 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->permission
= $this->getContents();
5090 case "TIMEMODIFIED":
5091 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->timemodified
= $this->getContents();
5094 $this->info
->tempsection
->mods
[$this->info
->tempmod
->id
]->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->modifierid
= $this->getContents();
5098 } /// ends role_overrides
5102 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
5103 //Speed up a lot (avoid parse all)
5104 if ($tagName == "SECTIONS") {
5105 $this->finished
= true;
5109 $this->tree
[$this->level
] = "";
5111 $this->content
= "";
5115 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
5116 function endElementFormatData($parser, $tagName) {
5117 //Check if we are into FORMATDATA zone
5118 if ($this->tree
[3] == 'FORMATDATA') {
5119 if (!isset($this->temp
)) {
5122 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
5125 if($tagName=='FORMATDATA') {
5126 //Did we have any data? If not don't bother
5127 if($this->temp
!='<FORMATDATA></FORMATDATA>') {
5128 //Prepend XML standard header to info gathered
5129 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5132 //Call to xmlize for this portion of xml data (the FORMATDATA block)
5133 $this->info
->format_data
= xmlize($xml_data,0);
5135 //Stop parsing at end of FORMATDATA
5136 $this->finished
=true;
5140 $this->tree
[$this->level
] = "";
5142 $this->content
= "";
5145 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
5146 function endElementMetacourse($parser, $tagName) {
5147 //Check if we are into METACOURSE zone
5148 if ($this->tree
[3] == 'METACOURSE') {
5149 //if (trim($this->content)) //Debug
5150 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5151 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
5152 //Dependig of different combinations, do different things
5153 if ($this->level
== 5) {
5156 //We've finalized a child, get it
5157 $this->info
->childs
[] = $this->info
->tempmeta
;
5158 unset($this->info
->tempmeta
);
5161 //We've finalized a parent, get it
5162 $this->info
->parents
[] = $this->info
->tempmeta
;
5163 unset($this->info
->tempmeta
);
5169 if ($this->level
== 6) {
5172 $this->info
->tempmeta
->id
= $this->getContents();
5175 $this->info
->tempmeta
->idnumber
= $this->getContents();
5178 $this->info
->tempmeta
->shortname
= $this->getContents();
5184 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
5185 //Speed up a lot (avoid parse all)
5186 if ($this->tree
[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
5187 $this->finished
= true;
5191 $this->tree
[$this->level
] = '';
5193 $this->content
= "";
5196 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5197 function endElementGradebook($parser, $tagName) {
5198 //Check if we are into GRADEBOOK zone
5199 if ($this->tree
[3] == "GRADEBOOK") {
5200 //if (trim($this->content)) //Debug
5201 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5202 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n";//Debug
5203 //Acumulate data to info (content + close tag)
5204 //Reconvert: strip htmlchars again and trim to generate xml data
5205 if (!isset($this->temp
)) {
5208 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
5209 // We have finished outcome, grade_category or grade_item, reset accumulated
5210 // data because they are close tags
5211 if ($this->level
== 4) {
5214 //If we've finished a grade item, xmlize it an save to db
5215 if (($this->level
== 5) and ($tagName == "GRADE_ITEM")) {
5216 //Prepend XML standard header to info gathered
5217 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5218 //Call to xmlize for this portion of xml data (one PREFERENCE)
5219 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5220 $data = xmlize($xml_data,0);
5221 //echo strftime ("%X",time())."<p>"; //Debug
5222 //traverse_xmlize($data); //Debug
5223 //print_object ($GLOBALS['traverse_array']); //Debug
5224 //$GLOBALS['traverse_array']=""; //Debug
5225 //Now, save data to db. We'll use it later
5226 //Get id and status from data
5227 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
5231 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_items', $item_id,
5233 //Create returning info
5234 $this->info
= $this->counter
;
5240 //If we've finished a grade_category, xmlize it an save to db
5241 if (($this->level
== 5) and ($tagName == "GRADE_CATEGORY")) {
5242 //Prepend XML standard header to info gathered
5243 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5244 //Call to xmlize for this portion of xml data (one CATECORY)
5245 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5246 $data = xmlize($xml_data,0);
5247 //echo strftime ("%X",time())."<p>"; //Debug
5248 //traverse_xmlize($data); //Debug
5249 //print_object ($GLOBALS['traverse_array']); //Debug
5250 //$GLOBALS['traverse_array']=""; //Debug
5251 //Now, save data to db. We'll use it later
5252 //Get id and status from data
5253 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
5256 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_categories' ,$category_id,
5258 //Create returning info
5259 $this->info
= $this->counter
;
5264 //If we've finished a grade_outcome, xmlize it an save to db
5265 if (($this->level
== 5) and ($tagName == "GRADE_OUTCOME")) {
5266 //Prepend XML standard header to info gathered
5267 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5268 //Call to xmlize for this portion of xml data (one CATECORY)
5269 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5270 $data = xmlize($xml_data,0);
5271 //echo strftime ("%X",time())."<p>"; //Debug
5272 //traverse_xmlize($data); //Debug
5273 //print_object ($GLOBALS['traverse_array']); //Debug
5274 //$GLOBALS['traverse_array']=""; //Debug
5275 //Now, save data to db. We'll use it later
5276 //Get id and status from data
5277 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
5280 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_outcomes' ,$outcome_id,
5282 //Create returning info
5283 $this->info
= $this->counter
;
5288 //If we've finished a grade_outcomes_course, xmlize it an save to db
5289 if (($this->level
== 5) and ($tagName == "GRADE_OUTCOMES_COURSE")) {
5290 //Prepend XML standard header to info gathered
5291 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5292 //Call to xmlize for this portion of xml data (one CATECORY)
5293 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5294 $data = xmlize($xml_data,0);
5295 //echo strftime ("%X",time())."<p>"; //Debug
5296 //traverse_xmlize($data); //Debug
5297 //print_object ($GLOBALS['traverse_array']); //Debug
5298 //$GLOBALS['traverse_array']=""; //Debug
5299 //Now, save data to db. We'll use it later
5300 //Get id and status from data
5301 $outcomes_course_id = $data["GRADE_OUTCOMES_COURSE"]["#"]["ID"]["0"]["#"];
5304 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_outcomes_courses' ,$outcomes_course_id,
5306 //Create returning info
5307 $this->info
= $this->counter
;
5312 if (($this->level
== 5) and ($tagName == "GRADE_CATEGORIES_HISTORY")) {
5313 //Prepend XML standard header to info gathered
5314 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5315 //Call to xmlize for this portion of xml data (one PREFERENCE)
5316 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5317 $data = xmlize($xml_data,0);
5318 //echo strftime ("%X",time())."<p>"; //Debug
5319 //traverse_xmlize($data); //Debug
5320 //print_object ($GLOBALS['traverse_array']); //Debug
5321 //$GLOBALS['traverse_array']=""; //Debug
5322 //Now, save data to db. We'll use it later
5323 //Get id and status from data
5324 $id = $data["GRADE_CATEGORIES_HISTORY"]["#"]["ID"]["0"]["#"];
5328 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_categories_history', $id,
5330 //Create returning info
5331 $this->info
= $this->counter
;
5337 if (($this->level
== 5) and ($tagName == "GRADE_GRADES_HISTORY")) {
5338 //Prepend XML standard header to info gathered
5339 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5340 //Call to xmlize for this portion of xml data (one PREFERENCE)
5341 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5342 $data = xmlize($xml_data,0);
5343 //echo strftime ("%X",time())."<p>"; //Debug
5344 //traverse_xmlize($data); //Debug
5345 //print_object ($GLOBALS['traverse_array']); //Debug
5346 //$GLOBALS['traverse_array']=""; //Debug
5347 //Now, save data to db. We'll use it later
5348 //Get id and status from data
5349 $id = $data["GRADE_GRADES_HISTORY"]["#"]["ID"]["0"]["#"];
5353 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_grades_history', $id,
5355 //Create returning info
5356 $this->info
= $this->counter
;
5362 if (($this->level
== 5) and ($tagName == "GRADE_TEXT_HISTORY")) {
5363 //Prepend XML standard header to info gathered
5364 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5365 //Call to xmlize for this portion of xml data (one PREFERENCE)
5366 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5367 $data = xmlize($xml_data,0);
5368 //echo strftime ("%X",time())."<p>"; //Debug
5369 //traverse_xmlize($data); //Debug
5370 //print_object ($GLOBALS['traverse_array']); //Debug
5371 //$GLOBALS['traverse_array']=""; //Debug
5372 //Now, save data to db. We'll use it later
5373 //Get id and status from data
5374 $id = $data["GRADE_TEXT_HISTORY"]["#"]["ID"]["0"]["#"];
5377 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_grades_text_history', $id,
5379 //Create returning info
5380 $this->info
= $this->counter
;
5386 if (($this->level
== 5) and ($tagName == "GRADE_ITEM_HISTORY")) {
5387 //Prepend XML standard header to info gathered
5388 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5389 //Call to xmlize for this portion of xml data (one PREFERENCE)
5390 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5391 $data = xmlize($xml_data,0);
5392 //echo strftime ("%X",time())."<p>"; //Debug
5393 //traverse_xmlize($data); //Debug
5394 //print_object ($GLOBALS['traverse_array']); //Debug
5395 //$GLOBALS['traverse_array']=""; //Debug
5396 //Now, save data to db. We'll use it later
5397 //Get id and status from data
5398 $id = $data["GRADE_ITEM_HISTORY"]["#"]["ID"]["0"]["#"];
5402 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_items_history', $id,
5404 //Create returning info
5405 $this->info
= $this->counter
;
5411 if (($this->level
== 5) and ($tagName == "GRADE_OUTCOME_HISTORY")) {
5412 //Prepend XML standard header to info gathered
5413 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5414 //Call to xmlize for this portion of xml data (one PREFERENCE)
5415 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5416 $data = xmlize($xml_data,0);
5417 //echo strftime ("%X",time())."<p>"; //Debug
5418 //traverse_xmlize($data); //Debug
5419 //print_object ($GLOBALS['traverse_array']); //Debug
5420 //$GLOBALS['traverse_array']=""; //Debug
5421 //Now, save data to db. We'll use it later
5422 //Get id and status from data
5423 $id = $data["GRADE_OUTCOME_HISTORY"]["#"]["ID"]["0"]["#"];
5427 $status = backup_putid($this->preferences
->backup_unique_code
, 'grade_outcomes_history', $id,
5429 //Create returning info
5430 $this->info
= $this->counter
;
5437 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
5438 //Speed up a lot (avoid parse all)
5439 if ($tagName == "GRADEBOOK" and $this->level
== 3) {
5440 $this->finished
= true;
5445 $this->tree
[$this->level
] = "";
5447 $this->content
= "";
5451 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
5452 function endElementUsers($parser, $tagName) {
5454 //Check if we are into USERS zone
5455 if ($this->tree
[3] == "USERS") {
5456 //if (trim($this->content)) //Debug
5457 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5458 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
5459 //Dependig of different combinations, do different things
5460 if ($this->level
== 4) {
5465 //Save to db, only save if record not already exist
5466 // if there already is an new_id for this entry, just use that new_id?
5467 $newuser = backup_getid($this->preferences
->backup_unique_code
,"user",$this->info
->tempuser
->id
);
5468 if (isset($newuser->new_id
)) {
5469 $newid = $newuser->new_id
;
5474 backup_putid($this->preferences
->backup_unique_code
,"user",$this->info
->tempuser
->id
,
5475 $newid,$this->info
->tempuser
);
5478 if ($this->counter %
10 == 0) {
5479 if (!defined('RESTORE_SILENTLY')) {
5481 if ($this->counter %
200 == 0) {
5488 //Delete temp obejct
5489 unset($this->info
->tempuser
);
5493 if ($this->level
== 5) {
5496 $this->info
->users
[$this->getContents()] = $this->getContents();
5497 $this->info
->tempuser
->id
= $this->getContents();
5500 $this->info
->tempuser
->auth
= $this->getContents();
5503 $this->info
->tempuser
->confirmed
= $this->getContents();
5505 case "POLICYAGREED":
5506 $this->info
->tempuser
->policyagreed
= $this->getContents();
5509 $this->info
->tempuser
->deleted
= $this->getContents();
5512 $this->info
->tempuser
->username
= $this->getContents();
5515 $this->info
->tempuser
->password
= $this->getContents();
5518 $this->info
->tempuser
->idnumber
= $this->getContents();
5521 $this->info
->tempuser
->firstname
= $this->getContents();
5524 $this->info
->tempuser
->lastname
= $this->getContents();
5527 $this->info
->tempuser
->email
= $this->getContents();
5530 $this->info
->tempuser
->emailstop
= $this->getContents();
5533 $this->info
->tempuser
->icq
= $this->getContents();
5536 $this->info
->tempuser
->skype
= $this->getContents();
5539 $this->info
->tempuser
->aim
= $this->getContents();
5542 $this->info
->tempuser
->yahoo
= $this->getContents();
5545 $this->info
->tempuser
->msn
= $this->getContents();
5548 $this->info
->tempuser
->phone1
= $this->getContents();
5551 $this->info
->tempuser
->phone2
= $this->getContents();
5554 $this->info
->tempuser
->institution
= $this->getContents();
5557 $this->info
->tempuser
->department
= $this->getContents();
5560 $this->info
->tempuser
->address
= $this->getContents();
5563 $this->info
->tempuser
->city
= $this->getContents();
5566 $this->info
->tempuser
->country
= $this->getContents();
5569 $this->info
->tempuser
->lang
= $this->getContents();
5572 $this->info
->tempuser
->theme
= $this->getContents();
5575 $this->info
->tempuser
->timezone
= $this->getContents();
5578 $this->info
->tempuser
->firstaccess
= $this->getContents();
5581 $this->info
->tempuser
->lastaccess
= $this->getContents();
5584 $this->info
->tempuser
->lastlogin
= $this->getContents();
5586 case "CURRENTLOGIN":
5587 $this->info
->tempuser
->currentlogin
= $this->getContents();
5590 $this->info
->tempuser
->lastip
= $this->getContents();
5593 $this->info
->tempuser
->secret
= $this->getContents();
5596 $this->info
->tempuser
->picture
= $this->getContents();
5599 $this->info
->tempuser
->url
= $this->getContents();
5602 $this->info
->tempuser
->description
= $this->getContents();
5605 $this->info
->tempuser
->mailformat
= $this->getContents();
5608 $this->info
->tempuser
->maildigest
= $this->getContents();
5611 $this->info
->tempuser
->maildisplay
= $this->getContents();
5614 $this->info
->tempuser
->htmleditor
= $this->getContents();
5617 $this->info
->tempuser
->ajax
= $this->getContents();
5619 case "AUTOSUBSCRIBE":
5620 $this->info
->tempuser
->autosubscribe
= $this->getContents();
5623 $this->info
->tempuser
->trackforums
= $this->getContents();
5626 $this->info
->tempuser
->mnethosturl
= $this->getContents();
5628 case "TIMEMODIFIED":
5629 $this->info
->tempuser
->timemodified
= $this->getContents();
5635 if ($this->level
== 6 && $this->tree
[5]!="ROLES_ASSIGNMENTS" && $this->tree
[5]!="ROLES_OVERRIDES") {
5638 //We've finalized a role, get it
5639 $this->info
->tempuser
->roles
[$this->info
->temprole
->type
] = $this->info
->temprole
;
5640 unset($this->info
->temprole
);
5642 case "USER_PREFERENCE":
5643 //We've finalized a user_preference, get it
5644 $this->info
->tempuser
->user_preferences
[$this->info
->tempuserpreference
->name
] = $this->info
->tempuserpreference
;
5645 unset($this->info
->tempuserpreference
);
5650 if ($this->level
== 7) {
5653 $this->info
->temprole
->type
= $this->getContents();
5656 $this->info
->temprole
->authority
= $this->getContents();
5659 $this->info
->temprole
->tea_role
= $this->getContents();
5662 $this->info
->temprole
->editall
= $this->getContents();
5665 $this->info
->temprole
->timestart
= $this->getContents();
5668 $this->info
->temprole
->timeend
= $this->getContents();
5670 case "TIMEMODIFIED":
5671 $this->info
->temprole
->timemodified
= $this->getContents();
5674 $this->info
->temprole
->timestart
= $this->getContents();
5677 $this->info
->temprole
->timeend
= $this->getContents();
5680 $this->info
->temprole
->time
= $this->getContents();
5683 $this->info
->temprole
->timeaccess
= $this->getContents();
5686 $this->info
->temprole
->enrol
= $this->getContents();
5689 $this->info
->tempuserpreference
->name
= $this->getContents();
5692 $this->info
->tempuserpreference
->value
= $this->getContents();
5700 if ($this->tree
[5] == "ROLES_ASSIGNMENTS") {
5702 if ($this->level
== 7) {
5705 $this->info
->tempname
= $this->getContents();
5708 $this->info
->tempshortname
= $this->getContents();
5711 $this->info
->tempid
= $this->getContents(); // temp roleid
5716 if ($this->level
== 9) {
5720 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->name
= $this->info
->tempname
;
5722 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
5724 $this->info
->tempuserid
= $this->getContents();
5726 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->userid
= $this->getContents();
5729 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->hidden
= $this->getContents();
5732 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->timestart
= $this->getContents();
5735 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->timeend
= $this->getContents();
5737 case "TIMEMODIFIED":
5738 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->timemodified
= $this->getContents();
5741 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->modifierid
= $this->getContents();
5744 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->enrol
= $this->getContents();
5747 $this->info
->tempuser
->roleassignments
[$this->info
->tempid
]->assignments
[$this->info
->tempuserid
]->sortorder
= $this->getContents();
5752 } /// ends role_assignments
5754 if ($this->tree
[5] == "ROLES_OVERRIDES") {
5755 if ($this->level
== 7) {
5758 $this->info
->tempname
= $this->getContents();
5761 $this->info
->tempshortname
= $this->getContents();
5764 $this->info
->tempid
= $this->getContents(); // temp roleid
5769 if ($this->level
== 9) {
5773 $this->info
->tempuser
->roleoverrides
[$this->info
->tempid
]->name
= $this->info
->tempname
;
5774 $this->info
->tempuser
->roleoverrides
[$this->info
->tempid
]->shortname
= $this->info
->tempshortname
;
5775 $this->info
->tempname
= $this->getContents(); // change to name of capability
5776 $this->info
->tempuser
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->name
= $this->getContents();
5779 $this->info
->tempuser
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->permission
= $this->getContents();
5781 case "TIMEMODIFIED":
5782 $this->info
->tempuser
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->timemodified
= $this->getContents();
5785 $this->info
->tempuser
->roleoverrides
[$this->info
->tempid
]->overrides
[$this->info
->tempname
]->modifierid
= $this->getContents();
5789 } /// ends role_overrides
5791 } // closes if this->tree[3]=="users"
5793 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
5794 //Speed up a lot (avoid parse all)
5795 if ($tagName == "USERS" and $this->level
== 3) {
5796 $this->finished
= true;
5801 $this->tree
[$this->level
] = "";
5803 $this->content
= "";
5807 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
5808 function endElementMessages($parser, $tagName) {
5809 //Check if we are into MESSAGES zone
5810 if ($this->tree
[3] == "MESSAGES") {
5811 //if (trim($this->content)) //Debug
5812 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5813 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n";//Debug
5814 //Acumulate data to info (content + close tag)
5815 //Reconvert: strip htmlchars again and trim to generate xml data
5816 if (!isset($this->temp
)) {
5819 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
5820 //If we've finished a message, xmlize it an save to db
5821 if (($this->level
== 4) and ($tagName == "MESSAGE")) {
5822 //Prepend XML standard header to info gathered
5823 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5824 //Call to xmlize for this portion of xml data (one MESSAGE)
5825 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5826 $data = xmlize($xml_data,0);
5827 //echo strftime ("%X",time())."<p>"; //Debug
5828 //traverse_xmlize($data); //Debug
5829 //print_object ($GLOBALS['traverse_array']); //Debug
5830 //$GLOBALS['traverse_array']=""; //Debug
5831 //Now, save data to db. We'll use it later
5832 //Get id and status from data
5833 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
5834 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
5835 if ($message_status == "READ") {
5836 $table = "message_read";
5842 $status = backup_putid($this->preferences
->backup_unique_code
, $table,$message_id,
5844 //Create returning info
5845 $this->info
= $this->counter
;
5849 //If we've finished a contact, xmlize it an save to db
5850 if (($this->level
== 5) and ($tagName == "CONTACT")) {
5851 //Prepend XML standard header to info gathered
5852 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5853 //Call to xmlize for this portion of xml data (one MESSAGE)
5854 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5855 $data = xmlize($xml_data,0);
5856 //echo strftime ("%X",time())."<p>"; //Debug
5857 //traverse_xmlize($data); //Debug
5858 //print_object ($GLOBALS['traverse_array']); //Debug
5859 //$GLOBALS['traverse_array']=""; //Debug
5860 //Now, save data to db. We'll use it later
5861 //Get id and status from data
5862 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
5865 $status = backup_putid($this->preferences
->backup_unique_code
, 'message_contacts' ,$contact_id,
5867 //Create returning info
5868 $this->info
= $this->counter
;
5874 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
5875 //Speed up a lot (avoid parse all)
5876 if ($tagName == "MESSAGES" and $this->level
== 3) {
5877 $this->finished
= true;
5882 $this->tree
[$this->level
] = "";
5884 $this->content
= "";
5888 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
5889 function endElementQuestions($parser, $tagName) {
5890 //Check if we are into QUESTION_CATEGORIES zone
5891 if ($this->tree
[3] == "QUESTION_CATEGORIES") {
5892 //if (trim($this->content)) //Debug
5893 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5894 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
5895 //Acumulate data to info (content + close tag)
5896 //Reconvert: strip htmlchars again and trim to generate xml data
5897 if (!isset($this->temp
)) {
5900 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
5901 //If we've finished a mod, xmlize it an save to db
5902 if (($this->level
== 4) and ($tagName == "QUESTION_CATEGORY")) {
5903 //Prepend XML standard header to info gathered
5904 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5905 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
5906 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5907 $data = xmlize($xml_data,0);
5908 //echo strftime ("%X",time())."<p>"; //Debug
5909 //traverse_xmlize($data); //Debug
5910 //print_object ($GLOBALS['traverse_array']); //Debug
5911 //$GLOBALS['traverse_array']=""; //Debug
5912 //Now, save data to db. We'll use it later
5914 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
5916 $status = backup_putid($this->preferences
->backup_unique_code
,"question_categories",$category_id,
5918 //Create returning info
5919 $ret_info = new object();
5920 $ret_info->id
= $category_id;
5921 $this->info
[] = $ret_info;
5927 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
5928 //Speed up a lot (avoid parse all)
5929 if ($tagName == "QUESTION_CATEGORIES" and $this->level
== 3) {
5930 $this->finished
= true;
5934 $this->tree
[$this->level
] = "";
5936 $this->content
= "";
5940 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
5941 function endElementScales($parser, $tagName) {
5942 //Check if we are into SCALES zone
5943 if ($this->tree
[3] == "SCALES") {
5944 //if (trim($this->content)) //Debug
5945 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5946 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
5947 //Acumulate data to info (content + close tag)
5948 //Reconvert: strip htmlchars again and trim to generate xml data
5949 if (!isset($this->temp
)) {
5952 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
5953 //If we've finished a scale, xmlize it an save to db
5954 if (($this->level
== 4) and ($tagName == "SCALE")) {
5955 //Prepend XML standard header to info gathered
5956 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
5957 //Call to xmlize for this portion of xml data (one SCALE)
5958 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5959 $data = xmlize($xml_data,0);
5960 //echo strftime ("%X",time())."<p>"; //Debug
5961 //traverse_xmlize($data); //Debug
5962 //print_object ($GLOBALS['traverse_array']); //Debug
5963 //$GLOBALS['traverse_array']=""; //Debug
5964 //Now, save data to db. We'll use it later
5965 //Get id and from data
5966 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
5968 $status = backup_putid($this->preferences
->backup_unique_code
,"scale",$scale_id,
5970 //Create returning info
5971 $ret_info = new object();
5972 $ret_info->id
= $scale_id;
5973 $this->info
[] = $ret_info;
5979 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
5980 //Speed up a lot (avoid parse all)
5981 if ($tagName == "SCALES" and $this->level
== 3) {
5982 $this->finished
= true;
5986 $this->tree
[$this->level
] = "";
5988 $this->content
= "";
5992 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
5993 function endElementGroups($parser, $tagName) {
5994 //Check if we are into GROUPS zone
5995 if ($this->tree
[3] == "GROUPS") {
5996 //if (trim($this->content)) //Debug
5997 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5998 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
5999 //Acumulate data to info (content + close tag)
6000 //Reconvert: strip htmlchars again and trim to generate xml data
6001 if (!isset($this->temp
)) {
6004 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
6005 //If we've finished a group, xmlize it an save to db
6006 if (($this->level
== 4) and ($tagName == "GROUP")) {
6007 //Prepend XML standard header to info gathered
6008 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
6009 //Call to xmlize for this portion of xml data (one GROUP)
6010 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6011 $data = xmlize($xml_data,0);
6012 //echo strftime ("%X",time())."<p>"; //Debug
6013 //traverse_xmlize($data); //Debug
6014 //print_object ($GLOBALS['traverse_array']); //Debug
6015 //$GLOBALS['traverse_array']=""; //Debug
6016 //Now, save data to db. We'll use it later
6017 //Get id and from data
6018 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
6020 $status = backup_putid($this->preferences
->backup_unique_code
,"groups",$group_id,
6022 //Create returning info
6023 $ret_info = new Object();
6024 $ret_info->id
= $group_id;
6025 $this->info
[] = $ret_info;
6031 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
6032 //Speed up a lot (avoid parse all)
6033 if ($tagName == "GROUPS" and $this->level
== 3) {
6034 $this->finished
= true;
6038 $this->tree
[$this->level
] = "";
6040 $this->content
= "";
6044 //This is the endTag handler we use where we are reading the groupings zone (todo="GROUPINGS")
6045 function endElementGroupings($parser, $tagName) {
6046 //Check if we are into GROUPINGS zone
6047 if ($this->tree
[3] == "GROUPINGS") {
6048 //Acumulate data to info (content + close tag)
6049 //Reconvert: strip htmlchars again and trim to generate xml data
6050 if (!isset($this->temp
)) {
6053 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
6054 //If we've finished a group, xmlize it an save to db
6055 if (($this->level
== 4) and ($tagName == "GROUPING")) {
6056 //Prepend XML standard header to info gathered
6057 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
6058 //Call to xmlize for this portion of xml data (one GROUPING)
6059 $data = xmlize($xml_data,0);
6060 //Now, save data to db. We'll use it later
6061 //Get id and from data
6062 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
6064 $status = backup_putid($this->preferences
->backup_unique_code
,"groupings",$grouping_id,
6066 //Create returning info
6067 $ret_info = new Object();
6068 $ret_info->id
= $grouping_id;
6069 $this->info
[] = $ret_info;
6075 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6076 //Speed up a lot (avoid parse all)
6077 if ($tagName == "GROUPINGS" and $this->level
== 3) {
6078 $this->finished
= true;
6082 $this->tree
[$this->level
] = "";
6084 $this->content
= "";
6088 //This is the endTag handler we use where we are reading the groupingsgroups zone (todo="GROUPINGGROUPS")
6089 function endElementGroupingsGroups($parser, $tagName) {
6090 //Check if we are into GROUPINGSGROUPS zone
6091 if ($this->tree
[3] == "GROUPINGSGROUPS") {
6092 //Acumulate data to info (content + close tag)
6093 //Reconvert: strip htmlchars again and trim to generate xml data
6094 if (!isset($this->temp
)) {
6097 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
6098 //If we've finished a group, xmlize it an save to db
6099 if (($this->level
== 4) and ($tagName == "GROUPINGGROUP")) {
6100 //Prepend XML standard header to info gathered
6101 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
6102 //Call to xmlize for this portion of xml data (one GROUPING)
6103 $data = xmlize($xml_data,0);
6104 //Now, save data to db. We'll use it later
6105 //Get id and from data
6106 $groupinggroup_id = $data["GROUPINGGROUP"]["#"]["ID"]["0"]["#"];
6108 $status = backup_putid($this->preferences
->backup_unique_code
,"groupingsgroups",$groupinggroup_id,
6110 //Create returning info
6111 $ret_info = new Object();
6112 $ret_info->id
= $groupinggroup_id;
6113 $this->info
[] = $ret_info;
6119 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6120 //Speed up a lot (avoid parse all)
6121 if ($tagName == "GROUPINGSGROUPS" and $this->level
== 3) {
6122 $this->finished
= true;
6126 $this->tree
[$this->level
] = "";
6128 $this->content
= "";
6132 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
6133 function endElementEvents($parser, $tagName) {
6134 //Check if we are into EVENTS zone
6135 if ($this->tree
[3] == "EVENTS") {
6136 //if (trim($this->content)) //Debug
6137 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6138 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
6139 //Acumulate data to info (content + close tag)
6140 //Reconvert: strip htmlchars again and trim to generate xml data
6141 if (!isset($this->temp
)) {
6144 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
6145 //If we've finished a event, xmlize it an save to db
6146 if (($this->level
== 4) and ($tagName == "EVENT")) {
6147 //Prepend XML standard header to info gathered
6148 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
6149 //Call to xmlize for this portion of xml data (one EVENT)
6150 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6151 $data = xmlize($xml_data,0);
6152 //echo strftime ("%X",time())."<p>"; //Debug
6153 //traverse_xmlize($data); //Debug
6154 //print_object ($GLOBALS['traverse_array']); //Debug
6155 //$GLOBALS['traverse_array']=""; //Debug
6156 //Now, save data to db. We'll use it later
6157 //Get id and from data
6158 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
6160 $status = backup_putid($this->preferences
->backup_unique_code
,"event",$event_id,
6162 //Create returning info
6163 $ret_info = new object();
6164 $ret_info->id
= $event_id;
6165 $this->info
[] = $ret_info;
6171 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
6172 //Speed up a lot (avoid parse all)
6173 if ($tagName == "EVENTS" and $this->level
== 3) {
6174 $this->finished
= true;
6178 $this->tree
[$this->level
] = "";
6180 $this->content
= "";
6184 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
6185 function endElementModules($parser, $tagName) {
6186 //Check if we are into MODULES zone
6187 if ($this->tree
[3] == "MODULES") {
6188 //if (trim($this->content)) //Debug
6189 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6190 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
6191 //Acumulate data to info (content + close tag)
6192 //Reconvert: strip htmlchars again and trim to generate xml data
6193 if (!isset($this->temp
)) {
6196 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
6197 //If we've finished a mod, xmlize it an save to db
6198 if (($this->level
== 4) and ($tagName == "MOD")) {
6199 //Prepend XML standard header to info gathered
6200 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
6201 //Call to xmlize for this portion of xml data (one MOD)
6202 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6203 $data = xmlize($xml_data,0);
6204 //echo strftime ("%X",time())."<p>"; //Debug
6205 //traverse_xmlize($data); //Debug
6206 //print_object ($GLOBALS['traverse_array']); //Debug
6207 //$GLOBALS['traverse_array']=""; //Debug
6208 //Now, save data to db. We'll use it later
6209 //Get id and modtype from data
6210 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
6211 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
6212 //Only if we've selected to restore it
6213 if (!empty($this->preferences
->mods
[$mod_type]->restore
)) {
6215 $status = backup_putid($this->preferences
->backup_unique_code
,$mod_type,$mod_id,
6217 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6218 //Create returning info
6219 $ret_info = new object();
6220 $ret_info->id
= $mod_id;
6221 $ret_info->modtype
= $mod_type;
6222 $this->info
[] = $ret_info;
6231 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
6232 //Speed up a lot (avoid parse all)
6233 if ($tagName == "MODULES" and $this->level
== 3) {
6234 $this->finished
= true;
6238 $this->tree
[$this->level
] = "";
6240 $this->content
= "";
6244 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
6245 function endElementLogs($parser, $tagName) {
6246 //Check if we are into LOGS zone
6247 if ($this->tree
[3] == "LOGS") {
6248 //if (trim($this->content)) //Debug
6249 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6250 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br />\n"; //Debug
6251 //Acumulate data to info (content + close tag)
6252 //Reconvert: strip htmlchars again and trim to generate xml data
6253 if (!isset($this->temp
)) {
6256 $this->temp
.= htmlspecialchars(trim($this->content
))."</".$tagName.">";
6257 //If we've finished a log, xmlize it an save to db
6258 if (($this->level
== 4) and ($tagName == "LOG")) {
6259 //Prepend XML standard header to info gathered
6260 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp
;
6261 //Call to xmlize for this portion of xml data (one LOG)
6262 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6263 $data = xmlize($xml_data,0);
6264 //echo strftime ("%X",time())."<p>"; //Debug
6265 //traverse_xmlize($data); //Debug
6266 //print_object ($GLOBALS['traverse_array']); //Debug
6267 //$GLOBALS['traverse_array']=""; //Debug
6268 //Now, save data to db. We'll use it later
6269 //Get id and modtype from data
6270 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
6271 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
6272 //We only save log entries from backup file if they are:
6275 // - Module logs about one restored module
6276 if ($log_module == "course" or
6277 $log_module == "user" or
6278 $this->preferences
->mods
[$log_module]->restore
) {
6282 $status = backup_putid($this->preferences
->backup_unique_code
,"log",$log_id,
6284 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6285 //Create returning info
6286 $this->info
= $this->counter
;
6293 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
6294 //Speed up a lot (avoid parse all)
6295 if ($tagName == "LOGS" and $this->level
== 3) {
6296 $this->finished
= true;
6301 $this->tree
[$this->level
] = "";
6303 $this->content
= "";
6307 //This is the endTag default handler we use when todo is undefined
6308 function endElement($parser, $tagName) {
6309 if (trim($this->content
)) //Debug
6310 echo "C".str_repeat(" ",($this->level+
2)*2).$this->getContents()."<br />\n"; //Debug
6311 echo $this->level
.str_repeat(" ",$this->level
*2)."</".$tagName."><br />\n"; //Debug
6314 $this->tree
[$this->level
] = "";
6316 $this->content
= "";
6319 //This is the handler to read data contents (simple accumule it)
6320 function characterData($parser, $data) {
6321 $this->content
.= $data;
6325 //This function executes the MoodleParser
6326 function restore_read_xml ($xml_file,$todo,$preferences) {
6330 $xml_parser = xml_parser_create('UTF-8');
6331 $moodle_parser = new MoodleParser();
6332 $moodle_parser->todo
= $todo;
6333 $moodle_parser->preferences
= $preferences;
6334 xml_set_object($xml_parser,$moodle_parser);
6335 //Depending of the todo we use some element_handler or another
6336 if ($todo == "INFO") {
6337 //Define handlers to that zone
6338 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
6339 } else if ($todo == "ROLES") {
6340 // Define handlers to that zone
6341 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
6342 } else if ($todo == "COURSE_HEADER") {
6343 //Define handlers to that zone
6344 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
6345 } else if ($todo == 'BLOCKS') {
6346 //Define handlers to that zone
6347 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
6348 } else if ($todo == "SECTIONS") {
6349 //Define handlers to that zone
6350 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
6351 } else if ($todo == 'FORMATDATA') {
6352 //Define handlers to that zone
6353 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
6354 } else if ($todo == "METACOURSE") {
6355 //Define handlers to that zone
6356 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
6357 } else if ($todo == "GRADEBOOK") {
6358 //Define handlers to that zone
6359 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
6360 } else if ($todo == "USERS") {
6361 //Define handlers to that zone
6362 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
6363 } else if ($todo == "MESSAGES") {
6364 //Define handlers to that zone
6365 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
6366 } else if ($todo == "QUESTIONS") {
6367 //Define handlers to that zone
6368 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
6369 } else if ($todo == "SCALES") {
6370 //Define handlers to that zone
6371 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
6372 } else if ($todo == "GROUPS") {
6373 //Define handlers to that zone
6374 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
6375 } else if ($todo == "GROUPINGS") {
6376 //Define handlers to that zone
6377 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
6378 } else if ($todo == "GROUPINGSGROUPS") {
6379 //Define handlers to that zone
6380 xml_set_element_handler($xml_parser, "startElementGroupingsGroups", "endElementGroupingsGroups");
6381 } else if ($todo == "EVENTS") {
6382 //Define handlers to that zone
6383 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
6384 } else if ($todo == "MODULES") {
6385 //Define handlers to that zone
6386 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
6387 } else if ($todo == "LOGS") {
6388 //Define handlers to that zone
6389 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
6391 //Define default handlers (must no be invoked when everything become finished)
6392 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
6394 xml_set_character_data_handler($xml_parser, "characterData");
6395 $fp = fopen($xml_file,"r")
6398 while ($data = fread($fp, 4096) and !$moodle_parser->finished
)
6399 xml_parse($xml_parser, $data, feof($fp))
6400 or die(sprintf("XML error: %s at line %d",
6401 xml_error_string(xml_get_error_code($xml_parser)),
6402 xml_get_current_line_number($xml_parser)));
6405 //Get info from parser
6406 $info = $moodle_parser->info
;
6409 xml_parser_free($xml_parser);
6411 if ($status && !empty($info)) {
6419 * @param string $errorstr passed by reference, if silent is true,
6420 * errorstr will be populated and this function will return false rather than calling error() or notify()
6421 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
6422 * redirect to the next step in the restore process, instead will return $backup_unique_code
6424 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
6426 global $CFG, $SESSION;
6428 //Prepend dataroot to variable to have the absolute path
6429 $file = $CFG->dataroot
."/".$file;
6431 if (!defined('RESTORE_SILENTLY')) {
6432 //Start the main table
6433 echo "<table cellpadding=\"5\">";
6440 //Check the file exists
6441 if (!is_file($file)) {
6442 if (!defined('RESTORE_SILENTLY')) {
6443 error ("File not exists ($file)");
6445 $errorstr = "File not exists ($file)";
6450 //Check the file name ends with .zip
6451 if (!substr($file,-4) == ".zip") {
6452 if (!defined('RESTORE_SILENTLY')) {
6453 error ("File has an incorrect extension");
6455 $errorstr = 'File has an incorrect extension';
6460 //Now calculate the unique_code for this restore
6461 $backup_unique_code = time();
6463 //Now check and create the backup dir (if it doesn't exist)
6464 if (!defined('RESTORE_SILENTLY')) {
6465 echo "<li>".get_string("creatingtemporarystructures").'</li>';
6467 $status = check_and_create_backup_dir($backup_unique_code);
6470 $status = clear_backup_dir($backup_unique_code);
6473 //Now delete old data and directories under dataroot/temp/backup
6475 if (!defined('RESTORE_SILENTLY')) {
6476 echo "<li>".get_string("deletingolddata").'</li>';
6478 $status = backup_delete_old_data();
6481 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
6483 if (!defined('RESTORE_SILENTLY')) {
6484 echo "<li>".get_string("copyingzipfile").'</li>';
6486 if (! $status = backup_copy_file($file,$CFG->dataroot
."/temp/backup/".$backup_unique_code."/".basename($file))) {
6487 if (!defined('RESTORE_SILENTLY')) {
6488 notify("Error copying backup file. Invalid name or bad perms.");
6490 $errorstr = "Error copying backup file. Invalid name or bad perms";
6496 //Now unzip the file
6498 if (!defined('RESTORE_SILENTLY')) {
6499 echo "<li>".get_string("unzippingbackup").'</li>';
6501 if (! $status = restore_unzip ($CFG->dataroot
."/temp/backup/".$backup_unique_code."/".basename($file))) {
6502 if (!defined('RESTORE_SILENTLY')) {
6503 notify("Error unzipping backup file. Invalid zip file.");
6505 $errorstr = "Error unzipping backup file. Invalid zip file.";
6511 //Check for Blackboard backups and convert
6513 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
6514 if (!defined('RESTORE_SILENTLY')) {
6515 echo "<li>".get_string("checkingforbbexport").'</li>';
6517 $status = blackboard_convert($CFG->dataroot
."/temp/backup/".$backup_unique_code);
6520 //Now check for the moodle.xml file
6522 $xml_file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
6523 if (!defined('RESTORE_SILENTLY')) {
6524 echo "<li>".get_string("checkingbackup").'</li>';
6526 if (! $status = restore_check_moodle_file ($xml_file)) {
6527 if (!is_file($xml_file)) {
6528 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
6530 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
6532 if (!defined('RESTORE_SILENTLY')) {
6541 $course_header = "";
6543 //Now read the info tag (all)
6545 if (!defined('RESTORE_SILENTLY')) {
6546 echo "<li>".get_string("readinginfofrombackup").'</li>';
6548 //Reading info from file
6549 $info = restore_read_xml_info ($xml_file);
6550 //Reading course_header from file
6551 $course_header = restore_read_xml_course_header ($xml_file);
6554 if (!defined('RESTORE_SILENTLY')) {
6558 //End the main table
6563 //We compare Moodle's versions
6564 if ($CFG->version
< $info->backup_moodle_version
&& $status) {
6565 $message = new message();
6566 $message->serverversion
= $CFG->version
;
6567 $message->serverrelease
= $CFG->release
;
6568 $message->backupversion
= $info->backup_moodle_version
;
6569 $message->backuprelease
= $info->backup_moodle_release
;
6570 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
6574 //Now we print in other table, the backup and the course it contains info
6575 if ($info and $course_header and $status) {
6576 //First, the course info
6577 if (!defined('RESTORE_SILENTLY')) {
6578 $status = restore_print_course_header($course_header);
6580 //Now, the backup info
6582 if (!defined('RESTORE_SILENTLY')) {
6583 $status = restore_print_info($info);
6588 //Save course header and info into php session
6590 $SESSION->info
= $info;
6591 $SESSION->course_header
= $course_header;
6594 //Finally, a little form to continue
6595 //with some hidden fields
6597 if (!defined('RESTORE_SILENTLY')) {
6598 echo "<br /><div style='text-align:center'>";
6599 $hidden["backup_unique_code"] = $backup_unique_code;
6600 $hidden["launch"] = "form";
6601 $hidden["file"] = $file;
6602 $hidden["id"] = $id;
6603 print_single_button("restore.php", $hidden, get_string("continue"),"post");
6607 if (empty($noredirect)) {
6608 redirect($CFG->wwwroot
.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
6610 return $backup_unique_code;
6616 if (!defined('RESTORE_SILENTLY')) {
6617 error ("An error has ocurred");
6619 $errorstr = "An error has occured"; // helpful! :P
6626 function restore_setup_for_check(&$restore,$backup_unique_code) {
6628 $restore->backup_unique_code
=$backup_unique_code;
6629 $restore->users
= 2; // yuk
6630 $restore->course_files
= $SESSION->restore
->restore_course_files
;
6631 $restore->site_files
= $SESSION->restore
->restore_site_files
;
6632 if ($allmods = get_records("modules")) {
6633 foreach ($allmods as $mod) {
6634 $modname = $mod->name
;
6635 $var = "restore_".$modname;
6636 //Now check that we have that module info in the backup file
6637 if (isset($SESSION->info
->mods
[$modname]) && $SESSION->info
->mods
[$modname]->backup
== "true") {
6645 function backup_to_restore_array($backup,$k=0) {
6646 if (is_array($backup) ) {
6647 foreach ($backup as $key => $value) {
6648 $newkey = str_replace('backup','restore',$key);
6649 $restore[$newkey] = backup_to_restore_array($value,$key);
6652 else if (is_object($backup)) {
6653 $tmp = get_object_vars($backup);
6654 foreach ($tmp as $key => $value) {
6655 $newkey = str_replace('backup','restore',$key);
6656 $restore->$newkey = backup_to_restore_array($value,$key);
6660 $newkey = str_replace('backup','restore',$k);
6667 * compatibility function
6668 * checks for per-instance backups AND
6669 * older per-module backups
6670 * and returns whether userdata has been selected.
6672 function restore_userdata_selected($restore,$modname,$modid) {
6673 // check first for per instance array
6674 if (!empty($restore->mods
[$modname]->granular
)) { // supports per instance
6675 return array_key_exists($modid,$restore->mods
[$modname]->instances
)
6676 && !empty($restore->mods
[$modname]->instances
[$modid]->userinfo
);
6679 print_object($restore->mods
[$modname]);
6680 return !empty($restore->mods
[$modname]->userinfo
);
6683 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
6688 //Checks for the required files/functions to restore every module
6690 if ($allmods = get_records("modules") ) {
6691 foreach ($allmods as $mod) {
6692 $modname = $mod->name
;
6693 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
6694 //If file exists and we have selected to restore that type of module
6695 if ((file_exists($modfile)) and !empty($restore->mods
[$modname]) and ($restore->mods
[$modname]->restore
)) {
6696 include_once($modfile);
6701 if (!defined('RESTORE_SILENTLY')) {
6702 //Start the main table
6703 echo "<table cellpadding=\"5\">";
6710 //Localtion of the xml file
6711 $xml_file = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
."/moodle.xml";
6713 //If we've selected to restore into new course
6714 //create it (course)
6715 //Saving conversion id variables into backup_tables
6716 if ($restore->restoreto
== 2) {
6717 if (!defined('RESTORE_SILENTLY')) {
6718 echo '<li>'.get_string('creatingnewcourse') . '</li>';
6720 $oldidnumber = $course_header->course_idnumber
;
6721 if (!$status = restore_create_new_course($restore,$course_header)) {
6722 if (!defined('RESTORE_SILENTLY')) {
6723 notify("Error while creating the new empty course.");
6725 $errorstr = "Error while creating the new empty course.";
6730 //Print course fullname and shortname and category
6732 if (!defined('RESTORE_SILENTLY')) {
6734 echo "<li>".$course_header->course_fullname
." (".$course_header->course_shortname
.")".'</li>';
6735 echo "<li>".get_string("category").": ".$course_header->category
->name
.'</li>';
6736 if (!empty($oldidnumber)) {
6737 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
6740 //Put the destination course_id
6742 $restore->course_id
= $course_header->course_id
;
6745 if ($status = restore_open_html($restore,$course_header)){
6746 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
6750 $course = get_record("course","id",$restore->course_id
);
6752 if (!defined('RESTORE_SILENTLY')) {
6753 echo "<li>".get_string("usingexistingcourse");
6755 echo "<li>".get_string("from").": ".$course_header->course_fullname
." (".$course_header->course_shortname
.")".'</li>';
6756 echo "<li>".get_string("to").": ". format_string($course->fullname
) ." (".format_string($course->shortname
).")".'</li>';
6757 if (($restore->deleting
)) {
6758 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
6760 echo "<li>".get_string("addingdatatoexisting").'</li>';
6764 //If we have selected to restore deleting, we do it now.
6765 if ($restore->deleting
) {
6766 if (!defined('RESTORE_SILENTLY')) {
6767 echo "<li>".get_string("deletingolddata").'</li>';
6769 $status = remove_course_contents($restore->course_id
,false) and
6770 delete_dir_contents($CFG->dataroot
."/".$restore->course_id
,"backupdata");
6772 //Now , this situation is equivalent to the "restore to new course" one (we
6773 //have a course record and nothing more), so define it as "to new course"
6774 $restore->restoreto
= 2;
6776 if (!defined('RESTORE_SILENTLY')) {
6777 notify("An error occurred while deleting some of the course contents.");
6779 $errrostr = "An error occurred while deleting some of the course contents.";
6785 if (!defined('RESTORE_SILENTLY')) {
6786 notify("Error opening existing course.");
6789 $errorstr = "Error opening existing course.";
6795 //Now create users as needed
6796 if ($status and ($restore->users
== 0 or $restore->users
== 1)) {
6797 if (!defined('RESTORE_SILENTLY')) {
6798 echo "<li>".get_string("creatingusers")."<br />";
6800 if (!$status = restore_create_users($restore,$xml_file)) {
6801 if (!defined('RESTORE_SILENTLY')) {
6802 notify("Could not restore users.");
6804 $errorstr = "Could not restore users.";
6809 //Now print info about the work done
6811 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
6812 where backup_code = '$restore->backup_unique_code' and
6813 table_name = 'user'");
6821 //Iterate, filling counters
6822 foreach ($recs as $rec) {
6823 //Get full record, using backup_getids
6824 $record = backup_getid($restore->backup_unique_code
,"user",$rec->old_id
);
6825 if (strpos($record->info
,"new") !== false) {
6828 if (strpos($record->info
,"exists") !== false) {
6831 if (strpos($record->info
,"student") !== false) {
6833 } else if (strpos($record->info
,"teacher") !== false) {
6838 if ($counter %
10 == 0) {
6839 if (!defined('RESTORE_SILENTLY')) {
6841 if ($counter %
200 == 0) {
6848 if (!defined('RESTORE_SILENTLY')) {
6849 //Now print information gathered
6850 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
6852 echo "<li>".get_string("students").": ".$student_count.'</li>';
6853 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
6857 if (!defined('RESTORE_SILENTLY')) {
6858 notify("No users were found!");
6859 } // no need to return false here, it's recoverable.
6863 if (!defined('RESTORE_SILENTLY')) {
6869 //Now create groups as needed
6871 if (!defined('RESTORE_SILENTLY')) {
6872 echo "<li>".get_string("creatinggroups");
6874 if (!$status = restore_create_groups($restore,$xml_file)) {
6875 if (!defined('RESTORE_SILENTLY')) {
6876 notify("Could not restore groups!");
6878 $errorstr = "Could not restore groups!";
6882 if (!defined('RESTORE_SILENTLY')) {
6887 //Now create groupings as needed
6889 if (!defined('RESTORE_SILENTLY')) {
6890 echo "<li>".get_string("creatinggroupings");
6892 if (!$status = restore_create_groupings($restore,$xml_file)) {
6893 if (!defined('RESTORE_SILENTLY')) {
6894 notify("Could not restore groupings!");
6896 $errorstr = "Could not restore groupings!";
6900 if (!defined('RESTORE_SILENTLY')) {
6905 //Now create groupingsgroups as needed
6907 if (!defined('RESTORE_SILENTLY')) {
6908 echo "<li>".get_string("creatinggroupingsgroups");
6910 if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
6911 if (!defined('RESTORE_SILENTLY')) {
6912 notify("Could not restore groups in groupings!");
6914 $errorstr = "Could not restore groups in groupings!";
6918 if (!defined('RESTORE_SILENTLY')) {
6924 //Now create the course_sections and their associated course_modules
6925 //we have to do this after groups and groupings are restored, because we need the new groupings id
6928 if ($restore->restoreto
== 2) {
6929 if (!defined('RESTORE_SILENTLY')) {
6930 echo "<li>".get_string("creatingsections");
6932 if (!$status = restore_create_sections($restore,$xml_file)) {
6933 if (!defined('RESTORE_SILENTLY')) {
6934 notify("Error creating sections in the existing course.");
6936 $errorstr = "Error creating sections in the existing course.";
6940 if (!defined('RESTORE_SILENTLY')) {
6943 //Into existing course
6944 } else if ($restore->restoreto
== 0 or $restore->restoreto
== 1) {
6945 if (!defined('RESTORE_SILENTLY')) {
6946 echo "<li>".get_string("checkingsections");
6948 if (!$status = restore_create_sections($restore,$xml_file)) {
6949 if (!defined('RESTORE_SILENTLY')) {
6950 notify("Error creating sections in the existing course.");
6952 $errorstr = "Error creating sections in the existing course.";
6956 if (!defined('RESTORE_SILENTLY')) {
6961 if (!defined('RESTORE_SILENTLY')) {
6962 notify("Neither a new course or an existing one was specified.");
6965 $errorstr = "Neither a new course or an existing one was specified.";
6971 //Now create metacourse info
6972 if ($status and $restore->metacourse
) {
6973 //Only to new courses!
6974 if ($restore->restoreto
== 2) {
6975 if (!defined('RESTORE_SILENTLY')) {
6976 echo "<li>".get_string("creatingmetacoursedata");
6978 if (!$status = restore_create_metacourse($restore,$xml_file)) {
6979 if (!defined('RESTORE_SILENTLY')) {
6980 notify("Error creating metacourse in the course.");
6982 $errorstr = "Error creating metacourse in the course.";
6986 if (!defined('RESTORE_SILENTLY')) {
6993 //Now create categories and questions as needed
6995 include_once("$CFG->dirroot/question/restorelib.php");
6996 if (!defined('RESTORE_SILENTLY')) {
6997 echo "<li>".get_string("creatingcategoriesandquestions");
7000 if (!$status = restore_create_questions($restore,$xml_file)) {
7001 if (!defined('RESTORE_SILENTLY')) {
7002 notify("Could not restore categories and questions!");
7004 $errorstr = "Could not restore categories and questions!";
7008 if (!defined('RESTORE_SILENTLY')) {
7013 //Now create user_files as needed
7014 if ($status and ($restore->user_files
)) {
7015 if (!defined('RESTORE_SILENTLY')) {
7016 echo "<li>".get_string("copyinguserfiles");
7018 if (!$status = restore_user_files($restore)) {
7019 if (!defined('RESTORE_SILENTLY')) {
7020 notify("Could not restore user files!");
7022 $errorstr = "Could not restore user files!";
7026 //If all is ok (and we have a counter)
7027 if ($status and ($status !== true)) {
7028 //Inform about user dirs created from backup
7029 if (!defined('RESTORE_SILENTLY')) {
7031 echo "<li>".get_string("userzones").": ".$status;
7035 if (!defined('RESTORE_SILENTLY')) {
7040 //Now create course files as needed
7041 if ($status and ($restore->course_files
)) {
7042 if (!defined('RESTORE_SILENTLY')) {
7043 echo "<li>".get_string("copyingcoursefiles");
7045 if (!$status = restore_course_files($restore)) {
7046 if (empty($status)) {
7047 notify("Could not restore course files!");
7049 $errorstr = "Could not restore course files!";
7053 //If all is ok (and we have a counter)
7054 if ($status and ($status !== true)) {
7055 //Inform about user dirs created from backup
7056 if (!defined('RESTORE_SILENTLY')) {
7058 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7062 if (!defined('RESTORE_SILENTLY')) {
7068 //Now create site files as needed
7069 if ($status and ($restore->site_files
)) {
7070 if (!defined('RESTORE_SILENTLY')) {
7071 echo "<li>".get_string('copyingsitefiles');
7073 if (!$status = restore_site_files($restore)) {
7074 if (empty($status)) {
7075 notify("Could not restore site files!");
7077 $errorstr = "Could not restore site files!";
7081 //If all is ok (and we have a counter)
7082 if ($status and ($status !== true)) {
7083 //Inform about user dirs created from backup
7084 if (!defined('RESTORE_SILENTLY')) {
7086 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7090 if (!defined('RESTORE_SILENTLY')) {
7095 //Now create messages as needed
7096 if ($status and ($restore->messages
)) {
7097 if (!defined('RESTORE_SILENTLY')) {
7098 echo "<li>".get_string("creatingmessagesinfo");
7100 if (!$status = restore_create_messages($restore,$xml_file)) {
7101 if (!defined('RESTORE_SILENTLY')) {
7102 notify("Could not restore messages!");
7104 $errorstr = "Could not restore messages!";
7108 if (!defined('RESTORE_SILENTLY')) {
7113 //Now create scales as needed
7115 if (!defined('RESTORE_SILENTLY')) {
7116 echo "<li>".get_string("creatingscales");
7118 if (!$status = restore_create_scales($restore,$xml_file)) {
7119 if (!defined('RESTORE_SILENTLY')) {
7120 notify("Could not restore custom scales!");
7122 $errorstr = "Could not restore custom scales!";
7126 if (!defined('RESTORE_SILENTLY')) {
7131 //Now create events as needed
7133 if (!defined('RESTORE_SILENTLY')) {
7134 echo "<li>".get_string("creatingevents");
7136 if (!$status = restore_create_events($restore,$xml_file)) {
7137 if (!defined('RESTORE_SILENTLY')) {
7138 notify("Could not restore course events!");
7140 $errorstr = "Could not restore course events!";
7144 if (!defined('RESTORE_SILENTLY')) {
7149 //Now create course modules as needed
7151 if (!defined('RESTORE_SILENTLY')) {
7152 echo "<li>".get_string("creatingcoursemodules");
7154 if (!$status = restore_create_modules($restore,$xml_file)) {
7155 if (!defined('RESTORE_SILENTLY')) {
7156 notify("Could not restore modules!");
7158 $errorstr = "Could not restore modules!";
7162 if (!defined('RESTORE_SILENTLY')) {
7167 //Now create gradebook as needed -- AFTER modules!!!
7169 if (!defined('RESTORE_SILENTLY')) {
7170 echo "<li>".get_string("creatinggradebook");
7172 if (!$status = restore_create_gradebook($restore,$xml_file)) {
7173 if (!defined('RESTORE_SILENTLY')) {
7174 notify("Could not restore gradebook!");
7176 $errorstr = "Could not restore gradebook!";
7180 if (!defined('RESTORE_SILENTLY')) {
7185 //Bring back the course blocks -- do it AFTER the modules!!!
7187 //If we are deleting and bringing into a course or making a new course, same situation
7188 if($restore->restoreto
== 0 ||
$restore->restoreto
== 2) {
7189 if (!defined('RESTORE_SILENTLY')) {
7190 echo '<li>'.get_string('creatingblocks');
7192 $course_header->blockinfo
= !empty($course_header->blockinfo
) ?
$course_header->blockinfo
: NULL;
7193 if (!$status = restore_create_blocks($restore, $info->backup_block_format
, $course_header->blockinfo
, $xml_file)) {
7194 if (!defined('RESTORE_SILENTLY')) {
7195 notify('Error while creating the course blocks');
7197 $errorstr = "Error while creating the course blocks";
7201 if (!defined('RESTORE_SILENTLY')) {
7208 //If we are deleting and bringing into a course or making a new course, same situation
7209 if($restore->restoreto
== 0 ||
$restore->restoreto
== 2) {
7210 if (!defined('RESTORE_SILENTLY')) {
7211 echo '<li>'.get_string('courseformatdata');
7213 if (!$status = restore_set_format_data($restore, $xml_file)) {
7214 $error = "Error while setting the course format data";
7215 if (!defined('RESTORE_SILENTLY')) {
7222 if (!defined('RESTORE_SILENTLY')) {
7228 //Now create log entries as needed
7229 if ($status and ($restore->logs
)) {
7230 if (!defined('RESTORE_SILENTLY')) {
7231 echo "<li>".get_string("creatinglogentries");
7233 if (!$status = restore_create_logs($restore,$xml_file)) {
7234 if (!defined('RESTORE_SILENTLY')) {
7235 notify("Could not restore logs!");
7237 $errorstr = "Could not restore logs!";
7241 if (!defined('RESTORE_SILENTLY')) {
7246 //Now, if all is OK, adjust the instance field in course_modules !!
7248 if (!defined('RESTORE_SILENTLY')) {
7249 echo "<li>".get_string("checkinginstances");
7251 if (!$status = restore_check_instances($restore)) {
7252 if (!defined('RESTORE_SILENTLY')) {
7253 notify("Could not adjust instances in course_modules!");
7255 $errorstr = "Could not adjust instances in course_modules!";
7259 if (!defined('RESTORE_SILENTLY')) {
7264 //Now, if all is OK, adjust activity events
7266 if (!defined('RESTORE_SILENTLY')) {
7267 echo "<li>".get_string("refreshingevents");
7269 if (!$status = restore_refresh_events($restore)) {
7270 if (!defined('RESTORE_SILENTLY')) {
7271 notify("Could not refresh events for activities!");
7273 $errorstr = "Could not refresh events for activities!";
7277 if (!defined('RESTORE_SILENTLY')) {
7282 //Now, if all is OK, adjust inter-activity links
7284 if (!defined('RESTORE_SILENTLY')) {
7285 echo "<li>".get_string("decodinginternallinks");
7287 if (!$status = restore_decode_content_links($restore)) {
7288 if (!defined('RESTORE_SILENTLY')) {
7289 notify("Could not decode content links!");
7291 $errorstr = "Could not decode content links!";
7295 if (!defined('RESTORE_SILENTLY')) {
7300 //Now, with backup files prior to version 2005041100,
7301 //convert all the wiki texts in the course to markdown
7302 if ($status && $restore->backup_version
< 2005041100) {
7303 if (!defined('RESTORE_SILENTLY')) {
7304 echo "<li>".get_string("convertingwikitomarkdown");
7306 if (!$status = restore_convert_wiki2markdown($restore)) {
7307 if (!defined('RESTORE_SILENTLY')) {
7308 notify("Could not convert wiki texts to markdown!");
7310 $errorstr = "Could not convert wiki texts to markdown!";
7314 if (!defined('RESTORE_SILENTLY')) {
7319 /*******************************************************************************
7320 ************* Restore of Roles and Capabilities happens here ******************
7321 *******************************************************************************/
7322 $status = restore_create_roles($restore, $xml_file);
7323 $status = restore_roles_settings($restore, $xml_file);
7325 //Now if all is OK, update:
7326 // - course modinfo field
7327 // - categories table
7328 // - add user as teacher
7330 if (!defined('RESTORE_SILENTLY')) {
7331 echo "<li>".get_string("checkingcourse");
7334 rebuild_course_cache($restore->course_id
);
7336 $course = get_record("course","id",$restore->course_id
);
7337 fix_course_sortorder();
7338 // Check if the user has course update capability in the newly restored course
7339 // there is no need to load his capabilities again, because restore_roles_settings
7340 // would have loaded it anyway, if there is any assignments.
7342 $newcontext = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
7343 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
7344 // fix for MDL-9065, use the new config setting if exists
7345 if ($CFG->creatornewroleid
) {
7346 role_assign($CFG->creatornewroleid
, $USER->id
, 0, $newcontext->id
);
7348 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
7349 if ($legacyteacher = array_shift($legacyteachers)) {
7350 role_assign($legacyteacher->id
, $USER->id
, 0, $newcontext->id
);
7353 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
7357 if (!defined('RESTORE_SILENTLY')) {
7362 //Cleanup temps (files and db)
7364 if (!defined('RESTORE_SILENTLY')) {
7365 echo "<li>".get_string("cleaningtempdata");
7367 if (!$status = clean_temp_data ($restore)) {
7368 if (!defined('RESTORE_SILENTLY')) {
7369 notify("Could not clean up temporary data from files and database");
7371 $errorstr = "Could not clean up temporary data from files and database";
7375 if (!defined('RESTORE_SILENTLY')) {
7380 if ($status = restore_close_html($restore)){
7381 if (!defined('RESTORE_SILENTLY')) {
7382 echo '<li>Closing the Restorelog.html file.</li>';
7386 if (!defined('RESTORE_SILENTLY')) {
7387 notify("Could not close the restorelog.html file");
7390 $errorstr = "Could not close the restorelog.html file";
7395 if (!defined('RESTORE_SILENTLY')) {
7399 //End the main table
7406 //Create, open and write header of the html log file
7407 function restore_open_html($restore,$course_header) {
7413 //Open file for writing
7414 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
7415 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
7416 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
7418 $status = check_dir_exists($dest_dir,true);
7419 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
7420 //Add the stylesheet
7421 $stylesheetshtml = '';
7422 foreach ($CFG->stylesheets
as $stylesheet) {
7423 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
7425 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
7426 $languagehtml = get_html_lang($dir=true);
7428 //Write the header in the new logging file
7429 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
7430 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
7431 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
7432 fwrite ($restorelog_file,"<head>");
7433 fwrite ($restorelog_file,$stylesheetshtml);
7434 fwrite ($restorelog_file,"<title>".$course_header->course_shortname
." Restored </title>");
7435 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
7436 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname
." The FullName is now - ".$course_header->course_fullname
."<br/><br/>");
7437 $startdate = addslashes($course_header->course_startdate
);
7438 $date = usergetdate($startdate);
7439 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
7440 $startdate +
= $restore->course_startdateoffset
;
7441 $date = usergetdate($startdate);
7442 fwrite ($restorelog_file," This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
7445 return $restorelog_file;
7450 //Create & close footer of the html log file
7451 function restore_close_html($restore) {
7457 //Open file for writing
7458 //First, check that course_id/backupdata folder exists in CFG->dataroot
7459 $dest_dir = $CFG->dataroot
."/".$restore->course_id
."/backupdata";
7460 $status = check_dir_exists($dest_dir, true, true);
7461 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
7462 //Write the footer to close the logging file
7463 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
7464 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
7467 return $restorelog_file;
7473 /********************** Roles and Capabilities Related Functions *******************************/
7475 /* Yu: Note recovering of role assignments/overrides need to take place after
7476 users have been recovered, i.e. after we get their new_id, and after all
7477 roles have been recreated or mapped. Contexts can be created on the fly.
7478 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
7479 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
7480 once roles and users have been restored.
7484 * This function restores all the needed roles for this course
7485 * i.e. roles with an assignment in any of the mods or blocks,
7486 * roles assigned on any user (e.g. parent role) and roles
7487 * assigned at course levle
7488 * This function should check for duplicate roles first
7489 * It isn't now, just overwriting
7491 function restore_create_roles($restore, $xmlfile) {
7492 if (!defined('RESTORE_SILENTLY')) {
7493 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
7495 $info = restore_read_xml_roles($xmlfile);
7497 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
7499 // the following code creates new roles
7500 // but we could use more intelligent detection, and role mapping
7501 // get role mapping info from $restore
7502 $rolemappings = array();
7504 if (!empty($restore->rolesmapping
)) {
7505 $rolemappings = $restore->rolesmapping
;
7507 // $info->roles will be empty for backups pre 1.7
7508 if (isset($info->roles
) && $info->roles
) {
7510 foreach ($info->roles
as $oldroleid=>$roledata) {
7511 if (empty($restore->rolesmapping
)) {
7512 // if this is empty altogether, we came from import or there's no roles used in course at all
7513 // in this case, write the same oldid as this is the same site
7514 // no need to do mapping
7515 $status = backup_putid($restore->backup_unique_code
,"role",$oldroleid,
7516 $oldroleid); // adding a new id
7517 continue; // do not create additonal roles;
7519 // first we check if the roles are in the mappings
7520 // if so, we just do a mapping i.e. update oldids table
7521 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
7522 $status = backup_putid($restore->backup_unique_code
,"role",$oldroleid,
7523 $rolemappings[$oldroleid]); // adding a new id
7527 // code to make new role name/short name if same role name or shortname exists
7528 $fullname = $roledata->name
;
7529 $shortname = $roledata->shortname
;
7530 $currentfullname = "";
7531 $currentshortname = "";
7536 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
7537 $suffixshort = "_".$counter;
7542 $currentfullname = $fullname.$suffixfull;
7543 // Limit the size of shortname - database column accepts <= 15 chars
7544 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
7545 $coursefull = get_record("role","name",addslashes($currentfullname));
7546 $courseshort = get_record("role","shortname",addslashes($currentshortname));
7548 } while ($coursefull ||
$courseshort);
7550 $roledata->name
= $currentfullname;
7551 $roledata->shortname
= $currentshortname;
7553 // done finding a unique name
7555 $newroleid = create_role(addslashes($roledata->name
),addslashes($roledata->shortname
),'');
7556 $status = backup_putid($restore->backup_unique_code
,"role",$oldroleid,
7557 $newroleid); // adding a new id
7558 foreach ($roledata->capabilities
as $capability) {
7560 $roleinfo = new object();
7561 $roleinfo = (object)$capability;
7562 $roleinfo->contextid
= $sitecontext->id
;
7563 $roleinfo->capability
= $capability->name
;
7564 $roleinfo->roleid
= $newroleid;
7566 insert_record('role_capabilities', $roleinfo);
7575 * this function restores role assignments and role overrides
7576 * in course/user/block/mod level, it passed through
7577 * the xml file again
7579 function restore_roles_settings($restore, $xmlfile) {
7580 // data pulls from course, mod, user, and blocks
7582 /*******************************************************
7583 * Restoring from course level assignments *
7584 *******************************************************/
7585 if (!defined('RESTORE_SILENTLY')) {
7586 echo "<li>".get_string("creatingcourseroles").'</li>';
7588 $course = restore_read_xml_course_header($xmlfile);
7590 if (!isset($restore->rolesmapping
)) {
7591 $isimport = true; // course import from another course, or course with no role assignments
7593 $isimport = false; // course restore with role assignments
7596 if (!empty($course->roleassignments
) && !$isimport) {
7597 $courseassignments = $course->roleassignments
;
7599 foreach ($courseassignments as $oldroleid => $courseassignment) {
7600 restore_write_roleassignments($restore, $courseassignment->assignments
, "course", CONTEXT_COURSE
, $course->course_id
, $oldroleid);
7603 /*****************************************************
7604 * Restoring from course level overrides *
7605 *****************************************************/
7607 if (!empty($course->roleoverrides
) && !$isimport) {
7608 $courseoverrides = $course->roleoverrides
;
7609 foreach ($courseoverrides as $oldroleid => $courseoverride) {
7610 // if not importing into exiting course, or creating new role, we are ok
7611 // local course overrides to be respected (i.e. restored course overrides ignored)
7612 if ($restore->restoreto
!= 1 ||
empty($restore->rolesmapping
[$oldroleid])) {
7613 restore_write_roleoverrides($restore, $courseoverride->overrides
, "course", CONTEXT_COURSE
, $course->course_id
, $oldroleid);
7618 /*******************************************************
7619 * Restoring role assignments/overrdies *
7620 * from module level assignments *
7621 *******************************************************/
7623 if (!defined('RESTORE_SILENTLY')) {
7624 echo "<li>".get_string("creatingmodroles").'</li>';
7626 $sections = restore_read_xml_sections($xmlfile);
7627 $secs = $sections->sections
;
7629 foreach ($secs as $section) {
7630 if (isset($section->mods
)) {
7631 foreach ($section->mods
as $modid=>$mod) {
7632 if (isset($mod->roleassignments
) && !$isimport) {
7633 foreach ($mod->roleassignments
as $oldroleid=>$modassignment) {
7634 restore_write_roleassignments($restore, $modassignment->assignments
, "course_modules", CONTEXT_MODULE
, $modid, $oldroleid);
7637 // role overrides always applies, in import or backup/restore
7638 if (isset($mod->roleoverrides
)) {
7639 foreach ($mod->roleoverrides
as $oldroleid=>$modoverride) {
7640 restore_write_roleoverrides($restore, $modoverride->overrides
, "course_modules", CONTEXT_MODULE
, $modid, $oldroleid);
7647 /*************************************************
7648 * Restoring assignments from blocks level *
7649 * role assignments/overrides *
7650 *************************************************/
7652 if ($restore->restoreto
!= 1) { // skip altogether if restoring to exisitng course by adding
7653 if (!defined('RESTORE_SILENTLY')) {
7654 echo "<li>".get_string("creatingblocksroles").'</li>';
7656 $blocks = restore_read_xml_blocks($xmlfile);
7657 if (isset($blocks->instances
)) {
7658 foreach ($blocks->instances
as $instance) {
7659 if (isset($instance->roleassignments
) && !$isimport) {
7660 foreach ($instance->roleassignments
as $oldroleid=>$blockassignment) {
7661 restore_write_roleassignments($restore, $blockassignment->assignments
, "block_instance", CONTEXT_BLOCK
, $instance->id
, $oldroleid);
7665 // likewise block overrides should always be restored like mods
7666 if (isset($instance->roleoverrides
)) {
7667 foreach ($instance->roleoverrides
as $oldroleid=>$blockoverride) {
7668 restore_write_roleoverrides($restore, $blockoverride->overrides
, "block_instance", CONTEXT_BLOCK
, $instance->id
, $oldroleid);
7674 /************************************************
7675 * Restoring assignments from userid level *
7676 * role assignments/overrides *
7677 ************************************************/
7678 if (!defined('RESTORE_SILENTLY')) {
7679 echo "<li>".get_string("creatinguserroles").'</li>';
7681 $info = restore_read_xml_users($restore, $xmlfile);
7682 if (!empty($info->users
) && !$isimport) { // no need to restore user assignments for imports (same course)
7683 //For each user, take its info from backup_ids
7684 foreach ($info->users
as $userid) {
7685 $rec = backup_getid($restore->backup_unique_code
,"user",$userid);
7686 if (isset($rec->info
->roleassignments
)) {
7687 foreach ($rec->info
->roleassignments
as $oldroleid=>$userassignment) {
7688 restore_write_roleassignments($restore, $userassignment->assignments
, "user", CONTEXT_USER
, $userid, $oldroleid);
7691 if (isset($rec->info
->roleoverrides
)) {
7692 foreach ($rec->info
->roleoverrides
as $oldroleid=>$useroverride) {
7693 restore_write_roleoverrides($restore, $useroverride->overrides
, "user", CONTEXT_USER
, $userid, $oldroleid);
7702 // auxillary function to write role assignments read from xml to db
7703 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
7705 $role = backup_getid($restore->backup_unique_code
, "role", $oldroleid);
7707 foreach ($assignments as $assignment) {
7709 $olduser = backup_getid($restore->backup_unique_code
,"user",$assignment->userid
);
7710 //Oh dear, $olduser... can be an object, $obj->string or bool!
7711 if (!$olduser ||
(is_string($olduser->info
) && $olduser->info
== "notincourse")) { // it's possible that user is not in the course
7714 $assignment->userid
= $olduser->new_id
; // new userid here
7715 $oldmodifier = backup_getid($restore->backup_unique_code
,"user",$assignment->modifierid
);
7716 $assignment->modifierid
= !empty($oldmodifier->new_id
) ?
$oldmodifier->new_id
: 0; // new modifier id here
7717 $assignment->roleid
= $role->new_id
; // restored new role id
7719 // hack to make the correct contextid for course level imports
7720 if ($contextlevel == CONTEXT_COURSE
) {
7721 $oldinstance->new_id
= $restore->course_id
;
7723 $oldinstance = backup_getid($restore->backup_unique_code
,$table,$oldid);
7726 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id
);
7727 $assignment->contextid
= $newcontext->id
; // new context id
7728 // might already have same assignment
7729 role_assign($assignment->roleid
, $assignment->userid
, 0, $assignment->contextid
, $assignment->timestart
, $assignment->timeend
, $assignment->hidden
, $assignment->enrol
, $assignment->timemodified
);
7734 // auxillary function to write role assignments read from xml to db
7735 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
7737 // it is possible to have an override not relevant to this course context.
7738 // should be ignored(?)
7739 if (!$role = backup_getid($restore->backup_unique_code
, "role", $oldroleid)) {
7743 foreach ($overrides as $override) {
7744 $override->capability
= $override->name
;
7745 $oldmodifier = backup_getid($restore->backup_unique_code
,"user",$override->modifierid
);
7746 $override->modifierid
= $oldmodifier->new_id?
$oldmodifier->new_id
:0; // new modifier id here
7747 $override->roleid
= $role->new_id
; // restored new role id
7749 // hack to make the correct contextid for course level imports
7750 if ($contextlevel == CONTEXT_COURSE
) {
7751 $oldinstance->new_id
= $restore->course_id
;
7753 $oldinstance = backup_getid($restore->backup_unique_code
,$table,$oldid);
7756 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id
);
7757 $override->contextid
= $newcontext->id
; // new context id
7758 // use assign capability instead so we can add context to context_rel
7759 assign_capability($override->capability
, $override->permission
, $override->roleid
, $override->contextid
);
7762 //write activity date changes to the html log file, and update date values in the the xml array
7763 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
7768 // loop through time fields in $TAGS
7769 foreach ($TAGS as $TAG) {
7771 // check $TAG has a sensible value
7772 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
7774 if ($openlog==false) {
7775 $openlog = true; // only come through here once
7777 // open file for writing
7778 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
7779 check_dir_exists($course_dir, true);
7780 $restorelog = fopen("$course_dir/restorelog.html", "a");
7782 // start output for this record
7783 $msg = new stdClass();
7784 $msg->recordtype
= $recordtype;
7785 $msg->recordname
= $xml[$NAMETAG][0]['#'];
7786 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
7789 // write old date to $restorelog
7790 $value = $xml[$TAG][0]['#'];
7791 $date = usergetdate($value);
7793 $msg = new stdClass();
7795 $msg->weekday
= $date['weekday'];
7796 $msg->mday
= $date['mday'];
7797 $msg->month
= $date['month'];
7798 $msg->year
= $date['year'];
7799 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
7801 // write new date to $restorelog
7802 $value +
= $restore->course_startdateoffset
;
7803 $date = usergetdate($value);
7805 $msg = new stdClass();
7807 $msg->weekday
= $date['weekday'];
7808 $msg->mday
= $date['mday'];
7809 $msg->month
= $date['month'];
7810 $msg->year
= $date['year'];
7811 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
7813 // update $value in $xml tree for calling module
7814 $xml[$TAG][0]['#'] = "$value";
7817 // close the restore log, if it was opened
7819 fclose($restorelog);