PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/type/gregorian/classes/structure.php

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