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 and time input element
21 * Contains class for a group of elements used to input a date and time.
24 * @copyright 2006 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 * Element used to input a date and time.
35 * Class for a group of elements used to input a date and time.
39 * @copyright 2006 Jamie Pratt <me@jamiep.org>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class MoodleQuickForm_date_time_selector
extends MoodleQuickForm_group
{
45 * Options for the element.
47 * startyear => int start of range of years that can be selected
48 * stopyear => int last year that can be selected
49 * defaulttime => default time value if the field is currently not set
50 * timezone => int|float|string (optional) timezone modifier used for edge case only.
51 * If not specified, then date is caclulated based on current user timezone.
52 * Note: dst will be calculated for string timezones only
53 * {@link http://docs.moodle.org/dev/Time_API#Timezone}
54 * step => step to increment minutes by
55 * optional => if true, show a checkbox beside the date to turn it on (or off)
58 protected $_options = array();
61 * @var array These complement separators, they are appended to the resultant HTML.
63 protected $_wrap = array('', '');
66 * @var null|bool Keeps track of whether the date selector was initialised using createElement
67 * or addElement. If true, createElement was used signifying the element has been
68 * added to a group - see MDL-39187.
70 protected $_usedcreateelement = true;
75 * @param string $elementName Element's name
76 * @param mixed $elementLabel Label(s) for an element
77 * @param array $options Options to control the element's display
78 * @param mixed $attributes Either a typical HTML attribute string or an associative array
80 function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
81 // Get the calendar type used - see MDL-18375.
82 $calendartype = \core_calendar\type_factory
::get_calendar_instance();
83 $this->_options
= array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(),
84 'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);
86 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
87 $this->_persistantFreeze
= true;
88 $this->_appendName
= true;
89 $this->_type
= 'date_time_selector';
90 // set the options, do not bother setting bogus ones
91 if (is_array($options)) {
92 foreach ($options as $name => $value) {
93 if (isset($this->_options
[$name])) {
94 if (is_array($value) && is_array($this->_options
[$name])) {
95 $this->_options
[$name] = @array_merge
($this->_options
[$name], $value);
97 $this->_options
[$name] = $value;
103 // The YUI2 calendar only supports the gregorian calendar type.
104 if ($calendartype->get_name() === 'gregorian') {
110 * This will create date group element constisting of day, month and year.
114 function _createElements() {
117 // Get the calendar type used - see MDL-18375.
118 $calendartype = \core_calendar\type_factory
::get_calendar_instance();
120 for ($i = 0; $i <= 23; $i++
) {
121 $hours[$i] = sprintf("%02d", $i);
123 for ($i = 0; $i < 60; $i +
= $this->_options
['step']) {
124 $minutes[$i] = sprintf("%02d", $i);
127 $this->_elements
= array();
128 $dateformat = $calendartype->get_date_order($this->_options
['startyear'], $this->_options
['stopyear']);
129 foreach ($dateformat as $key => $date) {
130 // E_STRICT creating elements without forms is nasty because it internally uses $this
131 $this->_elements
[] = @MoodleQuickForm
::createElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true);
133 if (right_to_left()) { // Switch order of elements for Right-to-Left
134 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
135 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
137 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
138 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
140 // The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
141 if ($calendartype->get_name() === 'gregorian') {
142 $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle');
143 $this->_elements
[] = @MoodleQuickForm
::createElement('link', 'calendar',
145 array('class' => 'visibleifjs'));
147 // If optional we add a checkbox which the user can use to turn if on
148 if ($this->_options
['optional']) {
149 $this->_elements
[] = @MoodleQuickForm
::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
151 foreach ($this->_elements
as $element){
152 if (method_exists($element, 'setHiddenLabel')){
153 $element->setHiddenLabel(true);
160 * Called by HTML_QuickForm whenever form event is made on this element
162 * @param string $event Name of event
163 * @param mixed $arg event arguments
164 * @param object $caller calling object
167 function onQuickFormEvent($event, $arg, &$caller) {
170 // Constant values override both default and submitted ones
171 // default values are overriden by submitted.
172 $value = $this->_findValue($caller->_constantValues
);
173 if (null === $value) {
174 // If no boxes were checked, then there is no value in the array
175 // yet we don't want to display default value in this case.
176 if ($caller->isSubmitted()) {
177 $value = $this->_findValue($caller->_submitValues
);
179 $value = $this->_findValue($caller->_defaultValues
);
182 $requestvalue=$value;
184 $value = $this->_options
['defaulttime'];
189 if (!is_array($value)) {
190 $calendartype = \core_calendar\type_factory
::get_calendar_instance();
191 $currentdate = $calendartype->timestamp_to_date_array($value, $this->_options
['timezone']);
192 // Round minutes to the previous multiple of step.
193 $currentdate['minutes'] -= $currentdate['minutes'] %
$this->_options
['step'];
195 'minute' => $currentdate['minutes'],
196 'hour' => $currentdate['hours'],
197 'day' => $currentdate['mday'],
198 'month' => $currentdate['mon'],
199 'year' => $currentdate['year']);
200 // If optional, default to off, unless a date was provided.
201 if ($this->_options
['optional']) {
202 $value['enabled'] = $requestvalue != 0;
205 $value['enabled'] = isset($value['enabled']);
207 if (null !== $value) {
208 $this->setValue($value);
211 case 'createElement':
212 if ($arg[2]['optional']) {
213 // When using the function addElement, rather than createElement, we still
214 // enter this case, making this check necessary.
215 if ($this->_usedcreateelement
) {
216 $caller->disabledIf($arg[0] . '[day]', $arg[0] . '[enabled]');
217 $caller->disabledIf($arg[0] . '[month]', $arg[0] . '[enabled]');
218 $caller->disabledIf($arg[0] . '[year]', $arg[0] . '[enabled]');
219 $caller->disabledIf($arg[0] . '[hour]', $arg[0] . '[enabled]');
220 $caller->disabledIf($arg[0] . '[minute]', $arg[0] . '[enabled]');
222 $caller->disabledIf($arg[0], $arg[0] . '[enabled]');
225 return parent
::onQuickFormEvent($event, $arg, $caller);
228 $this->_usedcreateelement
= false;
229 return parent
::onQuickFormEvent($event, $arg, $caller);
232 return parent
::onQuickFormEvent($event, $arg, $caller);
237 * Returns HTML for advchecbox form element.
242 include_once('HTML/QuickForm/Renderer/Default.php');
243 $renderer = new HTML_QuickForm_Renderer_Default();
244 $renderer->setElementTemplate('{element}');
245 parent
::accept($renderer);
247 $html = $this->_wrap
[0];
248 if ($this->_usedcreateelement
) {
249 $html .= html_writer
::tag('span', $renderer->toHtml(), array('class' => 'fdate_time_selector'));
251 $html .= $renderer->toHtml();
253 $html .= $this->_wrap
[1];
261 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
262 * @param bool $required Whether a group is required
263 * @param string $error An error message associated with a group
265 function accept(&$renderer, $required = false, $error = null) {
266 $renderer->renderElement($this, $required, $error);
270 * Output a timestamp. Give it the name of the group.
272 * @param array $submitValues values submitted.
273 * @param bool $assoc specifies if returned array is associative
276 function exportValue(&$submitValues, $assoc = false) {
278 $valuearray = array();
279 foreach ($this->_elements
as $element){
280 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
281 if ($thisexport!=null){
282 $valuearray +
= $thisexport;
285 if (count($valuearray)){
286 if($this->_options
['optional']) {
287 // If checkbox is on, the value is zero, so go no further
288 if(empty($valuearray['enabled'])) {
289 $value[$this->getName()] = 0;
293 // Get the calendar type used - see MDL-18375.
294 $calendartype = \core_calendar\type_factory
::get_calendar_instance();
295 $gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'],
296 $valuearray['month'],
299 $valuearray['minute']);
300 $value[$this->getName()] = make_timestamp($gregoriandate['year'],
301 $gregoriandate['month'],
302 $gregoriandate['day'],
303 $gregoriandate['hour'],
304 $gregoriandate['minute'],
306 $this->_options
['timezone'],