* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7327 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) exit; class StatsLive extends Module { function __construct() { $this->name = 'statslive'; $this->tab = 'analytics_stats'; $this->version = 1.0; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Visitors online'); $this->description = $this->l('Display the list of customers and visitors currently online.'); } public function install() { return (parent::install() AND $this->registerHook('AdminStatsModules')); } /** * Get the number of online customers * * @return array(array, int) array of online customers entries, number of online customers */ private function getCustomersOnline() { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' SELECT u.id_customer, u.firstname, u.lastname, pt.name as page FROM `'._DB_PREFIX_.'connections` c LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections LEFT JOIN `'._DB_PREFIX_.'page` p ON p.id_page = cp.id_page LEFT JOIN `'._DB_PREFIX_.'page_type` pt ON p.id_page_type = pt.id_page_type INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest INNER JOIN `'._DB_PREFIX_.'customer` u ON u.id_customer = g.id_customer WHERE cp.`time_end` IS NULL AND TIME_TO_SEC(TIMEDIFF(NOW(), cp.`time_start`)) < 900 GROUP BY c.id_connections ORDER BY u.firstname, u.lastname'); return array($result, Db::getInstance()->NumRows()); } /** * Get the number of online visitors * * @return array(array, int) array of online visitors entries, number of online visitors */ private function getVisitorsOnline() { if (Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) $query = 'SELECT c.id_guest, c.ip_address, c.date_add, c.http_referer, pt.name as page FROM `'._DB_PREFIX_.'connections` c LEFT JOIN `'._DB_PREFIX_.'connections_page` cp ON c.id_connections = cp.id_connections LEFT JOIN `'._DB_PREFIX_.'page` p ON p.id_page = cp.id_page LEFT JOIN `'._DB_PREFIX_.'page_type` pt ON p.id_page_type = pt.id_page_type INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest WHERE cp.`time_end` IS NULL AND (g.id_customer IS NULL OR g.id_customer = 0) AND cp.`time_start` > '.strtotime('-15 minutes').' GROUP BY c.id_connections ORDER BY c.date_add DESC'; else $query = 'SELECT c.id_guest, c.ip_address, c.date_add, c.http_referer FROM `'._DB_PREFIX_.'connections` c INNER JOIN `'._DB_PREFIX_.'guest` g ON c.id_guest = g.id_guest WHERE (g.id_customer IS NULL OR g.id_customer = 0) AND c.`date_add` > "'.date('Y-m-d H:i:s', strtotime('-15 minutes')).'" ORDER BY c.date_add DESC'; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query); return array($result, Db::getInstance()->NumRows()); } public function hookAdminStatsModules($params) { global $cookie; list($customers, $totalCustomers) = $this->getCustomersOnline(); list($visitors, $totalVisitors) = $this->getVisitorsOnline(); $irow = 0; echo ''; if (!Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) echo '
'.$this->l('You must activate the option "pages views for each customer" in the "Stats datamining" module in order to see the pages currently viewed by your customers.').'
'; echo '
'.$this->l('Customers online').''; if ($totalCustomers) { echo $this->l('Total:').' '.(int)($totalCustomers).' '; foreach ($customers as $customer) echo ' '; echo '
'.$this->l('ID').''.$this->l('Name').''.$this->l('Current Page').''.$this->l('View').'
'.$customer['id_customer'].' '.$customer['firstname'].' '.$customer['lastname'].' '.$customer['page'].'
'; } else echo $this->l('There are no customers online.'); echo '
'.$this->l('Visitors online').''; if ($totalVisitors) { echo $this->l('Total:').' '.(int)($totalVisitors).'
'; foreach ($visitors as $visitor) echo ' '; echo '
'.$this->l('Guest').''.$this->l('IP').''.$this->l('Since').''.$this->l('Current page').''.$this->l('Referrer').'
'.$visitor['id_guest'].' '.long2ip($visitor['ip_address']).' '.substr($visitor['date_add'], 11).' '.(isset($visitor['page']) ? $visitor['page'] : $this->l('Undefined')).' '.(empty($visitor['http_referer']) ? $this->l('none') : parse_url($visitor['http_referer'], PHP_URL_HOST)).'
'; } else echo $this->l('There are no visitors online.'); echo '
'; } }