You are here

voting_service.module in MediaFront 6.2

Same filename and directory in other branches
  1. 6 services/voting_service/voting_service.module

File

services/voting_service/voting_service.module
View source
<?php

// Drupal Voting Service
// Written by Travis Tidwell @ www.travistidwell.com
function voting_service_perm() {
  return array(
    'allow remote voting',
  );
}

/**
 * Implementation of hook_help().
 */
function voting_service_help($section = 'admin/help#services_voting') {
  switch ($section) {
    case 'admin/settings/modules#description':
      return t('Provides voting methods to services applications. Requires services.module.');
  }
}

/**
 * Implementation of hook_service()
 */
function voting_service_service() {
  return array(
    // vote.get
    array(
      '#method' => 'vote.getVote',
      '#callback' => 'voting_service_get_vote',
      '#access arguments' => array(
        'access content',
      ),
      '#key' => FALSE,
      '#args' => array(
        array(
          '#name' => 'content_type',
          '#type' => 'string',
          '#description' => t('The type of content which you are voting for.'),
        ),
        array(
          '#name' => 'content_id',
          '#type' => 'int',
          '#description' => t('The ID of the content which you are voting for.'),
        ),
        array(
          '#name' => 'tag',
          '#type' => 'string',
          '#description' => t('The category of the vote within the content type.'),
          '#optional' => TRUE,
        ),
      ),
      '#return' => 'array',
      '#help' => t('Returns a vote.'),
    ),
    array(
      '#method' => 'vote.getUserVote',
      '#callback' => 'voting_service_get_user_vote',
      '#access arguments' => array(
        'access content',
      ),
      '#key' => FALSE,
      '#args' => array(
        array(
          '#name' => 'content_type',
          '#type' => 'string',
          '#description' => t('The type of content which you are voting for.'),
        ),
        array(
          '#name' => 'content_id',
          '#type' => 'int',
          '#description' => t('The ID of the content which you are voting for.'),
        ),
        array(
          '#name' => 'tag',
          '#type' => 'string',
          '#description' => t('The category of the vote within the content type.'),
          '#optional' => TRUE,
        ),
      ),
      '#return' => 'array',
      '#help' => t('Returns a vote.'),
    ),
    // vote.set
    array(
      '#method' => 'vote.setVote',
      '#callback' => 'voting_service_set_vote',
      '#access arguments' => array(
        'allow remote voting',
      ),
      '#args' => array(
        array(
          '#name' => 'content_type',
          '#type' => 'string',
          '#description' => t('The type of content which you are voting for.'),
        ),
        array(
          '#name' => 'content_id',
          '#type' => 'int',
          '#description' => t('The ID of the content which you are voting for.'),
        ),
        array(
          '#name' => 'vote_value',
          '#type' => 'int',
          '#description' => t('The value of the vote.'),
        ),
        array(
          '#name' => 'tag',
          '#type' => 'string',
          '#description' => t('The category of the vote within the content type.'),
          '#optional' => TRUE,
        ),
      ),
      '#return' => 'array',
      '#help' => t('Submit a new vote.'),
    ),
    // vote.delete
    array(
      '#method' => 'vote.deleteVote',
      '#callback' => 'voting_service_delete_vote',
      '#access arguments' => array(
        'allow remote voting',
      ),
      '#args' => array(
        array(
          '#name' => 'content_type',
          '#type' => 'string',
          '#description' => t('The type of content which you are deleting.'),
        ),
        array(
          '#name' => 'content_id',
          '#type' => 'int',
          '#description' => t('The ID of the content which you are deleting.'),
        ),
        array(
          '#name' => 'tag',
          '#type' => 'string',
          '#description' => t('The category of the vote within the content type.'),
          '#optional' => TRUE,
        ),
      ),
      '#return' => 'array',
      '#help' => t('Delete a vote.'),
    ),
  );
}
function voting_service_get_user_vote($content_type, $content_id, $tag = "vote") {
  global $user;
  if ($user->uid) {
    $criteria['content_type'] = $content_type;
    $criteria['content_id'] = $content_id;
    $criteria['tag'] = $tag;
    $criteria['value_type'] = 'percent';
    $criteria['uid'] = $user->uid;
    $votes = votingapi_select_votes($criteria);
  }
  if ($votes) {
    return $votes[0];
  }
  else {
    return array(
      'type' => $content_type,
      'tag' => $tag,
      'value' => 0,
    );
  }
}

/**
 * Returns a specified node.
 */
function voting_service_get_vote($content_type, $content_id, $tag = "vote") {
  $criteria['content_type'] = $content_type;
  $criteria['content_id'] = $content_id;
  $criteria['tag'] = $tag;
  $criteria['value_type'] = 'percent';
  $criteria['function'] = 'average';
  $votes = votingapi_select_results($criteria);
  if ($votes) {
    return $votes[0];
  }
  else {
    return array(
      'type' => $content_type,
      'tag' => $tag,
      'value' => 0,
    );
  }
}
function voting_service_set_vote($content_type, $content_id, $vote_value, $tag = "vote") {
  $vote['content_type'] = $content_type;
  $vote['content_id'] = $content_id;
  $vote['value'] = $vote_value;
  $vote['tag'] = $tag;
  votingapi_set_votes($vote);
  votingapi_recalculate_results($content_type, $content_id, TRUE);
  return voting_service_get_vote($content_type, $content_id, $tag);
}
function voting_service_delete_vote($content_type, $content_id, $tag = "vote") {
  global $user;
  $criteria['content_type'] = $content_type;
  $criteria['content_type'] = $content_id;
  $criteria['tag'] = $tag;
  $criteria['uid'] = $user->uid;
  $criteria['value_type'] = 'percent';
  $votes = votingapi_select_results($criteria);
  if ($votes) {
    votingapi_delete_vote(array(
      'vote_id' => $votes[0]->vote_id,
    ));
    votingapi_recalculate_results($content_type, $content_id, TRUE);
  }
  return voting_service_get_vote($content_type, $content_id, $tag);
}

/**
 * Implementation of hook_playlist_node()
 */
function voting_service_playlist_node($node) {
  $additions = array();
  $additions['vote'] = array(
    'vote' => voting_service_get_vote('node', $node->nid),
    'uservote' => voting_service_get_user_vote('node', $node->nid),
  );
  return $additions;
}

Functions

Namesort descending Description
voting_service_delete_vote
voting_service_get_user_vote
voting_service_get_vote Returns a specified node.
voting_service_help Implementation of hook_help().
voting_service_perm
voting_service_playlist_node Implementation of hook_playlist_node()
voting_service_service Implementation of hook_service()
voting_service_set_vote