How to get total galley view count and display in issue.tpl?

I am trying to display the sum of abstract and galley view count(eg: abstract view times+pdf download times) in every article display by issue.tpl. I know the total view can get from code below:

$articleId = $article->getId();
$metricsDao = DAORegistry::getDAO(‘MetricsDAO’);
$result = $metricsDao->getMetrics($journal->getDefaultMetricType(), array(), array(STATISTICS_DIMENSION_SUBMISSION_ID => $articleId));
$totalViews = $result[0][‘metric’];
$templateMgr->assign(‘totalViews’, $totalViews);

However, these code is not work when i put them in IssueHandler.inc.php. They only worked in ArticleHandler.inc.php.

Any help is appreciated. Thanks!

What version of OJS are you using and where exactly did you try to put the code?

In OJS 2.4.8, for example, you don’t really get down to operating $article by $article in the IssueHandler. The published articles are assigned as a group:

And then enumerated in issue.tpl:

1 Like

Hi ctgraham, thank you for your reply.

I am using ojs 2.4.8.0, and what I want to achieve is something like image below(the red highlighted box), which show abstract view, galley view count and then sum them up.

Getting the abstact view is ok, but the problem is I do not know how to get pdf view count as shown in the image, this make me fail to calculate the sum too.

I tried code $galley->getViews() in issue.tpl but return Fatal error: Call to a member function getViews() on a non-object. However, $article->getViews() worked. I guess that is something missing for $galley.

1 Like

Are you familiar with PHP code? You’ll need some basic understanding of object oriented programming to pull this off.

The message you are getting “Call to a member function getViews() on a non-object.” means that at the point where you’re trying $galley->getViews(), the variable $galley is undefined.

If you show the context of where you’re adding your code, we should be able to point you toward how to access an article’s galleys from there.

Could you please tell me the exact procedure in OJS 3. Like from starting (what codes to put and which files). If possible please make a community showcase, cos i am sure that every one wants that thing. I dont understand why OJS has not kept it as default by now.

Sorry, this will require a high degree of comfort with PHP and Smarty. We can help guide you if you have that experience, but we can’t provide custom coding for each situation on the forum.

We are rapidly moving toward making these kinds of statistics public in the latest version of the software.

Sir, i do have experience. I had done that in 2.8.4 version. But i was not able do it here. Please guide me. I have 3.0.1

Please post the changes you attempted, within the context of the file you modified, and we can help to provide guidance.

The best way to do this is via GitHub, but posting here in the forum is also fine. Be sure to use triple backticks to surround code posted in the forum for formatting purposes:

 filename:

original context before
your code here
original context after

/public_html/templates/frontend/pages/article.tpl

{**
 * templates/frontend/pages/article.tpl
 *
 * Copyright (c) 2014-2016 Simon Fraser University Library
 * Copyright (c) 2003-2016 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @brief Display the page to view an article with all of it's details.
 *
 * @uses $article Article This article
 * @uses $issue Issue The issue this article is assigned to
 * @uses $section Section The journal section this article is assigned to
 * @uses $journal Journal The journal currently being viewed.
 *}
{include file="frontend/components/header.tpl" pageTitleTranslated=$article->getLocalizedTitle()|escape}

<div class="page page_article">
	{if $section}
		{include file="frontend/components/breadcrumbs_article.tpl" currentTitle=$section->getLocalizedTitle()}
	{else}
		{include file="frontend/components/breadcrumbs_article.tpl" currentTitleKey="article.article"}
	{/if}

	{* Show article overview *}
	{include file="frontend/objects/article_details.tpl"}

	{call_hook name="Templates::Article::Footer::PageFooter"}
{/if}


</div><!-- .page -->

{include file="frontend/components/footer.tpl"}

Code-

View Counter: {translate key="article.abstract"} viewed = {$article->getViews()}  times
{if $galleys}
{foreach from=$galleys item=galley name=galleyList}
{$galley->getGalleyLabel()} | {$galley->getViews()} | times
{/foreach}
{/if}

Now how it looks-

{**
 * templates/frontend/pages/article.tpl
 *
 * Copyright (c) 2014-2016 Simon Fraser University Library
 * Copyright (c) 2003-2016 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @brief Display the page to view an article with all of it's details.
 *
 * @uses $article Article This article
 * @uses $issue Issue The issue this article is assigned to
 * @uses $section Section The journal section this article is assigned to
 * @uses $journal Journal The journal currently being viewed.
 *}
{include file="frontend/components/header.tpl" pageTitleTranslated=$article->getLocalizedTitle()|escape}

<div class="page page_article">
	{if $section}
		{include file="frontend/components/breadcrumbs_article.tpl" currentTitle=$section->getLocalizedTitle()}
	{else}
		{include file="frontend/components/breadcrumbs_article.tpl" currentTitleKey="article.article"}
	{/if}

	{* Show article overview *}
	{include file="frontend/objects/article_details.tpl"}

	{call_hook name="Templates::Article::Footer::PageFooter"}


View Counter: {translate key="article.abstract"} viewed = {$article->getViews()}  times
{if $galleys}
{foreach from=$galleys item=galley name=galleyList}
{$galley->getGalleyLabel()} | {$galley->getViews()} | times
{/foreach}

{/if}


</div><!-- .page -->

{include file="frontend/components/footer.tpl"}

I am able to see the abstract view (its still 0, I dont know why)
However, I cant see galley file downloads.

www.jmri.org.in is the site.

Sadly my knowledge of object-oriented programming is poor, but now the problem has been solved after I added code below.

```{if is_a($article, ‘PublishedArticle’)}{assign var=galleys value=$article->getGalleys()}{/if}`

Can anyone please explain this line of code? I guess it is a kind of declaration of object variable but I not sure what is mean by PublishedArticle in is_a function.

Thanks!

This is Smarty code.

It essentially says, if the object class of the $article value matches “PublishedArticle”, then create a new Smarty variable $galleys, and give it the value of $article->getGalleys().

The method Article::getGalleys() returns an array of galley files for that article.

Please check my site. www.jmri.org.in

The stats does not work. The abstract view and download still show 0.

This conversation is continued here:

ok cool. I dont have access to servers log files

So what exactly is “PublishedArticle” ? A class name?

Thank you for your patience.

Yes, PublishedArticle is a class within OJS. See:

Thank you!

I have one more question, what is the process of outputing a certain tpl??
For example, if we now going to display issue.tpl, first we called the index.php in public\pages\issue, and then call IssueHandler.inc.php, is this the correct flow? And when will the classes file(public\classes\issue) be called?

OJS routes URLs through the page Handlers, but after that the inclusion of the various classes and the output of templates is arbitrary.

For some example walkthroughs, see:

And:

And:

Where to add that code?

Hi All,

I did and manage the TOC to show the view counts of galleys but unfortunately not the total. I add in the in the OJS 2.4.8.0 /templates/issue/issue.tpl

<div class="tocreadcount">
			{if is_a($article, 'PublishedArticle')}{assign var=galleys value=$article->getGalleys()}{/if}
			<span> | {$totalViews}</span> <span>{translate key="article.abstract"} views:</span> <span><strong>{$article->getViews()}</strong></span> 
            {if $galleys}
            {foreach from=$galleys item=galley name=galleyList}
                <span> | {$galley->getGalleyLabel()} views:</span> <span><strong>{$galley->getViews()}</strong></span> 
            {/foreach}
            {/if}
		</div>

Thank you,
Romy