2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Duration form element
21 * Contains class to create length of time for element.
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once($CFG->libdir
. '/form/group.php');
30 require_once($CFG->libdir
. '/formslib.php');
31 require_once($CFG->libdir
. '/form/text.php');
36 * HTML class for a length of time. For example, 30 minutes of 4 days. The
37 * values returned to PHP is the duration in seconds.
41 * @copyright 2009 Tim Hunt
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class MoodleQuickForm_duration
extends MoodleQuickForm_group
{
46 * Control the fieldnames for form elements
47 * optional => if true, show a checkbox beside the element to turn it on (or off)
50 protected $_options = array('optional' => false, 'defaultunit' => 60);
52 /** @var array associative array of time units (days, hours, minutes, seconds) */
53 private $_units = null;
58 * @param string $elementName Element's name
59 * @param mixed $elementLabel Label(s) for an element
60 * @param array $options Options to control the element's display. Recognised values are
61 * 'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
62 * 'defaultunit' => 1|60|3600|86400|604800 - the default unit to display when the time is blank.
63 * If not specified, minutes is used.
64 * @param mixed $attributes Either a typical HTML attribute string or an associative array
66 function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
67 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
68 $this->_persistantFreeze
= true;
69 $this->_appendName
= true;
70 $this->_type
= 'duration';
72 // Set the options, do not bother setting bogus ones
73 if (!is_array($options)) {
76 $this->_options
['optional'] = !empty($options['optional']);
77 if (isset($options['defaultunit'])) {
78 if (!array_key_exists($options['defaultunit'], $this->get_units())) {
79 throw new coding_exception($options['defaultunit'] .
80 ' is not a recognised unit in MoodleQuickForm_duration.');
82 $this->_options
['defaultunit'] = $options['defaultunit'];
87 * Returns time associative array of unit length.
89 * @return array unit length in seconds => string unit name.
91 public function get_units() {
92 if (is_null($this->_units
)) {
93 $this->_units
= array(
94 604800 => get_string('weeks'),
95 86400 => get_string('days'),
96 3600 => get_string('hours'),
97 60 => get_string('minutes'),
98 1 => get_string('seconds'),
101 return $this->_units
;
105 * Converts seconds to the best possible time unit. for example
106 * 1800 -> array(30, 60) = 30 minutes.
108 * @param int $seconds an amout of time in seconds.
109 * @return array associative array ($number => $unit)
111 public function seconds_to_unit($seconds) {
113 return array(0, $this->_options
['defaultunit']);
115 foreach ($this->get_units() as $unit => $notused) {
116 if (fmod($seconds, $unit) == 0) {
117 return array($seconds / $unit, $unit);
120 return array($seconds, 1);
124 * Override of standard quickforms method to create this element.
126 function _createElements() {
127 $attributes = $this->getAttributes();
128 if (is_null($attributes)) {
129 $attributes = array();
131 if (!isset($attributes['size'])) {
132 $attributes['size'] = 3;
134 $this->_elements
= array();
135 // E_STRICT creating elements without forms is nasty because it internally uses $this
136 $this->_elements
[] = @MoodleQuickForm
::createElement('text', 'number', get_string('time', 'form'), $attributes, true);
137 unset($attributes['size']);
138 $this->_elements
[] = @MoodleQuickForm
::createElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true);
139 // If optional we add a checkbox which the user can use to turn if on
140 if($this->_options
['optional']) {
141 $this->_elements
[] = @MoodleQuickForm
::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
143 foreach ($this->_elements
as $element){
144 if (method_exists($element, 'setHiddenLabel')){
145 $element->setHiddenLabel(true);
151 * Called by HTML_QuickForm whenever form event is made on this element
153 * @param string $event Name of event
154 * @param mixed $arg event arguments
155 * @param object $caller calling object
158 function onQuickFormEvent($event, $arg, &$caller) {
161 // constant values override both default and submitted ones
162 // default values are overriden by submitted
163 $value = $this->_findValue($caller->_constantValues
);
164 if (null === $value) {
165 // if no boxes were checked, then there is no value in the array
166 // yet we don't want to display default value in this case
167 if ($caller->isSubmitted()) {
168 $value = $this->_findValue($caller->_submitValues
);
170 $value = $this->_findValue($caller->_defaultValues
);
173 if (!is_array($value)) {
174 list($number, $unit) = $this->seconds_to_unit($value);
175 $value = array('number' => $number, 'timeunit' => $unit);
176 // If optional, default to off, unless a date was provided
177 if ($this->_options
['optional']) {
178 $value['enabled'] = $number != 0;
181 $value['enabled'] = isset($value['enabled']);
183 if (null !== $value){
184 $this->setValue($value);
188 case 'createElement':
189 if ($arg[2]['optional']) {
190 $caller->disabledIf($arg[0], $arg[0] . '[enabled]');
192 $caller->setType($arg[0] . '[number]', PARAM_FLOAT
);
193 return parent
::onQuickFormEvent($event, $arg, $caller);
197 return parent
::onQuickFormEvent($event, $arg, $caller);
202 * Returns HTML for advchecbox form element.
207 include_once('HTML/QuickForm/Renderer/Default.php');
208 $renderer = new HTML_QuickForm_Renderer_Default();
209 $renderer->setElementTemplate('{element}');
210 parent
::accept($renderer);
211 return $renderer->toHtml();
217 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
218 * @param bool $required Whether a group is required
219 * @param string $error An error message associated with a group
221 function accept(&$renderer, $required = false, $error = null) {
222 $renderer->renderElement($this, $required, $error);
226 * Output a timestamp. Give it the name of the group.
227 * Override of standard quickforms method.
229 * @param array $submitValues
230 * @param bool $notused Not used.
231 * @return array field name => value. The value is the time interval in seconds.
233 function exportValue(&$submitValues, $notused = false) {
234 // Get the values from all the child elements.
235 $valuearray = array();
236 foreach ($this->_elements
as $element) {
237 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
238 if (!is_null($thisexport)) {
239 $valuearray +
= $thisexport;
243 // Convert the value to an integer number of seconds.
244 if (empty($valuearray)) {
247 if ($this->_options
['optional'] && empty($valuearray['enabled'])) {
248 return array($this->getName() => 0);
250 return array($this->getName() => $valuearray['number'] * $valuearray['timeunit']);