OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Counting comments for individual articles?

OJS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
Developer Resources:

Documentation: The OJS Technical Reference and the OJS API Reference are both available from the OJS Documentation page.

Git: You can access our public Git Repository here. Comprehensive Git usage instructions are available on the wiki.

Bugzilla: You can access our Bugzilla report tracker here.

Search: You can use our Google Custom Search to search across our main website, the support forum, and Bugzilla.

Questions and discussion are welcome, but if you have a workflow or usability question you should probably post to the OJS Editorial Support and Discussion subforum; if you have a technical support question, try the OJS Technical Support subforum.

Counting comments for individual articles?

Postby tarek » Sat Apr 24, 2010 11:32 pm

Hello all!

I'm trying to make a more interesting front page that includes the number of comments by the article links. At the moment, I'm having some trouble figuring out how to get an accurate comment count for a particular article. I have found getChildCommentCount(), but I can't quite figure out how to get the comment count for a particular article..

I'm sure it's something simple I'm missing in how to tell it *which* article I want the comment count for.. any ideas?

tarek : )
tarek
 
Posts: 14
Joined: Thu Jun 21, 2007 4:00 am

Re: Counting comments for individual articles?

Postby mcrider » Fri Apr 30, 2010 10:51 am

Hi tarek,

I think you might have to write a function in the classes/comment/CommentDAO.inc.php class--the getChildCount just get the number of comments (e.g. replies) for each comment itself. This is pretty simple, actually--Just duplicate the attributedCommentsExistForUser() function, and in the SQL statement, replace WHERE user_id = ? to WHERE article_id = ? (and change the userId variable to articleId). I'll assume you can figure out how to assign this value to the template you want it displayed on, but if you need any more help let us know.

Cheers,
Matt
mcrider
 
Posts: 952
Joined: Mon May 05, 2008 10:29 am
Location: Vancouver, BC

Re: Counting comments for individual articles?

Postby tarek » Fri Apr 30, 2010 12:12 pm

Thanks Matt!

I'll try this and post the patch.

tarek : )
tarek
 
Posts: 14
Joined: Thu Jun 21, 2007 4:00 am

Re: Counting comments for individual articles?

Postby tarek » Thu May 06, 2010 5:23 pm

Hmm. Actually, I can't figure it out.

So, I've created the following function in CommentDAO.inc.php:

Code: Select all
   /**
    * Check how many comments each article has.
    * @param $articleId int The ID of the article to check
    * @return boolean
    */
   function attributedCommentsExistForArticle($articleId) {
      $result =& $this->retrieve('SELECT count(*) FROM comments WHERE article_id = ?', (int) $articleId);
      $returner = $result->fields[0]?true:false;
      $result->Close();
      return $returner;
   }


But I can't figure out a way to access it at all.

I've tried playing around a bit in "pages" ,but no luck there. I've tried adding this to my customissue.tpl file:

Code: Select all
$commentDao =& DAORegistry::getDAO('CommentDAO');
$commentDao->attributedCommentsExistForArticle($articleID);


But also no go - it just presents as blank, even when I assign it to another variable ($test = $commentDao....)

Again.. probably I'm missing something quite basic here. Thoughts?

tarek : )
tarek
 
Posts: 14
Joined: Thu Jun 21, 2007 4:00 am

Re: Counting comments for individual articles?

Postby mcrider » Tue May 11, 2010 9:03 am

Hi tarek,

You're limited to what kind of code you can put in .tpl files, so you can't just put that call to $commentDao in there--You'll have to find the page that is calling customissue.tpl (i.e. by grepping for it) and assigning it to the template. There are many examples in the page classes of how to do this (e.g. where $templateMgr->assign() is called before $templateMgr->display() is called)--You then use the assigned variable in the template by calling {$variable}.

Hope that helps,
Matt
mcrider
 
Posts: 952
Joined: Mon May 05, 2008 10:29 am
Location: Vancouver, BC

Re: Counting comments for individual articles?

Postby tarek » Fri May 21, 2010 11:00 am

I finally got this working. You can see the effects on the Open Medicine site. Here are the modifications made:

pages/index/IndexHandler.inc.php
before templateMgr->display('index/journal.tpl');, add:
Code: Select all
$CommentDAO =& DAORegistry::getDAO('CommentDAO');
$templateMgr->assign('CommentDAO', $CommentDAO);


classes/comment/CommentDAO.inc.php
Code: Select all
function attributedCommentsExistForArticle($articleId) {
  $result =& $this->retrieve('SELECT count(*) FROM comments WHERE article_id = ?', (int) $articleId);
  $returner = $result->fields[0];
  $result->Close();
  return $returner;
}


templates/issue/customissue.tpl (or journal.tpl)
Code: Select all
{$CommentDAO->attributedCommentsExistForArticle($articleID)}


Where $articleID is the article you're counting comments for.

tarek : )
tarek
 
Posts: 14
Joined: Thu Jun 21, 2007 4:00 am

Re: Counting comments for individual articles?

Postby tarek » Tue Oct 19, 2010 10:14 pm

I've replaced that last bit of code (in the .tpl file) with this decided hack to make the output a bit more sane. Could be easily localized, obviously:

Code: Select all
{if $CommentDAO->attributedCommentsExistForArticle($article->getArticleId())}
   {if $CommentDAO->attributedCommentsExistForArticle($article->getArticleId()) == 1}
      <a href="{$baseUrl}/comment/view/{$article->getArticleId()}/0" class="file">Comment ({$CommentDAO->attributedCommentsExistForArticle($article->getArticleId())})</a>
   {else}
      <a href="{$baseUrl}/comment/view/{$article->getArticleId()}/0" class="file">Comments ({$CommentDAO->attributedCommentsExistForArticle($article->getArticleId())})</a>
   {/if}
{else}
   <a href="{$baseUrl}/comment/view/{$article->getArticleId()}/0" class="file">Add a comment</a>
{/if}
tarek
 
Posts: 14
Joined: Thu Jun 21, 2007 4:00 am


Return to OJS Development

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests