MDL-30085 core_grade: added a grade web service
[moodle.git] / lib / grade / tests / externallib_test.php
CommitLineData
d3549931
AD
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 the grade API at /lib/grade/externallib.php
19 *
20 * @package core_grade
21 * @category external
22 * @copyright 2012 Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 2.6
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->libdir . '/grade/externallib.php');
33
34class core_grade_external_testcase extends externallib_advanced_testcase {
35
36 protected function load_test_data($assignmentname, $student1rawgrade, $student2rawgrade) {
37 global $DB;
38
39 // Adds a course, a teacher, 2 students, an assignment and grades for the students.
40 $course = $this->getDataGenerator()->create_course();
41 $coursecontext = context_course::instance($course->id);
42
43 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
44
45 $student1 = $this->getDataGenerator()->create_user();
46 $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
47
48 $student2 = $this->getDataGenerator()->create_user();
49 $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
50
51 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
52 $teacher = $this->getDataGenerator()->create_user();
53 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
54
55 $parent = $this->getDataGenerator()->create_user();
56 $this->setUser($parent);
57 $student1context = context_user::instance($student1->id);
58 // Creates a new role, gives it the capability and gives $USER that role.
59 $parentroleid = $this->assignUserCapability('moodle/grade:viewall', $student1context->id);
60 // Enrol the user in the course using the new role.
61 $this->getDataGenerator()->enrol_user($parent->id, $course->id, $parentroleid);
62
63 $assignment = $this->getDataGenerator()->create_module('assign', array('name' => $assignmentname, 'course' => $course->id));
64 $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id);
65 $assignment->cmidnumber = $modcontext->id;
66
67 $student1grade = array('userid' => $student1->id, 'rawgrade' => $student1rawgrade);
68 $student2grade = array('userid' => $student2->id, 'rawgrade' => $student2rawgrade);
69 $studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade);
70 assign_grade_item_update($assignment, $studentgrades);
71
72 // Insert a custom grade scale to be used by an outcome.
73 $grade_scale = new grade_scale();
74 $grade_scale->name = 'unittestscale3';
75 $grade_scale->courseid = $course->id;
76 $grade_scale->userid = 0;
77 $grade_scale->scale = 'Distinction, Very Good, Good, Pass, Fail';
78 $grade_scale->description = 'This scale is used to mark standard assignments.';
79 $grade_scale->insert();
80
81 // Insert an outcome.
82 $data = new stdClass();
83 $data->courseid = $course->id;
84 $data->fullname = 'Team work';
85 $data->shortname = 'Team work';
86 $data->scaleid = $grade_scale->id;
87 $outcome = new grade_outcome($data, false);
88 $outcome->insert();
89
90 $outcome_gradeitem = new grade_item();
91 $outcome_gradeitem->itemname = $outcome->shortname;
92 $outcome_gradeitem->itemtype = 'mod';
93 $outcome_gradeitem->itemmodule = 'assign';
94 $outcome_gradeitem->iteminstance = $assignment->id;
95 $outcome_gradeitem->outcomeid = $outcome->id;
96 $outcome_gradeitem->cmid = 0;
97 $outcome_gradeitem->courseid = $course->id;
98 $outcome_gradeitem->aggregationcoef = 0;
99 $outcome_gradeitem->itemnumber = 1; // The activity's original grade item will be 0.
100 $outcome_gradeitem->gradetype = GRADE_TYPE_SCALE;
101 $outcome_gradeitem->scaleid = $outcome->scaleid;
102 $outcome_gradeitem->insert();
103
104 $assignment_gradeitem = grade_item::fetch(
105 array(
106 'itemtype' => 'mod',
107 'itemmodule' => 'assign',
108 'iteminstance' => $assignment->id,
109 'itemnumber' => 0,
110 'courseid' => $course->id
111 )
112 );
113 $outcome_gradeitem->set_parent($assignment_gradeitem->categoryid);
114 $outcome_gradeitem->move_after_sortorder($assignment_gradeitem->sortorder);
115
116 return array($course, $assignment, $student1, $student2, $teacher, $parent);
117 }
118
119 /**
120 * Test get_grades()
121 */
122 public function test_get_grades() {
123 global $CFG;
124
125 $this->resetAfterTest(true);
126 $CFG->enableoutcomes = 1;
127
128 $assignmentname = 'The assignment';
129 $student1rawgrade = 10;
130 $student2rawgrade = 20;
131 list($course, $assignment, $student1, $student2, $teacher, $parent) = $this->load_test_data($assignmentname, $student1rawgrade, $student2rawgrade);
132 $assigment_cm = get_coursemodule_from_id('assign', $assignment->id, 0, false, MUST_EXIST);
133
134 // Student requesting their own grade for the assignment.
135 $this->setUser($student1);
136 $grades = core_grade_external::get_grades(
137 $course->id,
138 'mod_assign',
139 $assigment_cm->id,
140 array($student1->id)
141 );
142 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
143 $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, $assigment_cm->id, $student1->id));
144
145 // Student requesting all of their grades in a course.
146 $grades = core_grade_external::get_grades(
147 $course->id,
148 null,
149 null,
150 array($student1->id)
151 );
152 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
153 $this->assertTrue(count($grades['items']) == 2);
154 $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, $assigment_cm->id, $student1->id));
155 $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, 'course', $student1->id));
156
157 $outcome = $this->get_outcome($grades, $assigment_cm->id);
158 $this->assertEquals($outcome['name'], 'Team work');
159 $this->assertEquals(0, $this->get_outcome_student_grade($grades, $assigment_cm->id, $student1->id));
160
161 // Student requesting another student's grade for the assignment (should fail).
162 try {
163 $grades = core_grade_external::get_grades(
164 $course->id,
165 'mod_assign',
166 $assigment_cm->id,
167 array($student2->id)
168 );
169 $this->fail('moodle_exception expected');
170 } catch (moodle_exception $ex) {
171 $this->assertTrue(true);
172 }
173
174 // Parent requesting their child's grade for the assignment.
175 $this->setUser($parent);
176 $grades = core_grade_external::get_grades(
177 $course->id,
178 'mod_assign',
179 $assigment_cm->id,
180 array($student1->id)
181 );
182 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
183 $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, $assigment_cm->id, $student1->id));
184
185 // Parent requesting another student's grade for the assignment(should fail).
186 try {
187 $grades = core_grade_external::get_grades(
188 $course->id,
189 'mod_assign',
190 $assigment_cm->id,
191 array($student2->id)
192 );
193 $this->fail('moodle_exception expected');
194 } catch (moodle_exception $ex) {
195 $this->assertTrue(true);
196 }
197
198 // Student requesting all other student grades for the assignment (should fail).
199 try {
200 $grades = core_grade_external::get_grades(
201 $course->id,
202 'mod_assign',
203 $assigment_cm->id,
204 array($student1->id, $student2->id)
205 );
206 $this->fail('moodle_exception expected');
207 } catch (moodle_exception $ex) {
208 $this->assertTrue(true);
209 }
210
211 // Student requesting only grade item information (should fail).
212 try {
213 $grades = core_grade_external::get_grades(
214 $course->id,
215 'mod_assign',
216 $assigment_cm->id,
217 array()
218 );
219 $this->fail('moodle_exception expected');
220 } catch (moodle_exception $ex) {
221 $this->assertTrue(true);
222 }
223
224 // Teacher requesting student grades for a course.
225 $this->setUser($teacher);
226 $grades = core_grade_external::get_grades(
227 $course->id,
228 'mod_assign',
229 $assigment_cm->id,
230 array($student1->id, $student2->id)
231 );
232 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
233 $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, $assigment_cm->id, $student1->id));
234 $this->assertEquals($student2rawgrade, $this->get_activity_student_grade($grades, $assigment_cm->id, $student2->id));
235
236 // Teacher requesting grade item information.
237 $grades = core_grade_external::get_grades(
238 $course->id,
239 'mod_assign',
240 $assigment_cm->id
241 );
242 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
243 $activity = $this->get_activity($grades, $assigment_cm->id);
244 $this->assertEquals($activity['name'], $assignmentname);
245 $this->assertEquals(count($activity['grades']), 0);
246
247 // Teacher requesting all grade items in a course.
248 $grades = core_grade_external::get_grades(
249 $course->id
250 );
251 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
252 $this->assertTrue(count($grades['items']) == 2);
253
254 $activity = $this->get_activity($grades, $assigment_cm->id);
255 $this->assertEquals($activity['name'], $assignmentname);
256 $this->assertEquals(count($activity['grades']), 0);
257
258 $outcome = $this->get_outcome($grades, $assigment_cm->id);
259 $this->assertEquals($outcome['name'], 'Team work');
260
261 // Hide a grade item then have student request it
262 $result = core_grade_external::update_grades(
263 'test',
264 $course->id,
265 'mod_assign',
266 $assigment_cm->id,
267 0,
268 array(),
269 array('hidden' => 1)
270 );
271 $this->assertTrue($result == GRADE_UPDATE_OK);
272
273 // Check it's definitely hidden.
274 $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
275 $this->assertEquals($grades->items[0]->hidden, 1);
276
277 // Student should now not be able to see it.
278 $this->setUser($student1);
279 $grades = core_grade_external::get_grades(
280 $course->id,
281 'mod_assign',
282 $assigment_cm->id,
283 array($student1->id)
284 );
285 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
286 $this->assertEquals(null, $this->get_activity($grades, $assigment_cm->id));
287
288 // Teacher should still be able to see the hidden grades.
289 $this->setUser($teacher);
290 $grades = core_grade_external::get_grades(
291 $course->id,
292 'mod_assign',
293 $assigment_cm->id,
294 array($student1->id)
295 );
296 $grades = external_api::clean_returnvalue(core_grade_external::get_grades_returns(), $grades);
297 $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, $assigment_cm->id, $student1->id));
298 }
299
300 private function get_activity($grades, $cmid) {
301 foreach ($grades['items'] as $item) {
302 if ($item['activityid'] == $cmid) {
303 return $item;
304 }
305 }
306 return null;
307 }
308
309 private function get_activity_student_grade($grades, $cmid, $studentid) {
310 $item = $this->get_activity($grades, $cmid);
311 foreach ($item['grades'] as $grade) {
312 if ($grade['userid'] == $studentid) {
313 return $grade['grade'];
314 }
315 }
316 return null;
317 }
318
319 private function get_outcome($grades, $cmid) {
320 foreach($grades['outcomes'] as $outcome) {
321 if ($outcome['activityid'] == $cmid) {
322 return $outcome;
323 }
324 }
325 return null;
326 }
327
328 private function get_outcome_student_grade($grades, $cmid, $studentid) {
329 $outcome = $this->get_outcome($grades, $cmid);
330 foreach ($outcome['grades'] as $grade) {
331 if ($grade['userid'] == $studentid) {
332 return $grade['grade'];
333 }
334 }
335 return null;
336 }
337
338 /**
339 * Test get_grades()
340 */
341 public function test_update_grades() {
342 global $DB;
343
344 $this->resetAfterTest(true);
345
346 $assignmentname = 'The assignment';
347 $student1rawgrade = 10;
348 $student2rawgrade = 20;
349 list($course, $assignment, $student1, $student2, $teacher, $parent) = $this->load_test_data($assignmentname, $student1rawgrade, $student2rawgrade);
350 $assigment_cm = get_coursemodule_from_id('assign', $assignment->id, 0, false, MUST_EXIST);
351
352 $this->setUser($teacher);
353
354 // Teacher updating grade item information
355 $changedmax = 93;
356 $result = core_grade_external::update_grades(
357 'test',
358 $course->id,
359 'mod_assign',
360 $assigment_cm->id,
361 0,
362 array(),
363 array('grademax' => $changedmax)
364 );
365 $this->assertTrue($result == GRADE_UPDATE_OK);
366 $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
367 $this->assertTrue($grades->items[0]->grademax == $changedmax);
368
369 // Teacher updating 1 student grade
370 $student1grade = 23;
371 $result = core_grade_external::update_grades(
372 'test',
373 $course->id,
374 'mod_assign',
375 $assigment_cm->id,
376 0,
377 array(array('studentid' => $student1->id, 'grade' => $student1grade))
378 );
379 $this->assertTrue($result == GRADE_UPDATE_OK);
380 $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id, array($student1->id));
381 $this->assertTrue($grades->items[0]->grades[$student1->id]->grade == $student1grade);
382
383 // Teacher updating multiple student grades
384 $student1grade = 11;
385 $student2grade = 13;
386 $result = core_grade_external::update_grades(
387 'test',
388 $course->id,
389 'mod_assign',
390 $assigment_cm->id,
391 0,
392 array(
393 array('studentid' => $student1->id, 'grade' => $student1grade),
394 array('studentid' => $student2->id, 'grade' => $student2grade)
395 )
396 );
397 $this->assertTrue($result == GRADE_UPDATE_OK);
398 $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id, array($student1->id, $student2->id));
399 $this->assertTrue($grades->items[0]->grades[$student1->id]->grade == $student1grade);
400 $this->assertTrue($grades->items[0]->grades[$student2->id]->grade == $student2grade);
401
402 // Student attempting to update their own grade (should fail)
403 $this->setUser($student1);
404 try {
405 $student1grade = 17;
406 $result = core_grade_external::update_grades(
407 'test',
408 $course->id,
409 'mod_assign',
410 $assigment_cm->id,
411 0,
412 array( array('studentid' => $student1->id, 'grade' => $student1grade))
413 );
414 $this->fail('moodle_exception expected');
415 } catch (moodle_exception $ex) {
416 $this->assertTrue(true);
417 }
418
419 // Parent attempting to update their child's grade (should fail)
420 $this->setUser($parent);
421 try {
422 $student1grade = 13;
423 $result = core_grade_external::update_grades(
424 'test',
425 $course->id,
426 'mod_assign',
427 $assigment_cm->id,
428 0,
429 array( array('studentid' => $student1->id, 'grade' => $student1grade))
430 );
431 $this->fail('moodle_exception expected');
432 } catch (moodle_exception $ex) {
433 $this->assertTrue(true);
434 }
435
436 // Student trying to hide a grade item (should fail).
437 $this->setUser($student1);
438 try {
439 $result = core_grade_external::update_grades(
440 'test',
441 $course->id,
442 'mod_assign',
443 $assigment_cm->id,
444 0,
445 array(),
446 array('hidden' => 1)
447 );
448 $this->fail('moodle_exception expected');
449 } catch (moodle_exception $ex) {
450 $this->assertTrue(true);
451 }
452
453 // Give the student role 'moodle/grade:hide' and they should now be able to hide the grade item.
454 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
455 $coursecontext = context_course::instance($course->id);
456 assign_capability('moodle/grade:hide', CAP_ALLOW, $studentrole->id, $coursecontext->id);
457 accesslib_clear_all_caches_for_unit_testing();
458
459 // Check the activity isn't already hidden.
460 $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
461 $this->assertTrue($grades->items[0]->hidden == 0);
462
463 $result = core_grade_external::update_grades(
464 'test',
465 $course->id,
466 'mod_assign',
467 $assigment_cm->id,
468 0,
469 array(),
470 array('hidden' => 1)
471 );
472 $this->assertTrue($result == GRADE_UPDATE_OK);
473 $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
474 $this->assertTrue($grades->items[0]->hidden == 1);
475 }
476
477}