Merge branch 'MDL-73076-master' of https://github.com/lameze/moodle
[moodle.git] / lib / classes / activity_dates.php
blob74564e0f68dc8bc318e2c36966ab04c71e9710e3
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.
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/>.
17 /**
18 * Contains the base class for fetching the important dates in an activity module for a given module instance and a user.
20 * @package core
21 * @copyright Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 declare(strict_types=1);
27 namespace core;
29 use cm_info;
31 /**
32 * Class for fetching the important dates of an activity module for a given module instance and a user.
34 * @copyright Shamim Rezaie <shamim@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 abstract class activity_dates {
39 /**
40 * @var cm_info The course module information object.
42 protected $cm;
44 /**
45 * @var int The user id.
47 protected $userid;
49 /**
50 * activity_dates constructor.
52 * @param cm_info $cm course module
53 * @param int $userid user id
55 public function __construct(cm_info $cm, int $userid) {
56 $this->cm = $cm;
57 $this->userid = $userid;
60 /**
61 * Returns a list of important dates in the given module for the user.
63 * @param cm_info $cm The course module information.
64 * @param int $userid The user ID.
65 * @return array|array[]
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 [];
73 /** @var activity_dates $dates */
74 $dates = new $cmdatesclassname($cm, $userid);
75 return $dates->get_dates();
78 /**
79 * Fetches the module's dates class implementation if it's available.
81 * @param string $modname The activity module name. Usually from cm_info::modname.
82 * @return string|null
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;
90 return null;
93 /**
94 * Returns a list of important dates for this module.
96 * @return array[] Each element of the array is an array with keys:
97 * label - The label for the date
98 * timestamp - The date
100 protected abstract function get_dates(): array;