MDL-10689:
[moodle-linuxchix.git] / lib / form / datetimeselector.php
blob3e5aba75596c20978f08e7340021bf0b5cde1e88
1 <?php
2 global $CFG;
3 require_once "$CFG->libdir/form/group.php";
4 require_once "$CFG->libdir/formslib.php";
6 /**
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>
12 * @access public
14 class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
15 /**
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);
27 /**
28 * These complement separators, they are appended to the resultant HTML
29 * @access private
30 * @var array
32 var $_wrap = array('', '');
34 /**
35 * Class constructor
37 * @access public
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);
55 } else {
56 $this->_options[$name] = $value;
63 // }}}
64 // {{{ _createElements()
66 function _createElements()
68 $this->_elements = array();
69 for ($i=1; $i<=31; $i++) {
70 $days[$i] = $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++) {
76 $years[$i] = $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);
90 } else {
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);
106 // }}}
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
115 * @since 1.0
116 * @access public
117 * @return void
119 function onQuickFormEvent($event, $arg, &$caller)
121 switch ($event) {
122 case 'updateValue':
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);
131 } else {
132 $value = $this->_findValue($caller->_defaultValues);
135 $requestvalue=$value;
136 if ($value == 0) {
137 $value = time();
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'];
143 $value = array(
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;
153 } else {
154 $value['off'] = (isset($value['off'])) ? true : false;
156 if (null !== $value){
157 $this->setValue($value);
159 break;
160 case 'createElement':
161 if($arg[2]['optional']) {
162 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
164 return parent::onQuickFormEvent($event, $arg, $caller);
165 break;
166 default:
167 return parent::onQuickFormEvent($event, $arg, $caller);
171 // }}}
172 // {{{ toHtml()
174 function toHtml()
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];
183 // }}}
184 // {{{ accept()
186 function accept(&$renderer, $required = false, $error = null)
188 $renderer->renderElement($this, $required, $error);
191 // }}}
194 * Output a timestamp. Give it the name of the group.
196 * @param array $submitValues
197 * @param bool $assoc
198 * @return array
200 function exportValue(&$submitValues, $assoc = false)
202 $value = null;
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;
215 return $value;
218 $valuearray=$valuearray + array('year'=>1970, 'month'=>1, 'day'=>1, 'hour'=>0, 'minute'=>0);
219 $value[$this->getName()]=make_timestamp(
220 $valuearray['year'],
221 $valuearray['month'],
222 $valuearray['day'],
223 $valuearray['hour'],
224 $valuearray['minute'],
226 $this->_options['timezone'],
227 $this->_options['applydst']);
229 return $value;
230 } else {
232 return null;
236 // }}}