Merge branch 'MDL-32657-master-1' of git://git.luns.net.uk/moodle
[moodle.git] / lib / form / datetimeselector.php
blobf7468c23c37d14654fb1da204fc82a83e63514f9
1 <?php
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.
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/>.
18 /**
19 * Group of date and time input element
21 * Contains class for a group of elements used to input a date and time.
23 * @package core_form
24 * @copyright 2006 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $CFG;
29 require_once($CFG->libdir . '/form/group.php');
30 require_once($CFG->libdir . '/formslib.php');
32 /**
33 * Element used to input a date and time.
35 * Class for a group of elements used to input a date and time.
37 * @package core_form
38 * @category form
39 * @copyright 2006 Jamie Pratt <me@jamiep.org>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
43 /**
44 * Options for the element
45 * startyear => int start of range of years that can be selected
46 * stopyear => int last year that can be selected
47 * defaulttime => default time value if the field is currently not set
48 * timezone => float/string timezone
49 * applydst => apply users daylight savings adjustment?
50 * step => step to increment minutes by
51 * optional => if true, show a checkbox beside the date to turn it on (or off)
52 * @var array
54 var $_options = array('startyear' => 1970, 'stopyear' => 2020, 'defaulttime' => 0,
55 'timezone' => 99, 'applydst' => true, 'step' => 5, 'optional' => false);
57 /** @var array These complement separators, they are appended to the resultant HTML */
58 var $_wrap = array('', '');
60 /**
61 * Class constructor
63 * @param string $elementName Element's name
64 * @param mixed $elementLabel Label(s) for an element
65 * @param array $options Options to control the element's display
66 * @param mixed $attributes Either a typical HTML attribute string or an associative array
68 function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
70 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
71 $this->_persistantFreeze = true;
72 $this->_appendName = true;
73 $this->_type = 'date_time_selector';
74 // set the options, do not bother setting bogus ones
75 if (is_array($options)) {
76 foreach ($options as $name => $value) {
77 if (isset($this->_options[$name])) {
78 if (is_array($value) && is_array($this->_options[$name])) {
79 $this->_options[$name] = @array_merge($this->_options[$name], $value);
80 } else {
81 $this->_options[$name] = $value;
86 form_init_date_js();
89 /**
90 * This will create date group element constisting of day, month and year.
92 * @access private
94 function _createElements()
96 $this->_elements = array();
97 for ($i=1; $i<=31; $i++) {
98 $days[$i] = $i;
100 for ($i=1; $i<=12; $i++) {
101 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
103 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
104 $years[$i] = $i;
106 for ($i=0; $i<=23; $i++) {
107 $hours[$i] = sprintf("%02d",$i);
109 for ($i=0; $i<60; $i+=$this->_options['step']) {
110 $minutes[$i] = sprintf("%02d",$i);
112 // E_STRICT creating elements without forms is nasty because it internally uses $this
113 $this->_elements[] = @MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
114 $this->_elements[] = @MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
115 $this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
116 if (right_to_left()) { // Switch order of elements for Right-to-Left
117 $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
118 $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
119 } else {
120 $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
121 $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
123 // If optional we add a checkbox which the user can use to turn if on
124 if($this->_options['optional']) {
125 $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
127 foreach ($this->_elements as $element){
128 if (method_exists($element, 'setHiddenLabel')){
129 $element->setHiddenLabel(true);
136 * Called by HTML_QuickForm whenever form event is made on this element
138 * @param string $event Name of event
139 * @param mixed $arg event arguments
140 * @param object $caller calling object
141 * @return bool
143 function onQuickFormEvent($event, $arg, &$caller)
145 switch ($event) {
146 case 'updateValue':
147 // constant values override both default and submitted ones
148 // default values are overriden by submitted
149 $value = $this->_findValue($caller->_constantValues);
150 if (null === $value) {
151 // if no boxes were checked, then there is no value in the array
152 // yet we don't want to display default value in this case
153 if ($caller->isSubmitted()) {
154 $value = $this->_findValue($caller->_submitValues);
155 } else {
156 $value = $this->_findValue($caller->_defaultValues);
159 $requestvalue=$value;
160 if ($value == 0) {
161 $value = $this->_options['defaulttime'];
162 if (!$value) {
163 $value = time();
166 if (!is_array($value)) {
167 $currentdate = usergetdate($value);
168 // Round minutes to the previous multiple of step.
169 $currentdate['minutes'] -= $currentdate['minutes'] % $this->_options['step'];
170 $value = array(
171 'minute' => $currentdate['minutes'],
172 'hour' => $currentdate['hours'],
173 'day' => $currentdate['mday'],
174 'month' => $currentdate['mon'],
175 'year' => $currentdate['year']);
176 // If optional, default to off, unless a date was provided
177 if($this->_options['optional']) {
178 $value['enabled'] = $requestvalue != 0;
180 } else {
181 $value['enabled'] = isset($value['enabled']);
183 if (null !== $value){
184 $this->setValue($value);
186 break;
187 case 'createElement':
188 if($arg[2]['optional']) {
189 $caller->disabledIf($arg[0], $arg[0].'[enabled]');
191 return parent::onQuickFormEvent($event, $arg, $caller);
192 break;
193 default:
194 return parent::onQuickFormEvent($event, $arg, $caller);
199 * Returns HTML for advchecbox form element.
201 * @return string
203 function toHtml()
205 include_once('HTML/QuickForm/Renderer/Default.php');
206 $renderer = new HTML_QuickForm_Renderer_Default();
207 $renderer->setElementTemplate('{element}');
208 parent::accept($renderer);
209 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
213 * Accepts a renderer
215 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
216 * @param bool $required Whether a group is required
217 * @param string $error An error message associated with a group
219 function accept(&$renderer, $required = false, $error = null)
221 $renderer->renderElement($this, $required, $error);
225 * Output a timestamp. Give it the name of the group.
227 * @param array $submitValues values submitted.
228 * @param bool $assoc specifies if returned array is associative
229 * @return array
231 function exportValue(&$submitValues, $assoc = false)
233 $value = null;
234 $valuearray = array();
235 foreach ($this->_elements as $element){
236 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
237 if ($thisexport!=null){
238 $valuearray += $thisexport;
241 if (count($valuearray)){
242 if($this->_options['optional']) {
243 // If checkbox is on, the value is zero, so go no further
244 if(empty($valuearray['enabled'])) {
245 $value[$this->getName()] = 0;
246 return $value;
249 $valuearray=$valuearray + array('year' => 1970, 'month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0);
250 $value[$this->getName()] = make_timestamp(
251 $valuearray['year'],
252 $valuearray['month'],
253 $valuearray['day'],
254 $valuearray['hour'],
255 $valuearray['minute'],
257 $this->_options['timezone'],
258 $this->_options['applydst']);
260 return $value;
261 } else {
263 return null;