Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-39945 qtype multichoice was not comparing blank responses correctly.

  • Loading branch information...
commit c0f5255cb985f644a9f568883f48e93239d0f101 1 parent ae61eea
Tim Hunt timhunt authored
6 question/engine/lib.php
@@ -757,16 +757,16 @@ public static function arrays_same_at_key_missing_is_blank(
757 757 public static function arrays_same_at_key_integer(
758 758 array $array1, array $array2, $key) {
759 759 if (array_key_exists($key, $array1)) {
760   - $value1 = $array1[$key];
  760 + $value1 = (int) $array1[$key];
761 761 } else {
762 762 $value1 = 0;
763 763 }
764 764 if (array_key_exists($key, $array2)) {
765   - $value2 = $array2[$key];
  765 + $value2 = (int) $array2[$key];
766 766 } else {
767 767 $value2 = 0;
768 768 }
769   - return ((integer) $value1) === ((integer) $value2);
  769 + return $value1 === $value2;
770 770 }
771 771
772 772 private static $units = array('', 'i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix');
2  question/type/multichoice/question.php
@@ -338,7 +338,7 @@ public function get_correct_response() {
338 338 public function is_same_response(array $prevresponse, array $newresponse) {
339 339 foreach ($this->order as $key => $notused) {
340 340 $fieldname = $this->field($key);
341   - if (!question_utils::arrays_same_at_key($prevresponse, $newresponse, $fieldname)) {
  341 + if (!question_utils::arrays_same_at_key_integer($prevresponse, $newresponse, $fieldname)) {
342 342 return false;
343 343 }
344 344 }
152 question/type/multichoice/tests/question_multi_test.php
... ... @@ -0,0 +1,152 @@
  1 +<?php
  2 +// This file is part of Moodle - http://moodle.org/
  3 +//
  4 +// Moodle is free software: you can redistribute it and/or modify
  5 +// it under the terms of the GNU General Public License as published by
  6 +// the Free Software Foundation, either version 3 of the License, or
  7 +// (at your option) any later version.
  8 +//
  9 +// Moodle is distributed in the hope that it will be useful,
  10 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +// GNU General Public License for more details.
  13 +//
  14 +// You should have received a copy of the GNU General Public License
  15 +// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16 +
  17 +/**
  18 + * Unit tests for the multiple choice, multi-response question definition classes.
  19 + *
  20 + * @package qtype_multichoice
  21 + * @copyright 2009 The Open University
  22 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23 + */
  24 +
  25 +defined('MOODLE_INTERNAL') || die();
  26 +
  27 +global $CFG;
  28 +require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  29 +
  30 +
  31 +/**
  32 + * Unit tests for the multiple choice, multi-response question definition class.
  33 + *
  34 + * @copyright 2009 The Open University
  35 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36 + */
  37 +class qtype_multichoice_multi_question_test extends advanced_testcase {
  38 +
  39 + public function test_get_expected_data() {
  40 + $question = test_question_maker::make_a_multichoice_multi_question();
  41 + $question->start_attempt(new question_attempt_step(), 1);
  42 +
  43 + $this->assertEquals(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL,
  44 + 'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data());
  45 + }
  46 +
  47 + public function test_is_complete_response() {
  48 + $question = test_question_maker::make_a_multichoice_multi_question();
  49 + $question->start_attempt(new question_attempt_step(), 1);
  50 +
  51 + $this->assertFalse($question->is_complete_response(array()));
  52 + $this->assertFalse($question->is_complete_response(
  53 + array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  54 + $this->assertTrue($question->is_complete_response(array('choice1' => '1')));
  55 + $this->assertTrue($question->is_complete_response(
  56 + array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  57 + }
  58 +
  59 + public function test_is_gradable_response() {
  60 + $question = test_question_maker::make_a_multichoice_multi_question();
  61 + $question->start_attempt(new question_attempt_step(), 1);
  62 +
  63 + $this->assertFalse($question->is_gradable_response(array()));
  64 + $this->assertFalse($question->is_gradable_response(
  65 + array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  66 + $this->assertTrue($question->is_gradable_response(array('choice1' => '1')));
  67 + $this->assertTrue($question->is_gradable_response(
  68 + array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  69 + }
  70 +
  71 + public function test_is_same_response() {
  72 + $question = test_question_maker::make_a_multichoice_multi_question();
  73 + $question->start_attempt(new question_attempt_step(), 1);
  74 +
  75 + $this->assertTrue($question->is_same_response(
  76 + array(),
  77 + array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  78 +
  79 + $this->assertTrue($question->is_same_response(
  80 + array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  81 + array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  82 +
  83 + $this->assertFalse($question->is_same_response(
  84 + array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  85 + array('choice0' => '1', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  86 +
  87 + $this->assertTrue($question->is_same_response(
  88 + array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0'),
  89 + array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0')));
  90 + }
  91 +
  92 + public function test_grading() {
  93 + $question = test_question_maker::make_a_multichoice_multi_question();
  94 + $question->shuffleanswers = false;
  95 + $question->start_attempt(new question_attempt_step(), 1);
  96 +
  97 + $this->assertEquals(array(1, question_state::$gradedright),
  98 + $question->grade_response(array('choice0' => '1', 'choice2' => '1')));
  99 + $this->assertEquals(array(0.5, question_state::$gradedpartial),
  100 + $question->grade_response(array('choice0' => '1')));
  101 + $this->assertEquals(array(0, question_state::$gradedwrong),
  102 + $question->grade_response(
  103 + array('choice0' => '1', 'choice1' => '1', 'choice2' => '1')));
  104 + $this->assertEquals(array(0, question_state::$gradedwrong),
  105 + $question->grade_response(array('choice1' => '1')));
  106 + }
  107 +
  108 + public function test_get_correct_response() {
  109 + $question = test_question_maker::make_a_multichoice_multi_question();
  110 + $question->shuffleanswers = false;
  111 + $question->start_attempt(new question_attempt_step(), 1);
  112 +
  113 + $this->assertEquals(array('choice0' => '1', 'choice2' => '1'),
  114 + $question->get_correct_response());
  115 + }
  116 +
  117 + public function test_get_question_summary() {
  118 + $mc = test_question_maker::make_a_multichoice_single_question();
  119 + $mc->start_attempt(new question_attempt_step(), 1);
  120 +
  121 + $qsummary = $mc->get_question_summary();
  122 +
  123 + $this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/', $qsummary);
  124 + foreach ($mc->answers as $answer) {
  125 + $this->assertRegExp('/' . preg_quote($answer->answer, '/') . '/', $qsummary);
  126 + }
  127 + }
  128 +
  129 + public function test_summarise_response() {
  130 + $mc = test_question_maker::make_a_multichoice_multi_question();
  131 + $mc->shuffleanswers = false;
  132 + $mc->start_attempt(new question_attempt_step(), 1);
  133 +
  134 + $summary = $mc->summarise_response(array('choice1' => 1, 'choice2' => 1),
  135 + test_question_maker::get_a_qa($mc));
  136 +
  137 + $this->assertEquals('B; C', $summary);
  138 + }
  139 +
  140 + public function test_classify_response() {
  141 + $mc = test_question_maker::make_a_multichoice_multi_question();
  142 + $mc->shuffleanswers = false;
  143 + $mc->start_attempt(new question_attempt_step(), 1);
  144 +
  145 + $this->assertEquals(array(
  146 + 13 => new question_classified_response(13, 'A', 0.5),
  147 + 14 => new question_classified_response(14, 'B', -1.0),
  148 + ), $mc->classify_response(array('choice0' => 1, 'choice1' => 1)));
  149 +
  150 + $this->assertEquals(array(), $mc->classify_response(array()));
  151 + }
  152 +}
144 question/type/multichoice/tests/question_test.php → ...n/type/multichoice/tests/question_single_test.php
@@ -15,15 +15,13 @@
15 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16 16
17 17 /**
18   - * Unit tests for the multiple choice question definition classes.
  18 + * Unit tests for the multiple choice, single response question definition classes.
19 19 *
20   - * @package qtype
21   - * @subpackage multichoice
22   - * @copyright 2009 The Open University
23   - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20 + * @package qtype_multichoice
  21 + * @copyright 2009 The Open University
  22 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 23 */
25 24
26   -
27 25 defined('MOODLE_INTERNAL') || die();
28 26
29 27 global $CFG;
@@ -31,10 +29,10 @@
31 29
32 30
33 31 /**
34   - * Unit tests for the multiple choice, multiple response question definition class.
  32 + * Unit tests for the multiple choice, single response question definition class.
35 33 *
36   - * @copyright 2009 The Open University
37   - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34 + * @copyright 2009 The Open University
  35 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 36 */
39 37 class qtype_multichoice_single_question_test extends advanced_testcase {
40 38
@@ -59,6 +57,31 @@ public function test_is_gradable_response() {
59 57 $this->assertTrue($question->is_gradable_response(array('answer' => '2')));
60 58 }
61 59
  60 + public function test_is_same_response() {
  61 + $question = test_question_maker::make_a_multichoice_single_question();
  62 + $question->start_attempt(new question_attempt_step(), 1);
  63 +
  64 + $this->assertTrue($question->is_same_response(
  65 + array(),
  66 + array()));
  67 +
  68 + $this->assertFalse($question->is_same_response(
  69 + array(),
  70 + array('answer' => '0')));
  71 +
  72 + $this->assertTrue($question->is_same_response(
  73 + array('answer' => '0'),
  74 + array('answer' => '0')));
  75 +
  76 + $this->assertFalse($question->is_same_response(
  77 + array('answer' => '0'),
  78 + array('answer' => '1')));
  79 +
  80 + $this->assertTrue($question->is_same_response(
  81 + array('answer' => '2'),
  82 + array('answer' => '2')));
  83 + }
  84 +
62 85 public function test_grading() {
63 86 $question = test_question_maker::make_a_multichoice_single_question();
64 87 $question->shuffleanswers = false;
@@ -151,106 +174,3 @@ public function test_make_html_inline() {
151 174 $this->assertEquals('Frog<br />†', $mc->make_html_inline('<p>Frog</p><p>†</p>'));
152 175 }
153 176 }
154   -
155   -
156   -/**
157   - * Unit tests for the multiple choice, single response question definition class.
158   - *
159   - * @copyright 2009 The Open University
160   - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
161   - */
162   -class qtype_multichoice_multi_question_test extends advanced_testcase {
163   -
164   - public function test_get_expected_data() {
165   - $question = test_question_maker::make_a_multichoice_multi_question();
166   - $question->start_attempt(new question_attempt_step(), 1);
167   -
168   - $this->assertEquals(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL,
169   - 'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data());
170   - }
171   -
172   - public function test_is_complete_response() {
173   - $question = test_question_maker::make_a_multichoice_multi_question();
174   - $question->start_attempt(new question_attempt_step(), 1);
175   -
176   - $this->assertFalse($question->is_complete_response(array()));
177   - $this->assertFalse($question->is_complete_response(
178   - array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
179   - $this->assertTrue($question->is_complete_response(array('choice1' => '1')));
180   - $this->assertTrue($question->is_complete_response(
181   - array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
182   - }
183   -
184   - public function test_is_gradable_response() {
185   - $question = test_question_maker::make_a_multichoice_multi_question();
186   - $question->start_attempt(new question_attempt_step(), 1);
187   -
188   - $this->assertFalse($question->is_gradable_response(array()));
189   - $this->assertFalse($question->is_gradable_response(
190   - array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
191   - $this->assertTrue($question->is_gradable_response(array('choice1' => '1')));
192   - $this->assertTrue($question->is_gradable_response(
193   - array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
194   - }
195   -
196   - public function test_grading() {
197   - $question = test_question_maker::make_a_multichoice_multi_question();
198   - $question->shuffleanswers = false;
199   - $question->start_attempt(new question_attempt_step(), 1);
200   -
201   - $this->assertEquals(array(1, question_state::$gradedright),
202   - $question->grade_response(array('choice0' => '1', 'choice2' => '1')));
203   - $this->assertEquals(array(0.5, question_state::$gradedpartial),
204   - $question->grade_response(array('choice0' => '1')));
205   - $this->assertEquals(array(0, question_state::$gradedwrong),
206   - $question->grade_response(
207   - array('choice0' => '1', 'choice1' => '1', 'choice2' => '1')));
208   - $this->assertEquals(array(0, question_state::$gradedwrong),
209   - $question->grade_response(array('choice1' => '1')));
210   - }
211   -
212   - public function test_get_correct_response() {
213   - $question = test_question_maker::make_a_multichoice_multi_question();
214   - $question->shuffleanswers = false;
215   - $question->start_attempt(new question_attempt_step(), 1);
216   -
217   - $this->assertEquals(array('choice0' => '1', 'choice2' => '1'),
218   - $question->get_correct_response());
219   - }
220   -
221   - public function test_get_question_summary() {
222   - $mc = test_question_maker::make_a_multichoice_single_question();
223   - $mc->start_attempt(new question_attempt_step(), 1);
224   -
225   - $qsummary = $mc->get_question_summary();
226   -
227   - $this->assertRegExp('/' . preg_quote($mc->questiontext) . '/', $qsummary);
228   - foreach ($mc->answers as $answer) {
229   - $this->assertRegExp('/' . preg_quote($answer->answer) . '/', $qsummary);
230   - }
231   - }
232   -
233   - public function test_summarise_response() {
234   - $mc = test_question_maker::make_a_multichoice_multi_question();
235   - $mc->shuffleanswers = false;
236   - $mc->start_attempt(new question_attempt_step(), 1);
237   -
238   - $summary = $mc->summarise_response(array('choice1' => 1, 'choice2' => 1),
239   - test_question_maker::get_a_qa($mc));
240   -
241   - $this->assertEquals('B; C', $summary);
242   - }
243   -
244   - public function test_classify_response() {
245   - $mc = test_question_maker::make_a_multichoice_multi_question();
246   - $mc->shuffleanswers = false;
247   - $mc->start_attempt(new question_attempt_step(), 1);
248   -
249   - $this->assertEquals(array(
250   - 13 => new question_classified_response(13, 'A', 0.5),
251   - 14 => new question_classified_response(14, 'B', -1.0),
252   - ), $mc->classify_response(array('choice0' => 1, 'choice1' => 1)));
253   -
254   - $this->assertEquals(array(), $mc->classify_response(array()));
255   - }
256   -}

0 comments on commit c0f5255

Please sign in to comment.
Something went wrong with that request. Please try again.