oops, removed print_object that was left in from debugging
[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?
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
7f42315c 36 *
da6f8763 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 */
7f40a229 43 function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
da6f8763 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 }
7f40a229 78 $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', null, $days, $this->getAttributes(), true);
7f42315c 79 $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', null, $months, $this->getAttributes(), true);
80 $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', null, $years, $this->getAttributes(), true);
e2294b98 81 $this->setValue();
da6f8763 82
83 }
84
85 // }}}
86 // {{{ setValue()
87
e2294b98 88 function setValue($value=0)
da6f8763 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']);
7f42315c 99
da6f8763 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?>