Posted by cesareaugusto on March 21, 2012 at 3:45pm
6 followers
Jump to:
| Project: | Computed Field |
| Version: | 7.x-1.x-dev |
| Component: | Documentation |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
Hi all! Sorry for writing here, but my PHP skills a pretty limited... I got a node type Club: it has four fivestar fields (food, service, price, comfort). I would like to have a fifth fivestar field, lets call it Average Node Rating which is computed as the average of the other four ratings. How do you think I could reproduce it? Is Computed Fields the right module?
Comments
#1
Yes, i have done that with Computed field.
I have created a computed_field named "field_moyenne_evaluation" (for the average), and I have surcharged the calculation and display functions by a custom module with this code :
Remember to change the field "field_moyenne_evaluation" by yours !
function computed_field_field_moyenne_evaluation_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)
{
$field_eval_cuisine = field_view_field('node', $entity, 'field_eval_cuisine');
$field_eval_service = field_view_field('node', $entity, 'field_eval_service');
$field_eval_lieu = field_view_field('node', $entity, 'field_eval_lieu');
$field_eval_toilettes = field_view_field('node', $entity, 'field_eval_toilettes');
$eval_cuisine = $field_eval_cuisine['#items'][0]['average'];
$eval_service = $field_eval_service['#items'][0]['average'];
$eval_lieu = $field_eval_lieu['#items'][0]['average'];
$eval_toilettes = $field_eval_toilettes['#items'][0]['average'];
$total = $eval_cuisine + $eval_service + $eval_lieu + $eval_toilettes;
$moyenne = $total / 4;
$entity_field[0]['value'] = $moyenne;
}
function computed_field_field_moyenne_evaluation_display($field, $entity_field_item, $entity_lang, $langcode){
//Récupérer la moyenne des votes du node
$moyenne = $entity_field_item['value'];
$variables = array("rating" => $moyenne, "stars" => 5);
$variables["widget"]["name"] = "basic";
$variables["widget"]["css"] = "sites/all/modules/contrib/fivestar/widgets/basic/basic.css";
//Add the fivestar CSS
drupal_add_css(drupal_get_path('module', 'fivestar') .'/css/fivestar.css');
//Retourner le résultat sous forme d'étoiles
return theme('fivestar_static', $variables);
}
#2
Hi Joel! First thank you really much for replying and sharing! I got a question (I'm not much into the deep Drupal coding thing):
I know how to create a computed field... though I don't know how to create a custom module... I found this on the internet http://drupal.org/node/361112... Is it what you are talking about?
#3
Hi,
You have to install the Computed Field module, add a Computed field to your content type (mine is field_moyenne_evaluation) and create a Drupal module (with http://drupal.org/node/361112 / the name of your module it's free) and create the two functions I have pasted.
You have to replace the name computed_field_field_moyenne_evaluation_compute and computed_field_field_moyenne_evaluation_display by computed_field_field_YOUR_FIELD_compute
Then you do your calculation in computed_field_field_YOUR_FIELD_compute and save the result in the $entity_field[0]['value'] variable :
For me :
<?php$entity_field[0]['value'] = $moyenne;
?>
In computed_field_field_YOUR_FIELD_display, you get the value previously saved with : $entity_field_item['value'];
For me :
<?php$moyenne = $entity_field_item['value'];
?>
And you send all the variables to the theme function.
Important: it's possible that you have to update your node to apply the changes on your computed field
Hope it help ! I'm french so my english explanation is maybe not very clear...
#4
Thanks again for your help Joel! I'll try it out and post here my results!
I was wondering... What about Views sorting according to such a computed Average Node Rating field? I would like to sort nodes according to their average rating. Would be the average computed on page view? Or stored within the Drupal database?
Again... what about when new users rate contents? Is the computed field updated on the fly?
Merci beaucoup! :)
#5
Yes, there is a view support for computed field, so you can sort with the average, I did it, so it works :)
The computed field is updated on the fly when a new user rate content.
#6
Hi,
I have a view where I managed to get the averaged numeric value for the nodes => it is the equivalent to the "$moyenne" variable in Joel Rotelli's code.
So for example, I get values such as 67.333, 73.333, etc.
Now I just need to display these values with fivestars, with the stars.
So I used a Global PHP field in the view, in which I pasted the second function of the code (function computed_field_field_moyenne_evaluation_display). I have no error message, but nothing is displayed either. Am I missing a "print" or "echo" something to have the stars displayed correctly ?
#7
Hi all,
The solution in #1 didn't work for me. For some reason, calling field_view_field() in _compute() crashes PHP with a memory exhaustion error after recursing indefinitely. Another problem is that field_view_field() returns a render array, which is heavy and complex.
What did work, and much simpler for me was to use fivestar_get_votes() like this:
function computed_field_field_overall_score_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
$axis1 = fivestar_get_votes($entity_type, $entity->nid, 'axis1');
$axis2 = fivestar_get_votes($entity_type, $entity->nid, 'axis2');
$axis3 = fivestar_get_votes($entity_type, $entity->nid, 'axis3');
$avg_axis1 = $axis1['average']['value'];
$avg_axis2 = $axis2['average']['value'];
$avg_axis3 = $axis3['average']['value'];
$entity_field[0]['value'] = ($avg_axis1 + $avg_axis2 + $avg_axis3) / 3;
}
axis1, axis2, and axis are the 3 different voting tags I created, each assigned to one of the 3 fivestar fields I have. You have to take care to handle the case where no one has voted yet, as the $axis1['average'] array (for example) will be empty. The snippet above doesn't account for this.
Other things to watch out are:
#8
The second solution by descender worked out for me really well. I used Joel Rotelli's method to manage the displaying of the field and have a question now:
The star-version of the field works but I want to have the numeric value to be displayed too.
How can I manage to do that? What do I have to change in the code?
#9
In the fivestar_theme function from the fivestar.module, you can find all the theme formatters you can use to display the value.
I used fivestar_classic (return theme('fivestar_static', $variables);) , but you can try others and send a correct $variables array
#10
I'm not a big coding kid. Could you please add a code suggestion for a fitting formatter for displaying the numeric value of the computed average field.
#11
I don't know exactly which theme formatter you need, but for exemple, with "fivestar_summary" :
You have to format a $variables array corresponding to the array needed in the fivestar_theme function
#12
When I use your solution with fivestar_summary, I get back "No Votes" instead of the numeric value.
Any suggestions?
#13
?