[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/form/ -> duration.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  
  18  /**
  19   * Duration form element
  20   *
  21   * Contains class to create length of time for element.
  22   *
  23   * @package   core_form
  24   * @copyright 2009 Tim Hunt
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  require_once($CFG->libdir . '/form/group.php');
  30  require_once($CFG->libdir . '/formslib.php');
  31  require_once($CFG->libdir . '/form/text.php');
  32  
  33  /**
  34   * Duration element
  35   *
  36   * HTML class for a length of time. For example, 30 minutes of 4 days. The
  37   * values returned to PHP is the duration in seconds.
  38   *
  39   * @package   core_form
  40   * @category  form
  41   * @copyright 2009 Tim Hunt
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class MoodleQuickForm_duration extends MoodleQuickForm_group {
  45     /**
  46      * Control the fieldnames for form elements
  47      * optional => if true, show a checkbox beside the element to turn it on (or off)
  48      * @var array
  49      */
  50     protected $_options = array('optional' => false, 'defaultunit' => 60);
  51  
  52     /** @var array associative array of time units (days, hours, minutes, seconds) */
  53     private $_units = null;
  54  
  55     /**
  56      * constructor
  57      *
  58      * @param string $elementName Element's name
  59      * @param mixed $elementLabel Label(s) for an element
  60      * @param array $options Options to control the element's display. Recognised values are
  61      *              'optional' => true/false - whether to display an 'enabled' checkbox next to the element.
  62      *              'defaultunit' => 1|60|3600|86400|604800 - the default unit to display when the time is blank.
  63      *              If not specified, minutes is used.
  64      * @param mixed $attributes Either a typical HTML attribute string or an associative array
  65      */
  66      function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
  67          $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  68          $this->_persistantFreeze = true;
  69          $this->_appendName = true;
  70          $this->_type = 'duration';
  71  
  72          // Set the options, do not bother setting bogus ones
  73          if (!is_array($options)) {
  74              $options = array();
  75          }
  76          $this->_options['optional'] = !empty($options['optional']);
  77          if (isset($options['defaultunit'])) {
  78              if (!array_key_exists($options['defaultunit'], $this->get_units())) {
  79                  throw new coding_exception($options['defaultunit'] .
  80                          ' is not a recognised unit in MoodleQuickForm_duration.');
  81              }
  82              $this->_options['defaultunit'] = $options['defaultunit'];
  83          }
  84      }
  85  
  86      /**
  87       * Returns time associative array of unit length.
  88       *
  89       * @return array unit length in seconds => string unit name.
  90       */
  91      public function get_units() {
  92          if (is_null($this->_units)) {
  93              $this->_units = array(
  94                  604800 => get_string('weeks'),
  95                  86400 => get_string('days'),
  96                  3600 => get_string('hours'),
  97                  60 => get_string('minutes'),
  98                  1 => get_string('seconds'),
  99              );
 100          }
 101          return $this->_units;
 102      }
 103  
 104      /**
 105       * Converts seconds to the best possible time unit. for example
 106       * 1800 -> array(30, 60) = 30 minutes.
 107       *
 108       * @param int $seconds an amout of time in seconds.
 109       * @return array associative array ($number => $unit)
 110       */
 111      public function seconds_to_unit($seconds) {
 112          if ($seconds == 0) {
 113              return array(0, $this->_options['defaultunit']);
 114          }
 115          foreach ($this->get_units() as $unit => $notused) {
 116              if (fmod($seconds, $unit) == 0) {
 117                  return array($seconds / $unit, $unit);
 118              }
 119          }
 120          return array($seconds, 1);
 121      }
 122  
 123      /**
 124       * Override of standard quickforms method to create this element.
 125       */
 126      function _createElements() {
 127          $attributes = $this->getAttributes();
 128          if (is_null($attributes)) {
 129              $attributes = array();
 130          }
 131          if (!isset($attributes['size'])) {
 132              $attributes['size'] = 3;
 133          }
 134          $this->_elements = array();
 135          // E_STRICT creating elements without forms is nasty because it internally uses $this
 136          $this->_elements[] = @MoodleQuickForm::createElement('text', 'number', get_string('time', 'form'), $attributes, true);
 137          unset($attributes['size']);
 138          $this->_elements[] = @MoodleQuickForm::createElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true);
 139          // If optional we add a checkbox which the user can use to turn if on
 140          if($this->_options['optional']) {
 141              $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
 142          }
 143          foreach ($this->_elements as $element){
 144              if (method_exists($element, 'setHiddenLabel')){
 145                  $element->setHiddenLabel(true);
 146              }
 147          }
 148      }
 149  
 150      /**
 151       * Called by HTML_QuickForm whenever form event is made on this element
 152       *
 153       * @param string $event Name of event
 154       * @param mixed $arg event arguments
 155       * @param object $caller calling object
 156       * @return bool
 157       */
 158      function onQuickFormEvent($event, $arg, &$caller) {
 159          switch ($event) {
 160              case 'updateValue':
 161                  // constant values override both default and submitted ones
 162                  // default values are overriden by submitted
 163                  $value = $this->_findValue($caller->_constantValues);
 164                  if (null === $value) {
 165                      // if no boxes were checked, then there is no value in the array
 166                      // yet we don't want to display default value in this case
 167                      if ($caller->isSubmitted()) {
 168                          $value = $this->_findValue($caller->_submitValues);
 169                      } else {
 170                          $value = $this->_findValue($caller->_defaultValues);
 171                      }
 172                  }
 173                  if (!is_array($value)) {
 174                      list($number, $unit) = $this->seconds_to_unit($value);
 175                      $value = array('number' => $number, 'timeunit' => $unit);
 176                      // If optional, default to off, unless a date was provided
 177                      if ($this->_options['optional']) {
 178                          $value['enabled'] = $number != 0;
 179                      }
 180                  } else {
 181                      $value['enabled'] = isset($value['enabled']);
 182                  }
 183                  if (null !== $value){
 184                      $this->setValue($value);
 185                  }
 186                  break;
 187  
 188              case 'createElement':
 189                  if ($arg[2]['optional']) {
 190                      $caller->disabledIf($arg[0], $arg[0] . '[enabled]');
 191                  }
 192                  $caller->setType($arg[0] . '[number]', PARAM_FLOAT);
 193                  return parent::onQuickFormEvent($event, $arg, $caller);
 194                  break;
 195  
 196              default:
 197                  return parent::onQuickFormEvent($event, $arg, $caller);
 198          }
 199      }
 200  
 201      /**
 202       * Returns HTML for advchecbox form element.
 203       *
 204       * @return string
 205       */
 206      function toHtml() {
 207          include_once('HTML/QuickForm/Renderer/Default.php');
 208          $renderer = new HTML_QuickForm_Renderer_Default();
 209          $renderer->setElementTemplate('{element}');
 210          parent::accept($renderer);
 211          return $renderer->toHtml();
 212      }
 213  
 214      /**
 215       * Accepts a renderer
 216       *
 217       * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
 218       * @param bool $required Whether a group is required
 219       * @param string $error An error message associated with a group
 220       */
 221      function accept(&$renderer, $required = false, $error = null) {
 222          $renderer->renderElement($this, $required, $error);
 223      }
 224  
 225      /**
 226       * Output a timestamp. Give it the name of the group.
 227       * Override of standard quickforms method.
 228       *
 229       * @param  array $submitValues
 230       * @param  bool  $notused Not used.
 231       * @return array field name => value. The value is the time interval in seconds.
 232       */
 233      function exportValue(&$submitValues, $notused = false) {
 234          // Get the values from all the child elements.
 235          $valuearray = array();
 236          foreach ($this->_elements as $element) {
 237              $thisexport = $element->exportValue($submitValues[$this->getName()], true);
 238              if (!is_null($thisexport)) {
 239                  $valuearray += $thisexport;
 240              }
 241          }
 242  
 243          // Convert the value to an integer number of seconds.
 244          if (empty($valuearray)) {
 245              return null;
 246          }
 247          if ($this->_options['optional'] && empty($valuearray['enabled'])) {
 248              return array($this->getName() => 0);
 249          }
 250          return array($this->getName() => $valuearray['number'] * $valuearray['timeunit']);
 251      }
 252  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1