"MDL-14932, accessibility imporvement, see tracker"
[moodle-linuxchix.git] / backup / restorelib.php
blob6377f2f09a8957ac838d5cdde735c5fef8bd602f
1 <?php //$Id$
2 //Functions used in restore
4 require_once($CFG->libdir.'/gradelib.php');
6 /**
7 * Group backup/restore constants, 0.
8 */
9 define('RESTORE_GROUPS_NONE', 0);
11 /**
12 * Group backup/restore constants, 1.
14 define('RESTORE_GROUPS_ONLY', 1);
16 /**
17 * Group backup/restore constants, 2.
19 define('RESTORE_GROUPINGS_ONLY', 2);
21 /**
22 * Group backup/restore constants, course/all.
24 define('RESTORE_GROUPS_GROUPINGS', 3);
26 //This function unzips a zip file in the same directory that it is
27 //It automatically uses pclzip or command line unzip
28 function restore_unzip ($file) {
30 return unzip_file($file, '', false);
34 //This function checks if moodle.xml seems to be a valid xml file
35 //(exists, has an xml header and a course main tag
36 function restore_check_moodle_file ($file) {
38 $status = true;
40 //Check if it exists
41 if ($status = is_file($file)) {
42 //Open it and read the first 200 bytes (chars)
43 $handle = fopen ($file, "r");
44 $first_chars = fread($handle,200);
45 $status = fclose ($handle);
46 //Chek if it has the requires strings
47 if ($status) {
48 $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
49 if ($status !== false) {
50 $status = strpos($first_chars,"<MOODLE_BACKUP>");
55 return $status;
58 //This function iterates over all modules in backup file, searching for a
59 //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
60 function restore_refresh_events($restore) {
62 global $CFG;
63 $status = true;
65 //Take all modules in backup
66 $modules = $restore->mods;
67 //Iterate
68 foreach($modules as $name => $module) {
69 //Only if the module is being restored
70 if (isset($module->restore) && $module->restore == 1) {
71 //Include module library
72 include_once("$CFG->dirroot/mod/$name/lib.php");
73 //If module_refresh_events exists
74 $function_name = $name."_refresh_events";
75 if (function_exists($function_name)) {
76 $status = $function_name($restore->course_id);
80 return $status;
83 //This function makes all the necessary calls to xxxx_decode_content_links_caller()
84 //function in each module, passing them the desired contents to be decoded
85 //from backup format to destination site/course in order to mantain inter-activities
86 //working in the backup/restore process
87 function restore_decode_content_links($restore) {
88 global $CFG;
90 $status = true;
92 if (!defined('RESTORE_SILENTLY')) {
93 echo "<ul>";
96 // Restore links in modules.
97 foreach ($restore->mods as $name => $info) {
98 //If the module is being restored
99 if (isset($info->restore) && $info->restore == 1) {
100 //Check if the xxxx_decode_content_links_caller exists
101 include_once("$CFG->dirroot/mod/$name/restorelib.php");
102 $function_name = $name."_decode_content_links_caller";
103 if (function_exists($function_name)) {
104 if (!defined('RESTORE_SILENTLY')) {
105 echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
107 $status = $function_name($restore) && $status;
108 if (!defined('RESTORE_SILENTLY')) {
109 echo '</li>';
115 // Process all html text also in blocks too
116 if (!defined('RESTORE_SILENTLY')) {
117 echo '<li>'.get_string ('from').' '.get_string('blocks');
120 if ($blocks = get_records('block', 'visible', 1)) {
121 foreach ($blocks as $block) {
122 if ($blockobject = block_instance($block->name)) {
123 $blockobject->decode_content_links_caller($restore);
128 if (!defined('RESTORE_SILENTLY')) {
129 echo '</li>';
132 // Restore links in questions.
133 require_once("$CFG->dirroot/question/restorelib.php");
134 if (!defined('RESTORE_SILENTLY')) {
135 echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
137 $status = question_decode_content_links_caller($restore) && $status;
138 if (!defined('RESTORE_SILENTLY')) {
139 echo '</li>';
142 if (!defined('RESTORE_SILENTLY')) {
143 echo "</ul>";
146 return $status;
149 //This function is called from all xxxx_decode_content_links_caller(),
150 //its task is to ask all modules (maybe other linkable objects) to restore
151 //links to them.
152 function restore_decode_content_links_worker($content,$restore) {
153 foreach($restore->mods as $name => $info) {
154 $function_name = $name."_decode_content_links";
155 if (function_exists($function_name)) {
156 $content = $function_name($content,$restore);
160 // For each block, call its encode_content_links method
161 static $blockobjects = null;
162 if (!isset($blockobjects)) {
163 $blockobjects = array();
164 if ($blocks = get_records('block', 'visible', 1)) {
165 foreach ($blocks as $block) {
166 if ($blockobject = block_instance($block->name)) {
167 $blockobjects[] = $blockobject;
173 foreach ($blockobjects as $blockobject) {
174 $content = $blockobject->decode_content_links($content,$restore);
177 return $content;
180 //This function converts all the wiki texts in the restored course
181 //to the Markdown format. Used only for backup files prior 2005041100.
182 //It calls every module xxxx_convert_wiki2markdown function
183 function restore_convert_wiki2markdown($restore) {
185 $status = true;
187 if (!defined('RESTORE_SILENTLY')) {
188 echo "<ul>";
190 foreach ($restore->mods as $name => $info) {
191 //If the module is being restored
192 if ($info->restore == 1) {
193 //Check if the xxxx_restore_wiki2markdown exists
194 $function_name = $name."_restore_wiki2markdown";
195 if (function_exists($function_name)) {
196 $status = $function_name($restore);
197 if (!defined('RESTORE_SILENTLY')) {
198 echo "<li>".get_string("modulenameplural",$name);
199 echo '</li>';
204 if (!defined('RESTORE_SILENTLY')) {
205 echo "</ul>";
207 return $status;
210 //This function receives a wiki text in the restore process and
211 //return it with every link to modules " modulename:moduleid"
212 //converted if possible. See the space before modulename!!
213 function restore_decode_wiki_content($content,$restore) {
215 global $CFG;
217 $result = $content;
219 $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
220 //We look for it
221 preg_match_all($searchstring,$content,$foundset);
222 //If found, then we are going to look for its new id (in backup tables)
223 if ($foundset[0]) {
224 //print_object($foundset); //Debug
225 //Iterate over foundset[2]. They are the old_ids
226 foreach($foundset[2] as $old_id) {
227 //We get the needed variables here (course id)
228 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
229 //Personalize the searchstring
230 $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
231 //If it is a link to this course, update the link to its new location
232 if($rec->new_id) {
233 //Now replace it
234 $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
235 } else {
236 //It's a foreign link so redirect it to its original URL
237 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
241 return $result;
245 //This function read the xml file and store it data from the info zone in an object
246 function restore_read_xml_info ($xml_file) {
248 //We call the main read_xml function, with todo = INFO
249 $info = restore_read_xml ($xml_file,"INFO",false);
251 return $info;
254 //This function read the xml file and store it data from the course header zone in an object
255 function restore_read_xml_course_header ($xml_file) {
257 //We call the main read_xml function, with todo = COURSE_HEADER
258 $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
260 return $info;
263 //This function read the xml file and store its data from the blocks in a object
264 function restore_read_xml_blocks ($restore, $xml_file) {
266 //We call the main read_xml function, with todo = BLOCKS
267 $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
269 return $info;
272 //This function read the xml file and store its data from the sections in a object
273 function restore_read_xml_sections ($xml_file) {
275 //We call the main read_xml function, with todo = SECTIONS
276 $info = restore_read_xml ($xml_file,"SECTIONS",false);
278 return $info;
281 //This function read the xml file and store its data from the course format in an object
282 function restore_read_xml_formatdata ($xml_file) {
284 //We call the main read_xml function, with todo = FORMATDATA
285 $info = restore_read_xml ($xml_file,'FORMATDATA',false);
287 return $info;
290 //This function read the xml file and store its data from the metacourse in a object
291 function restore_read_xml_metacourse ($xml_file) {
293 //We call the main read_xml function, with todo = METACOURSE
294 $info = restore_read_xml ($xml_file,"METACOURSE",false);
296 return $info;
299 //This function read the xml file and store its data from the gradebook in a object
300 function restore_read_xml_gradebook ($restore, $xml_file) {
302 //We call the main read_xml function, with todo = GRADEBOOK
303 $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
305 return $info;
308 //This function read the xml file and store its data from the users in
309 //backup_ids->info db (and user's id in $info)
310 function restore_read_xml_users ($restore,$xml_file) {
312 //We call the main read_xml function, with todo = USERS
313 $info = restore_read_xml ($xml_file,"USERS",$restore);
315 return $info;
318 //This function read the xml file and store its data from the messages in
319 //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
320 function restore_read_xml_messages ($restore,$xml_file) {
322 //We call the main read_xml function, with todo = MESSAGES
323 $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
325 return $info;
328 //This function read the xml file and store its data from the blogs in
329 //backup_ids->blog and backup_ids->blog_tag and db (and their counters in info)
330 function restore_read_xml_blogs ($restore,$xml_file) {
332 //We call the main read_xml function, with todo = BLOGS
333 $info = restore_read_xml ($xml_file,"BLOGS",$restore);
335 return $info;
339 //This function read the xml file and store its data from the questions in
340 //backup_ids->info db (and category's id in $info)
341 function restore_read_xml_questions ($restore,$xml_file) {
343 //We call the main read_xml function, with todo = QUESTIONS
344 $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
346 return $info;
349 //This function read the xml file and store its data from the scales in
350 //backup_ids->info db (and scale's id in $info)
351 function restore_read_xml_scales ($restore,$xml_file) {
353 //We call the main read_xml function, with todo = SCALES
354 $info = restore_read_xml ($xml_file,"SCALES",$restore);
356 return $info;
359 //This function read the xml file and store its data from the groups in
360 //backup_ids->info db (and group's id in $info)
361 function restore_read_xml_groups ($restore,$xml_file) {
363 //We call the main read_xml function, with todo = GROUPS
364 $info = restore_read_xml ($xml_file,"GROUPS",$restore);
366 return $info;
369 //This function read the xml file and store its data from the groupings in
370 //backup_ids->info db (and grouping's id in $info)
371 function restore_read_xml_groupings ($restore,$xml_file) {
373 //We call the main read_xml function, with todo = GROUPINGS
374 $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
376 return $info;
379 //This function read the xml file and store its data from the groupings in
380 //backup_ids->info db (and grouping's id in $info)
381 function restore_read_xml_groupings_groups ($restore,$xml_file) {
383 //We call the main read_xml function, with todo = GROUPINGS
384 $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
386 return $info;
389 //This function read the xml file and store its data from the events (course) in
390 //backup_ids->info db (and event's id in $info)
391 function restore_read_xml_events ($restore,$xml_file) {
393 //We call the main read_xml function, with todo = EVENTS
394 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
396 return $info;
399 //This function read the xml file and store its data from the modules in
400 //backup_ids->info
401 function restore_read_xml_modules ($restore,$xml_file) {
403 //We call the main read_xml function, with todo = MODULES
404 $info = restore_read_xml ($xml_file,"MODULES",$restore);
406 return $info;
409 //This function read the xml file and store its data from the logs in
410 //backup_ids->info
411 function restore_read_xml_logs ($restore,$xml_file) {
413 //We call the main read_xml function, with todo = LOGS
414 $info = restore_read_xml ($xml_file,"LOGS",$restore);
416 return $info;
419 function restore_read_xml_roles ($xml_file) {
420 //We call the main read_xml function, with todo = ROLES
421 $info = restore_read_xml ($xml_file,"ROLES",false);
423 return $info;
426 //This function prints the contents from the info parammeter passed
427 function restore_print_info ($info) {
429 global $CFG;
431 $status = true;
432 if ($info) {
433 $table = new object();
434 //This is tha align to every ingo table
435 $table->align = array ("right","left");
436 //This is the nowrap clause
437 $table->wrap = array ("","nowrap");
438 //The width
439 $table->width = "70%";
440 //Put interesting info in table
441 //The backup original name
442 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
443 $tab[0][1] = $info->backup_name;
444 //The moodle version
445 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
446 $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
447 //The backup version
448 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
449 $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
450 //The backup date
451 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
452 $tab[3][1] = userdate($info->backup_date);
453 //Print title
454 print_heading(get_string("backup").":");
455 $table->data = $tab;
456 //Print backup general info
457 print_table($table);
459 if ($info->backup_backup_version <= 2005070500) {
460 notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
463 //Now backup contents in another table
464 $tab = array();
465 //First mods info
466 $mods = $info->mods;
467 $elem = 0;
468 foreach ($mods as $key => $mod) {
469 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
470 if ($mod->backup == "false") {
471 $tab[$elem][1] = get_string("notincluded");
472 } else {
473 if ($mod->userinfo == "true") {
474 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
475 } else {
476 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
478 if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
479 foreach ($mod->instances as $instance) {
480 if ($instance->backup) {
481 $elem++;
482 $tab[$elem][0] = $instance->name;
483 if ($instance->userinfo == 'true') {
484 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
485 } else {
486 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
492 $elem++;
494 //Metacourse info
495 $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
496 if ($info->backup_metacourse == "true") {
497 $tab[$elem][1] = get_string("yes");
498 } else {
499 $tab[$elem][1] = get_string("no");
501 $elem++;
502 //Users info
503 $tab[$elem][0] = "<b>".get_string("users").":</b>";
504 $tab[$elem][1] = get_string($info->backup_users);
505 $elem++;
506 //Logs info
507 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
508 if ($info->backup_logs == "true") {
509 $tab[$elem][1] = get_string("yes");
510 } else {
511 $tab[$elem][1] = get_string("no");
513 $elem++;
514 //User Files info
515 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
516 if ($info->backup_user_files == "true") {
517 $tab[$elem][1] = get_string("yes");
518 } else {
519 $tab[$elem][1] = get_string("no");
521 $elem++;
522 //Course Files info
523 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
524 if ($info->backup_course_files == "true") {
525 $tab[$elem][1] = get_string("yes");
526 } else {
527 $tab[$elem][1] = get_string("no");
529 $elem++;
530 //site Files info
531 $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
532 if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
533 $tab[$elem][1] = get_string("yes");
534 } else {
535 $tab[$elem][1] = get_string("no");
537 $elem++;
538 //gradebook history info
539 $tab[$elem][0] = "<b>".get_string('gradebookhistories', 'grades').":</b>";
540 if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
541 $tab[$elem][1] = get_string("yes");
542 } else {
543 $tab[$elem][1] = get_string("no");
545 $elem++;
546 //Messages info (only showed if present)
547 if ($info->backup_messages == 'true') {
548 $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
549 $tab[$elem][1] = get_string('yes');
550 $elem++;
551 } else {
552 //Do nothing
554 $elem++;
555 //Blogs info (only showed if present)
556 if (isset($info->backup_blogs) && $info->backup_blogs == 'true') {
557 $tab[$elem][0] = "<b>".get_string('blogs','blog').":</b>";
558 $tab[$elem][1] = get_string('yes');
559 $elem++;
560 } else {
561 //Do nothing
563 $table->data = $tab;
564 //Print title
565 print_heading(get_string("backupdetails").":");
566 //Print backup general info
567 print_table($table);
568 } else {
569 $status = false;
572 return $status;
575 //This function prints the contents from the course_header parammeter passed
576 function restore_print_course_header ($course_header) {
578 $status = true;
579 if ($course_header) {
580 $table = new object();
581 //This is tha align to every ingo table
582 $table->align = array ("right","left");
583 //The width
584 $table->width = "70%";
585 //Put interesting course header in table
586 //The course name
587 $tab[0][0] = "<b>".get_string("name").":</b>";
588 $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
589 //The course summary
590 $tab[1][0] = "<b>".get_string("summary").":</b>";
591 $tab[1][1] = $course_header->course_summary;
592 $table->data = $tab;
593 //Print title
594 print_heading(get_string("course").":");
595 //Print backup course header info
596 print_table($table);
597 } else {
598 $status = false;
600 return $status;
603 //This function create a new course record.
604 //When finished, course_header contains the id of the new course
605 function restore_create_new_course($restore,&$course_header) {
607 global $CFG;
609 $status = true;
611 $fullname = $course_header->course_fullname;
612 $shortname = $course_header->course_shortname;
613 $currentfullname = "";
614 $currentshortname = "";
615 $counter = 0;
616 //Iteratere while the name exists
617 do {
618 if ($counter) {
619 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
620 $suffixshort = "_".$counter;
621 } else {
622 $suffixfull = "";
623 $suffixshort = "";
625 $currentfullname = $fullname.$suffixfull;
626 // Limit the size of shortname - database column accepts <= 100 chars
627 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
628 $coursefull = get_record("course","fullname",addslashes($currentfullname));
629 $courseshort = get_record("course","shortname",addslashes($currentshortname));
630 $counter++;
631 } while ($coursefull || $courseshort);
633 //New name = currentname
634 $course_header->course_fullname = $currentfullname;
635 $course_header->course_shortname = $currentshortname;
637 // first try to get it from restore
638 if ($restore->restore_restorecatto) {
639 $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
642 // else we try to get it from the xml file
643 //Now calculate the category
644 if (!$category) {
645 $category = get_record("course_categories","id",$course_header->category->id,
646 "name",addslashes($course_header->category->name));
649 //If no exists, try by name only
650 if (!$category) {
651 $category = get_record("course_categories","name",addslashes($course_header->category->name));
654 //If no exists, get category id 1
655 if (!$category) {
656 $category = get_record("course_categories","id","1");
659 //If category 1 doesn'exists, lets create the course category (get it from backup file)
660 if (!$category) {
661 $ins_category = new object();
662 $ins_category->name = addslashes($course_header->category->name);
663 $ins_category->parent = 0;
664 $ins_category->sortorder = 0;
665 $ins_category->coursecount = 0;
666 $ins_category->visible = 0; //To avoid interferences with the rest of the site
667 $ins_category->timemodified = time();
668 $newid = insert_record("course_categories",$ins_category);
669 $category->id = $newid;
670 $category->name = $course_header->category->name;
672 //If exists, put new category id
673 if ($category) {
674 $course_header->category->id = $category->id;
675 $course_header->category->name = $category->name;
676 //Error, cannot locate category
677 } else {
678 $course_header->category->id = 0;
679 $course_header->category->name = get_string("unknowncategory");
680 $status = false;
683 //Create the course_object
684 if ($status) {
685 $course = new object();
686 $course->category = addslashes($course_header->category->id);
687 $course->password = addslashes($course_header->course_password);
688 $course->fullname = addslashes($course_header->course_fullname);
689 $course->shortname = addslashes($course_header->course_shortname);
690 $course->idnumber = addslashes($course_header->course_idnumber);
691 $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
692 $course->summary = backup_todb($course_header->course_summary);
693 $course->format = addslashes($course_header->course_format);
694 $course->showgrades = addslashes($course_header->course_showgrades);
695 $course->newsitems = addslashes($course_header->course_newsitems);
696 $course->teacher = addslashes($course_header->course_teacher);
697 $course->teachers = addslashes($course_header->course_teachers);
698 $course->student = addslashes($course_header->course_student);
699 $course->students = addslashes($course_header->course_students);
700 $course->guest = addslashes($course_header->course_guest);
701 $course->startdate = addslashes($course_header->course_startdate);
702 $course->startdate += $restore->course_startdateoffset;
703 $course->numsections = addslashes($course_header->course_numsections);
704 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
705 $course->maxbytes = addslashes($course_header->course_maxbytes);
706 $course->showreports = addslashes($course_header->course_showreports);
707 if (isset($course_header->course_groupmode)) {
708 $course->groupmode = addslashes($course_header->course_groupmode);
710 if (isset($course_header->course_groupmodeforce)) {
711 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
713 if (isset($course_header->course_defaultgroupingid)) {
714 //keep the original now - convert after groupings restored
715 $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
717 $course->lang = addslashes($course_header->course_lang);
718 $course->theme = addslashes($course_header->course_theme);
719 $course->cost = addslashes($course_header->course_cost);
720 $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
721 $course->marker = addslashes($course_header->course_marker);
722 $course->visible = addslashes($course_header->course_visible);
723 $course->hiddensections = addslashes($course_header->course_hiddensections);
724 $course->timecreated = addslashes($course_header->course_timecreated);
725 $course->timemodified = addslashes($course_header->course_timemodified);
726 $course->metacourse = addslashes($course_header->course_metacourse);
727 $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
728 $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
729 $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
730 $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
731 $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
732 if ($course->enrolstartdate) { //Roll course dates
733 $course->enrolstartdate += $restore->course_startdateoffset;
735 $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
736 if ($course->enrolenddate) { //Roll course dates
737 $course->enrolenddate += $restore->course_startdateoffset;
739 $course->enrolperiod = addslashes($course_header->course_enrolperiod);
740 //Calculate sortorder field
741 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
742 FROM ' . $CFG->prefix . 'course
743 WHERE category=' . $course->category);
744 if (!empty($sortmax->max)) {
745 $course->sortorder = $sortmax->max + 1;
746 unset($sortmax);
747 } else {
748 $course->sortorder = 100;
751 //Now, recode some languages (Moodle 1.5)
752 if ($course->lang == 'ma_nt') {
753 $course->lang = 'mi_nt';
756 //Disable course->metacourse if avoided in restore config
757 if (!$restore->metacourse) {
758 $course->metacourse = 0;
761 //Check if the theme exists in destination server
762 $themes = get_list_of_themes();
763 if (!in_array($course->theme, $themes)) {
764 $course->theme = '';
767 //Now insert the record
768 $newid = insert_record("course",$course);
769 if ($newid) {
770 //save old and new course id
771 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
772 //Replace old course_id in course_header
773 $course_header->course_id = $newid;
774 } else {
775 $status = false;
779 return $status;
784 //This function creates all the block stuff when restoring courses
785 //It calls selectively to restore_create_block_instances() for 1.5
786 //and above backups. Upwards compatible with old blocks.
787 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
788 global $CFG;
789 $status = true;
791 delete_records('block_instance', 'pageid', $restore->course_id, 'pagetype', PAGE_COURSE_VIEW);
792 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
793 if (empty($blockinfo)) {
794 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
795 $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
796 blocks_repopulate_page($newpage);
797 } else if (!empty($CFG->showblocksonmodpages)) {
798 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
799 $blockrecords = get_records_select('block', '', '', 'name, id');
800 $temp_blocks_l = array();
801 $temp_blocks_r = array();
802 @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
803 $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
804 foreach($temp_blocks as $blockposition => $blocks) {
805 $blockweight = 0;
806 foreach($blocks as $blockname) {
807 if(!isset($blockrecords[$blockname])) {
808 // We don't know anything about this block!
809 continue;
811 $blockinstance = new stdClass;
812 // Remove any - prefix before doing the name-to-id mapping
813 if(substr($blockname, 0, 1) == '-') {
814 $blockname = substr($blockname, 1);
815 $blockinstance->visible = 0;
816 } else {
817 $blockinstance->visible = 1;
819 $blockinstance->blockid = $blockrecords[$blockname]->id;
820 $blockinstance->pageid = $restore->course_id;
821 $blockinstance->pagetype = PAGE_COURSE_VIEW;
822 $blockinstance->position = $blockposition;
823 $blockinstance->weight = $blockweight;
824 if(!$status = insert_record('block_instance', $blockinstance)) {
825 $status = false;
827 ++$blockweight;
831 } else if($backup_block_format == 'instances') {
832 $status = restore_create_block_instances($restore,$xml_file);
835 return $status;
839 //This function creates all the block_instances from xml when restoring in a
840 //new course
841 function restore_create_block_instances($restore,$xml_file) {
842 global $CFG;
843 $status = true;
845 //Check it exists
846 if (!file_exists($xml_file)) {
847 $status = false;
849 //Get info from xml
850 if ($status) {
851 $info = restore_read_xml_blocks($restore,$xml_file);
854 if(empty($info->instances)) {
855 return $status;
858 // First of all, iterate over the blocks to see which distinct pages we have
859 // in our hands and arrange the blocks accordingly.
860 $pageinstances = array();
861 foreach($info->instances as $instance) {
863 //pagetype and pageid black magic, we have to handle the case of blocks for the
864 //course, blocks from other pages in that course etc etc etc.
866 if($instance->pagetype == PAGE_COURSE_VIEW) {
867 // This one's easy...
868 $instance->pageid = $restore->course_id;
870 } else if (!empty($CFG->showblocksonmodpages)) {
871 $parts = explode('-', $instance->pagetype);
872 if($parts[0] == 'mod') {
873 if(!$restore->mods[$parts[1]]->restore) {
874 continue;
876 $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
878 if (empty($getid->new_id)) {
879 // Failed, perhaps the module was not included in the restore MDL-13554
880 continue;
882 $instance->pageid = $getid->new_id;
884 else {
885 // Not invented here ;-)
886 continue;
889 } else {
890 // do not restore activity blocks if disabled
891 continue;
894 if(!isset($pageinstances[$instance->pagetype])) {
895 $pageinstances[$instance->pagetype] = array();
897 if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
898 $pageinstances[$instance->pagetype][$instance->pageid] = array();
901 $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
904 $blocks = get_records_select('block', 'visible = 1', '', 'name, id, multiple');
906 // For each type of page we have restored
907 foreach($pageinstances as $thistypeinstances) {
909 // For each page id of that type
910 foreach($thistypeinstances as $thisidinstances) {
912 $addedblocks = array();
913 $maxweights = array();
915 // For each block instance in that page
916 foreach($thisidinstances as $instance) {
918 if(!isset($blocks[$instance->name])) {
919 //We are trying to restore a block we don't have...
920 continue;
923 //If we have already added this block once and multiples aren't allowed, disregard it
924 if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
925 continue;
928 //If its the first block we add to a new position, start weight counter equal to 0.
929 if(empty($maxweights[$instance->position])) {
930 $maxweights[$instance->position] = 0;
933 //If the instance weight is greater than the weight counter (we skipped some earlier
934 //blocks most probably), bring it back in line.
935 if($instance->weight > $maxweights[$instance->position]) {
936 $instance->weight = $maxweights[$instance->position];
939 //Add this instance
940 $instance->blockid = $blocks[$instance->name]->id;
942 // This will only be set if we come from 1.7 and above backups
943 // Also, must do this before insert (insert_record unsets id)
944 if (!empty($instance->id)) {
945 $oldid = $instance->id;
946 } else {
947 $oldid = 0;
950 if ($instance->id = insert_record('block_instance', $instance)) {
951 // Create block instance
952 if (!$blockobj = block_instance($instance->name, $instance)) {
953 $status = false;
954 break;
956 // Run the block restore if needed
957 if ($blockobj->backuprestore_instancedata_used()) {
958 // Get restore information
959 $data = backup_getid($restore->backup_unique_code,'block_instance',$oldid);
960 $data->new_id = $instance->id; // For completeness
961 if (!$blockobj->instance_restore($restore, $data)) {
962 $status = false;
963 break;
966 // Save oldid after block restore process because info will be over-written with blank string
967 if ($oldid) {
968 backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
971 } else {
972 $status = false;
973 break;
976 //Get an object for the block and tell it it's been restored so it can update dates
977 //etc. if necessary
978 if ($blockobj = block_instance($instance->name,$instance)) {
979 $blockobj->after_restore($restore);
982 //Now we can increment the weight counter
983 ++$maxweights[$instance->position];
985 //Keep track of block types we have already added
986 $addedblocks[$instance->name] = true;
992 return $status;
995 //This function creates all the course_sections and course_modules from xml
996 //when restoring in a new course or simply checks sections and create records
997 //in backup_ids when restoring in a existing course
998 function restore_create_sections(&$restore, $xml_file) {
1000 global $CFG,$db;
1002 $status = true;
1003 //Check it exists
1004 if (!file_exists($xml_file)) {
1005 $status = false;
1007 //Get info from xml
1008 if ($status) {
1009 $info = restore_read_xml_sections($xml_file);
1011 //Put the info in the DB, recoding ids and saving the in backup tables
1013 $sequence = "";
1015 if ($info) {
1016 //For each, section, save it to db
1017 foreach ($info->sections as $key => $sect) {
1018 $sequence = "";
1019 $section = new object();
1020 $section->course = $restore->course_id;
1021 $section->section = $sect->number;
1022 $section->summary = backup_todb($sect->summary);
1023 $section->visible = $sect->visible;
1024 $section->sequence = "";
1025 //Now calculate the section's newid
1026 $newid = 0;
1027 if ($restore->restoreto == 2) {
1028 //Save it to db (only if restoring to new course)
1029 $newid = insert_record("course_sections",$section);
1030 } else {
1031 //Get section id when restoring in existing course
1032 $rec = get_record("course_sections","course",$restore->course_id,
1033 "section",$section->section);
1034 //If that section doesn't exist, get section 0 (every mod will be
1035 //asigned there
1036 if(!$rec) {
1037 $rec = get_record("course_sections","course",$restore->course_id,
1038 "section","0");
1040 //New check. If section 0 doesn't exist, insert it here !!
1041 //Teorically this never should happen but, in practice, some users
1042 //have reported this issue.
1043 if(!$rec) {
1044 $zero_sec = new object();
1045 $zero_sec->course = $restore->course_id;
1046 $zero_sec->section = 0;
1047 $zero_sec->summary = "";
1048 $zero_sec->sequence = "";
1049 $newid = insert_record("course_sections",$zero_sec);
1050 $rec->id = $newid;
1051 $rec->sequence = "";
1053 $newid = $rec->id;
1054 $sequence = $rec->sequence;
1056 if ($newid) {
1057 //save old and new section id
1058 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
1059 } else {
1060 $status = false;
1062 //If all is OK, go with associated mods
1063 if ($status) {
1064 //If we have mods in the section
1065 if (!empty($sect->mods)) {
1066 //For each mod inside section
1067 foreach ($sect->mods as $keym => $mod) {
1068 // Yu: This part is called repeatedly for every instance,
1069 // so it is necessary to set the granular flag and check isset()
1070 // when the first instance of this type of mod is processed.
1072 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
1074 if (!isset($restore->mods[$mod->type]->granular)) {
1075 if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
1076 // This defines whether we want to restore specific
1077 // instances of the modules (granular restore), or
1078 // whether we don't care and just want to restore
1079 // all module instances (non-granular).
1080 $restore->mods[$mod->type]->granular = true;
1081 } else {
1082 $restore->mods[$mod->type]->granular = false;
1086 //Check if we've to restore this module (and instance)
1087 if (!empty($restore->mods[$mod->type]->restore)) {
1088 if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
1089 || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
1090 && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
1092 //Get the module id from modules
1093 $module = get_record("modules","name",$mod->type);
1094 if ($module) {
1095 $course_module = new object();
1096 $course_module->course = $restore->course_id;
1097 $course_module->module = $module->id;
1098 $course_module->section = $newid;
1099 $course_module->added = $mod->added;
1100 $course_module->score = $mod->score;
1101 $course_module->indent = $mod->indent;
1102 $course_module->visible = $mod->visible;
1103 $course_module->groupmode = $mod->groupmode;
1104 if ($mod->groupingid and $grouping = restore_grouping_getid($restore, $mod->groupingid)) {
1105 $course_module->groupingid = $grouping->new_id;
1106 } else {
1107 $course_module->groupingid = 0;
1109 $course_module->groupmembersonly = $mod->groupmembersonly;
1110 $course_module->instance = 0;
1111 //NOTE: The instance (new) is calculated and updated in db in the
1112 // final step of the restore. We don't know it yet.
1113 //print_object($course_module); //Debug
1114 //Save it to db
1115 if ($mod->idnumber) {
1116 if (grade_verify_idnumber($mod->idnumber, $restore->course_id)) {
1117 $course_module->idnumber = $mod->idnumber;
1121 $newidmod = insert_record("course_modules", addslashes_recursive($course_module));
1122 if ($newidmod) {
1123 //save old and new module id
1124 //In the info field, we save the original instance of the module
1125 //to use it later
1126 backup_putid ($restore->backup_unique_code,"course_modules",
1127 $keym,$newidmod,$mod->instance);
1129 $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
1130 } else {
1131 $status = false;
1133 //Now, calculate the sequence field
1134 if ($status) {
1135 if ($sequence) {
1136 $sequence .= ",".$newidmod;
1137 } else {
1138 $sequence = $newidmod;
1141 } else {
1142 $status = false;
1149 //If all is OK, update sequence field in course_sections
1150 if ($status) {
1151 if (isset($sequence)) {
1152 $update_rec = new object();
1153 $update_rec->id = $newid;
1154 $update_rec->sequence = $sequence;
1155 $status = update_record("course_sections",$update_rec);
1159 } else {
1160 $status = false;
1162 return $status;
1165 //Called to set up any course-format specific data that may be in the file
1166 function restore_set_format_data($restore,$xml_file) {
1167 global $CFG,$db;
1169 $status = true;
1170 //Check it exists
1171 if (!file_exists($xml_file)) {
1172 return false;
1174 //Load data from XML to info
1175 if(!($info = restore_read_xml_formatdata($xml_file))) {
1176 return false;
1179 //Process format data if there is any
1180 if (isset($info->format_data)) {
1181 if(!$format=get_field('course','format','id',$restore->course_id)) {
1182 return false;
1184 // If there was any data then it must have a restore method
1185 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
1186 if(!file_exists($file)) {
1187 return false;
1189 require_once($file);
1190 $function=$format.'_restore_format_data';
1191 if(!function_exists($function)) {
1192 return false;
1194 return $function($restore,$info->format_data);
1197 // If we got here then there's no data, but that's cool
1198 return true;
1201 //This function creates all the metacourse data from xml, notifying
1202 //about each incidence
1203 function restore_create_metacourse($restore,$xml_file) {
1205 global $CFG,$db;
1207 $status = true;
1208 //Check it exists
1209 if (!file_exists($xml_file)) {
1210 $status = false;
1212 //Get info from xml
1213 if ($status) {
1214 //Load data from XML to info
1215 $info = restore_read_xml_metacourse($xml_file);
1218 //Process info about metacourse
1219 if ($status and $info) {
1220 //Process child records
1221 if (!empty($info->childs)) {
1222 foreach ($info->childs as $child) {
1223 $dbcourse = false;
1224 $dbmetacourse = false;
1225 //Check if child course exists in destination server
1226 //(by id in the same server or by idnumber and shortname in other server)
1227 if ($restore->original_wwwroot == $CFG->wwwroot) {
1228 //Same server, lets see by id
1229 $dbcourse = get_record('course','id',$child->id);
1230 } else {
1231 //Different server, lets see by idnumber and shortname, and only ONE record
1232 $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1233 if ($dbcount == 1) {
1234 $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1237 //If child course has been found, insert data
1238 if ($dbcourse) {
1239 $dbmetacourse->child_course = $dbcourse->id;
1240 $dbmetacourse->parent_course = $restore->course_id;
1241 $status = insert_record ('course_meta',$dbmetacourse);
1242 } else {
1243 //Child course not found, notice!
1244 if (!defined('RESTORE_SILENTLY')) {
1245 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
1249 //Now, recreate student enrolments...
1250 sync_metacourse($restore->course_id);
1252 //Process parent records
1253 if (!empty($info->parents)) {
1254 foreach ($info->parents as $parent) {
1255 $dbcourse = false;
1256 $dbmetacourse = false;
1257 //Check if parent course exists in destination server
1258 //(by id in the same server or by idnumber and shortname in other server)
1259 if ($restore->original_wwwroot == $CFG->wwwroot) {
1260 //Same server, lets see by id
1261 $dbcourse = get_record('course','id',$parent->id);
1262 } else {
1263 //Different server, lets see by idnumber and shortname, and only ONE record
1264 $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1265 if ($dbcount == 1) {
1266 $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1269 //If parent course has been found, insert data if it is a metacourse
1270 if ($dbcourse) {
1271 if ($dbcourse->metacourse) {
1272 $dbmetacourse->parent_course = $dbcourse->id;
1273 $dbmetacourse->child_course = $restore->course_id;
1274 $status = insert_record ('course_meta',$dbmetacourse);
1275 //Now, recreate student enrolments in parent course
1276 sync_metacourse($dbcourse->id);
1277 } else {
1278 //Parent course isn't metacourse, notice!
1279 if (!defined('RESTORE_SILENTLY')) {
1280 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1283 } else {
1284 //Parent course not found, notice!
1285 if (!defined('RESTORE_SILENTLY')) {
1286 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1293 return $status;
1297 * This function migrades all the pre 1.9 gradebook data from xml
1299 function restore_migrate_old_gradebook($restore,$xml_file) {
1300 global $CFG;
1302 $status = true;
1303 //Check it exists
1304 if (!file_exists($xml_file)) {
1305 return false;
1308 // Get info from xml
1309 // info will contain the number of record to process
1310 $info = restore_read_xml_gradebook($restore, $xml_file);
1312 // If we have info, then process
1313 if (empty($info)) {
1314 return $status;
1317 // make sure top course category exists
1318 $course_category = grade_category::fetch_course_category($restore->course_id);
1319 $course_category->load_grade_item();
1321 // we need to know if all grade items that were backed up are being restored
1322 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1323 // i.e. the aggregated grades of that category
1325 $restoreall = true; // set to false if any grade_item is not selected/restored
1326 $importing = !empty($SESSION->restore->importing); // there should not be a way to import old backups, but anyway ;-)
1328 if ($importing) {
1329 $restoreall = false;
1331 } else {
1332 $prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
1333 $prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
1335 // if any categories already present, skip restore of categories from backup
1336 if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
1337 $restoreall = false;
1339 unset($prev_grade_items);
1340 unset($prev_grade_cats);
1343 // force creation of all grade_items - the course_modules already exist
1344 grade_force_full_regrading($restore->course_id);
1345 grade_grab_course_grades($restore->course_id);
1347 // Start ul
1348 if (!defined('RESTORE_SILENTLY')) {
1349 echo '<ul>';
1352 /// Process letters
1353 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1354 // respect current grade letters if defined
1355 if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
1356 if (!defined('RESTORE_SILENTLY')) {
1357 echo '<li>'.get_string('gradeletters','grades').'</li>';
1359 // Fetch recordset_size records in each iteration
1360 $recs = get_records_select("backup_ids","table_name = 'grade_letter' AND backup_code = $restore->backup_unique_code",
1362 "old_id");
1363 if ($recs) {
1364 foreach ($recs as $rec) {
1365 // Get the full record from backup_ids
1366 $data = backup_getid($restore->backup_unique_code,'grade_letter',$rec->old_id);
1367 if ($data) {
1368 $info = $data->info;
1369 $dbrec = new object();
1370 $dbrec->contextid = $context->id;
1371 $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['GRADE_LOW']['0']['#']);
1372 $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
1373 insert_record('grade_letters', $dbrec);
1379 if (!defined('RESTORE_SILENTLY')) {
1380 echo '<li>'.get_string('categories','grades').'</li>';
1382 //Fetch recordset_size records in each iteration
1383 $recs = get_records_select("backup_ids","table_name = 'grade_category' AND backup_code = $restore->backup_unique_code",
1384 "old_id",
1385 "old_id");
1386 $cat_count = count($recs);
1387 if ($recs) {
1388 foreach ($recs as $rec) {
1389 //Get the full record from backup_ids
1390 $data = backup_getid($restore->backup_unique_code,'grade_category',$rec->old_id);
1391 if ($data) {
1392 //Now get completed xmlized object
1393 $info = $data->info;
1395 if ($restoreall) {
1396 if ($cat_count == 1) {
1397 $course_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
1398 $course_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
1399 $course_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
1400 $course_category->aggregateonlygraded = 0;
1401 $course_category->update('restore');
1402 $grade_category = $course_category;
1404 } else {
1405 $grade_category = new grade_category();
1406 $grade_category->courseid = $restore->course_id;
1407 $grade_category->fullname = backup_todb($info['GRADE_CATEGORY']['#']['NAME']['0']['#'], false);
1408 $grade_category->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROP_X_LOWEST']['0']['#'], false);
1409 $grade_category->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
1410 $grade_category->aggregateonlygraded = 0;
1411 $grade_category->insert('restore');
1412 $grade_category->load_grade_item(); // force cretion of grade_item
1415 } else {
1416 $grade_category = null;
1419 /// now, restore grade_items
1420 $items = array();
1421 if (!empty($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'])) {
1422 //Iterate over items
1423 foreach ($info['GRADE_CATEGORY']['#']['GRADE_ITEMS']['0']['#']['GRADE_ITEM'] as $ite_info) {
1424 $modname = backup_todb($ite_info['#']['MODULE_NAME']['0']['#'], false);
1425 $olditeminstance = backup_todb($ite_info['#']['CMINSTANCE']['0']['#'], false);
1426 if (!$mod = backup_getid($restore->backup_unique_code,$modname, $olditeminstance)) {
1427 continue; // not restored
1429 $iteminstance = $mod->new_id;
1430 if (!$cm = get_coursemodule_from_instance($modname, $iteminstance, $restore->course_id)) {
1431 continue; // does not exist
1434 if (!$grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$cm->modname, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course, 'itemnumber'=>0))) {
1435 continue; // no item yet??
1438 if ($grade_category) {
1439 $grade_item->sortorder = backup_todb($ite_info['#']['SORT_ORDER']['0']['#'], false);
1440 $grade_item->set_parent($grade_category->id);
1443 if ($importing
1444 or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
1445 // module instance not selected when restored using granular
1446 // skip this item
1447 continue;
1450 //Now process grade excludes
1451 if (empty($ite_info['#']['GRADE_EXCEPTIONS'])) {
1452 continue;
1455 foreach($ite_info['#']['GRADE_EXCEPTIONS']['0']['#']['GRADE_EXCEPTION'] as $exc_info) {
1456 if ($u = backup_getid($restore->backup_unique_code,"user",backup_todb($exc_info['#']['USERID']['0']['#']))) {
1457 $userid = $u->new_id;
1458 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
1459 $grade_grade->excluded = 1;
1460 if ($grade_grade->id) {
1461 $grade_grade->update('restore');
1462 } else {
1463 $grade_grade->insert('restore');
1473 if (!defined('RESTORE_SILENTLY')) {
1474 //End ul
1475 echo '</ul>';
1478 return $status;
1482 * This function creates all the gradebook data from xml
1484 function restore_create_gradebook($restore,$xml_file) {
1485 global $CFG;
1487 $status = true;
1488 //Check it exists
1489 if (!file_exists($xml_file)) {
1490 return false;
1493 // Get info from xml
1494 // info will contain the number of record to process
1495 $info = restore_read_xml_gradebook($restore, $xml_file);
1497 // If we have info, then process
1498 if (empty($info)) {
1499 return $status;
1502 if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
1503 $restore_histories = true;
1504 } else {
1505 $restore_histories = false;
1508 // make sure top course category exists
1509 $course_category = grade_category::fetch_course_category($restore->course_id);
1510 $course_category->load_grade_item();
1512 // we need to know if all grade items that were backed up are being restored
1513 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1514 // i.e. the aggregated grades of that category
1516 $restoreall = true; // set to false if any grade_item is not selected/restored or already exist
1517 $importing = !empty($SESSION->restore->importing);
1519 if ($importing) {
1520 $restoreall = false;
1522 } else {
1523 $prev_grade_items = grade_item::fetch_all(array('courseid'=>$restore->course_id));
1524 $prev_grade_cats = grade_category::fetch_all(array('courseid'=>$restore->course_id));
1526 // if any categories already present, skip restore of categories from backup - course item or category already exist
1527 if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
1528 $restoreall = false;
1530 unset($prev_grade_items);
1531 unset($prev_grade_cats);
1533 if ($restoreall) {
1534 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = $restore->backup_unique_code", "", "old_id")) {
1535 foreach ($recs as $rec) {
1536 if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
1538 $info = $data->info;
1539 // do not restore if this grade_item is a mod, and
1540 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1542 if ($itemtype == 'mod') {
1543 $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1544 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1546 if (empty($restore->mods[$itemmodule]->granular)) {
1547 continue;
1548 } else if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
1549 continue;
1551 // at least one activity should not be restored - do not restore categories and manual items at all
1552 $restoreall = false;
1553 break;
1561 // Start ul
1562 if (!defined('RESTORE_SILENTLY')) {
1563 echo '<ul>';
1566 // array of restored categories - speedup ;-)
1567 $cached_categories = array();
1568 $outcomes = array();
1570 /// Process letters
1571 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1572 // respect current grade letters if defined
1573 if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
1574 if (!defined('RESTORE_SILENTLY')) {
1575 echo '<li>'.get_string('gradeletters','grades').'</li>';
1577 // Fetch recordset_size records in each iteration
1578 $recs = get_records_select("backup_ids","table_name = 'grade_letters' AND backup_code = $restore->backup_unique_code",
1580 "old_id");
1581 if ($recs) {
1582 foreach ($recs as $rec) {
1583 // Get the full record from backup_ids
1584 $data = backup_getid($restore->backup_unique_code,'grade_letters',$rec->old_id);
1585 if ($data) {
1586 $info = $data->info;
1587 $dbrec = new object();
1588 $dbrec->contextid = $context->id;
1589 $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
1590 $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
1591 insert_record('grade_letters', $dbrec);
1597 /// Preprocess outcomes - do not store them yet!
1598 if ($status and !$importing and $restoreall) {
1599 if (!defined('RESTORE_SILENTLY')) {
1600 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1602 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1604 "old_id");
1605 if ($recs) {
1606 foreach ($recs as $rec) {
1607 //Get the full record from backup_ids
1608 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1609 if ($data) {
1610 $info = $data->info;
1612 //first find out if outcome already exists
1613 $shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1615 if ($candidates = get_records_sql("SELECT *
1616 FROM {$CFG->prefix}grade_outcomes
1617 WHERE (courseid IS NULL OR courseid = $restore->course_id)
1618 AND shortname = '$shortname'
1619 ORDER BY courseid ASC, id ASC")) {
1620 $grade_outcome = reset($candidates);
1621 $outcomes[$rec->old_id] = $grade_outcome;
1622 continue;
1625 $dbrec = new object();
1627 if (has_capability('moodle/grade:manageoutcomes', get_context_instance(CONTEXT_SYSTEM))) {
1628 $oldoutcome = backup_todb($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']);
1629 if (empty($oldoutcome)) {
1630 //site wide
1631 $dbrec->courseid = null;
1632 } else {
1633 //course only
1634 $dbrec->courseid = $restore->course_id;
1636 } else {
1637 // no permission to add site outcomes
1638 $dbrec->courseid = $restore->course_id;
1641 //Get the fields
1642 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#'], false);
1643 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#'], false);
1644 $dbrec->scaleid = backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#'], false);
1645 $dbrec->description = backup_todb($info['GRADE_OUTCOME']['#']['DESCRIPTION']['0']['#'], false);
1646 $dbrec->timecreated = backup_todb($info['GRADE_OUTCOME']['#']['TIMECREATED']['0']['#'], false);
1647 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME']['#']['TIMEMODIFIED']['0']['#'], false);
1648 $dbrec->usermodified = backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#'], false);
1650 //Need to recode the scaleid
1651 if ($scale = backup_getid($restore->backup_unique_code, 'scale', $dbrec->scaleid)) {
1652 $dbrec->scaleid = $scale->new_id;
1655 //Need to recode the usermodified
1656 if ($modifier = backup_getid($restore->backup_unique_code, 'user', $dbrec->usermodified)) {
1657 $dbrec->usermodified = $modifier->new_id;
1660 $grade_outcome = new grade_outcome($dbrec, false);
1661 $outcomes[$rec->old_id] = $grade_outcome;
1667 /// Process grade items and grades
1668 if ($status) {
1669 if (!defined('RESTORE_SILENTLY')) {
1670 echo '<li>'.get_string('gradeitems','grades').'</li>';
1672 $counter = 0;
1674 //Fetch recordset_size records in each iteration
1675 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1676 "id", // restore in the backup order
1677 "old_id");
1679 if ($recs) {
1680 foreach ($recs as $rec) {
1681 //Get the full record from backup_ids
1682 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
1683 if ($data) {
1684 $info = $data->info;
1686 // first find out if category or normal item
1687 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
1688 if ($itemtype == 'course' or $itemtype == 'category') {
1689 if (!$restoreall or $importing) {
1690 continue;
1693 $oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
1694 if (!$cdata = backup_getid($restore->backup_unique_code,'grade_categories',$oldcat)) {
1695 continue;
1697 $cinfo = $cdata->info;
1698 unset($cdata);
1699 if ($itemtype == 'course') {
1701 $course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
1702 $course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
1703 $course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
1704 $course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
1705 $course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
1706 $course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
1707 $course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
1708 $course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
1709 $course_category->update('restore');
1711 $status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$course_category->id) && $status;
1712 $cached_categories[$oldcat] = $course_category;
1713 $grade_item = $course_category->get_grade_item();
1715 } else {
1716 $oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false);
1717 if (empty($cached_categories[$oldparent])) {
1718 debugging('parent not found '.$oldparent);
1719 continue; // parent not found, sorry
1721 $grade_category = new grade_category();
1722 $grade_category->courseid = $restore->course_id;
1723 $grade_category->parent = $cached_categories[$oldparent]->id;
1724 $grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
1725 $grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
1726 $grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
1727 $grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
1728 $grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
1729 $grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
1730 $grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
1731 $grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
1732 $grade_category->insert('restore');
1734 $status = backup_putid($restore->backup_unique_code,'grade_categories',$oldcat,$grade_category->id) && $status;
1735 $cached_categories[$oldcat] = $grade_category;
1736 $grade_item = $grade_category->get_grade_item(); // creates grade_item too
1738 unset($cinfo);
1740 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
1741 if (grade_verify_idnumber($idnumber, $restore->course_id)) {
1742 $grade_item->idnumber = $idnumber;
1745 $grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
1746 $grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
1747 $grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
1748 $grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
1749 $grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
1750 $grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
1751 $grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
1752 $grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
1753 $grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
1754 $grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
1755 $grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
1756 $grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
1757 $grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
1758 $grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
1759 $grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
1760 $grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
1762 if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
1763 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
1764 $grade_item->scaleid = $scale->new_id;
1767 if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) {
1768 $outcome = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false));
1769 $grade_item->outcomeid = $outcome->new_id;
1772 $grade_item->update('restore');
1773 $status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
1775 } else {
1776 if ($itemtype != 'mod' and (!$restoreall or $importing)) {
1777 // not extra gradebook stuff if restoring individual activities or something already there
1778 continue;
1781 $dbrec = new object();
1783 $dbrec->courseid = $restore->course_id;
1784 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
1785 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
1787 if ($itemtype == 'mod') {
1788 // iteminstance should point to new mod
1789 $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
1790 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $olditeminstance);
1791 $dbrec->iteminstance = $mod->new_id;
1792 if (!$cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id)) {
1793 // item not restored - no item
1794 continue;
1796 // keep in sync with activity idnumber
1797 $dbrec->idnumber = $cm->idnumber;
1799 } else {
1800 $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
1802 if (grade_verify_idnumber($idnumber, $restore->course_id)) {
1803 //make sure the new idnumber is unique
1804 $dbrec->idnumber = $idnumber;
1808 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
1809 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
1810 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
1811 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false);
1812 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
1813 $dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
1814 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
1815 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
1816 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
1817 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
1818 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
1819 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
1820 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
1821 $dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
1822 $dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
1823 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
1824 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
1825 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
1826 $dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
1828 if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
1829 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
1830 $dbrec->scaleid = $scale->new_id;
1833 if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) {
1834 $oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
1835 if (empty($outcomes[$oldoutcome])) {
1836 continue; // error!
1838 if (empty($outcomes[$oldoutcome]->id)) {
1839 $outcomes[$oldoutcome]->insert('restore');
1840 $outcomes[$oldoutcome]->use_in($restore->course_id);
1841 backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id);
1843 $dbrec->outcomeid = $outcomes[$oldoutcome]->id;
1846 $grade_item = new grade_item($dbrec, false);
1847 $grade_item->insert('restore');
1848 if ($restoreall) {
1849 // set original parent if restored
1850 $oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
1851 if (!empty($cached_categories[$oldcat])) {
1852 $grade_item->set_parent($cached_categories[$oldcat]->id);
1855 $status = backup_putid($restore->backup_unique_code,"grade_items", $rec->old_id, $grade_item->id) && $status;
1858 // no need to restore grades if user data is not selected or importing activities
1859 if ($importing
1860 or ($grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance))) {
1861 // module instance not selected when restored using granular
1862 // skip this item
1863 continue;
1866 /// now, restore grade_grades
1867 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1868 //Iterate over items
1869 foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) {
1871 $grade = new grade_grade();
1872 $grade->itemid = $grade_item->id;
1874 $olduser = backup_todb($g_info['#']['USERID']['0']['#'], false);
1875 $user = backup_getid($restore->backup_unique_code,"user",$olduser);
1876 $grade->userid = $user->new_id;
1878 $grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false);
1879 $grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false);
1880 $grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false);
1881 // need to find scaleid
1882 if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) {
1883 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false));
1884 $grade->rawscaleid = $scale->new_id;
1887 if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) {
1888 if ($modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) {
1889 $grade->usermodified = $modifier->new_id;
1893 $grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false);
1894 $grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false);
1895 $grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false);
1896 $grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false);
1897 $grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false);
1898 $grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false);
1899 $grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false);
1900 $grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false);
1901 $grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false);
1902 $grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false);
1903 $grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false);
1904 $grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false);
1905 $grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false);
1907 $grade->insert('restore');
1908 backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id);
1910 $counter++;
1911 if ($counter % 20 == 0) {
1912 if (!defined('RESTORE_SILENTLY')) {
1913 echo ".";
1914 if ($counter % 400 == 0) {
1915 echo "<br />";
1918 backup_flush(300);
1927 /// add outcomes that are not used when doing full restore
1928 if ($status and $restoreall) {
1929 foreach ($outcomes as $oldoutcome=>$grade_outcome) {
1930 if (empty($grade_outcome->id)) {
1931 $grade_outcome->insert('restore');
1932 $grade_outcome->use_in($restore->course_id);
1933 backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id);
1939 if ($status and !$importing and $restore_histories) {
1940 /// following code is very inefficient
1942 $gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
1943 $gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
1944 $gihcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
1945 $gohcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
1947 // Number of records to get in every chunk
1948 $recordset_size = 2;
1950 // process histories
1951 if ($gchcount && $status) {
1952 if (!defined('RESTORE_SILENTLY')) {
1953 echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
1955 $counter = 0;
1956 while ($counter < $gchcount) {
1957 //Fetch recordset_size records in each iteration
1958 $recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
1959 "old_id",
1960 "old_id",
1961 $counter,
1962 $recordset_size);
1963 if ($recs) {
1964 foreach ($recs as $rec) {
1965 //Get the full record from backup_ids
1966 $data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_id);
1967 if ($data) {
1968 //Now get completed xmlized object
1969 $info = $data->info;
1970 //traverse_xmlize($info); //Debug
1971 //print_object ($GLOBALS['traverse_array']); //Debug
1972 //$GLOBALS['traverse_array']=""; //Debug
1974 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
1975 if (empty($oldobj->new_id)) {
1976 // if the old object is not being restored, can't restoring its history
1977 $counter++;
1978 continue;
1980 $dbrec->oldid = $oldobj->new_id;
1981 $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
1982 $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
1983 $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1985 // loggeduser might not be restored, e.g. admin
1986 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1987 $dbrec->loggeduser = $oldobj->new_id;
1990 // this item might not have a parent at all, do not skip it if no parent is specified
1991 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
1992 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
1993 if (empty($oldobj->new_id)) {
1994 // if the parent category not restored
1995 $counter++;
1996 continue;
1999 $dbrec->parent = $oldobj->new_id;
2000 $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
2001 // path needs to be rebuilt
2002 if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
2003 // to preserve the path and make it work, we need to replace the categories one by one
2004 // we first get the list of categories in current path
2005 if ($paths = explode("/", $path)) {
2006 $newpath = '';
2007 foreach ($paths as $catid) {
2008 if ($catid) {
2009 // find the new corresponding path
2010 $oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
2011 $newpath .= "/$oldpath->new_id";
2014 $dbrec->path = $newpath;
2017 $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
2018 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
2019 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
2020 $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
2022 $dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
2023 $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
2024 $dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
2026 $dbrec->courseid = $restore->course_id;
2027 insert_record('grade_categories_history', $dbrec);
2028 unset($dbrec);
2031 //Increment counters
2032 $counter++;
2033 //Do some output
2034 if ($counter % 1 == 0) {
2035 if (!defined('RESTORE_SILENTLY')) {
2036 echo ".";
2037 if ($counter % 20 == 0) {
2038 echo "<br />";
2041 backup_flush(300);
2048 // process histories
2049 if ($gghcount && $status) {
2050 if (!defined('RESTORE_SILENTLY')) {
2051 echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
2053 $counter = 0;
2054 while ($counter < $gghcount) {
2055 //Fetch recordset_size records in each iteration
2056 $recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
2057 "old_id",
2058 "old_id",
2059 $counter,
2060 $recordset_size);
2061 if ($recs) {
2062 foreach ($recs as $rec) {
2063 //Get the full record from backup_ids
2064 $data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_id);
2065 if ($data) {
2066 //Now get completed xmlized object
2067 $info = $data->info;
2068 //traverse_xmlize($info); //Debug
2069 //print_object ($GLOBALS['traverse_array']); //Debug
2070 //$GLOBALS['traverse_array']=""; //Debug
2072 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
2073 if (empty($oldobj->new_id)) {
2074 // if the old object is not being restored, can't restoring its history
2075 $counter++;
2076 continue;
2078 $dbrec->oldid = $oldobj->new_id;
2079 $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
2080 $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
2081 $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2082 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2083 $dbrec->loggeduser = $oldobj->new_id;
2086 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
2087 $dbrec->itemid = $oldobj->new_id;
2088 if (empty($dbrec->itemid)) {
2089 $counter++;
2090 continue; // grade item not being restored
2092 $oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
2093 $dbrec->userid = $oldobj->new_id;
2094 $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
2095 $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
2096 $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
2097 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
2098 $dbrec->usermodified = $oldobj->new_id;
2101 if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
2102 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
2103 $dbrec->rawscaleid = $scale->new_id;
2106 $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
2107 $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
2108 $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
2109 $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
2110 $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
2111 $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
2112 $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
2113 $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
2114 $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
2115 $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
2116 $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
2118 insert_record('grade_grades_history', $dbrec);
2119 unset($dbrec);
2122 //Increment counters
2123 $counter++;
2124 //Do some output
2125 if ($counter % 1 == 0) {
2126 if (!defined('RESTORE_SILENTLY')) {
2127 echo ".";
2128 if ($counter % 20 == 0) {
2129 echo "<br />";
2132 backup_flush(300);
2139 // process histories
2141 if ($gihcount && $status) {
2142 if (!defined('RESTORE_SILENTLY')) {
2143 echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
2145 $counter = 0;
2146 while ($counter < $gihcount) {
2147 //Fetch recordset_size records in each iteration
2148 $recs = get_records_select("backup_ids","table_name = 'grade_items_history' AND backup_code = '$restore->backup_unique_code'",
2149 "old_id",
2150 "old_id",
2151 $counter,
2152 $recordset_size);
2153 if ($recs) {
2154 foreach ($recs as $rec) {
2155 //Get the full record from backup_ids
2156 $data = backup_getid($restore->backup_unique_code,'grade_items_history',$rec->old_id);
2157 if ($data) {
2158 //Now get completed xmlized object
2159 $info = $data->info;
2160 //traverse_xmlize($info); //Debug
2161 //print_object ($GLOBALS['traverse_array']); //Debug
2162 //$GLOBALS['traverse_array']=""; //Debug
2165 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
2166 if (empty($oldobj->new_id)) {
2167 // if the old object is not being restored, can't restoring its history
2168 $counter++;
2169 continue;
2171 $dbrec->oldid = $oldobj->new_id;
2172 $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
2173 $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
2174 $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2175 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2176 $dbrec->loggeduser = $oldobj->new_id;
2178 $dbrec->courseid = $restore->course_id;
2179 $oldobj = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
2180 $oldobj->categoryid = $category->new_id;
2181 if (empty($oldobj->categoryid)) {
2182 $counter++;
2183 continue; // category not restored
2186 $dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
2187 $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
2188 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
2190 // code from grade_items restore
2191 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
2192 // do not restore if this grade_item is a mod, and
2193 if ($dbrec->itemtype == 'mod') {
2195 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
2196 // module instance not selected when restored using granular
2197 // skip this item
2198 $counter++;
2199 continue;
2202 // iteminstance should point to new mod
2204 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
2205 $dbrec->iteminstance = $mod->new_id;
2207 } else if ($dbrec->itemtype == 'category') {
2208 // the item instance should point to the new grade category
2210 // only proceed if we are restoring all grade items
2211 if ($restoreall) {
2212 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
2213 $dbrec->iteminstance = $category->new_id;
2214 } else {
2215 // otherwise we can safely ignore this grade item and subsequent
2216 // grade_raws, grade_finals etc
2217 continue;
2219 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
2220 if ($restoreall) {
2221 // TODO any special code needed here to restore course item without duplicating it?
2222 // find the course category with depth 1, and course id = current course id
2223 // this would have been already restored
2225 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
2226 $dbrec->iteminstance = $cat->id;
2228 } else {
2229 $counter++;
2230 continue;
2234 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
2235 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
2236 $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
2237 $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
2238 $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
2239 $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
2240 $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
2241 if ($oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
2242 // scaleid is optional
2243 $dbrec->scaleid = $oldobj->new_id;
2245 if ($oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
2246 // outcome is optional
2247 $dbrec->outcomeid = $oldobj->new_id;
2249 $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
2250 $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
2251 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
2252 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
2253 $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
2254 $dbrec->display = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DISPLAY']['0']['#']);
2255 $dbrec->decimals = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DECIMALS']['0']['#']);
2256 $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
2257 $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
2258 $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
2259 $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
2261 insert_record('grade_items_history', $dbrec);
2262 unset($dbrec);
2265 //Increment counters
2266 $counter++;
2267 //Do some output
2268 if ($counter % 1 == 0) {
2269 if (!defined('RESTORE_SILENTLY')) {
2270 echo ".";
2271 if ($counter % 20 == 0) {
2272 echo "<br />";
2275 backup_flush(300);
2282 // process histories
2283 if ($gohcount && $status) {
2284 if (!defined('RESTORE_SILENTLY')) {
2285 echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
2287 $counter = 0;
2288 while ($counter < $gohcount) {
2289 //Fetch recordset_size records in each iteration
2290 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_history' AND backup_code = '$restore->backup_unique_code'",
2291 "old_id",
2292 "old_id",
2293 $counter,
2294 $recordset_size);
2295 if ($recs) {
2296 foreach ($recs as $rec) {
2297 //Get the full record from backup_ids
2298 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_history',$rec->old_id);
2299 if ($data) {
2300 //Now get completed xmlized object
2301 $info = $data->info;
2302 //traverse_xmlize($info); //Debug
2303 //print_object ($GLOBALS['traverse_array']); //Debug
2304 //$GLOBALS['traverse_array']=""; //Debug
2306 $oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
2307 if (empty($oldobj->new_id)) {
2308 // if the old object is not being restored, can't restoring its history
2309 $counter++;
2310 continue;
2312 $dbrec->oldid = $oldobj->new_id;
2313 $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
2314 $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
2315 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2316 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2317 $dbrec->loggeduser = $oldobj->new_id;
2319 $dbrec->courseid = $restore->course_id;
2320 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
2321 $dbrec->fullname= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
2322 $oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
2323 $dbrec->scaleid = $oldobj->new_id;
2324 $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
2326 insert_record('grade_outcomes_history', $dbrec);
2327 unset($dbrec);
2330 //Increment counters
2331 $counter++;
2332 //Do some output
2333 if ($counter % 1 == 0) {
2334 if (!defined('RESTORE_SILENTLY')) {
2335 echo ".";
2336 if ($counter % 20 == 0) {
2337 echo "<br />";
2340 backup_flush(300);
2348 if (!defined('RESTORE_SILENTLY')) {
2349 //End ul
2350 echo '</ul>';
2352 return $status;
2355 //This function creates all the user, user_students, user_teachers
2356 //user_course_creators and user_admins from xml
2357 function restore_create_users($restore,$xml_file) {
2359 global $CFG, $db;
2360 require_once ($CFG->dirroot.'/tag/lib.php');
2362 $status = true;
2363 //Check it exists
2364 if (!file_exists($xml_file)) {
2365 $status = false;
2367 //Get info from xml
2368 if ($status) {
2369 //info will contain the old_id of every user
2370 //in backup_ids->info will be the real info (serialized)
2371 $info = restore_read_xml_users($restore,$xml_file);
2374 //Now, get evey user_id from $info and user data from $backup_ids
2375 //and create the necessary db structures
2377 if (!empty($info->users)) {
2379 /// Grab mnethosts keyed by wwwroot, to map to id
2380 $mnethosts = get_records('mnet_host', '', '',
2381 'wwwroot', 'wwwroot, id');
2383 /// Get languages for quick search later
2384 $languages = get_list_of_languages();
2386 /// Iterate over all users loaded from xml
2387 $counter = 0;
2388 foreach ($info->users as $userid) {
2389 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
2390 $user = $rec->info;
2391 foreach (array_keys(get_object_vars($user)) as $field) {
2392 if (!is_array($user->$field)) {
2393 $user->$field = backup_todb($user->$field);
2394 if (is_null($user->$field)) {
2395 $user->$field = '';
2400 //Now, recode some languages (Moodle 1.5)
2401 if ($user->lang == 'ma_nt') {
2402 $user->lang = 'mi_nt';
2405 //Country list updates - MDL-13060
2406 //Any user whose country code has been deleted or modified needs to be assigned a valid one.
2407 $country_update_map = array(
2408 'ZR' => 'CD',
2409 'TP' => 'TL',
2410 'FX' => 'FR',
2411 'KO' => 'RS',
2412 'CS' => 'RS',
2413 'WA' => 'GB');
2414 if (array_key_exists($user->country, $country_update_map)) {
2415 $user->country = $country_update_map[$user->country];
2418 //If language does not exist here - use site default
2419 if (!array_key_exists($user->lang, $languages)) {
2420 $user->lang = $CFG->lang;
2423 //Check if it's admin and coursecreator
2424 $is_admin = !empty($user->roles['admin']);
2425 $is_coursecreator = !empty($user->roles['coursecreator']);
2427 //Check if it's teacher and student
2428 $is_teacher = !empty($user->roles['teacher']);
2429 $is_student = !empty($user->roles['student']);
2431 //Check if it's needed
2432 $is_needed = !empty($user->roles['needed']);
2434 //Calculate if it is a course user
2435 //Has role teacher or student or needed
2436 $is_course_user = ($is_teacher or $is_student or $is_needed);
2438 //Calculate mnethostid
2439 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
2440 $user->mnethostid = $CFG->mnet_localhost_id;
2441 } else {
2442 // fast url-to-id lookups
2443 if (isset($mnethosts[$user->mnethosturl])) {
2444 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
2445 } else {
2446 // should not happen, as we check in restore_chech.php
2447 // but handle the error if it does
2448 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
2451 unset($user->mnethosturl);
2453 //To store user->id along all the iteration
2454 $newid=null;
2455 //check if it exists (by username) and get its id
2456 $user_exists = true;
2457 $user_data = get_record("user","username",addslashes($user->username),
2458 'mnethostid', $user->mnethostid);
2459 if (!$user_data) {
2460 $user_exists = false;
2461 } else {
2462 $newid = $user_data->id;
2465 //Flags to see what parts are we going to restore
2466 $create_user = true;
2467 $create_roles = true;
2468 $create_custom_profile_fields = true;
2469 $create_tags = true;
2470 $create_preferences = true;
2472 //If we are restoring course users and it isn't a course user
2473 if ($restore->users == 1 and !$is_course_user) {
2474 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
2475 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
2476 $create_user = false;
2477 $create_roles = false;
2478 $create_custom_profile_fields = false;
2479 $create_tags = false;
2480 $create_preferences = false;
2483 if ($user_exists and $create_user) {
2484 //If user exists mark its newid in backup_ids (the same than old)
2485 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
2486 $create_user = false;
2487 $create_custom_profile_fields = false;
2488 $create_tags = false;
2489 $create_preferences = false;
2492 //Here, if create_user, do it
2493 if ($create_user) {
2494 //Unset the id because it's going to be inserted with a new one
2495 unset ($user->id);
2496 // relink the descriptions
2497 $user->description = stripslashes($user->description);
2499 /// Disable pictures based on global setting or existing empty value (old backups can contain wrong empties)
2500 if (!empty($CFG->disableuserimages) || empty($user->picture)) {
2501 $user->picture = 0;
2504 //We need to analyse the AUTH field to recode it:
2505 // - if the field isn't set, we are in a pre 1.4 backup and we'll
2506 // use manual
2508 if (empty($user->auth)) {
2509 if ($CFG->registerauth == 'email') {
2510 $user->auth = 'email';
2511 } else {
2512 $user->auth = 'manual';
2516 //We need to process the POLICYAGREED field to recalculate it:
2517 // - if the destination site is different (by wwwroot) reset it.
2518 // - if the destination site is the same (by wwwroot), leave it unmodified
2520 if ($restore->original_wwwroot != $CFG->wwwroot) {
2521 $user->policyagreed = 0;
2522 } else {
2523 //Nothing to do, we are in the same server
2526 //Check if the theme exists in destination server
2527 $themes = get_list_of_themes();
2528 if (!in_array($user->theme, $themes)) {
2529 $user->theme = '';
2532 //We are going to create the user
2533 //The structure is exactly as we need
2535 $newid = insert_record ("user", addslashes_recursive($user));
2536 //Put the new id
2537 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
2540 ///TODO: This seccion is to support pre 1.7 course backups, using old roles
2541 /// teacher, coursecreator, student.... providing a basic mapping to new ones.
2542 /// Someday we'll drop support for them and this section will be safely deleted (2.0?)
2543 //Here, if create_roles, do it as necessary
2544 if ($create_roles) {
2545 //Get the newid and current info from backup_ids
2546 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2547 $newid = $data->new_id;
2548 $currinfo = $data->info.",";
2550 //Now, depending of the role, create records in user_studentes and user_teacher
2551 //and/or mark it in backup_ids
2553 if ($is_admin) {
2554 //If the record (user_admins) doesn't exists
2555 //Only put status in backup_ids
2556 $currinfo = $currinfo."admin,";
2557 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2559 if ($is_coursecreator) {
2560 //If the record (user_coursecreators) doesn't exists
2561 //Only put status in backup_ids
2562 $currinfo = $currinfo."coursecreator,";
2563 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2565 if ($is_needed) {
2566 //Only put status in backup_ids
2567 $currinfo = $currinfo."needed,";
2568 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2570 if ($is_teacher) {
2571 //If the record (teacher) doesn't exists
2572 //Put status in backup_ids
2573 $currinfo = $currinfo."teacher,";
2574 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2575 //Set course and user
2576 $user->roles['teacher']->course = $restore->course_id;
2577 $user->roles['teacher']->userid = $newid;
2579 //Need to analyse the enrol field
2580 // - if it isn't set, set it to $CFG->enrol
2581 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2582 // - if we are in the same server (by wwwroot), maintain it unmodified.
2583 if (empty($user->roles['teacher']->enrol)) {
2584 $user->roles['teacher']->enrol = $CFG->enrol;
2585 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2586 $user->roles['teacher']->enrol = $CFG->enrol;
2587 } else {
2588 //Nothing to do. Leave it unmodified
2591 $rolesmapping = $restore->rolesmapping;
2592 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2593 if ($user->roles['teacher']->editall) {
2594 role_assign($rolesmapping['defaultteacheredit'],
2595 $newid,
2597 $context->id,
2598 $user->roles['teacher']->timestart,
2599 $user->roles['teacher']->timeend,
2601 $user->roles['teacher']->enrol);
2603 // editting teacher
2604 } else {
2605 // non editting teacher
2606 role_assign($rolesmapping['defaultteacher'],
2607 $newid,
2609 $context->id,
2610 $user->roles['teacher']->timestart,
2611 $user->roles['teacher']->timeend,
2613 $user->roles['teacher']->enrol);
2616 if ($is_student) {
2618 //Put status in backup_ids
2619 $currinfo = $currinfo."student,";
2620 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2621 //Set course and user
2622 $user->roles['student']->course = $restore->course_id;
2623 $user->roles['student']->userid = $newid;
2625 //Need to analyse the enrol field
2626 // - if it isn't set, set it to $CFG->enrol
2627 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2628 // - if we are in the same server (by wwwroot), maintain it unmodified.
2629 if (empty($user->roles['student']->enrol)) {
2630 $user->roles['student']->enrol = $CFG->enrol;
2631 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2632 $user->roles['student']->enrol = $CFG->enrol;
2633 } else {
2634 //Nothing to do. Leave it unmodified
2636 $rolesmapping = $restore->rolesmapping;
2637 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2639 role_assign($rolesmapping['defaultstudent'],
2640 $newid,
2642 $context->id,
2643 $user->roles['student']->timestart,
2644 $user->roles['student']->timeend,
2646 $user->roles['student']->enrol);
2649 if (!$is_course_user) {
2650 //If the record (user) doesn't exists
2651 if (!record_exists("user","id",$newid)) {
2652 //Put status in backup_ids
2653 $currinfo = $currinfo."user,";
2654 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2659 /// Here, if create_custom_profile_fields, do it as necessary
2660 if ($create_custom_profile_fields) {
2661 if (isset($user->user_custom_profile_fields)) {
2662 foreach($user->user_custom_profile_fields as $udata) {
2663 /// If the profile field has data and the profile shortname-datatype is defined in server
2664 if ($udata->field_data) {
2665 if ($field = get_record('user_info_field', 'shortname', $udata->field_name, 'datatype', $udata->field_type)) {
2666 /// Insert the user_custom_profile_field
2667 $rec = new object();
2668 $rec->userid = $newid;
2669 $rec->fieldid = $field->id;
2670 $rec->data = $udata->field_data;
2671 insert_record('user_info_data', $rec);
2678 /// Here, if create_tags, do it as necessary
2679 if ($create_tags) {
2680 /// if tags are enabled and there are user tags
2681 if (!empty($CFG->usetags) && isset($user->user_tags)) {
2682 $tags = array();
2683 foreach($user->user_tags as $user_tag) {
2684 $tags[] = $user_tag->rawname;
2686 tag_set('user', $newid, $tags);
2690 //Here, if create_preferences, do it as necessary
2691 if ($create_preferences) {
2692 if (isset($user->user_preferences)) {
2693 foreach($user->user_preferences as $user_preference) {
2694 //We check if that user_preference exists in DB
2695 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
2696 //Prepare the record and insert it
2697 $user_preference->userid = $newid;
2698 $status = insert_record("user_preferences",$user_preference);
2704 //Do some output
2705 $counter++;
2706 if ($counter % 10 == 0) {
2707 if (!defined('RESTORE_SILENTLY')) {
2708 echo ".";
2709 if ($counter % 200 == 0) {
2710 echo "<br />";
2713 backup_flush(300);
2715 } /// End of loop over all the users loaded from xml
2718 return $status;
2721 //This function creates all the structures messages and contacts
2722 function restore_create_messages($restore,$xml_file) {
2724 global $CFG;
2726 $status = true;
2727 //Check it exists
2728 if (!file_exists($xml_file)) {
2729 $status = false;
2731 //Get info from xml
2732 if ($status) {
2733 //info will contain the id and name of every table
2734 //(message, message_read and message_contacts)
2735 //in backup_ids->info will be the real info (serialized)
2736 $info = restore_read_xml_messages($restore,$xml_file);
2738 //If we have info, then process messages & contacts
2739 if ($info > 0) {
2740 //Count how many we have
2741 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
2742 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
2743 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
2744 if ($unreadcount || $readcount || $contactcount) {
2745 //Start ul
2746 if (!defined('RESTORE_SILENTLY')) {
2747 echo '<ul>';
2749 //Number of records to get in every chunk
2750 $recordset_size = 4;
2752 //Process unread
2753 if ($unreadcount) {
2754 if (!defined('RESTORE_SILENTLY')) {
2755 echo '<li>'.get_string('unreadmessages','message').'</li>';
2757 $counter = 0;
2758 while ($counter < $unreadcount) {
2759 //Fetch recordset_size records in each iteration
2760 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2761 if ($recs) {
2762 foreach ($recs as $rec) {
2763 //Get the full record from backup_ids
2764 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
2765 if ($data) {
2766 //Now get completed xmlized object
2767 $info = $data->info;
2768 //traverse_xmlize($info); //Debug
2769 //print_object ($GLOBALS['traverse_array']); //Debug
2770 //$GLOBALS['traverse_array']=""; //Debug
2771 //Now build the MESSAGE record structure
2772 $dbrec = new object();
2773 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2774 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2775 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2776 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2777 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2778 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2779 //We have to recode the useridfrom field
2780 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2781 if ($user) {
2782 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2783 $dbrec->useridfrom = $user->new_id;
2785 //We have to recode the useridto field
2786 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2787 if ($user) {
2788 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2789 $dbrec->useridto = $user->new_id;
2791 //Check if the record doesn't exist in DB!
2792 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
2793 'useridto', $dbrec->useridto,
2794 'timecreated',$dbrec->timecreated);
2795 if (!$exist) {
2796 //Not exist. Insert
2797 $status = insert_record('message',$dbrec);
2798 } else {
2799 //Duplicate. Do nothing
2802 //Do some output
2803 $counter++;
2804 if ($counter % 10 == 0) {
2805 if (!defined('RESTORE_SILENTLY')) {
2806 echo ".";
2807 if ($counter % 200 == 0) {
2808 echo "<br />";
2811 backup_flush(300);
2818 //Process read
2819 if ($readcount) {
2820 if (!defined('RESTORE_SILENTLY')) {
2821 echo '<li>'.get_string('readmessages','message').'</li>';
2823 $counter = 0;
2824 while ($counter < $readcount) {
2825 //Fetch recordset_size records in each iteration
2826 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2827 if ($recs) {
2828 foreach ($recs as $rec) {
2829 //Get the full record from backup_ids
2830 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
2831 if ($data) {
2832 //Now get completed xmlized object
2833 $info = $data->info;
2834 //traverse_xmlize($info); //Debug
2835 //print_object ($GLOBALS['traverse_array']); //Debug
2836 //$GLOBALS['traverse_array']=""; //Debug
2837 //Now build the MESSAGE_READ record structure
2838 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2839 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2840 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2841 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2842 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2843 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2844 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2845 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2846 //We have to recode the useridfrom field
2847 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2848 if ($user) {
2849 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2850 $dbrec->useridfrom = $user->new_id;
2852 //We have to recode the useridto field
2853 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2854 if ($user) {
2855 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2856 $dbrec->useridto = $user->new_id;
2858 //Check if the record doesn't exist in DB!
2859 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
2860 'useridto', $dbrec->useridto,
2861 'timecreated',$dbrec->timecreated);
2862 if (!$exist) {
2863 //Not exist. Insert
2864 $status = insert_record('message_read',$dbrec);
2865 } else {
2866 //Duplicate. Do nothing
2869 //Do some output
2870 $counter++;
2871 if ($counter % 10 == 0) {
2872 if (!defined('RESTORE_SILENTLY')) {
2873 echo ".";
2874 if ($counter % 200 == 0) {
2875 echo "<br />";
2878 backup_flush(300);
2885 //Process contacts
2886 if ($contactcount) {
2887 if (!defined('RESTORE_SILENTLY')) {
2888 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2890 $counter = 0;
2891 while ($counter < $contactcount) {
2892 //Fetch recordset_size records in each iteration
2893 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2894 if ($recs) {
2895 foreach ($recs as $rec) {
2896 //Get the full record from backup_ids
2897 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
2898 if ($data) {
2899 //Now get completed xmlized object
2900 $info = $data->info;
2901 //traverse_xmlize($info); //Debug
2902 //print_object ($GLOBALS['traverse_array']); //Debug
2903 //$GLOBALS['traverse_array']=""; //Debug
2904 //Now build the MESSAGE_CONTACTS record structure
2905 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2906 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2907 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2908 //We have to recode the userid field
2909 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
2910 if ($user) {
2911 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2912 $dbrec->userid = $user->new_id;
2914 //We have to recode the contactid field
2915 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
2916 if ($user) {
2917 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2918 $dbrec->contactid = $user->new_id;
2920 //Check if the record doesn't exist in DB!
2921 $exist = get_record('message_contacts','userid',$dbrec->userid,
2922 'contactid', $dbrec->contactid);
2923 if (!$exist) {
2924 //Not exist. Insert
2925 $status = insert_record('message_contacts',$dbrec);
2926 } else {
2927 //Duplicate. Do nothing
2930 //Do some output
2931 $counter++;
2932 if ($counter % 10 == 0) {
2933 if (!defined('RESTORE_SILENTLY')) {
2934 echo ".";
2935 if ($counter % 200 == 0) {
2936 echo "<br />";
2939 backup_flush(300);
2945 if (!defined('RESTORE_SILENTLY')) {
2946 //End ul
2947 echo '</ul>';
2953 return $status;
2956 //This function creates all the structures for blogs and blog tags
2957 function restore_create_blogs($restore,$xml_file) {
2959 global $CFG;
2961 $status = true;
2962 //Check it exists
2963 if (!file_exists($xml_file)) {
2964 $status = false;
2966 //Get info from xml
2967 if ($status) {
2968 //info will contain the number of blogs in the backup file
2969 //in backup_ids->info will be the real info (serialized)
2970 $info = restore_read_xml_blogs($restore,$xml_file);
2972 //If we have info, then process blogs & blog_tags
2973 if ($info > 0) {
2974 //Count how many we have
2975 $blogcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'blog');
2976 if ($blogcount) {
2977 //Number of records to get in every chunk
2978 $recordset_size = 4;
2980 //Process blog
2981 if ($blogcount) {
2982 $counter = 0;
2983 while ($counter < $blogcount) {
2984 //Fetch recordset_size records in each iteration
2985 $recs = get_records_select("backup_ids","table_name = 'blog' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2986 if ($recs) {
2987 foreach ($recs as $rec) {
2988 //Get the full record from backup_ids
2989 $data = backup_getid($restore->backup_unique_code,"blog",$rec->old_id);
2990 if ($data) {
2991 //Now get completed xmlized object
2992 $info = $data->info;
2993 //traverse_xmlize($info); //Debug
2994 //print_object ($GLOBALS['traverse_array']); //Debug
2995 //$GLOBALS['traverse_array']=""; //Debug
2996 //Now build the BLOG record structure
2997 $dbrec = new object();
2998 $dbrec->module = backup_todb($info['BLOG']['#']['MODULE']['0']['#']);
2999 $dbrec->userid = backup_todb($info['BLOG']['#']['USERID']['0']['#']);
3000 $dbrec->courseid = backup_todb($info['BLOG']['#']['COURSEID']['0']['#']);
3001 $dbrec->groupid = backup_todb($info['BLOG']['#']['GROUPID']['0']['#']);
3002 $dbrec->moduleid = backup_todb($info['BLOG']['#']['MODULEID']['0']['#']);
3003 $dbrec->coursemoduleid = backup_todb($info['BLOG']['#']['COURSEMODULEID']['0']['#']);
3004 $dbrec->subject = backup_todb($info['BLOG']['#']['SUBJECT']['0']['#']);
3005 $dbrec->summary = backup_todb($info['BLOG']['#']['SUMMARY']['0']['#']);
3006 $dbrec->content = backup_todb($info['BLOG']['#']['CONTENT']['0']['#']);
3007 $dbrec->uniquehash = backup_todb($info['BLOG']['#']['UNIQUEHASH']['0']['#']);
3008 $dbrec->rating = backup_todb($info['BLOG']['#']['RATING']['0']['#']);
3009 $dbrec->format = backup_todb($info['BLOG']['#']['FORMAT']['0']['#']);
3010 $dbrec->attachment = backup_todb($info['BLOG']['#']['ATTACHMENT']['0']['#']);
3011 $dbrec->publishstate = backup_todb($info['BLOG']['#']['PUBLISHSTATE']['0']['#']);
3012 $dbrec->lastmodified = backup_todb($info['BLOG']['#']['LASTMODIFIED']['0']['#']);
3013 $dbrec->created = backup_todb($info['BLOG']['#']['CREATED']['0']['#']);
3014 $dbrec->usermodified = backup_todb($info['BLOG']['#']['USERMODIFIED']['0']['#']);
3016 //We have to recode the userid field
3017 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
3018 if ($user) {
3019 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
3020 $dbrec->userid = $user->new_id;
3023 //Check if the record doesn't exist in DB!
3024 $exist = get_record('post','userid', $dbrec->userid,
3025 'subject', $dbrec->subject,
3026 'created', $dbrec->created);
3027 $newblogid = 0;
3028 if (!$exist) {
3029 //Not exist. Insert
3030 $newblogid = insert_record('post',$dbrec);
3033 //Going to restore related tags. Check they are enabled and we have inserted a blog
3034 if ($CFG->usetags && $newblogid) {
3035 //Look for tags in this blog
3036 if (isset($info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'])) {
3037 $tagsarr = $info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'];
3038 //Iterate over tags
3039 $tags = array();
3040 for($i = 0; $i < sizeof($tagsarr); $i++) {
3041 $tag_info = $tagsarr[$i];
3042 ///traverse_xmlize($tag_info); //Debug
3043 ///print_object ($GLOBALS['traverse_array']); //Debug
3044 ///$GLOBALS['traverse_array']=""; //Debug
3046 $name = backup_todb($tag_info['#']['NAME']['0']['#']);
3047 $rawname = backup_todb($tag_info['#']['RAWNAME']['0']['#']);
3049 $tags[] = $rawname; //Rawname is all we need
3051 tag_set('post', $newblogid, $tags); //Add all the tags in one API call
3055 //Do some output
3056 $counter++;
3057 if ($counter % 10 == 0) {
3058 if (!defined('RESTORE_SILENTLY')) {
3059 echo ".";
3060 if ($counter % 200 == 0) {
3061 echo "<br />";
3064 backup_flush(300);
3074 return $status;
3077 //This function creates all the categories and questions
3078 //from xml
3079 function restore_create_questions($restore,$xml_file) {
3081 global $CFG, $db;
3083 $status = true;
3084 //Check it exists
3085 if (!file_exists($xml_file)) {
3086 $status = false;
3088 //Get info from xml
3089 if ($status) {
3090 //info will contain the old_id of every category
3091 //in backup_ids->info will be the real info (serialized)
3092 $info = restore_read_xml_questions($restore,$xml_file);
3094 //Now, if we have anything in info, we have to restore that
3095 //categories/questions
3096 if ($info) {
3097 if ($info !== true) {
3098 $status = $status && restore_question_categories($info, $restore);
3100 } else {
3101 $status = false;
3103 return $status;
3106 //This function creates all the scales
3107 function restore_create_scales($restore,$xml_file) {
3109 global $CFG, $db;
3111 $status = true;
3112 //Check it exists
3113 if (!file_exists($xml_file)) {
3114 $status = false;
3116 //Get info from xml
3117 if ($status) {
3118 //scales will contain the old_id of every scale
3119 //in backup_ids->info will be the real info (serialized)
3120 $scales = restore_read_xml_scales($restore,$xml_file);
3122 //Now, if we have anything in scales, we have to restore that
3123 //scales
3124 if ($scales) {
3125 //Get admin->id for later use
3126 $admin = get_admin();
3127 $adminid = $admin->id;
3128 if ($scales !== true) {
3129 //Iterate over each scale
3130 foreach ($scales as $scale) {
3131 //Get record from backup_ids
3132 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
3133 //Init variables
3134 $create_scale = false;
3136 if ($data) {
3137 //Now get completed xmlized object
3138 $info = $data->info;
3139 //traverse_xmlize($info); //Debug
3140 //print_object ($GLOBALS['traverse_array']); //Debug
3141 //$GLOBALS['traverse_array']=""; //Debug
3143 //Now build the SCALE record structure
3144 $sca = new object();
3145 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
3146 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
3147 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
3148 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
3149 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
3150 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
3152 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
3153 //or in restore->course_id course (Personal scale)
3154 if ($sca->courseid == 0) {
3155 $course_to_search = 0;
3156 } else {
3157 $course_to_search = $restore->course_id;
3160 // scale is not course unique, use get_record_sql to suppress warning
3162 $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale
3163 WHERE scale = '$sca->scale'
3164 AND courseid = $course_to_search", true);
3166 //If it doesn't exist, create
3167 if (!$sca_db) {
3168 $create_scale = true;
3170 //If we must create the scale
3171 if ($create_scale) {
3172 //Me must recode the courseid if it's <> 0 (common scale)
3173 if ($sca->courseid != 0) {
3174 $sca->courseid = $restore->course_id;
3176 //We must recode the userid
3177 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
3178 if ($user) {
3179 $sca->userid = $user->new_id;
3180 } else {
3181 //Assign it to admin
3182 $sca->userid = $adminid;
3184 //The structure is equal to the db, so insert the scale
3185 $newid = insert_record ("scale",$sca);
3186 } else {
3187 //get current scale id
3188 $newid = $sca_db->id;
3190 if ($newid) {
3191 //We have the newid, update backup_ids
3192 backup_putid($restore->backup_unique_code,"scale",
3193 $scale->id, $newid);
3198 } else {
3199 $status = false;
3201 return $status;
3205 * Recode group ID field, and set group ID based on restore options.
3206 * @return object Group object with new_id field.
3208 function restore_group_getid($restore, $groupid) {
3209 //We have to recode the groupid field
3210 $group = backup_getid($restore->backup_unique_code, 'groups', $groupid);
3212 if ($restore->groups == RESTORE_GROUPS_NONE or $restore->groups == RESTORE_GROUPINGS_ONLY) {
3213 $group->new_id = 0;
3215 return $group;
3219 * Recode grouping ID field, and set grouping ID based on restore options.
3220 * @return object Group object with new_id field.
3222 function restore_grouping_getid($restore, $groupingid) {
3223 //We have to recode the groupid field
3224 $grouping = backup_getid($restore->backup_unique_code, 'groupings', $groupingid);
3226 if ($restore->groups != RESTORE_GROUPS_GROUPINGS and $restore->groups != RESTORE_GROUPINGS_ONLY) {
3227 $grouping->new_id = 0;
3229 return $grouping;
3232 //This function creates all the groups
3233 function restore_create_groups($restore,$xml_file) {
3235 global $CFG;
3237 //Check it exists
3238 if (!file_exists($xml_file)) {
3239 return false;
3241 //Get info from xml
3242 if (!$groups = restore_read_xml_groups($restore,$xml_file)) {
3243 //groups will contain the old_id of every group
3244 //in backup_ids->info will be the real info (serialized)
3245 return false;
3247 } else if ($groups === true) {
3248 return true;
3251 $status = true;
3253 //Iterate over each group
3254 foreach ($groups as $group) {
3255 //Get record from backup_ids
3256 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
3258 if ($data) {
3259 //Now get completed xmlized object
3260 $info = $data->info;
3261 //traverse_xmlize($info); //Debug
3262 //print_object ($GLOBALS['traverse_array']); //Debug
3263 //$GLOBALS['traverse_array']=""; //Debug
3264 //Now build the GROUP record structure
3265 $gro = new Object();
3266 $gro->courseid = $restore->course_id;
3267 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
3268 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
3269 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
3270 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
3271 } else {
3272 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
3274 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
3275 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
3276 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
3277 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
3279 //Now search if that group exists (by name and description field) in
3280 //restore->course_id course
3281 //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
3282 $description_clause = '';
3283 if (!empty($gro->description)) { /// Only for groups having a description
3284 $literal_description = "'" . $gro->description . "'";
3285 $description_clause = " AND " .
3286 sql_compare_text('description') . " = " .
3287 sql_compare_text($literal_description);
3289 if (!$gro_db = get_record_sql("SELECT *
3290 FROM {$CFG->prefix}groups
3291 WHERE courseid = $restore->course_id AND
3292 name = '{$gro->name}'" . $description_clause, true)) {
3293 //If it doesn't exist, create
3294 $newid = insert_record('groups', $gro);
3296 } else {
3297 //get current group id
3298 $newid = $gro_db->id;
3301 if ($newid) {
3302 //We have the newid, update backup_ids
3303 backup_putid($restore->backup_unique_code,"groups", $group->id, $newid);
3304 } else {
3306 $status = false;
3307 continue;
3310 //Now restore members in the groups_members, only if
3311 //users are included
3312 if ($restore->users != 2) {
3313 if (!restore_create_groups_members($newid,$info,$restore)) {
3314 $status = false;
3320 //Now, restore group_files
3321 if ($status) {
3322 $status = restore_group_files($restore);
3325 return $status;
3328 //This function restores the groups_members
3329 function restore_create_groups_members($group_id,$info,$restore) {
3331 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
3332 //OK, some groups have no members.
3333 return true;
3335 //Get the members array
3336 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
3338 $status = true;
3340 //Iterate over members
3341 for($i = 0; $i < sizeof($members); $i++) {
3342 $mem_info = $members[$i];
3343 //traverse_xmlize($mem_info); //Debug
3344 //print_object ($GLOBALS['traverse_array']); //Debug
3345 //$GLOBALS['traverse_array']=""; //Debug
3347 //Now, build the GROUPS_MEMBERS record structure
3348 $group_member = new Object();
3349 $group_member->groupid = $group_id;
3350 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
3351 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
3353 $newid = false;
3355 //We have to recode the userid field
3356 if (!$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid)) {
3357 $status = false;
3358 continue;
3361 $group_member->userid = $user->new_id;
3363 //The structure is equal to the db, so insert the groups_members
3364 if (!insert_record ("groups_members", $group_member)) {
3365 $status = false;
3366 continue;
3369 //Do some output
3370 if (($i+1) % 50 == 0) {
3371 if (!defined('RESTORE_SILENTLY')) {
3372 echo ".";
3373 if (($i+1) % 1000 == 0) {
3374 echo "<br />";
3377 backup_flush(300);
3381 return $status;
3384 //This function creates all the groupings
3385 function restore_create_groupings($restore,$xml_file) {
3387 //Check it exists
3388 if (!file_exists($xml_file)) {
3389 return false;
3391 //Get info from xml
3392 if (!$groupings = restore_read_xml_groupings($restore,$xml_file)) {
3393 return false;
3395 } else if ($groupings === true) {
3396 return true;
3399 $status = true;
3401 //Iterate over each group
3402 foreach ($groupings as $grouping) {
3403 if ($data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id)) {
3404 //Now get completed xmlized object
3405 $info = $data->info;
3406 //Now build the GROUPING record structure
3407 $gro = new Object();
3408 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
3409 $gro->courseid = $restore->course_id;
3410 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
3411 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
3412 $gro->configdata = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
3413 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
3415 //Now search if that group exists (by name and description field) in
3416 if ($gro_db = get_record('groupings', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
3417 //get current group id
3418 $newid = $gro_db->id;
3420 } else {
3421 //The structure is equal to the db, so insert the grouping
3422 if (!$newid = insert_record('groupings', $gro)) {
3423 $status = false;
3424 continue;
3428 //We have the newid, update backup_ids
3429 backup_putid($restore->backup_unique_code,"groupings",
3430 $grouping->id, $newid);
3435 // now fix the defaultgroupingid in course
3436 $course = get_record('course', 'id', $restore->course_id);
3437 if ($course->defaultgroupingid) {
3438 if ($grouping = restore_grouping_getid($restore, $course->defaultgroupingid)) {
3439 set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
3440 } else {
3441 set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
3445 return $status;
3448 //This function creates all the groupingsgroups
3449 function restore_create_groupings_groups($restore,$xml_file) {
3451 //Check it exists
3452 if (!file_exists($xml_file)) {
3453 return false;
3455 //Get info from xml
3456 if (!$groupingsgroups = restore_read_xml_groupings_groups($restore,$xml_file)) {
3457 return false;
3459 } else if ($groupingsgroups === true) {
3460 return true;
3463 $status = true;
3465 //Iterate over each group
3466 foreach ($groupingsgroups as $groupinggroup) {
3467 if ($data = backup_getid($restore->backup_unique_code,"groupingsgroups",$groupinggroup->id)) {
3468 //Now get completed xmlized object
3469 $info = $data->info;
3470 //Now build the GROUPING record structure
3471 $gro_member = new Object();
3472 $gro_member->groupingid = backup_todb($info['GROUPINGGROUP']['#']['GROUPINGID']['0']['#']);
3473 $gro_member->groupid = backup_todb($info['GROUPINGGROUP']['#']['GROUPID']['0']['#']);
3474 $gro_member->timeadded = backup_todb($info['GROUPINGGROUP']['#']['TIMEADDED']['0']['#']);
3476 if (!$grouping = backup_getid($restore->backup_unique_code,"groupings",$gro_member->groupingid)) {
3477 $status = false;
3478 continue;
3481 if (!$group = backup_getid($restore->backup_unique_code,"groups",$gro_member->groupid)) {
3482 $status = false;
3483 continue;
3486 $gro_member->groupid = $group->new_id;
3487 $gro_member->groupingid = $grouping->new_id;
3488 if (!get_record('groupings_groups', 'groupid', $gro_member->groupid, 'groupingid', $gro_member->groupingid)) {
3489 if (!insert_record('groupings_groups', $gro_member)) {
3490 $status = false;
3496 return $status;
3499 //This function creates all the course events
3500 function restore_create_events($restore,$xml_file) {
3502 global $CFG, $db;
3504 $status = true;
3505 //Check it exists
3506 if (!file_exists($xml_file)) {
3507 $status = false;
3509 //Get info from xml
3510 if ($status) {
3511 //events will contain the old_id of every event
3512 //in backup_ids->info will be the real info (serialized)
3513 $events = restore_read_xml_events($restore,$xml_file);
3516 //Get admin->id for later use
3517 $admin = get_admin();
3518 $adminid = $admin->id;
3520 //Now, if we have anything in events, we have to restore that
3521 //events
3522 if ($events) {
3523 if ($events !== true) {
3524 //Iterate over each event
3525 foreach ($events as $event) {
3526 //Get record from backup_ids
3527 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
3528 //Init variables
3529 $create_event = false;
3531 if ($data) {
3532 //Now get completed xmlized object
3533 $info = $data->info;
3534 //traverse_xmlize($info); //Debug
3535 //print_object ($GLOBALS['traverse_array']); //Debug
3536 //$GLOBALS['traverse_array']=""; //Debug
3538 //if necessary, write to restorelog and adjust date/time fields
3539 if ($restore->course_startdateoffset) {
3540 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
3543 //Now build the EVENT record structure
3544 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
3545 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
3546 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
3547 $eve->courseid = $restore->course_id;
3548 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
3549 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
3550 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
3551 $eve->modulename = "";
3552 if (!empty($info['EVENT']['#']['MODULENAME'])) {
3553 $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
3555 $eve->instance = 0;
3556 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
3557 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
3558 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
3559 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
3560 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
3562 //Now search if that event exists (by name, description, timestart fields) in
3563 //restore->course_id course
3564 $eve_db = get_record_select("event",
3565 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
3566 //If it doesn't exist, create
3567 if (!$eve_db) {
3568 $create_event = true;
3570 //If we must create the event
3571 if ($create_event) {
3573 //We must recode the userid
3574 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
3575 if ($user) {
3576 $eve->userid = $user->new_id;
3577 } else {
3578 //Assign it to admin
3579 $eve->userid = $adminid;
3582 //We have to recode the groupid field
3583 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
3584 if ($group) {
3585 $eve->groupid = $group->new_id;
3586 } else {
3587 //Assign it to group 0
3588 $eve->groupid = 0;
3591 //The structure is equal to the db, so insert the event
3592 $newid = insert_record ("event",$eve);
3594 //We must recode the repeatid if the event has it
3595 //The repeatid now refers to the id of the original event. (see Bug#5956)
3596 if ($newid && !empty($eve->repeatid)) {
3597 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
3598 if ($repeat_rec) { //Exists, so use it...
3599 $eve->repeatid = $repeat_rec->new_id;
3600 } else { //Doesn't exists, calculate the next and save it
3601 $oldrepeatid = $eve->repeatid;
3602 $eve->repeatid = $newid;
3603 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
3605 $eve->id = $newid;
3606 // update the record to contain the correct repeatid
3607 update_record('event',$eve);
3609 } else {
3610 //get current event id
3611 $newid = $eve_db->id;
3613 if ($newid) {
3614 //We have the newid, update backup_ids
3615 backup_putid($restore->backup_unique_code,"event",
3616 $event->id, $newid);
3621 } else {
3622 $status = false;
3624 return $status;
3627 //This function decode things to make restore multi-site fully functional
3628 //It does this conversions:
3629 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
3630 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
3632 //Note: Inter-activities linking is being implemented as a final
3633 //step in the restore execution, because we need to have it
3634 //finished to know all the oldid, newid equivaleces
3635 function restore_decode_absolute_links($content) {
3637 global $CFG,$restore;
3639 /// MDL-14072: Prevent NULLs, empties and numbers to be processed by the
3640 /// heavy interlinking. Just a few cpu cycles saved.
3641 if ($content === NULL) {
3642 return NULL;
3643 } else if ($content === '') {
3644 return '';
3645 } else if (is_numeric($content)) {
3646 return $content;
3649 //Now decode wwwroot and file.php calls
3650 $search = array ("$@FILEPHP@$");
3652 //Check for the status of the slasharguments config variable
3653 $slash = $CFG->slasharguments;
3655 //Build the replace string as needed
3656 if ($slash == 1) {
3657 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
3658 } else {
3659 $replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
3662 $result = str_replace($search,$replace,$content);
3664 if ($result != $content && debugging()) { //Debug
3665 if (!defined('RESTORE_SILENTLY')) {
3666 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
3668 } //Debug
3670 return $result;
3673 //This function restores the userfiles from the temp (user_files) directory to the
3674 //dataroot/users directory
3675 function restore_user_files($restore) {
3677 global $CFG;
3679 $status = true;
3681 $counter = 0;
3683 // 'users' is the old users folder, 'user' is the new one, with a new hierarchy. Detect which one is here and treat accordingly
3684 //in CFG->dataroot
3685 $dest_dir = $CFG->dataroot."/user";
3686 $status = check_dir_exists($dest_dir,true);
3688 //Now, we iterate over "user_files" records to check if that user dir must be
3689 //copied (and renamed) to the "users" dir.
3690 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
3692 //Check if directory exists
3693 $userlist = array();
3695 if (is_dir($rootdir) && ($list = list_directories ($rootdir))) {
3696 $counter = 0;
3697 foreach ($list as $dir) {
3698 // If there are directories in this folder, we are in the new user hierarchy
3699 if ($newlist = list_directories("$rootdir/$dir")) {
3700 foreach ($newlist as $olduserid) {
3701 $userlist[$olduserid] = "$rootdir/$dir/$olduserid";
3703 } else {
3704 $userlist[$dir] = "$rootdir/$dir";
3708 foreach ($userlist as $olduserid => $backup_location) {
3709 //Look for dir like username in backup_ids
3710 //If that user exists in backup_ids
3711 if ($user = backup_getid($restore->backup_unique_code,"user",$olduserid)) {
3712 //Only if user has been created now or if it existed previously, but he hasn't got an image (see bug 1123)
3713 $newuserdir = make_user_directory($user->new_id, true); // Doesn't create the folder, just returns the location
3715 // restore images if new user or image does not exist yet
3716 if (!empty($user->new) or !check_dir_exists($newuserdir)) {
3717 if (make_user_directory($user->new_id)) { // Creates the folder
3718 $status = backup_copy_file($backup_location, $newuserdir, true);
3719 $counter ++;
3721 //Do some output
3722 if ($counter % 2 == 0) {
3723 if (!defined('RESTORE_SILENTLY')) {
3724 echo ".";
3725 if ($counter % 40 == 0) {
3726 echo "<br />";
3729 backup_flush(300);
3735 //If status is ok and whe have dirs created, returns counter to inform
3736 if ($status and $counter) {
3737 return $counter;
3738 } else {
3739 return $status;
3743 //This function restores the groupfiles from the temp (group_files) directory to the
3744 //dataroot/groups directory
3745 function restore_group_files($restore) {
3747 global $CFG;
3749 $status = true;
3751 $counter = 0;
3753 //First, we check to "groups" exists and create is as necessary
3754 //in CFG->dataroot
3755 $dest_dir = $CFG->dataroot.'/groups';
3756 $status = check_dir_exists($dest_dir,true);
3758 //Now, we iterate over "group_files" records to check if that user dir must be
3759 //copied (and renamed) to the "groups" dir.
3760 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
3761 //Check if directory exists
3762 if (is_dir($rootdir)) {
3763 $list = list_directories ($rootdir);
3764 if ($list) {
3765 //Iterate
3766 $counter = 0;
3767 foreach ($list as $dir) {
3768 //Look for dir like groupid in backup_ids
3769 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
3770 "table_name","groups",
3771 "old_id",$dir);
3772 //If that group exists in backup_ids
3773 if ($data) {
3774 if (!file_exists($dest_dir."/".$data->new_id)) {
3775 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
3776 $counter ++;
3778 //Do some output
3779 if ($counter % 2 == 0) {
3780 if (!defined('RESTORE_SILENTLY')) {
3781 echo ".";
3782 if ($counter % 40 == 0) {
3783 echo "<br />";
3786 backup_flush(300);
3792 //If status is ok and whe have dirs created, returns counter to inform
3793 if ($status and $counter) {
3794 return $counter;
3795 } else {
3796 return $status;
3800 //This function restores the course files from the temp (course_files) directory to the
3801 //dataroot/course_id directory
3802 function restore_course_files($restore) {
3804 global $CFG;
3806 $status = true;
3808 $counter = 0;
3810 //First, we check to "course_id" exists and create is as necessary
3811 //in CFG->dataroot
3812 $dest_dir = $CFG->dataroot."/".$restore->course_id;
3813 $status = check_dir_exists($dest_dir,true);
3815 //Now, we iterate over "course_files" records to check if that file/dir must be
3816 //copied to the "dest_dir" dir.
3817 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
3818 //Check if directory exists
3819 if (is_dir($rootdir)) {
3820 $list = list_directories_and_files ($rootdir);
3821 if ($list) {
3822 //Iterate
3823 $counter = 0;
3824 foreach ($list as $dir) {
3825 //Copy the dir to its new location
3826 //Only if destination file/dir doesn exists
3827 if (!file_exists($dest_dir."/".$dir)) {
3828 $status = backup_copy_file($rootdir."/".$dir,
3829 $dest_dir."/".$dir,true);
3830 $counter ++;
3832 //Do some output
3833 if ($counter % 2 == 0) {
3834 if (!defined('RESTORE_SILENTLY')) {
3835 echo ".";
3836 if ($counter % 40 == 0) {
3837 echo "<br />";
3840 backup_flush(300);
3845 //If status is ok and whe have dirs created, returns counter to inform
3846 if ($status and $counter) {
3847 return $counter;
3848 } else {
3849 return $status;
3853 //This function restores the site files from the temp (site_files) directory to the
3854 //dataroot/SITEID directory
3855 function restore_site_files($restore) {
3857 global $CFG;
3859 $status = true;
3861 $counter = 0;
3863 //First, we check to "course_id" exists and create is as necessary
3864 //in CFG->dataroot
3865 $dest_dir = $CFG->dataroot."/".SITEID;
3866 $status = check_dir_exists($dest_dir,true);
3868 //Now, we iterate over "site_files" files to check if that file/dir must be
3869 //copied to the "dest_dir" dir.
3870 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/site_files";
3871 //Check if directory exists
3872 if (is_dir($rootdir)) {
3873 $list = list_directories_and_files ($rootdir);
3874 if ($list) {
3875 //Iterate
3876 $counter = 0;
3877 foreach ($list as $dir) {
3878 //Copy the dir to its new location
3879 //Only if destination file/dir doesn exists
3880 if (!file_exists($dest_dir."/".$dir)) {
3881 $status = backup_copy_file($rootdir."/".$dir,
3882 $dest_dir."/".$dir,true);
3883 $counter ++;
3885 //Do some output
3886 if ($counter % 2 == 0) {
3887 if (!defined('RESTORE_SILENTLY')) {
3888 echo ".";
3889 if ($counter % 40 == 0) {
3890 echo "<br />";
3893 backup_flush(300);
3898 //If status is ok and whe have dirs created, returns counter to inform
3899 if ($status and $counter) {
3900 return $counter;
3901 } else {
3902 return $status;
3907 //This function creates all the structures for every module in backup file
3908 //Depending what has been selected.
3909 function restore_create_modules($restore,$xml_file) {
3911 global $CFG;
3912 $status = true;
3913 //Check it exists
3914 if (!file_exists($xml_file)) {
3915 $status = false;
3917 //Get info from xml
3918 if ($status) {
3919 //info will contain the id and modtype of every module
3920 //in backup_ids->info will be the real info (serialized)
3921 $info = restore_read_xml_modules($restore,$xml_file);
3923 //Now, if we have anything in info, we have to restore that mods
3924 //from backup_ids (calling every mod restore function)
3925 if ($info) {
3926 if ($info !== true) {
3927 if (!defined('RESTORE_SILENTLY')) {
3928 echo '<ul>';
3930 //Iterate over each module
3931 foreach ($info as $mod) {
3932 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
3933 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
3934 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
3935 $modrestore = $mod->modtype."_restore_mods";
3936 if (function_exists($modrestore)) { //Debug
3937 // we want to restore all mods even when one fails
3938 // incorrect code here ignored any errors during module restore in 1.6-1.8
3939 $status = $status && $modrestore($mod,$restore);
3940 } else {
3941 //Something was wrong. Function should exist.
3942 $status = false;
3946 if (!defined('RESTORE_SILENTLY')) {
3947 echo '</ul>';
3950 } else {
3951 $status = false;
3953 return $status;
3956 //This function creates all the structures for every log in backup file
3957 //Depending what has been selected.
3958 function restore_create_logs($restore,$xml_file) {
3960 global $CFG,$db;
3962 //Number of records to get in every chunk
3963 $recordset_size = 4;
3964 //Counter, points to current record
3965 $counter = 0;
3966 //To count all the recods to restore
3967 $count_logs = 0;
3969 $status = true;
3970 //Check it exists
3971 if (!file_exists($xml_file)) {
3972 $status = false;
3974 //Get info from xml
3975 if ($status) {
3976 //count_logs will contain the number of logs entries to process
3977 //in backup_ids->info will be the real info (serialized)
3978 $count_logs = restore_read_xml_logs($restore,$xml_file);
3981 //Now, if we have records in count_logs, we have to restore that logs
3982 //from backup_ids. This piece of code makes calls to:
3983 // - restore_log_course() if it's a course log
3984 // - restore_log_user() if it's a user log
3985 // - restore_log_module() if it's a module log.
3986 //And all is segmented in chunks to allow large recordsets to be restored !!
3987 if ($count_logs > 0) {
3988 while ($counter < $count_logs) {
3989 //Get a chunk of records
3990 //Take old_id twice to avoid adodb limitation
3991 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3992 //We have logs
3993 if ($logs) {
3994 //Iterate
3995 foreach ($logs as $log) {
3996 //Get the full record from backup_ids
3997 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
3998 if ($data) {
3999 //Now get completed xmlized object
4000 $info = $data->info;
4001 //traverse_xmlize($info); //Debug
4002 //print_object ($GLOBALS['traverse_array']); //Debug
4003 //$GLOBALS['traverse_array']=""; //Debug
4004 //Now build the LOG record structure
4005 $dblog = new object();
4006 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
4007 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
4008 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
4009 $dblog->course = $restore->course_id;
4010 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
4011 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
4012 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
4013 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
4014 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
4015 //We have to recode the userid field
4016 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
4017 if ($user) {
4018 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
4019 $dblog->userid = $user->new_id;
4021 //We have to recode the cmid field (if module isn't "course" or "user")
4022 if ($dblog->module != "course" and $dblog->module != "user") {
4023 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
4024 if ($cm) {
4025 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
4026 $dblog->cmid = $cm->new_id;
4027 } else {
4028 $dblog->cmid = 0;
4031 //print_object ($dblog); //Debug
4032 //Now, we redirect to the needed function to make all the work
4033 if ($dblog->module == "course") {
4034 //It's a course log,
4035 $stat = restore_log_course($restore,$dblog);
4036 } elseif ($dblog->module == "user") {
4037 //It's a user log,
4038 $stat = restore_log_user($restore,$dblog);
4039 } else {
4040 //It's a module log,
4041 $stat = restore_log_module($restore,$dblog);
4045 //Do some output
4046 $counter++;
4047 if ($counter % 10 == 0) {
4048 if (!defined('RESTORE_SILENTLY')) {
4049 echo ".";
4050 if ($counter % 200 == 0) {
4051 echo "<br />";
4054 backup_flush(300);
4057 } else {
4058 //We never should arrive here
4059 $counter = $count_logs;
4060 $status = false;
4065 return $status;
4068 //This function inserts a course log record, calculating the URL field as necessary
4069 function restore_log_course($restore,$log) {
4071 $status = true;
4072 $toinsert = false;
4074 //echo "<hr />Before transformations<br />"; //Debug
4075 //print_object($log); //Debug
4076 //Depending of the action, we recode different things
4077 switch ($log->action) {
4078 case "view":
4079 $log->url = "view.php?id=".$log->course;
4080 $log->info = $log->course;
4081 $toinsert = true;
4082 break;
4083 case "guest":
4084 $log->url = "view.php?id=".$log->course;
4085 $toinsert = true;
4086 break;
4087 case "user report":
4088 //recode the info field (it's the user id)
4089 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4090 if ($user) {
4091 $log->info = $user->new_id;
4092 //Now, extract the mode from the url field
4093 $mode = substr(strrchr($log->url,"="),1);
4094 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
4095 $toinsert = true;
4097 break;
4098 case "add mod":
4099 //Extract the course_module from the url field
4100 $cmid = substr(strrchr($log->url,"="),1);
4101 //recode the course_module to see it it has been restored
4102 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
4103 if ($cm) {
4104 $cmid = $cm->new_id;
4105 //Extract the module name and the module id from the info field
4106 $modname = strtok($log->info," ");
4107 $modid = strtok(" ");
4108 //recode the module id to see if it has been restored
4109 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
4110 if ($mod) {
4111 $modid = $mod->new_id;
4112 //Now I have everything so reconstruct url and info
4113 $log->info = $modname." ".$modid;
4114 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
4115 $toinsert = true;
4118 break;
4119 case "update mod":
4120 //Extract the course_module from the url field
4121 $cmid = substr(strrchr($log->url,"="),1);
4122 //recode the course_module to see it it has been restored
4123 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
4124 if ($cm) {
4125 $cmid = $cm->new_id;
4126 //Extract the module name and the module id from the info field
4127 $modname = strtok($log->info," ");
4128 $modid = strtok(" ");
4129 //recode the module id to see if it has been restored
4130 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
4131 if ($mod) {
4132 $modid = $mod->new_id;
4133 //Now I have everything so reconstruct url and info
4134 $log->info = $modname." ".$modid;
4135 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
4136 $toinsert = true;
4139 break;
4140 case "delete mod":
4141 $log->url = "view.php?id=".$log->course;
4142 $toinsert = true;
4143 break;
4144 case "update":
4145 $log->url = "edit.php?id=".$log->course;
4146 $log->info = "";
4147 $toinsert = true;
4148 break;
4149 case "unenrol":
4150 //recode the info field (it's the user id)
4151 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4152 if ($user) {
4153 $log->info = $user->new_id;
4154 $log->url = "view.php?id=".$log->course;
4155 $toinsert = true;
4157 break;
4158 case "enrol":
4159 //recode the info field (it's the user id)
4160 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4161 if ($user) {
4162 $log->info = $user->new_id;
4163 $log->url = "view.php?id=".$log->course;
4164 $toinsert = true;
4166 break;
4167 case "editsection":
4168 //Extract the course_section from the url field
4169 $secid = substr(strrchr($log->url,"="),1);
4170 //recode the course_section to see if it has been restored
4171 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
4172 if ($sec) {
4173 $secid = $sec->new_id;
4174 //Now I have everything so reconstruct url and info
4175 $log->url = "editsection.php?id=".$secid;
4176 $toinsert = true;
4178 break;
4179 case "new":
4180 $log->url = "view.php?id=".$log->course;
4181 $log->info = "";
4182 $toinsert = true;
4183 break;
4184 case "recent":
4185 $log->url = "recent.php?id=".$log->course;
4186 $log->info = "";
4187 $toinsert = true;
4188 break;
4189 case "report log":
4190 $log->url = "report/log/index.php?id=".$log->course;
4191 $log->info = $log->course;
4192 $toinsert = true;
4193 break;
4194 case "report live":
4195 $log->url = "report/log/live.php?id=".$log->course;
4196 $log->info = $log->course;
4197 $toinsert = true;
4198 break;
4199 case "report outline":
4200 $log->url = "report/outline/index.php?id=".$log->course;
4201 $log->info = $log->course;
4202 $toinsert = true;
4203 break;
4204 case "report participation":
4205 $log->url = "report/participation/index.php?id=".$log->course;
4206 $log->info = $log->course;
4207 $toinsert = true;
4208 break;
4209 case "report stats":
4210 $log->url = "report/stats/index.php?id=".$log->course;
4211 $log->info = $log->course;
4212 $toinsert = true;
4213 break;
4214 default:
4215 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
4216 break;
4219 //echo "After transformations<br />"; //Debug
4220 //print_object($log); //Debug
4222 //Now if $toinsert is set, insert the record
4223 if ($toinsert) {
4224 //echo "Inserting record<br />"; //Debug
4225 $status = insert_record("log",$log);
4227 return $status;
4230 //This function inserts a user log record, calculating the URL field as necessary
4231 function restore_log_user($restore,$log) {
4233 $status = true;
4234 $toinsert = false;
4236 //echo "<hr />Before transformations<br />"; //Debug
4237 //print_object($log); //Debug
4238 //Depending of the action, we recode different things
4239 switch ($log->action) {
4240 case "view":
4241 //recode the info field (it's the user id)
4242 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4243 if ($user) {
4244 $log->info = $user->new_id;
4245 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4246 $toinsert = true;
4248 break;
4249 case "change password":
4250 //recode the info field (it's the user id)
4251 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4252 if ($user) {
4253 $log->info = $user->new_id;
4254 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4255 $toinsert = true;
4257 break;
4258 case "login":
4259 //recode the info field (it's the user id)
4260 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4261 if ($user) {
4262 $log->info = $user->new_id;
4263 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4264 $toinsert = true;
4266 break;
4267 case "logout":
4268 //recode the info field (it's the user id)
4269 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
4270 if ($user) {
4271 $log->info = $user->new_id;
4272 $log->url = "view.php?id=".$log->info."&course=".$log->course;
4273 $toinsert = true;
4275 break;
4276 case "view all":
4277 $log->url = "view.php?id=".$log->course;
4278 $log->info = "";
4279 $toinsert = true;
4280 case "update":
4281 //We split the url by ampersand char
4282 $first_part = strtok($log->url,"&");
4283 //Get data after the = char. It's the user being updated
4284 $userid = substr(strrchr($first_part,"="),1);
4285 //Recode the user
4286 $user = backup_getid($restore->backup_unique_code,"user",$userid);
4287 if ($user) {
4288 $log->info = "";
4289 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
4290 $toinsert = true;
4292 break;
4293 default:
4294 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
4295 break;
4298 //echo "After transformations<br />"; //Debug
4299 //print_object($log); //Debug
4301 //Now if $toinsert is set, insert the record
4302 if ($toinsert) {
4303 //echo "Inserting record<br />"; //Debug
4304 $status = insert_record("log",$log);
4306 return $status;
4309 //This function inserts a module log record, calculating the URL field as necessary
4310 function restore_log_module($restore,$log) {
4312 $status = true;
4313 $toinsert = false;
4315 //echo "<hr />Before transformations<br />"; //Debug
4316 //print_object($log); //Debug
4318 //Now we see if the required function in the module exists
4319 $function = $log->module."_restore_logs";
4320 if (function_exists($function)) {
4321 //Call the function
4322 $log = $function($restore,$log);
4323 //If everything is ok, mark the insert flag
4324 if ($log) {
4325 $toinsert = true;
4329 //echo "After transformations<br />"; //Debug
4330 //print_object($log); //Debug
4332 //Now if $toinsert is set, insert the record
4333 if ($toinsert) {
4334 //echo "Inserting record<br />"; //Debug
4335 $status = insert_record("log",$log);
4337 return $status;
4340 //This function adjusts the instance field into course_modules. It's executed after
4341 //modules restore. There, we KNOW the new instance id !!
4342 function restore_check_instances($restore) {
4344 global $CFG;
4346 $status = true;
4348 //We are going to iterate over each course_module saved in backup_ids
4349 $course_modules = get_records_sql("SELECT old_id,new_id
4350 FROM {$CFG->prefix}backup_ids
4351 WHERE backup_code = '$restore->backup_unique_code' AND
4352 table_name = 'course_modules'");
4353 if ($course_modules) {
4354 foreach($course_modules as $cm) {
4355 //Get full record, using backup_getids
4356 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
4357 //Now we are going to the REAL course_modules to get its type (field module)
4358 $module = get_record("course_modules","id",$cm_module->new_id);
4359 if ($module) {
4360 //We know the module type id. Get the name from modules
4361 $type = get_record("modules","id",$module->module);
4362 if ($type) {
4363 //We know the type name and the old_id. Get its new_id
4364 //from backup_ids. It's the instance !!!
4365 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
4366 if ($instance) {
4367 //We have the new instance, so update the record in course_modules
4368 $module->instance = $instance->new_id;
4369 //print_object ($module); //Debug
4370 $status = update_record("course_modules",$module);
4371 } else {
4372 $status = false;
4374 } else {
4375 $status = false;
4377 } else {
4378 $status = false;
4381 /// Finally, calculate modinfo cache.
4382 rebuild_course_cache($restore->course_id);
4386 return $status;
4389 //=====================================================================================
4390 //== ==
4391 //== XML Functions (SAX) ==
4392 //== ==
4393 //=====================================================================================
4395 //This is the class used to do all the xml parse
4396 class MoodleParser {
4398 var $level = 0; //Level we are
4399 var $counter = 0; //Counter
4400 var $tree = array(); //Array of levels we are
4401 var $content = ""; //Content under current level
4402 var $todo = ""; //What we hav to do when parsing
4403 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
4404 var $temp = ""; //Temp storage.
4405 var $preferences = ""; //Preferences about what to load !!
4406 var $finished = false; //Flag to say xml_parse to stop
4408 //This function is used to get the current contents property value
4409 //They are trimed (and converted from utf8 if needed)
4410 function getContents() {
4411 return trim($this->content);
4414 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
4415 function startElementInfo($parser, $tagName, $attrs) {
4416 //Refresh properties
4417 $this->level++;
4418 $this->tree[$this->level] = $tagName;
4420 //Output something to avoid browser timeouts...
4421 //backup_flush();
4423 //Check if we are into INFO zone
4424 //if ($this->tree[2] == "INFO") //Debug
4425 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4428 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
4429 function startElementRoles($parser, $tagName, $attrs) {
4430 //Refresh properties
4431 $this->level++;
4432 $this->tree[$this->level] = $tagName;
4434 //Output something to avoid browser timeouts...
4435 //backup_flush();
4437 //Check if we are into INFO zone
4438 //if ($this->tree[2] == "INFO") //Debug
4439 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4443 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
4444 function startElementCourseHeader($parser, $tagName, $attrs) {
4445 //Refresh properties
4446 $this->level++;
4447 $this->tree[$this->level] = $tagName;
4449 //Output something to avoid browser timeouts...
4450 //backup_flush();
4452 //Check if we are into COURSE_HEADER zone
4453 //if ($this->tree[3] == "HEADER") //Debug
4454 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4457 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
4458 function startElementBlocks($parser, $tagName, $attrs) {
4459 //Refresh properties
4460 $this->level++;
4461 $this->tree[$this->level] = $tagName;
4463 //Output something to avoid browser timeouts...
4464 //backup_flush();
4466 //Check if we are into BLOCKS zone
4467 //if ($this->tree[3] == "BLOCKS") //Debug
4468 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4470 //If we are under a BLOCK tag under a BLOCKS zone, accumule it
4471 if (isset($this->tree[4]) and isset($this->tree[3])) { //
4472 if ($this->tree[4] == "BLOCK" and $this->tree[3] == "BLOCKS") {
4473 if (!isset($this->temp)) {
4474 $this->temp = "";
4476 $this->temp .= "<".$tagName.">";
4481 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
4482 function startElementSections($parser, $tagName, $attrs) {
4483 //Refresh properties
4484 $this->level++;
4485 $this->tree[$this->level] = $tagName;
4487 //Output something to avoid browser timeouts...
4488 //backup_flush();
4490 //Check if we are into SECTIONS zone
4491 //if ($this->tree[3] == "SECTIONS") //Debug
4492 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4495 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4496 function startElementFormatData($parser, $tagName, $attrs) {
4497 //Refresh properties
4498 $this->level++;
4499 $this->tree[$this->level] = $tagName;
4501 //Output something to avoid browser timeouts...
4502 //backup_flush();
4504 //Accumulate all the data inside this tag
4505 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
4506 if (!isset($this->temp)) {
4507 $this->temp = '';
4509 $this->temp .= "<".$tagName.">";
4512 //Check if we are into FORMATDATA zone
4513 //if ($this->tree[3] == "FORMATDATA") //Debug
4514 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4517 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4518 function startElementMetacourse($parser, $tagName, $attrs) {
4520 //Refresh properties
4521 $this->level++;
4522 $this->tree[$this->level] = $tagName;
4524 //Output something to avoid browser timeouts...
4525 //backup_flush();
4527 //Check if we are into METACOURSE zone
4528 //if ($this->tree[3] == "METACOURSE") //Debug
4529 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4532 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4533 function startElementGradebook($parser, $tagName, $attrs) {
4535 //Refresh properties
4536 $this->level++;
4537 $this->tree[$this->level] = $tagName;
4539 //Output something to avoid browser timeouts...
4540 //backup_flush();
4542 //Check if we are into GRADEBOOK zone
4543 //if ($this->tree[3] == "GRADEBOOK") //Debug
4544 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4546 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4547 if (isset($this->tree[5]) and isset($this->tree[3])) {
4548 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")) {
4550 if (!isset($this->temp)) {
4551 $this->temp = "";
4553 $this->temp .= "<".$tagName.">";
4558 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4559 function startElementOldGradebook($parser, $tagName, $attrs) {
4561 //Refresh properties
4562 $this->level++;
4563 $this->tree[$this->level] = $tagName;
4565 //Output something to avoid browser timeouts...
4566 //backup_flush();
4568 //Check if we are into GRADEBOOK zone
4569 //if ($this->tree[3] == "GRADEBOOK") //Debug
4570 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4572 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4573 if (isset($this->tree[5]) and isset($this->tree[3])) {
4574 if (($this->tree[5] == "GRADE_PREFERENCE" || $this->tree[5] == "GRADE_LETTER" || $this->tree[5] == "GRADE_CATEGORY" ) && ($this->tree[3] == "GRADEBOOK")) {
4575 if (!isset($this->temp)) {
4576 $this->temp = "";
4578 $this->temp .= "<".$tagName.">";
4584 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
4585 function startElementUsers($parser, $tagName, $attrs) {
4586 //Refresh properties
4587 $this->level++;
4588 $this->tree[$this->level] = $tagName;
4590 //Check if we are into USERS zone
4591 //if ($this->tree[3] == "USERS") //Debug
4592 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4595 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
4596 function startElementMessages($parser, $tagName, $attrs) {
4597 //Refresh properties
4598 $this->level++;
4599 $this->tree[$this->level] = $tagName;
4601 //Output something to avoid browser timeouts...
4602 //backup_flush();
4604 //Check if we are into MESSAGES zone
4605 //if ($this->tree[3] == "MESSAGES") //Debug
4606 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4608 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
4609 if (isset($this->tree[4]) and isset($this->tree[3])) {
4610 if (($this->tree[4] == "MESSAGE" || (isset($this->tree[5]) && $this->tree[5] == "CONTACT" )) and ($this->tree[3] == "MESSAGES")) {
4611 if (!isset($this->temp)) {
4612 $this->temp = "";
4614 $this->temp .= "<".$tagName.">";
4619 //This is the startTag handler we use where we are reading the blogs zone (todo="BLOGS")
4620 function startElementBlogs($parser, $tagName, $attrs) {
4621 //Refresh properties
4622 $this->level++;
4623 $this->tree[$this->level] = $tagName;
4625 //Output something to avoid browser timeouts...
4626 //backup_flush();
4628 //Check if we are into BLOGS zone
4629 //if ($this->tree[3] == "BLOGS") //Debug
4630 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4632 //If we are under a BLOG tag under a BLOGS zone, accumule it
4633 if (isset($this->tree[4]) and isset($this->tree[3])) {
4634 if ($this->tree[4] == "BLOG" and $this->tree[3] == "BLOGS") {
4635 if (!isset($this->temp)) {
4636 $this->temp = "";
4638 $this->temp .= "<".$tagName.">";
4643 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
4644 function startElementQuestions($parser, $tagName, $attrs) {
4645 //Refresh properties
4646 $this->level++;
4647 $this->tree[$this->level] = $tagName;
4649 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
4650 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
4651 //} //Debug
4653 //Output something to avoid browser timeouts...
4654 //backup_flush();
4656 //Check if we are into QUESTION_CATEGORIES zone
4657 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
4658 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4660 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
4661 if (isset($this->tree[4]) and isset($this->tree[3])) {
4662 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
4663 if (!isset($this->temp)) {
4664 $this->temp = "";
4666 $this->temp .= "<".$tagName.">";
4671 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
4672 function startElementScales($parser, $tagName, $attrs) {
4673 //Refresh properties
4674 $this->level++;
4675 $this->tree[$this->level] = $tagName;
4677 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
4678 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
4679 //} //Debug
4681 //Output something to avoid browser timeouts...
4682 //backup_flush();
4684 //Check if we are into SCALES zone
4685 //if ($this->tree[3] == "SCALES") //Debug
4686 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4688 //If we are under a SCALE tag under a SCALES zone, accumule it
4689 if (isset($this->tree[4]) and isset($this->tree[3])) {
4690 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
4691 if (!isset($this->temp)) {
4692 $this->temp = "";
4694 $this->temp .= "<".$tagName.">";
4699 function startElementGroups($parser, $tagName, $attrs) {
4700 //Refresh properties
4701 $this->level++;
4702 $this->tree[$this->level] = $tagName;
4704 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
4705 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
4706 //} //Debug
4708 //Output something to avoid browser timeouts...
4709 //backup_flush();
4711 //Check if we are into GROUPS zone
4712 //if ($this->tree[3] == "GROUPS") //Debug
4713 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4715 //If we are under a GROUP tag under a GROUPS zone, accumule it
4716 if (isset($this->tree[4]) and isset($this->tree[3])) {
4717 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
4718 if (!isset($this->temp)) {
4719 $this->temp = "";
4721 $this->temp .= "<".$tagName.">";
4726 function startElementGroupings($parser, $tagName, $attrs) {
4727 //Refresh properties
4728 $this->level++;
4729 $this->tree[$this->level] = $tagName;
4731 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
4732 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
4733 //} //Debug
4735 //Output something to avoid browser timeouts...
4736 //backup_flush();
4738 //Check if we are into GROUPINGS zone
4739 //if ($this->tree[3] == "GROUPINGS") //Debug
4740 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4742 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
4743 if (isset($this->tree[4]) and isset($this->tree[3])) {
4744 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
4745 if (!isset($this->temp)) {
4746 $this->temp = "";
4748 $this->temp .= "<".$tagName.">";
4753 function startElementGroupingsGroups($parser, $tagName, $attrs) {
4754 //Refresh properties
4755 $this->level++;
4756 $this->tree[$this->level] = $tagName;
4758 //if ($tagName == "GROUPINGGROUP" && $this->tree[3] == "GROUPINGSGROUPS") { //Debug
4759 // echo "<P>GROUPINGSGROUP: ".strftime ("%X",time()),"-"; //Debug
4760 //} //Debug
4762 //Output something to avoid browser timeouts...
4763 backup_flush();
4765 //Check if we are into GROUPINGSGROUPS zone
4766 //if ($this->tree[3] == "GROUPINGSGROUPS") //Debug
4767 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4769 //If we are under a GROUPINGGROUP tag under a GROUPINGSGROUPS zone, accumule it
4770 if (isset($this->tree[4]) and isset($this->tree[3])) {
4771 if (($this->tree[4] == "GROUPINGGROUP") and ($this->tree[3] == "GROUPINGSGROUPS")) {
4772 if (!isset($this->temp)) {
4773 $this->temp = "";
4775 $this->temp .= "<".$tagName.">";
4780 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
4781 function startElementEvents($parser, $tagName, $attrs) {
4782 //Refresh properties
4783 $this->level++;
4784 $this->tree[$this->level] = $tagName;
4786 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
4787 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
4788 //} //Debug
4790 //Output something to avoid browser timeouts...
4791 //backup_flush();
4793 //Check if we are into EVENTS zone
4794 //if ($this->tree[3] == "EVENTS") //Debug
4795 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4797 //If we are under a EVENT tag under a EVENTS zone, accumule it
4798 if (isset($this->tree[4]) and isset($this->tree[3])) {
4799 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
4800 if (!isset($this->temp)) {
4801 $this->temp = "";
4803 $this->temp .= "<".$tagName.">";
4808 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
4809 function startElementModules($parser, $tagName, $attrs) {
4810 //Refresh properties
4811 $this->level++;
4812 $this->tree[$this->level] = $tagName;
4814 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
4815 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
4816 //} //Debug
4818 //Output something to avoid browser timeouts...
4819 //backup_flush();
4821 //Check if we are into MODULES zone
4822 //if ($this->tree[3] == "MODULES") //Debug
4823 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4825 //If we are under a MOD tag under a MODULES zone, accumule it
4826 if (isset($this->tree[4]) and isset($this->tree[3])) {
4827 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
4828 if (!isset($this->temp)) {
4829 $this->temp = "";
4831 $this->temp .= "<".$tagName.">";
4836 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
4837 function startElementLogs($parser, $tagName, $attrs) {
4838 //Refresh properties
4839 $this->level++;
4840 $this->tree[$this->level] = $tagName;
4842 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
4843 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
4844 //} //Debug
4846 //Output something to avoid browser timeouts...
4847 //backup_flush();
4849 //Check if we are into LOGS zone
4850 //if ($this->tree[3] == "LOGS") //Debug
4851 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4853 //If we are under a LOG tag under a LOGS zone, accumule it
4854 if (isset($this->tree[4]) and isset($this->tree[3])) {
4855 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
4856 if (!isset($this->temp)) {
4857 $this->temp = "";
4859 $this->temp .= "<".$tagName.">";
4864 //This is the startTag default handler we use when todo is undefined
4865 function startElement($parser, $tagName, $attrs) {
4866 $this->level++;
4867 $this->tree[$this->level] = $tagName;
4869 //Output something to avoid browser timeouts...
4870 //backup_flush();
4872 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4875 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
4876 function endElementInfo($parser, $tagName) {
4877 //Check if we are into INFO zone
4878 if ($this->tree[2] == "INFO") {
4879 //if (trim($this->content)) //Debug
4880 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4881 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4882 //Dependig of different combinations, do different things
4883 if ($this->level == 3) {
4884 switch ($tagName) {
4885 case "NAME":
4886 $this->info->backup_name = $this->getContents();
4887 break;
4888 case "MOODLE_VERSION":
4889 $this->info->backup_moodle_version = $this->getContents();
4890 break;
4891 case "MOODLE_RELEASE":
4892 $this->info->backup_moodle_release = $this->getContents();
4893 break;
4894 case "BACKUP_VERSION":
4895 $this->info->backup_backup_version = $this->getContents();
4896 break;
4897 case "BACKUP_RELEASE":
4898 $this->info->backup_backup_release = $this->getContents();
4899 break;
4900 case "DATE":
4901 $this->info->backup_date = $this->getContents();
4902 break;
4903 case "ORIGINAL_WWWROOT":
4904 $this->info->original_wwwroot = $this->getContents();
4905 break;
4906 case "MNET_EXTERNALUSERS":
4907 $this->info->mnet_externalusers = $this->getContents();
4908 break;
4911 if ($this->tree[3] == "DETAILS") {
4912 if ($this->level == 4) {
4913 switch ($tagName) {
4914 case "METACOURSE":
4915 $this->info->backup_metacourse = $this->getContents();
4916 break;
4917 case "USERS":
4918 $this->info->backup_users = $this->getContents();
4919 break;
4920 case "LOGS":
4921 $this->info->backup_logs = $this->getContents();
4922 break;
4923 case "USERFILES":
4924 $this->info->backup_user_files = $this->getContents();
4925 break;
4926 case "COURSEFILES":
4927 $this->info->backup_course_files = $this->getContents();
4928 break;
4929 case "SITEFILES":
4930 $this->info->backup_site_files = $this->getContents();
4931 break;
4932 case "GRADEBOOKHISTORIES":
4933 $this->info->gradebook_histories = $this->getContents();
4934 break;
4935 case "MESSAGES":
4936 $this->info->backup_messages = $this->getContents();
4937 break;
4938 case "BLOGS":
4939 $this->info->backup_blogs = $this->getContents();
4940 break;
4941 case 'BLOCKFORMAT':
4942 $this->info->backup_block_format = $this->getContents();
4943 break;
4946 if ($this->level == 5) {
4947 switch ($tagName) {
4948 case "NAME":
4949 $this->info->tempName = $this->getContents();
4950 break;
4951 case "INCLUDED":
4952 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
4953 break;
4954 case "USERINFO":
4955 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
4956 break;
4959 if ($this->level == 7) {
4960 switch ($tagName) {
4961 case "ID":
4962 $this->info->tempId = $this->getContents();
4963 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
4964 break;
4965 case "NAME":
4966 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
4967 break;
4968 case "INCLUDED":
4969 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
4970 break;
4971 case "USERINFO":
4972 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
4973 break;
4979 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4980 //Speed up a lot (avoid parse all)
4981 if ($tagName == "INFO") {
4982 $this->finished = true;
4985 //Clear things
4986 $this->tree[$this->level] = "";
4987 $this->level--;
4988 $this->content = "";
4992 function endElementRoles($parser, $tagName) {
4993 //Check if we are into INFO zone
4994 if ($this->tree[2] == "ROLES") {
4996 if ($this->tree[3] == "ROLE") {
4997 if ($this->level == 4) {
4998 switch ($tagName) {
4999 case "ID": // this is the old id
5000 $this->info->tempid = $this->getContents();
5001 $this->info->roles[$this->info->tempid]->id = $this->info->tempid;
5002 break;
5003 case "NAME":
5004 $this->info->roles[$this->info->tempid]->name = $this->getContents();;
5005 break;
5006 case "SHORTNAME":
5007 $this->info->roles[$this->info->tempid]->shortname = $this->getContents();;
5008 break;
5009 case "NAMEINCOURSE": // custom name of the role in course
5010 $this->info->roles[$this->info->tempid]->nameincourse = $this->getContents();;
5011 break;
5014 if ($this->level == 6) {
5015 switch ($tagName) {
5016 case "NAME":
5017 $this->info->tempcapname = $this->getContents();
5018 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
5019 break;
5020 case "PERMISSION":
5021 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
5022 break;
5023 case "TIMEMODIFIED":
5024 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
5025 break;
5026 case "MODIFIERID":
5027 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
5028 break;
5034 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
5035 //Speed up a lot (avoid parse all)
5036 if ($tagName == "ROLES") {
5037 $this->finished = true;
5040 //Clear things
5041 $this->tree[$this->level] = "";
5042 $this->level--;
5043 $this->content = "";
5047 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
5048 function endElementCourseHeader($parser, $tagName) {
5049 //Check if we are into COURSE_HEADER zone
5050 if ($this->tree[3] == "HEADER") {
5051 //if (trim($this->content)) //Debug
5052 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5053 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5054 //Dependig of different combinations, do different things
5055 if ($this->level == 4) {
5056 switch ($tagName) {
5057 case "ID":
5058 $this->info->course_id = $this->getContents();
5059 break;
5060 case "PASSWORD":
5061 $this->info->course_password = $this->getContents();
5062 break;
5063 case "FULLNAME":
5064 $this->info->course_fullname = $this->getContents();
5065 break;
5066 case "SHORTNAME":
5067 $this->info->course_shortname = $this->getContents();
5068 break;
5069 case "IDNUMBER":
5070 $this->info->course_idnumber = $this->getContents();
5071 break;
5072 case "SUMMARY":
5073 $this->info->course_summary = $this->getContents();
5074 break;
5075 case "FORMAT":
5076 $this->info->course_format = $this->getContents();
5077 break;
5078 case "SHOWGRADES":
5079 $this->info->course_showgrades = $this->getContents();
5080 break;
5081 case "BLOCKINFO":
5082 $this->info->blockinfo = $this->getContents();
5083 break;
5084 case "NEWSITEMS":
5085 $this->info->course_newsitems = $this->getContents();
5086 break;
5087 case "TEACHER":
5088 $this->info->course_teacher = $this->getContents();
5089 break;
5090 case "TEACHERS":
5091 $this->info->course_teachers = $this->getContents();
5092 break;
5093 case "STUDENT":
5094 $this->info->course_student = $this->getContents();
5095 break;
5096 case "STUDENTS":
5097 $this->info->course_students = $this->getContents();
5098 break;
5099 case "GUEST":
5100 $this->info->course_guest = $this->getContents();
5101 break;
5102 case "STARTDATE":
5103 $this->info->course_startdate = $this->getContents();
5104 break;
5105 case "NUMSECTIONS":
5106 $this->info->course_numsections = $this->getContents();
5107 break;
5108 //case "SHOWRECENT": INFO: This is out in 1.3
5109 // $this->info->course_showrecent = $this->getContents();
5110 // break;
5111 case "MAXBYTES":
5112 $this->info->course_maxbytes = $this->getContents();
5113 break;
5114 case "SHOWREPORTS":
5115 $this->info->course_showreports = $this->getContents();
5116 break;
5117 case "GROUPMODE":
5118 $this->info->course_groupmode = $this->getContents();
5119 break;
5120 case "GROUPMODEFORCE":
5121 $this->info->course_groupmodeforce = $this->getContents();
5122 break;
5123 case "DEFAULTGROUPINGID":
5124 $this->info->course_defaultgroupingid = $this->getContents();
5125 break;
5126 case "LANG":
5127 $this->info->course_lang = $this->getContents();
5128 break;
5129 case "THEME":
5130 $this->info->course_theme = $this->getContents();
5131 break;
5132 case "COST":
5133 $this->info->course_cost = $this->getContents();
5134 break;
5135 case "CURRENCY":
5136 $this->info->course_currency = $this->getContents();
5137 break;
5138 case "MARKER":
5139 $this->info->course_marker = $this->getContents();
5140 break;
5141 case "VISIBLE":
5142 $this->info->course_visible = $this->getContents();
5143 break;
5144 case "HIDDENSECTIONS":
5145 $this->info->course_hiddensections = $this->getContents();
5146 break;
5147 case "TIMECREATED":
5148 $this->info->course_timecreated = $this->getContents();
5149 break;
5150 case "TIMEMODIFIED":
5151 $this->info->course_timemodified = $this->getContents();
5152 break;
5153 case "METACOURSE":
5154 $this->info->course_metacourse = $this->getContents();
5155 break;
5156 case "EXPIRENOTIFY":
5157 $this->info->course_expirynotify = $this->getContents();
5158 break;
5159 case "NOTIFYSTUDENTS":
5160 $this->info->course_notifystudents = $this->getContents();
5161 break;
5162 case "EXPIRYTHRESHOLD":
5163 $this->info->course_expirythreshold = $this->getContents();
5164 break;
5165 case "ENROLLABLE":
5166 $this->info->course_enrollable = $this->getContents();
5167 break;
5168 case "ENROLSTARTDATE":
5169 $this->info->course_enrolstartdate = $this->getContents();
5170 break;
5171 case "ENROLENDDATE":
5172 $this->info->course_enrolenddate = $this->getContents();
5173 break;
5174 case "ENROLPERIOD":
5175 $this->info->course_enrolperiod = $this->getContents();
5176 break;
5179 if ($this->tree[4] == "CATEGORY") {
5180 if ($this->level == 5) {
5181 switch ($tagName) {
5182 case "ID":
5183 $this->info->category->id = $this->getContents();
5184 break;
5185 case "NAME":
5186 $this->info->category->name = $this->getContents();
5187 break;
5192 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
5193 if ($this->level == 6) {
5194 switch ($tagName) {
5195 case "NAME":
5196 $this->info->tempname = $this->getContents();
5197 break;
5198 case "SHORTNAME":
5199 $this->info->tempshortname = $this->getContents();
5200 break;
5201 case "ID":
5202 $this->info->tempid = $this->getContents();
5203 break;
5207 if ($this->level == 8) {
5208 switch ($tagName) {
5209 case "USERID":
5210 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5211 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5212 $this->info->tempuser = $this->getContents();
5213 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5214 break;
5215 case "HIDDEN":
5216 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5217 break;
5218 case "TIMESTART":
5219 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5220 break;
5221 case "TIMEEND":
5222 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5223 break;
5224 case "TIMEMODIFIED":
5225 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5226 break;
5227 case "MODIFIERID":
5228 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5229 break;
5230 case "ENROL":
5231 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5232 break;
5233 case "SORTORDER":
5234 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5235 break;
5239 } /// ends role_assignments
5241 if ($this->tree[4] == "ROLES_OVERRIDES") {
5242 if ($this->level == 6) {
5243 switch ($tagName) {
5244 case "NAME":
5245 $this->info->tempname = $this->getContents();
5246 break;
5247 case "SHORTNAME":
5248 $this->info->tempshortname = $this->getContents();
5249 break;
5250 case "ID":
5251 $this->info->tempid = $this->getContents();
5252 break;
5256 if ($this->level == 8) {
5257 switch ($tagName) {
5258 case "NAME":
5259 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5260 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5261 $this->info->tempname = $this->getContents(); // change to name of capability
5262 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5263 break;
5264 case "PERMISSION":
5265 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5266 break;
5267 case "TIMEMODIFIED":
5268 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5269 break;
5270 case "MODIFIERID":
5271 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5272 break;
5275 } /// ends role_overrides
5278 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
5279 //Speed up a lot (avoid parse all)
5280 if ($tagName == "HEADER") {
5281 $this->finished = true;
5284 //Clear things
5285 $this->tree[$this->level] = "";
5286 $this->level--;
5287 $this->content = "";
5291 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
5292 function endElementBlocks($parser, $tagName) {
5293 //Check if we are into BLOCKS zone
5294 if ($this->tree[3] == 'BLOCKS') {
5295 //if (trim($this->content)) //Debug
5296 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5297 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5299 // Collect everything into $this->temp
5300 if (!isset($this->temp)) {
5301 $this->temp = "";
5303 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5305 //Dependig of different combinations, do different things
5306 if ($this->level == 4) {
5307 switch ($tagName) {
5308 case 'BLOCK':
5309 //We've finalized a block, get it
5310 $this->info->instances[] = $this->info->tempinstance;
5311 unset($this->info->tempinstance);
5313 //Also, xmlize INSTANCEDATA and save to db
5314 //Prepend XML standard header to info gathered
5315 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5316 //Call to xmlize for this portion of xml data (one BLOCK)
5317 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5318 $data = xmlize($xml_data,0);
5319 //echo strftime ("%X",time())."<p>"; //Debug
5320 //traverse_xmlize($data); //Debug
5321 //print_object ($GLOBALS['traverse_array']); //Debug
5322 //$GLOBALS['traverse_array']=""; //Debug
5323 //Check for instancedata, is exists, then save to DB
5324 if (isset($data['BLOCK']['#']['INSTANCEDATA']['0']['#'])) {
5325 //Get old id
5326 $oldid = $data['BLOCK']['#']['ID']['0']['#'];
5327 //Get instancedata
5329 if ($data = $data['BLOCK']['#']['INSTANCEDATA']['0']['#']) {
5330 //Restore code calls this multiple times - so might already have the newid
5331 if ($newid = backup_getid($this->preferences->backup_unique_code,'block_instance',$oldid)) {
5332 $newid = $newid->new_id;
5333 } else {
5334 $newid = null;
5336 //Save to DB, we will use it later
5337 $status = backup_putid($this->preferences->backup_unique_code,'block_instance',$oldid,$newid,$data);
5340 //Reset temp
5341 unset($this->temp);
5343 break;
5344 default:
5345 die($tagName);
5348 if ($this->level == 5) {
5349 switch ($tagName) {
5350 case 'ID':
5351 $this->info->tempinstance->id = $this->getContents();
5352 case 'NAME':
5353 $this->info->tempinstance->name = $this->getContents();
5354 break;
5355 case 'PAGEID':
5356 $this->info->tempinstance->pageid = $this->getContents();
5357 break;
5358 case 'PAGETYPE':
5359 $this->info->tempinstance->pagetype = $this->getContents();
5360 break;
5361 case 'POSITION':
5362 $this->info->tempinstance->position = $this->getContents();
5363 break;
5364 case 'WEIGHT':
5365 $this->info->tempinstance->weight = $this->getContents();
5366 break;
5367 case 'VISIBLE':
5368 $this->info->tempinstance->visible = $this->getContents();
5369 break;
5370 case 'CONFIGDATA':
5371 $this->info->tempinstance->configdata = $this->getContents();
5372 break;
5373 default:
5374 break;
5378 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
5379 if ($this->level == 7) {
5380 switch ($tagName) {
5381 case "NAME":
5382 $this->info->tempname = $this->getContents();
5383 break;
5384 case "SHORTNAME":
5385 $this->info->tempshortname = $this->getContents();
5386 break;
5387 case "ID":
5388 $this->info->tempid = $this->getContents(); // temp roleid
5389 break;
5393 if ($this->level == 9) {
5395 switch ($tagName) {
5396 case "USERID":
5397 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5399 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5401 $this->info->tempuser = $this->getContents();
5403 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5404 break;
5405 case "HIDDEN":
5406 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5407 break;
5408 case "TIMESTART":
5409 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5410 break;
5411 case "TIMEEND":
5412 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5413 break;
5414 case "TIMEMODIFIED":
5415 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5416 break;
5417 case "MODIFIERID":
5418 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5419 break;
5420 case "ENROL":
5421 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5422 break;
5423 case "SORTORDER":
5424 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5425 break;
5429 } /// ends role_assignments
5431 if ($this->tree[5] == "ROLES_OVERRIDES") {
5432 if ($this->level == 7) {
5433 switch ($tagName) {
5434 case "NAME":
5435 $this->info->tempname = $this->getContents();
5436 break;
5437 case "SHORTNAME":
5438 $this->info->tempshortname = $this->getContents();
5439 break;
5440 case "ID":
5441 $this->info->tempid = $this->getContents(); // temp roleid
5442 break;
5446 if ($this->level == 9) {
5447 switch ($tagName) {
5448 case "NAME":
5450 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5451 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5452 $this->info->tempname = $this->getContents(); // change to name of capability
5453 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5454 break;
5455 case "PERMISSION":
5456 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5457 break;
5458 case "TIMEMODIFIED":
5459 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5460 break;
5461 case "MODIFIERID":
5462 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5463 break;
5466 } /// ends role_overrides
5469 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
5470 //Speed up a lot (avoid parse all)
5471 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
5472 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
5473 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
5474 $this->finished = true;
5477 //Clear things
5478 $this->tree[$this->level] = '';
5479 $this->level--;
5480 $this->content = "";
5483 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
5484 function endElementSections($parser, $tagName) {
5485 //Check if we are into SECTIONS zone
5486 if ($this->tree[3] == "SECTIONS") {
5487 //if (trim($this->content)) //Debug
5488 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5489 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5490 //Dependig of different combinations, do different things
5491 if ($this->level == 4) {
5492 switch ($tagName) {
5493 case "SECTION":
5494 //We've finalized a section, get it
5495 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
5496 unset($this->info->tempsection);
5499 if ($this->level == 5) {
5500 switch ($tagName) {
5501 case "ID":
5502 $this->info->tempsection->id = $this->getContents();
5503 break;
5504 case "NUMBER":
5505 $this->info->tempsection->number = $this->getContents();
5506 break;
5507 case "SUMMARY":
5508 $this->info->tempsection->summary = $this->getContents();
5509 break;
5510 case "VISIBLE":
5511 $this->info->tempsection->visible = $this->getContents();
5512 break;
5515 if ($this->level == 6) {
5516 switch ($tagName) {
5517 case "MOD":
5518 if (!isset($this->info->tempmod->groupmode)) {
5519 $this->info->tempmod->groupmode = 0;
5521 if (!isset($this->info->tempmod->groupingid)) {
5522 $this->info->tempmod->groupingid = 0;
5524 if (!isset($this->info->tempmod->groupmembersonly)) {
5525 $this->info->tempmod->groupmembersonly = 0;
5527 if (!isset($this->info->tempmod->idnumber)) {
5528 $this->info->tempmod->idnumber = null;
5531 //We've finalized a mod, get it
5532 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
5533 $this->info->tempmod->type;
5534 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
5535 $this->info->tempmod->instance;
5536 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
5537 $this->info->tempmod->added;
5538 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
5539 $this->info->tempmod->score;
5540 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
5541 $this->info->tempmod->indent;
5542 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
5543 $this->info->tempmod->visible;
5544 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
5545 $this->info->tempmod->groupmode;
5546 $this->info->tempsection->mods[$this->info->tempmod->id]->groupingid =
5547 $this->info->tempmod->groupingid;
5548 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmembersonly =
5549 $this->info->tempmod->groupmembersonly;
5550 $this->info->tempsection->mods[$this->info->tempmod->id]->idnumber =
5551 $this->info->tempmod->idnumber;
5553 unset($this->info->tempmod);
5556 if ($this->level == 7) {
5557 switch ($tagName) {
5558 case "ID":
5559 $this->info->tempmod->id = $this->getContents();
5560 break;
5561 case "TYPE":
5562 $this->info->tempmod->type = $this->getContents();
5563 break;
5564 case "INSTANCE":
5565 $this->info->tempmod->instance = $this->getContents();
5566 break;
5567 case "ADDED":
5568 $this->info->tempmod->added = $this->getContents();
5569 break;
5570 case "SCORE":
5571 $this->info->tempmod->score = $this->getContents();
5572 break;
5573 case "INDENT":
5574 $this->info->tempmod->indent = $this->getContents();
5575 break;
5576 case "VISIBLE":
5577 $this->info->tempmod->visible = $this->getContents();
5578 break;
5579 case "GROUPMODE":
5580 $this->info->tempmod->groupmode = $this->getContents();
5581 break;
5582 case "GROUPINGID":
5583 $this->info->tempmod->groupingid = $this->getContents();
5584 break;
5585 case "GROUPMEMBERSONLY":
5586 $this->info->tempmod->groupmembersonly = $this->getContents();
5587 case "IDNUMBER":
5588 $this->info->tempmod->idnumber = $this->getContents();
5589 break;
5590 default:
5591 break;
5595 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
5597 if ($this->level == 9) {
5598 switch ($tagName) {
5599 case "NAME":
5600 $this->info->tempname = $this->getContents();
5601 break;
5602 case "SHORTNAME":
5603 $this->info->tempshortname = $this->getContents();
5604 break;
5605 case "ID":
5606 $this->info->tempid = $this->getContents(); // temp roleid
5607 break;
5611 if ($this->level == 11) {
5612 switch ($tagName) {
5613 case "USERID":
5614 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5616 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5618 $this->info->tempuser = $this->getContents();
5620 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5621 break;
5622 case "HIDDEN":
5623 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5624 break;
5625 case "TIMESTART":
5626 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5627 break;
5628 case "TIMEEND":
5629 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5630 break;
5631 case "TIMEMODIFIED":
5632 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5633 break;
5634 case "MODIFIERID":
5635 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5636 break;
5637 case "ENROL":
5638 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5639 break;
5640 case "SORTORDER":
5641 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5642 break;
5646 } /// ends role_assignments
5648 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
5649 if ($this->level == 9) {
5650 switch ($tagName) {
5651 case "NAME":
5652 $this->info->tempname = $this->getContents();
5653 break;
5654 case "SHORTNAME":
5655 $this->info->tempshortname = $this->getContents();
5656 break;
5657 case "ID":
5658 $this->info->tempid = $this->getContents(); // temp roleid
5659 break;
5663 if ($this->level == 11) {
5664 switch ($tagName) {
5665 case "NAME":
5667 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5668 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5669 $this->info->tempname = $this->getContents(); // change to name of capability
5670 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5671 break;
5672 case "PERMISSION":
5673 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5674 break;
5675 case "TIMEMODIFIED":
5676 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5677 break;
5678 case "MODIFIERID":
5679 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5680 break;
5683 } /// ends role_overrides
5687 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
5688 //Speed up a lot (avoid parse all)
5689 if ($tagName == "SECTIONS") {
5690 $this->finished = true;
5693 //Clear things
5694 $this->tree[$this->level] = "";
5695 $this->level--;
5696 $this->content = "";
5700 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
5701 function endElementFormatData($parser, $tagName) {
5702 //Check if we are into FORMATDATA zone
5703 if ($this->tree[3] == 'FORMATDATA') {
5704 if (!isset($this->temp)) {
5705 $this->temp = '';
5707 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5710 if($tagName=='FORMATDATA') {
5711 //Did we have any data? If not don't bother
5712 if($this->temp!='<FORMATDATA></FORMATDATA>') {
5713 //Prepend XML standard header to info gathered
5714 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5715 $this->temp='';
5717 //Call to xmlize for this portion of xml data (the FORMATDATA block)
5718 $this->info->format_data = xmlize($xml_data,0);
5720 //Stop parsing at end of FORMATDATA
5721 $this->finished=true;
5724 //Clear things
5725 $this->tree[$this->level] = "";
5726 $this->level--;
5727 $this->content = "";
5730 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
5731 function endElementMetacourse($parser, $tagName) {
5732 //Check if we are into METACOURSE zone
5733 if ($this->tree[3] == 'METACOURSE') {
5734 //if (trim($this->content)) //Debug
5735 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5736 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5737 //Dependig of different combinations, do different things
5738 if ($this->level == 5) {
5739 switch ($tagName) {
5740 case 'CHILD':
5741 //We've finalized a child, get it
5742 $this->info->childs[] = $this->info->tempmeta;
5743 unset($this->info->tempmeta);
5744 break;
5745 case 'PARENT':
5746 //We've finalized a parent, get it
5747 $this->info->parents[] = $this->info->tempmeta;
5748 unset($this->info->tempmeta);
5749 break;
5750 default:
5751 die($tagName);
5754 if ($this->level == 6) {
5755 switch ($tagName) {
5756 case 'ID':
5757 $this->info->tempmeta->id = $this->getContents();
5758 break;
5759 case 'IDNUMBER':
5760 $this->info->tempmeta->idnumber = $this->getContents();
5761 break;
5762 case 'SHORTNAME':
5763 $this->info->tempmeta->shortname = $this->getContents();
5764 break;
5769 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
5770 //Speed up a lot (avoid parse all)
5771 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
5772 $this->finished = true;
5775 //Clear things
5776 $this->tree[$this->level] = '';
5777 $this->level--;
5778 $this->content = "";
5781 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5782 function endElementGradebook($parser, $tagName) {
5783 //Check if we are into GRADEBOOK zone
5784 if ($this->tree[3] == "GRADEBOOK") {
5785 //if (trim($this->content)) //Debug
5786 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5787 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5788 //Acumulate data to info (content + close tag)
5789 //Reconvert: strip htmlchars again and trim to generate xml data
5790 if (!isset($this->temp)) {
5791 $this->temp = "";
5793 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5794 // We have finished outcome, grade_category or grade_item, reset accumulated
5795 // data because they are close tags
5796 if ($this->level == 4) {
5797 $this->temp = "";
5799 //If we've finished a grade item, xmlize it an save to db
5800 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
5801 //Prepend XML standard header to info gathered
5802 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5803 //Call to xmlize for this portion of xml data (one PREFERENCE)
5804 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5805 $data = xmlize($xml_data,0);
5806 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
5807 $this->counter++;
5808 //Save to db
5810 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
5811 null,$data);
5812 //Create returning info
5813 $this->info = $this->counter;
5814 //Reset temp
5816 unset($this->temp);
5819 //If we've finished a grade_category, xmlize it an save to db
5820 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
5821 //Prepend XML standard header to info gathered
5822 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5823 //Call to xmlize for this portion of xml data (one CATECORY)
5824 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5825 $data = xmlize($xml_data,0);
5826 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
5827 $this->counter++;
5828 //Save to db
5829 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
5830 null,$data);
5831 //Create returning info
5832 $this->info = $this->counter;
5833 //Reset temp
5834 unset($this->temp);
5837 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
5838 //Prepend XML standard header to info gathered
5839 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5840 //Call to xmlize for this portion of xml data (one CATECORY)
5841 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5842 $data = xmlize($xml_data,0);
5843 $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
5844 $this->counter++;
5845 //Save to db
5846 $status = backup_putid($this->preferences->backup_unique_code, 'grade_letters' ,$letter_id,
5847 null,$data);
5848 //Create returning info
5849 $this->info = $this->counter;
5850 //Reset temp
5851 unset($this->temp);
5854 //If we've finished a grade_outcome, xmlize it an save to db
5855 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
5856 //Prepend XML standard header to info gathered
5857 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5858 //Call to xmlize for this portion of xml data (one CATECORY)
5859 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5860 $data = xmlize($xml_data,0);
5861 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
5862 $this->counter++;
5863 //Save to db
5864 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
5865 null,$data);
5866 //Create returning info
5867 $this->info = $this->counter;
5868 //Reset temp
5869 unset($this->temp);
5872 //If we've finished a grade_outcomes_course, xmlize it an save to db
5873 if (($this->level == 5) and ($tagName == "GRADE_OUTCOMES_COURSE")) {
5874 //Prepend XML standard header to info gathered
5875 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5876 //Call to xmlize for this portion of xml data (one CATECORY)
5877 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5878 $data = xmlize($xml_data,0);
5879 $outcomes_course_id = $data["GRADE_OUTCOMES_COURSE"]["#"]["ID"]["0"]["#"];
5880 $this->counter++;
5881 //Save to db
5882 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_courses' ,$outcomes_course_id,
5883 null,$data);
5884 //Create returning info
5885 $this->info = $this->counter;
5886 //Reset temp
5887 unset($this->temp);
5890 if (($this->level == 5) and ($tagName == "GRADE_CATEGORIES_HISTORY")) {
5891 //Prepend XML standard header to info gathered
5892 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5893 //Call to xmlize for this portion of xml data (one PREFERENCE)
5894 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5895 $data = xmlize($xml_data,0);
5896 $id = $data["GRADE_CATEGORIES_HISTORY"]["#"]["ID"]["0"]["#"];
5897 $this->counter++;
5898 //Save to db
5900 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories_history', $id,
5901 null,$data);
5902 //Create returning info
5903 $this->info = $this->counter;
5904 //Reset temp
5906 unset($this->temp);
5909 if (($this->level == 5) and ($tagName == "GRADE_GRADES_HISTORY")) {
5910 //Prepend XML standard header to info gathered
5911 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5912 //Call to xmlize for this portion of xml data (one PREFERENCE)
5913 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5914 $data = xmlize($xml_data,0);
5915 $id = $data["GRADE_GRADES_HISTORY"]["#"]["ID"]["0"]["#"];
5916 $this->counter++;
5917 //Save to db
5919 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_history', $id,
5920 null,$data);
5921 //Create returning info
5922 $this->info = $this->counter;
5923 //Reset temp
5925 unset($this->temp);
5928 if (($this->level == 5) and ($tagName == "GRADE_ITEM_HISTORY")) {
5929 //Prepend XML standard header to info gathered
5930 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5931 //Call to xmlize for this portion of xml data (one PREFERENCE)
5932 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5933 $data = xmlize($xml_data,0);
5934 $id = $data["GRADE_ITEM_HISTORY"]["#"]["ID"]["0"]["#"];
5935 $this->counter++;
5936 //Save to db
5938 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items_history', $id,
5939 null,$data);
5940 //Create returning info
5941 $this->info = $this->counter;
5942 //Reset temp
5944 unset($this->temp);
5947 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME_HISTORY")) {
5948 //Prepend XML standard header to info gathered
5949 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5950 //Call to xmlize for this portion of xml data (one PREFERENCE)
5951 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5952 $data = xmlize($xml_data,0);
5953 $id = $data["GRADE_OUTCOME_HISTORY"]["#"]["ID"]["0"]["#"];
5954 $this->counter++;
5955 //Save to db
5957 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_history', $id,
5958 null,$data);
5959 //Create returning info
5960 $this->info = $this->counter;
5961 //Reset temp
5963 unset($this->temp);
5967 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
5968 //Speed up a lot (avoid parse all)
5969 if ($tagName == "GRADEBOOK" and $this->level == 3) {
5970 $this->finished = true;
5971 $this->counter = 0;
5974 //Clear things
5975 $this->tree[$this->level] = "";
5976 $this->level--;
5977 $this->content = "";
5981 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5982 function endElementOldGradebook($parser, $tagName) {
5983 //Check if we are into GRADEBOOK zone
5984 if ($this->tree[3] == "GRADEBOOK") {
5985 //if (trim($this->content)) //Debug
5986 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5987 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5988 //Acumulate data to info (content + close tag)
5989 //Reconvert: strip htmlchars again and trim to generate xml data
5990 if (!isset($this->temp)) {
5991 $this->temp = "";
5993 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5994 //We have finished preferences, letters or categories, reset accumulated
5995 //data because they are close tags
5996 if ($this->level == 4) {
5997 $this->temp = "";
5999 //If we've finished a message, xmlize it an save to db
6000 if (($this->level == 5) and ($tagName == "GRADE_PREFERENCE")) {
6001 //Prepend XML standard header to info gathered
6002 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6003 //Call to xmlize for this portion of xml data (one PREFERENCE)
6004 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6005 $data = xmlize($xml_data,0);
6006 //echo strftime ("%X",time())."<p>"; //Debug
6007 //traverse_xmlize($data); //Debug
6008 //print_object ($GLOBALS['traverse_array']); //Debug
6009 //$GLOBALS['traverse_array']=""; //Debug
6010 //Now, save data to db. We'll use it later
6011 //Get id and status from data
6012 $preference_id = $data["GRADE_PREFERENCE"]["#"]["ID"]["0"]["#"];
6013 $this->counter++;
6014 //Save to db
6015 $status = backup_putid($this->preferences->backup_unique_code, 'grade_preferences', $preference_id,
6016 null,$data);
6017 //Create returning info
6018 $this->info = $this->counter;
6019 //Reset temp
6020 unset($this->temp);
6022 //If we've finished a grade_letter, xmlize it an save to db
6023 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
6024 //Prepend XML standard header to info gathered
6025 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6026 //Call to xmlize for this portion of xml data (one LETTER)
6027 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6028 $data = xmlize($xml_data,0);
6029 //echo strftime ("%X",time())."<p>"; //Debug
6030 //traverse_xmlize($data); //Debug
6031 //print_object ($GLOBALS['traverse_array']); //Debug
6032 //$GLOBALS['traverse_array']=""; //Debug
6033 //Now, save data to db. We'll use it later
6034 //Get id and status from data
6035 $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
6036 $this->counter++;
6037 //Save to db
6038 $status = backup_putid($this->preferences->backup_unique_code, 'grade_letter' ,$letter_id,
6039 null,$data);
6040 //Create returning info
6041 $this->info = $this->counter;
6042 //Reset temp
6043 unset($this->temp);
6046 //If we've finished a grade_category, xmlize it an save to db
6047 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
6048 //Prepend XML standard header to info gathered
6049 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6050 //Call to xmlize for this portion of xml data (one CATECORY)
6051 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6052 $data = xmlize($xml_data,0);
6053 //echo strftime ("%X",time())."<p>"; //Debug
6054 //traverse_xmlize($data); //Debug
6055 //print_object ($GLOBALS['traverse_array']); //Debug
6056 //$GLOBALS['traverse_array']=""; //Debug
6057 //Now, save data to db. We'll use it later
6058 //Get id and status from data
6059 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
6060 $this->counter++;
6061 //Save to db
6062 $status = backup_putid($this->preferences->backup_unique_code, 'grade_category' ,$category_id,
6063 null,$data);
6064 //Create returning info
6065 $this->info = $this->counter;
6066 //Reset temp
6067 unset($this->temp);
6071 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
6072 //Speed up a lot (avoid parse all)
6073 if ($tagName == "GRADEBOOK" and $this->level == 3) {
6074 $this->finished = true;
6075 $this->counter = 0;
6078 //Clear things
6079 $this->tree[$this->level] = "";
6080 $this->level--;
6081 $this->content = "";
6085 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
6086 function endElementUsers($parser, $tagName) {
6087 global $CFG;
6088 //Check if we are into USERS zone
6089 if ($this->tree[3] == "USERS") {
6090 //if (trim($this->content)) //Debug
6091 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6092 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6093 //Dependig of different combinations, do different things
6094 if ($this->level == 4) {
6095 switch ($tagName) {
6096 case "USER":
6097 //Increment counter
6098 $this->counter++;
6099 //Save to db, only save if record not already exist
6100 // if there already is an new_id for this entry, just use that new_id?
6101 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
6102 if (isset($newuser->new_id)) {
6103 $newid = $newuser->new_id;
6104 } else {
6105 $newid = null;
6108 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
6109 $newid,$this->info->tempuser);
6111 //Do some output
6112 if ($this->counter % 10 == 0) {
6113 if (!defined('RESTORE_SILENTLY')) {
6114 echo ".";
6115 if ($this->counter % 200 == 0) {
6116 echo "<br />";
6119 backup_flush(300);
6122 //Delete temp obejct
6123 unset($this->info->tempuser);
6124 break;
6128 if ($this->level == 5) {
6129 switch ($tagName) {
6130 case "ID":
6131 $this->info->users[$this->getContents()] = $this->getContents();
6132 $this->info->tempuser->id = $this->getContents();
6133 break;
6134 case "AUTH":
6135 $this->info->tempuser->auth = $this->getContents();
6136 break;
6137 case "CONFIRMED":
6138 $this->info->tempuser->confirmed = $this->getContents();
6139 break;
6140 case "POLICYAGREED":
6141 $this->info->tempuser->policyagreed = $this->getContents();
6142 break;
6143 case "DELETED":
6144 $this->info->tempuser->deleted = $this->getContents();
6145 break;
6146 case "USERNAME":
6147 $this->info->tempuser->username = $this->getContents();
6148 break;
6149 case "PASSWORD":
6150 $this->info->tempuser->password = $this->getContents();
6151 break;
6152 case "IDNUMBER":
6153 $this->info->tempuser->idnumber = $this->getContents();
6154 break;
6155 case "FIRSTNAME":
6156 $this->info->tempuser->firstname = $this->getContents();
6157 break;
6158 case "LASTNAME":
6159 $this->info->tempuser->lastname = $this->getContents();
6160 break;
6161 case "EMAIL":
6162 $this->info->tempuser->email = $this->getContents();
6163 break;
6164 case "EMAILSTOP":
6165 $this->info->tempuser->emailstop = $this->getContents();
6166 break;
6167 case "ICQ":
6168 $this->info->tempuser->icq = $this->getContents();
6169 break;
6170 case "SKYPE":
6171 $this->info->tempuser->skype = $this->getContents();
6172 break;
6173 case "AIM":
6174 $this->info->tempuser->aim = $this->getContents();
6175 break;
6176 case "YAHOO":
6177 $this->info->tempuser->yahoo = $this->getContents();
6178 break;
6179 case "MSN":
6180 $this->info->tempuser->msn = $this->getContents();
6181 break;
6182 case "PHONE1":
6183 $this->info->tempuser->phone1 = $this->getContents();
6184 break;
6185 case "PHONE2":
6186 $this->info->tempuser->phone2 = $this->getContents();
6187 break;
6188 case "INSTITUTION":
6189 $this->info->tempuser->institution = $this->getContents();
6190 break;
6191 case "DEPARTMENT":
6192 $this->info->tempuser->department = $this->getContents();
6193 break;
6194 case "ADDRESS":
6195 $this->info->tempuser->address = $this->getContents();
6196 break;
6197 case "CITY":
6198 $this->info->tempuser->city = $this->getContents();
6199 break;
6200 case "COUNTRY":
6201 $this->info->tempuser->country = $this->getContents();
6202 break;
6203 case "LANG":
6204 $this->info->tempuser->lang = $this->getContents();
6205 break;
6206 case "THEME":
6207 $this->info->tempuser->theme = $this->getContents();
6208 break;
6209 case "TIMEZONE":
6210 $this->info->tempuser->timezone = $this->getContents();
6211 break;
6212 case "FIRSTACCESS":
6213 $this->info->tempuser->firstaccess = $this->getContents();
6214 break;
6215 case "LASTACCESS":
6216 $this->info->tempuser->lastaccess = $this->getContents();
6217 break;
6218 case "LASTLOGIN":
6219 $this->info->tempuser->lastlogin = $this->getContents();
6220 break;
6221 case "CURRENTLOGIN":
6222 $this->info->tempuser->currentlogin = $this->getContents();
6223 break;
6224 case "LASTIP":
6225 $this->info->tempuser->lastip = $this->getContents();
6226 break;
6227 case "SECRET":
6228 $this->info->tempuser->secret = $this->getContents();
6229 break;
6230 case "PICTURE":
6231 $this->info->tempuser->picture = $this->getContents();
6232 break;
6233 case "URL":
6234 $this->info->tempuser->url = $this->getContents();
6235 break;
6236 case "DESCRIPTION":
6237 $this->info->tempuser->description = $this->getContents();
6238 break;
6239 case "MAILFORMAT":
6240 $this->info->tempuser->mailformat = $this->getContents();
6241 break;
6242 case "MAILDIGEST":
6243 $this->info->tempuser->maildigest = $this->getContents();
6244 break;
6245 case "MAILDISPLAY":
6246 $this->info->tempuser->maildisplay = $this->getContents();
6247 break;
6248 case "HTMLEDITOR":
6249 $this->info->tempuser->htmleditor = $this->getContents();
6250 break;
6251 case "AJAX":
6252 $this->info->tempuser->ajax = $this->getContents();
6253 break;
6254 case "AUTOSUBSCRIBE":
6255 $this->info->tempuser->autosubscribe = $this->getContents();
6256 break;
6257 case "TRACKFORUMS":
6258 $this->info->tempuser->trackforums = $this->getContents();
6259 break;
6260 case "MNETHOSTURL":
6261 $this->info->tempuser->mnethosturl = $this->getContents();
6262 break;
6263 case "TIMEMODIFIED":
6264 $this->info->tempuser->timemodified = $this->getContents();
6265 break;
6266 default:
6267 break;
6271 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
6272 switch ($tagName) {
6273 case "ROLE":
6274 //We've finalized a role, get it
6275 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
6276 unset($this->info->temprole);
6277 break;
6278 case "USER_PREFERENCE":
6279 //We've finalized a user_preference, get it
6280 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
6281 unset($this->info->tempuserpreference);
6282 break;
6283 case "USER_CUSTOM_PROFILE_FIELD":
6284 //We've finalized a user_custom_profile_field, get it
6285 $this->info->tempuser->user_custom_profile_fields[] = $this->info->tempusercustomprofilefield;
6286 unset($this->info->tempusercustomprofilefield);
6287 break;
6288 case "USER_TAG":
6289 //We've finalized a user_tag, get it
6290 $this->info->tempuser->user_tags[] = $this->info->tempusertag;
6291 unset($this->info->tempusertag);
6292 break;
6293 default:
6294 break;
6298 if ($this->level == 7 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
6299 /// If we are reading roles
6300 if($this->tree[6] == 'ROLE') {
6301 switch ($tagName) {
6302 case "TYPE":
6303 $this->info->temprole->type = $this->getContents();
6304 break;
6305 case "AUTHORITY":
6306 $this->info->temprole->authority = $this->getContents();
6307 break;
6308 case "TEA_ROLE":
6309 $this->info->temprole->tea_role = $this->getContents();
6310 break;
6311 case "EDITALL":
6312 $this->info->temprole->editall = $this->getContents();
6313 break;
6314 case "TIMESTART":
6315 $this->info->temprole->timestart = $this->getContents();
6316 break;
6317 case "TIMEEND":
6318 $this->info->temprole->timeend = $this->getContents();
6319 break;
6320 case "TIMEMODIFIED":
6321 $this->info->temprole->timemodified = $this->getContents();
6322 break;
6323 case "TIMESTART":
6324 $this->info->temprole->timestart = $this->getContents();
6325 break;
6326 case "TIMEEND":
6327 $this->info->temprole->timeend = $this->getContents();
6328 break;
6329 case "TIME":
6330 $this->info->temprole->time = $this->getContents();
6331 break;
6332 case "TIMEACCESS":
6333 $this->info->temprole->timeaccess = $this->getContents();
6334 break;
6335 case "ENROL":
6336 $this->info->temprole->enrol = $this->getContents();
6337 break;
6338 default:
6339 break;
6341 /// If we are reading user_preferences
6342 } else if ($this->tree[6] == 'USER_PREFERENCE') {
6343 switch ($tagName) {
6344 case "NAME":
6345 $this->info->tempuserpreference->name = $this->getContents();
6346 break;
6347 case "VALUE":
6348 $this->info->tempuserpreference->value = $this->getContents();
6349 break;
6350 default:
6351 break;
6353 /// If we are reading user_custom_profile_fields
6354 } else if ($this->tree[6] == 'USER_CUSTOM_PROFILE_FIELD') {
6355 switch ($tagName) {
6356 case "FIELD_NAME":
6357 $this->info->tempusercustomprofilefield->field_name = $this->getContents();
6358 break;
6359 case "FIELD_TYPE":
6360 $this->info->tempusercustomprofilefield->field_type = $this->getContents();
6361 break;
6362 case "FIELD_DATA":
6363 $this->info->tempusercustomprofilefield->field_data = $this->getContents();
6364 break;
6365 default:
6366 break;
6368 /// If we are reading user_tags
6369 } else if ($this->tree[6] == 'USER_TAG') {
6370 switch ($tagName) {
6371 case "NAME":
6372 $this->info->tempusertag->name = $this->getContents();
6373 break;
6374 case "RAWNAME":
6375 $this->info->tempusertag->rawname = $this->getContents();
6376 break;
6377 default:
6378 break;
6383 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
6385 if ($this->level == 7) {
6386 switch ($tagName) {
6387 case "NAME":
6388 $this->info->tempname = $this->getContents();
6389 break;
6390 case "SHORTNAME":
6391 $this->info->tempshortname = $this->getContents();
6392 break;
6393 case "ID":
6394 $this->info->tempid = $this->getContents(); // temp roleid
6395 break;
6399 if ($this->level == 9) {
6401 switch ($tagName) {
6402 case "USERID":
6403 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
6405 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
6407 $this->info->tempuserid = $this->getContents();
6409 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
6410 break;
6411 case "HIDDEN":
6412 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
6413 break;
6414 case "TIMESTART":
6415 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
6416 break;
6417 case "TIMEEND":
6418 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
6419 break;
6420 case "TIMEMODIFIED":
6421 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
6422 break;
6423 case "MODIFIERID":
6424 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
6425 break;
6426 case "ENROL":
6427 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
6428 break;
6429 case "SORTORDER":
6430 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
6431 break;
6435 } /// ends role_assignments
6437 if ($this->tree[5] == "ROLES_OVERRIDES") {
6438 if ($this->level == 7) {
6439 switch ($tagName) {
6440 case "NAME":
6441 $this->info->tempname = $this->getContents();
6442 break;
6443 case "SHORTNAME":
6444 $this->info->tempshortname = $this->getContents();
6445 break;
6446 case "ID":
6447 $this->info->tempid = $this->getContents(); // temp roleid
6448 break;
6452 if ($this->level == 9) {
6453 switch ($tagName) {
6454 case "NAME":
6456 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
6457 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
6458 $this->info->tempname = $this->getContents(); // change to name of capability
6459 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
6460 break;
6461 case "PERMISSION":
6462 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
6463 break;
6464 case "TIMEMODIFIED":
6465 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
6466 break;
6467 case "MODIFIERID":
6468 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
6469 break;
6472 } /// ends role_overrides
6474 } // closes if this->tree[3]=="users"
6476 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
6477 //Speed up a lot (avoid parse all)
6478 if ($tagName == "USERS" and $this->level == 3) {
6479 $this->finished = true;
6480 $this->counter = 0;
6483 //Clear things
6484 $this->tree[$this->level] = "";
6485 $this->level--;
6486 $this->content = "";
6490 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
6491 function endElementMessages($parser, $tagName) {
6492 //Check if we are into MESSAGES zone
6493 if ($this->tree[3] == "MESSAGES") {
6494 //if (trim($this->content)) //Debug
6495 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6496 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6497 //Acumulate data to info (content + close tag)
6498 //Reconvert: strip htmlchars again and trim to generate xml data
6499 if (!isset($this->temp)) {
6500 $this->temp = "";
6502 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6503 //If we've finished a message, xmlize it an save to db
6504 if (($this->level == 4) and ($tagName == "MESSAGE")) {
6505 //Prepend XML standard header to info gathered
6506 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6507 //Call to xmlize for this portion of xml data (one MESSAGE)
6508 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6509 $data = xmlize($xml_data,0);
6510 //echo strftime ("%X",time())."<p>"; //Debug
6511 //traverse_xmlize($data); //Debug
6512 //print_object ($GLOBALS['traverse_array']); //Debug
6513 //$GLOBALS['traverse_array']=""; //Debug
6514 //Now, save data to db. We'll use it later
6515 //Get id and status from data
6516 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
6517 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
6518 if ($message_status == "READ") {
6519 $table = "message_read";
6520 } else {
6521 $table = "message";
6523 $this->counter++;
6524 //Save to db
6525 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
6526 null,$data);
6527 //Create returning info
6528 $this->info = $this->counter;
6529 //Reset temp
6530 unset($this->temp);
6532 //If we've finished a contact, xmlize it an save to db
6533 if (($this->level == 5) and ($tagName == "CONTACT")) {
6534 //Prepend XML standard header to info gathered
6535 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6536 //Call to xmlize for this portion of xml data (one MESSAGE)
6537 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6538 $data = xmlize($xml_data,0);
6539 //echo strftime ("%X",time())."<p>"; //Debug
6540 //traverse_xmlize($data); //Debug
6541 //print_object ($GLOBALS['traverse_array']); //Debug
6542 //$GLOBALS['traverse_array']=""; //Debug
6543 //Now, save data to db. We'll use it later
6544 //Get id and status from data
6545 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
6546 $this->counter++;
6547 //Save to db
6548 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
6549 null,$data);
6550 //Create returning info
6551 $this->info = $this->counter;
6552 //Reset temp
6553 unset($this->temp);
6557 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
6558 //Speed up a lot (avoid parse all)
6559 if ($tagName == "MESSAGES" and $this->level == 3) {
6560 $this->finished = true;
6561 $this->counter = 0;
6564 //Clear things
6565 $this->tree[$this->level] = "";
6566 $this->level--;
6567 $this->content = "";
6571 //This is the endTag handler we use where we are reading the blogs zone (todo="BLOGS")
6572 function endElementBlogs($parser, $tagName) {
6573 //Check if we are into BLOGS zone
6574 if ($this->tree[3] == "BLOGS") {
6575 //if (trim($this->content)) //Debug
6576 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6577 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
6578 //Acumulate data to info (content + close tag)
6579 //Reconvert: strip htmlchars again and trim to generate xml data
6580 if (!isset($this->temp)) {
6581 $this->temp = "";
6583 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6584 //If we've finished a blog, xmlize it an save to db
6585 if (($this->level == 4) and ($tagName == "BLOG")) {
6586 //Prepend XML standard header to info gathered
6587 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6588 //Call to xmlize for this portion of xml data (one BLOG)
6589 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6590 $data = xmlize($xml_data,0);
6591 //echo strftime ("%X",time())."<p>"; //Debug
6592 //traverse_xmlize($data); //Debug
6593 //print_object ($GLOBALS['traverse_array']); //Debug
6594 //$GLOBALS['traverse_array']=""; //Debug
6595 //Now, save data to db. We'll use it later
6596 //Get id from data
6597 $blog_id = $data["BLOG"]["#"]["ID"]["0"]["#"];
6598 $this->counter++;
6599 //Save to db
6600 $status = backup_putid($this->preferences->backup_unique_code, 'blog', $blog_id,
6601 null,$data);
6602 //Create returning info
6603 $this->info = $this->counter;
6604 //Reset temp
6605 unset($this->temp);
6609 //Stop parsing if todo = BLOGS and tagName = BLOGS (end of the tag, of course)
6610 //Speed up a lot (avoid parse all)
6611 if ($tagName == "BLOGS" and $this->level == 3) {
6612 $this->finished = true;
6613 $this->counter = 0;
6616 //Clear things
6617 $this->tree[$this->level] = "";
6618 $this->level--;
6619 $this->content = "";
6623 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
6624 function endElementQuestions($parser, $tagName) {
6625 //Check if we are into QUESTION_CATEGORIES zone
6626 if ($this->tree[3] == "QUESTION_CATEGORIES") {
6627 //if (trim($this->content)) //Debug
6628 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6629 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6630 //Acumulate data to info (content + close tag)
6631 //Reconvert: strip htmlchars again and trim to generate xml data
6632 if (!isset($this->temp)) {
6633 $this->temp = "";
6635 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6636 //If we've finished a mod, xmlize it an save to db
6637 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
6638 //Prepend XML standard header to info gathered
6639 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6640 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
6641 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6642 $data = xmlize($xml_data,0);
6643 //echo strftime ("%X",time())."<p>"; //Debug
6644 //traverse_xmlize($data); //Debug
6645 //print_object ($GLOBALS['traverse_array']); //Debug
6646 //$GLOBALS['traverse_array']=""; //Debug
6647 //Now, save data to db. We'll use it later
6648 //Get id from data
6649 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
6650 //Save to db
6651 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
6652 null,$data);
6653 //Create returning info
6654 $ret_info = new object();
6655 $ret_info->id = $category_id;
6656 $this->info[] = $ret_info;
6657 //Reset temp
6658 unset($this->temp);
6662 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
6663 //Speed up a lot (avoid parse all)
6664 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
6665 $this->finished = true;
6668 //Clear things
6669 $this->tree[$this->level] = "";
6670 $this->level--;
6671 $this->content = "";
6675 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
6676 function endElementScales($parser, $tagName) {
6677 //Check if we are into SCALES zone
6678 if ($this->tree[3] == "SCALES") {
6679 //if (trim($this->content)) //Debug
6680 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6681 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6682 //Acumulate data to info (content + close tag)
6683 //Reconvert: strip htmlchars again and trim to generate xml data
6684 if (!isset($this->temp)) {
6685 $this->temp = "";
6687 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6688 //If we've finished a scale, xmlize it an save to db
6689 if (($this->level == 4) and ($tagName == "SCALE")) {
6690 //Prepend XML standard header to info gathered
6691 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6692 //Call to xmlize for this portion of xml data (one SCALE)
6693 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6694 $data = xmlize($xml_data,0);
6695 //echo strftime ("%X",time())."<p>"; //Debug
6696 //traverse_xmlize($data); //Debug
6697 //print_object ($GLOBALS['traverse_array']); //Debug
6698 //$GLOBALS['traverse_array']=""; //Debug
6699 //Now, save data to db. We'll use it later
6700 //Get id and from data
6701 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
6702 //Save to db
6703 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
6704 null,$data);
6705 //Create returning info
6706 $ret_info = new object();
6707 $ret_info->id = $scale_id;
6708 $this->info[] = $ret_info;
6709 //Reset temp
6710 unset($this->temp);
6714 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
6715 //Speed up a lot (avoid parse all)
6716 if ($tagName == "SCALES" and $this->level == 3) {
6717 $this->finished = true;
6720 //Clear things
6721 $this->tree[$this->level] = "";
6722 $this->level--;
6723 $this->content = "";
6727 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
6728 function endElementGroups($parser, $tagName) {
6729 //Check if we are into GROUPS zone
6730 if ($this->tree[3] == "GROUPS") {
6731 //if (trim($this->content)) //Debug
6732 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6733 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6734 //Acumulate data to info (content + close tag)
6735 //Reconvert: strip htmlchars again and trim to generate xml data
6736 if (!isset($this->temp)) {
6737 $this->temp = "";
6739 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6740 //If we've finished a group, xmlize it an save to db
6741 if (($this->level == 4) and ($tagName == "GROUP")) {
6742 //Prepend XML standard header to info gathered
6743 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6744 //Call to xmlize for this portion of xml data (one GROUP)
6745 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6746 $data = xmlize($xml_data,0);
6747 //echo strftime ("%X",time())."<p>"; //Debug
6748 //traverse_xmlize($data); //Debug
6749 //print_object ($GLOBALS['traverse_array']); //Debug
6750 //$GLOBALS['traverse_array']=""; //Debug
6751 //Now, save data to db. We'll use it later
6752 //Get id and from data
6753 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
6754 //Save to db
6755 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
6756 null,$data);
6757 //Create returning info
6758 $ret_info = new Object();
6759 $ret_info->id = $group_id;
6760 $this->info[] = $ret_info;
6761 //Reset temp
6762 unset($this->temp);
6766 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
6767 //Speed up a lot (avoid parse all)
6768 if ($tagName == "GROUPS" and $this->level == 3) {
6769 $this->finished = true;
6772 //Clear things
6773 $this->tree[$this->level] = "";
6774 $this->level--;
6775 $this->content = "";
6779 //This is the endTag handler we use where we are reading the groupings zone (todo="GROUPINGS")
6780 function endElementGroupings($parser, $tagName) {
6781 //Check if we are into GROUPINGS zone
6782 if ($this->tree[3] == "GROUPINGS") {
6783 //Acumulate data to info (content + close tag)
6784 //Reconvert: strip htmlchars again and trim to generate xml data
6785 if (!isset($this->temp)) {
6786 $this->temp = "";
6788 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6789 //If we've finished a group, xmlize it an save to db
6790 if (($this->level == 4) and ($tagName == "GROUPING")) {
6791 //Prepend XML standard header to info gathered
6792 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6793 //Call to xmlize for this portion of xml data (one GROUPING)
6794 $data = xmlize($xml_data,0);
6795 //Now, save data to db. We'll use it later
6796 //Get id and from data
6797 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
6798 //Save to db
6799 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
6800 null,$data);
6801 //Create returning info
6802 $ret_info = new Object();
6803 $ret_info->id = $grouping_id;
6804 $this->info[] = $ret_info;
6805 //Reset temp
6806 unset($this->temp);
6810 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6811 //Speed up a lot (avoid parse all)
6812 if ($tagName == "GROUPINGS" and $this->level == 3) {
6813 $this->finished = true;
6816 //Clear things
6817 $this->tree[$this->level] = "";
6818 $this->level--;
6819 $this->content = "";
6823 //This is the endTag handler we use where we are reading the groupingsgroups zone (todo="GROUPINGGROUPS")
6824 function endElementGroupingsGroups($parser, $tagName) {
6825 //Check if we are into GROUPINGSGROUPS zone
6826 if ($this->tree[3] == "GROUPINGSGROUPS") {
6827 //Acumulate data to info (content + close tag)
6828 //Reconvert: strip htmlchars again and trim to generate xml data
6829 if (!isset($this->temp)) {
6830 $this->temp = "";
6832 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6833 //If we've finished a group, xmlize it an save to db
6834 if (($this->level == 4) and ($tagName == "GROUPINGGROUP")) {
6835 //Prepend XML standard header to info gathered
6836 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6837 //Call to xmlize for this portion of xml data (one GROUPING)
6838 $data = xmlize($xml_data,0);
6839 //Now, save data to db. We'll use it later
6840 //Get id and from data
6841 $groupinggroup_id = $data["GROUPINGGROUP"]["#"]["ID"]["0"]["#"];
6842 //Save to db
6843 $status = backup_putid($this->preferences->backup_unique_code,"groupingsgroups",$groupinggroup_id,
6844 null,$data);
6845 //Create returning info
6846 $ret_info = new Object();
6847 $ret_info->id = $groupinggroup_id;
6848 $this->info[] = $ret_info;
6849 //Reset temp
6850 unset($this->temp);
6854 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6855 //Speed up a lot (avoid parse all)
6856 if ($tagName == "GROUPINGSGROUPS" and $this->level == 3) {
6857 $this->finished = true;
6860 //Clear things
6861 $this->tree[$this->level] = "";
6862 $this->level--;
6863 $this->content = "";
6867 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
6868 function endElementEvents($parser, $tagName) {
6869 //Check if we are into EVENTS zone
6870 if ($this->tree[3] == "EVENTS") {
6871 //if (trim($this->content)) //Debug
6872 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6873 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6874 //Acumulate data to info (content + close tag)
6875 //Reconvert: strip htmlchars again and trim to generate xml data
6876 if (!isset($this->temp)) {
6877 $this->temp = "";
6879 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6880 //If we've finished a event, xmlize it an save to db
6881 if (($this->level == 4) and ($tagName == "EVENT")) {
6882 //Prepend XML standard header to info gathered
6883 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6884 //Call to xmlize for this portion of xml data (one EVENT)
6885 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6886 $data = xmlize($xml_data,0);
6887 //echo strftime ("%X",time())."<p>"; //Debug
6888 //traverse_xmlize($data); //Debug
6889 //print_object ($GLOBALS['traverse_array']); //Debug
6890 //$GLOBALS['traverse_array']=""; //Debug
6891 //Now, save data to db. We'll use it later
6892 //Get id and from data
6893 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
6894 //Save to db
6895 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
6896 null,$data);
6897 //Create returning info
6898 $ret_info = new object();
6899 $ret_info->id = $event_id;
6900 $this->info[] = $ret_info;
6901 //Reset temp
6902 unset($this->temp);
6906 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
6907 //Speed up a lot (avoid parse all)
6908 if ($tagName == "EVENTS" and $this->level == 3) {
6909 $this->finished = true;
6912 //Clear things
6913 $this->tree[$this->level] = "";
6914 $this->level--;
6915 $this->content = "";
6919 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
6920 function endElementModules($parser, $tagName) {
6921 //Check if we are into MODULES zone
6922 if ($this->tree[3] == "MODULES") {
6923 //if (trim($this->content)) //Debug
6924 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6925 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6926 //Acumulate data to info (content + close tag)
6927 //Reconvert: strip htmlchars again and trim to generate xml data
6928 if (!isset($this->temp)) {
6929 $this->temp = "";
6931 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6932 //If we've finished a mod, xmlize it an save to db
6933 if (($this->level == 4) and ($tagName == "MOD")) {
6934 //Prepend XML standard header to info gathered
6935 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6936 //Call to xmlize for this portion of xml data (one MOD)
6937 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6938 $data = xmlize($xml_data,0);
6939 //echo strftime ("%X",time())."<p>"; //Debug
6940 //traverse_xmlize($data); //Debug
6941 //print_object ($GLOBALS['traverse_array']); //Debug
6942 //$GLOBALS['traverse_array']=""; //Debug
6943 //Now, save data to db. We'll use it later
6944 //Get id and modtype from data
6945 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
6946 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
6947 //Only if we've selected to restore it
6948 if (!empty($this->preferences->mods[$mod_type]->restore)) {
6949 //Save to db
6950 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
6951 null,$data);
6952 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6953 //Create returning info
6954 $ret_info = new object();
6955 $ret_info->id = $mod_id;
6956 $ret_info->modtype = $mod_type;
6957 $this->info[] = $ret_info;
6959 //Reset temp
6960 unset($this->temp);
6966 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
6967 //Speed up a lot (avoid parse all)
6968 if ($tagName == "MODULES" and $this->level == 3) {
6969 $this->finished = true;
6972 //Clear things
6973 $this->tree[$this->level] = "";
6974 $this->level--;
6975 $this->content = "";
6979 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
6980 function endElementLogs($parser, $tagName) {
6981 //Check if we are into LOGS zone
6982 if ($this->tree[3] == "LOGS") {
6983 //if (trim($this->content)) //Debug
6984 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6985 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6986 //Acumulate data to info (content + close tag)
6987 //Reconvert: strip htmlchars again and trim to generate xml data
6988 if (!isset($this->temp)) {
6989 $this->temp = "";
6991 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6992 //If we've finished a log, xmlize it an save to db
6993 if (($this->level == 4) and ($tagName == "LOG")) {
6994 //Prepend XML standard header to info gathered
6995 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6996 //Call to xmlize for this portion of xml data (one LOG)
6997 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6998 $data = xmlize($xml_data,0);
6999 //echo strftime ("%X",time())."<p>"; //Debug
7000 //traverse_xmlize($data); //Debug
7001 //print_object ($GLOBALS['traverse_array']); //Debug
7002 //$GLOBALS['traverse_array']=""; //Debug
7003 //Now, save data to db. We'll use it later
7004 //Get id and modtype from data
7005 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
7006 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
7007 //We only save log entries from backup file if they are:
7008 // - Course logs
7009 // - User logs
7010 // - Module logs about one restored module
7011 if ($log_module == "course" or
7012 $log_module == "user" or
7013 $this->preferences->mods[$log_module]->restore) {
7014 //Increment counter
7015 $this->counter++;
7016 //Save to db
7017 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
7018 null,$data);
7019 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
7020 //Create returning info
7021 $this->info = $this->counter;
7023 //Reset temp
7024 unset($this->temp);
7028 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
7029 //Speed up a lot (avoid parse all)
7030 if ($tagName == "LOGS" and $this->level == 3) {
7031 $this->finished = true;
7032 $this->counter = 0;
7035 //Clear things
7036 $this->tree[$this->level] = "";
7037 $this->level--;
7038 $this->content = "";
7042 //This is the endTag default handler we use when todo is undefined
7043 function endElement($parser, $tagName) {
7044 if (trim($this->content)) //Debug
7045 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
7046 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
7048 //Clear things
7049 $this->tree[$this->level] = "";
7050 $this->level--;
7051 $this->content = "";
7054 //This is the handler to read data contents (simple accumule it)
7055 function characterData($parser, $data) {
7056 $this->content .= $data;
7060 //This function executes the MoodleParser
7061 function restore_read_xml ($xml_file,$todo,$preferences) {
7063 $status = true;
7065 $xml_parser = xml_parser_create('UTF-8');
7066 $moodle_parser = new MoodleParser();
7067 $moodle_parser->todo = $todo;
7068 $moodle_parser->preferences = $preferences;
7069 xml_set_object($xml_parser,$moodle_parser);
7070 //Depending of the todo we use some element_handler or another
7071 if ($todo == "INFO") {
7072 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
7073 } else if ($todo == "ROLES") {
7074 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
7075 } else if ($todo == "COURSE_HEADER") {
7076 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
7077 } else if ($todo == 'BLOCKS') {
7078 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
7079 } else if ($todo == "SECTIONS") {
7080 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
7081 } else if ($todo == 'FORMATDATA') {
7082 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
7083 } else if ($todo == "METACOURSE") {
7084 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
7085 } else if ($todo == "GRADEBOOK") {
7086 if ($preferences->backup_version > 2007090500) {
7087 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
7088 } else {
7089 xml_set_element_handler($xml_parser, "startElementOldGradebook", "endElementOldGradebook");
7091 } else if ($todo == "USERS") {
7092 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
7093 } else if ($todo == "MESSAGES") {
7094 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
7095 } else if ($todo == "BLOGS") {
7096 xml_set_element_handler($xml_parser, "startElementBlogs", "endElementBlogs");
7097 } else if ($todo == "QUESTIONS") {
7098 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
7099 } else if ($todo == "SCALES") {
7100 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
7101 } else if ($todo == "GROUPS") {
7102 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
7103 } else if ($todo == "GROUPINGS") {
7104 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
7105 } else if ($todo == "GROUPINGSGROUPS") {
7106 xml_set_element_handler($xml_parser, "startElementGroupingsGroups", "endElementGroupingsGroups");
7107 } else if ($todo == "EVENTS") {
7108 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
7109 } else if ($todo == "MODULES") {
7110 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
7111 } else if ($todo == "LOGS") {
7112 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
7113 } else {
7114 //Define default handlers (must no be invoked when everything become finished)
7115 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
7117 xml_set_character_data_handler($xml_parser, "characterData");
7118 $fp = fopen($xml_file,"r")
7119 or $status = false;
7120 if ($status) {
7121 // MDL-9290 performance improvement on reading large xml
7122 $lasttime = time(); // crmas
7123 while ($data = fread($fp, 4096) and !$moodle_parser->finished) {
7125 if ((time() - $lasttime) > 5) {
7126 $lasttime = time();
7127 backup_flush(1);
7130 xml_parse($xml_parser, $data, feof($fp))
7131 or die(sprintf("XML error: %s at line %d",
7132 xml_error_string(xml_get_error_code($xml_parser)),
7133 xml_get_current_line_number($xml_parser)));
7135 fclose($fp);
7137 //Get info from parser
7138 $info = $moodle_parser->info;
7140 //Clear parser mem
7141 xml_parser_free($xml_parser);
7143 if ($status && !empty($info)) {
7144 return $info;
7145 } else {
7146 return $status;
7151 * @param string $errorstr passed by reference, if silent is true,
7152 * errorstr will be populated and this function will return false rather than calling error() or notify()
7153 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
7154 * redirect to the next step in the restore process, instead will return $backup_unique_code
7156 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
7158 global $CFG, $SESSION;
7160 //Prepend dataroot to variable to have the absolute path
7161 $file = $CFG->dataroot."/".$file;
7163 if (!defined('RESTORE_SILENTLY')) {
7164 //Start the main table
7165 echo "<table cellpadding=\"5\">";
7166 echo "<tr><td>";
7168 //Start the mail ul
7169 echo "<ul>";
7172 //Check the file exists
7173 if (!is_file($file)) {
7174 if (!defined('RESTORE_SILENTLY')) {
7175 error ("File not exists ($file)");
7176 } else {
7177 $errorstr = "File not exists ($file)";
7178 return false;
7182 //Check the file name ends with .zip
7183 if (!substr($file,-4) == ".zip") {
7184 if (!defined('RESTORE_SILENTLY')) {
7185 error ("File has an incorrect extension");
7186 } else {
7187 $errorstr = 'File has an incorrect extension';
7188 return false;
7192 //Now calculate the unique_code for this restore
7193 $backup_unique_code = time();
7195 //Now check and create the backup dir (if it doesn't exist)
7196 if (!defined('RESTORE_SILENTLY')) {
7197 echo "<li>".get_string("creatingtemporarystructures").'</li>';
7199 $status = check_and_create_backup_dir($backup_unique_code);
7200 //Empty dir
7201 if ($status) {
7202 $status = clear_backup_dir($backup_unique_code);
7205 //Now delete old data and directories under dataroot/temp/backup
7206 if ($status) {
7207 if (!defined('RESTORE_SILENTLY')) {
7208 echo "<li>".get_string("deletingolddata").'</li>';
7210 $status = backup_delete_old_data();
7213 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
7214 if ($status) {
7215 if (!defined('RESTORE_SILENTLY')) {
7216 echo "<li>".get_string("copyingzipfile").'</li>';
7218 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
7219 if (!defined('RESTORE_SILENTLY')) {
7220 notify("Error copying backup file. Invalid name or bad perms.");
7221 } else {
7222 $errorstr = "Error copying backup file. Invalid name or bad perms";
7223 return false;
7228 //Now unzip the file
7229 if ($status) {
7230 if (!defined('RESTORE_SILENTLY')) {
7231 echo "<li>".get_string("unzippingbackup").'</li>';
7233 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
7234 if (!defined('RESTORE_SILENTLY')) {
7235 notify("Error unzipping backup file. Invalid zip file.");
7236 } else {
7237 $errorstr = "Error unzipping backup file. Invalid zip file.";
7238 return false;
7243 //Check for Blackboard backups and convert
7244 if ($status){
7245 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
7246 if (!defined('RESTORE_SILENTLY')) {
7247 echo "<li>".get_string("checkingforbbexport").'</li>';
7249 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
7252 //Now check for the moodle.xml file
7253 if ($status) {
7254 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
7255 if (!defined('RESTORE_SILENTLY')) {
7256 echo "<li>".get_string("checkingbackup").'</li>';
7258 if (! $status = restore_check_moodle_file ($xml_file)) {
7259 if (!is_file($xml_file)) {
7260 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
7261 } else {
7262 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
7264 if (!defined('RESTORE_SILENTLY')) {
7265 notify($errorstr);
7266 } else {
7267 return false;
7272 $info = "";
7273 $course_header = "";
7275 //Now read the info tag (all)
7276 if ($status) {
7277 if (!defined('RESTORE_SILENTLY')) {
7278 echo "<li>".get_string("readinginfofrombackup").'</li>';
7280 //Reading info from file
7281 $info = restore_read_xml_info ($xml_file);
7282 //Reading course_header from file
7283 $course_header = restore_read_xml_course_header ($xml_file);
7285 if(!is_object($course_header)){
7286 // ensure we fail if there is no course header
7287 $course_header = false;
7291 if (!defined('RESTORE_SILENTLY')) {
7292 //End the main ul
7293 echo "</ul>\n";
7295 //End the main table
7296 echo "</td></tr>";
7297 echo "</table>";
7300 //We compare Moodle's versions
7301 if ($CFG->version < $info->backup_moodle_version && $status) {
7302 $message = new message();
7303 $message->serverversion = $CFG->version;
7304 $message->serverrelease = $CFG->release;
7305 $message->backupversion = $info->backup_moodle_version;
7306 $message->backuprelease = $info->backup_moodle_release;
7307 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
7311 //Now we print in other table, the backup and the course it contains info
7312 if ($info and $course_header and $status) {
7313 //First, the course info
7314 if (!defined('RESTORE_SILENTLY')) {
7315 $status = restore_print_course_header($course_header);
7317 //Now, the backup info
7318 if ($status) {
7319 if (!defined('RESTORE_SILENTLY')) {
7320 $status = restore_print_info($info);
7325 //Save course header and info into php session
7326 if ($status) {
7327 $SESSION->info = $info;
7328 $SESSION->course_header = $course_header;
7331 //Finally, a little form to continue
7332 //with some hidden fields
7333 if ($status) {
7334 if (!defined('RESTORE_SILENTLY')) {
7335 echo "<br /><div style='text-align:center'>";
7336 $hidden["backup_unique_code"] = $backup_unique_code;
7337 $hidden["launch"] = "form";
7338 $hidden["file"] = $file;
7339 $hidden["id"] = $id;
7340 print_single_button("restore.php", $hidden, get_string("continue"),"post");
7341 echo "</div>";
7343 else {
7344 if (empty($noredirect)) {
7345 redirect($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
7346 } else {
7347 return $backup_unique_code;
7352 if (!$status) {
7353 if (!defined('RESTORE_SILENTLY')) {
7354 error ("An error has ocurred");
7355 } else {
7356 $errorstr = "An error has occured"; // helpful! :P
7357 return false;
7360 return true;
7363 function restore_setup_for_check(&$restore,$backup_unique_code) {
7364 global $SESSION;
7365 $restore->backup_unique_code=$backup_unique_code;
7366 $restore->users = 2; // yuk
7367 $restore->course_files = $SESSION->restore->restore_course_files;
7368 $restore->site_files = $SESSION->restore->restore_site_files;
7369 if ($allmods = get_records("modules")) {
7370 foreach ($allmods as $mod) {
7371 $modname = $mod->name;
7372 $var = "restore_".$modname;
7373 //Now check that we have that module info in the backup file
7374 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
7375 $restore->$var = 1;
7379 return true;
7382 function backup_to_restore_array($backup,$k=0) {
7383 if (is_array($backup) ) {
7384 foreach ($backup as $key => $value) {
7385 $newkey = str_replace('backup','restore',$key);
7386 $restore[$newkey] = backup_to_restore_array($value,$key);
7389 else if (is_object($backup)) {
7390 $tmp = get_object_vars($backup);
7391 foreach ($tmp as $key => $value) {
7392 $newkey = str_replace('backup','restore',$key);
7393 $restore->$newkey = backup_to_restore_array($value,$key);
7396 else {
7397 $newkey = str_replace('backup','restore',$k);
7398 $restore = $backup;
7400 return $restore;
7404 * compatibility function
7405 * checks for per-instance backups AND
7406 * older per-module backups
7407 * and returns whether userdata has been selected.
7409 function restore_userdata_selected($restore,$modname,$modid) {
7410 // check first for per instance array
7411 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
7412 return array_key_exists($modid,$restore->mods[$modname]->instances)
7413 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
7416 //print_object($restore->mods[$modname]);
7417 return !empty($restore->mods[$modname]->userinfo);
7420 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
7422 global $CFG, $USER;
7423 $status = true;
7425 //Checks for the required files/functions to restore every module
7426 //and include them
7427 if ($allmods = get_records("modules") ) {
7428 foreach ($allmods as $mod) {
7429 $modname = $mod->name;
7430 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
7431 //If file exists and we have selected to restore that type of module
7432 if ((file_exists($modfile)) and !empty($restore->mods[$modname]) and ($restore->mods[$modname]->restore)) {
7433 include_once($modfile);
7438 if (!defined('RESTORE_SILENTLY')) {
7439 //Start the main table
7440 echo "<table cellpadding=\"5\">";
7441 echo "<tr><td>";
7443 //Start the main ul
7444 echo "<ul>";
7447 //Localtion of the xml file
7448 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
7450 //If we've selected to restore into new course
7451 //create it (course)
7452 //Saving conversion id variables into backup_tables
7453 if ($restore->restoreto == 2) {
7454 if (!defined('RESTORE_SILENTLY')) {
7455 echo '<li>'.get_string('creatingnewcourse') . '</li>';
7457 $oldidnumber = $course_header->course_idnumber;
7458 if (!$status = restore_create_new_course($restore,$course_header)) {
7459 if (!defined('RESTORE_SILENTLY')) {
7460 notify("Error while creating the new empty course.");
7461 } else {
7462 $errorstr = "Error while creating the new empty course.";
7463 return false;
7467 //Print course fullname and shortname and category
7468 if ($status) {
7469 if (!defined('RESTORE_SILENTLY')) {
7470 echo "<ul>";
7471 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
7472 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
7473 if (!empty($oldidnumber)) {
7474 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
7476 echo "</ul>";
7477 //Put the destination course_id
7479 $restore->course_id = $course_header->course_id;
7482 if ($status = restore_open_html($restore,$course_header)){
7483 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
7486 } else {
7487 $course = get_record("course","id",$restore->course_id);
7488 if ($course) {
7489 if (!defined('RESTORE_SILENTLY')) {
7490 echo "<li>".get_string("usingexistingcourse");
7491 echo "<ul>";
7492 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
7493 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
7494 if (($restore->deleting)) {
7495 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
7496 } else {
7497 echo "<li>".get_string("addingdatatoexisting").'</li>';
7499 echo "</ul></li>";
7501 //If we have selected to restore deleting, we do it now.
7502 if ($restore->deleting) {
7503 if (!defined('RESTORE_SILENTLY')) {
7504 echo "<li>".get_string("deletingolddata").'</li>';
7506 $status = remove_course_contents($restore->course_id,false) and
7507 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
7508 if ($status) {
7509 //Now , this situation is equivalent to the "restore to new course" one (we
7510 //have a course record and nothing more), so define it as "to new course"
7511 $restore->restoreto = 2;
7512 } else {
7513 if (!defined('RESTORE_SILENTLY')) {
7514 notify("An error occurred while deleting some of the course contents.");
7515 } else {
7516 $errrostr = "An error occurred while deleting some of the course contents.";
7517 return false;
7521 } else {
7522 if (!defined('RESTORE_SILENTLY')) {
7523 notify("Error opening existing course.");
7524 $status = false;
7525 } else {
7526 $errorstr = "Error opening existing course.";
7527 return false;
7532 //Now create users as needed
7533 if ($status and ($restore->users == 0 or $restore->users == 1)) {
7534 if (!defined('RESTORE_SILENTLY')) {
7535 echo "<li>".get_string("creatingusers")."<br />";
7537 if (!$status = restore_create_users($restore,$xml_file)) {
7538 if (!defined('RESTORE_SILENTLY')) {
7539 notify("Could not restore users.");
7540 } else {
7541 $errorstr = "Could not restore users.";
7542 return false;
7546 //Now print info about the work done
7547 if ($status) {
7548 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
7549 where backup_code = '$restore->backup_unique_code' and
7550 table_name = 'user'");
7551 //We've records
7552 if ($recs) {
7553 $new_count = 0;
7554 $exists_count = 0;
7555 $student_count = 0;
7556 $teacher_count = 0;
7557 $counter = 0;
7558 //Iterate, filling counters
7559 foreach ($recs as $rec) {
7560 //Get full record, using backup_getids
7561 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
7562 if (strpos($record->info,"new") !== false) {
7563 $new_count++;
7565 if (strpos($record->info,"exists") !== false) {
7566 $exists_count++;
7568 if (strpos($record->info,"student") !== false) {
7569 $student_count++;
7570 } else if (strpos($record->info,"teacher") !== false) {
7571 $teacher_count++;
7573 //Do some output
7574 $counter++;
7575 if ($counter % 10 == 0) {
7576 if (!defined('RESTORE_SILENTLY')) {
7577 echo ".";
7578 if ($counter % 200 == 0) {
7579 echo "<br />";
7582 backup_flush(300);
7585 if (!defined('RESTORE_SILENTLY')) {
7586 //Now print information gathered
7587 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
7588 echo "<ul>";
7589 echo "<li>".get_string("students").": ".$student_count.'</li>';
7590 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
7591 echo "</ul>";
7593 } else {
7594 if (!defined('RESTORE_SILENTLY')) {
7595 notify("No users were found!");
7596 } // no need to return false here, it's recoverable.
7600 if (!defined('RESTORE_SILENTLY')) {
7601 echo "</li>";
7606 //Now create groups as needed
7607 if ($status and ($restore->groups == RESTORE_GROUPS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
7608 if (!defined('RESTORE_SILENTLY')) {
7609 echo "<li>".get_string("creatinggroups");
7611 if (!$status = restore_create_groups($restore,$xml_file)) {
7612 if (!defined('RESTORE_SILENTLY')) {
7613 notify("Could not restore groups!");
7614 } else {
7615 $errorstr = "Could not restore groups!";
7616 return false;
7619 if (!defined('RESTORE_SILENTLY')) {
7620 echo '</li>';
7624 //Now create groupings as needed
7625 if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
7626 if (!defined('RESTORE_SILENTLY')) {
7627 echo "<li>".get_string("creatinggroupings");
7629 if (!$status = restore_create_groupings($restore,$xml_file)) {
7630 if (!defined('RESTORE_SILENTLY')) {
7631 notify("Could not restore groupings!");
7632 } else {
7633 $errorstr = "Could not restore groupings!";
7634 return false;
7637 if (!defined('RESTORE_SILENTLY')) {
7638 echo '</li>';
7642 //Now create groupingsgroups as needed
7643 if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) {
7644 if (!defined('RESTORE_SILENTLY')) {
7645 echo "<li>".get_string("creatinggroupingsgroups");
7647 if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
7648 if (!defined('RESTORE_SILENTLY')) {
7649 notify("Could not restore groups in groupings!");
7650 } else {
7651 $errorstr = "Could not restore groups in groupings!";
7652 return false;
7655 if (!defined('RESTORE_SILENTLY')) {
7656 echo '</li>';
7661 //Now create the course_sections and their associated course_modules
7662 //we have to do this after groups and groupings are restored, because we need the new groupings id
7663 if ($status) {
7664 //Into new course
7665 if ($restore->restoreto == 2) {
7666 if (!defined('RESTORE_SILENTLY')) {
7667 echo "<li>".get_string("creatingsections");
7669 if (!$status = restore_create_sections($restore,$xml_file)) {
7670 if (!defined('RESTORE_SILENTLY')) {
7671 notify("Error creating sections in the existing course.");
7672 } else {
7673 $errorstr = "Error creating sections in the existing course.";
7674 return false;
7677 if (!defined('RESTORE_SILENTLY')) {
7678 echo '</li>';
7680 //Into existing course
7681 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
7682 if (!defined('RESTORE_SILENTLY')) {
7683 echo "<li>".get_string("checkingsections");
7685 if (!$status = restore_create_sections($restore,$xml_file)) {
7686 if (!defined('RESTORE_SILENTLY')) {
7687 notify("Error creating sections in the existing course.");
7688 } else {
7689 $errorstr = "Error creating sections in the existing course.";
7690 return false;
7693 if (!defined('RESTORE_SILENTLY')) {
7694 echo '</li>';
7696 //Error
7697 } else {
7698 if (!defined('RESTORE_SILENTLY')) {
7699 notify("Neither a new course or an existing one was specified.");
7700 $status = false;
7701 } else {
7702 $errorstr = "Neither a new course or an existing one was specified.";
7703 return false;
7708 //Now create metacourse info
7709 if ($status and $restore->metacourse) {
7710 //Only to new courses!
7711 if ($restore->restoreto == 2) {
7712 if (!defined('RESTORE_SILENTLY')) {
7713 echo "<li>".get_string("creatingmetacoursedata");
7715 if (!$status = restore_create_metacourse($restore,$xml_file)) {
7716 if (!defined('RESTORE_SILENTLY')) {
7717 notify("Error creating metacourse in the course.");
7718 } else {
7719 $errorstr = "Error creating metacourse in the course.";
7720 return false;
7723 if (!defined('RESTORE_SILENTLY')) {
7724 echo '</li>';
7730 //Now create categories and questions as needed
7731 if ($status) {
7732 include_once("$CFG->dirroot/question/restorelib.php");
7733 if (!defined('RESTORE_SILENTLY')) {
7734 echo "<li>".get_string("creatingcategoriesandquestions");
7735 echo "<ul>";
7737 if (!$status = restore_create_questions($restore,$xml_file)) {
7738 if (!defined('RESTORE_SILENTLY')) {
7739 notify("Could not restore categories and questions!");
7740 } else {
7741 $errorstr = "Could not restore categories and questions!";
7742 return false;
7745 if (!defined('RESTORE_SILENTLY')) {
7746 echo "</ul></li>";
7750 //Now create user_files as needed
7751 if ($status and ($restore->user_files)) {
7752 if (!defined('RESTORE_SILENTLY')) {
7753 echo "<li>".get_string("copyinguserfiles");
7755 if (!$status = restore_user_files($restore)) {
7756 if (!defined('RESTORE_SILENTLY')) {
7757 notify("Could not restore user files!");
7758 } else {
7759 $errorstr = "Could not restore user files!";
7760 return false;
7763 //If all is ok (and we have a counter)
7764 if ($status and ($status !== true)) {
7765 //Inform about user dirs created from backup
7766 if (!defined('RESTORE_SILENTLY')) {
7767 echo "<ul>";
7768 echo "<li>".get_string("userzones").": ".$status;
7769 echo "</li></ul>";
7772 if (!defined('RESTORE_SILENTLY')) {
7773 echo '</li>';
7777 //Now create course files as needed
7778 if ($status and ($restore->course_files)) {
7779 if (!defined('RESTORE_SILENTLY')) {
7780 echo "<li>".get_string("copyingcoursefiles");
7782 if (!$status = restore_course_files($restore)) {
7783 if (empty($status)) {
7784 notify("Could not restore course files!");
7785 } else {
7786 $errorstr = "Could not restore course files!";
7787 return false;
7790 //If all is ok (and we have a counter)
7791 if ($status and ($status !== true)) {
7792 //Inform about user dirs created from backup
7793 if (!defined('RESTORE_SILENTLY')) {
7794 echo "<ul>";
7795 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7796 echo "</ul>";
7799 if (!defined('RESTORE_SILENTLY')) {
7800 echo "</li>";
7805 //Now create site files as needed
7806 if ($status and ($restore->site_files)) {
7807 if (!defined('RESTORE_SILENTLY')) {
7808 echo "<li>".get_string('copyingsitefiles');
7810 if (!$status = restore_site_files($restore)) {
7811 if (empty($status)) {
7812 notify("Could not restore site files!");
7813 } else {
7814 $errorstr = "Could not restore site files!";
7815 return false;
7818 //If all is ok (and we have a counter)
7819 if ($status and ($status !== true)) {
7820 //Inform about user dirs created from backup
7821 if (!defined('RESTORE_SILENTLY')) {
7822 echo "<ul>";
7823 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7824 echo "</ul>";
7827 if (!defined('RESTORE_SILENTLY')) {
7828 echo "</li>";
7832 //Now create messages as needed
7833 if ($status and ($restore->messages)) {
7834 if (!defined('RESTORE_SILENTLY')) {
7835 echo "<li>".get_string("creatingmessagesinfo");
7837 if (!$status = restore_create_messages($restore,$xml_file)) {
7838 if (!defined('RESTORE_SILENTLY')) {
7839 notify("Could not restore messages!");
7840 } else {
7841 $errorstr = "Could not restore messages!";
7842 return false;
7845 if (!defined('RESTORE_SILENTLY')) {
7846 echo "</li>";
7850 //Now create blogs as needed
7851 if ($status and ($restore->blogs)) {
7852 if (!defined('RESTORE_SILENTLY')) {
7853 echo "<li>".get_string("creatingblogsinfo");
7855 if (!$status = restore_create_blogs($restore,$xml_file)) {
7856 if (!defined('RESTORE_SILENTLY')) {
7857 notify("Could not restore blogs!");
7858 } else {
7859 $errorstr = "Could not restore blogs!";
7860 return false;
7863 if (!defined('RESTORE_SILENTLY')) {
7864 echo "</li>";
7868 //Now create scales as needed
7869 if ($status) {
7870 if (!defined('RESTORE_SILENTLY')) {
7871 echo "<li>".get_string("creatingscales");
7873 if (!$status = restore_create_scales($restore,$xml_file)) {
7874 if (!defined('RESTORE_SILENTLY')) {
7875 notify("Could not restore custom scales!");
7876 } else {
7877 $errorstr = "Could not restore custom scales!";
7878 return false;
7881 if (!defined('RESTORE_SILENTLY')) {
7882 echo '</li>';
7886 //Now create events as needed
7887 if ($status) {
7888 if (!defined('RESTORE_SILENTLY')) {
7889 echo "<li>".get_string("creatingevents");
7891 if (!$status = restore_create_events($restore,$xml_file)) {
7892 if (!defined('RESTORE_SILENTLY')) {
7893 notify("Could not restore course events!");
7894 } else {
7895 $errorstr = "Could not restore course events!";
7896 return false;
7899 if (!defined('RESTORE_SILENTLY')) {
7900 echo '</li>';
7904 //Now create course modules as needed
7905 if ($status) {
7906 if (!defined('RESTORE_SILENTLY')) {
7907 echo "<li>".get_string("creatingcoursemodules");
7909 if (!$status = restore_create_modules($restore,$xml_file)) {
7910 if (!defined('RESTORE_SILENTLY')) {
7911 notify("Could not restore modules!");
7912 } else {
7913 $errorstr = "Could not restore modules!";
7914 return false;
7917 if (!defined('RESTORE_SILENTLY')) {
7918 echo '</li>';
7922 //Bring back the course blocks -- do it AFTER the modules!!!
7923 if($status) {
7924 //If we are deleting and bringing into a course or making a new course, same situation
7925 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7926 if (!defined('RESTORE_SILENTLY')) {
7927 echo '<li>'.get_string('creatingblocks');
7929 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
7930 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
7931 if (!defined('RESTORE_SILENTLY')) {
7932 notify('Error while creating the course blocks');
7933 } else {
7934 $errorstr = "Error while creating the course blocks";
7935 return false;
7938 if (!defined('RESTORE_SILENTLY')) {
7939 echo '</li>';
7944 if($status) {
7945 //If we are deleting and bringing into a course or making a new course, same situation
7946 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7947 if (!defined('RESTORE_SILENTLY')) {
7948 echo '<li>'.get_string('courseformatdata');
7950 if (!$status = restore_set_format_data($restore, $xml_file)) {
7951 $error = "Error while setting the course format data";
7952 if (!defined('RESTORE_SILENTLY')) {
7953 notify($error);
7954 } else {
7955 $errorstr=$error;
7956 return false;
7959 if (!defined('RESTORE_SILENTLY')) {
7960 echo '</li>';
7965 //Now create log entries as needed
7966 if ($status and ($restore->logs)) {
7967 if (!defined('RESTORE_SILENTLY')) {
7968 echo "<li>".get_string("creatinglogentries");
7970 if (!$status = restore_create_logs($restore,$xml_file)) {
7971 if (!defined('RESTORE_SILENTLY')) {
7972 notify("Could not restore logs!");
7973 } else {
7974 $errorstr = "Could not restore logs!";
7975 return false;
7978 if (!defined('RESTORE_SILENTLY')) {
7979 echo '</li>';
7983 //Now, if all is OK, adjust the instance field in course_modules !!
7984 //this also calculates the final modinfo information so, after this,
7985 //code needing it can be used (like role_assignments. MDL-13740)
7986 if ($status) {
7987 if (!defined('RESTORE_SILENTLY')) {
7988 echo "<li>".get_string("checkinginstances");
7990 if (!$status = restore_check_instances($restore)) {
7991 if (!defined('RESTORE_SILENTLY')) {
7992 notify("Could not adjust instances in course_modules!");
7993 } else {
7994 $errorstr = "Could not adjust instances in course_modules!";
7995 return false;
7998 if (!defined('RESTORE_SILENTLY')) {
7999 echo '</li>';
8003 //Now, if all is OK, adjust activity events
8004 if ($status) {
8005 if (!defined('RESTORE_SILENTLY')) {
8006 echo "<li>".get_string("refreshingevents");
8008 if (!$status = restore_refresh_events($restore)) {
8009 if (!defined('RESTORE_SILENTLY')) {
8010 notify("Could not refresh events for activities!");
8011 } else {
8012 $errorstr = "Could not refresh events for activities!";
8013 return false;
8016 if (!defined('RESTORE_SILENTLY')) {
8017 echo '</li>';
8021 //Now, if all is OK, adjust inter-activity links
8022 if ($status) {
8023 if (!defined('RESTORE_SILENTLY')) {
8024 echo "<li>".get_string("decodinginternallinks");
8026 if (!$status = restore_decode_content_links($restore)) {
8027 if (!defined('RESTORE_SILENTLY')) {
8028 notify("Could not decode content links!");
8029 } else {
8030 $errorstr = "Could not decode content links!";
8031 return false;
8034 if (!defined('RESTORE_SILENTLY')) {
8035 echo '</li>';
8039 //Now, with backup files prior to version 2005041100,
8040 //convert all the wiki texts in the course to markdown
8041 if ($status && $restore->backup_version < 2005041100) {
8042 if (!defined('RESTORE_SILENTLY')) {
8043 echo "<li>".get_string("convertingwikitomarkdown");
8045 if (!$status = restore_convert_wiki2markdown($restore)) {
8046 if (!defined('RESTORE_SILENTLY')) {
8047 notify("Could not convert wiki texts to markdown!");
8048 } else {
8049 $errorstr = "Could not convert wiki texts to markdown!";
8050 return false;
8053 if (!defined('RESTORE_SILENTLY')) {
8054 echo '</li>';
8058 //Now create gradebook as needed -- AFTER modules and blocks!!!
8059 if ($status) {
8060 if ($restore->backup_version > 2007090500) {
8061 if (!defined('RESTORE_SILENTLY')) {
8062 echo "<li>".get_string("creatinggradebook");
8064 if (!$status = restore_create_gradebook($restore,$xml_file)) {
8065 if (!defined('RESTORE_SILENTLY')) {
8066 notify("Could not restore gradebook!");
8067 } else {
8068 $errorstr = "Could not restore gradebook!";
8069 return false;
8073 if (!defined('RESTORE_SILENTLY')) {
8074 echo '</li>';
8077 } else {
8078 // for moodle versions before 1.9, those grades need to be converted to use the new gradebook
8079 // this code needs to execute *after* the course_modules are sorted out
8080 if (!defined('RESTORE_SILENTLY')) {
8081 echo "<li>".get_string("migratinggrades");
8084 /// force full refresh of grading data before migration == crete all items first
8085 if (!$status = restore_migrate_old_gradebook($restore,$xml_file)) {
8086 if (!defined('RESTORE_SILENTLY')) {
8087 notify("Could not migrate gradebook!");
8088 } else {
8089 $errorstr = "Could not migrade gradebook!";
8090 return false;
8093 if (!defined('RESTORE_SILENTLY')) {
8094 echo '</li>';
8097 /// force full refresh of grading data after all items are created
8098 grade_force_full_regrading($restore->course_id);
8099 grade_grab_course_grades($restore->course_id);
8102 /*******************************************************************************
8103 ************* Restore of Roles and Capabilities happens here ******************
8104 *******************************************************************************/
8105 // try to restore roles even when restore is going to fail - teachers might have
8106 // at least some role assigned - this is not correct though
8107 $status = restore_create_roles($restore, $xml_file) && $status;
8108 $status = restore_roles_settings($restore, $xml_file) && $status;
8110 //Now if all is OK, update:
8111 // - course modinfo field
8112 // - categories table
8113 // - add user as teacher
8114 if ($status) {
8115 if (!defined('RESTORE_SILENTLY')) {
8116 echo "<li>".get_string("checkingcourse");
8118 //categories table
8119 $course = get_record("course","id",$restore->course_id);
8120 fix_course_sortorder();
8121 // Check if the user has course update capability in the newly restored course
8122 // there is no need to load his capabilities again, because restore_roles_settings
8123 // would have loaded it anyway, if there is any assignments.
8124 // fix for MDL-6831
8125 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
8126 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
8127 // fix for MDL-9065, use the new config setting if exists
8128 if ($CFG->creatornewroleid) {
8129 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
8130 } else {
8131 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM))) {
8132 if ($legacyteacher = array_shift($legacyteachers)) {
8133 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
8135 } else {
8136 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
8140 if (!defined('RESTORE_SILENTLY')) {
8141 echo '</li>';
8145 //Cleanup temps (files and db)
8146 if ($status) {
8147 if (!defined('RESTORE_SILENTLY')) {
8148 echo "<li>".get_string("cleaningtempdata");
8150 if (!$status = clean_temp_data ($restore)) {
8151 if (!defined('RESTORE_SILENTLY')) {
8152 notify("Could not clean up temporary data from files and database");
8153 } else {
8154 $errorstr = "Could not clean up temporary data from files and database";
8155 return false;
8158 if (!defined('RESTORE_SILENTLY')) {
8159 echo '</li>';
8163 // this is not a critical check - the result can be ignored
8164 if (restore_close_html($restore)){
8165 if (!defined('RESTORE_SILENTLY')) {
8166 echo '<li>Closing the Restorelog.html file.</li>';
8169 else {
8170 if (!defined('RESTORE_SILENTLY')) {
8171 notify("Could not close the restorelog.html file");
8175 if (!defined('RESTORE_SILENTLY')) {
8176 //End the main ul
8177 echo "</ul>";
8179 //End the main table
8180 echo "</td></tr>";
8181 echo "</table>";
8184 return $status;
8186 //Create, open and write header of the html log file
8187 function restore_open_html($restore,$course_header) {
8189 global $CFG;
8191 $status = true;
8193 //Open file for writing
8194 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
8195 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
8196 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
8198 $status = check_dir_exists($dest_dir,true);
8199 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
8200 //Add the stylesheet
8201 $stylesheetshtml = '';
8202 foreach ($CFG->stylesheets as $stylesheet) {
8203 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
8205 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
8206 $languagehtml = get_html_lang($dir=true);
8208 //Write the header in the new logging file
8209 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
8210 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
8211 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
8212 fwrite ($restorelog_file,"<head>");
8213 fwrite ($restorelog_file,$stylesheetshtml);
8214 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
8215 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
8216 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
8217 $startdate = addslashes($course_header->course_startdate);
8218 $date = usergetdate($startdate);
8219 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
8220 $startdate += $restore->course_startdateoffset;
8221 $date = usergetdate($startdate);
8222 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
8224 if ($status) {
8225 return $restorelog_file;
8226 } else {
8227 return false;
8230 //Create & close footer of the html log file
8231 function restore_close_html($restore) {
8233 global $CFG;
8235 $status = true;
8237 //Open file for writing
8238 //First, check that course_id/backupdata folder exists in CFG->dataroot
8239 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
8240 $status = check_dir_exists($dest_dir, true, true);
8241 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
8242 //Write the footer to close the logging file
8243 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
8244 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
8246 if ($status) {
8247 return $restorelog_file;
8248 } else {
8249 return false;
8253 /********************** Roles and Capabilities Related Functions *******************************/
8255 /* Yu: Note recovering of role assignments/overrides need to take place after
8256 users have been recovered, i.e. after we get their new_id, and after all
8257 roles have been recreated or mapped. Contexts can be created on the fly.
8258 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
8259 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
8260 once roles and users have been restored.
8264 * This function restores all the needed roles for this course
8265 * i.e. roles with an assignment in any of the mods or blocks,
8266 * roles assigned on any user (e.g. parent role) and roles
8267 * assigned at course levle
8268 * This function should check for duplicate roles first
8269 * It isn't now, just overwriting
8271 function restore_create_roles($restore, $xmlfile) {
8272 if (!defined('RESTORE_SILENTLY')) {
8273 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
8275 $info = restore_read_xml_roles($xmlfile);
8277 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
8279 // the following code creates new roles
8280 // but we could use more intelligent detection, and role mapping
8281 // get role mapping info from $restore
8282 $rolemappings = array();
8284 if (!empty($restore->rolesmapping)) {
8285 $rolemappings = $restore->rolesmapping;
8287 // $info->roles will be empty for backups pre 1.7
8288 if (isset($info->roles) && $info->roles) {
8290 foreach ($info->roles as $oldroleid=>$roledata) {
8292 if (empty($restore->rolesmapping)) {
8293 // if this is empty altogether, we came from import or there's no roles used in course at all
8294 // in this case, write the same oldid as this is the same site
8295 // no need to do mapping
8296 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
8297 $oldroleid); // adding a new id
8298 continue; // do not create additonal roles;
8300 // first we check if the roles are in the mappings
8301 // if so, we just do a mapping i.e. update oldids table
8302 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
8303 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
8304 $rolemappings[$oldroleid]); // adding a new id
8306 } else {
8308 // code to make new role name/short name if same role name or shortname exists
8309 $fullname = $roledata->name;
8310 $shortname = $roledata->shortname;
8311 $currentfullname = "";
8312 $currentshortname = "";
8313 $counter = 0;
8315 do {
8316 if ($counter) {
8317 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
8318 $suffixshort = "_".$counter;
8319 } else {
8320 $suffixfull = "";
8321 $suffixshort = "";
8323 $currentfullname = $fullname.$suffixfull;
8324 // Limit the size of shortname - database column accepts <= 100 chars
8325 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
8326 $coursefull = get_record("role","name",addslashes($currentfullname));
8327 $courseshort = get_record("role","shortname",addslashes($currentshortname));
8328 $counter++;
8329 } while ($coursefull || $courseshort);
8331 $roledata->name = $currentfullname;
8332 $roledata->shortname= $currentshortname;
8334 // done finding a unique name
8336 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
8337 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
8338 $newroleid); // adding a new id
8339 foreach ($roledata->capabilities as $capability) {
8341 $roleinfo = new object();
8342 $roleinfo = (object)$capability;
8343 $roleinfo->contextid = $sitecontext->id;
8344 $roleinfo->capability = $capability->name;
8345 $roleinfo->roleid = $newroleid;
8347 insert_record('role_capabilities', $roleinfo);
8350 /// Now, restore role nameincourse
8351 $newrole = backup_getid($restore->backup_unique_code, 'role', $oldroleid); /// Look for target role
8352 $coursecontext = get_context_instance(CONTEXT_COURSE, $restore->course_id); /// Look for target context
8353 if (!empty($newrole->new_id) && !empty($coursecontext)) {
8354 /// Check the role hasn't any custom name in context
8355 if (!record_exists('role_names', 'roleid', $newrole->new_id, 'contextid', $coursecontext->id)) {
8356 $rolename = new object();
8357 $rolename->roleid = $newrole->new_id;
8358 $rolename->contextid = $coursecontext->id;
8359 $rolename->name = addslashes($roledata->nameincourse);
8361 insert_record('role_names', $rolename);
8366 return true;
8370 * this function restores role assignments and role overrides
8371 * in course/user/block/mod level, it passed through
8372 * the xml file again
8374 function restore_roles_settings($restore, $xmlfile) {
8375 // data pulls from course, mod, user, and blocks
8377 /*******************************************************
8378 * Restoring from course level assignments *
8379 *******************************************************/
8380 if (!defined('RESTORE_SILENTLY')) {
8381 echo "<li>".get_string("creatingcourseroles").'</li>';
8383 $course = restore_read_xml_course_header($xmlfile);
8385 if (!isset($restore->rolesmapping)) {
8386 $isimport = true; // course import from another course, or course with no role assignments
8387 } else {
8388 $isimport = false; // course restore with role assignments
8391 if (!empty($course->roleassignments) && !$isimport) {
8392 $courseassignments = $course->roleassignments;
8394 foreach ($courseassignments as $oldroleid => $courseassignment) {
8395 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
8398 /*****************************************************
8399 * Restoring from course level overrides *
8400 *****************************************************/
8402 if (!empty($course->roleoverrides) && !$isimport) {
8403 $courseoverrides = $course->roleoverrides;
8404 foreach ($courseoverrides as $oldroleid => $courseoverride) {
8405 // if not importing into exiting course, or creating new role, we are ok
8406 // local course overrides to be respected (i.e. restored course overrides ignored)
8407 if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
8408 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
8413 /*******************************************************
8414 * Restoring role assignments/overrdies *
8415 * from module level assignments *
8416 *******************************************************/
8418 if (!defined('RESTORE_SILENTLY')) {
8419 echo "<li>".get_string("creatingmodroles").'</li>';
8421 $sections = restore_read_xml_sections($xmlfile);
8422 $secs = $sections->sections;
8424 foreach ($secs as $section) {
8425 if (isset($section->mods)) {
8426 foreach ($section->mods as $modid=>$mod) {
8427 if (isset($mod->roleassignments) && !$isimport) {
8428 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
8429 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
8432 // role overrides always applies, in import or backup/restore
8433 if (isset($mod->roleoverrides)) {
8434 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
8435 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
8442 /*************************************************
8443 * Restoring assignments from blocks level *
8444 * role assignments/overrides *
8445 *************************************************/
8447 if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
8448 if (!defined('RESTORE_SILENTLY')) {
8449 echo "<li>".get_string("creatingblocksroles").'</li>';
8451 $blocks = restore_read_xml_blocks($restore, $xmlfile);
8452 if (isset($blocks->instances)) {
8453 foreach ($blocks->instances as $instance) {
8454 if (isset($instance->roleassignments) && !$isimport) {
8455 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
8456 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
8460 // likewise block overrides should always be restored like mods
8461 if (isset($instance->roleoverrides)) {
8462 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
8463 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
8469 /************************************************
8470 * Restoring assignments from userid level *
8471 * role assignments/overrides *
8472 ************************************************/
8473 if (!defined('RESTORE_SILENTLY')) {
8474 echo "<li>".get_string("creatinguserroles").'</li>';
8476 $info = restore_read_xml_users($restore, $xmlfile);
8477 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
8478 //For each user, take its info from backup_ids
8479 foreach ($info->users as $userid) {
8480 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
8481 if (isset($rec->info->roleassignments)) {
8482 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
8483 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
8486 if (isset($rec->info->roleoverrides)) {
8487 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
8488 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
8494 return true;
8497 // auxillary function to write role assignments read from xml to db
8498 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
8500 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
8502 foreach ($assignments as $assignment) {
8504 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
8505 //Oh dear, $olduser... can be an object, $obj->string or bool!
8506 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
8507 continue;
8509 $assignment->userid = $olduser->new_id; // new userid here
8510 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
8511 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
8512 $assignment->roleid = $role->new_id; // restored new role id
8514 // hack to make the correct contextid for course level imports
8515 if ($contextlevel == CONTEXT_COURSE) {
8516 $oldinstance->new_id = $restore->course_id;
8517 } else {
8518 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
8521 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
8522 $assignment->contextid = $newcontext->id; // new context id
8523 // might already have same assignment
8524 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol, $assignment->timemodified);
8529 // auxillary function to write role assignments read from xml to db
8530 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
8532 // it is possible to have an override not relevant to this course context.
8533 // should be ignored(?)
8534 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
8535 return null;
8538 foreach ($overrides as $override) {
8539 $override->capability = $override->name;
8540 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
8541 $override->modifierid = !empty($oldmodifier->new_id)?$oldmodifier->new_id:0; // new modifier id here
8542 $override->roleid = $role->new_id; // restored new role id
8544 // hack to make the correct contextid for course level imports
8545 if ($contextlevel == CONTEXT_COURSE) {
8546 $oldinstance->new_id = $restore->course_id;
8547 } else {
8548 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
8551 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
8552 $override->contextid = $newcontext->id; // new context id
8553 // use assign capability instead so we can add context to context_rel
8554 assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
8557 //write activity date changes to the html log file, and update date values in the the xml array
8558 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
8560 global $CFG;
8561 $openlog = false;
8563 // loop through time fields in $TAGS
8564 foreach ($TAGS as $TAG) {
8566 // check $TAG has a sensible value
8567 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
8569 if ($openlog==false) {
8570 $openlog = true; // only come through here once
8572 // open file for writing
8573 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
8574 check_dir_exists($course_dir, true);
8575 $restorelog = fopen("$course_dir/restorelog.html", "a");
8577 // start output for this record
8578 $msg = new stdClass();
8579 $msg->recordtype = $recordtype;
8580 $msg->recordname = $xml[$NAMETAG][0]['#'];
8581 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
8584 // write old date to $restorelog
8585 $value = $xml[$TAG][0]['#'];
8586 $date = usergetdate($value);
8588 $msg = new stdClass();
8589 $msg->TAG = $TAG;
8590 $msg->weekday = $date['weekday'];
8591 $msg->mday = $date['mday'];
8592 $msg->month = $date['month'];
8593 $msg->year = $date['year'];
8594 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
8596 // write new date to $restorelog
8597 $value += $restore->course_startdateoffset;
8598 $date = usergetdate($value);
8600 $msg = new stdClass();
8601 $msg->TAG = $TAG;
8602 $msg->weekday = $date['weekday'];
8603 $msg->mday = $date['mday'];
8604 $msg->month = $date['month'];
8605 $msg->year = $date['year'];
8606 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
8608 // update $value in $xml tree for calling module
8609 $xml[$TAG][0]['#'] = "$value";
8612 // close the restore log, if it was opened
8613 if ($openlog) {
8614 fclose($restorelog);