merged removed warning during restore
[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.
8 *
9 * Emulates moodle print_date_selector function
10 *
11 * @author Jamie Pratt <me@jamiep.org>
12 * @access public
13 */
14class moodleform_date_selector extends moodleform_group
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?
23 */
24 var $_options = array('startyear'=>1970, 'stopyear'=>2020,
25 'timezone'=>99, 'applydst'=>true);
26
27 /**
28 * These complement separators, they are appended to the resultant HTML
29 * @access private
30 * @var array
31 */
32 var $_wrap = array('', '');
33
34 /**
35 * Class constructor
36 *
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
42 */
43 function moodleform_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
44 {
45 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
46 $this->_persistantFreeze = true;
47 $this->_appendName = true;
48 $this->_type = 'date_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;
57 }
58 }
59 }
60 }
61 }
62
63 // }}}
64 // {{{ _createElements()
65
66 function _createElements()
67 {
68 $this->_elements = array();
69 for ($i=1; $i<=31; $i++) {
70 $days[$i] = $i;
71 }
72 for ($i=1; $i<=12; $i++) {
73 $months[$i] = userdate(gmmktime(12,0,0,$i,1,2000), "%B");
74 }
75 for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
76 $years[$i] = $i;
77 }
78 $this->_elements[] =& moodleform::createElement('select', 'day', null, $days, $this->getAttributes(), true);
79 $this->_elements[] =& moodleform::createElement('select','month', null, $months, $this->getAttributes(), true);
80 $this->_elements[] =& moodleform::createElement('select','year', null, $years, $this->getAttributes(), true);
81
82 }
83
84 // }}}
85 // {{{ setValue()
86
87 function setValue($value)
88 {
89 if (!($value)) {
90 $value = time();
91 }
92 if (!is_array($value)) {
93 $currentdate = usergetdate($value);
94 $value = array(
95 'day' => $currentdate['mday'],
96 'month' => $currentdate['mon'],
97 'year' => $currentdate['year']);
98
99 }
100 parent::setValue($value);
101 }
102
103 // }}}
104 // {{{ toHtml()
105
106 function toHtml()
107 {
108 include_once('HTML/QuickForm/Renderer/Default.php');
109 $renderer =& new HTML_QuickForm_Renderer_Default();
110 $renderer->setElementTemplate('{element}');
111 parent::accept($renderer);
112 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
113 }
114
115 // }}}
116 // {{{ accept()
117
118 function accept(&$renderer, $required = false, $error = null)
119 {
120 $renderer->renderElement($this, $required, $error);
121 }
122
123 // }}}
124 // {{{ onQuickFormEvent()
125
126 function onQuickFormEvent($event, $arg, &$caller)
127 {
128 if ('updateValue' == $event) {
129 return HTML_QuickForm_element::onQuickFormEvent($event, $arg, $caller);
130 } else {
131 return parent::onQuickFormEvent($event, $arg, $caller);
132 }
133 }
134 /**
135 * Output a timestamp. Give it the name of the group.
136 *
137 * @param array $submitValues
138 * @param bool $assoc
139 * @return array
140 */
141 function exportValue(&$submitValues, $assoc = false)
142 {
143 $value = null;
144 $valuearray = $this->_elements[0]->exportValue($submitValues[$this->getName()], true);
145 $valuearray +=$this->_elements[1]->exportValue($submitValues[$this->getName()], true);
146 $valuearray +=$this->_elements[2]->exportValue($submitValues[$this->getName()], true);
147 $value[$this->getName()]=make_timestamp($valuearray['year'],
148 $valuearray['month'],
149 $valuearray['day'],
150 0,0,0,
151 $this->_options['timezone'],
152 $this->_options['applydst']);
153 return $value;
154 }
155
156 // }}}
157}
158?>