You are here

function fivestar_form in Fivestar 5

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

Create the fivestar form for the current item. Note that this is not an implementation of hook_form(). We should probably change the function to reflect that.

2 string references to 'fivestar_form'
fivestar_custom_widget in ./fivestar.module
fivestar_forms in ./fivestar.module
Implementation of hook_forms().

File

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

Code

function fivestar_form($content_type, $content_id) {
  global $user;
  if ($content_type == 'node') {
    if (is_numeric($content_id)) {
      $node = node_load($content_id);
    }
    else {
      return array();
    }
  }
  $current_avg = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'average');
  $current_count = votingapi_get_voting_result($content_type, $content_id, 'percent', 'vote', 'count');
  if ($user->uid) {
    $user_vote = votingapi_get_vote($content_type, $content_id, 'percent', 'vote', $user->uid);
  }
  else {

    // If the user is anonymous, we never bother loading their existing votes.
    // Not only would it be hit-or-miss, it would break page caching. Safer to always
    // show the 'fresh' version to anon users.
    $user_vote->value = 0;
  }
  $values = array(
    'average' => (int) $current_avg->value,
    'user' => (int) $user_vote->value,
    'count' => (int) $current_count->value,
  );
  $settings = array(
    'stars' => variable_get('fivestar_stars_' . $node->type, 5),
    'allow_clear' => variable_get('fivestar_unvote_' . $node->type, FALSE),
    'style' => variable_get('fivestar_style_' . $node->type, 'average'),
    'text' => variable_get('fivestar_text_' . $node->type, 'dual'),
    'content_type' => $content_type,
    'content_id' => $content_id,
    'tag' => 'vote',
    'autosubmit' => TRUE,
    'title' => variable_get('fivestar_title_' . $node->type, 1) ? NULL : FALSE,
    'feedback_enable' => variable_get('fivestar_feedback_' . $node->type, 1),
    'labels_enable' => variable_get('fivestar_labels_enable_' . $node->type, 1),
    'labels' => variable_get('fivestar_labels_' . $node->type, array()),
  );
  return fivestar_custom_widget($values, $settings);
}