2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Advance grading form element
21 * Element-container for advanced grading custom input
24 * @copyright 2011 Marina Glancy
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once("HTML/QuickForm/element.php");
30 require_once($CFG->dirroot
.'/grade/grading/form/lib.php');
32 if (class_exists('HTML_QuickForm')) {
33 HTML_QuickForm
::registerRule('gradingvalidated', 'callback', '_validate', 'MoodleQuickForm_grading');
37 * Advance grading form element
39 * HTML class for a grading element. This is a wrapper for advanced grading plugins.
40 * When adding the 'grading' element to the form, developer must pass an object of
41 * class gradingform_instance as $attributes['gradinginstance']. Otherwise an exception will be
43 * This object is responsible for implementing functions to render element html and validate it
47 * @copyright 2011 Marina Glancy
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50 class MoodleQuickForm_grading
extends HTML_QuickForm_input
{
51 /** @var string html for help button, if empty then no help */
54 /** @var array Stores attributes passed to the element */
55 private $gradingattributes;
60 * @param string $elementName Input field name attribute
61 * @param mixed $elementLabel Label(s) for the input field
62 * @param mixed $attributes Either a typical HTML attribute string or an associative array
64 public function MoodleQuickForm_grading($elementName=null, $elementLabel=null, $attributes=null) {
65 parent
::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
66 $this->gradingattributes
= $attributes;
70 * Helper function to retrieve gradingform_instance passed in element attributes
72 * @return gradingform_instance
74 public function get_gradinginstance() {
75 if (is_array($this->gradingattributes
) && array_key_exists('gradinginstance', $this->gradingattributes
)) {
76 return $this->gradingattributes
['gradinginstance'];
83 * Returns the input field in HTML
87 public function toHtml(){
89 return $this->get_gradinginstance()->render_grading_element($PAGE, $this);
93 * get html for help button
95 * @return string html for help button
97 public function getHelpButton(){
98 return $this->_helpbutton
;
102 * The renderer of gradingform_instance will take care itself about different display
103 * in normal and frozen states
107 public function getElementTemplateType(){
112 * Called by HTML_QuickForm whenever form event is made on this element.
113 * Adds necessary rules to the element and checks that coorenct instance of gradingform_instance
114 * is passed in attributes
116 * @param string $event Name of event
117 * @param mixed $arg event arguments
118 * @param object $caller calling object
120 * @throws moodle_exception
122 public function onQuickFormEvent($event, $arg, &$caller) {
123 if ($event == 'createElement') {
124 $attributes = $arg[2];
125 if (!is_array($attributes) ||
!array_key_exists('gradinginstance', $attributes) ||
!($attributes['gradinginstance'] instanceof gradingform_instance
)) {
126 throw new moodle_exception('exc_gradingformelement', 'grading');
130 $name = $this->getName();
131 if ($name && $caller->elementExists($name)) {
132 $caller->addRule($name, $this->get_gradinginstance()->default_validation_error_message(), 'gradingvalidated', $this->gradingattributes
);
134 return parent
::onQuickFormEvent($event, $arg, $caller);
138 * Function registered as rule for this element and is called when this element is being validated.
139 * This is a wrapper to pass the validation to the method gradingform_instance::validate_grading_element
141 * @param mixed $elementvalue value of element to be validated
142 * @param array $attributes element attributes
143 * @return MoodleQuickForm_grading
145 public static function _validate($elementvalue, $attributes = null) {
146 if (!$attributes['gradinginstance']->is_empty_form($elementvalue)) {
147 return $attributes['gradinginstance']->validate_grading_element($elementvalue);