You are here

function fivestar_static in Fivestar 6.2

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

Retreive and print out a static display of stars for a piece of content.

Parameters

$content_type: The type of content that will have its vote retreived. i.e. "node".

$content_id: The ID of the content that will have its vote retreived.

$node_type: Optional. If retreiving a node's rating, passing in the node type will prevent Fivestar from doing an additional query to find it.

$tag: Optional. The voting tag that will be retreived. Defaults to "vote" if none is specified.

1 call to fivestar_static()
fivestar_nodeapi in ./fivestar.module
Implementation of hook_nodeapi().

File

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

Code

function fivestar_static($content_type, $content_id, $node_type = NULL, $tag = 'vote') {
  global $user;
  $criteria = array(
    'content_type' => $content_type,
    'content_id' => $content_id,
    'value_type' => 'percent',
    'tag' => 'vote',
  );
  $votes = fivestar_get_votes($content_type, $content_id, $tag);
  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;
    }
    $settings = fivestar_get_settings($node_type, $tag);
    $stars = $settings['stars'];
    switch ($settings['star_display']) {
      case 'average':
      case 'dual':
        $star_value = $votes['average']['value'];
        $title = $settings['title_display'] ? t('Average') : NULL;
        break;
      case 'user':
        $star_value = $votes['user']['value'];
        $title = $settings['title_display'] ? t('Your rating') : NULL;
        break;
      case 'smart':
        $star_value = $votes['user']['value'] ? $votes['user']['value'] : $votes['average']['value'];
        $title = $settings['title_display'] ? $votes['user']['value'] ? t('Your rating') : t('Average') : NULL;
        break;
    }
    if ($tag != 'vote') {
      $title .= ' (' . ucfirst($tag) . ')';
    }

    // Set all text values, then unset the unnecessary ones.
    $user_value = $votes['user']['value'];
    $average_value = $votes['average']['value'];
    $count_value = $votes['count']['value'];
    switch ($settings['text_display']) {
      case 'average':
        $user_value = NULL;
        break;
      case 'user':
        $average_value = NULL;
        break;
      case 'smart':
        if ($votes['user']['value']) {
          $average_value = NULL;
        }
        else {
          $user_value = NULL;
        }
        break;
    }
  }
  else {
    $stars = 5;
    $star_value = $votes['average']['value'];
    $user_value = $votes['user']['value'];
    $average_value = $votes['average']['value'];
    $count_value = $votes['count']['value'];
  }
  $star_display = theme('fivestar_static', $star_value, $stars);
  $text_display = $settings['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);
}