From dfa14bc98fe676ff399c35397acadb58ddd6b2bd Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 25 Jan 2004 20:57:58 +0000 Subject: [PATCH] Now groups and members are restored and recoded when restoring courses. Existing courses are searched by name+description to add members to existing groups (when restoring over another course). --- backup/STATUS.txt | 4 +- backup/restore_execute.html | 8 ++ backup/restorelib.php | 218 ++++++++++++++++++++++++++++++++++++ backup/version.php | 2 +- 4 files changed, 230 insertions(+), 2 deletions(-) diff --git a/backup/STATUS.txt b/backup/STATUS.txt index fc733699de..df888f9c34 100644 --- a/backup/STATUS.txt +++ b/backup/STATUS.txt @@ -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 diff --git a/backup/restore_execute.html b/backup/restore_execute.html index f1ba6ebeeb..500e33ad9b 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -227,6 +227,14 @@ } } + //Now create groups as needed + if ($status) { + echo "
  • ".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 "
  • ".get_string("creatingcoursemodules"); diff --git a/backup/restorelib.php b/backup/restorelib.php index affa785e57..52cd6746e7 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -109,6 +109,16 @@ 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) { @@ -762,6 +772,130 @@ 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 "
    "; + } + 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) { @@ -770,6 +904,8 @@ $status = true; + $counter = 0; + //First, we check to "users" exists and create is as necessary //in CFG->dataroot $dest_dir = $CFG->dataroot."/users"; @@ -828,6 +964,8 @@ global $CFG; $status = true; + + $counter = 0; //First, we check to "course_id" exists and create is as necessary //in CFG->dataroot @@ -1092,6 +1230,33 @@ } } + function startElementGroups($parser, $tagName, $attrs) { + //Refresh properties + $this->level++; + $this->tree[$this->level] = $tagName; + + //if ($tagName == "GROUP" && $this->tree[3] == "GROUPS") { //Debug + // echo "

    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(" ",$this->level*2)."<".$tagName.">
    \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 @@ -1706,6 +1871,56 @@ $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(" ",($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 + if (!isset($this->temp)) { + $this->temp = ""; + } + $this->temp .= htmlspecialchars(trim($this->content)).""; + //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 = "\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())."

    "; //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 @@ -1809,6 +2024,9 @@ } 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"); diff --git a/backup/version.php b/backup/version.php index 1f4b87339b..f524ad0fc4 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 = 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 -- 2.39.5