When I define multiple rating-fields in CCK I like to get one summary field displaying the average of all single rating fields.

How can this be realized?

Comments

Anonymous’s picture

That should be possible by using a reaction rule (Rules module). Wenn saving a node, you could calculate the average and set the summary field. It might be tricky to hide the summary field during editing, though. For hiding the field, two modules come to my mind: Display Suite and Hidden Field Widgets. There could be other ways of hiding the summary field.

This approach needs some elaboration. I'm about to implement a similar thing and will try to provide an update soon.

Anonymous’s picture

Title: Overall rating from multiple rating fields » Add documentation: How to set overall/average rating field using Rules
Category: support » task

The procedure of #1 works. It's a bit tricky though, and should be documented somewhere with screenshots. I try to provide the required information here.

You'll need the latest dev version of Rules (because of the new type conversion actions) and the Hidden Field Widgets.

These are the steps:
- Set up the content type. All rating fields should be of type integer.
- For the overall rating, set the widget type "server side only" (to hide it from the form)
- Create a reaction rule to be invoked "before saving a node"

The rule config requires adding up the ratings in several steps, pseudocode:

result1 = rating1 + rating2; // add up the first and second rating field
result2 = result1 + rating3; // add the third rating field, so result2=rating1+rating2+rating3
average = result2 / 3;
average_int = convert_to_integer(average); // can be assigned using "set a value" action

This will get even longer, if you need to get the average of more than 3 rating fields.

The Rules config for 3 ratings looks like:

Rule config:

{ "rules_rating_calc_overall" : {
    "LABEL" : "calc overall rating",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "rating" ],
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_rating1" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_rating2" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_rating3" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_rating_overall" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "node:field-rating1" ],
            "op" : "+",
            "input_2" : [ "node:field-rating2" ]
          },
          "PROVIDE" : { "result" : { "rating_intermediate_result" : "intermediate result" } }
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "rating-intermediate-result" ],
            "op" : "+",
            "input_2" : [ "node:field-rating3" ]
          },
          "PROVIDE" : { "result" : { "total" : "total" } }
        }
      },
      { "data_calc" : {
          "USING" : { "input_1" : [ "total" ], "op" : "\/", "input_2" : "3" },
          "PROVIDE" : { "result" : { "rating_avg" : "average rating" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "rating-avg" ] },
          "PROVIDE" : { "conversion_result" : { "rating_avg_int" : "avg to integer" } }
        }
      },
      { "data_set" : {
          "data" : [ "node:field-rating-overall" ],
          "value" : [ "rating-avg-int" ]
        }
      }
    ]
  }
}
marcoka’s picture

Issue summary: View changes

if you try to do a real rating system, just use the "rate" module
https://drupal.org/project/rate

marcoka’s picture

Status: Active » Closed (works as designed)