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 * checkbox form element
21 * Contains HTML class for a checkbox type element
24 * @copyright 2007 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/checkbox.php');
29 require_once('templatable_form_element.php');
32 * HTML class for a checkbox type element
34 * Overloaded {@link HTML_QuickForm_checkbox} to add help button. Also, fixes bug in quickforms
35 * checkbox, which lets previous set value override submitted value if checkbox is not checked
36 * and no value is submitted
40 * @copyright 2007 Jamie Pratt <me@jamiep.org>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class MoodleQuickForm_checkbox
extends HTML_QuickForm_checkbox
implements templatable
{
45 use templatable_form_element
{
46 export_for_template
as export_for_template_base
;
49 /** @var string html for help button, if empty then no help */
55 * @param string $elementName (optional) name of the checkbox
56 * @param string $elementLabel (optional) checkbox label
57 * @param string $text (optional) Text to put after the checkbox
58 * @param mixed $attributes (optional) Either a typical HTML attribute string
59 * or an associative array
61 public function __construct($elementName=null, $elementLabel=null, $text='', $attributes=null) {
62 parent
::__construct($elementName, $elementLabel, $text, $attributes);
66 * Old syntax of class constructor. Deprecated in PHP7.
68 * @deprecated since Moodle 3.1
70 public function MoodleQuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null) {
71 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
72 self
::__construct($elementName, $elementLabel, $text, $attributes);
76 * get html for help button
78 * @return string html for help button
80 function getHelpButton(){
81 return $this->_helpbutton
;
85 * Called by HTML_QuickForm whenever form event is made on this element
87 * @param string $event Name of event
88 * @param mixed $arg event arguments
89 * @param object $caller calling object
92 function onQuickFormEvent($event, $arg, &$caller)
94 //fixes bug in quickforms which lets previous set value override submitted value if checkbox is not checked
95 // and no value is submitted
98 // constant values override both default and submitted ones
99 // default values are overriden by submitted
100 $value = $this->_findValue($caller->_constantValues
);
101 if (null === $value) {
102 // if no boxes were checked, then there is no value in the array
103 // yet we don't want to display default value in this case
104 if ($caller->isSubmitted()) {
105 $value = $this->_findValue($caller->_submitValues
);
108 $value = $this->_findValue($caller->_defaultValues
);
111 //fix here. setChecked should not be conditional
112 $this->setChecked($value);
115 parent
::onQuickFormEvent($event, $arg, $caller);
121 * Returns HTML for checbox form element.
127 return '<span>' . parent
::toHtml() . '</span>';
131 * Returns the disabled field. Accessibility: the return "[ ]" from parent
132 * class is not acceptable for screenreader users, and we DO want a label.
136 function getFrozenHtml()
138 //$this->_generateId();
139 $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
140 if ($this->getChecked()) {
141 $output .= 'checked="checked" />'.$this->_getPersistantData();
148 public function export_for_template(renderer_base
$output) {
149 $context = $this->export_for_template_base($output);
150 $context['frozenvalue'] = $this->getValue();