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