Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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_gregorian;
  18  use core_calendar\type_base;
  19  
  20  /**
  21   * Handles calendar functions for the gregorian calendar.
  22   *
  23   * @package calendartype_gregorian
  24   * @copyright 2008 onwards Foodle Group {@link http://foodle.org}
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class structure extends type_base {
  28  
  29      /**
  30       * Returns the name of the calendar.
  31       *
  32       * This is the non-translated name, usually just
  33       * the name of the folder.
  34       *
  35       * @return string the calendar name
  36       */
  37      public function get_name() {
  38          return 'gregorian';
  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          $date = new \DateTime('@1263556800');
  72          $date->setTimezone(new \DateTimeZone('UTC'));
  73          for ($i = 1; $i <= 12; $i++) {
  74              $date->setDate(2000, $i, 15);
  75              $months[$i] = userdate($date->getTimestamp(), '%B', 'UTC');
  76          }
  77  
  78          return $months;
  79      }
  80  
  81      /**
  82       * Returns the minimum year for the calendar.
  83       *
  84       * @return int The minimum year
  85       */
  86      public function get_min_year() {
  87          return 1900;
  88      }
  89  
  90      /**
  91       * Returns the maximum year for the calendar
  92       *
  93       * @return int The maximum year
  94       */
  95      public function get_max_year() {
  96          return 2050;
  97      }
  98  
  99      /**
 100       * Returns an array of years.
 101       *
 102       * @param int $minyear
 103       * @param int $maxyear
 104       * @return array the years
 105       */
 106      public function get_years($minyear = null, $maxyear = null) {
 107          if (is_null($minyear)) {
 108              $minyear = $this->get_min_year();
 109          }
 110  
 111          if (is_null($maxyear)) {
 112              $maxyear = $this->get_max_year();
 113          }
 114  
 115          $years = array();
 116          for ($i = $minyear; $i <= $maxyear; $i++) {
 117              $years[$i] = $i;
 118          }
 119  
 120          return $years;
 121      }
 122  
 123      /**
 124       * Returns a multidimensional array with information for day, month, year
 125       * and the order they are displayed when selecting a date.
 126       * The order in the array will be the order displayed when selecting a date.
 127       * Override this function to change the date selector order.
 128       *
 129       * @param int $minyear The year to start with
 130       * @param int $maxyear The year to finish with
 131       * @return array Full date information
 132       */
 133      public function get_date_order($minyear = null, $maxyear = null) {
 134          $dateinfo = array();
 135          $dateinfo['day'] = $this->get_days();
 136          $dateinfo['month'] = $this->get_months();
 137          $dateinfo['year'] = $this->get_years($minyear, $maxyear);
 138  
 139          return $dateinfo;
 140      }
 141  
 142      /**
 143       * Returns the number of days in a week.
 144       *
 145       * @return int the number of days
 146       */
 147      public function get_num_weekdays() {
 148          return 7;
 149      }
 150  
 151      /**
 152       * Returns an indexed list of all the names of the weekdays.
 153       *
 154       * The list starts with the index 0. Each index, representing a
 155       * day, must be an array that contains the indexes 'shortname'
 156       * and 'fullname'.
 157       *
 158       * @return array array of days
 159       */
 160      public function get_weekdays() {
 161          return array(
 162              0 => array(
 163                  'shortname' => get_string('sun', 'calendar'),
 164                  'fullname' => get_string('sunday', 'calendar')
 165              ),
 166              1 => array(
 167                  'shortname' => get_string('mon', 'calendar'),
 168                  'fullname' => get_string('monday', 'calendar')
 169              ),
 170              2 => array(
 171                  'shortname' => get_string('tue', 'calendar'),
 172                  'fullname' => get_string('tuesday', 'calendar')
 173              ),
 174              3 => array(
 175                  'shortname' => get_string('wed', 'calendar'),
 176                  'fullname' => get_string('wednesday', 'calendar')
 177              ),
 178              4 => array(
 179                  'shortname' => get_string('thu', 'calendar'),
 180                  'fullname' => get_string('thursday', 'calendar')
 181              ),
 182              5 => array(
 183                  'shortname' => get_string('fri', 'calendar'),
 184                  'fullname' => get_string('friday', 'calendar')
 185              ),
 186              6 => array(
 187                  'shortname' => get_string('sat', 'calendar'),
 188                  'fullname' => get_string('saturday', 'calendar')
 189              ),
 190          );
 191      }
 192  
 193      /**
 194       * Returns the index of the starting week day.
 195       *
 196       * This may vary, for example some may consider Monday as the start of the week,
 197       * where as others may consider Sunday the start.
 198       *
 199       * @return int
 200       */
 201      public function get_starting_weekday() {
 202          global $CFG;
 203  
 204          if (isset($CFG->calendar_startwday)) {
 205              $firstday = $CFG->calendar_startwday;
 206          } else {
 207              $firstday = get_string('firstdayofweek', 'langconfig');
 208          }
 209  
 210          if (!is_numeric($firstday)) {
 211              $startingweekday = CALENDAR_DEFAULT_STARTING_WEEKDAY;
 212          } else {
 213              $startingweekday = intval($firstday) % 7;
 214          }
 215  
 216          return get_user_preferences('calendar_startwday', $startingweekday);
 217      }
 218  
 219      /**
 220       * Returns the index of the weekday for a specific calendar date.
 221       *
 222       * @param int $year
 223       * @param int $month
 224       * @param int $day
 225       * @return int
 226       */
 227      public function get_weekday($year, $month, $day) {
 228          return intval(date('w', mktime(12, 0, 0, $month, $day, $year)));
 229      }
 230  
 231      /**
 232       * Returns the number of days in a given month.
 233       *
 234       * @param int $year
 235       * @param int $month
 236       * @return int the number of days
 237       */
 238      public function get_num_days_in_month($year, $month) {
 239          return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
 240      }
 241  
 242      /**
 243       * Get the previous month.
 244       *
 245       * If the current month is January, it will get the last month of the previous year.
 246       *
 247       * @param int $year
 248       * @param int $month
 249       * @return array previous month and year
 250       */
 251      public function get_prev_month($year, $month) {
 252          if ($month == 1) {
 253              return array(12, $year - 1);
 254          } else {
 255              return array($month - 1, $year);
 256          }
 257      }
 258  
 259      /**
 260       * Get the next month.
 261       *
 262       * If the current month is December, it will get the first month of the following year.
 263       *
 264       * @param int $year
 265       * @param int $month
 266       * @return array the following month and year
 267       */
 268      public function get_next_month($year, $month) {
 269          if ($month == 12) {
 270              return array(1, $year + 1);
 271          } else {
 272              return array($month + 1, $year);
 273          }
 274      }
 275  
 276      /**
 277       * Returns a formatted string that represents a date in user time.
 278       *
 279       * Returns a formatted string that represents a date in user time
 280       * <b>WARNING: note that the format is for strftime(), not date().</b>
 281       * Because of a bug in most Windows time libraries, we can't use
 282       * the nicer %e, so we have to use %d which has leading zeroes.
 283       * A lot of the fuss in the function is just getting rid of these leading
 284       * zeroes as efficiently as possible.
 285       *
 286       * If parameter fixday = true (default), then take off leading
 287       * zero from %d, else maintain it.
 288       *
 289       * @param int $time the timestamp in UTC, as obtained from the database
 290       * @param string $format strftime format
 291       * @param int|float|string $timezone the timezone to use
 292       *        {@link http://docs.moodle.org/dev/Time_API#Timezone}
 293       * @param bool $fixday if true then the leading zero from %d is removed,
 294       *        if false then the leading zero is maintained
 295       * @param bool $fixhour if true then the leading zero from %I is removed,
 296       *        if false then the leading zero is maintained
 297       * @return string the formatted date/time
 298       */
 299      public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) {
 300          global $CFG;
 301  
 302          if (empty($format)) {
 303              $format = get_string('strftimedaydatetime', 'langconfig');
 304          }
 305  
 306          if (!empty($CFG->nofixday)) { // Config.php can force %d not to be fixed.
 307              $fixday = false;
 308          } else if ($fixday) {
 309              $formatnoday = str_replace('%d', 'DD', $format);
 310              $fixday = ($formatnoday != $format);
 311              $format = $formatnoday;
 312          }
 313  
 314          // Note: This logic about fixing 12-hour time to remove unnecessary leading
 315          // zero is required because on Windows, PHP strftime function does not
 316          // support the correct 'hour without leading zero' parameter (%l).
 317          if (!empty($CFG->nofixhour)) {
 318              // Config.php can force %I not to be fixed.
 319              $fixhour = false;
 320          } else if ($fixhour) {
 321              $formatnohour = str_replace('%I', 'HH', $format);
 322              $fixhour = ($formatnohour != $format);
 323              $format = $formatnohour;
 324          }
 325  
 326          $time = (int)$time; // Moodle allows rubbish in input...
 327          $datestring = date_format_string($time, $format, $timezone);
 328  
 329          date_default_timezone_set(\core_date::get_user_timezone($timezone));
 330  
 331          if ($fixday) {
 332              $daystring  = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $time)));
 333              $datestring = str_replace('DD', $daystring, $datestring);
 334          }
 335          if ($fixhour) {
 336              $hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $time)));
 337              $datestring = str_replace('HH', $hourstring, $datestring);
 338          }
 339  
 340          \core_date::set_default_server_timezone();
 341  
 342          return $datestring;
 343      }
 344  
 345      /**
 346       * Given a $time timestamp in GMT (seconds since epoch), returns an array that
 347       * represents the date in user time.
 348       *
 349       * @param int $time Timestamp in GMT
 350       * @param float|int|string $timezone offset's time with timezone, if float and not 99, then no
 351       *        dst offset is applied {@link http://docs.moodle.org/dev/Time_API#Timezone}
 352       * @return array an array that represents the date in user time
 353       */
 354      public function timestamp_to_date_array($time, $timezone = 99) {
 355          return usergetdate($time, $timezone);
 356      }
 357  
 358      /**
 359       * Provided with a day, month, year, hour and minute in a specific
 360       * calendar type convert it into the equivalent Gregorian date.
 361       *
 362       * In this function we don't need to do anything except pass the data
 363       * back as an array. This is because the date received is Gregorian.
 364       *
 365       * @param int $year
 366       * @param int $month
 367       * @param int $day
 368       * @param int $hour
 369       * @param int $minute
 370       * @return array the converted date
 371       */
 372      public function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
 373          $date = array();
 374          $date['year'] = $year;
 375          $date['month'] = $month;
 376          $date['day'] = $day;
 377          $date['hour'] = $hour;
 378          $date['minute'] = $minute;
 379  
 380          return $date;
 381      }
 382  
 383      /**
 384       * Provided with a day, month, year, hour and minute in a specific
 385       * calendar type convert it into the equivalent Gregorian date.
 386       *
 387       * In this function we don't need to do anything except pass the data
 388       * back as an array. This is because the date received is Gregorian.
 389       *
 390       * @param int $year
 391       * @param int $month
 392       * @param int $day
 393       * @param int $hour
 394       * @param int $minute
 395       * @return array the converted date
 396       */
 397      public function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
 398          $date = array();
 399          $date['year'] = $year;
 400          $date['month'] = $month;
 401          $date['day'] = $day;
 402          $date['hour'] = $hour;
 403          $date['minute'] = $minute;
 404  
 405          return $date;
 406      }
 407  
 408      /**
 409       * This return locale for windows os.
 410       *
 411       * @return string locale
 412       */
 413      public function locale_win_charset() {
 414          return get_string('localewincharset', 'langconfig');
 415      }
 416  }