Hi,
I’m still struggling with this one. I have looked at the code from MostReadBlockPlugin.inc (for OJS 3.3) and would like to add the calls to my theme plugin so that I can display the most read articles inside the main content of the home page. Not entirely sure what I’m doing wrong, but there are no results being shown, no article read-counts being recorded.
To add the code to the theme class I have used: (edited to remove irrelevant code-blocks)
class TestThemePlugin extends ThemePlugin {
public function init() {
AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_APP_MANAGER);
$request = Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();
$router = $request->getRouter();
import('classes.file.PublicFileManager');
$publicFileManager = new PublicFileManager();
$baseUrl = $request->getBaseUrl() . '/' . $publicFileManager->getContextFilesPath($context->getId());
$contextApiUrl = $dispatcher->url($request, ROUTE_API, $context->getPath(), 'contexts/' . $context->getId());
$themeApiUrl = $dispatcher->url($request, ROUTE_API, $context->getPath(), 'contexts/' . $context->getId() . '/theme');
$temporaryFileApiUrl = $dispatcher->url($request, ROUTE_API, $context->getPath(), 'temporaryFiles');
// Only process for frontend context
if ($context) {
HookRegistry::register('TemplateManager::display', [$this, 'mostRead']);
}
... other styles and scripts ...
}
public function mostRead($hookName, $args) {
$smarty = $args[0];
$template = $args[1];
if ($template != 'frontend/pages/indexJournal.tpl') return false;
$request = Application::get()->getRequest();
$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();
$smarty->assign('resultMetrics', $resultMetrics);
// Commented out the block title as it is not required by my theme
//$mostReadBlockTitle = unserialize($this->getSetting($context->getId(), 'mostReadBlockTitle'));
//$locale = AppLocale::getLocale();
//$blockTitle = $mostReadBlockTitle[$locale] ? $mostReadBlockTitle[$locale] : __('plugins.blocks.mostRead.settings.blockTitle');
//$smarty->assign('blockTitle', $blockTitle);
return;
}
/**
* 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);
$articles=[]; // This line is new - $articles[] was not set and was producing an error in the log
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;
}
}
mostRead() and _cacheMiss() are both being called but no data is being returned.
When I was attempting to use the data returned from the block plugin itself, it was blank too, so this is why I am attempting to load the information from the main theme class instead.
Has anyone else been able to retrieve and display the most-read articles in OJS 3.3.0.10?