<?
    
function thread_overall_rating($id)
    {
        
$vote_count mysql_fetch_array(mysql_query("SELECT COUNT(*) AS num FROM community_threads_ratings WHERE thread_id = '" $id "'"));
        if (
intval($vote_count['num']) > 0)
        {
            
$positive mysql_num_rows(mysql_query("SELECT * FROM community_threads_ratings, community_threads_categories WHERE community_threads_ratings.category_id = community_threads_categories.id AND community_threads_ratings.thread_id = '" $id "' AND community_threads_categories.positive = '1'"));
            
$negative $vote_count['num'] - $positive;
            if (
$positive >= $negative)
            {
                
$score $positive $negative;
            }
            else
            {
                
$score $negative $positive;
            }
            
$rating mysql_fetch_array(mysql_query("SELECT COUNT(*) AS num, community_threads_ratings.category_id, community_threads_categories.positive FROM community_threads_ratings, community_threads_categories WHERE community_threads_ratings.thread_id = '" $id "' AND community_threads_ratings.category_id = community_threads_categories.id GROUP BY community_threads_ratings.category_id ORDER BY num DESC, posted_on ASC LIMIT 1"));
            
$category mysql_fetch_array(mysql_query("SELECT name, worksafe, positive FROM community_threads_categories WHERE id = '" $rating['category_id'] . "'"));
            if (
$category['positive'])
            {
                return 
'<span class="notice_good">' $category['name'] . ' [' $score ']</span>';
            }
            else
            {
                return 
'<span class="notice_error">' $category['name'] . ' [-' $score ']</span>';
            }
        }
        else
        {
            return 
'Unrated [0]';
        }
    }
?>