MDL-41664 - Calendar: Add method for formating years in the new calendar plugin.

This commit is contained in:
Adrian Greeve 2013-09-09 11:36:25 +08:00 committed by Mark Nelson
parent 68291f2d57
commit 73412d9680
6 changed files with 101 additions and 61 deletions

View file

@ -27,6 +27,12 @@ namespace core_calendar;
*/
abstract class type_base {
/** string $minyear Minimum year we are using. */
protected $minyear = 1900;
/** string $maxyear Maximum year we are using. */
protected $maxyear = 2050;
/**
* Returns the name of the calendar.
*
@ -59,18 +65,23 @@ abstract class type_base {
public abstract function get_months();
/**
* Returns the minimum year of the calendar.
* Returns a list of all of the years being used.
*
* @return int the minumum year
* @return array the years.
*/
public abstract function get_min_year();
public abstract function get_years();
/**
* Returns the maximum year of the calendar.
* Returns a multidimensional array with information for day, month, year
* and the order they are displayed when selecting a date.
* The order in the array will be the order displayed when selecting a date.
* Override this function to change the date selector order.
*
* @return int the max year
* @param int $minyear The year to start with.
* @param int $maxyear The year to finish with.
* @return array Full date information.
*/
public abstract function get_max_year();
public abstract function date_order($minyear = null, $maxyear = null);
/**
* Returns the number of days in a week.

View file

@ -29,6 +29,9 @@ use \core_calendar\type_base;
*/
class structure extends type_base {
/** string $minyear Minimum year we are using. */
protected $minyear = 1970;
/**
* Returns the name of the calendar.
*
@ -76,21 +79,43 @@ class structure extends type_base {
}
/**
* Returns the minimum year of the calendar.
* Returns a list of all of the years being used.
*
* @return int the minumum year
* @return array the years.
*/
public function get_min_year() {
return 1970;
public function get_years() {
$years = array();
for ($i = $this->minyear; $i <= $this->maxyear; $i++) {
$years[$i] = $i;
}
return $years;
}
/**
* Returns the maximum year of the calendar.
* Returns a multidimensional array with information for day, month, year
* and the order they are displayed when selecting a date.
* The order in the array will be the order displayed when selecting a date.
* Override this function to change the date selector order.
*
* @return int the max year
* @param int $minyear The year to start with.
* @param int $maxyear The year to finish with.
* @return array Full date information.
*/
public function get_max_year() {
return 2050;
public function date_order($minyear = null, $maxyear = null) {
if (!empty($minyear)) {
$this->minyear = $minyear;
}
if (!empty($maxyear)) {
$this->maxyear = $maxyear;
}
$dateinfo = array();
$dateinfo['day'] = $this->get_days();
$dateinfo['month'] = $this->get_months();
$dateinfo['year'] = $this->get_years();
return $dateinfo;
}
/**

View file

@ -76,21 +76,43 @@ class structure extends type_base {
}
/**
* Returns the minimum year of the calendar.
* Returns a list of all of the years being used.
*
* @return int the minumum year
* @return array the years.
*/
public function get_min_year() {
return 1900;
public function get_years() {
$years = array();
for ($i = $this->minyear; $i <= $this->maxyear; $i++) {
$years[$i] = $i;
}
return $years;
}
/**
* Returns the maximum year of the calendar.
* Returns a multidimensional array with information for day, month, year
* and the order they are displayed when selecting a date.
* The order in the array will be the order displayed when selecting a date.
* Override this function to change the date selector order.
*
* @return int the max year
* @param int $minyear The year to start with.
* @param int $maxyear The year to finish with.
* @return array Full date information.
*/
public function get_max_year() {
return 2050;
public function date_order($minyear = null, $maxyear = null) {
if (!empty($minyear)) {
$this->minyear = $minyear;
}
if (!empty($maxyear)) {
$this->maxyear = $maxyear;
}
$dateinfo = array();
$dateinfo['day'] = $this->get_days();
$dateinfo['year'] = $this->get_years();
$dateinfo['month'] = $this->get_months();
return $dateinfo;
}
/**

View file

@ -53,8 +53,8 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
* optional => if true, show a checkbox beside the date to turn it on (or off)
* @var array
*/
protected $_options = array('startyear' => null, 'stopyear' => null,
'timezone' => null, 'optional' => null);
protected $_options = array('startyear' => null, 'stopyear' => null, 'defaulttime' => 0,
'timezone' => 99, 'step' => 5, 'optional' => false);
/**
* @var array These complement separators, they are appended to the resultant HTML.
@ -77,11 +77,6 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
* @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) {
// Get the calendar type used - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$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;
$this->_appendName = true;
@ -98,6 +93,8 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
}
}
}
// Get the calendar type used - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
// The YUI2 calendar only supports the gregorian calendar type.
if ($calendartype->get_name() === 'gregorian') {
form_init_date_js();
@ -114,17 +111,14 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
// Get the calendar type used - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$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);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
$dateformat = $calendartype->date_order($this->_options['startyear'], $this->_options['stopyear']);
foreach ($dateformat as $key => $value) {
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = @MoodleQuickForm::createElement('select', $key, get_string($key, 'form'), $value, $this->getAttributes(), true);
}
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if ($calendartype->get_name() === 'gregorian') {
$this->_elements[] = @MoodleQuickForm::createElement('image', 'calendar', $OUTPUT->pix_url('i/calendar', 'moodle'),

View file

@ -55,8 +55,8 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
* optional => if true, show a checkbox beside the date to turn it on (or off)
* @var array
*/
var $_options = array('startyear' => null, 'stopyear' => null, 'defaulttime' => null,
'timezone' => null, 'step' => null, 'optional' => null);
protected $_options = array('startyear' => null, 'stopyear' => null, 'defaulttime' => 0,
'timezone' => 99, 'step' => 5, 'optional' => false);
/**
* @var array These complement separators, they are appended to the resultant HTML.
@ -79,11 +79,6 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
* @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) {
// Get the calendar type used - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$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;
$this->_appendName = true;
@ -100,6 +95,8 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
}
}
}
// Get the calendar type used - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
// The YUI2 calendar only supports the gregorian calendar type.
if ($calendartype->get_name() === 'gregorian') {
form_init_date_js();
@ -116,11 +113,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
// Get the calendar type used - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$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++) {
$hours[$i] = sprintf("%02d",$i);
}
@ -129,10 +122,11 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
}
$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);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
$dateformat = $calendartype->date_order($this->_options['startyear'], $this->_options['stopyear']);
foreach ($dateformat as $key => $date) {
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = @MoodleQuickForm::createElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true);
}
if (right_to_left()) { // Switch order of elements for Right-to-Left
$this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);

View file

@ -36,14 +36,8 @@ class profile_define_datetime extends profile_define_base {
list($year, $month, $day) = explode('_', date('Y_m_d'));
$currentdate = $calendartype->convert_from_gregorian($year, $month, $day);
$currentyear = $currentdate['year'];
$startyear = $calendartype->get_min_year();
$endyear = $calendartype->get_max_year();
// Create array for the years.
$arryears = array();
for ($i = $startyear; $i <= $endyear; $i++) {
$arryears[$i] = $i;
}
$arryears = $calendartype->get_years();
// Add elements.
$form->addElement('select', 'param1', get_string('startyear', 'profilefield_datetime'), $arryears);