Merge remote branch 'origin/master' into rubric
[moodle.git] / grade / grading / lib.php
CommitLineData
4333580e
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Advanced grading methods support
20 *
21 * @package core
22 * @subpackage grading
23 * @copyright 2011 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Factory method returning an instance of the grading manager
31 *
fb13a148 32 * @param stdClass|int $context or $areaid
4333580e 33 * @param string $component the frankenstyle name of the component
8a4acb3a 34 * @param string $area the name of the gradable area
4333580e
DM
35 * @return grading_manager
36 */
fb13a148
DM
37function get_grading_manager($context_or_areaid = null, $component = null, $area = null) {
38 global $DB;
4333580e
DM
39
40 $manager = new grading_manager();
41
fb13a148
DM
42 if (is_object($context_or_areaid)) {
43 $context = $context_or_areaid;
44 } else {
45 $context = null;
46
47 if (is_numeric($context_or_areaid)) {
48 $manager->load($context_or_areaid);
49 return $manager;
50 }
51 }
52
4333580e
DM
53 if (!is_null($context)) {
54 $manager->set_context($context);
55 }
56
57 if (!is_null($component)) {
58 $manager->set_component($component);
59 }
60
8a4acb3a
DM
61 if (!is_null($area)) {
62 $manager->set_area($area);
4333580e
DM
63 }
64
65 return $manager;
66}
67
68/**
69 * General class providing access to common grading features
70 *
9b8550f8
DM
71 * Grading manager provides access to the particular grading method controller
72 * in that area.
73 *
4333580e
DM
74 * Fully initialized instance of the grading manager operates over a single
75 * gradable area. It is possible to work with a partially initialized manager
8a4acb3a
DM
76 * that knows just context and component without known area, for example.
77 * It is also possible to change context, component and area of an existing
4333580e
DM
78 * manager. Such pattern is used when copying form definitions, for example.
79 */
80class grading_manager {
81
82 /** @var stdClass the context */
83 protected $context;
84
85 /** @var string the frankenstyle name of the component */
86 protected $component;
87
88 /** @var string the name of the gradable area */
8a4acb3a 89 protected $area;
4333580e 90
9b8550f8
DM
91 /** @var stdClass|false|null the raw record from {grading_areas}, false if does not exist, null if invalidated cache */
92 private $areacache = null;
93
21d37aa6
DM
94 /**
95 * @return stdClass grading manager context
96 */
97 public function get_context() {
98 return $this->context;
99 }
100
4333580e
DM
101 /**
102 * Sets the context the manager operates on
103 *
104 * @param stdClass $context
105 */
106 public function set_context(stdClass $context) {
9b8550f8 107 $this->areacache = null;
4333580e
DM
108 $this->context = $context;
109 }
110
21d37aa6
DM
111 /**
112 * @return string grading manager component
113 */
114 public function get_component() {
115 return $this->component;
116 }
117
4333580e
DM
118 /**
119 * Sets the component the manager operates on
120 *
121 * @param string $component the frankenstyle name of the component
122 */
123 public function set_component($component) {
9b8550f8
DM
124 $this->areacache = null;
125 list($type, $name) = normalize_component($component);
126 $this->component = $type.'_'.$name;
4333580e
DM
127 }
128
21d37aa6
DM
129 /**
130 * @return string grading manager area name
131 */
132 public function get_area() {
133 return $this->area;
134 }
135
4333580e 136 /**
8a4acb3a 137 * Sets the area the manager operates on
4333580e 138 *
8a4acb3a 139 * @param string $area the name of the gradable area
4333580e 140 */
8a4acb3a 141 public function set_area($area) {
9b8550f8 142 $this->areacache = null;
8a4acb3a 143 $this->area = $area;
4333580e
DM
144 }
145
fb13a148
DM
146 /**
147 * Loads the gradable area info from the database
148 *
149 * @param int $areaid
150 */
151 public function load($areaid) {
152 global $DB;
153
154 $this->areacache = $DB->get_record('grading_areas', array('id' => $areaid), '*', MUST_EXIST);
155 $this->context = get_context_instance_by_id($this->areacache->contextid, MUST_EXIST);
156 $this->component = $this->areacache->component;
157 $this->area = $this->areacache->areaname;
158 }
159
4333580e
DM
160 /**
161 * Returns the list of available grading methods in the given context
162 *
163 * Basically this returns the list of installed grading plugins with an empty value
164 * for simple direct grading. In the future, the list of available methods may be
165 * controlled per-context.
166 *
167 * Requires the context property to be set in advance.
6c9e506c
DM
168 *
169 * @param bool $includenone should the 'Simple direct grading' be included
4333580e
DM
170 * @return array of the (string)name => (string)localized title of the method
171 */
6c9e506c 172 public function get_available_methods($includenone = true) {
4333580e
DM
173
174 $this->ensure_isset(array('context'));
175
6c9e506c
DM
176 if ($includenone) {
177 $list = array('' => get_string('gradingmethodnone', 'core_grading'));
178 } else {
179 $list = array();
180 }
181
182 foreach (get_plugin_list('gradingform') as $name => $location) {
183 $list[$name] = get_string('pluginname', 'gradingform_'.$name);
184 }
185
186 return $list;
4333580e
DM
187 }
188
189 /**
190 * Returns the list of gradable areas in the given context and component
191 *
192 * This performs a callback to the library of the relevant plugin to obtain
193 * the list of supported areas.
194 * @return array of (string)areacode => (string)localized title of the area
195 */
196 public function get_available_areas() {
197 global $CFG;
198
199 $this->ensure_isset(array('context', 'component'));
200
201 // example: if the given context+component lead to mod_assignment, this method
202 // will do something like
203 // require_once($CFG->dirroot.'/mod/assignment/lib.php');
204 // return assignment_gradable_area_list();
205
206 // todo - hardcoded list for now
9b8550f8 207 return array('submission' => 'Submissions');
4333580e
DM
208 }
209
210 /**
8a4acb3a 211 * Returns the currently active grading method in the gradable area
4333580e 212 *
64402867 213 * @return string|null the name of the grading plugin of null if it has not been set
4333580e 214 */
8a4acb3a 215 public function get_active_method() {
64402867
DM
216 global $DB;
217
218 $this->ensure_isset(array('context', 'component', 'area'));
219
220 // get the current grading area record if it exists
9b8550f8
DM
221 if (is_null($this->areacache)) {
222 $this->areacache = $DB->get_record('grading_areas', array(
223 'contextid' => $this->context->id,
224 'component' => $this->component,
225 'areaname' => $this->area),
226 '*', IGNORE_MISSING);
227 }
64402867 228
9b8550f8 229 if ($this->areacache === false) {
64402867
DM
230 // no area record yet
231 return null;
232 }
233
9b8550f8 234 return $this->areacache->activemethod;
64402867
DM
235 }
236
237 /**
238 * Sets the currently active grading method in the gradable area
239 *
240 * @param string $method the method name, eg 'rubric' (must be available)
241 */
242 public function set_active_method($method) {
243 global $DB;
244
8a4acb3a 245 $this->ensure_isset(array('context', 'component', 'area'));
64402867 246
8cd65f16
DM
247 // make sure the passed method is empty or a valid plugin name
248 if (empty($method)) {
249 $method = null;
250 } else {
251 if ('gradingform_'.$method !== clean_param('gradingform_'.$method, PARAM_COMPONENT)) {
252 throw new moodle_exception('invalid_method_name', 'core_grading');
253 }
254 $available = $this->get_available_methods(false);
255 if (!array_key_exists($method, $available)) {
256 throw new moodle_exception('invalid_method_name', 'core_grading');
257 }
64402867
DM
258 }
259
260 // get the current grading area record if it exists
9b8550f8
DM
261 if (is_null($this->areacache)) {
262 $this->areacache = $DB->get_record('grading_areas', array(
263 'contextid' => $this->context->id,
264 'component' => $this->component,
265 'areaname' => $this->area),
266 '*', IGNORE_MISSING);
267 }
64402867 268
9b8550f8 269 if ($this->areacache === false) {
64402867
DM
270 // no area record yet, create one with the active method set
271 $area = array(
272 'contextid' => $this->context->id,
273 'component' => $this->component,
274 'areaname' => $this->area,
275 'activemethod' => $method);
276 $DB->insert_record('grading_areas', $area);
277
278 } else {
279 // update the existing record if needed
8cd65f16 280 if ($this->areacache->activemethod !== $method) {
9b8550f8 281 $DB->set_field('grading_areas', 'activemethod', $method, array('id' => $this->areacache->id));
64402867
DM
282 }
283 }
9b8550f8
DM
284
285 $this->areacache = null;
286 }
287
288 /**
289 * Extends the settings navigation with the grading settings
290 *
291 * This function is called when the context for the page is an activity module with the
292 * FEATURE_ADVANCED_GRADING and the user has the permission moodle/grade:managegradingforms.
293 *
294 * @param settings_navigation $settingsnav {@link settings_navigation}
295 * @param navigation_node $modulenode {@link navigation_node}
296 */
297 public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $modulenode=null) {
298 global $PAGE, $CFG;
299
300 $this->ensure_isset(array('context', 'component'));
301
302 $areas = $this->get_available_areas();
303
304 if (empty($areas)) {
305 // no money, no funny
306 return;
307 }
308
309 foreach ($areas as $areaname => $areatitle) {
310 $this->set_area($areaname);
311 $method = $this->get_active_method();
312
313 if (empty($method)) {
314 // no grading method selected for the given area - nothing to display
315 continue;
316 }
317
318 if (count($areas) > 1) {
319 // if the module supports multiple gradable areas, make a node for each of them
320 $node = $modulenode->add(get_string('gradinginarea', 'core_grading', $areatitle), null, settings_navigation::NODETYPE_BRANCH);
321 } else {
322 // otherwise put the items directly into the module's node
323 $node = $modulenode;
324 }
325
326 $controller = $this->get_controller($method);
327 $controller->extend_settings_navigation($settingsnav, $node);
328 }
329 }
330
331 /**
332 * Returns the given method's controller in the gradable area
333 *
334 * @param string $method the method name, eg 'rubric' (must be available)
335 * @return grading_controller
336 */
337 public function get_controller($method) {
338 global $CFG;
339
340 $this->ensure_isset(array('context', 'component', 'area'));
341
342 // make sure the passed method is a valid plugin name
343 if ('gradingform_'.$method !== clean_param('gradingform_'.$method, PARAM_COMPONENT)) {
344 throw new moodle_exception('invalid_method_name', 'core_grading');
345 }
346 $available = $this->get_available_methods(false);
347 if (!array_key_exists($method, $available)) {
348 throw new moodle_exception('invalid_method_name', 'core_grading');
349 }
350
351 // get the current grading area record if it exists
352 if (is_null($this->areacache)) {
353 $this->areacache = $DB->get_record('grading_areas', array(
354 'contextid' => $this->context->id,
355 'component' => $this->component,
356 'areaname' => $this->area),
357 '*', IGNORE_MISSING);
358 }
359
360 if ($this->areacache === false) {
361 // no area record yet, create one
362 $area = array(
363 'contextid' => $this->context->id,
364 'component' => $this->component,
365 'areaname' => $this->area);
366 $areaid = $DB->insert_record('grading_areas', $area);
367 // reload the cache
368 $this->areacache = $DB->get_record('grading_areas', array('id' => $areaid), '*', MUST_EXIST);
369 }
370
371 require_once($CFG->dirroot.'/grade/grading/form/'.$method.'/lib.php');
3e43eff5 372 $classname = 'gradingform_'.$method.'_controller';
9b8550f8
DM
373
374 return new $classname($this->context, $this->component, $this->area, $this->areacache->id);
4333580e
DM
375 }
376
9b8550f8 377 ////////////////////////////////////////////////////////////////////////////
64402867 378
4333580e
DM
379 /**
380 * Make sure that the given properties were set to some not-null value
381 *
382 * @param array $properties the list of properties
383 * @throws coding_exception
384 */
385 private function ensure_isset(array $properties) {
386 foreach ($properties as $property) {
387 if (!isset($this->$property)) {
9b8550f8 388 throw new coding_exception('The property "'.$property.'" is not set.');
4333580e
DM
389 }
390 }
391 }
392}