Commit 432fbbc8 authored by Paul Holden's avatar Paul Holden
Browse files

MDL-74327 calendar: format event location property if it's a URL.

parent 5500d143
......@@ -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,
......
......@@ -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.
*
......
......@@ -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 @@
<div class="description-content col-11">{{{.}}}</div>
</div>
{{/description}}
{{#location}}
{{#formattedlocation}}
<div class="row mt-1">
<div class="col-1">{{#pix}} i/location, core, {{#str}} location {{/str}} {{/pix}}</div>
<div class="location-content col-11">{{{.}}}</div>
</div>
{{/location}}
{{/formattedlocation}}
{{#iscategoryevent}}
<div class="row mt-1">
<div class="col-1">{{#pix}} i/categoryevent, core, {{#str}} category {{/str}} {{/pix}}</div>
......
......@@ -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"
......
......@@ -14,19 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the class containing unit tests for the calendar lib.
*
* @package core_calendar
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @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', '<a title=".*" href="http://example.com">http://example.com</a>'],
'Link (https)' => ['https://example.com', '<a title=".*" href="https://example.com">https://example.com</a>'],
];
}
/**
* 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));
}
}
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
......
......@@ -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';
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment