MDL-27562 formslib: applydst should be always true, hence option removed from datesel...
[moodle.git] / lib / form / dateselector.php
CommitLineData
4a0e2e63 1<?php
6c1fd304
RT
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.
13//
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/>.
25fe66b0 16
6c1fd304
RT
17
18/**
19 * Group of date input element
20 *
21 * Contains class for a group of elements used to input a date.
22 *
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
26 */
25fe66b0 27
da6f8763 28global $CFG;
25fe66b0 29require_once($CFG->libdir . '/form/group.php');
30require_once($CFG->libdir . '/formslib.php');
da6f8763 31
32/**
33 * Class for a group of elements used to input a date.
7f42315c 34 *
35 * Emulates moodle print_date_selector function
36 *
6c1fd304
RT
37 * @package core_form
38 * @category form
39 * @copyright 2007 Jamie Pratt <me@jamiep.org>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
da6f8763 41 */
7f40a229 42class MoodleQuickForm_date_selector extends MoodleQuickForm_group
da6f8763 43{
44 /**
6c1fd304
RT
45 * Control the fieldnames for form elements
46 * startyear => int start of range of years that can be selected
47 * stopyear => int last year that can be selected
fa70f635
RT
48 * timezone => int|float|string (optional) timezone modifier used for edge case only.
49 * If not specified, then date is caclulated based on current user timezone.
50 * Note: dst will be calculated for string timezones only
51 * {@link http://docs.moodle.org/dev/Time_API#Timezone}
6c1fd304
RT
52 * optional => if true, show a checkbox beside the date to turn it on (or off)
53 * @var array
54 */
25fe66b0 55 protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
c106a9bf 56 'timezone' => 99, 'optional' => false);
da6f8763 57
6c1fd304 58 /** @var array These complement separators, they are appended to the resultant HTML */
25fe66b0 59 protected $_wrap = array('', '');
da6f8763 60
6c1fd304
RT
61 /**
62 * constructor
63 *
64 * @param string $elementName Element's name
65 * @param mixed $elementLabel Label(s) for an element
66 * @param array $options Options to control the element's display
67 * @param mixed $attributes Either a typical HTML attribute string or an associative array
68 */
7f40a229 69 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
da6f8763 70 {
71 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
72 $this->_persistantFreeze = true;
73 $this->_appendName = true;
74 $this->_type = 'date_selector';
75 // set the options, do not bother setting bogus ones
76 if (is_array($options)) {
77 foreach ($options as $name => $value) {
78 if (isset($this->_options[$name])) {
79 if (is_array($value) && is_array($this->_options[$name])) {
80 $this->_options[$name] = @array_merge($this->_options[$name], $value);
81 } else {
82 $this->_options[$name] = $value;
83 }
84 }
85 }
86 }
cf615522 87 form_init_date_js();
da6f8763 88 }
89
6c1fd304
RT
90 /**
91 * This will create date group element constisting of day, month and year.
92 *
93 * @access private
94 */
da6f8763 95 function _createElements()
96 {
97 $this->_elements = array();
98 for ($i=1; $i<=31; $i++) {
99 $days[$i] = $i;
100 }
101 for ($i=1; $i<=12; $i++) {
76ab1c33 102 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
da6f8763 103 }
104 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
105 $years[$i] = $i;
106 }
fabbf439
PS
107 // E_STRICT creating elements without forms is nasty because it internally uses $this
108 $this->_elements[] = @MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
109 $this->_elements[] = @MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
110 $this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
46e648b6 111 // If optional we add a checkbox which the user can use to turn if on
de312b53 112 if($this->_options['optional']) {
e2bb3c92 113 $this->_elements[] =@MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
de312b53 114 }
44875d78 115 foreach ($this->_elements as $element){
116 if (method_exists($element, 'setHiddenLabel')){
117 $element->setHiddenLabel(true);
118 }
119 }
da6f8763 120
121 }
122
11f260f4 123 /**
124 * Called by HTML_QuickForm whenever form event is made on this element
58b7d48f 125 *
6c1fd304
RT
126 * @param string $event Name of event
127 * @param mixed $arg event arguments
128 * @param object $caller calling object
129 * @return bool
11f260f4 130 */
46e648b6 131 function onQuickFormEvent($event, $arg, &$caller)
132 {
11f260f4 133 switch ($event) {
134 case 'updateValue':
135 // constant values override both default and submitted ones
136 // default values are overriden by submitted
137 $value = $this->_findValue($caller->_constantValues);
138 if (null === $value) {
139 // if no boxes were checked, then there is no value in the array
140 // yet we don't want to display default value in this case
141 if ($caller->isSubmitted()) {
142 $value = $this->_findValue($caller->_submitValues);
143 } else {
144 $value = $this->_findValue($caller->_defaultValues);
145 }
146 }
147 $requestvalue=$value;
148 if ($value == 0) {
149 $value = time();
150 }
151 if (!is_array($value)) {
fa70f635 152 $currentdate = usergetdate($value, $this->_options['timezone']);
11f260f4 153 $value = array(
154 'day' => $currentdate['mday'],
155 'month' => $currentdate['mon'],
156 'year' => $currentdate['year']);
157 // If optional, default to off, unless a date was provided
158 if($this->_options['optional']) {
25fe66b0 159 $value['enabled'] = $requestvalue != 0;
11f260f4 160 }
161 } else {
25fe66b0 162 $value['enabled'] = isset($value['enabled']);
11f260f4 163 }
164 if (null !== $value){
165 $this->setValue($value);
166 }
167 break;
168 case 'createElement':
fec617de
SH
169 // Optional is an optional param, if its set we need to add a disabledIf rule.
170 // If its empty or not specified then its not an optional dateselector.
171 if (!empty($arg[2]['optional']) && !empty($arg[0])) {
25fe66b0 172 $caller->disabledIf($arg[0], $arg[0].'[enabled]');
11f260f4 173 }
174 return parent::onQuickFormEvent($event, $arg, $caller);
175 break;
176 default:
177 return parent::onQuickFormEvent($event, $arg, $caller);
da6f8763 178 }
6c1fd304 179 }
da6f8763 180
6c1fd304
RT
181 /**
182 * Returns HTML for advchecbox form element.
183 *
184 * @return string
185 */
da6f8763 186 function toHtml()
187 {
188 include_once('HTML/QuickForm/Renderer/Default.php');
bd69b9db 189 $renderer = new HTML_QuickForm_Renderer_Default();
da6f8763 190 $renderer->setElementTemplate('{element}');
191 parent::accept($renderer);
192 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
193 }
194
6c1fd304
RT
195 /**
196 * Accepts a renderer
197 *
198 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
199 * @param bool $required Whether a group is required
200 * @param string $error An error message associated with a group
201 */
da6f8763 202 function accept(&$renderer, $required = false, $error = null)
203 {
204 $renderer->renderElement($this, $required, $error);
205 }
206
da6f8763 207 /**
208 * Output a timestamp. Give it the name of the group.
209 *
6c1fd304
RT
210 * @param array $submitValues values submitted.
211 * @param bool $assoc specifies if returned array is associative
da6f8763 212 * @return array
213 */
214 function exportValue(&$submitValues, $assoc = false)
215 {
216 $value = null;
57cf1be2 217 $valuearray = array();
218 foreach ($this->_elements as $element){
219 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
220 if ($thisexport!=null){
221 $valuearray += $thisexport;
222 }
223 }
224 if (count($valuearray)){
de312b53 225 if($this->_options['optional']) {
11f260f4 226 // If checkbox is on, the value is zero, so go no further
25fe66b0 227 if(empty($valuearray['enabled'])) {
228 $value[$this->getName()] = 0;
de312b53 229 return $value;
230 }
231 }
11f260f4 232
25fe66b0 233 $value[$this->getName()] = make_timestamp($valuearray['year'],
57cf1be2 234 $valuearray['month'],
235 $valuearray['day'],
25fe66b0 236 0, 0, 0,
57cf1be2 237 $this->_options['timezone'],
c106a9bf 238 true);
57cf1be2 239
240 return $value;
241 } else {
242 return null;
243 }
da6f8763 244 }
da6f8763 245}