Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / form / checkbox.php
bloba2d9cbd9707b4c55c73b78c83c4bfa309988eaf4
1 <?php
2 require_once('HTML/QuickForm/checkbox.php');
4 /**
5 * HTML class for a checkbox type element
7 * @author Jamie Pratt
8 * @access public
9 */
10 class MoodleQuickForm_checkbox extends HTML_QuickForm_checkbox{
11 /**
12 * html for help button, if empty then no help
14 * @var string
16 var $_helpbutton='';
17 /**
18 * set html for help button
20 * @access public
21 * @param array $help array of arguments to make a help button
22 * @param string $function function name to call to get html
24 function setHelpButton($helpbuttonargs, $function='helpbutton'){
25 if (!is_array($helpbuttonargs)){
26 $helpbuttonargs=array($helpbuttonargs);
27 }else{
28 $helpbuttonargs=$helpbuttonargs;
30 //we do this to to return html instead of printing it
31 //without having to specify it in every call to make a button.
32 if ('helpbutton' == $function){
33 $defaultargs=array('', '', 'moodle', true, false, '', true);
34 $helpbuttonargs=$helpbuttonargs + $defaultargs ;
36 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
38 /**
39 * get html for help button
41 * @access public
42 * @return string html for help button
44 function getHelpButton(){
45 return $this->_helpbutton;
47 /**
48 * Automatically generates and assigns an 'id' attribute for the element.
50 * Currently used to ensure that labels work on radio buttons and
51 * checkboxes. Per idea of Alexander Radivanovich.
52 * Overriden in moodleforms to remove qf_ prefix.
54 * @access private
55 * @return void
57 function _generateId()
59 static $idx = 1;
61 if (!$this->getAttribute('id')) {
62 $this->updateAttributes(array('id' => 'id_'.substr(md5(microtime() . $idx++), 0, 6)));
64 } // end func _generateId
65 /**
66 * Called by HTML_QuickForm whenever form event is made on this element
68 * @param string $event Name of event
69 * @param mixed $arg event arguments
70 * @param object $caller calling object
71 * @since 1.0
72 * @access public
73 * @return void
75 function onQuickFormEvent($event, $arg, &$caller)
77 //fixes bug in quickforms which lets previous set value override submitted value if checkbox is not checked
78 // and no value is submitted
79 switch ($event) {
80 case 'updateValue':
81 // constant values override both default and submitted ones
82 // default values are overriden by submitted
83 $value = $this->_findValue($caller->_constantValues);
84 if (null === $value) {
85 // if no boxes were checked, then there is no value in the array
86 // yet we don't want to display default value in this case
87 if ($caller->isSubmitted()) {
88 $value = $this->_findValue($caller->_submitValues);
89 } else {
91 $value = $this->_findValue($caller->_defaultValues);
94 //fix here. setChecked should not be conditional
95 $this->setChecked($value);
96 break;
97 default:
98 parent::onQuickFormEvent($event, $arg, $caller);
100 return true;
101 } // end func onQuickFormEvent
102 function toHtml()
104 return '<span>' . parent::toHtml() . '</span>';
108 * Returns the disabled field. Accessibility: the return "[ ]" from parent
109 * class is not acceptable for screenreader users, and we DO want a label.
110 * @return string
112 function getFrozenHtml()
114 //$this->_generateId();
115 $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
116 if ($this->getChecked()) {
117 $output .= 'checked="checked" />'.$this->_getPersistantData();
118 } else {
119 $output .= '/>';
121 return $output;
122 } //end func getFrozenHtml