Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / form / datetimeselector.php
blob0f07b4fa1363a698dc0905b303fb99203c620738
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 $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);
101 // }}}
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
110 * @since 1.0
111 * @access public
112 * @return void
114 function onQuickFormEvent($event, $arg, &$caller)
116 switch ($event) {
117 case 'updateValue':
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);
126 } else {
127 $value = $this->_findValue($caller->_defaultValues);
130 $requestvalue=$value;
131 if ($value == 0) {
132 $value = time();
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'];
138 $value = array(
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;
148 } else {
149 $value['off'] = (isset($value['off'])) ? true : false;
151 if (null !== $value){
152 $this->setValue($value);
154 break;
155 case 'createElement':
156 if($arg[2]['optional']) {
157 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
159 return parent::onQuickFormEvent($event, $arg, $caller);
160 break;
161 default:
162 return parent::onQuickFormEvent($event, $arg, $caller);
166 // }}}
167 // {{{ toHtml()
169 function toHtml()
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];
178 // }}}
179 // {{{ accept()
181 function accept(&$renderer, $required = false, $error = null)
183 $renderer->renderElement($this, $required, $error);
186 // }}}
189 * Output a timestamp. Give it the name of the group.
191 * @param array $submitValues
192 * @param bool $assoc
193 * @return array
195 function exportValue(&$submitValues, $assoc = false)
197 $value = null;
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;
210 return $value;
213 $valuearray=$valuearray + array('year'=>1970, 'month'=>1, 'day'=>1, 'hour'=>0, 'minute'=>0);
214 $value[$this->getName()]=make_timestamp(
215 $valuearray['year'],
216 $valuearray['month'],
217 $valuearray['day'],
218 $valuearray['hour'],
219 $valuearray['minute'],
221 $this->_options['timezone'],
222 $this->_options['applydst']);
224 return $value;
225 } else {
227 return null;
231 // }}}