MDL-41664 core_calendar: replaced class variables that stored minimum and maximum...
authorMark Nelson <markn@moodle.com>
Wed, 30 Oct 2013 04:36:45 +0000 (12:36 +0800)
committerMark Nelson <markn@moodle.com>
Wed, 30 Oct 2013 04:42:12 +0000 (12:42 +0800)
Also made slight changes to the existing functions.

calendar/classes/type_base.php
calendar/tests/calendartype_test_example.php
calendar/type/gregorian/classes/structure.php
lib/form/dateselector.php
lib/form/datetimeselector.php

index 889be20..7367119 100644 (file)
@@ -27,12 +27,6 @@ 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.
      *
@@ -65,11 +59,27 @@ abstract class type_base {
     public abstract function get_months();
 
     /**
-     * Returns a list of all of the years being used.
+     * Returns the minimum year for the calendar.
+     *
+     * @return int The minimum year
+     */
+    public abstract function get_min_year();
+
+    /**
+     * Returns the maximum year for the calendar
+     *
+     * @return int The maximum year
+     */
+    public abstract function get_max_year();
+
+    /**
+     * Returns an array of years.
      *
-     * @return array the years.
+     * @param int $minyear
+     * @param int $maxyear
+     * @return array the years
      */
-    public abstract function get_years();
+    public abstract function get_years($minyear = null, $maxyear = null);
 
     /**
      * Returns a multidimensional array with information for day, month, year
@@ -77,11 +87,11 @@ abstract class type_base {
      * 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.
+     * @param int $minyear The year to start with
+     * @param int $maxyear The year to finish with
+     * @return array Full date information
      */
-    public abstract function date_order($minyear = null, $maxyear = null);
+    public abstract function get_date_order($minyear = null, $maxyear = null);
 
     /**
      * Returns the number of days in a week.
index fd74208..e596edf 100644 (file)
@@ -29,9 +29,6 @@ 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.
      *
@@ -79,14 +76,41 @@ class structure extends type_base {
     }
 
     /**
-     * Returns a list of all of the years being used.
+     * Returns the minimum year for the calendar.
+     *
+     * @return int The minimum year
+     */
+    public function get_min_year() {
+        return 1900;
+    }
+
+    /**
+     * Returns the maximum year for the calendar
+     *
+     * @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() {
-        $years = array();
+    public function get_years($minyear = null, $maxyear = null) {
+        if (is_null($minyear)) {
+            $minyear = $this->get_min_year();
+        }
 
-        for ($i = $this->minyear; $i <= $this->maxyear; $i++) {
+        if (is_null($maxyear)) {
+            $maxyear = $this->get_max_year();
+        }
+
+        $years = array();
+        for ($i = $minyear; $i <= $maxyear; $i++) {
             $years[$i] = $i;
         }
 
@@ -103,17 +127,11 @@ class structure extends type_base {
      * @param int $maxyear The year to finish with.
      * @return array Full date information.
      */
-    public function date_order($minyear = null, $maxyear = null) {
-        if (!empty($minyear)) {
-            $this->minyear = $minyear;
-        }
-        if (!empty($maxyear)) {
-            $this->maxyear = $maxyear;
-        }
+    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();
+        $dateinfo['year'] = $this->get_years($minyear, $maxyear);
 
         return $dateinfo;
     }
index dab81ce..0c9ee7d 100644 (file)
@@ -76,14 +76,41 @@ class structure extends type_base {
     }
 
     /**
-     * Returns a list of all of the years being used.
+     * Returns the minimum year for the calendar.
      *
-     * @return array the years.
+     * @return int The minimum year
      */
-    public function get_years() {
-        $years = array();
+    public function get_min_year() {
+        return 1900;
+    }
 
-        for ($i = $this->minyear; $i <= $this->maxyear; $i++) {
+    /**
+     * Returns the maximum year for the calendar
+     *
+     * @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;
         }
 
@@ -96,21 +123,15 @@ class structure extends type_base {
      * 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.
+     * @param int $minyear The year to start with
+     * @param int $maxyear The year to finish with
+     * @return array Full date information
      */
-    public function date_order($minyear = null, $maxyear = null) {
-        if (!empty($minyear)) {
-            $this->minyear = $minyear;
-        }
-        if (!empty($maxyear)) {
-            $this->maxyear = $maxyear;
-        }
+    public function get_date_order($minyear = null, $maxyear = null) {
         $dateinfo = array();
         $dateinfo['day'] = $this->get_days();
-        $dateinfo['year'] = $this->get_years();
         $dateinfo['month'] = $this->get_months();
+        $dateinfo['year'] = $this->get_years($minyear, $maxyear);
 
         return $dateinfo;
     }
index 12259db..9094d02 100644 (file)
@@ -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, 'defaulttime' => 0,
-            'timezone' => 99, 'step' => 5, 'optional' => false);
+    protected $_options = array();
 
     /**
      * @var array These complement separators, they are appended to the resultant HTML.
@@ -77,6 +76,10 @@ 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;
@@ -93,8 +96,7 @@ 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,7 +116,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
 
         $this->_elements = array();
 
-        $dateformat = $calendartype->date_order($this->_options['startyear'], $this->_options['stopyear']);
+        $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);
index b6f0f57..ff2fa99 100644 (file)
@@ -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
      */
-    protected $_options = array('startyear' => null, 'stopyear' => null, 'defaulttime' => 0,
-            'timezone' => 99, 'step' => 5, 'optional' => false);
+    protected $_options = array();
 
     /**
      * @var array These complement separators, they are appended to the resultant HTML.
@@ -79,6 +78,11 @@ 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;
@@ -95,8 +99,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();
+
         // The YUI2 calendar only supports the gregorian calendar type.
         if ($calendartype->get_name() === 'gregorian') {
             form_init_date_js();
@@ -114,15 +117,15 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
         // Get the calendar type used - see MDL-18375.
         $calendartype = \core_calendar\type_factory::get_calendar_instance();
 
-        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();
-        $dateformat = $calendartype->date_order($this->_options['startyear'], $this->_options['stopyear']);
+        $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);