From f43f3f65bce4311e3e430954aa81c20535be1309 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Fri, 15 Aug 2003 11:08:51 +0000 Subject: [PATCH] Solved one important bug in RANDOM(4) and RANDOMSAMATCH(6) question types. And modified some of the quiz_answers restore system to allow remapping answers with existing questions. //Detected by scott dot elliott at mchsi dot com in Bug 84 --- backup/version.php | 4 +-- mod/quiz/restorelib.php | 74 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/backup/version.php b/backup/version.php index 78d0be9150..d617843149 100644 --- a/backup/version.php +++ b/backup/version.php @@ -5,6 +5,6 @@ // database (backup_version) to determine whether upgrades should // be performed (see db/backup_*.php) -$backup_version = 2003080400; // The current version is a date (YYYYMMDDXX) +$backup_version = 2003081500; // The current version is a date (YYYYMMDDXX) -$backup_release = "0.8.6 alpha"; // User-friendly version number +$backup_release = "0.8.7 alpha"; // User-friendly version number diff --git a/mod/quiz/restorelib.php b/mod/quiz/restorelib.php index 2aeb2d9cab..19ab2bb1c5 100644 --- a/mod/quiz/restorelib.php +++ b/mod/quiz/restorelib.php @@ -198,9 +198,17 @@ //Check if the question exists //by category,questiontext and qtype - $question_exists = get_record ("quiz_questions","category",$question->category, - "questiontext",$question->questiontext, - "qtype",$question->qtype); + //But do it only if the question type isn't RANDOM (4) or RANDOMSAMATCH (6), because this + //types can have duplicates in the fields I'm searching !! + //I think that some modifications should be done here !! + //Detected by scott dot elliott at mchsi dot com in Bug 84 + if ($question->qtype != 4 and $question->qtype != 6) { + $question_exists = get_record ("quiz_questions","category",$question->category, + "questiontext",$question->questiontext, + "qtype",$question->qtype); + } else { + $question_exists = false; + } //If the question exists, only record its id if ($question_exists) { $newid = $question_exists->id; @@ -248,6 +256,11 @@ } else if ($question->qtype == "8") { $status = quiz_restore_numerical($oldid,$newid,$que_info,$restore); } + } else { + //We are NOT creating the question, but we need to know every quiz_answers + //map between the XML file and the database to be able to restore the responses + //in each attempt. + $status = quiz_restore_map_answers($oldid,$newid,$que_info,$restore); } } return $status; @@ -301,6 +314,61 @@ return $status; } + + function quiz_restore_map_answers ($old_question_id,$new_question_id,$info,$restore) { + + global $CFG; + + $status = true; + + //Get the answers array + $answers = $info['#']['ANSWERS']['0']['#']['ANSWER']; + + //Iterate over answers + for($i = 0; $i < sizeof($answers); $i++) { + $ans_info = $answers[$i]; + //traverse_xmlize($ans_info); //Debug + //print_object ($GLOBALS['traverse_array']); //Debug + //$GLOBALS['traverse_array']=""; //Debug + + //We'll need this later!! + $oldid = backup_todb($ans_info['#']['ID']['0']['#']); + + //Now, build the QUIZ_ANSWERS record structure + $answer->question = $new_question_id; + $answer->answer = backup_todb($ans_info['#']['ANSWER_TEXT']['0']['#']); + $answer->fraction = backup_todb($ans_info['#']['FRACTION']['0']['#']); + $answer->feedback = backup_todb($ans_info['#']['FEEDBACK']['0']['#']); + + //If we are in this method is because the question exists in DB, so its + //answers must exist too. + //Now, we are going to look for that answer in DB and to create the + //mappings in backup_ids to use them later where restoring responses (user level). + + //Get the answer from DB (by question and answer) + $db_answer = get_record ("quiz_answers","question",$new_question_id, + "answer",$answer->answer); + + //Do some output + if (($i+1) % 50 == 0) { + echo "."; + if (($i+1) % 1000 == 0) { + echo "
"; + } + backup_flush(300); + } + + if ($db_answer) { + //We have the database answer, update backup_ids + backup_putid($restore->backup_unique_code,"quiz_answers",$oldid, + $db_answer->id); + } else { + $status = false; + } + } + + return $status; + } function quiz_restore_shortanswer ($old_question_id,$new_question_id,$info,$restore) { -- 2.39.5