Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace calendartype_test_example;
  18  use \core_calendar\type_base;
  19  
  20  /**
  21   * Handles calendar functions for the test calendar.
  22   *
  23   * The test calendar is going to be 2 years, 2 days, 2 hours and 2 minutes
  24   * in the future of the Gregorian calendar.
  25   *
  26   * @package core_calendar
  27   * @copyright 2013 Mark Nelson <markn@moodle.com>
  28   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class structure extends type_base {
  31  
  32      /**
  33       * Returns the name of the calendar.
  34       *
  35       * @return string the calendar name
  36       */
  37      public function get_name() {
  38          return 'test_example';
  39      }
  40  
  41      /**
  42       * Returns a list of all the possible days for all months.
  43       *
  44       * This is used to generate the select box for the days
  45       * in the date selector elements. Some months contain more days
  46       * than others so this function should return all possible days as
  47       * we can not predict what month will be chosen (the user
  48       * may have JS turned off and we need to support this situation in
  49       * Moodle).
  50       *
  51       * @return array the days
  52       */
  53      public function get_days() {
  54          $days = array();
  55  
  56          for ($i = 1; $i <= 31; $i++) {
  57              $days[$i] = $i;
  58          }
  59  
  60          return $days;
  61      }
  62  
  63      /**
  64       * Returns a list of all the names of the months.
  65       *
  66       * @return array the month names
  67       */
  68      public function get_months() {
  69          $months = array();
  70  
  71          for ($i = 1; $i <= 12; $i++) {
  72              $months[$i] = $i;
  73          }
  74  
  75          return $months;
  76      }
  77  
  78      /**
  79       * Returns the minimum year for the calendar.
  80       *
  81       * @return int The minimum year
  82       */
  83      public function get_min_year() {
  84          return 1900;
  85      }
  86  
  87      /**
  88       * Returns the maximum year for the calendar
  89       *
  90       * @return int The maximum year
  91       */
  92      public function get_max_year() {
  93          return 2050;
  94      }
  95  
  96      /**
  97       * Returns an array of years.
  98       *
  99       * @param int $minyear
 100       * @param int $maxyear
 101       * @return array the years.
 102       */
 103      public function get_years($minyear = null, $maxyear = null) {
 104          if (is_null($minyear)) {
 105              $minyear = $this->get_min_year();
 106          }
 107  
 108          if (is_null($maxyear)) {
 109              $maxyear = $this->get_max_year();
 110          }
 111  
 112          $years = array();
 113          for ($i = $minyear; $i <= $maxyear; $i++) {
 114              $years[$i] = $i;
 115          }
 116  
 117          return $years;
 118      }
 119  
 120      /**
 121       * Returns a multidimensional array with information for day, month, year
 122       * and the order they are displayed when selecting a date.
 123       * The order in the array will be the order displayed when selecting a date.
 124       * Override this function to change the date selector order.
 125       *
 126       * @param int $minyear The year to start with.
 127       * @param int $maxyear The year to finish with.
 128       * @return array Full date information.
 129       */
 130      public function get_date_order($minyear = null, $maxyear = null) {
 131          $dateinfo = array();
 132          $dateinfo['day'] = $this->get_days();
 133          $dateinfo['month'] = $this->get_months();
 134          $dateinfo['year'] = $this->get_years($minyear, $maxyear);
 135  
 136          return $dateinfo;
 137      }
 138  
 139      /**
 140       * Returns the number of days in a week.
 141       *
 142       * @return int the number of days
 143       */
 144      public function get_num_weekdays() {
 145          return 7;
 146      }
 147  
 148      /**
 149       * Returns an indexed list of all the names of the weekdays.
 150       *
 151       * The list starts with the index 0. Each index, representing a
 152       * day, must be an array that contains the indexes 'shortname'
 153       * and 'fullname'.
 154       *
 155       * @return array array of days
 156       */
 157      public function get_weekdays() {
 158          return '';
 159      }
 160  
 161      /**
 162       * Returns the index of the starting week day.
 163       *
 164       * @return int
 165       */
 166      public function get_starting_weekday() {
 167          return '';
 168      }
 169  
 170      /**
 171       * Returns the index of the weekday for a specific calendar date.
 172       *
 173       * @param int $year
 174       * @param int $month
 175       * @param int $day
 176       * @return int
 177       */
 178      public function get_weekday($year, $month, $day) {
 179          return '';
 180      }
 181  
 182      /**
 183       * Returns the number of days in a given month.
 184       *
 185       * @param int $year
 186       * @param int $month
 187       * @return int the number of days
 188       */
 189      public function get_num_days_in_month($year, $month) {
 190          return '';
 191      }
 192  
 193      /**
 194       * Get the previous month.
 195       *
 196       * If the current month is January, it will get the last month of the previous year.
 197       *
 198       * @param int $year
 199       * @param int $month
 200       * @return array previous month and year
 201       */
 202      public function get_prev_month($year, $month) {
 203          return '';
 204      }
 205  
 206      /**
 207       * Get the next month.
 208       *
 209       * If the current month is December, it will get the first month of the following year.
 210       *
 211       * @param int $year
 212       * @param int $month
 213       * @return array the following month and year
 214       */
 215      public function get_next_month($year, $month) {
 216          return '';
 217      }
 218  
 219      /**
 220       * Returns a formatted string that represents a date in user time.
 221       *
 222       * @param int $time the timestamp in UTC, as obtained from the database
 223       * @param string $format strftime format
 224       * @param int|float|string $timezone the timezone to use
 225       *        {@link https://moodledev.io/docs/apis/subsystems/time#timezone}
 226       * @param bool $fixday if true then the leading zero from %d is removed,
 227       *        if false then the leading zero is maintained
 228       * @param bool $fixhour if true then the leading zero from %I is removed,
 229       *        if false then the leading zero is maintained
 230       * @return string the formatted date/time
 231       */
 232      public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) {
 233          return '';
 234      }
 235  
 236      /**
 237       * Given a $time timestamp in GMT (seconds since epoch), returns an array that represents
 238       * the date in user time.
 239       *
 240       * @param int $time timestamp in GMT
 241       * @param float|int|string $timezone the timezone to use to calculate the time
 242       *        {@link https://moodledev.io/docs/apis/subsystems/time#timezone}
 243       * @return array an array that represents the date in user time
 244       */
 245      public function timestamp_to_date_array($time, $timezone = 99) {
 246          $gregoriancalendar = \core_calendar\type_factory::get_calendar_instance('gregorian');
 247          $date = $gregoriancalendar->timestamp_to_date_array($time, $timezone);
 248          $newdate = $this->convert_from_gregorian($date['year'], $date['mon'], $date['mday'],
 249              $date['hours'], $date['minutes']);
 250  
 251          $date['year'] = $newdate['year'];
 252          $date['mon'] = $newdate['month'];
 253          $date['mday'] = $newdate['day'];
 254          $date['hours'] = $newdate['hour'];
 255          $date['minutes']  = $newdate['minute'];
 256  
 257          return $date;
 258      }
 259  
 260      /**
 261       * Provided with a day, month, year, hour and minute
 262       * convert it into the equivalent Gregorian date.
 263       *
 264       * @param int $year
 265       * @param int $month
 266       * @param int $day
 267       * @param int $hour
 268       * @param int $minute
 269       * @return array the converted day, month, year, hour and minute.
 270       */
 271      public function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
 272          $timestamp = make_timestamp($year, $month, $day, $hour, $minute);
 273          $date = date('Y/n/j/H/i', strtotime('-2 year, -2 months, -2 days, -2 hours, -2 minutes', $timestamp));
 274  
 275          list($year, $month, $day, $hour, $minute) = explode('/', $date);
 276  
 277          return array('year' => (int) $year,
 278              'month' => (int) $month,
 279              'day' => (int) $day,
 280              'hour' => (int) $hour,
 281              'minute' => (int) $minute);
 282  
 283      }
 284  
 285      /**
 286       * Provided with a day, month, year, hour and minute in a Gregorian date
 287       * convert it into the specific calendar type date.
 288       *
 289       * @param int $year
 290       * @param int $month
 291       * @param int $day
 292       * @param int $hour
 293       * @param int $minute
 294       * @return array the converted day, month, year, hour and minute.
 295       */
 296      public function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
 297          $timestamp = make_timestamp($year, $month, $day, $hour, $minute);
 298          $date = date('Y/n/j/H/i', strtotime('+2 year, +2 months, +2 days, +2 hours, +2 minutes', $timestamp));
 299  
 300          list($year, $month, $day, $hour, $minute) = explode('/', $date);
 301  
 302          return array('year' => (int) $year,
 303              'month' => (int) $month,
 304              'day' => (int) $day,
 305              'hour' => (int) $hour,
 306              'minute' => (int) $minute);
 307      }
 308  
 309      /**
 310       * This return locale for windows os.
 311       *
 312       * @return string locale
 313       */
 314      public function locale_win_charset() {
 315          return get_string('localewincharset', 'langconfig');
 316      }
 317  }