if the average is like 3.7 it will show full 4 stars
it's supposed to show full 3 stars and half a star??

CommentFileSizeAuthor
2017-11-04_230314.jpg29.46 KBabarood
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

abarood created an issue.

System Lord’s picture

Is it supposed to? I'd like to know this as well.

vdsh’s picture

I managed to solve this doing the following (in a custom module):

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add possibility to see .5 star
 * 
 */
function rating_helper_form_fivestar_custom_widget_alter(&$form, &$form_state, $form_id) {
  drupal_add_js(array('fivestar' => array('average' => $form['vote']['#values']['average'])), array('type' => 'setting')); 
  drupal_add_js(drupal_get_path('module', 'rating_helper').'/rating_helper.js', 'file');
}

Then in the rating_helper.js:

(function ($) {
  jQuery(document).ready(function($) {   
    var $container = $('.fivestar-widget');

    // allow half stars for average
    if (typeof(Drupal.settings.fivestar.average) !== 'undefined') {
      var average = Drupal.settings.fivestar.average; // on 100
      average = average * $('.fivestar-widget .star').length /  100 ;  // against # of stars, 
      // fake doubling the number of stars to know if we should round to half or not
      average = Math.round(average * 2);
      
      if ( average % 2 == 1) {
        $container.find('.star').eq(Math.floor(average/2)).addClass('half');
      }
      
    }
  });
})(jQuery);

And then I needed to create a fourth "half star" in my template and update the css (there is probably a possibility to display only half a star from a complete star image in css, but I couldn't make it work)

.fivestar-YOURWIDGET div.fivestar-widget div.hover a,
.fivestar-YOURWIDGET div.rating div a:hover {
  background-image: url(star.png);
  background-position: 0 -32px !important;
}

div.fivestar-widget .star.on.half a{
  background-position: 0 -48px;
}