[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

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


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