diff --git a/question/type/multichoice/question.php b/question/type/multichoice/question.php index 553fcd35096..58a21364bed 100644 --- a/question/type/multichoice/question.php +++ b/question/type/multichoice/question.php @@ -197,7 +197,7 @@ class qtype_multichoice_single_question extends qtype_multichoice_base { public function prepare_simulated_post_data($simulatedresponse) { $ansid = 0; foreach ($this->answers as $answer) { - if ($answer->answer == $simulatedresponse['answer']) { + if (clean_param($answer->answer, PARAM_NOTAGS) == $simulatedresponse['answer']) { $ansid = $answer->id; } } @@ -360,12 +360,11 @@ class qtype_multichoice_multi_question extends qtype_multichoice_base { public function prepare_simulated_post_data($simulatedresponse) { $postdata = array(); foreach ($simulatedresponse as $ans => $checked) { - if ($checked) { - foreach ($this->answers as $ansid => $answer) { - if ($answer->answer == $ans) { - $fieldno = array_search($ansid, $this->order); - $postdata[$this->field($fieldno)] = '1'; - } + foreach ($this->answers as $ansid => $answer) { + if (clean_param($answer->answer, PARAM_NOTAGS) == $ans) { + $fieldno = array_search($ansid, $this->order); + $postdata[$this->field($fieldno)] = $checked; + break; } } } @@ -376,7 +375,8 @@ class qtype_multichoice_multi_question extends qtype_multichoice_base { $simulatedresponse = array(); foreach ($this->order as $fieldno => $ansid) { if (isset($postdata[$this->field($fieldno)])) { - $simulatedresponse[$this->answers[$ansid]->answer] = 1; + $checked = $postdata[$this->field($fieldno)]; + $simulatedresponse[clean_param($this->answers[$ansid]->answer, PARAM_NOTAGS)] = $checked; } } ksort($simulatedresponse); diff --git a/question/type/multichoice/tests/question_multi_test.php b/question/type/multichoice/tests/question_multi_test.php index 11fe16fdc57..fa3775d123a 100644 --- a/question/type/multichoice/tests/question_multi_test.php +++ b/question/type/multichoice/tests/question_multi_test.php @@ -91,27 +91,23 @@ class qtype_multichoice_multi_question_test extends advanced_testcase { public function test_grading() { $question = test_question_maker::make_a_multichoice_multi_question(); - $question->shuffleanswers = false; $question->start_attempt(new question_attempt_step(), 1); $this->assertEquals(array(1, question_state::$gradedright), - $question->grade_response(array('choice0' => '1', 'choice2' => '1'))); + $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1)))); $this->assertEquals(array(0.5, question_state::$gradedpartial), - $question->grade_response(array('choice0' => '1'))); + $question->grade_response($question->prepare_simulated_post_data(array('A' => 1)))); $this->assertEquals(array(0, question_state::$gradedwrong), - $question->grade_response( - array('choice0' => '1', 'choice1' => '1', 'choice2' => '1'))); + $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'B' => 1, 'C' => 1)))); $this->assertEquals(array(0, question_state::$gradedwrong), - $question->grade_response(array('choice1' => '1'))); + $question->grade_response($question->prepare_simulated_post_data(array('B' => 1)))); } public function test_get_correct_response() { $question = test_question_maker::make_a_multichoice_multi_question(); - $question->shuffleanswers = false; $question->start_attempt(new question_attempt_step(), 1); - $this->assertEquals(array('choice0' => '1', 'choice2' => '1'), - $question->get_correct_response()); + $this->assertEquals($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1)), $question->get_correct_response()); } public function test_get_question_summary() { @@ -131,7 +127,7 @@ class qtype_multichoice_multi_question_test extends advanced_testcase { $mc->shuffleanswers = false; $mc->start_attempt(new question_attempt_step(), 1); - $summary = $mc->summarise_response(array('choice1' => 1, 'choice2' => 1), + $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('B' => 1, 'C' => 1)), test_question_maker::get_a_qa($mc)); $this->assertEquals('B; C', $summary); @@ -139,14 +135,33 @@ class qtype_multichoice_multi_question_test extends advanced_testcase { public function test_classify_response() { $mc = test_question_maker::make_a_multichoice_multi_question(); - $mc->shuffleanswers = false; $mc->start_attempt(new question_attempt_step(), 1); $this->assertEquals(array( 13 => new question_classified_response(13, 'A', 0.5), 14 => new question_classified_response(14, 'B', -1.0), - ), $mc->classify_response(array('choice0' => 1, 'choice1' => 1))); + ), $mc->classify_response($mc->prepare_simulated_post_data(array('A' => 1, 'B' => 1)))); $this->assertEquals(array(), $mc->classify_response(array())); } + + public function test_prepare_simulated_post_data() { + $mc = test_question_maker::make_a_multichoice_multi_question(); + $mc->start_attempt(new question_attempt_step(), 1); + $correctanswers = array( + array(), + array('A' => 1), + array('B' => 1, 'D' => 0), + array('A' => 0, 'B' => 0, 'C' => 0, 'D' => 0), + array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 0), + array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 1), + array('A' => 1, 'B' => 1, 'C' => 1, 'D' => 1) + ); + foreach ($correctanswers as $correctanswer) { + $postdata = $mc->prepare_simulated_post_data($correctanswer); + $simulatedreponse = $mc->get_student_response_values_for_simulation($postdata); + $this->assertEquals($correctanswer, $simulatedreponse, '', 0, 10, true); + } + } + } diff --git a/question/type/multichoice/tests/question_single_test.php b/question/type/multichoice/tests/question_single_test.php index 335b2460c45..ca09489f6f2 100644 --- a/question/type/multichoice/tests/question_single_test.php +++ b/question/type/multichoice/tests/question_single_test.php @@ -84,15 +84,14 @@ class qtype_multichoice_single_question_test extends advanced_testcase { public function test_grading() { $question = test_question_maker::make_a_multichoice_single_question(); - $question->shuffleanswers = false; $question->start_attempt(new question_attempt_step(), 1); $this->assertEquals(array(1, question_state::$gradedright), - $question->grade_response(array('answer' => 0))); + $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'A')))); $this->assertEquals(array(-0.3333333, question_state::$gradedwrong), - $question->grade_response(array('answer' => 1))); + $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'B')))); $this->assertEquals(array(-0.3333333, question_state::$gradedwrong), - $question->grade_response(array('answer' => 2))); + $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'C')))); } public function test_grading_rounding_three_right() { @@ -104,7 +103,6 @@ class qtype_multichoice_single_question_test extends advanced_testcase { $mc->generalfeedback = '1, 3 and 5 are the odd numbers.'; $mc->qtype = question_bank::get_qtype('multichoice'); - $mc->shuffleanswers = 0; $mc->answernumbering = 'abc'; test_question_maker::set_standard_combined_feedback_fields($mc); @@ -120,40 +118,34 @@ class qtype_multichoice_single_question_test extends advanced_testcase { $mc->start_attempt(new question_attempt_step(), 1); - list($grade, $state) = $mc->grade_response( - array('choice0' => 1, 'choice2' => 1, 'choice4' => 1)); + list($grade, $state) = $mc->grade_response($mc->prepare_simulated_post_data(array('1' => '1', '3' => '1', '5' => '1'))); $this->assertEquals(1, $grade, '', 0.000001); $this->assertEquals(question_state::$gradedright, $state); } public function test_get_correct_response() { $question = test_question_maker::make_a_multichoice_single_question(); - $question->shuffleanswers = false; $question->start_attempt(new question_attempt_step(), 1); - $this->assertEquals(array('answer' => 0), - $question->get_correct_response()); + $this->assertEquals($question->prepare_simulated_post_data(array('answer' => 'A')), $question->get_correct_response()); } public function test_summarise_response() { $mc = test_question_maker::make_a_multichoice_single_question(); - $mc->shuffleanswers = false; $mc->start_attempt(new question_attempt_step(), 1); - $summary = $mc->summarise_response(array('answer' => 0), - test_question_maker::get_a_qa($mc)); + $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('answer' => 'A')), + test_question_maker::get_a_qa($mc)); $this->assertEquals('A', $summary); } public function test_classify_response() { $mc = test_question_maker::make_a_multichoice_single_question(); - $mc->shuffleanswers = false; $mc->start_attempt(new question_attempt_step(), 1); - $this->assertEquals(array( - $mc->id => new question_classified_response(14, 'B', -0.3333333), - ), $mc->classify_response(array('answer' => 1))); + $this->assertEquals(array($mc->id => new question_classified_response(14, 'B', -0.3333333)), + $mc->classify_response($mc->prepare_simulated_post_data(array('answer' => 'B')))); $this->assertEquals(array( $mc->id => question_classified_response::no_response(),