3 * Question bank restore code.
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package questionbank
10 // the restoration of the parent and sortorder fields in the category table needs
11 // a lot more thought. We should probably use a library function to add the category
12 // rather than just writing it to the database
14 // whereever it says "/// We have to recode the .... field" we should put in a check
15 // to see if the recoding was successful and throw an appropriate error otherwise
17 //This is the "graphical" structure of the question database:
18 //To see, put your terminal to 160cc
20 // The following holds student-independent information about the questions
22 // question_categories
26 // |.......................................
29 // | -------question_datasets------ .
30 // | | (CL,pk->id,fk->question, | .
31 // | | fk->dataset_definition) | .
35 // | | question_dataset_definitions
36 // | | (CL,pk->id,fk->category)
38 // (CL,pk->id,fk->category,files) |
39 // | question_dataset_items
40 // | (CL,pk->id,fk->definition)
44 // --------------------------------------------------------------------------------------------------------------
47 // | | | | question_calculated | |
48 // question_truefalse | question_multichoice | (CL,pl->id,fk->question) | |
49 // (CL,pk->id,fk->question) | (CL,pk->id,fk->question) | . | | question_randomsamatch
50 // . | . | . | |--(CL,pk->id,fk->question)
51 // . question_shortanswer . question_numerical . question_multianswer. |
52 // . (CL,pk->id,fk->question) . (CL,pk->id,fk->question) . (CL,pk->id,fk->question) |
53 // . . . . . . | question_match
54 // . . . . . . |--(CL,pk->id,fk->question)
58 // . . . . . . | question_match_sub
59 // ........................................................................................ |--(CL,pk->id,fk->question)
62 // . | question_numerical_units
63 // question_answers |--(CL,pk->id,fk->question)
64 // (CL,pk->id,fk->question)----------------------------------------------------------
67 // The following holds the information about student interaction with the questions
70 // (UL,pk->id,fk->attempt,question)
74 // (UL,pk->id,fk->attempt,question)
76 // Meaning: pk->primary key field of the table
77 // fk->foreign key to link with parent
78 // nt->nested field (recursive data)
79 // SL->site level info
80 // CL->course level info
81 // UL->user level info
82 // files->table may have files
84 //-----------------------------------------------------------
86 include_once($CFG->libdir
.'/questionlib.php');
88 function restore_question_categories($category,$restore) {
94 //Hook to call Moodle < 1.5 Quiz Restore
95 if ($restore->backup_version
< 2005043000) {
96 include_once($CFG->dirroot
.'/mod/quiz/restorelibpre15.php');
97 return quiz_restore_pre15_question_categories($category,$restore);
100 //Get record from backup_ids
101 $data = backup_getid($restore->backup_unique_code
,"question_categories",$category->id
);
104 //Now get completed xmlized object
106 //traverse_xmlize($info); //Debug
107 //print_object ($GLOBALS['traverse_array']); //Debug
108 //$GLOBALS['traverse_array']=""; //Debug
110 //Now, build the question_categories record structure
111 $question_cat = new stdClass
;
112 $question_cat->course
= $restore->course_id
;
113 $question_cat->name
= backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']);
114 $question_cat->info
= backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']);
115 $question_cat->publish
= backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']);
116 $question_cat->stamp
= backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']);
117 $question_cat->parent
= backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']);
118 $question_cat->sortorder
= backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']);
120 if ($catfound = restore_get_best_question_category($question_cat, $restore->course_id
)) {
123 if (!$question_cat->stamp
) {
124 $question_cat->stamp
= make_unique_id_code();
126 $newid = insert_record ("question_categories",$question_cat);
131 if (!defined('RESTORE_SILENTLY')) {
132 echo "<li>".get_string('category', 'quiz')." \"".$question_cat->name
."\"<br />";
135 //We must never arrive here !!
136 if (!defined('RESTORE_SILENTLY')) {
137 echo "<li>".get_string('category', 'quiz')." \"".$question_cat->name
."\" Error!<br />";
143 //Here category has been created or selected, so save results in backup_ids and start with questions
144 if ($newid and $status) {
145 //We have the newid, update backup_ids
146 backup_putid($restore->backup_unique_code
,"question_categories",
147 $category->id
, $newid);
148 //Now restore question
149 $status = restore_questions ($category->id
, $newid,$info,$restore);
153 if (!defined('RESTORE_SILENTLY')) {
157 echo 'Could not get backup info for question category'. $category->id
;
163 function restore_questions ($old_category_id,$new_category_id,$info,$restore) {
165 global $CFG, $QTYPES;
168 $restored_questions = array();
170 //Get the questions array
171 if (!empty($info['QUESTION_CATEGORY']['#']['QUESTIONS'])) {
172 $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
174 $questions = array();
177 //Iterate over questions
178 for($i = 0; $i < sizeof($questions); $i++
) {
179 $que_info = $questions[$i];
180 //traverse_xmlize($que_info); //Debug
181 //print_object ($GLOBALS['traverse_array']); //Debug
182 //$GLOBALS['traverse_array']=""; //Debug
184 //We'll need this later!!
185 $oldid = backup_todb($que_info['#']['ID']['0']['#']);
187 //Now, build the question record structure
188 $question = new object;
189 $question->category
= $new_category_id;
190 $question->parent
= backup_todb($que_info['#']['PARENT']['0']['#']);
191 $question->name
= backup_todb($que_info['#']['NAME']['0']['#']);
192 $question->questiontext
= backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
193 $question->questiontextformat
= backup_todb($que_info['#']['QUESTIONTEXTFORMAT']['0']['#']);
194 $question->image
= backup_todb($que_info['#']['IMAGE']['0']['#']);
195 if (array_key_exists('GENERALFEEDBACK', $que_info['#'])) {
196 $question->generalfeedback
= backup_todb($que_info['#']['GENERALFEEDBACK']['0']['#']);
198 $question->generalfeedback
= '';
200 $question->defaultgrade
= backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
201 $question->penalty
= backup_todb($que_info['#']['PENALTY']['0']['#']);
202 $question->qtype
= backup_todb($que_info['#']['QTYPE']['0']['#']);
203 $question->length
= backup_todb($que_info['#']['LENGTH']['0']['#']);
204 $question->stamp
= backup_todb($que_info['#']['STAMP']['0']['#']);
205 $question->version
= backup_todb($que_info['#']['VERSION']['0']['#']);
206 $question->hidden
= backup_todb($que_info['#']['HIDDEN']['0']['#']);
208 if ($restore->backup_version
< 2006032200) {
209 // The qtype was an integer that now needs to be converted to the name
210 $qtypenames = array(1=>'shortanswer',2=>'truefalse',3=>'multichoice',4=>'random',5=>'match',
211 6=>'randomsamatch',7=>'description',8=>'numerical',9=>'multianswer',10=>'calculated',
212 11=>'rqp',12=>'essay');
213 $question->qtype
= $qtypenames[$question->qtype
];
216 //Check if the question exists
217 //by category, stamp, and version
218 $question_exists = get_record ("question","category",$question->category
,
219 "stamp",$question->stamp
,"version",$question->version
);
221 //If the question exists, only record its id
222 if ($question_exists) {
223 $newid = $question_exists->id
;
224 $creatingnewquestion = false;
225 //Else, create a new question
227 //The structure is equal to the db, so insert the question
228 $newid = insert_record ("question",$question);
229 $creatingnewquestion = true;
232 //Save newid to backup tables
234 //We have the newid, update backup_ids
235 backup_putid($restore->backup_unique_code
,"question",$oldid,
239 $restored_questions[$i] = new stdClass
;
240 $restored_questions[$i]->newid
= $newid;
241 $restored_questions[$i]->oldid
= $oldid;
242 $restored_questions[$i]->qtype
= $question->qtype
;
243 $restored_questions[$i]->parent
= $question->parent
;
244 $restored_questions[$i]->is_new
= $creatingnewquestion;
247 // Loop again, now all the question id mappings exist, so everything can
249 for($i = 0; $i < sizeof($questions); $i++
) {
250 $que_info = $questions[$i];
252 $newid = $restored_questions[$i]->newid
;
253 $oldid = $restored_questions[$i]->oldid
;
255 $question = new object;
256 $question->qtype
= $restored_questions[$i]->qtype
;
257 $question->parent
= $restored_questions[$i]->parent
;
260 //If it's a new question in the DB, restore it
261 if ($restored_questions[$i]->is_new
) {
263 ////We have to recode the parent field
264 if ($question->parent
) {
265 if ($parent = backup_getid($restore->backup_unique_code
,"question",$question->parent
)) {
266 $question->parent
= $parent->new_id
;
267 } elseif ($question->parent
= $oldid) {
268 $question->parent
= $newid;
270 echo 'Could not recode parent '.$question->parent
.' for question '.$oldid.'<br />';
274 //Now, restore every question_answers in this question
275 $status = question_restore_answers($oldid,$newid,$que_info,$restore);
276 // Restore questiontype specific data
277 if (array_key_exists($question->qtype
, $QTYPES)) {
278 $status = $QTYPES[$question->qtype
]->restore($oldid,$newid,$que_info,$restore);
280 echo 'Unknown question type '.$question->qtype
.' for question '.$oldid.'<br />';
284 //We are NOT creating the question, but we need to know every question_answers
285 //map between the XML file and the database to be able to restore the states
287 $status = question_restore_map_answers($oldid,$newid,$que_info,$restore);
288 // Do the questiontype specific mapping
289 if (array_key_exists($question->qtype
, $QTYPES)) {
290 $status = $QTYPES[$question->qtype
]->restore_map($oldid,$newid,$que_info,$restore);
292 echo 'Unknown question type '.$question->qtype
.' for question '.$oldid.'<br />';
298 if (($i+
1) %
2 == 0) {
299 if (!defined('RESTORE_SILENTLY')) {
301 if (($i+
1) %
40 == 0) {
311 function question_restore_answers ($old_question_id,$new_question_id,$info,$restore) {
316 $qtype = backup_todb($info['#']['QTYPE']['0']['#']);
318 //Get the answers array
319 if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
320 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
322 //Iterate over answers
323 for($i = 0; $i < sizeof($answers); $i++
) {
324 $ans_info = $answers[$i];
325 //traverse_xmlize($ans_info); //Debug
326 //print_object ($GLOBALS['traverse_array']); //Debug
327 //$GLOBALS['traverse_array']=""; //Debug
329 //We'll need this later!!
330 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
332 //Now, build the question_answers record structure
333 $answer = new stdClass
;
334 $answer->question
= $new_question_id;
335 $answer->answer
= backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
336 $answer->fraction
= backup_todb($ans_info['#']['FRACTION']['0']['#']);
337 $answer->feedback
= backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
339 // Update 'match everything' answers for numerical questions coming from old backup files.
340 if ($qtype == 'numerical' && $answer->answer
== '') {
341 $answer->answer
= '*';
344 //The structure is equal to the db, so insert the question_answers
345 $newid = insert_record ("question_answers",$answer);
348 if (($i+
1) %
50 == 0) {
349 if (!defined('RESTORE_SILENTLY')) {
351 if (($i+
1) %
1000 == 0) {
359 //We have the newid, update backup_ids
360 backup_putid($restore->backup_unique_code
,"question_answers",$oldid,
371 function question_restore_map_answers ($old_question_id,$new_question_id,$info,$restore) {
377 if (!isset($info['#']['ANSWERS'])) { // No answers in this question (eg random)
381 //Get the answers array
382 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
384 //Iterate over answers
385 for($i = 0; $i < sizeof($answers); $i++
) {
386 $ans_info = $answers[$i];
387 //traverse_xmlize($ans_info); //Debug
388 //print_object ($GLOBALS['traverse_array']); //Debug
389 //$GLOBALS['traverse_array']=""; //Debug
391 //We'll need this later!!
392 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
394 //Now, build the question_answers record structure
395 $answer->question
= $new_question_id;
396 $answer->answer
= backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
397 $answer->fraction
= backup_todb($ans_info['#']['FRACTION']['0']['#']);
398 $answer->feedback
= backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
400 //If we are in this method is because the question exists in DB, so its
401 //answers must exist too.
402 //Now, we are going to look for that answer in DB and to create the
403 //mappings in backup_ids to use them later where restoring states (user level).
405 //Get the answer from DB (by question and answer)
406 $db_answer = get_record ("question_answers","question",$new_question_id,
407 "answer",$answer->answer
);
410 if (($i+
1) %
50 == 0) {
411 if (!defined('RESTORE_SILENTLY')) {
413 if (($i+
1) %
1000 == 0) {
421 //We have the database answer, update backup_ids
422 backup_putid($restore->backup_unique_code
,"question_answers",$oldid,
432 function question_restore_numerical_units($old_question_id,$new_question_id,$info,$restore) {
438 //Get the numerical array
439 if (!empty($info['#']['NUMERICAL_UNITS'])) {
440 $numerical_units = $info['#']['NUMERICAL_UNITS']['0']['#']['NUMERICAL_UNIT'];
442 $numerical_units = array();
445 //Iterate over numerical_units
446 for($i = 0; $i < sizeof($numerical_units); $i++
) {
447 $nu_info = $numerical_units[$i];
448 //traverse_xmlize($nu_info); //Debug
449 //print_object ($GLOBALS['traverse_array']); //Debug
450 //$GLOBALS['traverse_array']=""; //Debug
452 // Check to see if this until already exists in the database, which it might, for
453 // Historical reasons.
454 $unit = backup_todb($nu_info['#']['UNIT']['0']['#']);
455 if (!record_exists('question_numerical_units', 'question', $new_question_id, 'unit', $unit)) {
457 //Now, build the question_numerical_UNITS record structure.
458 $numerical_unit = new stdClass
;
459 $numerical_unit->question
= $new_question_id;
460 $numerical_unit->multiplier
= backup_todb($nu_info['#']['MULTIPLIER']['0']['#']);
461 $numerical_unit->unit
= $unit;
463 //The structure is equal to the db, so insert the question_numerical_units
464 $newid = insert_record("question_numerical_units", $numerical_unit);
475 function question_restore_dataset_definitions ($old_question_id,$new_question_id,$info,$restore) {
481 //Get the dataset_definitions array
482 $dataset_definitions = $info['#']['DATASET_DEFINITIONS']['0']['#']['DATASET_DEFINITION'];
484 //Iterate over dataset_definitions
485 for($i = 0; $i < sizeof($dataset_definitions); $i++
) {
486 $dd_info = $dataset_definitions[$i];
487 //traverse_xmlize($dd_info); //Debug
488 //print_object ($GLOBALS['traverse_array']); //Debug
489 //$GLOBALS['traverse_array']=""; //Debug
491 //Now, build the question_dataset_DEFINITION record structure
492 $dataset_definition = new stdClass
;
493 $dataset_definition->category
= backup_todb($dd_info['#']['CATEGORY']['0']['#']);
494 $dataset_definition->name
= backup_todb($dd_info['#']['NAME']['0']['#']);
495 $dataset_definition->type
= backup_todb($dd_info['#']['TYPE']['0']['#']);
496 $dataset_definition->options
= backup_todb($dd_info['#']['OPTIONS']['0']['#']);
497 $dataset_definition->itemcount
= backup_todb($dd_info['#']['ITEMCOUNT']['0']['#']);
499 //We have to recode the category field (only if the category != 0)
500 if ($dataset_definition->category
!= 0) {
501 $category = backup_getid($restore->backup_unique_code
,"question_categories",$dataset_definition->category
);
503 $dataset_definition->category
= $category->new_id
;
505 echo 'Could not recode category id '.$dataset_definition->category
.' for dataset definition'.$dataset_definition->name
.'<br />';
509 //Now, we hace to decide when to create the new records or reuse an existing one
510 $create_definition = false;
512 //If the dataset_definition->category = 0, it's a individual question dataset_definition, so we'll create it
513 if ($dataset_definition->category
== 0) {
514 $create_definition = true;
516 //The category isn't 0, so it's a category question dataset_definition, we have to see if it exists
517 //Look for a definition with the same category, name and type
518 if ($definitionrec = get_record_sql("SELECT d.*
519 FROM {$CFG->prefix}question_dataset_definitions d
520 WHERE d.category = '$dataset_definition->category' AND
521 d.name = '$dataset_definition->name' AND
522 d.type = '$dataset_definition->type'")) {
523 //Such dataset_definition exist. Now we must check if it has enough itemcount
524 if ($definitionrec->itemcount
< $dataset_definition->itemcount
) {
525 //We haven't enough itemcount, so we have to create the definition as an individual question one.
526 $dataset_definition->category
= 0;
527 $create_definition = true;
529 //We have enough itemcount, so we'll reuse the existing definition
530 $create_definition = false;
531 $newid = $definitionrec->id
;
534 //Such dataset_definition doesn't exist. We'll create it.
535 $create_definition = true;
539 //If we've to create the definition, do it
540 if ($create_definition) {
541 //The structure is equal to the db, so insert the question_dataset_definitions
542 $newid = insert_record ("question_dataset_definitions",$dataset_definition);
544 //Restore question_dataset_items
545 $status = question_restore_dataset_items($newid,$dd_info,$restore);
549 //Now, we must have a definition (created o reused). Its id is in newid. Create the question_datasets record
550 //to join the question and the dataset_definition
552 $question_dataset = new stdClass
;
553 $question_dataset->question
= $new_question_id;
554 $question_dataset->datasetdefinition
= $newid;
555 $newid = insert_record ("question_datasets",$question_dataset);
566 function question_restore_dataset_items ($definitionid,$info,$restore) {
572 //Get the items array
573 $dataset_items = $info['#']['DATASET_ITEMS']['0']['#']['DATASET_ITEM'];
575 //Iterate over dataset_items
576 for($i = 0; $i < sizeof($dataset_items); $i++
) {
577 $di_info = $dataset_items[$i];
578 //traverse_xmlize($di_info); //Debug
579 //print_object ($GLOBALS['traverse_array']); //Debug
580 //$GLOBALS['traverse_array']=""; //Debug
582 //Now, build the question_dataset_ITEMS record structure
583 $dataset_item = new stdClass
;
584 $dataset_item->definition
= $definitionid;
585 $dataset_item->itemnumber
= backup_todb($di_info['#']['NUMBER']['0']['#']);
586 $dataset_item->value
= backup_todb($di_info['#']['VALUE']['0']['#']);
588 //The structure is equal to the db, so insert the question_dataset_items
589 $newid = insert_record ("question_dataset_items",$dataset_item);
600 //This function restores the question_states
601 function question_states_restore_mods($attempt_id,$info,$restore) {
603 global $CFG, $QTYPES;
607 //Get the question_states array
608 $states = $info['#']['STATES']['0']['#']['STATE'];
609 //Iterate over states
610 for($i = 0; $i < sizeof($states); $i++
) {
611 $res_info = $states[$i];
612 //traverse_xmlize($res_info); //Debug
613 //print_object ($GLOBALS['traverse_array']); //Debug
614 //$GLOBALS['traverse_array']=""; //Debug
616 //We'll need this later!!
617 $oldid = backup_todb($res_info['#']['ID']['0']['#']);
619 //Now, build the STATES record structure
620 $state = new stdClass
;
621 $state->attempt
= $attempt_id;
622 $state->question
= backup_todb($res_info['#']['QUESTION']['0']['#']);
623 $state->originalquestion
= backup_todb($res_info['#']['ORIGINALQUESTION']['0']['#']);
624 $state->seq_number
= backup_todb($res_info['#']['SEQ_NUMBER']['0']['#']);
625 $state->answer
= backup_todb($res_info['#']['ANSWER']['0']['#']);
626 $state->timestamp
= backup_todb($res_info['#']['TIMESTAMP']['0']['#']);
627 $state->event
= backup_todb($res_info['#']['EVENT']['0']['#']);
628 $state->grade
= backup_todb($res_info['#']['GRADE']['0']['#']);
629 $state->raw_grade
= backup_todb($res_info['#']['RAW_GRADE']['0']['#']);
630 $state->penalty
= backup_todb($res_info['#']['PENALTY']['0']['#']);
631 $state->oldid
= $oldid; // So it is available to restore_recode_answer.
633 //We have to recode the question field
634 $question = backup_getid($restore->backup_unique_code
,"question",$state->question
);
636 $state->question
= $question->new_id
;
638 echo 'Could not recode question id '.$state->question
.' for state '.$oldid.'<br />';
641 //We have to recode the originalquestion field if it is nonzero
642 if ($state->originalquestion
) {
643 $question = backup_getid($restore->backup_unique_code
,"question",$state->originalquestion
);
645 $state->originalquestion
= $question->new_id
;
647 echo 'Could not recode originalquestion id '.$state->question
.' for state '.$oldid.'<br />';
651 //We have to recode the answer field
652 //It depends of the question type !!
653 //We get the question first
654 if (!$question = get_record("question","id",$state->question
)) {
655 error("Can't find the record for question $state->question for which I am trying to restore a state");
657 //Depending on the qtype, we make different recodes
658 if ($state->answer
) {
659 $state->answer
= $QTYPES[$question->qtype
]->restore_recode_answer($state, $restore);
662 //The structure is equal to the db, so insert the question_states
663 $newid = insert_record ("question_states",$state);
666 if (($i+
1) %
10 == 0) {
667 if (!defined('RESTORE_SILENTLY')) {
669 if (($i+
1) %
200 == 0) {
677 //We have the newid, update backup_ids
678 backup_putid($restore->backup_unique_code
, 'question_states', $oldid, $newid);
684 //Get the question_sessions array
685 $sessions = $info['#']['NEWEST_STATES']['0']['#']['NEWEST_STATE'];
686 //Iterate over question_sessions
687 for($i = 0; $i < sizeof($sessions); $i++
) {
688 $res_info = $sessions[$i];
689 //traverse_xmlize($res_info); //Debug
690 //print_object ($GLOBALS['traverse_array']); //Debug
691 //$GLOBALS['traverse_array']=""; //Debug
693 //Now, build the NEWEST_STATES record structure
694 $session = new stdClass
;
695 $session->attemptid
= $attempt_id;
696 $session->questionid
= backup_todb($res_info['#']['QUESTIONID']['0']['#']);
697 $session->newest
= backup_todb($res_info['#']['NEWEST']['0']['#']);
698 $session->newgraded
= backup_todb($res_info['#']['NEWGRADED']['0']['#']);
699 $session->sumpenalty
= backup_todb($res_info['#']['SUMPENALTY']['0']['#']);
701 if ($res_info['#']['MANUALCOMMENT']['0']['#']) {
702 $session->manualcomment
= backup_todb($res_info['#']['MANUALCOMMENT']['0']['#']);
703 } else { // pre 1.7 backups
704 $session->manualcomment
= backup_todb($res_info['#']['COMMENT']['0']['#']);
707 //We have to recode the question field
708 $question = backup_getid($restore->backup_unique_code
,"question",$session->questionid
);
710 $session->questionid
= $question->new_id
;
712 echo 'Could not recode question id '.$session->questionid
.'<br />';
715 //We have to recode the newest field
716 $state = backup_getid($restore->backup_unique_code
,"question_states",$session->newest
);
718 $session->newest
= $state->new_id
;
720 echo 'Could not recode newest state id '.$session->newest
.'<br />';
723 //If the session has been graded we have to recode the newgraded field
724 if ($session->newgraded
) {
725 $state = backup_getid($restore->backup_unique_code
,"question_states",$session->newgraded
);
727 $session->newgraded
= $state->new_id
;
729 echo 'Could not recode newest graded state id '.$session->newgraded
.'<br />';
733 //The structure is equal to the db, so insert the question_sessions
734 $newid = insert_record ("question_sessions",$session);
742 * Recode content links in question texts.
743 * @param object $restore the restore metadata object.
744 * @return boolean whether the operation succeeded.
746 function question_decode_content_links_caller($restore) {
747 global $CFG, $QTYPES;
749 $i = 1; //Counter to send some output to the browser to avoid timeouts
751 // Get a list of which question types have custom field that will need decoding.
752 $qtypeswithextrafields = array();
753 $qtypeswithhtmlanswers = array();
754 foreach ($QTYPES as $qtype => $qtypeclass) {
755 $qtypeswithextrafields[$qtype] = method_exists($qtypeclass, 'decode_content_links_caller');
756 $qtypeswithhtmlanswers[$qtype] = $qtypeclass->has_html_answers();
758 $extraprocessing = array();
760 // Decode links in questions.
761 if ($questions = get_records_sql('SELECT q.id, q.qtype, q.questiontext, q.generalfeedback
762 FROM ' . $CFG->prefix
. 'question q,
763 ' . $CFG->prefix
. 'question_categories qc
764 WHERE q.category = qc.id
765 AND qc.course = ' . $restore->course_id
)) {
767 foreach ($questions as $question) {
768 $questiontext = restore_decode_content_links_worker($question->questiontext
, $restore);
769 $generalfeedback = restore_decode_content_links_worker($question->generalfeedback
, $restore);
770 if ($questiontext != $question->questiontext ||
$generalfeedback != $question->generalfeedback
) {
771 $question->questiontext
= addslashes($questiontext);
772 $question->generalfeedback
= addslashes($generalfeedback);
773 if (!update_record('question', $question)) {
779 if (++
$i %
5 == 0 && !defined('RESTORE_SILENTLY')) {
787 // Decode any questiontype specific fields.
788 if ($qtypeswithextrafields[$question->qtype
]) {
789 if (!array_key_exists($question->qtype
, $extraprocessing)) {
790 $extraprocessing[$question->qtype
] = array();
792 $extraprocessing[$question->qtype
][] = $question->id
;
797 // Decode links in answers.
798 if ($answers = get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype
799 FROM ' . $CFG->prefix
. 'question_answers qa,
800 ' . $CFG->prefix
. 'question q,
801 ' . $CFG->prefix
. 'question_categories qc
802 WHERE qa.question = q.id
803 AND q.category = qc.id
804 AND qc.course = ' . $restore->course_id
)) {
806 foreach ($answers as $answer) {
807 $feedback = restore_decode_content_links_worker($answer->feedback
, $restore);
808 if ($qtypeswithhtmlanswers[$answer->qtype
]) {
809 $answertext = restore_decode_content_links_worker($answer->answer
, $restore);
811 $answertext = $answer->answer
;
813 if ($feedback != $answer->feedback ||
$answertext != $answer->answer
) {
814 unset($answer->qtype
);
815 $answer->feedback
= addslashes($feedback);
816 $answer->answer
= addslashes($answertext);
817 if (!update_record('question_answers', $answer)) {
823 if (++
$i %
5 == 0 && !defined('RESTORE_SILENTLY')) {
833 // Do extra work for certain question types.
834 foreach ($extraprocessing as $qtype => $questionids) {
835 if (!$QTYPES[$qtype]->decode_content_links_caller($questionids, $restore, $i)) {