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) {
12 if (empty($CFG->unzip)) { // Use built-in php-based unzip function
13 include_once("$CFG->dirroot/lib/pclzip/pclzip.lib.php");
14 //include_once("$CFG->dirroot/lib/pclzip/pclerror.lib.php"); //Debug
15 //include_once("$CFG->dirroot/lib/pclzip/pcltrace.lib.php"); //Debug
16 //PclTraceOn(2); //Debug
17 $archive = new PclZip($file);
18 if (!$list = $archive->extract(dirname($file))) {
21 //PclTraceDisplay(); //Debug
22 //PclTraceOff(); //Debug
23 } else { // Use external unzip program
24 $command = "cd ".dirname($file)."; $CFG->unzip -o ".basename($file);
31 //This function checks if moodle.xml seems to be a valid xml file
32 //(exists, has an xml header and a course main tag
33 function restore_check_moodle_file ($file) {
38 if ($status = is_file($file)) {
39 //Open it and read the first 200 bytes (chars)
40 $handle = fopen ($file, "r");
41 $first_chars = fread($handle,200);
42 $status = fclose ($handle);
43 //Chek if it has the requires strings
45 $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
46 if ($status !== false) {
47 $status = strpos($first_chars,"<MOODLE_BACKUP>");
55 //This function iterates over all modules in backup file, searching for a
56 //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
57 function restore_refresh_events($restore) {
62 //Take all modules in backup
63 $modules = $restore->mods;
65 foreach($modules as $name => $module) {
66 //Only if the module is being restored
67 if ($module->restore == 1) {
68 //Include module library
69 include_once("$CFG->dirroot/mod/$name/lib.php");
70 //If module_refresh_events exists
71 $function_name = $name."_refresh_events";
72 if (function_exists($function_name)) {
73 $status = $function_name($restore->course_id);
80 //This function makes all the necessary calls to xxxx_decode_content_links_caller()
81 //function in each module, passing them the desired contents to be decoded
82 //from backup format to destination site/course in order to mantain inter-activities
83 //working in the backup/restore process
84 function restore_decode_content_links($restore) {
91 foreach ($restore->mods as $name => $info) {
92 //If the module is being restored
93 if ($info->restore == 1) {
94 //Check if the xxxx_decode_content_links_caller exists
95 $function_name = $name."_decode_content_links_caller";
96 if (function_exists($function_name)) {
97 echo "<li>".get_string ("to")." ".get_string("modulenameplural",$name);
98 $status = $function_name($restore);
103 //Now I'm going to decode to their new location all the links in wiki texts
104 //having the syntax " modulename:moduleid".
106 $status = restore_decode_wiki_texts($restore);
113 //This function search for some wiki texts in differenct parts of Moodle to
114 //decode them to their new ids.
115 function restore_decode_wiki_texts($restore) {
123 if (file_exists("$CFG->dirroot/mod/resource/lib.php")) {
124 include_once("$CFG->dirroot/mod/resource/lib.php");
127 $formatwiki = FORMAT_WIKI;
128 $typewiki = WIKITEXT;
130 //FORUM: Decode every POST (message) in the course
131 //Check we are restoring forums
132 if ($restore->mods['forum']->restore == 1) {
133 echo "<li>".get_string("from")." ".get_string("modulenameplural","forum");
134 //Get all course posts
135 if ($posts = get_records_sql ("SELECT p.id, p.message
136 FROM {$CFG->prefix}forum_posts p,
137 {$CFG->prefix}forum_discussions d
138 WHERE d.course = $restore->course_id AND
139 p.discussion = d.id AND
140 p.format = $formatwiki")) {
141 //Iterate over each post->message
142 $i = 0; //Counter to send some output to the browser to avoid timeouts
143 foreach ($posts as $post) {
146 $content = $post->message;
148 $result = restore_decode_wiki_content($content,$restore);
150 if ($result != $content) {
152 $post->message = addslashes($result);
153 $status = update_record("forum_posts",$post);
155 echo "<br><hr>".$content."<br>changed to</br>".$result."<hr><br>";
159 if (($i+1) % 5 == 0) {
161 if (($i+1) % 100 == 0) {
170 //RESOURCE: Decode every RESOURCE (alltext) in the coure
172 //Check we are restoring resources
173 if ($restore->mods['resource']->restore == 1) {
174 echo "<li>".get_string("from")." ".get_string("modulenameplural","resource");
175 //Get all course resources of type=8 WIKITEXT
176 if ($resources = get_records_sql ("SELECT r.id, r.alltext
177 FROM {$CFG->prefix}resource r
178 WHERE r.course = $restore->course_id AND
179 r.type = $typewiki")) {
180 //Iterate over each resource->alltext
181 $i = 0; //Counter to send some output to the browser to avoid timeouts
182 foreach ($resources as $resource) {
185 $content = $resource->alltext;
187 $result = restore_decode_wiki_content($content,$restore);
188 if ($result != $content) {
190 $resource->alltext = addslashes($result);
191 $status = update_record("resource",$resource);
193 echo "<br><hr>".$content."<br>changed to</br>".$result."<hr><br>";
197 if (($i+1) % 5 == 0) {
199 if (($i+1) % 100 == 0) {
213 //This function receives a wiki text in the restore process and
214 //return it with every link to modules " modulename:moduleid"
215 //converted if possible. See the space before modulename!!
216 function restore_decode_wiki_content($content,$restore) {
222 $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
224 preg_match_all($searchstring,$content,$foundset);
225 //If found, then we are going to look for its new id (in backup tables)
227 //print_object($foundset); //Debug
228 //Iterate over foundset[2]. They are the old_ids
229 foreach($foundset[2] as $old_id) {
230 //We get the needed variables here (course id)
231 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
232 //Personalize the searchstring
233 $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
234 //If it is a link to this course, update the link to its new location
237 $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
239 //It's a foreign link so redirect it to its original URL
240 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
248 //This function read the xml file and store it data from the info zone in an object
249 function restore_read_xml_info ($xml_file) {
251 //We call the main read_xml function, with todo = INFO
252 $info = restore_read_xml ($xml_file,"INFO",false);
257 //This function read the xml file and store it data from the course header zone in an object
258 function restore_read_xml_course_header ($xml_file) {
260 //We call the main read_xml function, with todo = COURSE_HEADER
261 $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
266 //This function read the xml file and store its data from the sections in a object
267 function restore_read_xml_sections ($xml_file) {
269 //We call the main read_xml function, with todo = SECTIONS
270 $info = restore_read_xml ($xml_file,"SECTIONS",false);
275 //This function read the xml file and store its data from the users in
276 //backup_ids->info db (and user's id in $info)
277 function restore_read_xml_users ($restore,$xml_file) {
279 //We call the main read_xml function, with todo = USERS
280 $info = restore_read_xml ($xml_file,"USERS",$restore);
285 //This function read the xml file and store its data from the questions in
286 //backup_ids->info db (and category's id in $info)
287 function restore_read_xml_questions ($restore,$xml_file) {
289 //We call the main read_xml function, with todo = QUESTIONS
290 $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
295 //This function read the xml file and store its data from the scales in
296 //backup_ids->info db (and scale's id in $info)
297 function restore_read_xml_scales ($restore,$xml_file) {
299 //We call the main read_xml function, with todo = SCALES
300 $info = restore_read_xml ($xml_file,"SCALES",$restore);
305 //This function read the xml file and store its data from the groups in
306 //backup_ids->info db (and group's id in $info)
307 function restore_read_xml_groups ($restore,$xml_file) {
309 //We call the main read_xml function, with todo = GROUPS
310 $info = restore_read_xml ($xml_file,"GROUPS",$restore);
315 //This function read the xml file and store its data from the events (course) in
316 //backup_ids->info db (and event's id in $info)
317 function restore_read_xml_events ($restore,$xml_file) {
319 //We call the main read_xml function, with todo = EVENTS
320 $info = restore_read_xml ($xml_file,"EVENTS",$restore);
325 //This function read the xml file and store its data from the modules in
327 function restore_read_xml_modules ($restore,$xml_file) {
329 //We call the main read_xml function, with todo = MODULES
330 $info = restore_read_xml ($xml_file,"MODULES",$restore);
335 //This function read the xml file and store its data from the logs in
337 function restore_read_xml_logs ($restore,$xml_file) {
339 //We call the main read_xml function, with todo = LOGS
340 $info = restore_read_xml ($xml_file,"LOGS",$restore);
345 //This function prints the contents from the info parammeter passed
346 function restore_print_info ($info) {
350 //This is tha align to every ingo table
351 $table->align = array ("RIGHT","LEFT");
352 //This is the nowrap clause
353 $table->wrap = array ("","NOWRAP");
355 $table->width = "70%";
356 //Put interesting info in table
357 //The backup original name
358 $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
359 $tab[0][1] = $info->backup_name;
361 $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
362 $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
364 $tab[2][0] = "<b>".get_string("backupversion").":</b>";
365 $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
367 $tab[3][0] = "<b>".get_string("backupdate").":</b>";
368 $tab[3][1] = userdate($info->backup_date);
370 print_heading(get_string("backup").":");
372 //Print backup general info
374 //Now backup contents in another table
379 foreach ($mods as $key => $mod) {
380 $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
381 if ($mod->backup == "false") {
382 $tab[$elem][1] = get_string("notincluded");
384 if ($mod->userinfo == "true") {
385 $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
387 $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
393 $tab[$elem][0] = "<b>".get_string("users").":</b>";
394 $tab[$elem][1] = get_string($info->backup_users);
397 $tab[$elem][0] = "<b>".get_string("logs").":</b>";
398 if ($info->backup_logs == "true") {
399 $tab[$elem][1] = get_string("yes");
401 $tab[$elem][1] = get_string("no");
405 $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
406 if ($info->backup_user_files == "true") {
407 $tab[$elem][1] = get_string("yes");
409 $tab[$elem][1] = get_string("no");
413 $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
414 if ($info->backup_course_files == "true") {
415 $tab[$elem][1] = get_string("yes");
417 $tab[$elem][1] = get_string("no");
422 print_heading(get_string("backupdetails").":");
423 //Print backup general info
432 //This function prints the contents from the course_header parammeter passed
433 function restore_print_course_header ($course_header) {
436 if ($course_header) {
437 //This is tha align to every ingo table
438 $table->align = array ("RIGHT","LEFT");
440 $table->width = "70%";
441 //Put interesting course header in table
443 $tab[0][0] = "<b>".get_string("name").":</b>";
444 $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
446 $tab[1][0] = "<b>".get_string("summary").":</b>";
447 $tab[1][1] = $course_header->course_summary;
450 print_heading(get_string("course").":");
451 //Print backup course header info
459 //This function create a new course record.
460 //When finished, course_header contains the id of the new course
461 function restore_create_new_course($restore,&$course_header) {
467 $fullname = $course_header->course_fullname;
468 $shortname = $course_header->course_shortname;
469 $currentfullname = "";
470 $currentshortname = "";
472 //Iteratere while the name exists
475 $suffixfull = " ".get_string("copy")." ".$counter;
476 $suffixshort = "_".$counter;
481 $currentfullname = $fullname.$suffixfull;
482 $currentshortname = $shortname.$suffixshort;
483 $course = get_record("course","fullname",addslashes($currentfullname));
487 //New name = currentname
488 $course_header->course_fullname = $currentfullname;
489 $course_header->course_shortname = $currentshortname;
491 //Now calculate the category
492 $category = get_record("course_categories","id",$course_header->category->id,
493 "name",addslashes($course_header->category->name));
494 //If no exists, try by name only
496 $category = get_record("course_categories","name",addslashes($course_header->category->name));
498 //If no exists, get category id 1
500 $category = get_record("course_categories","id","1");
502 //If category 1 doesn'exists, lets create the course category (get it from backup file)
504 $ins_category->name = addslashes($course_header->category->name);
505 $ins_category->parent = 0;
506 $ins_category->sortorder = 0;
507 $ins_category->coursecount = 0;
508 $ins_category->visible = 0; //To avoid interferences with the rest of the site
509 $ins_category->timemodified = time();
510 $newid = insert_record("course_categories",$ins_category);
511 $category->id = $newid;
512 $category->name = $course_header->category->name;
514 //If exists, put new category id
516 $course_header->category->id = $category->id;
517 $course_header->category->name = $category->name;
518 //Error, cannot locate category
520 $course_header->category->id = 0;
521 $course_header->category->name = get_string("unknowncategory");
525 //Create the course_object
527 $course->category = addslashes($course_header->category->id);
528 $course->password = addslashes($course_header->course_password);
529 $course->fullname = addslashes($course_header->course_fullname);
530 $course->shortname = addslashes($course_header->course_shortname);
531 $course->summary = addslashes($course_header->course_summary);
532 $course->format = addslashes($course_header->course_format);
533 $course->showgrades = addslashes($course_header->course_showgrades);
534 $course->blockinfo = addslashes($course_header->blockinfo);
535 $course->newsitems = addslashes($course_header->course_newsitems);
536 $course->teacher = addslashes($course_header->course_teacher);
537 $course->teachers = addslashes($course_header->course_teachers);
538 $course->student = addslashes($course_header->course_student);
539 $course->students = addslashes($course_header->course_students);
540 $course->guest = addslashes($course_header->course_guest);
541 $course->startdate = addslashes($course_header->course_startdate);
542 $course->numsections = addslashes($course_header->course_numsections);
543 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
544 $course->maxbytes = addslashes($course_header->course_maxbytes);
545 $course->showreports = addslashes($course_header->course_showreports);
546 $course->groupmode = addslashes($course_header->course_groupmode);
547 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
548 $course->lang = addslashes($course_header->course_lang);
549 $course->marker = addslashes($course_header->course_marker);
550 $course->visible = addslashes($course_header->course_visible);
551 $course->hiddensections = addslashes($course_header->course_hiddensections);
552 $course->timecreated = addslashes($course_header->course_timecreated);
553 $course->timemodified = addslashes($course_header->course_timemodified);
554 //Adjust blockinfo field.
555 //If the info doesn't exist in backup, we create defaults, else we recode it
556 //to current site blocks.
557 if (!$course->blockinfo) {
558 //Create blockinfo default content
559 if ($course->format == "social") {
560 $course->blockinfo = blocks_get_default_blocks (NULL,"participants,search_forums,calendar_month,calendar_upcoming,social_activities,recent_activity,admin,course_list");
562 //For topics and weeks formats (default built in the function)
563 $course->blockinfo = blocks_get_default_blocks();
566 $course->blockinfo = blocks_get_block_ids($course->blockinfo);
568 //Now insert the record
569 $newid = insert_record("course",$course);
571 //save old and new course id
572 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
573 //Replace old course_id in course_header
574 $course_header->course_id = $newid;
583 //This function creates all the course_sections and course_modules from xml
584 //when restoring in a new course or simply checks sections and create records
585 //in backup_ids when restoring in a existing course
586 function restore_create_sections($restore,$xml_file) {
592 if (!file_exists($xml_file)) {
597 $info = restore_read_xml_sections($xml_file);
599 //Put the info in the DB, recoding ids and saving the in backup tables
604 //For each, section, save it to db
605 foreach ($info->sections as $key => $sect) {
607 $section->course = $restore->course_id;
608 $section->section = $sect->number;
609 $section->summary = addslashes($sect->summary);
610 $section->visible = $sect->visible;
611 $section->sequence = "";
612 //Now calculate the section's newid
614 if ($restore->restoreto == 2) {
615 //Save it to db (only if restoring to new course)
616 $newid = insert_record("course_sections",$section);
618 //Get section id when restoring in existing course
619 $rec = get_record("course_sections","course",$restore->course_id,
620 "section",$section->section);
621 //If that section doesn't exist, get section 0 (every mod will be
624 $rec = get_record("course_sections","course",$restore->course_id,
627 //New check. If section 0 doesn't exist, insert it here !!
628 //Teorically this never should happen but, in practice, some users
629 //have reported this issue.
631 $zero_sec->course = $restore->course_id;
632 $zero_sec->section = 0;
633 $zero_sec->summary = "";
634 $zero_sec->sequence = "";
635 $newid = insert_record("course_sections",$zero_sec);
640 $sequence = $rec->sequence;
643 //save old and new section id
644 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
648 //If all is OK, go with associated mods
650 //If we have mods in the section
651 if (!empty($sect->mods)) {
652 //For each mod inside section
653 foreach ($sect->mods as $keym => $mod) {
654 //Check if we've to restore this module
655 if ($restore->mods[$mod->type]->restore) {
656 //Get the module id from modules
657 $module = get_record("modules","name",$mod->type);
659 $course_module->course = $restore->course_id;
660 $course_module->module = $module->id;
661 $course_module->section = $newid;
662 $course_module->added = $mod->added;
663 $course_module->deleted = $mod->deleted;
664 $course_module->score = $mod->score;
665 $course_module->indent = $mod->indent;
666 $course_module->visible = $mod->visible;
667 $course_module->groupmode = $mod->groupmode;
668 $course_module->instance = null;
669 //NOTE: The instance (new) is calculated and updated in db in the
670 // final step of the restore. We don't know it yet.
671 //print_object($course_module); //Debug
673 $newidmod = insert_record("course_modules",$course_module);
675 //save old and new module id
676 //In the info field, we save the original instance of the module
678 backup_putid ($restore->backup_unique_code,"course_modules",
679 $keym,$newidmod,$mod->instance);
683 //Now, calculate the sequence field
686 $sequence .= ",".$newidmod;
688 $sequence = $newidmod;
698 //If all is OK, update sequence field in course_sections
700 if (isset($sequence)) {
701 $update_rec->id = $newid;
702 $update_rec->sequence = $sequence;
703 $status = update_record("course_sections",$update_rec);
713 //This function creates all the user, user_students, user_teachers
714 //user_course_creators and user_admins from xml
715 function restore_create_users($restore,$xml_file) {
721 if (!file_exists($xml_file)) {
726 //info will contain the old_id of every user
727 //in backup_ids->info will be the real info (serialized)
728 $info = restore_read_xml_users($restore,$xml_file);
731 //Now, get evey user_id from $info and user data from $backup_ids
732 //and create the necessary records (users, user_students, user_teachers
733 //user_course_creators and user_admins
734 if (!empty($info->users)) {
735 //For each user, take its info from backup_ids
736 foreach ($info->users as $userid) {
737 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
740 //Check if it's admin and coursecreator
741 $is_admin = !empty($user->roles['admin']);
742 $is_coursecreator = !empty($user->roles['coursecreator']);
744 //Check if it's teacher and student
745 $is_teacher = !empty($user->roles['teacher']);
746 $is_student = !empty($user->roles['student']);
748 //Check if it's needed
749 $is_needed = !empty($user->roles['needed']);
751 //Calculate if it is a course user
752 //Has role teacher or student or needed
753 $is_course_user = ($is_teacher or $is_student or $is_needed);
755 //To store new ids created
757 //check if it exists (by username) and get its id
759 $user_data = get_record("user","username",addslashes($user->username));
761 $user_exists = false;
763 $newid = $user_data->id;
765 //Flags to see if we have to create the user, roles and preferences
767 $create_roles = true;
768 $create_preferences = true;
770 //If we are restoring course users and it isn't a course user
771 if ($restore->users == 1 and !$is_course_user) {
772 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
773 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
774 $create_user = false;
775 $create_roles = false;
776 $create_preferences = false;
779 if ($user_exists and $create_user) {
780 //If user exists mark its newid in backup_ids (the same than old)
781 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
782 $create_user = false;
785 //Here, if create_user, do it
787 //Unset the id because it's going to be inserted with a new one
789 //We addslashes to necessary fields
790 $user->username = addslashes($user->username);
791 $user->firstname = addslashes($user->firstname);
792 $user->lastname = addslashes($user->lastname);
793 $user->email = addslashes($user->email);
794 $user->institution = addslashes($user->institution);
795 $user->department = addslashes($user->department);
796 $user->address = addslashes($user->address);
797 $user->city = addslashes($user->city);
798 $user->url = addslashes($user->url);
799 $user->description = addslashes($user->description);
800 //We are going to create the user
801 //The structure is exactly as we need
802 $newid = insert_record ("user",$user);
804 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
807 //Here, if create_roles, do it as necessary
809 //Get the newid and current info from backup_ids
810 $data = backup_getid($restore->backup_unique_code,"user",$userid);
811 $newid = $data->new_id;
812 $currinfo = $data->info.",";
814 //Now, depending of the role, create records in user_studentes and user_teacher
815 //and/or mark it in backup_ids
818 //If the record (user_admins) doesn't exists
819 if (!record_exists("user_admins","userid",$newid)) {
820 //Only put status in backup_ids
821 $currinfo = $currinfo."admin,";
822 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
825 if ($is_coursecreator) {
826 //If the record (user_coursecreators) doesn't exists
827 if (!record_exists("user_coursecreators","userid",$newid)) {
828 //Only put status in backup_ids
829 $currinfo = $currinfo."coursecreator,";
830 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
834 //Only put status in backup_ids
835 $currinfo = $currinfo."needed,";
836 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
839 //If the record (teacher) doesn't exists
840 if (!record_exists("user_teachers","userid",$newid,"course", $restore->course_id)) {
841 //Put status in backup_ids
842 $currinfo = $currinfo."teacher,";
843 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
844 //Set course and user
845 $user->roles['teacher']->course = $restore->course_id;
846 $user->roles['teacher']->userid = $newid;
847 //Insert data in user_teachers
848 //The structure is exactly as we need
849 $status = insert_record("user_teachers",$user->roles['teacher']);
853 //If the record (student) doesn't exists
854 if (!record_exists("user_students","userid",$newid,"course", $restore->course_id)) {
855 //Put status in backup_ids
856 $currinfo = $currinfo."student,";
857 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
858 //Set course and user
859 $user->roles['student']->course = $restore->course_id;
860 $user->roles['student']->userid = $newid;
861 //Insert data in user_students
862 //The structure is exactly as we need
863 $status = insert_record("user_students",$user->roles['student']);
866 if (!$is_course_user) {
867 //If the record (user) doesn't exists
868 if (!record_exists("user","id",$newid)) {
869 //Put status in backup_ids
870 $currinfo = $currinfo."user,";
871 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
876 //Here, if create_preferences, do it as necessary
877 if ($create_preferences) {
878 //echo "Checking for preferences of user ".$user->username."<br>"; //Debug
879 //Get user new id from backup_ids
880 $data = backup_getid($restore->backup_unique_code,"user",$userid);
881 $newid = $data->new_id;
882 if (isset($user->user_preferences)) {
883 //echo "Preferences exist in backup file<br>"; //Debug
884 foreach($user->user_preferences as $user_preference) {
885 //echo $user_preference->name." = ".$user_preference->value."<br>"; //Debug
886 //We check if that user_preference exists in DB
887 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
888 //echo "Creating it<br>"; //Debug
889 //Prepare the record and insert it
890 $user_preference->userid = $newid;
891 $status = insert_record("user_preferences",$user_preference);
902 //This function creates all the categories and questions
903 //from xml (STEP1 of quiz restore)
904 function restore_create_questions($restore,$xml_file) {
910 if (!file_exists($xml_file)) {
915 //info will contain the old_id of every category
916 //in backup_ids->info will be the real info (serialized)
917 $info = restore_read_xml_questions($restore,$xml_file);
919 //Now, if we have anything in info, we have to restore that
920 //categories/questions
922 if ($info !== true) {
923 //Iterate over each category
924 foreach ($info as $category) {
925 $catrestore = "quiz_restore_question_categories";
926 if (function_exists($catrestore)) {
927 //print_object ($category); //Debug
928 $status = $catrestore($category,$restore);
930 //Something was wrong. Function should exist.
941 //This function creates all the scales
942 function restore_create_scales($restore,$xml_file) {
948 if (!file_exists($xml_file)) {
953 //scales will contain the old_id of every scale
954 //in backup_ids->info will be the real info (serialized)
955 $scales = restore_read_xml_scales($restore,$xml_file);
957 //Now, if we have anything in scales, we have to restore that
960 if ($scales !== true) {
961 //Iterate over each scale
962 foreach ($scales as $scale) {
963 //Get record from backup_ids
964 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
966 $create_scale = false;
969 //Now get completed xmlized object
971 //traverse_xmlize($info); //Debug
972 //print_object ($GLOBALS['traverse_array']); //Debug
973 //$GLOBALS['traverse_array']=""; //Debug
975 //Now build the SCALE record structure
976 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
977 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
978 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
979 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
980 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
981 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
983 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
984 //or in restore->course_id course (Personal scale)
985 if ($sca->courseid == 0) {
986 $course_to_search = 0;
988 $course_to_search = $restore->course_id;
990 $sca_db = get_record("scale","scale",$sca->scale,"courseid",$course_to_search);
991 //If it doesn't exist, create
993 $create_scale = true;
995 //If we must create the scale
997 //Me must recode the courseid if it's <> 0 (common scale)
998 if ($sca->courseid != 0) {
999 $sca->courseid = $restore->course_id;
1001 //We must recode the userid
1002 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
1004 $sca->userid = $user->new_id;
1006 //Assign it to admin
1009 //The structure is equal to the db, so insert the scale
1010 $newid = insert_record ("scale",$sca);
1012 //get current scale id
1013 $newid = $sca_db->id;
1016 //We have the newid, update backup_ids
1017 backup_putid($restore->backup_unique_code,"scale",
1018 $scale->id, $newid);
1029 //This function creates all the groups
1030 function restore_create_groups($restore,$xml_file) {
1037 if (!file_exists($xml_file)) {
1042 //groups will contain the old_id of every group
1043 //in backup_ids->info will be the real info (serialized)
1044 $groups = restore_read_xml_groups($restore,$xml_file);
1046 //Now, if we have anything in groups, we have to restore that
1049 if ($groups !== true) {
1050 //Iterate over each group
1051 foreach ($groups as $group) {
1052 //Get record from backup_ids
1053 $data = backup_getid($restore->backup_unique_code,"group",$group->id);
1055 $create_group = false;
1058 //Now get completed xmlized object
1059 $info = $data->info;
1060 //traverse_xmlize($info); //Debug
1061 //print_object ($GLOBALS['traverse_array']); //Debug
1062 //$GLOBALS['traverse_array']=""; //Debug
1063 //Now build the GROUP record structure
1064 $gro->courseid = backup_todb($info['GROUP']['#']['COURSEID']['0']['#']);
1065 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
1066 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
1067 $gro->lang = backup_todb($info['GROUP']['#']['LANG']['0']['#']);
1068 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
1069 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
1070 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
1071 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
1073 //Now search if that group exists (by name and description field) in
1074 //restore->course_id course
1075 $gro_db = get_record("groups","courseid",$restore->course_id,"name",$gro->name,"description",$gro->description);
1076 //If it doesn't exist, create
1078 $create_group = true;
1080 //If we must create the group
1081 if ($create_group) {
1082 //Me must recode the courseid to the restore->course_id
1083 $gro->courseid = $restore->course_id;
1084 //The structure is equal to the db, so insert the group
1085 $newid = insert_record ("groups",$gro);
1087 //get current group id
1088 $newid = $gro_db->id;
1091 //We have the newid, update backup_ids
1092 backup_putid($restore->backup_unique_code,"group",
1093 $group->id, $newid);
1095 //Now restore members in the groups_members
1096 $status2 = restore_create_groups_members($newid,$info,$restore);
1103 return ($status && $status2);
1106 //This function restores the groups_members
1107 function restore_create_groups_members($group_id,$info,$restore) {
1113 //Get the members array
1114 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
1116 //Iterate over members
1117 for($i = 0; $i < sizeof($members); $i++) {
1118 $mem_info = $members[$i];
1119 //traverse_xmlize($mem_info); //Debug
1120 //print_object ($GLOBALS['traverse_array']); //Debug
1121 //$GLOBALS['traverse_array']=""; //Debug
1123 //Now, build the GROUPS_MEMBERS record structure
1124 $group_member->groupid = $group_id;
1125 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
1126 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
1128 //We have to recode the userid field
1129 $user = backup_getid($restore->backup_unique_code,"user",$group_member->userid);
1131 $group_member->userid = $user->new_id;
1134 //The structure is equal to the db, so insert the groups_members
1135 $newid = insert_record ("groups_members",$group_member);
1138 if (($i+1) % 50 == 0) {
1140 if (($i+1) % 1000 == 0) {
1154 //This function creates all the course events
1155 function restore_create_events($restore,$xml_file) {
1161 if (!file_exists($xml_file)) {
1166 //events will contain the old_id of every event
1167 //in backup_ids->info will be the real info (serialized)
1168 $events = restore_read_xml_events($restore,$xml_file);
1170 //Now, if we have anything in events, we have to restore that
1173 if ($events !== true) {
1174 //Iterate over each event
1175 foreach ($events as $event) {
1176 //Get record from backup_ids
1177 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
1179 $create_event = false;
1182 //Now get completed xmlized object
1183 $info = $data->info;
1184 //traverse_xmlize($info); //Debug
1185 //print_object ($GLOBALS['traverse_array']); //Debug
1186 //$GLOBALS['traverse_array']=""; //Debug
1188 //Now build the EVENT record structure
1189 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
1190 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
1191 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
1192 $eve->courseid = $restore->course_id;
1193 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
1194 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
1195 $eve->modulename = "";
1197 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
1198 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
1199 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
1200 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
1201 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
1203 //Now search if that event exists (by description and timestart field) in
1204 //restore->course_id course
1205 $eve_db = get_record("event","courseid",$eve->courseid,"description",$eve->description,"timestart",$eve->timestart);
1206 //If it doesn't exist, create
1208 $create_event = true;
1210 //If we must create the event
1211 if ($create_event) {
1213 //We must recode the userid
1214 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
1216 $eve->userid = $user->new_id;
1218 //Assign it to admin
1221 //We have to recode the groupid field
1222 $group = backup_getid($restore->backup_unique_code,"group",$eve->groupid);
1224 $eve->groupid = $group->new_id;
1226 //Assign it to group 0
1230 //The structure is equal to the db, so insert the event
1231 $newid = insert_record ("event",$eve);
1233 //get current event id
1234 $newid = $eve_db->id;
1237 //We have the newid, update backup_ids
1238 backup_putid($restore->backup_unique_code,"event",
1239 $event->id, $newid);
1250 //This function decode things to make restore multi-site fully functional
1251 //It does this conversions:
1252 // - $@FILEPHP@$ -------------------------------> $CFG->wwwroot/file.php/courseid
1254 //Note: Inter-activities linking is being implemented as a final
1255 //step in the restore execution, because we need to have it
1256 //finished to know all the oldid, newid equivaleces
1257 function restore_decode_absolute_links($content) {
1259 global $CFG,$restore;
1261 //Now decode wwwroot and file.php calls
1262 $search = array ("$@FILEPHP@$");
1264 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
1266 $result = str_replace($search,$replace,$content);
1268 if ($result != $content && $CFG->debug>7) { //Debug
1269 echo "<br><hr>".$content."<br>changed to<br>".$result."<hr><br>"; //Debug
1275 //This function restores the userfiles from the temp (user_files) directory to the
1276 //dataroot/users directory
1277 function restore_user_files($restore) {
1285 //First, we check to "users" exists and create is as necessary
1287 $dest_dir = $CFG->dataroot."/users";
1288 $status = check_dir_exists($dest_dir,true);
1290 //Now, we iterate over "user_files" records to check if that user dir must be
1291 //copied (and renamed) to the "users" dir.
1292 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
1293 //Check if directory exists
1294 if (is_dir($rootdir)) {
1295 $list = list_directories ($rootdir);
1299 foreach ($list as $dir) {
1300 //Look for dir like username in backup_ids
1301 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
1302 "table_name","user",
1304 //If thar user exists in backup_ids
1306 //Only it user has been created now
1307 //or if it existed previously, but he hasn't image (see bug 1123)
1308 if ((strpos($data->info,"new") !== false) or
1309 (!check_dir_exists($dest_dir."/".$data->new_id,false))) {
1310 //Copy the old_dir to its new location (and name) !!
1311 //Only if destination doesn't exists
1312 if (!file_exists($dest_dir."/".$data->new_id)) {
1313 $status = backup_copy_file($rootdir."/".$dir,
1314 $dest_dir."/".$data->new_id);
1318 if ($counter % 2 == 0) {
1320 if ($counter % 40 == 0) {
1330 //If status is ok and whe have dirs created, returns counter to inform
1331 if ($status and $counter) {
1338 //This function restores the course files from the temp (course_files) directory to the
1339 //dataroot/course_id directory
1340 function restore_course_files($restore) {
1348 //First, we check to "course_id" exists and create is as necessary
1350 $dest_dir = $CFG->dataroot."/".$restore->course_id;
1351 $status = check_dir_exists($dest_dir,true);
1353 //Now, we iterate over "course_files" records to check if that file/dir must be
1354 //copied to the "dest_dir" dir.
1355 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
1356 //Check if directory exists
1357 if (is_dir($rootdir)) {
1358 $list = list_directories_and_files ($rootdir);
1362 foreach ($list as $dir) {
1363 //Copy the dir to its new location
1364 //Only if destination file/dir doesn exists
1365 if (!file_exists($dest_dir."/".$dir)) {
1366 $status = backup_copy_file($rootdir."/".$dir,
1367 $dest_dir."/".$dir);
1371 if ($counter % 2 == 0) {
1373 if ($counter % 40 == 0) {
1381 //If status is ok and whe have dirs created, returns counter to inform
1382 if ($status and $counter) {
1390 //This function creates all the structures for every module in backup file
1391 //Depending what has been selected.
1392 function restore_create_modules($restore,$xml_file) {
1398 if (!file_exists($xml_file)) {
1403 //info will contain the id and modtype of every module
1404 //in backup_ids->info will be the real info (serialized)
1405 $info = restore_read_xml_modules($restore,$xml_file);
1408 //Now, if we have anything in info, we have to restore that mods
1409 //from backup_ids (calling every mod restore function)
1411 if ($info !== true) {
1412 //Iterate over each module
1413 foreach ($info as $mod) {
1414 $modrestore = $mod->modtype."_restore_mods";
1415 if (function_exists($modrestore)) {
1416 //print_object ($mod); //Debug
1417 $status = $modrestore($mod,$restore);
1419 //Something was wrong. Function should exist.
1430 //This function creates all the structures for every log in backup file
1431 //Depending what has been selected.
1432 function restore_create_logs($restore,$xml_file) {
1436 //Number of records to get in every chunk
1437 $recordset_size = 4;
1438 //Counter, points to current record
1440 //To count all the recods to restore
1445 if (!file_exists($xml_file)) {
1450 //count_logs will contain the number of logs entries to process
1451 //in backup_ids->info will be the real info (serialized)
1452 $count_logs = restore_read_xml_logs($restore,$xml_file);
1455 //Now, if we have records in count_logs, we have to restore that logs
1456 //from backup_ids. This piece of code makes calls to:
1457 // - restore_log_course() if it's a course log
1458 // - restore_log_user() if it's a user log
1459 // - restore_log_module() if it's a module log.
1460 //And all is segmented in chunks to allow large recordsets to be restored !!
1461 if ($count_logs > 0) {
1462 while ($counter < $count_logs) {
1463 //Get a chunk of records
1464 //Take old_id twice to avoid adodb limitation
1465 $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);
1469 foreach ($logs as $log) {
1470 //Get the full record from backup_ids
1471 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
1473 //Now get completed xmlized object
1474 $info = $data->info;
1475 //traverse_xmlize($info); //Debug
1476 //print_object ($GLOBALS['traverse_array']); //Debug
1477 //$GLOBALS['traverse_array']=""; //Debug
1478 //Now build the LOG record structure
1479 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
1480 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
1481 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
1482 $dblog->course = $restore->course_id;
1483 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
1484 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
1485 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
1486 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
1487 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
1488 //We have to recode the userid field
1489 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
1491 //echo "User ".$dblog->userid." to user ".$user->new_id."<br>"; //Debug
1492 $dblog->userid = $user->new_id;
1494 //We have to recode the cmid field (if module isn't "course" or "user")
1495 if ($dblog->module != "course" and $dblog->module != "user") {
1496 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
1498 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br>"; //Debug
1499 $dblog->cmid = $cm->new_id;
1504 //print_object ($dblog); //Debug
1505 //Now, we redirect to the needed function to make all the work
1506 if ($dblog->module == "course") {
1507 //It's a course log,
1508 $stat = restore_log_course($restore,$dblog);
1509 } elseif ($dblog->module == "user") {
1511 $stat = restore_log_user($restore,$dblog);
1513 //It's a module log,
1514 $stat = restore_log_module($restore,$dblog);
1520 if ($counter % 10 == 0) {
1522 if ($counter % 200 == 0) {
1529 //We never should arrive here
1530 $counter = $count_logs;
1539 //This function inserts a course log record, calculating the URL field as necessary
1540 function restore_log_course($restore,$log) {
1545 //echo "<hr>Before transformations<br>"; //Debug
1546 //print_object($log); //Debug
1547 //Depending of the action, we recode different things
1548 switch ($log->action) {
1550 $log->url = "view.php?id=".$log->course;
1551 $log->info = $log->course;
1555 //recode the info field (it's the user id)
1556 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
1558 $log->info = $user->new_id;
1559 //Now, extract the mode from the url field
1560 $mode = substr(strrchr($log->url,"="),1);
1561 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
1566 //Extract the course_module from the url field
1567 $cmid = substr(strrchr($log->url,"="),1);
1568 //recode the course_module to see it it has been restored
1569 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
1571 $cmid = $cm->new_id;
1572 //Extract the module name and the module id from the info field
1573 $modname = strtok($log->info," ");
1574 $modid = strtok(" ");
1575 //recode the module id to see if it has been restored
1576 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
1578 $modid = $mod->new_id;
1579 //Now I have everything so reconstruct url and info
1580 $log->info = $modname." ".$modid;
1581 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
1587 //Extract the course_module from the url field
1588 $cmid = substr(strrchr($log->url,"="),1);
1589 //recode the course_module to see it it has been restored
1590 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
1592 $cmid = $cm->new_id;
1593 //Extract the module name and the module id from the info field
1594 $modname = strtok($log->info," ");
1595 $modid = strtok(" ");
1596 //recode the module id to see if it has been restored
1597 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
1599 $modid = $mod->new_id;
1600 //Now I have everything so reconstruct url and info
1601 $log->info = $modname." ".$modid;
1602 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
1608 $log->url = "view.php?id=".$log->course;
1612 $log->url = "edit.php?id=".$log->course;
1617 //recode the info field (it's the user id)
1618 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
1620 $log->info = $user->new_id;
1621 $log->url = "view.php?id=".$log->course;
1626 //recode the info field (it's the user id)
1627 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
1629 $log->info = $user->new_id;
1630 $log->url = "view.php?id=".$log->course;
1635 //Extract the course_section from the url field
1636 $secid = substr(strrchr($log->url,"="),1);
1637 //recode the course_section to see if it has been restored
1638 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
1640 $secid = $sec->new_id;
1641 //Now I have everything so reconstruct url and info
1642 $log->url = "editsection.php?id=".$secid;
1647 $log->url = "view.php?id=".$log->course;
1652 echo "action (".$log->module."-".$log->action.") unknow. Not restored<br>"; //Debug
1656 //echo "After transformations<br>"; //Debug
1657 //print_object($log); //Debug
1659 //Now if $toinsert is set, insert the record
1661 //echo "Inserting record<br>"; //Debug
1662 $status = insert_record("log",$log);
1667 //This function inserts a user log record, calculating the URL field as necessary
1668 function restore_log_user($restore,$log) {
1673 //echo "<hr>Before transformations<br>"; //Debug
1674 //print_object($log); //Debug
1675 //Depending of the action, we recode different things
1676 switch ($log->action) {
1678 //recode the info field (it's the user id)
1679 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
1681 $log->info = $user->new_id;
1682 $log->url = "view.php?id=".$log->info."&course=".$log->course;
1686 case "change password":
1687 //recode the info field (it's the user id)
1688 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
1690 $log->info = $user->new_id;
1691 $log->url = "view.php?id=".$log->info."&course=".$log->course;
1696 $log->url = "view.php?id=".$log->course;
1700 //We split the url by ampersand char
1701 $first_part = strtok($log->url,"&");
1702 //Get data after the = char. It's the user being updated
1703 $userid = substr(strrchr($first_part,"="),1);
1705 $user = backup_getid($restore->backup_unique_code,"user",$userid);
1708 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
1713 echo "action (".$log->module."-".$log->action.") unknow. Not restored<br>"; //Debug
1717 //echo "After transformations<br>"; //Debug
1718 //print_object($log); //Debug
1720 //Now if $toinsert is set, insert the record
1722 //echo "Inserting record<br>"; //Debug
1723 $status = insert_record("log",$log);
1728 //This function inserts a module log record, calculating the URL field as necessary
1729 function restore_log_module($restore,$log) {
1734 //echo "<hr>Before transformations<br>"; //Debug
1735 //print_object($log); //Debug
1737 //Now we see if the required function in the module exists
1738 $function = $log->module."_restore_logs";
1739 if (function_exists($function)) {
1741 $log = $function($restore,$log);
1742 //If everything is ok, mark the insert flag
1748 //echo "After transformations<br>"; //Debug
1749 //print_object($log); //Debug
1751 //Now if $toinsert is set, insert the record
1753 //echo "Inserting record<br>"; //Debug
1754 $status = insert_record("log",$log);
1759 //This function adjusts the instance field into course_modules. It's executed after
1760 //modules restore. There, we KNOW the new instance id !!
1761 function restore_check_instances($restore) {
1767 //We are going to iterate over each course_module saved in backup_ids
1768 $course_modules = get_records_sql("SELECT old_id,new_id
1769 FROM {$CFG->prefix}backup_ids
1770 WHERE backup_code = '$restore->backup_unique_code' AND
1771 table_name = 'course_modules'");
1772 if ($course_modules) {
1773 foreach($course_modules as $cm) {
1774 //Get full record, using backup_getids
1775 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
1776 //Now we are going to the REAL course_modules to get its type (field module)
1777 $module = get_record("course_modules","id",$cm_module->new_id);
1779 //We know the module type id. Get the name from modules
1780 $type = get_record("modules","id",$module->module);
1782 //We know the type name and the old_id. Get its new_id
1783 //from backup_ids. It's the instance !!!
1784 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
1786 //We have the new instance, so update the record in course_modules
1787 $module->instance = $instance->new_id;
1788 //print_object ($module); //Debug
1789 $status = update_record("course_modules",$module);
1806 //=====================================================================================
1808 //== XML Functions (SAX) ==
1810 //=====================================================================================
1812 //This is the class used to do all the xml parse
1813 class MoodleParser {
1815 var $level = 0; //Level we are
1816 var $counter = 0; //Counter
1817 var $tree = array(); //Array of levels we are
1818 var $content = ""; //Content under current level
1819 var $todo = ""; //What we hav to do when parsing
1820 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
1821 var $temp = ""; //Temp storage.
1822 var $preferences = ""; //Preferences about what to load !!
1823 var $finished = false; //Flag to say xml_parse to stop
1825 //This function is used to get the current contents property value
1826 //They are trimed and converted from utf8
1827 function getContents() {
1828 return trim(utf8_decode($this->content));
1831 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
1832 function startElementInfo($parser, $tagName, $attrs) {
1833 //Refresh properties
1835 $this->tree[$this->level] = $tagName;
1837 //Output something to avoid browser timeouts...
1840 //Check if we are into INFO zone
1841 //if ($this->tree[2] == "INFO") //Debug
1842 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1845 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
1846 function startElementCourseHeader($parser, $tagName, $attrs) {
1847 //Refresh properties
1849 $this->tree[$this->level] = $tagName;
1851 //Output something to avoid browser timeouts...
1854 //Check if we are into COURSE_HEADER zone
1855 //if ($this->tree[3] == "HEADER") //Debug
1856 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1859 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
1860 function startElementSections($parser, $tagName, $attrs) {
1861 //Refresh properties
1863 $this->tree[$this->level] = $tagName;
1865 //Output something to avoid browser timeouts...
1868 //Check if we are into SECTIONS zone
1869 //if ($this->tree[3] == "SECTIONS") //Debug
1870 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1873 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
1874 function startElementUsers($parser, $tagName, $attrs) {
1875 //Refresh properties
1877 $this->tree[$this->level] = $tagName;
1879 //Check if we are into USERS zone
1880 //if ($this->tree[3] == "USERS") //Debug
1881 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1884 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
1885 function startElementQuestions($parser, $tagName, $attrs) {
1886 //Refresh properties
1888 $this->tree[$this->level] = $tagName;
1890 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
1891 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
1894 //Output something to avoid browser timeouts...
1897 //Check if we are into QUESTION_CATEGORIES zone
1898 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
1899 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1901 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
1902 if (isset($this->tree[4]) and isset($this->tree[3])) {
1903 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
1904 if (!isset($this->temp)) {
1907 $this->temp .= "<".$tagName.">";
1912 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
1913 function startElementScales($parser, $tagName, $attrs) {
1914 //Refresh properties
1916 $this->tree[$this->level] = $tagName;
1918 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
1919 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
1922 //Output something to avoid browser timeouts...
1925 //Check if we are into SCALES zone
1926 //if ($this->tree[3] == "SCALES") //Debug
1927 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1929 //If we are under a SCALE tag under a SCALES zone, accumule it
1930 if (isset($this->tree[4]) and isset($this->tree[3])) {
1931 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
1932 if (!isset($this->temp)) {
1935 $this->temp .= "<".$tagName.">";
1940 function startElementGroups($parser, $tagName, $attrs) {
1941 //Refresh properties
1943 $this->tree[$this->level] = $tagName;
1945 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
1946 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
1949 //Output something to avoid browser timeouts...
1952 //Check if we are into GROUPS zone
1953 //if ($this->tree[3] == "GROUPS") //Debug
1954 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1956 //If we are under a GROUP tag under a GROUPS zone, accumule it
1957 if (isset($this->tree[4]) and isset($this->tree[3])) {
1958 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
1959 if (!isset($this->temp)) {
1962 $this->temp .= "<".$tagName.">";
1967 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
1968 function startElementEvents($parser, $tagName, $attrs) {
1969 //Refresh properties
1971 $this->tree[$this->level] = $tagName;
1973 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
1974 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
1977 //Output something to avoid browser timeouts...
1980 //Check if we are into EVENTS zone
1981 //if ($this->tree[3] == "EVENTS") //Debug
1982 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
1984 //If we are under a EVENT tag under a EVENTS zone, accumule it
1985 if (isset($this->tree[4]) and isset($this->tree[3])) {
1986 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
1987 if (!isset($this->temp)) {
1990 $this->temp .= "<".$tagName.">";
1995 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
1996 function startElementModules($parser, $tagName, $attrs) {
1997 //Refresh properties
1999 $this->tree[$this->level] = $tagName;
2001 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
2002 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
2005 //Output something to avoid browser timeouts...
2008 //Check if we are into MODULES zone
2009 //if ($this->tree[3] == "MODULES") //Debug
2010 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
2012 //If we are under a MOD tag under a MODULES zone, accumule it
2013 if (isset($this->tree[4]) and isset($this->tree[3])) {
2014 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
2015 if (!isset($this->temp)) {
2018 $this->temp .= "<".$tagName.">";
2023 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
2024 function startElementLogs($parser, $tagName, $attrs) {
2025 //Refresh properties
2027 $this->tree[$this->level] = $tagName;
2029 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
2030 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
2033 //Output something to avoid browser timeouts...
2036 //Check if we are into LOGS zone
2037 //if ($this->tree[3] == "LOGS") //Debug
2038 // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
2040 //If we are under a LOG tag under a LOGS zone, accumule it
2041 if (isset($this->tree[4]) and isset($this->tree[3])) {
2042 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
2043 if (!isset($this->temp)) {
2046 $this->temp .= "<".$tagName.">";
2051 //This is the startTag default handler we use when todo is undefined
2052 function startElement($parser, $tagName, $attrs) {
2054 $this->tree[$this->level] = $tagName;
2056 //Output something to avoid browser timeouts...
2059 echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><br>\n"; //Debug
2062 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
2063 function endElementInfo($parser, $tagName) {
2064 //Check if we are into INFO zone
2065 if ($this->tree[2] == "INFO") {
2066 //if (trim($this->content)) //Debug
2067 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2068 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2069 //Dependig of different combinations, do different things
2070 if ($this->level == 3) {
2073 $this->info->backup_name = $this->getContents();
2075 case "MOODLE_VERSION":
2076 $this->info->backup_moodle_version = $this->getContents();
2078 case "MOODLE_RELEASE":
2079 $this->info->backup_moodle_release = $this->getContents();
2081 case "BACKUP_VERSION":
2082 $this->info->backup_backup_version = $this->getContents();
2084 case "BACKUP_RELEASE":
2085 $this->info->backup_backup_release = $this->getContents();
2088 $this->info->backup_date = $this->getContents();
2090 case "ORIGINAL_WWWROOT":
2091 $this->info->original_wwwroot = $this->getContents();
2095 if ($this->tree[3] == "DETAILS") {
2096 if ($this->level == 4) {
2099 $this->info->backup_users = $this->getContents();
2102 $this->info->backup_logs = $this->getContents();
2105 $this->info->backup_user_files = $this->getContents();
2108 $this->info->backup_course_files = $this->getContents();
2112 if ($this->level == 5) {
2115 $this->info->tempName = $this->getContents();
2118 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
2121 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
2130 $this->tree[$this->level] = "";
2132 $this->content = "";
2134 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
2135 //Speed up a lot (avoid parse all)
2136 if ($tagName == "INFO") {
2137 $this->finished = true;
2141 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
2142 function endElementCourseHeader($parser, $tagName) {
2143 //Check if we are into COURSE_HEADER zone
2144 if ($this->tree[3] == "HEADER") {
2145 //if (trim($this->content)) //Debug
2146 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2147 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2148 //Dependig of different combinations, do different things
2149 if ($this->level == 4) {
2152 $this->info->course_id = $this->getContents();
2155 $this->info->course_password = $this->getContents();
2158 $this->info->course_fullname = $this->getContents();
2161 $this->info->course_shortname = $this->getContents();
2164 $this->info->course_summary = $this->getContents();
2167 $this->info->course_format = $this->getContents();
2170 $this->info->course_showgrades = $this->getContents();
2173 $this->info->blockinfo = $this->getContents();
2176 $this->info->course_newsitems = $this->getContents();
2179 $this->info->course_teacher = $this->getContents();
2182 $this->info->course_teachers = $this->getContents();
2185 $this->info->course_student = $this->getContents();
2188 $this->info->course_students = $this->getContents();
2191 $this->info->course_guest = $this->getContents();
2194 $this->info->course_startdate = $this->getContents();
2197 $this->info->course_numsections = $this->getContents();
2199 //case "SHOWRECENT": INFO: This is out in 1.3
2200 // $this->info->course_showrecent = $this->getContents();
2203 $this->info->course_maxbytes = $this->getContents();
2206 $this->info->course_showreports = $this->getContents();
2209 $this->info->course_groupmode = $this->getContents();
2211 case "GROUPMODEFORCE":
2212 $this->info->course_groupmodeforce = $this->getContents();
2215 $this->info->course_lang = $this->getContents();
2218 $this->info->course_marker = $this->getContents();
2221 $this->info->course_visible = $this->getContents();
2223 case "HIDDENSECTIONS":
2224 $this->info->course_hiddensections = $this->getContents();
2227 $this->info->course_timecreated = $this->getContents();
2229 case "TIMEMODIFIED":
2230 $this->info->course_timemodified = $this->getContents();
2234 if ($this->tree[4] == "CATEGORY") {
2235 if ($this->level == 5) {
2238 $this->info->category->id = $this->getContents();
2241 $this->info->category->name = $this->getContents();
2249 $this->tree[$this->level] = "";
2251 $this->content = "";
2253 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
2254 //Speed up a lot (avoid parse all)
2255 if ($tagName == "HEADER") {
2256 $this->finished = true;
2260 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
2261 function endElementSections($parser, $tagName) {
2262 //Check if we are into SECTIONS zone
2263 if ($this->tree[3] == "SECTIONS") {
2264 //if (trim($this->content)) //Debug
2265 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2266 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2267 //Dependig of different combinations, do different things
2268 if ($this->level == 4) {
2271 //We've finalized a section, get it
2272 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
2273 unset($this->info->tempsection);
2276 if ($this->level == 5) {
2279 $this->info->tempsection->id = $this->getContents();
2282 $this->info->tempsection->number = $this->getContents();
2285 $this->info->tempsection->summary = $this->getContents();
2288 $this->info->tempsection->visible = $this->getContents();
2292 if ($this->level == 6) {
2295 //We've finalized a mod, get it
2296 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
2297 $this->info->tempmod->type;
2298 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
2299 $this->info->tempmod->instance;
2300 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
2301 $this->info->tempmod->added;
2302 $this->info->tempsection->mods[$this->info->tempmod->id]->deleted =
2303 $this->info->tempmod->deleted;
2304 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
2305 $this->info->tempmod->score;
2306 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
2307 $this->info->tempmod->indent;
2308 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
2309 $this->info->tempmod->visible;
2310 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
2311 $this->info->tempmod->groupmode;
2312 unset($this->info->tempmod);
2315 if ($this->level == 7) {
2318 $this->info->tempmod->id = $this->getContents();
2321 $this->info->tempmod->type = $this->getContents();
2324 $this->info->tempmod->instance = $this->getContents();
2327 $this->info->tempmod->added = $this->getContents();
2330 $this->info->tempmod->deleted = $this->getContents();
2333 $this->info->tempmod->score = $this->getContents();
2336 $this->info->tempmod->indent = $this->getContents();
2339 $this->info->tempmod->visible = $this->getContents();
2342 $this->info->tempmod->groupmode = $this->getContents();
2348 $this->tree[$this->level] = "";
2350 $this->content = "";
2352 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
2353 //Speed up a lot (avoid parse all)
2354 if ($tagName == "SECTIONS") {
2355 $this->finished = true;
2359 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
2360 function endElementUsers($parser, $tagName) {
2361 //Check if we are into USERS zone
2362 if ($this->tree[3] == "USERS") {
2363 //if (trim($this->content)) //Debug
2364 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2365 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2366 //Dependig of different combinations, do different things
2367 if ($this->level == 4) {
2373 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
2374 null,$this->info->tempuser);
2377 if ($this->counter % 10 == 0) {
2379 if ($this->counter % 200 == 0) {
2385 //Delete temp obejct
2386 unset($this->info->tempuser);
2390 if ($this->level == 5) {
2393 $this->info->users[$this->getContents()] = $this->getContents();
2394 $this->info->tempuser->id = $this->getContents();
2397 $this->info->tempuser->confirmed = $this->getContents();
2400 $this->info->tempuser->deleted = $this->getContents();
2403 $this->info->tempuser->username = $this->getContents();
2406 $this->info->tempuser->password = $this->getContents();
2409 $this->info->tempuser->idnumber = $this->getContents();
2412 $this->info->tempuser->firstname = $this->getContents();
2415 $this->info->tempuser->lastname = $this->getContents();
2418 $this->info->tempuser->email = $this->getContents();
2421 $this->info->tempuser->emailstop = $this->getContents();
2424 $this->info->tempuser->icq = $this->getContents();
2427 $this->info->tempuser->phone1 = $this->getContents();
2430 $this->info->tempuser->phone2 = $this->getContents();
2433 $this->info->tempuser->institution = $this->getContents();
2436 $this->info->tempuser->department = $this->getContents();
2439 $this->info->tempuser->address = $this->getContents();
2442 $this->info->tempuser->city = $this->getContents();
2445 $this->info->tempuser->country = $this->getContents();
2448 $this->info->tempuser->lang = $this->getContents();
2451 $this->info->tempuser->timezone = $this->getContents();
2454 $this->info->tempuser->firstaccess = $this->getContents();
2457 $this->info->tempuser->lastaccess = $this->getContents();
2460 $this->info->tempuser->lastlogin = $this->getContents();
2462 case "CURRENTLOGIN":
2463 $this->info->tempuser->currentlogin = $this->getContents();
2466 $this->info->tempuser->lastip = $this->getContents();
2469 $this->info->tempuser->secret = $this->getContents();
2472 $this->info->tempuser->picture = $this->getContents();
2475 $this->info->tempuser->url = $this->getContents();
2478 $this->info->tempuser->description = $this->getContents();
2481 $this->info->tempuser->mailformat = $this->getContents();
2484 $this->info->tempuser->maildisplay = $this->getContents();
2487 $this->info->tempuser->htmleditor = $this->getContents();
2489 case "AUTOSUBSCRIBE":
2490 $this->info->tempuser->autosubscribe = $this->getContents();
2492 case "TIMEMODIFIED":
2493 $this->info->tempuser->timemodified = $this->getContents();
2497 if ($this->level == 6) {
2500 //We've finalized a role, get it
2501 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
2502 unset($this->info->temprole);
2504 case "USER_PREFERENCE":
2505 //We've finalized a user_preference, get it
2506 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
2507 unset($this->info->tempuserpreference);
2511 if ($this->level == 7) {
2514 $this->info->temprole->type = $this->getContents();
2517 $this->info->temprole->authority = $this->getContents();
2520 $this->info->temprole->tea_role = $this->getContents();
2523 $this->info->temprole->editall = $this->getContents();
2525 case "TIMEMODIFIED":
2526 $this->info->temprole->timemodified = $this->getContents();
2529 $this->info->temprole->timestart = $this->getContents();
2532 $this->info->temprole->timeend = $this->getContents();
2535 $this->info->temprole->time = $this->getContents();
2538 $this->info->temprole->timeaccess = $this->getContents();
2541 $this->info->tempuserpreference->name = $this->getContents();
2544 $this->info->tempuserpreference->value = $this->getContents();
2550 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
2551 //Speed up a lot (avoid parse all)
2552 if ($tagName == "USERS" and $this->level == 3) {
2553 $this->finished = true;
2558 $this->tree[$this->level] = "";
2560 $this->content = "";
2563 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
2564 function endElementQuestions($parser, $tagName) {
2565 //Check if we are into QUESTION_CATEGORIES zone
2566 if ($this->tree[3] == "QUESTION_CATEGORIES") {
2567 //if (trim($this->content)) //Debug
2568 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2569 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2570 //Acumulate data to info (content + close tag)
2571 //Reconvert: strip htmlchars again and trim to generate xml data
2572 if (!isset($this->temp)) {
2575 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
2576 //If we've finished a mod, xmlize it an save to db
2577 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
2578 //Prepend XML standard header to info gathered
2579 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
2580 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
2581 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
2582 $data = xmlize($xml_data,0);
2583 //echo strftime ("%X",time())."<p>"; //Debug
2584 //traverse_xmlize($data); //Debug
2585 //print_object ($GLOBALS['traverse_array']); //Debug
2586 //$GLOBALS['traverse_array']=""; //Debug
2587 //Now, save data to db. We'll use it later
2589 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
2591 $status = backup_putid($this->preferences->backup_unique_code,"quiz_categories",$category_id,
2593 //Create returning info
2594 $ret_info->id = $category_id;
2595 $this->info[] = $ret_info;
2601 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
2602 //Speed up a lot (avoid parse all)
2603 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
2604 $this->finished = true;
2608 $this->tree[$this->level] = "";
2610 $this->content = "";
2613 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
2614 function endElementScales($parser, $tagName) {
2615 //Check if we are into SCALES zone
2616 if ($this->tree[3] == "SCALES") {
2617 //if (trim($this->content)) //Debug
2618 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2619 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2620 //Acumulate data to info (content + close tag)
2621 //Reconvert: strip htmlchars again and trim to generate xml data
2622 if (!isset($this->temp)) {
2625 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
2626 //If we've finished a scale, xmlize it an save to db
2627 if (($this->level == 4) and ($tagName == "SCALE")) {
2628 //Prepend XML standard header to info gathered
2629 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
2630 //Call to xmlize for this portion of xml data (one SCALE)
2631 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
2632 $data = xmlize($xml_data,0);
2633 //echo strftime ("%X",time())."<p>"; //Debug
2634 //traverse_xmlize($data); //Debug
2635 //print_object ($GLOBALS['traverse_array']); //Debug
2636 //$GLOBALS['traverse_array']=""; //Debug
2637 //Now, save data to db. We'll use it later
2638 //Get id and from data
2639 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
2641 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
2643 //Create returning info
2644 $ret_info->id = $scale_id;
2645 $this->info[] = $ret_info;
2651 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
2652 //Speed up a lot (avoid parse all)
2653 if ($tagName == "SCALES" and $this->level == 3) {
2654 $this->finished = true;
2658 $this->tree[$this->level] = "";
2660 $this->content = "";
2663 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
2664 function endElementGroups($parser, $tagName) {
2665 //Check if we are into GROUPS zone
2666 if ($this->tree[3] == "GROUPS") {
2667 //if (trim($this->content)) //Debug
2668 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2669 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2670 //Acumulate data to info (content + close tag)
2671 //Reconvert: strip htmlchars again and trim to generate xml data
2672 if (!isset($this->temp)) {
2675 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
2676 //If we've finished a group, xmlize it an save to db
2677 if (($this->level == 4) and ($tagName == "GROUP")) {
2678 //Prepend XML standard header to info gathered
2679 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
2680 //Call to xmlize for this portion of xml data (one GROUP)
2681 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
2682 $data = xmlize($xml_data,0);
2683 //echo strftime ("%X",time())."<p>"; //Debug
2684 //traverse_xmlize($data); //Debug
2685 //print_object ($GLOBALS['traverse_array']); //Debug
2686 //$GLOBALS['traverse_array']=""; //Debug
2687 //Now, save data to db. We'll use it later
2688 //Get id and from data
2689 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
2691 $status = backup_putid($this->preferences->backup_unique_code,"group",$group_id,
2693 //Create returning info
2694 $ret_info->id = $group_id;
2695 $this->info[] = $ret_info;
2701 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
2702 //Speed up a lot (avoid parse all)
2703 if ($tagName == "GROUPS" and $this->level == 3) {
2704 $this->finished = true;
2708 $this->tree[$this->level] = "";
2710 $this->content = "";
2713 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
2714 function endElementEvents($parser, $tagName) {
2715 //Check if we are into EVENTS zone
2716 if ($this->tree[3] == "EVENTS") {
2717 //if (trim($this->content)) //Debug
2718 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2719 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2720 //Acumulate data to info (content + close tag)
2721 //Reconvert: strip htmlchars again and trim to generate xml data
2722 if (!isset($this->temp)) {
2725 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
2726 //If we've finished a event, xmlize it an save to db
2727 if (($this->level == 4) and ($tagName == "EVENT")) {
2728 //Prepend XML standard header to info gathered
2729 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
2730 //Call to xmlize for this portion of xml data (one EVENT)
2731 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
2732 $data = xmlize($xml_data,0);
2733 //echo strftime ("%X",time())."<p>"; //Debug
2734 //traverse_xmlize($data); //Debug
2735 //print_object ($GLOBALS['traverse_array']); //Debug
2736 //$GLOBALS['traverse_array']=""; //Debug
2737 //Now, save data to db. We'll use it later
2738 //Get id and from data
2739 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
2741 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
2743 //Create returning info
2744 $ret_info->id = $event_id;
2745 $this->info[] = $ret_info;
2751 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
2752 //Speed up a lot (avoid parse all)
2753 if ($tagName == "EVENTS" and $this->level == 3) {
2754 $this->finished = true;
2758 $this->tree[$this->level] = "";
2760 $this->content = "";
2763 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
2764 function endElementModules($parser, $tagName) {
2765 //Check if we are into MODULES zone
2766 if ($this->tree[3] == "MODULES") {
2767 //if (trim($this->content)) //Debug
2768 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2769 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2770 //Acumulate data to info (content + close tag)
2771 //Reconvert: strip htmlchars again and trim to generate xml data
2772 if (!isset($this->temp)) {
2775 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
2776 //If we've finished a mod, xmlize it an save to db
2777 if (($this->level == 4) and ($tagName == "MOD")) {
2778 //Prepend XML standard header to info gathered
2779 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
2780 //Call to xmlize for this portion of xml data (one MOD)
2781 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
2782 $data = xmlize($xml_data,0);
2783 //echo strftime ("%X",time())."<p>"; //Debug
2784 //traverse_xmlize($data); //Debug
2785 //print_object ($GLOBALS['traverse_array']); //Debug
2786 //$GLOBALS['traverse_array']=""; //Debug
2787 //Now, save data to db. We'll use it later
2788 //Get id and modtype from data
2789 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
2790 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
2791 //Only if we've selected to restore it
2792 if ($this->preferences->mods[$mod_type]->restore) {
2794 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
2796 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
2797 //Create returning info
2798 $ret_info->id = $mod_id;
2799 $ret_info->modtype = $mod_type;
2800 $this->info[] = $ret_info;
2807 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
2808 //Speed up a lot (avoid parse all)
2809 if ($tagName == "MODULES" and $this->level == 3) {
2810 $this->finished = true;
2814 $this->tree[$this->level] = "";
2816 $this->content = "";
2819 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
2820 function endElementLogs($parser, $tagName) {
2821 //Check if we are into LOGS zone
2822 if ($this->tree[3] == "LOGS") {
2823 //if (trim($this->content)) //Debug
2824 // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br>\n"; //Debug
2825 //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><br>\n"; //Debug
2826 //Acumulate data to info (content + close tag)
2827 //Reconvert: strip htmlchars again and trim to generate xml data
2828 if (!isset($this->temp)) {
2831 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
2832 //If we've finished a log, xmlize it an save to db
2833 if (($this->level == 4) and ($tagName == "LOG")) {
2834 //Prepend XML standard header to info gathered
2835 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
2836 //Call to xmlize for this portion of xml data (one LOG)
2837 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
2838 $data = xmlize($xml_data,0);
2839 //echo strftime ("%X",time())."<p>"; //Debug
2840 //traverse_xmlize($data); //Debug
2841 //print_object ($GLOBALS['traverse_array']); //Debug
2842 //$GLOBALS['traverse_array']=""; //Debug
2843 //Now, save data to db. We'll use it later
2844 //Get id and modtype from data
2845 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
2846 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
2847 //We only save log entries from backup file if they are:
2850 // - Module logs about one restored module
2851 if ($log_module == "course" or
2852 $log_module == "user" or
2853 $this->preferences->mods[$log_module]->restore) {