diff --git a/message/classes/helper.php b/message/classes/helper.php index a7b7a84286c..8c74eb4110b 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -151,6 +151,7 @@ class helper { // Check if the user is online. $data->isonline = \core_message\helper::is_online($userfields->lastaccess); $data->isread = isset($contact->isread) ? $contact->isread : 0; + $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null; return new \core_message\output\messagearea\contact($data); } diff --git a/message/classes/output/messagearea/contact.php b/message/classes/output/messagearea/contact.php index 5a2f016aef4..a6a64ef2949 100644 --- a/message/classes/output/messagearea/contact.php +++ b/message/classes/output/messagearea/contact.php @@ -76,6 +76,7 @@ class contact implements templatable, renderable { } $contact->isonline = $this->contact->isonline; $contact->isread = $this->contact->isread; + $contact->unreadcount = $this->contact->unreadcount; return $contact; } diff --git a/message/externallib.php b/message/externallib.php index dea9798f4ce..17053a86ec5 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -752,6 +752,7 @@ class core_message_external extends external_api { 'lastmessage' => new external_value(PARAM_RAW, 'The user\'s last message, null if none.'), 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'), 'isread' => new external_value(PARAM_BOOL, 'If the user has read the message'), + 'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation'), ) ) ) diff --git a/message/lib.php b/message/lib.php index 48b4a10d992..c6e4b927910 100644 --- a/message/lib.php +++ b/message/lib.php @@ -411,6 +411,12 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { $sql = str_replace('1 as readtable', '0 as readtable', $sql); $unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); + $unreadcountssql = 'SELECT useridfrom, count(*) as count + FROM {message} + WHERE useridto = :userid + GROUP BY useridfrom'; + $unreadcounts = $DB->get_records_sql($unreadcountssql, array('userid' => $user->id)); + // Union the 2 result sets together looking for the message with the most // recent timecreated for each other user. // $conversation->id (the array key) is the other user's ID. @@ -419,8 +425,9 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { foreach ($conversation_arrays as $conversation_array) { foreach ($conversation_array as $conversation) { // Only consider it unread if $user has unread messages. - if (!$conversation->readtable && $conversation->useridto == $user->id) { + if (isset($unreadcounts[$conversation->useridfrom])) { $conversation->isread = 0; + $conversation->unreadcount = $unreadcounts[$conversation->useridfrom]->count; } else { $conversation->isread = 1; } @@ -429,6 +436,13 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { $conversations[$conversation->id] = $conversation; } else { $current = $conversations[$conversation->id]; + // We need to maintain the isread and unreadcount values from existing + // parts of the conversation if we're replacing it. + $conversation->isread = ($conversation->isread && $current->isread); + if (isset($current->unreadcount) && !isset($conversation->unreadcount)) { + $conversation->unreadcount = $current->unreadcount; + } + if ($current->timecreated < $conversation->timecreated) { $conversations[$conversation->id] = $conversation; } else if ($current->timecreated == $conversation->timecreated) { diff --git a/message/templates/message_content_item.mustache b/message/templates/message_content_item.mustache index 856515ae1c6..e32faf72801 100644 --- a/message/templates/message_content_item.mustache +++ b/message/templates/message_content_item.mustache @@ -50,6 +50,9 @@
{{lastmessage}}
+