-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMostReadBlockPlugin.php
More file actions
205 lines (181 loc) · 6.13 KB
/
Copy pathMostReadBlockPlugin.php
File metadata and controls
205 lines (181 loc) · 6.13 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
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
<?php
/**
* @file plugins/blocks/mostRead/MostReadBlockPlugin.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class MostReadBlockPlugin
*
* @ingroup plugins_blocks_mostRead
*
* @brief Class for "Most Read" block plugin
*/
namespace APP\plugins\blocks\mostRead;
use APP\core\Application;
use APP\facades\Repo;
use APP\template\TemplateManager;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use PKP\core\JSONMessage;
use PKP\facades\Locale;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\BlockPlugin;
use PKP\submission\PKPSubmission;
class MostReadBlockPlugin extends BlockPlugin
{
/** Cache lifetime in seconds (1 day). */
private const CACHE_TTL = 60 * 60 * 24;
/**
* Install default settings on press creation.
*/
public function getContextSpecificPluginSettingsFile()
{
return $this->getPluginPath() . '/settings.xml';
}
/**
* Get the display name of this plugin.
*/
public function getDisplayName()
{
return __('plugins.blocks.mostRead.displayName');
}
/**
* Get a description of the plugin.
*/
public function getDescription()
{
return __('plugins.blocks.mostRead.description');
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $actionArgs)
{
$router = $request->getRouter();
return array_merge(
$this->getEnabled() ? [
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array_merge($actionArgs, ['verb' => 'settings'])),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
] : [],
parent::getActions($request, $actionArgs)
);
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->registerPlugin('function', 'plugin_url', $this->smartyPluginUrl(...));
$form = new MostReadSettingsForm($this, $context->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* @copydoc BlockPlugin::getContents()
*/
public function getContents($templateMgr, $request = null)
{
$context = $request?->getContext();
if (!$context) {
return '';
}
$contextId = $context->getId();
$metrics = Cache::remember(
$this->getCacheKey($contextId),
self::CACHE_TTL,
fn () => $this->loadMostRead($contextId)
);
$locale = Locale::getLocale();
$mostReadBlockTitle = (array) json_decode($this->getSetting($contextId, 'mostReadBlockTitle') ?? '');
$blockTitle = $mostReadBlockTitle[$locale] ?? '';
$templateMgr->assign('blockTitle', $blockTitle);
$mostRead = [];
foreach ($metrics as $metric) {
$submission = Repo::submission()->get($metric['submissionId']);
if (!$submission) {
continue;
}
$publication = $submission->getCurrentPublication();
if (!$publication || (int) $publication->getData('status') !== PKPSubmission::STATUS_PUBLISHED) {
continue;
}
$mostRead[] = [
// OMP publishes monographs in the catalogue; there is no `article` page.
'url' => $request->url($context->getPath(), 'catalog', 'book', [$submission->getBestId()]),
'metric' => $metric['metric'],
'title' => $publication->getLocalizedFullTitle($locale, 'html'),
];
}
$templateMgr->assign('mostRead', $mostRead);
return parent::getContents($templateMgr, $request);
}
/**
* Build the cache key for a given context.
*/
public function getCacheKey(int $contextId): string
{
return "plugins.blocks.mostRead.{$contextId}";
}
/**
* Clear the cached metrics for a given context.
*/
public function clearCache(int $contextId): void
{
Cache::forget($this->getCacheKey($contextId));
}
/**
* Query the most read submissions for a context.
*
* @return array<int, array{submissionId: int, metric: int}>
*/
public function loadMostRead(int $contextId): array
{
$mostReadDays = (int) $this->getSetting($contextId, 'mostReadDays');
if (empty($mostReadDays)) {
$mostReadDays = 7;
}
$dayString = '-' . $mostReadDays . ' days';
$mostReadCount = (int) $this->getSetting($contextId, 'mostReadCount');
if ($mostReadCount < 1) {
$mostReadCount = 5;
}
$mostRead = app()->get('publicationStats')->getTotals([
'dateStart' => date('Y-m-d', strtotime($dayString)),
'contextIds' => [$contextId],
'count' => $mostReadCount,
'assocTypes' => [Application::ASSOC_TYPE_SUBMISSION_FILE],
]);
return (new Collection($mostRead))
->map(fn ($result) => [
'submissionId' => $result->submission_id,
'metric' => $result->metric,
])
->filter(fn ($result) => $result['submissionId'] && $result['metric'])
->values()
->toArray();
}
}