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 @@

{{fullname}}

{{lastmessage}}

+
+ {{unreadcount}} +
{{#contexturl}} diff --git a/theme/bootstrapbase/less/moodle/popover_region.less b/theme/bootstrapbase/less/moodle/popover_region.less index bc3822b1d2c..87754fa01e5 100644 --- a/theme/bootstrapbase/less/moodle/popover_region.less +++ b/theme/bootstrapbase/less/moodle/popover_region.less @@ -637,6 +637,7 @@ height: 100%; width: 20%; display: inline-block; + text-align: center; img { height: 100%; @@ -664,9 +665,23 @@ margin: 0; } } + + .unread-count-container { + display: none; + } } &.unread { background-color: #f1f1f1; + + .content-item-body { + width: 65%; + } + + .unread-count-container { + display: inline-block; + width: 10%; + text-align: center; + } } } } diff --git a/theme/bootstrapbase/style/moodle.css b/theme/bootstrapbase/style/moodle.css index f5f6d1d4ed1..e065b7a1740 100644 --- a/theme/bootstrapbase/style/moodle.css +++ b/theme/bootstrapbase/style/moodle.css @@ -7848,6 +7848,7 @@ body.path-question-type .mform fieldset.hidden { height: 100%; width: 20%; display: inline-block; + text-align: center; } .popover-region-messages.popover-region .popover-region-container .popover-region-content-container .popover-region-content .content-item-container .content-item .profile-image-container img { height: 100%; @@ -7871,9 +7872,20 @@ body.path-question-type .mform fieldset.hidden { .popover-region-messages.popover-region .popover-region-container .popover-region-content-container .popover-region-content .content-item-container .content-item .content-item-body p { margin: 0; } +.popover-region-messages.popover-region .popover-region-container .popover-region-content-container .popover-region-content .content-item-container .content-item .unread-count-container { + display: none; +} .popover-region-messages.popover-region .popover-region-container .popover-region-content-container .popover-region-content .content-item-container.unread { background-color: #f1f1f1; } +.popover-region-messages.popover-region .popover-region-container .popover-region-content-container .popover-region-content .content-item-container.unread .content-item-body { + width: 65%; +} +.popover-region-messages.popover-region .popover-region-container .popover-region-content-container .popover-region-content .content-item-container.unread .unread-count-container { + display: inline-block; + width: 10%; + text-align: center; +} .popover-region-messages.popover-region .popover-region-container .popover-region-content-container.loading .popover-region-content .messages:empty + .empty-message { display: none; }