MDL-49617 webservices: Return additional fields in get_assignments WS
[moodle.git] / mod / assign / tests / externallib_test.php
CommitLineData
1f8c8f61
PC
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
17defined('MOODLE_INTERNAL') || die();
18
19global $CFG;
20
21require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22
23/**
24 * External mod assign functions unit tests
25 *
26 * @package mod_assign
27 * @category external
28 * @copyright 2012 Paul Charsley
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31class mod_assign_external_testcase extends externallib_advanced_testcase {
32
33 /**
34 * Tests set up
35 */
36 protected function setUp() {
37 global $CFG;
38 require_once($CFG->dirroot . '/mod/assign/externallib.php');
39 }
40
41 /**
42 * Test get_grades
43 */
05a6445a 44 public function test_get_grades() {
1f8c8f61
PC
45 global $DB, $USER;
46
47 $this->resetAfterTest(true);
48 // Create a course and assignment.
49 $coursedata['idnumber'] = 'idnumbercourse';
50 $coursedata['fullname'] = 'Lightwork Course';
51 $coursedata['summary'] = 'Lightwork Course description';
52 $coursedata['summaryformat'] = FORMAT_MOODLE;
53 $course = self::getDataGenerator()->create_course($coursedata);
54
55 $assigndata['course'] = $course->id;
56 $assigndata['name'] = 'lightwork assignment';
57
58 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
59
60 // Create a manual enrolment record.
07df8c38
PC
61 $manualenroldata['enrol'] = 'manual';
62 $manualenroldata['status'] = 0;
63 $manualenroldata['courseid'] = $course->id;
64 $enrolid = $DB->insert_record('enrol', $manualenroldata);
1f8c8f61
PC
65
66 // Create a teacher and give them capabilities.
67 $context = context_course::instance($course->id);
68 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
780cf0a7 69 $context = context_module::instance($assign->cmid);
1f8c8f61
PC
70 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
71
72 // Create the teacher's enrolment record.
07df8c38
PC
73 $userenrolmentdata['status'] = 0;
74 $userenrolmentdata['enrolid'] = $enrolid;
75 $userenrolmentdata['userid'] = $USER->id;
76 $DB->insert_record('user_enrolments', $userenrolmentdata);
1f8c8f61 77
a13fbf5f 78 // Create a student and give them 2 grades (for 2 attempts).
1f8c8f61 79 $student = self::getDataGenerator()->create_user();
9e3eee67
DW
80
81 $submission = new stdClass();
82 $submission->assignment = $assign->id;
83 $submission->userid = $student->id;
84 $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
85 $submission->latest = 0;
86 $submission->attemptnumber = 0;
87 $submission->groupid = 0;
88 $submission->timecreated = time();
89 $submission->timemodified = time();
90 $DB->insert_record('assign_submission', $submission);
91
a13fbf5f
DW
92 $grade = new stdClass();
93 $grade->assignment = $assign->id;
94 $grade->userid = $student->id;
95 $grade->timecreated = time();
96 $grade->timemodified = $grade->timecreated;
97 $grade->grader = $USER->id;
98 $grade->grade = 50;
a13fbf5f
DW
99 $grade->attemptnumber = 0;
100 $DB->insert_record('assign_grades', $grade);
101
9e3eee67
DW
102 $submission = new stdClass();
103 $submission->assignment = $assign->id;
104 $submission->userid = $student->id;
105 $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
106 $submission->latest = 1;
107 $submission->attemptnumber = 1;
108 $submission->groupid = 0;
109 $submission->timecreated = time();
110 $submission->timemodified = time();
111 $DB->insert_record('assign_submission', $submission);
112
1f8c8f61
PC
113 $grade = new stdClass();
114 $grade->assignment = $assign->id;
115 $grade->userid = $student->id;
116 $grade->timecreated = time();
117 $grade->timemodified = $grade->timecreated;
118 $grade->grader = $USER->id;
119 $grade->grade = 75;
a13fbf5f 120 $grade->attemptnumber = 1;
1f8c8f61
PC
121 $DB->insert_record('assign_grades', $grade);
122
123 $assignmentids[] = $assign->id;
124 $result = mod_assign_external::get_grades($assignmentids);
125
fb695f6e
JM
126 // We need to execute the return values cleaning process to simulate the web service server.
127 $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
128
1f8c8f61
PC
129 // Check that the correct grade information for the student is returned.
130 $this->assertEquals(1, count($result['assignments']));
131 $assignment = $result['assignments'][0];
132 $this->assertEquals($assign->id, $assignment['assignmentid']);
a13fbf5f 133 // Should only get the last grade for this student.
1f8c8f61
PC
134 $this->assertEquals(1, count($assignment['grades']));
135 $grade = $assignment['grades'][0];
136 $this->assertEquals($student->id, $grade['userid']);
13e82f1c 137 // Should be the last grade (not the first).
1f8c8f61
PC
138 $this->assertEquals(75, $grade['grade']);
139 }
140
1378838e
PC
141 /**
142 * Test get_assignments
143 */
05a6445a 144 public function test_get_assignments() {
1378838e
PC
145 global $DB, $USER;
146
147 $this->resetAfterTest(true);
148
149 $category = self::getDataGenerator()->create_category(array(
150 'name' => 'Test category'
151 ));
152
153 // Create a course.
154 $course1 = self::getDataGenerator()->create_course(array(
155 'idnumber' => 'idnumbercourse1',
156 'fullname' => 'Lightwork Course 1',
157 'summary' => 'Lightwork Course 1 description',
158 'summaryformat' => FORMAT_MOODLE,
159 'category' => $category->id
160 ));
161
162 // Create a second course, just for testing.
163 $course2 = self::getDataGenerator()->create_course(array(
164 'idnumber' => 'idnumbercourse2',
165 'fullname' => 'Lightwork Course 2',
166 'summary' => 'Lightwork Course 2 description',
167 'summaryformat' => FORMAT_MOODLE,
168 'category' => $category->id
169 ));
170
171 // Create the assignment module.
172 $assign1 = self::getDataGenerator()->create_module('assign', array(
173 'course' => $course1->id,
d1ec5c83
PC
174 'name' => 'lightwork assignment',
175 'markingworkflow' => 1,
176 'markingallocation' => 1
1378838e
PC
177 ));
178
179 // Create manual enrolment record.
180 $enrolid = $DB->insert_record('enrol', (object)array(
181 'enrol' => 'manual',
182 'status' => 0,
183 'courseid' => $course1->id
184 ));
185
186 // Create the user and give them capabilities.
187 $context = context_course::instance($course1->id);
188 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
74b63eae 189 $context = context_module::instance($assign1->cmid);
1378838e
PC
190 $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
191
192 // Create the user enrolment record.
193 $DB->insert_record('user_enrolments', (object)array(
194 'status' => 0,
195 'enrolid' => $enrolid,
196 'userid' => $USER->id
197 ));
198
199 $result = mod_assign_external::get_assignments();
fb695f6e
JM
200
201 // We need to execute the return values cleaning process to simulate the web service server.
202 $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
203
1378838e
PC
204 // Check the course and assignment are returned.
205 $this->assertEquals(1, count($result['courses']));
206 $course = $result['courses'][0];
207 $this->assertEquals('Lightwork Course 1', $course['fullname']);
208 $this->assertEquals(1, count($course['assignments']));
209 $assignment = $course['assignments'][0];
210 $this->assertEquals($assign1->id, $assignment['id']);
211 $this->assertEquals($course1->id, $assignment['course']);
212 $this->assertEquals('lightwork assignment', $assignment['name']);
d1ec5c83
PC
213 $this->assertEquals(1, $assignment['markingworkflow']);
214 $this->assertEquals(1, $assignment['markingallocation']);
1378838e
PC
215
216 $result = mod_assign_external::get_assignments(array($course1->id));
fb695f6e
JM
217
218 // We need to execute the return values cleaning process to simulate the web service server.
219 $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
220
1378838e
PC
221 $this->assertEquals(1, count($result['courses']));
222 $course = $result['courses'][0];
223 $this->assertEquals('Lightwork Course 1', $course['fullname']);
224 $this->assertEquals(1, count($course['assignments']));
225 $assignment = $course['assignments'][0];
226 $this->assertEquals($assign1->id, $assignment['id']);
227 $this->assertEquals($course1->id, $assignment['course']);
228 $this->assertEquals('lightwork assignment', $assignment['name']);
d1ec5c83
PC
229 $this->assertEquals(1, $assignment['markingworkflow']);
230 $this->assertEquals(1, $assignment['markingallocation']);
1378838e
PC
231
232 $result = mod_assign_external::get_assignments(array($course2->id));
fb695f6e
JM
233
234 // We need to execute the return values cleaning process to simulate the web service server.
235 $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
236
1378838e
PC
237 $this->assertEquals(0, count($result['courses']));
238 $this->assertEquals(1, count($result['warnings']));
239 }
c144959c
PC
240
241 /**
242 * Test get_submissions
243 */
05a6445a 244 public function test_get_submissions() {
c144959c
PC
245 global $DB, $USER;
246
247 $this->resetAfterTest(true);
248 // Create a course and assignment.
249 $coursedata['idnumber'] = 'idnumbercourse1';
250 $coursedata['fullname'] = 'Lightwork Course 1';
251 $coursedata['summary'] = 'Lightwork Course 1 description';
252 $coursedata['summaryformat'] = FORMAT_MOODLE;
253 $course1 = self::getDataGenerator()->create_course($coursedata);
254
255 $assigndata['course'] = $course1->id;
256 $assigndata['name'] = 'lightwork assignment';
257
258 $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
259
260 // Create a student with an online text submission.
a13fbf5f 261 // First attempt.
c144959c
PC
262 $student = self::getDataGenerator()->create_user();
263 $submission = new stdClass();
264 $submission->assignment = $assign1->id;
265 $submission->userid = $student->id;
266 $submission->timecreated = time();
267 $submission->timemodified = $submission->timecreated;
a13fbf5f
DW
268 $submission->status = 'draft';
269 $submission->attemptnumber = 0;
1523f9e0 270 $submission->latest = 0;
a13fbf5f
DW
271 $sid = $DB->insert_record('assign_submission', $submission);
272
273 // Second attempt.
274 $submission = new stdClass();
275 $submission->assignment = $assign1->id;
276 $submission->userid = $student->id;
277 $submission->timecreated = time();
278 $submission->timemodified = $submission->timecreated;
c144959c 279 $submission->status = 'submitted';
a13fbf5f 280 $submission->attemptnumber = 1;
1523f9e0 281 $submission->latest = 1;
c144959c
PC
282 $sid = $DB->insert_record('assign_submission', $submission);
283 $submission->id = $sid;
284
285 $onlinetextsubmission = new stdClass();
9f780282 286 $onlinetextsubmission->onlinetext = "<p>online test text</p>";
c144959c
PC
287 $onlinetextsubmission->onlineformat = 1;
288 $onlinetextsubmission->submission = $submission->id;
289 $onlinetextsubmission->assignment = $assign1->id;
290 $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
291
292 // Create manual enrolment record.
07df8c38
PC
293 $manualenroldata['enrol'] = 'manual';
294 $manualenroldata['status'] = 0;
295 $manualenroldata['courseid'] = $course1->id;
296 $enrolid = $DB->insert_record('enrol', $manualenroldata);
c144959c
PC
297
298 // Create a teacher and give them capabilities.
299 $context = context_course::instance($course1->id);
300 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
d2c58b95 301 $context = context_module::instance($assign1->cmid);
c144959c
PC
302 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
303
304 // Create the teacher's enrolment record.
07df8c38
PC
305 $userenrolmentdata['status'] = 0;
306 $userenrolmentdata['enrolid'] = $enrolid;
307 $userenrolmentdata['userid'] = $USER->id;
308 $DB->insert_record('user_enrolments', $userenrolmentdata);
c144959c
PC
309
310 $assignmentids[] = $assign1->id;
311 $result = mod_assign_external::get_submissions($assignmentids);
312
313 // Check the online text submission is returned.
314 $this->assertEquals(1, count($result['assignments']));
315 $assignment = $result['assignments'][0];
316 $this->assertEquals($assign1->id, $assignment['assignmentid']);
317 $this->assertEquals(1, count($assignment['submissions']));
318 $submission = $assignment['submissions'][0];
319 $this->assertEquals($sid, $submission['id']);
320 $this->assertGreaterThanOrEqual(3, count($submission['plugins']));
321 $plugins = $submission['plugins'];
322 foreach ($plugins as $plugin) {
323 $foundonlinetext = false;
324 if ($plugin['type'] == 'onlinetext') {
325 $foundonlinetext = true;
326 break;
327 }
328 }
329 $this->assertTrue($foundonlinetext);
330 }
07df8c38
PC
331
332 /**
333 * Test get_user_flags
334 */
05a6445a 335 public function test_get_user_flags() {
07df8c38
PC
336 global $DB, $USER;
337
338 $this->resetAfterTest(true);
339 // Create a course and assignment.
340 $coursedata['idnumber'] = 'idnumbercourse';
341 $coursedata['fullname'] = 'Lightwork Course';
342 $coursedata['summary'] = 'Lightwork Course description';
343 $coursedata['summaryformat'] = FORMAT_MOODLE;
344 $course = self::getDataGenerator()->create_course($coursedata);
345
346 $assigndata['course'] = $course->id;
347 $assigndata['name'] = 'lightwork assignment';
348
349 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
350
351 // Create a manual enrolment record.
352 $manualenroldata['enrol'] = 'manual';
353 $manualenroldata['status'] = 0;
354 $manualenroldata['courseid'] = $course->id;
355 $enrolid = $DB->insert_record('enrol', $manualenroldata);
356
357 // Create a teacher and give them capabilities.
358 $context = context_course::instance($course->id);
359 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
780cf0a7 360 $context = context_module::instance($assign->cmid);
07df8c38
PC
361 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
362
363 // Create the teacher's enrolment record.
364 $userenrolmentdata['status'] = 0;
365 $userenrolmentdata['enrolid'] = $enrolid;
366 $userenrolmentdata['userid'] = $USER->id;
367 $DB->insert_record('user_enrolments', $userenrolmentdata);
368
369 // Create a student and give them a user flag record.
370 $student = self::getDataGenerator()->create_user();
371 $userflag = new stdClass();
372 $userflag->assignment = $assign->id;
373 $userflag->userid = $student->id;
374 $userflag->locked = 0;
375 $userflag->mailed = 0;
376 $userflag->extensionduedate = 0;
377 $userflag->workflowstate = 'inmarking';
378 $userflag->allocatedmarker = $USER->id;
379
380 $DB->insert_record('assign_user_flags', $userflag);
381
382 $assignmentids[] = $assign->id;
383 $result = mod_assign_external::get_user_flags($assignmentids);
384
385 // We need to execute the return values cleaning process to simulate the web service server.
386 $result = external_api::clean_returnvalue(mod_assign_external::get_user_flags_returns(), $result);
387
388 // Check that the correct user flag information for the student is returned.
389 $this->assertEquals(1, count($result['assignments']));
390 $assignment = $result['assignments'][0];
391 $this->assertEquals($assign->id, $assignment['assignmentid']);
392 // Should be one user flag record.
393 $this->assertEquals(1, count($assignment['userflags']));
394 $userflag = $assignment['userflags'][0];
395 $this->assertEquals($student->id, $userflag['userid']);
396 $this->assertEquals(0, $userflag['locked']);
397 $this->assertEquals(0, $userflag['mailed']);
398 $this->assertEquals(0, $userflag['extensionduedate']);
399 $this->assertEquals('inmarking', $userflag['workflowstate']);
400 $this->assertEquals($USER->id, $userflag['allocatedmarker']);
401 }
402
403 /**
404 * Test get_user_mappings
405 */
05a6445a 406 public function test_get_user_mappings() {
07df8c38
PC
407 global $DB, $USER;
408
409 $this->resetAfterTest(true);
410 // Create a course and assignment.
411 $coursedata['idnumber'] = 'idnumbercourse';
412 $coursedata['fullname'] = 'Lightwork Course';
413 $coursedata['summary'] = 'Lightwork Course description';
414 $coursedata['summaryformat'] = FORMAT_MOODLE;
415 $course = self::getDataGenerator()->create_course($coursedata);
416
417 $assigndata['course'] = $course->id;
418 $assigndata['name'] = 'lightwork assignment';
419
420 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
421
422 // Create a manual enrolment record.
423 $manualenroldata['enrol'] = 'manual';
424 $manualenroldata['status'] = 0;
425 $manualenroldata['courseid'] = $course->id;
426 $enrolid = $DB->insert_record('enrol', $manualenroldata);
427
428 // Create a teacher and give them capabilities.
429 $context = context_course::instance($course->id);
430 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
780cf0a7 431 $context = context_module::instance($assign->cmid);
07df8c38
PC
432 $this->assignUserCapability('mod/assign:revealidentities', $context->id, $roleid);
433
434 // Create the teacher's enrolment record.
435 $userenrolmentdata['status'] = 0;
436 $userenrolmentdata['enrolid'] = $enrolid;
437 $userenrolmentdata['userid'] = $USER->id;
438 $DB->insert_record('user_enrolments', $userenrolmentdata);
439
440 // Create a student and give them a user mapping record.
441 $student = self::getDataGenerator()->create_user();
442 $mapping = new stdClass();
443 $mapping->assignment = $assign->id;
444 $mapping->userid = $student->id;
445
446 $DB->insert_record('assign_user_mapping', $mapping);
447
448 $assignmentids[] = $assign->id;
449 $result = mod_assign_external::get_user_mappings($assignmentids);
450
451 // We need to execute the return values cleaning process to simulate the web service server.
452 $result = external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result);
453
454 // Check that the correct user mapping information for the student is returned.
455 $this->assertEquals(1, count($result['assignments']));
456 $assignment = $result['assignments'][0];
457 $this->assertEquals($assign->id, $assignment['assignmentid']);
458 // Should be one user mapping record.
459 $this->assertEquals(1, count($assignment['mappings']));
460 $mapping = $assignment['mappings'][0];
461 $this->assertEquals($student->id, $mapping['userid']);
462 }
463
05a6445a
DW
464 /**
465 * Test lock_submissions
466 */
467 public function test_lock_submissions() {
468 global $DB, $USER;
469
470 $this->resetAfterTest(true);
471 // Create a course and assignment and users.
472 $course = self::getDataGenerator()->create_course();
473
474 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
475 $params['course'] = $course->id;
476 $params['assignsubmission_onlinetext_enabled'] = 1;
477 $instance = $generator->create_instance($params);
478 $cm = get_coursemodule_from_instance('assign', $instance->id);
479 $context = context_module::instance($cm->id);
480
481 $assign = new assign($context, $cm, $course);
482
483 $student1 = self::getDataGenerator()->create_user();
484 $student2 = self::getDataGenerator()->create_user();
485 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
486 $this->getDataGenerator()->enrol_user($student1->id,
487 $course->id,
488 $studentrole->id);
489 $this->getDataGenerator()->enrol_user($student2->id,
490 $course->id,
491 $studentrole->id);
492 $teacher = self::getDataGenerator()->create_user();
493 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
494 $this->getDataGenerator()->enrol_user($teacher->id,
495 $course->id,
496 $teacherrole->id);
497
6d90ef2b 498 // Create a student1 with an online text submission.
05a6445a
DW
499 // Simulate a submission.
500 $this->setUser($student1);
501 $submission = $assign->get_user_submission($student1->id, true);
502 $data = new stdClass();
503 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
504 'text'=>'Submission text',
505 'format'=>FORMAT_MOODLE);
506 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
507 $plugin->save($submission, $data);
508
539af602 509 // Ready to test.
05a6445a
DW
510 $this->setUser($teacher);
511 $students = array($student1->id, $student2->id);
512 $result = mod_assign_external::lock_submissions($instance->id, $students);
513
514 // Check for 0 warnings.
515 $this->assertEquals(0, count($result));
516
517 $this->setUser($student2);
518 $submission = $assign->get_user_submission($student2->id, true);
519 $data = new stdClass();
520 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
521 'text'=>'Submission text',
522 'format'=>FORMAT_MOODLE);
523 $notices = array();
524 $this->setExpectedException('moodle_exception');
525 $assign->save_submission($data, $notices);
526 }
527
528 /**
529 * Test unlock_submissions
530 */
531 public function test_unlock_submissions() {
532 global $DB, $USER;
533
534 $this->resetAfterTest(true);
535 // Create a course and assignment and users.
536 $course = self::getDataGenerator()->create_course();
537
538 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
539 $params['course'] = $course->id;
540 $params['assignsubmission_onlinetext_enabled'] = 1;
541 $instance = $generator->create_instance($params);
542 $cm = get_coursemodule_from_instance('assign', $instance->id);
543 $context = context_module::instance($cm->id);
544
545 $assign = new assign($context, $cm, $course);
546
547 $student1 = self::getDataGenerator()->create_user();
548 $student2 = self::getDataGenerator()->create_user();
549 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
550 $this->getDataGenerator()->enrol_user($student1->id,
551 $course->id,
552 $studentrole->id);
553 $this->getDataGenerator()->enrol_user($student2->id,
554 $course->id,
555 $studentrole->id);
556 $teacher = self::getDataGenerator()->create_user();
557 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
558 $this->getDataGenerator()->enrol_user($teacher->id,
559 $course->id,
560 $teacherrole->id);
561
6d90ef2b 562 // Create a student1 with an online text submission.
05a6445a
DW
563 // Simulate a submission.
564 $this->setUser($student1);
565 $submission = $assign->get_user_submission($student1->id, true);
566 $data = new stdClass();
567 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
568 'text'=>'Submission text',
569 'format'=>FORMAT_MOODLE);
570 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
571 $plugin->save($submission, $data);
572
539af602 573 // Ready to test.
05a6445a
DW
574 $this->setUser($teacher);
575 $students = array($student1->id, $student2->id);
576 $result = mod_assign_external::lock_submissions($instance->id, $students);
577
578 // Check for 0 warnings.
579 $this->assertEquals(0, count($result));
580
581 $result = mod_assign_external::unlock_submissions($instance->id, $students);
582
583 // Check for 0 warnings.
584 $this->assertEquals(0, count($result));
585
586 $this->setUser($student2);
587 $submission = $assign->get_user_submission($student2->id, true);
588 $data = new stdClass();
589 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
590 'text'=>'Submission text',
591 'format'=>FORMAT_MOODLE);
592 $notices = array();
593 $assign->save_submission($data, $notices);
594 }
595
596 /**
597 * Test submit_for_grading
598 */
599 public function test_submit_for_grading() {
600 global $DB, $USER;
601
602 $this->resetAfterTest(true);
603 // Create a course and assignment and users.
604 $course = self::getDataGenerator()->create_course();
605
606 set_config('submissionreceipts', 0, 'assign');
607 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
608 $params['course'] = $course->id;
609 $params['assignsubmission_onlinetext_enabled'] = 1;
610 $params['submissiondrafts'] = 1;
611 $params['sendnotifications'] = 0;
91327d96 612 $params['requiresubmissionstatement'] = 1;
05a6445a
DW
613 $instance = $generator->create_instance($params);
614 $cm = get_coursemodule_from_instance('assign', $instance->id);
615 $context = context_module::instance($cm->id);
616
617 $assign = new assign($context, $cm, $course);
618
619 $student1 = self::getDataGenerator()->create_user();
620 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
621 $this->getDataGenerator()->enrol_user($student1->id,
622 $course->id,
623 $studentrole->id);
624
625 // Create a student1 with an online text submission.
626 // Simulate a submission.
627 $this->setUser($student1);
628 $submission = $assign->get_user_submission($student1->id, true);
629 $data = new stdClass();
630 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
631 'text'=>'Submission text',
632 'format'=>FORMAT_MOODLE);
633 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
634 $plugin->save($submission, $data);
635
91327d96
DW
636 $result = mod_assign_external::submit_for_grading($instance->id, false);
637
638 // Should be 1 fail because the submission statement was not aceptted.
639 $this->assertEquals(1, count($result));
640
641 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
642
643 // Check for 0 warnings.
644 $this->assertEquals(0, count($result));
645
646 $submission = $assign->get_user_submission($student1->id, false);
647
648 $this->assertEquals(ASSIGN_SUBMISSION_STATUS_SUBMITTED, $submission->status);
649 }
650
651 /**
652 * Test save_user_extensions
653 */
654 public function test_save_user_extensions() {
655 global $DB, $USER;
656
657 $this->resetAfterTest(true);
658 // Create a course and assignment and users.
659 $course = self::getDataGenerator()->create_course();
660
661 $teacher = self::getDataGenerator()->create_user();
662 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
663 $this->getDataGenerator()->enrol_user($teacher->id,
664 $course->id,
665 $teacherrole->id);
666 $this->setUser($teacher);
667
668 $now = time();
669 $yesterday = $now - 24*60*60;
670 $tomorrow = $now + 24*60*60;
671 set_config('submissionreceipts', 0, 'assign');
672 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
673 $params['course'] = $course->id;
674 $params['submissiondrafts'] = 1;
675 $params['sendnotifications'] = 0;
676 $params['duedate'] = $yesterday;
677 $params['cutoffdate'] = $now - 10;
678 $instance = $generator->create_instance($params);
679 $cm = get_coursemodule_from_instance('assign', $instance->id);
680 $context = context_module::instance($cm->id);
681
682 $assign = new assign($context, $cm, $course);
683
684 $student1 = self::getDataGenerator()->create_user();
685 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
686 $this->getDataGenerator()->enrol_user($student1->id,
687 $course->id,
688 $studentrole->id);
689
690 $this->setUser($student1);
91327d96 691 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
692
693 // Check for 0 warnings.
694 $this->assertEquals(1, count($result));
695
696 $this->setUser($teacher);
697 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
698 $this->assertEquals(1, count($result));
699
700 $this->setUser($teacher);
701 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($yesterday - 10));
702 $this->assertEquals(1, count($result));
703
704 $this->setUser($teacher);
705 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($tomorrow));
706 $this->assertEquals(0, count($result));
707
708 $this->setUser($student1);
91327d96 709 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
710 $this->assertEquals(0, count($result));
711
712 $this->setUser($student1);
713 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
714
715 }
716
717 /**
718 * Test reveal_identities
719 */
720 public function test_reveal_identities() {
721 global $DB, $USER;
722
723 $this->resetAfterTest(true);
724 // Create a course and assignment and users.
725 $course = self::getDataGenerator()->create_course();
726
727 $teacher = self::getDataGenerator()->create_user();
728 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
729 $this->getDataGenerator()->enrol_user($teacher->id,
730 $course->id,
731 $teacherrole->id);
732 $this->setUser($teacher);
733
734 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
735 $params['course'] = $course->id;
736 $params['submissiondrafts'] = 1;
737 $params['sendnotifications'] = 0;
738 $params['blindmarking'] = 1;
739 $instance = $generator->create_instance($params);
740 $cm = get_coursemodule_from_instance('assign', $instance->id);
741 $context = context_module::instance($cm->id);
742
743 $assign = new assign($context, $cm, $course);
744
745 $student1 = self::getDataGenerator()->create_user();
746 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
747 $this->getDataGenerator()->enrol_user($student1->id,
748 $course->id,
749 $studentrole->id);
750
751 $this->setUser($student1);
752 $this->setExpectedException('required_capability_exception');
753 $result = mod_assign_external::reveal_identities($instance->id);
754 $this->assertEquals(1, count($result));
755 $this->assertEquals(true, $assign->is_blind_marking());
756
757 $this->setUser($teacher);
758 $result = mod_assign_external::reveal_identities($instance->id);
759 $this->assertEquals(0, count($result));
760 $this->assertEquals(false, $assign->is_blind_marking());
761
762 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
763 $params['course'] = $course->id;
764 $params['submissiondrafts'] = 1;
765 $params['sendnotifications'] = 0;
766 $params['blindmarking'] = 0;
767 $instance = $generator->create_instance($params);
768 $cm = get_coursemodule_from_instance('assign', $instance->id);
769 $context = context_module::instance($cm->id);
770
771 $assign = new assign($context, $cm, $course);
772 $result = mod_assign_external::reveal_identities($instance->id);
773 $this->assertEquals(1, count($result));
774 $this->assertEquals(false, $assign->is_blind_marking());
775
776 }
777
778 /**
779 * Test revert_submissions_to_draft
780 */
781 public function test_revert_submissions_to_draft() {
782 global $DB, $USER;
783
784 $this->resetAfterTest(true);
785 set_config('submissionreceipts', 0, 'assign');
786 // Create a course and assignment and users.
787 $course = self::getDataGenerator()->create_course();
788
789 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
790 $params['course'] = $course->id;
791 $params['sendnotifications'] = 0;
792 $params['submissiondrafts'] = 1;
793 $instance = $generator->create_instance($params);
794 $cm = get_coursemodule_from_instance('assign', $instance->id);
795 $context = context_module::instance($cm->id);
796
797 $assign = new assign($context, $cm, $course);
798
799 $student1 = self::getDataGenerator()->create_user();
800 $student2 = self::getDataGenerator()->create_user();
801 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
802 $this->getDataGenerator()->enrol_user($student1->id,
803 $course->id,
804 $studentrole->id);
805 $this->getDataGenerator()->enrol_user($student2->id,
806 $course->id,
807 $studentrole->id);
808 $teacher = self::getDataGenerator()->create_user();
809 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
810 $this->getDataGenerator()->enrol_user($teacher->id,
811 $course->id,
812 $teacherrole->id);
813
814 // Create a student1 with an online text submission.
815 // Simulate a submission.
816 $this->setUser($student1);
91327d96 817 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
818 $this->assertEquals(0, count($result));
819
539af602 820 // Ready to test.
05a6445a
DW
821 $this->setUser($teacher);
822 $students = array($student1->id, $student2->id);
823 $result = mod_assign_external::revert_submissions_to_draft($instance->id, array($student1->id));
824
825 // Check for 0 warnings.
826 $this->assertEquals(0, count($result));
827
828 }
829
830 /**
831 * Test save_submission
832 */
833 public function test_save_submission() {
834 global $DB, $USER;
835
836 $this->resetAfterTest(true);
837 // Create a course and assignment and users.
838 $course = self::getDataGenerator()->create_course();
839
840 $teacher = self::getDataGenerator()->create_user();
841 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
842 $this->getDataGenerator()->enrol_user($teacher->id,
843 $course->id,
844 $teacherrole->id);
845 $this->setUser($teacher);
846
847 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
848 $params['course'] = $course->id;
849 $params['assignsubmission_onlinetext_enabled'] = 1;
850 $params['assignsubmission_file_enabled'] = 1;
851 $params['assignsubmission_file_maxfiles'] = 5;
852 $params['assignsubmission_file_maxsizebytes'] = 1024*1024;
853 $instance = $generator->create_instance($params);
854 $cm = get_coursemodule_from_instance('assign', $instance->id);
855 $context = context_module::instance($cm->id);
856
857 $assign = new assign($context, $cm, $course);
858
859 $student1 = self::getDataGenerator()->create_user();
860 $student2 = self::getDataGenerator()->create_user();
861 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
862 $this->getDataGenerator()->enrol_user($student1->id,
863 $course->id,
864 $studentrole->id);
865 $this->getDataGenerator()->enrol_user($student2->id,
866 $course->id,
867 $studentrole->id);
868 // Create a student1 with an online text submission.
869 // Simulate a submission.
870 $this->setUser($student1);
871
872 // Create a file in a draft area.
873 $draftidfile = file_get_unused_draft_itemid();
874
875 $usercontext = context_user::instance($student1->id);
876 $filerecord = array(
877 'contextid' => $usercontext->id,
878 'component' => 'user',
879 'filearea' => 'draft',
880 'itemid' => $draftidfile,
881 'filepath' => '/',
882 'filename' => 'testtext.txt',
883 );
884
885 $fs = get_file_storage();
886 $fs->create_file_from_string($filerecord, 'text contents');
887
888 // Create another file in a different draft area.
889 $draftidonlinetext = file_get_unused_draft_itemid();
890
891 $filerecord = array(
892 'contextid' => $usercontext->id,
893 'component' => 'user',
894 'filearea' => 'draft',
895 'itemid' => $draftidonlinetext,
896 'filepath' => '/',
897 'filename' => 'shouldbeanimage.txt',
898 );
899
900 $fs->create_file_from_string($filerecord, 'image contents (not really)');
901
539af602 902 // Now try a submission.
05a6445a
DW
903 $submissionpluginparams = array();
904 $submissionpluginparams['files_filemanager'] = $draftidfile;
905 $onlinetexteditorparams = array('text'=>'Yeeha!',
906 'format'=>1,
907 'itemid'=>$draftidonlinetext);
908 $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
909 $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
910
911 $this->assertEquals(0, count($result));
912
913 }
914
915 /**
916 * Test save_grade
917 */
918 public function test_save_grade() {
919 global $DB, $USER;
920
921 $this->resetAfterTest(true);
922 // Create a course and assignment and users.
923 $course = self::getDataGenerator()->create_course();
924
925 $teacher = self::getDataGenerator()->create_user();
926 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
927 $this->getDataGenerator()->enrol_user($teacher->id,
928 $course->id,
929 $teacherrole->id);
930 $this->setUser($teacher);
931
932 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
933 $params['course'] = $course->id;
934 $params['assignfeedback_file_enabled'] = 1;
935 $params['assignfeedback_comments_enabled'] = 1;
936 $instance = $generator->create_instance($params);
937 $cm = get_coursemodule_from_instance('assign', $instance->id);
938 $context = context_module::instance($cm->id);
939
940 $assign = new assign($context, $cm, $course);
941
942 $student1 = self::getDataGenerator()->create_user();
943 $student2 = self::getDataGenerator()->create_user();
40c3dacf 944 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
05a6445a
DW
945 $this->getDataGenerator()->enrol_user($student1->id,
946 $course->id,
947 $studentrole->id);
948 $this->getDataGenerator()->enrol_user($student2->id,
949 $course->id,
950 $studentrole->id);
951 // Simulate a grade.
952 $this->setUser($teacher);
953
954 // Create a file in a draft area.
955 $draftidfile = file_get_unused_draft_itemid();
956
957 $usercontext = context_user::instance($teacher->id);
958 $filerecord = array(
959 'contextid' => $usercontext->id,
960 'component' => 'user',
961 'filearea' => 'draft',
962 'itemid' => $draftidfile,
963 'filepath' => '/',
964 'filename' => 'testtext.txt',
965 );
966
967 $fs = get_file_storage();
968 $fs->create_file_from_string($filerecord, 'text contents');
969
539af602 970 // Now try a grade.
05a6445a
DW
971 $feedbackpluginparams = array();
972 $feedbackpluginparams['files_filemanager'] = $draftidfile;
40c3dacf
PC
973 $feedbackeditorparams = array('text' => 'Yeeha!',
974 'format' => 1);
05a6445a 975 $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
539af602
DW
976 $result = mod_assign_external::save_grade($instance->id,
977 $student1->id,
978 50.0,
979 -1,
980 true,
981 'released',
982 false,
983 $feedbackpluginparams);
05a6445a
DW
984
985 // No warnings.
986 $this->assertEquals(0, count($result));
987
988 $result = mod_assign_external::get_grades(array($instance->id));
989
990 $this->assertEquals($result['assignments'][0]['grades'][0]['grade'], '50.0');
05a6445a
DW
991 }
992
40c3dacf
PC
993 /**
994 * Test save grades with advanced grading data
995 */
996 public function test_save_grades_with_advanced_grading() {
997 global $DB, $USER;
998
999 $this->resetAfterTest(true);
1000 // Create a course and assignment and users.
1001 $course = self::getDataGenerator()->create_course();
1002
1003 $teacher = self::getDataGenerator()->create_user();
1004 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
1005 $this->getDataGenerator()->enrol_user($teacher->id,
1006 $course->id,
1007 $teacherrole->id);
1008
1009 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
1010 $params['course'] = $course->id;
1011 $params['assignfeedback_file_enabled'] = 0;
1012 $params['assignfeedback_comments_enabled'] = 0;
1013 $instance = $generator->create_instance($params);
1014 $cm = get_coursemodule_from_instance('assign', $instance->id);
1015 $context = context_module::instance($cm->id);
1016
1017 $assign = new assign($context, $cm, $course);
1018
1019 $student1 = self::getDataGenerator()->create_user();
1020 $student2 = self::getDataGenerator()->create_user();
1021 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1022 $this->getDataGenerator()->enrol_user($student1->id,
1023 $course->id,
1024 $studentrole->id);
1025 $this->getDataGenerator()->enrol_user($student2->id,
1026 $course->id,
1027 $studentrole->id);
1028
1029 $this->setUser($teacher);
1030
1031 $feedbackpluginparams = array();
1032 $feedbackpluginparams['files_filemanager'] = 0;
1033 $feedbackeditorparams = array('text' => '', 'format' => 1);
1034 $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
1035
1036 // Create advanced grading data.
1037 // Create grading area.
1038 $gradingarea = array(
1039 'contextid' => $context->id,
1040 'component' => 'mod_assign',
1041 'areaname' => 'submissions',
1042 'activemethod' => 'rubric'
1043 );
1044 $areaid = $DB->insert_record('grading_areas', $gradingarea);
1045
1046 // Create a rubric grading definition.
1047 $rubricdefinition = array (
1048 'areaid' => $areaid,
1049 'method' => 'rubric',
1050 'name' => 'test',
1051 'status' => 20,
1052 'copiedfromid' => 1,
1053 'timecreated' => 1,
1054 'usercreated' => $teacher->id,
1055 'timemodified' => 1,
1056 'usermodified' => $teacher->id,
1057 'timecopied' => 0
1058 );
1059 $definitionid = $DB->insert_record('grading_definitions', $rubricdefinition);
1060
1061 // Create a criterion with a level.
1062 $rubriccriteria = array (
1063 'definitionid' => $definitionid,
1064 'sortorder' => 1,
1065 'description' => 'Demonstrate an understanding of disease control',
1066 'descriptionformat' => 0
1067 );
1068 $criterionid = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria);
1069 $rubriclevel1 = array (
1070 'criterionid' => $criterionid,
1071 'score' => 50,
1072 'definition' => 'pass',
1073 'definitionformat' => 0
1074 );
1075 $rubriclevel2 = array (
1076 'criterionid' => $criterionid,
1077 'score' => 100,
1078 'definition' => 'excellent',
1079 'definitionformat' => 0
1080 );
1081 $rubriclevel3 = array (
1082 'criterionid' => $criterionid,
1083 'score' => 0,
1084 'definition' => 'fail',
1085 'definitionformat' => 0
1086 );
1087 $levelid1 = $DB->insert_record('gradingform_rubric_levels', $rubriclevel1);
1088 $levelid2 = $DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
1089 $levelid3 = $DB->insert_record('gradingform_rubric_levels', $rubriclevel3);
1090
1091 // Create the filling.
1092 $student1filling = array (
1093 'criterionid' => $criterionid,
1094 'levelid' => $levelid1,
1095 'remark' => 'well done you passed',
1096 'remarkformat' => 0
1097 );
1098
1099 $student2filling = array (
1100 'criterionid' => $criterionid,
1101 'levelid' => $levelid2,
1102 'remark' => 'Excellent work',
1103 'remarkformat' => 0
1104 );
1105
1106 $student1criteria = array(array('criterionid' => $criterionid, 'fillings' => array($student1filling)));
1107 $student1advancedgradingdata = array('rubric' => array('criteria' => $student1criteria));
1108
1109 $student2criteria = array(array('criterionid' => $criterionid, 'fillings' => array($student2filling)));
1110 $student2advancedgradingdata = array('rubric' => array('criteria' => $student2criteria));
1111
1112 $grades = array();
1113 $student1gradeinfo = array();
1114 $student1gradeinfo['userid'] = $student1->id;
1115 $student1gradeinfo['grade'] = 0; // Ignored since advanced grading is being used.
1116 $student1gradeinfo['attemptnumber'] = -1;
1117 $student1gradeinfo['addattempt'] = true;
1118 $student1gradeinfo['workflowstate'] = 'released';
1119 $student1gradeinfo['plugindata'] = $feedbackpluginparams;
1120 $student1gradeinfo['advancedgradingdata'] = $student1advancedgradingdata;
1121 $grades[] = $student1gradeinfo;
1122
1123 $student2gradeinfo = array();
1124 $student2gradeinfo['userid'] = $student2->id;
1125 $student2gradeinfo['grade'] = 0; // Ignored since advanced grading is being used.
1126 $student2gradeinfo['attemptnumber'] = -1;
1127 $student2gradeinfo['addattempt'] = true;
1128 $student2gradeinfo['workflowstate'] = 'released';
1129 $student2gradeinfo['plugindata'] = $feedbackpluginparams;
1130 $student2gradeinfo['advancedgradingdata'] = $student2advancedgradingdata;
1131 $grades[] = $student2gradeinfo;
1132
1133 $result = mod_assign_external::save_grades($instance->id, false, $grades);
1134 // No warnings.
1135 $this->assertEquals(0, count($result));
1136
1137 $student1grade = $DB->get_record('assign_grades',
1138 array('userid' => $student1->id, 'assignment' => $instance->id),
1139 '*',
1140 MUST_EXIST);
1141 $this->assertEquals($student1grade->grade, '50.0');
1142
1143 $student2grade = $DB->get_record('assign_grades',
1144 array('userid' => $student2->id, 'assignment' => $instance->id),
1145 '*',
1146 MUST_EXIST);
1147 $this->assertEquals($student2grade->grade, '100.0');
1148 }
1149
1150 /**
1151 * Test save grades for a team submission
1152 */
1153 public function test_save_grades_with_group_submission() {
1154 global $DB, $USER, $CFG;
1155 require_once($CFG->dirroot . '/group/lib.php');
1156
1157 $this->resetAfterTest(true);
1158 // Create a course and assignment and users.
1159 $course = self::getDataGenerator()->create_course();
1160
1161 $teacher = self::getDataGenerator()->create_user();
1162 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
1163 $this->getDataGenerator()->enrol_user($teacher->id,
1164 $course->id,
1165 $teacherrole->id);
1166
1167 $groupingdata = array();
1168 $groupingdata['courseid'] = $course->id;
1169 $groupingdata['name'] = 'Group assignment grouping';
1170
1171 $grouping = self::getDataGenerator()->create_grouping($groupingdata);
1172
1173 $group1data = array();
1174 $group1data['courseid'] = $course->id;
1175 $group1data['name'] = 'Team 1';
1176 $group2data = array();
1177 $group2data['courseid'] = $course->id;
1178 $group2data['name'] = 'Team 2';
1179
1180 $group1 = self::getDataGenerator()->create_group($group1data);
1181 $group2 = self::getDataGenerator()->create_group($group2data);
1182
1183 groups_assign_grouping($grouping->id, $group1->id);
1184 groups_assign_grouping($grouping->id, $group2->id);
1185
1186 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
1187 $params['course'] = $course->id;
1188 $params['teamsubmission'] = 1;
1189 $params['teamsubmissiongroupingid'] = $grouping->id;
1190 $instance = $generator->create_instance($params);
1191 $cm = get_coursemodule_from_instance('assign', $instance->id);
1192 $context = context_module::instance($cm->id);
1193
1194 $assign = new assign($context, $cm, $course);
1195
1196 $student1 = self::getDataGenerator()->create_user();
1197 $student2 = self::getDataGenerator()->create_user();
1198 $student3 = self::getDataGenerator()->create_user();
1199 $student4 = self::getDataGenerator()->create_user();
1200 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1201 $this->getDataGenerator()->enrol_user($student1->id,
1202 $course->id,
1203 $studentrole->id);
1204 $this->getDataGenerator()->enrol_user($student2->id,
1205 $course->id,
1206 $studentrole->id);
1207 $this->getDataGenerator()->enrol_user($student3->id,
1208 $course->id,
1209 $studentrole->id);
1210 $this->getDataGenerator()->enrol_user($student4->id,
1211 $course->id,
1212 $studentrole->id);
1213
1214 groups_add_member($group1->id, $student1->id);
1215 groups_add_member($group1->id, $student2->id);
1216 groups_add_member($group1->id, $student3->id);
1217 groups_add_member($group2->id, $student4->id);
1218 $this->setUser($teacher);
1219
1220 $feedbackpluginparams = array();
1221 $feedbackpluginparams['files_filemanager'] = 0;
1222 $feedbackeditorparams = array('text' => '', 'format' => 1);
1223 $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
1224
1225 $grades1 = array();
1226 $student1gradeinfo = array();
1227 $student1gradeinfo['userid'] = $student1->id;
1228 $student1gradeinfo['grade'] = 50;
1229 $student1gradeinfo['attemptnumber'] = -1;
1230 $student1gradeinfo['addattempt'] = true;
1231 $student1gradeinfo['workflowstate'] = 'released';
1232 $student1gradeinfo['plugindata'] = $feedbackpluginparams;
1233 $grades1[] = $student1gradeinfo;
1234
1235 $student2gradeinfo = array();
1236 $student2gradeinfo['userid'] = $student2->id;
1237 $student2gradeinfo['grade'] = 75;
1238 $student2gradeinfo['attemptnumber'] = -1;
1239 $student2gradeinfo['addattempt'] = true;
1240 $student2gradeinfo['workflowstate'] = 'released';
1241 $student2gradeinfo['plugindata'] = $feedbackpluginparams;
1242 $grades1[] = $student2gradeinfo;
1243
1244 $this->setExpectedException('invalid_parameter_exception');
1245 // Expect an exception since 2 grades have been submitted for the same team.
1246 $result = mod_assign_external::save_grades($instance->id, true, $grades1);
1247
1248 $grades2 = array();
1249 $student3gradeinfo = array();
1250 $student3gradeinfo['userid'] = $student3->id;
1251 $student3gradeinfo['grade'] = 50;
1252 $student3gradeinfo['attemptnumber'] = -1;
1253 $student3gradeinfo['addattempt'] = true;
1254 $student3gradeinfo['workflowstate'] = 'released';
1255 $student3gradeinfo['plugindata'] = $feedbackpluginparams;
1256 $grades2[] = $student3gradeinfo;
1257
1258 $student4gradeinfo = array();
1259 $student4gradeinfo['userid'] = $student4->id;
1260 $student4gradeinfo['grade'] = 75;
1261 $student4gradeinfo['attemptnumber'] = -1;
1262 $student4gradeinfo['addattempt'] = true;
1263 $student4gradeinfo['workflowstate'] = 'released';
1264 $student4gradeinfo['plugindata'] = $feedbackpluginparams;
1265 $grades2[] = $student4gradeinfo;
1266 $result = mod_assign_external::save_grades($instance->id, true, $grades2);
1267 // There should be no warnings.
1268 $this->assertEquals(0, count($result));
1269
1270 $student3grade = $DB->get_record('assign_grades',
1271 array('userid' => $student3->id, 'assignment' => $instance->id),
1272 '*',
1273 MUST_EXIST);
1274 $this->assertEquals($student3grade->grade, '50.0');
1275
1276 $student4grade = $DB->get_record('assign_grades',
1277 array('userid' => $student4->id, 'assignment' => $instance->id),
1278 '*',
1279 MUST_EXIST);
1280 $this->assertEquals($student4grade->grade, '75.0');
1281 }
1282
05a6445a
DW
1283 /**
1284 * Test copy_previous_attempt
1285 */
1286 public function test_copy_previous_attempt() {
1287 global $DB, $USER;
1288
1289 $this->resetAfterTest(true);
1290 // Create a course and assignment and users.
1291 $course = self::getDataGenerator()->create_course();
1292
1293 $teacher = self::getDataGenerator()->create_user();
1294 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
1295 $this->getDataGenerator()->enrol_user($teacher->id,
1296 $course->id,
1297 $teacherrole->id);
1298 $this->setUser($teacher);
1299
1300 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
1301 $params['course'] = $course->id;
1302 $params['assignsubmission_onlinetext_enabled'] = 1;
1303 $params['assignsubmission_file_enabled'] = 0;
1304 $params['assignfeedback_file_enabled'] = 0;
1305 $params['attemptreopenmethod'] = 'manual';
1306 $params['maxattempts'] = 5;
1307 $instance = $generator->create_instance($params);
1308 $cm = get_coursemodule_from_instance('assign', $instance->id);
1309 $context = context_module::instance($cm->id);
1310
1311 $assign = new assign($context, $cm, $course);
1312
1313 $student1 = self::getDataGenerator()->create_user();
1314 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
1315 $this->getDataGenerator()->enrol_user($student1->id,
1316 $course->id,
1317 $studentrole->id);
539af602 1318 // Now try a submission.
05a6445a
DW
1319 $this->setUser($student1);
1320 $draftidonlinetext = file_get_unused_draft_itemid();
1321 $submissionpluginparams = array();
1322 $onlinetexteditorparams = array('text'=>'Yeeha!',
1323 'format'=>1,
1324 'itemid'=>$draftidonlinetext);
1325 $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
1326 $submissionpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
1327 $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
1328
1329 $this->setUser($teacher);
1330 // Add a grade and reopen the attempt.
539af602 1331 // Now try a grade.
05a6445a
DW
1332 $feedbackpluginparams = array();
1333 $feedbackpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
1334 $feedbackeditorparams = array('text'=>'Yeeha!',
1335 'format'=>1);
1336 $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
539af602
DW
1337 $result = mod_assign_external::save_grade($instance->id,
1338 $student1->id,
1339 50.0,
1340 -1,
1341 true,
1342 'released',
1343 false,
1344 $feedbackpluginparams);
05a6445a
DW
1345
1346 $this->setUser($student1);
1347 // Now copy the previous attempt.
1348 $result = mod_assign_external::copy_previous_attempt($instance->id);
1349 // No warnings.
1350 $this->assertEquals(0, count($result));
1351
1352 $this->setUser($teacher);
1353 $result = mod_assign_external::get_submissions(array($instance->id));
1354
1355 // Check we are now on the second attempt.
1356 $this->assertEquals($result['assignments'][0]['submissions'][0]['attemptnumber'], 1);
1357 // Check the plugins data is not empty.
1358 $this->assertNotEmpty($result['assignments'][0]['submissions'][0]['plugins']);
1359
1360 }
e8b443df
PC
1361
1362 /**
1363 * Test set_user_flags
1364 */
1365 public function test_set_user_flags() {
1366 global $DB, $USER;
1367
1368 $this->resetAfterTest(true);
1369 // Create a course and assignment.
1370 $coursedata['idnumber'] = 'idnumbercourse';
1371 $coursedata['fullname'] = 'Lightwork Course';
1372 $coursedata['summary'] = 'Lightwork Course description';
1373 $coursedata['summaryformat'] = FORMAT_MOODLE;
1374 $course = self::getDataGenerator()->create_course($coursedata);
1375
1376 $assigndata['course'] = $course->id;
1377 $assigndata['name'] = 'lightwork assignment';
1378
1379 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
1380
1381 // Create a manual enrolment record.
1382 $manualenroldata['enrol'] = 'manual';
1383 $manualenroldata['status'] = 0;
1384 $manualenroldata['courseid'] = $course->id;
1385 $enrolid = $DB->insert_record('enrol', $manualenroldata);
1386
1387 // Create a teacher and give them capabilities.
1388 $context = context_course::instance($course->id);
1389 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
780cf0a7 1390 $context = context_module::instance($assign->cmid);
e8b443df
PC
1391 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
1392
1393 // Create the teacher's enrolment record.
1394 $userenrolmentdata['status'] = 0;
1395 $userenrolmentdata['enrolid'] = $enrolid;
1396 $userenrolmentdata['userid'] = $USER->id;
1397 $DB->insert_record('user_enrolments', $userenrolmentdata);
1398
1399 // Create a student.
1400 $student = self::getDataGenerator()->create_user();
1401
1402 // Create test user flags record.
1403 $userflags = array();
1404 $userflag['userid'] = $student->id;
1405 $userflag['workflowstate'] = 'inmarking';
1406 $userflag['allocatedmarker'] = $USER->id;
1407 $userflags = array($userflag);
1408
1409 $createduserflags = mod_assign_external::set_user_flags($assign->id, $userflags);
1410 // We need to execute the return values cleaning process to simulate the web service server.
1411 $createduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $createduserflags);
1412
1413 $this->assertEquals($student->id, $createduserflags[0]['userid']);
1414 $createduserflag = $DB->get_record('assign_user_flags', array('id' => $createduserflags[0]['id']));
1415
1416 // Confirm that all data was inserted correctly.
1417 $this->assertEquals($student->id, $createduserflag->userid);
1418 $this->assertEquals($assign->id, $createduserflag->assignment);
1419 $this->assertEquals(0, $createduserflag->locked);
1420 $this->assertEquals(2, $createduserflag->mailed);
1421 $this->assertEquals(0, $createduserflag->extensionduedate);
1422 $this->assertEquals('inmarking', $createduserflag->workflowstate);
1423 $this->assertEquals($USER->id, $createduserflag->allocatedmarker);
1424
1425 // Create update data.
1426 $userflags = array();
1427 $userflag['userid'] = $createduserflag->userid;
1428 $userflag['workflowstate'] = 'readyforreview';
1429 $userflags = array($userflag);
1430
1431 $updateduserflags = mod_assign_external::set_user_flags($assign->id, $userflags);
1432 // We need to execute the return values cleaning process to simulate the web service server.
1433 $updateduserflags = external_api::clean_returnvalue(mod_assign_external::set_user_flags_returns(), $updateduserflags);
1434
1435 $this->assertEquals($student->id, $updateduserflags[0]['userid']);
1436 $updateduserflag = $DB->get_record('assign_user_flags', array('id' => $updateduserflags[0]['id']));
1437
1438 // Confirm that all data was updated correctly.
1439 $this->assertEquals($student->id, $updateduserflag->userid);
1440 $this->assertEquals($assign->id, $updateduserflag->assignment);
1441 $this->assertEquals(0, $updateduserflag->locked);
1442 $this->assertEquals(2, $updateduserflag->mailed);
1443 $this->assertEquals(0, $updateduserflag->extensionduedate);
1444 $this->assertEquals('readyforreview', $updateduserflag->workflowstate);
1445 $this->assertEquals($USER->id, $updateduserflag->allocatedmarker);
1446 }
1447
1f8c8f61 1448}