-
Notifications
You must be signed in to change notification settings - Fork 1
/
PopularArticlesBlockPlugin.inc.php
217 lines (182 loc) · 5.79 KB
/
PopularArticlesBlockPlugin.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @file plugins/blocks/popularArticles/PopularArticlesBlockPlugin.inc.php
*
* Copyright (c) 2013-2014 Simon Fraser University Library
* Copyright (c) 2003-2014 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class PopularArticlesBlockPlugin
* @ingroup plugins_blocks_popularArticles
*
* @brief Class for Popular Articles Block plugin
*/
import('lib.pkp.classes.plugins.BlockPlugin');
define('POPULAR_ARTICLES_BLOCK_PLUGIN_DEFAULT_MAX_ARTICLES_COUNT', 10);
class PopularArticlesBlockPlugin extends BlockPlugin {
/**
* @copydoc LazyLoadPlugin::register()
*/
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
}
return $success;
}
/**
* @copydoc BlockPlugin::getSupportedContexts()
*/
function getSupportedContexts() {
return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR, BLOCK_CONTEXT_HOMEPAGE);
}
/**
* @copydoc PKPPlugin::getName()
*/
function getName() {
return 'popularArticlesBlockPlugin';
}
/**
* @copydoc PKPPlugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.block.PopularArticles.displayName');
}
/**
* @copydoc PKPPlugin::getDescription()
*/
function getDescription() {
return __('plugins.block.PopularArticles.description');
}
/**
* @copydoc PKPPlugin::getTemplatePath()
*/
function getTemplatePath() {
return parent::getTemplatePath() . 'templates/';
}
/**
* @copydoc PKPPlugin::getManagementVerbs()
*/
function getManagementVerbs() {
$verbs = parent::getManagementVerbs();
if ($this->getEnabled()) {
$verbs[] = array('settings', __('manager.plugins.settings'));
}
return $verbs;
}
/**
* @copydoc PKPPlugin::manage()
*/
function manage($verb, $args, &$message, &$messageParams) {
$returner = parent::manage($verb, $args, $message, $messageParams);
$journal = $this->_getCurrentJournal();
switch($verb) {
case 'settings':
$settingsForm = $this->_getSettingsForm($request, $journal);
$settingsForm->initData();
$settingsForm->display($request);
break;
case 'save':
$settingsForm = $this->_getSettingsForm($request, $journal);
$settingsForm->readInputData();
if ($settingsForm->validate()) {
$settingsForm->execute();
$message = NOTIFICATION_TYPE_SUCCESS;
$messageParams = array('contents' => __('plugins.blocks.popularArticles.settings.saved'));
return false;
} else {
$settingsForm->display($request);
}
break;
default:
return $returner;
}
return true;
}
/**
* @copydoc BlockPlugin::getContents()
*/
function getContents(&$templateMgr) {
$journal =& $this->_getCurrentJournal();
if (!$journal) return '';
$popularArticles = $this->_getPopularArticles($journal);
if (empty($popularArticles)) return '';
$templateMgr->assign('popularArticles', $popularArticles);
$templateMgr->addStyleSheet(Request::getBaseUrl() . DIRECTORY_SEPARATOR . $this->getPluginPath() . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'popularArticles.css');
return parent::getContents($templateMgr);
}
//
// Private helper methods.
//
/**
* Get current requested journal.
* @return Journal
*/
function &_getCurrentJournal() {
$application =& PKPApplication::getApplication();
$request =& $application->getRequest();
$router =& $request->getRouter();
return $router->getContext($request);
}
/**
* Get plugin's setting form.
* @param $request Request
* @param $journal Journal
* @return PopularArticlesSettingsForm
*/
function _getSettingsForm(&$request, &$journal) {
$this->import('form.PopularArticlesSettingsForm');
$templateMgr =& TemplateManager::getManager($request);
$templateMgr->register_function('plugin_url', array(&$this, 'smartyPluginUrl'));
return new PopularArticlesSettingsForm(&$this, $journal);
}
/*
* Gets the most popular articles in the last months.
* If months setting is null, get from all time.
* @param $journal Journal
* @return array
*/
function _getPopularArticles($journal) {
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$journalId = $journal->getId();
$maxArticlesCount = $this->getSetting($journalId, 'maxArticlesCount');
if (!$maxArticlesCount) {
$maxArticlesCount = POPULAR_ARTICLES_BLOCK_PLUGIN_DEFAULT_MAX_ARTICLES_COUNT;
}
$months = $this->getSetting($journalId, 'months');
$mixOldStats = $this->getSetting($journalId, 'mixOldStats');
import('lib.pkp.classes.db.DBResultRange');
$dbResultRange = new DBResultRange($maxArticlesCount);
$metricsDao =& DAORegistry::getDAO('MetricsDAO'); /* @var $metricsDao MetricsDAO */
// Get view statistics.
PluginRegistry::loadCategory('reports');
$metricType = array($journal->getDefaultMetricType());
if ($mixOldStats) {
$metricType[] = OJS_METRIC_TYPE_LEGACY_DEFAULT;
}
$stats = $metricsDao->getMetrics(
$metricType,
array(STATISTICS_DIMENSION_SUBMISSION_ID),
array(STATISTICS_DIMENSION_CONTEXT_ID => $journalId, STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_ARTICLE),
array(STATISTICS_METRIC => STATISTICS_ORDER_DESC),
$dbResultRange,
false
);
$articles = array();
if (!is_array($stats)) return $articles;
foreach ($stats as $statRecord) {
if (count($articlesInfo) == $maxArticlesCount) break;
$articleId = $statRecord[STATISTICS_DIMENSION_SUBMISSION_ID];
$count = $statRecord[STATISTICS_METRIC];
$article = $publishedArticleDao->getPublishedArticleByArticleId($articleId); /* @var $article PublishedArticle */
if (!is_a($article, 'PublishedArticle')) continue;
if ($months) {
$limit = strtotime('-' . $months . ' months');
if (strtotime($article->getDatePublished()) < $limit) continue;
}
$articles[$articleId] = $article;
}
return $articles;
}
}
?>