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_ddwtos;
|
|
|
18 |
|
|
|
19 |
use question_answer;
|
|
|
20 |
use question_bank;
|
|
|
21 |
use question_hint_with_parts;
|
|
|
22 |
use question_possible_response;
|
|
|
23 |
|
|
|
24 |
defined('MOODLE_INTERNAL') || die();
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
28 |
require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Unit tests for the drag-and-drop words into sentences question definition class.
|
|
|
33 |
*
|
|
|
34 |
* @package qtype_ddwtos
|
|
|
35 |
* @copyright 2012 The Open University
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
1441 |
ariadna |
38 |
final class question_type_test extends \question_testcase {
|
1 |
efrain |
39 |
/** @var qtype_ddwtos instance of the question type class to test. */
|
|
|
40 |
protected $qtype;
|
|
|
41 |
|
|
|
42 |
protected function setUp(): void {
|
1441 |
ariadna |
43 |
parent::setUp();
|
1 |
efrain |
44 |
$this->qtype = question_bank::get_qtype('ddwtos');;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
protected function tearDown(): void {
|
|
|
48 |
$this->qtype = null;
|
1441 |
ariadna |
49 |
parent::tearDown();
|
1 |
efrain |
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Get some test question data.
|
|
|
54 |
*
|
|
|
55 |
* @return object the data to construct a question like
|
|
|
56 |
* {@link qtype_ddwtos_test_helper::make_ddwtos_question_fox()}.
|
|
|
57 |
*/
|
|
|
58 |
protected function get_test_question_data() {
|
|
|
59 |
global $USER;
|
|
|
60 |
|
|
|
61 |
$dd = new \stdClass();
|
|
|
62 |
$dd->id = 0;
|
|
|
63 |
$dd->category = 0;
|
|
|
64 |
$dd->contextid = 0;
|
|
|
65 |
$dd->parent = 0;
|
|
|
66 |
$dd->questiontextformat = FORMAT_HTML;
|
|
|
67 |
$dd->generalfeedbackformat = FORMAT_HTML;
|
|
|
68 |
$dd->defaultmark = 1;
|
|
|
69 |
$dd->penalty = 0.3333333;
|
|
|
70 |
$dd->length = 1;
|
|
|
71 |
$dd->stamp = make_unique_id_code();
|
|
|
72 |
$dd->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
73 |
$dd->version = 1;
|
|
|
74 |
$dd->versionid = 0;
|
|
|
75 |
$dd->questionbankentryid = 0;
|
|
|
76 |
$dd->idnumber = null;
|
|
|
77 |
$dd->timecreated = time();
|
|
|
78 |
$dd->timemodified = time();
|
|
|
79 |
$dd->createdby = $USER->id;
|
|
|
80 |
$dd->modifiedby = $USER->id;
|
|
|
81 |
|
|
|
82 |
$dd->name = 'Drag-and-drop words into sentences question';
|
|
|
83 |
$dd->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
|
|
|
84 |
$dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
|
|
|
85 |
$dd->qtype = 'ddwtos';
|
|
|
86 |
|
|
|
87 |
$dd->options = new \stdClass();
|
|
|
88 |
$dd->options->shuffleanswers = true;
|
|
|
89 |
|
|
|
90 |
\test_question_maker::set_standard_combined_feedback_fields($dd->options);
|
|
|
91 |
|
|
|
92 |
$dd->options->answers = array(
|
|
|
93 |
(object) array('answer' => 'quick', 'feedback' =>
|
|
|
94 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
|
|
|
95 |
(object) array('answer' => 'fox', 'feedback' =>
|
|
|
96 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
|
|
|
97 |
(object) array('answer' => 'lazy', 'feedback' =>
|
|
|
98 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
|
|
|
99 |
(object) array('answer' => 'assiduous', 'feedback' =>
|
|
|
100 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
|
|
|
101 |
(object) array('answer' => 'dog', 'feedback' =>
|
|
|
102 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
|
|
|
103 |
(object) array('answer' => 'slow', 'feedback' =>
|
|
|
104 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
|
|
|
105 |
);
|
|
|
106 |
|
|
|
107 |
return $dd;
|
|
|
108 |
}
|
|
|
109 |
|
11 |
efrain |
110 |
public function test_name(): void {
|
1 |
efrain |
111 |
$this->assertEquals($this->qtype->name(), 'ddwtos');
|
|
|
112 |
}
|
|
|
113 |
|
11 |
efrain |
114 |
public function test_can_analyse_responses(): void {
|
1 |
efrain |
115 |
$this->assertTrue($this->qtype->can_analyse_responses());
|
|
|
116 |
}
|
|
|
117 |
|
11 |
efrain |
118 |
public function test_save_question(): void {
|
1 |
efrain |
119 |
$this->resetAfterTest();
|
|
|
120 |
|
|
|
121 |
$syscontext = \context_system::instance();
|
|
|
122 |
/** @var core_question_generator $generator */
|
|
|
123 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
124 |
$category = $generator->create_question_category(['contextid' => $syscontext->id]);
|
|
|
125 |
|
|
|
126 |
$fromform = \test_question_maker::get_question_form_data('ddwtos', 'missingchoiceno');
|
|
|
127 |
$fromform->category = $category->id . ',' . $syscontext->id;
|
|
|
128 |
|
|
|
129 |
$question = new \stdClass();
|
|
|
130 |
$question->category = $category->id;
|
|
|
131 |
$question->qtype = 'ddwtos';
|
|
|
132 |
$question->createdby = 0;
|
|
|
133 |
|
|
|
134 |
$this->qtype->save_question($question, $fromform);
|
|
|
135 |
$q = question_bank::load_question($question->id);
|
|
|
136 |
// We just want to verify that this does not cause errors,
|
|
|
137 |
// but also verify some of the outcome.
|
|
|
138 |
$this->assertEquals('The [[1]] sat on the [[2]].', $q->questiontext);
|
|
|
139 |
$this->assertEquals([1 => 1, 2 => 1], $q->places);
|
|
|
140 |
$this->assertEquals([1 => 1, 2 => 2], $q->rightchoices);
|
|
|
141 |
}
|
|
|
142 |
|
11 |
efrain |
143 |
public function test_initialise_question_instance(): void {
|
1 |
efrain |
144 |
$qdata = $this->get_test_question_data();
|
|
|
145 |
|
|
|
146 |
$expected = \test_question_maker::make_question('ddwtos');
|
|
|
147 |
$expected->stamp = $qdata->stamp;
|
|
|
148 |
$expected->idnumber = null;
|
|
|
149 |
|
|
|
150 |
$q = $this->qtype->make_question($qdata);
|
|
|
151 |
|
|
|
152 |
$this->assertEquals($expected, $q);
|
|
|
153 |
}
|
|
|
154 |
|
11 |
efrain |
155 |
public function test_get_random_guess_score(): void {
|
1 |
efrain |
156 |
$q = $this->get_test_question_data();
|
|
|
157 |
$this->assertEqualsWithDelta(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
|
|
|
158 |
}
|
|
|
159 |
|
11 |
efrain |
160 |
public function test_get_possible_responses(): void {
|
1 |
efrain |
161 |
$q = $this->get_test_question_data();
|
|
|
162 |
|
|
|
163 |
$this->assertEquals(array(
|
|
|
164 |
1 => array(
|
|
|
165 |
1 => new question_possible_response('quick', 1 / 3),
|
|
|
166 |
2 => new question_possible_response('slow', 0),
|
|
|
167 |
null => question_possible_response::no_response()),
|
|
|
168 |
2 => array(
|
|
|
169 |
1 => new question_possible_response('fox', 1 / 3),
|
|
|
170 |
2 => new question_possible_response('dog', 0),
|
|
|
171 |
null => question_possible_response::no_response()),
|
|
|
172 |
3 => array(
|
|
|
173 |
1 => new question_possible_response('lazy', 1 / 3),
|
|
|
174 |
2 => new question_possible_response('assiduous', 0),
|
|
|
175 |
null => question_possible_response::no_response()),
|
|
|
176 |
), $this->qtype->get_possible_responses($q));
|
|
|
177 |
}
|
|
|
178 |
|
11 |
efrain |
179 |
public function test_xml_import(): void {
|
1 |
efrain |
180 |
$xml = ' <question type="ddwtos">
|
|
|
181 |
<name>
|
|
|
182 |
<text>A drag-and-drop question</text>
|
|
|
183 |
</name>
|
|
|
184 |
<questiontext format="moodle_auto_format">
|
|
|
185 |
<text>Put these in order: [[1]], [[2]], [[3]].</text>
|
|
|
186 |
</questiontext>
|
|
|
187 |
<generalfeedback>
|
|
|
188 |
<text>The answer is Alpha, Beta, Gamma.</text>
|
|
|
189 |
</generalfeedback>
|
|
|
190 |
<defaultgrade>3</defaultgrade>
|
|
|
191 |
<penalty>0.3333333</penalty>
|
|
|
192 |
<hidden>0</hidden>
|
|
|
193 |
<shuffleanswers>1</shuffleanswers>
|
|
|
194 |
<correctfeedback>
|
|
|
195 |
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
|
|
|
196 |
</correctfeedback>
|
|
|
197 |
<partiallycorrectfeedback>
|
|
|
198 |
<text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
|
|
|
199 |
</partiallycorrectfeedback>
|
|
|
200 |
<incorrectfeedback>
|
|
|
201 |
<text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
|
|
|
202 |
</incorrectfeedback>
|
|
|
203 |
<shownumcorrect/>
|
|
|
204 |
<dragbox>
|
|
|
205 |
<text>Alpha</text>
|
|
|
206 |
<group>1</group>
|
|
|
207 |
</dragbox>
|
|
|
208 |
<dragbox>
|
|
|
209 |
<text>Beta</text>
|
|
|
210 |
<group>1</group>
|
|
|
211 |
</dragbox>
|
|
|
212 |
<dragbox>
|
|
|
213 |
<text>Gamma</text>
|
|
|
214 |
<group>1</group>
|
|
|
215 |
<infinite/>
|
|
|
216 |
</dragbox>
|
|
|
217 |
<hint>
|
|
|
218 |
<text>Try again.</text>
|
|
|
219 |
<shownumcorrect />
|
|
|
220 |
</hint>
|
|
|
221 |
<hint>
|
|
|
222 |
<text>These are the first three letters of the Greek alphabet.</text>
|
|
|
223 |
<shownumcorrect />
|
|
|
224 |
<clearwrong />
|
|
|
225 |
</hint>
|
|
|
226 |
</question>';
|
|
|
227 |
$xmldata = xmlize($xml);
|
|
|
228 |
|
|
|
229 |
$importer = new \qformat_xml();
|
|
|
230 |
$q = $importer->try_importing_using_qtypes(
|
|
|
231 |
$xmldata['question'], null, null, 'ddwtos');
|
|
|
232 |
|
|
|
233 |
$expectedq = new \stdClass();
|
|
|
234 |
$expectedq->qtype = 'ddwtos';
|
|
|
235 |
$expectedq->name = 'A drag-and-drop question';
|
|
|
236 |
$expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
|
|
|
237 |
$expectedq->questiontextformat = FORMAT_MOODLE;
|
|
|
238 |
$expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
|
|
|
239 |
$expectedq->defaultmark = 3;
|
|
|
240 |
$expectedq->length = 1;
|
|
|
241 |
$expectedq->penalty = 0.3333333;
|
|
|
242 |
|
|
|
243 |
$expectedq->shuffleanswers = 1;
|
|
|
244 |
$expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
|
|
|
245 |
'format' => FORMAT_MOODLE);
|
|
|
246 |
$expectedq->partiallycorrectfeedback = array(
|
|
|
247 |
'text' => '<p>Your answer is partially correct.</p>',
|
|
|
248 |
'format' => FORMAT_MOODLE);
|
|
|
249 |
$expectedq->shownumcorrect = true;
|
|
|
250 |
$expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
|
|
|
251 |
'format' => FORMAT_MOODLE);
|
|
|
252 |
|
|
|
253 |
$expectedq->choices = array(
|
|
|
254 |
array('answer' => 'Alpha', 'choicegroup' => 1, 'infinite' => false),
|
|
|
255 |
array('answer' => 'Beta', 'choicegroup' => 1, 'infinite' => false),
|
|
|
256 |
array('answer' => 'Gamma', 'choicegroup' => 1, 'infinite' => true),
|
|
|
257 |
);
|
|
|
258 |
|
|
|
259 |
$expectedq->hint = array(
|
|
|
260 |
array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
|
|
|
261 |
array('text' => 'These are the first three letters of the Greek alphabet.',
|
|
|
262 |
'format' => FORMAT_MOODLE));
|
|
|
263 |
$expectedq->hintshownumcorrect = array(true, true);
|
|
|
264 |
$expectedq->hintclearwrong = array(false, true);
|
|
|
265 |
|
|
|
266 |
$this->assert(new \question_check_specified_fields_expectation($expectedq), $q);
|
|
|
267 |
$this->assertEquals($expectedq->hint, $q->hint);
|
|
|
268 |
}
|
|
|
269 |
|
11 |
efrain |
270 |
public function test_xml_export(): void {
|
1 |
efrain |
271 |
$qdata = new \stdClass();
|
|
|
272 |
$qdata->id = 123;
|
|
|
273 |
$qdata->contextid = \context_system::instance()->id;
|
|
|
274 |
$qdata->idnumber = null;
|
|
|
275 |
$qdata->qtype = 'ddwtos';
|
|
|
276 |
$qdata->name = 'A drag-and-drop question';
|
|
|
277 |
$qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
|
|
|
278 |
$qdata->questiontextformat = FORMAT_MOODLE;
|
|
|
279 |
$qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
|
|
|
280 |
$qdata->generalfeedbackformat = FORMAT_MOODLE;
|
|
|
281 |
$qdata->defaultmark = 3;
|
|
|
282 |
$qdata->length = 1;
|
|
|
283 |
$qdata->penalty = 0.3333333;
|
|
|
284 |
$qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
285 |
|
|
|
286 |
$qdata->options = new \stdClass();
|
|
|
287 |
$qdata->options->shuffleanswers = 1;
|
|
|
288 |
$qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
|
|
|
289 |
$qdata->options->correctfeedbackformat = FORMAT_MOODLE;
|
|
|
290 |
$qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
|
|
|
291 |
$qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
|
|
|
292 |
$qdata->options->shownumcorrect = 1;
|
|
|
293 |
$qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
|
|
|
294 |
$qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
|
|
|
295 |
|
|
|
296 |
$qdata->options->answers = array(
|
|
|
297 |
13 => new question_answer(13, 'Alpha', 0,
|
|
|
298 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
|
|
|
299 |
FORMAT_MOODLE),
|
|
|
300 |
14 => new question_answer(14, 'Beta', 0,
|
|
|
301 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
|
|
|
302 |
FORMAT_MOODLE),
|
|
|
303 |
15 => new question_answer(15, 'Gamma', 0,
|
|
|
304 |
'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:1;}',
|
|
|
305 |
FORMAT_MOODLE),
|
|
|
306 |
);
|
|
|
307 |
|
|
|
308 |
$qdata->hints = array(
|
|
|
309 |
1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
|
|
|
310 |
2 => new question_hint_with_parts(2,
|
|
|
311 |
'These are the first three letters of the Greek alphabet.',
|
|
|
312 |
FORMAT_MOODLE, true, true),
|
|
|
313 |
);
|
|
|
314 |
|
|
|
315 |
$exporter = new \qformat_xml();
|
|
|
316 |
$xml = $exporter->writequestion($qdata);
|
|
|
317 |
|
|
|
318 |
$expectedxml = '<!-- question: 123 -->
|
|
|
319 |
<question type="ddwtos">
|
|
|
320 |
<name>
|
|
|
321 |
<text>A drag-and-drop question</text>
|
|
|
322 |
</name>
|
|
|
323 |
<questiontext format="moodle_auto_format">
|
|
|
324 |
<text>Put these in order: [[1]], [[2]], [[3]].</text>
|
|
|
325 |
</questiontext>
|
|
|
326 |
<generalfeedback format="moodle_auto_format">
|
|
|
327 |
<text>The answer is Alpha, Beta, Gamma.</text>
|
|
|
328 |
</generalfeedback>
|
|
|
329 |
<defaultgrade>3</defaultgrade>
|
|
|
330 |
<penalty>0.3333333</penalty>
|
|
|
331 |
<hidden>0</hidden>
|
|
|
332 |
<idnumber></idnumber>
|
|
|
333 |
<shuffleanswers>1</shuffleanswers>
|
|
|
334 |
<correctfeedback format="moodle_auto_format">
|
|
|
335 |
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
|
|
|
336 |
</correctfeedback>
|
|
|
337 |
<partiallycorrectfeedback format="moodle_auto_format">
|
|
|
338 |
<text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
|
|
|
339 |
</partiallycorrectfeedback>
|
|
|
340 |
<incorrectfeedback format="moodle_auto_format">
|
|
|
341 |
<text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
|
|
|
342 |
</incorrectfeedback>
|
|
|
343 |
<shownumcorrect/>
|
|
|
344 |
<dragbox>
|
|
|
345 |
<text>Alpha</text>
|
|
|
346 |
<group>1</group>
|
|
|
347 |
</dragbox>
|
|
|
348 |
<dragbox>
|
|
|
349 |
<text>Beta</text>
|
|
|
350 |
<group>1</group>
|
|
|
351 |
</dragbox>
|
|
|
352 |
<dragbox>
|
|
|
353 |
<text>Gamma</text>
|
|
|
354 |
<group>1</group>
|
|
|
355 |
<infinite/>
|
|
|
356 |
</dragbox>
|
|
|
357 |
<hint format="moodle_auto_format">
|
|
|
358 |
<text>Try again.</text>
|
|
|
359 |
<shownumcorrect/>
|
|
|
360 |
</hint>
|
|
|
361 |
<hint format="moodle_auto_format">
|
|
|
362 |
<text>These are the first three letters of the Greek alphabet.</text>
|
|
|
363 |
<shownumcorrect/>
|
|
|
364 |
<clearwrong/>
|
|
|
365 |
</hint>
|
|
|
366 |
</question>
|
|
|
367 |
';
|
|
|
368 |
|
|
|
369 |
$this->assert_same_xml($expectedxml, $xml);
|
|
|
370 |
}
|
|
|
371 |
}
|