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.
9 * Emulates moodle print_date_selector function
11 * @author Jamie Pratt <me@jamiep.org>
14 class MoodleQuickForm_date_selector
extends MoodleQuickForm_group
17 * Control the fieldnames for form elements
19 * startyear => integer start of range of years that can be selected
20 * stopyear => integer last year that can be selected
21 * timezone => float/string timezone
22 * applydst => apply users daylight savings adjustment?
23 * optional => if true, show a checkbox beside the date to turn it on (or off)
25 var $_options = array('startyear'=>1970, 'stopyear'=>2020,
26 'timezone'=>99, 'applydst'=>true, 'optional'=>false);
29 * These complement separators, they are appended to the resultant HTML
33 var $_wrap = array('', '');
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
44 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
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);
57 $this->_options
[$name] = $value;
65 // {{{ _createElements()
67 function _createElements()
69 $this->_elements
= array();
70 for ($i=1; $i<=31; $i++
) {
73 for ($i=1; $i<=12; $i++
) {
74 $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
76 for ($i=$this->_options
['startyear']; $i<=$this->_options
['stopyear']; $i++
) {
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);
82 // If optional we add a checkbox which the user can use to turn if on
83 if($this->_options
['optional']) {
84 $this->_elements
[] =& MoodleQuickForm
::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
86 foreach ($this->_elements
as $element){
87 if (method_exists($element, 'setHiddenLabel')){
88 $element->setHiddenLabel(true);
95 // {{{ onQuickFormEvent()
98 * Called by HTML_QuickForm whenever form event is made on this element
100 * @param string $event Name of event
101 * @param mixed $arg event arguments
102 * @param object $caller calling object
107 function onQuickFormEvent($event, $arg, &$caller)
111 // constant values override both default and submitted ones
112 // default values are overriden by submitted
113 $value = $this->_findValue($caller->_constantValues
);
114 if (null === $value) {
115 // if no boxes were checked, then there is no value in the array
116 // yet we don't want to display default value in this case
117 if ($caller->isSubmitted()) {
118 $value = $this->_findValue($caller->_submitValues
);
120 $value = $this->_findValue($caller->_defaultValues
);
123 $requestvalue=$value;
127 if (!is_array($value)) {
128 $currentdate = usergetdate($value);
130 'day' => $currentdate['mday'],
131 'month' => $currentdate['mon'],
132 'year' => $currentdate['year']);
133 // If optional, default to off, unless a date was provided
134 if($this->_options
['optional']) {
135 $value['off'] = ($requestvalue == 0) ?
true : false;
138 $value['off'] = (isset($value['off'])) ?
true : false;
140 if (null !== $value){
141 $this->setValue($value);
144 case 'createElement':
145 if($arg[2]['optional']) {
146 $caller->disabledIf($arg[0], $arg[0].'[off]', 'checked');
148 return parent
::onQuickFormEvent($event, $arg, $caller);
151 return parent
::onQuickFormEvent($event, $arg, $caller);
153 } // end func onQuickFormEvent
159 include_once('HTML/QuickForm/Renderer/Default.php');
160 $renderer =& new HTML_QuickForm_Renderer_Default();
161 $renderer->setElementTemplate('{element}');
162 parent
::accept($renderer);
163 return $this->_wrap
[0] . $renderer->toHtml() . $this->_wrap
[1];
169 function accept(&$renderer, $required = false, $error = null)
171 $renderer->renderElement($this, $required, $error);
177 * Output a timestamp. Give it the name of the group.
179 * @param array $submitValues
183 function exportValue(&$submitValues, $assoc = false)
186 $valuearray = array();
187 foreach ($this->_elements
as $element){
188 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
189 if ($thisexport!=null){
190 $valuearray +
= $thisexport;
193 if (count($valuearray)){
194 if($this->_options
['optional']) {
195 // If checkbox is on, the value is zero, so go no further
196 if(!empty($valuearray['off'])) {
197 $value[$this->getName()]=0;
202 $value[$this->getName()]=make_timestamp($valuearray['year'],
203 $valuearray['month'],
206 $this->_options
['timezone'],
207 $this->_options
['applydst']);