MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / exercise / restorelib.php
blobc9430af61d9930208938216a8f3731391185e615
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 restore procedure about this mod
40 function exercise_restore_mods($mod,$restore) {
42 global $CFG;
44 $status = true;
46 //Get record from backup_ids
47 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
49 if ($data) {
50 //Now get completed xmlized object
51 $info = $data->info;
52 // if necessary, write to restorelog and adjust date/time fields
53 if ($restore->course_startdateoffset) {
54 restore_log_date_changes('Exercise', $restore, $info['MOD']['#'], array('DEADLINE'));
56 //traverse_xmlize($info); //Debug
57 //print_object ($GLOBALS['traverse_array']); //Debug
58 //$GLOBALS['traverse_array']=""; //Debug
60 //Now, build the exercise record structure
61 $exercise->course = $restore->course_id;
62 $exercise->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
63 $exercise->nelements = backup_todb($info['MOD']['#']['NELEMENTS']['0']['#']);
64 $exercise->phase = backup_todb($info['MOD']['#']['PHASE']['0']['#']);
65 $exercise->gradingstrategy = backup_todb($info['MOD']['#']['GRADINGSTRATEGY']['0']['#']);
66 $exercise->usemaximum = backup_todb($info['MOD']['#']['USEMAXIMUM']['0']['#']);
67 $exercise->assessmentcomps = backup_todb($info['MOD']['#']['ASSESSMENTCOMPS']['0']['#']);
68 $exercise->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
69 $exercise->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
70 $exercise->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
71 $date = usergetdate($exercise->deadline);
72 fwrite ($restorelog_file,"<br/>The Exercise - ".$exercise->name." <br/>");
73 fwrite ($restorelog_file,"The DEADLINE was " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."");
74 $exercise->deadline += $restore->course_startdateoffset;
75 $date = usergetdate($exercise->deadline);
76 fwrite ($restorelog_file,"&nbsp;&nbsp;&nbsp;the DEADLINE is now " .$date['weekday'].", ".$date['mday']." ".$date['month']." ".$date['year']."<br/>");
77 $exercise->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
78 $exercise->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
79 $exercise->gradinggrade = backup_todb($info['MOD']['#']['GRADINGGRADE']['0']['#']);
80 $exercise->showleaguetable = backup_todb($info['MOD']['#']['SHOWLEAGUETABLE']['0']['#']);
81 $exercise->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
82 $exercise->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
84 //The structure is equal to the db, so insert the exercise
85 $newid = insert_record ("exercise",$exercise);
87 //Do some output
88 if (!defined('RESTORE_SILENTLY')) {
89 echo "<li>".get_string("modulename","exercise")." \"".format_string(stripslashes($exercise->name),true)."\"</li>";
91 backup_flush(300);
93 if ($newid) {
94 //We have the newid, update backup_ids
95 backup_putid($restore->backup_unique_code,$mod->modtype,
96 $mod->id, $newid);
97 //We have to restore the exercise_elements table now (course level table)
98 $status = exercise_elements_restore($newid,$info,$restore);
99 //restore the teacher submissions and optionally the student submissions
100 $status = exercise_submissions_restore($mod->id, $newid,$info,$restore);
101 } else {
102 $status = false;
104 } else {
105 $status = false;
108 return $status;
111 //This function restores the exercise_elements
112 function exercise_elements_restore($exercise_id,$info,$restore) {
114 global $CFG;
116 $status = true;
118 //Get the exercise_elements array
119 $elements = $info['MOD']['#']['ELEMENTS']['0']['#']['ELEMENT'];
121 //Iterate over exercise_elements
122 for($i = 0; $i < sizeof($elements); $i++) {
123 $ele_info = $elements[$i];
124 //traverse_xmlize($ele_info); //Debug
125 //print_object ($GLOBALS['traverse_array']); //Debug
126 //$GLOBALS['traverse_array']=""; //Debug
128 //Now, build the exercise_ELEMENTS record structure
129 $element->exerciseid = $exercise_id;
130 $element->elementno = backup_todb($ele_info['#']['ELEMENTNO']['0']['#']);
131 $element->description = backup_todb($ele_info['#']['DESCRIPTION']['0']['#']);
132 $element->scale = backup_todb($ele_info['#']['SCALE']['0']['#']);
133 $element->maxscore = backup_todb($ele_info['#']['MAXSCORE']['0']['#']);
134 $element->weight = backup_todb($ele_info['#']['WEIGHT']['0']['#']);
136 //The structure is equal to the db, so insert the exercise_elements
137 $newid = insert_record ("exercise_elements",$element);
139 //Do some output
140 if (($i+1) % 10 == 0) {
141 if (!defined('RESTORE_SILENTLY')) {
142 echo ".";
143 if (($i+1) % 200 == 0) {
144 echo "<br />";
147 backup_flush(300);
150 if ($newid) {
151 //We have to restore the exercise_rubrics table now (course level table)
152 $status = exercise_rubrics_restore($exercise_id,$element->elementno,$ele_info,$restore);
153 } else {
154 $status = false;
158 return $status;
162 //This function restores the exercise_rubrics
163 function exercise_rubrics_restore($exercise_id,$elementno,$info,$restore) {
165 global $CFG;
167 $status = true;
169 //Get the exercise_rubrics array (optional)
170 if (isset($info['#']['RUBRICS']['0']['#']['RUBRIC'])) {
171 $rubrics = $info['#']['RUBRICS']['0']['#']['RUBRIC'];
173 //Iterate over exercise_rubrics
174 for($i = 0; $i < sizeof($rubrics); $i++) {
175 $rub_info = $rubrics[$i];
176 //traverse_xmlize($rub_info); //Debug
177 //print_object ($GLOBALS['traverse_array']); //Debug
178 //$GLOBALS['traverse_array']=""; //Debug
180 //Now, build the exercise_RUBRICS record structure
181 $rubric->exerciseid = $exercise_id;
182 $rubric->elementno = $elementno;
183 $rubric->rubricno = backup_todb($rub_info['#']['RUBRICNO']['0']['#']);
184 $rubric->description = backup_todb($rub_info['#']['DESCRIPTION']['0']['#']);
186 //The structure is equal to the db, so insert the exercise_rubrics
187 $newid = insert_record ("exercise_rubrics",$rubric);
189 //Do some output
190 if (($i+1) % 10 == 0) {
191 if (!defined('RESTORE_SILENTLY')) {
192 echo ".";
193 if (($i+1) % 200 == 0) {
194 echo "<br />";
197 backup_flush(300);
200 if (!$newid) {
201 $status = false;
205 return $status;
209 //This function restores the submissions
210 function exercise_submissions_restore($old_exercise_id, $new_exercise_id,$info,$restore) {
212 global $CFG;
214 $status = true;
216 //Get the submissions array (teacher submissions)
217 $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];
218 //Iterate over submissions
219 for($i = 0; $i < sizeof($submissions); $i++) {
220 $sub_info = $submissions[$i];
221 //traverse_xmlize($sub_info); //Debug
222 //print_object ($GLOBALS['traverse_array']); //Debug
223 //$GLOBALS['traverse_array']=""; //Debug
225 //We'll need this later!!
226 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
227 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
229 //Now, build the exercise_SUBMISSIONS record structure
230 $submission->exerciseid = $new_exercise_id;
231 $submission->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
232 $submission->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
233 $submission->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
234 $submission->resubmit = backup_todb($sub_info['#']['RESUBMIT']['0']['#']);
235 $submission->mailed = backup_todb($sub_info['#']['MAILED']['0']['#']);
236 $submission->isexercise = backup_todb($sub_info['#']['ISEXERCISE']['0']['#']);
237 $submission->late = backup_todb($sub_info['#']['LATE']['0']['#']);
239 // always save the exercise descriptions and optionally the student submissions
240 if ($submission->isexercise or restore_userdata_selected($restore,'exercise',$old_exercise_id)) {
241 //We have to recode the userid field
242 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
243 if ($user) {
244 $submission->userid = $user->new_id;
247 //The structure is equal to the db, so insert the exercise_submission
248 $newid = insert_record ("exercise_submissions",$submission);
250 //Do some output
251 if (($i+1) % 50 == 0) {
252 if (!defined('RESTORE_SILENTLY')) {
253 echo ".";
254 if (($i+1) % 1000 == 0) {
255 echo "<br />";
258 backup_flush(300);
261 if ($newid) {
262 //We have the newid, update backup_ids
263 backup_putid($restore->backup_unique_code,"exercise_submissions",$oldid,
264 $newid);
266 //Now copy moddata associated files
267 $status = exercise_restore_files($oldid, $newid,$restore);
268 //Now we need to restore exercise_assessments (user level table)
269 if ($status and restore_userdata_selected($restore,'exercise',$old_exercise_id)) {
270 $status = exercise_assessments_restore($new_exercise_id, $newid,$sub_info,$restore);
272 } else {
273 $status = false;
278 return $status;
281 //This function restores the exercise_assessments
282 function exercise_assessments_restore($new_exercise_id, $new_submission_id,$info,$restore) {
284 global $CFG;
286 $status = true;
288 //Get the assessments array (optional)
289 if (isset($info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'])) {
290 $assessments = $info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'];
292 //Iterate over assessments
293 for($i = 0; $i < sizeof($assessments); $i++) {
294 $ass_info = $assessments[$i];
295 //traverse_xmlize($ass_info); //Debug
296 //print_object ($GLOBALS['traverse_array']); //Debug
297 //$GLOBALS['traverse_array']=""; //Debug
299 //We'll need this later!!
300 $oldid = backup_todb($ass_info['#']['ID']['0']['#']);
301 $olduserid = backup_todb($ass_info['#']['USERID']['0']['#']);
303 //Now, build the exercise_ASSESSMENTS record structure
304 $assessment->exerciseid = $new_exercise_id;
305 $assessment->submissionid = $new_submission_id;
306 $assessment->userid = backup_todb($ass_info['#']['USERID']['0']['#']);
307 $assessment->timecreated = backup_todb($ass_info['#']['TIMECREATED']['0']['#']);
308 $assessment->timegraded = backup_todb($ass_info['#']['TIMEGRADED']['0']['#']);
309 $assessment->grade = backup_todb($ass_info['#']['GRADE']['0']['#']);
310 $assessment->gradinggrade = backup_todb($ass_info['#']['GRADINGGRADE']['0']['#']);
311 $assessment->mailed = backup_todb($ass_info['#']['MAILED']['0']['#']);
312 $assessment->generalcomment = backup_todb($ass_info['#']['GENERALCOMMENT']['0']['#']);
313 $assessment->teachercomment = backup_todb($ass_info['#']['TEACHERCOMMENT']['0']['#']);
315 //We have to recode the userid field
316 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
317 if ($user) {
318 $assessment->userid = $user->new_id;
321 //The structure is equal to the db, so insert the exercise_assessment
322 $newid = insert_record ("exercise_assessments",$assessment);
324 //Do some output
325 if (($i+1) % 50 == 0) {
326 if (!defined('RESTORE_SILENTLY')) {
327 echo ".";
328 if (($i+1) % 1000 == 0) {
329 echo "<br />";
332 backup_flush(300);
335 if ($newid) {
336 //We have the newid, update backup_ids
337 backup_putid($restore->backup_unique_code,"exercise_assessments",$oldid,
338 $newid);
340 //Now we need to restore exercise_grades (user level table)
341 if ($status) {
342 $status = exercise_grades_restore_mods ($new_exercise_id, $newid,$ass_info,$restore);
344 } else {
345 $status = false;
350 return $status;
353 //This function restores the exercise_grades
354 function exercise_grades_restore_mods($new_exercise_id, $new_assessment_id,$info,$restore) {
356 global $CFG;
358 $status = true;
360 //Get the grades array (optional)
361 if (isset($info['#']['GRADES']['0']['#']['GRADE'])) {
362 $grades = $info['#']['GRADES']['0']['#']['GRADE'];
364 //Iterate over grades
365 for($i = 0; $i < sizeof($grades); $i++) {
366 $gra_info = $grades[$i];
367 //traverse_xmlize($gra_info); //Debug
368 //print_object ($GLOBALS['traverse_array']); //Debug
369 //$GLOBALS['traverse_array']=""; //Debug
371 //Now, build the exercise_GRADES record structure
372 $grade->exerciseid = $new_exercise_id;
373 $grade->assessmentid = $new_assessment_id;
374 $grade->elementno = backup_todb($gra_info['#']['ELEMENTNO']['0']['#']);
375 $grade->feedback = backup_todb($gra_info['#']['FEEDBACK']['0']['#']);
376 $grade->grade = backup_todb($gra_info['#']['GRADE_VALUE']['0']['#']);
378 //The structure is equal to the db, so insert the exercise_grade
379 $newid = insert_record ("exercise_grades",$grade);
381 //Do some output
382 if (($i+1) % 50 == 0) {
383 if (!defined('RESTORE_SILENTLY')) {
384 echo ".";
385 if (($i+1) % 1000 == 0) {
386 echo "<br />";
389 backup_flush(300);
392 if (!$newid) {
393 $status = false;
398 return $status;
401 //This function copies the exercise related info from backup temp dir to course moddata folder,
402 //creating it if needed and recoding everything (submission_id)
403 function exercise_restore_files ($oldsubmiss, $newsubmiss, $restore) {
405 global $CFG;
407 $status = true;
408 $todo = false;
409 $moddata_path = "";
410 $exercise_path = "";
411 $temp_path = "";
413 //First, we check to "course_id" exists and create is as necessary
414 //in CFG->dataroot
415 $dest_dir = $CFG->dataroot."/".$restore->course_id;
416 $status = check_dir_exists($dest_dir,true);
418 //Now, locate course's moddata directory
419 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
421 //Check it exists and create it
422 $status = check_dir_exists($moddata_path,true);
424 //Now, locate exercise directory
425 if ($status) {
426 $exercise_path = $moddata_path."/exercise";
427 //Check it exists and create it
428 $status = check_dir_exists($exercise_path,true);
431 //Now locate the temp dir we are gong to restore
432 if ($status) {
433 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
434 "/moddata/exercise/".$oldsubmiss;
435 //Check it exists
436 if (is_dir($temp_path)) {
437 $todo = true;
441 //If todo, we create the neccesary dirs in course moddata/exercise
442 if ($status and $todo) {
443 //First this exercise id
444 $this_exercise_path = $exercise_path."/".$newsubmiss;
445 $status = check_dir_exists($this_exercise_path,true);
446 //And now, copy temp_path to this_exercise_path
447 $status = backup_copy_file($temp_path, $this_exercise_path);
450 return $status;