Saving a form as a public template
[moodle.git] / grade / grading / form / lib.php
CommitLineData
9b8550f8
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/**
21d37aa6
DM
19 * Common classes used by gradingform plugintypes are defined here
20 *
9b8550f8
DM
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/**
21d37aa6 30 * Grading method controller represents a plugin used in a particular area
9b8550f8 31 */
3e43eff5 32abstract class gradingform_controller {
9b8550f8 33
21d37aa6
DM
34 const DEFINITION_STATUS_WORKINPROGRESS = 0;
35 const DEFINITION_STATUS_PRIVATE = 1;
36 const DEFINITION_STATUS_PUBLIC = 2;
37
9b8550f8
DM
38 /** @var stdClass the context */
39 protected $context;
40
41 /** @var string the frankenstyle name of the component */
42 protected $component;
43
44 /** @var string the name of the gradable area */
45 protected $area;
46
47 /** @var int the id of the gradable area record */
48 protected $areaid;
49
fe817d87 50 /** @var stdClass|false the definition structure */
3e43eff5
DM
51 protected $definition;
52
9b8550f8
DM
53 /**
54 * Do not instantinate this directly, use {@link grading_manager::get_controller()}
55 *
fe817d87
DM
56 * @param stdClass $context the context of the form
57 * @param string $component the frankenstyle name of the component
58 * @param string $area the name of the gradable area
59 * @param int $areaid the id of the gradable area record
9b8550f8
DM
60 */
61 public function __construct(stdClass $context, $component, $area, $areaid) {
3e43eff5
DM
62 global $DB;
63
64 $this->context = $context;
fe817d87
DM
65 list($type, $name) = normalize_component($component);
66 $this->component = $type.'_'.$name;
3e43eff5
DM
67 $this->area = $area;
68 $this->areaid = $areaid;
69
70 $this->load_definition();
71 }
72
fe817d87
DM
73 /**
74 * @return stdClass controller context
75 */
76 public function get_context() {
77 return $this->context;
78 }
79
80 /**
81 * @return string gradable component name
82 */
83 public function get_component() {
84 return $this->component;
85 }
86
87 /**
88 * @return string gradable area name
89 */
90 public function get_area() {
91 return $this->area;
92 }
93
94 /**
95 * @return int gradable area id
96 */
97 public function get_areaid() {
98 return $this->areaid;
99 }
100
6832a102
DM
101 /**
102 * Is the form definition record available?
103 *
104 * Note that this actually checks whether the process of defining the form ever started
105 * and not whether the form definition should be considered as final.
106 *
107 * @return boolean
108 */
109 public function is_form_defined() {
110 return !empty($this->definition);
111 }
112
3e43eff5 113 /**
21d37aa6 114 * Is the grading form defined and released for usage by the given user?
3e43eff5 115 *
21d37aa6 116 * @param int $foruserid the id of the user who attempts to work with the form
3e43eff5
DM
117 * @return boolean
118 */
21d37aa6
DM
119 public function is_form_available($foruserid = null) {
120 global $USER;
121
122 if (is_null($foruserid)) {
123 $foruserid = $USER->id;
124 }
125
6832a102 126 if (!$this->is_form_defined()) {
21d37aa6
DM
127 return false;
128 }
129
130 if ($this->definition->status == self::DEFINITION_STATUS_PUBLIC) {
131 return true;
132 }
133
134 if ($this->definition->status == self::DEFINITION_STATUS_PRIVATE) {
135 if ($this->definition->usercreated == $foruserid) {
136 return true;
137 }
138 }
139
140 return false;
3e43eff5
DM
141 }
142
6832a102
DM
143 /**
144 * Returns URL of a page where the grading form can be defined and edited.
145 *
146 * @param moodle_url $returnurl optional URL of a page where the user should be sent once they are finished with editing
147 * @return moodle_url
148 */
149 public function get_editor_url(moodle_url $returnurl = null) {
150
151 $params = array('areaid' => $this->areaid);
152
153 if (!is_null($returnurl)) {
154 $params['returnurl'] = $returnurl->out(false);
155 }
156
157 return new moodle_url('/grade/grading/form/'.$this->get_method_name().'/edit.php', $params);
158 }
159
9b8550f8
DM
160 /**
161 * Extends the module settings navigation
162 *
163 * This function is called when the context for the page is an activity module with the
164 * FEATURE_ADVANCED_GRADING, the user has the permission moodle/grade:managegradingforms
165 * and there is an area with the active grading method set to the given plugin.
166 *
167 * @param settings_navigation $settingsnav {@link settings_navigation}
168 * @param navigation_node $node {@link navigation_node}
169 */
170 public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $node=null) {
171 // do not extend by default
172 }
3e43eff5
DM
173
174 /**
fe817d87 175 * Returns the grading form definition structure
3e43eff5 176 *
fe817d87 177 * @return stdClass|false definition data or false if the form is not defined yet
3e43eff5 178 */
fe817d87
DM
179 public function get_definition() {
180 if (is_null($this->definition)) {
181 $this->load_definition();
182 }
183 return $this->definition;
3e43eff5
DM
184 }
185
fde33804
DM
186 /**
187 * Returns the form definition suitable for cloning into another area
188 *
189 * @param gradingform_controller $target the controller of the new copy
190 * @return stdClass definition structure to pass to the target's {@link update_definition()}
191 */
192 public function get_definition_copy(gradingform_controller $target) {
193
194 if (get_class($this) != get_class($target)) {
195 throw new coding_exception('The source and copy controller mismatch');
196 }
197
198 if ($target->is_form_defined()) {
199 throw new coding_exception('The target controller already contains a form definition');
200 }
201
202 $old = $this->get_definition();
203 // keep our id
204 $new = new stdClass();
205 $new->copiedfromid = $old->id;
206 $new->name = $old->name;
207 // once we support files embedded into the description, we will want to
208 // relink them into the new file area here (that is why we accept $target)
209 $new->description = $old->description;
210 $new->descriptionformat = $old->descriptionformat;
211 $new->options = $old->options;
212
213 return $new;
214 }
215
3e43eff5 216 /**
fe817d87 217 * Saves the defintion data into the database
21d37aa6 218 *
fe817d87
DM
219 * The implementation in this base class stores the common data into the record
220 * into the {grading_definition} table. The plugins are likely to extend this
221 * and save their data into own tables, too.
21d37aa6 222 *
fe817d87
DM
223 * @param stdClass $definition data containing values for the {grading_definition} table
224 * @param int|null $usermodified optional userid of the author of the definition, defaults to the current user
21d37aa6
DM
225 */
226 public function update_definition(stdClass $definition, $usermodified = null) {
227 global $DB, $USER;
228
229 if (is_null($usermodified)) {
230 $usermodified = $USER->id;
231 }
232
fe817d87
DM
233 if (!empty($this->definition->id)) {
234 // prepare a record to be updated
235 $record = new stdClass();
236 // populate it with scalar values from the passed definition structure
237 foreach ($definition as $prop => $val) {
238 if (is_array($val) or is_object($val)) {
239 // probably plugin's data
240 continue;
241 }
242 $record->{$prop} = $val;
243 }
244 // make sure we do not override some crucial values by accident
245 if (!empty($record->id) and $record->id != $this->definition->id) {
246 throw new coding_exception('Attempting to update other definition record.');
247 }
248 $record->id = $this->definition->id;
249 unset($record->areaid);
250 unset($record->method);
251 unset($record->timecreated);
252 // set the modification flags
253 $record->timemodified = time();
254 $record->usermodified = $usermodified;
21d37aa6 255
fe817d87 256 $DB->update_record('grading_definitions', $record);
21d37aa6
DM
257
258 } else if ($this->definition === false) {
fe817d87
DM
259 // prepare a record to be inserted
260 $record = new stdClass();
261 // populate it with scalar values from the passed definition structure
262 foreach ($definition as $prop => $val) {
263 if (is_array($val) or is_object($val)) {
264 // probably plugin's data
265 continue;
266 }
267 $record->{$prop} = $val;
268 }
269 // make sure we do not override some crucial values by accident
270 if (!empty($record->id)) {
271 throw new coding_exception('Attempting to create a new record while there is already one existing.');
272 }
273 unset($record->id);
274 $record->areaid = $this->areaid;
275 $record->method = $this->get_method_name();
276 $record->timecreated = time();
277 $record->usercreated = $usermodified;
ac16cedf
DM
278 $record->timemodified = $record->timecreated;
279 $record->usermodified = $record->usercreated;
fe817d87 280 $record->status = self::DEFINITION_STATUS_WORKINPROGRESS;
21d37aa6 281
ac16cedf 282 $DB->insert_record('grading_definitions', $record);
21d37aa6
DM
283
284 } else {
fe817d87 285 throw new coding_exception('Unknown status of the cached definition record.');
21d37aa6
DM
286 }
287 }
288
289 /**
290 * Makes sure there is a form instance for the given rater grading the given item
291 *
292 * Plugins will probably override/extend this and load additional data of how their
293 * forms are filled in one complex query.
294 *
295 * @todo this might actually become abstract method
296 * @param int $raterid
297 * @param int $itemid
298 * @return stdClass newly created or existing record from {grading_instances}
299 */
300 public function prepare_instance($raterid, $itemid) {
301 global $DB;
302
303 if (empty($this->definition)) {
304 throw new coding_exception('Attempting to prepare an instance of non-existing grading form');
305 }
306
307 $current = $DB->get_record('grading_instances', array(
308 'formid' => $this->definition->id,
309 'raterid' => $raterid,
310 'itemid' => $itemid), '*', IGNORE_MISSING);
311
312 if (empty($current)) {
313 $instance = new stdClass();
314 $instance->formid = $this->definition->id;
315 $instance->raterid = $raterid;
316 $instance->itemid = $itemid;
317 $instance->timemodified = time();
3bf7ef29 318 $instance->feedbackformat = FORMAT_MOODLE;
21d37aa6
DM
319 $instance->id = $DB->insert_record('grading_instances', $instance);
320 return $instance;
321
322 } else {
323 return $current;
324 }
325 }
326
18e6298c
MG
327 /**
328 * Saves non-js data and returns the gradebook grade
329 */
3bf7ef29 330 abstract public function save_and_get_grade($raterid, $itemid, $formdata);
18e6298c
MG
331
332 /**
333 * Returns html for form element
334 */
6798c63e
MG
335 abstract public function to_html($gradingformelement);
336
337 /**
338 *
339 */
340 public function default_validation_error_message() {
341 return '';
342 }
343
344 /**
345 *
346 */
347 public function validate_grading_element($elementvalue, $itemid) {
348 return true;
349 }
18e6298c 350
ab156741 351 /**
6832a102 352 * Returns the HTML code displaying the preview of the grading form
ab156741 353 *
6832a102
DM
354 * Plugins are supposed to override/extend this. Ideally they should delegate
355 * the task to their own renderer.
356 *
357 * @param moodle_page $page the target page
358 * @return string
ab156741 359 */
6832a102
DM
360 public function render_preview(moodle_page $page) {
361
362 if (!$this->is_form_defined()) {
363 throw new coding_exception('It is the caller\'s responsibility to make sure that the form is actually defined');
364 }
365
366 $output = $page->get_renderer('core_grading');
367
368 return $output->preview_definition_header($this);
ab156741
MG
369 }
370
fe817d87
DM
371 ////////////////////////////////////////////////////////////////////////////
372
373
21d37aa6
DM
374 /**
375 * Loads the form definition if it exists
3e43eff5 376 *
21d37aa6 377 * The default implementation just tries to load the record from the {grading_definitions}
3e43eff5
DM
378 * table. The plugins are likely to override this with a more complex query that loads
379 * all required data at once.
380 */
381 protected function load_definition() {
382 global $DB;
3e43eff5
DM
383 $this->definition = $DB->get_record('grading_definitions', array(
384 'areaid' => $this->areaid,
385 'method' => $this->get_method_name()), '*', IGNORE_MISSING);
386 }
21d37aa6
DM
387
388 /**
fe817d87
DM
389 * @return string the name of the grading method plugin, eg 'rubric'
390 * @see PARAM_PLUGIN
21d37aa6 391 */
fe817d87
DM
392 protected function get_method_name() {
393 if (preg_match('/^gradingform_([a-z][a-z0-9_]*[a-z0-9])_controller$/', get_class($this), $matches)) {
394 return $matches[1];
395 } else {
396 throw new coding_exception('Invalid class name');
397 }
21d37aa6
DM
398 }
399}