MDL-23479 backup - survey revised & old code deleted (but restore logs)
authorEloy Lafuente <stronk7@moodle.org>
Mon, 2 Aug 2010 14:10:43 +0000 (14:10 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Mon, 2 Aug 2010 14:10:43 +0000 (14:10 +0000)
mod/survey/backup/moodle2/restore_survey_stepslib.php
mod/survey/backuplib.php [deleted file]
mod/survey/restorelib.php

index 3611c4b..96e2c3b 100644 (file)
@@ -81,6 +81,7 @@ class restore_survey_activity_structure_step extends restore_activity_structure_
         $oldid = $data->id;
         $data->survey = $this->get_new_parentid('survey');
         $data->userid = $this->get_mappingid('user', $data->userid);
+        $data->time = $this->apply_date_offset($data->time);
 
         $newitemid = $DB->insert_record('survey_answers', $data);
         // No need to save this mapping as far as nothing depend on it
diff --git a/mod/survey/backuplib.php b/mod/survey/backuplib.php
deleted file mode 100644 (file)
index 546aa89..0000000
+++ /dev/null
@@ -1,227 +0,0 @@
-<?php
-    //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_backup_mods($bf,$preferences) {
-        global $DB;
-
-        $status = true;
-
-        //Iterate over survey table
-        $surveys = $DB->get_records ("survey", array("course"=>$preferences->backup_course),"id");
-        if ($surveys) {
-            foreach ($surveys as $survey) {
-                if (backup_mod_selected($preferences,'survey',$survey->id)) {
-                    $status = survey_backup_one_mod($bf,$preferences,$survey);
-                }
-            }
-        }
-        return $status;
-    }
-
-    function survey_backup_one_mod($bf,$preferences,$survey) {
-        global $DB;
-
-        $status = true;
-
-        if (is_numeric($survey)) {
-            $survey = $DB->get_record('survey', array('id'=>$survey));
-        }
-
-        //Start mod
-        fwrite ($bf,start_tag("MOD",3,true));
-        //Print choice data
-        fwrite ($bf,full_tag("ID",4,false,$survey->id));
-        fwrite ($bf,full_tag("MODTYPE",4,false,"survey"));
-        fwrite ($bf,full_tag("TEMPLATE",4,false,$survey->template));
-        fwrite ($bf,full_tag("DAYS",4,false,$survey->days));
-        fwrite ($bf,full_tag("TIMECREATED",4,false,$survey->timecreated));
-        fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$survey->timemodified));
-        fwrite ($bf,full_tag("NAME",4,false,$survey->name));
-        fwrite ($bf,full_tag("INTRO",4,false,$survey->intro));
-        fwrite ($bf,full_tag("QUESTIONS",4,false,$survey->questions));
-
-        //if we've selected to backup users info, then execute backup_survey_answers and
-        //backup_survey_analysis
-        if (backup_userdata_selected($preferences,'survey',$survey->id)) {
-            $status = backup_survey_answers($bf,$preferences,$survey->id);
-            $status = backup_survey_analysis($bf,$preferences,$survey->id);
-        }
-        //End mod
-        $status =fwrite ($bf,end_tag("MOD",3,true));
-        return $status;
-    }
-
-    //Backup survey_answers contents (executed from survey_backup_mods)
-    function backup_survey_answers ($bf,$preferences,$survey) {
-        global $CFG, $DB;
-
-        $status = true;
-
-        $survey_answers = $DB->get_records("survey_answers", array("survey"=>$survey), "id");
-        //If there is answers
-        if ($survey_answers) {
-            //Write start tag
-            $status =fwrite ($bf,start_tag("ANSWERS",4,true));
-            //Iterate over each answer
-            foreach ($survey_answers as $sur_ans) {
-                //Start answer
-                $status =fwrite ($bf,start_tag("ANSWER",5,true));
-                //Print survey_answers contents
-                fwrite ($bf,full_tag("ID",6,false,$sur_ans->id));
-                fwrite ($bf,full_tag("USERID",6,false,$sur_ans->userid));
-                fwrite ($bf,full_tag("QUESTION",6,false,$sur_ans->question));
-                fwrite ($bf,full_tag("TIME",6,false,$sur_ans->time));
-                fwrite ($bf,full_tag("ANSWER1",6,false,$sur_ans->answer1));
-                fwrite ($bf,full_tag("ANSWER2",6,false,$sur_ans->answer2));
-                //End answer
-                $status =fwrite ($bf,end_tag("ANSWER",5,true));
-            }
-            //Write end tag
-            $status =fwrite ($bf,end_tag("ANSWERS",4,true));
-        }
-        return $status;
-    }
-
-    //Backup survey_analysis contents (executed from survey_backup_mods)
-    function backup_survey_analysis ($bf,$preferences,$survey) {
-        global $CFG, $DB;
-
-        $status = true;
-
-        $survey_analysis = $DB->get_records("survey_analysis", array("survey"=>$survey,"id"));
-        //If there is analysis
-        if ($survey_analysis) {
-            //Write start tag
-            $status =fwrite ($bf,start_tag("ANALYSIS",4,true));
-            //Iterate over each analysis
-            foreach ($survey_analysis as $sur_ana) {
-                //Start answer
-                $status =fwrite ($bf,start_tag("ANALYS",5,true));
-                //Print survey_analysis contents
-                fwrite ($bf,full_tag("ID",6,false,$sur_ana->id));
-                fwrite ($bf,full_tag("USERID",6,false,$sur_ana->userid));
-                fwrite ($bf,full_tag("NOTES",6,false,$sur_ana->notes));
-                //End answer
-                $status =fwrite ($bf,end_tag("ANALYS",5,true));
-            }
-            //Write end tag
-            $status =fwrite ($bf,end_tag("ANALYSIS",4,true));
-        }
-        return $status;
-    }
-
-    function survey_check_backup_mods_instances($instance,$backup_unique_code) {
-        $info[$instance->id.'0'][0] = $instance->name;
-        $info[$instance->id.'0'][1] = '';
-        if (!empty($instance->userdata)) {
-              //Subscriptions
-            $info[$instance->id.'1'][0] = get_string("answers","survey");
-            if ($ids = survey_answer_ids_by_instance ($instance->id)) {
-                $info[$instance->id.'1'][1] = count($ids);
-            } else {
-                $info[$instance->id.'1'][1] = 0;
-            }
-        }
-        return $info;
-    }
-
-
-    ////Return an array of info (name,value)
-    function survey_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
-        if (!empty($instances) && is_array($instances) && count($instances)) {
-            $info = array();
-            foreach ($instances as $id => $instance) {
-                $info += survey_check_backup_mods_instances($instance,$backup_unique_code);
-            }
-            return $info;
-        }
-       //First the course data
-       $info[0][0] = get_string("modulenameplural","survey");
-       if ($ids = survey_ids ($course)) {
-           $info[0][1] = count($ids);
-       } else {
-           $info[0][1] = 0;
-       }
-
-        //Now, if requested, the user_data
-        if ($user_data) {
-            //Subscriptions
-            $info[1][0] = get_string("answers","survey");
-            if ($ids = survey_answer_ids_by_course ($course)) {
-                $info[1][1] = count($ids);
-            } else {
-                $info[1][1] = 0;
-            }
-        }
-        return $info;
-    }
-
-    //Return a content encoded to support interactivities linking. Every module
-    //should have its own. They are called automatically from the backup procedure.
-    function servey_encode_content_links ($content,$preferences) {
-
-        global $CFG;
-
-        $base = preg_quote($CFG->wwwroot,"/");
-
-        //Link to the list of serveys
-        $buscar="/(".$base."\/mod\/servey\/index.php\?id\=)([0-9]+)/";
-        $result= preg_replace($buscar,'$@SURVEYINDEX*$2@$',$content);
-
-        //Link to servey view by moduleid
-        $buscar="/(".$base."\/mod\/servey\/view.php\?id\=)([0-9]+)/";
-        $result= preg_replace($buscar,'$@SURVEYVIEWBYID*$2@$',$result);
-
-        return $result;
-    }
-
-    // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
-
-    //Returns an array of surveys id
-    function survey_ids ($course) {
-        global $DB;
-
-        return $DB->get_records_sql ("SELECT a.id, a.course
-                                        FROM {survey} a
-                                       WHERE a.course = ?", array($course));
-    }
-
-    //Returns an array of survey answer id
-    function survey_answer_ids_by_course ($course) {
-        global $DB;
-
-        return $DB->get_records_sql ("SELECT s.id , s.survey
-                                        FROM {survey_answers} s, {survey} a
-                                       WHERE a.course = ? AND s.survey = a.id", array($course));
-    }
-
-    function survey_answer_ids_by_instance ($instanceid) {
-        global $DB;
-
-        return $DB->get_records_sql ("SELECT s.id , s.survey
-                                        FROM {survey_answers} s
-                                       WHERE s.survey = ?", array($instanceid));
-    }
-
-
index b085e76..cbab393 100644 (file)
     //-----------------------------------------------------------
 
 
-    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) {
-            //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 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 = $DB->insert_record ("survey",$survey);
-
-            //Do some output
-            if (!defined('RESTORE_SILENTLY')) {
-                echo "<li>".get_string("modulename","survey")." \"".format_string($survey->name,true)."\"</li>";
-            }
-            backup_flush(300);
-
-            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_userdata_selected($restore,'survey',$mod->id)) {
-                    //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, $DB;
-
-        $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 = $DB->insert_record ("survey_answers",$answer);
-
-            //Do some output
-            if (($i+1) % 50 == 0) {
-                if (!defined('RESTORE_SILENTLY')) {
-                    echo ".";
-                    if (($i+1) % 1000 == 0) {
-                        echo "<br />";
-                    }
-                }
-                backup_flush(300);
-            }
-
-            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, $DB;
-
-        $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 = $DB->insert_record ("survey_analysis",$analys);
-
-            //Do some output
-            if (($i+1) % 50 == 0) {
-                if (!defined('RESTORE_SILENTLY')) {
-                    echo ".";
-                    if (($i+1) % 1000 == 0) {
-                        echo "<br />";
-                    }
-                }
-                backup_flush(300);
-            }
-
-            if ($newid) {
-                //We have the newid, update backup_ids
-                backup_putid($restore->backup_unique_code,"survey_analysis",$oldid,
-                             $newid);
-            } else {
-                $status = false;
-            }
-        }
-
-        return $status;
-    }
-
-    //Return a content decoded to support interactivities linking. Every module
-    //should have its own. They are called automatically from
-    //servey_decode_content_links_caller() function in each module
-    //in the restore process
-    function servey_decode_content_links ($content,$restore) {
-
-        global $CFG;
-
-        $result = $content;
-
-        //Link to the list of serveys
-
-        $searchstring='/\$@(SURVEYINDEX)\*([0-9]+)@\$/';
-        //We look for it
-        preg_match_all($searchstring,$content,$foundset);
-        //If found, then we are going to look for its new id (in backup tables)
-        if ($foundset[0]) {
-            //print_object($foundset);                                     //Debug
-            //Iterate over foundset[2]. They are the old_ids
-            foreach($foundset[2] as $old_id) {
-                //We get the needed variables here (course id)
-                $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
-                //Personalize the searchstring
-                $searchstring='/\$@(SURVEYINDEX)\*('.$old_id.')@\$/';
-                //If it is a link to this course, update the link to its new location
-                if($rec->new_id) {
-                    //Now replace it
-                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/servey/index.php?id='.$rec->new_id,$result);
-                } else {
-                    //It's a foreign link so leave it as original
-                    $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/servey/index.php?id='.$old_id,$result);
-                }
-            }
-        }
-
-        //Link to servey view by moduleid
-
-        $searchstring='/\$@(SURVEYVIEWBYID)\*([0-9]+)@\$/';
-        //We look for it
-        preg_match_all($searchstring,$result,$foundset);
-        //If found, then we are going to look for its new id (in backup tables)
-        if ($foundset[0]) {
-            //print_object($foundset);                                     //Debug
-            //Iterate over foundset[2]. They are the old_ids
-            foreach($foundset[2] as $old_id) {
-                //We get the needed variables here (course_modules id)
-                $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
-                //Personalize the searchstring
-                $searchstring='/\$@(SURVEYVIEWBYID)\*('.$old_id.')@\$/';
-                //If it is a link to this course, update the link to its new location
-                if($rec->new_id) {
-                    //Now replace it
-                    $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/servey/view.php?id='.$rec->new_id,$result);
-                } else {
-                    //It's a foreign link so leave it as original
-                    $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/servey/view.php?id='.$old_id,$result);
-                }
-            }
-        }
-
-        return $result;
-    }
-
-    //This function makes all the necessary calls to xxxx_decode_content_links()
-    //function in each module, passing them the desired contents to be decoded
-    //from backup format to destination site/course in order to mantain inter-activities
-    //working in the backup/restore process. It's called from restore_decode_content_links()
-    //function in restore process
-    function survey_decode_content_links_caller($restore) {
-        global $CFG, $DB;
-        $status = true;
-
-        if ($surveys = $DB->get_records('survey', array('course'=>$restore->course_id), '', "id,intro")) {
-                                               //Iterate over each survey->intro
-            $i = 0;   //Counter to send some output to the browser to avoid timeouts
-            foreach ($surveys as $survey) {
-                //Increment counter
-                $i++;
-                $content = $survey->intro;
-                $result = restore_decode_content_links_worker($content,$restore);
-                if ($result != $content) {
-                    //Update record
-                    $survey->intro = $result;
-                    $status = $DB->update_record("survey",$survey);
-                    if (debugging()) {
-                        if (!defined('RESTORE_SILENTLY')) {
-                            echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
-                        }
-                    }
-                }
-                //Do some output
-                if (($i+1) % 5 == 0) {
-                    if (!defined('RESTORE_SILENTLY')) {
-                        echo ".";
-                        if (($i+1) % 100 == 0) {
-                            echo "<br />";
-                        }
-                    }
-                    backup_flush(300);
-                }
-            }
-        }
-
-        return $status;
-    }
-
     //This function returns a log record with all the necessay transformations
     //done. It's used by restore_log_module() to restore modules log.
     function survey_restore_logs($restore,$log) {