MDL-29479 List of available grading methods returned properly
[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.
6c9e506c
DM
111 *
112 * @param bool $includenone should the 'Simple direct grading' be included
4333580e
DM
113 * @return array of the (string)name => (string)localized title of the method
114 */
6c9e506c 115 public function get_available_methods($includenone = true) {
4333580e
DM
116
117 $this->ensure_isset(array('context'));
118
6c9e506c
DM
119 if ($includenone) {
120 $list = array('' => get_string('gradingmethodnone', 'core_grading'));
121 } else {
122 $list = array();
123 }
124
125 foreach (get_plugin_list('gradingform') as $name => $location) {
126 $list[$name] = get_string('pluginname', 'gradingform_'.$name);
127 }
128
129 return $list;
4333580e
DM
130 }
131
132 /**
133 * Returns the list of gradable areas in the given context and component
134 *
135 * This performs a callback to the library of the relevant plugin to obtain
136 * the list of supported areas.
137 * @return array of (string)areacode => (string)localized title of the area
138 */
139 public function get_available_areas() {
140 global $CFG;
141
142 $this->ensure_isset(array('context', 'component'));
143
144 // example: if the given context+component lead to mod_assignment, this method
145 // will do something like
146 // require_once($CFG->dirroot.'/mod/assignment/lib.php');
147 // return assignment_gradable_area_list();
148
149 // todo - hardcoded list for now
150 return array('submission' => get_string('assignmentsubmission', 'assignment'));
151 }
152
153 /**
154 * Returns the currently active grading method in the given gradable area
155 *
156 * @return string the name of the grading plugin
157 */
158 public function get_active_area_method() {
159 $this->ensure_isset(array('context', 'component', 'areaname'));
160 // todo - hardcoded value for now
161 return 'rubric';
162 }
163
164 /**
165 * Make sure that the given properties were set to some not-null value
166 *
167 * @param array $properties the list of properties
168 * @throws coding_exception
169 */
170 private function ensure_isset(array $properties) {
171 foreach ($properties as $property) {
172 if (!isset($this->$property)) {
173 throw new coding_exception('The property '.$property.' is not set.');
174 }
175 }
176 }
177}