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