]> git.mjollnir.org Git - moodle.git/commitdiff
Some important changes:
authorstronk7 <stronk7>
Sat, 21 Feb 2004 01:30:22 +0000 (01:30 +0000)
committerstronk7 <stronk7>
Sat, 21 Feb 2004 01:30:22 +0000 (01:30 +0000)
    - 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
backup/backup_execute.html
backup/backup_scheduled.php
backup/backuplib.php
backup/restore_execute.html
backup/restorelib.php
backup/version.php

index 8fdb61f37680c3893db7f649e5c32324cb1b58fa..2a77101a3d99f8b5fcf08be4100c35fa41ed4736 100644 (file)
@@ -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
index c328076cc1233d1a27523938450ad891cd33d9f0..59d8ab14a94c856bbe8303cbe5b192d034067f0e 100644 (file)
             }
         }
 
+        //Print events info
+        if ($status) { 
+            echo "<li>".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) {
index f53be0a55c32960219aa6d533664bf49678d50aa..676d55660f003c80fb619ee27801fa9240200990 100644 (file)
@@ -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) {
index 4bfa38d3ad2cf6fedc8f245d431d48cf7a01d452..33f072615ad216837257b98a5b0a5016b8341ca5 100644 (file)
         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) {
     
index 50021a3fc63744691f0a1444265f6a1d6e1adfa2..e41324de6a00581d6307a029720d60a03b8b4317 100644 (file)
         }
     }
 
+    //Now create events as needed
+    if ($status) {
+        echo "<li>".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 "<li>".get_string("creatingcoursemodules");
index cc4e88540a32641254f5a0c4c70912210a46387c..e1d6d5effafb78b197eb600eb7aee89f3e2d7d16 100644 (file)
         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) {
                         //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']['#']);
         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) {
                 $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 = "";
             }
         }
 
+        //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 "<P>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("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br>\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
             $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("&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 event, xmlize it an save to db
+                if (($this->level == 4) and ($tagName == "EVENT")) {
+                    //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 EVENT)
+                    //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
+                    $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
         } 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");
index 83c4dd281ff89540886e8ea4cd2a7b4f7a672465..51d91619851fb166b2d849ef0aac56b2147d8ade 100644 (file)
@@ -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