MDL-30085 core_grades functions moved to correct location
[moodle.git] / grade / tests / externallib_grades_test.php
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/>.
17 /**
18  * Unit tests for the grade API at /grade/externallib.php
19  *
20  * @package    core_grades
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.7
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/grade/externallib.php');
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  */
42 class core_grades_external_testcase extends externallib_advanced_testcase {
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      */
52     protected function load_test_data($assignmentname, $student1rawgrade, $student2rawgrade) {
53         global $DB;
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);
59         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
61         $student1 = $this->getDataGenerator()->create_user();
62         $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
64         $student2 = $this->getDataGenerator()->create_user();
65         $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
67         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
68         $teacher = $this->getDataGenerator()->create_user();
69         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
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);
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;
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);
88         // Insert a custom grade scale to be used by an outcome.
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();
97         // Insert an outcome.
98         $data = new stdClass();
99         $data->courseid = $course->id;
100         $data->fullname = 'Team work';
101         $data->shortname = 'Team work';
102         $data->scaleid = $gradescale->id;
103         $outcome = new grade_outcome($data, false);
104         $outcome->insert();
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();
120         $assignmentgradeitem = grade_item::fetch(
121             array(
122                 'itemtype' => 'mod',
123                 'itemmodule' => 'assign',
124                 'iteminstance' => $assignment->id,
125                 'itemnumber' => 0,
126                 'courseid' => $course->id
127             )
128         );
129         $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
130         $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
132         return array($course, $assignment, $student1, $student2, $teacher, $parent);
133     }
135     /**
136      * Test get_grades()
137      */
138     public function test_get_grades() {
139         global $CFG;
141         $this->resetAfterTest(true);
142         $CFG->enableoutcomes = 1;
144         $assignmentname = 'The assignment';
145         $student1rawgrade = 10;
146         $student2rawgrade = 20;
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);
151         // Student requesting their own grade for the assignment.
152         $this->setUser($student1);
153         $grades = core_grades_external::get_grades(
154             $course->id,
155             'mod_assign',
156             $assigmentcm->id,
157             array($student1->id)
158         );
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));
162         // Student requesting all of their grades in a course.
163         $grades = core_grades_external::get_grades(
164             $course->id,
165             null,
166             null,
167             array($student1->id)
168         );
169         $grades = external_api::clean_returnvalue(core_grades_external::get_grades_returns(), $grades);
170         $this->assertTrue(count($grades['items']) == 2);
171         $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, $assigmentcm->id, $student1->id));
172         $this->assertEquals($student1rawgrade, $this->get_activity_student_grade($grades, 'course', $student1->id));
174         $outcome = $this->get_outcome($grades, $assigmentcm->id);
175         $this->assertEquals($outcome['name'], 'Team work');
176         $this->assertEquals(0, $this->get_outcome_student_grade($grades, $assigmentcm->id, $student1->id));
178         // Student requesting another student's grade for the assignment (should fail).
179         try {
180             $grades = core_grades_external::get_grades(
181                 $course->id,
182                 'mod_assign',
183                 $assigmentcm->id,
184                 array($student2->id)
185             );
186             $this->fail('moodle_exception expected');
187         } catch (moodle_exception $ex) {
188             $this->assertTrue(true);
189         }
191         // Parent requesting their child's grade for the assignment.
192         $this->setUser($parent);
193         $grades = core_grades_external::get_grades(
194             $course->id,
195             'mod_assign',
196             $assigmentcm->id,
197             array($student1->id)
198         );
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));
202         // Parent requesting another student's grade for the assignment(should fail).
203         try {
204             $grades = core_grades_external::get_grades(
205                 $course->id,
206                 'mod_assign',
207                 $assigmentcm->id,
208                 array($student2->id)
209             );
210             $this->fail('moodle_exception expected');
211         } catch (moodle_exception $ex) {
212             $this->assertTrue(true);
213         }
215         // Student requesting all other student grades for the assignment (should fail).
216         try {
217             $grades = core_grades_external::get_grades(
218                 $course->id,
219                 'mod_assign',
220                 $assigmentcm->id,
221                 array($student1->id, $student2->id)
222             );
223             $this->fail('moodle_exception expected');
224         } catch (moodle_exception $ex) {
225             $this->assertTrue(true);
226         }
228         // Student requesting only grade item information (should fail).
229         try {
230             $grades = core_grades_external::get_grades(
231                 $course->id,
232                 'mod_assign',
233                 $assigmentcm->id,
234                 array()
235             );
236             $this->fail('moodle_exception expected');
237         } catch (moodle_exception $ex) {
238             $this->assertTrue(true);
239         }
241         // Teacher requesting student grades for a course.
242         $this->setUser($teacher);
243         $grades = core_grades_external::get_grades(
244             $course->id,
245             'mod_assign',
246             $assigmentcm->id,
247             array($student1->id, $student2->id)
248         );
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));
253         // Teacher requesting grade item information.
254         $grades = core_grades_external::get_grades(
255             $course->id,
256             'mod_assign',
257             $assigmentcm->id
258         );
259         $grades = external_api::clean_returnvalue(core_grades_external::get_grades_returns(), $grades);
260         $activity = $this->get_activity($grades, $assigmentcm->id);
261         $this->assertEquals($activity['name'], $assignmentname);
262         $this->assertEquals(count($activity['grades']), 0);
264         // Teacher requesting all grade items in a course.
265         $grades = core_grades_external::get_grades(
266             $course->id
267         );
268         $grades = external_api::clean_returnvalue(core_grades_external::get_grades_returns(), $grades);
269         $this->assertTrue(count($grades['items']) == 2);
271         $activity = $this->get_activity($grades, $assigmentcm->id);
272         $this->assertEquals($activity['name'], $assignmentname);
273         $this->assertEquals(count($activity['grades']), 0);
275         $outcome = $this->get_outcome($grades, $assigmentcm->id);
276         $this->assertEquals($outcome['name'], 'Team work');
278         // Hide a grade item then have student request it.
279         $result = core_grades_external::update_grades(
280             'test',
281             $course->id,
282             'mod_assign',
283             $assigmentcm->id,
284             0,
285             array(),
286             array('hidden' => 1)
287         );
288         $this->assertTrue($result == GRADE_UPDATE_OK);
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);
294         // Student should now not be able to see it.
295         $this->setUser($student1);
296         $grades = core_grades_external::get_grades(
297             $course->id,
298             'mod_assign',
299             $assigmentcm->id,
300             array($student1->id)
301         );
302         $grades = external_api::clean_returnvalue(core_grades_external::get_grades_returns(), $grades);
303         $this->assertEquals(null, $this->get_activity($grades, $assigmentcm->id));
305         // Teacher should still be able to see the hidden grades.
306         $this->setUser($teacher);
307         $grades = core_grades_external::get_grades(
308             $course->id,
309             'mod_assign',
310             $assigmentcm->id,
311             array($student1->id)
312         );
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));
315     }
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      */
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     }
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      */
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     }
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      */
358     private function get_outcome($grades, $cmid) {
359         foreach ($grades['outcomes'] as $outcome) {
360             if ($outcome['activityid'] == $cmid) {
361                 return $outcome;
362             }
363         }
364         return null;
365     }
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      */
375     private function get_outcome_student_grade($grades, $cmid, $studentid) {
376         $outcome = $this->get_outcome($grades, $cmid);
377         foreach ($outcome['grades'] as $grade) {
378             if ($grade['userid'] == $studentid) {
379                 return $grade['grade'];
380             }
381         }
382         return null;
383     }
385     /**
386      * Test update_grades()
387      */
388     public function test_update_grades() {
389         global $DB;
391         $this->resetAfterTest(true);
393         $assignmentname = 'The assignment';
394         $student1rawgrade = 10;
395         $student2rawgrade = 20;
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);
400         $this->setUser($teacher);
402         // Teacher updating grade item information.
403         $changedmax = 93;
404         $result = core_grades_external::update_grades(
405             'test',
406             $course->id,
407             'mod_assign',
408             $assigmentcm->id,
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);
417         // Teacher updating 1 student grade.
418         $student1grade = 23;
419         $result = core_grades_external::update_grades(
420             'test',
421             $course->id,
422             'mod_assign',
423             $assigmentcm->id,
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);
431         // Teacher updating multiple student grades.
432         $student1grade = 11;
433         $student2grade = 13;
434         $result = core_grades_external::update_grades(
435             'test',
436             $course->id,
437             'mod_assign',
438             $assigmentcm->id,
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);
450         // Student attempting to update their own grade (should fail).
451         $this->setUser($student1);
452         try {
453             $student1grade = 17;
454             $result = core_grades_external::update_grades(
455                 'test',
456                 $course->id,
457                 'mod_assign',
458                 $assigmentcm->id,
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         }
467         // Parent attempting to update their child's grade (should fail).
468         $this->setUser($parent);
469         try {
470             $student1grade = 13;
471             $result = core_grades_external::update_grades(
472                 'test',
473                 $course->id,
474                 'mod_assign',
475                 $assigmentcm->id,
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         }
484         // Student trying to hide a grade item (should fail).
485         $this->setUser($student1);
486         try {
487             $result = core_grades_external::update_grades(
488                 'test',
489                 $course->id,
490                 'mod_assign',
491                 $assigmentcm->id,
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         }
501         // Give the student role 'moodle/grade:hide' and they should now be able to hide the grade item.
502         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
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();
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);
511         $result = core_grades_external::update_grades(
512             'test',
513             $course->id,
514             'mod_assign',
515             $assigmentcm->id,
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     }