]> git.mjollnir.org Git - moodle.git/commitdiff
Now groups and members are restored and recoded when restoring courses.
authorstronk7 <stronk7>
Sun, 25 Jan 2004 20:57:58 +0000 (20:57 +0000)
committerstronk7 <stronk7>
Sun, 25 Jan 2004 20:57:58 +0000 (20:57 +0000)
Existing courses are searched by name+description to add members to
existing groups (when restoring over another course).

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

index fc733699de5390831f663e90a7448ab08d4454ab..df888f9c34ad9a3a4acfac29a7a30ef78aca97f1 100644 (file)
@@ -5,7 +5,7 @@ This documment shows the current status of the backup/restore option.
 
 Backup:  WORKING. COMPLETED !!
 
-Restore: WORKING. COMPLETED except logs.
+Restore: WORKING. COMPLETED except logs restore.
                   
 
 ========== ========== ========== ========== ========== ==========
@@ -49,6 +49,7 @@ Backup Details:
                  - All..........................................DONE 
            - Logs...............................................DONE
            - Scales.............................................DONE
+           - Groups.............................................DONE
            - Categories and Questions.(STEP 1)..................DONE
               + categories......................................DONE
               + questions structure.............................DONE
@@ -177,6 +178,7 @@ Restore Details:
               + categories......................................DONE
               + questions structure.............................DONE
            - Scales.............................................DONE
+           - Groups.............................................DONE
            - Mods Info Prepare..................................DONE
               x read modules zone...............................DONE
               x separate every mod..............................DONE
index f1ba6ebeeb83ef6200520290de64b054926bff3c..500e33ad9bd57d2f5345d4028612a4b52dbae171 100644 (file)
         }
     }
 
+    //Now create groups as needed
+    if ($status) {
+        echo "<li>".get_string("creatinggroups");
+        if (!$status = restore_create_groups($restore,$xml_file)) {
+            notify("Could not restore groups!");
+        }
+    }
+
     //Now create course modules as needed
     if ($status) {
         echo "<li>".get_string("creatingcoursemodules");
index affa785e57e6a502f09debbd67d9a6a1ecc6e11c..52cd6746e743538adea0d6516a07a53fa2bf0908 100644 (file)
         return $info;
     }
 
+    //This function read the xml file and store its data from the groups in
+    //backup_ids->info db (and group's id in $info)
+    function restore_read_xml_groups ($restore,$xml_file) {
+
+        //We call the main read_xml function, with todo = GROUPS
+        $info = restore_read_xml ($xml_file,"GROUPS",$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 groups
+    function restore_create_groups($restore,$xml_file) {
+
+        global $CFG, $db;
+
+        $status = true;
+        //Check it exists
+        if (!file_exists($xml_file)) {
+            $status = false;
+        }
+        //Get info from xml
+        if ($status) {
+            //groups will contain the old_id of every scale
+            //in backup_ids->info will be the real info (serialized)
+            $groups = restore_read_xml_groups($restore,$xml_file);
+        }
+        //Now, if we have anything in groups, we have to restore that
+        //groups
+        if ($groups) {
+            if ($groups !== true) {
+                //Iterate over each group
+                foreach ($groups as $group) {
+                    //Get record from backup_ids
+                    $data = backup_getid($restore->backup_unique_code,"group",$group->id);
+                    //Init variables
+                    $create_group = 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 GROUP record structure
+                        $gro->id = backup_todb($info['GROUP']['#']['ID']['0']['#']);
+                        $gro->courseid = backup_todb($info['GROUP']['#']['COURSEID']['0']['#']);
+                        $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']);
+                        $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']);
+                        $gro->lang = backup_todb($info['GROUP']['#']['LANG']['0']['#']);
+                        $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']);
+                        $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']);
+                        $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']);
+                
+                        //Now search if that group exists (by name and description field) in 
+                        //restore->course_id course 
+                        $gro_db = get_record("groups","courseid",$restore->course_id,"name",$gro->name,"description",$gro->description);
+                        //If it doesn't exist, create
+                        if (!$gro_db) {
+                            $create_group = true;
+                        }
+                        //If we must create the group
+                        if ($create_group) {
+                            //Me must recode the courseid to the restore->course_id 
+                            $gro->courseid = $restore->course_id;
+                            //The structure is equal to the db, so insert the group
+                            $newid = insert_record ("groups",$gro);
+                        } else { 
+                            //get current group id
+                            $newid = $group_db->id;
+                        }
+                        if ($newid) {
+                            //We have the newid, update backup_ids
+                            backup_putid($restore->backup_unique_code,"group",
+                                         $group->id, $newid);
+                        }
+                        //Now restore members in the groups_members
+                        $status2 = restore_create_groups_members($newid,$info,$restore);
+                    }   
+                }
+            }
+        } else {
+            $status = false;
+        } 
+        return ($status && $status2);
+    }
+
+    //This function restores the groups_members
+    function restore_create_groups_members($group_id,$info,$restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        //Get the members array
+        $members = $info['GROUP']['#']['MEMBERS']['0']['#']['MEMBER'];
+
+        //Iterate over members
+        for($i = 0; $i < sizeof($members); $i++) {
+            $mem_info = $members[$i];
+            traverse_xmlize($mem_info);                                                                 //Debug
+            print_object ($GLOBALS['traverse_array']);                                                  //Debug
+            $GLOBALS['traverse_array']="";                                                              //Debug
+
+            //Now, build the GROUPS_MEMBERS record structure
+            $group_member->groupid = $group_id;
+            $group_member->userid = backup_todb($mem_info['#']['USERID']['0']['#']);
+            $group_member->timeadded = backup_todb($mem_info['#']['TIMEADDED']['0']['#']);
+
+            //We have to recode the userid field
+            $user = backup_getid($restore->backup_unique_code,"user",$group_member->userid);
+            if ($user) {
+                $group_member->userid = $user->new_id;
+            }
+
+            //The structure is equal to the db, so insert the groups_members
+            $newid = insert_record ("groups_members",$group_member);
+
+            //Do some output
+            if (($i+1) % 50 == 0) {
+                echo ".";
+                if (($i+1) % 1000 == 0) {
+                    echo "<br>";
+                }
+                backup_flush(300);
+            }
+            
+            if (!$newid) {
+                $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) {
 
         $status = true;
 
+        $counter = 0;
+
         //First, we check to "users" exists and create is as necessary
         //in CFG->dataroot
         $dest_dir = $CFG->dataroot."/users";
         global $CFG;
 
         $status = true;
+        $counter = 0;
 
         //First, we check to "course_id" exists and create is as necessary
         //in CFG->dataroot
             }
         }
 
+        function startElementGroups($parser, $tagName, $attrs) {
+            //Refresh properties
+            $this->level++;
+            $this->tree[$this->level] = $tagName;
+
+            //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") {                                 //Debug
+            //    echo "<P>GROUP: ".strftime ("%X",time()),"-";                                        //Debug
+            //}                                                                                        //Debug
+
+            //Output something to avoid browser timeouts...
+            backup_flush();
+
+            //Check if we are into GROUPS zone
+            //if ($this->tree[3] == "GROUPS")                                                           //Debug
+            //    echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br>\n";   //Debug
+
+            //If we are under a GROUP tag under a GROUPS zone, accumule it
+            if (isset($this->tree[4]) and isset($this->tree[3])) {
+                if (($this->tree[4] == "GROUP") and ($this->tree[3] == "GROUPS")) {
+                    if (!isset($this->temp)) {
+                        $this->temp = "";
+                    }
+                    $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 groups zone (todo="GROUPS")
+        function endElementGroups($parser, $tagName) {
+            //Check if we are into GROUPS zone
+            if ($this->tree[3] == "GROUPS") {
+                //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
+                if (!isset($this->temp)) {
+                    $this->temp = "";
+                }
+                $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
+                //If we've finished a group, xmlize it an save to db
+                if (($this->level == 4) and ($tagName == "GROUP")) {
+                    //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 GROUP)
+                    //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
+                    $group_id = $data["GROUP"]["#"]["ID"]["0"]["#"];
+                    //Save to db
+                    $status = backup_putid($this->preferences->backup_unique_code,"group",$group_id,
+                                     null,$data);
+                    //Create returning info
+                    $ret_info->id = $group_id;
+                    $this->info[] = $ret_info;
+                    //Reset temp
+                    unset($this->temp);
+                }
+            }
+
+            //Stop parsing if todo = GROUPS and tagName = GROUP (en of the tag, of course)
+            //Speed up a lot (avoid parse all)
+            if ($tagName == "GROUPS" 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 == "SCALES") {
             //Define handlers to that zone
             xml_set_element_handler($xml_parser, "startElementScales", "endElementScales");
+        } else if ($todo == "GROUPS") {
+            //Define handlers to that zone
+            xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups");
         } else if ($todo == "MODULES") {
             //Define handlers to that zone
             xml_set_element_handler($xml_parser, "startElementModules", "endElementModules");
index 1f4b87339b732690f66acdbf12a618dfc54601a5..f524ad0fc497aa717f3349689f73f9aa7a07eae2 100644 (file)
@@ -5,6 +5,6 @@
 // database (backup_version) to determine whether upgrades should
 // be performed (see db/backup_*.php)
 
-$backup_version = 2004012200;   // The current version is a date (YYYYMMDDXX)
+$backup_version = 2004012500;   // The current version is a date (YYYYMMDDXX)
 
 $backup_release = "1.2 development";  // User-friendly version number