<?php
    require_once('include/functions/community_thread_overall_rating.php');

    $_id = (isset($_GET['i'])) ? trim($_GET['i']) : '';
    $_vote = (isset($_GET['v'])) ? trim($_GET['v']) : '';
        
    $community_threads = mysqli_query_logged("SELECT thread_user_id FROM community_threads WHERE thread_id = '" . $_id . "'");
    if ($community_threads_row = mysqli_fetch_array($community_threads))
    {
        if ($community_threads_row['thread_user_id'] != $GLOBALS['auth']['id'])
        {
            $community_threads_categories = mysqli_query_logged("SELECT * FROM community_threads_categories WHERE id = '" . $_vote . "'");
            if ($community_threads_categories_row = mysqli_fetch_array($community_threads_categories))
            {
                mysqli_query_logged("REPLACE INTO community_threads_ratings SET thread_id = '" . $_id . "', category_id = '" . $_vote . "', user_id = '" . $GLOBALS['auth']['id'] . "', posted_on = NOW()");
            }
            elseif ($_vote == '0')
            {
                mysqli_query_logged("DELETE FROM community_threads_ratings WHERE thread_id = '" . $_id . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'");
            }    
        }
        echo "document.getElementById('thread_ranking').innerHTML = '" . thread_overall_rating($_id) . "';";
    }
?>