Tuesday, March 20, 2012

Sort Articles By Rating In Joomla 2.5

The following tutorial will tell you how to sort articles in Joomla by rating

I needed to sort articles in a list category layout and here are the few changes that I made. I am sure they will be applicable on previous Joomla versions as well, or at least you will get the idea:

Open file 'components\com_content\models\articles.php'

Look for the following line (221 for my Joomla 2.5 version)
$query->select('ROUND(v.rating_sum / v.rating_count,0) AS rating, v.rating_count as rating_count');

It is currently rounding the rating for some unknown reason i.e. if you have 4 ratings that combine to make it 4.25, it will show 4 (rounded) instead of 4.25 so first I fixed this by replacing it with the following line:

$query->select('FORMAT(v.rating_sum / v.rating_count,2) AS rating, v.rating_count as rating_count');

(You can even remove the 'FORMAT' if you are fine with going it up to 4-5 decimals)

Once you are done with this, then comes the sorting part, go to the end of this function and look for the following line (463 for me)

$query->order($this->getState('list.ordering', 'a.ordering').' '.$this->getState('list.direction', 'ASC'));

And replace it with

$query->order(' rating DESC ');

And you are good to go.


Note: You may also want to add a column in the front end to show the ratings. If you want and your are in list view of category you can go to the following file:
'components/com_content/views/category/tmpl/default_articles.php'
and look for a 'foreach' loop at approximately the mid of the file, add  your column of rating along with hits, author columns.

17 comments:

  1. Hi, thanks for your post - it works fine.

    But I'm wondering what If you need more than one ordering directions / example one page by rating and other page by default ordering.

    How is that possible?

    Thanks

    ReplyDelete
    Replies
    1. You can do that by keeping the default ordering and ordering by rating both in if else and then pass a parameter in URL for rating ordering.. something like this

      $sort = JRequest::getVar('sort');
      if($sort=="rating") {
      $query->order(' rating DESC ');
      } else {
      $query->order($this->getState('list.ordering', 'a.ordering').' '.$this->getState('list.direction', 'ASC'));
      }

      of course you will need to add &sort=rating in the URL link that you want to show posts by rating.

      Hope that helps.

      Delete
  2. Nice! Thanks, actually I was thinking something in that knida way :) Work's fine ;)

    ReplyDelete
  3. I'm having some trouble getting this to work. Could you post the code you used for this:

    "Note: You may also want to add a column in the front end to show the ratings. If you want and your are in list view of category you can go to the following file:
    'components/com_content/views/category/tmpl/default_articles.php'
    and look for a 'foreach' loop at approximately the mid of the file, add your column of rating along with hits, author columns."

    ReplyDelete
    Replies
    1. Sure. I replaced the hits section with rating using the following code. Let me know if you are still confused.


      params->get('list_show_hits', 1)) : ?>

      rating==NULL)
      echo '-';
      else
      echo $article->rating . '/5';

      Delete
    2. Its not letting me paste the code correctly, see if you can get file from here:

      http://dl.dropbox.com/u/57337019/default_articles.php

      Delete
  4. worked like a charm ... thanks a million :)

    Question: Any idea how to change the rating variable validation from ip address to user accounts?

    My issue is that a user (a) can log in, rate an article however if user (b) logs in on the same machine they can't rate the same article.

    ReplyDelete
  5. So this code caused a more pressing issue with my site. Its sorts all articles on all my pages by rating and it overrides all other sorting options.

    My client is a number of medical clinics and they would like to post articles listing them from newest first. On another page there is a category listing of older articles that I wanted to have the option to sort by rating among by date, author etc. Now the rating sorting works but it overrides all article sorting on all pages of the website.

    thank you in advance for your time :)

    ReplyDelete
  6. Nevermind I figured out a fix.

    I reverted the $query->order('rating DESC '); back to
    $query->order($this->getState('list.ordering', 'a.ordering').' '.$this->getState('list.direction', 'ASC'));

    in the articles.php file and I changed

    echo JHtml::_('grid.sort', 'Rating', 'a.hits', $listDirn, $listOrder); ?>


    in the default_articles.php file. Seems to have done the trick.

    ReplyDelete
  7. I am still curious about the rating in joomla based on an ip address. If a user (a) logs in on machine (1) and rates an article, its prohibited for user (b) rate the same article while logged in on machine (1).

    Any thoughts to get around this?

    ReplyDelete
  8. I haven't looked at the exact code but yes you are right that Joomla works on ip and not user and I have not found any plugin/module that does that way so apparently its a big change to make.

    ReplyDelete
  9. Something interesting about this rating system; Joomla validates against the last ip address.

    So user(a) logs in with machine(1) rates an article, then user(b) logs in with machine(2) (a different ip) rates same article. Any user on machine(1) can rate the article again

    ReplyDelete
    Replies
    1. Yea I noticed that as well. I have 2 internet connections and I could vote hundred times by just keep swaping the connections and not even changing the user

      Delete
  10. Hey, this was really useful and was exactly what i needed, thanks

    i was just wondering if there was a way to edit the link of a category blog (j 2.5.6) to add the &sort=rating function, as it is not editable in the back end and, external link creates a category list

    Ta in advance

    ReplyDelete
  11. Hi Usman Zaheer

    The articles are getting sorted by number of times they are voted. But i want to display articles in category blog layout by rating like 5 star rated at the top, 4 star next and so on. Please let me know how do it.

    ReplyDelete