MDL-10383 - fixed stupid bug from last commit, sorry
[moodle-pu.git] / backup / restorelib.php
blob80658e5ed33c25de781c000fde78667d4e571647
1 <?php //$Id$
2 //Functions used in restore
4 //This function unzips a zip file in the same directory that it is
5 //It automatically uses pclzip or command line unzip
6 function restore_unzip ($file) {
8 return unzip_file($file, '', false);
12 //This function checks if moodle.xml seems to be a valid xml file
13 //(exists, has an xml header and a course main tag
14 function restore_check_moodle_file ($file) {
16 $status = true;
18 //Check if it exists
19 if ($status = is_file($file)) {
20 //Open it and read the first 200 bytes (chars)
21 $handle = fopen ($file, "r");
22 $first_chars = fread($handle,200);
23 $status = fclose ($handle);
24 //Chek if it has the requires strings
25 if ($status) {
26 $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
27 if ($status !== false) {
28 $status = strpos($first_chars,"<MOODLE_BACKUP>");
33 return $status;
36 //This function iterates over all modules in backup file, searching for a
37 //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
38 function restore_refresh_events($restore) {
40 global $CFG;
41 $status = true;
43 //Take all modules in backup
44 $modules = $restore->mods;
45 //Iterate
46 foreach($modules as $name => $module) {
47 //Only if the module is being restored
48 if (isset($module->restore) && $module->restore == 1) {
49 //Include module library
50 include_once("$CFG->dirroot/mod/$name/lib.php");
51 //If module_refresh_events exists
52 $function_name = $name."_refresh_events";
53 if (function_exists($function_name)) {
54 $status = $function_name($restore->course_id);
58 return $status;
61 //This function makes all the necessary calls to xxxx_decode_content_links_caller()
62 //function in each module, passing them the desired contents to be decoded
63 //from backup format to destination site/course in order to mantain inter-activities
64 //working in the backup/restore process
65 function restore_decode_content_links($restore) {
66 global $CFG;
68 $status = true;
70 if (!defined('RESTORE_SILENTLY')) {
71 echo "<ul>";
74 // Restore links in modules.
75 foreach ($restore->mods as $name => $info) {
76 //If the module is being restored
77 if (isset($info->restore) && $info->restore == 1) {
78 //Check if the xxxx_decode_content_links_caller exists
79 include_once("$CFG->dirroot/mod/$name/restorelib.php");
80 $function_name = $name."_decode_content_links_caller";
81 if (function_exists($function_name)) {
82 if (!defined('RESTORE_SILENTLY')) {
83 echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
85 $status = $function_name($restore);
86 if (!defined('RESTORE_SILENTLY')) {
87 echo '</li>';
93 // TODO: process all html text also in blocks too
95 // Restore links in questions.
96 require_once("$CFG->dirroot/question/restorelib.php");
97 if (!defined('RESTORE_SILENTLY')) {
98 echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
100 $status = question_decode_content_links_caller($restore);
101 if (!defined('RESTORE_SILENTLY')) {
102 echo '</li>';
105 if (!defined('RESTORE_SILENTLY')) {
106 echo "</ul>";
109 return $status;
112 //This function is called from all xxxx_decode_content_links_caller(),
113 //its task is to ask all modules (maybe other linkable objects) to restore
114 //links to them.
115 function restore_decode_content_links_worker($content,$restore) {
116 foreach($restore->mods as $name => $info) {
117 $function_name = $name."_decode_content_links";
118 if (function_exists($function_name)) {
119 $content = $function_name($content,$restore);
122 return $content;
125 //This function converts all the wiki texts in the restored course
126 //to the Markdown format. Used only for backup files prior 2005041100.
127 //It calls every module xxxx_convert_wiki2markdown function
128 function restore_convert_wiki2markdown($restore) {
130 $status = true;
132 if (!defined('RESTORE_SILENTLY')) {
133 echo "<ul>";
135 foreach ($restore->mods as $name => $info) {
136 //If the module is being restored
137 if ($info->restore == 1) {
138 //Check if the xxxx_restore_wiki2markdown exists
139 $function_name = $name."_restore_wiki2markdown";
140 if (function_exists($function_name)) {
141 $status = $function_name($restore);
142 if (!defined('RESTORE_SILENTLY')) {
143 echo "<li>".get_string("modulenameplural",$name);
144 echo '</li>';
149 if (!defined('RESTORE_SILENTLY')) {
150 echo "</ul>";
152 return $status;
155 //This function receives a wiki text in the restore process and
156 //return it with every link to modules " modulename:moduleid"
157 //converted if possible. See the space before modulename!!
158 function restore_decode_wiki_content($content,$restore) {
160 global $CFG;
162 $result = $content;
164 $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
165 //We look for it
166 preg_match_all($searchstring,$content,$foundset);
167 //If found, then we are going to look for its new id (in backup tables)
168 if ($foundset[0]) {
169 //print_object($foundset); //Debug
170 //Iterate over foundset[2]. They are the old_ids
171 foreach($foundset[2] as $old_id) {
172 //We get the needed variables here (course id)
173 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
174 //Personalize the searchstring
175 $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
176 //If it is a link to this course, update the link to its new location
177 if($rec->new_id) {
178 //Now replace it
179 $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
180 } else {
181 //It's a foreign link so redirect it to its original URL
182 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
186 return $result;
190 //This function read the xml file and store it data from the info zone in an object
191 function restore_read_xml_info ($xml_file) {
193 //We call the main read_xml function, with todo = INFO
194 $info = restore_read_xml ($xml_file,"INFO",false);
196 return $info;
199 //This function read the xml file and store it data from the course header zone in an object
200 function restore_read_xml_course_header ($xml_file) {
202 //We call the main read_xml function, with todo = COURSE_HEADER
203 $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
205 return $info;
208 //This function read the xml file and store its data from the blocks in a object
209 function restore_read_xml_blocks ($xml_file) {
211 //We call the main read_xml function, with todo = BLOCKS
212 $info = restore_read_xml ($xml_file,'BLOCKS',false);
214 return $info;
217 //This function read the xml file and store its data from the sections in a object
218 function restore_read_xml_sections ($xml_file) {
220 //We call the main read_xml function, with todo = SECTIONS
221 $info = restore_read_xml ($xml_file,"SECTIONS",false);
223 return $info;
226 //This function read the xml file and store its data from the course format in an object
227 function restore_read_xml_formatdata ($xml_file) {
229 //We call the main read_xml function, with todo = FORMATDATA
230 $info = restore_read_xml ($xml_file,'FORMATDATA',false);
232 return $info;
235 //This function read the xml file and store its data from the metacourse in a object
236 function restore_read_xml_metacourse ($xml_file) {
238 //We call the main read_xml function, with todo = METACOURSE
239 $info = restore_read_xml ($xml_file,"METACOURSE",false);
241 return $info;
244 //This function read the xml file and store its data from the gradebook in a object
245 function restore_read_xml_gradebook ($restore, $xml_file) {
247 //We call the main read_xml function, with todo = GRADEBOOK
248 $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
250 return $info;
253 //This function read the xml file and store its data from the users in
254 //backup_ids->info db (and user's id in $info)
255 function restore_read_xml_users ($restore,$xml_file) {
257 //We call the main read_xml function, with todo = USERS
258 $info = restore_read_xml ($xml_file,"USERS",$restore);
260 return $info;
263 //This function read the xml file and store its data from the messages in
264 //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
265 function restore_read_xml_messages ($restore,$xml_file) {
267 //We call the main read_xml function, with todo = MESSAGES
268 $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
270 return $info;
274 //This function read the xml file and store its data from the questions in
275 //backup_ids->info db (and category's id in $info)
276 function restore_read_xml_questions ($restore,$xml_file) {
278 //We call the main read_xml function, with todo = QUESTIONS
279 $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
281 return $info;
284 //This function read the xml file and store its data from the scales in
285 //backup_ids->info db (and scale's id in $info)
286 function restore_read_xml_scales ($restore,$xml_file) {
288 //We call the main read_xml function, with todo = SCALES
289 $info = restore_read_xml ($xml_file,"SCALES",$restore);
291 return $info;
294 //This function read the xml file and store its data from the groups in
295 //backup_ids->info db (and group's id in $info)
296 function restore_read_xml_groups ($restore,$xml_file) {
298 //We call the main read_xml function, with todo = GROUPS
299 $info = restore_read_xml ($xml_file,"GROUPS",$restore);
301 return $info;
304 //This function read the xml file and store its data from the groupings in
305 //backup_ids->info db (and grouping's id in $info)
306 function restore_read_xml_groupings ($restore,$xml_file) {
308 //We call the main read_xml function, with todo = GROUPINGS
309 $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
311 return $info;
314 //This function read the xml file and store its data from the groupings in
315 //backup_ids->info db (and grouping's id in $info)
316 function restore_read_xml_groupings_groups ($restore,$xml_file) {
318 //We call the main read_xml function, with todo = GROUPINGS
319 $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
321 return $info;
324 //This function read the xml file and store its data from the events (course) in
325 //backup_ids->info db (and event's id in $info)
326 function restore_read_xml_events ($restore,$xml_file) {
328 //We call the main read_xml function, with todo = EVENTS
329 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
331 return $info;
334 //This function read the xml file and store its data from the modules in
335 //backup_ids->info
336 function restore_read_xml_modules ($restore,$xml_file) {
338 //We call the main read_xml function, with todo = MODULES
339 $info = restore_read_xml ($xml_file,"MODULES",$restore);
341 return $info;
344 //This function read the xml file and store its data from the logs in
345 //backup_ids->info
346 function restore_read_xml_logs ($restore,$xml_file) {
348 //We call the main read_xml function, with todo = LOGS
349 $info = restore_read_xml ($xml_file,"LOGS",$restore);
351 return $info;
354 function restore_read_xml_roles ($xml_file) {
355 //We call the main read_xml function, with todo = ROLES
356 $info = restore_read_xml ($xml_file,"ROLES",false);
358 return $info;
361 //This function prints the contents from the info parammeter passed
362 function restore_print_info ($info) {
364 global $CFG;
366 $status = true;
367 if ($info) {
368 $table = new object();
369 //This is tha align to every ingo table
370 $table->align = array ("right","left");
371 //This is the nowrap clause
372 $table->wrap = array ("","nowrap");
373 //The width
374 $table->width = "70%";
375 //Put interesting info in table
376 //The backup original name
377 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
378 $tab[0][1] = $info->backup_name;
379 //The moodle version
380 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
381 $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
382 //The backup version
383 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
384 $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
385 //The backup date
386 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
387 $tab[3][1] = userdate($info->backup_date);
388 //Print title
389 print_heading(get_string("backup").":");
390 $table->data = $tab;
391 //Print backup general info
392 print_table($table);
394 if ($info->backup_backup_version <= 2005070500) {
395 notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
398 //Now backup contents in another table
399 $tab = array();
400 //First mods info
401 $mods = $info->mods;
402 $elem = 0;
403 foreach ($mods as $key => $mod) {
404 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
405 if ($mod->backup == "false") {
406 $tab[$elem][1] = get_string("notincluded");
407 } else {
408 if ($mod->userinfo == "true") {
409 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
410 } else {
411 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
413 if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
414 foreach ($mod->instances as $instance) {
415 if ($instance->backup) {
416 $elem++;
417 $tab[$elem][0] = $instance->name;
418 if ($instance->userinfo == 'true') {
419 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
420 } else {
421 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
427 $elem++;
429 //Metacourse info
430 $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
431 if ($info->backup_metacourse == "true") {
432 $tab[$elem][1] = get_string("yes");
433 } else {
434 $tab[$elem][1] = get_string("no");
436 $elem++;
437 //Users info
438 $tab[$elem][0] = "<b>".get_string("users").":</b>";
439 $tab[$elem][1] = get_string($info->backup_users);
440 $elem++;
441 //Logs info
442 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
443 if ($info->backup_logs == "true") {
444 $tab[$elem][1] = get_string("yes");
445 } else {
446 $tab[$elem][1] = get_string("no");
448 $elem++;
449 //User Files info
450 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
451 if ($info->backup_user_files == "true") {
452 $tab[$elem][1] = get_string("yes");
453 } else {
454 $tab[$elem][1] = get_string("no");
456 $elem++;
457 //Course Files info
458 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
459 if ($info->backup_course_files == "true") {
460 $tab[$elem][1] = get_string("yes");
461 } else {
462 $tab[$elem][1] = get_string("no");
464 $elem++;
465 //site Files info
466 $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
467 if ($info->backup_site_files == "true") {
468 $tab[$elem][1] = get_string("yes");
469 } else {
470 $tab[$elem][1] = get_string("no");
472 $elem++;
473 //Messages info (only showed if present)
474 if ($info->backup_messages == 'true') {
475 $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
476 $tab[$elem][1] = get_string('yes');
477 $elem++;
478 } else {
479 //Do nothing
481 $table->data = $tab;
482 //Print title
483 print_heading(get_string("backupdetails").":");
484 //Print backup general info
485 print_table($table);
486 } else {
487 $status = false;
490 return $status;
493 //This function prints the contents from the course_header parammeter passed
494 function restore_print_course_header ($course_header) {
496 $status = true;
497 if ($course_header) {
498 $table = new object();
499 //This is tha align to every ingo table
500 $table->align = array ("right","left");
501 //The width
502 $table->width = "70%";
503 //Put interesting course header in table
504 //The course name
505 $tab[0][0] = "<b>".get_string("name").":</b>";
506 $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
507 //The course summary
508 $tab[1][0] = "<b>".get_string("summary").":</b>";
509 $tab[1][1] = $course_header->course_summary;
510 $table->data = $tab;
511 //Print title
512 print_heading(get_string("course").":");
513 //Print backup course header info
514 print_table($table);
515 } else {
516 $status = false;
518 return $status;
521 //This function create a new course record.
522 //When finished, course_header contains the id of the new course
523 function restore_create_new_course($restore,&$course_header) {
525 global $CFG;
527 $status = true;
529 $fullname = $course_header->course_fullname;
530 $shortname = $course_header->course_shortname;
531 $currentfullname = "";
532 $currentshortname = "";
533 $counter = 0;
534 //Iteratere while the name exists
535 do {
536 if ($counter) {
537 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
538 $suffixshort = "_".$counter;
539 } else {
540 $suffixfull = "";
541 $suffixshort = "";
543 $currentfullname = $fullname.$suffixfull;
544 // Limit the size of shortname - database column accepts <= 100 chars
545 $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
546 $coursefull = get_record("course","fullname",addslashes($currentfullname));
547 $courseshort = get_record("course","shortname",addslashes($currentshortname));
548 $counter++;
549 } while ($coursefull || $courseshort);
551 //New name = currentname
552 $course_header->course_fullname = $currentfullname;
553 $course_header->course_shortname = $currentshortname;
555 // first try to get it from restore
556 if ($restore->restore_restorecatto) {
557 $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
560 // else we try to get it from the xml file
561 //Now calculate the category
562 if (!$category) {
563 $category = get_record("course_categories","id",$course_header->category->id,
564 "name",addslashes($course_header->category->name));
567 //If no exists, try by name only
568 if (!$category) {
569 $category = get_record("course_categories","name",addslashes($course_header->category->name));
572 //If no exists, get category id 1
573 if (!$category) {
574 $category = get_record("course_categories","id","1");
577 //If category 1 doesn'exists, lets create the course category (get it from backup file)
578 if (!$category) {
579 $ins_category = new object();
580 $ins_category->name = addslashes($course_header->category->name);
581 $ins_category->parent = 0;
582 $ins_category->sortorder = 0;
583 $ins_category->coursecount = 0;
584 $ins_category->visible = 0; //To avoid interferences with the rest of the site
585 $ins_category->timemodified = time();
586 $newid = insert_record("course_categories",$ins_category);
587 $category->id = $newid;
588 $category->name = $course_header->category->name;
590 //If exists, put new category id
591 if ($category) {
592 $course_header->category->id = $category->id;
593 $course_header->category->name = $category->name;
594 //Error, cannot locate category
595 } else {
596 $course_header->category->id = 0;
597 $course_header->category->name = get_string("unknowncategory");
598 $status = false;
601 //Create the course_object
602 if ($status) {
603 $course = new object();
604 $course->category = addslashes($course_header->category->id);
605 $course->password = addslashes($course_header->course_password);
606 $course->fullname = addslashes($course_header->course_fullname);
607 $course->shortname = addslashes($course_header->course_shortname);
608 $course->idnumber = addslashes($course_header->course_idnumber);
609 $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
610 $course->summary = backup_todb($course_header->course_summary);
611 $course->format = addslashes($course_header->course_format);
612 $course->showgrades = addslashes($course_header->course_showgrades);
613 $course->newsitems = addslashes($course_header->course_newsitems);
614 $course->teacher = addslashes($course_header->course_teacher);
615 $course->teachers = addslashes($course_header->course_teachers);
616 $course->student = addslashes($course_header->course_student);
617 $course->students = addslashes($course_header->course_students);
618 $course->guest = addslashes($course_header->course_guest);
619 $course->startdate = addslashes($course_header->course_startdate);
620 $course->startdate += $restore->course_startdateoffset;
621 $course->numsections = addslashes($course_header->course_numsections);
622 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
623 $course->maxbytes = addslashes($course_header->course_maxbytes);
624 $course->showreports = addslashes($course_header->course_showreports);
625 if (isset($course_header->course_groupmode)) {
626 $course->groupmode = addslashes($course_header->course_groupmode);
628 if (isset($course_header->course_groupmodeforce)) {
629 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
631 if (isset($course_header->course_defaultgroupingid)) {
632 //keep the original now - convert after groupings restored
633 $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
635 $course->lang = addslashes($course_header->course_lang);
636 $course->theme = addslashes($course_header->course_theme);
637 $course->cost = addslashes($course_header->course_cost);
638 $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
639 $course->marker = addslashes($course_header->course_marker);
640 $course->visible = addslashes($course_header->course_visible);
641 $course->hiddensections = addslashes($course_header->course_hiddensections);
642 $course->timecreated = addslashes($course_header->course_timecreated);
643 $course->timemodified = addslashes($course_header->course_timemodified);
644 $course->metacourse = addslashes($course_header->course_metacourse);
645 $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
646 $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
647 $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
648 $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
649 $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
650 if ($course->enrolstartdate) { //Roll course dates
651 $course->enrolstartdate += $restore->course_startdateoffset;
653 $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
654 if ($course->enrolenddate) { //Roll course dates
655 $course->enrolenddate += $restore->course_startdateoffset;
657 $course->enrolperiod = addslashes($course_header->course_enrolperiod);
658 //Calculate sortorder field
659 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
660 FROM ' . $CFG->prefix . 'course
661 WHERE category=' . $course->category);
662 if (!empty($sortmax->max)) {
663 $course->sortorder = $sortmax->max + 1;
664 unset($sortmax);
665 } else {
666 $course->sortorder = 100;
669 //Now, recode some languages (Moodle 1.5)
670 if ($course->lang == 'ma_nt') {
671 $course->lang = 'mi_nt';
674 //Disable course->metacourse if avoided in restore config
675 if (!$restore->metacourse) {
676 $course->metacourse = 0;
679 //Check if the theme exists in destination server
680 $themes = get_list_of_themes();
681 if (!in_array($course->theme, $themes)) {
682 $course->theme = '';
685 //Now insert the record
686 $newid = insert_record("course",$course);
687 if ($newid) {
688 //save old and new course id
689 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
690 //Replace old course_id in course_header
691 $course_header->course_id = $newid;
692 } else {
693 $status = false;
697 return $status;
702 //This function creates all the block stuff when restoring courses
703 //It calls selectively to restore_create_block_instances() for 1.5
704 //and above backups. Upwards compatible with old blocks.
705 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
707 $status = true;
709 delete_records('block_instance', 'pageid', $restore->course_id, 'pagetype', PAGE_COURSE_VIEW);
710 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
711 if (empty($blockinfo)) {
712 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
713 $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
714 blocks_repopulate_page($newpage);
715 } else {
716 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
717 $blockrecords = get_records_select('block', '', '', 'name, id');
718 $temp_blocks_l = array();
719 $temp_blocks_r = array();
720 @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
721 $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
722 foreach($temp_blocks as $blockposition => $blocks) {
723 $blockweight = 0;
724 foreach($blocks as $blockname) {
725 if(!isset($blockrecords[$blockname])) {
726 // We don't know anything about this block!
727 continue;
729 $blockinstance = new stdClass;
730 // Remove any - prefix before doing the name-to-id mapping
731 if(substr($blockname, 0, 1) == '-') {
732 $blockname = substr($blockname, 1);
733 $blockinstance->visible = 0;
734 } else {
735 $blockinstance->visible = 1;
737 $blockinstance->blockid = $blockrecords[$blockname]->id;
738 $blockinstance->pageid = $restore->course_id;
739 $blockinstance->pagetype = PAGE_COURSE_VIEW;
740 $blockinstance->position = $blockposition;
741 $blockinstance->weight = $blockweight;
742 if(!$status = insert_record('block_instance', $blockinstance)) {
743 $status = false;
745 ++$blockweight;
749 } else if($backup_block_format == 'instances') {
750 $status = restore_create_block_instances($restore,$xml_file);
753 return $status;
757 //This function creates all the block_instances from xml when restoring in a
758 //new course
759 function restore_create_block_instances($restore,$xml_file) {
761 $status = true;
762 //Check it exists
763 if (!file_exists($xml_file)) {
764 $status = false;
766 //Get info from xml
767 if ($status) {
768 $info = restore_read_xml_blocks($xml_file);
771 if(empty($info->instances)) {
772 return $status;
775 // First of all, iterate over the blocks to see which distinct pages we have
776 // in our hands and arrange the blocks accordingly.
777 $pageinstances = array();
778 foreach($info->instances as $instance) {
780 //pagetype and pageid black magic, we have to handle the case of blocks for the
781 //course, blocks from other pages in that course etc etc etc.
783 if($instance->pagetype == PAGE_COURSE_VIEW) {
784 // This one's easy...
785 $instance->pageid = $restore->course_id;
787 else {
788 $parts = explode('-', $instance->pagetype);
789 if($parts[0] == 'mod') {
790 if(!$restore->mods[$parts[1]]->restore) {
791 continue;
793 $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
794 $instance->pageid = $getid->new_id;
796 else {
797 // Not invented here ;-)
798 continue;
802 if(!isset($pageinstances[$instance->pagetype])) {
803 $pageinstances[$instance->pagetype] = array();
805 if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
806 $pageinstances[$instance->pagetype][$instance->pageid] = array();
809 $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
812 $blocks = get_records_select('block', '', '', 'name, id, multiple');
814 // For each type of page we have restored
815 foreach($pageinstances as $thistypeinstances) {
817 // For each page id of that type
818 foreach($thistypeinstances as $thisidinstances) {
820 $addedblocks = array();
821 $maxweights = array();
823 // For each block instance in that page
824 foreach($thisidinstances as $instance) {
826 if(!isset($blocks[$instance->name])) {
827 //We are trying to restore a block we don't have...
828 continue;
831 //If we have already added this block once and multiples aren't allowed, disregard it
832 if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
833 continue;
836 //If its the first block we add to a new position, start weight counter equal to 0.
837 if(empty($maxweights[$instance->position])) {
838 $maxweights[$instance->position] = 0;
841 //If the instance weight is greater than the weight counter (we skipped some earlier
842 //blocks most probably), bring it back in line.
843 if($instance->weight > $maxweights[$instance->position]) {
844 $instance->weight = $maxweights[$instance->position];
847 //Add this instance
848 $instance->blockid = $blocks[$instance->name]->id;
850 if ($newid = insert_record('block_instance', $instance)) {
851 if (!empty($instance->id)) { // this will only be set if we come from 1.7 and above backups
852 backup_putid ($restore->backup_unique_code,"block_instance",$instance->id,$newid);
854 } else {
855 $status = false;
856 break;
859 //Get an object for the block and tell it it's been restored so it can update dates
860 //etc. if necessary
861 $blockobj=block_instance($instance->name,$instance);
862 $blockobj->after_restore($restore);
864 //Now we can increment the weight counter
865 ++$maxweights[$instance->position];
867 //Keep track of block types we have already added
868 $addedblocks[$instance->name] = true;
874 return $status;
877 //This function creates all the course_sections and course_modules from xml
878 //when restoring in a new course or simply checks sections and create records
879 //in backup_ids when restoring in a existing course
880 function restore_create_sections(&$restore, $xml_file) {
882 global $CFG,$db;
884 $status = true;
885 //Check it exists
886 if (!file_exists($xml_file)) {
887 $status = false;
889 //Get info from xml
890 if ($status) {
891 $info = restore_read_xml_sections($xml_file);
893 //Put the info in the DB, recoding ids and saving the in backup tables
895 $sequence = "";
897 if ($info) {
898 //For each, section, save it to db
899 foreach ($info->sections as $key => $sect) {
900 $sequence = "";
901 $section = new object();
902 $section->course = $restore->course_id;
903 $section->section = $sect->number;
904 $section->summary = backup_todb($sect->summary);
905 $section->visible = $sect->visible;
906 $section->sequence = "";
907 //Now calculate the section's newid
908 $newid = 0;
909 if ($restore->restoreto == 2) {
910 //Save it to db (only if restoring to new course)
911 $newid = insert_record("course_sections",$section);
912 } else {
913 //Get section id when restoring in existing course
914 $rec = get_record("course_sections","course",$restore->course_id,
915 "section",$section->section);
916 //If that section doesn't exist, get section 0 (every mod will be
917 //asigned there
918 if(!$rec) {
919 $rec = get_record("course_sections","course",$restore->course_id,
920 "section","0");
922 //New check. If section 0 doesn't exist, insert it here !!
923 //Teorically this never should happen but, in practice, some users
924 //have reported this issue.
925 if(!$rec) {
926 $zero_sec = new object();
927 $zero_sec->course = $restore->course_id;
928 $zero_sec->section = 0;
929 $zero_sec->summary = "";
930 $zero_sec->sequence = "";
931 $newid = insert_record("course_sections",$zero_sec);
932 $rec->id = $newid;
933 $rec->sequence = "";
935 $newid = $rec->id;
936 $sequence = $rec->sequence;
938 if ($newid) {
939 //save old and new section id
940 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
941 } else {
942 $status = false;
944 //If all is OK, go with associated mods
945 if ($status) {
946 //If we have mods in the section
947 if (!empty($sect->mods)) {
948 //For each mod inside section
949 foreach ($sect->mods as $keym => $mod) {
950 // Yu: This part is called repeatedly for every instance,
951 // so it is necessary to set the granular flag and check isset()
952 // when the first instance of this type of mod is processed.
954 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
956 if (!isset($restore->mods[$mod->type]->granular)) {
957 if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
958 // This defines whether we want to restore specific
959 // instances of the modules (granular restore), or
960 // whether we don't care and just want to restore
961 // all module instances (non-granular).
962 $restore->mods[$mod->type]->granular = true;
963 } else {
964 $restore->mods[$mod->type]->granular = false;
968 //Check if we've to restore this module (and instance)
969 if (!empty($restore->mods[$mod->type]->restore)) {
970 if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
971 || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
972 && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
974 //Get the module id from modules
975 $module = get_record("modules","name",$mod->type);
976 if ($module) {
977 $course_module = new object();
978 $course_module->course = $restore->course_id;
979 $course_module->module = $module->id;
980 $course_module->section = $newid;
981 $course_module->added = $mod->added;
982 $course_module->score = $mod->score;
983 $course_module->indent = $mod->indent;
984 $course_module->visible = $mod->visible;
985 $course_module->groupmode = $mod->groupmode;
986 if ($mod->groupingid and $grouping = backup_getid($restore->backup_unique_code,"groupings",$mod->groupingid)) {
987 $course_module->groupingid = $grouping->new_id;
988 } else {
989 $course_module->groupingid = 0;
991 $course_module->groupmembersonly = $mod->groupmembersonly;
992 $course_module->instance = 0;
993 //NOTE: The instance (new) is calculated and updated in db in the
994 // final step of the restore. We don't know it yet.
995 //print_object($course_module); //Debug
996 //Save it to db
998 $newidmod = insert_record("course_modules",$course_module);
999 if ($newidmod) {
1000 //save old and new module id
1001 //In the info field, we save the original instance of the module
1002 //to use it later
1003 backup_putid ($restore->backup_unique_code,"course_modules",
1004 $keym,$newidmod,$mod->instance);
1006 $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
1007 } else {
1008 $status = false;
1010 //Now, calculate the sequence field
1011 if ($status) {
1012 if ($sequence) {
1013 $sequence .= ",".$newidmod;
1014 } else {
1015 $sequence = $newidmod;
1018 } else {
1019 $status = false;
1026 //If all is OK, update sequence field in course_sections
1027 if ($status) {
1028 if (isset($sequence)) {
1029 $update_rec = new object();
1030 $update_rec->id = $newid;
1031 $update_rec->sequence = $sequence;
1032 $status = update_record("course_sections",$update_rec);
1036 } else {
1037 $status = false;
1039 return $status;
1042 //Called to set up any course-format specific data that may be in the file
1043 function restore_set_format_data($restore,$xml_file) {
1044 global $CFG,$db;
1046 $status = true;
1047 //Check it exists
1048 if (!file_exists($xml_file)) {
1049 return false;
1051 //Load data from XML to info
1052 if(!($info = restore_read_xml_formatdata($xml_file))) {
1053 return false;
1056 //Process format data if there is any
1057 if (isset($info->format_data)) {
1058 if(!$format=get_field('course','format','id',$restore->course_id)) {
1059 return false;
1061 // If there was any data then it must have a restore method
1062 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
1063 if(!file_exists($file)) {
1064 return false;
1066 require_once($file);
1067 $function=$format.'_restore_format_data';
1068 if(!function_exists($function)) {
1069 return false;
1071 return $function($restore,$info->format_data);
1074 // If we got here then there's no data, but that's cool
1075 return true;
1078 //This function creates all the metacourse data from xml, notifying
1079 //about each incidence
1080 function restore_create_metacourse($restore,$xml_file) {
1082 global $CFG,$db;
1084 $status = true;
1085 //Check it exists
1086 if (!file_exists($xml_file)) {
1087 $status = false;
1089 //Get info from xml
1090 if ($status) {
1091 //Load data from XML to info
1092 $info = restore_read_xml_metacourse($xml_file);
1095 //Process info about metacourse
1096 if ($status and $info) {
1097 //Process child records
1098 if (!empty($info->childs)) {
1099 foreach ($info->childs as $child) {
1100 $dbcourse = false;
1101 $dbmetacourse = false;
1102 //Check if child course exists in destination server
1103 //(by id in the same server or by idnumber and shortname in other server)
1104 if ($restore->original_wwwroot == $CFG->wwwroot) {
1105 //Same server, lets see by id
1106 $dbcourse = get_record('course','id',$child->id);
1107 } else {
1108 //Different server, lets see by idnumber and shortname, and only ONE record
1109 $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1110 if ($dbcount == 1) {
1111 $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1114 //If child course has been found, insert data
1115 if ($dbcourse) {
1116 $dbmetacourse->child_course = $dbcourse->id;
1117 $dbmetacourse->parent_course = $restore->course_id;
1118 $status = insert_record ('course_meta',$dbmetacourse);
1119 } else {
1120 //Child course not found, notice!
1121 if (!defined('RESTORE_SILENTLY')) {
1122 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
1126 //Now, recreate student enrolments...
1127 sync_metacourse($restore->course_id);
1129 //Process parent records
1130 if (!empty($info->parents)) {
1131 foreach ($info->parents as $parent) {
1132 $dbcourse = false;
1133 $dbmetacourse = false;
1134 //Check if parent course exists in destination server
1135 //(by id in the same server or by idnumber and shortname in other server)
1136 if ($restore->original_wwwroot == $CFG->wwwroot) {
1137 //Same server, lets see by id
1138 $dbcourse = get_record('course','id',$parent->id);
1139 } else {
1140 //Different server, lets see by idnumber and shortname, and only ONE record
1141 $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1142 if ($dbcount == 1) {
1143 $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1146 //If parent course has been found, insert data if it is a metacourse
1147 if ($dbcourse) {
1148 if ($dbcourse->metacourse) {
1149 $dbmetacourse->parent_course = $dbcourse->id;
1150 $dbmetacourse->child_course = $restore->course_id;
1151 $status = insert_record ('course_meta',$dbmetacourse);
1152 //Now, recreate student enrolments in parent course
1153 sync_metacourse($dbcourse->id);
1154 } else {
1155 //Parent course isn't metacourse, notice!
1156 if (!defined('RESTORE_SILENTLY')) {
1157 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1160 } else {
1161 //Parent course not found, notice!
1162 if (!defined('RESTORE_SILENTLY')) {
1163 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1170 return $status;
1173 //This function creates all the gradebook data from xml, notifying
1174 //about each incidence
1175 function restore_create_gradebook($restore,$xml_file) {
1177 global $CFG, $db;
1179 $status = true;
1180 //Check it exists
1181 if (!file_exists($xml_file)) {
1182 return false;
1185 // Get info from xml
1186 // info will contain the number of record to process
1187 $info = restore_read_xml_gradebook($restore, $xml_file);
1189 // If we have info, then process
1190 if ($info <= 0) {
1191 return $status;
1194 // Count how many we have
1195 $categoriescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories');
1196 $itemscount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items');
1197 $outcomecount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes');
1198 $outcomescoursescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_courses');
1199 $gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
1200 $gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
1201 $ggthcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_text_history');
1202 $gihcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
1203 $gohcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
1205 // we need to know if all grade items that were backed up are being restored
1206 // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
1207 // i.e. the aggregated grades of that category
1209 $restoreall = true; // set to false if any grade_item is not selected/restored
1211 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'", "old_id", "old_id, old_id")) {
1212 foreach ($recs as $rec) {
1214 if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
1216 $info = $data->info;
1217 // do not restore if this grade_item is a mod, and
1218 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1221 if ($itemtype == 'mod') {
1223 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1224 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1225 if (!restore_userdata_selected($restore, $itemmodule, $iteminstance)) {
1226 // module instance not selected when restored using granular
1227 // we are not restoring all grade items, set flag to false
1228 // so that we do not process grade categories and related grade items/grades
1229 $restoreall = false;
1230 break;
1237 // return if nothing to restore
1238 if (!$itemscount && !$categoriescount && !$outcomecount) {
1239 return $status;
1242 // Start ul
1243 if (!defined('RESTORE_SILENTLY')) {
1244 echo '<ul>';
1246 // Number of records to get in every chunk
1247 $recordset_size = 2;
1248 // Flag to mark if we must continue
1249 $continue = true;
1251 // Process categories
1252 if ($categoriescount && $continue && $restoreall) {
1253 if (!defined('RESTORE_SILENTLY')) {
1254 echo '<li>'.get_string('gradecategories','grades').'</li>';
1256 $counter = 0;
1257 while ($counter < $categoriescount) {
1258 // Fetch recordset_size records in each iteration
1259 $recs = get_records_select("backup_ids","table_name = 'grade_categories' AND backup_code = '$restore->backup_unique_code'",
1260 "old_id",
1261 "old_id, old_id",
1262 $counter,
1263 $recordset_size);
1264 if ($recs) {
1265 foreach ($recs as $rec) {
1266 // Get the full record from backup_ids
1267 $data = backup_getid($restore->backup_unique_code,'grade_categories',$rec->old_id);
1268 if ($data) {
1269 // Now get completed xmlized object
1270 $info = $data->info;
1271 //traverse_xmlize($info); //Debug
1272 //print_object ($GLOBALS['traverse_array']); //Debug
1273 //$GLOBALS['traverse_array']=""; //Debug
1274 //Now build the GRADE_PREFERENCES record structure
1276 $dbrec->courseid = $restore->course_id;
1278 // get the new grade category parent
1279 if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#'])) {
1280 $parent = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
1281 $dbrec->parent = $parent->new_id;
1284 $dbrec->fullname = backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
1285 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
1286 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
1287 $dbrec->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROPLOW']['0']['#']);
1288 $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
1290 //Structure is equal to db, insert record
1291 //if the fullname doesn't exist
1292 if (!$prerec = get_record('grade_categories','courseid',$dbrec->courseid,'fullname',$dbrec->fullname)) {
1293 $newid = insert_record('grade_categories',$dbrec);
1294 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$newid);
1295 // update this record so we can put in the right paths
1296 // this can only be done after we got the new id
1297 $dbrec->id = $newid;
1298 include_once($CFG->dirroot.'/lib/grade/grade_category.php');
1299 // rebuild the path, we need only parents info
1300 // the order of restoring should ensure that the parent and grandparent(s)
1301 // are already restored
1302 $dbrec->path = grade_category::build_path($dbrec);
1303 // this is not needed in the xml because
1304 // given this parent and grandparent(s) we can recalculate the depth
1305 $dbrec->depth = substr_count($dbrec->path, '/');
1306 update_record('grade_categories', $dbrec);
1307 } else {
1308 // if fullname already exists, we should keep the current grade category
1309 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$rec->oldid);
1312 //Increment counters
1313 $counter++;
1314 //Do some output
1315 if ($counter % 1 == 0) {
1316 if (!defined('RESTORE_SILENTLY')) {
1317 echo ".";
1318 if ($counter % 20 == 0) {
1319 echo "<br />";
1322 backup_flush(300);
1329 // process outcomes
1330 if ($outcomecount && $continue) {
1331 if (!defined('RESTORE_SILENTLY')) {
1332 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1334 $counter = 0;
1335 while ($counter < $outcomecount) {
1336 //Fetch recordset_size records in each iteration
1337 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1338 "old_id",
1339 "old_id, old_id",
1340 $counter,
1341 $recordset_size);
1342 if ($recs) {
1343 foreach ($recs as $rec) {
1344 //Get the full record from backup_ids
1345 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1346 if ($data) {
1347 //Now get completed xmlized object
1348 $info = $data->info;
1349 //traverse_xmlize($info); //Debug
1350 //print_object ($GLOBALS['traverse_array']); //Debug
1351 //$GLOBALS['traverse_array']=""; //Debug
1352 //Now build the GRADE_PREFERENCES record structure
1353 if ($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']) {
1354 $dbrec->courseid = $restore->course_id;
1355 } else {
1356 $dbrec->courseid = NULL;
1358 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1359 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#']);
1361 if ($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']) {
1362 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']));
1363 $dbrec->scaleid = $scale->new_id;
1366 $modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#']));
1367 $dbrec->usermodified = $modifier->new_id;
1369 // Structure is equal to db, insert record
1370 // If the shortname doesn't exist
1372 if (empty($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#'])) {
1373 $prerec = get_record_sql("SELECT * FROM {$CFG->prefix}grade_outcomes
1374 WHERE courseid IS NULL
1375 AND shortname = '$dbrec->shortname'");
1376 } else {
1377 $prerec = get_record('grade_outcomes','courseid',$restore->course_id,'shortname',$dbrec->shortname);
1380 if (!$prerec) {
1381 $newid = insert_record('grade_outcomes',$dbrec);
1382 } else {
1383 $newid = $prerec->id;
1386 if ($newid) {
1387 backup_putid($restore->backup_unique_code,"grade_outcomes", $rec->old_id, $newid);
1390 //Increment counters
1391 $counter++;
1392 //Do some output
1393 if ($counter % 1 == 0) {
1394 if (!defined('RESTORE_SILENTLY')) {
1395 echo ".";
1396 if ($counter % 20 == 0) {
1397 echo "<br />";
1400 backup_flush(300);
1407 // process outcomescourses
1408 if ($outcomescoursescount && $continue) {
1409 if (!defined('RESTORE_SILENTLY')) {
1410 echo '<li>'.get_string('gradeoutcomescourses','grades').'</li>';
1412 $counter = 0;
1413 while ($counter < $outcomescoursescount) {
1414 //Fetch recordset_size records in each iteration
1415 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_courses' AND backup_code = '$restore->backup_unique_code'",
1416 "old_id",
1417 "old_id, old_id",
1418 $counter,
1419 $recordset_size);
1420 if ($recs) {
1421 foreach ($recs as $rec) {
1422 //Get the full record from backup_ids
1423 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_courses',$rec->old_id);
1424 if ($data) {
1425 //Now get completed xmlized object
1426 $info = $data->info;
1427 //traverse_xmlize($info); //Debug
1428 //print_object ($GLOBALS['traverse_array']); //Debug
1429 //$GLOBALS['traverse_array']=""; //Debug
1431 $oldoutcomesid = backup_todb($info['GRADE_OUTCOMES_COURSE']['#']['OUTCOMEID']['0']['#']);
1432 $newoutcome = backup_getid($restore->backup_unique_code,"grade_outcomes",$oldoutcomesid);
1433 unset($dbrec);
1434 $dbrec->courseid = $restore->course_id;
1435 $dbrec->outcomeid = $newoutcome->new_id;
1436 insert_record('grade_outcomes_courses', $dbrec);
1438 //Increment counters
1439 $counter++;
1440 //Do some output
1441 if ($counter % 1 == 0) {
1442 if (!defined('RESTORE_SILENTLY')) {
1443 echo ".";
1444 if ($counter % 20 == 0) {
1445 echo "<br />";
1448 backup_flush(300);
1455 // Process grade items (grade_raw, grade_final, and grade_text)
1456 if ($itemscount && $continue) {
1457 if (!defined('RESTORE_SILENTLY')) {
1458 echo '<li>'.get_string('gradeitems','grades').'</li>';
1460 $counter = 0;
1461 $counteritems = 0;
1462 while ($counteritems < $itemscount) {
1464 //Fetch recordset_size records in each iteration
1465 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1466 "old_id",
1467 "old_id, old_id",
1468 $counteritems,
1469 $recordset_size);
1470 if ($recs) {
1471 foreach ($recs as $rec) {
1472 //Get the full record from backup_ids
1473 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
1474 if ($data) {
1476 //Now get completed xmlized object
1477 $info = $data->info;
1478 //traverse_xmlize($info); //Debug
1479 //print_object ($GLOBALS['traverse_array']); //Debug
1480 //$GLOBALS['traverse_array']=""; //Debug
1482 $dbrec->courseid = $restore->course_id;
1484 if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'])) {
1485 $category = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
1486 $dbrec->categoryid = $category->new_id;
1489 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
1490 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1491 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1492 /// this needs to point to either the new mod id
1493 /// or the category id
1494 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1496 // do not restore if this grade_item is a mod, and
1497 if ($dbrec->itemtype == 'mod') {
1499 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
1500 // module instance not selected when restored using granular
1501 // skip this item
1502 $counteritems++;
1503 continue;
1506 // iteminstance should point to new mod
1508 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
1509 $dbrec->iteminstance = $mod->new_id;
1511 } else if ($dbrec->itemtype == 'category') {
1512 // the item instance should point to the new grade category
1514 // only proceed if we are restoring all grade items
1515 if ($restoreall) {
1516 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
1517 $dbrec->iteminstance = $category->new_id;
1518 } else {
1519 // otherwise we can safely ignore this grade item and subsequent
1520 // grade_raws, grade_finals etc
1521 continue;
1523 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
1524 if ($restoreall) {
1525 // TODO any special code needed here to restore course item without duplicating it?
1526 // find the course category with depth 1, and course id = current course id
1527 // this would have been already restored
1529 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
1530 $dbrec->iteminstance = $cat->id;
1532 } else {
1533 continue;
1537 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
1538 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#']);
1539 $dbrec->idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#']);
1540 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#']);
1541 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#']);
1542 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#']);
1543 /// needs to be restored first
1545 if ($info['GRADE_ITEM']['#']['SCALEID']['0']['#']) {
1546 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#']));
1547 $dbrec->scaleid = $scale->new_id;
1550 /// needs to be restored first
1551 $dbrec->outcomeid = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']));
1553 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#']);
1554 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
1555 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
1556 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);
1557 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);
1558 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);
1561 // get the current sortorder, add 1 to it and use that
1562 if ($lastitem = get_record_sql("SELECT sortorder, id FROM {$CFG->prefix}grade_items
1563 WHERE courseid = $restore->course_id
1564 ORDER BY sortorder DESC ", true)) {
1566 // we just need the first one
1567 $dbrec->sortorder = $lastitem->sortorder + 1;
1568 } else {
1569 // this is the first grade_item
1570 $dbrec->sortorder = 1;
1572 // always insert, since modules restored to existing courses are always inserted
1573 $itemid = insert_record('grade_items',$dbrec);
1574 if ($itemid) {
1575 backup_putid($restore->backup_unique_code,'grade_items', backup_todb($info['GRADE_ITEM']['#']['ID']['0']['#']), $itemid);
1577 /// now, restore grade_grades, grade_text
1578 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1579 //Iterate over items
1580 for($i = 0; $i < sizeof($grades); $i++) {
1581 $ite_info = $grades[$i];
1582 //traverse_xmlize($ite_info);
1583 //Debug
1584 //print_object ($GLOBALS['traverse_array']); //Debug
1585 //$GLOBALS['traverse_array']=""; //Debug
1586 //Now build the GRADE_ITEM record structure
1587 $grade = new object();
1588 $grade->itemid = $itemid;
1589 $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));
1590 $grade->userid = $user->new_id;
1591 $grade->rawgrade = backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
1592 $grade->rawgrademax = backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
1593 $grade->rawgrademin = backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
1594 // need to find scaleid
1595 if ($ite_info['#']['RAWSCALEID']['0']['#']) {
1596 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
1597 $grade->rawscaleid = $scale->new_id;
1599 $grade->finalgrade = backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
1600 $grade->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
1601 $grade->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
1602 $grade->locktime = backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
1603 $grade->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
1604 $grade->overridden = backup_todb($ite_info['#']['OVERRIDDEN']['0']['#']);
1605 $grade->excluded = backup_todb($ite_info['#']['EXCLUDED']['0']['#']);
1607 $newid = insert_record('grade_grades', $grade);
1609 if ($newid) {
1610 backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($ite_info['#']['ID']['0']['#']), $newid);
1612 $counter++;
1613 if ($counter % 20 == 0) {
1614 if (!defined('RESTORE_SILENTLY')) {
1615 echo ".";
1616 if ($counter % 400 == 0) {
1617 echo "<br />";
1620 backup_flush(300);
1627 /// processing grade_grades_text
1628 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']) && ($texts = $info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']['GRADE_TEXT'])) {
1629 //Iterate over items
1630 for($i = 0; $i < sizeof($texts); $i++) {
1631 $ite_info = $texts[$i];
1632 //traverse_xmlize($ite_info); //Debug
1633 //print_object ($GLOBALS['traverse_array']); //Debug
1634 //$GLOBALS['traverse_array']=""; //Debug
1635 $grade = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($ite_info['#']['GRADEID']['0']['#']));
1637 $text = new object();
1638 $text->gradeid = $grade->new_id;
1639 $text->information = backup_todb($ite_info['#']['INFORMATION']['0']['#']);
1640 $text->informationformat = backup_todb($ite_info['#']['INFORMATIONFORMAT']['0']['#']);
1641 $text->feedback = backup_todb($ite_info['#']['FEEDBACK']['0']['#']);
1642 $text->feedbackformat = backup_todb($ite_info['#']['FEEDBACKFORMAT']['0']['#']);
1644 $newid = insert_record('grade_grades_text', $text);
1645 if ($newid) {
1646 backup_putid($restore->backup_unique_code,'grade_grades_text', backup_todb($ite_info['#']['ID']['0']['#']), $newid);
1648 $counter++;
1649 if ($counter % 20 == 0) {
1650 if (!defined('RESTORE_SILENTLY')) {
1651 echo ".";
1652 if ($counter % 400 == 0) {
1653 echo "<br />";
1656 backup_flush(300);
1661 $counteritems++; // increment item count
1668 // process histories
1669 if ($gchcount && $continue) {
1670 if (!defined('RESTORE_SILENTLY')) {
1671 echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
1673 $counter = 0;
1674 while ($counter < $gchcount) {
1675 //Fetch recordset_size records in each iteration
1676 $recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
1677 "old_id",
1678 "old_id, old_id",
1679 $counter,
1680 $recordset_size);
1681 if ($recs) {
1682 foreach ($recs as $rec) {
1683 //Get the full record from backup_ids
1684 $data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_id);
1685 if ($data) {
1686 //Now get completed xmlized object
1687 $info = $data->info;
1688 //traverse_xmlize($info); //Debug
1689 //print_object ($GLOBALS['traverse_array']); //Debug
1690 //$GLOBALS['traverse_array']=""; //Debug
1692 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
1693 if (empty($oldobj->new_id)) {
1694 // if the old object is not being restored, can't restoring its history
1695 $counter++;
1696 continue;
1698 $dbrec->oldid = $oldobj->new_id;
1699 $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
1700 $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
1701 $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1703 // loggeduser might not be restored, e.g. admin
1704 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1705 $dbrec->loggeduser = $oldobj->new_id;
1708 // this item might not have a parent at all, do not skip it if no parent is specified
1709 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
1710 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
1711 if (empty($oldobj->new_id)) {
1712 // if the parent category not restored
1713 $counter++;
1714 continue;
1717 $dbrec->parent = $oldobj->new_id;
1718 $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
1719 // path needs to be rebuilt
1720 if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
1721 // to preserve the path and make it work, we need to replace the categories one by one
1722 // we first get the list of categories in current path
1723 if ($paths = explode("/", $path)) {
1724 $newpath = '';
1725 foreach ($paths as $catid) {
1726 if ($catid) {
1727 // find the new corresponding path
1728 $oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
1729 $newpath .= "/$oldpath->new_id";
1732 $dbrec->path = $newpath;
1735 $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
1736 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
1737 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
1738 $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
1739 $dbrec->courseid = $restore->course_id;
1740 insert_record('grade_categories_history', $dbrec);
1741 unset($dbrec);
1744 //Increment counters
1745 $counter++;
1746 //Do some output
1747 if ($counter % 1 == 0) {
1748 if (!defined('RESTORE_SILENTLY')) {
1749 echo ".";
1750 if ($counter % 20 == 0) {
1751 echo "<br />";
1754 backup_flush(300);
1761 // process histories
1762 if ($gghcount && $continue) {
1763 if (!defined('RESTORE_SILENTLY')) {
1764 echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
1766 $counter = 0;
1767 while ($counter < $gghcount) {
1768 //Fetch recordset_size records in each iteration
1769 $recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
1770 "old_id",
1771 "old_id, old_id",
1772 $counter,
1773 $recordset_size);
1774 if ($recs) {
1775 foreach ($recs as $rec) {
1776 //Get the full record from backup_ids
1777 $data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_id);
1778 if ($data) {
1779 //Now get completed xmlized object
1780 $info = $data->info;
1781 //traverse_xmlize($info); //Debug
1782 //print_object ($GLOBALS['traverse_array']); //Debug
1783 //$GLOBALS['traverse_array']=""; //Debug
1785 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
1786 if (empty($oldobj->new_id)) {
1787 // if the old object is not being restored, can't restoring its history
1788 $counter++;
1789 continue;
1791 $dbrec->oldid = $oldobj->new_id;
1792 $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
1793 $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
1794 $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1795 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1796 $dbrec->loggeduser = $oldobj->new_id;
1798 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
1799 $dbrec->itemid = $oldobj->new_id;
1800 if (empty($dbrec->itemid)) {
1801 $counter++;
1802 continue; // grade item not being restored
1804 $oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
1805 $dbrec->userid = $oldobj->new_id;
1806 $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
1807 $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
1808 $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
1809 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
1810 $dbrec->usermodified = $oldobj->new_id;
1812 $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
1813 $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
1814 $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
1815 $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
1816 $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
1817 $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
1818 $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
1820 insert_record('grade_grades_history', $dbrec);
1821 unset($dbrec);
1824 //Increment counters
1825 $counter++;
1826 //Do some output
1827 if ($counter % 1 == 0) {
1828 if (!defined('RESTORE_SILENTLY')) {
1829 echo ".";
1830 if ($counter % 20 == 0) {
1831 echo "<br />";
1834 backup_flush(300);
1841 // process histories
1843 if ($ggthcount && $continue) {
1844 if (!defined('RESTORE_SILENTLY')) {
1845 echo '<li>'.get_string('gradegradestexthistory','grades').'</li>';
1847 $counter = 0;
1848 while ($counter < $ggthcount) {
1849 //Fetch recordset_size records in each iteration
1850 $recs = get_records_select("backup_ids","table_name = 'grade_grades_text_history' AND backup_code = '$restore->backup_unique_code'",
1851 "old_id",
1852 "old_id, old_id",
1853 $counter,
1854 $recordset_size);
1856 if ($recs) {
1857 foreach ($recs as $rec) {
1858 //Get the full record from backup_ids
1859 $data = backup_getid($restore->backup_unique_code,'grade_grades_text_history',$rec->old_id);
1860 if ($data) {
1861 //Now get completed xmlized object
1862 $info = $data->info;
1863 //traverse_xmlize($info); //Debug
1864 //print_object ($GLOBALS['traverse_array']); //Debug
1865 //$GLOBALS['traverse_array']=""; //Debug
1867 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades_text", backup_todb($info['GRADE_TEXT_HISTORY']['#']['OLDID']['0']['#']));
1868 if (empty($oldobj->new_id)) {
1869 // if the old object is not being restored, can't restoring its history
1870 $counter++;
1871 continue;
1873 $dbrec->oldid = $oldobj->new_id;
1874 $dbrec->action = backup_todb($info['GRADE_TEXT_HISTORY']['#']['ACTION']['0']['#']);
1875 $dbrec->source = backup_todb($info['GRADE_TEXT_HISTORY']['#']['SOURCE']['0']['#']);
1876 $dbrec->timemodified = backup_todb($info['GRADE_TEXT_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1877 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1878 $dbrec->loggeduser = $oldobj->new_id;
1880 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_TEXT_HISTORY']['#']['GRADEID']['0']['#']));
1881 $dbrec->gradeid = $oldobj->new_id;
1882 if (empty($dbrec->gradeid)) {
1883 $counter++;
1884 continue; // grade not being restore, possibly because grade item is not restored
1886 $oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['USERID']['0']['#']));
1887 $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
1888 $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
1889 $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
1890 $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
1891 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
1892 $dbrec->usermodified = $oldobj->new_id;
1895 insert_record('grade_grades_text_history', $dbrec);
1896 unset($dbrec);
1899 //Increment counters
1900 $counter++;
1901 //Do some output
1902 if ($counter % 1 == 0) {
1903 if (!defined('RESTORE_SILENTLY')) {
1904 echo ".";
1905 if ($counter % 20 == 0) {
1906 echo "<br />";
1909 backup_flush(300);
1916 // process histories
1917 if ($gihcount && $continue) {
1918 if (!defined('RESTORE_SILENTLY')) {
1919 echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
1921 $counter = 0;
1922 while ($counter < $gihcount) {
1923 //Fetch recordset_size records in each iteration
1924 $recs = get_records_select("backup_ids","table_name = 'grade_items_history' AND backup_code = '$restore->backup_unique_code'",
1925 "old_id",
1926 "old_id, old_id",
1927 $counter,
1928 $recordset_size);
1929 if ($recs) {
1930 foreach ($recs as $rec) {
1931 //Get the full record from backup_ids
1932 $data = backup_getid($restore->backup_unique_code,'grade_items_history',$rec->old_id);
1933 if ($data) {
1934 //Now get completed xmlized object
1935 $info = $data->info;
1936 //traverse_xmlize($info); //Debug
1937 //print_object ($GLOBALS['traverse_array']); //Debug
1938 //$GLOBALS['traverse_array']=""; //Debug
1941 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
1942 if (empty($oldobj->new_id)) {
1943 // if the old object is not being restored, can't restoring its history
1944 $counter++;
1945 continue;
1947 $dbrec->oldid = $oldobj->new_id;
1948 $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
1949 $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
1950 $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1951 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1952 $dbrec->loggeduser = $oldobj->new_id;
1954 $oldobj = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
1955 $oldobj->categoryid = $category->new_id;
1956 if (empty($oldobj->categoryid)) {
1957 $counter++;
1958 continue; // category not restored
1961 $dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
1962 $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
1963 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
1965 // code from grade_items restore
1966 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
1967 // do not restore if this grade_item is a mod, and
1968 if ($dbrec->itemtype == 'mod') {
1970 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
1971 // module instance not selected when restored using granular
1972 // skip this item
1973 $counter++;
1974 continue;
1977 // iteminstance should point to new mod
1979 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
1980 $dbrec->iteminstance = $mod->new_id;
1982 } else if ($dbrec->itemtype == 'category') {
1983 // the item instance should point to the new grade category
1985 // only proceed if we are restoring all grade items
1986 if ($restoreall) {
1987 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
1988 $dbrec->iteminstance = $category->new_id;
1989 } else {
1990 // otherwise we can safely ignore this grade item and subsequent
1991 // grade_raws, grade_finals etc
1992 continue;
1994 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
1995 if ($restoreall) {
1996 // TODO any special code needed here to restore course item without duplicating it?
1997 // find the course category with depth 1, and course id = current course id
1998 // this would have been already restored
2000 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
2001 $dbrec->iteminstance = $cat->id;
2003 } else {
2004 $counter++;
2005 continue;
2009 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
2010 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
2011 $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
2012 $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
2013 $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
2014 $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
2015 $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
2016 if ($oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
2017 // scaleid is optional
2018 $dbrec->scaleid = $oldobj->new_id;
2020 if ($oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
2021 // outcome is optional
2022 $dbrec->outcomeid = $oldobj->new_id;
2024 $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
2025 $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
2026 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
2027 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
2028 $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
2029 $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
2030 $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
2031 $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
2032 $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
2034 insert_record('grade_items_history', $dbrec);
2035 unset($dbrec);
2038 //Increment counters
2039 $counter++;
2040 //Do some output
2041 if ($counter % 1 == 0) {
2042 if (!defined('RESTORE_SILENTLY')) {
2043 echo ".";
2044 if ($counter % 20 == 0) {
2045 echo "<br />";
2048 backup_flush(300);
2055 // process histories
2056 if ($gohcount && $continue) {
2057 if (!defined('RESTORE_SILENTLY')) {
2058 echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
2060 $counter = 0;
2061 while ($counter < $gohcount) {
2062 //Fetch recordset_size records in each iteration
2063 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_history' AND backup_code = '$restore->backup_unique_code'",
2064 "old_id",
2065 "old_id, old_id",
2066 $counter,
2067 $recordset_size);
2068 if ($recs) {
2069 foreach ($recs as $rec) {
2070 //Get the full record from backup_ids
2071 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_history',$rec->old_id);
2072 if ($data) {
2073 //Now get completed xmlized object
2074 $info = $data->info;
2075 //traverse_xmlize($info); //Debug
2076 //print_object ($GLOBALS['traverse_array']); //Debug
2077 //$GLOBALS['traverse_array']=""; //Debug
2079 $oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
2080 if (empty($oldobj->new_id)) {
2081 // if the old object is not being restored, can't restoring its history
2082 $counter++;
2083 continue;
2085 $dbrec->oldid = $oldobj->new_id;
2086 $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
2087 $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
2088 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2089 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2090 $dbrec->loggeduser = $oldobj->new_id;
2092 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
2093 $dbrec->fullname= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
2094 $oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
2095 $dbrec->scaleid = $oldobj->new_id;
2096 $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
2098 insert_record('grade_outcomes_history', $dbrec);
2099 unset($dbrec);
2102 //Increment counters
2103 $counter++;
2104 //Do some output
2105 if ($counter % 1 == 0) {
2106 if (!defined('RESTORE_SILENTLY')) {
2107 echo ".";
2108 if ($counter % 20 == 0) {
2109 echo "<br />";
2112 backup_flush(300);
2119 if (!defined('RESTORE_SILENTLY')) {
2120 //End ul
2121 echo '</ul>';
2123 return $status;
2126 //This function creates all the user, user_students, user_teachers
2127 //user_course_creators and user_admins from xml
2128 function restore_create_users($restore,$xml_file) {
2130 global $CFG, $db;
2132 $status = true;
2133 //Check it exists
2134 if (!file_exists($xml_file)) {
2135 $status = false;
2137 //Get info from xml
2138 if ($status) {
2139 //info will contain the old_id of every user
2140 //in backup_ids->info will be the real info (serialized)
2141 $info = restore_read_xml_users($restore,$xml_file);
2144 //Now, get evey user_id from $info and user data from $backup_ids
2145 //and create the necessary records (users, user_students, user_teachers
2146 //user_course_creators and user_admins
2147 if (!empty($info->users)) {
2148 // Grab mnethosts keyed by wwwroot, to map to id
2149 $mnethosts = get_records('mnet_host', '', '',
2150 'wwwroot', 'wwwroot, id');
2152 $languages = get_list_of_languages();
2154 foreach ($info->users as $userid) {
2155 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
2156 $user = $rec->info;
2158 //Now, recode some languages (Moodle 1.5)
2159 if ($user->lang == 'ma_nt') {
2160 $user->lang = 'mi_nt';
2164 //If language does not exist here - use site default
2165 if (!array_key_exists($user->lang, $languages)) {
2166 $user->lang = $CFG->lang;
2169 //Check if it's admin and coursecreator
2170 $is_admin = !empty($user->roles['admin']);
2171 $is_coursecreator = !empty($user->roles['coursecreator']);
2173 //Check if it's teacher and student
2174 $is_teacher = !empty($user->roles['teacher']);
2175 $is_student = !empty($user->roles['student']);
2177 //Check if it's needed
2178 $is_needed = !empty($user->roles['needed']);
2180 //Calculate if it is a course user
2181 //Has role teacher or student or needed
2182 $is_course_user = ($is_teacher or $is_student or $is_needed);
2184 //Calculate mnethostid
2185 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
2186 $user->mnethostid = $CFG->mnet_localhost_id;
2187 } else {
2188 // fast url-to-id lookups
2189 if (isset($mnethosts[$user->mnethosturl])) {
2190 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
2191 } else {
2192 // should not happen, as we check in restore_chech.php
2193 // but handle the error if it does
2194 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
2197 unset($user->mnethosturl);
2199 //To store new ids created
2200 $newid=null;
2201 //check if it exists (by username) and get its id
2202 $user_exists = true;
2203 $user_data = get_record("user","username",addslashes($user->username),
2204 'mnethostid', $user->mnethostid);
2205 if (!$user_data) {
2206 $user_exists = false;
2207 } else {
2208 $newid = $user_data->id;
2210 //Flags to see if we have to create the user, roles and preferences
2211 $create_user = true;
2212 $create_roles = true;
2213 $create_preferences = true;
2215 //If we are restoring course users and it isn't a course user
2216 if ($restore->users == 1 and !$is_course_user) {
2217 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
2218 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
2219 $create_user = false;
2220 $create_roles = false;
2221 $create_preferences = false;
2224 if ($user_exists and $create_user) {
2225 //If user exists mark its newid in backup_ids (the same than old)
2226 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
2227 $create_user = false;
2230 //Here, if create_user, do it
2231 if ($create_user) {
2232 //Unset the id because it's going to be inserted with a new one
2233 unset ($user->id);
2234 //We addslashes to necessary fields
2235 $user->username = addslashes($user->username);
2236 $user->firstname = addslashes($user->firstname);
2237 $user->lastname = addslashes($user->lastname);
2238 $user->email = addslashes($user->email);
2239 $user->institution = addslashes($user->institution);
2240 $user->department = addslashes($user->department);
2241 $user->address = addslashes($user->address);
2242 $user->city = addslashes($user->city);
2243 $user->url = addslashes($user->url);
2244 $user->description = backup_todb($user->description);
2246 //We need to analyse the AUTH field to recode it:
2247 // - if the field isn't set, we are in a pre 1.4 backup and we'll
2248 // use manual
2250 if (empty($user->auth)) {
2251 if ($CFG->registerauth == 'email') {
2252 $user->auth = 'email';
2253 } else {
2254 $user->auth = 'manual';
2258 //We need to process the POLICYAGREED field to recalculate it:
2259 // - if the destination site is different (by wwwroot) reset it.
2260 // - if the destination site is the same (by wwwroot), leave it unmodified
2262 if ($restore->original_wwwroot != $CFG->wwwroot) {
2263 $user->policyagreed = 0;
2264 } else {
2265 //Nothing to do, we are in the same server
2268 //Check if the theme exists in destination server
2269 $themes = get_list_of_themes();
2270 if (!in_array($user->theme, $themes)) {
2271 $user->theme = '';
2274 //We are going to create the user
2275 //The structure is exactly as we need
2276 $newid = insert_record ("user",$user);
2277 //Put the new id
2278 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
2281 //Here, if create_roles, do it as necessary
2282 if ($create_roles) {
2283 //Get the newid and current info from backup_ids
2284 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2285 $newid = $data->new_id;
2286 $currinfo = $data->info.",";
2288 //Now, depending of the role, create records in user_studentes and user_teacher
2289 //and/or mark it in backup_ids
2291 if ($is_admin) {
2292 //If the record (user_admins) doesn't exists
2293 //Only put status in backup_ids
2294 $currinfo = $currinfo."admin,";
2295 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2297 if ($is_coursecreator) {
2298 //If the record (user_coursecreators) doesn't exists
2299 //Only put status in backup_ids
2300 $currinfo = $currinfo."coursecreator,";
2301 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2303 if ($is_needed) {
2304 //Only put status in backup_ids
2305 $currinfo = $currinfo."needed,";
2306 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2308 if ($is_teacher) {
2309 //If the record (teacher) doesn't exists
2310 //Put status in backup_ids
2311 $currinfo = $currinfo."teacher,";
2312 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2313 //Set course and user
2314 $user->roles['teacher']->course = $restore->course_id;
2315 $user->roles['teacher']->userid = $newid;
2317 //Need to analyse the enrol field
2318 // - if it isn't set, set it to $CFG->enrol
2319 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2320 // - if we are in the same server (by wwwroot), maintain it unmodified.
2321 if (empty($user->roles['teacher']->enrol)) {
2322 $user->roles['teacher']->enrol = $CFG->enrol;
2323 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2324 $user->roles['teacher']->enrol = $CFG->enrol;
2325 } else {
2326 //Nothing to do. Leave it unmodified
2329 $rolesmapping = $restore->rolesmapping;
2330 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2331 if ($user->roles['teacher']->editall) {
2332 role_assign($rolesmapping['defaultteacheredit'],
2333 $newid,
2335 $context->id,
2336 $user->roles['teacher']->timestart,
2337 $user->roles['teacher']->timeend,
2339 $user->roles['teacher']->enrol);
2341 // editting teacher
2342 } else {
2343 // non editting teacher
2344 role_assign($rolesmapping['defaultteacher'],
2345 $newid,
2347 $context->id,
2348 $user->roles['teacher']->timestart,
2349 $user->roles['teacher']->timeend,
2351 $user->roles['teacher']->enrol);
2354 if ($is_student) {
2356 //Put status in backup_ids
2357 $currinfo = $currinfo."student,";
2358 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2359 //Set course and user
2360 $user->roles['student']->course = $restore->course_id;
2361 $user->roles['student']->userid = $newid;
2363 //Need to analyse the enrol field
2364 // - if it isn't set, set it to $CFG->enrol
2365 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2366 // - if we are in the same server (by wwwroot), maintain it unmodified.
2367 if (empty($user->roles['student']->enrol)) {
2368 $user->roles['student']->enrol = $CFG->enrol;
2369 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2370 $user->roles['student']->enrol = $CFG->enrol;
2371 } else {
2372 //Nothing to do. Leave it unmodified
2374 $rolesmapping = $restore->rolesmapping;
2375 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2377 role_assign($rolesmapping['defaultstudent'],
2378 $newid,
2380 $context->id,
2381 $user->roles['student']->timestart,
2382 $user->roles['student']->timeend,
2384 $user->roles['student']->enrol);
2387 if (!$is_course_user) {
2388 //If the record (user) doesn't exists
2389 if (!record_exists("user","id",$newid)) {
2390 //Put status in backup_ids
2391 $currinfo = $currinfo."user,";
2392 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2397 //Here, if create_preferences, do it as necessary
2398 if ($create_preferences) {
2399 //echo "Checking for preferences of user ".$user->username."<br />"; //Debug
2400 //Get user new id from backup_ids
2401 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2402 $newid = $data->new_id;
2403 if (isset($user->user_preferences)) {
2404 //echo "Preferences exist in backup file<br />"; //Debug
2405 foreach($user->user_preferences as $user_preference) {
2406 //echo $user_preference->name." = ".$user_preference->value."<br />"; //Debug
2407 //We check if that user_preference exists in DB
2408 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
2409 //echo "Creating it<br />"; //Debug
2410 //Prepare the record and insert it
2411 $user_preference->userid = $newid;
2412 $status = insert_record("user_preferences",$user_preference);
2420 return $status;
2423 //This function creates all the structures messages and contacts
2424 function restore_create_messages($restore,$xml_file) {
2426 global $CFG;
2428 $status = true;
2429 //Check it exists
2430 if (!file_exists($xml_file)) {
2431 $status = false;
2433 //Get info from xml
2434 if ($status) {
2435 //info will contain the id and name of every table
2436 //(message, message_read and message_contacts)
2437 //in backup_ids->info will be the real info (serialized)
2438 $info = restore_read_xml_messages($restore,$xml_file);
2440 //If we have info, then process messages & contacts
2441 if ($info > 0) {
2442 //Count how many we have
2443 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
2444 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
2445 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
2446 if ($unreadcount || $readcount || $contactcount) {
2447 //Start ul
2448 if (!defined('RESTORE_SILENTLY')) {
2449 echo '<ul>';
2451 //Number of records to get in every chunk
2452 $recordset_size = 4;
2454 //Process unread
2455 if ($unreadcount) {
2456 if (!defined('RESTORE_SILENTLY')) {
2457 echo '<li>'.get_string('unreadmessages','message').'</li>';
2459 $counter = 0;
2460 while ($counter < $unreadcount) {
2461 //Fetch recordset_size records in each iteration
2462 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2463 if ($recs) {
2464 foreach ($recs as $rec) {
2465 //Get the full record from backup_ids
2466 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
2467 if ($data) {
2468 //Now get completed xmlized object
2469 $info = $data->info;
2470 //traverse_xmlize($info); //Debug
2471 //print_object ($GLOBALS['traverse_array']); //Debug
2472 //$GLOBALS['traverse_array']=""; //Debug
2473 //Now build the MESSAGE record structure
2474 $dbrec = new object();
2475 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2476 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2477 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2478 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2479 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2480 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2481 //We have to recode the useridfrom field
2482 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2483 if ($user) {
2484 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2485 $dbrec->useridfrom = $user->new_id;
2487 //We have to recode the useridto field
2488 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2489 if ($user) {
2490 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2491 $dbrec->useridto = $user->new_id;
2493 //Check if the record doesn't exist in DB!
2494 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
2495 'useridto', $dbrec->useridto,
2496 'timecreated',$dbrec->timecreated);
2497 if (!$exist) {
2498 //Not exist. Insert
2499 $status = insert_record('message',$dbrec);
2500 } else {
2501 //Duplicate. Do nothing
2504 //Do some output
2505 $counter++;
2506 if ($counter % 10 == 0) {
2507 if (!defined('RESTORE_SILENTLY')) {
2508 echo ".";
2509 if ($counter % 200 == 0) {
2510 echo "<br />";
2513 backup_flush(300);
2520 //Process read
2521 if ($readcount) {
2522 if (!defined('RESTORE_SILENTLY')) {
2523 echo '<li>'.get_string('readmessages','message').'</li>';
2525 $counter = 0;
2526 while ($counter < $readcount) {
2527 //Fetch recordset_size records in each iteration
2528 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2529 if ($recs) {
2530 foreach ($recs as $rec) {
2531 //Get the full record from backup_ids
2532 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
2533 if ($data) {
2534 //Now get completed xmlized object
2535 $info = $data->info;
2536 //traverse_xmlize($info); //Debug
2537 //print_object ($GLOBALS['traverse_array']); //Debug
2538 //$GLOBALS['traverse_array']=""; //Debug
2539 //Now build the MESSAGE_READ record structure
2540 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2541 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2542 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2543 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2544 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2545 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2546 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2547 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2548 //We have to recode the useridfrom field
2549 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2550 if ($user) {
2551 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2552 $dbrec->useridfrom = $user->new_id;
2554 //We have to recode the useridto field
2555 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2556 if ($user) {
2557 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2558 $dbrec->useridto = $user->new_id;
2560 //Check if the record doesn't exist in DB!
2561 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
2562 'useridto', $dbrec->useridto,
2563 'timecreated',$dbrec->timecreated);
2564 if (!$exist) {
2565 //Not exist. Insert
2566 $status = insert_record('message_read',$dbrec);
2567 } else {
2568 //Duplicate. Do nothing
2571 //Do some output
2572 $counter++;
2573 if ($counter % 10 == 0) {
2574 if (!defined('RESTORE_SILENTLY')) {
2575 echo ".";
2576 if ($counter % 200 == 0) {
2577 echo "<br />";
2580 backup_flush(300);
2587 //Process contacts
2588 if ($contactcount) {
2589 if (!defined('RESTORE_SILENTLY')) {
2590 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2592 $counter = 0;
2593 while ($counter < $contactcount) {
2594 //Fetch recordset_size records in each iteration
2595 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2596 if ($recs) {
2597 foreach ($recs as $rec) {
2598 //Get the full record from backup_ids
2599 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
2600 if ($data) {
2601 //Now get completed xmlized object
2602 $info = $data->info;
2603 //traverse_xmlize($info); //Debug
2604 //print_object ($GLOBALS['traverse_array']); //Debug
2605 //$GLOBALS['traverse_array']=""; //Debug
2606 //Now build the MESSAGE_CONTACTS record structure
2607 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2608 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2609 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2610 //We have to recode the userid field
2611 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
2612 if ($user) {
2613 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2614 $dbrec->userid = $user->new_id;
2616 //We have to recode the contactid field
2617 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
2618 if ($user) {
2619 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2620 $dbrec->contactid = $user->new_id;
2622 //Check if the record doesn't exist in DB!
2623 $exist = get_record('message_contacts','userid',$dbrec->userid,
2624 'contactid', $dbrec->contactid);
2625 if (!$exist) {
2626 //Not exist. Insert
2627 $status = insert_record('message_contacts',$dbrec);
2628 } else {
2629 //Duplicate. Do nothing
2632 //Do some output
2633 $counter++;
2634 if ($counter % 10 == 0) {
2635 if (!defined('RESTORE_SILENTLY')) {
2636 echo ".";
2637 if ($counter % 200 == 0) {
2638 echo "<br />";
2641 backup_flush(300);
2647 if (!defined('RESTORE_SILENTLY')) {
2648 //End ul
2649 echo '</ul>';
2655 return $status;
2658 //This function creates all the categories and questions
2659 //from xml
2660 function restore_create_questions($restore,$xml_file) {
2662 global $CFG, $db;
2664 $status = true;
2665 //Check it exists
2666 if (!file_exists($xml_file)) {
2667 $status = false;
2669 //Get info from xml
2670 if ($status) {
2671 //info will contain the old_id of every category
2672 //in backup_ids->info will be the real info (serialized)
2673 $info = restore_read_xml_questions($restore,$xml_file);
2675 //Now, if we have anything in info, we have to restore that
2676 //categories/questions
2677 if ($info) {
2678 if ($info !== true) {
2679 $status = $status && restore_question_categories($info, $restore);
2681 } else {
2682 $status = false;
2684 return $status;
2687 //This function creates all the scales
2688 function restore_create_scales($restore,$xml_file) {
2690 global $CFG, $db;
2692 $status = true;
2693 //Check it exists
2694 if (!file_exists($xml_file)) {
2695 $status = false;
2697 //Get info from xml
2698 if ($status) {
2699 //scales will contain the old_id of every scale
2700 //in backup_ids->info will be the real info (serialized)
2701 $scales = restore_read_xml_scales($restore,$xml_file);
2703 //Now, if we have anything in scales, we have to restore that
2704 //scales
2705 if ($scales) {
2706 //Get admin->id for later use
2707 $admin = get_admin();
2708 $adminid = $admin->id;
2709 if ($scales !== true) {
2710 //Iterate over each scale
2711 foreach ($scales as $scale) {
2712 //Get record from backup_ids
2713 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
2714 //Init variables
2715 $create_scale = false;
2717 if ($data) {
2718 //Now get completed xmlized object
2719 $info = $data->info;
2720 //traverse_xmlize($info); //Debug
2721 //print_object ($GLOBALS['traverse_array']); //Debug
2722 //$GLOBALS['traverse_array']=""; //Debug
2724 //Now build the SCALE record structure
2725 $sca = new object();
2726 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
2727 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
2728 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
2729 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
2730 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
2731 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
2733 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
2734 //or in restore->course_id course (Personal scale)
2735 if ($sca->courseid == 0) {
2736 $course_to_search = 0;
2737 } else {
2738 $course_to_search = $restore->course_id;
2741 // scale is not course unique, use get_record_sql to suppress warning
2743 $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale
2744 WHERE scale = '$sca->scale'
2745 AND courseid = $course_to_search", true);
2747 //If it doesn't exist, create
2748 if (!$sca_db) {
2749 $create_scale = true;
2751 //If we must create the scale
2752 if ($create_scale) {
2753 //Me must recode the courseid if it's <> 0 (common scale)
2754 if ($sca->courseid != 0) {
2755 $sca->courseid = $restore->course_id;
2757 //We must recode the userid
2758 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
2759 if ($user) {
2760 $sca->userid = $user->new_id;
2761 } else {
2762 //Assign it to admin
2763 $sca->userid = $adminid;
2765 //The structure is equal to the db, so insert the scale
2766 $newid = insert_record ("scale",$sca);
2767 } else {
2768 //get current scale id
2769 $newid = $sca_db->id;
2771 if ($newid) {
2772 //We have the newid, update backup_ids
2773 backup_putid($restore->backup_unique_code,"scale",
2774 $scale->id, $newid);
2779 } else {
2780 $status = false;
2782 return $status;
2785 //This function creates all the groups
2786 function restore_create_groups($restore,$xml_file) {
2788 //Check it exists
2789 if (!file_exists($xml_file)) {
2790 return false;
2792 //Get info from xml
2793 if (!$groups = restore_read_xml_groups($restore,$xml_file)) {
2794 //groups will contain the old_id of every group
2795 //in backup_ids->info will be the real info (serialized)
2796 return false;
2798 } else if ($groups === true) {
2799 return true;
2802 $status = true;
2804 //Iterate over each group
2805 foreach ($groups as $group) {
2806 //Get record from backup_ids
2807 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
2809 if ($data) {
2810 //Now get completed xmlized object
2811 $info = $data->info;
2812 //traverse_xmlize($info); //Debug
2813 //print_object ($GLOBALS['traverse_array']); //Debug
2814 //$GLOBALS['traverse_array']=""; //Debug
2815 //Now build the GROUP record structure
2816 $gro = new Object();
2817 $gro->courseid = $restore->course_id;
2818 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
2819 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
2820 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
2821 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
2822 } else {
2823 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
2825 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
2826 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
2827 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
2828 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
2830 //Now search if that group exists (by name and description field) in
2831 if (!$gro_db = get_record('groups', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
2832 //If it doesn't exist, create
2833 $newid = insert_record('groups', $gro);
2835 } else {
2836 //get current group id
2837 $newid = $gro_db->id;
2840 if ($newid) {
2841 //We have the newid, update backup_ids
2842 backup_putid($restore->backup_unique_code,"groups", $group->id, $newid);
2843 } else {
2845 $status = false;
2846 continue;
2849 //Now restore members in the groups_members, only if
2850 //users are included
2851 if ($restore->users != 2) {
2852 if (!restore_create_groups_members($newid,$info,$restore)) {
2853 $status = false;
2859 //Now, restore group_files
2860 if ($status) {
2861 $status = restore_group_files($restore);
2864 return $status;
2867 //This function restores the groups_members
2868 function restore_create_groups_members($group_id,$info,$restore) {
2870 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
2871 //OK, some groups have no members.
2872 return true;
2874 //Get the members array
2875 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
2877 $status = true;
2879 //Iterate over members
2880 for($i = 0; $i < sizeof($members); $i++) {
2881 $mem_info = $members[$i];
2882 //traverse_xmlize($mem_info); //Debug
2883 //print_object ($GLOBALS['traverse_array']); //Debug
2884 //$GLOBALS['traverse_array']=""; //Debug
2886 //Now, build the GROUPS_MEMBERS record structure
2887 $group_member = new Object();
2888 $group_member->groupid = $group_id;
2889 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
2890 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2892 $newid = false;
2894 //We have to recode the userid field
2895 if (!$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid)) {
2896 $status = false;
2897 continue;
2900 $group_member->userid = $user->new_id;
2902 //The structure is equal to the db, so insert the groups_members
2903 if (!insert_record ("groups_members", $group_member)) {
2904 $status = false;
2905 continue;
2908 //Do some output
2909 if (($i+1) % 50 == 0) {
2910 if (!defined('RESTORE_SILENTLY')) {
2911 echo ".";
2912 if (($i+1) % 1000 == 0) {
2913 echo "<br />";
2916 backup_flush(300);
2920 return $status;
2923 //This function creates all the groupings
2924 function restore_create_groupings($restore,$xml_file) {
2926 //Check it exists
2927 if (!file_exists($xml_file)) {
2928 return false;
2930 //Get info from xml
2931 if (!$groupings = restore_read_xml_groupings($restore,$xml_file)) {
2932 return false;
2934 } else if ($groupings === true) {
2935 return true;
2938 $status = true;
2940 //Iterate over each group
2941 foreach ($groupings as $grouping) {
2942 if ($data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id)) {
2943 //Now get completed xmlized object
2944 $info = $data->info;
2945 //Now build the GROUPING record structure
2946 $gro = new Object();
2947 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
2948 $gro->courseid = $restore->course_id;
2949 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
2950 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
2951 $gro->configdata = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
2952 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
2954 //Now search if that group exists (by name and description field) in
2955 if ($gro_db = get_record('groupings', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
2956 //get current group id
2957 $newid = $gro_db->id;
2959 } else {
2960 //The structure is equal to the db, so insert the grouping
2961 if (!$newid = insert_record('groupings', $gro)) {
2962 $status = false;
2963 continue;
2967 //We have the newid, update backup_ids
2968 backup_putid($restore->backup_unique_code,"groupings",
2969 $grouping->id, $newid);
2974 // now fix the defaultgroupingid in course
2975 $course = get_record('course', 'id', $restore->course_id);
2976 if ($course->defaultgroupingid) {
2977 if ($grouping = backup_getid($restore->backup_unique_code,"groupings",$course->defaultgroupingid)) {
2978 set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
2979 } else {
2980 set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
2984 return $status;
2987 //This function creates all the groupingsgroups
2988 function restore_create_groupings_groups($restore,$xml_file) {
2990 //Check it exists
2991 if (!file_exists($xml_file)) {
2992 return false;
2994 //Get info from xml
2995 if (!$groupingsgroups = restore_read_xml_groupings_groups($restore,$xml_file)) {
2996 return false;
2998 } else if ($groupingsgroups === true) {
2999 return true;
3002 $status = true;
3004 //Iterate over each group
3005 foreach ($groupingsgroups as $groupinggroup) {
3006 if ($data = backup_getid($restore->backup_unique_code,"groupingsgroups",$groupinggroup->id)) {
3007 //Now get completed xmlized object
3008 $info = $data->info;
3009 //Now build the GROUPING record structure
3010 $gro_member = new Object();
3011 $gro_member->groupingid = backup_todb($info['GROUPINGGROUP']['#']['GROUPINGID']['0']['#']);
3012 $gro_member->groupid = backup_todb($info['GROUPINGGROUP']['#']['GROUPID']['0']['#']);
3013 $gro_member->timeadded = backup_todb($info['GROUPINGGROUP']['#']['TIMEADDED']['0']['#']);
3015 if (!$grouping = backup_getid($restore->backup_unique_code,"groupings",$gro_member->groupingid)) {
3016 $status = false;
3017 continue;
3020 if (!$group = backup_getid($restore->backup_unique_code,"groups",$gro_member->groupid)) {
3021 $status = false;
3022 continue;
3025 $gro_member->groupid = $group->new_id;
3026 $gro_member->groupingid = $grouping->new_id;
3027 if (!get_record('groupings_groups', 'groupid', $gro_member->groupid, 'groupingid', $gro_member->groupingid)) {
3028 if (!insert_record('groupings_groups', $gro_member)) {
3029 $status = false;
3035 return $status;
3038 //This function creates all the course events
3039 function restore_create_events($restore,$xml_file) {
3041 global $CFG, $db;
3043 $status = true;
3044 //Check it exists
3045 if (!file_exists($xml_file)) {
3046 $status = false;
3048 //Get info from xml
3049 if ($status) {
3050 //events will contain the old_id of every event
3051 //in backup_ids->info will be the real info (serialized)
3052 $events = restore_read_xml_events($restore,$xml_file);
3055 //Get admin->id for later use
3056 $admin = get_admin();
3057 $adminid = $admin->id;
3059 //Now, if we have anything in events, we have to restore that
3060 //events
3061 if ($events) {
3062 if ($events !== true) {
3063 //Iterate over each event
3064 foreach ($events as $event) {
3065 //Get record from backup_ids
3066 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
3067 //Init variables
3068 $create_event = false;
3070 if ($data) {
3071 //Now get completed xmlized object
3072 $info = $data->info;
3073 //traverse_xmlize($info); //Debug
3074 //print_object ($GLOBALS['traverse_array']); //Debug
3075 //$GLOBALS['traverse_array']=""; //Debug
3077 //if necessary, write to restorelog and adjust date/time fields
3078 if ($restore->course_startdateoffset) {
3079 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
3082 //Now build the EVENT record structure
3083 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
3084 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
3085 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
3086 $eve->courseid = $restore->course_id;
3087 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
3088 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
3089 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
3090 $eve->modulename = "";
3091 if (!empty($info['EVENT']['#']['MODULENAME'])) {
3092 $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
3094 $eve->instance = 0;
3095 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
3096 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
3097 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
3098 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
3099 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
3101 //Now search if that event exists (by name, description, timestart fields) in
3102 //restore->course_id course
3103 $eve_db = get_record_select("event",
3104 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
3105 //If it doesn't exist, create
3106 if (!$eve_db) {
3107 $create_event = true;
3109 //If we must create the event
3110 if ($create_event) {
3112 //We must recode the userid
3113 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
3114 if ($user) {
3115 $eve->userid = $user->new_id;
3116 } else {
3117 //Assign it to admin
3118 $eve->userid = $adminid;
3121 //We must recode the repeatid if the event has it
3122 if (!empty($eve->repeatid)) {
3123 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
3124 if ($repeat_rec) { //Exists, so use it...
3125 $eve->repeatid = $repeat_rec->new_id;
3126 } else { //Doesn't exists, calculate the next and save it
3127 $oldrepeatid = $eve->repeatid;
3128 $max_rec = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event');
3129 $eve->repeatid = empty($max_rec) ? 1 : $max_rec->repeatid + 1;
3130 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
3134 //We have to recode the groupid field
3135 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
3136 if ($group) {
3137 $eve->groupid = $group->new_id;
3138 } else {
3139 //Assign it to group 0
3140 $eve->groupid = 0;
3143 //The structure is equal to the db, so insert the event
3144 $newid = insert_record ("event",$eve);
3145 } else {
3146 //get current event id
3147 $newid = $eve_db->id;
3149 if ($newid) {
3150 //We have the newid, update backup_ids
3151 backup_putid($restore->backup_unique_code,"event",
3152 $event->id, $newid);
3157 } else {
3158 $status = false;
3160 return $status;
3163 //This function decode things to make restore multi-site fully functional
3164 //It does this conversions:
3165 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
3166 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
3168 //Note: Inter-activities linking is being implemented as a final
3169 //step in the restore execution, because we need to have it
3170 //finished to know all the oldid, newid equivaleces
3171 function restore_decode_absolute_links($content) {
3173 global $CFG,$restore;
3175 // MDL-10770
3176 // This function was replacing null with empty string
3177 // Nullity check is added in backup_todb(), this function will no longer not be called from backup_todb() if content is null
3178 // I noticed some parts of the restore code is calling this directly instead of calling backup_todb(), so just in case
3179 // 3rd party mod etc are doing the same
3180 if ($content === NULL) {
3181 return NULL;
3184 //Now decode wwwroot and file.php calls
3185 $search = array ("$@FILEPHP@$");
3187 //Check for the status of the slasharguments config variable
3188 $slash = $CFG->slasharguments;
3190 //Build the replace string as needed
3191 if ($slash == 1) {
3192 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
3193 } else {
3194 $replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
3197 $result = str_replace($search,$replace,$content);
3199 if ($result != $content && debugging()) { //Debug
3200 if (!defined('RESTORE_SILENTLY')) {
3201 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
3203 } //Debug
3205 return $result;
3208 //This function restores the userfiles from the temp (user_files) directory to the
3209 //dataroot/users directory
3210 function restore_user_files($restore) {
3212 global $CFG;
3214 $status = true;
3216 $counter = 0;
3218 //First, we check to "users" exists and create is as necessary
3219 //in CFG->dataroot
3220 $dest_dir = $CFG->dataroot."/users";
3221 $status = check_dir_exists($dest_dir,true);
3223 //Now, we iterate over "user_files" records to check if that user dir must be
3224 //copied (and renamed) to the "users" dir.
3225 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
3226 //Check if directory exists
3227 if (is_dir($rootdir)) {
3228 $list = list_directories ($rootdir);
3229 if ($list) {
3230 //Iterate
3231 $counter = 0;
3232 foreach ($list as $dir) {
3233 //Look for dir like username in backup_ids
3234 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
3235 "table_name","user",
3236 "old_id",$dir);
3237 //If thar user exists in backup_ids
3238 if ($data) {
3239 //Only it user has been created now
3240 //or if it existed previously, but he hasn't image (see bug 1123)
3241 if ((strpos($data->info,"new") !== false) or
3242 (!check_dir_exists($dest_dir."/".$data->new_id,false))) {
3243 //Copy the old_dir to its new location (and name) !!
3244 //Only if destination doesn't exists
3245 if (!file_exists($dest_dir."/".$data->new_id)) {
3246 $status = backup_copy_file($rootdir."/".$dir,
3247 $dest_dir."/".$data->new_id,true);
3248 $counter ++;
3250 //Do some output
3251 if ($counter % 2 == 0) {
3252 if (!defined('RESTORE_SILENTLY')) {
3253 echo ".";
3254 if ($counter % 40 == 0) {
3255 echo "<br />";
3258 backup_flush(300);
3265 //If status is ok and whe have dirs created, returns counter to inform
3266 if ($status and $counter) {
3267 return $counter;
3268 } else {
3269 return $status;
3273 //This function restores the groupfiles from the temp (group_files) directory to the
3274 //dataroot/groups directory
3275 function restore_group_files($restore) {
3277 global $CFG;
3279 $status = true;
3281 $counter = 0;
3283 //First, we check to "groups" exists and create is as necessary
3284 //in CFG->dataroot
3285 $dest_dir = $CFG->dataroot.'/groups';
3286 $status = check_dir_exists($dest_dir,true);
3288 //Now, we iterate over "group_files" records to check if that user dir must be
3289 //copied (and renamed) to the "groups" dir.
3290 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
3291 //Check if directory exists
3292 if (is_dir($rootdir)) {
3293 $list = list_directories ($rootdir);
3294 if ($list) {
3295 //Iterate
3296 $counter = 0;
3297 foreach ($list as $dir) {
3298 //Look for dir like groupid in backup_ids
3299 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
3300 "table_name","groups",
3301 "old_id",$dir);
3302 //If that group exists in backup_ids
3303 if ($data) {
3304 if (!file_exists($dest_dir."/".$data->new_id)) {
3305 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
3306 $counter ++;
3308 //Do some output
3309 if ($counter % 2 == 0) {
3310 if (!defined('RESTORE_SILENTLY')) {
3311 echo ".";
3312 if ($counter % 40 == 0) {
3313 echo "<br />";
3316 backup_flush(300);
3322 //If status is ok and whe have dirs created, returns counter to inform
3323 if ($status and $counter) {
3324 return $counter;
3325 } else {
3326 return $status;
3330 //This function restores the course files from the temp (course_files) directory to the
3331 //dataroot/course_id directory
3332 function restore_course_files($restore) {
3334 global $CFG;
3336 $status = true;
3338 $counter = 0;
3340 //First, we check to "course_id" exists and create is as necessary
3341 //in CFG->dataroot
3342 $dest_dir = $CFG->dataroot."/".$restore->course_id;
3343 $status = check_dir_exists($dest_dir,true);
3345 //Now, we iterate over "course_files" records to check if that file/dir must be
3346 //copied to the "dest_dir" dir.
3347 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
3348 //Check if directory exists
3349 if (is_dir($rootdir)) {
3350 $list = list_directories_and_files ($rootdir);
3351 if ($list) {
3352 //Iterate
3353 $counter = 0;
3354 foreach ($list as $dir) {
3355 //Copy the dir to its new location
3356 //Only if destination file/dir doesn exists
3357 if (!file_exists($dest_dir."/".$dir)) {
3358 $status = backup_copy_file($rootdir."/".$dir,
3359 $dest_dir."/".$dir,true);
3360 $counter ++;
3362 //Do some output
3363 if ($counter % 2 == 0) {
3364 if (!defined('RESTORE_SILENTLY')) {
3365 echo ".";
3366 if ($counter % 40 == 0) {
3367 echo "<br />";
3370 backup_flush(300);
3375 //If status is ok and whe have dirs created, returns counter to inform
3376 if ($status and $counter) {
3377 return $counter;
3378 } else {
3379 return $status;
3383 //This function restores the site files from the temp (site_files) directory to the
3384 //dataroot/SITEID directory
3385 function restore_site_files($restore) {
3387 global $CFG;
3389 $status = true;
3391 $counter = 0;
3393 //First, we check to "course_id" exists and create is as necessary
3394 //in CFG->dataroot
3395 $dest_dir = $CFG->dataroot."/".SITEID;
3396 $status = check_dir_exists($dest_dir,true);
3398 //Now, we iterate over "site_files" files to check if that file/dir must be
3399 //copied to the "dest_dir" dir.
3400 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/site_files";
3401 //Check if directory exists
3402 if (is_dir($rootdir)) {
3403 $list = list_directories_and_files ($rootdir);
3404 if ($list) {
3405 //Iterate
3406 $counter = 0;
3407 foreach ($list as $dir) {
3408 //Copy the dir to its new location
3409 //Only if destination file/dir doesn exists
3410 if (!file_exists($dest_dir."/".$dir)) {
3411 $status = backup_copy_file($rootdir."/".$dir,
3412 $dest_dir."/".$dir,true);
3413 $counter ++;
3415 //Do some output
3416 if ($counter % 2 == 0) {
3417 if (!defined('RESTORE_SILENTLY')) {
3418 echo ".";
3419 if ($counter % 40 == 0) {
3420 echo "<br />";
3423 backup_flush(300);
3428 //If status is ok and whe have dirs created, returns counter to inform
3429 if ($status and $counter) {
3430 return $counter;
3431 } else {
3432 return $status;
3437 //This function creates all the structures for every module in backup file
3438 //Depending what has been selected.
3439 function restore_create_modules($restore,$xml_file) {
3441 global $CFG;
3442 $status = true;
3443 //Check it exists
3444 if (!file_exists($xml_file)) {
3445 $status = false;
3447 //Get info from xml
3448 if ($status) {
3449 //info will contain the id and modtype of every module
3450 //in backup_ids->info will be the real info (serialized)
3451 $info = restore_read_xml_modules($restore,$xml_file);
3453 //Now, if we have anything in info, we have to restore that mods
3454 //from backup_ids (calling every mod restore function)
3455 if ($info) {
3456 if ($info !== true) {
3457 if (!defined('RESTORE_SILENTLY')) {
3458 echo '<ul>';
3460 //Iterate over each module
3461 foreach ($info as $mod) {
3462 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
3463 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
3464 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
3465 $modrestore = $mod->modtype."_restore_mods";
3466 if (function_exists($modrestore)) { //Debug
3467 $status = $status and $modrestore($mod,$restore); //bit operator & not reliable here!
3468 } else {
3469 //Something was wrong. Function should exist.
3470 $status = false;
3474 if (!defined('RESTORE_SILENTLY')) {
3475 echo '</ul>';
3478 } else {
3479 $status = false;
3481 return $status;
3484 //This function creates all the structures for every log in backup file
3485 //Depending what has been selected.
3486 function restore_create_logs($restore,$xml_file) {
3488 global $CFG,$db;
3490 //Number of records to get in every chunk
3491 $recordset_size = 4;
3492 //Counter, points to current record
3493 $counter = 0;
3494 //To count all the recods to restore
3495 $count_logs = 0;
3497 $status = true;
3498 //Check it exists
3499 if (!file_exists($xml_file)) {
3500 $status = false;
3502 //Get info from xml
3503 if ($status) {
3504 //count_logs will contain the number of logs entries to process
3505 //in backup_ids->info will be the real info (serialized)
3506 $count_logs = restore_read_xml_logs($restore,$xml_file);
3509 //Now, if we have records in count_logs, we have to restore that logs
3510 //from backup_ids. This piece of code makes calls to:
3511 // - restore_log_course() if it's a course log
3512 // - restore_log_user() if it's a user log
3513 // - restore_log_module() if it's a module log.
3514 //And all is segmented in chunks to allow large recordsets to be restored !!
3515 if ($count_logs > 0) {
3516 while ($counter < $count_logs) {
3517 //Get a chunk of records
3518 //Take old_id twice to avoid adodb limitation
3519 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id,old_id",$counter,$recordset_size);
3520 //We have logs
3521 if ($logs) {
3522 //Iterate
3523 foreach ($logs as $log) {
3524 //Get the full record from backup_ids
3525 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
3526 if ($data) {
3527 //Now get completed xmlized object
3528 $info = $data->info;
3529 //traverse_xmlize($info); //Debug
3530 //print_object ($GLOBALS['traverse_array']); //Debug
3531 //$GLOBALS['traverse_array']=""; //Debug
3532 //Now build the LOG record structure
3533 $dblog = new object();
3534 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
3535 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
3536 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
3537 $dblog->course = $restore->course_id;
3538 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
3539 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
3540 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
3541 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
3542 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
3543 //We have to recode the userid field
3544 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
3545 if ($user) {
3546 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
3547 $dblog->userid = $user->new_id;
3549 //We have to recode the cmid field (if module isn't "course" or "user")
3550 if ($dblog->module != "course" and $dblog->module != "user") {
3551 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
3552 if ($cm) {
3553 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
3554 $dblog->cmid = $cm->new_id;
3555 } else {
3556 $dblog->cmid = 0;
3559 //print_object ($dblog); //Debug
3560 //Now, we redirect to the needed function to make all the work
3561 if ($dblog->module == "course") {
3562 //It's a course log,
3563 $stat = restore_log_course($restore,$dblog);
3564 } elseif ($dblog->module == "user") {
3565 //It's a user log,
3566 $stat = restore_log_user($restore,$dblog);
3567 } else {
3568 //It's a module log,
3569 $stat = restore_log_module($restore,$dblog);
3573 //Do some output
3574 $counter++;
3575 if ($counter % 10 == 0) {
3576 if (!defined('RESTORE_SILENTLY')) {
3577 echo ".";
3578 if ($counter % 200 == 0) {
3579 echo "<br />";
3582 backup_flush(300);
3585 } else {
3586 //We never should arrive here
3587 $counter = $count_logs;
3588 $status = false;
3593 return $status;
3596 //This function inserts a course log record, calculating the URL field as necessary
3597 function restore_log_course($restore,$log) {
3599 $status = true;
3600 $toinsert = false;
3602 //echo "<hr />Before transformations<br />"; //Debug
3603 //print_object($log); //Debug
3604 //Depending of the action, we recode different things
3605 switch ($log->action) {
3606 case "view":
3607 $log->url = "view.php?id=".$log->course;
3608 $log->info = $log->course;
3609 $toinsert = true;
3610 break;
3611 case "guest":
3612 $log->url = "view.php?id=".$log->course;
3613 $toinsert = true;
3614 break;
3615 case "user report":
3616 //recode the info field (it's the user id)
3617 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3618 if ($user) {
3619 $log->info = $user->new_id;
3620 //Now, extract the mode from the url field
3621 $mode = substr(strrchr($log->url,"="),1);
3622 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
3623 $toinsert = true;
3625 break;
3626 case "add mod":
3627 //Extract the course_module from the url field
3628 $cmid = substr(strrchr($log->url,"="),1);
3629 //recode the course_module to see it it has been restored
3630 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3631 if ($cm) {
3632 $cmid = $cm->new_id;
3633 //Extract the module name and the module id from the info field
3634 $modname = strtok($log->info," ");
3635 $modid = strtok(" ");
3636 //recode the module id to see if it has been restored
3637 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3638 if ($mod) {
3639 $modid = $mod->new_id;
3640 //Now I have everything so reconstruct url and info
3641 $log->info = $modname." ".$modid;
3642 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3643 $toinsert = true;
3646 break;
3647 case "update mod":
3648 //Extract the course_module from the url field
3649 $cmid = substr(strrchr($log->url,"="),1);
3650 //recode the course_module to see it it has been restored
3651 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3652 if ($cm) {
3653 $cmid = $cm->new_id;
3654 //Extract the module name and the module id from the info field
3655 $modname = strtok($log->info," ");
3656 $modid = strtok(" ");
3657 //recode the module id to see if it has been restored
3658 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3659 if ($mod) {
3660 $modid = $mod->new_id;
3661 //Now I have everything so reconstruct url and info
3662 $log->info = $modname." ".$modid;
3663 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3664 $toinsert = true;
3667 break;
3668 case "delete mod":
3669 $log->url = "view.php?id=".$log->course;
3670 $toinsert = true;
3671 break;
3672 case "update":
3673 $log->url = "edit.php?id=".$log->course;
3674 $log->info = "";
3675 $toinsert = true;
3676 break;
3677 case "unenrol":
3678 //recode the info field (it's the user id)
3679 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3680 if ($user) {
3681 $log->info = $user->new_id;
3682 $log->url = "view.php?id=".$log->course;
3683 $toinsert = true;
3685 break;
3686 case "enrol":
3687 //recode the info field (it's the user id)
3688 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3689 if ($user) {
3690 $log->info = $user->new_id;
3691 $log->url = "view.php?id=".$log->course;
3692 $toinsert = true;
3694 break;
3695 case "editsection":
3696 //Extract the course_section from the url field
3697 $secid = substr(strrchr($log->url,"="),1);
3698 //recode the course_section to see if it has been restored
3699 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
3700 if ($sec) {
3701 $secid = $sec->new_id;
3702 //Now I have everything so reconstruct url and info
3703 $log->url = "editsection.php?id=".$secid;
3704 $toinsert = true;
3706 break;
3707 case "new":
3708 $log->url = "view.php?id=".$log->course;
3709 $log->info = "";
3710 $toinsert = true;
3711 break;
3712 case "recent":
3713 $log->url = "recent.php?id=".$log->course;
3714 $log->info = "";
3715 $toinsert = true;
3716 break;
3717 case "report log":
3718 $log->url = "report/log/index.php?id=".$log->course;
3719 $log->info = $log->course;
3720 $toinsert = true;
3721 break;
3722 case "report live":
3723 $log->url = "report/log/live.php?id=".$log->course;
3724 $log->info = $log->course;
3725 $toinsert = true;
3726 break;
3727 case "report outline":
3728 $log->url = "report/outline/index.php?id=".$log->course;
3729 $log->info = $log->course;
3730 $toinsert = true;
3731 break;
3732 case "report participation":
3733 $log->url = "report/participation/index.php?id=".$log->course;
3734 $log->info = $log->course;
3735 $toinsert = true;
3736 break;
3737 case "report stats":
3738 $log->url = "report/stats/index.php?id=".$log->course;
3739 $log->info = $log->course;
3740 $toinsert = true;
3741 break;
3742 default:
3743 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3744 break;
3747 //echo "After transformations<br />"; //Debug
3748 //print_object($log); //Debug
3750 //Now if $toinsert is set, insert the record
3751 if ($toinsert) {
3752 //echo "Inserting record<br />"; //Debug
3753 $status = insert_record("log",$log);
3755 return $status;
3758 //This function inserts a user log record, calculating the URL field as necessary
3759 function restore_log_user($restore,$log) {
3761 $status = true;
3762 $toinsert = false;
3764 //echo "<hr />Before transformations<br />"; //Debug
3765 //print_object($log); //Debug
3766 //Depending of the action, we recode different things
3767 switch ($log->action) {
3768 case "view":
3769 //recode the info field (it's the user id)
3770 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3771 if ($user) {
3772 $log->info = $user->new_id;
3773 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3774 $toinsert = true;
3776 break;
3777 case "change password":
3778 //recode the info field (it's the user id)
3779 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3780 if ($user) {
3781 $log->info = $user->new_id;
3782 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3783 $toinsert = true;
3785 break;
3786 case "login":
3787 //recode the info field (it's the user id)
3788 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3789 if ($user) {
3790 $log->info = $user->new_id;
3791 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3792 $toinsert = true;
3794 break;
3795 case "logout":
3796 //recode the info field (it's the user id)
3797 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3798 if ($user) {
3799 $log->info = $user->new_id;
3800 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3801 $toinsert = true;
3803 break;
3804 case "view all":
3805 $log->url = "view.php?id=".$log->course;
3806 $log->info = "";
3807 $toinsert = true;
3808 case "update":
3809 //We split the url by ampersand char
3810 $first_part = strtok($log->url,"&");
3811 //Get data after the = char. It's the user being updated
3812 $userid = substr(strrchr($first_part,"="),1);
3813 //Recode the user
3814 $user = backup_getid($restore->backup_unique_code,"user",$userid);
3815 if ($user) {
3816 $log->info = "";
3817 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
3818 $toinsert = true;
3820 break;
3821 default:
3822 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3823 break;
3826 //echo "After transformations<br />"; //Debug
3827 //print_object($log); //Debug
3829 //Now if $toinsert is set, insert the record
3830 if ($toinsert) {
3831 //echo "Inserting record<br />"; //Debug
3832 $status = insert_record("log",$log);
3834 return $status;
3837 //This function inserts a module log record, calculating the URL field as necessary
3838 function restore_log_module($restore,$log) {
3840 $status = true;
3841 $toinsert = false;
3843 //echo "<hr />Before transformations<br />"; //Debug
3844 //print_object($log); //Debug
3846 //Now we see if the required function in the module exists
3847 $function = $log->module."_restore_logs";
3848 if (function_exists($function)) {
3849 //Call the function
3850 $log = $function($restore,$log);
3851 //If everything is ok, mark the insert flag
3852 if ($log) {
3853 $toinsert = true;
3857 //echo "After transformations<br />"; //Debug
3858 //print_object($log); //Debug
3860 //Now if $toinsert is set, insert the record
3861 if ($toinsert) {
3862 //echo "Inserting record<br />"; //Debug
3863 $status = insert_record("log",$log);
3865 return $status;
3868 //This function adjusts the instance field into course_modules. It's executed after
3869 //modules restore. There, we KNOW the new instance id !!
3870 function restore_check_instances($restore) {
3872 global $CFG;
3874 $status = true;
3876 //We are going to iterate over each course_module saved in backup_ids
3877 $course_modules = get_records_sql("SELECT old_id,new_id
3878 FROM {$CFG->prefix}backup_ids
3879 WHERE backup_code = '$restore->backup_unique_code' AND
3880 table_name = 'course_modules'");
3881 if ($course_modules) {
3882 foreach($course_modules as $cm) {
3883 //Get full record, using backup_getids
3884 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
3885 //Now we are going to the REAL course_modules to get its type (field module)
3886 $module = get_record("course_modules","id",$cm_module->new_id);
3887 if ($module) {
3888 //We know the module type id. Get the name from modules
3889 $type = get_record("modules","id",$module->module);
3890 if ($type) {
3891 //We know the type name and the old_id. Get its new_id
3892 //from backup_ids. It's the instance !!!
3893 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
3894 if ($instance) {
3895 //We have the new instance, so update the record in course_modules
3896 $module->instance = $instance->new_id;
3897 //print_object ($module); //Debug
3898 $status = update_record("course_modules",$module);
3899 } else {
3900 $status = false;
3902 } else {
3903 $status = false;
3905 } else {
3906 $status = false;
3912 return $status;
3915 //=====================================================================================
3916 //== ==
3917 //== XML Functions (SAX) ==
3918 //== ==
3919 //=====================================================================================
3921 //This is the class used to do all the xml parse
3922 class MoodleParser {
3924 var $level = 0; //Level we are
3925 var $counter = 0; //Counter
3926 var $tree = array(); //Array of levels we are
3927 var $content = ""; //Content under current level
3928 var $todo = ""; //What we hav to do when parsing
3929 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
3930 var $temp = ""; //Temp storage.
3931 var $preferences = ""; //Preferences about what to load !!
3932 var $finished = false; //Flag to say xml_parse to stop
3934 //This function is used to get the current contents property value
3935 //They are trimed (and converted from utf8 if needed)
3936 function getContents() {
3937 return trim($this->content);
3940 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3941 function startElementInfo($parser, $tagName, $attrs) {
3942 //Refresh properties
3943 $this->level++;
3944 $this->tree[$this->level] = $tagName;
3946 //Output something to avoid browser timeouts...
3947 backup_flush();
3949 //Check if we are into INFO zone
3950 //if ($this->tree[2] == "INFO") //Debug
3951 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3954 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3955 function startElementRoles($parser, $tagName, $attrs) {
3956 //Refresh properties
3957 $this->level++;
3958 $this->tree[$this->level] = $tagName;
3960 //Output something to avoid browser timeouts...
3961 backup_flush();
3963 //Check if we are into INFO zone
3964 //if ($this->tree[2] == "INFO") //Debug
3965 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3969 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
3970 function startElementCourseHeader($parser, $tagName, $attrs) {
3971 //Refresh properties
3972 $this->level++;
3973 $this->tree[$this->level] = $tagName;
3975 //Output something to avoid browser timeouts...
3976 backup_flush();
3978 //Check if we are into COURSE_HEADER zone
3979 //if ($this->tree[3] == "HEADER") //Debug
3980 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3983 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
3984 function startElementBlocks($parser, $tagName, $attrs) {
3985 //Refresh properties
3986 $this->level++;
3987 $this->tree[$this->level] = $tagName;
3989 //Output something to avoid browser timeouts...
3990 backup_flush();
3992 //Check if we are into BLOCKS zone
3993 //if ($this->tree[3] == "BLOCKS") //Debug
3994 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3997 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
3998 function startElementSections($parser, $tagName, $attrs) {
3999 //Refresh properties
4000 $this->level++;
4001 $this->tree[$this->level] = $tagName;
4003 //Output something to avoid browser timeouts...
4004 backup_flush();
4006 //Check if we are into SECTIONS zone
4007 //if ($this->tree[3] == "SECTIONS") //Debug
4008 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4011 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4012 function startElementFormatData($parser, $tagName, $attrs) {
4013 //Refresh properties
4014 $this->level++;
4015 $this->tree[$this->level] = $tagName;
4017 //Output something to avoid browser timeouts...
4018 backup_flush();
4020 //Accumulate all the data inside this tag
4021 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
4022 if (!isset($this->temp)) {
4023 $this->temp = '';
4025 $this->temp .= "<".$tagName.">";
4028 //Check if we are into FORMATDATA zone
4029 //if ($this->tree[3] == "FORMATDATA") //Debug
4030 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4033 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4034 function startElementMetacourse($parser, $tagName, $attrs) {
4036 //Refresh properties
4037 $this->level++;
4038 $this->tree[$this->level] = $tagName;
4040 //Output something to avoid browser timeouts...
4041 backup_flush();
4043 //Check if we are into METACOURSE zone
4044 //if ($this->tree[3] == "METACOURSE") //Debug
4045 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4048 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4049 function startElementGradebook($parser, $tagName, $attrs) {
4051 //Refresh properties
4052 $this->level++;
4053 $this->tree[$this->level] = $tagName;
4055 //Output something to avoid browser timeouts...
4056 backup_flush();
4058 //Check if we are into GRADEBOOK zone
4059 //if ($this->tree[3] == "GRADEBOOK") //Debug
4060 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4062 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4063 if (isset($this->tree[5]) and isset($this->tree[3])) {
4064 if (($this->tree[5] == "GRADE_ITEM" || $this->tree[5] == "GRADE_CATEGORY" || $this->tree[5] == "GRADE_OUTCOME" || $this->tree[5] == "GRADE_OUTCOMES_COURSE" || $this->tree[5] == "GRADE_CATEGORIES_HISTORY" || $this->tree[5] == "GRADE_GRADES_HISTORY" || $this->tree[5] == "GRADE_TEXT_HISTORY" || $this->tree[5] == "GRADE_ITEM_HISTORY" || $this->tree[5] == "GRADE_OUTCOME_HISTORY") && ($this->tree[3] == "GRADEBOOK")) {
4066 if (!isset($this->temp)) {
4067 $this->temp = "";
4069 $this->temp .= "<".$tagName.">";
4075 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
4076 function startElementUsers($parser, $tagName, $attrs) {
4077 //Refresh properties
4078 $this->level++;
4079 $this->tree[$this->level] = $tagName;
4081 //Check if we are into USERS zone
4082 //if ($this->tree[3] == "USERS") //Debug
4083 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4086 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
4087 function startElementMessages($parser, $tagName, $attrs) {
4088 //Refresh properties
4089 $this->level++;
4090 $this->tree[$this->level] = $tagName;
4092 //Output something to avoid browser timeouts...
4093 backup_flush();
4095 //Check if we are into MESSAGES zone
4096 //if ($this->tree[3] == "MESSAGES") //Debug
4097 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4099 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
4100 if (isset($this->tree[4]) and isset($this->tree[3])) {
4101 if (($this->tree[4] == "MESSAGE" || $this->tree[5] == "CONTACT" ) and ($this->tree[3] == "MESSAGES")) {
4102 if (!isset($this->temp)) {
4103 $this->temp = "";
4105 $this->temp .= "<".$tagName.">";
4109 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
4110 function startElementQuestions($parser, $tagName, $attrs) {
4111 //Refresh properties
4112 $this->level++;
4113 $this->tree[$this->level] = $tagName;
4115 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
4116 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
4117 //} //Debug
4119 //Output something to avoid browser timeouts...
4120 backup_flush();
4122 //Check if we are into QUESTION_CATEGORIES zone
4123 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
4124 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4126 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
4127 if (isset($this->tree[4]) and isset($this->tree[3])) {
4128 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
4129 if (!isset($this->temp)) {
4130 $this->temp = "";
4132 $this->temp .= "<".$tagName.">";
4137 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
4138 function startElementScales($parser, $tagName, $attrs) {
4139 //Refresh properties
4140 $this->level++;
4141 $this->tree[$this->level] = $tagName;
4143 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
4144 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
4145 //} //Debug
4147 //Output something to avoid browser timeouts...
4148 backup_flush();
4150 //Check if we are into SCALES zone
4151 //if ($this->tree[3] == "SCALES") //Debug
4152 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4154 //If we are under a SCALE tag under a SCALES zone, accumule it
4155 if (isset($this->tree[4]) and isset($this->tree[3])) {
4156 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
4157 if (!isset($this->temp)) {
4158 $this->temp = "";
4160 $this->temp .= "<".$tagName.">";
4165 function startElementGroups($parser, $tagName, $attrs) {
4166 //Refresh properties
4167 $this->level++;
4168 $this->tree[$this->level] = $tagName;
4170 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
4171 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
4172 //} //Debug
4174 //Output something to avoid browser timeouts...
4175 backup_flush();
4177 //Check if we are into GROUPS zone
4178 //if ($this->tree[3] == "GROUPS") //Debug
4179 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4181 //If we are under a GROUP tag under a GROUPS zone, accumule it
4182 if (isset($this->tree[4]) and isset($this->tree[3])) {
4183 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
4184 if (!isset($this->temp)) {
4185 $this->temp = "";
4187 $this->temp .= "<".$tagName.">";
4192 function startElementGroupings($parser, $tagName, $attrs) {
4193 //Refresh properties
4194 $this->level++;
4195 $this->tree[$this->level] = $tagName;
4197 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
4198 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
4199 //} //Debug
4201 //Output something to avoid browser timeouts...
4202 backup_flush();
4204 //Check if we are into GROUPINGS zone
4205 //if ($this->tree[3] == "GROUPINGS") //Debug
4206 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4208 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
4209 if (isset($this->tree[4]) and isset($this->tree[3])) {
4210 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
4211 if (!isset($this->temp)) {
4212 $this->temp = "";
4214 $this->temp .= "<".$tagName.">";
4219 function startElementGroupingsGroups($parser, $tagName, $attrs) {
4220 //Refresh properties
4221 $this->level++;
4222 $this->tree[$this->level] = $tagName;
4224 //if ($tagName == "GROUPINGGROUP" && $this->tree[3] == "GROUPINGSGROUPS") { //Debug
4225 // echo "<P>GROUPINGSGROUP: ".strftime ("%X",time()),"-"; //Debug
4226 //} //Debug
4228 //Output something to avoid browser timeouts...
4229 backup_flush();
4231 //Check if we are into GROUPINGSGROUPS zone
4232 //if ($this->tree[3] == "GROUPINGSGROUPS") //Debug
4233 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4235 //If we are under a GROUPINGGROUP tag under a GROUPINGSGROUPS zone, accumule it
4236 if (isset($this->tree[4]) and isset($this->tree[3])) {
4237 if (($this->tree[4] == "GROUPINGGROUP") and ($this->tree[3] == "GROUPINGSGROUPS")) {
4238 if (!isset($this->temp)) {
4239 $this->temp = "";
4241 $this->temp .= "<".$tagName.">";
4246 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
4247 function startElementEvents($parser, $tagName, $attrs) {
4248 //Refresh properties
4249 $this->level++;
4250 $this->tree[$this->level] = $tagName;
4252 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
4253 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
4254 //} //Debug
4256 //Output something to avoid browser timeouts...
4257 backup_flush();
4259 //Check if we are into EVENTS zone
4260 //if ($this->tree[3] == "EVENTS") //Debug
4261 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4263 //If we are under a EVENT tag under a EVENTS zone, accumule it
4264 if (isset($this->tree[4]) and isset($this->tree[3])) {
4265 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
4266 if (!isset($this->temp)) {
4267 $this->temp = "";
4269 $this->temp .= "<".$tagName.">";
4274 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
4275 function startElementModules($parser, $tagName, $attrs) {
4276 //Refresh properties
4277 $this->level++;
4278 $this->tree[$this->level] = $tagName;
4280 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
4281 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
4282 //} //Debug
4284 //Output something to avoid browser timeouts...
4285 backup_flush();
4287 //Check if we are into MODULES zone
4288 //if ($this->tree[3] == "MODULES") //Debug
4289 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4291 //If we are under a MOD tag under a MODULES zone, accumule it
4292 if (isset($this->tree[4]) and isset($this->tree[3])) {
4293 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
4294 if (!isset($this->temp)) {
4295 $this->temp = "";
4297 $this->temp .= "<".$tagName.">";
4302 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
4303 function startElementLogs($parser, $tagName, $attrs) {
4304 //Refresh properties
4305 $this->level++;
4306 $this->tree[$this->level] = $tagName;
4308 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
4309 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
4310 //} //Debug
4312 //Output something to avoid browser timeouts...
4313 backup_flush();
4315 //Check if we are into LOGS zone
4316 //if ($this->tree[3] == "LOGS") //Debug
4317 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4319 //If we are under a LOG tag under a LOGS zone, accumule it
4320 if (isset($this->tree[4]) and isset($this->tree[3])) {
4321 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
4322 if (!isset($this->temp)) {
4323 $this->temp = "";
4325 $this->temp .= "<".$tagName.">";
4330 //This is the startTag default handler we use when todo is undefined
4331 function startElement($parser, $tagName, $attrs) {
4332 $this->level++;
4333 $this->tree[$this->level] = $tagName;
4335 //Output something to avoid browser timeouts...
4336 backup_flush();
4338 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4341 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
4342 function endElementInfo($parser, $tagName) {
4343 //Check if we are into INFO zone
4344 if ($this->tree[2] == "INFO") {
4345 //if (trim($this->content)) //Debug
4346 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4347 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4348 //Dependig of different combinations, do different things
4349 if ($this->level == 3) {
4350 switch ($tagName) {
4351 case "NAME":
4352 $this->info->backup_name = $this->getContents();
4353 break;
4354 case "MOODLE_VERSION":
4355 $this->info->backup_moodle_version = $this->getContents();
4356 break;
4357 case "MOODLE_RELEASE":
4358 $this->info->backup_moodle_release = $this->getContents();
4359 break;
4360 case "BACKUP_VERSION":
4361 $this->info->backup_backup_version = $this->getContents();
4362 break;
4363 case "BACKUP_RELEASE":
4364 $this->info->backup_backup_release = $this->getContents();
4365 break;
4366 case "DATE":
4367 $this->info->backup_date = $this->getContents();
4368 break;
4369 case "ORIGINAL_WWWROOT":
4370 $this->info->original_wwwroot = $this->getContents();
4371 break;
4372 case "MNET_EXTERNALUSERS":
4373 $this->info->mnet_externalusers = $this->getContents();
4374 break;
4377 if ($this->tree[3] == "DETAILS") {
4378 if ($this->level == 4) {
4379 switch ($tagName) {
4380 case "METACOURSE":
4381 $this->info->backup_metacourse = $this->getContents();
4382 break;
4383 case "USERS":
4384 $this->info->backup_users = $this->getContents();
4385 break;
4386 case "LOGS":
4387 $this->info->backup_logs = $this->getContents();
4388 break;
4389 case "USERFILES":
4390 $this->info->backup_user_files = $this->getContents();
4391 break;
4392 case "COURSEFILES":
4393 $this->info->backup_course_files = $this->getContents();
4394 break;
4395 case "SITEFILES":
4396 $this->info->backup_site_files = $this->getContents();
4397 break;
4398 case "MESSAGES":
4399 $this->info->backup_messages = $this->getContents();
4400 break;
4401 case 'BLOCKFORMAT':
4402 $this->info->backup_block_format = $this->getContents();
4403 break;
4406 if ($this->level == 5) {
4407 switch ($tagName) {
4408 case "NAME":
4409 $this->info->tempName = $this->getContents();
4410 break;
4411 case "INCLUDED":
4412 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
4413 break;
4414 case "USERINFO":
4415 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
4416 break;
4419 if ($this->level == 7) {
4420 switch ($tagName) {
4421 case "ID":
4422 $this->info->tempId = $this->getContents();
4423 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
4424 break;
4425 case "NAME":
4426 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
4427 break;
4428 case "INCLUDED":
4429 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
4430 break;
4431 case "USERINFO":
4432 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
4433 break;
4439 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4440 //Speed up a lot (avoid parse all)
4441 if ($tagName == "INFO") {
4442 $this->finished = true;
4445 //Clear things
4446 $this->tree[$this->level] = "";
4447 $this->level--;
4448 $this->content = "";
4452 function endElementRoles($parser, $tagName) {
4453 //Check if we are into INFO zone
4454 if ($this->tree[2] == "ROLES") {
4456 if ($this->tree[3] == "ROLE") {
4457 if ($this->level == 4) {
4458 switch ($tagName) {
4459 case "NAME":
4460 $this->info->tempname = $this->getContents();
4462 break;
4463 case "SHORTNAME":
4464 $this->info->tempshortname = $this->getContents();
4465 break;
4466 case "ID": // this is the old id
4467 $this->info->tempid = $this->getContents();
4468 break;
4471 if ($this->level == 6) {
4472 switch ($tagName) {
4473 case "NAME":
4474 $this->info->roles[$this->info->tempid]->name = $this->info->tempname;
4475 $this->info->roles[$this->info->tempid]->shortname = $this->info->tempshortname;
4477 $this->info->tempcapname = $this->getContents();
4478 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
4479 break;
4480 case "PERMISSION":
4481 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
4482 break;
4483 case "TIMEMODIFIED":
4484 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
4485 break;
4486 case "MODIFIERID":
4487 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
4488 break;
4494 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4495 //Speed up a lot (avoid parse all)
4496 if ($tagName == "ROLES") {
4497 $this->finished = true;
4500 //Clear things
4501 $this->tree[$this->level] = "";
4502 $this->level--;
4503 $this->content = "";
4507 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
4508 function endElementCourseHeader($parser, $tagName) {
4509 //Check if we are into COURSE_HEADER zone
4510 if ($this->tree[3] == "HEADER") {
4511 //if (trim($this->content)) //Debug
4512 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4513 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4514 //Dependig of different combinations, do different things
4515 if ($this->level == 4) {
4516 switch ($tagName) {
4517 case "ID":
4518 $this->info->course_id = $this->getContents();
4519 break;
4520 case "PASSWORD":
4521 $this->info->course_password = $this->getContents();
4522 break;
4523 case "FULLNAME":
4524 $this->info->course_fullname = $this->getContents();
4525 break;
4526 case "SHORTNAME":
4527 $this->info->course_shortname = $this->getContents();
4528 break;
4529 case "IDNUMBER":
4530 $this->info->course_idnumber = $this->getContents();
4531 break;
4532 case "SUMMARY":
4533 $this->info->course_summary = $this->getContents();
4534 break;
4535 case "FORMAT":
4536 $this->info->course_format = $this->getContents();
4537 break;
4538 case "SHOWGRADES":
4539 $this->info->course_showgrades = $this->getContents();
4540 break;
4541 case "BLOCKINFO":
4542 $this->info->blockinfo = $this->getContents();
4543 break;
4544 case "NEWSITEMS":
4545 $this->info->course_newsitems = $this->getContents();
4546 break;
4547 case "TEACHER":
4548 $this->info->course_teacher = $this->getContents();
4549 break;
4550 case "TEACHERS":
4551 $this->info->course_teachers = $this->getContents();
4552 break;
4553 case "STUDENT":
4554 $this->info->course_student = $this->getContents();
4555 break;
4556 case "STUDENTS":
4557 $this->info->course_students = $this->getContents();
4558 break;
4559 case "GUEST":
4560 $this->info->course_guest = $this->getContents();
4561 break;
4562 case "STARTDATE":
4563 $this->info->course_startdate = $this->getContents();
4564 break;
4565 case "NUMSECTIONS":
4566 $this->info->course_numsections = $this->getContents();
4567 break;
4568 //case "SHOWRECENT": INFO: This is out in 1.3
4569 // $this->info->course_showrecent = $this->getContents();
4570 // break;
4571 case "MAXBYTES":
4572 $this->info->course_maxbytes = $this->getContents();
4573 break;
4574 case "SHOWREPORTS":
4575 $this->info->course_showreports = $this->getContents();
4576 break;
4577 case "GROUPMODE":
4578 $this->info->course_groupmode = $this->getContents();
4579 break;
4580 case "GROUPMODEFORCE":
4581 $this->info->course_groupmodeforce = $this->getContents();
4582 break;
4583 case "DEFAULTGROUPINGID":
4584 $this->info->course_defaultgroupingid = $this->getContents();
4585 break;
4586 case "LANG":
4587 $this->info->course_lang = $this->getContents();
4588 break;
4589 case "THEME":
4590 $this->info->course_theme = $this->getContents();
4591 break;
4592 case "COST":
4593 $this->info->course_cost = $this->getContents();
4594 break;
4595 case "CURRENCY":
4596 $this->info->course_currency = $this->getContents();
4597 break;
4598 case "MARKER":
4599 $this->info->course_marker = $this->getContents();
4600 break;
4601 case "VISIBLE":
4602 $this->info->course_visible = $this->getContents();
4603 break;
4604 case "HIDDENSECTIONS":
4605 $this->info->course_hiddensections = $this->getContents();
4606 break;
4607 case "TIMECREATED":
4608 $this->info->course_timecreated = $this->getContents();
4609 break;
4610 case "TIMEMODIFIED":
4611 $this->info->course_timemodified = $this->getContents();
4612 break;
4613 case "METACOURSE":
4614 $this->info->course_metacourse = $this->getContents();
4615 break;
4616 case "EXPIRENOTIFY":
4617 $this->info->course_expirynotify = $this->getContents();
4618 break;
4619 case "NOTIFYSTUDENTS":
4620 $this->info->course_notifystudents = $this->getContents();
4621 break;
4622 case "EXPIRYTHRESHOLD":
4623 $this->info->course_expirythreshold = $this->getContents();
4624 break;
4625 case "ENROLLABLE":
4626 $this->info->course_enrollable = $this->getContents();
4627 break;
4628 case "ENROLSTARTDATE":
4629 $this->info->course_enrolstartdate = $this->getContents();
4630 break;
4631 case "ENROLENDDATE":
4632 $this->info->course_enrolenddate = $this->getContents();
4633 break;
4634 case "ENROLPERIOD":
4635 $this->info->course_enrolperiod = $this->getContents();
4636 break;
4639 if ($this->tree[4] == "CATEGORY") {
4640 if ($this->level == 5) {
4641 switch ($tagName) {
4642 case "ID":
4643 $this->info->category->id = $this->getContents();
4644 break;
4645 case "NAME":
4646 $this->info->category->name = $this->getContents();
4647 break;
4652 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
4653 if ($this->level == 6) {
4654 switch ($tagName) {
4655 case "NAME":
4656 $this->info->tempname = $this->getContents();
4657 break;
4658 case "SHORTNAME":
4659 $this->info->tempshortname = $this->getContents();
4660 break;
4661 case "ID":
4662 $this->info->tempid = $this->getContents();
4663 break;
4667 if ($this->level == 8) {
4668 switch ($tagName) {
4669 case "USERID":
4670 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4671 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4672 $this->info->tempuser = $this->getContents();
4673 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4674 break;
4675 case "HIDDEN":
4676 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4677 break;
4678 case "TIMESTART":
4679 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4680 break;
4681 case "TIMEEND":
4682 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4683 break;
4684 case "TIMEMODIFIED":
4685 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4686 break;
4687 case "MODIFIERID":
4688 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4689 break;
4690 case "ENROL":
4691 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4692 break;
4693 case "SORTORDER":
4694 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4695 break;
4699 } /// ends role_assignments
4701 if ($this->tree[4] == "ROLES_OVERRIDES") {
4702 if ($this->level == 6) {
4703 switch ($tagName) {
4704 case "NAME":
4705 $this->info->tempname = $this->getContents();
4706 break;
4707 case "SHORTNAME":
4708 $this->info->tempshortname = $this->getContents();
4709 break;
4710 case "ID":
4711 $this->info->tempid = $this->getContents();
4712 break;
4716 if ($this->level == 8) {
4717 switch ($tagName) {
4718 case "NAME":
4719 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4720 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4721 $this->info->tempname = $this->getContents(); // change to name of capability
4722 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4723 break;
4724 case "PERMISSION":
4725 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4726 break;
4727 case "TIMEMODIFIED":
4728 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4729 break;
4730 case "MODIFIERID":
4731 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4732 break;
4735 } /// ends role_overrides
4738 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
4739 //Speed up a lot (avoid parse all)
4740 if ($tagName == "HEADER") {
4741 $this->finished = true;
4744 //Clear things
4745 $this->tree[$this->level] = "";
4746 $this->level--;
4747 $this->content = "";
4751 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
4752 function endElementBlocks($parser, $tagName) {
4753 //Check if we are into BLOCKS zone
4754 if ($this->tree[3] == 'BLOCKS') {
4755 //if (trim($this->content)) //Debug
4756 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4757 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4758 //Dependig of different combinations, do different things
4759 if ($this->level == 4) {
4760 switch ($tagName) {
4761 case 'BLOCK':
4762 //We've finalized a block, get it
4763 $this->info->instances[] = $this->info->tempinstance;
4764 unset($this->info->tempinstance);
4765 break;
4766 default:
4767 die($tagName);
4770 if ($this->level == 5) {
4771 switch ($tagName) {
4772 case 'ID':
4773 $this->info->tempinstance->id = $this->getContents();
4774 case 'NAME':
4775 $this->info->tempinstance->name = $this->getContents();
4776 break;
4777 case 'PAGEID':
4778 $this->info->tempinstance->pageid = $this->getContents();
4779 break;
4780 case 'PAGETYPE':
4781 $this->info->tempinstance->pagetype = $this->getContents();
4782 break;
4783 case 'POSITION':
4784 $this->info->tempinstance->position = $this->getContents();
4785 break;
4786 case 'WEIGHT':
4787 $this->info->tempinstance->weight = $this->getContents();
4788 break;
4789 case 'VISIBLE':
4790 $this->info->tempinstance->visible = $this->getContents();
4791 break;
4792 case 'CONFIGDATA':
4793 $this->info->tempinstance->configdata = $this->getContents();
4794 break;
4795 default:
4796 break;
4800 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
4801 if ($this->level == 7) {
4802 switch ($tagName) {
4803 case "NAME":
4804 $this->info->tempname = $this->getContents();
4805 break;
4806 case "SHORTNAME":
4807 $this->info->tempshortname = $this->getContents();
4808 break;
4809 case "ID":
4810 $this->info->tempid = $this->getContents(); // temp roleid
4811 break;
4815 if ($this->level == 9) {
4817 switch ($tagName) {
4818 case "USERID":
4819 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4821 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4823 $this->info->tempuser = $this->getContents();
4825 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4826 break;
4827 case "HIDDEN":
4828 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4829 break;
4830 case "TIMESTART":
4831 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4832 break;
4833 case "TIMEEND":
4834 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4835 break;
4836 case "TIMEMODIFIED":
4837 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4838 break;
4839 case "MODIFIERID":
4840 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4841 break;
4842 case "ENROL":
4843 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4844 break;
4845 case "SORTORDER":
4846 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4847 break;
4851 } /// ends role_assignments
4853 if ($this->tree[5] == "ROLES_OVERRIDES") {
4854 if ($this->level == 7) {
4855 switch ($tagName) {
4856 case "NAME":
4857 $this->info->tempname = $this->getContents();
4858 break;
4859 case "SHORTNAME":
4860 $this->info->tempshortname = $this->getContents();
4861 break;
4862 case "ID":
4863 $this->info->tempid = $this->getContents(); // temp roleid
4864 break;
4868 if ($this->level == 9) {
4869 switch ($tagName) {
4870 case "NAME":
4872 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4873 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4874 $this->info->tempname = $this->getContents(); // change to name of capability
4875 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4876 break;
4877 case "PERMISSION":
4878 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4879 break;
4880 case "TIMEMODIFIED":
4881 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4882 break;
4883 case "MODIFIERID":
4884 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4885 break;
4888 } /// ends role_overrides
4891 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
4892 //Speed up a lot (avoid parse all)
4893 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
4894 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
4895 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
4896 $this->finished = true;
4899 //Clear things
4900 $this->tree[$this->level] = '';
4901 $this->level--;
4902 $this->content = "";
4905 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
4906 function endElementSections($parser, $tagName) {
4907 //Check if we are into SECTIONS zone
4908 if ($this->tree[3] == "SECTIONS") {
4909 //if (trim($this->content)) //Debug
4910 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4911 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4912 //Dependig of different combinations, do different things
4913 if ($this->level == 4) {
4914 switch ($tagName) {
4915 case "SECTION":
4916 //We've finalized a section, get it
4917 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
4918 unset($this->info->tempsection);
4921 if ($this->level == 5) {
4922 switch ($tagName) {
4923 case "ID":
4924 $this->info->tempsection->id = $this->getContents();
4925 break;
4926 case "NUMBER":
4927 $this->info->tempsection->number = $this->getContents();
4928 break;
4929 case "SUMMARY":
4930 $this->info->tempsection->summary = $this->getContents();
4931 break;
4932 case "VISIBLE":
4933 $this->info->tempsection->visible = $this->getContents();
4934 break;
4937 if ($this->level == 6) {
4938 switch ($tagName) {
4939 case "MOD":
4940 if (!isset($this->info->tempmod->groupmode)) {
4941 $this->info->tempmod->groupmode = 0;
4943 if (!isset($this->info->tempmod->groupingid)) {
4944 $this->info->tempmod->groupingid = 0;
4946 if (!isset($this->info->tempmod->groupmembersonly)) {
4947 $this->info->tempmod->groupmembersonly = 0;
4950 //We've finalized a mod, get it
4951 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
4952 $this->info->tempmod->type;
4953 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
4954 $this->info->tempmod->instance;
4955 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
4956 $this->info->tempmod->added;
4957 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
4958 $this->info->tempmod->score;
4959 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
4960 $this->info->tempmod->indent;
4961 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
4962 $this->info->tempmod->visible;
4963 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
4964 $this->info->tempmod->groupmode;
4965 $this->info->tempsection->mods[$this->info->tempmod->id]->groupingid =
4966 $this->info->tempmod->groupingid;
4967 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmembersonly =
4968 $this->info->tempmod->groupmembersonly;
4970 unset($this->info->tempmod);
4973 if ($this->level == 7) {
4974 switch ($tagName) {
4975 case "ID":
4976 $this->info->tempmod->id = $this->getContents();
4977 break;
4978 case "TYPE":
4979 $this->info->tempmod->type = $this->getContents();
4980 break;
4981 case "INSTANCE":
4982 $this->info->tempmod->instance = $this->getContents();
4983 break;
4984 case "ADDED":
4985 $this->info->tempmod->added = $this->getContents();
4986 break;
4987 case "SCORE":
4988 $this->info->tempmod->score = $this->getContents();
4989 break;
4990 case "INDENT":
4991 $this->info->tempmod->indent = $this->getContents();
4992 break;
4993 case "VISIBLE":
4994 $this->info->tempmod->visible = $this->getContents();
4995 break;
4996 case "GROUPMODE":
4997 $this->info->tempmod->groupmode = $this->getContents();
4998 break;
4999 case "GROUPINGID":
5000 $this->info->tempmod->groupingid = $this->getContents();
5001 break;
5002 case "GROUPMEMBERSONLY":
5003 $this->info->tempmod->groupmembersonly = $this->getContents();
5004 break;
5005 default:
5006 break;
5010 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
5012 if ($this->level == 9) {
5013 switch ($tagName) {
5014 case "NAME":
5015 $this->info->tempname = $this->getContents();
5016 break;
5017 case "SHORTNAME":
5018 $this->info->tempshortname = $this->getContents();
5019 break;
5020 case "ID":
5021 $this->info->tempid = $this->getContents(); // temp roleid
5022 break;
5026 if ($this->level == 11) {
5027 switch ($tagName) {
5028 case "USERID":
5029 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5031 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5033 $this->info->tempuser = $this->getContents();
5035 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5036 break;
5037 case "HIDDEN":
5038 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5039 break;
5040 case "TIMESTART":
5041 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5042 break;
5043 case "TIMEEND":
5044 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5045 break;
5046 case "TIMEMODIFIED":
5047 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5048 break;
5049 case "MODIFIERID":
5050 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5051 break;
5052 case "ENROL":
5053 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5054 break;
5055 case "SORTORDER":
5056 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5057 break;
5061 } /// ends role_assignments
5063 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
5064 if ($this->level == 9) {
5065 switch ($tagName) {
5066 case "NAME":
5067 $this->info->tempname = $this->getContents();
5068 break;
5069 case "SHORTNAME":
5070 $this->info->tempshortname = $this->getContents();
5071 break;
5072 case "ID":
5073 $this->info->tempid = $this->getContents(); // temp roleid
5074 break;
5078 if ($this->level == 11) {
5079 switch ($tagName) {
5080 case "NAME":
5082 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5083 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5084 $this->info->tempname = $this->getContents(); // change to name of capability
5085 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5086 break;
5087 case "PERMISSION":
5088 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5089 break;
5090 case "TIMEMODIFIED":
5091 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5092 break;
5093 case "MODIFIERID":
5094 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5095 break;
5098 } /// ends role_overrides
5102 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
5103 //Speed up a lot (avoid parse all)
5104 if ($tagName == "SECTIONS") {
5105 $this->finished = true;
5108 //Clear things
5109 $this->tree[$this->level] = "";
5110 $this->level--;
5111 $this->content = "";
5115 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
5116 function endElementFormatData($parser, $tagName) {
5117 //Check if we are into FORMATDATA zone
5118 if ($this->tree[3] == 'FORMATDATA') {
5119 if (!isset($this->temp)) {
5120 $this->temp = '';
5122 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5125 if($tagName=='FORMATDATA') {
5126 //Did we have any data? If not don't bother
5127 if($this->temp!='<FORMATDATA></FORMATDATA>') {
5128 //Prepend XML standard header to info gathered
5129 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5130 $this->temp='';
5132 //Call to xmlize for this portion of xml data (the FORMATDATA block)
5133 $this->info->format_data = xmlize($xml_data,0);
5135 //Stop parsing at end of FORMATDATA
5136 $this->finished=true;
5139 //Clear things
5140 $this->tree[$this->level] = "";
5141 $this->level--;
5142 $this->content = "";
5145 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
5146 function endElementMetacourse($parser, $tagName) {
5147 //Check if we are into METACOURSE zone
5148 if ($this->tree[3] == 'METACOURSE') {
5149 //if (trim($this->content)) //Debug
5150 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5151 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5152 //Dependig of different combinations, do different things
5153 if ($this->level == 5) {
5154 switch ($tagName) {
5155 case 'CHILD':
5156 //We've finalized a child, get it
5157 $this->info->childs[] = $this->info->tempmeta;
5158 unset($this->info->tempmeta);
5159 break;
5160 case 'PARENT':
5161 //We've finalized a parent, get it
5162 $this->info->parents[] = $this->info->tempmeta;
5163 unset($this->info->tempmeta);
5164 break;
5165 default:
5166 die($tagName);
5169 if ($this->level == 6) {
5170 switch ($tagName) {
5171 case 'ID':
5172 $this->info->tempmeta->id = $this->getContents();
5173 break;
5174 case 'IDNUMBER':
5175 $this->info->tempmeta->idnumber = $this->getContents();
5176 break;
5177 case 'SHORTNAME':
5178 $this->info->tempmeta->shortname = $this->getContents();
5179 break;
5184 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
5185 //Speed up a lot (avoid parse all)
5186 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
5187 $this->finished = true;
5190 //Clear things
5191 $this->tree[$this->level] = '';
5192 $this->level--;
5193 $this->content = "";
5196 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5197 function endElementGradebook($parser, $tagName) {
5198 //Check if we are into GRADEBOOK zone
5199 if ($this->tree[3] == "GRADEBOOK") {
5200 //if (trim($this->content)) //Debug
5201 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5202 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5203 //Acumulate data to info (content + close tag)
5204 //Reconvert: strip htmlchars again and trim to generate xml data
5205 if (!isset($this->temp)) {
5206 $this->temp = "";
5208 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5209 // We have finished outcome, grade_category or grade_item, reset accumulated
5210 // data because they are close tags
5211 if ($this->level == 4) {
5212 $this->temp = "";
5214 //If we've finished a grade item, xmlize it an save to db
5215 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
5216 //Prepend XML standard header to info gathered
5217 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5218 //Call to xmlize for this portion of xml data (one PREFERENCE)
5219 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5220 $data = xmlize($xml_data,0);
5221 //echo strftime ("%X",time())."<p>"; //Debug
5222 //traverse_xmlize($data); //Debug
5223 //print_object ($GLOBALS['traverse_array']); //Debug
5224 //$GLOBALS['traverse_array']=""; //Debug
5225 //Now, save data to db. We'll use it later
5226 //Get id and status from data
5227 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
5228 $this->counter++;
5229 //Save to db
5231 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
5232 null,$data);
5233 //Create returning info
5234 $this->info = $this->counter;
5235 //Reset temp
5237 unset($this->temp);
5240 //If we've finished a grade_category, xmlize it an save to db
5241 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
5242 //Prepend XML standard header to info gathered
5243 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5244 //Call to xmlize for this portion of xml data (one CATECORY)
5245 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5246 $data = xmlize($xml_data,0);
5247 //echo strftime ("%X",time())."<p>"; //Debug
5248 //traverse_xmlize($data); //Debug
5249 //print_object ($GLOBALS['traverse_array']); //Debug
5250 //$GLOBALS['traverse_array']=""; //Debug
5251 //Now, save data to db. We'll use it later
5252 //Get id and status from data
5253 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
5254 $this->counter++;
5255 //Save to db
5256 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
5257 null,$data);
5258 //Create returning info
5259 $this->info = $this->counter;
5260 //Reset temp
5261 unset($this->temp);
5264 //If we've finished a grade_outcome, xmlize it an save to db
5265 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
5266 //Prepend XML standard header to info gathered
5267 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5268 //Call to xmlize for this portion of xml data (one CATECORY)
5269 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5270 $data = xmlize($xml_data,0);
5271 //echo strftime ("%X",time())."<p>"; //Debug
5272 //traverse_xmlize($data); //Debug
5273 //print_object ($GLOBALS['traverse_array']); //Debug
5274 //$GLOBALS['traverse_array']=""; //Debug
5275 //Now, save data to db. We'll use it later
5276 //Get id and status from data
5277 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
5278 $this->counter++;
5279 //Save to db
5280 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
5281 null,$data);
5282 //Create returning info
5283 $this->info = $this->counter;
5284 //Reset temp
5285 unset($this->temp);
5288 //If we've finished a grade_outcomes_course, xmlize it an save to db
5289 if (($this->level == 5) and ($tagName == "GRADE_OUTCOMES_COURSE")) {
5290 //Prepend XML standard header to info gathered
5291 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5292 //Call to xmlize for this portion of xml data (one CATECORY)
5293 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5294 $data = xmlize($xml_data,0);
5295 //echo strftime ("%X",time())."<p>"; //Debug
5296 //traverse_xmlize($data); //Debug
5297 //print_object ($GLOBALS['traverse_array']); //Debug
5298 //$GLOBALS['traverse_array']=""; //Debug
5299 //Now, save data to db. We'll use it later
5300 //Get id and status from data
5301 $outcomes_course_id = $data["GRADE_OUTCOMES_COURSE"]["#"]["ID"]["0"]["#"];
5302 $this->counter++;
5303 //Save to db
5304 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_courses' ,$outcomes_course_id,
5305 null,$data);
5306 //Create returning info
5307 $this->info = $this->counter;
5308 //Reset temp
5309 unset($this->temp);
5312 if (($this->level == 5) and ($tagName == "GRADE_CATEGORIES_HISTORY")) {
5313 //Prepend XML standard header to info gathered
5314 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5315 //Call to xmlize for this portion of xml data (one PREFERENCE)
5316 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5317 $data = xmlize($xml_data,0);
5318 //echo strftime ("%X",time())."<p>"; //Debug
5319 //traverse_xmlize($data); //Debug
5320 //print_object ($GLOBALS['traverse_array']); //Debug
5321 //$GLOBALS['traverse_array']=""; //Debug
5322 //Now, save data to db. We'll use it later
5323 //Get id and status from data
5324 $id = $data["GRADE_CATEGORIES_HISTORY"]["#"]["ID"]["0"]["#"];
5325 $this->counter++;
5326 //Save to db
5328 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories_history', $id,
5329 null,$data);
5330 //Create returning info
5331 $this->info = $this->counter;
5332 //Reset temp
5334 unset($this->temp);
5337 if (($this->level == 5) and ($tagName == "GRADE_GRADES_HISTORY")) {
5338 //Prepend XML standard header to info gathered
5339 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5340 //Call to xmlize for this portion of xml data (one PREFERENCE)
5341 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5342 $data = xmlize($xml_data,0);
5343 //echo strftime ("%X",time())."<p>"; //Debug
5344 //traverse_xmlize($data); //Debug
5345 //print_object ($GLOBALS['traverse_array']); //Debug
5346 //$GLOBALS['traverse_array']=""; //Debug
5347 //Now, save data to db. We'll use it later
5348 //Get id and status from data
5349 $id = $data["GRADE_GRADES_HISTORY"]["#"]["ID"]["0"]["#"];
5350 $this->counter++;
5351 //Save to db
5353 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_history', $id,
5354 null,$data);
5355 //Create returning info
5356 $this->info = $this->counter;
5357 //Reset temp
5359 unset($this->temp);
5362 if (($this->level == 5) and ($tagName == "GRADE_TEXT_HISTORY")) {
5363 //Prepend XML standard header to info gathered
5364 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5365 //Call to xmlize for this portion of xml data (one PREFERENCE)
5366 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5367 $data = xmlize($xml_data,0);
5368 //echo strftime ("%X",time())."<p>"; //Debug
5369 //traverse_xmlize($data); //Debug
5370 //print_object ($GLOBALS['traverse_array']); //Debug
5371 //$GLOBALS['traverse_array']=""; //Debug
5372 //Now, save data to db. We'll use it later
5373 //Get id and status from data
5374 $id = $data["GRADE_TEXT_HISTORY"]["#"]["ID"]["0"]["#"];
5375 $this->counter++;
5376 //Save to db
5377 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_text_history', $id,
5378 null,$data);
5379 //Create returning info
5380 $this->info = $this->counter;
5381 //Reset temp
5383 unset($this->temp);
5386 if (($this->level == 5) and ($tagName == "GRADE_ITEM_HISTORY")) {
5387 //Prepend XML standard header to info gathered
5388 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5389 //Call to xmlize for this portion of xml data (one PREFERENCE)
5390 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5391 $data = xmlize($xml_data,0);
5392 //echo strftime ("%X",time())."<p>"; //Debug
5393 //traverse_xmlize($data); //Debug
5394 //print_object ($GLOBALS['traverse_array']); //Debug
5395 //$GLOBALS['traverse_array']=""; //Debug
5396 //Now, save data to db. We'll use it later
5397 //Get id and status from data
5398 $id = $data["GRADE_ITEM_HISTORY"]["#"]["ID"]["0"]["#"];
5399 $this->counter++;
5400 //Save to db
5402 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items_history', $id,
5403 null,$data);
5404 //Create returning info
5405 $this->info = $this->counter;
5406 //Reset temp
5408 unset($this->temp);
5411 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME_HISTORY")) {
5412 //Prepend XML standard header to info gathered
5413 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5414 //Call to xmlize for this portion of xml data (one PREFERENCE)
5415 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5416 $data = xmlize($xml_data,0);
5417 //echo strftime ("%X",time())."<p>"; //Debug
5418 //traverse_xmlize($data); //Debug
5419 //print_object ($GLOBALS['traverse_array']); //Debug
5420 //$GLOBALS['traverse_array']=""; //Debug
5421 //Now, save data to db. We'll use it later
5422 //Get id and status from data
5423 $id = $data["GRADE_OUTCOME_HISTORY"]["#"]["ID"]["0"]["#"];
5424 $this->counter++;
5425 //Save to db
5427 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_history', $id,
5428 null,$data);
5429 //Create returning info
5430 $this->info = $this->counter;
5431 //Reset temp
5433 unset($this->temp);
5437 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
5438 //Speed up a lot (avoid parse all)
5439 if ($tagName == "GRADEBOOK" and $this->level == 3) {
5440 $this->finished = true;
5441 $this->counter = 0;
5444 //Clear things
5445 $this->tree[$this->level] = "";
5446 $this->level--;
5447 $this->content = "";
5451 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
5452 function endElementUsers($parser, $tagName) {
5453 global $CFG;
5454 //Check if we are into USERS zone
5455 if ($this->tree[3] == "USERS") {
5456 //if (trim($this->content)) //Debug
5457 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5458 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5459 //Dependig of different combinations, do different things
5460 if ($this->level == 4) {
5461 switch ($tagName) {
5462 case "USER":
5463 //Increment counter
5464 $this->counter++;
5465 //Save to db, only save if record not already exist
5466 // if there already is an new_id for this entry, just use that new_id?
5467 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
5468 if (isset($newuser->new_id)) {
5469 $newid = $newuser->new_id;
5470 } else {
5471 $newid = null;
5474 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
5475 $newid,$this->info->tempuser);
5477 //Do some output
5478 if ($this->counter % 10 == 0) {
5479 if (!defined('RESTORE_SILENTLY')) {
5480 echo ".";
5481 if ($this->counter % 200 == 0) {
5482 echo "<br />";
5485 backup_flush(300);
5488 //Delete temp obejct
5489 unset($this->info->tempuser);
5490 break;
5493 if ($this->level == 5) {
5494 switch ($tagName) {
5495 case "ID":
5496 $this->info->users[$this->getContents()] = $this->getContents();
5497 $this->info->tempuser->id = $this->getContents();
5498 break;
5499 case "AUTH":
5500 $this->info->tempuser->auth = $this->getContents();
5501 break;
5502 case "CONFIRMED":
5503 $this->info->tempuser->confirmed = $this->getContents();
5504 break;
5505 case "POLICYAGREED":
5506 $this->info->tempuser->policyagreed = $this->getContents();
5507 break;
5508 case "DELETED":
5509 $this->info->tempuser->deleted = $this->getContents();
5510 break;
5511 case "USERNAME":
5512 $this->info->tempuser->username = $this->getContents();
5513 break;
5514 case "PASSWORD":
5515 $this->info->tempuser->password = $this->getContents();
5516 break;
5517 case "IDNUMBER":
5518 $this->info->tempuser->idnumber = $this->getContents();
5519 break;
5520 case "FIRSTNAME":
5521 $this->info->tempuser->firstname = $this->getContents();
5522 break;
5523 case "LASTNAME":
5524 $this->info->tempuser->lastname = $this->getContents();
5525 break;
5526 case "EMAIL":
5527 $this->info->tempuser->email = $this->getContents();
5528 break;
5529 case "EMAILSTOP":
5530 $this->info->tempuser->emailstop = $this->getContents();
5531 break;
5532 case "ICQ":
5533 $this->info->tempuser->icq = $this->getContents();
5534 break;
5535 case "SKYPE":
5536 $this->info->tempuser->skype = $this->getContents();
5537 break;
5538 case "AIM":
5539 $this->info->tempuser->aim = $this->getContents();
5540 break;
5541 case "YAHOO":
5542 $this->info->tempuser->yahoo = $this->getContents();
5543 break;
5544 case "MSN":
5545 $this->info->tempuser->msn = $this->getContents();
5546 break;
5547 case "PHONE1":
5548 $this->info->tempuser->phone1 = $this->getContents();
5549 break;
5550 case "PHONE2":
5551 $this->info->tempuser->phone2 = $this->getContents();
5552 break;
5553 case "INSTITUTION":
5554 $this->info->tempuser->institution = $this->getContents();
5555 break;
5556 case "DEPARTMENT":
5557 $this->info->tempuser->department = $this->getContents();
5558 break;
5559 case "ADDRESS":
5560 $this->info->tempuser->address = $this->getContents();
5561 break;
5562 case "CITY":
5563 $this->info->tempuser->city = $this->getContents();
5564 break;
5565 case "COUNTRY":
5566 $this->info->tempuser->country = $this->getContents();
5567 break;
5568 case "LANG":
5569 $this->info->tempuser->lang = $this->getContents();
5570 break;
5571 case "THEME":
5572 $this->info->tempuser->theme = $this->getContents();
5573 break;
5574 case "TIMEZONE":
5575 $this->info->tempuser->timezone = $this->getContents();
5576 break;
5577 case "FIRSTACCESS":
5578 $this->info->tempuser->firstaccess = $this->getContents();
5579 break;
5580 case "LASTACCESS":
5581 $this->info->tempuser->lastaccess = $this->getContents();
5582 break;
5583 case "LASTLOGIN":
5584 $this->info->tempuser->lastlogin = $this->getContents();
5585 break;
5586 case "CURRENTLOGIN":
5587 $this->info->tempuser->currentlogin = $this->getContents();
5588 break;
5589 case "LASTIP":
5590 $this->info->tempuser->lastip = $this->getContents();
5591 break;
5592 case "SECRET":
5593 $this->info->tempuser->secret = $this->getContents();
5594 break;
5595 case "PICTURE":
5596 $this->info->tempuser->picture = $this->getContents();
5597 break;
5598 case "URL":
5599 $this->info->tempuser->url = $this->getContents();
5600 break;
5601 case "DESCRIPTION":
5602 $this->info->tempuser->description = $this->getContents();
5603 break;
5604 case "MAILFORMAT":
5605 $this->info->tempuser->mailformat = $this->getContents();
5606 break;
5607 case "MAILDIGEST":
5608 $this->info->tempuser->maildigest = $this->getContents();
5609 break;
5610 case "MAILDISPLAY":
5611 $this->info->tempuser->maildisplay = $this->getContents();
5612 break;
5613 case "HTMLEDITOR":
5614 $this->info->tempuser->htmleditor = $this->getContents();
5615 break;
5616 case "AJAX":
5617 $this->info->tempuser->ajax = $this->getContents();
5618 break;
5619 case "AUTOSUBSCRIBE":
5620 $this->info->tempuser->autosubscribe = $this->getContents();
5621 break;
5622 case "TRACKFORUMS":
5623 $this->info->tempuser->trackforums = $this->getContents();
5624 break;
5625 case "MNETHOSTURL":
5626 $this->info->tempuser->mnethosturl = $this->getContents();
5627 break;
5628 case "TIMEMODIFIED":
5629 $this->info->tempuser->timemodified = $this->getContents();
5630 break;
5631 default:
5632 break;
5635 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
5636 switch ($tagName) {
5637 case "ROLE":
5638 //We've finalized a role, get it
5639 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
5640 unset($this->info->temprole);
5641 break;
5642 case "USER_PREFERENCE":
5643 //We've finalized a user_preference, get it
5644 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
5645 unset($this->info->tempuserpreference);
5646 break;
5650 if ($this->level == 7) {
5651 switch ($tagName) {
5652 case "TYPE":
5653 $this->info->temprole->type = $this->getContents();
5654 break;
5655 case "AUTHORITY":
5656 $this->info->temprole->authority = $this->getContents();
5657 break;
5658 case "TEA_ROLE":
5659 $this->info->temprole->tea_role = $this->getContents();
5660 break;
5661 case "EDITALL":
5662 $this->info->temprole->editall = $this->getContents();
5663 break;
5664 case "TIMESTART":
5665 $this->info->temprole->timestart = $this->getContents();
5666 break;
5667 case "TIMEEND":
5668 $this->info->temprole->timeend = $this->getContents();
5669 break;
5670 case "TIMEMODIFIED":
5671 $this->info->temprole->timemodified = $this->getContents();
5672 break;
5673 case "TIMESTART":
5674 $this->info->temprole->timestart = $this->getContents();
5675 break;
5676 case "TIMEEND":
5677 $this->info->temprole->timeend = $this->getContents();
5678 break;
5679 case "TIME":
5680 $this->info->temprole->time = $this->getContents();
5681 break;
5682 case "TIMEACCESS":
5683 $this->info->temprole->timeaccess = $this->getContents();
5684 break;
5685 case "ENROL":
5686 $this->info->temprole->enrol = $this->getContents();
5687 break;
5688 case "NAME":
5689 $this->info->tempuserpreference->name = $this->getContents();
5690 break;
5691 case "VALUE":
5692 $this->info->tempuserpreference->value = $this->getContents();
5693 break;
5694 default:
5695 break;
5700 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
5702 if ($this->level == 7) {
5703 switch ($tagName) {
5704 case "NAME":
5705 $this->info->tempname = $this->getContents();
5706 break;
5707 case "SHORTNAME":
5708 $this->info->tempshortname = $this->getContents();
5709 break;
5710 case "ID":
5711 $this->info->tempid = $this->getContents(); // temp roleid
5712 break;
5716 if ($this->level == 9) {
5718 switch ($tagName) {
5719 case "USERID":
5720 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5722 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5724 $this->info->tempuserid = $this->getContents();
5726 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
5727 break;
5728 case "HIDDEN":
5729 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
5730 break;
5731 case "TIMESTART":
5732 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
5733 break;
5734 case "TIMEEND":
5735 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
5736 break;
5737 case "TIMEMODIFIED":
5738 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
5739 break;
5740 case "MODIFIERID":
5741 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
5742 break;
5743 case "ENROL":
5744 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
5745 break;
5746 case "SORTORDER":
5747 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
5748 break;
5752 } /// ends role_assignments
5754 if ($this->tree[5] == "ROLES_OVERRIDES") {
5755 if ($this->level == 7) {
5756 switch ($tagName) {
5757 case "NAME":
5758 $this->info->tempname = $this->getContents();
5759 break;
5760 case "SHORTNAME":
5761 $this->info->tempshortname = $this->getContents();
5762 break;
5763 case "ID":
5764 $this->info->tempid = $this->getContents(); // temp roleid
5765 break;
5769 if ($this->level == 9) {
5770 switch ($tagName) {
5771 case "NAME":
5773 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5774 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5775 $this->info->tempname = $this->getContents(); // change to name of capability
5776 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5777 break;
5778 case "PERMISSION":
5779 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5780 break;
5781 case "TIMEMODIFIED":
5782 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5783 break;
5784 case "MODIFIERID":
5785 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5786 break;
5789 } /// ends role_overrides
5791 } // closes if this->tree[3]=="users"
5793 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
5794 //Speed up a lot (avoid parse all)
5795 if ($tagName == "USERS" and $this->level == 3) {
5796 $this->finished = true;
5797 $this->counter = 0;
5800 //Clear things
5801 $this->tree[$this->level] = "";
5802 $this->level--;
5803 $this->content = "";
5807 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
5808 function endElementMessages($parser, $tagName) {
5809 //Check if we are into MESSAGES zone
5810 if ($this->tree[3] == "MESSAGES") {
5811 //if (trim($this->content)) //Debug
5812 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5813 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5814 //Acumulate data to info (content + close tag)
5815 //Reconvert: strip htmlchars again and trim to generate xml data
5816 if (!isset($this->temp)) {
5817 $this->temp = "";
5819 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5820 //If we've finished a message, xmlize it an save to db
5821 if (($this->level == 4) and ($tagName == "MESSAGE")) {
5822 //Prepend XML standard header to info gathered
5823 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5824 //Call to xmlize for this portion of xml data (one MESSAGE)
5825 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5826 $data = xmlize($xml_data,0);
5827 //echo strftime ("%X",time())."<p>"; //Debug
5828 //traverse_xmlize($data); //Debug
5829 //print_object ($GLOBALS['traverse_array']); //Debug
5830 //$GLOBALS['traverse_array']=""; //Debug
5831 //Now, save data to db. We'll use it later
5832 //Get id and status from data
5833 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
5834 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
5835 if ($message_status == "READ") {
5836 $table = "message_read";
5837 } else {
5838 $table = "message";
5840 $this->counter++;
5841 //Save to db
5842 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
5843 null,$data);
5844 //Create returning info
5845 $this->info = $this->counter;
5846 //Reset temp
5847 unset($this->temp);
5849 //If we've finished a contact, xmlize it an save to db
5850 if (($this->level == 5) and ($tagName == "CONTACT")) {
5851 //Prepend XML standard header to info gathered
5852 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5853 //Call to xmlize for this portion of xml data (one MESSAGE)
5854 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5855 $data = xmlize($xml_data,0);
5856 //echo strftime ("%X",time())."<p>"; //Debug
5857 //traverse_xmlize($data); //Debug
5858 //print_object ($GLOBALS['traverse_array']); //Debug
5859 //$GLOBALS['traverse_array']=""; //Debug
5860 //Now, save data to db. We'll use it later
5861 //Get id and status from data
5862 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
5863 $this->counter++;
5864 //Save to db
5865 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
5866 null,$data);
5867 //Create returning info
5868 $this->info = $this->counter;
5869 //Reset temp
5870 unset($this->temp);
5874 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
5875 //Speed up a lot (avoid parse all)
5876 if ($tagName == "MESSAGES" and $this->level == 3) {
5877 $this->finished = true;
5878 $this->counter = 0;
5881 //Clear things
5882 $this->tree[$this->level] = "";
5883 $this->level--;
5884 $this->content = "";
5888 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
5889 function endElementQuestions($parser, $tagName) {
5890 //Check if we are into QUESTION_CATEGORIES zone
5891 if ($this->tree[3] == "QUESTION_CATEGORIES") {
5892 //if (trim($this->content)) //Debug
5893 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5894 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5895 //Acumulate data to info (content + close tag)
5896 //Reconvert: strip htmlchars again and trim to generate xml data
5897 if (!isset($this->temp)) {
5898 $this->temp = "";
5900 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5901 //If we've finished a mod, xmlize it an save to db
5902 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
5903 //Prepend XML standard header to info gathered
5904 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5905 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
5906 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5907 $data = xmlize($xml_data,0);
5908 //echo strftime ("%X",time())."<p>"; //Debug
5909 //traverse_xmlize($data); //Debug
5910 //print_object ($GLOBALS['traverse_array']); //Debug
5911 //$GLOBALS['traverse_array']=""; //Debug
5912 //Now, save data to db. We'll use it later
5913 //Get id from data
5914 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
5915 //Save to db
5916 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
5917 null,$data);
5918 //Create returning info
5919 $ret_info = new object();
5920 $ret_info->id = $category_id;
5921 $this->info[] = $ret_info;
5922 //Reset temp
5923 unset($this->temp);
5927 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
5928 //Speed up a lot (avoid parse all)
5929 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
5930 $this->finished = true;
5933 //Clear things
5934 $this->tree[$this->level] = "";
5935 $this->level--;
5936 $this->content = "";
5940 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
5941 function endElementScales($parser, $tagName) {
5942 //Check if we are into SCALES zone
5943 if ($this->tree[3] == "SCALES") {
5944 //if (trim($this->content)) //Debug
5945 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5946 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5947 //Acumulate data to info (content + close tag)
5948 //Reconvert: strip htmlchars again and trim to generate xml data
5949 if (!isset($this->temp)) {
5950 $this->temp = "";
5952 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5953 //If we've finished a scale, xmlize it an save to db
5954 if (($this->level == 4) and ($tagName == "SCALE")) {
5955 //Prepend XML standard header to info gathered
5956 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5957 //Call to xmlize for this portion of xml data (one SCALE)
5958 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5959 $data = xmlize($xml_data,0);
5960 //echo strftime ("%X",time())."<p>"; //Debug
5961 //traverse_xmlize($data); //Debug
5962 //print_object ($GLOBALS['traverse_array']); //Debug
5963 //$GLOBALS['traverse_array']=""; //Debug
5964 //Now, save data to db. We'll use it later
5965 //Get id and from data
5966 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
5967 //Save to db
5968 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
5969 null,$data);
5970 //Create returning info
5971 $ret_info = new object();
5972 $ret_info->id = $scale_id;
5973 $this->info[] = $ret_info;
5974 //Reset temp
5975 unset($this->temp);
5979 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
5980 //Speed up a lot (avoid parse all)
5981 if ($tagName == "SCALES" and $this->level == 3) {
5982 $this->finished = true;
5985 //Clear things
5986 $this->tree[$this->level] = "";
5987 $this->level--;
5988 $this->content = "";
5992 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
5993 function endElementGroups($parser, $tagName) {
5994 //Check if we are into GROUPS zone
5995 if ($this->tree[3] == "GROUPS") {
5996 //if (trim($this->content)) //Debug
5997 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5998 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5999 //Acumulate data to info (content + close tag)
6000 //Reconvert: strip htmlchars again and trim to generate xml data
6001 if (!isset($this->temp)) {
6002 $this->temp = "";
6004 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6005 //If we've finished a group, xmlize it an save to db
6006 if (($this->level == 4) and ($tagName == "GROUP")) {
6007 //Prepend XML standard header to info gathered
6008 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6009 //Call to xmlize for this portion of xml data (one GROUP)
6010 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6011 $data = xmlize($xml_data,0);
6012 //echo strftime ("%X",time())."<p>"; //Debug
6013 //traverse_xmlize($data); //Debug
6014 //print_object ($GLOBALS['traverse_array']); //Debug
6015 //$GLOBALS['traverse_array']=""; //Debug
6016 //Now, save data to db. We'll use it later
6017 //Get id and from data
6018 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
6019 //Save to db
6020 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
6021 null,$data);
6022 //Create returning info
6023 $ret_info = new Object();
6024 $ret_info->id = $group_id;
6025 $this->info[] = $ret_info;
6026 //Reset temp
6027 unset($this->temp);
6031 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
6032 //Speed up a lot (avoid parse all)
6033 if ($tagName == "GROUPS" and $this->level == 3) {
6034 $this->finished = true;
6037 //Clear things
6038 $this->tree[$this->level] = "";
6039 $this->level--;
6040 $this->content = "";
6044 //This is the endTag handler we use where we are reading the groupings zone (todo="GROUPINGS")
6045 function endElementGroupings($parser, $tagName) {
6046 //Check if we are into GROUPINGS zone
6047 if ($this->tree[3] == "GROUPINGS") {
6048 //Acumulate data to info (content + close tag)
6049 //Reconvert: strip htmlchars again and trim to generate xml data
6050 if (!isset($this->temp)) {
6051 $this->temp = "";
6053 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6054 //If we've finished a group, xmlize it an save to db
6055 if (($this->level == 4) and ($tagName == "GROUPING")) {
6056 //Prepend XML standard header to info gathered
6057 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6058 //Call to xmlize for this portion of xml data (one GROUPING)
6059 $data = xmlize($xml_data,0);
6060 //Now, save data to db. We'll use it later
6061 //Get id and from data
6062 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
6063 //Save to db
6064 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
6065 null,$data);
6066 //Create returning info
6067 $ret_info = new Object();
6068 $ret_info->id = $grouping_id;
6069 $this->info[] = $ret_info;
6070 //Reset temp
6071 unset($this->temp);
6075 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6076 //Speed up a lot (avoid parse all)
6077 if ($tagName == "GROUPINGS" and $this->level == 3) {
6078 $this->finished = true;
6081 //Clear things
6082 $this->tree[$this->level] = "";
6083 $this->level--;
6084 $this->content = "";
6088 //This is the endTag handler we use where we are reading the groupingsgroups zone (todo="GROUPINGGROUPS")
6089 function endElementGroupingsGroups($parser, $tagName) {
6090 //Check if we are into GROUPINGSGROUPS zone
6091 if ($this->tree[3] == "GROUPINGSGROUPS") {
6092 //Acumulate data to info (content + close tag)
6093 //Reconvert: strip htmlchars again and trim to generate xml data
6094 if (!isset($this->temp)) {
6095 $this->temp = "";
6097 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6098 //If we've finished a group, xmlize it an save to db
6099 if (($this->level == 4) and ($tagName == "GROUPINGGROUP")) {
6100 //Prepend XML standard header to info gathered
6101 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6102 //Call to xmlize for this portion of xml data (one GROUPING)
6103 $data = xmlize($xml_data,0);
6104 //Now, save data to db. We'll use it later
6105 //Get id and from data
6106 $groupinggroup_id = $data["GROUPINGGROUP"]["#"]["ID"]["0"]["#"];
6107 //Save to db
6108 $status = backup_putid($this->preferences->backup_unique_code,"groupingsgroups",$groupinggroup_id,
6109 null,$data);
6110 //Create returning info
6111 $ret_info = new Object();
6112 $ret_info->id = $groupinggroup_id;
6113 $this->info[] = $ret_info;
6114 //Reset temp
6115 unset($this->temp);
6119 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6120 //Speed up a lot (avoid parse all)
6121 if ($tagName == "GROUPINGSGROUPS" and $this->level == 3) {
6122 $this->finished = true;
6125 //Clear things
6126 $this->tree[$this->level] = "";
6127 $this->level--;
6128 $this->content = "";
6132 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
6133 function endElementEvents($parser, $tagName) {
6134 //Check if we are into EVENTS zone
6135 if ($this->tree[3] == "EVENTS") {
6136 //if (trim($this->content)) //Debug
6137 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6138 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6139 //Acumulate data to info (content + close tag)
6140 //Reconvert: strip htmlchars again and trim to generate xml data
6141 if (!isset($this->temp)) {
6142 $this->temp = "";
6144 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6145 //If we've finished a event, xmlize it an save to db
6146 if (($this->level == 4) and ($tagName == "EVENT")) {
6147 //Prepend XML standard header to info gathered
6148 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6149 //Call to xmlize for this portion of xml data (one EVENT)
6150 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6151 $data = xmlize($xml_data,0);
6152 //echo strftime ("%X",time())."<p>"; //Debug
6153 //traverse_xmlize($data); //Debug
6154 //print_object ($GLOBALS['traverse_array']); //Debug
6155 //$GLOBALS['traverse_array']=""; //Debug
6156 //Now, save data to db. We'll use it later
6157 //Get id and from data
6158 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
6159 //Save to db
6160 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
6161 null,$data);
6162 //Create returning info
6163 $ret_info = new object();
6164 $ret_info->id = $event_id;
6165 $this->info[] = $ret_info;
6166 //Reset temp
6167 unset($this->temp);
6171 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
6172 //Speed up a lot (avoid parse all)
6173 if ($tagName == "EVENTS" and $this->level == 3) {
6174 $this->finished = true;
6177 //Clear things
6178 $this->tree[$this->level] = "";
6179 $this->level--;
6180 $this->content = "";
6184 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
6185 function endElementModules($parser, $tagName) {
6186 //Check if we are into MODULES zone
6187 if ($this->tree[3] == "MODULES") {
6188 //if (trim($this->content)) //Debug
6189 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6190 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6191 //Acumulate data to info (content + close tag)
6192 //Reconvert: strip htmlchars again and trim to generate xml data
6193 if (!isset($this->temp)) {
6194 $this->temp = "";
6196 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6197 //If we've finished a mod, xmlize it an save to db
6198 if (($this->level == 4) and ($tagName == "MOD")) {
6199 //Prepend XML standard header to info gathered
6200 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6201 //Call to xmlize for this portion of xml data (one MOD)
6202 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6203 $data = xmlize($xml_data,0);
6204 //echo strftime ("%X",time())."<p>"; //Debug
6205 //traverse_xmlize($data); //Debug
6206 //print_object ($GLOBALS['traverse_array']); //Debug
6207 //$GLOBALS['traverse_array']=""; //Debug
6208 //Now, save data to db. We'll use it later
6209 //Get id and modtype from data
6210 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
6211 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
6212 //Only if we've selected to restore it
6213 if (!empty($this->preferences->mods[$mod_type]->restore)) {
6214 //Save to db
6215 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
6216 null,$data);
6217 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6218 //Create returning info
6219 $ret_info = new object();
6220 $ret_info->id = $mod_id;
6221 $ret_info->modtype = $mod_type;
6222 $this->info[] = $ret_info;
6224 //Reset temp
6225 unset($this->temp);
6231 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
6232 //Speed up a lot (avoid parse all)
6233 if ($tagName == "MODULES" and $this->level == 3) {
6234 $this->finished = true;
6237 //Clear things
6238 $this->tree[$this->level] = "";
6239 $this->level--;
6240 $this->content = "";
6244 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
6245 function endElementLogs($parser, $tagName) {
6246 //Check if we are into LOGS zone
6247 if ($this->tree[3] == "LOGS") {
6248 //if (trim($this->content)) //Debug
6249 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6250 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6251 //Acumulate data to info (content + close tag)
6252 //Reconvert: strip htmlchars again and trim to generate xml data
6253 if (!isset($this->temp)) {
6254 $this->temp = "";
6256 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6257 //If we've finished a log, xmlize it an save to db
6258 if (($this->level == 4) and ($tagName == "LOG")) {
6259 //Prepend XML standard header to info gathered
6260 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6261 //Call to xmlize for this portion of xml data (one LOG)
6262 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6263 $data = xmlize($xml_data,0);
6264 //echo strftime ("%X",time())."<p>"; //Debug
6265 //traverse_xmlize($data); //Debug
6266 //print_object ($GLOBALS['traverse_array']); //Debug
6267 //$GLOBALS['traverse_array']=""; //Debug
6268 //Now, save data to db. We'll use it later
6269 //Get id and modtype from data
6270 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
6271 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
6272 //We only save log entries from backup file if they are:
6273 // - Course logs
6274 // - User logs
6275 // - Module logs about one restored module
6276 if ($log_module == "course" or
6277 $log_module == "user" or
6278 $this->preferences->mods[$log_module]->restore) {
6279 //Increment counter
6280 $this->counter++;
6281 //Save to db
6282 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
6283 null,$data);
6284 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6285 //Create returning info
6286 $this->info = $this->counter;
6288 //Reset temp
6289 unset($this->temp);
6293 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
6294 //Speed up a lot (avoid parse all)
6295 if ($tagName == "LOGS" and $this->level == 3) {
6296 $this->finished = true;
6297 $this->counter = 0;
6300 //Clear things
6301 $this->tree[$this->level] = "";
6302 $this->level--;
6303 $this->content = "";
6307 //This is the endTag default handler we use when todo is undefined
6308 function endElement($parser, $tagName) {
6309 if (trim($this->content)) //Debug
6310 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6311 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6313 //Clear things
6314 $this->tree[$this->level] = "";
6315 $this->level--;
6316 $this->content = "";
6319 //This is the handler to read data contents (simple accumule it)
6320 function characterData($parser, $data) {
6321 $this->content .= $data;
6325 //This function executes the MoodleParser
6326 function restore_read_xml ($xml_file,$todo,$preferences) {
6328 $status = true;
6330 $xml_parser = xml_parser_create('UTF-8');
6331 $moodle_parser = new MoodleParser();
6332 $moodle_parser->todo = $todo;
6333 $moodle_parser->preferences = $preferences;
6334 xml_set_object($xml_parser,$moodle_parser);
6335 //Depending of the todo we use some element_handler or another
6336 if ($todo == "INFO") {
6337 //Define handlers to that zone
6338 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
6339 } else if ($todo == "ROLES") {
6340 // Define handlers to that zone
6341 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
6342 } else if ($todo == "COURSE_HEADER") {
6343 //Define handlers to that zone
6344 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
6345 } else if ($todo == 'BLOCKS') {
6346 //Define handlers to that zone
6347 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
6348 } else if ($todo == "SECTIONS") {
6349 //Define handlers to that zone
6350 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
6351 } else if ($todo == 'FORMATDATA') {
6352 //Define handlers to that zone
6353 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
6354 } else if ($todo == "METACOURSE") {
6355 //Define handlers to that zone
6356 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
6357 } else if ($todo == "GRADEBOOK") {
6358 //Define handlers to that zone
6359 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
6360 } else if ($todo == "USERS") {
6361 //Define handlers to that zone
6362 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
6363 } else if ($todo == "MESSAGES") {
6364 //Define handlers to that zone
6365 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
6366 } else if ($todo == "QUESTIONS") {
6367 //Define handlers to that zone
6368 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
6369 } else if ($todo == "SCALES") {
6370 //Define handlers to that zone
6371 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
6372 } else if ($todo == "GROUPS") {
6373 //Define handlers to that zone
6374 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
6375 } else if ($todo == "GROUPINGS") {
6376 //Define handlers to that zone
6377 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
6378 } else if ($todo == "GROUPINGSGROUPS") {
6379 //Define handlers to that zone
6380 xml_set_element_handler($xml_parser, "startElementGroupingsGroups", "endElementGroupingsGroups");
6381 } else if ($todo == "EVENTS") {
6382 //Define handlers to that zone
6383 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
6384 } else if ($todo == "MODULES") {
6385 //Define handlers to that zone
6386 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
6387 } else if ($todo == "LOGS") {
6388 //Define handlers to that zone
6389 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
6390 } else {
6391 //Define default handlers (must no be invoked when everything become finished)
6392 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
6394 xml_set_character_data_handler($xml_parser, "characterData");
6395 $fp = fopen($xml_file,"r")
6396 or $status = false;
6397 if ($status) {
6398 while ($data = fread($fp, 4096) and !$moodle_parser->finished)
6399 xml_parse($xml_parser, $data, feof($fp))
6400 or die(sprintf("XML error: %s at line %d",
6401 xml_error_string(xml_get_error_code($xml_parser)),
6402 xml_get_current_line_number($xml_parser)));
6403 fclose($fp);
6405 //Get info from parser
6406 $info = $moodle_parser->info;
6408 //Clear parser mem
6409 xml_parser_free($xml_parser);
6411 if ($status && !empty($info)) {
6412 return $info;
6413 } else {
6414 return $status;
6419 * @param string $errorstr passed by reference, if silent is true,
6420 * errorstr will be populated and this function will return false rather than calling error() or notify()
6421 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
6422 * redirect to the next step in the restore process, instead will return $backup_unique_code
6424 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
6426 global $CFG, $SESSION;
6428 //Prepend dataroot to variable to have the absolute path
6429 $file = $CFG->dataroot."/".$file;
6431 if (!defined('RESTORE_SILENTLY')) {
6432 //Start the main table
6433 echo "<table cellpadding=\"5\">";
6434 echo "<tr><td>";
6436 //Start the mail ul
6437 echo "<ul>";
6440 //Check the file exists
6441 if (!is_file($file)) {
6442 if (!defined('RESTORE_SILENTLY')) {
6443 error ("File not exists ($file)");
6444 } else {
6445 $errorstr = "File not exists ($file)";
6446 return false;
6450 //Check the file name ends with .zip
6451 if (!substr($file,-4) == ".zip") {
6452 if (!defined('RESTORE_SILENTLY')) {
6453 error ("File has an incorrect extension");
6454 } else {
6455 $errorstr = 'File has an incorrect extension';
6456 return false;
6460 //Now calculate the unique_code for this restore
6461 $backup_unique_code = time();
6463 //Now check and create the backup dir (if it doesn't exist)
6464 if (!defined('RESTORE_SILENTLY')) {
6465 echo "<li>".get_string("creatingtemporarystructures").'</li>';
6467 $status = check_and_create_backup_dir($backup_unique_code);
6468 //Empty dir
6469 if ($status) {
6470 $status = clear_backup_dir($backup_unique_code);
6473 //Now delete old data and directories under dataroot/temp/backup
6474 if ($status) {
6475 if (!defined('RESTORE_SILENTLY')) {
6476 echo "<li>".get_string("deletingolddata").'</li>';
6478 $status = backup_delete_old_data();
6481 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
6482 if ($status) {
6483 if (!defined('RESTORE_SILENTLY')) {
6484 echo "<li>".get_string("copyingzipfile").'</li>';
6486 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
6487 if (!defined('RESTORE_SILENTLY')) {
6488 notify("Error copying backup file. Invalid name or bad perms.");
6489 } else {
6490 $errorstr = "Error copying backup file. Invalid name or bad perms";
6491 return false;
6496 //Now unzip the file
6497 if ($status) {
6498 if (!defined('RESTORE_SILENTLY')) {
6499 echo "<li>".get_string("unzippingbackup").'</li>';
6501 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
6502 if (!defined('RESTORE_SILENTLY')) {
6503 notify("Error unzipping backup file. Invalid zip file.");
6504 } else {
6505 $errorstr = "Error unzipping backup file. Invalid zip file.";
6506 return false;
6511 //Check for Blackboard backups and convert
6512 if ($status){
6513 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
6514 if (!defined('RESTORE_SILENTLY')) {
6515 echo "<li>".get_string("checkingforbbexport").'</li>';
6517 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
6520 //Now check for the moodle.xml file
6521 if ($status) {
6522 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
6523 if (!defined('RESTORE_SILENTLY')) {
6524 echo "<li>".get_string("checkingbackup").'</li>';
6526 if (! $status = restore_check_moodle_file ($xml_file)) {
6527 if (!is_file($xml_file)) {
6528 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
6529 } else {
6530 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
6532 if (!defined('RESTORE_SILENTLY')) {
6533 notify($errorstr);
6534 } else {
6535 return false;
6540 $info = "";
6541 $course_header = "";
6543 //Now read the info tag (all)
6544 if ($status) {
6545 if (!defined('RESTORE_SILENTLY')) {
6546 echo "<li>".get_string("readinginfofrombackup").'</li>';
6548 //Reading info from file
6549 $info = restore_read_xml_info ($xml_file);
6550 //Reading course_header from file
6551 $course_header = restore_read_xml_course_header ($xml_file);
6554 if (!defined('RESTORE_SILENTLY')) {
6555 //End the main ul
6556 echo "</ul>\n";
6558 //End the main table
6559 echo "</td></tr>";
6560 echo "</table>";
6563 //We compare Moodle's versions
6564 if ($CFG->version < $info->backup_moodle_version && $status) {
6565 $message = new message();
6566 $message->serverversion = $CFG->version;
6567 $message->serverrelease = $CFG->release;
6568 $message->backupversion = $info->backup_moodle_version;
6569 $message->backuprelease = $info->backup_moodle_release;
6570 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
6574 //Now we print in other table, the backup and the course it contains info
6575 if ($info and $course_header and $status) {
6576 //First, the course info
6577 if (!defined('RESTORE_SILENTLY')) {
6578 $status = restore_print_course_header($course_header);
6580 //Now, the backup info
6581 if ($status) {
6582 if (!defined('RESTORE_SILENTLY')) {
6583 $status = restore_print_info($info);
6588 //Save course header and info into php session
6589 if ($status) {
6590 $SESSION->info = $info;
6591 $SESSION->course_header = $course_header;
6594 //Finally, a little form to continue
6595 //with some hidden fields
6596 if ($status) {
6597 if (!defined('RESTORE_SILENTLY')) {
6598 echo "<br /><div style='text-align:center'>";
6599 $hidden["backup_unique_code"] = $backup_unique_code;
6600 $hidden["launch"] = "form";
6601 $hidden["file"] = $file;
6602 $hidden["id"] = $id;
6603 print_single_button("restore.php", $hidden, get_string("continue"),"post");
6604 echo "</div>";
6606 else {
6607 if (empty($noredirect)) {
6608 redirect($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
6609 } else {
6610 return $backup_unique_code;
6615 if (!$status) {
6616 if (!defined('RESTORE_SILENTLY')) {
6617 error ("An error has ocurred");
6618 } else {
6619 $errorstr = "An error has occured"; // helpful! :P
6620 return false;
6623 return true;
6626 function restore_setup_for_check(&$restore,$backup_unique_code) {
6627 global $SESSION;
6628 $restore->backup_unique_code=$backup_unique_code;
6629 $restore->users = 2; // yuk
6630 $restore->course_files = $SESSION->restore->restore_course_files;
6631 $restore->site_files = $SESSION->restore->restore_site_files;
6632 if ($allmods = get_records("modules")) {
6633 foreach ($allmods as $mod) {
6634 $modname = $mod->name;
6635 $var = "restore_".$modname;
6636 //Now check that we have that module info in the backup file
6637 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
6638 $restore->$var = 1;
6642 return true;
6645 function backup_to_restore_array($backup,$k=0) {
6646 if (is_array($backup) ) {
6647 foreach ($backup as $key => $value) {
6648 $newkey = str_replace('backup','restore',$key);
6649 $restore[$newkey] = backup_to_restore_array($value,$key);
6652 else if (is_object($backup)) {
6653 $tmp = get_object_vars($backup);
6654 foreach ($tmp as $key => $value) {
6655 $newkey = str_replace('backup','restore',$key);
6656 $restore->$newkey = backup_to_restore_array($value,$key);
6659 else {
6660 $newkey = str_replace('backup','restore',$k);
6661 $restore = $backup;
6663 return $restore;
6667 * compatibility function
6668 * checks for per-instance backups AND
6669 * older per-module backups
6670 * and returns whether userdata has been selected.
6672 function restore_userdata_selected($restore,$modname,$modid) {
6673 // check first for per instance array
6674 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
6675 return array_key_exists($modid,$restore->mods[$modname]->instances)
6676 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
6679 print_object($restore->mods[$modname]);
6680 return !empty($restore->mods[$modname]->userinfo);
6683 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
6685 global $CFG, $USER;
6686 $status = true;
6688 //Checks for the required files/functions to restore every module
6689 //and include them
6690 if ($allmods = get_records("modules") ) {
6691 foreach ($allmods as $mod) {
6692 $modname = $mod->name;
6693 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
6694 //If file exists and we have selected to restore that type of module
6695 if ((file_exists($modfile)) and !empty($restore->mods[$modname]) and ($restore->mods[$modname]->restore)) {
6696 include_once($modfile);
6701 if (!defined('RESTORE_SILENTLY')) {
6702 //Start the main table
6703 echo "<table cellpadding=\"5\">";
6704 echo "<tr><td>";
6706 //Start the main ul
6707 echo "<ul>";
6710 //Localtion of the xml file
6711 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
6713 //If we've selected to restore into new course
6714 //create it (course)
6715 //Saving conversion id variables into backup_tables
6716 if ($restore->restoreto == 2) {
6717 if (!defined('RESTORE_SILENTLY')) {
6718 echo '<li>'.get_string('creatingnewcourse') . '</li>';
6720 $oldidnumber = $course_header->course_idnumber;
6721 if (!$status = restore_create_new_course($restore,$course_header)) {
6722 if (!defined('RESTORE_SILENTLY')) {
6723 notify("Error while creating the new empty course.");
6724 } else {
6725 $errorstr = "Error while creating the new empty course.";
6726 return false;
6730 //Print course fullname and shortname and category
6731 if ($status) {
6732 if (!defined('RESTORE_SILENTLY')) {
6733 echo "<ul>";
6734 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
6735 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
6736 if (!empty($oldidnumber)) {
6737 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
6739 echo "</ul>";
6740 //Put the destination course_id
6742 $restore->course_id = $course_header->course_id;
6745 if ($status = restore_open_html($restore,$course_header)){
6746 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
6749 } else {
6750 $course = get_record("course","id",$restore->course_id);
6751 if ($course) {
6752 if (!defined('RESTORE_SILENTLY')) {
6753 echo "<li>".get_string("usingexistingcourse");
6754 echo "<ul>";
6755 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
6756 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
6757 if (($restore->deleting)) {
6758 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
6759 } else {
6760 echo "<li>".get_string("addingdatatoexisting").'</li>';
6762 echo "</ul></li>";
6764 //If we have selected to restore deleting, we do it now.
6765 if ($restore->deleting) {
6766 if (!defined('RESTORE_SILENTLY')) {
6767 echo "<li>".get_string("deletingolddata").'</li>';
6769 $status = remove_course_contents($restore->course_id,false) and
6770 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
6771 if ($status) {
6772 //Now , this situation is equivalent to the "restore to new course" one (we
6773 //have a course record and nothing more), so define it as "to new course"
6774 $restore->restoreto = 2;
6775 } else {
6776 if (!defined('RESTORE_SILENTLY')) {
6777 notify("An error occurred while deleting some of the course contents.");
6778 } else {
6779 $errrostr = "An error occurred while deleting some of the course contents.";
6780 return false;
6784 } else {
6785 if (!defined('RESTORE_SILENTLY')) {
6786 notify("Error opening existing course.");
6787 $status = false;
6788 } else {
6789 $errorstr = "Error opening existing course.";
6790 return false;
6795 //Now create users as needed
6796 if ($status and ($restore->users == 0 or $restore->users == 1)) {
6797 if (!defined('RESTORE_SILENTLY')) {
6798 echo "<li>".get_string("creatingusers")."<br />";
6800 if (!$status = restore_create_users($restore,$xml_file)) {
6801 if (!defined('RESTORE_SILENTLY')) {
6802 notify("Could not restore users.");
6803 } else {
6804 $errorstr = "Could not restore users.";
6805 return false;
6809 //Now print info about the work done
6810 if ($status) {
6811 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
6812 where backup_code = '$restore->backup_unique_code' and
6813 table_name = 'user'");
6814 //We've records
6815 if ($recs) {
6816 $new_count = 0;
6817 $exists_count = 0;
6818 $student_count = 0;
6819 $teacher_count = 0;
6820 $counter = 0;
6821 //Iterate, filling counters
6822 foreach ($recs as $rec) {
6823 //Get full record, using backup_getids
6824 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
6825 if (strpos($record->info,"new") !== false) {
6826 $new_count++;
6828 if (strpos($record->info,"exists") !== false) {
6829 $exists_count++;
6831 if (strpos($record->info,"student") !== false) {
6832 $student_count++;
6833 } else if (strpos($record->info,"teacher") !== false) {
6834 $teacher_count++;
6836 //Do some output
6837 $counter++;
6838 if ($counter % 10 == 0) {
6839 if (!defined('RESTORE_SILENTLY')) {
6840 echo ".";
6841 if ($counter % 200 == 0) {
6842 echo "<br />";
6845 backup_flush(300);
6848 if (!defined('RESTORE_SILENTLY')) {
6849 //Now print information gathered
6850 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
6851 echo "<ul>";
6852 echo "<li>".get_string("students").": ".$student_count.'</li>';
6853 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
6854 echo "</ul>";
6856 } else {
6857 if (!defined('RESTORE_SILENTLY')) {
6858 notify("No users were found!");
6859 } // no need to return false here, it's recoverable.
6863 if (!defined('RESTORE_SILENTLY')) {
6864 echo "</li>";
6869 //Now create groups as needed
6870 if ($status) {
6871 if (!defined('RESTORE_SILENTLY')) {
6872 echo "<li>".get_string("creatinggroups");
6874 if (!$status = restore_create_groups($restore,$xml_file)) {
6875 if (!defined('RESTORE_SILENTLY')) {
6876 notify("Could not restore groups!");
6877 } else {
6878 $errorstr = "Could not restore groups!";
6879 return false;
6882 if (!defined('RESTORE_SILENTLY')) {
6883 echo '</li>';
6887 //Now create groupings as needed
6888 if ($status) {
6889 if (!defined('RESTORE_SILENTLY')) {
6890 echo "<li>".get_string("creatinggroupings");
6892 if (!$status = restore_create_groupings($restore,$xml_file)) {
6893 if (!defined('RESTORE_SILENTLY')) {
6894 notify("Could not restore groupings!");
6895 } else {
6896 $errorstr = "Could not restore groupings!";
6897 return false;
6900 if (!defined('RESTORE_SILENTLY')) {
6901 echo '</li>';
6905 //Now create groupingsgroups as needed
6906 if ($status) {
6907 if (!defined('RESTORE_SILENTLY')) {
6908 echo "<li>".get_string("creatinggroupingsgroups");
6910 if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
6911 if (!defined('RESTORE_SILENTLY')) {
6912 notify("Could not restore groups in groupings!");
6913 } else {
6914 $errorstr = "Could not restore groups in groupings!";
6915 return false;
6918 if (!defined('RESTORE_SILENTLY')) {
6919 echo '</li>';
6924 //Now create the course_sections and their associated course_modules
6925 //we have to do this after groups and groupings are restored, because we need the new groupings id
6926 if ($status) {
6927 //Into new course
6928 if ($restore->restoreto == 2) {
6929 if (!defined('RESTORE_SILENTLY')) {
6930 echo "<li>".get_string("creatingsections");
6932 if (!$status = restore_create_sections($restore,$xml_file)) {
6933 if (!defined('RESTORE_SILENTLY')) {
6934 notify("Error creating sections in the existing course.");
6935 } else {
6936 $errorstr = "Error creating sections in the existing course.";
6937 return false;
6940 if (!defined('RESTORE_SILENTLY')) {
6941 echo '</li>';
6943 //Into existing course
6944 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
6945 if (!defined('RESTORE_SILENTLY')) {
6946 echo "<li>".get_string("checkingsections");
6948 if (!$status = restore_create_sections($restore,$xml_file)) {
6949 if (!defined('RESTORE_SILENTLY')) {
6950 notify("Error creating sections in the existing course.");
6951 } else {
6952 $errorstr = "Error creating sections in the existing course.";
6953 return false;
6956 if (!defined('RESTORE_SILENTLY')) {
6957 echo '</li>';
6959 //Error
6960 } else {
6961 if (!defined('RESTORE_SILENTLY')) {
6962 notify("Neither a new course or an existing one was specified.");
6963 $status = false;
6964 } else {
6965 $errorstr = "Neither a new course or an existing one was specified.";
6966 return false;
6971 //Now create metacourse info
6972 if ($status and $restore->metacourse) {
6973 //Only to new courses!
6974 if ($restore->restoreto == 2) {
6975 if (!defined('RESTORE_SILENTLY')) {
6976 echo "<li>".get_string("creatingmetacoursedata");
6978 if (!$status = restore_create_metacourse($restore,$xml_file)) {
6979 if (!defined('RESTORE_SILENTLY')) {
6980 notify("Error creating metacourse in the course.");
6981 } else {
6982 $errorstr = "Error creating metacourse in the course.";
6983 return false;
6986 if (!defined('RESTORE_SILENTLY')) {
6987 echo '</li>';
6993 //Now create categories and questions as needed
6994 if ($status) {
6995 include_once("$CFG->dirroot/question/restorelib.php");
6996 if (!defined('RESTORE_SILENTLY')) {
6997 echo "<li>".get_string("creatingcategoriesandquestions");
6998 echo "<ul>";
7000 if (!$status = restore_create_questions($restore,$xml_file)) {
7001 if (!defined('RESTORE_SILENTLY')) {
7002 notify("Could not restore categories and questions!");
7003 } else {
7004 $errorstr = "Could not restore categories and questions!";
7005 return false;
7008 if (!defined('RESTORE_SILENTLY')) {
7009 echo "</ul></li>";
7013 //Now create user_files as needed
7014 if ($status and ($restore->user_files)) {
7015 if (!defined('RESTORE_SILENTLY')) {
7016 echo "<li>".get_string("copyinguserfiles");
7018 if (!$status = restore_user_files($restore)) {
7019 if (!defined('RESTORE_SILENTLY')) {
7020 notify("Could not restore user files!");
7021 } else {
7022 $errorstr = "Could not restore user files!";
7023 return false;
7026 //If all is ok (and we have a counter)
7027 if ($status and ($status !== true)) {
7028 //Inform about user dirs created from backup
7029 if (!defined('RESTORE_SILENTLY')) {
7030 echo "<ul>";
7031 echo "<li>".get_string("userzones").": ".$status;
7032 echo "</li></ul>";
7035 if (!defined('RESTORE_SILENTLY')) {
7036 echo '</li>';
7040 //Now create course files as needed
7041 if ($status and ($restore->course_files)) {
7042 if (!defined('RESTORE_SILENTLY')) {
7043 echo "<li>".get_string("copyingcoursefiles");
7045 if (!$status = restore_course_files($restore)) {
7046 if (empty($status)) {
7047 notify("Could not restore course files!");
7048 } else {
7049 $errorstr = "Could not restore course files!";
7050 return false;
7053 //If all is ok (and we have a counter)
7054 if ($status and ($status !== true)) {
7055 //Inform about user dirs created from backup
7056 if (!defined('RESTORE_SILENTLY')) {
7057 echo "<ul>";
7058 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7059 echo "</ul>";
7062 if (!defined('RESTORE_SILENTLY')) {
7063 echo "</li>";
7068 //Now create site files as needed
7069 if ($status and ($restore->site_files)) {
7070 if (!defined('RESTORE_SILENTLY')) {
7071 echo "<li>".get_string('copyingsitefiles');
7073 if (!$status = restore_site_files($restore)) {
7074 if (empty($status)) {
7075 notify("Could not restore site files!");
7076 } else {
7077 $errorstr = "Could not restore site files!";
7078 return false;
7081 //If all is ok (and we have a counter)
7082 if ($status and ($status !== true)) {
7083 //Inform about user dirs created from backup
7084 if (!defined('RESTORE_SILENTLY')) {
7085 echo "<ul>";
7086 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7087 echo "</ul>";
7090 if (!defined('RESTORE_SILENTLY')) {
7091 echo "</li>";
7095 //Now create messages as needed
7096 if ($status and ($restore->messages)) {
7097 if (!defined('RESTORE_SILENTLY')) {
7098 echo "<li>".get_string("creatingmessagesinfo");
7100 if (!$status = restore_create_messages($restore,$xml_file)) {
7101 if (!defined('RESTORE_SILENTLY')) {
7102 notify("Could not restore messages!");
7103 } else {
7104 $errorstr = "Could not restore messages!";
7105 return false;
7108 if (!defined('RESTORE_SILENTLY')) {
7109 echo "</li>";
7113 //Now create scales as needed
7114 if ($status) {
7115 if (!defined('RESTORE_SILENTLY')) {
7116 echo "<li>".get_string("creatingscales");
7118 if (!$status = restore_create_scales($restore,$xml_file)) {
7119 if (!defined('RESTORE_SILENTLY')) {
7120 notify("Could not restore custom scales!");
7121 } else {
7122 $errorstr = "Could not restore custom scales!";
7123 return false;
7126 if (!defined('RESTORE_SILENTLY')) {
7127 echo '</li>';
7131 //Now create events as needed
7132 if ($status) {
7133 if (!defined('RESTORE_SILENTLY')) {
7134 echo "<li>".get_string("creatingevents");
7136 if (!$status = restore_create_events($restore,$xml_file)) {
7137 if (!defined('RESTORE_SILENTLY')) {
7138 notify("Could not restore course events!");
7139 } else {
7140 $errorstr = "Could not restore course events!";
7141 return false;
7144 if (!defined('RESTORE_SILENTLY')) {
7145 echo '</li>';
7149 //Now create course modules as needed
7150 if ($status) {
7151 if (!defined('RESTORE_SILENTLY')) {
7152 echo "<li>".get_string("creatingcoursemodules");
7154 if (!$status = restore_create_modules($restore,$xml_file)) {
7155 if (!defined('RESTORE_SILENTLY')) {
7156 notify("Could not restore modules!");
7157 } else {
7158 $errorstr = "Could not restore modules!";
7159 return false;
7162 if (!defined('RESTORE_SILENTLY')) {
7163 echo '</li>';
7167 //Now create gradebook as needed -- AFTER modules!!!
7168 if ($status) {
7169 if (!defined('RESTORE_SILENTLY')) {
7170 echo "<li>".get_string("creatinggradebook");
7172 if (!$status = restore_create_gradebook($restore,$xml_file)) {
7173 if (!defined('RESTORE_SILENTLY')) {
7174 notify("Could not restore gradebook!");
7175 } else {
7176 $errorstr = "Could not restore gradebook!";
7177 return false;
7180 if (!defined('RESTORE_SILENTLY')) {
7181 echo '</li>';
7185 //Bring back the course blocks -- do it AFTER the modules!!!
7186 if($status) {
7187 //If we are deleting and bringing into a course or making a new course, same situation
7188 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7189 if (!defined('RESTORE_SILENTLY')) {
7190 echo '<li>'.get_string('creatingblocks');
7192 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
7193 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
7194 if (!defined('RESTORE_SILENTLY')) {
7195 notify('Error while creating the course blocks');
7196 } else {
7197 $errorstr = "Error while creating the course blocks";
7198 return false;
7201 if (!defined('RESTORE_SILENTLY')) {
7202 echo '</li>';
7207 if($status) {
7208 //If we are deleting and bringing into a course or making a new course, same situation
7209 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7210 if (!defined('RESTORE_SILENTLY')) {
7211 echo '<li>'.get_string('courseformatdata');
7213 if (!$status = restore_set_format_data($restore, $xml_file)) {
7214 $error = "Error while setting the course format data";
7215 if (!defined('RESTORE_SILENTLY')) {
7216 notify($error);
7217 } else {
7218 $errorstr=$error;
7219 return false;
7222 if (!defined('RESTORE_SILENTLY')) {
7223 echo '</li>';
7228 //Now create log entries as needed
7229 if ($status and ($restore->logs)) {
7230 if (!defined('RESTORE_SILENTLY')) {
7231 echo "<li>".get_string("creatinglogentries");
7233 if (!$status = restore_create_logs($restore,$xml_file)) {
7234 if (!defined('RESTORE_SILENTLY')) {
7235 notify("Could not restore logs!");
7236 } else {
7237 $errorstr = "Could not restore logs!";
7238 return false;
7241 if (!defined('RESTORE_SILENTLY')) {
7242 echo '</li>';
7246 //Now, if all is OK, adjust the instance field in course_modules !!
7247 if ($status) {
7248 if (!defined('RESTORE_SILENTLY')) {
7249 echo "<li>".get_string("checkinginstances");
7251 if (!$status = restore_check_instances($restore)) {
7252 if (!defined('RESTORE_SILENTLY')) {
7253 notify("Could not adjust instances in course_modules!");
7254 } else {
7255 $errorstr = "Could not adjust instances in course_modules!";
7256 return false;
7259 if (!defined('RESTORE_SILENTLY')) {
7260 echo '</li>';
7264 //Now, if all is OK, adjust activity events
7265 if ($status) {
7266 if (!defined('RESTORE_SILENTLY')) {
7267 echo "<li>".get_string("refreshingevents");
7269 if (!$status = restore_refresh_events($restore)) {
7270 if (!defined('RESTORE_SILENTLY')) {
7271 notify("Could not refresh events for activities!");
7272 } else {
7273 $errorstr = "Could not refresh events for activities!";
7274 return false;
7277 if (!defined('RESTORE_SILENTLY')) {
7278 echo '</li>';
7282 //Now, if all is OK, adjust inter-activity links
7283 if ($status) {
7284 if (!defined('RESTORE_SILENTLY')) {
7285 echo "<li>".get_string("decodinginternallinks");
7287 if (!$status = restore_decode_content_links($restore)) {
7288 if (!defined('RESTORE_SILENTLY')) {
7289 notify("Could not decode content links!");
7290 } else {
7291 $errorstr = "Could not decode content links!";
7292 return false;
7295 if (!defined('RESTORE_SILENTLY')) {
7296 echo '</li>';
7300 //Now, with backup files prior to version 2005041100,
7301 //convert all the wiki texts in the course to markdown
7302 if ($status && $restore->backup_version < 2005041100) {
7303 if (!defined('RESTORE_SILENTLY')) {
7304 echo "<li>".get_string("convertingwikitomarkdown");
7306 if (!$status = restore_convert_wiki2markdown($restore)) {
7307 if (!defined('RESTORE_SILENTLY')) {
7308 notify("Could not convert wiki texts to markdown!");
7309 } else {
7310 $errorstr = "Could not convert wiki texts to markdown!";
7311 return false;
7314 if (!defined('RESTORE_SILENTLY')) {
7315 echo '</li>';
7319 /*******************************************************************************
7320 ************* Restore of Roles and Capabilities happens here ******************
7321 *******************************************************************************/
7322 $status = restore_create_roles($restore, $xml_file);
7323 $status = restore_roles_settings($restore, $xml_file);
7325 //Now if all is OK, update:
7326 // - course modinfo field
7327 // - categories table
7328 // - add user as teacher
7329 if ($status) {
7330 if (!defined('RESTORE_SILENTLY')) {
7331 echo "<li>".get_string("checkingcourse");
7333 //modinfo field
7334 rebuild_course_cache($restore->course_id);
7335 //categories table
7336 $course = get_record("course","id",$restore->course_id);
7337 fix_course_sortorder();
7338 // Check if the user has course update capability in the newly restored course
7339 // there is no need to load his capabilities again, because restore_roles_settings
7340 // would have loaded it anyway, if there is any assignments.
7341 // fix for MDL-6831
7342 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
7343 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
7344 // fix for MDL-9065, use the new config setting if exists
7345 if ($CFG->creatornewroleid) {
7346 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
7347 } else {
7348 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID))) {
7349 if ($legacyteacher = array_shift($legacyteachers)) {
7350 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
7352 } else {
7353 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
7357 if (!defined('RESTORE_SILENTLY')) {
7358 echo '</li>';
7362 //Cleanup temps (files and db)
7363 if ($status) {
7364 if (!defined('RESTORE_SILENTLY')) {
7365 echo "<li>".get_string("cleaningtempdata");
7367 if (!$status = clean_temp_data ($restore)) {
7368 if (!defined('RESTORE_SILENTLY')) {
7369 notify("Could not clean up temporary data from files and database");
7370 } else {
7371 $errorstr = "Could not clean up temporary data from files and database";
7372 return false;
7375 if (!defined('RESTORE_SILENTLY')) {
7376 echo '</li>';
7380 if ($status = restore_close_html($restore)){
7381 if (!defined('RESTORE_SILENTLY')) {
7382 echo '<li>Closing the Restorelog.html file.</li>';
7385 else {
7386 if (!defined('RESTORE_SILENTLY')) {
7387 notify("Could not close the restorelog.html file");
7389 else {
7390 $errorstr = "Could not close the restorelog.html file";
7391 return false;
7395 if (!defined('RESTORE_SILENTLY')) {
7396 //End the main ul
7397 echo "</ul>";
7399 //End the main table
7400 echo "</td></tr>";
7401 echo "</table>";
7404 return $status;
7406 //Create, open and write header of the html log file
7407 function restore_open_html($restore,$course_header) {
7409 global $CFG;
7411 $status = true;
7413 //Open file for writing
7414 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
7415 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
7416 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
7418 $status = check_dir_exists($dest_dir,true);
7419 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
7420 //Add the stylesheet
7421 $stylesheetshtml = '';
7422 foreach ($CFG->stylesheets as $stylesheet) {
7423 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
7425 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
7426 $languagehtml = get_html_lang($dir=true);
7428 //Write the header in the new logging file
7429 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
7430 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
7431 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
7432 fwrite ($restorelog_file,"<head>");
7433 fwrite ($restorelog_file,$stylesheetshtml);
7434 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
7435 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
7436 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
7437 $startdate = addslashes($course_header->course_startdate);
7438 $date = usergetdate($startdate);
7439 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
7440 $startdate += $restore->course_startdateoffset;
7441 $date = usergetdate($startdate);
7442 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
7444 if ($status) {
7445 return $restorelog_file;
7446 } else {
7447 return false;
7450 //Create & close footer of the html log file
7451 function restore_close_html($restore) {
7453 global $CFG;
7455 $status = true;
7457 //Open file for writing
7458 //First, check that course_id/backupdata folder exists in CFG->dataroot
7459 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
7460 $status = check_dir_exists($dest_dir, true, true);
7461 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
7462 //Write the footer to close the logging file
7463 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
7464 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
7466 if ($status) {
7467 return $restorelog_file;
7468 } else {
7469 return false;
7473 /********************** Roles and Capabilities Related Functions *******************************/
7475 /* Yu: Note recovering of role assignments/overrides need to take place after
7476 users have been recovered, i.e. after we get their new_id, and after all
7477 roles have been recreated or mapped. Contexts can be created on the fly.
7478 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
7479 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
7480 once roles and users have been restored.
7484 * This function restores all the needed roles for this course
7485 * i.e. roles with an assignment in any of the mods or blocks,
7486 * roles assigned on any user (e.g. parent role) and roles
7487 * assigned at course levle
7488 * This function should check for duplicate roles first
7489 * It isn't now, just overwriting
7491 function restore_create_roles($restore, $xmlfile) {
7492 if (!defined('RESTORE_SILENTLY')) {
7493 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
7495 $info = restore_read_xml_roles($xmlfile);
7497 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
7499 // the following code creates new roles
7500 // but we could use more intelligent detection, and role mapping
7501 // get role mapping info from $restore
7502 $rolemappings = array();
7504 if (!empty($restore->rolesmapping)) {
7505 $rolemappings = $restore->rolesmapping;
7507 // $info->roles will be empty for backups pre 1.7
7508 if (isset($info->roles) && $info->roles) {
7510 foreach ($info->roles as $oldroleid=>$roledata) {
7511 if (empty($restore->rolesmapping)) {
7512 // if this is empty altogether, we came from import or there's no roles used in course at all
7513 // in this case, write the same oldid as this is the same site
7514 // no need to do mapping
7515 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
7516 $oldroleid); // adding a new id
7517 continue; // do not create additonal roles;
7519 // first we check if the roles are in the mappings
7520 // if so, we just do a mapping i.e. update oldids table
7521 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
7522 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
7523 $rolemappings[$oldroleid]); // adding a new id
7525 } else {
7527 // code to make new role name/short name if same role name or shortname exists
7528 $fullname = $roledata->name;
7529 $shortname = $roledata->shortname;
7530 $currentfullname = "";
7531 $currentshortname = "";
7532 $counter = 0;
7534 do {
7535 if ($counter) {
7536 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
7537 $suffixshort = "_".$counter;
7538 } else {
7539 $suffixfull = "";
7540 $suffixshort = "";
7542 $currentfullname = $fullname.$suffixfull;
7543 // Limit the size of shortname - database column accepts <= 15 chars
7544 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
7545 $coursefull = get_record("role","name",addslashes($currentfullname));
7546 $courseshort = get_record("role","shortname",addslashes($currentshortname));
7547 $counter++;
7548 } while ($coursefull || $courseshort);
7550 $roledata->name = $currentfullname;
7551 $roledata->shortname= $currentshortname;
7553 // done finding a unique name
7555 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
7556 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
7557 $newroleid); // adding a new id
7558 foreach ($roledata->capabilities as $capability) {
7560 $roleinfo = new object();
7561 $roleinfo = (object)$capability;
7562 $roleinfo->contextid = $sitecontext->id;
7563 $roleinfo->capability = $capability->name;
7564 $roleinfo->roleid = $newroleid;
7566 insert_record('role_capabilities', $roleinfo);
7571 return true;
7575 * this function restores role assignments and role overrides
7576 * in course/user/block/mod level, it passed through
7577 * the xml file again
7579 function restore_roles_settings($restore, $xmlfile) {
7580 // data pulls from course, mod, user, and blocks
7582 /*******************************************************
7583 * Restoring from course level assignments *
7584 *******************************************************/
7585 if (!defined('RESTORE_SILENTLY')) {
7586 echo "<li>".get_string("creatingcourseroles").'</li>';
7588 $course = restore_read_xml_course_header($xmlfile);
7590 if (!isset($restore->rolesmapping)) {
7591 $isimport = true; // course import from another course, or course with no role assignments
7592 } else {
7593 $isimport = false; // course restore with role assignments
7596 if (!empty($course->roleassignments) && !$isimport) {
7597 $courseassignments = $course->roleassignments;
7599 foreach ($courseassignments as $oldroleid => $courseassignment) {
7600 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
7603 /*****************************************************
7604 * Restoring from course level overrides *
7605 *****************************************************/
7607 if (!empty($course->roleoverrides) && !$isimport) {
7608 $courseoverrides = $course->roleoverrides;
7609 foreach ($courseoverrides as $oldroleid => $courseoverride) {
7610 // if not importing into exiting course, or creating new role, we are ok
7611 // local course overrides to be respected (i.e. restored course overrides ignored)
7612 if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
7613 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
7618 /*******************************************************
7619 * Restoring role assignments/overrdies *
7620 * from module level assignments *
7621 *******************************************************/
7623 if (!defined('RESTORE_SILENTLY')) {
7624 echo "<li>".get_string("creatingmodroles").'</li>';
7626 $sections = restore_read_xml_sections($xmlfile);
7627 $secs = $sections->sections;
7629 foreach ($secs as $section) {
7630 if (isset($section->mods)) {
7631 foreach ($section->mods as $modid=>$mod) {
7632 if (isset($mod->roleassignments) && !$isimport) {
7633 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
7634 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
7637 // role overrides always applies, in import or backup/restore
7638 if (isset($mod->roleoverrides)) {
7639 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
7640 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
7647 /*************************************************
7648 * Restoring assignments from blocks level *
7649 * role assignments/overrides *
7650 *************************************************/
7652 if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
7653 if (!defined('RESTORE_SILENTLY')) {
7654 echo "<li>".get_string("creatingblocksroles").'</li>';
7656 $blocks = restore_read_xml_blocks($xmlfile);
7657 if (isset($blocks->instances)) {
7658 foreach ($blocks->instances as $instance) {
7659 if (isset($instance->roleassignments) && !$isimport) {
7660 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
7661 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
7665 // likewise block overrides should always be restored like mods
7666 if (isset($instance->roleoverrides)) {
7667 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
7668 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
7674 /************************************************
7675 * Restoring assignments from userid level *
7676 * role assignments/overrides *
7677 ************************************************/
7678 if (!defined('RESTORE_SILENTLY')) {
7679 echo "<li>".get_string("creatinguserroles").'</li>';
7681 $info = restore_read_xml_users($restore, $xmlfile);
7682 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
7683 //For each user, take its info from backup_ids
7684 foreach ($info->users as $userid) {
7685 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
7686 if (isset($rec->info->roleassignments)) {
7687 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
7688 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
7691 if (isset($rec->info->roleoverrides)) {
7692 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
7693 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
7699 return true;
7702 // auxillary function to write role assignments read from xml to db
7703 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
7705 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
7707 foreach ($assignments as $assignment) {
7709 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
7710 //Oh dear, $olduser... can be an object, $obj->string or bool!
7711 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
7712 continue;
7714 $assignment->userid = $olduser->new_id; // new userid here
7715 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
7716 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
7717 $assignment->roleid = $role->new_id; // restored new role id
7719 // hack to make the correct contextid for course level imports
7720 if ($contextlevel == CONTEXT_COURSE) {
7721 $oldinstance->new_id = $restore->course_id;
7722 } else {
7723 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
7726 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
7727 $assignment->contextid = $newcontext->id; // new context id
7728 // might already have same assignment
7729 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol, $assignment->timemodified);
7734 // auxillary function to write role assignments read from xml to db
7735 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
7737 // it is possible to have an override not relevant to this course context.
7738 // should be ignored(?)
7739 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
7740 return null;
7743 foreach ($overrides as $override) {
7744 $override->capability = $override->name;
7745 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
7746 $override->modifierid = $oldmodifier->new_id?$oldmodifier->new_id:0; // new modifier id here
7747 $override->roleid = $role->new_id; // restored new role id
7749 // hack to make the correct contextid for course level imports
7750 if ($contextlevel == CONTEXT_COURSE) {
7751 $oldinstance->new_id = $restore->course_id;
7752 } else {
7753 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
7756 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
7757 $override->contextid = $newcontext->id; // new context id
7758 // use assign capability instead so we can add context to context_rel
7759 assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
7762 //write activity date changes to the html log file, and update date values in the the xml array
7763 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
7765 global $CFG;
7766 $openlog = false;
7768 // loop through time fields in $TAGS
7769 foreach ($TAGS as $TAG) {
7771 // check $TAG has a sensible value
7772 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
7774 if ($openlog==false) {
7775 $openlog = true; // only come through here once
7777 // open file for writing
7778 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
7779 check_dir_exists($course_dir, true);
7780 $restorelog = fopen("$course_dir/restorelog.html", "a");
7782 // start output for this record
7783 $msg = new stdClass();
7784 $msg->recordtype = $recordtype;
7785 $msg->recordname = $xml[$NAMETAG][0]['#'];
7786 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
7789 // write old date to $restorelog
7790 $value = $xml[$TAG][0]['#'];
7791 $date = usergetdate($value);
7793 $msg = new stdClass();
7794 $msg->TAG = $TAG;
7795 $msg->weekday = $date['weekday'];
7796 $msg->mday = $date['mday'];
7797 $msg->month = $date['month'];
7798 $msg->year = $date['year'];
7799 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
7801 // write new date to $restorelog
7802 $value += $restore->course_startdateoffset;
7803 $date = usergetdate($value);
7805 $msg = new stdClass();
7806 $msg->TAG = $TAG;
7807 $msg->weekday = $date['weekday'];
7808 $msg->mday = $date['mday'];
7809 $msg->month = $date['month'];
7810 $msg->year = $date['year'];
7811 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
7813 // update $value in $xml tree for calling module
7814 $xml[$TAG][0]['#'] = "$value";
7817 // close the restore log, if it was opened
7818 if ($openlog) {
7819 fclose($restorelog);