You are here

function fivestar_vote in Fivestar 5

Same name and namespace in other branches
  1. 6.2 fivestar.module \fivestar_vote()
  2. 6 fivestar.module \fivestar_vote()

Callback function for fivestar/vote.

Parameters

type: A content-type to log the vote to. 'node' is the most common.

cid: A content id to log the vote to. This would be a node ID, a comment ID, etc.

tag: Multi-axis tag to allow multiple votes per node. 'vote' is the most common.

value: A value from 1-100, representing the vote cast for the content.

Return value

An XML chunk containing the results of the vote, for use by the client-side javascript code.

1 string reference to 'fivestar_vote'
fivestar_menu in ./fivestar.module
Implementation of hook_menu().

File

./fivestar.module, line 644
A simple n-star voting widget, usable in other forms.

Code

function fivestar_vote($type, $cid, $tag, $value) {
  drupal_set_header("Content-Type: text/xml");
  $output = '';
  $output .= '<?xml version="1.0" encoding="UTF-8"?>';

  // Rebuild the #auto_submit_path that was used as the token seed.
  $path = preg_replace('/\\/' . $value . '$/', '', $_GET['q']);
  if (!isset($_GET['token']) || !fivestar_check_token($_GET['token'], $path)) {
    $output .= '<xml><error>' . t('Invalid token') . '</error></xml>';
    exit($output);
  }
  _fivestar_cast_vote($type, $cid, $value, $tag);
  $result = votingapi_recalculate_results($type, $cid);

  // Retrieve cached result if recalculation is not set to immediate.
  if (!is_array($result)) {
    $result = votingapi_get_voting_results($type, $cid);
  }
  if ($type == 'node') {
    $node = node_load($cid);
  }
  $stars = variable_get('fivestar_stars_' . (!isset($node) ? 'default' : $node->type), 5);
  $feedback_enable = variable_get('fivestar_feedback_' . (!isset($node) ? 'default' : $node->type), 1);
  $output .= '<xml><result>';
  if (count($result)) {
    foreach ($result as $data) {
      if ($data->tag == $tag) {
        $output .= '<' . $data->function . '>' . $data->value . '</' . $data->function . '>';
        $summary[$data->tag][$data->function] = $data->value;
      }
    }
  }
  $output .= '<summary>';
  $output .= '<average><![CDATA[' . theme('fivestar_summary', NULL, $summary[$tag]['average'], NULL, $stars, $feedback_enable) . ']]></average>';
  $output .= '<average_count><![CDATA[' . theme('fivestar_summary', NULL, $summary[$tag]['average'], $summary[$tag]['count'], $stars, $feedback_enable) . ']]></average_count>';
  $output .= '<user><![CDATA[' . theme('fivestar_summary', $value, NULL, NULL, $stars, $feedback_enable) . ']]></user>';
  $output .= '<user_count><![CDATA[' . theme('fivestar_summary', $value, NULL, $summary[$tag]['count'], $stars, $feedback_enable) . ']]></user_count>';
  $output .= '<combo><![CDATA[' . theme('fivestar_summary', $value, $summary[$tag]['average'], $summary[$tag]['count'], $stars, $feedback_enable) . ']]></combo>';
  $output .= '<count><![CDATA[' . theme('fivestar_summary', NULL, NULL, $summary[$tag]['count'], $stars, $feedback_enable) . ']]></count>';
  $output .= '</summary>';
  $output .= '</result>';
  $output .= '<vote>';
  $output .= '<value>' . $value . '</value>';
  $output .= '<type>' . $type . '</type>';
  $output .= '<id>' . $cid . '</id>';
  $output .= '<tag>' . $tag . '</tag>';
  $output .= '</vote></xml>';
  drupal_set_header("Content-Type: text/xml");
  exit($output);
}