MDL-18375 calendar: implemented the usage of the recently introduced automatic class loading

This commit is contained in:
Mark Nelson 2013-07-03 17:58:14 +08:00
parent 84423bd9e5
commit 022745acd2
12 changed files with 107 additions and 92 deletions

View File

@ -15,6 +15,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_calendar;
/**
* Defines functions used by calendar type plugins.
*
@ -26,7 +28,7 @@
* @copyright 2008 onwards Foodle Group {@link http://foodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class calendar_type_plugin_base {
abstract class type_base {
/**
* Returns a list of all the possible days for all months.
@ -232,73 +234,3 @@ abstract class calendar_type_plugin_base {
return $getdate;
}
}
/**
* Class calendar_type_plugin_factory.
*
* Factory class producing required subclasses of {@link calendar_type_plugin_base}.
*/
class calendar_type_plugin_factory {
/**
* Returns an instance of the currently used calendar type.
*
* @return calendar_type_plugin_* the created calendar_type class
* @throws coding_exception if the calendar type file could not be loaded
*/
static function factory() {
global $CFG;
$type = self::get_calendar_type();
$file = 'calendar/type/' . $type . '/lib.php';
$fullpath = $CFG->dirroot . '/' . $file;
if (is_readable($fullpath)) {
require_once($fullpath);
$class = "calendar_type_plugin_$type";
return new $class();
} else {
throw new coding_exception("The calendar type file $file could not be initialised, check that it exists
and that the web server has permission to read it.");
}
}
/**
* Returns a list of calendar typess available for use.
*
* @return array the list of calendar types
*/
static function get_list_of_calendar_types() {
$calendars = array();
$calendardirs = core_component::get_plugin_list('calendartype');
foreach ($calendardirs as $name => $location) {
$calendars[$name] = get_string('name', "calendartype_{$name}");
}
return $calendars;
}
/**
* Returns the current calendar type in use.
*
* @return string the current calendar type being used
*/
static function get_calendar_type() {
global $CFG, $USER, $SESSION, $COURSE;
if (!empty($COURSE->id) and $COURSE->id != SITEID and !empty($COURSE->calendartype)) { // Course calendartype can override all other settings for this page.
$return = $COURSE->calendartype;
} else if (!empty($SESSION->calendartype)) { // Session calendartype can override other settings.
$return = $SESSION->calendartype;
} else if (!empty($USER->calendartype)) {
$return = $USER->calendartype;
} else if (!empty($CFG->calendartype)) {
$return = $CFG->calendartype;
} else {
$return = 'gregorian';
}
return $return;
}
}

View File

@ -0,0 +1,81 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_calendar;
/**
* Class \core_calendar\type_factory.
*
* Factory class producing required subclasses of {@link \core_calendar\type_base}.
*/
class type_factory {
/**
* Returns an instance of the currently used calendar type.
*
* @param string|null $type the calendar type to use, if none provided use logic to determine
* @return calendar_type_plugin_* the created calendar_type class
* @throws coding_exception if the calendar type file could not be loaded
*/
static function factory($type = null) {
if (is_null($type)) {
$type = self::get_calendar_type();
}
$class = "\\calendartype_$type\\structure";
return new $class();
}
/**
* Returns a list of calendar typess available for use.
*
* @return array the list of calendar types
*/
static function get_list_of_calendar_types() {
$calendars = array();
$calendardirs = \core_component::get_plugin_list('calendartype');
foreach ($calendardirs as $name => $location) {
$calendars[$name] = get_string('name', "calendartype_{$name}");
}
return $calendars;
}
/**
* Returns the current calendar type in use.
*
* @return string the current calendar type being used
*/
static function get_calendar_type() {
global $CFG, $USER, $SESSION, $COURSE;
if (!empty($COURSE->id) and $COURSE->id != SITEID and !empty($COURSE->calendartype)) { // Course calendartype can override all other settings for this page.
$return = $COURSE->calendartype;
} else if (!empty($SESSION->calendartype)) { // Session calendartype can override other settings.
$return = $SESSION->calendartype;
} else if (!empty($USER->calendartype)) {
$return = $USER->calendartype;
} else if (!empty($CFG->calendartype)) {
$return = $CFG->calendartype;
} else {
$return = 'gregorian';
}
return $return;
}
}

View File

@ -15,6 +15,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace calendartype_gregorian;
use core_calendar\type_base;
/**
* Handles calendar functions for the gregorian calendar.
*
@ -24,7 +27,7 @@
* @copyright 2008 onwards Foodle Group {@link http://foodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_type_plugin_gregorian extends calendar_type_plugin_base {
class structure extends type_base {
/**
* Returns a list of all the possible days for all months.

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2013062000; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2013070300; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2012120300; // Requires this Moodle version.
$plugin->component = 'calendartype_gregorian'; // Full name of the plugin (used for diagnostics).

View File

@ -201,7 +201,7 @@ class course_edit_form extends moodleform {
$calendartypes = array();
$calendartypes[''] = get_string('forceno');
$calendartypes += core_calendar\type_factory::get_list_of_calendar_types();
$calendartypes += \core_calendar\type_factory::get_list_of_calendar_types();
$mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendartypes);
$options = range(0, 10);

View File

@ -78,7 +78,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
*/
function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
// Get the calendar type used - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
$this->_options = array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(),
'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);
@ -99,7 +99,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
}
}
// The YUI2 calendar only supports the gregorian calendar type.
if (calendar_type_plugin_factory::get_calendar_type() === 'gregorian') {
if (\core_calendar\type_factory::get_calendar_type() === 'gregorian') {
form_init_date_js();
}
}
@ -113,7 +113,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
global $OUTPUT;
// Get the calendar type used - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
$days = $calendartype->get_days();
$months = $calendartype->get_months();
for ($i = $this->_options['startyear']; $i <= $this->_options['stopyear']; $i++) {
@ -126,7 +126,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
$this->_elements[] = @MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if (calendar_type_plugin_factory::get_calendar_type() === 'gregorian') {
if (\core_calendar\type_factory::get_calendar_type() === 'gregorian') {
$this->_elements[] = @MoodleQuickForm::createElement('image', 'calendar', $OUTPUT->pix_url('i/calendar', 'moodle'),
array('title' => get_string('calendar', 'calendar'), 'class' => 'visibleifjs'));
}
@ -269,7 +269,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
}
}
// Get the calendar type used - see MDL-18375.
$calendartype = core_calendar\type_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
$gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'], $valuearray['month'], $valuearray['day']);
$value[$this->getName()] = make_timestamp($gregoriandate['year'],
$gregoriandate['month'],

View File

@ -80,7 +80,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
*/
function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
// Get the calendar type used - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
$this->_options = array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(),
'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);
@ -101,7 +101,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
}
}
// The YUI2 calendar only supports the gregorian calendar type.
if (calendar_type_plugin_factory::get_calendar_type() === 'gregorian') {
if (\core_calendar\type_factory::get_calendar_type() === 'gregorian') {
form_init_date_js();
}
}
@ -115,7 +115,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
global $OUTPUT;
// Get the calendar type used - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
$days = $calendartype->get_days();
$months = $calendartype->get_months();
for ($i = $this->_options['startyear']; $i <= $this->_options['stopyear']; $i++) {
@ -141,7 +141,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
$this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
}
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if (calendar_type_plugin_factory::get_calendar_type() === 'gregorian') {
if (\core_calendar\type_factory::get_calendar_type() === 'gregorian') {
$this->_elements[] = @MoodleQuickForm::createElement('image', 'calendar', $OUTPUT->pix_url('i/calendar', 'moodle'),
array('title' => get_string('calendar', 'calendar'), 'class' => 'visibleifjs'));
}
@ -291,7 +291,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
}
}
// Get the calendar type used - see MDL-18375.
$calendartype = core_calendar\type_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
$gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'],
$valuearray['month'],
$valuearray['day'],

View File

@ -2158,7 +2158,7 @@ function format_time($totalsecs, $str = null) {
* @return string the formatted date/time.
*/
function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour = true) {
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
return $calendartype->userdate($date, $format, $timezone, $fixday, $fixhour);
}
@ -2213,7 +2213,7 @@ function date_format_string($date, $format, $tz = 99) {
* @return array An array that represents the date in user time
*/
function usergetdate($time, $timezone = 99) {
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
return $calendartype->usergetdate($time, $timezone);
}

View File

@ -561,7 +561,6 @@ require_once($CFG->libdir .'/editorlib.php'); // All text editor related f
require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions
require_once($CFG->libdir .'/modinfolib.php'); // Cached information on course-module instances
require_once($CFG->dirroot.'/cache/lib.php'); // Cache API
require_once($CFG->dirroot.'/calendar/type/calendartype.class.php'); // Calendar type.
// make sure PHP is not severly misconfigured
setup_validate_php_configuration();

View File

@ -263,7 +263,7 @@ function useredit_shared_definition(&$mform, $editoroptions = null, $filemanager
$mform->setDefault('lang', $CFG->lang);
// Multi-Calendar Support - see MDL-18375.
$mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), calendar_type_plugin_factory::get_list_of_calendar_types());
$mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), \core_calendar\type_factory::get_list_of_calendar_types());
if (!empty($CFG->allowuserthemes)) {
$choices = array();

View File

@ -32,7 +32,7 @@ class profile_define_datetime extends profile_define_base {
*/
public function define_form_specific($form) {
// Get the current calendar in use - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
// Create variables to store start and end.
list($year, $month, $day) = explode('_', date('Y_m_d'));
@ -88,7 +88,7 @@ class profile_define_datetime extends profile_define_base {
*/
public function define_after_data(&$mform) {
// Get the current calendar in use - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
// The start and end year will be set as a Gregorian year in the DB. We want
// to convert these to the equivalent year in the current calendar system.
@ -115,7 +115,7 @@ class profile_define_datetime extends profile_define_base {
*/
public function define_save_preprocess($data) {
// Get the current calendar in use - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
// Ensure the years are saved as Gregorian in the database.
$startdate = $calendartype->convert_to_gregorian($data->param1, 1, 1);

View File

@ -32,7 +32,7 @@ class profile_field_datetime extends profile_field_base {
*/
public function edit_field_add($mform) {
// Get the current calendar in use - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$calendartype = \core_calendar\type_factory::factory();
// Check if the field is required.
if ($this->field->required) {