This commit is contained in:
Sara Arjona 2022-06-08 16:10:54 +02:00
commit 67237e7d94
7 changed files with 81 additions and 14 deletions

View file

@ -262,6 +262,9 @@ class event_exporter_base extends exporter {
'formattedtime' => [ 'formattedtime' => [
'type' => PARAM_RAW, 'type' => PARAM_RAW,
], ],
'formattedlocation' => [
'type' => PARAM_RAW,
],
'isactionevent' => [ 'isactionevent' => [
'type' => PARAM_BOOL 'type' => PARAM_BOOL
], ],
@ -371,6 +374,7 @@ class event_exporter_base extends exporter {
$values['viewurl'] = $viewurl->out(false); $values['viewurl'] = $viewurl->out(false);
$values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false, $values['formattedtime'] = calendar_format_event_time($legacyevent, time(), null, false,
$timesort); $timesort);
$values['formattedlocation'] = calendar_format_event_location($legacyevent);
if ($group = $event->get_group()) { if ($group = $event->get_group()) {
$values['groupname'] = format_string($group->get('name'), true, $values['groupname'] = format_string($group->get('name'), true,

View file

@ -2554,6 +2554,25 @@ function calendar_format_event_time($event, $now, $linkparams = null, $usecommon
return $eventtime; 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. * Checks to see if the requested type of event should be shown for the given user.
* *

View file

@ -33,10 +33,10 @@
Example context (json): Example context (json):
{ {
"formattedtime": "Wednesday, 17 April, 9:27 AM", "formattedtime": "Wednesday, 17 April, 9:27 AM",
"formattedlocation": "Moodle HQ",
"normalisedeventtype": "Group", "normalisedeventtype": "Group",
"normalisedeventtypetext": "Group event", "normalisedeventtypetext": "Group event",
"description": "An random event description", "description": "An random event description",
"location": "Moodle HQ",
"isactionevent": "true", "isactionevent": "true",
"course": { "course": {
"viewurl": "http://mymoodlesite/course/view.php?id=1", "viewurl": "http://mymoodlesite/course/view.php?id=1",
@ -72,12 +72,12 @@
<div class="description-content col-11">{{{.}}}</div> <div class="description-content col-11">{{{.}}}</div>
</div> </div>
{{/description}} {{/description}}
{{#location}} {{#formattedlocation}}
<div class="row mt-1"> <div class="row mt-1">
<div class="col-1">{{#pix}} i/location, core, {{#str}} location {{/str}} {{/pix}}</div> <div class="col-1">{{#pix}} i/location, core, {{#str}} location {{/str}} {{/pix}}</div>
<div class="location-content col-11">{{{.}}}</div> <div class="location-content col-11">{{{.}}}</div>
</div> </div>
{{/location}} {{/formattedlocation}}
{{#iscategoryevent}} {{#iscategoryevent}}
<div class="row mt-1"> <div class="row mt-1">
<div class="col-1">{{#pix}} i/categoryevent, core, {{#str}} category {{/str}} {{/pix}}</div> <div class="col-1">{{#pix}} i/categoryevent, core, {{#str}} category {{/str}} {{/pix}}</div>

View file

@ -99,6 +99,16 @@ Feature: Perform basic calendar functionality
And I follow "Full calendar" And I follow "Full calendar"
Then I should not see "Really awesome event!" 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 @javascript
Scenario: Delete an event Scenario: Delete an event
Given I log in as "teacher1" Given I log in as "teacher1"

View file

@ -14,19 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // 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; namespace core_calendar;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/helpers.php');
/** /**
* Class contaning unit tests for the calendar lib. * Class contaning unit tests for the calendar lib.
* *
@ -36,6 +25,15 @@ require_once(__DIR__ . '/helpers.php');
*/ */
class lib_test extends \advanced_testcase { 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 * Tests set up
*/ */
@ -1046,4 +1044,35 @@ class lib_test extends \advanced_testcase {
$result = calendar_can_manage_user_event($adminevent); $result = calendar_can_manage_user_event($adminevent);
$this->assertEquals(false, $result); $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));
}
} }

View file

@ -1,6 +1,10 @@
This files describes API changes in /calendar/* , This files describes API changes in /calendar/* ,
information provided here is intended especially for developers. 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 === === 4.0 ===
* The following external functions now accepts an optional parameter 'searchvalue' to search the events: * The following external functions now accepts an optional parameter 'searchvalue' to search the events:
- core_calendar_external::get_calendar_action_events_by_timesort - core_calendar_external::get_calendar_action_events_by_timesort

View file

@ -113,6 +113,7 @@ $string['eventkind'] = 'Type of event';
$string['eventname'] = 'Event title'; $string['eventname'] = 'Event title';
$string['eventnameandcategory'] = '{$a->category}: {$a->name}'; $string['eventnameandcategory'] = '{$a->category}: {$a->name}';
$string['eventnameandcourse'] = '{$a->course}: {$a->name}'; $string['eventnameandcourse'] = '{$a->course}: {$a->name}';
$string['eventnamelocation'] = '{$a->name} location: {$a->location}';
$string['eventnone'] = 'No events'; $string['eventnone'] = 'No events';
$string['eventrepeat'] = 'Repeats'; $string['eventrepeat'] = 'Repeats';
$string['events'] = 'Events'; $string['events'] = 'Events';