<?php

/*
 * Implements hook_vote_definitions()
 */
function hook_vote_type_definitions() {
  return array(
    'points' => array(
      'title' => t('Points'),
      'description' => t('Allows point-based votes with an individual set of allowed values.'),
      'handler' => 'vote_pool_points',
    ),
    'percent' => array(
      'title' => t('Percent'),
      'description' => t('Allows percentual votes (1-100).'),
      'handler' => 'vote_pool_percent',
    ),
  );
}

/*
 * Implements hook_vote_function_definitions()
 */
function hook_vote_function_definitions() {
  return array(
    'count' => array(
      'title' => t('Count'),
      'description' => t('Counts all votes on an entity.'),
      'handler' => 'vote_function_count',
      'vote types' => array('points', 'percent'),
      'table' => 'vote_result',
    ),
    'average' => array(
      'title' => t('Average'),
      'description' => t('Calculates the average vote value on an entity.'),
      'handler' => 'vote_function_average',
      'vote types' => array('points', 'percent'),
      'table' => 'vote_result',
    ),
    'sum' => array(
      'title' => t('Sum'),
      'description' => t('Calculates the sum of all votes on an entity.'),
      'handler' => 'vote_function_sum',
      'vote types' => array('points'),
      'table' => 'vote_result',
    ),
  );
}

/*
 * Implements hook_vote_storage_methods()
 */
function hook_default_vote_storage_methods() {
  return array(
  	'default' => array(
      'label' => t('Default'),
      'description' => t('Provides the default storage method.'),
    ),
  );
}

/*
 * Implements hook_vote_query_build_QUERY_ID()
 */
function hook_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 hook_vote_query_build_grouped_value($item) {
  return hook_vote_query_build_default($item)
    ->fields('v', array('value'))
    ->groupBy('v.value');
}

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

/*
 * Implements hook_vote_function_query_build_QUERY_ID()
 */
function hook_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 hook_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 hook_vote_save($vote, $operation) {
  if (empty($vote->vid)) {
    drupal_write_record('vote', $vote);
  } 
  else {
    drupal_write_record('vote', $vote, 'vid');
  }
    
  return $vote;
}

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

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

/*
 * Implements hook_vote_has_data()
 */
function hook_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 hook_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 hook_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 hook_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();  
}