Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / survey / backuplib.php
blob708813f09d52d90d85c482b2d9aae56e414edafa
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //survey mods
5 //This is the "graphical" structure of the survey mod:
6 // --------------------
7 // survey | survey_questions |
8 // (CL,pk->id) |(CL,pk->id,?????) |
9 // | --------------------
10 // |
11 // -----------------------------------
12 // | |
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) {
27 global $CFG;
29 $status = true;
31 //Iterate over survey table
32 $surveys = get_records ("survey","course",$preferences->backup_course,"id");
33 if ($surveys) {
34 foreach ($surveys as $survey) {
35 if (backup_mod_selected($preferences,'survey',$survey->id)) {
36 $status = survey_backup_one_mod($bf,$preferences,$survey);
40 return $status;
43 function survey_backup_one_mod($bf,$preferences,$survey) {
45 $status = true;
47 if (is_numeric($survey)) {
48 $survey = get_record('survey','id',$survey);
51 //Start mod
52 fwrite ($bf,start_tag("MOD",3,true));
53 //Print choice data
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);
70 //End mod
71 $status =fwrite ($bf,end_tag("MOD",3,true));
72 return $status;
75 //Backup survey_answers contents (executed from survey_backup_mods)
76 function backup_survey_answers ($bf,$preferences,$survey) {
78 global $CFG;
80 $status = true;
82 $survey_answers = get_records("survey_answers","survey",$survey,"id");
83 //If there is answers
84 if ($survey_answers) {
85 //Write start tag
86 $status =fwrite ($bf,start_tag("ANSWERS",4,true));
87 //Iterate over each answer
88 foreach ($survey_answers as $sur_ans) {
89 //Start answer
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));
98 //End answer
99 $status =fwrite ($bf,end_tag("ANSWER",5,true));
101 //Write end tag
102 $status =fwrite ($bf,end_tag("ANSWERS",4,true));
104 return $status;
107 //Backup survey_analysis contents (executed from survey_backup_mods)
108 function backup_survey_analysis ($bf,$preferences,$survey) {
110 global $CFG;
112 $status = true;
114 $survey_analysis = get_records("survey_analysis","survey",$survey,"id");
115 //If there is analysis
116 if ($survey_analysis) {
117 //Write start tag
118 $status =fwrite ($bf,start_tag("ANALYSIS",4,true));
119 //Iterate over each analysis
120 foreach ($survey_analysis as $sur_ana) {
121 //Start answer
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));
127 //End answer
128 $status =fwrite ($bf,end_tag("ANALYS",5,true));
130 //Write end tag
131 $status =fwrite ($bf,end_tag("ANALYSIS",4,true));
133 return $status;
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)) {
140 //Subscriptions
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);
144 } else {
145 $info[$instance->id.'1'][1] = 0;
148 return $info;
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)) {
155 $info = array();
156 foreach ($instances as $id => $instance) {
157 $info += survey_check_backup_mods_instances($instance,$backup_unique_code);
159 return $info;
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);
165 } else {
166 $info[0][1] = 0;
169 //Now, if requested, the user_data
170 if ($user_data) {
171 //Subscriptions
172 $info[1][0] = get_string("answers","survey");
173 if ($ids = survey_answer_ids_by_course ($course)) {
174 $info[1][1] = count($ids);
175 } else {
176 $info[1][1] = 0;
179 return $info;
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) {
186 global $CFG;
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);
198 return $result;
201 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
203 //Returns an array of surveys id
204 function survey_ids ($course) {
206 global $CFG;
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) {
216 global $CFG;
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
222 s.survey = a.id");
225 function survey_answer_ids_by_instance ($instanceid) {
227 global $CFG;
229 return get_records_sql ("SELECT s.id , s.survey
230 FROM {$CFG->prefix}survey_answers s
231 WHERE s.survey = $instanceid");