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 |
/**
|
|
|
18 |
* Quiz module external functions tests.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_quiz
|
|
|
21 |
* @category external
|
|
|
22 |
* @copyright 2016 Juan Leyva <juan@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 3.1
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
namespace mod_quiz\external;
|
|
|
28 |
|
|
|
29 |
use core_external\external_api;
|
|
|
30 |
use core_question\local\bank\question_version_status;
|
|
|
31 |
use externallib_advanced_testcase;
|
|
|
32 |
use mod_quiz\question\display_options;
|
|
|
33 |
use mod_quiz\quiz_attempt;
|
|
|
34 |
use mod_quiz\quiz_settings;
|
|
|
35 |
use mod_quiz\structure;
|
|
|
36 |
use mod_quiz_external;
|
|
|
37 |
use moodle_exception;
|
|
|
38 |
|
|
|
39 |
defined('MOODLE_INTERNAL') || die();
|
|
|
40 |
|
|
|
41 |
global $CFG;
|
|
|
42 |
|
|
|
43 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
44 |
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Silly class to access mod_quiz_external internal methods.
|
|
|
48 |
*
|
|
|
49 |
* @package mod_quiz
|
|
|
50 |
* @copyright 2016 Juan Leyva <juan@moodle.com>
|
|
|
51 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
52 |
* @since Moodle 3.1
|
|
|
53 |
*/
|
|
|
54 |
class testable_mod_quiz_external extends mod_quiz_external {
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Public accessor.
|
|
|
58 |
*
|
|
|
59 |
* @param array $params Array of parameters including the attemptid and preflight data
|
|
|
60 |
* @param bool $checkaccessrules whether to check the quiz access rules or not
|
|
|
61 |
* @param bool $failifoverdue whether to return error if the attempt is overdue
|
|
|
62 |
* @return array containing the attempt object and access messages
|
|
|
63 |
*/
|
|
|
64 |
public static function validate_attempt($params, $checkaccessrules = true, $failifoverdue = true) {
|
|
|
65 |
return parent::validate_attempt($params, $checkaccessrules, $failifoverdue);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Public accessor.
|
|
|
70 |
*
|
|
|
71 |
* @param array $params Array of parameters including the attemptid
|
|
|
72 |
* @return array containing the attempt object and display options
|
|
|
73 |
*/
|
|
|
74 |
public static function validate_attempt_review($params) {
|
|
|
75 |
return parent::validate_attempt_review($params);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Quiz module external functions tests
|
|
|
81 |
*
|
|
|
82 |
* @package mod_quiz
|
|
|
83 |
* @category external
|
|
|
84 |
* @copyright 2016 Juan Leyva <juan@moodle.com>
|
|
|
85 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
86 |
* @since Moodle 3.1
|
|
|
87 |
* @covers \mod_quiz_external
|
|
|
88 |
*/
|
|
|
89 |
class external_test extends externallib_advanced_testcase {
|
|
|
90 |
|
|
|
91 |
use \quiz_question_helper_test_trait;
|
|
|
92 |
|
|
|
93 |
/** @var \stdClass course record. */
|
|
|
94 |
protected $course;
|
|
|
95 |
|
|
|
96 |
/** @var \stdClass activity record. */
|
|
|
97 |
protected $quiz;
|
|
|
98 |
|
|
|
99 |
/** @var \context_module context instance. */
|
|
|
100 |
protected $context;
|
|
|
101 |
|
|
|
102 |
/** @var \stdClass */
|
|
|
103 |
protected $cm;
|
|
|
104 |
|
|
|
105 |
/** @var \stdClass user record. */
|
|
|
106 |
protected $student;
|
|
|
107 |
|
|
|
108 |
/** @var \stdClass user record. */
|
|
|
109 |
protected $teacher;
|
|
|
110 |
|
|
|
111 |
/** @var \stdClass user role record. */
|
|
|
112 |
protected $studentrole;
|
|
|
113 |
|
|
|
114 |
/** @var \stdClass user role record. */
|
|
|
115 |
protected $teacherrole;
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Set up for every test
|
|
|
119 |
*/
|
|
|
120 |
public function setUp(): void {
|
|
|
121 |
global $DB;
|
|
|
122 |
$this->resetAfterTest();
|
|
|
123 |
$this->setAdminUser();
|
|
|
124 |
|
|
|
125 |
// Setup test data.
|
|
|
126 |
$this->course = $this->getDataGenerator()->create_course();
|
|
|
127 |
$this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
|
|
|
128 |
$this->context = \context_module::instance($this->quiz->cmid);
|
|
|
129 |
$this->cm = get_coursemodule_from_instance('quiz', $this->quiz->id);
|
|
|
130 |
|
|
|
131 |
// Create users.
|
|
|
132 |
$this->student = self::getDataGenerator()->create_user();
|
|
|
133 |
$this->teacher = self::getDataGenerator()->create_user();
|
|
|
134 |
|
|
|
135 |
// Users enrolments.
|
|
|
136 |
$this->studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
137 |
$this->teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
|
|
|
138 |
// Allow student to receive messages.
|
|
|
139 |
$coursecontext = \context_course::instance($this->course->id);
|
|
|
140 |
assign_capability('mod/quiz:emailnotifysubmission', CAP_ALLOW, $this->teacherrole->id, $coursecontext, true);
|
|
|
141 |
|
|
|
142 |
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
|
|
|
143 |
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Create a quiz with questions including a started or finished attempt optionally
|
|
|
148 |
*
|
|
|
149 |
* @param boolean $startattempt whether to start a new attempt
|
|
|
150 |
* @param boolean $finishattempt whether to finish the new attempt
|
|
|
151 |
* @param string $behaviour the quiz preferredbehaviour, defaults to 'deferredfeedback'.
|
|
|
152 |
* @param boolean $includeqattachments whether to include a question that supports attachments, defaults to false.
|
|
|
153 |
* @param array $extraoptions extra options for Quiz.
|
|
|
154 |
* @return array array containing the quiz, context and the attempt
|
|
|
155 |
*/
|
|
|
156 |
private function create_quiz_with_questions($startattempt = false, $finishattempt = false, $behaviour = 'deferredfeedback',
|
|
|
157 |
$includeqattachments = false, $extraoptions = []) {
|
|
|
158 |
|
|
|
159 |
// Create a new quiz with attempts.
|
|
|
160 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
161 |
$data = ['course' => $this->course->id,
|
|
|
162 |
'sumgrades' => 2,
|
|
|
163 |
'preferredbehaviour' => $behaviour];
|
|
|
164 |
$data = array_merge($data, $extraoptions);
|
|
|
165 |
$quiz = $quizgenerator->create_instance($data);
|
|
|
166 |
$context = \context_module::instance($quiz->cmid);
|
|
|
167 |
|
|
|
168 |
// Create a couple of questions.
|
|
|
169 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
170 |
|
|
|
171 |
$cat = $questiongenerator->create_question_category();
|
|
|
172 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
173 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
174 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
175 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
176 |
|
|
|
177 |
if ($includeqattachments) {
|
|
|
178 |
$question = $questiongenerator->create_question('essay', null, ['category' => $cat->id, 'attachments' => 1,
|
|
|
179 |
'attachmentsrequired' => 1]);
|
|
|
180 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
$quizobj = quiz_settings::create($quiz->id, $this->student->id);
|
|
|
184 |
|
|
|
185 |
// Set grade to pass.
|
|
|
186 |
$item = \grade_item::fetch(['courseid' => $this->course->id, 'itemtype' => 'mod',
|
|
|
187 |
'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null]);
|
|
|
188 |
$item->gradepass = 80;
|
|
|
189 |
$item->update();
|
|
|
190 |
|
|
|
191 |
if ($startattempt or $finishattempt) {
|
|
|
192 |
// Now, do one attempt.
|
|
|
193 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
194 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
195 |
|
|
|
196 |
$timenow = time();
|
|
|
197 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $this->student->id);
|
|
|
198 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
199 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
200 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
201 |
|
|
|
202 |
if ($finishattempt) {
|
|
|
203 |
// Process some responses from the student.
|
|
|
204 |
$tosubmit = [1 => ['answer' => '3.14']];
|
|
|
205 |
$attemptobj->process_submitted_actions(time(), false, $tosubmit);
|
|
|
206 |
|
|
|
207 |
// Finish the attempt.
|
|
|
208 |
$attemptobj->process_finish(time(), false);
|
|
|
209 |
}
|
|
|
210 |
return [$quiz, $context, $quizobj, $attempt, $attemptobj, $quba];
|
|
|
211 |
} else {
|
|
|
212 |
return [$quiz, $context, $quizobj];
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/*
|
|
|
218 |
* Test get quizzes by courses
|
|
|
219 |
*/
|
11 |
efrain |
220 |
public function test_mod_quiz_get_quizzes_by_courses(): void {
|
1 |
efrain |
221 |
global $DB;
|
|
|
222 |
|
|
|
223 |
// Create additional course.
|
|
|
224 |
$course2 = self::getDataGenerator()->create_course();
|
|
|
225 |
|
|
|
226 |
// Second quiz.
|
|
|
227 |
$record = new \stdClass();
|
|
|
228 |
$record->course = $course2->id;
|
|
|
229 |
$record->intro = '<button>Test with HTML allowed.</button>';
|
|
|
230 |
$quiz2 = self::getDataGenerator()->create_module('quiz', $record);
|
|
|
231 |
|
|
|
232 |
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
|
|
|
233 |
$enrol = enrol_get_plugin('manual');
|
|
|
234 |
$enrolinstances = enrol_get_instances($course2->id, true);
|
|
|
235 |
foreach ($enrolinstances as $courseenrolinstance) {
|
|
|
236 |
if ($courseenrolinstance->enrol == "manual") {
|
|
|
237 |
$instance2 = $courseenrolinstance;
|
|
|
238 |
break;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
$enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
|
|
|
242 |
|
|
|
243 |
self::setUser($this->student);
|
|
|
244 |
|
|
|
245 |
$returndescription = mod_quiz_external::get_quizzes_by_courses_returns();
|
|
|
246 |
|
|
|
247 |
// Create what we expect to be returned when querying the two courses.
|
|
|
248 |
// First for the student user.
|
|
|
249 |
$allusersfields = ['id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'lang',
|
|
|
250 |
'timeopen', 'timeclose', 'grademethod', 'section', 'visible', 'groupmode', 'groupingid',
|
|
|
251 |
'attempts', 'timelimit', 'grademethod', 'decimalpoints', 'questiondecimalpoints', 'sumgrades',
|
|
|
252 |
'grade', 'preferredbehaviour', 'hasfeedback'];
|
|
|
253 |
$userswithaccessfields = ['attemptonlast', 'reviewattempt', 'reviewcorrectness', 'reviewmaxmarks', 'reviewmarks',
|
|
|
254 |
'reviewspecificfeedback', 'reviewgeneralfeedback', 'reviewrightanswer',
|
|
|
255 |
'reviewoverallfeedback', 'questionsperpage', 'navmethod',
|
|
|
256 |
'browsersecurity', 'delay1', 'delay2', 'showuserpicture', 'showblocks',
|
|
|
257 |
'completionattemptsexhausted', 'completionpass', 'autosaveperiod', 'hasquestions',
|
|
|
258 |
'overduehandling', 'graceperiod', 'canredoquestions', 'allowofflineattempts'];
|
|
|
259 |
$managerfields = ['shuffleanswers', 'timecreated', 'timemodified', 'password', 'subnet'];
|
|
|
260 |
|
|
|
261 |
// Add expected coursemodule and other data.
|
|
|
262 |
$quiz1 = $this->quiz;
|
|
|
263 |
$quiz1->coursemodule = $quiz1->cmid;
|
|
|
264 |
$quiz1->introformat = 1;
|
|
|
265 |
$quiz1->section = 0;
|
|
|
266 |
$quiz1->visible = true;
|
|
|
267 |
$quiz1->groupmode = 0;
|
|
|
268 |
$quiz1->groupingid = 0;
|
|
|
269 |
$quiz1->hasquestions = 0;
|
|
|
270 |
$quiz1->hasfeedback = 0;
|
|
|
271 |
$quiz1->completionpass = 0;
|
|
|
272 |
$quiz1->autosaveperiod = get_config('quiz', 'autosaveperiod');
|
|
|
273 |
$quiz1->introfiles = [];
|
|
|
274 |
$quiz1->lang = '';
|
|
|
275 |
|
|
|
276 |
$quiz2->coursemodule = $quiz2->cmid;
|
|
|
277 |
$quiz2->introformat = 1;
|
|
|
278 |
$quiz2->section = 0;
|
|
|
279 |
$quiz2->visible = true;
|
|
|
280 |
$quiz2->groupmode = 0;
|
|
|
281 |
$quiz2->groupingid = 0;
|
|
|
282 |
$quiz2->hasquestions = 0;
|
|
|
283 |
$quiz2->hasfeedback = 0;
|
|
|
284 |
$quiz2->completionpass = 0;
|
|
|
285 |
$quiz2->autosaveperiod = get_config('quiz', 'autosaveperiod');
|
|
|
286 |
$quiz2->introfiles = [];
|
|
|
287 |
$quiz2->lang = '';
|
|
|
288 |
|
|
|
289 |
foreach (array_merge($allusersfields, $userswithaccessfields) as $field) {
|
|
|
290 |
$expected1[$field] = $quiz1->{$field};
|
|
|
291 |
$expected2[$field] = $quiz2->{$field};
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
$expectedquizzes = [$expected2, $expected1];
|
|
|
295 |
|
|
|
296 |
// Call the external function passing course ids.
|
|
|
297 |
$result = mod_quiz_external::get_quizzes_by_courses([$course2->id, $this->course->id]);
|
|
|
298 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
299 |
|
|
|
300 |
$this->assertEquals($expectedquizzes, $result['quizzes']);
|
|
|
301 |
$this->assertCount(0, $result['warnings']);
|
|
|
302 |
|
|
|
303 |
// Call the external function without passing course id.
|
|
|
304 |
$result = mod_quiz_external::get_quizzes_by_courses();
|
|
|
305 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
306 |
$this->assertEquals($expectedquizzes, $result['quizzes']);
|
|
|
307 |
$this->assertCount(0, $result['warnings']);
|
|
|
308 |
|
|
|
309 |
// Unenrol user from second course and alter expected quizzes.
|
|
|
310 |
$enrol->unenrol_user($instance2, $this->student->id);
|
|
|
311 |
array_shift($expectedquizzes);
|
|
|
312 |
|
|
|
313 |
// Call the external function without passing course id.
|
|
|
314 |
$result = mod_quiz_external::get_quizzes_by_courses();
|
|
|
315 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
316 |
$this->assertEquals($expectedquizzes, $result['quizzes']);
|
|
|
317 |
|
|
|
318 |
// Call for the second course we unenrolled the user from, expected warning.
|
|
|
319 |
$result = mod_quiz_external::get_quizzes_by_courses([$course2->id]);
|
|
|
320 |
$this->assertCount(1, $result['warnings']);
|
|
|
321 |
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
|
|
|
322 |
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
|
|
|
323 |
|
|
|
324 |
// Now, try as a teacher for getting all the additional fields.
|
|
|
325 |
self::setUser($this->teacher);
|
|
|
326 |
|
|
|
327 |
foreach ($managerfields as $field) {
|
|
|
328 |
$expectedquizzes[0][$field] = $quiz1->{$field};
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
$result = mod_quiz_external::get_quizzes_by_courses();
|
|
|
332 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
333 |
$this->assertEquals($expectedquizzes, $result['quizzes']);
|
|
|
334 |
|
|
|
335 |
// Admin also should get all the information.
|
|
|
336 |
self::setAdminUser();
|
|
|
337 |
|
|
|
338 |
$result = mod_quiz_external::get_quizzes_by_courses([$this->course->id]);
|
|
|
339 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
340 |
$this->assertEquals($expectedquizzes, $result['quizzes']);
|
|
|
341 |
|
|
|
342 |
// Now, prevent access.
|
|
|
343 |
$enrol->enrol_user($instance2, $this->student->id);
|
|
|
344 |
|
|
|
345 |
self::setUser($this->student);
|
|
|
346 |
|
|
|
347 |
$quiz2->timeclose = time() - DAYSECS;
|
|
|
348 |
$DB->update_record('quiz', $quiz2);
|
|
|
349 |
|
|
|
350 |
$result = mod_quiz_external::get_quizzes_by_courses();
|
|
|
351 |
$result = external_api::clean_returnvalue($returndescription, $result);
|
|
|
352 |
$this->assertCount(2, $result['quizzes']);
|
|
|
353 |
// We only see a limited set of fields.
|
|
|
354 |
$this->assertCount(5, $result['quizzes'][0]);
|
|
|
355 |
$this->assertEquals($quiz2->id, $result['quizzes'][0]['id']);
|
|
|
356 |
$this->assertEquals($quiz2->cmid, $result['quizzes'][0]['coursemodule']);
|
|
|
357 |
$this->assertEquals($quiz2->course, $result['quizzes'][0]['course']);
|
|
|
358 |
$this->assertEquals($quiz2->name, $result['quizzes'][0]['name']);
|
|
|
359 |
$this->assertEquals($quiz2->course, $result['quizzes'][0]['course']);
|
|
|
360 |
|
|
|
361 |
$this->assertFalse(isset($result['quizzes'][0]['timelimit']));
|
|
|
362 |
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
/**
|
|
|
366 |
* Test test_view_quiz
|
|
|
367 |
*/
|
11 |
efrain |
368 |
public function test_view_quiz(): void {
|
1 |
efrain |
369 |
global $DB;
|
|
|
370 |
|
|
|
371 |
// Test invalid instance id.
|
|
|
372 |
try {
|
|
|
373 |
mod_quiz_external::view_quiz(0);
|
|
|
374 |
$this->fail('Exception expected due to invalid mod_quiz instance id.');
|
|
|
375 |
} catch (moodle_exception $e) {
|
|
|
376 |
$this->assertEquals('invalidrecord', $e->errorcode);
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
// Test not-enrolled user.
|
|
|
380 |
$usernotenrolled = self::getDataGenerator()->create_user();
|
|
|
381 |
$this->setUser($usernotenrolled);
|
|
|
382 |
try {
|
|
|
383 |
mod_quiz_external::view_quiz($this->quiz->id);
|
|
|
384 |
$this->fail('Exception expected due to not enrolled user.');
|
|
|
385 |
} catch (moodle_exception $e) {
|
|
|
386 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
// Test user with full capabilities.
|
|
|
390 |
$this->setUser($this->student);
|
|
|
391 |
|
|
|
392 |
// Trigger and capture the event.
|
|
|
393 |
$sink = $this->redirectEvents();
|
|
|
394 |
|
|
|
395 |
$result = mod_quiz_external::view_quiz($this->quiz->id);
|
|
|
396 |
$result = external_api::clean_returnvalue(mod_quiz_external::view_quiz_returns(), $result);
|
|
|
397 |
$this->assertTrue($result['status']);
|
|
|
398 |
|
|
|
399 |
$events = $sink->get_events();
|
|
|
400 |
$this->assertCount(1, $events);
|
|
|
401 |
$event = array_shift($events);
|
|
|
402 |
|
|
|
403 |
// Checking that the event contains the expected values.
|
|
|
404 |
$this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
|
|
|
405 |
$this->assertEquals($this->context, $event->get_context());
|
|
|
406 |
$moodlequiz = new \moodle_url('/mod/quiz/view.php', ['id' => $this->cm->id]);
|
|
|
407 |
$this->assertEquals($moodlequiz, $event->get_url());
|
|
|
408 |
$this->assertEventContextNotUsed($event);
|
|
|
409 |
$this->assertNotEmpty($event->get_name());
|
|
|
410 |
|
|
|
411 |
// Test user with no capabilities.
|
|
|
412 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
413 |
assign_capability('mod/quiz:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
|
|
|
414 |
// Empty all the caches that may be affected by this change.
|
|
|
415 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
416 |
\course_modinfo::clear_instance_cache();
|
|
|
417 |
|
|
|
418 |
try {
|
|
|
419 |
mod_quiz_external::view_quiz($this->quiz->id);
|
|
|
420 |
$this->fail('Exception expected due to missing capability.');
|
|
|
421 |
} catch (moodle_exception $e) {
|
|
|
422 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
public function test_get_user_attempts(): void {
|
|
|
428 |
|
|
|
429 |
// Create a quiz with one attempt finished.
|
|
|
430 |
[$quiz, $context, $quizobj, $attempt, $attemptobj] = $this->create_quiz_with_questions(true, true);
|
|
|
431 |
|
|
|
432 |
$this->setUser($this->student);
|
|
|
433 |
$result = mod_quiz_external::get_user_attempts($quiz->id);
|
|
|
434 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
435 |
|
|
|
436 |
$this->assertCount(1, $result['attempts']);
|
|
|
437 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
438 |
$this->assertEquals($quiz->id, $result['attempts'][0]['quiz']);
|
|
|
439 |
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
|
|
440 |
$this->assertEquals(1, $result['attempts'][0]['attempt']);
|
|
|
441 |
$this->assertArrayHasKey('sumgrades', $result['attempts'][0]);
|
|
|
442 |
$this->assertEquals(1.0, $result['attempts'][0]['sumgrades']);
|
|
|
443 |
|
|
|
444 |
// Test filters. Only finished.
|
|
|
445 |
$result = mod_quiz_external::get_user_attempts($quiz->id, 0, 'finished', false);
|
|
|
446 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
447 |
|
|
|
448 |
$this->assertCount(1, $result['attempts']);
|
|
|
449 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
450 |
|
|
|
451 |
// Test filters. All attempts.
|
|
|
452 |
$result = mod_quiz_external::get_user_attempts($quiz->id, 0, 'all', false);
|
|
|
453 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
454 |
|
|
|
455 |
$this->assertCount(1, $result['attempts']);
|
|
|
456 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
457 |
|
|
|
458 |
// Test filters. Unfinished.
|
|
|
459 |
$result = mod_quiz_external::get_user_attempts($quiz->id, 0, 'unfinished', false);
|
|
|
460 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
461 |
|
|
|
462 |
$this->assertCount(0, $result['attempts']);
|
|
|
463 |
|
|
|
464 |
// Start a new attempt, but not finish it.
|
|
|
465 |
$timenow = time();
|
|
|
466 |
$attempt = quiz_create_attempt($quizobj, 2, false, $timenow, false, $this->student->id);
|
|
|
467 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
468 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
469 |
|
|
|
470 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
471 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
472 |
|
|
|
473 |
// Test filters. All attempts.
|
|
|
474 |
$result = mod_quiz_external::get_user_attempts($quiz->id, 0, 'all', false);
|
|
|
475 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
476 |
|
|
|
477 |
$this->assertCount(2, $result['attempts']);
|
|
|
478 |
|
|
|
479 |
// Test filters. Unfinished.
|
|
|
480 |
$result = mod_quiz_external::get_user_attempts($quiz->id, 0, 'unfinished', false);
|
|
|
481 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
482 |
|
|
|
483 |
$this->assertCount(1, $result['attempts']);
|
|
|
484 |
|
|
|
485 |
// Test manager can see user attempts.
|
|
|
486 |
$this->setUser($this->teacher);
|
|
|
487 |
$result = mod_quiz_external::get_user_attempts($quiz->id, $this->student->id);
|
|
|
488 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
489 |
|
|
|
490 |
$this->assertCount(1, $result['attempts']);
|
|
|
491 |
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
|
|
492 |
|
|
|
493 |
$result = mod_quiz_external::get_user_attempts($quiz->id, $this->student->id, 'all');
|
|
|
494 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
495 |
|
|
|
496 |
$this->assertCount(2, $result['attempts']);
|
|
|
497 |
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
|
|
498 |
|
|
|
499 |
// Invalid parameters.
|
|
|
500 |
try {
|
|
|
501 |
mod_quiz_external::get_user_attempts($quiz->id, $this->student->id, 'INVALID_PARAMETER');
|
|
|
502 |
$this->fail('Exception expected due to missing capability.');
|
|
|
503 |
} catch (\invalid_parameter_exception $e) {
|
|
|
504 |
$this->assertEquals('invalidparameter', $e->errorcode);
|
|
|
505 |
}
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
public function test_get_user_attempts_with_extra_grades(): void {
|
|
|
509 |
global $DB;
|
|
|
510 |
|
|
|
511 |
// Create a quiz with one attempt finished.
|
|
|
512 |
[$quiz, , , $attempt, $attemptobj] = $this->create_quiz_with_questions(true, true);
|
|
|
513 |
|
|
|
514 |
// Add some extra grade items.
|
|
|
515 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
516 |
$listeninggrade = $quizgenerator->create_grade_item(['quizid' => $attemptobj->get_quizid(), 'name' => 'Listening']);
|
|
|
517 |
$readinggrade = $quizgenerator->create_grade_item(['quizid' => $attemptobj->get_quizid(), 'name' => 'Reading']);
|
|
|
518 |
$structure = $attemptobj->get_quizobj()->get_structure();
|
|
|
519 |
$structure->update_slot_grade_item($structure->get_slot_by_number(1), $listeninggrade->id);
|
|
|
520 |
$structure->update_slot_grade_item($structure->get_slot_by_number(2), $readinggrade->id);
|
|
|
521 |
|
|
|
522 |
$this->setUser($this->student);
|
|
|
523 |
$result = mod_quiz_external::get_user_attempts($quiz->id);
|
|
|
524 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
525 |
|
|
|
526 |
$this->assertCount(1, $result['attempts']);
|
|
|
527 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
528 |
|
|
|
529 |
// Verify additional grades.
|
|
|
530 |
$this->assertEquals(['name' => 'Listening', 'grade' => 1, 'maxgrade' => 1], $result['attempts'][0]['gradeitemmarks'][0]);
|
|
|
531 |
$this->assertEquals(['name' => 'Reading', 'grade' => 0, 'maxgrade' => 1], $result['attempts'][0]['gradeitemmarks'][1]);
|
|
|
532 |
|
|
|
533 |
// Now change the review options, so marks are not displayed, and check the result.
|
|
|
534 |
$DB->set_field('quiz', 'reviewmarks', 0, ['id' => $quiz->id]);
|
|
|
535 |
$result = mod_quiz_external::get_user_attempts($quiz->id);
|
|
|
536 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
537 |
|
|
|
538 |
$this->assertCount(1, $result['attempts']);
|
|
|
539 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
540 |
$this->assertArrayNotHasKey('gradeitemmarks', $result['attempts'][0]);
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
/**
|
|
|
544 |
* Test get_user_attempts with marks hidden
|
|
|
545 |
*/
|
11 |
efrain |
546 |
public function test_get_user_attempts_with_marks_hidden(): void {
|
1 |
efrain |
547 |
// Create quiz with one attempt finished and hide the mark.
|
|
|
548 |
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(
|
|
|
549 |
true, true, 'deferredfeedback', false,
|
|
|
550 |
['marksduring' => 0, 'marksimmediately' => 0, 'marksopen' => 0, 'marksclosed' => 0]);
|
|
|
551 |
|
|
|
552 |
// Student cannot see the grades.
|
|
|
553 |
$this->setUser($this->student);
|
|
|
554 |
$result = mod_quiz_external::get_user_attempts($quiz->id);
|
|
|
555 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
556 |
|
|
|
557 |
$this->assertCount(1, $result['attempts']);
|
|
|
558 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
559 |
$this->assertEquals($quiz->id, $result['attempts'][0]['quiz']);
|
|
|
560 |
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
|
|
561 |
$this->assertEquals(1, $result['attempts'][0]['attempt']);
|
|
|
562 |
$this->assertArrayHasKey('sumgrades', $result['attempts'][0]);
|
|
|
563 |
$this->assertEquals(null, $result['attempts'][0]['sumgrades']);
|
|
|
564 |
|
|
|
565 |
// Test manager can see user grades.
|
|
|
566 |
$this->setUser($this->teacher);
|
|
|
567 |
$result = mod_quiz_external::get_user_attempts($quiz->id, $this->student->id);
|
|
|
568 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
|
|
569 |
|
|
|
570 |
$this->assertCount(1, $result['attempts']);
|
|
|
571 |
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
|
|
572 |
$this->assertEquals($quiz->id, $result['attempts'][0]['quiz']);
|
|
|
573 |
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
|
|
574 |
$this->assertEquals(1, $result['attempts'][0]['attempt']);
|
|
|
575 |
$this->assertArrayHasKey('sumgrades', $result['attempts'][0]);
|
|
|
576 |
$this->assertEquals(1.0, $result['attempts'][0]['sumgrades']);
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
/**
|
|
|
580 |
* Test get_user_best_grade
|
|
|
581 |
*/
|
11 |
efrain |
582 |
public function test_get_user_best_grade(): void {
|
1 |
efrain |
583 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
584 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
585 |
$questioncat = $questiongenerator->create_question_category();
|
|
|
586 |
|
|
|
587 |
// Create a new quiz.
|
|
|
588 |
$quizapi1 = $quizgenerator->create_instance([
|
|
|
589 |
'name' => 'Test Quiz API 1',
|
|
|
590 |
'course' => $this->course->id,
|
|
|
591 |
'sumgrades' => 1
|
|
|
592 |
]);
|
|
|
593 |
$quizapi2 = $quizgenerator->create_instance([
|
|
|
594 |
'name' => 'Test Quiz API 2',
|
|
|
595 |
'course' => $this->course->id,
|
|
|
596 |
'sumgrades' => 1,
|
|
|
597 |
'marksduring' => 0,
|
|
|
598 |
'marksimmediately' => 0,
|
|
|
599 |
'marksopen' => 0,
|
|
|
600 |
'marksclosed' => 0
|
|
|
601 |
]);
|
|
|
602 |
|
|
|
603 |
// Create a question.
|
|
|
604 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $questioncat->id]);
|
|
|
605 |
|
|
|
606 |
// Add question to the quizzes.
|
|
|
607 |
quiz_add_quiz_question($question->id, $quizapi1);
|
|
|
608 |
quiz_add_quiz_question($question->id, $quizapi2);
|
|
|
609 |
|
|
|
610 |
// Create quiz object.
|
|
|
611 |
$quizapiobj1 = quiz_settings::create($quizapi1->id, $this->student->id);
|
|
|
612 |
$quizapiobj2 = quiz_settings::create($quizapi2->id, $this->student->id);
|
|
|
613 |
|
|
|
614 |
// Set grade to pass.
|
|
|
615 |
$item = \grade_item::fetch([
|
|
|
616 |
'courseid' => $this->course->id,
|
|
|
617 |
'itemtype' => 'mod',
|
|
|
618 |
'itemmodule' => 'quiz',
|
|
|
619 |
'iteminstance' => $quizapi1->id,
|
|
|
620 |
'outcomeid' => null
|
|
|
621 |
]);
|
|
|
622 |
$item->gradepass = 80;
|
|
|
623 |
$item->update();
|
|
|
624 |
|
|
|
625 |
$item = \grade_item::fetch([
|
|
|
626 |
'courseid' => $this->course->id,
|
|
|
627 |
'itemtype' => 'mod',
|
|
|
628 |
'itemmodule' => 'quiz',
|
|
|
629 |
'iteminstance' => $quizapi2->id,
|
|
|
630 |
'outcomeid' => null
|
|
|
631 |
]);
|
|
|
632 |
$item->gradepass = 80;
|
|
|
633 |
$item->update();
|
|
|
634 |
|
|
|
635 |
// Start the passing attempt.
|
|
|
636 |
$quba1 = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizapiobj1->get_context());
|
|
|
637 |
$quba1->set_preferred_behaviour($quizapiobj1->get_quiz()->preferredbehaviour);
|
|
|
638 |
|
|
|
639 |
$quba2 = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizapiobj2->get_context());
|
|
|
640 |
$quba2->set_preferred_behaviour($quizapiobj2->get_quiz()->preferredbehaviour);
|
|
|
641 |
|
|
|
642 |
// Start the testing for quizapi1 that allow the student to view the grade.
|
|
|
643 |
|
|
|
644 |
$this->setUser($this->student);
|
|
|
645 |
$result = mod_quiz_external::get_user_best_grade($quizapi1->id);
|
|
|
646 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_best_grade_returns(), $result);
|
|
|
647 |
|
|
|
648 |
// No grades yet.
|
|
|
649 |
$this->assertFalse($result['hasgrade']);
|
|
|
650 |
$this->assertTrue(!isset($result['grade']));
|
|
|
651 |
|
|
|
652 |
// Start the attempt.
|
|
|
653 |
$timenow = time();
|
|
|
654 |
$attempt = quiz_create_attempt($quizapiobj1, 1, false, $timenow, false, $this->student->id);
|
|
|
655 |
quiz_start_new_attempt($quizapiobj1, $quba1, $attempt, 1, $timenow);
|
|
|
656 |
quiz_attempt_save_started($quizapiobj1, $quba1, $attempt);
|
|
|
657 |
|
|
|
658 |
// Process some responses from the student.
|
|
|
659 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
660 |
$attemptobj->process_submitted_actions($timenow, false, [1 => ['answer' => '3.14']]);
|
|
|
661 |
|
|
|
662 |
// Finish the attempt.
|
|
|
663 |
$attemptobj->process_finish($timenow, false);
|
|
|
664 |
|
|
|
665 |
$result = mod_quiz_external::get_user_best_grade($quizapi1->id);
|
|
|
666 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_best_grade_returns(), $result);
|
|
|
667 |
|
|
|
668 |
// Now I have grades.
|
|
|
669 |
$this->assertTrue($result['hasgrade']);
|
|
|
670 |
$this->assertEquals(100.0, $result['grade']);
|
|
|
671 |
$this->assertEquals(80, $result['gradetopass']);
|
|
|
672 |
|
|
|
673 |
// We should not see other users grades.
|
|
|
674 |
$anotherstudent = self::getDataGenerator()->create_user();
|
|
|
675 |
$this->getDataGenerator()->enrol_user($anotherstudent->id, $this->course->id, $this->studentrole->id, 'manual');
|
|
|
676 |
|
|
|
677 |
try {
|
|
|
678 |
mod_quiz_external::get_user_best_grade($quizapi1->id, $anotherstudent->id);
|
|
|
679 |
$this->fail('Exception expected due to missing capability.');
|
|
|
680 |
} catch (\required_capability_exception $e) {
|
|
|
681 |
$this->assertEquals('nopermissions', $e->errorcode);
|
|
|
682 |
}
|
|
|
683 |
|
|
|
684 |
// Teacher must be able to see student grades.
|
|
|
685 |
$this->setUser($this->teacher);
|
|
|
686 |
|
|
|
687 |
$result = mod_quiz_external::get_user_best_grade($quizapi1->id, $this->student->id);
|
|
|
688 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_best_grade_returns(), $result);
|
|
|
689 |
|
|
|
690 |
$this->assertTrue($result['hasgrade']);
|
|
|
691 |
$this->assertEquals(100.0, $result['grade']);
|
|
|
692 |
$this->assertEquals(80, $result['gradetopass']);
|
|
|
693 |
|
|
|
694 |
// Invalid user.
|
|
|
695 |
try {
|
|
|
696 |
mod_quiz_external::get_user_best_grade($this->quiz->id, -1);
|
|
|
697 |
$this->fail('Exception expected due to missing capability.');
|
|
|
698 |
} catch (\dml_missing_record_exception $e) {
|
|
|
699 |
$this->assertEquals('invaliduser', $e->errorcode);
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
// End the testing for quizapi1 that allow the student to view the grade.
|
|
|
703 |
|
|
|
704 |
// Start the testing for quizapi2 that do not allow the student to view the grade.
|
|
|
705 |
|
|
|
706 |
$this->setUser($this->student);
|
|
|
707 |
$result = mod_quiz_external::get_user_best_grade($quizapi2->id);
|
|
|
708 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_best_grade_returns(), $result);
|
|
|
709 |
|
|
|
710 |
// No grades yet.
|
|
|
711 |
$this->assertFalse($result['hasgrade']);
|
|
|
712 |
$this->assertTrue(!isset($result['grade']));
|
|
|
713 |
|
|
|
714 |
// Start the attempt.
|
|
|
715 |
$timenow = time();
|
|
|
716 |
$attempt = quiz_create_attempt($quizapiobj2, 1, false, $timenow, false, $this->student->id);
|
|
|
717 |
quiz_start_new_attempt($quizapiobj2, $quba2, $attempt, 1, $timenow);
|
|
|
718 |
quiz_attempt_save_started($quizapiobj2, $quba2, $attempt);
|
|
|
719 |
|
|
|
720 |
// Process some responses from the student.
|
|
|
721 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
722 |
$attemptobj->process_submitted_actions($timenow, false, [1 => ['answer' => '3.14']]);
|
|
|
723 |
|
|
|
724 |
// Finish the attempt.
|
|
|
725 |
$attemptobj->process_finish($timenow, false);
|
|
|
726 |
|
|
|
727 |
$result = mod_quiz_external::get_user_best_grade($quizapi2->id);
|
|
|
728 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_best_grade_returns(), $result);
|
|
|
729 |
|
|
|
730 |
// Now I have grades but I will not be allowed to see it.
|
|
|
731 |
$this->assertFalse($result['hasgrade']);
|
|
|
732 |
$this->assertTrue(!isset($result['grade']));
|
|
|
733 |
|
|
|
734 |
// Teacher must be able to see student grades.
|
|
|
735 |
$this->setUser($this->teacher);
|
|
|
736 |
|
|
|
737 |
$result = mod_quiz_external::get_user_best_grade($quizapi2->id, $this->student->id);
|
|
|
738 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_best_grade_returns(), $result);
|
|
|
739 |
|
|
|
740 |
$this->assertTrue($result['hasgrade']);
|
|
|
741 |
$this->assertEquals(100.0, $result['grade']);
|
|
|
742 |
|
|
|
743 |
// End the testing for quizapi2 that do not allow the student to view the grade.
|
|
|
744 |
|
|
|
745 |
}
|
|
|
746 |
/**
|
|
|
747 |
* Test get_combined_review_options.
|
|
|
748 |
* This is a basic test, this is already tested in display_options_testcase.
|
|
|
749 |
*/
|
11 |
efrain |
750 |
public function test_get_combined_review_options(): void {
|
1 |
efrain |
751 |
global $DB;
|
|
|
752 |
|
|
|
753 |
// Create a new quiz with attempts.
|
|
|
754 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
755 |
$data = ['course' => $this->course->id,
|
|
|
756 |
'sumgrades' => 1];
|
|
|
757 |
$quiz = $quizgenerator->create_instance($data);
|
|
|
758 |
|
|
|
759 |
// Create a couple of questions.
|
|
|
760 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
761 |
|
|
|
762 |
$cat = $questiongenerator->create_question_category();
|
|
|
763 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
764 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
765 |
|
|
|
766 |
$quizobj = quiz_settings::create($quiz->id, $this->student->id);
|
|
|
767 |
|
|
|
768 |
// Set grade to pass.
|
|
|
769 |
$item = \grade_item::fetch(['courseid' => $this->course->id, 'itemtype' => 'mod',
|
|
|
770 |
'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null]);
|
|
|
771 |
$item->gradepass = 80;
|
|
|
772 |
$item->update();
|
|
|
773 |
|
|
|
774 |
// Start the passing attempt.
|
|
|
775 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
776 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
777 |
|
|
|
778 |
$timenow = time();
|
|
|
779 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $this->student->id);
|
|
|
780 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
781 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
782 |
|
|
|
783 |
$this->setUser($this->student);
|
|
|
784 |
|
|
|
785 |
$result = mod_quiz_external::get_combined_review_options($quiz->id);
|
|
|
786 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_combined_review_options_returns(), $result);
|
|
|
787 |
|
|
|
788 |
// Expected values.
|
|
|
789 |
$expected = [
|
|
|
790 |
"someoptions" => [
|
|
|
791 |
["name" => "feedback", "value" => 1],
|
|
|
792 |
["name" => "generalfeedback", "value" => 1],
|
|
|
793 |
["name" => "rightanswer", "value" => 1],
|
|
|
794 |
["name" => "overallfeedback", "value" => 0],
|
|
|
795 |
["name" => "marks", "value" => 2],
|
|
|
796 |
],
|
|
|
797 |
"alloptions" => [
|
|
|
798 |
["name" => "feedback", "value" => 1],
|
|
|
799 |
["name" => "generalfeedback", "value" => 1],
|
|
|
800 |
["name" => "rightanswer", "value" => 1],
|
|
|
801 |
["name" => "overallfeedback", "value" => 0],
|
|
|
802 |
["name" => "marks", "value" => 2],
|
|
|
803 |
],
|
|
|
804 |
"warnings" => [],
|
|
|
805 |
];
|
|
|
806 |
|
|
|
807 |
$this->assertEquals($expected, $result);
|
|
|
808 |
|
|
|
809 |
// Now, finish the attempt.
|
|
|
810 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
811 |
$attemptobj->process_finish($timenow, false);
|
|
|
812 |
|
|
|
813 |
$expected = [
|
|
|
814 |
"someoptions" => [
|
|
|
815 |
["name" => "feedback", "value" => 1],
|
|
|
816 |
["name" => "generalfeedback", "value" => 1],
|
|
|
817 |
["name" => "rightanswer", "value" => 1],
|
|
|
818 |
["name" => "overallfeedback", "value" => 1],
|
|
|
819 |
["name" => "marks", "value" => 2],
|
|
|
820 |
],
|
|
|
821 |
"alloptions" => [
|
|
|
822 |
["name" => "feedback", "value" => 1],
|
|
|
823 |
["name" => "generalfeedback", "value" => 1],
|
|
|
824 |
["name" => "rightanswer", "value" => 1],
|
|
|
825 |
["name" => "overallfeedback", "value" => 1],
|
|
|
826 |
["name" => "marks", "value" => 2],
|
|
|
827 |
],
|
|
|
828 |
"warnings" => [],
|
|
|
829 |
];
|
|
|
830 |
|
|
|
831 |
// We should see now the overall feedback.
|
|
|
832 |
$result = mod_quiz_external::get_combined_review_options($quiz->id);
|
|
|
833 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_combined_review_options_returns(), $result);
|
|
|
834 |
$this->assertEquals($expected, $result);
|
|
|
835 |
|
|
|
836 |
// Start a new attempt, but not finish it.
|
|
|
837 |
$timenow = time();
|
|
|
838 |
$attempt = quiz_create_attempt($quizobj, 2, false, $timenow, false, $this->student->id);
|
|
|
839 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
840 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
841 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
842 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
843 |
|
|
|
844 |
$expected = [
|
|
|
845 |
"someoptions" => [
|
|
|
846 |
["name" => "feedback", "value" => 1],
|
|
|
847 |
["name" => "generalfeedback", "value" => 1],
|
|
|
848 |
["name" => "rightanswer", "value" => 1],
|
|
|
849 |
["name" => "overallfeedback", "value" => 1],
|
|
|
850 |
["name" => "marks", "value" => 2],
|
|
|
851 |
],
|
|
|
852 |
"alloptions" => [
|
|
|
853 |
["name" => "feedback", "value" => 1],
|
|
|
854 |
["name" => "generalfeedback", "value" => 1],
|
|
|
855 |
["name" => "rightanswer", "value" => 1],
|
|
|
856 |
["name" => "overallfeedback", "value" => 0],
|
|
|
857 |
["name" => "marks", "value" => 2],
|
|
|
858 |
],
|
|
|
859 |
"warnings" => [],
|
|
|
860 |
];
|
|
|
861 |
|
|
|
862 |
$result = mod_quiz_external::get_combined_review_options($quiz->id);
|
|
|
863 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_combined_review_options_returns(), $result);
|
|
|
864 |
$this->assertEquals($expected, $result);
|
|
|
865 |
|
|
|
866 |
// Teacher, for see student options.
|
|
|
867 |
$this->setUser($this->teacher);
|
|
|
868 |
|
|
|
869 |
$result = mod_quiz_external::get_combined_review_options($quiz->id, $this->student->id);
|
|
|
870 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_combined_review_options_returns(), $result);
|
|
|
871 |
|
|
|
872 |
$this->assertEquals($expected, $result);
|
|
|
873 |
|
|
|
874 |
// Invalid user.
|
|
|
875 |
try {
|
|
|
876 |
mod_quiz_external::get_combined_review_options($quiz->id, -1);
|
|
|
877 |
$this->fail('Exception expected due to missing capability.');
|
|
|
878 |
} catch (\dml_missing_record_exception $e) {
|
|
|
879 |
$this->assertEquals('invaliduser', $e->errorcode);
|
|
|
880 |
}
|
|
|
881 |
}
|
|
|
882 |
|
|
|
883 |
/**
|
|
|
884 |
* Test get_combined_review_options when the user has an override.
|
|
|
885 |
*
|
|
|
886 |
* @covers ::get_combined_review_options
|
|
|
887 |
* @covers ::get_combined_review_options_parameters
|
|
|
888 |
* @covers ::get_combined_review_options_returns
|
|
|
889 |
*/
|
|
|
890 |
public function test_get_combined_review_options_with_overrides(): void {
|
|
|
891 |
global $DB;
|
|
|
892 |
|
|
|
893 |
// Create a closed quiz with review marks only when quiz is closed.
|
|
|
894 |
list($quiz, $context, $quizobj) = $this->create_quiz_with_questions(true, true, 'deferredfeedback', false, [
|
|
|
895 |
'timeclose' => time() - HOURSECS,
|
|
|
896 |
'marksduring' => 0,
|
|
|
897 |
'maxmarksduring' => 0,
|
|
|
898 |
'marksimmediately' => 0,
|
|
|
899 |
'maxmarksimmediately' => 0,
|
|
|
900 |
'marksopen' => 0,
|
|
|
901 |
'maxmarksopen' => 0,
|
|
|
902 |
'marksclosed' => 1,
|
|
|
903 |
'maxmarksclosed' => 1,
|
|
|
904 |
]);
|
|
|
905 |
|
|
|
906 |
// Check that the student can see the marks because the quiz is closed.
|
|
|
907 |
$this->setUser($this->student);
|
|
|
908 |
|
|
|
909 |
$expected = [
|
|
|
910 |
"someoptions" => [
|
|
|
911 |
["name" => "feedback", "value" => 1],
|
|
|
912 |
["name" => "generalfeedback", "value" => 1],
|
|
|
913 |
["name" => "rightanswer", "value" => 1],
|
|
|
914 |
["name" => "overallfeedback", "value" => 1],
|
|
|
915 |
["name" => "marks", "value" => 2],
|
|
|
916 |
],
|
|
|
917 |
"alloptions" => [
|
|
|
918 |
["name" => "feedback", "value" => 1],
|
|
|
919 |
["name" => "generalfeedback", "value" => 1],
|
|
|
920 |
["name" => "rightanswer", "value" => 1],
|
|
|
921 |
["name" => "overallfeedback", "value" => 1],
|
|
|
922 |
["name" => "marks", "value" => 2],
|
|
|
923 |
],
|
|
|
924 |
"warnings" => [],
|
|
|
925 |
];
|
|
|
926 |
|
|
|
927 |
$result = mod_quiz_external::get_combined_review_options($quiz->id);
|
|
|
928 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_combined_review_options_returns(), $result);
|
|
|
929 |
|
|
|
930 |
$this->assertEquals($expected, $result);
|
|
|
931 |
|
|
|
932 |
// Add an override for the student to increase the close time.
|
|
|
933 |
$DB->insert_record('quiz_overrides', [
|
|
|
934 |
'quiz' => $quiz->id,
|
|
|
935 |
'userid' => $this->student->id,
|
|
|
936 |
'timeclose' => time() + HOURSECS,
|
|
|
937 |
]);
|
|
|
938 |
|
|
|
939 |
// Check that now the marks option has changed.
|
|
|
940 |
$expected = [
|
|
|
941 |
"someoptions" => [
|
|
|
942 |
["name" => "feedback", "value" => 1],
|
|
|
943 |
["name" => "generalfeedback", "value" => 1],
|
|
|
944 |
["name" => "rightanswer", "value" => 1],
|
|
|
945 |
["name" => "overallfeedback", "value" => 1],
|
|
|
946 |
["name" => "marks", "value" => 0],
|
|
|
947 |
],
|
|
|
948 |
"alloptions" => [
|
|
|
949 |
["name" => "feedback", "value" => 1],
|
|
|
950 |
["name" => "generalfeedback", "value" => 1],
|
|
|
951 |
["name" => "rightanswer", "value" => 1],
|
|
|
952 |
["name" => "overallfeedback", "value" => 1],
|
|
|
953 |
["name" => "marks", "value" => 0],
|
|
|
954 |
],
|
|
|
955 |
"warnings" => [],
|
|
|
956 |
];
|
|
|
957 |
|
|
|
958 |
$result = mod_quiz_external::get_combined_review_options($quiz->id);
|
|
|
959 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_combined_review_options_returns(), $result);
|
|
|
960 |
|
|
|
961 |
$this->assertEquals($expected, $result);
|
|
|
962 |
}
|
|
|
963 |
|
|
|
964 |
/**
|
|
|
965 |
* Test start_attempt
|
|
|
966 |
*/
|
11 |
efrain |
967 |
public function test_start_attempt(): void {
|
1 |
efrain |
968 |
global $DB;
|
|
|
969 |
|
|
|
970 |
// Create a new quiz with questions.
|
|
|
971 |
list($quiz, $context, $quizobj) = $this->create_quiz_with_questions();
|
|
|
972 |
|
|
|
973 |
$this->setUser($this->student);
|
|
|
974 |
|
|
|
975 |
// Try to open attempt in closed quiz.
|
|
|
976 |
$quiz->timeopen = time() - WEEKSECS;
|
|
|
977 |
$quiz->timeclose = time() - DAYSECS;
|
|
|
978 |
$DB->update_record('quiz', $quiz);
|
|
|
979 |
$result = mod_quiz_external::start_attempt($quiz->id);
|
|
|
980 |
$result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result);
|
|
|
981 |
|
|
|
982 |
$this->assertEquals([], $result['attempt']);
|
|
|
983 |
$this->assertCount(1, $result['warnings']);
|
|
|
984 |
|
|
|
985 |
// Now with a password.
|
|
|
986 |
$quiz->timeopen = 0;
|
|
|
987 |
$quiz->timeclose = 0;
|
|
|
988 |
$quiz->password = 'abc';
|
|
|
989 |
$DB->update_record('quiz', $quiz);
|
|
|
990 |
|
|
|
991 |
try {
|
|
|
992 |
mod_quiz_external::start_attempt($quiz->id, [["name" => "quizpassword", "value" => 'bad']]);
|
|
|
993 |
$this->fail('Exception expected due to invalid passwod.');
|
|
|
994 |
} catch (moodle_exception $e) {
|
|
|
995 |
$this->assertEquals(get_string('passworderror', 'quizaccess_password'), $e->errorcode);
|
|
|
996 |
}
|
|
|
997 |
|
|
|
998 |
// Now, try everything correct.
|
|
|
999 |
$result = mod_quiz_external::start_attempt($quiz->id, [["name" => "quizpassword", "value" => 'abc']]);
|
|
|
1000 |
$result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result);
|
|
|
1001 |
|
|
|
1002 |
$this->assertEquals(1, $result['attempt']['attempt']);
|
|
|
1003 |
$this->assertEquals($this->student->id, $result['attempt']['userid']);
|
|
|
1004 |
$this->assertEquals($quiz->id, $result['attempt']['quiz']);
|
|
|
1005 |
$this->assertCount(0, $result['warnings']);
|
|
|
1006 |
$attemptid = $result['attempt']['id'];
|
|
|
1007 |
|
|
|
1008 |
// We are good, try to start a new attempt now.
|
|
|
1009 |
|
|
|
1010 |
try {
|
|
|
1011 |
mod_quiz_external::start_attempt($quiz->id, [["name" => "quizpassword", "value" => 'abc']]);
|
|
|
1012 |
$this->fail('Exception expected due to attempt not finished.');
|
|
|
1013 |
} catch (moodle_exception $e) {
|
|
|
1014 |
$this->assertEquals('attemptstillinprogress', $e->errorcode);
|
|
|
1015 |
}
|
|
|
1016 |
|
|
|
1017 |
// Finish the started attempt.
|
|
|
1018 |
|
|
|
1019 |
// Process some responses from the student.
|
|
|
1020 |
$timenow = time();
|
|
|
1021 |
$attemptobj = quiz_attempt::create($attemptid);
|
|
|
1022 |
$tosubmit = [1 => ['answer' => '3.14']];
|
|
|
1023 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
1024 |
|
|
|
1025 |
// Finish the attempt.
|
|
|
1026 |
$attemptobj = quiz_attempt::create($attemptid);
|
|
|
1027 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
1028 |
$attemptobj->process_finish($timenow, false);
|
|
|
1029 |
|
|
|
1030 |
// We should be able to start a new attempt.
|
|
|
1031 |
$result = mod_quiz_external::start_attempt($quiz->id, [["name" => "quizpassword", "value" => 'abc']]);
|
|
|
1032 |
$result = external_api::clean_returnvalue(mod_quiz_external::start_attempt_returns(), $result);
|
|
|
1033 |
|
|
|
1034 |
$this->assertEquals(2, $result['attempt']['attempt']);
|
|
|
1035 |
$this->assertEquals($this->student->id, $result['attempt']['userid']);
|
|
|
1036 |
$this->assertEquals($quiz->id, $result['attempt']['quiz']);
|
|
|
1037 |
$this->assertCount(0, $result['warnings']);
|
|
|
1038 |
|
|
|
1039 |
// Test user with no capabilities.
|
|
|
1040 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
1041 |
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $this->studentrole->id, $context->id);
|
|
|
1042 |
// Empty all the caches that may be affected by this change.
|
|
|
1043 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
1044 |
\course_modinfo::clear_instance_cache();
|
|
|
1045 |
|
|
|
1046 |
try {
|
|
|
1047 |
mod_quiz_external::start_attempt($quiz->id);
|
|
|
1048 |
$this->fail('Exception expected due to missing capability.');
|
|
|
1049 |
} catch (\required_capability_exception $e) {
|
|
|
1050 |
$this->assertEquals('nopermissions', $e->errorcode);
|
|
|
1051 |
}
|
|
|
1052 |
|
|
|
1053 |
}
|
|
|
1054 |
|
|
|
1055 |
/**
|
|
|
1056 |
* Test validate_attempt
|
|
|
1057 |
*/
|
11 |
efrain |
1058 |
public function test_validate_attempt(): void {
|
1 |
efrain |
1059 |
global $DB;
|
|
|
1060 |
|
|
|
1061 |
// Create a new quiz with one attempt started.
|
|
|
1062 |
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true);
|
|
|
1063 |
|
|
|
1064 |
$this->setUser($this->student);
|
|
|
1065 |
|
|
|
1066 |
// Invalid attempt.
|
|
|
1067 |
try {
|
|
|
1068 |
$params = ['attemptid' => -1, 'page' => 0];
|
|
|
1069 |
testable_mod_quiz_external::validate_attempt($params);
|
|
|
1070 |
$this->fail('Exception expected due to invalid attempt id.');
|
|
|
1071 |
} catch (\dml_missing_record_exception $e) {
|
|
|
1072 |
$this->assertEquals('invalidrecord', $e->errorcode);
|
|
|
1073 |
}
|
|
|
1074 |
|
|
|
1075 |
// Test OK case.
|
|
|
1076 |
$params = ['attemptid' => $attempt->id, 'page' => 0];
|
|
|
1077 |
$result = testable_mod_quiz_external::validate_attempt($params);
|
|
|
1078 |
$this->assertEquals($attempt->id, $result[0]->get_attempt()->id);
|
|
|
1079 |
$this->assertEquals([], $result[1]);
|
|
|
1080 |
|
|
|
1081 |
// Test with preflight data.
|
|
|
1082 |
$quiz->password = 'abc';
|
|
|
1083 |
$DB->update_record('quiz', $quiz);
|
|
|
1084 |
|
|
|
1085 |
try {
|
|
|
1086 |
$params = ['attemptid' => $attempt->id, 'page' => 0,
|
|
|
1087 |
'preflightdata' => [["name" => "quizpassword", "value" => 'bad']]];
|
|
|
1088 |
testable_mod_quiz_external::validate_attempt($params);
|
|
|
1089 |
$this->fail('Exception expected due to invalid passwod.');
|
|
|
1090 |
} catch (moodle_exception $e) {
|
|
|
1091 |
$this->assertEquals(get_string('passworderror', 'quizaccess_password'), $e->errorcode);
|
|
|
1092 |
}
|
|
|
1093 |
|
|
|
1094 |
// Now, try everything correct.
|
|
|
1095 |
$params['preflightdata'][0]['value'] = 'abc';
|
|
|
1096 |
$result = testable_mod_quiz_external::validate_attempt($params);
|
|
|
1097 |
$this->assertEquals($attempt->id, $result[0]->get_attempt()->id);
|
|
|
1098 |
$this->assertEquals([], $result[1]);
|
|
|
1099 |
|
|
|
1100 |
// Page out of range.
|
|
|
1101 |
$DB->update_record('quiz', $quiz);
|
|
|
1102 |
$params['page'] = 4;
|
|
|
1103 |
try {
|
|
|
1104 |
testable_mod_quiz_external::validate_attempt($params);
|
|
|
1105 |
$this->fail('Exception expected due to page out of range.');
|
|
|
1106 |
} catch (moodle_exception $e) {
|
|
|
1107 |
$this->assertEquals('Invalid page number', $e->errorcode);
|
|
|
1108 |
}
|
|
|
1109 |
|
|
|
1110 |
$params['page'] = 0;
|
|
|
1111 |
// Try to open attempt in closed quiz.
|
|
|
1112 |
$quiz->timeopen = time() - WEEKSECS;
|
|
|
1113 |
$quiz->timeclose = time() - DAYSECS;
|
|
|
1114 |
$DB->update_record('quiz', $quiz);
|
|
|
1115 |
|
|
|
1116 |
// This should work, ommit access rules.
|
|
|
1117 |
testable_mod_quiz_external::validate_attempt($params, false);
|
|
|
1118 |
|
|
|
1119 |
// Get a generic error because prior to checking the dates the attempt is closed.
|
|
|
1120 |
try {
|
|
|
1121 |
testable_mod_quiz_external::validate_attempt($params);
|
|
|
1122 |
$this->fail('Exception expected due to passed dates.');
|
|
|
1123 |
} catch (moodle_exception $e) {
|
|
|
1124 |
$this->assertEquals('attempterror', $e->errorcode);
|
|
|
1125 |
}
|
|
|
1126 |
|
|
|
1127 |
// Finish the attempt.
|
|
|
1128 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
1129 |
$attemptobj->process_finish(time(), false);
|
|
|
1130 |
|
|
|
1131 |
try {
|
|
|
1132 |
testable_mod_quiz_external::validate_attempt($params, false);
|
|
|
1133 |
$this->fail('Exception expected due to attempt finished.');
|
|
|
1134 |
} catch (moodle_exception $e) {
|
|
|
1135 |
$this->assertEquals('attemptalreadyclosed', $e->errorcode);
|
|
|
1136 |
}
|
|
|
1137 |
|
|
|
1138 |
// Test user with no capabilities.
|
|
|
1139 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
1140 |
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $this->studentrole->id, $context->id);
|
|
|
1141 |
// Empty all the caches that may be affected by this change.
|
|
|
1142 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
1143 |
\course_modinfo::clear_instance_cache();
|
|
|
1144 |
|
|
|
1145 |
try {
|
|
|
1146 |
testable_mod_quiz_external::validate_attempt($params);
|
|
|
1147 |
$this->fail('Exception expected due to missing permissions.');
|
|
|
1148 |
} catch (\required_capability_exception $e) {
|
|
|
1149 |
$this->assertEquals('nopermissions', $e->errorcode);
|
|
|
1150 |
}
|
|
|
1151 |
|
|
|
1152 |
// Now try with a different user.
|
|
|
1153 |
$this->setUser($this->teacher);
|
|
|
1154 |
|
|
|
1155 |
$params['page'] = 0;
|
|
|
1156 |
try {
|
|
|
1157 |
testable_mod_quiz_external::validate_attempt($params);
|
|
|
1158 |
$this->fail('Exception expected due to not your attempt.');
|
|
|
1159 |
} catch (moodle_exception $e) {
|
|
|
1160 |
$this->assertEquals('notyourattempt', $e->errorcode);
|
|
|
1161 |
}
|
|
|
1162 |
}
|
|
|
1163 |
|
|
|
1164 |
/**
|
|
|
1165 |
* Test get_attempt_data
|
|
|
1166 |
*/
|
11 |
efrain |
1167 |
public function test_get_attempt_data(): void {
|
1 |
efrain |
1168 |
global $DB;
|
|
|
1169 |
|
|
|
1170 |
$timenow = time();
|
|
|
1171 |
// Create a new quiz with one attempt started.
|
|
|
1172 |
[$quiz, , $quizobj, $attempt] = $this->create_quiz_with_questions(true);
|
|
|
1173 |
/** @var structure $structure */
|
|
|
1174 |
$structure = $quizobj->get_structure();
|
|
|
1175 |
$structure->update_slot_display_number($structure->get_slot_id_for_slot(1), '1.a');
|
|
|
1176 |
|
|
|
1177 |
// Set correctness mask so questions state can be fetched only after finishing the attempt.
|
|
|
1178 |
$DB->set_field('quiz', 'reviewcorrectness', display_options::IMMEDIATELY_AFTER, ['id' => $quiz->id]);
|
|
|
1179 |
|
|
|
1180 |
// Having changed some settings, recreate the objects.
|
|
|
1181 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
1182 |
$quizobj = $attemptobj->get_quizobj();
|
|
|
1183 |
$quizobj->preload_questions();
|
|
|
1184 |
$quizobj->load_questions();
|
|
|
1185 |
$questions = $quizobj->get_questions();
|
|
|
1186 |
|
|
|
1187 |
$this->setUser($this->student);
|
|
|
1188 |
|
|
|
1189 |
// We receive one question per page.
|
|
|
1190 |
$result = mod_quiz_external::get_attempt_data($attempt->id, 0);
|
|
|
1191 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
|
|
|
1192 |
|
|
|
1193 |
$this->assertEquals($attempt, (object) $result['attempt']);
|
|
|
1194 |
$this->assertEquals(1, $result['nextpage']);
|
|
|
1195 |
$this->assertCount(0, $result['messages']);
|
|
|
1196 |
$this->assertCount(1, $result['questions']);
|
|
|
1197 |
$this->assertEquals(1, $result['questions'][0]['slot']);
|
|
|
1198 |
$this->assertArrayNotHasKey('number', $result['questions'][0]);
|
|
|
1199 |
$this->assertEquals('1.a', $result['questions'][0]['questionnumber']);
|
|
|
1200 |
$this->assertEquals('numerical', $result['questions'][0]['type']);
|
|
|
1201 |
$this->assertEquals('notyetanswered', $result['questions'][0]['stateclass']);
|
|
|
1202 |
$this->assertArrayNotHasKey('state', $result['questions'][0]); // We don't receive the state yet.
|
|
|
1203 |
$this->assertEquals('notyetanswered', $result['questions'][0]['stateclass']);
|
|
|
1204 |
$this->assertEquals(get_string('notyetanswered', 'question'), $result['questions'][0]['status']);
|
|
|
1205 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1206 |
$this->assertEquals(0, $result['questions'][0]['page']);
|
|
|
1207 |
$this->assertEmpty($result['questions'][0]['mark']);
|
|
|
1208 |
$this->assertEquals(1, $result['questions'][0]['maxmark']);
|
|
|
1209 |
$this->assertEquals(1, $result['questions'][0]['sequencecheck']);
|
|
|
1210 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1211 |
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
|
|
|
1212 |
|
|
|
1213 |
// Now try the last page.
|
|
|
1214 |
$result = mod_quiz_external::get_attempt_data($attempt->id, 1);
|
|
|
1215 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
|
|
|
1216 |
|
|
|
1217 |
$this->assertEquals($attempt, (object) $result['attempt']);
|
|
|
1218 |
$this->assertEquals(-1, $result['nextpage']);
|
|
|
1219 |
$this->assertCount(0, $result['messages']);
|
|
|
1220 |
$this->assertCount(1, $result['questions']);
|
|
|
1221 |
$this->assertEquals(2, $result['questions'][0]['slot']);
|
|
|
1222 |
$this->assertEquals(2, $result['questions'][0]['questionnumber']);
|
|
|
1223 |
$this->assertEquals(2, $result['questions'][0]['number']);
|
|
|
1224 |
$this->assertEquals('numerical', $result['questions'][0]['type']);
|
|
|
1225 |
$this->assertEquals('notyetanswered', $result['questions'][0]['stateclass']);
|
|
|
1226 |
$this->assertArrayNotHasKey('state', $result['questions'][0]); // We don't receive the state yet.
|
|
|
1227 |
$this->assertEquals(get_string('notyetanswered', 'question'), $result['questions'][0]['status']);
|
|
|
1228 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1229 |
$this->assertEquals(1, $result['questions'][0]['page']);
|
|
|
1230 |
$this->assertEquals(1, $result['questions'][0]['sequencecheck']);
|
|
|
1231 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1232 |
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
|
|
|
1233 |
|
|
|
1234 |
// Finish previous attempt.
|
|
|
1235 |
$attemptobj->process_finish(time(), false);
|
|
|
1236 |
|
|
|
1237 |
// Now we should receive the question state.
|
|
|
1238 |
$result = mod_quiz_external::get_attempt_review($attempt->id, 1);
|
|
|
1239 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_review_returns(), $result);
|
|
|
1240 |
$this->assertEquals('notanswered', $result['questions'][0]['stateclass']);
|
|
|
1241 |
$this->assertEquals('gaveup', $result['questions'][0]['state']);
|
|
|
1242 |
|
|
|
1243 |
// Change setting and expect two pages.
|
|
|
1244 |
$quiz->questionsperpage = 4;
|
|
|
1245 |
$DB->update_record('quiz', $quiz);
|
|
|
1246 |
quiz_repaginate_questions($quiz->id, $quiz->questionsperpage);
|
|
|
1247 |
|
|
|
1248 |
// Start with new attempt with the new layout.
|
|
|
1249 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
1250 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
1251 |
|
|
|
1252 |
$timenow = time();
|
|
|
1253 |
$attempt = quiz_create_attempt($quizobj, 2, false, $timenow, false, $this->student->id);
|
|
|
1254 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
1255 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
1256 |
|
|
|
1257 |
// We receive two questions per page.
|
|
|
1258 |
$result = mod_quiz_external::get_attempt_data($attempt->id, 0);
|
|
|
1259 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
|
|
|
1260 |
$this->assertCount(2, $result['questions']);
|
|
|
1261 |
$this->assertEquals(-1, $result['nextpage']);
|
|
|
1262 |
|
|
|
1263 |
// Check questions looks good.
|
|
|
1264 |
$found = 0;
|
|
|
1265 |
foreach ($questions as $question) {
|
|
|
1266 |
foreach ($result['questions'] as $rquestion) {
|
|
|
1267 |
if ($rquestion['slot'] == $question->slot) {
|
|
|
1268 |
$this->assertTrue(strpos($rquestion['html'], "qid=$question->id") !== false);
|
|
|
1269 |
$found++;
|
|
|
1270 |
}
|
|
|
1271 |
}
|
|
|
1272 |
}
|
|
|
1273 |
$this->assertEquals(2, $found);
|
|
|
1274 |
|
|
|
1275 |
}
|
|
|
1276 |
|
|
|
1277 |
/**
|
|
|
1278 |
* Test get_attempt_data with blocked questions.
|
|
|
1279 |
* @since 3.2
|
|
|
1280 |
*/
|
11 |
efrain |
1281 |
public function test_get_attempt_data_with_blocked_questions(): void {
|
1 |
efrain |
1282 |
global $DB;
|
|
|
1283 |
|
|
|
1284 |
// Create a new quiz with one attempt started and using immediatefeedback.
|
|
|
1285 |
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(
|
|
|
1286 |
true, false, 'immediatefeedback');
|
|
|
1287 |
|
|
|
1288 |
$quizobj = $attemptobj->get_quizobj();
|
|
|
1289 |
|
|
|
1290 |
// Make second question blocked by the first one.
|
|
|
1291 |
$structure = $quizobj->get_structure();
|
|
|
1292 |
$slots = $structure->get_slots();
|
|
|
1293 |
$structure->update_question_dependency(end($slots)->id, true);
|
|
|
1294 |
|
|
|
1295 |
$quizobj->preload_questions();
|
|
|
1296 |
$quizobj->load_questions();
|
|
|
1297 |
$questions = $quizobj->get_questions();
|
|
|
1298 |
|
|
|
1299 |
$this->setUser($this->student);
|
|
|
1300 |
|
|
|
1301 |
// We receive one question per page.
|
|
|
1302 |
$result = mod_quiz_external::get_attempt_data($attempt->id, 0);
|
|
|
1303 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
|
|
|
1304 |
|
|
|
1305 |
$this->assertEquals($attempt, (object) $result['attempt']);
|
|
|
1306 |
$this->assertCount(1, $result['questions']);
|
|
|
1307 |
$this->assertEquals(1, $result['questions'][0]['slot']);
|
|
|
1308 |
$this->assertEquals(1, $result['questions'][0]['number']);
|
|
|
1309 |
$this->assertEquals(false, $result['questions'][0]['blockedbyprevious']);
|
|
|
1310 |
|
|
|
1311 |
// Now try the last page.
|
|
|
1312 |
$result = mod_quiz_external::get_attempt_data($attempt->id, 1);
|
|
|
1313 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
|
|
|
1314 |
|
|
|
1315 |
$this->assertEquals($attempt, (object) $result['attempt']);
|
|
|
1316 |
$this->assertCount(1, $result['questions']);
|
|
|
1317 |
$this->assertEquals(2, $result['questions'][0]['slot']);
|
|
|
1318 |
$this->assertEquals(2, $result['questions'][0]['number']);
|
|
|
1319 |
$this->assertEquals(true, $result['questions'][0]['blockedbyprevious']);
|
|
|
1320 |
}
|
|
|
1321 |
|
|
|
1322 |
/**
|
|
|
1323 |
* Test get_attempt_summary
|
|
|
1324 |
*/
|
11 |
efrain |
1325 |
public function test_get_attempt_summary(): void {
|
1 |
efrain |
1326 |
|
|
|
1327 |
$timenow = time();
|
|
|
1328 |
// Create a new quiz with one attempt started.
|
|
|
1329 |
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true);
|
|
|
1330 |
|
|
|
1331 |
$this->setUser($this->student);
|
|
|
1332 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1333 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1334 |
|
|
|
1335 |
// Check the state, flagged and mark data is correct.
|
|
|
1336 |
$this->assertEquals('todo', $result['questions'][0]['state']);
|
|
|
1337 |
$this->assertEquals('notyetanswered', $result['questions'][0]['stateclass']);
|
|
|
1338 |
$this->assertEquals('todo', $result['questions'][1]['state']);
|
|
|
1339 |
$this->assertEquals('notyetanswered', $result['questions'][1]['stateclass']);
|
|
|
1340 |
$this->assertEquals(1, $result['questions'][0]['number']);
|
|
|
1341 |
$this->assertEquals(2, $result['questions'][1]['number']);
|
|
|
1342 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1343 |
$this->assertFalse($result['questions'][1]['flagged']);
|
|
|
1344 |
$this->assertEmpty($result['questions'][0]['mark']);
|
|
|
1345 |
$this->assertEmpty($result['questions'][1]['mark']);
|
|
|
1346 |
$this->assertEquals(1, $result['questions'][0]['sequencecheck']);
|
|
|
1347 |
$this->assertEquals(1, $result['questions'][1]['sequencecheck']);
|
|
|
1348 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1349 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][1]['lastactiontime']);
|
|
|
1350 |
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
|
|
|
1351 |
$this->assertEquals(false, $result['questions'][1]['hasautosavedstep']);
|
|
|
1352 |
|
|
|
1353 |
// Check question options.
|
|
|
1354 |
$this->assertNotEmpty(5, $result['questions'][0]['settings']);
|
|
|
1355 |
// Check at least some settings returned.
|
|
|
1356 |
$this->assertCount(4, (array) json_decode($result['questions'][0]['settings']));
|
|
|
1357 |
$this->assertEquals(2, $result['totalunanswered']); // All questions are unanswered.
|
|
|
1358 |
|
|
|
1359 |
// Submit a response for the first question.
|
|
|
1360 |
$tosubmit = [1 => ['answer' => '3.14']];
|
|
|
1361 |
$attemptobj->process_submitted_actions(time(), false, $tosubmit);
|
|
|
1362 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1363 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1364 |
|
|
|
1365 |
// Check it's marked as completed only the first one.
|
|
|
1366 |
$this->assertEquals('complete', $result['questions'][0]['state']);
|
|
|
1367 |
$this->assertEquals('answersaved', $result['questions'][0]['stateclass']);
|
|
|
1368 |
$this->assertEquals('todo', $result['questions'][1]['state']);
|
|
|
1369 |
$this->assertEquals('notyetanswered', $result['questions'][1]['stateclass']);
|
|
|
1370 |
$this->assertEquals(1, $result['questions'][0]['number']);
|
|
|
1371 |
$this->assertEquals(2, $result['questions'][1]['number']);
|
|
|
1372 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1373 |
$this->assertFalse($result['questions'][1]['flagged']);
|
|
|
1374 |
$this->assertEmpty($result['questions'][0]['mark']);
|
|
|
1375 |
$this->assertEmpty($result['questions'][1]['mark']);
|
|
|
1376 |
$this->assertEquals(2, $result['questions'][0]['sequencecheck']);
|
|
|
1377 |
$this->assertEquals(1, $result['questions'][1]['sequencecheck']);
|
|
|
1378 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1379 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][1]['lastactiontime']);
|
|
|
1380 |
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
|
|
|
1381 |
$this->assertEquals(false, $result['questions'][1]['hasautosavedstep']);
|
|
|
1382 |
$this->assertEquals(1, $result['totalunanswered']); // Only one question is unanswered.
|
|
|
1383 |
}
|
|
|
1384 |
|
|
|
1385 |
/**
|
|
|
1386 |
* Test save_attempt
|
|
|
1387 |
*/
|
11 |
efrain |
1388 |
public function test_save_attempt(): void {
|
1 |
efrain |
1389 |
|
|
|
1390 |
$timenow = time();
|
|
|
1391 |
// Create a new quiz with one attempt started.
|
|
|
1392 |
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true);
|
|
|
1393 |
|
|
|
1394 |
// Response for slot 1.
|
|
|
1395 |
$prefix = $quba->get_field_prefix(1);
|
|
|
1396 |
$data = [
|
|
|
1397 |
['name' => 'slots', 'value' => 1],
|
|
|
1398 |
['name' => $prefix . ':sequencecheck',
|
|
|
1399 |
'value' => $attemptobj->get_question_attempt(1)->get_sequence_check_count()],
|
|
|
1400 |
['name' => $prefix . 'answer', 'value' => 1],
|
|
|
1401 |
];
|
|
|
1402 |
|
|
|
1403 |
$this->setUser($this->student);
|
|
|
1404 |
|
|
|
1405 |
$result = mod_quiz_external::save_attempt($attempt->id, $data);
|
|
|
1406 |
$result = external_api::clean_returnvalue(mod_quiz_external::save_attempt_returns(), $result);
|
|
|
1407 |
$this->assertTrue($result['status']);
|
|
|
1408 |
|
|
|
1409 |
// Now, get the summary.
|
|
|
1410 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1411 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1412 |
|
|
|
1413 |
// Check it's marked as completed only the first one.
|
|
|
1414 |
$this->assertEquals('complete', $result['questions'][0]['state']);
|
|
|
1415 |
$this->assertEquals('answersaved', $result['questions'][0]['stateclass']);
|
|
|
1416 |
$this->assertEquals('todo', $result['questions'][1]['state']);
|
|
|
1417 |
$this->assertEquals('notyetanswered', $result['questions'][1]['stateclass']);
|
|
|
1418 |
$this->assertEquals(1, $result['questions'][0]['number']);
|
|
|
1419 |
$this->assertEquals(2, $result['questions'][1]['number']);
|
|
|
1420 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1421 |
$this->assertFalse($result['questions'][1]['flagged']);
|
|
|
1422 |
$this->assertEmpty($result['questions'][0]['mark']);
|
|
|
1423 |
$this->assertEmpty($result['questions'][1]['mark']);
|
|
|
1424 |
$this->assertEquals(1, $result['questions'][0]['sequencecheck']);
|
|
|
1425 |
$this->assertEquals(1, $result['questions'][1]['sequencecheck']);
|
|
|
1426 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1427 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][1]['lastactiontime']);
|
|
|
1428 |
$this->assertEquals(true, $result['questions'][0]['hasautosavedstep']);
|
|
|
1429 |
$this->assertEquals(false, $result['questions'][1]['hasautosavedstep']);
|
|
|
1430 |
|
|
|
1431 |
// Now, second slot.
|
|
|
1432 |
$prefix = $quba->get_field_prefix(2);
|
|
|
1433 |
$data = [
|
|
|
1434 |
['name' => 'slots', 'value' => 2],
|
|
|
1435 |
['name' => $prefix . ':sequencecheck',
|
|
|
1436 |
'value' => $attemptobj->get_question_attempt(1)->get_sequence_check_count()],
|
|
|
1437 |
['name' => $prefix . 'answer', 'value' => 1],
|
|
|
1438 |
];
|
|
|
1439 |
|
|
|
1440 |
$result = mod_quiz_external::save_attempt($attempt->id, $data);
|
|
|
1441 |
$result = external_api::clean_returnvalue(mod_quiz_external::save_attempt_returns(), $result);
|
|
|
1442 |
$this->assertTrue($result['status']);
|
|
|
1443 |
|
|
|
1444 |
// Now, get the summary.
|
|
|
1445 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1446 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1447 |
|
|
|
1448 |
// Check it's marked as completed only the first one.
|
|
|
1449 |
$this->assertEquals('complete', $result['questions'][0]['state']);
|
|
|
1450 |
$this->assertEquals('answersaved', $result['questions'][0]['stateclass']);
|
|
|
1451 |
$this->assertEquals(1, $result['questions'][0]['sequencecheck']);
|
|
|
1452 |
$this->assertEquals('complete', $result['questions'][1]['state']);
|
|
|
1453 |
$this->assertEquals('answersaved', $result['questions'][1]['stateclass']);
|
|
|
1454 |
$this->assertEquals(1, $result['questions'][1]['sequencecheck']);
|
|
|
1455 |
|
|
|
1456 |
}
|
|
|
1457 |
|
|
|
1458 |
/**
|
|
|
1459 |
* Test process_attempt
|
|
|
1460 |
*/
|
11 |
efrain |
1461 |
public function test_process_attempt(): void {
|
1 |
efrain |
1462 |
global $DB;
|
|
|
1463 |
|
|
|
1464 |
$timenow = time();
|
|
|
1465 |
// Create a new quiz with three questions and one attempt started.
|
|
|
1466 |
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, false,
|
|
|
1467 |
'deferredfeedback', true);
|
|
|
1468 |
|
|
|
1469 |
// Response for slot 1.
|
|
|
1470 |
$prefix = $quba->get_field_prefix(1);
|
|
|
1471 |
$data = [
|
|
|
1472 |
['name' => 'slots', 'value' => 1],
|
|
|
1473 |
['name' => $prefix . ':sequencecheck',
|
|
|
1474 |
'value' => $attemptobj->get_question_attempt(1)->get_sequence_check_count()],
|
|
|
1475 |
['name' => $prefix . 'answer', 'value' => 1],
|
|
|
1476 |
];
|
|
|
1477 |
|
|
|
1478 |
$this->setUser($this->student);
|
|
|
1479 |
|
|
|
1480 |
$result = mod_quiz_external::process_attempt($attempt->id, $data);
|
|
|
1481 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1482 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $result['state']);
|
|
|
1483 |
|
|
|
1484 |
$result = mod_quiz_external::get_attempt_data($attempt->id, 2);
|
|
|
1485 |
|
|
|
1486 |
// Now, get the summary.
|
|
|
1487 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1488 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1489 |
|
|
|
1490 |
// Check it's marked as completed only the first one.
|
|
|
1491 |
$this->assertEquals('complete', $result['questions'][0]['state']);
|
|
|
1492 |
$this->assertEquals('todo', $result['questions'][1]['state']);
|
|
|
1493 |
$this->assertEquals(1, $result['questions'][0]['number']);
|
|
|
1494 |
$this->assertEquals(2, $result['questions'][1]['number']);
|
|
|
1495 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1496 |
$this->assertFalse($result['questions'][1]['flagged']);
|
|
|
1497 |
$this->assertEmpty($result['questions'][0]['mark']);
|
|
|
1498 |
$this->assertEmpty($result['questions'][1]['mark']);
|
|
|
1499 |
$this->assertEquals(2, $result['questions'][0]['sequencecheck']);
|
|
|
1500 |
$this->assertEquals(2, $result['questions'][0]['sequencecheck']);
|
|
|
1501 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1502 |
$this->assertGreaterThanOrEqual($timenow, $result['questions'][0]['lastactiontime']);
|
|
|
1503 |
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
|
|
|
1504 |
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
|
|
|
1505 |
|
|
|
1506 |
// Now, second slot.
|
|
|
1507 |
$prefix = $quba->get_field_prefix(2);
|
|
|
1508 |
$data = [
|
|
|
1509 |
['name' => 'slots', 'value' => 2],
|
|
|
1510 |
['name' => $prefix . ':sequencecheck',
|
|
|
1511 |
'value' => $attemptobj->get_question_attempt(1)->get_sequence_check_count()],
|
|
|
1512 |
['name' => $prefix . 'answer', 'value' => 1],
|
|
|
1513 |
['name' => $prefix . ':flagged', 'value' => 1],
|
|
|
1514 |
];
|
|
|
1515 |
|
|
|
1516 |
$result = mod_quiz_external::process_attempt($attempt->id, $data);
|
|
|
1517 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1518 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $result['state']);
|
|
|
1519 |
|
|
|
1520 |
// Now, get the summary.
|
|
|
1521 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1522 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1523 |
|
|
|
1524 |
// Check it's marked as completed the two first questions.
|
|
|
1525 |
$this->assertEquals('complete', $result['questions'][0]['state']);
|
|
|
1526 |
$this->assertEquals('complete', $result['questions'][1]['state']);
|
|
|
1527 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1528 |
$this->assertTrue($result['questions'][1]['flagged']);
|
|
|
1529 |
|
|
|
1530 |
// Add files in the attachment response.
|
|
|
1531 |
$draftitemid = file_get_unused_draft_itemid();
|
|
|
1532 |
$filerecordinline = [
|
|
|
1533 |
'contextid' => \context_user::instance($this->student->id)->id,
|
|
|
1534 |
'component' => 'user',
|
|
|
1535 |
'filearea' => 'draft',
|
|
|
1536 |
'itemid' => $draftitemid,
|
|
|
1537 |
'filepath' => '/',
|
|
|
1538 |
'filename' => 'faketxt.txt',
|
|
|
1539 |
];
|
|
|
1540 |
$fs = get_file_storage();
|
|
|
1541 |
$fs->create_file_from_string($filerecordinline, 'fake txt contents 1.');
|
|
|
1542 |
|
|
|
1543 |
// Last slot.
|
|
|
1544 |
$prefix = $quba->get_field_prefix(3);
|
|
|
1545 |
$data = [
|
|
|
1546 |
['name' => 'slots', 'value' => 3],
|
|
|
1547 |
['name' => $prefix . ':sequencecheck',
|
|
|
1548 |
'value' => $attemptobj->get_question_attempt(1)->get_sequence_check_count()],
|
|
|
1549 |
['name' => $prefix . 'answer', 'value' => 'Some test'],
|
|
|
1550 |
['name' => $prefix . 'answerformat', 'value' => FORMAT_HTML],
|
|
|
1551 |
['name' => $prefix . 'attachments', 'value' => $draftitemid],
|
|
|
1552 |
];
|
|
|
1553 |
|
|
|
1554 |
$result = mod_quiz_external::process_attempt($attempt->id, $data);
|
|
|
1555 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1556 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $result['state']);
|
|
|
1557 |
|
|
|
1558 |
// Now, get the summary.
|
|
|
1559 |
$result = mod_quiz_external::get_attempt_summary($attempt->id);
|
|
|
1560 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_summary_returns(), $result);
|
|
|
1561 |
|
|
|
1562 |
$this->assertEquals('complete', $result['questions'][0]['state']);
|
|
|
1563 |
$this->assertEquals('complete', $result['questions'][1]['state']);
|
|
|
1564 |
$this->assertEquals('complete', $result['questions'][2]['state']);
|
|
|
1565 |
$this->assertFalse($result['questions'][0]['flagged']);
|
|
|
1566 |
$this->assertTrue($result['questions'][1]['flagged']);
|
|
|
1567 |
$this->assertFalse($result['questions'][2]['flagged']);
|
|
|
1568 |
|
|
|
1569 |
// Check submitted files are there.
|
|
|
1570 |
$this->assertCount(1, $result['questions'][2]['responsefileareas']);
|
|
|
1571 |
$this->assertEquals('attachments', $result['questions'][2]['responsefileareas'][0]['area']);
|
|
|
1572 |
$this->assertCount(1, $result['questions'][2]['responsefileareas'][0]['files']);
|
|
|
1573 |
$this->assertEquals($filerecordinline['filename'], $result['questions'][2]['responsefileareas'][0]['files'][0]['filename']);
|
|
|
1574 |
|
|
|
1575 |
// Finish the attempt.
|
|
|
1576 |
$sink = $this->redirectMessages();
|
|
|
1577 |
$result = mod_quiz_external::process_attempt($attempt->id, [], true);
|
|
|
1578 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1579 |
$this->assertEquals(quiz_attempt::FINISHED, $result['state']);
|
|
|
1580 |
$messages = $sink->get_messages();
|
|
|
1581 |
$message = reset($messages);
|
|
|
1582 |
$sink->close();
|
|
|
1583 |
// Test customdata.
|
|
|
1584 |
if (!empty($message->customdata)) {
|
|
|
1585 |
$customdata = json_decode($message->customdata);
|
|
|
1586 |
$this->assertEquals($quizobj->get_quizid(), $customdata->instance);
|
|
|
1587 |
$this->assertEquals($quizobj->get_cmid(), $customdata->cmid);
|
|
|
1588 |
$this->assertEquals($attempt->id, $customdata->attemptid);
|
|
|
1589 |
$this->assertObjectHasProperty('notificationiconurl', $customdata);
|
|
|
1590 |
}
|
|
|
1591 |
|
|
|
1592 |
// Start new attempt.
|
|
|
1593 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
1594 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
1595 |
|
|
|
1596 |
$timenow = time();
|
|
|
1597 |
$attempt = quiz_create_attempt($quizobj, 2, false, $timenow, false, $this->student->id);
|
|
|
1598 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 2, $timenow);
|
|
|
1599 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
1600 |
|
|
|
1601 |
// Force grace period, attempt going to overdue.
|
|
|
1602 |
$quiz->timeclose = $timenow - 10;
|
|
|
1603 |
$quiz->graceperiod = 60;
|
|
|
1604 |
$quiz->overduehandling = 'graceperiod';
|
|
|
1605 |
$DB->update_record('quiz', $quiz);
|
|
|
1606 |
|
|
|
1607 |
$result = mod_quiz_external::process_attempt($attempt->id, []);
|
|
|
1608 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1609 |
$this->assertEquals(quiz_attempt::OVERDUE, $result['state']);
|
|
|
1610 |
|
|
|
1611 |
// Force grace period for time limit.
|
|
|
1612 |
$quiz->timeclose = 0;
|
|
|
1613 |
$quiz->timelimit = 1;
|
|
|
1614 |
$quiz->graceperiod = 60;
|
|
|
1615 |
$quiz->overduehandling = 'graceperiod';
|
|
|
1616 |
$DB->update_record('quiz', $quiz);
|
|
|
1617 |
|
|
|
1618 |
$timenow = time();
|
|
|
1619 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
1620 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
1621 |
$attempt = quiz_create_attempt($quizobj, 3, 2, $timenow - 10, false, $this->student->id);
|
|
|
1622 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 2, $timenow - 10);
|
|
|
1623 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
1624 |
|
|
|
1625 |
$result = mod_quiz_external::process_attempt($attempt->id, []);
|
|
|
1626 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1627 |
$this->assertEquals(quiz_attempt::OVERDUE, $result['state']);
|
|
|
1628 |
|
|
|
1629 |
// New attempt.
|
|
|
1630 |
$timenow = time();
|
|
|
1631 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
1632 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
1633 |
$attempt = quiz_create_attempt($quizobj, 4, 3, $timenow, false, $this->student->id);
|
|
|
1634 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 3, $timenow);
|
|
|
1635 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
1636 |
|
|
|
1637 |
// Force abandon.
|
|
|
1638 |
$quiz->timeclose = $timenow - HOURSECS;
|
|
|
1639 |
$DB->update_record('quiz', $quiz);
|
|
|
1640 |
|
|
|
1641 |
$result = mod_quiz_external::process_attempt($attempt->id, []);
|
|
|
1642 |
$result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
|
|
|
1643 |
$this->assertEquals(quiz_attempt::ABANDONED, $result['state']);
|
|
|
1644 |
|
|
|
1645 |
}
|
|
|
1646 |
|
|
|
1647 |
/**
|
|
|
1648 |
* Test validate_attempt_review
|
|
|
1649 |
*/
|
11 |
efrain |
1650 |
public function test_validate_attempt_review(): void {
|
1 |
efrain |
1651 |
global $DB;
|
|
|
1652 |
|
|
|
1653 |
// Create a new quiz with one attempt started.
|
|
|
1654 |
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true);
|
|
|
1655 |
|
|
|
1656 |
$this->setUser($this->student);
|
|
|
1657 |
|
|
|
1658 |
// Invalid attempt, invalid id.
|
|
|
1659 |
try {
|
|
|
1660 |
$params = ['attemptid' => -1];
|
|
|
1661 |
testable_mod_quiz_external::validate_attempt_review($params);
|
|
|
1662 |
$this->fail('Exception expected due invalid id.');
|
|
|
1663 |
} catch (\dml_missing_record_exception $e) {
|
|
|
1664 |
$this->assertEquals('invalidrecord', $e->errorcode);
|
|
|
1665 |
}
|
|
|
1666 |
|
|
|
1667 |
// Invalid attempt, not closed.
|
|
|
1668 |
try {
|
|
|
1669 |
$params = ['attemptid' => $attempt->id];
|
|
|
1670 |
testable_mod_quiz_external::validate_attempt_review($params);
|
|
|
1671 |
$this->fail('Exception expected due not closed attempt.');
|
|
|
1672 |
} catch (moodle_exception $e) {
|
|
|
1673 |
$this->assertEquals('attemptclosed', $e->errorcode);
|
|
|
1674 |
}
|
|
|
1675 |
|
|
|
1676 |
// Test ok case (finished attempt).
|
|
|
1677 |
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true, true);
|
|
|
1678 |
|
|
|
1679 |
$params = ['attemptid' => $attempt->id];
|
|
|
1680 |
testable_mod_quiz_external::validate_attempt_review($params);
|
|
|
1681 |
|
|
|
1682 |
// Teacher should be able to view the review of one student's attempt.
|
|
|
1683 |
$this->setUser($this->teacher);
|
|
|
1684 |
testable_mod_quiz_external::validate_attempt_review($params);
|
|
|
1685 |
|
|
|
1686 |
// We should not see other students attempts.
|
|
|
1687 |
$anotherstudent = self::getDataGenerator()->create_user();
|
|
|
1688 |
$this->getDataGenerator()->enrol_user($anotherstudent->id, $this->course->id, $this->studentrole->id, 'manual');
|
|
|
1689 |
|
|
|
1690 |
$this->setUser($anotherstudent);
|
|
|
1691 |
try {
|
|
|
1692 |
$params = ['attemptid' => $attempt->id];
|
|
|
1693 |
testable_mod_quiz_external::validate_attempt_review($params);
|
|
|
1694 |
$this->fail('Exception expected due missing permissions.');
|
|
|
1695 |
} catch (moodle_exception $e) {
|
|
|
1696 |
$this->assertEquals('noreviewattempt', $e->errorcode);
|
|
|
1697 |
}
|
|
|
1698 |
}
|
|
|
1699 |
|
|
|
1700 |
/**
|
|
|
1701 |
* Test get_attempt_review
|
|
|
1702 |
*/
|
|
|
1703 |
public function test_get_attempt_review(): void {
|
|
|
1704 |
global $DB;
|
|
|
1705 |
|
|
|
1706 |
// Create a new quiz with two questions and one attempt finished.
|
|
|
1707 |
[$quiz, , , $attempt] = $this->create_quiz_with_questions(true, true);
|
|
|
1708 |
|
|
|
1709 |
// Add feedback to the quiz.
|
|
|
1710 |
$feedback = new \stdClass();
|
|
|
1711 |
$feedback->quizid = $quiz->id;
|
|
|
1712 |
$feedback->feedbacktext = 'Feedback text 1';
|
|
|
1713 |
$feedback->feedbacktextformat = 1;
|
|
|
1714 |
$feedback->mingrade = 49;
|
|
|
1715 |
$feedback->maxgrade = 100;
|
|
|
1716 |
$feedback->id = $DB->insert_record('quiz_feedback', $feedback);
|
|
|
1717 |
|
|
|
1718 |
$feedback->feedbacktext = 'Feedback text 2';
|
|
|
1719 |
$feedback->feedbacktextformat = 1;
|
|
|
1720 |
$feedback->mingrade = 30;
|
|
|
1721 |
$feedback->maxgrade = 48;
|
|
|
1722 |
$feedback->id = $DB->insert_record('quiz_feedback', $feedback);
|
|
|
1723 |
|
|
|
1724 |
$result = mod_quiz_external::get_attempt_review($attempt->id);
|
|
|
1725 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_review_returns(), $result);
|
|
|
1726 |
|
|
|
1727 |
// Two questions, one completed and correct, the other gave up.
|
|
|
1728 |
$this->assertEquals(50, $result['grade']);
|
|
|
1729 |
$this->assertEquals(1, $result['attempt']['attempt']);
|
|
|
1730 |
$this->assertEquals('finished', $result['attempt']['state']);
|
|
|
1731 |
$this->assertEquals(1, $result['attempt']['sumgrades']);
|
|
|
1732 |
$this->assertCount(2, $result['questions']);
|
|
|
1733 |
$this->assertEquals('gradedright', $result['questions'][0]['state']);
|
|
|
1734 |
$this->assertEquals(1, $result['questions'][0]['slot']);
|
|
|
1735 |
$this->assertEquals('gaveup', $result['questions'][1]['state']);
|
|
|
1736 |
$this->assertEquals(2, $result['questions'][1]['slot']);
|
|
|
1737 |
|
|
|
1738 |
// Only first page.
|
|
|
1739 |
$result = mod_quiz_external::get_attempt_review($attempt->id, 0);
|
|
|
1740 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_review_returns(), $result);
|
|
|
1741 |
|
|
|
1742 |
$this->assertEquals(50, $result['grade']);
|
|
|
1743 |
$this->assertEquals(1, $result['attempt']['attempt']);
|
|
|
1744 |
$this->assertEquals('finished', $result['attempt']['state']);
|
|
|
1745 |
$this->assertEquals(1, $result['attempt']['sumgrades']);
|
|
|
1746 |
$this->assertCount(1, $result['questions']);
|
|
|
1747 |
$this->assertEquals('gradedright', $result['questions'][0]['state']);
|
|
|
1748 |
$this->assertEquals(1, $result['questions'][0]['slot']);
|
|
|
1749 |
|
|
|
1750 |
$this->assertCount(1, $result['additionaldata']);
|
|
|
1751 |
$this->assertEquals('feedback', $result['additionaldata'][0]['id']);
|
|
|
1752 |
$this->assertEquals('Feedback', $result['additionaldata'][0]['title']);
|
|
|
1753 |
$this->assertEquals('Feedback text 1', $result['additionaldata'][0]['content']);
|
|
|
1754 |
}
|
|
|
1755 |
|
|
|
1756 |
/**
|
|
|
1757 |
* Test get_attempt_review
|
|
|
1758 |
*/
|
|
|
1759 |
public function test_get_attempt_review_with_extra_grades(): void {
|
|
|
1760 |
global $DB;
|
|
|
1761 |
|
|
|
1762 |
// Create a new quiz with two questions and one attempt finished.
|
|
|
1763 |
$this->setUser($this->student);
|
|
|
1764 |
[, , , $attempt, $attemptobj] = $this->create_quiz_with_questions(true, true);
|
|
|
1765 |
|
|
|
1766 |
// Add some extra grade items.
|
|
|
1767 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
1768 |
$listeninggrade = $quizgenerator->create_grade_item(['quizid' => $attemptobj->get_quizid(), 'name' => 'Listening']);
|
|
|
1769 |
$readinggrade = $quizgenerator->create_grade_item(['quizid' => $attemptobj->get_quizid(), 'name' => 'Reading']);
|
|
|
1770 |
$structure = $attemptobj->get_quizobj()->get_structure();
|
|
|
1771 |
$structure->update_slot_grade_item($structure->get_slot_by_number(1), $listeninggrade->id);
|
|
|
1772 |
$structure->update_slot_grade_item($structure->get_slot_by_number(2), $readinggrade->id);
|
|
|
1773 |
|
|
|
1774 |
$result = mod_quiz_external::get_attempt_review($attempt->id);
|
|
|
1775 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_review_returns(), $result);
|
|
|
1776 |
|
|
|
1777 |
// Two questions, one completed and correct, the other gave up.
|
|
|
1778 |
$this->assertEquals(50, $result['grade']);
|
|
|
1779 |
$this->assertEquals(1, $result['attempt']['attempt']);
|
|
|
1780 |
$this->assertEquals('finished', $result['attempt']['state']);
|
|
|
1781 |
$this->assertEquals(1, $result['attempt']['sumgrades']);
|
|
|
1782 |
$this->assertCount(2, $result['questions']);
|
|
|
1783 |
$this->assertEquals('gradedright', $result['questions'][0]['state']);
|
|
|
1784 |
$this->assertEquals(1, $result['questions'][0]['slot']);
|
|
|
1785 |
$this->assertEquals('gaveup', $result['questions'][1]['state']);
|
|
|
1786 |
$this->assertEquals(2, $result['questions'][1]['slot']);
|
|
|
1787 |
|
|
|
1788 |
// Only first page.
|
|
|
1789 |
$result = mod_quiz_external::get_attempt_review($attempt->id, 0);
|
|
|
1790 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_review_returns(), $result);
|
|
|
1791 |
|
|
|
1792 |
$this->assertEquals(50, $result['grade']);
|
|
|
1793 |
$this->assertEquals(1, $result['attempt']['attempt']);
|
|
|
1794 |
$this->assertEquals('finished', $result['attempt']['state']);
|
|
|
1795 |
$this->assertEquals(1, $result['attempt']['sumgrades']);
|
|
|
1796 |
$this->assertCount(1, $result['questions']);
|
|
|
1797 |
$this->assertEquals('gradedright', $result['questions'][0]['state']);
|
|
|
1798 |
$this->assertEquals(1, $result['questions'][0]['slot']);
|
|
|
1799 |
|
|
|
1800 |
// Verify additional grades.
|
|
|
1801 |
$this->assertEquals(['name' => 'Listening', 'grade' => 1, 'maxgrade' => 1], $result['attempt']['gradeitemmarks'][0]);
|
|
|
1802 |
$this->assertEquals(['name' => 'Reading', 'grade' => 0, 'maxgrade' => 1], $result['attempt']['gradeitemmarks'][1]);
|
|
|
1803 |
|
|
|
1804 |
// Now change the review options, so marks are not displayed, and check the result.
|
|
|
1805 |
$DB->set_field('quiz', 'reviewmarks', 0, ['id' => $attemptobj->get_quizid()]);
|
|
|
1806 |
$result = mod_quiz_external::get_attempt_review($attempt->id, 0);
|
|
|
1807 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_review_returns(), $result);
|
|
|
1808 |
|
|
|
1809 |
$this->assertEquals(1, $result['attempt']['attempt']);
|
|
|
1810 |
$this->assertEquals('finished', $result['attempt']['state']);
|
|
|
1811 |
$this->assertNull($result['attempt']['sumgrades']);
|
|
|
1812 |
$this->assertArrayNotHasKey('gradeitemmarks', $result['attempt']);
|
|
|
1813 |
}
|
|
|
1814 |
|
|
|
1815 |
/**
|
|
|
1816 |
* Test test_view_attempt
|
|
|
1817 |
*/
|
11 |
efrain |
1818 |
public function test_view_attempt(): void {
|
1 |
efrain |
1819 |
global $DB;
|
|
|
1820 |
|
|
|
1821 |
// Create a new quiz with two questions and one attempt started.
|
|
|
1822 |
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, false);
|
|
|
1823 |
|
|
|
1824 |
// Test user with full capabilities.
|
|
|
1825 |
$this->setUser($this->student);
|
|
|
1826 |
|
|
|
1827 |
// Trigger and capture the event.
|
|
|
1828 |
$sink = $this->redirectEvents();
|
|
|
1829 |
|
|
|
1830 |
$result = mod_quiz_external::view_attempt($attempt->id, 0);
|
|
|
1831 |
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_returns(), $result);
|
|
|
1832 |
$this->assertTrue($result['status']);
|
|
|
1833 |
|
|
|
1834 |
$events = $sink->get_events();
|
|
|
1835 |
$this->assertCount(1, $events);
|
|
|
1836 |
$event = array_shift($events);
|
|
|
1837 |
|
|
|
1838 |
// Checking that the event contains the expected values.
|
|
|
1839 |
$this->assertInstanceOf('\mod_quiz\event\attempt_viewed', $event);
|
|
|
1840 |
$this->assertEquals($context, $event->get_context());
|
|
|
1841 |
$this->assertEventContextNotUsed($event);
|
|
|
1842 |
$this->assertNotEmpty($event->get_name());
|
|
|
1843 |
|
|
|
1844 |
// Now, force the quiz with QUIZ_NAVMETHOD_SEQ (sequential) navigation method.
|
|
|
1845 |
$DB->set_field('quiz', 'navmethod', QUIZ_NAVMETHOD_SEQ, ['id' => $quiz->id]);
|
|
|
1846 |
// Quiz requiring preflightdata.
|
|
|
1847 |
$DB->set_field('quiz', 'password', 'abcdef', ['id' => $quiz->id]);
|
|
|
1848 |
$preflightdata = [["name" => "quizpassword", "value" => 'abcdef']];
|
|
|
1849 |
|
|
|
1850 |
// See next page.
|
|
|
1851 |
$result = mod_quiz_external::view_attempt($attempt->id, 1, $preflightdata);
|
|
|
1852 |
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_returns(), $result);
|
|
|
1853 |
$this->assertTrue($result['status']);
|
|
|
1854 |
|
|
|
1855 |
$events = $sink->get_events();
|
|
|
1856 |
$this->assertCount(2, $events);
|
|
|
1857 |
|
|
|
1858 |
// Try to go to previous page.
|
|
|
1859 |
try {
|
|
|
1860 |
mod_quiz_external::view_attempt($attempt->id, 0);
|
|
|
1861 |
$this->fail('Exception expected due to try to see a previous page.');
|
|
|
1862 |
} catch (moodle_exception $e) {
|
|
|
1863 |
$this->assertEquals('Out of sequence access', $e->errorcode);
|
|
|
1864 |
}
|
|
|
1865 |
|
|
|
1866 |
}
|
|
|
1867 |
|
|
|
1868 |
/**
|
|
|
1869 |
* Test test_view_attempt_summary
|
|
|
1870 |
*/
|
11 |
efrain |
1871 |
public function test_view_attempt_summary(): void {
|
1 |
efrain |
1872 |
global $DB;
|
|
|
1873 |
|
|
|
1874 |
// Create a new quiz with two questions and one attempt started.
|
|
|
1875 |
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, false);
|
|
|
1876 |
|
|
|
1877 |
// Test user with full capabilities.
|
|
|
1878 |
$this->setUser($this->student);
|
|
|
1879 |
|
|
|
1880 |
// Trigger and capture the event.
|
|
|
1881 |
$sink = $this->redirectEvents();
|
|
|
1882 |
|
|
|
1883 |
$result = mod_quiz_external::view_attempt_summary($attempt->id);
|
|
|
1884 |
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_summary_returns(), $result);
|
|
|
1885 |
$this->assertTrue($result['status']);
|
|
|
1886 |
|
|
|
1887 |
$events = $sink->get_events();
|
|
|
1888 |
$this->assertCount(1, $events);
|
|
|
1889 |
$event = array_shift($events);
|
|
|
1890 |
|
|
|
1891 |
// Checking that the event contains the expected values.
|
|
|
1892 |
$this->assertInstanceOf('\mod_quiz\event\attempt_summary_viewed', $event);
|
|
|
1893 |
$this->assertEquals($context, $event->get_context());
|
|
|
1894 |
$moodlequiz = new \moodle_url('/mod/quiz/summary.php', ['attempt' => $attempt->id]);
|
|
|
1895 |
$this->assertEquals($moodlequiz, $event->get_url());
|
|
|
1896 |
$this->assertEventContextNotUsed($event);
|
|
|
1897 |
$this->assertNotEmpty($event->get_name());
|
|
|
1898 |
|
|
|
1899 |
// Quiz requiring preflightdata.
|
|
|
1900 |
$DB->set_field('quiz', 'password', 'abcdef', ['id' => $quiz->id]);
|
|
|
1901 |
$preflightdata = [["name" => "quizpassword", "value" => 'abcdef']];
|
|
|
1902 |
|
|
|
1903 |
$result = mod_quiz_external::view_attempt_summary($attempt->id, $preflightdata);
|
|
|
1904 |
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_summary_returns(), $result);
|
|
|
1905 |
$this->assertTrue($result['status']);
|
|
|
1906 |
|
|
|
1907 |
}
|
|
|
1908 |
|
|
|
1909 |
/**
|
|
|
1910 |
* Test test_view_attempt_summary
|
|
|
1911 |
*/
|
11 |
efrain |
1912 |
public function test_view_attempt_review(): void {
|
1 |
efrain |
1913 |
global $DB;
|
|
|
1914 |
|
|
|
1915 |
// Create a new quiz with two questions and one attempt finished.
|
|
|
1916 |
list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, true);
|
|
|
1917 |
|
|
|
1918 |
// Test user with full capabilities.
|
|
|
1919 |
$this->setUser($this->student);
|
|
|
1920 |
|
|
|
1921 |
// Trigger and capture the event.
|
|
|
1922 |
$sink = $this->redirectEvents();
|
|
|
1923 |
|
|
|
1924 |
$result = mod_quiz_external::view_attempt_review($attempt->id, 0);
|
|
|
1925 |
$result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_review_returns(), $result);
|
|
|
1926 |
$this->assertTrue($result['status']);
|
|
|
1927 |
|
|
|
1928 |
$events = $sink->get_events();
|
|
|
1929 |
$this->assertCount(1, $events);
|
|
|
1930 |
$event = array_shift($events);
|
|
|
1931 |
|
|
|
1932 |
// Checking that the event contains the expected values.
|
|
|
1933 |
$this->assertInstanceOf('\mod_quiz\event\attempt_reviewed', $event);
|
|
|
1934 |
$this->assertEquals($context, $event->get_context());
|
|
|
1935 |
$moodlequiz = new \moodle_url('/mod/quiz/review.php', ['attempt' => $attempt->id]);
|
|
|
1936 |
$this->assertEquals($moodlequiz, $event->get_url());
|
|
|
1937 |
$this->assertEventContextNotUsed($event);
|
|
|
1938 |
$this->assertNotEmpty($event->get_name());
|
|
|
1939 |
|
|
|
1940 |
}
|
|
|
1941 |
|
|
|
1942 |
/**
|
|
|
1943 |
* Test get_quiz_feedback_for_grade
|
|
|
1944 |
*/
|
11 |
efrain |
1945 |
public function test_get_quiz_feedback_for_grade(): void {
|
1 |
efrain |
1946 |
global $DB;
|
|
|
1947 |
|
|
|
1948 |
// Add feedback to the quiz.
|
|
|
1949 |
$feedback = new \stdClass();
|
|
|
1950 |
$feedback->quizid = $this->quiz->id;
|
|
|
1951 |
$feedback->feedbacktext = 'Feedback text 1';
|
|
|
1952 |
$feedback->feedbacktextformat = 1;
|
|
|
1953 |
$feedback->mingrade = 49;
|
|
|
1954 |
$feedback->maxgrade = 100;
|
|
|
1955 |
$feedback->id = $DB->insert_record('quiz_feedback', $feedback);
|
|
|
1956 |
// Add a fake inline image to the feedback text.
|
|
|
1957 |
$filename = 'shouldbeanimage.jpg';
|
|
|
1958 |
$filerecordinline = [
|
|
|
1959 |
'contextid' => $this->context->id,
|
|
|
1960 |
'component' => 'mod_quiz',
|
|
|
1961 |
'filearea' => 'feedback',
|
|
|
1962 |
'itemid' => $feedback->id,
|
|
|
1963 |
'filepath' => '/',
|
|
|
1964 |
'filename' => $filename,
|
|
|
1965 |
];
|
|
|
1966 |
$fs = get_file_storage();
|
|
|
1967 |
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
|
|
|
1968 |
|
|
|
1969 |
$feedback->feedbacktext = 'Feedback text 2';
|
|
|
1970 |
$feedback->feedbacktextformat = 1;
|
|
|
1971 |
$feedback->mingrade = 30;
|
|
|
1972 |
$feedback->maxgrade = 49;
|
|
|
1973 |
$feedback->id = $DB->insert_record('quiz_feedback', $feedback);
|
|
|
1974 |
|
|
|
1975 |
$result = mod_quiz_external::get_quiz_feedback_for_grade($this->quiz->id, 50);
|
|
|
1976 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_feedback_for_grade_returns(), $result);
|
|
|
1977 |
$this->assertEquals('Feedback text 1', $result['feedbacktext']);
|
|
|
1978 |
$this->assertEquals($filename, $result['feedbackinlinefiles'][0]['filename']);
|
|
|
1979 |
$this->assertEquals(FORMAT_HTML, $result['feedbacktextformat']);
|
|
|
1980 |
|
|
|
1981 |
$result = mod_quiz_external::get_quiz_feedback_for_grade($this->quiz->id, 30);
|
|
|
1982 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_feedback_for_grade_returns(), $result);
|
|
|
1983 |
$this->assertEquals('Feedback text 2', $result['feedbacktext']);
|
|
|
1984 |
$this->assertEquals(FORMAT_HTML, $result['feedbacktextformat']);
|
|
|
1985 |
|
|
|
1986 |
$result = mod_quiz_external::get_quiz_feedback_for_grade($this->quiz->id, 10);
|
|
|
1987 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_feedback_for_grade_returns(), $result);
|
|
|
1988 |
$this->assertEquals('', $result['feedbacktext']);
|
|
|
1989 |
$this->assertEquals(FORMAT_MOODLE, $result['feedbacktextformat']);
|
|
|
1990 |
}
|
|
|
1991 |
|
|
|
1992 |
/**
|
|
|
1993 |
* Test get_quiz_access_information
|
|
|
1994 |
*/
|
11 |
efrain |
1995 |
public function test_get_quiz_access_information(): void {
|
1 |
efrain |
1996 |
global $DB;
|
|
|
1997 |
|
|
|
1998 |
// Create a new quiz.
|
|
|
1999 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
2000 |
$data = ['course' => $this->course->id];
|
|
|
2001 |
$quiz = $quizgenerator->create_instance($data);
|
|
|
2002 |
|
|
|
2003 |
$this->setUser($this->student);
|
|
|
2004 |
|
|
|
2005 |
// Default restrictions (none).
|
|
|
2006 |
$result = mod_quiz_external::get_quiz_access_information($quiz->id);
|
|
|
2007 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_access_information_returns(), $result);
|
|
|
2008 |
|
|
|
2009 |
$expected = [
|
|
|
2010 |
'canattempt' => true,
|
|
|
2011 |
'canmanage' => false,
|
|
|
2012 |
'canpreview' => false,
|
|
|
2013 |
'canreviewmyattempts' => true,
|
|
|
2014 |
'canviewreports' => false,
|
|
|
2015 |
'accessrules' => [],
|
|
|
2016 |
// This rule is always used, even if the quiz has no open or close date.
|
|
|
2017 |
'activerulenames' => ['quizaccess_openclosedate'],
|
|
|
2018 |
'preventaccessreasons' => [],
|
|
|
2019 |
'warnings' => []
|
|
|
2020 |
];
|
|
|
2021 |
|
|
|
2022 |
$this->assertEquals($expected, $result);
|
|
|
2023 |
|
|
|
2024 |
// Now teacher, different privileges.
|
|
|
2025 |
$this->setUser($this->teacher);
|
|
|
2026 |
$result = mod_quiz_external::get_quiz_access_information($quiz->id);
|
|
|
2027 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_access_information_returns(), $result);
|
|
|
2028 |
|
|
|
2029 |
$expected['canmanage'] = true;
|
|
|
2030 |
$expected['canpreview'] = true;
|
|
|
2031 |
$expected['canviewreports'] = true;
|
|
|
2032 |
$expected['canattempt'] = false;
|
|
|
2033 |
$expected['canreviewmyattempts'] = false;
|
|
|
2034 |
|
|
|
2035 |
$this->assertEquals($expected, $result);
|
|
|
2036 |
|
|
|
2037 |
$this->setUser($this->student);
|
|
|
2038 |
// Now add some restrictions.
|
|
|
2039 |
$quiz->timeopen = time() + DAYSECS;
|
|
|
2040 |
$quiz->timeclose = time() + WEEKSECS;
|
|
|
2041 |
$quiz->password = '123456';
|
|
|
2042 |
$DB->update_record('quiz', $quiz);
|
|
|
2043 |
|
|
|
2044 |
$result = mod_quiz_external::get_quiz_access_information($quiz->id);
|
|
|
2045 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_access_information_returns(), $result);
|
|
|
2046 |
|
|
|
2047 |
// Access is limited by time and password, but only the password limit has a description.
|
|
|
2048 |
$this->assertCount(1, $result['accessrules']);
|
|
|
2049 |
// Two rule names, password and open/close date.
|
|
|
2050 |
$this->assertCount(2, $result['activerulenames']);
|
|
|
2051 |
$this->assertCount(1, $result['preventaccessreasons']);
|
|
|
2052 |
|
|
|
2053 |
}
|
|
|
2054 |
|
|
|
2055 |
/**
|
|
|
2056 |
* Test get_attempt_access_information
|
|
|
2057 |
*/
|
11 |
efrain |
2058 |
public function test_get_attempt_access_information(): void {
|
1 |
efrain |
2059 |
global $DB;
|
|
|
2060 |
|
|
|
2061 |
$this->setAdminUser();
|
|
|
2062 |
|
|
|
2063 |
// Create a new quiz with attempts.
|
|
|
2064 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
2065 |
$data = ['course' => $this->course->id,
|
|
|
2066 |
'sumgrades' => 2];
|
|
|
2067 |
$quiz = $quizgenerator->create_instance($data);
|
|
|
2068 |
|
|
|
2069 |
// Create some questions.
|
|
|
2070 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
2071 |
|
|
|
2072 |
$cat = $questiongenerator->create_question_category();
|
|
|
2073 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
2074 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2075 |
|
|
|
2076 |
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
2077 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2078 |
|
|
|
2079 |
// Add new question types in the category (for the random one).
|
|
|
2080 |
$question = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
|
|
|
2081 |
$question = $questiongenerator->create_question('essay', null, ['category' => $cat->id]);
|
|
|
2082 |
|
|
|
2083 |
$this->add_random_questions($quiz->id, 0, $cat->id, 1);
|
|
|
2084 |
|
|
|
2085 |
$quizobj = quiz_settings::create($quiz->id, $this->student->id);
|
|
|
2086 |
|
|
|
2087 |
// Set grade to pass.
|
|
|
2088 |
$item = \grade_item::fetch(['courseid' => $this->course->id, 'itemtype' => 'mod',
|
|
|
2089 |
'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null]);
|
|
|
2090 |
$item->gradepass = 80;
|
|
|
2091 |
$item->update();
|
|
|
2092 |
|
|
|
2093 |
$this->setUser($this->student);
|
|
|
2094 |
|
|
|
2095 |
// Default restrictions (none).
|
|
|
2096 |
$result = mod_quiz_external::get_attempt_access_information($quiz->id);
|
|
|
2097 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_access_information_returns(), $result);
|
|
|
2098 |
|
|
|
2099 |
$expected = [
|
|
|
2100 |
'isfinished' => false,
|
|
|
2101 |
'preventnewattemptreasons' => [],
|
|
|
2102 |
'warnings' => []
|
|
|
2103 |
];
|
|
|
2104 |
|
|
|
2105 |
$this->assertEquals($expected, $result);
|
|
|
2106 |
|
|
|
2107 |
// Limited attempts.
|
|
|
2108 |
$quiz->attempts = 1;
|
|
|
2109 |
$DB->update_record('quiz', $quiz);
|
|
|
2110 |
|
|
|
2111 |
// Now, do one attempt.
|
|
|
2112 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
2113 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
2114 |
|
|
|
2115 |
$timenow = time();
|
|
|
2116 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $this->student->id);
|
|
|
2117 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
2118 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
2119 |
|
|
|
2120 |
// Process some responses from the student.
|
|
|
2121 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
2122 |
$tosubmit = [1 => ['answer' => '3.14']];
|
|
|
2123 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
2124 |
|
|
|
2125 |
// Finish the attempt.
|
|
|
2126 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
2127 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
2128 |
$attemptobj->process_finish($timenow, false);
|
|
|
2129 |
|
|
|
2130 |
// Can we start a new attempt? We shall not!
|
|
|
2131 |
$result = mod_quiz_external::get_attempt_access_information($quiz->id, $attempt->id);
|
|
|
2132 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_access_information_returns(), $result);
|
|
|
2133 |
|
|
|
2134 |
// Now new attemps allowed.
|
|
|
2135 |
$this->assertCount(1, $result['preventnewattemptreasons']);
|
|
|
2136 |
$this->assertFalse($result['ispreflightcheckrequired']);
|
|
|
2137 |
$this->assertEquals(get_string('nomoreattempts', 'quiz'), $result['preventnewattemptreasons'][0]);
|
|
|
2138 |
|
|
|
2139 |
}
|
|
|
2140 |
|
|
|
2141 |
/**
|
|
|
2142 |
* Test get_quiz_required_qtypes
|
|
|
2143 |
*/
|
11 |
efrain |
2144 |
public function test_get_quiz_required_qtypes(): void {
|
1 |
efrain |
2145 |
$this->setAdminUser();
|
|
|
2146 |
|
|
|
2147 |
// Create a new quiz.
|
|
|
2148 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
2149 |
$data = ['course' => $this->course->id];
|
|
|
2150 |
$quiz = $quizgenerator->create_instance($data);
|
|
|
2151 |
|
|
|
2152 |
// Create some questions.
|
|
|
2153 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
2154 |
|
|
|
2155 |
$cat = $questiongenerator->create_question_category();
|
|
|
2156 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
2157 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2158 |
|
|
|
2159 |
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
2160 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2161 |
|
|
|
2162 |
$question = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
|
|
|
2163 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2164 |
|
|
|
2165 |
$question = $questiongenerator->create_question('essay', null, ['category' => $cat->id]);
|
|
|
2166 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2167 |
|
|
|
2168 |
$question = $questiongenerator->create_question('multichoice', null,
|
|
|
2169 |
['category' => $cat->id, 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
|
|
|
2170 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
2171 |
|
|
|
2172 |
$this->setUser($this->student);
|
|
|
2173 |
|
|
|
2174 |
$result = mod_quiz_external::get_quiz_required_qtypes($quiz->id);
|
|
|
2175 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_required_qtypes_returns(), $result);
|
|
|
2176 |
|
|
|
2177 |
$expected = [
|
|
|
2178 |
'questiontypes' => ['essay', 'numerical', 'shortanswer', 'truefalse'],
|
|
|
2179 |
'warnings' => []
|
|
|
2180 |
];
|
|
|
2181 |
|
|
|
2182 |
$this->assertEquals($expected, $result);
|
|
|
2183 |
|
|
|
2184 |
}
|
|
|
2185 |
|
|
|
2186 |
/**
|
|
|
2187 |
* Test get_quiz_required_qtypes for quiz with random questions
|
|
|
2188 |
*/
|
11 |
efrain |
2189 |
public function test_get_quiz_required_qtypes_random(): void {
|
1 |
efrain |
2190 |
$this->setAdminUser();
|
|
|
2191 |
|
|
|
2192 |
// Create a new quiz.
|
|
|
2193 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
2194 |
$quiz = $quizgenerator->create_instance(['course' => $this->course->id]);
|
|
|
2195 |
|
|
|
2196 |
// Create some questions.
|
|
|
2197 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
2198 |
|
|
|
2199 |
$cat = $questiongenerator->create_question_category();
|
|
|
2200 |
$anothercat = $questiongenerator->create_question_category();
|
|
|
2201 |
|
|
|
2202 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
2203 |
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
2204 |
$question = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
|
|
|
2205 |
// Question in a different category.
|
|
|
2206 |
$question = $questiongenerator->create_question('essay', null, ['category' => $anothercat->id]);
|
|
|
2207 |
|
|
|
2208 |
// Add a couple of random questions from the same category.
|
|
|
2209 |
$this->add_random_questions($quiz->id, 0, $cat->id, 1);
|
|
|
2210 |
$this->add_random_questions($quiz->id, 0, $cat->id, 1);
|
|
|
2211 |
|
|
|
2212 |
$this->setUser($this->student);
|
|
|
2213 |
|
|
|
2214 |
$result = mod_quiz_external::get_quiz_required_qtypes($quiz->id);
|
|
|
2215 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_required_qtypes_returns(), $result);
|
|
|
2216 |
|
|
|
2217 |
$expected = ['numerical', 'shortanswer', 'truefalse'];
|
|
|
2218 |
ksort($result['questiontypes']);
|
|
|
2219 |
|
|
|
2220 |
$this->assertEquals($expected, $result['questiontypes']);
|
|
|
2221 |
|
|
|
2222 |
// Add more questions to the quiz, this time from the other category.
|
|
|
2223 |
$this->setAdminUser();
|
|
|
2224 |
$this->add_random_questions($quiz->id, 0, $anothercat->id, 1);
|
|
|
2225 |
|
|
|
2226 |
$this->setUser($this->student);
|
|
|
2227 |
$result = mod_quiz_external::get_quiz_required_qtypes($quiz->id);
|
|
|
2228 |
$result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_required_qtypes_returns(), $result);
|
|
|
2229 |
|
|
|
2230 |
// The new question from the new category is returned as a potential random question for the quiz.
|
|
|
2231 |
$expected = ['essay', 'numerical', 'shortanswer', 'truefalse'];
|
|
|
2232 |
ksort($result['questiontypes']);
|
|
|
2233 |
|
|
|
2234 |
$this->assertEquals($expected, $result['questiontypes']);
|
|
|
2235 |
}
|
|
|
2236 |
|
|
|
2237 |
/**
|
|
|
2238 |
* Test that a sequential navigation quiz is not allowing to see questions in advance except if reviewing
|
|
|
2239 |
*/
|
11 |
efrain |
2240 |
public function test_sequential_navigation_view_attempt(): void {
|
1 |
efrain |
2241 |
// Test user with full capabilities.
|
|
|
2242 |
$quiz = $this->prepare_sequential_quiz();
|
|
|
2243 |
$attemptobj = $this->create_quiz_attempt_object($quiz);
|
|
|
2244 |
$this->setUser($this->student);
|
|
|
2245 |
// Check out of sequence access for view.
|
|
|
2246 |
$this->assertNotEmpty(mod_quiz_external::view_attempt($attemptobj->get_attemptid(), 0, []));
|
|
|
2247 |
try {
|
|
|
2248 |
mod_quiz_external::view_attempt($attemptobj->get_attemptid(), 3, []);
|
|
|
2249 |
$this->fail('Exception expected due to out of sequence access.');
|
|
|
2250 |
} catch (moodle_exception $e) {
|
|
|
2251 |
$this->assertStringContainsString('quiz/Out of sequence access', $e->getMessage());
|
|
|
2252 |
}
|
|
|
2253 |
}
|
|
|
2254 |
|
|
|
2255 |
/**
|
|
|
2256 |
* Test that a sequential navigation quiz is not allowing to see questions content in advance for a student.
|
|
|
2257 |
*/
|
11 |
efrain |
2258 |
public function test_sequential_navigation_attempt_summary(): void {
|
1 |
efrain |
2259 |
// Test user with full capabilities.
|
|
|
2260 |
$quiz = $this->prepare_sequential_quiz();
|
|
|
2261 |
$attemptobj = $this->create_quiz_attempt_object($quiz);
|
|
|
2262 |
$this->setUser($this->student);
|
|
|
2263 |
// Check that we do not return content from other questions except than the ones currently viewed.
|
|
|
2264 |
$result = mod_quiz_external::get_attempt_summary($attemptobj->get_attemptid());
|
|
|
2265 |
$this->assertStringContainsString('Question (1)', $result['questions'][0]['html']); // Current question.
|
|
|
2266 |
$this->assertEmpty($result['questions'][1]['html']); // Next question.
|
|
|
2267 |
$this->assertEmpty($result['questions'][2]['html']); // And more.
|
|
|
2268 |
$this->assertEmpty($result['questions'][3]['html']); // And more.
|
|
|
2269 |
$this->assertEmpty($result['questions'][4]['html']); // And more.
|
|
|
2270 |
$this->assertNotContains('totalunanswered', $result); // For sequential quizzes, unanswered questions are not considered.
|
|
|
2271 |
}
|
|
|
2272 |
|
|
|
2273 |
/**
|
|
|
2274 |
* Test that a sequential navigation quiz is not allowing to see questions in advance for student
|
|
|
2275 |
*/
|
11 |
efrain |
2276 |
public function test_sequential_navigation_get_attempt_data(): void {
|
1 |
efrain |
2277 |
// Test user with full capabilities.
|
|
|
2278 |
$quiz = $this->prepare_sequential_quiz();
|
|
|
2279 |
$attemptobj = $this->create_quiz_attempt_object($quiz);
|
|
|
2280 |
$this->setUser($this->student);
|
|
|
2281 |
// Test invalid instance id.
|
|
|
2282 |
try {
|
|
|
2283 |
mod_quiz_external::get_attempt_data($attemptobj->get_attemptid(), 2);
|
|
|
2284 |
$this->fail('Exception expected due to out of sequence access.');
|
|
|
2285 |
} catch (moodle_exception $e) {
|
|
|
2286 |
$this->assertStringContainsString('quiz/Out of sequence access', $e->getMessage());
|
|
|
2287 |
}
|
|
|
2288 |
// Now we moved to page 1, we should see page 2 and 1 but not 0 or 3.
|
|
|
2289 |
$attemptobj->set_currentpage(1);
|
|
|
2290 |
// Test invalid instance id.
|
|
|
2291 |
try {
|
|
|
2292 |
mod_quiz_external::get_attempt_data($attemptobj->get_attemptid(), 0);
|
|
|
2293 |
$this->fail('Exception expected due to out of sequence access.');
|
|
|
2294 |
} catch (moodle_exception $e) {
|
|
|
2295 |
$this->assertStringContainsString('quiz/Out of sequence access', $e->getMessage());
|
|
|
2296 |
}
|
|
|
2297 |
|
|
|
2298 |
try {
|
|
|
2299 |
mod_quiz_external::get_attempt_data($attemptobj->get_attemptid(), 3);
|
|
|
2300 |
$this->fail('Exception expected due to out of sequence access.');
|
|
|
2301 |
} catch (moodle_exception $e) {
|
|
|
2302 |
$this->assertStringContainsString('quiz/Out of sequence access', $e->getMessage());
|
|
|
2303 |
}
|
|
|
2304 |
|
|
|
2305 |
// Now we can see page 1.
|
|
|
2306 |
$result = mod_quiz_external::get_attempt_data($attemptobj->get_attemptid(), 1);
|
|
|
2307 |
$this->assertCount(1, $result['questions']);
|
|
|
2308 |
$this->assertStringContainsString('Question (2)', $result['questions'][0]['html']);
|
|
|
2309 |
}
|
|
|
2310 |
|
|
|
2311 |
/**
|
|
|
2312 |
* Prepare quiz for sequential navigation tests
|
|
|
2313 |
*
|
|
|
2314 |
* @return quiz_settings
|
|
|
2315 |
*/
|
|
|
2316 |
private function prepare_sequential_quiz(): quiz_settings {
|
|
|
2317 |
// Create a new quiz with 5 questions and one attempt started.
|
|
|
2318 |
// Create a new quiz with attempts.
|
|
|
2319 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
2320 |
$data = [
|
|
|
2321 |
'course' => $this->course->id,
|
|
|
2322 |
'sumgrades' => 2,
|
|
|
2323 |
'questionsperpage' => 1,
|
|
|
2324 |
'preferredbehaviour' => 'deferredfeedback',
|
|
|
2325 |
'navmethod' => QUIZ_NAVMETHOD_SEQ
|
|
|
2326 |
];
|
|
|
2327 |
$quiz = $quizgenerator->create_instance($data);
|
|
|
2328 |
|
|
|
2329 |
// Now generate the questions.
|
|
|
2330 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
2331 |
$cat = $questiongenerator->create_question_category();
|
|
|
2332 |
for ($pageindex = 1; $pageindex <= 5; $pageindex++) {
|
|
|
2333 |
$question = $questiongenerator->create_question('truefalse', null, [
|
|
|
2334 |
'category' => $cat->id,
|
|
|
2335 |
'questiontext' => ['text' => "Question ($pageindex)"]
|
|
|
2336 |
]);
|
|
|
2337 |
quiz_add_quiz_question($question->id, $quiz, $pageindex);
|
|
|
2338 |
}
|
|
|
2339 |
|
|
|
2340 |
$quizobj = quiz_settings::create($quiz->id, $this->student->id);
|
|
|
2341 |
// Set grade to pass.
|
|
|
2342 |
$item = \grade_item::fetch(['courseid' => $this->course->id, 'itemtype' => 'mod',
|
|
|
2343 |
'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null]);
|
|
|
2344 |
$item->gradepass = 80;
|
|
|
2345 |
$item->update();
|
|
|
2346 |
return $quizobj;
|
|
|
2347 |
}
|
|
|
2348 |
|
|
|
2349 |
/**
|
|
|
2350 |
* Create question attempt
|
|
|
2351 |
*
|
|
|
2352 |
* @param quiz_settings $quizobj
|
|
|
2353 |
* @param int|null $userid
|
|
|
2354 |
* @param bool|null $ispreview
|
|
|
2355 |
* @return quiz_attempt
|
|
|
2356 |
* @throws moodle_exception
|
|
|
2357 |
*/
|
|
|
2358 |
private function create_quiz_attempt_object(
|
|
|
2359 |
quiz_settings $quizobj,
|
|
|
2360 |
?int $userid = null,
|
|
|
2361 |
?bool $ispreview = false
|
|
|
2362 |
): quiz_attempt {
|
|
|
2363 |
global $USER;
|
|
|
2364 |
|
|
|
2365 |
$timenow = time();
|
|
|
2366 |
// Now, do one attempt.
|
|
|
2367 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
2368 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
2369 |
$attemptnumber = 1;
|
|
|
2370 |
if (!empty($USER->id)) {
|
|
|
2371 |
$attemptnumber = count(quiz_get_user_attempts($quizobj->get_quizid(), $USER->id)) + 1;
|
|
|
2372 |
}
|
|
|
2373 |
$attempt = quiz_create_attempt($quizobj, $attemptnumber, false, $timenow, $ispreview, $userid ?? $this->student->id);
|
|
|
2374 |
quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow);
|
|
|
2375 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
2376 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
2377 |
return $attemptobj;
|
|
|
2378 |
}
|
|
|
2379 |
}
|