From 2356454f21a8ddbc79abd367df674ed9941bc62f Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 27 Mar 2019 12:54:59 +0100 Subject: [PATCH] MDL-60680 notifications: Include action buttons information --- badges/tests/badgeslib_test.php | 2 +- competency/tests/lib_test.php | 4 +-- lang/en/moodle.php | 1 + message/classes/api.php | 29 +++++++++++---- message/lib.php | 6 ++++ .../output/popup/tests/externallib_test.php | 9 ++++- message/tests/api_test.php | 35 +++++++++++++------ mod/assign/tests/locallib_test.php | 2 +- mod/feedback/tests/external_test.php | 8 ++--- .../classes/task/send_user_notifications.php | 3 ++ mod/forum/tests/mail_test.php | 4 ++- mod/quiz/tests/external_test.php | 1 + 12 files changed, 77 insertions(+), 27 deletions(-) diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index f4493fbbce360..4d9604e703607 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -309,7 +309,7 @@ public function test_badge_awards() { $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); diff --git a/competency/tests/lib_test.php b/competency/tests/lib_test.php index dd185a18d0c4c..dcfe80e99a305 100644 --- a/competency/tests/lib_test.php +++ b/competency/tests/lib_test.php @@ -97,7 +97,7 @@ public function test_comment_add_user_competency() { $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 @@ public function test_comment_add_plan() { $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. diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 46566af458d57..66c6f9c2e9576 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -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'; diff --git a/message/classes/api.php b/message/classes/api.php index 5fcf569b7855c..3824b746b0bcc 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -1915,6 +1915,15 @@ public static function send_message_to_conversation(int $userid, int $conversati $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 @@ public static function send_message_to_conversation(int $userid, int $conversati // 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 @@ public static function create_contact_request(int $userid, int $requesteduserid) $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); diff --git a/message/lib.php b/message/lib.php index 527ce2f23430a..9493f9e1cb858 100644 --- a/message/lib.php +++ b/message/lib.php @@ -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); } diff --git a/message/output/popup/tests/externallib_test.php b/message/output/popup/tests/externallib_test.php index 0eb88bb8994e1..94fe0dbc42a97 100644 --- a/message/output/popup/tests/externallib_test.php +++ b/message/output/popup/tests/externallib_test.php @@ -97,7 +97,14 @@ public function test_get_popup_notifications_as_recipient() { $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); diff --git a/message/tests/api_test.php b/message/tests/api_test.php index d47d24f88cc57..30b023a01ff8a 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -4758,8 +4758,10 @@ public function test_create_contact_request() { $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 @@ public function test_send_message_to_conversation_individual_conversation() { // 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 @@ public function test_send_message_to_conversation_group_conversation() { // 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 @@ public function test_send_message_to_conversation_group_conversation() { $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 @@ public function test_send_message_to_conversation_linked_group_conversation() { $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 @@ public function test_send_message_to_conversation_linked_group_conversation() { $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); + } /** diff --git a/mod/assign/tests/locallib_test.php b/mod/assign/tests/locallib_test.php index 7ee734a9baa07..e2fe5852fa40a 100644 --- a/mod/assign/tests/locallib_test.php +++ b/mod/assign/tests/locallib_test.php @@ -1521,7 +1521,7 @@ public function test_cron() { $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); diff --git a/mod/feedback/tests/external_test.php b/mod/feedback/tests/external_test.php index 838dfc2f3aff4..d920b9b84ac75 100644 --- a/mod/feedback/tests/external_test.php +++ b/mod/feedback/tests/external_test.php @@ -519,7 +519,7 @@ public function test_process_page() { $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 @@ public function test_process_page() { $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); diff --git a/mod/forum/classes/task/send_user_notifications.php b/mod/forum/classes/task/send_user_notifications.php index 20ad9608daf31..7b22ba0ea6c54 100644 --- a/mod/forum/classes/task/send_user_notifications.php +++ b/mod/forum/classes/task/send_user_notifications.php @@ -360,6 +360,9 @@ protected function send_post($course, $forum, $discussion, $post, $cm, $context) '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); diff --git a/mod/forum/tests/mail_test.php b/mod/forum/tests/mail_test.php index 46ac30893c486..e6ecae3b791b7 100644 --- a/mod/forum/tests/mail_test.php +++ b/mod/forum/tests/mail_test.php @@ -1530,11 +1530,13 @@ public function test_notification_customdata() { $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); } } diff --git a/mod/quiz/tests/external_test.php b/mod/quiz/tests/external_test.php index 69e8ba162aaa0..a5072f2f4326f 100644 --- a/mod/quiz/tests/external_test.php +++ b/mod/quiz/tests/external_test.php @@ -1193,6 +1193,7 @@ public function test_process_attempt() { $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)) {