From a3f59740650cd1d6adbdd5137d1869f4a59bafcd Mon Sep 17 00:00:00 2001 From: Corey Wallis Date: Thu, 13 Aug 2015 14:17:50 +0930 Subject: [PATCH] MDL-51048 IMS-CC: Fix import & export of true/false questions --- backup/cc/cc_lib/cc_assesment_truefalse.php | 14 ++++++++-- backup/cc/entity.quiz.class.php | 30 ++++++++++++--------- backup/cc/entity11.quiz.class.php | 30 ++++++++++++--------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/backup/cc/cc_lib/cc_assesment_truefalse.php b/backup/cc/cc_lib/cc_assesment_truefalse.php index 64d0e7958e8..8a14e914ef6 100644 --- a/backup/cc/cc_lib/cc_assesment_truefalse.php +++ b/backup/cc/cc_lib/cc_assesment_truefalse.php @@ -27,8 +27,18 @@ class cc_assesment_question_truefalse extends cc_assesment_question_proc_base { public function __construct($quiz, $questions, $manifest, $section, $question_node, $rootpath, $contextid, $outdir) { parent::__construct($quiz, $questions, $manifest, $section, $question_node, $rootpath, $contextid, $outdir); $this->qtype = cc_qti_profiletype::true_false; - $this->correct_answer_node_id = $this->questions->nodeValue( - 'plugin_qtype_truefalse_question/truefalse/trueanswer', $this->question_node); + + // Determine the correct answer by finding out which answer has the non zero fraction... + // This is because a true / false question type can have 'false' as the correct answer. + $answers = $this->questions->nodeList('plugin_qtype_truefalse_question/answers/answer', $this->question_node); + foreach ($answers as $answer) { + $fraction = $this->questions->nodeValue('fraction', $answer); + + if ($fraction != 0) { + $this->correct_answer_node_id = (int)$this->questions->nodeValue('@id', $answer); + } + } + $maximum_quiz_grade = (int)$this->quiz->nodeValue('/activity/quiz/grade'); $this->total_grade_value = ($maximum_quiz_grade + 1).'.0000000'; } diff --git a/backup/cc/entity.quiz.class.php b/backup/cc/entity.quiz.class.php index 56c42de19c6..81081761633 100644 --- a/backup/cc/entity.quiz.class.php +++ b/backup/cc/entity.quiz.class.php @@ -886,26 +886,30 @@ class cc_quiz extends entities { $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_TRUE_FALSE); $max_score = 0; - $true_answer_id = 0; - $false_answer_id = 0; + $trueanswer = null; + $falseanswer = null; if (!empty($question['answers'])) { + // Identify the true and false answers. foreach ($question['answers'] as $answer) { - if ($answer['score'] > $max_score) { - $max_score = $answer['score']; - $true_answer_id = $answer['id']; + if ($answer['identifier'] == 'true') { + $trueanswer = $answer; + } else if ($answer['identifier'] == 'false') { + $falseanswer = $answer; + } else { + // Should not happen, but just in case. + throw new coding_exception("Unknown answer identifier detected" . + " in true/false quiz question with id {$question['id']}."); } $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer); } - foreach ($question['answers'] as $answer) { - - if ($answer['id'] != $true_answer_id) { - $max_score = $answer['score']; - $false_answer_id = $answer['id']; - } + // Make sure the true and false answer was found. + if (is_null($trueanswer) || is_null($falseanswer)) { + throw new coding_exception("Unable to correctly identify the " . + "true and false answers in the question with id {$question['id']}."); } } @@ -914,8 +918,8 @@ class cc_quiz extends entities { '[#false_answer_id#]'); $replace_values = array($node_course_question_categories_question_answer, - $true_answer_id, - $false_answer_id); + $trueanswer['id'], + $falseanswer['id']); $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question); diff --git a/backup/cc/entity11.quiz.class.php b/backup/cc/entity11.quiz.class.php index 5410caa9ea9..f19a4696194 100644 --- a/backup/cc/entity11.quiz.class.php +++ b/backup/cc/entity11.quiz.class.php @@ -909,26 +909,30 @@ class cc11_quiz extends entities11 { $sheet_question_categories_question = cc112moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_TRUE_FALSE); $max_score = 0; - $true_answer_id = 0; - $false_answer_id = 0; + $trueanswer = null; + $falseanswer = null; if (!empty($question['answers'])) { + // Identify the true and false answers. foreach ($question['answers'] as $answer) { - if ($answer['score'] > $max_score) { - $max_score = $answer['score']; - $true_answer_id = $answer['id']; + if ($answer['identifier'] == 'true') { + $trueanswer = $answer; + } else if ($answer['identifier'] == 'false') { + $falseanswer = $answer; + } else { + // Should not happen, but just in case. + throw new coding_exception("Unknown answer identifier detected " . + "in true/false quiz question with id {$question['id']}."); } $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer); } - foreach ($question['answers'] as $answer) { - - if ($answer['id'] != $true_answer_id) { - $max_score = $answer['score']; - $false_answer_id = $answer['id']; - } + // Make sure the true and false answer was found. + if (is_null($trueanswer) || is_null($falseanswer)) { + throw new coding_exception("Unable to correctly identify the " . + "true and false answers in the question with id {$question['id']}."); } } @@ -937,8 +941,8 @@ class cc11_quiz extends entities11 { '[#false_answer_id#]'); $replace_values = array($node_course_question_categories_question_answer, - $true_answer_id, - $false_answer_id); + $trueanswer['id'], + $falseanswer['id']); $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);