Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / restorelibpre15.php
blob68b1c5b3ee901cf7b5782d0996fcd7c4cb3f6812
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //quiz mods
5 //To see, put your terminal to 160cc
7 //This is the "graphical" structure of the quiz mod:
8 //
9 // quiz question_categories
10 // (CL,pk->id) (CL,pk->id)
11 // | |
12 // ------------------------------------------------------------------- |
13 // | | | | |.......................................
14 // | | | | | .
15 // | | | | | .
16 // quiz_attempts quiz_grades quiz_question_grades quiz_question_versions | ----question_datasets---- .
17 // (UL,pk->id, fk->quiz) (UL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) (CL,pk->id,fk->quiz) | | (CL,pk->id,fk->question, | .
18 // | | . | | fk->dataset_definition) | .
19 // | | . | | | .
20 // | | . | | | .
21 // | | . | | | .
22 // quiz_responses | question question_dataset_definitions
23 // (UL,pk->id, fk->attempt)----------------------------------------------------(CL,pk->id,fk->category,files) (CL,pk->id,fk->category)
24 // | |
25 // | |
26 // | |
27 // | question_dataset_items
28 // | (CL,pk->id,fk->definition)
29 // |
30 // |
31 // |
32 // --------------------------------------------------------------------------------------------------------------
33 // | | | | | | |
34 // | | | | | | |
35 // | | | | question_calculated | | question_randomsamatch
36 // question_truefalse | question_multichoice | (CL,pl->id,fk->question) | |--(CL,pl->id,fk->question)
37 // (CL,pl->id,fk->question) | (CL,pl->id,fk->question) | . | |
38 // . | . | . | |
39 // . question_shortanswer . question_numerical . question_multianswer. |
40 // . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) . (CL,pl->id,fk->question) | question_match
41 // . . . . . . |--(CL,pl->id,fk->question)
42 // . . . . . . | .
43 // . . . . . . | .
44 // . . . . . . | .
45 // . . . . . . | question_match_sub
46 // . . . . . . |--(CL,pl->id,fk->question)
47 // ........................................................................................ |
48 // . |
49 // . |
50 // . | question_numerical_units
51 // question_answers |--(CL,pl->id,fk->question)
52 // (CL,pk->id,fk->question)----------------------------------------------------------
54 // Meaning: pk->primary key field of the table
55 // fk->foreign key to link with parent
56 // nt->nested field (recursive data)
57 // CL->course level info
58 // UL->user level info
59 // files->table may have files
61 //-----------------------------------------------------------
63 //This module is special, because we make the restore in two steps:
64 // 1.-We restore every category and their questions (complete structure). It includes this tables:
65 // - question_categories
66 // - question
67 // - question_truefalse
68 // - question_shortanswer
69 // - question_multianswer
70 // - question_multichoice
71 // - question_numerical
72 // - question_randomsamatch
73 // - question_match
74 // - question_match_sub
75 // - question_calculated
76 // - question_answers
77 // - question_numerical_units
78 // - question_datasets
79 // - question_dataset_definitions
80 // - question_dataset_items
81 // All this backup info have its own section in moodle.xml (QUESTION_CATEGORIES) and it's generated
82 // before every module backup standard invocation. And only if to restore quizzes has been selected !!
83 // It's invoked with quiz_restore_question_categories. (course independent).
85 // 2.-Standard module restore (Invoked via quiz_restore_mods). It includes this tables:
86 // - quiz
87 // - quiz_question_versions
88 // - quiz_question_grades
89 // - quiz_attempts
90 // - quiz_grades
91 // - quiz_responses
92 // This step is the standard mod backup. (course dependent).
94 //We are going to nedd quiz libs to be able to mimic the upgrade process
95 require_once("$CFG->dirroot/mod/quiz/locallib.php");
97 //STEP 1. Restore categories/questions and associated structures
98 // (course independent)
99 function quiz_restore_pre15_question_categories($category,$restore) {
101 global $CFG;
103 $status = true;
105 //Get record from backup_ids
106 $data = backup_getid($restore->backup_unique_code,"question_categories",$category->id);
108 if ($data) {
109 //Now get completed xmlized object
110 $info = $data->info;
111 //traverse_xmlize($info); //Debug
112 //print_object ($GLOBALS['traverse_array']); //Debug
113 //$GLOBALS['traverse_array']=""; //Debug
115 //Now, build the question_categories record structure
116 $quiz_cat->course = $restore->course_id;
117 $quiz_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']);
118 $quiz_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']);
119 $quiz_cat->publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']);
120 $quiz_cat->stamp = backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']);
121 $quiz_cat->parent = backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']);
122 $quiz_cat->sortorder = backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']);
124 if ($catfound = restore_get_best_question_category($quiz_cat, $restore->course_id)) {
125 $newid = $catfound;
126 } else {
127 if (!$quiz_cat->stamp) {
128 $quiz_cat->stamp = make_unique_id_code();
130 $newid = insert_record ("question_categories",$quiz_cat);
133 //Do some output
134 if ($newid) {
135 if (!defined('RESTORE_SILENTLY')) {
136 echo "<li>".get_string('category', 'quiz')." \"".$quiz_cat->name."\"<br />";
138 } else {
139 if (!defined('RESTORE_SILENTLY')) {
140 //We must never arrive here !!
141 echo "<li>".get_string('category', 'quiz')." \"".$quiz_cat->name."\" Error!<br />";
143 $status = false;
145 backup_flush(300);
147 //Here category has been created or selected, so save results in backup_ids and start with questions
148 if ($newid and $status) {
149 //We have the newid, update backup_ids
150 backup_putid($restore->backup_unique_code,"question_categories",
151 $category->id, $newid);
152 //Now restore question
153 $status = quiz_restore_pre15_questions ($category->id, $newid,$info,$restore);
154 } else {
155 $status = false;
157 if (!defined('RESTORE_SILENTLY')) {
158 echo '</li>';
162 return $status;
165 function quiz_restore_pre15_questions ($old_category_id,$new_category_id,$info,$restore) {
167 global $CFG;
169 $status = true;
171 //Get the questions array
172 $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
174 //Iterate over questions
175 for($i = 0; $i < sizeof($questions); $i++) {
176 $question = new object;
177 $que_info = $questions[$i];
178 //traverse_xmlize($que_info); //Debug
179 //print_object ($GLOBALS['traverse_array']); //Debug
180 //$GLOBALS['traverse_array']=""; //Debug
182 //We'll need this later!!
183 $oldid = backup_todb($que_info['#']['ID']['0']['#']);
185 //Now, build the question record structure
186 $question->category = $new_category_id;
187 $question->parent = backup_todb($que_info['#']['PARENT']['0']['#']);
188 $question->name = backup_todb($que_info['#']['NAME']['0']['#']);
189 $question->questiontext = backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
190 $question->questiontextformat = backup_todb($que_info['#']['QUESTIONTEXTFORMAT']['0']['#']);
191 $question->image = backup_todb($que_info['#']['IMAGE']['0']['#']);
192 $question->defaultgrade = backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
193 if (isset($que_info['#']['PENALTY']['0']['#'])) { //Only if it's set, to apply DB default else.
194 $question->penalty = backup_todb($que_info['#']['PENALTY']['0']['#']);
196 $question->qtype = backup_todb($que_info['#']['QTYPE']['0']['#']);
197 if (isset($que_info['#']['LENGTH']['0']['#'])) { //Only if it's set, to apply DB default else.
198 $question->length = backup_todb($que_info['#']['LENGTH']['0']['#']);
200 $question->stamp = backup_todb($que_info['#']['STAMP']['0']['#']);
201 if (isset($que_info['#']['VERSION']['0']['#'])) { //Only if it's set, to apply DB default else.
202 $question->version = backup_todb($que_info['#']['VERSION']['0']['#']);
204 if (isset($que_info['#']['HIDDEN']['0']['#'])) { //Only if it's set, to apply DB default else.
205 $question->hidden = backup_todb($que_info['#']['HIDDEN']['0']['#']);
208 //Although only a few backups can have questions with parent, we try to recode it
209 //if it contains something
210 if ($question->parent and $parent = backup_getid($restore->backup_unique_code,"question",$question->parent)) {
211 $question->parent = $parent->new_id;
214 // If it is a random question then hide it
215 if ($question->qtype == RANDOM) {
216 $question->hidden = 1;
219 //If it is a description question, length = 0
220 if ($question->qtype == DESCRIPTION) {
221 $question->length = 0;
224 //Check if the question exists
225 //by category and stamp
226 $question_exists = get_record ("question","category",$question->category,
227 "stamp",$question->stamp);
228 //If the stamp doesn't exists, check if question exists
229 //by category, name and questiontext and calculate stamp
230 //Mantains pre Beta 1.1 compatibility !!
231 if (!$question->stamp) {
232 $question->stamp = make_unique_id_code();
233 $question->version = 1;
234 $question_exists = get_record ("question","category",$question->category,
235 "name",$question->name,
236 "questiontext",$question->questiontext);
239 //If the question exists, only record its id
240 if ($question_exists) {
241 $newid = $question_exists->id;
242 $creatingnewquestion = false;
243 //Else, create a new question
244 } else {
245 //The structure is equal to the db, so insert the question
246 $newid = insert_record ("question",$question);
247 //If it is a random question, parent = id
248 if ($newid && $question->qtype == RANDOM) {
249 set_field ('question', 'parent', $newid, 'id', $newid);
251 $creatingnewquestion = true;
254 //Do some output
255 if (($i+1) % 2 == 0) {
256 if (!defined('RESTORE_SILENTLY')) {
257 echo ".";
258 if (($i+1) % 40 == 0) {
259 echo "<br />";
262 backup_flush(300);
264 //Save newid to backup tables
265 if ($newid) {
266 //We have the newid, update backup_ids
267 backup_putid($restore->backup_unique_code,"question",$oldid,
268 $newid);
270 //If it's a new question in the DB, restore it
271 if ($creatingnewquestion) {
272 //Now, restore every question_answers in this question
273 $status = quiz_restore_pre15_answers($oldid,$newid,$que_info,$restore);
274 //Now, depending of the type of questions, invoke different functions
275 if ($question->qtype == "1") {
276 $status = quiz_restore_pre15_shortanswer($oldid,$newid,$que_info,$restore);
277 } else if ($question->qtype == "2") {
278 $status = quiz_restore_pre15_truefalse($oldid,$newid,$que_info,$restore);
279 } else if ($question->qtype == "3") {
280 $status = quiz_restore_pre15_multichoice($oldid,$newid,$que_info,$restore);
281 } else if ($question->qtype == "4") {
282 //Random question. Nothing to do.
283 } else if ($question->qtype == "5") {
284 $status = quiz_restore_pre15_match($oldid,$newid,$que_info,$restore);
285 } else if ($question->qtype == "6") {
286 $status = quiz_restore_pre15_randomsamatch($oldid,$newid,$que_info,$restore);
287 } else if ($question->qtype == "7") {
288 //Description question. Nothing to do.
289 } else if ($question->qtype == "8") {
290 $status = quiz_restore_pre15_numerical($oldid,$newid,$que_info,$restore);
291 } else if ($question->qtype == "9") {
292 $status = quiz_restore_pre15_multianswer($oldid,$newid,$que_info,$restore);
293 } else if ($question->qtype == "10") {
294 $status = quiz_restore_pre15_calculated($oldid,$newid,$que_info,$restore);
296 } else {
297 //We are NOT creating the question, but we need to know every question_answers
298 //map between the XML file and the database to be able to restore the responses
299 //in each attempt.
300 $status = quiz_restore_pre15_map_answers($oldid,$newid,$que_info,$restore);
301 //Now, depending of the type of questions, invoke different functions
302 //to create the necessary mappings in backup_ids, because we are not
303 //creating the question, but need some records in backup table
304 if ($question->qtype == "1") {
305 //Shortanswer question. Nothing to remap
306 } else if ($question->qtype == "2") {
307 //Truefalse question. Nothing to remap
308 } else if ($question->qtype == "3") {
309 //Multichoice question. Nothing to remap
310 } else if ($question->qtype == "4") {
311 //Random question. Nothing to remap
312 } else if ($question->qtype == "5") {
313 $status = quiz_restore_pre15_map_match($oldid,$newid,$que_info,$restore);
314 } else if ($question->qtype == "6") {
315 //Randomsamatch question. Nothing to remap
316 } else if ($question->qtype == "7") {
317 //Description question. Nothing to remap
318 } else if ($question->qtype == "8") {
319 //Numerical question. Nothing to remap
320 } else if ($question->qtype == "9") {
321 //Multianswer question. Nothing to remap
322 } else if ($question->qtype == "10") {
323 //Calculated question. Nothing to remap
327 return $status;
330 function quiz_restore_pre15_answers ($old_question_id,$new_question_id,$info,$restore) {
332 global $CFG;
334 $status = true;
336 //Get the answers array
337 if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
338 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
340 //Iterate over answers
341 for($i = 0; $i < sizeof($answers); $i++) {
342 $ans_info = $answers[$i];
343 //traverse_xmlize($ans_info); //Debug
344 //print_object ($GLOBALS['traverse_array']); //Debug
345 //$GLOBALS['traverse_array']=""; //Debug
347 //We'll need this later!!
348 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
350 //Now, build the question_answers record structure
351 $answer->question = $new_question_id;
352 $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
353 $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
354 $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
356 //The structure is equal to the db, so insert the question_answers
357 $newid = insert_record ("question_answers",$answer);
359 //Do some output
360 if (($i+1) % 50 == 0) {
361 if (!defined('RESTORE_SILENTLY')) {
362 echo ".";
363 if (($i+1) % 1000 == 0) {
364 echo "<br />";
367 backup_flush(300);
370 if ($newid) {
371 //We have the newid, update backup_ids
372 backup_putid($restore->backup_unique_code,"question_answers",$oldid,
373 $newid);
374 } else {
375 $status = false;
380 return $status;
383 function quiz_restore_pre15_map_answers ($old_question_id,$new_question_id,$info,$restore) {
385 global $CFG;
387 $status = true;
389 if (!isset($info['#']['ANSWERS'])) { // No answers in this question (eg random)
390 return $status;
393 //Get the answers array
394 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
396 //Iterate over answers
397 for($i = 0; $i < sizeof($answers); $i++) {
398 $ans_info = $answers[$i];
399 //traverse_xmlize($ans_info); //Debug
400 //print_object ($GLOBALS['traverse_array']); //Debug
401 //$GLOBALS['traverse_array']=""; //Debug
403 //We'll need this later!!
404 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
406 //Now, build the question_answers record structure
407 $answer->question = $new_question_id;
408 $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
409 $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
410 $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
412 //If we are in this method is because the question exists in DB, so its
413 //answers must exist too.
414 //Now, we are going to look for that answer in DB and to create the
415 //mappings in backup_ids to use them later where restoring responses (user level).
417 //Get the answer from DB (by question, answer and fraction)
418 $db_answer = get_record ("question_answers","question",$new_question_id,
419 "answer",$answer->answer,
420 "fraction",$answer->fraction);
422 //Do some output
423 if (($i+1) % 50 == 0) {
424 if (!defined('RESTORE_SILENTLY')) {
425 echo ".";
426 if (($i+1) % 1000 == 0) {
427 echo "<br />";
430 backup_flush(300);
433 if ($db_answer) {
434 //We have the database answer, update backup_ids
435 backup_putid($restore->backup_unique_code,"question_answers",$oldid,
436 $db_answer->id);
437 } else {
438 $status = false;
442 return $status;
445 function quiz_restore_pre15_shortanswer ($old_question_id,$new_question_id,$info,$restore,$restrictto = '') {
447 global $CFG;
449 $status = true;
451 //Get the shortanswers array
452 $shortanswers = $info['#']['SHORTANSWER'];
454 //Iterate over shortanswers
455 for($i = 0; $i < sizeof($shortanswers); $i++) {
456 $sho_info = $shortanswers[$i];
457 //traverse_xmlize($sho_info); //Debug
458 //print_object ($GLOBALS['traverse_array']); //Debug
459 //$GLOBALS['traverse_array']=""; //Debug
461 //Now, build the question_shortanswer record structure
462 $shortanswer->question = $new_question_id;
463 $shortanswer->answers = backup_todb($sho_info['#']['ANSWERS']['0']['#']);
464 $shortanswer->usecase = backup_todb($sho_info['#']['USECASE']['0']['#']);
466 //We have to recode the answers field (a list of answers id)
467 //Extracts answer id from sequence
468 $answers_field = "";
469 $in_first = true;
470 $tok = strtok($shortanswer->answers,",");
471 while ($tok) {
472 //Get the answer from backup_ids
473 $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
474 if ($answer) {
475 if ($in_first) {
476 $answers_field .= $answer->new_id;
477 $in_first = false;
478 } else {
479 $answers_field .= ",".$answer->new_id;
482 //check for next
483 $tok = strtok(",");
485 //We have the answers field recoded to its new ids
486 $shortanswer->answers = $answers_field;
488 //The structure is equal to the db, so insert the question_shortanswer
489 //Only if there aren't restrictions or there are restriction concordance
490 if (empty($restrictto) || (!empty($restrictto) && $shortanswer->answers == $restrictto)) {
491 $newid = insert_record ("question_shortanswer",$shortanswer);
494 //Do some output
495 if (($i+1) % 50 == 0) {
496 if (!defined('RESTORE_SILENTLY')) {
497 echo ".";
498 if (($i+1) % 1000 == 0) {
499 echo "<br />";
502 backup_flush(300);
505 if (!$newid && !$restrictto) {
506 $status = false;
510 return $status;
513 function quiz_restore_pre15_truefalse ($old_question_id,$new_question_id,$info,$restore) {
515 global $CFG;
517 $status = true;
519 //Get the truefalse array
520 $truefalses = $info['#']['TRUEFALSE'];
522 //Iterate over truefalse
523 for($i = 0; $i < sizeof($truefalses); $i++) {
524 $tru_info = $truefalses[$i];
525 //traverse_xmlize($tru_info); //Debug
526 //print_object ($GLOBALS['traverse_array']); //Debug
527 //$GLOBALS['traverse_array']=""; //Debug
529 //Now, build the question_truefalse record structure
530 $truefalse->question = $new_question_id;
531 $truefalse->trueanswer = backup_todb($tru_info['#']['TRUEANSWER']['0']['#']);
532 $truefalse->falseanswer = backup_todb($tru_info['#']['FALSEANSWER']['0']['#']);
534 ////We have to recode the trueanswer field
535 $answer = backup_getid($restore->backup_unique_code,"question_answers",$truefalse->trueanswer);
536 if ($answer) {
537 $truefalse->trueanswer = $answer->new_id;
540 ////We have to recode the falseanswer field
541 $answer = backup_getid($restore->backup_unique_code,"question_answers",$truefalse->falseanswer);
542 if ($answer) {
543 $truefalse->falseanswer = $answer->new_id;
546 //The structure is equal to the db, so insert the question_truefalse
547 $newid = insert_record ("question_truefalse",$truefalse);
549 //Do some output
550 if (($i+1) % 50 == 0) {
551 if (!defined('RESTORE_SILENTLY')) {
552 echo ".";
553 if (($i+1) % 1000 == 0) {
554 echo "<br />";
557 backup_flush(300);
560 if (!$newid) {
561 $status = false;
565 return $status;
568 function quiz_restore_pre15_multichoice ($old_question_id,$new_question_id,$info,$restore, $restrictto = '') {
570 global $CFG;
572 $status = true;
574 //Get the multichoices array
575 $multichoices = $info['#']['MULTICHOICE'];
577 //Iterate over multichoices
578 for($i = 0; $i < sizeof($multichoices); $i++) {
579 $mul_info = $multichoices[$i];
580 //traverse_xmlize($mul_info); //Debug
581 //print_object ($GLOBALS['traverse_array']); //Debug
582 //$GLOBALS['traverse_array']=""; //Debug
584 //Now, build the question_multichoice record structure
585 $multichoice->question = $new_question_id;
586 $multichoice->layout = backup_todb($mul_info['#']['LAYOUT']['0']['#']);
587 $multichoice->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
588 $multichoice->single = backup_todb($mul_info['#']['SINGLE']['0']['#']);
590 //We have to recode the answers field (a list of answers id)
591 //Extracts answer id from sequence
592 $answers_field = "";
593 $in_first = true;
594 $tok = strtok($multichoice->answers,",");
595 while ($tok) {
596 //Get the answer from backup_ids
597 $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
598 if ($answer) {
599 if ($in_first) {
600 $answers_field .= $answer->new_id;
601 $in_first = false;
602 } else {
603 $answers_field .= ",".$answer->new_id;
606 //check for next
607 $tok = strtok(",");
609 //We have the answers field recoded to its new ids
610 $multichoice->answers = $answers_field;
612 //The structure is equal to the db, so insert the question_shortanswer
613 //Only if there aren't restrictions or there are restriction concordance
614 if (empty($restrictto) || (!empty($restrictto) && $multichoice->answers == $restrictto)) {
615 $newid = insert_record ("question_multichoice",$multichoice);
618 //Do some output
619 if (($i+1) % 50 == 0) {
620 if (!defined('RESTORE_SILENTLY')) {
621 echo ".";
622 if (($i+1) % 1000 == 0) {
623 echo "<br />";
626 backup_flush(300);
629 if (!$newid && !$restrictto) {
630 $status = false;
634 return $status;
637 function quiz_restore_pre15_match ($old_question_id,$new_question_id,$info,$restore) {
639 global $CFG;
641 $status = true;
643 //Get the matchs array
644 $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
646 //We have to build the subquestions field (a list of match_sub id)
647 $subquestions_field = "";
648 $in_first = true;
650 //Iterate over matchs
651 for($i = 0; $i < sizeof($matchs); $i++) {
652 $mat_info = $matchs[$i];
653 //traverse_xmlize($mat_info); //Debug
654 //print_object ($GLOBALS['traverse_array']); //Debug
655 //$GLOBALS['traverse_array']=""; //Debug
657 //We'll need this later!!
658 $oldid = backup_todb($mat_info['#']['ID']['0']['#']);
660 //Now, build the question_match_SUB record structure
661 $match_sub->question = $new_question_id;
662 $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
663 $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
665 //The structure is equal to the db, so insert the question_match_sub
666 $newid = insert_record ("question_match_sub",$match_sub);
668 //Do some output
669 if (($i+1) % 50 == 0) {
670 if (!defined('RESTORE_SILENTLY')) {
671 echo ".";
672 if (($i+1) % 1000 == 0) {
673 echo "<br />";
676 backup_flush(300);
679 if ($newid) {
680 //We have the newid, update backup_ids
681 backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,
682 $newid);
683 //We have a new match_sub, append it to subquestions_field
684 if ($in_first) {
685 $subquestions_field .= $newid;
686 $in_first = false;
687 } else {
688 $subquestions_field .= ",".$newid;
690 } else {
691 $status = false;
695 //We have created every match_sub, now create the match
696 $match->question = $new_question_id;
697 $match->subquestions = $subquestions_field;
699 //The structure is equal to the db, so insert the question_match_sub
700 $newid = insert_record ("question_match",$match);
702 if (!$newid) {
703 $status = false;
706 return $status;
709 function quiz_restore_pre15_map_match ($old_question_id,$new_question_id,$info,$restore) {
711 global $CFG;
713 $status = true;
715 //Get the matchs array
716 $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];
718 //We have to build the subquestions field (a list of match_sub id)
719 $subquestions_field = "";
720 $in_first = true;
722 //Iterate over matchs
723 for($i = 0; $i < sizeof($matchs); $i++) {
724 $mat_info = $matchs[$i];
725 //traverse_xmlize($mat_info); //Debug
726 //print_object ($GLOBALS['traverse_array']); //Debug
727 //$GLOBALS['traverse_array']=""; //Debug
729 //We'll need this later!!
730 $oldid = backup_todb($mat_info['#']['ID']['0']['#']);
732 //Now, build the question_match_SUB record structure
733 $match_sub->question = $new_question_id;
734 $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);
735 $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);
737 //If we are in this method is because the question exists in DB, so its
738 //match_sub must exist too.
739 //Now, we are going to look for that match_sub in DB and to create the
740 //mappings in backup_ids to use them later where restoring responses (user level).
742 //Get the match_sub from DB (by question, questiontext and answertext)
743 $db_match_sub = get_record ("question_match_sub","question",$new_question_id,
744 "questiontext",$match_sub->questiontext,
745 "answertext",$match_sub->answertext);
746 //Do some output
747 if (($i+1) % 50 == 0) {
748 if (!defined('RESTORE_SILENTLY')) {
749 echo ".";
750 if (($i+1) % 1000 == 0) {
751 echo "<br />";
754 backup_flush(300);
757 //We have the database match_sub, so update backup_ids
758 if ($db_match_sub) {
759 //We have the newid, update backup_ids
760 backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,
761 $db_match_sub->id);
762 } else {
763 $status = false;
767 return $status;
770 function quiz_restore_pre15_randomsamatch ($old_question_id,$new_question_id,$info,$restore) {
772 global $CFG;
774 $status = true;
776 //Get the randomsamatchs array
777 $randomsamatchs = $info['#']['RANDOMSAMATCH'];
779 //Iterate over randomsamatchs
780 for($i = 0; $i < sizeof($randomsamatchs); $i++) {
781 $ran_info = $randomsamatchs[$i];
782 //traverse_xmlize($ran_info); //Debug
783 //print_object ($GLOBALS['traverse_array']); //Debug
784 //$GLOBALS['traverse_array']=""; //Debug
786 //Now, build the question_randomsamatch record structure
787 $randomsamatch->question = $new_question_id;
788 $randomsamatch->choose = backup_todb($ran_info['#']['CHOOSE']['0']['#']);
790 //The structure is equal to the db, so insert the question_randomsamatch
791 $newid = insert_record ("question_randomsamatch",$randomsamatch);
793 //Do some output
794 if (($i+1) % 50 == 0) {
795 if (!defined('RESTORE_SILENTLY')) {
796 echo ".";
797 if (($i+1) % 1000 == 0) {
798 echo "<br />";
801 backup_flush(300);
804 if (!$newid) {
805 $status = false;
809 return $status;
812 function quiz_restore_pre15_numerical ($old_question_id,$new_question_id,$info,$restore, $restrictto = '') {
814 global $CFG;
816 $status = true;
818 //Get the numerical array
819 $numericals = $info['#']['NUMERICAL'];
821 //Iterate over numericals
822 for($i = 0; $i < sizeof($numericals); $i++) {
823 $num_info = $numericals[$i];
824 //traverse_xmlize($num_info); //Debug
825 //print_object ($GLOBALS['traverse_array']); //Debug
826 //$GLOBALS['traverse_array']=""; //Debug
828 //Now, build the question_numerical record structure
829 $numerical->question = $new_question_id;
830 $numerical->answer = backup_todb($num_info['#']['ANSWER']['0']['#']);
831 $numerical->min = backup_todb($num_info['#']['MIN']['0']['#']);
832 $numerical->max = backup_todb($num_info['#']['MAX']['0']['#']);
834 ////We have to recode the answer field
835 $answer = backup_getid($restore->backup_unique_code,"question_answers",$numerical->answer);
836 if ($answer) {
837 $numerical->answer = $answer->new_id;
840 //Answer goes to answers in 1.5 (although it continues being only one!)
841 //Changed 12-05 (chating with Gustav and Julian this remains = pre15 = answer)
842 //$numerical->answers = $numerical->answer;
844 //We have to calculate the tolerance field of the numerical question
845 $numerical->tolerance = ($numerical->max - $numerical->min)/2;
847 //The structure is equal to the db, so insert the question_numerical
848 //Only if there aren't restrictions or there are restriction concordance
849 if (empty($restrictto) || (!empty($restrictto) && in_array($numerical->answer,explode(",",$restrictto)))) {
850 $newid = insert_record ("question_numerical",$numerical);
853 //Do some output
854 if (($i+1) % 50 == 0) {
855 if (!defined('RESTORE_SILENTLY')) {
856 echo ".";
857 if (($i+1) % 1000 == 0) {
858 echo "<br />";
861 backup_flush(300);
864 //Now restore numerical_units
865 if ($newid) {
866 $status = quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$num_info,$restore);
869 if (!$newid && !$restrictto) {
870 $status = false;
874 return $status;
877 function quiz_restore_pre15_calculated ($old_question_id,$new_question_id,$info,$restore) {
879 global $CFG;
881 $status = true;
883 //Get the calculated-s array
884 $calculateds = $info['#']['CALCULATED'];
886 //Iterate over calculateds
887 for($i = 0; $i < sizeof($calculateds); $i++) {
888 $cal_info = $calculateds[$i];
889 //traverse_xmlize($cal_info); //Debug
890 //print_object ($GLOBALS['traverse_array']); //Debug
891 //$GLOBALS['traverse_array']=""; //Debug
893 //Now, build the question_calculated record structure
894 $calculated->question = $new_question_id;
895 $calculated->answer = backup_todb($cal_info['#']['ANSWER']['0']['#']);
896 $calculated->tolerance = backup_todb($cal_info['#']['TOLERANCE']['0']['#']);
897 $calculated->tolerancetype = backup_todb($cal_info['#']['TOLERANCETYPE']['0']['#']);
898 $calculated->correctanswerlength = backup_todb($cal_info['#']['CORRECTANSWERLENGTH']['0']['#']);
899 $calculated->correctanswerformat = backup_todb($cal_info['#']['CORRECTANSWERFORMAT']['0']['#']);
901 ////We have to recode the answer field
902 $answer = backup_getid($restore->backup_unique_code,"question_answers",$calculated->answer);
903 if ($answer) {
904 $calculated->answer = $answer->new_id;
907 //If we haven't correctanswerformat, it defaults to 2 (in DB)
908 if (empty($calculated->correctanswerformat)) {
909 $calculated->correctanswerformat = 2;
912 //The structure is equal to the db, so insert the question_calculated
913 $newid = insert_record ("question_calculated",$calculated);
915 //Do some output
916 if (($i+1) % 50 == 0) {
917 if (!defined('RESTORE_SILENTLY')) {
918 echo ".";
919 if (($i+1) % 1000 == 0) {
920 echo "<br />";
923 backup_flush(300);
926 //Now restore numerical_units
927 $status = quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$cal_info,$restore);
929 //Now restore dataset_definitions
930 if ($status && $newid) {
931 $status = quiz_restore_pre15_dataset_definitions ($old_question_id,$new_question_id,$cal_info,$restore);
934 if (!$newid) {
935 $status = false;
939 return $status;
942 function quiz_restore_pre15_multianswer ($old_question_id,$new_question_id,$info,$restore) {
944 global $CFG;
946 $status = true;
948 //We need some question fields here so we get the full record from DB
949 $parentquestion = get_record('question','id',$new_question_id);
951 //We need to store all the positions with their created questions
952 //to be able to calculate the sequence field
953 $createdquestions = array();
955 //Under 1.5, every multianswer record becomes a question itself
956 //with its parent set to the cloze question. And there is only
957 //ONE multianswer record with the sequence of questions used.
959 //Get the multianswers array
960 $multianswers_array = $info['#']['MULTIANSWERS']['0']['#']['MULTIANSWER'];
961 //Iterate over multianswers_array
962 for($i = 0; $i < sizeof($multianswers_array); $i++) {
963 $mul_info = $multianswers_array[$i];
964 //traverse_xmlize($mul_info); //Debug
965 //print_object ($GLOBALS['traverse_array']); //Debug
966 //$GLOBALS['traverse_array']=""; //Debug
968 //We need this later
969 $oldid = backup_todb($mul_info['#']['ID']['0']['#']);
971 //Now, build the question_multianswer record structure
972 $multianswer->question = $new_question_id;
973 $multianswer->answers = backup_todb($mul_info['#']['ANSWERS']['0']['#']);
974 $multianswer->positionkey = backup_todb($mul_info['#']['POSITIONKEY']['0']['#']);
975 $multianswer->answertype = backup_todb($mul_info['#']['ANSWERTYPE']['0']['#']);
976 $multianswer->norm = backup_todb($mul_info['#']['NORM']['0']['#']);
978 //Saving multianswer and positionkey to use them later restoring states
979 backup_putid ($restore->backup_unique_code,'multianswer-pos',$oldid,$multianswer->positionkey);
981 //We have to recode all the answers to their new ids
982 $ansarr = explode(",", $multianswer->answers);
983 foreach ($ansarr as $key => $value) {
984 //Get the answer from backup_ids
985 $answer = backup_getid($restore->backup_unique_code,'question_answers',$value);
986 $ansarr[$key] = $answer->new_id;
988 $multianswer->answers = implode(",",$ansarr);
990 //Build the new question structure
991 $question = new object;
992 $question->category = $parentquestion->category;
993 $question->parent = $parentquestion->id;
994 $question->name = $parentquestion->name;
995 $question->questiontextformat = $parentquestion->questiontextformat;
996 $question->defaultgrade = $multianswer->norm;
997 $question->penalty = $parentquestion->penalty;
998 $question->qtype = $multianswer->answertype;
999 $question->version = $parentquestion->version;
1000 $question->hidden = $parentquestion->hidden;
1001 $question->length = 0;
1002 $question->questiontext = '';
1003 $question->stamp = make_unique_id_code();
1005 //Save the new question to DB
1006 $newid = insert_record('question', $question);
1008 if ($newid) {
1009 $createdquestions[$multianswer->positionkey] = $newid;
1012 //Do some output
1013 if (($i+1) % 50 == 0) {
1014 if (!defined('RESTORE_SILENTLY')) {
1015 echo ".";
1016 if (($i+1) % 1000 == 0) {
1017 echo "<br />";
1020 backup_flush(300);
1023 //Remap question_answers records from the original multianswer question
1024 //to their newly created question
1025 if ($newid) {
1026 $answersdb = get_records_list('question_answers','id',$multianswer->answers);
1027 foreach ($answersdb as $answerdb) {
1028 set_field('question_answers','question',$newid,'id',$answerdb->id);
1032 //If we have created the question record, now, depending of the
1033 //answertype, delegate the restore to every qtype function
1034 if ($newid) {
1035 if ($multianswer->answertype == "1") {
1036 $status = quiz_restore_pre15_shortanswer ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers);
1037 } else if ($multianswer->answertype == "3") {
1038 $status = quiz_restore_pre15_multichoice ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers);
1039 } else if ($multianswer->answertype == "8") {
1040 $status = quiz_restore_pre15_numerical ($old_question_id,$newid,$mul_info,$restore,$multianswer->answers);
1042 } else {
1043 $status = false;
1047 //Everything is created, just going to create the multianswer record
1048 if ($status) {
1049 ksort($createdquestions);
1051 $multianswerdb = new object;
1052 $multianswerdb->question = $parentquestion->id;
1053 $multianswerdb->sequence = implode(",",$createdquestions);
1054 $mid = insert_record('question_multianswer', $multianswerdb);
1056 if (!$mid) {
1057 $status = false;
1061 return $status;
1064 function quiz_restore_pre15_numerical_units ($old_question_id,$new_question_id,$info,$restore) {
1066 global $CFG;
1068 $status = true;
1070 //Get the numerical array
1071 $numerical_units = $info['#']['NUMERICAL_UNITS']['0']['#']['NUMERICAL_UNIT'];
1073 //Iterate over numerical_units
1074 for($i = 0; $i < sizeof($numerical_units); $i++) {
1075 $nu_info = $numerical_units[$i];
1076 //traverse_xmlize($nu_info); //Debug
1077 //print_object ($GLOBALS['traverse_array']); //Debug
1078 //$GLOBALS['traverse_array']=""; //Debug
1080 //Now, build the question_numerical_UNITS record structure
1081 $numerical_unit->question = $new_question_id;
1082 $numerical_unit->multiplier = backup_todb($nu_info['#']['MULTIPLIER']['0']['#']);
1083 $numerical_unit->unit = backup_todb($nu_info['#']['UNIT']['0']['#']);
1085 //The structure is equal to the db, so insert the question_numerical_units
1086 $newid = insert_record ("question_numerical_units",$numerical_unit);
1088 if (!$newid) {
1089 $status = false;
1093 return $status;
1096 function quiz_restore_pre15_dataset_definitions ($old_question_id,$new_question_id,$info,$restore) {
1098 global $CFG;
1100 $status = true;
1102 //Get the dataset_definitions array
1103 $dataset_definitions = $info['#']['DATASET_DEFINITIONS']['0']['#']['DATASET_DEFINITION'];
1105 //Iterate over dataset_definitions
1106 for($i = 0; $i < sizeof($dataset_definitions); $i++) {
1107 $dd_info = $dataset_definitions[$i];
1108 //traverse_xmlize($dd_info); //Debug
1109 //print_object ($GLOBALS['traverse_array']); //Debug
1110 //$GLOBALS['traverse_array']=""; //Debug
1112 //Now, build the question_dataset_DEFINITION record structure
1113 $dataset_definition->category = backup_todb($dd_info['#']['CATEGORY']['0']['#']);
1114 $dataset_definition->name = backup_todb($dd_info['#']['NAME']['0']['#']);
1115 $dataset_definition->type = backup_todb($dd_info['#']['TYPE']['0']['#']);
1116 $dataset_definition->options = backup_todb($dd_info['#']['OPTIONS']['0']['#']);
1117 $dataset_definition->itemcount = backup_todb($dd_info['#']['ITEMCOUNT']['0']['#']);
1119 //We have to recode the category field (only if the category != 0)
1120 if ($dataset_definition->category != 0) {
1121 $category = backup_getid($restore->backup_unique_code,"question_categories",$dataset_definition->category);
1122 if ($category) {
1123 $dataset_definition->category = $category->new_id;
1127 //Now, we hace to decide when to create the new records or reuse an existing one
1128 $create_definition = false;
1130 //If the dataset_definition->category = 0, it's a individual question dataset_definition, so we'll create it
1131 if ($dataset_definition->category == 0) {
1132 $create_definition = true;
1133 } else {
1134 //The category isn't 0, so it's a category question dataset_definition, we have to see if it exists
1135 //Look for a definition with the same category, name and type
1136 if ($definitionrec = get_record_sql("SELECT d.*
1137 FROM {$CFG->prefix}question_dataset_definitions d
1138 WHERE d.category = '$dataset_definition->category' AND
1139 d.name = '$dataset_definition->name' AND
1140 d.type = '$dataset_definition->type'")) {
1141 //Such dataset_definition exist. Now we must check if it has enough itemcount
1142 if ($definitionrec->itemcount < $dataset_definition->itemcount) {
1143 //We haven't enough itemcount, so we have to create the definition as an individual question one.
1144 $dataset_definition->category = 0;
1145 $create_definition = true;
1146 } else {
1147 //We have enough itemcount, so we'll reuse the existing definition
1148 $create_definition = false;
1149 $newid = $definitionrec->id;
1151 } else {
1152 //Such dataset_definition doesn't exist. We'll create it.
1153 $create_definition = true;
1157 //If we've to create the definition, do it
1158 if ($create_definition) {
1159 //The structure is equal to the db, so insert the question_dataset_definitions
1160 $newid = insert_record ("question_dataset_definitions",$dataset_definition);
1161 if ($newid) {
1162 //Restore question_dataset_items
1163 $status = quiz_restore_pre15_dataset_items($newid,$dd_info,$restore);
1167 //Now, we must have a definition (created o reused). Its id is in newid. Create the question_datasets record
1168 //to join the question and the dataset_definition
1169 if ($newid) {
1170 $question_dataset->question = $new_question_id;
1171 $question_dataset->datasetdefinition = $newid;
1172 $newid = insert_record ("question_datasets",$question_dataset);
1175 if (!$newid) {
1176 $status = false;
1180 return $status;
1183 function quiz_restore_pre15_dataset_items ($definitionid,$info,$restore) {
1185 global $CFG;
1187 $status = true;
1189 //Get the items array
1190 $dataset_items = $info['#']['DATASET_ITEMS']['0']['#']['DATASET_ITEM'];
1192 //Iterate over dataset_items
1193 for($i = 0; $i < sizeof($dataset_items); $i++) {
1194 $di_info = $dataset_items[$i];
1195 //traverse_xmlize($di_info); //Debug
1196 //print_object ($GLOBALS['traverse_array']); //Debug
1197 //$GLOBALS['traverse_array']=""; //Debug
1199 //Now, build the question_dataset_ITEMS record structure
1200 $dataset_item->definition = $definitionid;
1201 $dataset_item->itemnumber = backup_todb($di_info['#']['NUMBER']['0']['#']);
1202 $dataset_item->value = backup_todb($di_info['#']['VALUE']['0']['#']);
1204 //The structure is equal to the db, so insert the question_dataset_items
1205 $newid = insert_record ("question_dataset_items",$dataset_item);
1207 if (!$newid) {
1208 $status = false;
1212 return $status;
1215 //STEP 2. Restore quizzes and associated structures
1216 // (course dependent)
1217 function quiz_restore_pre15_mods($mod,$restore) {
1219 global $CFG;
1221 $status = true;
1223 //Get record from backup_ids
1224 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
1226 if ($data) {
1227 //Now get completed xmlized object
1228 $info = $data->info;
1229 //if necessary, write to restorelog and adjust date/time fields
1230 if ($restore->course_startdateoffset) {
1231 restore_log_date_changes('Quiz', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
1233 //traverse_xmlize($info); //Debug
1234 //print_object ($GLOBALS['traverse_array']); //Debug
1235 //$GLOBALS['traverse_array']=""; //Debug
1237 //Now, build the QUIZ record structure
1238 $quiz->course = $restore->course_id;
1239 $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
1240 $quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
1241 $quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
1242 $quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
1243 $quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
1244 $quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
1245 $quiz->feedback = backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']);
1246 $quiz->correctanswers = backup_todb($info['MOD']['#']['CORRECTANSWERS']['0']['#']);
1247 $quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
1248 if (isset($info['MOD']['#']['DECIMALPOINTS']['0']['#'])) { //Only if it's set, to apply DB default else.
1249 $quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
1251 $quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
1252 $quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['0']['#']);
1253 $quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
1254 $quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
1255 $quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
1256 $quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
1257 $quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
1258 $quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
1259 $quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
1260 $quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
1261 $quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
1262 $quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
1263 $quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
1265 //We have to recode the questions field (a list of questions id)
1266 $newquestions = array();
1267 if ($questionsarr = explode (",",$quiz->questions)) {
1268 foreach ($questionsarr as $key => $value) {
1269 if ($question = backup_getid($restore->backup_unique_code,"question",$value)) {
1270 $newquestions[] = $question->new_id;
1274 $quiz->questions = implode (",", $newquestions);
1276 //Recalculate the questions field to include page breaks if necessary
1277 $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
1279 //Calculate the new review field contents (logic extracted from upgrade)
1280 $review = (QUIZ_REVIEW_IMMEDIATELY & (QUIZ_REVIEW_RESPONSES + QUIZ_REVIEW_SCORES));
1281 if ($quiz->feedback) {
1282 $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_FEEDBACK);
1284 if ($quiz->correctanswers) {
1285 $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_ANSWERS);
1287 if ($quiz->review & 1) {
1288 $review += QUIZ_REVIEW_CLOSED;
1290 if ($quiz->review & 2) {
1291 $review += QUIZ_REVIEW_OPEN;
1293 $quiz->review = $review;
1295 //The structure is equal to the db, so insert the quiz
1296 $newid = insert_record ("quiz",$quiz);
1298 //Do some output
1299 if (!defined('RESTORE_SILENTLY')) {
1300 echo "<li>".get_string("modulename","quiz")." \"".format_string(stripslashes($quiz->name),true)."\"</li>";
1302 backup_flush(300);
1304 if ($newid) {
1305 //We have the newid, update backup_ids
1306 backup_putid($restore->backup_unique_code,$mod->modtype,
1307 $mod->id, $newid);
1308 //We have to restore the quiz_question_instances now (old quiz_question_grades, course level)
1309 $status = quiz_question_instances_restore_pre15_mods($newid,$info,$restore);
1310 //We have to restore the question_versions now (course level table)
1311 $status = quiz_question_versions_restore_pre15_mods($newid,$info,$restore);
1312 //Now check if want to restore user data and do it.
1313 if (restore_userdata_selected($restore,'quiz',$mod->id)) {
1314 //Restore quiz_attempts
1315 $status = quiz_attempts_restore_pre15_mods ($newid,$info,$restore, $quiz->questions);
1316 if ($status) {
1317 //Restore quiz_grades
1318 $status = quiz_grades_restore_pre15_mods ($newid,$info,$restore);
1321 } else {
1322 $status = false;
1324 } else {
1325 $status = false;
1328 return $status;
1331 //This function restores the quiz_question_instances (old quiz_question_grades)
1332 function quiz_question_instances_restore_pre15_mods($quiz_id,$info,$restore) {
1334 global $CFG;
1336 $status = true;
1338 //Get the quiz_question_grades array
1339 $grades = $info['MOD']['#']['QUESTION_GRADES']['0']['#']['QUESTION_GRADE'];
1341 //Iterate over question_grades
1342 for($i = 0; $i < sizeof($grades); $i++) {
1343 $gra_info = $grades[$i];
1344 //traverse_xmlize($gra_info); //Debug
1345 //print_object ($GLOBALS['traverse_array']); //Debug
1346 //$GLOBALS['traverse_array']=""; //Debug
1348 //We'll need this later!!
1349 $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
1351 //Now, build the QUESTION_GRADES record structure
1352 $grade->quiz = $quiz_id;
1353 $grade->question = backup_todb($gra_info['#']['QUESTION']['0']['#']);
1354 $grade->grade = backup_todb($gra_info['#']['GRADE']['0']['#']);
1356 //We have to recode the question field
1357 $question = backup_getid($restore->backup_unique_code,"question",$grade->question);
1358 if ($question) {
1359 $grade->question = $question->new_id;
1362 //The structure is equal to the db, so insert the quiz_question_grades
1363 $newid = insert_record ("quiz_question_instances",$grade);
1365 //Do some output
1366 if (($i+1) % 10 == 0) {
1367 if (!defined('RESTORE_SILENTLY')) {
1368 echo ".";
1369 if (($i+1) % 200 == 0) {
1370 echo "<br />";
1373 backup_flush(300);
1376 if ($newid) {
1377 //We have the newid, update backup_ids
1378 backup_putid($restore->backup_unique_code,"quiz_question_instances",$oldid,
1379 $newid);
1380 } else {
1381 $status = false;
1385 return $status;
1388 //This function restores the quiz_question_versions
1389 function quiz_question_versions_restore_pre15_mods($quiz_id,$info,$restore) {
1391 global $CFG, $USER;
1393 $status = true;
1395 //Get the quiz_question_versions array
1396 $versions = $info['MOD']['#']['QUESTION_VERSIONS']['0']['#']['QUESTION_VERSION'];
1398 //Iterate over question_versions
1399 for($i = 0; $i < sizeof($versions); $i++) {
1400 $ver_info = $versions[$i];
1401 //traverse_xmlize($ver_info); //Debug
1402 //print_object ($GLOBALS['traverse_array']); //Debug
1403 //$GLOBALS['traverse_array']=""; //Debug
1405 //We'll need this later!!
1406 $oldid = backup_todb($ver_info['#']['ID']['0']['#']);
1408 //Now, build the QUESTION_VERSIONS record structure
1409 $version->quiz = $quiz_id;
1410 $version->oldquestion = backup_todb($ver_info['#']['OLDQUESTION']['0']['#']);
1411 $version->newquestion = backup_todb($ver_info['#']['NEWQUESTION']['0']['#']);
1412 $version->userid = backup_todb($ver_info['#']['USERID']['0']['#']);
1413 $version->timestamp = backup_todb($ver_info['#']['TIMESTAMP']['0']['#']);
1415 //We have to recode the oldquestion field
1416 $question = backup_getid($restore->backup_unique_code,"question",$version->oldquestion);
1417 if ($question) {
1418 $version->oldquestion = $question->new_id;
1421 //We have to recode the newquestion field
1422 $question = backup_getid($restore->backup_unique_code,"question",$version->newquestion);
1423 if ($question) {
1424 $version->newquestion = $question->new_id;
1427 //We have to recode the userid field
1428 $user = backup_getid($restore->backup_unique_code,"user",$version->userid);
1429 if ($user) {
1430 $version->userid = $user->new_id;
1431 } else { //Assign to current user
1432 $version->userid = $USER->id;
1435 //The structure is equal to the db, so insert the quiz_question_versions
1436 $newid = insert_record ("quiz_question_versions",$version);
1438 //Do some output
1439 if (($i+1) % 10 == 0) {
1440 if (!defined('RESTORE_SILENTLY')) {
1441 echo ".";
1442 if (($i+1) % 200 == 0) {
1443 echo "<br />";
1446 backup_flush(300);
1449 if ($newid) {
1450 //We have the newid, update backup_ids
1451 backup_putid($restore->backup_unique_code,"quiz_question_versions",$oldid,
1452 $newid);
1453 } else {
1454 $status = false;
1458 return $status;
1461 //This function restores the quiz_attempts
1462 function quiz_attempts_restore_pre15_mods($quiz_id,$info,$restore,$quizquestions) {
1464 global $CFG;
1466 $status = true;
1468 //Get the quiz_attempts array
1469 $attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
1471 //Iterate over attempts
1472 for($i = 0; $i < sizeof($attempts); $i++) {
1473 $att_info = $attempts[$i];
1474 //traverse_xmlize($att_info); //Debug
1475 //print_object ($GLOBALS['traverse_array']); //Debug
1476 //$GLOBALS['traverse_array']=""; //Debug
1478 //We'll need this later!!
1479 $oldid = backup_todb($att_info['#']['ID']['0']['#']);
1480 $olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
1482 //Now, build the ATTEMPTS record structure
1483 $attempt->quiz = $quiz_id;
1484 $attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
1485 $attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
1486 $attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
1487 $attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
1488 $attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
1489 $attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
1491 //We have to recode the userid field
1492 $user = backup_getid($restore->backup_unique_code,"user",$attempt->userid);
1493 if ($user) {
1494 $attempt->userid = $user->new_id;
1497 //Set the layout field (inherited from quiz by default)
1498 $attempt->layout = $quizquestions;
1500 //Set the preview field (code from upgrade)
1501 $cm = get_coursemodule_from_instance('quiz', $quiz_id);
1502 if (has_capability('mod/quiz:preview', get_context_instance(CONTEXT_MODULE, $cm->id))) {
1503 $attempt->preview = 1;
1506 //Set the uniqueid field
1507 $attempt->uniqueid = question_new_attempt_uniqueid();
1509 //The structure is equal to the db, so insert the quiz_attempts
1510 $newid = insert_record ("quiz_attempts",$attempt);
1512 //Do some output
1513 if (($i+1) % 10 == 0) {
1514 if (!defined('RESTORE_SILENTLY')) {
1515 echo ".";
1516 if (($i+1) % 200 == 0) {
1517 echo "<br />";
1520 backup_flush(300);
1523 if ($newid) {
1524 //We have the newid, update backup_ids
1525 backup_putid($restore->backup_unique_code,"quiz_attempts",$oldid,
1526 $newid);
1527 //Now process question_states (old quiz_responses table)
1528 $status = question_states_restore_pre15_mods($newid,$att_info,$restore);
1529 } else {
1530 $status = false;
1534 return $status;
1537 //This function restores the question_states (old quiz_responses)
1538 function question_states_restore_pre15_mods($attempt_id,$info,$restore) {
1540 global $CFG;
1542 $status = true;
1544 //Get the quiz_responses array
1545 $responses = $info['#']['RESPONSES']['0']['#']['RESPONSE'];
1546 //Iterate over responses
1547 for($i = 0; $i < sizeof($responses); $i++) {
1548 $res_info = $responses[$i];
1549 //traverse_xmlize($res_info); //Debug
1550 //print_object ($GLOBALS['traverse_array']); //Debug
1551 //$GLOBALS['traverse_array']=""; //Debug
1553 //We'll need this later!!
1554 $oldid = backup_todb($res_info['#']['ID']['0']['#']);
1556 //Now, build the RESPONSES record structure
1557 $response->attempt = $attempt_id;
1558 $response->question = backup_todb($res_info['#']['QUESTION']['0']['#']);
1559 $response->originalquestion = backup_todb($res_info['#']['ORIGINALQUESTION']['0']['#']);
1560 $response->answer = backup_todb($res_info['#']['ANSWER']['0']['#']);
1561 $response->grade = backup_todb($res_info['#']['GRADE']['0']['#']);
1563 //We have to recode the question field
1564 $question = backup_getid($restore->backup_unique_code,"question",$response->question);
1565 if ($question) {
1566 $response->question = $question->new_id;
1569 //We have to recode the originalquestion field
1570 $question = backup_getid($restore->backup_unique_code,"question",$response->originalquestion);
1571 if ($question) {
1572 $response->originalquestion = $question->new_id;
1575 //Set the raw_grade field (default to the existing grade one, no penalty in pre15 backups)
1576 $response->raw_grade = $response->grade;
1578 //We have to recode the answer field
1579 //It depends of the question type !!
1580 //We get the question first
1581 $question = get_record("question","id",$response->question);
1582 //It exists
1583 if ($question) {
1584 //Depending of the qtype, we make different recodes
1585 switch ($question->qtype) {
1586 case 1: //SHORTANSWER QTYPE
1587 //Nothing to do. The response is a text.
1588 break;
1589 case 2: //TRUEFALSE QTYPE
1590 //The answer is one answer id. We must recode it
1591 $answer = backup_getid($restore->backup_unique_code,"question_answers",$response->answer);
1592 if ($answer) {
1593 $response->answer = $answer->new_id;
1595 break;
1596 case 3: //MULTICHOICE QTYPE
1597 //The answer is a comma separated list of answers. We must recode them
1598 $answer_field = "";
1599 $in_first = true;
1600 $tok = strtok($response->answer,",");
1601 while ($tok) {
1602 //Get the answer from backup_ids
1603 $answer = backup_getid($restore->backup_unique_code,"question_answers",$tok);
1604 if ($answer) {
1605 if ($in_first) {
1606 $answer_field .= $answer->new_id;
1607 $in_first = false;
1608 } else {
1609 $answer_field .= ",".$answer->new_id;
1612 //check for next
1613 $tok = strtok(",");
1615 $response->answer = $answer_field;
1616 break;
1617 case 4: //RANDOM QTYPE
1618 //The answer links to another question id, we must recode it
1619 $answer_link = backup_getid($restore->backup_unique_code,"question",$response->answer);
1620 if ($answer_link) {
1621 $response->answer = $answer_link->new_id;
1623 break;
1624 case 5: //MATCH QTYPE
1625 //The answer is a comma separated list of hypen separated math_subs (for question and answer)
1626 $answer_field = "";
1627 $in_first = true;
1628 $tok = strtok($response->answer,",");
1629 while ($tok) {
1630 //Extract the match_sub for the question and the answer
1631 $exploded = explode("-",$tok);
1632 $match_question_id = $exploded[0];
1633 $match_answer_id = $exploded[1];
1634 //Get the match_sub from backup_ids (for the question)
1635 $match_que = backup_getid($restore->backup_unique_code,"question_match_sub",$match_question_id);
1636 //Get the match_sub from backup_ids (for the answer)
1637 $match_ans = backup_getid($restore->backup_unique_code,"question_match_sub",$match_answer_id);
1638 if ($match_que) {
1639 //It the question hasn't response, it must be 0
1640 if (!$match_ans and $match_answer_id == 0) {
1641 $match_ans->new_id = 0;
1643 if ($in_first) {
1644 $answer_field .= $match_que->new_id."-".$match_ans->new_id;
1645 $in_first = false;
1646 } else {
1647 $answer_field .= ",".$match_que->new_id."-".$match_ans->new_id;
1650 //check for next
1651 $tok = strtok(",");
1653 $response->answer = $answer_field;
1654 break;
1655 case 6: //RANDOMSAMATCH QTYPE
1656 //The answer is a comma separated list of hypen separated question_id and answer_id. We must recode them
1657 $answer_field = "";
1658 $in_first = true;
1659 $tok = strtok($response->answer,",");
1660 while ($tok) {
1661 //Extract the question_id and the answer_id
1662 $exploded = explode("-",$tok);
1663 $question_id = $exploded[0];
1664 $answer_id = $exploded[1];
1665 //Get the question from backup_ids
1666 $que = backup_getid($restore->backup_unique_code,"question",$question_id);
1667 //Get the answer from backup_ids
1668 $ans = backup_getid($restore->backup_unique_code,"question_answers",$answer_id);
1669 if ($que) {
1670 //It the question hasn't response, it must be 0
1671 if (!$ans and $answer_id == 0) {
1672 $ans->new_id = 0;
1674 if ($in_first) {
1675 $answer_field .= $que->new_id."-".$ans->new_id;
1676 $in_first = false;
1677 } else {
1678 $answer_field .= ",".$que->new_id."-".$ans->new_id;
1681 //check for next
1682 $tok = strtok(",");
1684 $response->answer = $answer_field;
1685 break;
1686 case 7: //DESCRIPTION QTYPE
1687 //Nothing to do (there is no awser to this qtype)
1688 //But this case must exist !!
1689 break;
1690 case 8: //NUMERICAL QTYPE
1691 //Nothing to do. The response is a text.
1692 break;
1693 case 9: //MULTIANSWER QTYPE
1694 //The answer is a comma separated list of hypen separated multianswer ids and answers. We must recode them.
1695 //We need to have the sequence of questions here to be able to detect qtypes
1696 $multianswerdb = get_record('question_multianswer','question',$response->question);
1697 //Make an array of sequence to easy access
1698 $sequencearr = explode(",",$multianswerdb->sequence);
1699 $answer_field = "";
1700 $in_first = true;
1701 $tok = strtok($response->answer,",");
1702 $counter = 1;
1703 while ($tok) {
1704 //Extract the multianswer_id and the answer
1705 $exploded = explode("-",$tok);
1706 $multianswer_id = $exploded[0];
1707 $answer = $exploded[1];
1708 //Get position key (if it fails, next iteration)
1709 if ($oldposrec = backup_getid($restore->backup_unique_code,'multianswer-pos',$multianswer_id)) {
1710 $positionkey = $oldposrec->new_id;
1711 } else {
1712 //Next iteration
1713 $tok = strtok(",");
1714 continue;
1716 //Calculate question type
1717 $questiondb = get_record('question','id',$sequencearr[$counter-1]);
1718 $questiontype = $questiondb->qtype;
1719 //Now, depending of the answertype field in question_multianswer
1720 //we do diferent things
1721 if ($questiontype == "1") {
1722 //Shortanswer
1723 //The answer is text, do nothing
1724 } else if ($questiontype == "3") {
1725 //Multichoice
1726 //The answer is an answer_id, look for it in backup_ids
1727 $ans = backup_getid($restore->backup_unique_code,"question_answers",$answer);
1728 $answer = $ans->new_id;
1729 } else if ($questiontype == "8") {
1730 //Numeric
1731 //The answer is text, do nothing
1734 //Finaly, build the new answer field for each pair
1735 if ($in_first) {
1736 $answer_field .= $positionkey."-".$answer;
1737 $in_first = false;
1738 } else {
1739 $answer_field .= ",".$positionkey."-".$answer;
1741 //check for next
1742 $tok = strtok(",");
1743 $counter++;
1745 $response->answer = $answer_field;
1746 break;
1747 case 10: //CALCULATED QTYPE
1748 //Nothing to do. The response is a text.
1749 break;
1750 default: //UNMATCHED QTYPE.
1751 //This is an error (unimplemented qtype)
1752 $status = false;
1753 break;
1755 } else {
1756 $status = false;
1759 //The structure is equal to the db, so insert the question_states
1760 $newid = insert_record ("question_states",$response);
1762 //Do some output
1763 if (($i+1) % 10 == 0) {
1764 if (!defined('RESTORE_SILENTLY')) {
1765 echo ".";
1766 if (($i+1) % 200 == 0) {
1767 echo "<br />";
1770 backup_flush(300);
1773 if ($newid) {
1774 //We have the newid, update backup_ids
1775 backup_putid($restore->backup_unique_code,"question_states",$oldid,
1776 $newid);
1777 } else {
1778 $status = false;
1782 return $status;
1785 //This function restores the quiz_grades
1786 function quiz_grades_restore_pre15_mods($quiz_id,$info,$restore) {
1788 global $CFG;
1790 $status = true;
1792 //Get the quiz_grades array
1793 $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
1795 //Iterate over grades
1796 for($i = 0; $i < sizeof($grades); $i++) {
1797 $gra_info = $grades[$i];
1798 //traverse_xmlize($gra_info); //Debug
1799 //print_object ($GLOBALS['traverse_array']); //Debug
1800 //$GLOBALS['traverse_array']=""; //Debug
1802 //We'll need this later!!
1803 $oldid = backup_todb($gra_info['#']['ID']['0']['#']);
1804 $olduserid = backup_todb($gra_info['#']['USERID']['0']['#']);
1806 //Now, build the GRADES record structure
1807 $grade->quiz = $quiz_id;
1808 $grade->userid = backup_todb($gra_info['#']['USERID']['0']['#']);
1809 $grade->grade = backup_todb($gra_info['#']['GRADEVAL']['0']['#']);
1810 $grade->timemodified = backup_todb($gra_info['#']['TIMEMODIFIED']['0']['#']);
1812 //We have to recode the userid field
1813 $user = backup_getid($restore->backup_unique_code,"user",$grade->userid);
1814 if ($user) {
1815 $grade->userid = $user->new_id;
1818 //The structure is equal to the db, so insert the quiz_grades
1819 $newid = insert_record ("quiz_grades",$grade);
1821 //Do some output
1822 if (($i+1) % 10 == 0) {
1823 if (!defined('RESTORE_SILENTLY')) {
1824 echo ".";
1825 if (($i+1) % 200 == 0) {
1826 echo "<br />";
1829 backup_flush(300);
1832 if ($newid) {
1833 //We have the newid, update backup_ids
1834 backup_putid($restore->backup_unique_code,"quiz_grades",$oldid,
1835 $newid);
1836 } else {
1837 $status = false;
1841 return $status;
1844 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
1845 //some texts in the module
1846 function quiz_restore_pre15_wiki2markdown ($restore) {
1848 global $CFG;
1850 $status = true;
1852 //Convert question->questiontext
1853 if ($records = get_records_sql ("SELECT q.id, q.questiontext, q.questiontextformat
1854 FROM {$CFG->prefix}question q,
1855 {$CFG->prefix}backup_ids b
1856 WHERE b.backup_code = $restore->backup_unique_code AND
1857 b.table_name = 'question' AND
1858 q.id = b.new_id AND
1859 q.questiontextformat = ".FORMAT_WIKI)) {
1860 foreach ($records as $record) {
1861 //Rebuild wiki links
1862 $record->questiontext = restore_decode_wiki_content($record->questiontext, $restore);
1863 //Convert to Markdown
1864 $wtm = new WikiToMarkdown();
1865 $record->questiontext = $wtm->convert($record->questiontext, $restore->course_id);
1866 $record->questiontextformat = FORMAT_MARKDOWN;
1867 $status = update_record('question', addslashes_object($record));
1868 //Do some output
1869 $i++;
1870 if (($i+1) % 1 == 0) {
1871 if (!defined('RESTORE_SILENTLY')) {
1872 echo ".";
1873 if (($i+1) % 20 == 0) {
1874 echo "<br />";
1877 backup_flush(300);
1881 return $status;
1884 //This function returns a log record with all the necessay transformations
1885 //done. It's used by restore_log_module() to restore modules log.
1886 function quiz_restore_pre15_logs($restore,$log) {
1888 $status = false;
1890 //Depending of the action, we recode different things
1891 switch ($log->action) {
1892 case "add":
1893 if ($log->cmid) {
1894 //Get the new_id of the module (to recode the info field)
1895 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1896 if ($mod) {
1897 $log->url = "view.php?id=".$log->cmid;
1898 $log->info = $mod->new_id;
1899 $status = true;
1902 break;
1903 case "update":
1904 if ($log->cmid) {
1905 //Get the new_id of the module (to recode the info field)
1906 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1907 if ($mod) {
1908 $log->url = "view.php?id=".$log->cmid;
1909 $log->info = $mod->new_id;
1910 $status = true;
1913 break;
1914 case "view":
1915 if ($log->cmid) {
1916 //Get the new_id of the module (to recode the info field)
1917 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1918 if ($mod) {
1919 $log->url = "view.php?id=".$log->cmid;
1920 $log->info = $mod->new_id;
1921 $status = true;
1924 break;
1925 case "view all":
1926 $log->url = "index.php?id=".$log->course;
1927 $status = true;
1928 break;
1929 case "report":
1930 if ($log->cmid) {
1931 //Get the new_id of the module (to recode the info field)
1932 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1933 if ($mod) {
1934 $log->url = "report.php?id=".$log->cmid;
1935 $log->info = $mod->new_id;
1936 $status = true;
1939 break;
1940 case "attempt":
1941 if ($log->cmid) {
1942 //Get the new_id of the module (to recode the info field)
1943 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1944 if ($mod) {
1945 //Extract the attempt id from the url field
1946 $attid = substr(strrchr($log->url,"="),1);
1947 //Get the new_id of the attempt (to recode the url field)
1948 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
1949 if ($att) {
1950 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
1951 $log->info = $mod->new_id;
1952 $status = true;
1956 break;
1957 case "submit":
1958 if ($log->cmid) {
1959 //Get the new_id of the module (to recode the info field)
1960 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1961 if ($mod) {
1962 //Extract the attempt id from the url field
1963 $attid = substr(strrchr($log->url,"="),1);
1964 //Get the new_id of the attempt (to recode the url field)
1965 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
1966 if ($att) {
1967 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
1968 $log->info = $mod->new_id;
1969 $status = true;
1973 break;
1974 case "review":
1975 if ($log->cmid) {
1976 //Get the new_id of the module (to recode the info field)
1977 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1978 if ($mod) {
1979 //Extract the attempt id from the url field
1980 $attid = substr(strrchr($log->url,"="),1);
1981 //Get the new_id of the attempt (to recode the url field)
1982 $att = backup_getid($restore->backup_unique_code,"quiz_attempts",$attid);
1983 if ($att) {
1984 $log->url = "review.php?id=".$log->cmid."&attempt=".$att->new_id;
1985 $log->info = $mod->new_id;
1986 $status = true;
1990 break;
1991 case "editquestions":
1992 if ($log->cmid) {
1993 //Get the new_id of the module (to recode the url field)
1994 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
1995 if ($mod) {
1996 $log->url = "view.php?id=".$log->cmid;
1997 $log->info = $mod->new_id;
1998 $status = true;
2001 break;
2002 default:
2003 if (!defined('RESTORE_SILENTLY')) {
2004 echo "action (".$log->module."-".$log->action.") unknow. Not restored<br />"; //Debug
2006 break;
2009 if ($status) {
2010 $status = $log;
2012 return $status;