Source of file StatsReportCommon.php

Size: 10,133 Bytes - Last Modified: 2016-05-18T03:08:27+02:00

buildproject/core/module_stats/admin/reports/StatsReportCommon.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
<?php
/*"******************************************************************************************************
*   (c) 2004-2006 by MulchProductions, www.mulchprod.de                                                 *
*   (c) 2007-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$                                *
********************************************************************************************************/

namespace Kajona\Stats\Admin\Reports;


use Kajona\System\Admin\Reports\AdminStatsreportsInterface;
use Kajona\System\Admin\ToolkitAdmin;
use Kajona\System\System\Carrier;
use Kajona\System\System\Database;
use Kajona\System\System\GraphFactory;
use Kajona\System\System\Lang;
use Kajona\System\System\SystemSetting;

/**
 * This plugin creates a view common numbers, such as "user online" or "total pagehits"
 *
 * @package module_stats
 * @author sidler@mulchprod.de
 */
class StatsReportCommon implements AdminStatsreportsInterface
{

    //class vars
    private $intDateStart;
    private $intDateEnd;
    private $intInterval = 1;

    private $objTexts;
    private $objToolkit;
    private $objDB;


    /**
     * Constructor
     */
    public function __construct(Database $objDB, ToolkitAdmin $objToolkit, Lang $objTexts)
    {

        $this->objTexts = $objTexts;
        $this->objToolkit = $objToolkit;
        $this->objDB = $objDB;

        if (Carrier::getInstance()->getObjConfig()->getPhpIni("memory_limit") < 30) {
            @ini_set("memory_limit", "30M");
        }
    }

    /**
     * Returns the name of extension/plugin the objects wants to contribute to.
     *
     * @return string
     */
    public static function getExtensionName()
    {
        return "core.stats.admin.statsreport";
    }

    /**
     * @param int $intEndDate
     *
     * @return void
     */
    public function setEndDate($intEndDate)
    {
        $this->intDateEnd = $intEndDate;
    }

    /**
     * @param int $intStartDate
     *
     * @return void
     */
    public function setStartDate($intStartDate)
    {
        $this->intDateStart = $intStartDate;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->objTexts->getLang("allgemein", "stats");
    }

    /**
     * @return bool
     */
    public function isIntervalable()
    {
        return true;
    }

    /**
     * @param int $intInterval
     *
     * @return void
     */
    public function setInterval($intInterval)
    {
        $this->intInterval = $intInterval;
    }

    /**
     * @return string
     */
    public function getReport()
    {
        $strReturn = "";

        //Create Data-table
        $arrHeader = array();

        $arrValues = array();
        $arrValues[0] = array();
        $arrValues[0][] = $this->objTexts->getLang("anzahl_hits", "stats");
        $arrValues[0][] = $this->getHits();

        $arrValues[1] = array();
        $arrValues[1][] = $this->objTexts->getLang("anzahl_visitor", "stats");
        $arrValues[1][] = $this->getVisitors();

        $arrValues[2] = array();
        $arrValues[2][] = $this->objTexts->getLang("anzahl_pagespvisit", "stats");
        $arrValues[2][] = $this->getPagesPerVisit();

        $arrValues[3] = array();
        $arrValues[3][] = $this->objTexts->getLang("anzahl_timepvisit", "stats");
        $arrValues[3][] = $this->getTimePerVisit();

        $arrValues[4] = array();
        $arrValues[4][] = "&nbsp;";
        $arrValues[4][] = " ";

        $arrValues[5] = array();
        $arrValues[5][] = $this->objTexts->getLang("anzahl_online", "stats");
        $arrValues[5][] = $this->getNumberOfCurrentUsers();


        $strReturn .= $this->objToolkit->dataTable($arrHeader, $arrValues);

        return $strReturn;
    }

    /**
     * Returns the number of hits
     *
     * @return int
     */
    public function getHits()
    {
        $strQuery = "SELECT COUNT(*)
						FROM "._dbprefix_."stats_data
						WHERE stats_date > ?
						  AND stats_date <= ?";

        $arrRow = $this->objDB->getPRow($strQuery, array($this->intDateStart, $this->intDateEnd));
        $intReturn = $arrRow["COUNT(*)"];

        return $intReturn;
    }

    /**
     * Returns the number of hits
     *
     * @param int $intStart
     * @param int $intEnd
     *
     * @return array
     */
    private function getHitsForOnePeriod($intStart, $intEnd)
    {
        $strQuery = "SELECT COUNT(*)
						FROM "._dbprefix_."stats_data
						WHERE stats_date > ?
								AND stats_date <= ?";

        $arrTemp = $this->objDB->getPRow($strQuery, array($intStart, $intEnd));
        return $arrTemp["COUNT(*)"];
    }

    /**
     * Returns the number of visitors
     *
     * @return int
     */
    public function getVisitors()
    {

        $strQuery = "SELECT stats_ip , stats_browser
						FROM "._dbprefix_."stats_data
						WHERE stats_date > ?
								AND stats_date <= ?
						GROUP BY stats_ip, stats_browser";

        $arrRows = $this->objDB->getPArray($strQuery, array($this->intDateStart, $this->intDateEnd));
        return count($arrRows);
    }


    /**
     * Returns the number of visitors
     *
     * @param int $intStart
     * @param int $intEnd
     *
     * @return array
     */
    private function getVisitorsForOnePeriod($intStart, $intEnd)
    {
        $strQuery = "SELECT stats_ip, stats_browser
						FROM "._dbprefix_."stats_data
						WHERE stats_date > ?
								AND stats_date <= ?
						GROUP BY stats_ip, stats_browser";
        $arrTemp = $this->objDB->getPArray($strQuery, array($intStart, $intEnd));
        return count($arrTemp);
    }

    /**
     * Returns the average number of pages per visit
     *
     * @return int
     */
    private function getPagesPerVisit()
    {
        $intReturn = 0;
        $intUser = $this->getVisitors();
        $intHits = $this->getHits();

        if ($intHits != 0) {
            $intReturn = (int)($intHits / $intUser);
        }

        return $intReturn;
    }

    /**
     * Returns the number of useres currently online browsing the portal
     *
     * @return int
     */
    public function getNumberOfCurrentUsers()
    {
        $strQuery = "SELECT stats_ip, stats_browser, count(*)
					  FROM "._dbprefix_."stats_data
					  WHERE stats_date > ?
					  GROUP BY stats_ip, stats_browser";

        $arrRow = $this->objDB->getPArray($strQuery, array(time() - SystemSetting::getConfigValue("_stats_duration_online_")));

        return count($arrRow);
    }

    /**
     * Returns the average time a user spent on the site
     *
     * @return int
     */
    private function getTimePerVisit()
    {
        $strQuery = "SELECT MAX(stats_date) as max,
                            MIN(stats_date) as min,
                            MAX(stats_date)-MIN(stats_date) as dauer,
                            stats_session
                     FROM "._dbprefix_."stats_data
                     WHERE stats_session != ''
                       AND stats_date > ?
					   AND stats_date <= ?
                     GROUP BY stats_session";

        $arrSessions = $this->objDB->getPArray($strQuery, array($this->intDateStart, $this->intDateEnd));
        $intTime = 0;
        foreach ($arrSessions as $arrOneSession) {
            $intTime += $arrOneSession["dauer"];
        }

        if (count($arrSessions) > 0) {
            $intTime = $intTime / count($arrSessions);

            return ceil($intTime);
        }
        else {
            return "0";
        }
    }

    /**
     * @return array|string
     */
    public function getReportGraph()
    {
        //load datasets, reloading after 30 days to limit memory consumption
        $arrHits = array();
        $arrUser = array();
        $arrTickLabels = array();

        //create tick labels
        $intCount = 0;
        $intDBStart = $this->intDateStart;
        $intDBEnd = ($intDBStart + $this->intInterval * 24 * 60 * 60);


        while ($intDBStart <= $this->intDateEnd) {

            $arrTickLabels[$intCount] = date("d.m.", $intDBStart);
            $arrHits[$intCount] = $this->getHitsForOnePeriod($intDBStart, $intDBEnd);
            $arrUser[$intCount] = $this->getVisitorsForOnePeriod($intDBStart, $intDBEnd);

            $intDBStart = $intDBEnd;
            $intDBEnd = ($intDBStart + $this->intInterval * 24 * 60 * 60);

            $this->objDB->flushQueryCache();
            $intCount++;
        }

        //create a graph ->line-graph
        if ($intCount > 1) {


            $objChart1 = GraphFactory::getGraphInstance();
            $objChart1->setStrGraphTitle($this->objTexts->getLang("graph_hitsPerDay", "stats"));
            $objChart1->setStrXAxisTitle($this->objTexts->getLang("graph_date", "stats"));
            $objChart1->setStrYAxisTitle($this->objTexts->getLang("graph_hits", "stats"));
            $objChart1->setIntWidth(715);
            $objChart1->setIntHeight(200);
            $objChart1->setArrXAxisTickLabels($arrTickLabels);
            $objChart1->addLinePlot($arrHits, "Hits");
            $objChart1->setBitRenderLegend(false);

            $objChart2 = GraphFactory::getGraphInstance();
            $objChart2->setStrGraphTitle($this->objTexts->getLang("graph_visitorsPerDay", "stats"));
            $objChart2->setStrXAxisTitle($this->objTexts->getLang("graph_date", "stats"));
            $objChart2->setStrYAxisTitle($this->objTexts->getLang("graph_visitors", "stats"));
            $objChart2->setIntWidth(715);
            $objChart2->setIntHeight(200);
            $objChart2->setArrXAxisTickLabels($arrTickLabels);
            $objChart2->addLinePlot($arrUser, "Visitors/Day");
            $objChart2->setBitRenderLegend(false);

            $this->objDB->flushQueryCache();

            return array($objChart1->renderGraph(), $objChart2->renderGraph());

        }
        else {
            return "";
        }
    }

}