From f3a076ded425b9c62cb05e30453b518e8d770462 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 11 Mar 2009 06:07:33 +0000 Subject: [PATCH] MDL-18500 New formslib element type duration for periods of time. --- lang/en_utf8/form.php | 4 +- lang/en_utf8/moodle.php | 1 + lib/form/duration.php | 182 ++++++++++++++++++++++++++- lib/form/simpletest/testduration.php | 85 +++++++++++++ 4 files changed, 269 insertions(+), 3 deletions(-) create mode 100644 lib/form/simpletest/testduration.php diff --git a/lang/en_utf8/form.php b/lang/en_utf8/form.php index 0a2ac2b251..0f46cdaca3 100644 --- a/lang/en_utf8/form.php +++ b/lang/en_utf8/form.php @@ -31,7 +31,9 @@ $string['revealpassword'] = 'Reveal'; $string['security'] = 'Security'; $string['selectallornone'] = 'Select all/none'; $string['showadvanced']='Show Advanced'; -$string['somefieldsrequired'] = 'There are required fields in this form marked$a.'; +$string['somefieldsrequired'] = 'There are required fields in this form marked$a.'; +$string['time'] = 'Time'; +$string['timeunit'] = 'Time unit'; $string['timing'] = 'Timing'; $string['unmaskpassword'] = 'Unmask'; $string['year'] = 'Year'; diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index f518aadabc..d6ef84212c 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -1381,6 +1381,7 @@ $string['secondstotime604800'] = '1 Week'; $string['secondstotime86400'] = '1 day'; $string['secretalreadyused'] = 'Change password confirmation link was already used, password was not changed.'; $string['secs'] = 'secs'; +$string['seconds'] = 'seconds'; $string['section'] = 'Section'; $string['sections'] = 'Sections'; $string['seealsostats'] = 'See also: stats'; diff --git a/lib/form/duration.php b/lib/form/duration.php index 8d2e7874f9..6812740b4b 100644 --- a/lib/form/duration.php +++ b/lib/form/duration.php @@ -34,6 +34,184 @@ require_once($CFG->libdir . '/form/text.php'); * * @package formslib */ -class MoodleQuickForm_duration extends MoodleQuickForm_text { +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); -} \ No newline at end of file + 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 + */ + function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { + $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes); + $this->_persistantFreeze = true; + $this->_appendName = true; + $this->_type = 'duration'; + + // Set the options, do not bother setting bogus ones + 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; + } + } + } + } + + /** + * @return array unit length in seconds => string unit name. + */ + public function get_units() { + if (is_null($this->_units)) { + $this->_units = array( + 86400 => get_string('days'), + 3600 => get_string('hours'), + 60 => get_string('minutes'), + 1 => get_string('seconds'), + ); + } + return $this->_units; + } + + /** + * @param $seconds an amout of time in seconds. + * @return array($number, $unit) Conver an interval to the best possible unit. + * for example 1800 -> array(30, 60) = 30 minutes. + */ + public function seconds_to_unit($seconds) { + if ($seconds == 0) { + return array(0, 60); + } + foreach ($this->get_units() as $unit => $notused) { + if (fmod($seconds, $unit) == 0) { + return array($seconds / $unit, $unit); + } + } + return array($seconds, 1); + } + + // Override of standard quickforms method. + function _createElements() { + $attributes = $this->getAttributes(); + if (is_null($attributes)) { + $attributes = array(); + } + if (!isset($attributes['size'])) { + $attributes['size'] = 3; + } + $this->_elements = array(); + $this->_elements[] = MoodleQuickForm::createElement('text', 'number', get_string('time', 'form'), $attributes, true); + unset($attributes['size']); + $this->_elements[] = MoodleQuickForm::createElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true); + // If optional we add a checkbox which the user can use to turn if on + if($this->_options['optional']) { + $this->_elements[] = MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); + } + foreach ($this->_elements as $element){ + if (method_exists($element, 'setHiddenLabel')){ + $element->setHiddenLabel(true); + } + } + } + + // Override of standard quickforms method. + function onQuickFormEvent($event, $arg, $caller) { + switch ($event) { + case 'updateValue': + // constant values override both default and submitted ones + // default values are overriden by submitted + $value = $this->_findValue($caller->_constantValues); + if (null === $value) { + // if no boxes were checked, then there is no value in the array + // yet we don't want to display default value in this case + if ($caller->isSubmitted()) { + $value = $this->_findValue($caller->_submitValues); + } else { + $value = $this->_findValue($caller->_defaultValues); + } + } + if (!is_array($value)) { + list($number, $unit) = $this->seconds_to_unit($value); + $value = array('number' => $number, 'timeunit' => $unit); + // If optional, default to off, unless a date was provided + if ($this->_options['optional']) { + $value['enabled'] = $number != 0; + } + } else { + $value['enabled'] = isset($value['enabled']); + } + if (null !== $value){ + $this->setValue($value); + } + break; + + case 'createElement': + if ($arg[2]['optional']) { + $caller->disabledIf($arg[0], $arg[0] . '[enabled]'); + } + $caller->setType($arg[0] . '[number]', PARAM_NUMBER); + return parent::onQuickFormEvent($event, $arg, $caller); + break; + + default: + return parent::onQuickFormEvent($event, $arg, $caller); + } + } + + // Override of standard quickforms method. + function toHtml() { + include_once('HTML/QuickForm/Renderer/Default.php'); + $renderer = new HTML_QuickForm_Renderer_Default(); + $renderer->setElementTemplate('{element}'); + parent::accept($renderer); + return $renderer->toHtml(); + } + + // Override of standard quickforms method. + function accept($renderer, $required = false, $error = null) { + $renderer->renderElement($this, $required, $error); + } + + /** + * Output a timestamp. Give it the name of the group. + * Override of standard quickforms method. + * + * @param array $submitValues + * @param bool $notused Not used. + * @return array field name => value. The value is the time interval in seconds. + */ + function exportValue($submitValues, $notused = false) { + // Get the values from all the child elements. + $valuearray = array(); + foreach ($this->_elements as $element) { + $thisexport = $element->exportValue($submitValues[$this->getName()], true); + if (!is_null($thisexport)) { + $valuearray += $thisexport; + } + } + + // Convert the value to an integer number of seconds. + if (empty($valuearray)) { + return null; + } + if ($this->_options['optional'] && empty($valuearray['enabled'])) { + return array($this->getName() => 0); + } + return array($this->getName() => $valuearray['number'] * $valuearray['timeunit']); + } +} diff --git a/lib/form/simpletest/testduration.php b/lib/form/simpletest/testduration.php new file mode 100644 index 0000000000..307308273b --- /dev/null +++ b/lib/form/simpletest/testduration.php @@ -0,0 +1,85 @@ +libdir . '/form/duration.php'); + +/** + * Unit tests for (some of) ../duration.php. + * + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + * @package formslib + */ +class duration_form_element_test extends UnitTestCase { + private $element; + + function setUp() { + $this->element = new MoodleQuickForm_duration(); + } + + function tearDown() { + $this->element = null; + } + + function test_get_units() { + $units = $this->element->get_units(); + ksort($units); + $this->assertEqual($units, array(1 => get_string('seconds'), 60 => get_string('minutes'), + 3600 => get_string('hours'), 86400 => get_string('days'))); + } + + function test_seconds_to_unit() { + $this->assertEqual($this->element->seconds_to_unit(0), array(0, 60)); // Zero minutes, for a nice default unit. + $this->assertEqual($this->element->seconds_to_unit(1), array(1, 1)); + $this->assertEqual($this->element->seconds_to_unit(3601), array(3601, 1)); + $this->assertEqual($this->element->seconds_to_unit(60), array(1, 60)); + $this->assertEqual($this->element->seconds_to_unit(180), array(3, 60)); + $this->assertEqual($this->element->seconds_to_unit(3600), array(1, 3600)); + $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)); + } + + function test_exportValue() { + $el = new MoodleQuickForm_duration('testel'); + $el->_createElements(); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 10, 'timeunit' => 1))), array('testel' => 10)); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 3, 'timeunit' => 60))), array('testel' => 180)); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 1.5, 'timeunit' => 60))), array('testel' => 90)); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 2, 'timeunit' => 3600))), array('testel' => 7200)); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 1, 'timeunit' => 86400))), array('testel' => 86400)); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 0, 'timeunit' => 3600))), array('testel' => 0)); + + $el = new MoodleQuickForm_duration('testel', null, array('optional' => true)); + $el->_createElements(); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 10, 'timeunit' => 1))), array('testel' => 0)); + $this->assertEqual($el->exportValue(array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1))), array('testel' => 20)); + } +} +?> -- 2.39.5