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

Last updated on
13 May 2020

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Tutorial Objective:

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

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.

Warning: Each user is allowed to vote though Reviews on each Product just once. This is by design of the Fivestar module. The vote is stored as a Product vote not a Review vote.

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

  1. Create a new node type called "Product".
  2. Important: Check the "Enable fivestar rating" box under the "Fivestar ratings" fieldset.
    This is important because votes made by the Review content type will not be calculated unless this box is checked.
  3. 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).
  4. Save this content type
  5. 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

  1. Create a new node type called "Review".
  2. Save the node type
  3. Select "Manage Fields" for "Review" from the list of content types
  4. Add a node reference field
    1. Set the label to "Select the product you wish to review"
    2. Set field name to "product_reference".
    3. Select "Node reference" as the field type
    4. Save the field
    5. Make the field a required field
    6. Check the "Product" node type under "Content types that can be referenced".
    7. Save the field
  5. Add a Fivestar rating field
    1. Set the label "Reliablity Rating"
    2. Set field name "reliability".
    3. Select "Fivestar rating" as the field type
    4. Save the field
    5. Make the field a required field
    6. Under "Voting target" select the node reference field "field_product_reference". No need for php code.
    7. Under voting axis, enter "reliability".
    8. Save the field settings.
  6. Add a second Fivestar rating field
    1. Set label to "Value Rating"
    2. Set field name "value".
    3. Use the same field settings as the "Reliability Rating" field
    4. Under voting axis, enter "value".
    5. 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.

Step 4: 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.

  1. Go to admin/build/views and create a new view.
  2. Give it a View name of "Products" and a View type of "Node".
  3. Add a Page display and make the title of the page "Products and Product Ratings".
  4. Make the Style of the page a "Table"
  5. Make the Path of the page "products".
  6. Add a field of type "Node: Title".
  7. Add a filter of type "Node: Type"
    1. Select "Is one of"
    2. Check the node type "Product".
  8. Add a Relationship to the view of the type "Node: Voting results".
    1. In the previously mentioned tutorial, the axes that were used were "reliability" and "value". Staying consistent, give the relationship a label of "Reliability Results".
    2. Make the Value type "Percent",
    3. Select the Vote tag "reliability",
    4. Select Aggregation function "Average".
  9. Add a second Relationship of type "Node: Voting results"
    1. Label it "Value Results"
    2. Make the Value type "Percent"
    3. Vote tag "value"
    4. Aggregation function "Average".
  10. Add a field of type "Voting API results: Value".
    1. Make the Appearance "Fivestar Stars (display only)
    2. Select the Relationship "Reliability Results"
    3. Set label" "Average Reliability Rating".
  11. Add another field of the type "Voting API results: Value".
    1. Make the Appearance "Fivestar Stars (display only)
    2. Set the Relationship "Value Results"
    3. Set 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).

Help improve this page

Page status: No known problems

You can: