MDL-60061 mod_scorm: add support for drag/drop events
[moodle.git] / lib / form / checkbox.php
blob5366ebb57e140bdbc16c4aced2edcef168a65522
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 * checkbox form element
21 * Contains HTML class for a checkbox type element
23 * @package core_form
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');
31 /**
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
38 * @package core_form
39 * @category form
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 */
50 var $_helpbutton='';
52 /**
53 * Constructor
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);
65 /**
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);
75 /**
76 * get html for help button
78 * @return string html for help button
80 function getHelpButton(){
81 return $this->_helpbutton;
84 /**
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
90 * @return bool
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
96 switch ($event) {
97 case 'updateValue':
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);
106 } else {
108 $value = $this->_findValue($caller->_defaultValues);
111 //fix here. setChecked should not be conditional
112 $this->setChecked($value);
113 break;
114 default:
115 parent::onQuickFormEvent($event, $arg, $caller);
117 return true;
121 * Returns HTML for checbox form element.
123 * @return string
125 function toHtml()
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.
134 * @return string
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();
142 } else {
143 $output .= '/>';
145 return $output;
148 public function export_for_template(renderer_base $output) {
149 $context = $this->export_for_template_base($output);
150 $context['frozenvalue'] = $this->getValue();
151 return $context;