Automatic installer.php lang files by installer_builder (20061209)
[moodle.git] / lib / form / dateselector.php
CommitLineData
da6f8763 1<?php
2global $CFG;
3require_once "$CFG->libdir/form/group.php";
4require_once "$CFG->libdir/formslib.php";
5
6/**
7 * Class for a group of elements used to input a date.
7f42315c 8 *
9 * Emulates moodle print_date_selector function
10 *
da6f8763 11 * @author Jamie Pratt <me@jamiep.org>
12 * @access public
13 */
7f40a229 14class MoodleQuickForm_date_selector extends MoodleQuickForm_group
da6f8763 15{
16 /**
17 * Control the fieldnames for form elements
18 *
e9d39a32 19 * startyear => integer start of range of years that can be selected
20 * stopyear => integer last year that can be selected
da6f8763 21 * timezone => float/string timezone
22 * applydst => apply users daylight savings adjustment?
de312b53 23 * optional => if true, show a checkbox beside the date to turn it on (or off)
da6f8763 24 */
25 var $_options = array('startyear'=>1970, 'stopyear'=>2020,
de312b53 26 'timezone'=>99, 'applydst'=>true, 'optional'=>false);
da6f8763 27
28 /**
29 * These complement separators, they are appended to the resultant HTML
30 * @access private
31 * @var array
32 */
33 var $_wrap = array('', '');
34
35 /**
36 * Class constructor
7f42315c 37 *
da6f8763 38 * @access public
39 * @param string Element's name
40 * @param mixed Label(s) for an element
41 * @param array Options to control the element's display
42 * @param mixed Either a typical HTML attribute string or an associative array
43 */
7f40a229 44 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
da6f8763 45 {
46 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
47 $this->_persistantFreeze = true;
48 $this->_appendName = true;
49 $this->_type = 'date_selector';
50 // set the options, do not bother setting bogus ones
51 if (is_array($options)) {
52 foreach ($options as $name => $value) {
53 if (isset($this->_options[$name])) {
54 if (is_array($value) && is_array($this->_options[$name])) {
55 $this->_options[$name] = @array_merge($this->_options[$name], $value);
56 } else {
57 $this->_options[$name] = $value;
58 }
59 }
60 }
61 }
62 }
63
64 // }}}
65 // {{{ _createElements()
66
67 function _createElements()
68 {
69 $this->_elements = array();
70 for ($i=1; $i<=31; $i++) {
71 $days[$i] = $i;
72 }
73 for ($i=1; $i<=12; $i++) {
74 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
75 }
76 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
77 $years[$i] = $i;
78 }
dbc6607b 79 $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
80 $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
81 $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
46e648b6 82 // If optional we add a checkbox which the user can use to turn if on
de312b53 83 if($this->_options['optional']) {
46e648b6 84 $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'on', null, get_string('enable'), $this->getAttributes(), true);
de312b53 85 }
e2294b98 86 $this->setValue();
da6f8763 87
88 }
89
90 // }}}
46e648b6 91 // {{{ onQuickFormEvent()
92
93 function onQuickFormEvent($event, $arg, &$caller)
94 {
95 if ('updateValue' == $event) {
96 return HTML_QuickForm_element::onQuickFormEvent($event, $arg, $caller);
97 } else {
98 return parent::onQuickFormEvent($event, $arg, $caller);
99 }
100 }
da6f8763 101 // {{{ setValue()
102
e2294b98 103 function setValue($value=0)
da6f8763 104 {
de312b53 105 $requestvalue=$value;
da6f8763 106 if (!($value)) {
107 $value = time();
108 }
109 if (!is_array($value)) {
110 $currentdate = usergetdate($value);
111 $value = array(
112 'day' => $currentdate['mday'],
113 'month' => $currentdate['mon'],
114 'year' => $currentdate['year']);
de312b53 115 // If optional, default to off, unless a date was provided
116 if($this->_options['optional']) {
117 $value['on'] = $requestvalue ? true : false;
118 }
da6f8763 119 }
120 parent::setValue($value);
121 }
122
123 // }}}
124 // {{{ toHtml()
125
126 function toHtml()
127 {
128 include_once('HTML/QuickForm/Renderer/Default.php');
129 $renderer =& new HTML_QuickForm_Renderer_Default();
130 $renderer->setElementTemplate('{element}');
131 parent::accept($renderer);
132 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
133 }
134
135 // }}}
136 // {{{ accept()
137
138 function accept(&$renderer, $required = false, $error = null)
139 {
140 $renderer->renderElement($this, $required, $error);
141 }
142
143 // }}}
da6f8763 144
da6f8763 145 /**
146 * Output a timestamp. Give it the name of the group.
147 *
148 * @param array $submitValues
149 * @param bool $assoc
150 * @return array
151 */
152 function exportValue(&$submitValues, $assoc = false)
153 {
154 $value = null;
57cf1be2 155 $valuearray = array();
156 foreach ($this->_elements as $element){
157 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
158 if ($thisexport!=null){
159 $valuearray += $thisexport;
160 }
161 }
162 if (count($valuearray)){
de312b53 163 if($this->_options['optional']) {
164 // If checkbox is not on, the value is zero, so go no further
165 if(empty($valuearray['on'])) {
166 $value[$this->getName()]=0;
167 return $value;
168 }
169 }
57cf1be2 170 $value[$this->getName()]=make_timestamp($valuearray['year'],
171 $valuearray['month'],
172 $valuearray['day'],
173 0,0,0,
174 $this->_options['timezone'],
175 $this->_options['applydst']);
176
177 return $value;
178 } else {
179 return null;
180 }
da6f8763 181 }
182
183 // }}}
184}
185?>