From a980739fd47126ebf0502f3aef935c216fcd938d Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 17 Aug 2011 12:33:00 +0100 Subject: [PATCH] MDL-28942 qtype multichoice, multi-response. Avoid computing the wrong random guess score. It is effectively impossible to compute the right random guess score, so we should not compute anything, rather than computing a number that we know is wrong. This patch also fixes a multianswer unit test error that was shown up by this fix. --- question/type/multianswer/simpletest/testquestiontype.php | 4 ++-- question/type/multichoice/questiontype.php | 6 ++++++ question/type/multichoice/simpletest/testquestiontype.php | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/question/type/multianswer/simpletest/testquestiontype.php b/question/type/multianswer/simpletest/testquestiontype.php index add7a165507..b8f85819772 100644 --- a/question/type/multianswer/simpletest/testquestiontype.php +++ b/question/type/multianswer/simpletest/testquestiontype.php @@ -77,7 +77,7 @@ class qtype_multianswer_test extends UnitTestCase { $sadata->id = 1; $sadata->qtype = 'shortanswer'; $sadata->defaultmark = 1; - $sadata->options->single = true; + $sadata->options->usecase = true; $sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0); $sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0); $sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1); @@ -86,7 +86,7 @@ class qtype_multianswer_test extends UnitTestCase { $mcdata->id = 1; $mcdata->qtype = 'multichoice'; $mcdata->defaultmark = 1; - $mcdata->options->usecase = true; + $mcdata->options->single = true; $mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0); $mcdata->options->answers[2] = (object) array('answer' => 'Owl', 'fraction' => 1); $mcdata->options->answers[3] = (object) array('answer' => '*', 'fraction' => 0); diff --git a/question/type/multichoice/questiontype.php b/question/type/multichoice/questiontype.php index 240bc4c0384..e155366f157 100644 --- a/question/type/multichoice/questiontype.php +++ b/question/type/multichoice/questiontype.php @@ -182,6 +182,12 @@ class qtype_multichoice extends question_type { } public function get_random_guess_score($questiondata) { + if (!$questiondata->options->single) { + // Pretty much impossible to compute for _multi questions. Don't try. + return null; + } + + // Single choice questions - average choice fraction. $totalfraction = 0; foreach ($questiondata->options->answers as $answer) { $totalfraction += $answer->fraction; diff --git a/question/type/multichoice/simpletest/testquestiontype.php b/question/type/multichoice/simpletest/testquestiontype.php index 1afa1249fa0..8123972fbb6 100644 --- a/question/type/multichoice/simpletest/testquestiontype.php +++ b/question/type/multichoice/simpletest/testquestiontype.php @@ -71,6 +71,12 @@ class qtype_multichoice_test extends UnitTestCase { $this->assertEqual(0.5, $this->qtype->get_random_guess_score($q)); } + public function test_get_random_guess_score_multi() { + $q = $this->get_test_question_data(); + $q->options->single = false; + $this->assertNull($this->qtype->get_random_guess_score($q)); + } + public function test_get_possible_responses_single() { $q = $this->get_test_question_data(); $responses = $this->qtype->get_possible_responses($q);