2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the choice mod:
8 // (CL,pk->id)----------|
13 // (UL,pk->id, fk->choiceid) |
18 // (UL,pk->id, fk->choiceid, fk->optionid)
20 // Meaning: pk->primary key field of the table
21 // fk->foreign key to link with parent
22 // nt->nested field (recursive data)
23 // CL->course level info
24 // UL->user level info
25 // files->table may have files)
27 //-----------------------------------------------------------
29 function choice_backup_mods($bf,$preferences) {
35 //Iterate over choice table
36 $choices = get_records ("choice","course",$preferences->backup_course
,"id");
38 foreach ($choices as $choice) {
39 if (backup_mod_selected($preferences,'choice',$choice->id
)) {
40 $status = choice_backup_one_mod($bf,$preferences,$choice);
47 function choice_backup_one_mod($bf,$preferences,$choice) {
51 if (is_numeric($choice)) {
52 $choice = get_record('choice','id',$choice);
58 fwrite ($bf,start_tag("MOD",3,true));
60 fwrite ($bf,full_tag("ID",4,false,$choice->id
));
61 fwrite ($bf,full_tag("MODTYPE",4,false,"choice"));
62 fwrite ($bf,full_tag("NAME",4,false,$choice->name
));
63 fwrite ($bf,full_tag("TEXT",4,false,$choice->text
));
64 fwrite ($bf,full_tag("FORMAT",4,false,$choice->format
));
65 fwrite ($bf,full_tag("PUBLISH",4,false,$choice->publish
));
66 fwrite ($bf,full_tag("SHOWRESULTS",4,false,$choice->showresults
));
67 fwrite ($bf,full_tag("DISPLAY",4,false,$choice->display
));
68 fwrite ($bf,full_tag("ALLOWUPDATE",4,false,$choice->allowupdate
));
69 fwrite ($bf,full_tag("SHOWUNANSWERED",4,false,$choice->showunanswered
));
70 fwrite ($bf,full_tag("LIMITANSWERS",4,false,$choice->limitanswers
));
71 fwrite ($bf,full_tag("TIMEOPEN",4,false,$choice->timeopen
));
72 fwrite ($bf,full_tag("TIMECLOSE",4,false,$choice->timeclose
));
73 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$choice->timemodified
));
75 //Now backup choice_options
76 $status = backup_choice_options($bf,$preferences,$choice->id
);
78 //if we've selected to backup users info, then execute backup_choice_answers
79 if (backup_userdata_selected($preferences,'choice',$choice->id
)) {
80 $status = backup_choice_answers($bf,$preferences,$choice->id
);
83 $status =fwrite ($bf,end_tag("MOD",3,true));
88 //Backup choice_answers contents (executed from choice_backup_mods)
89 function backup_choice_answers ($bf,$preferences,$choice) {
95 $choice_answers = get_records("choice_answers","choiceid",$choice,"id");
97 if ($choice_answers) {
99 $status =fwrite ($bf,start_tag("ANSWERS",4,true));
100 //Iterate over each answer
101 foreach ($choice_answers as $cho_ans) {
103 $status =fwrite ($bf,start_tag("ANSWER",5,true));
104 //Print answer contents
105 fwrite ($bf,full_tag("ID",6,false,$cho_ans->id
));
106 fwrite ($bf,full_tag("USERID",6,false,$cho_ans->userid
));
107 fwrite ($bf,full_tag("OPTIONID",6,false,$cho_ans->optionid
));
108 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_ans->timemodified
));
110 $status =fwrite ($bf,end_tag("ANSWER",5,true));
113 $status =fwrite ($bf,end_tag("ANSWERS",4,true));
119 //backup choice_options contents (executed from choice_backup_mods)
120 function backup_choice_options ($bf,$preferences,$choice) {
126 $choice_options = get_records("choice_options","choiceid",$choice,"id");
127 //If there is options
128 if ($choice_options) {
130 $status =fwrite ($bf,start_tag("OPTIONS",4,true));
131 //Iterate over each answer
132 foreach ($choice_options as $cho_opt) {
134 $status =fwrite ($bf,start_tag("OPTION",5,true));
135 //Print option contents
136 fwrite ($bf,full_tag("ID",6,false,$cho_opt->id
));
137 fwrite ($bf,full_tag("TEXT",6,false,$cho_opt->text
));
138 fwrite ($bf,full_tag("MAXANSWERS",6,false,$cho_opt->maxanswers
));
139 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_opt->timemodified
));
141 $status =fwrite ($bf,end_tag("OPTION",5,true));
144 $status =fwrite ($bf,end_tag("OPTIONS",4,true));
149 ////Return an array of info (name,value)
150 function choice_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
152 if (!empty($instances) && is_array($instances) && count($instances)) {
154 foreach ($instances as $id => $instance) {
155 $info +
= choice_check_backup_mods_instances($instance,$backup_unique_code);
159 //First the course data
160 $info[0][0] = get_string("modulenameplural","choice");
161 if ($ids = choice_ids ($course)) {
162 $info[0][1] = count($ids);
167 //Now, if requested, the user_data
169 $info[1][0] = get_string("responses","choice");
170 if ($ids = choice_answer_ids_by_course ($course)) {
171 $info[1][1] = count($ids);
179 ////Return an array of info (name,value)
180 function choice_check_backup_mods_instances($instance,$backup_unique_code) {
181 //First the course data
182 $info[$instance->id
.'0'][0] = '<b>'.$instance->name
.'</b>';
183 $info[$instance->id
.'0'][1] = '';
185 //Now, if requested, the user_data
186 if (!empty($instance->userdata
)) {
187 $info[$instance->id
.'1'][0] = get_string("responses","choice");
188 if ($ids = choice_answer_ids_by_instance ($instance->id
)) {
189 $info[$instance->id
.'1'][1] = count($ids);
191 $info[$instance->id
.'1'][1] = 0;
197 //Return a content encoded to support interactivities linking. Every module
198 //should have its own. They are called automatically from the backup procedure.
199 function choice_encode_content_links ($content,$preferences) {
203 $base = preg_quote($CFG->wwwroot
,"/");
205 //Link to the list of choices
206 $buscar="/(".$base."\/mod\/choice\/index.php\?id\=)([0-9]+)/";
207 $result= preg_replace($buscar,'$@CHOICEINDEX*$2@$',$content);
209 //Link to choice view by moduleid
210 $buscar="/(".$base."\/mod\/choice\/view.php\?id\=)([0-9]+)/";
211 $result= preg_replace($buscar,'$@CHOICEVIEWBYID*$2@$',$result);
216 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
218 //Returns an array of choices id
219 function choice_ids ($course) {
223 return get_records_sql ("SELECT a.id, a.course
224 FROM {$CFG->prefix}choice a
225 WHERE a.course = '$course'");
228 //Returns an array of choice_answers id
229 function choice_answer_ids_by_course ($course) {
233 return get_records_sql ("SELECT s.id , s.choiceid
234 FROM {$CFG->prefix}choice_answers s,
235 {$CFG->prefix}choice a
236 WHERE a.course = '$course' AND
240 //Returns an array of choice_answers id
241 function choice_answer_ids_by_instance ($instanceid) {
245 return get_records_sql ("SELECT s.id , s.choiceid
246 FROM {$CFG->prefix}choice_answers s
247 WHERE s.choiceid = $instanceid");