I need to be able to define my own ranking calculation that would reset daily and be compared to historical levels for that day of the week over say the previous 5 weeks (inclusive of that day). Summarized, the ranking is not number of votes compared to other nodes based on total, but number of votes that day (call it Wednesday) divided by the highest number of votes on that and the previous 5 Wednesdays. Eventually, will need to normalize this data as the site grows; otherwise, increasing user base would eventually skew the calculated data upward.

I have been searching for hours and haven't run across a question like this, but would be grateful if someone could point me there or respond.

Comments

Scott Reynolds’s picture

do so in a hook_votingapi_results_alter($cache, $content_type, $content_id)

function my_module_votingapi_results_alter($cache, $content_type, $content_id) {
  if ( $content_type == 'node') {
    $node = node_load($content_id);
    // Calculate stuff based on $node
    $cache['tag_i_care_about']['points']['my_cool_votingapi_function_name'] = $some_value_here
  }
}
Starvin15’s picture

I greatly appreciate this feedback. When using the hook_votingapi_results_alter, do you know if I be able to access this custom calculation as a field to add it to a view or will I need to make another changes to the Voting API code?

Scott Reynolds’s picture

I actually did a book page for the Reddit algorithm here: http://drupal.org/node/683090

It address your votingapi views question.

Starvin15’s picture

Thanks again, this looks like a great way to add calculations to views. My only issue is when I add your hood to the code in Voting API module then I add the relationship to my view, none of my votes show up. In addition, I am unable to add in reddit as a value to be displayed. Do you know why would this be?

Scott Reynolds’s picture

That actually made 0 sense to me. could you try explaining again?

Starvin15’s picture

Apologies for the confusion. So I followed your book page on the Reddit algorithm. I've created a hook module and enabled it through Admin > Site building > Modules. I have created a custom view, which is displaying the fields that I want (including vote results). When I add in the reddit relationship and require it on Node: Vote Results all of my content disappears and the view can't find any data to display. In addition, I cannot find a field associated with the reddit calculation to add to my view.

My initial thought is that I am missing the relationships hook outlined in the API.txt. (providing the Views Relationship connection for votes being cast). In summary, the book page just describing the code for the calculation and not necessarily the code to build the relationship for views. Does this sound accurate or am I off base?

I am trying to get up to speed on the voting API so I do appreciate your patience.

Scott Reynolds’s picture

Have you cast any votes sense creating your module?

Starvin15’s picture

Correct, I have cast multiple votes on various nodes to test. In addition, as soon as I take of "require this relationship" all of my nodes show up with votes.

Doubt it is helpful, but here is the query being run:

SELECT node.nid AS nid,
node.title AS node_title,
node_data_field_rating.field_rating_rating AS node_data_field_rating_field_rating_rating,
node_data_field_rating.field_rating_target AS node_data_field_rating_field_rating_target,
node.type AS node_type,
node.vid AS node_vid,
node_data_field_rating.field_cons_value AS node_data_field_rating_field_cons_value,
node_data_field_rating.field_pros_value AS node_data_field_rating_field_pros_value,
node_data_field_rating.field_ratingstotal_rating AS node_data_field_rating_field_ratingstotal_rating,
node_data_field_rating.field_ratingstotal_target AS node_data_field_rating_field_ratingstotal_target
FROM node node
INNER JOIN votingapi_cache votingapi_cache_node_points_vote_reddit ON node.nid = votingapi_cache_node_points_vote_reddit.content_id AND (votingapi_cache_node_points_vote_reddit.content_type = 'node' AND votingapi_cache_node_points_vote_reddit.value_type = 'points' AND votingapi_cache_node_points_vote_reddit.tag = 'vote' AND votingapi_cache_node_points_vote_reddit.function = 'reddit')
LEFT JOIN content_type_review node_data_field_rating ON node.vid = node_data_field_rating.vid
WHERE node.status <> 0
ORDER BY node_data_field_rating_field_ratingstotal_rating ASC

Scott Reynolds’s picture

my guess would be that you arn't doing a 'points' calculation. Are you using Fivestar? That would be percent.

My next guess would be that the 'tag' isn't 'vote' but something else

Edit: The reddit function will only work with points voting style for obvious reasons.

Starvin15’s picture

I have switched to the vote up/down module which I believe is based off points. I have now activated the module and required the relationship. My data is now showing up which is great so it appears the reddit module is working correctly, however, I cannot add it to the fields to show up as data. I believe the calculation is working correctly so this isn't so much of an issue - but it would be helpful to pull this data into a view but not display it. If I add a relationship hook would that allow me to add the reddit result as a field?

Scott Reynolds’s picture

you already have it, just use the Vote results: Value and use your relationship you already set up.

Starvin15’s picture

You seems to have a pretty good understanding of the voting API module. I am trying to create a custom calc to only count those votes from today. I created the following which is giving me a parse error. Could you take a quick look and let me know what my problem is:

/**
* @file
* Custom functions for this site.
* Implementation of hook_votingapi_results_alter().
*/
function mymodule_votingapi_results_alter(&$results, $content_type, $content_id) {
// Calc for generating the votes for that day.
$sql = "SELECT v.tag, SUM(v.value) AS todays_votes ";
$sql .= "FROM {votingapi_vote} v ";
$sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'points' AND v.timestamp > DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(NOW(),'%m')-DATE_FORMAT(NOW(),'%d') 00:00:00 ";
$sql .= "GROUP BY v.tag";

$results = db_query($sql, $content_type, $content_id);

// VotingAPI wants the data in the following format:
// $cache[$tag][$value_type][$math_function] = $value;
while ($result = db_fetch_array($results)) {
$cache[$result['tag']]['points']['todays_votes'] = $result['todays_votes'];
}
}

legolasbo’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Drupal 6 is no longer supported. Closing old issues to clean up the issue queue.

Please reopen and update this issue if this is still an issue in the D7 or D8 version.