[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/user/profile/field/datetime/ -> field.class.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   * This file contains the datetime profile field class.
  19   *
  20   * @package profilefield_datetime
  21   * @copyright 2010 Mark Nelson <markn@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23   */
  24  
  25  /**
  26   * Handles displaying and editing the datetime field.
  27   *
  28   * @copyright 2010 Mark Nelson <markn@moodle.com>
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  30   */
  31  class profile_field_datetime extends profile_field_base {
  32  
  33      /**
  34       * Handles editing datetime fields.
  35       *
  36       * @param moodleform $mform
  37       */
  38      public function edit_field_add($mform) {
  39          // Get the current calendar in use - see MDL-18375.
  40          $calendartype = \core_calendar\type_factory::get_calendar_instance();
  41  
  42          // Check if the field is required.
  43          if ($this->field->required) {
  44              $optional = false;
  45          } else {
  46              $optional = true;
  47          }
  48  
  49          // Convert the year stored in the DB as gregorian to that used by the calendar type.
  50          $startdate = $calendartype->convert_from_gregorian($this->field->param1, 1, 1);
  51          $stopdate = $calendartype->convert_from_gregorian($this->field->param2, 1, 1);
  52  
  53          $attributes = array(
  54              'startyear' => $startdate['year'],
  55              'stopyear'  => $stopdate['year'],
  56              'optional'  => $optional
  57          );
  58  
  59          // Check if they wanted to include time as well.
  60          if (!empty($this->field->param3)) {
  61              $mform->addElement('date_time_selector', $this->inputname, format_string($this->field->name), $attributes);
  62          } else {
  63              $mform->addElement('date_selector', $this->inputname, format_string($this->field->name), $attributes);
  64          }
  65  
  66          $mform->setType($this->inputname, PARAM_INT);
  67          $mform->setDefault($this->inputname, time());
  68      }
  69  
  70      /**
  71       * If timestamp is in YYYY-MM-DD or YYYY-MM-DD-HH-MM-SS format, then convert it to timestamp.
  72       *
  73       * @param string|int $datetime datetime to be converted.
  74       * @param stdClass $datarecord The object that will be used to save the record
  75       * @return int timestamp
  76       * @since Moodle 2.5
  77       */
  78      public function edit_save_data_preprocess($datetime, $datarecord) {
  79          if (!$datetime) {
  80              return 0;
  81          }
  82  
  83          if (is_numeric($datetime)) {
  84              $datetime = userdate($datetime, '%Y-%m-%d-%H-%M-%S');
  85          }
  86  
  87          $datetime = explode('-', $datetime);
  88          // Bound year with start and end year.
  89          $datetime[0] = min(max($datetime[0], $this->field->param1), $this->field->param2);
  90  
  91          if (!empty($this->field->param3) && count($datetime) == 6) {
  92              return make_timestamp($datetime[0], $datetime[1], $datetime[2], $datetime[3], $datetime[4], $datetime[5]);
  93          } else {
  94              return make_timestamp($datetime[0], $datetime[1], $datetime[2]);
  95          }
  96      }
  97  
  98      /**
  99       * Display the data for this field.
 100       */
 101      public function display_data() {
 102          // Check if time was specified.
 103          if (!empty($this->field->param3)) {
 104              $format = get_string('strftimedaydatetime', 'langconfig');
 105          } else {
 106              $format = get_string('strftimedate', 'langconfig');
 107          }
 108  
 109          // Check if a date has been specified.
 110          if (empty($this->data)) {
 111              return get_string('notset', 'profilefield_datetime');
 112          } else {
 113              return userdate($this->data, $format);
 114          }
 115      }
 116  
 117      /**
 118       * Check if the field data is considered empty
 119       *
 120       * @return boolean
 121       */
 122      public function is_empty() {
 123          return empty($this->data);
 124      }
 125  }


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