You are here

function fivestar_views_widget_handler in Fivestar 6.2

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

Generic VotingAPI Views formatter for displaying rating widget.

2 calls to fivestar_views_widget_handler()
fivestar_views_widget_compact_handler in ./fivestar.module
VotingAPI Views formatter for displaying rating widget without text.
fivestar_views_widget_normal_handler in ./fivestar.module
VotingAPI Views formatter for displaying rating widget with text.

File

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

Code

function fivestar_views_widget_handler($value, $field, $columns, $summary) {

  // If the user can't rate, use the display handler.
  if (!user_access('rate content')) {
    return fivestar_views_value_display_handler($value, $field, $columns);
  }
  if ($field->view->base_table == 'node') {

    // Find the VotingAPI tag for this field.
    foreach ($field->query->table_queue[$field->relationship]['join']->extra as $votingapi_setting) {
      if ($votingapi_setting['field'] == 'tag') {
        $tag = $votingapi_setting['value'];
      }
    }
    $content_type = 'node';
    $content_id = $columns->nid;
    $node_type = isset($columns->node_type) ? $columns->node_type : db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $columns->nid));
    $values = array(
      'user' => 0,
      'average' => 0,
      'count' => 0,
    );
    if ($field->table == 'votingapi_vote') {
      $values['user'] = $value;
    }
    if ($field->table == 'votingapi_cache') {
      $values['average'] = $value;
    }

    // Only pull in all the votes if we need to display the summary text.
    if ($summary) {
      $votes = fivestar_get_votes($content_type, $content_id, $tag);
      if ($field->table != 'votingapi_vote') {
        $values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
      }
      if ($field->table != 'votingapi_cache') {
        $values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0;
      }
      $values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0;
    }
    $settings = array(
      'content_type' => $content_type,
      'content_id' => $content_id,
      'tag' => $tag,
      'autosubmit' => TRUE,
      'title' => FALSE,
    );
    $settings += fivestar_get_settings($node_type, $tag);
    return drupal_get_form('fivestar_custom_widget', $values, $settings);
  }
  else {
    return theme('fivestar_static', $value, 5);
  }
}