MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / choice / backuplib.php
blobbbbb63ae9ff04c884d5613c371d4f7dc663d006d
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //choice mods
5 //This is the "graphical" structure of the choice mod:
6 //
7 // choice
8 // (CL,pk->id)----------|
9 // | |
10 // | |
11 // | |
12 // choice_options |
13 // (UL,pk->id, fk->choiceid) |
14 // | |
15 // | |
16 // | |
17 // choice_answers |
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) {
31 global $CFG;
33 $status = true;
35 //Iterate over choice table
36 $choices = get_records ("choice","course",$preferences->backup_course,"id");
37 if ($choices) {
38 foreach ($choices as $choice) {
39 if (backup_mod_selected($preferences,'choice',$choice->id)) {
40 $status = choice_backup_one_mod($bf,$preferences,$choice);
44 return $status;
47 function choice_backup_one_mod($bf,$preferences,$choice) {
49 global $CFG;
51 if (is_numeric($choice)) {
52 $choice = get_record('choice','id',$choice);
55 $status = true;
57 //Start mod
58 fwrite ($bf,start_tag("MOD",3,true));
59 //Print choice data
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);
82 //End mod
83 $status =fwrite ($bf,end_tag("MOD",3,true));
85 return $status;
88 //Backup choice_answers contents (executed from choice_backup_mods)
89 function backup_choice_answers ($bf,$preferences,$choice) {
91 global $CFG;
93 $status = true;
95 $choice_answers = get_records("choice_answers","choiceid",$choice,"id");
96 //If there is answers
97 if ($choice_answers) {
98 //Write start tag
99 $status =fwrite ($bf,start_tag("ANSWERS",4,true));
100 //Iterate over each answer
101 foreach ($choice_answers as $cho_ans) {
102 //Start answer
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));
109 //End answer
110 $status =fwrite ($bf,end_tag("ANSWER",5,true));
112 //Write end tag
113 $status =fwrite ($bf,end_tag("ANSWERS",4,true));
115 return $status;
119 //backup choice_options contents (executed from choice_backup_mods)
120 function backup_choice_options ($bf,$preferences,$choice) {
122 global $CFG;
124 $status = true;
126 $choice_options = get_records("choice_options","choiceid",$choice,"id");
127 //If there is options
128 if ($choice_options) {
129 //Write start tag
130 $status =fwrite ($bf,start_tag("OPTIONS",4,true));
131 //Iterate over each answer
132 foreach ($choice_options as $cho_opt) {
133 //Start option
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));
140 //End answer
141 $status =fwrite ($bf,end_tag("OPTION",5,true));
143 //Write end tag
144 $status =fwrite ($bf,end_tag("OPTIONS",4,true));
146 return $status;
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)) {
153 $info = array();
154 foreach ($instances as $id => $instance) {
155 $info += choice_check_backup_mods_instances($instance,$backup_unique_code);
157 return $info;
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);
163 } else {
164 $info[0][1] = 0;
167 //Now, if requested, the user_data
168 if ($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);
172 } else {
173 $info[1][1] = 0;
176 return $info;
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);
190 } else {
191 $info[$instance->id.'1'][1] = 0;
194 return $info;
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) {
201 global $CFG;
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);
213 return $result;
216 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
218 //Returns an array of choices id
219 function choice_ids ($course) {
221 global $CFG;
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) {
231 global $CFG;
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
237 s.choiceid = a.id");
240 //Returns an array of choice_answers id
241 function choice_answer_ids_by_instance ($instanceid) {
243 global $CFG;
245 return get_records_sql ("SELECT s.id , s.choiceid
246 FROM {$CFG->prefix}choice_answers s
247 WHERE s.choiceid = $instanceid");