Fix typo.
[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
115 $items = $info['MOD']['#']['ITEMS']['0']['#']['ITEM'];
116 for($i = 0; $i < sizeof($items); $i++) {
117 $item_info = $items[$i];
118 $item->feedback = $newid;
119 $item->template = 0;
120 $item->name = backup_todb($item_info['#']['NAME']['0']['#']);
121 $item->presentation = backup_todb($item_info['#']['PRESENTATION']['0']['#']);
122 $item->presentation = str_replace("\n", '', $item->presentation);
123 if($version >= 1) {
124 $item->typ = backup_todb($item_info['#']['TYP']['0']['#']);
125 $item->hasvalue = backup_todb($item_info['#']['HASVALUE']['0']['#']);
6ee09cfe 126 switch($item->typ) {
127 case 'radio':
128 $item->typ = 'multichoice';
129 $item->presentation = 'r'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
130 break;
131 case 'check':
132 $item->typ = 'multichoice';
133 $item->presentation = 'c'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
134 break;
135 case 'dropdown':
136 $item->typ = 'multichoice';
137 $item->presentation = 'd'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
138 break;
139 case 'radiorated':
140 $item->typ = 'multichoicerated';
141 $item->presentation = 'r'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
142 break;
143 case 'dropdownrated':
144 $item->typ = 'multichoicerated';
145 $item->presentation = 'd'.FEEDBACK_MULTICHOICERESTORE_TYPE_SEP.$item->presentation;
146 break;
147 }
c70ad9f7 148 } else {
149 $oldtyp = intval(backup_todb($item_info['#']['TYP']['0']['#']));
150 switch($oldtyp) {
151 case 0:
152 $item->typ = 'label';
153 $item->hasvalue = 0;
154 break;
155 case 1:
156 $item->typ = 'textfield';
157 $item->hasvalue = 1;
158 break;
159 case 2:
160 $item->typ = 'textarea';
161 $item->hasvalue = 1;
162 break;
163 case 3:
164 $item->typ = 'radio';
165 $item->hasvalue = 1;
166 break;
167 case 4:
168 $item->typ = 'check';
169 $item->hasvalue = 1;
170 break;
171 case 5:
172 $item->typ = 'dropdown';
173 $item->hasvalue = 1;
174 break;
175 }
176 }
177 $item->position = backup_todb($item_info['#']['POSITION']['0']['#']);
178 $item->required = backup_todb($item_info['#']['REQUIRED']['0']['#']);
179 //put this new item into the database
180 $newitemid = insert_record('feedback_item', $item);
181
182 //Now check if want to restore user data and do it.
183 if ($restore_userdata) {
184 $values = $item_info['#']['FBVALUES']['0']['#']['FBVALUE'];
185 for($ii = 0; $ii < sizeof($values); $ii++) {
186 $value_info = $values[$ii];
6ee09cfe 187 $value = new object();
c70ad9f7 188 $value->id = '';
189 $value->item = $newitemid;
190 $value->completed = 0;
191 $value->tmp_completed = backup_todb($value_info['#']['COMPLETED']['0']['#']);
192 $value->value = backup_todb($value_info['#']['VAL']['0']['#']);
193 $value->value = addslashes($value->value);
194 $value->course_id = backup_todb($value_info['#']['COURSE_ID']['0']['#']);
195 //put this new value into the database
196 $newvalueid = insert_record('feedback_value', $value);
197 $value->id = $newvalueid;
6ee09cfe 198 // $allValues[] = $value;
c70ad9f7 199 }
200 }
201 }
202 //Now check if want to restore user data again and do it.
203 if ($restore_userdata) {
204 //restore tracking-data
205 $trackings = $info['MOD']['#']['TRACKINGS']['0']['#']['TRACKING'];
206 for($i = 0; $i < sizeof($trackings); $i++) {
207 $tracking_info = $trackings[$i];
6ee09cfe 208 $tracking = new object();
c70ad9f7 209 $tracking->id = '';
210 $tracking->userid = backup_todb($tracking_info['#']['USERID']['0']['#']); //have to change later
211 $tracking->feedback = $newid;
212 $tracking->completed = backup_todb($tracking_info['#']['COMPLETED']['0']['#']); //have to change later
213 $tracking->count = backup_todb($tracking_info['#']['COUNT']['0']['#']);
214 if($tracking->userid > 0) {
215 //We have to recode the userid field
216 $user = backup_getid($restore->backup_unique_code,"user",$tracking->userid);
217 if ($user) {
218 $tracking->userid = $user->new_id;
219 }
220 }
221
222 //save the tracking
223 $newtrackingid = insert_record('feedback_tracking', $tracking);
224 $tracking->id = $newtrackingid;
6ee09cfe 225 // $allTrackings[] = $tracking;
c70ad9f7 226 }
227
228 //restore completeds
229 $completeds = $info['MOD']['#']['COMPLETEDS']['0']['#']['COMPLETED'];
230 for($i = 0; $i < sizeof($completeds); $i++) {
231 $completed_info = $completeds[$i];
6ee09cfe 232 $completed = new object();
c70ad9f7 233 $completed->feedback = $newid;
234 $completed->userid = backup_todb($completed_info['#']['USERID']['0']['#']);
235 $completed->timemodified = backup_todb($completed_info['#']['TIMEMODIFIED']['0']['#']);
236 $completed->random_response = backup_todb($completed_info['#']['RANDOMRESPONSE']['0']['#']);
237 if(!$anonymous_response = backup_todb($completed_info['#']['ANONYMOUSRESPONSE']['0']['#'])) {
238 $anonymous_response = 1;
239 }
240 $completed->anonymous_response = $anonymous_response;
241 if($completed->userid > 0) {
242 //We have to recode the userid field
243 $user = backup_getid($restore->backup_unique_code,"user",$completed->userid);
244 if ($user) {
245 $completed->userid = $user->new_id;
246 }
247 }
248 //later this have to be changed
249 $oldcompletedid = backup_todb($completed_info['#']['ID']['0']['#']);
250
251 //save the completed
252 $newcompletedid = insert_record('feedback_completed', $completed);
253
254 //the newcompletedid have to be changed at every values
255 $tochangevals = get_records('feedback_value', 'tmp_completed', $oldcompletedid);
256 if($tochangevals) {
257 foreach($tochangevals as $tmpVal) {
258 $tmpVal->completed = $newcompletedid;
259 $tmpVal->tmp_completed = 0;
260 update_record('feedback_value', $tmpVal);
261 }
262 }
263
264 //the newcompletedid have to be changed at every tracking
265 $tochangetracks = get_records('feedback_tracking', 'completed', $oldcompletedid);
266 if($tochangetracks) {
267 foreach($tochangetracks as $tmpTrack) {
268 $tmpTrack->completed = $newcompletedid;
269 $tmpTrack->tmp_completed = 0;
270 update_record('feedback_tracking', $tmpTrack);
271 }
272 }
273 }
274 }
275
276 //We have the newid, update backup_ids
277 backup_putid($restore->backup_unique_code,$mod->modtype, $mod->id, $newid);
278 } else {
279 $status = false;
280 }
281
282 //Finalize ul
283 echo "</ul>";
284
285 } else {
286 $status = false;
287 }
288
289 return $status;
290 }
291
292 //This function returns a log record with all the necessay transformations
293 //done. It's used by restore_log_module() to restore modules log.
294 function feedback_restore_logs($restore,$log) {
295
296 $status = false;
297
298 //Depending of the action, we recode different things
299 switch ($log->action) {
300 case "add":
301 if ($log->cmid) {
302 //Get the new_id of the module (to recode the info field)
303 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
304 if ($mod) {
305 $log->url = "view.php?id=".$log->cmid;
306 $log->info = $mod->new_id;
307 $status = true;
308 }
309 }
310 break;
311 case "update":
312 if ($log->cmid) {
313 //Get the new_id of the module (to recode the info field)
314 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
315 if ($mod) {
316 $log->url = "view.php?id=".$log->cmid;
317 $log->info = $mod->new_id;
318 $status = true;
319 }
320 }
321 break;
322 case "view":
323 if ($log->cmid) {
324 //Get the new_id of the module (to recode the info field)
325 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
326 if ($mod) {
327 $log->url = "view.php?id=".$log->cmid;
328 $log->info = $mod->new_id;
329 $status = true;
330 }
331 }
332 break;
333 case "add entry":
334 if ($log->cmid) {
335 //Get the new_id of the module (to recode the info field)
336 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
337 if ($mod) {
338 $log->url = "view.php?id=".$log->cmid;
339 $log->info = $mod->new_id;
340 $status = true;
341 }
342 }
343 break;
344 case "update entry":
345 if ($log->cmid) {
346 //Get the new_id of the module (to recode the info field)
347 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
348 if ($mod) {
349 $log->url = "view.php?id=".$log->cmid;
350 $log->info = $mod->new_id;
351 $status = true;
352 }
353 }
354 break;
355 case "view responses":
356 if ($log->cmid) {
357 //Get the new_id of the module (to recode the info field)
358 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
359 if ($mod) {
360 $log->url = "report.php?id=".$log->cmid;
361 $log->info = $mod->new_id;
362 $status = true;
363 }
364 }
365 break;
366 case "update feedback":
367 if ($log->cmid) {
368 $log->url = "report.php?id=".$log->cmid;
369 $status = true;
370 }
371 break;
372 case "view all":
373 $log->url = "index.php?id=".$log->course;
374 $status = true;
375 break;
376 default:
377 if (!defined('RESTORE_SILENTLY')) {
378 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
379 }
380 break;
381 }
382
383 if ($status) {
384 $status = $log;
385 }
386 return $status;
387 }
388
389?>