Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / calculatedsimple / tests / upgradelibnewqe_test.php
CommitLineData
603bd001
PS
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 * Tests of the upgrade to the new Moodle question engine for attempts at
19 * simple calculated questions.
20 *
21 * @package qtype
22 * @subpackage calculatedsimple
23 * @copyright 2011 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->dirroot . '/question/engine/upgrade/tests/helper.php');
32
33
34/**
35 * Testing the upgrade of simple calculated question attempts.
36 *
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class qtype_calculatedsimple_attempt_upgrader_test extends question_attempt_upgrader_test_base {
41 public function test_calculatedsimple_adaptive_qsession95() {
42 $quiz = (object) array(
43 'id' => '4',
44 'course' => '2',
45 'name' => 'Calculated quiz',
46 'intro' => '',
47 'introformat' => '1',
48 'timeopen' => '0',
49 'timeclose' => '0',
50 'attempts' => '0',
51 'attemptonlast' => '0',
52 'grademethod' => '1',
53 'decimalpoints' => '2',
54 'questiondecimalpoints' => '-1',
55 'questionsperpage' => '1',
56 'shufflequestions' => '0',
57 'shuffleanswers' => '1',
603bd001
PS
58 'sumgrades' => '3.00000',
59 'grade' => '10.00000',
60 'timecreated' => '0',
61 'timemodified' => '1305648351',
62 'timelimit' => '0',
63 'password' => '',
64 'subnet' => '',
65 'popup' => '0',
66 'delay1' => '0',
67 'delay2' => '0',
68 'showuserpicture' => '0',
69 'showblocks' => '0',
70 'preferredbehaviour' => 'adaptive',
71 'reviewattempt' => '69888',
72 'reviewcorrectness' => '69888',
73 'reviewmarks' => '69888',
74 'reviewspecificfeedback' => '69888',
75 'reviewgeneralfeedback' => '69888',
76 'reviewrightanswer' => '69888',
77 'reviewoverallfeedback' => '4352',
78 );
79 $attempt = (object) array(
80 'id' => '13',
81 'uniqueid' => '13',
82 'quiz' => '4',
83 'userid' => '4',
84 'attempt' => '1',
85 'sumgrades' => '0.00000',
86 'timestart' => '1305830650',
87 'timefinish' => '1305830656',
88 'timemodified' => '1305830656',
89 'layout' => '16,0,17,0,18,0',
90 'preview' => '0',
603bd001
PS
91 );
92 $question = (object) array(
93 'id' => '16',
94 'category' => '2',
95 'parent' => '0',
96 'name' => 'Calculated simple',
97 'questiontext' => '<p>What is {={a}} + {={b}} ?</p>',
98 'questiontextformat' => '1',
99 'generalfeedback' => '',
100 'generalfeedbackformat' => '1',
101 'defaultmark' => '1.0000000',
102 'penalty' => '0.1',
103 'qtype' => 'calculatedsimple',
104 'length' => '1',
105 'stamp' => 'tjh238.vledev2.open.ac.uk+110517161007+2Barhu',
106 'version' => 'tjh238.vledev2.open.ac.uk+110517161008+Mu6OQu',
107 'hidden' => '0',
108 'timecreated' => '1305648607',
109 'timemodified' => '1305648607',
110 'createdby' => '2',
111 'modifiedby' => '2',
112 'maxmark' => '1.0000000',
113 'options' => (object) array(
114 'synchronize' => 0,
115 'single' => 0,
116 'answernumbering' => 'abc',
117 'shuffleanswers' => 0,
118 'correctfeedback' => '',
119 'partiallycorrectfeedback' => '',
120 'incorrectfeedback' => '',
121 'correctfeedbackformat' => 0,
122 'partiallycorrectfeedbackformat' => 0,
123 'incorrectfeedbackformat' => 0,
124 'answers' => array(
125 23 => (object) array(
126 'id' => '23',
127 'question' => '16',
128 'answer' => '{a} + {b}',
129 'answerformat' => '0',
130 'fraction' => '1.0000000',
131 'feedback' => '<p>Well done!</p>',
132 'feedbackformat' => '1',
133 'tolerance' => '0.01',
134 'tolerancetype' => '1',
135 'correctanswerlength' => '2',
136 'correctanswerformat' => '1',
137 ),
138 ),
139 'units' => array(
140 ),
141 'unitgradingtype' => '0',
142 'unitpenalty' => '0.1000000',
143 'showunits' => '3',
144 'unitsleft' => '0',
145 ),
146 'hints' => array(
147 ),
148 );
149 $qsession = (object) array(
150 'id' => '95',
151 'attemptid' => '13',
152 'questionid' => '16',
153 'newest' => '256',
154 'newgraded' => '256',
155 'sumpenalty' => '0.1000000',
156 'manualcomment' => '',
157 'manualcommentformat' => '1',
158 'flagged' => '0',
159 );
160 $qstates = array(
161 253 => (object) array(
162 'id' => '253',
163 'attempt' => '13',
164 'question' => '16',
165 'seq_number' => '0',
166 'answer' => 'dataset7-|||||',
167 'timestamp' => '1305830650',
168 'event' => '0',
169 'grade' => '0.0000000',
170 'raw_grade' => '0.0000000',
171 'penalty' => '0.0000000',
172 ),
173 256 => (object) array(
174 'id' => '256',
175 'attempt' => '13',
176 'question' => '16',
177 'seq_number' => '1',
178 'answer' => 'dataset7-|||||',
179 'timestamp' => '1305830650',
180 'event' => '6',
181 'grade' => '0.0000000',
182 'raw_grade' => '0.0000000',
183 'penalty' => '0.1000000',
184 ),
185 );
186 $this->loader->put_dataset_in_cache($question->id, 7, array('a' => '3', 'b' => '6'));
187
188 $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
189
190 $expectedqa = (object) array(
191 'behaviour' => 'adaptive',
192 'questionid' => 16,
193 'variant' => 7,
194 'maxmark' => 1.0000000,
195 'minfraction' => 0,
4e3d8293 196 'maxfraction' => 1,
603bd001
PS
197 'flagged' => 0,
198 'questionsummary' => 'What is 3 + 6 ?',
199 'rightanswer' => '9',
200 'responsesummary' => '',
201 'timemodified' => 1305830650,
202 'steps' => array(
203 0 => (object) array(
204 'sequencenumber' => 0,
205 'state' => 'todo',
206 'fraction' => null,
207 'timecreated' => 1305830650,
208 'userid' => 4,
209 'data' => array('_separators' => '.$,',
210 '_var_a' => '3', '_var_b' => '6'),
211 ),
212 1 => (object) array(
213 'sequencenumber' => 1,
214 'state' => 'gradedwrong',
215 'fraction' => 0,
216 'timecreated' => 1305830650,
217 'userid' => 4,
218 'data' => array('answer' => '', '-finish' => 1, '-_try' => 1, '-_rawfraction' => 0),
219 ),
220 ),
221 );
222
223 $this->compare_qas($expectedqa, $qa);
224 }
225
226 public function test_calculatedsimple_adaptive_qsession98() {
227 $quiz = (object) array(
228 'id' => '4',
229 'course' => '2',
230 'name' => 'Calculated quiz',
231 'intro' => '',
232 'introformat' => '1',
233 'timeopen' => '0',
234 'timeclose' => '0',
235 'attempts' => '0',
236 'attemptonlast' => '0',
237 'grademethod' => '1',
238 'decimalpoints' => '2',
239 'questiondecimalpoints' => '-1',
240 'questionsperpage' => '1',
241 'shufflequestions' => '0',
242 'shuffleanswers' => '1',
603bd001
PS
243 'sumgrades' => '3.00000',
244 'grade' => '10.00000',
245 'timecreated' => '0',
246 'timemodified' => '1305648351',
247 'timelimit' => '0',
248 'password' => '',
249 'subnet' => '',
250 'popup' => '0',
251 'delay1' => '0',
252 'delay2' => '0',
253 'showuserpicture' => '0',
254 'showblocks' => '0',
255 'preferredbehaviour' => 'adaptive',
256 'reviewattempt' => '69888',
257 'reviewcorrectness' => '69888',
258 'reviewmarks' => '69888',
259 'reviewspecificfeedback' => '69888',
260 'reviewgeneralfeedback' => '69888',
261 'reviewrightanswer' => '69888',
262 'reviewoverallfeedback' => '4352',
263 );
264 $attempt = (object) array(
265 'id' => '14',
266 'uniqueid' => '14',
267 'quiz' => '4',
268 'userid' => '4',
269 'attempt' => '2',
270 'sumgrades' => '2.80000',
271 'timestart' => '1305830661',
272 'timefinish' => '1305830729',
273 'timemodified' => '1305830729',
274 'layout' => '16,0,17,0,18,0',
275 'preview' => '0',
603bd001
PS
276 );
277 $question = (object) array(
278 'id' => '16',
279 'category' => '2',
280 'parent' => '0',
281 'name' => 'Calculated simple',
282 'questiontext' => '<p>What is {={a}} + {={b}} ?</p>',
283 'questiontextformat' => '1',
284 'generalfeedback' => '',
285 'generalfeedbackformat' => '1',
286 'defaultmark' => '1.0000000',
287 'penalty' => '0.1',
288 'qtype' => 'calculatedsimple',
289 'length' => '1',
290 'stamp' => 'tjh238.vledev2.open.ac.uk+110517161007+2Barhu',
291 'version' => 'tjh238.vledev2.open.ac.uk+110517161008+Mu6OQu',
292 'hidden' => '0',
293 'timecreated' => '1305648607',
294 'timemodified' => '1305648607',
295 'createdby' => '2',
296 'modifiedby' => '2',
297 'maxmark' => '1.0000000',
298 'options' => (object) array(
299 'synchronize' => 0,
300 'single' => 0,
301 'answernumbering' => 'abc',
302 'shuffleanswers' => 0,
303 'correctfeedback' => '',
304 'partiallycorrectfeedback' => '',
305 'incorrectfeedback' => '',
306 'correctfeedbackformat' => 0,
307 'partiallycorrectfeedbackformat' => 0,
308 'incorrectfeedbackformat' => 0,
309 'answers' => array(
310 23 => (object) array(
311 'id' => '23',
312 'question' => '16',
313 'answer' => '{a} + {b}',
314 'answerformat' => '0',
315 'fraction' => '1.0000000',
316 'feedback' => '<p>Well done!</p>',
317 'feedbackformat' => '1',
318 'tolerance' => '0.01',
319 'tolerancetype' => '1',
320 'correctanswerlength' => '2',
321 'correctanswerformat' => '1',
322 ),
323 ),
324 'units' => array(
325 ),
326 'unitgradingtype' => '0',
327 'unitpenalty' => '0.1000000',
328 'showunits' => '3',
329 'unitsleft' => '0',
330 ),
331 'hints' => array(
332 ),
333 );
334 $qsession = (object) array(
335 'id' => '98',
336 'attemptid' => '14',
337 'questionid' => '16',
338 'newest' => '267',
339 'newgraded' => '267',
340 'sumpenalty' => '0.3000000',
341 'manualcomment' => '',
342 'manualcommentformat' => '1',
343 'flagged' => '0',
344 );
345 $qstates = array(
346 259 => (object) array(
347 'id' => '259',
348 'attempt' => '14',
349 'question' => '16',
350 'seq_number' => '0',
351 'answer' => 'dataset4-|||||',
352 'timestamp' => '1305830661',
353 'event' => '0',
354 'grade' => '0.0000000',
355 'raw_grade' => '0.0000000',
356 'penalty' => '0.0000000',
357 ),
358 262 => (object) array(
359 'id' => '262',
360 'attempt' => '14',
361 'question' => '16',
362 'seq_number' => '1',
363 'answer' => 'dataset4-9.00|||||',
364 'timestamp' => '1305830668',
365 'event' => '3',
366 'grade' => '0.0000000',
367 'raw_grade' => '0.0000000',
368 'penalty' => '0.1000000',
369 ),
370 263 => (object) array(
371 'id' => '263',
372 'attempt' => '14',
373 'question' => '16',
374 'seq_number' => '2',
375 'answer' => 'dataset4-15.40|||||',
376 'timestamp' => '1305830679',
377 'event' => '3',
378 'grade' => '0.9000000',
379 'raw_grade' => '1.0000000',
380 'penalty' => '0.1000000',
381 ),
382 267 => (object) array(
383 'id' => '267',
384 'attempt' => '14',
385 'question' => '16',
386 'seq_number' => '3',
387 'answer' => 'dataset4-15.40|||||',
388 'timestamp' => '1305830679',
389 'event' => '6',
390 'grade' => '0.9000000',
391 'raw_grade' => '1.0000000',
392 'penalty' => '0.1000000',
393 ),
394 );
395 $this->loader->put_dataset_in_cache($question->id, 4, array('a' => '6.4', 'b' => '9'));
396
397 $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
398
399 $expectedqa = (object) array(
400 'behaviour' => 'adaptive',
401 'questionid' => 16,
402 'variant' => 4,
403 'maxmark' => 1.0000000,
404 'minfraction' => 0,
4e3d8293 405 'maxfraction' => 1,
603bd001
PS
406 'flagged' => 0,
407 'questionsummary' => 'What is 6.4 + 9 ?',
408 'rightanswer' => '15.4',
409 'responsesummary' => '15.40',
410 'timemodified' => 1305830679,
411 'steps' => array(
412 0 => (object) array(
413 'sequencenumber' => 0,
414 'state' => 'todo',
415 'fraction' => null,
416 'timecreated' => 1305830661,
417 'userid' => 4,
418 'data' => array('_separators' => '.$,',
419 '_var_a' => '6.4', '_var_b' => '9'),
420 ),
421 1 => (object) array(
422 'sequencenumber' => 1,
423 'state' => 'todo',
424 'fraction' => 0,
425 'timecreated' => 1305830668,
426 'userid' => 4,
427 'data' => array('answer' => '9.00', '-submit' => 1, '-_try' => 1, '-_rawfraction' => 0),
428 ),
429 2 => (object) array(
430 'sequencenumber' => 2,
431 'state' => 'todo',
432 'fraction' => 0.9,
433 'timecreated' => 1305830679,
434 'userid' => 4,
435 'data' => array('answer' => '15.40', '-submit' => 1, '-_try' => 2, '-_rawfraction' => 1),
436 ),
437 3 => (object) array(
438 'sequencenumber' => 3,
439 'state' => 'gradedright',
440 'fraction' => 0.9,
441 'timecreated' => 1305830679,
442 'userid' => 4,
443 'data' => array('answer' => '15.40', '-finish' => 1, '-_try' => 2, '-_rawfraction' => 1),
444 ),
445 ),
446 );
447
448 $this->compare_qas($expectedqa, $qa);
449 }
450
451 public function test_calculatedsimple_adaptive_qsession101() {
452 $quiz = (object) array(
453 'id' => '4',
454 'course' => '2',
455 'name' => 'Calculated quiz',
456 'intro' => '',
457 'introformat' => '1',
458 'timeopen' => '0',
459 'timeclose' => '0',
460 'attempts' => '0',
461 'attemptonlast' => '0',
462 'grademethod' => '1',
463 'decimalpoints' => '2',
464 'questiondecimalpoints' => '-1',
465 'questionsperpage' => '1',
466 'shufflequestions' => '0',
467 'shuffleanswers' => '1',
603bd001
PS
468 'sumgrades' => '3.00000',
469 'grade' => '10.00000',
470 'timecreated' => '0',
471 'timemodified' => '1305648351',
472 'timelimit' => '0',
473 'password' => '',
474 'subnet' => '',
475 'popup' => '0',
476 'delay1' => '0',
477 'delay2' => '0',
478 'showuserpicture' => '0',
479 'showblocks' => '0',
480 'preferredbehaviour' => 'adaptive',
481 'reviewattempt' => '69888',
482 'reviewcorrectness' => '69888',
483 'reviewmarks' => '69888',
484 'reviewspecificfeedback' => '69888',
485 'reviewgeneralfeedback' => '69888',
486 'reviewrightanswer' => '69888',
487 'reviewoverallfeedback' => '4352',
488 );
489 $attempt = (object) array(
490 'id' => '15',
491 'uniqueid' => '15',
492 'quiz' => '4',
493 'userid' => '3',
494 'attempt' => '1',
495 'sumgrades' => '0.70000',
496 'timestart' => '1305830744',
497 'timefinish' => '0',
498 'timemodified' => '1305830792',
499 'layout' => '16,0,17,0,18,0',
500 'preview' => '0',
603bd001
PS
501 );
502 $question = (object) array(
503 'id' => '16',
504 'category' => '2',
505 'parent' => '0',
506 'name' => 'Calculated simple',
507 'questiontext' => '<p>What is {={a}} + {={b}} ?</p>',
508 'questiontextformat' => '1',
509 'generalfeedback' => '',
510 'generalfeedbackformat' => '1',
511 'defaultmark' => '1.0000000',
512 'penalty' => '0.1',
513 'qtype' => 'calculatedsimple',
514 'length' => '1',
515 'stamp' => 'tjh238.vledev2.open.ac.uk+110517161007+2Barhu',
516 'version' => 'tjh238.vledev2.open.ac.uk+110517161008+Mu6OQu',
517 'hidden' => '0',
518 'timecreated' => '1305648607',
519 'timemodified' => '1305648607',
520 'createdby' => '2',
521 'modifiedby' => '2',
522 'maxmark' => '1.0000000',
523 'options' => (object) array(
524 'synchronize' => 0,
525 'single' => 0,
526 'answernumbering' => 'abc',
527 'shuffleanswers' => 0,
528 'correctfeedback' => '',
529 'partiallycorrectfeedback' => '',
530 'incorrectfeedback' => '',
531 'correctfeedbackformat' => 0,
532 'partiallycorrectfeedbackformat' => 0,
533 'incorrectfeedbackformat' => 0,
534 'answers' => array(
535 23 => (object) array(
536 'id' => '23',
537 'question' => '16',
538 'answer' => '{a} + {b}',
539 'answerformat' => '0',
540 'fraction' => '1.0000000',
541 'feedback' => '<p>Well done!</p>',
542 'feedbackformat' => '1',
543 'tolerance' => '0.01',
544 'tolerancetype' => '1',
545 'correctanswerlength' => '2',
546 'correctanswerformat' => '1',
547 ),
548 ),
549 'units' => array(
550 ),
551 'unitgradingtype' => '0',
552 'unitpenalty' => '0.1000000',
553 'showunits' => '3',
554 'unitsleft' => '0',
555 ),
556 'hints' => array(
557 ),
558 );
559 $qsession = (object) array(
560 'id' => '101',
561 'attemptid' => '15',
562 'questionid' => '16',
563 'newest' => '273',
564 'newgraded' => '270',
565 'sumpenalty' => '0.0000000',
566 'manualcomment' => '',
567 'manualcommentformat' => '1',
568 'flagged' => '0',
569 );
570 $qstates = array(
571 270 => (object) array(
572 'id' => '270',
573 'attempt' => '15',
574 'question' => '16',
575 'seq_number' => '0',
576 'answer' => 'dataset6-|||||',
577 'timestamp' => '1305830744',
578 'event' => '0',
579 'grade' => '0.0000000',
580 'raw_grade' => '0.0000000',
581 'penalty' => '0.0000000',
582 ),
583 273 => (object) array(
584 'id' => '273',
585 'attempt' => '15',
586 'question' => '16',
587 'seq_number' => '1',
588 'answer' => 'dataset6-13.1|||||',
589 'timestamp' => '1305830755',
590 'event' => '2',
591 'grade' => '0.0000000',
592 'raw_grade' => '1.0000000',
593 'penalty' => '0.1000000',
594 ),
595 );
596 $this->loader->put_dataset_in_cache($question->id, 6, array('a' => '6.1', 'b' => '7'));
597
598 $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
599
600 $expectedqa = (object) array(
601 'behaviour' => 'adaptive',
602 'questionid' => 16,
603 'variant' => 6,
604 'maxmark' => 1.0000000,
605 'minfraction' => 0,
4e3d8293 606 'maxfraction' => 1,
603bd001
PS
607 'flagged' => 0,
608 'questionsummary' => 'What is 6.1 + 7 ?',
609 'rightanswer' => '13.1',
610 'responsesummary' => '13.1',
611 'timemodified' => 1305830755,
612 'steps' => array(
613 0 => (object) array(
614 'sequencenumber' => 0,
615 'state' => 'todo',
616 'fraction' => null,
617 'timecreated' => 1305830744,
618 'userid' => 3,
619 'data' => array('_separators' => '.$,',
620 '_var_a' => '6.1', '_var_b' => '7'),
621 ),
622 1 => (object) array(
623 'sequencenumber' => 1,
624 'state' => 'complete',
625 'fraction' => null,
626 'timecreated' => 1305830755,
627 'userid' => 3,
628 'data' => array('answer' => 13.1),
629 ),
630 ),
631 );
632
633 $this->compare_qas($expectedqa, $qa);
634 }
635}