Source of file StatsReportTopqueries.php

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

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
<?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                                 *
********************************************************************************************************/

namespace Kajona\Stats\Admin\Reports;

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

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

    //class vars
    private $intDateStart;
    private $intDateEnd;

    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;
    }

    /**
     * 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("topqueries", "stats");
    }

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

    /**
     * @param int $intInterval
     *
     * @return void
     */
    public function setInterval($intInterval)
    {

    }

    /**
     * @return string
     */
    public function getReport()
    {
        $strReturn = "";
        //Create Data-table
        $arrHeader = array();
        $arrValues = array();
        //Fetch data
        $arrStats = $this->getTopQueries();

        //calc a few values
        $intSum = 0;
        foreach ($arrStats as $intHits) {
            $intSum += $intHits;
        }

        $intI = 0;
        $objUser = new UserUser(Session::getInstance()->getUserID());
        foreach ($arrStats as $strKey => $intHits) {
            //Escape?
            if ($intI >= $objUser->getIntItemsPerPage()) {
                break;
            }
            $arrValues[$intI] = array();
            $arrValues[$intI][] = $intI + 1;
            $arrValues[$intI][] = $strKey;
            $arrValues[$intI][] = $intHits;
            $arrValues[$intI][] = $this->objToolkit->percentBeam($intHits / $intSum * 100);
            $intI++;
        }
        //HeaderRow
        $arrHeader[] = "#";
        $arrHeader[] = $this->objTexts->getLang("top_query_titel", "stats");
        $arrHeader[] = $this->objTexts->getLang("top_query_gewicht", "stats");
        $arrHeader[] = $this->objTexts->getLang("anteil", "stats");

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

        return $strReturn;
    }

    /**
     * Returns the list of top-queries
     *
     * @return mixed
     */
    public function getTopQueries()
    {
        //Load all records in the passed interval
        $arrBlocked = explode(",", SystemSetting::getConfigValue("_stats_exclusionlist_"));

        $arrParams = array($this->intDateStart, $this->intDateEnd);

        $strExclude = "";
        foreach ($arrBlocked as $strBlocked) {
            if ($strBlocked != "") {
                $strExclude .= " AND stats_referer NOT LIKE ? \n";
                $arrParams[] = "%".str_replace("%", "\%", $strBlocked)."%";
            }
        }

        $strQuery = "SELECT stats_referer
						FROM "._dbprefix_."stats_data
						WHERE stats_date > ?
						  AND stats_date <= ?
						  AND stats_referer != ''
						  AND stats_referer IS NOT NULL
						    ".$strExclude."
						ORDER BY stats_date desc";
        $arrRecords = $this->objDB->getPArray($strQuery, $arrParams);

        $arrHits = array();
        //Suchpatterns: q=, query=
        $arrQuerypatterns = array("q=", "query=");
        foreach ($arrRecords as $arrOneRecord) {
            foreach ($arrQuerypatterns as $strOnePattern) {
                if (uniStrpos($arrOneRecord["stats_referer"], $strOnePattern) !== false) {
                    $strQueryterm = uniSubstr($arrOneRecord["stats_referer"], (uniStrpos($arrOneRecord["stats_referer"], $strOnePattern) + uniStrlen($strOnePattern)));
                    $strQueryterm = uniSubstr($strQueryterm, 0, uniStrpos($strQueryterm, "&"));
                    $strQueryterm = uniStrtolower(trim(urldecode($strQueryterm)));
                    if ($strQueryterm != "") {
                        if (isset($arrHits[$strQueryterm])) {
                            $arrHits[$strQueryterm]++;
                        }
                        else {
                            $arrHits[$strQueryterm] = 1;
                        }
                    }
                    break;
                }
            }
        }
        arsort($arrHits);
        return $arrHits;
    }

    /**
     * @return mixed|string
     */
    public function getReportGraph()
    {
        //collect data
        $arrPages = $this->getTopQueries();

        $arrGraphData = array();
        $arrLabels = array();
        $intCount = 1;
        foreach ($arrPages as $intHits) {
            $arrGraphData[$intCount] = $intHits;
            $arrLabels[] = $intCount;
            if ($intCount++ >= 8) {
                break;
            }
        }


        //generate a bar-chart
        if (count($arrGraphData) > 1) {
            $objGraph = GraphFactory::getGraphInstance();

            $objGraph->setArrXAxisTickLabels($arrLabels);
            $objGraph->addBarChartSet($arrGraphData, $this->objTexts->getLang("top_query_titel", "stats"));

            $objGraph->setStrXAxisTitle($this->objTexts->getLang("top_query_titel", "stats"));
            $objGraph->setStrYAxisTitle($this->objTexts->getLang("top_query_gewicht", "stats"));
            return $objGraph->renderGraph();
        }
        else {
            return "";
        }
    }

}