MDL-57968 core_message: Remove multiple unnecessary AJAX requests
authorcescobedo <carlos.escobedo@gmail.com>
Tue, 5 Jun 2018 23:02:00 +0000 (01:02 +0200)
committercescobedo <carlos.escobedo@gmail.com>
Tue, 5 Jun 2018 23:02:00 +0000 (01:02 +0200)
Remove AJAX requests core_message_get_unread_conversations_count and
message_popup_get_unread_popup_notification_count and
render the unread messages and notifications into mustache template.

message/output/popup/amd/build/message_popover_controller.min.js
message/output/popup/amd/build/notification_popover_controller.min.js
message/output/popup/amd/src/message_popover_controller.js
message/output/popup/amd/src/notification_popover_controller.js
message/output/popup/lib.php
message/output/popup/templates/message_popover.mustache
message/output/popup/templates/notification_popover.mustache

index b67fdc8..37fea75 100644 (file)
Binary files a/message/output/popup/amd/build/message_popover_controller.min.js and b/message/output/popup/amd/build/message_popover_controller.min.js differ
index 4d099a4..3ce78cb 100644 (file)
Binary files a/message/output/popup/amd/build/notification_popover_controller.min.js and b/message/output/popup/amd/build/notification_popover_controller.min.js differ
index 499980b..6ea5608 100644 (file)
@@ -57,7 +57,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str',
         this.initialLoad = false;
 
         // Let's find out how many unread messages there are.
-        this.loadUnreadMessageCount();
+        this.unreadCount = this.root.find(SELECTORS.COUNT_CONTAINER).html();
     };
 
     /**
@@ -139,21 +139,6 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str',
         this.root.find(SELECTORS.COUNT_CONTAINER).addClass('hidden');
     };
 
-    /**
-     * Ask the server how many unread messages are left, render the value
-     * as a badge on the menu toggle and update the aria labels on the menu
-     * toggle.
-     *
-     * @method loadUnreadMessageCount
-     */
-    MessagePopoverController.prototype.loadUnreadMessageCount = function() {
-        MessageRepo.countUnreadConversations({useridto: this.userId}).then(function(count) {
-            this.unreadCount = count;
-            this.renderUnreadCount();
-            this.updateButtonAriaLabel();
-        }.bind(this)).catch(Notification.exception);
-    };
-
     /**
      * Render the message data with the appropriate template and add it to the DOM.
      *
index de45501..9ffc65d 100644 (file)
@@ -60,7 +60,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
         this.initialLoad = false;
 
         // Let's find out how many unread notifications there are.
-        this.loadUnreadNotificationCount();
+        this.unreadCount = this.root.find(SELECTORS.COUNT_CONTAINER).html();
     };
 
     /**
@@ -188,21 +188,6 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str', 'core/url',
         this.root.find(SELECTORS.COUNT_CONTAINER).addClass('hidden');
     };
 
-    /**
-     * Ask the server how many unread notifications are left, render the value
-     * as a badge on the menu toggle and update the aria labels on the menu
-     * toggle.
-     *
-     * @method loadUnreadNotificationCount
-     */
-    NotificationPopoverController.prototype.loadUnreadNotificationCount = function() {
-        NotificationRepo.countUnread({useridto: this.userId}).then(function(count) {
-            this.unreadCount = count;
-            this.renderUnreadCount();
-            this.updateButtonAriaLabel();
-        }.bind(this)).catch(DebugNotification.exception);
-    };
-
     /**
      * Find the notification element for the given id.
      *
index 4c4f910..3253109 100644 (file)
@@ -14,8 +14,6 @@
 // You should have received a copy of the GNU General Public License
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
-defined('MOODLE_INTERNAL') || die();
-
 /**
  * Contains standard functions for message_popup.
  *
@@ -24,6 +22,8 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+defined('MOODLE_INTERNAL') || die();
+
 /**
  * Renders the popup.
  *
@@ -45,8 +45,10 @@ function message_popup_render_navbar_output(\renderer_base $renderer) {
 
     // Add the messages popover.
     if (!empty($CFG->messaging)) {
+        $unreadcount = \core_message\api::count_unread_conversations($USER);
         $context = [
             'userid' => $USER->id,
+            'unreadcount' => $unreadcount,
             'urls' => [
                 'seeall' => (new moodle_url('/message/index.php'))->out(),
                 'writeamessage' => (new moodle_url('/message/index.php', ['contactsfirst' => 1]))->out(),
@@ -59,8 +61,10 @@ function message_popup_render_navbar_output(\renderer_base $renderer) {
     // Add the notifications popover.
     $enabled = \core_message\api::is_processor_enabled("popup");
     if ($enabled) {
+        $unreadcount = \message_popup\api::count_unread_popup_notifications($USER->id);
         $context = [
             'userid' => $USER->id,
+            'unreadcount' => $unreadcount,
             'urls' => [
                 'seeall' => (new moodle_url('/message/output/popup/notifications.php'))->out(),
                 'preferences' => (new moodle_url('/message/notificationpreferences.php', ['userid' => $USER->id]))->out(),
index 0355993..75b363d 100644 (file)
@@ -45,7 +45,7 @@
     {{$togglelabel}}{{#str}} showmessagewindownonew, message {{/str}}{{/togglelabel}}
     {{$togglecontent}}
         {{#pix}} t/message, core, {{#str}} togglemessagemenu, message {{/str}} {{/pix}}
-        <div class="count-container hidden" data-region="count-container"></div>
+        <div class="count-container {{^unreadcount}}hidden{{/unreadcount}}" data-region="count-container">{{unreadcount}}</div>
     {{/togglecontent}}
 
     {{$containerlabel}}{{#str}} notificationwindow, message {{/str}}{{/containerlabel}}
index 97efbff..602d106 100644 (file)
@@ -45,7 +45,7 @@
     {{$togglelabel}}{{#str}} shownotificationwindownonew, message {{/str}}{{/togglelabel}}
     {{$togglecontent}}
         {{#pix}} i/notifications, core, {{#str}} togglenotificationmenu, message {{/str}} {{/pix}}
-        <div class="count-container hidden" data-region="count-container"></div>
+        <div class="count-container {{^unreadcount}}hidden{{/unreadcount}}" data-region="count-container">{{unreadcount}}</div>
     {{/togglecontent}}
 
     {{$containerlabel}}{{#str}} notificationwindow, message {{/str}}{{/containerlabel}}