Automatic installer lang files (20110214)
[moodle.git] / lib / form / checkbox.php
blob237c796a14950e489907f3ff51a65f8af6abc6f8
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 function MoodleQuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null) {
18 parent::HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes);
20 /**
21 * set html for help button
23 * @access public
24 * @param array $help array of arguments to make a help button
25 * @param string $function function name to call to get html
27 function setHelpButton($helpbuttonargs, $function='helpbutton'){
28 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
30 /**
31 * get html for help button
33 * @access public
34 * @return string html for help button
36 function getHelpButton(){
37 return $this->_helpbutton;
39 /**
40 * Automatically generates and assigns an 'id' attribute for the element.
42 * Currently used to ensure that labels work on radio buttons and
43 * checkboxes. Per idea of Alexander Radivanovich.
44 * Overriden in moodleforms to remove qf_ prefix.
46 * @access private
47 * @return void
49 function _generateId()
51 static $idx = 1;
53 if (!$this->getAttribute('id')) {
54 $this->updateAttributes(array('id' => 'id_'.substr(md5(microtime() . $idx++), 0, 6)));
56 } // end func _generateId
57 /**
58 * Called by HTML_QuickForm whenever form event is made on this element
60 * @param string $event Name of event
61 * @param mixed $arg event arguments
62 * @param object $caller calling object
63 * @since 1.0
64 * @access public
65 * @return void
67 function onQuickFormEvent($event, $arg, &$caller)
69 //fixes bug in quickforms which lets previous set value override submitted value if checkbox is not checked
70 // and no value is submitted
71 switch ($event) {
72 case 'updateValue':
73 // constant values override both default and submitted ones
74 // default values are overriden by submitted
75 $value = $this->_findValue($caller->_constantValues);
76 if (null === $value) {
77 // if no boxes were checked, then there is no value in the array
78 // yet we don't want to display default value in this case
79 if ($caller->isSubmitted()) {
80 $value = $this->_findValue($caller->_submitValues);
81 } else {
83 $value = $this->_findValue($caller->_defaultValues);
86 //fix here. setChecked should not be conditional
87 $this->setChecked($value);
88 break;
89 default:
90 parent::onQuickFormEvent($event, $arg, $caller);
92 return true;
93 } // end func onQuickFormEvent
94 function toHtml()
96 return '<span>' . parent::toHtml() . '</span>';
99 /**
100 * Returns the disabled field. Accessibility: the return "[ ]" from parent
101 * class is not acceptable for screenreader users, and we DO want a label.
102 * @return string
104 function getFrozenHtml()
106 //$this->_generateId();
107 $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
108 if ($this->getChecked()) {
109 $output .= 'checked="checked" />'.$this->_getPersistantData();
110 } else {
111 $output .= '/>';
113 return $output;
114 } //end func getFrozenHtml