Welcome to mirror list, hosted at ThFree Co, Russian Federation.

API.php « VisitTime « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f706860b4e4adeea0163f8b636619a8008de81d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\VisitTime;

use Exception;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Date;
use Piwik\Metrics;
use Piwik\Period;
use Piwik\Piwik;
use Piwik\Site;

require_once PIWIK_INCLUDE_PATH . '/plugins/VisitTime/functions.php';

/**
 * VisitTime API lets you access reports by Hour (Server time), and by Hour Local Time of your visitors.
 *
 * @method static \Piwik\Plugins\VisitTime\API getInstance()
 */
class API extends \Piwik\Plugin\API
{
    protected function getDataTable($name, $idSite, $period, $date, $segment)
    {
        Piwik::checkUserHasViewAccess($idSite);
        $archive = Archive::build($idSite, $period, $date, $segment);
        $dataTable = $archive->getDataTable($name);

        $dataTable->filter('Sort', array('label', 'asc', true, false));
        $dataTable->queueFilter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getTimeLabel'));
        $dataTable->queueFilter('ReplaceColumnNames');
        return $dataTable;
    }

    public function getVisitInformationPerLocalTime($idSite, $period, $date, $segment = false)
    {
        $table = $this->getDataTable(Archiver::LOCAL_TIME_RECORD_NAME, $idSite, $period, $date, $segment);
        $table->filter('AddSegmentValue');

        return $table;
    }

    public function getVisitInformationPerServerTime($idSite, $period, $date, $segment = false, $hideFutureHoursWhenToday = false)
    {
        $table = $this->getDataTable(Archiver::SERVER_TIME_RECORD_NAME, $idSite, $period, $date, $segment);

        $timezone = Site::getTimezoneFor($idSite);
        $table->filter('Piwik\Plugins\VisitTime\DataTable\Filter\AddSegmentByLabelInUTC', array($timezone, $period, $date));

        if ($hideFutureHoursWhenToday) {
            $table = $this->removeHoursInFuture($table, $idSite, $period, $date);
        }

        return $table;
    }

    /**
     * Returns datatable describing the number of visits for each day of the week.
     *
     * @param string $idSite The site ID. Cannot refer to multiple sites.
     * @param string $period The period type: day, week, year, range...
     * @param string $date The start date of the period. Cannot refer to multiple dates.
     * @param bool|string $segment The segment.
     * @throws Exception
     * @return DataTable
     */
    public function getByDayOfWeek($idSite, $period, $date, $segment = false)
    {

        Piwik::checkUserHasViewAccess($idSite);

        // metrics to query
        $metrics = Metrics::getVisitsMetricNames();
        unset($metrics[Metrics::INDEX_MAX_ACTIONS]);

        // disabled for multiple dates
        if (Period::isMultiplePeriod($date, $period)) {
            throw new Exception("VisitTime.getByDayOfWeek does not support multiple dates.");
        }

        // get metric data for every day within the supplied period
        $oPeriod = Period\Factory::makePeriodFromQueryParams(Site::getTimezoneFor($idSite), $period, $date);
        $dateRange = $oPeriod->getDateStart()->toString() . ',' . $oPeriod->getDateEnd()->toString();
        $archive = Archive::build($idSite, 'day', $dateRange, $segment);

        // disabled for multiple sites
        if (count($archive->getParams()->getIdSites()) > 1) {
            throw new Exception("VisitTime.getByDayOfWeek does not support multiple sites.");
        }

        $dataTable = $archive->getDataTableFromNumeric($metrics)->mergeChildren();

        // if there's no data for this report, don't bother w/ anything else
        if ($dataTable->getRowsCount() == 0) {
            return $dataTable;
        }

        // group by the day of the week (see below for dayOfWeekFromDate function)
        $dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\dayOfWeekFromDate'));

        // create new datatable w/ empty rows, then add calculated datatable
        $rows = array();
        foreach (array(1, 2, 3, 4, 5, 6, 7) as $day) {
            $rows[] = array('label' => $day, 'nb_visits' => 0);
        }
        $result = new DataTable();
        $result->addRowsFromSimpleArray($rows);
        $result->addDataTable($dataTable);

        // set day of week integer as metadata
        $result->filter('ColumnCallbackAddMetadata', array('label', 'day_of_week'));

        // translate labels
        $result->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\translateDayOfWeek'));

        // set datatable metadata for period start & finish
        $result->setMetadata('date_start', $oPeriod->getDateStart());
        $result->setMetadata('date_end', $oPeriod->getDateEnd());

        return $result;
    }

    /**
     * @param DataTable $table
     * @param int $idSite
     * @param string $period
     * @param string $date
     * @return mixed
     */
    protected function removeHoursInFuture($table, $idSite, $period, $date)
    {
        $site = new Site($idSite);

        if ($period == 'day'
            && ($date == 'today'
                || $date == Date::factory('now', $site->getTimezone())->toString())
        ) {
            $currentHour = Date::factory('now', $site->getTimezone())->toString('G');
            // If no data for today, this is an exception to the API output rule, as we normally return nothing:
            // we shall return all hours of the day, with nb_visits = 0
            if ($table->getRowsCount() == 0) {
                for ($hour = 0; $hour <= $currentHour; $hour++) {
                    $table->addRowFromSimpleArray(array('label' => $hour, 'nb_visits' => 0));
                }
                return $table;
            }

            $idsToDelete = array();
            foreach ($table->getRows() as $id => $row) {
                $hour = $row->getColumn('label');
                if ($hour > $currentHour) {
                    $idsToDelete[] = $id;
                }
            }
            $table->deleteRows($idsToDelete);
        }
        return $table;
    }
}