How to calculate the user's average rating on all his/her nodes with Fivestar? 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:

&lt;div class="fivestar-basic"&gt;&lt;div class="fivestar-widget-static fivestar-widget-static-vote fivestar-widget-static-5 clearfix"&gt;&lt;div class="star star-1 star-odd star-first"&gt;&lt;span class="off"&gt;0&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-2 star-even"&gt;&lt;span class="off"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-3 star-odd"&gt;&lt;span class="off"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-4 star-even"&gt;&lt;span class="off"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-5 star-odd star-last"&gt;&lt;span class="off"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="fivestar-summary fivestar-summary-average-count"&gt;&lt;span class="empty"&gt;No votes yet&lt;/span&gt;&lt;/div&gt; 




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:

&lt;div class="fivestar-basic"&gt;&lt;div class="fivestar-widget-static fivestar-widget-static-vote fivestar-widget-static-5 clearfix"&gt;&lt;div class="star star-1 star-odd star-first"&gt;&lt;span class="on"&gt;3&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-2 star-even"&gt;&lt;span class="on"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-3 star-odd"&gt;&lt;span class="on"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-4 star-even"&gt;&lt;span class="off"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="star star-5 star-odd star-last"&gt;&lt;span class="off"&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="fivestar-summary fivestar-summary-average-count"&gt;&lt;span class="average-rating"&gt;Average: &lt;span &gt;3&lt;/span&gt;&lt;/span&gt; &lt;span class="total-votes"&gt;(&lt;span&gt;1&lt;/span&gt; vote)&lt;/span&gt;&lt;/div&gt;


What went wrong?
I know that something like this can be done with computed field. It will require some PHP code though. See some discussion here

Here's what you need to do:


Install Computed Field and Fivestar
Go to admin/config/people/accounts/fields and create a computed field on the user entity type. In my case I named it "field_computed_rating". On the settings form for the field make sure the checkbox "Store value in the database" is unchecked.
Click on "Manage Display" for the content type you created the field on. Then make sure the format for that field is set to "Unsanitized".
(if you already have a custom module you can skip this step) In your code base within sites/all/modules, create a new folder titled fivestar_user_aggregate. Within that folder you will need to create  fivestar_user_aggregate.info with the following contents. 

name = Fivestar User Aggregate
description = Assign users an aggregate rating based on all their nodes
core = 7.x
package = Custom

Add this custom php function to your module file or create fivestar_user_aggregate.module within fivestar_user_aggregate with the following contents. If you named your field differently then you will need to modify the function name accordingly.

&lt;?php

function computed_field_field_computed_rating_display($field, $entity_field_item, $entity_lang, $langcode, $entity) {
    $result = db_query("SELECT n.uid, n.nid, vote1.value AS count, vote2.value AS average FROM {node} n
INNER JOIN {votingapi_cache} vote1 ON n.nid = vote1.entity_id
AND vote1.function =  'COUNT'
INNER JOIN {votingapi_cache} vote2 ON n.nid = vote2.entity_id
AND vote2.function =  'average'
WHERE n.uid = :uid", array(':uid' =&gt; $entity-&gt;uid));

    $rating_count = 0;
    $rating_total = 0;

    $nodes = $result-&gt;fetchAll();
    foreach ($nodes as $node) {
        $rating_count += $node-&gt;count;
        $rating_total += $node-&gt;average * $node-&gt;count;
    }

    if ($rating_total == 0) {
        $weighted = 0;
    } else {
        $weighted = $rating_total / $rating_count;
    }

    $variables = array(
        "rating" =&gt; $weighted,
        "average_rating" =&gt; $weighted,
        "votes"  =&gt; $rating_count,
        "stars" =&gt; 5
    );
    $variables["widget"]["name"] = "basic";
    $variables["widget"]["css"] = "sites/all/modules/fivestar/widgets/basic/basic.css";  
    drupal_add_css(drupal_get_path('module', 'fivestar') .'/css/fivestar.css');
    return theme('fivestar_static', $variables) . theme('fivestar_summary', $variables);
}

Enjoy! You should now see a weighted average of all a user's node's ratings on their page.