MDL-15182 Fix LDAP enrolment course auto-creation at user login
[moodle.git] / mod / feedback / backuplib.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
29 //This function executes all the backup procedure about this mod
30 function feedback_backup_mods($bf,$preferences) {
31 global $CFG;
32
33 $status = true;
34
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 }
46
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));
efc59167 65 fwrite ($bf,full_tag("AUTONUMBERING",4,false,$feedback->autonumbering));
c184660d 66 fwrite ($bf,full_tag("SITEAFTERSUB",4,false,$feedback->site_after_submit));
c70ad9f7 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;
83 $status = true;
84 $feedbackitems = get_records('feedback_item', 'feedback', $feedbackid);
85 if(function_exists('backup_userdata_selected')) { //compatibility-hack for moodle 1.5.x
86 $backup_userdata = backup_userdata_selected($preferences,'feedback',$feedbackid);
87 }else {
88 $backup_userdata = $preferences->mods["feedback"]->userinfo;
89 }
90
91 if ($feedbackitems) {
92 $status =fwrite ($bf,start_tag("ITEMS",4,true));
93 foreach ($feedbackitems as $feedbackitem) {
94 //Start item
95 fwrite ($bf,start_tag("ITEM",5,true));
96 //Print item data
97 fwrite ($bf,full_tag("ID",6,false,$feedbackitem->id));
98 fwrite ($bf,full_tag("NAME",6,false,$feedbackitem->name));
99 fwrite ($bf,full_tag("PRESENTATION",6,false,$feedbackitem->presentation));
100 fwrite ($bf,full_tag("TYP",6,false,$feedbackitem->typ));
101 fwrite ($bf,full_tag("HASVALUE",6,false,$feedbackitem->hasvalue));
102 fwrite ($bf,full_tag("POSITION",6,false,$feedbackitem->position));
103 fwrite ($bf,full_tag("REQUIRED",6,false,$feedbackitem->required));
104
105 if ($backup_userdata) {
106 //backup the values of items
107 $feedbackvalues = get_records('feedback_value', 'item', $feedbackitem->id);
108 if($feedbackvalues) {
109 $status =fwrite ($bf,start_tag("FBVALUES",6,true));
110 foreach($feedbackvalues as $feedbackvalue) {
111 //start value
112 fwrite ($bf,start_tag("FBVALUE",7,true));
113 //print value data
114 fwrite ($bf,full_tag("ID",8,false,$feedbackvalue->id));
115 fwrite ($bf,full_tag("ITEM",8,false,$feedbackvalue->item));
116 fwrite ($bf,full_tag("COMPLETED",8,false,$feedbackvalue->completed));
117 fwrite ($bf,full_tag("VAL",8,false,$feedbackvalue->value));
118 fwrite ($bf,full_tag("COURSE_ID",8,false,$feedbackvalue->course_id));
119 //End value
120 $status =fwrite ($bf,end_tag("FBVALUE",7,true));
121 }
122 $status =fwrite ($bf,end_tag("FBVALUES",6,true));
123 }
124 }
125 //End item
126 $status =fwrite ($bf,end_tag("ITEM",5,true));
127 }
128 $status =fwrite ($bf,end_tag("ITEMS",4,true));
129 }
130
131 if($backup_userdata) {
132 //backup of feedback-completeds
133 $feedbackcompleteds = get_records('feedback_completed', 'feedback', $feedbackid);
134 if($feedbackcompleteds) {
135 fwrite ($bf,start_tag("COMPLETEDS",4,true));
136 foreach ($feedbackcompleteds as $feedbackcompleted) {
137 //Start completed
138 fwrite ($bf,start_tag("COMPLETED",5,true));
139 //Print completed data
140 fwrite ($bf,full_tag("ID",6,false,$feedbackcompleted->id));
141 fwrite ($bf,full_tag("FEEDBACK",6,false,$feedbackcompleted->feedback));
142 fwrite ($bf,full_tag("USERID",6,false,$feedbackcompleted->userid));
143 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$feedbackcompleted->timemodified));
144 fwrite ($bf,full_tag("RANDOMRESPONSE",6,false,$feedbackcompleted->random_response));
145 fwrite ($bf,full_tag("ANONYMOUSRESPONSE",6,false,$feedbackcompleted->anonymous_response));
146
147 //End completed
148 $status =fwrite ($bf,end_tag("COMPLETED",5,true));
149 }
150 $status =fwrite ($bf,end_tag("COMPLETEDS",4,true));
151 }
152
153 //backup of tracking-data
154 $feedbacktrackings = get_records('feedback_tracking', 'feedback', $feedbackid);
155 if($feedbacktrackings) {
156 fwrite ($bf,start_tag("TRACKINGS",4,true));
157 foreach ($feedbacktrackings as $feedbacktracking) {
158 //Start tracking
159 fwrite ($bf,start_tag("TRACKING",5,true));
160 //Print tracking data
161 fwrite ($bf,full_tag("ID",6,false,$feedbacktracking->id));
162 fwrite ($bf,full_tag("USERID",6,false,$feedbacktracking->userid));
163 fwrite ($bf,full_tag("FEEDBACK",6,false,$feedbacktracking->feedback));
164 fwrite ($bf,full_tag("COMPLETED",6,false,$feedbacktracking->completed));
c70ad9f7 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 }
173
174 }
175
176
177 function feedback_backup_template_data($bf, $templateid, $userinfo) {
178 global $CFG;
179 $status = true;
180 $templateitems = get_records('feedback_item', 'template', $templateid);
181
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 }
202
203
204
205
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 }
228
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] = '';
234
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 }
246
247///////////////////////////////////////////////////////////////
248///////////////////////////////////////////////////////////////
249///////////////////////////////////////////////////////////////
250//// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
251
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?>