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 |
* Contains the class containing unit tests for the quiz notify attempt manual grading completed cron task.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_quiz
|
|
|
21 |
* @copyright 2021 The Open University
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_quiz;
|
|
|
26 |
|
|
|
27 |
use advanced_testcase;
|
|
|
28 |
use context_module;
|
11 |
efrain |
29 |
use core\context;
|
1 |
efrain |
30 |
use mod_quiz\task\quiz_notify_attempt_manual_grading_completed;
|
|
|
31 |
use question_engine;
|
11 |
efrain |
32 |
use question_usage_by_activity;
|
1 |
efrain |
33 |
use stdClass;
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Class containing unit tests for the quiz notify attempt manual grading completed cron task.
|
|
|
38 |
*
|
|
|
39 |
* @package mod_quiz
|
|
|
40 |
* @copyright 2021 The Open University
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
*/
|
|
|
43 |
class quiz_notify_attempt_manual_grading_completed_test extends advanced_testcase {
|
11 |
efrain |
44 |
/** @var stdClass $course Test course to contain quiz. */
|
|
|
45 |
protected stdClass $course;
|
1 |
efrain |
46 |
|
11 |
efrain |
47 |
/** @var stdClass $quiz A test quiz. */
|
|
|
48 |
protected stdClass $quiz;
|
1 |
efrain |
49 |
|
|
|
50 |
/** @var context The quiz context. */
|
11 |
efrain |
51 |
protected context $context;
|
1 |
efrain |
52 |
|
|
|
53 |
/** @var stdClass The course_module. */
|
11 |
efrain |
54 |
protected stdClass $cm;
|
1 |
efrain |
55 |
|
|
|
56 |
/** @var stdClass The student test. */
|
11 |
efrain |
57 |
protected stdClass $student;
|
1 |
efrain |
58 |
|
11 |
efrain |
59 |
/** @var stdClass The student role. */
|
|
|
60 |
protected stdClass $studentrole;
|
1 |
efrain |
61 |
|
|
|
62 |
/** @var quiz_settings Object containing the quiz settings. */
|
11 |
efrain |
63 |
protected quiz_settings $quizobj;
|
1 |
efrain |
64 |
|
|
|
65 |
/** @var question_usage_by_activity The question usage for this quiz attempt. */
|
11 |
efrain |
66 |
protected question_usage_by_activity $quba;
|
1 |
efrain |
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Standard test setup.
|
|
|
70 |
*
|
11 |
efrain |
71 |
* Create a course with a quiz and a student.
|
|
|
72 |
* The quiz has a truefalse question and an essay question.
|
1 |
efrain |
73 |
*
|
|
|
74 |
* Also create some bits of a quiz attempt to be used later.
|
|
|
75 |
*/
|
|
|
76 |
public function setUp(): void {
|
|
|
77 |
global $DB;
|
|
|
78 |
|
|
|
79 |
$this->resetAfterTest();
|
|
|
80 |
$this->setAdminUser();
|
|
|
81 |
|
|
|
82 |
// Setup test data.
|
|
|
83 |
$this->course = $this->getDataGenerator()->create_course();
|
|
|
84 |
|
11 |
efrain |
85 |
// Create a user enrolled as a student.
|
1 |
efrain |
86 |
$this->student = self::getDataGenerator()->create_user();
|
11 |
efrain |
87 |
$this->studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
88 |
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id);
|
1 |
efrain |
89 |
|
|
|
90 |
// Make a quiz.
|
|
|
91 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
92 |
$this->quiz = $quizgenerator->create_instance(['course' => $this->course->id, 'questionsperpage' => 0,
|
|
|
93 |
'grade' => 100.0, 'sumgrades' => 2]);
|
11 |
efrain |
94 |
$this->context = context_module::instance($this->quiz->cmid);
|
|
|
95 |
$this->cm = get_coursemodule_from_instance('quiz', $this->quiz->id);
|
1 |
efrain |
96 |
|
|
|
97 |
// Create a truefalse question and an essay question.
|
11 |
efrain |
98 |
/** @var \core_question_generator $questiongenerator */
|
1 |
efrain |
99 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
100 |
$cat = $questiongenerator->create_question_category();
|
|
|
101 |
$truefalse = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
|
|
|
102 |
$essay = $questiongenerator->create_question('essay', null, ['category' => $cat->id]);
|
|
|
103 |
|
|
|
104 |
// Add them to the quiz.
|
|
|
105 |
quiz_add_quiz_question($truefalse->id, $this->quiz);
|
|
|
106 |
quiz_add_quiz_question($essay->id, $this->quiz);
|
|
|
107 |
|
|
|
108 |
$this->quizobj = quiz_settings::create($this->quiz->id);
|
|
|
109 |
$this->quba = question_engine::make_questions_usage_by_activity('mod_quiz', $this->quizobj->get_context());
|
|
|
110 |
$this->quba->set_preferred_behaviour($this->quizobj->get_quiz()->preferredbehaviour);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Test SQL querry get list attempt in condition.
|
|
|
115 |
*/
|
11 |
efrain |
116 |
public function test_get_list_of_attempts_within_conditions(): void {
|
1 |
efrain |
117 |
global $DB;
|
|
|
118 |
|
|
|
119 |
$timenow = time();
|
|
|
120 |
|
|
|
121 |
// Create an attempt to be completely graded (one hour ago).
|
11 |
efrain |
122 |
$attempt1 = quiz_create_attempt($this->quizobj, 1, false, $timenow - HOURSECS, false, $this->student->id);
|
1 |
efrain |
123 |
quiz_start_new_attempt($this->quizobj, $this->quba, $attempt1, 1, $timenow - HOURSECS);
|
|
|
124 |
quiz_attempt_save_started($this->quizobj, $this->quba, $attempt1);
|
|
|
125 |
|
|
|
126 |
// Process some responses from the student (30 mins ago) and submit (20 mins ago).
|
|
|
127 |
$attemptobj1 = quiz_attempt::create($attempt1->id);
|
|
|
128 |
$tosubmit = [2 => ['answer' => 'Student 1 answer', 'answerformat' => FORMAT_HTML]];
|
|
|
129 |
$attemptobj1->process_submitted_actions($timenow - 30 * MINSECS, false, $tosubmit);
|
|
|
130 |
$attemptobj1->process_finish($timenow - 20 * MINSECS, false);
|
|
|
131 |
|
|
|
132 |
// Finish the attempt of student (now).
|
|
|
133 |
$attemptobj1->get_question_usage()->manual_grade(2, 'Good!', 1, FORMAT_HTML);
|
|
|
134 |
question_engine::save_questions_usage_by_activity($attemptobj1->get_question_usage());
|
|
|
135 |
|
|
|
136 |
$update = new stdClass();
|
|
|
137 |
$update->id = $attemptobj1->get_attemptid();
|
|
|
138 |
$update->timemodified = $timenow;
|
|
|
139 |
$update->sumgrades = $attemptobj1->get_question_usage()->get_total_mark();
|
11 |
efrain |
140 |
$update->gradednotificationsenttime = null; // Processfinish detects the notification is not required, so sets this.
|
|
|
141 |
// Unset to allow testing of our logic.
|
1 |
efrain |
142 |
$DB->update_record('quiz_attempts', $update);
|
|
|
143 |
$attemptobj1->get_quizobj()->get_grade_calculator()->recompute_final_grade();
|
|
|
144 |
|
|
|
145 |
// Not quite time to send yet.
|
|
|
146 |
$task = new quiz_notify_attempt_manual_grading_completed();
|
|
|
147 |
$task->set_time_for_testing($timenow + 5 * HOURSECS - 1);
|
|
|
148 |
$attempts = $task->get_list_of_attempts();
|
|
|
149 |
$this->assertEquals(0, iterator_count($attempts));
|
|
|
150 |
|
|
|
151 |
// After time to send.
|
|
|
152 |
$task->set_time_for_testing($timenow + 5 * HOURSECS + 1);
|
|
|
153 |
$attempts = $task->get_list_of_attempts();
|
|
|
154 |
$this->assertEquals(1, iterator_count($attempts));
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Test SQL query does not return attempts if the grading is not complete yet.
|
|
|
159 |
*/
|
11 |
efrain |
160 |
public function test_get_list_of_attempts_without_manual_graded(): void {
|
1 |
efrain |
161 |
|
|
|
162 |
$timenow = time();
|
|
|
163 |
|
|
|
164 |
// Create an attempt which won't be graded (1 hour ago).
|
11 |
efrain |
165 |
$attempt2 = quiz_create_attempt($this->quizobj, 2, false, $timenow - HOURSECS, false, $this->student->id);
|
1 |
efrain |
166 |
quiz_start_new_attempt($this->quizobj, $this->quba, $attempt2, 2, $timenow - HOURSECS);
|
|
|
167 |
quiz_attempt_save_started($this->quizobj, $this->quba, $attempt2);
|
|
|
168 |
|
|
|
169 |
// Process some responses from the student (30 mins ago) and submit (now).
|
|
|
170 |
$attemptobj2 = quiz_attempt::create($attempt2->id);
|
|
|
171 |
$tosubmit = [2 => ['answer' => 'Answer of student 2.', 'answerformat' => FORMAT_HTML]];
|
|
|
172 |
$attemptobj2->process_submitted_actions($timenow - 30 * MINSECS, false, $tosubmit);
|
|
|
173 |
$attemptobj2->process_finish($timenow, false);
|
|
|
174 |
|
|
|
175 |
// After time to notify, except attempt not graded, so it won't appear.
|
|
|
176 |
$task = new quiz_notify_attempt_manual_grading_completed();
|
|
|
177 |
$task->set_time_for_testing($timenow + 5 * HOURSECS + 1);
|
|
|
178 |
$attempts = $task->get_list_of_attempts();
|
|
|
179 |
|
|
|
180 |
$this->assertEquals(0, iterator_count($attempts));
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Test notify manual grading completed task which the user attempt has not capability.
|
|
|
185 |
*/
|
11 |
efrain |
186 |
public function test_notify_manual_grading_completed_task_without_capability(): void {
|
1 |
efrain |
187 |
global $DB;
|
|
|
188 |
|
|
|
189 |
// Create an attempt for a user without the capability.
|
|
|
190 |
$timenow = time();
|
11 |
efrain |
191 |
$attempt = quiz_create_attempt($this->quizobj, 3, false, $timenow, false, $this->student->id);
|
1 |
efrain |
192 |
quiz_start_new_attempt($this->quizobj, $this->quba, $attempt, 3, $timenow - HOURSECS);
|
|
|
193 |
quiz_attempt_save_started($this->quizobj, $this->quba, $attempt);
|
|
|
194 |
|
|
|
195 |
// Process some responses and submit.
|
|
|
196 |
$attemptobj = quiz_attempt::create($attempt->id);
|
11 |
efrain |
197 |
$tosubmit = [2 => ['answer' => 'Essay answer.', 'answerformat' => FORMAT_HTML]];
|
1 |
efrain |
198 |
$attemptobj->process_submitted_actions($timenow - 30 * MINSECS, false, $tosubmit);
|
|
|
199 |
$attemptobj->process_finish($timenow - 20 * MINSECS, false);
|
|
|
200 |
|
|
|
201 |
// Grade the attempt.
|
|
|
202 |
$attemptobj->get_question_usage()->manual_grade(2, 'Good!', 1, FORMAT_HTML);
|
|
|
203 |
question_engine::save_questions_usage_by_activity($attemptobj->get_question_usage());
|
|
|
204 |
|
|
|
205 |
$update = new stdClass();
|
|
|
206 |
$update->id = $attemptobj->get_attemptid();
|
|
|
207 |
$update->timemodified = $timenow;
|
|
|
208 |
$update->sumgrades = $attemptobj->get_question_usage()->get_total_mark();
|
11 |
efrain |
209 |
$update->gradednotificationsenttime = null; // Processfinish detects the notification is not required, so sets this.
|
|
|
210 |
// Unset to allow testing of our logic.
|
1 |
efrain |
211 |
$DB->update_record('quiz_attempts', $update);
|
|
|
212 |
$attemptobj->get_quizobj()->get_grade_calculator()->recompute_final_grade();
|
|
|
213 |
|
|
|
214 |
// Run the quiz notify attempt manual graded task.
|
11 |
efrain |
215 |
$this->expectOutputRegex("~Not sending an email because user does not have mod/quiz:emailnotifyattemptgraded capability.~");
|
1 |
efrain |
216 |
$task = new quiz_notify_attempt_manual_grading_completed();
|
|
|
217 |
$task->set_time_for_testing($timenow + 5 * HOURSECS + 1);
|
|
|
218 |
$task->execute();
|
|
|
219 |
|
|
|
220 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
221 |
$this->assertEquals($attemptobj->get_attempt()->timefinish, $attemptobj->get_attempt()->gradednotificationsenttime);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Test notify manual grading completed task which the user attempt has capability.
|
|
|
226 |
*/
|
11 |
efrain |
227 |
public function test_notify_manual_grading_completed_task_with_capability(): void {
|
1 |
efrain |
228 |
global $DB;
|
|
|
229 |
|
11 |
efrain |
230 |
// Allow student to receive messages.
|
|
|
231 |
assign_capability('mod/quiz:emailnotifyattemptgraded', CAP_ALLOW, $this->studentrole->id, $this->context, true);
|
|
|
232 |
|
1 |
efrain |
233 |
// Create an attempt with capability.
|
|
|
234 |
$timenow = time();
|
11 |
efrain |
235 |
$attempt = quiz_create_attempt($this->quizobj, 4, false, $timenow, false, $this->student->id);
|
1 |
efrain |
236 |
quiz_start_new_attempt($this->quizobj, $this->quba, $attempt, 4, $timenow - HOURSECS);
|
|
|
237 |
quiz_attempt_save_started($this->quizobj, $this->quba, $attempt);
|
|
|
238 |
|
|
|
239 |
// Process some responses from the student.
|
|
|
240 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
241 |
$tosubmit = [2 => ['answer' => 'Answer of student.', 'answerformat' => FORMAT_HTML]];
|
|
|
242 |
$attemptobj->process_submitted_actions($timenow - 30 * MINSECS, false, $tosubmit);
|
|
|
243 |
$attemptobj->process_finish($timenow - 20 * MINSECS, false);
|
|
|
244 |
|
|
|
245 |
// Finish the attempt of student.
|
|
|
246 |
$attemptobj->get_question_usage()->manual_grade(2, 'Good!', 1, FORMAT_HTML);
|
|
|
247 |
question_engine::save_questions_usage_by_activity($attemptobj->get_question_usage());
|
|
|
248 |
|
|
|
249 |
$update = new stdClass();
|
|
|
250 |
$update->id = $attemptobj->get_attemptid();
|
|
|
251 |
$update->timemodified = $timenow;
|
|
|
252 |
$update->sumgrades = $attemptobj->get_question_usage()->get_total_mark();
|
|
|
253 |
$DB->update_record('quiz_attempts', $update);
|
|
|
254 |
$attemptobj->get_quizobj()->get_grade_calculator()->recompute_final_grade();
|
|
|
255 |
|
|
|
256 |
// Run the quiz notify attempt manual graded task.
|
11 |
efrain |
257 |
$this->expectOutputRegex("~Sending email to user {$this->student->id}~");
|
1 |
efrain |
258 |
$task = new quiz_notify_attempt_manual_grading_completed();
|
|
|
259 |
$task->set_time_for_testing($timenow + 5 * HOURSECS + 1);
|
|
|
260 |
$task->execute();
|
|
|
261 |
|
|
|
262 |
$attemptobj = quiz_attempt::create($attempt->id);
|
|
|
263 |
|
|
|
264 |
$this->assertNotEquals(null, $attemptobj->get_attempt()->gradednotificationsenttime);
|
|
|
265 |
$this->assertNotEquals($attemptobj->get_attempt()->timefinish, $attemptobj->get_attempt()->gradednotificationsenttime);
|
|
|
266 |
}
|
|
|
267 |
}
|