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