MDL-14967 fixed syntax error
[moodle.git] / mod / feedback / restorelib.php
CommitLineData
c70ad9f7 1<?php // $Id$
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
6ee09cfe 29 define('FEEDBACK_MULTICHOICERESTORE_TYPE_SEP', '>>>>>');
30
c70ad9f7 31 function feedback_restore_mods($mod,$restore) {
32
33 global $CFG;
34
6ee09cfe 35 // $allValues = array();
36 // $allTrackings = array();
c70ad9f7 37
38 $status = true;
39 $restore_userdata = restore_userdata_selected($restore,'feedback',$mod->id);
40
41 //Get record from backup_ids
42 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
43 if ($data) {
44 //Now get completed xmlized object
45 $info = $data->info;
46
47 //check of older backupversion of feedback
48 $version = intval(backup_todb($info['MOD']['#']['VERSION']['0']['#']));
49
50 //Now, build the feedback record structure
51 $feedback->course = $restore->course_id;
52 $feedback->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
53 $feedback->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
54 $feedback->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
55 $feedback->email_notification = backup_todb($info['MOD']['#']['EMAILNOTIFICATION']['0']['#']);
56 $feedback->multiple_submit = backup_todb($info['MOD']['#']['MULTIPLESUBMIT']['0']['#']);
efc59167 57 $feedback->autonumbering = backup_todb($info['MOD']['#']['AUTONUMBERING']['0']['#']);
c70ad9f7 58 $feedback->page_after_submit = backup_todb($info['MOD']['#']['PAGEAFTERSUB']['0']['#']);
c184660d 59 $feedback->site_after_submit = backup_todb($info['MOD']['#']['SITEAFTERSUB']['0']['#']);
c70ad9f7 60 $feedback->publish_stats = backup_todb($info['MOD']['#']['PUBLISHSTATS']['0']['#']);
61 $feedback->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
62 $feedback->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
63 $feedback->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
64
65 //The structure is equal to the db, so insert the feedback
66 $newid = insert_record ("feedback",$feedback);
67
68 //create events
69 // the open-event
70 if($feedback->timeopen > 0) {
71 $event = NULL;
72 $event->name = get_string('start', 'feedback').' '.$feedback->name;
73 $event->description = $feedback->summary;
74 $event->courseid = $feedback->course;
75 $event->groupid = 0;
76 $event->userid = 0;
77 $event->modulename = 'feedback';
78 $event->instance = $newid;
79 $event->eventtype = 'open';
80 $event->timestart = $feedback->timeopen;
81 $event->visible = instance_is_visible('feedback', $feedback);
82 if($feedback->timeclose > 0) {
83 $event->timeduration = ($feedback->timeclose - $feedback->timeopen);
84 } else {
85 $event->timeduration = 0;
86 }
87
88 add_event($event);
89 }
90
91 // the close-event
92 if($feedback->timeclose > 0) {
93 $event = NULL;
94 $event->name = get_string('stop', 'feedback').' '.$feedback->name;
95 $event->description = $feedback->summary;
96 $event->courseid = $feedback->course;
97 $event->groupid = 0;
98 $event->userid = 0;
99 $event->modulename = 'feedback';
100 $event->instance = $newid;
101 $event->eventtype = 'close';
102 $event->timestart = $feedback->timeclose;
103 $event->visible = instance_is_visible('feedback', $feedback);
104 $event->timeduration = 0;
105
106 add_event($event);
107 }
108
109 //Do some output
110 echo "<ul><li>".get_string("modulename","feedback")." \"".$feedback->name."\"<br />";
111 backup_flush(300);
112
113 if ($newid) {
114 //Now, build the feedback_item record structure
239ba4ee 115 if(isset($info['MOD']['#']['ITEMS']['0']['#']['ITEM'])) {
116 $items = $info['MOD']['#']['ITEMS']['0']['#']['ITEM'];
117 for($i = 0; $i < sizeof($items); $i++) {
118 $item_info = $items[$i];
119 $item->feedback = $newid;
120 $item->template = 0;
121 $item->name = backup_todb($item_info['#']['NAME']['0']['#']);
122 $item->presentation = backup_todb($item_info['#']['PRESENTATION']['0']['#']);
123 $item->presentation = str_replace("\n", '', $item->presentation);
124 if($version >= 1) {
125 $item->typ = backup_todb($item_info['#']['TYP']['0']['#']);
126 $item->hasvalue = backup_todb($item_info['#']['HASVALUE']['0']['#']);
127 switch($item->typ) {
128 case 'radio':
129 $item->typ = 'multichoice';
130 $item->presentation = 'r'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
131 break;
132 case 'check':
133 $item->typ = 'multichoice';
134 $item->presentation = 'c'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
135 break;
136 case 'dropdown':
137 $item->typ = 'multichoice';
138 $item->presentation = 'd'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
139 break;
140 case 'radiorated':
141 $item->typ = 'multichoicerated';
142 $item->presentation = 'r'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
143 break;
144 case 'dropdownrated':
145 $item->typ = 'multichoicerated';
146 $item->presentation = 'd'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
147 break;
148 }
149 } else {
150 $oldtyp = intval(backup_todb($item_info['#']['TYP']['0']['#']));
151 switch($oldtyp) {
152 case 0:
153 $item->typ = 'label';
154 $item->hasvalue = 0;
155 break;
156 case 1:
157 $item->typ = 'textfield';
158 $item->hasvalue = 1;
159 break;
160 case 2:
161 $item->typ = 'textarea';
162 $item->hasvalue = 1;
163 break;
164 case 3:
165 $item->typ = 'radio';
166 $item->hasvalue = 1;
167 break;
168 case 4:
169 $item->typ = 'check';
170 $item->hasvalue = 1;
171 break;
172 case 5:
173 $item->typ = 'dropdown';
174 $item->hasvalue = 1;
175 break;
176 }
c70ad9f7 177 }
239ba4ee 178 $item->position = backup_todb($item_info['#']['POSITION']['0']['#']);
179 $item->required = backup_todb($item_info['#']['REQUIRED']['0']['#']);
180 //put this new item into the database
181 $newitemid = insert_record('feedback_item', $item);
c70ad9f7 182
239ba4ee 183 //Now check if want to restore user data and do it.
184 if ($restore_userdata) {
185 if(isset($item_info['#']['FBVALUES']['0']['#']['FBVALUE'])) {
186 $values = $item_info['#']['FBVALUES']['0']['#']['FBVALUE'];
187 for($ii = 0; $ii < sizeof($values); $ii++) {
188 $value_info = $values[$ii];
189 $value = new object();
190 $value->id = '';
191 $value->item = $newitemid;
192 $value->completed = 0;
193 $value->tmp_completed = backup_todb($value_info['#']['COMPLETED']['0']['#']);
194 $value->value = backup_todb($value_info['#']['VAL']['0']['#']);
195 $value->value = addslashes($value->value);
196 $value->course_id = backup_todb($value_info['#']['COURSE_ID']['0']['#']);
197 //put this new value into the database
198 $newvalueid = insert_record('feedback_value', $value);
199 $value->id = $newvalueid;
200 // $allValues[] = $value;
201 }
202 }
c70ad9f7 203 }
204 }
205 }
206 //Now check if want to restore user data again and do it.
207 if ($restore_userdata) {
208 //restore tracking-data
239ba4ee 209 if(isset($info['MOD']['#']['TRACKINGS']['0']['#']['TRACKING'])) {
210 $trackings = $info['MOD']['#']['TRACKINGS']['0']['#']['TRACKING'];
211 for($i = 0; $i < sizeof($trackings); $i++) {
212 $tracking_info = $trackings[$i];
213 $tracking = new object();
214 $tracking->id = '';
215 $tracking->userid = backup_todb($tracking_info['#']['USERID']['0']['#']); //have to change later
216 $tracking->feedback = $newid;
217 $tracking->completed = backup_todb($tracking_info['#']['COMPLETED']['0']['#']); //have to change later
218 if($tracking->userid > 0) {
219 //We have to recode the userid field
220 $user = backup_getid($restore->backup_unique_code,"user",$tracking->userid);
221 if ($user) {
222 $tracking->userid = $user->new_id;
223 }
c70ad9f7 224 }
239ba4ee 225
226 //save the tracking
227 $newtrackingid = insert_record('feedback_tracking', $tracking);
228 $tracking->id = $newtrackingid;
229 // $allTrackings[] = $tracking;
c70ad9f7 230 }
c70ad9f7 231 }
232
233 //restore completeds
239ba4ee 234 if(isset($info['MOD']['#']['COMPLETEDS']['0']['#']['COMPLETED'])) {
235 $completeds = $info['MOD']['#']['COMPLETEDS']['0']['#']['COMPLETED'];
236 for($i = 0; $i < sizeof($completeds); $i++) {
237 $completed_info = $completeds[$i];
238 $completed = new object();
239 $completed->feedback = $newid;
240 $completed->userid = backup_todb($completed_info['#']['USERID']['0']['#']);
241 $completed->timemodified = backup_todb($completed_info['#']['TIMEMODIFIED']['0']['#']);
242 $completed->random_response = backup_todb($completed_info['#']['RANDOMRESPONSE']['0']['#']);
243 if(!$anonymous_response = backup_todb($completed_info['#']['ANONYMOUSRESPONSE']['0']['#'])) {
244 $anonymous_response = 1;
c70ad9f7 245 }
239ba4ee 246 $completed->anonymous_response = $anonymous_response;
247 if($completed->userid > 0) {
248 //We have to recode the userid field
249 $user = backup_getid($restore->backup_unique_code,"user",$completed->userid);
250 if ($user) {
251 $completed->userid = $user->new_id;
252 }
c70ad9f7 253 }
239ba4ee 254 //later this have to be changed
255 $oldcompletedid = backup_todb($completed_info['#']['ID']['0']['#']);
256
257 //save the completed
258 $newcompletedid = insert_record('feedback_completed', $completed);
259
260 //the newcompletedid have to be changed at every values
261 $tochangevals = get_records('feedback_value', 'tmp_completed', $oldcompletedid);
262 if($tochangevals) {
263 foreach($tochangevals as $tmpVal) {
264 $tmpVal->completed = $newcompletedid;
265 $tmpVal->tmp_completed = 0;
266 update_record('feedback_value', $tmpVal);
267 }
268 }
269
270 //the newcompletedid have to be changed at every tracking
271 $tochangetracks = get_records('feedback_tracking', 'completed', $oldcompletedid);
272 if($tochangetracks) {
273 foreach($tochangetracks as $tmpTrack) {
274 $tmpTrack->completed = $newcompletedid;
275 $tmpTrack->tmp_completed = 0;
276 update_record('feedback_tracking', $tmpTrack);
277 }
c70ad9f7 278 }
279 }
280 }
281 }
282
283 //We have the newid, update backup_ids
284 backup_putid($restore->backup_unique_code,$mod->modtype, $mod->id, $newid);
285 } else {
286 $status = false;
287 }
288
289 //Finalize ul
290 echo "</ul>";
291
292 } else {
293 $status = false;
294 }
295
296 return $status;
297 }
298
299 //This function returns a log record with all the necessay transformations
300 //done. It's used by restore_log_module() to restore modules log.
301 function feedback_restore_logs($restore,$log) {
302
303 $status = false;
304
305 //Depending of the action, we recode different things
306 switch ($log->action) {
307 case "add":
308 if ($log->cmid) {
309 //Get the new_id of the module (to recode the info field)
310 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
311 if ($mod) {
312 $log->url = "view.php?id=".$log->cmid;
313 $log->info = $mod->new_id;
314 $status = true;
315 }
316 }
317 break;
318 case "update":
319 if ($log->cmid) {
320 //Get the new_id of the module (to recode the info field)
321 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
322 if ($mod) {
323 $log->url = "view.php?id=".$log->cmid;
324 $log->info = $mod->new_id;
325 $status = true;
326 }
327 }
328 break;
329 case "view":
330 if ($log->cmid) {
331 //Get the new_id of the module (to recode the info field)
332 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
333 if ($mod) {
334 $log->url = "view.php?id=".$log->cmid;
335 $log->info = $mod->new_id;
336 $status = true;
337 }
338 }
339 break;
340 case "add entry":
341 if ($log->cmid) {
342 //Get the new_id of the module (to recode the info field)
343 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
344 if ($mod) {
345 $log->url = "view.php?id=".$log->cmid;
346 $log->info = $mod->new_id;
347 $status = true;
348 }
349 }
350 break;
351 case "update entry":
352 if ($log->cmid) {
353 //Get the new_id of the module (to recode the info field)
354 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
355 if ($mod) {
356 $log->url = "view.php?id=".$log->cmid;
357 $log->info = $mod->new_id;
358 $status = true;
359 }
360 }
361 break;
362 case "view responses":
363 if ($log->cmid) {
364 //Get the new_id of the module (to recode the info field)
365 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
366 if ($mod) {
367 $log->url = "report.php?id=".$log->cmid;
368 $log->info = $mod->new_id;
369 $status = true;
370 }
371 }
372 break;
373 case "update feedback":
374 if ($log->cmid) {
375 $log->url = "report.php?id=".$log->cmid;
376 $status = true;
377 }
378 break;
379 case "view all":
380 $log->url = "index.php?id=".$log->course;
381 $status = true;
382 break;
383 default:
384 if (!defined('RESTORE_SILENTLY')) {
385 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
386 }
387 break;
388 }
389
390 if ($status) {
391 $status = $log;
392 }
393 return $status;
394 }
395
396?>