timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / lib / form / checkbox.php
blobd3c026214a6f28490839bbfa0cd9d968decb3a7b
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 if (!is_array($helpbuttonargs)){
29 $helpbuttonargs=array($helpbuttonargs);
30 }else{
31 $helpbuttonargs=$helpbuttonargs;
33 //we do this to to return html instead of printing it
34 //without having to specify it in every call to make a button.
35 if ('helpbutton' == $function){
36 $defaultargs=array('', '', 'moodle', true, false, '', true);
37 $helpbuttonargs=$helpbuttonargs + $defaultargs ;
39 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
41 /**
42 * get html for help button
44 * @access public
45 * @return string html for help button
47 function getHelpButton(){
48 return $this->_helpbutton;
50 /**
51 * Automatically generates and assigns an 'id' attribute for the element.
53 * Currently used to ensure that labels work on radio buttons and
54 * checkboxes. Per idea of Alexander Radivanovich.
55 * Overriden in moodleforms to remove qf_ prefix.
57 * @access private
58 * @return void
60 function _generateId()
62 static $idx = 1;
64 if (!$this->getAttribute('id')) {
65 $this->updateAttributes(array('id' => 'id_'.substr(md5(microtime() . $idx++), 0, 6)));
67 } // end func _generateId
68 /**
69 * Called by HTML_QuickForm whenever form event is made on this element
71 * @param string $event Name of event
72 * @param mixed $arg event arguments
73 * @param object $caller calling object
74 * @since 1.0
75 * @access public
76 * @return void
78 function onQuickFormEvent($event, $arg, &$caller)
80 //fixes bug in quickforms which lets previous set value override submitted value if checkbox is not checked
81 // and no value is submitted
82 switch ($event) {
83 case 'updateValue':
84 // constant values override both default and submitted ones
85 // default values are overriden by submitted
86 $value = $this->_findValue($caller->_constantValues);
87 if (null === $value) {
88 // if no boxes were checked, then there is no value in the array
89 // yet we don't want to display default value in this case
90 if ($caller->isSubmitted()) {
91 $value = $this->_findValue($caller->_submitValues);
92 } else {
94 $value = $this->_findValue($caller->_defaultValues);
97 //fix here. setChecked should not be conditional
98 $this->setChecked($value);
99 break;
100 default:
101 parent::onQuickFormEvent($event, $arg, $caller);
103 return true;
104 } // end func onQuickFormEvent
105 function toHtml()
107 return '<span>' . parent::toHtml() . '</span>';
111 * Returns the disabled field. Accessibility: the return "[ ]" from parent
112 * class is not acceptable for screenreader users, and we DO want a label.
113 * @return string
115 function getFrozenHtml()
117 //$this->_generateId();
118 $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
119 if ($this->getChecked()) {
120 $output .= 'checked="checked" />'.$this->_getPersistantData();
121 } else {
122 $output .= '/>';
124 return $output;
125 } //end func getFrozenHtml