From cd44fdcb8222cf98f309d7fa004fcc00fcb43c6a Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sat, 21 Feb 2004 01:30:22 +0000 Subject: [PATCH] Some important changes: - Now course events are supported in backup & restore. - in manual backups - in scheduled backups - Fixed important bug when restoring groups. - Added support to the "user change password" action (with some changes done in login/change_password.php) Please, test it !!!! --- backup/STATUS.txt | 4 +- backup/backup_execute.html | 8 ++ backup/backup_scheduled.php | 8 +- backup/backuplib.php | 40 ++++++++ backup/restore_execute.html | 8 ++ backup/restorelib.php | 190 +++++++++++++++++++++++++++++++++++- backup/version.php | 2 +- 7 files changed, 256 insertions(+), 4 deletions(-) diff --git a/backup/STATUS.txt b/backup/STATUS.txt index 8fdb61f376..2a77101a3d 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 events and some (forums and lessons) +Restore: WORKING. COMPLETED except some (forums and lessons) logs restore (the rest should be working). ========== ========== ========== ========== ========== ========== @@ -49,6 +49,7 @@ Backup Details: - All..........................................DONE - Logs...............................................DONE - Scales.............................................DONE + - Events.(course)....................................DONE - Groups.............................................DONE - Categories and Questions.(STEP 1)..................DONE + categories......................................DONE @@ -178,6 +179,7 @@ Restore Details: + categories......................................DONE + questions structure.............................DONE - Scales.............................................DONE + - Events.(course)....................................DONE - Groups.............................................DONE - Mods Info Prepare..................................DONE x read modules zone...............................DONE diff --git a/backup/backup_execute.html b/backup/backup_execute.html index c328076cc1..59d8ab14a9 100644 --- a/backup/backup_execute.html +++ b/backup/backup_execute.html @@ -220,6 +220,14 @@ } } + //Print events info + if ($status) { + echo "
  • ".get_string("writingeventsinfo"); + if (!$status = backup_events_info($backup_file,$preferences)) { + notify("An error occurred while backing up events"); + } + } + //Module info, this unique function makes all the work!! //db export and module fileis copy if ($status) { diff --git a/backup/backup_scheduled.php b/backup/backup_scheduled.php index f53be0a55c..676d55660f 100644 --- a/backup/backup_scheduled.php +++ b/backup/backup_scheduled.php @@ -527,13 +527,19 @@ function schedule_backup_course_execute($preferences,$starttime = 0) { schedule_backup_log($starttime,$preferences->backup_course," scales"); $status = backup_scales_info($backup_file,$preferences); } - + //Print groups info if ($status) { schedule_backup_log($starttime,$preferences->backup_course," groups"); $status = backup_groups_info($backup_file,$preferences); } + //Print events info + if ($status) { + schedule_backup_log($starttime,$preferences->backup_course," events"); + $status = backup_events_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 4bfa38d3ad..33f072615a 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -961,6 +961,46 @@ return $status; } + //Backup events info (course events) + function backup_events_info($bf,$preferences) { + + global $CFG; + + $status = true; + + //Counter, points to current record + $counter = 0; + + //Get events (course events) + $events = get_records_select("event","courseid='$preferences->backup_course' AND instance='0'","id"); + + //Pring events header + if ($events) { + //Pring events header + fwrite ($bf,start_tag("EVENTS",2,true)); + //Iterate + foreach ($events as $event) { + //Begin event tag + fwrite ($bf,start_tag("EVENT",3,true)); + //Output event tag + fwrite ($bf,full_tag("ID",4,false,$event->id)); + fwrite ($bf,full_tag("NAME",4,false,$event->name)); + fwrite ($bf,full_tag("DESCRIPTION",4,false,$event->description)); + fwrite ($bf,full_tag("GROUPID",4,false,$event->groupid)); + fwrite ($bf,full_tag("USERID",4,false,$event->userid)); + fwrite ($bf,full_tag("EVENTTYPE",4,false,$event->eventtype)); + fwrite ($bf,full_tag("TIMESTART",4,false,$event->timestart)); + fwrite ($bf,full_tag("TIMEDURATION",4,false,$event->timeduration)); + fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$event->timemodified)); + //End event tag + fwrite ($bf,end_tag("EVENT",3,true)); + } + //End events tag + $status = fwrite ($bf,end_tag("EVENTS",2,true)); + } + return $status; + } + //Backup groups info function backup_groups_info($bf,$preferences) { diff --git a/backup/restore_execute.html b/backup/restore_execute.html index 50021a3fc6..e41324de6a 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -235,6 +235,14 @@ } } + //Now create events as needed + if ($status) { + echo "
  • ".get_string("creatingevents"); + if (!$status = restore_create_events($restore,$xml_file)) { + notify("Could not restore course events!"); + } + } + //Now create course modules as needed if ($status) { echo "
  • ".get_string("creatingcoursemodules"); diff --git a/backup/restorelib.php b/backup/restorelib.php index cc4e88540a..e1d6d5effa 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -119,6 +119,16 @@ return $info; } + //This function read the xml file and store its data from the events (course) in + //backup_ids->info db (and event's id in $info) + function restore_read_xml_events ($restore,$xml_file) { + + //We call the main read_xml function, with todo = EVENTS + $info = restore_read_xml ($xml_file,"EVENTS",$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) { @@ -841,7 +851,6 @@ //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']['#']); @@ -932,6 +941,95 @@ return $status; } + //This function creates all the course events + function restore_create_events($restore,$xml_file) { + + global $CFG, $db; + + $status = true; + //Check it exists + if (!file_exists($xml_file)) { + $status = false; + } + //Get info from xml + if ($status) { + //events will contain the old_id of every event + //in backup_ids->info will be the real info (serialized) + $events = restore_read_xml_events($restore,$xml_file); + } + //Now, if we have anything in events, we have to restore that + //events + if ($events) { + if ($events !== true) { + //Iterate over each event + foreach ($events as $event) { + //Get record from backup_ids + $data = backup_getid($restore->backup_unique_code,"event",$event->id); + //Init variables + $create_event = 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 EVENT record structure + $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']); + $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']); + $eve->courseid = $restore->course_id; + $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']); + $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']); + $eve->modulename = ""; + $eve->instance = 0; + $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']); + $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']); + $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']); + $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']); + + + //Now search if that event exists (by description and timestart field) in + //restore->course_id course + $eve_db = get_record("event","courseid",$restore->course_id,"description",$eve->description,"timestart",$eve->timestart); + //If it doesn't exist, create + if (!$eve_db) { + $create_event = true; + } + //If we must create the event + if ($create_event) { + + //We must recode the userid + $user = backup_getid($restore->backup_unique_code,"user",$eve->userid); + if ($user) { + $eve->userid = $user->new_id; + } else { + //Assign it to admin + $eve->userid = 1; + } + //We have to recode the groupid field + $group = backup_getid($restore->backup_unique_code,"group",$eve->groupid); + if ($group) { + $eve->groupid = $group->new_id; + } + + //The structure is equal to the db, so insert the event + $newid = insert_record ("event",$eve); + } + if ($newid) { + //We have the newid, update backup_ids + backup_putid($restore->backup_unique_code,"event", + $event->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) { @@ -1341,6 +1439,15 @@ $toinsert = true; } break; + case "change password": + //recode the info field (it's the user id) + $user = backup_getid($restore->backup_unique_code,"user",$log->info); + if ($user) { + $log->info = $user->new_id; + $log->url = "view.php?id=".$log->info."&course=".$log->course; + $toinsert = true; + } + break; case "view all": $log->url = "view.php?id=".$log->course; $log->info = ""; @@ -1613,6 +1720,34 @@ } } + //This is the startTag handler we use where we are reading the events zone (todo="EVENTS") + function startElementEvents($parser, $tagName, $attrs) { + //Refresh properties + $this->level++; + $this->tree[$this->level] = $tagName; + + //if ($tagName == "EVENT" && $this->tree[3] == "EVENTS") { //Debug + // echo "

    EVENT: ".strftime ("%X",time()),"-"; //Debug + //} //Debug + + //Output something to avoid browser timeouts... + backup_flush(); + + //Check if we are into EVENTS zone + //if ($this->tree[3] == "EVENTS") //Debug + // echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName.">
    \n"; //Debug + + //If we are under a EVENT tag under a EVENTS zone, accumule it + if (isset($this->tree[4]) and isset($this->tree[3])) { + if (($this->tree[4] == "EVENT") and ($this->tree[3] == "EVENTS")) { + 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 @@ -2322,6 +2457,56 @@ $this->content = ""; } + //This is the endTag handler we use where we are reading the events zone (todo="EVENTS") + function endElementEvents($parser, $tagName) { + //Check if we are into EVENTS zone + if ($this->tree[3] == "EVENTS") { + //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 event, xmlize it an save to db + if (($this->level == 4) and ($tagName == "EVENT")) { + //Prepend XML standard header to info gathered + $xml_data = "\n".$this->temp; + //Call to xmlize for this portion of xml data (one EVENT) + //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 + $event_id = $data["EVENT"]["#"]["ID"]["0"]["#"]; + //Save to db + $status = backup_putid($this->preferences->backup_unique_code,"event",$event_id, + null,$data); + //Create returning info + $ret_info->id = $event_id; + $this->info[] = $ret_info; + //Reset temp + unset($this->temp); + } + } + + //Stop parsing if todo = EVENTS and tagName = EVENT (en of the tag, of course) + //Speed up a lot (avoid parse all) + if ($tagName == "EVENTS" 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 @@ -2491,6 +2676,9 @@ } else if ($todo == "GROUPS") { //Define handlers to that zone xml_set_element_handler($xml_parser, "startElementGroups", "endElementGroups"); + } else if ($todo == "EVENTS") { + //Define handlers to that zone + xml_set_element_handler($xml_parser, "startElementEvents", "endElementEvents"); } 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 83c4dd281f..51d9161985 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 = 2004022000; // The current version is a date (YYYYMMDDXX) +$backup_version = 2004022100; // The current version is a date (YYYYMMDDXX) $backup_release = "1.2 alpha"; // User-friendly version number -- 2.39.5