Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / restorelib.php
blobe9e7de6857b74e476425cfaac26adcbe83f11283
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
506 $searchstring='/\$@(QUIZVIEWBYID)\*([0-9]+)@\$/';
507 //We look for it
508 preg_match_all($searchstring,$result,$foundset);
509 //If found, then we are going to look for its new id (in backup tables)
510 if ($foundset[0]) {
511 //print_object($foundset); //Debug
512 //Iterate over foundset[2]. They are the old_ids
513 foreach($foundset[2] as $old_id) {
514 //We get the needed variables here (course_modules id)
515 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
516 //Personalize the searchstring
517 $searchstring='/\$@(QUIZVIEWBYID)\*('.$old_id.')@\$/';
518 //If it is a link to this course, update the link to its new location
519 if($rec->new_id) {
520 //Now replace it
521 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/quiz/view.php?id='.$rec->new_id,$result);
522 } else {
523 //It's a foreign link so leave it as original
524 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/quiz/view.php?id='.$old_id,$result);
529 return $result;
532 //This function makes all the necessary calls to xxxx_decode_content_links()
533 //function in each module, passing them the desired contents to be decoded
534 //from backup format to destination site/course in order to mantain inter-activities
535 //working in the backup/restore process. It's called from restore_decode_content_links()
536 //function in restore process
537 function quiz_decode_content_links_caller($restore) {
538 global $CFG;
539 $status = true;
541 if ($quizs = get_records_sql ("SELECT q.id, q.intro
542 FROM {$CFG->prefix}quiz q
543 WHERE q.course = $restore->course_id")) {
544 //Iterate over each quiz->intro
545 $i = 0; //Counter to send some output to the browser to avoid timeouts
546 foreach ($quizs as $quiz) {
547 //Increment counter
548 $i++;
549 $content = $quiz->intro;
550 $result = restore_decode_content_links_worker($content,$restore);
551 if ($result != $content) {
552 //Update record
553 $quiz->intro = addslashes($result);
554 $status = update_record("quiz",$quiz);
555 if (debugging()) {
556 if (!defined('RESTORE_SILENTLY')) {
557 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
561 //Do some output
562 if (($i+1) % 5 == 0) {
563 if (!defined('RESTORE_SILENTLY')) {
564 echo ".";
565 if (($i+1) % 100 == 0) {
566 echo "<br />";
569 backup_flush(300);
574 return $status;
577 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
578 //some texts in the module
579 function quiz_restore_wiki2markdown ($restore) {
581 global $CFG;
583 $status = true;
585 //Convert question->questiontext
586 if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat
587 FROM {$CFG->prefix}question q,
588 {$CFG->prefix}backup_ids b
589 WHERE b.backup_code = $restore->backup_unique_code AND
590 b.table_name = 'question' AND
591 q.id = b.new_id AND
592 q.questiontextformat = ".FORMAT_WIKI)) {
593 $i = 0;
594 foreach ($records as $record) {
595 //Rebuild wiki links
596 $record->questiontext = restore_decode_wiki_content($record->questiontext, $restore);
597 //Convert to Markdown
598 $wtm = new WikiToMarkdown();
599 $record->questiontext = $wtm->convert($record->questiontext, $restore->course_id);
600 $record->questiontextformat = FORMAT_MARKDOWN;
601 $status = update_record('question', addslashes_object($record));
602 //Do some output
603 $i++;
604 if (($i+1) % 1 == 0) {
605 if (!defined('RESTORE_SILENTLY')) {
606 echo ".";
607 if (($i+1) % 20 == 0) {
608 echo "<br />";
611 backup_flush(300);
615 return $status;
618 //This function returns a log record with all the necessay transformations
619 //done. It's used by restore_log_module() to restore modules log.
620 function quiz_restore_logs($restore,$log) {
622 $status = false;
624 //Depending of the action, we recode different things
625 switch ($log->action) {
626 case "add":
627 if ($log->cmid) {
628 //Get the new_id of the module (to recode the info field)
629 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
630 if ($mod) {
631 $log->url = "view.php?id=".$log->cmid;
632 $log->info = $mod->new_id;
633 $status = true;
636 break;
637 case "update":
638 if ($log->cmid) {
639 //Get the new_id of the module (to recode the info field)
640 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
641 if ($mod) {
642 $log->url = "view.php?id=".$log->cmid;
643 $log->info = $mod->new_id;
644 $status = true;
647 break;
648 case "view":
649 if ($log->cmid) {
650 //Get the new_id of the module (to recode the info field)
651 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
652 if ($mod) {
653 $log->url = "view.php?id=".$log->cmid;
654 $log->info = $mod->new_id;
655 $status = true;
658 break;
659 case "view all":
660 $log->url = "index.php?id=".$log->course;
661 $status = true;
662 break;
663 case "report":
664 if ($log->cmid) {
665 //Get the new_id of the module (to recode the info field)
666 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
667 if ($mod) {
668 $log->url = "report.php?id=".$log->cmid;
669 $log->info = $mod->new_id;
670 $status = true;
673 break;
674 case "attempt":
675 if ($log->cmid) {
676 //Get the new_id of the module (to recode the info field)
677 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
678 if ($mod) {
679 //Extract the attempt id from the url field
680 $attid = substr(strrchr($log->url,"="),1);
681 //Get the new_id of the attempt (to recode the url field)
682 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
683 if ($att) {
684 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
685 $log->info = $mod->new_id;
686 $status = true;
690 break;
691 case "submit":
692 if ($log->cmid) {
693 //Get the new_id of the module (to recode the info field)
694 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
695 if ($mod) {
696 //Extract the attempt id from the url field
697 $attid = substr(strrchr($log->url,"="),1);
698 //Get the new_id of the attempt (to recode the url field)
699 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
700 if ($att) {
701 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
702 $log->info = $mod->new_id;
703 $status = true;
707 break;
708 case "review":
709 if ($log->cmid) {
710 //Get the new_id of the module (to recode the info field)
711 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
712 if ($mod) {
713 //Extract the attempt id from the url field
714 $attid = substr(strrchr($log->url,"="),1);
715 //Get the new_id of the attempt (to recode the url field)
716 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
717 if ($att) {
718 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
719 $log->info = $mod->new_id;
720 $status = true;
724 break;
725 case "editquestions":
726 if ($log->cmid) {
727 //Get the new_id of the module (to recode the url field)
728 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
729 if ($mod) {
730 $log->url = "view.php?id=".$log->cmid;
731 $log->info = $mod->new_id;
732 $status = true;
735 break;
736 case "preview":
737 if ($log->cmid) {
738 //Get the new_id of the module (to recode the url field)
739 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
740 if ($mod) {
741 $log->url = "attempt.php?id=".$log->cmid;
742 $log->info = $mod->new_id;
743 $status = true;
746 break;
747 case "start attempt":
748 if ($log->cmid) {
749 //Get the new_id of the module (to recode the info field)
750 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
751 if ($mod) {
752 //Extract the attempt id from the url field
753 $attid = substr(strrchr($log->url,"="),1);
754 //Get the new_id of the attempt (to recode the url field)
755 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
756 if ($att) {
757 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
758 $log->info = $mod->new_id;
759 $status = true;
763 break;
764 case "close attempt":
765 if ($log->cmid) {
766 //Get the new_id of the module (to recode the info field)
767 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
768 if ($mod) {
769 //Extract the attempt id from the url field
770 $attid = substr(strrchr($log->url,"="),1);
771 //Get the new_id of the attempt (to recode the url field)
772 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
773 if ($att) {
774 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
775 $log->info = $mod->new_id;
776 $status = true;
780 break;
781 case "continue attempt":
782 if ($log->cmid) {
783 //Get the new_id of the module (to recode the info field)
784 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
785 if ($mod) {
786 //Extract the attempt id from the url field
787 $attid = substr(strrchr($log->url,"="),1);
788 //Get the new_id of the attempt (to recode the url field)
789 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
790 if ($att) {
791 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
792 $log->info = $mod->new_id;
793 $status = true;
797 break;
798 case "continue attemp":
799 if ($log->cmid) {
800 //Get the new_id of the module (to recode the info field)
801 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
802 if ($mod) {
803 //Extract the attempt id from the url field
804 $attid = substr(strrchr($log->url,"="),1);
805 //Get the new_id of the attempt (to recode the url field)
806 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
807 if ($att) {
808 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
809 $log->info = $mod->new_id;
810 $log->action = "continue attempt"; //To recover some bad actions
811 $status = true;
815 break;
816 default:
817 if (!defined('RESTORE_SILENTLY')) {
818 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
820 break;
823 if ($status) {
824 $status = $log;
826 return $status;
829 function quiz_recode_layout($layout, $restore) {
830 //Recodes the quiz layout (a list of questions id and pagebreaks)
832 //Extracts question id from sequence
833 if ($questionids = explode(',', $layout)) {
834 foreach ($questionids as $id => $questionid) {
835 if ($questionid) { // If it is zero then this is a pagebreak, don't translate
836 $newq = backup_getid($restore->backup_unique_code,"question",$questionid);
837 $questionids[$id] = $newq->new_id;
841 return implode(',', $questionids);