2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Group of date input element
21 * Contains class for a group of elements used to input a date.
24 * @copyright 2007 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once($CFG->libdir
. '/form/group.php');
30 require_once($CFG->libdir
. '/formslib.php');
33 * Class for a group of elements used to input a date.
35 * Emulates moodle print_date_selector function
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
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 => int|float|string (optional) timezone modifier used for edge case only.
49 * If not specified, then date is caclulated based on current user timezone.
50 * Note: dst will be calculated for string timezones only
51 * {@link http://docs.moodle.org/dev/Time_API#Timezone}
52 * optional => if true, show a checkbox beside the date to turn it on (or off)
55 protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
56 'timezone' => 99, 'optional' => false);
58 /** @var array These complement separators, they are appended to the resultant HTML */
59 protected $_wrap = array('', '');
64 * @param string $elementName Element's name
65 * @param mixed $elementLabel Label(s) for an element
66 * @param array $options Options to control the element's display
67 * @param mixed $attributes Either a typical HTML attribute string or an associative array
69 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
71 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
72 $this->_persistantFreeze
= true;
73 $this->_appendName
= true;
74 $this->_type
= 'date_selector';
75 // set the options, do not bother setting bogus ones
76 if (is_array($options)) {
77 foreach ($options as $name => $value) {
78 if (isset($this->_options
[$name])) {
79 if (is_array($value) && is_array($this->_options
[$name])) {
80 $this->_options
[$name] = @array_merge
($this->_options
[$name], $value);
82 $this->_options
[$name] = $value;
91 * This will create date group element constisting of day, month and year.
95 function _createElements()
97 $this->_elements
= array();
98 for ($i=1; $i<=31; $i++
) {
101 for ($i=1; $i<=12; $i++
) {
102 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
104 for ($i=$this->_options
['startyear']; $i<=$this->_options
['stopyear']; $i++
) {
107 // E_STRICT creating elements without forms is nasty because it internally uses $this
108 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
109 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
110 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
111 // If optional we add a checkbox which the user can use to turn if on
112 if($this->_options
['optional']) {
113 $this->_elements
[] =@MoodleQuickForm
::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
115 foreach ($this->_elements
as $element){
116 if (method_exists($element, 'setHiddenLabel')){
117 $element->setHiddenLabel(true);
124 * Called by HTML_QuickForm whenever form event is made on this element
126 * @param string $event Name of event
127 * @param mixed $arg event arguments
128 * @param object $caller calling object
131 function onQuickFormEvent($event, $arg, &$caller)
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
);
144 $value = $this->_findValue($caller->_defaultValues
);
147 $requestvalue=$value;
151 if (!is_array($value)) {
152 $currentdate = usergetdate($value, $this->_options
['timezone']);
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;
162 $value['enabled'] = isset($value['enabled']);
164 if (null !== $value){
165 $this->setValue($value);
168 case 'createElement':
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])) {
172 $caller->disabledIf($arg[0], $arg[0].'[enabled]');
174 return parent
::onQuickFormEvent($event, $arg, $caller);
177 return parent
::onQuickFormEvent($event, $arg, $caller);
182 * Returns HTML for advchecbox form element.
188 include_once('HTML/QuickForm/Renderer/Default.php');
189 $renderer = new HTML_QuickForm_Renderer_Default();
190 $renderer->setElementTemplate('{element}');
191 parent
::accept($renderer);
192 return $this->_wrap
[0] . $renderer->toHtml() . $this->_wrap
[1];
198 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
199 * @param bool $required Whether a group is required
200 * @param string $error An error message associated with a group
202 function accept(&$renderer, $required = false, $error = null)
204 $renderer->renderElement($this, $required, $error);
208 * Output a timestamp. Give it the name of the group.
210 * @param array $submitValues values submitted.
211 * @param bool $assoc specifies if returned array is associative
214 function exportValue(&$submitValues, $assoc = false)
217 $valuearray = array();
218 foreach ($this->_elements
as $element){
219 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
220 if ($thisexport!=null){
221 $valuearray +
= $thisexport;
224 if (count($valuearray)){
225 if($this->_options
['optional']) {
226 // If checkbox is on, the value is zero, so go no further
227 if(empty($valuearray['enabled'])) {
228 $value[$this->getName()] = 0;
233 $value[$this->getName()] = make_timestamp($valuearray['year'],
234 $valuearray['month'],
237 $this->_options
['timezone'],