MDL-10496:
[moodle-linuxchix.git] / mod / exercise / backuplib.php
blobdc158fc833e83dbcb5741e66803de77801ad4b8a
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //exercise mods
5 //This is the "graphical" structure of the exercise mod:
6 //
7 // exercise
8 // (CL,pk->id)
9 // |
10 // |
11 // |
12 // |------------------------------|---------------------------------------------|
13 // | |
14 // | |
15 // | exercise_submissions
16 // | (UL,pk->id,fk->exerciseid,files)
17 // | |
18 // | |
19 // | |
20 // | |---------------------| |------------------------------| |
21 // | | | | | |
22 // exercise_elements exercise_grades exercise_assessments
23 // (CL,pk->id,fk->exerciseid) (UL,pk->id,fk->assessmentid) (UL,pk->id,fk->submissionid)
24 // | ( fk->elementno )
25 // |
26 // |
27 // exercise_rubrics
28 // (CL,pk->id,fk->elementno)
30 // Meaning: pk->primary key field of the table
31 // fk->foreign key to link with parent
32 // nt->nested field (recursive data)
33 // CL->course level info
34 // UL->user level info
35 // files->table may have files)
37 //-----------------------------------------------------------
39 //This function executes all the backup procedure about this mod
40 function exercise_backup_mods($bf,$preferences) {
42 global $CFG;
44 $status = true;
46 //Iterate over exercise table
47 $exercises = get_records ("exercise","course",$preferences->backup_course,"id");
48 if ($exercises) {
49 foreach ($exercises as $exercise) {
50 if (backup_mod_selected($preferences,'exercise',$exercise->id)) {
51 $status = exercise_backup_one_mod($bf,$preferences,$exercise);
55 return $status;
58 function exercise_backup_one_mod($bf,$preferences,$exercise) {
60 global $CFG;
62 if (is_numeric($exercise)) {
63 $exercise = get_record('exercise','id',$exercise);
66 $status = true;
68 //Start mod
69 fwrite ($bf,start_tag("MOD",3,true));
70 //Print exercise data
71 fwrite ($bf,full_tag("ID",4,false,$exercise->id));
72 fwrite ($bf,full_tag("MODTYPE",4,false,"exercise"));
73 fwrite ($bf,full_tag("NAME",4,false,$exercise->name));
74 fwrite ($bf,full_tag("NELEMENTS",4,false,$exercise->nelements));
75 fwrite ($bf,full_tag("PHASE",4,false,$exercise->phase));
76 fwrite ($bf,full_tag("GRADINGSTRATEGY",4,false,$exercise->gradingstrategy));
77 fwrite ($bf,full_tag("USEMAXIMUM",4,false,$exercise->usemaximum));
78 fwrite ($bf,full_tag("ASSESSMENTCOMPS",4,false,$exercise->assessmentcomps));
79 fwrite ($bf,full_tag("ANONYMOUS",4,false,$exercise->anonymous));
80 fwrite ($bf,full_tag("MAXBYTES",4,false,$exercise->maxbytes));
81 fwrite ($bf,full_tag("DEADLINE",4,false,$exercise->deadline));
82 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$exercise->timemodified));
83 fwrite ($bf,full_tag("GRADE",4,false,$exercise->grade));
84 fwrite ($bf,full_tag("GRADINGGRADE",4,false,$exercise->gradinggrade));
85 fwrite ($bf,full_tag("SHOWLEAGUETABLE",4,false,$exercise->showleaguetable));
86 fwrite ($bf,full_tag("USEPASSWORD",4,false,$exercise->usepassword));
87 fwrite ($bf,full_tag("PASSWORD",4,false,$exercise->password));
88 //Now we backup exercise elements
89 $status = backup_exercise_elements($bf,$preferences,$exercise->id);
90 //Now we backup any teacher submissions (these are an integral part of the exercise)
91 $status = backup_exercise_submissions($bf, $preferences, $exercise->id);
92 //End mod
93 $status =fwrite ($bf,end_tag("MOD",3,true));
94 //we need to backup the teacher files (the exercise descriptions)
95 $status = backup_exercise_teacher_files($bf, $preferences, $exercise->id);
96 //if we've selected to backup users info, then backup files too
97 if ($status) {
98 if (backup_userdata_selected($preferences,'exercise',$exercise->id)) {
99 $status = backup_exercise_student_files($bf,$preferences, $exercise->id);
103 return $status;
106 //Backup exercise_elements contents (executed from exercise_backup_mods)
107 function backup_exercise_elements ($bf,$preferences,$exercise) {
109 global $CFG;
111 $status = true;
113 $exercise_elements = get_records("exercise_elements","exerciseid",$exercise,"id");
114 //If there is exercise_elements
115 if ($exercise_elements) {
116 //Write start tag
117 $status =fwrite ($bf,start_tag("ELEMENTS",4,true));
118 //Iterate over each element
119 foreach ($exercise_elements as $element) {
120 //Start element
121 $status =fwrite ($bf,start_tag("ELEMENT",5,true));
122 //Print element contents
123 fwrite ($bf,full_tag("ELEMENTNO",6,false,$element->elementno));
124 fwrite ($bf,full_tag("DESCRIPTION",6,false,$element->description));
125 fwrite ($bf,full_tag("SCALE",6,false,$element->scale));
126 fwrite ($bf,full_tag("MAXSCORE",6,false,$element->maxscore));
127 fwrite ($bf,full_tag("WEIGHT",6,false,$element->weight));
128 //Now we backup exercise rubrics
129 $status = backup_exercise_rubrics($bf,$preferences,$exercise,$element->elementno);
130 //End element
131 $status =fwrite ($bf,end_tag("ELEMENT",5,true));
133 //Write end tag
134 $status =fwrite ($bf,end_tag("ELEMENTS",4,true));
136 return $status;
139 //Backup exercise_rubrics contents (executed from backup_exercise_elements)
140 function backup_exercise_rubrics ($bf,$preferences,$exercise,$elementno) {
142 global $CFG;
144 $status = true;
146 $exercise_rubrics = get_records_sql("SELECT * from {$CFG->prefix}exercise_rubrics r
147 WHERE r.exerciseid = '$exercise' and r.elementno = '$elementno'
148 ORDER BY r.elementno");
150 //If there is exercise_rubrics
151 if ($exercise_rubrics) {
152 //Write start tag
153 $status =fwrite ($bf,start_tag("RUBRICS",6,true));
154 //Iterate over each element
155 foreach ($exercise_rubrics as $rubric) {
156 //Start rubric
157 $status =fwrite ($bf,start_tag("RUBRIC",7,true));
158 //Print rubric contents
159 fwrite ($bf,full_tag("RUBRICNO",8,false,$rubric->rubricno));
160 fwrite ($bf,full_tag("DESCRIPTION",8,false,$rubric->description));
161 //End rubric
162 $status =fwrite ($bf,end_tag("RUBRIC",7,true));
164 //Write end tag
165 $status =fwrite ($bf,end_tag("RUBRICS",6,true));
167 return $status;
170 //Backup exercise_submissions contents (executed from exercise_backup_mods)
171 function backup_exercise_submissions ($bf,$preferences,$exerciseid) {
173 global $CFG;
175 $status = true;
177 $exercise_submissions = get_records_select("exercise_submissions","exerciseid = $exerciseid
178 AND isexercise = 1");
179 //If there is submissions
180 if ($exercise_submissions) {
181 //Write start tag
182 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
183 //Iterate over each submission
184 foreach ($exercise_submissions as $submission) {
185 //Start submission
186 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
187 //Print submission contents
188 fwrite ($bf,full_tag("ID",6,false,$submission->id));
189 fwrite ($bf,full_tag("USERID",6,false,$submission->userid));
190 fwrite ($bf,full_tag("TITLE",6,false,$submission->title));
191 fwrite ($bf,full_tag("TIMECREATED",6,false,$submission->timecreated));
192 fwrite ($bf,full_tag("RESUBMIT",6,false,$submission->resubmit));
193 fwrite ($bf,full_tag("MAILED",6,false,$submission->mailed));
194 fwrite ($bf,full_tag("ISEXERCISE",6,false,$submission->isexercise));
195 fwrite ($bf,full_tag("LATE",6,false,$submission->late));
196 //Now we backup any exercise assessments (if student data required)
197 if (backup_userdata_selected($preferences,'exercise',$exerciseid)) {
198 $status = backup_exercise_assessments($bf,$preferences,$exerciseid,$submission->id);
200 //End submission
201 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
203 //if we've selected to backup users info, then backup the student submisions
204 if (backup_userdata_selected($preferences,'exercise',$exerciseid)) {
205 $exercise_submissions = get_records_select("exercise_submissions","exerciseid = $exerciseid
206 AND isexercise = 0");
207 //If there is submissions
208 if ($exercise_submissions) {
209 //Iterate over each submission
210 foreach ($exercise_submissions as $submission) {
211 //Start submission
212 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
213 //Print submission contents
214 fwrite ($bf,full_tag("ID",6,false,$submission->id));
215 fwrite ($bf,full_tag("USERID",6,false,$submission->userid));
216 fwrite ($bf,full_tag("TITLE",6,false,$submission->title));
217 fwrite ($bf,full_tag("TIMECREATED",6,false,$submission->timecreated));
218 fwrite ($bf,full_tag("RESUBMIT",6,false,$submission->resubmit));
219 fwrite ($bf,full_tag("MAILED",6,false,$submission->mailed));
220 fwrite ($bf,full_tag("ISEXERCISE",6,false,$submission->isexercise));
221 fwrite ($bf,full_tag("LATE",6,false,$submission->late));
222 //Now we backup any exercise assessments
223 $status = backup_exercise_assessments($bf,$preferences,$exerciseid,$submission->id);
224 //End submission
225 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
229 //Write end tag
230 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
232 return $status;
235 //Backup exercise_student_submissions contents (executed from exercise_backup_mods)
236 function backup_exercise_student_submissions ($bf,$preferences,$exerciseid) {
238 global $CFG;
240 $status = true;
242 return $status;
245 //Backup exercise_assessments contents (executed from backup_exercise_submissions)
246 function backup_exercise_assessments ($bf,$preferences,$exercise,$submission) {
248 global $CFG;
250 $status = true;
252 //NOTE: I think that the exerciseid can go out (submissionid is a good unique fk), but mantain it, as is in db !!
253 $exercise_assessments = get_records_sql("SELECT * from {$CFG->prefix}exercise_assessments a
254 WHERE a.exerciseid = '$exercise' and a.submissionid = '$submission'
255 ORDER BY a.id");
257 //If there is exercise_assessments
258 if ($exercise_assessments) {
259 //Write start tag
260 $status =fwrite ($bf,start_tag("ASSESSMENTS",6,true));
261 //Iterate over each assessment
262 foreach ($exercise_assessments as $wor_ass) {
263 //Start assessment
264 $status =fwrite ($bf,start_tag("ASSESSMENT",7,true));
265 //Print assessment contents
266 fwrite ($bf,full_tag("ID",8,false,$wor_ass->id));
267 fwrite ($bf,full_tag("USERID",8,false,$wor_ass->userid));
268 fwrite ($bf,full_tag("TIMECREATED",8,false,$wor_ass->timecreated));
269 fwrite ($bf,full_tag("TIMEGRADED",8,false,$wor_ass->timegraded));
270 fwrite ($bf,full_tag("GRADE",8,false,$wor_ass->grade));
271 fwrite ($bf,full_tag("GRADINGGRADE",8,false,$wor_ass->gradinggrade));
272 fwrite ($bf,full_tag("MAILED",8,false,$wor_ass->mailed));
273 fwrite ($bf,full_tag("GENERALCOMMENT",8,false,$wor_ass->generalcomment));
274 fwrite ($bf,full_tag("TEACHERCOMMENT",8,false,$wor_ass->teachercomment));
275 //Now we backup exercise grades
276 $status = backup_exercise_grades($bf,$preferences,$exercise,$wor_ass->id);
277 //End assessment
278 $status =fwrite ($bf,end_tag("ASSESSMENT",7,true));
280 //Write end tag
281 $status =fwrite ($bf,end_tag("ASSESSMENTS",6,true));
283 return $status;
287 //Backup exercise_grades contents (executed from backup_exercise_assessments)
288 function backup_exercise_grades ($bf,$preferences,$exercise,$assessmentid) {
290 global $CFG;
292 $status = true;
294 //NOTE: I think that the exerciseid can go out (assessmentid is a good unique fk), but mantain it, as is in db !!
295 $exercise_grades = get_records_sql("SELECT * from {$CFG->prefix}exercise_grades g
296 WHERE g.exerciseid = '$exercise' and g.assessmentid = '$assessmentid'
297 ORDER BY g.elementno");
299 //If there is exercise_grades
300 if ($exercise_grades) {
301 //Write start tag
302 $status =fwrite ($bf,start_tag("GRADES",8,true));
303 //Iterate over each grade
304 foreach ($exercise_grades as $wor_gra) {
305 //Start grade
306 $status =fwrite ($bf,start_tag("GRADE",9,true));
307 //Print grade contents
308 fwrite ($bf,full_tag("ELEMENTNO",10,false,$wor_gra->elementno));
309 fwrite ($bf,full_tag("FEEDBACK",10,false,$wor_gra->feedback));
310 fwrite ($bf,full_tag("GRADE_VALUE",10,false,$wor_gra->grade));
311 //End comment
312 $status =fwrite ($bf,end_tag("GRADE",9,true));
314 //Write end tag
315 $status =fwrite ($bf,end_tag("GRADES",8,true));
317 return $status;
321 //Backup the teacher's exercise files (they are an integral part of the exercise)
322 function backup_exercise_teacher_files($bf,$preferences, $exerciseid) {
324 global $CFG;
326 $status = true;
328 //First we check to moddata exists and create it as necessary
329 //in temp/backup/$backup_code dir
330 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
331 //in temp/backup/$backup_code/moddate create the exercise diretory
332 $status = check_dir_exists("$CFG->dataroot/temp/backup/$preferences->backup_unique_code/moddata/exercise", true);
333 if ($status) {
334 //Now copy the submission dirs
335 if ($submissions = get_records_select("exercise_submissions", "exerciseid = $exerciseid
336 AND isexercise = 1")) {
337 foreach ($submissions as $submission) {
338 //Only if it exists !! Thanks to Daniel Miksik.
339 if (is_dir("{$CFG->dataroot}/$preferences->backup_course/{$CFG->moddata}/exercise/$submission->id")) {
340 // create directory
341 // $status = check_dir_exists("$CFG->dataroot/temp/backup/$preferences->backup_unique_code/moddata/exercise", true);
342 // copy all the files in the directory
343 $status = backup_copy_file("{$CFG->dataroot}/$preferences->backup_course/{$CFG->moddata}/exercise/$submission->id", "{$CFG->dataroot}/temp/backup/$preferences->backup_unique_code/moddata/exercise/$submission->id");
349 return $status;
353 //Backup students' exercise files because we've selected to backup user info
354 //and files are user info's level
355 function backup_exercise_student_files($bf,$preferences, $exerciseid) {
357 global $CFG;
359 $status = true;
361 //First we check to moddata exists and create it as necessary
362 //in temp/backup/$backup_code dir
363 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
364 if ($status) {
365 //Now copy the submission dirs
366 if ($submissions = get_records_select("exercise_submissions", "exerciseid = $exerciseid
367 AND isexercise = 0")) {
368 foreach ($submissions as $submission) {
369 //Only if it exists !! Thanks to Daniel Miksik.
370 if (is_dir("{$CFG->dataroot}/$preferences->backup_course/$CFG->moddata/exercise/$submission->id")) {
371 $status = backup_copy_file("{$CFG->dataroot}/$preferences->backup_course/{$CFG->moddata}/exercise/$submission->id", "{$CFG->dataroot}/temp/backup/$preferences->backup_unique_code/moddata/exercise/$submission->id");
377 return $status;
381 //Return an array of info (name,value)
382 function exercise_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
383 if (!empty($instances) && is_array($instances) && count($instances)) {
384 $info = array();
385 foreach ($instances as $id => $instance) {
386 $info += exercise_check_backup_mods_instances($instance,$backup_unique_code);
388 return $info;
390 //First the course data
391 $info[0][0] = get_string("modulenameplural","exercise");
392 if ($ids = exercise_ids ($course)) {
393 $info[0][1] = count($ids);
394 } else {
395 $info[0][1] = 0;
398 //Now, if requested, the user_data
399 if ($user_data) {
400 $info[1][0] = get_string("submissions","exercise");
401 if ($ids = exercise_submission_ids_by_course ($course)) {
402 $info[1][1] = count($ids);
403 } else {
404 $info[1][1] = 0;
407 return $info;
410 //Return an array of info (name,value)
411 function exercise_check_backup_mods_instances($instance,$backup_unique_code) {
412 //First the course data
413 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
414 $info[$instance->id.'0'][1] = '';
416 //Now, if requested, the user_data
417 if (!empty($instance->userdata)) {
418 $info[$instance->id.'1'][0] = get_string("submissions","exercise");
419 if ($ids = exercise_submission_ids_by_instance ($instance->id)) {
420 $info[$instance->id.'1'][1] = count($ids);
421 } else {
422 $info[$instance->id.'1'][1] = 0;
425 return $info;
433 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
435 //Returns an array of exercise id
436 function exercise_ids ($course) {
438 global $CFG;
440 return get_records_sql ("SELECT w.id, w.course
441 FROM {$CFG->prefix}exercise w
442 WHERE w.course = '$course'");
445 //Returns an array of exercise_submissions id
446 function exercise_submission_ids_by_course ($course) {
448 global $CFG;
450 return get_records_sql ("SELECT s.id , s.exerciseid
451 FROM {$CFG->prefix}exercise_submissions s,
452 {$CFG->prefix}exercise w
453 WHERE w.course = '$course' AND
454 s.exerciseid = w.id");
457 //Returns an array of exercise_submissions id
458 function exercise_submission_ids_by_instance ($instanceid) {
460 global $CFG;
462 return get_records_sql ("SELECT s.id , s.exerciseid
463 FROM {$CFG->prefix}exercise_submissions s
464 WHERE s.exerciseid = $instanceid");