I posed a similar question over in the Fivestar issue queue, but after some more work I am guessing that I may need to work through the Voting API rather than through Fivestar to accomplish this.

I would like to create a list of items that are 'trending'. By 'trending' I mean nodes (in my case, Watches) that have been highly rated within the last several days. Right now I have a block that displays the 4 highest rated Watches, and would like to create a similar block that displays the 4 highest rated 'trending' Watches, where I only use votes that have been cast within the last 3 days.

For the trending block (created using Views), I have the following filter criteria (the content type for a watch is Watch Detail):

  • Content: Published (Yes)
  • Content: Type (= Watch Detail)
  • (Vote results) Vote results: Timestamp (>= -3 days)

The watches are sorted by highest rated using the following sort critera:

  • (Vote results) Vote results: Value (desc)

It's a fairly standard view, and I used the Relationship Content: Vote results to access the voting results for sorting and filtering.

The problem with what I've done is that while it is filtering by watches that have received votes within the last 3 days, it is not calculating the average value of the rating on votes that have been cast in the last 3 days. It is using the rating of the watch since the watch node was created.

Initial thoughts: it may be that I need to use something like the hook_votingapi_views_formatters or votingapi_votingapi_storage_select_votes but beyond a basic idea that this might be useful in terms of allowing me to manipulate the information stored in the votingapi_vote table (which contains the timestamp associated with each vote) within the context of Views, I'm a bit at a loss.

I did hack out a sql query that pulls the correct data from the votingapi_vote table as well:

select v.entity_id, avg(v.value) as value from votingapi_vote v 
WHERE v.timestamp > '1350960000'
 group by v.entity_id
 order by v.value desc;

The timestamp above is just a recent timestamp to confirm that this query is valid and does display and order the watches by highest rated when only looking at votes cast since 1350960000 to calculate the average rating.

If anyone has done something like this I would very much appreciate some tips for how to limit the calculation of the average vote value to only votes cast within the last 3 days.

See http://ratemytimepiece.com/ for the exact website I'm working on if that helps you visualize this a little better.

Comments

torotil’s picture

Hi,

The VotingAPI stores two things: the actual votes (in votingapi_vote) and a few precalculated overall values called resultes (in votingapi_cache). If you use the Vote Result relation in views you always get the overall average.

You need to join with the actual votes for this node and do a on-the-fly aggregation with views (enable in the "advanced" section of the views interface). Depending on the number of votes and nodes being voted on this may be a costly operation, so you'd perhaps want to cache that block.

pifagor’s picture

Status: Active » Closed (outdated)