1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace mod_quiz;
|
|
|
18 |
|
|
|
19 |
use moodle_url;
|
|
|
20 |
use question_bank;
|
|
|
21 |
use question_engine;
|
|
|
22 |
use mod_quiz\question\bank\qbank_helper;
|
|
|
23 |
|
|
|
24 |
defined('MOODLE_INTERNAL') || die();
|
|
|
25 |
|
|
|
26 |
global $CFG;
|
|
|
27 |
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
|
|
28 |
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Quiz attempt walk through.
|
|
|
32 |
*
|
|
|
33 |
* @package mod_quiz
|
|
|
34 |
* @category test
|
|
|
35 |
* @copyright 2013 The Open University
|
|
|
36 |
* @author Jamie Pratt <me@jamiep.org>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
* @covers \mod_quiz\quiz_attempt
|
|
|
39 |
*/
|
|
|
40 |
class attempt_walkthrough_test extends \advanced_testcase {
|
|
|
41 |
|
|
|
42 |
use \quiz_question_helper_test_trait;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Create a quiz with questions and walk through a quiz attempt.
|
|
|
46 |
*/
|
|
|
47 |
public function test_quiz_attempt_walkthrough() {
|
|
|
48 |
global $SITE;
|
|
|
49 |
|
|
|
50 |
$this->resetAfterTest(true);
|
|
|
51 |
|
|
|
52 |
// Make a quiz.
|
|
|
53 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
54 |
|
|
|
55 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0,
|
|
|
56 |
'sumgrades' => 3]);
|
|
|
57 |
|
|
|
58 |
// Create a couple of questions.
|
|
|
59 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
60 |
|
|
|
61 |
$cat = $questiongenerator->create_question_category();
|
|
|
62 |
$saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
63 |
$numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
64 |
$matchq = $questiongenerator->create_question('match', null, ['category' => $cat->id]);
|
|
|
65 |
$description = $questiongenerator->create_question('description', null, ['category' => $cat->id]);
|
|
|
66 |
|
|
|
67 |
// Add them to the quiz.
|
|
|
68 |
quiz_add_quiz_question($saq->id, $quiz);
|
|
|
69 |
quiz_add_quiz_question($numq->id, $quiz);
|
|
|
70 |
quiz_add_quiz_question($matchq->id, $quiz);
|
|
|
71 |
quiz_add_quiz_question($description->id, $quiz);
|
|
|
72 |
|
|
|
73 |
// Make a user to do the quiz.
|
|
|
74 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
75 |
|
|
|
76 |
$quizobj = quiz_settings::create($quiz->id, $user1->id);
|
|
|
77 |
|
|
|
78 |
// Start the attempt.
|
|
|
79 |
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
80 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
81 |
|
|
|
82 |
$timenow = time();
|
|
|
83 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id);
|
|
|
84 |
|
|
|
85 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
|
|
86 |
$this->assertEquals('1,2,3,4,0', $attempt->layout);
|
|
|
87 |
|
|
|
88 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
89 |
|
|
|
90 |
// Process some responses from the student.
|
|
|
91 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
92 |
$this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
93 |
// The student has not answered any questions.
|
|
|
94 |
$this->assertEquals(3, $attemptobj->get_number_of_unanswered_questions());
|
|
|
95 |
|
|
|
96 |
$tosubmit = [1 => ['answer' => 'frog'],
|
|
|
97 |
2 => ['answer' => '3.14']];
|
|
|
98 |
|
|
|
99 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
100 |
// The student has answered two questions, and only one remaining.
|
|
|
101 |
$this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions());
|
|
|
102 |
|
|
|
103 |
$tosubmit = [
|
|
|
104 |
3 => [
|
|
|
105 |
'frog' => 'amphibian',
|
|
|
106 |
'cat' => 'mammal',
|
|
|
107 |
'newt' => ''
|
|
|
108 |
]
|
|
|
109 |
];
|
|
|
110 |
|
|
|
111 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
112 |
// The student has answered three questions but one is invalid, so there is still one remaining.
|
|
|
113 |
$this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions());
|
|
|
114 |
|
|
|
115 |
$tosubmit = [
|
|
|
116 |
3 => [
|
|
|
117 |
'frog' => 'amphibian',
|
|
|
118 |
'cat' => 'mammal',
|
|
|
119 |
'newt' => 'amphibian'
|
|
|
120 |
]
|
|
|
121 |
];
|
|
|
122 |
|
|
|
123 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
124 |
// The student has answered three questions, so there are no remaining.
|
|
|
125 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
126 |
|
|
|
127 |
// Finish the attempt.
|
|
|
128 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
129 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
130 |
$attemptobj->process_finish($timenow, false);
|
|
|
131 |
|
|
|
132 |
// Re-load quiz attempt data.
|
|
|
133 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
134 |
|
|
|
135 |
// Check that results are stored as expected.
|
|
|
136 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
137 |
$this->assertEquals(3, $attemptobj->get_sum_marks());
|
|
|
138 |
$this->assertEquals(true, $attemptobj->is_finished());
|
|
|
139 |
$this->assertEquals($timenow, $attemptobj->get_submitted_date());
|
|
|
140 |
$this->assertEquals($user1->id, $attemptobj->get_userid());
|
|
|
141 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
142 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
143 |
|
|
|
144 |
// Check quiz grades.
|
|
|
145 |
$grades = quiz_get_user_grades($quiz, $user1->id);
|
|
|
146 |
$grade = array_shift($grades);
|
|
|
147 |
$this->assertEquals(100.0, $grade->rawgrade);
|
|
|
148 |
|
|
|
149 |
// Check grade book.
|
|
|
150 |
$gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
|
|
|
151 |
$gradebookitem = array_shift($gradebookgrades->items);
|
|
|
152 |
$gradebookgrade = array_shift($gradebookitem->grades);
|
|
|
153 |
$this->assertEquals(100, $gradebookgrade->grade);
|
|
|
154 |
|
|
|
155 |
// Update question in quiz.
|
|
|
156 |
$newsa = $questiongenerator->update_question($saq, null,
|
|
|
157 |
['name' => 'This is the second version of shortanswer']);
|
|
|
158 |
$newnumbq = $questiongenerator->update_question($numq, null,
|
|
|
159 |
['name' => 'This is the second version of numerical']);
|
|
|
160 |
$newmatch = $questiongenerator->update_question($matchq, null,
|
|
|
161 |
['name' => 'This is the second version of match']);
|
|
|
162 |
$newdescription = $questiongenerator->update_question($description, null,
|
|
|
163 |
['name' => 'This is the second version of description']);
|
|
|
164 |
|
|
|
165 |
// Update the attempt to use this questions.
|
|
|
166 |
// Would not normally be done for a non-preview, but this is just a unit test.
|
|
|
167 |
$attemptobj->update_questions_to_new_version_if_changed();
|
|
|
168 |
|
|
|
169 |
// Verify.
|
|
|
170 |
$this->assertEquals($newsa->id, $attemptobj->get_question_attempt(1)->get_question_id());
|
|
|
171 |
$this->assertEquals($newnumbq->id, $attemptobj->get_question_attempt(2)->get_question_id());
|
|
|
172 |
$this->assertEquals($newmatch->id, $attemptobj->get_question_attempt(3)->get_question_id());
|
|
|
173 |
$this->assertEquals($newdescription->id, $attemptobj->get_question_attempt(4)->get_question_id());
|
|
|
174 |
|
|
|
175 |
// Repeat the checks from above.
|
|
|
176 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
177 |
$this->assertEquals(3, $attemptobj->get_sum_marks());
|
|
|
178 |
$this->assertEquals(true, $attemptobj->is_finished());
|
|
|
179 |
$this->assertEquals($timenow, $attemptobj->get_submitted_date());
|
|
|
180 |
$this->assertEquals($user1->id, $attemptobj->get_userid());
|
|
|
181 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
182 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
183 |
|
|
|
184 |
// Re-load quiz attempt data and repeat the verification.
|
|
|
185 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
186 |
|
|
|
187 |
$this->assertEquals($newsa->id, $attemptobj->get_question_attempt(1)->get_question_id());
|
|
|
188 |
$this->assertEquals($newnumbq->id, $attemptobj->get_question_attempt(2)->get_question_id());
|
|
|
189 |
$this->assertEquals($newmatch->id, $attemptobj->get_question_attempt(3)->get_question_id());
|
|
|
190 |
$this->assertEquals($newdescription->id, $attemptobj->get_question_attempt(4)->get_question_id());
|
|
|
191 |
|
|
|
192 |
// Repeat the checks from above.
|
|
|
193 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
194 |
$this->assertEquals(3, $attemptobj->get_sum_marks());
|
|
|
195 |
$this->assertEquals(true, $attemptobj->is_finished());
|
|
|
196 |
$this->assertEquals($timenow, $attemptobj->get_submitted_date());
|
|
|
197 |
$this->assertEquals($user1->id, $attemptobj->get_userid());
|
|
|
198 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
199 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Create a quiz containing one question and a close time.
|
|
|
204 |
*
|
|
|
205 |
* The question is the standard shortanswer test question.
|
|
|
206 |
* The quiz is set to close 1 hour from now.
|
|
|
207 |
* The quiz is set to use a grade period of 1 hour once time expires.
|
|
|
208 |
*
|
|
|
209 |
* @param string $overduehandling value for the overduehandling quiz setting.
|
|
|
210 |
* @return \stdClass the quiz that was created.
|
|
|
211 |
*/
|
|
|
212 |
protected function create_quiz_with_one_question(string $overduehandling = 'graceperiod'): \stdClass {
|
|
|
213 |
global $SITE;
|
|
|
214 |
$this->resetAfterTest();
|
|
|
215 |
|
|
|
216 |
// Make a quiz.
|
|
|
217 |
$timeclose = time() + HOURSECS;
|
|
|
218 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
219 |
|
|
|
220 |
$quiz = $quizgenerator->create_instance(
|
|
|
221 |
['course' => $SITE->id, 'timeclose' => $timeclose,
|
|
|
222 |
'overduehandling' => $overduehandling, 'graceperiod' => HOURSECS]);
|
|
|
223 |
|
|
|
224 |
// Create a question.
|
|
|
225 |
/** @var \core_question_generator $questiongenerator */
|
|
|
226 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
227 |
$cat = $questiongenerator->create_question_category();
|
|
|
228 |
$saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
229 |
|
|
|
230 |
// Add them to the quiz.
|
|
|
231 |
$quizobj = quiz_settings::create($quiz->id);
|
|
|
232 |
quiz_add_quiz_question($saq->id, $quiz, 0, 1);
|
|
|
233 |
$quizobj->get_grade_calculator()->recompute_quiz_sumgrades();
|
|
|
234 |
|
|
|
235 |
return $quiz;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
public function test_quiz_attempt_walkthrough_submit_time_recorded_correctly_when_overdue() {
|
|
|
239 |
|
|
|
240 |
$quiz = $this->create_quiz_with_one_question();
|
|
|
241 |
|
|
|
242 |
// Make a user to do the quiz.
|
|
|
243 |
$user = $this->getDataGenerator()->create_user();
|
|
|
244 |
$this->setUser($user);
|
|
|
245 |
$quizobj = quiz_settings::create($quiz->id, $user->id);
|
|
|
246 |
|
|
|
247 |
// Start the attempt.
|
|
|
248 |
$attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
|
|
|
249 |
|
|
|
250 |
// Process some responses from the student.
|
|
|
251 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
252 |
$this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions());
|
|
|
253 |
$attemptobj->process_submitted_actions($quiz->timeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]);
|
|
|
254 |
|
|
|
255 |
// Attempt goes overdue (e.g. if cron ran).
|
|
|
256 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
257 |
$attemptobj->process_going_overdue($quiz->timeclose + 2 * get_config('quiz', 'graceperiodmin'), false);
|
|
|
258 |
|
|
|
259 |
// Verify the attempt state.
|
|
|
260 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
261 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
262 |
$this->assertEquals(false, $attemptobj->is_finished());
|
|
|
263 |
$this->assertEquals(0, $attemptobj->get_submitted_date());
|
|
|
264 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
265 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
266 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
267 |
|
|
|
268 |
// Student submits the attempt during the grace period.
|
|
|
269 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
270 |
$attemptobj->process_attempt($quiz->timeclose + 30 * MINSECS, true, false, 1);
|
|
|
271 |
|
|
|
272 |
// Verify the attempt state.
|
|
|
273 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
274 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
275 |
$this->assertEquals(true, $attemptobj->is_finished());
|
|
|
276 |
$this->assertEquals($quiz->timeclose + 30 * MINSECS, $attemptobj->get_submitted_date());
|
|
|
277 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
278 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
279 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
public function test_quiz_attempt_walkthrough_close_time_extended_at_last_minute() {
|
|
|
283 |
global $DB;
|
|
|
284 |
|
|
|
285 |
$quiz = $this->create_quiz_with_one_question();
|
|
|
286 |
$originaltimeclose = $quiz->timeclose;
|
|
|
287 |
|
|
|
288 |
// Make a user to do the quiz.
|
|
|
289 |
$user = $this->getDataGenerator()->create_user();
|
|
|
290 |
$this->setUser($user);
|
|
|
291 |
$quizobj = quiz_settings::create($quiz->id, $user->id);
|
|
|
292 |
|
|
|
293 |
// Start the attempt.
|
|
|
294 |
$attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
|
|
|
295 |
|
|
|
296 |
// Process some responses from the student during the attempt.
|
|
|
297 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
298 |
$attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]);
|
|
|
299 |
|
|
|
300 |
// Teacher edits the quiz to extend the time-limit by one minute.
|
|
|
301 |
$DB->set_field('quiz', 'timeclose', $originaltimeclose + MINSECS, ['id' => $quiz->id]);
|
|
|
302 |
\course_modinfo::clear_instance_cache($quiz->course);
|
|
|
303 |
|
|
|
304 |
// Timer expires in the student browser and thinks it is time to submit the quiz.
|
|
|
305 |
// This sets $finishattempt to false - since the student did not click the button, and $timeup to true.
|
|
|
306 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
307 |
$attemptobj->process_attempt($originaltimeclose, false, true, 1);
|
|
|
308 |
|
|
|
309 |
// Verify the attempt state - the $timeup was ignored becuase things have changed server-side.
|
|
|
310 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
311 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
312 |
$this->assertFalse($attemptobj->is_finished());
|
|
|
313 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attemptobj->get_state());
|
|
|
314 |
$this->assertEquals(0, $attemptobj->get_submitted_date());
|
|
|
315 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Create a quiz with a random as well as other questions and walk through quiz attempts.
|
|
|
320 |
*/
|
|
|
321 |
public function test_quiz_with_random_question_attempt_walkthrough() {
|
|
|
322 |
global $SITE;
|
|
|
323 |
|
|
|
324 |
$this->resetAfterTest(true);
|
|
|
325 |
question_bank::get_qtype('random')->clear_caches_before_testing();
|
|
|
326 |
|
|
|
327 |
$this->setAdminUser();
|
|
|
328 |
|
|
|
329 |
// Make a quiz.
|
|
|
330 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
331 |
|
|
|
332 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 2, 'grade' => 100.0,
|
|
|
333 |
'sumgrades' => 4]);
|
|
|
334 |
|
|
|
335 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
336 |
|
|
|
337 |
// Add two questions to question category.
|
|
|
338 |
$cat = $questiongenerator->create_question_category();
|
|
|
339 |
$saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
|
|
340 |
$numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
|
|
|
341 |
|
|
|
342 |
// Add random question to the quiz.
|
|
|
343 |
$this->add_random_questions($quiz->id, 0, $cat->id, 1);
|
|
|
344 |
|
|
|
345 |
// Make another category.
|
|
|
346 |
$cat2 = $questiongenerator->create_question_category();
|
|
|
347 |
$match = $questiongenerator->create_question('match', null, ['category' => $cat->id]);
|
|
|
348 |
|
|
|
349 |
quiz_add_quiz_question($match->id, $quiz, 0);
|
|
|
350 |
|
|
|
351 |
$multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', ['category' => $cat->id]);
|
|
|
352 |
|
|
|
353 |
quiz_add_quiz_question($multichoicemulti->id, $quiz, 0);
|
|
|
354 |
|
|
|
355 |
$multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', ['category' => $cat->id]);
|
|
|
356 |
|
|
|
357 |
quiz_add_quiz_question($multichoicesingle->id, $quiz, 0);
|
|
|
358 |
|
|
|
359 |
foreach ([$saq->id => 'frog', $numq->id => '3.14'] as $randomqidtoselect => $randqanswer) {
|
|
|
360 |
// Make a new user to do the quiz each loop.
|
|
|
361 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
362 |
$this->setUser($user1);
|
|
|
363 |
|
|
|
364 |
$quizobj = quiz_settings::create($quiz->id, $user1->id);
|
|
|
365 |
|
|
|
366 |
// Start the attempt.
|
|
|
367 |
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
368 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
369 |
|
|
|
370 |
$timenow = time();
|
|
|
371 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
|
|
|
372 |
|
|
|
373 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, [1 => $randomqidtoselect]);
|
|
|
374 |
$this->assertEquals('1,2,0,3,4,0', $attempt->layout);
|
|
|
375 |
|
|
|
376 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
377 |
|
|
|
378 |
// Process some responses from the student.
|
|
|
379 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
380 |
$this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
381 |
$this->assertEquals(4, $attemptobj->get_number_of_unanswered_questions());
|
|
|
382 |
|
|
|
383 |
$tosubmit = [];
|
|
|
384 |
$selectedquestionid = $quba->get_question_attempt(1)->get_question_id();
|
|
|
385 |
$tosubmit[1] = ['answer' => $randqanswer];
|
|
|
386 |
$tosubmit[2] = [
|
|
|
387 |
'frog' => 'amphibian',
|
|
|
388 |
'cat' => 'mammal',
|
|
|
389 |
'newt' => 'amphibian'];
|
|
|
390 |
$tosubmit[3] = ['One' => '1', 'Two' => '0', 'Three' => '1', 'Four' => '0']; // First and third choice.
|
|
|
391 |
$tosubmit[4] = ['answer' => 'One']; // The first choice.
|
|
|
392 |
|
|
|
393 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
394 |
|
|
|
395 |
// Finish the attempt.
|
|
|
396 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
397 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
398 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
399 |
$attemptobj->process_finish($timenow, false);
|
|
|
400 |
|
|
|
401 |
// Re-load quiz attempt data.
|
|
|
402 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
403 |
|
|
|
404 |
// Check that results are stored as expected.
|
|
|
405 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
406 |
$this->assertEquals(4, $attemptobj->get_sum_marks());
|
|
|
407 |
$this->assertEquals(true, $attemptobj->is_finished());
|
|
|
408 |
$this->assertEquals($timenow, $attemptobj->get_submitted_date());
|
|
|
409 |
$this->assertEquals($user1->id, $attemptobj->get_userid());
|
|
|
410 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
411 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
412 |
|
|
|
413 |
// Check quiz grades.
|
|
|
414 |
$grades = quiz_get_user_grades($quiz, $user1->id);
|
|
|
415 |
$grade = array_shift($grades);
|
|
|
416 |
$this->assertEquals(100.0, $grade->rawgrade);
|
|
|
417 |
|
|
|
418 |
// Check grade book.
|
|
|
419 |
$gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
|
|
|
420 |
$gradebookitem = array_shift($gradebookgrades->items);
|
|
|
421 |
$gradebookgrade = array_shift($gradebookitem->grades);
|
|
|
422 |
$this->assertEquals(100, $gradebookgrade->grade);
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
public function get_correct_response_for_variants() {
|
|
|
428 |
return [[1, 9.9], [2, 8.5], [5, 14.2], [10, 6.8, true]];
|
|
|
429 |
}
|
|
|
430 |
|
|
|
431 |
protected $quizwithvariants = null;
|
|
|
432 |
|
|
|
433 |
/**
|
|
|
434 |
* Create a quiz with a single question with variants and walk through quiz attempts.
|
|
|
435 |
*
|
|
|
436 |
* @dataProvider get_correct_response_for_variants
|
|
|
437 |
*/
|
|
|
438 |
public function test_quiz_with_question_with_variants_attempt_walkthrough($variantno, $correctresponse, $done = false) {
|
|
|
439 |
global $SITE;
|
|
|
440 |
|
|
|
441 |
$this->resetAfterTest($done);
|
|
|
442 |
|
|
|
443 |
$this->setAdminUser();
|
|
|
444 |
|
|
|
445 |
if ($this->quizwithvariants === null) {
|
|
|
446 |
// Make a quiz.
|
|
|
447 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
448 |
|
|
|
449 |
$this->quizwithvariants = $quizgenerator->create_instance(['course' => $SITE->id,
|
|
|
450 |
'questionsperpage' => 0,
|
|
|
451 |
'grade' => 100.0,
|
|
|
452 |
'sumgrades' => 1]);
|
|
|
453 |
|
|
|
454 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
455 |
|
|
|
456 |
$cat = $questiongenerator->create_question_category();
|
|
|
457 |
$calc = $questiongenerator->create_question('calculatedsimple', 'sumwithvariants', ['category' => $cat->id]);
|
|
|
458 |
quiz_add_quiz_question($calc->id, $this->quizwithvariants, 0);
|
|
|
459 |
}
|
|
|
460 |
|
|
|
461 |
|
|
|
462 |
// Make a new user to do the quiz.
|
|
|
463 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
464 |
$this->setUser($user1);
|
|
|
465 |
$quizobj = quiz_settings::create($this->quizwithvariants->id, $user1->id);
|
|
|
466 |
|
|
|
467 |
// Start the attempt.
|
|
|
468 |
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
469 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
470 |
|
|
|
471 |
$timenow = time();
|
|
|
472 |
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
|
|
|
473 |
|
|
|
474 |
// Select variant.
|
|
|
475 |
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, [], [1 => $variantno]);
|
|
|
476 |
$this->assertEquals('1,0', $attempt->layout);
|
|
|
477 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
478 |
|
|
|
479 |
// Process some responses from the student.
|
|
|
480 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
481 |
$this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
482 |
$this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions());
|
|
|
483 |
|
|
|
484 |
$tosubmit = [1 => ['answer' => $correctresponse]];
|
|
|
485 |
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
|
|
486 |
|
|
|
487 |
// Finish the attempt.
|
|
|
488 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
489 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
490 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
491 |
|
|
|
492 |
$attemptobj->process_finish($timenow, false);
|
|
|
493 |
|
|
|
494 |
// Re-load quiz attempt data.
|
|
|
495 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
496 |
|
|
|
497 |
// Check that results are stored as expected.
|
|
|
498 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
499 |
$this->assertEquals(1, $attemptobj->get_sum_marks());
|
|
|
500 |
$this->assertEquals(true, $attemptobj->is_finished());
|
|
|
501 |
$this->assertEquals($timenow, $attemptobj->get_submitted_date());
|
|
|
502 |
$this->assertEquals($user1->id, $attemptobj->get_userid());
|
|
|
503 |
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
|
|
504 |
$this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions());
|
|
|
505 |
|
|
|
506 |
// Check quiz grades.
|
|
|
507 |
$grades = quiz_get_user_grades($this->quizwithvariants, $user1->id);
|
|
|
508 |
$grade = array_shift($grades);
|
|
|
509 |
$this->assertEquals(100.0, $grade->rawgrade);
|
|
|
510 |
|
|
|
511 |
// Check grade book.
|
|
|
512 |
$gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $this->quizwithvariants->id, $user1->id);
|
|
|
513 |
$gradebookitem = array_shift($gradebookgrades->items);
|
|
|
514 |
$gradebookgrade = array_shift($gradebookitem->grades);
|
|
|
515 |
$this->assertEquals(100, $gradebookgrade->grade);
|
|
|
516 |
}
|
|
|
517 |
|
|
|
518 |
public function test_quiz_attempt_walkthrough_abandoned_attempt_reopened_with_timelimit_override() {
|
|
|
519 |
global $DB;
|
|
|
520 |
|
|
|
521 |
$quiz = $this->create_quiz_with_one_question('autoabandon');
|
|
|
522 |
$originaltimeclose = $quiz->timeclose;
|
|
|
523 |
|
|
|
524 |
// Make a user to do the quiz.
|
|
|
525 |
$user = $this->getDataGenerator()->create_user();
|
|
|
526 |
$this->setUser($user);
|
|
|
527 |
$quizobj = quiz_settings::create($quiz->id, $user->id);
|
|
|
528 |
|
|
|
529 |
// Start the attempt.
|
|
|
530 |
$attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
|
|
|
531 |
|
|
|
532 |
// Process some responses from the student during the attempt.
|
|
|
533 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
534 |
$attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]);
|
|
|
535 |
|
|
|
536 |
// Student leaves, so cron closes the attempt when time expires.
|
|
|
537 |
$attemptobj->process_abandon($originaltimeclose + 5 * MINSECS, false);
|
|
|
538 |
|
|
|
539 |
// Verify the attempt state.
|
|
|
540 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
541 |
$this->assertEquals(quiz_attempt::ABANDONED, $attemptobj->get_state());
|
|
|
542 |
$this->assertEquals(0, $attemptobj->get_submitted_date());
|
|
|
543 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
544 |
|
|
|
545 |
// The teacher feels kind, so adds an override for the student, and re-opens the attempt.
|
|
|
546 |
$sink = $this->redirectEvents();
|
|
|
547 |
$overriddentimeclose = $originaltimeclose + HOURSECS;
|
|
|
548 |
$DB->insert_record('quiz_overrides', [
|
|
|
549 |
'quiz' => $quiz->id,
|
|
|
550 |
'userid' => $user->id,
|
|
|
551 |
'timeclose' => $overriddentimeclose,
|
|
|
552 |
]);
|
|
|
553 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
554 |
$reopentime = $originaltimeclose + 10 * MINSECS;
|
|
|
555 |
$attemptobj->process_reopen_abandoned($reopentime);
|
|
|
556 |
|
|
|
557 |
// Verify the attempt state.
|
|
|
558 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
559 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
560 |
$this->assertFalse($attemptobj->is_finished());
|
|
|
561 |
$this->assertEquals(quiz_attempt::IN_PROGRESS, $attemptobj->get_state());
|
|
|
562 |
$this->assertEquals(0, $attemptobj->get_submitted_date());
|
|
|
563 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
564 |
$this->assertEquals($overriddentimeclose,
|
|
|
565 |
$attemptobj->get_access_manager($reopentime)->get_end_time($attemptobj->get_attempt()));
|
|
|
566 |
|
|
|
567 |
// Verify this was logged correctly.
|
|
|
568 |
$events = $sink->get_events();
|
|
|
569 |
$this->assertCount(1, $events);
|
|
|
570 |
|
|
|
571 |
$reopenedevent = array_shift($events);
|
|
|
572 |
$this->assertInstanceOf('\mod_quiz\event\attempt_reopened', $reopenedevent);
|
|
|
573 |
$this->assertEquals($attemptobj->get_context(), $reopenedevent->get_context());
|
|
|
574 |
$this->assertEquals(new moodle_url('/mod/quiz/review.php', ['attempt' => $attemptobj->get_attemptid()]),
|
|
|
575 |
$reopenedevent->get_url());
|
|
|
576 |
}
|
|
|
577 |
|
|
|
578 |
public function test_quiz_attempt_walkthrough_abandoned_attempt_reopened_after_close_time() {
|
|
|
579 |
$quiz = $this->create_quiz_with_one_question('autoabandon');
|
|
|
580 |
$originaltimeclose = $quiz->timeclose;
|
|
|
581 |
|
|
|
582 |
// Make a user to do the quiz.
|
|
|
583 |
$user = $this->getDataGenerator()->create_user();
|
|
|
584 |
$this->setUser($user);
|
|
|
585 |
$quizobj = quiz_settings::create($quiz->id, $user->id);
|
|
|
586 |
|
|
|
587 |
// Start the attempt.
|
|
|
588 |
$attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
|
|
|
589 |
|
|
|
590 |
// Process some responses from the student during the attempt.
|
|
|
591 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
592 |
$attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]);
|
|
|
593 |
|
|
|
594 |
// Student leaves, so cron closes the attempt when time expires.
|
|
|
595 |
$attemptobj->process_abandon($originaltimeclose + 5 * MINSECS, false);
|
|
|
596 |
|
|
|
597 |
// Verify the attempt state.
|
|
|
598 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
599 |
$this->assertEquals(quiz_attempt::ABANDONED, $attemptobj->get_state());
|
|
|
600 |
$this->assertEquals(0, $attemptobj->get_submitted_date());
|
|
|
601 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
602 |
|
|
|
603 |
// The teacher reopens the attempt without granting more time, so previously submitted responess are graded.
|
|
|
604 |
$sink = $this->redirectEvents();
|
|
|
605 |
$reopentime = $originaltimeclose + 10 * MINSECS;
|
|
|
606 |
$attemptobj->process_reopen_abandoned($reopentime);
|
|
|
607 |
|
|
|
608 |
// Verify the attempt state.
|
|
|
609 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
610 |
$this->assertEquals(1, $attemptobj->get_attempt_number());
|
|
|
611 |
$this->assertTrue($attemptobj->is_finished());
|
|
|
612 |
$this->assertEquals(quiz_attempt::FINISHED, $attemptobj->get_state());
|
|
|
613 |
$this->assertEquals($originaltimeclose, $attemptobj->get_submitted_date());
|
|
|
614 |
$this->assertEquals($user->id, $attemptobj->get_userid());
|
|
|
615 |
$this->assertEquals(1, $attemptobj->get_sum_marks());
|
|
|
616 |
|
|
|
617 |
// Verify this was logged correctly - there are some gradebook events between the two we want to check.
|
|
|
618 |
$events = $sink->get_events();
|
|
|
619 |
$this->assertGreaterThanOrEqual(2, $events);
|
|
|
620 |
|
|
|
621 |
$reopenedevent = array_shift($events);
|
|
|
622 |
$this->assertInstanceOf('\mod_quiz\event\attempt_reopened', $reopenedevent);
|
|
|
623 |
$this->assertEquals($attemptobj->get_context(), $reopenedevent->get_context());
|
|
|
624 |
$this->assertEquals(new moodle_url('/mod/quiz/review.php', ['attempt' => $attemptobj->get_attemptid()]),
|
|
|
625 |
$reopenedevent->get_url());
|
|
|
626 |
|
|
|
627 |
$submittedevent = array_pop($events);
|
|
|
628 |
$this->assertInstanceOf('\mod_quiz\event\attempt_submitted', $submittedevent);
|
|
|
629 |
$this->assertEquals($attemptobj->get_context(), $submittedevent->get_context());
|
|
|
630 |
$this->assertEquals(new moodle_url('/mod/quiz/review.php', ['attempt' => $attemptobj->get_attemptid()]),
|
|
|
631 |
$submittedevent->get_url());
|
|
|
632 |
}
|
|
|
633 |
}
|