3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 require_once($CFG->libdir
. '/form/group.php');
28 require_once($CFG->libdir
. '/formslib.php');
31 * Class for a group of elements used to input a date.
33 * Emulates moodle print_date_selector function
37 class MoodleQuickForm_date_selector
extends MoodleQuickForm_group
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);
52 * These complement separators, they are appended to the resultant HTML
56 protected $_wrap = array('', '');
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);
80 $this->_options
[$name] = $value;
89 // {{{ _createElements()
91 function _createElements()
93 $this->_elements
= array();
94 for ($i=1; $i<=31; $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++
) {
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);
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
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);
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 if($arg[2]['optional']) {
170 $caller->disabledIf($arg[0], $arg[0].'[enabled]');
172 return parent
::onQuickFormEvent($event, $arg, $caller);
175 return parent
::onQuickFormEvent($event, $arg, $caller);
177 } // end func onQuickFormEvent
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];
193 function accept(&$renderer, $required = false, $error = null)
195 $renderer->renderElement($this, $required, $error);
201 * Output a timestamp. Give it the name of the group.
203 * @param array $submitValues
207 function exportValue(&$submitValues, $assoc = false)
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;
226 $value[$this->getName()] = make_timestamp($valuearray['year'],
227 $valuearray['month'],
230 $this->_options
['timezone'],
231 $this->_options
['applydst']);