Moodle release 1.9.16
[moodle.git] / mod / quiz / restorelib.php
blob758e2b2b186593946323d5fc3751096be788e86c
1 <?php // $Id$
2 //This php script contains all the stuff to restore quiz mods
4 // Todo:
6 // whereever it says "/// We have to recode the .... field" we should put in a check
7 // to see if the recoding was successful and throw an appropriate error otherwise
9 //This is the "graphical" structure of the quiz mod:
10 //To see, put your terminal to 160cc
13 // quiz
14 // (CL,pk->id)
15 // |
16 // -------------------------------------------------------------------
17 // | | | |
18 // | quiz_grades | quiz_question_versions
19 // | (UL,pk->id,fk->quiz) | (CL,pk->id,fk->quiz)
20 // | |
21 // quiz_attempts quiz_question_instances
22 // (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz,question)
24 // Meaning: pk->primary key field of the table
25 // fk->foreign key to link with parent
26 // nt->nested field (recursive data)
27 // SL->site level info
28 // CL->course level info
29 // UL->user level info
30 // files->table may have files
32 //-----------------------------------------------------------
34 // When we restore a quiz we also need to restore the questions and possibly
35 // the data about student interaction with the questions. The functions to do
36 // that are included with the following library
37 include_once("$CFG->dirroot/question/restorelib.php");
39 function quiz_restore_mods($mod,$restore) {
41 global $CFG;
43 $status = true;
45 //Hook to call Moodle < 1.5 Quiz Restore
46 if ($restore->backup_version < 2005043000) {
47 include_once("restorelibpre15.php");
48 return quiz_restore_pre15_mods($mod,$restore);
51 //Get record from backup_ids
52 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
54 if ($data) {
55 //Now get completed xmlized object
56 $info = $data->info;
57 //if necessary, write to restorelog and adjust date/time fields
58 if ($restore->course_startdateoffset) {
59 restore_log_date_changes('Quiz', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
61 //traverse_xmlize($info); //Debug
62 //print_object ($GLOBALS['traverse_array']); //Debug
63 //$GLOBALS['traverse_array']=""; //Debug
65 //Now, build the QUIZ record structure
66 $quiz = new stdClass;
67 $quiz->course = $restore->course_id;
68 $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
69 $quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
70 $quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
71 $quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
72 $quiz->optionflags = backup_todb($info['MOD']['#']['OPTIONFLAGS']['0']['#']);
73 $quiz->penaltyscheme = backup_todb($info['MOD']['#']['PENALTYSCHEME']['0']['#']);
74 $quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
75 $quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
76 $quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
77 $quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
78 $quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
79 $quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['0']['#']);
80 $quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
81 $quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
82 $quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
83 $quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
84 $quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
85 $quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
86 $quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
87 $quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
88 $quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
89 $quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
90 $quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
91 $quiz->delay1 = isset($info['MOD']['#']['DELAY1']['0']['#'])?backup_todb($info['MOD']['#']['DELAY1']['0']['#']):'';
92 $quiz->delay2 = isset($info['MOD']['#']['DELAY2']['0']['#'])?backup_todb($info['MOD']['#']['DELAY2']['0']['#']):'';
93 //We have to recode the questions field (a list of questions id and pagebreaks)
94 $quiz->questions = quiz_recode_layout($quiz->questions, $restore);
96 //The structure is equal to the db, so insert the quiz
97 $newid = insert_record ("quiz",$quiz);
99 //Do some output
100 if (!defined('RESTORE_SILENTLY')) {
101 echo "<li>".get_string("modulename","quiz")." \"".format_string(stripslashes($quiz->name),true)."\"</li>";
103 backup_flush(300);
105 if ($newid) {
106 //We have the newid, update backup_ids
107 backup_putid($restore->backup_unique_code,$mod->modtype,
108 $mod->id, $newid);
109 //We have to restore the question_instances now (course level table)
110 $status = quiz_question_instances_restore_mods($newid,$info,$restore);
111 //We have to restore the feedback now (course level table)
112 $status = quiz_feedback_restore_mods($newid, $info, $restore, $quiz);
113 //We have to restore the question_versions now (course level table)
114 $status = quiz_question_versions_restore_mods($newid,$info,$restore);
115 //Now check if want to restore user data and do it.
116 if (restore_userdata_selected($restore,'quiz',$mod->id)) {
117 //Restore quiz_attempts
118 $status = quiz_attempts_restore_mods ($newid,$info,$restore);
119 if ($status) {
120 //Restore quiz_grades
121 $status = quiz_grades_restore_mods ($newid,$info,$restore);
124 } else {
125 $status = false;
127 } else {
128 $status = false;
131 return $status;
134 //This function restores the quiz_question_instances
135 function quiz_question_instances_restore_mods($quiz_id,$info,$restore) {
137 global $CFG;
139 $status = true;
141 //Get the quiz_question_instances array
142 if (array_key_exists('QUESTION_INSTANCES', $info['MOD']['#'])) {
143 $instances = $info['MOD']['#']['QUESTION_INSTANCES']['0']['#']['QUESTION_INSTANCE'];
144 } else {
145 $instances = array();
148 //Iterate over question_instances
149 for($i = 0; $i < sizeof($instances); $i++) {
150 $gra_info = $instances[$i];
151 //traverse_xmlize($gra_info); //Debug
152 //print_object ($GLOBALS['traverse_array']); //Debug
153 //$GLOBALS['traverse_array']=""; //Debug
155 //We'll need this later!!
156 $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
158 //Now, build the QUESTION_INSTANCES record structure
159 $instance = new stdClass;
160 $instance->quiz = $quiz_id;
161 $instance->question = backup_todb($gra_info['#']['QUESTION']['0']['#']);
162 $instance->grade = backup_todb($gra_info['#']['GRADE']['0']['#']);
164 //We have to recode the question field
165 $question = backup_getid($restore->backup_unique_code,"question",$instance->question);
166 if ($question) {
167 $instance->question = $question->new_id;
170 //The structure is equal to the db, so insert the quiz_question_instances
171 $newid = insert_record ("quiz_question_instances",$instance);
173 //Do some output
174 if (($i+1) % 10 == 0) {
175 if (!defined('RESTORE_SILENTLY')) {
176 echo ".";
177 if (($i+1) % 200 == 0) {
178 echo "<br />";
181 backup_flush(300);
184 if ($newid) {
185 //We have the newid, update backup_ids
186 backup_putid($restore->backup_unique_code,"quiz_question_instances",$oldid,
187 $newid);
188 } else {
189 $status = false;
193 return $status;
196 //This function restores the quiz_question_instances
197 function quiz_feedback_restore_mods($quiz_id, $info, $restore, $quiz) {
198 $status = true;
200 //Get the quiz_feedback array
201 if (array_key_exists('FEEDBACKS', $info['MOD']['#'])) {
202 $feedbacks = $info['MOD']['#']['FEEDBACKS']['0']['#']['FEEDBACK'];
204 //Iterate over the feedbacks
205 foreach ($feedbacks as $feedback_info) {
206 //traverse_xmlize($feedback_info); //Debug
207 //print_object ($GLOBALS['traverse_array']); //Debug
208 //$GLOBALS['traverse_array']=""; //Debug
210 //We'll need this later!!
211 $oldid = backup_todb($feedback_info['#']['ID']['0']['#']);
213 //Now, build the quiz_feedback record structure
214 $feedback = new stdClass();
215 $feedback->quizid = $quiz_id;
216 $feedback->feedbacktext = backup_todb($feedback_info['#']['FEEDBACKTEXT']['0']['#']);
217 $feedback->mingrade = backup_todb($feedback_info['#']['MINGRADE']['0']['#']);
218 $feedback->maxgrade = backup_todb($feedback_info['#']['MAXGRADE']['0']['#']);
220 //The structure is equal to the db, so insert the quiz_question_instances
221 $newid = insert_record('quiz_feedback', $feedback);
223 if ($newid) {
224 //We have the newid, update backup_ids
225 backup_putid($restore->backup_unique_code, 'quiz_feedback', $oldid, $newid);
226 } else {
227 $status = false;
230 } else {
231 $feedback = new stdClass();
232 $feedback->quizid = $quiz_id;
233 $feedback->feedbacktext = '';
234 $feedback->mingrade = 0;
235 $feedback->maxgrade = $quiz->grade + 1;
236 insert_record('quiz_feedback', $feedback);
239 return $status;
242 //This function restores the quiz_question_versions
243 function quiz_question_versions_restore_mods($quiz_id,$info,$restore) {
245 global $CFG, $USER;
247 $status = true;
249 //Get the quiz_question_versions array
250 if (!empty($info['MOD']['#']['QUESTION_VERSIONS'])) {
251 $versions = $info['MOD']['#']['QUESTION_VERSIONS']['0']['#']['QUESTION_VERSION'];
252 } else {
253 $versions = array();
256 //Iterate over question_versions
257 for($i = 0; $i < sizeof($versions); $i++) {
258 $ver_info = $versions[$i];
259 //traverse_xmlize($ver_info); //Debug
260 //print_object ($GLOBALS['traverse_array']); //Debug
261 //$GLOBALS['traverse_array']=""; //Debug
263 //We'll need this later!!
264 $oldid = backup_todb($ver_info['#']['ID']['0']['#']);
266 //Now, build the QUESTION_VERSIONS record structure
267 $version = new stdClass;
268 $version->quiz = $quiz_id;
269 $version->oldquestion = backup_todb($ver_info['#']['OLDQUESTION']['0']['#']);
270 $version->newquestion = backup_todb($ver_info['#']['NEWQUESTION']['0']['#']);
271 $version->originalquestion = backup_todb($ver_info['#']['ORIGINALQUESTION']['0']['#']);
272 $version->userid = backup_todb($ver_info['#']['USERID']['0']['#']);
273 $version->timestamp = backup_todb($ver_info['#']['TIMESTAMP']['0']['#']);
275 //We have to recode the oldquestion field
276 $question = backup_getid($restore->backup_unique_code,"question",$version->oldquestion);
277 if ($question) {
278 $version->oldquestion = $question->new_id;
281 //We have to recode the newquestion field
282 $question = backup_getid($restore->backup_unique_code,"question",$version->newquestion);
283 if ($question) {
284 $version->newquestion = $question->new_id;
287 //We have to recode the originalquestion field
288 $question = backup_getid($restore->backup_unique_code,"question",$version->originalquestion);
289 if ($question) {
290 $version->newquestion = $question->new_id;
293 //We have to recode the userid field
294 $user = backup_getid($restore->backup_unique_code,"user",$version->userid);
295 if ($user) {
296 $version->userid = $user->new_id;
297 } else { //Assign to current user
298 $version->userid = $USER->id;
301 //The structure is equal to the db, so insert the quiz_question_versions
302 $newid = insert_record ("quiz_question_versions",$version);
304 //Do some output
305 if (($i+1) % 10 == 0) {
306 if (!defined('RESTORE_SILENTLY')) {
307 echo ".";
308 if (($i+1) % 200 == 0) {
309 echo "<br />";
312 backup_flush(300);
315 if ($newid) {
316 //We have the newid, update backup_ids
317 backup_putid($restore->backup_unique_code,"quiz_question_versions",$oldid,
318 $newid);
319 } else {
320 $status = false;
324 return $status;
327 //This function restores the quiz_attempts
328 function quiz_attempts_restore_mods($quiz_id,$info,$restore) {
330 global $CFG;
332 $status = true;
334 //Get the quiz_attempts array
335 if (array_key_exists('ATTEMPTS', $info['MOD']['#'])) {
336 $attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
337 } else {
338 $attempts = array();
341 //Iterate over attempts
342 for($i = 0; $i < sizeof($attempts); $i++) {
343 $att_info = $attempts[$i];
344 //traverse_xmlize($att_info); //Debug
345 //print_object ($GLOBALS['traverse_array']); //Debug
346 //$GLOBALS['traverse_array']=""; //Debug
348 //We'll need this later!!
349 $oldid = backup_todb($att_info['#']['ID']['0']['#']);
350 $olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
352 //Now, build the ATTEMPTS record structure
353 $attempt = new stdClass;
354 $attempt->quiz = $quiz_id;
355 $attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
356 $attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
357 $attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
358 $attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
359 $attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
360 $attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
361 $attempt->layout = backup_todb($att_info['#']['LAYOUT']['0']['#']);
362 $attempt->preview = backup_todb($att_info['#']['PREVIEW']['0']['#']);
364 //We have to recode the userid field
365 $user = backup_getid($restore->backup_unique_code,"user",$attempt->userid);
366 if ($user) {
367 $attempt->userid = $user->new_id;
370 //Set the uniqueid field
371 $attempt->uniqueid = question_new_attempt_uniqueid();
373 //We have to recode the layout field (a list of questions id and pagebreaks)
374 $attempt->layout = quiz_recode_layout($attempt->layout, $restore);
376 //The structure is equal to the db, so insert the quiz_attempts
377 $newid = insert_record ("quiz_attempts",$attempt);
379 //Do some output
380 if (($i+1) % 10 == 0) {
381 if (!defined('RESTORE_SILENTLY')) {
382 echo ".";
383 if (($i+1) % 200 == 0) {
384 echo "<br />";
387 backup_flush(300);
390 if ($newid) {
391 //We have the newid, update backup_ids
392 backup_putid($restore->backup_unique_code,"quiz_attempts",$oldid,
393 $newid);
394 //Now process question_states
395 // This function is defined in question/restorelib.php
396 $status = question_states_restore_mods($attempt->uniqueid,$att_info,$restore);
397 } else {
398 $status = false;
402 return $status;
405 //This function restores the quiz_grades
406 function quiz_grades_restore_mods($quiz_id,$info,$restore) {
408 global $CFG;
410 $status = true;
412 //Get the quiz_grades array
413 if (array_key_exists('GRADES', $info['MOD']['#'])) {
414 $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
415 } else {
416 $grades = array();
419 //Iterate over grades
420 for($i = 0; $i < sizeof($grades); $i++) {
421 $gra_info = $grades[$i];
422 //traverse_xmlize($gra_info); //Debug
423 //print_object ($GLOBALS['traverse_array']); //Debug
424 //$GLOBALS['traverse_array']=""; //Debug
426 //We'll need this later!!
427 $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
428 $olduserid = backup_todb($gra_info['#']['USERID']['0']['#']);
430 //Now, build the GRADES record structure
431 $grade = new stdClass;
432 $grade->quiz = $quiz_id;
433 $grade->userid = backup_todb($gra_info['#']['USERID']['0']['#']);
434 $grade->grade = backup_todb($gra_info['#']['GRADEVAL']['0']['#']);
435 $grade->timemodified = backup_todb($gra_info['#']['TIMEMODIFIED']['0']['#']);
437 //We have to recode the userid field
438 $user = backup_getid($restore->backup_unique_code,"user",$grade->userid);
439 if ($user) {
440 $grade->userid = $user->new_id;
443 //The structure is equal to the db, so insert the quiz_grades
444 $newid = insert_record ("quiz_grades",$grade);
446 //Do some output
447 if (($i+1) % 10 == 0) {
448 if (!defined('RESTORE_SILENTLY')) {
449 echo ".";
450 if (($i+1) % 200 == 0) {
451 echo "<br />";
454 backup_flush(300);
457 if ($newid) {
458 //We have the newid, update backup_ids
459 backup_putid($restore->backup_unique_code,"quiz_grades",$oldid,
460 $newid);
461 } else {
462 $status = false;
466 return $status;
469 //Return a content decoded to support interactivities linking. Every module
470 //should have its own. They are called automatically from
471 //quiz_decode_content_links_caller() function in each module
472 //in the restore process
473 function quiz_decode_content_links ($content,$restore) {
475 global $CFG;
477 $result = $content;
479 //Link to the list of quizs
481 $searchstring='/\$@(QUIZINDEX)\*([0-9]+)@\$/';
482 //We look for it
483 preg_match_all($searchstring,$content,$foundset);
484 //If found, then we are going to look for its new id (in backup tables)
485 if ($foundset[0]) {
486 //print_object($foundset); //Debug
487 //Iterate over foundset[2]. They are the old_ids
488 foreach($foundset[2] as $old_id) {
489 //We get the needed variables here (course id)
490 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
491 //Personalize the searchstring
492 $searchstring='/\$@(QUIZINDEX)\*('.$old_id.')@\$/';
493 //If it is a link to this course, update the link to its new location
494 if($rec->new_id) {
495 //Now replace it
496 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/index.php?id='.$rec->new_id,$result);
497 } else {
498 //It's a foreign link so leave it as original
499 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/index.php?id='.$old_id,$result);
504 //Link to quiz view by moduleid
505 $searchstring='/\$@(QUIZVIEWBYID)\*([0-9]+)@\$/';
506 //We look for it
507 preg_match_all($searchstring,$result,$foundset);
508 //If found, then we are going to look for its new id (in backup tables)
509 if ($foundset[0]) {
510 //print_object($foundset); //Debug
511 //Iterate over foundset[2]. They are the old_ids
512 foreach($foundset[2] as $old_id) {
513 //We get the needed variables here (course_modules id)
514 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
515 //Personalize the searchstring
516 $searchstring='/\$@(QUIZVIEWBYID)\*('.$old_id.')@\$/';
517 //If it is a link to this course, update the link to its new location
518 if($rec->new_id) {
519 //Now replace it
520 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?id='.$rec->new_id,$result);
521 } else {
522 //It's a foreign link so leave it as original
523 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?id='.$old_id,$result);
528 //Link to quiz view by quizid
529 $searchstring='/\$@(QUIZVIEWBYQ)\*([0-9]+)@\$/';
530 //We look for it
531 preg_match_all($searchstring,$result,$foundset);
532 //If found, then we are going to look for its new id (in backup tables)
533 if ($foundset[0]) {
534 //print_object($foundset); //Debug
535 //Iterate over foundset[2]. They are the old_ids
536 foreach($foundset[2] as $old_id) {
537 //We get the needed variables here (course_modules id)
538 $rec = backup_getid($restore->backup_unique_code,'quiz',$old_id);
539 //Personalize the searchstring
540 $searchstring='/\$@(QUIZVIEWBYQ)\*('.$old_id.')@\$/';
541 //If it is a link to this course, update the link to its new location
542 if($rec->new_id) {
543 //Now replace it
544 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?q='.$rec->new_id,$result);
545 } else {
546 //It's a foreign link so leave it as original
547 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?q='.$old_id,$result);
552 return $result;
555 //This function makes all the necessary calls to xxxx_decode_content_links()
556 //function in each module, passing them the desired contents to be decoded
557 //from backup format to destination site/course in order to mantain inter-activities
558 //working in the backup/restore process. It's called from restore_decode_content_links()
559 //function in restore process
560 function quiz_decode_content_links_caller($restore) {
561 global $CFG;
562 $status = true;
564 if ($quizs = get_records_sql ("SELECT q.id, q.intro
565 FROM {$CFG->prefix}quiz q
566 WHERE q.course = $restore->course_id")) {
567 //Iterate over each quiz->intro
568 $i = 0; //Counter to send some output to the browser to avoid timeouts
569 foreach ($quizs as $quiz) {
570 //Increment counter
571 $i++;
572 $content = $quiz->intro;
573 $result = restore_decode_content_links_worker($content,$restore);
574 if ($result != $content) {
575 //Update record
576 $quiz->intro = addslashes($result);
577 $status = update_record("quiz",$quiz);
578 if (debugging()) {
579 if (!defined('RESTORE_SILENTLY')) {
580 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
584 //Do some output
585 if (($i+1) % 5 == 0) {
586 if (!defined('RESTORE_SILENTLY')) {
587 echo ".";
588 if (($i+1) % 100 == 0) {
589 echo "<br />";
592 backup_flush(300);
597 return $status;
600 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
601 //some texts in the module
602 function quiz_restore_wiki2markdown ($restore) {
604 global $CFG;
606 $status = true;
608 //Convert question->questiontext
609 if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat
610 FROM {$CFG->prefix}question q,
611 {$CFG->prefix}backup_ids b
612 WHERE b.backup_code = $restore->backup_unique_code AND
613 b.table_name = 'question' AND
614 q.id = b.new_id AND
615 q.questiontextformat = ".FORMAT_WIKI)) {
616 $i = 0;
617 foreach ($records as $record) {
618 //Rebuild wiki links
619 $record->questiontext = restore_decode_wiki_content($record->questiontext, $restore);
620 //Convert to Markdown
621 $wtm = new WikiToMarkdown();
622 $record->questiontext = $wtm->convert($record->questiontext, $restore->course_id);
623 $record->questiontextformat = FORMAT_MARKDOWN;
624 $status = update_record('question', addslashes_object($record));
625 //Do some output
626 $i++;
627 if (($i+1) % 1 == 0) {
628 if (!defined('RESTORE_SILENTLY')) {
629 echo ".";
630 if (($i+1) % 20 == 0) {
631 echo "<br />";
634 backup_flush(300);
638 return $status;
641 //This function returns a log record with all the necessay transformations
642 //done. It's used by restore_log_module() to restore modules log.
643 function quiz_restore_logs($restore,$log) {
645 $status = false;
647 //Depending of the action, we recode different things
648 switch ($log->action) {
649 case "add":
650 if ($log->cmid) {
651 //Get the new_id of the module (to recode the info field)
652 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
653 if ($mod) {
654 $log->url = "view.php?id=".$log->cmid;
655 $log->info = $mod->new_id;
656 $status = true;
659 break;
660 case "update":
661 if ($log->cmid) {
662 //Get the new_id of the module (to recode the info field)
663 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
664 if ($mod) {
665 $log->url = "view.php?id=".$log->cmid;
666 $log->info = $mod->new_id;
667 $status = true;
670 break;
671 case "view":
672 if ($log->cmid) {
673 //Get the new_id of the module (to recode the info field)
674 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
675 if ($mod) {
676 $log->url = "view.php?id=".$log->cmid;
677 $log->info = $mod->new_id;
678 $status = true;
681 break;
682 case "view all":
683 $log->url = "index.php?id=".$log->course;
684 $status = true;
685 break;
686 case "report":
687 if ($log->cmid) {
688 //Get the new_id of the module (to recode the info field)
689 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
690 if ($mod) {
691 $log->url = "report.php?id=".$log->cmid;
692 $log->info = $mod->new_id;
693 $status = true;
696 break;
697 case "attempt":
698 if ($log->cmid) {
699 //Get the new_id of the module (to recode the info field)
700 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
701 if ($mod) {
702 //Extract the attempt id from the url field
703 $attid = substr(strrchr($log->url,"="),1);
704 //Get the new_id of the attempt (to recode the url field)
705 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
706 if ($att) {
707 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
708 $log->info = $mod->new_id;
709 $status = true;
713 break;
714 case "submit":
715 if ($log->cmid) {
716 //Get the new_id of the module (to recode the info field)
717 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
718 if ($mod) {
719 //Extract the attempt id from the url field
720 $attid = substr(strrchr($log->url,"="),1);
721 //Get the new_id of the attempt (to recode the url field)
722 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
723 if ($att) {
724 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
725 $log->info = $mod->new_id;
726 $status = true;
730 break;
731 case "review":
732 if ($log->cmid) {
733 //Get the new_id of the module (to recode the info field)
734 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
735 if ($mod) {
736 //Extract the attempt id from the url field
737 $attid = substr(strrchr($log->url,"="),1);
738 //Get the new_id of the attempt (to recode the url field)
739 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
740 if ($att) {
741 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
742 $log->info = $mod->new_id;
743 $status = true;
747 break;
748 case "editquestions":
749 if ($log->cmid) {
750 //Get the new_id of the module (to recode the url field)
751 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
752 if ($mod) {
753 $log->url = "view.php?id=".$log->cmid;
754 $log->info = $mod->new_id;
755 $status = true;
758 break;
759 case "preview":
760 if ($log->cmid) {
761 //Get the new_id of the module (to recode the url field)
762 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
763 if ($mod) {
764 $log->url = "attempt.php?id=".$log->cmid;
765 $log->info = $mod->new_id;
766 $status = true;
769 break;
770 case "start attempt":
771 if ($log->cmid) {
772 //Get the new_id of the module (to recode the info field)
773 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
774 if ($mod) {
775 //Extract the attempt id from the url field
776 $attid = substr(strrchr($log->url,"="),1);
777 //Get the new_id of the attempt (to recode the url field)
778 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
779 if ($att) {
780 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
781 $log->info = $mod->new_id;
782 $status = true;
786 break;
787 case "close attempt":
788 if ($log->cmid) {
789 //Get the new_id of the module (to recode the info field)
790 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
791 if ($mod) {
792 //Extract the attempt id from the url field
793 $attid = substr(strrchr($log->url,"="),1);
794 //Get the new_id of the attempt (to recode the url field)
795 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
796 if ($att) {
797 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
798 $log->info = $mod->new_id;
799 $status = true;
803 break;
804 case "continue attempt":
805 if ($log->cmid) {
806 //Get the new_id of the module (to recode the info field)
807 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
808 if ($mod) {
809 //Extract the attempt id from the url field
810 $attid = substr(strrchr($log->url,"="),1);
811 //Get the new_id of the attempt (to recode the url field)
812 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
813 if ($att) {
814 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
815 $log->info = $mod->new_id;
816 $status = true;
820 break;
821 case "continue attemp":
822 if ($log->cmid) {
823 //Get the new_id of the module (to recode the info field)
824 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
825 if ($mod) {
826 //Extract the attempt id from the url field
827 $attid = substr(strrchr($log->url,"="),1);
828 //Get the new_id of the attempt (to recode the url field)
829 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
830 if ($att) {
831 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
832 $log->info = $mod->new_id;
833 $log->action = "continue attempt"; //To recover some bad actions
834 $status = true;
838 break;
839 default:
840 if (!defined('RESTORE_SILENTLY')) {
841 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
843 break;
846 if ($status) {
847 $status = $log;
849 return $status;
852 function quiz_recode_layout($layout, $restore) {
853 //Recodes the quiz layout (a list of questions id and pagebreaks)
855 //Extracts question id from sequence
856 if ($questionids = explode(',', $layout)) {
857 foreach ($questionids as $id => $questionid) {
858 if ($questionid) { // If it is zero then this is a pagebreak, don't translate
859 $newq = backup_getid($restore->backup_unique_code,"question",$questionid);
860 $questionids[$id] = $newq->new_id;
864 return implode(',', $questionids);