I have installed the rate module, and found out that the votes associated to a node are stored in the votingapi_vote table.
Is there a function to use to get the sum of all the votes for a node?
-
The answer is here: drupal.stackexchange.com/questions/125089/…– user14666Jul 29, 2014 at 19:47
4 Answers
As you have installed the Rate module, then you can use the function rate_get_results(), which can be called as rate_get_results('node', $nid, $widget_id)
; $nid
is the node ID of the node for which you want to get the result, and $widget_id
is the identifier of the widget used to cast the vote.
The result is an array that contains many values (some of them are present only for a particular widget); in particular $output['rating']
is the rating, and $output['count']
is the number of the votes.
If the widget saves the sum of the votes, then $output['rating']
(where $output
contains the result returned from rate_get_results('node', $nid, $widget_id)
) contains the sum of the votes, which is what you are looking for.
-
-
@kiamlaluno where would one place this code?– user12342Apr 4, 2013 at 1:36
-
-
1You can get the widgets_id in "/admin/structure/rate", is an int, if you edit one you can see it in the url "/admin/structure/rate/WIDGET_ID/edit"– alzzJul 17, 2014 at 10:40
Just use _votingapi_get_standard_results()!
-
3To notice that the function name starts with an underscore. Such functions should not be normally called, as future versions of the module could not contain the function you are using, or could rename such functions. Read this as "use at your own risk."– apaderno ♦Apr 18, 2011 at 23:56
First create the followings relationships:
Content: Vote results -- Value type: No filtering -- Aggregation function: Average vote.
Second, create the same relationship again changing the Aggregate function to: Number of votes and the identifier to: Vote result (rank.)
Third, add the following Sort criterion: Vote results: Value (desc.)
Last, add another sort criterion identical to the one you just created except that you will select the second relationship (Vote result (rank.) The sort order is also descending.
That will give you the desired results: The nodes are now properly ranked.
You can test this by enabling Text to display under the stars: Both user's and average vote in the rating field.
With Drupal 7, you can get vote information programatically using the function votingapi_select_results().
Ex:
$criteria = array();
$criteria["entity_id"] = $nid;
$criteria["entity_type"] = "node";
$criteria["value_type"] = "points";
$criteria["tag"] = "hopper";
//$criteria["function"] = "count"; // don't set, and get an array of all.
$res = votingapi_select_results($criteria);
If you then examine (ex: dpm($res)) you can see that $res is an array of all the information you will need for each "function" of your voting widget.
To figure out which "value_type" and "tag" to use, look at the votingapi_cache table.