/
MostReadBlockPlugin.inc.php
105 lines (87 loc) · 3.08 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
<?php
/**
* @file plugins/blocks/mostRead/MostReadBlockPlugin.inc.php
*
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2003-2018 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.block.mostRead.displayName');
}
/**
* Get a description of the plugin.
*/
function getDescription() {
return __('plugins.block.mostRead.description');
}
/**
* @see BlockPlugin::getContents
*/
function getContents($templateMgr, $request = null) {
$context = $request->getContext();
if (!$context) return '';
$metricsDao = DAORegistry::getDAO('MetricsDAO');
$cacheManager =& CacheManager::getManager();
$cache =& $cacheManager->getCache('mostread', $context->getId(), array($this, '_cacheMiss'));
$daysToStale = 1;
$cachedMetrics = false;
if (time() - $cache->getCacheTime() > 60 * 60 * 24 * $daysToStale) {
$cachedMetrics = $cache->getContents();
$cache->flush();
}
$resultMetrics = $cache->getContents();
if (!$resultMetrics && $cachedMetrics) {
$resultMetrics = $cachedMetrics;
$cache->setEntireCache($cachedMetrics);
} elseif (!$resultMetrics) {
$cache->flush();
}
$templateMgr->assign('resultMetrics', $resultMetrics);
return parent::getContents($templateMgr, $request);
}
function _cacheMiss($cache) {
$metricsDao = DAORegistry::getDAO('MetricsDAO');
$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');
$journalDao = DAORegistry::getDAO('JournalDAO');
$request = Application::getRequest();
$context = $request->getContext();
$currentDate = date('Ymd');
$weekAgo = date('Ymd', strtotime("-1 week"));
$result = $metricsDao->retrieve(
"SELECT submission_id, SUM(metric) AS metric FROM metrics WHERE (day BETWEEN $weekAgo AND $currentDate) AND (assoc_type='515' AND submission_id IS NOT NULL) AND (context_id='?') GROUP BY submission_id ORDER BY metric DESC LIMIT 5", (int) $context->getId()
);
while (!$result->EOF) {
$resultRow = $result->GetRowAssoc(false);
$article = $publishedArticleDao->getById($resultRow['submission_id']);
$journal = $journalDao->getById($article->getJournalId());
$articles[$resultRow['submission_id']]['journalPath'] = $journal->getPath();
$articles[$resultRow['submission_id']]['articleId'] = $article->getBestArticleId();
$articles[$resultRow['submission_id']]['articleTitle'] = $article->getLocalizedTitle();
$articles[$resultRow['submission_id']]['metric'] = $resultRow['metric'];
$result->MoveNext();
}
$result->Close();
$cache->setEntireCache($articles);
return $result;
}
}
?>