ConversationModel::countUnread PHP Method

countUnread() public method

Count unread messages.
public countUnread ( integer $UserID, boolean $Save = true ) : integer
$UserID integer Unique ID for user being queried.
$Save boolean Whether to update user record.
return integer
    public function countUnread($UserID, $Save = true)
    {
        // Also update the unread conversation count for this user
        $CountUnread = $this->SQL->select('c.ConversationID', 'count', 'CountUnread')->from('UserConversation uc')->join('Conversation c', 'c.ConversationID = uc.ConversationID and uc.CountReadMessages < c.CountMessages')->where('uc.UserID', $UserID)->where('uc.Deleted', 0)->get()->value('CountUnread', 0);
        if ($Save) {
            Gdn::userModel()->setField($UserID, 'CountUnreadConversations', $CountUnread);
        }
        return $CountUnread;
    }

Usage Example

 protected function AddIgnore($ForUserID, $IgnoreUserID)
 {
     $this->SetUserMeta($ForUserID, "Blocked.User.{$IgnoreUserID}", date('Y-m-d H:i:s'));
     // Since the Conversation application can be turned off, check first if the ConversationModel is present.
     if (class_exists('ConversationModel')) {
         // Remove from conversations
         $Conversations = $this->IgnoreConversations($IgnoreUserID, $ForUserID);
         Gdn::SQL()->Delete('UserConversation', array('UserID' => $ForUserID, 'ConversationID' => $Conversations));
         $conversationModel = new ConversationModel();
         $conversationModel->countUnread($ForUserID, true);
     }
 }