I have a review site. I use flexinode to create customized nodes. One node type is "products" and another is "reviews". The taxonomy vocabulary allows nodes of type "products". Then, using the "Attach Node" module, a user viewing a specific product's node can select "attach new review to this node", which will allow them to create, submit and attach a review. There can be multiple reviews for each product.

In a review, one of the fields is a "rating" of the product, by that reviewer. The field is a drop down list of 0-5.

What I would like to do is have a calculated field in the PRODUCT node that displays the average of all ratings for that product. I'd also like to display that field in search results and other places on the site where tables or search results would also be showing the product or review node or a link to the products or review nodes.

Is there something that can already do this? Would it be easy to code? I'm a Perl guy - not a PHP guy. I'm at the point now where I am tempted to just write my own software to run the site rather than use Drupal - but I really would rather use Drupal instead.

Thanks for any assistance.

Comments

javanaut’s picture

I'm facing a similar problem in a project that I'm working on. As far as I know, there's no way to do this currently without writing some PHP code. It would at least involve a SQL query to perform a avg() on the field in question.

Assuming you're using the Node Relativity module to attach nodes, you could join the flexinode_data table against the relativity table and average the results:

SELECT AVG(numeric_data) AS average FROM flexinode_data f INNER JOIN relativity r ON f.nid=r.nid WHERE r.parent_nid=16 AND f.field_id=26;

This assumes that the Product's id is 16 and the "rating" field of the Review node type is 26.

A cheap hack would be to create a block with PHP code in it that displayed the average rating for the product being viewed. It would look something like:

$rating_field_id = 26;
$product_type = 'flexinode-1';
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(array("nid" => arg(1)));
  if($node->type == $product_type) {
    $avg = db_result(db_query('SELECT AVG(numeric_data) FROM flexinode_data f INNER JOIN relativity r ON f.nid=r.nid WHERE r.parent_nid=%d AND f.field_id=%d', $node->nid, $rating_field_id));
    printf("Average Rating: %2.0f", $avg);
  }
}

I think what you're really looking for won't be available until the next rework of flexinode comes out with drupal 4.6. They're redesigning most of the module. I'm looking forward to it, myself.