| Línea 34... |
Línea 34... |
| 34 |
* Duration element
|
34 |
* Duration element
|
| 35 |
*
|
35 |
*
|
| 36 |
* HTML class for a length of time. For example, 30 minutes of 4 days. The
|
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 (an int rounded to the nearest second).
|
37 |
* values returned to PHP is the duration in seconds (an int rounded to the nearest second).
|
| 38 |
*
|
38 |
*
|
| - |
|
39 |
* By default, only durations >= 0 can be input. If you want to allow negative
|
| - |
|
40 |
* durations set the option allownegative.
|
| - |
|
41 |
*
|
| 39 |
* @package core_form
|
42 |
* @package core_form
|
| 40 |
* @category form
|
43 |
* @category form
|
| 41 |
* @copyright 2009 Tim Hunt
|
44 |
* @copyright 2009 Tim Hunt
|
| 42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
45 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 43 |
*/
|
46 |
*/
|
| 44 |
class MoodleQuickForm_duration extends MoodleQuickForm_group {
|
47 |
class MoodleQuickForm_duration extends MoodleQuickForm_group {
|
| 45 |
/**
|
48 |
/**
|
| 46 |
* Control the field names for form elements
|
49 |
* Control the field names for form elements
|
| 47 |
* optional => if true, show a checkbox beside the element to turn it on (or off)
|
50 |
* optional => if true, show a checkbox beside the element to turn it on (or off)
|
| 48 |
* defaultunit => which unit is default when the form is blank (default Minutes).
|
51 |
* defaultunit => which unit is default when the form is blank (default Minutes).
|
| - |
|
52 |
* allownegative => are durations < 0 allowed? (default false)
|
| 49 |
* @var array
|
53 |
* @var array
|
| 50 |
*/
|
54 |
*/
|
| 51 |
protected $_options = ['optional' => false, 'defaultunit' => MINSECS];
|
55 |
protected $_options = ['optional' => false, 'defaultunit' => MINSECS, 'allownegative' => false];
|
| Línea 52... |
Línea 56... |
| 52 |
|
56 |
|
| 53 |
/** @var array associative array of time units (days, hours, minutes, seconds) */
|
57 |
/** @var array associative array of time units (days, hours, minutes, seconds) */
|
| Línea 54... |
Línea 58... |
| 54 |
private $_units = null;
|
58 |
private $_units = null;
|
| Línea 62... |
Línea 66... |
| 62 |
* 'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
|
66 |
* 'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
|
| 63 |
* 'defaultunit' => 1|MINSECS|HOURSECS|DAYSECS|WEEKSECS - the default unit to display when
|
67 |
* 'defaultunit' => 1|MINSECS|HOURSECS|DAYSECS|WEEKSECS - the default unit to display when
|
| 64 |
* the time is blank. If not specified, minutes is used.
|
68 |
* the time is blank. If not specified, minutes is used.
|
| 65 |
* 'units' => array containing some or all of 1, MINSECS, HOURSECS, DAYSECS and WEEKSECS
|
69 |
* 'units' => array containing some or all of 1, MINSECS, HOURSECS, DAYSECS and WEEKSECS
|
| 66 |
* which unit choices to offer.
|
70 |
* which unit choices to offer.
|
| - |
|
71 |
* 'allownegative' => true/false - are durations < 0 allowed? (default false)
|
| 67 |
* @param mixed $attributes Either a typical HTML attribute string or an associative array
|
72 |
* @param mixed $attributes Either a typical HTML attribute string or an associative array
|
| 68 |
*/
|
73 |
*/
|
| 69 |
public function __construct($elementName = null, $elementLabel = null,
|
74 |
public function __construct($elementName = null, $elementLabel = null,
|
| 70 |
$options = [], $attributes = null) {
|
75 |
$options = [], $attributes = null) {
|
| 71 |
parent::__construct($elementName, $elementLabel, $attributes);
|
76 |
parent::__construct($elementName, $elementLabel, $attributes);
|
| Línea 76... |
Línea 81... |
| 76 |
// Set the options, do not bother setting bogus ones
|
81 |
// Set the options, do not bother setting bogus ones
|
| 77 |
if (!is_array($options)) {
|
82 |
if (!is_array($options)) {
|
| 78 |
$options = [];
|
83 |
$options = [];
|
| 79 |
}
|
84 |
}
|
| 80 |
$this->_options['optional'] = !empty($options['optional']);
|
85 |
$this->_options['optional'] = !empty($options['optional']);
|
| - |
|
86 |
$this->_options['allownegative'] = !empty($options['allownegative']);
|
| 81 |
if (isset($options['defaultunit'])) {
|
87 |
if (isset($options['defaultunit'])) {
|
| 82 |
if (!array_key_exists($options['defaultunit'], $this->get_units())) {
|
88 |
if (!array_key_exists($options['defaultunit'], $this->get_units())) {
|
| 83 |
throw new coding_exception($options['defaultunit'] .
|
89 |
throw new coding_exception($options['defaultunit'] .
|
| 84 |
' is not a recognised unit in MoodleQuickForm_duration.');
|
90 |
' is not a recognised unit in MoodleQuickForm_duration.');
|
| 85 |
}
|
91 |
}
|
| Línea 175... |
Línea 181... |
| 175 |
$attributes = $this->getAttributesForFormElement();
|
181 |
$attributes = $this->getAttributesForFormElement();
|
| 176 |
if (!isset($attributes['size'])) {
|
182 |
if (!isset($attributes['size'])) {
|
| 177 |
$attributes['size'] = 3;
|
183 |
$attributes['size'] = 3;
|
| 178 |
}
|
184 |
}
|
| 179 |
$this->_elements = [];
|
185 |
$this->_elements = [];
|
| 180 |
// E_STRICT creating elements without forms is nasty because it internally uses $this
|
- |
|
| 181 |
$number = $this->createFormElement('text', 'number',
|
186 |
$number = $this->createFormElement('text', 'number',
|
| 182 |
get_string('time', 'form'), $attributes, true);
|
187 |
get_string('time', 'form'), $attributes, true);
|
| 183 |
$number->set_force_ltr(true);
|
188 |
$number->set_force_ltr(true);
|
| 184 |
$this->_elements[] = $number;
|
189 |
$this->_elements[] = $number;
|
| 185 |
unset($attributes['size']);
|
190 |
unset($attributes['size']);
|
| Línea 246... |
Línea 251... |
| 246 |
default:
|
251 |
default:
|
| 247 |
return parent::onQuickFormEvent($event, $arg, $caller);
|
252 |
return parent::onQuickFormEvent($event, $arg, $caller);
|
| 248 |
}
|
253 |
}
|
| 249 |
}
|
254 |
}
|
| Línea -... |
Línea 255... |
| - |
|
255 |
|
| - |
|
256 |
#[\Override]
|
| - |
|
257 |
public function validateSubmitValue($values) {
|
| - |
|
258 |
if ($this->exportValue($values) < 0 && !$this->_options['allownegative']) {
|
| - |
|
259 |
return get_string('err_positiveduration', 'core_form');
|
| - |
|
260 |
}
|
| - |
|
261 |
return null;
|
| - |
|
262 |
}
|
| 250 |
|
263 |
|
| 251 |
/**
|
264 |
/**
|
| 252 |
* Returns HTML for advchecbox form element.
|
265 |
* Returns HTML for advchecbox form element.
|
| 253 |
*
|
266 |
*
|
| 254 |
* @return string
|
267 |
* @return string
|