Skip to content

Commit

Permalink
MDL-60680 notifications: Include action buttons information
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Apr 25, 2019
1 parent 36fa0ec commit 2356454
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 27 deletions.
2 changes: 1 addition & 1 deletion badges/tests/badgeslib_test.php
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions competency/tests/lib_test.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Expand Up @@ -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';
Expand Down
29 changes: 23 additions & 6 deletions message/classes/api.php
Expand Up @@ -1915,23 +1915,36 @@ 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) {
$convextrafields = self::get_linked_conversation_extra_fields([$conv]);
// 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);

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions message/lib.php
Expand Up @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion message/output/popup/tests/externallib_test.php
Expand Up @@ -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);
Expand Down
35 changes: 24 additions & 11 deletions message/tests/api_test.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -5829,18 +5835,23 @@ 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);
$this->assertAttributeEquals($user1->id, 'useridfrom', $message1);
$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.
Expand Down Expand Up @@ -5893,20 +5904,22 @@ 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);
$this->assertAttributeEquals($user1->id, 'useridfrom', $message1);
$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);

}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod/assign/tests/locallib_test.php
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions mod/feedback/tests/external_test.php
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions mod/forum/classes/task/send_user_notifications.php
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion mod/forum/tests/mail_test.php
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions mod/quiz/tests/external_test.php
Expand Up @@ -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)) {
Expand Down

0 comments on commit 2356454

Please sign in to comment.