MDL-31890 corrected whitespaces and phdocs
[moodle.git] / grade / tests / externallib_test.php
CommitLineData
3f0ea6d8
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/**
9028d9b5 24 * Unit tests for the grading API at /grade/externallib.php
3f0ea6d8 25 *
9028d9b5 26 * @package core_grading
3f0ea6d8
PC
27 * @category external
28 * @copyright 2013 Paul Charsley
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
9028d9b5 31class core_grading_externallib_testcase extends externallib_advanced_testcase {
3f0ea6d8
PC
32
33 /**
34 * Tests set up
35 */
36 protected function setUp() {
37 global $CFG;
38 require_once($CFG->dirroot . '/grade/externallib.php');
39 }
40
41 /**
42 * Test get_definitions
43 */
dd8b6a19 44 public function test_get_definitions() {
3f0ea6d8
PC
45 global $DB, $CFG, $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 $cm = self::getDataGenerator()->create_module('assign', $assigndata);
59
60 // Create manual enrolment record.
9028d9b5
PC
61 $manualenroldata['enrol'] = 'manual';
62 $manualenroldata['status'] = 0;
63 $manualenroldata['courseid'] = $course->id;
64 $enrolid = $DB->insert_record('enrol', $manualenroldata);
3f0ea6d8
PC
65
66 // Create a teacher and give them capabilities.
67 $coursecontext = context_course::instance($course->id);
68 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
69 $modulecontext = context_module::instance($cm->id);
70 $this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
71
72 // Create the teacher's enrolment record.
9028d9b5
PC
73 $userenrolmentdata['status'] = 0;
74 $userenrolmentdata['enrolid'] = $enrolid;
75 $userenrolmentdata['userid'] = $USER->id;
76 $DB->insert_record('user_enrolments', $userenrolmentdata);
3f0ea6d8
PC
77
78 // Create a grading area.
79 $gradingarea = array(
80 'contextid' => $modulecontext->id,
81 'component' => 'mod_assign',
82 'areaname' => 'submissions',
83 'activemethod' => 'rubric'
84 );
85 $areaid = $DB->insert_record('grading_areas', $gradingarea);
86
87 // Create a rubric grading definition.
88 $rubricdefinition = array (
89 'areaid' => $areaid,
90 'method' => 'rubric',
91 'name' => 'test',
92 'status' => 20,
93 'copiedfromid' => 1,
94 'timecreated' => 1,
95 'usercreated' => $USER->id,
96 'timemodified' => 1,
97 'usermodified' => $USER->id,
98 'timecopied' => 0
99 );
100 $definitionid = $DB->insert_record('grading_definitions', $rubricdefinition);
101
102 // Create a criterion with levels.
103 $rubriccriteria1 = array (
104 'definitionid' => $definitionid,
105 'sortorder' => 1,
106 'description' => 'Demonstrate an understanding of disease control',
107 'descriptionformat' => 0
108 );
109 $criterionid1 = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria1);
110 $rubriclevel1 = array (
111 'criterionid' => $criterionid1,
112 'score' => 5,
113 'definition' => 'pass',
114 'definitionformat' => 0
115 );
116 $DB->insert_record('gradingform_rubric_levels', $rubriclevel1);
117 $rubriclevel2 = array (
118 'criterionid' => $criterionid1,
119 'score' => 10,
120 'definition' => 'excellent',
121 'definitionformat' => 0
122 );
123 $DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
124
125 // Create a second criterion with levels.
126 $rubriccriteria2 = array (
127 'definitionid' => $definitionid,
128 'sortorder' => 2,
129 'description' => 'Demonstrate an understanding of brucellosis',
130 'descriptionformat' => 0
131 );
132 $criterionid2 = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria2);
133 $rubriclevel1 = array (
134 'criterionid' => $criterionid2,
135 'score' => 5,
136 'definition' => 'pass',
137 'definitionformat' => 0
138 );
139 $DB->insert_record('gradingform_rubric_levels', $rubriclevel1);
140 $rubriclevel2 = array (
141 'criterionid' => $criterionid2,
142 'score' => 10,
143 'definition' => 'excellent',
144 'definitionformat' => 0
145 );
146 $DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
147
148 // Call the external function.
149 $cmids = array ($cm->id);
150 $areaname = 'submissions';
9028d9b5 151 $result = core_grading_external::get_definitions($cmids, $areaname);
3f0ea6d8
PC
152
153 $this->assertEquals(1, count($result['areas']));
154 $this->assertEquals(1, count($result['areas'][0]['definitions']));
155 $definition = $result['areas'][0]['definitions'][0];
156
157 $this->assertEquals($rubricdefinition['method'], $definition['method']);
158 $this->assertEquals($USER->id, $definition['usercreated']);
159
160 require_once("$CFG->dirroot/grade/grading/lib.php");
161 require_once($CFG->dirroot.'/grade/grading/form/'.$rubricdefinition['method'].'/lib.php');
162
163 $gradingmanager = get_grading_manager($areaid);
164
165 $this->assertEquals(1, count($definition[$rubricdefinition['method']]));
166
167 $rubricdetails = $definition[$rubricdefinition['method']];
168 $details = call_user_func('gradingform_'.$rubricdefinition['method'].'_controller::get_external_definition_details');
169
170 $this->assertEquals(2, count($rubricdetails[key($details)]));
171
172 $found = false;
173 foreach ($rubricdetails[key($details)] as $criterion) {
174 if ($criterion['id'] == $criterionid1) {
175 $this->assertEquals($rubriccriteria1['description'], $criterion['description']);
176 $this->assertEquals(2, count($criterion['levels']));
177 $found = true;
178 break;
179 }
180 }
181 $this->assertTrue($found);
182 }
183
9028d9b5
PC
184 /**
185 * Test get_gradingform_instances
186 */
dd8b6a19
MG
187 public function test_get_gradingform_instances() {
188 global $DB, $USER;
9028d9b5
PC
189
190 $this->resetAfterTest(true);
191 // Create a course and assignment.
192 $coursedata['idnumber'] = 'idnumbercourse';
193 $coursedata['fullname'] = 'Lightwork Course';
194 $coursedata['summary'] = 'Lightwork Course description';
195 $coursedata['summaryformat'] = FORMAT_MOODLE;
196 $course = self::getDataGenerator()->create_course($coursedata);
197
198 $assigndata['course'] = $course->id;
199 $assigndata['name'] = 'lightwork assignment';
200
201 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
202
203 // Create manual enrolment record.
204 $manualenroldata['enrol'] = 'manual';
205 $manualenroldata['status'] = 0;
206 $manualenroldata['courseid'] = $course->id;
207 $enrolid = $DB->insert_record('enrol', $manualenroldata);
208
209 // Create a teacher and give them capabilities.
210 $coursecontext = context_course::instance($course->id);
211 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
212 $modulecontext = context_module::instance($assign->id);
213 $this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
214
215 // Create the teacher's enrolment record.
216 $userenrolmentdata['status'] = 0;
217 $userenrolmentdata['enrolid'] = $enrolid;
218 $userenrolmentdata['userid'] = $USER->id;
219 $DB->insert_record('user_enrolments', $userenrolmentdata);
220
221 // Create a student with an assignment grade.
222 $student = self::getDataGenerator()->create_user();
223 $assigngrade = new stdClass();
224 $assigngrade->assignment = $assign->id;
225 $assigngrade->userid = $student->id;
226 $assigngrade->timecreated = time();
227 $assigngrade->timemodified = $assigngrade->timecreated;
228 $assigngrade->grader = $USER->id;
229 $assigngrade->grade = 50;
230 $assigngrade->attemptnumber = 0;
231 $gid = $DB->insert_record('assign_grades', $assigngrade);
232
233 // Create a grading area.
234 $gradingarea = array(
235 'contextid' => $modulecontext->id,
236 'component' => 'mod_assign',
237 'areaname' => 'submissions',
238 'activemethod' => 'rubric'
239 );
240 $areaid = $DB->insert_record('grading_areas', $gradingarea);
241
242 // Create a rubric grading definition.
243 $rubricdefinition = array (
244 'areaid' => $areaid,
245 'method' => 'rubric',
246 'name' => 'test',
247 'status' => 20,
248 'copiedfromid' => 1,
249 'timecreated' => 1,
250 'usercreated' => $USER->id,
251 'timemodified' => 1,
252 'usermodified' => $USER->id,
253 'timecopied' => 0
254 );
255 $definitionid = $DB->insert_record('grading_definitions', $rubricdefinition);
256
257 // Create a criterion with a level.
258 $rubriccriteria = array (
259 'definitionid' => $definitionid,
260 'sortorder' => 1,
261 'description' => 'Demonstrate an understanding of disease control',
262 'descriptionformat' => 0
263 );
264 $criterionid = $DB->insert_record('gradingform_rubric_criteria', $rubriccriteria);
265 $rubriclevel = array (
266 'criterionid' => $criterionid,
267 'score' => 50,
268 'definition' => 'pass',
269 'definitionformat' => 0
270 );
271 $levelid = $DB->insert_record('gradingform_rubric_levels', $rubriclevel);
272
273 // Create a grading instance.
274 $instance = array (
275 'definitionid' => $definitionid,
276 'raterid' => $USER->id,
277 'itemid' => $gid,
278 'status' => 1,
279 'feedbackformat' => 0,
280 'timemodified' => 1
281 );
282 $instanceid = $DB->insert_record('grading_instances', $instance);
283
284 // Create a filling.
285 $filling = array (
286 'instanceid' => $instanceid,
287 'criterionid' => $criterionid,
288 'levelid' => $levelid,
289 'remark' => 'excellent work',
290 'remarkformat' => 0
291 );
292 $DB->insert_record('gradingform_rubric_fillings', $filling);
293
294 // Call the external function.
295 $result = core_grading_external::get_gradingform_instances($definitionid, 0);
296
297 $this->assertEquals(1, count($result['instances']));
298 $this->assertEquals($USER->id, $result['instances'][0]['raterid']);
299 $this->assertEquals($gid, $result['instances'][0]['itemid']);
300 $this->assertEquals(1, $result['instances'][0]['status']);
301 $this->assertEquals(1, $result['instances'][0]['timemodified']);
302 $this->assertEquals(1, count($result['instances'][0]['rubric']));
303 $this->assertEquals(1, count($result['instances'][0]['rubric']['criteria']));
304 $criteria = $result['instances'][0]['rubric']['criteria'];
305 $this->assertEquals($criterionid, $criteria[$criterionid]['criterionid']);
306 $this->assertEquals($levelid, $criteria[$criterionid]['levelid']);
307 $this->assertEquals('excellent work', $criteria[$criterionid]['remark']);
308 }
3f0ea6d8 309}