diff --git a/calendar/classes/external/event_exporter_base.php b/calendar/classes/external/event_exporter_base.php index ba11090e791..aca99fefafa 100644 --- a/calendar/classes/external/event_exporter_base.php +++ b/calendar/classes/external/event_exporter_base.php @@ -262,6 +262,9 @@ class event_exporter_base extends exporter { 'formattedtime' => [ 'type' => PARAM_RAW, ], + 'formattedlocation' => [ + 'type' => PARAM_RAW, + ], 'isactionevent' => [ 'type' => PARAM_BOOL ], @@ -371,6 +374,7 @@ class event_exporter_base extends exporter { $values['viewurl'] = $viewurl->out(false); $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false, $timesort); + $values['formattedlocation'] = calendar_format_event_location($legacyevent); if ($group = $event->get_group()) { $values['groupname'] = format_string($group->get('name'), true, diff --git a/calendar/lib.php b/calendar/lib.php index bf73338ea54..87b0aa3d345 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2554,6 +2554,25 @@ function calendar_format_event_time($event, $now, $linkparams = null, $usecommon return $eventtime; } +/** + * Format event location property + * + * @param calendar_event $event + * @return string + */ +function calendar_format_event_location(calendar_event $event): string { + $location = format_text($event->location, FORMAT_PLAIN, ['context' => $event->context]); + + // If it looks like a link, convert it to one. + if (preg_match('/^https?:\/\//i', $location) && clean_param($location, PARAM_URL)) { + $location = \html_writer::link($location, $location, [ + 'title' => get_string('eventnamelocation', 'core_calendar', ['name' => $event->name, 'location' => $location]), + ]); + } + + return $location; +} + /** * Checks to see if the requested type of event should be shown for the given user. * diff --git a/calendar/templates/event_details.mustache b/calendar/templates/event_details.mustache index 0d9bf1e4b21..9b01e90cf90 100644 --- a/calendar/templates/event_details.mustache +++ b/calendar/templates/event_details.mustache @@ -33,10 +33,10 @@ Example context (json): { "formattedtime": "Wednesday, 17 April, 9:27 AM", + "formattedlocation": "Moodle HQ", "normalisedeventtype": "Group", "normalisedeventtypetext": "Group event", "description": "An random event description", - "location": "Moodle HQ", "isactionevent": "true", "course": { "viewurl": "http://mymoodlesite/course/view.php?id=1", @@ -72,12 +72,12 @@
{{{.}}}
{{/description}} -{{#location}} +{{#formattedlocation}}
{{#pix}} i/location, core, {{#str}} location {{/str}} {{/pix}}
{{{.}}}
-{{/location}} +{{/formattedlocation}} {{#iscategoryevent}}
{{#pix}} i/categoryevent, core, {{#str}} category {{/str}} {{/pix}}
diff --git a/calendar/tests/behat/calendar.feature b/calendar/tests/behat/calendar.feature index ebfd64d7b58..861de7f2746 100644 --- a/calendar/tests/behat/calendar.feature +++ b/calendar/tests/behat/calendar.feature @@ -99,6 +99,16 @@ Feature: Perform basic calendar functionality And I follow "Full calendar" Then I should not see "Really awesome event!" + @javascript + Scenario: Create an event containing URL as location + Given I log in as "admin" + And I create a calendar event with form data: + | Type of event | site | + | Event title | Important webinar | + | Location | https://moodle.org | + When I click on "Important webinar" "link" + Then "https://moodle.org" "link" should exist in the "Important webinar" "dialogue" + @javascript Scenario: Delete an event Given I log in as "teacher1" diff --git a/calendar/tests/lib_test.php b/calendar/tests/lib_test.php index a2d3b91de22..f212cdff9e9 100644 --- a/calendar/tests/lib_test.php +++ b/calendar/tests/lib_test.php @@ -14,19 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Contains the class containing unit tests for the calendar lib. - * - * @package core_calendar - * @copyright 2017 Mark Nelson - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ namespace core_calendar; -defined('MOODLE_INTERNAL') || die(); - -require_once(__DIR__ . '/helpers.php'); - /** * Class contaning unit tests for the calendar lib. * @@ -36,6 +25,15 @@ require_once(__DIR__ . '/helpers.php'); */ class lib_test extends \advanced_testcase { + /** + * Load required test libraries + */ + public static function setUpBeforeClass(): void { + global $CFG; + + require_once("{$CFG->dirroot}/calendar/tests/helpers.php"); + } + /** * Tests set up */ @@ -1046,4 +1044,35 @@ class lib_test extends \advanced_testcase { $result = calendar_can_manage_user_event($adminevent); $this->assertEquals(false, $result); } + + /** + * Data provider for {@see test_calendar_format_event_location} + * + * @return array[] + */ + public function calendar_format_event_location_provider(): array { + return [ + 'Empty' => ['', ''], + 'Text' => ['Barcelona', 'Barcelona'], + 'Link (http)' => ['http://example.com', 'http://example.com'], + 'Link (https)' => ['https://example.com', 'https://example.com'], + ]; + } + + /** + * Test formatting event location + * + * @param string $location + * @param string $expectedpattern + * + * @covers ::calendar_format_event_location + * @dataProvider calendar_format_event_location_provider + */ + public function test_calendar_format_event_location(string $location, string $expectedpattern): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + $event = create_event(['location' => $location]); + $this->assertMatchesRegularExpression("|^({$expectedpattern})$|", calendar_format_event_location($event)); + } } diff --git a/calendar/upgrade.txt b/calendar/upgrade.txt index 3349bc7afdd..79c554796e8 100644 --- a/calendar/upgrade.txt +++ b/calendar/upgrade.txt @@ -1,6 +1,10 @@ This files describes API changes in /calendar/* , information provided here is intended especially for developers. +=== 4.1 === +* New method `calendar_format_event_location` which will format the location property of an event, converting any + links into suitable markup + === 4.0 === * The following external functions now accepts an optional parameter 'searchvalue' to search the events: - core_calendar_external::get_calendar_action_events_by_timesort diff --git a/lang/en/calendar.php b/lang/en/calendar.php index aa04c3a7e22..2239d623973 100644 --- a/lang/en/calendar.php +++ b/lang/en/calendar.php @@ -113,6 +113,7 @@ $string['eventkind'] = 'Type of event'; $string['eventname'] = 'Event title'; $string['eventnameandcategory'] = '{$a->category}: {$a->name}'; $string['eventnameandcourse'] = '{$a->course}: {$a->name}'; +$string['eventnamelocation'] = '{$a->name} location: {$a->location}'; $string['eventnone'] = 'No events'; $string['eventrepeat'] = 'Repeats'; $string['events'] = 'Events';