Location: PHPKode > projects > Statiqz, website statistics > statiqz/ctrl/StatisticsCtrl.class.php
<?php
    require_once 'ctrl/BaseCtrl.class.php';

    require_once 'out/StatisticsOut.class.php';

    require_once 'xdbx/CountriesXdbx.class.php';
    require_once 'xdbx/UsersXdbx.class.php';
    require_once 'xdbx/VisitorsXdbx.class.php';
    require_once 'xdbx/VisitsXdbx.class.php';
    require_once 'xdbx/OsXdbx.class.php';
    require_once 'xdbx/BrowsersXdbx.class.php';
    require_once 'xdbx/ReferrersXdbx.class.php';

    class StatisticsCtrl extends BaseCtrl {
        function StatisticsCtrl() {

        }

        function process() {
            $visitorsXdbx   = new VisitorsXdbx();
            $usersXdbx      = new UsersXdbx();
            $visitsXdbx     = new VisitsXdbx();
            $countriesXdbx  = new CountriesXdbx();
            $osXdbx         = new OsXdbx();
            $browsersXdbx   = new BrowsersXdbx();
            $referrersXdbx  = new ReferrersXdbx();

            if (isset($_GET['ID'])) {
                $ID = $_GET['ID'];
            } else {
                $ID = 1;
            }

            $user               = $usersXdbx->getUserByID($ID);
            $userReferrerID     = $referrersXdbx->getIDByReferrer(substr($user[0]['url'], 7));

            if ($ID == 1) {
                $numVisits          = $visitsXdbx->numAllVisits();
                $numVisitors        = $visitsXdbx->numAllUniqueVisits();
                $visits             = $visitsXdbx->getAllVisits();
                $geoLocations       = $countriesXdbx->sumAllCountries();
                $browsers           = $browsersXdbx->sumAllUsedBrowsers();
                $operatingSystems   = $osXdbx->sumAllUsedOs();
                $recentVisitors     = $visitsXdbx->numAllRecentVisitors(time() - (3600*24*7));
                $recentPageviews    = $visitsXdbx->numAllRecentPageviews(time() - (3600*24*7));
                $java               = ($visitorsXdbx->numAllJavaEnabled() / $numVisitors) * 100;
                $js                 = ($visitorsXdbx->numAllJsEnabled() / $numVisitors) * 100;
                $referrers          = $visitsXdbx->getAllTopReferrers($userReferrerID[0]['ID']);
            } else {
                $numVisits          = $visitsXdbx->numVisitsByUserID($ID);
                $numVisitors        = $visitsXdbx->numUniqueVisitsByUserID($ID);
                $visits             = $visitsXdbx->getVisitsByUserID($ID);
                $geoLocations       = $countriesXdbx->sumCountries($ID);
                $browsers           = $browsersXdbx->sumUsedBrowsers($ID);
                $operatingSystems   = $osXdbx->sumUsedOs($ID);
                $recentVisitors     = $visitsXdbx->numRecentVisitors($ID, time() - (3600*24*7));
                $recentPageviews    = $visitsXdbx->numRecentPageviews($ID, time() - (3600*24*7));
                $java               = ($visitorsXdbx->numJavaEnabled($ID) / $numVisitors) * 100;
                $js                 = ($visitorsXdbx->numJsEnabled($ID) / $numVisitors) * 100;
                $referrers          = $visitsXdbx->getTopReferrersByUserID($ID, $userReferrerID[0]['ID']);
            }

            $midnight   = 0;
            $morning    = 0;
            $afternoon  = 0;
            $evening    = 0;

            for ($i = 0; $i < count($visits); $i++) {
                $hour = gmdate('G', $visits[$i]['timestamp']);

                if ($hour < 6) {
                    $midnight++;
                } elseif ($hour < 12) {
                    $morning++;
                } elseif ($hour < 18) {
                    $afternoon++;
                } else {
                    $evening++;
                }
            }

            $midnight   = ($midnight / $numVisits) * 100;
            $morning    = ($morning / $numVisits) * 100;
            $afternoon  = ($afternoon/ $numVisits) * 100;
            $evening    = ($evening / $numVisits) * 100;

            $timeOfDay          = array('midnight' => $midnight, 'morning' => $morning,
                                  'afternoon' => $afternoon, 'evening' => $evening);

            for ($i = 0; $i < count($geoLocations); $i++) {
                $geoLocations[$i]['countrySum'] = ($geoLocations[$i]['countrySum'] / $numVisitors) * 100;
            }

            for ($i = 0; $i < count($browsers); $i++) {
                $browsers[$i]['browserSum'] = ($browsers[$i]['browserSum'] / $numVisitors) * 100;
            }

            for ($i = 0; $i < count($operatingSystems); $i++) {
                $operatingSystems[$i]['osSum'] = ($operatingSystems[$i]['osSum'] / $numVisitors) * 100;
            }

            $statisticsOut = new StatisticsOut($user, $numVisits, $numVisitors, $timeOfDay,
                $referrers, $geoLocations, $operatingSystems, $browsers, $java, $js,
                $recentVisitors, $recentPageviews);

            return $statisticsOut->get();
        }
    }
?>
Return current item: Statiqz, website statistics