MDL-69507 duration form field: should return an int number of seconds

This commit is contained in:
Tim Hunt 2020-08-19 15:13:10 +01:00
parent e5a0f11b5e
commit 58b7986776
2 changed files with 5 additions and 2 deletions

View file

@ -34,7 +34,7 @@ require_once($CFG->libdir . '/form/text.php');
* Duration element * Duration element
* *
* HTML class for a length of time. For example, 30 minutes of 4 days. The * HTML class for a length of time. For example, 30 minutes of 4 days. The
* values returned to PHP is the duration in seconds. * values returned to PHP is the duration in seconds (an int rounded to the nearest second).
* *
* @package core_form * @package core_form
* @category form * @category form
@ -300,6 +300,7 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
if ($this->_options['optional'] && empty($valuearray['enabled'])) { if ($this->_options['optional'] && empty($valuearray['enabled'])) {
return $this->_prepareValue(0, $assoc); return $this->_prepareValue(0, $assoc);
} }
return $this->_prepareValue($valuearray['number'] * $valuearray['timeunit'], $assoc); return $this->_prepareValue(
(int) round($valuearray['number'] * $valuearray['timeunit']), $assoc);
} }
} }

View file

@ -145,6 +145,8 @@ class core_form_duration_testcase extends basic_testcase {
public function export_value_cases(): array { public function export_value_cases(): array {
return [ return [
[10, '10', 1], [10, '10', 1],
[9, '9.3', 1],
[10, '9.5', 1],
[180, '3', MINSECS], [180, '3', MINSECS],
[90, '1.5', MINSECS], [90, '1.5', MINSECS],
[7200, '2', HOURSECS], [7200, '2', HOURSECS],