MDL-29480 Introducing new rubric plugin
[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 *
32 * @param stdClass $context
33 * @param string $component the frankenstyle name of the component
34 * @param string $areaname the name of the gradable area
35 * @return grading_manager
36 */
37function get_grading_manager($context = null, $component = null, $areaname = null) {
38
39 $manager = new grading_manager();
40
41 if (!is_null($context)) {
42 $manager->set_context($context);
43 }
44
45 if (!is_null($component)) {
46 $manager->set_component($component);
47 }
48
49 if (!is_null($areaname)) {
50 $manager->set_areaname($areaname);
51 }
52
53 return $manager;
54}
55
56/**
57 * General class providing access to common grading features
58 *
59 * Fully initialized instance of the grading manager operates over a single
60 * gradable area. It is possible to work with a partially initialized manager
61 * that knows just context and component without known areaname, for example.
62 * It is also possible to change context, component and areaname of an existing
63 * manager. Such pattern is used when copying form definitions, for example.
64 */
65class grading_manager {
66
67 /** @var stdClass the context */
68 protected $context;
69
70 /** @var string the frankenstyle name of the component */
71 protected $component;
72
73 /** @var string the name of the gradable area */
74 protected $areaname;
75
76 /**
77 * Sets the context the manager operates on
78 *
79 * @param stdClass $context
80 */
81 public function set_context(stdClass $context) {
82 $this->context = $context;
83 }
84
85 /**
86 * Sets the component the manager operates on
87 *
88 * @param string $component the frankenstyle name of the component
89 */
90 public function set_component($component) {
91 $this->component = $component;
92 }
93
94 /**
95 * Sets the areaname the manager operates on
96 *
97 * @param string $areaname the name of the gradable area
98 */
99 public function set_areaname($areaname) {
100 $this->areaname = $areaname;
101 }
102
103 /**
104 * Returns the list of available grading methods in the given context
105 *
106 * Basically this returns the list of installed grading plugins with an empty value
107 * for simple direct grading. In the future, the list of available methods may be
108 * controlled per-context.
109 *
110 * Requires the context property to be set in advance.
111 * @return array of the (string)name => (string)localized title of the method
112 */
113 public function get_available_methods() {
114
115 $this->ensure_isset(array('context'));
116
117 // todo - hardcoded list for now, should read the list of installed grading plugins
118 return array(
119 '' => get_string('gradingmethodnone', 'core_grading'),
120 'rubric' => 'Rubric',
121 );
122 }
123
124 /**
125 * Returns the list of gradable areas in the given context and component
126 *
127 * This performs a callback to the library of the relevant plugin to obtain
128 * the list of supported areas.
129 * @return array of (string)areacode => (string)localized title of the area
130 */
131 public function get_available_areas() {
132 global $CFG;
133
134 $this->ensure_isset(array('context', 'component'));
135
136 // example: if the given context+component lead to mod_assignment, this method
137 // will do something like
138 // require_once($CFG->dirroot.'/mod/assignment/lib.php');
139 // return assignment_gradable_area_list();
140
141 // todo - hardcoded list for now
142 return array('submission' => get_string('assignmentsubmission', 'assignment'));
143 }
144
145 /**
146 * Returns the currently active grading method in the given gradable area
147 *
148 * @return string the name of the grading plugin
149 */
150 public function get_active_area_method() {
151 $this->ensure_isset(array('context', 'component', 'areaname'));
152 // todo - hardcoded value for now
153 return 'rubric';
154 }
155
156 /**
157 * Make sure that the given properties were set to some not-null value
158 *
159 * @param array $properties the list of properties
160 * @throws coding_exception
161 */
162 private function ensure_isset(array $properties) {
163 foreach ($properties as $property) {
164 if (!isset($this->$property)) {
165 throw new coding_exception('The property '.$property.' is not set.');
166 }
167 }
168 }
169}