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);
136 // Round minutes to the previous multiple of step.
137 $currentdate['minutes'] -= $currentdate['minutes'] %
$this->_options
['step'];
139 'minute' => $currentdate['minutes'],
140 'hour' => $currentdate['hours'],
141 'day' => $currentdate['mday'],
142 'month' => $currentdate['mon'],
143 'year' => $currentdate['year']);
144 // If optional, default to off, unless a date was provided
145 if($this->_options
['optional']) {
146 $value['off'] = ($requestvalue == 0) ?
true : false;
149 $value['off'] = (isset($value['off'])) ?
true : false;
151 if (null !== $value){
152 $this->setValue($value);
155 case 'createElement':
156 if($arg[2]['optional']) {
157 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
159 return parent
::onQuickFormEvent($event, $arg, $caller);
162 return parent
::onQuickFormEvent($event, $arg, $caller);
171 include_once('HTML/QuickForm/Renderer/Default.php');
172 $renderer =& new HTML_QuickForm_Renderer_Default();
173 $renderer->setElementTemplate('{element}');
174 parent
::accept($renderer);
175 return $this->_wrap
[0] . $renderer->toHtml() . $this->_wrap
[1];
181 function accept(&$renderer, $required = false, $error = null)
183 $renderer->renderElement($this, $required, $error);
189 * Output a timestamp. Give it the name of the group.
191 * @param array $submitValues
195 function exportValue(&$submitValues, $assoc = false)
198 $valuearray = array();
199 foreach ($this->_elements
as $element){
200 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
201 if ($thisexport!=null){
202 $valuearray +
= $thisexport;
205 if (count($valuearray)){
206 if($this->_options
['optional']) {
207 // If checkbox is on, the value is zero, so go no further
208 if(!empty($valuearray['off'])) {
209 $value[$this->getName()]=0;
213 $valuearray=$valuearray +
array('year'=>1970, 'month'=>1, 'day'=>1, 'hour'=>0, 'minute'=>0);
214 $value[$this->getName()]=make_timestamp(
216 $valuearray['month'],
219 $valuearray['minute'],
221 $this->_options
['timezone'],
222 $this->_options
['applydst']);