Merge branch 'MDL-66685_37' of https://github.com/timhunt/moodle into MOODLE_37_STABLE
[moodle.git] / calendar / classes / type_factory.php
blob3c92ededafb369c69a9e3461c415e84083ab9494
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 namespace core_calendar;
19 /**
20 * Class \core_calendar\type_factory.
22 * Factory class producing required subclasses of {@link \core_calendar\type_base}.
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
28 class type_factory {
30 /**
31 * Returns an instance of the currently used calendar type.
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
37 public static function get_calendar_instance($type = null) {
38 if (is_null($type)) {
39 $type = self::get_calendar_type();
42 $class = "\\calendartype_$type\\structure";
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";
50 return new $class();
53 /**
54 * Returns a list of calendar typess available for use.
56 * @return array the list of calendar types
58 public static function get_list_of_calendar_types() {
59 $calendars = array();
60 $calendardirs = \core_component::get_plugin_list('calendartype');
62 foreach ($calendardirs as $name => $location) {
63 $calendars[$name] = get_string('name', "calendartype_{$name}");
66 return $calendars;
69 /**
70 * Returns the current calendar type in use.
72 * @return string the current calendar type being used
74 public static function get_calendar_type() {
75 global $CFG, $USER, $SESSION, $COURSE;
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';
90 return $return;