<?php
/**
* @package BILLETS - A Joomla Support Ticket System Component
* @file mod_billets_feedbackstats.php
* @author Dioscouri Design, info@dioscouri.com
* @copyright Copyright (C) 2008 Dioscouri Design. All rights reserved.
* @license Released under the terms of the GNU General Public License (see LICENSE.php in the Joomla! root directory)
* @installinfo
	<name>Billets Feedback Stats</name>
    <author>Dioscouri.com</author>
    <authorEmail>info@dioscouri.com</authorEmail>
    <authorUrl>http://www.dioscouri.com</authorUrl>
    <version>0.1</version>
    <creationDate>2008-03-14</creationDate>
    <description>Displays Feeback Stats</description>
**/

/** ensure this file is being included by a parent file */
defined('_JEXEC') or die('Restricted access');
	global $mainframe;

/**
* mod_billets_feedbackstats
*/
// ************************************************************************
function mod_billets_feedbackstats () {
	global $mainframe;

	$database = &JFactory::getDBO();
	$today = JHTML::_('date', date("Y-m-d"), "%Y-%m-%d");
	$thismonth = JHTML::_('date', date("Y-m-d"), "%Y-%m-01");

	$return = array();
	
	$query = " SELECT DATE_SUB('".$thismonth."', INTERVAL 1 MONTH) ";
	$database->setQuery( $query );
	$lastmonth = $database->loadResult();
	
	$status_query = " AND ( t.status = '2' ) AND ( t.firstresponse_by > '0' ) ";
	
	// closed tix	  
	$query = "SELECT t.* FROM #__billets_tickets as t "
		. " WHERE 1 "
		. " AND ( t.status = '2' ) "
		;	
	$database->setQuery( $query );
	$return["closedbillets_all"] = $database->loadObjectList();

	// closed tix	  
	$query = "SELECT t.* FROM #__billets_tickets as t "
		. " WHERE 1 "
		. $status_query
		;	
	$database->setQuery( $query );
	$return["closedbillets"] = $database->loadObjectList();
	
	// their feedback
	$feedback_sum = "";
	if ($return["closedbillets"]) { foreach ($return["closedbillets"] as $r) {
		$feedback_sum = $feedback_sum + $r->feedback_rating;
	} 
		$return["avgfeedback_overall"] = $feedback_sum/count($return["closedbillets"]);
	}
	
	
	// closed tix this month
	$query = "SELECT t.* FROM #__billets_tickets as t "
		. " WHERE 1 "
		. $status_query
		. " AND t.closed_datetime >= '".$database->getEscaped( trim( strtolower( $thismonth ) ) )."' "
		. " AND t.closed_datetime <= NOW() "
		;	
	$database->setQuery( $query );
	$return["closedbillets_thismonth"] = $database->loadObjectList();
	
	// their feedback
	$feedback_sum_thismonth = "";
	if ($return["closedbillets_thismonth"]) { foreach ($return["closedbillets_thismonth"] as $r) {
		$feedback_sum_thismonth = $feedback_sum_thismonth + $r->feedback_rating;
		} 
		$return["avgfeedback_thismonth"] = $feedback_sum_thismonth/count($return["closedbillets_thismonth"]);
	}
	

	// closed tix last month
	$query = "SELECT t.* FROM #__billets_tickets as t "
		. " WHERE 1 "
		. $status_query
		. " AND t.closed_datetime >= '".$database->getEscaped( trim( strtolower( $lastmonth ) ) )."' "
		. " AND t.closed_datetime < '".$database->getEscaped( trim( strtolower( $thismonth ) ) )."' "
		;	
	$database->setQuery( $query );
	$return["closedbillets_lastmonth"] = $database->loadObjectList();
	
	// their feedback
	$feedback_sum_lastmonth = "";
	if ($return["closedbillets_lastmonth"]) { foreach ($return["closedbillets_lastmonth"] as $r) {
		$feedback_sum_lastmonth = $feedback_sum_lastmonth + $r->feedback_rating;
		} 
		$return["avgfeedback_lastmonth"] = $feedback_sum_lastmonth/count($return["closedbillets_lastmonth"]);
	}
	
	return $return;
} // end mod_billets_feedbackstats
// ************************************************************************

/**
* display the module
*/
// ************************************************************************

	$stats = mod_billets_feedbackstats();
		?>
		<div class='noteg'>
			<strong><?php echo _BILLETS_FEEDBACKSTATISTICS; ?></strong>
			 
			<table class='invisible'>
            
			  <tr>
				<td>
					<?php
						$link = $mainframe->getCfg( 'live_site' )."/administrator/index.php?option=com_billets&controller=tickets&task=list&status=closed";
						$num = count($stats["closedbillets_all"]);
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".$num." "._BILLETS_CLOSEDTICKETS."</a>"; 
					?>
				</td>
			  </tr>
              
			  <tr>
				<td>
					<?php
						$link = $mainframe->getCfg( 'live_site' )."/administrator/index.php?option=com_billets&controller=tickets&task=list&status=closed";
						$num = count($stats["closedbillets"]);
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>".$num." "._BILLETS_CLOSEDTICKETSWITHRESPONSE."</a>"; 
					?>
				</td>
			  </tr>
              
			  <tr>
				<td>
					<?php
						$link = $mainframe->getCfg( 'live_site' )."/administrator/index.php?option=com_billets&controller=tickets&task=list&status=closed";
						$num = number_format($stats["avgfeedback_overall"], '1', '.', ',');
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>"._BILLETS_AVGFEEDBACK.": ".$num."</a><br>"; 
						echo "<center>";
						echo "<a href='".$link."'>".billetsFeedbackRatingImage ( $num )."</a>";
						echo "</center>";						
					?>
				</td>
			  </tr>
                           
			  <tr>
				<td>
					<?php
						$link = $mainframe->getCfg( 'live_site' )."/administrator/index.php?option=com_billets&controller=tickets&task=list&status=closed";
						$num = number_format($stats["avgfeedback_lastmonth"], '1', '.', ',');
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>"._BILLETS_AVGFEEDBACK_LASTMONTH.": ".$num." (".count($stats["closedbillets_lastmonth"]).")</a><br>"; 
						echo "<center>";
						echo "<a href='".$link."'>".billetsFeedbackRatingImage ( $num )."</a>";
						echo "</center>";						
					?>
				</td>
			  </tr>
              
			  <tr>
				<td>
					<?php
						$link = $mainframe->getCfg( 'live_site' )."/administrator/index.php?option=com_billets&controller=tickets&task=list&status=closed";
						$num = number_format($stats["avgfeedback_thismonth"], '1', '.', ',');
					?>
				</td>
				<td>
					<?php 
						echo "<a href='".$link."'>"._BILLETS_AVGFEEDBACK_THISMONTH.": ".$num." (".count($stats["closedbillets_thismonth"]).")</a><br>"; 
						echo "<center>";
						echo "<a href='".$link."'>".billetsFeedbackRatingImage ( $num )."</a>";
						echo "</center>";						
					?>
				</td>
			  </tr>

              
			</table>
		</div>
        
        <?php
// ************************************************************************