Showing posts with label show rating. Show all posts
Showing posts with label show rating. Show all posts

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.