<?php global $gp, $ratings;

gp_get_hub_id( get_the_ID() );

// Get average user rating
if ( get_post_meta( $GLOBALS['gp_hub_id'], '_gp_user_rating', true ) ) { 
	$user_rating = number_format( get_post_meta( $GLOBALS['gp_hub_id'], '_gp_user_rating', true ), 1 ) + 0;
} else {
	$user_rating = 0;
}

// Get total votes
$user_votes = get_post_meta( $GLOBALS['gp_hub_id'], '_gp_user_votes', true );
if ( ! $user_votes ) {
	$user_votes = 0;
}
if ( ! $user_votes ) {
	$user_votes_text = __( ' votes', 'gp_lang');
} elseif ( $user_votes == 1 ) {
	$user_votes_text = __( ' vote', 'gp_lang');
} else {
	$user_votes_text = __( ' votes', 'gp_lang');	
}

// Get write a review page template URL
if ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'hub-template.php' ) {
	$gp_pages = get_pages( 'child_of=' . $GLOBALS['gp_hub_id'] );	
	foreach ( $gp_pages as $gp_page ) {	
		if ( get_post_meta( $gp_page->ID, '_wp_page_template', true ) == 'write-a-review-template.php' ) {
			$write_a_review_link = get_permalink( $gp_page->ID );
			break;
		} else {
			$write_a_review_link = '';
		}
	}
}

// Get user reviews page template URL
if ( get_post_meta( $GLOBALS['gp_hub_id'], '_wp_page_template', true ) == 'hub-template.php' ) {
	$gp_pages = get_pages( 'child_of=' . $GLOBALS['gp_hub_id'] );	
	foreach ( $gp_pages as $gp_page ) {	
		if ( get_post_meta( $gp_page->ID, '_wp_page_template', true ) == 'user-reviews-template.php' ) {
			$user_reviews_link = get_permalink( $gp_page->ID );
			break;
		} else {
			$user_reviews_link = '';
		}
	}
}
			
// Count number of user reviews
$args = array(
	'post_status' => 'publish',
	'post_type'       => 'gp_user_review',
	'meta_query' => array(
		array(
		   'key' => '_hub_page_id',
		   'value' => $GLOBALS['gp_post_id'],
		   'compare' => '=',
		),
	),
	'posts_per_page' => -1,
);
$gp_query = new wp_query( $args ); 
					
?>

<div id="user-rating-wrapper"<?php echo wp_kses_post( $GLOBALS['gp_title_bg_css'] ); ?>>

	<div class="bg-overlay-dark"></div>

	<div class="foreground-overlay">
	
		<div class="average-rating-wrapper">
		
			<div class="average-rating-text">
				<?php _e( 'Average User Rating', 'gp_lang' ); ?>
				<?php if ( ! empty( $write_a_review_link ) ) { ?><a href="<?php echo esc_url( $write_a_review_link ); ?>" class="button write-a-review-button"><?php _e('Write A Review', 'gp_lang' ); ?></a><?php } ?>
				<?php if ( ! empty( $user_reviews_link ) ) { ?><a href="<?php echo esc_url( $user_reviews_link ); ?>" class="user-reviews-link"><?php echo absint( $gp_query->post_count ); ?> <?php _e( 'User Reviews', 'gp_lang' ); ?></a><?php } ?>
			</div>
			
			<div class="average-rating-data">
				<div class="average-rating">
					<?php echo floatval( $user_rating ); ?>
				</div>	
				<div class="total-votes current-rating">
					<span class="count"><?php echo absint( $user_votes ); ?></span> <?php echo esc_attr( $user_votes_text ); ?>
				</div>
			</div>

		</div>			

		<?php if ( ( $gp['review_can_users_rate'] == 'enabled' && ! is_user_logged_in() ) OR is_user_logged_in() ) { ?>
		
			<div class="rating-criteria-wrapper">		
				<div class="rate-text"><?php _e( 'Rate', 'gp_lang' ); ?></div>	
				<div class="rating-slider-wrapper" style="width: <?php echo absint( 24 * $gp['review_rating_number'] ); ?>px;">
					<div class="rating-unrated">
						<div class="rating-selection"></div>
					</div>
				</div>
			</div>
			
			<div class="submit-rating button"><?php _e( 'Submit', 'gp_lang' ); ?></div>
		
		<?php } else { ?>

			 <div class="sign-in-to-rate"><?php _e( 'Please <a href="#login">sign in</a> to rate.', 'gp_lang' ); ?></div>

		<?php } ?>
			
		<div class="your-rating-wrapper">
			<div class="your-rating-text">
				<?php _e( 'Your Rating', 'gp_lang' ); ?>
			</div>	

			<div class="your-rating-score current-rating">	
				<div class="score">0</div>
			</div>
						
		</div>

	</div>
	
</div><!-- END USER RATING -->