Skip to content

Commit

Permalink
MDL-72585 message: Return notifications iconurl in get_messages WS
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Sep 30, 2021
1 parent 214adb7 commit 1d388e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 16 additions & 2 deletions message/externallib.php
Expand Up @@ -1990,7 +1990,7 @@ public static function get_messages_parameters() {
*/
public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true,
$newestfirst = true, $limitfrom = 0, $limitnum = 0) {
global $CFG, $USER;
global $CFG, $USER, $PAGE;

$warnings = array();

Expand Down Expand Up @@ -2088,13 +2088,26 @@ public static function get_messages($useridto, $useridfrom = 0, $type = 'both',
}
foreach ($messages as $mid => $message) {

// Do not return deleted messages.
if (!$message->notification) {
// Do not return deleted messages.
if (($useridto == $USER->id and $message->timeusertodeleted) or
($useridfrom == $USER->id and $message->timeuserfromdeleted)) {
unset($messages[$mid]);
continue;
}
} else {
// Return iconurl for notifications.
if (!isset($output)) {
$output = $PAGE->get_renderer('core');
}

if (!empty($message->component) && substr($message->component, 0, 4) == 'mod_') {
$iconurl = $output->image_url('icon', $message->component);
} else {
$iconurl = $output->image_url('i/marker', 'core');
}

$message->iconurl = clean_param($iconurl->out(), PARAM_URL);
}

// We need to get the user from the query.
Expand Down Expand Up @@ -2167,6 +2180,7 @@ public static function get_messages_returns() {
'eventtype' => new external_value(PARAM_TEXT, 'The type of notification', VALUE_OPTIONAL),
'customdata' => new external_value(PARAM_RAW, 'Custom data to be passed to the message processor.
The data here is serialised using json_encode().', VALUE_OPTIONAL),
'iconurl' => new external_value(PARAM_URL, 'URL for icon, only for notifications.', VALUE_OPTIONAL),
), 'message'
)
),
Expand Down
4 changes: 3 additions & 1 deletion message/tests/externallib_test.php
Expand Up @@ -1292,7 +1292,7 @@ public function test_search_contacts() {
* Test get_messages.
*/
public function test_get_messages() {
global $CFG, $DB;
global $CFG, $DB, $PAGE;
$this->resetAfterTest(true);

$this->preventResetByRollback();
Expand Down Expand Up @@ -1478,6 +1478,8 @@ public function test_get_messages() {
$this->assertObjectHasAttribute('datakey', json_decode($messages['messages'][0]['customdata']));
$this->assertEquals('mod_feedback', $messages['messages'][0]['component']);
$this->assertEquals('submission', $messages['messages'][0]['eventtype']);
$feedbackicon = clean_param($PAGE->get_renderer('core')->image_url('icon', 'mod_feedback')->out(), PARAM_URL);
$this->assertEquals($feedbackicon, $messages['messages'][0]['iconurl']);

// Test warnings.
$CFG->messaging = 0;
Expand Down

0 comments on commit 1d388e1

Please sign in to comment.