3 require_once "$CFG->libdir/form/group.php";
4 require_once "$CFG->libdir/formslib.php";
7 * Class for a group of elements used to input a date and time.
9 * Emulates moodle print_date_selector function and also allows you to select a time.
11 * @author Jamie Pratt <me@jamiep.org>
14 class MoodleQuickForm_date_time_selector
extends MoodleQuickForm_group
{
16 * Options for the element
18 * startyear => integer start of range of years that can be selected
19 * stopyear => integer last year that can be selected
20 * timezone => float/string timezone
21 * applydst => apply users daylight savings adjustment?
22 * step => step to increment minutes by
24 var $_options = array('startyear'=>1970, 'stopyear'=>2020,
25 'timezone'=>99, 'applydst'=>true, 'step'=>5, 'optional'=>false);
28 * These complement separators, they are appended to the resultant HTML
32 var $_wrap = array('', '');
38 * @param string Element's name
39 * @param mixed Label(s) for an element
40 * @param array Options to control the element's display
41 * @param mixed Either a typical HTML attribute string or an associative array
43 function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
45 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
46 $this->_persistantFreeze
= true;
47 $this->_appendName
= true;
48 $this->_type
= 'date_time_selector';
49 // set the options, do not bother setting bogus ones
50 if (is_array($options)) {
51 foreach ($options as $name => $value) {
52 if (isset($this->_options
[$name])) {
53 if (is_array($value) && is_array($this->_options
[$name])) {
54 $this->_options
[$name] = @array_merge
($this->_options
[$name], $value);
56 $this->_options
[$name] = $value;
64 // {{{ _createElements()
66 function _createElements()
68 $this->_elements
= array();
69 for ($i=1; $i<=31; $i++
) {
72 for ($i=1; $i<=12; $i++
) {
73 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
75 for ($i=$this->_options
['startyear']; $i<=$this->_options
['stopyear']; $i++
) {
78 for ($i=0; $i<=23; $i++
) {
79 $hours[$i] = sprintf("%02d",$i);
81 for ($i=0; $i<60; $i+
=$this->_options
['step']) {
82 $minutes[$i] = sprintf("%02d",$i);
84 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
85 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
86 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
87 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
88 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
89 // If optional we add a checkbox which the user can use to turn if on
90 if($this->_options
['optional']) {
91 $this->_elements
[] =& MoodleQuickForm
::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
93 foreach ($this->_elements
as $element){
94 if (method_exists($element, 'setHiddenLabel')){
95 $element->setHiddenLabel(true);
102 // {{{ onQuickFormEvent()
105 * Called by HTML_QuickForm whenever form event is made on this element
107 * @param string $event Name of event
108 * @param mixed $arg event arguments
109 * @param object $caller calling object
114 function onQuickFormEvent($event, $arg, &$caller)
118 // constant values override both default and submitted ones
119 // default values are overriden by submitted
120 $value = $this->_findValue($caller->_constantValues
);
121 if (null === $value) {
122 // if no boxes were checked, then there is no value in the array
123 // yet we don't want to display default value in this case
124 if ($caller->isSubmitted()) {
125 $value = $this->_findValue($caller->_submitValues
);
127 $value = $this->_findValue($caller->_defaultValues
);
130 $requestvalue=$value;
134 if (!is_array($value)) {
135 $currentdate = usergetdate($value);
137 'minute' => $currentdate['minutes'],
138 'hour' => $currentdate['hours'],
139 'day' => $currentdate['mday'],
140 'month' => $currentdate['mon'],
141 'year' => $currentdate['year']);
142 // If optional, default to off, unless a date was provided
143 if($this->_options
['optional']) {
144 $value['off'] = ($requestvalue == 0) ?
true : false;
147 $value['off'] = (isset($value['off'])) ?
true : false;
149 if (null !== $value){
150 $this->setValue($value);
153 case 'createElement':
154 if($arg[2]['optional']) {
155 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
157 return parent
::onQuickFormEvent($event, $arg, $caller);
160 return parent
::onQuickFormEvent($event, $arg, $caller);
169 include_once('HTML/QuickForm/Renderer/Default.php');
170 $renderer =& new HTML_QuickForm_Renderer_Default();
171 $renderer->setElementTemplate('{element}');
172 parent
::accept($renderer);
173 return $this->_wrap
[0] . $renderer->toHtml() . $this->_wrap
[1];
179 function accept(&$renderer, $required = false, $error = null)
181 $renderer->renderElement($this, $required, $error);
187 * Output a timestamp. Give it the name of the group.
189 * @param array $submitValues
193 function exportValue(&$submitValues, $assoc = false)
196 $valuearray = array();
197 foreach ($this->_elements
as $element){
198 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
199 if ($thisexport!=null){
200 $valuearray +
= $thisexport;
203 if (count($valuearray)){
204 if($this->_options
['optional']) {
205 // If checkbox is on, the value is zero, so go no further
206 if(!empty($valuearray['off'])) {
207 $value[$this->getName()]=0;
211 $valuearray=$valuearray +
array('year'=>1970, 'month'=>1, 'day'=>1, 'hour'=>0, 'minute'=>0);
212 $value[$this->getName()]=make_timestamp(
214 $valuearray['month'],
217 $valuearray['minute'],
219 $this->_options
['timezone'],
220 $this->_options
['applydst']);