<?php
/**************************************************************************************************
| Ionicware
| http://www.ionicware.com
| support@ionicware.com
|
|**************************************************************************************************
|
| Copyright (c) 2011 ionicware.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");

/**
 * Request the users username
 */
$uname 			= cleanit($_REQUEST['uname']);
$statusOfRating = cleanit($_REQUEST['status']);

if ($statusOfRating == 'buyer')
{
	$reviewText = $lang[3564];
	$linkText	= $lang[2842];
	$recommendText	= $lang[3571];
	$link 		= 'seller';
	$fromSql	= 'ratings_buyer';
	$selctSql	= 'BRID';
}
else
{
	$reviewText = $lang[2842];
	$linkText	= $lang[3564];
	$recommendText	= $lang[3563];
	$link 		= 'buyer';
	$fromSql	= 'ratings';
	$selctSql	= 'RID';
}

/**
 * if no user name is provided just kill the page
 */
if ($uname == "" || $statusOfRating == "")
{
	header("Location:$config[baseurl]/");exit;
	die();
}

/**
 * Grab all the users information
 */
$query = "SELECT *
FROM members
WHERE username = ?
AND status='1'";

/**
 * Setup our prepared value.
 */
$preparedVars[]	= escape($uname);

/**
 * Execute
 */
$executequery			= $conn->CacheExecute(500, $query, $preparedVars);

$userData				= $executequery->fields;
$uid 					= $userData['USERID'];

/*
 * Unknown or inactive member: a real 404, not an empty ratings page at 200.
 * 2026-09-23
 */
if (empty($uid))
{
	include '404.php';
	die();
}
$profileData 			= $userData['profile_data'];
$userData['userlevel'] 	= getUserLevelFromLevel($userData['userlevel']);
$userData['joinDate'] 	= $userData['addtime'];

/**
 * Code replaced with the background function.
 *
 * 2023-05-02 09:39:12 JD
 */
$userData['profileBackground']	= getUserBackground($userData['USERID'], '', false, $userData);

/*
* Get the total amount of recommends
* which will be used by the AJAX
* more button
* 2/5/2019
*/
$sql 	 		= "SELECT COUNT({$selctSql}) as total,
						SUM(bad) as badTotal,
						SUM(good) as goodTotal
					FROM {$fromSql}
					WHERE USERID = ?
					AND RATER != 0
					AND NOT (good = 0
					AND bad = 0)";

/**
 * Setup our prepared value.
 */
$preparedVari[]	= escape($uid);
$executequery	= $conn->CacheExecute(500, $sql, $preparedVari);

/*
 * A failed statement returns false, and ->getrows() on it is a fatal error
 * (seen in production on 7.4). No rows reads as no ratings below.
 * 2026-10-22
 */
$results 		= ($executequery === false) ? array() : $executequery->getrows();

$totalRatings	= isset($results[0]['total']) 		? $results[0]['total'] 		: 0;
$goodRatings	= isset($results[0]['goodTotal']) 	? $results[0]['goodTotal'] 	: 0;
$badRatings		= isset($results[0]['badTotal']) 	? $results[0]['badTotal'] 	: 0;

/*
 * No ratings: avoid dividing by zero (a warning on 7.4, an error on 8).
 * 2026-09-23
 */
$ratingDivisor	= max(1, intval($totalRatings));

/**
 * rating status array values for rating status component
 */
$ratingStatus['percent'] 		 		= (($goodRatings / $ratingDivisor) * 100);
$ratingStatus['percent'] 		 		= number_format($ratingStatus['percent'], 1);
$ratingStatus['positiveRatings'] 		= $goodRatings;
$ratingStatus['negitiveRatings'] 		= $badRatings;
$ratingStatus['numberOfRatings'] 		= $totalRatings;
$ratingStatus['positiveRatingsPercent'] = $goodRatings / $ratingDivisor * 100;
$ratingStatus['negitiveRatingsPercent'] = $badRatings / $ratingDivisor * 100;
$ratingStatus['ratingPercent'] 			= $ratingStatus['percent'] / 20;
$ratingStatus['ratingPercent']			= number_format($ratingStatus['ratingPercent'], 1);
$ratingStatus['recommendText']			= $recommendText;

/*
* Get the user skills and explode them.
* 1/12/2013
*/
if (!empty($userData['skills']))
{
	/*
	* Format our tags
	*/
	$skills		= FormatTags($userData['skills']);
	$skills 	= str_ireplace(" ", ",", $skills);
	$skills 	= explode(",", $skills);
}

/*
* Is the User online?
* 12/28/2012
*/
if (IsUserOnline($uid, $userData['lastlogin'])) {
	$userData['online'] = 1;
}
else
{
	$userData['online'] = 0;
}

/*
* Get achevements
* 12/20/2018
*/
if ($uid == $__loggedUserDetails['USERID'])
{
	$achievements = GetAchivements($countAch=0);
}

/*
* If the user is logged in, lets get some
* data about them.
* 8/17/2013
*/
if ($USERID != $__loggedUserDetails['USERID'] && intval($__loggedUserDetails['USERID']) > 0)
{
	$userRecommendation	= GetRecommendationCountUser($USERID);
	$sellerBuyerOrders	= GetSellerBuyerOrders($USERID);
}

/*
* Generate a JWT token for the socket server.
*
* Prefaced with __ so as not to conflict elsewhere.
*
* 10/18/2018
*/
$__socketParams = array(
	"type" => "ratings",
	"acc" => array(),
	"ouid" => [intval($userData['USERID'])]
);

/**
 * Get the time of the last delivery for the user.
 * 12/6/2018
 */
$lastDelivery = getLastDelivery($userData['USERID']);

if ($lastDelivery != false)
	$userData['lastDelivery'] = GetTimeAgo($lastDelivery);
else
	$userData['lastDelivery'] = false;

$config['sitetitle'] = $reviewText . " - " . $uname;

/*
 * A ratings page with no ratings is an empty page to Google (soft 404).
 * 2026-09-23
 */
if (intval($totalRatings) == 0) $config['noindex'] = 1;

include 'views/header/view_header-loader.php';
include 'views/profile/view_profile-rating.php';
include 'views/footer/view_footer-loader.php';
