MDL-60680 notifications: Return custom data in notifications

This commit is contained in:
Juan Leyva
2019-04-25 14:55:18 +02:00
parent 26e778d1b7
commit 36fa0ec900
18 changed files with 325 additions and 15 deletions
+26 -3
View File
@@ -1887,7 +1887,7 @@ class api {
*/
public static function send_message_to_conversation(int $userid, int $conversationid, string $message,
int $format) : \stdClass {
global $DB;
global $DB, $PAGE;
if (!self::can_send_message_to_conversation($userid, $conversationid)) {
throw new \moodle_exception("User $userid cannot send a message to conversation $conversationid");
@@ -1897,7 +1897,7 @@ class api {
$eventdata->courseid = 1;
$eventdata->component = 'moodle';
$eventdata->name = 'instantmessage';
$eventdata->userfrom = $userid;
$eventdata->userfrom = \core_user::get_user($userid);
$eventdata->convid = $conversationid;
if ($format == FORMAT_HTML) {
@@ -1915,6 +1915,24 @@ class api {
$eventdata->timecreated = time();
$eventdata->notification = 0;
$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,
];
}
} else if ($conv->type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
$userpicture = new \user_picture($eventdata->userfrom);
$eventdata->customdata = [
'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
];
}
$messageid = message_send($eventdata);
$messagerecord = $DB->get_record('messages', ['id' => $messageid], 'id, useridfrom, fullmessage,
@@ -2511,7 +2529,7 @@ class api {
* @return \stdClass the request
*/
public static function create_contact_request(int $userid, int $requesteduserid) : \stdClass {
global $DB;
global $DB, $PAGE;
$request = new \stdClass();
$request->userid = $userid;
@@ -2542,6 +2560,11 @@ class api {
$message->fullmessagehtml = $fullmessage;
$message->smallmessage = '';
$message->contexturl = $url->out(false);
$userpicture = new \user_picture($userfrom);
$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),
];
message_send($message);