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