2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the exercise mod:
12 // |------------------------------|---------------------------------------------|
15 // | exercise_submissions
16 // | (UL,pk->id,fk->exerciseid,files)
20 // | |---------------------| |------------------------------| |
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 )
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) {
46 //Get record from backup_ids
47 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
50 //Now get completed xmlized object
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," 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);
88 if (!defined('RESTORE_SILENTLY')) {
89 echo "<li>".get_string("modulename","exercise")." \"".format_string(stripslashes($exercise->name
),true)."\"</li>";
94 //We have the newid, update backup_ids
95 backup_putid($restore->backup_unique_code
,$mod->modtype
,
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);
111 //This function restores the exercise_elements
112 function exercise_elements_restore($exercise_id,$info,$restore) {
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);
140 if (($i+
1) %
10 == 0) {
141 if (!defined('RESTORE_SILENTLY')) {
143 if (($i+
1) %
200 == 0) {
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);
162 //This function restores the exercise_rubrics
163 function exercise_rubrics_restore($exercise_id,$elementno,$info,$restore) {
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);
190 if (($i+
1) %
10 == 0) {
191 if (!defined('RESTORE_SILENTLY')) {
193 if (($i+
1) %
200 == 0) {
209 //This function restores the submissions
210 function exercise_submissions_restore($old_exercise_id, $new_exercise_id,$info,$restore) {
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);
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);
251 if (($i+
1) %
50 == 0) {
252 if (!defined('RESTORE_SILENTLY')) {
254 if (($i+
1) %
1000 == 0) {
262 //We have the newid, update backup_ids
263 backup_putid($restore->backup_unique_code
,"exercise_submissions",$oldid,
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);
281 //This function restores the exercise_assessments
282 function exercise_assessments_restore($new_exercise_id, $new_submission_id,$info,$restore) {
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);
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);
325 if (($i+
1) %
50 == 0) {
326 if (!defined('RESTORE_SILENTLY')) {
328 if (($i+
1) %
1000 == 0) {
336 //We have the newid, update backup_ids
337 backup_putid($restore->backup_unique_code
,"exercise_assessments",$oldid,
340 //Now we need to restore exercise_grades (user level table)
342 $status = exercise_grades_restore_mods ($new_exercise_id, $newid,$ass_info,$restore);
353 //This function restores the exercise_grades
354 function exercise_grades_restore_mods($new_exercise_id, $new_assessment_id,$info,$restore) {
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);
382 if (($i+
1) %
50 == 0) {
383 if (!defined('RESTORE_SILENTLY')) {
385 if (($i+
1) %
1000 == 0) {
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) {
413 //First, we check to "course_id" exists and create is as necessary
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
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
433 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
434 "/moddata/exercise/".$oldsubmiss;
436 if (is_dir($temp_path)) {
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);