From 42f1ff47b441a43a7ef336ad6e03d27f1505f62d Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 18 Aug 2003 00:00:22 +0000 Subject: [PATCH] Scales are now supported in backup/restore. Updated version to 0.8.8 alpha. Please test. --- backup/STATUS.txt | 6 +- backup/backup_execute.html | 6 ++ backup/backuplib.php | 42 +++++++++ backup/restore_execute.html | 6 ++ backup/restorelib.php | 178 +++++++++++++++++++++++++++++++++++- backup/version.php | 4 +- 6 files changed, 234 insertions(+), 8 deletions(-) diff --git a/backup/STATUS.txt b/backup/STATUS.txt index 1786e5d392..035126925d 100644 --- a/backup/STATUS.txt +++ b/backup/STATUS.txt @@ -3,7 +3,7 @@ STATUS $Id$ This documment shows the current status of the backup/restore option. -Backup: WORKING. COMPLETED except workshops and scales. +Backup: WORKING. COMPLETED except workshops. Restore: WORKING. COMPLETED except workshops, logs and scales. @@ -48,7 +48,7 @@ Backup Details: - Course.......................................DONE - All..........................................DONE - Logs...............................................DONE - - Scales.............................................IN PROGRESS + - Scales.............................................DONE - Categories and Questions.(STEP 1)..................DONE + categories......................................DONE + questions structure.............................DONE @@ -155,6 +155,7 @@ Restore Details: - Categories and Questions.(STEP 1)..................DONE + categories......................................DONE + questions structure.............................DONE + - Scales.............................................DONE - Mods Info Prepare..................................DONE x read modules zone...............................DONE x separate every mod..............................DONE @@ -200,7 +201,6 @@ Restore Details: - User files from temp.......................................DONE - Course files from temp.....................................DONE - Log entries................................................NO EXISTS - - Scales.....................................................IN PROGRESS - Readjust instance in course_modules........................DONE - Readjust modinfo in course (serialize).....................DONE - Delete old entries at restore end. .......................DONE diff --git a/backup/backup_execute.html b/backup/backup_execute.html index 1bafb0bf54..ea2add4257 100644 --- a/backup/backup_execute.html +++ b/backup/backup_execute.html @@ -192,6 +192,12 @@ } } + //Print scales info + if ($status) { + echo "
  • ".get_string("writingscalesinfo"); + $status = backup_scales_info($backup_file,$preferences); + } + //Module info, this unique function makes all the work!! //db export and module fileis copy if ($status) { diff --git a/backup/backuplib.php b/backup/backuplib.php index 8bdc34c1af..f0cac69d42 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -757,6 +757,48 @@ return $status; } + + //Backup scales info (common and course scales) + function backup_scales_info($bf,$preferences) { + + global $CFG; + + $status = true; + + //Counter, points to current record + $counter = 0; + + //Get scales (common and course scales) + $scales = get_records_sql("SELECT id, courseid, userid, name, scale, description, timemodified + FROM {$CFG->prefix}scale + WHERE courseid = '0' or courseid = $preferences->backup_course"); + + //Pring scales header + if ($scales) { + //Pring scales header + fwrite ($bf,start_tag("SCALES",2,true)); + //Iterate + foreach ($scales as $scale) { + //Begin scale tag + fwrite ($bf,start_tag("SCALE",3,true)); + //Output scale tag + fwrite ($bf,full_tag("ID",4,false,$scale->id)); + fwrite ($bf,full_tag("COURSEID",4,false,$scale->courseid)); + fwrite ($bf,full_tag("USERID",4,false,$scale->userid)); + fwrite ($bf,full_tag("NAME",4,false,$scale->name)); + fwrite ($bf,full_tag("SCALETEXT",4,false,$scale->scale)); + fwrite ($bf,full_tag("DESCRIPTION",4,false,$scale->description)); + fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scale->timemodified)); + //End scale tag + fwrite ($bf,end_tag("SCALE",3,true)); + } + //End scales tag + $status = fwrite ($bf,end_tag("SCALES",2,true)); + } + return $status; + } + + //Start the modules tag function backup_modules_start ($bf,$preferences) { diff --git a/backup/restore_execute.html b/backup/restore_execute.html index 94154a5276..b38ac87221 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -216,6 +216,12 @@ } } + //Now create scales as needed + if ($status) { + echo "
  • ".get_string("creatingscales"); + $status = restore_create_scales($restore,$xml_file); + } + //Now create course modules as needed if ($status) { echo "
  • ".get_string("creatingcoursemodules"); diff --git a/backup/restorelib.php b/backup/restorelib.php index 9006022d31..3351bf64ae 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -94,6 +94,16 @@ return $info; } + //This function read the xml file and store its data from the scales in + //backup_ids->info db (and scale's id in $info) + function restore_read_xml_scales ($restore,$xml_file) { + + //We call the main read_xml function, with todo = SCALES + $info = restore_read_xml ($xml_file,"SCALES",$restore); + + 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) { @@ -621,6 +631,95 @@ return $status; } + //This function creates all the scales + function restore_create_scales($restore,$xml_file) { + + global $CFG, $db; + + $status = true; + //Check it exists + if (!file_exists($xml_file)) { + $status = false; + } + //Get info from xml + if ($status) { + //scales will contain the old_id of every scale + //in backup_ids->info will be the real info (serialized) + $scales = restore_read_xml_scales($restore,$xml_file); + } + //Now, if we have anything in scales, we have to restore that + //scales + if ($scales) { + if ($scales !== true) { + //Iterate over each scale + foreach ($scales as $scale) { + //Get record from backup_ids + $data = backup_getid($restore->backup_unique_code,"scale",$scale->id); + //Init variables + $create_scale = false; + + if ($data) { + //Now get completed xmlized object + $info = $data->info; + //traverse_xmlize($info); //Debug + //print_object ($GLOBALS['traverse_array']); //Debug + //$GLOBALS['traverse_array']=""; //Debug + + //Now build the SCALE record structure + $sca->id = backup_todb($info['SCALE']['#']['ID']['0']['#']); + $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']); + $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']); + $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']); + $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']); + $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']); + $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']); + + //Now search if that scale exists (by scale field) + $sca_db = get_record("scale","scale",$sca->scale); + //If it doesn't exist, create + if (!$sca_db) { + $create_scale = true; + } else { + //Exists. If the courseid in db is <> 0 and <> current course, + //we must create the scale too + if ($sca_db->courseid != 0 and $sca_db->courseid != $restore->course_id) { + $create_scale = true; + } + } + //If we must create the scale + if ($create_scale) { + //Me must recode the courseid if it's <> 0 (common scale) + if ($sca->courseid != 0) { + $sca->courseid = $restore->course_id; + } + //We must recode the userid + $user = backup_getid($restore->backup_unique_code,"user",$sca->userid); + if ($user) { + $sca->userid = $user->new_id; + } else { + //Assign it to admin + $sca->userid = 1; + } + //The structure is equal to the db, so insert the scale + $newid = insert_record ("scale",$sca); + } else { + //get current scale id + $newid = $sca_db->id; + } + if ($newid) { + //We have the newid, update backup_ids + backup_putid($restore->backup_unique_code,"scale", + $scale->id, $newid); + } + } + } + } + } else { + $status = false; + } + return $status; + } + //This function restores the userfiles from the temp (user_files) directory to the //dataroot/users directory function restore_user_files($restore) { @@ -918,6 +1017,29 @@ } } + //This is the startTag handler we use where we are reading the scales zone (todo="SCALES") + function startElementScales($parser, $tagName, $attrs) { + //Refresh properties + $this->level++; + $this->tree[$this->level] = $tagName; + + //if ($tagName == "SCALE" && $this->tree[3] == "SCALES") { //Debug + // echo "

    SCALE: ".strftime ("%X",time()),"-"; //Debug + //} //Debug + + //Output something to avoid browser timeouts... + backup_flush(); + + //Check if we are into SCALES zone + //if ($this->tree[3] == "SCALES") //Debug + // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName.">
    \n"; //Debug + + //If we are under a SCALE tag under a SCALES zone, accumule it + if (($this->tree[4] == "SCALE") and ($this->tree[3] == "SCALES")) { + $this->temp .= "<".$tagName.">"; + } + } + //This is the startTag handler we use where we are reading the modules zone (todo="MODULES") function startElementModules($parser, $tagName, $attrs) { //Refresh properties @@ -1415,7 +1537,7 @@ $this->content = ""; } - //This is the endTag handler we use where we are reading the modules zone (todo="QUESTIONS") + //This is the endTag handler we use where we are reading the questions zone (todo="QUESTIONS") function endElementQuestions($parser, $tagName) { //Check if we are into QUESTION_CATEGORIES zone if ($this->tree[3] == "QUESTION_CATEGORIES") { @@ -1429,7 +1551,7 @@ if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) { //Prepend XML standard header to info gathered $xml_data = "\n".$this->temp; - //Call to xmlize for this portion of xml data (one MOD) + //Call to xmlize for this portion of xml data (one QUESTION_CATEGORY) //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug $data = xmlize($xml_data,0); //echo strftime ("%X",time())."

    "; //Debug @@ -1437,7 +1559,7 @@ //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 + //Get id from data $category_id = $data["QUESTION_CATEGORY"]["#"]["ID"]["0"]["#"]; //Save to db $status = backup_putid($this->preferences->backup_unique_code,"quiz_categories",$category_id, @@ -1462,6 +1584,53 @@ $this->content = ""; } + //This is the endTag handler we use where we are reading the scales zone (todo="SCALES") + function endElementScales($parser, $tagName) { + //Check if we are into SCALES zone + if ($this->tree[3] == "SCALES") { + //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: strip htmlchars again and trim to generate xml data + $this->temp .= htmlspecialchars(trim($this->content)).""; + //If we've finished a scale, xmlize it an save to db + if (($this->level == 4) and ($tagName == "SCALE")) { + //Prepend XML standard header to info gathered + $xml_data = "\n".$this->temp; + //Call to xmlize for this portion of xml data (one SCALE) + //echo "-XMLIZE: ".strftime ("%X",time()),"-"; //Debug + $data = xmlize($xml_data,0); + //echo strftime ("%X",time())."

    "; //Debug + //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 from data + $scale_id = $data["SCALE"]["#"]["ID"]["0"]["#"]; + //Save to db + $status = backup_putid($this->preferences->backup_unique_code,"scale",$scale_id, + null,$data); + //Create returning info + $ret_info->id = $scale_id; + $this->info[] = $ret_info; + //Reset temp + unset($this->temp); + } + } + + //Stop parsing if todo = SCALES and tagName = SCALE (en of the tag, of course) + //Speed up a lot (avoid parse all) + if ($tagName == "SCALES" and $this->level == 3) { + $this->finished = true; + } + + //Clear things + $this->tree[$this->level] = ""; + $this->level--; + $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 @@ -1559,6 +1728,9 @@ } else if ($todo == "QUESTIONS") { //Define handlers to that zone xml_set_element_handler($xml_parser, "startElementQuestions", "endElementQuestions"); + } else if ($todo == "SCALES") { + //Define handlers to that zone + xml_set_element_handler($xml_parser, "startElementScales", "endElementScales"); } else if ($todo == "MODULES") { //Define handlers to that zone xml_set_element_handler($xml_parser, "startElementModules", "endElementModules"); diff --git a/backup/version.php b/backup/version.php index d617843149..9b9d27d16a 100644 --- a/backup/version.php +++ b/backup/version.php @@ -5,6 +5,6 @@ // database (backup_version) to determine whether upgrades should // be performed (see db/backup_*.php) -$backup_version = 2003081500; // The current version is a date (YYYYMMDDXX) +$backup_version = 2003081800; // The current version is a date (YYYYMMDDXX) -$backup_release = "0.8.7 alpha"; // User-friendly version number +$backup_release = "0.8.8 alpha"; // User-friendly version number -- 2.39.5