On my website, users can rate each other's nodes via a Fivestar field called Stars in a comment. I'd now like to calculate the overall average score of a user. That means, I'd like to display the user's average score for all his/her nodes.
For example. Let's say a user created 2 nodes: Node 1 and Node 2. Some people rated his content:
- Scores of Node 1: 4/5, 5/5, 3/5
- Scores of Node 2: 4/5, 4/5
The overall average would then be (4+5+3+4+4)/5=4.
Preferably, this result should also be displayed as stars.
How can I do that?
Edit 1: Working with the Computed Field module seems to be a general idea to solve this matter. It looks fine to me and I think it's best to store the users' average score in a user field, in order to make it easily accessable. (Working with a view is also fine.) I'm planning to display the average score on their profile page, but perhaps I'm going to use it elsewhere on the website in the future too.
Can you please help me with (a scetch) of the code I'm supposed to use?
Edit 2: I created a user field called Gemiddelde and pasted @Thomas4019's code in my custom module hiderating (the module's title refers to an earlier question), but it gave me the following error:
- Warning: Missing argument 5 for computed_field_field_gemiddelde_display(), called in C:\wamp\www\mysite\sites\all\modules\computed_field\computed_field.module on line 341 and defined in computed_field_field_gemiddelde_display()(line 24 of
C:\wamp\www\mysite\sites\all\modules\hiderating\hiderating.module).- Notice: Undefined variable: entity in computed_field_field_gemiddelde_display() (line 30 of C:\wamp\www\mysite\sites\all\modules\hiderating\hiderating.module).
- Notice: Trying to get property of non-object in computed_field_field_gemiddelde_display() (line 30 of C:\wamp\www\mysite\sites\all\modules\hiderating\hiderating.module).
The Gemiddelde field in the view on my profile page has the following value:
<div class="fivestar-basic"><div class="fivestar-widget-static fivestar-widget-static-vote fivestar-widget-static-5 clearfix"><div class="star star-1 star-odd star-first"><span class="off">0</span></div><div class="star star-2 star-even"><span class="off"></span></div><div class="star star-3 star-odd"><span class="off"></span></div><div class="star star-4 star-even"><span class="off"></span></div><div class="star star-5 star-odd star-last"><span class="off"></span></div></div></div><div class="fivestar-summary fivestar-summary-average-count"><span class="empty">No votes yet</span></div>
Edit 3: After reinstalling the Computed Field module and clearing caches, the error messages are gone, but the Gemiddelde field in my view still displays the following CSS code:
<div class="fivestar-basic"><div class="fivestar-widget-static fivestar-widget-static-vote fivestar-widget-static-5 clearfix"><div class="star star-1 star-odd star-first"><span class="on">3</span></div><div class="star star-2 star-even"><span class="on"></span></div><div class="star star-3 star-odd"><span class="on"></span></div><div class="star star-4 star-even"><span class="off"></span></div><div class="star star-5 star-odd star-last"><span class="off"></span></div></div></div><div class="fivestar-summary fivestar-summary-average-count"><span class="average-rating">Average: <span >3</span></span> <span class="total-votes">(<span>1</span> vote)</span></div>
What went wrong?
$display_output = $display_func($field, $entity_field_item, $entity_lang, $langcode);
The bounty is yours if you can fix this.$entity
is there again! Very odd... After clearing caches, the Gemiddelde field in my view still displays CSS code. You can find the code in Edit 3. Did you test your code? It would be quite strange if it worked on your website and not on mine...