rating out of 7
Posted: 14 July 2007 03:22 PM   [ Ignore ]
Sr. Member
Avatar
RankRankRankRank
Total Posts:  119
Joined  2005-09-13

From what I understand, the ratings module can’t support ratings out of, for example, 7. Is that correct or are there just certain aspects of it that won’t work with a non 5-10 star form that I might be able to hack at?

Profile
 
 
Posted: 14 July 2007 04:15 PM   [ Ignore ]   [ # 1 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  736
Joined  2004-03-30

You can do a scale of 7. What will not work for you are the handy dandy star graphics. What you can do instead though, if you want to represent ratings graphically, is create images and name them numerically. Then when your templates load pages for you, you will complete the name of a graphic with a rating variable like this.

img src=“star-{overall_avg}“

mk

 Signature 

Mitchell Kimbrough

Profile
 
 
Posted: 02 September 2007 03:07 PM   [ Ignore ]   [ # 2 ]
Sr. Member
Avatar
RankRankRankRank
Total Posts:  119
Joined  2005-09-13

Hey Mitchell, so I’m setting about on this specific feature right now and, having plugged in: scale=“7” in the rating:stats tag parameters I’m getting:

The following errors were encountered

* You have provided an unacceptable rating scale. Acceptable rating scales are either 5 or 10. Note that you need not declare a rating scale if you will not be using the built-in image representations of rating stats.

So do I need to just pull out the scale parameter and pop in the {overall_avg} tag? Does the scale matter with the average when you are just returning the average and throwing in a custom img?

I think I get it, perhaps more experimentation is needed here…

Profile
 
 
Posted: 02 September 2007 05:45 PM   [ Ignore ]   [ # 3 ]
Sr. Member
Avatar
RankRankRankRank
Total Posts:  119
Joined  2005-09-13

Okay, got that, but now I need to return a percentage based overall_avg, say “percent_avg”, based on the scale parameter. I’ve spent the last 45 min going through the rating stats() but not quite wanting to break it…
... 1 hour later…

I mustered the following change of the rating module:
EDIT: The parts that I changed are commented with //WM

// ----------------------------------------
   // Parse graphics
   // ----------------------------------------
   
   
if ( $scale AND stristr( $key, '_avg' ) )
   
{
$standard
= preg_replace( "/^stars_|^circles_|^bar_|^percent_/", "", $key ); //WM

if ( isset( $this->fields_stats[$this->entry_id][$standard] ) )
{
  
// ----------------------------------------
  // Do some quasi math
  // ----------------------------------------
  
  // Get array
  
$num  = explode( ".", number_format( $this->fields_stats[$this->entry_id][$standard], 2, '.', '' ) );
  
$filler  = 0;
  
  
// Handle remainder
  
if ( $num['1'] < 25 )
  
{
   $num[
'1'] = 0;
  
}
  
elseif ( $num['1'] >= 25 AND $num['1'] < 50 )
  
{
   $num[
'1'] = 25;
  
}
  
elseif ( $num['1'] >= 50 AND $num['1'] < 75 )
  
{
   $num[
'1'] = 50;
  
}
  
else
  
{
   $num[
'1'] = 75;
  
}
  
  
/*
  // Handle filler
  if ( $num['1'] != 0 AND ( ( $num['0'] + 1 ) > $scale ) )
  {
   $filler  = $scale - 1 - $num['0'];
  }
  else
  {
   $filler  = $scale - $num['0'];
  }
  */
  
  // Handle filler
  
if ( $num['1'] == 0 )
  
{
   $filler  
= $scale - $num['0'];
  
}
  
else
  
{
   $filler  
= $scale - 1 - $num['0'];
  
}
  
  
  
// ----------------------------------------
  // Determine type
  // ----------------------------------------
  
  
if ( eregi( "^circles_", $key ) )
  
{
   $img  
= '';
   
$ext  = '.gif';
   
$urlfull = $image_url.'circle-100'.$ext;
   
$urlrem  = $image_url.'circle-'.$num['1'].$ext;
   
$urlfill = $image_url.'circle-0'.$ext;
   
$w   = '12';
   
$h   = '12';
   
$type  = 'circle';
  
}
  
elseif ( eregi( "^bar_", $key ) )
  
{
   $img  
= '';
   
$ext  = '.gif';
   
$urlfull = $image_url.'bar-100'.$ext;
   
$urlrem  = $image_url.'bar-'.$num['1'].$ext;
   
$urlfill = $image_url.'bar-0'.$ext;
   
$w   = '12';
   
$h   = '12';
   
$type  = 'bar';
  
}
  
elseif ( eregi( "^percent_", $key ) ) //WM
  
{
   $avg  
= round($this->fields_stats[$this->entry_id][$standard],2);
   
$img  = round(100 / $scale * $avg);

  
}
  
else
  
{
   $img  
= '';
   
$ext  = '.gif';
   
$urlfull = $image_url.'star-100'.$ext;
   
$urlrem  = $image_url.'star-'.$num['1'].$ext;
   
$urlfill = $image_url.'star-0'.$ext;
   
$w   = '12';
   
$h   = '12';
   
$type  = 'star';
  
}
  
// ----------------------------------------
  // Parse images
  // ----------------------------------------
  
if ( eregi( "^percent_", $key ) ) //WM
  
{
  }else
  {
   
for ( $i = $num['0']; $i > 0; $i-- )
   
{
    $img
.= $this->_img( $urlfull, $w, $h, $i, $type );
   
}
   
   $img
.= ( $num['1'] == 0 ) ? '': $this->_img( $urlrem, $w, $h, $i, $type );
   
   for (
$i = $filler; $i > 0; $i-- )
   
{
    $img
.= $this->_img( $urlfill, $w, $h, $i, $type );
   
}
  }
  
  
  $tagdata
= $TMPL->swap_var_single( $key, $img, $tagdata );
}
   }
  }

It works though!!! It takes a scale of (currently only) 7 (just because I added it to your scale check).

I’m quite pleased, but I’m sure there’s an easier way to hack this into the module. Suggestions? Ideas? Thanks Mitchell!

Profile
 
 
Posted: 04 September 2007 11:10 AM   [ Ignore ]   [ # 4 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  736
Joined  2004-03-30

Today you’ve earned a hacking badge to wear next to your rope knot badge and fire starting badge. Not long till Eagle Scout NOW!!

Seriously though, If it works it works.

mk

 Signature 

Mitchell Kimbrough

Profile
 
 
Posted: 04 September 2007 11:17 AM   [ Ignore ]   [ # 5 ]
Sr. Member
Avatar
RankRankRankRank
Total Posts:  119
Joined  2005-09-13

:D Nice. I always wanted a hacking badge.

Oh well, if you get it into the next version then let me know.

I’m using the css stars here by the way, in case any of your clients want to know.

Profile