Bumped to 1.9.7
[moodle.git] / backup / restorelib.php
blob1f29a3339af6a65bb3a2729620585f56b6204751
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/block/course format..., 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 = backup_todb($course->summary,false); // Exception: Process FILEPHP (not available when restored) MDL-18222
102 $coursesummary = restore_decode_content_links_worker($coursesummary, $restore);
103 if ($coursesummary != $course->summary) {
104 $course->summary = addslashes($coursesummary);
105 if (!update_record('course', $course)) {
106 $status = false;
109 if (!defined('RESTORE_SILENTLY')) {
110 echo '</li>';
113 // Recode links in section summaries.
114 $sections = get_records('course_sections', 'course', $restore->course_id, 'id', 'id,summary');
115 if ($sections) {
116 if (!defined('RESTORE_SILENTLY')) {
117 echo '<li>' . get_string('from') . ' ' . get_string('sections');
119 foreach ($sections as $section) {
120 $sectionsummary = restore_decode_content_links_worker($section->summary, $restore);
121 if ($sectionsummary != $section->summary) {
122 $section->summary = addslashes($sectionsummary);
123 if (!update_record('course_sections', $section)) {
124 $status = false;
128 if (!defined('RESTORE_SILENTLY')) {
129 echo '</li>';
133 // Restore links in modules.
134 foreach ($restore->mods as $name => $info) {
135 //If the module is being restored
136 if (isset($info->restore) && $info->restore == 1) {
137 //Check if the xxxx_decode_content_links_caller exists
138 include_once("$CFG->dirroot/mod/$name/restorelib.php");
139 $function_name = $name."_decode_content_links_caller";
140 if (function_exists($function_name)) {
141 if (!defined('RESTORE_SILENTLY')) {
142 echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
144 $status = $function_name($restore) && $status;
145 if (!defined('RESTORE_SILENTLY')) {
146 echo '</li>';
152 // For the course format call its decode_content_links method (if it exists)
153 $format = get_field('course', 'format', 'id', $restore->course_id);
154 if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
155 include_once("$CFG->dirroot/course/format/$format/restorelib.php");
156 $function_name = $format.'_decode_format_content_links_caller';
158 if (function_exists($function_name)) {
159 if (!defined('RESTORE_SILENTLY')) {
160 echo "<li>".get_string ("from")." ".get_string("format").' '.$format;
162 $status = $function_name($restore);
163 if (!defined('RESTORE_SILENTLY')) {
164 echo '</li>';
169 // Process all html text also in blocks too
170 if (!defined('RESTORE_SILENTLY')) {
171 echo '<li>'.get_string ('from').' '.get_string('blocks');
174 if ($blocks = get_records('block', 'visible', 1)) {
175 foreach ($blocks as $block) {
176 if ($blockobject = block_instance($block->name)) {
177 $blockobject->decode_content_links_caller($restore);
182 if (!defined('RESTORE_SILENTLY')) {
183 echo '</li>';
186 // Restore links in questions.
187 require_once("$CFG->dirroot/question/restorelib.php");
188 if (!defined('RESTORE_SILENTLY')) {
189 echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
191 $status = question_decode_content_links_caller($restore) && $status;
192 if (!defined('RESTORE_SILENTLY')) {
193 echo '</li>';
196 if (!defined('RESTORE_SILENTLY')) {
197 echo "</ul>";
200 return $status;
203 //This function is called from all xxxx_decode_content_links_caller(),
204 //its task is to ask all modules (maybe other linkable objects) to restore
205 //links to them.
206 function restore_decode_content_links_worker($content,$restore) {
207 global $CFG;
208 foreach($restore->mods as $name => $info) {
209 $function_name = $name."_decode_content_links";
210 if (function_exists($function_name)) {
211 $content = $function_name($content,$restore);
215 // For the current format, call decode_format_content_links if it exists
216 static $format_function_name;
217 if (!isset($format_function_name)) {
218 $format_function_name = false;
219 if ($format = get_field('course', 'format', 'id', $restore->course_id)) {
220 if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
221 include_once("$CFG->dirroot/course/format/$format/restorelib.php");
222 $function_name = $format.'_decode_format_content_links';
223 if (function_exists($function_name)) {
224 $format_function_name = $function_name;
229 // If the above worked - then we have a function to call
230 if ($format_function_name) {
231 $content = $format_function_name($content, $restore);
234 // For each block, call its encode_content_links method
235 static $blockobjects = null;
236 if (!isset($blockobjects)) {
237 $blockobjects = array();
238 if ($blocks = get_records('block', 'visible', 1)) {
239 foreach ($blocks as $block) {
240 if ($blockobject = block_instance($block->name)) {
241 $blockobjects[] = $blockobject;
247 foreach ($blockobjects as $blockobject) {
248 $content = $blockobject->decode_content_links($content,$restore);
251 return $content;
254 //This function converts all the wiki texts in the restored course
255 //to the Markdown format. Used only for backup files prior 2005041100.
256 //It calls every module xxxx_convert_wiki2markdown function
257 function restore_convert_wiki2markdown($restore) {
259 $status = true;
261 if (!defined('RESTORE_SILENTLY')) {
262 echo "<ul>";
264 foreach ($restore->mods as $name => $info) {
265 //If the module is being restored
266 if ($info->restore == 1) {
267 //Check if the xxxx_restore_wiki2markdown exists
268 $function_name = $name."_restore_wiki2markdown";
269 if (function_exists($function_name)) {
270 $status = $function_name($restore);
271 if (!defined('RESTORE_SILENTLY')) {
272 echo "<li>".get_string("modulenameplural",$name);
273 echo '</li>';
278 if (!defined('RESTORE_SILENTLY')) {
279 echo "</ul>";
281 return $status;
284 //This function receives a wiki text in the restore process and
285 //return it with every link to modules " modulename:moduleid"
286 //converted if possible. See the space before modulename!!
287 function restore_decode_wiki_content($content,$restore) {
289 global $CFG;
291 $result = $content;
293 $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
294 //We look for it
295 preg_match_all($searchstring,$content,$foundset);
296 //If found, then we are going to look for its new id (in backup tables)
297 if ($foundset[0]) {
298 //print_object($foundset); //Debug
299 //Iterate over foundset[2]. They are the old_ids
300 foreach($foundset[2] as $old_id) {
301 //We get the needed variables here (course id)
302 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
303 //Personalize the searchstring
304 $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
305 //If it is a link to this course, update the link to its new location
306 if($rec->new_id) {
307 //Now replace it
308 $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
309 } else {
310 //It's a foreign link so redirect it to its original URL
311 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
315 return $result;
319 //This function read the xml file and store it data from the info zone in an object
320 function restore_read_xml_info ($xml_file) {
322 //We call the main read_xml function, with todo = INFO
323 $info = restore_read_xml ($xml_file,"INFO",false);
325 return $info;
328 //This function read the xml file and store it data from the course header zone in an object
329 function restore_read_xml_course_header ($xml_file) {
331 //We call the main read_xml function, with todo = COURSE_HEADER
332 $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
334 return $info;
337 //This function read the xml file and store its data from the blocks in a object
338 function restore_read_xml_blocks ($restore, $xml_file) {
340 //We call the main read_xml function, with todo = BLOCKS
341 $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
343 return $info;
346 //This function read the xml file and store its data from the sections in a object
347 function restore_read_xml_sections ($xml_file) {
349 //We call the main read_xml function, with todo = SECTIONS
350 $info = restore_read_xml ($xml_file,"SECTIONS",false);
352 return $info;
355 //This function read the xml file and store its data from the course format in an object
356 function restore_read_xml_formatdata ($xml_file) {
358 //We call the main read_xml function, with todo = FORMATDATA
359 $info = restore_read_xml ($xml_file,'FORMATDATA',false);
361 return $info;
364 //This function read the xml file and store its data from the metacourse in a object
365 function restore_read_xml_metacourse ($xml_file) {
367 //We call the main read_xml function, with todo = METACOURSE
368 $info = restore_read_xml ($xml_file,"METACOURSE",false);
370 return $info;
373 //This function read the xml file and store its data from the gradebook in a object
374 function restore_read_xml_gradebook ($restore, $xml_file) {
376 //We call the main read_xml function, with todo = GRADEBOOK
377 $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
379 return $info;
382 //This function read the xml file and store its data from the users in
383 //backup_ids->info db (and user's id in $info)
384 function restore_read_xml_users ($restore,$xml_file) {
386 //We call the main read_xml function, with todo = USERS
387 $info = restore_read_xml ($xml_file,"USERS",$restore);
389 return $info;
392 //This function read the xml file and store its data from the messages in
393 //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
394 function restore_read_xml_messages ($restore,$xml_file) {
396 //We call the main read_xml function, with todo = MESSAGES
397 $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
399 return $info;
402 //This function read the xml file and store its data from the blogs in
403 //backup_ids->blog and backup_ids->blog_tag and db (and their counters in info)
404 function restore_read_xml_blogs ($restore,$xml_file) {
406 //We call the main read_xml function, with todo = BLOGS
407 $info = restore_read_xml ($xml_file,"BLOGS",$restore);
409 return $info;
413 //This function read the xml file and store its data from the questions in
414 //backup_ids->info db (and category's id in $info)
415 function restore_read_xml_questions ($restore,$xml_file) {
417 //We call the main read_xml function, with todo = QUESTIONS
418 $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
420 return $info;
423 //This function read the xml file and store its data from the scales in
424 //backup_ids->info db (and scale's id in $info)
425 function restore_read_xml_scales ($restore,$xml_file) {
427 //We call the main read_xml function, with todo = SCALES
428 $info = restore_read_xml ($xml_file,"SCALES",$restore);
430 return $info;
433 //This function read the xml file and store its data from the groups in
434 //backup_ids->info db (and group's id in $info)
435 function restore_read_xml_groups ($restore,$xml_file) {
437 //We call the main read_xml function, with todo = GROUPS
438 $info = restore_read_xml ($xml_file,"GROUPS",$restore);
440 return $info;
443 //This function read the xml file and store its data from the groupings in
444 //backup_ids->info db (and grouping's id in $info)
445 function restore_read_xml_groupings ($restore,$xml_file) {
447 //We call the main read_xml function, with todo = GROUPINGS
448 $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
450 return $info;
453 //This function read the xml file and store its data from the groupings in
454 //backup_ids->info db (and grouping's id in $info)
455 function restore_read_xml_groupings_groups ($restore,$xml_file) {
457 //We call the main read_xml function, with todo = GROUPINGS
458 $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
460 return $info;
463 //This function read the xml file and store its data from the events (course) in
464 //backup_ids->info db (and event's id in $info)
465 function restore_read_xml_events ($restore,$xml_file) {
467 //We call the main read_xml function, with todo = EVENTS
468 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
470 return $info;
473 //This function read the xml file and store its data from the modules in
474 //backup_ids->info
475 function restore_read_xml_modules ($restore,$xml_file) {
477 //We call the main read_xml function, with todo = MODULES
478 $info = restore_read_xml ($xml_file,"MODULES",$restore);
480 return $info;
483 //This function read the xml file and store its data from the logs in
484 //backup_ids->info
485 function restore_read_xml_logs ($restore,$xml_file) {
487 //We call the main read_xml function, with todo = LOGS
488 $info = restore_read_xml ($xml_file,"LOGS",$restore);
490 return $info;
493 function restore_read_xml_roles ($xml_file) {
494 //We call the main read_xml function, with todo = ROLES
495 $info = restore_read_xml ($xml_file,"ROLES",false);
497 return $info;
500 //This function prints the contents from the info parammeter passed
501 function restore_print_info ($info) {
503 global $CFG;
505 $status = true;
506 if ($info) {
507 $table = new object();
508 //This is tha align to every ingo table
509 $table->align = array ("right","left");
510 //This is the nowrap clause
511 $table->wrap = array ("","nowrap");
512 //The width
513 $table->width = "70%";
514 //Put interesting info in table
515 //The backup original name
516 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
517 $tab[0][1] = $info->backup_name;
518 //The moodle version
519 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
520 $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
521 //The backup version
522 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
523 $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
524 //The backup date
525 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
526 $tab[3][1] = userdate($info->backup_date);
527 //Is this the same Moodle install?
528 if (!empty($info->original_siteidentifier)) {
529 $tab[4][0] = "<b>".get_string("backupfromthissite").":</b>";
530 if (backup_is_same_site($info)) {
531 $tab[4][1] = get_string('yes');
532 } else {
533 $tab[4][1] = get_string('no');
536 //Print title
537 print_heading(get_string("backup").":");
538 $table->data = $tab;
539 //Print backup general info
540 print_table($table);
542 if ($info->backup_backup_version <= 2005070500) {
543 notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
546 //Now backup contents in another table
547 $tab = array();
548 //First mods info
549 $mods = $info->mods;
550 $elem = 0;
551 foreach ($mods as $key => $mod) {
552 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
553 if ($mod->backup == "false") {
554 $tab[$elem][1] = get_string("notincluded");
555 } else {
556 if ($mod->userinfo == "true") {
557 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
558 } else {
559 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
561 if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
562 foreach ($mod->instances as $instance) {
563 if ($instance->backup) {
564 $elem++;
565 $tab[$elem][0] = $instance->name;
566 if ($instance->userinfo == 'true') {
567 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
568 } else {
569 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
575 $elem++;
577 //Metacourse info
578 $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
579 if ($info->backup_metacourse == "true") {
580 $tab[$elem][1] = get_string("yes");
581 } else {
582 $tab[$elem][1] = get_string("no");
584 $elem++;
585 //Users info
586 $tab[$elem][0] = "<b>".get_string("users").":</b>";
587 $tab[$elem][1] = get_string($info->backup_users);
588 $elem++;
589 //Logs info
590 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
591 if ($info->backup_logs == "true") {
592 $tab[$elem][1] = get_string("yes");
593 } else {
594 $tab[$elem][1] = get_string("no");
596 $elem++;
597 //User Files info
598 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
599 if ($info->backup_user_files == "true") {
600 $tab[$elem][1] = get_string("yes");
601 } else {
602 $tab[$elem][1] = get_string("no");
604 $elem++;
605 //Course Files info
606 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
607 if ($info->backup_course_files == "true") {
608 $tab[$elem][1] = get_string("yes");
609 } else {
610 $tab[$elem][1] = get_string("no");
612 $elem++;
613 //site Files info
614 $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
615 if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
616 $tab[$elem][1] = get_string("yes");
617 } else {
618 $tab[$elem][1] = get_string("no");
620 $elem++;
621 //gradebook history info
622 $tab[$elem][0] = "<b>".get_string('gradebookhistories', 'grades').":</b>";
623 if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
624 $tab[$elem][1] = get_string("yes");
625 } else {
626 $tab[$elem][1] = get_string("no");
628 $elem++;
629 //Messages info (only showed if present)
630 if ($info->backup_messages == 'true') {
631 $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
632 $tab[$elem][1] = get_string('yes');
633 $elem++;
634 } else {
635 //Do nothing
637 $elem++;
638 //Blogs info (only showed if present)
639 if (isset($info->backup_blogs) && $info->backup_blogs == 'true') {
640 $tab[$elem][0] = "<b>".get_string('blogs','blog').":</b>";
641 $tab[$elem][1] = get_string('yes');
642 $elem++;
643 } else {
644 //Do nothing
646 $table->data = $tab;
647 //Print title
648 print_heading(get_string("backupdetails").":");
649 //Print backup general info
650 print_table($table);
651 } else {
652 $status = false;
655 return $status;
658 //This function prints the contents from the course_header parammeter passed
659 function restore_print_course_header ($course_header) {
661 $status = true;
662 if ($course_header) {
663 $table = new object();
664 //This is tha align to every ingo table
665 $table->align = array ("right","left");
666 //The width
667 $table->width = "70%";
668 //Put interesting course header in table
669 //The course name
670 $tab[0][0] = "<b>".get_string("name").":</b>";
671 $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
672 //The course summary
673 $tab[1][0] = "<b>".get_string("summary").":</b>";
674 $tab[1][1] = $course_header->course_summary;
675 $table->data = $tab;
676 //Print title
677 print_heading(get_string("course").":");
678 //Print backup course header info
679 print_table($table);
680 } else {
681 $status = false;
683 return $status;
687 * Given one user object (from backup file), perform all the neccesary
688 * checks is order to decide how that user will be handled on restore.
690 * Note the function requires $user->mnethostid to be already calculated
691 * so it's caller responsibility to set it
693 * This function is used both by @restore_precheck_users() and
694 * @restore_create_users() to get consistent results in both places
696 * It returns:
697 * - one user object (from DB), if match has been found and user will be remapped
698 * - boolean true if the user needs to be created
699 * - boolean false if some conflict happened and the user cannot be handled
701 * Each test is responsible for returning its results and interrupt
702 * execution. At the end, boolean true (user needs to be created) will be
703 * returned if no test has interrupted that.
705 * Here it's the logic applied, keep it updated:
707 * If restoring users from same site backup:
708 * 1A - If match by id and username and mnethost => ok, return target user
709 * 1B - If match by id and mnethost and user is deleted in DB and
710 * match by email LIKE 'backup_email%' => ok, return target user
711 * 1C - If match by id and mnethost and user is deleted in backup file
712 * and match by email = email_without_time(backup_email) => ok, return target user
713 * 1D - If match by username and mnethost and doesn't match by id => conflict, return false
714 * 1E - else => user needs to be created, return true
716 * if restoring from another site backup:
717 * 2A - If match by username and mnethost and
718 * (email or non-zero firstaccess) => ok, return target user
719 * 2B - Note: we cannot handle "deleted" situations here as far
720 * as username gets modified and id cannot be used here
721 * 2C - If match by username and mnethost and not
722 * by (email or non-zero firstaccess) => conflict, return false
723 * 2D - else => user needs to be created, return true
725 function restore_check_user($restore, $user) {
726 global $CFG;
728 // Verify mnethostid is set, return error if not
729 // it's parent responsibility to define that before
730 // arriving here
731 if (empty($user->mnethostid)) {
732 debugging("restore_check_user() wrong use, mnethostid not set for user $user->username", DEBUG_DEVELOPER);
733 return false;
736 // Handle checks from same site backups
737 if (backup_is_same_site($restore)) {
739 // 1A - If match by id and username and mnethost => ok, return target user
740 if ($rec = get_record('user', 'id', $user->id, 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
741 return $rec; // Matching user found, return it
744 // 1B - Handle users deleted in DB and "alive" in backup file
745 // 1B1- If match by id and mnethost and user is deleted in DB and
746 // match by email LIKE 'backup_email.%' => ok, return target user
747 // Note: for deleted users email is stored in username field, hence we
748 // are looking there for emails in the query below. See delete_user()
749 if ($rec = get_record_sql("SELECT *
750 FROM {$CFG->prefix}user u
751 WHERE id = $user->id
752 AND mnethostid = $user->mnethostid
753 AND deleted = 1
754 AND username LIKE '$user->email.%'")) {
755 return $rec; // Matching user, deleted in DB found, return it
758 // 1C - Handle users deleted in backup file and "alive" in DB
759 // 1C1- If match by id and mnethost and user is deleted in backup file
760 // and match by email = email_without_time(backup_email) => ok, return target user
761 if ($user->deleted) {
762 // Trim time() from email
763 // Note: for deleted users email is stored in username field, hece
764 // we are trimming the username field to get the email. See delete_user()
765 $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
766 if ($rec = get_record_sql("SELECT *
767 FROM {$CFG->prefix}user u
768 WHERE id = $user->id
769 AND mnethostid = $user->mnethostid
770 AND email = '$trimemail'")) {
771 return $rec; // Matching user, deleted in backup file found, return it
775 // 1D - If match by username and mnethost and doesn't match by id => conflict, return false
776 if ($rec = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
777 if ($user->id != $rec->id) {
778 return false; // Conflict, username already exists and belongs to another id
782 // Handle checks from different site backups
783 } else {
785 // 2A - If match by username and mnethost and
786 // (email or non-zero firstaccess) => ok, return target user
787 if ($rec = get_record_sql("SELECT *
788 FROM {$CFG->prefix}user u
789 WHERE username = '$user->username'
790 AND mnethostid = $user->mnethostid
791 AND (
792 email = '$user->email'
793 OR (
794 firstaccess != 0
795 AND firstaccess = $user->firstaccess
797 )")) {
798 return $rec; // Matching user
801 // 2B - Handle users deleted in DB and "alive" in backup file
802 // Note: for deleted users email is stored in username field, hence we
803 // are looking there for emails in the query below. See delete_user()
804 // Note: for deleted users md5(username) is stored *sometimes* in the
805 // email field, hence we are looking there for usernames in the query below
806 // 2B - Note: we cannot handle "deleted" situations here as far
807 // as username gets modified and id cannot be used either
808 // 2B1-deleted = 1 AND email = md5(username) AND mnsethostid AND (username like $user->email.% OR firstaccess)
809 // 2B2 deleted and mnsethostid AND username like $user->email.% AND firstaccess
811 // 2C - Handle users deleted in backup file and "alive" in DB
813 // 2D - If match by username and mnethost and not
814 // by (email or non-zero firstaccess) => conflict, return false
815 if ($rec = get_record_sql("SELECT *
816 FROM {$CFG->prefix}user u
817 WHERE username = '$user->username'
818 AND mnethostid = $user->mnethostid
819 AND NOT (
820 email = '$user->email'
821 OR (
822 firstaccess != 0
823 AND firstaccess = $user->firstaccess
825 )")) {
826 return false; // Conflict, username/mnethostid already exist and belong to another user (by email/firstaccess)
830 // Arrived here, return true as the user will need to be created and no
831 // conflicts have been found in the logic above. This covers:
832 // 1E - else => user needs to be created, return true
833 // 2D - else => user needs to be created, return true
834 return true;
838 * For all the users being restored, check if they are going to cause problems
839 * before executing the restore process itself, detecting situations like:
840 * - conflicts preventing restore to continue - provided by @restore_check_user()
841 * - prevent creation of users if not allowed - check some global settings/caps
843 function restore_precheck_users($xml_file, $restore, &$problems) {
844 global $CFG;
846 $status = true; // Init $status
848 // We aren't restoring users, nothing to check, allow continue
849 if ($restore->users == 2) {
850 return true;
853 // Get array of users from xml file and load them in backup_ids table
854 if (!$info = restore_read_xml_users($restore,$xml_file)) {
855 return true; // No users, nothing to check, allow continue
858 // We are going to map mnethostid, so load all the available ones
859 $mnethosts = get_records('mnet_host', '', '', 'wwwroot', 'wwwroot, id');
861 // Calculate the context we are going to use for capability checking
862 if (!empty($restore->course_id)) { // Know the target (existing) course, check capabilities there
863 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
864 } else if (!empty($restore->restore_restorecatto)) { // Know the category, check capabilities there
865 $context = get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto);
866 } else { // Last resort, check capabilities at system level
867 $context = get_context_instance(CONTEXT_SYSTEM);
870 // Calculate if we have perms to create users, by checking:
871 // to 'moodle/restore:createuser' and 'moodle/restore:userinfo'
872 // and also observe $CFG->disableusercreationonrestore
873 $cancreateuser = false;
874 if (has_capability('moodle/restore:createuser', $context) and
875 has_capability('moodle/restore:userinfo', $context) and
876 empty($CFG->disableusercreationonrestore)) { // Can create users
878 $cancreateuser = true;
881 // Iterate over all users, checking if they are likely to cause problems on restore
882 $counter = 0;
883 foreach ($info->users as $userid) {
884 $rec = backup_getid($restore->backup_unique_code, 'user', $userid);
885 $user = $rec->info;
887 // Find the correct mnethostid for user before performing any further check
888 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
889 $user->mnethostid = $CFG->mnet_localhost_id;
890 } else {
891 // fast url-to-id lookups
892 if (isset($mnethosts[$user->mnethosturl])) {
893 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
894 } else {
895 $user->mnethostid = $CFG->mnet_localhost_id;
899 // Calculate the best way to handle this user from backup file
900 $usercheck = restore_check_user($restore, $user);
902 if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to
904 } else if ($usercheck === false) { // Found conflict, report it as problem
905 $problems[] = get_string('restoreuserconflict', '', $user->username);
906 $status = false;
908 } else if ($usercheck === true) { // User needs to be created, check if we are able
909 if (!$cancreateuser) { // Cannot create, report as problem
911 $problems[] = get_string('restorecannotcreateuser', '', $user->username);
912 $status = false;
915 } else { // Shouldn't arrive here ever, something is for sure wrong in restore_check_user()
916 if (!defined('RESTORE_SILENTLY')) {
917 notify('Unexpected error pre-checking user ' . s($user->username) . ' from backup file');
918 return false;
922 // Do some output
923 $counter++;
924 if ($counter % 10 == 0) {
925 if (!defined('RESTORE_SILENTLY')) {
926 echo ".";
927 if ($counter % 200 == 0) {
928 echo "<br />";
931 backup_flush(300);
935 return $status;
938 //This function create a new course record.
939 //When finished, course_header contains the id of the new course
940 function restore_create_new_course($restore,&$course_header) {
942 global $CFG, $SESSION;
944 $status = true;
946 $fullname = $course_header->course_fullname;
947 $shortname = $course_header->course_shortname;
948 $currentfullname = "";
949 $currentshortname = "";
950 $counter = 0;
951 //Iteratere while the name exists
952 do {
953 if ($counter) {
954 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
955 $suffixshort = "_".$counter;
956 } else {
957 $suffixfull = "";
958 $suffixshort = "";
960 $currentfullname = $fullname.$suffixfull;
961 // Limit the size of shortname - database column accepts <= 100 chars
962 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
963 $coursefull = get_record("course","fullname",addslashes($currentfullname));
964 $courseshort = get_record("course","shortname",addslashes($currentshortname));
965 $counter++;
966 } while ($coursefull || $courseshort);
968 //New name = currentname
969 $course_header->course_fullname = $currentfullname;
970 $course_header->course_shortname = $currentshortname;
972 // first try to get it from restore
973 if ($restore->restore_restorecatto) {
974 $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
977 // else we try to get it from the xml file
978 //Now calculate the category
979 if (empty($category)) {
980 $category = get_record("course_categories","id",$course_header->category->id,
981 "name",addslashes($course_header->category->name));
984 //If no exists, try by name only
985 if (!$category) {
986 $category = get_record("course_categories","name",addslashes($course_header->category->name));
989 //If no exists, get category id 1
990 if (!$category) {
991 $category = get_record("course_categories","id","1");
994 //If category 1 doesn'exists, lets create the course category (get it from backup file)
995 if (!$category) {
996 $ins_category = new object();
997 $ins_category->name = addslashes($course_header->category->name);
998 $ins_category->parent = 0;
999 $ins_category->sortorder = 0;
1000 $ins_category->coursecount = 0;
1001 $ins_category->visible = 0; //To avoid interferences with the rest of the site
1002 $ins_category->timemodified = time();
1003 $newid = insert_record("course_categories",$ins_category);
1004 $category->id = $newid;
1005 $category->name = $course_header->category->name;
1007 //If exists, put new category id
1008 if ($category) {
1009 $course_header->category->id = $category->id;
1010 $course_header->category->name = $category->name;
1011 //Error, cannot locate category
1012 } else {
1013 $course_header->category->id = 0;
1014 $course_header->category->name = get_string("unknowncategory");
1015 $status = false;
1018 //Create the course_object
1019 if ($status) {
1020 $course = new object();
1021 $course->category = addslashes($course_header->category->id);
1022 $course->password = addslashes($course_header->course_password);
1023 $course->fullname = addslashes($course_header->course_fullname);
1024 $course->shortname = addslashes($course_header->course_shortname);
1025 $course->idnumber = addslashes($course_header->course_idnumber);
1026 $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
1027 $course->summary = addslashes($course_header->course_summary);
1028 $course->format = addslashes($course_header->course_format);
1029 $course->showgrades = addslashes($course_header->course_showgrades);
1030 $course->newsitems = addslashes($course_header->course_newsitems);
1031 $course->teacher = addslashes($course_header->course_teacher);
1032 $course->teachers = addslashes($course_header->course_teachers);
1033 $course->student = addslashes($course_header->course_student);
1034 $course->students = addslashes($course_header->course_students);
1035 $course->guest = addslashes($course_header->course_guest);
1036 $course->startdate = addslashes($course_header->course_startdate);
1037 $course->startdate += $restore->course_startdateoffset;
1038 $course->numsections = addslashes($course_header->course_numsections);
1039 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
1040 $course->maxbytes = addslashes($course_header->course_maxbytes);
1041 $course->showreports = addslashes($course_header->course_showreports);
1042 if (isset($course_header->course_groupmode)) {
1043 $course->groupmode = addslashes($course_header->course_groupmode);
1045 if (isset($course_header->course_groupmodeforce)) {
1046 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
1048 if (isset($course_header->course_defaultgroupingid)) {
1049 //keep the original now - convert after groupings restored
1050 $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
1052 $course->lang = addslashes($course_header->course_lang);
1053 $course->theme = addslashes($course_header->course_theme);
1054 $course->cost = addslashes($course_header->course_cost);
1055 $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
1056 $course->marker = addslashes($course_header->course_marker);
1057 $course->visible = addslashes($course_header->course_visible);
1058 $course->hiddensections = addslashes($course_header->course_hiddensections);
1059 $course->timecreated = addslashes($course_header->course_timecreated);
1060 $course->timemodified = addslashes($course_header->course_timemodified);
1061 $course->metacourse = addslashes($course_header->course_metacourse);
1062 $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
1063 $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
1064 $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
1065 $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
1066 $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
1067 if ($course->enrolstartdate) { //Roll course dates
1068 $course->enrolstartdate += $restore->course_startdateoffset;
1070 $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
1071 if ($course->enrolenddate) { //Roll course dates
1072 $course->enrolenddate += $restore->course_startdateoffset;
1074 $course->enrolperiod = addslashes($course_header->course_enrolperiod);
1075 //Calculate sortorder field
1076 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
1077 FROM ' . $CFG->prefix . 'course
1078 WHERE category=' . $course->category);
1079 if (!empty($sortmax->max)) {
1080 $course->sortorder = $sortmax->max + 1;
1081 unset($sortmax);
1082 } else {
1083 $course->sortorder = 100;
1086 //Now, recode some languages (Moodle 1.5)
1087 if ($course->lang == 'ma_nt') {
1088 $course->lang = 'mi_nt';
1091 //Disable course->metacourse if avoided in restore config
1092 if (!$restore->metacourse) {
1093 $course->metacourse = 0;
1096 //Check if the theme exists in destination server
1097 $themes = get_list_of_themes();
1098 if (!in_array($course->theme, $themes)) {
1099 $course->theme = '';
1102 //Now insert the record
1103 $newid = insert_record("course",$course);
1104 if ($newid) {
1105 //save old and new course id
1106 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
1107 //Replace old course_id in course_header
1108 $course_header->course_id = $newid;
1109 $SESSION->restore->course_id = $newid;
1110 return $newid;
1111 } else {
1112 $status = false;
1116 return $status;
1121 //This function creates all the block stuff when restoring courses
1122 //It calls selectively to restore_create_block_instances() for 1.5
1123 //and above backups. Upwards compatible with old blocks.
1124 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
1125 global $CFG;
1126 $status = true;
1128 blocks_delete_all_on_page(PAGE_COURSE_VIEW, $restore->course_id);
1129 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
1130 if (empty($blockinfo)) {
1131 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
1132 $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
1133 blocks_repopulate_page($newpage);
1134 } else {
1135 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
1136 $blockrecords = get_records_select('block', '', '', 'name, id');
1137 $temp_blocks_l = array();
1138 $temp_blocks_r = array();
1139 @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
1140 $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
1141 foreach($temp_blocks as $blockposition => $blocks) {
1142 $blockweight = 0;
1143 foreach($blocks as $blockname) {
1144 if(!isset($blockrecords[$blockname])) {
1145 // We don't know anything about this block!
1146 continue;
1148 $blockinstance = new stdClass;
1149 // Remove any - prefix before doing the name-to-id mapping
1150 if(substr($blockname, 0, 1) == '-') {
1151 $blockname = substr($blockname, 1);
1152 $blockinstance->visible = 0;
1153 } else {
1154 $blockinstance->visible = 1;
1156 $blockinstance->blockid = $blockrecords[$blockname]->id;
1157 $blockinstance->pageid = $restore->course_id;
1158 $blockinstance->pagetype = PAGE_COURSE_VIEW;
1159 $blockinstance->position = $blockposition;
1160 $blockinstance->weight = $blockweight;
1161 if(!$status = insert_record('block_instance', $blockinstance)) {
1162 $status = false;
1164 ++$blockweight;
1168 } else if($backup_block_format == 'instances') {
1169 $status = restore_create_block_instances($restore,$xml_file);
1172 return $status;
1176 //This function creates all the block_instances from xml when restoring in a
1177 //new course
1178 function restore_create_block_instances($restore,$xml_file) {
1179 global $CFG;
1180 $status = true;
1182 //Check it exists
1183 if (!file_exists($xml_file)) {
1184 $status = false;
1186 //Get info from xml
1187 if ($status) {
1188 $info = restore_read_xml_blocks($restore,$xml_file);
1191 if(empty($info->instances)) {
1192 return $status;
1195 // First of all, iterate over the blocks to see which distinct pages we have
1196 // in our hands and arrange the blocks accordingly.
1197 $pageinstances = array();
1198 foreach($info->instances as $instance) {
1200 //pagetype and pageid black magic, we have to handle the case of blocks for the
1201 //course, blocks from other pages in that course etc etc etc.
1203 if($instance->pagetype == PAGE_COURSE_VIEW) {
1204 // This one's easy...
1205 $instance->pageid = $restore->course_id;
1207 } else if (!empty($CFG->showblocksonmodpages)) {
1208 $parts = explode('-', $instance->pagetype);
1209 if($parts[0] == 'mod') {
1210 if(!$restore->mods[$parts[1]]->restore) {
1211 continue;
1213 $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
1215 if (empty($getid->new_id)) {
1216 // Failed, perhaps the module was not included in the restore MDL-13554
1217 continue;
1219 $instance->pageid = $getid->new_id;
1221 else {
1222 // Not invented here ;-)
1223 continue;
1226 } else {
1227 // do not restore activity blocks if disabled
1228 continue;
1231 if(!isset($pageinstances[$instance->pagetype])) {
1232 $pageinstances[$instance->pagetype] = array();
1234 if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
1235 $pageinstances[$instance->pagetype][$instance->pageid] = array();
1238 $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
1241 $blocks = get_records_select('block', 'visible = 1', '', 'name, id, multiple');
1243 // For each type of page we have restored
1244 foreach($pageinstances as $thistypeinstances) {
1246 // For each page id of that type
1247 foreach($thistypeinstances as $thisidinstances) {
1249 $addedblocks = array();
1250 $maxweights = array();
1252 // For each block instance in that page
1253 foreach($thisidinstances as $instance) {
1255 if(!isset($blocks[$instance->name])) {
1256 //We are trying to restore a block we don't have...
1257 continue;
1260 //If we have already added this block once and multiples aren't allowed, disregard it
1261 if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
1262 continue;
1265 //If its the first block we add to a new position, start weight counter equal to 0.
1266 if(empty($maxweights[$instance->position])) {
1267 $maxweights[$instance->position] = 0;
1270 //If the instance weight is greater than the weight counter (we skipped some earlier
1271 //blocks most probably), bring it back in line.
1272 if($instance->weight > $maxweights[$instance->position]) {
1273 $instance->weight = $maxweights[$instance->position];
1276 //Add this instance
1277 $instance->blockid = $blocks[$instance->name]->id;
1279 // This will only be set if we come from 1.7 and above backups
1280 // Also, must do this before insert (insert_record unsets id)
1281 if (!empty($instance->id)) {
1282 $oldid = $instance->id;
1283 } else {
1284 $oldid = 0;
1287 if ($instance->id = insert_record('block_instance', $instance)) {
1288 // Create block instance
1289 if (!$blockobj = block_instance($instance->name, $instance)) {
1290 $status = false;
1291 break;
1293 // Run the block restore if needed
1294 if ($blockobj->backuprestore_instancedata_used()) {
1295 // Get restore information
1296 $data = backup_getid($restore->backup_unique_code,'block_instance',$oldid);
1297 $data->new_id = $instance->id; // For completeness
1298 if (!$blockobj->instance_restore($restore, $data)) {
1299 $status = false;
1300 break;
1303 // Save oldid after block restore process because info will be over-written with blank string
1304 if ($oldid) {
1305 backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
1308 } else {
1309 $status = false;
1310 break;
1313 //Get an object for the block and tell it it's been restored so it can update dates
1314 //etc. if necessary
1315 if ($blockobj = block_instance($instance->name,$instance)) {
1316 $blockobj->after_restore($restore);
1319 //Now we can increment the weight counter
1320 ++$maxweights[$instance->position];
1322 //Keep track of block types we have already added
1323 $addedblocks[$instance->name] = true;
1329 return $status;
1332 //This function creates all the course_sections and course_modules from xml
1333 //when restoring in a new course or simply checks sections and create records
1334 //in backup_ids when restoring in a existing course
1335 function restore_create_sections(&$restore, $xml_file) {
1337 global $CFG,$db;
1339 $status = true;
1340 //Check it exists
1341 if (!file_exists($xml_file)) {
1342 $status = false;
1344 //Get info from xml
1345 if ($status) {
1346 $info = restore_read_xml_sections($xml_file);
1348 //Put the info in the DB, recoding ids and saving the in backup tables
1350 $sequence = "";
1352 if ($info) {
1353 //For each, section, save it to db
1354 foreach ($info->sections as $key => $sect) {
1355 $sequence = "";
1356 $section = new object();
1357 $section->course = $restore->course_id;
1358 $section->section = $sect->number;
1359 $section->summary = backup_todb($sect->summary);
1360 $section->visible = $sect->visible;
1361 $section->sequence = "";
1362 //Now calculate the section's newid
1363 $newid = 0;
1364 if ($restore->restoreto == RESTORETO_NEW_COURSE) {
1365 //Save it to db (only if restoring to new course)
1366 $newid = insert_record("course_sections",$section);
1367 } else {
1368 //Get section id when restoring in existing course
1369 $rec = get_record("course_sections","course",$restore->course_id,
1370 "section",$section->section);
1371 //If section exists, has empty summary and backup has some summary, use it. MDL-8848
1372 if ($rec && empty($rec->summary) && !empty($section->summary)) {
1373 $rec->summary = $section->summary;
1374 update_record("course_sections", $rec);
1376 //If that section doesn't exist, get section 0 (every mod will be
1377 //asigned there
1378 if(!$rec) {
1379 $rec = get_record("course_sections","course",$restore->course_id,
1380 "section","0");
1382 //New check. If section 0 doesn't exist, insert it here !!
1383 //Teorically this never should happen but, in practice, some users
1384 //have reported this issue.
1385 if(!$rec) {
1386 $zero_sec = new object();
1387 $zero_sec->course = $restore->course_id;
1388 $zero_sec->section = 0;
1389 $zero_sec->summary = "";
1390 $zero_sec->sequence = "";
1391 $newid = insert_record("course_sections",$zero_sec);
1392 $rec->id = $newid;
1393 $rec->sequence = "";
1395 $newid = $rec->id;
1396 $sequence = $rec->sequence;
1398 if ($newid) {
1399 //save old and new section id
1400 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
1401 } else {
1402 $status = false;
1404 //If all is OK, go with associated mods
1405 if ($status) {
1406 //If we have mods in the section
1407 if (!empty($sect->mods)) {
1408 //For each mod inside section
1409 foreach ($sect->mods as $keym => $mod) {
1410 // Yu: This part is called repeatedly for every instance,
1411 // so it is necessary to set the granular flag and check isset()
1412 // when the first instance of this type of mod is processed.
1414 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
1416 if (!isset($restore->mods[$mod->type]->granular)) {
1417 if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
1418 // This defines whether we want to restore specific
1419 // instances of the modules (granular restore), or
1420 // whether we don't care and just want to restore
1421 // all module instances (non-granular).
1422 $restore->mods[$mod->type]->granular = true;
1423 } else {
1424 $restore->mods[$mod->type]->granular = false;
1428 //Check if we've to restore this module (and instance)
1429 if (!empty($restore->mods[$mod->type]->restore)) {
1430 if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
1431 || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
1432 && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
1434 //Get the module id from modules
1435 $module = get_record("modules","name",$mod->type);
1436 if ($module) {
1437 $course_module = new object();
1438 $course_module->course = $restore->course_id;
1439 $course_module->module = $module->id;
1440 $course_module->section = $newid;
1441 $course_module->added = $mod->added;
1442 $course_module->score = $mod->score;
1443 $course_module->indent = $mod->indent;
1444 $course_module->visible = $mod->visible;
1445 $course_module->groupmode = $mod->groupmode;
1446 if ($mod->groupingid and $grouping = restore_grouping_getid($restore, $mod->groupingid)) {
1447 $course_module->groupingid = $grouping->new_id;
1448 } else {
1449 $course_module->groupingid = 0;
1451 $course_module->groupmembersonly = $mod->groupmembersonly;
1452 $course_module->instance = 0;
1453 //NOTE: The instance (new) is calculated and updated in db in the
1454 // final step of the restore. We don't know it yet.
1455 //print_object($course_module); //Debug
1456 //Save it to db
1457 if ($mod->idnumber) {
1458 if (grade_verify_idnumber($mod->idnumber, $restore->course_id)) {
1459 $course_module->idnumber = $mod->idnumber;
1463 $newidmod = insert_record("course_modules", addslashes_recursive($course_module));
1464 if ($newidmod) {
1465 //save old and new module id
1466 //In the info field, we save the original instance of the module
1467 //to use it later
1468 backup_putid ($restore->backup_unique_code,"course_modules",
1469 $keym,$newidmod,$mod->instance);
1471 $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
1472 } else {
1473 $status = false;
1475 //Now, calculate the sequence field
1476 if ($status) {
1477 if ($sequence) {
1478 $sequence .= ",".$newidmod;
1479 } else {
1480 $sequence = $newidmod;
1483 } else {
1484 $status = false;
1491 //If all is OK, update sequence field in course_sections
1492 if ($status) {
1493 if (isset($sequence)) {
1494 $update_rec = new object();
1495 $update_rec->id = $newid;
1496 $update_rec->sequence = $sequence;
1497 $status = update_record("course_sections",$update_rec);
1501 } else {
1502 $status = false;
1504 return $status;
1507 //Called to set up any course-format specific data that may be in the file
1508 function restore_set_format_data($restore,$xml_file) {
1509 global $CFG,$db;
1511 $status = true;
1512 //Check it exists
1513 if (!file_exists($xml_file)) {
1514 return false;
1516 //Load data from XML to info
1517 if(!($info = restore_read_xml_formatdata($xml_file))) {
1518 return false;
1521 //Process format data if there is any
1522 if (isset($info->format_data)) {
1523 if(!$format=get_field('course','format','id',$restore->course_id)) {
1524 return false;
1526 // If there was any data then it must have a restore method
1527 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
1528 if(!file_exists($file)) {
1529 return false;
1531 require_once($file);
1532 $function=$format.'_restore_format_data';
1533 if(!function_exists($function)) {
1534 return false;
1536 return $function($restore,$info->format_data);
1539 // If we got here then there's no data, but that's cool
1540 return true;
1543 //This function creates all the metacourse data from xml, notifying
1544 //about each incidence
1545 function restore_create_metacourse($restore,$xml_file) {
1547 global $CFG,$db;
1549 $status = true;
1550 //Check it exists
1551 if (!file_exists($xml_file)) {
1552 $status = false;
1554 //Get info from xml
1555 if ($status) {
1556 //Load data from XML to info
1557 $info = restore_read_xml_metacourse($xml_file);
1560 //Process info about metacourse
1561 if ($status and $info) {
1562 //Process child records
1563 if (!empty($info->childs)) {
1564 foreach ($info->childs as $child) {
1565 $dbcourse = false;
1566 $dbmetacourse = false;
1567 //Check if child course exists in destination server
1568 //(by id in the same server or by idnumber and shortname in other server)
1569 if (backup_is_same_site($restore)) {
1570 //Same server, lets see by id
1571 $dbcourse = get_record('course','id',$child->id);
1572 } else {
1573 //Different server, lets see by idnumber and shortname, and only ONE record
1574 $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1575 if ($dbcount == 1) {
1576 $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1579 //If child course has been found, insert data
1580 if ($dbcourse) {
1581 $dbmetacourse->child_course = $dbcourse->id;
1582 $dbmetacourse->parent_course = $restore->course_id;
1583 $status = insert_record ('course_meta',$dbmetacourse);
1584 } else {
1585 //Child course not found, notice!
1586 if (!defined('RESTORE_SILENTLY')) {
1587 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
1591 //Now, recreate student enrolments...
1592 sync_metacourse($restore->course_id);
1594 //Process parent records
1595 if (!empty($info->parents)) {
1596 foreach ($info->parents as $parent) {
1597 $dbcourse = false;
1598 $dbmetacourse = false;
1599 //Check if parent course exists in destination server
1600 //(by id in the same server or by idnumber and shortname in other server)
1601 if (backup_is_same_site($restore)) {
1602 //Same server, lets see by id
1603 $dbcourse = get_record('course','id',$parent->id);
1604 } else {
1605 //Different server, lets see by idnumber and shortname, and only ONE record
1606 $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1607 if ($dbcount == 1) {
1608 $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1611 //If parent course has been found, insert data if it is a metacourse
1612 if ($dbcourse) {
1613 if ($dbcourse->metacourse) {
1614 $dbmetacourse->parent_course = $dbcourse->id;
1615 $dbmetacourse->child_course = $restore->course_id;
1616 $status = insert_record ('course_meta',$dbmetacourse);
1617 //Now, recreate student enrolments in parent course
1618 sync_metacourse($dbcourse->id);
1619 } else {
1620 //Parent course isn't metacourse, notice!
1621 if (!defined('RESTORE_SILENTLY')) {
1622 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1625 } else {
1626 //Parent course not found, notice!
1627 if (!defined('RESTORE_SILENTLY')) {
1628 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1635 return $status;
1639 * This function migrades all the pre 1.9 gradebook data from xml
1641 function restore_migrate_old_gradebook($restore,$xml_file) {
1642 global $CFG;
1644 $status = true;
1645 //Check it exists
1646 if (!file_exists($xml_file)) {
1647 return false;
1650 // Get info from xml
1651 // info will contain the number of record to process
1652 $info = restore_read_xml_gradebook($restore, $xml_file);
1654 // If we have info, then process
1655 if (empty($info)) {
1656 return $status;
1659 // make sure top course category exists
1660 $course_category = grade_category::fetch_course_category($restore->course_id);
1661 $course_category->load_grade_item();
1663 // we need to know if all grade items that were backed up are being restored
1664 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1665 // i.e. the aggregated grades of that category
1667 $restoreall = true; // set to false if any grade_item is not selected/restored
1668 $importing = !empty($SESSION->restore->importing); // there should not be a way to import old backups, but anyway ;-)
1670 if ($importing || $restore->users == 2) {
1671 $restoreall = false;
1673 } else {
1674 $prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
1675 $prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
1677 // if any categories already present, skip restore of categories from backup
1678 if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
1679 $restoreall = false;
1681 unset($prev_grade_items);
1682 unset($prev_grade_cats);
1685 // force creation of all grade_items - the course_modules already exist
1686 grade_force_full_regrading($restore->course_id);
1687 grade_grab_course_grades($restore->course_id);
1689 // Start ul
1690 if (!defined('RESTORE_SILENTLY')) {
1691 echo '<ul>';
1694 /// Process letters
1695 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1696 // respect current grade letters if defined
1697 if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
1698 if (!defined('RESTORE_SILENTLY')) {
1699 echo '<li>'.get_string('gradeletters','grades').'</li>';
1701 // Fetch recordset_size records in each iteration
1702 $recs = get_records_select("backup_ids","table_name = 'grade_letter' AND backup_code = $restore->backup_unique_code",
1704 "old_id");
1705 if ($recs) {
1706 foreach ($recs as $rec) {
1707 // Get the full record from backup_ids
1708 $data = backup_getid($restore->backup_unique_code,'grade_letter',$rec->old_id);
1709 if ($data) {
1710 $info = $data->info;
1711 $dbrec = new object();
1712 $dbrec->contextid = $context->id;
1713 $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['GRADE_LOW']['0']['#']);
1714 $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
1715 insert_record('grade_letters', $dbrec);
1721 if (!defined('RESTORE_SILENTLY')) {
1722 echo '<li>'.get_string('categories','grades').'</li>';
1724 //Fetch recordset_size records in each iteration
1725 $recs = get_records_select("backup_ids","table_name = 'grade_category' AND backup_code = $restore->backup_unique_code",
1726 "old_id",
1727 "old_id");
1728 $cat_count = count($recs);
1729 if ($recs) {
1730 foreach ($recs as $rec) {
1731 //Get the full record from backup_ids
1732 $data = backup_getid($restore->backup_unique_code,'grade_category',$rec->old_id);
1733 if ($data) {
1734 //Now get completed xmlized object
1735 $info = $data->info;
1737 if ($restoreall) {
1738 if ($cat_count == 1) {
1739 $course_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
1740 $course_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
1741 $course_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
1742 $course_category->aggregateonlygraded = 0;
1743 $course_category->update('restore');
1744 $grade_category = $course_category;
1746 } else {
1747 $grade_category = new grade_category();
1748 $grade_category->courseid = $restore->course_id;
1749 $grade_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
1750 $grade_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
1751 $grade_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
1752 $grade_category->aggregateonlygraded = 0;
1753 $grade_category->insert('restore');
1754 $grade_category->load_grade_item(); // force cretion of grade_item
1757 } else {
1758 $grade_category = null;
1761 /// now, restore grade_items
1762 $items = array();
1763 if (!empty($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'])) {
1764 //Iterate over items
1765 foreach ($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'] as $ite_info) {
1766 $modname = backup_todb($ite_info['#']['MODULE_NAME']['0']['#'], false);
1767 $olditeminstance = backup_todb($ite_info['#']['CMINSTANCE']['0']['#'], false);
1768 if (!$mod = backup_getid($restore->backup_unique_code,$modname, $olditeminstance)) {
1769 continue; // not restored
1771 $iteminstance = $mod->new_id;
1772 if (!$cm = get_coursemodule_from_instance($modname, $iteminstance, $restore->course_id)) {
1773 continue; // does not exist
1776 if (!$grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0))) {
1777 continue; // no item yet??
1780 if ($grade_category) {
1781 $grade_item->sortorder = backup_todb($ite_info['#']['SORT_ORDER']['0']['#'], false);
1782 $grade_item->set_parent($grade_category->id);
1785 if ($importing
1786 or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
1787 // module instance not selected when restored using granular
1788 // skip this item
1789 continue;
1792 //Now process grade excludes
1793 if (empty($ite_info['#']['GRADE_EXCEPTIONS'])) {
1794 continue;
1797 foreach($ite_info['#']['GRADE_EXCEPTIONS']['0']['#']['GRADE_EXCEPTION'] as $exc_info) {
1798 if ($u = backup_getid($restore->backup_unique_code,"user",backup_todb($exc_info['#']['USERID']['0']['#']))) {
1799 $userid = $u->new_id;
1800 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
1801 $grade_grade->excluded = 1;
1802 if ($grade_grade->id) {
1803 $grade_grade->update('restore');
1804 } else {
1805 $grade_grade->insert('restore');
1815 if (!defined('RESTORE_SILENTLY')) {
1816 //End ul
1817 echo '</ul>';
1820 return $status;
1824 * This function creates all the gradebook data from xml
1826 function restore_create_gradebook($restore,$xml_file) {
1827 global $CFG;
1829 $status = true;
1830 //Check it exists
1831 if (!file_exists($xml_file)) {
1832 return false;
1835 // Get info from xml
1836 // info will contain the number of record to process
1837 $info = restore_read_xml_gradebook($restore, $xml_file);
1839 // If we have info, then process
1840 if (empty($info)) {
1841 return $status;
1844 if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
1845 $restore_histories = true;
1846 } else {
1847 $restore_histories = false;
1850 // make sure top course category exists
1851 $course_category = grade_category::fetch_course_category($restore->course_id);
1852 $course_category->load_grade_item();
1854 // we need to know if all grade items that were backed up are being restored
1855 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1856 // i.e. the aggregated grades of that category
1858 $restoreall = true; // set to false if any grade_item is not selected/restored or already exist
1859 $importing = !empty($SESSION->restore->importing);
1861 if ($importing || $restore->users == 2) {
1862 $restoreall = false;
1864 } else {
1865 $prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
1866 $prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
1868 // if any categories already present, skip restore of categories from backup - course item or category already exist
1869 if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
1870 $restoreall = false;
1872 unset($prev_grade_items);
1873 unset($prev_grade_cats);
1875 if ($restoreall) {
1876 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = $restore->backup_unique_code", "", "old_id")) {
1877 foreach ($recs as $rec) {
1878 if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
1880 $info = $data->info;
1881 // do not restore if this grade_item is a mod, and
1882 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1884 if ($itemtype == 'mod') {
1885 $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1886 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1888 if (empty($restore->mods[$itemmodule]->granular)) {
1889 continue;
1890 } else if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
1891 continue;
1893 // at least one activity should not be restored - do not restore categories and manual items at all
1894 $restoreall = false;
1895 break;
1903 // Start ul
1904 if (!defined('RESTORE_SILENTLY')) {
1905 echo '<ul>';
1908 // array of restored categories - speedup ;-)
1909 $cached_categories = array();
1910 $outcomes = array();
1912 /// Process letters
1913 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1914 // respect current grade letters if defined
1915 if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
1916 if (!defined('RESTORE_SILENTLY')) {
1917 echo '<li>'.get_string('gradeletters','grades').'</li>';
1919 // Fetch recordset_size records in each iteration
1920 $recs = get_records_select("backup_ids","table_name = 'grade_letters' AND backup_code = $restore->backup_unique_code",
1922 "old_id");
1923 if ($recs) {
1924 foreach ($recs as $rec) {
1925 // Get the full record from backup_ids
1926 $data = backup_getid($restore->backup_unique_code,'grade_letters',$rec->old_id);
1927 if ($data) {
1928 $info = $data->info;
1929 $dbrec = new object();
1930 $dbrec->contextid = $context->id;
1931 $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
1932 $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
1933 insert_record('grade_letters', $dbrec);
1939 /// Preprocess outcomes - do not store them yet!
1940 if ($status and !$importing and $restoreall) {
1941 if (!defined('RESTORE_SILENTLY')) {
1942 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1944 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1946 "old_id");
1947 if ($recs) {
1948 foreach ($recs as $rec) {
1949 //Get the full record from backup_ids
1950 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1951 if ($data) {
1952 $info = $data->info;
1954 //first find out if outcome already exists
1955 $shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1957 if ($candidates = get_records_sql("SELECT *
1958 FROM {$CFG->prefix}grade_outcomes
1959 WHERE (courseid IS NULL OR courseid = $restore->course_id)
1960 AND shortname = '$shortname'
1961 ORDER BY courseid ASC, id ASC")) {
1962 $grade_outcome = reset($candidates);
1963 $outcomes[$rec->old_id] = $grade_outcome;
1964 continue;
1967 $dbrec = new object();
1969 if (has_capability('moodle/grade:manageoutcomes', get_context_instance(CONTEXT_SYSTEM))) {
1970 $oldoutcome = backup_todb($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']);
1971 if (empty($oldoutcome)) {
1972 //site wide
1973 $dbrec->courseid = null;
1974 } else {
1975 //course only
1976 $dbrec->courseid = $restore->course_id;
1978 } else {
1979 // no permission to add site outcomes
1980 $dbrec->courseid = $restore->course_id;
1983 //Get the fields
1984 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#'], false);
1985 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#'], false);
1986 $dbrec->scaleid = backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#'], false);
1987 $dbrec->description = backup_todb($info['GRADE_OUTCOME']['#']['DESCRIPTION']['0']['#'], false);
1988 $dbrec->timecreated = backup_todb($info['GRADE_OUTCOME']['#']['TIMECREATED']['0']['#'], false);
1989 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME']['#']['TIMEMODIFIED']['0']['#'], false);
1990 $dbrec->usermodified = backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#'], false);
1992 //Need to recode the scaleid
1993 if ($scale = backup_getid($restore->backup_unique_code, 'scale', $dbrec->scaleid)) {
1994 $dbrec->scaleid = $scale->new_id;
1997 //Need to recode the usermodified
1998 if ($modifier = backup_getid($restore->backup_unique_code, 'user', $dbrec->usermodified)) {
1999 $dbrec->usermodified = $modifier->new_id;
2002 $grade_outcome = new grade_outcome($dbrec, false);
2003 $outcomes[$rec->old_id] = $grade_outcome;
2009 /// Process grade items and grades
2010 if ($status) {
2011 if (!defined('RESTORE_SILENTLY')) {
2012 echo '<li>'.get_string('gradeitems','grades').'</li>';
2014 $counter = 0;
2016 //Fetch recordset_size records in each iteration
2017 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
2018 "id", // restore in the backup order
2019 "old_id");
2021 if ($recs) {
2022 foreach ($recs as $rec) {
2023 //Get the full record from backup_ids
2024 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
2025 if ($data) {
2026 $info = $data->info;
2028 // first find out if category or normal item
2029 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
2030 if ($itemtype == 'course' or $itemtype == 'category') {
2031 if (!$restoreall or $importing) {
2032 continue;
2035 $oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
2036 if (!$cdata = backup_getid($restore->backup_unique_code,'grade_categories',$oldcat)) {
2037 continue;
2039 $cinfo = $cdata->info;
2040 unset($cdata);
2041 if ($itemtype == 'course') {
2043 $course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
2044 $course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
2045 $course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
2046 $course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
2047 $course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
2048 $course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
2049 $course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
2050 $course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
2051 $course_category->update('restore');
2053 $status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$course_category->id) && $status;
2054 $cached_categories[$oldcat] = $course_category;
2055 $grade_item = $course_category->get_grade_item();
2057 } else {
2058 $oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false);
2059 if (empty($cached_categories[$oldparent])) {
2060 debugging('parent not found '.$oldparent);
2061 continue; // parent not found, sorry
2063 $grade_category = new grade_category();
2064 $grade_category->courseid = $restore->course_id;
2065 $grade_category->parent = $cached_categories[$oldparent]->id;
2066 $grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
2067 $grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
2068 $grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
2069 $grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
2070 $grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
2071 $grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
2072 $grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
2073 $grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
2074 $grade_category->insert('restore');
2076 $status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$grade_category->id) && $status;
2077 $cached_categories[$oldcat] = $grade_category;
2078 $grade_item = $grade_category->get_grade_item(); // creates grade_item too
2080 unset($cinfo);
2082 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
2083 if (grade_verify_idnumber($idnumber, $restore->course_id)) {
2084 $grade_item->idnumber = $idnumber;
2087 $grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
2088 $grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
2089 $grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
2090 $grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
2091 $grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
2092 $grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
2093 $grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
2094 $grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
2095 $grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
2096 $grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
2097 $grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
2098 $grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
2099 $grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
2100 $grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
2101 $grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
2102 $grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
2104 if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
2105 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
2106 $grade_item->scaleid = $scale->new_id;
2109 if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) {
2110 $outcome = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false));
2111 $grade_item->outcomeid = $outcome->new_id;
2114 $grade_item->update('restore');
2115 $status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
2117 } else {
2118 if ($itemtype != 'mod' and (!$restoreall or $importing)) {
2119 // not extra gradebook stuff if restoring individual activities or something already there
2120 continue;
2123 $dbrec = new object();
2125 $dbrec->courseid = $restore->course_id;
2126 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
2127 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
2129 if ($itemtype == 'mod') {
2130 // iteminstance should point to new mod
2131 $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
2132 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $olditeminstance);
2133 $dbrec->iteminstance = $mod->new_id;
2134 if (!$cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id)) {
2135 // item not restored - no item
2136 continue;
2138 // keep in sync with activity idnumber
2139 $dbrec->idnumber = $cm->idnumber;
2141 } else {
2142 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
2144 if (grade_verify_idnumber($idnumber, $restore->course_id)) {
2145 //make sure the new idnumber is unique
2146 $dbrec->idnumber = $idnumber;
2150 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
2151 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
2152 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
2153 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false);
2154 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
2155 $dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
2156 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
2157 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
2158 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
2159 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
2160 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
2161 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
2162 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
2163 $dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
2164 $dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
2165 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
2166 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
2167 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
2168 $dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
2170 if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
2171 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
2172 $dbrec->scaleid = $scale->new_id;
2175 if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) {
2176 $oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
2177 if (empty($outcomes[$oldoutcome])) {
2178 continue; // error!
2180 if (empty($outcomes[$oldoutcome]->id)) {
2181 $outcomes[$oldoutcome]->insert('restore');
2182 $outcomes[$oldoutcome]->use_in($restore->course_id);
2183 backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id);
2185 $dbrec->outcomeid = $outcomes[$oldoutcome]->id;
2188 $grade_item = new grade_item($dbrec, false);
2189 $grade_item->insert('restore');
2190 if ($restoreall) {
2191 // set original parent if restored
2192 $oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
2193 if (!empty($cached_categories[$oldcat])) {
2194 $grade_item->set_parent($cached_categories[$oldcat]->id);
2197 $status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
2200 // no need to restore grades if user data is not selected or importing activities
2201 if ($importing
2202 or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
2203 // module instance not selected when restored using granular
2204 // skip this item
2205 continue;
2208 /// now, restore grade_grades
2209 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
2210 //Iterate over items
2211 foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) {
2213 $grade = new grade_grade();
2214 $grade->itemid = $grade_item->id;
2216 $olduser = backup_todb($g_info['#']['USERID']['0']['#'], false);
2217 $user = backup_getid($restore->backup_unique_code,"user",$olduser);
2218 $grade->userid = $user->new_id;
2220 $grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false);
2221 $grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false);
2222 $grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false);
2223 // need to find scaleid
2224 if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) {
2225 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false));
2226 $grade->rawscaleid = $scale->new_id;
2229 if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) {
2230 if ($modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) {
2231 $grade->usermodified = $modifier->new_id;
2235 $grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false);
2236 $grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false);
2237 $grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false);
2238 $grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false);
2239 $grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false);
2240 $grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false);
2241 $grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false);
2242 $grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false);
2243 $grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false);
2244 $grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false);
2245 $grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false);
2246 $grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false);
2247 $grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false);
2249 $grade->insert('restore');
2250 backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id);
2252 $counter++;
2253 if ($counter % 20 == 0) {
2254 if (!defined('RESTORE_SILENTLY')) {
2255 echo ".";
2256 if ($counter % 400 == 0) {
2257 echo "<br />";
2260 backup_flush(300);
2269 /// add outcomes that are not used when doing full restore
2270 if ($status and $restoreall) {
2271 foreach ($outcomes as $oldoutcome=>$grade_outcome) {
2272 if (empty($grade_outcome->id)) {
2273 $grade_outcome->insert('restore');
2274 $grade_outcome->use_in($restore->course_id);
2275 backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id);
2281 if ($status and !$importing and $restore_histories) {
2282 /// following code is very inefficient
2284 $gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
2285 $gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
2286 $gihcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
2287 $gohcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
2289 // Number of records to get in every chunk
2290 $recordset_size = 2;
2292 // process histories
2293 if ($gchcount && $status) {
2294 if (!defined('RESTORE_SILENTLY')) {
2295 echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
2297 $counter = 0;
2298 while ($counter < $gchcount) {
2299 //Fetch recordset_size records in each iteration
2300 $recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
2301 "old_id",
2302 "old_id",
2303 $counter,
2304 $recordset_size);
2305 if ($recs) {
2306 foreach ($recs as $rec) {
2307 //Get the full record from backup_ids
2308 $data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_id);
2309 if ($data) {
2310 //Now get completed xmlized object
2311 $info = $data->info;
2312 //traverse_xmlize($info); //Debug
2313 //print_object ($GLOBALS['traverse_array']); //Debug
2314 //$GLOBALS['traverse_array']=""; //Debug
2316 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
2317 if (empty($oldobj->new_id)) {
2318 // if the old object is not being restored, can't restoring its history
2319 $counter++;
2320 continue;
2322 $dbrec->oldid = $oldobj->new_id;
2323 $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
2324 $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
2325 $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2327 // loggeduser might not be restored, e.g. admin
2328 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2329 $dbrec->loggeduser = $oldobj->new_id;
2332 // this item might not have a parent at all, do not skip it if no parent is specified
2333 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
2334 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
2335 if (empty($oldobj->new_id)) {
2336 // if the parent category not restored
2337 $counter++;
2338 continue;
2341 $dbrec->parent = $oldobj->new_id;
2342 $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
2343 // path needs to be rebuilt
2344 if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
2345 // to preserve the path and make it work, we need to replace the categories one by one
2346 // we first get the list of categories in current path
2347 if ($paths = explode("/", $path)) {
2348 $newpath = '';
2349 foreach ($paths as $catid) {
2350 if ($catid) {
2351 // find the new corresponding path
2352 $oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
2353 $newpath .= "/$oldpath->new_id";
2356 $dbrec->path = $newpath;
2359 $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
2360 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
2361 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
2362 $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
2364 $dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
2365 $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
2366 $dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
2368 $dbrec->courseid = $restore->course_id;
2369 insert_record('grade_categories_history', $dbrec);
2370 unset($dbrec);
2373 //Increment counters
2374 $counter++;
2375 //Do some output
2376 if ($counter % 1 == 0) {
2377 if (!defined('RESTORE_SILENTLY')) {
2378 echo ".";
2379 if ($counter % 20 == 0) {
2380 echo "<br />";
2383 backup_flush(300);
2390 // process histories
2391 if ($gghcount && $status) {
2392 if (!defined('RESTORE_SILENTLY')) {
2393 echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
2395 $counter = 0;
2396 while ($counter < $gghcount) {
2397 //Fetch recordset_size records in each iteration
2398 $recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
2399 "old_id",
2400 "old_id",
2401 $counter,
2402 $recordset_size);
2403 if ($recs) {
2404 foreach ($recs as $rec) {
2405 //Get the full record from backup_ids
2406 $data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_id);
2407 if ($data) {
2408 //Now get completed xmlized object
2409 $info = $data->info;
2410 //traverse_xmlize($info); //Debug
2411 //print_object ($GLOBALS['traverse_array']); //Debug
2412 //$GLOBALS['traverse_array']=""; //Debug
2414 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
2415 if (empty($oldobj->new_id)) {
2416 // if the old object is not being restored, can't restoring its history
2417 $counter++;
2418 continue;
2420 $dbrec->oldid = $oldobj->new_id;
2421 $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
2422 $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
2423 $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2424 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2425 $dbrec->loggeduser = $oldobj->new_id;
2428 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
2429 $dbrec->itemid = $oldobj->new_id;
2430 if (empty($dbrec->itemid)) {
2431 $counter++;
2432 continue; // grade item not being restored
2434 $oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
2435 $dbrec->userid = $oldobj->new_id;
2436 $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
2437 $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
2438 $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
2439 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
2440 $dbrec->usermodified = $oldobj->new_id;
2443 if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
2444 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
2445 $dbrec->rawscaleid = $scale->new_id;
2448 $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
2449 $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
2450 $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
2451 $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
2452 $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
2453 $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
2454 $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
2455 $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
2456 $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
2457 $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
2458 $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
2460 insert_record('grade_grades_history', $dbrec);
2461 unset($dbrec);
2464 //Increment counters
2465 $counter++;
2466 //Do some output
2467 if ($counter % 1 == 0) {
2468 if (!defined('RESTORE_SILENTLY')) {
2469 echo ".";
2470 if ($counter % 20 == 0) {
2471 echo "<br />";
2474 backup_flush(300);
2481 // process histories
2483 if ($gihcount && $status) {
2484 if (!defined('RESTORE_SILENTLY')) {
2485 echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
2487 $counter = 0;
2488 while ($counter < $gihcount) {
2489 //Fetch recordset_size records in each iteration
2490 $recs = get_records_select("backup_ids","table_name = 'grade_items_history' AND backup_code = '$restore->backup_unique_code'",
2491 "old_id",
2492 "old_id",
2493 $counter,
2494 $recordset_size);
2495 if ($recs) {
2496 foreach ($recs as $rec) {
2497 //Get the full record from backup_ids
2498 $data = backup_getid($restore->backup_unique_code,'grade_items_history',$rec->old_id);
2499 if ($data) {
2500 //Now get completed xmlized object
2501 $info = $data->info;
2502 //traverse_xmlize($info); //Debug
2503 //print_object ($GLOBALS['traverse_array']); //Debug
2504 //$GLOBALS['traverse_array']=""; //Debug
2507 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
2508 if (empty($oldobj->new_id)) {
2509 // if the old object is not being restored, can't restoring its history
2510 $counter++;
2511 continue;
2513 $dbrec->oldid = $oldobj->new_id;
2514 $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
2515 $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
2516 $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2517 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2518 $dbrec->loggeduser = $oldobj->new_id;
2520 $dbrec->courseid = $restore->course_id;
2521 $oldobj = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
2522 $oldobj->categoryid = $category->new_id;
2523 if (empty($oldobj->categoryid)) {
2524 $counter++;
2525 continue; // category not restored
2528 $dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
2529 $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
2530 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
2532 // code from grade_items restore
2533 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
2534 // do not restore if this grade_item is a mod, and
2535 if ($dbrec->itemtype == 'mod') {
2537 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
2538 // module instance not selected when restored using granular
2539 // skip this item
2540 $counter++;
2541 continue;
2544 // iteminstance should point to new mod
2546 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
2547 $dbrec->iteminstance = $mod->new_id;
2549 } else if ($dbrec->itemtype == 'category') {
2550 // the item instance should point to the new grade category
2552 // only proceed if we are restoring all grade items
2553 if ($restoreall) {
2554 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
2555 $dbrec->iteminstance = $category->new_id;
2556 } else {
2557 // otherwise we can safely ignore this grade item and subsequent
2558 // grade_raws, grade_finals etc
2559 continue;
2561 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
2562 if ($restoreall) {
2563 // TODO any special code needed here to restore course item without duplicating it?
2564 // find the course category with depth 1, and course id = current course id
2565 // this would have been already restored
2567 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
2568 $dbrec->iteminstance = $cat->id;
2570 } else {
2571 $counter++;
2572 continue;
2576 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
2577 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
2578 $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
2579 $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
2580 $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
2581 $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
2582 $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
2583 if ($oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
2584 // scaleid is optional
2585 $dbrec->scaleid = $oldobj->new_id;
2587 if ($oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
2588 // outcome is optional
2589 $dbrec->outcomeid = $oldobj->new_id;
2591 $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
2592 $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
2593 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
2594 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
2595 $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
2596 $dbrec->display = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DISPLAY']['0']['#']);
2597 $dbrec->decimals = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DECIMALS']['0']['#']);
2598 $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
2599 $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
2600 $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
2601 $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
2603 insert_record('grade_items_history', $dbrec);
2604 unset($dbrec);
2607 //Increment counters
2608 $counter++;
2609 //Do some output
2610 if ($counter % 1 == 0) {
2611 if (!defined('RESTORE_SILENTLY')) {
2612 echo ".";
2613 if ($counter % 20 == 0) {
2614 echo "<br />";
2617 backup_flush(300);
2624 // process histories
2625 if ($gohcount && $status) {
2626 if (!defined('RESTORE_SILENTLY')) {
2627 echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
2629 $counter = 0;
2630 while ($counter < $gohcount) {
2631 //Fetch recordset_size records in each iteration
2632 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_history' AND backup_code = '$restore->backup_unique_code'",
2633 "old_id",
2634 "old_id",
2635 $counter,
2636 $recordset_size);
2637 if ($recs) {
2638 foreach ($recs as $rec) {
2639 //Get the full record from backup_ids
2640 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_history',$rec->old_id);
2641 if ($data) {
2642 //Now get completed xmlized object
2643 $info = $data->info;
2644 //traverse_xmlize($info); //Debug
2645 //print_object ($GLOBALS['traverse_array']); //Debug
2646 //$GLOBALS['traverse_array']=""; //Debug
2648 $oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
2649 if (empty($oldobj->new_id)) {
2650 // if the old object is not being restored, can't restoring its history
2651 $counter++;
2652 continue;
2654 $dbrec->oldid = $oldobj->new_id;
2655 $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
2656 $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
2657 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2658 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2659 $dbrec->loggeduser = $oldobj->new_id;
2661 $dbrec->courseid = $restore->course_id;
2662 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
2663 $dbrec->fullname= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
2664 $oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
2665 $dbrec->scaleid = $oldobj->new_id;
2666 $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
2668 insert_record('grade_outcomes_history', $dbrec);
2669 unset($dbrec);
2672 //Increment counters
2673 $counter++;
2674 //Do some output
2675 if ($counter % 1 == 0) {
2676 if (!defined('RESTORE_SILENTLY')) {
2677 echo ".";
2678 if ($counter % 20 == 0) {
2679 echo "<br />";
2682 backup_flush(300);
2690 if (!defined('RESTORE_SILENTLY')) {
2691 //End ul
2692 echo '</ul>';
2694 return $status;
2697 //This function creates all the user, user_students, user_teachers
2698 //user_course_creators and user_admins from xml
2699 function restore_create_users($restore,$xml_file) {
2701 global $CFG, $db;
2702 require_once ($CFG->dirroot.'/tag/lib.php');
2704 $authcache = array(); // Cache to get some bits from authentication plugins
2706 $status = true;
2707 //Check it exists
2708 if (!file_exists($xml_file)) {
2709 $status = false;
2711 //Get info from xml
2712 if ($status) {
2713 //info will contain the old_id of every user
2714 //in backup_ids->info will be the real info (serialized)
2715 $info = restore_read_xml_users($restore,$xml_file);
2718 //Now, get evey user_id from $info and user data from $backup_ids
2719 //and create the necessary db structures
2721 if (!empty($info->users)) {
2723 /// Grab mnethosts keyed by wwwroot, to map to id
2724 $mnethosts = get_records('mnet_host', '', '',
2725 'wwwroot', 'wwwroot, id');
2727 /// Get languages for quick search later
2728 $languages = get_list_of_languages();
2730 /// Iterate over all users loaded from xml
2731 $counter = 0;
2733 /// Init trailing messages
2734 $messages = array();
2735 foreach ($info->users as $userid) {
2736 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
2737 $user = $rec->info;
2738 foreach (array_keys(get_object_vars($user)) as $field) {
2739 if (!is_array($user->$field)) {
2740 $user->$field = backup_todb($user->$field);
2741 if (is_null($user->$field)) {
2742 $user->$field = '';
2747 //Now, recode some languages (Moodle 1.5)
2748 if ($user->lang == 'ma_nt') {
2749 $user->lang = 'mi_nt';
2752 //Country list updates - MDL-13060
2753 //Any user whose country code has been deleted or modified needs to be assigned a valid one.
2754 $country_update_map = array(
2755 'ZR' => 'CD',
2756 'TP' => 'TL',
2757 'FX' => 'FR',
2758 'KO' => 'RS',
2759 'CS' => 'RS',
2760 'WA' => 'GB');
2761 if (array_key_exists($user->country, $country_update_map)) {
2762 $user->country = $country_update_map[$user->country];
2765 //If language does not exist here - use site default
2766 if (!array_key_exists($user->lang, $languages)) {
2767 $user->lang = $CFG->lang;
2770 //Check if it's admin and coursecreator
2771 $is_admin = !empty($user->roles['admin']);
2772 $is_coursecreator = !empty($user->roles['coursecreator']);
2774 //Check if it's teacher and student
2775 $is_teacher = !empty($user->roles['teacher']);
2776 $is_student = !empty($user->roles['student']);
2778 //Check if it's needed
2779 $is_needed = !empty($user->roles['needed']);
2781 //Calculate if it is a course user
2782 //Has role teacher or student or needed
2783 $is_course_user = ($is_teacher or $is_student or $is_needed);
2785 // in case we are restoring to same server, look for user by id and username
2786 // it should return record always, but in sites rebuilt from scratch
2787 // and being reconstructed using course backups
2788 $user_data = false;
2789 if (backup_is_same_site($restore)) {
2790 $user_data = get_record('user', 'id', $user->id, 'username', addslashes($user->username));
2793 // Only try to perform mnethost/auth modifications if restoring to another server
2794 // or if, while restoring to same server, the user doesn't exists yet (rebuilt site)
2796 // So existing user data in same server *won't be modified by restore anymore*,
2797 // under any circumpstance. If somehting is wrong with existing data, it's server fault.
2798 if (!backup_is_same_site($restore) || (backup_is_same_site($restore) && !$user_data)) {
2799 //Calculate mnethostid
2800 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
2801 $user->mnethostid = $CFG->mnet_localhost_id;
2802 } else {
2803 // fast url-to-id lookups
2804 if (isset($mnethosts[$user->mnethosturl])) {
2805 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
2806 } else {
2807 $user->mnethostid = $CFG->mnet_localhost_id;
2810 //Arriving here, any user with mnet auth and using $CFG->mnet_localhost_id is wrong
2811 //as own server cannot be accesed over mnet. Change auth to manual and inform about the switch
2812 if ($user->auth == 'mnet' && $user->mnethostid == $CFG->mnet_localhost_id) {
2813 // Respect registerauth
2814 if ($CFG->registerauth == 'email') {
2815 $user->auth = 'email';
2816 } else {
2817 $user->auth = 'manual';
2819 // inform about the automatic switch of authentication/host
2820 if(empty($user->mnethosturl)) {
2821 $user->mnethosturl = '----';
2823 $messages[] = get_string('mnetrestore_extusers_switchuserauth', 'admin', $user);
2826 unset($user->mnethosturl);
2828 //To store user->id along all the iteration
2829 $newid=null;
2830 //check if it exists (by username) and get its id
2831 $user_exists = true;
2832 if (!backup_is_same_site($restore) || !$user_data) { /// Restoring to another server, or rebuilding site (failed id&
2833 /// login search above), look for existing user based on fields
2834 $user_data = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid);
2837 if (!$user_data) {
2838 $user_exists = false;
2839 } else {
2840 $newid = $user_data->id;
2843 //Flags to see what parts are we going to restore
2844 $create_user = true;
2845 $create_roles = true;
2846 $create_custom_profile_fields = true;
2847 $create_tags = true;
2848 $create_preferences = true;
2850 //If we are restoring course users and it isn't a course user
2851 if ($restore->users == 1 and !$is_course_user) {
2852 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
2853 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
2854 $create_user = false;
2855 $create_roles = false;
2856 $create_custom_profile_fields = false;
2857 $create_tags = false;
2858 $create_preferences = false;
2861 if ($user_exists and $create_user) {
2862 //If user exists mark its newid in backup_ids (the same than old)
2863 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
2864 $create_user = false;
2865 $create_custom_profile_fields = false;
2866 $create_tags = false;
2867 $create_preferences = false;
2870 //Here, if create_user, do it
2871 if ($create_user) {
2872 //Unset the id because it's going to be inserted with a new one
2873 unset ($user->id);
2874 // relink the descriptions
2875 $user->description = stripslashes($user->description);
2877 /// Disable pictures based on global setting or existing empty value (old backups can contain wrong empties)
2878 if (!empty($CFG->disableuserimages) || empty($user->picture)) {
2879 $user->picture = 0;
2882 //We need to analyse the AUTH field to recode it:
2883 // - if the field isn't set, we are in a pre 1.4 backup and $CFG->registerauth will decide
2884 // - if the auth isn't enabled in target site, $CFG->registerauth will decide
2885 // - finally, if the auth resulting isn't enabled, default to 'manual'
2886 if (empty($user->auth) || !is_enabled_auth($user->auth)) {
2887 if ($CFG->registerauth == 'email') {
2888 $user->auth = 'email';
2889 } else {
2890 $user->auth = 'manual';
2893 if (!is_enabled_auth($user->auth)) { // Final auth check verify, default to manual if not enabled
2894 $user->auth = 'manual';
2897 // Now that we know the auth method, for users to be created without pass
2898 // if password handling is internal and reset password is available
2899 // we set the password to "restored" (plain text), so the login process
2900 // will know how to handle that situation in order to allow the user to
2901 // recover the password. MDL-20846
2902 if (empty($user->password)) { // Only if restore comes without password
2903 if (!array_key_exists($user->auth, $authcache)) { // Not in cache
2904 $userauth = new stdClass();
2905 $authplugin = get_auth_plugin($user->auth);
2906 $userauth->preventpassindb = $authplugin->prevent_local_passwords();
2907 $userauth->isinternal = $authplugin->is_internal();
2908 $userauth->canresetpwd = $authplugin->can_reset_password();
2909 $authcache[$user->auth] = $userauth;
2910 } else {
2911 $userauth = $authcache[$user->auth]; // Get from cache
2914 // Most external plugins do not store passwords locally
2915 if (!empty($userauth->preventpassindb)) {
2916 $user->password = 'not cached';
2918 // If Moodle is responsible for storing/validating pwd and reset functionality is available, mark
2919 } else if ($userauth->isinternal and $userauth->canresetpwd) {
2920 $user->password = 'restored';
2924 //We need to process the POLICYAGREED field to recalculate it:
2925 // - if the destination site is different (by wwwroot) reset it.
2926 // - if the destination site is the same (by wwwroot), leave it unmodified
2928 if (!backup_is_same_site($restore)) {
2929 $user->policyagreed = 0;
2930 } else {
2931 //Nothing to do, we are in the same server
2934 //Check if the theme exists in destination server
2935 $themes = get_list_of_themes();
2936 if (!in_array($user->theme, $themes)) {
2937 $user->theme = '';
2940 //We are going to create the user
2941 //The structure is exactly as we need
2943 $newid = insert_record ("user", addslashes_recursive($user));
2944 //Put the new id
2945 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
2948 ///TODO: This seccion is to support pre 1.7 course backups, using old roles
2949 /// teacher, coursecreator, student.... providing a basic mapping to new ones.
2950 /// Someday we'll drop support for them and this section will be safely deleted (2.0?)
2951 //Here, if create_roles, do it as necessary
2952 if ($create_roles) {
2953 //Get the newid and current info from backup_ids
2954 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2955 $newid = $data->new_id;
2956 $currinfo = $data->info.",";
2958 //Now, depending of the role, create records in user_studentes and user_teacher
2959 //and/or mark it in backup_ids
2961 if ($is_admin) {
2962 //If the record (user_admins) doesn't exists
2963 //Only put status in backup_ids
2964 $currinfo = $currinfo."admin,";
2965 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2967 if ($is_coursecreator) {
2968 //If the record (user_coursecreators) doesn't exists
2969 //Only put status in backup_ids
2970 $currinfo = $currinfo."coursecreator,";
2971 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2973 if ($is_needed) {
2974 //Only put status in backup_ids
2975 $currinfo = $currinfo."needed,";
2976 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2978 if ($is_teacher) {
2979 //If the record (teacher) doesn't exists
2980 //Put status in backup_ids
2981 $currinfo = $currinfo."teacher,";
2982 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2983 //Set course and user
2984 $user->roles['teacher']->course = $restore->course_id;
2985 $user->roles['teacher']->userid = $newid;
2987 //Need to analyse the enrol field
2988 // - if it isn't set, set it to $CFG->enrol
2989 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2990 // - if we are in the same server (by wwwroot), maintain it unmodified.
2991 if (empty($user->roles['teacher']->enrol)) {
2992 $user->roles['teacher']->enrol = $CFG->enrol;
2993 } else if (!backup_is_same_site($restore)) {
2994 $user->roles['teacher']->enrol = $CFG->enrol;
2995 } else {
2996 //Nothing to do. Leave it unmodified
2999 $rolesmapping = $restore->rolesmapping;
3000 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
3001 if ($user->roles['teacher']->editall) {
3002 role_assign($rolesmapping['defaultteacheredit'],
3003 $newid,
3005 $context->id,
3006 $user->roles['teacher']->timestart,
3007 $user->roles['teacher']->timeend,
3009 $user->roles['teacher']->enrol);
3011 // editting teacher
3012 } else {
3013 // non editting teacher
3014 role_assign($rolesmapping['defaultteacher'],
3015 $newid,
3017 $context->id,
3018 $user->roles['teacher']->timestart,
3019 $user->roles['teacher']->timeend,
3021 $user->roles['teacher']->enrol);
3024 if ($is_student) {
3026 //Put status in backup_ids
3027 $currinfo = $currinfo."student,";
3028 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
3029 //Set course and user
3030 $user->roles['student']->course = $restore->course_id;
3031 $user->roles['student']->userid = $newid;
3033 //Need to analyse the enrol field
3034 // - if it isn't set, set it to $CFG->enrol
3035 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
3036 // - if we are in the same server (by wwwroot), maintain it unmodified.
3037 if (empty($user->roles['student']->enrol)) {
3038 $user->roles['student']->enrol = $CFG->enrol;
3039 } else if (!backup_is_same_site($restore)) {
3040 $user->roles['student']->enrol = $CFG->enrol;
3041 } else {
3042 //Nothing to do. Leave it unmodified
3044 $rolesmapping = $restore->rolesmapping;
3045 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
3047 role_assign($rolesmapping['defaultstudent'],
3048 $newid,
3050 $context->id,
3051 $user->roles['student']->timestart,
3052 $user->roles['student']->timeend,
3054 $user->roles['student']->enrol);
3057 if (!$is_course_user) {
3058 //If the record (user) doesn't exists
3059 if (!record_exists("user","id",$newid)) {
3060 //Put status in backup_ids
3061 $currinfo = $currinfo."user,";
3062 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
3067 /// Here, if create_custom_profile_fields, do it as necessary
3068 if ($create_custom_profile_fields) {
3069 if (isset($user->user_custom_profile_fields)) {
3070 foreach($user->user_custom_profile_fields as $udata) {
3071 /// If the profile field has data and the profile shortname-datatype is defined in server
3072 if ($udata->field_data) {
3073 if ($field = get_record('user_info_field', 'shortname', $udata->field_name, 'datatype', $udata->field_type)) {
3074 /// Insert the user_custom_profile_field
3075 $rec = new object();
3076 $rec->userid = $newid;
3077 $rec->fieldid = $field->id;
3078 $rec->data = $udata->field_data;
3079 insert_record('user_info_data', $rec);
3086 /// Here, if create_tags, do it as necessary
3087 if ($create_tags) {
3088 /// if tags are enabled and there are user tags
3089 if (!empty($CFG->usetags) && isset($user->user_tags)) {
3090 $tags = array();
3091 foreach($user->user_tags as $user_tag) {
3092 $tags[] = $user_tag->rawname;
3094 tag_set('user', $newid, $tags);
3098 //Here, if create_preferences, do it as necessary
3099 if ($create_preferences) {
3100 if (isset($user->user_preferences)) {
3101 foreach($user->user_preferences as $user_preference) {
3102 //We check if that user_preference exists in DB
3103 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
3104 //Prepare the record and insert it
3105 $user_preference->userid = $newid;
3106 $status = insert_record("user_preferences",$user_preference);
3112 //Do some output
3113 $counter++;
3114 if ($counter % 10 == 0) {
3115 if (!defined('RESTORE_SILENTLY')) {
3116 echo ".";
3117 if ($counter % 200 == 0) {
3118 echo "<br />";
3121 backup_flush(300);
3123 } /// End of loop over all the users loaded from xml
3125 /// Inform about all the messages geerated while restoring users
3126 if (!defined('RESTORE_SILENTLY')) {
3127 if ($messages) {
3128 echo '<ul>';
3129 foreach ($messages as $message) {
3130 echo '<li>' . $message . '</li>';
3132 echo '</ul>';
3137 return $status;
3140 //This function creates all the structures messages and contacts
3141 function restore_create_messages($restore,$xml_file) {
3143 global $CFG;
3145 $status = true;
3146 //Check it exists
3147 if (!file_exists($xml_file)) {
3148 $status = false;
3150 //Get info from xml
3151 if ($status) {
3152 //info will contain the id and name of every table
3153 //(message, message_read and message_contacts)
3154 //in backup_ids->info will be the real info (serialized)
3155 $info = restore_read_xml_messages($restore,$xml_file);
3157 //If we have info, then process messages & contacts
3158 if ($info > 0) {
3159 //Count how many we have
3160 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
3161 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
3162 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
3163 if ($unreadcount || $readcount || $contactcount) {
3164 //Start ul
3165 if (!defined('RESTORE_SILENTLY')) {
3166 echo '<ul>';
3168 //Number of records to get in every chunk
3169 $recordset_size = 4;
3171 //Process unread
3172 if ($unreadcount) {
3173 if (!defined('RESTORE_SILENTLY')) {
3174 echo '<li>'.get_string('unreadmessages','message').'</li>';
3176 $counter = 0;
3177 while ($counter < $unreadcount) {
3178 //Fetch recordset_size records in each iteration
3179 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3180 if ($recs) {
3181 foreach ($recs as $rec) {
3182 //Get the full record from backup_ids
3183 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
3184 if ($data) {
3185 //Now get completed xmlized object
3186 $info = $data->info;
3187 //traverse_xmlize($info); //Debug
3188 //print_object ($GLOBALS['traverse_array']); //Debug
3189 //$GLOBALS['traverse_array']=""; //Debug
3190 //Now build the MESSAGE record structure
3191 $dbrec = new object();
3192 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
3193 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
3194 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
3195 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
3196 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
3197 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
3198 //We have to recode the useridfrom field
3199 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
3200 if ($user) {
3201 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
3202 $dbrec->useridfrom = $user->new_id;
3204 //We have to recode the useridto field
3205 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
3206 if ($user) {
3207 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
3208 $dbrec->useridto = $user->new_id;
3210 //Check if the record doesn't exist in DB!
3211 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
3212 'useridto', $dbrec->useridto,
3213 'timecreated',$dbrec->timecreated);
3214 if (!$exist) {
3215 //Not exist. Insert
3216 $status = insert_record('message',$dbrec);
3217 } else {
3218 //Duplicate. Do nothing
3221 //Do some output
3222 $counter++;
3223 if ($counter % 10 == 0) {
3224 if (!defined('RESTORE_SILENTLY')) {
3225 echo ".";
3226 if ($counter % 200 == 0) {
3227 echo "<br />";
3230 backup_flush(300);
3237 //Process read
3238 if ($readcount) {
3239 if (!defined('RESTORE_SILENTLY')) {
3240 echo '<li>'.get_string('readmessages','message').'</li>';
3242 $counter = 0;
3243 while ($counter < $readcount) {
3244 //Fetch recordset_size records in each iteration
3245 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3246 if ($recs) {
3247 foreach ($recs as $rec) {
3248 //Get the full record from backup_ids
3249 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
3250 if ($data) {
3251 //Now get completed xmlized object
3252 $info = $data->info;
3253 //traverse_xmlize($info); //Debug
3254 //print_object ($GLOBALS['traverse_array']); //Debug
3255 //$GLOBALS['traverse_array']=""; //Debug
3256 //Now build the MESSAGE_READ record structure
3257 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
3258 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
3259 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
3260 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
3261 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
3262 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
3263 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
3264 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
3265 //We have to recode the useridfrom field
3266 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
3267 if ($user) {
3268 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
3269 $dbrec->useridfrom = $user->new_id;
3271 //We have to recode the useridto field
3272 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
3273 if ($user) {
3274 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
3275 $dbrec->useridto = $user->new_id;
3277 //Check if the record doesn't exist in DB!
3278 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
3279 'useridto', $dbrec->useridto,
3280 'timecreated',$dbrec->timecreated);
3281 if (!$exist) {
3282 //Not exist. Insert
3283 $status = insert_record('message_read',$dbrec);
3284 } else {
3285 //Duplicate. Do nothing
3288 //Do some output
3289 $counter++;
3290 if ($counter % 10 == 0) {
3291 if (!defined('RESTORE_SILENTLY')) {
3292 echo ".";
3293 if ($counter % 200 == 0) {
3294 echo "<br />";
3297 backup_flush(300);
3304 //Process contacts
3305 if ($contactcount) {
3306 if (!defined('RESTORE_SILENTLY')) {
3307 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
3309 $counter = 0;
3310 while ($counter < $contactcount) {
3311 //Fetch recordset_size records in each iteration
3312 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3313 if ($recs) {
3314 foreach ($recs as $rec) {
3315 //Get the full record from backup_ids
3316 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
3317 if ($data) {
3318 //Now get completed xmlized object
3319 $info = $data->info;
3320 //traverse_xmlize($info); //Debug
3321 //print_object ($GLOBALS['traverse_array']); //Debug
3322 //$GLOBALS['traverse_array']=""; //Debug
3323 //Now build the MESSAGE_CONTACTS record structure
3324 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
3325 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
3326 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
3327 //We have to recode the userid field
3328 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
3329 if ($user) {
3330 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
3331 $dbrec->userid = $user->new_id;
3333 //We have to recode the contactid field
3334 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
3335 if ($user) {
3336 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
3337 $dbrec->contactid = $user->new_id;
3339 //Check if the record doesn't exist in DB!
3340 $exist = get_record('message_contacts','userid',$dbrec->userid,
3341 'contactid', $dbrec->contactid);
3342 if (!$exist) {
3343 //Not exist. Insert
3344 $status = insert_record('message_contacts',$dbrec);
3345 } else {
3346 //Duplicate. Do nothing
3349 //Do some output
3350 $counter++;
3351 if ($counter % 10 == 0) {
3352 if (!defined('RESTORE_SILENTLY')) {
3353 echo ".";
3354 if ($counter % 200 == 0) {
3355 echo "<br />";
3358 backup_flush(300);
3364 if (!defined('RESTORE_SILENTLY')) {
3365 //End ul
3366 echo '</ul>';
3372 return $status;
3375 //This function creates all the structures for blogs and blog tags
3376 function restore_create_blogs($restore,$xml_file) {
3378 global $CFG;
3380 $status = true;
3381 //Check it exists
3382 if (!file_exists($xml_file)) {
3383 $status = false;
3385 //Get info from xml
3386 if ($status) {
3387 //info will contain the number of blogs in the backup file
3388 //in backup_ids->info will be the real info (serialized)
3389 $info = restore_read_xml_blogs($restore,$xml_file);
3391 //If we have info, then process blogs & blog_tags
3392 if ($info > 0) {
3393 //Count how many we have
3394 $blogcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'blog');
3395 if ($blogcount) {
3396 //Number of records to get in every chunk
3397 $recordset_size = 4;
3399 //Process blog
3400 if ($blogcount) {
3401 $counter = 0;
3402 while ($counter < $blogcount) {
3403 //Fetch recordset_size records in each iteration
3404 $recs = get_records_select("backup_ids","table_name = 'blog' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3405 if ($recs) {
3406 foreach ($recs as $rec) {
3407 //Get the full record from backup_ids
3408 $data = backup_getid($restore->backup_unique_code,"blog",$rec->old_id);
3409 if ($data) {
3410 //Now get completed xmlized object
3411 $info = $data->info;
3412 //traverse_xmlize($info); //Debug
3413 //print_object ($GLOBALS['traverse_array']); //Debug
3414 //$GLOBALS['traverse_array']=""; //Debug
3415 //Now build the BLOG record structure
3416 $dbrec = new object();
3417 $dbrec->module = backup_todb($info['BLOG']['#']['MODULE']['0']['#']);
3418 $dbrec->userid = backup_todb($info['BLOG']['#']['USERID']['0']['#']);
3419 $dbrec->courseid = backup_todb($info['BLOG']['#']['COURSEID']['0']['#']);
3420 $dbrec->groupid = backup_todb($info['BLOG']['#']['GROUPID']['0']['#']);
3421 $dbrec->moduleid = backup_todb($info['BLOG']['#']['MODULEID']['0']['#']);
3422 $dbrec->coursemoduleid = backup_todb($info['BLOG']['#']['COURSEMODULEID']['0']['#']);
3423 $dbrec->subject = backup_todb($info['BLOG']['#']['SUBJECT']['0']['#']);
3424 $dbrec->summary = backup_todb($info['BLOG']['#']['SUMMARY']['0']['#']);
3425 $dbrec->content = backup_todb($info['BLOG']['#']['CONTENT']['0']['#']);
3426 $dbrec->uniquehash = backup_todb($info['BLOG']['#']['UNIQUEHASH']['0']['#']);
3427 $dbrec->rating = backup_todb($info['BLOG']['#']['RATING']['0']['#']);
3428 $dbrec->format = backup_todb($info['BLOG']['#']['FORMAT']['0']['#']);
3429 $dbrec->attachment = backup_todb($info['BLOG']['#']['ATTACHMENT']['0']['#']);
3430 $dbrec->publishstate = backup_todb($info['BLOG']['#']['PUBLISHSTATE']['0']['#']);
3431 $dbrec->lastmodified = backup_todb($info['BLOG']['#']['LASTMODIFIED']['0']['#']);
3432 $dbrec->created = backup_todb($info['BLOG']['#']['CREATED']['0']['#']);
3433 $dbrec->usermodified = backup_todb($info['BLOG']['#']['USERMODIFIED']['0']['#']);
3435 //We have to recode the userid field
3436 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
3437 if ($user) {
3438 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
3439 $dbrec->userid = $user->new_id;
3442 //Check if the record doesn't exist in DB!
3443 $exist = get_record('post','userid', $dbrec->userid,
3444 'subject', $dbrec->subject,
3445 'created', $dbrec->created);
3446 $newblogid = 0;
3447 if (!$exist) {
3448 //Not exist. Insert
3449 $newblogid = insert_record('post',$dbrec);
3452 //Going to restore related tags. Check they are enabled and we have inserted a blog
3453 if ($CFG->usetags && $newblogid) {
3454 //Look for tags in this blog
3455 if (isset($info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'])) {
3456 $tagsarr = $info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'];
3457 //Iterate over tags
3458 $tags = array();
3459 for($i = 0; $i < sizeof($tagsarr); $i++) {
3460 $tag_info = $tagsarr[$i];
3461 ///traverse_xmlize($tag_info); //Debug
3462 ///print_object ($GLOBALS['traverse_array']); //Debug
3463 ///$GLOBALS['traverse_array']=""; //Debug
3465 $name = backup_todb($tag_info['#']['NAME']['0']['#']);
3466 $rawname = backup_todb($tag_info['#']['RAWNAME']['0']['#']);
3468 $tags[] = $rawname; //Rawname is all we need
3470 tag_set('post', $newblogid, $tags); //Add all the tags in one API call
3474 //Do some output
3475 $counter++;
3476 if ($counter % 10 == 0) {
3477 if (!defined('RESTORE_SILENTLY')) {
3478 echo ".";
3479 if ($counter % 200 == 0) {
3480 echo "<br />";
3483 backup_flush(300);
3493 return $status;
3496 //This function creates all the categories and questions
3497 //from xml
3498 function restore_create_questions($restore,$xml_file) {
3500 global $CFG, $db;
3502 $status = true;
3503 //Check it exists
3504 if (!file_exists($xml_file)) {
3505 $status = false;
3507 //Get info from xml
3508 if ($status) {
3509 //info will contain the old_id of every category
3510 //in backup_ids->info will be the real info (serialized)
3511 $info = restore_read_xml_questions($restore,$xml_file);
3513 //Now, if we have anything in info, we have to restore that
3514 //categories/questions
3515 if ($info) {
3516 if ($info !== true) {
3517 $status = $status && restore_question_categories($info, $restore);
3519 } else {
3520 $status = false;
3522 return $status;
3525 //This function creates all the scales
3526 function restore_create_scales($restore,$xml_file) {
3528 global $CFG, $USER, $db;
3530 $status = true;
3531 //Check it exists
3532 if (!file_exists($xml_file)) {
3533 $status = false;
3535 //Get info from xml
3536 if ($status) {
3537 //scales will contain the old_id of every scale
3538 //in backup_ids->info will be the real info (serialized)
3539 $scales = restore_read_xml_scales($restore,$xml_file);
3541 //Now, if we have anything in scales, we have to restore that
3542 //scales
3543 if ($scales) {
3544 if ($scales !== true) {
3545 //Iterate over each scale
3546 foreach ($scales as $scale) {
3547 //Get record from backup_ids
3548 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
3550 if ($data) {
3551 //Now get completed xmlized object
3552 $info = $data->info;
3553 //traverse_xmlize($info); //Debug
3554 //print_object ($GLOBALS['traverse_array']); //Debug
3555 //$GLOBALS['traverse_array']=""; //Debug
3557 //Now build the SCALE record structure
3558 $sca = new object();
3559 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
3560 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
3561 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
3562 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
3563 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
3564 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
3566 // Look for scale (by 'scale' both in standard (course=0) and current course
3567 // with priority to standard scales (ORDER clause)
3568 // scale is not course unique, use get_record_sql to suppress warning
3569 // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
3570 $compare_scale_clause = sql_compare_text('scale') . "=" . sql_compare_text("'" . $sca->scale . "'");
3571 // Scale doesn't exist, create it
3572 if (!$sca_db = get_record_sql("SELECT *
3573 FROM {$CFG->prefix}scale
3574 WHERE $compare_scale_clause
3575 AND courseid IN (0, $restore->course_id)
3576 ORDER BY courseid", true)) {
3578 // Try to recode the user field, defaulting to current user if not found
3579 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
3580 if ($user) {
3581 $sca->userid = $user->new_id;
3582 } else {
3583 $sca->userid = $USER->id;
3585 // If scale is standard, if user lacks perms to manage standar scales
3586 // 'downgrade' them to course scales
3587 if ($sca->courseid == 0 and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM), $sca->userid)) {
3588 $sca->courseid = $restore->course_id;
3590 //The structure is equal to the db, so insert the scale
3591 $newid = insert_record ("scale",$sca);
3593 // Scale exists, reuse it
3594 } else {
3595 $newid = $sca_db->id;
3598 if ($newid) {
3599 //We have the newid, update backup_ids
3600 backup_putid($restore->backup_unique_code,"scale", $scale->id, $newid);
3605 } else {
3606 $status = false;
3608 return $status;
3612 * Recode group ID field, and set group ID based on restore options.
3613 * @return object Group object with new_id field.
3615 function restore_group_getid($restore, $groupid) {
3616 //We have to recode the groupid field
3617 $group = backup_getid($restore->backup_unique_code, 'groups', $groupid);
3619 if ($restore->groups == RESTORE_GROUPS_NONE or $restore->groups == RESTORE_GROUPINGS_ONLY) {
3620 $group->new_id = 0;
3622 return $group;
3626 * Recode grouping ID field, and set grouping ID based on restore options.
3627 * @return object Group object with new_id field.
3629 function restore_grouping_getid($restore, $groupingid) {
3630 //We have to recode the groupid field
3631 $grouping = backup_getid($restore->backup_unique_code, 'groupings', $groupingid);
3633 if ($restore->groups != RESTORE_GROUPS_GROUPINGS and $restore->groups != RESTORE_GROUPINGS_ONLY) {
3634 $grouping->new_id = 0;
3636 return $grouping;
3639 //This function creates all the groups
3640 function restore_create_groups($restore,$xml_file) {
3642 global $CFG;
3644 //Check it exists
3645 if (!file_exists($xml_file)) {
3646 return false;
3648 //Get info from xml
3649 if (!$groups = restore_read_xml_groups($restore,$xml_file)) {
3650 //groups will contain the old_id of every group
3651 //in backup_ids->info will be the real info (serialized)
3652 return false;
3654 } else if ($groups === true) {
3655 return true;
3658 $status = true;
3660 //Iterate over each group
3661 foreach ($groups as $group) {
3662 //Get record from backup_ids
3663 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
3665 if ($data) {
3666 //Now get completed xmlized object
3667 $info = $data->info;
3668 //traverse_xmlize($info); //Debug
3669 //print_object ($GLOBALS['traverse_array']); //Debug
3670 //$GLOBALS['traverse_array']=""; //Debug
3671 //Now build the GROUP record structure
3672 $gro = new Object();
3673 $gro->courseid = $restore->course_id;
3674 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
3675 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
3676 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
3677 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
3678 } else {
3679 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
3681 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
3682 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
3683 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
3684 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
3686 //Now search if that group exists (by name and description field) in
3687 //restore->course_id course
3688 //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
3689 $description_clause = '';
3690 if (!empty($gro->description)) { /// Only for groups having a description
3691 $literal_description = "'" . $gro->description . "'";
3692 $description_clause = " AND " .
3693 sql_compare_text('description') . " = " .
3694 sql_compare_text($literal_description);
3696 if (!$gro_db = get_record_sql("SELECT *
3697 FROM {$CFG->prefix}groups
3698 WHERE courseid = $restore->course_id AND
3699 name = '{$gro->name}'" . $description_clause, true)) {
3700 //If it doesn't exist, create
3701 $newid = insert_record('groups', $gro);
3703 } else {
3704 //get current group id
3705 $newid = $gro_db->id;
3708 if ($newid) {
3709 //We have the newid, update backup_ids
3710 backup_putid($restore->backup_unique_code,"groups", $group->id, $newid);
3711 } else {
3713 $status = false;
3714 continue;
3717 //Now restore members in the groups_members, only if
3718 //users are included
3719 if ($restore->users != 2) {
3720 if (!restore_create_groups_members($newid,$info,$restore)) {
3721 $status = false;
3727 //Now, restore group_files
3728 if ($status) {
3729 $status = restore_group_files($restore);
3732 return $status;
3735 //This function restores the groups_members
3736 function restore_create_groups_members($group_id,$info,$restore) {
3738 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
3739 //OK, some groups have no members.
3740 return true;
3742 //Get the members array
3743 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
3745 $status = true;
3747 //Iterate over members
3748 for($i = 0; $i < sizeof($members); $i++) {
3749 $mem_info = $members[$i];
3750 //traverse_xmlize($mem_info); //Debug
3751 //print_object ($GLOBALS['traverse_array']); //Debug
3752 //$GLOBALS['traverse_array']=""; //Debug
3754 //Now, build the GROUPS_MEMBERS record structure
3755 $group_member = new Object();
3756 $group_member->groupid = $group_id;
3757 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
3758 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
3760 $newid = false;
3762 //We have to recode the userid field
3763 if (!$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid)) {
3764 debugging("group membership can not be restored, user id $group_member->userid not present in backup");
3765 // do not not block the restore
3766 continue;
3769 $group_member->userid = $user->new_id;
3771 //The structure is equal to the db, so insert the groups_members
3772 if (record_exists("groups_members", 'groupid', $group_member->groupid, 'userid', $group_member->userid)) {
3773 // user already member
3774 } else if (!insert_record ("groups_members", $group_member)) {
3775 $status = false;
3776 continue;
3779 //Do some output
3780 if (($i+1) % 50 == 0) {
3781 if (!defined('RESTORE_SILENTLY')) {
3782 echo ".";
3783 if (($i+1) % 1000 == 0) {
3784 echo "<br />";
3787 backup_flush(300);
3791 return $status;
3794 //This function creates all the groupings
3795 function restore_create_groupings($restore,$xml_file) {
3797 //Check it exists
3798 if (!file_exists($xml_file)) {
3799 return false;
3801 //Get info from xml
3802 if (!$groupings = restore_read_xml_groupings($restore,$xml_file)) {
3803 return false;
3805 } else if ($groupings === true) {
3806 return true;
3809 $status = true;
3811 //Iterate over each group
3812 foreach ($groupings as $grouping) {
3813 if ($data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id)) {
3814 //Now get completed xmlized object
3815 $info = $data->info;
3816 //Now build the GROUPING record structure
3817 $gro = new Object();
3818 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
3819 $gro->courseid = $restore->course_id;
3820 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
3821 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
3822 $gro->configdata = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
3823 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
3825 //Now search if that group exists (by name and description field) in
3826 if ($gro_db = get_record('groupings', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
3827 //get current group id
3828 $newid = $gro_db->id;
3830 } else {
3831 //The structure is equal to the db, so insert the grouping
3832 if (!$newid = insert_record('groupings', $gro)) {
3833 $status = false;
3834 continue;
3838 //We have the newid, update backup_ids
3839 backup_putid($restore->backup_unique_code,"groupings",
3840 $grouping->id, $newid);
3845 // now fix the defaultgroupingid in course
3846 $course = get_record('course', 'id', $restore->course_id);
3847 if ($course->defaultgroupingid) {
3848 if ($grouping = restore_grouping_getid($restore, $course->defaultgroupingid)) {
3849 set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
3850 } else {
3851 set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
3855 return $status;
3858 //This function creates all the groupingsgroups
3859 function restore_create_groupings_groups($restore,$xml_file) {
3861 //Check it exists
3862 if (!file_exists($xml_file)) {
3863 return false;
3865 //Get info from xml
3866 if (!$groupingsgroups = restore_read_xml_groupings_groups($restore,$xml_file)) {
3867 return false;
3869 } else if ($groupingsgroups === true) {
3870 return true;
3873 $status = true;
3875 //Iterate over each group
3876 foreach ($groupingsgroups as $groupinggroup) {
3877 if ($data = backup_getid($restore->backup_unique_code,"groupingsgroups",$groupinggroup->id)) {
3878 //Now get completed xmlized object
3879 $info = $data->info;
3880 //Now build the GROUPING record structure
3881 $gro_member = new Object();
3882 $gro_member->groupingid = backup_todb($info['GROUPINGGROUP']['#']['GROUPINGID']['0']['#']);
3883 $gro_member->groupid = backup_todb($info['GROUPINGGROUP']['#']['GROUPID']['0']['#']);
3884 $gro_member->timeadded = backup_todb($info['GROUPINGGROUP']['#']['TIMEADDED']['0']['#']);
3886 if (!$grouping = backup_getid($restore->backup_unique_code,"groupings",$gro_member->groupingid)) {
3887 $status = false;
3888 continue;
3891 if (!$group = backup_getid($restore->backup_unique_code,"groups",$gro_member->groupid)) {
3892 $status = false;
3893 continue;
3896 $gro_member->groupid = $group->new_id;
3897 $gro_member->groupingid = $grouping->new_id;
3898 if (!get_record('groupings_groups', 'groupid', $gro_member->groupid, 'groupingid', $gro_member->groupingid)) {
3899 if (!insert_record('groupings_groups', $gro_member)) {
3900 $status = false;
3906 return $status;
3909 //This function creates all the course events
3910 function restore_create_events($restore,$xml_file) {
3912 global $CFG, $db;
3914 $status = true;
3915 //Check it exists
3916 if (!file_exists($xml_file)) {
3917 $status = false;
3919 //Get info from xml
3920 if ($status) {
3921 //events will contain the old_id of every event
3922 //in backup_ids->info will be the real info (serialized)
3923 $events = restore_read_xml_events($restore,$xml_file);
3926 //Get admin->id for later use
3927 $admin = get_admin();
3928 $adminid = $admin->id;
3930 //Now, if we have anything in events, we have to restore that
3931 //events
3932 if ($events) {
3933 if ($events !== true) {
3934 //Iterate over each event
3935 foreach ($events as $event) {
3936 //Get record from backup_ids
3937 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
3938 //Init variables
3939 $create_event = false;
3941 if ($data) {
3942 //Now get completed xmlized object
3943 $info = $data->info;
3944 //traverse_xmlize($info); //Debug
3945 //print_object ($GLOBALS['traverse_array']); //Debug
3946 //$GLOBALS['traverse_array']=""; //Debug
3948 //if necessary, write to restorelog and adjust date/time fields
3949 if ($restore->course_startdateoffset) {
3950 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
3953 //Now build the EVENT record structure
3954 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
3955 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
3956 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
3957 $eve->courseid = $restore->course_id;
3958 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
3959 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
3960 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
3961 $eve->modulename = "";
3962 if (!empty($info['EVENT']['#']['MODULENAME'])) {
3963 $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
3965 $eve->instance = 0;
3966 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
3967 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
3968 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
3969 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
3970 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
3972 //Now search if that event exists (by name, description, timestart fields) in
3973 //restore->course_id course
3974 //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
3975 $compare_description_clause = sql_compare_text('description') . "=" . sql_compare_text("'" . $eve->description . "'");
3976 $eve_db = get_record_select("event",
3977 "courseid={$eve->courseid} AND name='{$eve->name}' AND $compare_description_clause AND timestart=$eve->timestart");
3978 //If it doesn't exist, create
3979 if (!$eve_db) {
3980 $create_event = true;
3982 //If we must create the event
3983 if ($create_event) {
3985 //We must recode the userid
3986 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
3987 if ($user) {
3988 $eve->userid = $user->new_id;
3989 } else {
3990 //Assign it to admin
3991 $eve->userid = $adminid;
3994 //We have to recode the groupid field
3995 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
3996 if ($group) {
3997 $eve->groupid = $group->new_id;
3998 } else {
3999 //Assign it to group 0
4000 $eve->groupid = 0;
4003 //The structure is equal to the db, so insert the event
4004 $newid = insert_record ("event",$eve);
4006 //We must recode the repeatid if the event has it
4007 //The repeatid now refers to the id of the original event. (see Bug#5956)
4008 if ($newid && !empty($eve->repeatid)) {
4009 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
4010 if ($repeat_rec) { //Exists, so use it...
4011 $eve->repeatid = $repeat_rec->new_id;
4012 } else { //Doesn't exists, calculate the next and save it
4013 $oldrepeatid = $eve->repeatid;
4014 $eve->repeatid = $newid;
4015 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
4017 $eve->id = $newid;
4018 // update the record to contain the correct repeatid
4019 update_record('event',$eve);
4021 } else {
4022 //get current event id
4023 $newid = $eve_db->id;
4025 if ($newid) {
4026 //We have the newid, update backup_ids
4027 backup_putid($restore->backup_unique_code,"event",
4028 $event->id, $newid);
4033 } else {
4034 $status = false;
4036 return $status;
4039 //This function decode things to make restore multi-site fully functional
4040 //It does this conversions:
4041 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
4042 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
4044 // - $@SLASH@$ --|---------------> / (slasharguments on)
4045 // |---------------> %2F (slasharguments off)
4047 // - $@FORCEDOWNLOAD@$ --|-------> ?forcedownload=1 (slasharguments on)
4048 // |-------> &amp;forcedownload=1(slasharguments off)
4049 //Note: Inter-activities linking is being implemented as a final
4050 //step in the restore execution, because we need to have it
4051 //finished to know all the oldid, newid equivaleces
4052 function restore_decode_absolute_links($content) {
4054 global $CFG,$restore;
4055 require_once($CFG->libdir.'/filelib.php');
4057 /// MDL-14072: Prevent NULLs, empties and numbers to be processed by the
4058 /// heavy interlinking. Just a few cpu cycles saved.
4059 if ($content === NULL) {
4060 return NULL;
4061 } else if ($content === '') {
4062 return '';
4063 } else if (is_numeric($content)) {
4064 return $content;
4067 //Now decode wwwroot and file.php calls
4068 $search = array ("$@FILEPHP@$");
4069 $replace = array(get_file_url($restore->course_id));
4070 $result = str_replace($search,$replace,$content);
4072 //Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799
4073 $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
4074 if ($CFG->slasharguments) {
4075 $replace = array('/', '?forcedownload=1');
4076 } else {
4077 $replace = array('%2F', '&amp;forcedownload=1');
4079 $result = str_replace($search, $replace, $result);
4081 if ($result != $content && debugging()) { //Debug
4082 if (!defined('RESTORE_SILENTLY')) {
4083 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
4085 } //Debug
4087 return $result;
4090 //This function restores the userfiles from the temp (user_files) directory to the
4091 //dataroot/users directory
4092 function restore_user_files($restore) {
4094 global $CFG;
4096 $status = true;
4098 $counter = 0;
4100 // 'users' is the old users folder, 'user' is the new one, with a new hierarchy. Detect which one is here and treat accordingly
4101 //in CFG->dataroot
4102 $dest_dir = $CFG->dataroot."/user";
4103 $status = check_dir_exists($dest_dir,true);
4105 //Now, we iterate over "user_files" records to check if that user dir must be
4106 //copied (and renamed) to the "users" dir.
4107 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
4109 //Check if directory exists
4110 $userlist = array();
4112 if (is_dir($rootdir) && ($list = list_directories ($rootdir))) {
4113 $counter = 0;
4114 foreach ($list as $dir) {
4115 // If there are directories in this folder, we are in the new user hierarchy
4116 if ($newlist = list_directories("$rootdir/$dir")) {
4117 foreach ($newlist as $olduserid) {
4118 $userlist[$olduserid] = "$rootdir/$dir/$olduserid";
4120 } else {
4121 $userlist[$dir] = "$rootdir/$dir";
4125 foreach ($userlist as $olduserid => $backup_location) {
4126 //Look for dir like username in backup_ids
4127 //If that user exists in backup_ids
4128 if ($user = backup_getid($restore->backup_unique_code,"user",$olduserid)) {
4129 //Only if user has been created now or if it existed previously, but he hasn't got an image (see bug 1123)
4130 $newuserdir = make_user_directory($user->new_id, true); // Doesn't create the folder, just returns the location
4132 // restore images if new user or image does not exist yet
4133 if (!empty($user->new) or !check_dir_exists($newuserdir)) {
4134 if (make_user_directory($user->new_id)) { // Creates the folder
4135 $status = backup_copy_file($backup_location, $newuserdir, true);
4136 $counter ++;
4138 //Do some output
4139 if ($counter % 2 == 0) {
4140 if (!defined('RESTORE_SILENTLY')) {
4141 echo ".";
4142 if ($counter % 40 == 0) {
4143 echo "<br />";
4146 backup_flush(300);
4152 //If status is ok and whe have dirs created, returns counter to inform
4153 if ($status and $counter) {
4154 return $counter;
4155 } else {
4156 return $status;
4160 //This function restores the groupfiles from the temp (group_files) directory to the
4161 //dataroot/groups directory
4162 function restore_group_files($restore) {
4164 global $CFG;
4166 $status = true;
4168 $counter = 0;
4170 //First, we check to "groups" exists and create is as necessary
4171 //in CFG->dataroot
4172 $dest_dir = $CFG->dataroot.'/groups';
4173 $status = check_dir_exists($dest_dir,true);
4175 //Now, we iterate over "group_files" records to check if that user dir must be
4176 //copied (and renamed) to the "groups" dir.
4177 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
4178 //Check if directory exists
4179 if (is_dir($rootdir)) {
4180 $list = list_directories ($rootdir);
4181 if ($list) {
4182 //Iterate
4183 $counter = 0;
4184 foreach ($list as $dir) {
4185 //Look for dir like groupid in backup_ids
4186 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
4187 "table_name","groups",
4188 "old_id",$dir);
4189 //If that group exists in backup_ids
4190 if ($data) {
4191 if (!file_exists($dest_dir."/".$data->new_id)) {
4192 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
4193 $counter ++;
4195 //Do some output
4196 if ($counter % 2 == 0) {
4197 if (!defined('RESTORE_SILENTLY')) {
4198 echo ".";
4199 if ($counter % 40 == 0) {
4200 echo "<br />";
4203 backup_flush(300);
4209 //If status is ok and whe have dirs created, returns counter to inform
4210 if ($status and $counter) {
4211 return $counter;
4212 } else {
4213 return $status;
4217 //This function restores the course files from the temp (course_files) directory to the
4218 //dataroot/course_id directory
4219 function restore_course_files($restore) {
4221 global $CFG;
4223 $status = true;
4225 $counter = 0;
4227 //First, we check to "course_id" exists and create is as necessary
4228 //in CFG->dataroot
4229 $dest_dir = $CFG->dataroot."/".$restore->course_id;
4230 $status = check_dir_exists($dest_dir,true);
4232 //Now, we iterate over "course_files" records to check if that file/dir must be
4233 //copied to the "dest_dir" dir.
4234 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
4235 //Check if directory exists
4236 if (is_dir($rootdir)) {
4237 $list = list_directories_and_files ($rootdir);
4238 if ($list) {
4239 //Iterate
4240 $counter = 0;
4241 foreach ($list as $dir) {
4242 //Copy the dir to its new location
4243 //Only if destination file/dir doesn exists
4244 if (!file_exists($dest_dir."/".$dir)) {
4245 $status = backup_copy_file($rootdir."/".$dir,
4246 $dest_dir."/".$dir,true);
4247 $counter ++;
4249 //Do some output
4250 if ($counter % 2 == 0) {
4251 if (!defined('RESTORE_SILENTLY')) {
4252 echo ".";
4253 if ($counter % 40 == 0) {
4254 echo "<br />";
4257 backup_flush(300);
4262 //If status is ok and whe have dirs created, returns counter to inform
4263 if ($status and $counter) {
4264 return $counter;
4265 } else {
4266 return $status;
4270 //This function restores the site files from the temp (site_files) directory to the
4271 //dataroot/SITEID directory
4272 function restore_site_files($restore) {
4274 global $CFG;
4276 $status = true;
4278 $counter = 0;
4280 //First, we check to "course_id" exists and create is as necessary
4281 //in CFG->dataroot
4282 $dest_dir = $CFG->dataroot."/".SITEID;
4283 $status = check_dir_exists($dest_dir,true);
4285 //Now, we iterate over "site_files" files to check if that file/dir must be
4286 //copied to the "dest_dir" dir.
4287 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/site_files";
4288 //Check if directory exists
4289 if (is_dir($rootdir)) {
4290 $list = list_directories_and_files ($rootdir);
4291 if ($list) {
4292 //Iterate
4293 $counter = 0;
4294 foreach ($list as $dir) {
4295 //Avoid copying maintenance.html. MDL-18594
4296 if ($dir == 'maintenance.html') {
4297 continue;
4299 //Copy the dir to its new location
4300 //Only if destination file/dir doesn exists
4301 if (!file_exists($dest_dir."/".$dir)) {
4302 $status = backup_copy_file($rootdir."/".$dir,
4303 $dest_dir."/".$dir,true);
4304 $counter ++;
4306 //Do some output
4307 if ($counter % 2 == 0) {
4308 if (!defined('RESTORE_SILENTLY')) {
4309 echo ".";
4310 if ($counter % 40 == 0) {
4311 echo "<br />";
4314 backup_flush(300);
4319 //If status is ok and whe have dirs created, returns counter to inform
4320 if ($status and $counter) {
4321 return $counter;
4322 } else {
4323 return $status;
4328 //This function creates all the structures for every module in backup file
4329 //Depending what has been selected.
4330 function restore_create_modules($restore,$xml_file) {
4332 global $CFG;
4333 $status = true;
4334 //Check it exists
4335 if (!file_exists($xml_file)) {
4336 $status = false;
4338 //Get info from xml
4339 if ($status) {
4340 //info will contain the id and modtype of every module
4341 //in backup_ids->info will be the real info (serialized)
4342 $info = restore_read_xml_modules($restore,$xml_file);
4344 //Now, if we have anything in info, we have to restore that mods
4345 //from backup_ids (calling every mod restore function)
4346 if ($info) {
4347 if ($info !== true) {
4348 if (!defined('RESTORE_SILENTLY')) {
4349 echo '<ul>';
4351 //Iterate over each module
4352 foreach ($info as $mod) {
4353 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
4354 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
4355 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
4356 $modrestore = $mod->modtype."_restore_mods";
4357 if (function_exists($modrestore)) { //Debug
4358 // we want to restore all mods even when one fails
4359 // incorrect code here ignored any errors during module restore in 1.6-1.8
4360 $status = $status && $modrestore($mod,$restore);
4361 } else {
4362 //Something was wrong. Function should exist.
4363 $status = false;
4367 if (!defined('RESTORE_SILENTLY')) {
4368 echo '</ul>';
4371 } else {
4372 $status = false;
4374 return $status;
4377 //This function creates all the structures for every log in backup file
4378 //Depending what has been selected.
4379 function restore_create_logs($restore,$xml_file) {
4381 global $CFG,$db;
4383 //Number of records to get in every chunk
4384 $recordset_size = 4;
4385 //Counter, points to current record
4386 $counter = 0;
4387 //To count all the recods to restore
4388 $count_logs = 0;
4390 $status = true;
4391 //Check it exists
4392 if (!file_exists($xml_file)) {
4393 $status = false;
4395 //Get info from xml
4396 if ($status) {
4397 //count_logs will contain the number of logs entries to process
4398 //in backup_ids->info will be the real info (serialized)
4399 $count_logs = restore_read_xml_logs($restore,$xml_file);
4402 //Now, if we have records in count_logs, we have to restore that logs
4403 //from backup_ids. This piece of code makes calls to:
4404 // - restore_log_course() if it's a course log
4405 // - restore_log_user() if it's a user log
4406 // - restore_log_module() if it's a module log.
4407 //And all is segmented in chunks to allow large recordsets to be restored !!
4408 if ($count_logs > 0) {
4409 while ($counter < $count_logs) {
4410 //Get a chunk of records
4411 //Take old_id twice to avoid adodb limitation
4412 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
4413 //We have logs
4414 if ($logs) {
4415 //Iterate
4416 foreach ($logs as $log) {
4417 //Get the full record from backup_ids
4418 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
4419 if ($data) {
4420 //Now get completed xmlized object
4421 $info = $data->info;
4422 //traverse_xmlize($info); //Debug
4423 //print_object ($GLOBALS['traverse_array']); //Debug
4424 //$GLOBALS['traverse_array']=""; //Debug
4425 //Now build the LOG record structure
4426 $dblog = new object();
4427 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
4428 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
4429 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
4430 $dblog->course = $restore->course_id;
4431 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
4432 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
4433 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
4434 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
4435 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
4436 //We have to recode the userid field
4437 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
4438 if ($user) {
4439 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
4440 $dblog->userid = $user->new_id;
4442 //We have to recode the cmid field (if module isn't "course" or "user")
4443 if ($dblog->module != "course" and $dblog->module != "user") {
4444 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
4445 if ($cm) {
4446 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
4447 $dblog->cmid = $cm->new_id;
4448 } else {
4449 $dblog->cmid = 0;
4452 //print_object ($dblog); //Debug
4453 //Now, we redirect to the needed function to make all the work
4454 if ($dblog->module == "course") {
4455 //It's a course log,
4456 $stat = restore_log_course($restore,$dblog);
4457 } elseif ($dblog->module == "user") {
4458 //It's a user log,
4459 $stat = restore_log_user($restore,$dblog);
4460 } else {
4461 //It's a module log,
4462 $stat = restore_log_module($restore,$dblog);
4466 //Do some output
4467 $counter++;
4468 if ($counter % 10 == 0) {
4469 if (!defined('RESTORE_SILENTLY')) {
4470 echo ".";
4471 if ($counter % 200 == 0) {
4472 echo "<br />";
4475 backup_flush(300);
4478 } else {
4479 //We never should arrive here
4480 $counter = $count_logs;
4481 $status = false;
4486 return $status;
4489 //This function inserts a course log record, calculating the URL field as necessary
4490 function restore_log_course($restore,$log) {
4492 $status = true;
4493 $toinsert = false;
4495 //echo "<hr />Before transformations<br />"; //Debug
4496 //print_object($log); //Debug
4497 //Depending of the action, we recode different things
4498 switch ($log->action) {
4499 case "view":
4500 $log->url = "view.php?id=".$log->course;
4501 $log->info = $log->course;
4502 $toinsert = true;
4503 break;
4504 case "guest":
4505 $log->url = "view.php?id=".$log->course;
4506 $toinsert = true;
4507 break;
4508 case "user report":
4509 //recode the info field (it's the user id)
4510 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4511 if ($user) {
4512 $log->info = $user->new_id;
4513 //Now, extract the mode from the url field
4514 $mode = substr(strrchr($log->url,"="),1);
4515 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
4516 $toinsert = true;
4518 break;
4519 case "add mod":
4520 //Extract the course_module from the url field
4521 $cmid = substr(strrchr($log->url,"="),1);
4522 //recode the course_module to see it it has been restored
4523 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
4524 if ($cm) {
4525 $cmid = $cm->new_id;
4526 //Extract the module name and the module id from the info field
4527 $modname = strtok($log->info," ");
4528 $modid = strtok(" ");
4529 //recode the module id to see if it has been restored
4530 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
4531 if ($mod) {
4532 $modid = $mod->new_id;
4533 //Now I have everything so reconstruct url and info
4534 $log->info = $modname." ".$modid;
4535 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
4536 $toinsert = true;
4539 break;
4540 case "update mod":
4541 //Extract the course_module from the url field
4542 $cmid = substr(strrchr($log->url,"="),1);
4543 //recode the course_module to see it it has been restored
4544 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
4545 if ($cm) {
4546 $cmid = $cm->new_id;
4547 //Extract the module name and the module id from the info field
4548 $modname = strtok($log->info," ");
4549 $modid = strtok(" ");
4550 //recode the module id to see if it has been restored
4551 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
4552 if ($mod) {
4553 $modid = $mod->new_id;
4554 //Now I have everything so reconstruct url and info
4555 $log->info = $modname." ".$modid;
4556 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
4557 $toinsert = true;
4560 break;
4561 case "delete mod":
4562 $log->url = "view.php?id=".$log->course;
4563 $toinsert = true;
4564 break;
4565 case "update":
4566 $log->url = "edit.php?id=".$log->course;
4567 $log->info = "";
4568 $toinsert = true;
4569 break;
4570 case "unenrol":
4571 //recode the info field (it's the user id)
4572 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4573 if ($user) {
4574 $log->info = $user->new_id;
4575 $log->url = "view.php?id=".$log->course;
4576 $toinsert = true;
4578 break;
4579 case "enrol":
4580 //recode the info field (it's the user id)
4581 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4582 if ($user) {
4583 $log->info = $user->new_id;
4584 $log->url = "view.php?id=".$log->course;
4585 $toinsert = true;
4587 break;
4588 case "editsection":
4589 //Extract the course_section from the url field
4590 $secid = substr(strrchr($log->url,"="),1);
4591 //recode the course_section to see if it has been restored
4592 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
4593 if ($sec) {
4594 $secid = $sec->new_id;
4595 //Now I have everything so reconstruct url and info
4596 $log->url = "editsection.php?id=".$secid;
4597 $toinsert = true;
4599 break;
4600 case "new":
4601 $log->url = "view.php?id=".$log->course;
4602 $log->info = "";
4603 $toinsert = true;
4604 break;
4605 case "recent":
4606 $log->url = "recent.php?id=".$log->course;
4607 $log->info = "";
4608 $toinsert = true;
4609 break;
4610 case "report log":
4611 $log->url = "report/log/index.php?id=".$log->course;
4612 $log->info = $log->course;
4613 $toinsert = true;
4614 break;
4615 case "report live":
4616 $log->url = "report/log/live.php?id=".$log->course;
4617 $log->info = $log->course;
4618 $toinsert = true;
4619 break;
4620 case "report outline":
4621 $log->url = "report/outline/index.php?id=".$log->course;
4622 $log->info = $log->course;
4623 $toinsert = true;
4624 break;
4625 case "report participation":
4626 $log->url = "report/participation/index.php?id=".$log->course;
4627 $log->info = $log->course;
4628 $toinsert = true;
4629 break;
4630 case "report stats":
4631 $log->url = "report/stats/index.php?id=".$log->course;
4632 $log->info = $log->course;
4633 $toinsert = true;
4634 break;
4635 default:
4636 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
4637 break;
4640 //echo "After transformations<br />"; //Debug
4641 //print_object($log); //Debug
4643 //Now if $toinsert is set, insert the record
4644 if ($toinsert) {
4645 //echo "Inserting record<br />"; //Debug
4646 $status = insert_record("log",$log);
4648 return $status;
4651 //This function inserts a user log record, calculating the URL field as necessary
4652 function restore_log_user($restore,$log) {
4654 $status = true;
4655 $toinsert = false;
4657 //echo "<hr />Before transformations<br />"; //Debug
4658 //print_object($log); //Debug
4659 //Depending of the action, we recode different things
4660 switch ($log->action) {
4661 case "view":
4662 //recode the info field (it's the user id)
4663 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4664 if ($user) {
4665 $log->info = $user->new_id;
4666 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4667 $toinsert = true;
4669 break;
4670 case "change password":
4671 //recode the info field (it's the user id)
4672 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4673 if ($user) {
4674 $log->info = $user->new_id;
4675 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4676 $toinsert = true;
4678 break;
4679 case "login":
4680 //recode the info field (it's the user id)
4681 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4682 if ($user) {
4683 $log->info = $user->new_id;
4684 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4685 $toinsert = true;
4687 break;
4688 case "logout":
4689 //recode the info field (it's the user id)
4690 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4691 if ($user) {
4692 $log->info = $user->new_id;
4693 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4694 $toinsert = true;
4696 break;
4697 case "view all":
4698 $log->url = "view.php?id=".$log->course;
4699 $log->info = "";
4700 $toinsert = true;
4701 case "update":
4702 //We split the url by ampersand char
4703 $first_part = strtok($log->url,"&");
4704 //Get data after the = char. It's the user being updated
4705 $userid = substr(strrchr($first_part,"="),1);
4706 //Recode the user
4707 $user = backup_getid($restore->backup_unique_code,"user",$userid);
4708 if ($user) {
4709 $log->info = "";
4710 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
4711 $toinsert = true;
4713 break;
4714 default:
4715 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
4716 break;
4719 //echo "After transformations<br />"; //Debug
4720 //print_object($log); //Debug
4722 //Now if $toinsert is set, insert the record
4723 if ($toinsert) {
4724 //echo "Inserting record<br />"; //Debug
4725 $status = insert_record("log",$log);
4727 return $status;
4730 //This function inserts a module log record, calculating the URL field as necessary
4731 function restore_log_module($restore,$log) {
4733 $status = true;
4734 $toinsert = false;
4736 //echo "<hr />Before transformations<br />"; //Debug
4737 //print_object($log); //Debug
4739 //Now we see if the required function in the module exists
4740 $function = $log->module."_restore_logs";
4741 if (function_exists($function)) {
4742 //Call the function
4743 $log = $function($restore,$log);
4744 //If everything is ok, mark the insert flag
4745 if ($log) {
4746 $toinsert = true;
4750 //echo "After transformations<br />"; //Debug
4751 //print_object($log); //Debug
4753 //Now if $toinsert is set, insert the record
4754 if ($toinsert) {
4755 //echo "Inserting record<br />"; //Debug
4756 $status = insert_record("log",$log);
4758 return $status;
4761 //This function adjusts the instance field into course_modules. It's executed after
4762 //modules restore. There, we KNOW the new instance id !!
4763 function restore_check_instances($restore) {
4765 global $CFG;
4767 $status = true;
4769 //We are going to iterate over each course_module saved in backup_ids
4770 $course_modules = get_records_sql("SELECT old_id,new_id
4771 FROM {$CFG->prefix}backup_ids
4772 WHERE backup_code = '$restore->backup_unique_code' AND
4773 table_name = 'course_modules'");
4774 if ($course_modules) {
4775 foreach($course_modules as $cm) {
4776 //Get full record, using backup_getids
4777 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
4778 //Now we are going to the REAL course_modules to get its type (field module)
4779 $module = get_record("course_modules","id",$cm_module->new_id);
4780 if ($module) {
4781 //We know the module type id. Get the name from modules
4782 $type = get_record("modules","id",$module->module);
4783 if ($type) {
4784 //We know the type name and the old_id. Get its new_id
4785 //from backup_ids. It's the instance !!!
4786 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
4787 if ($instance) {
4788 //We have the new instance, so update the record in course_modules
4789 $module->instance = $instance->new_id;
4790 //print_object ($module); //Debug
4791 $status = update_record("course_modules",$module);
4792 } else {
4793 $status = false;
4795 } else {
4796 $status = false;
4798 } else {
4799 $status = false;
4801 // MDL-14326 remove empty course modules instance's (credit goes to John T. Macklin from Remote Learner)
4802 $course_modules_inst_zero = get_records_sql("SELECT id, course, instance
4803 FROM {$CFG->prefix}course_modules
4804 WHERE id = '$cm_module->new_id' AND
4805 instance = '0'");
4807 if($course_modules_inst_zero){ // Clean up the invalid instances
4808 foreach($course_modules_inst_zero as $course_modules_inst){
4809 delete_records('course_modules', 'id',$course_modules_inst->id);
4814 /// Finally, calculate modinfo cache.
4815 rebuild_course_cache($restore->course_id);
4819 return $status;
4822 //=====================================================================================
4823 //== ==
4824 //== XML Functions (SAX) ==
4825 //== ==
4826 //=====================================================================================
4828 /// This is the class used to split, in first instance, the monolithic moodle.xml into
4829 /// smaller xml files allowing the MoodleParser later to process only the required info
4830 /// based in each TODO, instead of processing the whole xml for each TODO. In theory
4831 /// processing time can be reduced upto 1/20th of original time (depending of the
4832 /// number of TODOs in the original moodle.xml file)
4834 /// Anyway, note it's a general splitter parser, and only needs to be instantiated
4835 /// with the proper destination dir and the tosplit configuration. Be careful when
4836 /// using it because it doesn't support XML attributes nor real cdata out from tags.
4837 /// (both not used in the target Moodle backup files)
4839 class moodle_splitter_parser {
4840 var $level = 0; /// Level we are
4841 var $tree = array(); /// Array of levels we are
4842 var $cdata = ''; /// Raw storage for character data
4843 var $content = ''; /// Content buffer to be printed to file
4844 var $trailing= ''; /// Content of the trailing tree for each splited file
4845 var $savepath = null; /// Path to store splited files
4846 var $fhandler = null; /// Current file we are writing to
4847 var $tosplit = array(); /// Array defining the files we want to split, in this format:
4848 /// array( level/tag/level/tag => filename)
4849 var $splitwords = array(); /// Denormalised array containing the potential tags
4850 /// being a split point. To speed up check_split_point()
4851 var $maxsplitlevel = 0; /// Precalculated max level where any split happens. To speed up check_split_point()
4852 var $buffersize = 65536; /// 64KB is a good write buffer. Don't expect big benefits by increasing this.
4853 var $repectformat = false; /// With this setting enabled, the splited files will look like the original one
4854 /// with all the indentations 100% copied from original (character data outer tags).
4855 /// But this is a waste of time from our perspective, and splited xml files are completely
4856 /// functional without that, so we disable this for production, generating a more compact
4857 /// XML quicker
4859 /// PHP4 constructor
4860 function moodle_splitter_parser($savepath, $tosplit = null) {
4861 return $this->__construct($savepath, $tosplit);
4864 /// PHP5 constructor
4865 function __construct($savepath, $tosplit = null) {
4866 $this->savepath = $savepath;
4867 if (!empty($tosplit)) {
4868 $this->tosplit = $tosplit;
4869 } else { /// No tosplit list passed, process all the possible parts in one moodle.xml file
4870 $this->tosplit = array(
4871 '1/MOODLE_BACKUP/2/INFO' => 'split_info.xml',
4872 '1/MOODLE_BACKUP/2/ROLES' => 'split_roles.xml',
4873 '2/COURSE/3/HEADER' => 'split_course_header.xml',
4874 '2/COURSE/3/BLOCKS' => 'split_blocks.xml',
4875 '2/COURSE/3/SECTIONS' => 'split_sections.xml',
4876 '2/COURSE/3/FORMATDATA' => 'split_formatdata.xml',
4877 '2/COURSE/3/METACOURSE' => 'split_metacourse.xml',
4878 '2/COURSE/3/GRADEBOOK' => 'split_gradebook.xml',
4879 '2/COURSE/3/USERS' => 'split_users.xml',
4880 '2/COURSE/3/MESSAGES' => 'split_messages.xml',
4881 '2/COURSE/3/BLOGS' => 'split_blogs.xml',
4882 '2/COURSE/3/QUESTION_CATEGORIES'=> 'split_questions.xml',
4883 '2/COURSE/3/SCALES' => 'split_scales.xml',
4884 '2/COURSE/3/GROUPS' => 'split_groups.xml',
4885 '2/COURSE/3/GROUPINGS' => 'split_groupings.xml',
4886 '2/COURSE/3/GROUPINGSGROUPS' => 'split_groupingsgroups.xml',
4887 '2/COURSE/3/EVENTS' => 'split_events.xml',
4888 '2/COURSE/3/MODULES' => 'split_modules.xml',
4889 '2/COURSE/3/LOGS' => 'split_logs.xml'
4892 /// Precalculate some info used to speedup checks
4893 foreach ($this->tosplit as $key=>$value) {
4894 $this->splitwords[basename($key)] = true;
4895 if (((int) basename(dirname($key))) > $this->maxsplitlevel) {
4896 $this->maxsplitlevel = (int) basename(dirname($key));
4901 /// Given one tag being opened, check if it's one split point.
4902 /// Return false or split filename
4903 function check_split_point($tag) {
4904 /// Quick check. Level < 2 cannot be a split point
4905 if ($this->level < 2) {
4906 return false;
4908 /// Quick check. Current tag against potential splitwords
4909 if (!isset($this->splitwords[$tag])) {
4910 return false;
4912 /// Prev test passed, take a look to 2-level tosplit
4913 $keytocheck = ($this->level - 1) . '/' . $this->tree[$this->level - 1] . '/' . $this->level . '/' . $this->tree[$this->level];
4914 if (!isset($this->tosplit[$keytocheck])) {
4915 return false;
4917 /// Prev test passed, we are in a split point, return new filename
4918 return $this->tosplit[$keytocheck];
4921 /// To append data (xml-escaped) to contents buffer
4922 function character_data($parser, $data) {
4924 ///$this->content .= preg_replace($this->entity_find, $this->entity_replace, $data); ///40% slower
4925 ///$this->content .= str_replace($this->entity_find, $this->entity_replace, $data); ///25% slower
4926 ///$this->content .= htmlspecialchars($data); ///the best
4927 /// Instead of htmlspecialchars() each chunk of character data, we are going to
4928 /// concat it without transformation and will apply the htmlspecialchars() when
4929 /// that character data is, efectively, going to be added to contents buffer. This
4930 /// makes the number of transformations to be reduced (speedup) and avoid potential
4931 /// problems with transformations being applied "in the middle" of multibyte chars.
4932 $this->cdata .= $data;
4935 /// To detect start of tags, keeping level, tree and fhandle updated.
4936 /// Also handles creation of split files
4937 function start_tag($parser, $tag, $attrs) {
4939 /// Update things before processing
4940 $this->level++;
4941 $this->tree[$this->level] = $tag;
4943 /// Check if we need to start a new split file,
4944 /// Speedup: we only do that if we haven't a fhandler and if level <= $maxsplitlevel
4945 if ($this->level <= $this->maxsplitlevel && !$this->fhandler && $newfilename = $this->check_split_point($tag)) {
4946 /// Open new file handler, init everything
4947 $this->fhandler = fopen($this->savepath . '/' . $newfilename, 'w');
4948 $this->content = '';
4949 $this->cdata = '';
4950 $this->trailing = '';
4951 /// Build the original leading tree (and calculate the original trailing one)
4952 for ($l = 1; $l < $this->level; $l++) {
4953 $this->content .= "<{$this->tree[$l]}>\n";
4954 $this->trailing = "\n</{$this->tree[$l]}>" . $this->trailing;
4957 /// Perform xml-entities transformation and add to contents buffer together with opening tag.
4958 /// Speedup. We lose nice formatting of the split XML but avoid 50% of transformations and XML is 100% equivalent
4959 $this->content .= ($this->repectformat ? htmlspecialchars($this->cdata) : '') . "<$tag>";
4960 $this->cdata = '';
4963 /// To detect end of tags, keeping level, tree and fhandle updated, writting contents buffer to split file.
4964 /// Also handles closing of split files
4965 function end_tag($parser, $tag) {
4967 /// Perform xml-entities transformation and add to contents buffer together with closing tag, repecting (or no) format
4968 $this->content .= ($this->repectformat ? htmlspecialchars($this->cdata) : htmlspecialchars(trim($this->cdata))) . "</$tag>";
4969 $this->cdata = '';
4971 /// Check if we need to close current split file
4972 /// Speedup: we only do that if we have a fhandler and if level <= $maxsplitlevel
4973 if ($this->level <= $this->maxsplitlevel && $this->fhandler && $newfilename = $this->check_split_point($tag)) {
4974 /// Write pending contents buffer before closing. It's a must
4975 fwrite($this->fhandler, $this->content);
4976 $this->content = "";
4977 /// Write the original trailing tree for fhandler
4978 fwrite($this->fhandler, $this->trailing);
4979 fclose($this->fhandler);
4980 $this->fhandler = null;
4981 } else {
4982 /// Normal write of contents (use one buffer to improve speed)
4983 if ($this->fhandler && strlen($this->content) > $this->buffersize) {
4984 fwrite($this->fhandler, $this->content);
4985 $this->content = "";
4989 /// Update things after processing
4990 $this->tree[$this->level] = "";
4991 $this->level--;
4996 /// This function executes the moodle_splitter_parser, causing the monolithic moodle.xml
4997 /// file to be splitted in n smaller files for better treatament by the MoodleParser in restore_read_xml()
4998 function restore_split_xml ($xml_file, $preferences) {
5000 $status = true;
5002 $xml_parser = xml_parser_create('UTF-8');
5003 $split_parser = new moodle_splitter_parser(dirname($xml_file));
5004 xml_set_object($xml_parser,$split_parser);
5005 xml_set_element_handler($xml_parser, 'start_tag', 'end_tag');
5006 xml_set_character_data_handler($xml_parser, 'character_data');
5008 $doteach = filesize($xml_file) / 20;
5009 $fromdot = 0;
5011 $fp = fopen($xml_file,"r")
5012 or $status = false;
5013 if ($status) {
5014 $lasttime = time();
5015 while ($data = fread($fp, 8192)) {
5016 if (!defined('RESTORE_SILENTLY')) {
5017 $fromdot += 8192;
5018 if ($fromdot > $doteach) {
5019 echo ".";
5020 backup_flush(300);
5021 $fromdot = 0;
5023 if ((time() - $lasttime) > 10) {
5024 $lasttime = time();
5025 backup_flush(300);
5028 xml_parse($xml_parser, $data, feof($fp))
5029 or die(sprintf("XML error: %s at line %d",
5030 xml_error_string(xml_get_error_code($xml_parser)),
5031 xml_get_current_line_number($xml_parser)));
5033 fclose($fp);
5035 xml_parser_free($xml_parser);
5036 return $status;
5039 //This is the class used to do all the xml parse
5040 class MoodleParser {
5042 var $level = 0; //Level we are
5043 var $counter = 0; //Counter
5044 var $tree = array(); //Array of levels we are
5045 var $content = ""; //Content under current level
5046 var $todo = ""; //What we hav to do when parsing
5047 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
5048 var $temp = ""; //Temp storage.
5049 var $preferences = ""; //Preferences about what to load !!
5050 var $finished = false; //Flag to say xml_parse to stop
5052 //This function is used to get the current contents property value
5053 //They are trimed (and converted from utf8 if needed)
5054 function getContents() {
5055 return trim($this->content);
5058 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
5059 function startElementInfo($parser, $tagName, $attrs) {
5060 //Refresh properties
5061 $this->level++;
5062 $this->tree[$this->level] = $tagName;
5064 //Output something to avoid browser timeouts...
5065 //backup_flush();
5067 //Check if we are into INFO zone
5068 //if ($this->tree[2] == "INFO") //Debug
5069 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5072 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
5073 function startElementRoles($parser, $tagName, $attrs) {
5074 //Refresh properties
5075 $this->level++;
5076 $this->tree[$this->level] = $tagName;
5078 //Output something to avoid browser timeouts...
5079 //backup_flush();
5081 //Check if we are into INFO zone
5082 //if ($this->tree[2] == "INFO") //Debug
5083 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5087 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
5088 function startElementCourseHeader($parser, $tagName, $attrs) {
5089 //Refresh properties
5090 $this->level++;
5091 $this->tree[$this->level] = $tagName;
5093 //Output something to avoid browser timeouts...
5094 //backup_flush();
5096 //Check if we are into COURSE_HEADER zone
5097 //if ($this->tree[3] == "HEADER") //Debug
5098 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5101 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
5102 function startElementBlocks($parser, $tagName, $attrs) {
5103 //Refresh properties
5104 $this->level++;
5105 $this->tree[$this->level] = $tagName;
5107 //Output something to avoid browser timeouts...
5108 //backup_flush();
5110 //Check if we are into BLOCKS zone
5111 //if ($this->tree[3] == "BLOCKS") //Debug
5112 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5114 //If we are under a BLOCK tag under a BLOCKS zone, accumule it
5115 if (isset($this->tree[4]) and isset($this->tree[3])) { //
5116 if ($this->tree[4] == "BLOCK" and $this->tree[3] == "BLOCKS") {
5117 if (!isset($this->temp)) {
5118 $this->temp = "";
5120 $this->temp .= "<".$tagName.">";
5125 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
5126 function startElementSections($parser, $tagName, $attrs) {
5127 //Refresh properties
5128 $this->level++;
5129 $this->tree[$this->level] = $tagName;
5131 //Output something to avoid browser timeouts...
5132 //backup_flush();
5134 //Check if we are into SECTIONS zone
5135 //if ($this->tree[3] == "SECTIONS") //Debug
5136 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5139 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
5140 function startElementFormatData($parser, $tagName, $attrs) {
5141 //Refresh properties
5142 $this->level++;
5143 $this->tree[$this->level] = $tagName;
5145 //Output something to avoid browser timeouts...
5146 //backup_flush();
5148 //Accumulate all the data inside this tag
5149 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
5150 if (!isset($this->temp)) {
5151 $this->temp = '';
5153 $this->temp .= "<".$tagName.">";
5156 //Check if we are into FORMATDATA zone
5157 //if ($this->tree[3] == "FORMATDATA") //Debug
5158 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5161 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
5162 function startElementMetacourse($parser, $tagName, $attrs) {
5164 //Refresh properties
5165 $this->level++;
5166 $this->tree[$this->level] = $tagName;
5168 //Output something to avoid browser timeouts...
5169 //backup_flush();
5171 //Check if we are into METACOURSE zone
5172 //if ($this->tree[3] == "METACOURSE") //Debug
5173 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5176 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5177 function startElementGradebook($parser, $tagName, $attrs) {
5179 //Refresh properties
5180 $this->level++;
5181 $this->tree[$this->level] = $tagName;
5183 //Output something to avoid browser timeouts...
5184 //backup_flush();
5186 //Check if we are into GRADEBOOK zone
5187 //if ($this->tree[3] == "GRADEBOOK") //Debug
5188 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5190 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
5191 if (isset($this->tree[5]) and isset($this->tree[3])) {
5192 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")) {
5194 if (!isset($this->temp)) {
5195 $this->temp = "";
5197 $this->temp .= "<".$tagName.">";
5202 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5203 function startElementOldGradebook($parser, $tagName, $attrs) {
5205 //Refresh properties
5206 $this->level++;
5207 $this->tree[$this->level] = $tagName;
5209 //Output something to avoid browser timeouts...
5210 //backup_flush();
5212 //Check if we are into GRADEBOOK zone
5213 //if ($this->tree[3] == "GRADEBOOK") //Debug
5214 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5216 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
5217 if (isset($this->tree[5]) and isset($this->tree[3])) {
5218 if (($this->tree[5] == "GRADE_PREFERENCE" || $this->tree[5] == "GRADE_LETTER" || $this->tree[5] == "GRADE_CATEGORY" ) && ($this->tree[3] == "GRADEBOOK")) {
5219 if (!isset($this->temp)) {
5220 $this->temp = "";
5222 $this->temp .= "<".$tagName.">";
5228 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
5229 function startElementUsers($parser, $tagName, $attrs) {
5230 //Refresh properties
5231 $this->level++;
5232 $this->tree[$this->level] = $tagName;
5234 //Check if we are into USERS zone
5235 //if ($this->tree[3] == "USERS") //Debug
5236 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5239 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
5240 function startElementMessages($parser, $tagName, $attrs) {
5241 //Refresh properties
5242 $this->level++;
5243 $this->tree[$this->level] = $tagName;
5245 //Output something to avoid browser timeouts...
5246 //backup_flush();
5248 //Check if we are into MESSAGES zone
5249 //if ($this->tree[3] == "MESSAGES") //Debug
5250 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5252 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
5253 if (isset($this->tree[4]) and isset($this->tree[3])) {
5254 if (($this->tree[4] == "MESSAGE" || (isset($this->tree[5]) && $this->tree[5] == "CONTACT" )) and ($this->tree[3] == "MESSAGES")) {
5255 if (!isset($this->temp)) {
5256 $this->temp = "";
5258 $this->temp .= "<".$tagName.">";
5263 //This is the startTag handler we use where we are reading the blogs zone (todo="BLOGS")
5264 function startElementBlogs($parser, $tagName, $attrs) {
5265 //Refresh properties
5266 $this->level++;
5267 $this->tree[$this->level] = $tagName;
5269 //Output something to avoid browser timeouts...
5270 //backup_flush();
5272 //Check if we are into BLOGS zone
5273 //if ($this->tree[3] == "BLOGS") //Debug
5274 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5276 //If we are under a BLOG tag under a BLOGS zone, accumule it
5277 if (isset($this->tree[4]) and isset($this->tree[3])) {
5278 if ($this->tree[4] == "BLOG" and $this->tree[3] == "BLOGS") {
5279 if (!isset($this->temp)) {
5280 $this->temp = "";
5282 $this->temp .= "<".$tagName.">";
5287 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
5288 function startElementQuestions($parser, $tagName, $attrs) {
5289 //Refresh properties
5290 $this->level++;
5291 $this->tree[$this->level] = $tagName;
5293 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
5294 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
5295 //} //Debug
5297 //Output something to avoid browser timeouts...
5298 //backup_flush();
5300 //Check if we are into QUESTION_CATEGORIES zone
5301 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
5302 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5304 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
5305 if (isset($this->tree[4]) and isset($this->tree[3])) {
5306 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
5307 if (!isset($this->temp)) {
5308 $this->temp = "";
5310 $this->temp .= "<".$tagName.">";
5315 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
5316 function startElementScales($parser, $tagName, $attrs) {
5317 //Refresh properties
5318 $this->level++;
5319 $this->tree[$this->level] = $tagName;
5321 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
5322 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
5323 //} //Debug
5325 //Output something to avoid browser timeouts...
5326 //backup_flush();
5328 //Check if we are into SCALES zone
5329 //if ($this->tree[3] == "SCALES") //Debug
5330 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5332 //If we are under a SCALE tag under a SCALES zone, accumule it
5333 if (isset($this->tree[4]) and isset($this->tree[3])) {
5334 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
5335 if (!isset($this->temp)) {
5336 $this->temp = "";
5338 $this->temp .= "<".$tagName.">";
5343 function startElementGroups($parser, $tagName, $attrs) {
5344 //Refresh properties
5345 $this->level++;
5346 $this->tree[$this->level] = $tagName;
5348 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
5349 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
5350 //} //Debug
5352 //Output something to avoid browser timeouts...
5353 //backup_flush();
5355 //Check if we are into GROUPS zone
5356 //if ($this->tree[3] == "GROUPS") //Debug
5357 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5359 //If we are under a GROUP tag under a GROUPS zone, accumule it
5360 if (isset($this->tree[4]) and isset($this->tree[3])) {
5361 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
5362 if (!isset($this->temp)) {
5363 $this->temp = "";
5365 $this->temp .= "<".$tagName.">";
5370 function startElementGroupings($parser, $tagName, $attrs) {
5371 //Refresh properties
5372 $this->level++;
5373 $this->tree[$this->level] = $tagName;
5375 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
5376 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
5377 //} //Debug
5379 //Output something to avoid browser timeouts...
5380 //backup_flush();
5382 //Check if we are into GROUPINGS zone
5383 //if ($this->tree[3] == "GROUPINGS") //Debug
5384 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5386 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
5387 if (isset($this->tree[4]) and isset($this->tree[3])) {
5388 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
5389 if (!isset($this->temp)) {
5390 $this->temp = "";
5392 $this->temp .= "<".$tagName.">";
5397 function startElementGroupingsGroups($parser, $tagName, $attrs) {
5398 //Refresh properties
5399 $this->level++;
5400 $this->tree[$this->level] = $tagName;
5402 //if ($tagName == "GROUPINGGROUP" && $this->tree[3] == "GROUPINGSGROUPS") { //Debug
5403 // echo "<P>GROUPINGSGROUP: ".strftime ("%X",time()),"-"; //Debug
5404 //} //Debug
5406 //Output something to avoid browser timeouts...
5407 backup_flush();
5409 //Check if we are into GROUPINGSGROUPS zone
5410 //if ($this->tree[3] == "GROUPINGSGROUPS") //Debug
5411 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5413 //If we are under a GROUPINGGROUP tag under a GROUPINGSGROUPS zone, accumule it
5414 if (isset($this->tree[4]) and isset($this->tree[3])) {
5415 if (($this->tree[4] == "GROUPINGGROUP") and ($this->tree[3] == "GROUPINGSGROUPS")) {
5416 if (!isset($this->temp)) {
5417 $this->temp = "";
5419 $this->temp .= "<".$tagName.">";
5424 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
5425 function startElementEvents($parser, $tagName, $attrs) {
5426 //Refresh properties
5427 $this->level++;
5428 $this->tree[$this->level] = $tagName;
5430 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
5431 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
5432 //} //Debug
5434 //Output something to avoid browser timeouts...
5435 //backup_flush();
5437 //Check if we are into EVENTS zone
5438 //if ($this->tree[3] == "EVENTS") //Debug
5439 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5441 //If we are under a EVENT tag under a EVENTS zone, accumule it
5442 if (isset($this->tree[4]) and isset($this->tree[3])) {
5443 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
5444 if (!isset($this->temp)) {
5445 $this->temp = "";
5447 $this->temp .= "<".$tagName.">";
5452 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
5453 function startElementModules($parser, $tagName, $attrs) {
5454 //Refresh properties
5455 $this->level++;
5456 $this->tree[$this->level] = $tagName;
5458 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
5459 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
5460 //} //Debug
5462 //Output something to avoid browser timeouts...
5463 //backup_flush();
5465 //Check if we are into MODULES zone
5466 //if ($this->tree[3] == "MODULES") //Debug
5467 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5469 //If we are under a MOD tag under a MODULES zone, accumule it
5470 if (isset($this->tree[4]) and isset($this->tree[3])) {
5471 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
5472 if (!isset($this->temp)) {
5473 $this->temp = "";
5475 $this->temp .= "<".$tagName.">";
5480 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
5481 function startElementLogs($parser, $tagName, $attrs) {
5482 //Refresh properties
5483 $this->level++;
5484 $this->tree[$this->level] = $tagName;
5486 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
5487 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
5488 //} //Debug
5490 //Output something to avoid browser timeouts...
5491 //backup_flush();
5493 //Check if we are into LOGS zone
5494 //if ($this->tree[3] == "LOGS") //Debug
5495 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5497 //If we are under a LOG tag under a LOGS zone, accumule it
5498 if (isset($this->tree[4]) and isset($this->tree[3])) {
5499 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
5500 if (!isset($this->temp)) {
5501 $this->temp = "";
5503 $this->temp .= "<".$tagName.">";
5508 //This is the startTag default handler we use when todo is undefined
5509 function startElement($parser, $tagName, $attrs) {
5510 $this->level++;
5511 $this->tree[$this->level] = $tagName;
5513 //Output something to avoid browser timeouts...
5514 //backup_flush();
5516 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
5519 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
5520 function endElementInfo($parser, $tagName) {
5521 //Check if we are into INFO zone
5522 if ($this->tree[2] == "INFO") {
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 == 3) {
5528 switch ($tagName) {
5529 case "NAME":
5530 $this->info->backup_name = $this->getContents();
5531 break;
5532 case "MOODLE_VERSION":
5533 $this->info->backup_moodle_version = $this->getContents();
5534 break;
5535 case "MOODLE_RELEASE":
5536 $this->info->backup_moodle_release = $this->getContents();
5537 break;
5538 case "BACKUP_VERSION":
5539 $this->info->backup_backup_version = $this->getContents();
5540 break;
5541 case "BACKUP_RELEASE":
5542 $this->info->backup_backup_release = $this->getContents();
5543 break;
5544 case "DATE":
5545 $this->info->backup_date = $this->getContents();
5546 break;
5547 case "ORIGINAL_WWWROOT":
5548 $this->info->original_wwwroot = $this->getContents();
5549 break;
5550 case "ORIGINAL_SITE_IDENTIFIER_HASH":
5551 $this->info->original_siteidentifier = $this->getContents();
5552 break;
5553 case "MNET_REMOTEUSERS":
5554 $this->info->mnet_remoteusers = $this->getContents();
5555 break;
5558 if ($this->tree[3] == "DETAILS") {
5559 if ($this->level == 4) {
5560 switch ($tagName) {
5561 case "METACOURSE":
5562 $this->info->backup_metacourse = $this->getContents();
5563 break;
5564 case "USERS":
5565 $this->info->backup_users = $this->getContents();
5566 break;
5567 case "LOGS":
5568 $this->info->backup_logs = $this->getContents();
5569 break;
5570 case "USERFILES":
5571 $this->info->backup_user_files = $this->getContents();
5572 break;
5573 case "COURSEFILES":
5574 $this->info->backup_course_files = $this->getContents();
5575 break;
5576 case "SITEFILES":
5577 $this->info->backup_site_files = $this->getContents();
5578 break;
5579 case "GRADEBOOKHISTORIES":
5580 $this->info->gradebook_histories = $this->getContents();
5581 break;
5582 case "MESSAGES":
5583 $this->info->backup_messages = $this->getContents();
5584 break;
5585 case "BLOGS":
5586 $this->info->backup_blogs = $this->getContents();
5587 break;
5588 case 'BLOCKFORMAT':
5589 $this->info->backup_block_format = $this->getContents();
5590 break;
5593 if ($this->level == 5) {
5594 switch ($tagName) {
5595 case "NAME":
5596 $this->info->tempName = $this->getContents();
5597 break;
5598 case "INCLUDED":
5599 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
5600 break;
5601 case "USERINFO":
5602 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
5603 break;
5606 if ($this->level == 7) {
5607 switch ($tagName) {
5608 case "ID":
5609 $this->info->tempId = $this->getContents();
5610 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
5611 break;
5612 case "NAME":
5613 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
5614 break;
5615 case "INCLUDED":
5616 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
5617 break;
5618 case "USERINFO":
5619 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
5620 break;
5626 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
5627 //Speed up a lot (avoid parse all)
5628 if ($tagName == "INFO") {
5629 $this->finished = true;
5632 //Clear things
5633 $this->tree[$this->level] = "";
5634 $this->level--;
5635 $this->content = "";
5639 function endElementRoles($parser, $tagName) {
5640 //Check if we are into ROLES zone
5641 if ($this->tree[2] == "ROLES") {
5643 if ($this->tree[3] == "ROLE") {
5644 if ($this->level == 4) {
5645 switch ($tagName) {
5646 case "ID": // this is the old id
5647 $this->info->tempid = $this->getContents();
5648 $this->info->roles[$this->info->tempid]->id = $this->info->tempid;
5649 break;
5650 case "NAME":
5651 $this->info->roles[$this->info->tempid]->name = $this->getContents();;
5652 break;
5653 case "SHORTNAME":
5654 $this->info->roles[$this->info->tempid]->shortname = $this->getContents();;
5655 break;
5656 case "NAMEINCOURSE": // custom name of the role in course
5657 $this->info->roles[$this->info->tempid]->nameincourse = $this->getContents();;
5658 break;
5661 if ($this->level == 6) {
5662 switch ($tagName) {
5663 case "NAME":
5664 $this->info->tempcapname = $this->getContents();
5665 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
5666 break;
5667 case "PERMISSION":
5668 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
5669 break;
5670 case "TIMEMODIFIED":
5671 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
5672 break;
5673 case "MODIFIERID":
5674 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
5675 break;
5681 //Stop parsing if todo = ROLES and tagName = ROLES (en of the tag, of course)
5682 //Speed up a lot (avoid parse all)
5683 if ($tagName == "ROLES") {
5684 $this->finished = true;
5687 //Clear things
5688 $this->tree[$this->level] = "";
5689 $this->level--;
5690 $this->content = "";
5694 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
5695 function endElementCourseHeader($parser, $tagName) {
5696 //Check if we are into COURSE_HEADER zone
5697 if ($this->tree[3] == "HEADER") {
5698 //if (trim($this->content)) //Debug
5699 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5700 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5701 //Dependig of different combinations, do different things
5702 if ($this->level == 4) {
5703 switch ($tagName) {
5704 case "ID":
5705 $this->info->course_id = $this->getContents();
5706 break;
5707 case "PASSWORD":
5708 $this->info->course_password = $this->getContents();
5709 break;
5710 case "FULLNAME":
5711 $this->info->course_fullname = $this->getContents();
5712 break;
5713 case "SHORTNAME":
5714 $this->info->course_shortname = $this->getContents();
5715 break;
5716 case "IDNUMBER":
5717 $this->info->course_idnumber = $this->getContents();
5718 break;
5719 case "SUMMARY":
5720 $this->info->course_summary = $this->getContents();
5721 break;
5722 case "FORMAT":
5723 $this->info->course_format = $this->getContents();
5724 break;
5725 case "SHOWGRADES":
5726 $this->info->course_showgrades = $this->getContents();
5727 break;
5728 case "BLOCKINFO":
5729 $this->info->blockinfo = $this->getContents();
5730 break;
5731 case "NEWSITEMS":
5732 $this->info->course_newsitems = $this->getContents();
5733 break;
5734 case "TEACHER":
5735 $this->info->course_teacher = $this->getContents();
5736 break;
5737 case "TEACHERS":
5738 $this->info->course_teachers = $this->getContents();
5739 break;
5740 case "STUDENT":
5741 $this->info->course_student = $this->getContents();
5742 break;
5743 case "STUDENTS":
5744 $this->info->course_students = $this->getContents();
5745 break;
5746 case "GUEST":
5747 $this->info->course_guest = $this->getContents();
5748 break;
5749 case "STARTDATE":
5750 $this->info->course_startdate = $this->getContents();
5751 break;
5752 case "NUMSECTIONS":
5753 $this->info->course_numsections = $this->getContents();
5754 break;
5755 //case "SHOWRECENT": INFO: This is out in 1.3
5756 // $this->info->course_showrecent = $this->getContents();
5757 // break;
5758 case "MAXBYTES":
5759 $this->info->course_maxbytes = $this->getContents();
5760 break;
5761 case "SHOWREPORTS":
5762 $this->info->course_showreports = $this->getContents();
5763 break;
5764 case "GROUPMODE":
5765 $this->info->course_groupmode = $this->getContents();
5766 break;
5767 case "GROUPMODEFORCE":
5768 $this->info->course_groupmodeforce = $this->getContents();
5769 break;
5770 case "DEFAULTGROUPINGID":
5771 $this->info->course_defaultgroupingid = $this->getContents();
5772 break;
5773 case "LANG":
5774 $this->info->course_lang = $this->getContents();
5775 break;
5776 case "THEME":
5777 $this->info->course_theme = $this->getContents();
5778 break;
5779 case "COST":
5780 $this->info->course_cost = $this->getContents();
5781 break;
5782 case "CURRENCY":
5783 $this->info->course_currency = $this->getContents();
5784 break;
5785 case "MARKER":
5786 $this->info->course_marker = $this->getContents();
5787 break;
5788 case "VISIBLE":
5789 $this->info->course_visible = $this->getContents();
5790 break;
5791 case "HIDDENSECTIONS":
5792 $this->info->course_hiddensections = $this->getContents();
5793 break;
5794 case "TIMECREATED":
5795 $this->info->course_timecreated = $this->getContents();
5796 break;
5797 case "TIMEMODIFIED":
5798 $this->info->course_timemodified = $this->getContents();
5799 break;
5800 case "METACOURSE":
5801 $this->info->course_metacourse = $this->getContents();
5802 break;
5803 case "EXPIRENOTIFY":
5804 $this->info->course_expirynotify = $this->getContents();
5805 break;
5806 case "NOTIFYSTUDENTS":
5807 $this->info->course_notifystudents = $this->getContents();
5808 break;
5809 case "EXPIRYTHRESHOLD":
5810 $this->info->course_expirythreshold = $this->getContents();
5811 break;
5812 case "ENROLLABLE":
5813 $this->info->course_enrollable = $this->getContents();
5814 break;
5815 case "ENROLSTARTDATE":
5816 $this->info->course_enrolstartdate = $this->getContents();
5817 break;
5818 case "ENROLENDDATE":
5819 $this->info->course_enrolenddate = $this->getContents();
5820 break;
5821 case "ENROLPERIOD":
5822 $this->info->course_enrolperiod = $this->getContents();
5823 break;
5826 if ($this->tree[4] == "CATEGORY") {
5827 if ($this->level == 5) {
5828 switch ($tagName) {
5829 case "ID":
5830 $this->info->category->id = $this->getContents();
5831 break;
5832 case "NAME":
5833 $this->info->category->name = $this->getContents();
5834 break;
5839 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
5840 if ($this->level == 6) {
5841 switch ($tagName) {
5842 case "NAME":
5843 $this->info->tempname = $this->getContents();
5844 break;
5845 case "SHORTNAME":
5846 $this->info->tempshortname = $this->getContents();
5847 break;
5848 case "ID":
5849 $this->info->tempid = $this->getContents();
5850 break;
5854 if ($this->level == 8) {
5855 switch ($tagName) {
5856 case "USERID":
5857 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5858 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5859 $this->info->tempuser = $this->getContents();
5860 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5861 break;
5862 case "HIDDEN":
5863 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5864 break;
5865 case "TIMESTART":
5866 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5867 break;
5868 case "TIMEEND":
5869 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5870 break;
5871 case "TIMEMODIFIED":
5872 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5873 break;
5874 case "MODIFIERID":
5875 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5876 break;
5877 case "ENROL":
5878 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5879 break;
5880 case "SORTORDER":
5881 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5882 break;
5886 } /// ends role_assignments
5888 if ($this->tree[4] == "ROLES_OVERRIDES") {
5889 if ($this->level == 6) {
5890 switch ($tagName) {
5891 case "NAME":
5892 $this->info->tempname = $this->getContents();
5893 break;
5894 case "SHORTNAME":
5895 $this->info->tempshortname = $this->getContents();
5896 break;
5897 case "ID":
5898 $this->info->tempid = $this->getContents();
5899 break;
5903 if ($this->level == 8) {
5904 switch ($tagName) {
5905 case "NAME":
5906 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5907 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5908 $this->info->tempname = $this->getContents(); // change to name of capability
5909 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5910 break;
5911 case "PERMISSION":
5912 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5913 break;
5914 case "TIMEMODIFIED":
5915 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5916 break;
5917 case "MODIFIERID":
5918 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5919 break;
5922 } /// ends role_overrides
5925 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
5926 //Speed up a lot (avoid parse all)
5927 if ($tagName == "HEADER") {
5928 $this->finished = true;
5931 //Clear things
5932 $this->tree[$this->level] = "";
5933 $this->level--;
5934 $this->content = "";
5938 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
5939 function endElementBlocks($parser, $tagName) {
5940 //Check if we are into BLOCKS zone
5941 if ($this->tree[3] == 'BLOCKS') {
5942 //if (trim($this->content)) //Debug
5943 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5944 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5946 // Collect everything into $this->temp
5947 if (!isset($this->temp)) {
5948 $this->temp = "";
5950 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5952 //Dependig of different combinations, do different things
5953 if ($this->level == 4) {
5954 switch ($tagName) {
5955 case 'BLOCK':
5956 //We've finalized a block, get it
5957 $this->info->instances[] = $this->info->tempinstance;
5958 unset($this->info->tempinstance);
5960 //Also, xmlize INSTANCEDATA and save to db
5961 //Prepend XML standard header to info gathered
5962 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5963 //Call to xmlize for this portion of xml data (one BLOCK)
5964 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5965 $data = xmlize($xml_data,0);
5966 //echo strftime ("%X",time())."<p>"; //Debug
5967 //traverse_xmlize($data); //Debug
5968 //print_object ($GLOBALS['traverse_array']); //Debug
5969 //$GLOBALS['traverse_array']=""; //Debug
5970 //Check for instancedata, is exists, then save to DB
5971 if (isset($data['BLOCK']['#']['INSTANCEDATA']['0']['#'])) {
5972 //Get old id
5973 $oldid = $data['BLOCK']['#']['ID']['0']['#'];
5974 //Get instancedata
5976 if ($data = $data['BLOCK']['#']['INSTANCEDATA']['0']['#']) {
5977 //Restore code calls this multiple times - so might already have the newid
5978 if ($newid = backup_getid($this->preferences->backup_unique_code,'block_instance',$oldid)) {
5979 $newid = $newid->new_id;
5980 } else {
5981 $newid = null;
5983 //Save to DB, we will use it later
5984 $status = backup_putid($this->preferences->backup_unique_code,'block_instance',$oldid,$newid,$data);
5987 //Reset temp
5988 unset($this->temp);
5990 break;
5991 default:
5992 die($tagName);
5995 if ($this->level == 5) {
5996 switch ($tagName) {
5997 case 'ID':
5998 $this->info->tempinstance->id = $this->getContents();
5999 case 'NAME':
6000 $this->info->tempinstance->name = $this->getContents();
6001 break;
6002 case 'PAGEID':
6003 $this->info->tempinstance->pageid = $this->getContents();
6004 break;
6005 case 'PAGETYPE':
6006 $this->info->tempinstance->pagetype = $this->getContents();
6007 break;
6008 case 'POSITION':
6009 $this->info->tempinstance->position = $this->getContents();
6010 break;
6011 case 'WEIGHT':
6012 $this->info->tempinstance->weight = $this->getContents();
6013 break;
6014 case 'VISIBLE':
6015 $this->info->tempinstance->visible = $this->getContents();
6016 break;
6017 case 'CONFIGDATA':
6018 $this->info->tempinstance->configdata = $this->getContents();
6019 break;
6020 default:
6021 break;
6025 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
6026 if ($this->level == 7) {
6027 switch ($tagName) {
6028 case "NAME":
6029 $this->info->tempname = $this->getContents();
6030 break;
6031 case "SHORTNAME":
6032 $this->info->tempshortname = $this->getContents();
6033 break;
6034 case "ID":
6035 $this->info->tempid = $this->getContents(); // temp roleid
6036 break;
6040 if ($this->level == 9) {
6042 switch ($tagName) {
6043 case "USERID":
6044 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
6046 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
6048 $this->info->tempuser = $this->getContents();
6050 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
6051 break;
6052 case "HIDDEN":
6053 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
6054 break;
6055 case "TIMESTART":
6056 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
6057 break;
6058 case "TIMEEND":
6059 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
6060 break;
6061 case "TIMEMODIFIED":
6062 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
6063 break;
6064 case "MODIFIERID":
6065 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
6066 break;
6067 case "ENROL":
6068 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
6069 break;
6070 case "SORTORDER":
6071 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
6072 break;
6076 } /// ends role_assignments
6078 if ($this->tree[5] == "ROLES_OVERRIDES") {
6079 if ($this->level == 7) {
6080 switch ($tagName) {
6081 case "NAME":
6082 $this->info->tempname = $this->getContents();
6083 break;
6084 case "SHORTNAME":
6085 $this->info->tempshortname = $this->getContents();
6086 break;
6087 case "ID":
6088 $this->info->tempid = $this->getContents(); // temp roleid
6089 break;
6093 if ($this->level == 9) {
6094 switch ($tagName) {
6095 case "NAME":
6097 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
6098 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
6099 $this->info->tempname = $this->getContents(); // change to name of capability
6100 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
6101 break;
6102 case "PERMISSION":
6103 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
6104 break;
6105 case "TIMEMODIFIED":
6106 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
6107 break;
6108 case "MODIFIERID":
6109 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
6110 break;
6113 } /// ends role_overrides
6116 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
6117 //Speed up a lot (avoid parse all)
6118 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
6119 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
6120 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
6121 $this->finished = true;
6124 //Clear things
6125 $this->tree[$this->level] = '';
6126 $this->level--;
6127 $this->content = "";
6130 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
6131 function endElementSections($parser, $tagName) {
6132 //Check if we are into SECTIONS zone
6133 if ($this->tree[3] == "SECTIONS") {
6134 //if (trim($this->content)) //Debug
6135 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6136 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6137 //Dependig of different combinations, do different things
6138 if ($this->level == 4) {
6139 switch ($tagName) {
6140 case "SECTION":
6141 //We've finalized a section, get it
6142 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
6143 unset($this->info->tempsection);
6146 if ($this->level == 5) {
6147 switch ($tagName) {
6148 case "ID":
6149 $this->info->tempsection->id = $this->getContents();
6150 break;
6151 case "NUMBER":
6152 $this->info->tempsection->number = $this->getContents();
6153 break;
6154 case "SUMMARY":
6155 $this->info->tempsection->summary = $this->getContents();
6156 break;
6157 case "VISIBLE":
6158 $this->info->tempsection->visible = $this->getContents();
6159 break;
6162 if ($this->level == 6) {
6163 switch ($tagName) {
6164 case "MOD":
6165 if (!isset($this->info->tempmod->groupmode)) {
6166 $this->info->tempmod->groupmode = 0;
6168 if (!isset($this->info->tempmod->groupingid)) {
6169 $this->info->tempmod->groupingid = 0;
6171 if (!isset($this->info->tempmod->groupmembersonly)) {
6172 $this->info->tempmod->groupmembersonly = 0;
6174 if (!isset($this->info->tempmod->idnumber)) {
6175 $this->info->tempmod->idnumber = null;
6178 //We've finalized a mod, get it
6179 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
6180 $this->info->tempmod->type;
6181 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
6182 $this->info->tempmod->instance;
6183 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
6184 $this->info->tempmod->added;
6185 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
6186 $this->info->tempmod->score;
6187 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
6188 $this->info->tempmod->indent;
6189 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
6190 $this->info->tempmod->visible;
6191 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
6192 $this->info->tempmod->groupmode;
6193 $this->info->tempsection->mods[$this->info->tempmod->id]->groupingid =
6194 $this->info->tempmod->groupingid;
6195 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmembersonly =
6196 $this->info->tempmod->groupmembersonly;
6197 $this->info->tempsection->mods[$this->info->tempmod->id]->idnumber =
6198 $this->info->tempmod->idnumber;
6200 unset($this->info->tempmod);
6203 if ($this->level == 7) {
6204 switch ($tagName) {
6205 case "ID":
6206 $this->info->tempmod->id = $this->getContents();
6207 break;
6208 case "TYPE":
6209 $this->info->tempmod->type = $this->getContents();
6210 break;
6211 case "INSTANCE":
6212 $this->info->tempmod->instance = $this->getContents();
6213 break;
6214 case "ADDED":
6215 $this->info->tempmod->added = $this->getContents();
6216 break;
6217 case "SCORE":
6218 $this->info->tempmod->score = $this->getContents();
6219 break;
6220 case "INDENT":
6221 $this->info->tempmod->indent = $this->getContents();
6222 break;
6223 case "VISIBLE":
6224 $this->info->tempmod->visible = $this->getContents();
6225 break;
6226 case "GROUPMODE":
6227 $this->info->tempmod->groupmode = $this->getContents();
6228 break;
6229 case "GROUPINGID":
6230 $this->info->tempmod->groupingid = $this->getContents();
6231 break;
6232 case "GROUPMEMBERSONLY":
6233 $this->info->tempmod->groupmembersonly = $this->getContents();
6234 break;
6235 case "IDNUMBER":
6236 $this->info->tempmod->idnumber = $this->getContents();
6237 break;
6238 default:
6239 break;
6243 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
6245 if ($this->level == 9) {
6246 switch ($tagName) {
6247 case "NAME":
6248 $this->info->tempname = $this->getContents();
6249 break;
6250 case "SHORTNAME":
6251 $this->info->tempshortname = $this->getContents();
6252 break;
6253 case "ID":
6254 $this->info->tempid = $this->getContents(); // temp roleid
6255 break;
6259 if ($this->level == 11) {
6260 switch ($tagName) {
6261 case "USERID":
6262 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
6264 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
6266 $this->info->tempuser = $this->getContents();
6268 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
6269 break;
6270 case "HIDDEN":
6271 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
6272 break;
6273 case "TIMESTART":
6274 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
6275 break;
6276 case "TIMEEND":
6277 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
6278 break;
6279 case "TIMEMODIFIED":
6280 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
6281 break;
6282 case "MODIFIERID":
6283 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
6284 break;
6285 case "ENROL":
6286 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
6287 break;
6288 case "SORTORDER":
6289 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
6290 break;
6294 } /// ends role_assignments
6296 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
6297 if ($this->level == 9) {
6298 switch ($tagName) {
6299 case "NAME":
6300 $this->info->tempname = $this->getContents();
6301 break;
6302 case "SHORTNAME":
6303 $this->info->tempshortname = $this->getContents();
6304 break;
6305 case "ID":
6306 $this->info->tempid = $this->getContents(); // temp roleid
6307 break;
6311 if ($this->level == 11) {
6312 switch ($tagName) {
6313 case "NAME":
6315 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
6316 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
6317 $this->info->tempname = $this->getContents(); // change to name of capability
6318 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
6319 break;
6320 case "PERMISSION":
6321 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
6322 break;
6323 case "TIMEMODIFIED":
6324 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
6325 break;
6326 case "MODIFIERID":
6327 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
6328 break;
6331 } /// ends role_overrides
6335 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
6336 //Speed up a lot (avoid parse all)
6337 if ($tagName == "SECTIONS") {
6338 $this->finished = true;
6341 //Clear things
6342 $this->tree[$this->level] = "";
6343 $this->level--;
6344 $this->content = "";
6348 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
6349 function endElementFormatData($parser, $tagName) {
6350 //Check if we are into FORMATDATA zone
6351 if ($this->tree[3] == 'FORMATDATA') {
6352 if (!isset($this->temp)) {
6353 $this->temp = '';
6355 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6358 if($tagName=='FORMATDATA') {
6359 //Did we have any data? If not don't bother
6360 if($this->temp!='<FORMATDATA></FORMATDATA>') {
6361 //Prepend XML standard header to info gathered
6362 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6363 $this->temp='';
6365 //Call to xmlize for this portion of xml data (the FORMATDATA block)
6366 $this->info->format_data = xmlize($xml_data,0);
6368 //Stop parsing at end of FORMATDATA
6369 $this->finished=true;
6372 //Clear things
6373 $this->tree[$this->level] = "";
6374 $this->level--;
6375 $this->content = "";
6378 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
6379 function endElementMetacourse($parser, $tagName) {
6380 //Check if we are into METACOURSE zone
6381 if ($this->tree[3] == 'METACOURSE') {
6382 //if (trim($this->content)) //Debug
6383 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6384 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6385 //Dependig of different combinations, do different things
6386 if ($this->level == 5) {
6387 switch ($tagName) {
6388 case 'CHILD':
6389 //We've finalized a child, get it
6390 $this->info->childs[] = $this->info->tempmeta;
6391 unset($this->info->tempmeta);
6392 break;
6393 case 'PARENT':
6394 //We've finalized a parent, get it
6395 $this->info->parents[] = $this->info->tempmeta;
6396 unset($this->info->tempmeta);
6397 break;
6398 default:
6399 die($tagName);
6402 if ($this->level == 6) {
6403 switch ($tagName) {
6404 case 'ID':
6405 $this->info->tempmeta->id = $this->getContents();
6406 break;
6407 case 'IDNUMBER':
6408 $this->info->tempmeta->idnumber = $this->getContents();
6409 break;
6410 case 'SHORTNAME':
6411 $this->info->tempmeta->shortname = $this->getContents();
6412 break;
6417 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
6418 //Speed up a lot (avoid parse all)
6419 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
6420 $this->finished = true;
6423 //Clear things
6424 $this->tree[$this->level] = '';
6425 $this->level--;
6426 $this->content = "";
6429 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
6430 function endElementGradebook($parser, $tagName) {
6431 //Check if we are into GRADEBOOK zone
6432 if ($this->tree[3] == "GRADEBOOK") {
6433 //if (trim($this->content)) //Debug
6434 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6435 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6436 //Acumulate data to info (content + close tag)
6437 //Reconvert: strip htmlchars again and trim to generate xml data
6438 if (!isset($this->temp)) {
6439 $this->temp = "";
6441 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6442 // We have finished outcome, grade_category or grade_item, reset accumulated
6443 // data because they are close tags
6444 if ($this->level == 4) {
6445 $this->temp = "";
6447 //If we've finished a grade item, xmlize it an save to db
6448 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
6449 //Prepend XML standard header to info gathered
6450 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6451 //Call to xmlize for this portion of xml data (one PREFERENCE)
6452 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6453 $data = xmlize($xml_data,0);
6454 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
6455 $this->counter++;
6456 //Save to db
6458 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
6459 null,$data);
6460 //Create returning info
6461 $this->info = $this->counter;
6462 //Reset temp
6464 unset($this->temp);
6467 //If we've finished a grade_category, xmlize it an save to db
6468 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
6469 //Prepend XML standard header to info gathered
6470 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6471 //Call to xmlize for this portion of xml data (one CATECORY)
6472 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6473 $data = xmlize($xml_data,0);
6474 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
6475 $this->counter++;
6476 //Save to db
6477 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
6478 null,$data);
6479 //Create returning info
6480 $this->info = $this->counter;
6481 //Reset temp
6482 unset($this->temp);
6485 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
6486 //Prepend XML standard header to info gathered
6487 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6488 //Call to xmlize for this portion of xml data (one CATECORY)
6489 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6490 $data = xmlize($xml_data,0);
6491 $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
6492 $this->counter++;
6493 //Save to db
6494 $status = backup_putid($this->preferences->backup_unique_code, 'grade_letters' ,$letter_id,
6495 null,$data);
6496 //Create returning info
6497 $this->info = $this->counter;
6498 //Reset temp
6499 unset($this->temp);
6502 //If we've finished a grade_outcome, xmlize it an save to db
6503 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
6504 //Prepend XML standard header to info gathered
6505 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6506 //Call to xmlize for this portion of xml data (one CATECORY)
6507 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6508 $data = xmlize($xml_data,0);
6509 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
6510 $this->counter++;
6511 //Save to db
6512 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
6513 null,$data);
6514 //Create returning info
6515 $this->info = $this->counter;
6516 //Reset temp
6517 unset($this->temp);
6520 //If we've finished a grade_outcomes_course, xmlize it an save to db
6521 if (($this->level == 5) and ($tagName == "GRADE_OUTCOMES_COURSE")) {
6522 //Prepend XML standard header to info gathered
6523 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6524 //Call to xmlize for this portion of xml data (one CATECORY)
6525 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6526 $data = xmlize($xml_data,0);
6527 $outcomes_course_id = $data["GRADE_OUTCOMES_COURSE"]["#"]["ID"]["0"]["#"];
6528 $this->counter++;
6529 //Save to db
6530 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_courses' ,$outcomes_course_id,
6531 null,$data);
6532 //Create returning info
6533 $this->info = $this->counter;
6534 //Reset temp
6535 unset($this->temp);
6538 if (($this->level == 5) and ($tagName == "GRADE_CATEGORIES_HISTORY")) {
6539 //Prepend XML standard header to info gathered
6540 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6541 //Call to xmlize for this portion of xml data (one PREFERENCE)
6542 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6543 $data = xmlize($xml_data,0);
6544 $id = $data["GRADE_CATEGORIES_HISTORY"]["#"]["ID"]["0"]["#"];
6545 $this->counter++;
6546 //Save to db
6548 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories_history', $id,
6549 null,$data);
6550 //Create returning info
6551 $this->info = $this->counter;
6552 //Reset temp
6554 unset($this->temp);
6557 if (($this->level == 5) and ($tagName == "GRADE_GRADES_HISTORY")) {
6558 //Prepend XML standard header to info gathered
6559 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6560 //Call to xmlize for this portion of xml data (one PREFERENCE)
6561 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6562 $data = xmlize($xml_data,0);
6563 $id = $data["GRADE_GRADES_HISTORY"]["#"]["ID"]["0"]["#"];
6564 $this->counter++;
6565 //Save to db
6567 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_history', $id,
6568 null,$data);
6569 //Create returning info
6570 $this->info = $this->counter;
6571 //Reset temp
6573 unset($this->temp);
6576 if (($this->level == 5) and ($tagName == "GRADE_ITEM_HISTORY")) {
6577 //Prepend XML standard header to info gathered
6578 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6579 //Call to xmlize for this portion of xml data (one PREFERENCE)
6580 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6581 $data = xmlize($xml_data,0);
6582 $id = $data["GRADE_ITEM_HISTORY"]["#"]["ID"]["0"]["#"];
6583 $this->counter++;
6584 //Save to db
6586 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items_history', $id,
6587 null,$data);
6588 //Create returning info
6589 $this->info = $this->counter;
6590 //Reset temp
6592 unset($this->temp);
6595 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME_HISTORY")) {
6596 //Prepend XML standard header to info gathered
6597 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6598 //Call to xmlize for this portion of xml data (one PREFERENCE)
6599 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6600 $data = xmlize($xml_data,0);
6601 $id = $data["GRADE_OUTCOME_HISTORY"]["#"]["ID"]["0"]["#"];
6602 $this->counter++;
6603 //Save to db
6605 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_history', $id,
6606 null,$data);
6607 //Create returning info
6608 $this->info = $this->counter;
6609 //Reset temp
6611 unset($this->temp);
6615 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
6616 //Speed up a lot (avoid parse all)
6617 if ($tagName == "GRADEBOOK" and $this->level == 3) {
6618 $this->finished = true;
6619 $this->counter = 0;
6622 //Clear things
6623 $this->tree[$this->level] = "";
6624 $this->level--;
6625 $this->content = "";
6629 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
6630 function endElementOldGradebook($parser, $tagName) {
6631 //Check if we are into GRADEBOOK zone
6632 if ($this->tree[3] == "GRADEBOOK") {
6633 //if (trim($this->content)) //Debug
6634 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6635 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6636 //Acumulate data to info (content + close tag)
6637 //Reconvert: strip htmlchars again and trim to generate xml data
6638 if (!isset($this->temp)) {
6639 $this->temp = "";
6641 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6642 //We have finished preferences, letters or categories, reset accumulated
6643 //data because they are close tags
6644 if ($this->level == 4) {
6645 $this->temp = "";
6647 //If we've finished a message, xmlize it an save to db
6648 if (($this->level == 5) and ($tagName == "GRADE_PREFERENCE")) {
6649 //Prepend XML standard header to info gathered
6650 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6651 //Call to xmlize for this portion of xml data (one PREFERENCE)
6652 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6653 $data = xmlize($xml_data,0);
6654 //echo strftime ("%X",time())."<p>"; //Debug
6655 //traverse_xmlize($data); //Debug
6656 //print_object ($GLOBALS['traverse_array']); //Debug
6657 //$GLOBALS['traverse_array']=""; //Debug
6658 //Now, save data to db. We'll use it later
6659 //Get id and status from data
6660 $preference_id = $data["GRADE_PREFERENCE"]["#"]["ID"]["0"]["#"];
6661 $this->counter++;
6662 //Save to db
6663 $status = backup_putid($this->preferences->backup_unique_code, 'grade_preferences', $preference_id,
6664 null,$data);
6665 //Create returning info
6666 $this->info = $this->counter;
6667 //Reset temp
6668 unset($this->temp);
6670 //If we've finished a grade_letter, xmlize it an save to db
6671 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
6672 //Prepend XML standard header to info gathered
6673 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6674 //Call to xmlize for this portion of xml data (one LETTER)
6675 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6676 $data = xmlize($xml_data,0);
6677 //echo strftime ("%X",time())."<p>"; //Debug
6678 //traverse_xmlize($data); //Debug
6679 //print_object ($GLOBALS['traverse_array']); //Debug
6680 //$GLOBALS['traverse_array']=""; //Debug
6681 //Now, save data to db. We'll use it later
6682 //Get id and status from data
6683 $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
6684 $this->counter++;
6685 //Save to db
6686 $status = backup_putid($this->preferences->backup_unique_code, 'grade_letter' ,$letter_id,
6687 null,$data);
6688 //Create returning info
6689 $this->info = $this->counter;
6690 //Reset temp
6691 unset($this->temp);
6694 //If we've finished a grade_category, xmlize it an save to db
6695 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
6696 //Prepend XML standard header to info gathered
6697 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6698 //Call to xmlize for this portion of xml data (one CATECORY)
6699 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6700 $data = xmlize($xml_data,0);
6701 //echo strftime ("%X",time())."<p>"; //Debug
6702 //traverse_xmlize($data); //Debug
6703 //print_object ($GLOBALS['traverse_array']); //Debug
6704 //$GLOBALS['traverse_array']=""; //Debug
6705 //Now, save data to db. We'll use it later
6706 //Get id and status from data
6707 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
6708 $this->counter++;
6709 //Save to db
6710 $status = backup_putid($this->preferences->backup_unique_code, 'grade_category' ,$category_id,
6711 null,$data);
6712 //Create returning info
6713 $this->info = $this->counter;
6714 //Reset temp
6715 unset($this->temp);
6719 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
6720 //Speed up a lot (avoid parse all)
6721 if ($tagName == "GRADEBOOK" and $this->level == 3) {
6722 $this->finished = true;
6723 $this->counter = 0;
6726 //Clear things
6727 $this->tree[$this->level] = "";
6728 $this->level--;
6729 $this->content = "";
6733 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
6734 function endElementUsers($parser, $tagName) {
6735 global $CFG;
6736 //Check if we are into USERS zone
6737 if ($this->tree[3] == "USERS") {
6738 //if (trim($this->content)) //Debug
6739 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6740 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6741 //Dependig of different combinations, do different things
6742 if ($this->level == 4) {
6743 switch ($tagName) {
6744 case "USER":
6745 //Increment counter
6746 $this->counter++;
6747 //Save to db, only save if record not already exist
6748 // if there already is an new_id for this entry, just use that new_id?
6749 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
6750 if (isset($newuser->new_id)) {
6751 $newid = $newuser->new_id;
6752 } else {
6753 $newid = null;
6756 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
6757 $newid,$this->info->tempuser);
6759 //Do some output
6760 if ($this->counter % 10 == 0) {
6761 if (!defined('RESTORE_SILENTLY')) {
6762 echo ".";
6763 if ($this->counter % 200 == 0) {
6764 echo "<br />";
6767 backup_flush(300);
6770 //Delete temp obejct
6771 unset($this->info->tempuser);
6772 break;
6776 if ($this->level == 5) {
6777 switch ($tagName) {
6778 case "ID":
6779 $this->info->users[$this->getContents()] = $this->getContents();
6780 $this->info->tempuser->id = $this->getContents();
6781 break;
6782 case "AUTH":
6783 $this->info->tempuser->auth = $this->getContents();
6784 break;
6785 case "CONFIRMED":
6786 $this->info->tempuser->confirmed = $this->getContents();
6787 break;
6788 case "POLICYAGREED":
6789 $this->info->tempuser->policyagreed = $this->getContents();
6790 break;
6791 case "DELETED":
6792 $this->info->tempuser->deleted = $this->getContents();
6793 break;
6794 case "USERNAME":
6795 $this->info->tempuser->username = $this->getContents();
6796 break;
6797 case "PASSWORD":
6798 $this->info->tempuser->password = $this->getContents();
6799 break;
6800 case "IDNUMBER":
6801 $this->info->tempuser->idnumber = $this->getContents();
6802 break;
6803 case "FIRSTNAME":
6804 $this->info->tempuser->firstname = $this->getContents();
6805 break;
6806 case "LASTNAME":
6807 $this->info->tempuser->lastname = $this->getContents();
6808 break;
6809 case "EMAIL":
6810 $this->info->tempuser->email = $this->getContents();
6811 break;
6812 case "EMAILSTOP":
6813 $this->info->tempuser->emailstop = $this->getContents();
6814 break;
6815 case "ICQ":
6816 $this->info->tempuser->icq = $this->getContents();
6817 break;
6818 case "SKYPE":
6819 $this->info->tempuser->skype = $this->getContents();
6820 break;
6821 case "AIM":
6822 $this->info->tempuser->aim = $this->getContents();
6823 break;
6824 case "YAHOO":
6825 $this->info->tempuser->yahoo = $this->getContents();
6826 break;
6827 case "MSN":
6828 $this->info->tempuser->msn = $this->getContents();
6829 break;
6830 case "PHONE1":
6831 $this->info->tempuser->phone1 = $this->getContents();
6832 break;
6833 case "PHONE2":
6834 $this->info->tempuser->phone2 = $this->getContents();
6835 break;
6836 case "INSTITUTION":
6837 $this->info->tempuser->institution = $this->getContents();
6838 break;
6839 case "DEPARTMENT":
6840 $this->info->tempuser->department = $this->getContents();
6841 break;
6842 case "ADDRESS":
6843 $this->info->tempuser->address = $this->getContents();
6844 break;
6845 case "CITY":
6846 $this->info->tempuser->city = $this->getContents();
6847 break;
6848 case "COUNTRY":
6849 $this->info->tempuser->country = $this->getContents();
6850 break;
6851 case "LANG":
6852 $this->info->tempuser->lang = $this->getContents();
6853 break;
6854 case "THEME":
6855 $this->info->tempuser->theme = $this->getContents();
6856 break;
6857 case "TIMEZONE":
6858 $this->info->tempuser->timezone = $this->getContents();
6859 break;
6860 case "FIRSTACCESS":
6861 $this->info->tempuser->firstaccess = $this->getContents();
6862 break;
6863 case "LASTACCESS":
6864 $this->info->tempuser->lastaccess = $this->getContents();
6865 break;
6866 case "LASTLOGIN":
6867 $this->info->tempuser->lastlogin = $this->getContents();
6868 break;
6869 case "CURRENTLOGIN":
6870 $this->info->tempuser->currentlogin = $this->getContents();
6871 break;
6872 case "LASTIP":
6873 $this->info->tempuser->lastip = $this->getContents();
6874 break;
6875 case "PICTURE":
6876 $this->info->tempuser->picture = $this->getContents();
6877 break;
6878 case "URL":
6879 $this->info->tempuser->url = $this->getContents();
6880 break;
6881 case "DESCRIPTION":
6882 $this->info->tempuser->description = $this->getContents();
6883 break;
6884 case "MAILFORMAT":
6885 $this->info->tempuser->mailformat = $this->getContents();
6886 break;
6887 case "MAILDIGEST":
6888 $this->info->tempuser->maildigest = $this->getContents();
6889 break;
6890 case "MAILDISPLAY":
6891 $this->info->tempuser->maildisplay = $this->getContents();
6892 break;
6893 case "HTMLEDITOR":
6894 $this->info->tempuser->htmleditor = $this->getContents();
6895 break;
6896 case "AJAX":
6897 $this->info->tempuser->ajax = $this->getContents();
6898 break;
6899 case "AUTOSUBSCRIBE":
6900 $this->info->tempuser->autosubscribe = $this->getContents();
6901 break;
6902 case "TRACKFORUMS":
6903 $this->info->tempuser->trackforums = $this->getContents();
6904 break;
6905 case "MNETHOSTURL":
6906 $this->info->tempuser->mnethosturl = $this->getContents();
6907 break;
6908 case "TIMEMODIFIED":
6909 $this->info->tempuser->timemodified = $this->getContents();
6910 break;
6911 default:
6912 break;
6916 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
6917 switch ($tagName) {
6918 case "ROLE":
6919 //We've finalized a role, get it
6920 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
6921 unset($this->info->temprole);
6922 break;
6923 case "USER_PREFERENCE":
6924 //We've finalized a user_preference, get it
6925 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
6926 unset($this->info->tempuserpreference);
6927 break;
6928 case "USER_CUSTOM_PROFILE_FIELD":
6929 //We've finalized a user_custom_profile_field, get it
6930 $this->info->tempuser->user_custom_profile_fields[] = $this->info->tempusercustomprofilefield;
6931 unset($this->info->tempusercustomprofilefield);
6932 break;
6933 case "USER_TAG":
6934 //We've finalized a user_tag, get it
6935 $this->info->tempuser->user_tags[] = $this->info->tempusertag;
6936 unset($this->info->tempusertag);
6937 break;
6938 default:
6939 break;
6943 if ($this->level == 7 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
6944 /// If we are reading roles
6945 if($this->tree[6] == 'ROLE') {
6946 switch ($tagName) {
6947 case "TYPE":
6948 $this->info->temprole->type = $this->getContents();
6949 break;
6950 case "AUTHORITY":
6951 $this->info->temprole->authority = $this->getContents();
6952 break;
6953 case "TEA_ROLE":
6954 $this->info->temprole->tea_role = $this->getContents();
6955 break;
6956 case "EDITALL":
6957 $this->info->temprole->editall = $this->getContents();
6958 break;
6959 case "TIMESTART":
6960 $this->info->temprole->timestart = $this->getContents();
6961 break;
6962 case "TIMEEND":
6963 $this->info->temprole->timeend = $this->getContents();
6964 break;
6965 case "TIMEMODIFIED":
6966 $this->info->temprole->timemodified = $this->getContents();
6967 break;
6968 case "TIMESTART":
6969 $this->info->temprole->timestart = $this->getContents();
6970 break;
6971 case "TIMEEND":
6972 $this->info->temprole->timeend = $this->getContents();
6973 break;
6974 case "TIME":
6975 $this->info->temprole->time = $this->getContents();
6976 break;
6977 case "TIMEACCESS":
6978 $this->info->temprole->timeaccess = $this->getContents();
6979 break;
6980 case "ENROL":
6981 $this->info->temprole->enrol = $this->getContents();
6982 break;
6983 default:
6984 break;
6986 /// If we are reading user_preferences
6987 } else if ($this->tree[6] == 'USER_PREFERENCE') {
6988 switch ($tagName) {
6989 case "NAME":
6990 $this->info->tempuserpreference->name = $this->getContents();
6991 break;
6992 case "VALUE":
6993 $this->info->tempuserpreference->value = $this->getContents();
6994 break;
6995 default:
6996 break;
6998 /// If we are reading user_custom_profile_fields
6999 } else if ($this->tree[6] == 'USER_CUSTOM_PROFILE_FIELD') {
7000 switch ($tagName) {
7001 case "FIELD_NAME":
7002 $this->info->tempusercustomprofilefield->field_name = $this->getContents();
7003 break;
7004 case "FIELD_TYPE":
7005 $this->info->tempusercustomprofilefield->field_type = $this->getContents();
7006 break;
7007 case "FIELD_DATA":
7008 $this->info->tempusercustomprofilefield->field_data = $this->getContents();
7009 break;
7010 default:
7011 break;
7013 /// If we are reading user_tags
7014 } else if ($this->tree[6] == 'USER_TAG') {
7015 switch ($tagName) {
7016 case "NAME":
7017 $this->info->tempusertag->name = $this->getContents();
7018 break;
7019 case "RAWNAME":
7020 $this->info->tempusertag->rawname = $this->getContents();
7021 break;
7022 default:
7023 break;
7028 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
7030 if ($this->level == 7) {
7031 switch ($tagName) {
7032 case "NAME":
7033 $this->info->tempname = $this->getContents();
7034 break;
7035 case "SHORTNAME":
7036 $this->info->tempshortname = $this->getContents();
7037 break;
7038 case "ID":
7039 $this->info->tempid = $this->getContents(); // temp roleid
7040 break;
7044 if ($this->level == 9) {
7046 switch ($tagName) {
7047 case "USERID":
7048 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
7050 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
7052 $this->info->tempuserid = $this->getContents();
7054 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
7055 break;
7056 case "HIDDEN":
7057 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
7058 break;
7059 case "TIMESTART":
7060 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
7061 break;
7062 case "TIMEEND":
7063 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
7064 break;
7065 case "TIMEMODIFIED":
7066 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
7067 break;
7068 case "MODIFIERID":
7069 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
7070 break;
7071 case "ENROL":
7072 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
7073 break;
7074 case "SORTORDER":
7075 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
7076 break;
7080 } /// ends role_assignments
7082 if ($this->tree[5] == "ROLES_OVERRIDES") {
7083 if ($this->level == 7) {
7084 switch ($tagName) {
7085 case "NAME":
7086 $this->info->tempname = $this->getContents();
7087 break;
7088 case "SHORTNAME":
7089 $this->info->tempshortname = $this->getContents();
7090 break;
7091 case "ID":
7092 $this->info->tempid = $this->getContents(); // temp roleid
7093 break;
7097 if ($this->level == 9) {
7098 switch ($tagName) {
7099 case "NAME":
7101 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
7102 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
7103 $this->info->tempname = $this->getContents(); // change to name of capability
7104 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
7105 break;
7106 case "PERMISSION":
7107 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
7108 break;
7109 case "TIMEMODIFIED":
7110 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
7111 break;
7112 case "MODIFIERID":
7113 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
7114 break;
7117 } /// ends role_overrides
7119 } // closes if this->tree[3]=="users"
7121 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
7122 //Speed up a lot (avoid parse all)
7123 if ($tagName == "USERS" and $this->level == 3) {
7124 $this->finished = true;
7125 $this->counter = 0;
7128 //Clear things
7129 $this->tree[$this->level] = "";
7130 $this->level--;
7131 $this->content = "";
7135 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
7136 function endElementMessages($parser, $tagName) {
7137 //Check if we are into MESSAGES zone
7138 if ($this->tree[3] == "MESSAGES") {
7139 //if (trim($this->content)) //Debug
7140 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7141 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
7142 //Acumulate data to info (content + close tag)
7143 //Reconvert: strip htmlchars again and trim to generate xml data
7144 if (!isset($this->temp)) {
7145 $this->temp = "";
7147 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7148 //If we've finished a message, xmlize it an save to db
7149 if (($this->level == 4) and ($tagName == "MESSAGE")) {
7150 //Prepend XML standard header to info gathered
7151 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7152 //Call to xmlize for this portion of xml data (one MESSAGE)
7153 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7154 $data = xmlize($xml_data,0);
7155 //echo strftime ("%X",time())."<p>"; //Debug
7156 //traverse_xmlize($data); //Debug
7157 //print_object ($GLOBALS['traverse_array']); //Debug
7158 //$GLOBALS['traverse_array']=""; //Debug
7159 //Now, save data to db. We'll use it later
7160 //Get id and status from data
7161 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
7162 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
7163 if ($message_status == "READ") {
7164 $table = "message_read";
7165 } else {
7166 $table = "message";
7168 $this->counter++;
7169 //Save to db
7170 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
7171 null,$data);
7172 //Create returning info
7173 $this->info = $this->counter;
7174 //Reset temp
7175 unset($this->temp);
7177 //If we've finished a contact, xmlize it an save to db
7178 if (($this->level == 5) and ($tagName == "CONTACT")) {
7179 //Prepend XML standard header to info gathered
7180 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7181 //Call to xmlize for this portion of xml data (one MESSAGE)
7182 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7183 $data = xmlize($xml_data,0);
7184 //echo strftime ("%X",time())."<p>"; //Debug
7185 //traverse_xmlize($data); //Debug
7186 //print_object ($GLOBALS['traverse_array']); //Debug
7187 //$GLOBALS['traverse_array']=""; //Debug
7188 //Now, save data to db. We'll use it later
7189 //Get id and status from data
7190 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
7191 $this->counter++;
7192 //Save to db
7193 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
7194 null,$data);
7195 //Create returning info
7196 $this->info = $this->counter;
7197 //Reset temp
7198 unset($this->temp);
7202 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
7203 //Speed up a lot (avoid parse all)
7204 if ($tagName == "MESSAGES" and $this->level == 3) {
7205 $this->finished = true;
7206 $this->counter = 0;
7209 //Clear things
7210 $this->tree[$this->level] = "";
7211 $this->level--;
7212 $this->content = "";
7216 //This is the endTag handler we use where we are reading the blogs zone (todo="BLOGS")
7217 function endElementBlogs($parser, $tagName) {
7218 //Check if we are into BLOGS zone
7219 if ($this->tree[3] == "BLOGS") {
7220 //if (trim($this->content)) //Debug
7221 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7222 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
7223 //Acumulate data to info (content + close tag)
7224 //Reconvert: strip htmlchars again and trim to generate xml data
7225 if (!isset($this->temp)) {
7226 $this->temp = "";
7228 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7229 //If we've finished a blog, xmlize it an save to db
7230 if (($this->level == 4) and ($tagName == "BLOG")) {
7231 //Prepend XML standard header to info gathered
7232 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7233 //Call to xmlize for this portion of xml data (one BLOG)
7234 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7235 $data = xmlize($xml_data,0);
7236 //echo strftime ("%X",time())."<p>"; //Debug
7237 //traverse_xmlize($data); //Debug
7238 //print_object ($GLOBALS['traverse_array']); //Debug
7239 //$GLOBALS['traverse_array']=""; //Debug
7240 //Now, save data to db. We'll use it later
7241 //Get id from data
7242 $blog_id = $data["BLOG"]["#"]["ID"]["0"]["#"];
7243 $this->counter++;
7244 //Save to db
7245 $status = backup_putid($this->preferences->backup_unique_code, 'blog', $blog_id,
7246 null,$data);
7247 //Create returning info
7248 $this->info = $this->counter;
7249 //Reset temp
7250 unset($this->temp);
7254 //Stop parsing if todo = BLOGS and tagName = BLOGS (end of the tag, of course)
7255 //Speed up a lot (avoid parse all)
7256 if ($tagName == "BLOGS" and $this->level == 3) {
7257 $this->finished = true;
7258 $this->counter = 0;
7261 //Clear things
7262 $this->tree[$this->level] = "";
7263 $this->level--;
7264 $this->content = "";
7268 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
7269 function endElementQuestions($parser, $tagName) {
7270 //Check if we are into QUESTION_CATEGORIES zone
7271 if ($this->tree[3] == "QUESTION_CATEGORIES") {
7272 //if (trim($this->content)) //Debug
7273 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7274 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7275 //Acumulate data to info (content + close tag)
7276 //Reconvert: strip htmlchars again and trim to generate xml data
7277 if (!isset($this->temp)) {
7278 $this->temp = "";
7280 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7281 //If we've finished a mod, xmlize it an save to db
7282 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
7283 //Prepend XML standard header to info gathered
7284 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7285 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
7286 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7287 $data = xmlize($xml_data,0);
7288 //echo strftime ("%X",time())."<p>"; //Debug
7289 //traverse_xmlize($data); //Debug
7290 //print_object ($GLOBALS['traverse_array']); //Debug
7291 //$GLOBALS['traverse_array']=""; //Debug
7292 //Now, save data to db. We'll use it later
7293 //Get id from data
7294 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
7295 //Save to db
7296 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
7297 null,$data);
7298 //Create returning info
7299 $ret_info = new object();
7300 $ret_info->id = $category_id;
7301 $this->info[] = $ret_info;
7302 //Reset temp
7303 unset($this->temp);
7307 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
7308 //Speed up a lot (avoid parse all)
7309 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
7310 $this->finished = true;
7313 //Clear things
7314 $this->tree[$this->level] = "";
7315 $this->level--;
7316 $this->content = "";
7320 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
7321 function endElementScales($parser, $tagName) {
7322 //Check if we are into SCALES zone
7323 if ($this->tree[3] == "SCALES") {
7324 //if (trim($this->content)) //Debug
7325 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7326 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7327 //Acumulate data to info (content + close tag)
7328 //Reconvert: strip htmlchars again and trim to generate xml data
7329 if (!isset($this->temp)) {
7330 $this->temp = "";
7332 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7333 //If we've finished a scale, xmlize it an save to db
7334 if (($this->level == 4) and ($tagName == "SCALE")) {
7335 //Prepend XML standard header to info gathered
7336 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7337 //Call to xmlize for this portion of xml data (one SCALE)
7338 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7339 $data = xmlize($xml_data,0);
7340 //echo strftime ("%X",time())."<p>"; //Debug
7341 //traverse_xmlize($data); //Debug
7342 //print_object ($GLOBALS['traverse_array']); //Debug
7343 //$GLOBALS['traverse_array']=""; //Debug
7344 //Now, save data to db. We'll use it later
7345 //Get id and from data
7346 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
7347 //Save to db
7348 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
7349 null,$data);
7350 //Create returning info
7351 $ret_info = new object();
7352 $ret_info->id = $scale_id;
7353 $this->info[] = $ret_info;
7354 //Reset temp
7355 unset($this->temp);
7359 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
7360 //Speed up a lot (avoid parse all)
7361 if ($tagName == "SCALES" and $this->level == 3) {
7362 $this->finished = true;
7365 //Clear things
7366 $this->tree[$this->level] = "";
7367 $this->level--;
7368 $this->content = "";
7372 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
7373 function endElementGroups($parser, $tagName) {
7374 //Check if we are into GROUPS zone
7375 if ($this->tree[3] == "GROUPS") {
7376 //if (trim($this->content)) //Debug
7377 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7378 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7379 //Acumulate data to info (content + close tag)
7380 //Reconvert: strip htmlchars again and trim to generate xml data
7381 if (!isset($this->temp)) {
7382 $this->temp = "";
7384 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7385 //If we've finished a group, xmlize it an save to db
7386 if (($this->level == 4) and ($tagName == "GROUP")) {
7387 //Prepend XML standard header to info gathered
7388 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7389 //Call to xmlize for this portion of xml data (one GROUP)
7390 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7391 $data = xmlize($xml_data,0);
7392 //echo strftime ("%X",time())."<p>"; //Debug
7393 //traverse_xmlize($data); //Debug
7394 //print_object ($GLOBALS['traverse_array']); //Debug
7395 //$GLOBALS['traverse_array']=""; //Debug
7396 //Now, save data to db. We'll use it later
7397 //Get id and from data
7398 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
7399 //Save to db
7400 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
7401 null,$data);
7402 //Create returning info
7403 $ret_info = new Object();
7404 $ret_info->id = $group_id;
7405 $this->info[] = $ret_info;
7406 //Reset temp
7407 unset($this->temp);
7411 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
7412 //Speed up a lot (avoid parse all)
7413 if ($tagName == "GROUPS" and $this->level == 3) {
7414 $this->finished = true;
7417 //Clear things
7418 $this->tree[$this->level] = "";
7419 $this->level--;
7420 $this->content = "";
7424 //This is the endTag handler we use where we are reading the groupings zone (todo="GROUPINGS")
7425 function endElementGroupings($parser, $tagName) {
7426 //Check if we are into GROUPINGS zone
7427 if ($this->tree[3] == "GROUPINGS") {
7428 //Acumulate data to info (content + close tag)
7429 //Reconvert: strip htmlchars again and trim to generate xml data
7430 if (!isset($this->temp)) {
7431 $this->temp = "";
7433 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7434 //If we've finished a group, xmlize it an save to db
7435 if (($this->level == 4) and ($tagName == "GROUPING")) {
7436 //Prepend XML standard header to info gathered
7437 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7438 //Call to xmlize for this portion of xml data (one GROUPING)
7439 $data = xmlize($xml_data,0);
7440 //Now, save data to db. We'll use it later
7441 //Get id and from data
7442 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
7443 //Save to db
7444 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
7445 null,$data);
7446 //Create returning info
7447 $ret_info = new Object();
7448 $ret_info->id = $grouping_id;
7449 $this->info[] = $ret_info;
7450 //Reset temp
7451 unset($this->temp);
7455 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
7456 //Speed up a lot (avoid parse all)
7457 if ($tagName == "GROUPINGS" and $this->level == 3) {
7458 $this->finished = true;
7461 //Clear things
7462 $this->tree[$this->level] = "";
7463 $this->level--;
7464 $this->content = "";
7468 //This is the endTag handler we use where we are reading the groupingsgroups zone (todo="GROUPINGGROUPS")
7469 function endElementGroupingsGroups($parser, $tagName) {
7470 //Check if we are into GROUPINGSGROUPS zone
7471 if ($this->tree[3] == "GROUPINGSGROUPS") {
7472 //Acumulate data to info (content + close tag)
7473 //Reconvert: strip htmlchars again and trim to generate xml data
7474 if (!isset($this->temp)) {
7475 $this->temp = "";
7477 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7478 //If we've finished a group, xmlize it an save to db
7479 if (($this->level == 4) and ($tagName == "GROUPINGGROUP")) {
7480 //Prepend XML standard header to info gathered
7481 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7482 //Call to xmlize for this portion of xml data (one GROUPING)
7483 $data = xmlize($xml_data,0);
7484 //Now, save data to db. We'll use it later
7485 //Get id and from data
7486 $groupinggroup_id = $data["GROUPINGGROUP"]["#"]["ID"]["0"]["#"];
7487 //Save to db
7488 $status = backup_putid($this->preferences->backup_unique_code,"groupingsgroups",$groupinggroup_id,
7489 null,$data);
7490 //Create returning info
7491 $ret_info = new Object();
7492 $ret_info->id = $groupinggroup_id;
7493 $this->info[] = $ret_info;
7494 //Reset temp
7495 unset($this->temp);
7499 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
7500 //Speed up a lot (avoid parse all)
7501 if ($tagName == "GROUPINGSGROUPS" and $this->level == 3) {
7502 $this->finished = true;
7505 //Clear things
7506 $this->tree[$this->level] = "";
7507 $this->level--;
7508 $this->content = "";
7512 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
7513 function endElementEvents($parser, $tagName) {
7514 //Check if we are into EVENTS zone
7515 if ($this->tree[3] == "EVENTS") {
7516 //if (trim($this->content)) //Debug
7517 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7518 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7519 //Acumulate data to info (content + close tag)
7520 //Reconvert: strip htmlchars again and trim to generate xml data
7521 if (!isset($this->temp)) {
7522 $this->temp = "";
7524 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7525 //If we've finished a event, xmlize it an save to db
7526 if (($this->level == 4) and ($tagName == "EVENT")) {
7527 //Prepend XML standard header to info gathered
7528 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7529 //Call to xmlize for this portion of xml data (one EVENT)
7530 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7531 $data = xmlize($xml_data,0);
7532 //echo strftime ("%X",time())."<p>"; //Debug
7533 //traverse_xmlize($data); //Debug
7534 //print_object ($GLOBALS['traverse_array']); //Debug
7535 //$GLOBALS['traverse_array']=""; //Debug
7536 //Now, save data to db. We'll use it later
7537 //Get id and from data
7538 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
7539 //Save to db
7540 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
7541 null,$data);
7542 //Create returning info
7543 $ret_info = new object();
7544 $ret_info->id = $event_id;
7545 $this->info[] = $ret_info;
7546 //Reset temp
7547 unset($this->temp);
7551 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
7552 //Speed up a lot (avoid parse all)
7553 if ($tagName == "EVENTS" and $this->level == 3) {
7554 $this->finished = true;
7557 //Clear things
7558 $this->tree[$this->level] = "";
7559 $this->level--;
7560 $this->content = "";
7564 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
7565 function endElementModules($parser, $tagName) {
7566 //Check if we are into MODULES zone
7567 if ($this->tree[3] == "MODULES") {
7568 //if (trim($this->content)) //Debug
7569 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7570 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7571 //Acumulate data to info (content + close tag)
7572 //Reconvert: strip htmlchars again and trim to generate xml data
7573 if (!isset($this->temp)) {
7574 $this->temp = "";
7576 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7577 //If we've finished a mod, xmlize it an save to db
7578 if (($this->level == 4) and ($tagName == "MOD")) {
7579 //Only process the module if efectively it has been selected for restore. MDL-18482
7580 if (empty($this->preferences->mods[$this->temp_mod_type]->granular) // We don't care about per instance, i.e. restore all instances.
7581 or (array_key_exists($this->temp_mod_id, $this->preferences->mods[$this->temp_mod_type]->instances)
7583 !empty($this->preferences->mods[$this->temp_mod_type]->instances[$this->temp_mod_id]->restore))) {
7585 //Prepend XML standard header to info gathered
7586 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7587 //Call to xmlize for this portion of xml data (one MOD)
7588 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7589 $data = xmlize($xml_data,0);
7590 //echo strftime ("%X",time())."<p>"; //Debug
7591 //traverse_xmlize($data); //Debug
7592 //print_object ($GLOBALS['traverse_array']); //Debug
7593 //$GLOBALS['traverse_array']=""; //Debug
7594 //Now, save data to db. We'll use it later
7595 //Get id and modtype from data
7596 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
7597 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
7598 //Only if we've selected to restore it
7599 if (!empty($this->preferences->mods[$mod_type]->restore)) {
7600 //Save to db
7601 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
7602 null,$data);
7603 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
7604 //Create returning info
7605 $ret_info = new object();
7606 $ret_info->id = $mod_id;
7607 $ret_info->modtype = $mod_type;
7608 $this->info[] = $ret_info;
7610 } else {
7611 debugging("Info: skipping $this->temp_mod_type activity with mod id: $this->temp_mod_id. Not selected for restore", DEBUG_DEVELOPER);
7613 //Reset current mod_type and mod_id
7614 unset($this->temp_mod_type);
7615 unset($this->temp_mod_id);
7616 //Reset temp
7617 unset($this->temp);
7620 /// Grab current mod id and type when available
7621 if ($this->level == 5) {
7622 if ($tagName == 'ID') {
7623 $this->temp_mod_id = trim($this->content);
7624 } else if ($tagName == 'MODTYPE') {
7625 $this->temp_mod_type = trim($this->content);
7631 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
7632 //Speed up a lot (avoid parse all)
7633 if ($tagName == "MODULES" and $this->level == 3) {
7634 $this->finished = true;
7637 //Clear things
7638 $this->tree[$this->level] = "";
7639 $this->level--;
7640 $this->content = "";
7644 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
7645 function endElementLogs($parser, $tagName) {
7646 //Check if we are into LOGS zone
7647 if ($this->tree[3] == "LOGS") {
7648 //if (trim($this->content)) //Debug
7649 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7650 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7651 //Acumulate data to info (content + close tag)
7652 //Reconvert: strip htmlchars again and trim to generate xml data
7653 if (!isset($this->temp)) {
7654 $this->temp = "";
7656 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
7657 //If we've finished a log, xmlize it an save to db
7658 if (($this->level == 4) and ($tagName == "LOG")) {
7659 //Prepend XML standard header to info gathered
7660 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
7661 //Call to xmlize for this portion of xml data (one LOG)
7662 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
7663 $data = xmlize($xml_data,0);
7664 //echo strftime ("%X",time())."<p>"; //Debug
7665 //traverse_xmlize($data); //Debug
7666 //print_object ($GLOBALS['traverse_array']); //Debug
7667 //$GLOBALS['traverse_array']=""; //Debug
7668 //Now, save data to db. We'll use it later
7669 //Get id and modtype from data
7670 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
7671 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
7672 //We only save log entries from backup file if they are:
7673 // - Course logs
7674 // - User logs
7675 // - Module logs about one restored module
7676 if ($log_module == "course" or
7677 $log_module == "user" or
7678 $this->preferences->mods[$log_module]->restore) {
7679 //Increment counter
7680 $this->counter++;
7681 //Save to db
7682 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
7683 null,$data);
7684 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
7685 //Create returning info
7686 $this->info = $this->counter;
7688 //Reset temp
7689 unset($this->temp);
7693 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
7694 //Speed up a lot (avoid parse all)
7695 if ($tagName == "LOGS" and $this->level == 3) {
7696 $this->finished = true;
7697 $this->counter = 0;
7700 //Clear things
7701 $this->tree[$this->level] = "";
7702 $this->level--;
7703 $this->content = "";
7707 //This is the endTag default handler we use when todo is undefined
7708 function endElement($parser, $tagName) {
7709 if (trim($this->content)) //Debug
7710 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7711 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7713 //Clear things
7714 $this->tree[$this->level] = "";
7715 $this->level--;
7716 $this->content = "";
7719 //This is the handler to read data contents (simple accumule it)
7720 function characterData($parser, $data) {
7721 $this->content .= $data;
7725 //This function executes the MoodleParser
7726 function restore_read_xml ($xml_file,$todo,$preferences) {
7728 global $CFG;
7730 $status = true;
7732 /// If enabled in the site, use split files instead of original moodle.xml file
7733 /// This will speed parsing speed upto 20x.
7734 if (!empty($CFG->experimentalsplitrestore)) {
7735 /// Use splite file, else nothing to process (saves one full parsing for each non-existing todo)
7736 $splitfile= dirname($xml_file) . '/' . strtolower('split_' . $todo . '.xml');
7737 if (file_exists($splitfile)) {
7738 $xml_file = $splitfile;
7739 debugging("Info: todo=$todo, using split file", DEBUG_DEVELOPER);
7740 } else {
7741 /// For some todos, that are used in earlier restore steps (restore_precheck(), restore_form...
7742 /// allow fallback to monolithic moodle.xml. Those todos are at the beggining of the xml, so
7743 /// it doesn't hurts too much.
7744 if ($todo == 'INFO' || $todo == 'COURSE_HEADER' || $todo == 'ROLES') {
7745 debugging("Info: todo=$todo, no split file. Fallback to moodle.xml", DEBUG_DEVELOPER);
7746 } else {
7747 debugging("Info: todo=$todo, no split file. Parse skipped", DEBUG_DEVELOPER);
7748 return true;
7753 $xml_parser = xml_parser_create('UTF-8');
7754 $moodle_parser = new MoodleParser();
7755 $moodle_parser->todo = $todo;
7756 $moodle_parser->preferences = $preferences;
7757 xml_set_object($xml_parser,$moodle_parser);
7758 //Depending of the todo we use some element_handler or another
7759 if ($todo == "INFO") {
7760 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
7761 } else if ($todo == "ROLES") {
7762 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
7763 } else if ($todo == "COURSE_HEADER") {
7764 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
7765 } else if ($todo == 'BLOCKS') {
7766 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
7767 } else if ($todo == "SECTIONS") {
7768 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
7769 } else if ($todo == 'FORMATDATA') {
7770 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
7771 } else if ($todo == "METACOURSE") {
7772 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
7773 } else if ($todo == "GRADEBOOK") {
7774 if ($preferences->backup_version > 2007090500) {
7775 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
7776 } else {
7777 xml_set_element_handler($xml_parser, "startElementOldGradebook", "endElementOldGradebook");
7779 } else if ($todo == "USERS") {
7780 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
7781 } else if ($todo == "MESSAGES") {
7782 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
7783 } else if ($todo == "BLOGS") {
7784 xml_set_element_handler($xml_parser, "startElementBlogs", "endElementBlogs");
7785 } else if ($todo == "QUESTIONS") {
7786 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
7787 } else if ($todo == "SCALES") {
7788 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
7789 } else if ($todo == "GROUPS") {
7790 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
7791 } else if ($todo == "GROUPINGS") {
7792 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
7793 } else if ($todo == "GROUPINGSGROUPS") {
7794 xml_set_element_handler($xml_parser, "startElementGroupingsGroups", "endElementGroupingsGroups");
7795 } else if ($todo == "EVENTS") {
7796 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
7797 } else if ($todo == "MODULES") {
7798 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
7799 } else if ($todo == "LOGS") {
7800 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
7801 } else {
7802 //Define default handlers (must no be invoked when everything become finished)
7803 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
7805 xml_set_character_data_handler($xml_parser, "characterData");
7806 $fp = fopen($xml_file,"r")
7807 or $status = false;
7808 if ($status) {
7809 // MDL-9290 performance improvement on reading large xml
7810 $lasttime = time(); // crmas
7811 while ($data = fread($fp, 8192) and !$moodle_parser->finished) {
7813 if ((time() - $lasttime) > 5) {
7814 $lasttime = time();
7815 backup_flush(1);
7818 xml_parse($xml_parser, $data, feof($fp))
7819 or die(sprintf("XML error: %s at line %d",
7820 xml_error_string(xml_get_error_code($xml_parser)),
7821 xml_get_current_line_number($xml_parser)));
7823 fclose($fp);
7825 //Get info from parser
7826 $info = $moodle_parser->info;
7828 //Clear parser mem
7829 xml_parser_free($xml_parser);
7831 if ($status && !empty($info)) {
7832 return $info;
7833 } else {
7834 return $status;
7839 * @param string $errorstr passed by reference, if silent is true,
7840 * errorstr will be populated and this function will return false rather than calling error() or notify()
7841 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
7842 * redirect to the next step in the restore process, instead will return $backup_unique_code
7844 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
7846 global $CFG, $SESSION;
7848 //Prepend dataroot to variable to have the absolute path
7849 $file = $CFG->dataroot."/".$file;
7851 if (!defined('RESTORE_SILENTLY')) {
7852 //Start the main table
7853 echo "<table cellpadding=\"5\">";
7854 echo "<tr><td>";
7856 //Start the mail ul
7857 echo "<ul>";
7860 //Check the file exists
7861 if (!is_file($file)) {
7862 if (!defined('RESTORE_SILENTLY')) {
7863 error ("File not exists ($file)");
7864 } else {
7865 $errorstr = "File not exists ($file)";
7866 return false;
7870 //Check the file name ends with .zip
7871 if (!substr($file,-4) == ".zip") {
7872 if (!defined('RESTORE_SILENTLY')) {
7873 error ("File has an incorrect extension");
7874 } else {
7875 $errorstr = 'File has an incorrect extension';
7876 return false;
7880 //Now calculate the unique_code for this restore
7881 $backup_unique_code = time();
7883 //Now check and create the backup dir (if it doesn't exist)
7884 if (!defined('RESTORE_SILENTLY')) {
7885 echo "<li>".get_string("creatingtemporarystructures").'</li>';
7887 $status = check_and_create_backup_dir($backup_unique_code);
7888 //Empty dir
7889 if ($status) {
7890 $status = clear_backup_dir($backup_unique_code);
7893 //Now delete old data and directories under dataroot/temp/backup
7894 if ($status) {
7895 if (!defined('RESTORE_SILENTLY')) {
7896 echo "<li>".get_string("deletingolddata").'</li>';
7898 $status = backup_delete_old_data();
7901 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
7902 if ($status) {
7903 if (!defined('RESTORE_SILENTLY')) {
7904 echo "<li>".get_string("copyingzipfile").'</li>';
7906 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
7907 if (!defined('RESTORE_SILENTLY')) {
7908 notify("Error copying backup file. Invalid name or bad perms.");
7909 } else {
7910 $errorstr = "Error copying backup file. Invalid name or bad perms";
7911 return false;
7916 //Now unzip the file
7917 if ($status) {
7918 if (!defined('RESTORE_SILENTLY')) {
7919 echo "<li>".get_string("unzippingbackup").'</li>';
7921 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
7922 if (!defined('RESTORE_SILENTLY')) {
7923 notify("Error unzipping backup file. Invalid zip file.");
7924 } else {
7925 $errorstr = "Error unzipping backup file. Invalid zip file.";
7926 return false;
7931 // If experimental option is enabled (enableimsccimport)
7932 // check for Common Cartridge packages and convert to Moodle format
7933 if ($status && isset($CFG->enableimsccimport) && $CFG->enableimsccimport == 1) {
7934 require_once($CFG->dirroot. '/backup/cc/restore_cc.php');
7935 if (!defined('RESTORE_SILENTLY')) {
7936 echo "<li>".get_string('checkingforimscc', 'imscc').'</li>';
7938 $status = cc_convert($CFG->dataroot. DIRECTORY_SEPARATOR .'temp'. DIRECTORY_SEPARATOR . 'backup'. DIRECTORY_SEPARATOR . $backup_unique_code);
7941 //Check for Blackboard backups and convert
7942 if ($status){
7943 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
7944 if (!defined('RESTORE_SILENTLY')) {
7945 echo "<li>".get_string("checkingforbbexport").'</li>';
7947 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
7950 //Now check for the moodle.xml file
7951 if ($status) {
7952 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
7953 if (!defined('RESTORE_SILENTLY')) {
7954 echo "<li>".get_string("checkingbackup").'</li>';
7956 if (! $status = restore_check_moodle_file ($xml_file)) {
7957 if (!is_file($xml_file)) {
7958 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
7959 } else {
7960 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
7962 if (!defined('RESTORE_SILENTLY')) {
7963 notify($errorstr);
7964 } else {
7965 return false;
7970 $info = "";
7971 $course_header = "";
7973 //Now read the info tag (all)
7974 if ($status) {
7975 if (!defined('RESTORE_SILENTLY')) {
7976 echo "<li>".get_string("readinginfofrombackup").'</li>';
7978 //Reading info from file
7979 $info = restore_read_xml_info ($xml_file);
7980 //Reading course_header from file
7981 $course_header = restore_read_xml_course_header ($xml_file);
7983 if(!is_object($course_header)){
7984 // ensure we fail if there is no course header
7985 $course_header = false;
7989 if (!defined('RESTORE_SILENTLY')) {
7990 //End the main ul
7991 echo "</ul>\n";
7993 //End the main table
7994 echo "</td></tr>";
7995 echo "</table>";
7998 //We compare Moodle's versions
7999 if ($status && $CFG->version < $info->backup_moodle_version) {
8000 $message = new object();
8001 $message->serverversion = $CFG->version;
8002 $message->serverrelease = $CFG->release;
8003 $message->backupversion = $info->backup_moodle_version;
8004 $message->backuprelease = $info->backup_moodle_release;
8005 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
8009 //Now we print in other table, the backup and the course it contains info
8010 if ($info and $course_header and $status) {
8011 //First, the course info
8012 if (!defined('RESTORE_SILENTLY')) {
8013 $status = restore_print_course_header($course_header);
8015 //Now, the backup info
8016 if ($status) {
8017 if (!defined('RESTORE_SILENTLY')) {
8018 $status = restore_print_info($info);
8023 //Save course header and info into php session
8024 if ($status) {
8025 $SESSION->info = $info;
8026 $SESSION->course_header = $course_header;
8029 //Finally, a little form to continue
8030 //with some hidden fields
8031 if ($status) {
8032 if (!defined('RESTORE_SILENTLY')) {
8033 echo "<br /><div style='text-align:center'>";
8034 $hidden["backup_unique_code"] = $backup_unique_code;
8035 $hidden["launch"] = "form";
8036 $hidden["file"] = $file;
8037 $hidden["id"] = $id;
8038 print_single_button("restore.php", $hidden, get_string("continue"),"post");
8039 echo "</div>";
8041 else {
8042 if (empty($noredirect)) {
8043 print_continue($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id.'&sesskey='.sesskey());
8044 print_footer();
8045 die;
8047 } else {
8048 return $backup_unique_code;
8053 if (!$status) {
8054 if (!defined('RESTORE_SILENTLY')) {
8055 error ("An error has ocurred");
8056 } else {
8057 $errorstr = "An error has occured"; // helpful! :P
8058 return false;
8061 return true;
8064 function restore_setup_for_check(&$restore,$backup_unique_code) {
8065 global $SESSION;
8066 $restore->backup_unique_code=$backup_unique_code;
8067 $restore->users = 2; // yuk
8068 // we set these from restore object on silent restore and from info (backup) object on import
8069 $restore->course_files = isset($SESSION->restore->restore_course_files) ? $SESSION->restore->restore_course_files : $SESSION->info->backup_course_files;
8070 $restore->site_files = isset($SESSION->restore->restore_site_files) ? $SESSION->restore->restore_site_files : $SESSION->info->backup_site_files;
8071 if ($allmods = get_records("modules")) {
8072 foreach ($allmods as $mod) {
8073 $modname = $mod->name;
8074 $var = "restore_".$modname;
8075 //Now check that we have that module info in the backup file
8076 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
8077 $restore->$var = 1;
8081 // Calculate all session objects checksum and store them in session too
8082 // so restore_execute.html (used by manual restore and import) will be
8083 // able to detect any problem in session info.
8084 restore_save_session_object_checksums($restore, $SESSION->info, $SESSION->course_header);
8086 return true;
8090 * Save the checksum of the 3 main in-session restore objects (restore, info, course_header)
8091 * so restore_execute.html will be able to check that all them have arrived correctly, without
8092 * losing data for any type of session size limit/error. MDL-18469. Used both by manual restore
8093 * and import
8095 function restore_save_session_object_checksums($restore, $info, $course_header) {
8096 global $SESSION;
8097 $restore_checksums = array();
8098 $restore_checksums['info'] = md5(serialize($info));
8099 $restore_checksums['course_header'] = md5(serialize($course_header));
8100 $restore_checksums['restore'] = md5(serialize($restore));
8101 $SESSION->restore_checksums = $restore_checksums;
8104 function backup_to_restore_array($backup,$k=0) {
8105 if (is_array($backup) ) {
8106 $restore = array();
8107 foreach ($backup as $key => $value) {
8108 $newkey = str_replace('backup','restore',$key);
8109 $restore[$newkey] = backup_to_restore_array($value,$key);
8112 else if (is_object($backup)) {
8113 $restore = new stdClass();
8114 $tmp = get_object_vars($backup);
8115 foreach ($tmp as $key => $value) {
8116 $newkey = str_replace('backup','restore',$key);
8117 $restore->$newkey = backup_to_restore_array($value,$key);
8120 else {
8121 $newkey = str_replace('backup','restore',$k);
8122 $restore = $backup;
8124 return $restore;
8128 * compatibility function
8129 * checks for per-instance backups AND
8130 * older per-module backups
8131 * and returns whether userdata has been selected.
8133 function restore_userdata_selected($restore,$modname,$modid) {
8134 // check first for per instance array
8135 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
8136 return array_key_exists($modid,$restore->mods[$modname]->instances)
8137 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
8140 //print_object($restore->mods[$modname]);
8141 return !empty($restore->mods[$modname]->userinfo);
8144 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
8146 global $CFG, $USER;
8147 $status = true;
8149 //Checks for the required files/functions to restore every module
8150 //and include them
8151 if ($allmods = get_records("modules") ) {
8152 foreach ($allmods as $mod) {
8153 $modname = $mod->name;
8154 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
8155 //If file exists and we have selected to restore that type of module
8156 if ((file_exists($modfile)) and !empty($restore->mods[$modname]) and ($restore->mods[$modname]->restore)) {
8157 include_once($modfile);
8162 if (!defined('RESTORE_SILENTLY')) {
8163 //Start the main table
8164 echo "<table cellpadding=\"5\">";
8165 echo "<tr><td>";
8167 //Start the main ul
8168 echo "<ul>";
8171 //Location of the xml file
8172 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
8174 // Re-assure xml file is in place before any further process
8175 if (! $status = restore_check_moodle_file($xml_file)) {
8176 if (!is_file($xml_file)) {
8177 $errorstr = 'Error checking backup file. moodle.xml not found. Session problem?';
8178 } else {
8179 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted. Session problem?';
8181 if (!defined('RESTORE_SILENTLY')) {
8182 notify($errorstr);
8184 return false;
8187 //Preprocess the moodle.xml file spliting into smaller chucks (modules, users, logs...)
8188 //for optimal parsing later in the restore process.
8189 if (!empty($CFG->experimentalsplitrestore)) {
8190 if (!defined('RESTORE_SILENTLY')) {
8191 echo '<li>'.get_string('preprocessingbackupfile') . '</li>';
8193 //First of all, split moodle.xml into handy files
8194 if (!restore_split_xml ($xml_file, $restore)) {
8195 $errorstr = "Error proccessing moodle.xml file. Process ended.";
8196 if (!defined('RESTORE_SILENTLY')) {
8197 notify($errorstr);
8199 return false;
8203 // Precheck the users section, detecting various situations that can lead to problems, so
8204 // we stop restore before performing any further action
8206 if (!defined('RESTORE_SILENTLY')) {
8207 echo '<li>'.get_string('restoreusersprecheck').'</li>';
8209 if (!restore_precheck_users($xml_file, $restore, $problems)) {
8210 $errorstr = get_string('restoreusersprecheckerror');
8211 if (!empty($problems)) {
8212 $errorstr .= ' (' . implode(', ', $problems) . ')';
8214 if (!defined('RESTORE_SILENTLY')) {
8215 notify($errorstr);
8217 return false;
8221 //If we've selected to restore into new course
8222 //create it (course)
8223 //Saving conversion id variables into backup_tables
8224 if ($restore->restoreto == RESTORETO_NEW_COURSE) {
8225 if (!defined('RESTORE_SILENTLY')) {
8226 echo '<li>'.get_string('creatingnewcourse') . '</li>';
8228 $oldidnumber = $course_header->course_idnumber;
8229 if (!$status = restore_create_new_course($restore,$course_header)) {
8230 if (!defined('RESTORE_SILENTLY')) {
8231 notify("Error while creating the new empty course.");
8232 } else {
8233 $errorstr = "Error while creating the new empty course.";
8234 return false;
8238 //Print course fullname and shortname and category
8239 if ($status) {
8240 if (!defined('RESTORE_SILENTLY')) {
8241 echo "<ul>";
8242 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
8243 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
8244 if (!empty($oldidnumber)) {
8245 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
8247 echo "</ul>";
8248 //Put the destination course_id
8250 $restore->course_id = $course_header->course_id;
8253 if ($status = restore_open_html($restore,$course_header)){
8254 if (!defined('RESTORE_SILENTLY')) {
8255 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
8259 } else {
8260 $course = get_record("course","id",$restore->course_id);
8261 if ($course) {
8262 if (!defined('RESTORE_SILENTLY')) {
8263 echo "<li>".get_string("usingexistingcourse");
8264 echo "<ul>";
8265 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
8266 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
8267 if (($restore->deleting)) {
8268 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
8269 } else {
8270 echo "<li>".get_string("addingdatatoexisting").'</li>';
8272 echo "</ul></li>";
8274 //If we have selected to restore deleting, we do it now.
8275 if ($restore->deleting) {
8276 if (!defined('RESTORE_SILENTLY')) {
8277 echo "<li>".get_string("deletingolddata").'</li>';
8279 $status = remove_course_contents($restore->course_id,false) and
8280 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
8281 if ($status) {
8282 //Now , this situation is equivalent to the "restore to new course" one (we
8283 //have a course record and nothing more), so define it as "to new course"
8284 $restore->restoreto = RESTORETO_NEW_COURSE;
8285 } else {
8286 if (!defined('RESTORE_SILENTLY')) {
8287 notify("An error occurred while deleting some of the course contents.");
8288 } else {
8289 $errrostr = "An error occurred while deleting some of the course contents.";
8290 return false;
8294 } else {
8295 if (!defined('RESTORE_SILENTLY')) {
8296 notify("Error opening existing course.");
8297 $status = false;
8298 } else {
8299 $errorstr = "Error opening existing course.";
8300 return false;
8305 //Now create users as needed
8306 if ($status and ($restore->users == 0 or $restore->users == 1)) {
8307 if (!defined('RESTORE_SILENTLY')) {
8308 echo "<li>".get_string("creatingusers")."<br />";
8310 if (!$status = restore_create_users($restore,$xml_file)) {
8311 if (!defined('RESTORE_SILENTLY')) {
8312 notify("Could not restore users.");
8313 } else {
8314 $errorstr = "Could not restore users.";
8315 return false;
8319 //Now print info about the work done
8320 if ($status) {
8321 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
8322 where backup_code = '$restore->backup_unique_code' and
8323 table_name = 'user'");
8324 //We've records
8325 if ($recs) {
8326 $new_count = 0;
8327 $exists_count = 0;
8328 $student_count = 0;
8329 $teacher_count = 0;
8330 $counter = 0;
8331 //Iterate, filling counters
8332 foreach ($recs as $rec) {
8333 //Get full record, using backup_getids
8334 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
8335 if (strpos($record->info,"new") !== false) {
8336 $new_count++;
8338 if (strpos($record->info,"exists") !== false) {
8339 $exists_count++;
8341 if (strpos($record->info,"student") !== false) {
8342 $student_count++;
8343 } else if (strpos($record->info,"teacher") !== false) {
8344 $teacher_count++;
8346 //Do some output
8347 $counter++;
8348 if ($counter % 10 == 0) {
8349 if (!defined('RESTORE_SILENTLY')) {
8350 echo ".";
8351 if ($counter % 200 == 0) {
8352 echo "<br />";
8355 backup_flush(300);
8358 if (!defined('RESTORE_SILENTLY')) {
8359 //Now print information gathered
8360 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
8361 echo "<ul>";
8362 echo "<li>".get_string("students").": ".$student_count.'</li>';
8363 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
8364 echo "</ul>";
8366 } else {
8367 if (!defined('RESTORE_SILENTLY')) {
8368 notify("No users were found!");
8369 } // no need to return false here, it's recoverable.
8373 if (!defined('RESTORE_SILENTLY')) {
8374 echo "</li>";
8379 //Now create groups as needed
8380 if ($status and ($restore->groups == RESTORE_GROUPS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
8381 if (!defined('RESTORE_SILENTLY')) {
8382 echo "<li>".get_string("creatinggroups");
8384 if (!$status = restore_create_groups($restore,$xml_file)) {
8385 if (!defined('RESTORE_SILENTLY')) {
8386 notify("Could not restore groups!");
8387 } else {
8388 $errorstr = "Could not restore groups!";
8389 return false;
8392 if (!defined('RESTORE_SILENTLY')) {
8393 echo '</li>';
8397 //Now create groupings as needed
8398 if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
8399 if (!defined('RESTORE_SILENTLY')) {
8400 echo "<li>".get_string("creatinggroupings");
8402 if (!$status = restore_create_groupings($restore,$xml_file)) {
8403 if (!defined('RESTORE_SILENTLY')) {
8404 notify("Could not restore groupings!");
8405 } else {
8406 $errorstr = "Could not restore groupings!";
8407 return false;
8410 if (!defined('RESTORE_SILENTLY')) {
8411 echo '</li>';
8415 //Now create groupingsgroups as needed
8416 if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) {
8417 if (!defined('RESTORE_SILENTLY')) {
8418 echo "<li>".get_string("creatinggroupingsgroups");
8420 if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
8421 if (!defined('RESTORE_SILENTLY')) {
8422 notify("Could not restore groups in groupings!");
8423 } else {
8424 $errorstr = "Could not restore groups in groupings!";
8425 return false;
8428 if (!defined('RESTORE_SILENTLY')) {
8429 echo '</li>';
8434 //Now create the course_sections and their associated course_modules
8435 //we have to do this after groups and groupings are restored, because we need the new groupings id
8436 if ($status) {
8437 //Into new course
8438 if ($restore->restoreto == RESTORETO_NEW_COURSE) {
8439 if (!defined('RESTORE_SILENTLY')) {
8440 echo "<li>".get_string("creatingsections");
8442 if (!$status = restore_create_sections($restore,$xml_file)) {
8443 if (!defined('RESTORE_SILENTLY')) {
8444 notify("Error creating sections in the existing course.");
8445 } else {
8446 $errorstr = "Error creating sections in the existing course.";
8447 return false;
8450 if (!defined('RESTORE_SILENTLY')) {
8451 echo '</li>';
8453 //Into existing course
8454 } else if ($restore->restoreto != RESTORETO_NEW_COURSE) {
8455 if (!defined('RESTORE_SILENTLY')) {
8456 echo "<li>".get_string("checkingsections");
8458 if (!$status = restore_create_sections($restore,$xml_file)) {
8459 if (!defined('RESTORE_SILENTLY')) {
8460 notify("Error creating sections in the existing course.");
8461 } else {
8462 $errorstr = "Error creating sections in the existing course.";
8463 return false;
8466 if (!defined('RESTORE_SILENTLY')) {
8467 echo '</li>';
8469 //Error
8470 } else {
8471 if (!defined('RESTORE_SILENTLY')) {
8472 notify("Neither a new course or an existing one was specified.");
8473 $status = false;
8474 } else {
8475 $errorstr = "Neither a new course or an existing one was specified.";
8476 return false;
8481 //Now create metacourse info
8482 if ($status and $restore->metacourse) {
8483 //Only to new courses!
8484 if ($restore->restoreto == RESTORETO_NEW_COURSE) {
8485 if (!defined('RESTORE_SILENTLY')) {
8486 echo "<li>".get_string("creatingmetacoursedata");
8488 if (!$status = restore_create_metacourse($restore,$xml_file)) {
8489 if (!defined('RESTORE_SILENTLY')) {
8490 notify("Error creating metacourse in the course.");
8491 } else {
8492 $errorstr = "Error creating metacourse in the course.";
8493 return false;
8496 if (!defined('RESTORE_SILENTLY')) {
8497 echo '</li>';
8503 //Now create categories and questions as needed
8504 if ($status) {
8505 include_once("$CFG->dirroot/question/restorelib.php");
8506 if (!defined('RESTORE_SILENTLY')) {
8507 echo "<li>".get_string("creatingcategoriesandquestions");
8508 echo "<ul>";
8510 if (!$status = restore_create_questions($restore,$xml_file)) {
8511 if (!defined('RESTORE_SILENTLY')) {
8512 notify("Could not restore categories and questions!");
8513 } else {
8514 $errorstr = "Could not restore categories and questions!";
8515 return false;
8518 if (!defined('RESTORE_SILENTLY')) {
8519 echo "</ul></li>";
8523 //Now create user_files as needed
8524 if ($status and ($restore->user_files)) {
8525 if (!defined('RESTORE_SILENTLY')) {
8526 echo "<li>".get_string("copyinguserfiles");
8528 if (!$status = restore_user_files($restore)) {
8529 if (!defined('RESTORE_SILENTLY')) {
8530 notify("Could not restore user files!");
8531 } else {
8532 $errorstr = "Could not restore user files!";
8533 return false;
8536 //If all is ok (and we have a counter)
8537 if ($status and ($status !== true)) {
8538 //Inform about user dirs created from backup
8539 if (!defined('RESTORE_SILENTLY')) {
8540 echo "<ul>";
8541 echo "<li>".get_string("userzones").": ".$status;
8542 echo "</li></ul>";
8545 if (!defined('RESTORE_SILENTLY')) {
8546 echo '</li>';
8550 //Now create course files as needed
8551 if ($status and ($restore->course_files)) {
8552 if (!defined('RESTORE_SILENTLY')) {
8553 echo "<li>".get_string("copyingcoursefiles");
8555 if (!$status = restore_course_files($restore)) {
8556 if (empty($status)) {
8557 notify("Could not restore course files!");
8558 } else {
8559 $errorstr = "Could not restore course files!";
8560 return false;
8563 //If all is ok (and we have a counter)
8564 if ($status and ($status !== true)) {
8565 //Inform about user dirs created from backup
8566 if (!defined('RESTORE_SILENTLY')) {
8567 echo "<ul>";
8568 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
8569 echo "</ul>";
8572 if (!defined('RESTORE_SILENTLY')) {
8573 echo "</li>";
8578 //Now create site files as needed
8579 if ($status and ($restore->site_files)) {
8580 if (!defined('RESTORE_SILENTLY')) {
8581 echo "<li>".get_string('copyingsitefiles');
8583 if (!$status = restore_site_files($restore)) {
8584 if (empty($status)) {
8585 notify("Could not restore site files!");
8586 } else {
8587 $errorstr = "Could not restore site files!";
8588 return false;
8591 //If all is ok (and we have a counter)
8592 if ($status and ($status !== true)) {
8593 //Inform about user dirs created from backup
8594 if (!defined('RESTORE_SILENTLY')) {
8595 echo "<ul>";
8596 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
8597 echo "</ul>";
8600 if (!defined('RESTORE_SILENTLY')) {
8601 echo "</li>";
8605 //Now create messages as needed
8606 if ($status and ($restore->messages)) {
8607 if (!defined('RESTORE_SILENTLY')) {
8608 echo "<li>".get_string("creatingmessagesinfo");
8610 if (!$status = restore_create_messages($restore,$xml_file)) {
8611 if (!defined('RESTORE_SILENTLY')) {
8612 notify("Could not restore messages!");
8613 } else {
8614 $errorstr = "Could not restore messages!";
8615 return false;
8618 if (!defined('RESTORE_SILENTLY')) {
8619 echo "</li>";
8623 //Now create blogs as needed
8624 if ($status and ($restore->blogs)) {
8625 if (!defined('RESTORE_SILENTLY')) {
8626 echo "<li>".get_string("creatingblogsinfo");
8628 if (!$status = restore_create_blogs($restore,$xml_file)) {
8629 if (!defined('RESTORE_SILENTLY')) {
8630 notify("Could not restore blogs!");
8631 } else {
8632 $errorstr = "Could not restore blogs!";
8633 return false;
8636 if (!defined('RESTORE_SILENTLY')) {
8637 echo "</li>";
8641 //Now create scales as needed
8642 if ($status) {
8643 if (!defined('RESTORE_SILENTLY')) {
8644 echo "<li>".get_string("creatingscales");
8646 if (!$status = restore_create_scales($restore,$xml_file)) {
8647 if (!defined('RESTORE_SILENTLY')) {
8648 notify("Could not restore custom scales!");
8649 } else {
8650 $errorstr = "Could not restore custom scales!";
8651 return false;
8654 if (!defined('RESTORE_SILENTLY')) {
8655 echo '</li>';
8659 //Now create events as needed
8660 if ($status) {
8661 if (!defined('RESTORE_SILENTLY')) {
8662 echo "<li>".get_string("creatingevents");
8664 if (!$status = restore_create_events($restore,$xml_file)) {
8665 if (!defined('RESTORE_SILENTLY')) {
8666 notify("Could not restore course events!");
8667 } else {
8668 $errorstr = "Could not restore course events!";
8669 return false;
8672 if (!defined('RESTORE_SILENTLY')) {
8673 echo '</li>';
8677 //Now create course modules as needed
8678 if ($status) {
8679 if (!defined('RESTORE_SILENTLY')) {
8680 echo "<li>".get_string("creatingcoursemodules");
8682 if (!$status = restore_create_modules($restore,$xml_file)) {
8683 if (!defined('RESTORE_SILENTLY')) {
8684 notify("Could not restore modules!");
8685 } else {
8686 $errorstr = "Could not restore modules!";
8687 return false;
8690 if (!defined('RESTORE_SILENTLY')) {
8691 echo '</li>';
8695 //Bring back the course blocks -- do it AFTER the modules!!!
8696 if ($status) {
8697 //If we are deleting and bringing into a course or making a new course, same situation
8698 if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
8699 $restore->restoreto == RESTORETO_EXISTING_DELETING ||
8700 $restore->restoreto == RESTORETO_NEW_COURSE) {
8701 if (!defined('RESTORE_SILENTLY')) {
8702 echo '<li>'.get_string('creatingblocks');
8704 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
8705 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
8706 if (!defined('RESTORE_SILENTLY')) {
8707 notify('Error while creating the course blocks');
8708 } else {
8709 $errorstr = "Error while creating the course blocks";
8710 return false;
8713 if (!defined('RESTORE_SILENTLY')) {
8714 echo '</li>';
8719 if ($status) {
8720 //If we are deleting and bringing into a course or making a new course, same situation
8721 if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
8722 $restore->restoreto == RESTORETO_EXISTING_DELETING ||
8723 $restore->restoreto == RESTORETO_NEW_COURSE) {
8724 if (!defined('RESTORE_SILENTLY')) {
8725 echo '<li>'.get_string('courseformatdata');
8727 if (!$status = restore_set_format_data($restore, $xml_file)) {
8728 $error = "Error while setting the course format data";
8729 if (!defined('RESTORE_SILENTLY')) {
8730 notify($error);
8731 } else {
8732 $errorstr=$error;
8733 return false;
8736 if (!defined('RESTORE_SILENTLY')) {
8737 echo '</li>';
8742 //Now create log entries as needed
8743 if ($status and ($info->backup_logs == 'true' && $restore->logs)) {
8744 if (!defined('RESTORE_SILENTLY')) {
8745 echo "<li>".get_string("creatinglogentries");
8747 if (!$status = restore_create_logs($restore,$xml_file)) {
8748 if (!defined('RESTORE_SILENTLY')) {
8749 notify("Could not restore logs!");
8750 } else {
8751 $errorstr = "Could not restore logs!";
8752 return false;
8755 if (!defined('RESTORE_SILENTLY')) {
8756 echo '</li>';
8760 //Now, if all is OK, adjust the instance field in course_modules !!
8761 //this also calculates the final modinfo information so, after this,
8762 //code needing it can be used (like role_assignments. MDL-13740)
8763 if ($status) {
8764 if (!defined('RESTORE_SILENTLY')) {
8765 echo "<li>".get_string("checkinginstances");
8767 if (!$status = restore_check_instances($restore)) {
8768 if (!defined('RESTORE_SILENTLY')) {
8769 notify("Could not adjust instances in course_modules!");
8770 } else {
8771 $errorstr = "Could not adjust instances in course_modules!";
8772 return false;
8775 if (!defined('RESTORE_SILENTLY')) {
8776 echo '</li>';
8780 //Now, if all is OK, adjust activity events
8781 if ($status) {
8782 if (!defined('RESTORE_SILENTLY')) {
8783 echo "<li>".get_string("refreshingevents");
8785 if (!$status = restore_refresh_events($restore)) {
8786 if (!defined('RESTORE_SILENTLY')) {
8787 notify("Could not refresh events for activities!");
8788 } else {
8789 $errorstr = "Could not refresh events for activities!";
8790 return false;
8793 if (!defined('RESTORE_SILENTLY')) {
8794 echo '</li>';
8798 //Now, if all is OK, adjust inter-activity links
8799 if ($status) {
8800 if (!defined('RESTORE_SILENTLY')) {
8801 echo "<li>".get_string("decodinginternallinks");
8803 if (!$status = restore_decode_content_links($restore)) {
8804 if (!defined('RESTORE_SILENTLY')) {
8805 notify("Could not decode content links!");
8806 } else {
8807 $errorstr = "Could not decode content links!";
8808 return false;
8811 if (!defined('RESTORE_SILENTLY')) {
8812 echo '</li>';
8816 //Now, with backup files prior to version 2005041100,
8817 //convert all the wiki texts in the course to markdown
8818 if ($status && $restore->backup_version < 2005041100) {
8819 if (!defined('RESTORE_SILENTLY')) {
8820 echo "<li>".get_string("convertingwikitomarkdown");
8822 if (!$status = restore_convert_wiki2markdown($restore)) {
8823 if (!defined('RESTORE_SILENTLY')) {
8824 notify("Could not convert wiki texts to markdown!");
8825 } else {
8826 $errorstr = "Could not convert wiki texts to markdown!";
8827 return false;
8830 if (!defined('RESTORE_SILENTLY')) {
8831 echo '</li>';
8835 //Now create gradebook as needed -- AFTER modules and blocks!!!
8836 if ($status) {
8837 if ($restore->backup_version > 2007090500) {
8838 if (!defined('RESTORE_SILENTLY')) {
8839 echo "<li>".get_string("creatinggradebook");
8841 if (!$status = restore_create_gradebook($restore,$xml_file)) {
8842 if (!defined('RESTORE_SILENTLY')) {
8843 notify("Could not restore gradebook!");
8844 } else {
8845 $errorstr = "Could not restore gradebook!";
8846 return false;
8850 if (!defined('RESTORE_SILENTLY')) {
8851 echo '</li>';
8854 } else {
8855 // for moodle versions before 1.9, those grades need to be converted to use the new gradebook
8856 // this code needs to execute *after* the course_modules are sorted out
8857 if (!defined('RESTORE_SILENTLY')) {
8858 echo "<li>".get_string("migratinggrades");
8861 /// force full refresh of grading data before migration == crete all items first
8862 if (!$status = restore_migrate_old_gradebook($restore,$xml_file)) {
8863 if (!defined('RESTORE_SILENTLY')) {
8864 notify("Could not migrate gradebook!");
8865 } else {
8866 $errorstr = "Could not migrade gradebook!";
8867 return false;
8870 if (!defined('RESTORE_SILENTLY')) {
8871 echo '</li>';
8874 /// force full refresh of grading data after all items are created
8875 grade_force_full_regrading($restore->course_id);
8876 grade_grab_course_grades($restore->course_id);
8879 /*******************************************************************************
8880 ************* Restore of Roles and Capabilities happens here ******************
8881 *******************************************************************************/
8882 // try to restore roles even when restore is going to fail - teachers might have
8883 // at least some role assigned - this is not correct though
8884 $status = restore_create_roles($restore, $xml_file) && $status;
8885 $status = restore_roles_settings($restore, $xml_file) && $status;
8887 //Now if all is OK, update:
8888 // - course modinfo field
8889 // - categories table
8890 // - add user as teacher
8891 if ($status) {
8892 if (!defined('RESTORE_SILENTLY')) {
8893 echo "<li>".get_string("checkingcourse");
8895 //categories table
8896 $course = get_record("course","id",$restore->course_id);
8897 fix_course_sortorder();
8898 // Check if the user has course update capability in the newly restored course
8899 // there is no need to load his capabilities again, because restore_roles_settings
8900 // would have loaded it anyway, if there is any assignments.
8901 // fix for MDL-6831
8902 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
8903 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
8904 // fix for MDL-9065, use the new config setting if exists
8905 if ($CFG->creatornewroleid) {
8906 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
8907 } else {
8908 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM))) {
8909 if ($legacyteacher = array_shift($legacyteachers)) {
8910 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
8912 } else {
8913 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
8917 if (!defined('RESTORE_SILENTLY')) {
8918 echo '</li>';
8922 //Cleanup temps (files and db)
8923 if ($status) {
8924 if (!defined('RESTORE_SILENTLY')) {
8925 echo "<li>".get_string("cleaningtempdata");
8927 if (!$status = clean_temp_data ($restore)) {
8928 if (!defined('RESTORE_SILENTLY')) {
8929 notify("Could not clean up temporary data from files and database");
8930 } else {
8931 $errorstr = "Could not clean up temporary data from files and database";
8932 return false;
8935 if (!defined('RESTORE_SILENTLY')) {
8936 echo '</li>';
8940 // this is not a critical check - the result can be ignored
8941 if (restore_close_html($restore)){
8942 if (!defined('RESTORE_SILENTLY')) {
8943 echo '<li>Closing the Restorelog.html file.</li>';
8946 else {
8947 if (!defined('RESTORE_SILENTLY')) {
8948 notify("Could not close the restorelog.html file");
8952 if (!defined('RESTORE_SILENTLY')) {
8953 //End the main ul
8954 echo "</ul>";
8956 //End the main table
8957 echo "</td></tr>";
8958 echo "</table>";
8961 return $status;
8963 //Create, open and write header of the html log file
8964 function restore_open_html($restore,$course_header) {
8966 global $CFG;
8968 $status = true;
8970 //Open file for writing
8971 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
8972 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
8973 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
8975 $status = check_dir_exists($dest_dir,true);
8976 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
8977 //Add the stylesheet
8978 $stylesheetshtml = '';
8979 foreach ($CFG->stylesheets as $stylesheet) {
8980 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
8982 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
8983 $languagehtml = get_html_lang($dir=true);
8985 //Write the header in the new logging file
8986 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
8987 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
8988 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
8989 fwrite ($restorelog_file,"<head>");
8990 fwrite ($restorelog_file,$stylesheetshtml);
8991 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
8992 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
8993 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
8994 $startdate = addslashes($course_header->course_startdate);
8995 $date = usergetdate($startdate);
8996 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
8997 $startdate += $restore->course_startdateoffset;
8998 $date = usergetdate($startdate);
8999 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
9001 if ($status) {
9002 return $restorelog_file;
9003 } else {
9004 return false;
9007 //Create & close footer of the html log file
9008 function restore_close_html($restore) {
9010 global $CFG;
9012 $status = true;
9014 //Open file for writing
9015 //First, check that course_id/backupdata folder exists in CFG->dataroot
9016 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
9017 $status = check_dir_exists($dest_dir, true, true);
9018 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
9019 //Write the footer to close the logging file
9020 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
9021 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
9023 if ($status) {
9024 return $restorelog_file;
9025 } else {
9026 return false;
9030 /********************** Roles and Capabilities Related Functions *******************************/
9032 /* Yu: Note recovering of role assignments/overrides need to take place after
9033 users have been recovered, i.e. after we get their new_id, and after all
9034 roles have been recreated or mapped. Contexts can be created on the fly.
9035 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
9036 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
9037 once roles and users have been restored.
9041 * This function restores all the needed roles for this course
9042 * i.e. roles with an assignment in any of the mods or blocks,
9043 * roles assigned on any user (e.g. parent role) and roles
9044 * assigned at course levle
9045 * This function should check for duplicate roles first
9046 * It isn't now, just overwriting
9048 function restore_create_roles($restore, $xmlfile) {
9049 if (!defined('RESTORE_SILENTLY')) {
9050 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
9052 $info = restore_read_xml_roles($xmlfile);
9054 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
9056 // the following code creates new roles
9057 // but we could use more intelligent detection, and role mapping
9058 // get role mapping info from $restore
9059 $rolemappings = array();
9061 if (!empty($restore->rolesmapping)) {
9062 $rolemappings = $restore->rolesmapping;
9064 // $info->roles will be empty for backups pre 1.7
9065 if (isset($info->roles) && $info->roles) {
9067 foreach ($info->roles as $oldroleid=>$roledata) {
9069 if (empty($restore->rolesmapping)) {
9070 // if this is empty altogether, we came from import or there's no roles used in course at all
9071 // in this case, write the same oldid as this is the same site
9072 // no need to do mapping
9073 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
9074 $oldroleid); // adding a new id
9075 continue; // do not create additonal roles;
9077 // first we check if the roles are in the mappings
9078 // if so, we just do a mapping i.e. update oldids table
9079 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
9080 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
9081 $rolemappings[$oldroleid]); // adding a new id
9083 } else {
9085 // code to make new role name/short name if same role name or shortname exists
9086 $fullname = $roledata->name;
9087 $shortname = $roledata->shortname;
9088 $currentfullname = "";
9089 $currentshortname = "";
9090 $counter = 0;
9092 do {
9093 if ($counter) {
9094 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
9095 $suffixshort = "_".$counter;
9096 } else {
9097 $suffixfull = "";
9098 $suffixshort = "";
9100 $currentfullname = $fullname.$suffixfull;
9101 // Limit the size of shortname - database column accepts <= 100 chars
9102 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
9103 $coursefull = get_record("role","name",addslashes($currentfullname));
9104 $courseshort = get_record("role","shortname",addslashes($currentshortname));
9105 $counter++;
9106 } while ($coursefull || $courseshort);
9108 $roledata->name = $currentfullname;
9109 $roledata->shortname= $currentshortname;
9111 // done finding a unique name
9113 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
9114 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
9115 $newroleid); // adding a new id
9116 foreach ($roledata->capabilities as $capability) {
9118 $roleinfo = new object();
9119 $roleinfo = (object)$capability;
9120 $roleinfo->contextid = $sitecontext->id;
9121 $roleinfo->capability = $capability->name;
9122 $roleinfo->roleid = $newroleid;
9124 insert_record('role_capabilities', $roleinfo);
9127 /// Now, restore role nameincourse (only if the role had nameincourse in backup)
9128 if (!empty($roledata->nameincourse)) {
9129 $newrole = backup_getid($restore->backup_unique_code, 'role', $oldroleid); /// Look for target role
9130 $coursecontext = get_context_instance(CONTEXT_COURSE, $restore->course_id); /// Look for target context
9131 if (!empty($newrole->new_id) && !empty($coursecontext)) {
9132 /// Check the role hasn't any custom name in context
9133 if (!record_exists('role_names', 'roleid', $newrole->new_id, 'contextid', $coursecontext->id)) {
9134 $rolename = new object();
9135 $rolename->roleid = $newrole->new_id;
9136 $rolename->contextid = $coursecontext->id;
9137 $rolename->name = addslashes($roledata->nameincourse);
9139 insert_record('role_names', $rolename);
9145 return true;
9149 * this function restores role assignments and role overrides
9150 * in course/user/block/mod level, it passed through
9151 * the xml file again
9153 function restore_roles_settings($restore, $xmlfile) {
9154 // data pulls from course, mod, user, and blocks
9156 /*******************************************************
9157 * Restoring from course level assignments *
9158 *******************************************************/
9159 if (!defined('RESTORE_SILENTLY')) {
9160 echo "<li>".get_string("creatingcourseroles").'</li>';
9162 $course = restore_read_xml_course_header($xmlfile);
9164 if (!isset($restore->rolesmapping)) {
9165 $isimport = true; // course import from another course, or course with no role assignments
9166 } else {
9167 $isimport = false; // course restore with role assignments
9170 if (!empty($course->roleassignments) && !$isimport) {
9171 $courseassignments = $course->roleassignments;
9173 foreach ($courseassignments as $oldroleid => $courseassignment) {
9174 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
9177 /*****************************************************
9178 * Restoring from course level overrides *
9179 *****************************************************/
9181 if (!empty($course->roleoverrides) && !$isimport) {
9182 $courseoverrides = $course->roleoverrides;
9183 foreach ($courseoverrides as $oldroleid => $courseoverride) {
9184 // if not importing into exiting course, or creating new role, we are ok
9185 // local course overrides to be respected (i.e. restored course overrides ignored)
9186 if (($restore->restoreto != RESTORETO_CURRENT_ADDING && $restore->restoreto != RESTORETO_EXISTING_ADDING) || empty($restore->rolesmapping[$oldroleid])) {
9187 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
9192 /*******************************************************
9193 * Restoring role assignments/overrdies *
9194 * from module level assignments *
9195 *******************************************************/
9197 if (!defined('RESTORE_SILENTLY')) {
9198 echo "<li>".get_string("creatingmodroles").'</li>';
9200 $sections = restore_read_xml_sections($xmlfile);
9201 $secs = $sections->sections;
9203 foreach ($secs as $section) {
9204 if (isset($section->mods)) {
9205 foreach ($section->mods as $modid=>$mod) {
9206 if (isset($mod->roleassignments) && !$isimport) {
9207 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
9208 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
9211 // role overrides always applies, in import or backup/restore
9212 if (isset($mod->roleoverrides)) {
9213 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
9214 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
9221 /*************************************************
9222 * Restoring assignments from blocks level *
9223 * role assignments/overrides *
9224 *************************************************/
9226 if ($restore->restoreto != RESTORETO_CURRENT_ADDING && $restore->restoreto != RESTORETO_EXISTING_ADDING) { // skip altogether if restoring to exisitng course by adding
9227 if (!defined('RESTORE_SILENTLY')) {
9228 echo "<li>".get_string("creatingblocksroles").'</li>';
9230 $blocks = restore_read_xml_blocks($restore, $xmlfile);
9231 if (isset($blocks->instances)) {
9232 foreach ($blocks->instances as $instance) {
9233 if (isset($instance->roleassignments) && !$isimport) {
9234 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
9235 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
9239 // likewise block overrides should always be restored like mods
9240 if (isset($instance->roleoverrides)) {
9241 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
9242 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
9249 /************************************************
9250 * Restoring assignments from userid level *
9251 * role assignments/overrides *
9252 ************************************************/
9253 if (!defined('RESTORE_SILENTLY')) {
9254 echo "<li>".get_string("creatinguserroles").'</li>';
9256 $info = restore_read_xml_users($restore, $xmlfile);
9257 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
9258 //For each user, take its info from backup_ids
9259 foreach ($info->users as $userid) {
9260 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
9261 if (isset($rec->info->roleassignments)) {
9262 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
9263 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
9266 if (isset($rec->info->roleoverrides)) {
9267 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
9268 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
9274 return true;
9277 // auxillary function to write role assignments read from xml to db
9278 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
9280 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
9282 foreach ($assignments as $assignment) {
9284 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
9285 //Oh dear, $olduser... can be an object, $obj->string or bool!
9286 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
9287 continue;
9289 $assignment->userid = $olduser->new_id; // new userid here
9290 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
9291 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
9292 $assignment->roleid = $role->new_id; // restored new role id
9294 // hack to make the correct contextid for course level imports
9295 if ($contextlevel == CONTEXT_COURSE) {
9296 $oldinstance->new_id = $restore->course_id;
9297 } else {
9298 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
9301 // new instance id not found (not restored module/block/user)... skip any assignment
9302 if (!$oldinstance || empty($oldinstance->new_id)) {
9303 continue;
9306 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
9307 $assignment->contextid = $newcontext->id; // new context id
9308 // might already have same assignment
9309 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol, $assignment->timemodified);
9314 // auxillary function to write role assignments read from xml to db
9315 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
9317 // it is possible to have an override not relevant to this course context.
9318 // should be ignored(?)
9319 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
9320 return null;
9323 foreach ($overrides as $override) {
9324 $override->capability = $override->name;
9325 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
9326 $override->modifierid = !empty($oldmodifier->new_id)?$oldmodifier->new_id:0; // new modifier id here
9327 $override->roleid = $role->new_id; // restored new role id
9329 // hack to make the correct contextid for course level imports
9330 if ($contextlevel == CONTEXT_COURSE) {
9331 $oldinstance->new_id = $restore->course_id;
9332 } else {
9333 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
9336 // new instance id not found (not restored module/block/user)... skip any override
9337 if (!$oldinstance || empty($oldinstance->new_id)) {
9338 continue;
9341 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
9342 $override->contextid = $newcontext->id; // new context id
9343 // use assign capability instead so we can add context to context_rel
9344 assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
9349 * true or false function to see if user can roll dates on restore (any course is enough)
9350 * @return bool
9352 function restore_user_can_roll_dates() {
9353 global $USER;
9354 // if user has moodle/restore:rolldates capability at system or any course cat return true
9356 if (has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_SYSTEM))) {
9357 return true;
9360 // Non-cached - get accessinfo
9361 if (isset($USER->access)) {
9362 $accessinfo = $USER->access;
9363 } else {
9364 $accessinfo = get_user_access_sitewide($USER->id);
9366 $courses = get_user_courses_bycap($USER->id, 'moodle/restore:rolldates', $accessinfo, true);
9367 return !empty($courses);
9370 //write activity date changes to the html log file, and update date values in the the xml array
9371 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
9373 global $CFG;
9374 $openlog = false;
9376 // loop through time fields in $TAGS
9377 foreach ($TAGS as $TAG) {
9379 // check $TAG has a sensible value
9380 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
9382 if ($openlog==false) {
9383 $openlog = true; // only come through here once
9385 // open file for writing
9386 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
9387 check_dir_exists($course_dir, true);
9388 $restorelog = fopen("$course_dir/restorelog.html", "a");
9390 // start output for this record
9391 $msg = new stdClass();
9392 $msg->recordtype = $recordtype;
9393 $msg->recordname = $xml[$NAMETAG][0]['#'];
9394 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
9397 // write old date to $restorelog
9398 $value = $xml[$TAG][0]['#'];
9399 $date = usergetdate($value);
9401 $msg = new stdClass();
9402 $msg->TAG = $TAG;
9403 $msg->weekday = $date['weekday'];
9404 $msg->mday = $date['mday'];
9405 $msg->month = $date['month'];
9406 $msg->year = $date['year'];
9407 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
9409 // write new date to $restorelog
9410 $value += $restore->course_startdateoffset;
9411 $date = usergetdate($value);
9413 $msg = new stdClass();
9414 $msg->TAG = $TAG;
9415 $msg->weekday = $date['weekday'];
9416 $msg->mday = $date['mday'];
9417 $msg->month = $date['month'];
9418 $msg->year = $date['year'];
9419 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
9421 // update $value in $xml tree for calling module
9422 $xml[$TAG][0]['#'] = "$value";
9425 // close the restore log, if it was opened
9426 if ($openlog) {
9427 fclose($restorelog);