Automatic installer.php lang files by installer_builder (20070310)
[moodle.git] / mod / exercise / restorelib.php
blobf5169415ff16806a73e1656d8249c61d2bd6b01f
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 //traverse_xmlize($info); //Debug
53 //print_object ($GLOBALS['traverse_array']); //Debug
54 //$GLOBALS['traverse_array']=""; //Debug
56 //Now, build the exercise record structure
57 $exercise->course = $restore->course_id;
58 $exercise->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
59 $exercise->nelements = backup_todb($info['MOD']['#']['NELEMENTS']['0']['#']);
60 $exercise->phase = backup_todb($info['MOD']['#']['PHASE']['0']['#']);
61 $exercise->gradingstrategy = backup_todb($info['MOD']['#']['GRADINGSTRATEGY']['0']['#']);
62 $exercise->usemaximum = backup_todb($info['MOD']['#']['USEMAXIMUM']['0']['#']);
63 $exercise->assessmentcomps = backup_todb($info['MOD']['#']['ASSESSMENTCOMPS']['0']['#']);
64 $exercise->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
65 $exercise->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
66 $exercise->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
67 $exercise->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
68 $exercise->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
69 $exercise->gradinggrade = backup_todb($info['MOD']['#']['GRADINGGRADE']['0']['#']);
70 $exercise->showleaguetable = backup_todb($info['MOD']['#']['SHOWLEAGUETABLE']['0']['#']);
71 $exercise->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
72 $exercise->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
74 //The structure is equal to the db, so insert the exercise
75 $newid = insert_record ("exercise",$exercise);
77 //Do some output
78 if (!defined('RESTORE_SILENTLY')) {
79 echo "<li>".get_string("modulename","exercise")." \"".format_string(stripslashes($exercise->name),true)."\"</li>";
81 backup_flush(300);
83 if ($newid) {
84 //We have the newid, update backup_ids
85 backup_putid($restore->backup_unique_code,$mod->modtype,
86 $mod->id, $newid);
87 //We have to restore the exercise_elements table now (course level table)
88 $status = exercise_elements_restore($newid,$info,$restore);
89 //restore the teacher submissions and optionally the student submissions
90 $status = exercise_submissions_restore($mod->id, $newid,$info,$restore);
91 } else {
92 $status = false;
94 } else {
95 $status = false;
98 return $status;
101 //This function restores the exercise_elements
102 function exercise_elements_restore($exercise_id,$info,$restore) {
104 global $CFG;
106 $status = true;
108 //Get the exercise_elements array
109 $elements = $info['MOD']['#']['ELEMENTS']['0']['#']['ELEMENT'];
111 //Iterate over exercise_elements
112 for($i = 0; $i < sizeof($elements); $i++) {
113 $ele_info = $elements[$i];
114 //traverse_xmlize($ele_info); //Debug
115 //print_object ($GLOBALS['traverse_array']); //Debug
116 //$GLOBALS['traverse_array']=""; //Debug
118 //Now, build the exercise_ELEMENTS record structure
119 $element->exerciseid = $exercise_id;
120 $element->elementno = backup_todb($ele_info['#']['ELEMENTNO']['0']['#']);
121 $element->description = backup_todb($ele_info['#']['DESCRIPTION']['0']['#']);
122 $element->scale = backup_todb($ele_info['#']['SCALE']['0']['#']);
123 $element->maxscore = backup_todb($ele_info['#']['MAXSCORE']['0']['#']);
124 $element->weight = backup_todb($ele_info['#']['WEIGHT']['0']['#']);
126 //The structure is equal to the db, so insert the exercise_elements
127 $newid = insert_record ("exercise_elements",$element);
129 //Do some output
130 if (($i+1) % 10 == 0) {
131 if (!defined('RESTORE_SILENTLY')) {
132 echo ".";
133 if (($i+1) % 200 == 0) {
134 echo "<br />";
137 backup_flush(300);
140 if ($newid) {
141 //We have to restore the exercise_rubrics table now (course level table)
142 $status = exercise_rubrics_restore($exercise_id,$element->elementno,$ele_info,$restore);
143 } else {
144 $status = false;
148 return $status;
152 //This function restores the exercise_rubrics
153 function exercise_rubrics_restore($exercise_id,$elementno,$info,$restore) {
155 global $CFG;
157 $status = true;
159 //Get the exercise_rubrics array (optional)
160 if (isset($info['#']['RUBRICS']['0']['#']['RUBRIC'])) {
161 $rubrics = $info['#']['RUBRICS']['0']['#']['RUBRIC'];
163 //Iterate over exercise_rubrics
164 for($i = 0; $i < sizeof($rubrics); $i++) {
165 $rub_info = $rubrics[$i];
166 //traverse_xmlize($rub_info); //Debug
167 //print_object ($GLOBALS['traverse_array']); //Debug
168 //$GLOBALS['traverse_array']=""; //Debug
170 //Now, build the exercise_RUBRICS record structure
171 $rubric->exerciseid = $exercise_id;
172 $rubric->elementno = $elementno;
173 $rubric->rubricno = backup_todb($rub_info['#']['RUBRICNO']['0']['#']);
174 $rubric->description = backup_todb($rub_info['#']['DESCRIPTION']['0']['#']);
176 //The structure is equal to the db, so insert the exercise_rubrics
177 $newid = insert_record ("exercise_rubrics",$rubric);
179 //Do some output
180 if (($i+1) % 10 == 0) {
181 if (!defined('RESTORE_SILENTLY')) {
182 echo ".";
183 if (($i+1) % 200 == 0) {
184 echo "<br />";
187 backup_flush(300);
190 if (!$newid) {
191 $status = false;
195 return $status;
199 //This function restores the submissions
200 function exercise_submissions_restore($old_exercise_id, $new_exercise_id,$info,$restore) {
202 global $CFG;
204 $status = true;
206 //Get the submissions array (teacher submissions)
207 $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];
208 //Iterate over submissions
209 for($i = 0; $i < sizeof($submissions); $i++) {
210 $sub_info = $submissions[$i];
211 //traverse_xmlize($sub_info); //Debug
212 //print_object ($GLOBALS['traverse_array']); //Debug
213 //$GLOBALS['traverse_array']=""; //Debug
215 //We'll need this later!!
216 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
217 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
219 //Now, build the exercise_SUBMISSIONS record structure
220 $submission->exerciseid = $new_exercise_id;
221 $submission->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
222 $submission->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
223 $submission->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
224 $submission->resubmit = backup_todb($sub_info['#']['RESUBMIT']['0']['#']);
225 $submission->mailed = backup_todb($sub_info['#']['MAILED']['0']['#']);
226 $submission->isexercise = backup_todb($sub_info['#']['ISEXERCISE']['0']['#']);
227 $submission->late = backup_todb($sub_info['#']['LATE']['0']['#']);
229 // always save the exercise descriptions and optionally the student submissions
230 if ($submission->isexercise or restore_userdata_selected($restore,'exercise',$old_exercise_id)) {
231 //We have to recode the userid field
232 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
233 if ($user) {
234 $submission->userid = $user->new_id;
237 //The structure is equal to the db, so insert the exercise_submission
238 $newid = insert_record ("exercise_submissions",$submission);
240 //Do some output
241 if (($i+1) % 50 == 0) {
242 if (!defined('RESTORE_SILENTLY')) {
243 echo ".";
244 if (($i+1) % 1000 == 0) {
245 echo "<br />";
248 backup_flush(300);
251 if ($newid) {
252 //We have the newid, update backup_ids
253 backup_putid($restore->backup_unique_code,"exercise_submissions",$oldid,
254 $newid);
256 //Now copy moddata associated files
257 $status = exercise_restore_files($oldid, $newid,$restore);
258 //Now we need to restore exercise_assessments (user level table)
259 if ($status and restore_userdata_selected($restore,'exercise',$old_exercise_id)) {
260 $status = exercise_assessments_restore($new_exercise_id, $newid,$sub_info,$restore);
262 } else {
263 $status = false;
268 return $status;
271 //This function restores the exercise_assessments
272 function exercise_assessments_restore($new_exercise_id, $new_submission_id,$info,$restore) {
274 global $CFG;
276 $status = true;
278 //Get the assessments array (optional)
279 if (isset($info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'])) {
280 $assessments = $info['#']['ASSESSMENTS']['0']['#']['ASSESSMENT'];
282 //Iterate over assessments
283 for($i = 0; $i < sizeof($assessments); $i++) {
284 $ass_info = $assessments[$i];
285 //traverse_xmlize($ass_info); //Debug
286 //print_object ($GLOBALS['traverse_array']); //Debug
287 //$GLOBALS['traverse_array']=""; //Debug
289 //We'll need this later!!
290 $oldid = backup_todb($ass_info['#']['ID']['0']['#']);
291 $olduserid = backup_todb($ass_info['#']['USERID']['0']['#']);
293 //Now, build the exercise_ASSESSMENTS record structure
294 $assessment->exerciseid = $new_exercise_id;
295 $assessment->submissionid = $new_submission_id;
296 $assessment->userid = backup_todb($ass_info['#']['USERID']['0']['#']);
297 $assessment->timecreated = backup_todb($ass_info['#']['TIMECREATED']['0']['#']);
298 $assessment->timegraded = backup_todb($ass_info['#']['TIMEGRADED']['0']['#']);
299 $assessment->grade = backup_todb($ass_info['#']['GRADE']['0']['#']);
300 $assessment->gradinggrade = backup_todb($ass_info['#']['GRADINGGRADE']['0']['#']);
301 $assessment->mailed = backup_todb($ass_info['#']['MAILED']['0']['#']);
302 $assessment->generalcomment = backup_todb($ass_info['#']['GENERALCOMMENT']['0']['#']);
303 $assessment->teachercomment = backup_todb($ass_info['#']['TEACHERCOMMENT']['0']['#']);
305 //We have to recode the userid field
306 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
307 if ($user) {
308 $assessment->userid = $user->new_id;
311 //The structure is equal to the db, so insert the exercise_assessment
312 $newid = insert_record ("exercise_assessments",$assessment);
314 //Do some output
315 if (($i+1) % 50 == 0) {
316 if (!defined('RESTORE_SILENTLY')) {
317 echo ".";
318 if (($i+1) % 1000 == 0) {
319 echo "<br />";
322 backup_flush(300);
325 if ($newid) {
326 //We have the newid, update backup_ids
327 backup_putid($restore->backup_unique_code,"exercise_assessments",$oldid,
328 $newid);
330 //Now we need to restore exercise_grades (user level table)
331 if ($status) {
332 $status = exercise_grades_restore_mods ($new_exercise_id, $newid,$ass_info,$restore);
334 } else {
335 $status = false;
340 return $status;
343 //This function restores the exercise_grades
344 function exercise_grades_restore_mods($new_exercise_id, $new_assessment_id,$info,$restore) {
346 global $CFG;
348 $status = true;
350 //Get the grades array (optional)
351 if (isset($info['#']['GRADES']['0']['#']['GRADE'])) {
352 $grades = $info['#']['GRADES']['0']['#']['GRADE'];
354 //Iterate over grades
355 for($i = 0; $i < sizeof($grades); $i++) {
356 $gra_info = $grades[$i];
357 //traverse_xmlize($gra_info); //Debug
358 //print_object ($GLOBALS['traverse_array']); //Debug
359 //$GLOBALS['traverse_array']=""; //Debug
361 //Now, build the exercise_GRADES record structure
362 $grade->exerciseid = $new_exercise_id;
363 $grade->assessmentid = $new_assessment_id;
364 $grade->elementno = backup_todb($gra_info['#']['ELEMENTNO']['0']['#']);
365 $grade->feedback = backup_todb($gra_info['#']['FEEDBACK']['0']['#']);
366 $grade->grade = backup_todb($gra_info['#']['GRADE_VALUE']['0']['#']);
368 //The structure is equal to the db, so insert the exercise_grade
369 $newid = insert_record ("exercise_grades",$grade);
371 //Do some output
372 if (($i+1) % 50 == 0) {
373 if (!defined('RESTORE_SILENTLY')) {
374 echo ".";
375 if (($i+1) % 1000 == 0) {
376 echo "<br />";
379 backup_flush(300);
382 if (!$newid) {
383 $status = false;
388 return $status;
391 //This function copies the exercise related info from backup temp dir to course moddata folder,
392 //creating it if needed and recoding everything (submission_id)
393 function exercise_restore_files ($oldsubmiss, $newsubmiss, $restore) {
395 global $CFG;
397 $status = true;
398 $todo = false;
399 $moddata_path = "";
400 $exercise_path = "";
401 $temp_path = "";
403 //First, we check to "course_id" exists and create is as necessary
404 //in CFG->dataroot
405 $dest_dir = $CFG->dataroot."/".$restore->course_id;
406 $status = check_dir_exists($dest_dir,true);
408 //Now, locate course's moddata directory
409 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
411 //Check it exists and create it
412 $status = check_dir_exists($moddata_path,true);
414 //Now, locate exercise directory
415 if ($status) {
416 $exercise_path = $moddata_path."/exercise";
417 //Check it exists and create it
418 $status = check_dir_exists($exercise_path,true);
421 //Now locate the temp dir we are gong to restore
422 if ($status) {
423 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
424 "/moddata/exercise/".$oldsubmiss;
425 //Check it exists
426 if (is_dir($temp_path)) {
427 $todo = true;
431 //If todo, we create the neccesary dirs in course moddata/exercise
432 if ($status and $todo) {
433 //First this exercise id
434 $this_exercise_path = $exercise_path."/".$newsubmiss;
435 $status = check_dir_exists($this_exercise_path,true);
436 //And now, copy temp_path to this_exercise_path
437 $status = backup_copy_file($temp_path, $this_exercise_path);
440 return $status;