I have created a product listing page where I give editor's the choice of reviewing the product and giving it a rating using the Fivestar CCK. The problem is that the fivestar rating appears even when the product has not been reviewed and has no star rating. I would like the Fivestar rating to be optional even if there is a review, but as the Fivestars appear with no results and so it seems that the reviewer has given zero stars. For my case 1 star is the lowest rating. What is the method (php code perhaps) to hide fivestar CCK field if there are no votes please? Thanks

Comments

pforestsd’s picture

I am having this exact same issue. If anyone has any suggestions it would be greatly appreciated.

smartparty’s picture

Ditto. Subscribing.

hexblot’s picture

Had the same problem, here's a fix :

find the file fivestar.module, and find the function theme_fivestar_static (around ln 1396 in my case )
The first line should be :

  $output = '';

Add the line

  if ( $rating == 0 ) return $output;

right under it. You should now have something like :

function theme_fivestar_static($rating, $stars = 5, $tag = 'vote') {
  $output = '';
  if ( $rating == 0 ) return $output;

CCK will recognise the output as empty, and not show the field.

I'm not submitting a patch since this would require an admin checkbox to figure out whether this is a desired behavior, plus I'm not sure if this approach differentiates 0 as in "no rating" and 0 as in "score of 0".
However, in most cases you have a minimum rating anyway ( our reviews always have a typical minimum of 1 ), so it might help some people.

Enjoy :)

bnicoll’s picture

Perfect worked well for me.

Thanks again.
Ben.

jasom’s picture

Nice @hexblot, worked for me, good job.

imk’s picture

I'm having the same problem but am using Drupal 7.

I have implemented the fivestar rating into the comments.
I have two user roles, A and B, and A is not allowed to use fivestar to rate. I used the Field Permissions module to hide the fivestar field for role A. However, when they submit their comments, the fivestars appear empty.

I am relatively new to Drupal and would appreciate any help.

- imk

imk’s picture

Anyone have a D7 solution for this?

imk’s picture

Anyone?

BeaPower’s picture

same here, d7

Casper_Deseyne’s picture

for D7 a temporary solution is to find fivestar.field.inc and add the following code at line 467:

// No stars will be displayed in a form. Build a renderable array.
$value = $entity->$field['field_name'];
if ($value['und'][0]['count'] == 0) return false;
marcelovani’s picture

Status: Active » Needs review
StatusFileSize
new3.4 KB
new30.99 KB

I have created a patch that adds an extra option (see screenshot)

marcelovani’s picture

I am also using this patch http://drupal.org/node/1506582#comment-6677268
So, for anyone that want to use the patch above I create another patch for the current issue, merged with it

marcelovani’s picture

StatusFileSize
new12.34 KB

I am also using this patch http://drupal.org/node/1514826#comment-5866494
So, for anyone using http://drupal.org/node/1506582#comment-6677268 and http://drupal.org/node/1514826#comment-5866494 on the top of the current issue, here is a merge of the 3.

Status: Needs review » Needs work

The last submitted patch, merge-539444-1514826-539444-13.patch, failed testing.

marcelovani’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev

Changing the version to test the patch for D7

marcelovani’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, merge-539444-1514826-539444-13.patch, failed testing.

skruf’s picture

StatusFileSize
new12.26 KB

Updated patch #13 to fix bug where values were rendered as percentage on form validation fail. Also fixed bug that allowed user to add higher value than 'stars' setting.

rainman-2’s picture

For Drupal 7, I copied the theming function, theme_fivestar_static($variables) from sites/all/modules/fivestar/includes/fivestar.theme.inc into my template.php file. As always, I renamed the function to have my theme's name in place of 'theme'. Of course, clear your cache too.

All I did was add a conditional around the output that checked if there were any ratings first.

/**
 * Display a plain HTML view-only version of the widget with a specified rating.
 *
 * @param $rating
 *   The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars).
 * @param $stars
 *   The total number of stars this rating is out of.
 * @param $tag
 *   Allows multiple ratings per node.
 * @return
 *   A themed HTML string representing the star widget.
 */
function my_theme_name_fivestar_static($variables) {
  $rating  = $variables['rating'];
  $stars = $variables['stars'];
  $tag = $variables['tag'];
  $widget = $variables['widget'];
  if($widget['name'] != 'default') {
    drupal_add_css($widget['css']);
  }
  
  $output = '<div class="fivestar-' . $widget['name'] . '">';
  $output .= '<div class="fivestar-widget-static rian fivestar-widget-static-'. $tag .' fivestar-widget-static-'. $stars . ' clearfix">';
  if (empty($stars)) {
    $stars = 5;
  }
  if($rating != 0) { // THE ONLY LINE I ADDED, JUST BE SURE TO CLOSE YOUR CONDITIONAL
    $numeric_rating = $rating/(100/$stars);
    for ($n=1; $n <= $stars; $n++) {
      $star_value = ceil((100/$stars) * $n);
      $prev_star_value = ceil((100/$stars) * ($n-1));
      $zebra = ($n % 2 == 0) ? 'even' : 'odd';
      $first = $n == 1 ? ' star-first' : '';
      $last = $n == $stars ? ' star-last' : '';
      $output .= '<div class="star star-'. $n .' star-'. $zebra . $first . $last .'">';
      if ($rating < $star_value && $rating > $prev_star_value) {
        $percent = (($rating - $prev_star_value) / ($star_value - $prev_star_value)) * 100;
        $output .= '<span class="on" style="width: '. $percent .'%">';
      }
      elseif ($rating >= $star_value) {
        $output .= '<span class="on">';
      }
      else {
        $output .= '<span class="off">';
      }
      if ($n == 1)$output .= $numeric_rating;
      $output .= '</span></div>';
    }
  }
  $output .= '</div></div>';
  return $output;
}
GrahamO’s picture

Issue summary: View changes

Thank you rainman. #19 is just what I needed.

kopeboy’s picture

Title: Code to hide empty Fivestar CCK field » Hide empty Fivestar CCK field
Component: Documentation » Code
Category: Support request » Feature request

First patch 3 years ago, can we include a working patch please?

In my case it would be enough just to put a CSS class around the item that has no vote..