Merge branch 'm20_MDL-26672_aiccfix' of git://github.com/danmarsden/moodle
[moodle.git] / lib / form / dateselector.php
blob17e3db1f79c0460d2fff8a178f1a23183a5a33cd
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 global $CFG;
27 require_once($CFG->libdir . '/form/group.php');
28 require_once($CFG->libdir . '/formslib.php');
30 /**
31 * Class for a group of elements used to input a date.
33 * Emulates moodle print_date_selector function
35 * @package formslib
37 class MoodleQuickForm_date_selector extends MoodleQuickForm_group
39 /**
40 * Control the fieldnames for form elements
42 * startyear => integer start of range of years that can be selected
43 * stopyear => integer last year that can be selected
44 * timezone => float/string timezone
45 * applydst => apply users daylight savings adjustment?
46 * optional => if true, show a checkbox beside the date to turn it on (or off)
48 protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
49 'timezone' => 99, 'applydst' => true, 'optional' => false);
51 /**
52 * These complement separators, they are appended to the resultant HTML
53 * @access private
54 * @var array
56 protected $_wrap = array('', '');
58 /**
59 * Class constructor
61 * @access public
62 * @param string Element's name
63 * @param mixed Label(s) for an element
64 * @param array Options to control the element's display
65 * @param mixed 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 // {{{ _createElements()
91 function _createElements()
93 $this->_elements = array();
94 for ($i=1; $i<=31; $i++) {
95 $days[$i] = $i;
97 for ($i=1; $i<=12; $i++) {
98 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
100 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
101 $years[$i] = $i;
103 $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
104 $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
105 $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
106 // If optional we add a checkbox which the user can use to turn if on
107 if($this->_options['optional']) {
108 $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
110 foreach ($this->_elements as $element){
111 if (method_exists($element, 'setHiddenLabel')){
112 $element->setHiddenLabel(true);
118 // }}}
119 // {{{ onQuickFormEvent()
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 * @since 1.0
128 * @access public
129 * @return void
131 function onQuickFormEvent($event, $arg, &$caller)
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);
147 $requestvalue=$value;
148 if ($value == 0) {
149 $value = time();
151 if (!is_array($value)) {
152 $currentdate = usergetdate($value);
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']) {
159 $value['enabled'] = $requestvalue != 0;
161 } else {
162 $value['enabled'] = isset($value['enabled']);
164 if (null !== $value){
165 $this->setValue($value);
167 break;
168 case 'createElement':
169 if($arg[2]['optional']) {
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);
177 } // end func onQuickFormEvent
179 // {{{ toHtml()
181 function toHtml()
183 include_once('HTML/QuickForm/Renderer/Default.php');
184 $renderer = new HTML_QuickForm_Renderer_Default();
185 $renderer->setElementTemplate('{element}');
186 parent::accept($renderer);
187 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
190 // }}}
191 // {{{ accept()
193 function accept(&$renderer, $required = false, $error = null)
195 $renderer->renderElement($this, $required, $error);
198 // }}}
201 * Output a timestamp. Give it the name of the group.
203 * @param array $submitValues
204 * @param bool $assoc
205 * @return array
207 function exportValue(&$submitValues, $assoc = false)
209 $value = null;
210 $valuearray = array();
211 foreach ($this->_elements as $element){
212 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
213 if ($thisexport!=null){
214 $valuearray += $thisexport;
217 if (count($valuearray)){
218 if($this->_options['optional']) {
219 // If checkbox is on, the value is zero, so go no further
220 if(empty($valuearray['enabled'])) {
221 $value[$this->getName()] = 0;
222 return $value;
226 $value[$this->getName()] = make_timestamp($valuearray['year'],
227 $valuearray['month'],
228 $valuearray['day'],
229 0, 0, 0,
230 $this->_options['timezone'],
231 $this->_options['applydst']);
233 return $value;
234 } else {
235 return null;
239 // }}}