Merge pull request #20 from marinaglancy/wip-nov02-rubric
[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
7622ae95
DM
34 /** undefined definition status */
35 const DEFINITION_STATUS_NULL = 0;
36 /** the form is currently being edited and is not ready for usage yet */
37 const DEFINITION_STATUS_DRAFT = 10;
38 /** the for was marked as ready for actual usage */
39 const DEFINITION_STATUS_READY = 20;
21d37aa6 40
9b8550f8
DM
41 /** @var stdClass the context */
42 protected $context;
43
44 /** @var string the frankenstyle name of the component */
45 protected $component;
46
47 /** @var string the name of the gradable area */
48 protected $area;
49
50 /** @var int the id of the gradable area record */
51 protected $areaid;
52
fe817d87 53 /** @var stdClass|false the definition structure */
5060997b 54 protected $definition = false;
3e43eff5 55
9e2eca0f
MG
56 /** @var array graderange array of valid grades for this area. Use set_grade_range and get_grade_range to access this */
57 private $graderange = null;
58
9b8550f8
DM
59 /**
60 * Do not instantinate this directly, use {@link grading_manager::get_controller()}
61 *
fe817d87
DM
62 * @param stdClass $context the context of the form
63 * @param string $component the frankenstyle name of the component
64 * @param string $area the name of the gradable area
65 * @param int $areaid the id of the gradable area record
9b8550f8
DM
66 */
67 public function __construct(stdClass $context, $component, $area, $areaid) {
3e43eff5
DM
68 global $DB;
69
70 $this->context = $context;
fe817d87
DM
71 list($type, $name) = normalize_component($component);
72 $this->component = $type.'_'.$name;
3e43eff5
DM
73 $this->area = $area;
74 $this->areaid = $areaid;
75
76 $this->load_definition();
77 }
78
fe817d87
DM
79 /**
80 * @return stdClass controller context
81 */
82 public function get_context() {
83 return $this->context;
84 }
85
86 /**
87 * @return string gradable component name
88 */
89 public function get_component() {
90 return $this->component;
91 }
92
93 /**
94 * @return string gradable area name
95 */
96 public function get_area() {
97 return $this->area;
98 }
99
100 /**
101 * @return int gradable area id
102 */
103 public function get_areaid() {
104 return $this->areaid;
105 }
106
6832a102
DM
107 /**
108 * Is the form definition record available?
109 *
110 * Note that this actually checks whether the process of defining the form ever started
111 * and not whether the form definition should be considered as final.
112 *
113 * @return boolean
114 */
115 public function is_form_defined() {
5060997b 116 return ($this->definition !== false);
6832a102
DM
117 }
118
3e43eff5 119 /**
7622ae95 120 * Is the grading form defined and ready for usage?
3e43eff5
DM
121 *
122 * @return boolean
123 */
7622ae95 124 public function is_form_available() {
3599b113 125 return ($this->is_form_defined() && $this->definition->status == self::DEFINITION_STATUS_READY);
3e43eff5
DM
126 }
127
2ae7faf1
MG
128 /**
129 * Returns a message why this form is unavailable. Maybe overriden by plugins to give more details.
130 * @see is_form_available()
131 *
132 * @return string
133 */
134 public function form_unavailable_notification() {
135 if ($this->is_form_available()) {
136 return null;
137 }
0e2a1d75 138 return get_string('gradingformunavailable', 'grading');
2ae7faf1
MG
139 }
140
6832a102
DM
141 /**
142 * Returns URL of a page where the grading form can be defined and edited.
143 *
144 * @param moodle_url $returnurl optional URL of a page where the user should be sent once they are finished with editing
145 * @return moodle_url
146 */
147 public function get_editor_url(moodle_url $returnurl = null) {
148
149 $params = array('areaid' => $this->areaid);
150
151 if (!is_null($returnurl)) {
152 $params['returnurl'] = $returnurl->out(false);
153 }
154
155 return new moodle_url('/grade/grading/form/'.$this->get_method_name().'/edit.php', $params);
156 }
157
9b8550f8
DM
158 /**
159 * Extends the module settings navigation
160 *
161 * This function is called when the context for the page is an activity module with the
162 * FEATURE_ADVANCED_GRADING, the user has the permission moodle/grade:managegradingforms
163 * and there is an area with the active grading method set to the given plugin.
164 *
165 * @param settings_navigation $settingsnav {@link settings_navigation}
166 * @param navigation_node $node {@link navigation_node}
167 */
168 public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $node=null) {
169 // do not extend by default
170 }
3e43eff5
DM
171
172 /**
fe817d87 173 * Returns the grading form definition structure
3e43eff5 174 *
9e2eca0f 175 * @param boolean $force whether to force loading from DB even if it was already loaded
fe817d87 176 * @return stdClass|false definition data or false if the form is not defined yet
3e43eff5 177 */
9e2eca0f 178 public function get_definition($force = false) {
5060997b 179 if ($this->definition === false || $force) {
fe817d87
DM
180 $this->load_definition();
181 }
182 return $this->definition;
3e43eff5
DM
183 }
184
fde33804
DM
185 /**
186 * Returns the form definition suitable for cloning into another area
187 *
188 * @param gradingform_controller $target the controller of the new copy
189 * @return stdClass definition structure to pass to the target's {@link update_definition()}
190 */
191 public function get_definition_copy(gradingform_controller $target) {
192
193 if (get_class($this) != get_class($target)) {
194 throw new coding_exception('The source and copy controller mismatch');
195 }
196
197 if ($target->is_form_defined()) {
198 throw new coding_exception('The target controller already contains a form definition');
199 }
200
201 $old = $this->get_definition();
202 // keep our id
203 $new = new stdClass();
204 $new->copiedfromid = $old->id;
205 $new->name = $old->name;
206 // once we support files embedded into the description, we will want to
207 // relink them into the new file area here (that is why we accept $target)
208 $new->description = $old->description;
209 $new->descriptionformat = $old->descriptionformat;
210 $new->options = $old->options;
3599b113 211 $new->status = $old->status;
fde33804
DM
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 225 */
3599b113 226 public function update_definition(stdClass $definition, $usermodified = null) {
21d37aa6
DM
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;
3599b113 280 if (empty($record->status)) {
7622ae95
DM
281 $record->status = self::DEFINITION_STATUS_DRAFT;
282 }
3599b113
MG
283 if (empty($record->descriptionformat)) {
284 $record->descriptionformat = FORMAT_MOODLE; // field can not be empty
285 }
21d37aa6 286
ac16cedf 287 $DB->insert_record('grading_definitions', $record);
21d37aa6
DM
288
289 } else {
fe817d87 290 throw new coding_exception('Unknown status of the cached definition record.');
21d37aa6
DM
291 }
292 }
293
3599b113
MG
294 /**
295 * Formats the definition description for display on page
296 *
297 * @return string
298 */
299 public function get_formatted_description() {
300 if (!isset($this->definition->description)) {
301 return '';
302 }
303 return format_text($this->definition->description, $this->definition->descriptionformat);
304 }
305
21d37aa6 306 /**
36937f02
MG
307 * Returns the ACTIVE instance for this definition for the specified $raterid and $itemid
308 * (if multiple raters are allowed, or only for $itemid otherwise).
21d37aa6 309 *
21d37aa6
DM
310 * @param int $raterid
311 * @param int $itemid
36937f02
MG
312 * @param boolean $idonly
313 * @return mixed if $idonly=true returns id of the found instance, otherwise returns the instance object
21d37aa6 314 */
36937f02 315 public function get_current_instance($raterid, $itemid, $idonly = false) {
21d37aa6 316 global $DB;
36937f02
MG
317 $select = array(
318 'formid' => $this->definition->id,
319 'itemid' => $itemid,
320 'status' => gradingform_instance::INSTANCE_STATUS_ACTIVE);
321 if (false /* TODO $manager->allow_multiple_raters() */) {
322 $select['raterid'] = $raterid;
21d37aa6 323 }
36937f02
MG
324 if ($idonly) {
325 if ($current = $DB->get_record('grading_instances', $select, 'id', IGNORE_MISSING)) {
326 return $current->id;
327 }
21d37aa6 328 } else {
36937f02
MG
329 if ($current = $DB->get_record('grading_instances', $select, '*', IGNORE_MISSING)) {
330 return $this->get_instance($current);
331 }
21d37aa6 332 }
36937f02 333 return null;
21d37aa6
DM
334 }
335
18e6298c 336 /**
36937f02
MG
337 * Returns list of active instances for the specified $itemid
338 *
339 * @param int $itemid
340 * @return array of gradingform_instance objects
18e6298c 341 */
36937f02
MG
342 public function get_current_instances($itemid) {
343 global $DB;
344 $conditions = array('formid' => $this->definition->id,
345 'itemid' => $itemid,
346 'status' => gradingform_instance::INSTANCE_STATUS_ACTIVE);
347 $records = $DB->get_recordset('grading_instances', $conditions);
348 $rv = array();
349 foreach ($records as $record) {
350 $rv[] = $this->get_instance($record);
351 }
352 return $rv;
353 }
6798c63e
MG
354
355 /**
36937f02 356 * Returns the object of type gradingform_XXX_instance (where XXX is the plugin method name)
6798c63e 357 *
36937f02
MG
358 * @param mixed $instance id or row from grading_isntances table
359 * @return gradingform_instance
6798c63e 360 */
36937f02
MG
361 protected function get_instance($instance) {
362 global $DB;
363 if (is_scalar($instance)) {
364 // instance id is passed as parameter
365 $instance = $DB->get_record('grading_instances', array('id' => $instance), '*', MUST_EXIST);
366 }
367 if ($instance) {
368 $class = 'gradingform_'. $this->get_method_name(). '_instance';
369 return new $class($this, $instance);
370 }
371 return null;
6798c63e
MG
372 }
373
374 /**
36937f02
MG
375 * This function is invoked when user (teacher) starts grading.
376 * It creates and returns copy of the current ACTIVE instance if it exists. If this is the
377 * first grading attempt, a new instance is created.
378 * The status of the returned instance is INCOMPLETE
6798c63e 379 *
36937f02
MG
380 * @param int $raterid
381 * @param int $itemid
382 * @return gradingform_instance
6798c63e 383 */
fc05f222 384 public function create_instance($raterid, $itemid = null) {
36937f02
MG
385 global $DB;
386 // first find if there is already an active instance for this itemid
fc05f222 387 if ($itemid && $current = $this->get_current_instance($raterid, $itemid)) {
36937f02
MG
388 return $this->get_instance($current->copy($raterid, $itemid));
389 } else {
390 $class = 'gradingform_'. $this->get_method_name(). '_instance';
391 return $this->get_instance($class::create_new($this->definition->id, $raterid, $itemid));
392 }
6798c63e 393 }
18e6298c 394
ab156741 395 /**
6832a102 396 * Returns the HTML code displaying the preview of the grading form
ab156741 397 *
20836db9 398 * Plugins are forced to override this. Ideally they should delegate
6832a102
DM
399 * the task to their own renderer.
400 *
401 * @param moodle_page $page the target page
402 * @return string
ab156741 403 */
20836db9 404 abstract public function render_preview(moodle_page $page);
ab156741 405
671ec8f5
DM
406 /**
407 * Deletes the form definition and all the associated data
408 *
409 * @see delete_plugin_definition()
410 * @return void
411 */
412 public function delete_definition() {
413 global $DB;
414
415 if (!$this->is_form_defined()) {
416 // nothing to do
417 return;
418 }
fe817d87 419
671ec8f5
DM
420 // firstly, let the plugin delete everything from their own tables
421 $this->delete_plugin_definition();
422 // then, delete all instances left
423 $DB->delete_records('grading_instances', array('formid' => $this->definition->id));
424 // finally, delete the main definition record
425 $DB->delete_records('grading_definitions', array('id' => $this->definition->id));
426
427 $this->definition = false;
428 }
429
20836db9
DM
430 /**
431 * Prepare the part of the search query to append to the FROM statement
432 *
433 * @param string $gdid the alias of grading_definitions.id column used by the caller
434 * @return string
435 */
436 public static function sql_search_from_tables($gdid) {
437 return '';
438 }
439
440 /**
441 * Prepare the parts of the SQL WHERE statement to search for the given token
442 *
443 * The returned array cosists of the list of SQL comparions and the list of
444 * respective parameters for the comparisons. The returned chunks will be joined
445 * with other conditions using the OR operator.
446 *
447 * @param string $token token to search for
448 * @return array
449 */
450 public static function sql_search_where($token) {
451 global $DB;
452
453 $subsql = array();
454 $params = array();
455
456 return array($subsql, $params);
457 }
458
671ec8f5 459 ////////////////////////////////////////////////////////////////////////////
fe817d87 460
21d37aa6
DM
461 /**
462 * Loads the form definition if it exists
3e43eff5 463 *
21d37aa6 464 * The default implementation just tries to load the record from the {grading_definitions}
3e43eff5
DM
465 * table. The plugins are likely to override this with a more complex query that loads
466 * all required data at once.
467 */
468 protected function load_definition() {
469 global $DB;
3e43eff5
DM
470 $this->definition = $DB->get_record('grading_definitions', array(
471 'areaid' => $this->areaid,
472 'method' => $this->get_method_name()), '*', IGNORE_MISSING);
473 }
21d37aa6 474
671ec8f5
DM
475 /**
476 * Deletes all plugin data associated with the given form definiton
477 *
478 * @see delete_definition()
479 */
480 abstract protected function delete_plugin_definition();
481
21d37aa6 482 /**
fe817d87
DM
483 * @return string the name of the grading method plugin, eg 'rubric'
484 * @see PARAM_PLUGIN
21d37aa6 485 */
fe817d87
DM
486 protected function get_method_name() {
487 if (preg_match('/^gradingform_([a-z][a-z0-9_]*[a-z0-9])_controller$/', get_class($this), $matches)) {
488 return $matches[1];
489 } else {
490 throw new coding_exception('Invalid class name');
491 }
21d37aa6 492 }
36937f02
MG
493
494 /**
495 * Returns html code to be included in student's feedback.
496 *
497 * @param moodle_page $page
498 * @param int $itemid
9e2eca0f
MG
499 * @param array $grading_info result of function grade_get_grades if plugin want to use some of their info
500 * @param string $defaultcontent default string to be returned if no active grading is found or for some reason can not be shown to a user
36937f02
MG
501 * @return string
502 */
9e2eca0f 503 public function render_grade($page, $itemid, $grading_info, $defaultcontent) {
36937f02
MG
504 return $defaultcontent;
505 }
9e2eca0f
MG
506
507 /**
508 * Sets the range of grades used in this area. This is usually either range like 0-100
509 * or the scale where keys start from 1. Typical use:
510 * $controller->set_grade_range(make_grades_menu($gradingtype));
511 */
512 public final function set_grade_range(array $graderange) {
513 $this->graderange = $graderange;
514 }
515
516 /**
517 * Returns the range of grades used in this area
518 * @return array
519 */
520 public final function get_grade_range() {
521 if (empty($this->graderange)) {
522 return array();
523 }
524 return $this->graderange;
525 }
21d37aa6 526}
36937f02
MG
527
528/**
529 * Class to manage one grading instance. Stores information and performs actions like
530 * update, copy, validate, submit, etc.
531 *
532 * @copyright 2011 Marina Glancy
533 */
534abstract class gradingform_instance {
535 const INSTANCE_STATUS_ACTIVE = 1;
536 const INSTANCE_STATUS_INCOMPLETE = 0;
537 const INSTANCE_STATUS_ARCHIVE = 3;
538
539 /** @var stdClass record from table grading_instances */
540 protected $data;
541 /** @var gradingform_controller link to the corresponding controller */
542 protected $controller;
543
544 /**
545 * Creates an instance
546 *
547 * @param gradingform_controller $controller
548 * @param stdClass $data
549 */
550 public function __construct($controller, $data) {
551 $this->data = (object)$data;
552 $this->controller = $controller;
553 }
554
555 /**
556 * Creates a new empty instance in DB and mark its status as INCOMPLETE
557 *
558 * @param int $formid
559 * @param int $raterid
560 * @param int $itemid
561 * @return int id of the created instance
562 */
563 public static function create_new($formid, $raterid, $itemid) {
564 global $DB;
565 $instance = new stdClass();
566 $instance->formid = $formid;
567 $instance->raterid = $raterid;
568 $instance->itemid = $itemid;
569 $instance->status = self::INSTANCE_STATUS_INCOMPLETE;
570 $instance->timemodified = time();
571 $instance->feedbackformat = FORMAT_MOODLE;
572 $instanceid = $DB->insert_record('grading_instances', $instance);
573 return $instanceid;
574 }
575
576 /**
577 * Duplicates the instance before editing (optionally substitutes raterid and/or itemid with
578 * the specified values)
579 * Plugins may want to override this function to copy data from additional tables as well
580 *
581 * @param int $raterid value for raterid in the duplicate
582 * @param int $itemid value for itemid in the duplicate
583 * @return int id of the new instance
584 */
585 public function copy($raterid, $itemid) {
586 global $DB;
587 $data = (array)$this->data; // Cast to array to make a copy
588 unset($data['id']);
589 $data['raterid'] = $raterid;
590 $data['itemid'] = $itemid;
591 $data['timemodified'] = time();
592 $data['status'] = self::INSTANCE_STATUS_INCOMPLETE;
593 $instanceid = $DB->insert_record('grading_instances', $data);
594 return $instanceid;
595 }
596
597 /**
598 * Returns the controller
599 *
600 * @return gradingform_controller
601 */
602 public function get_controller() {
603 return $this->controller;
604 }
605
606 /**
607 * Returns instance id
608 *
609 * @return int
610 */
611 public function get_id() {
612 return $this->data->id;
613 }
614
615 /**
616 * Marks the instance as ACTIVE and current active instance (if exists) as ARCHIVE
617 */
618 protected function make_active() {
619 global $DB;
620 if ($this->data->status == self::INSTANCE_STATUS_ACTIVE) {
621 // already active
622 return;
623 }
fc05f222
MG
624 if (empty($this->data->itemid)) {
625 throw new coding_exception('You cannot mark active the grading instance without itemid');
626 }
36937f02
MG
627 $currentid = $this->get_controller()->get_current_instance($this->data->raterid, $this->data->itemid, true);
628 if ($currentid) {
629 if ($currentid != $this->get_id()) {
630 $DB->update_record('grading_instances', array('id' => $currentid, 'status' => self::INSTANCE_STATUS_ARCHIVE));
631 $DB->update_record('grading_instances', array('id' => $this->get_id(), 'status' => self::INSTANCE_STATUS_ACTIVE));
632 }
633 } else {
634 $DB->update_record('grading_instances', array('id' => $this->get_id(), 'status' => self::INSTANCE_STATUS_ACTIVE));
635 }
636 $this->data->status = self::INSTANCE_STATUS_ACTIVE;
637 }
638
639 /**
640 * Deletes this (INCOMPLETE) instance from database. This function is invoked on cancelling the
641 * grading form and/or during cron cleanup.
642 * Plugins using additional tables must override this method to remove additional data.
643 * Note that if the teacher just closes the window or presses 'Back' button of the browser,
644 * this function is not invoked.
645 */
646 public function cancel() {
647 global $DB;
648 // TODO what if we happen delete the ACTIVE instance, shall we rollback to the last ARCHIVE? or throw an exception?
649 // TODO create cleanup cron
650 $DB->delete_records('grading_instances', array('id' => $this->get_id()));
651 }
652
653 /**
654 * Updates the instance with the data received from grading form. This function may be
655 * called via AJAX when grading is not yet completed, so it does not change the
656 * status of the instance.
fc05f222
MG
657 *
658 * @param array $elementvalue
36937f02
MG
659 */
660 public function update($elementvalue) {
fc05f222
MG
661 global $DB;
662 $newdata = new stdClass();
663 $newdata->id = $this->get_id();
664 $newdata->timemodified = time();
665 if (isset($elementvalue['itemid']) && $elementvalue['itemid'] != $this->data->itemid) {
666 $newdata->itemid = $elementvalue['itemid'];
667 }
668 // TODO also update: rawgrade, feedback, feedbackformat
669 $DB->update_record('grading_instances', $newdata);
670 foreach ($newdata as $key => $value) {
671 $this->data->$key = $value;
672 }
36937f02
MG
673 }
674
675 /**
676 * Calculates the grade to be pushed to the gradebook
9e2eca0f
MG
677 *
678 * @return int the valid grade from $this->get_controller()->get_grade_range()
36937f02
MG
679 */
680 abstract public function get_grade();
681
682 /**
683 * Called when teacher submits the grading form:
fc05f222
MG
684 * updates the instance in DB, marks it as ACTIVE and returns the grade to be pushed to the gradebook.
685 * $itemid must be specified here (it was not required when the instance was
686 * created, because it might not existed in draft)
687 *
688 * @param array $elementvalue
689 * @param int $itemid
36937f02
MG
690 * @return int the grade on 0-100 scale
691 */
fc05f222
MG
692 public function submit_and_get_grade($elementvalue, $itemid) {
693 $elementvalue['itemid'] = $itemid;
36937f02
MG
694 $this->update($elementvalue);
695 $this->make_active();
696 return $this->get_grade();
697 }
698
699
700 /**
701 * Returns html for form element of type 'grading'. If there is a form input element
702 * it must have the name $gradingformelement->getName().
703 * If there are more than one input elements they MUST be elements of array with
704 * name $gradingformelement->getName().
705 * Example: {NAME}[myelement1], {NAME}[myelement2][sub1], {NAME}[myelement2][sub2], etc.
9e2eca0f 706 * ( {NAME} is a shortcut for $gradingformelement->getName() )
36937f02
MG
707 * After submitting the form the value of $_POST[{NAME}] is passed to the functions
708 * validate_grading_element() and submit_and_get_grade()
709 *
710 * Plugins may use $gradingformelement->getValue() to get the value passed on previous
9e2eca0f 711 * form submit
36937f02
MG
712 *
713 * When forming html it is a plugin's responsibility to analyze flags
714 * $gradingformelement->_flagFrozen and $gradingformelement->_persistantFreeze:
715 *
716 * (_flagFrozen == false) => form element is editable
717 *
718 * (_flagFrozen == false && _persistantFreeze == true) => form element is not editable
719 * but all values are passed as hidden elements
720 *
721 * (_flagFrozen == false && _persistantFreeze == false) => form element is not editable
722 * and no values are passed as hidden elements
723 *
724 * Plugins are welcome to use AJAX in the form element. But it is strongly recommended
725 * that the grading only becomes active when teacher presses 'Submit' button (the
726 * method submit_and_get_grade() is invoked)
727 *
728 * Also client-side JS validation may be implemented here
729 *
730 * @see MoodleQuickForm_grading in lib/form/grading.php
731 *
732 * @param moodle_page $page
733 * @param MoodleQuickForm_grading $gradingformelement
734 * @return string
735 */
736 abstract function render_grading_element($page, $gradingformelement);
737
738 /**
739 * Server-side validation of the data received from grading form.
740 *
741 * @param mixed $elementvalue is the scalar or array received in $_POST
742 * @return boolean true if the form data is validated and contains no errors
743 */
744 public function validate_grading_element($elementvalue) {
745 return true;
746 }
747
748 /**
749 * Returns the error message displayed if validation failed.
750 * If plugin wants to display custom message, the empty string should be returned here
751 * and the custom message should be output in render_grading_element()
752 *
753 * @see validate_grading_element()
754 * @return string
755 */
756 public function default_validation_error_message() {
757 return '';
758 }
759}