You are here

function VotingApi_VoteStorage::standardResults in Voting API 7.3

Builds the default VotingAPI results for the three supported voting styles.

File

./votingapi.module, line 307
A generalized voting API for Drupal.

Class

VotingApi_VoteStorage

Code

function standardResults($entity_type, $entity_id) {
  $cache = array();
  $sql = "SELECT v.value_type, v.tag, ";
  $sql .= "COUNT(v.value) as value_count, SUM(v.value) as value_sum  ";
  $sql .= "FROM {votingapi_vote} v ";
  $sql .= "WHERE v.entity_type = :type AND v.entity_id = :id AND v.value_type IN ('points', 'percent') ";
  $sql .= "GROUP BY v.value_type, v.tag";
  $results = db_query($sql, array(
    ':type' => $entity_type,
    ':id' => $entity_id,
  ));
  foreach ($results as $result) {
    $cache[$result->tag][$result->value_type]['count'] = $result->value_count;
    $cache[$result->tag][$result->value_type]['average'] = $result->value_sum / $result->value_count;
    if ($result->value_type == 'points') {
      $cache[$result->tag][$result->value_type]['sum'] = $result->value_sum;
    }
  }
  $sql = "SELECT v.tag, v.value, v.value_type, COUNT(1) AS score ";
  $sql .= "FROM {votingapi_vote} v ";
  $sql .= "WHERE v.entity_type = :type AND v.entity_id = :id AND v.value_type = 'option' ";
  $sql .= "GROUP BY v.value, v.tag, v.value_type";
  $results = db_query($sql, array(
    ':type' => $entity_type,
    ':id' => $entity_id,
  ));
  foreach ($results as $result) {
    $cache[$result->tag][$result->value_type]['option-' . $result->value] = $result->score;
  }
  return $cache;
}