[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> datetimeselector.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   * Group of date and time input element
  20   *
  21   * Contains class for a group of elements used to input a date and time.
  22   *
  23   * @package   core_form
  24   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  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  
  32  /**
  33   * Element used to input a date and time.
  34   *
  35   * Class for a group of elements used to input a date and time.
  36   *
  37   * @package   core_form
  38   * @category  form
  39   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
  43  
  44      /**
  45       * Options for the element.
  46       *
  47       * startyear => int start of range of years that can be selected
  48       * stopyear => int last year that can be selected
  49       * defaulttime => default time value if the field is currently not set
  50       * timezone => int|float|string (optional) timezone modifier used for edge case only.
  51       *      If not specified, then date is caclulated based on current user timezone.
  52       *      Note: dst will be calculated for string timezones only
  53       *      {@link http://docs.moodle.org/dev/Time_API#Timezone}
  54       * step => step to increment minutes by
  55       * optional => if true, show a checkbox beside the date to turn it on (or off)
  56       * @var array
  57       */
  58      protected $_options = array();
  59  
  60      /**
  61       * @var array These complement separators, they are appended to the resultant HTML.
  62       */
  63      protected $_wrap = array('', '');
  64  
  65      /**
  66       * @var null|bool Keeps track of whether the date selector was initialised using createElement
  67       *                or addElement. If true, createElement was used signifying the element has been
  68       *                added to a group - see MDL-39187.
  69       */
  70      protected $_usedcreateelement = true;
  71  
  72      /**
  73       * Class constructor
  74       *
  75       * @param string $elementName Element's name
  76       * @param mixed $elementLabel Label(s) for an element
  77       * @param array $options Options to control the element's display
  78       * @param mixed $attributes Either a typical HTML attribute string or an associative array
  79       */
  80      public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
  81          // Get the calendar type used - see MDL-18375.
  82          $calendartype = \core_calendar\type_factory::get_calendar_instance();
  83          $this->_options = array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(),
  84              'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);
  85  
  86          // TODO MDL-52313 Replace with the call to parent::__construct().
  87          HTML_QuickForm_element::__construct($elementName, $elementLabel, $attributes);
  88          $this->_persistantFreeze = true;
  89          $this->_appendName = true;
  90          $this->_type = 'date_time_selector';
  91          // set the options, do not bother setting bogus ones
  92          if (is_array($options)) {
  93              foreach ($options as $name => $value) {
  94                  if (isset($this->_options[$name])) {
  95                      if (is_array($value) && is_array($this->_options[$name])) {
  96                          $this->_options[$name] = @array_merge($this->_options[$name], $value);
  97                      } else {
  98                          $this->_options[$name] = $value;
  99                      }
 100                  }
 101              }
 102          }
 103  
 104          // The YUI2 calendar only supports the gregorian calendar type.
 105          if ($calendartype->get_name() === 'gregorian') {
 106              form_init_date_js();
 107          }
 108      }
 109  
 110      /**
 111       * Old syntax of class constructor. Deprecated in PHP7.
 112       *
 113       * @deprecated since Moodle 3.1
 114       */
 115      public function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
 116          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 117          self::__construct($elementName, $elementLabel, $options, $attributes);
 118      }
 119  
 120      /**
 121       * This will create date group element constisting of day, month and year.
 122       *
 123       * @access private
 124       */
 125      function _createElements() {
 126          global $OUTPUT;
 127  
 128          // Get the calendar type used - see MDL-18375.
 129          $calendartype = \core_calendar\type_factory::get_calendar_instance();
 130  
 131          for ($i = 0; $i <= 23; $i++) {
 132              $hours[$i] = sprintf("%02d", $i);
 133          }
 134          for ($i = 0; $i < 60; $i += $this->_options['step']) {
 135              $minutes[$i] = sprintf("%02d", $i);
 136          }
 137  
 138          $this->_elements = array();
 139          $dateformat = $calendartype->get_date_order($this->_options['startyear'], $this->_options['stopyear']);
 140          foreach ($dateformat as $key => $date) {
 141              // E_STRICT creating elements without forms is nasty because it internally uses $this
 142              $this->_elements[] = @MoodleQuickForm::createElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true);
 143          }
 144          if (right_to_left()) {   // Switch order of elements for Right-to-Left
 145              $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
 146              $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
 147          } else {
 148              $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
 149              $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
 150          }
 151          // The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
 152          if ($calendartype->get_name() === 'gregorian') {
 153              $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle');
 154              $this->_elements[] = @MoodleQuickForm::createElement('link', 'calendar',
 155                      null, '#', $image,
 156                      array('class' => 'visibleifjs'));
 157          }
 158          // If optional we add a checkbox which the user can use to turn if on
 159          if ($this->_options['optional']) {
 160              $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
 161          }
 162          foreach ($this->_elements as $element){
 163              if (method_exists($element, 'setHiddenLabel')){
 164                  $element->setHiddenLabel(true);
 165              }
 166          }
 167  
 168      }
 169  
 170      /**
 171       * Called by HTML_QuickForm whenever form event is made on this element
 172       *
 173       * @param string $event Name of event
 174       * @param mixed $arg event arguments
 175       * @param object $caller calling object
 176       * @return bool
 177       */
 178      function onQuickFormEvent($event, $arg, &$caller) {
 179          switch ($event) {
 180              case 'updateValue':
 181                  // Constant values override both default and submitted ones
 182                  // default values are overriden by submitted.
 183                  $value = $this->_findValue($caller->_constantValues);
 184                  if (null === $value) {
 185                      // If no boxes were checked, then there is no value in the array
 186                      // yet we don't want to display default value in this case.
 187                      if ($caller->isSubmitted()) {
 188                          $value = $this->_findValue($caller->_submitValues);
 189                      } else {
 190                          $value = $this->_findValue($caller->_defaultValues);
 191                      }
 192                  }
 193                  $requestvalue=$value;
 194                  if ($value == 0) {
 195                      $value = $this->_options['defaulttime'];
 196                      if (!$value) {
 197                          $value = time();
 198                      }
 199                  }
 200                  if (!is_array($value)) {
 201                      $calendartype = \core_calendar\type_factory::get_calendar_instance();
 202                      $currentdate = $calendartype->timestamp_to_date_array($value, $this->_options['timezone']);
 203                      // Round minutes to the previous multiple of step.
 204                      $currentdate['minutes'] -= $currentdate['minutes'] % $this->_options['step'];
 205                      $value = array(
 206                          'minute' => $currentdate['minutes'],
 207                          'hour' => $currentdate['hours'],
 208                          'day' => $currentdate['mday'],
 209                          'month' => $currentdate['mon'],
 210                          'year' => $currentdate['year']);
 211                      // If optional, default to off, unless a date was provided.
 212                      if ($this->_options['optional']) {
 213                          $value['enabled'] = $requestvalue != 0;
 214                      }
 215                  } else {
 216                      $value['enabled'] = isset($value['enabled']);
 217                  }
 218                  if (null !== $value) {
 219                      $this->setValue($value);
 220                  }
 221                  break;
 222              case 'createElement':
 223                  if ($arg[2]['optional']) {
 224                      // When using the function addElement, rather than createElement, we still
 225                      // enter this case, making this check necessary.
 226                      if ($this->_usedcreateelement) {
 227                          $caller->disabledIf($arg[0] . '[day]', $arg[0] . '[enabled]');
 228                          $caller->disabledIf($arg[0] . '[month]', $arg[0] . '[enabled]');
 229                          $caller->disabledIf($arg[0] . '[year]', $arg[0] . '[enabled]');
 230                          $caller->disabledIf($arg[0] . '[hour]', $arg[0] . '[enabled]');
 231                          $caller->disabledIf($arg[0] . '[minute]', $arg[0] . '[enabled]');
 232                      } else {
 233                          $caller->disabledIf($arg[0], $arg[0] . '[enabled]');
 234                      }
 235                  }
 236                  return parent::onQuickFormEvent($event, $arg, $caller);
 237                  break;
 238              case 'addElement':
 239                  $this->_usedcreateelement = false;
 240                  return parent::onQuickFormEvent($event, $arg, $caller);
 241                  break;
 242              default:
 243                  return parent::onQuickFormEvent($event, $arg, $caller);
 244          }
 245      }
 246  
 247      /**
 248       * Returns HTML for advchecbox form element.
 249       *
 250       * @return string
 251       */
 252      function toHtml() {
 253          include_once('HTML/QuickForm/Renderer/Default.php');
 254          $renderer = new HTML_QuickForm_Renderer_Default();
 255          $renderer->setElementTemplate('{element}');
 256          parent::accept($renderer);
 257  
 258          $html = $this->_wrap[0];
 259          if ($this->_usedcreateelement) {
 260              $html .= html_writer::tag('span', $renderer->toHtml(), array('class' => 'fdate_time_selector'));
 261          } else {
 262              $html .= $renderer->toHtml();
 263          }
 264          $html .= $this->_wrap[1];
 265  
 266          return $html;
 267      }
 268  
 269      /**
 270       * Accepts a renderer
 271       *
 272       * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
 273       * @param bool $required Whether a group is required
 274       * @param string $error An error message associated with a group
 275       */
 276      function accept(&$renderer, $required = false, $error = null) {
 277          $renderer->renderElement($this, $required, $error);
 278      }
 279  
 280      /**
 281       * Output a timestamp. Give it the name of the group.
 282       *
 283       * @param array $submitValues values submitted.
 284       * @param bool $assoc specifies if returned array is associative
 285       * @return array
 286       */
 287      function exportValue(&$submitValues, $assoc = false) {
 288          $value = null;
 289          $valuearray = array();
 290          foreach ($this->_elements as $element){
 291              $thisexport = $element->exportValue($submitValues[$this->getName()], true);
 292              if ($thisexport!=null){
 293                  $valuearray += $thisexport;
 294              }
 295          }
 296          if (count($valuearray)){
 297              if($this->_options['optional']) {
 298                  // If checkbox is on, the value is zero, so go no further
 299                  if(empty($valuearray['enabled'])) {
 300                      $value[$this->getName()] = 0;
 301                      return $value;
 302                  }
 303              }
 304              // Get the calendar type used - see MDL-18375.
 305              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 306              $gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'],
 307                                                                   $valuearray['month'],
 308                                                                   $valuearray['day'],
 309                                                                   $valuearray['hour'],
 310                                                                   $valuearray['minute']);
 311              $value[$this->getName()] = make_timestamp($gregoriandate['year'],
 312                                                        $gregoriandate['month'],
 313                                                        $gregoriandate['day'],
 314                                                        $gregoriandate['hour'],
 315                                                        $gregoriandate['minute'],
 316                                                        0,
 317                                                        $this->_options['timezone'],
 318                                                        true);
 319  
 320              return $value;
 321          } else {
 322              return null;
 323          }
 324      }
 325  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1