Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
/**
18
 * List of deprecated calendar functions.
19
 *
20
 * @package     core_calendar
21
 * @copyright   2025 Amaia Anabitarte <amaia@moodle.com>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
use core\url;
28
use core_calendar\output\humandate;
29
use core_calendar\output\humantimeperiod;
30
 
31
/**
32
 * Return the representation day.
33
 *
34
 * @param int $tstamp Timestamp in GMT
35
 * @param int|bool $now current Unix timestamp
36
 * @param bool $usecommonwords
37
 * @return string the formatted date/time
38
 *
39
 * @deprecated since Moodle 5.0.
40
 * @todo MDL-84268 Final deprecation in Moodle 6.0.
41
 */
42
#[\core\attribute\deprecated(
43
    replacement: '\core_calendar\output\humandate',
44
    since: '5.0',
45
    mdl: 'MDL-83873',
46
)]
47
function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
48
    static $shortformat;
49
 
50
    \core\deprecation::emit_deprecation(__FUNCTION__);
51
 
52
    if (empty($shortformat)) {
53
        $shortformat = get_string('strftimedayshort');
54
    }
55
 
56
    if ($now === false) {
57
        $now = time();
58
    }
59
 
60
    // To have it in one place, if a change is needed.
61
    $formal = userdate($tstamp, $shortformat);
62
 
63
    $datestamp = usergetdate($tstamp);
64
    $datenow = usergetdate($now);
65
 
66
    if ($usecommonwords == false) {
67
        // We don't want words, just a date.
68
        return $formal;
69
    } else if ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
70
        return get_string('today', 'calendar');
71
    } else if (($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
72
            ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12
73
                    && $datenow['yday'] == 1)) {
74
        return get_string('yesterday', 'calendar');
75
    } else if (($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
76
            ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12
77
                    && $datestamp['yday'] == 1)) {
78
        return get_string('tomorrow', 'calendar');
79
    } else {
80
        return $formal;
81
    }
82
}
83
 
84
/**
85
 * return the formatted representation time.
86
 *
87
 * @param int $time the timestamp in UTC, as obtained from the database
88
 * @return string the formatted date/time
89
 *
90
 * @deprecated since Moodle 5.0.
91
 * @todo MDL-84268 Final deprecation in Moodle 6.0.
92
 */
93
#[\core\attribute\deprecated(
94
    replacement: '\core_calendar\output\humandate',
95
    since: '5.0',
96
    mdl: 'MDL-83873',
97
)]
98
function calendar_time_representation($time) {
99
    \core\deprecation::emit_deprecation(__FUNCTION__);
100
 
101
    global $OUTPUT;
102
 
103
    $humantime = humandate::create_from_timestamp(
104
        timestamp: $time,
105
        near: null,
106
        timeonly: true
107
    );
108
    return $OUTPUT->render($humantime);
109
}
110
 
111
/**
112
 * Get event format time.
113
 *
114
 * @param calendar_event $event event object
115
 * @param int $now current time in gmt
116
 * @param array $linkparams list of params for event link
117
 * @param bool $usecommonwords the words as formatted date/time.
118
 * @param int $showtime determine the show time GMT timestamp
119
 * @return string $eventtime link/string for event time
120
 *
121
 * @deprecated since Moodle 5.0.
122
 * @todo MDL-84268 Final deprecation in Moodle 6.0.
123
 */
124
#[\core\attribute\deprecated(
125
    replacement: '\core_calendar\output\humantimeperiod',
126
    since: '5.0',
127
    mdl: 'MDL-83873',
128
)]
129
function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime = 0) {
130
    \core\deprecation::emit_deprecation(__FUNCTION__);
131
 
132
    global $OUTPUT;
133
 
134
    $humanperiod = humantimeperiod::create_from_timestamp(
135
        starttimestamp: $event->timestart,
136
        endtimestamp: $event->timestart + $event->timeduration,
137
        link: new url(CALENDAR_URL . 'view.php'),
138
    );
139
 
140
    return $OUTPUT->render($humanperiod);
141
 
142
}
143
 
144
/**
145
 * @deprecated 3.9
146
 */
147
#[\core\attribute\deprecated(
148
    replacement: 'calendar_add_event_metadata no longer used',
149
    since: '3.9',
150
    mdl: 'MDL-58866',
151
    final: true,
152
)]
153
function calendar_add_event_metadata() {
154
    \core\deprecation::emit_deprecation(__FUNCTION__);
155
}
156
 
157
/**
158
 * Get a HTML link to a course.
159
 *
160
 * @param int|stdClass $course the course id or course object
161
 * @return string a link to the course (as HTML); empty if the course id is invalid
162
 *
163
 * @deprecated since 5.0
164
 * @todo MDL-84268 Final deprecation in Moodle 6.0.
165
 */
166
#[\core\attribute\deprecated(
167
    replacement: 'calendar_get_courselink no longer used',
168
    since: '5.0',
169
    mdl: 'MDL-84617',
170
)]
171
function calendar_get_courselink($course) {
172
    \core\deprecation::emit_deprecation(__FUNCTION__);
173
 
174
    if (!$course) {
175
        return '';
176
    }
177
 
178
    if (!is_object($course)) {
179
        $course = calendar_get_course_cached($coursecache, $course);
180
    }
181
    $context = \context_course::instance($course->id);
182
    $fullname = format_string($course->fullname, true, ['context' => $context]);
183
    $url = new \moodle_url('/course/view.php', ['id' => $course->id]);
184
    $link = \html_writer::link($url, $fullname);
185
 
186
    return $link;
187
}
188
 
189
/**
190
 * Get per-day basis events
191
 *
192
 * @param array $events list of events
193
 * @param int $month the number of the month
194
 * @param int $year the number of the year
195
 * @param array $eventsbyday event on specific day
196
 * @param array $durationbyday duration of the event in days
197
 * @param array $typesbyday event type (eg: site, course, user, or group)
198
 * @param array $courses list of courses
199
 * @return void
200
 *
201
 * @deprecated since 5.0
202
 * @todo MDL-84268 Final deprecation in Moodle 6.0.
203
 */
204
#[\core\attribute\deprecated(
205
    replacement: 'calendar_events_by_day not used since 3.4',
206
    since: '5.0',
207
    mdl: 'MDL-84617',
208
)]
209
function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) {
210
    \core\deprecation::emit_deprecation(__FUNCTION__);
211
 
212
    $calendartype = \core_calendar\type_factory::get_calendar_instance();
213
 
214
    $eventsbyday = [];
215
    $typesbyday = [];
216
    $durationbyday = [];
217
 
218
    if ($events === false) {
219
        return;
220
    }
221
 
222
    foreach ($events as $event) {
223
        $startdate = $calendartype->timestamp_to_date_array($event->timestart);
224
        if ($event->timeduration) {
225
            $enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1);
226
        } else {
227
            $enddate = $startdate;
228
        }
229
 
230
        // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair.
231
        if (!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) &&
232
            ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) {
233
            continue;
234
        }
235
 
236
        $eventdaystart = intval($startdate['mday']);
237
 
238
        if ($startdate['mon'] == $month && $startdate['year'] == $year) {
239
            // Give the event to its day.
240
            $eventsbyday[$eventdaystart][] = $event->id;
241
 
242
            // Mark the day as having such an event.
243
            if ($event->courseid == SITEID && $event->groupid == 0) {
244
                $typesbyday[$eventdaystart]['startsite'] = true;
245
                // Set event class for site event.
246
                $events[$event->id]->class = 'calendar_event_site';
247
            } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
248
                $typesbyday[$eventdaystart]['startcourse'] = true;
249
                // Set event class for course event.
250
                $events[$event->id]->class = 'calendar_event_course';
251
            } else if ($event->groupid) {
252
                $typesbyday[$eventdaystart]['startgroup'] = true;
253
                // Set event class for group event.
254
                $events[$event->id]->class = 'calendar_event_group';
255
            } else if ($event->userid) {
256
                $typesbyday[$eventdaystart]['startuser'] = true;
257
                // Set event class for user event.
258
                $events[$event->id]->class = 'calendar_event_user';
259
            }
260
        }
261
 
262
        if ($event->timeduration == 0) {
263
            // Proceed with the next.
264
            continue;
265
        }
266
 
267
        // The event starts on $month $year or before.
268
        if ($startdate['mon'] == $month && $startdate['year'] == $year) {
269
            $lowerbound = intval($startdate['mday']);
270
        } else {
271
            $lowerbound = 0;
272
        }
273
 
274
        // Also, it ends on $month $year or later.
275
        if ($enddate['mon'] == $month && $enddate['year'] == $year) {
276
            $upperbound = intval($enddate['mday']);
277
        } else {
278
            $upperbound = calendar_days_in_month($month, $year);
279
        }
280
 
281
        // Mark all days between $lowerbound and $upperbound (inclusive) as duration.
282
        for ($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
283
            $durationbyday[$i][] = $event->id;
284
            if ($event->courseid == SITEID && $event->groupid == 0) {
285
                $typesbyday[$i]['durationsite'] = true;
286
            } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
287
                $typesbyday[$i]['durationcourse'] = true;
288
            } else if ($event->groupid) {
289
                $typesbyday[$i]['durationgroup'] = true;
290
            } else if ($event->userid) {
291
                $typesbyday[$i]['durationuser'] = true;
292
            }
293
        }
294
 
295
    }
296
    return;
297
}