-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAPI.php
80 lines (69 loc) · 2.78 KB
/
API.php
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
<?php
namespace Piwik\Plugins\RerIntranetSubnetwork;
use Piwik\DataTable\Row;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;
/**
* @see plugins/RerIntranetSubNetwork/functions.php
*/
require_once PIWIK_INCLUDE_PATH . '/plugins/RerIntranetSubnetwork/functions.php';
/**
* API for plugin RerIntranetSubnetwork
* @method static \Piwik\Plugins\RerIntranetSubnetwork\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
public function getIntranetSubnetwork($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTable(Archiver::INTRANETSUBNETWORK_RECORD_NAME);
$dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS));
$dataTable->queueFilter('ColumnCallbackReplace', ['label', __NAMESPACE__ . '\getSubnetName']);
$dataTable->queueFilter('ReplaceColumnNames');
$column = 'nb_visits';
$percCol = 'nb_visits_percentage';
$percColName = 'General_ColumnPercentageVisits';
$visitsSums = $archive->getDataTableFromNumeric($column);
// check whether given tables are arrays
if ($dataTable instanceof DataTable\Map) {
$tableArray = $dataTable->getDataTables();
$visitSumsArray = $visitsSums->getDataTables();
} else {
$tableArray = array($dataTable);
$visitSumsArray = array($visitsSums);
}
// walk through the results and calculate the percentage
foreach ($tableArray as $key => $table) {
foreach ($visitSumsArray as $k => $visits) {
if ($k == $key) {
$visitsSumTotal = (is_object($visits))
?
(float)$visits->getFirstRow()->getColumn($column)
:
(float)$visits;
}
}
// Skip aggregation of percentages when AddSummaryRow is called.
$columnAggregationOps = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME);
$columnAggregationOps[$percCol] = 'skip';
$table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnAggregationOps);
if (isset($visitsSumTotal)) {
$table->filter(
'ColumnCallbackAddColumnPercentage',
[
$percCol,
Metrics::INDEX_NB_VISITS,
$visitsSumTotal,
1
]
);
// we don't want <0% or >100%:
$table->filter('RangeCheck', array($percCol));
}
}
return $dataTable;
}
}