merged fix for MDL-7770, preventing users with no capability to view user profiles
[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 }
7f40a229 79 $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', null, $days, $this->getAttributes(), true);
7f42315c 80 $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', null, $months, $this->getAttributes(), true);
81 $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', null, $years, $this->getAttributes(), true);
de312b53 82 // If optional we add a checkbox (no text) which the user can use to turn if on
83 if($this->_options['optional']) {
84 $this->_elements[] =& MoodleQuickForm::createElement('checkbox','on', null, get_string('enable'), $this->getAttributes(), true);
85 }
e2294b98 86 $this->setValue();
da6f8763 87
88 }
89
90 // }}}
91 // {{{ setValue()
92
e2294b98 93 function setValue($value=0)
da6f8763 94 {
de312b53 95 $requestvalue=$value;
da6f8763 96 if (!($value)) {
97 $value = time();
98 }
99 if (!is_array($value)) {
100 $currentdate = usergetdate($value);
101 $value = array(
102 'day' => $currentdate['mday'],
103 'month' => $currentdate['mon'],
104 'year' => $currentdate['year']);
de312b53 105 // If optional, default to off, unless a date was provided
106 if($this->_options['optional']) {
107 $value['on'] = $requestvalue ? true : false;
108 }
da6f8763 109 }
110 parent::setValue($value);
111 }
112
113 // }}}
114 // {{{ toHtml()
115
116 function toHtml()
117 {
118 include_once('HTML/QuickForm/Renderer/Default.php');
119 $renderer =& new HTML_QuickForm_Renderer_Default();
120 $renderer->setElementTemplate('{element}');
121 parent::accept($renderer);
122 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
123 }
124
125 // }}}
126 // {{{ accept()
127
128 function accept(&$renderer, $required = false, $error = null)
129 {
130 $renderer->renderElement($this, $required, $error);
131 }
132
133 // }}}
134 // {{{ onQuickFormEvent()
135
136 function onQuickFormEvent($event, $arg, &$caller)
137 {
138 if ('updateValue' == $event) {
139 return HTML_QuickForm_element::onQuickFormEvent($event, $arg, $caller);
140 } else {
141 return parent::onQuickFormEvent($event, $arg, $caller);
142 }
143 }
144 /**
145 * Output a timestamp. Give it the name of the group.
146 *
147 * @param array $submitValues
148 * @param bool $assoc
149 * @return array
150 */
151 function exportValue(&$submitValues, $assoc = false)
152 {
153 $value = null;
57cf1be2 154 $valuearray = array();
155 foreach ($this->_elements as $element){
156 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
157 if ($thisexport!=null){
158 $valuearray += $thisexport;
159 }
160 }
161 if (count($valuearray)){
de312b53 162 if($this->_options['optional']) {
163 // If checkbox is not on, the value is zero, so go no further
164 if(empty($valuearray['on'])) {
165 $value[$this->getName()]=0;
166 return $value;
167 }
168 }
57cf1be2 169 $value[$this->getName()]=make_timestamp($valuearray['year'],
170 $valuearray['month'],
171 $valuearray['day'],
172 0,0,0,
173 $this->_options['timezone'],
174 $this->_options['applydst']);
175
176 return $value;
177 } else {
178 return null;
179 }
da6f8763 180 }
181
182 // }}}
183}
184?>