Skip to content

Commit 3040bcf

Browse files
Amaia Anabitartesarjona
Amaia Anabitarte
authored andcommitted
MDL-83873 core_calendar: Deprecating lib functions
1 parent 76709b7 commit 3040bcf

8 files changed

+186
-170
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
issueNumber: MDL-83873
2+
notes:
3+
core_calendar:
4+
- message: >-
5+
calendar_day_representation(), calendar_time_representation() and
6+
calendar_format_event_time() functions have been deprecated and can't be
7+
used anymore. Use humandate and humantimeperiod classes instead.
8+
type: deprecated

calendar/classes/external/calendar_day_exporter.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace core_calendar\external;
1818

1919
use core\external\exporter;
20+
use core_calendar\output\humantimeperiod;
2021
use core_date;
2122
use DateTimeImmutable;
2223
use renderer_base;
@@ -161,7 +162,12 @@ protected function get_other_values(renderer_base $output) {
161162
// We need to override default formatted time because it differs from day view.
162163
// Formatted time for day view adds a link to the day view.
163164
$legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
164-
$data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
165+
$humanperiod = new humantimeperiod(
166+
starttimestamp: $legacyevent->timestart,
167+
endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
168+
link: new moodle_url(CALENDAR_URL . 'view.php'),
169+
);
170+
$data->formattedtime = $output->render($humanperiod);
165171

166172
return $data;
167173
}, $this->related['events']);

calendar/classes/external/calendar_upcoming_exporter.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
defined('MOODLE_INTERNAL') || die();
2828

2929
use core\external\exporter;
30+
use core_calendar\output\humantimeperiod;
3031
use renderer_base;
3132
use moodle_url;
3233
use \core_calendar\local\event\container;
@@ -131,7 +132,12 @@ protected function get_other_values(renderer_base $output) {
131132
// We need to override default formatted time because it differs from day view.
132133
// Formatted time for upcoming view adds a link to the day view.
133134
$legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
134-
$data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
135+
$humanperiod = new humantimeperiod(
136+
starttimestamp: $legacyevent->timestart,
137+
endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
138+
link: new moodle_url(CALENDAR_URL . 'view.php'),
139+
);
140+
$data->formattedtime = $output->render($humanperiod);
135141

136142
return $data;
137143
}, $this->related['events']);

calendar/classes/external/event_exporter.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
require_once($CFG->dirroot . "/calendar/lib.php");
3030

3131
use \core_calendar\local\event\container;
32+
use core_calendar\output\humantimeperiod;
3233
use \renderer_base;
3334

3435
/**
@@ -86,7 +87,12 @@ protected function get_other_values(renderer_base $output) {
8687

8788
// Override default formatted time to make sure the date portion of the time is always rendered.
8889
$legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
89-
$values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false);
90+
$humanperiod = new humantimeperiod(
91+
starttimestamp: $legacyevent->timestart,
92+
endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
93+
link: new \moodle_url(CALENDAR_URL . 'view.php'),
94+
);
95+
$values['formattedtime'] = $output->render($humanperiod);
9096

9197
return $values;
9298
}

calendar/classes/external/event_exporter_base.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use \core_calendar\local\event\container;
3434
use \core_calendar\local\event\entities\event_interface;
3535
use \core_calendar\local\event\entities\action_event_interface;
36+
use core_calendar\output\humantimeperiod;
3637
use \core_course\external\course_summary_exporter;
3738
use \core\external\coursecat_summary_exporter;
3839
use \renderer_base;
@@ -379,8 +380,13 @@ protected function get_other_values(renderer_base $output) {
379380
'time' => $timesort]);
380381
$viewurl->set_anchor('event_' . $event->get_id());
381382
$values['viewurl'] = $viewurl->out(false);
382-
$values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
383-
$timesort);
383+
$legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
384+
$humanperiod = new humantimeperiod(
385+
starttimestamp: $legacyevent->timestart,
386+
endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
387+
link: new moodle_url(CALENDAR_URL . 'view.php'),
388+
);
389+
$values['formattedtime'] = $output->render($humanperiod);
384390
$values['formattedlocation'] = calendar_format_event_location($legacyevent);
385391

386392
if ($group = $event->get_group()) {

calendar/deprecatedlib.php

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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_calendar\output\humandate;
28+
use core_calendar\output\humantimeperiod;
29+
30+
/**
31+
* Return the representation day.
32+
*
33+
* @param int $tstamp Timestamp in GMT
34+
* @param int|bool $now current Unix timestamp
35+
* @param bool $usecommonwords
36+
* @return string the formatted date/time
37+
*
38+
* @deprecated since Moodle 5.0.
39+
* @todo MDL-84268 Final deprecation in Moodle 6.0.
40+
*/
41+
#[\core\attribute\deprecated(
42+
replacement: '\core_calendar\output\humandate',
43+
since: '5.0',
44+
mdl: 'MDL-83873',
45+
)]
46+
function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
47+
static $shortformat;
48+
49+
\core\deprecation::emit_deprecation_if_present(__FUNCTION__);
50+
51+
if (empty($shortformat)) {
52+
$shortformat = get_string('strftimedayshort');
53+
}
54+
55+
if ($now === false) {
56+
$now = time();
57+
}
58+
59+
// To have it in one place, if a change is needed.
60+
$formal = userdate($tstamp, $shortformat);
61+
62+
$datestamp = usergetdate($tstamp);
63+
$datenow = usergetdate($now);
64+
65+
if ($usecommonwords == false) {
66+
// We don't want words, just a date.
67+
return $formal;
68+
} else if ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
69+
return get_string('today', 'calendar');
70+
} else if (($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
71+
($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12
72+
&& $datenow['yday'] == 1)) {
73+
return get_string('yesterday', 'calendar');
74+
} else if (($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
75+
($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12
76+
&& $datestamp['yday'] == 1)) {
77+
return get_string('tomorrow', 'calendar');
78+
} else {
79+
return $formal;
80+
}
81+
}
82+
83+
/**
84+
* return the formatted representation time.
85+
*
86+
* @param int $time the timestamp in UTC, as obtained from the database
87+
* @return string the formatted date/time
88+
*
89+
* @deprecated since Moodle 5.0.
90+
* @todo MDL-84268 Final deprecation in Moodle 6.0.
91+
*/
92+
#[\core\attribute\deprecated(
93+
replacement: '\core_calendar\output\humandate',
94+
since: '5.0',
95+
mdl: 'MDL-83873',
96+
)]
97+
function calendar_time_representation($time) {
98+
\core\deprecation::emit_deprecation_if_present(__FUNCTION__);
99+
100+
global $OUTPUT;
101+
102+
$humantime = new humandate(
103+
timestamp: $time,
104+
near: null,
105+
timeonly: true
106+
);
107+
return $OUTPUT->render($humantime);
108+
}
109+
110+
/**
111+
* Get event format time.
112+
*
113+
* @param calendar_event $event event object
114+
* @param int $now current time in gmt
115+
* @param array $linkparams list of params for event link
116+
* @param bool $usecommonwords the words as formatted date/time.
117+
* @param int $showtime determine the show time GMT timestamp
118+
* @return string $eventtime link/string for event time
119+
*
120+
* @deprecated since Moodle 5.0.
121+
* @todo MDL-84268 Final deprecation in Moodle 6.0.
122+
*/
123+
#[\core\attribute\deprecated(
124+
replacement: '\core_calendar\output\humantimeperiod',
125+
since: '5.0',
126+
mdl: 'MDL-83873',
127+
)]
128+
function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime = 0) {
129+
// \core\deprecation::emit_deprecation_if_present(__FUNCTION__);
130+
131+
global $OUTPUT;
132+
133+
$humanperiod = new humantimeperiod(
134+
starttimestamp: $event->timestart,
135+
endtimestamp: $event->timestart + $event->timeduration,
136+
link: new \moodle_url(CALENDAR_URL . 'view.php'),
137+
);
138+
139+
return $OUTPUT->render($humanperiod);
140+
141+
}

0 commit comments

Comments
 (0)