MDL-18375 calendar: huge refactor of the initial patch

1) Moved the calendar types location to a sub-folder in the calendar directory.
2) Removed/moved language strings.
3) Removed calendar types that should be downloaded as plugins.
4) Removed a Non-English language pack for the Gregorian calendar type that
should be downloaded separately.
5) Removed custom files responsible for checking for updates and uninstalling
calendar types, which should be done by core.
6) Removed JS from the calendar_month block as there is no non-JS alternative
provided and the JS written does not make use of the YUI library to ensure
multiple browser support.
7) Removed code from the base class responsible for creating timestamps that
are saved in the DB.
8) Added PHPDocs.

Note: In the original patch we are editing core functions responsible for saving
time in the database in the calendar base class. This is very dangerous, we do
not want to touch these functions as it could cause a complete fubar of the
database. There are places we are forcing the use of the gregorian calendar as
we are passing dates generated by the PHP date function, where as sometimes
we pass dates from usergetdate (which was being overwritten to return the
date specific to the calendar). We can not expect third party modules to
change the calendar type depending on the format they pass to these functions.
This commit is contained in:
Mark Nelson 2013-06-10 17:35:55 +08:00
parent 6dd59aabfa
commit 2f00e1b245
45 changed files with 606 additions and 1931 deletions

View file

@ -39,12 +39,10 @@ require_once($CFG->libdir . '/formslib.php');
* @copyright 2007 Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_date_selector extends MoodleQuickForm_group
{
class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
/**
* Control the fieldnames for form elements
*
* MDL-18375, Multi-Calendar Support
* Control the fieldnames for form elements.
*
* startyear => int start of range of years that can be selected
* stopyear => int last year that can be selected
@ -78,13 +76,11 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
* @param array $options Options to control the element's display
* @param mixed $attributes Either a typical HTML attribute string or an associative array
*/
function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
{
// MDL-18375, Multi-Calendar Support
global $CALENDARSYSTEM;
$this->_options = array('startyear'=> $CALENDARSYSTEM->get_min_year(), 'stopyear'=>$CALENDARSYSTEM->get_max_year(),
'timezone'=>99, 'optional'=>false);
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();
$this->_options = array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(),
'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);
$this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
@ -111,18 +107,18 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
*
* @access private
*/
function _createElements()
{
global $OUTPUT, $CALENDARSYSTEM;
function _createElements() {
global $OUTPUT;
$this->_elements = array();
for ($i=1; $i<=31; $i++) {
$days[$i] = $i;
}
$months = $CALENDARSYSTEM->get_month_names();
for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
// Get the calendar type used - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$days = $calendartype->get_days();
$months = $calendartype->get_months();
for ($i = $this->_options['startyear']; $i <= $this->_options['stopyear']; $i++) {
$years[$i] = $i;
}
$this->_elements = array();
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = @MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
@ -149,8 +145,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
* @param object $caller calling object
* @return bool
*/
function onQuickFormEvent($event, $arg, &$caller)
{
function onQuickFormEvent($event, $arg, &$caller) {
switch ($event) {
case 'updateValue':
// constant values override both default and submitted ones
@ -176,7 +171,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
'month' => $currentdate['mon'],
'year' => $currentdate['year']);
// If optional, default to off, unless a date was provided
if($this->_options['optional']) {
if($this->_options['optional']) {
$value['enabled'] = $requestvalue != 0;
}
} else {
@ -216,8 +211,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
*
* @return string
*/
function toHtml()
{
function toHtml() {
include_once('HTML/QuickForm/Renderer/Default.php');
$renderer = new HTML_QuickForm_Renderer_Default();
$renderer->setElementTemplate('{element}');
@ -241,8 +235,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
* @param bool $required Whether a group is required
* @param string $error An error message associated with a group
*/
function accept(&$renderer, $required = false, $error = null)
{
function accept(&$renderer, $required = false, $error = null) {
$renderer->renderElement($this, $required, $error);
}
@ -253,8 +246,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
* @param bool $assoc specifies if returned array is associative
* @return array
*/
function exportValue(&$submitValues, $assoc = false)
{
function exportValue(&$submitValues, $assoc = false) {
$value = null;
$valuearray = array();
foreach ($this->_elements as $element){
@ -271,13 +263,15 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
return $value;
}
}
$value[$this->getName()] = make_timestamp($valuearray['year'],
$valuearray['month'],
$valuearray['day'],
0, 0, 0,
$this->_options['timezone'],
true);
// Get the calendar type used - see MDL-18375.
$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'],
$gregoriandate['day'],
0, 0, 0,
$this->_options['timezone'],
true);
return $value;
} else {

View file

@ -39,11 +39,10 @@ require_once($CFG->libdir . '/formslib.php');
* @copyright 2006 Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
/**
* Options for the element
*
* MDL-18375, Multi-Calendar Support
* Options for the element.
*
* startyear => int start of range of years that can be selected
* stopyear => int last year that can be selected
@ -79,13 +78,11 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
* @param array $options Options to control the element's display
* @param mixed $attributes Either a typical HTML attribute string or an associative array
*/
function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
{
// MDL-18375, Multi-Calendar Support
global $CALENDARSYSTEM;
$this->_options = array('startyear'=> $CALENDARSYSTEM->get_min_year(), 'stopyear'=>$CALENDARSYSTEM->get_max_year(),
'defaulttime' => 0, 'timezone'=>99, 'step'=>5, 'optional'=>false);
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();
$this->_options = array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(),
'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);
$this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
@ -112,16 +109,14 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
*
* @access private
*/
function _createElements()
{
global $OUTPUT, $CALENDARSYSTEM;
function _createElements() {
global $OUTPUT;
$this->_elements = array();
for ($i=1; $i<=31; $i++) {
$days[$i] = $i;
}
$months = $CALENDARSYSTEM->get_month_names();
for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
// Get the calendar type used - see MDL-18375.
$calendartype = calendar_type_plugin_factory::factory();
$days = $calendartype->get_days();
$months = $calendartype->get_months();
for ($i = $this->_options['startyear']; $i <= $this->_options['stopyear']; $i++) {
$years[$i] = $i;
}
for ($i=0; $i<=23; $i++) {
@ -130,6 +125,8 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
for ($i=0; $i<60; $i+=$this->_options['step']) {
$minutes[$i] = sprintf("%02d",$i);
}
$this->_elements = array();
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = @MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
@ -163,8 +160,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
* @param object $caller calling object
* @return bool
*/
function onQuickFormEvent($event, $arg, &$caller)
{
function onQuickFormEvent($event, $arg, &$caller) {
switch ($event) {
case 'updateValue':
// constant values override both default and submitted ones
@ -237,8 +233,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
*
* @return string
*/
function toHtml()
{
function toHtml() {
include_once('HTML/QuickForm/Renderer/Default.php');
$renderer = new HTML_QuickForm_Renderer_Default();
$renderer->setElementTemplate('{element}');
@ -262,8 +257,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
* @param bool $required Whether a group is required
* @param string $error An error message associated with a group
*/
function accept(&$renderer, $required = false, $error = null)
{
function accept(&$renderer, $required = false, $error = null) {
$renderer->renderElement($this, $required, $error);
}
@ -274,8 +268,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
* @param bool $assoc specifies if returned array is associative
* @return array
*/
function exportValue(&$submitValues, $assoc = false)
{
function exportValue(&$submitValues, $assoc = false) {
$value = null;
$valuearray = array();
foreach ($this->_elements as $element){
@ -292,20 +285,24 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
return $value;
}
}
$valuearray=$valuearray + array('year' => 1970, 'month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0);
$value[$this->getName()] = make_timestamp(
$valuearray['year'],
$valuearray['month'],
$valuearray['day'],
$valuearray['hour'],
$valuearray['minute'],
0,
$this->_options['timezone'],
true);
// Get the calendar type used - see MDL-18375.
$calendartype = core_calendar\type_factory::factory();
$gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'],
$valuearray['month'],
$valuearray['day'],
$valuearray['hour'],
$valuearray['minute']);
$value[$this->getName()] = make_timestamp($gregoriandate['year'],
$gregoriandate['month'],
$gregoriandate['day'],
$gregoriandate['hour'],
$gregoriandate['minute'],
0,
$this->_options['timezone'],
true);
return $value;
} else {
return null;
}
}