<?php
defined('_CONSTANT_') or die('Error. You don`t have permision to access.');
class Count 
{ 
    /*
     * Количество пользователей;
     */
    public static function userCount() 
    { 
        global $Filter;
        $count = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS.""); 
        return $Filter->clearInt($count);
    }
    
    /*
     * Количество пользователей онлайн;
     * $setLimit - Сколько времени после бездействия отображается в онлайне;
     */ 
    public static function userCountOnline() 
    {
        global $Filter;
        $setLimit = 600; // 10 мин.
        $count = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS." WHERE `last_time` > ?", [(time() - $setLimit)]);
        return $Filter->clearInt($count);
    }
    
    /*
     * Количество банов;
     */ 
    public static function userCountBan($userId) 
    {
        global $Filter;
        $count = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".BAN." WHERE `user_id` = ?", [$userId]);
        
        return $Filter->clearInt($count);
    }

    # Count chat msg;
    public static function chatCountMsg() 
    {
        global $Filter;
        $count = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". CHAT ."");

        return $Filter->clearInt($count);
    }
    
    # Chat new msg;
    public static function chatCountMsgNew() 
    {
        global $Filter;
        
        $count = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". CHAT ." WHERE `time` > ?", [(time() - 600)]);
        
        return $count;
    }
    
    # Count user in chat;
    public static function chatCountUsers() 
    {
        global $Filter;
        $count = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". USERS ." WHERE `where_link` = ? && `last_time` > ? or `where_link` = ? && `last_time` > ?", ['/modules/chat/index.php', (time() - 600), '/modules/chat/', (time() - 600)]);
        
        return $Filter->clearInt($count);
    }
    
    # Clan rating;
    public static function clanCountRating($clanId) 
    {
        global $Filter;
        
        $clanId = $Filter->clearInt($clanId);
        
        $clan = DB::$dbs->queryFetch("SELECT `id` FROM ". CLANS ." WHERE `id` = ?", [$clanId]);
        
        if (empty($clan)) 
        {
            return 'Клан не найден';
        } 
        else 
        {
            $count = DB::$dbs->querySingle("SELECT SUM(`clan_rating`) FROM ". USERS ." WHERE `clan_id` = ?", [$clanId]);
            return $Filter->clearInt($count);
        }
    }
}
