Multi-axis Review System using Fivestar with Views Implementation for Drupal 6

Last modified: July 29, 2009 - 03:52

Tutorial Objective:

Create a simple rating/review system with a Drupal 6 installation and the latest stable versions of the following modules:

  • Fivestar
  • Voting API
  • CCK

This tutorial was written based on testing performed in the following environment:

  • Windows Vista Home Edition
  • Apache 2.2.8, PHP 5.2.6, MySQL 5.0.51a
  • Drupal 6.11
  • CCK 6.x-2.2
  • Fivestar 6.x-1.15
  • Voting API 6.x-2.0-rc2

This tutorial will use two content types created using CCK - "Product" and "Review". "Product" is the target node type, or in other words, the node type that is being reviewed by users. Users can submit reviews of Product's using the "Review" node type. The "Review" node type will contain the Fivestar CCK fields that users will use to rate the Product nodes. A multi-axis review system will be demonstrated. The multi-axis review system will consist of reviews that allows users rate a Product in more than one category (e.g. Reliability and Value instead of a single Overall Rating).

Step 1: Create the Product node type using CCK and create a Product

Create a new node type called "Product". IMPORTANT: CHECK THE "ENABLE FIVESTAR RATING" BOX UNDER THE "FIVESTAR RATINGS" FIELDSET. Votes made by the Review content type will not be calculated unless this box is checked. Then select "Hidden" under "Teaser Display" and "Full node display". This will hide the rating widget on the Product nodes (we don't want the widget on the product nodes, we want it on the Review nodes). Save this content type and create a Product node titled "Product A". Note the URL of this node as you will need to come back to it later.

Step 2: Create the Review node type with multiple rating fields using CCK and create a Review node

Create a new node type called "Review". Save the node and add a node reference field with the label "Select the product you wish to review" and field name "product_reference". Make the field a required field and check the "Product" node type under "Content types that can be referenced". Save the field and add a Fivestar rating field with the label "Reliablity Rating" and field name "reliability". Make the field a required field and under "Voting target" select the node reference field "field_product_reference". PHP CODE IS NOT NEEDED -- LEAVE BLANK. Under voting axis, enter "reliability". Save the field settings. Enter a second Fivestar rating field with label "Value Rating" and field name "value". Use the same field settings as the "Reliability Rating" field, but under voting axis, enter "value". Save the field settings.

Now you can create a Review, enter a title, enter body text (optional), give it a rating, and select "Product A" under "Select the product you wish to review". Submit the review.

Step 3: Create custom node template file

Go to your theme folder. Copy node.tpl.php and rename it node-product.tpl.php. Insert the following code in the template file to display the average of the rating:

<?php
$reliability_rating
= votingapi_select_results(array('content_id' => $node->nid, 'tag' =>'reliability', 'function' => 'average'));
print
'<div><strong>Reliability Rating:</strong>';
print
theme('fivestar_static', $reliability_rating[0]['value'], '5');
print
'</div>';
$value_rating = votingapi_select_results(array('content_id' => $node->nid, 'tag' =>'value', 'function' => 'average'));
print
'<div><strong>Value Rating:</strong>';
print
theme('fivestar_static', $value_rating[0]['value'], '5');
print
'</div>';
?>

The position of this code in the template file will determine where the rating widget(s) is/are displayed on the Product node pages. See Creating and Contributing a Fivestar Widget Set for creating custom fivestar widgets

After you have inserted the display code into node-product.tpl.php, visit the Product A node. You should now see the rating widget(s). In order for the widget to display an average of ratings, you must submit multiple Reviews. By design, a second rating by a user will overwrite the initial rating that user submitted. In other words, you must submit Reviews by at least two different users to see an average calculated on the Product node pages. After you submit multiple Reviews, you can verify that the average is being calculated and displayed.

After this review system has been built, the display of these reviews using Views is the common next step. This second part of this tutorial outlines a simple procedure for displaying the results of each rating axis for each node that is being rated using the system outlined above.

Once the review system is in place, make sure you have at least 1 "Product" node (call it "Product A") and 2 "Review" nodes (call them Review 1 and Review 2) created by 2 different users that rate the "Product A" node.

Objective: Build a view that displays all "Product" nodes and the average of each rating given to each "Product" node. Sort views by highest rating in different categories.

Go to admin/build/views and create a new view. Give it a View name of "Products" and a View type of "Node". Add a Page display and make the title of the page "Products and Product Ratings". Make the Style of the page a "Table" and make the Path of the page "products". Add a field of type "Node: Title". Add a filter of type "Node: Type" and select "Is one of" and check the node type "Product".

Now add a Relationship to the view of the type "Node: Voting results". In the previously mentioned tutorial, the axes that were used were "reliability" and "value". Staying consistent, give the relationship a label of "Reliability Results". Make the Value type "Percent", the Vote tag "reliability", and the Aggregation function "Average". Add a second Relationship of type "Node: Voting results" - label it "Value Results", make the Value type "Percent", Vote tag "value", and Aggregation function "Average".

Now add a field of type "Voting API results: Value". Make the Appearance "Fivestar Stars (display only), the Relationship "Reliability Results" and the Label" "Average Reliability Rating". Add another field of the type "Voting API results: Value". Make the Appearance "Fivestar Stars (display only), the Relationship "Value Results" and the Label "Average Value Rating".

You should now have a view that displays the Product name and the aggregate averages for each rating axis (Reliability and Value). Now a sort can be added to this view. Add a Sort Criteria of type "Voting API Results: Value". Make the Relationship "Reliability Rating" and sort "Descending" to go from highest values to lowest values. This will sort the Products from highest "Reliability Rating" to lowest "Reliability Rating". This view can then be cloned and edited so that the products are sorted by different rating axes (sort by Value rating for instance).

Making this work...

njwrigley - July 15, 2009 - 14:36

Hi there,

This is exactly what I am looking for. All of the functionality that you describe meets all of my needs. However, I simply cannot make it work. I am a complete newbie to Drupal and as such I have tried really hard to follow all of the instructions to the letter, but something somewhere has gone wrong.

I can see the Fivestar 'stars' on the Product A node, but when my mouse rolls over them, nothing happens! JavaScript is enabled and I am able to vote on 'stars' if I simply enable the Fivestar module in comments from the Product content type 'edit' area.

I'm just stumped!

Sorry, and I hope that someone might be able to help me out with this one.

Thanks.

What you are seeing on the

Ryan Aslett - July 17, 2009 - 16:26

What you are seeing on the Product A node is the aggregation of all the other Reviews, not a "rateable" widget, but a ratings display widget. What this tutorial leaves out is how to place a link on the Product A node that takes you to the "review node" with the node-reference already filled out. Either that or I just dont understand it..

Same here

rreck - August 12, 2009 - 19:26

Yep, me too. I have reproduced this situation. I am not sure how exactly to solve it, but it had to do with permissions. I did fix it.

Prepopulate

bara.munchies - October 13, 2009 - 10:26

You can do that with the prepopulate module or if you plan to use Form API just send a get parameter along the way. If you did not understand the last part, then go for prepopulate :)

http://drupal.org/project/prepopulate

Brilliant tutorial, worked

digitisation - August 5, 2009 - 12:55

Brilliant tutorial, worked fine using D6.13 and VotingAPI 6.x-2.x-dev and Fivestar 6.x-1.18.
Hopefully, with some theming and something like a javscript popup to create a review on a node without navigating away will be great.

Thanks but I cant get views to work.

blb2397 - August 7, 2009 - 01:41

I learned a lot here but for whatever reason the view is empty whenever I add the relationship as above.
Any ideas??

this work only if review is published

dhalgren - August 8, 2009 - 06:08

how to show the voting result with unpublished review? and how to show the numer of voters?

would like to use this too

rreck - August 18, 2009 - 16:06

I am trying to make this work also, but the tutorial assumes that I know more about Drupal than I do.

  • For steps #1 & #2 you are creating a node type but to me it makes more sense to call it a content type in this situation
  • To me, it seems much clearer if the instructions read

    Create a new content type called "Review" and save it. Then under the [Manage Fields] tab, add a node reference field ...

    Instead of:

    Create a new node type called "Review". Save the node and add a node reference field with the label "Select the product you wish to review" and field name "product_reference". (how did you complete the instructions in the first sentence WITHOUT saving it?)
  • This statement is not quite accurate:
    "This tutorial was written based on testing performed in the following environment:"

    If any of the tutorial discusses views, installing it should be a pre-requisite, or at least mentioned.

  • Also:
    "Copy node.tpl.php and rename it node-product.tpl.php"

    Might be re-written "Copy node.tpl.php to node-product.tpl.php".

Hey guys I am trying

braggadoshis - August 27, 2009 - 00:39

Hey guys I am trying something similar as well, but this tutorial doesn't quite allow us to do what I think we all want. I am trying to create a game rater where users rate a game based on graphics, sound, gameplay, and difficulty. I want them to be able to vote for all of these things right in the node with the game info and picture. How can I do this?

Hi, I'm using fivestar

czeky - September 10, 2009 - 06:56

Hi, I'm using fivestar comments rating for rating comments and I'd like to have average voting widget calculated from all comments ratings in node.tpl.php, is it possible?

search for video tutorial.

mansspams - October 1, 2009 - 01:05

search for video tutorial. one of them had this info

template part works. views

mansspams - October 1, 2009 - 01:59

template part works. views all good until creation of relationship - it immediatly returns error

Unknown column 'votingapi_cache_node_percent_overall_overall_average_average.tag_other'

OR .value_type_other. "overall" is tag or axis.

sorry. update to latest

mansspams - October 1, 2009 - 02:12

sorry. update to latest version helped.

set the widget to display current average in text

sedmi - October 1, 2009 - 11:41

How to set the widget to display current average in text (example:Average: 2.5 (20 votes)) below the stars? I guess node-product.tpl.php should be edited but I don't know how.

How to add vote up/down?

prabhakarsun - October 23, 2009 - 11:09

I want to use vote up/down along with fivestar.

I want to show "x% of users recommend this" on product page.

How can I do that?

up down voting

drupalhooked - October 26, 2009 - 06:46

Try Following drupal modules for up down voting
vote_up_down
updown

There is a module called flag this I guess you can use to make any node favourite node(you can use favourite node in context of recommendation ).

you can achieve what you are looking for by using any 2 of above mentioned 3 modules. just do a bit of RnD on those.

They don't

prabhakarsun - October 26, 2009 - 13:35

I tried them.

They won't integrate with node reference. They can be used to vote a node directly, but can't be used to vote on referenced node.

Meanwhile, I also thought of using 2 star "fivestar". But I can't find a way to show different images on different stars. i.e. 1st one = thumbs up, 2nd =thumbs down.

Up down and node referral

drupalhooked - October 27, 2009 - 06:50

Hi prabhakarsun
I m not clear about your complete requirements , just a try...

Im assuming that there is always only one review node corresponding to a product node
pn1 referenced by rn1
pn2 referenced by rn2
...
Apply vote up down on review node.
Download http://drupal.org/project/referral module : it will provide a read only cck field, Add it to product content type.
Product $node object will contain id of referral node (that will be review type node).
Load referral node(review node) which will contain all data of review.
Print the up down score of review node, which is actually for the product node.

And while giving fivestar rating to review node user will be able to up/down vote for review node.
You just need to put efforts on how to use referral module...

Note : once you apply node referral cck field to product node you must edit and save corresponding review node so that this module saves referral value.

Hope it helps

Thanks for your advice, but

prabhakarsun - October 27, 2009 - 12:24

I am trying to create a user review site.

Here are full details:
Content type 1: Products
Content type 2: Reviews

Registered Users can review products by referencing "Product" nodes.

What I want to have is a "Would you recommend? [ Yes/No ]" field in Review. Then I need to show "x% of users recommend it" on product page.

So obviously, I will be having multiple review nodes per product node.

NOt showing up..

NelM - October 26, 2009 - 05:04

i applied the php code to node-product.tpl.php and no widget is appearing on the node display. when i use devel to check, it shows that it is still using the node.tpl.php file with candidate node-product.tpl.php < node.tpl.php, what am i doing wrong? thanks in advance

Displaying average of all votes (sum up the multi-axis)

deanloh - November 9, 2009 - 08:16

This is one great documentation, with the help I have managed to come as far as setting up the review system of 3 axis. I was able to configure Views to display the average of votes on a view page.

See screenshot: http://dropbox.akandatang.info/multi-axis.png

But now I'm faced with the new challenge: to display the average of overall votes (by summing up all 3 axis, then average.)

For example, given a voting scores like below:

First axis: Quality - 60%
Second axis: Value - 50%
Third axis: Reliability - 90%

Therefore the average of the above = (60+50+90)/3 = 66.67%

I was able to show the average on node page by using this code:

$overall_score = (($quality_rating[0]['value'])+($value_rating[0]['value'])+($reliability_rating[0]['value']))/3;
print theme('fivestar_static', $overall_score, '5');

But what I need now is to add a column to the views page and display the average. What should I do to achieve that?

which permissions are needed to display the node selection

luksedj - November 11, 2009 - 21:23

Hi,

I followed this nice tutorial, and it works great. As long as i'm logged in as an admin, i can create reviews. When I'm a reviewer (seperate role), I can create a review, but the product drop down is missing. It is there when I'm an administrator (all privileges).

So i'm trying to figure out where I need to add an extra permission so that the product selection will show up for my reviewers.

At least, if this is a permissions issue. Ideas are most welcome!

thanks.

 
 

Drupal is a registered trademark of Dries Buytaert.