Commit | Line | Data |
---|---|---|
47f48152 DW |
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/assign/locallib.php. | |
19 | * | |
20 | * @package mod_assign | |
21 | * @category phpunit | |
22 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | require_once($CFG->dirroot . '/mod/assign/locallib.php'); | |
31 | require_once($CFG->dirroot . '/mod/assign/upgradelib.php'); | |
9c986ee0 | 32 | require_once($CFG->dirroot . '/mod/assign/tests/base_test.php'); |
47f48152 DW |
33 | |
34 | /** | |
35 | * Unit tests for (some of) mod/assign/locallib.php. | |
36 | * | |
37 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
38 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
39 | */ | |
9c986ee0 | 40 | class mod_assign_locallib_testcase extends mod_assign_base_testcase { |
47f48152 DW |
41 | |
42 | public function test_return_links() { | |
c2114099 | 43 | global $PAGE; |
47f48152 DW |
44 | $this->setUser($this->editingteachers[0]); |
45 | $returnaction = 'RETURNACTION'; | |
c2114099 | 46 | $returnparams = array('param'=>'1'); |
47f48152 | 47 | $assign = $this->create_instance(); |
c2114099 | 48 | $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id))); |
47f48152 DW |
49 | $assign->register_return_link($returnaction, $returnparams); |
50 | $this->assertEquals($returnaction, $assign->get_return_action()); | |
51 | $this->assertEquals($returnparams, $assign->get_return_params()); | |
52 | } | |
53 | ||
54 | public function test_get_feedback_plugins() { | |
55 | $this->setUser($this->editingteachers[0]); | |
56 | $assign = $this->create_instance(); | |
11527706 DW |
57 | $installedplugins = array_keys(get_plugin_list('assignfeedback')); |
58 | ||
59 | foreach ($assign->get_feedback_plugins() as $plugin) { | |
60 | $this->assertContains($plugin->get_type(), $installedplugins, 'Feedback plugin not in list of installed plugins'); | |
61 | } | |
47f48152 DW |
62 | } |
63 | ||
64 | public function test_get_submission_plugins() { | |
65 | $this->setUser($this->editingteachers[0]); | |
66 | $assign = $this->create_instance(); | |
11527706 DW |
67 | $installedplugins = array_keys(get_plugin_list('assignsubmission')); |
68 | ||
69 | foreach ($assign->get_submission_plugins() as $plugin) { | |
70 | $this->assertContains($plugin->get_type(), $installedplugins, 'Submission plugin not in list of installed plugins'); | |
71 | } | |
47f48152 DW |
72 | } |
73 | ||
74 | public function test_is_blind_marking() { | |
75 | $this->setUser($this->editingteachers[0]); | |
76 | $assign = $this->create_instance(array('blindmarking'=>1)); | |
77 | $this->assertEquals(true, $assign->is_blind_marking()); | |
78 | ||
79 | // Test cannot see student names. | |
80 | $gradingtable = new assign_grading_table($assign, 1, '', 0, true); | |
81 | $output = $assign->get_renderer()->render($gradingtable); | |
82 | $this->assertEquals(true, strpos($output, get_string('hiddenuser', 'assign'))); | |
83 | ||
84 | // Test students cannot reveal identities. | |
85 | $nopermission = false; | |
86 | $this->setUser($this->students[0]); | |
87 | $this->setExpectedException('required_capability_exception'); | |
88 | $assign->testable_process_reveal_identities(); | |
89 | ||
90 | // Test teachers cannot reveal identities. | |
91 | $nopermission = false; | |
92 | $this->setUser($this->teachers[0]); | |
93 | $this->setExpectedException('required_capability_exception'); | |
94 | $assign->testable_process_reveal_identities(); | |
95 | ||
96 | // Test sesskey is required. | |
97 | $nosesskey = true; | |
98 | $this->setUser($this->editingteachers[0]); | |
99 | $this->setExpectedException('moodle_exception'); | |
100 | $assign->testable_process_reveal_identities(); | |
101 | ||
102 | // Test editingteacher can reveal identities if sesskey is ignored. | |
103 | $this->editingteachers[0]->ignoresesskey = true; | |
104 | $this->setUser($this->editingteachers[0]); | |
105 | $assign->testable_process_reveal_identities(); | |
106 | $this->assertEquals(false, $assign->is_blind_marking()); | |
107 | ||
108 | // Test student names are visible. | |
109 | $gradingtable = new assign_grading_table($assign, 1, '', 0, true); | |
110 | $output = $assign->get_renderer()->render($gradingtable); | |
111 | $this->assertEquals(false, strpos($output, get_string('hiddenuser', 'assign'))); | |
112 | ||
113 | // Set this back to default. | |
114 | $this->editingteachers[0]->ignoresesskey = false; | |
115 | } | |
116 | ||
117 | public function test_show_intro() { | |
118 | // Test whether we are showing the intro at the correct times. | |
119 | $this->setUser($this->editingteachers[0]); | |
120 | $assign = $this->create_instance(array('alwaysshowdescription'=>1)); | |
121 | ||
122 | $this->assertEquals(true, $assign->testable_show_intro()); | |
123 | ||
124 | $tomorrow = time() + (24*60*60); | |
125 | ||
126 | $assign = $this->create_instance(array('alwaysshowdescription'=>0, | |
127 | 'allowsubmissionsfromdate'=>$tomorrow)); | |
128 | $this->assertEquals(false, $assign->testable_show_intro()); | |
129 | $yesterday = time() - (24*60*60); | |
130 | $assign = $this->create_instance(array('alwaysshowdescription'=>0, | |
131 | 'allowsubmissionsfromdate'=>$yesterday)); | |
132 | $this->assertEquals(true, $assign->testable_show_intro()); | |
133 | } | |
134 | ||
135 | public function test_has_submissions_or_grades() { | |
136 | $this->setUser($this->editingteachers[0]); | |
137 | $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1)); | |
138 | ||
139 | $instance = $assign->get_instance(); | |
140 | ||
141 | // Should start empty. | |
142 | $this->assertEquals(false, $assign->has_submissions_or_grades()); | |
143 | ||
144 | // Simulate a submission. | |
145 | $this->setUser($this->students[0]); | |
146 | $submission = $assign->get_user_submission($this->students[0]->id, true); | |
147 | $data = new stdClass(); | |
148 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
149 | 'text'=>'Submission text', | |
150 | 'format'=>FORMAT_MOODLE); | |
151 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
152 | $plugin->save($submission, $data); | |
153 | ||
154 | // Now test again. | |
155 | $this->assertEquals(true, $assign->has_submissions_or_grades()); | |
156 | // Set this back to default. | |
157 | $this->students[0]->ignoresesskey = false; | |
158 | } | |
159 | ||
160 | public function test_delete_grades() { | |
161 | $this->setUser($this->editingteachers[0]); | |
162 | $assign = $this->create_instance(); | |
163 | ||
164 | // Simulate adding a grade. | |
165 | $this->setUser($this->teachers[0]); | |
166 | $data = new stdClass(); | |
167 | $data->grade = '50.0'; | |
168 | $assign->testable_apply_grade_to_user($data, $this->students[0]->id); | |
169 | ||
170 | // Now see if the data is in the gradebook. | |
171 | $gradinginfo = grade_get_grades($this->course->id, | |
172 | 'mod', | |
173 | 'assign', | |
174 | $assign->get_instance()->id); | |
175 | ||
176 | $this->assertNotEquals(0, count($gradinginfo->items)); | |
177 | ||
178 | $assign->testable_delete_grades(); | |
179 | $gradinginfo = grade_get_grades($this->course->id, | |
180 | 'mod', | |
181 | 'assign', | |
182 | $assign->get_instance()->id); | |
183 | ||
184 | $this->assertEquals(0, count($gradinginfo->items)); | |
185 | } | |
186 | ||
187 | public function test_delete_instance() { | |
188 | $this->setUser($this->editingteachers[0]); | |
189 | $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1)); | |
190 | ||
191 | // Simulate adding a grade. | |
192 | $this->setUser($this->teachers[0]); | |
193 | $data = new stdClass(); | |
194 | $data->grade = '50.0'; | |
195 | $assign->testable_apply_grade_to_user($data, $this->students[0]->id); | |
196 | ||
197 | // Simulate a submission. | |
198 | $this->setUser($this->students[0]); | |
199 | $submission = $assign->get_user_submission($this->students[0]->id, true); | |
200 | $data = new stdClass(); | |
201 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
202 | 'text'=>'Submission text', | |
203 | 'format'=>FORMAT_MOODLE); | |
204 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
205 | $plugin->save($submission, $data); | |
206 | ||
207 | // Now try and delete. | |
208 | $this->assertEquals(true, $assign->delete_instance()); | |
209 | } | |
210 | ||
211 | public function test_reset_userdata() { | |
212 | global $DB; | |
213 | ||
214 | $now = time(); | |
215 | $this->setUser($this->editingteachers[0]); | |
216 | $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1, | |
217 | 'duedate'=>$now)); | |
218 | ||
219 | // Simulate adding a grade. | |
220 | $this->setUser($this->teachers[0]); | |
221 | $data = new stdClass(); | |
222 | $data->grade = '50.0'; | |
223 | $assign->testable_apply_grade_to_user($data, $this->students[0]->id); | |
224 | ||
225 | // Simulate a submission. | |
226 | $this->setUser($this->students[0]); | |
227 | $submission = $assign->get_user_submission($this->students[0]->id, true); | |
228 | $data = new stdClass(); | |
229 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
230 | 'text'=>'Submission text', | |
231 | 'format'=>FORMAT_MOODLE); | |
232 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
233 | $plugin->save($submission, $data); | |
234 | ||
235 | $this->assertEquals(true, $assign->has_submissions_or_grades()); | |
236 | // Now try and reset. | |
237 | $data = new stdClass(); | |
238 | $data->reset_assign_submissions = 1; | |
239 | $data->reset_gradebook_grades = 1; | |
240 | $data->courseid = $this->course->id; | |
241 | $data->timeshift = 24*60*60; | |
242 | $this->setUser($this->editingteachers[0]); | |
243 | $assign->reset_userdata($data); | |
244 | $this->assertEquals(false, $assign->has_submissions_or_grades()); | |
245 | ||
246 | // Reload the instance data. | |
247 | $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id)); | |
248 | $this->assertEquals($now + 24*60*60, $instance->duedate); | |
249 | } | |
250 | ||
251 | public function test_plugin_settings() { | |
252 | global $DB; | |
253 | ||
254 | $now = time(); | |
255 | $this->setUser($this->editingteachers[0]); | |
256 | $assign = $this->create_instance(array('assignsubmission_file_enabled'=>1, | |
257 | 'assignsubmission_file_maxfiles'=>12, | |
258 | 'assignsubmission_file_maxsizebytes'=>10)); | |
259 | ||
260 | $plugin = $assign->get_submission_plugin_by_type('file'); | |
261 | $this->assertEquals('12', $plugin->get_config('maxfilesubmissions')); | |
262 | } | |
263 | ||
264 | public function test_update_calendar() { | |
265 | global $DB; | |
266 | ||
267 | $now = time(); | |
268 | $this->setUser($this->editingteachers[0]); | |
269 | $assign = $this->create_instance(array('duedate'=>$now)); | |
270 | ||
271 | // See if there is an event in the calendar. | |
272 | $params = array('modulename'=>'assign', 'instance'=>$assign->get_instance()->id); | |
273 | $id = $DB->get_field('event', 'id', $params); | |
274 | ||
275 | $this->assertEquals(false, empty($id)); | |
276 | } | |
277 | ||
278 | public function test_update_instance() { | |
279 | global $DB; | |
280 | ||
281 | $this->setUser($this->editingteachers[0]); | |
282 | $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1)); | |
283 | ||
284 | $now = time(); | |
285 | $instance = $assign->get_instance(); | |
286 | $instance->duedate = $now; | |
287 | $instance->instance = $instance->id; | |
288 | $instance->assignsubmission_onlinetext_enabled = 1; | |
289 | $instance->assignsubmission_file_enabled = 0; | |
290 | $instance->assignsubmission_comments_enabled = 0; | |
291 | $instance->assignfeedback_comments_enabled = 0; | |
292 | $instance->assignfeedback_file_enabled = 0; | |
293 | $instance->assignfeedback_offline_enabled = 0; | |
294 | ||
295 | $assign->update_instance($instance); | |
296 | ||
297 | $instance = $DB->get_record('assign', array('id'=>$assign->get_instance()->id)); | |
298 | $this->assertEquals($now, $instance->duedate); | |
299 | } | |
300 | ||
301 | public function test_list_participants() { | |
a5c793c3 | 302 | $this->createExtraUsers(); |
47f48152 DW |
303 | $this->setUser($this->editingteachers[0]); |
304 | $assign = $this->create_instance(array('grade'=>100)); | |
305 | ||
a5c793c3 | 306 | $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($assign->list_participants(null, true))); |
47f48152 DW |
307 | } |
308 | ||
309 | public function test_count_teams() { | |
a5c793c3 | 310 | $this->createExtraUsers(); |
47f48152 DW |
311 | $this->setUser($this->editingteachers[0]); |
312 | $assign = $this->create_instance(array('teamsubmission'=>1)); | |
313 | ||
a5c793c3 | 314 | $this->assertEquals(self::GROUP_COUNT + 1, $assign->count_teams()); |
47f48152 DW |
315 | } |
316 | ||
317 | public function test_count_submissions() { | |
a5c793c3 | 318 | $this->createExtraUsers(); |
47f48152 DW |
319 | $this->setUser($this->editingteachers[0]); |
320 | $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled'=>1)); | |
321 | ||
322 | // Simulate a submission. | |
a5c793c3 DW |
323 | $this->setUser($this->extrastudents[0]); |
324 | $submission = $assign->get_user_submission($this->extrastudents[0]->id, true); | |
47f48152 DW |
325 | // Leave this one as DRAFT. |
326 | $data = new stdClass(); | |
327 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
328 | 'text'=>'Submission text', | |
329 | 'format'=>FORMAT_MOODLE); | |
330 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
331 | $plugin->save($submission, $data); | |
332 | ||
333 | // Simulate adding a grade. | |
334 | $this->setUser($this->teachers[0]); | |
335 | $data = new stdClass(); | |
336 | $data->grade = '50.0'; | |
a5c793c3 | 337 | $assign->testable_apply_grade_to_user($data, $this->extrastudents[0]->id); |
47f48152 DW |
338 | |
339 | // Simulate a submission. | |
a5c793c3 DW |
340 | $this->setUser($this->extrastudents[1]); |
341 | $submission = $assign->get_user_submission($this->extrastudents[1]->id, true); | |
47f48152 | 342 | $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; |
a5c793c3 | 343 | $assign->testable_update_submission($submission, $this->extrastudents[1]->id, true, false); |
47f48152 DW |
344 | $data = new stdClass(); |
345 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
346 | 'text'=>'Submission text', | |
347 | 'format'=>FORMAT_MOODLE); | |
348 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
349 | $plugin->save($submission, $data); | |
350 | ||
351 | // Simulate a submission. | |
a5c793c3 DW |
352 | $this->setUser($this->extrastudents[2]); |
353 | $submission = $assign->get_user_submission($this->extrastudents[2]->id, true); | |
47f48152 | 354 | $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; |
a5c793c3 | 355 | $assign->testable_update_submission($submission, $this->extrastudents[2]->id, true, false); |
47f48152 DW |
356 | $data = new stdClass(); |
357 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
358 | 'text'=>'Submission text', | |
359 | 'format'=>FORMAT_MOODLE); | |
360 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
361 | $plugin->save($submission, $data); | |
362 | ||
363 | // Simulate a submission. | |
a5c793c3 DW |
364 | $this->setUser($this->extrastudents[3]); |
365 | $submission = $assign->get_user_submission($this->extrastudents[3]->id, true); | |
47f48152 | 366 | $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; |
a5c793c3 | 367 | $assign->testable_update_submission($submission, $this->extrastudents[3]->id, true, false); |
47f48152 DW |
368 | $data = new stdClass(); |
369 | $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(), | |
370 | 'text'=>'Submission text', | |
371 | 'format'=>FORMAT_MOODLE); | |
372 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
373 | $plugin->save($submission, $data); | |
374 | ||
375 | // Simulate adding a grade. | |
376 | $this->setUser($this->teachers[0]); | |
377 | $data = new stdClass(); | |
378 | $data->grade = '50.0'; | |
a5c793c3 | 379 | $assign->testable_apply_grade_to_user($data, $this->extrastudents[3]->id); |
47f48152 DW |
380 | |
381 | $this->assertEquals(2, $assign->count_grades()); | |
382 | $this->assertEquals(4, $assign->count_submissions()); | |
383 | $this->assertEquals(2, $assign->count_submissions_need_grading()); | |
384 | $this->assertEquals(3, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED)); | |
385 | $this->assertEquals(1, $assign->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_DRAFT)); | |
386 | } | |
387 | ||
388 | public function test_get_grading_userid_list() { | |
a5c793c3 | 389 | $this->createExtraUsers(); |
47f48152 DW |
390 | $this->setUser($this->editingteachers[0]); |
391 | $assign = $this->create_instance(); | |
392 | ||
393 | $users = $assign->testable_get_grading_userid_list(); | |
a5c793c3 | 394 | $this->assertEquals(self::DEFAULT_STUDENT_COUNT + self::EXTRA_STUDENT_COUNT, count($users)); |
47f48152 DW |
395 | } |
396 | ||
397 | public function test_cron() { | |
398 | // First run cron so there are no messages waiting to be sent (from other tests). | |
399 | cron_setup_user(); | |
400 | assign::cron(); | |
401 | ||
402 | // Now create an assignment and add some feedback. | |
403 | $this->setUser($this->editingteachers[0]); | |
404 | $assign = $this->create_instance(); | |
405 | ||
406 | // Simulate adding a grade. | |
407 | $this->setUser($this->teachers[0]); | |
408 | $data = new stdClass(); | |
409 | $data->grade = '50.0'; | |
a5c793c3 | 410 | $assign->testable_apply_grade_to_user($data, $this->students[0]->id); |
47f48152 DW |
411 | |
412 | // Now run cron and see that one message was sent. | |
413 | $this->preventResetByRollback(); | |
414 | $sink = $this->redirectMessages(); | |
415 | cron_setup_user(); | |
416 | $this->expectOutputRegex('/Done processing 1 assignment submissions/'); | |
417 | assign::cron(); | |
418 | ||
419 | $messages = $sink->get_messages(); | |
420 | $this->assertEquals(1, count($messages)); | |
421 | $this->assertEquals(1, $messages[0]->notification); | |
422 | $this->assertEquals($assign->get_instance()->name, $messages[0]->contexturlname); | |
423 | } | |
424 | ||
425 | public function test_is_graded() { | |
426 | $this->setUser($this->editingteachers[0]); | |
427 | $assign = $this->create_instance(); | |
428 | ||
429 | // Simulate adding a grade. | |
430 | $this->setUser($this->teachers[0]); | |
431 | $data = new stdClass(); | |
432 | $data->grade = '50.0'; | |
433 | $assign->testable_apply_grade_to_user($data, $this->students[0]->id); | |
434 | ||
435 | $this->assertEquals(true, $assign->testable_is_graded($this->students[0]->id)); | |
436 | $this->assertEquals(false, $assign->testable_is_graded($this->students[1]->id)); | |
437 | } | |
438 | ||
439 | public function test_can_view_submission() { | |
440 | $this->setUser($this->editingteachers[0]); | |
441 | $assign = $this->create_instance(); | |
442 | ||
443 | $this->setUser($this->students[0]); | |
444 | $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id)); | |
445 | $this->assertEquals(false, $assign->can_view_submission($this->students[1]->id)); | |
446 | $this->assertEquals(false, $assign->can_view_submission($this->teachers[0]->id)); | |
447 | $this->setUser($this->teachers[0]); | |
448 | $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id)); | |
449 | $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id)); | |
450 | $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id)); | |
451 | $this->setUser($this->editingteachers[0]); | |
452 | $this->assertEquals(true, $assign->can_view_submission($this->students[0]->id)); | |
453 | $this->assertEquals(true, $assign->can_view_submission($this->students[1]->id)); | |
454 | $this->assertEquals(true, $assign->can_view_submission($this->teachers[0]->id)); | |
455 | } | |
456 | ||
457 | ||
458 | public function test_update_submission() { | |
a5c793c3 | 459 | $this->createExtraUsers(); |
47f48152 DW |
460 | $this->setUser($this->editingteachers[0]); |
461 | $assign = $this->create_instance(); | |
462 | ||
a5c793c3 | 463 | $this->setUser($this->extrastudents[0]); |
47f48152 | 464 | $now = time(); |
a5c793c3 DW |
465 | $submission = $assign->get_user_submission($this->extrastudents[0]->id, true); |
466 | $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false); | |
47f48152 DW |
467 | |
468 | $this->setUser($this->teachers[0]); | |
469 | // Verify the gradebook update. | |
470 | $gradinginfo = grade_get_grades($this->course->id, | |
471 | 'mod', | |
472 | 'assign', | |
473 | $assign->get_instance()->id, | |
a5c793c3 | 474 | $this->extrastudents[0]->id); |
47f48152 | 475 | |
a5c793c3 DW |
476 | $this->assertEquals($this->extrastudents[0]->id, |
477 | $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified); | |
47f48152 DW |
478 | |
479 | // Now verify group assignments. | |
480 | $this->setUser($this->editingteachers[0]); | |
481 | $assign = $this->create_instance(array('teamsubmission'=>1)); | |
482 | ||
a5c793c3 | 483 | $this->setUser($this->extrastudents[0]); |
47f48152 | 484 | $now = time(); |
a5c793c3 DW |
485 | $submission = $assign->get_group_submission($this->extrastudents[0]->id, 0, true); |
486 | $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, true); | |
47f48152 DW |
487 | |
488 | // Check that at least 2 members of the submission group had their submission updated. | |
489 | ||
490 | $this->setUser($this->editingteachers[0]); | |
491 | $gradinginfo = grade_get_grades($this->course->id, | |
492 | 'mod', | |
493 | 'assign', | |
494 | $assign->get_instance()->id, | |
a5c793c3 | 495 | $this->extrastudents[0]->id); |
47f48152 | 496 | |
a5c793c3 DW |
497 | $this->assertEquals($this->extrastudents[0]->id, |
498 | $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->usermodified); | |
47f48152 DW |
499 | |
500 | $gradinginfo = grade_get_grades($this->course->id, | |
501 | 'mod', | |
502 | 'assign', | |
503 | $assign->get_instance()->id, | |
a5c793c3 | 504 | $this->extrastudents[self::GROUP_COUNT]->id); |
47f48152 | 505 | |
a5c793c3 DW |
506 | $this->assertEquals($this->extrastudents[self::GROUP_COUNT]->id, |
507 | $gradinginfo->items[0]->grades[$this->extrastudents[self::GROUP_COUNT]->id]->usermodified); | |
47f48152 DW |
508 | |
509 | // Now verify blind marking. | |
510 | $this->setUser($this->editingteachers[0]); | |
511 | $assign = $this->create_instance(array('blindmarking'=>1)); | |
512 | ||
a5c793c3 | 513 | $this->setUser($this->extrastudents[0]); |
47f48152 | 514 | $now = time(); |
a5c793c3 DW |
515 | $submission = $assign->get_user_submission($this->extrastudents[0]->id, true); |
516 | $assign->testable_update_submission($submission, $this->extrastudents[0]->id, true, false); | |
47f48152 DW |
517 | |
518 | $this->setUser($this->editingteachers[0]); | |
519 | $gradinginfo = grade_get_grades($this->course->id, | |
520 | 'mod', | |
521 | 'assign', | |
522 | $assign->get_instance()->id, | |
a5c793c3 | 523 | $this->extrastudents[0]->id); |
47f48152 | 524 | |
a5c793c3 | 525 | $this->assertEquals(null, $gradinginfo->items[0]->grades[$this->extrastudents[0]->id]->datesubmitted); |
47f48152 DW |
526 | } |
527 | ||
528 | public function test_submissions_open() { | |
529 | $this->setUser($this->editingteachers[0]); | |
530 | ||
531 | $now = time(); | |
532 | $tomorrow = $now + 24*60*60; | |
533 | $oneweek = $now + 7*24*60*60; | |
534 | $yesterday = $now - 24*60*60; | |
535 | ||
536 | $assign = $this->create_instance(); | |
537 | $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id)); | |
538 | ||
539 | $assign = $this->create_instance(array('duedate'=>$tomorrow)); | |
540 | $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id)); | |
541 | ||
542 | $assign = $this->create_instance(array('duedate'=>$yesterday)); | |
543 | $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id)); | |
544 | ||
545 | $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$tomorrow)); | |
546 | $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id)); | |
547 | ||
548 | $assign = $this->create_instance(array('duedate'=>$yesterday, 'cutoffdate'=>$yesterday)); | |
549 | $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id)); | |
550 | ||
551 | $assign->testable_save_user_extension($this->students[0]->id, $tomorrow); | |
552 | $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id)); | |
553 | ||
554 | $assign = $this->create_instance(array('submissiondrafts'=>1)); | |
555 | $this->assertEquals(true, $assign->testable_submissions_open($this->students[0]->id)); | |
556 | ||
557 | $this->setUser($this->students[0]); | |
558 | $now = time(); | |
559 | $submission = $assign->get_user_submission($this->students[0]->id, true); | |
560 | $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; | |
561 | $assign->testable_update_submission($submission, $this->students[0]->id, true, false); | |
562 | $this->setUser($this->editingteachers[0]); | |
563 | $this->assertEquals(false, $assign->testable_submissions_open($this->students[0]->id)); | |
564 | } | |
565 | ||
566 | public function test_get_graders() { | |
a5c793c3 | 567 | $this->createExtraUsers(); |
47f48152 DW |
568 | $this->setUser($this->editingteachers[0]); |
569 | $assign = $this->create_instance(); | |
570 | ||
a5c793c3 DW |
571 | $this->assertCount(self::DEFAULT_TEACHER_COUNT + |
572 | self::DEFAULT_EDITING_TEACHER_COUNT + | |
573 | self::EXTRA_TEACHER_COUNT + | |
574 | self::EXTRA_EDITING_TEACHER_COUNT, | |
575 | $assign->testable_get_graders($this->students[0]->id)); | |
47f48152 DW |
576 | |
577 | $assign = $this->create_instance(); | |
578 | // Force create an assignment with SEPARATEGROUPS. | |
579 | $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); | |
580 | $params = array('course'=>$this->course->id); | |
581 | $instance = $generator->create_instance($params); | |
582 | $cm = get_coursemodule_from_instance('assign', $instance->id); | |
583 | set_coursemodule_groupmode($cm->id, SEPARATEGROUPS); | |
584 | $cm->groupmode = SEPARATEGROUPS; | |
585 | $context = context_module::instance($cm->id); | |
586 | $assign = new testable_assign($context, $cm, $this->course); | |
587 | ||
588 | $this->setUser($this->students[1]); | |
a5c793c3 | 589 | $this->assertCount(4, $assign->testable_get_graders($this->students[0]->id)); |
47f48152 DW |
590 | } |
591 | ||
592 | public function test_get_uniqueid_for_user() { | |
593 | $this->setUser($this->editingteachers[0]); | |
594 | $assign = $this->create_instance(); | |
595 | ||
596 | foreach ($this->students as $student) { | |
597 | $uniqueid = $assign->get_uniqueid_for_user($student->id); | |
598 | $this->assertEquals($student->id, $assign->get_user_id_for_uniqueid($uniqueid)); | |
599 | } | |
600 | } | |
601 | ||
46692c3a | 602 | public function test_show_student_summary() { |
c2114099 | 603 | global $CFG, $PAGE; |
46692c3a DW |
604 | |
605 | $this->setUser($this->editingteachers[0]); | |
606 | $assign = $this->create_instance(); | |
c2114099 | 607 | $PAGE->set_url(new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id))); |
46692c3a DW |
608 | |
609 | // No feedback should be available because this student has not been graded. | |
610 | $this->setUser($this->students[0]); | |
611 | $output = $assign->view_student_summary($this->students[0], true); | |
612 | $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if there is no grade'); | |
613 | // Simulate adding a grade. | |
614 | $this->setUser($this->teachers[0]); | |
615 | $data = new stdClass(); | |
616 | $data->grade = '50.0'; | |
617 | $assign->testable_apply_grade_to_user($data, $this->students[0]->id); | |
618 | ||
b0da618b | 619 | // Now we should see the feedback. |
46692c3a DW |
620 | $this->setUser($this->students[0]); |
621 | $output = $assign->view_student_summary($this->students[0], true); | |
622 | $this->assertNotEquals(false, strpos($output, 'Feedback'), 'Show feedback if there is a grade'); | |
623 | ||
624 | // Now hide the grade in gradebook. | |
625 | $this->setUser($this->teachers[0]); | |
626 | require_once($CFG->libdir.'/gradelib.php'); | |
627 | $gradeitem = new grade_item(array( | |
628 | 'itemtype' => 'mod', | |
629 | 'itemmodule' => 'assign', | |
630 | 'iteminstance' => $assign->get_instance()->id, | |
631 | 'courseid' => $this->course->id)); | |
632 | ||
633 | $gradeitem->set_hidden(1, false); | |
634 | ||
635 | // No feedback should be available because the grade is hidden. | |
636 | $this->setUser($this->students[0]); | |
637 | $output = $assign->view_student_summary($this->students[0], true); | |
638 | $this->assertEquals(false, strpos($output, 'Feedback'), 'Do not show feedback if the grade is hidden in the gradebook'); | |
639 | ||
b0da618b | 640 | // Do the same but add feedback. |
46692c3a DW |
641 | $assign = $this->create_instance(array('assignfeedback_comments_enabled' => 1)); |
642 | ||
643 | $this->setUser($this->teachers[0]); | |
644 | $grade = $assign->get_user_grade($this->students[0]->id, true); | |
645 | $data = new stdClass(); | |
646 | $data->assignfeedbackcomments_editor = array('text'=>'Tomato sauce', | |
647 | 'format'=>FORMAT_MOODLE); | |
648 | $plugin = $assign->get_feedback_plugin_by_type('comments'); | |
649 | $plugin->save($grade, $data); | |
650 | ||
b0da618b | 651 | // Should have feedback but no grade. |
46692c3a DW |
652 | $this->setUser($this->students[0]); |
653 | $output = $assign->view_student_summary($this->students[0], true); | |
654 | $this->assertNotEquals(false, strpos($output, 'Tomato sauce'), 'Show feedback even if there is no grade'); | |
655 | $this->assertEquals(false, strpos($output, 'Grade'), 'Do not show grade when there is no grade.'); | |
656 | $this->assertEquals(false, strpos($output, 'Graded on'), 'Do not show graded date when there is no grade.'); | |
657 | } | |
658 | ||
659 | ||
47f48152 DW |
660 | } |
661 |