<?php
/**
* @package		UserPoints for Joomla 4.x
* @copyright	Copyright (C) 2008-2016. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

class modUserPointsPointsFromStartHelper {

	public static function getPoints($params) 
	{

		$db		= SQL::getInstance();
		
		$earnedpoints = 0;
		$spentpoints  = 0;
		$enabled	  = " AND enabled='1'";
		
		$purged = $params->get('purged', '0');
		
		if ( $purged ) $enabled	= "";
		
		$user 	= UserPoints::getUser();  		
		
		if ( $user->id ) 
		{		
			$referreid = $db->quote(UserPoints::getAnyUserReferreID( $user->id ));
			
			$query = "SELECT SUM(points) FROM #__alpha_userpoints_details " .
					 "WHERE points>'0' AND referreid=$referreid AND approved='1' AND `status`='1' $enabled";
			$db->setQuery( $query );
			$earnedpoints = $db->loadResult();			
			
			$query = "SELECT SUM(points) FROM #__alpha_userpoints_details " .
					 "WHERE points<'0' AND referreid=$referreid AND approved='1' AND `status`='1' $enabled";
			$db->setQuery( $query );
			$spentpoints = $db->loadResult();			
			
		}
	
		return array($earnedpoints, $spentpoints);	
	}
}
