Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / backup / restorelib.php
bloba2c6c4795c0ab48b65a8d46cae084913d04b9423
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 events (course) in
315 //backup_ids->info db (and event's id in $info)
316 function restore_read_xml_events ($restore,$xml_file) {
318 //We call the main read_xml function, with todo = EVENTS
319 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
321 return $info;
324 //This function read the xml file and store its data from the modules in
325 //backup_ids->info
326 function restore_read_xml_modules ($restore,$xml_file) {
328 //We call the main read_xml function, with todo = MODULES
329 $info = restore_read_xml ($xml_file,"MODULES",$restore);
331 return $info;
334 //This function read the xml file and store its data from the logs in
335 //backup_ids->info
336 function restore_read_xml_logs ($restore,$xml_file) {
338 //We call the main read_xml function, with todo = LOGS
339 $info = restore_read_xml ($xml_file,"LOGS",$restore);
341 return $info;
344 function restore_read_xml_roles ($xml_file) {
345 //We call the main read_xml function, with todo = ROLES
346 $info = restore_read_xml ($xml_file,"ROLES",false);
348 return $info;
351 //This function prints the contents from the info parammeter passed
352 function restore_print_info ($info) {
354 global $CFG;
356 $status = true;
357 if ($info) {
358 //This is tha align to every ingo table
359 $table->align = array ("right","left");
360 //This is the nowrap clause
361 $table->wrap = array ("","nowrap");
362 //The width
363 $table->width = "70%";
364 //Put interesting info in table
365 //The backup original name
366 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
367 $tab[0][1] = $info->backup_name;
368 //The moodle version
369 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
370 $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
371 //The backup version
372 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
373 $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
374 //The backup date
375 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
376 $tab[3][1] = userdate($info->backup_date);
377 //Print title
378 print_heading(get_string("backup").":");
379 $table->data = $tab;
380 //Print backup general info
381 print_table($table);
383 if ($info->backup_backup_version <= 2005070500) {
384 notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
387 //Now backup contents in another table
388 $tab = array();
389 //First mods info
390 $mods = $info->mods;
391 $elem = 0;
392 foreach ($mods as $key => $mod) {
393 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
394 if ($mod->backup == "false") {
395 $tab[$elem][1] = get_string("notincluded");
396 } else {
397 if ($mod->userinfo == "true") {
398 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
399 } else {
400 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
402 if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
403 foreach ($mod->instances as $instance) {
404 if ($instance->backup) {
405 $elem++;
406 $tab[$elem][0] = $instance->name;
407 if ($instance->userinfo == 'true') {
408 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
409 } else {
410 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
416 $elem++;
418 //Metacourse info
419 $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
420 if ($info->backup_metacourse == "true") {
421 $tab[$elem][1] = get_string("yes");
422 } else {
423 $tab[$elem][1] = get_string("no");
425 $elem++;
426 //Users info
427 $tab[$elem][0] = "<b>".get_string("users").":</b>";
428 $tab[$elem][1] = get_string($info->backup_users);
429 $elem++;
430 //Logs info
431 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
432 if ($info->backup_logs == "true") {
433 $tab[$elem][1] = get_string("yes");
434 } else {
435 $tab[$elem][1] = get_string("no");
437 $elem++;
438 //User Files info
439 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
440 if ($info->backup_user_files == "true") {
441 $tab[$elem][1] = get_string("yes");
442 } else {
443 $tab[$elem][1] = get_string("no");
445 $elem++;
446 //Course Files info
447 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
448 if ($info->backup_course_files == "true") {
449 $tab[$elem][1] = get_string("yes");
450 } else {
451 $tab[$elem][1] = get_string("no");
453 $elem++;
454 //Messages info (only showed if present)
455 if ($info->backup_messages == 'true') {
456 $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
457 $tab[$elem][1] = get_string('yes');
458 $elem++;
459 } else {
460 //Do nothing
462 $table->data = $tab;
463 //Print title
464 print_heading(get_string("backupdetails").":");
465 //Print backup general info
466 print_table($table);
467 } else {
468 $status = false;
471 return $status;
474 //This function prints the contents from the course_header parammeter passed
475 function restore_print_course_header ($course_header) {
477 $status = true;
478 if ($course_header) {
479 //This is tha align to every ingo table
480 $table->align = array ("right","left");
481 //The width
482 $table->width = "70%";
483 //Put interesting course header in table
484 //The course name
485 $tab[0][0] = "<b>".get_string("name").":</b>";
486 $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
487 //The course summary
488 $tab[1][0] = "<b>".get_string("summary").":</b>";
489 $tab[1][1] = $course_header->course_summary;
490 $table->data = $tab;
491 //Print title
492 print_heading(get_string("course").":");
493 //Print backup course header info
494 print_table($table);
495 } else {
496 $status = false;
498 return $status;
501 //This function create a new course record.
502 //When finished, course_header contains the id of the new course
503 function restore_create_new_course($restore,&$course_header) {
505 global $CFG;
507 $status = true;
509 $fullname = $course_header->course_fullname;
510 $shortname = $course_header->course_shortname;
511 $currentfullname = "";
512 $currentshortname = "";
513 $counter = 0;
514 //Iteratere while the name exists
515 do {
516 if ($counter) {
517 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
518 $suffixshort = "_".$counter;
519 } else {
520 $suffixfull = "";
521 $suffixshort = "";
523 $currentfullname = $fullname.$suffixfull;
524 // Limit the size of shortname - database column accepts <= 15 chars
525 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
526 $coursefull = get_record("course","fullname",addslashes($currentfullname));
527 $courseshort = get_record("course","shortname",addslashes($currentshortname));
528 $counter++;
529 } while ($coursefull || $courseshort);
531 //New name = currentname
532 $course_header->course_fullname = $currentfullname;
533 $course_header->course_shortname = $currentshortname;
535 // first try to get it from restore
536 if ($restore->restore_restorecatto) {
537 $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
540 // else we try to get it from the xml file
541 //Now calculate the category
542 if (!$category) {
543 $category = get_record("course_categories","id",$course_header->category->id,
544 "name",addslashes($course_header->category->name));
547 //If no exists, try by name only
548 if (!$category) {
549 $category = get_record("course_categories","name",addslashes($course_header->category->name));
552 //If no exists, get category id 1
553 if (!$category) {
554 $category = get_record("course_categories","id","1");
557 //If category 1 doesn'exists, lets create the course category (get it from backup file)
558 if (!$category) {
559 $ins_category->name = addslashes($course_header->category->name);
560 $ins_category->parent = 0;
561 $ins_category->sortorder = 0;
562 $ins_category->coursecount = 0;
563 $ins_category->visible = 0; //To avoid interferences with the rest of the site
564 $ins_category->timemodified = time();
565 $newid = insert_record("course_categories",$ins_category);
566 $category->id = $newid;
567 $category->name = $course_header->category->name;
569 //If exists, put new category id
570 if ($category) {
571 $course_header->category->id = $category->id;
572 $course_header->category->name = $category->name;
573 //Error, cannot locate category
574 } else {
575 $course_header->category->id = 0;
576 $course_header->category->name = get_string("unknowncategory");
577 $status = false;
580 //Create the course_object
581 if ($status) {
582 $course->category = addslashes($course_header->category->id);
583 $course->password = addslashes($course_header->course_password);
584 $course->fullname = addslashes($course_header->course_fullname);
585 $course->shortname = addslashes($course_header->course_shortname);
586 $course->idnumber = addslashes($course_header->course_idnumber);
587 $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
588 $course->summary = restore_decode_absolute_links(addslashes($course_header->course_summary));
589 $course->format = addslashes($course_header->course_format);
590 $course->showgrades = addslashes($course_header->course_showgrades);
591 $course->newsitems = addslashes($course_header->course_newsitems);
592 $course->teacher = addslashes($course_header->course_teacher);
593 $course->teachers = addslashes($course_header->course_teachers);
594 $course->student = addslashes($course_header->course_student);
595 $course->students = addslashes($course_header->course_students);
596 $course->guest = addslashes($course_header->course_guest);
597 $course->startdate = addslashes($course_header->course_startdate);
598 $course->startdate += $restore->course_startdateoffset;
599 $course->numsections = addslashes($course_header->course_numsections);
600 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
601 $course->maxbytes = addslashes($course_header->course_maxbytes);
602 $course->showreports = addslashes($course_header->course_showreports);
603 if (isset($course_header->course_groupmode)) {
604 $course->groupmode = addslashes($course_header->course_groupmode);
606 if (isset($course_header->course_groupmodeforce)) {
607 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
609 $course->lang = addslashes($course_header->course_lang);
610 $course->theme = addslashes($course_header->course_theme);
611 $course->cost = addslashes($course_header->course_cost);
612 $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
613 $course->marker = addslashes($course_header->course_marker);
614 $course->visible = addslashes($course_header->course_visible);
615 $course->hiddensections = addslashes($course_header->course_hiddensections);
616 $course->timecreated = addslashes($course_header->course_timecreated);
617 $course->timemodified = addslashes($course_header->course_timemodified);
618 $course->metacourse = addslashes($course_header->course_metacourse);
619 $course->expirynotify = isset($course_header->course_expirynotify) ? addslashes($course_header->course_expirynotify):0;
620 $course->notifystudents = isset($course_header->course_notifystudents) ? addslashes($course_header->course_notifystudents) : 0;
621 $course->expirythreshold = isset($course_header->course_expirythreshold) ? addslashes($course_header->course_expirythreshold) : 0;
622 $course->enrollable = isset($course_header->course_enrollable) ? addslashes($course_header->course_enrollable) : 1;
623 $course->enrolstartdate = isset($course_header->course_enrolstartdate) ? addslashes($course_header->course_enrolstartdate) : 0;
624 if ($course->enrolstartdate) { //Roll course dates
625 $course->enrolstartdate += $restore->course_startdateoffset;
627 $course->enrolenddate = isset($course_header->course_enrolenddate) ? addslashes($course_header->course_enrolenddate) : 0;
628 if ($course->enrolenddate) { //Roll course dates
629 $course->enrolenddate += $restore->course_startdateoffset;
631 $course->enrolperiod = addslashes($course_header->course_enrolperiod);
632 //Calculate sortorder field
633 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
634 FROM ' . $CFG->prefix . 'course
635 WHERE category=' . $course->category);
636 if (!empty($sortmax->max)) {
637 $course->sortorder = $sortmax->max + 1;
638 unset($sortmax);
639 } else {
640 $course->sortorder = 100;
643 //Now, recode some languages (Moodle 1.5)
644 if ($course->lang == 'ma_nt') {
645 $course->lang = 'mi_nt';
648 //Disable course->metacourse if avoided in restore config
649 if (!$restore->metacourse) {
650 $course->metacourse = 0;
653 //Check if the theme exists in destination server
654 $themes = get_list_of_themes();
655 if (!in_array($course->theme, $themes)) {
656 $course->theme = '';
659 //Now insert the record
660 $newid = insert_record("course",$course);
661 if ($newid) {
662 //save old and new course id
663 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
664 //Replace old course_id in course_header
665 $course_header->course_id = $newid;
666 } else {
667 $status = false;
671 return $status;
675 * Returns the best question category (id) found to restore one
676 * question category from a backup file. Works by stamp (since Moodle 1.1)
677 * or by name (for older versions).
679 * @param object $cat the question_categories record to be searched
680 * @param integer $courseid the course where we are restoring
681 * @return integer the id of a existing question_category or 0 (not found)
683 function restore_get_best_question_category($cat, $courseid) {
685 $found = 0;
687 //Decide how to work (by stamp or name)
688 if ($cat->stamp) {
689 $searchfield = 'stamp';
690 $searchvalue = $cat->stamp;
691 } else {
692 $searchfield = 'name';
693 $searchvalue = $cat->name;
696 //First shot. Try to get the category from the course being restored
697 if ($fcat = get_record('question_categories','course',$courseid,$searchfield,$searchvalue)) {
698 $found = $fcat->id;
699 //Second shot. Try to obtain any concordant category and check its publish status and editing rights
700 } else if ($fcats = get_records('question_categories', $searchfield, $searchvalue, 'id', 'id, publish, course')) {
701 foreach ($fcats as $fcat) {
702 if ($fcat->publish == 1 && has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $fcat->course))) {
703 $found = $fcat->id;
704 break;
709 return $found;
712 //This function creates all the block stuff when restoring courses
713 //It calls selectively to restore_create_block_instances() for 1.5
714 //and above backups. Upwards compatible with old blocks.
715 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
717 $status = true;
719 delete_records('block_instance', 'pageid', $restore->course_id, 'pagetype', PAGE_COURSE_VIEW);
720 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
721 if (empty($blockinfo)) {
722 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
723 $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
724 blocks_repopulate_page($newpage);
725 } else {
726 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
727 $blockrecords = get_records_select('block', '', '', 'name, id');
728 $temp_blocks_l = array();
729 $temp_blocks_r = array();
730 @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
731 $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
732 foreach($temp_blocks as $blockposition => $blocks) {
733 $blockweight = 0;
734 foreach($blocks as $blockname) {
735 if(!isset($blockrecords[$blockname])) {
736 // We don't know anything about this block!
737 continue;
739 $blockinstance = new stdClass;
740 // Remove any - prefix before doing the name-to-id mapping
741 if(substr($blockname, 0, 1) == '-') {
742 $blockname = substr($blockname, 1);
743 $blockinstance->visible = 0;
744 } else {
745 $blockinstance->visible = 1;
747 $blockinstance->blockid = $blockrecords[$blockname]->id;
748 $blockinstance->pageid = $restore->course_id;
749 $blockinstance->pagetype = PAGE_COURSE_VIEW;
750 $blockinstance->position = $blockposition;
751 $blockinstance->weight = $blockweight;
752 if(!$status = insert_record('block_instance', $blockinstance)) {
753 $status = false;
755 ++$blockweight;
759 } else if($backup_block_format == 'instances') {
760 $status = restore_create_block_instances($restore,$xml_file);
763 return $status;
767 //This function creates all the block_instances from xml when restoring in a
768 //new course
769 function restore_create_block_instances($restore,$xml_file) {
771 $status = true;
772 //Check it exists
773 if (!file_exists($xml_file)) {
774 $status = false;
776 //Get info from xml
777 if ($status) {
778 $info = restore_read_xml_blocks($xml_file);
781 if(empty($info->instances)) {
782 return $status;
785 // First of all, iterate over the blocks to see which distinct pages we have
786 // in our hands and arrange the blocks accordingly.
787 $pageinstances = array();
788 foreach($info->instances as $instance) {
790 //pagetype and pageid black magic, we have to handle the case of blocks for the
791 //course, blocks from other pages in that course etc etc etc.
793 if($instance->pagetype == PAGE_COURSE_VIEW) {
794 // This one's easy...
795 $instance->pageid = $restore->course_id;
797 else {
798 $parts = explode('-', $instance->pagetype);
799 if($parts[0] == 'mod') {
800 if(!$restore->mods[$parts[1]]->restore) {
801 continue;
803 $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
804 $instance->pageid = $getid->new_id;
806 else {
807 // Not invented here ;-)
808 continue;
812 if(!isset($pageinstances[$instance->pagetype])) {
813 $pageinstances[$instance->pagetype] = array();
815 if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
816 $pageinstances[$instance->pagetype][$instance->pageid] = array();
819 $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
822 $blocks = get_records_select('block', '', '', 'name, id, multiple');
824 // For each type of page we have restored
825 foreach($pageinstances as $thistypeinstances) {
827 // For each page id of that type
828 foreach($thistypeinstances as $thisidinstances) {
830 $addedblocks = array();
831 $maxweights = array();
833 // For each block instance in that page
834 foreach($thisidinstances as $instance) {
836 if(!isset($blocks[$instance->name])) {
837 //We are trying to restore a block we don't have...
838 continue;
841 //If we have already added this block once and multiples aren't allowed, disregard it
842 if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
843 continue;
846 //If its the first block we add to a new position, start weight counter equal to 0.
847 if(empty($maxweights[$instance->position])) {
848 $maxweights[$instance->position] = 0;
851 //If the instance weight is greater than the weight counter (we skipped some earlier
852 //blocks most probably), bring it back in line.
853 if($instance->weight > $maxweights[$instance->position]) {
854 $instance->weight = $maxweights[$instance->position];
857 //Add this instance
858 $instance->blockid = $blocks[$instance->name]->id;
860 if ($newid = insert_record('block_instance', $instance)) {
861 if (!empty($instance->id)) { // this will only be set if we come from 1.7 and above backups
862 backup_putid ($restore->backup_unique_code,"block_instance",$instance->id,$newid);
864 } else {
865 $status = false;
866 break;
869 //Get an object for the block and tell it it's been restored so it can update dates
870 //etc. if necessary
871 $blockobj=block_instance($instance->name,$instance);
872 $blockobj->after_restore($restore);
874 //Now we can increment the weight counter
875 ++$maxweights[$instance->position];
877 //Keep track of block types we have already added
878 $addedblocks[$instance->name] = true;
884 return $status;
887 //This function creates all the course_sections and course_modules from xml
888 //when restoring in a new course or simply checks sections and create records
889 //in backup_ids when restoring in a existing course
890 function restore_create_sections(&$restore, $xml_file) {
892 global $CFG,$db;
894 $status = true;
895 //Check it exists
896 if (!file_exists($xml_file)) {
897 $status = false;
899 //Get info from xml
900 if ($status) {
901 $info = restore_read_xml_sections($xml_file);
903 //Put the info in the DB, recoding ids and saving the in backup tables
905 $sequence = "";
907 if ($info) {
908 //For each, section, save it to db
909 foreach ($info->sections as $key => $sect) {
910 $sequence = "";
911 $section->course = $restore->course_id;
912 $section->section = $sect->number;
913 $section->summary = restore_decode_absolute_links(addslashes($sect->summary));
914 $section->visible = $sect->visible;
915 $section->sequence = "";
916 //Now calculate the section's newid
917 $newid = 0;
918 if ($restore->restoreto == 2) {
919 //Save it to db (only if restoring to new course)
920 $newid = insert_record("course_sections",$section);
921 } else {
922 //Get section id when restoring in existing course
923 $rec = get_record("course_sections","course",$restore->course_id,
924 "section",$section->section);
925 //If that section doesn't exist, get section 0 (every mod will be
926 //asigned there
927 if(!$rec) {
928 $rec = get_record("course_sections","course",$restore->course_id,
929 "section","0");
931 //New check. If section 0 doesn't exist, insert it here !!
932 //Teorically this never should happen but, in practice, some users
933 //have reported this issue.
934 if(!$rec) {
935 $zero_sec->course = $restore->course_id;
936 $zero_sec->section = 0;
937 $zero_sec->summary = "";
938 $zero_sec->sequence = "";
939 $newid = insert_record("course_sections",$zero_sec);
940 $rec->id = $newid;
941 $rec->sequence = "";
943 $newid = $rec->id;
944 $sequence = $rec->sequence;
946 if ($newid) {
947 //save old and new section id
948 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
949 } else {
950 $status = false;
952 //If all is OK, go with associated mods
953 if ($status) {
954 //If we have mods in the section
955 if (!empty($sect->mods)) {
956 //For each mod inside section
957 foreach ($sect->mods as $keym => $mod) {
958 // Yu: This part is called repeatedly for every instance,
959 // so it is necessary to set the granular flag and check isset()
960 // when the first instance of this type of mod is processed.
962 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
964 if (!isset($restore->mods[$mod->type]->granular)) {
965 if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
966 // This defines whether we want to restore specific
967 // instances of the modules (granular restore), or
968 // whether we don't care and just want to restore
969 // all module instances (non-granular).
970 $restore->mods[$mod->type]->granular = true;
971 } else {
972 $restore->mods[$mod->type]->granular = false;
976 //Check if we've to restore this module (and instance)
977 if (!empty($restore->mods[$mod->type]->restore)) {
978 if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
979 || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
980 && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
982 //Get the module id from modules
983 $module = get_record("modules","name",$mod->type);
984 if ($module) {
985 $course_module->course = $restore->course_id;
986 $course_module->module = $module->id;
987 $course_module->section = $newid;
988 $course_module->added = $mod->added;
989 $course_module->score = $mod->score;
990 $course_module->indent = $mod->indent;
991 $course_module->visible = $mod->visible;
992 if (isset($mod->groupmode)) {
993 $course_module->groupmode = $mod->groupmode;
995 $course_module->instance = 0;
996 //NOTE: The instance (new) is calculated and updated in db in the
997 // final step of the restore. We don't know it yet.
998 //print_object($course_module); //Debug
999 //Save it to db
1001 $newidmod = insert_record("course_modules",$course_module);
1002 if ($newidmod) {
1003 //save old and new module id
1004 //In the info field, we save the original instance of the module
1005 //to use it later
1006 backup_putid ($restore->backup_unique_code,"course_modules",
1007 $keym,$newidmod,$mod->instance);
1009 $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
1010 } else {
1011 $status = false;
1013 //Now, calculate the sequence field
1014 if ($status) {
1015 if ($sequence) {
1016 $sequence .= ",".$newidmod;
1017 } else {
1018 $sequence = $newidmod;
1021 } else {
1022 $status = false;
1029 //If all is OK, update sequence field in course_sections
1030 if ($status) {
1031 if (isset($sequence)) {
1032 $update_rec->id = $newid;
1033 $update_rec->sequence = $sequence;
1034 $status = update_record("course_sections",$update_rec);
1038 } else {
1039 $status = false;
1041 return $status;
1044 //Called to set up any course-format specific data that may be in the file
1045 function restore_set_format_data($restore,$xml_file) {
1046 global $CFG,$db;
1048 $status = true;
1049 //Check it exists
1050 if (!file_exists($xml_file)) {
1051 return false;
1053 //Load data from XML to info
1054 if(!($info = restore_read_xml_formatdata($xml_file))) {
1055 return false;
1058 //Process format data if there is any
1059 if (isset($info->format_data)) {
1060 if(!$format=get_field('course','format','id',$restore->course_id)) {
1061 return false;
1063 // If there was any data then it must have a restore method
1064 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
1065 if(!file_exists($file)) {
1066 return false;
1068 require_once($file);
1069 $function=$format.'_restore_format_data';
1070 if(!function_exists($function)) {
1071 return false;
1073 return $function($restore,$info->format_data);
1076 // If we got here then there's no data, but that's cool
1077 return true;
1080 //This function creates all the metacourse data from xml, notifying
1081 //about each incidence
1082 function restore_create_metacourse($restore,$xml_file) {
1084 global $CFG,$db;
1086 $status = true;
1087 //Check it exists
1088 if (!file_exists($xml_file)) {
1089 $status = false;
1091 //Get info from xml
1092 if ($status) {
1093 //Load data from XML to info
1094 $info = restore_read_xml_metacourse($xml_file);
1097 //Process info about metacourse
1098 if ($status and $info) {
1099 //Process child records
1100 if (!empty($info->childs)) {
1101 foreach ($info->childs as $child) {
1102 $dbcourse = false;
1103 $dbmetacourse = false;
1104 //Check if child course exists in destination server
1105 //(by id in the same server or by idnumber and shortname in other server)
1106 if ($restore->original_wwwroot == $CFG->wwwroot) {
1107 //Same server, lets see by id
1108 $dbcourse = get_record('course','id',$child->id);
1109 } else {
1110 //Different server, lets see by idnumber and shortname, and only ONE record
1111 $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1112 if ($dbcount == 1) {
1113 $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1116 //If child course has been found, insert data
1117 if ($dbcourse) {
1118 $dbmetacourse->child_course = $dbcourse->id;
1119 $dbmetacourse->parent_course = $restore->course_id;
1120 $status = insert_record ('course_meta',$dbmetacourse);
1121 } else {
1122 //Child course not found, notice!
1123 if (!defined('RESTORE_SILENTLY')) {
1124 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
1128 //Now, recreate student enrolments...
1129 sync_metacourse($restore->course_id);
1131 //Process parent records
1132 if (!empty($info->parents)) {
1133 foreach ($info->parents as $parent) {
1134 $dbcourse = false;
1135 $dbmetacourse = false;
1136 //Check if parent course exists in destination server
1137 //(by id in the same server or by idnumber and shortname in other server)
1138 if ($restore->original_wwwroot == $CFG->wwwroot) {
1139 //Same server, lets see by id
1140 $dbcourse = get_record('course','id',$parent->id);
1141 } else {
1142 //Different server, lets see by idnumber and shortname, and only ONE record
1143 $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1144 if ($dbcount == 1) {
1145 $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1148 //If parent course has been found, insert data if it is a metacourse
1149 if ($dbcourse) {
1150 if ($dbcourse->metacourse) {
1151 $dbmetacourse->parent_course = $dbcourse->id;
1152 $dbmetacourse->child_course = $restore->course_id;
1153 $status = insert_record ('course_meta',$dbmetacourse);
1154 //Now, recreate student enrolments in parent course
1155 sync_metacourse($dbcourse->id);
1156 } else {
1157 //Parent course isn't metacourse, notice!
1158 if (!defined('RESTORE_SILENTLY')) {
1159 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1162 } else {
1163 //Parent course not found, notice!
1164 if (!defined('RESTORE_SILENTLY')) {
1165 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1172 return $status;
1175 //This function creates all the gradebook data from xml, notifying
1176 //about each incidence
1177 function restore_create_gradebook($restore,$xml_file) {
1179 global $CFG,$db;
1181 $status = true;
1182 //Check it exists
1183 if (!file_exists($xml_file)) {
1184 return false;
1187 // Get info from xml
1188 // info will contain the number of record to process
1189 $info = restore_read_xml_gradebook($restore, $xml_file);
1191 // If we have info, then process
1192 if ($info <= 0) {
1193 return $status;
1196 // Count how many we have
1197 $categoriescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories');
1198 $itemscount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items');
1199 $outcomecount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes');
1201 // we need to know if all grade items that were backed up are being restored
1202 // if that is not the case, we do not restore grade categories nor gradeitems of category type
1203 // i.e. the aggregated grades of that category
1205 $restoreall = true; // set to false if any grade_item is not selected/restored
1207 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'", "old_id", "old_id, old_id")) {
1208 foreach ($recs as $rec) {
1211 if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
1213 $info = $data->info;
1214 // do not restore if this grade_item is a mod, and
1215 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1218 if ($itemtype == 'mod') {
1220 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1221 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1222 if (!restore_userdata_selected($restore, $itemmodule, $iteminstance)) {
1223 // module instance not selected when restored using granular
1224 // we are not restoring all grade items, set flag to false
1225 // so that we do not process grade categories and related grade items/grades
1226 $restoreall = false;
1227 break;
1234 // return if nothing to restore
1235 if (!$itemscount && !$categoriescount && !outcomecount) {
1236 return $status;
1239 // Start ul
1240 if (!defined('RESTORE_SILENTLY')) {
1241 echo '<ul>';
1243 // Number of records to get in every chunk
1244 $recordset_size = 2;
1245 // Flag to mark if we must continue
1246 $continue = true;
1248 // Process categories
1249 if ($categoriescount && $continue && $restoreall) {
1250 if (!defined('RESTORE_SILENTLY')) {
1251 echo '<li>'.get_string('gradecategories','grades').'</li>';
1253 $counter = 0;
1254 while ($counter < $categoriescount) {
1255 // Fetch recordset_size records in each iteration
1256 $recs = get_records_select("backup_ids","table_name = 'grade_categories' AND backup_code = '$restore->backup_unique_code'",
1257 "old_id",
1258 "old_id, old_id",
1259 $counter,
1260 $recordset_size);
1261 if ($recs) {
1262 foreach ($recs as $rec) {
1263 // Get the full record from backup_ids
1264 $data = backup_getid($restore->backup_unique_code,'grade_categories',$rec->old_id);
1265 if ($data) {
1266 // Now get completed xmlized object
1267 $info = $data->info;
1268 //traverse_xmlize($info); //Debug
1269 //print_object ($GLOBALS['traverse_array']); //Debug
1270 //$GLOBALS['traverse_array']=""; //Debug
1271 //Now build the GRADE_PREFERENCES record structure
1273 $dbrec->courseid = $restore->course_id;
1275 // get the new grade category parent
1276 if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#'])) {
1277 $parent = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
1278 $dbrec->parent = $parent->new_id;
1281 $dbrec->fullname = backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
1282 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
1283 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
1284 $dbrec->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROPLOW']['0']['#']);
1285 $dbrec->hidden = backup_todb($info['GRADE_CATEGORY']['#']['HIDDEN']['0']['#']);
1287 //Structure is equal to db, insert record
1288 //if the fullname doesn't exist
1289 if (!$prerec = get_record('grade_categories','courseid',$dbrec->courseid,'fullname',$dbrec->fullname)) {
1290 $newid = insert_record('grade_categories',$dbrec);
1291 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$newid);
1292 // update this record so we can put in the right paths
1293 // this can only be done after we got the new id
1294 $dbrec->id = $newid;
1295 include_once($CFG->dirroot.'/lib/grade/grade_category.php');
1296 // rebuild the path, we need only parents info
1297 // the order of restoring should ensure that the parent and grandparent(s)
1298 // are already restored
1299 $dbrec->path = grade_category::build_path($dbrec);
1300 // this is not needed in the xml because
1301 // given this parent and grandparent(s) we can recalculate the depth
1302 $dbrec->depth = substr_count($dbrec->path, '/');
1303 update_record('grade_categories', $dbrec);
1304 } else {
1305 // if fullname already exists, we should keep the current grade category
1306 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$rec->oldid);
1309 //Increment counters
1310 $counter++;
1311 //Do some output
1312 if ($counter % 1 == 0) {
1313 if (!defined('RESTORE_SILENTLY')) {
1314 echo ".";
1315 if ($counter % 20 == 0) {
1316 echo "<br />";
1319 backup_flush(300);
1326 // process outcomes
1327 if ($outcomecount && $continue) {
1328 if (!defined('RESTORE_SILENTLY')) {
1329 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1331 $counter = 0;
1332 while ($counter < $outcomecount) {
1333 //Fetch recordset_size records in each iteration
1334 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1335 "old_id",
1336 "old_id, old_id",
1337 $counter,
1338 $recordset_size);
1339 if ($recs) {
1340 foreach ($recs as $rec) {
1341 //Get the full record from backup_ids
1342 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1343 if ($data) {
1344 //Now get completed xmlized object
1345 $info = $data->info;
1346 //traverse_xmlize($info); //Debug
1347 //print_object ($GLOBALS['traverse_array']); //Debug
1348 //$GLOBALS['traverse_array']=""; //Debug
1349 //Now build the GRADE_PREFERENCES record structure
1351 $dbrec->courseid = $restore->course_id;
1352 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1353 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#']);
1355 if ($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']) {
1356 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']));
1357 $derec->scaleid = $scale->new_id;
1360 $modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#']));
1361 $derec->usermodified = $modifier->new_id;
1363 // Structure is equal to db, insert record
1364 // If the shortname doesn't exist
1365 if (!$prerec = get_record('grade_outcomes','courseid',$dbrec->courseid,'shortname',$dbrec->shortname)) {
1366 $status = insert_record('grade_outcomes',$dbrec);
1369 //Increment counters
1370 $counter++;
1371 //Do some output
1372 if ($counter % 1 == 0) {
1373 if (!defined('RESTORE_SILENTLY')) {
1374 echo ".";
1375 if ($counter % 20 == 0) {
1376 echo "<br />";
1379 backup_flush(300);
1386 // Process grade items (grade_raw, grade_final, and grade_text)
1387 if ($itemscount && $continue) {
1388 if (!defined('RESTORE_SILENTLY')) {
1389 echo '<li>'.get_string('gradeitems','grades').'</li>';
1391 $counter = 0;
1392 $counteritems = 0;
1393 while ($counteritems < $itemscount) {
1395 //Fetch recordset_size records in each iteration
1396 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1397 "old_id",
1398 "old_id, old_id",
1399 $counteritems,
1400 $recordset_size);
1401 if ($recs) {
1402 foreach ($recs as $rec) {
1403 //Get the full record from backup_ids
1404 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
1405 if ($data) {
1407 //Now get completed xmlized object
1408 $info = $data->info;
1409 //traverse_xmlize($info); //Debug
1410 //print_object ($GLOBALS['traverse_array']); //Debug
1411 //$GLOBALS['traverse_array']=""; //Debug
1413 $dbrec->courseid = $restore->course_id;
1415 if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'])) {
1417 $category = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
1418 $dbrec->categoryid = $category->new_id;
1421 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
1422 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1423 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1424 /// this needs to point to either the new mod id
1425 /// or the category id
1426 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1428 // do not restore if this grade_item is a mod, and
1429 if ($dbrec->itemtype == 'mod') {
1431 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
1432 // module instance not selected when restored using granular
1433 // skip this item
1434 $counteritems++;
1435 continue;
1438 // iteminstance should point to new mod
1440 $cm = backup_getid($restore->backup_unique_code,'course_modules', $iteminstance);
1441 $dbrec->iteminstance = $cm->new_id;
1443 } else if ($dbrec->itemtype == 'category') {
1444 // the item instance should point to the new grade category
1446 // only proceed if we are restoring all grade items
1447 if ($restoreall) {
1448 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
1449 $dbrec->iteminstance = $category->new_id;
1450 } else {
1451 // otherwise we can safely ignore this grade item and subsequent
1452 // grade_raws, grade_finals etc
1453 continue;
1457 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
1458 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#']);
1459 $dbrec->idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#']);
1460 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#']);
1461 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#']);
1462 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#']);
1463 /// needs to be restored first
1465 if ($info['GRADE_ITEM']['#']['SCALEID']['0']['#']) {
1466 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#']));
1467 $derec->scaleid = $scale->new_id;
1470 /// needs to be restored first
1471 $dbrec->outcomeid = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
1472 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#']);
1473 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
1474 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
1475 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);
1476 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);
1477 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);
1479 /// if thesse 5 all match then we know this item is already in db
1482 $itemex = get_record_sql('SELECT id,id FROM grade_items
1483 WHERE courseid = '.$dbrec->courseid.'
1484 AND itemtype = '.$dbrec->itemtype.'
1485 AND itemmodule = '.$dbrec->itemmodule.'
1486 AND iteminstance = '.$dbrec->iteminstance.'
1487 AND itemnumber = '.$dbrec->itemnumber);
1489 if (!$itemex) {
1490 //Structure is equal to db, insert record
1491 $itemid = insert_record('grade_items',$dbrec);
1492 } else {
1493 //Simply remap category
1494 $itemid = $itemex->id;
1498 // always insert, since modules restored to existing courses are always inserted
1500 // get the current sortorder, add 1 to it and use that
1502 if ($lastitem = get_record_sql("SELECT sortorder, id FROM {$CFG->prefix}grade_items
1503 WHERE courseid = $restore->course_id
1504 ORDER BY sortorder DESC ", true)) {
1506 // we just need the first one
1507 $dbrec->sortorder = $lastitem->sortorder + 1;
1508 } else {
1509 // this is the first grade_item
1510 $dbrec->sortorder = 0;
1513 $itemid = insert_record('grade_items',$dbrec);
1515 /// now, restore grade_grades, grade_text
1516 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1517 //Iterate over items
1518 for($i = 0; $i < sizeof($grades); $i++) {
1519 $ite_info = $grades[$i];
1520 //traverse_xmlize($ite_info);
1521 //Debug
1522 //print_object ($GLOBALS['traverse_array']); //Debug
1523 //$GLOBALS['traverse_array']=""; //Debug
1524 //Now build the GRADE_ITEM record structure
1525 $grade = new object();
1526 $grade->itemid = $itemid;
1527 $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));
1528 $grade->userid = $user->new_id;
1529 $grade->rawgrade = backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
1530 $grade->rawgrademax = backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
1531 $grade->rawgrademin = backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
1532 // need to find scaleid
1533 if ($ite_info['#']['RAWSCALEID']['0']['#']) {
1534 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
1535 $grade->rawscaleid = $scale->new_id;
1537 $grade->finalgrade = backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
1538 $grade->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
1539 $grade->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
1540 $grade->locktime = backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
1541 $grade->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
1542 $grade->overridden = backup_todb($ite_info['#']['OVERRIDDEN']['0']['#']);
1543 $grade->excluded = backup_todb($ite_info['#']['EXCLUDED']['0']['#']);
1545 insert_record('grade_grades', $grade);
1547 $counter++;
1548 if ($counter % 20 == 0) {
1549 if (!defined('RESTORE_SILENTLY')) {
1550 echo ".";
1551 if ($counter % 400 == 0) {
1552 echo "<br />";
1555 backup_flush(300);
1561 /// processing grade_grades_text
1562 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']) && ($texts = $info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']['GRADE_TEXT'])) {
1563 //Iterate over items
1564 for($i = 0; $i < sizeof($texts); $i++) {
1565 $ite_info = $texts[$i];
1566 //traverse_xmlize($ite_info); //Debug
1567 //print_object ($GLOBALS['traverse_array']); //Debug
1568 //$GLOBALS['traverse_array']=""; //Debug
1569 $grade = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($ite_info['#']['GRADEID']['0']['#']));
1570 $text->gradeid = $grade->new_id;
1571 $text->information = backup_todb($ite_info['#']['INFORMATION']['0']['#']);
1572 $text->informationformat = backup_todb($ite_info['#']['INFORMATIONFORMAT']['0']['#']);
1573 $text->feedback = backup_todb($ite_info['#']['FEEDBACK']['0']['#']);
1574 $text->feedbackformat = backup_todb($ite_info['#']['FEEDBACKFORMAT']['0']['#']);
1576 insert_record('grade_grades_text', $text);
1578 $counter++;
1579 if ($counter % 20 == 0) {
1580 if (!defined('RESTORE_SILENTLY')) {
1581 echo ".";
1582 if ($counter % 400 == 0) {
1583 echo "<br />";
1586 backup_flush(300);
1591 $counteritems++; // increment item count
1598 if (!defined('RESTORE_SILENTLY')) {
1599 //End ul
1600 echo '</ul>';
1602 return $status;
1605 //This function creates all the user, user_students, user_teachers
1606 //user_course_creators and user_admins from xml
1607 function restore_create_users($restore,$xml_file) {
1609 global $CFG, $db;
1611 $status = true;
1612 //Check it exists
1613 if (!file_exists($xml_file)) {
1614 $status = false;
1616 //Get info from xml
1617 if ($status) {
1618 //info will contain the old_id of every user
1619 //in backup_ids->info will be the real info (serialized)
1620 $info = restore_read_xml_users($restore,$xml_file);
1623 //Now, get evey user_id from $info and user data from $backup_ids
1624 //and create the necessary records (users, user_students, user_teachers
1625 //user_course_creators and user_admins
1626 if (!empty($info->users)) {
1627 // Grab mnethosts keyed by wwwroot, to map to id
1628 $mnethosts = get_records('mnet_host', '', '',
1629 'wwwroot', 'wwwroot, id');
1631 $languages = get_list_of_languages();
1633 foreach ($info->users as $userid) {
1634 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
1635 $user = $rec->info;
1637 //Now, recode some languages (Moodle 1.5)
1638 if ($user->lang == 'ma_nt') {
1639 $user->lang = 'mi_nt';
1643 //If language does not exist here - use site default
1644 if (!array_key_exists($user->lang, $languages)) {
1645 $user->lang = $CFG->lang;
1648 //Check if it's admin and coursecreator
1649 $is_admin = !empty($user->roles['admin']);
1650 $is_coursecreator = !empty($user->roles['coursecreator']);
1652 //Check if it's teacher and student
1653 $is_teacher = !empty($user->roles['teacher']);
1654 $is_student = !empty($user->roles['student']);
1656 //Check if it's needed
1657 $is_needed = !empty($user->roles['needed']);
1659 //Calculate if it is a course user
1660 //Has role teacher or student or needed
1661 $is_course_user = ($is_teacher or $is_student or $is_needed);
1663 //Calculate mnethostid
1664 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
1665 $user->mnethostid = $CFG->mnet_localhost_id;
1666 } else {
1667 // fast url-to-id lookups
1668 if (isset($mnethosts[$user->mnethosturl])) {
1669 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
1670 } else {
1671 // should not happen, as we check in restore_chech.php
1672 // but handle the error if it does
1673 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
1676 unset($user->mnethosturl);
1678 //To store new ids created
1679 $newid=null;
1680 //check if it exists (by username) and get its id
1681 $user_exists = true;
1682 $user_data = get_record("user","username",addslashes($user->username),
1683 'mnethostid', $user->mnethostid);
1684 if (!$user_data) {
1685 $user_exists = false;
1686 } else {
1687 $newid = $user_data->id;
1689 //Flags to see if we have to create the user, roles and preferences
1690 $create_user = true;
1691 $create_roles = true;
1692 $create_preferences = true;
1694 //If we are restoring course users and it isn't a course user
1695 if ($restore->users == 1 and !$is_course_user) {
1696 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
1697 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
1698 $create_user = false;
1699 $create_roles = false;
1700 $create_preferences = false;
1703 if ($user_exists and $create_user) {
1704 //If user exists mark its newid in backup_ids (the same than old)
1705 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
1706 $create_user = false;
1709 //Here, if create_user, do it
1710 if ($create_user) {
1711 //Unset the id because it's going to be inserted with a new one
1712 unset ($user->id);
1713 //We addslashes to necessary fields
1714 $user->username = addslashes($user->username);
1715 $user->firstname = addslashes($user->firstname);
1716 $user->lastname = addslashes($user->lastname);
1717 $user->email = addslashes($user->email);
1718 $user->institution = addslashes($user->institution);
1719 $user->department = addslashes($user->department);
1720 $user->address = addslashes($user->address);
1721 $user->city = addslashes($user->city);
1722 $user->url = addslashes($user->url);
1723 $user->description = restore_decode_absolute_links(addslashes($user->description));
1725 //We need to analyse the AUTH field to recode it:
1726 // - if the field isn't set, we are in a pre 1.4 backup and we'll
1727 // use manual
1729 if (empty($user->auth)) {
1730 if ($CFG->registerauth == 'email') {
1731 $user->auth = 'email';
1732 } else {
1733 $user->auth = 'manual';
1737 //We need to process the POLICYAGREED field to recalculate it:
1738 // - if the destination site is different (by wwwroot) reset it.
1739 // - if the destination site is the same (by wwwroot), leave it unmodified
1741 if ($restore->original_wwwroot != $CFG->wwwroot) {
1742 $user->policyagreed = 0;
1743 } else {
1744 //Nothing to do, we are in the same server
1747 //Check if the theme exists in destination server
1748 $themes = get_list_of_themes();
1749 if (!in_array($user->theme, $themes)) {
1750 $user->theme = '';
1753 //We are going to create the user
1754 //The structure is exactly as we need
1755 $newid = insert_record ("user",$user);
1756 //Put the new id
1757 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
1760 //Here, if create_roles, do it as necessary
1761 if ($create_roles) {
1762 //Get the newid and current info from backup_ids
1763 $data = backup_getid($restore->backup_unique_code,"user",$userid);
1764 $newid = $data->new_id;
1765 $currinfo = $data->info.",";
1767 //Now, depending of the role, create records in user_studentes and user_teacher
1768 //and/or mark it in backup_ids
1770 if ($is_admin) {
1771 //If the record (user_admins) doesn't exists
1772 //Only put status in backup_ids
1773 $currinfo = $currinfo."admin,";
1774 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1776 if ($is_coursecreator) {
1777 //If the record (user_coursecreators) doesn't exists
1778 //Only put status in backup_ids
1779 $currinfo = $currinfo."coursecreator,";
1780 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1782 if ($is_needed) {
1783 //Only put status in backup_ids
1784 $currinfo = $currinfo."needed,";
1785 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1787 if ($is_teacher) {
1788 //If the record (teacher) doesn't exists
1789 //Put status in backup_ids
1790 $currinfo = $currinfo."teacher,";
1791 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1792 //Set course and user
1793 $user->roles['teacher']->course = $restore->course_id;
1794 $user->roles['teacher']->userid = $newid;
1796 //Need to analyse the enrol field
1797 // - if it isn't set, set it to $CFG->enrol
1798 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
1799 // - if we are in the same server (by wwwroot), maintain it unmodified.
1800 if (empty($user->roles['teacher']->enrol)) {
1801 $user->roles['teacher']->enrol = $CFG->enrol;
1802 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
1803 $user->roles['teacher']->enrol = $CFG->enrol;
1804 } else {
1805 //Nothing to do. Leave it unmodified
1808 $rolesmapping = $restore->rolesmapping;
1809 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1810 if ($user->roles['teacher']->editall) {
1811 role_assign($rolesmapping['defaultteacheredit'],
1812 $newid,
1814 $context->id,
1815 $user->roles['teacher']->timestart,
1816 $user->roles['teacher']->timeend,
1818 $user->roles['teacher']->enrol);
1820 // editting teacher
1821 } else {
1822 // non editting teacher
1823 role_assign($rolesmapping['defaultteacher'],
1824 $newid,
1826 $context->id,
1827 $user->roles['teacher']->timestart,
1828 $user->roles['teacher']->timeend,
1830 $user->roles['teacher']->enrol);
1833 if ($is_student) {
1835 //Put status in backup_ids
1836 $currinfo = $currinfo."student,";
1837 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1838 //Set course and user
1839 $user->roles['student']->course = $restore->course_id;
1840 $user->roles['student']->userid = $newid;
1842 //Need to analyse the enrol field
1843 // - if it isn't set, set it to $CFG->enrol
1844 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
1845 // - if we are in the same server (by wwwroot), maintain it unmodified.
1846 if (empty($user->roles['student']->enrol)) {
1847 $user->roles['student']->enrol = $CFG->enrol;
1848 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
1849 $user->roles['student']->enrol = $CFG->enrol;
1850 } else {
1851 //Nothing to do. Leave it unmodified
1853 $rolesmapping = $restore->rolesmapping;
1854 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1856 role_assign($rolesmapping['defaultstudent'],
1857 $newid,
1859 $context->id,
1860 $user->roles['student']->timestart,
1861 $user->roles['student']->timeend,
1863 $user->roles['student']->enrol);
1866 if (!$is_course_user) {
1867 //If the record (user) doesn't exists
1868 if (!record_exists("user","id",$newid)) {
1869 //Put status in backup_ids
1870 $currinfo = $currinfo."user,";
1871 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1876 //Here, if create_preferences, do it as necessary
1877 if ($create_preferences) {
1878 //echo "Checking for preferences of user ".$user->username."<br />"; //Debug
1879 //Get user new id from backup_ids
1880 $data = backup_getid($restore->backup_unique_code,"user",$userid);
1881 $newid = $data->new_id;
1882 if (isset($user->user_preferences)) {
1883 //echo "Preferences exist in backup file<br />"; //Debug
1884 foreach($user->user_preferences as $user_preference) {
1885 //echo $user_preference->name." = ".$user_preference->value."<br />"; //Debug
1886 //We check if that user_preference exists in DB
1887 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
1888 //echo "Creating it<br />"; //Debug
1889 //Prepare the record and insert it
1890 $user_preference->userid = $newid;
1891 $status = insert_record("user_preferences",$user_preference);
1899 return $status;
1902 //This function creates all the structures messages and contacts
1903 function restore_create_messages($restore,$xml_file) {
1905 global $CFG;
1907 $status = true;
1908 //Check it exists
1909 if (!file_exists($xml_file)) {
1910 $status = false;
1912 //Get info from xml
1913 if ($status) {
1914 //info will contain the id and name of every table
1915 //(message, message_read and message_contacts)
1916 //in backup_ids->info will be the real info (serialized)
1917 $info = restore_read_xml_messages($restore,$xml_file);
1919 //If we have info, then process messages & contacts
1920 if ($info > 0) {
1921 //Count how many we have
1922 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
1923 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
1924 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
1925 if ($unreadcount || $readcount || $contactcount) {
1926 //Start ul
1927 if (!defined('RESTORE_SILENTLY')) {
1928 echo '<ul>';
1930 //Number of records to get in every chunk
1931 $recordset_size = 4;
1933 //Process unread
1934 if ($unreadcount) {
1935 if (!defined('RESTORE_SILENTLY')) {
1936 echo '<li>'.get_string('unreadmessages','message').'</li>';
1938 $counter = 0;
1939 while ($counter < $unreadcount) {
1940 //Fetch recordset_size records in each iteration
1941 $recs = get_records_select("backup_ids","table_name = 'message' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
1942 if ($recs) {
1943 foreach ($recs as $rec) {
1944 //Get the full record from backup_ids
1945 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
1946 if ($data) {
1947 //Now get completed xmlized object
1948 $info = $data->info;
1949 //traverse_xmlize($info); //Debug
1950 //print_object ($GLOBALS['traverse_array']); //Debug
1951 //$GLOBALS['traverse_array']=""; //Debug
1952 //Now build the MESSAGE record structure
1953 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
1954 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
1955 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
1956 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
1957 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
1958 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
1959 //We have to recode the useridfrom field
1960 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
1961 if ($user) {
1962 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
1963 $dbrec->useridfrom = $user->new_id;
1965 //We have to recode the useridto field
1966 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
1967 if ($user) {
1968 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
1969 $dbrec->useridto = $user->new_id;
1971 //Check if the record doesn't exist in DB!
1972 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
1973 'useridto', $dbrec->useridto,
1974 'timecreated',$dbrec->timecreated);
1975 if (!$exist) {
1976 //Not exist. Insert
1977 $status = insert_record('message',$dbrec);
1978 } else {
1979 //Duplicate. Do nothing
1982 //Do some output
1983 $counter++;
1984 if ($counter % 10 == 0) {
1985 if (!defined('RESTORE_SILENTLY')) {
1986 echo ".";
1987 if ($counter % 200 == 0) {
1988 echo "<br />";
1991 backup_flush(300);
1998 //Process read
1999 if ($readcount) {
2000 if (!defined('RESTORE_SILENTLY')) {
2001 echo '<li>'.get_string('readmessages','message').'</li>';
2003 $counter = 0;
2004 while ($counter < $readcount) {
2005 //Fetch recordset_size records in each iteration
2006 $recs = get_records_select("backup_ids","table_name = 'message_read' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2007 if ($recs) {
2008 foreach ($recs as $rec) {
2009 //Get the full record from backup_ids
2010 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
2011 if ($data) {
2012 //Now get completed xmlized object
2013 $info = $data->info;
2014 //traverse_xmlize($info); //Debug
2015 //print_object ($GLOBALS['traverse_array']); //Debug
2016 //$GLOBALS['traverse_array']=""; //Debug
2017 //Now build the MESSAGE_READ record structure
2018 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2019 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2020 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2021 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2022 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2023 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2024 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2025 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2026 //We have to recode the useridfrom field
2027 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2028 if ($user) {
2029 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2030 $dbrec->useridfrom = $user->new_id;
2032 //We have to recode the useridto field
2033 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2034 if ($user) {
2035 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2036 $dbrec->useridto = $user->new_id;
2038 //Check if the record doesn't exist in DB!
2039 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
2040 'useridto', $dbrec->useridto,
2041 'timecreated',$dbrec->timecreated);
2042 if (!$exist) {
2043 //Not exist. Insert
2044 $status = insert_record('message_read',$dbrec);
2045 } else {
2046 //Duplicate. Do nothing
2049 //Do some output
2050 $counter++;
2051 if ($counter % 10 == 0) {
2052 if (!defined('RESTORE_SILENTLY')) {
2053 echo ".";
2054 if ($counter % 200 == 0) {
2055 echo "<br />";
2058 backup_flush(300);
2065 //Process contacts
2066 if ($contactcount) {
2067 if (!defined('RESTORE_SILENTLY')) {
2068 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2070 $counter = 0;
2071 while ($counter < $contactcount) {
2072 //Fetch recordset_size records in each iteration
2073 $recs = get_records_select("backup_ids","table_name = 'message_contacts' AND backup_code = '$restore->backup_unique_code'","old_id","old_id, old_id",$counter,$recordset_size);
2074 if ($recs) {
2075 foreach ($recs as $rec) {
2076 //Get the full record from backup_ids
2077 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
2078 if ($data) {
2079 //Now get completed xmlized object
2080 $info = $data->info;
2081 //traverse_xmlize($info); //Debug
2082 //print_object ($GLOBALS['traverse_array']); //Debug
2083 //$GLOBALS['traverse_array']=""; //Debug
2084 //Now build the MESSAGE_CONTACTS record structure
2085 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2086 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2087 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2088 //We have to recode the userid field
2089 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
2090 if ($user) {
2091 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2092 $dbrec->userid = $user->new_id;
2094 //We have to recode the contactid field
2095 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
2096 if ($user) {
2097 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2098 $dbrec->contactid = $user->new_id;
2100 //Check if the record doesn't exist in DB!
2101 $exist = get_record('message_contacts','userid',$dbrec->userid,
2102 'contactid', $dbrec->contactid);
2103 if (!$exist) {
2104 //Not exist. Insert
2105 $status = insert_record('message_contacts',$dbrec);
2106 } else {
2107 //Duplicate. Do nothing
2110 //Do some output
2111 $counter++;
2112 if ($counter % 10 == 0) {
2113 if (!defined('RESTORE_SILENTLY')) {
2114 echo ".";
2115 if ($counter % 200 == 0) {
2116 echo "<br />";
2119 backup_flush(300);
2125 if (!defined('RESTORE_SILENTLY')) {
2126 //End ul
2127 echo '</ul>';
2133 return $status;
2136 //This function creates all the categories and questions
2137 //from xml
2138 function restore_create_questions($restore,$xml_file) {
2140 global $CFG, $db;
2142 $status = true;
2143 //Check it exists
2144 if (!file_exists($xml_file)) {
2145 $status = false;
2147 //Get info from xml
2148 if ($status) {
2149 //info will contain the old_id of every category
2150 //in backup_ids->info will be the real info (serialized)
2151 $info = restore_read_xml_questions($restore,$xml_file);
2153 //Now, if we have anything in info, we have to restore that
2154 //categories/questions
2155 if ($info) {
2156 if ($info !== true) {
2157 //Iterate over each category
2158 foreach ($info as $category) {
2159 //Skip empty categories (some backups can contain them)
2160 if (!empty($category->id)) {
2161 $status = restore_question_categories($category,$restore);
2165 //Now we have to recode the parent field of each restored category
2166 $categories = get_records_sql("SELECT old_id, new_id
2167 FROM {$CFG->prefix}backup_ids
2168 WHERE backup_code = $restore->backup_unique_code AND
2169 table_name = 'question_categories'");
2170 if ($categories) {
2171 foreach ($categories as $category) {
2172 $restoredcategory = get_record('question_categories','id',$category->new_id);
2173 $restoredcategory = addslashes_object($restoredcategory);
2174 if ($restoredcategory->parent != 0) {
2175 $idcat = backup_getid($restore->backup_unique_code,'question_categories',$restoredcategory->parent);
2176 if ($idcat->new_id) {
2177 $restoredcategory->parent = $idcat->new_id;
2178 } else {
2179 $restoredcategory->parent = 0;
2181 update_record('question_categories', $restoredcategory);
2186 } else {
2187 $status = false;
2189 return $status;
2192 //This function creates all the scales
2193 function restore_create_scales($restore,$xml_file) {
2195 global $CFG, $db;
2197 $status = true;
2198 //Check it exists
2199 if (!file_exists($xml_file)) {
2200 $status = false;
2202 //Get info from xml
2203 if ($status) {
2204 //scales will contain the old_id of every scale
2205 //in backup_ids->info will be the real info (serialized)
2206 $scales = restore_read_xml_scales($restore,$xml_file);
2208 //Now, if we have anything in scales, we have to restore that
2209 //scales
2210 if ($scales) {
2211 //Get admin->id for later use
2212 $admin = get_admin();
2213 $adminid = $admin->id;
2214 if ($scales !== true) {
2215 //Iterate over each scale
2216 foreach ($scales as $scale) {
2217 //Get record from backup_ids
2218 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
2219 //Init variables
2220 $create_scale = false;
2222 if ($data) {
2223 //Now get completed xmlized object
2224 $info = $data->info;
2225 //traverse_xmlize($info); //Debug
2226 //print_object ($GLOBALS['traverse_array']); //Debug
2227 //$GLOBALS['traverse_array']=""; //Debug
2229 //Now build the SCALE record structure
2230 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
2231 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
2232 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
2233 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
2234 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
2235 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
2237 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
2238 //or in restore->course_id course (Personal scale)
2239 if ($sca->courseid == 0) {
2240 $course_to_search = 0;
2241 } else {
2242 $course_to_search = $restore->course_id;
2244 $sca_db = get_record("scale","scale",$sca->scale,"courseid",$course_to_search);
2245 //If it doesn't exist, create
2246 if (!$sca_db) {
2247 $create_scale = true;
2249 //If we must create the scale
2250 if ($create_scale) {
2251 //Me must recode the courseid if it's <> 0 (common scale)
2252 if ($sca->courseid != 0) {
2253 $sca->courseid = $restore->course_id;
2255 //We must recode the userid
2256 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
2257 if ($user) {
2258 $sca->userid = $user->new_id;
2259 } else {
2260 //Assign it to admin
2261 $sca->userid = $adminid;
2263 //The structure is equal to the db, so insert the scale
2264 $newid = insert_record ("scale",$sca);
2265 } else {
2266 //get current scale id
2267 $newid = $sca_db->id;
2269 if ($newid) {
2270 //We have the newid, update backup_ids
2271 backup_putid($restore->backup_unique_code,"scale",
2272 $scale->id, $newid);
2277 } else {
2278 $status = false;
2280 return $status;
2283 //This function creates all the groups
2284 function restore_create_groups($restore,$xml_file) {
2286 global $CFG, $db;
2288 $status = true;
2289 $status2 = true;
2290 //Check it exists
2291 if (!file_exists($xml_file)) {
2292 $status = false;
2294 //Get info from xml
2295 if ($status) {
2296 //groups will contain the old_id of every group
2297 //in backup_ids->info will be the real info (serialized)
2298 $groups = restore_read_xml_groups($restore,$xml_file);
2300 //Now, if we have anything in groups, we have to restore that
2301 //groups
2302 if ($groups) {
2303 if ($groups !== true) {
2304 //Iterate over each group
2305 foreach ($groups as $group) {
2306 //Get record from backup_ids
2307 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
2308 //Init variables
2309 $create_group = false;
2311 if ($data) {
2312 //Now get completed xmlized object
2313 $info = $data->info;
2314 //traverse_xmlize($info); //Debug
2315 //print_object ($GLOBALS['traverse_array']); //Debug
2316 //$GLOBALS['traverse_array']=""; //Debug
2317 //Now build the GROUP record structure
2318 $gro = new Object();
2319 ///$gro->courseid = backup_todb($info['GROUP']['#']['COURSEID']['0']['#']);
2320 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
2321 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
2322 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
2323 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
2324 } else { //if (! isset($gro->enrolment)) {
2325 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
2327 $gro->lang = backup_todb($info['GROUP']['#']['LANG']['0']['#']);
2328 $gro->theme = backup_todb($info['GROUP']['#']['THEME']['0']['#']);
2329 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
2330 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
2331 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
2332 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
2334 //Now search if that group exists (by name and description field) in
2335 //restore->course_id course
2336 $gro_db = groups_group_matches($restore->course_id, $gro->name, $gro->description);
2337 //If it doesn't exist, create
2338 if (!$gro_db) {
2339 $create_group = true;
2341 //If we must create the group
2342 if ($create_group) {
2343 //Me must recode the courseid to the restore->course_id
2344 $gro->courseid = $restore->course_id;
2346 //Check if the theme exists in destination server
2347 $themes = get_list_of_themes();
2348 if (!in_array($gro->theme, $themes)) {
2349 $gro->theme = '';
2352 //The structure is equal to the db, so insert the group
2353 $newid = groups_restore_group($restore->course_id, $gro);
2354 } else {
2355 //get current group id
2356 $newid = $gro_db->id;
2358 if ($newid) {
2359 //We have the newid, update backup_ids
2360 backup_putid($restore->backup_unique_code,"groups",
2361 $group->id, $newid);
2363 //Now restore members in the groups_members, only if
2364 //users are included
2365 if ($restore->users != 2) {
2366 $status2 = restore_create_groups_members($newid,$info,$restore);
2370 //Now, restore group_files
2371 if ($status && $status2) {
2372 $status2 = restore_group_files($restore);
2375 } else {
2376 $status = false;
2378 return ($status && $status2);
2381 //This function restores the groups_members
2382 function restore_create_groups_members($group_id,$info,$restore) {
2384 global $CFG;
2386 $status = true;
2388 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
2389 //OK, some groups have no members.
2390 return $status;
2392 //Get the members array
2393 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
2395 //Iterate over members
2396 for($i = 0; $i < sizeof($members); $i++) {
2397 $mem_info = $members[$i];
2398 //traverse_xmlize($mem_info); //Debug
2399 //print_object ($GLOBALS['traverse_array']); //Debug
2400 //$GLOBALS['traverse_array']=""; //Debug
2402 //Now, build the GROUPS_MEMBERS record structure
2403 $group_member = new Object();
2404 $group_member->groupid = $group_id;
2405 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
2406 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2408 //We have to recode the userid field
2409 $user = backup_getid($restore->backup_unique_code,"user",$group_member->userid);
2410 if ($user) {
2411 $group_member->userid = $user->new_id;
2414 //The structure is equal to the db, so insert the groups_members
2415 $newid = groups_restore_member($group_member);
2416 //Do some output
2417 if (($i+1) % 50 == 0) {
2418 if (!defined('RESTORE_SILENTLY')) {
2419 echo ".";
2420 if (($i+1) % 1000 == 0) {
2421 echo "<br />";
2424 backup_flush(300);
2427 if (!$newid) {
2428 $status = false;
2432 return $status;
2435 //This function creates all the groupings
2436 function restore_create_groupings($restore,$xml_file) {
2438 global $CFG, $db;
2440 $status = true;
2441 $status2 = true;
2442 //Check it exists
2443 if (!file_exists($xml_file)) {
2444 $status = false;
2446 //Get info from xml
2447 if ($status) {
2448 //groupings will contain the old_id of every group
2449 //in backup_ids->info will be the real info (serialized)
2450 $groupings = restore_read_xml_groupings($restore,$xml_file);
2452 //Now, if we have anything in groupings, we have to restore that grouping
2453 if ($groupings) {
2454 if ($groupings !== true) {
2455 //Iterate over each group
2456 foreach ($groupings as $grouping) {
2457 //Get record from backup_ids
2458 $data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id);
2459 //Init variables
2460 $create_grouping = false;
2462 if ($data) {
2463 //Now get completed xmlized object
2464 $info = $data->info;
2465 //Now build the GROUPING record structure
2466 $gro = new Object();
2467 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
2468 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
2469 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
2470 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
2472 //Now search if that group exists (by name and description field) in
2473 //restore->course_id course
2474 $gro_db = groups_grouping_matches($restore->course_id, $gro->name, $gro->description);
2475 //If it doesn't exist, create
2476 if (!$gro_db) {
2477 $create_grouping = true;
2479 //If we must create the group
2480 if ($create_grouping) {
2482 //The structure is equal to the db, so insert the grouping TODO: RESTORE.
2483 $newid = groups_create_grouping($restore->course_id, $gro);
2484 } else {
2485 //get current group id
2486 $newid = $gro_db->id;
2488 if ($newid) {
2489 //We have the newid, update backup_ids
2490 backup_putid($restore->backup_unique_code,"groupings",
2491 $grouping->id, $newid);
2493 //Now restore links from groupings to groups
2494 $status2 = restore_create_groupings_groups($newid,$info,$restore);
2497 //(Now, restore grouping_files)
2499 } else {
2500 $status = false;
2502 return ($status && $status2);
2505 //This function restores the groups_members
2506 function restore_create_groupings_groups($grouping_id,$info,$restore) {
2508 global $CFG;
2510 $status = true;
2512 //Get the members array
2513 $members = $info['GROUPING']['#']['GROUPS']['0']['#']['GROUP'];
2515 //Iterate over members
2516 for($i = 0; $i < sizeof($members); $i++) {
2517 $mem_info = $members[$i];
2518 //Now, build the GROUPINGS_GROUPS record structure
2519 $gro_member = new Object();
2520 $gro_member->groupingid = $grouping_id;
2521 $gro_member->groupid = backup_todb($mem_info['#']['GROUPID']['0']['#']);
2522 $gro_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2524 //We have to recode the userid field
2525 ///$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid);
2526 $group = backup_getid($restore->backup_unique_code,"group",$gro_member->groupid);
2527 if ($group) {
2528 $gro_member->groupid = $group->new_id;
2531 //The structure is equal to the db, so link the groups to the groupings. TODO: RESTORE.
2532 $newid = groups_add_group_to_grouping($gro_member->groupid, $gro_member->groupingid);
2533 //Do some output
2534 if (($i+1) % 50 == 0) {
2535 if (!defined('RESTORE_SILENTLY')) {
2536 echo ".";
2537 if (($i+1) % 1000 == 0) {
2538 echo "<br />";
2541 backup_flush(300);
2544 if (!$newid) {
2545 $status = false;
2549 return $status;
2552 //This function creates all the course events
2553 function restore_create_events($restore,$xml_file) {
2555 global $CFG, $db;
2557 $status = true;
2558 //Check it exists
2559 if (!file_exists($xml_file)) {
2560 $status = false;
2562 //Get info from xml
2563 if ($status) {
2564 //events will contain the old_id of every event
2565 //in backup_ids->info will be the real info (serialized)
2566 $events = restore_read_xml_events($restore,$xml_file);
2569 //Get admin->id for later use
2570 $admin = get_admin();
2571 $adminid = $admin->id;
2573 //Now, if we have anything in events, we have to restore that
2574 //events
2575 if ($events) {
2576 if ($events !== true) {
2577 //Iterate over each event
2578 foreach ($events as $event) {
2579 //Get record from backup_ids
2580 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
2581 //Init variables
2582 $create_event = false;
2584 if ($data) {
2585 //Now get completed xmlized object
2586 $info = $data->info;
2587 //traverse_xmlize($info); //Debug
2588 //print_object ($GLOBALS['traverse_array']); //Debug
2589 //$GLOBALS['traverse_array']=""; //Debug
2591 //if necessary, write to restorelog and adjust date/time fields
2592 if ($restore->course_startdateoffset) {
2593 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
2596 //Now build the EVENT record structure
2597 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
2598 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
2599 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
2600 $eve->courseid = $restore->course_id;
2601 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
2602 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
2603 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
2604 $eve->modulename = "";
2605 if (!empty($info['EVENT']['#']['MODULENAME'])) {
2606 $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
2608 $eve->instance = 0;
2609 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
2610 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
2611 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
2612 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
2613 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
2615 //Now search if that event exists (by name, description, timestart fields) in
2616 //restore->course_id course
2617 $eve_db = get_record_select("event",
2618 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
2619 //If it doesn't exist, create
2620 if (!$eve_db) {
2621 $create_event = true;
2623 //If we must create the event
2624 if ($create_event) {
2626 //We must recode the userid
2627 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
2628 if ($user) {
2629 $eve->userid = $user->new_id;
2630 } else {
2631 //Assign it to admin
2632 $eve->userid = $adminid;
2635 //We must recode the repeatid if the event has it
2636 if (!empty($eve->repeatid)) {
2637 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
2638 if ($repeat_rec) { //Exists, so use it...
2639 $eve->repeatid = $repeat_rec->new_id;
2640 } else { //Doesn't exists, calculate the next and save it
2641 $oldrepeatid = $eve->repeatid;
2642 $max_rec = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event');
2643 $eve->repeatid = empty($max_rec) ? 1 : $max_rec->repeatid + 1;
2644 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
2648 //We have to recode the groupid field
2649 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
2650 if ($group) {
2651 $eve->groupid = $group->new_id;
2652 } else {
2653 //Assign it to group 0
2654 $eve->groupid = 0;
2657 //The structure is equal to the db, so insert the event
2658 $newid = insert_record ("event",$eve);
2659 } else {
2660 //get current event id
2661 $newid = $eve_db->id;
2663 if ($newid) {
2664 //We have the newid, update backup_ids
2665 backup_putid($restore->backup_unique_code,"event",
2666 $event->id, $newid);
2671 } else {
2672 $status = false;
2674 return $status;
2677 //This function decode things to make restore multi-site fully functional
2678 //It does this conversions:
2679 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
2680 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
2682 //Note: Inter-activities linking is being implemented as a final
2683 //step in the restore execution, because we need to have it
2684 //finished to know all the oldid, newid equivaleces
2685 function restore_decode_absolute_links($content) {
2687 global $CFG,$restore;
2689 //Now decode wwwroot and file.php calls
2690 $search = array ("$@FILEPHP@$");
2692 //Check for the status of the slasharguments config variable
2693 $slash = $CFG->slasharguments;
2695 //Build the replace string as needed
2696 if ($slash == 1) {
2697 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
2698 } else {
2699 $replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
2702 $result = str_replace($search,$replace,$content);
2704 if ($result != $content && debugging()) { //Debug
2705 if (!defined('RESTORE_SILENTLY')) {
2706 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
2708 } //Debug
2710 return $result;
2713 //This function restores the userfiles from the temp (user_files) directory to the
2714 //dataroot/users directory
2715 function restore_user_files($restore) {
2717 global $CFG;
2719 $status = true;
2721 $counter = 0;
2723 //First, we check to "users" exists and create is as necessary
2724 //in CFG->dataroot
2725 $dest_dir = $CFG->dataroot."/users";
2726 $status = check_dir_exists($dest_dir,true);
2728 //Now, we iterate over "user_files" records to check if that user dir must be
2729 //copied (and renamed) to the "users" dir.
2730 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
2731 //Check if directory exists
2732 if (is_dir($rootdir)) {
2733 $list = list_directories ($rootdir);
2734 if ($list) {
2735 //Iterate
2736 $counter = 0;
2737 foreach ($list as $dir) {
2738 //Look for dir like username in backup_ids
2739 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
2740 "table_name","user",
2741 "old_id",$dir);
2742 //If thar user exists in backup_ids
2743 if ($data) {
2744 //Only it user has been created now
2745 //or if it existed previously, but he hasn't image (see bug 1123)
2746 if ((strpos($data->info,"new") !== false) or
2747 (!check_dir_exists($dest_dir."/".$data->new_id,false))) {
2748 //Copy the old_dir to its new location (and name) !!
2749 //Only if destination doesn't exists
2750 if (!file_exists($dest_dir."/".$data->new_id)) {
2751 $status = backup_copy_file($rootdir."/".$dir,
2752 $dest_dir."/".$data->new_id,true);
2753 $counter ++;
2755 //Do some output
2756 if ($counter % 2 == 0) {
2757 if (!defined('RESTORE_SILENTLY')) {
2758 echo ".";
2759 if ($counter % 40 == 0) {
2760 echo "<br />";
2763 backup_flush(300);
2770 //If status is ok and whe have dirs created, returns counter to inform
2771 if ($status and $counter) {
2772 return $counter;
2773 } else {
2774 return $status;
2778 //This function restores the groupfiles from the temp (group_files) directory to the
2779 //dataroot/groups directory
2780 function restore_group_files($restore) {
2782 global $CFG;
2784 $status = true;
2786 $counter = 0;
2788 //First, we check to "groups" exists and create is as necessary
2789 //in CFG->dataroot
2790 $dest_dir = $CFG->dataroot.'/groups';
2791 $status = check_dir_exists($dest_dir,true);
2793 //Now, we iterate over "group_files" records to check if that user dir must be
2794 //copied (and renamed) to the "groups" dir.
2795 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
2796 //Check if directory exists
2797 if (is_dir($rootdir)) {
2798 $list = list_directories ($rootdir);
2799 if ($list) {
2800 //Iterate
2801 $counter = 0;
2802 foreach ($list as $dir) {
2803 //Look for dir like groupid in backup_ids
2804 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
2805 "table_name","groups",
2806 "old_id",$dir);
2807 //If that group exists in backup_ids
2808 if ($data) {
2809 if (!file_exists($dest_dir."/".$data->new_id)) {
2810 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
2811 $counter ++;
2813 //Do some output
2814 if ($counter % 2 == 0) {
2815 if (!defined('RESTORE_SILENTLY')) {
2816 echo ".";
2817 if ($counter % 40 == 0) {
2818 echo "<br />";
2821 backup_flush(300);
2827 //If status is ok and whe have dirs created, returns counter to inform
2828 if ($status and $counter) {
2829 return $counter;
2830 } else {
2831 return $status;
2835 //This function restores the course files from the temp (course_files) directory to the
2836 //dataroot/course_id directory
2837 function restore_course_files($restore) {
2839 global $CFG;
2841 $status = true;
2843 $counter = 0;
2845 //First, we check to "course_id" exists and create is as necessary
2846 //in CFG->dataroot
2847 $dest_dir = $CFG->dataroot."/".$restore->course_id;
2848 $status = check_dir_exists($dest_dir,true);
2850 //Now, we iterate over "course_files" records to check if that file/dir must be
2851 //copied to the "dest_dir" dir.
2852 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
2853 //Check if directory exists
2854 if (is_dir($rootdir)) {
2855 $list = list_directories_and_files ($rootdir);
2856 if ($list) {
2857 //Iterate
2858 $counter = 0;
2859 foreach ($list as $dir) {
2860 //Copy the dir to its new location
2861 //Only if destination file/dir doesn exists
2862 if (!file_exists($dest_dir."/".$dir)) {
2863 $status = backup_copy_file($rootdir."/".$dir,
2864 $dest_dir."/".$dir,true);
2865 $counter ++;
2867 //Do some output
2868 if ($counter % 2 == 0) {
2869 if (!defined('RESTORE_SILENTLY')) {
2870 echo ".";
2871 if ($counter % 40 == 0) {
2872 echo "<br />";
2875 backup_flush(300);
2880 //If status is ok and whe have dirs created, returns counter to inform
2881 if ($status and $counter) {
2882 return $counter;
2883 } else {
2884 return $status;
2889 //This function creates all the structures for every module in backup file
2890 //Depending what has been selected.
2891 function restore_create_modules($restore,$xml_file) {
2893 global $CFG;
2894 $status = true;
2895 //Check it exists
2896 if (!file_exists($xml_file)) {
2897 $status = false;
2899 //Get info from xml
2900 if ($status) {
2901 //info will contain the id and modtype of every module
2902 //in backup_ids->info will be the real info (serialized)
2903 $info = restore_read_xml_modules($restore,$xml_file);
2905 //Now, if we have anything in info, we have to restore that mods
2906 //from backup_ids (calling every mod restore function)
2907 if ($info) {
2908 if ($info !== true) {
2909 if (!defined('RESTORE_SILENTLY')) {
2910 echo '<ul>';
2912 //Iterate over each module
2913 foreach ($info as $mod) {
2914 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
2915 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
2916 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
2917 $modrestore = $mod->modtype."_restore_mods";
2918 if (function_exists($modrestore)) { //Debug
2919 $status = $status and $modrestore($mod,$restore); //bit operator & not reliable here!
2920 } else {
2921 //Something was wrong. Function should exist.
2922 $status = false;
2926 if (!defined('RESTORE_SILENTLY')) {
2927 echo '</ul>';
2930 } else {
2931 $status = false;
2933 return $status;
2936 //This function creates all the structures for every log in backup file
2937 //Depending what has been selected.
2938 function restore_create_logs($restore,$xml_file) {
2940 global $CFG,$db;
2942 //Number of records to get in every chunk
2943 $recordset_size = 4;
2944 //Counter, points to current record
2945 $counter = 0;
2946 //To count all the recods to restore
2947 $count_logs = 0;
2949 $status = true;
2950 //Check it exists
2951 if (!file_exists($xml_file)) {
2952 $status = false;
2954 //Get info from xml
2955 if ($status) {
2956 //count_logs will contain the number of logs entries to process
2957 //in backup_ids->info will be the real info (serialized)
2958 $count_logs = restore_read_xml_logs($restore,$xml_file);
2961 //Now, if we have records in count_logs, we have to restore that logs
2962 //from backup_ids. This piece of code makes calls to:
2963 // - restore_log_course() if it's a course log
2964 // - restore_log_user() if it's a user log
2965 // - restore_log_module() if it's a module log.
2966 //And all is segmented in chunks to allow large recordsets to be restored !!
2967 if ($count_logs > 0) {
2968 while ($counter < $count_logs) {
2969 //Get a chunk of records
2970 //Take old_id twice to avoid adodb limitation
2971 $logs = get_records_select("backup_ids","table_name = 'log' AND backup_code = '$restore->backup_unique_code'","old_id","old_id,old_id",$counter,$recordset_size);
2972 //We have logs
2973 if ($logs) {
2974 //Iterate
2975 foreach ($logs as $log) {
2976 //Get the full record from backup_ids
2977 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
2978 if ($data) {
2979 //Now get completed xmlized object
2980 $info = $data->info;
2981 //traverse_xmlize($info); //Debug
2982 //print_object ($GLOBALS['traverse_array']); //Debug
2983 //$GLOBALS['traverse_array']=""; //Debug
2984 //Now build the LOG record structure
2985 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
2986 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
2987 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
2988 $dblog->course = $restore->course_id;
2989 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
2990 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
2991 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
2992 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
2993 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
2994 //We have to recode the userid field
2995 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
2996 if ($user) {
2997 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
2998 $dblog->userid = $user->new_id;
3000 //We have to recode the cmid field (if module isn't "course" or "user")
3001 if ($dblog->module != "course" and $dblog->module != "user") {
3002 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
3003 if ($cm) {
3004 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
3005 $dblog->cmid = $cm->new_id;
3006 } else {
3007 $dblog->cmid = 0;
3010 //print_object ($dblog); //Debug
3011 //Now, we redirect to the needed function to make all the work
3012 if ($dblog->module == "course") {
3013 //It's a course log,
3014 $stat = restore_log_course($restore,$dblog);
3015 } elseif ($dblog->module == "user") {
3016 //It's a user log,
3017 $stat = restore_log_user($restore,$dblog);
3018 } else {
3019 //It's a module log,
3020 $stat = restore_log_module($restore,$dblog);
3024 //Do some output
3025 $counter++;
3026 if ($counter % 10 == 0) {
3027 if (!defined('RESTORE_SILENTLY')) {
3028 echo ".";
3029 if ($counter % 200 == 0) {
3030 echo "<br />";
3033 backup_flush(300);
3036 } else {
3037 //We never should arrive here
3038 $counter = $count_logs;
3039 $status = false;
3044 return $status;
3047 //This function inserts a course log record, calculating the URL field as necessary
3048 function restore_log_course($restore,$log) {
3050 $status = true;
3051 $toinsert = false;
3053 //echo "<hr />Before transformations<br />"; //Debug
3054 //print_object($log); //Debug
3055 //Depending of the action, we recode different things
3056 switch ($log->action) {
3057 case "view":
3058 $log->url = "view.php?id=".$log->course;
3059 $log->info = $log->course;
3060 $toinsert = true;
3061 break;
3062 case "guest":
3063 $log->url = "view.php?id=".$log->course;
3064 $toinsert = true;
3065 break;
3066 case "user report":
3067 //recode the info field (it's the user id)
3068 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3069 if ($user) {
3070 $log->info = $user->new_id;
3071 //Now, extract the mode from the url field
3072 $mode = substr(strrchr($log->url,"="),1);
3073 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
3074 $toinsert = true;
3076 break;
3077 case "add mod":
3078 //Extract the course_module from the url field
3079 $cmid = substr(strrchr($log->url,"="),1);
3080 //recode the course_module to see it it has been restored
3081 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3082 if ($cm) {
3083 $cmid = $cm->new_id;
3084 //Extract the module name and the module id from the info field
3085 $modname = strtok($log->info," ");
3086 $modid = strtok(" ");
3087 //recode the module id to see if it has been restored
3088 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3089 if ($mod) {
3090 $modid = $mod->new_id;
3091 //Now I have everything so reconstruct url and info
3092 $log->info = $modname." ".$modid;
3093 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3094 $toinsert = true;
3097 break;
3098 case "update mod":
3099 //Extract the course_module from the url field
3100 $cmid = substr(strrchr($log->url,"="),1);
3101 //recode the course_module to see it it has been restored
3102 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3103 if ($cm) {
3104 $cmid = $cm->new_id;
3105 //Extract the module name and the module id from the info field
3106 $modname = strtok($log->info," ");
3107 $modid = strtok(" ");
3108 //recode the module id to see if it has been restored
3109 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3110 if ($mod) {
3111 $modid = $mod->new_id;
3112 //Now I have everything so reconstruct url and info
3113 $log->info = $modname." ".$modid;
3114 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3115 $toinsert = true;
3118 break;
3119 case "delete mod":
3120 $log->url = "view.php?id=".$log->course;
3121 $toinsert = true;
3122 break;
3123 case "update":
3124 $log->url = "edit.php?id=".$log->course;
3125 $log->info = "";
3126 $toinsert = true;
3127 break;
3128 case "unenrol":
3129 //recode the info field (it's the user id)
3130 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3131 if ($user) {
3132 $log->info = $user->new_id;
3133 $log->url = "view.php?id=".$log->course;
3134 $toinsert = true;
3136 break;
3137 case "enrol":
3138 //recode the info field (it's the user id)
3139 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3140 if ($user) {
3141 $log->info = $user->new_id;
3142 $log->url = "view.php?id=".$log->course;
3143 $toinsert = true;
3145 break;
3146 case "editsection":
3147 //Extract the course_section from the url field
3148 $secid = substr(strrchr($log->url,"="),1);
3149 //recode the course_section to see if it has been restored
3150 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
3151 if ($sec) {
3152 $secid = $sec->new_id;
3153 //Now I have everything so reconstruct url and info
3154 $log->url = "editsection.php?id=".$secid;
3155 $toinsert = true;
3157 break;
3158 case "new":
3159 $log->url = "view.php?id=".$log->course;
3160 $log->info = "";
3161 $toinsert = true;
3162 break;
3163 case "recent":
3164 $log->url = "recent.php?id=".$log->course;
3165 $log->info = "";
3166 $toinsert = true;
3167 break;
3168 case "report log":
3169 $log->url = "report/log/index.php?id=".$log->course;
3170 $log->info = $log->course;
3171 $toinsert = true;
3172 break;
3173 case "report live":
3174 $log->url = "report/log/live.php?id=".$log->course;
3175 $log->info = $log->course;
3176 $toinsert = true;
3177 break;
3178 case "report outline":
3179 $log->url = "report/outline/index.php?id=".$log->course;
3180 $log->info = $log->course;
3181 $toinsert = true;
3182 break;
3183 case "report participation":
3184 $log->url = "report/participation/index.php?id=".$log->course;
3185 $log->info = $log->course;
3186 $toinsert = true;
3187 break;
3188 case "report stats":
3189 $log->url = "report/stats/index.php?id=".$log->course;
3190 $log->info = $log->course;
3191 $toinsert = true;
3192 break;
3193 default:
3194 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3195 break;
3198 //echo "After transformations<br />"; //Debug
3199 //print_object($log); //Debug
3201 //Now if $toinsert is set, insert the record
3202 if ($toinsert) {
3203 //echo "Inserting record<br />"; //Debug
3204 $status = insert_record("log",$log);
3206 return $status;
3209 //This function inserts a user log record, calculating the URL field as necessary
3210 function restore_log_user($restore,$log) {
3212 $status = true;
3213 $toinsert = false;
3215 //echo "<hr />Before transformations<br />"; //Debug
3216 //print_object($log); //Debug
3217 //Depending of the action, we recode different things
3218 switch ($log->action) {
3219 case "view":
3220 //recode the info field (it's the user id)
3221 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3222 if ($user) {
3223 $log->info = $user->new_id;
3224 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3225 $toinsert = true;
3227 break;
3228 case "change password":
3229 //recode the info field (it's the user id)
3230 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3231 if ($user) {
3232 $log->info = $user->new_id;
3233 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3234 $toinsert = true;
3236 break;
3237 case "login":
3238 //recode the info field (it's the user id)
3239 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3240 if ($user) {
3241 $log->info = $user->new_id;
3242 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3243 $toinsert = true;
3245 break;
3246 case "logout":
3247 //recode the info field (it's the user id)
3248 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3249 if ($user) {
3250 $log->info = $user->new_id;
3251 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3252 $toinsert = true;
3254 break;
3255 case "view all":
3256 $log->url = "view.php?id=".$log->course;
3257 $log->info = "";
3258 $toinsert = true;
3259 case "update":
3260 //We split the url by ampersand char
3261 $first_part = strtok($log->url,"&");
3262 //Get data after the = char. It's the user being updated
3263 $userid = substr(strrchr($first_part,"="),1);
3264 //Recode the user
3265 $user = backup_getid($restore->backup_unique_code,"user",$userid);
3266 if ($user) {
3267 $log->info = "";
3268 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
3269 $toinsert = true;
3271 break;
3272 default:
3273 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3274 break;
3277 //echo "After transformations<br />"; //Debug
3278 //print_object($log); //Debug
3280 //Now if $toinsert is set, insert the record
3281 if ($toinsert) {
3282 //echo "Inserting record<br />"; //Debug
3283 $status = insert_record("log",$log);
3285 return $status;
3288 //This function inserts a module log record, calculating the URL field as necessary
3289 function restore_log_module($restore,$log) {
3291 $status = true;
3292 $toinsert = false;
3294 //echo "<hr />Before transformations<br />"; //Debug
3295 //print_object($log); //Debug
3297 //Now we see if the required function in the module exists
3298 $function = $log->module."_restore_logs";
3299 if (function_exists($function)) {
3300 //Call the function
3301 $log = $function($restore,$log);
3302 //If everything is ok, mark the insert flag
3303 if ($log) {
3304 $toinsert = true;
3308 //echo "After transformations<br />"; //Debug
3309 //print_object($log); //Debug
3311 //Now if $toinsert is set, insert the record
3312 if ($toinsert) {
3313 //echo "Inserting record<br />"; //Debug
3314 $status = insert_record("log",$log);
3316 return $status;
3319 //This function adjusts the instance field into course_modules. It's executed after
3320 //modules restore. There, we KNOW the new instance id !!
3321 function restore_check_instances($restore) {
3323 global $CFG;
3325 $status = true;
3327 //We are going to iterate over each course_module saved in backup_ids
3328 $course_modules = get_records_sql("SELECT old_id,new_id
3329 FROM {$CFG->prefix}backup_ids
3330 WHERE backup_code = '$restore->backup_unique_code' AND
3331 table_name = 'course_modules'");
3332 if ($course_modules) {
3333 foreach($course_modules as $cm) {
3334 //Get full record, using backup_getids
3335 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
3336 //Now we are going to the REAL course_modules to get its type (field module)
3337 $module = get_record("course_modules","id",$cm_module->new_id);
3338 if ($module) {
3339 //We know the module type id. Get the name from modules
3340 $type = get_record("modules","id",$module->module);
3341 if ($type) {
3342 //We know the type name and the old_id. Get its new_id
3343 //from backup_ids. It's the instance !!!
3344 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
3345 if ($instance) {
3346 //We have the new instance, so update the record in course_modules
3347 $module->instance = $instance->new_id;
3348 //print_object ($module); //Debug
3349 $status = update_record("course_modules",$module);
3350 } else {
3351 $status = false;
3353 } else {
3354 $status = false;
3356 } else {
3357 $status = false;
3363 return $status;
3366 //=====================================================================================
3367 //== ==
3368 //== XML Functions (SAX) ==
3369 //== ==
3370 //=====================================================================================
3372 //This is the class used to do all the xml parse
3373 class MoodleParser {
3375 var $level = 0; //Level we are
3376 var $counter = 0; //Counter
3377 var $tree = array(); //Array of levels we are
3378 var $content = ""; //Content under current level
3379 var $todo = ""; //What we hav to do when parsing
3380 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
3381 var $temp = ""; //Temp storage.
3382 var $preferences = ""; //Preferences about what to load !!
3383 var $finished = false; //Flag to say xml_parse to stop
3385 //This function is used to get the current contents property value
3386 //They are trimed (and converted from utf8 if needed)
3387 function getContents() {
3388 return trim($this->content);
3391 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3392 function startElementInfo($parser, $tagName, $attrs) {
3393 //Refresh properties
3394 $this->level++;
3395 $this->tree[$this->level] = $tagName;
3397 //Output something to avoid browser timeouts...
3398 backup_flush();
3400 //Check if we are into INFO zone
3401 //if ($this->tree[2] == "INFO") //Debug
3402 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3405 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3406 function startElementRoles($parser, $tagName, $attrs) {
3407 //Refresh properties
3408 $this->level++;
3409 $this->tree[$this->level] = $tagName;
3411 //Output something to avoid browser timeouts...
3412 backup_flush();
3414 //Check if we are into INFO zone
3415 //if ($this->tree[2] == "INFO") //Debug
3416 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3420 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
3421 function startElementCourseHeader($parser, $tagName, $attrs) {
3422 //Refresh properties
3423 $this->level++;
3424 $this->tree[$this->level] = $tagName;
3426 //Output something to avoid browser timeouts...
3427 backup_flush();
3429 //Check if we are into COURSE_HEADER zone
3430 //if ($this->tree[3] == "HEADER") //Debug
3431 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3434 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
3435 function startElementBlocks($parser, $tagName, $attrs) {
3436 //Refresh properties
3437 $this->level++;
3438 $this->tree[$this->level] = $tagName;
3440 //Output something to avoid browser timeouts...
3441 backup_flush();
3443 //Check if we are into BLOCKS zone
3444 //if ($this->tree[3] == "BLOCKS") //Debug
3445 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3448 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
3449 function startElementSections($parser, $tagName, $attrs) {
3450 //Refresh properties
3451 $this->level++;
3452 $this->tree[$this->level] = $tagName;
3454 //Output something to avoid browser timeouts...
3455 backup_flush();
3457 //Check if we are into SECTIONS zone
3458 //if ($this->tree[3] == "SECTIONS") //Debug
3459 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3462 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
3463 function startElementFormatData($parser, $tagName, $attrs) {
3464 //Refresh properties
3465 $this->level++;
3466 $this->tree[$this->level] = $tagName;
3468 //Output something to avoid browser timeouts...
3469 backup_flush();
3471 //Accumulate all the data inside this tag
3472 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
3473 if (!isset($this->temp)) {
3474 $this->temp = '';
3476 $this->temp .= "<".$tagName.">";
3479 //Check if we are into FORMATDATA zone
3480 //if ($this->tree[3] == "FORMATDATA") //Debug
3481 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3484 //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
3485 function startElementMetacourse($parser, $tagName, $attrs) {
3487 //Refresh properties
3488 $this->level++;
3489 $this->tree[$this->level] = $tagName;
3491 //Output something to avoid browser timeouts...
3492 backup_flush();
3494 //Check if we are into METACOURSE zone
3495 //if ($this->tree[3] == "METACOURSE") //Debug
3496 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3499 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
3500 function startElementGradebook($parser, $tagName, $attrs) {
3502 //Refresh properties
3503 $this->level++;
3504 $this->tree[$this->level] = $tagName;
3506 //Output something to avoid browser timeouts...
3507 backup_flush();
3509 //Check if we are into GRADEBOOK zone
3510 //if ($this->tree[3] == "GRADEBOOK") //Debug
3511 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3513 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
3514 if (isset($this->tree[5]) and isset($this->tree[3])) {
3515 if (($this->tree[5] == "GRADE_ITEM" || $this->tree[5] == "GRADE_CATEGORY" || $this->tree[5] == "GRADE_OUTCOME") && ($this->tree[3] == "GRADEBOOK")) {
3516 if (!isset($this->temp)) {
3517 $this->temp = "";
3519 $this->temp .= "<".$tagName.">";
3525 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
3526 function startElementUsers($parser, $tagName, $attrs) {
3527 //Refresh properties
3528 $this->level++;
3529 $this->tree[$this->level] = $tagName;
3531 //Check if we are into USERS zone
3532 //if ($this->tree[3] == "USERS") //Debug
3533 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3536 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
3537 function startElementMessages($parser, $tagName, $attrs) {
3538 //Refresh properties
3539 $this->level++;
3540 $this->tree[$this->level] = $tagName;
3542 //Output something to avoid browser timeouts...
3543 backup_flush();
3545 //Check if we are into MESSAGES zone
3546 //if ($this->tree[3] == "MESSAGES") //Debug
3547 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3549 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
3550 if (isset($this->tree[4]) and isset($this->tree[3])) {
3551 if (($this->tree[4] == "MESSAGE" || $this->tree[5] == "CONTACT" ) and ($this->tree[3] == "MESSAGES")) {
3552 if (!isset($this->temp)) {
3553 $this->temp = "";
3555 $this->temp .= "<".$tagName.">";
3559 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
3560 function startElementQuestions($parser, $tagName, $attrs) {
3561 //Refresh properties
3562 $this->level++;
3563 $this->tree[$this->level] = $tagName;
3565 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
3566 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
3567 //} //Debug
3569 //Output something to avoid browser timeouts...
3570 backup_flush();
3572 //Check if we are into QUESTION_CATEGORIES zone
3573 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
3574 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3576 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
3577 if (isset($this->tree[4]) and isset($this->tree[3])) {
3578 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
3579 if (!isset($this->temp)) {
3580 $this->temp = "";
3582 $this->temp .= "<".$tagName.">";
3587 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
3588 function startElementScales($parser, $tagName, $attrs) {
3589 //Refresh properties
3590 $this->level++;
3591 $this->tree[$this->level] = $tagName;
3593 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
3594 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
3595 //} //Debug
3597 //Output something to avoid browser timeouts...
3598 backup_flush();
3600 //Check if we are into SCALES zone
3601 //if ($this->tree[3] == "SCALES") //Debug
3602 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3604 //If we are under a SCALE tag under a SCALES zone, accumule it
3605 if (isset($this->tree[4]) and isset($this->tree[3])) {
3606 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
3607 if (!isset($this->temp)) {
3608 $this->temp = "";
3610 $this->temp .= "<".$tagName.">";
3615 function startElementGroups($parser, $tagName, $attrs) {
3616 //Refresh properties
3617 $this->level++;
3618 $this->tree[$this->level] = $tagName;
3620 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
3621 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
3622 //} //Debug
3624 //Output something to avoid browser timeouts...
3625 backup_flush();
3627 //Check if we are into GROUPS zone
3628 //if ($this->tree[3] == "GROUPS") //Debug
3629 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3631 //If we are under a GROUP tag under a GROUPS zone, accumule it
3632 if (isset($this->tree[4]) and isset($this->tree[3])) {
3633 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
3634 if (!isset($this->temp)) {
3635 $this->temp = "";
3637 $this->temp .= "<".$tagName.">";
3642 function startElementGroupings($parser, $tagName, $attrs) { //TODO:
3643 //Refresh properties
3644 $this->level++;
3645 $this->tree[$this->level] = $tagName;
3647 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
3648 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
3649 //} //Debug
3651 //Output something to avoid browser timeouts...
3652 backup_flush();
3654 //Check if we are into GROUPINGS zone
3655 //if ($this->tree[3] == "GROUPINGS") //Debug
3656 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3658 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
3659 if (isset($this->tree[4]) and isset($this->tree[3])) {
3660 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
3661 if (!isset($this->temp)) {
3662 $this->temp = "";
3664 $this->temp .= "<".$tagName.">";
3669 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
3670 function startElementEvents($parser, $tagName, $attrs) {
3671 //Refresh properties
3672 $this->level++;
3673 $this->tree[$this->level] = $tagName;
3675 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
3676 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
3677 //} //Debug
3679 //Output something to avoid browser timeouts...
3680 backup_flush();
3682 //Check if we are into EVENTS zone
3683 //if ($this->tree[3] == "EVENTS") //Debug
3684 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3686 //If we are under a EVENT tag under a EVENTS zone, accumule it
3687 if (isset($this->tree[4]) and isset($this->tree[3])) {
3688 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
3689 if (!isset($this->temp)) {
3690 $this->temp = "";
3692 $this->temp .= "<".$tagName.">";
3697 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
3698 function startElementModules($parser, $tagName, $attrs) {
3699 //Refresh properties
3700 $this->level++;
3701 $this->tree[$this->level] = $tagName;
3703 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
3704 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
3705 //} //Debug
3707 //Output something to avoid browser timeouts...
3708 backup_flush();
3710 //Check if we are into MODULES zone
3711 //if ($this->tree[3] == "MODULES") //Debug
3712 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3714 //If we are under a MOD tag under a MODULES zone, accumule it
3715 if (isset($this->tree[4]) and isset($this->tree[3])) {
3716 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
3717 if (!isset($this->temp)) {
3718 $this->temp = "";
3720 $this->temp .= "<".$tagName.">";
3725 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
3726 function startElementLogs($parser, $tagName, $attrs) {
3727 //Refresh properties
3728 $this->level++;
3729 $this->tree[$this->level] = $tagName;
3731 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
3732 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
3733 //} //Debug
3735 //Output something to avoid browser timeouts...
3736 backup_flush();
3738 //Check if we are into LOGS zone
3739 //if ($this->tree[3] == "LOGS") //Debug
3740 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3742 //If we are under a LOG tag under a LOGS zone, accumule it
3743 if (isset($this->tree[4]) and isset($this->tree[3])) {
3744 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
3745 if (!isset($this->temp)) {
3746 $this->temp = "";
3748 $this->temp .= "<".$tagName.">";
3753 //This is the startTag default handler we use when todo is undefined
3754 function startElement($parser, $tagName, $attrs) {
3755 $this->level++;
3756 $this->tree[$this->level] = $tagName;
3758 //Output something to avoid browser timeouts...
3759 backup_flush();
3761 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3764 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
3765 function endElementInfo($parser, $tagName) {
3766 //Check if we are into INFO zone
3767 if ($this->tree[2] == "INFO") {
3768 //if (trim($this->content)) //Debug
3769 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
3770 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
3771 //Dependig of different combinations, do different things
3772 if ($this->level == 3) {
3773 switch ($tagName) {
3774 case "NAME":
3775 $this->info->backup_name = $this->getContents();
3776 break;
3777 case "MOODLE_VERSION":
3778 $this->info->backup_moodle_version = $this->getContents();
3779 break;
3780 case "MOODLE_RELEASE":
3781 $this->info->backup_moodle_release = $this->getContents();
3782 break;
3783 case "BACKUP_VERSION":
3784 $this->info->backup_backup_version = $this->getContents();
3785 break;
3786 case "BACKUP_RELEASE":
3787 $this->info->backup_backup_release = $this->getContents();
3788 break;
3789 case "DATE":
3790 $this->info->backup_date = $this->getContents();
3791 break;
3792 case "ORIGINAL_WWWROOT":
3793 $this->info->original_wwwroot = $this->getContents();
3794 break;
3795 case "MNET_EXTERNALUSERS":
3796 $this->info->mnet_externalusers = $this->getContents();
3797 break;
3800 if ($this->tree[3] == "DETAILS") {
3801 if ($this->level == 4) {
3802 switch ($tagName) {
3803 case "METACOURSE":
3804 $this->info->backup_metacourse = $this->getContents();
3805 break;
3806 case "USERS":
3807 $this->info->backup_users = $this->getContents();
3808 break;
3809 case "LOGS":
3810 $this->info->backup_logs = $this->getContents();
3811 break;
3812 case "USERFILES":
3813 $this->info->backup_user_files = $this->getContents();
3814 break;
3815 case "COURSEFILES":
3816 $this->info->backup_course_files = $this->getContents();
3817 break;
3818 case "MESSAGES":
3819 $this->info->backup_messages = $this->getContents();
3820 break;
3821 case 'BLOCKFORMAT':
3822 $this->info->backup_block_format = $this->getContents();
3823 break;
3826 if ($this->level == 5) {
3827 switch ($tagName) {
3828 case "NAME":
3829 $this->info->tempName = $this->getContents();
3830 break;
3831 case "INCLUDED":
3832 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
3833 break;
3834 case "USERINFO":
3835 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
3836 break;
3839 if ($this->level == 7) {
3840 switch ($tagName) {
3841 case "ID":
3842 $this->info->tempId = $this->getContents();
3843 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
3844 break;
3845 case "NAME":
3846 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
3847 break;
3848 case "INCLUDED":
3849 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
3850 break;
3851 case "USERINFO":
3852 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
3853 break;
3859 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
3860 //Speed up a lot (avoid parse all)
3861 if ($tagName == "INFO") {
3862 $this->finished = true;
3865 //Clear things
3866 $this->tree[$this->level] = "";
3867 $this->level--;
3868 $this->content = "";
3872 function endElementRoles($parser, $tagName) {
3873 //Check if we are into INFO zone
3874 if ($this->tree[2] == "ROLES") {
3876 if ($this->tree[3] == "ROLE") {
3877 if ($this->level == 4) {
3878 switch ($tagName) {
3879 case "NAME":
3880 $this->info->tempname = $this->getContents();
3882 break;
3883 case "SHORTNAME":
3884 $this->info->tempshortname = $this->getContents();
3885 break;
3886 case "ID": // this is the old id
3887 $this->info->tempid = $this->getContents();
3888 break;
3891 if ($this->level == 6) {
3892 switch ($tagName) {
3893 case "NAME":
3894 $this->info->roles[$this->info->tempid]->name = $this->info->tempname;
3895 $this->info->roles[$this->info->tempid]->shortname = $this->info->tempshortname;
3897 $this->info->tempcapname = $this->getContents();
3898 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
3899 break;
3900 case "PERMISSION":
3901 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
3902 break;
3903 case "TIMEMODIFIED":
3904 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
3905 break;
3906 case "MODIFIERID":
3907 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
3908 break;
3914 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
3915 //Speed up a lot (avoid parse all)
3916 if ($tagName == "ROLES") {
3917 $this->finished = true;
3920 //Clear things
3921 $this->tree[$this->level] = "";
3922 $this->level--;
3923 $this->content = "";
3927 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
3928 function endElementCourseHeader($parser, $tagName) {
3929 //Check if we are into COURSE_HEADER zone
3930 if ($this->tree[3] == "HEADER") {
3931 //if (trim($this->content)) //Debug
3932 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
3933 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
3934 //Dependig of different combinations, do different things
3935 if ($this->level == 4) {
3936 switch ($tagName) {
3937 case "ID":
3938 $this->info->course_id = $this->getContents();
3939 break;
3940 case "PASSWORD":
3941 $this->info->course_password = $this->getContents();
3942 break;
3943 case "FULLNAME":
3944 $this->info->course_fullname = $this->getContents();
3945 break;
3946 case "SHORTNAME":
3947 $this->info->course_shortname = $this->getContents();
3948 break;
3949 case "IDNUMBER":
3950 $this->info->course_idnumber = $this->getContents();
3951 break;
3952 case "SUMMARY":
3953 $this->info->course_summary = $this->getContents();
3954 break;
3955 case "FORMAT":
3956 $this->info->course_format = $this->getContents();
3957 break;
3958 case "SHOWGRADES":
3959 $this->info->course_showgrades = $this->getContents();
3960 break;
3961 case "BLOCKINFO":
3962 $this->info->blockinfo = $this->getContents();
3963 break;
3964 case "NEWSITEMS":
3965 $this->info->course_newsitems = $this->getContents();
3966 break;
3967 case "TEACHER":
3968 $this->info->course_teacher = $this->getContents();
3969 break;
3970 case "TEACHERS":
3971 $this->info->course_teachers = $this->getContents();
3972 break;
3973 case "STUDENT":
3974 $this->info->course_student = $this->getContents();
3975 break;
3976 case "STUDENTS":
3977 $this->info->course_students = $this->getContents();
3978 break;
3979 case "GUEST":
3980 $this->info->course_guest = $this->getContents();
3981 break;
3982 case "STARTDATE":
3983 $this->info->course_startdate = $this->getContents();
3984 break;
3985 case "NUMSECTIONS":
3986 $this->info->course_numsections = $this->getContents();
3987 break;
3988 //case "SHOWRECENT": INFO: This is out in 1.3
3989 // $this->info->course_showrecent = $this->getContents();
3990 // break;
3991 case "MAXBYTES":
3992 $this->info->course_maxbytes = $this->getContents();
3993 break;
3994 case "SHOWREPORTS":
3995 $this->info->course_showreports = $this->getContents();
3996 break;
3997 case "GROUPMODE":
3998 $this->info->course_groupmode = $this->getContents();
3999 break;
4000 case "GROUPMODEFORCE":
4001 $this->info->course_groupmodeforce = $this->getContents();
4002 break;
4003 case "LANG":
4004 $this->info->course_lang = $this->getContents();
4005 break;
4006 case "THEME":
4007 $this->info->course_theme = $this->getContents();
4008 break;
4009 case "COST":
4010 $this->info->course_cost = $this->getContents();
4011 break;
4012 case "CURRENCY":
4013 $this->info->course_currency = $this->getContents();
4014 break;
4015 case "MARKER":
4016 $this->info->course_marker = $this->getContents();
4017 break;
4018 case "VISIBLE":
4019 $this->info->course_visible = $this->getContents();
4020 break;
4021 case "HIDDENSECTIONS":
4022 $this->info->course_hiddensections = $this->getContents();
4023 break;
4024 case "TIMECREATED":
4025 $this->info->course_timecreated = $this->getContents();
4026 break;
4027 case "TIMEMODIFIED":
4028 $this->info->course_timemodified = $this->getContents();
4029 break;
4030 case "METACOURSE":
4031 $this->info->course_metacourse = $this->getContents();
4032 break;
4033 case "EXPIRENOTIFY":
4034 $this->info->course_expirynotify = $this->getContents();
4035 break;
4036 case "NOTIFYSTUDENTS":
4037 $this->info->course_notifystudents = $this->getContents();
4038 break;
4039 case "EXPIRYTHRESHOLD":
4040 $this->info->course_expirythreshold = $this->getContents();
4041 break;
4042 case "ENROLLABLE":
4043 $this->info->course_enrollable = $this->getContents();
4044 break;
4045 case "ENROLSTARTDATE":
4046 $this->info->course_enrolstartdate = $this->getContents();
4047 break;
4048 case "ENROLENDDATE":
4049 $this->info->course_enrolenddate = $this->getContents();
4050 break;
4051 case "ENROLPERIOD":
4052 $this->info->course_enrolperiod = $this->getContents();
4053 break;
4056 if ($this->tree[4] == "CATEGORY") {
4057 if ($this->level == 5) {
4058 switch ($tagName) {
4059 case "ID":
4060 $this->info->category->id = $this->getContents();
4061 break;
4062 case "NAME":
4063 $this->info->category->name = $this->getContents();
4064 break;
4069 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
4070 if ($this->level == 6) {
4071 switch ($tagName) {
4072 case "NAME":
4073 $this->info->tempname = $this->getContents();
4074 break;
4075 case "SHORTNAME":
4076 $this->info->tempshortname = $this->getContents();
4077 break;
4078 case "ID":
4079 $this->info->tempid = $this->getContents();
4080 break;
4084 if ($this->level == 8) {
4085 switch ($tagName) {
4086 case "USERID":
4087 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4088 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4089 $this->info->tempuser = $this->getContents();
4090 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4091 break;
4092 case "HIDDEN":
4093 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4094 break;
4095 case "TIMESTART":
4096 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4097 break;
4098 case "TIMEEND":
4099 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4100 break;
4101 case "TIMEMODIFIED":
4102 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4103 break;
4104 case "MODIFIERID":
4105 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4106 break;
4107 case "ENROL":
4108 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4109 break;
4110 case "SORTORDER":
4111 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4112 break;
4116 } /// ends role_assignments
4118 if ($this->tree[4] == "ROLES_OVERRIDES") {
4119 if ($this->level == 6) {
4120 switch ($tagName) {
4121 case "NAME":
4122 $this->info->tempname = $this->getContents();
4123 break;
4124 case "SHORTNAME":
4125 $this->info->tempshortname = $this->getContents();
4126 break;
4127 case "ID":
4128 $this->info->tempid = $this->getContents();
4129 break;
4133 if ($this->level == 8) {
4134 switch ($tagName) {
4135 case "NAME":
4136 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4137 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4138 $this->info->tempname = $this->getContents(); // change to name of capability
4139 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4140 break;
4141 case "PERMISSION":
4142 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4143 break;
4144 case "TIMEMODIFIED":
4145 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4146 break;
4147 case "MODIFIERID":
4148 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4149 break;
4152 } /// ends role_overrides
4155 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
4156 //Speed up a lot (avoid parse all)
4157 if ($tagName == "HEADER") {
4158 $this->finished = true;
4161 //Clear things
4162 $this->tree[$this->level] = "";
4163 $this->level--;
4164 $this->content = "";
4168 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
4169 function endElementBlocks($parser, $tagName) {
4170 //Check if we are into BLOCKS zone
4171 if ($this->tree[3] == 'BLOCKS') {
4172 //if (trim($this->content)) //Debug
4173 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4174 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4175 //Dependig of different combinations, do different things
4176 if ($this->level == 4) {
4177 switch ($tagName) {
4178 case 'BLOCK':
4179 //We've finalized a block, get it
4180 $this->info->instances[] = $this->info->tempinstance;
4181 unset($this->info->tempinstance);
4182 break;
4183 default:
4184 die($tagName);
4187 if ($this->level == 5) {
4188 switch ($tagName) {
4189 case 'ID':
4190 $this->info->tempinstance->id = $this->getContents();
4191 case 'NAME':
4192 $this->info->tempinstance->name = $this->getContents();
4193 break;
4194 case 'PAGEID':
4195 $this->info->tempinstance->pageid = $this->getContents();
4196 break;
4197 case 'PAGETYPE':
4198 $this->info->tempinstance->pagetype = $this->getContents();
4199 break;
4200 case 'POSITION':
4201 $this->info->tempinstance->position = $this->getContents();
4202 break;
4203 case 'WEIGHT':
4204 $this->info->tempinstance->weight = $this->getContents();
4205 break;
4206 case 'VISIBLE':
4207 $this->info->tempinstance->visible = $this->getContents();
4208 break;
4209 case 'CONFIGDATA':
4210 $this->info->tempinstance->configdata = $this->getContents();
4211 break;
4212 default:
4213 break;
4217 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
4218 if ($this->level == 7) {
4219 switch ($tagName) {
4220 case "NAME":
4221 $this->info->tempname = $this->getContents();
4222 break;
4223 case "SHORTNAME":
4224 $this->info->tempshortname = $this->getContents();
4225 break;
4226 case "ID":
4227 $this->info->tempid = $this->getContents(); // temp roleid
4228 break;
4232 if ($this->level == 9) {
4234 switch ($tagName) {
4235 case "USERID":
4236 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4238 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4240 $this->info->tempuser = $this->getContents();
4242 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4243 break;
4244 case "HIDDEN":
4245 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4246 break;
4247 case "TIMESTART":
4248 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4249 break;
4250 case "TIMEEND":
4251 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4252 break;
4253 case "TIMEMODIFIED":
4254 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4255 break;
4256 case "MODIFIERID":
4257 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4258 break;
4259 case "ENROL":
4260 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4261 break;
4262 case "SORTORDER":
4263 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4264 break;
4268 } /// ends role_assignments
4270 if ($this->tree[5] == "ROLES_OVERRIDES") {
4271 if ($this->level == 7) {
4272 switch ($tagName) {
4273 case "NAME":
4274 $this->info->tempname = $this->getContents();
4275 break;
4276 case "SHORTNAME":
4277 $this->info->tempshortname = $this->getContents();
4278 break;
4279 case "ID":
4280 $this->info->tempid = $this->getContents(); // temp roleid
4281 break;
4285 if ($this->level == 9) {
4286 switch ($tagName) {
4287 case "NAME":
4289 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4290 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4291 $this->info->tempname = $this->getContents(); // change to name of capability
4292 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4293 break;
4294 case "PERMISSION":
4295 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4296 break;
4297 case "TIMEMODIFIED":
4298 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4299 break;
4300 case "MODIFIERID":
4301 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4302 break;
4305 } /// ends role_overrides
4308 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
4309 //Speed up a lot (avoid parse all)
4310 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
4311 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
4312 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
4313 $this->finished = true;
4316 //Clear things
4317 $this->tree[$this->level] = '';
4318 $this->level--;
4319 $this->content = "";
4322 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
4323 function endElementSections($parser, $tagName) {
4324 //Check if we are into SECTIONS zone
4325 if ($this->tree[3] == "SECTIONS") {
4326 //if (trim($this->content)) //Debug
4327 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4328 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4329 //Dependig of different combinations, do different things
4330 if ($this->level == 4) {
4331 switch ($tagName) {
4332 case "SECTION":
4333 //We've finalized a section, get it
4334 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
4335 unset($this->info->tempsection);
4338 if ($this->level == 5) {
4339 switch ($tagName) {
4340 case "ID":
4341 $this->info->tempsection->id = $this->getContents();
4342 break;
4343 case "NUMBER":
4344 $this->info->tempsection->number = $this->getContents();
4345 break;
4346 case "SUMMARY":
4347 $this->info->tempsection->summary = $this->getContents();
4348 break;
4349 case "VISIBLE":
4350 $this->info->tempsection->visible = $this->getContents();
4351 break;
4354 if ($this->level == 6) {
4355 switch ($tagName) {
4356 case "MOD":
4357 //We've finalized a mod, get it
4358 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
4359 $this->info->tempmod->type;
4360 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
4361 $this->info->tempmod->instance;
4362 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
4363 $this->info->tempmod->added;
4364 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
4365 $this->info->tempmod->score;
4366 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
4367 $this->info->tempmod->indent;
4368 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
4369 $this->info->tempmod->visible;
4370 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
4371 $this->info->tempmod->groupmode;
4372 unset($this->info->tempmod);
4375 if ($this->level == 7) {
4376 switch ($tagName) {
4377 case "ID":
4378 $this->info->tempmod->id = $this->getContents();
4379 break;
4380 case "TYPE":
4381 $this->info->tempmod->type = $this->getContents();
4382 break;
4383 case "INSTANCE":
4384 $this->info->tempmod->instance = $this->getContents();
4385 break;
4386 case "ADDED":
4387 $this->info->tempmod->added = $this->getContents();
4388 break;
4389 case "SCORE":
4390 $this->info->tempmod->score = $this->getContents();
4391 break;
4392 case "INDENT":
4393 $this->info->tempmod->indent = $this->getContents();
4394 break;
4395 case "VISIBLE":
4396 $this->info->tempmod->visible = $this->getContents();
4397 break;
4398 case "GROUPMODE":
4399 $this->info->tempmod->groupmode = $this->getContents();
4400 break;
4401 default:
4402 break;
4406 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
4408 if ($this->level == 9) {
4409 switch ($tagName) {
4410 case "NAME":
4411 $this->info->tempname = $this->getContents();
4412 break;
4413 case "SHORTNAME":
4414 $this->info->tempshortname = $this->getContents();
4415 break;
4416 case "ID":
4417 $this->info->tempid = $this->getContents(); // temp roleid
4418 break;
4422 if ($this->level == 11) {
4423 switch ($tagName) {
4424 case "USERID":
4425 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4427 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4429 $this->info->tempuser = $this->getContents();
4431 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4432 break;
4433 case "HIDDEN":
4434 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4435 break;
4436 case "TIMESTART":
4437 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4438 break;
4439 case "TIMEEND":
4440 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4441 break;
4442 case "TIMEMODIFIED":
4443 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4444 break;
4445 case "MODIFIERID":
4446 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4447 break;
4448 case "ENROL":
4449 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4450 break;
4451 case "SORTORDER":
4452 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4453 break;
4457 } /// ends role_assignments
4459 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
4460 if ($this->level == 9) {
4461 switch ($tagName) {
4462 case "NAME":
4463 $this->info->tempname = $this->getContents();
4464 break;
4465 case "SHORTNAME":
4466 $this->info->tempshortname = $this->getContents();
4467 break;
4468 case "ID":
4469 $this->info->tempid = $this->getContents(); // temp roleid
4470 break;
4474 if ($this->level == 11) {
4475 switch ($tagName) {
4476 case "NAME":
4478 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4479 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4480 $this->info->tempname = $this->getContents(); // change to name of capability
4481 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4482 break;
4483 case "PERMISSION":
4484 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4485 break;
4486 case "TIMEMODIFIED":
4487 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4488 break;
4489 case "MODIFIERID":
4490 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4491 break;
4494 } /// ends role_overrides
4498 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
4499 //Speed up a lot (avoid parse all)
4500 if ($tagName == "SECTIONS") {
4501 $this->finished = true;
4504 //Clear things
4505 $this->tree[$this->level] = "";
4506 $this->level--;
4507 $this->content = "";
4511 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4512 function endElementFormatData($parser, $tagName) {
4513 //Check if we are into FORMATDATA zone
4514 if ($this->tree[3] == 'FORMATDATA') {
4515 if (!isset($this->temp)) {
4516 $this->temp = '';
4518 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
4521 if($tagName=='FORMATDATA') {
4522 //Did we have any data? If not don't bother
4523 if($this->temp!='<FORMATDATA></FORMATDATA>') {
4524 //Prepend XML standard header to info gathered
4525 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4526 $this->temp='';
4528 //Call to xmlize for this portion of xml data (the FORMATDATA block)
4529 $this->info->format_data = xmlize($xml_data,0);
4531 //Stop parsing at end of FORMATDATA
4532 $this->finished=true;
4535 //Clear things
4536 $this->tree[$this->level] = "";
4537 $this->level--;
4538 $this->content = "";
4541 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4542 function endElementMetacourse($parser, $tagName) {
4543 //Check if we are into METACOURSE zone
4544 if ($this->tree[3] == 'METACOURSE') {
4545 //if (trim($this->content)) //Debug
4546 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4547 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4548 //Dependig of different combinations, do different things
4549 if ($this->level == 5) {
4550 switch ($tagName) {
4551 case 'CHILD':
4552 //We've finalized a child, get it
4553 $this->info->childs[] = $this->info->tempmeta;
4554 unset($this->info->tempmeta);
4555 break;
4556 case 'PARENT':
4557 //We've finalized a parent, get it
4558 $this->info->parents[] = $this->info->tempmeta;
4559 unset($this->info->tempmeta);
4560 break;
4561 default:
4562 die($tagName);
4565 if ($this->level == 6) {
4566 switch ($tagName) {
4567 case 'ID':
4568 $this->info->tempmeta->id = $this->getContents();
4569 break;
4570 case 'IDNUMBER':
4571 $this->info->tempmeta->idnumber = $this->getContents();
4572 break;
4573 case 'SHORTNAME':
4574 $this->info->tempmeta->shortname = $this->getContents();
4575 break;
4580 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
4581 //Speed up a lot (avoid parse all)
4582 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
4583 $this->finished = true;
4586 //Clear things
4587 $this->tree[$this->level] = '';
4588 $this->level--;
4589 $this->content = "";
4592 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4593 function endElementGradebook($parser, $tagName) {
4594 //Check if we are into GRADEBOOK zone
4595 if ($this->tree[3] == "GRADEBOOK") {
4596 //if (trim($this->content)) //Debug
4597 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4598 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
4599 //Acumulate data to info (content + close tag)
4600 //Reconvert: strip htmlchars again and trim to generate xml data
4601 if (!isset($this->temp)) {
4602 $this->temp = "";
4604 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
4605 // We have finished outcome, grade_category or grade_item, reset accumulated
4606 // data because they are close tags
4607 if ($this->level == 4) {
4608 $this->temp = "";
4610 //If we've finished a message, xmlize it an save to db
4611 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
4612 //Prepend XML standard header to info gathered
4613 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4614 //Call to xmlize for this portion of xml data (one PREFERENCE)
4615 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
4616 $data = xmlize($xml_data,0);
4617 //echo strftime ("%X",time())."<p>"; //Debug
4618 //traverse_xmlize($data); //Debug
4619 //print_object ($GLOBALS['traverse_array']); //Debug
4620 //$GLOBALS['traverse_array']=""; //Debug
4621 //Now, save data to db. We'll use it later
4622 //Get id and status from data
4623 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
4624 $this->counter++;
4625 //Save to db
4627 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
4628 null,$data);
4629 //Create returning info
4630 $this->info = $this->counter;
4631 //Reset temp
4633 unset($this->temp);
4636 //If we've finished a grade_category, xmlize it an save to db
4637 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
4638 //Prepend XML standard header to info gathered
4639 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4640 //Call to xmlize for this portion of xml data (one CATECORY)
4641 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
4642 $data = xmlize($xml_data,0);
4643 //echo strftime ("%X",time())."<p>"; //Debug
4644 //traverse_xmlize($data); //Debug
4645 //print_object ($GLOBALS['traverse_array']); //Debug
4646 //$GLOBALS['traverse_array']=""; //Debug
4647 //Now, save data to db. We'll use it later
4648 //Get id and status from data
4649 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
4650 $this->counter++;
4651 //Save to db
4652 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
4653 null,$data);
4654 //Create returning info
4655 $this->info = $this->counter;
4656 //Reset temp
4657 unset($this->temp);
4660 //If we've finished a grade_category, xmlize it an save to db
4661 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
4662 //Prepend XML standard header to info gathered
4663 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4664 //Call to xmlize for this portion of xml data (one CATECORY)
4665 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
4666 $data = xmlize($xml_data,0);
4667 //echo strftime ("%X",time())."<p>"; //Debug
4668 //traverse_xmlize($data); //Debug
4669 //print_object ($GLOBALS['traverse_array']); //Debug
4670 //$GLOBALS['traverse_array']=""; //Debug
4671 //Now, save data to db. We'll use it later
4672 //Get id and status from data
4673 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
4674 $this->counter++;
4675 //Save to db
4676 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
4677 null,$data);
4678 //Create returning info
4679 $this->info = $this->counter;
4680 //Reset temp
4681 unset($this->temp);
4685 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
4686 //Speed up a lot (avoid parse all)
4687 if ($tagName == "GRADEBOOK" and $this->level == 3) {
4688 $this->finished = true;
4689 $this->counter = 0;
4692 //Clear things
4693 $this->tree[$this->level] = "";
4694 $this->level--;
4695 $this->content = "";
4699 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
4700 function endElementUsers($parser, $tagName) {
4701 global $CFG;
4702 //Check if we are into USERS zone
4703 if ($this->tree[3] == "USERS") {
4704 //if (trim($this->content)) //Debug
4705 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4706 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4707 //Dependig of different combinations, do different things
4708 if ($this->level == 4) {
4709 switch ($tagName) {
4710 case "USER":
4711 //Increment counter
4712 $this->counter++;
4713 //Save to db, only save if record not already exist
4714 // if there already is an new_id for this entry, just use that new_id?
4715 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
4716 if (isset($newuser->new_id)) {
4717 $newid = $newuser->new_id;
4718 } else {
4719 $newid = null;
4722 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
4723 $newid,$this->info->tempuser);
4725 //Do some output
4726 if ($this->counter % 10 == 0) {
4727 if (!defined('RESTORE_SILENTLY')) {
4728 echo ".";
4729 if ($this->counter % 200 == 0) {
4730 echo "<br />";
4733 backup_flush(300);
4736 //Delete temp obejct
4737 unset($this->info->tempuser);
4738 break;
4741 if ($this->level == 5) {
4742 switch ($tagName) {
4743 case "ID":
4744 $this->info->users[$this->getContents()] = $this->getContents();
4745 $this->info->tempuser->id = $this->getContents();
4746 break;
4747 case "AUTH":
4748 $this->info->tempuser->auth = $this->getContents();
4749 break;
4750 case "CONFIRMED":
4751 $this->info->tempuser->confirmed = $this->getContents();
4752 break;
4753 case "POLICYAGREED":
4754 $this->info->tempuser->policyagreed = $this->getContents();
4755 break;
4756 case "DELETED":
4757 $this->info->tempuser->deleted = $this->getContents();
4758 break;
4759 case "USERNAME":
4760 $this->info->tempuser->username = $this->getContents();
4761 break;
4762 case "PASSWORD":
4763 $this->info->tempuser->password = $this->getContents();
4764 break;
4765 case "IDNUMBER":
4766 $this->info->tempuser->idnumber = $this->getContents();
4767 break;
4768 case "FIRSTNAME":
4769 $this->info->tempuser->firstname = $this->getContents();
4770 break;
4771 case "LASTNAME":
4772 $this->info->tempuser->lastname = $this->getContents();
4773 break;
4774 case "EMAIL":
4775 $this->info->tempuser->email = $this->getContents();
4776 break;
4777 case "EMAILSTOP":
4778 $this->info->tempuser->emailstop = $this->getContents();
4779 break;
4780 case "ICQ":
4781 $this->info->tempuser->icq = $this->getContents();
4782 break;
4783 case "SKYPE":
4784 $this->info->tempuser->skype = $this->getContents();
4785 break;
4786 case "AIM":
4787 $this->info->tempuser->aim = $this->getContents();
4788 break;
4789 case "YAHOO":
4790 $this->info->tempuser->yahoo = $this->getContents();
4791 break;
4792 case "MSN":
4793 $this->info->tempuser->msn = $this->getContents();
4794 break;
4795 case "PHONE1":
4796 $this->info->tempuser->phone1 = $this->getContents();
4797 break;
4798 case "PHONE2":
4799 $this->info->tempuser->phone2 = $this->getContents();
4800 break;
4801 case "INSTITUTION":
4802 $this->info->tempuser->institution = $this->getContents();
4803 break;
4804 case "DEPARTMENT":
4805 $this->info->tempuser->department = $this->getContents();
4806 break;
4807 case "ADDRESS":
4808 $this->info->tempuser->address = $this->getContents();
4809 break;
4810 case "CITY":
4811 $this->info->tempuser->city = $this->getContents();
4812 break;
4813 case "COUNTRY":
4814 $this->info->tempuser->country = $this->getContents();
4815 break;
4816 case "LANG":
4817 $this->info->tempuser->lang = $this->getContents();
4818 break;
4819 case "THEME":
4820 $this->info->tempuser->theme = $this->getContents();
4821 break;
4822 case "TIMEZONE":
4823 $this->info->tempuser->timezone = $this->getContents();
4824 break;
4825 case "FIRSTACCESS":
4826 $this->info->tempuser->firstaccess = $this->getContents();
4827 break;
4828 case "LASTACCESS":
4829 $this->info->tempuser->lastaccess = $this->getContents();
4830 break;
4831 case "LASTLOGIN":
4832 $this->info->tempuser->lastlogin = $this->getContents();
4833 break;
4834 case "CURRENTLOGIN":
4835 $this->info->tempuser->currentlogin = $this->getContents();
4836 break;
4837 case "LASTIP":
4838 $this->info->tempuser->lastip = $this->getContents();
4839 break;
4840 case "SECRET":
4841 $this->info->tempuser->secret = $this->getContents();
4842 break;
4843 case "PICTURE":
4844 $this->info->tempuser->picture = $this->getContents();
4845 break;
4846 case "URL":
4847 $this->info->tempuser->url = $this->getContents();
4848 break;
4849 case "DESCRIPTION":
4850 $this->info->tempuser->description = $this->getContents();
4851 break;
4852 case "MAILFORMAT":
4853 $this->info->tempuser->mailformat = $this->getContents();
4854 break;
4855 case "MAILDIGEST":
4856 $this->info->tempuser->maildigest = $this->getContents();
4857 break;
4858 case "MAILDISPLAY":
4859 $this->info->tempuser->maildisplay = $this->getContents();
4860 break;
4861 case "HTMLEDITOR":
4862 $this->info->tempuser->htmleditor = $this->getContents();
4863 break;
4864 case "AJAX":
4865 $this->info->tempuser->ajax = $this->getContents();
4866 break;
4867 case "AUTOSUBSCRIBE":
4868 $this->info->tempuser->autosubscribe = $this->getContents();
4869 break;
4870 case "TRACKFORUMS":
4871 $this->info->tempuser->trackforums = $this->getContents();
4872 break;
4873 case "MNETHOSTURL":
4874 $this->info->tempuser->mnethosturl = $this->getContents();
4875 break;
4876 case "TIMEMODIFIED":
4877 $this->info->tempuser->timemodified = $this->getContents();
4878 break;
4879 default:
4880 break;
4883 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
4884 switch ($tagName) {
4885 case "ROLE":
4886 //We've finalized a role, get it
4887 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
4888 unset($this->info->temprole);
4889 break;
4890 case "USER_PREFERENCE":
4891 //We've finalized a user_preference, get it
4892 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
4893 unset($this->info->tempuserpreference);
4894 break;
4898 if ($this->level == 7) {
4899 switch ($tagName) {
4900 case "TYPE":
4901 $this->info->temprole->type = $this->getContents();
4902 break;
4903 case "AUTHORITY":
4904 $this->info->temprole->authority = $this->getContents();
4905 break;
4906 case "TEA_ROLE":
4907 $this->info->temprole->tea_role = $this->getContents();
4908 break;
4909 case "EDITALL":
4910 $this->info->temprole->editall = $this->getContents();
4911 break;
4912 case "TIMESTART":
4913 $this->info->temprole->timestart = $this->getContents();
4914 break;
4915 case "TIMEEND":
4916 $this->info->temprole->timeend = $this->getContents();
4917 break;
4918 case "TIMEMODIFIED":
4919 $this->info->temprole->timemodified = $this->getContents();
4920 break;
4921 case "TIMESTART":
4922 $this->info->temprole->timestart = $this->getContents();
4923 break;
4924 case "TIMEEND":
4925 $this->info->temprole->timeend = $this->getContents();
4926 break;
4927 case "TIME":
4928 $this->info->temprole->time = $this->getContents();
4929 break;
4930 case "TIMEACCESS":
4931 $this->info->temprole->timeaccess = $this->getContents();
4932 break;
4933 case "ENROL":
4934 $this->info->temprole->enrol = $this->getContents();
4935 break;
4936 case "NAME":
4937 $this->info->tempuserpreference->name = $this->getContents();
4938 break;
4939 case "VALUE":
4940 $this->info->tempuserpreference->value = $this->getContents();
4941 break;
4942 default:
4943 break;
4948 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
4950 if ($this->level == 7) {
4951 switch ($tagName) {
4952 case "NAME":
4953 $this->info->tempname = $this->getContents();
4954 break;
4955 case "SHORTNAME":
4956 $this->info->tempshortname = $this->getContents();
4957 break;
4958 case "ID":
4959 $this->info->tempid = $this->getContents(); // temp roleid
4960 break;
4964 if ($this->level == 9) {
4966 switch ($tagName) {
4967 case "USERID":
4968 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4970 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4972 $this->info->tempuserid = $this->getContents();
4974 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
4975 break;
4976 case "HIDDEN":
4977 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
4978 break;
4979 case "TIMESTART":
4980 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
4981 break;
4982 case "TIMEEND":
4983 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
4984 break;
4985 case "TIMEMODIFIED":
4986 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
4987 break;
4988 case "MODIFIERID":
4989 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
4990 break;
4991 case "ENROL":
4992 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
4993 break;
4994 case "SORTORDER":
4995 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
4996 break;
5000 } /// ends role_assignments
5002 if ($this->tree[5] == "ROLES_OVERRIDES") {
5003 if ($this->level == 7) {
5004 switch ($tagName) {
5005 case "NAME":
5006 $this->info->tempname = $this->getContents();
5007 break;
5008 case "SHORTNAME":
5009 $this->info->tempshortname = $this->getContents();
5010 break;
5011 case "ID":
5012 $this->info->tempid = $this->getContents(); // temp roleid
5013 break;
5017 if ($this->level == 9) {
5018 switch ($tagName) {
5019 case "NAME":
5021 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5022 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5023 $this->info->tempname = $this->getContents(); // change to name of capability
5024 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5025 break;
5026 case "PERMISSION":
5027 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5028 break;
5029 case "TIMEMODIFIED":
5030 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5031 break;
5032 case "MODIFIERID":
5033 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5034 break;
5037 } /// ends role_overrides
5039 } // closes if this->tree[3]=="users"
5041 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
5042 //Speed up a lot (avoid parse all)
5043 if ($tagName == "USERS" and $this->level == 3) {
5044 $this->finished = true;
5045 $this->counter = 0;
5048 //Clear things
5049 $this->tree[$this->level] = "";
5050 $this->level--;
5051 $this->content = "";
5055 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
5056 function endElementMessages($parser, $tagName) {
5057 //Check if we are into MESSAGES zone
5058 if ($this->tree[3] == "MESSAGES") {
5059 //if (trim($this->content)) //Debug
5060 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5061 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5062 //Acumulate data to info (content + close tag)
5063 //Reconvert: strip htmlchars again and trim to generate xml data
5064 if (!isset($this->temp)) {
5065 $this->temp = "";
5067 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5068 //If we've finished a message, xmlize it an save to db
5069 if (($this->level == 4) and ($tagName == "MESSAGE")) {
5070 //Prepend XML standard header to info gathered
5071 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5072 //Call to xmlize for this portion of xml data (one MESSAGE)
5073 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5074 $data = xmlize($xml_data,0);
5075 //echo strftime ("%X",time())."<p>"; //Debug
5076 //traverse_xmlize($data); //Debug
5077 //print_object ($GLOBALS['traverse_array']); //Debug
5078 //$GLOBALS['traverse_array']=""; //Debug
5079 //Now, save data to db. We'll use it later
5080 //Get id and status from data
5081 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
5082 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
5083 if ($message_status == "READ") {
5084 $table = "message_read";
5085 } else {
5086 $table = "message";
5088 $this->counter++;
5089 //Save to db
5090 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
5091 null,$data);
5092 //Create returning info
5093 $this->info = $this->counter;
5094 //Reset temp
5095 unset($this->temp);
5097 //If we've finished a contact, xmlize it an save to db
5098 if (($this->level == 5) and ($tagName == "CONTACT")) {
5099 //Prepend XML standard header to info gathered
5100 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5101 //Call to xmlize for this portion of xml data (one MESSAGE)
5102 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5103 $data = xmlize($xml_data,0);
5104 //echo strftime ("%X",time())."<p>"; //Debug
5105 //traverse_xmlize($data); //Debug
5106 //print_object ($GLOBALS['traverse_array']); //Debug
5107 //$GLOBALS['traverse_array']=""; //Debug
5108 //Now, save data to db. We'll use it later
5109 //Get id and status from data
5110 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
5111 $this->counter++;
5112 //Save to db
5113 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
5114 null,$data);
5115 //Create returning info
5116 $this->info = $this->counter;
5117 //Reset temp
5118 unset($this->temp);
5122 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
5123 //Speed up a lot (avoid parse all)
5124 if ($tagName == "MESSAGES" and $this->level == 3) {
5125 $this->finished = true;
5126 $this->counter = 0;
5129 //Clear things
5130 $this->tree[$this->level] = "";
5131 $this->level--;
5132 $this->content = "";
5136 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
5137 function endElementQuestions($parser, $tagName) {
5138 //Check if we are into QUESTION_CATEGORIES zone
5139 if ($this->tree[3] == "QUESTION_CATEGORIES") {
5140 //if (trim($this->content)) //Debug
5141 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5142 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5143 //Acumulate data to info (content + close tag)
5144 //Reconvert: strip htmlchars again and trim to generate xml data
5145 if (!isset($this->temp)) {
5146 $this->temp = "";
5148 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5149 //If we've finished a mod, xmlize it an save to db
5150 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
5151 //Prepend XML standard header to info gathered
5152 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5153 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
5154 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5155 $data = xmlize($xml_data,0);
5156 //echo strftime ("%X",time())."<p>"; //Debug
5157 //traverse_xmlize($data); //Debug
5158 //print_object ($GLOBALS['traverse_array']); //Debug
5159 //$GLOBALS['traverse_array']=""; //Debug
5160 //Now, save data to db. We'll use it later
5161 //Get id from data
5162 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
5163 //Save to db
5164 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
5165 null,$data);
5166 //Create returning info
5167 $ret_info->id = $category_id;
5168 $this->info[] = $ret_info;
5169 //Reset temp
5170 unset($this->temp);
5174 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
5175 //Speed up a lot (avoid parse all)
5176 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
5177 $this->finished = true;
5180 //Clear things
5181 $this->tree[$this->level] = "";
5182 $this->level--;
5183 $this->content = "";
5187 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
5188 function endElementScales($parser, $tagName) {
5189 //Check if we are into SCALES zone
5190 if ($this->tree[3] == "SCALES") {
5191 //if (trim($this->content)) //Debug
5192 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5193 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5194 //Acumulate data to info (content + close tag)
5195 //Reconvert: strip htmlchars again and trim to generate xml data
5196 if (!isset($this->temp)) {
5197 $this->temp = "";
5199 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5200 //If we've finished a scale, xmlize it an save to db
5201 if (($this->level == 4) and ($tagName == "SCALE")) {
5202 //Prepend XML standard header to info gathered
5203 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5204 //Call to xmlize for this portion of xml data (one SCALE)
5205 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5206 $data = xmlize($xml_data,0);
5207 //echo strftime ("%X",time())."<p>"; //Debug
5208 //traverse_xmlize($data); //Debug
5209 //print_object ($GLOBALS['traverse_array']); //Debug
5210 //$GLOBALS['traverse_array']=""; //Debug
5211 //Now, save data to db. We'll use it later
5212 //Get id and from data
5213 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
5214 //Save to db
5215 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
5216 null,$data);
5217 //Create returning info
5218 $ret_info->id = $scale_id;
5219 $this->info[] = $ret_info;
5220 //Reset temp
5221 unset($this->temp);
5225 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
5226 //Speed up a lot (avoid parse all)
5227 if ($tagName == "SCALES" and $this->level == 3) {
5228 $this->finished = true;
5231 //Clear things
5232 $this->tree[$this->level] = "";
5233 $this->level--;
5234 $this->content = "";
5238 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
5239 function endElementGroups($parser, $tagName) {
5240 //Check if we are into GROUPS zone
5241 if ($this->tree[3] == "GROUPS") {
5242 //if (trim($this->content)) //Debug
5243 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5244 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5245 //Acumulate data to info (content + close tag)
5246 //Reconvert: strip htmlchars again and trim to generate xml data
5247 if (!isset($this->temp)) {
5248 $this->temp = "";
5250 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5251 //If we've finished a group, xmlize it an save to db
5252 if (($this->level == 4) and ($tagName == "GROUP")) {
5253 //Prepend XML standard header to info gathered
5254 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5255 //Call to xmlize for this portion of xml data (one GROUP)
5256 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5257 $data = xmlize($xml_data,0);
5258 //echo strftime ("%X",time())."<p>"; //Debug
5259 //traverse_xmlize($data); //Debug
5260 //print_object ($GLOBALS['traverse_array']); //Debug
5261 //$GLOBALS['traverse_array']=""; //Debug
5262 //Now, save data to db. We'll use it later
5263 //Get id and from data
5264 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
5265 //Save to db
5266 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
5267 null,$data);
5268 //Create returning info
5269 $ret_info = new Object();
5270 $ret_info->id = $group_id;
5271 $this->info[] = $ret_info;
5272 //Reset temp
5273 unset($this->temp);
5277 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
5278 //Speed up a lot (avoid parse all)
5279 if ($tagName == "GROUPS" and $this->level == 3) {
5280 $this->finished = true;
5283 //Clear things
5284 $this->tree[$this->level] = "";
5285 $this->level--;
5286 $this->content = "";
5290 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPINGS")
5291 function endElementGroupings($parser, $tagName) { //TODO:
5292 //Check if we are into GROUPS zone
5293 if ($this->tree[3] == "GROUPINGS") {
5294 //Acumulate data to info (content + close tag)
5295 //Reconvert: strip htmlchars again and trim to generate xml data
5296 if (!isset($this->temp)) {
5297 $this->temp = "";
5299 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5300 //If we've finished a group, xmlize it an save to db
5301 if (($this->level == 4) and ($tagName == "GROUPING")) {
5302 //Prepend XML standard header to info gathered
5303 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5304 //Call to xmlize for this portion of xml data (one GROUPING)
5305 $data = xmlize($xml_data,0);
5306 //Now, save data to db. We'll use it later
5307 //Get id and from data
5308 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
5309 //Save to db
5310 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
5311 null,$data);
5312 //Create returning info
5313 $ret_info = new Object();
5314 $ret_info->id = $grouping_id;
5315 $this->info[] = $ret_info;
5316 //Reset temp
5317 unset($this->temp);
5321 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
5322 //Speed up a lot (avoid parse all)
5323 if ($tagName == "GROUPINGS" and $this->level == 3) {
5324 $this->finished = true;
5327 //Clear things
5328 $this->tree[$this->level] = "";
5329 $this->level--;
5330 $this->content = "";
5334 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
5335 function endElementEvents($parser, $tagName) {
5336 //Check if we are into EVENTS zone
5337 if ($this->tree[3] == "EVENTS") {
5338 //if (trim($this->content)) //Debug
5339 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5340 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5341 //Acumulate data to info (content + close tag)
5342 //Reconvert: strip htmlchars again and trim to generate xml data
5343 if (!isset($this->temp)) {
5344 $this->temp = "";
5346 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5347 //If we've finished a event, xmlize it an save to db
5348 if (($this->level == 4) and ($tagName == "EVENT")) {
5349 //Prepend XML standard header to info gathered
5350 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5351 //Call to xmlize for this portion of xml data (one EVENT)
5352 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5353 $data = xmlize($xml_data,0);
5354 //echo strftime ("%X",time())."<p>"; //Debug
5355 //traverse_xmlize($data); //Debug
5356 //print_object ($GLOBALS['traverse_array']); //Debug
5357 //$GLOBALS['traverse_array']=""; //Debug
5358 //Now, save data to db. We'll use it later
5359 //Get id and from data
5360 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
5361 //Save to db
5362 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
5363 null,$data);
5364 //Create returning info
5365 $ret_info->id = $event_id;
5366 $this->info[] = $ret_info;
5367 //Reset temp
5368 unset($this->temp);
5372 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
5373 //Speed up a lot (avoid parse all)
5374 if ($tagName == "EVENTS" and $this->level == 3) {
5375 $this->finished = true;
5378 //Clear things
5379 $this->tree[$this->level] = "";
5380 $this->level--;
5381 $this->content = "";
5385 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
5386 function endElementModules($parser, $tagName) {
5387 //Check if we are into MODULES zone
5388 if ($this->tree[3] == "MODULES") {
5389 //if (trim($this->content)) //Debug
5390 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5391 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5392 //Acumulate data to info (content + close tag)
5393 //Reconvert: strip htmlchars again and trim to generate xml data
5394 if (!isset($this->temp)) {
5395 $this->temp = "";
5397 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5398 //If we've finished a mod, xmlize it an save to db
5399 if (($this->level == 4) and ($tagName == "MOD")) {
5400 //Prepend XML standard header to info gathered
5401 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5402 //Call to xmlize for this portion of xml data (one MOD)
5403 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5404 $data = xmlize($xml_data,0);
5405 //echo strftime ("%X",time())."<p>"; //Debug
5406 //traverse_xmlize($data); //Debug
5407 //print_object ($GLOBALS['traverse_array']); //Debug
5408 //$GLOBALS['traverse_array']=""; //Debug
5409 //Now, save data to db. We'll use it later
5410 //Get id and modtype from data
5411 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
5412 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
5413 //Only if we've selected to restore it
5414 if (!empty($this->preferences->mods[$mod_type]->restore)) {
5415 //Save to db
5416 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
5417 null,$data);
5418 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
5419 //Create returning info
5420 $ret_info->id = $mod_id;
5421 $ret_info->modtype = $mod_type;
5422 $this->info[] = $ret_info;
5424 //Reset temp
5425 unset($this->temp);
5431 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
5432 //Speed up a lot (avoid parse all)
5433 if ($tagName == "MODULES" and $this->level == 3) {
5434 $this->finished = true;
5437 //Clear things
5438 $this->tree[$this->level] = "";
5439 $this->level--;
5440 $this->content = "";
5444 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
5445 function endElementLogs($parser, $tagName) {
5446 //Check if we are into LOGS zone
5447 if ($this->tree[3] == "LOGS") {
5448 //if (trim($this->content)) //Debug
5449 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5450 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5451 //Acumulate data to info (content + close tag)
5452 //Reconvert: strip htmlchars again and trim to generate xml data
5453 if (!isset($this->temp)) {
5454 $this->temp = "";
5456 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5457 //If we've finished a log, xmlize it an save to db
5458 if (($this->level == 4) and ($tagName == "LOG")) {
5459 //Prepend XML standard header to info gathered
5460 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5461 //Call to xmlize for this portion of xml data (one LOG)
5462 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5463 $data = xmlize($xml_data,0);
5464 //echo strftime ("%X",time())."<p>"; //Debug
5465 //traverse_xmlize($data); //Debug
5466 //print_object ($GLOBALS['traverse_array']); //Debug
5467 //$GLOBALS['traverse_array']=""; //Debug
5468 //Now, save data to db. We'll use it later
5469 //Get id and modtype from data
5470 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
5471 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
5472 //We only save log entries from backup file if they are:
5473 // - Course logs
5474 // - User logs
5475 // - Module logs about one restored module
5476 if ($log_module == "course" or
5477 $log_module == "user" or
5478 $this->preferences->mods[$log_module]->restore) {
5479 //Increment counter
5480 $this->counter++;
5481 //Save to db
5482 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
5483 null,$data);
5484 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
5485 //Create returning info
5486 $this->info = $this->counter;
5488 //Reset temp
5489 unset($this->temp);
5493 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
5494 //Speed up a lot (avoid parse all)
5495 if ($tagName == "LOGS" and $this->level == 3) {
5496 $this->finished = true;
5497 $this->counter = 0;
5500 //Clear things
5501 $this->tree[$this->level] = "";
5502 $this->level--;
5503 $this->content = "";
5507 //This is the endTag default handler we use when todo is undefined
5508 function endElement($parser, $tagName) {
5509 if (trim($this->content)) //Debug
5510 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5511 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5513 //Clear things
5514 $this->tree[$this->level] = "";
5515 $this->level--;
5516 $this->content = "";
5519 //This is the handler to read data contents (simple accumule it)
5520 function characterData($parser, $data) {
5521 $this->content .= $data;
5525 //This function executes the MoodleParser
5526 function restore_read_xml ($xml_file,$todo,$preferences) {
5528 $status = true;
5530 $xml_parser = xml_parser_create('UTF-8');
5531 $moodle_parser = new MoodleParser();
5532 $moodle_parser->todo = $todo;
5533 $moodle_parser->preferences = $preferences;
5534 xml_set_object($xml_parser,$moodle_parser);
5535 //Depending of the todo we use some element_handler or another
5536 if ($todo == "INFO") {
5537 //Define handlers to that zone
5538 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
5539 } else if ($todo == "ROLES") {
5540 // Define handlers to that zone
5541 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
5542 } else if ($todo == "COURSE_HEADER") {
5543 //Define handlers to that zone
5544 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
5545 } else if ($todo == 'BLOCKS') {
5546 //Define handlers to that zone
5547 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
5548 } else if ($todo == "SECTIONS") {
5549 //Define handlers to that zone
5550 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
5551 } else if ($todo == 'FORMATDATA') {
5552 //Define handlers to that zone
5553 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
5554 } else if ($todo == "METACOURSE") {
5555 //Define handlers to that zone
5556 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
5557 } else if ($todo == "GRADEBOOK") {
5558 //Define handlers to that zone
5559 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
5560 } else if ($todo == "USERS") {
5561 //Define handlers to that zone
5562 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
5563 } else if ($todo == "MESSAGES") {
5564 //Define handlers to that zone
5565 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
5566 } else if ($todo == "QUESTIONS") {
5567 //Define handlers to that zone
5568 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
5569 } else if ($todo == "SCALES") {
5570 //Define handlers to that zone
5571 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
5572 } else if ($todo == "GROUPS") {
5573 //Define handlers to that zone
5574 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
5575 } else if ($todo == "GROUPINGS") {
5576 //Define handlers to that zone
5577 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
5578 } else if ($todo == "EVENTS") {
5579 //Define handlers to that zone
5580 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
5581 } else if ($todo == "MODULES") {
5582 //Define handlers to that zone
5583 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
5584 } else if ($todo == "LOGS") {
5585 //Define handlers to that zone
5586 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
5587 } else {
5588 //Define default handlers (must no be invoked when everything become finished)
5589 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
5591 xml_set_character_data_handler($xml_parser, "characterData");
5592 $fp = fopen($xml_file,"r")
5593 or $status = false;
5594 if ($status) {
5595 while ($data = fread($fp, 4096) and !$moodle_parser->finished)
5596 xml_parse($xml_parser, $data, feof($fp))
5597 or die(sprintf("XML error: %s at line %d",
5598 xml_error_string(xml_get_error_code($xml_parser)),
5599 xml_get_current_line_number($xml_parser)));
5600 fclose($fp);
5602 //Get info from parser
5603 $info = $moodle_parser->info;
5605 //Clear parser mem
5606 xml_parser_free($xml_parser);
5608 if ($status && !empty($info)) {
5609 return $info;
5610 } else {
5611 return $status;
5616 * @param string $errorstr passed by reference, if silent is true,
5617 * errorstr will be populated and this function will return false rather than calling error() or notify()
5618 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
5619 * redirect to the next step in the restore process, instead will return $backup_unique_code
5621 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
5623 global $CFG, $SESSION;
5625 //Prepend dataroot to variable to have the absolute path
5626 $file = $CFG->dataroot."/".$file;
5628 if (!defined('RESTORE_SILENTLY')) {
5629 //Start the main table
5630 echo "<table cellpadding=\"5\">";
5631 echo "<tr><td>";
5633 //Start the mail ul
5634 echo "<ul>";
5637 //Check the file exists
5638 if (!is_file($file)) {
5639 if (!defined('RESTORE_SILENTLY')) {
5640 error ("File not exists ($file)");
5641 } else {
5642 $errorstr = "File not exists ($file)";
5643 return false;
5647 //Check the file name ends with .zip
5648 if (!substr($file,-4) == ".zip") {
5649 if (!defined('RESTORE_SILENTLY')) {
5650 error ("File has an incorrect extension");
5651 } else {
5652 $errorstr = 'File has an incorrect extension';
5653 return false;
5657 //Now calculate the unique_code for this restore
5658 $backup_unique_code = time();
5660 //Now check and create the backup dir (if it doesn't exist)
5661 if (!defined('RESTORE_SILENTLY')) {
5662 echo "<li>".get_string("creatingtemporarystructures").'</li>';
5664 $status = check_and_create_backup_dir($backup_unique_code);
5665 //Empty dir
5666 if ($status) {
5667 $status = clear_backup_dir($backup_unique_code);
5670 //Now delete old data and directories under dataroot/temp/backup
5671 if ($status) {
5672 if (!defined('RESTORE_SILENTLY')) {
5673 echo "<li>".get_string("deletingolddata").'</li>';
5675 $status = backup_delete_old_data();
5678 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
5679 if ($status) {
5680 if (!defined('RESTORE_SILENTLY')) {
5681 echo "<li>".get_string("copyingzipfile").'</li>';
5683 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
5684 if (!defined('RESTORE_SILENTLY')) {
5685 notify("Error copying backup file. Invalid name or bad perms.");
5686 } else {
5687 $errorstr = "Error copying backup file. Invalid name or bad perms";
5688 return false;
5693 //Now unzip the file
5694 if ($status) {
5695 if (!defined('RESTORE_SILENTLY')) {
5696 echo "<li>".get_string("unzippingbackup").'</li>';
5698 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
5699 if (!defined('RESTORE_SILENTLY')) {
5700 notify("Error unzipping backup file. Invalid zip file.");
5701 } else {
5702 $errorstr = "Error unzipping backup file. Invalid zip file.";
5703 return false;
5708 //Check for Blackboard backups and convert
5709 if ($status){
5710 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
5711 if (!defined('RESTORE_SILENTLY')) {
5712 echo "<li>".get_string("checkingforbbexport").'</li>';
5714 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
5717 //Now check for the moodle.xml file
5718 if ($status) {
5719 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
5720 if (!defined('RESTORE_SILENTLY')) {
5721 echo "<li>".get_string("checkingbackup").'</li>';
5723 if (! $status = restore_check_moodle_file ($xml_file)) {
5724 if (!is_file($xml_file)) {
5725 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
5726 } else {
5727 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
5729 if (!defined('RESTORE_SILENTLY')) {
5730 notify($errorstr);
5731 } else {
5732 return false;
5737 $info = "";
5738 $course_header = "";
5740 //Now read the info tag (all)
5741 if ($status) {
5742 if (!defined('RESTORE_SILENTLY')) {
5743 echo "<li>".get_string("readinginfofrombackup").'</li>';
5745 //Reading info from file
5746 $info = restore_read_xml_info ($xml_file);
5747 //Reading course_header from file
5748 $course_header = restore_read_xml_course_header ($xml_file);
5751 if (!defined('RESTORE_SILENTLY')) {
5752 //End the main ul
5753 echo "</ul>\n";
5755 //End the main table
5756 echo "</td></tr>";
5757 echo "</table>";
5760 //We compare Moodle's versions
5761 if ($CFG->version < $info->backup_moodle_version && $status) {
5762 $message->serverversion = $CFG->version;
5763 $message->serverrelease = $CFG->release;
5764 $message->backupversion = $info->backup_moodle_version;
5765 $message->backuprelease = $info->backup_moodle_release;
5766 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
5770 //Now we print in other table, the backup and the course it contains info
5771 if ($info and $course_header and $status) {
5772 //First, the course info
5773 if (!defined('RESTORE_SILENTLY')) {
5774 $status = restore_print_course_header($course_header);
5776 //Now, the backup info
5777 if ($status) {
5778 if (!defined('RESTORE_SILENTLY')) {
5779 $status = restore_print_info($info);
5784 //Save course header and info into php session
5785 if ($status) {
5786 $SESSION->info = $info;
5787 $SESSION->course_header = $course_header;
5790 //Finally, a little form to continue
5791 //with some hidden fields
5792 if ($status) {
5793 if (!defined('RESTORE_SILENTLY')) {
5794 echo "<br /><div style='text-align:center'>";
5795 $hidden["backup_unique_code"] = $backup_unique_code;
5796 $hidden["launch"] = "form";
5797 $hidden["file"] = $file;
5798 $hidden["id"] = $id;
5799 print_single_button("restore.php", $hidden, get_string("continue"),"post");
5800 echo "</div>";
5802 else {
5803 if (empty($noredirect)) {
5804 redirect($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
5805 } else {
5806 return $backup_unique_code;
5811 if (!$status) {
5812 if (!defined('RESTORE_SILENTLY')) {
5813 error ("An error has ocurred");
5814 } else {
5815 $errorstr = "An error has occured"; // helpful! :P
5816 return false;
5819 return true;
5822 function restore_setup_for_check(&$restore,$backup_unique_code) {
5823 global $SESSION;
5824 $restore->backup_unique_code=$backup_unique_code;
5825 $restore->users = 2; // yuk
5826 $restore->course_files = $SESSION->restore->restore_course_files;
5827 if ($allmods = get_records("modules")) {
5828 foreach ($allmods as $mod) {
5829 $modname = $mod->name;
5830 $var = "restore_".$modname;
5831 //Now check that we have that module info in the backup file
5832 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
5833 $restore->$var = 1;
5837 return true;
5840 function backup_to_restore_array($backup,$k=0) {
5841 if (is_array($backup) ) {
5842 foreach ($backup as $key => $value) {
5843 $newkey = str_replace('backup','restore',$key);
5844 $restore[$newkey] = backup_to_restore_array($value,$key);
5847 else if (is_object($backup)) {
5848 $tmp = get_object_vars($backup);
5849 foreach ($tmp as $key => $value) {
5850 $newkey = str_replace('backup','restore',$key);
5851 $restore->$newkey = backup_to_restore_array($value,$key);
5854 else {
5855 $newkey = str_replace('backup','restore',$k);
5856 $restore = $backup;
5858 return $restore;
5861 /**
5862 * compatibility function
5863 * checks for per-instance backups AND
5864 * older per-module backups
5865 * and returns whether userdata has been selected.
5867 function restore_userdata_selected($restore,$modname,$modid) {
5868 // check first for per instance array
5869 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
5870 return array_key_exists($modid,$restore->mods[$modname]->instances)
5871 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
5874 print_object($restore->mods[$modname]);
5875 return !empty($restore->mods[$modname]->userinfo);
5878 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
5880 global $CFG, $USER;
5881 $status = true;
5883 //Checks for the required files/functions to restore every module
5884 //and include them
5885 if ($allmods = get_records("modules") ) {
5886 foreach ($allmods as $mod) {
5887 $modname = $mod->name;
5888 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
5889 //If file exists and we have selected to restore that type of module
5890 if ((file_exists($modfile)) and ($restore->mods[$modname]->restore)) {
5891 include_once($modfile);
5896 if (!defined('RESTORE_SILENTLY')) {
5897 //Start the main table
5898 echo "<table cellpadding=\"5\">";
5899 echo "<tr><td>";
5901 //Start the main ul
5902 echo "<ul>";
5905 //Localtion of the xml file
5906 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
5908 //If we've selected to restore into new course
5909 //create it (course)
5910 //Saving conversion id variables into backup_tables
5911 if ($restore->restoreto == 2) {
5912 if (!defined('RESTORE_SILENTLY')) {
5913 echo '<li>'.get_string('creatingnewcourse') . '</li>';
5915 $oldidnumber = $course_header->course_idnumber;
5916 if (!$status = restore_create_new_course($restore,$course_header)) {
5917 if (!defined('RESTORE_SILENTLY')) {
5918 notify("Error while creating the new empty course.");
5919 } else {
5920 $errorstr = "Error while creating the new empty course.";
5921 return false;
5925 //Print course fullname and shortname and category
5926 if ($status) {
5927 if (!defined('RESTORE_SILENTLY')) {
5928 echo "<ul>";
5929 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
5930 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
5931 if (!empty($oldidnumber)) {
5932 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
5934 echo "</ul>";
5935 //Put the destination course_id
5937 $restore->course_id = $course_header->course_id;
5940 if ($status = restore_open_html($restore,$course_header)){
5941 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
5944 } else {
5945 $course = get_record("course","id",$restore->course_id);
5946 if ($course) {
5947 if (!defined('RESTORE_SILENTLY')) {
5948 echo "<li>".get_string("usingexistingcourse");
5949 echo "<ul>";
5950 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
5951 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
5952 if (($restore->deleting)) {
5953 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
5954 } else {
5955 echo "<li>".get_string("addingdatatoexisting").'</li>';
5957 echo "</ul></li>";
5959 //If we have selected to restore deleting, we do it now.
5960 if ($restore->deleting) {
5961 if (!defined('RESTORE_SILENTLY')) {
5962 echo "<li>".get_string("deletingolddata").'</li>';
5964 $status = remove_course_contents($restore->course_id,false) and
5965 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
5966 if ($status) {
5967 //Now , this situation is equivalent to the "restore to new course" one (we
5968 //have a course record and nothing more), so define it as "to new course"
5969 $restore->restoreto = 2;
5970 } else {
5971 if (!defined('RESTORE_SILENTLY')) {
5972 notify("An error occurred while deleting some of the course contents.");
5973 } else {
5974 $errrostr = "An error occurred while deleting some of the course contents.";
5975 return false;
5979 } else {
5980 if (!defined('RESTORE_SILENTLY')) {
5981 notify("Error opening existing course.");
5982 $status = false;
5983 } else {
5984 $errorstr = "Error opening existing course.";
5985 return false;
5990 //Now create the course_sections and their associated course_modules
5991 if ($status) {
5992 //Into new course
5993 if ($restore->restoreto == 2) {
5994 if (!defined('RESTORE_SILENTLY')) {
5995 echo "<li>".get_string("creatingsections");
5997 if (!$status = restore_create_sections($restore,$xml_file)) {
5998 if (!defined('RESTORE_SILENTLY')) {
5999 notify("Error creating sections in the existing course.");
6000 } else {
6001 $errorstr = "Error creating sections in the existing course.";
6002 return false;
6005 if (!defined('RESTORE_SILENTLY')) {
6006 echo '</li>';
6008 //Into existing course
6009 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
6010 if (!defined('RESTORE_SILENTLY')) {
6011 echo "<li>".get_string("checkingsections");
6013 if (!$status = restore_create_sections($restore,$xml_file)) {
6014 if (!defined('RESTORE_SILENTLY')) {
6015 notify("Error creating sections in the existing course.");
6016 } else {
6017 $errorstr = "Error creating sections in the existing course.";
6018 return false;
6021 if (!defined('RESTORE_SILENTLY')) {
6022 echo '</li>';
6024 //Error
6025 } else {
6026 if (!defined('RESTORE_SILENTLY')) {
6027 notify("Neither a new course or an existing one was specified.");
6028 $status = false;
6029 } else {
6030 $errorstr = "Neither a new course or an existing one was specified.";
6031 return false;
6036 //Now create users as needed
6037 if ($status and ($restore->users == 0 or $restore->users == 1)) {
6038 if (!defined('RESTORE_SILENTLY')) {
6039 echo "<li>".get_string("creatingusers")."<br />";
6041 if (!$status = restore_create_users($restore,$xml_file)) {
6042 if (!defined('RESTORE_SILENTLY')) {
6043 notify("Could not restore users.");
6044 } else {
6045 $errorstr = "Could not restore users.";
6046 return false;
6050 //Now print info about the work done
6051 if ($status) {
6052 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
6053 where backup_code = '$restore->backup_unique_code' and
6054 table_name = 'user'");
6055 //We've records
6056 if ($recs) {
6057 $new_count = 0;
6058 $exists_count = 0;
6059 $student_count = 0;
6060 $teacher_count = 0;
6061 $counter = 0;
6062 //Iterate, filling counters
6063 foreach ($recs as $rec) {
6064 //Get full record, using backup_getids
6065 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
6066 if (strpos($record->info,"new") !== false) {
6067 $new_count++;
6069 if (strpos($record->info,"exists") !== false) {
6070 $exists_count++;
6072 if (strpos($record->info,"student") !== false) {
6073 $student_count++;
6074 } else if (strpos($record->info,"teacher") !== false) {
6075 $teacher_count++;
6077 //Do some output
6078 $counter++;
6079 if ($counter % 10 == 0) {
6080 if (!defined('RESTORE_SILENTLY')) {
6081 echo ".";
6082 if ($counter % 200 == 0) {
6083 echo "<br />";
6086 backup_flush(300);
6089 if (!defined('RESTORE_SILENTLY')) {
6090 //Now print information gathered
6091 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
6092 echo "<ul>";
6093 echo "<li>".get_string("students").": ".$student_count.'</li>';
6094 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
6095 echo "</ul>";
6097 } else {
6098 if (!defined('RESTORE_SILENTLY')) {
6099 notify("No users were found!");
6100 } // no need to return false here, it's recoverable.
6104 if (!defined('RESTORE_SILENTLY')) {
6105 echo "</li>";
6109 //Now create metacourse info
6110 if ($status and $restore->metacourse) {
6111 //Only to new courses!
6112 if ($restore->restoreto == 2) {
6113 if (!defined('RESTORE_SILENTLY')) {
6114 echo "<li>".get_string("creatingmetacoursedata");
6116 if (!$status = restore_create_metacourse($restore,$xml_file)) {
6117 if (!defined('RESTORE_SILENTLY')) {
6118 notify("Error creating metacourse in the course.");
6119 } else {
6120 $errorstr = "Error creating metacourse in the course.";
6121 return false;
6124 if (!defined('RESTORE_SILENTLY')) {
6125 echo '</li>';
6131 //Now create categories and questions as needed
6132 if ($status and ($restore->mods['quiz']->restore)) {
6133 include_once("$CFG->dirroot/question/restorelib.php");
6134 if (!defined('RESTORE_SILENTLY')) {
6135 echo "<li>".get_string("creatingcategoriesandquestions");
6136 echo "<ul>";
6138 if (!$status = restore_create_questions($restore,$xml_file)) {
6139 if (!defined('RESTORE_SILENTLY')) {
6140 notify("Could not restore categories and questions!");
6141 } else {
6142 $errorstr = "Could not restore categories and questions!";
6143 return false;
6146 if (!defined('RESTORE_SILENTLY')) {
6147 echo "</ul></li>";
6151 //Now create user_files as needed
6152 if ($status and ($restore->user_files)) {
6153 if (!defined('RESTORE_SILENTLY')) {
6154 echo "<li>".get_string("copyinguserfiles");
6156 if (!$status = restore_user_files($restore)) {
6157 if (!defined('RESTORE_SILENTLY')) {
6158 notify("Could not restore user files!");
6159 } else {
6160 $errorstr = "Could not restore user files!";
6161 return false;
6164 //If all is ok (and we have a counter)
6165 if ($status and ($status !== true)) {
6166 //Inform about user dirs created from backup
6167 if (!defined('RESTORE_SILENTLY')) {
6168 echo "<ul>";
6169 echo "<li>".get_string("userzones").": ".$status;
6170 echo "</li></ul>";
6173 if (!defined('RESTORE_SILENTLY')) {
6174 echo '</li>';
6178 //Now create course files as needed
6179 if ($status and ($restore->course_files)) {
6180 if (!defined('RESTORE_SILENTLY')) {
6181 echo "<li>".get_string("copyingcoursefiles");
6183 if (!$status = restore_course_files($restore)) {
6184 if (empty($status)) {
6185 notify("Could not restore course files!");
6186 } else {
6187 $errorstr = "Could not restore course files!";
6188 return false;
6191 //If all is ok (and we have a counter)
6192 if ($status and ($status !== true)) {
6193 //Inform about user dirs created from backup
6194 if (!defined('RESTORE_SILENTLY')) {
6195 echo "<ul>";
6196 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
6197 echo "</ul>";
6200 if (!defined('RESTORE_SILENTLY')) {
6201 echo "</li>";
6205 //Now create messages as needed
6206 if ($status and ($restore->messages)) {
6207 if (!defined('RESTORE_SILENTLY')) {
6208 echo "<li>".get_string("creatingmessagesinfo");
6210 if (!$status = restore_create_messages($restore,$xml_file)) {
6211 if (!defined('RESTORE_SILENTLY')) {
6212 notify("Could not restore messages!");
6213 } else {
6214 $errorstr = "Could not restore messages!";
6215 return false;
6218 if (!defined('RESTORE_SILENTLY')) {
6219 echo "</li>";
6223 //Now create scales as needed
6224 if ($status) {
6225 if (!defined('RESTORE_SILENTLY')) {
6226 echo "<li>".get_string("creatingscales");
6228 if (!$status = restore_create_scales($restore,$xml_file)) {
6229 if (!defined('RESTORE_SILENTLY')) {
6230 notify("Could not restore custom scales!");
6231 } else {
6232 $errorstr = "Could not restore custom scales!";
6233 return false;
6236 if (!defined('RESTORE_SILENTLY')) {
6237 echo '</li>';
6241 //Now create groups as needed
6242 if ($status) {
6243 if (!defined('RESTORE_SILENTLY')) {
6244 echo "<li>".get_string("creatinggroups");
6246 if (!$status = restore_create_groups($restore,$xml_file)) {
6247 if (!defined('RESTORE_SILENTLY')) {
6248 notify("Could not restore groups!");
6249 } else {
6250 $errorstr = "Could not restore groups!";
6251 return false;
6254 if (!defined('RESTORE_SILENTLY')) {
6255 echo '</li>';
6259 //Now create groupings as needed
6260 if ($status) {
6261 if (!defined('RESTORE_SILENTLY')) {
6262 echo "<li>".get_string("creatinggroupings");
6264 if (!$status = restore_create_groupings($restore,$xml_file)) {
6265 if (!defined('RESTORE_SILENTLY')) {
6266 notify("Could not restore groupings!");
6267 } else {
6268 $errorstr = "Could not restore groupings!";
6269 return false;
6272 if (!defined('RESTORE_SILENTLY')) {
6273 echo '</li>';
6277 //Now create events as needed
6278 if ($status) {
6279 if (!defined('RESTORE_SILENTLY')) {
6280 echo "<li>".get_string("creatingevents");
6282 if (!$status = restore_create_events($restore,$xml_file)) {
6283 if (!defined('RESTORE_SILENTLY')) {
6284 notify("Could not restore course events!");
6285 } else {
6286 $errorstr = "Could not restore course events!";
6287 return false;
6290 if (!defined('RESTORE_SILENTLY')) {
6291 echo '</li>';
6295 //Now create course modules as needed
6296 if ($status) {
6297 if (!defined('RESTORE_SILENTLY')) {
6298 echo "<li>".get_string("creatingcoursemodules");
6300 if (!$status = restore_create_modules($restore,$xml_file)) {
6301 if (!defined('RESTORE_SILENTLY')) {
6302 notify("Could not restore modules!");
6303 } else {
6304 $errorstr = "Could not restore modules!";
6305 return false;
6308 if (!defined('RESTORE_SILENTLY')) {
6309 echo '</li>';
6313 //Now create gradebook as needed -- AFTER modules!!!
6314 if ($status) {
6315 if (!defined('RESTORE_SILENTLY')) {
6316 echo "<li>".get_string("creatinggradebook");
6318 if (!$status = restore_create_gradebook($restore,$xml_file)) {
6319 if (!defined('RESTORE_SILENTLY')) {
6320 notify("Could not restore gradebook!");
6321 } else {
6322 $errorstr = "Could not restore gradebook!";
6323 return false;
6326 if (!defined('RESTORE_SILENTLY')) {
6327 echo '</li>';
6331 //Bring back the course blocks -- do it AFTER the modules!!!
6332 if($status) {
6333 //If we are deleting and bringing into a course or making a new course, same situation
6334 if($restore->restoreto == 0 || $restore->restoreto == 2) {
6335 if (!defined('RESTORE_SILENTLY')) {
6336 echo '<li>'.get_string('creatingblocks');
6338 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
6339 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
6340 if (!defined('RESTORE_SILENTLY')) {
6341 notify('Error while creating the course blocks');
6342 } else {
6343 $errorstr = "Error while creating the course blocks";
6344 return false;
6347 if (!defined('RESTORE_SILENTLY')) {
6348 echo '</li>';
6353 if($status) {
6354 //If we are deleting and bringing into a course or making a new course, same situation
6355 if($restore->restoreto == 0 || $restore->restoreto == 2) {
6356 if (!defined('RESTORE_SILENTLY')) {
6357 echo '<li>'.get_string('courseformatdata');
6359 if (!$status = restore_set_format_data($restore, $xml_file)) {
6360 $error = "Error while setting the course format data";
6361 if (!defined('RESTORE_SILENTLY')) {
6362 notify($error);
6363 } else {
6364 $errorstr=$error;
6365 return false;
6368 if (!defined('RESTORE_SILENTLY')) {
6369 echo '</li>';
6374 //Now create log entries as needed
6375 if ($status and ($restore->logs)) {
6376 if (!defined('RESTORE_SILENTLY')) {
6377 echo "<li>".get_string("creatinglogentries");
6379 if (!$status = restore_create_logs($restore,$xml_file)) {
6380 if (!defined('RESTORE_SILENTLY')) {
6381 notify("Could not restore logs!");
6382 } else {
6383 $errorstr = "Could not restore logs!";
6384 return false;
6387 if (!defined('RESTORE_SILENTLY')) {
6388 echo '</li>';
6392 //Now, if all is OK, adjust the instance field in course_modules !!
6393 if ($status) {
6394 if (!defined('RESTORE_SILENTLY')) {
6395 echo "<li>".get_string("checkinginstances");
6397 if (!$status = restore_check_instances($restore)) {
6398 if (!defined('RESTORE_SILENTLY')) {
6399 notify("Could not adjust instances in course_modules!");
6400 } else {
6401 $errorstr = "Could not adjust instances in course_modules!";
6402 return false;
6405 if (!defined('RESTORE_SILENTLY')) {
6406 echo '</li>';
6410 //Now, if all is OK, adjust activity events
6411 if ($status) {
6412 if (!defined('RESTORE_SILENTLY')) {
6413 echo "<li>".get_string("refreshingevents");
6415 if (!$status = restore_refresh_events($restore)) {
6416 if (!defined('RESTORE_SILENTLY')) {
6417 notify("Could not refresh events for activities!");
6418 } else {
6419 $errorstr = "Could not refresh events for activities!";
6420 return false;
6423 if (!defined('RESTORE_SILENTLY')) {
6424 echo '</li>';
6428 //Now, if all is OK, adjust inter-activity links
6429 if ($status) {
6430 if (!defined('RESTORE_SILENTLY')) {
6431 echo "<li>".get_string("decodinginternallinks");
6433 if (!$status = restore_decode_content_links($restore)) {
6434 if (!defined('RESTORE_SILENTLY')) {
6435 notify("Could not decode content links!");
6436 } else {
6437 $errorstr = "Could not decode content links!";
6438 return false;
6441 if (!defined('RESTORE_SILENTLY')) {
6442 echo '</li>';
6446 //Now, with backup files prior to version 2005041100,
6447 //convert all the wiki texts in the course to markdown
6448 if ($status && $restore->backup_version < 2005041100) {
6449 if (!defined('RESTORE_SILENTLY')) {
6450 echo "<li>".get_string("convertingwikitomarkdown");
6452 if (!$status = restore_convert_wiki2markdown($restore)) {
6453 if (!defined('RESTORE_SILENTLY')) {
6454 notify("Could not convert wiki texts to markdown!");
6455 } else {
6456 $errorstr = "Could not convert wiki texts to markdown!";
6457 return false;
6460 if (!defined('RESTORE_SILENTLY')) {
6461 echo '</li>';
6465 /*******************************************************************************
6466 ************* Restore of Roles and Capabilities happens here ******************
6467 *******************************************************************************/
6468 $status = restore_create_roles($restore, $xml_file);
6469 $status = restore_roles_settings($restore, $xml_file);
6471 //Now if all is OK, update:
6472 // - course modinfo field
6473 // - categories table
6474 // - add user as teacher
6475 if ($status) {
6476 if (!defined('RESTORE_SILENTLY')) {
6477 echo "<li>".get_string("checkingcourse");
6479 //modinfo field
6480 rebuild_course_cache($restore->course_id);
6481 //categories table
6482 $course = get_record("course","id",$restore->course_id);
6483 fix_course_sortorder();
6484 // Check if the user has course update capability in the newly restored course
6485 // there is no need to load his capabilities again, because restore_roles_settings
6486 // would have loaded it anyway, if there is any assignments.
6487 // fix for MDL-6831
6488 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
6489 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
6490 // fix for MDL-9065, use the new config setting if exists
6491 if ($CFG->creatornewroleid) {
6492 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
6493 } else {
6494 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID))) {
6495 if ($legacyteacher = array_shift($legacyteachers)) {
6496 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
6498 } else {
6499 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
6503 if (!defined('RESTORE_SILENTLY')) {
6504 echo '</li>';
6508 //Cleanup temps (files and db)
6509 if ($status) {
6510 if (!defined('RESTORE_SILENTLY')) {
6511 echo "<li>".get_string("cleaningtempdata");
6513 if (!$status = clean_temp_data ($restore)) {
6514 if (!defined('RESTORE_SILENTLY')) {
6515 notify("Could not clean up temporary data from files and database");
6516 } else {
6517 $errorstr = "Could not clean up temporary data from files and database";
6518 return false;
6521 if (!defined('RESTORE_SILENTLY')) {
6522 echo '</li>';
6526 if ($status = restore_close_html($restore)){
6527 if (!defined('RESTORE_SILENTLY')) {
6528 echo '<li>Closing the Restorelog.html file.</li>';
6531 else {
6532 if (!defined('RESTORE_SILENTLY')) {
6533 notify("Could not close the restorelog.html file");
6535 else {
6536 $errorstr = "Could not close the restorelog.html file";
6537 return false;
6541 if (!defined('RESTORE_SILENTLY')) {
6542 //End the main ul
6543 echo "</ul>";
6545 //End the main table
6546 echo "</td></tr>";
6547 echo "</table>";
6550 return $status;
6552 //Create, open and write header of the html log file
6553 function restore_open_html($restore,$course_header) {
6555 global $CFG;
6557 $status = true;
6559 //Open file for writing
6560 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
6561 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
6562 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
6564 $status = check_dir_exists($dest_dir,true);
6565 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
6566 //Add the stylesheet
6567 $stylesheetshtml = '';
6568 foreach ($CFG->stylesheets as $stylesheet) {
6569 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
6571 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
6572 $languagehtml = get_html_lang($dir=true);
6574 //Write the header in the new logging file
6575 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
6576 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
6577 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
6578 fwrite ($restorelog_file,"<head>");
6579 fwrite ($restorelog_file,$stylesheetshtml);
6580 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
6581 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
6582 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
6583 $startdate = addslashes($course_header->course_startdate);
6584 $date = usergetdate($startdate);
6585 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
6586 $startdate += $restore->course_startdateoffset;
6587 $date = usergetdate($startdate);
6588 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
6590 if ($status) {
6591 return $restorelog_file;
6592 } else {
6593 return false;
6596 //Create & close footer of the html log file
6597 function restore_close_html($restore) {
6599 global $CFG;
6601 $status = true;
6603 //Open file for writing
6604 //First, check that course_id/backupdata folder exists in CFG->dataroot
6605 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
6606 $status = check_dir_exists($dest_dir, true, true);
6607 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
6608 //Write the footer to close the logging file
6609 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
6610 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
6612 if ($status) {
6613 return $restorelog_file;
6614 } else {
6615 return false;
6619 /********************** Roles and Capabilities Related Functions *******************************/
6621 /* Yu: Note recovering of role assignments/overrides need to take place after
6622 users have been recovered, i.e. after we get their new_id, and after all
6623 roles have been recreated or mapped. Contexts can be created on the fly.
6624 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
6625 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
6626 once roles and users have been restored.
6630 * This function restores all the needed roles for this course
6631 * i.e. roles with an assignment in any of the mods or blocks,
6632 * roles assigned on any user (e.g. parent role) and roles
6633 * assigned at course levle
6634 * This function should check for duplicate roles first
6635 * It isn't now, just overwriting
6637 function restore_create_roles($restore, $xmlfile) {
6638 if (!defined('RESTORE_SILENTLY')) {
6639 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
6641 $info = restore_read_xml_roles($xmlfile);
6643 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
6645 // the following code creates new roles
6646 // but we could use more intelligent detection, and role mapping
6647 // get role mapping info from $restore
6648 $rolemappings = array();
6650 if (!empty($restore->rolesmapping)) {
6651 $rolemappings = $restore->rolesmapping;
6653 // $info->roles will be empty for backups pre 1.7
6654 if (isset($info->roles) && $info->roles) {
6656 foreach ($info->roles as $oldroleid=>$roledata) {
6657 if (empty($restore->rolesmapping)) {
6658 // if this is empty altogether, we came from import or there's no roles used in course at all
6659 // in this case, write the same oldid as this is the same site
6660 // no need to do mapping
6661 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
6662 $oldroleid); // adding a new id
6663 continue; // do not create additonal roles;
6665 // first we check if the roles are in the mappings
6666 // if so, we just do a mapping i.e. update oldids table
6667 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
6668 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
6669 $rolemappings[$oldroleid]); // adding a new id
6671 } else {
6673 // code to make new role name/short name if same role name or shortname exists
6674 $fullname = $roledata->name;
6675 $shortname = $roledata->shortname;
6676 $currentfullname = "";
6677 $currentshortname = "";
6678 $counter = 0;
6680 do {
6681 if ($counter) {
6682 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
6683 $suffixshort = "_".$counter;
6684 } else {
6685 $suffixfull = "";
6686 $suffixshort = "";
6688 $currentfullname = $fullname.$suffixfull;
6689 // Limit the size of shortname - database column accepts <= 15 chars
6690 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
6691 $coursefull = get_record("role","name",addslashes($currentfullname));
6692 $courseshort = get_record("role","shortname",addslashes($currentshortname));
6693 $counter++;
6694 } while ($coursefull || $courseshort);
6696 $roledata->name = $currentfullname;
6697 $roledata->shortname= $currentshortname;
6699 // done finding a unique name
6701 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
6702 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
6703 $newroleid); // adding a new id
6704 foreach ($roledata->capabilities as $capability) {
6706 $roleinfo = new object();
6707 $roleinfo = (object)$capability;
6708 $roleinfo->contextid = $sitecontext->id;
6709 $roleinfo->capability = $capability->name;
6710 $roleinfo->roleid = $newroleid;
6712 insert_record('role_capabilities', $roleinfo);
6717 return true;
6721 * this function restores role assignments and role overrides
6722 * in course/user/block/mod level, it passed through
6723 * the xml file again
6725 function restore_roles_settings($restore, $xmlfile) {
6726 // data pulls from course, mod, user, and blocks
6728 /*******************************************************
6729 * Restoring from course level assignments *
6730 *******************************************************/
6731 if (!defined('RESTORE_SILENTLY')) {
6732 echo "<li>".get_string("creatingcourseroles").'</li>';
6734 $course = restore_read_xml_course_header($xmlfile);
6736 if (!isset($restore->rolesmapping)) {
6737 $isimport = true; // course import from another course, or course with no role assignments
6738 } else {
6739 $isimport = false; // course restore with role assignments
6742 if (!empty($course->roleassignments) && !$isimport) {
6743 $courseassignments = $course->roleassignments;
6745 foreach ($courseassignments as $oldroleid => $courseassignment) {
6746 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
6749 /*****************************************************
6750 * Restoring from course level overrides *
6751 *****************************************************/
6753 if (!empty($course->roleoverrides) && !$isimport) {
6754 $courseoverrides = $course->roleoverrides;
6755 foreach ($courseoverrides as $oldroleid => $courseoverride) {
6756 // if not importing into exiting course, or creating new role, we are ok
6757 // local course overrides to be respected (i.e. restored course overrides ignored)
6758 if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
6759 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
6764 /*******************************************************
6765 * Restoring role assignments/overrdies *
6766 * from module level assignments *
6767 *******************************************************/
6769 if (!defined('RESTORE_SILENTLY')) {
6770 echo "<li>".get_string("creatingmodroles").'</li>';
6772 $sections = restore_read_xml_sections($xmlfile);
6773 $secs = $sections->sections;
6775 foreach ($secs as $section) {
6776 if (isset($section->mods)) {
6777 foreach ($section->mods as $modid=>$mod) {
6778 if (isset($mod->roleassignments) && !$isimport) {
6779 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
6780 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
6783 // role overrides always applies, in import or backup/restore
6784 if (isset($mod->roleoverrides)) {
6785 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
6786 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
6793 /*************************************************
6794 * Restoring assignments from blocks level *
6795 * role assignments/overrides *
6796 *************************************************/
6798 if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
6799 if (!defined('RESTORE_SILENTLY')) {
6800 echo "<li>".get_string("creatingblocksroles").'</li>';
6802 $blocks = restore_read_xml_blocks($xmlfile);
6803 if (isset($blocks->instances)) {
6804 foreach ($blocks->instances as $instance) {
6805 if (isset($instance->roleassignments) && !$isimport) {
6806 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
6807 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
6811 // likewise block overrides should always be restored like mods
6812 if (isset($instance->roleoverrides)) {
6813 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
6814 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
6820 /************************************************
6821 * Restoring assignments from userid level *
6822 * role assignments/overrides *
6823 ************************************************/
6824 if (!defined('RESTORE_SILENTLY')) {
6825 echo "<li>".get_string("creatinguserroles").'</li>';
6827 $info = restore_read_xml_users($restore, $xmlfile);
6828 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
6829 //For each user, take its info from backup_ids
6830 foreach ($info->users as $userid) {
6831 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
6832 if (isset($rec->info->roleassignments)) {
6833 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
6834 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
6837 if (isset($rec->info->roleoverrides)) {
6838 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
6839 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
6845 return true;
6848 // auxillary function to write role assignments read from xml to db
6849 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
6851 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
6853 foreach ($assignments as $assignment) {
6855 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
6856 //Oh dear, $olduser... can be an object, $obj->string or bool!
6857 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
6858 continue;
6860 $assignment->userid = $olduser->new_id; // new userid here
6861 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
6862 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
6863 $assignment->roleid = $role->new_id; // restored new role id
6865 // hack to make the correct contextid for course level imports
6866 if ($contextlevel == CONTEXT_COURSE) {
6867 $oldinstance->new_id = $restore->course_id;
6868 } else {
6869 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
6872 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
6873 $assignment->contextid = $newcontext->id; // new context id
6874 // might already have same assignment
6875 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol, $assignment->timemodified);
6880 // auxillary function to write role assignments read from xml to db
6881 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
6883 // it is possible to have an override not relevant to this course context.
6884 // should be ignored(?)
6885 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
6886 return null;
6889 foreach ($overrides as $override) {
6890 $override->capability = $override->name;
6891 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
6892 $override->modifierid = $oldmodifier->new_id?$oldmodifier->new_id:0; // new modifier id here
6893 $override->roleid = $role->new_id; // restored new role id
6895 // hack to make the correct contextid for course level imports
6896 if ($contextlevel == CONTEXT_COURSE) {
6897 $oldinstance->new_id = $restore->course_id;
6898 } else {
6899 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
6902 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
6903 $override->contextid = $newcontext->id; // new context id
6904 // might already have same override
6905 if (!get_record('role_capabilities', 'capability', $override->capability, 'roleid', $override->roleid, 'contextid', $override->contextid)) {
6906 insert_record('role_capabilities', $override);
6910 //write activity date changes to the html log file, and update date values in the the xml array
6911 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
6913 global $CFG;
6914 $openlog = false;
6916 // loop through time fields in $TAGS
6917 foreach ($TAGS as $TAG) {
6919 // check $TAG has a sensible value
6920 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
6922 if ($openlog==false) {
6923 $openlog = true; // only come through here once
6925 // open file for writing
6926 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
6927 check_dir_exists($course_dir, true);
6928 $restorelog = fopen("$course_dir/restorelog.html", "a");
6930 // start output for this record
6931 $msg = new stdClass();
6932 $msg->recordtype = $recordtype;
6933 $msg->recordname = $xml[$NAMETAG][0]['#'];
6934 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
6937 // write old date to $restorelog
6938 $value = $xml[$TAG][0]['#'];
6939 $date = usergetdate($value);
6941 $msg = new stdClass();
6942 $msg->TAG = $TAG;
6943 $msg->weekday = $date['weekday'];
6944 $msg->mday = $date['mday'];
6945 $msg->month = $date['month'];
6946 $msg->year = $date['year'];
6947 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
6949 // write new date to $restorelog
6950 $value += $restore->course_startdateoffset;
6951 $date = usergetdate($value);
6953 $msg = new stdClass();
6954 $msg->TAG = $TAG;
6955 $msg->weekday = $date['weekday'];
6956 $msg->mday = $date['mday'];
6957 $msg->month = $date['month'];
6958 $msg->year = $date['year'];
6959 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
6961 // update $value in $xml tree for calling module
6962 $xml[$TAG][0]['#'] = "$value";
6965 // close the restore log, if it was opened
6966 if ($openlog) {
6967 fclose($restorelog);