<?php

/*
 * Implements hook_vote_query_build_QUERY_ID()
 */
function vote_default_vote_query_build_default($item) {
  return db_select('vote', 'v')
    ->condition('v.pool', $item->pool)
    ->condition('v.entity_type', $item->entity_type)
    ->condition('v.entity_id', $item->entity_id);
}

/*
 * Implements hook_vote_query_build_QUERY_ID()
 */
function vote_default_vote_query_build_grouped_value($item) {
  return vote_default_vote_query_build_default($item)
    ->fields('v', array('value'))
    ->groupBy('v.value');
}

/*
 * Implements hook_vote_query_build_query_finalize()
 */
function vote_default_vote_query_finalize($query) {
  return $query->execute()->fetchAll();
}

/*
 * Implements hook_vote_function_query_build_QUERY_ID()
 */
function vote_default_vote_function_query_build_default($query, $function, $pool) {
  switch ($function) {
    case 'count': 
       $query->addExpression('COUNT(*)', 'count');
       break;
    case 'average':
      $query->addExpression('ROUND(AVG(v.value))', 'average');
      break;
    case 'sum':
      $query->addExpression('SUM(v.value)', 'sum');
      break;
  }
  
  return $query;
}

/*
 * Implements hook_vote_function_query_build_QUERY_ID()
 */
function vote_default_vote_function_query_build_grouped_value($query, $function, $pool) {
  switch ($function) {
    case 'score': 
       $query->addExpression('COUNT(*)', 'score');
       break;
  }
  
  return $query;
}

/*
 * Implements hook_vote_save()
 */
function vote_default_vote_save($vote, $operation) {
  switch ($operation) {
    case 'insert':
      drupal_write_record('vote', $vote);
      break;
    case 'update':
      drupal_write_record('vote', $vote, 'vid');
      break;
  }
    
  return $vote;
}

/*
 * Implements hook_vote_pool_save()
 */
function vote_default_vote_pool_save($pool, $old) {
  db_update('vote')
    ->fields(array('pool' => $vote->pool))
    ->condition('pool', $old->pool)
    ->execute();    
}

/*
 * Implements hook_vote_pool_delete()
 */
function vote_default_vote_pool_delete($name) {
  db_delete('vote')->condition('pool', $name)->execute();   
}

/*
 * Implements hook_vote_has_data()
 */
function vote_default_vote_has_data() {
  $query = db_select('vote', 'v')
    ->range(0, 1);
  
  $query->addExpression('COUNT(*)', 'count');
  
  return (bool) $query
    ->execute()
    ->fetchField();  
}

/*
 * Implements hook_vote_pool_has_data()
 */
function vote_default_vote_pool_has_data($pool) {
  $query = db_select('vote', 'v')
    ->condition('pool', $pool)
    ->range(0, 1);
  
  $query->addExpression('COUNT(*)', 'count');
  
  return (bool) $query
    ->execute()
    ->fetchField();
}

/*
 * Implements hook_vote_schedule_all()
 */
function vote_default_vote_schedule_all() {
  return db_select('vote', 'v')
    ->fields('v', array('pool', 'entity_type', 'entity_id'))
    ->groupBy('v.pool')
    ->groupBy('v.entity_type')
    ->groupBy('v.entity_id')
    ->execute()->fetchAll();
}

/*
 * Implements hook_vote_schedule_pool()
 */
function vote_default_vote_schedule_pool($name) {
  return db_select('vote', 'v')
    ->fields('v', array('pool', 'entity_type', 'entity_id'))
    ->condition('pool', $name)
    ->groupBy('v.pool')
    ->groupBy('v.entity_type')
    ->groupBy('v.entity_id')
    ->execute()->fetchAll();  
}