OJS 3.3+ Most Read Articles block plugin

The Most Read Articles block plugin was available for OJS 3.2+ but not for 3.3.

for the OJS 3.3
I just edited the language files (xml > po).
I tried it in OJS 3.3.0.8 and it worked fine.
You can download the file for OJS 3.3.0.8 from the following link:

OJS3.3+ Most Read Articles block plugin (mostRead_OJS_3.3.tar.gz)

1 Like

Hi @kerimsarigul,

I believe @ajnyga is planning to update this plugin at some point, so it will be tested with 3.3, and at that point it could be added to the plugin gallery.

-Roger
PKP Team

1 Like

Hi All,

Just found this post - could I ask if there are any updates regarding the most-read plugin for OJS 3.3?

The code I’m using currently (taken from the OldGregg theme) doesn’t work due to the now missing PublishedArticleDAO. Does anyone have any examples of how to modify that code to work with 3.3?

For completeness I’ll paste the [non-functioning] code here:-

public function mostRead($hookName, $args) {
		$smarty = $args[0];

		$metricsDao = DAORegistry::getDAO('MetricsDAO');
		$publishedArticleDao = DAORegistry::getDAO('PublishedArticleDAO');

		$currentDate = date('Ymd');
		$sixMonthsAgo = date('Ymd', strtotime("-6 month"));

		$result = $metricsDao->retrieve("
			SELECT submission_id, SUM(metric) AS metric
			FROM metrics 
			WHERE (day BETWEEN $sixMonthsAgo AND $currentDate) AND
			(assoc_type='515' AND submission_id IS NOT NULL)
			GROUP BY submission_id
			ORDER BY metric DESC LIMIT {$this->getThemeLatestArticleCount()}
		");

		$articles = [];

		while (!$result->EOF) {
			$resultRow = $result->GetRowAssoc(false);
			$articles[] = $publishedArticleDao->getById($resultRow['submission_id']);
			$result->MoveNext();
		}

		$smarty->assign('mostRead', $articles);
	}

Many thanks,

Anthony

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?

Quick update - today I can see some results in the most-read section of the homepage. I cleared the cache yesterday but couldn’t see anything but I guess it’s because the metrics being recorded are on a 1-day lag? (Is that correct?)