0

On a single entry page I'm displaying that entry's ratings using {exp:rating:entries}. Within each rating returned, I'd like to display the average of some/all of the different ratings fields for that rating.

So I need something like {overall_avg} but it's an average of the fields in that single rating rather then an average of all ratings for that entry.

I don't see any combination of tags that can make this happen. Any creative ways to achieve this?

Here some template code. Inside my channel:entries tag on a single entry page I have this embed code:

{embed="vendor/.ratings" entry-id="{entry_id}" vendor-type="{categories show_group="10" limit="1"}{category_url_title}{/categories}"}

And then the embed template looks like this:

{exp:rating:entries precision="2" orderby="rating_date" sort="desc" img_height="14" img_width="14" collection="directory" entry_id="{embed:entry-id}" limit="200"}
{if count == 1}
<article class="listing-reviews" id="listing-reviews">
    <h3 class="centered"><em>{total_rating_count} review{if total_rating_count !="1"}s{/if}</em> for {title} <a class="btn directory write-edit" href="#listing-write-review">Write a review</a></h3>
{/if}
    <section class="box review" id="review-{rating_id}">
        <header>
            {if photo}
            {exp:ce_img:single class="pic" src="{photo_url}" max="45" crop="yes" alt="{name}"}
            {if:else}
            {exp:ce_img:single class="pic" src="/i/default-avatar.png" max="45" crop="yes" alt="{name}"}
            {/if}
            <h5>{name}</h5>
            <h6>{who} • {rating_date format="%F %d, %Y"}</h6>
            <div class="stars">
                {stars}{rating}{/stars}
            </div>
        </header>
        <div class="review-text">
            <div class="restrict-height">
                {review}
                <div class="overflow"><a href="#">Read more…</a></div>
            </div>
            <p class="small light">Wedding Date: {wedding}</p>
        </div>
        <ul class="ratings">
            <li>
                <label>{{embed:vendor-type}_1label}</label>
                {if rating1 !="" && rating1 !="0"}
                <div class="stars">
                    {stars}{rating1}{/stars}
                </div>
                <span class="total">{rating1}</span>
                {if:else}
                <div class="stars">
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                </div>
                <span class="total">N/A</span>
                {/if}
            </li>
            <li>
                <label>{{embed:vendor-type}_2label}</label>
                {if rating2 !="" && rating2 !="0"}
                <div class="stars">
                    {stars}{rating2}{/stars}
                </div>
                <span class="total">{rating2}</span>
                {if:else}
                <div class="stars">
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                </div>
                <span class="total">N/A</span>
                {/if}
            </li>
            <li>
                <label>{{embed:vendor-type}_3label}</label>
                {if rating3 !="" && rating3 !="0"}
                <div class="stars">
                    {stars}{rating3}{/stars}
                </div>
                <span class="total">{rating3}</span>
                {if:else}
                <div class="stars">
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                </div>
                <span class="total">N/A</span>
                {/if}
            </li>
            <li>
                <label>{{embed:vendor-type}_4label}</label>
                {if rating4 !="" && rating4 !="0"}
                <div class="stars">
                    {stars}{rating4}{/stars}
                </div>
                <span class="total">{rating4}</span>
                {if:else}
                <div class="stars">
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                    <img src="/i/star-0.png" width="14" height="14" class="rating_star" />
                </div>
                <span class="total">N/A</span>
                {/if}
            </li>
        </ul>
        <footer>
            {exp:rating:rating_vote_stats rating_id="{rating_id}"}
            {if not_already_voted}<span class="helpful"><strong>Was this review helpful?</strong><a href="/vendor/rating-vote/{rating_id}/up/">Yes</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="/vendor/rating-vote/{rating_id}/down/">No</a></span>{/if}
            {if already_voted}<span class="helpful"><strong>Was this review helpful?</strong>Thanks for your vote</span>{/if}
            <a class="permalink" href="#review-{rating_id}">#</a>
            <a class="report" href="/contact">Problem with this review?</a> 
            {if total > 0}<span class="light people-like">{up_votes}/{total} people liked this review</span>{/if}
            {/exp:rating:rating_vote_stats}  
        </footer>
    </section>
{if count == total_rating_count}
</article>
{/if}
{/exp:rating:entries}

I would like to replace the line {stars}{rating}{/stars} with stars for the average of the other 4 fields (rating1, rating2, rating3, rating4).

If I use the rating:stats, I can only specify an entry_id, which means I can get the average rating for the entry. I don't want that. I want the average rating of all rating fields for single rating. Since I cannot specify a rating_entry_id to the stats tag I cannot get an average for a single rating.

I hope that clarifies the question.

EE 2.7.2 Rating 3.2.3

3
  • I can get the numerical average of the fields with CE String like this: {exp:ce_str:ing math_lite}({rating1}+{rating2}+{rating3}+{rating4})/4{/exp:ce_str:ing} but I have no way of converting that into stars. It also doesn't account for "null" values which shouldn't affect average. Commented Jan 31, 2014 at 3:02
  • Template code please :) Also, are you using {exp:rating:stats} to get {overall_avg}? Finally, make sure you're feeding a Channel Entry entry_id, and not a Rating Entry entry_id.
    – Solspace
    Commented Jan 31, 2014 at 21:07
  • I've added some template code and clarification above. Commented Feb 1, 2014 at 7:01

2 Answers 2

2

Rating doesn't have a feature for calculating an average of rating fields for a single Rating entry (only an average of all ratings, for all fields, for a Channel entry is possible using {overall_avg}).

You'll have to do this manually, through PHP or a plugin that specializes in calculations.

Your solution above would work:

{exp:ce_str:ing math_lite}({rating1}+{rating2}+{rating3}+{rating4})/4{/exp:ce_str:ing}

Alternatively, you could do this with PHP:

<?php echo ("{rating1}" + "{rating2}" + "{rating3}" + "{rating4}")/4; ?>

As for displaying stars, you'll also have to do this manually. Here's an example using PHP. You might be able to use a plugin (or plugins) to do something similar:

$rating_avg = ("{rating1}" + "{rating2}" + "{rating3}" + "{rating4}")/4;

for($i = 1; $i <= $rating_avg; $i++)
{
    echo 'Your full star image';
}

$rounded_rating_avg = floor($rating_avg); // 4
$rating_avg_decimal = $rating_avg - $rounded_rating_avg; // eg. .75

switch($rating_avg_decimal)
{
    case 0.25;
        echo 'Your quarter star image';
    break;
    case 0.50;
        echo 'Your half star image';
    break;
    case 0.75;
        echo 'Your 3/4 star image';
    break;
}

Important Note: This is just an example. You will have to think about and/or adapt this example to your own needs.

1
  • Thanks for the example. In my case it may be simpler to just use some javascript on the rating form to add an average value of the other rating fields to a hidden rating field and then display that as the average. Commented Feb 7, 2014 at 19:09
0

Working off the example from Solspace with some modifications, I got good results from the PHP below.

The main differences are:

  1. I added code to check to see if each rating field had a valid entry (null or 0 are not valid in my system) and adjusted the rating count variable based on that. Then use rating count to divide for the average so the null values do not affect the average.

  2. I also added code at bottom to display extra 0 stars after lower ratings so five stars are always shown.


$rating_count = 0;
if ("{rating1}">=1) 
{
    $rating_count++;
}
if ("{rating2}">=1) 
{
    $rating_count++;
}
if ("{rating3}">=1) 
{
    $rating_count++;
}
if ("{rating4}">=1) 
{
    $rating_count++;
}

$rating_avg = ("{rating1}" + "{rating2}" + "{rating3}" + "{rating4}")/$rating_count;

for($i = 1; $i <= $rating_avg; $i++)
{
    echo '<img src="/i/star-100.png" width="14" height="14" class="rating_star" />';
}

$rounded_rating_avg = floor($rating_avg);
$rating_avg_decimal = $rating_avg - $rounded_rating_avg;

switch($rating_avg_decimal)
{
    case 0.25;
        echo '<img src="/i/star-25.png" width="14" height="14" class="rating_star" />';
    break;
    case 0.50;
        echo '<img src="/i/star-50.png" width="14" height="14" class="rating_star" />';
    break;
    case 0.75;
        echo '<img src="/i/star-75.png" width="14" height="14" class="rating_star" />';
    break;
}

$rating_max_diff = 5 - $rounded_rating_avg - 1;

if ($rating_avg == $rounded_rating_avg)
{
    $rating_max_diff++;
}

for($i = 1; $i <= $rating_max_diff; $i++)
{
    echo '<img src="/i/star-0.png" width="14" height="14" class="rating_star" />';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.