Updating Nodereview Module to Make Fivestar Comments Optional & Link Reviewed Node Information in Views
Recently, I've been doing some work on a site which requires users to be able to review nodes but users should only be able to provide one review per node. This is because there's a ranking system incorporated with the reviews (I'm using the Drupal Five Star module for this). So, before I started custom coding, I thought I'd take a look at drupal.org some something that could provide the functionality I was looking for. Low and behold, the Nodereview Module.
The Node Review module provided the exact functionality I needed, it creates a new "content type" for which you can define which other content types can be reviewed. I have the quotation marks because it is not really a typical content type... Additionally, Nodereview can integrate with the Fivestar module allowing you to create axes which users can vote the node on. The number of axes that can be created is unlimited (I believe). Finally, Nodereview also comes with a default view for the Views Module which includes a views field to list your reviewed nodes.
Problems with the Nodereview Module
However, I've so far run into 3 issues:
- The module does not seem to be properly maintained. I have sent an email to the maintainer so that I can take it over but have not, as of yet, heard back.
- When you enable the integration with Fivestar and allow users to rate content, each of axis rating must be commented on. For example, if a users rates a node with 5 stars, they can not submit that rating without a description / comment for that rating.
- While there is access to reviewed content's node ID, you can not access additional information such as the title, author, date it was published etc. all contained in the node table
The Solution? Start Modifying Nodereview's Code
So I had two choices, modify the code for Nodereview to fit my needs or start a module from scratch. Naturally I started the module from scratch... No I didn't, that woulda been a huge waste. So, to the point of this post, below is the code that I modified to allow site administrators to choose whether or not fivestar reviews should require comments and update to the views hook to link to all information about the reviewed node, not just the ID.
Hope this helps.
From nodereview.install, add the following after line 110:
//this creates a description_required field in the database.
'description_required' => array(
'type' => 'int',
'not null' => TRUE,
'length' => 11,
'default' => 0,
'description' => t('Whether or not a vote description is required.'),
),
from nodereview.admin.inc:
replace line 48 with:
$result = db_query("SELECT aid, tag, description, description_required, weight FROM {nodereview_axes} WHERE node_type='%s' ORDER BY weight", $type);
replace line 88 with:
//this ensures any changes to the required description checkbox get recorded when updating the admin page
db_query("UPDATE {nodereview_axes} SET tag='%s', description='%s', description_required=%d, weight=%d WHERE aid=%d", $axis['tag'], $axis['description'], $axis['description_required'], $axis['weight'], $axis['aid']);
replace line 97 with:
//this updates the insert query to include recording whether or not a vote description is required
db_query("INSERT INTO {nodereview_axes} (node_type, tag, description, weight, description_required) VALUES ('%s', '%s', '%s', %d, %d)", $form_values['node_type'], $axis['tag'], $axis['description'], $axis['weight'], $axis['description_required']);
after line 137, add:
//this adds a description_required checkbox to the form
$form['description_required'] = array(
'#type' => 'checkbox',
'#title' => t('require a vote description'),
'#return_value' => 1,
'#default_value' => $record->description_required,
'#description' => t(''),
);
from nodereview.theme.inc:
after line 23, add:
//this eliminates the title for the checkbox like the other fields
$form['axes'][$key]['description_required']['#title'] = '';
after line 29, add:
//this renders the checkbox, without this line, your checkbox won't appear
$row[] = drupal_render($form['axes'][$key]['description_required']);
replace line 35:
//this updates the headers to include "require vote comments"
$header = array('use', 'name', 'description', 'require vote comments', 'weight');
From nodereview_node_nodereview.inc:
replace line 128 to the comment for implementation of hook_validate with:
//the #required field now looks at the field we've added to the database indicating whether or not a description/comments are required per axis
$form['reviews'][$axis->aid]['review'] = array(
'#type' => 'textarea',
'#title' => t('Review'),
'#default_value' => $node->reviews[$axis->aid]['review'],
'#required' => $axis->description_required,
);
}
update line 150:
//updates so that if there is no review and a view is required that the form won't validate
if (! $review['review'] && $review['review']['required']==1) {
Open nodereview.views.inc and replace line 17 onwards with:
$data['nodereview']['reviewed_nid'] = array(
'title' => t('Reviewed Node'),
'help' => t('The node which is reviewed.'),
'relationship' => array ( //this creates the relationship with node reviewed linking all available fields from the node table
'base' => 'node',
'field' => 'nid',
'handler' => 'views_handler_relationship',
'label' => t('Reviewed Node'),
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
'numeric' => TRUE,
),
'argument' => array(
'handler' => 'views_handler_argument_numeric',
'numeric' => TRUE,
),
);
return $data;
}

Comments
Byperetrice
Very Interesting Post! Thank You For Thi Information!
It Worked!
I have been trying to get this working for a few months now. I left the after Line 88 change out to get it to work for me. Thank you!
Post new comment