admin setting: MDL-18485 Improve the display of defaults for admin_setting_text_with_...
[moodle.git] / lib / form / dateselector.php
CommitLineData
da6f8763 1<?php
2global $CFG;
3require_once "$CFG->libdir/form/group.php";
4require_once "$CFG->libdir/formslib.php";
5
6/**
7 * Class for a group of elements used to input a date.
7f42315c 8 *
9 * Emulates moodle print_date_selector function
10 *
da6f8763 11 * @author Jamie Pratt <me@jamiep.org>
12 * @access public
13 */
7f40a229 14class MoodleQuickForm_date_selector extends MoodleQuickForm_group
da6f8763 15{
16 /**
17 * Control the fieldnames for form elements
18 *
e9d39a32 19 * startyear => integer start of range of years that can be selected
20 * stopyear => integer last year that can be selected
da6f8763 21 * timezone => float/string timezone
22 * applydst => apply users daylight savings adjustment?
de312b53 23 * optional => if true, show a checkbox beside the date to turn it on (or off)
da6f8763 24 */
25 var $_options = array('startyear'=>1970, 'stopyear'=>2020,
de312b53 26 'timezone'=>99, 'applydst'=>true, 'optional'=>false);
da6f8763 27
28 /**
29 * These complement separators, they are appended to the resultant HTML
30 * @access private
31 * @var array
32 */
33 var $_wrap = array('', '');
34
35 /**
36 * Class constructor
7f42315c 37 *
da6f8763 38 * @access public
39 * @param string Element's name
40 * @param mixed Label(s) for an element
41 * @param array Options to control the element's display
42 * @param mixed Either a typical HTML attribute string or an associative array
43 */
7f40a229 44 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
da6f8763 45 {
46 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
47 $this->_persistantFreeze = true;
48 $this->_appendName = true;
49 $this->_type = 'date_selector';
50 // set the options, do not bother setting bogus ones
51 if (is_array($options)) {
52 foreach ($options as $name => $value) {
53 if (isset($this->_options[$name])) {
54 if (is_array($value) && is_array($this->_options[$name])) {
55 $this->_options[$name] = @array_merge($this->_options[$name], $value);
56 } else {
57 $this->_options[$name] = $value;
58 }
59 }
60 }
61 }
62 }
63
64 // }}}
65 // {{{ _createElements()
66
67 function _createElements()
68 {
69 $this->_elements = array();
70 for ($i=1; $i<=31; $i++) {
71 $days[$i] = $i;
72 }
73 for ($i=1; $i<=12; $i++) {
76ab1c33 74 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
da6f8763 75 }
76 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
77 $years[$i] = $i;
78 }
dbc6607b 79 $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
80 $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
81 $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
46e648b6 82 // If optional we add a checkbox which the user can use to turn if on
de312b53 83 if($this->_options['optional']) {
11f260f4 84 $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
de312b53 85 }
44875d78 86 foreach ($this->_elements as $element){
87 if (method_exists($element, 'setHiddenLabel')){
88 $element->setHiddenLabel(true);
89 }
90 }
da6f8763 91
92 }
93
94 // }}}
46e648b6 95 // {{{ onQuickFormEvent()
96
11f260f4 97 /**
98 * Called by HTML_QuickForm whenever form event is made on this element
99 *
100 * @param string $event Name of event
101 * @param mixed $arg event arguments
102 * @param object $caller calling object
103 * @since 1.0
104 * @access public
105 * @return void
106 */
46e648b6 107 function onQuickFormEvent($event, $arg, &$caller)
108 {
11f260f4 109 switch ($event) {
110 case 'updateValue':
111 // constant values override both default and submitted ones
112 // default values are overriden by submitted
113 $value = $this->_findValue($caller->_constantValues);
114 if (null === $value) {
115 // if no boxes were checked, then there is no value in the array
116 // yet we don't want to display default value in this case
117 if ($caller->isSubmitted()) {
118 $value = $this->_findValue($caller->_submitValues);
119 } else {
120 $value = $this->_findValue($caller->_defaultValues);
121 }
122 }
123 $requestvalue=$value;
124 if ($value == 0) {
125 $value = time();
126 }
127 if (!is_array($value)) {
128 $currentdate = usergetdate($value);
129 $value = array(
130 'day' => $currentdate['mday'],
131 'month' => $currentdate['mon'],
132 'year' => $currentdate['year']);
133 // If optional, default to off, unless a date was provided
134 if($this->_options['optional']) {
135 $value['off'] = ($requestvalue == 0) ? true : false;
136 }
137 } else {
138 $value['off'] = (isset($value['off'])) ? true : false;
139 }
140 if (null !== $value){
141 $this->setValue($value);
142 }
143 break;
144 case 'createElement':
145 if($arg[2]['optional']) {
146 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
147 }
148 return parent::onQuickFormEvent($event, $arg, $caller);
149 break;
150 default:
151 return parent::onQuickFormEvent($event, $arg, $caller);
da6f8763 152 }
11f260f4 153 } // end func onQuickFormEvent
da6f8763 154
da6f8763 155 // {{{ toHtml()
156
157 function toHtml()
158 {
159 include_once('HTML/QuickForm/Renderer/Default.php');
160 $renderer =& new HTML_QuickForm_Renderer_Default();
161 $renderer->setElementTemplate('{element}');
162 parent::accept($renderer);
163 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
164 }
165
166 // }}}
167 // {{{ accept()
168
169 function accept(&$renderer, $required = false, $error = null)
170 {
171 $renderer->renderElement($this, $required, $error);
172 }
173
174 // }}}
da6f8763 175
da6f8763 176 /**
177 * Output a timestamp. Give it the name of the group.
178 *
179 * @param array $submitValues
180 * @param bool $assoc
181 * @return array
182 */
183 function exportValue(&$submitValues, $assoc = false)
184 {
185 $value = null;
57cf1be2 186 $valuearray = array();
187 foreach ($this->_elements as $element){
188 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
189 if ($thisexport!=null){
190 $valuearray += $thisexport;
191 }
192 }
193 if (count($valuearray)){
de312b53 194 if($this->_options['optional']) {
11f260f4 195 // If checkbox is on, the value is zero, so go no further
196 if(!empty($valuearray['off'])) {
de312b53 197 $value[$this->getName()]=0;
198 return $value;
199 }
200 }
11f260f4 201
57cf1be2 202 $value[$this->getName()]=make_timestamp($valuearray['year'],
203 $valuearray['month'],
204 $valuearray['day'],
205 0,0,0,
206 $this->_options['timezone'],
207 $this->_options['applydst']);
208
209 return $value;
210 } else {
211 return null;
212 }
da6f8763 213 }
214
215 // }}}
216}
76ab1c33 217?>