da6f8763 |
1 | <?php |
2 | global $CFG; |
3 | require_once "$CFG->libdir/form/group.php"; |
4 | require_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 |
14 | class 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']) { |
11f260f4 |
84 | $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true); |
de312b53 |
85 | } |
da6f8763 |
86 | |
87 | } |
88 | |
89 | // }}} |
46e648b6 |
90 | // {{{ onQuickFormEvent() |
91 | |
11f260f4 |
92 | /** |
93 | * Called by HTML_QuickForm whenever form event is made on this element |
94 | * |
95 | * @param string $event Name of event |
96 | * @param mixed $arg event arguments |
97 | * @param object $caller calling object |
98 | * @since 1.0 |
99 | * @access public |
100 | * @return void |
101 | */ |
46e648b6 |
102 | function onQuickFormEvent($event, $arg, &$caller) |
103 | { |
11f260f4 |
104 | switch ($event) { |
105 | case 'updateValue': |
106 | // constant values override both default and submitted ones |
107 | // default values are overriden by submitted |
108 | $value = $this->_findValue($caller->_constantValues); |
109 | if (null === $value) { |
110 | // if no boxes were checked, then there is no value in the array |
111 | // yet we don't want to display default value in this case |
112 | if ($caller->isSubmitted()) { |
113 | $value = $this->_findValue($caller->_submitValues); |
114 | } else { |
115 | $value = $this->_findValue($caller->_defaultValues); |
116 | } |
117 | } |
118 | $requestvalue=$value; |
119 | if ($value == 0) { |
120 | $value = time(); |
121 | } |
122 | if (!is_array($value)) { |
123 | $currentdate = usergetdate($value); |
124 | $value = array( |
125 | 'day' => $currentdate['mday'], |
126 | 'month' => $currentdate['mon'], |
127 | 'year' => $currentdate['year']); |
128 | // If optional, default to off, unless a date was provided |
129 | if($this->_options['optional']) { |
130 | $value['off'] = ($requestvalue == 0) ? true : false; |
131 | } |
132 | } else { |
133 | $value['off'] = (isset($value['off'])) ? true : false; |
134 | } |
135 | if (null !== $value){ |
136 | $this->setValue($value); |
137 | } |
138 | break; |
139 | case 'createElement': |
140 | if($arg[2]['optional']) { |
141 | $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked'); |
142 | } |
143 | return parent::onQuickFormEvent($event, $arg, $caller); |
144 | break; |
145 | default: |
146 | return parent::onQuickFormEvent($event, $arg, $caller); |
da6f8763 |
147 | } |
11f260f4 |
148 | } // end func onQuickFormEvent |
da6f8763 |
149 | |
da6f8763 |
150 | // {{{ toHtml() |
151 | |
152 | function toHtml() |
153 | { |
154 | include_once('HTML/QuickForm/Renderer/Default.php'); |
155 | $renderer =& new HTML_QuickForm_Renderer_Default(); |
156 | $renderer->setElementTemplate('{element}'); |
157 | parent::accept($renderer); |
158 | return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1]; |
159 | } |
160 | |
161 | // }}} |
162 | // {{{ accept() |
163 | |
164 | function accept(&$renderer, $required = false, $error = null) |
165 | { |
166 | $renderer->renderElement($this, $required, $error); |
167 | } |
168 | |
169 | // }}} |
da6f8763 |
170 | |
da6f8763 |
171 | /** |
172 | * Output a timestamp. Give it the name of the group. |
173 | * |
174 | * @param array $submitValues |
175 | * @param bool $assoc |
176 | * @return array |
177 | */ |
178 | function exportValue(&$submitValues, $assoc = false) |
179 | { |
180 | $value = null; |
57cf1be2 |
181 | $valuearray = array(); |
182 | foreach ($this->_elements as $element){ |
183 | $thisexport = $element->exportValue($submitValues[$this->getName()], true); |
184 | if ($thisexport!=null){ |
185 | $valuearray += $thisexport; |
186 | } |
187 | } |
188 | if (count($valuearray)){ |
de312b53 |
189 | if($this->_options['optional']) { |
11f260f4 |
190 | // If checkbox is on, the value is zero, so go no further |
191 | if(!empty($valuearray['off'])) { |
de312b53 |
192 | $value[$this->getName()]=0; |
193 | return $value; |
194 | } |
195 | } |
11f260f4 |
196 | |
57cf1be2 |
197 | $value[$this->getName()]=make_timestamp($valuearray['year'], |
198 | $valuearray['month'], |
199 | $valuearray['day'], |
200 | 0,0,0, |
201 | $this->_options['timezone'], |
202 | $this->_options['applydst']); |
203 | |
204 | return $value; |
205 | } else { |
206 | return null; |
207 | } |
da6f8763 |
208 | } |
209 | |
210 | // }}} |
211 | } |
212 | ?> |