on-demand release 4.0dev+
[moodle.git] / calendar / classes / type_factory.php
CommitLineData
022745ac 1<?php
022745ac
MN
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
17namespace core_calendar;
18
19/**
20 * Class \core_calendar\type_factory.
21 *
22 * Factory class producing required subclasses of {@link \core_calendar\type_base}.
df01e4c7
MN
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
022745ac
MN
27 */
28class 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
d6e7a63d
PS
34 * @return \core_calendar\type_base the created calendar_type class
35 * @throws \coding_exception if the calendar type file could not be loaded
022745ac 36 */
6eafa4dd 37 public static function get_calendar_instance($type = null) {
022745ac
MN
38 if (is_null($type)) {
39 $type = self::get_calendar_type();
40 }
41
42 $class = "\\calendartype_$type\\structure";
2aefba53
MN
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
022745ac
MN
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 */
df01e4c7 58 public static function get_list_of_calendar_types() {
022745ac
MN
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 */
df01e4c7 74 public static function get_calendar_type() {
022745ac
MN
75 global $CFG, $USER, $SESSION, $COURSE;
76
da304137
MN
77 // Course calendartype can override all other settings for this page.
78 if (!empty($COURSE->id) and $COURSE->id != SITEID and !empty($COURSE->calendartype)) {
022745ac
MN
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 }
df01e4c7 92}