Hi @ajnyga, thank you for this plugin, we’ve been using it in our 3.1 installation since last year.
I was trying to make it compatible with the 3.2, but since I am only an OJS enthusiast without any coding background probably the modification below will need more works. Nevertheless, I can confirm that the modification of your codes below are working in our local installation.
// Start Most read
public function loadTemplateData($hookName, $args) {
$templateMgr = $args[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$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);
}
// End Most read
/**
* Set cache
* @param $cache object
/
function _cacheMiss($cache) {
$metricsDao = DAORegistry::getDAO(‘MetricsDAO’);
$submissionDao = DAORegistry::getDAO(‘SubmissionDAO’); / @var $submissionDao SubmissionDAO */
$publishedArticles = Services::get(‘submission’)->getMany([
‘status’ => STATUS_PUBLISHED,
‘count’ => 5000, // large upper limit
]);
//
$journalDao = DAORegistry::getDAO(‘JournalDAO’);
$request = Application::get()->getRequest();
$context = $request->getContext();
$mostReadDays = (int) $this->getSetting($context->getId(), ‘mostReadDays’);
if (empty($mostReadDays)){
$mostReadDays = 30;
}
$dayString = “-” . $mostReadDays . " days";
$daysAgo = date(‘Ymd’, strtotime($dayString));
$currentDate = date(‘Ymd’);
$filter = array(
STATISTICS_DIMENSION_CONTEXT_ID => $context->getId(),
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(7);
$metricsDao = DAORegistry::getDAO(‘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);
$request = $this->getRequest(); /ok/
$journal = $request->getJournal(); /ok/
$journalId = $journal->getId(); /ok/
$articles[$submissionId][‘journalPath’] = $journal->getPath();
$articles[$submissionId][‘articleId’] = $article->getId();
$articles[$submissionId][‘articleTitle’] = $article->getLocalizedTitle();
$articles[$submissionId][‘metric’] = $resultRecord[STATISTICS_METRIC];
}
$cache->setEntireCache($articles);
return $result;
}
Request in the template
<ul class="most_read"> {foreach from=$resultMetrics item=submission} <li class="most_read_submission"> <div class="most_read_submission_title"><a href="{url >journal=$submission.journalPath page="article" op="view" path=$submission.articleId}">>{$submission.articleTitle}{if !empty($submission.articleSubTitle)} {$submission.articleSubTitle}{/if}</a></div> <div class="most_read_submission_journal"><span class="fa fa-eye"></span> {$submission.metric}</div> </li> {/foreach} </ul>