MDL-11104
[moodle-linuxchix.git] / backup / restorelib.php
blobae6e34054d05e7f2a2ec294619d350a44eecac74
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 (isset($info->backup_site_files) && $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, $SESSION;
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")) {
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>';
1247 // fetch the course grade item
1249 require_once($CFG->libdir.'/grade/grade_item.php');
1250 require_once($CFG->libdir.'/grade/grade_category.php');
1251 require_once($CFG->libdir.'/gradelib.php');
1252 $courseitem = grade_item::fetch_course_item($restore->course_id);
1253 $coursecategory = grade_category::fetch_course_category($restore->course_id);
1255 // Number of records to get in every chunk
1256 $recordset_size = 2;
1257 // Flag to mark if we must continue
1258 $continue = true;
1260 // Process categories
1261 if ($categoriescount && $continue && $restoreall) {
1262 if (!defined('RESTORE_SILENTLY')) {
1263 echo '<li>'.get_string('gradecategories','grades').'</li>';
1265 $counter = 0;
1266 while ($counter < $categoriescount) {
1267 // Fetch recordset_size records in each iteration
1268 $recs = get_records_select("backup_ids","table_name = 'grade_categories' AND backup_code = '$restore->backup_unique_code'",
1269 "old_id",
1270 "old_id",
1271 $counter,
1272 $recordset_size);
1273 if ($recs) {
1274 foreach ($recs as $rec) {
1275 // Get the full record from backup_ids
1276 $data = backup_getid($restore->backup_unique_code,'grade_categories',$rec->old_id);
1277 if ($data) {
1278 // Now get completed xmlized object
1279 $info = $data->info;
1280 //traverse_xmlize($info); //Debug
1281 //print_object ($GLOBALS['traverse_array']); //Debug
1282 //$GLOBALS['traverse_array']=""; //Debug
1283 //Now build the GRADE_PREFERENCES record structure
1285 $dbrec->courseid = $restore->course_id;
1286 // categories are not backed up during import.
1287 // however, depth 1 categories needs to be skipped during restore into exisiting course
1289 // get the new grade category parent
1291 //if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']) && $info['GRADE_CATEGORY']['#']['PARENT']['0']['#'] != '$@NULL@$') {
1293 $parent = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
1294 if (isset($parent->new_id)) {
1295 $dbrec->parent = $parent->new_id;
1296 } else {
1297 // orphans should get adopted by course category
1298 $dbrec->parent = $coursecategory->id;
1302 $dbrec->fullname = backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
1303 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
1304 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
1305 $dbrec->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROPLOW']['0']['#']);
1306 $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
1308 //Structure is equal to db, insert record
1309 //if the fullname doesn't exist
1310 if (!$prerec = get_record('grade_categories','courseid',$dbrec->courseid,'fullname',$dbrec->fullname)) {
1311 $newid = insert_record('grade_categories',$dbrec);
1312 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$newid);
1313 // update this record so we can put in the right paths
1314 // this can only be done after we got the new id
1315 $dbrec->id = $newid;
1316 include_once($CFG->dirroot.'/lib/grade/grade_category.php');
1317 // rebuild the path, we need only parents info
1318 // the order of restoring should ensure that the parent and grandparent(s)
1319 // are already restored
1320 $dbrec->path = grade_category::build_path($dbrec);
1321 // this is not needed in the xml because
1322 // given this parent and grandparent(s) we can recalculate the depth
1323 $dbrec->depth = substr_count($dbrec->path, '/');
1324 update_record('grade_categories', $dbrec);
1325 } else {
1326 // if fullname already exists, we should keep the current grade category
1327 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$rec->oldid);
1330 //Increment counters
1331 $counter++;
1332 //Do some output
1333 if ($counter % 1 == 0) {
1334 if (!defined('RESTORE_SILENTLY')) {
1335 echo ".";
1336 if ($counter % 20 == 0) {
1337 echo "<br />";
1340 backup_flush(300);
1347 // process outcomes
1348 if ($outcomecount && $continue) {
1349 if (!defined('RESTORE_SILENTLY')) {
1350 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1352 $counter = 0;
1353 while ($counter < $outcomecount) {
1354 //Fetch recordset_size records in each iteration
1355 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1356 "old_id",
1357 "old_id",
1358 $counter,
1359 $recordset_size);
1360 if ($recs) {
1361 foreach ($recs as $rec) {
1362 //Get the full record from backup_ids
1363 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1364 if ($data) {
1365 //Now get completed xmlized object
1366 $info = $data->info;
1367 //traverse_xmlize($info); //Debug
1368 //print_object ($GLOBALS['traverse_array']); //Debug
1369 //$GLOBALS['traverse_array']=""; //Debug
1370 //Now build the GRADE_PREFERENCES record structure
1371 if ($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']) {
1372 $dbrec->courseid = $restore->course_id;
1373 } else {
1374 $dbrec->courseid = NULL;
1376 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1377 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#']);
1379 if ($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']) {
1380 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']));
1381 $dbrec->scaleid = $scale->new_id;
1384 $modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#']));
1385 $dbrec->usermodified = $modifier->new_id;
1387 // Structure is equal to db, insert record
1388 // If the shortname doesn't exist
1390 if (empty($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#'])) {
1391 $prerec = get_record_sql("SELECT * FROM {$CFG->prefix}grade_outcomes
1392 WHERE courseid IS NULL
1393 AND shortname = '$dbrec->shortname'");
1394 } else {
1395 $prerec = get_record('grade_outcomes','courseid',$restore->course_id,'shortname',$dbrec->shortname);
1398 if (!$prerec) {
1399 $newid = insert_record('grade_outcomes',$dbrec);
1400 } else {
1401 $newid = $prerec->id;
1404 if ($newid) {
1405 backup_putid($restore->backup_unique_code,"grade_outcomes", $rec->old_id, $newid);
1408 //Increment counters
1409 $counter++;
1410 //Do some output
1411 if ($counter % 1 == 0) {
1412 if (!defined('RESTORE_SILENTLY')) {
1413 echo ".";
1414 if ($counter % 20 == 0) {
1415 echo "<br />";
1418 backup_flush(300);
1425 // process outcomescourses
1426 if ($outcomescoursescount && $continue) {
1427 if (!defined('RESTORE_SILENTLY')) {
1428 echo '<li>'.get_string('gradeoutcomescourses','grades').'</li>';
1430 $counter = 0;
1431 while ($counter < $outcomescoursescount) {
1432 //Fetch recordset_size records in each iteration
1433 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_courses' AND backup_code = '$restore->backup_unique_code'",
1434 "old_id",
1435 "old_id",
1436 $counter,
1437 $recordset_size);
1438 if ($recs) {
1439 foreach ($recs as $rec) {
1440 //Get the full record from backup_ids
1441 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_courses',$rec->old_id);
1442 if ($data) {
1443 //Now get completed xmlized object
1444 $info = $data->info;
1445 //traverse_xmlize($info); //Debug
1446 //print_object ($GLOBALS['traverse_array']); //Debug
1447 //$GLOBALS['traverse_array']=""; //Debug
1449 $oldoutcomesid = backup_todb($info['GRADE_OUTCOMES_COURSE']['#']['OUTCOMEID']['0']['#']);
1450 $newoutcome = backup_getid($restore->backup_unique_code,"grade_outcomes",$oldoutcomesid);
1451 unset($dbrec);
1452 $dbrec->courseid = $restore->course_id;
1453 $dbrec->outcomeid = $newoutcome->new_id;
1454 insert_record('grade_outcomes_courses', $dbrec);
1456 //Increment counters
1457 $counter++;
1458 //Do some output
1459 if ($counter % 1 == 0) {
1460 if (!defined('RESTORE_SILENTLY')) {
1461 echo ".";
1462 if ($counter % 20 == 0) {
1463 echo "<br />";
1466 backup_flush(300);
1473 // Process grade items (grade_raw, grade_final, and grade_text)
1474 if ($itemscount && $continue) {
1475 if (!defined('RESTORE_SILENTLY')) {
1476 echo '<li>'.get_string('gradeitems','grades').'</li>';
1478 $counter = 0;
1479 $counteritems = 0;
1480 while ($counteritems < $itemscount) {
1482 //Fetch recordset_size records in each iteration
1483 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1484 "old_id",
1485 "old_id",
1486 $counteritems,
1487 $recordset_size);
1489 if ($recs) {
1490 foreach ($recs as $rec) {
1491 //Get the full record from backup_ids
1492 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
1493 if ($data) {
1494 //Now get completed xmlized object
1495 $info = $data->info;
1496 //traverse_xmlize($info); //Debug
1497 //print_object ($GLOBALS['traverse_array']); //Debug
1498 //$GLOBALS['traverse_array']=""; //Debug
1500 $dbrec->courseid = $restore->course_id;
1502 if (isset($SESSION->restore->importing)) {
1503 // if we are importing, points all grade_items to the course category
1504 $coursecat = get_record('grade_categories', 'courseid', $restore->course_id, 'depth', 1);
1505 $dbrec->categoryid = $coursecat->id;
1506 } else if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']) && $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']!='$@NULL@$') {
1507 $category = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
1508 if ($category->new_id) {
1509 $dbrec->categoryid = $category->new_id;
1510 } else {
1511 // this could be restoring into existing course, and grade item points to the old course grade item (category)
1512 // which was never imported. In this case we just point them to the new course item
1513 $dbrec->categoryid = $coursecategory->id;
1517 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
1518 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1519 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1520 /// this needs to point to either the new mod id
1521 /// or the category id
1522 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1523 // do not restore if this grade_item is a mod, and
1524 if ($dbrec->itemtype == 'mod') {
1526 // iteminstance should point to new mod
1528 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
1529 $dbrec->iteminstance = $mod->new_id;
1531 } else if ($dbrec->itemtype == 'category') {
1532 // the item instance should point to the new grade category
1534 // only proceed if we are restoring all grade items
1535 // need to skip for imports
1536 if ($restoreall && !isset($SESSION->restore->importing)) {
1537 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
1538 $dbrec->iteminstance = $category->new_id;
1539 } else {
1540 // otherwise we can safely ignore this grade item and subsequent
1541 // grade_raws, grade_finals etc
1542 $counteritems++;
1543 continue;
1545 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
1547 if ($restoreall && !isset($SESSION->restore->importing) && $restore->restoreto == 2) {
1548 // TODO any special code needed here to restore course item without duplicating it?
1549 // find the course category with depth 1, and course id = current course id
1550 // this would have been already restored
1551 $counteritems++;
1552 continue;
1553 } else {
1554 $counteritems++;
1555 continue;
1559 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
1560 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#']);
1561 $dbrec->idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#']);
1562 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#']);
1563 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#']);
1564 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#']);
1565 /// needs to be restored first
1567 if ($info['GRADE_ITEM']['#']['SCALEID']['0']['#']) {
1568 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#']));
1569 $dbrec->scaleid = $scale->new_id;
1572 /// needs to be restored first
1573 $dbrec->outcomeid = backup_getid($restore->backup_unique_code,"grade_outcomes",backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']));
1575 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#']);
1576 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
1577 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
1578 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);
1579 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);
1580 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);
1582 // get the current sortorder, add 1 to it and use that
1583 if ($lastitem = get_record_sql("SELECT sortorder, id FROM {$CFG->prefix}grade_items
1584 WHERE courseid = $restore->course_id
1585 ORDER BY sortorder DESC ", true)) {
1587 // we just need the first one
1588 $dbrec->sortorder = $lastitem->sortorder + 1;
1589 } else {
1590 // this is the first grade_item
1591 $dbrec->sortorder = 1;
1593 // always insert, since modules restored to existing courses are always inserted
1594 $itemid = insert_record('grade_items',$dbrec);
1595 if ($itemid) {
1596 backup_putid($restore->backup_unique_code,'grade_items', backup_todb($info['GRADE_ITEM']['#']['ID']['0']['#']), $itemid);
1599 // no need to restore grades/grades_text if user data is not selected
1600 if ($dbrec->itemtype == 'mod' && !restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
1601 // module instance not selected when restored using granular
1602 // skip this item
1603 $counteritems++;
1604 continue;
1607 /// now, restore grade_grades, grade_text
1608 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1609 //Iterate over items
1610 for($i = 0; $i < sizeof($grades); $i++) {
1611 $ite_info = $grades[$i];
1612 //traverse_xmlize($ite_info);
1613 //Debug
1614 //print_object ($GLOBALS['traverse_array']); //Debug
1615 //$GLOBALS['traverse_array']=""; //Debug
1616 //Now build the GRADE_ITEM record structure
1617 $grade = new object();
1618 $grade->itemid = $itemid;
1619 $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));
1620 $grade->userid = $user->new_id;
1621 $grade->rawgrade = backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
1622 $grade->rawgrademax = backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
1623 $grade->rawgrademin = backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
1624 // need to find scaleid
1625 if ($ite_info['#']['RAWSCALEID']['0']['#']) {
1626 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
1627 $grade->rawscaleid = $scale->new_id;
1629 $grade->finalgrade = backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
1630 $grade->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
1631 $grade->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
1632 $grade->locktime = backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
1633 $grade->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
1634 $grade->overridden = backup_todb($ite_info['#']['OVERRIDDEN']['0']['#']);
1635 $grade->excluded = backup_todb($ite_info['#']['EXCLUDED']['0']['#']);
1637 $newid = insert_record('grade_grades', $grade);
1639 if ($newid) {
1640 backup_putid($restore->backup_unique_code,"grade_grades", backup_todb($ite_info['#']['ID']['0']['#']), $newid);
1642 $counter++;
1643 if ($counter % 20 == 0) {
1644 if (!defined('RESTORE_SILENTLY')) {
1645 echo ".";
1646 if ($counter % 400 == 0) {
1647 echo "<br />";
1650 backup_flush(300);
1657 /// processing grade_grades_text
1658 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']) && ($texts = $info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']['GRADE_TEXT'])) {
1659 //Iterate over items
1660 for($i = 0; $i < sizeof($texts); $i++) {
1661 $ite_info = $texts[$i];
1662 //traverse_xmlize($ite_info); //Debug
1663 //print_object ($GLOBALS['traverse_array']); //Debug
1664 //$GLOBALS['traverse_array']=""; //Debug
1665 $grade = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($ite_info['#']['GRADEID']['0']['#']));
1667 $text = new object();
1668 $text->gradeid = $grade->new_id;
1669 $text->information = backup_todb($ite_info['#']['INFORMATION']['0']['#']);
1670 $text->informationformat = backup_todb($ite_info['#']['INFORMATIONFORMAT']['0']['#']);
1671 $text->feedback = backup_todb($ite_info['#']['FEEDBACK']['0']['#']);
1672 $text->feedbackformat = backup_todb($ite_info['#']['FEEDBACKFORMAT']['0']['#']);
1674 $newid = insert_record('grade_grades_text', $text);
1675 if ($newid) {
1676 backup_putid($restore->backup_unique_code,'grade_grades_text', backup_todb($ite_info['#']['ID']['0']['#']), $newid);
1678 $counter++;
1679 if ($counter % 20 == 0) {
1680 if (!defined('RESTORE_SILENTLY')) {
1681 echo ".";
1682 if ($counter % 400 == 0) {
1683 echo "<br />";
1686 backup_flush(300);
1691 $counteritems++; // increment item count
1698 // process histories
1699 if ($gchcount && $continue && !isset($SESSION->restore->importing)) {
1700 if (!defined('RESTORE_SILENTLY')) {
1701 echo '<li>'.get_string('gradecategoryhistory','grades').'</li>';
1703 $counter = 0;
1704 while ($counter < $gchcount) {
1705 //Fetch recordset_size records in each iteration
1706 $recs = get_records_select("backup_ids","table_name = 'grade_categories_history' AND backup_code = '$restore->backup_unique_code'",
1707 "old_id",
1708 "old_id",
1709 $counter,
1710 $recordset_size);
1711 if ($recs) {
1712 foreach ($recs as $rec) {
1713 //Get the full record from backup_ids
1714 $data = backup_getid($restore->backup_unique_code,'grade_categories_history',$rec->old_id);
1715 if ($data) {
1716 //Now get completed xmlized object
1717 $info = $data->info;
1718 //traverse_xmlize($info); //Debug
1719 //print_object ($GLOBALS['traverse_array']); //Debug
1720 //$GLOBALS['traverse_array']=""; //Debug
1722 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
1723 if (empty($oldobj->new_id)) {
1724 // if the old object is not being restored, can't restoring its history
1725 $counter++;
1726 continue;
1728 $dbrec->oldid = $oldobj->new_id;
1729 $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
1730 $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
1731 $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1733 // loggeduser might not be restored, e.g. admin
1734 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1735 $dbrec->loggeduser = $oldobj->new_id;
1738 // this item might not have a parent at all, do not skip it if no parent is specified
1739 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
1740 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
1741 if (empty($oldobj->new_id)) {
1742 // if the parent category not restored
1743 $counter++;
1744 continue;
1747 $dbrec->parent = $oldobj->new_id;
1748 $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
1749 // path needs to be rebuilt
1750 if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
1751 // to preserve the path and make it work, we need to replace the categories one by one
1752 // we first get the list of categories in current path
1753 if ($paths = explode("/", $path)) {
1754 $newpath = '';
1755 foreach ($paths as $catid) {
1756 if ($catid) {
1757 // find the new corresponding path
1758 $oldpath = backup_getid($restore->backup_unique_code,"grade_categories", $catid);
1759 $newpath .= "/$oldpath->new_id";
1762 $dbrec->path = $newpath;
1765 $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
1766 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
1767 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
1768 $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
1769 $dbrec->courseid = $restore->course_id;
1770 insert_record('grade_categories_history', $dbrec);
1771 unset($dbrec);
1774 //Increment counters
1775 $counter++;
1776 //Do some output
1777 if ($counter % 1 == 0) {
1778 if (!defined('RESTORE_SILENTLY')) {
1779 echo ".";
1780 if ($counter % 20 == 0) {
1781 echo "<br />";
1784 backup_flush(300);
1791 // process histories
1792 if ($gghcount && $continue && !isset($SESSION->restore->importing)) {
1793 if (!defined('RESTORE_SILENTLY')) {
1794 echo '<li>'.get_string('gradegradeshistory','grades').'</li>';
1796 $counter = 0;
1797 while ($counter < $gghcount) {
1798 //Fetch recordset_size records in each iteration
1799 $recs = get_records_select("backup_ids","table_name = 'grade_grades_history' AND backup_code = '$restore->backup_unique_code'",
1800 "old_id",
1801 "old_id",
1802 $counter,
1803 $recordset_size);
1804 if ($recs) {
1805 foreach ($recs as $rec) {
1806 //Get the full record from backup_ids
1807 $data = backup_getid($restore->backup_unique_code,'grade_grades_history',$rec->old_id);
1808 if ($data) {
1809 //Now get completed xmlized object
1810 $info = $data->info;
1811 //traverse_xmlize($info); //Debug
1812 //print_object ($GLOBALS['traverse_array']); //Debug
1813 //$GLOBALS['traverse_array']=""; //Debug
1815 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
1816 if (empty($oldobj->new_id)) {
1817 // if the old object is not being restored, can't restoring its history
1818 $counter++;
1819 continue;
1821 $dbrec->oldid = $oldobj->new_id;
1822 $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
1823 $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
1824 $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1825 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1826 $dbrec->loggeduser = $oldobj->new_id;
1828 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
1829 $dbrec->itemid = $oldobj->new_id;
1830 if (empty($dbrec->itemid)) {
1831 $counter++;
1832 continue; // grade item not being restored
1834 $oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
1835 $dbrec->userid = $oldobj->new_id;
1836 $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
1837 $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
1838 $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
1839 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
1840 $dbrec->usermodified = $oldobj->new_id;
1842 $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
1843 $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
1844 $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
1845 $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
1846 $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
1847 $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
1848 $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
1850 insert_record('grade_grades_history', $dbrec);
1851 unset($dbrec);
1854 //Increment counters
1855 $counter++;
1856 //Do some output
1857 if ($counter % 1 == 0) {
1858 if (!defined('RESTORE_SILENTLY')) {
1859 echo ".";
1860 if ($counter % 20 == 0) {
1861 echo "<br />";
1864 backup_flush(300);
1871 // process histories
1873 if ($ggthcount && $continue && !isset($SESSION->restore->importing)) {
1874 if (!defined('RESTORE_SILENTLY')) {
1875 echo '<li>'.get_string('gradegradestexthistory','grades').'</li>';
1877 $counter = 0;
1878 while ($counter < $ggthcount) {
1879 //Fetch recordset_size records in each iteration
1880 $recs = get_records_select("backup_ids","table_name = 'grade_grades_text_history' AND backup_code = '$restore->backup_unique_code'",
1881 "old_id",
1882 "old_id",
1883 $counter,
1884 $recordset_size);
1886 if ($recs) {
1887 foreach ($recs as $rec) {
1888 //Get the full record from backup_ids
1889 $data = backup_getid($restore->backup_unique_code,'grade_grades_text_history',$rec->old_id);
1890 if ($data) {
1891 //Now get completed xmlized object
1892 $info = $data->info;
1893 //traverse_xmlize($info); //Debug
1894 //print_object ($GLOBALS['traverse_array']); //Debug
1895 //$GLOBALS['traverse_array']=""; //Debug
1897 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades_text", backup_todb($info['GRADE_TEXT_HISTORY']['#']['OLDID']['0']['#']));
1898 if (empty($oldobj->new_id)) {
1899 // if the old object is not being restored, can't restoring its history
1900 $counter++;
1901 continue;
1903 $dbrec->oldid = $oldobj->new_id;
1904 $dbrec->action = backup_todb($info['GRADE_TEXT_HISTORY']['#']['ACTION']['0']['#']);
1905 $dbrec->source = backup_todb($info['GRADE_TEXT_HISTORY']['#']['SOURCE']['0']['#']);
1906 $dbrec->timemodified = backup_todb($info['GRADE_TEXT_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1907 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1908 $dbrec->loggeduser = $oldobj->new_id;
1910 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_TEXT_HISTORY']['#']['GRADEID']['0']['#']));
1911 $dbrec->gradeid = $oldobj->new_id;
1912 if (empty($dbrec->gradeid)) {
1913 $counter++;
1914 continue; // grade not being restore, possibly because grade item is not restored
1917 $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
1918 $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
1919 $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
1920 $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
1921 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_TEXT_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
1922 $dbrec->usermodified = $oldobj->new_id;
1925 insert_record('grade_grades_text_history', $dbrec);
1926 unset($dbrec);
1929 //Increment counters
1930 $counter++;
1931 //Do some output
1932 if ($counter % 1 == 0) {
1933 if (!defined('RESTORE_SILENTLY')) {
1934 echo ".";
1935 if ($counter % 20 == 0) {
1936 echo "<br />";
1939 backup_flush(300);
1946 // process histories
1947 if ($gihcount && $continue && !isset($SESSION->restore->importing)) {
1948 if (!defined('RESTORE_SILENTLY')) {
1949 echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
1951 $counter = 0;
1952 while ($counter < $gihcount) {
1953 //Fetch recordset_size records in each iteration
1954 $recs = get_records_select("backup_ids","table_name = 'grade_items_history' AND backup_code = '$restore->backup_unique_code'",
1955 "old_id",
1956 "old_id",
1957 $counter,
1958 $recordset_size);
1959 if ($recs) {
1960 foreach ($recs as $rec) {
1961 //Get the full record from backup_ids
1962 $data = backup_getid($restore->backup_unique_code,'grade_items_history',$rec->old_id);
1963 if ($data) {
1964 //Now get completed xmlized object
1965 $info = $data->info;
1966 //traverse_xmlize($info); //Debug
1967 //print_object ($GLOBALS['traverse_array']); //Debug
1968 //$GLOBALS['traverse_array']=""; //Debug
1971 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
1972 if (empty($oldobj->new_id)) {
1973 // if the old object is not being restored, can't restoring its history
1974 $counter++;
1975 continue;
1977 $dbrec->oldid = $oldobj->new_id;
1978 $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
1979 $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
1980 $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
1981 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
1982 $dbrec->loggeduser = $oldobj->new_id;
1984 $oldobj = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
1985 $oldobj->categoryid = $category->new_id;
1986 if (empty($oldobj->categoryid)) {
1987 $counter++;
1988 continue; // category not restored
1991 $dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
1992 $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
1993 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
1995 // code from grade_items restore
1996 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
1997 // do not restore if this grade_item is a mod, and
1998 if ($dbrec->itemtype == 'mod') {
2000 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
2001 // module instance not selected when restored using granular
2002 // skip this item
2003 $counter++;
2004 continue;
2007 // iteminstance should point to new mod
2009 $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
2010 $dbrec->iteminstance = $mod->new_id;
2012 } else if ($dbrec->itemtype == 'category') {
2013 // the item instance should point to the new grade category
2015 // only proceed if we are restoring all grade items
2016 if ($restoreall) {
2017 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
2018 $dbrec->iteminstance = $category->new_id;
2019 } else {
2020 // otherwise we can safely ignore this grade item and subsequent
2021 // grade_raws, grade_finals etc
2022 continue;
2024 } elseif ($dbrec->itemtype == 'course') { // We don't restore course type to avoid duplicate course items
2025 if ($restoreall) {
2026 // TODO any special code needed here to restore course item without duplicating it?
2027 // find the course category with depth 1, and course id = current course id
2028 // this would have been already restored
2030 $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
2031 $dbrec->iteminstance = $cat->id;
2033 } else {
2034 $counter++;
2035 continue;
2039 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
2040 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
2041 $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
2042 $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
2043 $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
2044 $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
2045 $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
2046 if ($oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
2047 // scaleid is optional
2048 $dbrec->scaleid = $oldobj->new_id;
2050 if ($oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
2051 // outcome is optional
2052 $dbrec->outcomeid = $oldobj->new_id;
2054 $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
2055 $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
2056 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
2057 $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
2058 $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
2059 $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
2060 $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
2061 $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
2062 $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
2064 insert_record('grade_items_history', $dbrec);
2065 unset($dbrec);
2068 //Increment counters
2069 $counter++;
2070 //Do some output
2071 if ($counter % 1 == 0) {
2072 if (!defined('RESTORE_SILENTLY')) {
2073 echo ".";
2074 if ($counter % 20 == 0) {
2075 echo "<br />";
2078 backup_flush(300);
2085 // process histories
2086 if ($gohcount && $continue && !isset($SESSION->restore->importing)) {
2087 if (!defined('RESTORE_SILENTLY')) {
2088 echo '<li>'.get_string('gradeoutcomeshistory','grades').'</li>';
2090 $counter = 0;
2091 while ($counter < $gohcount) {
2092 //Fetch recordset_size records in each iteration
2093 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes_history' AND backup_code = '$restore->backup_unique_code'",
2094 "old_id",
2095 "old_id",
2096 $counter,
2097 $recordset_size);
2098 if ($recs) {
2099 foreach ($recs as $rec) {
2100 //Get the full record from backup_ids
2101 $data = backup_getid($restore->backup_unique_code,'grade_outcomes_history',$rec->old_id);
2102 if ($data) {
2103 //Now get completed xmlized object
2104 $info = $data->info;
2105 //traverse_xmlize($info); //Debug
2106 //print_object ($GLOBALS['traverse_array']); //Debug
2107 //$GLOBALS['traverse_array']=""; //Debug
2109 $oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
2110 if (empty($oldobj->new_id)) {
2111 // if the old object is not being restored, can't restoring its history
2112 $counter++;
2113 continue;
2115 $dbrec->oldid = $oldobj->new_id;
2116 $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
2117 $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
2118 $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
2119 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
2120 $dbrec->loggeduser = $oldobj->new_id;
2122 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
2123 $dbrec->fullname= backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
2124 $oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
2125 $dbrec->scaleid = $oldobj->new_id;
2126 $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
2128 insert_record('grade_outcomes_history', $dbrec);
2129 unset($dbrec);
2132 //Increment counters
2133 $counter++;
2134 //Do some output
2135 if ($counter % 1 == 0) {
2136 if (!defined('RESTORE_SILENTLY')) {
2137 echo ".";
2138 if ($counter % 20 == 0) {
2139 echo "<br />";
2142 backup_flush(300);
2149 if (!defined('RESTORE_SILENTLY')) {
2150 //End ul
2151 echo '</ul>';
2153 return $status;
2156 //This function creates all the user, user_students, user_teachers
2157 //user_course_creators and user_admins from xml
2158 function restore_create_users($restore,$xml_file) {
2160 global $CFG, $db;
2162 $status = true;
2163 //Check it exists
2164 if (!file_exists($xml_file)) {
2165 $status = false;
2167 //Get info from xml
2168 if ($status) {
2169 //info will contain the old_id of every user
2170 //in backup_ids->info will be the real info (serialized)
2171 $info = restore_read_xml_users($restore,$xml_file);
2174 //Now, get evey user_id from $info and user data from $backup_ids
2175 //and create the necessary records (users, user_students, user_teachers
2176 //user_course_creators and user_admins
2177 if (!empty($info->users)) {
2178 // Grab mnethosts keyed by wwwroot, to map to id
2179 $mnethosts = get_records('mnet_host', '', '',
2180 'wwwroot', 'wwwroot, id');
2182 $languages = get_list_of_languages();
2184 foreach ($info->users as $userid) {
2185 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
2186 $user = $rec->info;
2188 //Now, recode some languages (Moodle 1.5)
2189 if ($user->lang == 'ma_nt') {
2190 $user->lang = 'mi_nt';
2194 //If language does not exist here - use site default
2195 if (!array_key_exists($user->lang, $languages)) {
2196 $user->lang = $CFG->lang;
2199 //Check if it's admin and coursecreator
2200 $is_admin = !empty($user->roles['admin']);
2201 $is_coursecreator = !empty($user->roles['coursecreator']);
2203 //Check if it's teacher and student
2204 $is_teacher = !empty($user->roles['teacher']);
2205 $is_student = !empty($user->roles['student']);
2207 //Check if it's needed
2208 $is_needed = !empty($user->roles['needed']);
2210 //Calculate if it is a course user
2211 //Has role teacher or student or needed
2212 $is_course_user = ($is_teacher or $is_student or $is_needed);
2214 //Calculate mnethostid
2215 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
2216 $user->mnethostid = $CFG->mnet_localhost_id;
2217 } else {
2218 // fast url-to-id lookups
2219 if (isset($mnethosts[$user->mnethosturl])) {
2220 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
2221 } else {
2222 // should not happen, as we check in restore_chech.php
2223 // but handle the error if it does
2224 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
2227 unset($user->mnethosturl);
2229 //To store new ids created
2230 $newid=null;
2231 //check if it exists (by username) and get its id
2232 $user_exists = true;
2233 $user_data = get_record("user","username",addslashes($user->username),
2234 'mnethostid', $user->mnethostid);
2235 if (!$user_data) {
2236 $user_exists = false;
2237 } else {
2238 $newid = $user_data->id;
2240 //Flags to see if we have to create the user, roles and preferences
2241 $create_user = true;
2242 $create_roles = true;
2243 $create_preferences = true;
2245 //If we are restoring course users and it isn't a course user
2246 if ($restore->users == 1 and !$is_course_user) {
2247 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
2248 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
2249 $create_user = false;
2250 $create_roles = false;
2251 $create_preferences = false;
2254 if ($user_exists and $create_user) {
2255 //If user exists mark its newid in backup_ids (the same than old)
2256 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
2257 $create_user = false;
2260 //Here, if create_user, do it
2261 if ($create_user) {
2262 //Unset the id because it's going to be inserted with a new one
2263 unset ($user->id);
2264 //We addslashes to necessary fields
2265 $user->username = addslashes($user->username);
2266 $user->firstname = addslashes($user->firstname);
2267 $user->lastname = addslashes($user->lastname);
2268 $user->email = addslashes($user->email);
2269 $user->institution = addslashes($user->institution);
2270 $user->department = addslashes($user->department);
2271 $user->address = addslashes($user->address);
2272 $user->city = addslashes($user->city);
2273 $user->url = addslashes($user->url);
2274 $user->description = backup_todb($user->description);
2276 //We need to analyse the AUTH field to recode it:
2277 // - if the field isn't set, we are in a pre 1.4 backup and we'll
2278 // use manual
2280 if (empty($user->auth)) {
2281 if ($CFG->registerauth == 'email') {
2282 $user->auth = 'email';
2283 } else {
2284 $user->auth = 'manual';
2288 //We need to process the POLICYAGREED field to recalculate it:
2289 // - if the destination site is different (by wwwroot) reset it.
2290 // - if the destination site is the same (by wwwroot), leave it unmodified
2292 if ($restore->original_wwwroot != $CFG->wwwroot) {
2293 $user->policyagreed = 0;
2294 } else {
2295 //Nothing to do, we are in the same server
2298 //Check if the theme exists in destination server
2299 $themes = get_list_of_themes();
2300 if (!in_array($user->theme, $themes)) {
2301 $user->theme = '';
2304 //We are going to create the user
2305 //The structure is exactly as we need
2306 $newid = insert_record ("user",$user);
2307 //Put the new id
2308 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
2311 //Here, if create_roles, do it as necessary
2312 if ($create_roles) {
2313 //Get the newid and current info from backup_ids
2314 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2315 $newid = $data->new_id;
2316 $currinfo = $data->info.",";
2318 //Now, depending of the role, create records in user_studentes and user_teacher
2319 //and/or mark it in backup_ids
2321 if ($is_admin) {
2322 //If the record (user_admins) doesn't exists
2323 //Only put status in backup_ids
2324 $currinfo = $currinfo."admin,";
2325 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2327 if ($is_coursecreator) {
2328 //If the record (user_coursecreators) doesn't exists
2329 //Only put status in backup_ids
2330 $currinfo = $currinfo."coursecreator,";
2331 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2333 if ($is_needed) {
2334 //Only put status in backup_ids
2335 $currinfo = $currinfo."needed,";
2336 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2338 if ($is_teacher) {
2339 //If the record (teacher) doesn't exists
2340 //Put status in backup_ids
2341 $currinfo = $currinfo."teacher,";
2342 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2343 //Set course and user
2344 $user->roles['teacher']->course = $restore->course_id;
2345 $user->roles['teacher']->userid = $newid;
2347 //Need to analyse the enrol field
2348 // - if it isn't set, set it to $CFG->enrol
2349 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2350 // - if we are in the same server (by wwwroot), maintain it unmodified.
2351 if (empty($user->roles['teacher']->enrol)) {
2352 $user->roles['teacher']->enrol = $CFG->enrol;
2353 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2354 $user->roles['teacher']->enrol = $CFG->enrol;
2355 } else {
2356 //Nothing to do. Leave it unmodified
2359 $rolesmapping = $restore->rolesmapping;
2360 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2361 if ($user->roles['teacher']->editall) {
2362 role_assign($rolesmapping['defaultteacheredit'],
2363 $newid,
2365 $context->id,
2366 $user->roles['teacher']->timestart,
2367 $user->roles['teacher']->timeend,
2369 $user->roles['teacher']->enrol);
2371 // editting teacher
2372 } else {
2373 // non editting teacher
2374 role_assign($rolesmapping['defaultteacher'],
2375 $newid,
2377 $context->id,
2378 $user->roles['teacher']->timestart,
2379 $user->roles['teacher']->timeend,
2381 $user->roles['teacher']->enrol);
2384 if ($is_student) {
2386 //Put status in backup_ids
2387 $currinfo = $currinfo."student,";
2388 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2389 //Set course and user
2390 $user->roles['student']->course = $restore->course_id;
2391 $user->roles['student']->userid = $newid;
2393 //Need to analyse the enrol field
2394 // - if it isn't set, set it to $CFG->enrol
2395 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
2396 // - if we are in the same server (by wwwroot), maintain it unmodified.
2397 if (empty($user->roles['student']->enrol)) {
2398 $user->roles['student']->enrol = $CFG->enrol;
2399 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
2400 $user->roles['student']->enrol = $CFG->enrol;
2401 } else {
2402 //Nothing to do. Leave it unmodified
2404 $rolesmapping = $restore->rolesmapping;
2405 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
2407 role_assign($rolesmapping['defaultstudent'],
2408 $newid,
2410 $context->id,
2411 $user->roles['student']->timestart,
2412 $user->roles['student']->timeend,
2414 $user->roles['student']->enrol);
2417 if (!$is_course_user) {
2418 //If the record (user) doesn't exists
2419 if (!record_exists("user","id",$newid)) {
2420 //Put status in backup_ids
2421 $currinfo = $currinfo."user,";
2422 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
2427 //Here, if create_preferences, do it as necessary
2428 if ($create_preferences) {
2429 //echo "Checking for preferences of user ".$user->username."<br />"; //Debug
2430 //Get user new id from backup_ids
2431 $data = backup_getid($restore->backup_unique_code,"user",$userid);
2432 $newid = $data->new_id;
2433 if (isset($user->user_preferences)) {
2434 //echo "Preferences exist in backup file<br />"; //Debug
2435 foreach($user->user_preferences as $user_preference) {
2436 //echo $user_preference->name." = ".$user_preference->value."<br />"; //Debug
2437 //We check if that user_preference exists in DB
2438 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
2439 //echo "Creating it<br />"; //Debug
2440 //Prepare the record and insert it
2441 $user_preference->userid = $newid;
2442 $status = insert_record("user_preferences",$user_preference);
2450 return $status;
2453 //This function creates all the structures messages and contacts
2454 function restore_create_messages($restore,$xml_file) {
2456 global $CFG;
2458 $status = true;
2459 //Check it exists
2460 if (!file_exists($xml_file)) {
2461 $status = false;
2463 //Get info from xml
2464 if ($status) {
2465 //info will contain the id and name of every table
2466 //(message, message_read and message_contacts)
2467 //in backup_ids->info will be the real info (serialized)
2468 $info = restore_read_xml_messages($restore,$xml_file);
2470 //If we have info, then process messages & contacts
2471 if ($info > 0) {
2472 //Count how many we have
2473 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
2474 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
2475 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
2476 if ($unreadcount || $readcount || $contactcount) {
2477 //Start ul
2478 if (!defined('RESTORE_SILENTLY')) {
2479 echo '<ul>';
2481 //Number of records to get in every chunk
2482 $recordset_size = 4;
2484 //Process unread
2485 if ($unreadcount) {
2486 if (!defined('RESTORE_SILENTLY')) {
2487 echo '<li>'.get_string('unreadmessages','message').'</li>';
2489 $counter = 0;
2490 while ($counter < $unreadcount) {
2491 //Fetch recordset_size records in each iteration
2492 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2493 if ($recs) {
2494 foreach ($recs as $rec) {
2495 //Get the full record from backup_ids
2496 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
2497 if ($data) {
2498 //Now get completed xmlized object
2499 $info = $data->info;
2500 //traverse_xmlize($info); //Debug
2501 //print_object ($GLOBALS['traverse_array']); //Debug
2502 //$GLOBALS['traverse_array']=""; //Debug
2503 //Now build the MESSAGE record structure
2504 $dbrec = new object();
2505 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2506 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2507 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2508 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2509 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2510 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2511 //We have to recode the useridfrom field
2512 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2513 if ($user) {
2514 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2515 $dbrec->useridfrom = $user->new_id;
2517 //We have to recode the useridto field
2518 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2519 if ($user) {
2520 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2521 $dbrec->useridto = $user->new_id;
2523 //Check if the record doesn't exist in DB!
2524 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
2525 'useridto', $dbrec->useridto,
2526 'timecreated',$dbrec->timecreated);
2527 if (!$exist) {
2528 //Not exist. Insert
2529 $status = insert_record('message',$dbrec);
2530 } else {
2531 //Duplicate. Do nothing
2534 //Do some output
2535 $counter++;
2536 if ($counter % 10 == 0) {
2537 if (!defined('RESTORE_SILENTLY')) {
2538 echo ".";
2539 if ($counter % 200 == 0) {
2540 echo "<br />";
2543 backup_flush(300);
2550 //Process read
2551 if ($readcount) {
2552 if (!defined('RESTORE_SILENTLY')) {
2553 echo '<li>'.get_string('readmessages','message').'</li>';
2555 $counter = 0;
2556 while ($counter < $readcount) {
2557 //Fetch recordset_size records in each iteration
2558 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2559 if ($recs) {
2560 foreach ($recs as $rec) {
2561 //Get the full record from backup_ids
2562 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
2563 if ($data) {
2564 //Now get completed xmlized object
2565 $info = $data->info;
2566 //traverse_xmlize($info); //Debug
2567 //print_object ($GLOBALS['traverse_array']); //Debug
2568 //$GLOBALS['traverse_array']=""; //Debug
2569 //Now build the MESSAGE_READ record structure
2570 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2571 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2572 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2573 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2574 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2575 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2576 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2577 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2578 //We have to recode the useridfrom field
2579 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2580 if ($user) {
2581 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2582 $dbrec->useridfrom = $user->new_id;
2584 //We have to recode the useridto field
2585 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2586 if ($user) {
2587 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2588 $dbrec->useridto = $user->new_id;
2590 //Check if the record doesn't exist in DB!
2591 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
2592 'useridto', $dbrec->useridto,
2593 'timecreated',$dbrec->timecreated);
2594 if (!$exist) {
2595 //Not exist. Insert
2596 $status = insert_record('message_read',$dbrec);
2597 } else {
2598 //Duplicate. Do nothing
2601 //Do some output
2602 $counter++;
2603 if ($counter % 10 == 0) {
2604 if (!defined('RESTORE_SILENTLY')) {
2605 echo ".";
2606 if ($counter % 200 == 0) {
2607 echo "<br />";
2610 backup_flush(300);
2617 //Process contacts
2618 if ($contactcount) {
2619 if (!defined('RESTORE_SILENTLY')) {
2620 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2622 $counter = 0;
2623 while ($counter < $contactcount) {
2624 //Fetch recordset_size records in each iteration
2625 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
2626 if ($recs) {
2627 foreach ($recs as $rec) {
2628 //Get the full record from backup_ids
2629 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
2630 if ($data) {
2631 //Now get completed xmlized object
2632 $info = $data->info;
2633 //traverse_xmlize($info); //Debug
2634 //print_object ($GLOBALS['traverse_array']); //Debug
2635 //$GLOBALS['traverse_array']=""; //Debug
2636 //Now build the MESSAGE_CONTACTS record structure
2637 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2638 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2639 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2640 //We have to recode the userid field
2641 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
2642 if ($user) {
2643 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2644 $dbrec->userid = $user->new_id;
2646 //We have to recode the contactid field
2647 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
2648 if ($user) {
2649 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2650 $dbrec->contactid = $user->new_id;
2652 //Check if the record doesn't exist in DB!
2653 $exist = get_record('message_contacts','userid',$dbrec->userid,
2654 'contactid', $dbrec->contactid);
2655 if (!$exist) {
2656 //Not exist. Insert
2657 $status = insert_record('message_contacts',$dbrec);
2658 } else {
2659 //Duplicate. Do nothing
2662 //Do some output
2663 $counter++;
2664 if ($counter % 10 == 0) {
2665 if (!defined('RESTORE_SILENTLY')) {
2666 echo ".";
2667 if ($counter % 200 == 0) {
2668 echo "<br />";
2671 backup_flush(300);
2677 if (!defined('RESTORE_SILENTLY')) {
2678 //End ul
2679 echo '</ul>';
2685 return $status;
2688 //This function creates all the categories and questions
2689 //from xml
2690 function restore_create_questions($restore,$xml_file) {
2692 global $CFG, $db;
2694 $status = true;
2695 //Check it exists
2696 if (!file_exists($xml_file)) {
2697 $status = false;
2699 //Get info from xml
2700 if ($status) {
2701 //info will contain the old_id of every category
2702 //in backup_ids->info will be the real info (serialized)
2703 $info = restore_read_xml_questions($restore,$xml_file);
2705 //Now, if we have anything in info, we have to restore that
2706 //categories/questions
2707 if ($info) {
2708 if ($info !== true) {
2709 $status = $status && restore_question_categories($info, $restore);
2711 } else {
2712 $status = false;
2714 return $status;
2717 //This function creates all the scales
2718 function restore_create_scales($restore,$xml_file) {
2720 global $CFG, $db;
2722 $status = true;
2723 //Check it exists
2724 if (!file_exists($xml_file)) {
2725 $status = false;
2727 //Get info from xml
2728 if ($status) {
2729 //scales will contain the old_id of every scale
2730 //in backup_ids->info will be the real info (serialized)
2731 $scales = restore_read_xml_scales($restore,$xml_file);
2733 //Now, if we have anything in scales, we have to restore that
2734 //scales
2735 if ($scales) {
2736 //Get admin->id for later use
2737 $admin = get_admin();
2738 $adminid = $admin->id;
2739 if ($scales !== true) {
2740 //Iterate over each scale
2741 foreach ($scales as $scale) {
2742 //Get record from backup_ids
2743 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
2744 //Init variables
2745 $create_scale = false;
2747 if ($data) {
2748 //Now get completed xmlized object
2749 $info = $data->info;
2750 //traverse_xmlize($info); //Debug
2751 //print_object ($GLOBALS['traverse_array']); //Debug
2752 //$GLOBALS['traverse_array']=""; //Debug
2754 //Now build the SCALE record structure
2755 $sca = new object();
2756 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
2757 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
2758 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
2759 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
2760 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
2761 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
2763 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
2764 //or in restore->course_id course (Personal scale)
2765 if ($sca->courseid == 0) {
2766 $course_to_search = 0;
2767 } else {
2768 $course_to_search = $restore->course_id;
2771 // scale is not course unique, use get_record_sql to suppress warning
2773 $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale
2774 WHERE scale = '$sca->scale'
2775 AND courseid = $course_to_search", true);
2777 //If it doesn't exist, create
2778 if (!$sca_db) {
2779 $create_scale = true;
2781 //If we must create the scale
2782 if ($create_scale) {
2783 //Me must recode the courseid if it's <> 0 (common scale)
2784 if ($sca->courseid != 0) {
2785 $sca->courseid = $restore->course_id;
2787 //We must recode the userid
2788 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
2789 if ($user) {
2790 $sca->userid = $user->new_id;
2791 } else {
2792 //Assign it to admin
2793 $sca->userid = $adminid;
2795 //The structure is equal to the db, so insert the scale
2796 $newid = insert_record ("scale",$sca);
2797 } else {
2798 //get current scale id
2799 $newid = $sca_db->id;
2801 if ($newid) {
2802 //We have the newid, update backup_ids
2803 backup_putid($restore->backup_unique_code,"scale",
2804 $scale->id, $newid);
2809 } else {
2810 $status = false;
2812 return $status;
2815 //This function creates all the groups
2816 function restore_create_groups($restore,$xml_file) {
2818 global $CFG;
2820 //Check it exists
2821 if (!file_exists($xml_file)) {
2822 return false;
2824 //Get info from xml
2825 if (!$groups = restore_read_xml_groups($restore,$xml_file)) {
2826 //groups will contain the old_id of every group
2827 //in backup_ids->info will be the real info (serialized)
2828 return false;
2830 } else if ($groups === true) {
2831 return true;
2834 $status = true;
2836 //Iterate over each group
2837 foreach ($groups as $group) {
2838 //Get record from backup_ids
2839 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
2841 if ($data) {
2842 //Now get completed xmlized object
2843 $info = $data->info;
2844 //traverse_xmlize($info); //Debug
2845 //print_object ($GLOBALS['traverse_array']); //Debug
2846 //$GLOBALS['traverse_array']=""; //Debug
2847 //Now build the GROUP record structure
2848 $gro = new Object();
2849 $gro->courseid = $restore->course_id;
2850 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
2851 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
2852 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
2853 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
2854 } else {
2855 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
2857 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
2858 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
2859 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
2860 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
2862 //Now search if that group exists (by name and description field) in
2863 //restore->course_id course
2864 //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
2865 $description_clause = '';
2866 if (!empty($gro->description)) { /// Only for groups having a description
2867 $literal_description = "'" . $gro->description . "'";
2868 $description_clause = " AND " .
2869 sql_compare_text('description') . " = " .
2870 sql_compare_text($literal_description);
2872 if (!$gro_db = get_record_sql("SELECT *
2873 FROM {$CFG->prefix}groups
2874 WHERE courseid = $restore->course_id AND
2875 name = '{$gro->name}'" . $description_clause)) {
2876 //If it doesn't exist, create
2877 $newid = insert_record('groups', $gro);
2879 } else {
2880 //get current group id
2881 $newid = $gro_db->id;
2884 if ($newid) {
2885 //We have the newid, update backup_ids
2886 backup_putid($restore->backup_unique_code,"groups", $group->id, $newid);
2887 } else {
2889 $status = false;
2890 continue;
2893 //Now restore members in the groups_members, only if
2894 //users are included
2895 if ($restore->users != 2) {
2896 if (!restore_create_groups_members($newid,$info,$restore)) {
2897 $status = false;
2903 //Now, restore group_files
2904 if ($status) {
2905 $status = restore_group_files($restore);
2908 return $status;
2911 //This function restores the groups_members
2912 function restore_create_groups_members($group_id,$info,$restore) {
2914 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
2915 //OK, some groups have no members.
2916 return true;
2918 //Get the members array
2919 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
2921 $status = true;
2923 //Iterate over members
2924 for($i = 0; $i < sizeof($members); $i++) {
2925 $mem_info = $members[$i];
2926 //traverse_xmlize($mem_info); //Debug
2927 //print_object ($GLOBALS['traverse_array']); //Debug
2928 //$GLOBALS['traverse_array']=""; //Debug
2930 //Now, build the GROUPS_MEMBERS record structure
2931 $group_member = new Object();
2932 $group_member->groupid = $group_id;
2933 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
2934 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2936 $newid = false;
2938 //We have to recode the userid field
2939 if (!$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid)) {
2940 $status = false;
2941 continue;
2944 $group_member->userid = $user->new_id;
2946 //The structure is equal to the db, so insert the groups_members
2947 if (!insert_record ("groups_members", $group_member)) {
2948 $status = false;
2949 continue;
2952 //Do some output
2953 if (($i+1) % 50 == 0) {
2954 if (!defined('RESTORE_SILENTLY')) {
2955 echo ".";
2956 if (($i+1) % 1000 == 0) {
2957 echo "<br />";
2960 backup_flush(300);
2964 return $status;
2967 //This function creates all the groupings
2968 function restore_create_groupings($restore,$xml_file) {
2970 //Check it exists
2971 if (!file_exists($xml_file)) {
2972 return false;
2974 //Get info from xml
2975 if (!$groupings = restore_read_xml_groupings($restore,$xml_file)) {
2976 return false;
2978 } else if ($groupings === true) {
2979 return true;
2982 $status = true;
2984 //Iterate over each group
2985 foreach ($groupings as $grouping) {
2986 if ($data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id)) {
2987 //Now get completed xmlized object
2988 $info = $data->info;
2989 //Now build the GROUPING record structure
2990 $gro = new Object();
2991 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
2992 $gro->courseid = $restore->course_id;
2993 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
2994 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
2995 $gro->configdata = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
2996 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
2998 //Now search if that group exists (by name and description field) in
2999 if ($gro_db = get_record('groupings', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
3000 //get current group id
3001 $newid = $gro_db->id;
3003 } else {
3004 //The structure is equal to the db, so insert the grouping
3005 if (!$newid = insert_record('groupings', $gro)) {
3006 $status = false;
3007 continue;
3011 //We have the newid, update backup_ids
3012 backup_putid($restore->backup_unique_code,"groupings",
3013 $grouping->id, $newid);
3018 // now fix the defaultgroupingid in course
3019 $course = get_record('course', 'id', $restore->course_id);
3020 if ($course->defaultgroupingid) {
3021 if ($grouping = backup_getid($restore->backup_unique_code,"groupings",$course->defaultgroupingid)) {
3022 set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
3023 } else {
3024 set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
3028 return $status;
3031 //This function creates all the groupingsgroups
3032 function restore_create_groupings_groups($restore,$xml_file) {
3034 //Check it exists
3035 if (!file_exists($xml_file)) {
3036 return false;
3038 //Get info from xml
3039 if (!$groupingsgroups = restore_read_xml_groupings_groups($restore,$xml_file)) {
3040 return false;
3042 } else if ($groupingsgroups === true) {
3043 return true;
3046 $status = true;
3048 //Iterate over each group
3049 foreach ($groupingsgroups as $groupinggroup) {
3050 if ($data = backup_getid($restore->backup_unique_code,"groupingsgroups",$groupinggroup->id)) {
3051 //Now get completed xmlized object
3052 $info = $data->info;
3053 //Now build the GROUPING record structure
3054 $gro_member = new Object();
3055 $gro_member->groupingid = backup_todb($info['GROUPINGGROUP']['#']['GROUPINGID']['0']['#']);
3056 $gro_member->groupid = backup_todb($info['GROUPINGGROUP']['#']['GROUPID']['0']['#']);
3057 $gro_member->timeadded = backup_todb($info['GROUPINGGROUP']['#']['TIMEADDED']['0']['#']);
3059 if (!$grouping = backup_getid($restore->backup_unique_code,"groupings",$gro_member->groupingid)) {
3060 $status = false;
3061 continue;
3064 if (!$group = backup_getid($restore->backup_unique_code,"groups",$gro_member->groupid)) {
3065 $status = false;
3066 continue;
3069 $gro_member->groupid = $group->new_id;
3070 $gro_member->groupingid = $grouping->new_id;
3071 if (!get_record('groupings_groups', 'groupid', $gro_member->groupid, 'groupingid', $gro_member->groupingid)) {
3072 if (!insert_record('groupings_groups', $gro_member)) {
3073 $status = false;
3079 return $status;
3082 //This function creates all the course events
3083 function restore_create_events($restore,$xml_file) {
3085 global $CFG, $db;
3087 $status = true;
3088 //Check it exists
3089 if (!file_exists($xml_file)) {
3090 $status = false;
3092 //Get info from xml
3093 if ($status) {
3094 //events will contain the old_id of every event
3095 //in backup_ids->info will be the real info (serialized)
3096 $events = restore_read_xml_events($restore,$xml_file);
3099 //Get admin->id for later use
3100 $admin = get_admin();
3101 $adminid = $admin->id;
3103 //Now, if we have anything in events, we have to restore that
3104 //events
3105 if ($events) {
3106 if ($events !== true) {
3107 //Iterate over each event
3108 foreach ($events as $event) {
3109 //Get record from backup_ids
3110 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
3111 //Init variables
3112 $create_event = false;
3114 if ($data) {
3115 //Now get completed xmlized object
3116 $info = $data->info;
3117 //traverse_xmlize($info); //Debug
3118 //print_object ($GLOBALS['traverse_array']); //Debug
3119 //$GLOBALS['traverse_array']=""; //Debug
3121 //if necessary, write to restorelog and adjust date/time fields
3122 if ($restore->course_startdateoffset) {
3123 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
3126 //Now build the EVENT record structure
3127 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
3128 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
3129 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
3130 $eve->courseid = $restore->course_id;
3131 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
3132 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
3133 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
3134 $eve->modulename = "";
3135 if (!empty($info['EVENT']['#']['MODULENAME'])) {
3136 $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
3138 $eve->instance = 0;
3139 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
3140 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
3141 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
3142 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
3143 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
3145 //Now search if that event exists (by name, description, timestart fields) in
3146 //restore->course_id course
3147 $eve_db = get_record_select("event",
3148 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
3149 //If it doesn't exist, create
3150 if (!$eve_db) {
3151 $create_event = true;
3153 //If we must create the event
3154 if ($create_event) {
3156 //We must recode the userid
3157 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
3158 if ($user) {
3159 $eve->userid = $user->new_id;
3160 } else {
3161 //Assign it to admin
3162 $eve->userid = $adminid;
3165 //We must recode the repeatid if the event has it
3166 if (!empty($eve->repeatid)) {
3167 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
3168 if ($repeat_rec) { //Exists, so use it...
3169 $eve->repeatid = $repeat_rec->new_id;
3170 } else { //Doesn't exists, calculate the next and save it
3171 $oldrepeatid = $eve->repeatid;
3172 $max_rec = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event');
3173 $eve->repeatid = empty($max_rec) ? 1 : $max_rec->repeatid + 1;
3174 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
3178 //We have to recode the groupid field
3179 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
3180 if ($group) {
3181 $eve->groupid = $group->new_id;
3182 } else {
3183 //Assign it to group 0
3184 $eve->groupid = 0;
3187 //The structure is equal to the db, so insert the event
3188 $newid = insert_record ("event",$eve);
3189 } else {
3190 //get current event id
3191 $newid = $eve_db->id;
3193 if ($newid) {
3194 //We have the newid, update backup_ids
3195 backup_putid($restore->backup_unique_code,"event",
3196 $event->id, $newid);
3201 } else {
3202 $status = false;
3204 return $status;
3207 //This function decode things to make restore multi-site fully functional
3208 //It does this conversions:
3209 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
3210 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
3212 //Note: Inter-activities linking is being implemented as a final
3213 //step in the restore execution, because we need to have it
3214 //finished to know all the oldid, newid equivaleces
3215 function restore_decode_absolute_links($content) {
3217 global $CFG,$restore;
3219 // MDL-10770
3220 // This function was replacing null with empty string
3221 // Nullity check is added in backup_todb(), this function will no longer not be called from backup_todb() if content is null
3222 // I noticed some parts of the restore code is calling this directly instead of calling backup_todb(), so just in case
3223 // 3rd party mod etc are doing the same
3224 if ($content === NULL) {
3225 return NULL;
3228 //Now decode wwwroot and file.php calls
3229 $search = array ("$@FILEPHP@$");
3231 //Check for the status of the slasharguments config variable
3232 $slash = $CFG->slasharguments;
3234 //Build the replace string as needed
3235 if ($slash == 1) {
3236 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
3237 } else {
3238 $replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
3241 $result = str_replace($search,$replace,$content);
3243 if ($result != $content && debugging()) { //Debug
3244 if (!defined('RESTORE_SILENTLY')) {
3245 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
3247 } //Debug
3249 return $result;
3252 //This function restores the userfiles from the temp (user_files) directory to the
3253 //dataroot/users directory
3254 function restore_user_files($restore) {
3256 global $CFG;
3258 $status = true;
3260 $counter = 0;
3262 //First, we check to "users" exists and create is as necessary
3263 //in CFG->dataroot
3264 $dest_dir = $CFG->dataroot."/users";
3265 $status = check_dir_exists($dest_dir,true);
3267 //Now, we iterate over "user_files" records to check if that user dir must be
3268 //copied (and renamed) to the "users" dir.
3269 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
3270 //Check if directory exists
3271 if (is_dir($rootdir)) {
3272 $list = list_directories ($rootdir);
3273 if ($list) {
3274 //Iterate
3275 $counter = 0;
3276 foreach ($list as $dir) {
3277 //Look for dir like username in backup_ids
3278 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
3279 "table_name","user",
3280 "old_id",$dir);
3281 //If thar user exists in backup_ids
3282 if ($data) {
3283 //Only it user has been created now
3284 //or if it existed previously, but he hasn't image (see bug 1123)
3285 if ((strpos($data->info,"new") !== false) or
3286 (!check_dir_exists($dest_dir."/".$data->new_id,false))) {
3287 //Copy the old_dir to its new location (and name) !!
3288 //Only if destination doesn't exists
3289 if (!file_exists($dest_dir."/".$data->new_id)) {
3290 $status = backup_copy_file($rootdir."/".$dir,
3291 $dest_dir."/".$data->new_id,true);
3292 $counter ++;
3294 //Do some output
3295 if ($counter % 2 == 0) {
3296 if (!defined('RESTORE_SILENTLY')) {
3297 echo ".";
3298 if ($counter % 40 == 0) {
3299 echo "<br />";
3302 backup_flush(300);
3309 //If status is ok and whe have dirs created, returns counter to inform
3310 if ($status and $counter) {
3311 return $counter;
3312 } else {
3313 return $status;
3317 //This function restores the groupfiles from the temp (group_files) directory to the
3318 //dataroot/groups directory
3319 function restore_group_files($restore) {
3321 global $CFG;
3323 $status = true;
3325 $counter = 0;
3327 //First, we check to "groups" exists and create is as necessary
3328 //in CFG->dataroot
3329 $dest_dir = $CFG->dataroot.'/groups';
3330 $status = check_dir_exists($dest_dir,true);
3332 //Now, we iterate over "group_files" records to check if that user dir must be
3333 //copied (and renamed) to the "groups" dir.
3334 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
3335 //Check if directory exists
3336 if (is_dir($rootdir)) {
3337 $list = list_directories ($rootdir);
3338 if ($list) {
3339 //Iterate
3340 $counter = 0;
3341 foreach ($list as $dir) {
3342 //Look for dir like groupid in backup_ids
3343 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
3344 "table_name","groups",
3345 "old_id",$dir);
3346 //If that group exists in backup_ids
3347 if ($data) {
3348 if (!file_exists($dest_dir."/".$data->new_id)) {
3349 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
3350 $counter ++;
3352 //Do some output
3353 if ($counter % 2 == 0) {
3354 if (!defined('RESTORE_SILENTLY')) {
3355 echo ".";
3356 if ($counter % 40 == 0) {
3357 echo "<br />";
3360 backup_flush(300);
3366 //If status is ok and whe have dirs created, returns counter to inform
3367 if ($status and $counter) {
3368 return $counter;
3369 } else {
3370 return $status;
3374 //This function restores the course files from the temp (course_files) directory to the
3375 //dataroot/course_id directory
3376 function restore_course_files($restore) {
3378 global $CFG;
3380 $status = true;
3382 $counter = 0;
3384 //First, we check to "course_id" exists and create is as necessary
3385 //in CFG->dataroot
3386 $dest_dir = $CFG->dataroot."/".$restore->course_id;
3387 $status = check_dir_exists($dest_dir,true);
3389 //Now, we iterate over "course_files" records to check if that file/dir must be
3390 //copied to the "dest_dir" dir.
3391 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
3392 //Check if directory exists
3393 if (is_dir($rootdir)) {
3394 $list = list_directories_and_files ($rootdir);
3395 if ($list) {
3396 //Iterate
3397 $counter = 0;
3398 foreach ($list as $dir) {
3399 //Copy the dir to its new location
3400 //Only if destination file/dir doesn exists
3401 if (!file_exists($dest_dir."/".$dir)) {
3402 $status = backup_copy_file($rootdir."/".$dir,
3403 $dest_dir."/".$dir,true);
3404 $counter ++;
3406 //Do some output
3407 if ($counter % 2 == 0) {
3408 if (!defined('RESTORE_SILENTLY')) {
3409 echo ".";
3410 if ($counter % 40 == 0) {
3411 echo "<br />";
3414 backup_flush(300);
3419 //If status is ok and whe have dirs created, returns counter to inform
3420 if ($status and $counter) {
3421 return $counter;
3422 } else {
3423 return $status;
3427 //This function restores the site files from the temp (site_files) directory to the
3428 //dataroot/SITEID directory
3429 function restore_site_files($restore) {
3431 global $CFG;
3433 $status = true;
3435 $counter = 0;
3437 //First, we check to "course_id" exists and create is as necessary
3438 //in CFG->dataroot
3439 $dest_dir = $CFG->dataroot."/".SITEID;
3440 $status = check_dir_exists($dest_dir,true);
3442 //Now, we iterate over "site_files" files to check if that file/dir must be
3443 //copied to the "dest_dir" dir.
3444 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/site_files";
3445 //Check if directory exists
3446 if (is_dir($rootdir)) {
3447 $list = list_directories_and_files ($rootdir);
3448 if ($list) {
3449 //Iterate
3450 $counter = 0;
3451 foreach ($list as $dir) {
3452 //Copy the dir to its new location
3453 //Only if destination file/dir doesn exists
3454 if (!file_exists($dest_dir."/".$dir)) {
3455 $status = backup_copy_file($rootdir."/".$dir,
3456 $dest_dir."/".$dir,true);
3457 $counter ++;
3459 //Do some output
3460 if ($counter % 2 == 0) {
3461 if (!defined('RESTORE_SILENTLY')) {
3462 echo ".";
3463 if ($counter % 40 == 0) {
3464 echo "<br />";
3467 backup_flush(300);
3472 //If status is ok and whe have dirs created, returns counter to inform
3473 if ($status and $counter) {
3474 return $counter;
3475 } else {
3476 return $status;
3481 //This function creates all the structures for every module in backup file
3482 //Depending what has been selected.
3483 function restore_create_modules($restore,$xml_file) {
3485 global $CFG;
3486 $status = true;
3487 //Check it exists
3488 if (!file_exists($xml_file)) {
3489 $status = false;
3491 //Get info from xml
3492 if ($status) {
3493 //info will contain the id and modtype of every module
3494 //in backup_ids->info will be the real info (serialized)
3495 $info = restore_read_xml_modules($restore,$xml_file);
3497 //Now, if we have anything in info, we have to restore that mods
3498 //from backup_ids (calling every mod restore function)
3499 if ($info) {
3500 if ($info !== true) {
3501 if (!defined('RESTORE_SILENTLY')) {
3502 echo '<ul>';
3504 //Iterate over each module
3505 foreach ($info as $mod) {
3506 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
3507 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
3508 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
3509 $modrestore = $mod->modtype."_restore_mods";
3510 if (function_exists($modrestore)) { //Debug
3511 // we want to restore all mods even when one fails
3512 // incorrect code here ignored any errors during module restore in 1.6-1.8
3513 $status = $status && $modrestore($mod,$restore);
3514 } else {
3515 //Something was wrong. Function should exist.
3516 $status = false;
3520 if (!defined('RESTORE_SILENTLY')) {
3521 echo '</ul>';
3524 } else {
3525 $status = false;
3527 return $status;
3530 //This function creates all the structures for every log in backup file
3531 //Depending what has been selected.
3532 function restore_create_logs($restore,$xml_file) {
3534 global $CFG,$db;
3536 //Number of records to get in every chunk
3537 $recordset_size = 4;
3538 //Counter, points to current record
3539 $counter = 0;
3540 //To count all the recods to restore
3541 $count_logs = 0;
3543 $status = true;
3544 //Check it exists
3545 if (!file_exists($xml_file)) {
3546 $status = false;
3548 //Get info from xml
3549 if ($status) {
3550 //count_logs will contain the number of logs entries to process
3551 //in backup_ids->info will be the real info (serialized)
3552 $count_logs = restore_read_xml_logs($restore,$xml_file);
3555 //Now, if we have records in count_logs, we have to restore that logs
3556 //from backup_ids. This piece of code makes calls to:
3557 // - restore_log_course() if it's a course log
3558 // - restore_log_user() if it's a user log
3559 // - restore_log_module() if it's a module log.
3560 //And all is segmented in chunks to allow large recordsets to be restored !!
3561 if ($count_logs > 0) {
3562 while ($counter < $count_logs) {
3563 //Get a chunk of records
3564 //Take old_id twice to avoid adodb limitation
3565 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id",$counter,$recordset_size);
3566 //We have logs
3567 if ($logs) {
3568 //Iterate
3569 foreach ($logs as $log) {
3570 //Get the full record from backup_ids
3571 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
3572 if ($data) {
3573 //Now get completed xmlized object
3574 $info = $data->info;
3575 //traverse_xmlize($info); //Debug
3576 //print_object ($GLOBALS['traverse_array']); //Debug
3577 //$GLOBALS['traverse_array']=""; //Debug
3578 //Now build the LOG record structure
3579 $dblog = new object();
3580 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
3581 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
3582 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
3583 $dblog->course = $restore->course_id;
3584 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
3585 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
3586 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
3587 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
3588 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
3589 //We have to recode the userid field
3590 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
3591 if ($user) {
3592 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
3593 $dblog->userid = $user->new_id;
3595 //We have to recode the cmid field (if module isn't "course" or "user")
3596 if ($dblog->module != "course" and $dblog->module != "user") {
3597 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
3598 if ($cm) {
3599 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
3600 $dblog->cmid = $cm->new_id;
3601 } else {
3602 $dblog->cmid = 0;
3605 //print_object ($dblog); //Debug
3606 //Now, we redirect to the needed function to make all the work
3607 if ($dblog->module == "course") {
3608 //It's a course log,
3609 $stat = restore_log_course($restore,$dblog);
3610 } elseif ($dblog->module == "user") {
3611 //It's a user log,
3612 $stat = restore_log_user($restore,$dblog);
3613 } else {
3614 //It's a module log,
3615 $stat = restore_log_module($restore,$dblog);
3619 //Do some output
3620 $counter++;
3621 if ($counter % 10 == 0) {
3622 if (!defined('RESTORE_SILENTLY')) {
3623 echo ".";
3624 if ($counter % 200 == 0) {
3625 echo "<br />";
3628 backup_flush(300);
3631 } else {
3632 //We never should arrive here
3633 $counter = $count_logs;
3634 $status = false;
3639 return $status;
3642 //This function inserts a course log record, calculating the URL field as necessary
3643 function restore_log_course($restore,$log) {
3645 $status = true;
3646 $toinsert = false;
3648 //echo "<hr />Before transformations<br />"; //Debug
3649 //print_object($log); //Debug
3650 //Depending of the action, we recode different things
3651 switch ($log->action) {
3652 case "view":
3653 $log->url = "view.php?id=".$log->course;
3654 $log->info = $log->course;
3655 $toinsert = true;
3656 break;
3657 case "guest":
3658 $log->url = "view.php?id=".$log->course;
3659 $toinsert = true;
3660 break;
3661 case "user report":
3662 //recode the info field (it's the user id)
3663 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3664 if ($user) {
3665 $log->info = $user->new_id;
3666 //Now, extract the mode from the url field
3667 $mode = substr(strrchr($log->url,"="),1);
3668 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
3669 $toinsert = true;
3671 break;
3672 case "add mod":
3673 //Extract the course_module from the url field
3674 $cmid = substr(strrchr($log->url,"="),1);
3675 //recode the course_module to see it it has been restored
3676 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3677 if ($cm) {
3678 $cmid = $cm->new_id;
3679 //Extract the module name and the module id from the info field
3680 $modname = strtok($log->info," ");
3681 $modid = strtok(" ");
3682 //recode the module id to see if it has been restored
3683 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3684 if ($mod) {
3685 $modid = $mod->new_id;
3686 //Now I have everything so reconstruct url and info
3687 $log->info = $modname." ".$modid;
3688 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3689 $toinsert = true;
3692 break;
3693 case "update mod":
3694 //Extract the course_module from the url field
3695 $cmid = substr(strrchr($log->url,"="),1);
3696 //recode the course_module to see it it has been restored
3697 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3698 if ($cm) {
3699 $cmid = $cm->new_id;
3700 //Extract the module name and the module id from the info field
3701 $modname = strtok($log->info," ");
3702 $modid = strtok(" ");
3703 //recode the module id to see if it has been restored
3704 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3705 if ($mod) {
3706 $modid = $mod->new_id;
3707 //Now I have everything so reconstruct url and info
3708 $log->info = $modname." ".$modid;
3709 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3710 $toinsert = true;
3713 break;
3714 case "delete mod":
3715 $log->url = "view.php?id=".$log->course;
3716 $toinsert = true;
3717 break;
3718 case "update":
3719 $log->url = "edit.php?id=".$log->course;
3720 $log->info = "";
3721 $toinsert = true;
3722 break;
3723 case "unenrol":
3724 //recode the info field (it's the user id)
3725 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3726 if ($user) {
3727 $log->info = $user->new_id;
3728 $log->url = "view.php?id=".$log->course;
3729 $toinsert = true;
3731 break;
3732 case "enrol":
3733 //recode the info field (it's the user id)
3734 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3735 if ($user) {
3736 $log->info = $user->new_id;
3737 $log->url = "view.php?id=".$log->course;
3738 $toinsert = true;
3740 break;
3741 case "editsection":
3742 //Extract the course_section from the url field
3743 $secid = substr(strrchr($log->url,"="),1);
3744 //recode the course_section to see if it has been restored
3745 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
3746 if ($sec) {
3747 $secid = $sec->new_id;
3748 //Now I have everything so reconstruct url and info
3749 $log->url = "editsection.php?id=".$secid;
3750 $toinsert = true;
3752 break;
3753 case "new":
3754 $log->url = "view.php?id=".$log->course;
3755 $log->info = "";
3756 $toinsert = true;
3757 break;
3758 case "recent":
3759 $log->url = "recent.php?id=".$log->course;
3760 $log->info = "";
3761 $toinsert = true;
3762 break;
3763 case "report log":
3764 $log->url = "report/log/index.php?id=".$log->course;
3765 $log->info = $log->course;
3766 $toinsert = true;
3767 break;
3768 case "report live":
3769 $log->url = "report/log/live.php?id=".$log->course;
3770 $log->info = $log->course;
3771 $toinsert = true;
3772 break;
3773 case "report outline":
3774 $log->url = "report/outline/index.php?id=".$log->course;
3775 $log->info = $log->course;
3776 $toinsert = true;
3777 break;
3778 case "report participation":
3779 $log->url = "report/participation/index.php?id=".$log->course;
3780 $log->info = $log->course;
3781 $toinsert = true;
3782 break;
3783 case "report stats":
3784 $log->url = "report/stats/index.php?id=".$log->course;
3785 $log->info = $log->course;
3786 $toinsert = true;
3787 break;
3788 default:
3789 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3790 break;
3793 //echo "After transformations<br />"; //Debug
3794 //print_object($log); //Debug
3796 //Now if $toinsert is set, insert the record
3797 if ($toinsert) {
3798 //echo "Inserting record<br />"; //Debug
3799 $status = insert_record("log",$log);
3801 return $status;
3804 //This function inserts a user log record, calculating the URL field as necessary
3805 function restore_log_user($restore,$log) {
3807 $status = true;
3808 $toinsert = false;
3810 //echo "<hr />Before transformations<br />"; //Debug
3811 //print_object($log); //Debug
3812 //Depending of the action, we recode different things
3813 switch ($log->action) {
3814 case "view":
3815 //recode the info field (it's the user id)
3816 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3817 if ($user) {
3818 $log->info = $user->new_id;
3819 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3820 $toinsert = true;
3822 break;
3823 case "change password":
3824 //recode the info field (it's the user id)
3825 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3826 if ($user) {
3827 $log->info = $user->new_id;
3828 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3829 $toinsert = true;
3831 break;
3832 case "login":
3833 //recode the info field (it's the user id)
3834 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3835 if ($user) {
3836 $log->info = $user->new_id;
3837 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3838 $toinsert = true;
3840 break;
3841 case "logout":
3842 //recode the info field (it's the user id)
3843 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3844 if ($user) {
3845 $log->info = $user->new_id;
3846 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3847 $toinsert = true;
3849 break;
3850 case "view all":
3851 $log->url = "view.php?id=".$log->course;
3852 $log->info = "";
3853 $toinsert = true;
3854 case "update":
3855 //We split the url by ampersand char
3856 $first_part = strtok($log->url,"&");
3857 //Get data after the = char. It's the user being updated
3858 $userid = substr(strrchr($first_part,"="),1);
3859 //Recode the user
3860 $user = backup_getid($restore->backup_unique_code,"user",$userid);
3861 if ($user) {
3862 $log->info = "";
3863 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
3864 $toinsert = true;
3866 break;
3867 default:
3868 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3869 break;
3872 //echo "After transformations<br />"; //Debug
3873 //print_object($log); //Debug
3875 //Now if $toinsert is set, insert the record
3876 if ($toinsert) {
3877 //echo "Inserting record<br />"; //Debug
3878 $status = insert_record("log",$log);
3880 return $status;
3883 //This function inserts a module log record, calculating the URL field as necessary
3884 function restore_log_module($restore,$log) {
3886 $status = true;
3887 $toinsert = false;
3889 //echo "<hr />Before transformations<br />"; //Debug
3890 //print_object($log); //Debug
3892 //Now we see if the required function in the module exists
3893 $function = $log->module."_restore_logs";
3894 if (function_exists($function)) {
3895 //Call the function
3896 $log = $function($restore,$log);
3897 //If everything is ok, mark the insert flag
3898 if ($log) {
3899 $toinsert = true;
3903 //echo "After transformations<br />"; //Debug
3904 //print_object($log); //Debug
3906 //Now if $toinsert is set, insert the record
3907 if ($toinsert) {
3908 //echo "Inserting record<br />"; //Debug
3909 $status = insert_record("log",$log);
3911 return $status;
3914 //This function adjusts the instance field into course_modules. It's executed after
3915 //modules restore. There, we KNOW the new instance id !!
3916 function restore_check_instances($restore) {
3918 global $CFG;
3920 $status = true;
3922 //We are going to iterate over each course_module saved in backup_ids
3923 $course_modules = get_records_sql("SELECT old_id,new_id
3924 FROM {$CFG->prefix}backup_ids
3925 WHERE backup_code = '$restore->backup_unique_code' AND
3926 table_name = 'course_modules'");
3927 if ($course_modules) {
3928 foreach($course_modules as $cm) {
3929 //Get full record, using backup_getids
3930 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
3931 //Now we are going to the REAL course_modules to get its type (field module)
3932 $module = get_record("course_modules","id",$cm_module->new_id);
3933 if ($module) {
3934 //We know the module type id. Get the name from modules
3935 $type = get_record("modules","id",$module->module);
3936 if ($type) {
3937 //We know the type name and the old_id. Get its new_id
3938 //from backup_ids. It's the instance !!!
3939 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
3940 if ($instance) {
3941 //We have the new instance, so update the record in course_modules
3942 $module->instance = $instance->new_id;
3943 //print_object ($module); //Debug
3944 $status = update_record("course_modules",$module);
3945 } else {
3946 $status = false;
3948 } else {
3949 $status = false;
3951 } else {
3952 $status = false;
3958 return $status;
3961 //=====================================================================================
3962 //== ==
3963 //== XML Functions (SAX) ==
3964 //== ==
3965 //=====================================================================================
3967 //This is the class used to do all the xml parse
3968 class MoodleParser {
3970 var $level = 0; //Level we are
3971 var $counter = 0; //Counter
3972 var $tree = array(); //Array of levels we are
3973 var $content = ""; //Content under current level
3974 var $todo = ""; //What we hav to do when parsing
3975 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
3976 var $temp = ""; //Temp storage.
3977 var $preferences = ""; //Preferences about what to load !!
3978 var $finished = false; //Flag to say xml_parse to stop
3980 //This function is used to get the current contents property value
3981 //They are trimed (and converted from utf8 if needed)
3982 function getContents() {
3983 return trim($this->content);
3986 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3987 function startElementInfo($parser, $tagName, $attrs) {
3988 //Refresh properties
3989 $this->level++;
3990 $this->tree[$this->level] = $tagName;
3992 //Output something to avoid browser timeouts...
3993 backup_flush();
3995 //Check if we are into INFO zone
3996 //if ($this->tree[2] == "INFO") //Debug
3997 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4000 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
4001 function startElementRoles($parser, $tagName, $attrs) {
4002 //Refresh properties
4003 $this->level++;
4004 $this->tree[$this->level] = $tagName;
4006 //Output something to avoid browser timeouts...
4007 backup_flush();
4009 //Check if we are into INFO zone
4010 //if ($this->tree[2] == "INFO") //Debug
4011 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4015 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
4016 function startElementCourseHeader($parser, $tagName, $attrs) {
4017 //Refresh properties
4018 $this->level++;
4019 $this->tree[$this->level] = $tagName;
4021 //Output something to avoid browser timeouts...
4022 backup_flush();
4024 //Check if we are into COURSE_HEADER zone
4025 //if ($this->tree[3] == "HEADER") //Debug
4026 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4029 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
4030 function startElementBlocks($parser, $tagName, $attrs) {
4031 //Refresh properties
4032 $this->level++;
4033 $this->tree[$this->level] = $tagName;
4035 //Output something to avoid browser timeouts...
4036 backup_flush();
4038 //Check if we are into BLOCKS zone
4039 //if ($this->tree[3] == "BLOCKS") //Debug
4040 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4043 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
4044 function startElementSections($parser, $tagName, $attrs) {
4045 //Refresh properties
4046 $this->level++;
4047 $this->tree[$this->level] = $tagName;
4049 //Output something to avoid browser timeouts...
4050 backup_flush();
4052 //Check if we are into SECTIONS zone
4053 //if ($this->tree[3] == "SECTIONS") //Debug
4054 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4057 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4058 function startElementFormatData($parser, $tagName, $attrs) {
4059 //Refresh properties
4060 $this->level++;
4061 $this->tree[$this->level] = $tagName;
4063 //Output something to avoid browser timeouts...
4064 backup_flush();
4066 //Accumulate all the data inside this tag
4067 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
4068 if (!isset($this->temp)) {
4069 $this->temp = '';
4071 $this->temp .= "<".$tagName.">";
4074 //Check if we are into FORMATDATA zone
4075 //if ($this->tree[3] == "FORMATDATA") //Debug
4076 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4079 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4080 function startElementMetacourse($parser, $tagName, $attrs) {
4082 //Refresh properties
4083 $this->level++;
4084 $this->tree[$this->level] = $tagName;
4086 //Output something to avoid browser timeouts...
4087 backup_flush();
4089 //Check if we are into METACOURSE zone
4090 //if ($this->tree[3] == "METACOURSE") //Debug
4091 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4094 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4095 function startElementGradebook($parser, $tagName, $attrs) {
4097 //Refresh properties
4098 $this->level++;
4099 $this->tree[$this->level] = $tagName;
4101 //Output something to avoid browser timeouts...
4102 backup_flush();
4104 //Check if we are into GRADEBOOK zone
4105 //if ($this->tree[3] == "GRADEBOOK") //Debug
4106 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4108 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
4109 if (isset($this->tree[5]) and isset($this->tree[3])) {
4110 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")) {
4112 if (!isset($this->temp)) {
4113 $this->temp = "";
4115 $this->temp .= "<".$tagName.">";
4121 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
4122 function startElementUsers($parser, $tagName, $attrs) {
4123 //Refresh properties
4124 $this->level++;
4125 $this->tree[$this->level] = $tagName;
4127 //Check if we are into USERS zone
4128 //if ($this->tree[3] == "USERS") //Debug
4129 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4132 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
4133 function startElementMessages($parser, $tagName, $attrs) {
4134 //Refresh properties
4135 $this->level++;
4136 $this->tree[$this->level] = $tagName;
4138 //Output something to avoid browser timeouts...
4139 backup_flush();
4141 //Check if we are into MESSAGES zone
4142 //if ($this->tree[3] == "MESSAGES") //Debug
4143 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4145 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
4146 if (isset($this->tree[4]) and isset($this->tree[3])) {
4147 if (($this->tree[4] == "MESSAGE" || $this->tree[5] == "CONTACT" ) and ($this->tree[3] == "MESSAGES")) {
4148 if (!isset($this->temp)) {
4149 $this->temp = "";
4151 $this->temp .= "<".$tagName.">";
4155 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
4156 function startElementQuestions($parser, $tagName, $attrs) {
4157 //Refresh properties
4158 $this->level++;
4159 $this->tree[$this->level] = $tagName;
4161 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
4162 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
4163 //} //Debug
4165 //Output something to avoid browser timeouts...
4166 backup_flush();
4168 //Check if we are into QUESTION_CATEGORIES zone
4169 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
4170 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4172 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
4173 if (isset($this->tree[4]) and isset($this->tree[3])) {
4174 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
4175 if (!isset($this->temp)) {
4176 $this->temp = "";
4178 $this->temp .= "<".$tagName.">";
4183 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
4184 function startElementScales($parser, $tagName, $attrs) {
4185 //Refresh properties
4186 $this->level++;
4187 $this->tree[$this->level] = $tagName;
4189 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
4190 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
4191 //} //Debug
4193 //Output something to avoid browser timeouts...
4194 backup_flush();
4196 //Check if we are into SCALES zone
4197 //if ($this->tree[3] == "SCALES") //Debug
4198 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4200 //If we are under a SCALE tag under a SCALES zone, accumule it
4201 if (isset($this->tree[4]) and isset($this->tree[3])) {
4202 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
4203 if (!isset($this->temp)) {
4204 $this->temp = "";
4206 $this->temp .= "<".$tagName.">";
4211 function startElementGroups($parser, $tagName, $attrs) {
4212 //Refresh properties
4213 $this->level++;
4214 $this->tree[$this->level] = $tagName;
4216 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
4217 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
4218 //} //Debug
4220 //Output something to avoid browser timeouts...
4221 backup_flush();
4223 //Check if we are into GROUPS zone
4224 //if ($this->tree[3] == "GROUPS") //Debug
4225 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4227 //If we are under a GROUP tag under a GROUPS zone, accumule it
4228 if (isset($this->tree[4]) and isset($this->tree[3])) {
4229 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
4230 if (!isset($this->temp)) {
4231 $this->temp = "";
4233 $this->temp .= "<".$tagName.">";
4238 function startElementGroupings($parser, $tagName, $attrs) {
4239 //Refresh properties
4240 $this->level++;
4241 $this->tree[$this->level] = $tagName;
4243 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
4244 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
4245 //} //Debug
4247 //Output something to avoid browser timeouts...
4248 backup_flush();
4250 //Check if we are into GROUPINGS zone
4251 //if ($this->tree[3] == "GROUPINGS") //Debug
4252 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4254 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
4255 if (isset($this->tree[4]) and isset($this->tree[3])) {
4256 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
4257 if (!isset($this->temp)) {
4258 $this->temp = "";
4260 $this->temp .= "<".$tagName.">";
4265 function startElementGroupingsGroups($parser, $tagName, $attrs) {
4266 //Refresh properties
4267 $this->level++;
4268 $this->tree[$this->level] = $tagName;
4270 //if ($tagName == "GROUPINGGROUP" && $this->tree[3] == "GROUPINGSGROUPS") { //Debug
4271 // echo "<P>GROUPINGSGROUP: ".strftime ("%X",time()),"-"; //Debug
4272 //} //Debug
4274 //Output something to avoid browser timeouts...
4275 backup_flush();
4277 //Check if we are into GROUPINGSGROUPS zone
4278 //if ($this->tree[3] == "GROUPINGSGROUPS") //Debug
4279 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4281 //If we are under a GROUPINGGROUP tag under a GROUPINGSGROUPS zone, accumule it
4282 if (isset($this->tree[4]) and isset($this->tree[3])) {
4283 if (($this->tree[4] == "GROUPINGGROUP") and ($this->tree[3] == "GROUPINGSGROUPS")) {
4284 if (!isset($this->temp)) {
4285 $this->temp = "";
4287 $this->temp .= "<".$tagName.">";
4292 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
4293 function startElementEvents($parser, $tagName, $attrs) {
4294 //Refresh properties
4295 $this->level++;
4296 $this->tree[$this->level] = $tagName;
4298 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
4299 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
4300 //} //Debug
4302 //Output something to avoid browser timeouts...
4303 backup_flush();
4305 //Check if we are into EVENTS zone
4306 //if ($this->tree[3] == "EVENTS") //Debug
4307 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4309 //If we are under a EVENT tag under a EVENTS zone, accumule it
4310 if (isset($this->tree[4]) and isset($this->tree[3])) {
4311 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
4312 if (!isset($this->temp)) {
4313 $this->temp = "";
4315 $this->temp .= "<".$tagName.">";
4320 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
4321 function startElementModules($parser, $tagName, $attrs) {
4322 //Refresh properties
4323 $this->level++;
4324 $this->tree[$this->level] = $tagName;
4326 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
4327 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
4328 //} //Debug
4330 //Output something to avoid browser timeouts...
4331 backup_flush();
4333 //Check if we are into MODULES zone
4334 //if ($this->tree[3] == "MODULES") //Debug
4335 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4337 //If we are under a MOD tag under a MODULES zone, accumule it
4338 if (isset($this->tree[4]) and isset($this->tree[3])) {
4339 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
4340 if (!isset($this->temp)) {
4341 $this->temp = "";
4343 $this->temp .= "<".$tagName.">";
4348 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
4349 function startElementLogs($parser, $tagName, $attrs) {
4350 //Refresh properties
4351 $this->level++;
4352 $this->tree[$this->level] = $tagName;
4354 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
4355 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
4356 //} //Debug
4358 //Output something to avoid browser timeouts...
4359 backup_flush();
4361 //Check if we are into LOGS zone
4362 //if ($this->tree[3] == "LOGS") //Debug
4363 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4365 //If we are under a LOG tag under a LOGS zone, accumule it
4366 if (isset($this->tree[4]) and isset($this->tree[3])) {
4367 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
4368 if (!isset($this->temp)) {
4369 $this->temp = "";
4371 $this->temp .= "<".$tagName.">";
4376 //This is the startTag default handler we use when todo is undefined
4377 function startElement($parser, $tagName, $attrs) {
4378 $this->level++;
4379 $this->tree[$this->level] = $tagName;
4381 //Output something to avoid browser timeouts...
4382 backup_flush();
4384 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
4387 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
4388 function endElementInfo($parser, $tagName) {
4389 //Check if we are into INFO zone
4390 if ($this->tree[2] == "INFO") {
4391 //if (trim($this->content)) //Debug
4392 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4393 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4394 //Dependig of different combinations, do different things
4395 if ($this->level == 3) {
4396 switch ($tagName) {
4397 case "NAME":
4398 $this->info->backup_name = $this->getContents();
4399 break;
4400 case "MOODLE_VERSION":
4401 $this->info->backup_moodle_version = $this->getContents();
4402 break;
4403 case "MOODLE_RELEASE":
4404 $this->info->backup_moodle_release = $this->getContents();
4405 break;
4406 case "BACKUP_VERSION":
4407 $this->info->backup_backup_version = $this->getContents();
4408 break;
4409 case "BACKUP_RELEASE":
4410 $this->info->backup_backup_release = $this->getContents();
4411 break;
4412 case "DATE":
4413 $this->info->backup_date = $this->getContents();
4414 break;
4415 case "ORIGINAL_WWWROOT":
4416 $this->info->original_wwwroot = $this->getContents();
4417 break;
4418 case "MNET_EXTERNALUSERS":
4419 $this->info->mnet_externalusers = $this->getContents();
4420 break;
4423 if ($this->tree[3] == "DETAILS") {
4424 if ($this->level == 4) {
4425 switch ($tagName) {
4426 case "METACOURSE":
4427 $this->info->backup_metacourse = $this->getContents();
4428 break;
4429 case "USERS":
4430 $this->info->backup_users = $this->getContents();
4431 break;
4432 case "LOGS":
4433 $this->info->backup_logs = $this->getContents();
4434 break;
4435 case "USERFILES":
4436 $this->info->backup_user_files = $this->getContents();
4437 break;
4438 case "COURSEFILES":
4439 $this->info->backup_course_files = $this->getContents();
4440 break;
4441 case "SITEFILES":
4442 $this->info->backup_site_files = $this->getContents();
4443 break;
4444 case "MESSAGES":
4445 $this->info->backup_messages = $this->getContents();
4446 break;
4447 case 'BLOCKFORMAT':
4448 $this->info->backup_block_format = $this->getContents();
4449 break;
4452 if ($this->level == 5) {
4453 switch ($tagName) {
4454 case "NAME":
4455 $this->info->tempName = $this->getContents();
4456 break;
4457 case "INCLUDED":
4458 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
4459 break;
4460 case "USERINFO":
4461 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
4462 break;
4465 if ($this->level == 7) {
4466 switch ($tagName) {
4467 case "ID":
4468 $this->info->tempId = $this->getContents();
4469 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
4470 break;
4471 case "NAME":
4472 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
4473 break;
4474 case "INCLUDED":
4475 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
4476 break;
4477 case "USERINFO":
4478 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
4479 break;
4485 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4486 //Speed up a lot (avoid parse all)
4487 if ($tagName == "INFO") {
4488 $this->finished = true;
4491 //Clear things
4492 $this->tree[$this->level] = "";
4493 $this->level--;
4494 $this->content = "";
4498 function endElementRoles($parser, $tagName) {
4499 //Check if we are into INFO zone
4500 if ($this->tree[2] == "ROLES") {
4502 if ($this->tree[3] == "ROLE") {
4503 if ($this->level == 4) {
4504 switch ($tagName) {
4505 case "NAME":
4506 $this->info->tempname = $this->getContents();
4508 break;
4509 case "SHORTNAME":
4510 $this->info->tempshortname = $this->getContents();
4511 break;
4512 case "ID": // this is the old id
4513 $this->info->tempid = $this->getContents();
4514 break;
4517 if ($this->level == 6) {
4518 switch ($tagName) {
4519 case "NAME":
4520 $this->info->roles[$this->info->tempid]->name = $this->info->tempname;
4521 $this->info->roles[$this->info->tempid]->shortname = $this->info->tempshortname;
4523 $this->info->tempcapname = $this->getContents();
4524 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
4525 break;
4526 case "PERMISSION":
4527 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
4528 break;
4529 case "TIMEMODIFIED":
4530 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
4531 break;
4532 case "MODIFIERID":
4533 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
4534 break;
4540 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
4541 //Speed up a lot (avoid parse all)
4542 if ($tagName == "ROLES") {
4543 $this->finished = true;
4546 //Clear things
4547 $this->tree[$this->level] = "";
4548 $this->level--;
4549 $this->content = "";
4553 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
4554 function endElementCourseHeader($parser, $tagName) {
4555 //Check if we are into COURSE_HEADER zone
4556 if ($this->tree[3] == "HEADER") {
4557 //if (trim($this->content)) //Debug
4558 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4559 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4560 //Dependig of different combinations, do different things
4561 if ($this->level == 4) {
4562 switch ($tagName) {
4563 case "ID":
4564 $this->info->course_id = $this->getContents();
4565 break;
4566 case "PASSWORD":
4567 $this->info->course_password = $this->getContents();
4568 break;
4569 case "FULLNAME":
4570 $this->info->course_fullname = $this->getContents();
4571 break;
4572 case "SHORTNAME":
4573 $this->info->course_shortname = $this->getContents();
4574 break;
4575 case "IDNUMBER":
4576 $this->info->course_idnumber = $this->getContents();
4577 break;
4578 case "SUMMARY":
4579 $this->info->course_summary = $this->getContents();
4580 break;
4581 case "FORMAT":
4582 $this->info->course_format = $this->getContents();
4583 break;
4584 case "SHOWGRADES":
4585 $this->info->course_showgrades = $this->getContents();
4586 break;
4587 case "BLOCKINFO":
4588 $this->info->blockinfo = $this->getContents();
4589 break;
4590 case "NEWSITEMS":
4591 $this->info->course_newsitems = $this->getContents();
4592 break;
4593 case "TEACHER":
4594 $this->info->course_teacher = $this->getContents();
4595 break;
4596 case "TEACHERS":
4597 $this->info->course_teachers = $this->getContents();
4598 break;
4599 case "STUDENT":
4600 $this->info->course_student = $this->getContents();
4601 break;
4602 case "STUDENTS":
4603 $this->info->course_students = $this->getContents();
4604 break;
4605 case "GUEST":
4606 $this->info->course_guest = $this->getContents();
4607 break;
4608 case "STARTDATE":
4609 $this->info->course_startdate = $this->getContents();
4610 break;
4611 case "NUMSECTIONS":
4612 $this->info->course_numsections = $this->getContents();
4613 break;
4614 //case "SHOWRECENT": INFO: This is out in 1.3
4615 // $this->info->course_showrecent = $this->getContents();
4616 // break;
4617 case "MAXBYTES":
4618 $this->info->course_maxbytes = $this->getContents();
4619 break;
4620 case "SHOWREPORTS":
4621 $this->info->course_showreports = $this->getContents();
4622 break;
4623 case "GROUPMODE":
4624 $this->info->course_groupmode = $this->getContents();
4625 break;
4626 case "GROUPMODEFORCE":
4627 $this->info->course_groupmodeforce = $this->getContents();
4628 break;
4629 case "DEFAULTGROUPINGID":
4630 $this->info->course_defaultgroupingid = $this->getContents();
4631 break;
4632 case "LANG":
4633 $this->info->course_lang = $this->getContents();
4634 break;
4635 case "THEME":
4636 $this->info->course_theme = $this->getContents();
4637 break;
4638 case "COST":
4639 $this->info->course_cost = $this->getContents();
4640 break;
4641 case "CURRENCY":
4642 $this->info->course_currency = $this->getContents();
4643 break;
4644 case "MARKER":
4645 $this->info->course_marker = $this->getContents();
4646 break;
4647 case "VISIBLE":
4648 $this->info->course_visible = $this->getContents();
4649 break;
4650 case "HIDDENSECTIONS":
4651 $this->info->course_hiddensections = $this->getContents();
4652 break;
4653 case "TIMECREATED":
4654 $this->info->course_timecreated = $this->getContents();
4655 break;
4656 case "TIMEMODIFIED":
4657 $this->info->course_timemodified = $this->getContents();
4658 break;
4659 case "METACOURSE":
4660 $this->info->course_metacourse = $this->getContents();
4661 break;
4662 case "EXPIRENOTIFY":
4663 $this->info->course_expirynotify = $this->getContents();
4664 break;
4665 case "NOTIFYSTUDENTS":
4666 $this->info->course_notifystudents = $this->getContents();
4667 break;
4668 case "EXPIRYTHRESHOLD":
4669 $this->info->course_expirythreshold = $this->getContents();
4670 break;
4671 case "ENROLLABLE":
4672 $this->info->course_enrollable = $this->getContents();
4673 break;
4674 case "ENROLSTARTDATE":
4675 $this->info->course_enrolstartdate = $this->getContents();
4676 break;
4677 case "ENROLENDDATE":
4678 $this->info->course_enrolenddate = $this->getContents();
4679 break;
4680 case "ENROLPERIOD":
4681 $this->info->course_enrolperiod = $this->getContents();
4682 break;
4685 if ($this->tree[4] == "CATEGORY") {
4686 if ($this->level == 5) {
4687 switch ($tagName) {
4688 case "ID":
4689 $this->info->category->id = $this->getContents();
4690 break;
4691 case "NAME":
4692 $this->info->category->name = $this->getContents();
4693 break;
4698 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
4699 if ($this->level == 6) {
4700 switch ($tagName) {
4701 case "NAME":
4702 $this->info->tempname = $this->getContents();
4703 break;
4704 case "SHORTNAME":
4705 $this->info->tempshortname = $this->getContents();
4706 break;
4707 case "ID":
4708 $this->info->tempid = $this->getContents();
4709 break;
4713 if ($this->level == 8) {
4714 switch ($tagName) {
4715 case "USERID":
4716 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4717 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4718 $this->info->tempuser = $this->getContents();
4719 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4720 break;
4721 case "HIDDEN":
4722 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4723 break;
4724 case "TIMESTART":
4725 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4726 break;
4727 case "TIMEEND":
4728 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4729 break;
4730 case "TIMEMODIFIED":
4731 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4732 break;
4733 case "MODIFIERID":
4734 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4735 break;
4736 case "ENROL":
4737 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4738 break;
4739 case "SORTORDER":
4740 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4741 break;
4745 } /// ends role_assignments
4747 if ($this->tree[4] == "ROLES_OVERRIDES") {
4748 if ($this->level == 6) {
4749 switch ($tagName) {
4750 case "NAME":
4751 $this->info->tempname = $this->getContents();
4752 break;
4753 case "SHORTNAME":
4754 $this->info->tempshortname = $this->getContents();
4755 break;
4756 case "ID":
4757 $this->info->tempid = $this->getContents();
4758 break;
4762 if ($this->level == 8) {
4763 switch ($tagName) {
4764 case "NAME":
4765 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4766 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4767 $this->info->tempname = $this->getContents(); // change to name of capability
4768 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4769 break;
4770 case "PERMISSION":
4771 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4772 break;
4773 case "TIMEMODIFIED":
4774 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4775 break;
4776 case "MODIFIERID":
4777 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4778 break;
4781 } /// ends role_overrides
4784 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
4785 //Speed up a lot (avoid parse all)
4786 if ($tagName == "HEADER") {
4787 $this->finished = true;
4790 //Clear things
4791 $this->tree[$this->level] = "";
4792 $this->level--;
4793 $this->content = "";
4797 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
4798 function endElementBlocks($parser, $tagName) {
4799 //Check if we are into BLOCKS zone
4800 if ($this->tree[3] == 'BLOCKS') {
4801 //if (trim($this->content)) //Debug
4802 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4803 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4804 //Dependig of different combinations, do different things
4805 if ($this->level == 4) {
4806 switch ($tagName) {
4807 case 'BLOCK':
4808 //We've finalized a block, get it
4809 $this->info->instances[] = $this->info->tempinstance;
4810 unset($this->info->tempinstance);
4811 break;
4812 default:
4813 die($tagName);
4816 if ($this->level == 5) {
4817 switch ($tagName) {
4818 case 'ID':
4819 $this->info->tempinstance->id = $this->getContents();
4820 case 'NAME':
4821 $this->info->tempinstance->name = $this->getContents();
4822 break;
4823 case 'PAGEID':
4824 $this->info->tempinstance->pageid = $this->getContents();
4825 break;
4826 case 'PAGETYPE':
4827 $this->info->tempinstance->pagetype = $this->getContents();
4828 break;
4829 case 'POSITION':
4830 $this->info->tempinstance->position = $this->getContents();
4831 break;
4832 case 'WEIGHT':
4833 $this->info->tempinstance->weight = $this->getContents();
4834 break;
4835 case 'VISIBLE':
4836 $this->info->tempinstance->visible = $this->getContents();
4837 break;
4838 case 'CONFIGDATA':
4839 $this->info->tempinstance->configdata = $this->getContents();
4840 break;
4841 default:
4842 break;
4846 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
4847 if ($this->level == 7) {
4848 switch ($tagName) {
4849 case "NAME":
4850 $this->info->tempname = $this->getContents();
4851 break;
4852 case "SHORTNAME":
4853 $this->info->tempshortname = $this->getContents();
4854 break;
4855 case "ID":
4856 $this->info->tempid = $this->getContents(); // temp roleid
4857 break;
4861 if ($this->level == 9) {
4863 switch ($tagName) {
4864 case "USERID":
4865 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4867 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4869 $this->info->tempuser = $this->getContents();
4871 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4872 break;
4873 case "HIDDEN":
4874 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4875 break;
4876 case "TIMESTART":
4877 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4878 break;
4879 case "TIMEEND":
4880 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4881 break;
4882 case "TIMEMODIFIED":
4883 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4884 break;
4885 case "MODIFIERID":
4886 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4887 break;
4888 case "ENROL":
4889 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4890 break;
4891 case "SORTORDER":
4892 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4893 break;
4897 } /// ends role_assignments
4899 if ($this->tree[5] == "ROLES_OVERRIDES") {
4900 if ($this->level == 7) {
4901 switch ($tagName) {
4902 case "NAME":
4903 $this->info->tempname = $this->getContents();
4904 break;
4905 case "SHORTNAME":
4906 $this->info->tempshortname = $this->getContents();
4907 break;
4908 case "ID":
4909 $this->info->tempid = $this->getContents(); // temp roleid
4910 break;
4914 if ($this->level == 9) {
4915 switch ($tagName) {
4916 case "NAME":
4918 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4919 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4920 $this->info->tempname = $this->getContents(); // change to name of capability
4921 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4922 break;
4923 case "PERMISSION":
4924 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4925 break;
4926 case "TIMEMODIFIED":
4927 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4928 break;
4929 case "MODIFIERID":
4930 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4931 break;
4934 } /// ends role_overrides
4937 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
4938 //Speed up a lot (avoid parse all)
4939 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
4940 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
4941 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
4942 $this->finished = true;
4945 //Clear things
4946 $this->tree[$this->level] = '';
4947 $this->level--;
4948 $this->content = "";
4951 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
4952 function endElementSections($parser, $tagName) {
4953 //Check if we are into SECTIONS zone
4954 if ($this->tree[3] == "SECTIONS") {
4955 //if (trim($this->content)) //Debug
4956 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4957 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4958 //Dependig of different combinations, do different things
4959 if ($this->level == 4) {
4960 switch ($tagName) {
4961 case "SECTION":
4962 //We've finalized a section, get it
4963 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
4964 unset($this->info->tempsection);
4967 if ($this->level == 5) {
4968 switch ($tagName) {
4969 case "ID":
4970 $this->info->tempsection->id = $this->getContents();
4971 break;
4972 case "NUMBER":
4973 $this->info->tempsection->number = $this->getContents();
4974 break;
4975 case "SUMMARY":
4976 $this->info->tempsection->summary = $this->getContents();
4977 break;
4978 case "VISIBLE":
4979 $this->info->tempsection->visible = $this->getContents();
4980 break;
4983 if ($this->level == 6) {
4984 switch ($tagName) {
4985 case "MOD":
4986 if (!isset($this->info->tempmod->groupmode)) {
4987 $this->info->tempmod->groupmode = 0;
4989 if (!isset($this->info->tempmod->groupingid)) {
4990 $this->info->tempmod->groupingid = 0;
4992 if (!isset($this->info->tempmod->groupmembersonly)) {
4993 $this->info->tempmod->groupmembersonly = 0;
4996 //We've finalized a mod, get it
4997 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
4998 $this->info->tempmod->type;
4999 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
5000 $this->info->tempmod->instance;
5001 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
5002 $this->info->tempmod->added;
5003 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
5004 $this->info->tempmod->score;
5005 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
5006 $this->info->tempmod->indent;
5007 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
5008 $this->info->tempmod->visible;
5009 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
5010 $this->info->tempmod->groupmode;
5011 $this->info->tempsection->mods[$this->info->tempmod->id]->groupingid =
5012 $this->info->tempmod->groupingid;
5013 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmembersonly =
5014 $this->info->tempmod->groupmembersonly;
5016 unset($this->info->tempmod);
5019 if ($this->level == 7) {
5020 switch ($tagName) {
5021 case "ID":
5022 $this->info->tempmod->id = $this->getContents();
5023 break;
5024 case "TYPE":
5025 $this->info->tempmod->type = $this->getContents();
5026 break;
5027 case "INSTANCE":
5028 $this->info->tempmod->instance = $this->getContents();
5029 break;
5030 case "ADDED":
5031 $this->info->tempmod->added = $this->getContents();
5032 break;
5033 case "SCORE":
5034 $this->info->tempmod->score = $this->getContents();
5035 break;
5036 case "INDENT":
5037 $this->info->tempmod->indent = $this->getContents();
5038 break;
5039 case "VISIBLE":
5040 $this->info->tempmod->visible = $this->getContents();
5041 break;
5042 case "GROUPMODE":
5043 $this->info->tempmod->groupmode = $this->getContents();
5044 break;
5045 case "GROUPINGID":
5046 $this->info->tempmod->groupingid = $this->getContents();
5047 break;
5048 case "GROUPMEMBERSONLY":
5049 $this->info->tempmod->groupmembersonly = $this->getContents();
5050 break;
5051 default:
5052 break;
5056 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
5058 if ($this->level == 9) {
5059 switch ($tagName) {
5060 case "NAME":
5061 $this->info->tempname = $this->getContents();
5062 break;
5063 case "SHORTNAME":
5064 $this->info->tempshortname = $this->getContents();
5065 break;
5066 case "ID":
5067 $this->info->tempid = $this->getContents(); // temp roleid
5068 break;
5072 if ($this->level == 11) {
5073 switch ($tagName) {
5074 case "USERID":
5075 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5077 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5079 $this->info->tempuser = $this->getContents();
5081 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
5082 break;
5083 case "HIDDEN":
5084 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
5085 break;
5086 case "TIMESTART":
5087 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
5088 break;
5089 case "TIMEEND":
5090 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
5091 break;
5092 case "TIMEMODIFIED":
5093 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
5094 break;
5095 case "MODIFIERID":
5096 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
5097 break;
5098 case "ENROL":
5099 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
5100 break;
5101 case "SORTORDER":
5102 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
5103 break;
5107 } /// ends role_assignments
5109 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
5110 if ($this->level == 9) {
5111 switch ($tagName) {
5112 case "NAME":
5113 $this->info->tempname = $this->getContents();
5114 break;
5115 case "SHORTNAME":
5116 $this->info->tempshortname = $this->getContents();
5117 break;
5118 case "ID":
5119 $this->info->tempid = $this->getContents(); // temp roleid
5120 break;
5124 if ($this->level == 11) {
5125 switch ($tagName) {
5126 case "NAME":
5128 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5129 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5130 $this->info->tempname = $this->getContents(); // change to name of capability
5131 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5132 break;
5133 case "PERMISSION":
5134 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5135 break;
5136 case "TIMEMODIFIED":
5137 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5138 break;
5139 case "MODIFIERID":
5140 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5141 break;
5144 } /// ends role_overrides
5148 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
5149 //Speed up a lot (avoid parse all)
5150 if ($tagName == "SECTIONS") {
5151 $this->finished = true;
5154 //Clear things
5155 $this->tree[$this->level] = "";
5156 $this->level--;
5157 $this->content = "";
5161 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
5162 function endElementFormatData($parser, $tagName) {
5163 //Check if we are into FORMATDATA zone
5164 if ($this->tree[3] == 'FORMATDATA') {
5165 if (!isset($this->temp)) {
5166 $this->temp = '';
5168 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5171 if($tagName=='FORMATDATA') {
5172 //Did we have any data? If not don't bother
5173 if($this->temp!='<FORMATDATA></FORMATDATA>') {
5174 //Prepend XML standard header to info gathered
5175 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5176 $this->temp='';
5178 //Call to xmlize for this portion of xml data (the FORMATDATA block)
5179 $this->info->format_data = xmlize($xml_data,0);
5181 //Stop parsing at end of FORMATDATA
5182 $this->finished=true;
5185 //Clear things
5186 $this->tree[$this->level] = "";
5187 $this->level--;
5188 $this->content = "";
5191 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
5192 function endElementMetacourse($parser, $tagName) {
5193 //Check if we are into METACOURSE zone
5194 if ($this->tree[3] == 'METACOURSE') {
5195 //if (trim($this->content)) //Debug
5196 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5197 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5198 //Dependig of different combinations, do different things
5199 if ($this->level == 5) {
5200 switch ($tagName) {
5201 case 'CHILD':
5202 //We've finalized a child, get it
5203 $this->info->childs[] = $this->info->tempmeta;
5204 unset($this->info->tempmeta);
5205 break;
5206 case 'PARENT':
5207 //We've finalized a parent, get it
5208 $this->info->parents[] = $this->info->tempmeta;
5209 unset($this->info->tempmeta);
5210 break;
5211 default:
5212 die($tagName);
5215 if ($this->level == 6) {
5216 switch ($tagName) {
5217 case 'ID':
5218 $this->info->tempmeta->id = $this->getContents();
5219 break;
5220 case 'IDNUMBER':
5221 $this->info->tempmeta->idnumber = $this->getContents();
5222 break;
5223 case 'SHORTNAME':
5224 $this->info->tempmeta->shortname = $this->getContents();
5225 break;
5230 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
5231 //Speed up a lot (avoid parse all)
5232 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
5233 $this->finished = true;
5236 //Clear things
5237 $this->tree[$this->level] = '';
5238 $this->level--;
5239 $this->content = "";
5242 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
5243 function endElementGradebook($parser, $tagName) {
5244 //Check if we are into GRADEBOOK zone
5245 if ($this->tree[3] == "GRADEBOOK") {
5246 //if (trim($this->content)) //Debug
5247 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5248 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5249 //Acumulate data to info (content + close tag)
5250 //Reconvert: strip htmlchars again and trim to generate xml data
5251 if (!isset($this->temp)) {
5252 $this->temp = "";
5254 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5255 // We have finished outcome, grade_category or grade_item, reset accumulated
5256 // data because they are close tags
5257 if ($this->level == 4) {
5258 $this->temp = "";
5260 //If we've finished a grade item, xmlize it an save to db
5261 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
5262 //Prepend XML standard header to info gathered
5263 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5264 //Call to xmlize for this portion of xml data (one PREFERENCE)
5265 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5266 $data = xmlize($xml_data,0);
5267 //echo strftime ("%X",time())."<p>"; //Debug
5268 //traverse_xmlize($data); //Debug
5269 //print_object ($GLOBALS['traverse_array']); //Debug
5270 //$GLOBALS['traverse_array']=""; //Debug
5271 //Now, save data to db. We'll use it later
5272 //Get id and status from data
5273 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
5274 $this->counter++;
5275 //Save to db
5277 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
5278 null,$data);
5279 //Create returning info
5280 $this->info = $this->counter;
5281 //Reset temp
5283 unset($this->temp);
5286 //If we've finished a grade_category, xmlize it an save to db
5287 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
5288 //Prepend XML standard header to info gathered
5289 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5290 //Call to xmlize for this portion of xml data (one CATECORY)
5291 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5292 $data = xmlize($xml_data,0);
5293 //echo strftime ("%X",time())."<p>"; //Debug
5294 //traverse_xmlize($data); //Debug
5295 //print_object ($GLOBALS['traverse_array']); //Debug
5296 //$GLOBALS['traverse_array']=""; //Debug
5297 //Now, save data to db. We'll use it later
5298 //Get id and status from data
5299 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
5300 $this->counter++;
5301 //Save to db
5302 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
5303 null,$data);
5304 //Create returning info
5305 $this->info = $this->counter;
5306 //Reset temp
5307 unset($this->temp);
5310 //If we've finished a grade_outcome, xmlize it an save to db
5311 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
5312 //Prepend XML standard header to info gathered
5313 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5314 //Call to xmlize for this portion of xml data (one CATECORY)
5315 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5316 $data = xmlize($xml_data,0);
5317 //echo strftime ("%X",time())."<p>"; //Debug
5318 //traverse_xmlize($data); //Debug
5319 //print_object ($GLOBALS['traverse_array']); //Debug
5320 //$GLOBALS['traverse_array']=""; //Debug
5321 //Now, save data to db. We'll use it later
5322 //Get id and status from data
5323 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
5324 $this->counter++;
5325 //Save to db
5326 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
5327 null,$data);
5328 //Create returning info
5329 $this->info = $this->counter;
5330 //Reset temp
5331 unset($this->temp);
5334 //If we've finished a grade_outcomes_course, xmlize it an save to db
5335 if (($this->level == 5) and ($tagName == "GRADE_OUTCOMES_COURSE")) {
5336 //Prepend XML standard header to info gathered
5337 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5338 //Call to xmlize for this portion of xml data (one CATECORY)
5339 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5340 $data = xmlize($xml_data,0);
5341 //echo strftime ("%X",time())."<p>"; //Debug
5342 //traverse_xmlize($data); //Debug
5343 //print_object ($GLOBALS['traverse_array']); //Debug
5344 //$GLOBALS['traverse_array']=""; //Debug
5345 //Now, save data to db. We'll use it later
5346 //Get id and status from data
5347 $outcomes_course_id = $data["GRADE_OUTCOMES_COURSE"]["#"]["ID"]["0"]["#"];
5348 $this->counter++;
5349 //Save to db
5350 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_courses' ,$outcomes_course_id,
5351 null,$data);
5352 //Create returning info
5353 $this->info = $this->counter;
5354 //Reset temp
5355 unset($this->temp);
5358 if (($this->level == 5) and ($tagName == "GRADE_CATEGORIES_HISTORY")) {
5359 //Prepend XML standard header to info gathered
5360 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5361 //Call to xmlize for this portion of xml data (one PREFERENCE)
5362 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5363 $data = xmlize($xml_data,0);
5364 //echo strftime ("%X",time())."<p>"; //Debug
5365 //traverse_xmlize($data); //Debug
5366 //print_object ($GLOBALS['traverse_array']); //Debug
5367 //$GLOBALS['traverse_array']=""; //Debug
5368 //Now, save data to db. We'll use it later
5369 //Get id and status from data
5370 $id = $data["GRADE_CATEGORIES_HISTORY"]["#"]["ID"]["0"]["#"];
5371 $this->counter++;
5372 //Save to db
5374 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories_history', $id,
5375 null,$data);
5376 //Create returning info
5377 $this->info = $this->counter;
5378 //Reset temp
5380 unset($this->temp);
5383 if (($this->level == 5) and ($tagName == "GRADE_GRADES_HISTORY")) {
5384 //Prepend XML standard header to info gathered
5385 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5386 //Call to xmlize for this portion of xml data (one PREFERENCE)
5387 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5388 $data = xmlize($xml_data,0);
5389 //echo strftime ("%X",time())."<p>"; //Debug
5390 //traverse_xmlize($data); //Debug
5391 //print_object ($GLOBALS['traverse_array']); //Debug
5392 //$GLOBALS['traverse_array']=""; //Debug
5393 //Now, save data to db. We'll use it later
5394 //Get id and status from data
5395 $id = $data["GRADE_GRADES_HISTORY"]["#"]["ID"]["0"]["#"];
5396 $this->counter++;
5397 //Save to db
5399 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_history', $id,
5400 null,$data);
5401 //Create returning info
5402 $this->info = $this->counter;
5403 //Reset temp
5405 unset($this->temp);
5408 if (($this->level == 5) and ($tagName == "GRADE_TEXT_HISTORY")) {
5409 //Prepend XML standard header to info gathered
5410 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5411 //Call to xmlize for this portion of xml data (one PREFERENCE)
5412 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5413 $data = xmlize($xml_data,0);
5414 //echo strftime ("%X",time())."<p>"; //Debug
5415 //traverse_xmlize($data); //Debug
5416 //print_object ($GLOBALS['traverse_array']); //Debug
5417 //$GLOBALS['traverse_array']=""; //Debug
5418 //Now, save data to db. We'll use it later
5419 //Get id and status from data
5420 $id = $data["GRADE_TEXT_HISTORY"]["#"]["ID"]["0"]["#"];
5421 $this->counter++;
5422 //Save to db
5423 $status = backup_putid($this->preferences->backup_unique_code, 'grade_grades_text_history', $id,
5424 null,$data);
5425 //Create returning info
5426 $this->info = $this->counter;
5427 //Reset temp
5429 unset($this->temp);
5432 if (($this->level == 5) and ($tagName == "GRADE_ITEM_HISTORY")) {
5433 //Prepend XML standard header to info gathered
5434 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5435 //Call to xmlize for this portion of xml data (one PREFERENCE)
5436 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5437 $data = xmlize($xml_data,0);
5438 //echo strftime ("%X",time())."<p>"; //Debug
5439 //traverse_xmlize($data); //Debug
5440 //print_object ($GLOBALS['traverse_array']); //Debug
5441 //$GLOBALS['traverse_array']=""; //Debug
5442 //Now, save data to db. We'll use it later
5443 //Get id and status from data
5444 $id = $data["GRADE_ITEM_HISTORY"]["#"]["ID"]["0"]["#"];
5445 $this->counter++;
5446 //Save to db
5448 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items_history', $id,
5449 null,$data);
5450 //Create returning info
5451 $this->info = $this->counter;
5452 //Reset temp
5454 unset($this->temp);
5457 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME_HISTORY")) {
5458 //Prepend XML standard header to info gathered
5459 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5460 //Call to xmlize for this portion of xml data (one PREFERENCE)
5461 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5462 $data = xmlize($xml_data,0);
5463 //echo strftime ("%X",time())."<p>"; //Debug
5464 //traverse_xmlize($data); //Debug
5465 //print_object ($GLOBALS['traverse_array']); //Debug
5466 //$GLOBALS['traverse_array']=""; //Debug
5467 //Now, save data to db. We'll use it later
5468 //Get id and status from data
5469 $id = $data["GRADE_OUTCOME_HISTORY"]["#"]["ID"]["0"]["#"];
5470 $this->counter++;
5471 //Save to db
5473 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes_history', $id,
5474 null,$data);
5475 //Create returning info
5476 $this->info = $this->counter;
5477 //Reset temp
5479 unset($this->temp);
5483 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
5484 //Speed up a lot (avoid parse all)
5485 if ($tagName == "GRADEBOOK" and $this->level == 3) {
5486 $this->finished = true;
5487 $this->counter = 0;
5490 //Clear things
5491 $this->tree[$this->level] = "";
5492 $this->level--;
5493 $this->content = "";
5497 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
5498 function endElementUsers($parser, $tagName) {
5499 global $CFG;
5500 //Check if we are into USERS zone
5501 if ($this->tree[3] == "USERS") {
5502 //if (trim($this->content)) //Debug
5503 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5504 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5505 //Dependig of different combinations, do different things
5506 if ($this->level == 4) {
5507 switch ($tagName) {
5508 case "USER":
5509 //Increment counter
5510 $this->counter++;
5511 //Save to db, only save if record not already exist
5512 // if there already is an new_id for this entry, just use that new_id?
5513 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
5514 if (isset($newuser->new_id)) {
5515 $newid = $newuser->new_id;
5516 } else {
5517 $newid = null;
5520 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
5521 $newid,$this->info->tempuser);
5523 //Do some output
5524 if ($this->counter % 10 == 0) {
5525 if (!defined('RESTORE_SILENTLY')) {
5526 echo ".";
5527 if ($this->counter % 200 == 0) {
5528 echo "<br />";
5531 backup_flush(300);
5534 //Delete temp obejct
5535 unset($this->info->tempuser);
5536 break;
5539 if ($this->level == 5) {
5540 switch ($tagName) {
5541 case "ID":
5542 $this->info->users[$this->getContents()] = $this->getContents();
5543 $this->info->tempuser->id = $this->getContents();
5544 break;
5545 case "AUTH":
5546 $this->info->tempuser->auth = $this->getContents();
5547 break;
5548 case "CONFIRMED":
5549 $this->info->tempuser->confirmed = $this->getContents();
5550 break;
5551 case "POLICYAGREED":
5552 $this->info->tempuser->policyagreed = $this->getContents();
5553 break;
5554 case "DELETED":
5555 $this->info->tempuser->deleted = $this->getContents();
5556 break;
5557 case "USERNAME":
5558 $this->info->tempuser->username = $this->getContents();
5559 break;
5560 case "PASSWORD":
5561 $this->info->tempuser->password = $this->getContents();
5562 break;
5563 case "IDNUMBER":
5564 $this->info->tempuser->idnumber = $this->getContents();
5565 break;
5566 case "FIRSTNAME":
5567 $this->info->tempuser->firstname = $this->getContents();
5568 break;
5569 case "LASTNAME":
5570 $this->info->tempuser->lastname = $this->getContents();
5571 break;
5572 case "EMAIL":
5573 $this->info->tempuser->email = $this->getContents();
5574 break;
5575 case "EMAILSTOP":
5576 $this->info->tempuser->emailstop = $this->getContents();
5577 break;
5578 case "ICQ":
5579 $this->info->tempuser->icq = $this->getContents();
5580 break;
5581 case "SKYPE":
5582 $this->info->tempuser->skype = $this->getContents();
5583 break;
5584 case "AIM":
5585 $this->info->tempuser->aim = $this->getContents();
5586 break;
5587 case "YAHOO":
5588 $this->info->tempuser->yahoo = $this->getContents();
5589 break;
5590 case "MSN":
5591 $this->info->tempuser->msn = $this->getContents();
5592 break;
5593 case "PHONE1":
5594 $this->info->tempuser->phone1 = $this->getContents();
5595 break;
5596 case "PHONE2":
5597 $this->info->tempuser->phone2 = $this->getContents();
5598 break;
5599 case "INSTITUTION":
5600 $this->info->tempuser->institution = $this->getContents();
5601 break;
5602 case "DEPARTMENT":
5603 $this->info->tempuser->department = $this->getContents();
5604 break;
5605 case "ADDRESS":
5606 $this->info->tempuser->address = $this->getContents();
5607 break;
5608 case "CITY":
5609 $this->info->tempuser->city = $this->getContents();
5610 break;
5611 case "COUNTRY":
5612 $this->info->tempuser->country = $this->getContents();
5613 break;
5614 case "LANG":
5615 $this->info->tempuser->lang = $this->getContents();
5616 break;
5617 case "THEME":
5618 $this->info->tempuser->theme = $this->getContents();
5619 break;
5620 case "TIMEZONE":
5621 $this->info->tempuser->timezone = $this->getContents();
5622 break;
5623 case "FIRSTACCESS":
5624 $this->info->tempuser->firstaccess = $this->getContents();
5625 break;
5626 case "LASTACCESS":
5627 $this->info->tempuser->lastaccess = $this->getContents();
5628 break;
5629 case "LASTLOGIN":
5630 $this->info->tempuser->lastlogin = $this->getContents();
5631 break;
5632 case "CURRENTLOGIN":
5633 $this->info->tempuser->currentlogin = $this->getContents();
5634 break;
5635 case "LASTIP":
5636 $this->info->tempuser->lastip = $this->getContents();
5637 break;
5638 case "SECRET":
5639 $this->info->tempuser->secret = $this->getContents();
5640 break;
5641 case "PICTURE":
5642 $this->info->tempuser->picture = $this->getContents();
5643 break;
5644 case "URL":
5645 $this->info->tempuser->url = $this->getContents();
5646 break;
5647 case "DESCRIPTION":
5648 $this->info->tempuser->description = $this->getContents();
5649 break;
5650 case "MAILFORMAT":
5651 $this->info->tempuser->mailformat = $this->getContents();
5652 break;
5653 case "MAILDIGEST":
5654 $this->info->tempuser->maildigest = $this->getContents();
5655 break;
5656 case "MAILDISPLAY":
5657 $this->info->tempuser->maildisplay = $this->getContents();
5658 break;
5659 case "HTMLEDITOR":
5660 $this->info->tempuser->htmleditor = $this->getContents();
5661 break;
5662 case "AJAX":
5663 $this->info->tempuser->ajax = $this->getContents();
5664 break;
5665 case "AUTOSUBSCRIBE":
5666 $this->info->tempuser->autosubscribe = $this->getContents();
5667 break;
5668 case "TRACKFORUMS":
5669 $this->info->tempuser->trackforums = $this->getContents();
5670 break;
5671 case "MNETHOSTURL":
5672 $this->info->tempuser->mnethosturl = $this->getContents();
5673 break;
5674 case "TIMEMODIFIED":
5675 $this->info->tempuser->timemodified = $this->getContents();
5676 break;
5677 default:
5678 break;
5681 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
5682 switch ($tagName) {
5683 case "ROLE":
5684 //We've finalized a role, get it
5685 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
5686 unset($this->info->temprole);
5687 break;
5688 case "USER_PREFERENCE":
5689 //We've finalized a user_preference, get it
5690 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
5691 unset($this->info->tempuserpreference);
5692 break;
5696 if ($this->level == 7) {
5697 switch ($tagName) {
5698 case "TYPE":
5699 $this->info->temprole->type = $this->getContents();
5700 break;
5701 case "AUTHORITY":
5702 $this->info->temprole->authority = $this->getContents();
5703 break;
5704 case "TEA_ROLE":
5705 $this->info->temprole->tea_role = $this->getContents();
5706 break;
5707 case "EDITALL":
5708 $this->info->temprole->editall = $this->getContents();
5709 break;
5710 case "TIMESTART":
5711 $this->info->temprole->timestart = $this->getContents();
5712 break;
5713 case "TIMEEND":
5714 $this->info->temprole->timeend = $this->getContents();
5715 break;
5716 case "TIMEMODIFIED":
5717 $this->info->temprole->timemodified = $this->getContents();
5718 break;
5719 case "TIMESTART":
5720 $this->info->temprole->timestart = $this->getContents();
5721 break;
5722 case "TIMEEND":
5723 $this->info->temprole->timeend = $this->getContents();
5724 break;
5725 case "TIME":
5726 $this->info->temprole->time = $this->getContents();
5727 break;
5728 case "TIMEACCESS":
5729 $this->info->temprole->timeaccess = $this->getContents();
5730 break;
5731 case "ENROL":
5732 $this->info->temprole->enrol = $this->getContents();
5733 break;
5734 case "NAME":
5735 $this->info->tempuserpreference->name = $this->getContents();
5736 break;
5737 case "VALUE":
5738 $this->info->tempuserpreference->value = $this->getContents();
5739 break;
5740 default:
5741 break;
5746 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
5748 if ($this->level == 7) {
5749 switch ($tagName) {
5750 case "NAME":
5751 $this->info->tempname = $this->getContents();
5752 break;
5753 case "SHORTNAME":
5754 $this->info->tempshortname = $this->getContents();
5755 break;
5756 case "ID":
5757 $this->info->tempid = $this->getContents(); // temp roleid
5758 break;
5762 if ($this->level == 9) {
5764 switch ($tagName) {
5765 case "USERID":
5766 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
5768 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
5770 $this->info->tempuserid = $this->getContents();
5772 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
5773 break;
5774 case "HIDDEN":
5775 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
5776 break;
5777 case "TIMESTART":
5778 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
5779 break;
5780 case "TIMEEND":
5781 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
5782 break;
5783 case "TIMEMODIFIED":
5784 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
5785 break;
5786 case "MODIFIERID":
5787 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
5788 break;
5789 case "ENROL":
5790 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
5791 break;
5792 case "SORTORDER":
5793 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
5794 break;
5798 } /// ends role_assignments
5800 if ($this->tree[5] == "ROLES_OVERRIDES") {
5801 if ($this->level == 7) {
5802 switch ($tagName) {
5803 case "NAME":
5804 $this->info->tempname = $this->getContents();
5805 break;
5806 case "SHORTNAME":
5807 $this->info->tempshortname = $this->getContents();
5808 break;
5809 case "ID":
5810 $this->info->tempid = $this->getContents(); // temp roleid
5811 break;
5815 if ($this->level == 9) {
5816 switch ($tagName) {
5817 case "NAME":
5819 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5820 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5821 $this->info->tempname = $this->getContents(); // change to name of capability
5822 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5823 break;
5824 case "PERMISSION":
5825 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5826 break;
5827 case "TIMEMODIFIED":
5828 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5829 break;
5830 case "MODIFIERID":
5831 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5832 break;
5835 } /// ends role_overrides
5837 } // closes if this->tree[3]=="users"
5839 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
5840 //Speed up a lot (avoid parse all)
5841 if ($tagName == "USERS" and $this->level == 3) {
5842 $this->finished = true;
5843 $this->counter = 0;
5846 //Clear things
5847 $this->tree[$this->level] = "";
5848 $this->level--;
5849 $this->content = "";
5853 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
5854 function endElementMessages($parser, $tagName) {
5855 //Check if we are into MESSAGES zone
5856 if ($this->tree[3] == "MESSAGES") {
5857 //if (trim($this->content)) //Debug
5858 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5859 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5860 //Acumulate data to info (content + close tag)
5861 //Reconvert: strip htmlchars again and trim to generate xml data
5862 if (!isset($this->temp)) {
5863 $this->temp = "";
5865 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5866 //If we've finished a message, xmlize it an save to db
5867 if (($this->level == 4) and ($tagName == "MESSAGE")) {
5868 //Prepend XML standard header to info gathered
5869 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5870 //Call to xmlize for this portion of xml data (one MESSAGE)
5871 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5872 $data = xmlize($xml_data,0);
5873 //echo strftime ("%X",time())."<p>"; //Debug
5874 //traverse_xmlize($data); //Debug
5875 //print_object ($GLOBALS['traverse_array']); //Debug
5876 //$GLOBALS['traverse_array']=""; //Debug
5877 //Now, save data to db. We'll use it later
5878 //Get id and status from data
5879 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
5880 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
5881 if ($message_status == "READ") {
5882 $table = "message_read";
5883 } else {
5884 $table = "message";
5886 $this->counter++;
5887 //Save to db
5888 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
5889 null,$data);
5890 //Create returning info
5891 $this->info = $this->counter;
5892 //Reset temp
5893 unset($this->temp);
5895 //If we've finished a contact, xmlize it an save to db
5896 if (($this->level == 5) and ($tagName == "CONTACT")) {
5897 //Prepend XML standard header to info gathered
5898 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5899 //Call to xmlize for this portion of xml data (one MESSAGE)
5900 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5901 $data = xmlize($xml_data,0);
5902 //echo strftime ("%X",time())."<p>"; //Debug
5903 //traverse_xmlize($data); //Debug
5904 //print_object ($GLOBALS['traverse_array']); //Debug
5905 //$GLOBALS['traverse_array']=""; //Debug
5906 //Now, save data to db. We'll use it later
5907 //Get id and status from data
5908 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
5909 $this->counter++;
5910 //Save to db
5911 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
5912 null,$data);
5913 //Create returning info
5914 $this->info = $this->counter;
5915 //Reset temp
5916 unset($this->temp);
5920 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
5921 //Speed up a lot (avoid parse all)
5922 if ($tagName == "MESSAGES" and $this->level == 3) {
5923 $this->finished = true;
5924 $this->counter = 0;
5927 //Clear things
5928 $this->tree[$this->level] = "";
5929 $this->level--;
5930 $this->content = "";
5934 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
5935 function endElementQuestions($parser, $tagName) {
5936 //Check if we are into QUESTION_CATEGORIES zone
5937 if ($this->tree[3] == "QUESTION_CATEGORIES") {
5938 //if (trim($this->content)) //Debug
5939 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5940 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5941 //Acumulate data to info (content + close tag)
5942 //Reconvert: strip htmlchars again and trim to generate xml data
5943 if (!isset($this->temp)) {
5944 $this->temp = "";
5946 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5947 //If we've finished a mod, xmlize it an save to db
5948 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
5949 //Prepend XML standard header to info gathered
5950 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5951 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
5952 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5953 $data = xmlize($xml_data,0);
5954 //echo strftime ("%X",time())."<p>"; //Debug
5955 //traverse_xmlize($data); //Debug
5956 //print_object ($GLOBALS['traverse_array']); //Debug
5957 //$GLOBALS['traverse_array']=""; //Debug
5958 //Now, save data to db. We'll use it later
5959 //Get id from data
5960 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
5961 //Save to db
5962 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
5963 null,$data);
5964 //Create returning info
5965 $ret_info = new object();
5966 $ret_info->id = $category_id;
5967 $this->info[] = $ret_info;
5968 //Reset temp
5969 unset($this->temp);
5973 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
5974 //Speed up a lot (avoid parse all)
5975 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
5976 $this->finished = true;
5979 //Clear things
5980 $this->tree[$this->level] = "";
5981 $this->level--;
5982 $this->content = "";
5986 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
5987 function endElementScales($parser, $tagName) {
5988 //Check if we are into SCALES zone
5989 if ($this->tree[3] == "SCALES") {
5990 //if (trim($this->content)) //Debug
5991 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5992 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5993 //Acumulate data to info (content + close tag)
5994 //Reconvert: strip htmlchars again and trim to generate xml data
5995 if (!isset($this->temp)) {
5996 $this->temp = "";
5998 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5999 //If we've finished a scale, xmlize it an save to db
6000 if (($this->level == 4) and ($tagName == "SCALE")) {
6001 //Prepend XML standard header to info gathered
6002 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6003 //Call to xmlize for this portion of xml data (one SCALE)
6004 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6005 $data = xmlize($xml_data,0);
6006 //echo strftime ("%X",time())."<p>"; //Debug
6007 //traverse_xmlize($data); //Debug
6008 //print_object ($GLOBALS['traverse_array']); //Debug
6009 //$GLOBALS['traverse_array']=""; //Debug
6010 //Now, save data to db. We'll use it later
6011 //Get id and from data
6012 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
6013 //Save to db
6014 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
6015 null,$data);
6016 //Create returning info
6017 $ret_info = new object();
6018 $ret_info->id = $scale_id;
6019 $this->info[] = $ret_info;
6020 //Reset temp
6021 unset($this->temp);
6025 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
6026 //Speed up a lot (avoid parse all)
6027 if ($tagName == "SCALES" and $this->level == 3) {
6028 $this->finished = true;
6031 //Clear things
6032 $this->tree[$this->level] = "";
6033 $this->level--;
6034 $this->content = "";
6038 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
6039 function endElementGroups($parser, $tagName) {
6040 //Check if we are into GROUPS zone
6041 if ($this->tree[3] == "GROUPS") {
6042 //if (trim($this->content)) //Debug
6043 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6044 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6045 //Acumulate data to info (content + close tag)
6046 //Reconvert: strip htmlchars again and trim to generate xml data
6047 if (!isset($this->temp)) {
6048 $this->temp = "";
6050 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6051 //If we've finished a group, xmlize it an save to db
6052 if (($this->level == 4) and ($tagName == "GROUP")) {
6053 //Prepend XML standard header to info gathered
6054 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6055 //Call to xmlize for this portion of xml data (one GROUP)
6056 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6057 $data = xmlize($xml_data,0);
6058 //echo strftime ("%X",time())."<p>"; //Debug
6059 //traverse_xmlize($data); //Debug
6060 //print_object ($GLOBALS['traverse_array']); //Debug
6061 //$GLOBALS['traverse_array']=""; //Debug
6062 //Now, save data to db. We'll use it later
6063 //Get id and from data
6064 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
6065 //Save to db
6066 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
6067 null,$data);
6068 //Create returning info
6069 $ret_info = new Object();
6070 $ret_info->id = $group_id;
6071 $this->info[] = $ret_info;
6072 //Reset temp
6073 unset($this->temp);
6077 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
6078 //Speed up a lot (avoid parse all)
6079 if ($tagName == "GROUPS" and $this->level == 3) {
6080 $this->finished = true;
6083 //Clear things
6084 $this->tree[$this->level] = "";
6085 $this->level--;
6086 $this->content = "";
6090 //This is the endTag handler we use where we are reading the groupings zone (todo="GROUPINGS")
6091 function endElementGroupings($parser, $tagName) {
6092 //Check if we are into GROUPINGS zone
6093 if ($this->tree[3] == "GROUPINGS") {
6094 //Acumulate data to info (content + close tag)
6095 //Reconvert: strip htmlchars again and trim to generate xml data
6096 if (!isset($this->temp)) {
6097 $this->temp = "";
6099 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6100 //If we've finished a group, xmlize it an save to db
6101 if (($this->level == 4) and ($tagName == "GROUPING")) {
6102 //Prepend XML standard header to info gathered
6103 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6104 //Call to xmlize for this portion of xml data (one GROUPING)
6105 $data = xmlize($xml_data,0);
6106 //Now, save data to db. We'll use it later
6107 //Get id and from data
6108 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
6109 //Save to db
6110 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
6111 null,$data);
6112 //Create returning info
6113 $ret_info = new Object();
6114 $ret_info->id = $grouping_id;
6115 $this->info[] = $ret_info;
6116 //Reset temp
6117 unset($this->temp);
6121 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6122 //Speed up a lot (avoid parse all)
6123 if ($tagName == "GROUPINGS" and $this->level == 3) {
6124 $this->finished = true;
6127 //Clear things
6128 $this->tree[$this->level] = "";
6129 $this->level--;
6130 $this->content = "";
6134 //This is the endTag handler we use where we are reading the groupingsgroups zone (todo="GROUPINGGROUPS")
6135 function endElementGroupingsGroups($parser, $tagName) {
6136 //Check if we are into GROUPINGSGROUPS zone
6137 if ($this->tree[3] == "GROUPINGSGROUPS") {
6138 //Acumulate data to info (content + close tag)
6139 //Reconvert: strip htmlchars again and trim to generate xml data
6140 if (!isset($this->temp)) {
6141 $this->temp = "";
6143 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6144 //If we've finished a group, xmlize it an save to db
6145 if (($this->level == 4) and ($tagName == "GROUPINGGROUP")) {
6146 //Prepend XML standard header to info gathered
6147 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6148 //Call to xmlize for this portion of xml data (one GROUPING)
6149 $data = xmlize($xml_data,0);
6150 //Now, save data to db. We'll use it later
6151 //Get id and from data
6152 $groupinggroup_id = $data["GROUPINGGROUP"]["#"]["ID"]["0"]["#"];
6153 //Save to db
6154 $status = backup_putid($this->preferences->backup_unique_code,"groupingsgroups",$groupinggroup_id,
6155 null,$data);
6156 //Create returning info
6157 $ret_info = new Object();
6158 $ret_info->id = $groupinggroup_id;
6159 $this->info[] = $ret_info;
6160 //Reset temp
6161 unset($this->temp);
6165 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
6166 //Speed up a lot (avoid parse all)
6167 if ($tagName == "GROUPINGSGROUPS" and $this->level == 3) {
6168 $this->finished = true;
6171 //Clear things
6172 $this->tree[$this->level] = "";
6173 $this->level--;
6174 $this->content = "";
6178 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
6179 function endElementEvents($parser, $tagName) {
6180 //Check if we are into EVENTS zone
6181 if ($this->tree[3] == "EVENTS") {
6182 //if (trim($this->content)) //Debug
6183 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6184 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6185 //Acumulate data to info (content + close tag)
6186 //Reconvert: strip htmlchars again and trim to generate xml data
6187 if (!isset($this->temp)) {
6188 $this->temp = "";
6190 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6191 //If we've finished a event, xmlize it an save to db
6192 if (($this->level == 4) and ($tagName == "EVENT")) {
6193 //Prepend XML standard header to info gathered
6194 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6195 //Call to xmlize for this portion of xml data (one EVENT)
6196 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6197 $data = xmlize($xml_data,0);
6198 //echo strftime ("%X",time())."<p>"; //Debug
6199 //traverse_xmlize($data); //Debug
6200 //print_object ($GLOBALS['traverse_array']); //Debug
6201 //$GLOBALS['traverse_array']=""; //Debug
6202 //Now, save data to db. We'll use it later
6203 //Get id and from data
6204 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
6205 //Save to db
6206 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
6207 null,$data);
6208 //Create returning info
6209 $ret_info = new object();
6210 $ret_info->id = $event_id;
6211 $this->info[] = $ret_info;
6212 //Reset temp
6213 unset($this->temp);
6217 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
6218 //Speed up a lot (avoid parse all)
6219 if ($tagName == "EVENTS" and $this->level == 3) {
6220 $this->finished = true;
6223 //Clear things
6224 $this->tree[$this->level] = "";
6225 $this->level--;
6226 $this->content = "";
6230 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
6231 function endElementModules($parser, $tagName) {
6232 //Check if we are into MODULES zone
6233 if ($this->tree[3] == "MODULES") {
6234 //if (trim($this->content)) //Debug
6235 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6236 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6237 //Acumulate data to info (content + close tag)
6238 //Reconvert: strip htmlchars again and trim to generate xml data
6239 if (!isset($this->temp)) {
6240 $this->temp = "";
6242 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6243 //If we've finished a mod, xmlize it an save to db
6244 if (($this->level == 4) and ($tagName == "MOD")) {
6245 //Prepend XML standard header to info gathered
6246 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6247 //Call to xmlize for this portion of xml data (one MOD)
6248 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6249 $data = xmlize($xml_data,0);
6250 //echo strftime ("%X",time())."<p>"; //Debug
6251 //traverse_xmlize($data); //Debug
6252 //print_object ($GLOBALS['traverse_array']); //Debug
6253 //$GLOBALS['traverse_array']=""; //Debug
6254 //Now, save data to db. We'll use it later
6255 //Get id and modtype from data
6256 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
6257 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
6258 //Only if we've selected to restore it
6259 if (!empty($this->preferences->mods[$mod_type]->restore)) {
6260 //Save to db
6261 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
6262 null,$data);
6263 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6264 //Create returning info
6265 $ret_info = new object();
6266 $ret_info->id = $mod_id;
6267 $ret_info->modtype = $mod_type;
6268 $this->info[] = $ret_info;
6270 //Reset temp
6271 unset($this->temp);
6277 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
6278 //Speed up a lot (avoid parse all)
6279 if ($tagName == "MODULES" and $this->level == 3) {
6280 $this->finished = true;
6283 //Clear things
6284 $this->tree[$this->level] = "";
6285 $this->level--;
6286 $this->content = "";
6290 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
6291 function endElementLogs($parser, $tagName) {
6292 //Check if we are into LOGS zone
6293 if ($this->tree[3] == "LOGS") {
6294 //if (trim($this->content)) //Debug
6295 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6296 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6297 //Acumulate data to info (content + close tag)
6298 //Reconvert: strip htmlchars again and trim to generate xml data
6299 if (!isset($this->temp)) {
6300 $this->temp = "";
6302 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
6303 //If we've finished a log, xmlize it an save to db
6304 if (($this->level == 4) and ($tagName == "LOG")) {
6305 //Prepend XML standard header to info gathered
6306 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
6307 //Call to xmlize for this portion of xml data (one LOG)
6308 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
6309 $data = xmlize($xml_data,0);
6310 //echo strftime ("%X",time())."<p>"; //Debug
6311 //traverse_xmlize($data); //Debug
6312 //print_object ($GLOBALS['traverse_array']); //Debug
6313 //$GLOBALS['traverse_array']=""; //Debug
6314 //Now, save data to db. We'll use it later
6315 //Get id and modtype from data
6316 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
6317 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
6318 //We only save log entries from backup file if they are:
6319 // - Course logs
6320 // - User logs
6321 // - Module logs about one restored module
6322 if ($log_module == "course" or
6323 $log_module == "user" or
6324 $this->preferences->mods[$log_module]->restore) {
6325 //Increment counter
6326 $this->counter++;
6327 //Save to db
6328 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
6329 null,$data);
6330 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
6331 //Create returning info
6332 $this->info = $this->counter;
6334 //Reset temp
6335 unset($this->temp);
6339 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
6340 //Speed up a lot (avoid parse all)
6341 if ($tagName == "LOGS" and $this->level == 3) {
6342 $this->finished = true;
6343 $this->counter = 0;
6346 //Clear things
6347 $this->tree[$this->level] = "";
6348 $this->level--;
6349 $this->content = "";
6353 //This is the endTag default handler we use when todo is undefined
6354 function endElement($parser, $tagName) {
6355 if (trim($this->content)) //Debug
6356 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
6357 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
6359 //Clear things
6360 $this->tree[$this->level] = "";
6361 $this->level--;
6362 $this->content = "";
6365 //This is the handler to read data contents (simple accumule it)
6366 function characterData($parser, $data) {
6367 $this->content .= $data;
6371 //This function executes the MoodleParser
6372 function restore_read_xml ($xml_file,$todo,$preferences) {
6374 $status = true;
6376 $xml_parser = xml_parser_create('UTF-8');
6377 $moodle_parser = new MoodleParser();
6378 $moodle_parser->todo = $todo;
6379 $moodle_parser->preferences = $preferences;
6380 xml_set_object($xml_parser,$moodle_parser);
6381 //Depending of the todo we use some element_handler or another
6382 if ($todo == "INFO") {
6383 //Define handlers to that zone
6384 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
6385 } else if ($todo == "ROLES") {
6386 // Define handlers to that zone
6387 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
6388 } else if ($todo == "COURSE_HEADER") {
6389 //Define handlers to that zone
6390 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
6391 } else if ($todo == 'BLOCKS') {
6392 //Define handlers to that zone
6393 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
6394 } else if ($todo == "SECTIONS") {
6395 //Define handlers to that zone
6396 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
6397 } else if ($todo == 'FORMATDATA') {
6398 //Define handlers to that zone
6399 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
6400 } else if ($todo == "METACOURSE") {
6401 //Define handlers to that zone
6402 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
6403 } else if ($todo == "GRADEBOOK") {
6404 //Define handlers to that zone
6405 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
6406 } else if ($todo == "USERS") {
6407 //Define handlers to that zone
6408 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
6409 } else if ($todo == "MESSAGES") {
6410 //Define handlers to that zone
6411 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
6412 } else if ($todo == "QUESTIONS") {
6413 //Define handlers to that zone
6414 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
6415 } else if ($todo == "SCALES") {
6416 //Define handlers to that zone
6417 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
6418 } else if ($todo == "GROUPS") {
6419 //Define handlers to that zone
6420 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
6421 } else if ($todo == "GROUPINGS") {
6422 //Define handlers to that zone
6423 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
6424 } else if ($todo == "GROUPINGSGROUPS") {
6425 //Define handlers to that zone
6426 xml_set_element_handler($xml_parser, "startElementGroupingsGroups", "endElementGroupingsGroups");
6427 } else if ($todo == "EVENTS") {
6428 //Define handlers to that zone
6429 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
6430 } else if ($todo == "MODULES") {
6431 //Define handlers to that zone
6432 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
6433 } else if ($todo == "LOGS") {
6434 //Define handlers to that zone
6435 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
6436 } else {
6437 //Define default handlers (must no be invoked when everything become finished)
6438 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
6440 xml_set_character_data_handler($xml_parser, "characterData");
6441 $fp = fopen($xml_file,"r")
6442 or $status = false;
6443 if ($status) {
6444 while ($data = fread($fp, 4096) and !$moodle_parser->finished)
6445 xml_parse($xml_parser, $data, feof($fp))
6446 or die(sprintf("XML error: %s at line %d",
6447 xml_error_string(xml_get_error_code($xml_parser)),
6448 xml_get_current_line_number($xml_parser)));
6449 fclose($fp);
6451 //Get info from parser
6452 $info = $moodle_parser->info;
6454 //Clear parser mem
6455 xml_parser_free($xml_parser);
6457 if ($status && !empty($info)) {
6458 return $info;
6459 } else {
6460 return $status;
6465 * @param string $errorstr passed by reference, if silent is true,
6466 * errorstr will be populated and this function will return false rather than calling error() or notify()
6467 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
6468 * redirect to the next step in the restore process, instead will return $backup_unique_code
6470 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
6472 global $CFG, $SESSION;
6474 //Prepend dataroot to variable to have the absolute path
6475 $file = $CFG->dataroot."/".$file;
6477 if (!defined('RESTORE_SILENTLY')) {
6478 //Start the main table
6479 echo "<table cellpadding=\"5\">";
6480 echo "<tr><td>";
6482 //Start the mail ul
6483 echo "<ul>";
6486 //Check the file exists
6487 if (!is_file($file)) {
6488 if (!defined('RESTORE_SILENTLY')) {
6489 error ("File not exists ($file)");
6490 } else {
6491 $errorstr = "File not exists ($file)";
6492 return false;
6496 //Check the file name ends with .zip
6497 if (!substr($file,-4) == ".zip") {
6498 if (!defined('RESTORE_SILENTLY')) {
6499 error ("File has an incorrect extension");
6500 } else {
6501 $errorstr = 'File has an incorrect extension';
6502 return false;
6506 //Now calculate the unique_code for this restore
6507 $backup_unique_code = time();
6509 //Now check and create the backup dir (if it doesn't exist)
6510 if (!defined('RESTORE_SILENTLY')) {
6511 echo "<li>".get_string("creatingtemporarystructures").'</li>';
6513 $status = check_and_create_backup_dir($backup_unique_code);
6514 //Empty dir
6515 if ($status) {
6516 $status = clear_backup_dir($backup_unique_code);
6519 //Now delete old data and directories under dataroot/temp/backup
6520 if ($status) {
6521 if (!defined('RESTORE_SILENTLY')) {
6522 echo "<li>".get_string("deletingolddata").'</li>';
6524 $status = backup_delete_old_data();
6527 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
6528 if ($status) {
6529 if (!defined('RESTORE_SILENTLY')) {
6530 echo "<li>".get_string("copyingzipfile").'</li>';
6532 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
6533 if (!defined('RESTORE_SILENTLY')) {
6534 notify("Error copying backup file. Invalid name or bad perms.");
6535 } else {
6536 $errorstr = "Error copying backup file. Invalid name or bad perms";
6537 return false;
6542 //Now unzip the file
6543 if ($status) {
6544 if (!defined('RESTORE_SILENTLY')) {
6545 echo "<li>".get_string("unzippingbackup").'</li>';
6547 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
6548 if (!defined('RESTORE_SILENTLY')) {
6549 notify("Error unzipping backup file. Invalid zip file.");
6550 } else {
6551 $errorstr = "Error unzipping backup file. Invalid zip file.";
6552 return false;
6557 //Check for Blackboard backups and convert
6558 if ($status){
6559 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
6560 if (!defined('RESTORE_SILENTLY')) {
6561 echo "<li>".get_string("checkingforbbexport").'</li>';
6563 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
6566 //Now check for the moodle.xml file
6567 if ($status) {
6568 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
6569 if (!defined('RESTORE_SILENTLY')) {
6570 echo "<li>".get_string("checkingbackup").'</li>';
6572 if (! $status = restore_check_moodle_file ($xml_file)) {
6573 if (!is_file($xml_file)) {
6574 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
6575 } else {
6576 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
6578 if (!defined('RESTORE_SILENTLY')) {
6579 notify($errorstr);
6580 } else {
6581 return false;
6586 $info = "";
6587 $course_header = "";
6589 //Now read the info tag (all)
6590 if ($status) {
6591 if (!defined('RESTORE_SILENTLY')) {
6592 echo "<li>".get_string("readinginfofrombackup").'</li>';
6594 //Reading info from file
6595 $info = restore_read_xml_info ($xml_file);
6596 //Reading course_header from file
6597 $course_header = restore_read_xml_course_header ($xml_file);
6600 if (!defined('RESTORE_SILENTLY')) {
6601 //End the main ul
6602 echo "</ul>\n";
6604 //End the main table
6605 echo "</td></tr>";
6606 echo "</table>";
6609 //We compare Moodle's versions
6610 if ($CFG->version < $info->backup_moodle_version && $status) {
6611 $message = new message();
6612 $message->serverversion = $CFG->version;
6613 $message->serverrelease = $CFG->release;
6614 $message->backupversion = $info->backup_moodle_version;
6615 $message->backuprelease = $info->backup_moodle_release;
6616 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
6620 //Now we print in other table, the backup and the course it contains info
6621 if ($info and $course_header and $status) {
6622 //First, the course info
6623 if (!defined('RESTORE_SILENTLY')) {
6624 $status = restore_print_course_header($course_header);
6626 //Now, the backup info
6627 if ($status) {
6628 if (!defined('RESTORE_SILENTLY')) {
6629 $status = restore_print_info($info);
6634 //Save course header and info into php session
6635 if ($status) {
6636 $SESSION->info = $info;
6637 $SESSION->course_header = $course_header;
6640 //Finally, a little form to continue
6641 //with some hidden fields
6642 if ($status) {
6643 if (!defined('RESTORE_SILENTLY')) {
6644 echo "<br /><div style='text-align:center'>";
6645 $hidden["backup_unique_code"] = $backup_unique_code;
6646 $hidden["launch"] = "form";
6647 $hidden["file"] = $file;
6648 $hidden["id"] = $id;
6649 print_single_button("restore.php", $hidden, get_string("continue"),"post");
6650 echo "</div>";
6652 else {
6653 if (empty($noredirect)) {
6654 redirect($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
6655 } else {
6656 return $backup_unique_code;
6661 if (!$status) {
6662 if (!defined('RESTORE_SILENTLY')) {
6663 error ("An error has ocurred");
6664 } else {
6665 $errorstr = "An error has occured"; // helpful! :P
6666 return false;
6669 return true;
6672 function restore_setup_for_check(&$restore,$backup_unique_code) {
6673 global $SESSION;
6674 $restore->backup_unique_code=$backup_unique_code;
6675 $restore->users = 2; // yuk
6676 $restore->course_files = $SESSION->restore->restore_course_files;
6677 $restore->site_files = $SESSION->restore->restore_site_files;
6678 if ($allmods = get_records("modules")) {
6679 foreach ($allmods as $mod) {
6680 $modname = $mod->name;
6681 $var = "restore_".$modname;
6682 //Now check that we have that module info in the backup file
6683 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
6684 $restore->$var = 1;
6688 return true;
6691 function backup_to_restore_array($backup,$k=0) {
6692 if (is_array($backup) ) {
6693 foreach ($backup as $key => $value) {
6694 $newkey = str_replace('backup','restore',$key);
6695 $restore[$newkey] = backup_to_restore_array($value,$key);
6698 else if (is_object($backup)) {
6699 $tmp = get_object_vars($backup);
6700 foreach ($tmp as $key => $value) {
6701 $newkey = str_replace('backup','restore',$key);
6702 $restore->$newkey = backup_to_restore_array($value,$key);
6705 else {
6706 $newkey = str_replace('backup','restore',$k);
6707 $restore = $backup;
6709 return $restore;
6713 * compatibility function
6714 * checks for per-instance backups AND
6715 * older per-module backups
6716 * and returns whether userdata has been selected.
6718 function restore_userdata_selected($restore,$modname,$modid) {
6719 // check first for per instance array
6720 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
6721 return array_key_exists($modid,$restore->mods[$modname]->instances)
6722 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
6725 //print_object($restore->mods[$modname]);
6726 return !empty($restore->mods[$modname]->userinfo);
6729 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
6731 global $CFG, $USER;
6732 $status = true;
6734 //Checks for the required files/functions to restore every module
6735 //and include them
6736 if ($allmods = get_records("modules") ) {
6737 foreach ($allmods as $mod) {
6738 $modname = $mod->name;
6739 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
6740 //If file exists and we have selected to restore that type of module
6741 if ((file_exists($modfile)) and !empty($restore->mods[$modname]) and ($restore->mods[$modname]->restore)) {
6742 include_once($modfile);
6747 if (!defined('RESTORE_SILENTLY')) {
6748 //Start the main table
6749 echo "<table cellpadding=\"5\">";
6750 echo "<tr><td>";
6752 //Start the main ul
6753 echo "<ul>";
6756 //Localtion of the xml file
6757 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
6759 //If we've selected to restore into new course
6760 //create it (course)
6761 //Saving conversion id variables into backup_tables
6762 if ($restore->restoreto == 2) {
6763 if (!defined('RESTORE_SILENTLY')) {
6764 echo '<li>'.get_string('creatingnewcourse') . '</li>';
6766 $oldidnumber = $course_header->course_idnumber;
6767 if (!$status = restore_create_new_course($restore,$course_header)) {
6768 if (!defined('RESTORE_SILENTLY')) {
6769 notify("Error while creating the new empty course.");
6770 } else {
6771 $errorstr = "Error while creating the new empty course.";
6772 return false;
6776 //Print course fullname and shortname and category
6777 if ($status) {
6778 if (!defined('RESTORE_SILENTLY')) {
6779 echo "<ul>";
6780 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
6781 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
6782 if (!empty($oldidnumber)) {
6783 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
6785 echo "</ul>";
6786 //Put the destination course_id
6788 $restore->course_id = $course_header->course_id;
6791 if ($status = restore_open_html($restore,$course_header)){
6792 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
6795 } else {
6796 $course = get_record("course","id",$restore->course_id);
6797 if ($course) {
6798 if (!defined('RESTORE_SILENTLY')) {
6799 echo "<li>".get_string("usingexistingcourse");
6800 echo "<ul>";
6801 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
6802 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
6803 if (($restore->deleting)) {
6804 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
6805 } else {
6806 echo "<li>".get_string("addingdatatoexisting").'</li>';
6808 echo "</ul></li>";
6810 //If we have selected to restore deleting, we do it now.
6811 if ($restore->deleting) {
6812 if (!defined('RESTORE_SILENTLY')) {
6813 echo "<li>".get_string("deletingolddata").'</li>';
6815 $status = remove_course_contents($restore->course_id,false) and
6816 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
6817 if ($status) {
6818 //Now , this situation is equivalent to the "restore to new course" one (we
6819 //have a course record and nothing more), so define it as "to new course"
6820 $restore->restoreto = 2;
6821 } else {
6822 if (!defined('RESTORE_SILENTLY')) {
6823 notify("An error occurred while deleting some of the course contents.");
6824 } else {
6825 $errrostr = "An error occurred while deleting some of the course contents.";
6826 return false;
6830 } else {
6831 if (!defined('RESTORE_SILENTLY')) {
6832 notify("Error opening existing course.");
6833 $status = false;
6834 } else {
6835 $errorstr = "Error opening existing course.";
6836 return false;
6841 //Now create users as needed
6842 if ($status and ($restore->users == 0 or $restore->users == 1)) {
6843 if (!defined('RESTORE_SILENTLY')) {
6844 echo "<li>".get_string("creatingusers")."<br />";
6846 if (!$status = restore_create_users($restore,$xml_file)) {
6847 if (!defined('RESTORE_SILENTLY')) {
6848 notify("Could not restore users.");
6849 } else {
6850 $errorstr = "Could not restore users.";
6851 return false;
6855 //Now print info about the work done
6856 if ($status) {
6857 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
6858 where backup_code = '$restore->backup_unique_code' and
6859 table_name = 'user'");
6860 //We've records
6861 if ($recs) {
6862 $new_count = 0;
6863 $exists_count = 0;
6864 $student_count = 0;
6865 $teacher_count = 0;
6866 $counter = 0;
6867 //Iterate, filling counters
6868 foreach ($recs as $rec) {
6869 //Get full record, using backup_getids
6870 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
6871 if (strpos($record->info,"new") !== false) {
6872 $new_count++;
6874 if (strpos($record->info,"exists") !== false) {
6875 $exists_count++;
6877 if (strpos($record->info,"student") !== false) {
6878 $student_count++;
6879 } else if (strpos($record->info,"teacher") !== false) {
6880 $teacher_count++;
6882 //Do some output
6883 $counter++;
6884 if ($counter % 10 == 0) {
6885 if (!defined('RESTORE_SILENTLY')) {
6886 echo ".";
6887 if ($counter % 200 == 0) {
6888 echo "<br />";
6891 backup_flush(300);
6894 if (!defined('RESTORE_SILENTLY')) {
6895 //Now print information gathered
6896 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
6897 echo "<ul>";
6898 echo "<li>".get_string("students").": ".$student_count.'</li>';
6899 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
6900 echo "</ul>";
6902 } else {
6903 if (!defined('RESTORE_SILENTLY')) {
6904 notify("No users were found!");
6905 } // no need to return false here, it's recoverable.
6909 if (!defined('RESTORE_SILENTLY')) {
6910 echo "</li>";
6915 //Now create groups as needed
6916 if ($status) {
6917 if (!defined('RESTORE_SILENTLY')) {
6918 echo "<li>".get_string("creatinggroups");
6920 if (!$status = restore_create_groups($restore,$xml_file)) {
6921 if (!defined('RESTORE_SILENTLY')) {
6922 notify("Could not restore groups!");
6923 } else {
6924 $errorstr = "Could not restore groups!";
6925 return false;
6928 if (!defined('RESTORE_SILENTLY')) {
6929 echo '</li>';
6933 //Now create groupings as needed
6934 if ($status) {
6935 if (!defined('RESTORE_SILENTLY')) {
6936 echo "<li>".get_string("creatinggroupings");
6938 if (!$status = restore_create_groupings($restore,$xml_file)) {
6939 if (!defined('RESTORE_SILENTLY')) {
6940 notify("Could not restore groupings!");
6941 } else {
6942 $errorstr = "Could not restore groupings!";
6943 return false;
6946 if (!defined('RESTORE_SILENTLY')) {
6947 echo '</li>';
6951 //Now create groupingsgroups as needed
6952 if ($status) {
6953 if (!defined('RESTORE_SILENTLY')) {
6954 echo "<li>".get_string("creatinggroupingsgroups");
6956 if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
6957 if (!defined('RESTORE_SILENTLY')) {
6958 notify("Could not restore groups in groupings!");
6959 } else {
6960 $errorstr = "Could not restore groups in groupings!";
6961 return false;
6964 if (!defined('RESTORE_SILENTLY')) {
6965 echo '</li>';
6970 //Now create the course_sections and their associated course_modules
6971 //we have to do this after groups and groupings are restored, because we need the new groupings id
6972 if ($status) {
6973 //Into new course
6974 if ($restore->restoreto == 2) {
6975 if (!defined('RESTORE_SILENTLY')) {
6976 echo "<li>".get_string("creatingsections");
6978 if (!$status = restore_create_sections($restore,$xml_file)) {
6979 if (!defined('RESTORE_SILENTLY')) {
6980 notify("Error creating sections in the existing course.");
6981 } else {
6982 $errorstr = "Error creating sections in the existing course.";
6983 return false;
6986 if (!defined('RESTORE_SILENTLY')) {
6987 echo '</li>';
6989 //Into existing course
6990 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
6991 if (!defined('RESTORE_SILENTLY')) {
6992 echo "<li>".get_string("checkingsections");
6994 if (!$status = restore_create_sections($restore,$xml_file)) {
6995 if (!defined('RESTORE_SILENTLY')) {
6996 notify("Error creating sections in the existing course.");
6997 } else {
6998 $errorstr = "Error creating sections in the existing course.";
6999 return false;
7002 if (!defined('RESTORE_SILENTLY')) {
7003 echo '</li>';
7005 //Error
7006 } else {
7007 if (!defined('RESTORE_SILENTLY')) {
7008 notify("Neither a new course or an existing one was specified.");
7009 $status = false;
7010 } else {
7011 $errorstr = "Neither a new course or an existing one was specified.";
7012 return false;
7017 //Now create metacourse info
7018 if ($status and $restore->metacourse) {
7019 //Only to new courses!
7020 if ($restore->restoreto == 2) {
7021 if (!defined('RESTORE_SILENTLY')) {
7022 echo "<li>".get_string("creatingmetacoursedata");
7024 if (!$status = restore_create_metacourse($restore,$xml_file)) {
7025 if (!defined('RESTORE_SILENTLY')) {
7026 notify("Error creating metacourse in the course.");
7027 } else {
7028 $errorstr = "Error creating metacourse in the course.";
7029 return false;
7032 if (!defined('RESTORE_SILENTLY')) {
7033 echo '</li>';
7039 //Now create categories and questions as needed
7040 if ($status) {
7041 include_once("$CFG->dirroot/question/restorelib.php");
7042 if (!defined('RESTORE_SILENTLY')) {
7043 echo "<li>".get_string("creatingcategoriesandquestions");
7044 echo "<ul>";
7046 if (!$status = restore_create_questions($restore,$xml_file)) {
7047 if (!defined('RESTORE_SILENTLY')) {
7048 notify("Could not restore categories and questions!");
7049 } else {
7050 $errorstr = "Could not restore categories and questions!";
7051 return false;
7054 if (!defined('RESTORE_SILENTLY')) {
7055 echo "</ul></li>";
7059 //Now create user_files as needed
7060 if ($status and ($restore->user_files)) {
7061 if (!defined('RESTORE_SILENTLY')) {
7062 echo "<li>".get_string("copyinguserfiles");
7064 if (!$status = restore_user_files($restore)) {
7065 if (!defined('RESTORE_SILENTLY')) {
7066 notify("Could not restore user files!");
7067 } else {
7068 $errorstr = "Could not restore user files!";
7069 return false;
7072 //If all is ok (and we have a counter)
7073 if ($status and ($status !== true)) {
7074 //Inform about user dirs created from backup
7075 if (!defined('RESTORE_SILENTLY')) {
7076 echo "<ul>";
7077 echo "<li>".get_string("userzones").": ".$status;
7078 echo "</li></ul>";
7081 if (!defined('RESTORE_SILENTLY')) {
7082 echo '</li>';
7086 //Now create course files as needed
7087 if ($status and ($restore->course_files)) {
7088 if (!defined('RESTORE_SILENTLY')) {
7089 echo "<li>".get_string("copyingcoursefiles");
7091 if (!$status = restore_course_files($restore)) {
7092 if (empty($status)) {
7093 notify("Could not restore course files!");
7094 } else {
7095 $errorstr = "Could not restore course files!";
7096 return false;
7099 //If all is ok (and we have a counter)
7100 if ($status and ($status !== true)) {
7101 //Inform about user dirs created from backup
7102 if (!defined('RESTORE_SILENTLY')) {
7103 echo "<ul>";
7104 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7105 echo "</ul>";
7108 if (!defined('RESTORE_SILENTLY')) {
7109 echo "</li>";
7114 //Now create site files as needed
7115 if ($status and ($restore->site_files)) {
7116 if (!defined('RESTORE_SILENTLY')) {
7117 echo "<li>".get_string('copyingsitefiles');
7119 if (!$status = restore_site_files($restore)) {
7120 if (empty($status)) {
7121 notify("Could not restore site files!");
7122 } else {
7123 $errorstr = "Could not restore site files!";
7124 return false;
7127 //If all is ok (and we have a counter)
7128 if ($status and ($status !== true)) {
7129 //Inform about user dirs created from backup
7130 if (!defined('RESTORE_SILENTLY')) {
7131 echo "<ul>";
7132 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
7133 echo "</ul>";
7136 if (!defined('RESTORE_SILENTLY')) {
7137 echo "</li>";
7141 //Now create messages as needed
7142 if ($status and ($restore->messages)) {
7143 if (!defined('RESTORE_SILENTLY')) {
7144 echo "<li>".get_string("creatingmessagesinfo");
7146 if (!$status = restore_create_messages($restore,$xml_file)) {
7147 if (!defined('RESTORE_SILENTLY')) {
7148 notify("Could not restore messages!");
7149 } else {
7150 $errorstr = "Could not restore messages!";
7151 return false;
7154 if (!defined('RESTORE_SILENTLY')) {
7155 echo "</li>";
7159 //Now create scales as needed
7160 if ($status) {
7161 if (!defined('RESTORE_SILENTLY')) {
7162 echo "<li>".get_string("creatingscales");
7164 if (!$status = restore_create_scales($restore,$xml_file)) {
7165 if (!defined('RESTORE_SILENTLY')) {
7166 notify("Could not restore custom scales!");
7167 } else {
7168 $errorstr = "Could not restore custom scales!";
7169 return false;
7172 if (!defined('RESTORE_SILENTLY')) {
7173 echo '</li>';
7177 //Now create events as needed
7178 if ($status) {
7179 if (!defined('RESTORE_SILENTLY')) {
7180 echo "<li>".get_string("creatingevents");
7182 if (!$status = restore_create_events($restore,$xml_file)) {
7183 if (!defined('RESTORE_SILENTLY')) {
7184 notify("Could not restore course events!");
7185 } else {
7186 $errorstr = "Could not restore course events!";
7187 return false;
7190 if (!defined('RESTORE_SILENTLY')) {
7191 echo '</li>';
7195 //Now create course modules as needed
7196 if ($status) {
7197 if (!defined('RESTORE_SILENTLY')) {
7198 echo "<li>".get_string("creatingcoursemodules");
7200 if (!$status = restore_create_modules($restore,$xml_file)) {
7201 if (!defined('RESTORE_SILENTLY')) {
7202 notify("Could not restore modules!");
7203 } else {
7204 $errorstr = "Could not restore modules!";
7205 return false;
7208 if (!defined('RESTORE_SILENTLY')) {
7209 echo '</li>';
7213 //Now create gradebook as needed -- AFTER modules!!!
7214 if ($status) {
7215 if (!defined('RESTORE_SILENTLY')) {
7216 echo "<li>".get_string("creatinggradebook");
7218 if (!$status = restore_create_gradebook($restore,$xml_file)) {
7219 if (!defined('RESTORE_SILENTLY')) {
7220 notify("Could not restore gradebook!");
7221 } else {
7222 $errorstr = "Could not restore gradebook!";
7223 return false;
7226 if (!defined('RESTORE_SILENTLY')) {
7227 echo '</li>';
7231 //Bring back the course blocks -- do it AFTER the modules!!!
7232 if($status) {
7233 //If we are deleting and bringing into a course or making a new course, same situation
7234 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7235 if (!defined('RESTORE_SILENTLY')) {
7236 echo '<li>'.get_string('creatingblocks');
7238 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
7239 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
7240 if (!defined('RESTORE_SILENTLY')) {
7241 notify('Error while creating the course blocks');
7242 } else {
7243 $errorstr = "Error while creating the course blocks";
7244 return false;
7247 if (!defined('RESTORE_SILENTLY')) {
7248 echo '</li>';
7253 if($status) {
7254 //If we are deleting and bringing into a course or making a new course, same situation
7255 if($restore->restoreto == 0 || $restore->restoreto == 2) {
7256 if (!defined('RESTORE_SILENTLY')) {
7257 echo '<li>'.get_string('courseformatdata');
7259 if (!$status = restore_set_format_data($restore, $xml_file)) {
7260 $error = "Error while setting the course format data";
7261 if (!defined('RESTORE_SILENTLY')) {
7262 notify($error);
7263 } else {
7264 $errorstr=$error;
7265 return false;
7268 if (!defined('RESTORE_SILENTLY')) {
7269 echo '</li>';
7274 //Now create log entries as needed
7275 if ($status and ($restore->logs)) {
7276 if (!defined('RESTORE_SILENTLY')) {
7277 echo "<li>".get_string("creatinglogentries");
7279 if (!$status = restore_create_logs($restore,$xml_file)) {
7280 if (!defined('RESTORE_SILENTLY')) {
7281 notify("Could not restore logs!");
7282 } else {
7283 $errorstr = "Could not restore logs!";
7284 return false;
7287 if (!defined('RESTORE_SILENTLY')) {
7288 echo '</li>';
7292 //Now, if all is OK, adjust the instance field in course_modules !!
7293 if ($status) {
7294 if (!defined('RESTORE_SILENTLY')) {
7295 echo "<li>".get_string("checkinginstances");
7297 if (!$status = restore_check_instances($restore)) {
7298 if (!defined('RESTORE_SILENTLY')) {
7299 notify("Could not adjust instances in course_modules!");
7300 } else {
7301 $errorstr = "Could not adjust instances in course_modules!";
7302 return false;
7305 if (!defined('RESTORE_SILENTLY')) {
7306 echo '</li>';
7310 //Now, if all is OK, adjust activity events
7311 if ($status) {
7312 if (!defined('RESTORE_SILENTLY')) {
7313 echo "<li>".get_string("refreshingevents");
7315 if (!$status = restore_refresh_events($restore)) {
7316 if (!defined('RESTORE_SILENTLY')) {
7317 notify("Could not refresh events for activities!");
7318 } else {
7319 $errorstr = "Could not refresh events for activities!";
7320 return false;
7323 if (!defined('RESTORE_SILENTLY')) {
7324 echo '</li>';
7328 //Now, if all is OK, adjust inter-activity links
7329 if ($status) {
7330 if (!defined('RESTORE_SILENTLY')) {
7331 echo "<li>".get_string("decodinginternallinks");
7333 if (!$status = restore_decode_content_links($restore)) {
7334 if (!defined('RESTORE_SILENTLY')) {
7335 notify("Could not decode content links!");
7336 } else {
7337 $errorstr = "Could not decode content links!";
7338 return false;
7341 if (!defined('RESTORE_SILENTLY')) {
7342 echo '</li>';
7346 //Now, with backup files prior to version 2005041100,
7347 //convert all the wiki texts in the course to markdown
7348 if ($status && $restore->backup_version < 2005041100) {
7349 if (!defined('RESTORE_SILENTLY')) {
7350 echo "<li>".get_string("convertingwikitomarkdown");
7352 if (!$status = restore_convert_wiki2markdown($restore)) {
7353 if (!defined('RESTORE_SILENTLY')) {
7354 notify("Could not convert wiki texts to markdown!");
7355 } else {
7356 $errorstr = "Could not convert wiki texts to markdown!";
7357 return false;
7360 if (!defined('RESTORE_SILENTLY')) {
7361 echo '</li>';
7365 // for moodle versions before 1.9, those grades need to be converted to use the new gradebook
7366 // this code needs to execute *after* the course_modules are sorted out
7367 if ($status && $restore->backup_version < 2007090500) {
7368 if (!defined('RESTORE_SILENTLY')) {
7369 echo "<li>".get_string("migratinggrades");
7372 // we need need to worry about mods that are restored
7373 // the others in the course are not relevent
7374 if (!empty($restore->mods)) {
7375 require_once($CFG->dirroot.'/lib/gradelib.php');
7376 foreach ($restore->mods as $mod=>$modtype) {
7377 if (!empty($modtype->instances)) {
7378 foreach ($modtype->instances as $modinstance) {
7379 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
7380 FROM {$CFG->prefix}$mod a,
7381 {$CFG->prefix}course_modules cm,
7382 {$CFG->prefix}modules m
7383 WHERE m.name='$mod'
7384 AND m.id=cm.module
7385 AND cm.instance=a.id
7386 AND cm.id= {$modinstance->restored_as_course_module}";
7388 if ($module = get_record_sql($sql)) {
7389 grade_update_mod_grades($module);
7396 if (!defined('RESTORE_SILENTLY')) {
7397 echo '</li>';
7401 /*******************************************************************************
7402 ************* Restore of Roles and Capabilities happens here ******************
7403 *******************************************************************************/
7404 // try to restore roles even when restore is going to fail - teachers might have
7405 // at least some role assigned - this is not correct though
7406 $status = restore_create_roles($restore, $xml_file) && $status;
7407 $status = restore_roles_settings($restore, $xml_file) && $status;
7409 //Now if all is OK, update:
7410 // - course modinfo field
7411 // - categories table
7412 // - add user as teacher
7413 if ($status) {
7414 if (!defined('RESTORE_SILENTLY')) {
7415 echo "<li>".get_string("checkingcourse");
7417 //modinfo field
7418 rebuild_course_cache($restore->course_id);
7419 //categories table
7420 $course = get_record("course","id",$restore->course_id);
7421 fix_course_sortorder();
7422 // Check if the user has course update capability in the newly restored course
7423 // there is no need to load his capabilities again, because restore_roles_settings
7424 // would have loaded it anyway, if there is any assignments.
7425 // fix for MDL-6831
7426 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
7427 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
7428 // fix for MDL-9065, use the new config setting if exists
7429 if ($CFG->creatornewroleid) {
7430 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
7431 } else {
7432 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID))) {
7433 if ($legacyteacher = array_shift($legacyteachers)) {
7434 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
7436 } else {
7437 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
7441 if (!defined('RESTORE_SILENTLY')) {
7442 echo '</li>';
7446 //Cleanup temps (files and db)
7447 if ($status) {
7448 if (!defined('RESTORE_SILENTLY')) {
7449 echo "<li>".get_string("cleaningtempdata");
7451 if (!$status = clean_temp_data ($restore)) {
7452 if (!defined('RESTORE_SILENTLY')) {
7453 notify("Could not clean up temporary data from files and database");
7454 } else {
7455 $errorstr = "Could not clean up temporary data from files and database";
7456 return false;
7459 if (!defined('RESTORE_SILENTLY')) {
7460 echo '</li>';
7464 // this is not a critical check - the result can be ignored
7465 if (restore_close_html($restore)){
7466 if (!defined('RESTORE_SILENTLY')) {
7467 echo '<li>Closing the Restorelog.html file.</li>';
7470 else {
7471 if (!defined('RESTORE_SILENTLY')) {
7472 notify("Could not close the restorelog.html file");
7476 if (!defined('RESTORE_SILENTLY')) {
7477 //End the main ul
7478 echo "</ul>";
7480 //End the main table
7481 echo "</td></tr>";
7482 echo "</table>";
7485 return $status;
7487 //Create, open and write header of the html log file
7488 function restore_open_html($restore,$course_header) {
7490 global $CFG;
7492 $status = true;
7494 //Open file for writing
7495 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
7496 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
7497 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
7499 $status = check_dir_exists($dest_dir,true);
7500 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
7501 //Add the stylesheet
7502 $stylesheetshtml = '';
7503 foreach ($CFG->stylesheets as $stylesheet) {
7504 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
7506 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
7507 $languagehtml = get_html_lang($dir=true);
7509 //Write the header in the new logging file
7510 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
7511 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
7512 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
7513 fwrite ($restorelog_file,"<head>");
7514 fwrite ($restorelog_file,$stylesheetshtml);
7515 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
7516 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
7517 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
7518 $startdate = addslashes($course_header->course_startdate);
7519 $date = usergetdate($startdate);
7520 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
7521 $startdate += $restore->course_startdateoffset;
7522 $date = usergetdate($startdate);
7523 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
7525 if ($status) {
7526 return $restorelog_file;
7527 } else {
7528 return false;
7531 //Create & close footer of the html log file
7532 function restore_close_html($restore) {
7534 global $CFG;
7536 $status = true;
7538 //Open file for writing
7539 //First, check that course_id/backupdata folder exists in CFG->dataroot
7540 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
7541 $status = check_dir_exists($dest_dir, true, true);
7542 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
7543 //Write the footer to close the logging file
7544 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
7545 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
7547 if ($status) {
7548 return $restorelog_file;
7549 } else {
7550 return false;
7554 /********************** Roles and Capabilities Related Functions *******************************/
7556 /* Yu: Note recovering of role assignments/overrides need to take place after
7557 users have been recovered, i.e. after we get their new_id, and after all
7558 roles have been recreated or mapped. Contexts can be created on the fly.
7559 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
7560 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
7561 once roles and users have been restored.
7565 * This function restores all the needed roles for this course
7566 * i.e. roles with an assignment in any of the mods or blocks,
7567 * roles assigned on any user (e.g. parent role) and roles
7568 * assigned at course levle
7569 * This function should check for duplicate roles first
7570 * It isn't now, just overwriting
7572 function restore_create_roles($restore, $xmlfile) {
7573 if (!defined('RESTORE_SILENTLY')) {
7574 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
7576 $info = restore_read_xml_roles($xmlfile);
7578 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
7580 // the following code creates new roles
7581 // but we could use more intelligent detection, and role mapping
7582 // get role mapping info from $restore
7583 $rolemappings = array();
7585 if (!empty($restore->rolesmapping)) {
7586 $rolemappings = $restore->rolesmapping;
7588 // $info->roles will be empty for backups pre 1.7
7589 if (isset($info->roles) && $info->roles) {
7591 foreach ($info->roles as $oldroleid=>$roledata) {
7592 if (empty($restore->rolesmapping)) {
7593 // if this is empty altogether, we came from import or there's no roles used in course at all
7594 // in this case, write the same oldid as this is the same site
7595 // no need to do mapping
7596 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
7597 $oldroleid); // adding a new id
7598 continue; // do not create additonal roles;
7600 // first we check if the roles are in the mappings
7601 // if so, we just do a mapping i.e. update oldids table
7602 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
7603 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
7604 $rolemappings[$oldroleid]); // adding a new id
7606 } else {
7608 // code to make new role name/short name if same role name or shortname exists
7609 $fullname = $roledata->name;
7610 $shortname = $roledata->shortname;
7611 $currentfullname = "";
7612 $currentshortname = "";
7613 $counter = 0;
7615 do {
7616 if ($counter) {
7617 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
7618 $suffixshort = "_".$counter;
7619 } else {
7620 $suffixfull = "";
7621 $suffixshort = "";
7623 $currentfullname = $fullname.$suffixfull;
7624 // Limit the size of shortname - database column accepts <= 15 chars
7625 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
7626 $coursefull = get_record("role","name",addslashes($currentfullname));
7627 $courseshort = get_record("role","shortname",addslashes($currentshortname));
7628 $counter++;
7629 } while ($coursefull || $courseshort);
7631 $roledata->name = $currentfullname;
7632 $roledata->shortname= $currentshortname;
7634 // done finding a unique name
7636 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
7637 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
7638 $newroleid); // adding a new id
7639 foreach ($roledata->capabilities as $capability) {
7641 $roleinfo = new object();
7642 $roleinfo = (object)$capability;
7643 $roleinfo->contextid = $sitecontext->id;
7644 $roleinfo->capability = $capability->name;
7645 $roleinfo->roleid = $newroleid;
7647 insert_record('role_capabilities', $roleinfo);
7652 return true;
7656 * this function restores role assignments and role overrides
7657 * in course/user/block/mod level, it passed through
7658 * the xml file again
7660 function restore_roles_settings($restore, $xmlfile) {
7661 // data pulls from course, mod, user, and blocks
7663 /*******************************************************
7664 * Restoring from course level assignments *
7665 *******************************************************/
7666 if (!defined('RESTORE_SILENTLY')) {
7667 echo "<li>".get_string("creatingcourseroles").'</li>';
7669 $course = restore_read_xml_course_header($xmlfile);
7671 if (!isset($restore->rolesmapping)) {
7672 $isimport = true; // course import from another course, or course with no role assignments
7673 } else {
7674 $isimport = false; // course restore with role assignments
7677 if (!empty($course->roleassignments) && !$isimport) {
7678 $courseassignments = $course->roleassignments;
7680 foreach ($courseassignments as $oldroleid => $courseassignment) {
7681 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
7684 /*****************************************************
7685 * Restoring from course level overrides *
7686 *****************************************************/
7688 if (!empty($course->roleoverrides) && !$isimport) {
7689 $courseoverrides = $course->roleoverrides;
7690 foreach ($courseoverrides as $oldroleid => $courseoverride) {
7691 // if not importing into exiting course, or creating new role, we are ok
7692 // local course overrides to be respected (i.e. restored course overrides ignored)
7693 if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
7694 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
7699 /*******************************************************
7700 * Restoring role assignments/overrdies *
7701 * from module level assignments *
7702 *******************************************************/
7704 if (!defined('RESTORE_SILENTLY')) {
7705 echo "<li>".get_string("creatingmodroles").'</li>';
7707 $sections = restore_read_xml_sections($xmlfile);
7708 $secs = $sections->sections;
7710 foreach ($secs as $section) {
7711 if (isset($section->mods)) {
7712 foreach ($section->mods as $modid=>$mod) {
7713 if (isset($mod->roleassignments) && !$isimport) {
7714 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
7715 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
7718 // role overrides always applies, in import or backup/restore
7719 if (isset($mod->roleoverrides)) {
7720 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
7721 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
7728 /*************************************************
7729 * Restoring assignments from blocks level *
7730 * role assignments/overrides *
7731 *************************************************/
7733 if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
7734 if (!defined('RESTORE_SILENTLY')) {
7735 echo "<li>".get_string("creatingblocksroles").'</li>';
7737 $blocks = restore_read_xml_blocks($xmlfile);
7738 if (isset($blocks->instances)) {
7739 foreach ($blocks->instances as $instance) {
7740 if (isset($instance->roleassignments) && !$isimport) {
7741 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
7742 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
7746 // likewise block overrides should always be restored like mods
7747 if (isset($instance->roleoverrides)) {
7748 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
7749 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
7755 /************************************************
7756 * Restoring assignments from userid level *
7757 * role assignments/overrides *
7758 ************************************************/
7759 if (!defined('RESTORE_SILENTLY')) {
7760 echo "<li>".get_string("creatinguserroles").'</li>';
7762 $info = restore_read_xml_users($restore, $xmlfile);
7763 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
7764 //For each user, take its info from backup_ids
7765 foreach ($info->users as $userid) {
7766 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
7767 if (isset($rec->info->roleassignments)) {
7768 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
7769 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
7772 if (isset($rec->info->roleoverrides)) {
7773 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
7774 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
7780 return true;
7783 // auxillary function to write role assignments read from xml to db
7784 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
7786 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
7788 foreach ($assignments as $assignment) {
7790 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
7791 //Oh dear, $olduser... can be an object, $obj->string or bool!
7792 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
7793 continue;
7795 $assignment->userid = $olduser->new_id; // new userid here
7796 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
7797 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
7798 $assignment->roleid = $role->new_id; // restored new role id
7800 // hack to make the correct contextid for course level imports
7801 if ($contextlevel == CONTEXT_COURSE) {
7802 $oldinstance->new_id = $restore->course_id;
7803 } else {
7804 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
7807 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
7808 $assignment->contextid = $newcontext->id; // new context id
7809 // might already have same assignment
7810 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol, $assignment->timemodified);
7815 // auxillary function to write role assignments read from xml to db
7816 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
7818 // it is possible to have an override not relevant to this course context.
7819 // should be ignored(?)
7820 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
7821 return null;
7824 foreach ($overrides as $override) {
7825 $override->capability = $override->name;
7826 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
7827 $override->modifierid = $oldmodifier->new_id?$oldmodifier->new_id:0; // new modifier id here
7828 $override->roleid = $role->new_id; // restored new role id
7830 // hack to make the correct contextid for course level imports
7831 if ($contextlevel == CONTEXT_COURSE) {
7832 $oldinstance->new_id = $restore->course_id;
7833 } else {
7834 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
7837 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
7838 $override->contextid = $newcontext->id; // new context id
7839 // use assign capability instead so we can add context to context_rel
7840 assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
7843 //write activity date changes to the html log file, and update date values in the the xml array
7844 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
7846 global $CFG;
7847 $openlog = false;
7849 // loop through time fields in $TAGS
7850 foreach ($TAGS as $TAG) {
7852 // check $TAG has a sensible value
7853 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
7855 if ($openlog==false) {
7856 $openlog = true; // only come through here once
7858 // open file for writing
7859 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
7860 check_dir_exists($course_dir, true);
7861 $restorelog = fopen("$course_dir/restorelog.html", "a");
7863 // start output for this record
7864 $msg = new stdClass();
7865 $msg->recordtype = $recordtype;
7866 $msg->recordname = $xml[$NAMETAG][0]['#'];
7867 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
7870 // write old date to $restorelog
7871 $value = $xml[$TAG][0]['#'];
7872 $date = usergetdate($value);
7874 $msg = new stdClass();
7875 $msg->TAG = $TAG;
7876 $msg->weekday = $date['weekday'];
7877 $msg->mday = $date['mday'];
7878 $msg->month = $date['month'];
7879 $msg->year = $date['year'];
7880 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
7882 // write new date to $restorelog
7883 $value += $restore->course_startdateoffset;
7884 $date = usergetdate($value);
7886 $msg = new stdClass();
7887 $msg->TAG = $TAG;
7888 $msg->weekday = $date['weekday'];
7889 $msg->mday = $date['mday'];
7890 $msg->month = $date['month'];
7891 $msg->year = $date['year'];
7892 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
7894 // update $value in $xml tree for calling module
7895 $xml[$TAG][0]['#'] = "$value";
7898 // close the restore log, if it was opened
7899 if ($openlog) {
7900 fclose($restorelog);