Display the rating control seperate from the average rating

smitty - July 27, 2007 - 08:55
Project:jRating
Version:5.x-1.0
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

On January 26, 2007 there was this posting in the Drupal Support Forum: http://drupal.org/node/113095

But in the release version 5.x-1.0 this feature was not includet. Will this show up in the next version?

#1

hickory - November 21, 2007 - 15:20

I think the best ways to do this are either :

a) with Javascript: once the average rating stars have been drawn, iterate through each node and move that div to where you want it,
or
b) call print jrating_average_rating($content_type, $content_id) in your theme template, where $content_type is 'node', for example, and $content_id is the nid. Then hide everything with a class of .rating-item .rating-mean using CSS. This will mean an extra database call for each item.

#2

smitty - January 17, 2008 - 19:21

Thank you for this hint. But ... well, unfortunately ... I'm not very experienced in Javascript.

It would be great if you could give me a sort of a code snippet and a hint, where to put it.

I tried the print jrating_average_rating. This displays the average rating very nice.
But how can I get only the rating form (without the average rating)? I want to put it into a section in the node.tpl.php, witch means behind the $content.
And how can I get rid of the Rating form at the end of the $content (Weight of the rating widget: 10)? I unchecked all "Display" checkboxes in the options dialog but it is still there.

Deeply grateful for every help!

#3

smitty - February 1, 2008 - 16:39

Now I found myself how it works:

In the node.tpl.php as an anchor for this control you have to put a <div> at the place you want the average-rating or the rating-widget to be shown, e.g. for the rating widget (the original id of the widget - put in by the jrating-module - is: id='rating-mean-node2-' followed by the node-id):

<div id='rating-mean-node2-<?php print $node->nid ?>' class="rating rating-mean"></div>

Now you can shift the control to this <div> using JS:

<script type="text/javascript">document.getElementById('rating-mean-node2-<?php print $node->nid ?>').innerHTML = document.getElementById('rating-mean-node-<?php print $node->nid ?>').innerHTML;</script>

At the end you have to delete the widget at the old position:

<script type="text/javascript">document.getElementById('rating-mean-node-<?php print $node->nid ?>').innerHTML = '';</script>

#4

hickory - February 1, 2008 - 16:52

That sounds about right, though you should probably be putting the script in a separate javascript file and doing something like this:

$(".rating-mean").each(function(){
var matches = this.id.match(/rating-mean-node-(\d+)$/);

if (matches){
  var nid = matches[1];
  var mean = $("#rating-mean-node-" + nid);

  $("#rating-mean-node2-" + nid).html($(mean).html());
  $(mean).empty();
}
});

 
 

Drupal is a registered trademark of Dries Buytaert.