source: moodle/trunk/fuentes/calendar/type/gregorian/classes/structure.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 3 years ago

Ported code to xenial

File size: 13.8 KB
Line 
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
17namespace calendartype_gregorian;
18use 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 */
27class 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        for ($i = 1; $i <= 12; $i++) {
72            $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B');
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 array(
159            0 => array(
160                'shortname' => get_string('sun', 'calendar'),
161                'fullname' => get_string('sunday', 'calendar')
162            ),
163            1 => array(
164                'shortname' => get_string('mon', 'calendar'),
165                'fullname' => get_string('monday', 'calendar')
166            ),
167            2 => array(
168                'shortname' => get_string('tue', 'calendar'),
169                'fullname' => get_string('tuesday', 'calendar')
170            ),
171            3 => array(
172                'shortname' => get_string('wed', 'calendar'),
173                'fullname' => get_string('wednesday', 'calendar')
174            ),
175            4 => array(
176                'shortname' => get_string('thu', 'calendar'),
177                'fullname' => get_string('thursday', 'calendar')
178            ),
179            5 => array(
180                'shortname' => get_string('fri', 'calendar'),
181                'fullname' => get_string('friday', 'calendar')
182            ),
183            6 => array(
184                'shortname' => get_string('sat', 'calendar'),
185                'fullname' => get_string('saturday', 'calendar')
186            ),
187        );
188    }
189
190    /**
191     * Returns the index of the starting week day.
192     *
193     * This may vary, for example some may consider Monday as the start of the week,
194     * where as others may consider Sunday the start.
195     *
196     * @return int
197     */
198    public function get_starting_weekday() {
199        global $CFG;
200
201        if (isset($CFG->calendar_startwday)) {
202            $firstday = $CFG->calendar_startwday;
203        } else {
204            $firstday = get_string('firstdayofweek', 'langconfig');
205        }
206
207        if (!is_numeric($firstday)) {
208            $startingweekday = CALENDAR_DEFAULT_STARTING_WEEKDAY;
209        } else {
210            $startingweekday = intval($firstday) % 7;
211        }
212
213        return get_user_preferences('calendar_startwday', $startingweekday);
214    }
215
216    /**
217     * Returns the index of the weekday for a specific calendar date.
218     *
219     * @param int $year
220     * @param int $month
221     * @param int $day
222     * @return int
223     */
224    public function get_weekday($year, $month, $day) {
225        return intval(date('w', mktime(12, 0, 0, $month, $day, $year)));
226    }
227
228    /**
229     * Returns the number of days in a given month.
230     *
231     * @param int $year
232     * @param int $month
233     * @return int the number of days
234     */
235    public function get_num_days_in_month($year, $month) {
236        return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
237    }
238
239    /**
240     * Get the previous month.
241     *
242     * If the current month is January, it will get the last month of the previous year.
243     *
244     * @param int $year
245     * @param int $month
246     * @return array previous month and year
247     */
248    public function get_prev_month($year, $month) {
249        if ($month == 1) {
250            return array(12, $year - 1);
251        } else {
252            return array($month - 1, $year);
253        }
254    }
255
256    /**
257     * Get the next month.
258     *
259     * If the current month is December, it will get the first month of the following year.
260     *
261     * @param int $year
262     * @param int $month
263     * @return array the following month and year
264     */
265    public function get_next_month($year, $month) {
266        if ($month == 12) {
267            return array(1, $year + 1);
268        } else {
269            return array($month + 1, $year);
270        }
271    }
272
273    /**
274     * Returns a formatted string that represents a date in user time.
275     *
276     * Returns a formatted string that represents a date in user time
277     * <b>WARNING: note that the format is for strftime(), not date().</b>
278     * Because of a bug in most Windows time libraries, we can't use
279     * the nicer %e, so we have to use %d which has leading zeroes.
280     * A lot of the fuss in the function is just getting rid of these leading
281     * zeroes as efficiently as possible.
282     *
283     * If parameter fixday = true (default), then take off leading
284     * zero from %d, else maintain it.
285     *
286     * @param int $time the timestamp in UTC, as obtained from the database
287     * @param string $format strftime format
288     * @param int|float|string $timezone the timezone to use
289     *        {@link http://docs.moodle.org/dev/Time_API#Timezone}
290     * @param bool $fixday if true then the leading zero from %d is removed,
291     *        if false then the leading zero is maintained
292     * @param bool $fixhour if true then the leading zero from %I is removed,
293     *        if false then the leading zero is maintained
294     * @return string the formatted date/time
295     */
296    public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) {
297        global $CFG;
298
299        if (empty($format)) {
300            $format = get_string('strftimedaydatetime', 'langconfig');
301        }
302
303        if (!empty($CFG->nofixday)) { // Config.php can force %d not to be fixed.
304            $fixday = false;
305        } else if ($fixday) {
306            $formatnoday = str_replace('%d', 'DD', $format);
307            $fixday = ($formatnoday != $format);
308            $format = $formatnoday;
309        }
310
311        // Note: This logic about fixing 12-hour time to remove unnecessary leading
312        // zero is required because on Windows, PHP strftime function does not
313        // support the correct 'hour without leading zero' parameter (%l).
314        if (!empty($CFG->nofixhour)) {
315            // Config.php can force %I not to be fixed.
316            $fixhour = false;
317        } else if ($fixhour) {
318            $formatnohour = str_replace('%I', 'HH', $format);
319            $fixhour = ($formatnohour != $format);
320            $format = $formatnohour;
321        }
322
323        // Add daylight saving offset for string timezones only, as we can't get dst for
324        // float values. if timezone is 99 (user default timezone), then try update dst.
325        if ((99 == $timezone) || !is_numeric($timezone)) {
326            $time += dst_offset_on($time, $timezone);
327        }
328
329        $timezone = get_user_timezone_offset($timezone);
330
331        // If we are running under Windows convert to windows encoding and then back to UTF-8
332        // (because it's impossible to specify UTF-8 to fetch locale info in Win32).
333        if (abs($timezone) > 13) { // Server time.
334            $datestring = date_format_string($time, $format, $timezone);
335            if ($fixday) {
336                $daystring  = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $time)));
337                $datestring = str_replace('DD', $daystring, $datestring);
338            }
339            if ($fixhour) {
340                $hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $time)));
341                $datestring = str_replace('HH', $hourstring, $datestring);
342            }
343        } else {
344            $time += (int)($timezone * 3600);
345            $datestring = date_format_string($time, $format, $timezone);
346            if ($fixday) {
347                $daystring  = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %d', $time)));
348                $datestring = str_replace('DD', $daystring, $datestring);
349            }
350            if ($fixhour) {
351                $hourstring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %I', $time)));
352                $datestring = str_replace('HH', $hourstring, $datestring);
353            }
354        }
355
356        return $datestring;
357    }
358
359    /**
360     * Given a $time timestamp in GMT (seconds since epoch), returns an array that
361     * represents the date in user time.
362     *
363     * @param int $time Timestamp in GMT
364     * @param float|int|string $timezone offset's time with timezone, if float and not 99, then no
365     *        dst offset is applied {@link http://docs.moodle.org/dev/Time_API#Timezone}
366     * @return array an array that represents the date in user time
367     */
368    public function timestamp_to_date_array($time, $timezone = 99) {
369        return usergetdate($time, $timezone);
370    }
371
372    /**
373     * Provided with a day, month, year, hour and minute in a specific
374     * calendar type convert it into the equivalent Gregorian date.
375     *
376     * In this function we don't need to do anything except pass the data
377     * back as an array. This is because the date received is Gregorian.
378     *
379     * @param int $year
380     * @param int $month
381     * @param int $day
382     * @param int $hour
383     * @param int $minute
384     * @return array the converted date
385     */
386    public function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
387        $date = array();
388        $date['year'] = $year;
389        $date['month'] = $month;
390        $date['day'] = $day;
391        $date['hour'] = $hour;
392        $date['minute'] = $minute;
393
394        return $date;
395    }
396
397    /**
398     * Provided with a day, month, year, hour and minute in a specific
399     * calendar type convert it into the equivalent Gregorian date.
400     *
401     * In this function we don't need to do anything except pass the data
402     * back as an array. This is because the date received is Gregorian.
403     *
404     * @param int $year
405     * @param int $month
406     * @param int $day
407     * @param int $hour
408     * @param int $minute
409     * @return array the converted date
410     */
411    public function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
412        $date = array();
413        $date['year'] = $year;
414        $date['month'] = $month;
415        $date['day'] = $day;
416        $date['hour'] = $hour;
417        $date['minute'] = $minute;
418
419        return $date;
420    }
421
422    /**
423     * This return locale for windows os.
424     *
425     * @return string locale
426     */
427    public function locale_win_charset() {
428        return get_string('localewincharset', 'langconfig');
429    }
430}
Note: See TracBrowser for help on using the repository browser.