Source of file StatsReportToppages.php

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

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
<?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\UserUser;

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

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

    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("topseiten", "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();
        //Fetch data
        $arrPages = $this->getTopPages();

        //calc a few values
        $intSum = 0;
        foreach ($arrPages as $arrOnePage) {
            $intSum += $arrOnePage["anzahl"];
        }

        $intI = 0;
        $objUser = new UserUser(Session::getInstance()->getUserID());
        foreach ($arrPages as $arrOnePage) {
            //Escape?
            if ($intI >= $objUser->getIntItemsPerPage()) {
                break;
            }
            $arrValues[$intI] = array();
            $arrValues[$intI][] = $intI + 1;
            $arrValues[$intI][] = $arrOnePage["name"];
            $arrValues[$intI][] = $arrOnePage["language"];
            $arrValues[$intI][] = $arrOnePage["anzahl"];
            $arrValues[$intI][] = $this->objToolkit->percentBeam($arrOnePage["anzahl"] / $intSum * 100);
            $intI++;
        }

        //HeaderRow
        $arrHeader[] = "#";
        $arrHeader[] = $this->objTexts->getLang("top_seiten_titel", "stats");
        $arrHeader[] = $this->objTexts->getLang("commons_language", "stats");
        $arrHeader[] = $this->objTexts->getLang("commons_hits_header", "stats");
        $arrHeader[] = $this->objTexts->getLang("anteil", "stats");

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

        return $strReturn;
    }

    /**
     * Returns the pages and their hits
     *
     * @return mixed
     */
    public function getTopPages()
    {
        $objUser = new UserUser(Session::getInstance()->getUserID());
        $strQuery = "SELECT stats_page as name, count(*) as anzahl, stats_language as language
						FROM "._dbprefix_."stats_data
						WHERE stats_date > ?
								AND stats_date <= ?
						GROUP BY stats_page, stats_language
							ORDER BY anzahl desc";

        return $this->objDB->getPArray($strQuery, array($this->intDateStart, $this->intDateEnd), 0, $objUser->getIntItemsPerPage() - 1);
    }

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

        $arrGraphData = array();
        $arrPlots = array();
        $arrLabels = array();
        $intCount = 1;
        foreach ($arrPages as $arrOnePage) {
            $arrGraphData[] = $arrOnePage["anzahl"];
            $arrLabels[] = $intCount;
            if ($intCount <= 6) {
                $arrPlots[$arrOnePage["name"]] = array();
            }

            if ($intCount++ >= 9) {
                break;
            }
        }

        if (count($arrGraphData) > 1) {
            //generate a bar-chart
            $objGraph = GraphFactory::getGraphInstance();
            $objGraph->setArrXAxisTickLabels($arrLabels);
            $objGraph->addBarChartSet($arrGraphData, $this->objTexts->getLang("top_seiten_titel", "stats"));
            $objGraph->setStrXAxisTitle($this->objTexts->getLang("top_seiten_titel", "stats"));
            $objGraph->setStrYAxisTitle($this->objTexts->getLang("commons_hits_header", "stats"));
            $objGraph->setBitRenderLegend(false);
            $arrReturn[] = $objGraph->renderGraph();

            //--- XY-Plot -----------------------------------------------------------------------------------
            //calc number of plots

            $arrTickLabels = array();

            $intGlobalEnd = $this->intDateEnd;
            $intGlobalStart = $this->intDateStart;

            $this->intDateEnd = $this->intDateStart + 60 * 60 * 24 * $this->intInterval;

            $intCount = 0;
            while ($this->intDateStart <= $intGlobalEnd) {
                $arrPagesData = $this->getTopPages();
                //init plot array for this period
                $arrTickLabels[] = date("d.m.", $this->intDateStart);
                foreach ($arrPlots as $strPage => &$arrOnePlot) {
                    $arrOnePlot[$intCount] = 0;
                    foreach ($arrPagesData as $arrOnePage) {
                        if ($arrOnePage["name"] == $strPage) {
                            $arrOnePlot[$intCount] += $arrOnePage["anzahl"];
                        }
                    }
                }
                //increase start & end-date
                $this->intDateStart = $this->intDateEnd;
                $this->intDateEnd = $this->intDateStart + 60 * 60 * 24 * $this->intInterval;
                $intCount++;
            }
            //create graph
            if ($intCount > 1) {
                $objGraph = GraphFactory::getGraphInstance();
                $objGraph->setArrXAxisTickLabels($arrTickLabels);

                foreach ($arrPlots as $arrPlotName => $arrPlotData) {
                    $objGraph->addLinePlot($arrPlotData, $arrPlotName);
                }
                $arrReturn[] = $objGraph->renderGraph();
            }
            //reset global dates
            $this->intDateEnd = $intGlobalEnd;
            $this->intDateStart = $intGlobalStart;

            return $arrReturn;
        }
        else {
            return "";
        }
    }

}