Skip to content

Commit

Permalink
MDL-18375 calendar: implemented the usage of the recently introduced …
Browse files Browse the repository at this point in the history
…automatic class loading
  • Loading branch information
mdjnelson committed Sep 5, 2013
1 parent 84423bd commit 022745a
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -232,73 +234,3 @@ function usergetdate($time, $timezone) {
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;
}
}

81 changes: 81 additions & 0 deletions calendar/classes/type_factory.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion calendar/type/gregorian/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 1 addition & 1 deletion course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function definition() {

$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);
Expand Down
10 changes: 5 additions & 5 deletions lib/form/dateselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -99,7 +99,7 @@ function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null
}
}
// 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();
}
}
Expand All @@ -113,7 +113,7 @@ function _createElements() {
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++) {
Expand All @@ -126,7 +126,7 @@ function _createElements() {
$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'));
}
Expand Down Expand Up @@ -269,7 +269,7 @@ function exportValue(&$submitValues, $assoc = false) {
}
}
// 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'],
Expand Down
10 changes: 5 additions & 5 deletions lib/form/datetimeselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -101,7 +101,7 @@ function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel =
}
}
// 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();
}
}
Expand All @@ -115,7 +115,7 @@ function _createElements() {
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++) {
Expand All @@ -141,7 +141,7 @@ function _createElements() {
$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'));
}
Expand Down Expand Up @@ -291,7 +291,7 @@ function exportValue(&$submitValues, $assoc = false) {
}
}
// 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'],
Expand Down
4 changes: 2 additions & 2 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion lib/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@
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();
Expand Down
2 changes: 1 addition & 1 deletion user/editlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions user/profile/field/datetime/define.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -88,7 +88,7 @@ public function define_validate_specific($data, $files) {
*/
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.
Expand All @@ -115,7 +115,7 @@ public function define_after_data(&$mform) {
*/
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);
Expand Down
2 changes: 1 addition & 1 deletion user/profile/field/datetime/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 022745a

Please sign in to comment.