Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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   * Customfield date plugin
  19   *
  20   * @package   customfield_date
  21   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace customfield_date;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Class field
  31   *
  32   * @package customfield_date
  33   * @copyright 2018 David Matamoros <davidmc@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class field_controller extends \core_customfield\field_controller {
  37      /**
  38       * Type of plugin data
  39       */
  40      const TYPE = 'date';
  41  
  42      /**
  43       * Validate the data from the config form.
  44       *
  45       * @param array $data
  46       * @param array $files
  47       * @return array associative array of error messages
  48       */
  49      public function config_form_validation(array $data, $files = array()) : array {
  50          $errors = array();
  51  
  52          // Make sure the start year is not greater than the end year.
  53          if (!empty($data['configdata']['mindate']) && !empty($data['configdata']['maxdate']) &&
  54                  $data['configdata']['mindate'] > $data['configdata']['maxdate']) {
  55              $errors['configdata[mindate]'] = get_string('mindateaftermax', 'customfield_date');
  56          }
  57  
  58          return $errors;
  59      }
  60  
  61      /**
  62       * Add fields for editing a date field.
  63       *
  64       * @param \MoodleQuickForm $mform
  65       */
  66      public function config_form_definition(\MoodleQuickForm $mform) {
  67          $config = $this->get('configdata');
  68  
  69          // Add elements.
  70          $mform->addElement('header', 'header_specificsettings', get_string('specificsettings', 'customfield_date'));
  71          $mform->setExpanded('header_specificsettings', true);
  72  
  73          $mform->addElement('advcheckbox', 'configdata[includetime]', get_string('includetime', 'customfield_date'));
  74  
  75          $mform->addElement('date_time_selector', 'configdata[mindate]', get_string('mindate', 'customfield_date'),
  76              ['optional' => true]);
  77  
  78          $mform->addElement('date_time_selector', 'configdata[maxdate]', get_string('maxdate', 'customfield_date'),
  79              ['optional' => true]);
  80  
  81          $mform->hideIf('configdata[maxdate][hour]', 'configdata[includetime]');
  82          $mform->hideIf('configdata[maxdate][minute]', 'configdata[includetime]');
  83          $mform->hideIf('configdata[mindate][hour]', 'configdata[includetime]');
  84          $mform->hideIf('configdata[mindate][minute]', 'configdata[includetime]');
  85      }
  86  
  87      /**
  88       * Does this custom field type support being used as part of the block_myoverview
  89       * custom field grouping?
  90       * @return bool
  91       */
  92      public function supports_course_grouping(): bool {
  93          return true;
  94      }
  95  
  96      /**
  97       * If this field supports course grouping, then this function needs overriding to
  98       * return the formatted values for this.
  99       * @param array $values the used values that need formatting
 100       * @return array
 101       */
 102      public function course_grouping_format_values($values): array {
 103          $format = get_string('strftimedate', 'langconfig');
 104          $ret = [];
 105          foreach ($values as $value) {
 106              if ($value) {
 107                  $ret[$value] = userdate($value, $format);
 108              }
 109          }
 110          if (!$ret) {
 111              return []; // If the only dates found are 0, then do not show any options.
 112          }
 113          $ret[BLOCK_MYOVERVIEW_CUSTOMFIELD_EMPTY] = get_string('nocustomvalue', 'block_myoverview',
 114              $this->get_formatted_name());
 115          return $ret;
 116      }
 117  
 118      /**
 119       * Convert given value into appropriate timestamp
 120       *
 121       * @param string $value
 122       * @return int
 123       */
 124      public function parse_value(string $value) {
 125          $timestamp = strtotime($value);
 126  
 127          // If we have a valid, positive timestamp then return it.
 128          return $timestamp > 0 ? $timestamp : 0;
 129      }
 130  }