You are here

function fivestar_form in Fivestar 6

Same name and namespace in other branches
  1. 5 fivestar.module \fivestar_form()
  2. 6.2 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.

1 string reference to 'fivestar_form'
fivestar_forms in ./fivestar.module
Implementation of hook_forms().

File

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

Code

function fivestar_form(&$form_state, $content_type, $content_id) {
  global $user;
  if ($content_type == 'node') {
    if (is_numeric($content_id)) {
      $node = node_load($content_id);
    }
    else {
      return array();
    }
  }
  $star_display = variable_get('fivestar_style_' . $node->type, 'average');
  $text_display = variable_get('fivestar_text_' . $node->type, 'dual');
  if ($star_display == 'average' && ($text_display == 'average' || $text_display == 'none')) {

    // Save a query and don't retrieve the user vote unnecessarily.
    $votes = fivestar_get_votes($content_type, $content_id, 'vote', 0);
  }
  else {
    $votes = fivestar_get_votes($content_type, $content_id);
  }
  $values = array(
    'user' => isset($votes['user']['value']) ? $votes['user']['value'] : 0,
    'average' => isset($votes['average']['value']) ? $votes['average']['value'] : 0,
    'count' => isset($votes['count']['value']) ? $votes['count']['value'] : 0,
  );
  $settings = array(
    'stars' => variable_get('fivestar_stars_' . $node->type, 5),
    'allow_clear' => variable_get('fivestar_unvote_' . $node->type, FALSE),
    'style' => $star_display,
    'text' => $text_display,
    '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($form_state, $values, $settings);
}