*
* @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']);
+ }
+}
--- /dev/null
+<?php // $Id$
+
+///////////////////////////////////////////////////////////////////////////
+// //
+// NOTICE OF COPYRIGHT //
+// //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment //
+// http://moodle.org //
+// //
+// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
+// //
+// This program is free software; you can redistribute it and/or modify //
+// it under the terms of the GNU General Public License as published by //
+// the Free Software Foundation; either version 2 of the License, or //
+// (at your option) any later version. //
+// //
+// This program is distributed in the hope that it will be useful, //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
+// GNU General Public License for more details: //
+// //
+// http://www.gnu.org/copyleft/gpl.html //
+// //
+///////////////////////////////////////////////////////////////////////////
+
+if (!defined('MOODLE_INTERNAL')) {
+ die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
+}
+
+global $CFG;
+require_once($CFG->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));
+ }
+}
+?>