]> git.mjollnir.org Git - moodle.git/commitdiff
Restore surveys COMPLETED.
authorstronk7 <stronk7>
Sun, 1 Jun 2003 19:38:14 +0000 (19:38 +0000)
committerstronk7 <stronk7>
Sun, 1 Jun 2003 19:38:14 +0000 (19:38 +0000)
backup/mod/survey/restorelib.php [new file with mode: 0644]

diff --git a/backup/mod/survey/restorelib.php b/backup/mod/survey/restorelib.php
new file mode 100644 (file)
index 0000000..7f429c6
--- /dev/null
@@ -0,0 +1,175 @@
+<?PHP //$Id$
+    //This php script contains all the stuff to backup/restore
+    //survey mods
+
+    //This is the "graphical" structure of the survey mod:
+    //                                                    --------------------
+    //                           survey                   | survey_questions |
+    //                        (CL,pk->id)                 |(CL,pk->id,?????) |
+    //                            |                       --------------------
+    //                            |
+    //             -----------------------------------        
+    //             |                                 |
+    //        survey_analysis                   survey_answers
+    //    (UL,pk->id, fk->survey)           (UL,pk->id, fk->survey)
+    //
+    // Meaning: pk->primary key field of the table
+    //          fk->foreign key to link with parent
+    //          nt->nested field (recursive data)
+    //          CL->course level info
+    //          UL->user level info
+    //          files->table may have files)
+    //
+    //-----------------------------------------------------------
+
+
+    function survey_restore_mods($mod,$restore) {
+
+        global $CFG,$db;
+
+        $status = true;
+
+        //Get record from backup_ids
+        $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
+
+        if ($data) {
+            //We have info, get and unserialize info
+            //First strip slashes
+            $temp = stripslashes($data->info);     
+            //Now get completed xmlized object   
+            $info = unserialize($temp);
+            //traverse_xmlize($info);                                                                     //Debug
+            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
+            //$GLOBALS['traverse_array']="";                                                              //Debug
+
+            //Now, build the SURVEY record structure
+            $survey->course = $restore->course_id;
+            $survey->template = backup_todb($info['MOD']['#']['TEMPLATE']['0']['#']);
+            $survey->days = backup_todb($info['MOD']['#']['DAYS']['0']['#']);
+            $survey->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
+            $survey->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
+            $survey->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
+            $survey->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
+            $survey->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
+
+            //The structure is equal to the db, so insert the survey
+            $newid = insert_record ("survey",$survey);
+            if ($newid) {
+                //We have the newid, update backup_ids
+                backup_putid($restore->backup_unique_code,$mod->modtype,
+                             $mod->id, $newid);
+                //Now check if want to restore user data and do it.
+                if ($restore->mods[survey]->userinfo) {
+                    //Restore survey_answers
+                    $status = survey_answers_restore_mods ($newid,$info,$restore);
+                    //Restore survey_analysis
+                    if ($status) {
+                        $status = survey_analysis_restore_mods ($newid,$info,$restore);
+                    }
+                }
+            } else {
+                $status = false;
+            }
+
+        } else {
+            $status = false;
+        }
+
+        return $status;
+    }
+
+    //This function restores the survey_answers
+    function survey_answers_restore_mods($survey_id,$info,$restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        //Get the answers array
+        $answers = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
+
+        //Iterate over answers
+        for($i = 0; $i < sizeof($answers); $i++) {
+            $sub_info = $answers[$i];
+            //traverse_xmlize($sub_info);                                                                 //Debug
+            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
+            //$GLOBALS['traverse_array']="";                                                              //Debug
+
+            //We'll need this later!!
+            $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
+            $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
+
+            //Now, build the SURVEY_ANSWERS record structure
+            $answer->survey = $survey_id;
+            $answer->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
+            $answer->question = backup_todb($sub_info['#']['QUESTION']['0']['#']);
+            $answer->time = backup_todb($sub_info['#']['TIME']['0']['#']);
+            $answer->answer1 = backup_todb($sub_info['#']['ANSWER1']['0']['#']);
+            $answer->answer2 = backup_todb($sub_info['#']['ANSWER2']['0']['#']);
+
+            //We have to recode the userid field
+            $user = backup_getid($restore->backup_unique_code,"user",$answer->userid);
+            if ($user) {
+                $answer->userid = $user->new_id;
+            }
+
+            //The structure is equal to the db, so insert the survey_answers
+            $newid = insert_record ("survey_answers",$answer);
+            if ($newid) {
+                //We have the newid, update backup_ids
+                backup_putid($restore->backup_unique_code,"survey_answers",$oldid,
+                             $newid);
+            } else {
+                $status = false;
+            }
+        }
+
+        return $status;
+    }
+
+    //This function restores the survey_analysis
+    function survey_analysis_restore_mods($survey_id,$info,$restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        //Get the analysis array
+        $analysis = $info['MOD']['#']['ANALYSIS']['0']['#']['ANALYS'];
+
+        //Iterate over analysis
+        for($i = 0; $i < sizeof($analysis); $i++) {
+            $sub_info = $analysis[$i];
+            traverse_xmlize($sub_info);                                                                 //Debug
+            print_object ($GLOBALS['traverse_array']);                                                  //Debug
+            $GLOBALS['traverse_array']="";                                                              //Debug
+
+            //We'll need this later!!
+            $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
+            $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
+
+            //Now, build the SURVEY_ANALYSIS record structure
+            $analys->survey = $survey_id;
+            $analys->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
+            $analys->notes = backup_todb($sub_info['#']['NOTES']['0']['#']);
+
+            //We have to recode the userid field
+            $user = backup_getid($restore->backup_unique_code,"user",$analys->userid);
+            if ($user) {
+                $analys->userid = $user->new_id;
+            }
+
+            //The structure is equal to the db, so insert the survey_analysis
+            $newid = insert_record ("survey_analysis",$analys);
+            if ($newid) {
+                //We have the newid, update backup_ids
+                backup_putid($restore->backup_unique_code,"survey_analysis",$oldid,
+                             $newid);
+            } else {
+                $status = false;
+            }
+        }
+
+        return $status;
+    }
+?>