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 |
* Unit tests for (some of) mod/quiz/locallib.php.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_quiz
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2008 The Open University
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
24 |
*/
|
|
|
25 |
namespace mod_quiz;
|
|
|
26 |
|
|
|
27 |
use context_module;
|
|
|
28 |
use core_external\external_api;
|
|
|
29 |
use mod_quiz\quiz_settings;
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die();
|
|
|
32 |
|
|
|
33 |
global $CFG;
|
|
|
34 |
require_once($CFG->dirroot . '/mod/quiz/lib.php');
|
|
|
35 |
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @copyright 2008 The Open University
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
40 |
*/
|
|
|
41 |
class lib_test extends \advanced_testcase {
|
|
|
42 |
use \quiz_question_helper_test_trait;
|
|
|
43 |
|
|
|
44 |
public function test_quiz_has_grades() {
|
|
|
45 |
$quiz = new \stdClass();
|
|
|
46 |
$quiz->grade = '100.0000';
|
|
|
47 |
$quiz->sumgrades = '100.0000';
|
|
|
48 |
$this->assertTrue(quiz_has_grades($quiz));
|
|
|
49 |
$quiz->sumgrades = '0.0000';
|
|
|
50 |
$this->assertFalse(quiz_has_grades($quiz));
|
|
|
51 |
$quiz->grade = '0.0000';
|
|
|
52 |
$this->assertFalse(quiz_has_grades($quiz));
|
|
|
53 |
$quiz->sumgrades = '100.0000';
|
|
|
54 |
$this->assertFalse(quiz_has_grades($quiz));
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function test_quiz_format_grade() {
|
|
|
58 |
$quiz = new \stdClass();
|
|
|
59 |
$quiz->decimalpoints = 2;
|
|
|
60 |
$this->assertEquals(quiz_format_grade($quiz, 0.12345678), format_float(0.12, 2));
|
|
|
61 |
$this->assertEquals(quiz_format_grade($quiz, 0), format_float(0, 2));
|
|
|
62 |
$this->assertEquals(quiz_format_grade($quiz, 1.000000000000), format_float(1, 2));
|
|
|
63 |
$quiz->decimalpoints = 0;
|
|
|
64 |
$this->assertEquals(quiz_format_grade($quiz, 0.12345678), '0');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function test_quiz_get_grade_format() {
|
|
|
68 |
$quiz = new \stdClass();
|
|
|
69 |
$quiz->decimalpoints = 2;
|
|
|
70 |
$this->assertEquals(quiz_get_grade_format($quiz), 2);
|
|
|
71 |
$this->assertEquals($quiz->questiondecimalpoints, -1);
|
|
|
72 |
$quiz->questiondecimalpoints = 2;
|
|
|
73 |
$this->assertEquals(quiz_get_grade_format($quiz), 2);
|
|
|
74 |
$quiz->decimalpoints = 3;
|
|
|
75 |
$quiz->questiondecimalpoints = -1;
|
|
|
76 |
$this->assertEquals(quiz_get_grade_format($quiz), 3);
|
|
|
77 |
$quiz->questiondecimalpoints = 4;
|
|
|
78 |
$this->assertEquals(quiz_get_grade_format($quiz), 4);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function test_quiz_format_question_grade() {
|
|
|
82 |
$quiz = new \stdClass();
|
|
|
83 |
$quiz->decimalpoints = 2;
|
|
|
84 |
$quiz->questiondecimalpoints = 2;
|
|
|
85 |
$this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2));
|
|
|
86 |
$this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2));
|
|
|
87 |
$this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 2));
|
|
|
88 |
$quiz->decimalpoints = 3;
|
|
|
89 |
$quiz->questiondecimalpoints = -1;
|
|
|
90 |
$this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3));
|
|
|
91 |
$this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3));
|
|
|
92 |
$this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 3));
|
|
|
93 |
$quiz->questiondecimalpoints = 4;
|
|
|
94 |
$this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4));
|
|
|
95 |
$this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4));
|
|
|
96 |
$this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 4));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Test deleting a quiz instance.
|
|
|
101 |
*/
|
|
|
102 |
public function test_quiz_delete_instance() {
|
|
|
103 |
global $SITE, $DB;
|
|
|
104 |
$this->resetAfterTest(true);
|
|
|
105 |
$this->setAdminUser();
|
|
|
106 |
|
|
|
107 |
// Setup a quiz with 1 standard and 1 random question.
|
|
|
108 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
109 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
|
|
|
110 |
$context = context_module::instance($quiz->cmid);
|
|
|
111 |
|
|
|
112 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
113 |
$cat = $questiongenerator->create_question_category();
|
|
|
114 |
$standardq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
115 |
|
|
|
116 |
quiz_add_quiz_question($standardq->id, $quiz);
|
|
|
117 |
$this->add_random_questions($quiz->id, 0, $cat->id, 1);
|
|
|
118 |
|
|
|
119 |
// Get the random question.
|
|
|
120 |
$randomq = $DB->get_record('question', ['qtype' => 'random']);
|
|
|
121 |
|
|
|
122 |
quiz_delete_instance($quiz->id);
|
|
|
123 |
|
|
|
124 |
// Check that the random question was deleted.
|
|
|
125 |
if ($randomq) {
|
|
|
126 |
$this->assertEquals(0, $DB->count_records('question', ['id' => $randomq->id]));
|
|
|
127 |
}
|
|
|
128 |
// Check that the standard question was not deleted.
|
|
|
129 |
$this->assertEquals(1, $DB->count_records('question', ['id' => $standardq->id]));
|
|
|
130 |
|
|
|
131 |
// Check that all the slots were removed.
|
|
|
132 |
$this->assertEquals(0, $DB->count_records('quiz_slots', ['quizid' => $quiz->id]));
|
|
|
133 |
|
|
|
134 |
// Check that the quiz was removed.
|
|
|
135 |
$this->assertEquals(0, $DB->count_records('quiz', ['id' => $quiz->id]));
|
|
|
136 |
|
|
|
137 |
// Check that any question references linked to this quiz are gone.
|
|
|
138 |
$this->assertEquals(0, $DB->count_records('question_references', ['usingcontextid' => $context->id]));
|
|
|
139 |
$this->assertEquals(0, $DB->count_records('question_set_references', ['usingcontextid' => $context->id]));
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public function test_quiz_get_user_attempts() {
|
|
|
143 |
global $DB;
|
|
|
144 |
$this->resetAfterTest();
|
|
|
145 |
|
|
|
146 |
$dg = $this->getDataGenerator();
|
|
|
147 |
$quizgen = $dg->get_plugin_generator('mod_quiz');
|
|
|
148 |
$course = $dg->create_course();
|
|
|
149 |
$u1 = $dg->create_user();
|
|
|
150 |
$u2 = $dg->create_user();
|
|
|
151 |
$u3 = $dg->create_user();
|
|
|
152 |
$u4 = $dg->create_user();
|
|
|
153 |
$role = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
154 |
|
|
|
155 |
$dg->enrol_user($u1->id, $course->id, $role->id);
|
|
|
156 |
$dg->enrol_user($u2->id, $course->id, $role->id);
|
|
|
157 |
$dg->enrol_user($u3->id, $course->id, $role->id);
|
|
|
158 |
$dg->enrol_user($u4->id, $course->id, $role->id);
|
|
|
159 |
|
|
|
160 |
$quiz1 = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
|
|
|
161 |
$quiz2 = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
|
|
|
162 |
|
|
|
163 |
// Questions.
|
|
|
164 |
$questgen = $dg->get_plugin_generator('core_question');
|
|
|
165 |
$quizcat = $questgen->create_question_category();
|
|
|
166 |
$question = $questgen->create_question('numerical', null, ['category' => $quizcat->id]);
|
|
|
167 |
quiz_add_quiz_question($question->id, $quiz1);
|
|
|
168 |
quiz_add_quiz_question($question->id, $quiz2);
|
|
|
169 |
|
|
|
170 |
$quizobj1a = quiz_settings::create($quiz1->id, $u1->id);
|
|
|
171 |
$quizobj1b = quiz_settings::create($quiz1->id, $u2->id);
|
|
|
172 |
$quizobj1c = quiz_settings::create($quiz1->id, $u3->id);
|
|
|
173 |
$quizobj1d = quiz_settings::create($quiz1->id, $u4->id);
|
|
|
174 |
$quizobj2a = quiz_settings::create($quiz2->id, $u1->id);
|
|
|
175 |
|
|
|
176 |
// Set attempts.
|
|
|
177 |
$quba1a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1a->get_context());
|
|
|
178 |
$quba1a->set_preferred_behaviour($quizobj1a->get_quiz()->preferredbehaviour);
|
|
|
179 |
$quba1b = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1b->get_context());
|
|
|
180 |
$quba1b->set_preferred_behaviour($quizobj1b->get_quiz()->preferredbehaviour);
|
|
|
181 |
$quba1c = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1c->get_context());
|
|
|
182 |
$quba1c->set_preferred_behaviour($quizobj1c->get_quiz()->preferredbehaviour);
|
|
|
183 |
$quba1d = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1d->get_context());
|
|
|
184 |
$quba1d->set_preferred_behaviour($quizobj1d->get_quiz()->preferredbehaviour);
|
|
|
185 |
$quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
|
|
|
186 |
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
|
|
|
187 |
|
|
|
188 |
$timenow = time();
|
|
|
189 |
|
|
|
190 |
// User 1 passes quiz 1.
|
|
|
191 |
$attempt = quiz_create_attempt($quizobj1a, 1, false, $timenow, false, $u1->id);
|
|
|
192 |
quiz_start_new_attempt($quizobj1a, $quba1a, $attempt, 1, $timenow);
|
|
|
193 |
quiz_attempt_save_started($quizobj1a, $quba1a, $attempt);
|
|
|
194 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
195 |
$attemptobj->process_submitted_actions($timenow, false, [1 => ['answer' => '3.14']]);
|
|
|
196 |
$attemptobj->process_finish($timenow, false);
|
|
|
197 |
|
|
|
198 |
// User 2 goes overdue in quiz 1.
|
|
|
199 |
$attempt = quiz_create_attempt($quizobj1b, 1, false, $timenow, false, $u2->id);
|
|
|
200 |
quiz_start_new_attempt($quizobj1b, $quba1b, $attempt, 1, $timenow);
|
|
|
201 |
quiz_attempt_save_started($quizobj1b, $quba1b, $attempt);
|
|
|
202 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
203 |
$attemptobj->process_going_overdue($timenow, true);
|
|
|
204 |
|
|
|
205 |
// User 3 does not finish quiz 1.
|
|
|
206 |
$attempt = quiz_create_attempt($quizobj1c, 1, false, $timenow, false, $u3->id);
|
|
|
207 |
quiz_start_new_attempt($quizobj1c, $quba1c, $attempt, 1, $timenow);
|
|
|
208 |
quiz_attempt_save_started($quizobj1c, $quba1c, $attempt);
|
|
|
209 |
|
|
|
210 |
// User 4 abandons the quiz 1.
|
|
|
211 |
$attempt = quiz_create_attempt($quizobj1d, 1, false, $timenow, false, $u4->id);
|
|
|
212 |
quiz_start_new_attempt($quizobj1d, $quba1d, $attempt, 1, $timenow);
|
|
|
213 |
quiz_attempt_save_started($quizobj1d, $quba1d, $attempt);
|
|
|
214 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
215 |
$attemptobj->process_abandon($timenow, true);
|
|
|
216 |
|
|
|
217 |
// User 1 attempts the quiz three times (abandon, finish, in progress).
|
|
|
218 |
$quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
|
|
|
219 |
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
|
|
|
220 |
|
|
|
221 |
$attempt = quiz_create_attempt($quizobj2a, 1, false, $timenow, false, $u1->id);
|
|
|
222 |
quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 1, $timenow);
|
|
|
223 |
quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
|
|
|
224 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
225 |
$attemptobj->process_abandon($timenow, true);
|
|
|
226 |
|
|
|
227 |
$quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
|
|
|
228 |
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
|
|
|
229 |
|
|
|
230 |
$attempt = quiz_create_attempt($quizobj2a, 2, false, $timenow, false, $u1->id);
|
|
|
231 |
quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 2, $timenow);
|
|
|
232 |
quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
|
|
|
233 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
234 |
$attemptobj->process_finish($timenow, false);
|
|
|
235 |
|
|
|
236 |
$quba2a = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj2a->get_context());
|
|
|
237 |
$quba2a->set_preferred_behaviour($quizobj2a->get_quiz()->preferredbehaviour);
|
|
|
238 |
|
|
|
239 |
$attempt = quiz_create_attempt($quizobj2a, 3, false, $timenow, false, $u1->id);
|
|
|
240 |
quiz_start_new_attempt($quizobj2a, $quba2a, $attempt, 3, $timenow);
|
|
|
241 |
quiz_attempt_save_started($quizobj2a, $quba2a, $attempt);
|
|
|
242 |
|
|
|
243 |
// Check for user 1.
|
|
|
244 |
$attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'all');
|
|
|
245 |
$this->assertCount(1, $attempts);
|
|
|
246 |
$attempt = array_shift($attempts);
|
|
|
247 |
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
|
|
|
248 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
249 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
250 |
|
|
|
251 |
$attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'finished');
|
|
|
252 |
$this->assertCount(1, $attempts);
|
|
|
253 |
$attempt = array_shift($attempts);
|
|
|
254 |
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
|
|
|
255 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
256 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
257 |
|
|
|
258 |
$attempts = quiz_get_user_attempts($quiz1->id, $u1->id, 'unfinished');
|
|
|
259 |
$this->assertCount(0, $attempts);
|
|
|
260 |
|
|
|
261 |
// Check for user 2.
|
|
|
262 |
$attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'all');
|
|
|
263 |
$this->assertCount(1, $attempts);
|
|
|
264 |
$attempt = array_shift($attempts);
|
|
|
265 |
$this->assertEquals(quiz_attempt::OVERDUE, $attempt->state);
|
|
|
266 |
$this->assertEquals($u2->id, $attempt->userid);
|
|
|
267 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
268 |
|
|
|
269 |
$attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'finished');
|
|
|
270 |
$this->assertCount(0, $attempts);
|
|
|
271 |
|
|
|
272 |
$attempts = quiz_get_user_attempts($quiz1->id, $u2->id, 'unfinished');
|
|
|
273 |
$this->assertCount(1, $attempts);
|
|
|
274 |
$attempt = array_shift($attempts);
|
|
|
275 |
$this->assertEquals(quiz_attempt::OVERDUE, $attempt->state);
|
|
|
276 |
$this->assertEquals($u2->id, $attempt->userid);
|
|
|
277 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
278 |
|
|
|
279 |
// Check for user 3.
|
|
|
280 |
$attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'all');
|
|
|
281 |
$this->assertCount(1, $attempts);
|
|
|
282 |
$attempt = array_shift($attempts);
|
|
|
283 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
|
|
|
284 |
$this->assertEquals($u3->id, $attempt->userid);
|
|
|
285 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
286 |
|
|
|
287 |
$attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'finished');
|
|
|
288 |
$this->assertCount(0, $attempts);
|
|
|
289 |
|
|
|
290 |
$attempts = quiz_get_user_attempts($quiz1->id, $u3->id, 'unfinished');
|
|
|
291 |
$this->assertCount(1, $attempts);
|
|
|
292 |
$attempt = array_shift($attempts);
|
|
|
293 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
|
|
|
294 |
$this->assertEquals($u3->id, $attempt->userid);
|
|
|
295 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
296 |
|
|
|
297 |
// Check for user 4.
|
|
|
298 |
$attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'all');
|
|
|
299 |
$this->assertCount(1, $attempts);
|
|
|
300 |
$attempt = array_shift($attempts);
|
|
|
301 |
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
|
|
|
302 |
$this->assertEquals($u4->id, $attempt->userid);
|
|
|
303 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
304 |
|
|
|
305 |
$attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'finished');
|
|
|
306 |
$this->assertCount(1, $attempts);
|
|
|
307 |
$attempt = array_shift($attempts);
|
|
|
308 |
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
|
|
|
309 |
$this->assertEquals($u4->id, $attempt->userid);
|
|
|
310 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
311 |
|
|
|
312 |
$attempts = quiz_get_user_attempts($quiz1->id, $u4->id, 'unfinished');
|
|
|
313 |
$this->assertCount(0, $attempts);
|
|
|
314 |
|
|
|
315 |
// Multiple attempts for user 1 in quiz 2.
|
|
|
316 |
$attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'all');
|
|
|
317 |
$this->assertCount(3, $attempts);
|
|
|
318 |
$attempt = array_shift($attempts);
|
|
|
319 |
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
|
|
|
320 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
321 |
$this->assertEquals($quiz2->id, $attempt->quiz);
|
|
|
322 |
$attempt = array_shift($attempts);
|
|
|
323 |
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
|
|
|
324 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
325 |
$this->assertEquals($quiz2->id, $attempt->quiz);
|
|
|
326 |
$attempt = array_shift($attempts);
|
|
|
327 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
|
|
|
328 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
329 |
$this->assertEquals($quiz2->id, $attempt->quiz);
|
|
|
330 |
|
|
|
331 |
$attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'finished');
|
|
|
332 |
$this->assertCount(2, $attempts);
|
|
|
333 |
$attempt = array_shift($attempts);
|
|
|
334 |
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
|
|
|
335 |
$attempt = array_shift($attempts);
|
|
|
336 |
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
|
|
|
337 |
|
|
|
338 |
$attempts = quiz_get_user_attempts($quiz2->id, $u1->id, 'unfinished');
|
|
|
339 |
$this->assertCount(1, $attempts);
|
|
|
340 |
$attempt = array_shift($attempts);
|
|
|
341 |
|
|
|
342 |
// Multiple quiz attempts fetched at once.
|
|
|
343 |
$attempts = quiz_get_user_attempts([$quiz1->id, $quiz2->id], $u1->id, 'all');
|
|
|
344 |
$this->assertCount(4, $attempts);
|
|
|
345 |
$attempt = array_shift($attempts);
|
|
|
346 |
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
|
|
|
347 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
348 |
$this->assertEquals($quiz1->id, $attempt->quiz);
|
|
|
349 |
$attempt = array_shift($attempts);
|
|
|
350 |
$this->assertEquals(quiz_attempt::ABANDONED, $attempt->state);
|
|
|
351 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
352 |
$this->assertEquals($quiz2->id, $attempt->quiz);
|
|
|
353 |
$attempt = array_shift($attempts);
|
|
|
354 |
$this->assertEquals(quiz_attempt::FINISHED, $attempt->state);
|
|
|
355 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
356 |
$this->assertEquals($quiz2->id, $attempt->quiz);
|
|
|
357 |
$attempt = array_shift($attempts);
|
|
|
358 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attempt->state);
|
|
|
359 |
$this->assertEquals($u1->id, $attempt->userid);
|
|
|
360 |
$this->assertEquals($quiz2->id, $attempt->quiz);
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* Test for quiz_get_group_override_priorities().
|
|
|
365 |
*/
|
|
|
366 |
public function test_quiz_get_group_override_priorities() {
|
|
|
367 |
global $DB;
|
|
|
368 |
$this->resetAfterTest();
|
|
|
369 |
|
|
|
370 |
$dg = $this->getDataGenerator();
|
|
|
371 |
$quizgen = $dg->get_plugin_generator('mod_quiz');
|
|
|
372 |
$course = $dg->create_course();
|
|
|
373 |
|
|
|
374 |
$quiz = $quizgen->create_instance(['course' => $course->id, 'sumgrades' => 2]);
|
|
|
375 |
|
|
|
376 |
$this->assertNull(quiz_get_group_override_priorities($quiz->id));
|
|
|
377 |
|
|
|
378 |
$group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
|
|
|
379 |
$group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
|
|
|
380 |
|
|
|
381 |
$now = 100;
|
|
|
382 |
$override1 = (object)[
|
|
|
383 |
'quiz' => $quiz->id,
|
|
|
384 |
'groupid' => $group1->id,
|
|
|
385 |
'timeopen' => $now,
|
|
|
386 |
'timeclose' => $now + 20
|
|
|
387 |
];
|
|
|
388 |
$DB->insert_record('quiz_overrides', $override1);
|
|
|
389 |
|
|
|
390 |
$override2 = (object)[
|
|
|
391 |
'quiz' => $quiz->id,
|
|
|
392 |
'groupid' => $group2->id,
|
|
|
393 |
'timeopen' => $now - 10,
|
|
|
394 |
'timeclose' => $now + 10
|
|
|
395 |
];
|
|
|
396 |
$DB->insert_record('quiz_overrides', $override2);
|
|
|
397 |
|
|
|
398 |
$priorities = quiz_get_group_override_priorities($quiz->id);
|
|
|
399 |
$this->assertNotEmpty($priorities);
|
|
|
400 |
|
|
|
401 |
$openpriorities = $priorities['open'];
|
|
|
402 |
// Override 2's time open has higher priority since it is sooner than override 1's.
|
|
|
403 |
$this->assertEquals(2, $openpriorities[$override1->timeopen]);
|
|
|
404 |
$this->assertEquals(1, $openpriorities[$override2->timeopen]);
|
|
|
405 |
|
|
|
406 |
$closepriorities = $priorities['close'];
|
|
|
407 |
// Override 1's time close has higher priority since it is later than override 2's.
|
|
|
408 |
$this->assertEquals(1, $closepriorities[$override1->timeclose]);
|
|
|
409 |
$this->assertEquals(2, $closepriorities[$override2->timeclose]);
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
public function test_quiz_core_calendar_provide_event_action_open() {
|
|
|
413 |
$this->resetAfterTest();
|
|
|
414 |
|
|
|
415 |
$this->setAdminUser();
|
|
|
416 |
|
|
|
417 |
// Create a course.
|
|
|
418 |
$course = $this->getDataGenerator()->create_course();
|
|
|
419 |
// Create a student and enrol into the course.
|
|
|
420 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
421 |
// Create a quiz.
|
|
|
422 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
423 |
'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS]);
|
|
|
424 |
|
|
|
425 |
// Create a calendar event.
|
|
|
426 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
|
|
427 |
// Now, log in as student.
|
|
|
428 |
$this->setUser($student);
|
|
|
429 |
// Create an action factory.
|
|
|
430 |
$factory = new \core_calendar\action_factory();
|
|
|
431 |
|
|
|
432 |
// Decorate action event.
|
|
|
433 |
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
|
|
|
434 |
|
|
|
435 |
// Confirm the event was decorated.
|
|
|
436 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
437 |
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
|
|
|
438 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
439 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
440 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
public function test_quiz_core_calendar_provide_event_action_open_for_user() {
|
|
|
444 |
$this->resetAfterTest();
|
|
|
445 |
|
|
|
446 |
$this->setAdminUser();
|
|
|
447 |
|
|
|
448 |
// Create a course.
|
|
|
449 |
$course = $this->getDataGenerator()->create_course();
|
|
|
450 |
// Create a student and enrol into the course.
|
|
|
451 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
452 |
// Create a quiz.
|
|
|
453 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
454 |
'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS]);
|
|
|
455 |
|
|
|
456 |
// Create a calendar event.
|
|
|
457 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
|
|
458 |
|
|
|
459 |
// Create an action factory.
|
|
|
460 |
$factory = new \core_calendar\action_factory();
|
|
|
461 |
|
|
|
462 |
// Decorate action event for the student.
|
|
|
463 |
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
464 |
|
|
|
465 |
// Confirm the event was decorated.
|
|
|
466 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
467 |
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
|
|
|
468 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
469 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
470 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
public function test_quiz_core_calendar_provide_event_action_closed() {
|
|
|
474 |
$this->resetAfterTest();
|
|
|
475 |
|
|
|
476 |
$this->setAdminUser();
|
|
|
477 |
|
|
|
478 |
// Create a course.
|
|
|
479 |
$course = $this->getDataGenerator()->create_course();
|
|
|
480 |
|
|
|
481 |
// Create a quiz.
|
|
|
482 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
483 |
'timeclose' => time() - DAYSECS]);
|
|
|
484 |
|
|
|
485 |
// Create a calendar event.
|
|
|
486 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
|
|
|
487 |
|
|
|
488 |
// Create an action factory.
|
|
|
489 |
$factory = new \core_calendar\action_factory();
|
|
|
490 |
|
|
|
491 |
// Confirm the result was null.
|
|
|
492 |
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
public function test_quiz_core_calendar_provide_event_action_closed_for_user() {
|
|
|
496 |
$this->resetAfterTest();
|
|
|
497 |
|
|
|
498 |
$this->setAdminUser();
|
|
|
499 |
|
|
|
500 |
// Create a course.
|
|
|
501 |
$course = $this->getDataGenerator()->create_course();
|
|
|
502 |
|
|
|
503 |
// Create a student.
|
|
|
504 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
505 |
|
|
|
506 |
// Create a quiz.
|
|
|
507 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
508 |
'timeclose' => time() - DAYSECS]);
|
|
|
509 |
|
|
|
510 |
// Create a calendar event.
|
|
|
511 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
|
|
|
512 |
|
|
|
513 |
// Create an action factory.
|
|
|
514 |
$factory = new \core_calendar\action_factory();
|
|
|
515 |
|
|
|
516 |
// Confirm the result was null.
|
|
|
517 |
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
public function test_quiz_core_calendar_provide_event_action_open_in_future() {
|
|
|
521 |
$this->resetAfterTest();
|
|
|
522 |
|
|
|
523 |
$this->setAdminUser();
|
|
|
524 |
|
|
|
525 |
// Create a course.
|
|
|
526 |
$course = $this->getDataGenerator()->create_course();
|
|
|
527 |
// Create a student and enrol into the course.
|
|
|
528 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
529 |
// Create a quiz.
|
|
|
530 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
531 |
'timeopen' => time() + DAYSECS]);
|
|
|
532 |
|
|
|
533 |
// Create a calendar event.
|
|
|
534 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
|
|
|
535 |
// Now, log in as student.
|
|
|
536 |
$this->setUser($student);
|
|
|
537 |
// Create an action factory.
|
|
|
538 |
$factory = new \core_calendar\action_factory();
|
|
|
539 |
|
|
|
540 |
// Decorate action event.
|
|
|
541 |
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
|
|
|
542 |
|
|
|
543 |
// Confirm the event was decorated.
|
|
|
544 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
545 |
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
|
|
|
546 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
547 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
548 |
$this->assertFalse($actionevent->is_actionable());
|
|
|
549 |
}
|
|
|
550 |
|
|
|
551 |
public function test_quiz_core_calendar_provide_event_action_open_in_future_for_user() {
|
|
|
552 |
$this->resetAfterTest();
|
|
|
553 |
|
|
|
554 |
$this->setAdminUser();
|
|
|
555 |
|
|
|
556 |
// Create a course.
|
|
|
557 |
$course = $this->getDataGenerator()->create_course();
|
|
|
558 |
// Create a student and enrol into the course.
|
|
|
559 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
560 |
// Create a quiz.
|
|
|
561 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
562 |
'timeopen' => time() + DAYSECS]);
|
|
|
563 |
|
|
|
564 |
// Create a calendar event.
|
|
|
565 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
|
|
|
566 |
|
|
|
567 |
// Create an action factory.
|
|
|
568 |
$factory = new \core_calendar\action_factory();
|
|
|
569 |
|
|
|
570 |
// Decorate action event for the student.
|
|
|
571 |
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
572 |
|
|
|
573 |
// Confirm the event was decorated.
|
|
|
574 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
575 |
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
|
|
|
576 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
577 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
578 |
$this->assertFalse($actionevent->is_actionable());
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
public function test_quiz_core_calendar_provide_event_action_no_capability() {
|
|
|
582 |
global $DB;
|
|
|
583 |
|
|
|
584 |
$this->resetAfterTest();
|
|
|
585 |
$this->setAdminUser();
|
|
|
586 |
|
|
|
587 |
// Create a course.
|
|
|
588 |
$course = $this->getDataGenerator()->create_course();
|
|
|
589 |
|
|
|
590 |
// Create a student.
|
|
|
591 |
$student = $this->getDataGenerator()->create_user();
|
|
|
592 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
593 |
|
|
|
594 |
// Enrol student.
|
|
|
595 |
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
|
|
|
596 |
|
|
|
597 |
// Create a quiz.
|
|
|
598 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
599 |
|
|
|
600 |
// Remove the permission to attempt or review the quiz for the student role.
|
|
|
601 |
$coursecontext = \context_course::instance($course->id);
|
|
|
602 |
assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
|
|
|
603 |
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
|
|
|
604 |
|
|
|
605 |
// Create a calendar event.
|
|
|
606 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
|
|
607 |
|
|
|
608 |
// Create an action factory.
|
|
|
609 |
$factory = new \core_calendar\action_factory();
|
|
|
610 |
|
|
|
611 |
// Set current user to the student.
|
|
|
612 |
$this->setUser($student);
|
|
|
613 |
|
|
|
614 |
// Confirm null is returned.
|
|
|
615 |
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
|
|
|
616 |
}
|
|
|
617 |
|
|
|
618 |
public function test_quiz_core_calendar_provide_event_action_no_capability_for_user() {
|
|
|
619 |
global $DB;
|
|
|
620 |
|
|
|
621 |
$this->resetAfterTest();
|
|
|
622 |
$this->setAdminUser();
|
|
|
623 |
|
|
|
624 |
// Create a course.
|
|
|
625 |
$course = $this->getDataGenerator()->create_course();
|
|
|
626 |
|
|
|
627 |
// Create a student.
|
|
|
628 |
$student = $this->getDataGenerator()->create_user();
|
|
|
629 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
630 |
|
|
|
631 |
// Enrol student.
|
|
|
632 |
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
|
|
|
633 |
|
|
|
634 |
// Create a quiz.
|
|
|
635 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
636 |
|
|
|
637 |
// Remove the permission to attempt or review the quiz for the student role.
|
|
|
638 |
$coursecontext = \context_course::instance($course->id);
|
|
|
639 |
assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
|
|
|
640 |
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
|
|
|
641 |
|
|
|
642 |
// Create a calendar event.
|
|
|
643 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
|
|
644 |
|
|
|
645 |
// Create an action factory.
|
|
|
646 |
$factory = new \core_calendar\action_factory();
|
|
|
647 |
|
|
|
648 |
// Confirm null is returned.
|
|
|
649 |
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
public function test_quiz_core_calendar_provide_event_action_already_finished() {
|
|
|
653 |
global $DB;
|
|
|
654 |
|
|
|
655 |
$this->resetAfterTest();
|
|
|
656 |
|
|
|
657 |
$this->setAdminUser();
|
|
|
658 |
|
|
|
659 |
// Create a course.
|
|
|
660 |
$course = $this->getDataGenerator()->create_course();
|
|
|
661 |
|
|
|
662 |
// Create a student.
|
|
|
663 |
$student = $this->getDataGenerator()->create_user();
|
|
|
664 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
665 |
|
|
|
666 |
// Enrol student.
|
|
|
667 |
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
|
|
|
668 |
|
|
|
669 |
// Create a quiz.
|
|
|
670 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
671 |
'sumgrades' => 1]);
|
|
|
672 |
|
|
|
673 |
// Add a question to the quiz.
|
|
|
674 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
675 |
$cat = $questiongenerator->create_question_category();
|
|
|
676 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
677 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
678 |
|
|
|
679 |
// Get the quiz object.
|
|
|
680 |
$quizobj = quiz_settings::create($quiz->id, $student->id);
|
|
|
681 |
|
|
|
682 |
// Create an attempt for the student in the quiz.
|
|
|
683 |
$timenow = time();
|
|
|
684 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
|
|
|
685 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
686 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
687 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
688 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
689 |
|
|
|
690 |
// Finish the attempt.
|
|
|
691 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
692 |
$attemptobj->process_finish($timenow, false);
|
|
|
693 |
|
|
|
694 |
// Create a calendar event.
|
|
|
695 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
|
|
696 |
|
|
|
697 |
// Create an action factory.
|
|
|
698 |
$factory = new \core_calendar\action_factory();
|
|
|
699 |
|
|
|
700 |
// Set current user to the student.
|
|
|
701 |
$this->setUser($student);
|
|
|
702 |
|
|
|
703 |
// Confirm null is returned.
|
|
|
704 |
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
|
|
|
705 |
}
|
|
|
706 |
|
|
|
707 |
public function test_quiz_core_calendar_provide_event_action_already_finished_for_user() {
|
|
|
708 |
global $DB;
|
|
|
709 |
|
|
|
710 |
$this->resetAfterTest();
|
|
|
711 |
|
|
|
712 |
$this->setAdminUser();
|
|
|
713 |
|
|
|
714 |
// Create a course.
|
|
|
715 |
$course = $this->getDataGenerator()->create_course();
|
|
|
716 |
|
|
|
717 |
// Create a student.
|
|
|
718 |
$student = $this->getDataGenerator()->create_user();
|
|
|
719 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
720 |
|
|
|
721 |
// Enrol student.
|
|
|
722 |
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
|
|
|
723 |
|
|
|
724 |
// Create a quiz.
|
|
|
725 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id,
|
|
|
726 |
'sumgrades' => 1]);
|
|
|
727 |
|
|
|
728 |
// Add a question to the quiz.
|
|
|
729 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
730 |
$cat = $questiongenerator->create_question_category();
|
|
|
731 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
732 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
733 |
|
|
|
734 |
// Get the quiz object.
|
|
|
735 |
$quizobj = quiz_settings::create($quiz->id, $student->id);
|
|
|
736 |
|
|
|
737 |
// Create an attempt for the student in the quiz.
|
|
|
738 |
$timenow = time();
|
|
|
739 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
|
|
|
740 |
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
741 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
742 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
743 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
744 |
|
|
|
745 |
// Finish the attempt.
|
|
|
746 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
747 |
$attemptobj->process_finish($timenow, false);
|
|
|
748 |
|
|
|
749 |
// Create a calendar event.
|
|
|
750 |
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
|
|
751 |
|
|
|
752 |
// Create an action factory.
|
|
|
753 |
$factory = new \core_calendar\action_factory();
|
|
|
754 |
|
|
|
755 |
// Confirm null is returned.
|
|
|
756 |
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
|
|
|
757 |
}
|
|
|
758 |
|
|
|
759 |
public function test_quiz_core_calendar_provide_event_action_already_completed() {
|
|
|
760 |
$this->resetAfterTest();
|
|
|
761 |
set_config('enablecompletion', 1);
|
|
|
762 |
$this->setAdminUser();
|
|
|
763 |
|
|
|
764 |
// Create the activity.
|
|
|
765 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
766 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id],
|
|
|
767 |
['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS]);
|
|
|
768 |
|
|
|
769 |
// Get some additional data.
|
|
|
770 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
|
|
771 |
|
|
|
772 |
// Create a calendar event.
|
|
|
773 |
$event = $this->create_action_event($course->id, $quiz->id,
|
|
|
774 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
775 |
|
|
|
776 |
// Mark the activity as completed.
|
|
|
777 |
$completion = new \completion_info($course);
|
|
|
778 |
$completion->set_module_viewed($cm);
|
|
|
779 |
|
|
|
780 |
// Create an action factory.
|
|
|
781 |
$factory = new \core_calendar\action_factory();
|
|
|
782 |
|
|
|
783 |
// Decorate action event.
|
|
|
784 |
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory);
|
|
|
785 |
|
|
|
786 |
// Ensure result was null.
|
|
|
787 |
$this->assertNull($actionevent);
|
|
|
788 |
}
|
|
|
789 |
|
|
|
790 |
public function test_quiz_core_calendar_provide_event_action_already_completed_for_user() {
|
|
|
791 |
$this->resetAfterTest();
|
|
|
792 |
set_config('enablecompletion', 1);
|
|
|
793 |
$this->setAdminUser();
|
|
|
794 |
|
|
|
795 |
// Create the activity.
|
|
|
796 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
797 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id],
|
|
|
798 |
['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS]);
|
|
|
799 |
|
|
|
800 |
// Enrol a student in the course.
|
|
|
801 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
802 |
|
|
|
803 |
// Get some additional data.
|
|
|
804 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
|
|
805 |
|
|
|
806 |
// Create a calendar event.
|
|
|
807 |
$event = $this->create_action_event($course->id, $quiz->id,
|
|
|
808 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
809 |
|
|
|
810 |
// Mark the activity as completed for the student.
|
|
|
811 |
$completion = new \completion_info($course);
|
|
|
812 |
$completion->set_module_viewed($cm, $student->id);
|
|
|
813 |
|
|
|
814 |
// Create an action factory.
|
|
|
815 |
$factory = new \core_calendar\action_factory();
|
|
|
816 |
|
|
|
817 |
// Decorate action event for the student.
|
|
|
818 |
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
819 |
|
|
|
820 |
// Ensure result was null.
|
|
|
821 |
$this->assertNull($actionevent);
|
|
|
822 |
}
|
|
|
823 |
|
|
|
824 |
/**
|
|
|
825 |
* Creates an action event.
|
|
|
826 |
*
|
|
|
827 |
* @param int $courseid
|
|
|
828 |
* @param int $instanceid The quiz id.
|
|
|
829 |
* @param string $eventtype The event type. eg. QUIZ_EVENT_TYPE_OPEN.
|
|
|
830 |
* @return bool|calendar_event
|
|
|
831 |
*/
|
|
|
832 |
private function create_action_event($courseid, $instanceid, $eventtype) {
|
|
|
833 |
$event = new \stdClass();
|
|
|
834 |
$event->name = 'Calendar event';
|
|
|
835 |
$event->modulename = 'quiz';
|
|
|
836 |
$event->courseid = $courseid;
|
|
|
837 |
$event->instance = $instanceid;
|
|
|
838 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
839 |
$event->eventtype = $eventtype;
|
|
|
840 |
$event->timestart = time();
|
|
|
841 |
|
|
|
842 |
return \calendar_event::create($event);
|
|
|
843 |
}
|
|
|
844 |
|
|
|
845 |
/**
|
|
|
846 |
* Test the callback responsible for returning the completion rule descriptions.
|
|
|
847 |
* This function should work given either an instance of the module (cm_info), such as when checking the active rules,
|
|
|
848 |
* or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
|
|
|
849 |
*/
|
|
|
850 |
public function test_mod_quiz_completion_get_active_rule_descriptions() {
|
|
|
851 |
$this->resetAfterTest();
|
|
|
852 |
$this->setAdminUser();
|
|
|
853 |
|
|
|
854 |
// Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
|
|
|
855 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
|
|
|
856 |
$quiz1 = $this->getDataGenerator()->create_module('quiz', [
|
|
|
857 |
'course' => $course->id,
|
|
|
858 |
'completion' => 2,
|
|
|
859 |
'completionusegrade' => 1,
|
|
|
860 |
'completionpassgrade' => 1,
|
|
|
861 |
'completionattemptsexhausted' => 1,
|
|
|
862 |
]);
|
|
|
863 |
$quiz2 = $this->getDataGenerator()->create_module('quiz', [
|
|
|
864 |
'course' => $course->id,
|
|
|
865 |
'completion' => 2,
|
|
|
866 |
'completionusegrade' => 0
|
|
|
867 |
]);
|
|
|
868 |
$cm1 = \cm_info::create(get_coursemodule_from_instance('quiz', $quiz1->id));
|
|
|
869 |
$cm2 = \cm_info::create(get_coursemodule_from_instance('quiz', $quiz2->id));
|
|
|
870 |
|
|
|
871 |
// Data for the stdClass input type.
|
|
|
872 |
// This type of input would occur when checking the default completion rules for an activity type, where we don't have
|
|
|
873 |
// any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
|
|
|
874 |
$moddefaults = new \stdClass();
|
|
|
875 |
$moddefaults->customdata = ['customcompletionrules' => [
|
|
|
876 |
'completionattemptsexhausted' => 1,
|
|
|
877 |
]];
|
|
|
878 |
$moddefaults->completion = 2;
|
|
|
879 |
|
|
|
880 |
$activeruledescriptions = [
|
|
|
881 |
get_string('completionpassorattemptsexhausteddesc', 'quiz'),
|
|
|
882 |
];
|
|
|
883 |
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
|
|
|
884 |
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm2), []);
|
|
|
885 |
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
|
|
|
886 |
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions(new \stdClass()), []);
|
|
|
887 |
}
|
|
|
888 |
|
|
|
889 |
/**
|
|
|
890 |
* A user who does not have capabilities to add events to the calendar should be able to create a quiz.
|
|
|
891 |
*/
|
|
|
892 |
public function test_creation_with_no_calendar_capabilities() {
|
|
|
893 |
$this->resetAfterTest();
|
|
|
894 |
$course = self::getDataGenerator()->create_course();
|
|
|
895 |
$context = \context_course::instance($course->id);
|
|
|
896 |
$user = self::getDataGenerator()->create_and_enrol($course, 'editingteacher');
|
|
|
897 |
$roleid = self::getDataGenerator()->create_role();
|
|
|
898 |
self::getDataGenerator()->role_assign($roleid, $user->id, $context->id);
|
|
|
899 |
assign_capability('moodle/calendar:manageentries', CAP_PROHIBIT, $roleid, $context, true);
|
|
|
900 |
$generator = self::getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
901 |
// Create an instance as a user without the calendar capabilities.
|
|
|
902 |
$this->setUser($user);
|
|
|
903 |
$time = time();
|
|
|
904 |
$params = [
|
|
|
905 |
'course' => $course->id,
|
|
|
906 |
'timeopen' => $time + 200,
|
|
|
907 |
'timeclose' => $time + 2000,
|
|
|
908 |
];
|
|
|
909 |
$generator->create_instance($params);
|
|
|
910 |
}
|
|
|
911 |
|
|
|
912 |
/**
|
|
|
913 |
* Data provider for summarise_response() test cases.
|
|
|
914 |
*
|
|
|
915 |
* @return array List of data sets (test cases)
|
|
|
916 |
*/
|
|
|
917 |
public function mod_quiz_inplace_editable_provider(): array {
|
|
|
918 |
return [
|
|
|
919 |
'set to A1' => [1, 'A1'],
|
|
|
920 |
'set with HTML characters' => [2, 'A & & <-:'],
|
|
|
921 |
'set to integer' => [3, '3'],
|
|
|
922 |
'set to blank' => [4, ''],
|
|
|
923 |
'set with Unicode characters' => [1, 'L\'Aina Lluís^'],
|
|
|
924 |
'set with Unicode at the truncation point' => [1, '123456789012345碁'],
|
|
|
925 |
'set with HTML Char at the truncation point' => [1, '123456789012345>'],
|
|
|
926 |
];
|
|
|
927 |
}
|
|
|
928 |
|
|
|
929 |
/**
|
|
|
930 |
* Test customised and automated question numbering for a given slot number and customised value.
|
|
|
931 |
*
|
|
|
932 |
* @dataProvider mod_quiz_inplace_editable_provider
|
|
|
933 |
* @param int $slotnumber
|
|
|
934 |
* @param string $newvalue
|
|
|
935 |
* @covers ::mod_quiz_inplace_editable
|
|
|
936 |
*/
|
|
|
937 |
public function test_mod_quiz_inplace_editable(int $slotnumber, string $newvalue): void {
|
|
|
938 |
global $CFG;
|
|
|
939 |
require_once($CFG->dirroot . '/lib/external/externallib.php');
|
|
|
940 |
$this->resetAfterTest();
|
|
|
941 |
|
|
|
942 |
$this->setAdminUser();
|
|
|
943 |
$course = self::getDataGenerator()->create_course();
|
|
|
944 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'sumgrades' => 1]);
|
|
|
945 |
$cm = get_coursemodule_from_id('quiz', $quiz->cmid);
|
|
|
946 |
|
|
|
947 |
// Add few questions to the quiz.
|
|
|
948 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
949 |
$cat = $questiongenerator->create_question_category();
|
|
|
950 |
|
|
|
951 |
$question = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
|
|
|
952 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
953 |
|
|
|
954 |
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
955 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
956 |
|
|
|
957 |
$question = $questiongenerator->create_question('multichoice', null, ['category' => $cat->id]);
|
|
|
958 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
959 |
|
|
|
960 |
$question = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
961 |
quiz_add_quiz_question($question->id, $quiz);
|
|
|
962 |
|
|
|
963 |
// Create the quiz object.
|
|
|
964 |
$quizobj = new quiz_settings($quiz, $cm, $course);
|
|
|
965 |
$structure = $quizobj->get_structure();
|
|
|
966 |
|
|
|
967 |
$slots = $structure->get_slots();
|
|
|
968 |
$this->assertEquals(4, count($slots));
|
|
|
969 |
|
|
|
970 |
$slotid = $structure->get_slot_id_for_slot($slotnumber);
|
|
|
971 |
$inplaceeditable = mod_quiz_inplace_editable('slotdisplaynumber', $slotid, $newvalue);
|
|
|
972 |
$result = \core_external::update_inplace_editable('mod_quiz', 'slotdisplaynumber', $slotid, $newvalue);
|
|
|
973 |
$result = external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $result);
|
|
|
974 |
|
|
|
975 |
$this->assertEquals(count((array) $inplaceeditable), count($result));
|
|
|
976 |
$this->assertEquals($slotid, $result['itemid']);
|
|
|
977 |
if ($newvalue === '' || is_null($newvalue)) {
|
|
|
978 |
// Check against default.
|
|
|
979 |
$this->assertEquals($slotnumber, $result['displayvalue']);
|
|
|
980 |
$this->assertEquals($slotnumber, $result['value']);
|
|
|
981 |
} else {
|
|
|
982 |
// Check against the custom number.
|
|
|
983 |
$this->assertEquals(s($newvalue), $result['displayvalue']);
|
|
|
984 |
$this->assertEquals($newvalue, $result['value']);
|
|
|
985 |
}
|
|
|
986 |
}
|
|
|
987 |
}
|