Oct 31, 2007 - This tutorial works but I've since found a more elegant way of doing it, so I recommend Rate and Review 2.
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 . I went this route because I wanted to use fivestar and I already have CCK and views installed so all I needed was prepopulate.
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 (as well as dependencies for those modules)
Note: In order for this to work, you need to make the following change to fivestar_field.inc:
Change
$items[$delta]['target'] = drupal eval($item['target']);
to
$items[$delta]['target'] = eval($item['target']);
The reason for this is that the $node object is not in scope in the evaluated PHP when using drupal_eval but it is with just eval. Hacking a module is uncool, so suggestions to get around that fix are welcome.
If you 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'):
//If we are adding a review, we need to grab the parent node ID from the URL
if (arg(1) == "add") {
// Get the parent node's id from the url
$pnid = $_GET['pnid'];
// Set the parentnid field's value
$form['field_parentnid'][0]['value']['#value'] = $pnid;
} else {
// Otherwise, grab the existing parent node ID to redirect to
$pnid = $form['field_parentnid'][0]['value']['#default_value'];
}
// Hide the field
$form['field_parentnid'][0]['value']['#access'] = FALSE;
// On submission, redirect to the parent node
$form['#redirect'] = "node/$pnid";
// This is in case we add more forms later
break;
}
}
?>If you don't already have one and don't know how to make a module, just use the one in the zip.
<?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("Add a review","node/add/review",NULL,"pnid=" . $node->nid,NULL,NULL,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
?>Enjoy!
| Attachment | Size |
|---|---|
| rateandreview.zip | 817 bytes |