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