forked from ohilbig01/simpleStatistics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleStatisticsPlugin.inc.php
More file actions
120 lines (100 loc) · 3.69 KB
/
SimpleStatisticsPlugin.inc.php
File metadata and controls
120 lines (100 loc) · 3.69 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
<?php
/**
* @file plugins/generic/simpleStatistics/SimpleStatisticsPlugin.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 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
*/
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');
}
/**
* 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');
// 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();
$galleyViewTotal = 0;
foreach ($galleys as $galley) {
// remove "(English)" etc. from label
$label = preg_replace("/\(\w+\)/", "", $galley->getGalleyLabel($label));
$file = $galley->getFile();
$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!');
}
$galleyViews[$i] = $views;
$galleyViewTotal += $views;
}
}
$templateMgr->assign('galleyViewTotal', $galleyViewTotal);
$templateMgr->assign('galleyDownloads', $galleyViews);
$templateMgr->assign('galleyLabels', $galleyLabels);
$templateMgr->assign('galleyCount', count($galleyLabels));
$templateMgr->assign('journalPath', $request->getJournal()->getPath());
$output = $templateMgr->fetch($this->getTemplateResource('simpleStatistics.tpl'));
return false;
}
}
?>