You are here

function theme_fivestar_summary in Fivestar 7.2

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

Display the text associated with a static star display.

Note that passing in explicit data types is extremely important when using this function. A NULL value will exclude the value entirely from display, while a 0 value indicates that the text should be shown but it has no value yet.

All ratings are from 0 to 100.

Parameters

$user_rating: The current user's rating.

$average: The average rating.

$votes: The total number of votes.

$stars: The number of stars being displayed.

Return value

A themed HTML string representing the star widget.

2 theme calls to theme_fivestar_summary()
fivestar_expand in ./fivestar.module
Process callback for fivestar_element -- see fivestar_element()
fivestar_field_formatter_view in includes/fivestar.field.inc
Implements hook_field_formatter_view().

File

includes/fivestar.theme.inc, line 259
Provides the theming functions for fivestar.

Code

function theme_fivestar_summary($variables) {
  $microdata = $variables['microdata'];
  extract($variables, EXTR_SKIP);
  $output = '';
  $div_class = '';
  $average_rating_microdata = '';
  $rating_count_microdata = '';
  if (isset($user_rating)) {
    $div_class = isset($votes) ? 'user-count' : 'user';
    $user_stars = round($user_rating * $stars / 100, 1);
    $output .= '<span class="user-rating">' . t('Your rating: <span>!stars</span>', array(
      '!stars' => $user_rating ? $user_stars : t('None'),
    )) . '</span>';
  }
  if (isset($user_rating) && isset($average_rating)) {
    $output .= ' ';
  }
  if (isset($average_rating)) {
    if (isset($user_rating)) {
      $div_class = 'combo';
    }
    else {
      $div_class = isset($votes) ? 'average-count' : 'average';
    }
    $average_stars = round($average_rating * $stars / 100, 1);
    if (!empty($microdata['average_rating']['#attributes'])) {
      $average_rating_microdata = drupal_attributes($microdata['average_rating']['#attributes']);
    }
    $output .= '<span class="average-rating">' . t('Average: !stars', array(
      '!stars' => "<span {$average_rating_microdata}>{$average_stars}</span>",
    )) . '</span>';
  }
  if (isset($votes)) {
    if (!isset($user_rating) && !isset($average_rating)) {
      $div_class = 'count';
    }
    if ($votes === 0) {
      $output = '<span class="empty">' . t('No votes yet') . '</span>';
    }
    else {
      if (!empty($microdata['rating_count']['#attributes'])) {
        $rating_count_microdata = drupal_attributes($microdata['rating_count']['#attributes']);
      }

      // We don't directly substitute $votes (i.e. use '@count') in
      // format_plural(), because it has a span around it which is not
      // translatable.
      $votes_str = format_plural($votes, '!cnt vote', '!cnt votes', array(
        '!cnt' => '<span ' . $rating_count_microdata . '>' . intval($votes) . '</span>',
      ));
      if (isset($user_rating) || isset($average_rating)) {
        $output .= ' <span class="total-votes">(' . $votes_str . ')</span>';
      }
      else {
        $output .= ' <span class="total-votes">' . $votes_str . '</span>';
      }
    }
  }
  $output = '<div class="fivestar-summary fivestar-summary-' . $div_class . '">' . $output . '</div>';
  return $output;
}