// BASED ON XenForo_Model_Forum::getUnreadThreadCountInForum
// THREADS ARE THE ONLY THING TO COUNT FOR THE DEFAULT NODE TYPES
// ADDONS THAT ADD NEW NODE TYPES CAN EXTEND THIS FUNCTION, COUNT THEIR OWN UNREAD CONTENT, AND ADD IT TO THE UNREAD COUNT THAT IS RETURNED
public function getUnreadCount($nodeIds, $userId, $ignored = false)
{
if (!$userId)
{
return false;
}
if ($ignored && is_string($ignored))
{
$ignored = unserialize($ignored);
$ignored = array_keys($ignored);
}
$db = $this->_getDb();
$visitor = XenForo_Visitor::getInstance();
$threadModel = XenForo_Model::create('XenForo_Model_Thread');
$autoReadDate = XenForo_Application::$time - (XenForo_Application::get('options')->readMarkingDataLifetime * 86400);
$unreadThreads = $db->fetchAll('
SELECT thread.*, permission.cache_value AS node_permission_cache
FROM xf_thread AS thread
LEFT JOIN xf_permission_cache_content AS permission
ON (permission.permission_combination_id = ?
AND permission.content_type = \'node\'
AND permission.content_id = thread.node_id)
LEFT JOIN xf_thread_read AS thread_read ON
(thread_read.thread_id = thread.thread_id AND thread_read.user_id = ?)
LEFT JOIN xf_forum_read AS forum_read ON
(forum_read.node_id = thread.node_id AND forum_read.user_id = ?)
WHERE thread.node_id IN (' . $db->quote($nodeIds) . ')
AND (thread.last_post_date > ' . strval($autoReadDate) . ' AND (forum_read.forum_read_date IS NULL OR thread.last_post_date > forum_read.forum_read_date))
AND (thread_read.thread_id IS NULL OR thread.last_post_date > thread_read.thread_read_date)
' . ($ignored ? 'AND thread.user_id NOT IN (' . $db->quote($ignored) . ')' : '') . '
AND thread.discussion_state = \'visible\'
AND thread.discussion_type <> \'redirect\'
AND thread.node_id NOT IN (7)
', array($visitor['permission_combination_id'], $userId, $userId));
$count = 0;
foreach ($unreadThreads AS $thread)
{
$visitor->setNodePermissions($thread['node_id'], $thread['node_permission_cache']);
if ($threadModel->canViewThreadAndContainer($thread, array('node_id' => $thread['node_id'])))
{
$count++;
}
}
return $count;
}