Browse Source

Rename MessageManager::get_new_messages to getCountNewMessages + improve query

jmontoyaa 8 years ago
parent
commit
6b08719b92
3 changed files with 8 additions and 7 deletions
  1. 6 5
      main/inc/lib/message.lib.php
  2. 1 1
      main/inc/lib/template.lib.php
  3. 1 1
      main/inc/lib/userportal.lib.php

+ 6 - 5
main/inc/lib/message.lib.php

@@ -15,23 +15,24 @@ use Chamilo\UserBundle\Entity\User;
 class MessageManager
 {
     /**
-     * Get the new messages for the current user from the database.
+     * Get count new messages for the current user from the database.
      * @return int
      */
-    public static function get_new_messages()
+    public static function getCountNewMessages()
     {
         $table = Database::get_main_table(TABLE_MESSAGE);
         if (!api_get_user_id()) {
             return false;
         }
-        $sql = "SELECT * FROM $table
+        $sql = "SELECT COUNT(id) as count 
+                FROM $table
                 WHERE
                     user_receiver_id=" . api_get_user_id() . " AND
                     msg_status=" . MESSAGE_STATUS_UNREAD;
         $result = Database::query($sql);
-        $i = Database::num_rows($result);
+        $row = Database::fetch_assoc($result);
 
-        return $i;
+        return $row['count'];
     }
 
     /**

+ 1 - 1
main/inc/lib/template.lib.php

@@ -473,7 +473,7 @@ class Template
                 $user_info['is_admin'] = 1;
             }
 
-            $user_info['messages_count'] = MessageManager::get_new_messages();
+            $user_info['messages_count'] = MessageManager::getCountNewMessages();
             $this->user_is_logged_in = true;
         }
         // Setting the $_u array that could be use in any template

+ 1 - 1
main/inc/lib/userportal.lib.php

@@ -912,7 +912,7 @@ class IndexManager
         //  @todo Add a platform setting to add the user image.
         if (api_get_setting('allow_message_tool') == 'true') {
             // New messages.
-            $number_of_new_messages = MessageManager::get_new_messages();
+            $number_of_new_messages = MessageManager::getCountNewMessages();
             // New contact invitations.
             $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(api_get_user_id());