forked from ajnyga/mostRead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMostReadBlockPlugin.inc.php
172 lines (148 loc) · 5.22 KB
/
MostReadBlockPlugin.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
<?php
/**
* @file plugins/blocks/mostRead/MostReadBlockPlugin.inc.php
*
* Copyright (c) 2014-2019 Simon Fraser University
* Copyright (c) 2003-2019 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class MostReadBlockPlugin
* @ingroup plugins_blocks_mostRead
*
* @brief Class for "Most Read" block plugin
*/
import('lib.pkp.classes.plugins.BlockPlugin');
class MostReadBlockPlugin extends BlockPlugin {
/**
* Install default settings on journal creation.
* @return string
*/
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
/**
* Get the display name of this plugin.
* @return String
*/
function getDisplayName() {
return __('plugins.blocks.mostRead.displayName');
}
/**
* Get a description of the plugin.
*/
function getDescription() {
return __('plugins.blocks.mostRead.description');
}
/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $actionArgs) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array_merge($actionArgs, array('verb' => 'settings'))),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $actionArgs)
);
}
/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
$this->import('MostReadSettingsForm');
$context = Application::getRequest()->getContext();
$contextId = ($context && isset($context) && $context->getId()) ? $context->getId() : CONTEXT_SITE;
switch($request->getUserVar('verb')) {
case 'settings':
$settingsForm = new MostReadSettingsForm($this, $contextId);
$settingsForm->initData();
return new JSONMessage(true, $settingsForm->fetch($request));
case 'save':
$settingsForm = new MostReadSettingsForm($this, $contextId);
$settingsForm->readInputData();
if ($settingsForm->validate()) {
$settingsForm->execute();
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification(
$request->getUser()->getId(),
NOTIFICATION_TYPE_SUCCESS,
array('contents' => __('plugins.blocks.mostRead.settings.saved'))
);
return new JSONMessage(true);
}
return new JSONMessage(true, $settingsForm->fetch($request));
}
return parent::manage($args, $request);
}
/**
* @copydoc BlockPlugin::getContents
*/
function getContents($templateMgr, $request = null) {
$context = $request->getContext();
if (!$context) return '';
$metricsDao = DAORegistry::getDAO('MetricsDAO');
$cacheManager = CacheManager::getManager();
$cache = $cacheManager->getCache($context->getId(), 'mostread' , array($this, '_cacheMiss'));
$daysToStale = 1;
if (time() - $cache->getCacheTime() > 60 * 60 * 24 * $daysToStale) {
$cache->flush();
}
$resultMetrics = $cache->getContents();
$templateMgr->assign('resultMetrics', $resultMetrics);
$mostReadBlockTitle = unserialize($this->getSetting($context->getId(), 'mostReadBlockTitle'));
$locale = AppLocale::getLocale();
$blockTitle = $mostReadBlockTitle[$locale] ? $mostReadBlockTitle[$locale] : __('plugins.blocks.mostRead.settings.blockTitle');
$templateMgr->assign('blockTitle', $blockTitle);
return parent::getContents($templateMgr, $request);
}
/**
* Set cache
* @param $cache object
*/
function _cacheMiss($cache) {
$submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
$journalDao = DAORegistry::getDAO('JournalDAO');
$mostReadDays = (int) $this->getSetting($cache->context, 'mostReadDays');
if (empty($mostReadDays)){
$mostReadDays = 120;
}
$dayString = "-" . $mostReadDays . " days";
$daysAgo = date('Ymd', strtotime($dayString));
$currentDate = date('Ymd');
$filter = array(
STATISTICS_DIMENSION_CONTEXT_ID => $cache->context,
STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_SUBMISSION_FILE,
);
$filter[STATISTICS_DIMENSION_DAY]['from'] = $daysAgo;
$filter[STATISTICS_DIMENSION_DAY]['to'] = $currentDate;
$orderBy = array(STATISTICS_METRIC => STATISTICS_ORDER_DESC);
$column = array(
STATISTICS_DIMENSION_SUBMISSION_ID,
);
import('lib.pkp.classes.db.DBResultRange');
$dbResultRange = new DBResultRange(5);
$metricsDao = DAORegistry::getDAO('MetricsDAO'); /* @var $metricsDao MetricsDAO */
$result = $metricsDao->getMetrics(OJS_METRIC_TYPE_COUNTER, $column, $filter, $orderBy, $dbResultRange);
foreach ($result as $resultRecord) {
$submissionId = $resultRecord[STATISTICS_DIMENSION_SUBMISSION_ID];
$article = $submissionDao->getById($submissionId);
$journal = $journalDao->getById($article->getJournalId());
$articles[$submissionId]['journalPath'] = $journal->getPath();
$articles[$submissionId]['articleId'] = $article->getBestArticleId();
$articles[$submissionId]['articleTitle'] = $article->getLocalizedTitle();
$articles[$submissionId]['metric'] = $resultRecord[STATISTICS_METRIC];
}
$cache->setEntireCache($articles);
return $result;
}
}
?>