Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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