MDL-39888 mnet: Removal of an old sql statement invloving the mdl_log table.
[moodle.git] / lib / form / modgrade.php
blob252151640a65ba9d3e8261739c952ecc8ed5f421
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 * Drop down form element to select the grade
21 * Contains HTML class for a drop down element to select the grade for an activity,
22 * used in mod update form
24 * @package core_form
25 * @copyright 2006 Jamie Pratt <me@jamiep.org>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 global $CFG;
30 require_once "$CFG->libdir/form/select.php";
31 require_once("HTML/QuickForm/element.php");
32 require_once($CFG->dirroot.'/lib/form/group.php');
33 require_once($CFG->dirroot.'/lib/grade/grade_scale.php');
35 /**
36 * Drop down form element to select the grade
38 * HTML class for a drop down element to select the grade for an activity,
39 * used in mod update form
41 * @package core_form
42 * @category form
43 * @copyright 2006 Jamie Pratt <me@jamiep.org>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 class MoodleQuickForm_modgrade extends MoodleQuickForm_group{
48 /**
49 * Constructor
51 * @param string $elementname Element's name
52 * @param mixed $elementlabel Label(s) for an element
53 * @param array $options Options to control the element's display. Not used.
54 * @param mixed $attributes Either a typical HTML attribute string or an associative array
56 public function MoodleQuickForm_modgrade($elementname = null, $elementlabel = null, $options = array(), $attributes = null) {
57 $this->HTML_QuickForm_element($elementname, $elementlabel, $attributes);
58 $this->_persistantFreeze = true;
59 $this->_appendName = true;
60 $this->_type = 'modgrade';
63 /**
64 * Create elements for this group.
66 public function _createElements() {
67 global $COURSE, $CFG;
68 $attributes = $this->getAttributes();
69 if (is_null($attributes)) {
70 $attributes = array();
73 $this->_elements = array();
75 // Create main elements
76 // We have to create the scale and point elements first, as we need their IDs.
78 // Grade scale select box.
79 $scales = get_scales_menu($COURSE->id);
80 $langscale = get_string('modgradetypescale', 'grades');
81 $scaleselect = @MoodleQuickForm::createElement('select', 'modgrade_scale', $langscale, $scales, $attributes);
82 $scaleselect->setHiddenLabel = false;
83 $scaleselect->_generateId();
84 $scaleselectid = $scaleselect->getAttribute('id');
86 // Maximum grade textbox.
87 $langmaxgrade = get_string('modgrademaxgrade', 'grades');
88 $maxgrade = @MoodleQuickForm::createElement('text', 'modgrade_point', $langmaxgrade, array());
89 $maxgrade->setHiddenLabel = false;
90 $maxgrade->_generateId();
91 $maxgradeid = $maxgrade->getAttribute('id');
93 // Grade type select box.
94 $gradetype = array(
95 'none' => get_string('modgradetypenone', 'grades'),
96 'scale' => get_string('modgradetypescale', 'grades'),
97 'point' => get_string('modgradetypepoint', 'grades'),
99 $langtype = get_string('modgradetype', 'grades');
100 $typeselect = @MoodleQuickForm::createElement('select', 'modgrade_type', $langtype, $gradetype, $attributes, true);
101 $typeselect->setHiddenLabel = false;
102 $typeselect->_generateId();
104 // Add elements.
106 // Grade type select box.
107 $label = html_writer::tag('label', $typeselect->getLabel(), array('for' => $typeselect->getAttribute('id')));
108 $this->_elements[] = @MoodleQuickForm::createElement('static', 'gradetypelabel', '', '&nbsp;'.$label);
109 $this->_elements[] = $typeselect;
110 $this->_elements[] = @MoodleQuickForm::createElement('static', 'gradetypespacer', '', '<br />');
112 // Grade scale select box.
113 $label = html_writer::tag('label', $scaleselect->getLabel(), array('for' => $scaleselectid));
114 $this->_elements[] = @MoodleQuickForm::createElement('static', 'scalelabel', '', $label);
115 $this->_elements[] = $scaleselect;
116 $this->_elements[] = @MoodleQuickForm::createElement('static', 'scalespacer', '', '<br />');
118 // Maximum grade textbox.
119 $label = html_writer::tag('label', $maxgrade->getLabel(), array('for' => $maxgradeid));
120 $this->_elements[] = @MoodleQuickForm::createElement('static', 'pointlabel', '', $label);
121 $this->_elements[] = $maxgrade;
122 $this->_elements[] = @MoodleQuickForm::createElement('static', 'pointspacer', '', '<br />');
126 * Calculate the output value for the element as a whole.
128 * @param array $submitvalues The incoming values from the form.
129 * @param bool $notused Not used.
130 * @return array Return value for the element, formatted like field name => value.
132 public function exportValue(&$submitvalues, $notused = false) {
133 global $COURSE;
135 // Get the values from all the child elements.
136 $vals = array();
137 foreach ($this->_elements as $element) {
138 $thisexport = $element->exportValue($submitvalues[$this->getName()], true);
139 if (!is_null($thisexport)) {
140 $vals += $thisexport;
144 $type = (isset($vals['modgrade_type'])) ? $vals['modgrade_type'] : 'none';
145 $point = (isset($vals['modgrade_point'])) ? $vals['modgrade_point'] : null;
146 $scale = (isset($vals['modgrade_scale'])) ? $vals['modgrade_scale'] : null;
147 $return = $this->process_value($type, $scale, $point);
148 return array($this->getName() => $return);
152 * Process the value for the group based on the selected grade type, and the input for the scale and point elements.
154 * @param string $type The value of the grade type select box. Can be 'none', 'scale', or 'point'
155 * @param string|int $scale The value of the scale select box.
156 * @param string|int $point The value of the point grade textbox.
157 * @return int The resulting value
159 protected function process_value($type='none', $scale=null, $point=null) {
160 global $COURSE;
161 $val = 0;
162 switch ($type) {
163 case 'point':
164 if ($this->validate_point($point) === true) {
165 $val = (int)$point;
167 break;
169 case 'scale':
170 if ($this->validate_scale($scale)) {
171 $val = (int)(-$scale);
173 break;
175 return $val;
179 * Determines whether a given value is a valid scale selection.
181 * @param string|int $val The value to test.
182 * @return bool Valid or invalid
184 protected function validate_scale($val) {
185 global $COURSE;
186 $scales = get_scales_menu($COURSE->id);
187 return (!empty($val) && isset($scales[(int)$val])) ? true : false;
191 * Determines whether a given value is a valid point selection.
193 * @param string|int $val The value to test.
194 * @return bool Valid or invalid
196 protected function validate_point($val) {
197 if (empty($val)) {
198 return false;
200 $maxgrade = (int)get_config('core', 'gradepointmax');
201 $isintlike = ((string)(int)$val === $val) ? true : false;
202 return ($isintlike === true && $val > 0 && $val <= $maxgrade) ? true : false;
206 * Called by HTML_QuickForm whenever form event is made on this element.
208 * @param string $event Name of event
209 * @param mixed $arg event arguments
210 * @param object $caller calling object
211 * @return mixed
213 public function onQuickFormEvent($event, $arg, &$caller) {
214 global $COURSE;
216 switch ($event) {
217 case 'updateValue':
218 $value = $this->_findValue($caller->_constantValues);
219 if (null === $value) {
220 if ($caller->isSubmitted()) {
221 $value = $this->_findValue($caller->_submitValues);
222 } else {
223 $value = $this->_findValue($caller->_defaultValues);
227 $name = $this->getName();
229 // Set disable actions.
230 $caller->disabledIf($name.'[modgrade_scale]', $name.'[modgrade_type]', 'neq', 'scale');
231 $caller->disabledIf($name.'[modgrade_point]', $name.'[modgrade_type]', 'neq', 'point');
233 // Set element state for existing data.
234 if (!empty($this->_elements)) {
235 if (!empty($value)) {
236 if ($value < 0) {
237 $this->_elements[1]->setValue('scale');
238 $this->_elements[4]->setValue(($value * -1));
239 } else if ($value > 0) {
240 $this->_elements[1]->setValue('point');
241 $this->_elements[7]->setValue($value);
243 } else {
244 $this->_elements[1]->setValue('none');
245 $this->_elements[7]->setValue('');
249 // Value Validation.
250 if ($name && $caller->elementExists($name)) {
251 $checkmaxgrade = function($val) {
252 if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') {
253 if (!isset($val['modgrade_point'])) {
254 return false;
256 return $this->validate_point($val['modgrade_point']);
258 return true;
261 $checkvalidscale = function($val) {
262 if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'scale') {
263 if (!isset($val['modgrade_scale'])) {
264 return false;
266 return $this->validate_scale($val['modgrade_scale']);
268 return true;
271 $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax'));
272 $invalidscale = get_string('modgradeerrorbadscale', 'grades');
273 $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade);
274 $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale);
276 break;
280 return parent::onQuickFormEvent($event, $arg, $caller);