/**
 * Review manager update rating for a post.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 *
 * @param int $post_id The post ID.
 * @param array $ratings The rating information.
 * @param float $overall overall rating.
 */
function geodir_reviewrating_update_postrating($post_id = 0, $ratings, $overall)
{
    geodir_reviewrating_update_postrating_all($post_id);
    return;
    // DISABLED FOR NOW, WE WILL JUST CALL AN OVERAL UPDATE FUNCTION ON COMMENT SAVE. geodir_reviewrating_update_postrating_all
    global $wpdb, $plugin_prefix;
    $post = get_post($post_id);
    $post_ratings = array();
    $post_ratings = geodir_reviewrating_get_post_rating($post_id);
    //print_r($post_ratings);exit;
    $old_ratings = $post_ratings;
    $new_ratings = array();
    //print_r($ratings);exit;
    if (!empty($ratings)) {
        $r_count = count($ratings);
        foreach ($ratings as $rating_id => $rating_value) {
            $rating_info = geodir_reviewrating_rating_categories($rating_id);
            if (!empty($post_ratings) && array_key_exists($rating_id, $old_ratings)) {
                $new_rating_value = (double) $old_ratings[$rating_id]['r'] + (double) $rating_value;
                $new_ratings[$rating_id]['c'] = $new_rating_value;
                $new_ratings[$rating_id]['r'] = (double) $old_ratings[$rating_id]['c'] + 1;
            } else {
                $new_ratings[$rating_id]['c'] = (double) $r_count;
                $new_ratings[$rating_id]['r'] = (double) $rating_value;
            }
        }
    }
    //update rating
    geodir_update_postrating($post_id, $post->post_type);
}
/**
 * Adds overall rating and sorting options to the comment list.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 *
 * @global object $post The current post object.
 * @global string $geodir_post_type The post type.
 *
 * @return bool|void
 */
function geodir_reviewrating_show_post_ratings()
{
    global $post, $geodir_post_type;
    $all_postypes = geodir_get_posttypes();
    if (!in_array($geodir_post_type, $all_postypes)) {
        return false;
    }
    if (isset($_REQUEST['comment_sorting'])) {
        ?>
	
			<script type="text/javascript">
				
				jQuery(document).ready(function(){
					
					jQuery('#gd-tabs dl dd').removeClass('geodir-tab-active');
					
					jQuery('#gd-tabs dl dd').find('a').each(function(){
					
						if(jQuery(this).attr('data-tab') == '#reviews')
							jQuery(this).closest('dd').addClass('geodir-tab-active');
						
					});
					
				});
				
			</script> <?php 
    }
    global $post;
    if (!isset($post->ID)) {
        return;
    }
    $post_link = get_permalink($post->id);
    $comment_shorting_form_field_val = array('latest' => __('Latest', GEODIRREVIEWRATING_TEXTDOMAIN), 'oldest' => __('Oldest', GEODIRREVIEWRATING_TEXTDOMAIN), 'low_rating' => __('Lowest Rating', GEODIRREVIEWRATING_TEXTDOMAIN), 'high_rating' => __('Highest Rating', GEODIRREVIEWRATING_TEXTDOMAIN));
    $comment_shorting_form_field_val = apply_filters('geodir_reviews_rating_comment_shorting', $comment_shorting_form_field_val);
    ?>
   <form name="comment_shorting_form" id="comment_sorting_form" method="get" action="<?php 
    echo $post_link;
    ?>
">
   <?php 
    $query_variables = $_GET;
    $hidden_vars = '';
    if (!empty($query_variables)) {
        foreach ($query_variables as $key => $val) {
            if ($key != 'comment_sorting') {
                $hidden_vars .= '<input type="hidden" name="' . $key . '" value="' . $val . '" />';
            }
        }
    }
    echo $hidden_vars;
    ?>
	
	<select name="comment_sorting" class="comment_sorting" onchange="jQuery(this).closest('#comment_sorting_form').submit()">
    
    <?php 
    /**
     * Filter the default comments sorting.
     *
     * @since 1.1.7
     * @package GeoDirectory_Review_Rating_Manager
     *
     * @param string $comment_sorting Sorting name to sort comments.
     */
    $comment_sorting = apply_filters('geodir_reviewrating_comments_shorting_default', 'latest');
    $comment_sorting = isset($_REQUEST['comment_sorting']) && !empty($_REQUEST['comment_sorting']) && isset($comment_shorting_form_field_val[$_REQUEST['comment_sorting']]) ? $_REQUEST['comment_sorting'] : $comment_sorting;
    if (isset($comment_shorting_form_field_val) && !empty($comment_shorting_form_field_val)) {
        foreach ($comment_shorting_form_field_val as $key => $value) {
            ?>
	<option <?php 
            if ($comment_sorting == $key) {
                echo 'selected="selected"';
            }
            ?>
 value="<?php 
            echo $key;
            ?>
"><?php 
            _e($value, GEODIRREVIEWRATING_TEXTDOMAIN);
            ?>
</option>
            <?php 
        }
    }
    ?>
	</select>
  </form>
    <?php 
    $ratings = array();
    $ratings = geodir_reviewrating_get_post_rating($post->ID);
    if (!$ratings['overall']) {
        $ratings = '';
    }
    if (!empty($ratings)) {
        $overall_html = geodir_reviewrating_draw_overall_rating($ratings['overall']);
        $ratings_html = geodir_reviewrating_draw_ratings($ratings);
        echo '<span class="gd-rating-overall-rating"><span class="gd-rating-overall-rating-title">' . __('Overall Rating:', GEODIRREVIEWRATING_TEXTDOMAIN) . '</span>' . $overall_html . '</span>' . $ratings_html;
    }
}