1 |
efrain |
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 |
namespace qtype_multianswer;
|
|
|
18 |
|
|
|
19 |
use qtype_multianswer;
|
|
|
20 |
use qtype_multianswer_edit_form;
|
|
|
21 |
use qtype_multichoice_base;
|
|
|
22 |
use question_bank;
|
1441 |
ariadna |
23 |
use stdClass;
|
1 |
efrain |
24 |
use test_question_maker;
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
global $CFG;
|
|
|
29 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
30 |
require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php');
|
|
|
31 |
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
|
|
|
32 |
require_once($CFG->dirroot . '/question/type/multianswer/edit_multianswer_form.php');
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Unit tests for the multianswer question definition class.
|
|
|
37 |
*
|
|
|
38 |
* @package qtype_multianswer
|
|
|
39 |
* @copyright 2011 The Open University
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
* @covers \qtype_multianswer
|
|
|
42 |
*/
|
1441 |
ariadna |
43 |
final class question_type_test extends \advanced_testcase {
|
1 |
efrain |
44 |
/** @var qtype_multianswer instance of the question type class to test. */
|
|
|
45 |
protected $qtype;
|
|
|
46 |
|
|
|
47 |
protected function setUp(): void {
|
1441 |
ariadna |
48 |
parent::setUp();
|
1 |
efrain |
49 |
$this->qtype = new qtype_multianswer();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
protected function tearDown(): void {
|
|
|
53 |
$this->qtype = null;
|
1441 |
ariadna |
54 |
parent::tearDown();
|
1 |
efrain |
55 |
}
|
|
|
56 |
|
|
|
57 |
protected function get_test_question_data() {
|
|
|
58 |
global $USER;
|
|
|
59 |
$q = new \stdClass();
|
|
|
60 |
$q->id = 0;
|
|
|
61 |
$q->name = 'Simple multianswer';
|
|
|
62 |
$q->category = 0;
|
|
|
63 |
$q->contextid = 0;
|
|
|
64 |
$q->parent = 0;
|
|
|
65 |
$q->questiontext =
|
|
|
66 |
'Complete this opening line of verse: "The {#1} and the {#2} went to sea".';
|
|
|
67 |
$q->questiontextformat = FORMAT_HTML;
|
|
|
68 |
$q->generalfeedback = 'Generalfeedback: It\'s from "The Owl and the Pussy-cat" by Lear: ' .
|
|
|
69 |
'"The owl and the pussycat went to see';
|
|
|
70 |
$q->generalfeedbackformat = FORMAT_HTML;
|
|
|
71 |
$q->defaultmark = 2;
|
|
|
72 |
$q->penalty = 0.3333333;
|
|
|
73 |
$q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
74 |
$q->versionid = 0;
|
|
|
75 |
$q->version = 1;
|
|
|
76 |
$q->questionbankentryid = 0;
|
|
|
77 |
$q->length = 1;
|
|
|
78 |
$q->stamp = make_unique_id_code();
|
|
|
79 |
$q->timecreated = time();
|
|
|
80 |
$q->timemodified = time();
|
|
|
81 |
$q->createdby = $USER->id;
|
|
|
82 |
$q->modifiedby = $USER->id;
|
1441 |
ariadna |
83 |
$q->options = new stdClass();
|
1 |
efrain |
84 |
|
|
|
85 |
$sadata = new \stdClass();
|
|
|
86 |
$sadata->id = 1;
|
|
|
87 |
$sadata->qtype = 'shortanswer';
|
|
|
88 |
$sadata->defaultmark = 1;
|
1441 |
ariadna |
89 |
$sadata->options = new stdClass();
|
1 |
efrain |
90 |
$sadata->options->usecase = true;
|
|
|
91 |
$sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0);
|
|
|
92 |
$sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0);
|
|
|
93 |
$sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1);
|
|
|
94 |
|
|
|
95 |
$mcdata = new \stdClass();
|
|
|
96 |
$mcdata->id = 1;
|
|
|
97 |
$mcdata->qtype = 'multichoice';
|
|
|
98 |
$mcdata->defaultmark = 1;
|
1441 |
ariadna |
99 |
$mcdata->options = new stdClass();
|
1 |
efrain |
100 |
$mcdata->options->single = true;
|
|
|
101 |
$mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0);
|
|
|
102 |
$mcdata->options->answers[2] = (object) array('answer' => 'Owl', 'fraction' => 1);
|
|
|
103 |
$mcdata->options->answers[3] = (object) array('answer' => '*', 'fraction' => 0);
|
|
|
104 |
|
|
|
105 |
$q->options->questions = array(
|
|
|
106 |
1 => $sadata,
|
|
|
107 |
2 => $mcdata,
|
|
|
108 |
);
|
|
|
109 |
|
|
|
110 |
return $q;
|
|
|
111 |
}
|
|
|
112 |
|
11 |
efrain |
113 |
public function test_name(): void {
|
1 |
efrain |
114 |
$this->assertEquals($this->qtype->name(), 'multianswer');
|
|
|
115 |
}
|
|
|
116 |
|
11 |
efrain |
117 |
public function test_can_analyse_responses(): void {
|
1 |
efrain |
118 |
$this->assertFalse($this->qtype->can_analyse_responses());
|
|
|
119 |
}
|
|
|
120 |
|
11 |
efrain |
121 |
public function test_get_random_guess_score(): void {
|
1 |
efrain |
122 |
$q = test_question_maker::get_question_data('multianswer', 'twosubq');
|
|
|
123 |
$this->assertEqualsWithDelta(0.1666667, $this->qtype->get_random_guess_score($q), 0.0000001);
|
|
|
124 |
}
|
|
|
125 |
|
11 |
efrain |
126 |
public function test_get_random_guess_score_with_missing_subquestion(): void {
|
1 |
efrain |
127 |
global $DB;
|
|
|
128 |
$this->resetAfterTest();
|
|
|
129 |
|
|
|
130 |
// Create a question referring to a subquesion that has got lost (which happens some time).
|
|
|
131 |
/** @var \core_question_generator $generator */
|
|
|
132 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
133 |
$category = $generator->create_question_category();
|
|
|
134 |
$question = $generator->create_question('multianswer', 'twosubq', ['category' => $category->id]);
|
|
|
135 |
// Add a non-existent subquestion id to the list.
|
|
|
136 |
$sequence = $DB->get_field('question_multianswer', 'sequence', ['question' => $question->id], MUST_EXIST);
|
|
|
137 |
$DB->set_field('question_multianswer', 'sequence', $sequence . ',-1', ['question' => $question->id]);
|
|
|
138 |
|
|
|
139 |
// Verify that computing the random guess score does not give an error.
|
|
|
140 |
$questiondata = question_bank::load_question_data($question->id);
|
|
|
141 |
$this->assertEqualsWithDelta(0.1666667, $this->qtype->get_random_guess_score($questiondata), 0.0000001);
|
|
|
142 |
}
|
|
|
143 |
|
11 |
efrain |
144 |
public function test_get_random_guess_score_with_all_missing_subquestions(): void {
|
1 |
efrain |
145 |
$this->resetAfterTest();
|
|
|
146 |
|
|
|
147 |
// Create a question where all subquestions are missing.
|
|
|
148 |
$questiondata = test_question_maker::get_question_data('multianswer', 'twosubq');
|
|
|
149 |
foreach ($questiondata->options->questions as $subq) {
|
|
|
150 |
$subq->qtype = 'subquestion_replacement';
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// Verify that computing the random guess score does not give an error.
|
|
|
154 |
$this->assertNull($this->qtype->get_random_guess_score($questiondata));
|
|
|
155 |
}
|
|
|
156 |
|
11 |
efrain |
157 |
public function test_load_question(): void {
|
1 |
efrain |
158 |
$this->resetAfterTest();
|
|
|
159 |
|
|
|
160 |
$syscontext = \context_system::instance();
|
|
|
161 |
/** @var \core_question_generator $generator */
|
|
|
162 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
163 |
$category = $generator->create_question_category(['contextid' => $syscontext->id]);
|
|
|
164 |
|
|
|
165 |
$fromform = \test_question_maker::get_question_form_data('multianswer');
|
|
|
166 |
$fromform->category = $category->id . ',' . $syscontext->id;
|
|
|
167 |
|
|
|
168 |
$question = new \stdClass();
|
|
|
169 |
$question->category = $category->id;
|
|
|
170 |
$question->qtype = 'multianswer';
|
|
|
171 |
$question->createdby = 0;
|
|
|
172 |
|
|
|
173 |
// Note, $question gets modified during save because of the way subquestions
|
|
|
174 |
// are extracted.
|
|
|
175 |
$question = $this->qtype->save_question($question, $fromform);
|
|
|
176 |
|
|
|
177 |
$questiondata = question_bank::load_question_data($question->id);
|
|
|
178 |
|
|
|
179 |
$this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat',
|
|
|
180 |
'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype',
|
|
|
181 |
'length', 'stamp', 'timecreated', 'timemodified', 'createdby', 'modifiedby', 'idnumber', 'contextid',
|
|
|
182 |
'status', 'versionid', 'version', 'questionbankentryid', 'categoryobject', 'options', 'hints'],
|
|
|
183 |
array_keys(get_object_vars($questiondata)));
|
|
|
184 |
$this->assertEquals($category->id, $questiondata->category);
|
|
|
185 |
$this->assertEquals(0, $questiondata->parent);
|
|
|
186 |
$this->assertEquals($fromform->name, $questiondata->name);
|
|
|
187 |
$this->assertEquals($fromform->questiontext, $questiondata->questiontext);
|
|
|
188 |
$this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat);
|
|
|
189 |
$this->assertEquals($fromform->generalfeedback['text'], $questiondata->generalfeedback);
|
|
|
190 |
$this->assertEquals($fromform->generalfeedback['format'], $questiondata->generalfeedbackformat);
|
|
|
191 |
$this->assertEquals($fromform->defaultmark, $questiondata->defaultmark);
|
|
|
192 |
$this->assertEquals(0, $questiondata->penalty);
|
|
|
193 |
$this->assertEquals('multianswer', $questiondata->qtype);
|
|
|
194 |
$this->assertEquals(1, $questiondata->length);
|
|
|
195 |
$this->assertEquals(\core_question\local\bank\question_version_status::QUESTION_STATUS_READY, $questiondata->status);
|
|
|
196 |
$this->assertEquals($question->createdby, $questiondata->createdby);
|
|
|
197 |
$this->assertEquals($question->createdby, $questiondata->modifiedby);
|
|
|
198 |
$this->assertEquals('', $questiondata->idnumber);
|
1441 |
ariadna |
199 |
$this->assertEquals($category->contextid, $questiondata->contextid);
|
1 |
efrain |
200 |
|
|
|
201 |
// Build the expected hint base.
|
|
|
202 |
$hintbase = [
|
|
|
203 |
'questionid' => $questiondata->id,
|
|
|
204 |
'shownumcorrect' => 0,
|
|
|
205 |
'clearwrong' => 0,
|
|
|
206 |
'options' => null];
|
|
|
207 |
$expectedhints = [];
|
|
|
208 |
foreach ($fromform->hint as $key => $value) {
|
|
|
209 |
$hint = $hintbase + [
|
|
|
210 |
'hint' => $value['text'],
|
|
|
211 |
'hintformat' => $value['format'],
|
|
|
212 |
];
|
|
|
213 |
$expectedhints[] = (object)$hint;
|
|
|
214 |
}
|
|
|
215 |
// Need to get rid of ids.
|
|
|
216 |
$gothints = array_map(function($hint) {
|
|
|
217 |
unset($hint->id);
|
|
|
218 |
return $hint;
|
|
|
219 |
}, $questiondata->hints);
|
|
|
220 |
// Compare hints.
|
|
|
221 |
$this->assertEquals($expectedhints, array_values($gothints));
|
|
|
222 |
|
|
|
223 |
// Options.
|
|
|
224 |
$this->assertEquals(['answers', 'questions'], array_keys(get_object_vars($questiondata->options)));
|
|
|
225 |
$this->assertEquals(count($fromform->options->questions), count($questiondata->options->questions));
|
|
|
226 |
|
|
|
227 |
// Option answers.
|
|
|
228 |
$this->assertEquals([], $questiondata->options->answers);
|
|
|
229 |
|
|
|
230 |
// Build the expected questions. We aren't going deeper to subquestion answers, options... that's another qtype job.
|
|
|
231 |
$expectedquestions = [];
|
|
|
232 |
foreach ($fromform->options->questions as $key => $value) {
|
|
|
233 |
$question = [
|
|
|
234 |
'id' => $value->id,
|
|
|
235 |
'category' => $category->id,
|
|
|
236 |
'parent' => $questiondata->id,
|
|
|
237 |
'name' => $value->name,
|
|
|
238 |
'questiontext' => $value->questiontext,
|
|
|
239 |
'questiontextformat' => $value->questiontextformat,
|
|
|
240 |
'generalfeedback' => $value->generalfeedback,
|
|
|
241 |
'generalfeedbackformat' => $value->generalfeedbackformat,
|
|
|
242 |
'defaultmark' => (float) $value->defaultmark,
|
|
|
243 |
'penalty' => (float)$value->penalty,
|
|
|
244 |
'qtype' => $value->qtype,
|
|
|
245 |
'length' => $value->length,
|
|
|
246 |
'stamp' => $value->stamp,
|
|
|
247 |
'timecreated' => $value->timecreated,
|
|
|
248 |
'timemodified' => $value->timemodified,
|
|
|
249 |
'createdby' => $value->createdby,
|
|
|
250 |
'modifiedby' => $value->modifiedby,
|
|
|
251 |
];
|
|
|
252 |
$expectedquestions[] = (object)$question;
|
|
|
253 |
}
|
|
|
254 |
// Need to get rid of (version, idnumber, options, hints, maxmark). They are missing @ fromform.
|
|
|
255 |
$gotquestions = array_map(function($question) {
|
|
|
256 |
$question->id = (int) $question->id;
|
|
|
257 |
$question->category = (int) $question->category;
|
|
|
258 |
$question->defaultmark = (float) $question->defaultmark;
|
|
|
259 |
$question->penalty = (float) $question->penalty;
|
|
|
260 |
$question->length = (int) $question->length;
|
|
|
261 |
$question->timecreated = (int) $question->timecreated;
|
|
|
262 |
$question->timemodified = (int) $question->timemodified;
|
|
|
263 |
$question->createdby = (int) $question->createdby;
|
|
|
264 |
$question->modifiedby = (int) $question->modifiedby;
|
|
|
265 |
unset($question->idnumber);
|
|
|
266 |
unset($question->options);
|
|
|
267 |
unset($question->hints);
|
|
|
268 |
unset($question->maxmark);
|
|
|
269 |
return $question;
|
|
|
270 |
}, $questiondata->options->questions);
|
|
|
271 |
// Compare questions.
|
|
|
272 |
$this->assertEquals($expectedquestions, array_values($gotquestions));
|
|
|
273 |
}
|
|
|
274 |
|
11 |
efrain |
275 |
public function test_question_saving_twosubq(): void {
|
1 |
efrain |
276 |
$this->resetAfterTest(true);
|
|
|
277 |
$this->setAdminUser();
|
|
|
278 |
|
|
|
279 |
$questiondata = \test_question_maker::get_question_data('multianswer');
|
|
|
280 |
|
|
|
281 |
$formdata = \test_question_maker::get_question_form_data('multianswer');
|
|
|
282 |
|
|
|
283 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
284 |
$cat = $generator->create_question_category(array());
|
|
|
285 |
|
|
|
286 |
$formdata->category = "{$cat->id},{$cat->contextid}";
|
|
|
287 |
qtype_multianswer_edit_form::mock_submit((array)$formdata);
|
|
|
288 |
|
|
|
289 |
$form = \qtype_multianswer_test_helper::get_question_editing_form($cat, $questiondata);
|
|
|
290 |
|
|
|
291 |
$this->assertTrue($form->is_validated());
|
|
|
292 |
|
|
|
293 |
$fromform = $form->get_data();
|
|
|
294 |
// Create a new question version with the form submission.
|
|
|
295 |
unset($questiondata->id);
|
|
|
296 |
$returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
|
|
|
297 |
$actualquestionsdata = question_load_questions(array($returnedfromsave->id));
|
|
|
298 |
$actualquestiondata = end($actualquestionsdata);
|
|
|
299 |
|
|
|
300 |
foreach ($questiondata as $property => $value) {
|
|
|
301 |
if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'hints', 'stamp',
|
|
|
302 |
'idnumber', 'version', 'versionid', 'questionbankentryid', 'contextid', 'category', 'status'])) {
|
|
|
303 |
$this->assertEquals($value, $actualquestiondata->$property);
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
foreach ($questiondata->options as $optionname => $value) {
|
|
|
308 |
if ($optionname != 'questions') {
|
|
|
309 |
$this->assertEquals($value, $actualquestiondata->options->$optionname);
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
foreach ($questiondata->hints as $hint) {
|
|
|
314 |
$actualhint = array_shift($actualquestiondata->hints);
|
|
|
315 |
foreach ($hint as $property => $value) {
|
|
|
316 |
if (!in_array($property, array('id', 'questionid', 'options'))) {
|
|
|
317 |
$this->assertEquals($value, $actualhint->$property);
|
|
|
318 |
}
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
$this->assertObjectHasProperty('questions', $actualquestiondata->options);
|
|
|
323 |
|
|
|
324 |
$subqpropstoignore =
|
|
|
325 |
['id', 'category', 'parent', 'contextid', 'question', 'options', 'stamp', 'timemodified',
|
|
|
326 |
'timecreated', 'status', 'idnumber', 'version', 'versionid', 'questionbankentryid'];
|
|
|
327 |
foreach ($questiondata->options->questions as $subqno => $subq) {
|
|
|
328 |
$actualsubq = $actualquestiondata->options->questions[$subqno];
|
|
|
329 |
foreach ($subq as $subqproperty => $subqvalue) {
|
|
|
330 |
if (!in_array($subqproperty, $subqpropstoignore)) {
|
|
|
331 |
$this->assertEquals($subqvalue, $actualsubq->$subqproperty);
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
foreach ($subq->options as $optionname => $value) {
|
|
|
335 |
if (!in_array($optionname, array('answers'))) {
|
|
|
336 |
$this->assertEquals($value, $actualsubq->options->$optionname);
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
foreach ($subq->options->answers as $answer) {
|
|
|
340 |
$actualanswer = array_shift($actualsubq->options->answers);
|
|
|
341 |
foreach ($answer as $ansproperty => $ansvalue) {
|
|
|
342 |
// These questions do not use 'answerformat', will ignore it.
|
|
|
343 |
if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
|
|
|
344 |
$this->assertEquals($ansvalue, $actualanswer->$ansproperty);
|
|
|
345 |
}
|
|
|
346 |
}
|
|
|
347 |
}
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
/**
|
|
|
352 |
* Verify that the multiplechoice variants parameters are correctly interpreted from
|
|
|
353 |
* the question text
|
|
|
354 |
*/
|
11 |
efrain |
355 |
public function test_questiontext_extraction_of_multiplechoice_subquestions_variants(): void {
|
1 |
efrain |
356 |
$questiontext = array();
|
|
|
357 |
$questiontext['format'] = FORMAT_HTML;
|
|
|
358 |
$questiontext['itemid'] = '';
|
|
|
359 |
$questiontext['text'] = '<p>Match the following cities with the correct state:</p>
|
|
|
360 |
<ul>
|
|
|
361 |
<li>1 San Francisco:{1:MULTICHOICE:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li>
|
|
|
362 |
<li>2 Tucson:{1:MC:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li>
|
|
|
363 |
<li>3 Los Angeles:{1:MULTICHOICE_S:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li>
|
|
|
364 |
<li>4 Phoenix:{1:MCS:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li>
|
|
|
365 |
<li>5 San Francisco:{1:MULTICHOICE_H:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li>
|
|
|
366 |
<li>6 Tucson:{1:MCH:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li>
|
|
|
367 |
<li>7 Los Angeles:{1:MULTICHOICE_HS:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li>
|
|
|
368 |
<li>8 Phoenix:{1:MCHS:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li>
|
|
|
369 |
<li>9 San Francisco:{1:MULTICHOICE_V:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li>
|
|
|
370 |
<li>10 Tucson:{1:MCV:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li>
|
|
|
371 |
<li>11 Los Angeles:{1:MULTICHOICE_VS:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li>
|
|
|
372 |
<li>12 Phoenix:{1:MCVS:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li>
|
|
|
373 |
</ul>';
|
|
|
374 |
|
|
|
375 |
$q = qtype_multianswer_extract_question($questiontext);
|
|
|
376 |
foreach ($q->options->questions as $key => $sub) {
|
|
|
377 |
$this->assertSame($sub->qtype, 'multichoice');
|
|
|
378 |
if ($key == 1 || $key == 2 || $key == 5 || $key == 6 || $key == 9 || $key == 10) {
|
|
|
379 |
$this->assertSame($sub->shuffleanswers, 0);
|
|
|
380 |
} else {
|
|
|
381 |
$this->assertSame($sub->shuffleanswers, 1);
|
|
|
382 |
}
|
|
|
383 |
if ($key == 1 || $key == 2 || $key == 3 || $key == 4) {
|
|
|
384 |
$this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_DROPDOWN);
|
|
|
385 |
} else if ($key == 5 || $key == 6 || $key == 7 || $key == 8) {
|
|
|
386 |
$this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_HORIZONTAL);
|
|
|
387 |
} else if ($key == 9 || $key == 10 || $key == 11 || $key == 12) {
|
|
|
388 |
$this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_VERTICAL);
|
|
|
389 |
}
|
|
|
390 |
foreach ($sub->feedback as $key => $feedback) {
|
|
|
391 |
if ($feedback['text'] === 'OK') {
|
|
|
392 |
$this->assertEquals(1, $sub->fraction[$key]);
|
|
|
393 |
} else if ($feedback['text'] === 'Wrong') {
|
|
|
394 |
$this->assertEquals(0, $sub->fraction[$key]);
|
|
|
395 |
} else {
|
|
|
396 |
$this->assertEquals('Not really', $feedback['text']);
|
|
|
397 |
$this->assertEquals(0.3333333, $sub->fraction[$key]);
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
/**
|
|
|
404 |
* Test get_question_options.
|
|
|
405 |
*
|
|
|
406 |
* @covers \qtype_multianswer::get_question_options
|
|
|
407 |
*/
|
11 |
efrain |
408 |
public function test_get_question_options(): void {
|
1 |
efrain |
409 |
global $DB;
|
|
|
410 |
|
|
|
411 |
$this->resetAfterTest(true);
|
|
|
412 |
|
|
|
413 |
$syscontext = \context_system::instance();
|
|
|
414 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
415 |
$category = $generator->create_question_category(['contextid' => $syscontext->id]);
|
|
|
416 |
|
|
|
417 |
$fromform = test_question_maker::get_question_form_data('multianswer', 'twosubq');
|
|
|
418 |
$fromform->category = $category->id . ',' . $syscontext->id;
|
|
|
419 |
|
|
|
420 |
$question = new \stdClass();
|
|
|
421 |
$question->category = $category->id;
|
|
|
422 |
$question->qtype = 'multianswer';
|
|
|
423 |
$question->createdby = 0;
|
|
|
424 |
|
|
|
425 |
$question = $this->qtype->save_question($question, $fromform);
|
|
|
426 |
$questiondata = question_bank::load_question_data($question->id);
|
|
|
427 |
|
|
|
428 |
$questiontodeletekey = array_keys($questiondata->options->questions)[0];
|
|
|
429 |
$questiontodelete = $questiondata->options->questions[$questiontodeletekey];
|
|
|
430 |
|
|
|
431 |
$this->assertCount(2, $questiondata->options->questions);
|
|
|
432 |
$this->assertEquals('shortanswer', $questiondata->options->questions[$questiontodeletekey]->qtype);
|
|
|
433 |
|
|
|
434 |
// Forcibly delete a subquestion to ensure get_question_options replaces it.
|
|
|
435 |
$DB->delete_records('question', ['id' => $questiontodelete->id]);
|
|
|
436 |
$this->qtype->get_question_options($questiondata);
|
|
|
437 |
|
|
|
438 |
$this->assertCount(2, $questiondata->options->questions);
|
|
|
439 |
$this->assertEquals('subquestion_replacement', $questiondata->options->questions[$questiontodeletekey]->qtype);
|
|
|
440 |
}
|
1441 |
ariadna |
441 |
|
|
|
442 |
/**
|
|
|
443 |
* Saving a new version of the question should retain the original subquestion versions, with their own qtype data.
|
|
|
444 |
*/
|
|
|
445 |
public function test_save_question_options(): void {
|
|
|
446 |
global $DB;
|
|
|
447 |
$this->resetAfterTest(true);
|
|
|
448 |
$this->setAdminUser();
|
|
|
449 |
|
|
|
450 |
/** @var \core_question_generator $generator */
|
|
|
451 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
452 |
$cat = $generator->create_question_category([]);
|
|
|
453 |
$question = $generator->create_question('multianswer', 'twosubq', ['category' => $cat->id]);
|
|
|
454 |
|
|
|
455 |
get_question_options($question);
|
|
|
456 |
$originalsubq1 = reset($question->options->questions);
|
|
|
457 |
$originalsubq2 = next($question->options->questions);
|
|
|
458 |
|
|
|
459 |
// Assert that the original subquestions are the expected types, and they have options records.
|
|
|
460 |
$this->assertEquals('shortanswer', $originalsubq1->qtype);
|
|
|
461 |
$this->assertTrue($DB->record_exists('qtype_shortanswer_options', ['questionid' => $originalsubq1->id]));
|
|
|
462 |
$this->assertEquals('multichoice', $originalsubq2->qtype);
|
|
|
463 |
$this->assertTrue($DB->record_exists('qtype_multichoice_options', ['questionid' => $originalsubq2->id]));
|
|
|
464 |
|
|
|
465 |
// Edit the question, replacing the subquestions with two new questions of different types.
|
|
|
466 |
$editedquestion = test_question_maker::get_question_data('multianswer', 'twosubq');
|
|
|
467 |
$editedquestion->id = $question->id;
|
|
|
468 |
$editedquestion->category = $cat->id;
|
|
|
469 |
$editedquestion->context = \context_helper::instance_by_id($cat->contextid);
|
|
|
470 |
$editedsubq1 = test_question_maker::get_question_form_data('multichoice', 'one_of_four');
|
|
|
471 |
$editedsubq1->id = $originalsubq1->id;
|
|
|
472 |
$editedsubq1->qtype = 'multichoice';
|
|
|
473 |
$editedsubq2 = test_question_maker::get_question_form_data('shortanswer', 'frogtoad');
|
|
|
474 |
$editedsubq2->id = $originalsubq2->id;
|
|
|
475 |
$editedsubq2->qtype = 'shortanswer';
|
|
|
476 |
$editedquestion->options->questions = [$editedsubq1, $editedsubq2];
|
|
|
477 |
$this->qtype->save_question_options($editedquestion);
|
|
|
478 |
|
|
|
479 |
$newquestion = $DB->get_record('question', ['id' => $question->id]);
|
|
|
480 |
get_question_options($newquestion);
|
|
|
481 |
$newsubq1 = reset($newquestion->options->questions);
|
|
|
482 |
$newsubq2 = next($newquestion->options->questions);
|
|
|
483 |
|
|
|
484 |
// The new subquestions are different types, and did not re-use IDs from the original subquestions.
|
|
|
485 |
$this->assertEquals('multichoice', $newsubq1->qtype);
|
|
|
486 |
$this->assertFalse(in_array($newsubq1->id, [$originalsubq1->id, $originalsubq2->id]));
|
|
|
487 |
$this->assertEquals('shortanswer', $newsubq2->qtype);
|
|
|
488 |
$this->assertFalse(in_array($newsubq2->id, [$originalsubq1->id, $originalsubq2->id]));
|
|
|
489 |
|
|
|
490 |
// The original questions and option records still exist.
|
|
|
491 |
$this->assertTrue($DB->record_exists('question', ['id' => $originalsubq1->id]));
|
|
|
492 |
$this->assertTrue($DB->record_exists('qtype_shortanswer_options', ['questionid' => $originalsubq1->id]));
|
|
|
493 |
$this->assertTrue($DB->record_exists('question', ['id' => $originalsubq2->id]));
|
|
|
494 |
$this->assertTrue($DB->record_exists('qtype_multichoice_options', ['questionid' => $originalsubq2->id]));
|
|
|
495 |
}
|
1 |
efrain |
496 |
}
|