Merge branch 'MDL-63999-master' of git://github.com/lameze/moodle
[moodle.git] / calendar / tests / calendartype_test_example.php
blobab725d1d03576e7a7c2a74fed38a336bc420f289
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.
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/>.
17 namespace calendartype_test_example;
18 use \core_calendar\type_base;
20 /**
21 * Handles calendar functions for the test calendar.
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.
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
30 class structure extends type_base {
32 /**
33 * Returns the name of the calendar.
35 * @return string the calendar name
37 public function get_name() {
38 return 'test_example';
41 /**
42 * Returns a list of all the possible days for all months.
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).
51 * @return array the days
53 public function get_days() {
54 $days = array();
56 for ($i = 1; $i <= 31; $i++) {
57 $days[$i] = $i;
60 return $days;
63 /**
64 * Returns a list of all the names of the months.
66 * @return array the month names
68 public function get_months() {
69 $months = array();
71 for ($i = 1; $i <= 12; $i++) {
72 $months[$i] = $i;
75 return $months;
78 /**
79 * Returns the minimum year for the calendar.
81 * @return int The minimum year
83 public function get_min_year() {
84 return 1900;
87 /**
88 * Returns the maximum year for the calendar
90 * @return int The maximum year
92 public function get_max_year() {
93 return 2050;
96 /**
97 * Returns an array of years.
99 * @param int $minyear
100 * @param int $maxyear
101 * @return array the years.
103 public function get_years($minyear = null, $maxyear = null) {
104 if (is_null($minyear)) {
105 $minyear = $this->get_min_year();
108 if (is_null($maxyear)) {
109 $maxyear = $this->get_max_year();
112 $years = array();
113 for ($i = $minyear; $i <= $maxyear; $i++) {
114 $years[$i] = $i;
117 return $years;
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.
126 * @param int $minyear The year to start with.
127 * @param int $maxyear The year to finish with.
128 * @return array Full date information.
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);
136 return $dateinfo;
140 * Returns the number of days in a week.
142 * @return int the number of days
144 public function get_num_weekdays() {
145 return 7;
149 * Returns an indexed list of all the names of the weekdays.
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'.
155 * @return array array of days
157 public function get_weekdays() {
158 return '';
162 * Returns the index of the starting week day.
164 * @return int
166 public function get_starting_weekday() {
167 return '';
171 * Returns the index of the weekday for a specific calendar date.
173 * @param int $year
174 * @param int $month
175 * @param int $day
176 * @return int
178 public function get_weekday($year, $month, $day) {
179 return '';
183 * Returns the number of days in a given month.
185 * @param int $year
186 * @param int $month
187 * @return int the number of days
189 public function get_num_days_in_month($year, $month) {
190 return '';
194 * Get the previous month.
196 * If the current month is January, it will get the last month of the previous year.
198 * @param int $year
199 * @param int $month
200 * @return array previous month and year
202 public function get_prev_month($year, $month) {
203 return '';
207 * Get the next month.
209 * If the current month is December, it will get the first month of the following year.
211 * @param int $year
212 * @param int $month
213 * @return array the following month and year
215 public function get_next_month($year, $month) {
216 return '';
220 * Returns a formatted string that represents a date in user time.
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 http://docs.moodle.org/dev/Time_API#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
232 public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) {
233 return '';
237 * Given a $time timestamp in GMT (seconds since epoch), returns an array that represents
238 * the date in user time.
240 * @param int $time timestamp in GMT
241 * @param float|int|string $timezone the timezone to use to calculate the time
242 * {@link http://docs.moodle.org/dev/Time_API#Timezone}
243 * @return array an array that represents the date in user time
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']);
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'];
257 return $date;
261 * Provided with a day, month, year, hour and minute
262 * convert it into the equivalent Gregorian date.
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.
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));
275 list($year, $month, $day, $hour, $minute) = explode('/', $date);
277 return array('year' => (int) $year,
278 'month' => (int) $month,
279 'day' => (int) $day,
280 'hour' => (int) $hour,
281 'minute' => (int) $minute);
286 * Provided with a day, month, year, hour and minute in a Gregorian date
287 * convert it into the specific calendar type date.
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.
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));
300 list($year, $month, $day, $hour, $minute) = explode('/', $date);
302 return array('year' => (int) $year,
303 'month' => (int) $month,
304 'day' => (int) $day,
305 'hour' => (int) $hour,
306 'minute' => (int) $minute);
310 * This return locale for windows os.
312 * @return string locale
314 public function locale_win_charset() {
315 return get_string('localewincharset', 'langconfig');