weekly release 4.5dev
[moodle.git] / lib / form / grading.php
blobaf379c7f8e5e81a795c3102452dcc0476da3dbb5
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
18 /**
19 * Advance grading form element
21 * Element-container for advanced grading custom input
23 * @package core_form
24 * @copyright 2011 Marina Glancy
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $CFG;
29 require_once("HTML/QuickForm/element.php");
30 require_once($CFG->dirroot.'/grade/grading/form/lib.php');
31 require_once('templatable_form_element.php');
33 if (class_exists('HTML_QuickForm')) {
34 HTML_QuickForm::registerRule('gradingvalidated', 'callback', '_validate', 'MoodleQuickForm_grading');
37 /**
38 * Advance grading form element
40 * HTML class for a grading element. This is a wrapper for advanced grading plugins.
41 * When adding the 'grading' element to the form, developer must pass an object of
42 * class gradingform_instance as $attributes['gradinginstance']. Otherwise an exception will be
43 * thrown.
44 * This object is responsible for implementing functions to render element html and validate it
46 * @package core_form
47 * @category form
48 * @copyright 2011 Marina Glancy
49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 class MoodleQuickForm_grading extends HTML_QuickForm_input implements templatable {
52 use templatable_form_element {
53 export_for_template as export_for_template_base;
56 /** @var string html for help button, if empty then no help */
57 var $_helpbutton='';
59 /** @var array Stores attributes passed to the element */
60 private $gradingattributes;
62 /**
63 * Class constructor
65 * @param string $elementName Input field name attribute
66 * @param mixed $elementLabel Label(s) for the input field
67 * @param mixed $attributes Either a typical HTML attribute string or an associative array
69 public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
70 parent::__construct($elementName, $elementLabel, $attributes);
71 $this->_type = 'grading';
72 $this->gradingattributes = $attributes;
75 /**
76 * Old syntax of class constructor. Deprecated in PHP7.
78 * @deprecated since Moodle 3.1
80 public function MoodleQuickForm_grading($elementName=null, $elementLabel=null, $attributes=null) {
81 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
82 self::__construct($elementName, $elementLabel, $attributes);
85 /**
86 * Helper function to retrieve gradingform_instance passed in element attributes
88 * @return gradingform_instance
90 public function get_gradinginstance() {
91 if (is_array($this->gradingattributes) && array_key_exists('gradinginstance', $this->gradingattributes)) {
92 return $this->gradingattributes['gradinginstance'];
93 } else {
94 return null;
98 /**
99 * Returns the input field in HTML
101 * @return string
103 public function toHtml(){
104 global $PAGE;
105 return $this->get_gradinginstance()->render_grading_element($PAGE, $this);
109 * get html for help button
111 * @return string html for help button
113 public function getHelpButton(){
114 return $this->_helpbutton;
118 * The renderer of gradingform_instance will take care itself about different display
119 * in normal and frozen states
121 * @return string
123 public function getElementTemplateType(){
124 return 'default';
128 * Called by HTML_QuickForm whenever form event is made on this element.
129 * Adds necessary rules to the element and checks that coorenct instance of gradingform_instance
130 * is passed in attributes
132 * @param string $event Name of event
133 * @param mixed $arg event arguments
134 * @param object $caller calling object
135 * @return bool
136 * @throws moodle_exception
138 public function onQuickFormEvent($event, $arg, &$caller) {
139 if ($event == 'createElement') {
140 $attributes = $arg[2];
141 if (!is_array($attributes) || !array_key_exists('gradinginstance', $attributes) || !($attributes['gradinginstance'] instanceof gradingform_instance)) {
142 throw new moodle_exception('exc_gradingformelement', 'grading');
146 $name = $this->getName();
147 if ($name && $caller->elementExists($name)) {
148 $caller->addRule($name, $this->get_gradinginstance()->default_validation_error_message(), 'gradingvalidated', $this->gradingattributes);
150 return parent::onQuickFormEvent($event, $arg, $caller);
154 * Function registered as rule for this element and is called when this element is being validated.
155 * This is a wrapper to pass the validation to the method gradingform_instance::validate_grading_element
157 * @param mixed $elementvalue value of element to be validated
158 * @param array $attributes element attributes
159 * @return MoodleQuickForm_grading
161 public static function _validate($elementvalue, $attributes = null) {
162 if (!$attributes['gradinginstance']->is_empty_form($elementvalue)) {
163 return $attributes['gradinginstance']->validate_grading_element($elementvalue);
165 return true;
168 public function export_for_template(renderer_base $output) {
169 $context = $this->export_for_template_base($output);
170 $context['html'] = $this->toHtml();
171 return $context;