You are here

function fivestar_static in Fivestar 5

Same name and namespace in other branches
  1. 6.2 fivestar.module \fivestar_static()
  2. 6 fivestar.module \fivestar_static()
1 call to fivestar_static()
fivestar_nodeapi in ./fivestar.module
Implementation of hook_nodeapi().

File

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

Code

function fivestar_static($content_type, $content_id, $tag = 'vote', $node_type = NULL) {
  global $user;
  $current_avg = votingapi_get_voting_result($content_type, $content_id, 'percent', $tag, 'average');
  $current_count = votingapi_get_voting_result($content_type, $content_id, 'percent', $tag, 'count');
  $user_vote = $user->uid ? votingapi_get_vote($content_type, $content_id, 'percent', $tag, $user->uid) : 0;
  if ($content_type == 'node') {

    // Content type should always be passed to avoid this node load.
    if (!isset($node_type)) {
      $node = node_load($content_id);
      $node_type = $node->type;
    }
    $star_display = variable_get('fivestar_style_' . $node_type, 'average');
    $text_display = variable_get('fivestar_text_' . $node_type, 'dual');
    $title_display = variable_get('fivestar_title_' . $node_type, 1);
    $stars = variable_get('fivestar_stars_' . $node_type, 5);
    switch ($star_display) {
      case 'user':
        if (user_access('rate content')) {
          $star_value = $user_vote->value;
          $title = $title_display ? t('Your rating') : NULL;
          break;
        }

      // Fall through to average if not allowed to rate.
      case 'smart':
        if (user_access('rate content')) {
          $star_value = $user_vote->value ? $user_vote->value : $current_avg->value;
          $title = $title_display ? $user_vote->value ? t('Your rating') : t('Average') : NULL;
          break;
        }

      // Fall through to average if not allowed to rate.
      case 'average':
      case 'dual':
        $star_value = $current_avg->value;
        $title = $title_display ? t('Average') : NULL;
        break;
    }

    // Set all text values, then unset the unnecessary ones.
    $user_value = $user_vote->value;
    $average_value = $current_avg->value;
    $count_value = $current_count->value;
    switch ($text_display) {
      case 'average':
        $user_value = NULL;
        break;
      case 'user':
        $average_value = NULL;
        break;
      case 'smart':
        if ($user_vote->value) {
          $average_value = NULL;
        }
        else {
          $user_value = NULL;
        }
        break;
    }
  }
  else {
    $stars = 5;
    $star_value = $current_avg->value;
    $user_value = $user_vote->value;
    $average_value = $current_avg->value;
    $count_value = $current_count->value;
  }
  $star_display = theme('fivestar_static', $star_value, $stars);
  $text_display = $text_display == 'none' ? NULL : theme('fivestar_summary', $user_value, $average_value, $count_value, $stars, FALSE);
  return theme('fivestar_static_element', $star_display, $title, $text_display);
}