See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 multianswer question definition class. 19 * 20 * @package qtype 21 * @subpackage multianswer 22 * @copyright 2011 The Open University 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 global $CFG; 27 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 28 29 30 /** 31 * Unit tests for qtype_multianswer_question. 32 * 33 * @copyright 2011 The Open University 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class qtype_multianswer_question_test extends advanced_testcase { 37 public function test_get_expected_data() { 38 $question = test_question_maker::make_question('multianswer'); 39 $this->assertEquals(array('sub1_answer' => PARAM_RAW_TRIMMED, 40 'sub2_answer' => PARAM_RAW), $question->get_expected_data()); 41 } 42 43 public function test_is_complete_response() { 44 $question = test_question_maker::make_question('multianswer'); 45 46 $this->assertFalse($question->is_complete_response(array())); 47 $this->assertTrue($question->is_complete_response(array('sub1_answer' => 'Owl', 48 'sub2_answer' => '2'))); 49 $this->assertTrue($question->is_complete_response(array('sub1_answer' => '0', 50 'sub2_answer' => 0))); 51 $this->assertFalse($question->is_complete_response(array('sub1_answer' => 'Owl'))); 52 } 53 54 public function test_is_gradable_response() { 55 $question = test_question_maker::make_question('multianswer'); 56 57 $this->assertFalse($question->is_gradable_response(array())); 58 $this->assertTrue($question->is_gradable_response(array('sub1_answer' => 'Owl', 59 'sub2_answer' => '2'))); 60 $this->assertTrue($question->is_gradable_response(array('sub1_answer' => '0', 61 'sub2_answer' => 0))); 62 $this->assertTrue($question->is_gradable_response(array('sub1_answer' => 'Owl'))); 63 } 64 65 public function test_grading() { 66 $question = test_question_maker::make_question('multianswer'); 67 $question->start_attempt(new question_attempt_step(), 1); 68 69 $rightchoice = $question->subquestions[2]->get_correct_response(); 70 71 $this->assertEquals(array(1, question_state::$gradedright), $question->grade_response( 72 array('sub1_answer' => 'Owl', 'sub2_answer' => reset($rightchoice)))); 73 $this->assertEquals(array(0.5, question_state::$gradedpartial), $question->grade_response( 74 array('sub1_answer' => 'Owl'))); 75 $this->assertEquals(array(0.5, question_state::$gradedpartial), $question->grade_response( 76 array('sub1_answer' => 'Goat', 'sub2_answer' => reset($rightchoice)))); 77 $this->assertEquals(array(0, question_state::$gradedwrong), $question->grade_response( 78 array('sub1_answer' => 'Dog'))); 79 } 80 81 public function test_get_correct_response() { 82 $question = test_question_maker::make_question('multianswer'); 83 $question->start_attempt(new question_attempt_step(), 1); 84 85 $rightchoice = $question->subquestions[2]->get_correct_response(); 86 87 $this->assertEquals(array('sub1_answer' => 'Owl', 'sub2_answer' => reset($rightchoice)), 88 $question->get_correct_response()); 89 } 90 91 public function test_get_question_summary() { 92 $question = test_question_maker::make_question('multianswer'); 93 94 // Bit of a hack to make testing easier. 95 $question->subquestions[2]->shuffleanswers = false; 96 97 $question->start_attempt(new question_attempt_step(), 1); 98 99 $qsummary = $question->get_question_summary(); 100 $this->assertEquals('Complete this opening line of verse: "The _____ and the ' . 101 '{Bow-wow; Wiggly worm; Pussy-cat} went to sea".', $qsummary); 102 } 103 104 public function test_summarise_response() { 105 $question = test_question_maker::make_question('multianswer'); 106 $question->start_attempt(new question_attempt_step(), 1); 107 108 $rightchoice = $question->subquestions[2]->get_correct_response(); 109 110 $this->assertEquals(get_string('subqresponse', 'qtype_multianswer', 111 array('i' => 1, 'response' => 'Owl')) . '; ' . 112 get_string('subqresponse', 'qtype_multianswer', 113 array('i' => 2, 'response' => 'Pussy-cat')), $question->summarise_response( 114 array('sub1_answer' => 'Owl', 'sub2_answer' => reset($rightchoice)))); 115 } 116 117 public function test_get_num_parts_right() { 118 $question = test_question_maker::make_question('multianswer'); 119 $question->start_attempt(new question_attempt_step(), 1); 120 121 $rightchoice = $question->subquestions[2]->get_correct_response(); 122 $right = reset($rightchoice); 123 124 $response = array('sub1_answer' => 'Frog', 'sub2_answer' => $right); 125 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 126 $this->assertEquals(1, $numpartsright); 127 $this->assertEquals(2, $numparts); 128 $response = array('sub1_answer' => 'Owl', 'sub2_answer' => $right); 129 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 130 $this->assertEquals(2, $numpartsright); 131 $response = array('sub1_answer' => 'Dog', 'sub2_answer' => 3); 132 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 133 $this->assertEquals(0, $numpartsright); 134 $response = array('sub1_answer' => 'Owl'); 135 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 136 $this->assertEquals(1, $numpartsright); 137 $response = array('sub1_answer' => 'Dog'); 138 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 139 $this->assertEquals(0, $numpartsright); 140 $response = array('sub2_answer' => $right); 141 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 142 $this->assertEquals(1, $numpartsright); 143 } 144 145 public function test_get_num_parts_right_fourmc() { 146 // Create a multianswer question with four mcq. 147 $question = test_question_maker::make_question('multianswer', 'fourmc'); 148 $question->start_attempt(new question_attempt_step(), 1); 149 150 $response = array('sub1_answer' => '1', 'sub2_answer' => '1', 151 'sub3_answer' => '1', 'sub4_answer' => '1'); 152 list($numpartsright, $numparts) = $question->get_num_parts_right($response); 153 $this->assertEquals(2, $numpartsright); 154 } 155 156 public function test_clear_wrong_from_response() { 157 $question = test_question_maker::make_question('multianswer'); 158 $question->start_attempt(new question_attempt_step(), 1); 159 160 $rightchoice = $question->subquestions[2]->get_correct_response(); 161 $right = reset($rightchoice); 162 163 $response = array('sub1_answer' => 'Frog', 'sub2_answer' => $right); 164 $this->assertEquals($question->clear_wrong_from_response($response), 165 array('sub1_answer' => '', 'sub2_answer' => $right)); 166 $response = array('sub1_answer' => 'Owl', 'sub2_answer' => $right); 167 $this->assertEquals($question->clear_wrong_from_response($response), 168 array('sub1_answer' => 'Owl', 'sub2_answer' => $right)); 169 $response = array('sub1_answer' => 'Dog', 'sub2_answer' => 3); 170 $this->assertEquals($question->clear_wrong_from_response($response), 171 array('sub1_answer' => '', 'sub2_answer' => '')); 172 $response = array('sub1_answer' => 'Owl'); 173 $this->assertEquals($question->clear_wrong_from_response($response), 174 array('sub1_answer' => 'Owl')); 175 $response = array('sub2_answer' => $right); 176 $this->assertEquals($question->clear_wrong_from_response($response), 177 array('sub2_answer' => $right)); 178 } 179 180 public function test_compute_final_grade() { 181 $question = test_question_maker::make_question('multianswer'); 182 // Set penalty to 0.2 to ease calculations. 183 $question->penalty = 0.2; 184 // Set subquestion 2 defaultmark to 2, to make it a better test, 185 // even thought (at the moment) that never happens for real. 186 $question->subquestions[2]->defaultmark = 2; 187 188 $question->start_attempt(new question_attempt_step(), 1); 189 190 // Compute right and wrong response for subquestion 2. 191 $rightchoice = $question->subquestions[2]->get_correct_response(); 192 $right = reset($rightchoice); 193 $wrong = ($right + 1) % 3; 194 195 // Get subquestion 1 right at 2nd try and subquestion 2 right at 3rd try. 196 $responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $wrong), 197 1 => array('sub1_answer' => 'Owl', 'sub2_answer' => $wrong), 198 2 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), 199 ); 200 $finalgrade = $question->compute_final_grade($responses, 1); 201 $this->assertEqualsWithDelta(1 / 3 * (1 - 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA); 202 203 // Get subquestion 1 right at 3rd try and subquestion 2 right at 2nd try. 204 $responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $wrong), 205 1 => array('sub1_answer' => 'Cat', 'sub2_answer' => $right), 206 2 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), 207 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), 208 ); 209 $finalgrade = $question->compute_final_grade($responses, 1); 210 $this->assertEqualsWithDelta(1 / 3 * (1 - 2 * 0.2) + 2 / 3 * (1 - 0.2), $finalgrade, question_testcase::GRADE_DELTA); 211 212 // Get subquestion 1 right at 4th try and subquestion 2 right at 1st try. 213 $responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $right), 214 1 => array('sub1_answer' => 'Dog', 'sub2_answer' => $right), 215 2 => array('sub1_answer' => 'Dog', 'sub2_answer' => $right), 216 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), 217 ); 218 $finalgrade = $question->compute_final_grade($responses, 1); 219 $this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3, $finalgrade, question_testcase::GRADE_DELTA); 220 221 // Get subquestion 1 right at 4th try and subquestion 2 right 3rd try. 222 // Subquestion 2 was right at 1st try, but last change is at 3rd try. 223 $responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $right), 224 1 => array('sub1_answer' => 'Cat', 'sub2_answer' => $wrong), 225 2 => array('sub1_answer' => 'Frog', 'sub2_answer' => $right), 226 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), 227 ); 228 $finalgrade = $question->compute_final_grade($responses, 1); 229 $this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA); 230 231 // Incomplete responses. Subquestion 1 is right at 4th try and subquestion 2 at 3rd try. 232 $responses = array(0 => array('sub1_answer' => 'Dog'), 233 1 => array('sub1_answer' => 'Cat'), 234 2 => array('sub1_answer' => 'Frog', 'sub2_answer' => $right), 235 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), 236 ); 237 $finalgrade = $question->compute_final_grade($responses, 1); 238 $this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA); 239 } 240 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body