]> git.mjollnir.org Git - moodle.git/commitdiff
Scales are now supported in backup/restore.
authorstronk7 <stronk7>
Mon, 18 Aug 2003 00:00:22 +0000 (00:00 +0000)
committerstronk7 <stronk7>
Mon, 18 Aug 2003 00:00:22 +0000 (00:00 +0000)
Updated version to 0.8.8 alpha. Please test.

backup/STATUS.txt
backup/backup_execute.html
backup/backuplib.php
backup/restore_execute.html
backup/restorelib.php
backup/version.php

index 1786e5d392f55cb0aa47a2504e0756182c4a3e59..035126925d1e0d4be68822cb269b06dc59022075 100644 (file)
@@ -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
index 1bafb0bf54d7a4b99e8d3e3734f25064aa603f7d..ea2add4257049ded50f211bdc7192312d1f53760 100644 (file)
             }
         }
 
+        //Print scales info
+        if ($status) {
+            echo "<li>".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) {
index 8bdc34c1af8d114ec82b97db9c76cef3c700cd4e..f0cac69d42af688ae6a71bce5db29ac26281361f 100644 (file)
         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) {
       
index 94154a52760f1ec04ed0f32ad75d5697f38a04a6..b38ac872214248add63a4654d20a1a399add9520 100644 (file)
         }       
     }
 
+    //Now create scales as needed
+    if ($status) {
+        echo "<li>".get_string("creatingscales");
+        $status = restore_create_scales($restore,$xml_file);
+    }
+
     //Now create course modules as needed
     if ($status) {
         echo "<li>".get_string("creatingcoursemodules");
index 9006022d31ec1cf5f6ce1049829cc366c53c60a6..3351bf64aee282404e88b5b060af70e389086f95 100644 (file)
         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) {
         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) {
             }
         }
 
+        //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 "<P>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("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br>\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
             $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") {
                 if (($this->level == 4) and ($tagName == "QUESTION_CATEGORY")) {
                     //Prepend XML standard header to info gathered
                     $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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())."<p>";                                                          //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
+                    //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,
             $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("&nbsp;",($this->level+2)*2).$this->getContents()."<br>\n";           //Debug
+                //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br>\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))."</".$tagName.">";
+                //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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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())."<p>";                                                          //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
         } 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");
index d6178431490521a648d121661fe9942410bdd26c..9b9d27d16ae03827a6ba7ca3b5a0366f3c9be947 100644 (file)
@@ -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