weekly release 2.3dev
[moodle.git] / lib / form / dateselector.php
CommitLineData
4a0e2e63 1<?php
25fe66b0 2
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///////////////////////////////////////////////////////////////////////////
25
da6f8763 26global $CFG;
25fe66b0 27require_once($CFG->libdir . '/form/group.php');
28require_once($CFG->libdir . '/formslib.php');
da6f8763 29
30/**
31 * Class for a group of elements used to input a date.
7f42315c 32 *
33 * Emulates moodle print_date_selector function
34 *
25fe66b0 35 * @package formslib
da6f8763 36 */
7f40a229 37class MoodleQuickForm_date_selector extends MoodleQuickForm_group
da6f8763 38{
39 /**
40 * Control the fieldnames for form elements
41 *
e9d39a32 42 * startyear => integer start of range of years that can be selected
43 * stopyear => integer last year that can be selected
da6f8763 44 * timezone => float/string timezone
45 * applydst => apply users daylight savings adjustment?
de312b53 46 * optional => if true, show a checkbox beside the date to turn it on (or off)
da6f8763 47 */
25fe66b0 48 protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
49 'timezone' => 99, 'applydst' => true, 'optional' => false);
da6f8763 50
51 /**
52 * These complement separators, they are appended to the resultant HTML
53 * @access private
54 * @var array
55 */
25fe66b0 56 protected $_wrap = array('', '');
da6f8763 57
58 /**
59 * Class constructor
7f42315c 60 *
da6f8763 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
66 */
7f40a229 67 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
da6f8763 68 {
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;
81 }
82 }
83 }
84 }
cf615522 85 form_init_date_js();
da6f8763 86 }
87
88 // }}}
89 // {{{ _createElements()
90
91 function _createElements()
92 {
93 $this->_elements = array();
94 for ($i=1; $i<=31; $i++) {
95 $days[$i] = $i;
96 }
97 for ($i=1; $i<=12; $i++) {
76ab1c33 98 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
da6f8763 99 }
100 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
101 $years[$i] = $i;
102 }
dbc6607b 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);
46e648b6 106 // If optional we add a checkbox which the user can use to turn if on
de312b53 107 if($this->_options['optional']) {
25fe66b0 108 $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
de312b53 109 }
44875d78 110 foreach ($this->_elements as $element){
111 if (method_exists($element, 'setHiddenLabel')){
112 $element->setHiddenLabel(true);
113 }
114 }
da6f8763 115
116 }
117
118 // }}}
46e648b6 119 // {{{ onQuickFormEvent()
120
11f260f4 121 /**
122 * Called by HTML_QuickForm whenever form event is made on this element
123 *
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
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)) {
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']) {
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 }
11f260f4 179 } // end func onQuickFormEvent
da6f8763 180
da6f8763 181 // {{{ toHtml()
182
183 function toHtml()
184 {
185 include_once('HTML/QuickForm/Renderer/Default.php');
bd69b9db 186 $renderer = new HTML_QuickForm_Renderer_Default();
da6f8763 187 $renderer->setElementTemplate('{element}');
188 parent::accept($renderer);
189 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
190 }
191
192 // }}}
193 // {{{ accept()
194
195 function accept(&$renderer, $required = false, $error = null)
196 {
197 $renderer->renderElement($this, $required, $error);
198 }
199
200 // }}}
da6f8763 201
da6f8763 202 /**
203 * Output a timestamp. Give it the name of the group.
204 *
205 * @param array $submitValues
206 * @param bool $assoc
207 * @return array
208 */
209 function exportValue(&$submitValues, $assoc = false)
210 {
211 $value = null;
57cf1be2 212 $valuearray = array();
213 foreach ($this->_elements as $element){
214 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
215 if ($thisexport!=null){
216 $valuearray += $thisexport;
217 }
218 }
219 if (count($valuearray)){
de312b53 220 if($this->_options['optional']) {
11f260f4 221 // If checkbox is on, the value is zero, so go no further
25fe66b0 222 if(empty($valuearray['enabled'])) {
223 $value[$this->getName()] = 0;
de312b53 224 return $value;
225 }
226 }
11f260f4 227
25fe66b0 228 $value[$this->getName()] = make_timestamp($valuearray['year'],
57cf1be2 229 $valuearray['month'],
230 $valuearray['day'],
25fe66b0 231 0, 0, 0,
57cf1be2 232 $this->_options['timezone'],
233 $this->_options['applydst']);
234
235 return $value;
236 } else {
237 return null;
238 }
da6f8763 239 }
240
241 // }}}
242}