MDL-64058 core_message: fix logic driving conversation section expansion
authorJake Dallimore <jake@moodle.com>
Mon, 19 Nov 2018 07:32:59 +0000 (15:32 +0800)
committerJake Dallimore <jake@moodle.com>
Tue, 20 Nov 2018 00:56:37 +0000 (08:56 +0800)
Unread conversations should be given priority over read. If one or more
sections have unread conversations, prioritise favourites over group and
group over individual. The same applies if all conversations are read.

message/lib.php

index 0fdca17..44e1eb5 100644 (file)
@@ -869,6 +869,22 @@ function core_message_standard_after_main_region_html() {
     // Get the unread counts for the current user.
     $unreadcounts = \core_message\api::get_unread_conversation_counts($USER->id);
 
+    // Determine which section will be expanded.
+    // Default behaviour - if no unread counts exist.
+    $favouritesexpanded = !empty($favouriteconversationcount);
+    $groupmessagesexpanded = empty($favouriteconversationcount) && !empty($groupconversationcount);
+    $messagesexpanded = empty($favouriteconversationcount) && empty($groupconversationcount);
+
+    // There is an unread conversation somewhere, so that takes priority.
+    if ($unreadcounts['favourites'] > 0 || $unreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP] > 0 ||
+            $unreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL] > 0) {
+        $favouritesexpanded = $unreadcounts['favourites'] > 0;
+        $groupmessagesexpanded = !$favouritesexpanded &&
+            $unreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP] > 0;
+        $messagesexpanded = !$groupmessagesexpanded && !$favouritesexpanded &&
+            $unreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL] > 0;
+    }
+
     return $renderer->render_from_template('core_message/message_drawer', [
         'contactrequestcount' => $requestcount,
         'loggedinuser' => [
@@ -877,7 +893,7 @@ function core_message_standard_after_main_region_html() {
         ],
         'overview' => [
             'messages' => [
-                'expanded' => empty($favouriteconversationcount) && empty($groupconversationcount),
+                'expanded' => $messagesexpanded,
                 'count' => [
                     'unread' => $unreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL],
                     'total' => $individualconversationcount
@@ -885,7 +901,7 @@ function core_message_standard_after_main_region_html() {
                 'placeholders' => array_fill(0, $individualconversationcount, true)
             ],
             'groupmessages' => [
-                'expanded' => empty($favouriteconversationcount) && !empty($groupconversationcount),
+                'expanded' => $groupmessagesexpanded,
                 'count' => [
                     'unread' => $unreadcounts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP],
                     'total' => $groupconversationcount
@@ -893,7 +909,7 @@ function core_message_standard_after_main_region_html() {
                 'placeholders' => array_fill(0, $groupconversationcount, true)
             ],
             'favourites' => [
-                'expanded' => !empty($favouriteconversationcount),
+                'expanded' => $favouritesexpanded,
                 'count' => [
                     'unread' => $unreadcounts['favourites'],
                     'total' => $favouriteconversationcount