MDL-15505
[moodle-linuxchix.git] / backup / restorelib.php
blobcd9c49c753412aa44a573a8b8cc652e504c48622
1 <?php //$Id$
2 //Functions used in restore
4 require_once($CFG->libdir.'/gradelib.php');
6 /**
7 * Group backup/restore constants, 0.
8 */
9 define('RESTORE_GROUPS_NONE', 0);
11 /**
12 * Group backup/restore constants, 1.
14 define('RESTORE_GROUPS_ONLY', 1);
16 /**
17 * Group backup/restore constants, 2.
19 define('RESTORE_GROUPINGS_ONLY', 2);
21 /**
22 * Group backup/restore constants, course/all.
24 define('RESTORE_GROUPS_GROUPINGS', 3);
26 //This function unzips a zip file in the same directory that it is
27 //It automatically uses pclzip or command line unzip
28 function restore_unzip ($file) {
30 return unzip_file($file, '', false);
34 //This function checks if moodle.xml seems to be a valid xml file
35 //(exists, has an xml header and a course main tag
36 function restore_check_moodle_file ($file) {
38 $status = true;
40 //Check if it exists
41 if ($status = is_file($file)) {
42 //Open it and read the first 200 bytes (chars)
43 $handle = fopen ($file, "r");
44 $first_chars = fread($handle,200);
45 $status = fclose ($handle);
46 //Chek if it has the requires strings
47 if ($status) {
48 $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
49 if ($status !== false) {
50 $status = strpos($first_chars,"<MOODLE_BACKUP>");
55 return $status;
58 //This function iterates over all modules in backup file, searching for a
59 //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
60 function restore_refresh_events($restore) {
62 global $CFG;
63 $status = true;
65 //Take all modules in backup
66 $modules = $restore->mods;
67 //Iterate
68 foreach($modules as $name => $module) {
69 //Only if the module is being restored
70 if (isset($module->restore) && $module->restore == 1) {
71 //Include module library
72 include_once("$CFG->dirroot/mod/$name/lib.php");
73 //If module_refresh_events exists
74 $function_name = $name."_refresh_events";
75 if (function_exists($function_name)) {
76 $status = $function_name($restore->course_id);
80 return $status;
83 //This function makes all the necessary calls to xxxx_decode_content_links_caller()
84 //function in each module, passing them the desired contents to be decoded
85 //from backup format to destination site/course in order to mantain inter-activities
86 //working in the backup/restore process
87 function restore_decode_content_links($restore) {
88 global $CFG;
90 $status = true;
92 if (!defined('RESTORE_SILENTLY')) {
93 echo "<ul>";
96 // Recode links in the course summary.
97 if (!defined('RESTORE_SILENTLY')) {
98 echo '<li>' . get_string('from') . ' ' . get_string('course');
100 $course = get_record('course', 'id', $restore->course_id, '', '', '', '', 'id,summary');
101 $coursesummary = restore_decode_content_links_worker($course->summary, $restore);
102 if ($coursesummary != $course->summary) {
103 $course->summary = addslashes($coursesummary);
104 if (!update_record('course', $course)) {
105 $status = false;
108 if (!defined('RESTORE_SILENTLY')) {
109 echo '</li>';
112 // Recode links in section summaries.
113 $sections = get_records('course_sections', 'course', $restore->course_id, 'id', 'id,summary');
114 if ($sections) {
115 if (!defined('RESTORE_SILENTLY')) {
116 echo '<li>' . get_string('from') . ' ' . get_string('sections');
118 foreach ($sections as $section) {
119 $sectionsummary = restore_decode_content_links_worker($section->summary, $restore);
120 if ($sectionsummary != $section->summary) {
121 $section->summary = addslashes($sectionsummary);
122 if (!update_record('course_sections', $section)) {
123 $status = false;
127 if (!defined('RESTORE_SILENTLY')) {
128 echo '</li>';
132 // Restore links in modules.
133 foreach ($restore->mods as $name => $info) {
134 //If the module is being restored
135 if (isset($info->restore) && $info->restore == 1) {
136 //Check if the xxxx_decode_content_links_caller exists
137 include_once("$CFG->dirroot/mod/$name/restorelib.php");
138 $function_name = $name."_decode_content_links_caller";
139 if (function_exists($function_name)) {
140 if (!defined('RESTORE_SILENTLY')) {
141 echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
143 $status = $function_name($restore) && $status;
144 if (!defined('RESTORE_SILENTLY')) {
145 echo '</li>';
151 // Process all html text also in blocks too
152 if (!defined('RESTORE_SILENTLY')) {
153 echo '<li>'.get_string ('from').' '.get_string('blocks');
156 if ($blocks = get_records('block', 'visible', 1)) {
157 foreach ($blocks as $block) {
158 if ($blockobject = block_instance($block->name)) {
159 $blockobject->decode_content_links_caller($restore);
164 if (!defined('RESTORE_SILENTLY')) {
165 echo '</li>';
168 // Restore links in questions.
169 require_once("$CFG->dirroot/question/restorelib.php");
170 if (!defined('RESTORE_SILENTLY')) {
171 echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
173 $status = question_decode_content_links_caller($restore) && $status;
174 if (!defined('RESTORE_SILENTLY')) {
175 echo '</li>';
178 if (!defined('RESTORE_SILENTLY')) {
179 echo "</ul>";
182 return $status;
185 //This function is called from all xxxx_decode_content_links_caller(),
186 //its task is to ask all modules (maybe other linkable objects) to restore
187 //links to them.
188 function restore_decode_content_links_worker($content,$restore) {
189 foreach($restore->mods as $name => $info) {
190 $function_name = $name."_decode_content_links";
191 if (function_exists($function_name)) {
192 $content = $function_name($content,$restore);
196 // For each block, call its encode_content_links method
197 static $blockobjects = null;
198 if (!isset($blockobjects)) {
199 $blockobjects = array();
200 if ($blocks = get_records('block', 'visible', 1)) {
201 foreach ($blocks as $block) {
202 if ($blockobject = block_instance($block->name)) {
203 $blockobjects[] = $blockobject;
209 foreach ($blockobjects as $blockobject) {
210 $content = $blockobject->decode_content_links($content,$restore);
213 return $content;
216 //This function converts all the wiki texts in the restored course
217 //to the Markdown format. Used only for backup files prior 2005041100.
218 //It calls every module xxxx_convert_wiki2markdown function
219 function restore_convert_wiki2markdown($restore) {
221 $status = true;
223 if (!defined('RESTORE_SILENTLY')) {
224 echo "<ul>";
226 foreach ($restore->mods as $name => $info) {
227 //If the module is being restored
228 if ($info->restore == 1) {
229 //Check if the xxxx_restore_wiki2markdown exists
230 $function_name = $name."_restore_wiki2markdown";
231 if (function_exists($function_name)) {
232 $status = $function_name($restore);
233 if (!defined('RESTORE_SILENTLY')) {
234 echo "<li>".get_string("modulenameplural",$name);
235 echo '</li>';
240 if (!defined('RESTORE_SILENTLY')) {
241 echo "</ul>";
243 return $status;
246 //This function receives a wiki text in the restore process and
247 //return it with every link to modules " modulename:moduleid"
248 //converted if possible. See the space before modulename!!
249 function restore_decode_wiki_content($content,$restore) {
251 global $CFG;
253 $result = $content;
255 $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
256 //We look for it
257 preg_match_all($searchstring,$content,$foundset);
258 //If found, then we are going to look for its new id (in backup tables)
259 if ($foundset[0]) {
260 //print_object($foundset); //Debug
261 //Iterate over foundset[2]. They are the old_ids
262 foreach($foundset[2] as $old_id) {
263 //We get the needed variables here (course id)
264 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
265 //Personalize the searchstring
266 $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
267 //If it is a link to this course, update the link to its new location
268 if($rec->new_id) {
269 //Now replace it
270 $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
271 } else {
272 //It's a foreign link so redirect it to its original URL
273 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
277 return $result;
281 //This function read the xml file and store it data from the info zone in an object
282 function restore_read_xml_info ($xml_file) {
284 //We call the main read_xml function, with todo = INFO
285 $info = restore_read_xml ($xml_file,"INFO",false);
287 return $info;
290 //This function read the xml file and store it data from the course header zone in an object
291 function restore_read_xml_course_header ($xml_file) {
293 //We call the main read_xml function, with todo = COURSE_HEADER
294 $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
296 return $info;
299 //This function read the xml file and store its data from the blocks in a object
300 function restore_read_xml_blocks ($restore, $xml_file) {
302 //We call the main read_xml function, with todo = BLOCKS
303 $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
305 return $info;
308 //This function read the xml file and store its data from the sections in a object
309 function restore_read_xml_sections ($xml_file) {
311 //We call the main read_xml function, with todo = SECTIONS
312 $info = restore_read_xml ($xml_file,"SECTIONS",false);
314 return $info;
317 //This function read the xml file and store its data from the course format in an object
318 function restore_read_xml_formatdata ($xml_file) {
320 //We call the main read_xml function, with todo = FORMATDATA
321 $info = restore_read_xml ($xml_file,'FORMATDATA',false);
323 return $info;
326 //This function read the xml file and store its data from the metacourse in a object
327 function restore_read_xml_metacourse ($xml_file) {
329 //We call the main read_xml function, with todo = METACOURSE
330 $info = restore_read_xml ($xml_file,"METACOURSE",false);
332 return $info;
335 //This function read the xml file and store its data from the gradebook in a object
336 function restore_read_xml_gradebook ($restore, $xml_file) {
338 //We call the main read_xml function, with todo = GRADEBOOK
339 $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
341 return $info;
344 //This function read the xml file and store its data from the users in
345 //backup_ids->info db (and user's id in $info)
346 function restore_read_xml_users ($restore,$xml_file) {
348 //We call the main read_xml function, with todo = USERS
349 $info = restore_read_xml ($xml_file,"USERS",$restore);
351 return $info;
354 //This function read the xml file and store its data from the messages in
355 //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
356 function restore_read_xml_messages ($restore,$xml_file) {
358 //We call the main read_xml function, with todo = MESSAGES
359 $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
361 return $info;
364 //This function read the xml file and store its data from the blogs in
365 //backup_ids->blog and backup_ids->blog_tag and db (and their counters in info)
366 function restore_read_xml_blogs ($restore,$xml_file) {
368 //We call the main read_xml function, with todo = BLOGS
369 $info = restore_read_xml ($xml_file,"BLOGS",$restore);
371 return $info;
375 //This function read the xml file and store its data from the questions in
376 //backup_ids->info db (and category's id in $info)
377 function restore_read_xml_questions ($restore,$xml_file) {
379 //We call the main read_xml function, with todo = QUESTIONS
380 $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
382 return $info;
385 //This function read the xml file and store its data from the scales in
386 //backup_ids->info db (and scale's id in $info)
387 function restore_read_xml_scales ($restore,$xml_file) {
389 //We call the main read_xml function, with todo = SCALES
390 $info = restore_read_xml ($xml_file,"SCALES",$restore);
392 return $info;
395 //This function read the xml file and store its data from the groups in
396 //backup_ids->info db (and group's id in $info)
397 function restore_read_xml_groups ($restore,$xml_file) {
399 //We call the main read_xml function, with todo = GROUPS
400 $info = restore_read_xml ($xml_file,"GROUPS",$restore);
402 return $info;
405 //This function read the xml file and store its data from the groupings in
406 //backup_ids->info db (and grouping's id in $info)
407 function restore_read_xml_groupings ($restore,$xml_file) {
409 //We call the main read_xml function, with todo = GROUPINGS
410 $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
412 return $info;
415 //This function read the xml file and store its data from the groupings in
416 //backup_ids->info db (and grouping's id in $info)
417 function restore_read_xml_groupings_groups ($restore,$xml_file) {
419 //We call the main read_xml function, with todo = GROUPINGS
420 $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
422 return $info;
425 //This function read the xml file and store its data from the events (course) in
426 //backup_ids->info db (and event's id in $info)
427 function restore_read_xml_events ($restore,$xml_file) {
429 //We call the main read_xml function, with todo = EVENTS
430 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
432 return $info;
435 //This function read the xml file and store its data from the modules in
436 //backup_ids->info
437 function restore_read_xml_modules ($restore,$xml_file) {
439 //We call the main read_xml function, with todo = MODULES
440 $info = restore_read_xml ($xml_file,"MODULES",$restore);
442 return $info;
445 //This function read the xml file and store its data from the logs in
446 //backup_ids->info
447 function restore_read_xml_logs ($restore,$xml_file) {
449 //We call the main read_xml function, with todo = LOGS
450 $info = restore_read_xml ($xml_file,"LOGS",$restore);
452 return $info;
455 function restore_read_xml_roles ($xml_file) {
456 //We call the main read_xml function, with todo = ROLES
457 $info = restore_read_xml ($xml_file,"ROLES",false);
459 return $info;
462 //This function prints the contents from the info parammeter passed
463 function restore_print_info ($info) {
465 global $CFG;
467 $status = true;
468 if ($info) {
469 $table = new object();
470 //This is tha align to every ingo table
471 $table->align = array ("right","left");
472 //This is the nowrap clause
473 $table->wrap = array ("","nowrap");
474 //The width
475 $table->width = "70%";
476 //Put interesting info in table
477 //The backup original name
478 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
479 $tab[0][1] = $info->backup_name;
480 //The moodle version
481 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
482 $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
483 //The backup version
484 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
485 $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
486 //The backup date
487 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
488 $tab[3][1] = userdate($info->backup_date);
489 //Print title
490 print_heading(get_string("backup").":");
491 $table->data = $tab;
492 //Print backup general info
493 print_table($table);
495 if ($info->backup_backup_version <= 2005070500) {
496 notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
499 //Now backup contents in another table
500 $tab = array();
501 //First mods info
502 $mods = $info->mods;
503 $elem = 0;
504 foreach ($mods as $key => $mod) {
505 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
506 if ($mod->backup == "false") {
507 $tab[$elem][1] = get_string("notincluded");
508 } else {
509 if ($mod->userinfo == "true") {
510 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
511 } else {
512 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
514 if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
515 foreach ($mod->instances as $instance) {
516 if ($instance->backup) {
517 $elem++;
518 $tab[$elem][0] = $instance->name;
519 if ($instance->userinfo == 'true') {
520 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
521 } else {
522 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
528 $elem++;
530 //Metacourse info
531 $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
532 if ($info->backup_metacourse == "true") {
533 $tab[$elem][1] = get_string("yes");
534 } else {
535 $tab[$elem][1] = get_string("no");
537 $elem++;
538 //Users info
539 $tab[$elem][0] = "<b>".get_string("users").":</b>";
540 $tab[$elem][1] = get_string($info->backup_users);
541 $elem++;
542 //Logs info
543 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
544 if ($info->backup_logs == "true") {
545 $tab[$elem][1] = get_string("yes");
546 } else {
547 $tab[$elem][1] = get_string("no");
549 $elem++;
550 //User Files info
551 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
552 if ($info->backup_user_files == "true") {
553 $tab[$elem][1] = get_string("yes");
554 } else {
555 $tab[$elem][1] = get_string("no");
557 $elem++;
558 //Course Files info
559 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
560 if ($info->backup_course_files == "true") {
561 $tab[$elem][1] = get_string("yes");
562 } else {
563 $tab[$elem][1] = get_string("no");
565 $elem++;
566 //site Files info
567 $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
568 if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
569 $tab[$elem][1] = get_string("yes");
570 } else {
571 $tab[$elem][1] = get_string("no");
573 $elem++;
574 //gradebook history info
575 $tab[$elem][0] = "<b>".get_string('gradebookhistories', 'grades').":</b>";
576 if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
577 $tab[$elem][1] = get_string("yes");
578 } else {
579 $tab[$elem][1] = get_string("no");
581 $elem++;
582 //Messages info (only showed if present)
583 if ($info->backup_messages == 'true') {
584 $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
585 $tab[$elem][1] = get_string('yes');
586 $elem++;
587 } else {
588 //Do nothing
590 $elem++;
591 //Blogs info (only showed if present)
592 if (isset($info->backup_blogs) && $info->backup_blogs == 'true') {
593 $tab[$elem][0] = "<b>".get_string('blogs','blog').":</b>";
594 $tab[$elem][1] = get_string('yes');
595 $elem++;
596 } else {
597 //Do nothing
599 $table->data = $tab;
600 //Print title
601 print_heading(get_string("backupdetails").":");
602 //Print backup general info
603 print_table($table);
604 } else {
605 $status = false;
608 return $status;
611 //This function prints the contents from the course_header parammeter passed
612 function restore_print_course_header ($course_header) {
614 $status = true;
615 if ($course_header) {
616 $table = new object();
617 //This is tha align to every ingo table
618 $table->align = array ("right","left");
619 //The width
620 $table->width = "70%";
621 //Put interesting course header in table
622 //The course name
623 $tab[0][0] = "<b>".get_string("name").":</b>";
624 $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
625 //The course summary
626 $tab[1][0] = "<b>".get_string("summary").":</b>";
627 $tab[1][1] = $course_header->course_summary;
628 $table->data = $tab;
629 //Print title
630 print_heading(get_string("course").":");
631 //Print backup course header info
632 print_table($table);
633 } else {
634 $status = false;
636 return $status;
639 //This function create a new course record.
640 //When finished, course_header contains the id of the new course
641 function restore_create_new_course($restore,&$course_header) {
643 global $CFG;
645 $status = true;
647 $fullname = $course_header->course_fullname;
648 $shortname = $course_header->course_shortname;
649 $currentfullname = "";
650 $currentshortname = "";
651 $counter = 0;
652 //Iteratere while the name exists
653 do {
654 if ($counter) {
655 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
656 $suffixshort = "_".$counter;
657 } else {
658 $suffixfull = "";
659 $suffixshort = "";
661 $currentfullname = $fullname.$suffixfull;
662 // Limit the size of shortname - database column accepts <= 100 chars
663 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
664 $coursefull = get_record("course","fullname",addslashes($currentfullname));
665 $courseshort = get_record("course","shortname",addslashes($currentshortname));
666 $counter++;
667 } while ($coursefull || $courseshort);
669 //New name = currentname
670 $course_header->course_fullname = $currentfullname;
671 $course_header->course_shortname = $currentshortname;
673 // first try to get it from restore
674 if ($restore->restore_restorecatto) {
675 $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
678 // else we try to get it from the xml file
679 //Now calculate the category
680 if (empty($category)) {
681 $category = get_record("course_categories","id",$course_header->category->id,
682 "name",addslashes($course_header->category->name));
685 //If no exists, try by name only
686 if (!$category) {
687 $category = get_record("course_categories","name",addslashes($course_header->category->name));
690 //If no exists, get category id 1
691 if (!$category) {
692 $category = get_record("course_categories","id","1");
695 //If category 1 doesn'exists, lets create the course category (get it from backup file)
696 if (!$category) {
697 $ins_category = new object();
698 $ins_category->name = addslashes($course_header->category->name);
699 $ins_category->parent = 0;
700 $ins_category->sortorder = 0;
701 $ins_category->coursecount = 0;
702 $ins_category->visible = 0; //To avoid interferences with the rest of the site
703 $ins_category->timemodified = time();
704 $newid = insert_record("course_categories",$ins_category);
705 $category->id = $newid;
706 $category->name = $course_header->category->name;
708 //If exists, put new category id
709 if ($category) {
710 $course_header->category->id = $category->id;
711 $course_header->category->name = $category->name;
712 //Error, cannot locate category
713 } else {
714 $course_header->category->id = 0;
715 $course_header->category->name = get_string("unknowncategory");
716 $status = false;
719 //Create the course_object
720 if ($status) {
721 $course = new object();
722 $course->category = addslashes($course_header->category->id);
723 $course->password = addslashes($course_header->course_password);
724 $course->fullname = addslashes($course_header->course_fullname);
725 $course->shortname = addslashes($course_header->course_shortname);
726 $course->idnumber = addslashes($course_header->course_idnumber);
727 $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
728 $course->summary = backup_todb($course_header->course_summary);
729 $course->format = addslashes($course_header->course_format);
730 $course->showgrades = addslashes($course_header->course_showgrades);
731 $course->newsitems = addslashes($course_header->course_newsitems);
732 $course->teacher = addslashes($course_header->course_teacher);
733 $course->teachers = addslashes($course_header->course_teachers);
734 $course->student = addslashes($course_header->course_student);
735 $course->students = addslashes($course_header->course_students);
736 $course->guest = addslashes($course_header->course_guest);
737 $course->startdate = addslashes($course_header->course_startdate);
738 $course->startdate += $restore->course_startdateoffset;
739 $course->numsections = addslashes($course_header->course_numsections);
740 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
741 $course->maxbytes = addslashes($course_header->course_maxbytes);
742 $course->showreports = addslashes($course_header->course_showreports);
743 if (isset($course_header->course_groupmode)) {
744 $course->groupmode = addslashes($course_header->course_groupmode);
746 if (isset($course_header->course_groupmodeforce)) {
747 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
749 if (isset($course_header->course_defaultgroupingid)) {
750 //keep the original now - convert after groupings restored
751 $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
753 $course->lang = addslashes($course_header->course_lang);
754 $course->theme = addslashes($course_header->course_theme);
755 $course->cost = addslashes($course_header->course_cost);
756 $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
757 $course->marker = addslashes($course_header->course_marker);
758 $course->visible = addslashes($course_header->course_visible);
759 $course->hiddensections = addslashes($course_header->course_hiddensections);
760 $course->timecreated = addslashes($course_header->course_timecreated);
761 $course->timemodified = addslashes($course_header->course_timemodified);
762 $course->metacourse = addslashes($course_header->course_metacourse);
763 $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
764 $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
765 $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
766 $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
767 $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
768 if ($course->enrolstartdate) { //Roll course dates
769 $course->enrolstartdate += $restore->course_startdateoffset;
771 $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
772 if ($course->enrolenddate) { //Roll course dates
773 $course->enrolenddate += $restore->course_startdateoffset;
775 $course->enrolperiod = addslashes($course_header->course_enrolperiod);
776 //Calculate sortorder field
777 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
778 FROM ' . $CFG->prefix . 'course
779 WHERE category=' . $course->category);
780 if (!empty($sortmax->max)) {
781 $course->sortorder = $sortmax->max + 1;
782 unset($sortmax);
783 } else {
784 $course->sortorder = 100;
787 //Now, recode some languages (Moodle 1.5)
788 if ($course->lang == 'ma_nt') {
789 $course->lang = 'mi_nt';
792 //Disable course->metacourse if avoided in restore config
793 if (!$restore->metacourse) {
794 $course->metacourse = 0;
797 //Check if the theme exists in destination server
798 $themes = get_list_of_themes();
799 if (!in_array($course->theme, $themes)) {
800 $course->theme = '';
803 //Now insert the record
804 $newid = insert_record("course",$course);
805 if ($newid) {
806 //save old and new course id
807 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
808 //Replace old course_id in course_header
809 $course_header->course_id = $newid;
810 } else {
811 $status = false;
815 return $status;
820 //This function creates all the block stuff when restoring courses
821 //It calls selectively to restore_create_block_instances() for 1.5
822 //and above backups. Upwards compatible with old blocks.
823 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
824 global $CFG;
825 $status = true;
827 delete_records('block_instance', 'pageid', $restore->course_id, 'pagetype', PAGE_COURSE_VIEW);
828 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
829 if (empty($blockinfo)) {
830 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
831 $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
832 blocks_repopulate_page($newpage);
833 } else if (!empty($CFG->showblocksonmodpages)) {
834 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
835 $blockrecords = get_records_select('block', '', '', 'name, id');
836 $temp_blocks_l = array();
837 $temp_blocks_r = array();
838 @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
839 $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
840 foreach($temp_blocks as $blockposition => $blocks) {
841 $blockweight = 0;
842 foreach($blocks as $blockname) {
843 if(!isset($blockrecords[$blockname])) {
844 // We don't know anything about this block!
845 continue;
847 $blockinstance = new stdClass;
848 // Remove any - prefix before doing the name-to-id mapping
849 if(substr($blockname, 0, 1) == '-') {
850 $blockname = substr($blockname, 1);
851 $blockinstance->visible = 0;
852 } else {
853 $blockinstance->visible = 1;
855 $blockinstance->blockid = $blockrecords[$blockname]->id;
856 $blockinstance->pageid = $restore->course_id;
857 $blockinstance->pagetype = PAGE_COURSE_VIEW;
858 $blockinstance->position = $blockposition;
859 $blockinstance->weight = $blockweight;
860 if(!$status = insert_record('block_instance', $blockinstance)) {
861 $status = false;
863 ++$blockweight;
867 } else if($backup_block_format == 'instances') {
868 $status = restore_create_block_instances($restore,$xml_file);
871 return $status;
875 //This function creates all the block_instances from xml when restoring in a
876 //new course
877 function restore_create_block_instances($restore,$xml_file) {
878 global $CFG;
879 $status = true;
881 //Check it exists
882 if (!file_exists($xml_file)) {
883 $status = false;
885 //Get info from xml
886 if ($status) {
887 $info = restore_read_xml_blocks($restore,$xml_file);
890 if(empty($info->instances)) {
891 return $status;
894 // First of all, iterate over the blocks to see which distinct pages we have
895 // in our hands and arrange the blocks accordingly.
896 $pageinstances = array();
897 foreach($info->instances as $instance) {
899 //pagetype and pageid black magic, we have to handle the case of blocks for the
900 //course, blocks from other pages in that course etc etc etc.
902 if($instance->pagetype == PAGE_COURSE_VIEW) {
903 // This one's easy...
904 $instance->pageid = $restore->course_id;
906 } else if (!empty($CFG->showblocksonmodpages)) {
907 $parts = explode('-', $instance->pagetype);
908 if($parts[0] == 'mod') {
909 if(!$restore->mods[$parts[1]]->restore) {
910 continue;
912 $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
914 if (empty($getid->new_id)) {
915 // Failed, perhaps the module was not included in the restore MDL-13554
916 continue;
918 $instance->pageid = $getid->new_id;
920 else {
921 // Not invented here ;-)
922 continue;
925 } else {
926 // do not restore activity blocks if disabled
927 continue;
930 if(!isset($pageinstances[$instance->pagetype])) {
931 $pageinstances[$instance->pagetype] = array();
933 if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
934 $pageinstances[$instance->pagetype][$instance->pageid] = array();
937 $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
940 $blocks = get_records_select('block', 'visible = 1', '', 'name, id, multiple');
942 // For each type of page we have restored
943 foreach($pageinstances as $thistypeinstances) {
945 // For each page id of that type
946 foreach($thistypeinstances as $thisidinstances) {
948 $addedblocks = array();
949 $maxweights = array();
951 // For each block instance in that page
952 foreach($thisidinstances as $instance) {
954 if(!isset($blocks[$instance->name])) {
955 //We are trying to restore a block we don't have...
956 continue;
959 //If we have already added this block once and multiples aren't allowed, disregard it
960 if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
961 continue;
964 //If its the first block we add to a new position, start weight counter equal to 0.
965 if(empty($maxweights[$instance->position])) {
966 $maxweights[$instance->position] = 0;
969 //If the instance weight is greater than the weight counter (we skipped some earlier
970 //blocks most probably), bring it back in line.
971 if($instance->weight > $maxweights[$instance->position]) {
972 $instance->weight = $maxweights[$instance->position];
975 //Add this instance
976 $instance->blockid = $blocks[$instance->name]->id;
978 // This will only be set if we come from 1.7 and above backups
979 // Also, must do this before insert (insert_record unsets id)
980 if (!empty($instance->id)) {
981 $oldid = $instance->id;
982 } else {
983 $oldid = 0;
986 if ($instance->id = insert_record('block_instance', $instance)) {
987 // Create block instance
988 if (!$blockobj = block_instance($instance->name, $instance)) {
989 $status = false;
990 break;
992 // Run the block restore if needed
993 if ($blockobj->backuprestore_instancedata_used()) {
994 // Get restore information
995 $data = backup_getid($restore->backup_unique_code,'block_instance',$oldid);
996 $data->new_id = $instance->id; // For completeness
997 if (!$blockobj->instance_restore($restore, $data)) {
998 $status = false;
999 break;
1002 // Save oldid after block restore process because info will be over-written with blank string
1003 if ($oldid) {
1004 backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
1007 } else {
1008 $status = false;
1009 break;
1012 //Get an object for the block and tell it it's been restored so it can update dates
1013 //etc. if necessary
1014 if ($blockobj = block_instance($instance->name,$instance)) {
1015 $blockobj->after_restore($restore);
1018 //Now we can increment the weight counter
1019 ++$maxweights[$instance->position];
1021 //Keep track of block types we have already added
1022 $addedblocks[$instance->name] = true;
1028 return $status;
1031 //This function creates all the course_sections and course_modules from xml
1032 //when restoring in a new course or simply checks sections and create records
1033 //in backup_ids when restoring in a existing course
1034 function restore_create_sections(&$restore, $xml_file) {
1036 global $CFG,$db;
1038 $status = true;
1039 //Check it exists
1040 if (!file_exists($xml_file)) {
1041 $status = false;
1043 //Get info from xml
1044 if ($status) {
1045 $info = restore_read_xml_sections($xml_file);
1047 //Put the info in the DB, recoding ids and saving the in backup tables
1049 $sequence = "";
1051 if ($info) {
1052 //For each, section, save it to db
1053 foreach ($info->sections as $key => $sect) {
1054 $sequence = "";
1055 $section = new object();
1056 $section->course = $restore->course_id;
1057 $section->section = $sect->number;
1058 $section->summary = backup_todb($sect->summary);
1059 $section->visible = $sect->visible;
1060 $section->sequence = "";
1061 //Now calculate the section's newid
1062 $newid = 0;
1063 if ($restore->restoreto == 2) {
1064 //Save it to db (only if restoring to new course)
1065 $newid = insert_record("course_sections",$section);
1066 } else {
1067 //Get section id when restoring in existing course
1068 $rec = get_record("course_sections","course",$restore->course_id,
1069 "section",$section->section);
1070 //If that section doesn't exist, get section 0 (every mod will be
1071 //asigned there
1072 if(!$rec) {
1073 $rec = get_record("course_sections","course",$restore->course_id,
1074 "section","0");
1076 //New check. If section 0 doesn't exist, insert it here !!
1077 //Teorically this never should happen but, in practice, some users
1078 //have reported this issue.
1079 if(!$rec) {
1080 $zero_sec = new object();
1081 $zero_sec->course = $restore->course_id;
1082 $zero_sec->section = 0;
1083 $zero_sec->summary = "";
1084 $zero_sec->sequence = "";
1085 $newid = insert_record("course_sections",$zero_sec);
1086 $rec->id = $newid;
1087 $rec->sequence = "";
1089 $newid = $rec->id;
1090 $sequence = $rec->sequence;
1092 if ($newid) {
1093 //save old and new section id
1094 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
1095 } else {
1096 $status = false;
1098 //If all is OK, go with associated mods
1099 if ($status) {
1100 //If we have mods in the section
1101 if (!empty($sect->mods)) {
1102 //For each mod inside section
1103 foreach ($sect->mods as $keym => $mod) {
1104 // Yu: This part is called repeatedly for every instance,
1105 // so it is necessary to set the granular flag and check isset()
1106 // when the first instance of this type of mod is processed.
1108 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
1110 if (!isset($restore->mods[$mod->type]->granular)) {
1111 if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
1112 // This defines whether we want to restore specific
1113 // instances of the modules (granular restore), or
1114 // whether we don't care and just want to restore
1115 // all module instances (non-granular).
1116 $restore->mods[$mod->type]->granular = true;
1117 } else {
1118 $restore->mods[$mod->type]->granular = false;
1122 //Check if we've to restore this module (and instance)
1123 if (!empty($restore->mods[$mod->type]->restore)) {
1124 if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
1125 || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
1126 && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
1128 //Get the module id from modules
1129 $module = get_record("modules","name",$mod->type);
1130 if ($module) {
1131 $course_module = new object();
1132 $course_module->course = $restore->course_id;
1133 $course_module->module = $module->id;
1134 $course_module->section = $newid;
1135 $course_module->added = $mod->added;
1136 $course_module->score = $mod->score;
1137 $course_module->indent = $mod->indent;
1138 $course_module->visible = $mod->visible;
1139 $course_module->groupmode = $mod->groupmode;
1140 if ($mod->groupingid and $grouping = restore_grouping_getid($restore, $mod->groupingid)) {
1141 $course_module->groupingid = $grouping->new_id;
1142 } else {
1143 $course_module->groupingid = 0;
1145 $course_module->groupmembersonly = $mod->groupmembersonly;
1146 $course_module->instance = 0;
1147 //NOTE: The instance (new) is calculated and updated in db in the
1148 // final step of the restore. We don't know it yet.
1149 //print_object($course_module); //Debug
1150 //Save it to db
1151 if ($mod->idnumber) {
1152 if (grade_verify_idnumber($mod->idnumber, $restore->course_id)) {
1153 $course_module->idnumber = $mod->idnumber;
1157 $newidmod = insert_record("course_modules", addslashes_recursive($course_module));
1158 if ($newidmod) {
1159 //save old and new module id
1160 //In the info field, we save the original instance of the module
1161 //to use it later
1162 backup_putid ($restore->backup_unique_code,"course_modules",
1163 $keym,$newidmod,$mod->instance);
1165 $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
1166 } else {
1167 $status = false;
1169 //Now, calculate the sequence field
1170 if ($status) {
1171 if ($sequence) {
1172 $sequence .= ",".$newidmod;
1173 } else {
1174 $sequence = $newidmod;
1177 } else {
1178 $status = false;
1185 //If all is OK, update sequence field in course_sections
1186 if ($status) {
1187 if (isset($sequence)) {
1188 $update_rec = new object();
1189 $update_rec->id = $newid;
1190 $update_rec->sequence = $sequence;
1191 $status = update_record("course_sections",$update_rec);
1195 } else {
1196 $status = false;
1198 return $status;
1201 //Called to set up any course-format specific data that may be in the file
1202 function restore_set_format_data($restore,$xml_file) {
1203 global $CFG,$db;
1205 $status = true;
1206 //Check it exists
1207 if (!file_exists($xml_file)) {
1208 return false;
1210 //Load data from XML to info
1211 if(!($info = restore_read_xml_formatdata($xml_file))) {
1212 return false;
1215 //Process format data if there is any
1216 if (isset($info->format_data)) {
1217 if(!$format=get_field('course','format','id',$restore->course_id)) {
1218 return false;
1220 // If there was any data then it must have a restore method
1221 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
1222 if(!file_exists($file)) {
1223 return false;
1225 require_once($file);
1226 $function=$format.'_restore_format_data';
1227 if(!function_exists($function)) {
1228 return false;
1230 return $function($restore,$info->format_data);
1233 // If we got here then there's no data, but that's cool
1234 return true;
1237 //This function creates all the metacourse data from xml, notifying
1238 //about each incidence
1239 function restore_create_metacourse($restore,$xml_file) {
1241 global $CFG,$db;
1243 $status = true;
1244 //Check it exists
1245 if (!file_exists($xml_file)) {
1246 $status = false;
1248 //Get info from xml
1249 if ($status) {
1250 //Load data from XML to info
1251 $info = restore_read_xml_metacourse($xml_file);
1254 //Process info about metacourse
1255 if ($status and $info) {
1256 //Process child records
1257 if (!empty($info->childs)) {
1258 foreach ($info->childs as $child) {
1259 $dbcourse = false;
1260 $dbmetacourse = false;
1261 //Check if child course exists in destination server
1262 //(by id in the same server or by idnumber and shortname in other server)
1263 if ($restore->original_wwwroot == $CFG->wwwroot) {
1264 //Same server, lets see by id
1265 $dbcourse = get_record('course','id',$child->id);
1266 } else {
1267 //Different server, lets see by idnumber and shortname, and only ONE record
1268 $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1269 if ($dbcount == 1) {
1270 $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1273 //If child course has been found, insert data
1274 if ($dbcourse) {
1275 $dbmetacourse->child_course = $dbcourse->id;
1276 $dbmetacourse->parent_course = $restore->course_id;
1277 $status = insert_record ('course_meta',$dbmetacourse);
1278 } else {
1279 //Child course not found, notice!
1280 if (!defined('RESTORE_SILENTLY')) {
1281 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
1285 //Now, recreate student enrolments...
1286 sync_metacourse($restore->course_id);
1288 //Process parent records
1289 if (!empty($info->parents)) {
1290 foreach ($info->parents as $parent) {
1291 $dbcourse = false;
1292 $dbmetacourse = false;
1293 //Check if parent course exists in destination server
1294 //(by id in the same server or by idnumber and shortname in other server)
1295 if ($restore->original_wwwroot == $CFG->wwwroot) {
1296 //Same server, lets see by id
1297 $dbcourse = get_record('course','id',$parent->id);
1298 } else {
1299 //Different server, lets see by idnumber and shortname, and only ONE record
1300 $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1301 if ($dbcount == 1) {
1302 $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1305 //If parent course has been found, insert data if it is a metacourse
1306 if ($dbcourse) {
1307 if ($dbcourse->metacourse) {
1308 $dbmetacourse->parent_course = $dbcourse->id;
1309 $dbmetacourse->child_course = $restore->course_id;
1310 $status = insert_record ('course_meta',$dbmetacourse);
1311 //Now, recreate student enrolments in parent course
1312 sync_metacourse($dbcourse->id);
1313 } else {
1314 //Parent course isn't metacourse, notice!
1315 if (!defined('RESTORE_SILENTLY')) {
1316 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1319 } else {
1320 //Parent course not found, notice!
1321 if (!defined('RESTORE_SILENTLY')) {
1322 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1329 return $status;
1333 * This function migrades all the pre 1.9 gradebook data from xml
1335 function restore_migrate_old_gradebook($restore,$xml_file) {
1336 global $CFG;
1338 $status = true;
1339 //Check it exists
1340 if (!file_exists($xml_file)) {
1341 return false;
1344 // Get info from xml
1345 // info will contain the number of record to process
1346 $info = restore_read_xml_gradebook($restore, $xml_file);
1348 // If we have info, then process
1349 if (empty($info)) {
1350 return $status;
1353 // make sure top course category exists
1354 $course_category = grade_category::fetch_course_category($restore->course_id);
1355 $course_category->load_grade_item();
1357 // we need to know if all grade items that were backed up are being restored
1358 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1359 // i.e. the aggregated grades of that category
1361 $restoreall = true; // set to false if any grade_item is not selected/restored
1362 $importing = !empty($SESSION->restore->importing); // there should not be a way to import old backups, but anyway ;-)
1364 if ($importing) {
1365 $restoreall = false;
1367 } else {
1368 $prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
1369 $prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
1371 // if any categories already present, skip restore of categories from backup
1372 if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
1373 $restoreall = false;
1375 unset($prev_grade_items);
1376 unset($prev_grade_cats);
1379 // force creation of all grade_items - the course_modules already exist
1380 grade_force_full_regrading($restore->course_id);
1381 grade_grab_course_grades($restore->course_id);
1383 // Start ul
1384 if (!defined('RESTORE_SILENTLY')) {
1385 echo '<ul>';
1388 /// Process letters
1389 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1390 // respect current grade letters if defined
1391 if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
1392 if (!defined('RESTORE_SILENTLY')) {
1393 echo '<li>'.get_string('gradeletters','grades').'</li>';
1395 // Fetch recordset_size records in each iteration
1396 $recs = get_records_select("backup_ids","table_name = 'grade_letter' AND backup_code = $restore->backup_unique_code",
1398 "old_id");
1399 if ($recs) {
1400 foreach ($recs as $rec) {
1401 // Get the full record from backup_ids
1402 $data = backup_getid($restore->backup_unique_code,'grade_letter',$rec->old_id);
1403 if ($data) {
1404 $info = $data->info;
1405 $dbrec = new object();
1406 $dbrec->contextid = $context->id;
1407 $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['GRADE_LOW']['0']['#']);
1408 $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
1409 insert_record('grade_letters', $dbrec);
1415 if (!defined('RESTORE_SILENTLY')) {
1416 echo '<li>'.get_string('categories','grades').'</li>';
1418 //Fetch recordset_size records in each iteration
1419 $recs = get_records_select("backup_ids","table_name = 'grade_category' AND backup_code = $restore->backup_unique_code",
1420 "old_id",
1421 "old_id");
1422 $cat_count = count($recs);
1423 if ($recs) {
1424 foreach ($recs as $rec) {
1425 //Get the full record from backup_ids
1426 $data = backup_getid($restore->backup_unique_code,'grade_category',$rec->old_id);
1427 if ($data) {
1428 //Now get completed xmlized object
1429 $info = $data->info;
1431 if ($restoreall) {
1432 if ($cat_count == 1) {
1433 $course_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
1434 $course_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
1435 $course_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
1436 $course_category->aggregateonlygraded = 0;
1437 $course_category->update('restore');
1438 $grade_category = $course_category;
1440 } else {
1441 $grade_category = new grade_category();
1442 $grade_category->courseid = $restore->course_id;
1443 $grade_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
1444 $grade_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
1445 $grade_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
1446 $grade_category->aggregateonlygraded = 0;
1447 $grade_category->insert('restore');
1448 $grade_category->load_grade_item(); // force cretion of grade_item
1451 } else {
1452 $grade_category = null;
1455 /// now, restore grade_items
1456 $items = array();
1457 if (!empty($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'])) {
1458 //Iterate over items
1459 foreach ($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'] as $ite_info) {
1460 $modname = backup_todb($ite_info['#']['MODULE_NAME']['0']['#'], false);
1461 $olditeminstance = backup_todb($ite_info['#']['CMINSTANCE']['0']['#'], false);
1462 if (!$mod = backup_getid($restore->backup_unique_code,$modname, $olditeminstance)) {
1463 continue; // not restored
1465 $iteminstance = $mod->new_id;
1466 if (!$cm = get_coursemodule_from_instance($modname, $iteminstance, $restore->course_id)) {
1467 continue; // does not exist
1470 if (!$grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0))) {
1471 continue; // no item yet??
1474 if ($grade_category) {
1475 $grade_item->sortorder = backup_todb($ite_info['#']['SORT_ORDER']['0']['#'], false);
1476 $grade_item->set_parent($grade_category->id);
1479 if ($importing
1480 or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
1481 // module instance not selected when restored using granular
1482 // skip this item
1483 continue;
1486 //Now process grade excludes
1487 if (empty($ite_info['#']['GRADE_EXCEPTIONS'])) {
1488 continue;
1491 foreach($ite_info['#']['GRADE_EXCEPTIONS']['0']['#']['GRADE_EXCEPTION'] as $exc_info) {
1492 if ($u = backup_getid($restore->backup_unique_code,"user",backup_todb($exc_info['#']['USERID']['0']['#']))) {
1493 $userid = $u->new_id;
1494 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
1495 $grade_grade->excluded = 1;
1496 if ($grade_grade->id) {
1497 $grade_grade->update('restore');
1498 } else {
1499 $grade_grade->insert('restore');
1509 if (!defined('RESTORE_SILENTLY')) {
1510 //End ul
1511 echo '</ul>';
1514 return $status;
1518 * This function creates all the gradebook data from xml
1520 function restore_create_gradebook($restore,$xml_file) {
1521 global $CFG;
1523 $status = true;
1524 //Check it exists
1525 if (!file_exists($xml_file)) {
1526 return false;
1529 // Get info from xml
1530 // info will contain the number of record to process
1531 $info = restore_read_xml_gradebook($restore, $xml_file);
1533 // If we have info, then process
1534 if (empty($info)) {
1535 return $status;
1538 if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
1539 $restore_histories = true;
1540 } else {
1541 $restore_histories = false;
1544 // make sure top course category exists
1545 $course_category = grade_category::fetch_course_category($restore->course_id);
1546 $course_category->load_grade_item();
1548 // we need to know if all grade items that were backed up are being restored
1549 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1550 // i.e. the aggregated grades of that category
1552 $restoreall = true; // set to false if any grade_item is not selected/restored or already exist
1553 $importing = !empty($SESSION->restore->importing);
1555 if ($importing) {
1556 $restoreall = false;
1558 } else {
1559 $prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
1560 $prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
1562 // if any categories already present, skip restore of categories from backup - course item or category already exist
1563 if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
1564 $restoreall = false;
1566 unset($prev_grade_items);
1567 unset($prev_grade_cats);
1569 if ($restoreall) {
1570 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = $restore->backup_unique_code", "", "old_id")) {
1571 foreach ($recs as $rec) {
1572 if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
1574 $info = $data->info;
1575 // do not restore if this grade_item is a mod, and
1576 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1578 if ($itemtype == 'mod') {
1579 $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1580 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1582 if (empty($restore->mods[$itemmodule]->granular)) {
1583 continue;
1584 } else if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
1585 continue;
1587 // at least one activity should not be restored - do not restore categories and manual items at all
1588 $restoreall = false;
1589 break;
1597 // Start ul
1598 if (!defined('RESTORE_SILENTLY')) {
1599 echo '<ul>';
1602 // array of restored categories - speedup ;-)
1603 $cached_categories = array();
1604 $outcomes = array();
1606 /// Process letters
1607 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1608 // respect current grade letters if defined
1609 if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
1610 if (!defined('RESTORE_SILENTLY')) {
1611 echo '<li>'.get_string('gradeletters','grades').'</li>';
1613 // Fetch recordset_size records in each iteration
1614 $recs = get_records_select("backup_ids","table_name = 'grade_letters' AND backup_code = $restore->backup_unique_code",
1616 "old_id");
1617 if ($recs) {
1618 foreach ($recs as $rec) {
1619 // Get the full record from backup_ids
1620 $data = backup_getid($restore->backup_unique_code,'grade_letters',$rec->old_id);
1621 if ($data) {
1622 $info = $data->info;
1623 $dbrec = new object();
1624 $dbrec->contextid = $context->id;
1625 $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
1626 $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
1627 insert_record('grade_letters', $dbrec);
1633 /// Preprocess outcomes - do not store them yet!
1634 if ($status and !$importing and $restoreall) {
1635 if (!defined('RESTORE_SILENTLY')) {
1636 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1638 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1640 "old_id");
1641 if ($recs) {
1642 foreach ($recs as $rec) {
1643 //Get the full record from backup_ids
1644 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1645 if ($data) {
1646 $info = $data->info;
1648 //first find out if outcome already exists
1649 $shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1651 if ($candidates = get_records_sql("SELECT *
1652 FROM {$CFG->prefix}grade_outcomes
1653 WHERE (courseid IS NULL OR courseid = $restore->course_id)
1654 AND shortname = '$shortname'
1655 ORDER BY courseid ASC, id ASC")) {
1656 $grade_outcome = reset($candidates);
1657 $outcomes[$rec->old_id] = $grade_outcome;
1658 continue;
1661 $dbrec = new object();
1663 if (has_capability('moodle/grade:manageoutcomes', get_context_instance(CONTEXT_SYSTEM))) {
1664 $oldoutcome = backup_todb($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']);
1665 if (empty($oldoutcome)) {
1666 //site wide
1667 $dbrec->courseid = null;
1668 } else {
1669 //course only
1670 $dbrec->courseid = $restore->course_id;
1672 } else {
1673 // no permission to add site outcomes
1674 $dbrec->courseid = $restore->course_id;
1677 //Get the fields
1678 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#'], false);
1679 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#'], false);
1680 $dbrec->scaleid = backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#'], false);
1681 $dbrec->description = backup_todb($info['GRADE_OUTCOME']['#']['DESCRIPTION']['0']['#'], false);
1682 $dbrec->timecreated = backup_todb($info['GRADE_OUTCOME']['#']['TIMECREATED']['0']['#'], false);
1683 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME']['#']['TIMEMODIFIED']['0']['#'], false);
1684 $dbrec->usermodified = backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#'], false);
1686 //Need to recode the scaleid
1687 if ($scale = backup_getid($restore->backup_unique_code, 'scale', $dbrec->scaleid)) {
1688 $dbrec->scaleid = $scale->new_id;
1691 //Need to recode the usermodified
1692 if ($modifier = backup_getid($restore->backup_unique_code, 'user', $dbrec->usermodified)) {
1693 $dbrec->usermodified = $modifier->new_id;
1696 $grade_outcome = new grade_outcome($dbrec, false);
1697 $outcomes[$rec->old_id] = $grade_outcome;
1703 /// Process grade items and grades
1704 if ($status) {
1705 if (!defined('RESTORE_SILENTLY')) {
1706 echo '<li>'.get_string('gradeitems','grades').'</li>';
1708 $counter = 0;
1710 //Fetch recordset_size records in each iteration
1711 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1712 "id", // restore in the backup order
1713 "old_id");
1715 if ($recs) {
1716 foreach ($recs as $rec) {
1717 //Get the full record from backup_ids
1718 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
1719 if ($data) {
1720 $info = $data->info;
1722 // first find out if category or normal item
1723 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
1724 if ($itemtype == 'course' or $itemtype == 'category') {
1725 if (!$restoreall or $importing) {
1726 continue;
1729 $oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
1730 if (!$cdata = backup_getid($restore->backup_unique_code,'grade_categories',$oldcat)) {
1731 continue;
1733 $cinfo = $cdata->info;
1734 unset($cdata);
1735 if ($itemtype == 'course') {
1737 $course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
1738 $course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
1739 $course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
1740 $course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
1741 $course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
1742 $course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
1743 $course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
1744 $course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
1745 $course_category->update('restore');
1747 $status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$course_category->id) && $status;
1748 $cached_categories[$oldcat] = $course_category;
1749 $grade_item = $course_category->get_grade_item();
1751 } else {
1752 $oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false);
1753 if (empty($cached_categories[$oldparent])) {
1754 debugging('parent not found '.$oldparent);
1755 continue; // parent not found, sorry
1757 $grade_category = new grade_category();
1758 $grade_category->courseid = $restore->course_id;
1759 $grade_category->parent = $cached_categories[$oldparent]->id;
1760 $grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
1761 $grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
1762 $grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
1763 $grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
1764 $grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
1765 $grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
1766 $grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
1767 $grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
1768 $grade_category->insert('restore');
1770 $status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$grade_category->id) && $status;
1771 $cached_categories[$oldcat] = $grade_category;
1772 $grade_item = $grade_category->get_grade_item(); // creates grade_item too
1774 unset($cinfo);
1776 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
1777 if (grade_verify_idnumber($idnumber, $restore->course_id)) {
1778 $grade_item->idnumber = $idnumber;
1781 $grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
1782 $grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
1783 $grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
1784 $grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
1785 $grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
1786 $grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
1787 $grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
1788 $grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
1789 $grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
1790 $grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
1791 $grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
1792 $grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
1793 $grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
1794 $grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
1795 $grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
1796 $grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
1798 if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
1799 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
1800 $grade_item->scaleid = $scale->new_id;
1803 if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) {
1804 $outcome = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false));
1805 $grade_item->outcomeid = $outcome->new_id;
1808 $grade_item->update('restore');
1809 $status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
1811 } else {
1812 if ($itemtype != 'mod' and (!$restoreall or $importing)) {
1813 // not extra gradebook stuff if restoring individual activities or something already there
1814 continue;
1817 $dbrec = new object();
1819 $dbrec->courseid = $restore->course_id;
1820 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
1821 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
1823 if ($itemtype == 'mod') {
1824 // iteminstance should point to new mod
1825 $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
1826 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $olditeminstance);
1827 $dbrec->iteminstance = $mod->new_id;
1828 if (!$cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id)) {
1829 // item not restored - no item
1830 continue;
1832 // keep in sync with activity idnumber
1833 $dbrec->idnumber = $cm->idnumber;
1835 } else {
1836 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
1838 if (grade_verify_idnumber($idnumber, $restore->course_id)) {
1839 //make sure the new idnumber is unique
1840 $dbrec->idnumber = $idnumber;
1844 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
1845 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
1846 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
1847 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false);
1848 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
1849 $dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
1850 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
1851 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
1852 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
1853 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
1854 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
1855 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
1856 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
1857 $dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
1858 $dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
1859 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
1860 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
1861 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
1862 $dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
1864 if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
1865 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
1866 $dbrec->scaleid = $scale->new_id;
1869 if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) {
1870 $oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
1871 if (empty($outcomes[$oldoutcome])) {
1872 continue; // error!
1874 if (empty($outcomes[$oldoutcome]->id)) {
1875 $outcomes[$oldoutcome]->insert('restore');
1876 $outcomes[$oldoutcome]->use_in($restore->course_id);
1877 backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id);
1879 $dbrec->outcomeid = $outcomes[$oldoutcome]->id;
1882 $grade_item = new grade_item($dbrec, false);
1883 $grade_item->insert('restore');
1884 if ($restoreall) {
1885 // set original parent if restored
1886 $oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
1887 if (!empty($cached_categories[$oldcat])) {
1888 $grade_item->set_parent($cached_categories[$oldcat]->id);
1891 $status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
1894 // no need to restore grades if user data is not selected or importing activities
1895 if ($importing
1896 or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
1897 // module instance not selected when restored using granular
1898 // skip this item
1899 continue;
1902 /// now, restore grade_grades
1903 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1904 //Iterate over items
1905 foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) {
1907 $grade = new grade_grade();
1908 $grade->itemid = $grade_item->id;
1910 $olduser = backup_todb($g_info['#']['USERID']['0']['#'], false);
1911 $user = backup_getid($restore->backup_unique_code,"user",$olduser);
1912 $grade->userid = $user->new_id;
1914 $grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false);
1915 $grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false);
1916 $grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false);
1917 // need to find scaleid
1918 if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) {
1919 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false));
1920 $grade->rawscaleid = $scale->new_id;
1923 if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) {
1924 if ($modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) {
1925 $grade->usermodified = $modifier->new_id;
1929 $grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false);
1930 $grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false);
1931 $grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false);
1932 $grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false);
1933 $grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false);
1934 $grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false);
1935 $grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false);
1936 $grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false);
1937 $grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false);
1938 $grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false);
1939 $grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false);
1940 $grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false);
1941 $grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false);
1943 $grade->insert('restore');
1944 backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id);
1946 $counter++;
1947 if ($counter % 20 == 0) {
1948 if (!defined('RESTORE_SILENTLY')) {
1949 echo ".";
1950 if ($counter % 400 == 0) {
1951 echo "<br />";
1954 backup_flush(300);
1963 /// add outcomes that are not used when doing full restore
1964 if ($status and $restoreall) {
1965 foreach ($outcomes as $oldoutcome=>$grade_outcome) {
1966 if (empty($grade_outcome->id)) {
1967 $grade_outcome->insert('restore');
1968 $grade_outcome->use_in($restore->course_id);
1969 backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id);
1975 if ($status and !$importing and $restore_histories) {
1976 /// following code is very inefficient
1978 $gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
1979 $gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
1980 $gihcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
1981 $gohcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
1983 // Number of records to get in every chunk
1984 $recordset_size = 2;
1986 // process histories
1987 if ($gchcount && $status) {
1988 if (!defined('RESTORE_SILENTLY')) {
1989 echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
1991 $counter = 0;
1992 while ($counter < $gchcount) {
1993 //Fetch recordset_size records in each iteration
1994 $recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
1995 "old_id",
1996 "old_id",
1997 $counter,
1998 $recordset_size);
1999 if ($recs) {
2000 foreach ($recs as $rec) {
2001 //Get the full record from backup_ids
2002 $data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_id);
2003 if ($data) {
2004 //Now get completed xmlized object
2005 $info = $data->info;
2006 //traverse_xmlize($info); //Debug
2007 //print_object ($GLOBALS['traverse_array']); //Debug
2008 //$GLOBALS['traverse_array']=""; //Debug
2010 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
2011 if (empty($oldobj->new_id)) {
2012 // if the old object is not being restored, can't restoring its history
2013 $counter++;
2014 continue;
2016 $dbrec->oldid = $oldobj->new_id;
2017 $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
2018 $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
2019 $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2021 // loggeduser might not be restored, e.g. admin
2022 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2023 $dbrec->loggeduser = $oldobj->new_id;
2026 // this item might not have a parent at all, do not skip it if no parent is specified
2027 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
2028 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
2029 if (empty($oldobj->new_id)) {
2030 // if the parent category not restored
2031 $counter++;
2032 continue;
2035 $dbrec->parent = $oldobj->new_id;
2036 $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
2037 // path needs to be rebuilt
2038 if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
2039 // to preserve the path and make it work, we need to replace the categories one by one
2040 // we first get the list of categories in current path
2041 if ($paths = explode("/", $path)) {
2042 $newpath = '';
2043 foreach ($paths as $catid) {
2044 if ($catid) {
2045 // find the new corresponding path
2046 $oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
2047 $newpath .= "/$oldpath->new_id";
2050 $dbrec->path = $newpath;
2053 $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
2054 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
2055 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
2056 $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
2058 $dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
2059 $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
2060 $dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
2062 $dbrec->courseid = $restore->course_id;
2063 insert_record('grade_categories_history', $dbrec);
2064 unset($dbrec);
2067 //Increment counters
2068 $counter++;
2069 //Do some output
2070 if ($counter % 1 == 0) {
2071 if (!defined('RESTORE_SILENTLY')) {
2072 echo ".";
2073 if ($counter % 20 == 0) {
2074 echo "<br />";
2077 backup_flush(300);
2084 // process histories
2085 if ($gghcount && $status) {
2086 if (!defined('RESTORE_SILENTLY')) {
2087 echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
2089 $counter = 0;
2090 while ($counter < $gghcount) {
2091 //Fetch recordset_size records in each iteration
2092 $recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
2093 "old_id",
2094 "old_id",
2095 $counter,
2096 $recordset_size);
2097 if ($recs) {
2098 foreach ($recs as $rec) {
2099 //Get the full record from backup_ids
2100 $data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_id);
2101 if ($data) {
2102 //Now get completed xmlized object
2103 $info = $data->info;
2104 //traverse_xmlize($info); //Debug
2105 //print_object ($GLOBALS['traverse_array']); //Debug
2106 //$GLOBALS['traverse_array']=""; //Debug
2108 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
2109 if (empty($oldobj->new_id)) {
2110 // if the old object is not being restored, can't restoring its history
2111 $counter++;
2112 continue;
2114 $dbrec->oldid = $oldobj->new_id;
2115 $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
2116 $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
2117 $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2118 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2119 $dbrec->loggeduser = $oldobj->new_id;
2122 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
2123 $dbrec->itemid = $oldobj->new_id;
2124 if (empty($dbrec->itemid)) {
2125 $counter++;
2126 continue; // grade item not being restored
2128 $oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
2129 $dbrec->userid = $oldobj->new_id;
2130 $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
2131 $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
2132 $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
2133 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
2134 $dbrec->usermodified = $oldobj->new_id;
2137 if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
2138 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
2139 $dbrec->rawscaleid = $scale->new_id;
2142 $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
2143 $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
2144 $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
2145 $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
2146 $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
2147 $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
2148 $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
2149 $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
2150 $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
2151 $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
2152 $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
2154 insert_record('grade_grades_history', $dbrec);
2155 unset($dbrec);
2158 //Increment counters
2159 $counter++;
2160 //Do some output
2161 if ($counter % 1 == 0) {
2162 if (!defined('RESTORE_SILENTLY')) {
2163 echo ".";
2164 if ($counter % 20 == 0) {
2165 echo "<br />";
2168 backup_flush(300);
2175 // process histories
2177 if ($gihcount && $status) {
2178 if (!defined('RESTORE_SILENTLY')) {
2179 echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
2181 $counter = 0;
2182 while ($counter < $gihcount) {
2183 //Fetch recordset_size records in each iteration
2184 $recs = get_records_select("backup_ids","table_name = 'grade_items_history' AND backup_code = '$restore->backup_unique_code'",
2185 "old_id",
2186 "old_id",
2187 $counter,
2188 $recordset_size);
2189 if ($recs) {
2190 foreach ($recs as $rec) {
2191 //Get the full record from backup_ids
2192 $data = backup_getid($restore->backup_unique_code,'grade_items_history',$rec->old_id);
2193 if ($data) {
2194 //Now get completed xmlized object
2195 $info = $data->info;
2196 //traverse_xmlize($info); //Debug
2197 //print_object ($GLOBALS['traverse_array']); //Debug
2198 //$GLOBALS['traverse_array']=""; //Debug
2201 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
2202 if (empty($oldobj->new_id)) {
2203 // if the old object is not being restored, can't restoring its history
2204 $counter++;
2205 continue;
2207 $dbrec->oldid = $oldobj->new_id;
2208 $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
2209 $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
2210 $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2211 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2212 $dbrec->loggeduser = $oldobj->new_id;
2214 $dbrec->courseid = $restore->course_id;
2215 $oldobj = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
2216 $oldobj->categoryid = $category->new_id;
2217 if (empty($oldobj->categoryid)) {
2218 $counter++;
2219 continue; // category not restored
2222 $dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
2223 $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
2224 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
2226 // code from grade_items restore
2227 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
2228 // do not restore if this grade_item is a mod, and
2229 if ($dbrec->itemtype == 'mod') {
2231 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
2232 // module instance not selected when restored using granular
2233 // skip this item
2234 $counter++;
2235 continue;
2238 // iteminstance should point to new mod
2240 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
2241 $dbrec->iteminstance = $mod->new_id;
2243 } else if ($dbrec->itemtype == 'category') {
2244 // the item instance should point to the new grade category
2246 // only proceed if we are restoring all grade items
2247 if ($restoreall) {
2248 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
2249 $dbrec->iteminstance = $category->new_id;
2250 } else {
2251 // otherwise we can safely ignore this grade item and subsequent
2252 // grade_raws, grade_finals etc
2253 continue;
2255 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
2256 if ($restoreall) {
2257 // TODO any special code needed here to restore course item without duplicating it?
2258 // find the course category with depth 1, and course id = current course id
2259 // this would have been already restored
2261 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
2262 $dbrec->iteminstance = $cat->id;
2264 } else {
2265 $counter++;
2266 continue;
2270 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
2271 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
2272 $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
2273 $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
2274 $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
2275 $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
2276 $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
2277 if ($oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
2278 // scaleid is optional
2279 $dbrec->scaleid = $oldobj->new_id;
2281 if ($oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
2282 // outcome is optional
2283 $dbrec->outcomeid = $oldobj->new_id;
2285 $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
2286 $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
2287 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
2288 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
2289 $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
2290 $dbrec->display = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DISPLAY']['0']['#']);
2291 $dbrec->decimals = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DECIMALS']['0']['#']);
2292 $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
2293 $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
2294 $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
2295 $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
2297 insert_record('grade_items_history', $dbrec);
2298 unset($dbrec);
2301 //Increment counters
2302 $counter++;
2303 //Do some output
2304 if ($counter % 1 == 0) {
2305 if (!defined('RESTORE_SILENTLY')) {
2306 echo ".";
2307 if ($counter % 20 == 0) {
2308 echo "<br />";
2311 backup_flush(300);
2318 // process histories
2319 if ($gohcount && $status) {
2320 if (!defined('RESTORE_SILENTLY')) {
2321 echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
2323 $counter = 0;
2324 while ($counter < $gohcount) {
2325 //Fetch recordset_size records in each iteration
2326 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_history' AND backup_code = '$restore->backup_unique_code'",
2327 "old_id",
2328 "old_id",
2329 $counter,
2330 $recordset_size);
2331 if ($recs) {
2332 foreach ($recs as $rec) {
2333 //Get the full record from backup_ids
2334 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_history',$rec->old_id);
2335 if ($data) {
2336 //Now get completed xmlized object
2337 $info = $data->info;
2338 //traverse_xmlize($info); //Debug
2339 //print_object ($GLOBALS['traverse_array']); //Debug
2340 //$GLOBALS['traverse_array']=""; //Debug
2342 $oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
2343 if (empty($oldobj->new_id)) {
2344 // if the old object is not being restored, can't restoring its history
2345 $counter++;
2346 continue;
2348 $dbrec->oldid = $oldobj->new_id;
2349 $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
2350 $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
2351 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2352 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2353 $dbrec->loggeduser = $oldobj->new_id;
2355 $dbrec->courseid = $restore->course_id;
2356 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
2357 $dbrec->fullname= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
2358 $oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
2359 $dbrec->scaleid = $oldobj->new_id;
2360 $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
2362 insert_record('grade_outcomes_history', $dbrec);
2363 unset($dbrec);
2366 //Increment counters
2367 $counter++;
2368 //Do some output
2369 if ($counter % 1 == 0) {
2370 if (!defined('RESTORE_SILENTLY')) {
2371 echo ".";
2372 if ($counter % 20 == 0) {
2373 echo "<br />";
2376 backup_flush(300);
2384 if (!defined('RESTORE_SILENTLY')) {
2385 //End ul
2386 echo '</ul>';
2388 return $status;
2391 //This function creates all the user, user_students, user_teachers
2392 //user_course_creators and user_admins from xml
2393 function restore_create_users($restore,$xml_file) {
2395 global $CFG, $db;
2396 require_once ($CFG->dirroot.'/tag/lib.php');
2398 $status = true;
2399 //Check it exists
2400 if (!file_exists($xml_file)) {
2401 $status = false;
2403 //Get info from xml
2404 if ($status) {
2405 //info will contain the old_id of every user
2406 //in backup_ids->info will be the real info (serialized)
2407 $info = restore_read_xml_users($restore,$xml_file);
2410 //Now, get evey user_id from $info and user data from $backup_ids
2411 //and create the necessary db structures
2413 if (!empty($info->users)) {
2415 /// Grab mnethosts keyed by wwwroot, to map to id
2416 $mnethosts = get_records('mnet_host', '', '',
2417 'wwwroot', 'wwwroot, id');
2419 /// Get languages for quick search later
2420 $languages = get_list_of_languages();
2422 /// Iterate over all users loaded from xml
2423 $counter = 0;
2424 foreach ($info->users as $userid) {
2425 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
2426 $user = $rec->info;
2427 foreach (array_keys(get_object_vars($user)) as $field) {
2428 if (!is_array($user->$field)) {
2429 $user->$field = backup_todb($user->$field);
2430 if (is_null($user->$field)) {
2431 $user->$field = '';
2436 //Now, recode some languages (Moodle 1.5)
2437 if ($user->lang == 'ma_nt') {
2438 $user->lang = 'mi_nt';
2441 //Country list updates - MDL-13060
2442 //Any user whose country code has been deleted or modified needs to be assigned a valid one.
2443 $country_update_map = array(
2444 'ZR' => 'CD',
2445 'TP' => 'TL',
2446 'FX' => 'FR',
2447 'KO' => 'RS',
2448 'CS' => 'RS',
2449 'WA' => 'GB');
2450 if (array_key_exists($user->country, $country_update_map)) {
2451 $user->country = $country_update_map[$user->country];
2454 //If language does not exist here - use site default
2455 if (!array_key_exists($user->lang, $languages)) {
2456 $user->lang = $CFG->lang;
2459 //Check if it's admin and coursecreator
2460 $is_admin = !empty($user->roles['admin']);
2461 $is_coursecreator = !empty($user->roles['coursecreator']);
2463 //Check if it's teacher and student
2464 $is_teacher = !empty($user->roles['teacher']);
2465 $is_student = !empty($user->roles['student']);
2467 //Check if it's needed
2468 $is_needed = !empty($user->roles['needed']);
2470 //Calculate if it is a course user
2471 //Has role teacher or student or needed
2472 $is_course_user = ($is_teacher or $is_student or $is_needed);
2474 //Calculate mnethostid
2475 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
2476 $user->mnethostid = $CFG->mnet_localhost_id;
2477 } else {
2478 // fast url-to-id lookups
2479 if (isset($mnethosts[$user->mnethosturl])) {
2480 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
2481 } else {
2482 // should not happen, as we check in restore_chech.php
2483 // but handle the error if it does
2484 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
2487 unset($user->mnethosturl);
2489 //To store user->id along all the iteration
2490 $newid=null;
2491 //check if it exists (by username) and get its id
2492 $user_exists = true;
2493 $user_data = get_record("user","username",addslashes($user->username),
2494 'mnethostid', $user->mnethostid);
2495 if (!$user_data) {
2496 $user_exists = false;
2497 } else {
2498 $newid = $user_data->id;
2501 //Flags to see what parts are we going to restore
2502 $create_user = true;
2503 $create_roles = true;
2504 $create_custom_profile_fields = true;
2505 $create_tags = true;
2506 $create_preferences = true;
2508 //If we are restoring course users and it isn't a course user
2509 if ($restore->users == 1 and !$is_course_user) {
2510 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
2511 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
2512 $create_user = false;
2513 $create_roles = false;
2514 $create_custom_profile_fields = false;
2515 $create_tags = false;
2516 $create_preferences = false;
2519 if ($user_exists and $create_user) {
2520 //If user exists mark its newid in backup_ids (the same than old)
2521 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
2522 $create_user = false;
2523 $create_custom_profile_fields = false;
2524 $create_tags = false;
2525 $create_preferences = false;
2528 //Here, if create_user, do it
2529 if ($create_user) {
2530 //Unset the id because it's going to be inserted with a new one
2531 unset ($user->id);
2532 // relink the descriptions
2533 $user->description = stripslashes($user->description);
2535 /// Disable pictures based on global setting or existing empty value (old backups can contain wrong empties)
2536 if (!empty($CFG->disableuserimages) || empty($user->picture)) {
2537 $user->picture = 0;
2540 //We need to analyse the AUTH field to recode it:
2541 // - if the field isn't set, we are in a pre 1.4 backup and we'll
2542 // use manual
2544 if (empty($user->auth)) {
2545 if ($CFG->registerauth == 'email') {
2546 $user->auth = 'email';
2547 } else {
2548 $user->auth = 'manual';
2552 //We need to process the POLICYAGREED field to recalculate it:
2553 // - if the destination site is different (by wwwroot) reset it.
2554 // - if the destination site is the same (by wwwroot), leave it unmodified
2556 if ($restore->original_wwwroot != $CFG->wwwroot) {
2557 $user->policyagreed = 0;
2558 } else {
2559 //Nothing to do, we are in the same server
2562 //Check if the theme exists in destination server
2563 $themes = get_list_of_themes();
2564 if (!in_array($user->theme, $themes)) {
2565 $user->theme = '';
2568 //We are going to create the user
2569 //The structure is exactly as we need
2571 $newid = insert_record ("user", addslashes_recursive($user));
2572 //Put the new id
2573 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
2576 ///TODO: This seccion is to support pre 1.7 course backups, using old roles
2577 /// teacher, coursecreator, student.... providing a basic mapping to new ones.
2578 /// Someday we'll drop support for them and this section will be safely deleted (2.0?)
2579 //Here, if create_roles, do it as necessary
2580 if ($create_roles) {
2581 //Get the newid and current info from backup_ids
2582 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2583 $newid = $data->new_id;
2584 $currinfo = $data->info.",";
2586 //Now, depending of the role, create records in user_studentes and user_teacher
2587 //and/or mark it in backup_ids
2589 if ($is_admin) {
2590 //If the record (user_admins) doesn't exists
2591 //Only put status in backup_ids
2592 $currinfo = $currinfo."admin,";
2593 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2595 if ($is_coursecreator) {
2596 //If the record (user_coursecreators) doesn't exists
2597 //Only put status in backup_ids
2598 $currinfo = $currinfo."coursecreator,";
2599 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2601 if ($is_needed) {
2602 //Only put status in backup_ids
2603 $currinfo = $currinfo."needed,";
2604 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2606 if ($is_teacher) {
2607 //If the record (teacher) doesn't exists
2608 //Put status in backup_ids
2609 $currinfo = $currinfo."teacher,";
2610 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2611 //Set course and user
2612 $user->roles['teacher']->course = $restore->course_id;
2613 $user->roles['teacher']->userid = $newid;
2615 //Need to analyse the enrol field
2616 // - if it isn't set, set it to $CFG->enrol
2617 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2618 // - if we are in the same server (by wwwroot), maintain it unmodified.
2619 if (empty($user->roles['teacher']->enrol)) {
2620 $user->roles['teacher']->enrol = $CFG->enrol;
2621 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2622 $user->roles['teacher']->enrol = $CFG->enrol;
2623 } else {
2624 //Nothing to do. Leave it unmodified
2627 $rolesmapping = $restore->rolesmapping;
2628 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2629 if ($user->roles['teacher']->editall) {
2630 role_assign($rolesmapping['defaultteacheredit'],
2631 $newid,
2633 $context->id,
2634 $user->roles['teacher']->timestart,
2635 $user->roles['teacher']->timeend,
2637 $user->roles['teacher']->enrol);
2639 // editting teacher
2640 } else {
2641 // non editting teacher
2642 role_assign($rolesmapping['defaultteacher'],
2643 $newid,
2645 $context->id,
2646 $user->roles['teacher']->timestart,
2647 $user->roles['teacher']->timeend,
2649 $user->roles['teacher']->enrol);
2652 if ($is_student) {
2654 //Put status in backup_ids
2655 $currinfo = $currinfo."student,";
2656 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2657 //Set course and user
2658 $user->roles['student']->course = $restore->course_id;
2659 $user->roles['student']->userid = $newid;
2661 //Need to analyse the enrol field
2662 // - if it isn't set, set it to $CFG->enrol
2663 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2664 // - if we are in the same server (by wwwroot), maintain it unmodified.
2665 if (empty($user->roles['student']->enrol)) {
2666 $user->roles['student']->enrol = $CFG->enrol;
2667 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2668 $user->roles['student']->enrol = $CFG->enrol;
2669 } else {
2670 //Nothing to do. Leave it unmodified
2672 $rolesmapping = $restore->rolesmapping;
2673 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2675 role_assign($rolesmapping['defaultstudent'],
2676 $newid,
2678 $context->id,
2679 $user->roles['student']->timestart,
2680 $user->roles['student']->timeend,
2682 $user->roles['student']->enrol);
2685 if (!$is_course_user) {
2686 //If the record (user) doesn't exists
2687 if (!record_exists("user","id",$newid)) {
2688 //Put status in backup_ids
2689 $currinfo = $currinfo."user,";
2690 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2695 /// Here, if create_custom_profile_fields, do it as necessary
2696 if ($create_custom_profile_fields) {
2697 if (isset($user->user_custom_profile_fields)) {
2698 foreach($user->user_custom_profile_fields as $udata) {
2699 /// If the profile field has data and the profile shortname-datatype is defined in server
2700 if ($udata->field_data) {
2701 if ($field = get_record('user_info_field', 'shortname', $udata->field_name, 'datatype', $udata->field_type)) {
2702 /// Insert the user_custom_profile_field
2703 $rec = new object();
2704 $rec->userid = $newid;
2705 $rec->fieldid = $field->id;
2706 $rec->data = $udata->field_data;
2707 insert_record('user_info_data', $rec);
2714 /// Here, if create_tags, do it as necessary
2715 if ($create_tags) {
2716 /// if tags are enabled and there are user tags
2717 if (!empty($CFG->usetags) && isset($user->user_tags)) {
2718 $tags = array();
2719 foreach($user->user_tags as $user_tag) {
2720 $tags[] = $user_tag->rawname;
2722 tag_set('user', $newid, $tags);
2726 //Here, if create_preferences, do it as necessary
2727 if ($create_preferences) {
2728 if (isset($user->user_preferences)) {
2729 foreach($user->user_preferences as $user_preference) {
2730 //We check if that user_preference exists in DB
2731 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
2732 //Prepare the record and insert it
2733 $user_preference->userid = $newid;
2734 $status = insert_record("user_preferences",$user_preference);
2740 //Do some output
2741 $counter++;
2742 if ($counter % 10 == 0) {
2743 if (!defined('RESTORE_SILENTLY')) {
2744 echo ".";
2745 if ($counter % 200 == 0) {
2746 echo "<br />";
2749 backup_flush(300);
2751 } /// End of loop over all the users loaded from xml
2754 return $status;
2757 //This function creates all the structures messages and contacts
2758 function restore_create_messages($restore,$xml_file) {
2760 global $CFG;
2762 $status = true;
2763 //Check it exists
2764 if (!file_exists($xml_file)) {
2765 $status = false;
2767 //Get info from xml
2768 if ($status) {
2769 //info will contain the id and name of every table
2770 //(message, message_read and message_contacts)
2771 //in backup_ids->info will be the real info (serialized)
2772 $info = restore_read_xml_messages($restore,$xml_file);
2774 //If we have info, then process messages & contacts
2775 if ($info > 0) {
2776 //Count how many we have
2777 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
2778 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
2779 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
2780 if ($unreadcount || $readcount || $contactcount) {
2781 //Start ul
2782 if (!defined('RESTORE_SILENTLY')) {
2783 echo '<ul>';
2785 //Number of records to get in every chunk
2786 $recordset_size = 4;
2788 //Process unread
2789 if ($unreadcount) {
2790 if (!defined('RESTORE_SILENTLY')) {
2791 echo '<li>'.get_string('unreadmessages','message').'</li>';
2793 $counter = 0;
2794 while ($counter < $unreadcount) {
2795 //Fetch recordset_size records in each iteration
2796 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2797 if ($recs) {
2798 foreach ($recs as $rec) {
2799 //Get the full record from backup_ids
2800 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
2801 if ($data) {
2802 //Now get completed xmlized object
2803 $info = $data->info;
2804 //traverse_xmlize($info); //Debug
2805 //print_object ($GLOBALS['traverse_array']); //Debug
2806 //$GLOBALS['traverse_array']=""; //Debug
2807 //Now build the MESSAGE record structure
2808 $dbrec = new object();
2809 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2810 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2811 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2812 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2813 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2814 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2815 //We have to recode the useridfrom field
2816 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2817 if ($user) {
2818 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2819 $dbrec->useridfrom = $user->new_id;
2821 //We have to recode the useridto field
2822 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2823 if ($user) {
2824 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2825 $dbrec->useridto = $user->new_id;
2827 //Check if the record doesn't exist in DB!
2828 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
2829 'useridto', $dbrec->useridto,
2830 'timecreated',$dbrec->timecreated);
2831 if (!$exist) {
2832 //Not exist. Insert
2833 $status = insert_record('message',$dbrec);
2834 } else {
2835 //Duplicate. Do nothing
2838 //Do some output
2839 $counter++;
2840 if ($counter % 10 == 0) {
2841 if (!defined('RESTORE_SILENTLY')) {
2842 echo ".";
2843 if ($counter % 200 == 0) {
2844 echo "<br />";
2847 backup_flush(300);
2854 //Process read
2855 if ($readcount) {
2856 if (!defined('RESTORE_SILENTLY')) {
2857 echo '<li>'.get_string('readmessages','message').'</li>';
2859 $counter = 0;
2860 while ($counter < $readcount) {
2861 //Fetch recordset_size records in each iteration
2862 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2863 if ($recs) {
2864 foreach ($recs as $rec) {
2865 //Get the full record from backup_ids
2866 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
2867 if ($data) {
2868 //Now get completed xmlized object
2869 $info = $data->info;
2870 //traverse_xmlize($info); //Debug
2871 //print_object ($GLOBALS['traverse_array']); //Debug
2872 //$GLOBALS['traverse_array']=""; //Debug
2873 //Now build the MESSAGE_READ record structure
2874 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2875 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2876 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2877 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2878 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2879 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2880 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2881 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2882 //We have to recode the useridfrom field
2883 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2884 if ($user) {
2885 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2886 $dbrec->useridfrom = $user->new_id;
2888 //We have to recode the useridto field
2889 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2890 if ($user) {
2891 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2892 $dbrec->useridto = $user->new_id;
2894 //Check if the record doesn't exist in DB!
2895 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
2896 'useridto', $dbrec->useridto,
2897 'timecreated',$dbrec->timecreated);
2898 if (!$exist) {
2899 //Not exist. Insert
2900 $status = insert_record('message_read',$dbrec);
2901 } else {
2902 //Duplicate. Do nothing
2905 //Do some output
2906 $counter++;
2907 if ($counter % 10 == 0) {
2908 if (!defined('RESTORE_SILENTLY')) {
2909 echo ".";
2910 if ($counter % 200 == 0) {
2911 echo "<br />";
2914 backup_flush(300);
2921 //Process contacts
2922 if ($contactcount) {
2923 if (!defined('RESTORE_SILENTLY')) {
2924 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2926 $counter = 0;
2927 while ($counter < $contactcount) {
2928 //Fetch recordset_size records in each iteration
2929 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2930 if ($recs) {
2931 foreach ($recs as $rec) {
2932 //Get the full record from backup_ids
2933 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
2934 if ($data) {
2935 //Now get completed xmlized object
2936 $info = $data->info;
2937 //traverse_xmlize($info); //Debug
2938 //print_object ($GLOBALS['traverse_array']); //Debug
2939 //$GLOBALS['traverse_array']=""; //Debug
2940 //Now build the MESSAGE_CONTACTS record structure
2941 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2942 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2943 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2944 //We have to recode the userid field
2945 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
2946 if ($user) {
2947 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2948 $dbrec->userid = $user->new_id;
2950 //We have to recode the contactid field
2951 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
2952 if ($user) {
2953 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2954 $dbrec->contactid = $user->new_id;
2956 //Check if the record doesn't exist in DB!
2957 $exist = get_record('message_contacts','userid',$dbrec->userid,
2958 'contactid', $dbrec->contactid);
2959 if (!$exist) {
2960 //Not exist. Insert
2961 $status = insert_record('message_contacts',$dbrec);
2962 } else {
2963 //Duplicate. Do nothing
2966 //Do some output
2967 $counter++;
2968 if ($counter % 10 == 0) {
2969 if (!defined('RESTORE_SILENTLY')) {
2970 echo ".";
2971 if ($counter % 200 == 0) {
2972 echo "<br />";
2975 backup_flush(300);
2981 if (!defined('RESTORE_SILENTLY')) {
2982 //End ul
2983 echo '</ul>';
2989 return $status;
2992 //This function creates all the structures for blogs and blog tags
2993 function restore_create_blogs($restore,$xml_file) {
2995 global $CFG;
2997 $status = true;
2998 //Check it exists
2999 if (!file_exists($xml_file)) {
3000 $status = false;
3002 //Get info from xml
3003 if ($status) {
3004 //info will contain the number of blogs in the backup file
3005 //in backup_ids->info will be the real info (serialized)
3006 $info = restore_read_xml_blogs($restore,$xml_file);
3008 //If we have info, then process blogs & blog_tags
3009 if ($info > 0) {
3010 //Count how many we have
3011 $blogcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'blog');
3012 if ($blogcount) {
3013 //Number of records to get in every chunk
3014 $recordset_size = 4;
3016 //Process blog
3017 if ($blogcount) {
3018 $counter = 0;
3019 while ($counter < $blogcount) {
3020 //Fetch recordset_size records in each iteration
3021 $recs = get_records_select("backup_ids","table_name = 'blog' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3022 if ($recs) {
3023 foreach ($recs as $rec) {
3024 //Get the full record from backup_ids
3025 $data = backup_getid($restore->backup_unique_code,"blog",$rec->old_id);
3026 if ($data) {
3027 //Now get completed xmlized object
3028 $info = $data->info;
3029 //traverse_xmlize($info); //Debug
3030 //print_object ($GLOBALS['traverse_array']); //Debug
3031 //$GLOBALS['traverse_array']=""; //Debug
3032 //Now build the BLOG record structure
3033 $dbrec = new object();
3034 $dbrec->module = backup_todb($info['BLOG']['#']['MODULE']['0']['#']);
3035 $dbrec->userid = backup_todb($info['BLOG']['#']['USERID']['0']['#']);
3036 $dbrec->courseid = backup_todb($info['BLOG']['#']['COURSEID']['0']['#']);
3037 $dbrec->groupid = backup_todb($info['BLOG']['#']['GROUPID']['0']['#']);
3038 $dbrec->moduleid = backup_todb($info['BLOG']['#']['MODULEID']['0']['#']);
3039 $dbrec->coursemoduleid = backup_todb($info['BLOG']['#']['COURSEMODULEID']['0']['#']);
3040 $dbrec->subject = backup_todb($info['BLOG']['#']['SUBJECT']['0']['#']);
3041 $dbrec->summary = backup_todb($info['BLOG']['#']['SUMMARY']['0']['#']);
3042 $dbrec->content = backup_todb($info['BLOG']['#']['CONTENT']['0']['#']);
3043 $dbrec->uniquehash = backup_todb($info['BLOG']['#']['UNIQUEHASH']['0']['#']);
3044 $dbrec->rating = backup_todb($info['BLOG']['#']['RATING']['0']['#']);
3045 $dbrec->format = backup_todb($info['BLOG']['#']['FORMAT']['0']['#']);
3046 $dbrec->attachment = backup_todb($info['BLOG']['#']['ATTACHMENT']['0']['#']);
3047 $dbrec->publishstate = backup_todb($info['BLOG']['#']['PUBLISHSTATE']['0']['#']);
3048 $dbrec->lastmodified = backup_todb($info['BLOG']['#']['LASTMODIFIED']['0']['#']);
3049 $dbrec->created = backup_todb($info['BLOG']['#']['CREATED']['0']['#']);
3050 $dbrec->usermodified = backup_todb($info['BLOG']['#']['USERMODIFIED']['0']['#']);
3052 //We have to recode the userid field
3053 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
3054 if ($user) {
3055 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
3056 $dbrec->userid = $user->new_id;
3059 //Check if the record doesn't exist in DB!
3060 $exist = get_record('post','userid', $dbrec->userid,
3061 'subject', $dbrec->subject,
3062 'created', $dbrec->created);
3063 $newblogid = 0;
3064 if (!$exist) {
3065 //Not exist. Insert
3066 $newblogid = insert_record('post',$dbrec);
3069 //Going to restore related tags. Check they are enabled and we have inserted a blog
3070 if ($CFG->usetags && $newblogid) {
3071 //Look for tags in this blog
3072 if (isset($info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'])) {
3073 $tagsarr = $info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'];
3074 //Iterate over tags
3075 $tags = array();
3076 for($i = 0; $i < sizeof($tagsarr); $i++) {
3077 $tag_info = $tagsarr[$i];
3078 ///traverse_xmlize($tag_info); //Debug
3079 ///print_object ($GLOBALS['traverse_array']); //Debug
3080 ///$GLOBALS['traverse_array']=""; //Debug
3082 $name = backup_todb($tag_info['#']['NAME']['0']['#']);
3083 $rawname = backup_todb($tag_info['#']['RAWNAME']['0']['#']);
3085 $tags[] = $rawname; //Rawname is all we need
3087 tag_set('post', $newblogid, $tags); //Add all the tags in one API call
3091 //Do some output
3092 $counter++;
3093 if ($counter % 10 == 0) {
3094 if (!defined('RESTORE_SILENTLY')) {
3095 echo ".";
3096 if ($counter % 200 == 0) {
3097 echo "<br />";
3100 backup_flush(300);
3110 return $status;
3113 //This function creates all the categories and questions
3114 //from xml
3115 function restore_create_questions($restore,$xml_file) {
3117 global $CFG, $db;
3119 $status = true;
3120 //Check it exists
3121 if (!file_exists($xml_file)) {
3122 $status = false;
3124 //Get info from xml
3125 if ($status) {
3126 //info will contain the old_id of every category
3127 //in backup_ids->info will be the real info (serialized)
3128 $info = restore_read_xml_questions($restore,$xml_file);
3130 //Now, if we have anything in info, we have to restore that
3131 //categories/questions
3132 if ($info) {
3133 if ($info !== true) {
3134 $status = $status && restore_question_categories($info, $restore);
3136 } else {
3137 $status = false;
3139 return $status;
3142 //This function creates all the scales
3143 function restore_create_scales($restore,$xml_file) {
3145 global $CFG, $db;
3147 $status = true;
3148 //Check it exists
3149 if (!file_exists($xml_file)) {
3150 $status = false;
3152 //Get info from xml
3153 if ($status) {
3154 //scales will contain the old_id of every scale
3155 //in backup_ids->info will be the real info (serialized)
3156 $scales = restore_read_xml_scales($restore,$xml_file);
3158 //Now, if we have anything in scales, we have to restore that
3159 //scales
3160 if ($scales) {
3161 //Get admin->id for later use
3162 $admin = get_admin();
3163 $adminid = $admin->id;
3164 if ($scales !== true) {
3165 //Iterate over each scale
3166 foreach ($scales as $scale) {
3167 //Get record from backup_ids
3168 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
3169 //Init variables
3170 $create_scale = false;
3172 if ($data) {
3173 //Now get completed xmlized object
3174 $info = $data->info;
3175 //traverse_xmlize($info); //Debug
3176 //print_object ($GLOBALS['traverse_array']); //Debug
3177 //$GLOBALS['traverse_array']=""; //Debug
3179 //Now build the SCALE record structure
3180 $sca = new object();
3181 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
3182 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
3183 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
3184 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
3185 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
3186 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
3188 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
3189 //or in restore->course_id course (Personal scale)
3190 if ($sca->courseid == 0) {
3191 $course_to_search = 0;
3192 } else {
3193 $course_to_search = $restore->course_id;
3196 // scale is not course unique, use get_record_sql to suppress warning
3198 $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale
3199 WHERE scale = '$sca->scale'
3200 AND courseid = $course_to_search", true);
3202 //If it doesn't exist, create
3203 if (!$sca_db) {
3204 $create_scale = true;
3206 //If we must create the scale
3207 if ($create_scale) {
3208 //Me must recode the courseid if it's <> 0 (common scale)
3209 if ($sca->courseid != 0) {
3210 $sca->courseid = $restore->course_id;
3212 //We must recode the userid
3213 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
3214 if ($user) {
3215 $sca->userid = $user->new_id;
3216 } else {
3217 //Assign it to admin
3218 $sca->userid = $adminid;
3220 //The structure is equal to the db, so insert the scale
3221 $newid = insert_record ("scale",$sca);
3222 } else {
3223 //get current scale id
3224 $newid = $sca_db->id;
3226 if ($newid) {
3227 //We have the newid, update backup_ids
3228 backup_putid($restore->backup_unique_code,"scale",
3229 $scale->id, $newid);
3234 } else {
3235 $status = false;
3237 return $status;
3241 * Recode group ID field, and set group ID based on restore options.
3242 * @return object Group object with new_id field.
3244 function restore_group_getid($restore, $groupid) {
3245 //We have to recode the groupid field
3246 $group = backup_getid($restore->backup_unique_code, 'groups', $groupid);
3248 if ($restore->groups == RESTORE_GROUPS_NONE or $restore->groups == RESTORE_GROUPINGS_ONLY) {
3249 $group->new_id = 0;
3251 return $group;
3255 * Recode grouping ID field, and set grouping ID based on restore options.
3256 * @return object Group object with new_id field.
3258 function restore_grouping_getid($restore, $groupingid) {
3259 //We have to recode the groupid field
3260 $grouping = backup_getid($restore->backup_unique_code, 'groupings', $groupingid);
3262 if ($restore->groups != RESTORE_GROUPS_GROUPINGS and $restore->groups != RESTORE_GROUPINGS_ONLY) {
3263 $grouping->new_id = 0;
3265 return $grouping;
3268 //This function creates all the groups
3269 function restore_create_groups($restore,$xml_file) {
3271 global $CFG;
3273 //Check it exists
3274 if (!file_exists($xml_file)) {
3275 return false;
3277 //Get info from xml
3278 if (!$groups = restore_read_xml_groups($restore,$xml_file)) {
3279 //groups will contain the old_id of every group
3280 //in backup_ids->info will be the real info (serialized)
3281 return false;
3283 } else if ($groups === true) {
3284 return true;
3287 $status = true;
3289 //Iterate over each group
3290 foreach ($groups as $group) {
3291 //Get record from backup_ids
3292 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
3294 if ($data) {
3295 //Now get completed xmlized object
3296 $info = $data->info;
3297 //traverse_xmlize($info); //Debug
3298 //print_object ($GLOBALS['traverse_array']); //Debug
3299 //$GLOBALS['traverse_array']=""; //Debug
3300 //Now build the GROUP record structure
3301 $gro = new Object();
3302 $gro->courseid = $restore->course_id;
3303 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
3304 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
3305 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
3306 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
3307 } else {
3308 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
3310 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
3311 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
3312 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
3313 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
3315 //Now search if that group exists (by name and description field) in
3316 //restore->course_id course
3317 //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
3318 $description_clause = '';
3319 if (!empty($gro->description)) { /// Only for groups having a description
3320 $literal_description = "'" . $gro->description . "'";
3321 $description_clause = " AND " .
3322 sql_compare_text('description') . " = " .
3323 sql_compare_text($literal_description);
3325 if (!$gro_db = get_record_sql("SELECT *
3326 FROM {$CFG->prefix}groups
3327 WHERE courseid = $restore->course_id AND
3328 name = '{$gro->name}'" . $description_clause, true)) {
3329 //If it doesn't exist, create
3330 $newid = insert_record('groups', $gro);
3332 } else {
3333 //get current group id
3334 $newid = $gro_db->id;
3337 if ($newid) {
3338 //We have the newid, update backup_ids
3339 backup_putid($restore->backup_unique_code,"groups", $group->id, $newid);
3340 } else {
3342 $status = false;
3343 continue;
3346 //Now restore members in the groups_members, only if
3347 //users are included
3348 if ($restore->users != 2) {
3349 if (!restore_create_groups_members($newid,$info,$restore)) {
3350 $status = false;
3356 //Now, restore group_files
3357 if ($status) {
3358 $status = restore_group_files($restore);
3361 return $status;
3364 //This function restores the groups_members
3365 function restore_create_groups_members($group_id,$info,$restore) {
3367 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
3368 //OK, some groups have no members.
3369 return true;
3371 //Get the members array
3372 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
3374 $status = true;
3376 //Iterate over members
3377 for($i = 0; $i < sizeof($members); $i++) {
3378 $mem_info = $members[$i];
3379 //traverse_xmlize($mem_info); //Debug
3380 //print_object ($GLOBALS['traverse_array']); //Debug
3381 //$GLOBALS['traverse_array']=""; //Debug
3383 //Now, build the GROUPS_MEMBERS record structure
3384 $group_member = new Object();
3385 $group_member->groupid = $group_id;
3386 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
3387 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
3389 $newid = false;
3391 //We have to recode the userid field
3392 if (!$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid)) {
3393 $status = false;
3394 continue;
3397 $group_member->userid = $user->new_id;
3399 //The structure is equal to the db, so insert the groups_members
3400 if (!insert_record ("groups_members", $group_member)) {
3401 $status = false;
3402 continue;
3405 //Do some output
3406 if (($i+1) % 50 == 0) {
3407 if (!defined('RESTORE_SILENTLY')) {
3408 echo ".";
3409 if (($i+1) % 1000 == 0) {
3410 echo "<br />";
3413 backup_flush(300);
3417 return $status;
3420 //This function creates all the groupings
3421 function restore_create_groupings($restore,$xml_file) {
3423 //Check it exists
3424 if (!file_exists($xml_file)) {
3425 return false;
3427 //Get info from xml
3428 if (!$groupings = restore_read_xml_groupings($restore,$xml_file)) {
3429 return false;
3431 } else if ($groupings === true) {
3432 return true;
3435 $status = true;
3437 //Iterate over each group
3438 foreach ($groupings as $grouping) {
3439 if ($data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id)) {
3440 //Now get completed xmlized object
3441 $info = $data->info;
3442 //Now build the GROUPING record structure
3443 $gro = new Object();
3444 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
3445 $gro->courseid = $restore->course_id;
3446 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
3447 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
3448 $gro->configdata = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
3449 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
3451 //Now search if that group exists (by name and description field) in
3452 if ($gro_db = get_record('groupings', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
3453 //get current group id
3454 $newid = $gro_db->id;
3456 } else {
3457 //The structure is equal to the db, so insert the grouping
3458 if (!$newid = insert_record('groupings', $gro)) {
3459 $status = false;
3460 continue;
3464 //We have the newid, update backup_ids
3465 backup_putid($restore->backup_unique_code,"groupings",
3466 $grouping->id, $newid);
3471 // now fix the defaultgroupingid in course
3472 $course = get_record('course', 'id', $restore->course_id);
3473 if ($course->defaultgroupingid) {
3474 if ($grouping = restore_grouping_getid($restore, $course->defaultgroupingid)) {
3475 set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
3476 } else {
3477 set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
3481 return $status;
3484 //This function creates all the groupingsgroups
3485 function restore_create_groupings_groups($restore,$xml_file) {
3487 //Check it exists
3488 if (!file_exists($xml_file)) {
3489 return false;
3491 //Get info from xml
3492 if (!$groupingsgroups = restore_read_xml_groupings_groups($restore,$xml_file)) {
3493 return false;
3495 } else if ($groupingsgroups === true) {
3496 return true;
3499 $status = true;
3501 //Iterate over each group
3502 foreach ($groupingsgroups as $groupinggroup) {
3503 if ($data = backup_getid($restore->backup_unique_code,"groupingsgroups",$groupinggroup->id)) {
3504 //Now get completed xmlized object
3505 $info = $data->info;
3506 //Now build the GROUPING record structure
3507 $gro_member = new Object();
3508 $gro_member->groupingid = backup_todb($info['GROUPINGGROUP']['#']['GROUPINGID']['0']['#']);
3509 $gro_member->groupid = backup_todb($info['GROUPINGGROUP']['#']['GROUPID']['0']['#']);
3510 $gro_member->timeadded = backup_todb($info['GROUPINGGROUP']['#']['TIMEADDED']['0']['#']);
3512 if (!$grouping = backup_getid($restore->backup_unique_code,"groupings",$gro_member->groupingid)) {
3513 $status = false;
3514 continue;
3517 if (!$group = backup_getid($restore->backup_unique_code,"groups",$gro_member->groupid)) {
3518 $status = false;
3519 continue;
3522 $gro_member->groupid = $group->new_id;
3523 $gro_member->groupingid = $grouping->new_id;
3524 if (!get_record('groupings_groups', 'groupid', $gro_member->groupid, 'groupingid', $gro_member->groupingid)) {
3525 if (!insert_record('groupings_groups', $gro_member)) {
3526 $status = false;
3532 return $status;
3535 //This function creates all the course events
3536 function restore_create_events($restore,$xml_file) {
3538 global $CFG, $db;
3540 $status = true;
3541 //Check it exists
3542 if (!file_exists($xml_file)) {
3543 $status = false;
3545 //Get info from xml
3546 if ($status) {
3547 //events will contain the old_id of every event
3548 //in backup_ids->info will be the real info (serialized)
3549 $events = restore_read_xml_events($restore,$xml_file);
3552 //Get admin->id for later use
3553 $admin = get_admin();
3554 $adminid = $admin->id;
3556 //Now, if we have anything in events, we have to restore that
3557 //events
3558 if ($events) {
3559 if ($events !== true) {
3560 //Iterate over each event
3561 foreach ($events as $event) {
3562 //Get record from backup_ids
3563 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
3564 //Init variables
3565 $create_event = false;
3567 if ($data) {
3568 //Now get completed xmlized object
3569 $info = $data->info;
3570 //traverse_xmlize($info); //Debug
3571 //print_object ($GLOBALS['traverse_array']); //Debug
3572 //$GLOBALS['traverse_array']=""; //Debug
3574 //if necessary, write to restorelog and adjust date/time fields
3575 if ($restore->course_startdateoffset) {
3576 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
3579 //Now build the EVENT record structure
3580 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
3581 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
3582 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
3583 $eve->courseid = $restore->course_id;
3584 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
3585 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
3586 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
3587 $eve->modulename = "";
3588 if (!empty($info['EVENT']['#']['MODULENAME'])) {
3589 $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
3591 $eve->instance = 0;
3592 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
3593 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
3594 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
3595 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
3596 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
3598 //Now search if that event exists (by name, description, timestart fields) in
3599 //restore->course_id course
3600 $eve_db = get_record_select("event",
3601 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
3602 //If it doesn't exist, create
3603 if (!$eve_db) {
3604 $create_event = true;
3606 //If we must create the event
3607 if ($create_event) {
3609 //We must recode the userid
3610 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
3611 if ($user) {
3612 $eve->userid = $user->new_id;
3613 } else {
3614 //Assign it to admin
3615 $eve->userid = $adminid;
3618 //We have to recode the groupid field
3619 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
3620 if ($group) {
3621 $eve->groupid = $group->new_id;
3622 } else {
3623 //Assign it to group 0
3624 $eve->groupid = 0;
3627 //The structure is equal to the db, so insert the event
3628 $newid = insert_record ("event",$eve);
3630 //We must recode the repeatid if the event has it
3631 //The repeatid now refers to the id of the original event. (see Bug#5956)
3632 if ($newid && !empty($eve->repeatid)) {
3633 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
3634 if ($repeat_rec) { //Exists, so use it...
3635 $eve->repeatid = $repeat_rec->new_id;
3636 } else { //Doesn't exists, calculate the next and save it
3637 $oldrepeatid = $eve->repeatid;
3638 $eve->repeatid = $newid;
3639 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
3641 $eve->id = $newid;
3642 // update the record to contain the correct repeatid
3643 update_record('event',$eve);
3645 } else {
3646 //get current event id
3647 $newid = $eve_db->id;
3649 if ($newid) {
3650 //We have the newid, update backup_ids
3651 backup_putid($restore->backup_unique_code,"event",
3652 $event->id, $newid);
3657 } else {
3658 $status = false;
3660 return $status;
3663 //This function decode things to make restore multi-site fully functional
3664 //It does this conversions:
3665 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
3666 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
3668 //Note: Inter-activities linking is being implemented as a final
3669 //step in the restore execution, because we need to have it
3670 //finished to know all the oldid, newid equivaleces
3671 function restore_decode_absolute_links($content) {
3673 global $CFG,$restore;
3675 /// MDL-14072: Prevent NULLs, empties and numbers to be processed by the
3676 /// heavy interlinking. Just a few cpu cycles saved.
3677 if ($content === NULL) {
3678 return NULL;
3679 } else if ($content === '') {
3680 return '';
3681 } else if (is_numeric($content)) {
3682 return $content;
3685 //Now decode wwwroot and file.php calls
3686 $search = array ("$@FILEPHP@$");
3688 //Check for the status of the slasharguments config variable
3689 $slash = $CFG->slasharguments;
3691 //Build the replace string as needed
3692 if ($slash == 1) {
3693 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
3694 } else {
3695 $replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
3698 $result = str_replace($search,$replace,$content);
3700 if ($result != $content && debugging()) { //Debug
3701 if (!defined('RESTORE_SILENTLY')) {
3702 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
3704 } //Debug
3706 return $result;
3709 //This function restores the userfiles from the temp (user_files) directory to the
3710 //dataroot/users directory
3711 function restore_user_files($restore) {
3713 global $CFG;
3715 $status = true;
3717 $counter = 0;
3719 // 'users' is the old users folder, 'user' is the new one, with a new hierarchy. Detect which one is here and treat accordingly
3720 //in CFG->dataroot
3721 $dest_dir = $CFG->dataroot."/user";
3722 $status = check_dir_exists($dest_dir,true);
3724 //Now, we iterate over "user_files" records to check if that user dir must be
3725 //copied (and renamed) to the "users" dir.
3726 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
3728 //Check if directory exists
3729 $userlist = array();
3731 if (is_dir($rootdir) && ($list = list_directories ($rootdir))) {
3732 $counter = 0;
3733 foreach ($list as $dir) {
3734 // If there are directories in this folder, we are in the new user hierarchy
3735 if ($newlist = list_directories("$rootdir/$dir")) {
3736 foreach ($newlist as $olduserid) {
3737 $userlist[$olduserid] = "$rootdir/$dir/$olduserid";
3739 } else {
3740 $userlist[$dir] = "$rootdir/$dir";
3744 foreach ($userlist as $olduserid => $backup_location) {
3745 //Look for dir like username in backup_ids
3746 //If that user exists in backup_ids
3747 if ($user = backup_getid($restore->backup_unique_code,"user",$olduserid)) {
3748 //Only if user has been created now or if it existed previously, but he hasn't got an image (see bug 1123)
3749 $newuserdir = make_user_directory($user->new_id, true); // Doesn't create the folder, just returns the location
3751 // restore images if new user or image does not exist yet
3752 if (!empty($user->new) or !check_dir_exists($newuserdir)) {
3753 if (make_user_directory($user->new_id)) { // Creates the folder
3754 $status = backup_copy_file($backup_location, $newuserdir, true);
3755 $counter ++;
3757 //Do some output
3758 if ($counter % 2 == 0) {
3759 if (!defined('RESTORE_SILENTLY')) {
3760 echo ".";
3761 if ($counter % 40 == 0) {
3762 echo "<br />";
3765 backup_flush(300);
3771 //If status is ok and whe have dirs created, returns counter to inform
3772 if ($status and $counter) {
3773 return $counter;
3774 } else {
3775 return $status;
3779 //This function restores the groupfiles from the temp (group_files) directory to the
3780 //dataroot/groups directory
3781 function restore_group_files($restore) {
3783 global $CFG;
3785 $status = true;
3787 $counter = 0;
3789 //First, we check to "groups" exists and create is as necessary
3790 //in CFG->dataroot
3791 $dest_dir = $CFG->dataroot.'/groups';
3792 $status = check_dir_exists($dest_dir,true);
3794 //Now, we iterate over "group_files" records to check if that user dir must be
3795 //copied (and renamed) to the "groups" dir.
3796 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
3797 //Check if directory exists
3798 if (is_dir($rootdir)) {
3799 $list = list_directories ($rootdir);
3800 if ($list) {
3801 //Iterate
3802 $counter = 0;
3803 foreach ($list as $dir) {
3804 //Look for dir like groupid in backup_ids
3805 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
3806 "table_name","groups",
3807 "old_id",$dir);
3808 //If that group exists in backup_ids
3809 if ($data) {
3810 if (!file_exists($dest_dir."/".$data->new_id)) {
3811 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
3812 $counter ++;
3814 //Do some output
3815 if ($counter % 2 == 0) {
3816 if (!defined('RESTORE_SILENTLY')) {
3817 echo ".";
3818 if ($counter % 40 == 0) {
3819 echo "<br />";
3822 backup_flush(300);
3828 //If status is ok and whe have dirs created, returns counter to inform
3829 if ($status and $counter) {
3830 return $counter;
3831 } else {
3832 return $status;
3836 //This function restores the course files from the temp (course_files) directory to the
3837 //dataroot/course_id directory
3838 function restore_course_files($restore) {
3840 global $CFG;
3842 $status = true;
3844 $counter = 0;
3846 //First, we check to "course_id" exists and create is as necessary
3847 //in CFG->dataroot
3848 $dest_dir = $CFG->dataroot."/".$restore->course_id;
3849 $status = check_dir_exists($dest_dir,true);
3851 //Now, we iterate over "course_files" records to check if that file/dir must be
3852 //copied to the "dest_dir" dir.
3853 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
3854 //Check if directory exists
3855 if (is_dir($rootdir)) {
3856 $list = list_directories_and_files ($rootdir);
3857 if ($list) {
3858 //Iterate
3859 $counter = 0;
3860 foreach ($list as $dir) {
3861 //Copy the dir to its new location
3862 //Only if destination file/dir doesn exists
3863 if (!file_exists($dest_dir."/".$dir)) {
3864 $status = backup_copy_file($rootdir."/".$dir,
3865 $dest_dir."/".$dir,true);
3866 $counter ++;
3868 //Do some output
3869 if ($counter % 2 == 0) {
3870 if (!defined('RESTORE_SILENTLY')) {
3871 echo ".";
3872 if ($counter % 40 == 0) {
3873 echo "<br />";
3876 backup_flush(300);
3881 //If status is ok and whe have dirs created, returns counter to inform
3882 if ($status and $counter) {
3883 return $counter;
3884 } else {
3885 return $status;
3889 //This function restores the site files from the temp (site_files) directory to the
3890 //dataroot/SITEID directory
3891 function restore_site_files($restore) {
3893 global $CFG;
3895 $status = true;
3897 $counter = 0;
3899 //First, we check to "course_id" exists and create is as necessary
3900 //in CFG->dataroot
3901 $dest_dir = $CFG->dataroot."/".SITEID;
3902 $status = check_dir_exists($dest_dir,true);
3904 //Now, we iterate over "site_files" files to check if that file/dir must be
3905 //copied to the "dest_dir" dir.
3906 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/site_files";
3907 //Check if directory exists
3908 if (is_dir($rootdir)) {
3909 $list = list_directories_and_files ($rootdir);
3910 if ($list) {
3911 //Iterate
3912 $counter = 0;
3913 foreach ($list as $dir) {
3914 //Copy the dir to its new location
3915 //Only if destination file/dir doesn exists
3916 if (!file_exists($dest_dir."/".$dir)) {
3917 $status = backup_copy_file($rootdir."/".$dir,
3918 $dest_dir."/".$dir,true);
3919 $counter ++;
3921 //Do some output
3922 if ($counter % 2 == 0) {
3923 if (!defined('RESTORE_SILENTLY')) {
3924 echo ".";
3925 if ($counter % 40 == 0) {
3926 echo "<br />";
3929 backup_flush(300);
3934 //If status is ok and whe have dirs created, returns counter to inform
3935 if ($status and $counter) {
3936 return $counter;
3937 } else {
3938 return $status;
3943 //This function creates all the structures for every module in backup file
3944 //Depending what has been selected.
3945 function restore_create_modules($restore,$xml_file) {
3947 global $CFG;
3948 $status = true;
3949 //Check it exists
3950 if (!file_exists($xml_file)) {
3951 $status = false;
3953 //Get info from xml
3954 if ($status) {
3955 //info will contain the id and modtype of every module
3956 //in backup_ids->info will be the real info (serialized)
3957 $info = restore_read_xml_modules($restore,$xml_file);
3959 //Now, if we have anything in info, we have to restore that mods
3960 //from backup_ids (calling every mod restore function)
3961 if ($info) {
3962 if ($info !== true) {
3963 if (!defined('RESTORE_SILENTLY')) {
3964 echo '<ul>';
3966 //Iterate over each module
3967 foreach ($info as $mod) {
3968 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
3969 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
3970 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
3971 $modrestore = $mod->modtype."_restore_mods";
3972 if (function_exists($modrestore)) { //Debug
3973 // we want to restore all mods even when one fails
3974 // incorrect code here ignored any errors during module restore in 1.6-1.8
3975 $status = $status && $modrestore($mod,$restore);
3976 } else {
3977 //Something was wrong. Function should exist.
3978 $status = false;
3982 if (!defined('RESTORE_SILENTLY')) {
3983 echo '</ul>';
3986 } else {
3987 $status = false;
3989 return $status;
3992 //This function creates all the structures for every log in backup file
3993 //Depending what has been selected.
3994 function restore_create_logs($restore,$xml_file) {
3996 global $CFG,$db;
3998 //Number of records to get in every chunk
3999 $recordset_size = 4;
4000 //Counter, points to current record
4001 $counter = 0;
4002 //To count all the recods to restore
4003 $count_logs = 0;
4005 $status = true;
4006 //Check it exists
4007 if (!file_exists($xml_file)) {
4008 $status = false;
4010 //Get info from xml
4011 if ($status) {
4012 //count_logs will contain the number of logs entries to process
4013 //in backup_ids->info will be the real info (serialized)
4014 $count_logs = restore_read_xml_logs($restore,$xml_file);
4017 //Now, if we have records in count_logs, we have to restore that logs
4018 //from backup_ids. This piece of code makes calls to:
4019 // - restore_log_course() if it's a course log
4020 // - restore_log_user() if it's a user log
4021 // - restore_log_module() if it's a module log.
4022 //And all is segmented in chunks to allow large recordsets to be restored !!
4023 if ($count_logs > 0) {
4024 while ($counter < $count_logs) {
4025 //Get a chunk of records
4026 //Take old_id twice to avoid adodb limitation
4027 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
4028 //We have logs
4029 if ($logs) {
4030 //Iterate
4031 foreach ($logs as $log) {
4032 //Get the full record from backup_ids
4033 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
4034 if ($data) {
4035 //Now get completed xmlized object
4036 $info = $data->info;
4037 //traverse_xmlize($info); //Debug
4038 //print_object ($GLOBALS['traverse_array']); //Debug
4039 //$GLOBALS['traverse_array']=""; //Debug
4040 //Now build the LOG record structure
4041 $dblog = new object();
4042 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
4043 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
4044 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
4045 $dblog->course = $restore->course_id;
4046 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
4047 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
4048 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
4049 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
4050 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
4051 //We have to recode the userid field
4052 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
4053 if ($user) {
4054 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
4055 $dblog->userid = $user->new_id;
4057 //We have to recode the cmid field (if module isn't "course" or "user")
4058 if ($dblog->module != "course" and $dblog->module != "user") {
4059 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
4060 if ($cm) {
4061 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
4062 $dblog->cmid = $cm->new_id;
4063 } else {
4064 $dblog->cmid = 0;
4067 //print_object ($dblog); //Debug
4068 //Now, we redirect to the needed function to make all the work
4069 if ($dblog->module == "course") {
4070 //It's a course log,
4071 $stat = restore_log_course($restore,$dblog);
4072 } elseif ($dblog->module == "user") {
4073 //It's a user log,
4074 $stat = restore_log_user($restore,$dblog);
4075 } else {
4076 //It's a module log,
4077 $stat = restore_log_module($restore,$dblog);
4081 //Do some output
4082 $counter++;
4083 if ($counter % 10 == 0) {
4084 if (!defined('RESTORE_SILENTLY')) {
4085 echo ".";
4086 if ($counter % 200 == 0) {
4087 echo "<br />";
4090 backup_flush(300);
4093 } else {
4094 //We never should arrive here
4095 $counter = $count_logs;
4096 $status = false;
4101 return $status;
4104 //This function inserts a course log record, calculating the URL field as necessary
4105 function restore_log_course($restore,$log) {
4107 $status = true;
4108 $toinsert = false;
4110 //echo "<hr />Before transformations<br />"; //Debug
4111 //print_object($log); //Debug
4112 //Depending of the action, we recode different things
4113 switch ($log->action) {
4114 case "view":
4115 $log->url = "view.php?id=".$log->course;
4116 $log->info = $log->course;
4117 $toinsert = true;
4118 break;
4119 case "guest":
4120 $log->url = "view.php?id=".$log->course;
4121 $toinsert = true;
4122 break;
4123 case "user report":
4124 //recode the info field (it's the user id)
4125 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4126 if ($user) {
4127 $log->info = $user->new_id;
4128 //Now, extract the mode from the url field
4129 $mode = substr(strrchr($log->url,"="),1);
4130 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
4131 $toinsert = true;
4133 break;
4134 case "add mod":
4135 //Extract the course_module from the url field
4136 $cmid = substr(strrchr($log->url,"="),1);
4137 //recode the course_module to see it it has been restored
4138 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
4139 if ($cm) {
4140 $cmid = $cm->new_id;
4141 //Extract the module name and the module id from the info field
4142 $modname = strtok($log->info," ");
4143 $modid = strtok(" ");
4144 //recode the module id to see if it has been restored
4145 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
4146 if ($mod) {
4147 $modid = $mod->new_id;
4148 //Now I have everything so reconstruct url and info
4149 $log->info = $modname." ".$modid;
4150 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
4151 $toinsert = true;
4154 break;
4155 case "update mod":
4156 //Extract the course_module from the url field
4157 $cmid = substr(strrchr($log->url,"="),1);
4158 //recode the course_module to see it it has been restored
4159 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
4160 if ($cm) {
4161 $cmid = $cm->new_id;
4162 //Extract the module name and the module id from the info field
4163 $modname = strtok($log->info," ");
4164 $modid = strtok(" ");
4165 //recode the module id to see if it has been restored
4166 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
4167 if ($mod) {
4168 $modid = $mod->new_id;
4169 //Now I have everything so reconstruct url and info
4170 $log->info = $modname." ".$modid;
4171 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
4172 $toinsert = true;
4175 break;
4176 case "delete mod":
4177 $log->url = "view.php?id=".$log->course;
4178 $toinsert = true;
4179 break;
4180 case "update":
4181 $log->url = "edit.php?id=".$log->course;
4182 $log->info = "";
4183 $toinsert = true;
4184 break;
4185 case "unenrol":
4186 //recode the info field (it's the user id)
4187 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4188 if ($user) {
4189 $log->info = $user->new_id;
4190 $log->url = "view.php?id=".$log->course;
4191 $toinsert = true;
4193 break;
4194 case "enrol":
4195 //recode the info field (it's the user id)
4196 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4197 if ($user) {
4198 $log->info = $user->new_id;
4199 $log->url = "view.php?id=".$log->course;
4200 $toinsert = true;
4202 break;
4203 case "editsection":
4204 //Extract the course_section from the url field
4205 $secid = substr(strrchr($log->url,"="),1);
4206 //recode the course_section to see if it has been restored
4207 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
4208 if ($sec) {
4209 $secid = $sec->new_id;
4210 //Now I have everything so reconstruct url and info
4211 $log->url = "editsection.php?id=".$secid;
4212 $toinsert = true;
4214 break;
4215 case "new":
4216 $log->url = "view.php?id=".$log->course;
4217 $log->info = "";
4218 $toinsert = true;
4219 break;
4220 case "recent":
4221 $log->url = "recent.php?id=".$log->course;
4222 $log->info = "";
4223 $toinsert = true;
4224 break;
4225 case "report log":
4226 $log->url = "report/log/index.php?id=".$log->course;
4227 $log->info = $log->course;
4228 $toinsert = true;
4229 break;
4230 case "report live":
4231 $log->url = "report/log/live.php?id=".$log->course;
4232 $log->info = $log->course;
4233 $toinsert = true;
4234 break;
4235 case "report outline":
4236 $log->url = "report/outline/index.php?id=".$log->course;
4237 $log->info = $log->course;
4238 $toinsert = true;
4239 break;
4240 case "report participation":
4241 $log->url = "report/participation/index.php?id=".$log->course;
4242 $log->info = $log->course;
4243 $toinsert = true;
4244 break;
4245 case "report stats":
4246 $log->url = "report/stats/index.php?id=".$log->course;
4247 $log->info = $log->course;
4248 $toinsert = true;
4249 break;
4250 default:
4251 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
4252 break;
4255 //echo "After transformations<br />"; //Debug
4256 //print_object($log); //Debug
4258 //Now if $toinsert is set, insert the record
4259 if ($toinsert) {
4260 //echo "Inserting record<br />"; //Debug
4261 $status = insert_record("log",$log);
4263 return $status;
4266 //This function inserts a user log record, calculating the URL field as necessary
4267 function restore_log_user($restore,$log) {
4269 $status = true;
4270 $toinsert = false;
4272 //echo "<hr />Before transformations<br />"; //Debug
4273 //print_object($log); //Debug
4274 //Depending of the action, we recode different things
4275 switch ($log->action) {
4276 case "view":
4277 //recode the info field (it's the user id)
4278 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4279 if ($user) {
4280 $log->info = $user->new_id;
4281 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4282 $toinsert = true;
4284 break;
4285 case "change password":
4286 //recode the info field (it's the user id)
4287 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4288 if ($user) {
4289 $log->info = $user->new_id;
4290 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4291 $toinsert = true;
4293 break;
4294 case "login":
4295 //recode the info field (it's the user id)
4296 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4297 if ($user) {
4298 $log->info = $user->new_id;
4299 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4300 $toinsert = true;
4302 break;
4303 case "logout":
4304 //recode the info field (it's the user id)
4305 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4306 if ($user) {
4307 $log->info = $user->new_id;
4308 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4309 $toinsert = true;
4311 break;
4312 case "view all":
4313 $log->url = "view.php?id=".$log->course;
4314 $log->info = "";
4315 $toinsert = true;
4316 case "update":
4317 //We split the url by ampersand char
4318 $first_part = strtok($log->url,"&");
4319 //Get data after the = char. It's the user being updated
4320 $userid = substr(strrchr($first_part,"="),1);
4321 //Recode the user
4322 $user = backup_getid($restore->backup_unique_code,"user",$userid);
4323 if ($user) {
4324 $log->info = "";
4325 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
4326 $toinsert = true;
4328 break;
4329 default:
4330 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
4331 break;
4334 //echo "After transformations<br />"; //Debug
4335 //print_object($log); //Debug
4337 //Now if $toinsert is set, insert the record
4338 if ($toinsert) {
4339 //echo "Inserting record<br />"; //Debug
4340 $status = insert_record("log",$log);
4342 return $status;
4345 //This function inserts a module log record, calculating the URL field as necessary
4346 function restore_log_module($restore,$log) {
4348 $status = true;
4349 $toinsert = false;
4351 //echo "<hr />Before transformations<br />"; //Debug
4352 //print_object($log); //Debug
4354 //Now we see if the required function in the module exists
4355 $function = $log->module."_restore_logs";
4356 if (function_exists($function)) {
4357 //Call the function
4358 $log = $function($restore,$log);
4359 //If everything is ok, mark the insert flag
4360 if ($log) {
4361 $toinsert = true;
4365 //echo "After transformations<br />"; //Debug
4366 //print_object($log); //Debug
4368 //Now if $toinsert is set, insert the record
4369 if ($toinsert) {
4370 //echo "Inserting record<br />"; //Debug
4371 $status = insert_record("log",$log);
4373 return $status;
4376 //This function adjusts the instance field into course_modules. It's executed after
4377 //modules restore. There, we KNOW the new instance id !!
4378 function restore_check_instances($restore) {
4380 global $CFG;
4382 $status = true;
4384 //We are going to iterate over each course_module saved in backup_ids
4385 $course_modules = get_records_sql("SELECT old_id,new_id
4386 FROM {$CFG->prefix}backup_ids
4387 WHERE backup_code = '$restore->backup_unique_code' AND
4388 table_name = 'course_modules'");
4389 if ($course_modules) {
4390 foreach($course_modules as $cm) {
4391 //Get full record, using backup_getids
4392 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
4393 //Now we are going to the REAL course_modules to get its type (field module)
4394 $module = get_record("course_modules","id",$cm_module->new_id);
4395 if ($module) {
4396 //We know the module type id. Get the name from modules
4397 $type = get_record("modules","id",$module->module);
4398 if ($type) {
4399 //We know the type name and the old_id. Get its new_id
4400 //from backup_ids. It's the instance !!!
4401 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
4402 if ($instance) {
4403 //We have the new instance, so update the record in course_modules
4404 $module->instance = $instance->new_id;
4405 //print_object ($module); //Debug
4406 $status = update_record("course_modules",$module);
4407 } else {
4408 $status = false;
4410 } else {
4411 $status = false;
4413 } else {
4414 $status = false;
4417 /// Finally, calculate modinfo cache.
4418 rebuild_course_cache($restore->course_id);
4422 return $status;
4425 //=====================================================================================
4426 //== ==
4427 //== XML Functions (SAX) ==
4428 //== ==
4429 //=====================================================================================
4431 //This is the class used to do all the xml parse
4432 class MoodleParser {
4434 var $level = 0; //Level we are
4435 var $counter = 0; //Counter
4436 var $tree = array(); //Array of levels we are
4437 var $content = ""; //Content under current level
4438 var $todo = ""; //What we hav to do when parsing
4439 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
4440 var $temp = ""; //Temp storage.
4441 var $preferences = ""; //Preferences about what to load !!
4442 var $finished = false; //Flag to say xml_parse to stop
4444 //This function is used to get the current contents property value
4445 //They are trimed (and converted from utf8 if needed)
4446 function getContents() {
4447 return trim($this->content);
4450 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
4451 function startElementInfo($parser, $tagName, $attrs) {
4452 //Refresh properties
4453 $this->level++;
4454 $this->tree[$this->level] = $tagName;
4456 //Output something to avoid browser timeouts...
4457 //backup_flush();
4459 //Check if we are into INFO zone
4460 //if ($this->tree[2] == "INFO") //Debug
4461 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4464 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
4465 function startElementRoles($parser, $tagName, $attrs) {
4466 //Refresh properties
4467 $this->level++;
4468 $this->tree[$this->level] = $tagName;
4470 //Output something to avoid browser timeouts...
4471 //backup_flush();
4473 //Check if we are into INFO zone
4474 //if ($this->tree[2] == "INFO") //Debug
4475 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4479 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
4480 function startElementCourseHeader($parser, $tagName, $attrs) {
4481 //Refresh properties
4482 $this->level++;
4483 $this->tree[$this->level] = $tagName;
4485 //Output something to avoid browser timeouts...
4486 //backup_flush();
4488 //Check if we are into COURSE_HEADER zone
4489 //if ($this->tree[3] == "HEADER") //Debug
4490 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4493 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
4494 function startElementBlocks($parser, $tagName, $attrs) {
4495 //Refresh properties
4496 $this->level++;
4497 $this->tree[$this->level] = $tagName;
4499 //Output something to avoid browser timeouts...
4500 //backup_flush();
4502 //Check if we are into BLOCKS zone
4503 //if ($this->tree[3] == "BLOCKS") //Debug
4504 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4506 //If we are under a BLOCK tag under a BLOCKS zone, accumule it
4507 if (isset($this->tree[4]) and isset($this->tree[3])) { //
4508 if ($this->tree[4] == "BLOCK" and $this->tree[3] == "BLOCKS") {
4509 if (!isset($this->temp)) {
4510 $this->temp = "";
4512 $this->temp .= "<".$tagName.">";
4517 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
4518 function startElementSections($parser, $tagName, $attrs) {
4519 //Refresh properties
4520 $this->level++;
4521 $this->tree[$this->level] = $tagName;
4523 //Output something to avoid browser timeouts...
4524 //backup_flush();
4526 //Check if we are into SECTIONS zone
4527 //if ($this->tree[3] == "SECTIONS") //Debug
4528 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4531 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4532 function startElementFormatData($parser, $tagName, $attrs) {
4533 //Refresh properties
4534 $this->level++;
4535 $this->tree[$this->level] = $tagName;
4537 //Output something to avoid browser timeouts...
4538 //backup_flush();
4540 //Accumulate all the data inside this tag
4541 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
4542 if (!isset($this->temp)) {
4543 $this->temp = '';
4545 $this->temp .= "<".$tagName.">";
4548 //Check if we are into FORMATDATA zone
4549 //if ($this->tree[3] == "FORMATDATA") //Debug
4550 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4553 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4554 function startElementMetacourse($parser, $tagName, $attrs) {
4556 //Refresh properties
4557 $this->level++;
4558 $this->tree[$this->level] = $tagName;
4560 //Output something to avoid browser timeouts...
4561 //backup_flush();
4563 //Check if we are into METACOURSE zone
4564 //if ($this->tree[3] == "METACOURSE") //Debug
4565 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4568 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4569 function startElementGradebook($parser, $tagName, $attrs) {
4571 //Refresh properties
4572 $this->level++;
4573 $this->tree[$this->level] = $tagName;
4575 //Output something to avoid browser timeouts...
4576 //backup_flush();
4578 //Check if we are into GRADEBOOK zone
4579 //if ($this->tree[3] == "GRADEBOOK") //Debug
4580 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4582 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4583 if (isset($this->tree[5]) and isset($this->tree[3])) {
4584 if (($this->tree[5] == "GRADE_ITEM" || $this->tree[5] == "GRADE_CATEGORY" || $this->tree[5] == "GRADE_LETTER" || $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")) {
4586 if (!isset($this->temp)) {
4587 $this->temp = "";
4589 $this->temp .= "<".$tagName.">";
4594 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4595 function startElementOldGradebook($parser, $tagName, $attrs) {
4597 //Refresh properties
4598 $this->level++;
4599 $this->tree[$this->level] = $tagName;
4601 //Output something to avoid browser timeouts...
4602 //backup_flush();
4604 //Check if we are into GRADEBOOK zone
4605 //if ($this->tree[3] == "GRADEBOOK") //Debug
4606 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4608 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4609 if (isset($this->tree[5]) and isset($this->tree[3])) {
4610 if (($this->tree[5] == "GRADE_PREFERENCE" || $this->tree[5] == "GRADE_LETTER" || $this->tree[5] == "GRADE_CATEGORY" ) && ($this->tree[3] == "GRADEBOOK")) {
4611 if (!isset($this->temp)) {
4612 $this->temp = "";
4614 $this->temp .= "<".$tagName.">";
4620 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
4621 function startElementUsers($parser, $tagName, $attrs) {
4622 //Refresh properties
4623 $this->level++;
4624 $this->tree[$this->level] = $tagName;
4626 //Check if we are into USERS zone
4627 //if ($this->tree[3] == "USERS") //Debug
4628 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4631 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
4632 function startElementMessages($parser, $tagName, $attrs) {
4633 //Refresh properties
4634 $this->level++;
4635 $this->tree[$this->level] = $tagName;
4637 //Output something to avoid browser timeouts...
4638 //backup_flush();
4640 //Check if we are into MESSAGES zone
4641 //if ($this->tree[3] == "MESSAGES") //Debug
4642 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4644 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
4645 if (isset($this->tree[4]) and isset($this->tree[3])) {
4646 if (($this->tree[4] == "MESSAGE" || (isset($this->tree[5]) && $this->tree[5] == "CONTACT" )) and ($this->tree[3] == "MESSAGES")) {
4647 if (!isset($this->temp)) {
4648 $this->temp = "";
4650 $this->temp .= "<".$tagName.">";
4655 //This is the startTag handler we use where we are reading the blogs zone (todo="BLOGS")
4656 function startElementBlogs($parser, $tagName, $attrs) {
4657 //Refresh properties
4658 $this->level++;
4659 $this->tree[$this->level] = $tagName;
4661 //Output something to avoid browser timeouts...
4662 //backup_flush();
4664 //Check if we are into BLOGS zone
4665 //if ($this->tree[3] == "BLOGS") //Debug
4666 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4668 //If we are under a BLOG tag under a BLOGS zone, accumule it
4669 if (isset($this->tree[4]) and isset($this->tree[3])) {
4670 if ($this->tree[4] == "BLOG" and $this->tree[3] == "BLOGS") {
4671 if (!isset($this->temp)) {
4672 $this->temp = "";
4674 $this->temp .= "<".$tagName.">";
4679 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
4680 function startElementQuestions($parser, $tagName, $attrs) {
4681 //Refresh properties
4682 $this->level++;
4683 $this->tree[$this->level] = $tagName;
4685 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
4686 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
4687 //} //Debug
4689 //Output something to avoid browser timeouts...
4690 //backup_flush();
4692 //Check if we are into QUESTION_CATEGORIES zone
4693 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
4694 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4696 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
4697 if (isset($this->tree[4]) and isset($this->tree[3])) {
4698 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
4699 if (!isset($this->temp)) {
4700 $this->temp = "";
4702 $this->temp .= "<".$tagName.">";
4707 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
4708 function startElementScales($parser, $tagName, $attrs) {
4709 //Refresh properties
4710 $this->level++;
4711 $this->tree[$this->level] = $tagName;
4713 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
4714 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
4715 //} //Debug
4717 //Output something to avoid browser timeouts...
4718 //backup_flush();
4720 //Check if we are into SCALES zone
4721 //if ($this->tree[3] == "SCALES") //Debug
4722 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4724 //If we are under a SCALE tag under a SCALES zone, accumule it
4725 if (isset($this->tree[4]) and isset($this->tree[3])) {
4726 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
4727 if (!isset($this->temp)) {
4728 $this->temp = "";
4730 $this->temp .= "<".$tagName.">";
4735 function startElementGroups($parser, $tagName, $attrs) {
4736 //Refresh properties
4737 $this->level++;
4738 $this->tree[$this->level] = $tagName;
4740 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
4741 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
4742 //} //Debug
4744 //Output something to avoid browser timeouts...
4745 //backup_flush();
4747 //Check if we are into GROUPS zone
4748 //if ($this->tree[3] == "GROUPS") //Debug
4749 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4751 //If we are under a GROUP tag under a GROUPS zone, accumule it
4752 if (isset($this->tree[4]) and isset($this->tree[3])) {
4753 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
4754 if (!isset($this->temp)) {
4755 $this->temp = "";
4757 $this->temp .= "<".$tagName.">";
4762 function startElementGroupings($parser, $tagName, $attrs) {
4763 //Refresh properties
4764 $this->level++;
4765 $this->tree[$this->level] = $tagName;
4767 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
4768 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
4769 //} //Debug
4771 //Output something to avoid browser timeouts...
4772 //backup_flush();
4774 //Check if we are into GROUPINGS zone
4775 //if ($this->tree[3] == "GROUPINGS") //Debug
4776 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4778 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
4779 if (isset($this->tree[4]) and isset($this->tree[3])) {
4780 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
4781 if (!isset($this->temp)) {
4782 $this->temp = "";
4784 $this->temp .= "<".$tagName.">";
4789 function startElementGroupingsGroups($parser, $tagName, $attrs) {
4790 //Refresh properties
4791 $this->level++;
4792 $this->tree[$this->level] = $tagName;
4794 //if ($tagName == "GROUPINGGROUP" && $this->tree[3] == "GROUPINGSGROUPS") { //Debug
4795 // echo "<P>GROUPINGSGROUP: ".strftime ("%X",time()),"-"; //Debug
4796 //} //Debug
4798 //Output something to avoid browser timeouts...
4799 backup_flush();
4801 //Check if we are into GROUPINGSGROUPS zone
4802 //if ($this->tree[3] == "GROUPINGSGROUPS") //Debug
4803 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4805 //If we are under a GROUPINGGROUP tag under a GROUPINGSGROUPS zone, accumule it
4806 if (isset($this->tree[4]) and isset($this->tree[3])) {
4807 if (($this->tree[4] == "GROUPINGGROUP") and ($this->tree[3] == "GROUPINGSGROUPS")) {
4808 if (!isset($this->temp)) {
4809 $this->temp = "";
4811 $this->temp .= "<".$tagName.">";
4816 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
4817 function startElementEvents($parser, $tagName, $attrs) {
4818 //Refresh properties
4819 $this->level++;
4820 $this->tree[$this->level] = $tagName;
4822 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
4823 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
4824 //} //Debug
4826 //Output something to avoid browser timeouts...
4827 //backup_flush();
4829 //Check if we are into EVENTS zone
4830 //if ($this->tree[3] == "EVENTS") //Debug
4831 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4833 //If we are under a EVENT tag under a EVENTS zone, accumule it
4834 if (isset($this->tree[4]) and isset($this->tree[3])) {
4835 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
4836 if (!isset($this->temp)) {
4837 $this->temp = "";
4839 $this->temp .= "<".$tagName.">";
4844 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
4845 function startElementModules($parser, $tagName, $attrs) {
4846 //Refresh properties
4847 $this->level++;
4848 $this->tree[$this->level] = $tagName;
4850 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
4851 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
4852 //} //Debug
4854 //Output something to avoid browser timeouts...
4855 //backup_flush();
4857 //Check if we are into MODULES zone
4858 //if ($this->tree[3] == "MODULES") //Debug
4859 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4861 //If we are under a MOD tag under a MODULES zone, accumule it
4862 if (isset($this->tree[4]) and isset($this->tree[3])) {
4863 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
4864 if (!isset($this->temp)) {
4865 $this->temp = "";
4867 $this->temp .= "<".$tagName.">";
4872 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
4873 function startElementLogs($parser, $tagName, $attrs) {
4874 //Refresh properties
4875 $this->level++;
4876 $this->tree[$this->level] = $tagName;
4878 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
4879 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
4880 //} //Debug
4882 //Output something to avoid browser timeouts...
4883 //backup_flush();
4885 //Check if we are into LOGS zone
4886 //if ($this->tree[3] == "LOGS") //Debug
4887 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4889 //If we are under a LOG tag under a LOGS zone, accumule it
4890 if (isset($this->tree[4]) and isset($this->tree[3])) {
4891 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
4892 if (!isset($this->temp)) {
4893 $this->temp = "";
4895 $this->temp .= "<".$tagName.">";
4900 //This is the startTag default handler we use when todo is undefined
4901 function startElement($parser, $tagName, $attrs) {
4902 $this->level++;
4903 $this->tree[$this->level] = $tagName;
4905 //Output something to avoid browser timeouts...
4906 //backup_flush();
4908 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4911 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
4912 function endElementInfo($parser, $tagName) {
4913 //Check if we are into INFO zone
4914 if ($this->tree[2] == "INFO") {
4915 //if (trim($this->content)) //Debug
4916 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4917 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4918 //Dependig of different combinations, do different things
4919 if ($this->level == 3) {
4920 switch ($tagName) {
4921 case "NAME":
4922 $this->info->backup_name = $this->getContents();
4923 break;
4924 case "MOODLE_VERSION":
4925 $this->info->backup_moodle_version = $this->getContents();
4926 break;
4927 case "MOODLE_RELEASE":
4928 $this->info->backup_moodle_release = $this->getContents();
4929 break;
4930 case "BACKUP_VERSION":
4931 $this->info->backup_backup_version = $this->getContents();
4932 break;
4933 case "BACKUP_RELEASE":
4934 $this->info->backup_backup_release = $this->getContents();
4935 break;
4936 case "DATE":
4937 $this->info->backup_date = $this->getContents();
4938 break;
4939 case "ORIGINAL_WWWROOT":
4940 $this->info->original_wwwroot = $this->getContents();
4941 break;
4942 case "MNET_EXTERNALUSERS":
4943 $this->info->mnet_externalusers = $this->getContents();
4944 break;
4947 if ($this->tree[3] == "DETAILS") {
4948 if ($this->level == 4) {
4949 switch ($tagName) {
4950 case "METACOURSE":
4951 $this->info->backup_metacourse = $this->getContents();
4952 break;
4953 case "USERS":
4954 $this->info->backup_users = $this->getContents();
4955 break;
4956 case "LOGS":
4957 $this->info->backup_logs = $this->getContents();
4958 break;
4959 case "USERFILES":
4960 $this->info->backup_user_files = $this->getContents();
4961 break;
4962 case "COURSEFILES":
4963 $this->info->backup_course_files = $this->getContents();
4964 break;
4965 case "SITEFILES":
4966 $this->info->backup_site_files = $this->getContents();
4967 break;
4968 case "GRADEBOOKHISTORIES":
4969 $this->info->gradebook_histories = $this->getContents();
4970 break;
4971 case "MESSAGES":
4972 $this->info->backup_messages = $this->getContents();
4973 break;
4974 case "BLOGS":
4975 $this->info->backup_blogs = $this->getContents();
4976 break;
4977 case 'BLOCKFORMAT':
4978 $this->info->backup_block_format = $this->getContents();
4979 break;
4982 if ($this->level == 5) {
4983 switch ($tagName) {
4984 case "NAME":
4985 $this->info->tempName = $this->getContents();
4986 break;
4987 case "INCLUDED":
4988 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
4989 break;
4990 case "USERINFO":
4991 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
4992 break;
4995 if ($this->level == 7) {
4996 switch ($tagName) {
4997 case "ID":
4998 $this->info->tempId = $this->getContents();
4999 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
5000 break;
5001 case "NAME":
5002 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
5003 break;
5004 case "INCLUDED":
5005 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
5006 break;
5007 case "USERINFO":
5008 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
5009 break;
5015 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
5016 //Speed up a lot (avoid parse all)
5017 if ($tagName == "INFO") {
5018 $this->finished = true;
5021 //Clear things
5022 $this->tree[$this->level] = "";
5023 $this->level--;
5024 $this->content = "";
5028 function endElementRoles($parser, $tagName) {
5029 //Check if we are into INFO zone
5030 if ($this->tree[2] == "ROLES") {
5032 if ($this->tree[3] == "ROLE") {
5033 if ($this->level == 4) {
5034 switch ($tagName) {
5035 case "ID": // this is the old id
5036 $this->info->tempid = $this->getContents();
5037 $this->info->roles[$this->info->tempid]->id = $this->info->tempid;
5038 break;
5039 case "NAME":
5040 $this->info->roles[$this->info->tempid]->name = $this->getContents();;
5041 break;
5042 case "SHORTNAME":
5043 $this->info->roles[$this->info->tempid]->shortname = $this->getContents();;
5044 break;
5045 case "NAMEINCOURSE": // custom name of the role in course
5046 $this->info->roles[$this->info->tempid]->nameincourse = $this->getContents();;
5047 break;
5050 if ($this->level == 6) {
5051 switch ($tagName) {
5052 case "NAME":
5053 $this->info->tempcapname = $this->getContents();
5054 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
5055 break;
5056 case "PERMISSION":
5057 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
5058 break;
5059 case "TIMEMODIFIED":
5060 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
5061 break;
5062 case "MODIFIERID":
5063 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
5064 break;
5070 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
5071 //Speed up a lot (avoid parse all)
5072 if ($tagName == "ROLES") {
5073 $this->finished = true;
5076 //Clear things
5077 $this->tree[$this->level] = "";
5078 $this->level--;
5079 $this->content = "";
5083 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
5084 function endElementCourseHeader($parser, $tagName) {
5085 //Check if we are into COURSE_HEADER zone
5086 if ($this->tree[3] == "HEADER") {
5087 //if (trim($this->content)) //Debug
5088 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5089 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5090 //Dependig of different combinations, do different things
5091 if ($this->level == 4) {
5092 switch ($tagName) {
5093 case "ID":
5094 $this->info->course_id = $this->getContents();
5095 break;
5096 case "PASSWORD":
5097 $this->info->course_password = $this->getContents();
5098 break;
5099 case "FULLNAME":
5100 $this->info->course_fullname = $this->getContents();
5101 break;
5102 case "SHORTNAME":
5103 $this->info->course_shortname = $this->getContents();
5104 break;
5105 case "IDNUMBER":
5106 $this->info->course_idnumber = $this->getContents();
5107 break;
5108 case "SUMMARY":
5109 $this->info->course_summary = $this->getContents();
5110 break;
5111 case "FORMAT":
5112 $this->info->course_format = $this->getContents();
5113 break;
5114 case "SHOWGRADES":
5115 $this->info->course_showgrades = $this->getContents();
5116 break;
5117 case "BLOCKINFO":
5118 $this->info->blockinfo = $this->getContents();
5119 break;
5120 case "NEWSITEMS":
5121 $this->info->course_newsitems = $this->getContents();
5122 break;
5123 case "TEACHER":
5124 $this->info->course_teacher = $this->getContents();
5125 break;
5126 case "TEACHERS":
5127 $this->info->course_teachers = $this->getContents();
5128 break;
5129 case "STUDENT":
5130 $this->info->course_student = $this->getContents();
5131 break;
5132 case "STUDENTS":
5133 $this->info->course_students = $this->getContents();
5134 break;
5135 case "GUEST":
5136 $this->info->course_guest = $this->getContents();
5137 break;
5138 case "STARTDATE":
5139 $this->info->course_startdate = $this->getContents();
5140 break;
5141 case "NUMSECTIONS":
5142 $this->info->course_numsections = $this->getContents();
5143 break;
5144 //case "SHOWRECENT": INFO: This is out in 1.3
5145 // $this->info->course_showrecent = $this->getContents();
5146 // break;
5147 case "MAXBYTES":
5148 $this->info->course_maxbytes = $this->getContents();
5149 break;
5150 case "SHOWREPORTS":
5151 $this->info->course_showreports = $this->getContents();
5152 break;
5153 case "GROUPMODE":
5154 $this->info->course_groupmode = $this->getContents();
5155 break;
5156 case "GROUPMODEFORCE":
5157 $this->info->course_groupmodeforce = $this->getContents();
5158 break;
5159 case "DEFAULTGROUPINGID":
5160 $this->info->course_defaultgroupingid = $this->getContents();
5161 break;
5162 case "LANG":
5163 $this->info->course_lang = $this->getContents();
5164 break;
5165 case "THEME":
5166 $this->info->course_theme = $this->getContents();
5167 break;
5168 case "COST":
5169 $this->info->course_cost = $this->getContents();
5170 break;
5171 case "CURRENCY":
5172 $this->info->course_currency = $this->getContents();
5173 break;
5174 case "MARKER":
5175 $this->info->course_marker = $this->getContents();
5176 break;
5177 case "VISIBLE":
5178 $this->info->course_visible = $this->getContents();
5179 break;
5180 case "HIDDENSECTIONS":
5181 $this->info->course_hiddensections = $this->getContents();
5182 break;
5183 case "TIMECREATED":
5184 $this->info->course_timecreated = $this->getContents();
5185 break;
5186 case "TIMEMODIFIED":
5187 $this->info->course_timemodified = $this->getContents();
5188 break;
5189 case "METACOURSE":
5190 $this->info->course_metacourse = $this->getContents();
5191 break;
5192 case "EXPIRENOTIFY":
5193 $this->info->course_expirynotify = $this->getContents();
5194 break;
5195 case "NOTIFYSTUDENTS":
5196 $this->info->course_notifystudents = $this->getContents();
5197 break;
5198 case "EXPIRYTHRESHOLD":
5199 $this->info->course_expirythreshold = $this->getContents();
5200 break;
5201 case "ENROLLABLE":
5202 $this->info->course_enrollable = $this->getContents();
5203 break;
5204 case "ENROLSTARTDATE":
5205 $this->info->course_enrolstartdate = $this->getContents();
5206 break;
5207 case "ENROLENDDATE":
5208 $this->info->course_enrolenddate = $this->getContents();
5209 break;
5210 case "ENROLPERIOD":
5211 $this->info->course_enrolperiod = $this->getContents();
5212 break;
5215 if ($this->tree[4] == "CATEGORY") {
5216 if ($this->level == 5) {
5217 switch ($tagName) {
5218 case "ID":
5219 $this->info->category->id = $this->getContents();
5220 break;
5221 case "NAME":
5222 $this->info->category->name = $this->getContents();
5223 break;
5228 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
5229 if ($this->level == 6) {
5230 switch ($tagName) {
5231 case "NAME":
5232 $this->info->tempname = $this->getContents();
5233 break;
5234 case "SHORTNAME":
5235 $this->info->tempshortname = $this->getContents();
5236 break;
5237 case "ID":
5238 $this->info->tempid = $this->getContents();
5239 break;
5243 if ($this->level == 8) {
5244 switch ($tagName) {
5245 case "USERID":
5246 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5247 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5248 $this->info->tempuser = $this->getContents();
5249 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5250 break;
5251 case "HIDDEN":
5252 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5253 break;
5254 case "TIMESTART":
5255 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5256 break;
5257 case "TIMEEND":
5258 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5259 break;
5260 case "TIMEMODIFIED":
5261 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5262 break;
5263 case "MODIFIERID":
5264 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5265 break;
5266 case "ENROL":
5267 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5268 break;
5269 case "SORTORDER":
5270 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5271 break;
5275 } /// ends role_assignments
5277 if ($this->tree[4] == "ROLES_OVERRIDES") {
5278 if ($this->level == 6) {
5279 switch ($tagName) {
5280 case "NAME":
5281 $this->info->tempname = $this->getContents();
5282 break;
5283 case "SHORTNAME":
5284 $this->info->tempshortname = $this->getContents();
5285 break;
5286 case "ID":
5287 $this->info->tempid = $this->getContents();
5288 break;
5292 if ($this->level == 8) {
5293 switch ($tagName) {
5294 case "NAME":
5295 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5296 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5297 $this->info->tempname = $this->getContents(); // change to name of capability
5298 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5299 break;
5300 case "PERMISSION":
5301 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5302 break;
5303 case "TIMEMODIFIED":
5304 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5305 break;
5306 case "MODIFIERID":
5307 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5308 break;
5311 } /// ends role_overrides
5314 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
5315 //Speed up a lot (avoid parse all)
5316 if ($tagName == "HEADER") {
5317 $this->finished = true;
5320 //Clear things
5321 $this->tree[$this->level] = "";
5322 $this->level--;
5323 $this->content = "";
5327 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
5328 function endElementBlocks($parser, $tagName) {
5329 //Check if we are into BLOCKS zone
5330 if ($this->tree[3] == 'BLOCKS') {
5331 //if (trim($this->content)) //Debug
5332 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5333 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5335 // Collect everything into $this->temp
5336 if (!isset($this->temp)) {
5337 $this->temp = "";
5339 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5341 //Dependig of different combinations, do different things
5342 if ($this->level == 4) {
5343 switch ($tagName) {
5344 case 'BLOCK':
5345 //We've finalized a block, get it
5346 $this->info->instances[] = $this->info->tempinstance;
5347 unset($this->info->tempinstance);
5349 //Also, xmlize INSTANCEDATA and save to db
5350 //Prepend XML standard header to info gathered
5351 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5352 //Call to xmlize for this portion of xml data (one BLOCK)
5353 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5354 $data = xmlize($xml_data,0);
5355 //echo strftime ("%X",time())."<p>"; //Debug
5356 //traverse_xmlize($data); //Debug
5357 //print_object ($GLOBALS['traverse_array']); //Debug
5358 //$GLOBALS['traverse_array']=""; //Debug
5359 //Check for instancedata, is exists, then save to DB
5360 if (isset($data['BLOCK']['#']['INSTANCEDATA']['0']['#'])) {
5361 //Get old id
5362 $oldid = $data['BLOCK']['#']['ID']['0']['#'];
5363 //Get instancedata
5365 if ($data = $data['BLOCK']['#']['INSTANCEDATA']['0']['#']) {
5366 //Restore code calls this multiple times - so might already have the newid
5367 if ($newid = backup_getid($this->preferences->backup_unique_code,'block_instance',$oldid)) {
5368 $newid = $newid->new_id;
5369 } else {
5370 $newid = null;
5372 //Save to DB, we will use it later
5373 $status = backup_putid($this->preferences->backup_unique_code,'block_instance',$oldid,$newid,$data);
5376 //Reset temp
5377 unset($this->temp);
5379 break;
5380 default:
5381 die($tagName);
5384 if ($this->level == 5) {
5385 switch ($tagName) {
5386 case 'ID':
5387 $this->info->tempinstance->id = $this->getContents();
5388 case 'NAME':
5389 $this->info->tempinstance->name = $this->getContents();
5390 break;
5391 case 'PAGEID':
5392 $this->info->tempinstance->pageid = $this->getContents();
5393 break;
5394 case 'PAGETYPE':
5395 $this->info->tempinstance->pagetype = $this->getContents();
5396 break;
5397 case 'POSITION':
5398 $this->info->tempinstance->position = $this->getContents();
5399 break;
5400 case 'WEIGHT':
5401 $this->info->tempinstance->weight = $this->getContents();
5402 break;
5403 case 'VISIBLE':
5404 $this->info->tempinstance->visible = $this->getContents();
5405 break;
5406 case 'CONFIGDATA':
5407 $this->info->tempinstance->configdata = $this->getContents();
5408 break;
5409 default:
5410 break;
5414 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
5415 if ($this->level == 7) {
5416 switch ($tagName) {
5417 case "NAME":
5418 $this->info->tempname = $this->getContents();
5419 break;
5420 case "SHORTNAME":
5421 $this->info->tempshortname = $this->getContents();
5422 break;
5423 case "ID":
5424 $this->info->tempid = $this->getContents(); // temp roleid
5425 break;
5429 if ($this->level == 9) {
5431 switch ($tagName) {
5432 case "USERID":
5433 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5435 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5437 $this->info->tempuser = $this->getContents();
5439 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5440 break;
5441 case "HIDDEN":
5442 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5443 break;
5444 case "TIMESTART":
5445 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5446 break;
5447 case "TIMEEND":
5448 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5449 break;
5450 case "TIMEMODIFIED":
5451 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5452 break;
5453 case "MODIFIERID":
5454 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5455 break;
5456 case "ENROL":
5457 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5458 break;
5459 case "SORTORDER":
5460 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5461 break;
5465 } /// ends role_assignments
5467 if ($this->tree[5] == "ROLES_OVERRIDES") {
5468 if ($this->level == 7) {
5469 switch ($tagName) {
5470 case "NAME":
5471 $this->info->tempname = $this->getContents();
5472 break;
5473 case "SHORTNAME":
5474 $this->info->tempshortname = $this->getContents();
5475 break;
5476 case "ID":
5477 $this->info->tempid = $this->getContents(); // temp roleid
5478 break;
5482 if ($this->level == 9) {
5483 switch ($tagName) {
5484 case "NAME":
5486 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5487 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5488 $this->info->tempname = $this->getContents(); // change to name of capability
5489 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5490 break;
5491 case "PERMISSION":
5492 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5493 break;
5494 case "TIMEMODIFIED":
5495 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5496 break;
5497 case "MODIFIERID":
5498 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5499 break;
5502 } /// ends role_overrides
5505 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
5506 //Speed up a lot (avoid parse all)
5507 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
5508 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
5509 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
5510 $this->finished = true;
5513 //Clear things
5514 $this->tree[$this->level] = '';
5515 $this->level--;
5516 $this->content = "";
5519 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
5520 function endElementSections($parser, $tagName) {
5521 //Check if we are into SECTIONS zone
5522 if ($this->tree[3] == "SECTIONS") {
5523 //if (trim($this->content)) //Debug
5524 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5525 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5526 //Dependig of different combinations, do different things
5527 if ($this->level == 4) {
5528 switch ($tagName) {
5529 case "SECTION":
5530 //We've finalized a section, get it
5531 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
5532 unset($this->info->tempsection);
5535 if ($this->level == 5) {
5536 switch ($tagName) {
5537 case "ID":
5538 $this->info->tempsection->id = $this->getContents();
5539 break;
5540 case "NUMBER":
5541 $this->info->tempsection->number = $this->getContents();
5542 break;
5543 case "SUMMARY":
5544 $this->info->tempsection->summary = $this->getContents();
5545 break;
5546 case "VISIBLE":
5547 $this->info->tempsection->visible = $this->getContents();
5548 break;
5551 if ($this->level == 6) {
5552 switch ($tagName) {
5553 case "MOD":
5554 if (!isset($this->info->tempmod->groupmode)) {
5555 $this->info->tempmod->groupmode = 0;
5557 if (!isset($this->info->tempmod->groupingid)) {
5558 $this->info->tempmod->groupingid = 0;
5560 if (!isset($this->info->tempmod->groupmembersonly)) {
5561 $this->info->tempmod->groupmembersonly = 0;
5563 if (!isset($this->info->tempmod->idnumber)) {
5564 $this->info->tempmod->idnumber = null;
5567 //We've finalized a mod, get it
5568 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
5569 $this->info->tempmod->type;
5570 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
5571 $this->info->tempmod->instance;
5572 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
5573 $this->info->tempmod->added;
5574 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
5575 $this->info->tempmod->score;
5576 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
5577 $this->info->tempmod->indent;
5578 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
5579 $this->info->tempmod->visible;
5580 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
5581 $this->info->tempmod->groupmode;
5582 $this->info->tempsection->mods[$this->info->tempmod->id]->groupingid =
5583 $this->info->tempmod->groupingid;
5584 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmembersonly =
5585 $this->info->tempmod->groupmembersonly;
5586 $this->info->tempsection->mods[$this->info->tempmod->id]->idnumber =
5587 $this->info->tempmod->idnumber;
5589 unset($this->info->tempmod);
5592 if ($this->level == 7) {
5593 switch ($tagName) {
5594 case "ID":
5595 $this->info->tempmod->id = $this->getContents();
5596 break;
5597 case "TYPE":
5598 $this->info->tempmod->type = $this->getContents();
5599 break;
5600 case "INSTANCE":
5601 $this->info->tempmod->instance = $this->getContents();
5602 break;
5603 case "ADDED":
5604 $this->info->tempmod->added = $this->getContents();
5605 break;
5606 case "SCORE":
5607 $this->info->tempmod->score = $this->getContents();
5608 break;
5609 case "INDENT":
5610 $this->info->tempmod->indent = $this->getContents();
5611 break;
5612 case "VISIBLE":
5613 $this->info->tempmod->visible = $this->getContents();
5614 break;
5615 case "GROUPMODE":
5616 $this->info->tempmod->groupmode = $this->getContents();
5617 break;
5618 case "GROUPINGID":
5619 $this->info->tempmod->groupingid = $this->getContents();
5620 break;
5621 case "GROUPMEMBERSONLY":
5622 $this->info->tempmod->groupmembersonly = $this->getContents();
5623 case "IDNUMBER":
5624 $this->info->tempmod->idnumber = $this->getContents();
5625 break;
5626 default:
5627 break;
5631 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
5633 if ($this->level == 9) {
5634 switch ($tagName) {
5635 case "NAME":
5636 $this->info->tempname = $this->getContents();
5637 break;
5638 case "SHORTNAME":
5639 $this->info->tempshortname = $this->getContents();
5640 break;
5641 case "ID":
5642 $this->info->tempid = $this->getContents(); // temp roleid
5643 break;
5647 if ($this->level == 11) {
5648 switch ($tagName) {
5649 case "USERID":
5650 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5652 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5654 $this->info->tempuser = $this->getContents();
5656 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5657 break;
5658 case "HIDDEN":
5659 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5660 break;
5661 case "TIMESTART":
5662 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5663 break;
5664 case "TIMEEND":
5665 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5666 break;
5667 case "TIMEMODIFIED":
5668 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5669 break;
5670 case "MODIFIERID":
5671 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5672 break;
5673 case "ENROL":
5674 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5675 break;
5676 case "SORTORDER":
5677 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5678 break;
5682 } /// ends role_assignments
5684 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
5685 if ($this->level == 9) {
5686 switch ($tagName) {
5687 case "NAME":
5688 $this->info->tempname = $this->getContents();
5689 break;
5690 case "SHORTNAME":
5691 $this->info->tempshortname = $this->getContents();
5692 break;
5693 case "ID":
5694 $this->info->tempid = $this->getContents(); // temp roleid
5695 break;
5699 if ($this->level == 11) {
5700 switch ($tagName) {
5701 case "NAME":
5703 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5704 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5705 $this->info->tempname = $this->getContents(); // change to name of capability
5706 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5707 break;
5708 case "PERMISSION":
5709 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5710 break;
5711 case "TIMEMODIFIED":
5712 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5713 break;
5714 case "MODIFIERID":
5715 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5716 break;
5719 } /// ends role_overrides
5723 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
5724 //Speed up a lot (avoid parse all)
5725 if ($tagName == "SECTIONS") {
5726 $this->finished = true;
5729 //Clear things
5730 $this->tree[$this->level] = "";
5731 $this->level--;
5732 $this->content = "";
5736 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
5737 function endElementFormatData($parser, $tagName) {
5738 //Check if we are into FORMATDATA zone
5739 if ($this->tree[3] == 'FORMATDATA') {
5740 if (!isset($this->temp)) {
5741 $this->temp = '';
5743 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5746 if($tagName=='FORMATDATA') {
5747 //Did we have any data? If not don't bother
5748 if($this->temp!='<FORMATDATA></FORMATDATA>') {
5749 //Prepend XML standard header to info gathered
5750 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5751 $this->temp='';
5753 //Call to xmlize for this portion of xml data (the FORMATDATA block)
5754 $this->info->format_data = xmlize($xml_data,0);
5756 //Stop parsing at end of FORMATDATA
5757 $this->finished=true;
5760 //Clear things
5761 $this->tree[$this->level] = "";
5762 $this->level--;
5763 $this->content = "";
5766 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
5767 function endElementMetacourse($parser, $tagName) {
5768 //Check if we are into METACOURSE zone
5769 if ($this->tree[3] == 'METACOURSE') {
5770 //if (trim($this->content)) //Debug
5771 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5772 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5773 //Dependig of different combinations, do different things
5774 if ($this->level == 5) {
5775 switch ($tagName) {
5776 case 'CHILD':
5777 //We've finalized a child, get it
5778 $this->info->childs[] = $this->info->tempmeta;
5779 unset($this->info->tempmeta);
5780 break;
5781 case 'PARENT':
5782 //We've finalized a parent, get it
5783 $this->info->parents[] = $this->info->tempmeta;
5784 unset($this->info->tempmeta);
5785 break;
5786 default:
5787 die($tagName);
5790 if ($this->level == 6) {
5791 switch ($tagName) {
5792 case 'ID':
5793 $this->info->tempmeta->id = $this->getContents();
5794 break;
5795 case 'IDNUMBER':
5796 $this->info->tempmeta->idnumber = $this->getContents();
5797 break;
5798 case 'SHORTNAME':
5799 $this->info->tempmeta->shortname = $this->getContents();
5800 break;
5805 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
5806 //Speed up a lot (avoid parse all)
5807 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
5808 $this->finished = true;
5811 //Clear things
5812 $this->tree[$this->level] = '';
5813 $this->level--;
5814 $this->content = "";
5817 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5818 function endElementGradebook($parser, $tagName) {
5819 //Check if we are into GRADEBOOK zone
5820 if ($this->tree[3] == "GRADEBOOK") {
5821 //if (trim($this->content)) //Debug
5822 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5823 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5824 //Acumulate data to info (content + close tag)
5825 //Reconvert: strip htmlchars again and trim to generate xml data
5826 if (!isset($this->temp)) {
5827 $this->temp = "";
5829 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5830 // We have finished outcome, grade_category or grade_item, reset accumulated
5831 // data because they are close tags
5832 if ($this->level == 4) {
5833 $this->temp = "";
5835 //If we've finished a grade item, xmlize it an save to db
5836 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
5837 //Prepend XML standard header to info gathered
5838 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5839 //Call to xmlize for this portion of xml data (one PREFERENCE)
5840 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5841 $data = xmlize($xml_data,0);
5842 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
5843 $this->counter++;
5844 //Save to db
5846 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
5847 null,$data);
5848 //Create returning info
5849 $this->info = $this->counter;
5850 //Reset temp
5852 unset($this->temp);
5855 //If we've finished a grade_category, xmlize it an save to db
5856 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
5857 //Prepend XML standard header to info gathered
5858 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5859 //Call to xmlize for this portion of xml data (one CATECORY)
5860 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5861 $data = xmlize($xml_data,0);
5862 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
5863 $this->counter++;
5864 //Save to db
5865 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
5866 null,$data);
5867 //Create returning info
5868 $this->info = $this->counter;
5869 //Reset temp
5870 unset($this->temp);
5873 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
5874 //Prepend XML standard header to info gathered
5875 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5876 //Call to xmlize for this portion of xml data (one CATECORY)
5877 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5878 $data = xmlize($xml_data,0);
5879 $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
5880 $this->counter++;
5881 //Save to db
5882 $status = backup_putid($this->preferences->backup_unique_code, 'grade_letters' ,$letter_id,
5883 null,$data);
5884 //Create returning info
5885 $this->info = $this->counter;
5886 //Reset temp
5887 unset($this->temp);
5890 //If we've finished a grade_outcome, xmlize it an save to db
5891 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
5892 //Prepend XML standard header to info gathered
5893 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5894 //Call to xmlize for this portion of xml data (one CATECORY)
5895 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5896 $data = xmlize($xml_data,0);
5897 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
5898 $this->counter++;
5899 //Save to db
5900 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
5901 null,$data);
5902 //Create returning info
5903 $this->info = $this->counter;
5904 //Reset temp
5905 unset($this->temp);
5908 //If we've finished a grade_outcomes_course, xmlize it an save to db
5909 if (($this->level == 5) and ($tagName == "GRADE_OUTCOMES_COURSE")) {
5910 //Prepend XML standard header to info gathered
5911 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5912 //Call to xmlize for this portion of xml data (one CATECORY)
5913 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5914 $data = xmlize($xml_data,0);
5915 $outcomes_course_id = $data["GRADE_OUTCOMES_COURSE"]["#"]["ID"]["0"]["#"];
5916 $this->counter++;
5917 //Save to db
5918 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_courses' ,$outcomes_course_id,
5919 null,$data);
5920 //Create returning info
5921 $this->info = $this->counter;
5922 //Reset temp
5923 unset($this->temp);
5926 if (($this->level == 5) and ($tagName == "GRADE_CATEGORIES_HISTORY")) {
5927 //Prepend XML standard header to info gathered
5928 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5929 //Call to xmlize for this portion of xml data (one PREFERENCE)
5930 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5931 $data = xmlize($xml_data,0);
5932 $id = $data["GRADE_CATEGORIES_HISTORY"]["#"]["ID"]["0"]["#"];
5933 $this->counter++;
5934 //Save to db
5936 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories_history', $id,
5937 null,$data);
5938 //Create returning info
5939 $this->info = $this->counter;
5940 //Reset temp
5942 unset($this->temp);
5945 if (($this->level == 5) and ($tagName == "GRADE_GRADES_HISTORY")) {
5946 //Prepend XML standard header to info gathered
5947 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5948 //Call to xmlize for this portion of xml data (one PREFERENCE)
5949 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5950 $data = xmlize($xml_data,0);
5951 $id = $data["GRADE_GRADES_HISTORY"]["#"]["ID"]["0"]["#"];
5952 $this->counter++;
5953 //Save to db
5955 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_history', $id,
5956 null,$data);
5957 //Create returning info
5958 $this->info = $this->counter;
5959 //Reset temp
5961 unset($this->temp);
5964 if (($this->level == 5) and ($tagName == "GRADE_ITEM_HISTORY")) {
5965 //Prepend XML standard header to info gathered
5966 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5967 //Call to xmlize for this portion of xml data (one PREFERENCE)
5968 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5969 $data = xmlize($xml_data,0);
5970 $id = $data["GRADE_ITEM_HISTORY"]["#"]["ID"]["0"]["#"];
5971 $this->counter++;
5972 //Save to db
5974 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items_history', $id,
5975 null,$data);
5976 //Create returning info
5977 $this->info = $this->counter;
5978 //Reset temp
5980 unset($this->temp);
5983 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME_HISTORY")) {
5984 //Prepend XML standard header to info gathered
5985 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5986 //Call to xmlize for this portion of xml data (one PREFERENCE)
5987 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5988 $data = xmlize($xml_data,0);
5989 $id = $data["GRADE_OUTCOME_HISTORY"]["#"]["ID"]["0"]["#"];
5990 $this->counter++;
5991 //Save to db
5993 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_history', $id,
5994 null,$data);
5995 //Create returning info
5996 $this->info = $this->counter;
5997 //Reset temp
5999 unset($this->temp);
6003 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
6004 //Speed up a lot (avoid parse all)
6005 if ($tagName == "GRADEBOOK" and $this->level == 3) {
6006 $this->finished = true;
6007 $this->counter = 0;
6010 //Clear things
6011 $this->tree[$this->level] = "";
6012 $this->level--;
6013 $this->content = "";
6017 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
6018 function endElementOldGradebook($parser, $tagName) {
6019 //Check if we are into GRADEBOOK zone
6020 if ($this->tree[3] == "GRADEBOOK") {
6021 //if (trim($this->content)) //Debug
6022 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6023 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6024 //Acumulate data to info (content + close tag)
6025 //Reconvert: strip htmlchars again and trim to generate xml data
6026 if (!isset($this->temp)) {
6027 $this->temp = "";
6029 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6030 //We have finished preferences, letters or categories, reset accumulated
6031 //data because they are close tags
6032 if ($this->level == 4) {
6033 $this->temp = "";
6035 //If we've finished a message, xmlize it an save to db
6036 if (($this->level == 5) and ($tagName == "GRADE_PREFERENCE")) {
6037 //Prepend XML standard header to info gathered
6038 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6039 //Call to xmlize for this portion of xml data (one PREFERENCE)
6040 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6041 $data = xmlize($xml_data,0);
6042 //echo strftime ("%X",time())."<p>"; //Debug
6043 //traverse_xmlize($data); //Debug
6044 //print_object ($GLOBALS['traverse_array']); //Debug
6045 //$GLOBALS['traverse_array']=""; //Debug
6046 //Now, save data to db. We'll use it later
6047 //Get id and status from data
6048 $preference_id = $data["GRADE_PREFERENCE"]["#"]["ID"]["0"]["#"];
6049 $this->counter++;
6050 //Save to db
6051 $status = backup_putid($this->preferences->backup_unique_code, 'grade_preferences', $preference_id,
6052 null,$data);
6053 //Create returning info
6054 $this->info = $this->counter;
6055 //Reset temp
6056 unset($this->temp);
6058 //If we've finished a grade_letter, xmlize it an save to db
6059 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
6060 //Prepend XML standard header to info gathered
6061 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6062 //Call to xmlize for this portion of xml data (one LETTER)
6063 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6064 $data = xmlize($xml_data,0);
6065 //echo strftime ("%X",time())."<p>"; //Debug
6066 //traverse_xmlize($data); //Debug
6067 //print_object ($GLOBALS['traverse_array']); //Debug
6068 //$GLOBALS['traverse_array']=""; //Debug
6069 //Now, save data to db. We'll use it later
6070 //Get id and status from data
6071 $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
6072 $this->counter++;
6073 //Save to db
6074 $status = backup_putid($this->preferences->backup_unique_code, 'grade_letter' ,$letter_id,
6075 null,$data);
6076 //Create returning info
6077 $this->info = $this->counter;
6078 //Reset temp
6079 unset($this->temp);
6082 //If we've finished a grade_category, xmlize it an save to db
6083 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
6084 //Prepend XML standard header to info gathered
6085 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6086 //Call to xmlize for this portion of xml data (one CATECORY)
6087 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6088 $data = xmlize($xml_data,0);
6089 //echo strftime ("%X",time())."<p>"; //Debug
6090 //traverse_xmlize($data); //Debug
6091 //print_object ($GLOBALS['traverse_array']); //Debug
6092 //$GLOBALS['traverse_array']=""; //Debug
6093 //Now, save data to db. We'll use it later
6094 //Get id and status from data
6095 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
6096 $this->counter++;
6097 //Save to db
6098 $status = backup_putid($this->preferences->backup_unique_code, 'grade_category' ,$category_id,
6099 null,$data);
6100 //Create returning info
6101 $this->info = $this->counter;
6102 //Reset temp
6103 unset($this->temp);
6107 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
6108 //Speed up a lot (avoid parse all)
6109 if ($tagName == "GRADEBOOK" and $this->level == 3) {
6110 $this->finished = true;
6111 $this->counter = 0;
6114 //Clear things
6115 $this->tree[$this->level] = "";
6116 $this->level--;
6117 $this->content = "";
6121 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
6122 function endElementUsers($parser, $tagName) {
6123 global $CFG;
6124 //Check if we are into USERS zone
6125 if ($this->tree[3] == "USERS") {
6126 //if (trim($this->content)) //Debug
6127 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6128 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6129 //Dependig of different combinations, do different things
6130 if ($this->level == 4) {
6131 switch ($tagName) {
6132 case "USER":
6133 //Increment counter
6134 $this->counter++;
6135 //Save to db, only save if record not already exist
6136 // if there already is an new_id for this entry, just use that new_id?
6137 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
6138 if (isset($newuser->new_id)) {
6139 $newid = $newuser->new_id;
6140 } else {
6141 $newid = null;
6144 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
6145 $newid,$this->info->tempuser);
6147 //Do some output
6148 if ($this->counter % 10 == 0) {
6149 if (!defined('RESTORE_SILENTLY')) {
6150 echo ".";
6151 if ($this->counter % 200 == 0) {
6152 echo "<br />";
6155 backup_flush(300);
6158 //Delete temp obejct
6159 unset($this->info->tempuser);
6160 break;
6164 if ($this->level == 5) {
6165 switch ($tagName) {
6166 case "ID":
6167 $this->info->users[$this->getContents()] = $this->getContents();
6168 $this->info->tempuser->id = $this->getContents();
6169 break;
6170 case "AUTH":
6171 $this->info->tempuser->auth = $this->getContents();
6172 break;
6173 case "CONFIRMED":
6174 $this->info->tempuser->confirmed = $this->getContents();
6175 break;
6176 case "POLICYAGREED":
6177 $this->info->tempuser->policyagreed = $this->getContents();
6178 break;
6179 case "DELETED":
6180 $this->info->tempuser->deleted = $this->getContents();
6181 break;
6182 case "USERNAME":
6183 $this->info->tempuser->username = $this->getContents();
6184 break;
6185 case "PASSWORD":
6186 $this->info->tempuser->password = $this->getContents();
6187 break;
6188 case "IDNUMBER":
6189 $this->info->tempuser->idnumber = $this->getContents();
6190 break;
6191 case "FIRSTNAME":
6192 $this->info->tempuser->firstname = $this->getContents();
6193 break;
6194 case "LASTNAME":
6195 $this->info->tempuser->lastname = $this->getContents();
6196 break;
6197 case "EMAIL":
6198 $this->info->tempuser->email = $this->getContents();
6199 break;
6200 case "EMAILSTOP":
6201 $this->info->tempuser->emailstop = $this->getContents();
6202 break;
6203 case "ICQ":
6204 $this->info->tempuser->icq = $this->getContents();
6205 break;
6206 case "SKYPE":
6207 $this->info->tempuser->skype = $this->getContents();
6208 break;
6209 case "AIM":
6210 $this->info->tempuser->aim = $this->getContents();
6211 break;
6212 case "YAHOO":
6213 $this->info->tempuser->yahoo = $this->getContents();
6214 break;
6215 case "MSN":
6216 $this->info->tempuser->msn = $this->getContents();
6217 break;
6218 case "PHONE1":
6219 $this->info->tempuser->phone1 = $this->getContents();
6220 break;
6221 case "PHONE2":
6222 $this->info->tempuser->phone2 = $this->getContents();
6223 break;
6224 case "INSTITUTION":
6225 $this->info->tempuser->institution = $this->getContents();
6226 break;
6227 case "DEPARTMENT":
6228 $this->info->tempuser->department = $this->getContents();
6229 break;
6230 case "ADDRESS":
6231 $this->info->tempuser->address = $this->getContents();
6232 break;
6233 case "CITY":
6234 $this->info->tempuser->city = $this->getContents();
6235 break;
6236 case "COUNTRY":
6237 $this->info->tempuser->country = $this->getContents();
6238 break;
6239 case "LANG":
6240 $this->info->tempuser->lang = $this->getContents();
6241 break;
6242 case "THEME":
6243 $this->info->tempuser->theme = $this->getContents();
6244 break;
6245 case "TIMEZONE":
6246 $this->info->tempuser->timezone = $this->getContents();
6247 break;
6248 case "FIRSTACCESS":
6249 $this->info->tempuser->firstaccess = $this->getContents();
6250 break;
6251 case "LASTACCESS":
6252 $this->info->tempuser->lastaccess = $this->getContents();
6253 break;
6254 case "LASTLOGIN":
6255 $this->info->tempuser->lastlogin = $this->getContents();
6256 break;
6257 case "CURRENTLOGIN":
6258 $this->info->tempuser->currentlogin = $this->getContents();
6259 break;
6260 case "LASTIP":
6261 $this->info->tempuser->lastip = $this->getContents();
6262 break;
6263 case "SECRET":
6264 $this->info->tempuser->secret = $this->getContents();
6265 break;
6266 case "PICTURE":
6267 $this->info->tempuser->picture = $this->getContents();
6268 break;
6269 case "URL":
6270 $this->info->tempuser->url = $this->getContents();
6271 break;
6272 case "DESCRIPTION":
6273 $this->info->tempuser->description = $this->getContents();
6274 break;
6275 case "MAILFORMAT":
6276 $this->info->tempuser->mailformat = $this->getContents();
6277 break;
6278 case "MAILDIGEST":
6279 $this->info->tempuser->maildigest = $this->getContents();
6280 break;
6281 case "MAILDISPLAY":
6282 $this->info->tempuser->maildisplay = $this->getContents();
6283 break;
6284 case "HTMLEDITOR":
6285 $this->info->tempuser->htmleditor = $this->getContents();
6286 break;
6287 case "AJAX":
6288 $this->info->tempuser->ajax = $this->getContents();
6289 break;
6290 case "AUTOSUBSCRIBE":
6291 $this->info->tempuser->autosubscribe = $this->getContents();
6292 break;
6293 case "TRACKFORUMS":
6294 $this->info->tempuser->trackforums = $this->getContents();
6295 break;
6296 case "MNETHOSTURL":
6297 $this->info->tempuser->mnethosturl = $this->getContents();
6298 break;
6299 case "TIMEMODIFIED":
6300 $this->info->tempuser->timemodified = $this->getContents();
6301 break;
6302 default:
6303 break;
6307 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
6308 switch ($tagName) {
6309 case "ROLE":
6310 //We've finalized a role, get it
6311 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
6312 unset($this->info->temprole);
6313 break;
6314 case "USER_PREFERENCE":
6315 //We've finalized a user_preference, get it
6316 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
6317 unset($this->info->tempuserpreference);
6318 break;
6319 case "USER_CUSTOM_PROFILE_FIELD":
6320 //We've finalized a user_custom_profile_field, get it
6321 $this->info->tempuser->user_custom_profile_fields[] = $this->info->tempusercustomprofilefield;
6322 unset($this->info->tempusercustomprofilefield);
6323 break;
6324 case "USER_TAG":
6325 //We've finalized a user_tag, get it
6326 $this->info->tempuser->user_tags[] = $this->info->tempusertag;
6327 unset($this->info->tempusertag);
6328 break;
6329 default:
6330 break;
6334 if ($this->level == 7 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
6335 /// If we are reading roles
6336 if($this->tree[6] == 'ROLE') {
6337 switch ($tagName) {
6338 case "TYPE":
6339 $this->info->temprole->type = $this->getContents();
6340 break;
6341 case "AUTHORITY":
6342 $this->info->temprole->authority = $this->getContents();
6343 break;
6344 case "TEA_ROLE":
6345 $this->info->temprole->tea_role = $this->getContents();
6346 break;
6347 case "EDITALL":
6348 $this->info->temprole->editall = $this->getContents();
6349 break;
6350 case "TIMESTART":
6351 $this->info->temprole->timestart = $this->getContents();
6352 break;
6353 case "TIMEEND":
6354 $this->info->temprole->timeend = $this->getContents();
6355 break;
6356 case "TIMEMODIFIED":
6357 $this->info->temprole->timemodified = $this->getContents();
6358 break;
6359 case "TIMESTART":
6360 $this->info->temprole->timestart = $this->getContents();
6361 break;
6362 case "TIMEEND":
6363 $this->info->temprole->timeend = $this->getContents();
6364 break;
6365 case "TIME":
6366 $this->info->temprole->time = $this->getContents();
6367 break;
6368 case "TIMEACCESS":
6369 $this->info->temprole->timeaccess = $this->getContents();
6370 break;
6371 case "ENROL":
6372 $this->info->temprole->enrol = $this->getContents();
6373 break;
6374 default:
6375 break;
6377 /// If we are reading user_preferences
6378 } else if ($this->tree[6] == 'USER_PREFERENCE') {
6379 switch ($tagName) {
6380 case "NAME":
6381 $this->info->tempuserpreference->name = $this->getContents();
6382 break;
6383 case "VALUE":
6384 $this->info->tempuserpreference->value = $this->getContents();
6385 break;
6386 default:
6387 break;
6389 /// If we are reading user_custom_profile_fields
6390 } else if ($this->tree[6] == 'USER_CUSTOM_PROFILE_FIELD') {
6391 switch ($tagName) {
6392 case "FIELD_NAME":
6393 $this->info->tempusercustomprofilefield->field_name = $this->getContents();
6394 break;
6395 case "FIELD_TYPE":
6396 $this->info->tempusercustomprofilefield->field_type = $this->getContents();
6397 break;
6398 case "FIELD_DATA":
6399 $this->info->tempusercustomprofilefield->field_data = $this->getContents();
6400 break;
6401 default:
6402 break;
6404 /// If we are reading user_tags
6405 } else if ($this->tree[6] == 'USER_TAG') {
6406 switch ($tagName) {
6407 case "NAME":
6408 $this->info->tempusertag->name = $this->getContents();
6409 break;
6410 case "RAWNAME":
6411 $this->info->tempusertag->rawname = $this->getContents();
6412 break;
6413 default:
6414 break;
6419 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
6421 if ($this->level == 7) {
6422 switch ($tagName) {
6423 case "NAME":
6424 $this->info->tempname = $this->getContents();
6425 break;
6426 case "SHORTNAME":
6427 $this->info->tempshortname = $this->getContents();
6428 break;
6429 case "ID":
6430 $this->info->tempid = $this->getContents(); // temp roleid
6431 break;
6435 if ($this->level == 9) {
6437 switch ($tagName) {
6438 case "USERID":
6439 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
6441 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
6443 $this->info->tempuserid = $this->getContents();
6445 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
6446 break;
6447 case "HIDDEN":
6448 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
6449 break;
6450 case "TIMESTART":
6451 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
6452 break;
6453 case "TIMEEND":
6454 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
6455 break;
6456 case "TIMEMODIFIED":
6457 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
6458 break;
6459 case "MODIFIERID":
6460 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
6461 break;
6462 case "ENROL":
6463 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
6464 break;
6465 case "SORTORDER":
6466 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
6467 break;
6471 } /// ends role_assignments
6473 if ($this->tree[5] == "ROLES_OVERRIDES") {
6474 if ($this->level == 7) {
6475 switch ($tagName) {
6476 case "NAME":
6477 $this->info->tempname = $this->getContents();
6478 break;
6479 case "SHORTNAME":
6480 $this->info->tempshortname = $this->getContents();
6481 break;
6482 case "ID":
6483 $this->info->tempid = $this->getContents(); // temp roleid
6484 break;
6488 if ($this->level == 9) {
6489 switch ($tagName) {
6490 case "NAME":
6492 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
6493 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
6494 $this->info->tempname = $this->getContents(); // change to name of capability
6495 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
6496 break;
6497 case "PERMISSION":
6498 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
6499 break;
6500 case "TIMEMODIFIED":
6501 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
6502 break;
6503 case "MODIFIERID":
6504 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
6505 break;
6508 } /// ends role_overrides
6510 } // closes if this->tree[3]=="users"
6512 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
6513 //Speed up a lot (avoid parse all)
6514 if ($tagName == "USERS" and $this->level == 3) {
6515 $this->finished = true;
6516 $this->counter = 0;
6519 //Clear things
6520 $this->tree[$this->level] = "";
6521 $this->level--;
6522 $this->content = "";
6526 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
6527 function endElementMessages($parser, $tagName) {
6528 //Check if we are into MESSAGES zone
6529 if ($this->tree[3] == "MESSAGES") {
6530 //if (trim($this->content)) //Debug
6531 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6532 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6533 //Acumulate data to info (content + close tag)
6534 //Reconvert: strip htmlchars again and trim to generate xml data
6535 if (!isset($this->temp)) {
6536 $this->temp = "";
6538 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6539 //If we've finished a message, xmlize it an save to db
6540 if (($this->level == 4) and ($tagName == "MESSAGE")) {
6541 //Prepend XML standard header to info gathered
6542 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6543 //Call to xmlize for this portion of xml data (one MESSAGE)
6544 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6545 $data = xmlize($xml_data,0);
6546 //echo strftime ("%X",time())."<p>"; //Debug
6547 //traverse_xmlize($data); //Debug
6548 //print_object ($GLOBALS['traverse_array']); //Debug
6549 //$GLOBALS['traverse_array']=""; //Debug
6550 //Now, save data to db. We'll use it later
6551 //Get id and status from data
6552 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
6553 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
6554 if ($message_status == "READ") {
6555 $table = "message_read";
6556 } else {
6557 $table = "message";
6559 $this->counter++;
6560 //Save to db
6561 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
6562 null,$data);
6563 //Create returning info
6564 $this->info = $this->counter;
6565 //Reset temp
6566 unset($this->temp);
6568 //If we've finished a contact, xmlize it an save to db
6569 if (($this->level == 5) and ($tagName == "CONTACT")) {
6570 //Prepend XML standard header to info gathered
6571 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6572 //Call to xmlize for this portion of xml data (one MESSAGE)
6573 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6574 $data = xmlize($xml_data,0);
6575 //echo strftime ("%X",time())."<p>"; //Debug
6576 //traverse_xmlize($data); //Debug
6577 //print_object ($GLOBALS['traverse_array']); //Debug
6578 //$GLOBALS['traverse_array']=""; //Debug
6579 //Now, save data to db. We'll use it later
6580 //Get id and status from data
6581 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
6582 $this->counter++;
6583 //Save to db
6584 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
6585 null,$data);
6586 //Create returning info
6587 $this->info = $this->counter;
6588 //Reset temp
6589 unset($this->temp);
6593 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
6594 //Speed up a lot (avoid parse all)
6595 if ($tagName == "MESSAGES" and $this->level == 3) {
6596 $this->finished = true;
6597 $this->counter = 0;
6600 //Clear things
6601 $this->tree[$this->level] = "";
6602 $this->level--;
6603 $this->content = "";
6607 //This is the endTag handler we use where we are reading the blogs zone (todo="BLOGS")
6608 function endElementBlogs($parser, $tagName) {
6609 //Check if we are into BLOGS zone
6610 if ($this->tree[3] == "BLOGS") {
6611 //if (trim($this->content)) //Debug
6612 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6613 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6614 //Acumulate data to info (content + close tag)
6615 //Reconvert: strip htmlchars again and trim to generate xml data
6616 if (!isset($this->temp)) {
6617 $this->temp = "";
6619 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6620 //If we've finished a blog, xmlize it an save to db
6621 if (($this->level == 4) and ($tagName == "BLOG")) {
6622 //Prepend XML standard header to info gathered
6623 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6624 //Call to xmlize for this portion of xml data (one BLOG)
6625 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6626 $data = xmlize($xml_data,0);
6627 //echo strftime ("%X",time())."<p>"; //Debug
6628 //traverse_xmlize($data); //Debug
6629 //print_object ($GLOBALS['traverse_array']); //Debug
6630 //$GLOBALS['traverse_array']=""; //Debug
6631 //Now, save data to db. We'll use it later
6632 //Get id from data
6633 $blog_id = $data["BLOG"]["#"]["ID"]["0"]["#"];
6634 $this->counter++;
6635 //Save to db
6636 $status = backup_putid($this->preferences->backup_unique_code, 'blog', $blog_id,
6637 null,$data);
6638 //Create returning info
6639 $this->info = $this->counter;
6640 //Reset temp
6641 unset($this->temp);
6645 //Stop parsing if todo = BLOGS and tagName = BLOGS (end of the tag, of course)
6646 //Speed up a lot (avoid parse all)
6647 if ($tagName == "BLOGS" and $this->level == 3) {
6648 $this->finished = true;
6649 $this->counter = 0;
6652 //Clear things
6653 $this->tree[$this->level] = "";
6654 $this->level--;
6655 $this->content = "";
6659 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
6660 function endElementQuestions($parser, $tagName) {
6661 //Check if we are into QUESTION_CATEGORIES zone
6662 if ($this->tree[3] == "QUESTION_CATEGORIES") {
6663 //if (trim($this->content)) //Debug
6664 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6665 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6666 //Acumulate data to info (content + close tag)
6667 //Reconvert: strip htmlchars again and trim to generate xml data
6668 if (!isset($this->temp)) {
6669 $this->temp = "";
6671 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6672 //If we've finished a mod, xmlize it an save to db
6673 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
6674 //Prepend XML standard header to info gathered
6675 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6676 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
6677 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6678 $data = xmlize($xml_data,0);
6679 //echo strftime ("%X",time())."<p>"; //Debug
6680 //traverse_xmlize($data); //Debug
6681 //print_object ($GLOBALS['traverse_array']); //Debug
6682 //$GLOBALS['traverse_array']=""; //Debug
6683 //Now, save data to db. We'll use it later
6684 //Get id from data
6685 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
6686 //Save to db
6687 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
6688 null,$data);
6689 //Create returning info
6690 $ret_info = new object();
6691 $ret_info->id = $category_id;
6692 $this->info[] = $ret_info;
6693 //Reset temp
6694 unset($this->temp);
6698 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
6699 //Speed up a lot (avoid parse all)
6700 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
6701 $this->finished = true;
6704 //Clear things
6705 $this->tree[$this->level] = "";
6706 $this->level--;
6707 $this->content = "";
6711 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
6712 function endElementScales($parser, $tagName) {
6713 //Check if we are into SCALES zone
6714 if ($this->tree[3] == "SCALES") {
6715 //if (trim($this->content)) //Debug
6716 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6717 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6718 //Acumulate data to info (content + close tag)
6719 //Reconvert: strip htmlchars again and trim to generate xml data
6720 if (!isset($this->temp)) {
6721 $this->temp = "";
6723 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6724 //If we've finished a scale, xmlize it an save to db
6725 if (($this->level == 4) and ($tagName == "SCALE")) {
6726 //Prepend XML standard header to info gathered
6727 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6728 //Call to xmlize for this portion of xml data (one SCALE)
6729 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6730 $data = xmlize($xml_data,0);
6731 //echo strftime ("%X",time())."<p>"; //Debug
6732 //traverse_xmlize($data); //Debug
6733 //print_object ($GLOBALS['traverse_array']); //Debug
6734 //$GLOBALS['traverse_array']=""; //Debug
6735 //Now, save data to db. We'll use it later
6736 //Get id and from data
6737 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
6738 //Save to db
6739 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
6740 null,$data);
6741 //Create returning info
6742 $ret_info = new object();
6743 $ret_info->id = $scale_id;
6744 $this->info[] = $ret_info;
6745 //Reset temp
6746 unset($this->temp);
6750 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
6751 //Speed up a lot (avoid parse all)
6752 if ($tagName == "SCALES" and $this->level == 3) {
6753 $this->finished = true;
6756 //Clear things
6757 $this->tree[$this->level] = "";
6758 $this->level--;
6759 $this->content = "";
6763 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
6764 function endElementGroups($parser, $tagName) {
6765 //Check if we are into GROUPS zone
6766 if ($this->tree[3] == "GROUPS") {
6767 //if (trim($this->content)) //Debug
6768 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6769 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6770 //Acumulate data to info (content + close tag)
6771 //Reconvert: strip htmlchars again and trim to generate xml data
6772 if (!isset($this->temp)) {
6773 $this->temp = "";
6775 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6776 //If we've finished a group, xmlize it an save to db
6777 if (($this->level == 4) and ($tagName == "GROUP")) {
6778 //Prepend XML standard header to info gathered
6779 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6780 //Call to xmlize for this portion of xml data (one GROUP)
6781 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6782 $data = xmlize($xml_data,0);
6783 //echo strftime ("%X",time())."<p>"; //Debug
6784 //traverse_xmlize($data); //Debug
6785 //print_object ($GLOBALS['traverse_array']); //Debug
6786 //$GLOBALS['traverse_array']=""; //Debug
6787 //Now, save data to db. We'll use it later
6788 //Get id and from data
6789 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
6790 //Save to db
6791 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
6792 null,$data);
6793 //Create returning info
6794 $ret_info = new Object();
6795 $ret_info->id = $group_id;
6796 $this->info[] = $ret_info;
6797 //Reset temp
6798 unset($this->temp);
6802 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
6803 //Speed up a lot (avoid parse all)
6804 if ($tagName == "GROUPS" and $this->level == 3) {
6805 $this->finished = true;
6808 //Clear things
6809 $this->tree[$this->level] = "";
6810 $this->level--;
6811 $this->content = "";
6815 //This is the endTag handler we use where we are reading the groupings zone (todo="GROUPINGS")
6816 function endElementGroupings($parser, $tagName) {
6817 //Check if we are into GROUPINGS zone
6818 if ($this->tree[3] == "GROUPINGS") {
6819 //Acumulate data to info (content + close tag)
6820 //Reconvert: strip htmlchars again and trim to generate xml data
6821 if (!isset($this->temp)) {
6822 $this->temp = "";
6824 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6825 //If we've finished a group, xmlize it an save to db
6826 if (($this->level == 4) and ($tagName == "GROUPING")) {
6827 //Prepend XML standard header to info gathered
6828 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6829 //Call to xmlize for this portion of xml data (one GROUPING)
6830 $data = xmlize($xml_data,0);
6831 //Now, save data to db. We'll use it later
6832 //Get id and from data
6833 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
6834 //Save to db
6835 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
6836 null,$data);
6837 //Create returning info
6838 $ret_info = new Object();
6839 $ret_info->id = $grouping_id;
6840 $this->info[] = $ret_info;
6841 //Reset temp
6842 unset($this->temp);
6846 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6847 //Speed up a lot (avoid parse all)
6848 if ($tagName == "GROUPINGS" and $this->level == 3) {
6849 $this->finished = true;
6852 //Clear things
6853 $this->tree[$this->level] = "";
6854 $this->level--;
6855 $this->content = "";
6859 //This is the endTag handler we use where we are reading the groupingsgroups zone (todo="GROUPINGGROUPS")
6860 function endElementGroupingsGroups($parser, $tagName) {
6861 //Check if we are into GROUPINGSGROUPS zone
6862 if ($this->tree[3] == "GROUPINGSGROUPS") {
6863 //Acumulate data to info (content + close tag)
6864 //Reconvert: strip htmlchars again and trim to generate xml data
6865 if (!isset($this->temp)) {
6866 $this->temp = "";
6868 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6869 //If we've finished a group, xmlize it an save to db
6870 if (($this->level == 4) and ($tagName == "GROUPINGGROUP")) {
6871 //Prepend XML standard header to info gathered
6872 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6873 //Call to xmlize for this portion of xml data (one GROUPING)
6874 $data = xmlize($xml_data,0);
6875 //Now, save data to db. We'll use it later
6876 //Get id and from data
6877 $groupinggroup_id = $data["GROUPINGGROUP"]["#"]["ID"]["0"]["#"];
6878 //Save to db
6879 $status = backup_putid($this->preferences->backup_unique_code,"groupingsgroups",$groupinggroup_id,
6880 null,$data);
6881 //Create returning info
6882 $ret_info = new Object();
6883 $ret_info->id = $groupinggroup_id;
6884 $this->info[] = $ret_info;
6885 //Reset temp
6886 unset($this->temp);
6890 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6891 //Speed up a lot (avoid parse all)
6892 if ($tagName == "GROUPINGSGROUPS" and $this->level == 3) {
6893 $this->finished = true;
6896 //Clear things
6897 $this->tree[$this->level] = "";
6898 $this->level--;
6899 $this->content = "";
6903 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
6904 function endElementEvents($parser, $tagName) {
6905 //Check if we are into EVENTS zone
6906 if ($this->tree[3] == "EVENTS") {
6907 //if (trim($this->content)) //Debug
6908 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6909 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6910 //Acumulate data to info (content + close tag)
6911 //Reconvert: strip htmlchars again and trim to generate xml data
6912 if (!isset($this->temp)) {
6913 $this->temp = "";
6915 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6916 //If we've finished a event, xmlize it an save to db
6917 if (($this->level == 4) and ($tagName == "EVENT")) {
6918 //Prepend XML standard header to info gathered
6919 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6920 //Call to xmlize for this portion of xml data (one EVENT)
6921 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6922 $data = xmlize($xml_data,0);
6923 //echo strftime ("%X",time())."<p>"; //Debug
6924 //traverse_xmlize($data); //Debug
6925 //print_object ($GLOBALS['traverse_array']); //Debug
6926 //$GLOBALS['traverse_array']=""; //Debug
6927 //Now, save data to db. We'll use it later
6928 //Get id and from data
6929 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
6930 //Save to db
6931 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
6932 null,$data);
6933 //Create returning info
6934 $ret_info = new object();
6935 $ret_info->id = $event_id;
6936 $this->info[] = $ret_info;
6937 //Reset temp
6938 unset($this->temp);
6942 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
6943 //Speed up a lot (avoid parse all)
6944 if ($tagName == "EVENTS" and $this->level == 3) {
6945 $this->finished = true;
6948 //Clear things
6949 $this->tree[$this->level] = "";
6950 $this->level--;
6951 $this->content = "";
6955 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
6956 function endElementModules($parser, $tagName) {
6957 //Check if we are into MODULES zone
6958 if ($this->tree[3] == "MODULES") {
6959 //if (trim($this->content)) //Debug
6960 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6961 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6962 //Acumulate data to info (content + close tag)
6963 //Reconvert: strip htmlchars again and trim to generate xml data
6964 if (!isset($this->temp)) {
6965 $this->temp = "";
6967 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6968 //If we've finished a mod, xmlize it an save to db
6969 if (($this->level == 4) and ($tagName == "MOD")) {
6970 //Prepend XML standard header to info gathered
6971 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6972 //Call to xmlize for this portion of xml data (one MOD)
6973 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6974 $data = xmlize($xml_data,0);
6975 //echo strftime ("%X",time())."<p>"; //Debug
6976 //traverse_xmlize($data); //Debug
6977 //print_object ($GLOBALS['traverse_array']); //Debug
6978 //$GLOBALS['traverse_array']=""; //Debug
6979 //Now, save data to db. We'll use it later
6980 //Get id and modtype from data
6981 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
6982 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
6983 //Only if we've selected to restore it
6984 if (!empty($this->preferences->mods[$mod_type]->restore)) {
6985 //Save to db
6986 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
6987 null,$data);
6988 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6989 //Create returning info
6990 $ret_info = new object();
6991 $ret_info->id = $mod_id;
6992 $ret_info->modtype = $mod_type;
6993 $this->info[] = $ret_info;
6995 //Reset temp
6996 unset($this->temp);
7002 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
7003 //Speed up a lot (avoid parse all)
7004 if ($tagName == "MODULES" and $this->level == 3) {
7005 $this->finished = true;
7008 //Clear things
7009 $this->tree[$this->level] = "";
7010 $this->level--;
7011 $this->content = "";
7015 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
7016 function endElementLogs($parser, $tagName) {
7017 //Check if we are into LOGS zone
7018 if ($this->tree[3] == "LOGS") {
7019 //if (trim($this->content)) //Debug
7020 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7021 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7022 //Acumulate data to info (content + close tag)
7023 //Reconvert: strip htmlchars again and trim to generate xml data
7024 if (!isset($this->temp)) {
7025 $this->temp = "";
7027 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7028 //If we've finished a log, xmlize it an save to db
7029 if (($this->level == 4) and ($tagName == "LOG")) {
7030 //Prepend XML standard header to info gathered
7031 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7032 //Call to xmlize for this portion of xml data (one LOG)
7033 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7034 $data = xmlize($xml_data,0);
7035 //echo strftime ("%X",time())."<p>"; //Debug
7036 //traverse_xmlize($data); //Debug
7037 //print_object ($GLOBALS['traverse_array']); //Debug
7038 //$GLOBALS['traverse_array']=""; //Debug
7039 //Now, save data to db. We'll use it later
7040 //Get id and modtype from data
7041 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
7042 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
7043 //We only save log entries from backup file if they are:
7044 // - Course logs
7045 // - User logs
7046 // - Module logs about one restored module
7047 if ($log_module == "course" or
7048 $log_module == "user" or
7049 $this->preferences->mods[$log_module]->restore) {
7050 //Increment counter
7051 $this->counter++;
7052 //Save to db
7053 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
7054 null,$data);
7055 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
7056 //Create returning info
7057 $this->info = $this->counter;
7059 //Reset temp
7060 unset($this->temp);
7064 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
7065 //Speed up a lot (avoid parse all)
7066 if ($tagName == "LOGS" and $this->level == 3) {
7067 $this->finished = true;
7068 $this->counter = 0;
7071 //Clear things
7072 $this->tree[$this->level] = "";
7073 $this->level--;
7074 $this->content = "";
7078 //This is the endTag default handler we use when todo is undefined
7079 function endElement($parser, $tagName) {
7080 if (trim($this->content)) //Debug
7081 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7082 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7084 //Clear things
7085 $this->tree[$this->level] = "";
7086 $this->level--;
7087 $this->content = "";
7090 //This is the handler to read data contents (simple accumule it)
7091 function characterData($parser, $data) {
7092 $this->content .= $data;
7096 //This function executes the MoodleParser
7097 function restore_read_xml ($xml_file,$todo,$preferences) {
7099 $status = true;
7101 $xml_parser = xml_parser_create('UTF-8');
7102 $moodle_parser = new MoodleParser();
7103 $moodle_parser->todo = $todo;
7104 $moodle_parser->preferences = $preferences;
7105 xml_set_object($xml_parser,$moodle_parser);
7106 //Depending of the todo we use some element_handler or another
7107 if ($todo == "INFO") {
7108 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
7109 } else if ($todo == "ROLES") {
7110 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
7111 } else if ($todo == "COURSE_HEADER") {
7112 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
7113 } else if ($todo == 'BLOCKS') {
7114 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
7115 } else if ($todo == "SECTIONS") {
7116 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
7117 } else if ($todo == 'FORMATDATA') {
7118 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
7119 } else if ($todo == "METACOURSE") {
7120 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
7121 } else if ($todo == "GRADEBOOK") {
7122 if ($preferences->backup_version > 2007090500) {
7123 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
7124 } else {
7125 xml_set_element_handler($xml_parser, "startElementOldGradebook", "endElementOldGradebook");
7127 } else if ($todo == "USERS") {
7128 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
7129 } else if ($todo == "MESSAGES") {
7130 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
7131 } else if ($todo == "BLOGS") {
7132 xml_set_element_handler($xml_parser, "startElementBlogs", "endElementBlogs");
7133 } else if ($todo == "QUESTIONS") {
7134 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
7135 } else if ($todo == "SCALES") {
7136 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
7137 } else if ($todo == "GROUPS") {
7138 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
7139 } else if ($todo == "GROUPINGS") {
7140 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
7141 } else if ($todo == "GROUPINGSGROUPS") {
7142 xml_set_element_handler($xml_parser, "startElementGroupingsGroups", "endElementGroupingsGroups");
7143 } else if ($todo == "EVENTS") {
7144 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
7145 } else if ($todo == "MODULES") {
7146 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
7147 } else if ($todo == "LOGS") {
7148 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
7149 } else {
7150 //Define default handlers (must no be invoked when everything become finished)
7151 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
7153 xml_set_character_data_handler($xml_parser, "characterData");
7154 $fp = fopen($xml_file,"r")
7155 or $status = false;
7156 if ($status) {
7157 // MDL-9290 performance improvement on reading large xml
7158 $lasttime = time(); // crmas
7159 while ($data = fread($fp, 4096) and !$moodle_parser->finished) {
7161 if ((time() - $lasttime) > 5) {
7162 $lasttime = time();
7163 backup_flush(1);
7166 xml_parse($xml_parser, $data, feof($fp))
7167 or die(sprintf("XML error: %s at line %d",
7168 xml_error_string(xml_get_error_code($xml_parser)),
7169 xml_get_current_line_number($xml_parser)));
7171 fclose($fp);
7173 //Get info from parser
7174 $info = $moodle_parser->info;
7176 //Clear parser mem
7177 xml_parser_free($xml_parser);
7179 if ($status && !empty($info)) {
7180 return $info;
7181 } else {
7182 return $status;
7187 * @param string $errorstr passed by reference, if silent is true,
7188 * errorstr will be populated and this function will return false rather than calling error() or notify()
7189 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
7190 * redirect to the next step in the restore process, instead will return $backup_unique_code
7192 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
7194 global $CFG, $SESSION;
7196 //Prepend dataroot to variable to have the absolute path
7197 $file = $CFG->dataroot."/".$file;
7199 if (!defined('RESTORE_SILENTLY')) {
7200 //Start the main table
7201 echo "<table cellpadding=\"5\">";
7202 echo "<tr><td>";
7204 //Start the mail ul
7205 echo "<ul>";
7208 //Check the file exists
7209 if (!is_file($file)) {
7210 if (!defined('RESTORE_SILENTLY')) {
7211 error ("File not exists ($file)");
7212 } else {
7213 $errorstr = "File not exists ($file)";
7214 return false;
7218 //Check the file name ends with .zip
7219 if (!substr($file,-4) == ".zip") {
7220 if (!defined('RESTORE_SILENTLY')) {
7221 error ("File has an incorrect extension");
7222 } else {
7223 $errorstr = 'File has an incorrect extension';
7224 return false;
7228 //Now calculate the unique_code for this restore
7229 $backup_unique_code = time();
7231 //Now check and create the backup dir (if it doesn't exist)
7232 if (!defined('RESTORE_SILENTLY')) {
7233 echo "<li>".get_string("creatingtemporarystructures").'</li>';
7235 $status = check_and_create_backup_dir($backup_unique_code);
7236 //Empty dir
7237 if ($status) {
7238 $status = clear_backup_dir($backup_unique_code);
7241 //Now delete old data and directories under dataroot/temp/backup
7242 if ($status) {
7243 if (!defined('RESTORE_SILENTLY')) {
7244 echo "<li>".get_string("deletingolddata").'</li>';
7246 $status = backup_delete_old_data();
7249 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
7250 if ($status) {
7251 if (!defined('RESTORE_SILENTLY')) {
7252 echo "<li>".get_string("copyingzipfile").'</li>';
7254 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
7255 if (!defined('RESTORE_SILENTLY')) {
7256 notify("Error copying backup file. Invalid name or bad perms.");
7257 } else {
7258 $errorstr = "Error copying backup file. Invalid name or bad perms";
7259 return false;
7264 //Now unzip the file
7265 if ($status) {
7266 if (!defined('RESTORE_SILENTLY')) {
7267 echo "<li>".get_string("unzippingbackup").'</li>';
7269 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
7270 if (!defined('RESTORE_SILENTLY')) {
7271 notify("Error unzipping backup file. Invalid zip file.");
7272 } else {
7273 $errorstr = "Error unzipping backup file. Invalid zip file.";
7274 return false;
7279 //Check for Blackboard backups and convert
7280 if ($status){
7281 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
7282 if (!defined('RESTORE_SILENTLY')) {
7283 echo "<li>".get_string("checkingforbbexport").'</li>';
7285 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
7288 //Now check for the moodle.xml file
7289 if ($status) {
7290 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
7291 if (!defined('RESTORE_SILENTLY')) {
7292 echo "<li>".get_string("checkingbackup").'</li>';
7294 if (! $status = restore_check_moodle_file ($xml_file)) {
7295 if (!is_file($xml_file)) {
7296 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
7297 } else {
7298 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
7300 if (!defined('RESTORE_SILENTLY')) {
7301 notify($errorstr);
7302 } else {
7303 return false;
7308 $info = "";
7309 $course_header = "";
7311 //Now read the info tag (all)
7312 if ($status) {
7313 if (!defined('RESTORE_SILENTLY')) {
7314 echo "<li>".get_string("readinginfofrombackup").'</li>';
7316 //Reading info from file
7317 $info = restore_read_xml_info ($xml_file);
7318 //Reading course_header from file
7319 $course_header = restore_read_xml_course_header ($xml_file);
7321 if(!is_object($course_header)){
7322 // ensure we fail if there is no course header
7323 $course_header = false;
7327 if (!defined('RESTORE_SILENTLY')) {
7328 //End the main ul
7329 echo "</ul>\n";
7331 //End the main table
7332 echo "</td></tr>";
7333 echo "</table>";
7336 //We compare Moodle's versions
7337 if ($CFG->version < $info->backup_moodle_version && $status) {
7338 $message = new message();
7339 $message->serverversion = $CFG->version;
7340 $message->serverrelease = $CFG->release;
7341 $message->backupversion = $info->backup_moodle_version;
7342 $message->backuprelease = $info->backup_moodle_release;
7343 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
7347 //Now we print in other table, the backup and the course it contains info
7348 if ($info and $course_header and $status) {
7349 //First, the course info
7350 if (!defined('RESTORE_SILENTLY')) {
7351 $status = restore_print_course_header($course_header);
7353 //Now, the backup info
7354 if ($status) {
7355 if (!defined('RESTORE_SILENTLY')) {
7356 $status = restore_print_info($info);
7361 //Save course header and info into php session
7362 if ($status) {
7363 $SESSION->info = $info;
7364 $SESSION->course_header = $course_header;
7367 //Finally, a little form to continue
7368 //with some hidden fields
7369 if ($status) {
7370 if (!defined('RESTORE_SILENTLY')) {
7371 echo "<br /><div style='text-align:center'>";
7372 $hidden["backup_unique_code"] = $backup_unique_code;
7373 $hidden["launch"] = "form";
7374 $hidden["file"] = $file;
7375 $hidden["id"] = $id;
7376 print_single_button("restore.php", $hidden, get_string("continue"),"post");
7377 echo "</div>";
7379 else {
7380 if (empty($noredirect)) {
7381 redirect($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
7382 } else {
7383 return $backup_unique_code;
7388 if (!$status) {
7389 if (!defined('RESTORE_SILENTLY')) {
7390 error ("An error has ocurred");
7391 } else {
7392 $errorstr = "An error has occured"; // helpful! :P
7393 return false;
7396 return true;
7399 function restore_setup_for_check(&$restore,$backup_unique_code) {
7400 global $SESSION;
7401 $restore->backup_unique_code=$backup_unique_code;
7402 $restore->users = 2; // yuk
7403 $restore->course_files = $SESSION->restore->restore_course_files;
7404 $restore->site_files = $SESSION->restore->restore_site_files;
7405 if ($allmods = get_records("modules")) {
7406 foreach ($allmods as $mod) {
7407 $modname = $mod->name;
7408 $var = "restore_".$modname;
7409 //Now check that we have that module info in the backup file
7410 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
7411 $restore->$var = 1;
7415 return true;
7418 function backup_to_restore_array($backup,$k=0) {
7419 if (is_array($backup) ) {
7420 foreach ($backup as $key => $value) {
7421 $newkey = str_replace('backup','restore',$key);
7422 $restore[$newkey] = backup_to_restore_array($value,$key);
7425 else if (is_object($backup)) {
7426 $tmp = get_object_vars($backup);
7427 foreach ($tmp as $key => $value) {
7428 $newkey = str_replace('backup','restore',$key);
7429 $restore->$newkey = backup_to_restore_array($value,$key);
7432 else {
7433 $newkey = str_replace('backup','restore',$k);
7434 $restore = $backup;
7436 return $restore;
7440 * compatibility function
7441 * checks for per-instance backups AND
7442 * older per-module backups
7443 * and returns whether userdata has been selected.
7445 function restore_userdata_selected($restore,$modname,$modid) {
7446 // check first for per instance array
7447 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
7448 return array_key_exists($modid,$restore->mods[$modname]->instances)
7449 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
7452 //print_object($restore->mods[$modname]);
7453 return !empty($restore->mods[$modname]->userinfo);
7456 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
7458 global $CFG, $USER;
7459 $status = true;
7461 //Checks for the required files/functions to restore every module
7462 //and include them
7463 if ($allmods = get_records("modules") ) {
7464 foreach ($allmods as $mod) {
7465 $modname = $mod->name;
7466 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
7467 //If file exists and we have selected to restore that type of module
7468 if ((file_exists($modfile)) and !empty($restore->mods[$modname]) and ($restore->mods[$modname]->restore)) {
7469 include_once($modfile);
7474 if (!defined('RESTORE_SILENTLY')) {
7475 //Start the main table
7476 echo "<table cellpadding=\"5\">";
7477 echo "<tr><td>";
7479 //Start the main ul
7480 echo "<ul>";
7483 //Localtion of the xml file
7484 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
7486 //If we've selected to restore into new course
7487 //create it (course)
7488 //Saving conversion id variables into backup_tables
7489 if ($restore->restoreto == 2) {
7490 if (!defined('RESTORE_SILENTLY')) {
7491 echo '<li>'.get_string('creatingnewcourse') . '</li>';
7493 $oldidnumber = $course_header->course_idnumber;
7494 if (!$status = restore_create_new_course($restore,$course_header)) {
7495 if (!defined('RESTORE_SILENTLY')) {
7496 notify("Error while creating the new empty course.");
7497 } else {
7498 $errorstr = "Error while creating the new empty course.";
7499 return false;
7503 //Print course fullname and shortname and category
7504 if ($status) {
7505 if (!defined('RESTORE_SILENTLY')) {
7506 echo "<ul>";
7507 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
7508 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
7509 if (!empty($oldidnumber)) {
7510 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
7512 echo "</ul>";
7513 //Put the destination course_id
7515 $restore->course_id = $course_header->course_id;
7518 if ($status = restore_open_html($restore,$course_header)){
7519 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
7522 } else {
7523 $course = get_record("course","id",$restore->course_id);
7524 if ($course) {
7525 if (!defined('RESTORE_SILENTLY')) {
7526 echo "<li>".get_string("usingexistingcourse");
7527 echo "<ul>";
7528 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
7529 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
7530 if (($restore->deleting)) {
7531 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
7532 } else {
7533 echo "<li>".get_string("addingdatatoexisting").'</li>';
7535 echo "</ul></li>";
7537 //If we have selected to restore deleting, we do it now.
7538 if ($restore->deleting) {
7539 if (!defined('RESTORE_SILENTLY')) {
7540 echo "<li>".get_string("deletingolddata").'</li>';
7542 $status = remove_course_contents($restore->course_id,false) and
7543 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
7544 if ($status) {
7545 //Now , this situation is equivalent to the "restore to new course" one (we
7546 //have a course record and nothing more), so define it as "to new course"
7547 $restore->restoreto = 2;
7548 } else {
7549 if (!defined('RESTORE_SILENTLY')) {
7550 notify("An error occurred while deleting some of the course contents.");
7551 } else {
7552 $errrostr = "An error occurred while deleting some of the course contents.";
7553 return false;
7557 } else {
7558 if (!defined('RESTORE_SILENTLY')) {
7559 notify("Error opening existing course.");
7560 $status = false;
7561 } else {
7562 $errorstr = "Error opening existing course.";
7563 return false;
7568 //Now create users as needed
7569 if ($status and ($restore->users == 0 or $restore->users == 1)) {
7570 if (!defined('RESTORE_SILENTLY')) {
7571 echo "<li>".get_string("creatingusers")."<br />";
7573 if (!$status = restore_create_users($restore,$xml_file)) {
7574 if (!defined('RESTORE_SILENTLY')) {
7575 notify("Could not restore users.");
7576 } else {
7577 $errorstr = "Could not restore users.";
7578 return false;
7582 //Now print info about the work done
7583 if ($status) {
7584 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
7585 where backup_code = '$restore->backup_unique_code' and
7586 table_name = 'user'");
7587 //We've records
7588 if ($recs) {
7589 $new_count = 0;
7590 $exists_count = 0;
7591 $student_count = 0;
7592 $teacher_count = 0;
7593 $counter = 0;
7594 //Iterate, filling counters
7595 foreach ($recs as $rec) {
7596 //Get full record, using backup_getids
7597 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
7598 if (strpos($record->info,"new") !== false) {
7599 $new_count++;
7601 if (strpos($record->info,"exists") !== false) {
7602 $exists_count++;
7604 if (strpos($record->info,"student") !== false) {
7605 $student_count++;
7606 } else if (strpos($record->info,"teacher") !== false) {
7607 $teacher_count++;
7609 //Do some output
7610 $counter++;
7611 if ($counter % 10 == 0) {
7612 if (!defined('RESTORE_SILENTLY')) {
7613 echo ".";
7614 if ($counter % 200 == 0) {
7615 echo "<br />";
7618 backup_flush(300);
7621 if (!defined('RESTORE_SILENTLY')) {
7622 //Now print information gathered
7623 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
7624 echo "<ul>";
7625 echo "<li>".get_string("students").": ".$student_count.'</li>';
7626 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
7627 echo "</ul>";
7629 } else {
7630 if (!defined('RESTORE_SILENTLY')) {
7631 notify("No users were found!");
7632 } // no need to return false here, it's recoverable.
7636 if (!defined('RESTORE_SILENTLY')) {
7637 echo "</li>";
7642 //Now create groups as needed
7643 if ($status and ($restore->groups == RESTORE_GROUPS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
7644 if (!defined('RESTORE_SILENTLY')) {
7645 echo "<li>".get_string("creatinggroups");
7647 if (!$status = restore_create_groups($restore,$xml_file)) {
7648 if (!defined('RESTORE_SILENTLY')) {
7649 notify("Could not restore groups!");
7650 } else {
7651 $errorstr = "Could not restore groups!";
7652 return false;
7655 if (!defined('RESTORE_SILENTLY')) {
7656 echo '</li>';
7660 //Now create groupings as needed
7661 if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
7662 if (!defined('RESTORE_SILENTLY')) {
7663 echo "<li>".get_string("creatinggroupings");
7665 if (!$status = restore_create_groupings($restore,$xml_file)) {
7666 if (!defined('RESTORE_SILENTLY')) {
7667 notify("Could not restore groupings!");
7668 } else {
7669 $errorstr = "Could not restore groupings!";
7670 return false;
7673 if (!defined('RESTORE_SILENTLY')) {
7674 echo '</li>';
7678 //Now create groupingsgroups as needed
7679 if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) {
7680 if (!defined('RESTORE_SILENTLY')) {
7681 echo "<li>".get_string("creatinggroupingsgroups");
7683 if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
7684 if (!defined('RESTORE_SILENTLY')) {
7685 notify("Could not restore groups in groupings!");
7686 } else {
7687 $errorstr = "Could not restore groups in groupings!";
7688 return false;
7691 if (!defined('RESTORE_SILENTLY')) {
7692 echo '</li>';
7697 //Now create the course_sections and their associated course_modules
7698 //we have to do this after groups and groupings are restored, because we need the new groupings id
7699 if ($status) {
7700 //Into new course
7701 if ($restore->restoreto == 2) {
7702 if (!defined('RESTORE_SILENTLY')) {
7703 echo "<li>".get_string("creatingsections");
7705 if (!$status = restore_create_sections($restore,$xml_file)) {
7706 if (!defined('RESTORE_SILENTLY')) {
7707 notify("Error creating sections in the existing course.");
7708 } else {
7709 $errorstr = "Error creating sections in the existing course.";
7710 return false;
7713 if (!defined('RESTORE_SILENTLY')) {
7714 echo '</li>';
7716 //Into existing course
7717 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
7718 if (!defined('RESTORE_SILENTLY')) {
7719 echo "<li>".get_string("checkingsections");
7721 if (!$status = restore_create_sections($restore,$xml_file)) {
7722 if (!defined('RESTORE_SILENTLY')) {
7723 notify("Error creating sections in the existing course.");
7724 } else {
7725 $errorstr = "Error creating sections in the existing course.";
7726 return false;
7729 if (!defined('RESTORE_SILENTLY')) {
7730 echo '</li>';
7732 //Error
7733 } else {
7734 if (!defined('RESTORE_SILENTLY')) {
7735 notify("Neither a new course or an existing one was specified.");
7736 $status = false;
7737 } else {
7738 $errorstr = "Neither a new course or an existing one was specified.";
7739 return false;
7744 //Now create metacourse info
7745 if ($status and $restore->metacourse) {
7746 //Only to new courses!
7747 if ($restore->restoreto == 2) {
7748 if (!defined('RESTORE_SILENTLY')) {
7749 echo "<li>".get_string("creatingmetacoursedata");
7751 if (!$status = restore_create_metacourse($restore,$xml_file)) {
7752 if (!defined('RESTORE_SILENTLY')) {
7753 notify("Error creating metacourse in the course.");
7754 } else {
7755 $errorstr = "Error creating metacourse in the course.";
7756 return false;
7759 if (!defined('RESTORE_SILENTLY')) {
7760 echo '</li>';
7766 //Now create categories and questions as needed
7767 if ($status) {
7768 include_once("$CFG->dirroot/question/restorelib.php");
7769 if (!defined('RESTORE_SILENTLY')) {
7770 echo "<li>".get_string("creatingcategoriesandquestions");
7771 echo "<ul>";
7773 if (!$status = restore_create_questions($restore,$xml_file)) {
7774 if (!defined('RESTORE_SILENTLY')) {
7775 notify("Could not restore categories and questions!");
7776 } else {
7777 $errorstr = "Could not restore categories and questions!";
7778 return false;
7781 if (!defined('RESTORE_SILENTLY')) {
7782 echo "</ul></li>";
7786 //Now create user_files as needed
7787 if ($status and ($restore->user_files)) {
7788 if (!defined('RESTORE_SILENTLY')) {
7789 echo "<li>".get_string("copyinguserfiles");
7791 if (!$status = restore_user_files($restore)) {
7792 if (!defined('RESTORE_SILENTLY')) {
7793 notify("Could not restore user files!");
7794 } else {
7795 $errorstr = "Could not restore user files!";
7796 return false;
7799 //If all is ok (and we have a counter)
7800 if ($status and ($status !== true)) {
7801 //Inform about user dirs created from backup
7802 if (!defined('RESTORE_SILENTLY')) {
7803 echo "<ul>";
7804 echo "<li>".get_string("userzones").": ".$status;
7805 echo "</li></ul>";
7808 if (!defined('RESTORE_SILENTLY')) {
7809 echo '</li>';
7813 //Now create course files as needed
7814 if ($status and ($restore->course_files)) {
7815 if (!defined('RESTORE_SILENTLY')) {
7816 echo "<li>".get_string("copyingcoursefiles");
7818 if (!$status = restore_course_files($restore)) {
7819 if (empty($status)) {
7820 notify("Could not restore course files!");
7821 } else {
7822 $errorstr = "Could not restore course files!";
7823 return false;
7826 //If all is ok (and we have a counter)
7827 if ($status and ($status !== true)) {
7828 //Inform about user dirs created from backup
7829 if (!defined('RESTORE_SILENTLY')) {
7830 echo "<ul>";
7831 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7832 echo "</ul>";
7835 if (!defined('RESTORE_SILENTLY')) {
7836 echo "</li>";
7841 //Now create site files as needed
7842 if ($status and ($restore->site_files)) {
7843 if (!defined('RESTORE_SILENTLY')) {
7844 echo "<li>".get_string('copyingsitefiles');
7846 if (!$status = restore_site_files($restore)) {
7847 if (empty($status)) {
7848 notify("Could not restore site files!");
7849 } else {
7850 $errorstr = "Could not restore site files!";
7851 return false;
7854 //If all is ok (and we have a counter)
7855 if ($status and ($status !== true)) {
7856 //Inform about user dirs created from backup
7857 if (!defined('RESTORE_SILENTLY')) {
7858 echo "<ul>";
7859 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7860 echo "</ul>";
7863 if (!defined('RESTORE_SILENTLY')) {
7864 echo "</li>";
7868 //Now create messages as needed
7869 if ($status and ($restore->messages)) {
7870 if (!defined('RESTORE_SILENTLY')) {
7871 echo "<li>".get_string("creatingmessagesinfo");
7873 if (!$status = restore_create_messages($restore,$xml_file)) {
7874 if (!defined('RESTORE_SILENTLY')) {
7875 notify("Could not restore messages!");
7876 } else {
7877 $errorstr = "Could not restore messages!";
7878 return false;
7881 if (!defined('RESTORE_SILENTLY')) {
7882 echo "</li>";
7886 //Now create blogs as needed
7887 if ($status and ($restore->blogs)) {
7888 if (!defined('RESTORE_SILENTLY')) {
7889 echo "<li>".get_string("creatingblogsinfo");
7891 if (!$status = restore_create_blogs($restore,$xml_file)) {
7892 if (!defined('RESTORE_SILENTLY')) {
7893 notify("Could not restore blogs!");
7894 } else {
7895 $errorstr = "Could not restore blogs!";
7896 return false;
7899 if (!defined('RESTORE_SILENTLY')) {
7900 echo "</li>";
7904 //Now create scales as needed
7905 if ($status) {
7906 if (!defined('RESTORE_SILENTLY')) {
7907 echo "<li>".get_string("creatingscales");
7909 if (!$status = restore_create_scales($restore,$xml_file)) {
7910 if (!defined('RESTORE_SILENTLY')) {
7911 notify("Could not restore custom scales!");
7912 } else {
7913 $errorstr = "Could not restore custom scales!";
7914 return false;
7917 if (!defined('RESTORE_SILENTLY')) {
7918 echo '</li>';
7922 //Now create events as needed
7923 if ($status) {
7924 if (!defined('RESTORE_SILENTLY')) {
7925 echo "<li>".get_string("creatingevents");
7927 if (!$status = restore_create_events($restore,$xml_file)) {
7928 if (!defined('RESTORE_SILENTLY')) {
7929 notify("Could not restore course events!");
7930 } else {
7931 $errorstr = "Could not restore course events!";
7932 return false;
7935 if (!defined('RESTORE_SILENTLY')) {
7936 echo '</li>';
7940 //Now create course modules as needed
7941 if ($status) {
7942 if (!defined('RESTORE_SILENTLY')) {
7943 echo "<li>".get_string("creatingcoursemodules");
7945 if (!$status = restore_create_modules($restore,$xml_file)) {
7946 if (!defined('RESTORE_SILENTLY')) {
7947 notify("Could not restore modules!");
7948 } else {
7949 $errorstr = "Could not restore modules!";
7950 return false;
7953 if (!defined('RESTORE_SILENTLY')) {
7954 echo '</li>';
7958 //Bring back the course blocks -- do it AFTER the modules!!!
7959 if($status) {
7960 //If we are deleting and bringing into a course or making a new course, same situation
7961 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7962 if (!defined('RESTORE_SILENTLY')) {
7963 echo '<li>'.get_string('creatingblocks');
7965 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
7966 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
7967 if (!defined('RESTORE_SILENTLY')) {
7968 notify('Error while creating the course blocks');
7969 } else {
7970 $errorstr = "Error while creating the course blocks";
7971 return false;
7974 if (!defined('RESTORE_SILENTLY')) {
7975 echo '</li>';
7980 if($status) {
7981 //If we are deleting and bringing into a course or making a new course, same situation
7982 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7983 if (!defined('RESTORE_SILENTLY')) {
7984 echo '<li>'.get_string('courseformatdata');
7986 if (!$status = restore_set_format_data($restore, $xml_file)) {
7987 $error = "Error while setting the course format data";
7988 if (!defined('RESTORE_SILENTLY')) {
7989 notify($error);
7990 } else {
7991 $errorstr=$error;
7992 return false;
7995 if (!defined('RESTORE_SILENTLY')) {
7996 echo '</li>';
8001 //Now create log entries as needed
8002 if ($status and ($restore->logs)) {
8003 if (!defined('RESTORE_SILENTLY')) {
8004 echo "<li>".get_string("creatinglogentries");
8006 if (!$status = restore_create_logs($restore,$xml_file)) {
8007 if (!defined('RESTORE_SILENTLY')) {
8008 notify("Could not restore logs!");
8009 } else {
8010 $errorstr = "Could not restore logs!";
8011 return false;
8014 if (!defined('RESTORE_SILENTLY')) {
8015 echo '</li>';
8019 //Now, if all is OK, adjust the instance field in course_modules !!
8020 //this also calculates the final modinfo information so, after this,
8021 //code needing it can be used (like role_assignments. MDL-13740)
8022 if ($status) {
8023 if (!defined('RESTORE_SILENTLY')) {
8024 echo "<li>".get_string("checkinginstances");
8026 if (!$status = restore_check_instances($restore)) {
8027 if (!defined('RESTORE_SILENTLY')) {
8028 notify("Could not adjust instances in course_modules!");
8029 } else {
8030 $errorstr = "Could not adjust instances in course_modules!";
8031 return false;
8034 if (!defined('RESTORE_SILENTLY')) {
8035 echo '</li>';
8039 //Now, if all is OK, adjust activity events
8040 if ($status) {
8041 if (!defined('RESTORE_SILENTLY')) {
8042 echo "<li>".get_string("refreshingevents");
8044 if (!$status = restore_refresh_events($restore)) {
8045 if (!defined('RESTORE_SILENTLY')) {
8046 notify("Could not refresh events for activities!");
8047 } else {
8048 $errorstr = "Could not refresh events for activities!";
8049 return false;
8052 if (!defined('RESTORE_SILENTLY')) {
8053 echo '</li>';
8057 //Now, if all is OK, adjust inter-activity links
8058 if ($status) {
8059 if (!defined('RESTORE_SILENTLY')) {
8060 echo "<li>".get_string("decodinginternallinks");
8062 if (!$status = restore_decode_content_links($restore)) {
8063 if (!defined('RESTORE_SILENTLY')) {
8064 notify("Could not decode content links!");
8065 } else {
8066 $errorstr = "Could not decode content links!";
8067 return false;
8070 if (!defined('RESTORE_SILENTLY')) {
8071 echo '</li>';
8075 //Now, with backup files prior to version 2005041100,
8076 //convert all the wiki texts in the course to markdown
8077 if ($status && $restore->backup_version < 2005041100) {
8078 if (!defined('RESTORE_SILENTLY')) {
8079 echo "<li>".get_string("convertingwikitomarkdown");
8081 if (!$status = restore_convert_wiki2markdown($restore)) {
8082 if (!defined('RESTORE_SILENTLY')) {
8083 notify("Could not convert wiki texts to markdown!");
8084 } else {
8085 $errorstr = "Could not convert wiki texts to markdown!";
8086 return false;
8089 if (!defined('RESTORE_SILENTLY')) {
8090 echo '</li>';
8094 //Now create gradebook as needed -- AFTER modules and blocks!!!
8095 if ($status) {
8096 if ($restore->backup_version > 2007090500) {
8097 if (!defined('RESTORE_SILENTLY')) {
8098 echo "<li>".get_string("creatinggradebook");
8100 if (!$status = restore_create_gradebook($restore,$xml_file)) {
8101 if (!defined('RESTORE_SILENTLY')) {
8102 notify("Could not restore gradebook!");
8103 } else {
8104 $errorstr = "Could not restore gradebook!";
8105 return false;
8109 if (!defined('RESTORE_SILENTLY')) {
8110 echo '</li>';
8113 } else {
8114 // for moodle versions before 1.9, those grades need to be converted to use the new gradebook
8115 // this code needs to execute *after* the course_modules are sorted out
8116 if (!defined('RESTORE_SILENTLY')) {
8117 echo "<li>".get_string("migratinggrades");
8120 /// force full refresh of grading data before migration == crete all items first
8121 if (!$status = restore_migrate_old_gradebook($restore,$xml_file)) {
8122 if (!defined('RESTORE_SILENTLY')) {
8123 notify("Could not migrate gradebook!");
8124 } else {
8125 $errorstr = "Could not migrade gradebook!";
8126 return false;
8129 if (!defined('RESTORE_SILENTLY')) {
8130 echo '</li>';
8133 /// force full refresh of grading data after all items are created
8134 grade_force_full_regrading($restore->course_id);
8135 grade_grab_course_grades($restore->course_id);
8138 /*******************************************************************************
8139 ************* Restore of Roles and Capabilities happens here ******************
8140 *******************************************************************************/
8141 // try to restore roles even when restore is going to fail - teachers might have
8142 // at least some role assigned - this is not correct though
8143 $status = restore_create_roles($restore, $xml_file) && $status;
8144 $status = restore_roles_settings($restore, $xml_file) && $status;
8146 //Now if all is OK, update:
8147 // - course modinfo field
8148 // - categories table
8149 // - add user as teacher
8150 if ($status) {
8151 if (!defined('RESTORE_SILENTLY')) {
8152 echo "<li>".get_string("checkingcourse");
8154 //categories table
8155 $course = get_record("course","id",$restore->course_id);
8156 fix_course_sortorder();
8157 // Check if the user has course update capability in the newly restored course
8158 // there is no need to load his capabilities again, because restore_roles_settings
8159 // would have loaded it anyway, if there is any assignments.
8160 // fix for MDL-6831
8161 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
8162 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
8163 // fix for MDL-9065, use the new config setting if exists
8164 if ($CFG->creatornewroleid) {
8165 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
8166 } else {
8167 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM))) {
8168 if ($legacyteacher = array_shift($legacyteachers)) {
8169 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
8171 } else {
8172 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
8176 if (!defined('RESTORE_SILENTLY')) {
8177 echo '</li>';
8181 //Cleanup temps (files and db)
8182 if ($status) {
8183 if (!defined('RESTORE_SILENTLY')) {
8184 echo "<li>".get_string("cleaningtempdata");
8186 if (!$status = clean_temp_data ($restore)) {
8187 if (!defined('RESTORE_SILENTLY')) {
8188 notify("Could not clean up temporary data from files and database");
8189 } else {
8190 $errorstr = "Could not clean up temporary data from files and database";
8191 return false;
8194 if (!defined('RESTORE_SILENTLY')) {
8195 echo '</li>';
8199 // this is not a critical check - the result can be ignored
8200 if (restore_close_html($restore)){
8201 if (!defined('RESTORE_SILENTLY')) {
8202 echo '<li>Closing the Restorelog.html file.</li>';
8205 else {
8206 if (!defined('RESTORE_SILENTLY')) {
8207 notify("Could not close the restorelog.html file");
8211 if (!defined('RESTORE_SILENTLY')) {
8212 //End the main ul
8213 echo "</ul>";
8215 //End the main table
8216 echo "</td></tr>";
8217 echo "</table>";
8220 return $status;
8222 //Create, open and write header of the html log file
8223 function restore_open_html($restore,$course_header) {
8225 global $CFG;
8227 $status = true;
8229 //Open file for writing
8230 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
8231 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
8232 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
8234 $status = check_dir_exists($dest_dir,true);
8235 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
8236 //Add the stylesheet
8237 $stylesheetshtml = '';
8238 foreach ($CFG->stylesheets as $stylesheet) {
8239 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
8241 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
8242 $languagehtml = get_html_lang($dir=true);
8244 //Write the header in the new logging file
8245 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
8246 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
8247 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
8248 fwrite ($restorelog_file,"<head>");
8249 fwrite ($restorelog_file,$stylesheetshtml);
8250 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
8251 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
8252 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
8253 $startdate = addslashes($course_header->course_startdate);
8254 $date = usergetdate($startdate);
8255 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
8256 $startdate += $restore->course_startdateoffset;
8257 $date = usergetdate($startdate);
8258 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
8260 if ($status) {
8261 return $restorelog_file;
8262 } else {
8263 return false;
8266 //Create & close footer of the html log file
8267 function restore_close_html($restore) {
8269 global $CFG;
8271 $status = true;
8273 //Open file for writing
8274 //First, check that course_id/backupdata folder exists in CFG->dataroot
8275 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
8276 $status = check_dir_exists($dest_dir, true, true);
8277 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
8278 //Write the footer to close the logging file
8279 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
8280 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
8282 if ($status) {
8283 return $restorelog_file;
8284 } else {
8285 return false;
8289 /********************** Roles and Capabilities Related Functions *******************************/
8291 /* Yu: Note recovering of role assignments/overrides need to take place after
8292 users have been recovered, i.e. after we get their new_id, and after all
8293 roles have been recreated or mapped. Contexts can be created on the fly.
8294 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
8295 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
8296 once roles and users have been restored.
8300 * This function restores all the needed roles for this course
8301 * i.e. roles with an assignment in any of the mods or blocks,
8302 * roles assigned on any user (e.g. parent role) and roles
8303 * assigned at course levle
8304 * This function should check for duplicate roles first
8305 * It isn't now, just overwriting
8307 function restore_create_roles($restore, $xmlfile) {
8308 if (!defined('RESTORE_SILENTLY')) {
8309 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
8311 $info = restore_read_xml_roles($xmlfile);
8313 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
8315 // the following code creates new roles
8316 // but we could use more intelligent detection, and role mapping
8317 // get role mapping info from $restore
8318 $rolemappings = array();
8320 if (!empty($restore->rolesmapping)) {
8321 $rolemappings = $restore->rolesmapping;
8323 // $info->roles will be empty for backups pre 1.7
8324 if (isset($info->roles) && $info->roles) {
8326 foreach ($info->roles as $oldroleid=>$roledata) {
8328 if (empty($restore->rolesmapping)) {
8329 // if this is empty altogether, we came from import or there's no roles used in course at all
8330 // in this case, write the same oldid as this is the same site
8331 // no need to do mapping
8332 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
8333 $oldroleid); // adding a new id
8334 continue; // do not create additonal roles;
8336 // first we check if the roles are in the mappings
8337 // if so, we just do a mapping i.e. update oldids table
8338 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
8339 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
8340 $rolemappings[$oldroleid]); // adding a new id
8342 } else {
8344 // code to make new role name/short name if same role name or shortname exists
8345 $fullname = $roledata->name;
8346 $shortname = $roledata->shortname;
8347 $currentfullname = "";
8348 $currentshortname = "";
8349 $counter = 0;
8351 do {
8352 if ($counter) {
8353 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
8354 $suffixshort = "_".$counter;
8355 } else {
8356 $suffixfull = "";
8357 $suffixshort = "";
8359 $currentfullname = $fullname.$suffixfull;
8360 // Limit the size of shortname - database column accepts <= 100 chars
8361 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
8362 $coursefull = get_record("role","name",addslashes($currentfullname));
8363 $courseshort = get_record("role","shortname",addslashes($currentshortname));
8364 $counter++;
8365 } while ($coursefull || $courseshort);
8367 $roledata->name = $currentfullname;
8368 $roledata->shortname= $currentshortname;
8370 // done finding a unique name
8372 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
8373 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
8374 $newroleid); // adding a new id
8375 foreach ($roledata->capabilities as $capability) {
8377 $roleinfo = new object();
8378 $roleinfo = (object)$capability;
8379 $roleinfo->contextid = $sitecontext->id;
8380 $roleinfo->capability = $capability->name;
8381 $roleinfo->roleid = $newroleid;
8383 insert_record('role_capabilities', $roleinfo);
8386 /// Now, restore role nameincourse
8387 $newrole = backup_getid($restore->backup_unique_code, 'role', $oldroleid); /// Look for target role
8388 $coursecontext = get_context_instance(CONTEXT_COURSE, $restore->course_id); /// Look for target context
8389 if (!empty($newrole->new_id) && !empty($coursecontext)) {
8390 /// Check the role hasn't any custom name in context
8391 if (!record_exists('role_names', 'roleid', $newrole->new_id, 'contextid', $coursecontext->id)) {
8392 $rolename = new object();
8393 $rolename->roleid = $newrole->new_id;
8394 $rolename->contextid = $coursecontext->id;
8395 $rolename->name = addslashes($roledata->nameincourse);
8397 insert_record('role_names', $rolename);
8402 return true;
8406 * this function restores role assignments and role overrides
8407 * in course/user/block/mod level, it passed through
8408 * the xml file again
8410 function restore_roles_settings($restore, $xmlfile) {
8411 // data pulls from course, mod, user, and blocks
8413 /*******************************************************
8414 * Restoring from course level assignments *
8415 *******************************************************/
8416 if (!defined('RESTORE_SILENTLY')) {
8417 echo "<li>".get_string("creatingcourseroles").'</li>';
8419 $course = restore_read_xml_course_header($xmlfile);
8421 if (!isset($restore->rolesmapping)) {
8422 $isimport = true; // course import from another course, or course with no role assignments
8423 } else {
8424 $isimport = false; // course restore with role assignments
8427 if (!empty($course->roleassignments) && !$isimport) {
8428 $courseassignments = $course->roleassignments;
8430 foreach ($courseassignments as $oldroleid => $courseassignment) {
8431 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
8434 /*****************************************************
8435 * Restoring from course level overrides *
8436 *****************************************************/
8438 if (!empty($course->roleoverrides) && !$isimport) {
8439 $courseoverrides = $course->roleoverrides;
8440 foreach ($courseoverrides as $oldroleid => $courseoverride) {
8441 // if not importing into exiting course, or creating new role, we are ok
8442 // local course overrides to be respected (i.e. restored course overrides ignored)
8443 if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
8444 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
8449 /*******************************************************
8450 * Restoring role assignments/overrdies *
8451 * from module level assignments *
8452 *******************************************************/
8454 if (!defined('RESTORE_SILENTLY')) {
8455 echo "<li>".get_string("creatingmodroles").'</li>';
8457 $sections = restore_read_xml_sections($xmlfile);
8458 $secs = $sections->sections;
8460 foreach ($secs as $section) {
8461 if (isset($section->mods)) {
8462 foreach ($section->mods as $modid=>$mod) {
8463 if (isset($mod->roleassignments) && !$isimport) {
8464 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
8465 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
8468 // role overrides always applies, in import or backup/restore
8469 if (isset($mod->roleoverrides)) {
8470 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
8471 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
8478 /*************************************************
8479 * Restoring assignments from blocks level *
8480 * role assignments/overrides *
8481 *************************************************/
8483 if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
8484 if (!defined('RESTORE_SILENTLY')) {
8485 echo "<li>".get_string("creatingblocksroles").'</li>';
8487 $blocks = restore_read_xml_blocks($restore, $xmlfile);
8488 if (isset($blocks->instances)) {
8489 foreach ($blocks->instances as $instance) {
8490 if (isset($instance->roleassignments) && !$isimport) {
8491 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
8492 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
8496 // likewise block overrides should always be restored like mods
8497 if (isset($instance->roleoverrides)) {
8498 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
8499 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
8505 /************************************************
8506 * Restoring assignments from userid level *
8507 * role assignments/overrides *
8508 ************************************************/
8509 if (!defined('RESTORE_SILENTLY')) {
8510 echo "<li>".get_string("creatinguserroles").'</li>';
8512 $info = restore_read_xml_users($restore, $xmlfile);
8513 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
8514 //For each user, take its info from backup_ids
8515 foreach ($info->users as $userid) {
8516 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
8517 if (isset($rec->info->roleassignments)) {
8518 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
8519 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
8522 if (isset($rec->info->roleoverrides)) {
8523 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
8524 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
8530 return true;
8533 // auxillary function to write role assignments read from xml to db
8534 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
8536 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
8538 foreach ($assignments as $assignment) {
8540 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
8541 //Oh dear, $olduser... can be an object, $obj->string or bool!
8542 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
8543 continue;
8545 $assignment->userid = $olduser->new_id; // new userid here
8546 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
8547 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
8548 $assignment->roleid = $role->new_id; // restored new role id
8550 // hack to make the correct contextid for course level imports
8551 if ($contextlevel == CONTEXT_COURSE) {
8552 $oldinstance->new_id = $restore->course_id;
8553 } else {
8554 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
8557 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
8558 $assignment->contextid = $newcontext->id; // new context id
8559 // might already have same assignment
8560 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol, $assignment->timemodified);
8565 // auxillary function to write role assignments read from xml to db
8566 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
8568 // it is possible to have an override not relevant to this course context.
8569 // should be ignored(?)
8570 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
8571 return null;
8574 foreach ($overrides as $override) {
8575 $override->capability = $override->name;
8576 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
8577 $override->modifierid = !empty($oldmodifier->new_id)?$oldmodifier->new_id:0; // new modifier id here
8578 $override->roleid = $role->new_id; // restored new role id
8580 // hack to make the correct contextid for course level imports
8581 if ($contextlevel == CONTEXT_COURSE) {
8582 $oldinstance->new_id = $restore->course_id;
8583 } else {
8584 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
8587 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
8588 $override->contextid = $newcontext->id; // new context id
8589 // use assign capability instead so we can add context to context_rel
8590 assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
8593 //write activity date changes to the html log file, and update date values in the the xml array
8594 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
8596 global $CFG;
8597 $openlog = false;
8599 // loop through time fields in $TAGS
8600 foreach ($TAGS as $TAG) {
8602 // check $TAG has a sensible value
8603 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
8605 if ($openlog==false) {
8606 $openlog = true; // only come through here once
8608 // open file for writing
8609 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
8610 check_dir_exists($course_dir, true);
8611 $restorelog = fopen("$course_dir/restorelog.html", "a");
8613 // start output for this record
8614 $msg = new stdClass();
8615 $msg->recordtype = $recordtype;
8616 $msg->recordname = $xml[$NAMETAG][0]['#'];
8617 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
8620 // write old date to $restorelog
8621 $value = $xml[$TAG][0]['#'];
8622 $date = usergetdate($value);
8624 $msg = new stdClass();
8625 $msg->TAG = $TAG;
8626 $msg->weekday = $date['weekday'];
8627 $msg->mday = $date['mday'];
8628 $msg->month = $date['month'];
8629 $msg->year = $date['year'];
8630 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
8632 // write new date to $restorelog
8633 $value += $restore->course_startdateoffset;
8634 $date = usergetdate($value);
8636 $msg = new stdClass();
8637 $msg->TAG = $TAG;
8638 $msg->weekday = $date['weekday'];
8639 $msg->mday = $date['mday'];
8640 $msg->month = $date['month'];
8641 $msg->year = $date['year'];
8642 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
8644 // update $value in $xml tree for calling module
8645 $xml[$TAG][0]['#'] = "$value";
8648 // close the restore log, if it was opened
8649 if ($openlog) {
8650 fclose($restorelog);