From cee6968fa1dacfffae82ddf3816885ac14f916fb Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 7 Feb 2005 17:52:58 +0000 Subject: [PATCH] Now metacourses are working in restore too! Please test them!! --- backup/CHANGES_14_15.txt | 4 +- backup/restore_check.html | 5 +- backup/restore_execute.html | 13 ++- backup/restore_form.html | 19 ++++ backup/restorelib.php | 188 ++++++++++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 4 deletions(-) diff --git a/backup/CHANGES_14_15.txt b/backup/CHANGES_14_15.txt index 3bc121161a..714edc6f70 100644 --- a/backup/CHANGES_14_15.txt +++ b/backup/CHANGES_14_15.txt @@ -36,12 +36,12 @@ Now I show the specific detailed status of every item in the process: mimic it. 22. TODO: Review the wiki module fully (if possible, try to make binary uploaded contents to work). -23. PROG: Add support for metacourses in backup and restore. Show a new option +23. DONE: Add support for metacourses in backup and restore. Show a new option to decide what to do (ignore, process). 24. DONE: Take out THEME from backup directory. 25. TODO: Add support for forum_read table in backup and restore. 26. DONE: chat_users->course and chat_users->lang. No changes required! 27. PROG: Make the backup/restore/config/logs/index.php XHTML 1.0 Transitional. - Done in manual backups and logs + Done in manual backups, config and logs Eloy (stronk7) 23-01-2004 diff --git a/backup/restore_check.html b/backup/restore_check.html index 879a8d57ca..a0a926a98f 100644 --- a/backup/restore_check.html +++ b/backup/restore_check.html @@ -49,6 +49,8 @@ } //restoreto require_variable($restore_restoreto); + //restore_metacourse + require_variable($restore_metacourse); //restore_users require_variable($restore_users); //restore_logs @@ -79,6 +81,7 @@ } } $restore->restoreto=$restore_restoreto; + $restore->metacourse=$restore_metacourse; $restore->users=$restore_users; $restore->logs=$restore_logs; $restore->user_files=$restore_user_files; @@ -94,7 +97,7 @@ //We have the object with data, put it in the session $SESSION->restore = $restore; - //From here to the wnd of the script, only use the $restore object + //From here to the end of the script, only use the $restore object //Check login require_login(); diff --git a/backup/restore_execute.html b/backup/restore_execute.html index 0ba822003b..4031b00f2c 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -150,7 +150,6 @@ } } - //Now create users as needed if ($status and ($restore->users == 0 or $restore->users == 1)) { echo "
  • ".get_string("creatingusers")."
    "; @@ -206,6 +205,18 @@ } } + //Now create metacourse info + if ($status and $restore->metacourse) { + //Only to new courses! + if ($restore->restoreto == 2) { + echo "
  • ".get_string("creatingmetacoursedata"); + if (!$status = restore_create_metacourse($restore,$xml_file)) { + notify("Error creating metacourse in the course."); + } + } + } + + //Now create categories and questions as needed (STEP1) if ($status and ($restore->mods['quiz']->restore)) { echo "
  • ".get_string("creatingcategoriesandquestions")."
    "; diff --git a/backup/restore_form.html b/backup/restore_form.html index 14b4749e5d..625fd91c72 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -67,6 +67,10 @@ } //Check other parameters + if (!isset($restore_metacourse)) { + $restore_metacourse = 1; + } + if (!isset($restore_users)) { $restore_users = 1; } @@ -199,6 +203,21 @@ function selectItemInMenuByName(formId, menuName, selectIndex ) { //Line echo "
    "; + //Now print the Metacourse tr + echo ""; + echo "

    "; + echo get_string("metacourse").":"; + echo ""; + //If metacourse are in the backup file, show menu, else fixed to no + if ($info->backup_metacourse == "true") { + $metacourse_options[0] = get_string("no"); + $metacourse_options[1] = get_string("yes"); + choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, ""); + } else { + echo get_string("no"); + echo ""; + } + echo ""; //Now print the Users tr echo ""; echo "

    "; diff --git a/backup/restorelib.php b/backup/restorelib.php index 5a5a86da2d..00235d48b4 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -270,6 +270,15 @@ return $info; } + //This function read the xml file and store its data from the metacourse in a object + function restore_read_xml_metacourse ($xml_file) { + + //We call the main read_xml function, with todo = METACOURSE + $info = restore_read_xml ($xml_file,"METACOURSE",false); + + return $info; + } + //This function read the xml file and store its data from the users in //backup_ids->info db (and user's id in $info) function restore_read_xml_users ($restore,$xml_file) { @@ -387,6 +396,14 @@ } $elem++; } + //Metacourse info + $tab[$elem][0] = "".get_string("metacourse").":"; + if ($info->backup_metacourse == "true") { + $tab[$elem][1] = get_string("yes"); + } else { + $tab[$elem][1] = get_string("no"); + } + $elem++; //Users info $tab[$elem][0] = "".get_string("users").":"; $tab[$elem][1] = get_string($info->backup_users); @@ -552,6 +569,7 @@ $course->hiddensections = addslashes($course_header->course_hiddensections); $course->timecreated = addslashes($course_header->course_timecreated); $course->timemodified = addslashes($course_header->course_timemodified); + $course->metacourse = addslashes($course_header->course_metacourse); //Calculate sortorder field $sortmax = get_record_sql('SELECT MAX(sortorder) AS max FROM ' . $CFG->prefix . 'course @@ -568,6 +586,11 @@ $course->lang = 'mi_nt'; } + //Disable course->metacourse if avoided in restore config + if (!$restore->metacourse) { + $course->metacourse = 0; + } + //Now insert the record $newid = insert_record("course",$course); if ($newid) { @@ -826,6 +849,95 @@ } return $status; } + + //This function creates all the metacourse data from xml, notifying + //about each incidence + function restore_create_metacourse($restore,$xml_file) { + + global $CFG,$db; + + $status = true; + //Check it exists + if (!file_exists($xml_file)) { + $status = false; + } + //Get info from xml + if ($status) { + //Load data from XML to info + $info = restore_read_xml_metacourse($xml_file); + } + + //Process info about metacourse + if ($status and $info) { + //Process child records + if ($info->childs) { + foreach ($info->childs as $child) { + $dbcourse = false; + $dbmetacourse = false; + //Check if child course exists in destination server + //(by id in the same server or by idnumber and shortname in other server) + if (!$restore->original_wwwroot == $CFG->wwwroot) { + //Same server, lets see by id + $dbcourse = get_record('course','id',$child->id); + } else { + //Different server, lets see by idnumber and shortname, and only ONE record + $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname); + if ($dbcount == 1) { + $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname); + } + } + //If child course has been found, insert data + if ($dbcourse) { + $dbmetacourse->child_course = $dbcourse->id; + $dbmetacourse->parent_course = $restore->course_id; + $status = insert_record ('course_meta',$dbmetacourse); + } else { + //Child course not found, notice! + echo '

    '; + } + } + //Now, recreate student enrolments... + sync_metacourse($restore->course_id); + } + //Process parent records + if ($info->parents) { + foreach ($info->parents as $parent) { + $dbcourse = false; + $dbmetacourse = false; + //Check if parent course exists in destination server + //(by id in the same server or by idnumber and shortname in other server) + if (!$restore->original_wwwroot == $CFG->wwwroot) { + //Same server, lets see by id + $dbcourse = get_record('course','id',$parent->id); + } else { + //Different server, lets see by idnumber and shortname, and only ONE record + $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname); + if ($dbcount == 1) { + $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname); + } + } + //If parent course has been found, insert data if it is a metacourse + if ($dbcourse) { + if ($dbcourse->metacourse) { + $dbmetacourse->parent_course = $dbcourse->id; + $dbmetacourse->child_course = $restore->course_id; + $status = insert_record ('course_meta',$dbmetacourse); + //Now, recreate student enrolments in parent course + sync_metacourse($dbcourse->id); + } else { + //Parent course isn't metacourse, notice! + echo '
    • '.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')
    '; + } + } else { + //Parent course not found, notice! + echo '
    • '.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')
    '; + } + } + } + + } + return $status; + } //This function creates all the user, user_students, user_teachers //user_course_creators and user_admins from xml @@ -2075,6 +2187,22 @@ //if ($this->tree[3] == "SECTIONS") //Debug // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName.">
    \n"; //Debug } + + //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE") + function startElementMetacourse($parser, $tagName, $attrs) { + + //Refresh properties + $this->level++; + $this->tree[$this->level] = $tagName; + + //Output something to avoid browser timeouts... + backup_flush(); + + //Check if we are into METACOURSE zone + //if ($this->tree[3] == "METACOURSE") //Debug + // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName.">
    \n"; //Debug + } + //This is the startTag handler we use where we are reading the user zone (todo="USERS") function startElementUsers($parser, $tagName, $attrs) { @@ -2301,6 +2429,9 @@ if ($this->tree[3] == "DETAILS") { if ($this->level == 4) { switch ($tagName) { + case "METACOURSE": + $this->info->backup_metacourse = $this->getContents(); + break; case "USERS": $this->info->backup_users = $this->getContents(); break; @@ -2447,6 +2578,9 @@ case "TIMEMODIFIED": $this->info->course_timemodified = $this->getContents(); break; + case "METACOURSE": + $this->info->course_metacourse = $this->getContents(); + break; } } if ($this->tree[4] == "CATEGORY") { @@ -2632,6 +2766,57 @@ $this->finished = true; } } + + //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE") + function endElementMetacourse($parser, $tagName) { + //Check if we are into METACOURSE zone + if ($this->tree[3] == 'METACOURSE') { + //if (trim($this->content)) //Debug + // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."
    \n"; //Debug + //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName.">
    \n"; //Debug + //Dependig of different combinations, do different things + if ($this->level == 5) { + switch ($tagName) { + case 'CHILD': + //We've finalized a child, get it + $this->info->childs[] = $this->info->tempmeta; + unset($this->info->tempmeta); + break; + case 'PARENT': + //We've finalized a parent, get it + $this->info->parents[] = $this->info->tempmeta; + unset($this->info->tempmeta); + break; + default: + die($tagName); + } + } + if ($this->level == 6) { + switch ($tagName) { + case 'ID': + $this->info->tempmeta->id = $this->getContents(); + break; + case 'IDNUMBER': + $this->info->tempmeta->idnumber = $this->getContents(); + break; + case 'SHORTNAME': + $this->info->tempmeta->shortname = $this->getContents(); + break; + } + } + } + //Clear things + $this->tree[$this->level] = ''; + $this->level--; + $this->content = ""; + + //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course) + //Speed up a lot (avoid parse all) + if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') { + $this->finished = true; + } + } + //This is the endTag handler we use where we are reading the users zone (todo="USERS") function endElementUsers($parser, $tagName) { @@ -3218,6 +3403,9 @@ } else if ($todo == "SECTIONS") { //Define handlers to that zone xml_set_element_handler($xml_parser, "startElementSections", "endElementSections"); + } else if ($todo == "METACOURSE") { + //Define handlers to that zone + xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse"); } else if ($todo == "USERS") { //Define handlers to that zone xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers"); -- 2.39.5