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   * Contains the base class for fetching the important dates in an activity module for a given module instance and a user.
  19   *
  20   * @package   core
  21   * @copyright Shamim Rezaie <shamim@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  namespace core;
  28  
  29  use cm_info;
  30  
  31  /**
  32   * Class for fetching the  important dates of an activity module for a given module instance and a user.
  33   *
  34   * @copyright Shamim Rezaie <shamim@moodle.com>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  abstract class activity_dates {
  38  
  39      /**
  40       * @var cm_info The course module information object.
  41       */
  42      protected $cm;
  43  
  44      /**
  45       * @var int The user id.
  46       */
  47      protected $userid;
  48  
  49      /**
  50       * activity_dates constructor.
  51       *
  52       * @param cm_info $cm course module
  53       * @param int $userid user id
  54       */
  55      public function __construct(cm_info $cm, int $userid) {
  56          $this->cm = $cm;
  57          $this->userid = $userid;
  58      }
  59  
  60      /**
  61       * Returns a list of important dates in the given module for the user.
  62       *
  63       * @param cm_info $cm The course module information.
  64       * @param int $userid The user ID.
  65       * @return array|array[]
  66       */
  67      public static function get_dates_for_module(cm_info $cm, int $userid): array {
  68          $cmdatesclassname = static::get_dates_classname($cm->modname);
  69          if (!$cmdatesclassname) {
  70              return [];
  71          }
  72  
  73          /** @var activity_dates $dates */
  74          $dates = new $cmdatesclassname($cm, $userid);
  75          return $dates->get_dates();
  76      }
  77  
  78      /**
  79       * Fetches the module's dates class implementation if it's available.
  80       *
  81       * @param string $modname The activity module name. Usually from cm_info::modname.
  82       * @return string|null
  83       */
  84      private static function get_dates_classname(string $modname): ?string {
  85          $cmdatesclass = "mod_{$modname}\\dates";
  86          if (class_exists($cmdatesclass) && is_subclass_of($cmdatesclass, self::class)) {
  87              return $cmdatesclass;
  88          }
  89  
  90          return null;
  91      }
  92  
  93      /**
  94       * Returns a list of important dates for this module.
  95       *
  96       * @return array[] Each element of the array is an array with keys:
  97       *                 label - The label for the date
  98       *                 timestamp - The date
  99       */
 100      protected abstract function get_dates(): array;
 101  }