]> git.mjollnir.org Git - moodle.git/commitdiff
Now metacourses are working in restore too!
authorstronk7 <stronk7>
Mon, 7 Feb 2005 17:52:58 +0000 (17:52 +0000)
committerstronk7 <stronk7>
Mon, 7 Feb 2005 17:52:58 +0000 (17:52 +0000)
Please test them!!

backup/CHANGES_14_15.txt
backup/restore_check.html
backup/restore_execute.html
backup/restore_form.html
backup/restorelib.php

index 3bc121161a43a5d599cdbc8be1336736d2dfa699..714edc6f70ba157e5e3fe964a6e58f3ff9233845 100644 (file)
@@ -36,12 +36,12 @@ Now I show the specific detailed status of every item in the process:
           mimic it.
 22. TODO: Review the wiki module fully (if possible, try to make binary
           uploaded contents to work).
-23. PROG: Add support for metacourses in backup and restore. Show a new option
+23. DONE: Add support for metacourses in backup and restore. Show a new option
           to decide what to do (ignore, process).
 24. DONE: Take out THEME from backup directory.
 25. TODO: Add support for forum_read table in backup and restore.
 26. DONE: chat_users->course and chat_users->lang. No changes required!
 27. PROG: Make the backup/restore/config/logs/index.php XHTML 1.0 Transitional.
-          Done in manual backups and logs
+          Done in manual backups, config and logs
 Eloy (stronk7)
 23-01-2004
index 879a8d57ca5ec1e44d30e3a9cfb0bdcc269a32e7..a0a926a98f3dfc58e355a4b590a0ee95f6ce52ed 100644 (file)
@@ -49,6 +49,8 @@
         }
         //restoreto
         require_variable($restore_restoreto);
+        //restore_metacourse
+        require_variable($restore_metacourse);
         //restore_users
         require_variable($restore_users);
         //restore_logs
@@ -79,6 +81,7 @@
             }
         }
         $restore->restoreto=$restore_restoreto;
+        $restore->metacourse=$restore_metacourse;
         $restore->users=$restore_users;
         $restore->logs=$restore_logs;
         $restore->user_files=$restore_user_files;
@@ -94,7 +97,7 @@
     //We have the object with data, put it in the session
     $SESSION->restore = $restore;
 
-    //From here to the wnd of the script, only use the $restore object
+    //From here to the end of the script, only use the $restore object
 
     //Check login
     require_login();
index 0ba822003ba4a15a54d0d74da52b11c2b725cea7..4031b00f2cbee2349f4685b2947c36291ec61a2c 100644 (file)
         }
     }
 
-
     //Now create users as needed 
     if ($status and ($restore->users == 0 or $restore->users == 1)) {
         echo "<li>".get_string("creatingusers")."<br />";
         }
     }
 
+    //Now create metacourse info
+    if ($status and $restore->metacourse) {
+        //Only to new courses!
+        if ($restore->restoreto == 2) {
+            echo "<li>".get_string("creatingmetacoursedata");
+            if (!$status = restore_create_metacourse($restore,$xml_file)) {
+                notify("Error creating metacourse in the course.");
+            }
+        }
+    }
+
+
     //Now create categories and questions as needed (STEP1)
     if ($status and ($restore->mods['quiz']->restore)) {
          echo "<li>".get_string("creatingcategoriesandquestions")."<br />";
index 14b4749e5d67503994de6dc2d176fac3b7b2d830..625fd91c72e4a4569fab53333592aa797e5fc321 100644 (file)
     }
 
     //Check other parameters
+    if (!isset($restore_metacourse)) {
+        $restore_metacourse = 1;
+    }
+   
     if (!isset($restore_users)) {
         $restore_users = 1;
     }
@@ -199,6 +203,21 @@ function selectItemInMenuByName(formId, menuName, selectIndex ) {
         //Line
         echo "<tr><td colspan=\"2\"><hr noshade size=\"1\"></td></tr>";
 
+        //Now print the Metacourse tr
+        echo "<tr>";
+        echo "<td align=\"right\"><p><b>";
+        echo get_string("metacourse").":";                                               
+        echo "</td><td>";
+        //If metacourse are in the backup file, show menu, else fixed to no
+        if ($info->backup_metacourse == "true") {
+            $metacourse_options[0] = get_string("no");
+            $metacourse_options[1] = get_string("yes"); 
+            choose_from_menu($metacourse_options, "restore_metacourse", $restore_metacourse, ""); 
+        } else {
+            echo get_string("no");
+            echo "<input type=\"hidden\" name=\"restore_metacourse\" value=\"0\">";
+        }
+        echo "</td></tr>";
         //Now print the Users tr
         echo "<tr>";
         echo "<td align=\"right\"><p><b>";
index 5a5a86da2dbbdeae6266cf2378b1dead2b6c9d71..00235d48b43d5831df539c86f6f12a2e6071d88a 100644 (file)
         return $info;
     }
     
+    //This function read the xml file and store its data from the metacourse in a object
+    function restore_read_xml_metacourse ($xml_file) {
+
+        //We call the main read_xml function, with todo = METACOURSE
+        $info = restore_read_xml ($xml_file,"METACOURSE",false);
+
+        return $info;
+    }
+
     //This function read the xml file and store its data from the users in 
     //backup_ids->info db (and user's id in $info)
     function restore_read_xml_users ($restore,$xml_file) {
                 }
                 $elem++;
             }
+            //Metacourse info
+            $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
+            if ($info->backup_metacourse == "true") {
+                $tab[$elem][1] = get_string("yes");
+            } else {
+                $tab[$elem][1] = get_string("no");
+            }
+            $elem++;
             //Users info
             $tab[$elem][0] = "<b>".get_string("users").":</b>";
             $tab[$elem][1] = get_string($info->backup_users);
             $course->hiddensections = addslashes($course_header->course_hiddensections);
             $course->timecreated = addslashes($course_header->course_timecreated);
             $course->timemodified = addslashes($course_header->course_timemodified);
+            $course->metacourse = addslashes($course_header->course_metacourse);
             //Calculate sortorder field
             $sortmax = get_record_sql('SELECT MAX(sortorder) AS max
                                        FROM ' . $CFG->prefix . 'course
                 $course->lang = 'mi_nt';
             }
 
+            //Disable course->metacourse if avoided in restore config
+            if (!$restore->metacourse) {
+                $course->metacourse = 0;
+            }
+
             //Now insert the record
             $newid = insert_record("course",$course);
             if ($newid) {
         }
         return $status;
     }
+
+    //This function creates all the metacourse data from xml, notifying 
+    //about each incidence
+    function restore_create_metacourse($restore,$xml_file) {
+
+        global $CFG,$db;
+
+        $status = true;
+        //Check it exists
+        if (!file_exists($xml_file)) {
+            $status = false;
+        }
+        //Get info from xml
+        if ($status) {
+            //Load data from XML to info
+            $info = restore_read_xml_metacourse($xml_file);
+        }
+
+        //Process info about metacourse
+        if ($status and $info) {
+            //Process child records
+            if ($info->childs) {
+                foreach ($info->childs as $child) {
+                    $dbcourse = false;
+                    $dbmetacourse = false;
+                    //Check if child course exists in destination server
+                    //(by id in the same server or by idnumber and shortname in other server)
+                    if (!$restore->original_wwwroot == $CFG->wwwroot) {
+                        //Same server, lets see by id
+                        $dbcourse = get_record('course','id',$child->id);
+                    } else {
+                        //Different server, lets see by idnumber and shortname, and only ONE record
+                        $dbcount = count_records('course','idnumber',$child->idnumber,'shortname',$child->shortname);
+                        if ($dbcount == 1) {
+                            $dbcourse = get_record('course','idnumber',$child->idnumber,'shortname',$child->shortname);
+                        }
+                    }
+                    //If child course has been found, insert data
+                    if ($dbcourse) {
+                        $dbmetacourse->child_course = $dbcourse->id;
+                        $dbmetacourse->parent_course = $restore->course_id;
+                        $status = insert_record ('course_meta',$dbmetacourse);
+                    } else {
+                        //Child course not found, notice!
+                        echo '<ul><li>'.get_string ('childcoursenotfound').' ('.$child->id.'/'.$child->idnumber.'/'.$child->shortname.')</li></ul>';
+                    }
+                }
+                //Now, recreate student enrolments...
+                sync_metacourse($restore->course_id);
+            }
+            //Process parent records
+            if ($info->parents) {
+                foreach ($info->parents as $parent) {
+                    $dbcourse = false;
+                    $dbmetacourse = false;
+                    //Check if parent course exists in destination server
+                    //(by id in the same server or by idnumber and shortname in other server)
+                    if (!$restore->original_wwwroot == $CFG->wwwroot) {
+                        //Same server, lets see by id
+                        $dbcourse = get_record('course','id',$parent->id);
+                    } else {
+                        //Different server, lets see by idnumber and shortname, and only ONE record
+                        $dbcount = count_records('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
+                        if ($dbcount == 1) {
+                            $dbcourse = get_record('course','idnumber',$parent->idnumber,'shortname',$parent->shortname);
+                        }
+                    }
+                    //If parent course has been found, insert data if it is a metacourse
+                    if ($dbcourse) {
+                        if ($dbcourse->metacourse) {
+                            $dbmetacourse->parent_course = $dbcourse->id;
+                            $dbmetacourse->child_course = $restore->course_id;
+                            $status = insert_record ('course_meta',$dbmetacourse);
+                            //Now, recreate student enrolments in parent course
+                            sync_metacourse($dbcourse->id);
+                        } else {
+                        //Parent course isn't metacourse, notice!
+                        echo '<ul><li>'.get_string ('parentcoursenotmetacourse').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
+                        }
+                    } else {
+                        //Parent course not found, notice!
+                        echo '<ul><li>'.get_string ('parentcoursenotfound').' ('.$parent->id.'/'.$parent->idnumber.'/'.$parent->shortname.')</li></ul>';
+                    }
+                }
+            }
+
+        }
+        return $status;
+    }
     
     //This function creates all the user, user_students, user_teachers
     //user_course_creators and user_admins from xml
             //if ($this->tree[3] == "SECTIONS")                                                         //Debug
             //    echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n";   //Debug
         }
+
+        //This is the startTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
+        function startElementMetacourse($parser, $tagName, $attrs) {
+
+            //Refresh properties     
+            $this->level++;
+            $this->tree[$this->level] = $tagName;   
+            
+            //Output something to avoid browser timeouts...
+            backup_flush();
+
+            //Check if we are into METACOURSE zone
+            //if ($this->tree[3] == "METACOURSE")                                                         //Debug
+            //    echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n";   //Debug
+        }
+        
         
         //This is the startTag handler we use where we are reading the user zone (todo="USERS")
         function startElementUsers($parser, $tagName, $attrs) {
                 if ($this->tree[3] == "DETAILS") {
                     if ($this->level == 4) {
                         switch ($tagName) {
+                            case "METACOURSE":
+                                $this->info->backup_metacourse = $this->getContents();
+                                break;
                             case "USERS":
                                 $this->info->backup_users = $this->getContents();
                                 break;
                         case "TIMEMODIFIED":
                             $this->info->course_timemodified = $this->getContents();
                             break;
+                        case "METACOURSE":
+                            $this->info->course_metacourse = $this->getContents();
+                            break;
                     }
                 }
                 if ($this->tree[4] == "CATEGORY") {
                 $this->finished = true;
             }
         }
+
+        //This is the endTag handler we use where we are reading the metacourse zone (todo="METACOURSE")
+        function endElementMetacourse($parser, $tagName) {
+            //Check if we are into METACOURSE zone
+            if ($this->tree[3] == 'METACOURSE') {
+                //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
+                //Dependig of different combinations, do different things
+                if ($this->level == 5) {
+                    switch ($tagName) {
+                        case 'CHILD':
+                            //We've finalized a child, get it
+                            $this->info->childs[] = $this->info->tempmeta;
+                            unset($this->info->tempmeta);
+                            break;
+                        case 'PARENT':
+                            //We've finalized a parent, get it
+                            $this->info->parents[] = $this->info->tempmeta;
+                            unset($this->info->tempmeta);
+                            break;
+                        default:
+                            die($tagName);
+                    }
+                }
+                if ($this->level == 6) {
+                    switch ($tagName) {
+                        case 'ID':
+                            $this->info->tempmeta->id = $this->getContents();
+                            break;
+                        case 'IDNUMBER':
+                            $this->info->tempmeta->idnumber = $this->getContents();
+                            break;
+                        case 'SHORTNAME':
+                            $this->info->tempmeta->shortname = $this->getContents();
+                            break;
+                    }
+                }
+            }
+            //Clear things
+            $this->tree[$this->level] = '';
+            $this->level--;
+            $this->content = "";
+
+            //Stop parsing if todo = METACOURSE and tagName = METACOURSE (en of the tag, of course)
+            //Speed up a lot (avoid parse all)
+            if ($this->tree[3] == 'METACOURSE' && $tagName == 'METACOURSE') {
+                $this->finished = true;
+            }
+        }
+
         
         //This is the endTag handler we use where we are reading the users zone (todo="USERS")
         function endElementUsers($parser, $tagName) {
         } else if ($todo == "SECTIONS") {
             //Define handlers to that zone
             xml_set_element_handler($xml_parser, "startElementSections", "endElementSections");
+        } else if ($todo == "METACOURSE") {
+            //Define handlers to that zone
+            xml_set_element_handler($xml_parser, "startElementMetacourse", "endElementMetacourse");
         } else if ($todo == "USERS") {
             //Define handlers to that zone
             xml_set_element_handler($xml_parser, "startElementUsers", "endElementUsers");