From 477cfba57c0f4b07168d96fb26473a3ca19df485 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 4 May 2016 12:52:45 +0200 Subject: [PATCH] MDL-54049 core_message: Add missing external format text --- calendar/externallib.php | 3 +++ calendar/lib.php | 30 +++++++++++++++++++++++++++ calendar/tests/externallib_test.php | 32 +++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/calendar/externallib.php b/calendar/externallib.php index da95699fab3..2383693de14 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -241,6 +241,9 @@ class core_calendar_external extends external_api { foreach ($eventlist as $eventid => $eventobj) { $event = (array) $eventobj; + // Description formatting. + $calendareventobj = new calendar_event($event); + list($event['description'], $event['format']) = $calendareventobj->format_external_text(); if ($hassystemcap) { // User can see everything, no further check is needed. diff --git a/calendar/lib.php b/calendar/lib.php index 67da3e49e9a..198aca9a0cc 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2671,6 +2671,36 @@ class calendar_event { return false; } } + + /** + * Format the text using the external API. + * This function should we used when text formatting is required in external functions. + * + * @return array an array containing the text formatted and the text format + */ + public function format_external_text() { + + if ($this->editorcontext === null) { + // Switch on the event type to decide upon the appropriate context to use for this event. + $this->editorcontext = $this->properties->context; + + if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course' + && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') { + // We don't have a context here, do a normal format_text. + return array(format_text($this->properties->description, $this->properties->format), $this->properties->format); + } + } + + // Work out the item id for the editor, if this is a repeated event then the files will be associated with the original. + if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) { + $itemid = $this->properties->repeatid; + } else { + $itemid = $this->properties->id; + } + + return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id, + 'calendar', 'event_description', $itemid); + } } /** diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index 7619b2a56bc..dc5041a49f4 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -277,8 +277,28 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { // Let's create a few events. $siteevent = $this->create_calendar_event('site', $USER->id, 'site'); + + // This event will have description with an inline fake image. + $draftidfile = file_get_unused_draft_itemid(); + $usercontext = context_course::instance($course->id); + $filerecord = array( + 'contextid' => $usercontext->id, + 'component' => 'user', + 'filearea' => 'draft', + 'itemid' => $draftidfile, + 'filepath' => '/', + 'filename' => 'fakeimage.png', + ); + $fs = get_file_storage(); + $fs->create_file_from_string($filerecord, 'img contents'); + $record = new stdClass(); $record->courseid = $course->id; + $record->description = array( + 'format' => FORMAT_HTML, + 'text' => 'Text with img ', + 'itemid' => $draftidfile + ); $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record); $userevent = $this->create_calendar_event('user', $USER->id); $record = new stdClass(); @@ -300,6 +320,18 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { $this->assertEquals(5, count($events['events'])); $this->assertEquals(0, count($events['warnings'])); + // Expect the same URL in the description of two different events (because they are repeated). + $coursecontext = context_course::instance($course->id); + $expectedurl = "webservice/pluginfile.php/$coursecontext->id/calendar/event_description/$courseevent->id/fakeimage.png"; + $withdescription = 0; + foreach ($events['events'] as $event) { + if (!empty($event['description'])) { + $withdescription++; + $this->assertContains($expectedurl, $event['description']); + } + } + $this->assertEquals(2, $withdescription); + // Let's play around with caps. $this->setUser($user); $events = core_calendar_external::get_calendar_events($paramevents, $options);