Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
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
     * If parameter fixday = true (default), then take off leading
280
     * zero from %d, else maintain it.
281
     *
282
     * @param int $time the timestamp in UTC, as obtained from the database
283
     * @param string $format strftime format
284
     * @param int|float|string $timezone the timezone to use
285
     *        {@link https://moodledev.io/docs/apis/subsystems/time#timezone}
286
     * @param bool $fixday if true then the leading zero from %d is removed,
287
     *        if false then the leading zero is maintained
288
     * @param bool $fixhour if true then the leading zero from %I is removed,
289
     *        if false then the leading zero is maintained
290
     * @return string the formatted date/time
291
     */
292
    public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) {
293
        global $CFG;
294
 
295
        if (empty($format)) {
296
            $format = get_string('strftimedaydatetime', 'langconfig');
297
        }
298
 
1441 ariadna 299
        // Note: This historical logic was about fixing 12-hour time to remove
300
        // unnecessary leading zero was required because on Windows, PHP strftime
301
        // function did not support the correct 'hour without leading zero' parameter (%l).
302
        // This is no longer required because we use IntlDateFormatter.
303
        // Unfortunately though the original implementation was done incorrectly.
304
        // The documentation for strftime notes that for the "%l" and "%e" specifiers where
305
        // no leading zero is used, a space is used instead.
306
        // As a result we switch to the new format specifiers "%l" and "%e", wrap them in placeholders
307
        // and then remove the spaces.
308
 
309
        if (empty($CFG->nofixday) && $fixday) {
310
            // Config.php can force %d not to be fixed, but only if the format did not specify it.
311
            $format = str_replace(
312
                '%d',
313
                'DDHH%eHHDD',
314
                $format,
315
            );
1 efrain 316
        }
317
 
1441 ariadna 318
        if (empty($CFG->nofixhour) && $fixhour) {
319
            $format = str_replace(
320
                '%I',
321
                'DDHH%lHHDD',
322
                $format,
323
            );
1 efrain 324
        }
325
 
1441 ariadna 326
        if (is_string($time) && !is_numeric($time)) {
327
            debugging(
328
                "Invalid time passed to timestamp_to_date_string: '{$time}'",
329
                DEBUG_DEVELOPER,
330
            );
331
            $time = 0;
332
        }
1 efrain 333
 
1441 ariadna 334
        if ($time === null || $time === '') {
335
            $time = 0;
336
        }
337
 
338
        $time = new \DateTime("@{$time}", new \DateTimeZone(date_default_timezone_get()));
339
 
1 efrain 340
        date_default_timezone_set(\core_date::get_user_timezone($timezone));
341
 
1441 ariadna 342
        $formattedtime = \core_date::strftime(
343
            $format,
344
            $time,
345
            get_string('locale', 'langconfig'),
346
        );
1 efrain 347
 
348
        \core_date::set_default_server_timezone();
349
 
1441 ariadna 350
        // Use a simple regex to remove the placeholders and any leading spaces to match the historically
351
        // generated format.
352
        $formattedtime = preg_replace('/DDHH ?(\d{1,2})HHDD/', '$1', $formattedtime);
353
 
354
        return $formattedtime;
1 efrain 355
    }
356
 
357
    /**
358
     * Given a $time timestamp in GMT (seconds since epoch), returns an array that
359
     * represents the date in user time.
360
     *
361
     * @param int $time Timestamp in GMT
362
     * @param float|int|string $timezone offset's time with timezone, if float and not 99, then no
363
     *        dst offset is applied {@link https://moodledev.io/docs/apis/subsystems/time#timezone}
364
     * @return array an array that represents the date in user time
365
     */
366
    public function timestamp_to_date_array($time, $timezone = 99) {
367
        return usergetdate($time, $timezone);
368
    }
369
 
370
    /**
371
     * Provided with a day, month, year, hour and minute in a specific
372
     * calendar type convert it into the equivalent Gregorian date.
373
     *
374
     * In this function we don't need to do anything except pass the data
375
     * back as an array. This is because the date received is Gregorian.
376
     *
377
     * @param int $year
378
     * @param int $month
379
     * @param int $day
380
     * @param int $hour
381
     * @param int $minute
382
     * @return array the converted date
383
     */
384
    public function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
385
        $date = array();
386
        $date['year'] = $year;
387
        $date['month'] = $month;
388
        $date['day'] = $day;
389
        $date['hour'] = $hour;
390
        $date['minute'] = $minute;
391
 
392
        return $date;
393
    }
394
 
395
    /**
396
     * Provided with a day, month, year, hour and minute in a specific
397
     * calendar type convert it into the equivalent Gregorian date.
398
     *
399
     * In this function we don't need to do anything except pass the data
400
     * back as an array. This is because the date received is Gregorian.
401
     *
402
     * @param int $year
403
     * @param int $month
404
     * @param int $day
405
     * @param int $hour
406
     * @param int $minute
407
     * @return array the converted date
408
     */
409
    public function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
410
        $date = array();
411
        $date['year'] = $year;
412
        $date['month'] = $month;
413
        $date['day'] = $day;
414
        $date['hour'] = $hour;
415
        $date['minute'] = $minute;
416
 
417
        return $date;
418
    }
419
 
420
    /**
421
     * This return locale for windows os.
422
     *
423
     * @return string locale
424
     */
425
    public function locale_win_charset() {
426
        return get_string('localewincharset', 'langconfig');
427
    }
428
}