Skip to content

Commit d7fc7f9

Browse files
Amaia Anabitartesarjona
Amaia Anabitarte
authored andcommitted
MDL-83873 core_calendar: Deprecating lib functions
1 parent 2b7cbeb commit d7fc7f9

8 files changed

+207
-190
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 = humantimeperiod::create_from_timestamp(
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

+11-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
defined('MOODLE_INTERNAL') || die();
2828

2929
use core\external\exporter;
30+
use core_calendar\output\humantimeperiod;
3031
use renderer_base;
31-
use moodle_url;
32-
use \core_calendar\local\event\container;
32+
use core\url;
33+
use core_calendar\local\event\container;
3334

3435
/**
3536
* Class for displaying the day view.
@@ -45,7 +46,7 @@ class calendar_upcoming_exporter extends exporter {
4546
protected $calendar;
4647

4748
/**
48-
* @var moodle_url $url The URL for the upcoming view page.
49+
* @var url $url The URL for the upcoming view page.
4950
*/
5051
protected $url;
5152

@@ -106,7 +107,7 @@ protected function get_other_values(renderer_base $output) {
106107
$timestamp = $this->calendar->time;
107108

108109
$cache = $this->related['cache'];
109-
$url = new moodle_url('/calendar/view.php', [
110+
$url = new url('/calendar/view.php', [
110111
'view' => 'upcoming',
111112
'time' => $timestamp,
112113
'course' => $this->calendar->course->id,
@@ -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 = humantimeperiod::create_from_timestamp(
136+
starttimestamp: $legacyevent->timestart,
137+
endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
138+
link: new 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

+12-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
require_once($CFG->dirroot . "/calendar/lib.php");
3030

31-
use \core_calendar\local\event\container;
32-
use \renderer_base;
31+
use core_calendar\local\event\container;
32+
use core_calendar\output\humantimeperiod;
33+
use renderer_base;
34+
use core\url;
3335

3436
/**
3537
* Class for displaying a calendar event.
@@ -69,11 +71,11 @@ protected function get_other_values(renderer_base $output) {
6971
if ($moduleproxy = $event->get_course_module()) {
7072
$modulename = $moduleproxy->get('modname');
7173
$moduleid = $moduleproxy->get('id');
72-
$url = new \moodle_url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
74+
$url = new url(sprintf('/mod/%s/view.php', $modulename), ['id' => $moduleid]);
7375

7476
// Build edit event url for action events.
7577
$params = array('update' => $moduleid, 'return' => true, 'sesskey' => sesskey());
76-
$editurl = new \moodle_url('/course/mod.php', $params);
78+
$editurl = new url('/course/mod.php', $params);
7779
$values['editurl'] = $editurl->out(false);
7880
} else if ($event->get_type() == 'category') {
7981
$url = $event->get_category()->get_proxied_instance()->get_view_link();
@@ -86,7 +88,12 @@ protected function get_other_values(renderer_base $output) {
8688

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

9198
return $values;
9299
}

calendar/classes/external/event_exporter_base.php

+19-13
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
require_once($CFG->dirroot . "/calendar/lib.php");
3030
require_once($CFG->libdir . "/filelib.php");
3131

32-
use \core\external\exporter;
33-
use \core_calendar\local\event\container;
34-
use \core_calendar\local\event\entities\event_interface;
35-
use \core_calendar\local\event\entities\action_event_interface;
36-
use \core_course\external\course_summary_exporter;
37-
use \core\external\coursecat_summary_exporter;
38-
use \renderer_base;
39-
use moodle_url;
32+
use core\external\exporter;
33+
use core_calendar\local\event\container;
34+
use core_calendar\local\event\entities\event_interface;
35+
use core_calendar\local\event\entities\action_event_interface;
36+
use core_calendar\output\humantimeperiod;
37+
use core_course\external\course_summary_exporter;
38+
use core\external\coursecat_summary_exporter;
39+
use renderer_base;
40+
use core\url;
4041

4142
/**
4243
* Class for displaying a calendar event.
@@ -369,18 +370,23 @@ protected function get_other_values(renderer_base $output) {
369370
$values['canedit'] = calendar_edit_event_allowed($legacyevent, true);
370371
$values['candelete'] = calendar_delete_event_allowed($legacyevent);
371372

372-
$deleteurl = new moodle_url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
373+
$deleteurl = new url('/calendar/delete.php', ['id' => $event->get_id(), 'course' => $courseid]);
373374
$values['deleteurl'] = $deleteurl->out(false);
374375

375-
$editurl = new moodle_url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
376+
$editurl = new url('/calendar/event.php', ['action' => 'edit', 'id' => $event->get_id(),
376377
'course' => $courseid]);
377378
$values['editurl'] = $editurl->out(false);
378-
$viewurl = new moodle_url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
379+
$viewurl = new url('/calendar/view.php', ['view' => 'day', 'course' => $courseid,
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 = humantimeperiod::create_from_timestamp(
385+
starttimestamp: $legacyevent->timestart,
386+
endtimestamp: $legacyevent->timestart + $legacyevent->timeduration,
387+
link: new 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

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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_if_present(__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_if_present(__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_if_present(__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+
}

0 commit comments

Comments
 (0)