<?php
/* 
* Theme: PREMIUMPRESS CORE FRAMEWORK FILE
* Url: www.premiumpress.com
* Author: Mark Fail
*
* THIS FILE WILL BE UPDATED WITH EVERY UPDATE
* IF YOU WANT TO MODIFY THIS FILE, CREATE A CHILD THEME
*
* http://codex.wordpress.org/Child_Themes
*/
if (!defined('THEME_VERSION')) {	header('HTTP/1.0 403 Forbidden'); exit; }

global $CORE, $CORE_DIRECTORY, $post, $settings, $userdata;

// STAR RATING
$starrating = get_post_meta($post->ID, 'starrating', true);
if(!is_numeric($starrating)){ $starrating = 0; }else{ $starrating = number_format($starrating,1); }

$starreviews = get_post_meta($post->ID, 'starrating_votes', true);
if(!is_numeric($starreviews)){ $starreviews = 1; }


$srating = array(0,0,0,0,0,0);
$spercent = array(0,0,0,0,0,0);
$sextras = array(0,0,0,0,0,0);
$c = get_comments( array ('fields' => 'ids', 'post_id' => $post->ID ) );
if(!empty($c)){
	foreach($c as $commentid){
		// GET THE RATING VALUE FOR EACH COMMENT
		$crating = get_comment_meta( $commentid, 'ratingtotal', true );	
		if($crating == 5){
		$b=5;
		}elseif($crating > 3.9 && $crating < 5){
		$b=4;
		}elseif($crating > 2.9 && $crating < 4){
		$b=3;
		}elseif($crating > 1.9 && $crating < 3){
		$b=2;
		}elseif($crating < 2){
		$b=1;
		}
		$srating[$b] = $srating[$b]+1; 
		$spercent[$b] = $spercent[$b] + (1/$starreviews*100);
		
		// GET THE RATING VALUE FOR EACH COMMENT
		$i=1; while($i < 5){
			$crating = get_comment_meta( $commentid, 'rating'.$i, true );	
			//echo $crating."< (".$i.")--<br>";
			if(!is_numeric($crating)){ $crating = 5; }		
			$sextras[$i] = $sextras[$i] + $crating;
			$i++;
		} 
		
			
	}
}
// CLEAN UP FRACTIONS
$i=1; while($i < 5){
$sextras[$i] = $sextras[$i]/$starreviews;
$i++;
}
 
?>
<div class="widget userrating" id="widget-rating" data-title="<?php echo __("User Rating","premiumpress") ?>">
   <?php 
   
   // WIDGET OPTION
   // SHOW TITLE
   if(!isset($settings['showtitle']) || ( isset($settings['showtitle']) && $settings['showtitle'] != 0) ){ ?>
   <div class="widget-title">
   <i class="far fa-star float-right mt-1"></i>
   <h6><?php echo __("User Rating","premiumpress") ?></h6>
   </div>
   <?php } ?>
   <div class="">
   <?php if($starrating > 0){ ?>
      <strong class="h4"><?php echo $starrating; ?>  <?php echo __("Overall","premiumpress") ?></strong> <em>(<?php echo  sprintf( _n( '%s review', '%s reviews', $starreviews, 'premiumpress' ), $starreviews ); ?>) </em>
   <?php }else{ ?>
   <strong class="h6"><?php echo __("No reviews yet!","premiumpress") ?></strong>
   <div class="small mt-2"><?php echo __("There are no reviews for this business, be first to write a review!","premiumpress") ?></div>
   <?php } ?>
   </div>
   <?php if($starrating > 0){ ?>
   <div class="container mt-3">
      <?php $i=1; $t=5; while($i < 6){ ?>
      <div class="row my-2 py-1 border-bottom">
         <div class="col-2 px-0">
            <?php echo $t; ?> <i class="fa fa-star <?php if($spercent[$t] == 0){ ?>text-muted<?php }else{ ?>text-warning<?php } ?>" aria-hidden="true"></i>
         </div>
         <div class="col-8">
            <div class="progress rounded-0" style="height:12px; margin-top:6px;">
               <div class="progress-bar bg-success" role="progressbar" style="width: <?php echo $spercent[$t]; ?>%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
            </div>
         </div>
         <div class="col-2 px-0">
            <?php echo $srating[$t]; ?>
         </div>
      </div>
      <?php $i++; $t--; } ?>
   </div>
   <div class="row">
      <div class="col-6 mb-1 pr-0">
         <label class="small mb-0 pb-0"><?php echo __("Location","premiumpress") ?></label>
         <div class="clearfix"></div>
         <div class="rating-item">
            <input type="hidden" class="rating" data-filled="fa fa-star rating-rated" data-empty="far fa-star" data-fractions="2" value="<?php echo $sextras[1]; ?>" style="cursor:pointer;" disabled/>
         </div>
      </div>
      <div class="col-6 mb-1 pr-0">
         <label class="small mb-0 pb-0"><?php echo __("Service","premiumpress") ?></label>
         <div class="clearfix"></div>
         <div class="rating-item">
            <input type="hidden" class="rating" data-filled="fa fa-star rating-rated" data-empty="far fa-star" data-fractions="2" value="<?php echo $sextras[2]; ?>" style="cursor:pointer;" disabled/>
         </div>
      </div>
      <div class="col-6 pr-0">
         <label class="small mb-0 pb-0"><?php echo __("Staff","premiumpress") ?></label>
         <div class="clearfix"></div>
         <div class="rating-item">
            <input type="hidden" class="rating" data-filled="fa fa-star rating-rated" data-empty="far fa-star" data-fractions="2" value="<?php echo $sextras[3]; ?>" style="cursor:pointer;" disabled/>
         </div>
      </div>
      <div class="col-6 pr-0">
         <label class="small mb-0 pb-0"><?php echo __("Value","premiumpress") ?></label>
         <div class="clearfix"></div>
         <div class="rating-item">
            <input type="hidden" class="rating" data-filled="fa fa-star rating-rated" data-empty="far fa-star" data-fractions="2" value="<?php echo $sextras[4]; ?>" style="cursor:pointer;" disabled/>
         </div>
      </div>
   </div>
   <?php } ?>
   <a href="<?php if($userdata->ID){ ?>javascript:void(0);<?php }else{ echo wp_login_url(); ?>?redirect=<?php echo get_permalink($post->ID); ?><?php } ?>" class="btn btn-block btn-primary rounded-0 mt-3 <?php if($userdata->ID){ ?>btn-ajax-rating<?php } ?>"><?php echo __("Write Review","premiumpress") ?></a>
</div>