Aug 26, 2008: A recent user of this tutorial wrote some updates here.
Note: This tutorial was written before fivestar gained the ability to use comments for rating. It's still useful if you want your reviews to be nodes.
This is a revised version of Rate and review in one step using the node relativity module. (Thanks to greggles for pointing this module out.)
Purpose: This tutorial will show you one way of allowing users to rate and review a node all in the same form. This isn't the only way of doing it. If you are looking for a more drop in solution, see http://drupal.org/project/userreview .
Skill level: This tutorial is not aimed at beginners. You need to be comfortable with creating content types and theming them.
Support: Please use the support forum if you have questions and I will try to answer if I am able.
Modules needed: CCK, Views, Fivestar, and Node Relativity (as well as dependencies for those modules)
Fivestar uses drupal_eval() to determine the target node for the voting widget. Unfortunately, the $node object is not in scope in drupal_eval() and we need to pull the target from the $node object. The fix is to make the following change to fivestar_field.inc:
Change
$items[$delta]['target'] = drupal eval($item['target']);
to
$items[$delta]['target'] = eval($item['target']);
Hacking a module is uncool, so suggestions to get around that fix are welcome.
<?php
$current_rating = votingapi_get_voting_result('node', $nid, 'percent', 'vote', 'average');
$numstars = 5; // Change this to how many stars you want
print theme('fivestar_static', $current_rating->value, $numstars);
?><?php
print l("node/add/review/parent/$node->nid",NULL,NULL,NULL,FALSE,TRUE);
?><?php
// Retrieve the view
$view = views_get_view('reviews_of_node');
// Build the view into the $reviews variable with paging on and nodes per page set to 20
$reviews = views_build_view('embed', $view, array(strval($node->nid)), true, 20);
// Get the number of reviews
global $pager_total_items;
$numreviews = $pager_total_items[0];
// Print the reviews
print "Number of reviews: $numreviews";
print $reviews
?>When submitting the review, it goes to the review view page. It makes more sense to go back to the node being reviewed. If you have already have a site specific module, add this form_alter code:
<?php
function YOURMODULENAME_form_alter($form_id, &$form) {
switch ($form_id) {
// Only do this for the review type's form
case ('review_node_form'):
// On submission, redirect to the parent node
$pnid = arg(4);
$form['#redirect'] = "node/$pnid";
// This is in case we add more forms later
break;
}
}
?>Enjoy!
| Attachment | Size |
|---|---|
| rateandreview.zip | 745 bytes |
| rate-review-screen-1.jpg | 63.17 KB |
| rate-review-screen-2.jpg | 75.38 KB |
| rate-review-screen-3.jpg | 63.21 KB |