weekly release 2.3dev
[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
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
52 */
25fe66b0 53 protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
54 'timezone' => 99, 'applydst' => true, 'optional' => false);
da6f8763 55
6c1fd304 56 /** @var array These complement separators, they are appended to the resultant HTML */
25fe66b0 57 protected $_wrap = array('', '');
da6f8763 58
6c1fd304
RT
59 /**
60 * constructor
61 *
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
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
6c1fd304
RT
88 /**
89 * This will create date group element constisting of day, month and year.
90 *
91 * @access private
92 */
da6f8763 93 function _createElements()
94 {
95 $this->_elements = array();
96 for ($i=1; $i<=31; $i++) {
97 $days[$i] = $i;
98 }
99 for ($i=1; $i<=12; $i++) {
76ab1c33 100 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
da6f8763 101 }
102 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
103 $years[$i] = $i;
104 }
fabbf439
PS
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);
46e648b6 109 // If optional we add a checkbox which the user can use to turn if on
de312b53 110 if($this->_options['optional']) {
e2bb3c92 111 $this->_elements[] =@MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
de312b53 112 }
44875d78 113 foreach ($this->_elements as $element){
114 if (method_exists($element, 'setHiddenLabel')){
115 $element->setHiddenLabel(true);
116 }
117 }
da6f8763 118
119 }
120
11f260f4 121 /**
122 * Called by HTML_QuickForm whenever form event is made on this element
58b7d48f 123 *
6c1fd304
RT
124 * @param string $event Name of event
125 * @param mixed $arg event arguments
126 * @param object $caller calling object
127 * @return bool
11f260f4 128 */
46e648b6 129 function onQuickFormEvent($event, $arg, &$caller)
130 {
11f260f4 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);
143 }
144 }
145 $requestvalue=$value;
146 if ($value == 0) {
147 $value = time();
148 }
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']) {
25fe66b0 157 $value['enabled'] = $requestvalue != 0;
11f260f4 158 }
159 } else {
25fe66b0 160 $value['enabled'] = isset($value['enabled']);
11f260f4 161 }
162 if (null !== $value){
163 $this->setValue($value);
164 }
165 break;
166 case 'createElement':
fec617de
SH
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])) {
25fe66b0 170 $caller->disabledIf($arg[0], $arg[0].'[enabled]');
11f260f4 171 }
172 return parent::onQuickFormEvent($event, $arg, $caller);
173 break;
174 default:
175 return parent::onQuickFormEvent($event, $arg, $caller);
da6f8763 176 }
6c1fd304 177 }
da6f8763 178
6c1fd304
RT
179 /**
180 * Returns HTML for advchecbox form element.
181 *
182 * @return string
183 */
da6f8763 184 function toHtml()
185 {
186 include_once('HTML/QuickForm/Renderer/Default.php');
bd69b9db 187 $renderer = new HTML_QuickForm_Renderer_Default();
da6f8763 188 $renderer->setElementTemplate('{element}');
189 parent::accept($renderer);
190 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
191 }
192
6c1fd304
RT
193 /**
194 * Accepts a renderer
195 *
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
199 */
da6f8763 200 function accept(&$renderer, $required = false, $error = null)
201 {
202 $renderer->renderElement($this, $required, $error);
203 }
204
da6f8763 205 /**
206 * Output a timestamp. Give it the name of the group.
207 *
6c1fd304
RT
208 * @param array $submitValues values submitted.
209 * @param bool $assoc specifies if returned array is associative
da6f8763 210 * @return array
211 */
212 function exportValue(&$submitValues, $assoc = false)
213 {
214 $value = null;
57cf1be2 215 $valuearray = array();
216 foreach ($this->_elements as $element){
217 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
218 if ($thisexport!=null){
219 $valuearray += $thisexport;
220 }
221 }
222 if (count($valuearray)){
de312b53 223 if($this->_options['optional']) {
11f260f4 224 // If checkbox is on, the value is zero, so go no further
25fe66b0 225 if(empty($valuearray['enabled'])) {
226 $value[$this->getName()] = 0;
de312b53 227 return $value;
228 }
229 }
11f260f4 230
25fe66b0 231 $value[$this->getName()] = make_timestamp($valuearray['year'],
57cf1be2 232 $valuearray['month'],
233 $valuearray['day'],
25fe66b0 234 0, 0, 0,
57cf1be2 235 $this->_options['timezone'],
236 $this->_options['applydst']);
237
238 return $value;
239 } else {
240 return null;
241 }
da6f8763 242 }
da6f8763 243}