MDL-60680 notifications: Include action buttons information
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 27 Mar 2019 11:54:59 +0000 (12:54 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 25 Apr 2019 12:55:57 +0000 (14:55 +0200)
12 files changed:
badges/tests/badgeslib_test.php
competency/tests/lib_test.php
lang/en/moodle.php
message/classes/api.php
message/lib.php
message/output/popup/tests/externallib_test.php
message/tests/api_test.php
mod/assign/tests/locallib_test.php
mod/feedback/tests/external_test.php
mod/forum/classes/task/send_user_notifications.php
mod/forum/tests/mail_test.php
mod/quiz/tests/external_test.php

index f4493fb..4d9604e 100644 (file)
@@ -309,7 +309,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
         $this->assertCount(1, $messages);
         $message = array_pop($messages);
         // Check we have the expected data.
-        $customdata =  json_decode($message->customdata);
+        $customdata = json_decode($message->customdata);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
         $this->assertObjectHasAttribute('hash', $customdata);
 
index dd185a1..dcfe80e 100644 (file)
@@ -97,7 +97,7 @@ class core_competency_lib_testcase extends advanced_testcase {
         $this->assertEquals($expectedurl->out(false), $message->contexturl);
         $this->assertEquals($expectedurlname, $message->contexturlname);
         // Test customdata.
-        $customdata =  json_decode($message->customdata);
+        $customdata = json_decode($message->customdata);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
         $this->assertContains('tokenpluginfile.php', $customdata->notificationiconurl);
         $userpicture = new \user_picture($u1);
@@ -226,7 +226,7 @@ class core_competency_lib_testcase extends advanced_testcase {
         $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
         $this->assertEquals($u1->id, $message->useridto);
         // Test customdata.
-        $customdata =  json_decode($message->customdata);
+        $customdata = json_decode($message->customdata);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
 
         // Post a comment in a plan with reviewer. The reviewer is messaged.
index 46566af..66c6f9c 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 $string['abouttobeinstalled'] = 'about to be installed';
+$string['accept'] = 'Accept';
 $string['action'] = 'Action';
 $string['actionchoice'] = 'What do you want to do with the file \'{$a}\'?';
 $string['actions'] = 'Actions';
index 5fcf569..3824b74 100644 (file)
@@ -1915,6 +1915,15 @@ class api {
 
         $eventdata->timecreated     = time();
         $eventdata->notification    = 0;
+        // Custom data for event.
+        $customdata = [
+            'actionbuttons' => [
+                'send' => get_string('send', 'message'),
+            ],
+            'placeholders' => [
+                'send' => get_string('writeamessage', 'message'),
+            ],
+        ];
 
         $conv = $DB->get_record('message_conversations', ['id' => $conversationid]);
         if ($conv->type == self::MESSAGE_CONVERSATION_TYPE_GROUP) {
@@ -1922,16 +1931,20 @@ class api {
             // Conversation image.
             $imageurl = isset($convextrafields[$conv->id]) ? $convextrafields[$conv->id]['imageurl'] : null;
             if ($imageurl) {
-                $eventdata->customdata = [
-                    'notificationiconurl' => $imageurl,
-                ];
+                $customdata['notificationiconurl'] = $imageurl;
+            }
+            // Conversation name.
+            if (is_null($conv->contextid)) {
+                $convcontext = \context_user::instance($userid);
+            } else {
+                $convcontext = \context::instance_by_id($conv->contextid);
             }
+            $customdata['conversationname'] = format_string($conv->name, true, ['context' => $convcontext]);
         } else if ($conv->type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
             $userpicture = new \user_picture($eventdata->userfrom);
-            $eventdata->customdata = [
-                'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
-            ];
+            $customdata['notificationiconurl'] = $userpicture->get_url($PAGE)->out(false);
         }
+        $eventdata->customdata = $customdata;
 
         $messageid = message_send($eventdata);
 
@@ -2564,6 +2577,10 @@ class api {
         $userpicture->includetoken = $userto->id; // Generate an out-of-session token for the user receiving the message.
         $message->customdata = [
             'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
+            'actionbuttons' => [
+                'accept' => get_string_manager()->get_string('accept', 'moodle', null, $userto->lang),
+                'reject' => get_string_manager()->get_string('reject', 'moodle', null, $userto->lang),
+            ],
         ];
 
         message_send($message);
index 527ce2f..9493f9e 100644 (file)
@@ -356,6 +356,12 @@ function message_post_message($userfrom, $userto, $message, $format) {
     $userpicture->includetoken = $userto->id; // Generate an out-of-session token for the user receiving the message.
     $eventdata->customdata = [
         'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
+        'actionbuttons' => [
+            'send' => get_string_manager()->get_string('send', 'message', null, $eventdata->userto->lang),
+        ],
+        'placeholders' => [
+            'send' => get_string_manager()->get_string('writeamessage', 'message', null, $eventdata->userto->lang),
+        ],
     ];
     return message_send($eventdata);
 }
index 0eb88bb..94fe0db 100644 (file)
@@ -97,7 +97,14 @@ class message_popup_externallib_testcase extends advanced_testcase {
         $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
         $this->assertCount(4, $result['notifications']);
         // Check we receive custom data as a unserialisable json.
-        $this->assertObjectHasAttribute('datakey', json_decode($result['notifications'][0]->customdata));
+        $found = 0;
+        foreach ($result['notifications'] as $notification) {
+            if (!empty($notification->customdata)) {
+                $this->assertObjectHasAttribute('datakey', json_decode($notification->customdata));
+                $found++;
+            }
+        }
+        $this->assertEquals(2, $found);
 
         $this->setUser($recipient);
         $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
index d47d24f..30b023a 100644 (file)
@@ -4758,8 +4758,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $messages = $sink->get_messages();
         $sink->close();
         // Test customdata.
-        $customdata =  json_decode($messages[0]->customdata);
+        $customdata = json_decode($messages[0]->customdata);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
+        $this->assertObjectHasAttribute('actionbuttons', $customdata);
+        $this->assertCount(2, (array) $customdata->actionbuttons);
 
         $this->assertEquals($user1->id, $request->userid);
         $this->assertEquals($user2->id, $request->requesteduserid);
@@ -5783,13 +5785,17 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
 
         // Send a message to an individual conversation.
         $sink = $this->redirectEvents();
-        $messagesSink = $this->redirectMessages();
+        $messagessink = $this->redirectMessages();
         $message1 = \core_message\api::send_message_to_conversation($user1->id, $ic1->id, 'this is a message', FORMAT_MOODLE);
         $events = $sink->get_events();
-        $messages = $messagesSink->get_messages();
+        $messages = $messagessink->get_messages();
         // Test customdata.
-        $customdata =  json_decode($messages[0]->customdata);
+        $customdata = json_decode($messages[0]->customdata);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
+        $this->assertObjectHasAttribute('actionbuttons', $customdata);
+        $this->assertCount(1, (array) $customdata->actionbuttons);
+        $this->assertObjectHasAttribute('placeholders', $customdata);
+        $this->assertCount(1, (array) $customdata->placeholders);
 
         // Verify the message returned.
         $this->assertInstanceOf(\stdClass::class, $message1);
@@ -5829,10 +5835,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
 
         // Send a message to a group conversation.
         $sink = $this->redirectEvents();
-        $messagesSink = $this->redirectMessages();
+        $messagessink = $this->redirectMessages();
         $message1 = \core_message\api::send_message_to_conversation($user1->id, $gc2->id, 'message to the group', FORMAT_MOODLE);
         $events = $sink->get_events();
-        $messages = $messagesSink->get_messages();
+        $messages = $messagessink->get_messages();
         // Verify the message returned.
         $this->assertInstanceOf(\stdClass::class, $message1);
         $this->assertObjectHasAttribute('id', $message1);
@@ -5840,7 +5846,12 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertAttributeEquals('message to the group', 'text', $message1);
         $this->assertObjectHasAttribute('timecreated', $message1);
         // Test customdata.
-        $this->assertObjectHasAttribute('customdata', $messages[0]);    // No group image means no customdata.
+        $customdata = json_decode($messages[0]->customdata);
+        $this->assertObjectHasAttribute('actionbuttons', $customdata);
+        $this->assertCount(1, (array) $customdata->actionbuttons);
+        $this->assertObjectHasAttribute('placeholders', $customdata);
+        $this->assertCount(1, (array) $customdata->placeholders);
+        $this->assertObjectNotHasAttribute('notificationiconurl', $customdata);    // No group image means no image.
 
         // Verify events. Note: the event is a message read event because of an if (PHPUNIT) conditional within message_send(),
         // however, we can still determine the number and ids of any recipients this way.
@@ -5893,10 +5904,10 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $sink = $this->redirectMessages();
 
         // Send a message to a group conversation.
-        $messagesSink = $this->redirectMessages();
+        $messagessink = $this->redirectMessages();
         $message1 = \core_message\api::send_message_to_conversation($user1->id, $conversations[0]->id,
             'message to the group', FORMAT_MOODLE);
-        $messages = $messagesSink->get_messages();
+        $messages = $messagessink->get_messages();
         // Verify the message returned.
         $this->assertInstanceOf(\stdClass::class, $message1);
         $this->assertObjectHasAttribute('id', $message1);
@@ -5904,9 +5915,11 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertAttributeEquals('message to the group', 'text', $message1);
         $this->assertObjectHasAttribute('timecreated', $message1);
         // Test customdata.
-        $customdata =  json_decode($messages[0]->customdata);
+        $customdata = json_decode($messages[0]->customdata);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
-        $this->assertEquals($groupimageurl, $customdata->notificationiconurl);;
+        $this->assertEquals($groupimageurl, $customdata->notificationiconurl);
+        $this->assertEquals($group->name, $customdata->conversationname);
+
     }
 
     /**
index 7ee734a..e2fe585 100644 (file)
@@ -1521,7 +1521,7 @@ class mod_assign_locallib_testcase extends advanced_testcase {
         $this->assertEquals(1, $messages[0]->notification);
         $this->assertEquals($assign->get_instance()->name, $messages[0]->contexturlname);
         // Test customdata.
-        $customdata =  json_decode($messages[0]->customdata);
+        $customdata = json_decode($messages[0]->customdata);
         $this->assertEquals($assign->get_course_module()->id, $customdata->cmid);
         $this->assertEquals($assign->get_instance()->id, $customdata->instance);
         $this->assertEquals('feedbackavailable', $customdata->messagetype);
index 838dfc2..d920b9b 100644 (file)
@@ -519,7 +519,7 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
         $this->assertCount(7, $tmpitems);   // 2 from the first page + 5 from the second page.
 
         // And finally, save everything! We are going to modify one previous recorded value.
-        $messagesSink = $this->redirectMessages();
+        $messagessink = $this->redirectMessages();
         $data[2]['value'] = 2; // 2 is value of the option 'b'.
         $secondpagedata = [$data[2], $data[3], $data[4], $data[5], $data[6]];
         $result = mod_feedback_external::process_page($this->feedback->id, 1, $secondpagedata);
@@ -544,10 +544,10 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals(0, $completed->courseid);
 
         // Test notifications sent.
-        $messages = $messagesSink->get_messages();
-        $messagesSink->close();
+        $messages = $messagessink->get_messages();
+        $messagessink->close();
         // Test customdata.
-        $customdata =  json_decode($messages[0]->customdata);
+        $customdata = json_decode($messages[0]->customdata);
         $this->assertEquals($this->feedback->id, $customdata->instance);
         $this->assertEquals($this->feedback->cmid, $customdata->cmid);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
index 20ad960..7b22ba0 100644 (file)
@@ -360,6 +360,9 @@ class send_user_notifications extends \core\task\adhoc_task {
             'discussionid' => $discussion->id,
             'postid' => $post->id,
             'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
+            'actionbuttons' => [
+                'reply' => get_string_manager()->get_string('reply', 'forum', null, $eventdata->userto->lang),
+            ],
         ];
 
         return message_send($eventdata);
index 46ac308..e6ecae3 100644 (file)
@@ -1530,11 +1530,13 @@ class mod_forum_mail_testcase extends advanced_testcase {
         $this->send_notifications_and_assert($author, [$post]);
         $this->send_notifications_and_assert($commenter, [$post]);
         $messages = $this->messagesink->get_messages();
-        $customdata =  json_decode($messages[0]->customdata);
+        $customdata = json_decode($messages[0]->customdata);
         $this->assertEquals($forum->id, $customdata->instance);
         $this->assertEquals($forum->cmid, $customdata->cmid);
         $this->assertEquals($post->id, $customdata->postid);
         $this->assertEquals($discussion->id, $customdata->discussionid);
         $this->assertObjectHasAttribute('notificationiconurl', $customdata);
+        $this->assertObjectHasAttribute('actionbuttons', $customdata);
+        $this->assertCount(1, (array) $customdata->actionbuttons);
     }
 }
index 69e8ba1..a5072f2 100644 (file)
@@ -1193,6 +1193,7 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
         $result = external_api::clean_returnvalue(mod_quiz_external::process_attempt_returns(), $result);
         $this->assertEquals(quiz_attempt::FINISHED, $result['state']);
         $messages = $sink->get_messages();
+        $message = reset($messages);
         $sink->close();
         // Test customdata.
         if (!empty($message->customdata)) {