Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'MDL-18375_master' of git://github.com/markn86/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Sep 9, 2013
2 parents d95e842 + 5ca04d0 commit 1320c31
Show file tree
Hide file tree
Showing 24 changed files with 1,325 additions and 235 deletions.
116 changes: 116 additions & 0 deletions calendar/classes/type_base.php
@@ -0,0 +1,116 @@
<?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;

/**
* Defines functions used by calendar type plugins.
*
* This library provides a unified interface for calendar types.
*
* @package core_calendar
* @copyright 2008 onwards Foodle Group {@link http://foodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class type_base {

/**
* Returns a list of all the possible days for all months.
*
* This is used to generate the select box for the days
* in the date selector elements. Some months contain more days
* than others so this function should return all possible days as
* we can not predict what month will be chosen (the user
* may have JS turned off and we need to support this situation in
* Moodle).
*
* @return array the days
*/
public abstract function get_days();

/**
* Returns a list of all the names of the months.
*
* @return array the month names
*/
public abstract function get_months();

/**
* Returns the minimum year of the calendar.
*
* @return int the minumum year
*/
public abstract function get_min_year();

/**
* Returns the maximum year of the calendar.
*
* @return int the max year
*/
public abstract function get_max_year();

/**
* Returns a formatted string that represents a date in user time.
*
* @param int $date the timestamp in UTC, as obtained from the database
* @param string $format strftime format
* @param int|float|string $timezone the timezone to use
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @param bool $fixday if true then the leading zero from %d is removed,
* if false then the leading zero is maintained
* @param bool $fixhour if true then the leading zero from %I is removed,
* if false then the leading zero is maintained
* @return string the formatted date/time
*/
public abstract function timestamp_to_date_string($date, $format, $timezone, $fixday, $fixhour);

/**
* Given a $time timestamp in GMT (seconds since epoch), returns an array that represents
* the date in user time.
*
* @param int $time timestamp in GMT
* @param float|int|string $timezone the timezone to use to calculate the time
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @return array an array that represents the date in user time
*/
public abstract function timestamp_to_date_array($time, $timezone);

/**
* Provided with a day, month, year, hour and minute in the specific
* calendar type convert it into the equivalent Gregorian date.
*
* @param int $year
* @param int $month
* @param int $day
* @param int $hour
* @param int $minute
* @return array the converted day, month and year.
*/
public abstract function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0);

/**
* Provided with a day, month, year, hour and minute in a Gregorian date
* convert it into the specific calendar type date.
*
* @param int $year
* @param int $month
* @param int $day
* @param int $hour
* @param int $minute
* @return array the converted day, month and year.
*/
public abstract function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0);
}
91 changes: 91 additions & 0 deletions calendar/classes/type_factory.php
@@ -0,0 +1,91 @@
<?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}.
*
* @package core_calendar
* @copyright 2008 onwards Foodle Group {@link http://foodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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 calendartype_* the created calendar_type class
* @throws coding_exception if the calendar type file could not be loaded
*/
public static function get_calendar_instance($type = null) {
if (is_null($type)) {
$type = self::get_calendar_type();
}

$class = "\\calendartype_$type\\structure";

// Ensure the calendar type exists. It may occur that a user has selected a calendar type, which was then
// deleted. If this happens we want to fall back on the Gregorian calendar type.
if (!class_exists($class)) {
$class = "\\calendartype_gregorian\\structure";
}

return new $class();
}

/**
* Returns a list of calendar typess available for use.
*
* @return array the list of calendar types
*/
public 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
*/
public 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;
}
}

0 comments on commit 1320c31

Please sign in to comment.