mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
formslib durations: MDL-18500 Let the form control the default unit.
This commit is contained in:
parent
8a948bfc9a
commit
562b75b21f
@ -39,18 +39,20 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
|
||||
* Control the fieldnames for form elements
|
||||
* optional => if true, show a checkbox beside the element to turn it on (or off)
|
||||
*/
|
||||
protected $_options = array('optional' => false);
|
||||
protected $_options = array('optional' => false, 'defaultunit' => 60);
|
||||
|
||||
private $_units = null;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @access public
|
||||
* @param string Element's name
|
||||
* @param mixed Label(s) for an element
|
||||
* @param array Options to control the element's display
|
||||
* @param mixed Either a typical HTML attribute string or an associative array
|
||||
* @access public
|
||||
* @param string $elementName Element's name
|
||||
* @param mixed $elementLabel Label(s) for an element
|
||||
* @param array $options Options to control the element's display. Recognised values are
|
||||
* 'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
|
||||
* 'defaultunit' => 1|60|3600|86400 - the default unit to display when the time is blank. If not specified, minutes is used.
|
||||
* @param mixed $attributes Either a typical HTML attribute string or an associative array
|
||||
*/
|
||||
function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
|
||||
$this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
|
||||
@ -62,14 +64,13 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
|
||||
if (!is_array($options)) {
|
||||
$options = array();
|
||||
}
|
||||
foreach ($options as $name => $value) {
|
||||
if (isset($this->_options[$name])) {
|
||||
if (is_array($value) && is_array($this->_options[$name])) {
|
||||
$this->_options[$name] = array_merge($this->_options[$name], $value);
|
||||
} else {
|
||||
$this->_options[$name] = $value;
|
||||
}
|
||||
$this->_options['optional'] = !empty($options['optional']);
|
||||
if (isset($options['defaultunit'])) {
|
||||
if (!array_key_exists($options['defaultunit'], $this->get_units())) {
|
||||
throw new coding_exception($options['defaultunit'] .
|
||||
' is not a recognised unit in MoodleQuickForm_duration.');
|
||||
}
|
||||
$this->_options['defaultunit'] = $options['defaultunit'];
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,7 +96,7 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
|
||||
*/
|
||||
public function seconds_to_unit($seconds) {
|
||||
if ($seconds == 0) {
|
||||
return array(0, 60);
|
||||
return array(0, $this->_options['defaultunit']);
|
||||
}
|
||||
foreach ($this->get_units() as $unit => $notused) {
|
||||
if (fmod($seconds, $unit) == 0) {
|
||||
|
@ -47,6 +47,12 @@ class duration_form_element_test extends UnitTestCase {
|
||||
$this->element = null;
|
||||
}
|
||||
|
||||
function test_constructor() {
|
||||
// Test trying to create with an invalid unit.
|
||||
$this->expectException();
|
||||
$this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123));
|
||||
}
|
||||
|
||||
function test_get_units() {
|
||||
$units = $this->element->get_units();
|
||||
ksort($units);
|
||||
@ -64,6 +70,9 @@ class duration_form_element_test extends UnitTestCase {
|
||||
$this->assertEqual($this->element->seconds_to_unit(7200), array(2, 3600));
|
||||
$this->assertEqual($this->element->seconds_to_unit(86400), array(1, 86400));
|
||||
$this->assertEqual($this->element->seconds_to_unit(90000), array(25, 3600));
|
||||
|
||||
$this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 86400));
|
||||
$this->assertEqual($this->element->seconds_to_unit(0), array(0, 86400)); // Zero minutes, for a nice default unit.
|
||||
}
|
||||
|
||||
function test_exportValue() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user