From: stronk7 Date: Fri, 30 May 2003 21:32:04 +0000 (+0000) Subject: Begin restoring mods. Now they are readed and saved to db X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e3df3ece17d9b40906f948a59c67f014e4a310fd;p=moodle.git Begin restoring mods. Now they are readed and saved to db (backup_ids) in serialized form. --- diff --git a/backup/restore_execute.html b/backup/restore_execute.html index 45b9ea3542..9b11d106df 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -156,6 +156,16 @@ } } + //Now create course modules as needed + if ($status) { + echo "
  • Creating Course Modules"; + $status = restore_create_modules($restore,$xml_file); + //If all is ok (and we have a counter) + if ($status and ($status !== true)) { + //Inform about modules restores + } + } + diff --git a/backup/restorelib.php b/backup/restorelib.php index 5922b38857..c256a7b9b3 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -84,6 +84,16 @@ return $info; } + //This function read the xml file and store its data from the modules in + //backup_ids->info + function restore_read_xml_modules ($restore,$xml_file) { + + //We call the main read_xml function, with todo = MODULES + $info = restore_read_xml ($xml_file,"MODULES",$restore); + + return $info; + } + //This function prints the contents from the info parammeter passed function restore_print_info ($info) { @@ -421,7 +431,10 @@ //For each, user, take its info from backup_ids foreach ($info->users as $userid) { $rec = backup_getid($restore->backup_unique_code,"user",$userid); - $user = unserialize($rec->info); + //First strip slashes + $temp = stripslashes($rec->info); + //Now unserialize + $user = unserialize($temp); //Calculate if it is a course user //Has role teacher or student or admin or coursecreator $is_course_user = ($user->roles[teacher] or $user->roles[student] or @@ -631,6 +644,26 @@ } + //This function creates all the structures for every module in backup file + //Depending what has been selected. + function restore_create_modules($restore,$xml_file) { + + global $CFG; + + $status = true; + //Check it exists + if (!file_exists($xml_file)) { + $status = false; + } + //Get info from xml + if ($status) { + //info will contain the old_id of every module + //in backup_ids->info will be the real info (serialized) + $info = restore_read_xml_modules($restore,$xml_file); + } + + } + //===================================================================================== //== == //== XML Functions (SAX) == @@ -697,6 +730,20 @@ // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName.">
    \n"; //Debug } + //This is the startTag handler we use where we are reading the modules zone (todo="MODULES") + function startElementModules($parser, $tagName, $attrs) { + //Refresh properties + $this->level++; + $this->tree[$this->level] = $tagName; + //Check if we are into MODULES zone + //if ($this->tree[3] == "MODULES") //Debug + // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName.">
    \n"; //Debug + //If we are under a MOD tag under a MODULES zone, accumule it + if (($this->tree[4] == "MOD") and ($this->tree[3] == "MODULES")) { + $this->info .= "<".$tagName.">"; + } + } + //This is the startTag default handler we use when todo is undefined function startElement($parser, $tagName, $attrs) { $this->level++; @@ -976,8 +1023,13 @@ switch ($tagName) { case "USER": //We've finalized a user, get it and save to db + //First serialize + $ser_temp = serialize($this->info->tempuser); + //Now add slashes + $sla_temp = addslashes($ser_temp); + //Save to db backup_putid($this->preferences->backup_unique_code,"user",$this->info->tempuser->id, - null,serialize($this->info->tempuser)); + null,$sla_temp); //Delete temp obejct unset($this->info->tempuser); break; @@ -1141,6 +1193,57 @@ $this->content = ""; } + //This is the endTag handler we use where we are reading the modules zone (todo="MODULES") + function endElementModules($parser, $tagName) { + //Check if we are into MODULES zone + if ($this->tree[3] == "MODULES") { + //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 + //Acumulate data to info (content + close tag) + //Reconvert it to utf & htmlchars and trim to generate xml data + $this->info .= utf8_encode(htmlspecialchars(trim($this->content))).""; + //If we've finished a mod, xmlize it an save to db + if (($this->level == 4) and ($tagName == "MOD")) { + //Prepend XML standard header to info gathered + $xml_data = "\n".$this->info; + //Call to xmlize for this portion of xml data (one MOD) + $data = xmlize($xml_data); + //traverse_xmlize($data); //Debug + //print_object ($GLOBALS['traverse_array']); //Debug + //$GLOBALS['traverse_array']=""; //Debug + //Now, save data to db. We'll use it later + //Get id and modtype from data + $mod_id = $data["MOD"]["#"]["ID"]["0"]["#"]; + $mod_type = $data["MOD"]["#"]["MODTYPE"]["0"]["#"]; + //Serialize it + $mod_temp = serialize($data); + //Now add slashes + $sla_mod_temp = addslashes($mod_temp); + //Save to db + backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id, + null,$sla_mod_temp); + //Reset info to empty + $this->info = ""; + } + + + + + } + + //Stop parsing if todo = MODULES and tagName = MODULES (en of the tag, of course) + //Speed up a lot (avoid parse all) + if ($tagName == "MODULES" and $this->level == 3) { + $this->finished = true; + } + + //Clear things + $this->tree[$this->level] = ""; + $this->level--; + $this->content = ""; + } + //This is the endTag default handler we use when todo is undefined function endElement($parser, $tagName) { if (trim($this->content)) //Debug @@ -1182,6 +1285,9 @@ } else if ($todo == "USERS") { //Define handlers to that zone xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers"); + } else if ($todo == "MODULES") { + //Define handlers to that zone + xml_set_element_handler($xml_parser, "startElementModules", "endElementModules"); } else { //Define default handlers (must no be invoked when everything become finished) xml_set_element_handler($xml_parser, "startElementInfo", "endElementInfo");