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 if (right_to_left()) { // Switch order of elements for Right-to-Left
88 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
89 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
91 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
92 $this->_elements
[] =& MoodleQuickForm
::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
94 // If optional we add a checkbox which the user can use to turn if on
95 if($this->_options
['optional']) {
96 $this->_elements
[] =& MoodleQuickForm
::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
98 foreach ($this->_elements
as $element){
99 if (method_exists($element, 'setHiddenLabel')){
100 $element->setHiddenLabel(true);
107 // {{{ onQuickFormEvent()
110 * Called by HTML_QuickForm whenever form event is made on this element
112 * @param string $event Name of event
113 * @param mixed $arg event arguments
114 * @param object $caller calling object
119 function onQuickFormEvent($event, $arg, &$caller)
123 // constant values override both default and submitted ones
124 // default values are overriden by submitted
125 $value = $this->_findValue($caller->_constantValues
);
126 if (null === $value) {
127 // if no boxes were checked, then there is no value in the array
128 // yet we don't want to display default value in this case
129 if ($caller->isSubmitted()) {
130 $value = $this->_findValue($caller->_submitValues
);
132 $value = $this->_findValue($caller->_defaultValues
);
135 $requestvalue=$value;
139 if (!is_array($value)) {
140 $currentdate = usergetdate($value);
141 // Round minutes to the previous multiple of step.
142 $currentdate['minutes'] -= $currentdate['minutes'] %
$this->_options
['step'];
144 'minute' => $currentdate['minutes'],
145 'hour' => $currentdate['hours'],
146 'day' => $currentdate['mday'],
147 'month' => $currentdate['mon'],
148 'year' => $currentdate['year']);
149 // If optional, default to off, unless a date was provided
150 if($this->_options
['optional']) {
151 $value['off'] = ($requestvalue == 0) ?
true : false;
154 $value['off'] = (isset($value['off'])) ?
true : false;
156 if (null !== $value){
157 $this->setValue($value);
160 case 'createElement':
161 if($arg[2]['optional']) {
162 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
164 return parent
::onQuickFormEvent($event, $arg, $caller);
167 return parent
::onQuickFormEvent($event, $arg, $caller);
176 include_once('HTML/QuickForm/Renderer/Default.php');
177 $renderer =& new HTML_QuickForm_Renderer_Default();
178 $renderer->setElementTemplate('{element}');
179 parent
::accept($renderer);
180 return $this->_wrap
[0] . $renderer->toHtml() . $this->_wrap
[1];
186 function accept(&$renderer, $required = false, $error = null)
188 $renderer->renderElement($this, $required, $error);
194 * Output a timestamp. Give it the name of the group.
196 * @param array $submitValues
200 function exportValue(&$submitValues, $assoc = false)
203 $valuearray = array();
204 foreach ($this->_elements
as $element){
205 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
206 if ($thisexport!=null){
207 $valuearray +
= $thisexport;
210 if (count($valuearray)){
211 if($this->_options
['optional']) {
212 // If checkbox is on, the value is zero, so go no further
213 if(!empty($valuearray['off'])) {
214 $value[$this->getName()]=0;
218 $valuearray=$valuearray +
array('year'=>1970, 'month'=>1, 'day'=>1, 'hour'=>0, 'minute'=>0);
219 $value[$this->getName()]=make_timestamp(
221 $valuearray['month'],
224 $valuearray['minute'],
226 $this->_options
['timezone'],
227 $this->_options
['applydst']);