MDL-10364 Added the showcalculations preference and re-factored preferences_form...
[moodle-pu.git] / backup / restorelib.php
blobd38cfd08e6392e1af1f80e8f2913e8ca7057fdb2
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->enrolperiod = addslashes($course_header->course_enrolperiod);
600 $course->numsections = addslashes($course_header->course_numsections);
601 //$course->showrecent = addslashes($course_header->course_showrecent); INFO: This is out in 1.3
602 $course->maxbytes = addslashes($course_header->course_maxbytes);
603 $course->showreports = addslashes($course_header->course_showreports);
604 if (isset($course_header->course_groupmode)) {
605 $course->groupmode = addslashes($course_header->course_groupmode);
607 if (isset($course_header->course_groupmodeforce)) {
608 $course->groupmodeforce = addslashes($course_header->course_groupmodeforce);
610 $course->lang = addslashes($course_header->course_lang);
611 $course->theme = addslashes($course_header->course_theme);
612 $course->cost = addslashes($course_header->course_cost);
613 $course->currency = isset($course_header->course_currency)?addslashes($course_header->course_currency):'';
614 $course->marker = addslashes($course_header->course_marker);
615 $course->visible = addslashes($course_header->course_visible);
616 $course->hiddensections = addslashes($course_header->course_hiddensections);
617 $course->timecreated = addslashes($course_header->course_timecreated);
618 $course->timemodified = addslashes($course_header->course_timemodified);
619 $course->metacourse = addslashes($course_header->course_metacourse);
620 //Calculate sortorder field
621 $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
622 FROM ' . $CFG->prefix . 'course
623 WHERE category=' . $course->category);
624 if (!empty($sortmax->max)) {
625 $course->sortorder = $sortmax->max + 1;
626 unset($sortmax);
627 } else {
628 $course->sortorder = 100;
631 //Now, recode some languages (Moodle 1.5)
632 if ($course->lang == 'ma_nt') {
633 $course->lang = 'mi_nt';
636 //Disable course->metacourse if avoided in restore config
637 if (!$restore->metacourse) {
638 $course->metacourse = 0;
641 //Check if the theme exists in destination server
642 $themes = get_list_of_themes();
643 if (!in_array($course->theme, $themes)) {
644 $course->theme = '';
647 //Now insert the record
648 $newid = insert_record("course",$course);
649 if ($newid) {
650 //save old and new course id
651 backup_putid ($restore->backup_unique_code,"course",$course_header->course_id,$newid);
652 //Replace old course_id in course_header
653 $course_header->course_id = $newid;
654 } else {
655 $status = false;
659 return $status;
663 * Returns the best question category (id) found to restore one
664 * question category from a backup file. Works by stamp (since Moodle 1.1)
665 * or by name (for older versions).
667 * @param object $cat the question_categories record to be searched
668 * @param integer $courseid the course where we are restoring
669 * @return integer the id of a existing question_category or 0 (not found)
671 function restore_get_best_question_category($cat, $courseid) {
673 $found = 0;
675 //Decide how to work (by stamp or name)
676 if ($cat->stamp) {
677 $searchfield = 'stamp';
678 $searchvalue = $cat->stamp;
679 } else {
680 $searchfield = 'name';
681 $searchvalue = $cat->name;
684 //First shot. Try to get the category from the course being restored
685 if ($fcat = get_record('question_categories','course',$courseid,$searchfield,$searchvalue)) {
686 $found = $fcat->id;
687 //Second shot. Try to obtain any concordant category and check its publish status and editing rights
688 } else if ($fcats = get_records('question_categories', $searchfield, $searchvalue, 'id', 'id, publish, course')) {
689 foreach ($fcats as $fcat) {
690 if ($fcat->publish == 1 && has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $fcat->course))) {
691 $found = $fcat->id;
692 break;
697 return $found;
700 //This function creates all the block stuff when restoring courses
701 //It calls selectively to restore_create_block_instances() for 1.5
702 //and above backups. Upwards compatible with old blocks.
703 function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) {
705 $status = true;
707 delete_records('block_instance', 'pageid', $restore->course_id, 'pagetype', PAGE_COURSE_VIEW);
708 if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5
709 if (empty($blockinfo)) {
710 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
711 $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id);
712 blocks_repopulate_page($newpage);
713 } else {
714 // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup
715 $blockrecords = get_records_select('block', '', '', 'name, id');
716 $temp_blocks_l = array();
717 $temp_blocks_r = array();
718 @list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo);
719 $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r));
720 foreach($temp_blocks as $blockposition => $blocks) {
721 $blockweight = 0;
722 foreach($blocks as $blockname) {
723 if(!isset($blockrecords[$blockname])) {
724 // We don't know anything about this block!
725 continue;
727 $blockinstance = new stdClass;
728 // Remove any - prefix before doing the name-to-id mapping
729 if(substr($blockname, 0, 1) == '-') {
730 $blockname = substr($blockname, 1);
731 $blockinstance->visible = 0;
732 } else {
733 $blockinstance->visible = 1;
735 $blockinstance->blockid = $blockrecords[$blockname]->id;
736 $blockinstance->pageid = $restore->course_id;
737 $blockinstance->pagetype = PAGE_COURSE_VIEW;
738 $blockinstance->position = $blockposition;
739 $blockinstance->weight = $blockweight;
740 if(!$status = insert_record('block_instance', $blockinstance)) {
741 $status = false;
743 ++$blockweight;
747 } else if($backup_block_format == 'instances') {
748 $status = restore_create_block_instances($restore,$xml_file);
751 return $status;
755 //This function creates all the block_instances from xml when restoring in a
756 //new course
757 function restore_create_block_instances($restore,$xml_file) {
759 $status = true;
760 //Check it exists
761 if (!file_exists($xml_file)) {
762 $status = false;
764 //Get info from xml
765 if ($status) {
766 $info = restore_read_xml_blocks($xml_file);
769 if(empty($info->instances)) {
770 return $status;
773 // First of all, iterate over the blocks to see which distinct pages we have
774 // in our hands and arrange the blocks accordingly.
775 $pageinstances = array();
776 foreach($info->instances as $instance) {
778 //pagetype and pageid black magic, we have to handle the case of blocks for the
779 //course, blocks from other pages in that course etc etc etc.
781 if($instance->pagetype == PAGE_COURSE_VIEW) {
782 // This one's easy...
783 $instance->pageid = $restore->course_id;
785 else {
786 $parts = explode('-', $instance->pagetype);
787 if($parts[0] == 'mod') {
788 if(!$restore->mods[$parts[1]]->restore) {
789 continue;
791 $getid = backup_getid($restore->backup_unique_code, $parts[1], $instance->pageid);
792 $instance->pageid = $getid->new_id;
794 else {
795 // Not invented here ;-)
796 continue;
800 if(!isset($pageinstances[$instance->pagetype])) {
801 $pageinstances[$instance->pagetype] = array();
803 if(!isset($pageinstances[$instance->pagetype][$instance->pageid])) {
804 $pageinstances[$instance->pagetype][$instance->pageid] = array();
807 $pageinstances[$instance->pagetype][$instance->pageid][] = $instance;
810 $blocks = get_records_select('block', '', '', 'name, id, multiple');
812 // For each type of page we have restored
813 foreach($pageinstances as $thistypeinstances) {
815 // For each page id of that type
816 foreach($thistypeinstances as $thisidinstances) {
818 $addedblocks = array();
819 $maxweights = array();
821 // For each block instance in that page
822 foreach($thisidinstances as $instance) {
824 if(!isset($blocks[$instance->name])) {
825 //We are trying to restore a block we don't have...
826 continue;
829 //If we have already added this block once and multiples aren't allowed, disregard it
830 if(!$blocks[$instance->name]->multiple && isset($addedblocks[$instance->name])) {
831 continue;
834 //If its the first block we add to a new position, start weight counter equal to 0.
835 if(empty($maxweights[$instance->position])) {
836 $maxweights[$instance->position] = 0;
839 //If the instance weight is greater than the weight counter (we skipped some earlier
840 //blocks most probably), bring it back in line.
841 if($instance->weight > $maxweights[$instance->position]) {
842 $instance->weight = $maxweights[$instance->position];
845 //Add this instance
846 $instance->blockid = $blocks[$instance->name]->id;
848 if ($newid = insert_record('block_instance', $instance)) {
849 if (!empty($instance->id)) { // this will only be set if we come from 1.7 and above backups
850 backup_putid ($restore->backup_unique_code,"block_instance",$instance->id,$newid);
852 } else {
853 $status = false;
854 break;
857 //Get an object for the block and tell it it's been restored so it can update dates
858 //etc. if necessary
859 $blockobj=block_instance($instance->name,$instance);
860 $blockobj->after_restore($restore);
862 //Now we can increment the weight counter
863 ++$maxweights[$instance->position];
865 //Keep track of block types we have already added
866 $addedblocks[$instance->name] = true;
872 return $status;
875 //This function creates all the course_sections and course_modules from xml
876 //when restoring in a new course or simply checks sections and create records
877 //in backup_ids when restoring in a existing course
878 function restore_create_sections(&$restore, $xml_file) {
880 global $CFG,$db;
882 $status = true;
883 //Check it exists
884 if (!file_exists($xml_file)) {
885 $status = false;
887 //Get info from xml
888 if ($status) {
889 $info = restore_read_xml_sections($xml_file);
891 //Put the info in the DB, recoding ids and saving the in backup tables
893 $sequence = "";
895 if ($info) {
896 //For each, section, save it to db
897 foreach ($info->sections as $key => $sect) {
898 $sequence = "";
899 $section->course = $restore->course_id;
900 $section->section = $sect->number;
901 $section->summary = restore_decode_absolute_links(addslashes($sect->summary));
902 $section->visible = $sect->visible;
903 $section->sequence = "";
904 //Now calculate the section's newid
905 $newid = 0;
906 if ($restore->restoreto == 2) {
907 //Save it to db (only if restoring to new course)
908 $newid = insert_record("course_sections",$section);
909 } else {
910 //Get section id when restoring in existing course
911 $rec = get_record("course_sections","course",$restore->course_id,
912 "section",$section->section);
913 //If that section doesn't exist, get section 0 (every mod will be
914 //asigned there
915 if(!$rec) {
916 $rec = get_record("course_sections","course",$restore->course_id,
917 "section","0");
919 //New check. If section 0 doesn't exist, insert it here !!
920 //Teorically this never should happen but, in practice, some users
921 //have reported this issue.
922 if(!$rec) {
923 $zero_sec->course = $restore->course_id;
924 $zero_sec->section = 0;
925 $zero_sec->summary = "";
926 $zero_sec->sequence = "";
927 $newid = insert_record("course_sections",$zero_sec);
928 $rec->id = $newid;
929 $rec->sequence = "";
931 $newid = $rec->id;
932 $sequence = $rec->sequence;
934 if ($newid) {
935 //save old and new section id
936 backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
937 } else {
938 $status = false;
940 //If all is OK, go with associated mods
941 if ($status) {
942 //If we have mods in the section
943 if (!empty($sect->mods)) {
944 //For each mod inside section
945 foreach ($sect->mods as $keym => $mod) {
946 // Yu: This part is called repeatedly for every instance,
947 // so it is necessary to set the granular flag and check isset()
948 // when the first instance of this type of mod is processed.
950 //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
952 if (!isset($restore->mods[$mod->type]->granular)) {
953 if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
954 // This defines whether we want to restore specific
955 // instances of the modules (granular restore), or
956 // whether we don't care and just want to restore
957 // all module instances (non-granular).
958 $restore->mods[$mod->type]->granular = true;
959 } else {
960 $restore->mods[$mod->type]->granular = false;
964 //Check if we've to restore this module (and instance)
965 if (!empty($restore->mods[$mod->type]->restore)) {
966 if (empty($restore->mods[$mod->type]->granular) // we don't care about per instance
967 || (array_key_exists($mod->instance,$restore->mods[$mod->type]->instances)
968 && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore))) {
970 //Get the module id from modules
971 $module = get_record("modules","name",$mod->type);
972 if ($module) {
973 $course_module->course = $restore->course_id;
974 $course_module->module = $module->id;
975 $course_module->section = $newid;
976 $course_module->added = $mod->added;
977 $course_module->score = $mod->score;
978 $course_module->indent = $mod->indent;
979 $course_module->visible = $mod->visible;
980 if (isset($mod->groupmode)) {
981 $course_module->groupmode = $mod->groupmode;
983 $course_module->instance = 0;
984 //NOTE: The instance (new) is calculated and updated in db in the
985 // final step of the restore. We don't know it yet.
986 //print_object($course_module); //Debug
987 //Save it to db
989 $newidmod = insert_record("course_modules",$course_module);
990 if ($newidmod) {
991 //save old and new module id
992 //In the info field, we save the original instance of the module
993 //to use it later
994 backup_putid ($restore->backup_unique_code,"course_modules",
995 $keym,$newidmod,$mod->instance);
997 $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
998 } else {
999 $status = false;
1001 //Now, calculate the sequence field
1002 if ($status) {
1003 if ($sequence) {
1004 $sequence .= ",".$newidmod;
1005 } else {
1006 $sequence = $newidmod;
1009 } else {
1010 $status = false;
1017 //If all is OK, update sequence field in course_sections
1018 if ($status) {
1019 if (isset($sequence)) {
1020 $update_rec->id = $newid;
1021 $update_rec->sequence = $sequence;
1022 $status = update_record("course_sections",$update_rec);
1026 } else {
1027 $status = false;
1029 return $status;
1032 //Called to set up any course-format specific data that may be in the file
1033 function restore_set_format_data($restore,$xml_file) {
1034 global $CFG,$db;
1036 $status = true;
1037 //Check it exists
1038 if (!file_exists($xml_file)) {
1039 return false;
1041 //Load data from XML to info
1042 if(!($info = restore_read_xml_formatdata($xml_file))) {
1043 return false;
1046 //Process format data if there is any
1047 if (isset($info->format_data)) {
1048 if(!$format=get_field('course','format','id',$restore->course_id)) {
1049 return false;
1051 // If there was any data then it must have a restore method
1052 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
1053 if(!file_exists($file)) {
1054 return false;
1056 require_once($file);
1057 $function=$format.'_restore_format_data';
1058 if(!function_exists($function)) {
1059 return false;
1061 return $function($restore,$info->format_data);
1064 // If we got here then there's no data, but that's cool
1065 return true;
1068 //This function creates all the metacourse data from xml, notifying
1069 //about each incidence
1070 function restore_create_metacourse($restore,$xml_file) {
1072 global $CFG,$db;
1074 $status = true;
1075 //Check it exists
1076 if (!file_exists($xml_file)) {
1077 $status = false;
1079 //Get info from xml
1080 if ($status) {
1081 //Load data from XML to info
1082 $info = restore_read_xml_metacourse($xml_file);
1085 //Process info about metacourse
1086 if ($status and $info) {
1087 //Process child records
1088 if (!empty($info->childs)) {
1089 foreach ($info->childs as $child) {
1090 $dbcourse = false;
1091 $dbmetacourse = false;
1092 //Check if child course exists in destination server
1093 //(by id in the same server or by idnumber and shortname in other server)
1094 if ($restore->original_wwwroot == $CFG->wwwroot) {
1095 //Same server, lets see by id
1096 $dbcourse = get_record('course','id',$child->id);
1097 } else {
1098 //Different server, lets see by idnumber and shortname, and only ONE record
1099 $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1100 if ($dbcount == 1) {
1101 $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
1104 //If child course has been found, insert data
1105 if ($dbcourse) {
1106 $dbmetacourse->child_course = $dbcourse->id;
1107 $dbmetacourse->parent_course = $restore->course_id;
1108 $status = insert_record ('course_meta',$dbmetacourse);
1109 } else {
1110 //Child course not found, notice!
1111 if (!defined('RESTORE_SILENTLY')) {
1112 echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
1116 //Now, recreate student enrolments...
1117 sync_metacourse($restore->course_id);
1119 //Process parent records
1120 if (!empty($info->parents)) {
1121 foreach ($info->parents as $parent) {
1122 $dbcourse = false;
1123 $dbmetacourse = false;
1124 //Check if parent course exists in destination server
1125 //(by id in the same server or by idnumber and shortname in other server)
1126 if ($restore->original_wwwroot == $CFG->wwwroot) {
1127 //Same server, lets see by id
1128 $dbcourse = get_record('course','id',$parent->id);
1129 } else {
1130 //Different server, lets see by idnumber and shortname, and only ONE record
1131 $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1132 if ($dbcount == 1) {
1133 $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
1136 //If parent course has been found, insert data if it is a metacourse
1137 if ($dbcourse) {
1138 if ($dbcourse->metacourse) {
1139 $dbmetacourse->parent_course = $dbcourse->id;
1140 $dbmetacourse->child_course = $restore->course_id;
1141 $status = insert_record ('course_meta',$dbmetacourse);
1142 //Now, recreate student enrolments in parent course
1143 sync_metacourse($dbcourse->id);
1144 } else {
1145 //Parent course isn't metacourse, notice!
1146 if (!defined('RESTORE_SILENTLY')) {
1147 echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1150 } else {
1151 //Parent course not found, notice!
1152 if (!defined('RESTORE_SILENTLY')) {
1153 echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
1160 return $status;
1163 //This function creates all the gradebook data from xml, notifying
1164 //about each incidence
1165 function restore_create_gradebook($restore,$xml_file) {
1167 global $CFG,$db;
1169 $status = true;
1170 //Check it exists
1171 if (!file_exists($xml_file)) {
1172 return false;
1175 // Get info from xml
1176 // info will contain the number of record to process
1177 $info = restore_read_xml_gradebook($restore, $xml_file);
1179 // If we have info, then process
1180 if ($info <= 0) {
1181 return $status;
1184 // Count how many we have
1185 $categoriescount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories');
1186 $itemscount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items');
1187 $outcomecount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes');
1189 // we need to know if all grade items that were backed up are being restored
1190 // if that is not the case, we do not restore grade categories nor gradeitems of category type
1191 // i.e. the aggregated grades of that category
1193 $restoreall = true; // set to false if any grade_item is not selected/restored
1195 if ($recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'", "old_id", "old_id, old_id")) {
1196 foreach ($recs as $rec) {
1199 if ($data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id)) {
1201 $info = $data->info;
1202 // do not restore if this grade_item is a mod, and
1203 $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1206 if ($itemtype == 'mod') {
1208 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1209 $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1210 if (!restore_userdata_selected($restore, $itemmodule, $iteminstance)) {
1211 // module instance not selected when restored using granular
1212 // we are not restoring all grade items, set flag to false
1213 // so that we do not process grade categories and related grade items/grades
1214 $restoreall = false;
1215 break;
1222 // return if nothing to restore
1223 if (!$itemscount && !$categoriescount && !outcomecount) {
1224 return $status;
1227 // Start ul
1228 if (!defined('RESTORE_SILENTLY')) {
1229 echo '<ul>';
1231 // Number of records to get in every chunk
1232 $recordset_size = 2;
1233 // Flag to mark if we must continue
1234 $continue = true;
1236 // Process categories
1237 if ($categoriescount && $continue && $restoreall) {
1238 if (!defined('RESTORE_SILENTLY')) {
1239 echo '<li>'.get_string('gradecategories','grades').'</li>';
1241 $counter = 0;
1242 while ($counter < $categoriescount) {
1243 // Fetch recordset_size records in each iteration
1244 $recs = get_records_select("backup_ids","table_name = 'grade_categories' AND backup_code = '$restore->backup_unique_code'",
1245 "old_id",
1246 "old_id, old_id",
1247 $counter,
1248 $recordset_size);
1249 if ($recs) {
1250 foreach ($recs as $rec) {
1251 // Get the full record from backup_ids
1252 $data = backup_getid($restore->backup_unique_code,'grade_categories',$rec->old_id);
1253 if ($data) {
1254 // Now get completed xmlized object
1255 $info = $data->info;
1256 //traverse_xmlize($info); //Debug
1257 //print_object ($GLOBALS['traverse_array']); //Debug
1258 //$GLOBALS['traverse_array']=""; //Debug
1259 //Now build the GRADE_PREFERENCES record structure
1261 $dbrec->courseid = $restore->course_id;
1263 // get the new grade category parent
1264 if (!empty($info['GRADE_CATEGORY']['#']['PARENT']['0']['#'])) {
1265 $parent = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_CATEGORY']['#']['PARENT']['0']['#']));
1266 $dbrec->parent = $parent->new_id;
1269 $dbrec->fullname = backup_todb($info['GRADE_CATEGORY']['#']['FULLNAME']['0']['#']);
1270 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#']);
1271 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#']);
1272 $dbrec->droplow = backup_todb($info['GRADE_CATEGORY']['#']['DROPLOW']['0']['#']);
1273 $dbrec->hidden = backup_todb($info['GRADE_CATEGORY']['#']['HIDDEN']['0']['#']);
1275 //Structure is equal to db, insert record
1276 //if the fullname doesn't exist
1277 if (!$prerec = get_record('grade_categories','courseid',$dbrec->courseid,'fullname',$dbrec->fullname)) {
1278 $newid = insert_record('grade_categories',$dbrec);
1279 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$newid);
1280 // update this record so we can put in the right paths
1281 // this can only be done after we got the new id
1282 $dbrec->id = $newid;
1283 include_once($CFG->dirroot.'/lib/grade/grade_category.php');
1284 // rebuild the path, we need only parents info
1285 // the order of restoring should ensure that the parent and grandparent(s)
1286 // are already restored
1287 $dbrec->path = grade_category::build_path($dbrec);
1288 // this is not needed in the xml because
1289 // given this parent and grandparent(s) we can recalculate the depth
1290 $dbrec->depth = substr_count($dbrec->path, '/');
1291 update_record('grade_categories', $dbrec);
1292 } else {
1293 // if fullname already exists, we should keep the current grade category
1294 $status = backup_putid($restore->backup_unique_code,'grade_categories',$rec->old_id,$rec->oldid);
1297 //Increment counters
1298 $counter++;
1299 //Do some output
1300 if ($counter % 1 == 0) {
1301 if (!defined('RESTORE_SILENTLY')) {
1302 echo ".";
1303 if ($counter % 20 == 0) {
1304 echo "<br />";
1307 backup_flush(300);
1314 // process outcomes
1315 if ($outcomecount && $continue) {
1316 if (!defined('RESTORE_SILENTLY')) {
1317 echo '<li>'.get_string('gradeoutcomes','grades').'</li>';
1319 $counter = 0;
1320 while ($counter < $outcomecount) {
1321 //Fetch recordset_size records in each iteration
1322 $recs = get_records_select("backup_ids","table_name = 'grade_outcomes' AND backup_code = '$restore->backup_unique_code'",
1323 "old_id",
1324 "old_id, old_id",
1325 $counter,
1326 $recordset_size);
1327 if ($recs) {
1328 foreach ($recs as $rec) {
1329 //Get the full record from backup_ids
1330 $data = backup_getid($restore->backup_unique_code,'grade_outcomes',$rec->old_id);
1331 if ($data) {
1332 //Now get completed xmlized object
1333 $info = $data->info;
1334 //traverse_xmlize($info); //Debug
1335 //print_object ($GLOBALS['traverse_array']); //Debug
1336 //$GLOBALS['traverse_array']=""; //Debug
1337 //Now build the GRADE_PREFERENCES record structure
1339 $dbrec->courseid = $restore->course_id;
1340 $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
1341 $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#']);
1343 if ($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']) {
1344 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#']));
1345 $derec->scaleid = $scale->new_id;
1348 $modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#']));
1349 $derec->usermodified = $modifier->new_id;
1351 // Structure is equal to db, insert record
1352 // If the shortname doesn't exist
1353 if (!$prerec = get_record('grade_outcomes','courseid',$dbrec->courseid,'shortname',$dbrec->shortname)) {
1354 $status = insert_record('grade_outcomes',$dbrec);
1357 //Increment counters
1358 $counter++;
1359 //Do some output
1360 if ($counter % 1 == 0) {
1361 if (!defined('RESTORE_SILENTLY')) {
1362 echo ".";
1363 if ($counter % 20 == 0) {
1364 echo "<br />";
1367 backup_flush(300);
1374 // Process grade items (grade_raw, grade_final, grade_history and grade_text)
1375 if ($itemscount && $continue) {
1376 if (!defined('RESTORE_SILENTLY')) {
1377 echo '<li>'.get_string('gradeitems','grades').'</li>';
1379 $counter = 0;
1380 $counteritems = 0;
1381 while ($counteritems < $itemscount) {
1383 //Fetch recordset_size records in each iteration
1384 $recs = get_records_select("backup_ids","table_name = 'grade_items' AND backup_code = '$restore->backup_unique_code'",
1385 "old_id",
1386 "old_id, old_id",
1387 $counteritems,
1388 $recordset_size);
1389 if ($recs) {
1390 foreach ($recs as $rec) {
1391 //Get the full record from backup_ids
1392 $data = backup_getid($restore->backup_unique_code,'grade_items',$rec->old_id);
1393 if ($data) {
1395 //Now get completed xmlized object
1396 $info = $data->info;
1397 //traverse_xmlize($info); //Debug
1398 //print_object ($GLOBALS['traverse_array']); //Debug
1399 //$GLOBALS['traverse_array']=""; //Debug
1401 $dbrec->courseid = $restore->course_id;
1403 if (!empty($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'])) {
1405 $category = backup_getid($restore->backup_unique_code,'grade_categories',backup_todb($info['GRADE_ITEM']['#']['CATEGORYID']['0']['#']));
1406 $dbrec->categoryid = $category->new_id;
1409 $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#']);
1410 $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
1411 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
1412 /// this needs to point to either the new mod id
1413 /// or the category id
1414 $iteminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
1416 // do not restore if this grade_item is a mod, and
1417 if ($dbrec->itemtype == 'mod') {
1419 if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
1420 // module instance not selected when restored using granular
1421 // skip this item
1422 $counteritems++;
1423 continue;
1426 // iteminstance should point to new mod
1428 $cm = backup_getid($restore->backup_unique_code,'course_modules', $iteminstance);
1429 $dbrec->iteminstance = $cm->new_id;
1431 } else if ($dbrec->itemtype == 'category') {
1432 // the item instance should point to the new grade category
1434 // only proceed if we are restoring all grade items
1435 if ($restoreall) {
1436 $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
1437 $dbrec->iteminstance = $category->new_id;
1438 } else {
1439 // otherwise we can safely ignore this grade item and subsequent
1440 // grade_raws, grade_finals etc
1441 continue;
1445 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#']);
1446 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#']);
1447 $dbrec->idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#']);
1448 $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#']);
1449 $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#']);
1450 $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#']);
1451 /// needs to be restored first
1453 if ($info['GRADE_ITEM']['#']['SCALEID']['0']['#']) {
1454 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#']));
1455 $derec->scaleid = $scale->new_id;
1458 /// needs to be restored first
1459 $dbrec->outcomeid = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
1460 $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#']);
1461 $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
1462 $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
1463 $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);
1464 $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);
1465 $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);
1467 /// if thesse 5 all match then we know this item is already in db
1470 $itemex = get_record_sql('SELECT id,id FROM grade_items
1471 WHERE courseid = '.$dbrec->courseid.'
1472 AND itemtype = '.$dbrec->itemtype.'
1473 AND itemmodule = '.$dbrec->itemmodule.'
1474 AND iteminstance = '.$dbrec->iteminstance.'
1475 AND itemnumber = '.$dbrec->itemnumber);
1477 if (!$itemex) {
1478 //Structure is equal to db, insert record
1479 $itemid = insert_record('grade_items',$dbrec);
1480 } else {
1481 //Simply remap category
1482 $itemid = $itemex->id;
1486 // always insert, since modules restored to existing courses are always inserted
1488 // get the current sortorder, add 1 to it and use that
1490 if ($lastitem = get_record_sql("SELECT sortorder, id FROM {$CFG->prefix}grade_items
1491 WHERE courseid = $restore->course_id
1492 ORDER BY sortorder DESC ", true)) {
1494 // we just need the first one
1495 $dbrec->sortorder = $lastitem->sortorder + 1;
1496 } else {
1497 // this is the first grade_item
1498 $dbrec->sortorder = 0;
1501 $itemid = insert_record('grade_items',$dbrec);
1503 /// now, restore grade_grades, grade_text, and grade_history
1504 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
1505 //Iterate over items
1506 for($i = 0; $i < sizeof($grades); $i++) {
1507 $ite_info = $grades[$i];
1508 //traverse_xmlize($ite_info);
1509 //Debug
1510 //print_object ($GLOBALS['traverse_array']); //Debug
1511 //$GLOBALS['traverse_array']=""; //Debug
1512 //Now build the GRADE_ITEM record structure
1513 $grade = new object();
1514 $grade->itemid = $itemid;
1515 $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));
1516 $grade->userid = $user->new_id;
1517 $grade->rawgrade = backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
1518 $grade->rawgrademax = backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
1519 $grade->rawgrademin = backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
1520 // need to find scaleid
1521 if ($ite_info['#']['RAWSCALEID']['0']['#']) {
1522 $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
1523 $grade->rawscaleid = $scale->new_id;
1525 $grade->finalgrade = backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
1526 $grade->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
1527 $grade->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
1528 $grade->locktime = backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
1529 $grade->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
1531 insert_record('grade_grades', $grade);
1533 $counter++;
1534 if ($counter % 20 == 0) {
1535 if (!defined('RESTORE_SILENTLY')) {
1536 echo ".";
1537 if ($counter % 400 == 0) {
1538 echo "<br />";
1541 backup_flush(300);
1547 /// processing grade_grades_text
1548 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']) && ($texts = $info['GRADE_ITEM']['#']['GRADE_GRADES_TEXT']['0']['#']['GRADE_TEXT'])) {
1549 //Iterate over items
1550 for($i = 0; $i < sizeof($texts); $i++) {
1551 $ite_info = $texts[$i];
1552 //traverse_xmlize($ite_info); //Debug
1553 //print_object ($GLOBALS['traverse_array']); //Debug
1554 //$GLOBALS['traverse_array']=""; //Debug
1555 $grade = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($ite_info['#']['GRADEID']['0']['#']));
1556 $text->gradeid = $grade->new_id;
1557 $text->information = backup_todb($ite_info['#']['INFORMATION']['0']['#']);
1558 $text->informationformat = backup_todb($ite_info['#']['INFORMATIONFORMAT']['0']['#']);
1559 $text->feedback = backup_todb($ite_info['#']['FEEDBACK']['0']['#']);
1560 $text->feedbackformat = backup_todb($ite_info['#']['FEEDBACKFORMAT']['0']['#']);
1562 insert_record('grade_grades_text', $text);
1564 $counter++;
1565 if ($counter % 20 == 0) {
1566 if (!defined('RESTORE_SILENTLY')) {
1567 echo ".";
1568 if ($counter % 400 == 0) {
1569 echo "<br />";
1572 backup_flush(300);
1577 /// processing grade_history
1578 if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_HISTORY']['0']['#']) && ($histories = $info['GRADE_ITEM']['#']['GRADE_GRADES_HISTORY']['0']['#']['GRADE_HISTORY'])) {
1579 //Iterate over items
1580 for($i = 0; $i < sizeof($histories); $i++) {
1581 $ite_info = $histories[$i];
1582 //traverse_xmlize($ite_info); //Debug
1583 //print_object ($GLOBALS['traverse_array']); //Debug
1584 //$GLOBALS['traverse_array']=""; //Debug
1585 $history->itemid = $itemid;
1586 $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));
1587 $history->userid = $user->new_id;
1588 $history->oldgrade = backup_todb($ite_info['#']['OLDGRADE']['0']['#']);
1589 $history->newgrade = backup_todb($ite_info['#']['NEWGRADE']['0']['#']);
1590 $history->note = backup_todb($ite_info['#']['NOTE']['0']['#']);
1591 $history->howmodified = backup_todb($ite_info['#']['HOWMODIFIED']['0']['#']);
1592 $modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERMODIFIED']['0']['#']));
1593 $history->usermodified = $modifier->new_id;
1594 insert_record('grade_history', $history);
1596 $counter++;
1597 if ($counter % 20 == 0) {
1598 if (!defined('RESTORE_SILENTLY')) {
1599 echo ".";
1600 if ($counter % 400 == 0) {
1601 echo "<br />";
1604 backup_flush(300);
1609 $counteritems++; // increment item count
1616 if (!defined('RESTORE_SILENTLY')) {
1617 //End ul
1618 echo '</ul>';
1620 return $status;
1623 //This function creates all the user, user_students, user_teachers
1624 //user_course_creators and user_admins from xml
1625 function restore_create_users($restore,$xml_file) {
1627 global $CFG, $db;
1629 $status = true;
1630 //Check it exists
1631 if (!file_exists($xml_file)) {
1632 $status = false;
1634 //Get info from xml
1635 if ($status) {
1636 //info will contain the old_id of every user
1637 //in backup_ids->info will be the real info (serialized)
1638 $info = restore_read_xml_users($restore,$xml_file);
1641 //Now, get evey user_id from $info and user data from $backup_ids
1642 //and create the necessary records (users, user_students, user_teachers
1643 //user_course_creators and user_admins
1644 if (!empty($info->users)) {
1645 // Grab mnethosts keyed by wwwroot, to map to id
1646 $mnethosts = get_records('mnet_host', '', '',
1647 'wwwroot', 'wwwroot, id');
1649 $languages = get_list_of_languages();
1651 foreach ($info->users as $userid) {
1652 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
1653 $user = $rec->info;
1655 //Now, recode some languages (Moodle 1.5)
1656 if ($user->lang == 'ma_nt') {
1657 $user->lang = 'mi_nt';
1661 //If language does not exist here - use site default
1662 if (!array_key_exists($user->lang, $languages)) {
1663 $user->lang = $CFG->lang;
1666 //Check if it's admin and coursecreator
1667 $is_admin = !empty($user->roles['admin']);
1668 $is_coursecreator = !empty($user->roles['coursecreator']);
1670 //Check if it's teacher and student
1671 $is_teacher = !empty($user->roles['teacher']);
1672 $is_student = !empty($user->roles['student']);
1674 //Check if it's needed
1675 $is_needed = !empty($user->roles['needed']);
1677 //Calculate if it is a course user
1678 //Has role teacher or student or needed
1679 $is_course_user = ($is_teacher or $is_student or $is_needed);
1681 //Calculate mnethostid
1682 if (empty($user->mnethosturl) || $user->mnethosturl===$CFG->wwwroot) {
1683 $user->mnethostid = $CFG->mnet_localhost_id;
1684 } else {
1685 // fast url-to-id lookups
1686 if (isset($mnethosts[$user->mnethosturl])) {
1687 $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
1688 } else {
1689 // should not happen, as we check in restore_chech.php
1690 // but handle the error if it does
1691 error("This backup file contains external Moodle Network Hosts that are not configured locally.");
1694 unset($user->mnethosturl);
1696 //To store new ids created
1697 $newid=null;
1698 //check if it exists (by username) and get its id
1699 $user_exists = true;
1700 $user_data = get_record("user","username",addslashes($user->username),
1701 'mnethostid', $user->mnethostid);
1702 if (!$user_data) {
1703 $user_exists = false;
1704 } else {
1705 $newid = $user_data->id;
1707 //Flags to see if we have to create the user, roles and preferences
1708 $create_user = true;
1709 $create_roles = true;
1710 $create_preferences = true;
1712 //If we are restoring course users and it isn't a course user
1713 if ($restore->users == 1 and !$is_course_user) {
1714 //If only restoring course_users and user isn't a course_user, inform to $backup_ids
1715 $status = backup_putid($restore->backup_unique_code,"user",$userid,null,'notincourse');
1716 $create_user = false;
1717 $create_roles = false;
1718 $create_preferences = false;
1721 if ($user_exists and $create_user) {
1722 //If user exists mark its newid in backup_ids (the same than old)
1723 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,'exists');
1724 $create_user = false;
1727 //Here, if create_user, do it
1728 if ($create_user) {
1729 //Unset the id because it's going to be inserted with a new one
1730 unset ($user->id);
1731 //We addslashes to necessary fields
1732 $user->username = addslashes($user->username);
1733 $user->firstname = addslashes($user->firstname);
1734 $user->lastname = addslashes($user->lastname);
1735 $user->email = addslashes($user->email);
1736 $user->institution = addslashes($user->institution);
1737 $user->department = addslashes($user->department);
1738 $user->address = addslashes($user->address);
1739 $user->city = addslashes($user->city);
1740 $user->url = addslashes($user->url);
1741 $user->description = restore_decode_absolute_links(addslashes($user->description));
1743 //We need to analyse the AUTH field to recode it:
1744 // - if the field isn't set, we are in a pre 1.4 backup and we'll
1745 // use manual
1747 if (empty($user->auth)) {
1748 if ($CFG->registerauth == 'email') {
1749 $user->auth = 'email';
1750 } else {
1751 $user->auth = 'manual';
1755 //We need to process the POLICYAGREED field to recalculate it:
1756 // - if the destination site is different (by wwwroot) reset it.
1757 // - if the destination site is the same (by wwwroot), leave it unmodified
1759 if ($restore->original_wwwroot != $CFG->wwwroot) {
1760 $user->policyagreed = 0;
1761 } else {
1762 //Nothing to do, we are in the same server
1765 //Check if the theme exists in destination server
1766 $themes = get_list_of_themes();
1767 if (!in_array($user->theme, $themes)) {
1768 $user->theme = '';
1771 //We are going to create the user
1772 //The structure is exactly as we need
1773 $newid = insert_record ("user",$user);
1774 //Put the new id
1775 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,"new");
1778 //Here, if create_roles, do it as necessary
1779 if ($create_roles) {
1780 //Get the newid and current info from backup_ids
1781 $data = backup_getid($restore->backup_unique_code,"user",$userid);
1782 $newid = $data->new_id;
1783 $currinfo = $data->info.",";
1785 //Now, depending of the role, create records in user_studentes and user_teacher
1786 //and/or mark it in backup_ids
1788 if ($is_admin) {
1789 //If the record (user_admins) doesn't exists
1790 //Only put status in backup_ids
1791 $currinfo = $currinfo."admin,";
1792 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1794 if ($is_coursecreator) {
1795 //If the record (user_coursecreators) doesn't exists
1796 //Only put status in backup_ids
1797 $currinfo = $currinfo."coursecreator,";
1798 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1800 if ($is_needed) {
1801 //Only put status in backup_ids
1802 $currinfo = $currinfo."needed,";
1803 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1805 if ($is_teacher) {
1806 //If the record (teacher) doesn't exists
1807 //Put status in backup_ids
1808 $currinfo = $currinfo."teacher,";
1809 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1810 //Set course and user
1811 $user->roles['teacher']->course = $restore->course_id;
1812 $user->roles['teacher']->userid = $newid;
1814 //Need to analyse the enrol field
1815 // - if it isn't set, set it to $CFG->enrol
1816 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
1817 // - if we are in the same server (by wwwroot), maintain it unmodified.
1818 if (empty($user->roles['teacher']->enrol)) {
1819 $user->roles['teacher']->enrol = $CFG->enrol;
1820 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
1821 $user->roles['teacher']->enrol = $CFG->enrol;
1822 } else {
1823 //Nothing to do. Leave it unmodified
1826 $rolesmapping = $restore->rolesmapping;
1827 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1828 if ($user->roles['teacher']->editall) {
1829 role_assign($rolesmapping['defaultteacheredit'],
1830 $newid,
1832 $context->id,
1833 $user->roles['teacher']->timestart,
1834 $user->roles['teacher']->timeend,
1836 $user->roles['teacher']->enrol);
1838 // editting teacher
1839 } else {
1840 // non editting teacher
1841 role_assign($rolesmapping['defaultteacher'],
1842 $newid,
1844 $context->id,
1845 $user->roles['teacher']->timestart,
1846 $user->roles['teacher']->timeend,
1848 $user->roles['teacher']->enrol);
1851 if ($is_student) {
1853 //Put status in backup_ids
1854 $currinfo = $currinfo."student,";
1855 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1856 //Set course and user
1857 $user->roles['student']->course = $restore->course_id;
1858 $user->roles['student']->userid = $newid;
1860 //Need to analyse the enrol field
1861 // - if it isn't set, set it to $CFG->enrol
1862 // - if we are in a different server (by wwwroot), set it to $CFG->enrol
1863 // - if we are in the same server (by wwwroot), maintain it unmodified.
1864 if (empty($user->roles['student']->enrol)) {
1865 $user->roles['student']->enrol = $CFG->enrol;
1866 } else if ($restore->original_wwwroot != $CFG->wwwroot) {
1867 $user->roles['student']->enrol = $CFG->enrol;
1868 } else {
1869 //Nothing to do. Leave it unmodified
1871 $rolesmapping = $restore->rolesmapping;
1872 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
1874 role_assign($rolesmapping['defaultstudent'],
1875 $newid,
1877 $context->id,
1878 $user->roles['student']->timestart,
1879 $user->roles['student']->timeend,
1881 $user->roles['student']->enrol);
1884 if (!$is_course_user) {
1885 //If the record (user) doesn't exists
1886 if (!record_exists("user","id",$newid)) {
1887 //Put status in backup_ids
1888 $currinfo = $currinfo."user,";
1889 $status = backup_putid($restore->backup_unique_code,"user",$userid,$newid,$currinfo);
1894 //Here, if create_preferences, do it as necessary
1895 if ($create_preferences) {
1896 //echo "Checking for preferences of user ".$user->username."<br />"; //Debug
1897 //Get user new id from backup_ids
1898 $data = backup_getid($restore->backup_unique_code,"user",$userid);
1899 $newid = $data->new_id;
1900 if (isset($user->user_preferences)) {
1901 //echo "Preferences exist in backup file<br />"; //Debug
1902 foreach($user->user_preferences as $user_preference) {
1903 //echo $user_preference->name." = ".$user_preference->value."<br />"; //Debug
1904 //We check if that user_preference exists in DB
1905 if (!record_exists("user_preferences","userid",$newid,"name",$user_preference->name)) {
1906 //echo "Creating it<br />"; //Debug
1907 //Prepare the record and insert it
1908 $user_preference->userid = $newid;
1909 $status = insert_record("user_preferences",$user_preference);
1917 return $status;
1920 //This function creates all the structures messages and contacts
1921 function restore_create_messages($restore,$xml_file) {
1923 global $CFG;
1925 $status = true;
1926 //Check it exists
1927 if (!file_exists($xml_file)) {
1928 $status = false;
1930 //Get info from xml
1931 if ($status) {
1932 //info will contain the id and name of every table
1933 //(message, message_read and message_contacts)
1934 //in backup_ids->info will be the real info (serialized)
1935 $info = restore_read_xml_messages($restore,$xml_file);
1937 //If we have info, then process messages & contacts
1938 if ($info > 0) {
1939 //Count how many we have
1940 $unreadcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message');
1941 $readcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_read');
1942 $contactcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'message_contacts');
1943 if ($unreadcount || $readcount || $contactcount) {
1944 //Start ul
1945 if (!defined('RESTORE_SILENTLY')) {
1946 echo '<ul>';
1948 //Number of records to get in every chunk
1949 $recordset_size = 4;
1951 //Process unread
1952 if ($unreadcount) {
1953 if (!defined('RESTORE_SILENTLY')) {
1954 echo '<li>'.get_string('unreadmessages','message').'</li>';
1956 $counter = 0;
1957 while ($counter < $unreadcount) {
1958 //Fetch recordset_size records in each iteration
1959 $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);
1960 if ($recs) {
1961 foreach ($recs as $rec) {
1962 //Get the full record from backup_ids
1963 $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
1964 if ($data) {
1965 //Now get completed xmlized object
1966 $info = $data->info;
1967 //traverse_xmlize($info); //Debug
1968 //print_object ($GLOBALS['traverse_array']); //Debug
1969 //$GLOBALS['traverse_array']=""; //Debug
1970 //Now build the MESSAGE record structure
1971 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
1972 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
1973 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
1974 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
1975 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
1976 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
1977 //We have to recode the useridfrom field
1978 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
1979 if ($user) {
1980 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
1981 $dbrec->useridfrom = $user->new_id;
1983 //We have to recode the useridto field
1984 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
1985 if ($user) {
1986 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
1987 $dbrec->useridto = $user->new_id;
1989 //Check if the record doesn't exist in DB!
1990 $exist = get_record('message','useridfrom',$dbrec->useridfrom,
1991 'useridto', $dbrec->useridto,
1992 'timecreated',$dbrec->timecreated);
1993 if (!$exist) {
1994 //Not exist. Insert
1995 $status = insert_record('message',$dbrec);
1996 } else {
1997 //Duplicate. Do nothing
2000 //Do some output
2001 $counter++;
2002 if ($counter % 10 == 0) {
2003 if (!defined('RESTORE_SILENTLY')) {
2004 echo ".";
2005 if ($counter % 200 == 0) {
2006 echo "<br />";
2009 backup_flush(300);
2016 //Process read
2017 if ($readcount) {
2018 if (!defined('RESTORE_SILENTLY')) {
2019 echo '<li>'.get_string('readmessages','message').'</li>';
2021 $counter = 0;
2022 while ($counter < $readcount) {
2023 //Fetch recordset_size records in each iteration
2024 $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);
2025 if ($recs) {
2026 foreach ($recs as $rec) {
2027 //Get the full record from backup_ids
2028 $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
2029 if ($data) {
2030 //Now get completed xmlized object
2031 $info = $data->info;
2032 //traverse_xmlize($info); //Debug
2033 //print_object ($GLOBALS['traverse_array']); //Debug
2034 //$GLOBALS['traverse_array']=""; //Debug
2035 //Now build the MESSAGE_READ record structure
2036 $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
2037 $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
2038 $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
2039 $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
2040 $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
2041 $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
2042 $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
2043 $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
2044 //We have to recode the useridfrom field
2045 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
2046 if ($user) {
2047 //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug
2048 $dbrec->useridfrom = $user->new_id;
2050 //We have to recode the useridto field
2051 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
2052 if ($user) {
2053 //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug
2054 $dbrec->useridto = $user->new_id;
2056 //Check if the record doesn't exist in DB!
2057 $exist = get_record('message_read','useridfrom',$dbrec->useridfrom,
2058 'useridto', $dbrec->useridto,
2059 'timecreated',$dbrec->timecreated);
2060 if (!$exist) {
2061 //Not exist. Insert
2062 $status = insert_record('message_read',$dbrec);
2063 } else {
2064 //Duplicate. Do nothing
2067 //Do some output
2068 $counter++;
2069 if ($counter % 10 == 0) {
2070 if (!defined('RESTORE_SILENTLY')) {
2071 echo ".";
2072 if ($counter % 200 == 0) {
2073 echo "<br />";
2076 backup_flush(300);
2083 //Process contacts
2084 if ($contactcount) {
2085 if (!defined('RESTORE_SILENTLY')) {
2086 echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
2088 $counter = 0;
2089 while ($counter < $contactcount) {
2090 //Fetch recordset_size records in each iteration
2091 $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);
2092 if ($recs) {
2093 foreach ($recs as $rec) {
2094 //Get the full record from backup_ids
2095 $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
2096 if ($data) {
2097 //Now get completed xmlized object
2098 $info = $data->info;
2099 //traverse_xmlize($info); //Debug
2100 //print_object ($GLOBALS['traverse_array']); //Debug
2101 //$GLOBALS['traverse_array']=""; //Debug
2102 //Now build the MESSAGE_CONTACTS record structure
2103 $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
2104 $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
2105 $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
2106 //We have to recode the userid field
2107 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
2108 if ($user) {
2109 //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug
2110 $dbrec->userid = $user->new_id;
2112 //We have to recode the contactid field
2113 $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
2114 if ($user) {
2115 //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug
2116 $dbrec->contactid = $user->new_id;
2118 //Check if the record doesn't exist in DB!
2119 $exist = get_record('message_contacts','userid',$dbrec->userid,
2120 'contactid', $dbrec->contactid);
2121 if (!$exist) {
2122 //Not exist. Insert
2123 $status = insert_record('message_contacts',$dbrec);
2124 } else {
2125 //Duplicate. Do nothing
2128 //Do some output
2129 $counter++;
2130 if ($counter % 10 == 0) {
2131 if (!defined('RESTORE_SILENTLY')) {
2132 echo ".";
2133 if ($counter % 200 == 0) {
2134 echo "<br />";
2137 backup_flush(300);
2143 if (!defined('RESTORE_SILENTLY')) {
2144 //End ul
2145 echo '</ul>';
2151 return $status;
2154 //This function creates all the categories and questions
2155 //from xml
2156 function restore_create_questions($restore,$xml_file) {
2158 global $CFG, $db;
2160 $status = true;
2161 //Check it exists
2162 if (!file_exists($xml_file)) {
2163 $status = false;
2165 //Get info from xml
2166 if ($status) {
2167 //info will contain the old_id of every category
2168 //in backup_ids->info will be the real info (serialized)
2169 $info = restore_read_xml_questions($restore,$xml_file);
2171 //Now, if we have anything in info, we have to restore that
2172 //categories/questions
2173 if ($info) {
2174 if ($info !== true) {
2175 //Iterate over each category
2176 foreach ($info as $category) {
2177 //Skip empty categories (some backups can contain them)
2178 if (!empty($category->id)) {
2179 $status = restore_question_categories($category,$restore);
2183 //Now we have to recode the parent field of each restored category
2184 $categories = get_records_sql("SELECT old_id, new_id
2185 FROM {$CFG->prefix}backup_ids
2186 WHERE backup_code = $restore->backup_unique_code AND
2187 table_name = 'question_categories'");
2188 if ($categories) {
2189 foreach ($categories as $category) {
2190 $restoredcategory = get_record('question_categories','id',$category->new_id);
2191 $restoredcategory = addslashes_object($restoredcategory);
2192 if ($restoredcategory->parent != 0) {
2193 $idcat = backup_getid($restore->backup_unique_code,'question_categories',$restoredcategory->parent);
2194 if ($idcat->new_id) {
2195 $restoredcategory->parent = $idcat->new_id;
2196 } else {
2197 $restoredcategory->parent = 0;
2199 update_record('question_categories', $restoredcategory);
2204 } else {
2205 $status = false;
2207 return $status;
2210 //This function creates all the scales
2211 function restore_create_scales($restore,$xml_file) {
2213 global $CFG, $db;
2215 $status = true;
2216 //Check it exists
2217 if (!file_exists($xml_file)) {
2218 $status = false;
2220 //Get info from xml
2221 if ($status) {
2222 //scales will contain the old_id of every scale
2223 //in backup_ids->info will be the real info (serialized)
2224 $scales = restore_read_xml_scales($restore,$xml_file);
2226 //Now, if we have anything in scales, we have to restore that
2227 //scales
2228 if ($scales) {
2229 //Get admin->id for later use
2230 $admin = get_admin();
2231 $adminid = $admin->id;
2232 if ($scales !== true) {
2233 //Iterate over each scale
2234 foreach ($scales as $scale) {
2235 //Get record from backup_ids
2236 $data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
2237 //Init variables
2238 $create_scale = false;
2240 if ($data) {
2241 //Now get completed xmlized object
2242 $info = $data->info;
2243 //traverse_xmlize($info); //Debug
2244 //print_object ($GLOBALS['traverse_array']); //Debug
2245 //$GLOBALS['traverse_array']=""; //Debug
2247 //Now build the SCALE record structure
2248 $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']);
2249 $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']);
2250 $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']);
2251 $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']);
2252 $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
2253 $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
2255 //Now search if that scale exists (by scale field) in course 0 (Standar scale)
2256 //or in restore->course_id course (Personal scale)
2257 if ($sca->courseid == 0) {
2258 $course_to_search = 0;
2259 } else {
2260 $course_to_search = $restore->course_id;
2262 $sca_db = get_record("scale","scale",$sca->scale,"courseid",$course_to_search);
2263 //If it doesn't exist, create
2264 if (!$sca_db) {
2265 $create_scale = true;
2267 //If we must create the scale
2268 if ($create_scale) {
2269 //Me must recode the courseid if it's <> 0 (common scale)
2270 if ($sca->courseid != 0) {
2271 $sca->courseid = $restore->course_id;
2273 //We must recode the userid
2274 $user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
2275 if ($user) {
2276 $sca->userid = $user->new_id;
2277 } else {
2278 //Assign it to admin
2279 $sca->userid = $adminid;
2281 //The structure is equal to the db, so insert the scale
2282 $newid = insert_record ("scale",$sca);
2283 } else {
2284 //get current scale id
2285 $newid = $sca_db->id;
2287 if ($newid) {
2288 //We have the newid, update backup_ids
2289 backup_putid($restore->backup_unique_code,"scale",
2290 $scale->id, $newid);
2295 } else {
2296 $status = false;
2298 return $status;
2301 //This function creates all the groups
2302 function restore_create_groups($restore,$xml_file) {
2304 global $CFG, $db;
2306 $status = true;
2307 $status2 = true;
2308 //Check it exists
2309 if (!file_exists($xml_file)) {
2310 $status = false;
2312 //Get info from xml
2313 if ($status) {
2314 //groups will contain the old_id of every group
2315 //in backup_ids->info will be the real info (serialized)
2316 $groups = restore_read_xml_groups($restore,$xml_file);
2318 //Now, if we have anything in groups, we have to restore that
2319 //groups
2320 if ($groups) {
2321 if ($groups !== true) {
2322 //Iterate over each group
2323 foreach ($groups as $group) {
2324 //Get record from backup_ids
2325 $data = backup_getid($restore->backup_unique_code,"groups",$group->id);
2326 //Init variables
2327 $create_group = false;
2329 if ($data) {
2330 //Now get completed xmlized object
2331 $info = $data->info;
2332 //traverse_xmlize($info); //Debug
2333 //print_object ($GLOBALS['traverse_array']); //Debug
2334 //$GLOBALS['traverse_array']=""; //Debug
2335 //Now build the GROUP record structure
2336 $gro = new Object();
2337 ///$gro->courseid = backup_todb($info['GROUP']['#']['COURSEID']['0']['#']);
2338 $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
2339 $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
2340 if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) {
2341 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']);
2342 } else { //if (! isset($gro->enrolment)) {
2343 $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']);
2345 $gro->lang = backup_todb($info['GROUP']['#']['LANG']['0']['#']);
2346 $gro->theme = backup_todb($info['GROUP']['#']['THEME']['0']['#']);
2347 $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
2348 $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']);
2349 $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
2350 $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
2352 //Now search if that group exists (by name and description field) in
2353 //restore->course_id course
2354 $gro_db = groups_group_matches($restore->course_id, $gro->name, $gro->description);
2355 //If it doesn't exist, create
2356 if (!$gro_db) {
2357 $create_group = true;
2359 //If we must create the group
2360 if ($create_group) {
2361 //Me must recode the courseid to the restore->course_id
2362 $gro->courseid = $restore->course_id;
2364 //Check if the theme exists in destination server
2365 $themes = get_list_of_themes();
2366 if (!in_array($gro->theme, $themes)) {
2367 $gro->theme = '';
2370 //The structure is equal to the db, so insert the group
2371 $newid = groups_restore_group($restore->course_id, $gro);
2372 } else {
2373 //get current group id
2374 $newid = $gro_db->id;
2376 if ($newid) {
2377 //We have the newid, update backup_ids
2378 backup_putid($restore->backup_unique_code,"groups",
2379 $group->id, $newid);
2381 //Now restore members in the groups_members, only if
2382 //users are included
2383 if ($restore->users != 2) {
2384 $status2 = restore_create_groups_members($newid,$info,$restore);
2388 //Now, restore group_files
2389 if ($status && $status2) {
2390 $status2 = restore_group_files($restore);
2393 } else {
2394 $status = false;
2396 return ($status && $status2);
2399 //This function restores the groups_members
2400 function restore_create_groups_members($group_id,$info,$restore) {
2402 global $CFG;
2404 $status = true;
2406 if (! isset($info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'])) {
2407 //OK, some groups have no members.
2408 return $status;
2410 //Get the members array
2411 $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
2413 //Iterate over members
2414 for($i = 0; $i < sizeof($members); $i++) {
2415 $mem_info = $members[$i];
2416 //traverse_xmlize($mem_info); //Debug
2417 //print_object ($GLOBALS['traverse_array']); //Debug
2418 //$GLOBALS['traverse_array']=""; //Debug
2420 //Now, build the GROUPS_MEMBERS record structure
2421 $group_member = new Object();
2422 $group_member->groupid = $group_id;
2423 $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
2424 $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2426 //We have to recode the userid field
2427 $user = backup_getid($restore->backup_unique_code,"user",$group_member->userid);
2428 if ($user) {
2429 $group_member->userid = $user->new_id;
2432 //The structure is equal to the db, so insert the groups_members
2433 $newid = groups_restore_member($group_member);
2434 //Do some output
2435 if (($i+1) % 50 == 0) {
2436 if (!defined('RESTORE_SILENTLY')) {
2437 echo ".";
2438 if (($i+1) % 1000 == 0) {
2439 echo "<br />";
2442 backup_flush(300);
2445 if (!$newid) {
2446 $status = false;
2450 return $status;
2453 //This function creates all the groupings
2454 function restore_create_groupings($restore,$xml_file) {
2456 global $CFG, $db;
2458 $status = true;
2459 $status2 = true;
2460 //Check it exists
2461 if (!file_exists($xml_file)) {
2462 $status = false;
2464 //Get info from xml
2465 if ($status) {
2466 //groupings will contain the old_id of every group
2467 //in backup_ids->info will be the real info (serialized)
2468 $groupings = restore_read_xml_groupings($restore,$xml_file);
2470 //Now, if we have anything in groupings, we have to restore that grouping
2471 if ($groupings) {
2472 if ($groupings !== true) {
2473 //Iterate over each group
2474 foreach ($groupings as $grouping) {
2475 //Get record from backup_ids
2476 $data = backup_getid($restore->backup_unique_code,"groupings",$grouping->id);
2477 //Init variables
2478 $create_grouping = false;
2480 if ($data) {
2481 //Now get completed xmlized object
2482 $info = $data->info;
2483 //Now build the GROUPING record structure
2484 $gro = new Object();
2485 ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
2486 $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
2487 $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
2488 $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
2490 //Now search if that group exists (by name and description field) in
2491 //restore->course_id course
2492 $gro_db = groups_grouping_matches($restore->course_id, $gro->name, $gro->description);
2493 //If it doesn't exist, create
2494 if (!$gro_db) {
2495 $create_grouping = true;
2497 //If we must create the group
2498 if ($create_grouping) {
2500 //The structure is equal to the db, so insert the grouping TODO: RESTORE.
2501 $newid = groups_create_grouping($restore->course_id, $gro);
2502 } else {
2503 //get current group id
2504 $newid = $gro_db->id;
2506 if ($newid) {
2507 //We have the newid, update backup_ids
2508 backup_putid($restore->backup_unique_code,"groupings",
2509 $grouping->id, $newid);
2511 //Now restore links from groupings to groups
2512 $status2 = restore_create_groupings_groups($newid,$info,$restore);
2515 //(Now, restore grouping_files)
2517 } else {
2518 $status = false;
2520 return ($status && $status2);
2523 //This function restores the groups_members
2524 function restore_create_groupings_groups($grouping_id,$info,$restore) {
2526 global $CFG;
2528 $status = true;
2530 //Get the members array
2531 $members = $info['GROUPING']['#']['GROUPS']['0']['#']['GROUP'];
2533 //Iterate over members
2534 for($i = 0; $i < sizeof($members); $i++) {
2535 $mem_info = $members[$i];
2536 //Now, build the GROUPINGS_GROUPS record structure
2537 $gro_member = new Object();
2538 $gro_member->groupingid = $grouping_id;
2539 $gro_member->groupid = backup_todb($mem_info['#']['GROUPID']['0']['#']);
2540 $gro_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
2542 //We have to recode the userid field
2543 ///$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid);
2544 $group = backup_getid($restore->backup_unique_code,"group",$gro_member->groupid);
2545 if ($group) {
2546 $gro_member->groupid = $group->new_id;
2549 //The structure is equal to the db, so link the groups to the groupings. TODO: RESTORE.
2550 $newid = groups_add_group_to_grouping($gro_member->groupid, $gro_member->groupingid);
2551 //Do some output
2552 if (($i+1) % 50 == 0) {
2553 if (!defined('RESTORE_SILENTLY')) {
2554 echo ".";
2555 if (($i+1) % 1000 == 0) {
2556 echo "<br />";
2559 backup_flush(300);
2562 if (!$newid) {
2563 $status = false;
2567 return $status;
2570 //This function creates all the course events
2571 function restore_create_events($restore,$xml_file) {
2573 global $CFG, $db;
2575 $status = true;
2576 //Check it exists
2577 if (!file_exists($xml_file)) {
2578 $status = false;
2580 //Get info from xml
2581 if ($status) {
2582 //events will contain the old_id of every event
2583 //in backup_ids->info will be the real info (serialized)
2584 $events = restore_read_xml_events($restore,$xml_file);
2587 //Get admin->id for later use
2588 $admin = get_admin();
2589 $adminid = $admin->id;
2591 //Now, if we have anything in events, we have to restore that
2592 //events
2593 if ($events) {
2594 if ($events !== true) {
2595 //Iterate over each event
2596 foreach ($events as $event) {
2597 //Get record from backup_ids
2598 $data = backup_getid($restore->backup_unique_code,"event",$event->id);
2599 //Init variables
2600 $create_event = false;
2602 if ($data) {
2603 //Now get completed xmlized object
2604 $info = $data->info;
2605 //traverse_xmlize($info); //Debug
2606 //print_object ($GLOBALS['traverse_array']); //Debug
2607 //$GLOBALS['traverse_array']=""; //Debug
2609 //if necessary, write to restorelog and adjust date/time fields
2610 if ($restore->course_startdateoffset) {
2611 restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
2614 //Now build the EVENT record structure
2615 $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
2616 $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
2617 $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
2618 $eve->courseid = $restore->course_id;
2619 $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
2620 $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
2621 $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
2622 $eve->modulename = "";
2623 $eve->instance = 0;
2624 $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
2625 $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
2626 $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
2627 $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
2628 $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
2630 //Now search if that event exists (by name, description, timestart fields) in
2631 //restore->course_id course
2632 $eve_db = get_record_select("event",
2633 "courseid={$eve->courseid} AND name='{$eve->name}' AND description='{$eve->description}' AND timestart=$eve->timestart");
2634 //If it doesn't exist, create
2635 if (!$eve_db) {
2636 $create_event = true;
2638 //If we must create the event
2639 if ($create_event) {
2641 //We must recode the userid
2642 $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
2643 if ($user) {
2644 $eve->userid = $user->new_id;
2645 } else {
2646 //Assign it to admin
2647 $eve->userid = $adminid;
2650 //We must recode the repeatid if the event has it
2651 if (!empty($eve->repeatid)) {
2652 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
2653 if ($repeat_rec) { //Exists, so use it...
2654 $eve->repeatid = $repeat_rec->new_id;
2655 } else { //Doesn't exists, calculate the next and save it
2656 $oldrepeatid = $eve->repeatid;
2657 $max_rec = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event');
2658 $eve->repeatid = empty($max_rec) ? 1 : $max_rec->repeatid + 1;
2659 backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
2663 //We have to recode the groupid field
2664 $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
2665 if ($group) {
2666 $eve->groupid = $group->new_id;
2667 } else {
2668 //Assign it to group 0
2669 $eve->groupid = 0;
2672 //The structure is equal to the db, so insert the event
2673 $newid = insert_record ("event",$eve);
2674 } else {
2675 //get current event id
2676 $newid = $eve_db->id;
2678 if ($newid) {
2679 //We have the newid, update backup_ids
2680 backup_putid($restore->backup_unique_code,"event",
2681 $event->id, $newid);
2686 } else {
2687 $status = false;
2689 return $status;
2692 //This function decode things to make restore multi-site fully functional
2693 //It does this conversions:
2694 // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on)
2695 // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off)
2697 //Note: Inter-activities linking is being implemented as a final
2698 //step in the restore execution, because we need to have it
2699 //finished to know all the oldid, newid equivaleces
2700 function restore_decode_absolute_links($content) {
2702 global $CFG,$restore;
2704 //Now decode wwwroot and file.php calls
2705 $search = array ("$@FILEPHP@$");
2707 //Check for the status of the slasharguments config variable
2708 $slash = $CFG->slasharguments;
2710 //Build the replace string as needed
2711 if ($slash == 1) {
2712 $replace = array ($CFG->wwwroot."/file.php/".$restore->course_id);
2713 } else {
2714 $replace = array ($CFG->wwwroot."/file.php?file=/".$restore->course_id);
2717 $result = str_replace($search,$replace,$content);
2719 if ($result != $content && debugging()) { //Debug
2720 if (!defined('RESTORE_SILENTLY')) {
2721 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />'; //Debug
2723 } //Debug
2725 return $result;
2728 //This function restores the userfiles from the temp (user_files) directory to the
2729 //dataroot/users directory
2730 function restore_user_files($restore) {
2732 global $CFG;
2734 $status = true;
2736 $counter = 0;
2738 //First, we check to "users" exists and create is as necessary
2739 //in CFG->dataroot
2740 $dest_dir = $CFG->dataroot."/users";
2741 $status = check_dir_exists($dest_dir,true);
2743 //Now, we iterate over "user_files" records to check if that user dir must be
2744 //copied (and renamed) to the "users" dir.
2745 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
2746 //Check if directory exists
2747 if (is_dir($rootdir)) {
2748 $list = list_directories ($rootdir);
2749 if ($list) {
2750 //Iterate
2751 $counter = 0;
2752 foreach ($list as $dir) {
2753 //Look for dir like username in backup_ids
2754 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
2755 "table_name","user",
2756 "old_id",$dir);
2757 //If thar user exists in backup_ids
2758 if ($data) {
2759 //Only it user has been created now
2760 //or if it existed previously, but he hasn't image (see bug 1123)
2761 if ((strpos($data->info,"new") !== false) or
2762 (!check_dir_exists($dest_dir."/".$data->new_id,false))) {
2763 //Copy the old_dir to its new location (and name) !!
2764 //Only if destination doesn't exists
2765 if (!file_exists($dest_dir."/".$data->new_id)) {
2766 $status = backup_copy_file($rootdir."/".$dir,
2767 $dest_dir."/".$data->new_id,true);
2768 $counter ++;
2770 //Do some output
2771 if ($counter % 2 == 0) {
2772 if (!defined('RESTORE_SILENTLY')) {
2773 echo ".";
2774 if ($counter % 40 == 0) {
2775 echo "<br />";
2778 backup_flush(300);
2785 //If status is ok and whe have dirs created, returns counter to inform
2786 if ($status and $counter) {
2787 return $counter;
2788 } else {
2789 return $status;
2793 //This function restores the groupfiles from the temp (group_files) directory to the
2794 //dataroot/groups directory
2795 function restore_group_files($restore) {
2797 global $CFG;
2799 $status = true;
2801 $counter = 0;
2803 //First, we check to "groups" exists and create is as necessary
2804 //in CFG->dataroot
2805 $dest_dir = $CFG->dataroot.'/groups';
2806 $status = check_dir_exists($dest_dir,true);
2808 //Now, we iterate over "group_files" records to check if that user dir must be
2809 //copied (and renamed) to the "groups" dir.
2810 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/group_files";
2811 //Check if directory exists
2812 if (is_dir($rootdir)) {
2813 $list = list_directories ($rootdir);
2814 if ($list) {
2815 //Iterate
2816 $counter = 0;
2817 foreach ($list as $dir) {
2818 //Look for dir like groupid in backup_ids
2819 $data = get_record ("backup_ids","backup_code",$restore->backup_unique_code,
2820 "table_name","groups",
2821 "old_id",$dir);
2822 //If that group exists in backup_ids
2823 if ($data) {
2824 if (!file_exists($dest_dir."/".$data->new_id)) {
2825 $status = backup_copy_file($rootdir."/".$dir, $dest_dir."/".$data->new_id,true);
2826 $counter ++;
2828 //Do some output
2829 if ($counter % 2 == 0) {
2830 if (!defined('RESTORE_SILENTLY')) {
2831 echo ".";
2832 if ($counter % 40 == 0) {
2833 echo "<br />";
2836 backup_flush(300);
2842 //If status is ok and whe have dirs created, returns counter to inform
2843 if ($status and $counter) {
2844 return $counter;
2845 } else {
2846 return $status;
2850 //This function restores the course files from the temp (course_files) directory to the
2851 //dataroot/course_id directory
2852 function restore_course_files($restore) {
2854 global $CFG;
2856 $status = true;
2858 $counter = 0;
2860 //First, we check to "course_id" exists and create is as necessary
2861 //in CFG->dataroot
2862 $dest_dir = $CFG->dataroot."/".$restore->course_id;
2863 $status = check_dir_exists($dest_dir,true);
2865 //Now, we iterate over "course_files" records to check if that file/dir must be
2866 //copied to the "dest_dir" dir.
2867 $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/course_files";
2868 //Check if directory exists
2869 if (is_dir($rootdir)) {
2870 $list = list_directories_and_files ($rootdir);
2871 if ($list) {
2872 //Iterate
2873 $counter = 0;
2874 foreach ($list as $dir) {
2875 //Copy the dir to its new location
2876 //Only if destination file/dir doesn exists
2877 if (!file_exists($dest_dir."/".$dir)) {
2878 $status = backup_copy_file($rootdir."/".$dir,
2879 $dest_dir."/".$dir,true);
2880 $counter ++;
2882 //Do some output
2883 if ($counter % 2 == 0) {
2884 if (!defined('RESTORE_SILENTLY')) {
2885 echo ".";
2886 if ($counter % 40 == 0) {
2887 echo "<br />";
2890 backup_flush(300);
2895 //If status is ok and whe have dirs created, returns counter to inform
2896 if ($status and $counter) {
2897 return $counter;
2898 } else {
2899 return $status;
2904 //This function creates all the structures for every module in backup file
2905 //Depending what has been selected.
2906 function restore_create_modules($restore,$xml_file) {
2908 global $CFG;
2909 $status = true;
2910 //Check it exists
2911 if (!file_exists($xml_file)) {
2912 $status = false;
2914 //Get info from xml
2915 if ($status) {
2916 //info will contain the id and modtype of every module
2917 //in backup_ids->info will be the real info (serialized)
2918 $info = restore_read_xml_modules($restore,$xml_file);
2920 //Now, if we have anything in info, we have to restore that mods
2921 //from backup_ids (calling every mod restore function)
2922 if ($info) {
2923 if ($info !== true) {
2924 if (!defined('RESTORE_SILENTLY')) {
2925 echo '<ul>';
2927 //Iterate over each module
2928 foreach ($info as $mod) {
2929 if (empty($restore->mods[$mod->modtype]->granular) // We don't care about per instance, i.e. restore all instances.
2930 || (array_key_exists($mod->id,$restore->mods[$mod->modtype]->instances)
2931 && !empty($restore->mods[$mod->modtype]->instances[$mod->id]->restore))) {
2932 $modrestore = $mod->modtype."_restore_mods";
2933 if (function_exists($modrestore)) { //Debug
2934 $status = $status and $modrestore($mod,$restore); //bit operator & not reliable here!
2935 } else {
2936 //Something was wrong. Function should exist.
2937 $status = false;
2941 if (!defined('RESTORE_SILENTLY')) {
2942 echo '</ul>';
2945 } else {
2946 $status = false;
2948 return $status;
2951 //This function creates all the structures for every log in backup file
2952 //Depending what has been selected.
2953 function restore_create_logs($restore,$xml_file) {
2955 global $CFG,$db;
2957 //Number of records to get in every chunk
2958 $recordset_size = 4;
2959 //Counter, points to current record
2960 $counter = 0;
2961 //To count all the recods to restore
2962 $count_logs = 0;
2964 $status = true;
2965 //Check it exists
2966 if (!file_exists($xml_file)) {
2967 $status = false;
2969 //Get info from xml
2970 if ($status) {
2971 //count_logs will contain the number of logs entries to process
2972 //in backup_ids->info will be the real info (serialized)
2973 $count_logs = restore_read_xml_logs($restore,$xml_file);
2976 //Now, if we have records in count_logs, we have to restore that logs
2977 //from backup_ids. This piece of code makes calls to:
2978 // - restore_log_course() if it's a course log
2979 // - restore_log_user() if it's a user log
2980 // - restore_log_module() if it's a module log.
2981 //And all is segmented in chunks to allow large recordsets to be restored !!
2982 if ($count_logs > 0) {
2983 while ($counter < $count_logs) {
2984 //Get a chunk of records
2985 //Take old_id twice to avoid adodb limitation
2986 $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);
2987 //We have logs
2988 if ($logs) {
2989 //Iterate
2990 foreach ($logs as $log) {
2991 //Get the full record from backup_ids
2992 $data = backup_getid($restore->backup_unique_code,"log",$log->old_id);
2993 if ($data) {
2994 //Now get completed xmlized object
2995 $info = $data->info;
2996 //traverse_xmlize($info); //Debug
2997 //print_object ($GLOBALS['traverse_array']); //Debug
2998 //$GLOBALS['traverse_array']=""; //Debug
2999 //Now build the LOG record structure
3000 $dblog->time = backup_todb($info['LOG']['#']['TIME']['0']['#']);
3001 $dblog->userid = backup_todb($info['LOG']['#']['USERID']['0']['#']);
3002 $dblog->ip = backup_todb($info['LOG']['#']['IP']['0']['#']);
3003 $dblog->course = $restore->course_id;
3004 $dblog->module = backup_todb($info['LOG']['#']['MODULE']['0']['#']);
3005 $dblog->cmid = backup_todb($info['LOG']['#']['CMID']['0']['#']);
3006 $dblog->action = backup_todb($info['LOG']['#']['ACTION']['0']['#']);
3007 $dblog->url = backup_todb($info['LOG']['#']['URL']['0']['#']);
3008 $dblog->info = backup_todb($info['LOG']['#']['INFO']['0']['#']);
3009 //We have to recode the userid field
3010 $user = backup_getid($restore->backup_unique_code,"user",$dblog->userid);
3011 if ($user) {
3012 //echo "User ".$dblog->userid." to user ".$user->new_id."<br />"; //Debug
3013 $dblog->userid = $user->new_id;
3015 //We have to recode the cmid field (if module isn't "course" or "user")
3016 if ($dblog->module != "course" and $dblog->module != "user") {
3017 $cm = backup_getid($restore->backup_unique_code,"course_modules",$dblog->cmid);
3018 if ($cm) {
3019 //echo "Module ".$dblog->cmid." to module ".$cm->new_id."<br />"; //Debug
3020 $dblog->cmid = $cm->new_id;
3021 } else {
3022 $dblog->cmid = 0;
3025 //print_object ($dblog); //Debug
3026 //Now, we redirect to the needed function to make all the work
3027 if ($dblog->module == "course") {
3028 //It's a course log,
3029 $stat = restore_log_course($restore,$dblog);
3030 } elseif ($dblog->module == "user") {
3031 //It's a user log,
3032 $stat = restore_log_user($restore,$dblog);
3033 } else {
3034 //It's a module log,
3035 $stat = restore_log_module($restore,$dblog);
3039 //Do some output
3040 $counter++;
3041 if ($counter % 10 == 0) {
3042 if (!defined('RESTORE_SILENTLY')) {
3043 echo ".";
3044 if ($counter % 200 == 0) {
3045 echo "<br />";
3048 backup_flush(300);
3051 } else {
3052 //We never should arrive here
3053 $counter = $count_logs;
3054 $status = false;
3059 return $status;
3062 //This function inserts a course log record, calculating the URL field as necessary
3063 function restore_log_course($restore,$log) {
3065 $status = true;
3066 $toinsert = false;
3068 //echo "<hr />Before transformations<br />"; //Debug
3069 //print_object($log); //Debug
3070 //Depending of the action, we recode different things
3071 switch ($log->action) {
3072 case "view":
3073 $log->url = "view.php?id=".$log->course;
3074 $log->info = $log->course;
3075 $toinsert = true;
3076 break;
3077 case "guest":
3078 $log->url = "view.php?id=".$log->course;
3079 $toinsert = true;
3080 break;
3081 case "user report":
3082 //recode the info field (it's the user id)
3083 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3084 if ($user) {
3085 $log->info = $user->new_id;
3086 //Now, extract the mode from the url field
3087 $mode = substr(strrchr($log->url,"="),1);
3088 $log->url = "user.php?id=".$log->course."&user=".$log->info."&mode=".$mode;
3089 $toinsert = true;
3091 break;
3092 case "add mod":
3093 //Extract the course_module from the url field
3094 $cmid = substr(strrchr($log->url,"="),1);
3095 //recode the course_module to see it it has been restored
3096 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3097 if ($cm) {
3098 $cmid = $cm->new_id;
3099 //Extract the module name and the module id from the info field
3100 $modname = strtok($log->info," ");
3101 $modid = strtok(" ");
3102 //recode the module id to see if it has been restored
3103 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3104 if ($mod) {
3105 $modid = $mod->new_id;
3106 //Now I have everything so reconstruct url and info
3107 $log->info = $modname." ".$modid;
3108 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3109 $toinsert = true;
3112 break;
3113 case "update mod":
3114 //Extract the course_module from the url field
3115 $cmid = substr(strrchr($log->url,"="),1);
3116 //recode the course_module to see it it has been restored
3117 $cm = backup_getid($restore->backup_unique_code,"course_modules",$cmid);
3118 if ($cm) {
3119 $cmid = $cm->new_id;
3120 //Extract the module name and the module id from the info field
3121 $modname = strtok($log->info," ");
3122 $modid = strtok(" ");
3123 //recode the module id to see if it has been restored
3124 $mod = backup_getid($restore->backup_unique_code,$modname,$modid);
3125 if ($mod) {
3126 $modid = $mod->new_id;
3127 //Now I have everything so reconstruct url and info
3128 $log->info = $modname." ".$modid;
3129 $log->url = "../mod/".$modname."/view.php?id=".$cmid;
3130 $toinsert = true;
3133 break;
3134 case "delete mod":
3135 $log->url = "view.php?id=".$log->course;
3136 $toinsert = true;
3137 break;
3138 case "update":
3139 $log->url = "edit.php?id=".$log->course;
3140 $log->info = "";
3141 $toinsert = true;
3142 break;
3143 case "unenrol":
3144 //recode the info field (it's the user id)
3145 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3146 if ($user) {
3147 $log->info = $user->new_id;
3148 $log->url = "view.php?id=".$log->course;
3149 $toinsert = true;
3151 break;
3152 case "enrol":
3153 //recode the info field (it's the user id)
3154 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3155 if ($user) {
3156 $log->info = $user->new_id;
3157 $log->url = "view.php?id=".$log->course;
3158 $toinsert = true;
3160 break;
3161 case "editsection":
3162 //Extract the course_section from the url field
3163 $secid = substr(strrchr($log->url,"="),1);
3164 //recode the course_section to see if it has been restored
3165 $sec = backup_getid($restore->backup_unique_code,"course_sections",$secid);
3166 if ($sec) {
3167 $secid = $sec->new_id;
3168 //Now I have everything so reconstruct url and info
3169 $log->url = "editsection.php?id=".$secid;
3170 $toinsert = true;
3172 break;
3173 case "new":
3174 $log->url = "view.php?id=".$log->course;
3175 $log->info = "";
3176 $toinsert = true;
3177 break;
3178 case "recent":
3179 $log->url = "recent.php?id=".$log->course;
3180 $log->info = "";
3181 $toinsert = true;
3182 break;
3183 case "report log":
3184 $log->url = "report/log/index.php?id=".$log->course;
3185 $log->info = $log->course;
3186 $toinsert = true;
3187 break;
3188 case "report live":
3189 $log->url = "report/log/live.php?id=".$log->course;
3190 $log->info = $log->course;
3191 $toinsert = true;
3192 break;
3193 case "report outline":
3194 $log->url = "report/outline/index.php?id=".$log->course;
3195 $log->info = $log->course;
3196 $toinsert = true;
3197 break;
3198 case "report participation":
3199 $log->url = "report/participation/index.php?id=".$log->course;
3200 $log->info = $log->course;
3201 $toinsert = true;
3202 break;
3203 case "report stats":
3204 $log->url = "report/stats/index.php?id=".$log->course;
3205 $log->info = $log->course;
3206 $toinsert = true;
3207 break;
3208 default:
3209 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3210 break;
3213 //echo "After transformations<br />"; //Debug
3214 //print_object($log); //Debug
3216 //Now if $toinsert is set, insert the record
3217 if ($toinsert) {
3218 //echo "Inserting record<br />"; //Debug
3219 $status = insert_record("log",$log);
3221 return $status;
3224 //This function inserts a user log record, calculating the URL field as necessary
3225 function restore_log_user($restore,$log) {
3227 $status = true;
3228 $toinsert = false;
3230 //echo "<hr />Before transformations<br />"; //Debug
3231 //print_object($log); //Debug
3232 //Depending of the action, we recode different things
3233 switch ($log->action) {
3234 case "view":
3235 //recode the info field (it's the user id)
3236 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3237 if ($user) {
3238 $log->info = $user->new_id;
3239 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3240 $toinsert = true;
3242 break;
3243 case "change password":
3244 //recode the info field (it's the user id)
3245 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3246 if ($user) {
3247 $log->info = $user->new_id;
3248 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3249 $toinsert = true;
3251 break;
3252 case "login":
3253 //recode the info field (it's the user id)
3254 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3255 if ($user) {
3256 $log->info = $user->new_id;
3257 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3258 $toinsert = true;
3260 break;
3261 case "logout":
3262 //recode the info field (it's the user id)
3263 $user = backup_getid($restore->backup_unique_code,"user",$log->info);
3264 if ($user) {
3265 $log->info = $user->new_id;
3266 $log->url = "view.php?id=".$log->info."&course=".$log->course;
3267 $toinsert = true;
3269 break;
3270 case "view all":
3271 $log->url = "view.php?id=".$log->course;
3272 $log->info = "";
3273 $toinsert = true;
3274 case "update":
3275 //We split the url by ampersand char
3276 $first_part = strtok($log->url,"&");
3277 //Get data after the = char. It's the user being updated
3278 $userid = substr(strrchr($first_part,"="),1);
3279 //Recode the user
3280 $user = backup_getid($restore->backup_unique_code,"user",$userid);
3281 if ($user) {
3282 $log->info = "";
3283 $log->url = "view.php?id=".$user->new_id."&course=".$log->course;
3284 $toinsert = true;
3286 break;
3287 default:
3288 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
3289 break;
3292 //echo "After transformations<br />"; //Debug
3293 //print_object($log); //Debug
3295 //Now if $toinsert is set, insert the record
3296 if ($toinsert) {
3297 //echo "Inserting record<br />"; //Debug
3298 $status = insert_record("log",$log);
3300 return $status;
3303 //This function inserts a module log record, calculating the URL field as necessary
3304 function restore_log_module($restore,$log) {
3306 $status = true;
3307 $toinsert = false;
3309 //echo "<hr />Before transformations<br />"; //Debug
3310 //print_object($log); //Debug
3312 //Now we see if the required function in the module exists
3313 $function = $log->module."_restore_logs";
3314 if (function_exists($function)) {
3315 //Call the function
3316 $log = $function($restore,$log);
3317 //If everything is ok, mark the insert flag
3318 if ($log) {
3319 $toinsert = true;
3323 //echo "After transformations<br />"; //Debug
3324 //print_object($log); //Debug
3326 //Now if $toinsert is set, insert the record
3327 if ($toinsert) {
3328 //echo "Inserting record<br />"; //Debug
3329 $status = insert_record("log",$log);
3331 return $status;
3334 //This function adjusts the instance field into course_modules. It's executed after
3335 //modules restore. There, we KNOW the new instance id !!
3336 function restore_check_instances($restore) {
3338 global $CFG;
3340 $status = true;
3342 //We are going to iterate over each course_module saved in backup_ids
3343 $course_modules = get_records_sql("SELECT old_id,new_id
3344 FROM {$CFG->prefix}backup_ids
3345 WHERE backup_code = '$restore->backup_unique_code' AND
3346 table_name = 'course_modules'");
3347 if ($course_modules) {
3348 foreach($course_modules as $cm) {
3349 //Get full record, using backup_getids
3350 $cm_module = backup_getid($restore->backup_unique_code,"course_modules",$cm->old_id);
3351 //Now we are going to the REAL course_modules to get its type (field module)
3352 $module = get_record("course_modules","id",$cm_module->new_id);
3353 if ($module) {
3354 //We know the module type id. Get the name from modules
3355 $type = get_record("modules","id",$module->module);
3356 if ($type) {
3357 //We know the type name and the old_id. Get its new_id
3358 //from backup_ids. It's the instance !!!
3359 $instance = backup_getid($restore->backup_unique_code,$type->name,$cm_module->info);
3360 if ($instance) {
3361 //We have the new instance, so update the record in course_modules
3362 $module->instance = $instance->new_id;
3363 //print_object ($module); //Debug
3364 $status = update_record("course_modules",$module);
3365 } else {
3366 $status = false;
3368 } else {
3369 $status = false;
3371 } else {
3372 $status = false;
3378 return $status;
3381 //=====================================================================================
3382 //== ==
3383 //== XML Functions (SAX) ==
3384 //== ==
3385 //=====================================================================================
3387 //This is the class used to do all the xml parse
3388 class MoodleParser {
3390 var $level = 0; //Level we are
3391 var $counter = 0; //Counter
3392 var $tree = array(); //Array of levels we are
3393 var $content = ""; //Content under current level
3394 var $todo = ""; //What we hav to do when parsing
3395 var $info = ""; //Information collected. Temp storage. Used to return data after parsing.
3396 var $temp = ""; //Temp storage.
3397 var $preferences = ""; //Preferences about what to load !!
3398 var $finished = false; //Flag to say xml_parse to stop
3400 //This function is used to get the current contents property value
3401 //They are trimed (and converted from utf8 if needed)
3402 function getContents() {
3403 return trim($this->content);
3406 //This is the startTag handler we use where we are reading the info zone (todo="INFO")
3407 function startElementInfo($parser, $tagName, $attrs) {
3408 //Refresh properties
3409 $this->level++;
3410 $this->tree[$this->level] = $tagName;
3412 //Output something to avoid browser timeouts...
3413 backup_flush();
3415 //Check if we are into INFO zone
3416 //if ($this->tree[2] == "INFO") //Debug
3417 // 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 info zone (todo="INFO")
3421 function startElementRoles($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 INFO zone
3430 //if ($this->tree[2] == "INFO") //Debug
3431 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3435 //This is the startTag handler we use where we are reading the course header zone (todo="COURSE_HEADER")
3436 function startElementCourseHeader($parser, $tagName, $attrs) {
3437 //Refresh properties
3438 $this->level++;
3439 $this->tree[$this->level] = $tagName;
3441 //Output something to avoid browser timeouts...
3442 backup_flush();
3444 //Check if we are into COURSE_HEADER zone
3445 //if ($this->tree[3] == "HEADER") //Debug
3446 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3449 //This is the startTag handler we use where we are reading the blocks zone (todo="BLOCKS")
3450 function startElementBlocks($parser, $tagName, $attrs) {
3451 //Refresh properties
3452 $this->level++;
3453 $this->tree[$this->level] = $tagName;
3455 //Output something to avoid browser timeouts...
3456 backup_flush();
3458 //Check if we are into BLOCKS zone
3459 //if ($this->tree[3] == "BLOCKS") //Debug
3460 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3463 //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
3464 function startElementSections($parser, $tagName, $attrs) {
3465 //Refresh properties
3466 $this->level++;
3467 $this->tree[$this->level] = $tagName;
3469 //Output something to avoid browser timeouts...
3470 backup_flush();
3472 //Check if we are into SECTIONS zone
3473 //if ($this->tree[3] == "SECTIONS") //Debug
3474 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3477 //This is the startTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
3478 function startElementFormatData($parser, $tagName, $attrs) {
3479 //Refresh properties
3480 $this->level++;
3481 $this->tree[$this->level] = $tagName;
3483 //Output something to avoid browser timeouts...
3484 backup_flush();
3486 //Accumulate all the data inside this tag
3487 if (isset($this->tree[3]) && $this->tree[3] == "FORMATDATA") {
3488 if (!isset($this->temp)) {
3489 $this->temp = '';
3491 $this->temp .= "<".$tagName.">";
3494 //Check if we are into FORMATDATA zone
3495 //if ($this->tree[3] == "FORMATDATA") //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 metacourse zone (todo="METACOURSE")
3500 function startElementMetacourse($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 METACOURSE zone
3510 //if ($this->tree[3] == "METACOURSE") //Debug
3511 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3514 //This is the startTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
3515 function startElementGradebook($parser, $tagName, $attrs) {
3517 //Refresh properties
3518 $this->level++;
3519 $this->tree[$this->level] = $tagName;
3521 //Output something to avoid browser timeouts...
3522 backup_flush();
3524 //Check if we are into GRADEBOOK zone
3525 //if ($this->tree[3] == "GRADEBOOK") //Debug
3526 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3528 //If we are under a GRADE_PREFERENCE, GRADE_LETTER or GRADE_CATEGORY tag under a GRADEBOOK zone, accumule it
3529 if (isset($this->tree[5]) and isset($this->tree[3])) {
3530 if (($this->tree[5] == "GRADE_ITEM" || $this->tree[5] == "GRADE_CATEGORY" || $this->tree[5] == "GRADE_OUTCOME") && ($this->tree[3] == "GRADEBOOK")) {
3531 if (!isset($this->temp)) {
3532 $this->temp = "";
3534 $this->temp .= "<".$tagName.">";
3540 //This is the startTag handler we use where we are reading the user zone (todo="USERS")
3541 function startElementUsers($parser, $tagName, $attrs) {
3542 //Refresh properties
3543 $this->level++;
3544 $this->tree[$this->level] = $tagName;
3546 //Check if we are into USERS zone
3547 //if ($this->tree[3] == "USERS") //Debug
3548 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3551 //This is the startTag handler we use where we are reading the messages zone (todo="MESSAGES")
3552 function startElementMessages($parser, $tagName, $attrs) {
3553 //Refresh properties
3554 $this->level++;
3555 $this->tree[$this->level] = $tagName;
3557 //Output something to avoid browser timeouts...
3558 backup_flush();
3560 //Check if we are into MESSAGES zone
3561 //if ($this->tree[3] == "MESSAGES") //Debug
3562 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3564 //If we are under a MESSAGE tag under a MESSAGES zone, accumule it
3565 if (isset($this->tree[4]) and isset($this->tree[3])) {
3566 if (($this->tree[4] == "MESSAGE" || $this->tree[5] == "CONTACT" ) and ($this->tree[3] == "MESSAGES")) {
3567 if (!isset($this->temp)) {
3568 $this->temp = "";
3570 $this->temp .= "<".$tagName.">";
3574 //This is the startTag handler we use where we are reading the questions zone (todo="QUESTIONS")
3575 function startElementQuestions($parser, $tagName, $attrs) {
3576 //Refresh properties
3577 $this->level++;
3578 $this->tree[$this->level] = $tagName;
3580 //if ($tagName == "QUESTION_CATEGORY" && $this->tree[3] == "QUESTION_CATEGORIES") { //Debug
3581 // echo "<P>QUESTION_CATEGORY: ".strftime ("%X",time()),"-"; //Debug
3582 //} //Debug
3584 //Output something to avoid browser timeouts...
3585 backup_flush();
3587 //Check if we are into QUESTION_CATEGORIES zone
3588 //if ($this->tree[3] == "QUESTION_CATEGORIES") //Debug
3589 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3591 //If we are under a QUESTION_CATEGORY tag under a QUESTION_CATEGORIES zone, accumule it
3592 if (isset($this->tree[4]) and isset($this->tree[3])) {
3593 if (($this->tree[4] == "QUESTION_CATEGORY") and ($this->tree[3] == "QUESTION_CATEGORIES")) {
3594 if (!isset($this->temp)) {
3595 $this->temp = "";
3597 $this->temp .= "<".$tagName.">";
3602 //This is the startTag handler we use where we are reading the scales zone (todo="SCALES")
3603 function startElementScales($parser, $tagName, $attrs) {
3604 //Refresh properties
3605 $this->level++;
3606 $this->tree[$this->level] = $tagName;
3608 //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug
3609 // echo "<P>SCALE: ".strftime ("%X",time()),"-"; //Debug
3610 //} //Debug
3612 //Output something to avoid browser timeouts...
3613 backup_flush();
3615 //Check if we are into SCALES zone
3616 //if ($this->tree[3] == "SCALES") //Debug
3617 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3619 //If we are under a SCALE tag under a SCALES zone, accumule it
3620 if (isset($this->tree[4]) and isset($this->tree[3])) {
3621 if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) {
3622 if (!isset($this->temp)) {
3623 $this->temp = "";
3625 $this->temp .= "<".$tagName.">";
3630 function startElementGroups($parser, $tagName, $attrs) {
3631 //Refresh properties
3632 $this->level++;
3633 $this->tree[$this->level] = $tagName;
3635 //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug
3636 // echo "<P>GROUP: ".strftime ("%X",time()),"-"; //Debug
3637 //} //Debug
3639 //Output something to avoid browser timeouts...
3640 backup_flush();
3642 //Check if we are into GROUPS zone
3643 //if ($this->tree[3] == "GROUPS") //Debug
3644 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3646 //If we are under a GROUP tag under a GROUPS zone, accumule it
3647 if (isset($this->tree[4]) and isset($this->tree[3])) {
3648 if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
3649 if (!isset($this->temp)) {
3650 $this->temp = "";
3652 $this->temp .= "<".$tagName.">";
3657 function startElementGroupings($parser, $tagName, $attrs) { //TODO:
3658 //Refresh properties
3659 $this->level++;
3660 $this->tree[$this->level] = $tagName;
3662 //if ($tagName == "GROUPING" && $this->tree[3] == "GROUPINGS") { //Debug
3663 // echo "<P>GROUPING: ".strftime ("%X",time()),"-"; //Debug
3664 //} //Debug
3666 //Output something to avoid browser timeouts...
3667 backup_flush();
3669 //Check if we are into GROUPINGS zone
3670 //if ($this->tree[3] == "GROUPINGS") //Debug
3671 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3673 //If we are under a GROUPING tag under a GROUPINGS zone, accumule it
3674 if (isset($this->tree[4]) and isset($this->tree[3])) {
3675 if (($this->tree[4] == "GROUPING") and ($this->tree[3] == "GROUPINGS")) {
3676 if (!isset($this->temp)) {
3677 $this->temp = "";
3679 $this->temp .= "<".$tagName.">";
3684 //This is the startTag handler we use where we are reading the events zone (todo="EVENTS")
3685 function startElementEvents($parser, $tagName, $attrs) {
3686 //Refresh properties
3687 $this->level++;
3688 $this->tree[$this->level] = $tagName;
3690 //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug
3691 // echo "<P>EVENT: ".strftime ("%X",time()),"-"; //Debug
3692 //} //Debug
3694 //Output something to avoid browser timeouts...
3695 backup_flush();
3697 //Check if we are into EVENTS zone
3698 //if ($this->tree[3] == "EVENTS") //Debug
3699 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3701 //If we are under a EVENT tag under a EVENTS zone, accumule it
3702 if (isset($this->tree[4]) and isset($this->tree[3])) {
3703 if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) {
3704 if (!isset($this->temp)) {
3705 $this->temp = "";
3707 $this->temp .= "<".$tagName.">";
3712 //This is the startTag handler we use where we are reading the modules zone (todo="MODULES")
3713 function startElementModules($parser, $tagName, $attrs) {
3714 //Refresh properties
3715 $this->level++;
3716 $this->tree[$this->level] = $tagName;
3718 //if ($tagName == "MOD" && $this->tree[3] == "MODULES") { //Debug
3719 // echo "<P>MOD: ".strftime ("%X",time()),"-"; //Debug
3720 //} //Debug
3722 //Output something to avoid browser timeouts...
3723 backup_flush();
3725 //Check if we are into MODULES zone
3726 //if ($this->tree[3] == "MODULES") //Debug
3727 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3729 //If we are under a MOD tag under a MODULES zone, accumule it
3730 if (isset($this->tree[4]) and isset($this->tree[3])) {
3731 if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) {
3732 if (!isset($this->temp)) {
3733 $this->temp = "";
3735 $this->temp .= "<".$tagName.">";
3740 //This is the startTag handler we use where we are reading the logs zone (todo="LOGS")
3741 function startElementLogs($parser, $tagName, $attrs) {
3742 //Refresh properties
3743 $this->level++;
3744 $this->tree[$this->level] = $tagName;
3746 //if ($tagName == "LOG" && $this->tree[3] == "LOGS") { //Debug
3747 // echo "<P>LOG: ".strftime ("%X",time()),"-"; //Debug
3748 //} //Debug
3750 //Output something to avoid browser timeouts...
3751 backup_flush();
3753 //Check if we are into LOGS zone
3754 //if ($this->tree[3] == "LOGS") //Debug
3755 // echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3757 //If we are under a LOG tag under a LOGS zone, accumule it
3758 if (isset($this->tree[4]) and isset($this->tree[3])) {
3759 if (($this->tree[4] == "LOG") and ($this->tree[3] == "LOGS")) {
3760 if (!isset($this->temp)) {
3761 $this->temp = "";
3763 $this->temp .= "<".$tagName.">";
3768 //This is the startTag default handler we use when todo is undefined
3769 function startElement($parser, $tagName, $attrs) {
3770 $this->level++;
3771 $this->tree[$this->level] = $tagName;
3773 //Output something to avoid browser timeouts...
3774 backup_flush();
3776 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n"; //Debug
3779 //This is the endTag handler we use where we are reading the info zone (todo="INFO")
3780 function endElementInfo($parser, $tagName) {
3781 //Check if we are into INFO zone
3782 if ($this->tree[2] == "INFO") {
3783 //if (trim($this->content)) //Debug
3784 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
3785 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
3786 //Dependig of different combinations, do different things
3787 if ($this->level == 3) {
3788 switch ($tagName) {
3789 case "NAME":
3790 $this->info->backup_name = $this->getContents();
3791 break;
3792 case "MOODLE_VERSION":
3793 $this->info->backup_moodle_version = $this->getContents();
3794 break;
3795 case "MOODLE_RELEASE":
3796 $this->info->backup_moodle_release = $this->getContents();
3797 break;
3798 case "BACKUP_VERSION":
3799 $this->info->backup_backup_version = $this->getContents();
3800 break;
3801 case "BACKUP_RELEASE":
3802 $this->info->backup_backup_release = $this->getContents();
3803 break;
3804 case "DATE":
3805 $this->info->backup_date = $this->getContents();
3806 break;
3807 case "ORIGINAL_WWWROOT":
3808 $this->info->original_wwwroot = $this->getContents();
3809 break;
3810 case "MNET_EXTERNALUSERS":
3811 $this->info->mnet_externalusers = $this->getContents();
3812 break;
3815 if ($this->tree[3] == "DETAILS") {
3816 if ($this->level == 4) {
3817 switch ($tagName) {
3818 case "METACOURSE":
3819 $this->info->backup_metacourse = $this->getContents();
3820 break;
3821 case "USERS":
3822 $this->info->backup_users = $this->getContents();
3823 break;
3824 case "LOGS":
3825 $this->info->backup_logs = $this->getContents();
3826 break;
3827 case "USERFILES":
3828 $this->info->backup_user_files = $this->getContents();
3829 break;
3830 case "COURSEFILES":
3831 $this->info->backup_course_files = $this->getContents();
3832 break;
3833 case "MESSAGES":
3834 $this->info->backup_messages = $this->getContents();
3835 break;
3836 case 'BLOCKFORMAT':
3837 $this->info->backup_block_format = $this->getContents();
3838 break;
3841 if ($this->level == 5) {
3842 switch ($tagName) {
3843 case "NAME":
3844 $this->info->tempName = $this->getContents();
3845 break;
3846 case "INCLUDED":
3847 $this->info->mods[$this->info->tempName]->backup = $this->getContents();
3848 break;
3849 case "USERINFO":
3850 $this->info->mods[$this->info->tempName]->userinfo = $this->getContents();
3851 break;
3854 if ($this->level == 7) {
3855 switch ($tagName) {
3856 case "ID":
3857 $this->info->tempId = $this->getContents();
3858 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->id = $this->info->tempId;
3859 break;
3860 case "NAME":
3861 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->name = $this->getContents();
3862 break;
3863 case "INCLUDED":
3864 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->backup = $this->getContents();
3865 break;
3866 case "USERINFO":
3867 $this->info->mods[$this->info->tempName]->instances[$this->info->tempId]->userinfo = $this->getContents();
3868 break;
3874 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
3875 //Speed up a lot (avoid parse all)
3876 if ($tagName == "INFO") {
3877 $this->finished = true;
3880 //Clear things
3881 $this->tree[$this->level] = "";
3882 $this->level--;
3883 $this->content = "";
3887 function endElementRoles($parser, $tagName) {
3888 //Check if we are into INFO zone
3889 if ($this->tree[2] == "ROLES") {
3891 if ($this->tree[3] == "ROLE") {
3892 if ($this->level == 4) {
3893 switch ($tagName) {
3894 case "NAME":
3895 $this->info->tempname = $this->getContents();
3897 break;
3898 case "SHORTNAME":
3899 $this->info->tempshortname = $this->getContents();
3900 break;
3901 case "ID": // this is the old id
3902 $this->info->tempid = $this->getContents();
3903 break;
3906 if ($this->level == 6) {
3907 switch ($tagName) {
3908 case "NAME":
3909 $this->info->roles[$this->info->tempid]->name = $this->info->tempname;
3910 $this->info->roles[$this->info->tempid]->shortname = $this->info->tempshortname;
3912 $this->info->tempcapname = $this->getContents();
3913 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->name = $this->getContents();
3914 break;
3915 case "PERMISSION":
3916 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->permission = $this->getContents();
3917 break;
3918 case "TIMEMODIFIED":
3919 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->timemodified = $this->getContents();
3920 break;
3921 case "MODIFIERID":
3922 $this->info->roles[$this->info->tempid]->capabilities[$this->info->tempcapname]->modifierid = $this->getContents();
3923 break;
3929 //Stop parsing if todo = INFO and tagName = INFO (en of the tag, of course)
3930 //Speed up a lot (avoid parse all)
3931 if ($tagName == "ROLES") {
3932 $this->finished = true;
3935 //Clear things
3936 $this->tree[$this->level] = "";
3937 $this->level--;
3938 $this->content = "";
3942 //This is the endTag handler we use where we are reading the course_header zone (todo="COURSE_HEADER")
3943 function endElementCourseHeader($parser, $tagName) {
3944 //Check if we are into COURSE_HEADER zone
3945 if ($this->tree[3] == "HEADER") {
3946 //if (trim($this->content)) //Debug
3947 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
3948 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
3949 //Dependig of different combinations, do different things
3950 if ($this->level == 4) {
3951 switch ($tagName) {
3952 case "ID":
3953 $this->info->course_id = $this->getContents();
3954 break;
3955 case "PASSWORD":
3956 $this->info->course_password = $this->getContents();
3957 break;
3958 case "FULLNAME":
3959 $this->info->course_fullname = $this->getContents();
3960 break;
3961 case "SHORTNAME":
3962 $this->info->course_shortname = $this->getContents();
3963 break;
3964 case "IDNUMBER":
3965 $this->info->course_idnumber = $this->getContents();
3966 break;
3967 case "SUMMARY":
3968 $this->info->course_summary = $this->getContents();
3969 break;
3970 case "FORMAT":
3971 $this->info->course_format = $this->getContents();
3972 break;
3973 case "SHOWGRADES":
3974 $this->info->course_showgrades = $this->getContents();
3975 break;
3976 case "BLOCKINFO":
3977 $this->info->blockinfo = $this->getContents();
3978 break;
3979 case "NEWSITEMS":
3980 $this->info->course_newsitems = $this->getContents();
3981 break;
3982 case "TEACHER":
3983 $this->info->course_teacher = $this->getContents();
3984 break;
3985 case "TEACHERS":
3986 $this->info->course_teachers = $this->getContents();
3987 break;
3988 case "STUDENT":
3989 $this->info->course_student = $this->getContents();
3990 break;
3991 case "STUDENTS":
3992 $this->info->course_students = $this->getContents();
3993 break;
3994 case "GUEST":
3995 $this->info->course_guest = $this->getContents();
3996 break;
3997 case "STARTDATE":
3998 $this->info->course_startdate = $this->getContents();
3999 break;
4000 case "ENROLPERIOD":
4001 $this->info->course_enrolperiod = $this->getContents();
4002 break;
4003 case "NUMSECTIONS":
4004 $this->info->course_numsections = $this->getContents();
4005 break;
4006 //case "SHOWRECENT": INFO: This is out in 1.3
4007 // $this->info->course_showrecent = $this->getContents();
4008 // break;
4009 case "MAXBYTES":
4010 $this->info->course_maxbytes = $this->getContents();
4011 break;
4012 case "SHOWREPORTS":
4013 $this->info->course_showreports = $this->getContents();
4014 break;
4015 case "GROUPMODE":
4016 $this->info->course_groupmode = $this->getContents();
4017 break;
4018 case "GROUPMODEFORCE":
4019 $this->info->course_groupmodeforce = $this->getContents();
4020 break;
4021 case "LANG":
4022 $this->info->course_lang = $this->getContents();
4023 break;
4024 case "THEME":
4025 $this->info->course_theme = $this->getContents();
4026 break;
4027 case "COST":
4028 $this->info->course_cost = $this->getContents();
4029 break;
4030 case "CURRENCY":
4031 $this->info->course_currency = $this->getContents();
4032 break;
4033 case "MARKER":
4034 $this->info->course_marker = $this->getContents();
4035 break;
4036 case "VISIBLE":
4037 $this->info->course_visible = $this->getContents();
4038 break;
4039 case "HIDDENSECTIONS":
4040 $this->info->course_hiddensections = $this->getContents();
4041 break;
4042 case "TIMECREATED":
4043 $this->info->course_timecreated = $this->getContents();
4044 break;
4045 case "TIMEMODIFIED":
4046 $this->info->course_timemodified = $this->getContents();
4047 break;
4048 case "METACOURSE":
4049 $this->info->course_metacourse = $this->getContents();
4050 break;
4053 if ($this->tree[4] == "CATEGORY") {
4054 if ($this->level == 5) {
4055 switch ($tagName) {
4056 case "ID":
4057 $this->info->category->id = $this->getContents();
4058 break;
4059 case "NAME":
4060 $this->info->category->name = $this->getContents();
4061 break;
4066 if ($this->tree[4] == "ROLES_ASSIGNMENTS") {
4067 if ($this->level == 6) {
4068 switch ($tagName) {
4069 case "NAME":
4070 $this->info->tempname = $this->getContents();
4071 break;
4072 case "SHORTNAME":
4073 $this->info->tempshortname = $this->getContents();
4074 break;
4075 case "ID":
4076 $this->info->tempid = $this->getContents();
4077 break;
4081 if ($this->level == 8) {
4082 switch ($tagName) {
4083 case "USERID":
4084 $this->info->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4085 $this->info->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4086 $this->info->tempuser = $this->getContents();
4087 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4088 break;
4089 case "HIDDEN":
4090 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4091 break;
4092 case "TIMESTART":
4093 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4094 break;
4095 case "TIMEEND":
4096 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4097 break;
4098 case "TIMEMODIFIED":
4099 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4100 break;
4101 case "MODIFIERID":
4102 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4103 break;
4104 case "ENROL":
4105 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4106 break;
4107 case "SORTORDER":
4108 $this->info->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4109 break;
4113 } /// ends role_assignments
4115 if ($this->tree[4] == "ROLES_OVERRIDES") {
4116 if ($this->level == 6) {
4117 switch ($tagName) {
4118 case "NAME":
4119 $this->info->tempname = $this->getContents();
4120 break;
4121 case "SHORTNAME":
4122 $this->info->tempshortname = $this->getContents();
4123 break;
4124 case "ID":
4125 $this->info->tempid = $this->getContents();
4126 break;
4130 if ($this->level == 8) {
4131 switch ($tagName) {
4132 case "NAME":
4133 $this->info->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4134 $this->info->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4135 $this->info->tempname = $this->getContents(); // change to name of capability
4136 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4137 break;
4138 case "PERMISSION":
4139 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4140 break;
4141 case "TIMEMODIFIED":
4142 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4143 break;
4144 case "MODIFIERID":
4145 $this->info->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4146 break;
4149 } /// ends role_overrides
4152 //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
4153 //Speed up a lot (avoid parse all)
4154 if ($tagName == "HEADER") {
4155 $this->finished = true;
4158 //Clear things
4159 $this->tree[$this->level] = "";
4160 $this->level--;
4161 $this->content = "";
4165 //This is the endTag handler we use where we are reading the sections zone (todo="BLOCKS")
4166 function endElementBlocks($parser, $tagName) {
4167 //Check if we are into BLOCKS zone
4168 if ($this->tree[3] == 'BLOCKS') {
4169 //if (trim($this->content)) //Debug
4170 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4171 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4172 //Dependig of different combinations, do different things
4173 if ($this->level == 4) {
4174 switch ($tagName) {
4175 case 'BLOCK':
4176 //We've finalized a block, get it
4177 $this->info->instances[] = $this->info->tempinstance;
4178 unset($this->info->tempinstance);
4179 break;
4180 default:
4181 die($tagName);
4184 if ($this->level == 5) {
4185 switch ($tagName) {
4186 case 'ID':
4187 $this->info->tempinstance->id = $this->getContents();
4188 case 'NAME':
4189 $this->info->tempinstance->name = $this->getContents();
4190 break;
4191 case 'PAGEID':
4192 $this->info->tempinstance->pageid = $this->getContents();
4193 break;
4194 case 'PAGETYPE':
4195 $this->info->tempinstance->pagetype = $this->getContents();
4196 break;
4197 case 'POSITION':
4198 $this->info->tempinstance->position = $this->getContents();
4199 break;
4200 case 'WEIGHT':
4201 $this->info->tempinstance->weight = $this->getContents();
4202 break;
4203 case 'VISIBLE':
4204 $this->info->tempinstance->visible = $this->getContents();
4205 break;
4206 case 'CONFIGDATA':
4207 $this->info->tempinstance->configdata = $this->getContents();
4208 break;
4209 default:
4210 break;
4214 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
4215 if ($this->level == 7) {
4216 switch ($tagName) {
4217 case "NAME":
4218 $this->info->tempname = $this->getContents();
4219 break;
4220 case "SHORTNAME":
4221 $this->info->tempshortname = $this->getContents();
4222 break;
4223 case "ID":
4224 $this->info->tempid = $this->getContents(); // temp roleid
4225 break;
4229 if ($this->level == 9) {
4231 switch ($tagName) {
4232 case "USERID":
4233 $this->info->tempinstance->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4235 $this->info->tempinstance->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4237 $this->info->tempuser = $this->getContents();
4239 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4240 break;
4241 case "HIDDEN":
4242 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4243 break;
4244 case "TIMESTART":
4245 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4246 break;
4247 case "TIMEEND":
4248 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4249 break;
4250 case "TIMEMODIFIED":
4251 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4252 break;
4253 case "MODIFIERID":
4254 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4255 break;
4256 case "ENROL":
4257 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4258 break;
4259 case "SORTORDER":
4260 $this->info->tempinstance->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4261 break;
4265 } /// ends role_assignments
4267 if ($this->tree[5] == "ROLES_OVERRIDES") {
4268 if ($this->level == 7) {
4269 switch ($tagName) {
4270 case "NAME":
4271 $this->info->tempname = $this->getContents();
4272 break;
4273 case "SHORTNAME":
4274 $this->info->tempshortname = $this->getContents();
4275 break;
4276 case "ID":
4277 $this->info->tempid = $this->getContents(); // temp roleid
4278 break;
4282 if ($this->level == 9) {
4283 switch ($tagName) {
4284 case "NAME":
4286 $this->info->tempinstance->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4287 $this->info->tempinstance->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4288 $this->info->tempname = $this->getContents(); // change to name of capability
4289 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4290 break;
4291 case "PERMISSION":
4292 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4293 break;
4294 case "TIMEMODIFIED":
4295 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4296 break;
4297 case "MODIFIERID":
4298 $this->info->tempinstance->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4299 break;
4302 } /// ends role_overrides
4305 //Stop parsing if todo = BLOCKS and tagName = BLOCKS (en of the tag, of course)
4306 //Speed up a lot (avoid parse all)
4307 //WARNING: ONLY EXIT IF todo = BLOCKS (thus tree[3] = "BLOCKS") OTHERWISE
4308 // THE BLOCKS TAG IN THE HEADER WILL TERMINATE US!
4309 if ($this->tree[3] == 'BLOCKS' && $tagName == 'BLOCKS') {
4310 $this->finished = true;
4313 //Clear things
4314 $this->tree[$this->level] = '';
4315 $this->level--;
4316 $this->content = "";
4319 //This is the endTag handler we use where we are reading the sections zone (todo="SECTIONS")
4320 function endElementSections($parser, $tagName) {
4321 //Check if we are into SECTIONS zone
4322 if ($this->tree[3] == "SECTIONS") {
4323 //if (trim($this->content)) //Debug
4324 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4325 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4326 //Dependig of different combinations, do different things
4327 if ($this->level == 4) {
4328 switch ($tagName) {
4329 case "SECTION":
4330 //We've finalized a section, get it
4331 $this->info->sections[$this->info->tempsection->id] = $this->info->tempsection;
4332 unset($this->info->tempsection);
4335 if ($this->level == 5) {
4336 switch ($tagName) {
4337 case "ID":
4338 $this->info->tempsection->id = $this->getContents();
4339 break;
4340 case "NUMBER":
4341 $this->info->tempsection->number = $this->getContents();
4342 break;
4343 case "SUMMARY":
4344 $this->info->tempsection->summary = $this->getContents();
4345 break;
4346 case "VISIBLE":
4347 $this->info->tempsection->visible = $this->getContents();
4348 break;
4351 if ($this->level == 6) {
4352 switch ($tagName) {
4353 case "MOD":
4354 //We've finalized a mod, get it
4355 $this->info->tempsection->mods[$this->info->tempmod->id]->type =
4356 $this->info->tempmod->type;
4357 $this->info->tempsection->mods[$this->info->tempmod->id]->instance =
4358 $this->info->tempmod->instance;
4359 $this->info->tempsection->mods[$this->info->tempmod->id]->added =
4360 $this->info->tempmod->added;
4361 $this->info->tempsection->mods[$this->info->tempmod->id]->score =
4362 $this->info->tempmod->score;
4363 $this->info->tempsection->mods[$this->info->tempmod->id]->indent =
4364 $this->info->tempmod->indent;
4365 $this->info->tempsection->mods[$this->info->tempmod->id]->visible =
4366 $this->info->tempmod->visible;
4367 $this->info->tempsection->mods[$this->info->tempmod->id]->groupmode =
4368 $this->info->tempmod->groupmode;
4369 unset($this->info->tempmod);
4372 if ($this->level == 7) {
4373 switch ($tagName) {
4374 case "ID":
4375 $this->info->tempmod->id = $this->getContents();
4376 break;
4377 case "TYPE":
4378 $this->info->tempmod->type = $this->getContents();
4379 break;
4380 case "INSTANCE":
4381 $this->info->tempmod->instance = $this->getContents();
4382 break;
4383 case "ADDED":
4384 $this->info->tempmod->added = $this->getContents();
4385 break;
4386 case "SCORE":
4387 $this->info->tempmod->score = $this->getContents();
4388 break;
4389 case "INDENT":
4390 $this->info->tempmod->indent = $this->getContents();
4391 break;
4392 case "VISIBLE":
4393 $this->info->tempmod->visible = $this->getContents();
4394 break;
4395 case "GROUPMODE":
4396 $this->info->tempmod->groupmode = $this->getContents();
4397 break;
4398 default:
4399 break;
4403 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_ASSIGNMENTS") {
4405 if ($this->level == 9) {
4406 switch ($tagName) {
4407 case "NAME":
4408 $this->info->tempname = $this->getContents();
4409 break;
4410 case "SHORTNAME":
4411 $this->info->tempshortname = $this->getContents();
4412 break;
4413 case "ID":
4414 $this->info->tempid = $this->getContents(); // temp roleid
4415 break;
4419 if ($this->level == 11) {
4420 switch ($tagName) {
4421 case "USERID":
4422 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4424 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4426 $this->info->tempuser = $this->getContents();
4428 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->userid = $this->getContents();
4429 break;
4430 case "HIDDEN":
4431 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->hidden = $this->getContents();
4432 break;
4433 case "TIMESTART":
4434 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timestart = $this->getContents();
4435 break;
4436 case "TIMEEND":
4437 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timeend = $this->getContents();
4438 break;
4439 case "TIMEMODIFIED":
4440 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->timemodified = $this->getContents();
4441 break;
4442 case "MODIFIERID":
4443 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->modifierid = $this->getContents();
4444 break;
4445 case "ENROL":
4446 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->enrol = $this->getContents();
4447 break;
4448 case "SORTORDER":
4449 $this->info->tempsection->mods[$this->info->tempmod->id]->roleassignments[$this->info->tempid]->assignments[$this->info->tempuser]->sortorder = $this->getContents();
4450 break;
4454 } /// ends role_assignments
4456 if (isset($this->tree[7]) && $this->tree[7] == "ROLES_OVERRIDES") {
4457 if ($this->level == 9) {
4458 switch ($tagName) {
4459 case "NAME":
4460 $this->info->tempname = $this->getContents();
4461 break;
4462 case "SHORTNAME":
4463 $this->info->tempshortname = $this->getContents();
4464 break;
4465 case "ID":
4466 $this->info->tempid = $this->getContents(); // temp roleid
4467 break;
4471 if ($this->level == 11) {
4472 switch ($tagName) {
4473 case "NAME":
4475 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
4476 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
4477 $this->info->tempname = $this->getContents(); // change to name of capability
4478 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
4479 break;
4480 case "PERMISSION":
4481 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
4482 break;
4483 case "TIMEMODIFIED":
4484 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
4485 break;
4486 case "MODIFIERID":
4487 $this->info->tempsection->mods[$this->info->tempmod->id]->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
4488 break;
4491 } /// ends role_overrides
4495 //Stop parsing if todo = SECTIONS and tagName = SECTIONS (en of the tag, of course)
4496 //Speed up a lot (avoid parse all)
4497 if ($tagName == "SECTIONS") {
4498 $this->finished = true;
4501 //Clear things
4502 $this->tree[$this->level] = "";
4503 $this->level--;
4504 $this->content = "";
4508 //This is the endTag handler we use where we are reading the optional format data zone (todo="FORMATDATA")
4509 function endElementFormatData($parser, $tagName) {
4510 //Check if we are into FORMATDATA zone
4511 if ($this->tree[3] == 'FORMATDATA') {
4512 if (!isset($this->temp)) {
4513 $this->temp = '';
4515 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
4518 if($tagName=='FORMATDATA') {
4519 //Did we have any data? If not don't bother
4520 if($this->temp!='<FORMATDATA></FORMATDATA>') {
4521 //Prepend XML standard header to info gathered
4522 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4523 $this->temp='';
4525 //Call to xmlize for this portion of xml data (the FORMATDATA block)
4526 $this->info->format_data = xmlize($xml_data,0);
4528 //Stop parsing at end of FORMATDATA
4529 $this->finished=true;
4532 //Clear things
4533 $this->tree[$this->level] = "";
4534 $this->level--;
4535 $this->content = "";
4538 //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
4539 function endElementMetacourse($parser, $tagName) {
4540 //Check if we are into METACOURSE zone
4541 if ($this->tree[3] == 'METACOURSE') {
4542 //if (trim($this->content)) //Debug
4543 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4544 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4545 //Dependig of different combinations, do different things
4546 if ($this->level == 5) {
4547 switch ($tagName) {
4548 case 'CHILD':
4549 //We've finalized a child, get it
4550 $this->info->childs[] = $this->info->tempmeta;
4551 unset($this->info->tempmeta);
4552 break;
4553 case 'PARENT':
4554 //We've finalized a parent, get it
4555 $this->info->parents[] = $this->info->tempmeta;
4556 unset($this->info->tempmeta);
4557 break;
4558 default:
4559 die($tagName);
4562 if ($this->level == 6) {
4563 switch ($tagName) {
4564 case 'ID':
4565 $this->info->tempmeta->id = $this->getContents();
4566 break;
4567 case 'IDNUMBER':
4568 $this->info->tempmeta->idnumber = $this->getContents();
4569 break;
4570 case 'SHORTNAME':
4571 $this->info->tempmeta->shortname = $this->getContents();
4572 break;
4577 //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
4578 //Speed up a lot (avoid parse all)
4579 if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
4580 $this->finished = true;
4583 //Clear things
4584 $this->tree[$this->level] = '';
4585 $this->level--;
4586 $this->content = "";
4589 //This is the endTag handler we use where we are reading the gradebook zone (todo="GRADEBOOK")
4590 function endElementGradebook($parser, $tagName) {
4591 //Check if we are into GRADEBOOK zone
4592 if ($this->tree[3] == "GRADEBOOK") {
4593 //if (trim($this->content)) //Debug
4594 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4595 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
4596 //Acumulate data to info (content + close tag)
4597 //Reconvert: strip htmlchars again and trim to generate xml data
4598 if (!isset($this->temp)) {
4599 $this->temp = "";
4601 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
4602 // We have finished outcome, grade_category or grade_item, reset accumulated
4603 // data because they are close tags
4604 if ($this->level == 4) {
4605 $this->temp = "";
4607 //If we've finished a message, xmlize it an save to db
4608 if (($this->level == 5) and ($tagName == "GRADE_ITEM")) {
4609 //Prepend XML standard header to info gathered
4610 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4611 //Call to xmlize for this portion of xml data (one PREFERENCE)
4612 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
4613 $data = xmlize($xml_data,0);
4614 //echo strftime ("%X",time())."<p>"; //Debug
4615 //traverse_xmlize($data); //Debug
4616 //print_object ($GLOBALS['traverse_array']); //Debug
4617 //$GLOBALS['traverse_array']=""; //Debug
4618 //Now, save data to db. We'll use it later
4619 //Get id and status from data
4620 $item_id = $data["GRADE_ITEM"]["#"]["ID"]["0"]["#"];
4621 $this->counter++;
4622 //Save to db
4624 $status = backup_putid($this->preferences->backup_unique_code, 'grade_items', $item_id,
4625 null,$data);
4626 //Create returning info
4627 $this->info = $this->counter;
4628 //Reset temp
4630 unset($this->temp);
4633 //If we've finished a grade_category, xmlize it an save to db
4634 if (($this->level == 5) and ($tagName == "GRADE_CATEGORY")) {
4635 //Prepend XML standard header to info gathered
4636 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4637 //Call to xmlize for this portion of xml data (one CATECORY)
4638 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
4639 $data = xmlize($xml_data,0);
4640 //echo strftime ("%X",time())."<p>"; //Debug
4641 //traverse_xmlize($data); //Debug
4642 //print_object ($GLOBALS['traverse_array']); //Debug
4643 //$GLOBALS['traverse_array']=""; //Debug
4644 //Now, save data to db. We'll use it later
4645 //Get id and status from data
4646 $category_id = $data["GRADE_CATEGORY"]["#"]["ID"]["0"]["#"];
4647 $this->counter++;
4648 //Save to db
4649 $status = backup_putid($this->preferences->backup_unique_code, 'grade_categories' ,$category_id,
4650 null,$data);
4651 //Create returning info
4652 $this->info = $this->counter;
4653 //Reset temp
4654 unset($this->temp);
4657 //If we've finished a grade_category, xmlize it an save to db
4658 if (($this->level == 5) and ($tagName == "GRADE_OUTCOME")) {
4659 //Prepend XML standard header to info gathered
4660 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
4661 //Call to xmlize for this portion of xml data (one CATECORY)
4662 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
4663 $data = xmlize($xml_data,0);
4664 //echo strftime ("%X",time())."<p>"; //Debug
4665 //traverse_xmlize($data); //Debug
4666 //print_object ($GLOBALS['traverse_array']); //Debug
4667 //$GLOBALS['traverse_array']=""; //Debug
4668 //Now, save data to db. We'll use it later
4669 //Get id and status from data
4670 $outcome_id = $data["GRADE_OUTCOME"]["#"]["ID"]["0"]["#"];
4671 $this->counter++;
4672 //Save to db
4673 $status = backup_putid($this->preferences->backup_unique_code, 'grade_outcomes' ,$outcome_id,
4674 null,$data);
4675 //Create returning info
4676 $this->info = $this->counter;
4677 //Reset temp
4678 unset($this->temp);
4682 //Stop parsing if todo = GRADEBOOK and tagName = GRADEBOOK (en of the tag, of course)
4683 //Speed up a lot (avoid parse all)
4684 if ($tagName == "GRADEBOOK" and $this->level == 3) {
4685 $this->finished = true;
4686 $this->counter = 0;
4689 //Clear things
4690 $this->tree[$this->level] = "";
4691 $this->level--;
4692 $this->content = "";
4696 //This is the endTag handler we use where we are reading the users zone (todo="USERS")
4697 function endElementUsers($parser, $tagName) {
4698 global $CFG;
4699 //Check if we are into USERS zone
4700 if ($this->tree[3] == "USERS") {
4701 //if (trim($this->content)) //Debug
4702 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
4703 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
4704 //Dependig of different combinations, do different things
4705 if ($this->level == 4) {
4706 switch ($tagName) {
4707 case "USER":
4708 //Increment counter
4709 $this->counter++;
4710 //Save to db, only save if record not already exist
4711 // if there already is an new_id for this entry, just use that new_id?
4712 $newuser = backup_getid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id);
4713 if (isset($newuser->new_id)) {
4714 $newid = $newuser->new_id;
4715 } else {
4716 $newid = null;
4719 backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id,
4720 $newid,$this->info->tempuser);
4722 //Do some output
4723 if ($this->counter % 10 == 0) {
4724 if (!defined('RESTORE_SILENTLY')) {
4725 echo ".";
4726 if ($this->counter % 200 == 0) {
4727 echo "<br />";
4730 backup_flush(300);
4733 //Delete temp obejct
4734 unset($this->info->tempuser);
4735 break;
4738 if ($this->level == 5) {
4739 switch ($tagName) {
4740 case "ID":
4741 $this->info->users[$this->getContents()] = $this->getContents();
4742 $this->info->tempuser->id = $this->getContents();
4743 break;
4744 case "AUTH":
4745 $this->info->tempuser->auth = $this->getContents();
4746 break;
4747 case "CONFIRMED":
4748 $this->info->tempuser->confirmed = $this->getContents();
4749 break;
4750 case "POLICYAGREED":
4751 $this->info->tempuser->policyagreed = $this->getContents();
4752 break;
4753 case "DELETED":
4754 $this->info->tempuser->deleted = $this->getContents();
4755 break;
4756 case "USERNAME":
4757 $this->info->tempuser->username = $this->getContents();
4758 break;
4759 case "PASSWORD":
4760 $this->info->tempuser->password = $this->getContents();
4761 break;
4762 case "IDNUMBER":
4763 $this->info->tempuser->idnumber = $this->getContents();
4764 break;
4765 case "FIRSTNAME":
4766 $this->info->tempuser->firstname = $this->getContents();
4767 break;
4768 case "LASTNAME":
4769 $this->info->tempuser->lastname = $this->getContents();
4770 break;
4771 case "EMAIL":
4772 $this->info->tempuser->email = $this->getContents();
4773 break;
4774 case "EMAILSTOP":
4775 $this->info->tempuser->emailstop = $this->getContents();
4776 break;
4777 case "ICQ":
4778 $this->info->tempuser->icq = $this->getContents();
4779 break;
4780 case "SKYPE":
4781 $this->info->tempuser->skype = $this->getContents();
4782 break;
4783 case "AIM":
4784 $this->info->tempuser->aim = $this->getContents();
4785 break;
4786 case "YAHOO":
4787 $this->info->tempuser->yahoo = $this->getContents();
4788 break;
4789 case "MSN":
4790 $this->info->tempuser->msn = $this->getContents();
4791 break;
4792 case "PHONE1":
4793 $this->info->tempuser->phone1 = $this->getContents();
4794 break;
4795 case "PHONE2":
4796 $this->info->tempuser->phone2 = $this->getContents();
4797 break;
4798 case "INSTITUTION":
4799 $this->info->tempuser->institution = $this->getContents();
4800 break;
4801 case "DEPARTMENT":
4802 $this->info->tempuser->department = $this->getContents();
4803 break;
4804 case "ADDRESS":
4805 $this->info->tempuser->address = $this->getContents();
4806 break;
4807 case "CITY":
4808 $this->info->tempuser->city = $this->getContents();
4809 break;
4810 case "COUNTRY":
4811 $this->info->tempuser->country = $this->getContents();
4812 break;
4813 case "LANG":
4814 $this->info->tempuser->lang = $this->getContents();
4815 break;
4816 case "THEME":
4817 $this->info->tempuser->theme = $this->getContents();
4818 break;
4819 case "TIMEZONE":
4820 $this->info->tempuser->timezone = $this->getContents();
4821 break;
4822 case "FIRSTACCESS":
4823 $this->info->tempuser->firstaccess = $this->getContents();
4824 break;
4825 case "LASTACCESS":
4826 $this->info->tempuser->lastaccess = $this->getContents();
4827 break;
4828 case "LASTLOGIN":
4829 $this->info->tempuser->lastlogin = $this->getContents();
4830 break;
4831 case "CURRENTLOGIN":
4832 $this->info->tempuser->currentlogin = $this->getContents();
4833 break;
4834 case "LASTIP":
4835 $this->info->tempuser->lastip = $this->getContents();
4836 break;
4837 case "SECRET":
4838 $this->info->tempuser->secret = $this->getContents();
4839 break;
4840 case "PICTURE":
4841 $this->info->tempuser->picture = $this->getContents();
4842 break;
4843 case "URL":
4844 $this->info->tempuser->url = $this->getContents();
4845 break;
4846 case "DESCRIPTION":
4847 $this->info->tempuser->description = $this->getContents();
4848 break;
4849 case "MAILFORMAT":
4850 $this->info->tempuser->mailformat = $this->getContents();
4851 break;
4852 case "MAILDIGEST":
4853 $this->info->tempuser->maildigest = $this->getContents();
4854 break;
4855 case "MAILDISPLAY":
4856 $this->info->tempuser->maildisplay = $this->getContents();
4857 break;
4858 case "HTMLEDITOR":
4859 $this->info->tempuser->htmleditor = $this->getContents();
4860 break;
4861 case "AJAX":
4862 $this->info->tempuser->ajax = $this->getContents();
4863 break;
4864 case "AUTOSUBSCRIBE":
4865 $this->info->tempuser->autosubscribe = $this->getContents();
4866 break;
4867 case "TRACKFORUMS":
4868 $this->info->tempuser->trackforums = $this->getContents();
4869 break;
4870 case "MNETHOSTURL":
4871 $this->info->tempuser->mnethosturl = $this->getContents();
4872 break;
4873 case "TIMEMODIFIED":
4874 $this->info->tempuser->timemodified = $this->getContents();
4875 break;
4876 default:
4877 break;
4880 if ($this->level == 6 && $this->tree[5]!="ROLES_ASSIGNMENTS" && $this->tree[5]!="ROLES_OVERRIDES") {
4881 switch ($tagName) {
4882 case "ROLE":
4883 //We've finalized a role, get it
4884 $this->info->tempuser->roles[$this->info->temprole->type] = $this->info->temprole;
4885 unset($this->info->temprole);
4886 break;
4887 case "USER_PREFERENCE":
4888 //We've finalized a user_preference, get it
4889 $this->info->tempuser->user_preferences[$this->info->tempuserpreference->name] = $this->info->tempuserpreference;
4890 unset($this->info->tempuserpreference);
4891 break;
4895 if ($this->level == 7) {
4896 switch ($tagName) {
4897 case "TYPE":
4898 $this->info->temprole->type = $this->getContents();
4899 break;
4900 case "AUTHORITY":
4901 $this->info->temprole->authority = $this->getContents();
4902 break;
4903 case "TEA_ROLE":
4904 $this->info->temprole->tea_role = $this->getContents();
4905 break;
4906 case "EDITALL":
4907 $this->info->temprole->editall = $this->getContents();
4908 break;
4909 case "TIMESTART":
4910 $this->info->temprole->timestart = $this->getContents();
4911 break;
4912 case "TIMEEND":
4913 $this->info->temprole->timeend = $this->getContents();
4914 break;
4915 case "TIMEMODIFIED":
4916 $this->info->temprole->timemodified = $this->getContents();
4917 break;
4918 case "TIMESTART":
4919 $this->info->temprole->timestart = $this->getContents();
4920 break;
4921 case "TIMEEND":
4922 $this->info->temprole->timeend = $this->getContents();
4923 break;
4924 case "TIME":
4925 $this->info->temprole->time = $this->getContents();
4926 break;
4927 case "TIMEACCESS":
4928 $this->info->temprole->timeaccess = $this->getContents();
4929 break;
4930 case "ENROL":
4931 $this->info->temprole->enrol = $this->getContents();
4932 break;
4933 case "NAME":
4934 $this->info->tempuserpreference->name = $this->getContents();
4935 break;
4936 case "VALUE":
4937 $this->info->tempuserpreference->value = $this->getContents();
4938 break;
4939 default:
4940 break;
4945 if ($this->tree[5] == "ROLES_ASSIGNMENTS") {
4947 if ($this->level == 7) {
4948 switch ($tagName) {
4949 case "NAME":
4950 $this->info->tempname = $this->getContents();
4951 break;
4952 case "SHORTNAME":
4953 $this->info->tempshortname = $this->getContents();
4954 break;
4955 case "ID":
4956 $this->info->tempid = $this->getContents(); // temp roleid
4957 break;
4961 if ($this->level == 9) {
4963 switch ($tagName) {
4964 case "USERID":
4965 $this->info->tempuser->roleassignments[$this->info->tempid]->name = $this->info->tempname;
4967 $this->info->tempuser->roleassignments[$this->info->tempid]->shortname = $this->info->tempshortname;
4969 $this->info->tempuserid = $this->getContents();
4971 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->userid = $this->getContents();
4972 break;
4973 case "HIDDEN":
4974 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->hidden = $this->getContents();
4975 break;
4976 case "TIMESTART":
4977 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timestart = $this->getContents();
4978 break;
4979 case "TIMEEND":
4980 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timeend = $this->getContents();
4981 break;
4982 case "TIMEMODIFIED":
4983 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->timemodified = $this->getContents();
4984 break;
4985 case "MODIFIERID":
4986 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->modifierid = $this->getContents();
4987 break;
4988 case "ENROL":
4989 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->enrol = $this->getContents();
4990 break;
4991 case "SORTORDER":
4992 $this->info->tempuser->roleassignments[$this->info->tempid]->assignments[$this->info->tempuserid]->sortorder = $this->getContents();
4993 break;
4997 } /// ends role_assignments
4999 if ($this->tree[5] == "ROLES_OVERRIDES") {
5000 if ($this->level == 7) {
5001 switch ($tagName) {
5002 case "NAME":
5003 $this->info->tempname = $this->getContents();
5004 break;
5005 case "SHORTNAME":
5006 $this->info->tempshortname = $this->getContents();
5007 break;
5008 case "ID":
5009 $this->info->tempid = $this->getContents(); // temp roleid
5010 break;
5014 if ($this->level == 9) {
5015 switch ($tagName) {
5016 case "NAME":
5018 $this->info->tempuser->roleoverrides[$this->info->tempid]->name = $this->info->tempname;
5019 $this->info->tempuser->roleoverrides[$this->info->tempid]->shortname = $this->info->tempshortname;
5020 $this->info->tempname = $this->getContents(); // change to name of capability
5021 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->name = $this->getContents();
5022 break;
5023 case "PERMISSION":
5024 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->permission = $this->getContents();
5025 break;
5026 case "TIMEMODIFIED":
5027 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->timemodified = $this->getContents();
5028 break;
5029 case "MODIFIERID":
5030 $this->info->tempuser->roleoverrides[$this->info->tempid]->overrides[$this->info->tempname]->modifierid = $this->getContents();
5031 break;
5034 } /// ends role_overrides
5036 } // closes if this->tree[3]=="users"
5038 //Stop parsing if todo = USERS and tagName = USERS (en of the tag, of course)
5039 //Speed up a lot (avoid parse all)
5040 if ($tagName == "USERS" and $this->level == 3) {
5041 $this->finished = true;
5042 $this->counter = 0;
5045 //Clear things
5046 $this->tree[$this->level] = "";
5047 $this->level--;
5048 $this->content = "";
5052 //This is the endTag handler we use where we are reading the messages zone (todo="MESSAGES")
5053 function endElementMessages($parser, $tagName) {
5054 //Check if we are into MESSAGES zone
5055 if ($this->tree[3] == "MESSAGES") {
5056 //if (trim($this->content)) //Debug
5057 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5058 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";//Debug
5059 //Acumulate data to info (content + close tag)
5060 //Reconvert: strip htmlchars again and trim to generate xml data
5061 if (!isset($this->temp)) {
5062 $this->temp = "";
5064 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5065 //If we've finished a message, xmlize it an save to db
5066 if (($this->level == 4) and ($tagName == "MESSAGE")) {
5067 //Prepend XML standard header to info gathered
5068 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5069 //Call to xmlize for this portion of xml data (one MESSAGE)
5070 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5071 $data = xmlize($xml_data,0);
5072 //echo strftime ("%X",time())."<p>"; //Debug
5073 //traverse_xmlize($data); //Debug
5074 //print_object ($GLOBALS['traverse_array']); //Debug
5075 //$GLOBALS['traverse_array']=""; //Debug
5076 //Now, save data to db. We'll use it later
5077 //Get id and status from data
5078 $message_id = $data["MESSAGE"]["#"]["ID"]["0"]["#"];
5079 $message_status = $data["MESSAGE"]["#"]["STATUS"]["0"]["#"];
5080 if ($message_status == "READ") {
5081 $table = "message_read";
5082 } else {
5083 $table = "message";
5085 $this->counter++;
5086 //Save to db
5087 $status = backup_putid($this->preferences->backup_unique_code, $table,$message_id,
5088 null,$data);
5089 //Create returning info
5090 $this->info = $this->counter;
5091 //Reset temp
5092 unset($this->temp);
5094 //If we've finished a contact, xmlize it an save to db
5095 if (($this->level == 5) and ($tagName == "CONTACT")) {
5096 //Prepend XML standard header to info gathered
5097 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5098 //Call to xmlize for this portion of xml data (one MESSAGE)
5099 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5100 $data = xmlize($xml_data,0);
5101 //echo strftime ("%X",time())."<p>"; //Debug
5102 //traverse_xmlize($data); //Debug
5103 //print_object ($GLOBALS['traverse_array']); //Debug
5104 //$GLOBALS['traverse_array']=""; //Debug
5105 //Now, save data to db. We'll use it later
5106 //Get id and status from data
5107 $contact_id = $data["CONTACT"]["#"]["ID"]["0"]["#"];
5108 $this->counter++;
5109 //Save to db
5110 $status = backup_putid($this->preferences->backup_unique_code, 'message_contacts' ,$contact_id,
5111 null,$data);
5112 //Create returning info
5113 $this->info = $this->counter;
5114 //Reset temp
5115 unset($this->temp);
5119 //Stop parsing if todo = MESSAGES and tagName = MESSAGES (en of the tag, of course)
5120 //Speed up a lot (avoid parse all)
5121 if ($tagName == "MESSAGES" and $this->level == 3) {
5122 $this->finished = true;
5123 $this->counter = 0;
5126 //Clear things
5127 $this->tree[$this->level] = "";
5128 $this->level--;
5129 $this->content = "";
5133 //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS")
5134 function endElementQuestions($parser, $tagName) {
5135 //Check if we are into QUESTION_CATEGORIES zone
5136 if ($this->tree[3] == "QUESTION_CATEGORIES") {
5137 //if (trim($this->content)) //Debug
5138 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5139 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5140 //Acumulate data to info (content + close tag)
5141 //Reconvert: strip htmlchars again and trim to generate xml data
5142 if (!isset($this->temp)) {
5143 $this->temp = "";
5145 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5146 //If we've finished a mod, xmlize it an save to db
5147 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
5148 //Prepend XML standard header to info gathered
5149 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5150 //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY)
5151 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5152 $data = xmlize($xml_data,0);
5153 //echo strftime ("%X",time())."<p>"; //Debug
5154 //traverse_xmlize($data); //Debug
5155 //print_object ($GLOBALS['traverse_array']); //Debug
5156 //$GLOBALS['traverse_array']=""; //Debug
5157 //Now, save data to db. We'll use it later
5158 //Get id from data
5159 $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"];
5160 //Save to db
5161 $status = backup_putid($this->preferences->backup_unique_code,"question_categories",$category_id,
5162 null,$data);
5163 //Create returning info
5164 $ret_info->id = $category_id;
5165 $this->info[] = $ret_info;
5166 //Reset temp
5167 unset($this->temp);
5171 //Stop parsing if todo = QUESTION_CATEGORIES and tagName = QUESTION_CATEGORY (en of the tag, of course)
5172 //Speed up a lot (avoid parse all)
5173 if ($tagName == "QUESTION_CATEGORIES" and $this->level == 3) {
5174 $this->finished = true;
5177 //Clear things
5178 $this->tree[$this->level] = "";
5179 $this->level--;
5180 $this->content = "";
5184 //This is the endTag handler we use where we are reading the scales zone (todo="SCALES")
5185 function endElementScales($parser, $tagName) {
5186 //Check if we are into SCALES zone
5187 if ($this->tree[3] == "SCALES") {
5188 //if (trim($this->content)) //Debug
5189 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5190 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5191 //Acumulate data to info (content + close tag)
5192 //Reconvert: strip htmlchars again and trim to generate xml data
5193 if (!isset($this->temp)) {
5194 $this->temp = "";
5196 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5197 //If we've finished a scale, xmlize it an save to db
5198 if (($this->level == 4) and ($tagName == "SCALE")) {
5199 //Prepend XML standard header to info gathered
5200 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5201 //Call to xmlize for this portion of xml data (one SCALE)
5202 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5203 $data = xmlize($xml_data,0);
5204 //echo strftime ("%X",time())."<p>"; //Debug
5205 //traverse_xmlize($data); //Debug
5206 //print_object ($GLOBALS['traverse_array']); //Debug
5207 //$GLOBALS['traverse_array']=""; //Debug
5208 //Now, save data to db. We'll use it later
5209 //Get id and from data
5210 $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"];
5211 //Save to db
5212 $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id,
5213 null,$data);
5214 //Create returning info
5215 $ret_info->id = $scale_id;
5216 $this->info[] = $ret_info;
5217 //Reset temp
5218 unset($this->temp);
5222 //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course)
5223 //Speed up a lot (avoid parse all)
5224 if ($tagName == "SCALES" and $this->level == 3) {
5225 $this->finished = true;
5228 //Clear things
5229 $this->tree[$this->level] = "";
5230 $this->level--;
5231 $this->content = "";
5235 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPS")
5236 function endElementGroups($parser, $tagName) {
5237 //Check if we are into GROUPS zone
5238 if ($this->tree[3] == "GROUPS") {
5239 //if (trim($this->content)) //Debug
5240 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5241 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5242 //Acumulate data to info (content + close tag)
5243 //Reconvert: strip htmlchars again and trim to generate xml data
5244 if (!isset($this->temp)) {
5245 $this->temp = "";
5247 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5248 //If we've finished a group, xmlize it an save to db
5249 if (($this->level == 4) and ($tagName == "GROUP")) {
5250 //Prepend XML standard header to info gathered
5251 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5252 //Call to xmlize for this portion of xml data (one GROUP)
5253 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5254 $data = xmlize($xml_data,0);
5255 //echo strftime ("%X",time())."<p>"; //Debug
5256 //traverse_xmlize($data); //Debug
5257 //print_object ($GLOBALS['traverse_array']); //Debug
5258 //$GLOBALS['traverse_array']=""; //Debug
5259 //Now, save data to db. We'll use it later
5260 //Get id and from data
5261 $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
5262 //Save to db
5263 $status = backup_putid($this->preferences->backup_unique_code,"groups",$group_id,
5264 null,$data);
5265 //Create returning info
5266 $ret_info = new Object();
5267 $ret_info->id = $group_id;
5268 $this->info[] = $ret_info;
5269 //Reset temp
5270 unset($this->temp);
5274 //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
5275 //Speed up a lot (avoid parse all)
5276 if ($tagName == "GROUPS" and $this->level == 3) {
5277 $this->finished = true;
5280 //Clear things
5281 $this->tree[$this->level] = "";
5282 $this->level--;
5283 $this->content = "";
5287 //This is the endTag handler we use where we are reading the groups zone (todo="GROUPINGS")
5288 function endElementGroupings($parser, $tagName) { //TODO:
5289 //Check if we are into GROUPS zone
5290 if ($this->tree[3] == "GROUPINGS") {
5291 //Acumulate data to info (content + close tag)
5292 //Reconvert: strip htmlchars again and trim to generate xml data
5293 if (!isset($this->temp)) {
5294 $this->temp = "";
5296 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5297 //If we've finished a group, xmlize it an save to db
5298 if (($this->level == 4) and ($tagName == "GROUPING")) {
5299 //Prepend XML standard header to info gathered
5300 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5301 //Call to xmlize for this portion of xml data (one GROUPING)
5302 $data = xmlize($xml_data,0);
5303 //Now, save data to db. We'll use it later
5304 //Get id and from data
5305 $grouping_id = $data["GROUPING"]["#"]["ID"]["0"]["#"];
5306 //Save to db
5307 $status = backup_putid($this->preferences->backup_unique_code,"groupings",$grouping_id,
5308 null,$data);
5309 //Create returning info
5310 $ret_info = new Object();
5311 $ret_info->id = $grouping_id;
5312 $this->info[] = $ret_info;
5313 //Reset temp
5314 unset($this->temp);
5318 //Stop parsing if todo = GROUPINGS and tagName = GROUPING (en of the tag, of course)
5319 //Speed up a lot (avoid parse all)
5320 if ($tagName == "GROUPINGS" and $this->level == 3) {
5321 $this->finished = true;
5324 //Clear things
5325 $this->tree[$this->level] = "";
5326 $this->level--;
5327 $this->content = "";
5331 //This is the endTag handler we use where we are reading the events zone (todo="EVENTS")
5332 function endElementEvents($parser, $tagName) {
5333 //Check if we are into EVENTS zone
5334 if ($this->tree[3] == "EVENTS") {
5335 //if (trim($this->content)) //Debug
5336 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5337 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5338 //Acumulate data to info (content + close tag)
5339 //Reconvert: strip htmlchars again and trim to generate xml data
5340 if (!isset($this->temp)) {
5341 $this->temp = "";
5343 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5344 //If we've finished a event, xmlize it an save to db
5345 if (($this->level == 4) and ($tagName == "EVENT")) {
5346 //Prepend XML standard header to info gathered
5347 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5348 //Call to xmlize for this portion of xml data (one EVENT)
5349 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5350 $data = xmlize($xml_data,0);
5351 //echo strftime ("%X",time())."<p>"; //Debug
5352 //traverse_xmlize($data); //Debug
5353 //print_object ($GLOBALS['traverse_array']); //Debug
5354 //$GLOBALS['traverse_array']=""; //Debug
5355 //Now, save data to db. We'll use it later
5356 //Get id and from data
5357 $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"];
5358 //Save to db
5359 $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id,
5360 null,$data);
5361 //Create returning info
5362 $ret_info->id = $event_id;
5363 $this->info[] = $ret_info;
5364 //Reset temp
5365 unset($this->temp);
5369 //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course)
5370 //Speed up a lot (avoid parse all)
5371 if ($tagName == "EVENTS" and $this->level == 3) {
5372 $this->finished = true;
5375 //Clear things
5376 $this->tree[$this->level] = "";
5377 $this->level--;
5378 $this->content = "";
5382 //This is the endTag handler we use where we are reading the modules zone (todo="MODULES")
5383 function endElementModules($parser, $tagName) {
5384 //Check if we are into MODULES zone
5385 if ($this->tree[3] == "MODULES") {
5386 //if (trim($this->content)) //Debug
5387 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5388 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5389 //Acumulate data to info (content + close tag)
5390 //Reconvert: strip htmlchars again and trim to generate xml data
5391 if (!isset($this->temp)) {
5392 $this->temp = "";
5394 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5395 //If we've finished a mod, xmlize it an save to db
5396 if (($this->level == 4) and ($tagName == "MOD")) {
5397 //Prepend XML standard header to info gathered
5398 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5399 //Call to xmlize for this portion of xml data (one MOD)
5400 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5401 $data = xmlize($xml_data,0);
5402 //echo strftime ("%X",time())."<p>"; //Debug
5403 //traverse_xmlize($data); //Debug
5404 //print_object ($GLOBALS['traverse_array']); //Debug
5405 //$GLOBALS['traverse_array']=""; //Debug
5406 //Now, save data to db. We'll use it later
5407 //Get id and modtype from data
5408 $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"];
5409 $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"];
5410 //Only if we've selected to restore it
5411 if (!empty($this->preferences->mods[$mod_type]->restore)) {
5412 //Save to db
5413 $status = backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id,
5414 null,$data);
5415 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
5416 //Create returning info
5417 $ret_info->id = $mod_id;
5418 $ret_info->modtype = $mod_type;
5419 $this->info[] = $ret_info;
5421 //Reset temp
5422 unset($this->temp);
5428 //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course)
5429 //Speed up a lot (avoid parse all)
5430 if ($tagName == "MODULES" and $this->level == 3) {
5431 $this->finished = true;
5434 //Clear things
5435 $this->tree[$this->level] = "";
5436 $this->level--;
5437 $this->content = "";
5441 //This is the endTag handler we use where we are reading the logs zone (todo="LOGS")
5442 function endElementLogs($parser, $tagName) {
5443 //Check if we are into LOGS zone
5444 if ($this->tree[3] == "LOGS") {
5445 //if (trim($this->content)) //Debug
5446 // echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5447 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5448 //Acumulate data to info (content + close tag)
5449 //Reconvert: strip htmlchars again and trim to generate xml data
5450 if (!isset($this->temp)) {
5451 $this->temp = "";
5453 $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
5454 //If we've finished a log, xmlize it an save to db
5455 if (($this->level == 4) and ($tagName == "LOG")) {
5456 //Prepend XML standard header to info gathered
5457 $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
5458 //Call to xmlize for this portion of xml data (one LOG)
5459 //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug
5460 $data = xmlize($xml_data,0);
5461 //echo strftime ("%X",time())."<p>"; //Debug
5462 //traverse_xmlize($data); //Debug
5463 //print_object ($GLOBALS['traverse_array']); //Debug
5464 //$GLOBALS['traverse_array']=""; //Debug
5465 //Now, save data to db. We'll use it later
5466 //Get id and modtype from data
5467 $log_id = $data["LOG"]["#"]["ID"]["0"]["#"];
5468 $log_module = $data["LOG"]["#"]["MODULE"]["0"]["#"];
5469 //We only save log entries from backup file if they are:
5470 // - Course logs
5471 // - User logs
5472 // - Module logs about one restored module
5473 if ($log_module == "course" or
5474 $log_module == "user" or
5475 $this->preferences->mods[$log_module]->restore) {
5476 //Increment counter
5477 $this->counter++;
5478 //Save to db
5479 $status = backup_putid($this->preferences->backup_unique_code,"log",$log_id,
5480 null,$data);
5481 //echo "<p>id: ".$mod_id."-".$mod_type." len.: ".strlen($sla_mod_temp)." to_db: ".$status."<p>"; //Debug
5482 //Create returning info
5483 $this->info = $this->counter;
5485 //Reset temp
5486 unset($this->temp);
5490 //Stop parsing if todo = LOGS and tagName = LOGS (en of the tag, of course)
5491 //Speed up a lot (avoid parse all)
5492 if ($tagName == "LOGS" and $this->level == 3) {
5493 $this->finished = true;
5494 $this->counter = 0;
5497 //Clear things
5498 $this->tree[$this->level] = "";
5499 $this->level--;
5500 $this->content = "";
5504 //This is the endTag default handler we use when todo is undefined
5505 function endElement($parser, $tagName) {
5506 if (trim($this->content)) //Debug
5507 echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
5508 echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n"; //Debug
5510 //Clear things
5511 $this->tree[$this->level] = "";
5512 $this->level--;
5513 $this->content = "";
5516 //This is the handler to read data contents (simple accumule it)
5517 function characterData($parser, $data) {
5518 $this->content .= $data;
5522 //This function executes the MoodleParser
5523 function restore_read_xml ($xml_file,$todo,$preferences) {
5525 $status = true;
5527 $xml_parser = xml_parser_create('UTF-8');
5528 $moodle_parser = new MoodleParser();
5529 $moodle_parser->todo = $todo;
5530 $moodle_parser->preferences = $preferences;
5531 xml_set_object($xml_parser,$moodle_parser);
5532 //Depending of the todo we use some element_handler or another
5533 if ($todo == "INFO") {
5534 //Define handlers to that zone
5535 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
5536 } else if ($todo == "ROLES") {
5537 // Define handlers to that zone
5538 xml_set_element_handler($xml_parser, "startElementRoles", "endElementRoles");
5539 } else if ($todo == "COURSE_HEADER") {
5540 //Define handlers to that zone
5541 xml_set_element_handler($xml_parser, "startElementCourseHeader", "endElementCourseHeader");
5542 } else if ($todo == 'BLOCKS') {
5543 //Define handlers to that zone
5544 xml_set_element_handler($xml_parser, "startElementBlocks", "endElementBlocks");
5545 } else if ($todo == "SECTIONS") {
5546 //Define handlers to that zone
5547 xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
5548 } else if ($todo == 'FORMATDATA') {
5549 //Define handlers to that zone
5550 xml_set_element_handler($xml_parser, "startElementFormatData", "endElementFormatData");
5551 } else if ($todo == "METACOURSE") {
5552 //Define handlers to that zone
5553 xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
5554 } else if ($todo == "GRADEBOOK") {
5555 //Define handlers to that zone
5556 xml_set_element_handler($xml_parser, "startElementGradebook", "endElementGradebook");
5557 } else if ($todo == "USERS") {
5558 //Define handlers to that zone
5559 xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");
5560 } else if ($todo == "MESSAGES") {
5561 //Define handlers to that zone
5562 xml_set_element_handler($xml_parser, "startElementMessages", "endElementMessages");
5563 } else if ($todo == "QUESTIONS") {
5564 //Define handlers to that zone
5565 xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions");
5566 } else if ($todo == "SCALES") {
5567 //Define handlers to that zone
5568 xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
5569 } else if ($todo == "GROUPS") {
5570 //Define handlers to that zone
5571 xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
5572 } else if ($todo == "GROUPINGS") {
5573 //Define handlers to that zone
5574 xml_set_element_handler($xml_parser, "startElementGroupings", "endElementGroupings");
5575 } else if ($todo == "EVENTS") {
5576 //Define handlers to that zone
5577 xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents");
5578 } else if ($todo == "MODULES") {
5579 //Define handlers to that zone
5580 xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
5581 } else if ($todo == "LOGS") {
5582 //Define handlers to that zone
5583 xml_set_element_handler($xml_parser, "startElementLogs", "endElementLogs");
5584 } else {
5585 //Define default handlers (must no be invoked when everything become finished)
5586 xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");
5588 xml_set_character_data_handler($xml_parser, "characterData");
5589 $fp = fopen($xml_file,"r")
5590 or $status = false;
5591 if ($status) {
5592 while ($data = fread($fp, 4096) and !$moodle_parser->finished)
5593 xml_parse($xml_parser, $data, feof($fp))
5594 or die(sprintf("XML error: %s at line %d",
5595 xml_error_string(xml_get_error_code($xml_parser)),
5596 xml_get_current_line_number($xml_parser)));
5597 fclose($fp);
5599 //Get info from parser
5600 $info = $moodle_parser->info;
5602 //Clear parser mem
5603 xml_parser_free($xml_parser);
5605 if ($status && !empty($info)) {
5606 return $info;
5607 } else {
5608 return $status;
5613 * @param string $errorstr passed by reference, if silent is true,
5614 * errorstr will be populated and this function will return false rather than calling error() or notify()
5615 * @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
5616 * redirect to the next step in the restore process, instead will return $backup_unique_code
5618 function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
5620 global $CFG, $SESSION;
5622 //Prepend dataroot to variable to have the absolute path
5623 $file = $CFG->dataroot."/".$file;
5625 if (!defined('RESTORE_SILENTLY')) {
5626 //Start the main table
5627 echo "<table cellpadding=\"5\">";
5628 echo "<tr><td>";
5630 //Start the mail ul
5631 echo "<ul>";
5634 //Check the file exists
5635 if (!is_file($file)) {
5636 if (!defined('RESTORE_SILENTLY')) {
5637 error ("File not exists ($file)");
5638 } else {
5639 $errorstr = "File not exists ($file)";
5640 return false;
5644 //Check the file name ends with .zip
5645 if (!substr($file,-4) == ".zip") {
5646 if (!defined('RESTORE_SILENTLY')) {
5647 error ("File has an incorrect extension");
5648 } else {
5649 $errorstr = 'File has an incorrect extension';
5650 return false;
5654 //Now calculate the unique_code for this restore
5655 $backup_unique_code = time();
5657 //Now check and create the backup dir (if it doesn't exist)
5658 if (!defined('RESTORE_SILENTLY')) {
5659 echo "<li>".get_string("creatingtemporarystructures").'</li>';
5661 $status = check_and_create_backup_dir($backup_unique_code);
5662 //Empty dir
5663 if ($status) {
5664 $status = clear_backup_dir($backup_unique_code);
5667 //Now delete old data and directories under dataroot/temp/backup
5668 if ($status) {
5669 if (!defined('RESTORE_SILENTLY')) {
5670 echo "<li>".get_string("deletingolddata").'</li>';
5672 $status = backup_delete_old_data();
5675 //Now copy he zip file to dataroot/temp/backup/backup_unique_code
5676 if ($status) {
5677 if (!defined('RESTORE_SILENTLY')) {
5678 echo "<li>".get_string("copyingzipfile").'</li>';
5680 if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
5681 if (!defined('RESTORE_SILENTLY')) {
5682 notify("Error copying backup file. Invalid name or bad perms.");
5683 } else {
5684 $errorstr = "Error copying backup file. Invalid name or bad perms";
5685 return false;
5690 //Now unzip the file
5691 if ($status) {
5692 if (!defined('RESTORE_SILENTLY')) {
5693 echo "<li>".get_string("unzippingbackup").'</li>';
5695 if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
5696 if (!defined('RESTORE_SILENTLY')) {
5697 notify("Error unzipping backup file. Invalid zip file.");
5698 } else {
5699 $errorstr = "Error unzipping backup file. Invalid zip file.";
5700 return false;
5705 //Check for Blackboard backups and convert
5706 if ($status){
5707 require_once("$CFG->dirroot/backup/bb/restore_bb.php");
5708 if (!defined('RESTORE_SILENTLY')) {
5709 echo "<li>".get_string("checkingforbbexport").'</li>';
5711 $status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
5714 //Now check for the moodle.xml file
5715 if ($status) {
5716 $xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
5717 if (!defined('RESTORE_SILENTLY')) {
5718 echo "<li>".get_string("checkingbackup").'</li>';
5720 if (! $status = restore_check_moodle_file ($xml_file)) {
5721 if (!is_file($xml_file)) {
5722 $errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
5723 } else {
5724 $errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
5726 if (!defined('RESTORE_SILENTLY')) {
5727 notify($errorstr);
5728 } else {
5729 return false;
5734 $info = "";
5735 $course_header = "";
5737 //Now read the info tag (all)
5738 if ($status) {
5739 if (!defined('RESTORE_SILENTLY')) {
5740 echo "<li>".get_string("readinginfofrombackup").'</li>';
5742 //Reading info from file
5743 $info = restore_read_xml_info ($xml_file);
5744 //Reading course_header from file
5745 $course_header = restore_read_xml_course_header ($xml_file);
5748 if (!defined('RESTORE_SILENTLY')) {
5749 //End the main ul
5750 echo "</ul>\n";
5752 //End the main table
5753 echo "</td></tr>";
5754 echo "</table>";
5757 //We compare Moodle's versions
5758 if ($CFG->version < $info->backup_moodle_version && $status) {
5759 $message->serverversion = $CFG->version;
5760 $message->serverrelease = $CFG->release;
5761 $message->backupversion = $info->backup_moodle_version;
5762 $message->backuprelease = $info->backup_moodle_release;
5763 print_simple_box(get_string('noticenewerbackup','',$message), "center", "70%", '', "20", "noticebox");
5767 //Now we print in other table, the backup and the course it contains info
5768 if ($info and $course_header and $status) {
5769 //First, the course info
5770 if (!defined('RESTORE_SILENTLY')) {
5771 $status = restore_print_course_header($course_header);
5773 //Now, the backup info
5774 if ($status) {
5775 if (!defined('RESTORE_SILENTLY')) {
5776 $status = restore_print_info($info);
5781 //Save course header and info into php session
5782 if ($status) {
5783 $SESSION->info = $info;
5784 $SESSION->course_header = $course_header;
5787 //Finally, a little form to continue
5788 //with some hidden fields
5789 if ($status) {
5790 if (!defined('RESTORE_SILENTLY')) {
5791 echo "<br /><div style='text-align:center'>";
5792 $hidden["backup_unique_code"] = $backup_unique_code;
5793 $hidden["launch"] = "form";
5794 $hidden["file"] = $file;
5795 $hidden["id"] = $id;
5796 print_single_button("restore.php", $hidden, get_string("continue"),"post");
5797 echo "</div>";
5799 else {
5800 if (empty($noredirect)) {
5801 redirect($CFG->wwwroot.'/backup/restore.php?backup_unique_code='.$backup_unique_code.'&launch=form&file='.$file.'&id='.$id);
5802 } else {
5803 return $backup_unique_code;
5808 if (!$status) {
5809 if (!defined('RESTORE_SILENTLY')) {
5810 error ("An error has ocurred");
5811 } else {
5812 $errorstr = "An error has occured"; // helpful! :P
5813 return false;
5816 return true;
5819 function restore_setup_for_check(&$restore,$backup_unique_code) {
5820 global $SESSION;
5821 $restore->backup_unique_code=$backup_unique_code;
5822 $restore->users = 2; // yuk
5823 $restore->course_files = $SESSION->restore->restore_course_files;
5824 if ($allmods = get_records("modules")) {
5825 foreach ($allmods as $mod) {
5826 $modname = $mod->name;
5827 $var = "restore_".$modname;
5828 //Now check that we have that module info in the backup file
5829 if (isset($SESSION->info->mods[$modname]) && $SESSION->info->mods[$modname]->backup == "true") {
5830 $restore->$var = 1;
5834 return true;
5837 function backup_to_restore_array($backup,$k=0) {
5838 if (is_array($backup) ) {
5839 foreach ($backup as $key => $value) {
5840 $newkey = str_replace('backup','restore',$key);
5841 $restore[$newkey] = backup_to_restore_array($value,$key);
5844 else if (is_object($backup)) {
5845 $tmp = get_object_vars($backup);
5846 foreach ($tmp as $key => $value) {
5847 $newkey = str_replace('backup','restore',$key);
5848 $restore->$newkey = backup_to_restore_array($value,$key);
5851 else {
5852 $newkey = str_replace('backup','restore',$k);
5853 $restore = $backup;
5855 return $restore;
5858 /**
5859 * compatibility function
5860 * checks for per-instance backups AND
5861 * older per-module backups
5862 * and returns whether userdata has been selected.
5864 function restore_userdata_selected($restore,$modname,$modid) {
5865 // check first for per instance array
5866 if (!empty($restore->mods[$modname]->granular)) { // supports per instance
5867 return array_key_exists($modid,$restore->mods[$modname]->instances)
5868 && !empty($restore->mods[$modname]->instances[$modid]->userinfo);
5871 print_object($restore->mods[$modname]);
5872 return !empty($restore->mods[$modname]->userinfo);
5875 function restore_execute(&$restore,$info,$course_header,&$errorstr) {
5877 global $CFG, $USER;
5878 $status = true;
5880 //Checks for the required files/functions to restore every module
5881 //and include them
5882 if ($allmods = get_records("modules") ) {
5883 foreach ($allmods as $mod) {
5884 $modname = $mod->name;
5885 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
5886 //If file exists and we have selected to restore that type of module
5887 if ((file_exists($modfile)) and ($restore->mods[$modname]->restore)) {
5888 include_once($modfile);
5893 if (!defined('RESTORE_SILENTLY')) {
5894 //Start the main table
5895 echo "<table cellpadding=\"5\">";
5896 echo "<tr><td>";
5898 //Start the main ul
5899 echo "<ul>";
5902 //Localtion of the xml file
5903 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
5905 //If we've selected to restore into new course
5906 //create it (course)
5907 //Saving conversion id variables into backup_tables
5908 if ($restore->restoreto == 2) {
5909 if (!defined('RESTORE_SILENTLY')) {
5910 echo '<li>'.get_string('creatingnewcourse') . '</li>';
5912 $oldidnumber = $course_header->course_idnumber;
5913 if (!$status = restore_create_new_course($restore,$course_header)) {
5914 if (!defined('RESTORE_SILENTLY')) {
5915 notify("Error while creating the new empty course.");
5916 } else {
5917 $errorstr = "Error while creating the new empty course.";
5918 return false;
5922 //Print course fullname and shortname and category
5923 if ($status) {
5924 if (!defined('RESTORE_SILENTLY')) {
5925 echo "<ul>";
5926 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
5927 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
5928 if (!empty($oldidnumber)) {
5929 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
5931 echo "</ul>";
5932 //Put the destination course_id
5934 $restore->course_id = $course_header->course_id;
5937 if ($status = restore_open_html($restore,$course_header)){
5938 echo "<li>Creating the Restorelog.html in the course backup folder</li>";
5941 } else {
5942 $course = get_record("course","id",$restore->course_id);
5943 if ($course) {
5944 if (!defined('RESTORE_SILENTLY')) {
5945 echo "<li>".get_string("usingexistingcourse");
5946 echo "<ul>";
5947 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
5948 echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
5949 if (($restore->deleting)) {
5950 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
5951 } else {
5952 echo "<li>".get_string("addingdatatoexisting").'</li>';
5954 echo "</ul></li>";
5956 //If we have selected to restore deleting, we do it now.
5957 if ($restore->deleting) {
5958 if (!defined('RESTORE_SILENTLY')) {
5959 echo "<li>".get_string("deletingolddata").'</li>';
5961 $status = remove_course_contents($restore->course_id,false) and
5962 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
5963 if ($status) {
5964 //Now , this situation is equivalent to the "restore to new course" one (we
5965 //have a course record and nothing more), so define it as "to new course"
5966 $restore->restoreto = 2;
5967 } else {
5968 if (!defined('RESTORE_SILENTLY')) {
5969 notify("An error occurred while deleting some of the course contents.");
5970 } else {
5971 $errrostr = "An error occurred while deleting some of the course contents.";
5972 return false;
5976 } else {
5977 if (!defined('RESTORE_SILENTLY')) {
5978 notify("Error opening existing course.");
5979 $status = false;
5980 } else {
5981 $errorstr = "Error opening existing course.";
5982 return false;
5987 //Now create the course_sections and their associated course_modules
5988 if ($status) {
5989 //Into new course
5990 if ($restore->restoreto == 2) {
5991 if (!defined('RESTORE_SILENTLY')) {
5992 echo "<li>".get_string("creatingsections");
5994 if (!$status = restore_create_sections($restore,$xml_file)) {
5995 if (!defined('RESTORE_SILENTLY')) {
5996 notify("Error creating sections in the existing course.");
5997 } else {
5998 $errorstr = "Error creating sections in the existing course.";
5999 return false;
6002 if (!defined('RESTORE_SILENTLY')) {
6003 echo '</li>';
6005 //Into existing course
6006 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
6007 if (!defined('RESTORE_SILENTLY')) {
6008 echo "<li>".get_string("checkingsections");
6010 if (!$status = restore_create_sections($restore,$xml_file)) {
6011 if (!defined('RESTORE_SILENTLY')) {
6012 notify("Error creating sections in the existing course.");
6013 } else {
6014 $errorstr = "Error creating sections in the existing course.";
6015 return false;
6018 if (!defined('RESTORE_SILENTLY')) {
6019 echo '</li>';
6021 //Error
6022 } else {
6023 if (!defined('RESTORE_SILENTLY')) {
6024 notify("Neither a new course or an existing one was specified.");
6025 $status = false;
6026 } else {
6027 $errorstr = "Neither a new course or an existing one was specified.";
6028 return false;
6033 //Now create users as needed
6034 if ($status and ($restore->users == 0 or $restore->users == 1)) {
6035 if (!defined('RESTORE_SILENTLY')) {
6036 echo "<li>".get_string("creatingusers")."<br />";
6038 if (!$status = restore_create_users($restore,$xml_file)) {
6039 if (!defined('RESTORE_SILENTLY')) {
6040 notify("Could not restore users.");
6041 } else {
6042 $errorstr = "Could not restore users.";
6043 return false;
6047 //Now print info about the work done
6048 if ($status) {
6049 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
6050 where backup_code = '$restore->backup_unique_code' and
6051 table_name = 'user'");
6052 //We've records
6053 if ($recs) {
6054 $new_count = 0;
6055 $exists_count = 0;
6056 $student_count = 0;
6057 $teacher_count = 0;
6058 $counter = 0;
6059 //Iterate, filling counters
6060 foreach ($recs as $rec) {
6061 //Get full record, using backup_getids
6062 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
6063 if (strpos($record->info,"new") !== false) {
6064 $new_count++;
6066 if (strpos($record->info,"exists") !== false) {
6067 $exists_count++;
6069 if (strpos($record->info,"student") !== false) {
6070 $student_count++;
6071 } else if (strpos($record->info,"teacher") !== false) {
6072 $teacher_count++;
6074 //Do some output
6075 $counter++;
6076 if ($counter % 10 == 0) {
6077 if (!defined('RESTORE_SILENTLY')) {
6078 echo ".";
6079 if ($counter % 200 == 0) {
6080 echo "<br />";
6083 backup_flush(300);
6086 if (!defined('RESTORE_SILENTLY')) {
6087 //Now print information gathered
6088 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
6089 echo "<ul>";
6090 echo "<li>".get_string("students").": ".$student_count.'</li>';
6091 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
6092 echo "</ul>";
6094 } else {
6095 if (!defined('RESTORE_SILENTLY')) {
6096 notify("No users were found!");
6097 } // no need to return false here, it's recoverable.
6101 if (!defined('RESTORE_SILENTLY')) {
6102 echo "</li>";
6106 //Now create metacourse info
6107 if ($status and $restore->metacourse) {
6108 //Only to new courses!
6109 if ($restore->restoreto == 2) {
6110 if (!defined('RESTORE_SILENTLY')) {
6111 echo "<li>".get_string("creatingmetacoursedata");
6113 if (!$status = restore_create_metacourse($restore,$xml_file)) {
6114 if (!defined('RESTORE_SILENTLY')) {
6115 notify("Error creating metacourse in the course.");
6116 } else {
6117 $errorstr = "Error creating metacourse in the course.";
6118 return false;
6121 if (!defined('RESTORE_SILENTLY')) {
6122 echo '</li>';
6128 //Now create categories and questions as needed
6129 if ($status and ($restore->mods['quiz']->restore)) {
6130 include_once("$CFG->dirroot/question/restorelib.php");
6131 if (!defined('RESTORE_SILENTLY')) {
6132 echo "<li>".get_string("creatingcategoriesandquestions");
6133 echo "<ul>";
6135 if (!$status = restore_create_questions($restore,$xml_file)) {
6136 if (!defined('RESTORE_SILENTLY')) {
6137 notify("Could not restore categories and questions!");
6138 } else {
6139 $errorstr = "Could not restore categories and questions!";
6140 return false;
6143 if (!defined('RESTORE_SILENTLY')) {
6144 echo "</ul></li>";
6148 //Now create user_files as needed
6149 if ($status and ($restore->user_files)) {
6150 if (!defined('RESTORE_SILENTLY')) {
6151 echo "<li>".get_string("copyinguserfiles");
6153 if (!$status = restore_user_files($restore)) {
6154 if (!defined('RESTORE_SILENTLY')) {
6155 notify("Could not restore user files!");
6156 } else {
6157 $errorstr = "Could not restore user files!";
6158 return false;
6161 //If all is ok (and we have a counter)
6162 if ($status and ($status !== true)) {
6163 //Inform about user dirs created from backup
6164 if (!defined('RESTORE_SILENTLY')) {
6165 echo "<ul>";
6166 echo "<li>".get_string("userzones").": ".$status;
6167 echo "</li></ul>";
6170 if (!defined('RESTORE_SILENTLY')) {
6171 echo '</li>';
6175 //Now create course files as needed
6176 if ($status and ($restore->course_files)) {
6177 if (!defined('RESTORE_SILENTLY')) {
6178 echo "<li>".get_string("copyingcoursefiles");
6180 if (!$status = restore_course_files($restore)) {
6181 if (empty($status)) {
6182 notify("Could not restore course files!");
6183 } else {
6184 $errorstr = "Could not restore course files!";
6185 return false;
6188 //If all is ok (and we have a counter)
6189 if ($status and ($status !== true)) {
6190 //Inform about user dirs created from backup
6191 if (!defined('RESTORE_SILENTLY')) {
6192 echo "<ul>";
6193 echo "<li>".get_string("filesfolders").": ".$status.'</li>';
6194 echo "</ul>";
6197 if (!defined('RESTORE_SILENTLY')) {
6198 echo "</li>";
6202 //Now create messages as needed
6203 if ($status and ($restore->messages)) {
6204 if (!defined('RESTORE_SILENTLY')) {
6205 echo "<li>".get_string("creatingmessagesinfo");
6207 if (!$status = restore_create_messages($restore,$xml_file)) {
6208 if (!defined('RESTORE_SILENTLY')) {
6209 notify("Could not restore messages!");
6210 } else {
6211 $errorstr = "Could not restore messages!";
6212 return false;
6215 if (!defined('RESTORE_SILENTLY')) {
6216 echo "</li>";
6220 //Now create scales as needed
6221 if ($status) {
6222 if (!defined('RESTORE_SILENTLY')) {
6223 echo "<li>".get_string("creatingscales");
6225 if (!$status = restore_create_scales($restore,$xml_file)) {
6226 if (!defined('RESTORE_SILENTLY')) {
6227 notify("Could not restore custom scales!");
6228 } else {
6229 $errorstr = "Could not restore custom scales!";
6230 return false;
6233 if (!defined('RESTORE_SILENTLY')) {
6234 echo '</li>';
6238 //Now create groups as needed
6239 if ($status) {
6240 if (!defined('RESTORE_SILENTLY')) {
6241 echo "<li>".get_string("creatinggroups");
6243 if (!$status = restore_create_groups($restore,$xml_file)) {
6244 if (!defined('RESTORE_SILENTLY')) {
6245 notify("Could not restore groups!");
6246 } else {
6247 $errorstr = "Could not restore groups!";
6248 return false;
6251 if (!defined('RESTORE_SILENTLY')) {
6252 echo '</li>';
6256 //Now create groupings as needed
6257 if ($status) {
6258 if (!defined('RESTORE_SILENTLY')) {
6259 echo "<li>".get_string("creatinggroupings");
6261 if (!$status = restore_create_groupings($restore,$xml_file)) {
6262 if (!defined('RESTORE_SILENTLY')) {
6263 notify("Could not restore groupings!");
6264 } else {
6265 $errorstr = "Could not restore groupings!";
6266 return false;
6269 if (!defined('RESTORE_SILENTLY')) {
6270 echo '</li>';
6274 //Now create events as needed
6275 if ($status) {
6276 if (!defined('RESTORE_SILENTLY')) {
6277 echo "<li>".get_string("creatingevents");
6279 if (!$status = restore_create_events($restore,$xml_file)) {
6280 if (!defined('RESTORE_SILENTLY')) {
6281 notify("Could not restore course events!");
6282 } else {
6283 $errorstr = "Could not restore course events!";
6284 return false;
6287 if (!defined('RESTORE_SILENTLY')) {
6288 echo '</li>';
6292 //Now create course modules as needed
6293 if ($status) {
6294 if (!defined('RESTORE_SILENTLY')) {
6295 echo "<li>".get_string("creatingcoursemodules");
6297 if (!$status = restore_create_modules($restore,$xml_file)) {
6298 if (!defined('RESTORE_SILENTLY')) {
6299 notify("Could not restore modules!");
6300 } else {
6301 $errorstr = "Could not restore modules!";
6302 return false;
6305 if (!defined('RESTORE_SILENTLY')) {
6306 echo '</li>';
6310 //Now create gradebook as needed -- AFTER modules!!!
6311 if ($status) {
6312 if (!defined('RESTORE_SILENTLY')) {
6313 echo "<li>".get_string("creatinggradebook");
6315 if (!$status = restore_create_gradebook($restore,$xml_file)) {
6316 if (!defined('RESTORE_SILENTLY')) {
6317 notify("Could not restore gradebook!");
6318 } else {
6319 $errorstr = "Could not restore gradebook!";
6320 return false;
6323 if (!defined('RESTORE_SILENTLY')) {
6324 echo '</li>';
6328 //Bring back the course blocks -- do it AFTER the modules!!!
6329 if($status) {
6330 //If we are deleting and bringing into a course or making a new course, same situation
6331 if($restore->restoreto == 0 || $restore->restoreto == 2) {
6332 if (!defined('RESTORE_SILENTLY')) {
6333 echo '<li>'.get_string('creatingblocks');
6335 $course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
6336 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
6337 if (!defined('RESTORE_SILENTLY')) {
6338 notify('Error while creating the course blocks');
6339 } else {
6340 $errorstr = "Error while creating the course blocks";
6341 return false;
6344 if (!defined('RESTORE_SILENTLY')) {
6345 echo '</li>';
6350 if($status) {
6351 //If we are deleting and bringing into a course or making a new course, same situation
6352 if($restore->restoreto == 0 || $restore->restoreto == 2) {
6353 if (!defined('RESTORE_SILENTLY')) {
6354 echo '<li>'.get_string('courseformatdata');
6356 if (!$status = restore_set_format_data($restore, $xml_file)) {
6357 $error = "Error while setting the course format data";
6358 if (!defined('RESTORE_SILENTLY')) {
6359 notify($error);
6360 } else {
6361 $errorstr=$error;
6362 return false;
6365 if (!defined('RESTORE_SILENTLY')) {
6366 echo '</li>';
6371 //Now create log entries as needed
6372 if ($status and ($restore->logs)) {
6373 if (!defined('RESTORE_SILENTLY')) {
6374 echo "<li>".get_string("creatinglogentries");
6376 if (!$status = restore_create_logs($restore,$xml_file)) {
6377 if (!defined('RESTORE_SILENTLY')) {
6378 notify("Could not restore logs!");
6379 } else {
6380 $errorstr = "Could not restore logs!";
6381 return false;
6384 if (!defined('RESTORE_SILENTLY')) {
6385 echo '</li>';
6389 //Now, if all is OK, adjust the instance field in course_modules !!
6390 if ($status) {
6391 if (!defined('RESTORE_SILENTLY')) {
6392 echo "<li>".get_string("checkinginstances");
6394 if (!$status = restore_check_instances($restore)) {
6395 if (!defined('RESTORE_SILENTLY')) {
6396 notify("Could not adjust instances in course_modules!");
6397 } else {
6398 $errorstr = "Could not adjust instances in course_modules!";
6399 return false;
6402 if (!defined('RESTORE_SILENTLY')) {
6403 echo '</li>';
6407 //Now, if all is OK, adjust activity events
6408 if ($status) {
6409 if (!defined('RESTORE_SILENTLY')) {
6410 echo "<li>".get_string("refreshingevents");
6412 if (!$status = restore_refresh_events($restore)) {
6413 if (!defined('RESTORE_SILENTLY')) {
6414 notify("Could not refresh events for activities!");
6415 } else {
6416 $errorstr = "Could not refresh events for activities!";
6417 return false;
6420 if (!defined('RESTORE_SILENTLY')) {
6421 echo '</li>';
6425 //Now, if all is OK, adjust inter-activity links
6426 if ($status) {
6427 if (!defined('RESTORE_SILENTLY')) {
6428 echo "<li>".get_string("decodinginternallinks");
6430 if (!$status = restore_decode_content_links($restore)) {
6431 if (!defined('RESTORE_SILENTLY')) {
6432 notify("Could not decode content links!");
6433 } else {
6434 $errorstr = "Could not decode content links!";
6435 return false;
6438 if (!defined('RESTORE_SILENTLY')) {
6439 echo '</li>';
6443 //Now, with backup files prior to version 2005041100,
6444 //convert all the wiki texts in the course to markdown
6445 if ($status && $restore->backup_version < 2005041100) {
6446 if (!defined('RESTORE_SILENTLY')) {
6447 echo "<li>".get_string("convertingwikitomarkdown");
6449 if (!$status = restore_convert_wiki2markdown($restore)) {
6450 if (!defined('RESTORE_SILENTLY')) {
6451 notify("Could not convert wiki texts to markdown!");
6452 } else {
6453 $errorstr = "Could not convert wiki texts to markdown!";
6454 return false;
6457 if (!defined('RESTORE_SILENTLY')) {
6458 echo '</li>';
6462 /*******************************************************************************
6463 ************* Restore of Roles and Capabilities happens here ******************
6464 *******************************************************************************/
6465 $status = restore_create_roles($restore, $xml_file);
6466 $status = restore_roles_settings($restore, $xml_file);
6468 //Now if all is OK, update:
6469 // - course modinfo field
6470 // - categories table
6471 // - add user as teacher
6472 if ($status) {
6473 if (!defined('RESTORE_SILENTLY')) {
6474 echo "<li>".get_string("checkingcourse");
6476 //modinfo field
6477 rebuild_course_cache($restore->course_id);
6478 //categories table
6479 $course = get_record("course","id",$restore->course_id);
6480 fix_course_sortorder();
6481 // Check if the user has course update capability in the newly restored course
6482 // there is no need to load his capabilities again, because restore_roles_settings
6483 // would have loaded it anyway, if there is any assignments.
6484 // fix for MDL-6831
6485 $newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
6486 if (!has_capability('moodle/course:manageactivities', $newcontext)) {
6487 // fix for MDL-9065, use the new config setting if exists
6488 if ($CFG->creatornewroleid) {
6489 role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
6490 } else {
6491 if ($legacyteachers = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM, SITEID))) {
6492 if ($legacyteacher = array_shift($legacyteachers)) {
6493 role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
6495 } else {
6496 notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
6500 if (!defined('RESTORE_SILENTLY')) {
6501 echo '</li>';
6505 //Cleanup temps (files and db)
6506 if ($status) {
6507 if (!defined('RESTORE_SILENTLY')) {
6508 echo "<li>".get_string("cleaningtempdata");
6510 if (!$status = clean_temp_data ($restore)) {
6511 if (!defined('RESTORE_SILENTLY')) {
6512 notify("Could not clean up temporary data from files and database");
6513 } else {
6514 $errorstr = "Could not clean up temporary data from files and database";
6515 return false;
6518 if (!defined('RESTORE_SILENTLY')) {
6519 echo '</li>';
6523 if ($status = restore_close_html($restore)){
6524 if (!defined('RESTORE_SILENTLY')) {
6525 echo '<li>Closing the Restorelog.html file.</li>';
6528 else {
6529 if (!defined('RESTORE_SILENTLY')) {
6530 notify("Could not close the restorelog.html file");
6532 else {
6533 $errorstr = "Could not close the restorelog.html file";
6534 return false;
6538 if (!defined('RESTORE_SILENTLY')) {
6539 //End the main ul
6540 echo "</ul>";
6542 //End the main table
6543 echo "</td></tr>";
6544 echo "</table>";
6547 return $status;
6549 //Create, open and write header of the html log file
6550 function restore_open_html($restore,$course_header) {
6552 global $CFG;
6554 $status = true;
6556 //Open file for writing
6557 //First, we check the course_id backup data folder exists and create it as necessary in CFG->dataroot
6558 if (!$dest_dir = make_upload_directory("$restore->course_id/backupdata")) { // Backup folder
6559 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
6561 $status = check_dir_exists($dest_dir,true);
6562 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
6563 //Add the stylesheet
6564 $stylesheetshtml = '';
6565 foreach ($CFG->stylesheets as $stylesheet) {
6566 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
6568 ///Accessibility: added the 'lang' attribute to $direction, used in theme <html> tag.
6569 $languagehtml = get_html_lang($dir=true);
6571 //Write the header in the new logging file
6572 fwrite ($restorelog_file,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
6573 fwrite ($restorelog_file," \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
6574 fwrite ($restorelog_file,"<html dir=\"ltr\".$languagehtml.");
6575 fwrite ($restorelog_file,"<head>");
6576 fwrite ($restorelog_file,$stylesheetshtml);
6577 fwrite ($restorelog_file,"<title>".$course_header->course_shortname." Restored </title>");
6578 fwrite ($restorelog_file,"</head><body><br/><h1>The following changes were made during the Restoration of this Course.</h1><br/><br/>");
6579 fwrite ($restorelog_file,"The Course ShortName is now - ".$course_header->course_shortname." The FullName is now - ".$course_header->course_fullname."<br/><br/>");
6580 $startdate = addslashes($course_header->course_startdate);
6581 $date = usergetdate($startdate);
6582 fwrite ($restorelog_file,"The Originating Courses Start Date was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
6583 $startdate += $restore->course_startdateoffset;
6584 $date = usergetdate($startdate);
6585 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;This Courses Start Date is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/><br/>");
6587 if ($status) {
6588 return $restorelog_file;
6589 } else {
6590 return false;
6593 //Create & close footer of the html log file
6594 function restore_close_html($restore) {
6596 global $CFG;
6598 $status = true;
6600 //Open file for writing
6601 //First, check that course_id/backupdata folder exists in CFG->dataroot
6602 $dest_dir = $CFG->dataroot."/".$restore->course_id."/backupdata";
6603 $status = check_dir_exists($dest_dir, true, true);
6604 $restorelog_file = fopen("$dest_dir/restorelog.html","a");
6605 //Write the footer to close the logging file
6606 fwrite ($restorelog_file,"<br/>This file was written to directly by each modules restore process.");
6607 fwrite ($restorelog_file,"<br/><br/>Log complete.</body></html>");
6609 if ($status) {
6610 return $restorelog_file;
6611 } else {
6612 return false;
6616 /********************** Roles and Capabilities Related Functions *******************************/
6618 /* Yu: Note recovering of role assignments/overrides need to take place after
6619 users have been recovered, i.e. after we get their new_id, and after all
6620 roles have been recreated or mapped. Contexts can be created on the fly.
6621 The current order of restore is Restore (old) -> restore roles -> restore assignment/overrides
6622 the order of restore among different contexts, i.e. course, mod, blocks, users should not matter
6623 once roles and users have been restored.
6627 * This function restores all the needed roles for this course
6628 * i.e. roles with an assignment in any of the mods or blocks,
6629 * roles assigned on any user (e.g. parent role) and roles
6630 * assigned at course levle
6631 * This function should check for duplicate roles first
6632 * It isn't now, just overwriting
6634 function restore_create_roles($restore, $xmlfile) {
6635 if (!defined('RESTORE_SILENTLY')) {
6636 echo "<li>".get_string("creatingrolesdefinitions").'</li>';
6638 $info = restore_read_xml_roles($xmlfile);
6640 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
6642 // the following code creates new roles
6643 // but we could use more intelligent detection, and role mapping
6644 // get role mapping info from $restore
6645 $rolemappings = array();
6647 if (!empty($restore->rolesmapping)) {
6648 $rolemappings = $restore->rolesmapping;
6650 // $info->roles will be empty for backups pre 1.7
6651 if (isset($info->roles) && $info->roles) {
6653 foreach ($info->roles as $oldroleid=>$roledata) {
6654 if (empty($restore->rolesmapping)) {
6655 // if this is empty altogether, we came from import or there's no roles used in course at all
6656 // in this case, write the same oldid as this is the same site
6657 // no need to do mapping
6658 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
6659 $oldroleid); // adding a new id
6660 continue; // do not create additonal roles;
6662 // first we check if the roles are in the mappings
6663 // if so, we just do a mapping i.e. update oldids table
6664 if (isset($rolemappings[$oldroleid]) && $rolemappings[$oldroleid]) {
6665 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
6666 $rolemappings[$oldroleid]); // adding a new id
6668 } else {
6670 // code to make new role name/short name if same role name or shortname exists
6671 $fullname = $roledata->name;
6672 $shortname = $roledata->shortname;
6673 $currentfullname = "";
6674 $currentshortname = "";
6675 $counter = 0;
6677 do {
6678 if ($counter) {
6679 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
6680 $suffixshort = "_".$counter;
6681 } else {
6682 $suffixfull = "";
6683 $suffixshort = "";
6685 $currentfullname = $fullname.$suffixfull;
6686 // Limit the size of shortname - database column accepts <= 15 chars
6687 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
6688 $coursefull = get_record("role","name",addslashes($currentfullname));
6689 $courseshort = get_record("role","shortname",addslashes($currentshortname));
6690 $counter++;
6691 } while ($coursefull || $courseshort);
6693 $roledata->name = $currentfullname;
6694 $roledata->shortname= $currentshortname;
6696 // done finding a unique name
6698 $newroleid = create_role(addslashes($roledata->name),addslashes($roledata->shortname),'');
6699 $status = backup_putid($restore->backup_unique_code,"role",$oldroleid,
6700 $newroleid); // adding a new id
6701 foreach ($roledata->capabilities as $capability) {
6703 $roleinfo = new object();
6704 $roleinfo = (object)$capability;
6705 $roleinfo->contextid = $sitecontext->id;
6706 $roleinfo->capability = $capability->name;
6707 $roleinfo->roleid = $newroleid;
6709 insert_record('role_capabilities', $roleinfo);
6714 return true;
6718 * this function restores role assignments and role overrides
6719 * in course/user/block/mod level, it passed through
6720 * the xml file again
6722 function restore_roles_settings($restore, $xmlfile) {
6723 // data pulls from course, mod, user, and blocks
6725 /*******************************************************
6726 * Restoring from course level assignments *
6727 *******************************************************/
6728 if (!defined('RESTORE_SILENTLY')) {
6729 echo "<li>".get_string("creatingcourseroles").'</li>';
6731 $course = restore_read_xml_course_header($xmlfile);
6733 if (!isset($restore->rolesmapping)) {
6734 $isimport = true; // course import from another course, or course with no role assignments
6735 } else {
6736 $isimport = false; // course restore with role assignments
6739 if (!empty($course->roleassignments) && !$isimport) {
6740 $courseassignments = $course->roleassignments;
6742 foreach ($courseassignments as $oldroleid => $courseassignment) {
6743 restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
6746 /*****************************************************
6747 * Restoring from course level overrides *
6748 *****************************************************/
6750 if (!empty($course->roleoverrides) && !$isimport) {
6751 $courseoverrides = $course->roleoverrides;
6752 foreach ($courseoverrides as $oldroleid => $courseoverride) {
6753 // if not importing into exiting course, or creating new role, we are ok
6754 // local course overrides to be respected (i.e. restored course overrides ignored)
6755 if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
6756 restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
6761 /*******************************************************
6762 * Restoring role assignments/overrdies *
6763 * from module level assignments *
6764 *******************************************************/
6766 if (!defined('RESTORE_SILENTLY')) {
6767 echo "<li>".get_string("creatingmodroles").'</li>';
6769 $sections = restore_read_xml_sections($xmlfile);
6770 $secs = $sections->sections;
6772 foreach ($secs as $section) {
6773 if (isset($section->mods)) {
6774 foreach ($section->mods as $modid=>$mod) {
6775 if (isset($mod->roleassignments) && !$isimport) {
6776 foreach ($mod->roleassignments as $oldroleid=>$modassignment) {
6777 restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
6780 // role overrides always applies, in import or backup/restore
6781 if (isset($mod->roleoverrides)) {
6782 foreach ($mod->roleoverrides as $oldroleid=>$modoverride) {
6783 restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
6790 /*************************************************
6791 * Restoring assignments from blocks level *
6792 * role assignments/overrides *
6793 *************************************************/
6795 if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
6796 if (!defined('RESTORE_SILENTLY')) {
6797 echo "<li>".get_string("creatingblocksroles").'</li>';
6799 $blocks = restore_read_xml_blocks($xmlfile);
6800 if (isset($blocks->instances)) {
6801 foreach ($blocks->instances as $instance) {
6802 if (isset($instance->roleassignments) && !$isimport) {
6803 foreach ($instance->roleassignments as $oldroleid=>$blockassignment) {
6804 restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
6808 // likewise block overrides should always be restored like mods
6809 if (isset($instance->roleoverrides)) {
6810 foreach ($instance->roleoverrides as $oldroleid=>$blockoverride) {
6811 restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
6817 /************************************************
6818 * Restoring assignments from userid level *
6819 * role assignments/overrides *
6820 ************************************************/
6821 if (!defined('RESTORE_SILENTLY')) {
6822 echo "<li>".get_string("creatinguserroles").'</li>';
6824 $info = restore_read_xml_users($restore, $xmlfile);
6825 if (!empty($info->users) && !$isimport) { // no need to restore user assignments for imports (same course)
6826 //For each user, take its info from backup_ids
6827 foreach ($info->users as $userid) {
6828 $rec = backup_getid($restore->backup_unique_code,"user",$userid);
6829 if (isset($rec->info->roleassignments)) {
6830 foreach ($rec->info->roleassignments as $oldroleid=>$userassignment) {
6831 restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
6834 if (isset($rec->info->roleoverrides)) {
6835 foreach ($rec->info->roleoverrides as $oldroleid=>$useroverride) {
6836 restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
6842 return true;
6845 // auxillary function to write role assignments read from xml to db
6846 function restore_write_roleassignments($restore, $assignments, $table, $contextlevel, $oldid, $oldroleid) {
6848 $role = backup_getid($restore->backup_unique_code, "role", $oldroleid);
6850 foreach ($assignments as $assignment) {
6852 $olduser = backup_getid($restore->backup_unique_code,"user",$assignment->userid);
6853 //Oh dear, $olduser... can be an object, $obj->string or bool!
6854 if (!$olduser || (is_string($olduser->info) && $olduser->info == "notincourse")) { // it's possible that user is not in the course
6855 continue;
6857 $assignment->userid = $olduser->new_id; // new userid here
6858 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$assignment->modifierid);
6859 $assignment->modifierid = !empty($oldmodifier->new_id) ? $oldmodifier->new_id : 0; // new modifier id here
6860 $assignment->roleid = $role->new_id; // restored new role id
6862 // hack to make the correct contextid for course level imports
6863 if ($contextlevel == CONTEXT_COURSE) {
6864 $oldinstance->new_id = $restore->course_id;
6865 } else {
6866 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
6869 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
6870 $assignment->contextid = $newcontext->id; // new context id
6871 // might already have same assignment
6872 role_assign($assignment->roleid, $assignment->userid, 0, $assignment->contextid, $assignment->timestart, $assignment->timeend, $assignment->hidden, $assignment->enrol);
6877 // auxillary function to write role assignments read from xml to db
6878 function restore_write_roleoverrides($restore, $overrides, $table, $contextlevel, $oldid, $oldroleid) {
6880 // it is possible to have an override not relevant to this course context.
6881 // should be ignored(?)
6882 if (!$role = backup_getid($restore->backup_unique_code, "role", $oldroleid)) {
6883 return null;
6886 foreach ($overrides as $override) {
6887 $override->capability = $override->name;
6888 $oldmodifier = backup_getid($restore->backup_unique_code,"user",$override->modifierid);
6889 $override->modifierid = $oldmodifier->new_id?$oldmodifier->new_id:0; // new modifier id here
6890 $override->roleid = $role->new_id; // restored new role id
6892 // hack to make the correct contextid for course level imports
6893 if ($contextlevel == CONTEXT_COURSE) {
6894 $oldinstance->new_id = $restore->course_id;
6895 } else {
6896 $oldinstance = backup_getid($restore->backup_unique_code,$table,$oldid);
6899 $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
6900 $override->contextid = $newcontext->id; // new context id
6901 // might already have same override
6902 if (!get_record('role_capabilities', 'capability', $override->capability, 'roleid', $override->roleid, 'contextid', $override->contextid)) {
6903 insert_record('role_capabilities', $override);
6907 //write activity date changes to the html log file, and update date values in the the xml array
6908 function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
6910 global $CFG;
6911 $openlog = false;
6913 // loop through time fields in $TAGS
6914 foreach ($TAGS as $TAG) {
6916 // check $TAG has a sensible value
6917 if (!empty($xml[$TAG][0]['#']) && is_string($xml[$TAG][0]['#']) && is_numeric($xml[$TAG][0]['#'])) {
6919 if ($openlog==false) {
6920 $openlog = true; // only come through here once
6922 // open file for writing
6923 $course_dir = "$CFG->dataroot/$restore->course_id/backupdata";
6924 check_dir_exists($course_dir, true);
6925 $restorelog = fopen("$course_dir/restorelog.html", "a");
6927 // start output for this record
6928 $msg = new stdClass();
6929 $msg->recordtype = $recordtype;
6930 $msg->recordname = $xml[$NAMETAG][0]['#'];
6931 fwrite ($restorelog, get_string("backupdaterecordtype", "moodle", $msg));
6934 // write old date to $restorelog
6935 $value = $xml[$TAG][0]['#'];
6936 $date = usergetdate($value);
6938 $msg = new stdClass();
6939 $msg->TAG = $TAG;
6940 $msg->weekday = $date['weekday'];
6941 $msg->mday = $date['mday'];
6942 $msg->month = $date['month'];
6943 $msg->year = $date['year'];
6944 fwrite ($restorelog, get_string("backupdateold", "moodle", $msg));
6946 // write new date to $restorelog
6947 $value += $restore->course_startdateoffset;
6948 $date = usergetdate($value);
6950 $msg = new stdClass();
6951 $msg->TAG = $TAG;
6952 $msg->weekday = $date['weekday'];
6953 $msg->mday = $date['mday'];
6954 $msg->month = $date['month'];
6955 $msg->year = $date['year'];
6956 fwrite ($restorelog, get_string("backupdatenew", "moodle", $msg));
6958 // update $value in $xml tree for calling module
6959 $xml[$TAG][0]['#'] = "$value";
6962 // close the restore log, if it was opened
6963 if ($openlog) {
6964 fclose($restorelog);