Merge branch 'wip-MDL-28948-MOODLE_19_STABLE' of git://github.com/abgreeve/moodle...
[moodle.git] / question / restorelib.php
blobca8d940560a2fd53cddad46b0ca636921e952e6c
1 <?php // $Id$
2 /**
3 * Question bank restore code.
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package questionbank
7 *//** */
9 // Todo:
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
23 // (CL,pk->id)
24 // |
25 // |
26 // |.......................................
27 // | .
28 // | .
29 // | -------question_datasets------ .
30 // | | (CL,pk->id,fk->question, | .
31 // | | fk->dataset_definition) | .
32 // | | | .
33 // | | | .
34 // | | | .
35 // | | question_dataset_definitions
36 // | | (CL,pk->id,fk->category)
37 // question |
38 // (CL,pk->id,fk->category,files) |
39 // | question_dataset_items
40 // | (CL,pk->id,fk->definition)
41 // |
42 // |
43 // |
44 // --------------------------------------------------------------------------------------------------------------
45 // | | | | | | |
46 // | | | | | | |
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)
55 // . . . . . . | .
56 // . . . . . . | .
57 // . . . . . . | .
58 // . . . . . . | question_match_sub
59 // ........................................................................................ |--(CL,pk->id,fk->question)
60 // . |
61 // . |
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
69 // question_sessions
70 // (UL,pk->id,fk->attempt,question)
71 // .
72 // .
73 // question_states
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 /**
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]['#']) {
98 case 'module':
99 if (!$instanceinfo = backup_getid($restore->backup_unique_code, 'course_modules', $contextinfo['INSTANCE'][0]['#'])){
100 //module has not been restored, probably not selected for restore
101 return false;
103 $tocontext = get_context_instance(CONTEXT_MODULE, $instanceinfo->new_id);
104 break;
105 case 'course':
106 $tocontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
107 break;
108 case 'coursecategory':
109 //search COURSECATEGORYLEVEL steps up the course cat tree or
110 //to the top of the tree if steps are exhausted.
111 $catno = $contextinfo['COURSECATEGORYLEVEL'][0]['#'];
112 $catid = get_field('course', 'category', 'id', $restore->course_id);
113 while ($catno > 1){
114 $nextcatid = get_field('course_categories', 'parent', 'id', $catid);
115 if ($nextcatid == 0){
116 break;
118 $catid = $nextcatid;
119 $catno--;
121 $tocontext = get_context_instance(CONTEXT_COURSECAT, $catid);
122 break;
123 case 'system':
124 $tocontext = get_context_instance(CONTEXT_SYSTEM);
125 break;
127 return $tocontext;
130 function restore_question_categories($info, $restore) {
131 $status = true;
132 //Iterate over each category
133 foreach ($info as $category) {
134 $status = $status && restore_question_category($category, $restore);
136 $status = $status && restore_recode_category_parents($restore);
137 return $status;
140 function restore_question_category($category, $restore){
141 $status = true;
142 //Skip empty categories (some backups can contain them)
143 if (!empty($category->id)) {
144 //Get record from backup_ids
145 $data = backup_getid($restore->backup_unique_code, "question_categories", $category->id);
147 if ($data) {
148 //Now get completed xmlized object
149 $info = $data->info;
150 //traverse_xmlize($info); //Debug
151 //print_object ($GLOBALS['traverse_array']); //Debug
152 //$GLOBALS['traverse_array']=""; //Debug
154 //Now, build the question_categories record structure
155 $question_cat = new stdClass;
156 $question_cat->name = backup_todb($info['QUESTION_CATEGORY']['#']['NAME']['0']['#']);
157 $question_cat->info = backup_todb($info['QUESTION_CATEGORY']['#']['INFO']['0']['#']);
158 $question_cat->stamp = backup_todb($info['QUESTION_CATEGORY']['#']['STAMP']['0']['#']);
159 //parent is fixed after all categories are restored and we know all the new ids.
160 $question_cat->parent = backup_todb($info['QUESTION_CATEGORY']['#']['PARENT']['0']['#']);
161 $question_cat->sortorder = backup_todb($info['QUESTION_CATEGORY']['#']['SORTORDER']['0']['#']);
162 if (!$question_cat->stamp) {
163 $question_cat->stamp = make_unique_id_code();
165 if (isset($info['QUESTION_CATEGORY']['#']['PUBLISH'])) {
166 $course = $restore->course_id;
167 $publish = backup_todb($info['QUESTION_CATEGORY']['#']['PUBLISH']['0']['#']);
168 if ($publish){
169 $tocontext = get_context_instance(CONTEXT_SYSTEM);
170 } else {
171 $tocontext = get_context_instance(CONTEXT_COURSE, $course);
173 } else {
174 if (!$tocontext = restore_question_get_best_category_context($restore, $info['QUESTION_CATEGORY']['#']['CONTEXT']['0']['#'])){
175 return $status; // context doesn't exist - a module has not been restored
178 $question_cat->contextid = $tocontext->id;
180 //does cat exist ?? if it does we check if the cat and questions already exist whether we have
181 //add permission or not if we have no permission to add questions to SYSTEM or COURSECAT context
182 //AND the question does not already exist then we create questions in COURSE context.
183 if (!$fcat = get_record('question_categories','contextid', $question_cat->contextid, 'stamp', $question_cat->stamp)){
184 //no preexisting cat
185 if ((($tocontext->contextlevel == CONTEXT_SYSTEM) || ($tocontext->contextlevel == CONTEXT_COURSECAT))
186 && !has_capability('moodle/question:add', $tocontext)){
187 //no preexisting cat and no permission to create questions here
188 //must restore to course.
189 $tocontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
191 $question_cat->contextid = $tocontext->id;
192 if (!$fcat = get_record('question_categories','contextid', $question_cat->contextid, 'stamp', $question_cat->stamp)){
193 $question_cat->id = insert_record ("question_categories", $question_cat);
194 } else {
195 $question_cat = $fcat;
197 //we'll be restoring all questions here.
198 backup_putid($restore->backup_unique_code, "question_categories", $category->id, $question_cat->id);
199 } else {
200 $question_cat = $fcat;
201 //we found an existing best category
202 //but later if context is above course need to check if there are questions need creating in category
203 //if we do need to create questions and permissions don't allow it create new category in course
206 //Do some output
207 if (!defined('RESTORE_SILENTLY')) {
208 echo "<li>".get_string('category', 'quiz')." \"".$question_cat->name."\"<br />";
211 backup_flush(300);
213 //start with questions
214 if ($question_cat->id) {
215 //We have the newid, update backup_ids
216 //Now restore question
217 $status = restore_questions($category->id, $question_cat, $info, $restore);
218 } else {
219 $status = false;
221 if (!defined('RESTORE_SILENTLY')) {
222 echo '</li>';
224 } else {
225 echo 'Could not get backup info for question category'. $category->id;
228 return $status;
231 function restore_recode_category_parents($restore){
232 global $CFG;
233 $status = true;
234 //Now we have to recode the parent field of each restored category
235 $categories = get_records_sql("SELECT old_id, new_id
236 FROM {$CFG->prefix}backup_ids
237 WHERE backup_code = $restore->backup_unique_code AND
238 table_name = 'question_categories'");
239 if ($categories) {
240 //recode all parents to point at their old parent cats no matter what context the parent is now in
241 foreach ($categories as $category) {
242 $restoredcategory = get_record('question_categories','id',$category->new_id);
243 if ($restoredcategory && $restoredcategory->parent != 0) {
244 $updateobj = new object();
245 $updateobj->id = $restoredcategory->id;
246 $idcat = backup_getid($restore->backup_unique_code,'question_categories',$restoredcategory->parent);
247 if ($idcat->new_id) {
248 $updateobj->parent = $idcat->new_id;
249 } else {
250 $updateobj->parent = 0;
252 $status = $status && update_record('question_categories', $updateobj);
255 //now we have recoded all parents, check through all parents and set parent to be
256 //grand parent / great grandparent etc where there is one in same context
257 //or else set parent to 0 (top level category).
258 $toupdate = array();
259 foreach ($categories as $category) {
260 $restoredcategory = get_record('question_categories','id',$category->new_id);
261 if ($restoredcategory && $restoredcategory->parent != 0) {
262 $nextparentid = $restoredcategory->parent;
263 do {
264 if (!$parent = get_record('question_categories', 'id', $nextparentid)){
265 if (!defined('RESTORE_SILENTLY')) {
266 echo 'Could not find parent for question category '. $category->id.' recoding as top category item.<br />';
268 break;//record fetch failed finish loop
269 } else {
270 $nextparentid = $parent->parent;
272 } while (($nextparentid != 0) && ($parent->contextid != $restoredcategory->contextid));
273 if (!$parent || ($parent->id != $restoredcategory->parent)){
274 //change needs to be made to the parent field.
275 if ($parent && ($parent->contextid == $restoredcategory->contextid)){
276 $toupdate[$restoredcategory->id] = $parent->id;
277 } else {
278 //searched up the tree till we came to the top and did not find cat in same
279 //context or there was an error getting next parent record
280 $toupdate[$restoredcategory->id] = 0;
285 //now finally do the changes to parent field.
286 foreach ($toupdate as $id => $parent){
287 $updateobj = new object();
288 $updateobj->id = $id;
289 $updateobj->parent = $parent;
290 $status = $status && update_record('question_categories', $updateobj);
293 return $status;
296 function restore_questions ($old_category_id, $best_question_cat, $info, $restore) {
298 global $CFG, $QTYPES;
300 $status = true;
301 $restored_questions = array();
303 //Get the questions array
304 if (!empty($info['QUESTION_CATEGORY']['#']['QUESTIONS'])) {
305 $questions = $info['QUESTION_CATEGORY']['#']['QUESTIONS']['0']['#']['QUESTION'];
306 } else {
307 $questions = array();
310 //Iterate over questions
311 for($i = 0; $i < sizeof($questions); $i++) {
312 $que_info = $questions[$i];
313 //traverse_xmlize($que_info); //Debug
314 //print_object ($GLOBALS['traverse_array']); //Debug
315 //$GLOBALS['traverse_array']=""; //Debug
317 //We'll need this later!!
318 $oldid = backup_todb($que_info['#']['ID']['0']['#']);
320 //Now, build the question record structure
321 $question = new object;
322 $question->parent = backup_todb($que_info['#']['PARENT']['0']['#']);
323 $question->name = backup_todb($que_info['#']['NAME']['0']['#']);
324 $question->questiontext = backup_todb($que_info['#']['QUESTIONTEXT']['0']['#']);
325 $question->questiontextformat = backup_todb($que_info['#']['QUESTIONTEXTFORMAT']['0']['#']);
326 $question->image = backup_todb($que_info['#']['IMAGE']['0']['#']);
327 $question->generalfeedback = backup_todb_optional_field($que_info, 'GENERALFEEDBACK', '');
328 $question->defaultgrade = backup_todb($que_info['#']['DEFAULTGRADE']['0']['#']);
329 $question->penalty = backup_todb($que_info['#']['PENALTY']['0']['#']);
330 $question->qtype = backup_todb($que_info['#']['QTYPE']['0']['#']);
331 $question->length = backup_todb($que_info['#']['LENGTH']['0']['#']);
332 $question->stamp = backup_todb($que_info['#']['STAMP']['0']['#']);
333 $question->version = backup_todb($que_info['#']['VERSION']['0']['#']);
334 $question->hidden = backup_todb($que_info['#']['HIDDEN']['0']['#']);
335 $question->timecreated = backup_todb_optional_field($que_info, 'TIMECREATED', 0);
336 $question->timemodified = backup_todb_optional_field($que_info, 'TIMEMODIFIED', 0);
338 // Set the createdby field, if the user was in the backup, or if we are on the same site.
339 $createdby = backup_todb_optional_field($que_info, 'CREATEDBY', null);
340 if (!empty($createdby)) {
341 $user = backup_getid($restore->backup_unique_code, 'user', $createdby);
342 if ($user) {
343 $question->createdby = $user->new_id;
344 } else if (backup_is_same_site($restore)) {
345 $question->createdby = $createdby;
349 // Set the modifiedby field, if the user was in the backup, or if we are on the same site.
350 $modifiedby = backup_todb_optional_field($que_info, 'MODIFIEDBY', null);
351 if (!empty($createdby)) {
352 $user = backup_getid($restore->backup_unique_code, 'user', $modifiedby);
353 if ($user) {
354 $question->modifiedby = $user->new_id;
355 } else if (backup_is_same_site($restore)) {
356 $question->modifiedby = $modifiedby;
360 if ($restore->backup_version < 2006032200) {
361 // The qtype was an integer that now needs to be converted to the name
362 $qtypenames = array(1=>'shortanswer',2=>'truefalse',3=>'multichoice',4=>'random',5=>'match',
363 6=>'randomsamatch',7=>'description',8=>'numerical',9=>'multianswer',10=>'calculated',
364 11=>'rqp',12=>'essay');
365 $question->qtype = $qtypenames[$question->qtype];
368 //Check if the question exists by category, stamp, and version
369 //first check for the question in the context specified in backup
370 $existingquestion = get_record ("question", "category", $best_question_cat->id, "stamp", $question->stamp,"version",$question->version);
371 //If the question exists, only record its id
372 //always use existing question, no permissions check here
373 if ($existingquestion) {
374 $question = $existingquestion;
375 $creatingnewquestion = false;
376 } else {
377 //then if context above course level check permissions and if no permission
378 //to restore above course level then restore to cat in course context.
379 $bestcontext = get_context_instance_by_id($best_question_cat->contextid);
380 if (($bestcontext->contextlevel == CONTEXT_SYSTEM || $bestcontext->contextlevel == CONTEXT_COURSECAT)
381 && !has_capability('moodle/question:add', $bestcontext)){
382 if (!isset($course_question_cat)) {
383 $coursecontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
384 $course_question_cat = clone($best_question_cat);
385 $course_question_cat->contextid = $coursecontext->id;
386 //create cat if it doesn't exist
387 if (!$fcat = get_record('question_categories','contextid', $course_question_cat->contextid, 'stamp', $course_question_cat->stamp)){
388 $course_question_cat->id = insert_record ("question_categories", $course_question_cat);
389 backup_putid($restore->backup_unique_code, "question_categories", $old_category_id, $course_question_cat->id);
390 } else {
391 $course_question_cat = $fcat;
393 //will fix category parents after all questions and categories restored. Will set parent to 0 if
394 //no parent in same context.
396 $question->category = $course_question_cat->id;
397 //does question already exist in course cat
398 $existingquestion = get_record ("question", "category", $question->category, "stamp", $question->stamp, "version", $question->version);
399 } else {
400 //permissions ok, restore to best cat
401 $question->category = $best_question_cat->id;
403 if (!$existingquestion){
404 //The structure is equal to the db, so insert the question
405 $question->id = insert_record ("question", $question);
406 $creatingnewquestion = true;
407 } else {
408 $question = $existingquestion;
409 $creatingnewquestion = false;
413 // Fixing bug #5482: random questions have parent field set to its own id,
414 // see: $QTYPES['random']->get_question_options()
415 if ($question->qtype == 'random' && $creatingnewquestion) {
416 $question->parent = $question->id;
417 $status = set_field('question', 'parent', $question->parent, 'id', $question->id);
420 //Save newid to backup tables
421 if ($question->id) {
422 //We have the newid, update backup_ids
423 backup_putid($restore->backup_unique_code, "question", $oldid, $question->id);
426 $restored_questions[$i] = new stdClass;
427 $restored_questions[$i]->newid = $question->id;
428 $restored_questions[$i]->oldid = $oldid;
429 $restored_questions[$i]->qtype = $question->qtype;
430 $restored_questions[$i]->parent = $question->parent;
431 $restored_questions[$i]->is_new = $creatingnewquestion;
433 backup_flush(300);
435 // Loop again, now all the question id mappings exist, so everything can
436 // be restored.
437 for($i = 0; $i < sizeof($questions); $i++) {
438 $que_info = $questions[$i];
440 $newid = $restored_questions[$i]->newid;
441 $oldid = $restored_questions[$i]->oldid;
443 $question = new object;
444 $question->qtype = $restored_questions[$i]->qtype;
445 $question->parent = $restored_questions[$i]->parent;
448 /// If it's a new question in the DB, restore it
449 if ($restored_questions[$i]->is_new) {
451 /// We have to recode the parent field
452 if ($question->parent && $question->qtype != 'random') {
453 /// If the parent field needs to be changed, do it here. Random questions are dealt with above.
454 if ($parent = backup_getid($restore->backup_unique_code,"question",$question->parent)) {
455 $question->parent = $parent->new_id;
456 if ($question->parent != $restored_questions[$i]->parent) {
457 if (!set_field('question', 'parent', $question->parent, 'id', $newid)) {
458 echo 'Could not update parent '.$question->parent.' for question '.$oldid.'<br />';
459 $status = false;
462 } else {
463 echo 'Could not recode parent '.$question->parent.' for question '.$oldid.'<br />';
464 $status = false;
468 //Now, restore every question_answers in this question
469 $status = question_restore_answers($oldid,$newid,$que_info,$restore);
470 // Restore questiontype specific data
471 if (array_key_exists($question->qtype, $QTYPES)) {
472 $status = $QTYPES[$question->qtype]->restore($oldid,$newid,$que_info,$restore);
473 } else {
474 echo 'Unknown question type '.$question->qtype.' for question '.$oldid.'<br />';
475 $status = false;
477 } else {
478 //We are NOT creating the question, but we need to know every question_answers
479 //map between the XML file and the database to be able to restore the states
480 //in each attempt.
481 $status = question_restore_map_answers($oldid,$newid,$que_info,$restore);
482 // Do the questiontype specific mapping
483 if (array_key_exists($question->qtype, $QTYPES)) {
484 $status = $QTYPES[$question->qtype]->restore_map($oldid,$newid,$que_info,$restore);
485 } else {
486 echo 'Unknown question type '.$question->qtype.' for question '.$oldid.'<br />';
487 $status = false;
491 //Do some output
492 if (($i+1) % 2 == 0) {
493 if (!defined('RESTORE_SILENTLY')) {
494 echo ".";
495 if (($i+1) % 40 == 0) {
496 echo "<br />";
499 backup_flush(300);
502 return $status;
505 function backup_todb_optional_field($data, $field, $default) {
506 if (array_key_exists($field, $data['#'])) {
507 return backup_todb($data['#'][$field]['0']['#']);
508 } else {
509 return $default;
513 function question_restore_answers ($old_question_id,$new_question_id,$info,$restore) {
515 global $CFG;
517 $status = true;
518 $qtype = backup_todb($info['#']['QTYPE']['0']['#']);
520 //Get the answers array
521 if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
522 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
524 //Iterate over answers
525 for($i = 0; $i < sizeof($answers); $i++) {
526 $ans_info = $answers[$i];
527 //traverse_xmlize($ans_info); //Debug
528 //print_object ($GLOBALS['traverse_array']); //Debug
529 //$GLOBALS['traverse_array']=""; //Debug
531 //We'll need this later!!
532 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
534 //Now, build the question_answers record structure
535 $answer = new stdClass;
536 $answer->question = $new_question_id;
537 $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
538 $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
539 $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
541 // Update 'match everything' answers for numerical questions coming from old backup files.
542 if ($qtype == 'numerical' && $answer->answer == '') {
543 $answer->answer = '*';
546 //The structure is equal to the db, so insert the question_answers
547 $newid = insert_record ("question_answers",$answer);
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 //We have the newid, update backup_ids
562 backup_putid($restore->backup_unique_code,"question_answers",$oldid,
563 $newid);
564 } else {
565 $status = false;
570 return $status;
573 function question_restore_map_answers ($old_question_id,$new_question_id,$info,$restore) {
575 global $CFG;
577 $status = true;
579 if (!isset($info['#']['ANSWERS'])) { // No answers in this question (eg random)
580 return $status;
583 //Get the answers array
584 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
586 //Iterate over answers
587 for($i = 0; $i < sizeof($answers); $i++) {
588 $ans_info = $answers[$i];
589 //traverse_xmlize($ans_info); //Debug
590 //print_object ($GLOBALS['traverse_array']); //Debug
591 //$GLOBALS['traverse_array']=""; //Debug
593 //We'll need this later!!
594 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
596 //Now, build the question_answers record structure
597 $answer->question = $new_question_id;
598 $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']);
599 $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']);
600 $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']);
602 //If we are in this method is because the question exists in DB, so its
603 //answers must exist too.
604 //Now, we are going to look for that answer in DB and to create the
605 //mappings in backup_ids to use them later where restoring states (user level).
607 //Get the answer from DB (by question and answer)
608 $db_answer = get_record ("question_answers","question",$new_question_id,
609 "answer",$answer->answer);
611 //Do some output
612 if (($i+1) % 50 == 0) {
613 if (!defined('RESTORE_SILENTLY')) {
614 echo ".";
615 if (($i+1) % 1000 == 0) {
616 echo "<br />";
619 backup_flush(300);
622 if ($db_answer) {
623 //We have the database answer, update backup_ids
624 backup_putid($restore->backup_unique_code,"question_answers",$oldid,
625 $db_answer->id);
626 } else {
627 $status = false;
631 return $status;
634 function question_restore_numerical_units($old_question_id,$new_question_id,$info,$restore) {
636 global $CFG;
638 $status = true;
640 //Get the numerical array
641 if (!empty($info['#']['NUMERICAL_UNITS'])) {
642 $numerical_units = $info['#']['NUMERICAL_UNITS']['0']['#']['NUMERICAL_UNIT'];
643 } else {
644 $numerical_units = array();
647 //Iterate over numerical_units
648 for($i = 0; $i < sizeof($numerical_units); $i++) {
649 $nu_info = $numerical_units[$i];
650 //traverse_xmlize($nu_info); //Debug
651 //print_object ($GLOBALS['traverse_array']); //Debug
652 //$GLOBALS['traverse_array']=""; //Debug
654 // Check to see if this until already exists in the database, which it might, for
655 // Historical reasons.
656 $unit = backup_todb($nu_info['#']['UNIT']['0']['#']);
657 if (!record_exists('question_numerical_units', 'question', $new_question_id, 'unit', $unit)) {
659 //Now, build the question_numerical_UNITS record structure.
660 $numerical_unit = new stdClass;
661 $numerical_unit->question = $new_question_id;
662 $numerical_unit->multiplier = backup_todb($nu_info['#']['MULTIPLIER']['0']['#']);
663 $numerical_unit->unit = $unit;
665 //The structure is equal to the db, so insert the question_numerical_units
666 $newid = insert_record("question_numerical_units", $numerical_unit);
668 if (!$newid) {
669 $status = false;
674 return $status;
677 function question_restore_dataset_definitions ($old_question_id,$new_question_id,$info,$restore) {
679 global $CFG;
681 $status = true;
683 //Get the dataset_definitions array
684 $dataset_definitions = $info['#']['DATASET_DEFINITIONS']['0']['#']['DATASET_DEFINITION'];
686 //Iterate over dataset_definitions
687 for($i = 0; $i < sizeof($dataset_definitions); $i++) {
688 $dd_info = $dataset_definitions[$i];
689 //traverse_xmlize($dd_info); //Debug
690 //print_object ($GLOBALS['traverse_array']); //Debug
691 //$GLOBALS['traverse_array']=""; //Debug
693 //Now, build the question_dataset_DEFINITION record structure
694 $dataset_definition = new stdClass;
695 $dataset_definition->category = backup_todb($dd_info['#']['CATEGORY']['0']['#']);
696 $dataset_definition->name = backup_todb($dd_info['#']['NAME']['0']['#']);
697 $dataset_definition->type = backup_todb($dd_info['#']['TYPE']['0']['#']);
698 $dataset_definition->options = backup_todb($dd_info['#']['OPTIONS']['0']['#']);
699 $dataset_definition->itemcount = backup_todb($dd_info['#']['ITEMCOUNT']['0']['#']);
701 //We have to recode the category field (only if the category != 0)
702 if ($dataset_definition->category != 0) {
703 $category = backup_getid($restore->backup_unique_code,"question_categories",$dataset_definition->category);
704 if ($category) {
705 $dataset_definition->category = $category->new_id;
706 } else {
707 echo 'Could not recode category id '.$dataset_definition->category.' for dataset definition'.$dataset_definition->name.'<br />';
711 //Now, we hace to decide when to create the new records or reuse an existing one
712 $create_definition = false;
714 //If the dataset_definition->category = 0, it's a individual question dataset_definition, so we'll create it
715 if ($dataset_definition->category == 0) {
716 $create_definition = true;
717 } else {
718 //The category isn't 0, so it's a category question dataset_definition, we have to see if it exists
719 //Look for a definition with the same category, name and type
720 if ($definitionrec = get_record_sql("SELECT d.*
721 FROM {$CFG->prefix}question_dataset_definitions d
722 WHERE d.category = '$dataset_definition->category' AND
723 d.name = '$dataset_definition->name' AND
724 d.type = '$dataset_definition->type'")) {
725 //Such dataset_definition exist. Now we must check if it has enough itemcount
726 if ($definitionrec->itemcount < $dataset_definition->itemcount) {
727 //We haven't enough itemcount, so we have to create the definition as an individual question one.
728 $dataset_definition->category = 0;
729 $create_definition = true;
730 } else {
731 //We have enough itemcount, so we'll reuse the existing definition
732 $create_definition = false;
733 $newid = $definitionrec->id;
735 } else {
736 //Such dataset_definition doesn't exist. We'll create it.
737 $create_definition = true;
741 //If we've to create the definition, do it
742 if ($create_definition) {
743 //The structure is equal to the db, so insert the question_dataset_definitions
744 $newid = insert_record ("question_dataset_definitions",$dataset_definition);
745 if ($newid) {
746 //Restore question_dataset_items
747 $status = question_restore_dataset_items($newid,$dd_info,$restore);
751 //Now, we must have a definition (created o reused). Its id is in newid. Create the question_datasets record
752 //to join the question and the dataset_definition
753 if ($newid) {
754 $question_dataset = new stdClass;
755 $question_dataset->question = $new_question_id;
756 $question_dataset->datasetdefinition = $newid;
757 $newid = insert_record ("question_datasets",$question_dataset);
760 if (!$newid) {
761 $status = false;
765 return $status;
768 function question_restore_dataset_items ($definitionid,$info,$restore) {
770 global $CFG;
772 $status = true;
774 //Get the items array
775 $dataset_items = $info['#']['DATASET_ITEMS']['0']['#']['DATASET_ITEM'];
777 //Iterate over dataset_items
778 for($i = 0; $i < sizeof($dataset_items); $i++) {
779 $di_info = $dataset_items[$i];
780 //traverse_xmlize($di_info); //Debug
781 //print_object ($GLOBALS['traverse_array']); //Debug
782 //$GLOBALS['traverse_array']=""; //Debug
784 //Now, build the question_dataset_ITEMS record structure
785 $dataset_item = new stdClass;
786 $dataset_item->definition = $definitionid;
787 $dataset_item->itemnumber = backup_todb($di_info['#']['NUMBER']['0']['#']);
788 $dataset_item->value = backup_todb($di_info['#']['VALUE']['0']['#']);
790 //The structure is equal to the db, so insert the question_dataset_items
791 $newid = insert_record ("question_dataset_items",$dataset_item);
793 if (!$newid) {
794 $status = false;
798 return $status;
802 //This function restores the question_states
803 function question_states_restore_mods($attempt_id,$info,$restore) {
805 global $CFG, $QTYPES;
807 $status = true;
809 //Get the question_states array
810 $states = $info['#']['STATES']['0']['#']['STATE'];
811 //Iterate over states
812 for($i = 0; $i < sizeof($states); $i++) {
813 $res_info = $states[$i];
814 //traverse_xmlize($res_info); //Debug
815 //print_object ($GLOBALS['traverse_array']); //Debug
816 //$GLOBALS['traverse_array']=""; //Debug
818 //We'll need this later!!
819 $oldid = backup_todb($res_info['#']['ID']['0']['#']);
821 //Now, build the STATES record structure
822 $state = new stdClass;
823 $state->attempt = $attempt_id;
824 $state->question = backup_todb($res_info['#']['QUESTION']['0']['#']);
825 $state->originalquestion = backup_todb($res_info['#']['ORIGINALQUESTION']['0']['#']);
826 $state->seq_number = backup_todb($res_info['#']['SEQ_NUMBER']['0']['#']);
827 $state->answer = backup_todb($res_info['#']['ANSWER']['0']['#']);
828 $state->timestamp = backup_todb($res_info['#']['TIMESTAMP']['0']['#']);
829 $state->event = backup_todb($res_info['#']['EVENT']['0']['#']);
830 $state->grade = backup_todb($res_info['#']['GRADE']['0']['#']);
831 $state->raw_grade = backup_todb($res_info['#']['RAW_GRADE']['0']['#']);
832 $state->penalty = backup_todb($res_info['#']['PENALTY']['0']['#']);
833 $state->oldid = $oldid; // So it is available to restore_recode_answer.
835 //We have to recode the question field
836 $question = backup_getid($restore->backup_unique_code,"question",$state->question);
837 if ($question) {
838 $state->question = $question->new_id;
839 } else {
840 echo 'Could not recode question id '.$state->question.' for state '.$oldid.'<br />';
843 //We have to recode the originalquestion field if it is nonzero
844 if ($state->originalquestion) {
845 $question = backup_getid($restore->backup_unique_code,"question",$state->originalquestion);
846 if ($question) {
847 $state->originalquestion = $question->new_id;
848 } else {
849 echo 'Could not recode originalquestion id '.$state->question.' for state '.$oldid.'<br />';
853 //We have to recode the answer field
854 //It depends of the question type !!
855 //We get the question first
856 if (!$question = get_record("question","id",$state->question)) {
857 error("Can't find the record for question $state->question for which I am trying to restore a state");
859 //Depending on the qtype, we make different recodes
860 if ($state->answer) {
861 $state->answer = $QTYPES[$question->qtype]->restore_recode_answer($state, $restore);
864 //The structure is equal to the db, so insert the question_states
865 $newid = insert_record ("question_states",$state);
867 //Do some output
868 if (($i+1) % 10 == 0) {
869 if (!defined('RESTORE_SILENTLY')) {
870 echo ".";
871 if (($i+1) % 200 == 0) {
872 echo "<br />";
875 backup_flush(300);
878 if ($newid) {
879 //We have the newid, update backup_ids
880 backup_putid($restore->backup_unique_code, 'question_states', $oldid, $newid);
881 } else {
882 $status = false;
886 //Get the question_sessions array
887 $sessions = $info['#']['NEWEST_STATES']['0']['#']['NEWEST_STATE'];
888 //Iterate over question_sessions
889 for($i = 0; $i < sizeof($sessions); $i++) {
890 $res_info = $sessions[$i];
891 //traverse_xmlize($res_info); //Debug
892 //print_object ($GLOBALS['traverse_array']); //Debug
893 //$GLOBALS['traverse_array']=""; //Debug
895 //Now, build the NEWEST_STATES record structure
896 $session = new stdClass;
897 $session->attemptid = $attempt_id;
898 $session->questionid = backup_todb($res_info['#']['QUESTIONID']['0']['#']);
899 $session->newest = backup_todb($res_info['#']['NEWEST']['0']['#']);
900 $session->newgraded = backup_todb($res_info['#']['NEWGRADED']['0']['#']);
901 $session->sumpenalty = backup_todb($res_info['#']['SUMPENALTY']['0']['#']);
903 if (isset($res_info['#']['MANUALCOMMENT']['0']['#'])) {
904 $session->manualcomment = backup_todb($res_info['#']['MANUALCOMMENT']['0']['#']);
905 } else { // pre 1.7 backups
906 $session->manualcomment = backup_todb($res_info['#']['COMMENT']['0']['#']);
909 //We have to recode the question field
910 $question = backup_getid($restore->backup_unique_code,"question",$session->questionid);
911 if ($question) {
912 $session->questionid = $question->new_id;
913 } else {
914 echo 'Could not recode question id '.$session->questionid.'<br />';
917 //We have to recode the newest field
918 $state = backup_getid($restore->backup_unique_code,"question_states",$session->newest);
919 if ($state) {
920 $session->newest = $state->new_id;
921 } else {
922 echo 'Could not recode newest state id '.$session->newest.'<br />';
925 //If the session has been graded we have to recode the newgraded field
926 if ($session->newgraded) {
927 $state = backup_getid($restore->backup_unique_code,"question_states",$session->newgraded);
928 if ($state) {
929 $session->newgraded = $state->new_id;
930 } else {
931 echo 'Could not recode newest graded state id '.$session->newgraded.'<br />';
935 //The structure is equal to the db, so insert the question_sessions
936 $newid = insert_record ("question_sessions",$session);
940 return $status;
944 * Recode content links in question texts.
945 * @param object $restore the restore metadata object.
946 * @return boolean whether the operation succeeded.
948 function question_decode_content_links_caller($restore) {
949 global $CFG, $QTYPES;
950 $status = true;
951 $i = 1; //Counter to send some output to the browser to avoid timeouts
953 // Get a list of which question types have custom field that will need decoding.
954 $qtypeswithextrafields = array();
955 $qtypeswithhtmlanswers = array();
956 foreach ($QTYPES as $qtype => $qtypeclass) {
957 $qtypeswithextrafields[$qtype] = method_exists($qtypeclass, 'decode_content_links_caller');
958 $qtypeswithhtmlanswers[$qtype] = $qtypeclass->has_html_answers();
960 $extraprocessing = array();
962 $coursemodulecontexts = array();
963 $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
964 $coursemodulecontexts[] = $context->id;
965 $cms = get_records('course_modules', 'course', $restore->course_id, '', 'id');
966 if ($cms){
967 foreach ($cms as $cm){
968 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
969 $coursemodulecontexts[] = $context->id;
972 $coursemodulecontextslist = join($coursemodulecontexts, ',');
973 // Decode links in questions.
974 if ($questions = get_records_sql('SELECT q.id, q.qtype, q.questiontext, q.generalfeedback '.
975 'FROM ' . $CFG->prefix . 'question q, '.
976 $CFG->prefix . 'question_categories qc '.
977 'WHERE q.category = qc.id '.
978 'AND qc.contextid IN (' .$coursemodulecontextslist.')')) {
980 foreach ($questions as $question) {
981 $questiontext = restore_decode_content_links_worker($question->questiontext, $restore);
982 $generalfeedback = restore_decode_content_links_worker($question->generalfeedback, $restore);
983 if ($questiontext != $question->questiontext || $generalfeedback != $question->generalfeedback) {
984 $question->questiontext = addslashes($questiontext);
985 $question->generalfeedback = addslashes($generalfeedback);
986 if (!update_record('question', $question)) {
987 $status = false;
991 // Do some output.
992 if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
993 echo ".";
994 if ($i % 100 == 0) {
995 echo "<br />";
997 backup_flush(300);
1000 // Decode any questiontype specific fields.
1001 if ($qtypeswithextrafields[$question->qtype]) {
1002 if (!array_key_exists($question->qtype, $extraprocessing)) {
1003 $extraprocessing[$question->qtype] = array();
1005 $extraprocessing[$question->qtype][] = $question->id;
1010 // Decode links in answers.
1011 if ($answers = get_records_sql('SELECT qa.id, qa.answer, qa.feedback, q.qtype
1012 FROM ' . $CFG->prefix . 'question_answers qa,
1013 ' . $CFG->prefix . 'question q,
1014 ' . $CFG->prefix . 'question_categories qc
1015 WHERE qa.question = q.id
1016 AND q.category = qc.id '.
1017 'AND qc.contextid IN ('.$coursemodulecontextslist.')')) {
1019 foreach ($answers as $answer) {
1020 $feedback = restore_decode_content_links_worker($answer->feedback, $restore);
1021 if ($qtypeswithhtmlanswers[$answer->qtype]) {
1022 $answertext = restore_decode_content_links_worker($answer->answer, $restore);
1023 } else {
1024 $answertext = $answer->answer;
1026 if ($feedback != $answer->feedback || $answertext != $answer->answer) {
1027 unset($answer->qtype);
1028 $answer->feedback = addslashes($feedback);
1029 $answer->answer = addslashes($answertext);
1030 if (!update_record('question_answers', $answer)) {
1031 $status = false;
1035 // Do some output.
1036 if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {
1037 echo ".";
1038 if ($i % 100 == 0) {
1039 echo "<br />";
1041 backup_flush(300);
1046 // Do extra work for certain question types.
1047 foreach ($extraprocessing as $qtype => $questionids) {
1048 if (!$QTYPES[$qtype]->decode_content_links_caller($questionids, $restore, $i)) {
1049 $status = false;
1053 return $status;