2

I'm using Joomla 2.5 and K2 2.5.7. I have a category with posts with a different number of votes. In the front end, I need to sort the items of this category by number of votes.

I re-configured standard stars rating systems to simple "Give one vote" system.

I need this for a ranking order page, so it will have items with the largest number of votes on the top.

What I have

I have my MVC template for K2 category. I was wondering, if sorting $this->leading in category.php is the right to go for.

If it is, how can I do it? With var_dump there is variable numOfVotes which carry real number of votes. How can I sort this object by this var?

Thank you very much!

George Wilson
  • 5,595
  • 5
  • 29
  • 42
CZBios
  • 57
  • 1
  • 10

1 Answers1

1

This K2 forum post seems to answer your question. You need to use the mod_content k2 module and use the "sort by" parameter and select highest rating.

If you go to 'modules/mod_k2_content/helper.php' in the ftp, you'll see on line 98, that there it says

$query .= ", (r.rating_sum/r.rating_count) AS rating";

This sorts the data by the highest rating. Now generally this would sort it by the number of votes divided by the number of people who have voted giving a result between 1 and 5. However as everyone in your case gets a vote of 5 - then your average result will always be 5 by that calculation!, I think that you'll have to replace that line with:

$query .= ", r.rating_count AS rating";

i.e. just sorting by the number of people who have voted (N.B. This assumes you're not using a vote down system as well! You haven't mentioned it so I'm assuming not)

Then you should just be able to use the module (selecting the parameter that you desire as normal)

For using the component category option etc. Then the same line of code can be found in 'components/com_k2/models/itemlist.php' on Line 39 which again would need to be edited. Then you could just use the built in parameters as usual!

George Wilson
  • 5,595
  • 5
  • 29
  • 42
  • This is not a solution, becaus i dont understand currently sort condition in option "higheest rating". Change that i made was that i replaced stars with button. Every click is highest with number 5. As i look into scripts /models/item.php line 758 and below, rating is simple math SUM. I dont know, why sort with high rating is doesnt work. --- re-configuring standard rating system is almost done with css and little template work --- – CZBios Sep 13 '12 at 20:34
  • @Marek Tesař Updated answer. Now I understand your question better - hopefully this answer will be a lot more help! – George Wilson Sep 14 '12 at 08:08
  • Thank you. Its good answer and according to SQL console of phpmyadmin this gives good results in right order. I have complete sql query but i cant paste here - its too long. Page is without change! Proposed change in mod_K2_content seems to give results in right order, but change the same things in component view in model - without change. I tried to sort an object array Items in views/itemlist/view.html.php on line after //prepare items FOR cycle (around line 345) and without luck. The component view is strange, can you advise me please, how scripts goes one after another to steps? – CZBios Sep 14 '12 at 17:34
  • @MarekTesař I'm not quite sure what you mean. How do you mean the scripts go in steps? Gonna try and answer what your asking - but if I've made a mess of it please do clarify!! Typically on loading a page the 'view' (category in the component) sends a request to the controller for the information to display. The controller then sends a request to the model for the information in this case to the function in the model getData(). In k2, the model gives the information direct to view.html.php in the k2 template (views/itemlist) on line 290. And this gets the data to display in the frontend. – George Wilson Sep 17 '12 at 08:34
  • Thanks for your help. In the end i loaded moduleposition in an K2 item a this k2 item links to menu. your edit works for module view, bud not for component view. I dont know why, but thats the main reason why i loaded a module position into k2 item. – CZBios Oct 29 '12 at 13:29