Download & Extend

Open Review ratings to Views module - PATCH INCLUDED!

Project:Review
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

I was wishing that the Review module were accessible to Views so that nodes could be sorted/displayed by rating when generating custom Views in the Views module. Then I figured it would make a fun project, so I did it myself. All you have to do is take the code I'm pasting near the bottom of this post and paste it into the review.module file before the first "theme_..." function. (Don't paste the opening and closing PHP tags, of course.)

This is a full-featured implementation of the Views hook, and has some kickass features. Now when you generate a View, you can choose to display, sort, and/or filter by rating. A new entry will exist in the Views UI for "Node: Review Rating" and there are many options for using it.

  • Under "Fields" you can add "Node: Review Rating" and display the rating of nodes.
  • The rating can be displayed as a number, as text (using the built-in theme function of the Review module), as a graphic (using the built-in theme function), or as a custom string you can enter yourself, like "Rated #rating out of a possible #maxrating".
  • The rating is available as a "Sortable" field.
  • The rating is available under "Filters" so you can filter the ratings by greater than/less than/equal to (and others) an entered number if desired.
  • The rating is available to the "Sort Criteria" section so you can use it in your sorting criteria if desired.

If you are updating your review.module file and uploading it, be sure to go to Administer > Site building > Views > Tools and clear the Views cache after uploading, so the new functionality will be enabled.

Now, with no further ado, here's the new code:

<?php
/**
* Implementation of hook_views_tables()
*
* This function makes the review ratings for nodes available to the Views module. You have
* to have Views installed to have this do anything. Coded by Matt F, <a href="http://www.whatismoving.com.
" title="www.whatismoving.com.
" rel="nofollow">www.whatismoving.com.
</a> */
function review_views_tables() {
 
$tables['review'] = array(
   
// First the name of the database table:
   
'name' => 'review',
   
// Then the description of how it joins to the node:
   
'join' => array(
     
'left' => array(
       
'table' => 'node',
       
'field' => 'nid',
      ),
     
'right' => array(
       
'field' => 'nid',
      ),
     ),
   
// Then the fields we're accessing from the table. If you use a "handler" it changes
        // the way the data is DISPLAYED in the listing. A simple example: if you have numeric data in the field
        // and have a handler function which adds "Number: " to it, it will still be sorted by the
        // number, but will display in the list with the prefixed text.
   
'fields' => array(
     
'review_rate' => array(
       
'name' => t('Node: Review Rating'),
       
'option' => 'string',
       
'sortable' => true,
       
'help' => t('Show the author\'s rating. If you select "Text Rating" or "Graphical Rating" the built-in theme' .
           
' functions for the Review module will generate the output. If you choose "Custom Format," then you must' .
           
' be sure to enter a string under "Option" for the display format. Use <strong>#rating</strong> and' .
           
' <strong>#maxrating</strong> as placeholders. For example, if you type "Rated #rating of possible #maxrating"' .
           
' in the Option field, the table view will output "Rated 6 of possible 10" and so on.'),
       
'handler' => array(
          
'views_handler_review_rating_text' => t('Text Rating'),
          
'views_handler_review_rating_graphic' => t('Graphical Rating'),
          
'views_handler_review_rating_numeric' => t('Number Only'),
          
'views_handler_review_rating_custom' => t('Custom Format'),
           ),
      ),
    ),
   
// Add an entry for the "Sorts" part of the Views module. Makes available a new criterion for sorting
    // results when creating/editing the View. If you enter Sort Criteria when building your View through the
    // Views module, then any sortable fields you set under Fields will be overridden and ignored.
    
'sorts' => array(
     
'review_rate' => array(
       
'name' => t('Node: Review Rating'),
       
'help' => t('Sort by the numeric rating of the node.'),
      ),
    ),
       
// Add an entry for the "Filters" part of the Views module:
   
'filters' => array(
     
'review_rate' => array(
       
'name' => t('Node: Review Rating'),
       
'operator' => 'views_handler_operator_gtlt',
       
'value' => array(
         
'#type' => 'select',
         
'#options' => _review_views_generate_ratings_options(),
        ),
       
'help' => t('This allows you to filter by numeric rating.'),
      ),
    )
   
  );
  return
$tables;
}

// Just a quick function to generate the array of possible ratings:
function _review_views_generate_ratings_options() {
 
$possibleratings = variable_get('review_rating_max',10);
 
$optionsarray = array();
  for (
$i=1;$i<=$possibleratings;$i++) {
   
$optionsarray[] = $i;
  }
  return
$optionsarray;
}

// Displays the numeric rating only:
function views_handler_review_rating_numbers($fieldinfo, $fielddata, $value, $data) {
  return
$value;
}
// Displays the rating using the revew module's text-rating theme function:
function views_handler_review_rating_text($fieldinfo, $fielddata, $value, $data) {
  return
theme('review_text_rating',$value,variable_get('review_rating_max',10));
}
// Displays the rating using the revew module's graphic-rating theme function:
function views_handler_review_rating_graphic($fieldinfo, $fielddata, $value, $data) {
  return
theme('review_graphic_rating',$value,variable_get('review_rating_max',10));
}
// Displays the rating using the custom field entered by the admin under "Option":
function views_handler_review_rating_custom($fieldinfo, $fielddata, $value, $data) {
 
$customstring = $fielddata['options'];
 
$maxrating = variable_get('review_rating_max',10);
  return
str_replace(array('#rating','#maxrating'),array($value,$maxrating),$customstring);
}
?>

Comments

#1

This is awesome, as I was just looking for exactly the same functionality! Except .... I don't know where to put this code .... silly me! Do I append it to the review.module file?

#2

oops -- he he -- never mind ... got it ...

#3

Does this work with the NodeReview Module, which file do you edit to implement this in?

Thanks, looks like an awesome idea, exactly what I need.

nobody click here