forked from pkp/usageStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKPUsageStatsReportPlugin.inc.php
172 lines (144 loc) · 4.95 KB
/
PKPUsageStatsReportPlugin.inc.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
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
166
167
168
169
170
171
<?php
/**
* @file plugins/generic/usageStats/PKPUsageStatsReportPlugin.inc.php
*
* Copyright (c) 2013-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PKPUsageStatsReportPlugin
* @ingroup plugins_generic_usageStats
*
* @brief OJS default statistics report plugin (and metrics provider)
*/
use PKP\statistics\PKPStatisticsHelper;
use PKP\plugins\ReportPlugin;
abstract class PKPUsageStatsReportPlugin extends ReportPlugin {
/**
* @copydoc Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
$this->addLocaleData();
return $success;
}
/**
* @copydoc Plugin::getName()
*/
function getName() {
return 'usageStatsReportPlugin';
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.reports.usageStats.report.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.reports.usageStats.report.description');
}
/**
* @copydoc ReportPlugin::display()
*/
function display($args, $request) {
$router = $request->getRouter();
$context = $router->getContext($request);
$metricType = $this->getMetricTypes();
import('classes.statistics.StatisticsHelper');
$statsHelper = new StatisticsHelper();
$columnNames = $statsHelper->getColumnNames();
// Make sure we aggregate by month instead of day.
unset($columnNames[PKPStatisticsHelper::STATISTICS_DIMENSION_DAY]);
$columns = array_keys($columnNames);
$reportArgs = array(
'metricType' => $metricType,
'columns' => $columns,
'filters' => json_encode(array(PKPStatisticsHelper::STATISTICS_DIMENSION_CONTEXT_ID => $context->getId())),
'orderBy' => json_encode(array(PKPStatisticsHelper::STATISTICS_DIMENSION_MONTH => PKPStatisticsHelper::STATISTICS_ORDER_ASC))
);
$request->redirect(null, null, 'reports', 'generateReport', $reportArgs);
}
/**
* @copydoc ReportPlugin::getMetrics()
*/
function getMetrics($metricType = null, $columns = null, $filters = null, $orderBy = null, $range = null) {
// This plug-in uses the MetricsDAO to store metrics. So we simply
// delegate there.
$metricsDao = DAORegistry::getDAO('MetricsDAO'); /* @var $metricsDao MetricsDAO */
return $metricsDao->getMetrics($metricType, $columns, $filters, $orderBy, $range);
}
/**
* @copydoc ReportPlugin::getMetricDisplayType()
*/
function getMetricDisplayType($metricType) {
return __('plugins.reports.usageStats.metricType');
}
/**
* @copydoc ReportPlugin::getMetricFullName()
*/
function getMetricFullName($metricType) {
return __('plugins.reports.usageStats.metricType.full');
}
/**
* @copydoc ReportPlugin::getDefaultReportTemplates()
*/
function getDefaultReportTemplates($metricTypes = null) {
$reports = array();
$pluginMetricTypes = $this->getMetricTypes();
if (is_null($metricTypes)) $metricTypes = $pluginMetricTypes;
if (!$this->isMetricTypeValid($metricTypes)) return $reports;
// Context index page views.
$columns = array(PKPStatisticsHelper::STATISTICS_DIMENSION_ASSOC_TYPE,
PKPStatisticsHelper::STATISTICS_DIMENSION_CONTEXT_ID,
PKPStatisticsHelper::STATISTICS_DIMENSION_MONTH,
PKPStatisticsHelper::STATISTICS_DIMENSION_COUNTRY);
$filter = array(PKPStatisticsHelper::STATISTICS_DIMENSION_ASSOC_TYPE => Application::getContextAssocType());
// We allow the subclasses to define the name locale key.
$reports[] = array('nameLocaleKey' => '',
'metricType' => $metricTypes, 'columns' => $columns, 'filter' => $filter,
'aggregationColumns' => $this->getAggregationColumns());
return $reports;
}
/**
* @copydoc ReportPlugin::getOptionalColumns()
*/
function getOptionalColumns($metricType) {
if (!$this->isMetricTypeValid($metricType)) return array();
return array(
PKPStatisticsHelper::STATISTICS_DIMENSION_CITY,
PKPStatisticsHelper::STATISTICS_DIMENSION_REGION
);
}
//
// Protected methods.
//
/**
* Get aggregation columns, the ones that
* can be part of any report template not changing
* it's main purpose.
* @return array
*/
protected function getAggregationColumns() {
return array(PKPStatisticsHelper::STATISTICS_DIMENSION_COUNTRY,
PKPStatisticsHelper::STATISTICS_DIMENSION_REGION,
PKPStatisticsHelper::STATISTICS_DIMENSION_CITY,
PKPStatisticsHelper::STATISTICS_DIMENSION_MONTH,
PKPStatisticsHelper::STATISTICS_DIMENSION_DAY);
}
/**
* Check the passed metric type against the
* metric types this plugin implements.
* @param $metricType array|string
* @return boolean
*/
protected function isMetricTypeValid($metricType) {
$pluginMetricTypes = $this->getMetricTypes();
if (!is_array($metricType)) $metricType = array($metricType);
// Check if the plugin supports the passed metric types.
$intersection = array_intersect($metricType, $pluginMetricTypes);
return !empty($intersection);
}
}