I spent two hours on this but couldn't find helpful information, so opening this issue. Where can I find some information about implementing own calculating algorithms?

Thanks.

Comments

eaton’s picture

There's some example code in the API.txt document:

The following function adds a standard_deviation result to the calculated result data. Note that in previous versions of VotingAPI, this function received in-memory copies of each and every cast vote to avoid the need for custom SQL. This turned out to be very, very, very inefficient -- the slowdown of possibly running multiple aggregate queries is far outweighed by the memory savings of each module handling its own queries. After all, MySQL calculates standard deviation far faster than you can in PHP.

function mymodule_votingapi_results_alter(&$results, $content_type, $content_id) {
  // We're using a MySQLism (STDDEV isn't ANSI SQL), but it's OK because this is
  // an example. And no one would ever base real code on sample code. Ever. Never.
  $sql  = "SELECT v.tag, STDDEV(v.value) as standard_deviation ";
  $sql .= "FROM {votingapi_vote} v ";
  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'percent' ";
  $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']]['percent']['standard_deviation'] = $result['standard_deviation'];
  }
}
eaton’s picture

Status: Active » Closed (fixed)
Equ’s picture

Deleted.

Equ’s picture

Status: Closed (fixed) » Active

Sorry for opening this issue again, but I don't get how to use my custom algorithm. I'm not a coder, I just started dealing with php and this example doesn't really say much to me.
I know that I have to make a new module, ok that's easy. But what code should I use and where can I apply my algorithm...?

Equ’s picture

Component: Documentation » Code

Anyone?

eaton’s picture

Equ, to implement a custom voting algorithm you'll need to make a custom module and enable it. That module will need to implement a function called yourmodulename_votingapi_results_alter() -- basically, a customized version of the function that's outlined above.

Every time someone casts a vote, that function will be triggered, giving you a chance to look at all the votes that have been cast and tally up your own custom totals.

tommytom’s picture

so this line in the sample code above

$cache[$result['tag']]['percent']['standard_deviation'] = $result['standard_deviation'];

creates a single row in table votingapi_cache with function='standard_deviation' ... , right ?

And I have one more question. Why is the "while" loop needed ?
( in while ($result = db_fetch_array($results)) { )

will this not work (wiithout the 'while') : $result = db_fetch_array($results) ?

Equ’s picture

Status: Active » Fixed
eaton’s picture

Durr. Replied to the *first* comment in the thread rather than the last. Whoops!

Equ’s picture

Yeah, it took me a while to understand how it works =)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.