fixed issue MDL-14531
[moodle.git] / mod / feedback / backuplib.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     //-----------------------------------------------------------
29     //This function executes all the backup procedure about this mod
30    function feedback_backup_mods($bf,$preferences) {
31       global $CFG;
33       $status = true;
35       //Iterate over feedback table
36       $feedbacks = get_records ("feedback","course",$preferences->backup_course,"id");
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       }
44       return $status;  
45    }
47    function feedback_backup_one_mod($bf,$preferences,$feedback) {
48       global $CFG;
49     
50       if (is_numeric($feedback)) {
51          $feedback = get_record('feedback','id',$feedback);
52       }
53       
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));
61       fwrite ($bf,full_tag("SUMMARY",4,false,$feedback->summary));
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));
65       fwrite ($bf,full_tag("AUTONUMBERING",4,false,$feedback->autonumbering));
66       fwrite ($bf,full_tag("PAGEAFTERSUB",4,false,$feedback->page_after_submit));
67       fwrite ($bf,full_tag("PUBLISHSTATS",4,false,$feedback->publish_stats));
68       fwrite ($bf,full_tag("TIMEOPEN",4,false,$feedback->timeopen));
69       fwrite ($bf,full_tag("TIMECLOSE",4,false,$feedback->timeclose));
70       fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$feedback->timemodified));
71        
72       //backup the items of each feedback
73       feedback_backup_data($bf, $preferences, $feedback->id);
74        
75       //End mod
76       $status =fwrite ($bf,end_tag("MOD",3,true));
77       return $status;
78    }
79    
80    function feedback_backup_data($bf, $preferences, $feedbackid) {
81       global $CFG;
82       $status = true;
83       $feedbackitems = get_records('feedback_item', 'feedback', $feedbackid);
84       if(function_exists('backup_userdata_selected')) { //compatibility-hack for moodle 1.5.x
85          $backup_userdata = backup_userdata_selected($preferences,'feedback',$feedbackid);
86       }else {
87          $backup_userdata = $preferences->mods["feedback"]->userinfo;
88       }
90       if ($feedbackitems) {
91          $status =fwrite ($bf,start_tag("ITEMS",4,true));
92          foreach ($feedbackitems as $feedbackitem) {
93             //Start item
94             fwrite ($bf,start_tag("ITEM",5,true));
95             //Print item data
96             fwrite ($bf,full_tag("ID",6,false,$feedbackitem->id));
97             fwrite ($bf,full_tag("NAME",6,false,$feedbackitem->name));
98             fwrite ($bf,full_tag("PRESENTATION",6,false,$feedbackitem->presentation));
99             fwrite ($bf,full_tag("TYP",6,false,$feedbackitem->typ));
100             fwrite ($bf,full_tag("HASVALUE",6,false,$feedbackitem->hasvalue));
101             fwrite ($bf,full_tag("POSITION",6,false,$feedbackitem->position));
102             fwrite ($bf,full_tag("REQUIRED",6,false,$feedbackitem->required));
103             
104             if ($backup_userdata) {
105                //backup the values of items
106                $feedbackvalues = get_records('feedback_value', 'item', $feedbackitem->id);
107                if($feedbackvalues) {
108                   $status =fwrite ($bf,start_tag("FBVALUES",6,true));
109                   foreach($feedbackvalues as $feedbackvalue) {
110                      //start value
111                      fwrite ($bf,start_tag("FBVALUE",7,true));
112                      //print value data
113                      fwrite ($bf,full_tag("ID",8,false,$feedbackvalue->id));
114                      fwrite ($bf,full_tag("ITEM",8,false,$feedbackvalue->item));
115                      fwrite ($bf,full_tag("COMPLETED",8,false,$feedbackvalue->completed));
116                      fwrite ($bf,full_tag("VAL",8,false,$feedbackvalue->value));
117                      fwrite ($bf,full_tag("COURSE_ID",8,false,$feedbackvalue->course_id));
118                      //End value
119                      $status =fwrite ($bf,end_tag("FBVALUE",7,true));
120                   }
121                   $status =fwrite ($bf,end_tag("FBVALUES",6,true));
122                }
123             }
124             //End item
125             $status =fwrite ($bf,end_tag("ITEM",5,true));
126          }
127          $status =fwrite ($bf,end_tag("ITEMS",4,true));
128       }
129       
130       if($backup_userdata) {
131          //backup of feedback-completeds
132          $feedbackcompleteds = get_records('feedback_completed', 'feedback', $feedbackid);
133          if($feedbackcompleteds) {
134             fwrite ($bf,start_tag("COMPLETEDS",4,true));
135             foreach ($feedbackcompleteds as $feedbackcompleted) {
136                //Start completed
137                fwrite ($bf,start_tag("COMPLETED",5,true));
138                //Print completed data
139                fwrite ($bf,full_tag("ID",6,false,$feedbackcompleted->id));
140                fwrite ($bf,full_tag("FEEDBACK",6,false,$feedbackcompleted->feedback));
141                fwrite ($bf,full_tag("USERID",6,false,$feedbackcompleted->userid));
142                fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$feedbackcompleted->timemodified));
143                fwrite ($bf,full_tag("RANDOMRESPONSE",6,false,$feedbackcompleted->random_response));
144                fwrite ($bf,full_tag("ANONYMOUSRESPONSE",6,false,$feedbackcompleted->anonymous_response));
145                
146                //End completed
147                $status =fwrite ($bf,end_tag("COMPLETED",5,true));
148             }
149             $status =fwrite ($bf,end_tag("COMPLETEDS",4,true));
150          }
151          
152          //backup of tracking-data
153          $feedbacktrackings = get_records('feedback_tracking', 'feedback', $feedbackid);
154          if($feedbacktrackings) {
155             fwrite ($bf,start_tag("TRACKINGS",4,true));
156             foreach ($feedbacktrackings as $feedbacktracking) {
157                //Start tracking
158                fwrite ($bf,start_tag("TRACKING",5,true));
159                //Print tracking data
160                fwrite ($bf,full_tag("ID",6,false,$feedbacktracking->id));
161                fwrite ($bf,full_tag("USERID",6,false,$feedbacktracking->userid));
162                fwrite ($bf,full_tag("FEEDBACK",6,false,$feedbacktracking->feedback));
163                fwrite ($bf,full_tag("COMPLETED",6,false,$feedbacktracking->completed));
164                fwrite ($bf,full_tag("COUNT",6,false,$feedbacktracking->count));
165                
166                //End completed
167                $status =fwrite ($bf,end_tag("TRACKING",5,true));
168             }
169             $status =fwrite ($bf,end_tag("TRACKINGS",4,true));
170          }
171          
172       }
174    }
177    function feedback_backup_template_data($bf, $templateid, $userinfo) {
178       global $CFG;
179       $status = true;
180       $templateitems = get_records('feedback_item', 'template', $templateid);
182       if ($templateitems) {
183          $status =fwrite ($bf,start_tag("ITEMS",5,true));
184          foreach ($templateitems as $templateitem) {
185             //Start item
186             fwrite ($bf,start_tag("ITEM",6,true));
187             //Print item data
188             fwrite ($bf,full_tag("ID",7,false,$templateitem->id));
189             fwrite ($bf,full_tag("NAME",7,false,$templateitem->name));
190             fwrite ($bf,full_tag("PRESENTATION",7,false,$templateitem->presentation));
191             fwrite ($bf,full_tag("TYP",7,false,$templateitem->typ));
192             fwrite ($bf,full_tag("HASVALUE",7,false,$templateitem->hasvalue));
193             fwrite ($bf,full_tag("POSITION",7,false,$templateitem->position));
194             fwrite ($bf,full_tag("REQUIRED",7,false,$templateitem->required));
195             
196             //End item
197             $status =fwrite ($bf,end_tag("ITEM",6,true));
198          }
199          $status =fwrite ($bf,end_tag("ITEMS",5,true));
200       }
201    }
206    //Return an array of info (name,value)
207    function feedback_check_backup_mods($course,$user_data=false,$backup_unique_code, $instances=null) {
208       if (!empty($instances) && is_array($instances) && count($instances)) {
209          $info = array();
210          foreach ($instances as $id => $instance) {
211             $info += feedback_check_backup_mods_instances($instance,$backup_unique_code);
212          }
213          return $info;
214       }
215       //First the course data
216       $info[0][0] = get_string("modulenameplural","feedback");
217       $info[0][1] = feedback_count($course);
218       
219       //Now, if requested, the user_data
220       
221       if ($user_data) {
222          $info[1][0] = get_string('ready_feedbacks','feedback');
223          $info[1][1] = feedback_completed_count($course);
224       }
225       
226       return $info;
227    }
229    ////Return an array of info (name,value)
230    function feedback_check_backup_mods_instances($instance,$backup_unique_code) {
231       //First the course data
232       $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
233       $info[$instance->id.'0'][1] = '';
235       //Now, if requested, the user_data
236       if (!empty($instance->userdata)) {
237          $info[$instance->id.'1'][0] = get_string("responses","feedback");
238          if ($responses_count = count_records ('feedback_completed', 'feedback', $instance->id)) {
239                $info[$instance->id.'1'][1] = $responses_count;
240          } else {
241                $info[$instance->id.'1'][1] = 0;
242          }
243       }
244       return $info;
245    }
247 ///////////////////////////////////////////////////////////////
248 ///////////////////////////////////////////////////////////////
249 ///////////////////////////////////////////////////////////////
250 //// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
252    //Returns an array of feedbacks ids 
253    function feedback_count ($course) {
254       global $CFG;
255       return count_records('feedback', 'course', $course);
256    }
257    
258    function feedback_completed_count($course) {
259       global $CFG;
260       $count = 0;
261       //get all feedbacks
262       $feedbacks = get_records('feedback', 'course', $course);
263       if($feedbacks) {
264          foreach($feedbacks as $feedback) {
265             $count += count_records('feedback_completed', 'feedback', $feedback->id);
266          }
267       }
268       return $count;
269    }
270    
271 ?>