Merge branch 'MDL-32657-master-1' of git://git.luns.net.uk/moodle
[moodle.git] / lib / form / dateselector.php
blob969b11c4dc6ccfc856083229d8f6768f45107433
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 input element
21 * Contains class for a group of elements used to input a date.
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
28 global $CFG;
29 require_once($CFG->libdir . '/form/group.php');
30 require_once($CFG->libdir . '/formslib.php');
32 /**
33 * Class for a group of elements used to input a date.
35 * Emulates moodle print_date_selector function
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
42 class MoodleQuickForm_date_selector extends MoodleQuickForm_group
44 /**
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
48 * timezone => float/string timezone
49 * applydst => apply users daylight savings adjustment?
50 * optional => if true, show a checkbox beside the date to turn it on (or off)
51 * @var array
53 protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
54 'timezone' => 99, 'applydst' => true, 'optional' => false);
56 /** @var array These complement separators, they are appended to the resultant HTML */
57 protected $_wrap = array('', '');
59 /**
60 * constructor
62 * @param string $elementName Element's name
63 * @param mixed $elementLabel Label(s) for an element
64 * @param array $options Options to control the element's display
65 * @param mixed $attributes Either a typical HTML attribute string or an associative array
67 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
69 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
70 $this->_persistantFreeze = true;
71 $this->_appendName = true;
72 $this->_type = 'date_selector';
73 // set the options, do not bother setting bogus ones
74 if (is_array($options)) {
75 foreach ($options as $name => $value) {
76 if (isset($this->_options[$name])) {
77 if (is_array($value) && is_array($this->_options[$name])) {
78 $this->_options[$name] = @array_merge($this->_options[$name], $value);
79 } else {
80 $this->_options[$name] = $value;
85 form_init_date_js();
88 /**
89 * This will create date group element constisting of day, month and year.
91 * @access private
93 function _createElements()
95 $this->_elements = array();
96 for ($i=1; $i<=31; $i++) {
97 $days[$i] = $i;
99 for ($i=1; $i<=12; $i++) {
100 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
102 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
103 $years[$i] = $i;
105 // E_STRICT creating elements without forms is nasty because it internally uses $this
106 $this->_elements[] = @MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
107 $this->_elements[] = @MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
108 $this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
109 // If optional we add a checkbox which the user can use to turn if on
110 if($this->_options['optional']) {
111 $this->_elements[] =@MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
113 foreach ($this->_elements as $element){
114 if (method_exists($element, 'setHiddenLabel')){
115 $element->setHiddenLabel(true);
122 * Called by HTML_QuickForm whenever form event is made on this element
124 * @param string $event Name of event
125 * @param mixed $arg event arguments
126 * @param object $caller calling object
127 * @return bool
129 function onQuickFormEvent($event, $arg, &$caller)
131 switch ($event) {
132 case 'updateValue':
133 // constant values override both default and submitted ones
134 // default values are overriden by submitted
135 $value = $this->_findValue($caller->_constantValues);
136 if (null === $value) {
137 // if no boxes were checked, then there is no value in the array
138 // yet we don't want to display default value in this case
139 if ($caller->isSubmitted()) {
140 $value = $this->_findValue($caller->_submitValues);
141 } else {
142 $value = $this->_findValue($caller->_defaultValues);
145 $requestvalue=$value;
146 if ($value == 0) {
147 $value = time();
149 if (!is_array($value)) {
150 $currentdate = usergetdate($value);
151 $value = array(
152 'day' => $currentdate['mday'],
153 'month' => $currentdate['mon'],
154 'year' => $currentdate['year']);
155 // If optional, default to off, unless a date was provided
156 if($this->_options['optional']) {
157 $value['enabled'] = $requestvalue != 0;
159 } else {
160 $value['enabled'] = isset($value['enabled']);
162 if (null !== $value){
163 $this->setValue($value);
165 break;
166 case 'createElement':
167 // Optional is an optional param, if its set we need to add a disabledIf rule.
168 // If its empty or not specified then its not an optional dateselector.
169 if (!empty($arg[2]['optional']) && !empty($arg[0])) {
170 $caller->disabledIf($arg[0], $arg[0].'[enabled]');
172 return parent::onQuickFormEvent($event, $arg, $caller);
173 break;
174 default:
175 return parent::onQuickFormEvent($event, $arg, $caller);
180 * Returns HTML for advchecbox form element.
182 * @return string
184 function toHtml()
186 include_once('HTML/QuickForm/Renderer/Default.php');
187 $renderer = new HTML_QuickForm_Renderer_Default();
188 $renderer->setElementTemplate('{element}');
189 parent::accept($renderer);
190 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
194 * Accepts a renderer
196 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
197 * @param bool $required Whether a group is required
198 * @param string $error An error message associated with a group
200 function accept(&$renderer, $required = false, $error = null)
202 $renderer->renderElement($this, $required, $error);
206 * Output a timestamp. Give it the name of the group.
208 * @param array $submitValues values submitted.
209 * @param bool $assoc specifies if returned array is associative
210 * @return array
212 function exportValue(&$submitValues, $assoc = false)
214 $value = null;
215 $valuearray = array();
216 foreach ($this->_elements as $element){
217 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
218 if ($thisexport!=null){
219 $valuearray += $thisexport;
222 if (count($valuearray)){
223 if($this->_options['optional']) {
224 // If checkbox is on, the value is zero, so go no further
225 if(empty($valuearray['enabled'])) {
226 $value[$this->getName()] = 0;
227 return $value;
231 $value[$this->getName()] = make_timestamp($valuearray['year'],
232 $valuearray['month'],
233 $valuearray['day'],
234 0, 0, 0,
235 $this->_options['timezone'],
236 $this->_options['applydst']);
238 return $value;
239 } else {
240 return null;