Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  namespace core_calendar;
  18  
  19  /**
  20   * Class \core_calendar\type_factory.
  21   *
  22   * Factory class producing required subclasses of {@link \core_calendar\type_base}.
  23   *
  24   * @package core_calendar
  25   * @copyright 2008 onwards Foodle Group {@link http://foodle.org}
  26   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class type_factory {
  29  
  30      /**
  31       * Returns an instance of the currently used calendar type.
  32       *
  33       * @param string|null $type the calendar type to use, if none provided use logic to determine
  34       * @return \core_calendar\type_base the created calendar_type class
  35       * @throws \coding_exception if the calendar type file could not be loaded
  36       */
  37      public static function get_calendar_instance($type = null) {
  38          if (is_null($type)) {
  39              $type = self::get_calendar_type();
  40          }
  41  
  42          $class = "\\calendartype_$type\\structure";
  43  
  44          // Ensure the calendar type exists. It may occur that a user has selected a calendar type, which was then
  45          // deleted. If this happens we want to fall back on the Gregorian calendar type.
  46          if (!class_exists($class)) {
  47              $class = "\\calendartype_gregorian\\structure";
  48          }
  49  
  50          return new $class();
  51      }
  52  
  53      /**
  54       * Returns a list of calendar typess available for use.
  55       *
  56       * @return array the list of calendar types
  57       */
  58      public static function get_list_of_calendar_types() {
  59          $calendars = array();
  60          $calendardirs = \core_component::get_plugin_list('calendartype');
  61  
  62          foreach ($calendardirs as $name => $location) {
  63              $calendars[$name] = get_string('name', "calendartype_{$name}");
  64          }
  65  
  66          return $calendars;
  67      }
  68  
  69      /**
  70       * Returns the current calendar type in use.
  71       *
  72       * @return string the current calendar type being used
  73       */
  74      public static function get_calendar_type() {
  75          global $CFG, $USER, $SESSION, $COURSE;
  76  
  77          // Course calendartype can override all other settings for this page.
  78          if (!empty($COURSE->id) and $COURSE->id != SITEID and !empty($COURSE->calendartype)) {
  79              $return = $COURSE->calendartype;
  80          } else if (!empty($SESSION->calendartype)) { // Session calendartype can override other settings.
  81              $return = $SESSION->calendartype;
  82          } else if (!empty($USER->calendartype)) {
  83              $return = $USER->calendartype;
  84          } else if (!empty($CFG->calendartype)) {
  85              $return = $CFG->calendartype;
  86          } else {
  87              $return = 'gregorian';
  88          }
  89  
  90          return $return;
  91      }
  92  }