2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the survey mod:
6 // --------------------
7 // survey | survey_questions |
8 // (CL,pk->id) |(CL,pk->id,?????) |
9 // | --------------------
11 // -----------------------------------
13 // survey_analysis survey_answers
14 // (UL,pk->id, fk->survey) (UL,pk->id, fk->survey)
16 // Meaning: pk->primary key field of the table
17 // fk->foreign key to link with parent
18 // nt->nested field (recursive data)
19 // CL->course level info
20 // UL->user level info
21 // files->table may have files)
23 //-----------------------------------------------------------
25 function survey_backup_mods($bf,$preferences) {
31 //Iterate over survey table
32 $surveys = get_records ("survey","course",$preferences->backup_course
,"id");
34 foreach ($surveys as $survey) {
35 if (backup_mod_selected($preferences,'survey',$survey->id
)) {
36 $status = survey_backup_one_mod($bf,$preferences,$survey);
43 function survey_backup_one_mod($bf,$preferences,$survey) {
47 if (is_numeric($survey)) {
48 $survey = get_record('survey','id',$survey);
52 fwrite ($bf,start_tag("MOD",3,true));
54 fwrite ($bf,full_tag("ID",4,false,$survey->id
));
55 fwrite ($bf,full_tag("MODTYPE",4,false,"survey"));
56 fwrite ($bf,full_tag("TEMPLATE",4,false,$survey->template
));
57 fwrite ($bf,full_tag("DAYS",4,false,$survey->days
));
58 fwrite ($bf,full_tag("TIMECREATED",4,false,$survey->timecreated
));
59 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$survey->timemodified
));
60 fwrite ($bf,full_tag("NAME",4,false,$survey->name
));
61 fwrite ($bf,full_tag("INTRO",4,false,$survey->intro
));
62 fwrite ($bf,full_tag("QUESTIONS",4,false,$survey->questions
));
64 //if we've selected to backup users info, then execute backup_survey_answers and
65 //backup_survey_analysis
66 if (backup_userdata_selected($preferences,'survey',$survey->id
)) {
67 $status = backup_survey_answers($bf,$preferences,$survey->id
);
68 $status = backup_survey_analysis($bf,$preferences,$survey->id
);
71 $status =fwrite ($bf,end_tag("MOD",3,true));
75 //Backup survey_answers contents (executed from survey_backup_mods)
76 function backup_survey_answers ($bf,$preferences,$survey) {
82 $survey_answers = get_records("survey_answers","survey",$survey,"id");
84 if ($survey_answers) {
86 $status =fwrite ($bf,start_tag("ANSWERS",4,true));
87 //Iterate over each answer
88 foreach ($survey_answers as $sur_ans) {
90 $status =fwrite ($bf,start_tag("ANSWER",5,true));
91 //Print survey_answers contents
92 fwrite ($bf,full_tag("ID",6,false,$sur_ans->id
));
93 fwrite ($bf,full_tag("USERID",6,false,$sur_ans->userid
));
94 fwrite ($bf,full_tag("QUESTION",6,false,$sur_ans->question
));
95 fwrite ($bf,full_tag("TIME",6,false,$sur_ans->time
));
96 fwrite ($bf,full_tag("ANSWER1",6,false,$sur_ans->answer1
));
97 fwrite ($bf,full_tag("ANSWER2",6,false,$sur_ans->answer2
));
99 $status =fwrite ($bf,end_tag("ANSWER",5,true));
102 $status =fwrite ($bf,end_tag("ANSWERS",4,true));
107 //Backup survey_analysis contents (executed from survey_backup_mods)
108 function backup_survey_analysis ($bf,$preferences,$survey) {
114 $survey_analysis = get_records("survey_analysis","survey",$survey,"id");
115 //If there is analysis
116 if ($survey_analysis) {
118 $status =fwrite ($bf,start_tag("ANALYSIS",4,true));
119 //Iterate over each analysis
120 foreach ($survey_analysis as $sur_ana) {
122 $status =fwrite ($bf,start_tag("ANALYS",5,true));
123 //Print survey_analysis contents
124 fwrite ($bf,full_tag("ID",6,false,$sur_ana->id
));
125 fwrite ($bf,full_tag("USERID",6,false,$sur_ana->userid
));
126 fwrite ($bf,full_tag("NOTES",6,false,$sur_ana->notes
));
128 $status =fwrite ($bf,end_tag("ANALYS",5,true));
131 $status =fwrite ($bf,end_tag("ANALYSIS",4,true));
136 function survey_check_backup_mods_instances($instance,$backup_unique_code) {
137 $info[$instance->id
.'0'][0] = $instance->name
;
138 $info[$instance->id
.'0'][1] = '';
139 if (!empty($instance->userdata
)) {
141 $info[$instance->id
.'1'][0] = get_string("answers","survey");
142 if ($ids = survey_answer_ids_by_instance ($instance->id
)) {
143 $info[$instance->id
.'1'][1] = count($ids);
145 $info[$instance->id
.'1'][1] = 0;
152 ////Return an array of info (name,value)
153 function survey_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
154 if (!empty($instances) && is_array($instances) && count($instances)) {
156 foreach ($instances as $id => $instance) {
157 $info +
= survey_check_backup_mods_instances($instance,$backup_unique_code);
161 //First the course data
162 $info[0][0] = get_string("modulenameplural","survey");
163 if ($ids = survey_ids ($course)) {
164 $info[0][1] = count($ids);
169 //Now, if requested, the user_data
172 $info[1][0] = get_string("answers","survey");
173 if ($ids = survey_answer_ids_by_course ($course)) {
174 $info[1][1] = count($ids);
182 //Return a content encoded to support interactivities linking. Every module
183 //should have its own. They are called automatically from the backup procedure.
184 function servey_encode_content_links ($content,$preferences) {
188 $base = preg_quote($CFG->wwwroot
,"/");
190 //Link to the list of serveys
191 $buscar="/(".$base."\/mod\/servey\/index.php\?id\=)([0-9]+)/";
192 $result= preg_replace($buscar,'$@SURVEYINDEX*$2@$',$content);
194 //Link to servey view by moduleid
195 $buscar="/(".$base."\/mod\/servey\/view.php\?id\=)([0-9]+)/";
196 $result= preg_replace($buscar,'$@SURVEYVIEWBYID*$2@$',$result);
201 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
203 //Returns an array of surveys id
204 function survey_ids ($course) {
208 return get_records_sql ("SELECT a.id, a.course
209 FROM {$CFG->prefix}survey a
210 WHERE a.course = '$course'");
213 //Returns an array of survey answer id
214 function survey_answer_ids_by_course ($course) {
218 return get_records_sql ("SELECT s.id , s.survey
219 FROM {$CFG->prefix}survey_answers s,
220 {$CFG->prefix}survey a
221 WHERE a.course = '$course' AND
225 function survey_answer_ids_by_instance ($instanceid) {
229 return get_records_sql ("SELECT s.id , s.survey
230 FROM {$CFG->prefix}survey_answers s
231 WHERE s.survey = $instanceid");