Skip to content

Commit

Permalink
MDL-74327 calendar: format event location property if it's a URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jun 8, 2022
1 parent 5500d14 commit 432fbbc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
4 changes: 4 additions & 0 deletions calendar/classes/external/event_exporter_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ protected static function define_other_properties() {
'formattedtime' => [
'type' => PARAM_RAW,
],
'formattedlocation' => [
'type' => PARAM_RAW,
],
'isactionevent' => [
'type' => PARAM_BOOL
],
Expand Down Expand Up @@ -371,6 +374,7 @@ protected function get_other_values(renderer_base $output) {
$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,
Expand Down
19 changes: 19 additions & 0 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 3 additions & 3 deletions calendar/templates/event_details.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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>
Expand Down
10 changes: 10 additions & 0 deletions calendar/tests/behat/calendar.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
51 changes: 40 additions & 11 deletions calendar/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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.
*
Expand All @@ -36,6 +25,15 @@
*/
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
*/
Expand Down Expand Up @@ -1046,4 +1044,35 @@ public function test_calendar_can_manage_user_event() {
$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));
}
}
4 changes: 4 additions & 0 deletions calendar/upgrade.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lang/en/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
$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';
Expand Down

0 comments on commit 432fbbc

Please sign in to comment.