<?php global $gp;

/*--------------------------------------------------------------
Load User Rating Script
--------------------------------------------------------------*/

if ( ! function_exists( 'gp_register_user_rating_script' ) ) {

	function gp_register_user_rating_script() {
	
		global $gp, $post;
	
		if ( ( $gp['review_can_users_rate'] == 'enabled' && ! is_user_logged_in() ) OR is_user_logged_in() ) {
			wp_enqueue_script( 'gp-user-ratings', gp_scripts_uri . 'user-ratings.js', array( 'jquery' ), '', true );						
		}
		
		gp_get_hub_id( get_the_ID() );

		wp_localize_script( 'gp-user-ratings', 'gp_rating', array(
			'post_id' => $GLOBALS['gp_hub_id'],
			'ajaxurl' => admin_url( 'admin-ajax.php' ),
			'rating_number' => $gp['review_rating_number'],
			'rating_width' => ( 24 * $gp['review_rating_number'] ),
			'rated' => get_user_meta( get_current_user_id(), '_gp_rated_' . $GLOBALS['gp_hub_id'], true ),
			'position' => get_user_meta( get_current_user_id(), '_gp_current_position_' . $GLOBALS['gp_hub_id'], true ),
			'rating' => get_user_meta( get_current_user_id(), '_gp_user_rating_' . $GLOBALS['gp_hub_id'], true )
		) );
	}
	
}
add_action( 'wp_enqueue_scripts', 'gp_register_user_rating_script' );


/*--------------------------------------------------------------
User Rating Script
--------------------------------------------------------------*/
			
if ( ! class_exists( 'gp_user_rating' ) ) {

    class gp_user_rating {
    
        public $user_rating;
        public $current_position;
        public $user_votes;

        function __construct() {

			if ( is_page() ) {
				$this->retrieve_values();
			}
			
			add_action('wp_ajax_gp_rating', array(&$this, 'sync_rating'));
			add_action('wp_ajax_nopriv_gp_rating', array(&$this, 'sync_rating'));

        }
		
        private function retrieve_values() {
        
			global $gp;
        
            $user_rating = get_post_meta(get_the_ID(), '_gp_user_rating', true);
            if (!$user_rating) {
                $user_rating = '0';
            }
            $this->user_rating = $user_rating;

            $current_position = get_post_meta(get_the_ID(), '_gp_current_position', true);
            if (!$current_position) {
                $current_position = 0;
            }
            $this->current_position = ( 24 * $gp['review_rating_number'] );

            $user_votes = get_post_meta(get_the_ID(), '_gp_votes', true);
            if (!$user_votes) {
                $user_votes = 0;
            }
            $this->user_votes = $user_votes;

        }

        public function sync_rating() {
        
        	global $gp;
        	
            // Sync scores
            $position = (int)$_POST['rating_position'];
            $post_id = (int)$_POST['post_id'];
            $new_score = $_POST['rating_score'];

            // Current scores
            $current_position = (int)get_post_meta($post_id, '_gp_current_position', true);
            if (!$current_position) {
                $current_position = 0;
            }
            $user_rating = (int)get_post_meta($post_id, '_gp_user_rating', true);
            if (!$user_rating) {
                $user_rating = 0;
            }
            $user_votes = (int)get_post_meta($post_id, '_gp_user_votes', true);
            if (!$user_votes) {
                $user_votes = 0;
            }

            // New score
            $new_position = ( $current_position * $user_votes + $position ) / ( $user_votes + 1 ) ;
            $new_votes = $user_votes + 1;
            $new_rating = floor( ( ( ( ( $new_position / ( 24 * $gp['review_rating_number'] ) * 100 ) * $gp['review_rating_number'] ) / 100 ) * 10 ) ) / 10;

            // Update scores
            update_post_meta( $post_id, '_gp_current_position', $new_position, get_post_meta( $post_id, '_gp_current_position', true ) );
            update_post_meta( $post_id, '_gp_user_rating', $new_rating, get_post_meta( $post_id, '_gp_user_rating', true ) );
            update_post_meta( $post_id, '_gp_user_votes', $new_votes, get_post_meta( $post_id, '_gp_user_votes', true ) );
            if ( is_user_logged_in() ) {
				add_user_meta( get_current_user_id(), '_gp_rated_' . $post_id, 'rated' );
				add_user_meta( get_current_user_id(), '_gp_user_rating_' . $post_id, $new_score );
				add_user_meta( get_current_user_id(), '_gp_current_position_' . $post_id, $position );
			}            
            exit;
            
        }
    }
}

new gp_user_rating();