-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleStatisticsPlugin.inc.php
More file actions
177 lines (148 loc) · 6.32 KB
/
SimpleStatisticsPlugin.inc.php
File metadata and controls
177 lines (148 loc) · 6.32 KB
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
172
173
174
175
176
177
<?php
/**
* @file plugins/generic/simpleStatistics/SimpleStatisticsPlugin.inc.php
*
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2003-2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class SimpleStatisticsPlugin
* @ingroup plugins_generic_simpleStatistics
*
* @brief SimpleStatistics plugin class
*/
define('MAX_LABEL_LENGTH', 28);
import('lib.pkp.classes.plugins.GenericPlugin');
class SimpleStatisticsPlugin extends GenericPlugin
{
function register($category, $path, $mainContextId = null) {
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
// add some statistics in article details
HookRegistry::register('Templates::Article::Details::SimpleStatistics', array($this, 'addStatistics'));
}
return true;
}
return false;
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.simpleStatistics.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.simpleStatistics.description');
}
/*
* see lib/pkp/api/v1/stats/publications/PKPStatsPublicationHandler.inc.php
*/
function getAllViews($request, $articleId)
{
// Get the earliest date of publication
$contextId = $request->getContext()->getId();
$dateRange = Services::get('publication')->getDateBoundaries(['contextIds' => $contextId]);
$allowedParams['dateStart'] = $dateRange[0];
//$allowedParams['dateEnd'] = date('Y-m-d', strtotime('yesterday'));
$allowedParams['submissionIds'] = $articleId;
$allowedParams['contextIds'] = $contextId;
$statsService = Services::get('stats');
// Get the abstract records
$abstractRecords = $statsService->getRecords(array_merge($allowedParams, [
'assocTypes' => [ASSOC_TYPE_SUBMISSION],
'submissionIds' => [$articleId],
]));
$abstractViews = array_reduce($abstractRecords, [$statsService, 'sumMetric'], 0);
// Get the galley totals for each file type (pdf, html, other)
$galleyRecords = $statsService->getRecords(array_merge($allowedParams, [
'assocTypes' => [ASSOC_TYPE_SUBMISSION_FILE],
]));
$galleyViews = array_reduce($galleyRecords, [$statsService, 'sumMetric'], 0);
$pdfViews = array_reduce(array_filter($galleyRecords, [$statsService, 'filterRecordPdf']), [$statsService, 'sumMetric'], 0);
$htmlViews = array_reduce(array_filter($galleyRecords, [$statsService, 'filterRecordHtml']), [$statsService, 'sumMetric'], 0);
$otherViews = array_reduce(array_filter($galleyRecords, [$statsService, 'filterRecordOther']), [$statsService, 'sumMetric'], 0);
return [
'abstractViews' => $abstractViews,
'galleyViews' => $galleyViews,
'pdfViews' => $pdfViews,
'htmlViews' => $htmlViews,
'otherViews' => $otherViews,
];
}
/**
* Add stuff to article details page
* @param $hookName string
* @param $params array
*/
function addStatistics($hookName, $params)
{
$templateMgr =& $params[1];
$output =& $params[2];
$request = $this->getRequest();
$journal = $request->getContext();
$publishedArticle = $templateMgr->get_template_vars('article');
$metrics = $this->getAllViews($request, $publishedArticle->getId());
//error_log("SimpleStatistic metrics: " . var_export($metrics, true));
// Add Style
$cssUrl = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/styles/simpleStatistics.css';
$templateMgr->assign('cssUrl', $cssUrl);
// Count galley views
$galleyLabels = array();
$galleys = $publishedArticle->getGalleys();
if ($galleys) {
$this->import('SupplementaryMetricsDAO');
$supplementaryMetrics = new SupplementaryMetricsDAO();
$genreDao = DAORegistry::getDAO('GenreDAO');
$primaryGenres = $genreDao->getPrimaryByContextId($journal->getId())->toArray();
$primaryGenreIds = array_map(function($genre) {
return $genre->getId();
}, $primaryGenres);
$supplementaryGenres = $genreDao->getBySupplementaryAndContextId(true, $journal->getId())->toArray();
$supplementaryGenreIds = array_map(function($genre) {
return $genre->getId();
}, $supplementaryGenres);
$galleyViews = array();
foreach ($galleys as $galley) {
// remove "(English)" etc. from label
$label = preg_replace("/\(\w+\)/", "", $galley->getGalleyLabel($label));
// next if there's no corresponding file (remote galley)
if (!$file = $galley->getFile()) continue;
$i = array_search($label, $galleyLabels);
if ($i === false) {
$i = count($galleyLabels);
$galleyLabels[] = $label;
}
$galleyViews = array_pad($galleyViews, count($galleyLabels), '');
if (in_array($file->getGenreId(), $primaryGenreIds)) {
$views = $galley->getViews();
} elseif (in_array($file->getGenreId(), $supplementaryGenreIds)) {
$views = $supplementaryMetrics->getSupplementaryGalleyView($galley->getBestGalleyId());
}
else {
error_log('SimpleStatistics: unknown galley!');
}
// if it starts with 'pdf' or 'htm' (case insensitive)
if (stripos($galleyLabels[$i], "pdf") === 0) {
//$galleyViews[$i] = $views;
$galleyViews[$i] = $metrics['pdfViews'];
} elseif (stripos($galleyLabels[$i], "htm") === 0) {
//$galleyViews[$i] = $views;
$galleyViews[$i] = $metrics['htmlViews'];
} else {
$galleyViews[$i] = $views;
}
}
}
$templateMgr->assign('galleyDownloads', $galleyViews);
$templateMgr->assign('abstractViews', $metrics['abstractViews']); // same as $article->getViews()
$templateMgr->assign('galleyLabels', $galleyLabels);
$templateMgr->assign('galleyCount', count($galleyLabels));
$templateMgr->assign('journalPath', $request->getJournal()->getPath());
$templateMgr->assign('maxLabelLength', MAX_LABEL_LENGTH);
$output = $templateMgr->fetch($this->getTemplateResource('simpleStatistics.tpl'));
return false;
}
}