MDL-21695 question: replaced help files with new help strings
[moodle.git] / mod / feedback / backuplib.php
CommitLineData
1adbd2c3 1<?php
c70ad9f7 2 //This php script contains all the stuff to backup/restore
3 //feedback mods
4
5 //This is the "graphical" structure of the feedback mod:
6 //
7 // feedback---------------------------------feedback_tracking
8 // (CL,pk->id) (UL, pk->id, fk->feedback,completed)
9 // | |
10 // | |
11 // | |
12 // feedback_template feedback_completed
13 // (CL,pk->id) (UL, pk->id, fk->feedback)
14 // | |
15 // | |
16 // | |
17 // feedback_item---------------------------------feedback_value
18 // (ML,pk->id, fk->feedback, fk->template) (UL, pk->id, fk->item, fk->completed)
19 //
20 // Meaning: pk->primary key field of the table
21 // fk->foreign key to link with parent
22 // CL->course level info
23 // ML->modul level info
24 // UL->userid level info
25 // message->text of each feedback_posting
26 //
27 //-----------------------------------------------------------
28
29 //This function executes all the backup procedure about this mod
30 function feedback_backup_mods($bf,$preferences) {
0085fff8 31 global $CFG, $DB;
c70ad9f7 32
33 $status = true;
34
35 //Iterate over feedback table
0085fff8 36 $feedbacks = $DB->get_records ("feedback", array("course"=>$preferences->backup_course), "id");
c70ad9f7 37 if ($feedbacks) {
38 foreach ($feedbacks as $feedback) {
39 if (backup_mod_selected($preferences,'feedback',$feedback->id)) {
40 $status = feedback_backup_one_mod($bf,$preferences,$feedback);
41 }
42 }
43 }
1adbd2c3 44 return $status;
c70ad9f7 45 }
46
47 function feedback_backup_one_mod($bf,$preferences,$feedback) {
0085fff8 48 global $CFG, $DB;
1adbd2c3 49
c70ad9f7 50 if (is_numeric($feedback)) {
0085fff8 51 $feedback = $DB->get_record('feedback', array('id'=>$feedback));
c70ad9f7 52 }
1adbd2c3 53
c70ad9f7 54 $status = true;
55 fwrite ($bf,start_tag("MOD",3,true));
56 //Print feedback data
57 fwrite ($bf,full_tag("ID",4,false,$feedback->id));
58 fwrite ($bf,full_tag("MODTYPE",4,false,"feedback"));
59 fwrite ($bf,full_tag("VERSION",4,false,1)); //version 1 steht fuer die neue Version
60 fwrite ($bf,full_tag("NAME",4,false,$feedback->name));
63b02567 61 fwrite ($bf,full_tag("SUMMARY",4,false,$feedback->intro));
c70ad9f7 62 fwrite ($bf,full_tag("ANONYMOUS",4,false,$feedback->anonymous));
63 fwrite ($bf,full_tag("EMAILNOTIFICATION",4,false,$feedback->email_notification));
64 fwrite ($bf,full_tag("MULTIPLESUBMIT",4,false,$feedback->multiple_submit));
efc59167 65 fwrite ($bf,full_tag("AUTONUMBERING",4,false,$feedback->autonumbering));
c184660d 66 fwrite ($bf,full_tag("SITEAFTERSUB",4,false,$feedback->site_after_submit));
c70ad9f7 67 fwrite ($bf,full_tag("PAGEAFTERSUB",4,false,$feedback->page_after_submit));
68 fwrite ($bf,full_tag("PUBLISHSTATS",4,false,$feedback->publish_stats));
69 fwrite ($bf,full_tag("TIMEOPEN",4,false,$feedback->timeopen));
70 fwrite ($bf,full_tag("TIMECLOSE",4,false,$feedback->timeclose));
71 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$feedback->timemodified));
1adbd2c3 72
c70ad9f7 73 //backup the items of each feedback
74 feedback_backup_data($bf, $preferences, $feedback->id);
1adbd2c3 75
c70ad9f7 76 //End mod
77 $status =fwrite ($bf,end_tag("MOD",3,true));
78 return $status;
79 }
1adbd2c3 80
c70ad9f7 81 function feedback_backup_data($bf, $preferences, $feedbackid) {
0085fff8 82 global $CFG, $DB;
83
c70ad9f7 84 $status = true;
0085fff8 85 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedbackid));
c70ad9f7 86 if(function_exists('backup_userdata_selected')) { //compatibility-hack for moodle 1.5.x
87 $backup_userdata = backup_userdata_selected($preferences,'feedback',$feedbackid);
88 }else {
89 $backup_userdata = $preferences->mods["feedback"]->userinfo;
90 }
91
92 if ($feedbackitems) {
93 $status =fwrite ($bf,start_tag("ITEMS",4,true));
94 foreach ($feedbackitems as $feedbackitem) {
95 //Start item
96 fwrite ($bf,start_tag("ITEM",5,true));
97 //Print item data
98 fwrite ($bf,full_tag("ID",6,false,$feedbackitem->id));
99 fwrite ($bf,full_tag("NAME",6,false,$feedbackitem->name));
100 fwrite ($bf,full_tag("PRESENTATION",6,false,$feedbackitem->presentation));
101 fwrite ($bf,full_tag("TYP",6,false,$feedbackitem->typ));
102 fwrite ($bf,full_tag("HASVALUE",6,false,$feedbackitem->hasvalue));
103 fwrite ($bf,full_tag("POSITION",6,false,$feedbackitem->position));
104 fwrite ($bf,full_tag("REQUIRED",6,false,$feedbackitem->required));
1adbd2c3 105
c70ad9f7 106 if ($backup_userdata) {
107 //backup the values of items
0085fff8 108 $feedbackvalues = $DB->get_records('feedback_value', array('item'=>$feedbackitem->id));
c70ad9f7 109 if($feedbackvalues) {
110 $status =fwrite ($bf,start_tag("FBVALUES",6,true));
111 foreach($feedbackvalues as $feedbackvalue) {
112 //start value
113 fwrite ($bf,start_tag("FBVALUE",7,true));
114 //print value data
115 fwrite ($bf,full_tag("ID",8,false,$feedbackvalue->id));
116 fwrite ($bf,full_tag("ITEM",8,false,$feedbackvalue->item));
117 fwrite ($bf,full_tag("COMPLETED",8,false,$feedbackvalue->completed));
118 fwrite ($bf,full_tag("VAL",8,false,$feedbackvalue->value));
119 fwrite ($bf,full_tag("COURSE_ID",8,false,$feedbackvalue->course_id));
120 //End value
121 $status =fwrite ($bf,end_tag("FBVALUE",7,true));
122 }
123 $status =fwrite ($bf,end_tag("FBVALUES",6,true));
124 }
125 }
126 //End item
127 $status =fwrite ($bf,end_tag("ITEM",5,true));
128 }
129 $status =fwrite ($bf,end_tag("ITEMS",4,true));
130 }
1adbd2c3 131
c70ad9f7 132 if($backup_userdata) {
133 //backup of feedback-completeds
0085fff8 134 $feedbackcompleteds = $DB->get_records('feedback_completed', array('feedback'=>$feedbackid));
c70ad9f7 135 if($feedbackcompleteds) {
136 fwrite ($bf,start_tag("COMPLETEDS",4,true));
137 foreach ($feedbackcompleteds as $feedbackcompleted) {
138 //Start completed
139 fwrite ($bf,start_tag("COMPLETED",5,true));
140 //Print completed data
141 fwrite ($bf,full_tag("ID",6,false,$feedbackcompleted->id));
142 fwrite ($bf,full_tag("FEEDBACK",6,false,$feedbackcompleted->feedback));
143 fwrite ($bf,full_tag("USERID",6,false,$feedbackcompleted->userid));
144 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$feedbackcompleted->timemodified));
145 fwrite ($bf,full_tag("RANDOMRESPONSE",6,false,$feedbackcompleted->random_response));
146 fwrite ($bf,full_tag("ANONYMOUSRESPONSE",6,false,$feedbackcompleted->anonymous_response));
1adbd2c3 147
c70ad9f7 148 //End completed
149 $status =fwrite ($bf,end_tag("COMPLETED",5,true));
150 }
151 $status =fwrite ($bf,end_tag("COMPLETEDS",4,true));
152 }
1adbd2c3 153
c70ad9f7 154 //backup of tracking-data
0085fff8 155 $feedbacktrackings = $DB->get_records('feedback_tracking', array('feedback'=>$feedbackid));
c70ad9f7 156 if($feedbacktrackings) {
157 fwrite ($bf,start_tag("TRACKINGS",4,true));
158 foreach ($feedbacktrackings as $feedbacktracking) {
159 //Start tracking
160 fwrite ($bf,start_tag("TRACKING",5,true));
161 //Print tracking data
162 fwrite ($bf,full_tag("ID",6,false,$feedbacktracking->id));
163 fwrite ($bf,full_tag("USERID",6,false,$feedbacktracking->userid));
164 fwrite ($bf,full_tag("FEEDBACK",6,false,$feedbacktracking->feedback));
165 fwrite ($bf,full_tag("COMPLETED",6,false,$feedbacktracking->completed));
1adbd2c3 166
c70ad9f7 167 //End completed
168 $status =fwrite ($bf,end_tag("TRACKING",5,true));
169 }
170 $status =fwrite ($bf,end_tag("TRACKINGS",4,true));
171 }
1adbd2c3 172
c70ad9f7 173 }
174
175 }
176
177
178 function feedback_backup_template_data($bf, $templateid, $userinfo) {
0085fff8 179 global $CFG, $DB;
180
c70ad9f7 181 $status = true;
0085fff8 182 $templateitems = $DB->get_records('feedback_item', array('template'=>$templateid));
c70ad9f7 183
184 if ($templateitems) {
185 $status =fwrite ($bf,start_tag("ITEMS",5,true));
186 foreach ($templateitems as $templateitem) {
187 //Start item
188 fwrite ($bf,start_tag("ITEM",6,true));
189 //Print item data
190 fwrite ($bf,full_tag("ID",7,false,$templateitem->id));
191 fwrite ($bf,full_tag("NAME",7,false,$templateitem->name));
192 fwrite ($bf,full_tag("PRESENTATION",7,false,$templateitem->presentation));
193 fwrite ($bf,full_tag("TYP",7,false,$templateitem->typ));
194 fwrite ($bf,full_tag("HASVALUE",7,false,$templateitem->hasvalue));
195 fwrite ($bf,full_tag("POSITION",7,false,$templateitem->position));
196 fwrite ($bf,full_tag("REQUIRED",7,false,$templateitem->required));
1adbd2c3 197
c70ad9f7 198 //End item
199 $status =fwrite ($bf,end_tag("ITEM",6,true));
200 }
201 $status =fwrite ($bf,end_tag("ITEMS",5,true));
202 }
203 }
204
205
206
207
208 //Return an array of info (name,value)
209 function feedback_check_backup_mods($course,$user_data=false,$backup_unique_code, $instances=null) {
210 if (!empty($instances) && is_array($instances) && count($instances)) {
211 $info = array();
212 foreach ($instances as $id => $instance) {
213 $info += feedback_check_backup_mods_instances($instance,$backup_unique_code);
214 }
215 return $info;
216 }
217 //First the course data
218 $info[0][0] = get_string("modulenameplural","feedback");
219 $info[0][1] = feedback_count($course);
1adbd2c3 220
c70ad9f7 221 //Now, if requested, the user_data
1adbd2c3 222
c70ad9f7 223 if ($user_data) {
224 $info[1][0] = get_string('ready_feedbacks','feedback');
225 $info[1][1] = feedback_completed_count($course);
226 }
1adbd2c3 227
c70ad9f7 228 return $info;
229 }
230
231 ////Return an array of info (name,value)
232 function feedback_check_backup_mods_instances($instance,$backup_unique_code) {
0085fff8 233 global $DB;
234
c70ad9f7 235 //First the course data
236 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
237 $info[$instance->id.'0'][1] = '';
238
239 //Now, if requested, the user_data
240 if (!empty($instance->userdata)) {
241 $info[$instance->id.'1'][0] = get_string("responses","feedback");
0085fff8 242 if ($responses_count = $DB->count_records ('feedback_completed', array('feedback'=>$instance->id))) {
c70ad9f7 243 $info[$instance->id.'1'][1] = $responses_count;
244 } else {
245 $info[$instance->id.'1'][1] = 0;
246 }
247 }
248 return $info;
249 }
250
251///////////////////////////////////////////////////////////////
252///////////////////////////////////////////////////////////////
253///////////////////////////////////////////////////////////////
254//// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
255
1adbd2c3 256 //Returns an array of feedbacks ids
c70ad9f7 257 function feedback_count ($course) {
0085fff8 258 global $DB;
259
260 return $DB->count_records('feedback', array('course'=>$course));
c70ad9f7 261 }
1adbd2c3 262
c70ad9f7 263 function feedback_completed_count($course) {
0085fff8 264 global $DB;
265
c70ad9f7 266 $count = 0;
267 //get all feedbacks
0085fff8 268 $feedbacks = $DB->get_records('feedback', array('course'=>$course));
c70ad9f7 269 if($feedbacks) {
270 foreach($feedbacks as $feedback) {
0085fff8 271 $count += $DB->count_records('feedback_completed', array('feedback'=>$feedback->id));
c70ad9f7 272 }
273 }
274 return $count;
275 }
1adbd2c3
PS
276
277