You are here

class VotingApi_VoteStorage in Voting API 7.3

Hierarchy

Expanded class hierarchy of VotingApi_VoteStorage

1 string reference to 'VotingApi_VoteStorage'
VotingApi_VoteStorage::get in ./votingapi.module

File

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

View source
class VotingApi_VoteStorage {
  public static $instance = NULL;
  public static function get() {
    if (!self::$instance) {
      $class = variable_get('votingapi_vote_storage', 'VotingApi_VoteStorage');
      self::$instance = new $class();
    }
    return self::$instance;
  }
  public function addVote(&$vote) {
    drupal_write_record('votingapi_vote', $vote);
  }
  public function deleteVotes($votes, $vids) {
    db_delete('votingapi_vote')
      ->condition('vote_id', $vids, 'IN')
      ->execute();
  }
  public function selectVotes($criteria, $limit) {
    $query = db_select('votingapi_vote')
      ->fields('votingapi_vote');
    foreach ($criteria as $key => $value) {
      if ($key == 'timestamp') {
        $query
          ->condition($key, $value, '>');
      }
      else {
        $query
          ->condition($key, $value, is_array($value) ? 'IN' : '=');
      }
    }
    if (!empty($limit)) {
      $query
        ->range(0, $limit);
    }
    $result = $query
      ->execute();
    $result->fetchOptions['class_name'] = 'VotingApi_Vote';
    return $result
      ->fetchAll(PDO::FETCH_CLASS);
  }

  /**
   * Builds the default VotingAPI results for the three supported voting styles.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VotingApi_VoteStorage::$instance public static property
VotingApi_VoteStorage::addVote public function
VotingApi_VoteStorage::deleteVotes public function
VotingApi_VoteStorage::get public static function
VotingApi_VoteStorage::selectVotes public function
VotingApi_VoteStorage::standardResults function Builds the default VotingAPI results for the three supported voting styles.