Files embedded into the form description are now served by the core itself
[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
6832a102
DM
146 /**
147 * Returns a text describing the context and the component
148 *
149 * At the moment this works for gradable areas in course modules. In the future, this
150 * method should be improved so it works for other contexts (blocks, gradebook items etc)
151 * or subplugins.
152 *
153 * @return string
154 */
155 public function get_component_title() {
156
157 $this->ensure_isset(array('context', 'component'));
6832a102 158
fde33804
DM
159 if ($this->get_context()->contextlevel == CONTEXT_SYSTEM) {
160 if ($this->get_component() == 'core_grading') {
161 $title = ''; // we are in the bank UI
162 } else {
163 throw new coding_exception('Unsupported component at the system context');
164 }
165
166 } else if ($this->get_context()->contextlevel >= CONTEXT_COURSE) {
167 list($context, $course, $cm) = get_context_info_array($this->get_context()->id);
168
169 if (!empty($cm->name)) {
170 $title = $cm->name;
171 } else {
172 debugging('Gradable areas are currently supported at the course module level only', DEBUG_DEVELOPER);
173 $title = $this->get_component();
174 }
175
6832a102 176 } else {
fde33804 177 throw new coding_exception('Unsupported gradable area context level');
6832a102
DM
178 }
179
180 return $title;
181 }
182
183 /**
184 * Returns the localized title of the currently set area
185 *
186 * @return string
187 */
188 public function get_area_title() {
189
fde33804
DM
190 if ($this->get_context()->contextlevel == CONTEXT_SYSTEM) {
191 return '';
192
193 } else if ($this->get_context()->contextlevel >= CONTEXT_COURSE) {
194 $this->ensure_isset(array('context', 'component', 'area'));
195 $areas = $this->get_available_areas();
196 if (array_key_exists($this->get_area(), $areas)) {
197 return $areas[$this->get_area()];
198 } else {
199 debugging('Unknown area!');
200 return '???';
201 }
6832a102 202
fde33804
DM
203 } else {
204 throw new coding_exception('Unsupported context level');
205 }
6832a102
DM
206 }
207
fb13a148
DM
208 /**
209 * Loads the gradable area info from the database
210 *
211 * @param int $areaid
212 */
213 public function load($areaid) {
214 global $DB;
215
216 $this->areacache = $DB->get_record('grading_areas', array('id' => $areaid), '*', MUST_EXIST);
217 $this->context = get_context_instance_by_id($this->areacache->contextid, MUST_EXIST);
218 $this->component = $this->areacache->component;
219 $this->area = $this->areacache->areaname;
220 }
221
4333580e
DM
222 /**
223 * Returns the list of available grading methods in the given context
224 *
225 * Basically this returns the list of installed grading plugins with an empty value
226 * for simple direct grading. In the future, the list of available methods may be
227 * controlled per-context.
228 *
229 * Requires the context property to be set in advance.
6c9e506c
DM
230 *
231 * @param bool $includenone should the 'Simple direct grading' be included
4333580e
DM
232 * @return array of the (string)name => (string)localized title of the method
233 */
6c9e506c 234 public function get_available_methods($includenone = true) {
4333580e
DM
235
236 $this->ensure_isset(array('context'));
237
6c9e506c
DM
238 if ($includenone) {
239 $list = array('' => get_string('gradingmethodnone', 'core_grading'));
240 } else {
241 $list = array();
242 }
243
244 foreach (get_plugin_list('gradingform') as $name => $location) {
245 $list[$name] = get_string('pluginname', 'gradingform_'.$name);
246 }
247
248 return $list;
4333580e
DM
249 }
250
251 /**
252 * Returns the list of gradable areas in the given context and component
253 *
254 * This performs a callback to the library of the relevant plugin to obtain
255 * the list of supported areas.
256 * @return array of (string)areacode => (string)localized title of the area
257 */
258 public function get_available_areas() {
259 global $CFG;
260
261 $this->ensure_isset(array('context', 'component'));
262
9e4eb676
DM
263 if ($this->get_context()->contextlevel == CONTEXT_SYSTEM) {
264 if ($this->get_component() !== 'core_grading') {
265 throw new coding_exception('Unsupported component at the system context');
266 } else {
267 return array();
268 }
269
270 } else if ($this->get_context()->contextlevel >= CONTEXT_COURSE) {
271 list($context, $course, $cm) = get_context_info_array($this->get_context()->id);
272
273 if (empty($cm->modname)) {
274 throw new coding_exception('Unsupported area location');
275 } else {
276 return plugin_callback('mod', $cm->modname, 'grading', 'areas_list', null, array());
277 }
278
279 } else {
280 throw new coding_exception('Unsupported gradable area context level');
281 }
4333580e
DM
282 }
283
284 /**
8a4acb3a 285 * Returns the currently active grading method in the gradable area
4333580e 286 *
64402867 287 * @return string|null the name of the grading plugin of null if it has not been set
4333580e 288 */
8a4acb3a 289 public function get_active_method() {
64402867
DM
290 global $DB;
291
292 $this->ensure_isset(array('context', 'component', 'area'));
293
294 // get the current grading area record if it exists
9b8550f8
DM
295 if (is_null($this->areacache)) {
296 $this->areacache = $DB->get_record('grading_areas', array(
297 'contextid' => $this->context->id,
298 'component' => $this->component,
299 'areaname' => $this->area),
300 '*', IGNORE_MISSING);
301 }
64402867 302
9b8550f8 303 if ($this->areacache === false) {
64402867
DM
304 // no area record yet
305 return null;
306 }
307
9b8550f8 308 return $this->areacache->activemethod;
64402867
DM
309 }
310
311 /**
312 * Sets the currently active grading method in the gradable area
313 *
314 * @param string $method the method name, eg 'rubric' (must be available)
3bd217aa 315 * @return bool true if the method changed or was just set, false otherwise
64402867
DM
316 */
317 public function set_active_method($method) {
318 global $DB;
319
8a4acb3a 320 $this->ensure_isset(array('context', 'component', 'area'));
64402867 321
8cd65f16
DM
322 // make sure the passed method is empty or a valid plugin name
323 if (empty($method)) {
324 $method = null;
325 } else {
326 if ('gradingform_'.$method !== clean_param('gradingform_'.$method, PARAM_COMPONENT)) {
327 throw new moodle_exception('invalid_method_name', 'core_grading');
328 }
329 $available = $this->get_available_methods(false);
330 if (!array_key_exists($method, $available)) {
331 throw new moodle_exception('invalid_method_name', 'core_grading');
332 }
64402867
DM
333 }
334
335 // get the current grading area record if it exists
9b8550f8
DM
336 if (is_null($this->areacache)) {
337 $this->areacache = $DB->get_record('grading_areas', array(
338 'contextid' => $this->context->id,
339 'component' => $this->component,
340 'areaname' => $this->area),
341 '*', IGNORE_MISSING);
342 }
64402867 343
3bd217aa
DM
344 $methodchanged = false;
345
9b8550f8 346 if ($this->areacache === false) {
64402867
DM
347 // no area record yet, create one with the active method set
348 $area = array(
349 'contextid' => $this->context->id,
350 'component' => $this->component,
351 'areaname' => $this->area,
352 'activemethod' => $method);
353 $DB->insert_record('grading_areas', $area);
3bd217aa 354 $methodchanged = true;
64402867
DM
355
356 } else {
357 // update the existing record if needed
8cd65f16 358 if ($this->areacache->activemethod !== $method) {
9b8550f8 359 $DB->set_field('grading_areas', 'activemethod', $method, array('id' => $this->areacache->id));
3bd217aa 360 $methodchanged = true;
64402867
DM
361 }
362 }
9b8550f8
DM
363
364 $this->areacache = null;
3bd217aa
DM
365
366 return $methodchanged;
9b8550f8
DM
367 }
368
369 /**
370 * Extends the settings navigation with the grading settings
371 *
372 * This function is called when the context for the page is an activity module with the
373 * FEATURE_ADVANCED_GRADING and the user has the permission moodle/grade:managegradingforms.
374 *
375 * @param settings_navigation $settingsnav {@link settings_navigation}
376 * @param navigation_node $modulenode {@link navigation_node}
377 */
378 public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $modulenode=null) {
9b8550f8
DM
379
380 $this->ensure_isset(array('context', 'component'));
381
382 $areas = $this->get_available_areas();
383
384 if (empty($areas)) {
385 // no money, no funny
386 return;
8168299d 387
98a73bb7
DM
388 } else if (count($areas) == 1) {
389 // make just a single node for the management screen
390 $areatitle = reset($areas);
391 $areaname = key($areas);
9b8550f8
DM
392 $this->set_area($areaname);
393 $method = $this->get_active_method();
98a73bb7
DM
394 $managementnode = $modulenode->add(get_string('gradingmanagement', 'core_grading'),
395 $this->get_management_url(), settings_navigation::TYPE_CUSTOM);
396 if ($method) {
397 $controller = $this->get_controller($method);
398 $controller->extend_settings_navigation($settingsnav, $managementnode);
9b8550f8
DM
399 }
400
98a73bb7
DM
401 } else {
402 // make management screen node for each area
403 $managementnode = $modulenode->add(get_string('gradingmanagement', 'core_grading'),
404 null, settings_navigation::TYPE_CUSTOM);
405 foreach ($areas as $areaname => $areatitle) {
406 $this->set_area($areaname);
407 $method = $this->get_active_method();
408 $node = $managementnode->add($areatitle,
409 $this->get_management_url(), settings_navigation::TYPE_CUSTOM);
410 if ($method) {
411 $controller = $this->get_controller($method);
412 $controller->extend_settings_navigation($settingsnav, $node);
413 }
9b8550f8 414 }
9b8550f8
DM
415 }
416 }
417
418 /**
419 * Returns the given method's controller in the gradable area
420 *
421 * @param string $method the method name, eg 'rubric' (must be available)
422 * @return grading_controller
423 */
424 public function get_controller($method) {
425 global $CFG;
426
427 $this->ensure_isset(array('context', 'component', 'area'));
428
429 // make sure the passed method is a valid plugin name
430 if ('gradingform_'.$method !== clean_param('gradingform_'.$method, PARAM_COMPONENT)) {
431 throw new moodle_exception('invalid_method_name', 'core_grading');
432 }
433 $available = $this->get_available_methods(false);
434 if (!array_key_exists($method, $available)) {
435 throw new moodle_exception('invalid_method_name', 'core_grading');
436 }
437
438 // get the current grading area record if it exists
439 if (is_null($this->areacache)) {
440 $this->areacache = $DB->get_record('grading_areas', array(
441 'contextid' => $this->context->id,
442 'component' => $this->component,
443 'areaname' => $this->area),
444 '*', IGNORE_MISSING);
445 }
446
447 if ($this->areacache === false) {
448 // no area record yet, create one
449 $area = array(
450 'contextid' => $this->context->id,
451 'component' => $this->component,
452 'areaname' => $this->area);
453 $areaid = $DB->insert_record('grading_areas', $area);
454 // reload the cache
455 $this->areacache = $DB->get_record('grading_areas', array('id' => $areaid), '*', MUST_EXIST);
456 }
457
458 require_once($CFG->dirroot.'/grade/grading/form/'.$method.'/lib.php');
3e43eff5 459 $classname = 'gradingform_'.$method.'_controller';
9b8550f8
DM
460
461 return new $classname($this->context, $this->component, $this->area, $this->areacache->id);
4333580e
DM
462 }
463
18e6298c
MG
464 /**
465 * Returns the controller for the active method if it is available
03d448e5
DM
466 *
467 * @return null|grading_controller
18e6298c
MG
468 */
469 public function get_active_controller() {
470 if ($gradingmethod = $this->get_active_method()) {
471 $controller = $this->get_controller($gradingmethod);
472 if ($controller->is_form_available()) {
473 return $controller;
474 }
475 }
476 return null;
477 }
478
03d448e5
DM
479 /**
480 * Returns the URL of the grading area management page
481 *
482 * @param moodle_url $returnurl optional URL of the page where the user should be sent back to
483 * @return moodle_url
484 */
98a73bb7 485 public function get_management_url(moodle_url $returnurl = null) {
03d448e5
DM
486
487 $this->ensure_isset(array('context', 'component'));
488
489 if ($this->areacache) {
490 $params = array('areaid' => $this->areacache->id);
491 } else {
492 $params = array('contextid' => $this->context->id, 'component' => $this->component);
98a73bb7
DM
493 if ($this->area) {
494 $params['area'] = $this->area;
6832a102 495 }
03d448e5
DM
496 }
497
498 if (!is_null($returnurl)) {
499 $params['returnurl'] = $returnurl->out(false);
500 }
501
6832a102 502 return new moodle_url('/grade/grading/manage.php', $params);
03d448e5
DM
503 }
504
fde33804
DM
505 /**
506 * Creates a new shared area to hold a grading form template
507 *
508 * Shared area are implemented as virtual gradable areas at the system level context
509 * with the component set to core_grading and unique random area name.
510 *
511 * @param string $method the name of the plugin we create the area for
512 * @return int the new area id
513 */
514 public function create_shared_area($method) {
515 global $DB;
516
517 // generate some unique random name for the new area
518 $name = $method . '_' . sha1(rand().uniqid($method, true));
519 // create new area record
520 $area = array(
dd736a87 521 'contextid' => context_system::instance()->id,
fde33804
DM
522 'component' => 'core_grading',
523 'areaname' => $name,
524 'activemethod' => $method);
525 return $DB->insert_record('grading_areas', $area);
526 }
527
20836db9
DM
528 /**
529 * Helper method to tokenize the given string
530 *
531 * Splits the given string into smaller strings. This is a helper method for
532 * full text searching in grading forms. If the given string is surrounded with
533 * double quotes, the resulting array consists of a single item containing the
534 * quoted content.
535 *
536 * Otherwise, string like 'grammar, english language' would be tokenized into
537 * the three tokens 'grammar', 'english', 'language'.
538 *
539 * One-letter tokens like are dropped in non-phrase mode. Repeated tokens are
540 * returned just once.
541 *
542 * @param string $needle
543 * @return array
544 */
545 public static function tokenize($needle) {
546
547 // check if we are searching for the exact phrase
548 if (preg_match('/^[\s]*"[\s]*(.*?)[\s]*"[\s]*$/', $needle, $matches)) {
549 $token = $matches[1];
550 if ($token === '') {
551 return array();
552 } else {
553 return array($token);
554 }
555 }
556
557 // split the needle into smaller parts separated by non-word characters
558 $tokens = preg_split("/\W/u", $needle);
559 // keep just non-empty parts
560 $tokens = array_filter($tokens);
561 // distinct
562 $tokens = array_unique($tokens);
563 // drop one-letter tokens
564 foreach ($tokens as $ix => $token) {
565 if (strlen($token) == 1) {
566 unset($tokens[$ix]);
567 }
568 }
569
570 return array_values($tokens);
571 }
572
9b8550f8 573 ////////////////////////////////////////////////////////////////////////////
64402867 574
4333580e
DM
575 /**
576 * Make sure that the given properties were set to some not-null value
577 *
578 * @param array $properties the list of properties
579 * @throws coding_exception
580 */
581 private function ensure_isset(array $properties) {
582 foreach ($properties as $property) {
583 if (!isset($this->$property)) {
9b8550f8 584 throw new coding_exception('The property "'.$property.'" is not set.');
4333580e
DM
585 }
586 }
587 }
588}