Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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              $gregoriancalendar = \core_calendar\type_factory::get_calendar_instance('gregorian');
  85              $datetime = $gregoriancalendar->timestamp_to_date_string($datetime, '%Y-%m-%d-%H-%M-%S', 99, true, true);
  86          }
  87  
  88          $datetime = explode('-', $datetime);
  89          // Bound year with start and end year.
  90          $datetime[0] = min(max($datetime[0], $this->field->param1), $this->field->param2);
  91  
  92          if (!empty($this->field->param3) && count($datetime) == 6) {
  93              return make_timestamp($datetime[0], $datetime[1], $datetime[2], $datetime[3], $datetime[4], $datetime[5]);
  94          } else {
  95              return make_timestamp($datetime[0], $datetime[1], $datetime[2]);
  96          }
  97      }
  98  
  99      /**
 100       * Display the data for this field.
 101       */
 102      public function display_data() {
 103          // Check if time was specified.
 104          if (!empty($this->field->param3)) {
 105              $format = get_string('strftimedaydatetime', 'langconfig');
 106          } else {
 107              $format = get_string('strftimedate', 'langconfig');
 108          }
 109  
 110          // Check if a date has been specified.
 111          if (empty($this->data)) {
 112              return get_string('notset', 'profilefield_datetime');
 113          } else {
 114              return userdate($this->data, $format);
 115          }
 116      }
 117  
 118      /**
 119       * Check if the field data is considered empty
 120       *
 121       * @return boolean
 122       */
 123      public function is_empty() {
 124          return empty($this->data);
 125      }
 126  
 127      /**
 128       * Return the field type and null properties.
 129       * This will be used for validating the data submitted by a user.
 130       *
 131       * @return array the param type and null property
 132       * @since Moodle 3.2
 133       */
 134      public function get_field_properties() {
 135          return array(PARAM_INT, NULL_NOT_ALLOWED);
 136      }
 137  }