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 public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
67 // TODO MDL-52313 Replace with the call to parent::__construct().
68 HTML_QuickForm_element
::__construct($elementName, $elementLabel, $attributes);
69 $this->_persistantFreeze
= true;
70 $this->_appendName
= true;
71 $this->_type
= 'duration';
73 // Set the options, do not bother setting bogus ones
74 if (!is_array($options)) {
77 $this->_options
['optional'] = !empty($options['optional']);
78 if (isset($options['defaultunit'])) {
79 if (!array_key_exists($options['defaultunit'], $this->get_units())) {
80 throw new coding_exception($options['defaultunit'] .
81 ' is not a recognised unit in MoodleQuickForm_duration.');
83 $this->_options
['defaultunit'] = $options['defaultunit'];
88 * Old syntax of class constructor. Deprecated in PHP7.
90 * @deprecated since Moodle 3.1
92 public function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
93 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
94 self
::__construct($elementName, $elementLabel, $options, $attributes);
98 * Returns time associative array of unit length.
100 * @return array unit length in seconds => string unit name.
102 public function get_units() {
103 if (is_null($this->_units
)) {
104 $this->_units
= array(
105 604800 => get_string('weeks'),
106 86400 => get_string('days'),
107 3600 => get_string('hours'),
108 60 => get_string('minutes'),
109 1 => get_string('seconds'),
112 return $this->_units
;
116 * Converts seconds to the best possible time unit. for example
117 * 1800 -> array(30, 60) = 30 minutes.
119 * @param int $seconds an amout of time in seconds.
120 * @return array associative array ($number => $unit)
122 public function seconds_to_unit($seconds) {
124 return array(0, $this->_options
['defaultunit']);
126 foreach ($this->get_units() as $unit => $notused) {
127 if (fmod($seconds, $unit) == 0) {
128 return array($seconds / $unit, $unit);
131 return array($seconds, 1);
135 * Override of standard quickforms method to create this element.
137 function _createElements() {
138 $attributes = $this->getAttributes();
139 if (is_null($attributes)) {
140 $attributes = array();
142 if (!isset($attributes['size'])) {
143 $attributes['size'] = 3;
145 $this->_elements
= array();
146 // E_STRICT creating elements without forms is nasty because it internally uses $this
147 $number = $this->createFormElement('text', 'number', get_string('time', 'form'), $attributes, true);
148 $number->set_force_ltr(true);
149 $this->_elements
[] = $number;
150 unset($attributes['size']);
151 $this->_elements
[] = $this->createFormElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true);
152 // If optional we add a checkbox which the user can use to turn if on
153 if($this->_options
['optional']) {
154 $this->_elements
[] = $this->createFormElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
156 foreach ($this->_elements
as $element){
157 if (method_exists($element, 'setHiddenLabel')){
158 $element->setHiddenLabel(true);
164 * Called by HTML_QuickForm whenever form event is made on this element
166 * @param string $event Name of event
167 * @param mixed $arg event arguments
168 * @param object $caller calling object
171 function onQuickFormEvent($event, $arg, &$caller) {
172 $this->setMoodleForm($caller);
175 // constant values override both default and submitted ones
176 // default values are overriden by submitted
177 $value = $this->_findValue($caller->_constantValues
);
178 if (null === $value) {
179 // if no boxes were checked, then there is no value in the array
180 // yet we don't want to display default value in this case
181 if ($caller->isSubmitted()) {
182 $value = $this->_findValue($caller->_submitValues
);
184 $value = $this->_findValue($caller->_defaultValues
);
187 if (!is_array($value)) {
188 list($number, $unit) = $this->seconds_to_unit($value);
189 $value = array('number' => $number, 'timeunit' => $unit);
190 // If optional, default to off, unless a date was provided
191 if ($this->_options
['optional']) {
192 $value['enabled'] = $number != 0;
195 $value['enabled'] = isset($value['enabled']);
197 if (null !== $value){
198 $this->setValue($value);
202 case 'createElement':
203 if ($arg[2]['optional']) {
204 $caller->disabledIf($arg[0], $arg[0] . '[enabled]');
206 $caller->setType($arg[0] . '[number]', PARAM_FLOAT
);
207 return parent
::onQuickFormEvent($event, $arg, $caller);
211 return parent
::onQuickFormEvent($event, $arg, $caller);
216 * Returns HTML for advchecbox form element.
221 include_once('HTML/QuickForm/Renderer/Default.php');
222 $renderer = new HTML_QuickForm_Renderer_Default();
223 $renderer->setElementTemplate('{element}');
224 parent
::accept($renderer);
225 return $renderer->toHtml();
231 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
232 * @param bool $required Whether a group is required
233 * @param string $error An error message associated with a group
235 function accept(&$renderer, $required = false, $error = null) {
236 $renderer->renderElement($this, $required, $error);
240 * Output a timestamp. Give it the name of the group.
241 * Override of standard quickforms method.
243 * @param array $submitValues
244 * @param bool $notused Not used.
245 * @return array field name => value. The value is the time interval in seconds.
247 function exportValue(&$submitValues, $notused = false) {
248 // Get the values from all the child elements.
249 $valuearray = array();
250 foreach ($this->_elements
as $element) {
251 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
252 if (!is_null($thisexport)) {
253 $valuearray +
= $thisexport;
257 // Convert the value to an integer number of seconds.
258 if (empty($valuearray)) {
261 if ($this->_options
['optional'] && empty($valuearray['enabled'])) {
262 return array($this->getName() => 0);
264 return array($this->getName() => $valuearray['number'] * $valuearray['timeunit']);