]> git.mjollnir.org Git - moodle.git/commitdiff
formslib durations: MDL-18500 Let the form control the default unit.
authortjhunt <tjhunt>
Mon, 16 Mar 2009 05:57:25 +0000 (05:57 +0000)
committertjhunt <tjhunt>
Mon, 16 Mar 2009 05:57:25 +0000 (05:57 +0000)
lib/form/duration.php
lib/form/simpletest/testduration.php

index 6812740b4b917efe6b472f5a6f76b9c5835dd62d..10fcbe1f50bc9001746d8f6bcebb0fbff1df53f7 100644 (file)
@@ -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) {
index 307308273b91c619b86a8d7a49c2fb889cadc3a0..93de4b9e883f2599f40a013c50d4ee7290017bd1 100644 (file)
@@ -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() {