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');
89 * Returns the best question category (id) found to restore one
90 * question category from a backup file. Works by stamp.
92 * @param object $restore preferences for restoration
93 * @param array $contextinfo fragment of decoded xml
94 * @return object best context instance for this category to be in
96 function restore_question_get_best_category_context($restore, $contextinfo) {
97 switch ($contextinfo['LEVEL'][0]['#']) {
99 $instanceinfo = backup_getid($restore->backup_unique_code
, 'course_modules', $contextinfo['INSTANCE'][0]['#']);
100 $tocontext = get_context_instance(CONTEXT_MODULE
, $instanceinfo->new_id
);
103 $tocontext = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
105 case 'coursecategory':
106 //search COURSECATEGORYLEVEL steps up the course cat tree or
107 //to the top of the tree if steps are exhausted.
108 $catno = $contextinfo['COURSECATEGORYLEVEL'][0]['#'];
109 $catid = get_field('course', 'parent', 'id', $restore->course_id
);
111 $nextcatid = get_field('course_categories', 'parent', 'id', $catid);
112 if ($nextcatid == 0){
115 $catid == $nextcatid;
118 $tocontext = get_context_instance(CONTEXT_COURSECAT
, $catid);
121 $tocontext = get_context_instance(CONTEXT_SYSTEM
);
127 function restore_question_categories($info, $restore) {
129 //Iterate over each category
130 foreach ($info as $category) {
131 $status = $status && restore_question_category($category, $restore);
133 $status = $status && restore_recode_category_parents($restore);
137 function restore_question_category($category, $restore){
139 //Skip empty categories (some backups can contain them)
140 if (!empty($category->id
)) {
141 //Get record from backup_ids
142 $data = backup_getid($restore->backup_unique_code
, "question_categories", $category->id
);
145 //Now get completed xmlized object
147 //traverse_xmlize($info); //Debug
148 //print_object ($GLOBALS['traverse_array']); //Debug
149 //$GLOBALS['traverse_array']=""; //Debug
151 //Now, build the question_categories record structure
152 $question_cat = new stdClass
;
153 $question_cat->name
= backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']);
154 $question_cat->info
= backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']);
155 $question_cat->stamp
= backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']);
156 //parent is fixed after all categories are restored and we know all the new ids.
157 $question_cat->parent
= backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']);
158 $question_cat->sortorder
= backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']);
159 if (!$question_cat->stamp
) {
160 $question_cat->stamp
= make_unique_id_code();
162 if (isset($info['QUESTION_CATEGORY']['#']['PUBLISH'])) {
163 $course = $restore->course_id
;
164 $publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']);
166 $tocontext = get_context_instance(CONTEXT_SYSTEM
);
168 $tocontext = get_context_instance(CONTEXT_COURSE
, $course);
171 $tocontext = restore_question_get_best_category_context($restore, $info['QUESTION_CATEGORY']['#']['CONTEXT']['0']['#']);
173 $question_cat->contextid
= $tocontext->id
;
175 //does cat exist ?? if it does we check if the cat and questions already exist whether we have
176 //add permission or not if we have no permission to add questions to SYSTEM or COURSECAT context
177 //AND the question does not already exist then we create questions in COURSE context.
178 if (!$fcat = get_record('question_categories','contextid', $question_cat->contextid
, 'stamp', $question_cat->stamp
)){
180 if ((($tocontext->contextlevel
== CONTEXT_SYSTEM
) ||
($tocontext->contextlevel
== CONTEXT_COURSECAT
))
181 && !has_capability('moodle/question:add', $tocontext)){
182 //no preexisting cat and no permission to create questions here
183 //must restore to course.
184 $tocontext = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
186 $question_cat->contextid
= $tocontext->id
;
187 if (!$fcat = get_record('question_categories','contextid', $question_cat->contextid
, 'stamp', $question_cat->stamp
)){
188 $question_cat->id
= insert_record ("question_categories", $question_cat);
190 $question_cat = $fcat;
192 //we'll be restoring all questions here.
193 backup_putid($restore->backup_unique_code
, "question_categories", $category->id
, $question_cat->id
);
195 $question_cat = $fcat;
196 //we found an existing best category
197 //but later if context is above course need to check if there are questions need creating in category
198 //if we do need to create questions and permissions don't allow it create new category in course
202 if (!defined('RESTORE_SILENTLY')) {
203 echo "<li>".get_string('category', 'quiz')." \"".$question_cat->name
."\"<br />";
208 //start with questions
209 if ($question_cat->id
) {
210 //We have the newid, update backup_ids
211 //Now restore question
212 $status = restore_questions($category->id
, $question_cat, $info, $restore);
216 if (!defined('RESTORE_SILENTLY')) {
220 echo 'Could not get backup info for question category'. $category->id
;
226 function restore_recode_category_parents($restore){
229 //Now we have to recode the parent field of each restored category
230 $categories = get_records_sql("SELECT old_id, new_id
231 FROM {$CFG->prefix}backup_ids
232 WHERE backup_code = $restore->backup_unique_code AND
233 table_name = 'question_categories'");
235 //recode all parents to point at their old parent cats no matter what context the parent is now in
236 foreach ($categories as $category) {
237 $restoredcategory = get_record('question_categories','id',$category->new_id
);
238 if ($restoredcategory->parent
!= 0) {
239 $updateobj = new object();
240 $updateobj->id
= $restoredcategory->id
;
241 $idcat = backup_getid($restore->backup_unique_code
,'question_categories',$restoredcategory->parent
);
242 if ($idcat->new_id
) {
243 $updateobj->parent
= $idcat->new_id
;
245 $updateobj->parent
= 0;
247 $status = $status && update_record('question_categories', $updateobj);
250 //now we have recoded all parents, check through all parents and set parent to be
251 //grand parent / great grandparent etc where there is one in same context
252 //or else set parent to 0 (top level category).
254 foreach ($categories as $category) {
255 $restoredcategory = get_record('question_categories','id',$category->new_id
);
256 if ($restoredcategory->parent
!= 0) {
257 $nextparentid = $restoredcategory->parent
;
259 if (!$parent = get_record('question_categories', 'id', $nextparentid)){
260 if (!defined('RESTORE_SILENTLY')) {
261 echo 'Could not find parent for question category '. $category->id
.' recoding as top category item.<br />';
263 break;//record fetch failed finish loop
265 $nextparentid = $nextparent->parent
;
267 } while (($nextparentid != 0) && ($parent->contextid
!= $restoredcategory->contextid
));
268 if (!$parent ||
($parent->id
!= $restoredcategory->parent
)){
269 //change needs to be made to the parent field.
270 if ($parent && ($parent->contextid
== $restoredcategory->contextid
)){
271 $toupdate[$restoredcategory->id
] = $parent->id
;
273 //searched up the tree till we came to the top and did not find cat in same
274 //context or there was an error getting next parent record
275 $toupdate[$restoredcategory->id
] = 0;
280 //now finally do the changes to parent field.
281 foreach ($toupdate as $id => $parent){
282 $updateobj = new object();
283 $updateobj->id
= $id;
284 $updateobj->parent
= $parent;
285 $status = $status && update_record('question_categories', $updateobj);
291 function restore_questions ($old_category_id, $best_question_cat, $info, $restore) {
293 global $CFG, $QTYPES;
296 $restored_questions = array();
298 //Get the questions array
299 if (!empty($info['QUESTION_CATEGORY']['#']['QUESTIONS'])) {
300 $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
302 $questions = array();
305 //Iterate over questions
306 for($i = 0; $i < sizeof($questions); $i++
) {
307 $que_info = $questions[$i];
308 //traverse_xmlize($que_info); //Debug
309 //print_object ($GLOBALS['traverse_array']); //Debug
310 //$GLOBALS['traverse_array']=""; //Debug
312 //We'll need this later!!
313 $oldid = backup_todb($que_info['#']['ID']['0']['#']);
315 //Now, build the question record structure
316 $question = new object;
317 $question->parent
= backup_todb($que_info['#']['PARENT']['0']['#']);
318 $question->name
= backup_todb($que_info['#']['NAME']['0']['#']);
319 $question->questiontext
= backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
320 $question->questiontextformat
= backup_todb($que_info['#']['QUESTIONTEXTFORMAT']['0']['#']);
321 $question->image
= backup_todb($que_info['#']['IMAGE']['0']['#']);
322 if (array_key_exists('GENERALFEEDBACK', $que_info['#'])) {
323 $question->generalfeedback
= backup_todb($que_info['#']['GENERALFEEDBACK']['0']['#']);
325 $question->generalfeedback
= '';
327 $question->defaultgrade
= backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
328 $question->penalty
= backup_todb($que_info['#']['PENALTY']['0']['#']);
329 $question->qtype
= backup_todb($que_info['#']['QTYPE']['0']['#']);
330 $question->length
= backup_todb($que_info['#']['LENGTH']['0']['#']);
331 $question->stamp
= backup_todb($que_info['#']['STAMP']['0']['#']);
332 $question->version
= backup_todb($que_info['#']['VERSION']['0']['#']);
333 $question->hidden
= backup_todb($que_info['#']['HIDDEN']['0']['#']);
334 $question->timecreated
= backup_todb($que_info['#']['TIMECREATED']['0']['#']);
335 $question->timemodified
= backup_todb($que_info['#']['TIMEMODIFIED']['0']['#']);
336 $question->createdby
= backup_todb($que_info['#']['CREATEDBY']['0']['#']);
337 $question->modifiedby
= backup_todb($que_info['#']['MODIFIEDBY']['0']['#']);
339 if ($restore->backup_version
< 2006032200) {
340 // The qtype was an integer that now needs to be converted to the name
341 $qtypenames = array(1=>'shortanswer',2=>'truefalse',3=>'multichoice',4=>'random',5=>'match',
342 6=>'randomsamatch',7=>'description',8=>'numerical',9=>'multianswer',10=>'calculated',
343 11=>'rqp',12=>'essay');
344 $question->qtype
= $qtypenames[$question->qtype
];
347 //Check if the question exists by category, stamp, and version
348 //first check for the question in the context specified in backup
349 $existingquestion = get_record ("question", "category", $best_question_cat->id
, "stamp", $question->stamp
,"version",$question->version
);
350 //If the question exists, only record its id
351 //always use existing question, no permissions check here
352 if ($existingquestion) {
353 $question = $existingquestion;
354 $creatingnewquestion = false;
356 //then if context above course level check permissions and if no permission
357 //to restore above course level then restore to cat in course context.
358 $bestcontext = get_context_instance_by_id($best_question_cat->contextid
);
359 if (($bestcontext->contextlevel
== CONTEXT_SYSTEM ||
$bestcontext->contextlevel
== CONTEXT_COURSECAT
)
360 && !has_capability('moodle/question:add', $bestcontext)){
361 if (!isset($course_question_cat)) {
362 $coursecontext = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
363 $course_question_cat = clone($best_question_cat);
364 $course_question_cat->contextid
= $coursecontext->id
;
365 //create cat if it doesn't exist
366 if (!$fcat = get_record('question_categories','contextid', $course_question_cat->contextid
, 'stamp', $course_question_cat->stamp
)){
367 $course_question_cat->id
= insert_record ("question_categories", $course_question_cat);
368 backup_putid($restore->backup_unique_code
, "question_categories", $old_category_id, $course_question_cat->id
);
370 $course_question_cat = $fcat;
372 //will fix category parents after all questions and categories restored. Will set parent to 0 if
373 //no parent in same context.
375 $question->category
= $course_question_cat->id
;
376 //does question already exist in course cat
377 $existingquestion = get_record ("question", "category", $question->category
, "stamp", $question->stamp
, "version", $question->version
);
379 //permissions ok, restore to best cat
380 $question->category
= $best_question_cat->id
;
382 if (!$existingquestion){
383 //The structure is equal to the db, so insert the question
384 $question->id
= insert_record ("question", $question);
385 $creatingnewquestion = true;
387 $question = $existingquestion;
388 $creatingnewquestion = false;
392 //Save newid to backup tables
394 //We have the newid, update backup_ids
395 backup_putid($restore->backup_unique_code
, "question", $oldid, $question->id
);
398 $restored_questions[$i] = new stdClass
;
399 $restored_questions[$i]->newid
= $question->id
;
400 $restored_questions[$i]->oldid
= $oldid;
401 $restored_questions[$i]->qtype
= $question->qtype
;
402 $restored_questions[$i]->parent
= $question->parent
;
403 $restored_questions[$i]->is_new
= $creatingnewquestion;
407 // Loop again, now all the question id mappings exist, so everything can
409 for($i = 0; $i < sizeof($questions); $i++
) {
410 $que_info = $questions[$i];
412 $newid = $restored_questions[$i]->newid
;
413 $oldid = $restored_questions[$i]->oldid
;
415 $question = new object;
416 $question->qtype
= $restored_questions[$i]->qtype
;
417 $question->parent
= $restored_questions[$i]->parent
;
420 //If it's a new question in the DB, restore it
421 if ($restored_questions[$i]->is_new
) {
423 ////We have to recode the parent field
424 if ($question->parent
) {
425 if ($parent = backup_getid($restore->backup_unique_code
,"question",$question->parent
)) {
426 $question->parent
= $parent->new_id
;
427 } elseif ($question->parent
= $oldid) {
428 $question->parent
= $newid;
430 echo 'Could not recode parent '.$question->parent
.' for question '.$oldid.'<br />';
434 //Now, restore every question_answers in this question
435 $status = question_restore_answers($oldid,$newid,$que_info,$restore);
436 // Restore questiontype specific data
437 if (array_key_exists($question->qtype
, $QTYPES)) {
438 $status = $QTYPES[$question->qtype
]->restore($oldid,$newid,$que_info,$restore);
440 echo 'Unknown question type '.$question->qtype
.' for question '.$oldid.'<br />';
444 //We are NOT creating the question, but we need to know every question_answers
445 //map between the XML file and the database to be able to restore the states
447 $status = question_restore_map_answers($oldid,$newid,$que_info,$restore);
448 // Do the questiontype specific mapping
449 if (array_key_exists($question->qtype
, $QTYPES)) {
450 $status = $QTYPES[$question->qtype
]->restore_map($oldid,$newid,$que_info,$restore);
452 echo 'Unknown question type '.$question->qtype
.' for question '.$oldid.'<br />';
458 if (($i+
1) %
2 == 0) {
459 if (!defined('RESTORE_SILENTLY')) {
461 if (($i+
1) %
40 == 0) {
471 function question_restore_answers ($old_question_id,$new_question_id,$info,$restore) {
476 $qtype = backup_todb($info['#']['QTYPE']['0']['#']);
478 //Get the answers array
479 if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
480 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
482 //Iterate over answers
483 for($i = 0; $i < sizeof($answers); $i++
) {
484 $ans_info = $answers[$i];
485 //traverse_xmlize($ans_info); //Debug
486 //print_object ($GLOBALS['traverse_array']); //Debug
487 //$GLOBALS['traverse_array']=""; //Debug
489 //We'll need this later!!
490 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
492 //Now, build the question_answers record structure
493 $answer = new stdClass
;
494 $answer->question
= $new_question_id;
495 $answer->answer
= backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
496 $answer->fraction
= backup_todb($ans_info['#']['FRACTION']['0']['#']);
497 $answer->feedback
= backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
499 // Update 'match everything' answers for numerical questions coming from old backup files.
500 if ($qtype == 'numerical' && $answer->answer
== '') {
501 $answer->answer
= '*';
504 //The structure is equal to the db, so insert the question_answers
505 $newid = insert_record ("question_answers",$answer);
508 if (($i+
1) %
50 == 0) {
509 if (!defined('RESTORE_SILENTLY')) {
511 if (($i+
1) %
1000 == 0) {
519 //We have the newid, update backup_ids
520 backup_putid($restore->backup_unique_code
,"question_answers",$oldid,
531 function question_restore_map_answers ($old_question_id,$new_question_id,$info,$restore) {
537 if (!isset($info['#']['ANSWERS'])) { // No answers in this question (eg random)
541 //Get the answers array
542 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
544 //Iterate over answers
545 for($i = 0; $i < sizeof($answers); $i++
) {
546 $ans_info = $answers[$i];
547 //traverse_xmlize($ans_info); //Debug
548 //print_object ($GLOBALS['traverse_array']); //Debug
549 //$GLOBALS['traverse_array']=""; //Debug
551 //We'll need this later!!
552 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
554 //Now, build the question_answers record structure
555 $answer->question
= $new_question_id;
556 $answer->answer
= backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
557 $answer->fraction
= backup_todb($ans_info['#']['FRACTION']['0']['#']);
558 $answer->feedback
= backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
560 //If we are in this method is because the question exists in DB, so its
561 //answers must exist too.
562 //Now, we are going to look for that answer in DB and to create the
563 //mappings in backup_ids to use them later where restoring states (user level).
565 //Get the answer from DB (by question and answer)
566 $db_answer = get_record ("question_answers","question",$new_question_id,
567 "answer",$answer->answer
);
570 if (($i+
1) %
50 == 0) {
571 if (!defined('RESTORE_SILENTLY')) {
573 if (($i+
1) %
1000 == 0) {
581 //We have the database answer, update backup_ids
582 backup_putid($restore->backup_unique_code
,"question_answers",$oldid,
592 function question_restore_numerical_units($old_question_id,$new_question_id,$info,$restore) {
598 //Get the numerical array
599 if (!empty($info['#']['NUMERICAL_UNITS'])) {
600 $numerical_units = $info['#']['NUMERICAL_UNITS']['0']['#']['NUMERICAL_UNIT'];
602 $numerical_units = array();
605 //Iterate over numerical_units
606 for($i = 0; $i < sizeof($numerical_units); $i++
) {
607 $nu_info = $numerical_units[$i];
608 //traverse_xmlize($nu_info); //Debug
609 //print_object ($GLOBALS['traverse_array']); //Debug
610 //$GLOBALS['traverse_array']=""; //Debug
612 // Check to see if this until already exists in the database, which it might, for
613 // Historical reasons.
614 $unit = backup_todb($nu_info['#']['UNIT']['0']['#']);
615 if (!record_exists('question_numerical_units', 'question', $new_question_id, 'unit', $unit)) {
617 //Now, build the question_numerical_UNITS record structure.
618 $numerical_unit = new stdClass
;
619 $numerical_unit->question
= $new_question_id;
620 $numerical_unit->multiplier
= backup_todb($nu_info['#']['MULTIPLIER']['0']['#']);
621 $numerical_unit->unit
= $unit;
623 //The structure is equal to the db, so insert the question_numerical_units
624 $newid = insert_record("question_numerical_units", $numerical_unit);
635 function question_restore_dataset_definitions ($old_question_id,$new_question_id,$info,$restore) {
641 //Get the dataset_definitions array
642 $dataset_definitions = $info['#']['DATASET_DEFINITIONS']['0']['#']['DATASET_DEFINITION'];
644 //Iterate over dataset_definitions
645 for($i = 0; $i < sizeof($dataset_definitions); $i++
) {
646 $dd_info = $dataset_definitions[$i];
647 //traverse_xmlize($dd_info); //Debug
648 //print_object ($GLOBALS['traverse_array']); //Debug
649 //$GLOBALS['traverse_array']=""; //Debug
651 //Now, build the question_dataset_DEFINITION record structure
652 $dataset_definition = new stdClass
;
653 $dataset_definition->category
= backup_todb($dd_info['#']['CATEGORY']['0']['#']);
654 $dataset_definition->name
= backup_todb($dd_info['#']['NAME']['0']['#']);
655 $dataset_definition->type
= backup_todb($dd_info['#']['TYPE']['0']['#']);
656 $dataset_definition->options
= backup_todb($dd_info['#']['OPTIONS']['0']['#']);
657 $dataset_definition->itemcount
= backup_todb($dd_info['#']['ITEMCOUNT']['0']['#']);
659 //We have to recode the category field (only if the category != 0)
660 if ($dataset_definition->category
!= 0) {
661 $category = backup_getid($restore->backup_unique_code
,"question_categories",$dataset_definition->category
);
663 $dataset_definition->category
= $category->new_id
;
665 echo 'Could not recode category id '.$dataset_definition->category
.' for dataset definition'.$dataset_definition->name
.'<br />';
669 //Now, we hace to decide when to create the new records or reuse an existing one
670 $create_definition = false;
672 //If the dataset_definition->category = 0, it's a individual question dataset_definition, so we'll create it
673 if ($dataset_definition->category
== 0) {
674 $create_definition = true;
676 //The category isn't 0, so it's a category question dataset_definition, we have to see if it exists
677 //Look for a definition with the same category, name and type
678 if ($definitionrec = get_record_sql("SELECT d.*
679 FROM {$CFG->prefix}question_dataset_definitions d
680 WHERE d.category = '$dataset_definition->category' AND
681 d.name = '$dataset_definition->name' AND
682 d.type = '$dataset_definition->type'")) {
683 //Such dataset_definition exist. Now we must check if it has enough itemcount
684 if ($definitionrec->itemcount
< $dataset_definition->itemcount
) {
685 //We haven't enough itemcount, so we have to create the definition as an individual question one.
686 $dataset_definition->category
= 0;
687 $create_definition = true;
689 //We have enough itemcount, so we'll reuse the existing definition
690 $create_definition = false;
691 $newid = $definitionrec->id
;
694 //Such dataset_definition doesn't exist. We'll create it.
695 $create_definition = true;
699 //If we've to create the definition, do it
700 if ($create_definition) {
701 //The structure is equal to the db, so insert the question_dataset_definitions
702 $newid = insert_record ("question_dataset_definitions",$dataset_definition);
704 //Restore question_dataset_items
705 $status = question_restore_dataset_items($newid,$dd_info,$restore);
709 //Now, we must have a definition (created o reused). Its id is in newid. Create the question_datasets record
710 //to join the question and the dataset_definition
712 $question_dataset = new stdClass
;
713 $question_dataset->question
= $new_question_id;
714 $question_dataset->datasetdefinition
= $newid;
715 $newid = insert_record ("question_datasets",$question_dataset);
726 function question_restore_dataset_items ($definitionid,$info,$restore) {
732 //Get the items array
733 $dataset_items = $info['#']['DATASET_ITEMS']['0']['#']['DATASET_ITEM'];
735 //Iterate over dataset_items
736 for($i = 0; $i < sizeof($dataset_items); $i++
) {
737 $di_info = $dataset_items[$i];
738 //traverse_xmlize($di_info); //Debug
739 //print_object ($GLOBALS['traverse_array']); //Debug
740 //$GLOBALS['traverse_array']=""; //Debug
742 //Now, build the question_dataset_ITEMS record structure
743 $dataset_item = new stdClass
;
744 $dataset_item->definition
= $definitionid;
745 $dataset_item->itemnumber
= backup_todb($di_info['#']['NUMBER']['0']['#']);
746 $dataset_item->value
= backup_todb($di_info['#']['VALUE']['0']['#']);
748 //The structure is equal to the db, so insert the question_dataset_items
749 $newid = insert_record ("question_dataset_items",$dataset_item);
760 //This function restores the question_states
761 function question_states_restore_mods($attempt_id,$info,$restore) {
763 global $CFG, $QTYPES;
767 //Get the question_states array
768 $states = $info['#']['STATES']['0']['#']['STATE'];
769 //Iterate over states
770 for($i = 0; $i < sizeof($states); $i++
) {
771 $res_info = $states[$i];
772 //traverse_xmlize($res_info); //Debug
773 //print_object ($GLOBALS['traverse_array']); //Debug
774 //$GLOBALS['traverse_array']=""; //Debug
776 //We'll need this later!!
777 $oldid = backup_todb($res_info['#']['ID']['0']['#']);
779 //Now, build the STATES record structure
780 $state = new stdClass
;
781 $state->attempt
= $attempt_id;
782 $state->question
= backup_todb($res_info['#']['QUESTION']['0']['#']);
783 $state->originalquestion
= backup_todb($res_info['#']['ORIGINALQUESTION']['0']['#']);
784 $state->seq_number
= backup_todb($res_info['#']['SEQ_NUMBER']['0']['#']);
785 $state->answer
= backup_todb($res_info['#']['ANSWER']['0']['#']);
786 $state->timestamp
= backup_todb($res_info['#']['TIMESTAMP']['0']['#']);
787 $state->event
= backup_todb($res_info['#']['EVENT']['0']['#']);
788 $state->grade
= backup_todb($res_info['#']['GRADE']['0']['#']);
789 $state->raw_grade
= backup_todb($res_info['#']['RAW_GRADE']['0']['#']);
790 $state->penalty
= backup_todb($res_info['#']['PENALTY']['0']['#']);
791 $state->oldid
= $oldid; // So it is available to restore_recode_answer.
793 //We have to recode the question field
794 $question = backup_getid($restore->backup_unique_code
,"question",$state->question
);
796 $state->question
= $question->new_id
;
798 echo 'Could not recode question id '.$state->question
.' for state '.$oldid.'<br />';
801 //We have to recode the originalquestion field if it is nonzero
802 if ($state->originalquestion
) {
803 $question = backup_getid($restore->backup_unique_code
,"question",$state->originalquestion
);
805 $state->originalquestion
= $question->new_id
;
807 echo 'Could not recode originalquestion id '.$state->question
.' for state '.$oldid.'<br />';
811 //We have to recode the answer field
812 //It depends of the question type !!
813 //We get the question first
814 if (!$question = get_record("question","id",$state->question
)) {
815 error("Can't find the record for question $state->question for which I am trying to restore a state");
817 //Depending on the qtype, we make different recodes
818 if ($state->answer
) {
819 $state->answer
= $QTYPES[$question->qtype
]->restore_recode_answer($state, $restore);
822 //The structure is equal to the db, so insert the question_states
823 $newid = insert_record ("question_states",$state);
826 if (($i+
1) %
10 == 0) {
827 if (!defined('RESTORE_SILENTLY')) {
829 if (($i+
1) %
200 == 0) {
837 //We have the newid, update backup_ids
838 backup_putid($restore->backup_unique_code
, 'question_states', $oldid, $newid);
844 //Get the question_sessions array
845 $sessions = $info['#']['NEWEST_STATES']['0']['#']['NEWEST_STATE'];
846 //Iterate over question_sessions
847 for($i = 0; $i < sizeof($sessions); $i++
) {
848 $res_info = $sessions[$i];
849 //traverse_xmlize($res_info); //Debug
850 //print_object ($GLOBALS['traverse_array']); //Debug
851 //$GLOBALS['traverse_array']=""; //Debug
853 //Now, build the NEWEST_STATES record structure
854 $session = new stdClass
;
855 $session->attemptid
= $attempt_id;
856 $session->questionid
= backup_todb($res_info['#']['QUESTIONID']['0']['#']);
857 $session->newest
= backup_todb($res_info['#']['NEWEST']['0']['#']);
858 $session->newgraded
= backup_todb($res_info['#']['NEWGRADED']['0']['#']);
859 $session->sumpenalty
= backup_todb($res_info['#']['SUMPENALTY']['0']['#']);
861 if ($res_info['#']['MANUALCOMMENT']['0']['#']) {
862 $session->manualcomment
= backup_todb($res_info['#']['MANUALCOMMENT']['0']['#']);
863 } else { // pre 1.7 backups
864 $session->manualcomment
= backup_todb($res_info['#']['COMMENT']['0']['#']);
867 //We have to recode the question field
868 $question = backup_getid($restore->backup_unique_code
,"question",$session->questionid
);
870 $session->questionid
= $question->new_id
;
872 echo 'Could not recode question id '.$session->questionid
.'<br />';
875 //We have to recode the newest field
876 $state = backup_getid($restore->backup_unique_code
,"question_states",$session->newest
);
878 $session->newest
= $state->new_id
;
880 echo 'Could not recode newest state id '.$session->newest
.'<br />';
883 //If the session has been graded we have to recode the newgraded field
884 if ($session->newgraded
) {
885 $state = backup_getid($restore->backup_unique_code
,"question_states",$session->newgraded
);
887 $session->newgraded
= $state->new_id
;
889 echo 'Could not recode newest graded state id '.$session->newgraded
.'<br />';
893 //The structure is equal to the db, so insert the question_sessions
894 $newid = insert_record ("question_sessions",$session);
902 * Recode content links in question texts.
903 * @param object $restore the restore metadata object.
904 * @return boolean whether the operation succeeded.
906 function question_decode_content_links_caller($restore) {
907 global $CFG, $QTYPES;
909 $i = 1; //Counter to send some output to the browser to avoid timeouts
911 // Get a list of which question types have custom field that will need decoding.
912 $qtypeswithextrafields = array();
913 $qtypeswithhtmlanswers = array();
914 foreach ($QTYPES as $qtype => $qtypeclass) {
915 $qtypeswithextrafields[$qtype] = method_exists($qtypeclass, 'decode_content_links_caller');
916 $qtypeswithhtmlanswers[$qtype] = $qtypeclass->has_html_answers();
918 $extraprocessing = array();
920 $coursemodulecontexts = array();
921 $context = get_context_instance(CONTEXT_COURSE
, $restore->course_id
);
922 $coursemodulecontexts[] = $context->id
;
923 $cms = get_records('course_modules', 'course', $restore->course_id
, '', 'id');
925 foreach ($cms as $cm){
926 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
927 $coursemodulecontexts[] = $context->id
;
930 $coursemodulecontextslist = join($coursemodulecontexts, ',');
931 // Decode links in questions.
932 if ($questions = get_records_sql('SELECT q.id, q.qtype, q.questiontext, q.generalfeedback '.
933 'FROM ' . $CFG->prefix
. 'question q, '.
934 $CFG->prefix
. 'question_categories qc '.
935 'WHERE q.category = qc.id '.
936 'AND qc.contextid IN (' .$coursemodulecontextslist.')')) {
938 foreach ($questions as $question) {
939 $questiontext = restore_decode_content_links_worker($question->questiontext
, $restore);
940 $generalfeedback = restore_decode_content_links_worker($question->generalfeedback
, $restore);
941 if ($questiontext != $question->questiontext ||
$generalfeedback != $question->generalfeedback
) {
942 $question->questiontext
= addslashes($questiontext);
943 $question->generalfeedback
= addslashes($generalfeedback);
944 if (!update_record('question', $question)) {
950 if (++
$i %
5 == 0 && !defined('RESTORE_SILENTLY')) {
958 // Decode any questiontype specific fields.
959 if ($qtypeswithextrafields[$question->qtype
]) {
960 if (!array_key_exists($question->qtype
, $extraprocessing)) {
961 $extraprocessing[$question->qtype
] = array();
963 $extraprocessing[$question->qtype
][] = $question->id
;
968 // Decode links in answers.
969 if ($answers = get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype
970 FROM ' . $CFG->prefix
. 'question_answers qa,
971 ' . $CFG->prefix
. 'question q,
972 ' . $CFG->prefix
. 'question_categories qc
973 WHERE qa.question = q.id
974 AND q.category = qc.id '.
975 'AND qc.contextid IN ('.$coursemodulecontextslist.')')) {
977 foreach ($answers as $answer) {
978 $feedback = restore_decode_content_links_worker($answer->feedback
, $restore);
979 if ($qtypeswithhtmlanswers[$answer->qtype
]) {
980 $answertext = restore_decode_content_links_worker($answer->answer
, $restore);
982 $answertext = $answer->answer
;
984 if ($feedback != $answer->feedback ||
$answertext != $answer->answer
) {
985 unset($answer->qtype
);
986 $answer->feedback
= addslashes($feedback);
987 $answer->answer
= addslashes($answertext);
988 if (!update_record('question_answers', $answer)) {
994 if (++
$i %
5 == 0 && !defined('RESTORE_SILENTLY')) {
1004 // Do extra work for certain question types.
1005 foreach ($extraprocessing as $qtype => $questionids) {
1006 if (!$QTYPES[$qtype]->decode_content_links_caller($questionids, $restore, $i)) {