MDL-72585 message: Return notifications iconurl in get_messages WS
authorDani Palou <dani@moodle.com>
Tue, 28 Sep 2021 07:26:02 +0000 (09:26 +0200)
committerDani Palou <dani@moodle.com>
Thu, 30 Sep 2021 06:00:27 +0000 (08:00 +0200)
message/externallib.php
message/tests/externallib_test.php

index 84e251a..a2efebb 100644 (file)
@@ -1990,7 +1990,7 @@ class core_message_external extends external_api {
      */
     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();
 
@@ -2088,13 +2088,26 @@ class core_message_external extends external_api {
             }
             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.
@@ -2167,6 +2180,7 @@ class core_message_external extends external_api {
                             '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'
                     )
                 ),
index 087b276..f774236 100644 (file)
@@ -1292,7 +1292,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      * Test get_messages.
      */
     public function test_get_messages() {
-        global $CFG, $DB;
+        global $CFG, $DB, $PAGE;
         $this->resetAfterTest(true);
 
         $this->preventResetByRollback();
@@ -1478,6 +1478,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $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;