Skip to content

Commit

Permalink
Merge branch 'MDL-41664_master' of https://github.com/markn86/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski authored and Sam Hemelryk committed Nov 1, 2013
2 parents d7052f7 + 1d43da0 commit 51bd152
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 48 deletions.
29 changes: 25 additions & 4 deletions calendar/classes/type_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,40 @@ public abstract function get_days();
public abstract function get_months();

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

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

/**
* Returns an array of years.
*
* @param int $minyear
* @param int $maxyear
* @return array the years
*/
public abstract function get_years($minyear = null, $maxyear = null);

/**
* 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.
*
* @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_date_order($minyear = null, $maxyear = null);

/**
* Returns the number of days in a week.
*
Expand Down
53 changes: 48 additions & 5 deletions calendar/tests/calendartype_test_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,66 @@ public function get_months() {
}

/**
* Returns the minimum year of the calendar.
* Returns the minimum year for the calendar.
*
* @return int the minumum year
* @return int The minimum year
*/
public function get_min_year() {
return 1970;
return 1900;
}

/**
* Returns the maximum year of the calendar.
* Returns the maximum year for the calendar
*
* @return int the max year
* @return int The maximum year
*/
public function get_max_year() {
return 2050;
}

/**
* Returns an array of years.
*
* @param int $minyear
* @param int $maxyear
* @return array the years.
*/
public function get_years($minyear = null, $maxyear = null) {
if (is_null($minyear)) {
$minyear = $this->get_min_year();
}

if (is_null($maxyear)) {
$maxyear = $this->get_max_year();
}

$years = array();
for ($i = $minyear; $i <= $maxyear; $i++) {
$years[$i] = $i;
}

return $years;
}

/**
* 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.
*
* @param int $minyear The year to start with.
* @param int $maxyear The year to finish with.
* @return array Full date information.
*/
public function get_date_order($minyear = null, $maxyear = null) {
$dateinfo = array();
$dateinfo['day'] = $this->get_days();
$dateinfo['month'] = $this->get_months();
$dateinfo['year'] = $this->get_years($minyear, $maxyear);

return $dateinfo;
}

/**
* Returns the number of days in a week.
*
Expand Down
51 changes: 47 additions & 4 deletions calendar/type/gregorian/classes/structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,66 @@ public function get_months() {
}

/**
* Returns the minimum year of the calendar.
* Returns the minimum year for the calendar.
*
* @return int the minumum year
* @return int The minimum year
*/
public function get_min_year() {
return 1900;
}

/**
* Returns the maximum year of the calendar.
* Returns the maximum year for the calendar
*
* @return int the max year
* @return int The maximum year
*/
public function get_max_year() {
return 2050;
}

/**
* Returns an array of years.
*
* @param int $minyear
* @param int $maxyear
* @return array the years
*/
public function get_years($minyear = null, $maxyear = null) {
if (is_null($minyear)) {
$minyear = $this->get_min_year();
}

if (is_null($maxyear)) {
$maxyear = $this->get_max_year();
}

$years = array();
for ($i = $minyear; $i <= $maxyear; $i++) {
$years[$i] = $i;
}

return $years;
}

/**
* 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.
*
* @param int $minyear The year to start with
* @param int $maxyear The year to finish with
* @return array Full date information
*/
public function get_date_order($minyear = null, $maxyear = null) {
$dateinfo = array();
$dateinfo['day'] = $this->get_days();
$dateinfo['month'] = $this->get_months();
$dateinfo['year'] = $this->get_years($minyear, $maxyear);

return $dateinfo;
}

/**
* Returns the number of days in a week.
*
Expand Down
20 changes: 8 additions & 12 deletions lib/form/dateselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ 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();

/**
* @var array These complement separators, they are appended to the resultant HTML.
Expand All @@ -81,7 +80,6 @@ function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null
$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;
Expand All @@ -98,6 +96,7 @@ function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null
}
}
}

// The YUI2 calendar only supports the gregorian calendar type.
if ($calendartype->get_name() === 'gregorian') {
form_init_date_js();
Expand All @@ -114,17 +113,14 @@ function _createElements() {

// 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->get_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'),
Expand Down
29 changes: 13 additions & 16 deletions lib/form/datetimeselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ 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();

/**
* @var array These complement separators, they are appended to the resultant HTML.
Expand All @@ -82,7 +81,7 @@ function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel =
// 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);
'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false);

$this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
Expand All @@ -100,6 +99,7 @@ function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel =
}
}
}

// The YUI2 calendar only supports the gregorian calendar type.
if ($calendartype->get_name() === 'gregorian') {
form_init_date_js();
Expand All @@ -116,23 +116,20 @@ function _createElements() {

// 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);

for ($i = 0; $i <= 23; $i++) {
$hours[$i] = sprintf("%02d", $i);
}
for ($i=0; $i<60; $i+=$this->_options['step']) {
$minutes[$i] = sprintf("%02d",$i);
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);
$this->_elements[] = @MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
$dateformat = $calendartype->get_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);
Expand Down
8 changes: 1 addition & 7 deletions user/profile/field/datetime/define.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ public function define_form_specific($form) {
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);
Expand Down

0 comments on commit 51bd152

Please sign in to comment.