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. |
8 | * |
9 | * Emulates moodle print_date_selector function |
10 | * |
11 | * @author Jamie Pratt <me@jamiep.org> |
12 | * @access public |
13 | */ |
14 | class moodleform_date_selector extends moodleform_group |
15 | { |
16 | /** |
17 | * Control the fieldnames for form elements |
18 | * |
19 | * day => string day fieldname |
20 | * month => string month fieldname |
21 | * year => string year fieldname |
22 | * timezone => float/string timezone |
23 | * applydst => apply users daylight savings adjustment? |
24 | */ |
25 | var $_options = array('startyear'=>1970, 'stopyear'=>2020, |
26 | 'timezone'=>99, 'applydst'=>true); |
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 |
37 | * |
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 | */ |
44 | function moodleform_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null) |
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 | } |
79 | $this->_elements[] =& moodleform::createElement('select', 'day', null, $days, $this->getAttributes(), true); |
80 | $this->_elements[] =& moodleform::createElement('select','month', null, $months, $this->getAttributes(), true); |
81 | $this->_elements[] =& moodleform::createElement('select','year', null, $years, $this->getAttributes(), true); |
82 | |
83 | } |
84 | |
85 | // }}} |
86 | // {{{ setValue() |
87 | |
88 | function setValue($value) |
89 | { |
90 | if (!($value)) { |
91 | $value = time(); |
92 | } |
93 | if (!is_array($value)) { |
94 | $currentdate = usergetdate($value); |
95 | $value = array( |
96 | 'day' => $currentdate['mday'], |
97 | 'month' => $currentdate['mon'], |
98 | 'year' => $currentdate['year']); |
99 | |
100 | } |
101 | parent::setValue($value); |
102 | } |
103 | |
104 | // }}} |
105 | // {{{ toHtml() |
106 | |
107 | function toHtml() |
108 | { |
109 | include_once('HTML/QuickForm/Renderer/Default.php'); |
110 | $renderer =& new HTML_QuickForm_Renderer_Default(); |
111 | $renderer->setElementTemplate('{element}'); |
112 | parent::accept($renderer); |
113 | return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1]; |
114 | } |
115 | |
116 | // }}} |
117 | // {{{ accept() |
118 | |
119 | function accept(&$renderer, $required = false, $error = null) |
120 | { |
121 | $renderer->renderElement($this, $required, $error); |
122 | } |
123 | |
124 | // }}} |
125 | // {{{ onQuickFormEvent() |
126 | |
127 | function onQuickFormEvent($event, $arg, &$caller) |
128 | { |
129 | if ('updateValue' == $event) { |
130 | return HTML_QuickForm_element::onQuickFormEvent($event, $arg, $caller); |
131 | } else { |
132 | return parent::onQuickFormEvent($event, $arg, $caller); |
133 | } |
134 | } |
135 | /** |
136 | * Output a timestamp. Give it the name of the group. |
137 | * |
138 | * @param array $submitValues |
139 | * @param bool $assoc |
140 | * @return array |
141 | */ |
142 | function exportValue(&$submitValues, $assoc = false) |
143 | { |
144 | $value = null; |
145 | $valuearray = $this->_elements[0]->exportValue($submitValues[$this->getName()], true); |
146 | $valuearray +=$this->_elements[1]->exportValue($submitValues[$this->getName()], true); |
147 | $valuearray +=$this->_elements[2]->exportValue($submitValues[$this->getName()], true); |
148 | $value[$this->getName()]=make_timestamp($valuearray['year'], |
149 | $valuearray['month'], |
150 | $valuearray['day'], |
151 | 0,0,0, |
152 | $this->_options['timezone'], |
153 | $this->_options['applydst']); |
154 | return $value; |
155 | } |
156 | |
157 | // }}} |
158 | } |
159 | ?> |