I have the votingapi and simplevote modules working smoothly on a 4.7 install, but I'm working towards having similar rating functionality on comments.

If I understand the documentation correctly, votingapi provides the possibility to do this, correct?

I'm new to the system and was hoping to modify simplevote to work on comments as a first project, but I'm not sure where to start. I was looking at rewriting the simplevote_nodeapi function, but I'm not sure how to do the node->comments "translation". If someone had an example of this, or any other pointers, I'd really appreciate it.

Comments

monjohn’s picture

I was actually trying something very similar. I tried to do use hook_comment but that is only called when a comment is being created or changed, not viewed.

It sounds like I am trying to do something a bit different, though. I want people to be able to vote in the comments on the node itself, as in Amazon.com.

I have tried to create a new templete (using phptemplate), such as comment-simplevote.tpl.php.

I inserted a line to call the voting function.

 print theme('simplevote_widget', $nid);

It is calling the widget, but it is not passing the node id, so I am not getting the right url address to register the vote.

I don't know if this would help you. Perhaps some good samaritan might come along and help us both.

Near Raleigh, NC, USA

eaton’s picture

After reading your post, I added a few quick internal modifications to the SimpleVote module. It's now capable of handling voting for any type of content, not just nodes.

To stick a voting widget in your theme, install the latest CVS version of Simplevote.module, and use the following code:

<?php
 print theme('simplevote_widget', 'node', $node->nid);
?> 

To rate a comment, use the following:

<?php
 print theme('simplevote_widget', 'comment', $comment->cid);
?> 

In fact, you could do that almost anywhere in Drupal. You might override the theme functions for rendering aggregator items, and include a call to the widget with 'aggregator-item' and the id of the aggregator items as the parameters. That would immediately enable rating of all of the news feed stories coming in. Hmmmmmmmmmm...

If you feel daring, you can open up simplevote.module and add the following function to it:

function simplevote_comment(&$comment, $op) {
  switch ($op) {
    case 'view':
      $comment->comment = theme('simplevote_widget', $comment->cid, 'comment') . $comment->comment;
      break;
  }
}

That will add the ratings widget to ALL comments. The theme function could probably use some work, too, as comment votes should be displayed a bit differently. They look goofy when the comments are only a line or so long.

--
Jeff Eaton | I heart Drupal.

--
Eaton — Partner at Autogram

monjohn’s picture

I am tryig to do something a bit different than the first post. I want the widget to appear in a comment, but the vote to be cast on the node. I have hacked the simplevote module a bit, so the solution doesn't have to fit directly with the current version.

I added a function that gets users vote. And i have put the line

print theme('ratings_user_results', $node->nid, $uid);

This calls the widget and displays the individual voter's vote. But the node id is not being passed, as it does not show up in the url?

What else could I use other than $node->id?

Near Raleigh, NC, USA

jl79’s picture

Eaton -

Thank you. Just excellent.

One thing to note, in the theme calls above, the paramaters are still switched around (I noticed your comment on CVS), which results in votes not set.

<?php
 print theme('simplevote_widget', $comment->cid, 'comment');
?>

Just in case someone reading this missed it...

The idea of adding to the aggregator is excellent one - I hadn't gotten to that point yet, but it's on my to-do list now.

Thanks again.

Monjohn -

I'm not sure how to go about what you're trying to do (yet), but I would also like to see the answer - seems useful.

antarcticlemur’s picture

I've been waiting for a mod like this to be published since Sept of 2005.

Brilliant.

This is exactly what popular blogs based on Drupal require - a way for miscreant commenters to be 'shamed' and good commenters to be rewarded.

Glad I moved my site from WordPress when I had the chance.

Thanks again.

m_wright’s picture

Is there a easy change to make simplevote appear on the teaser for a node?

Thanks
Martin

monjohn’s picture

The last function in simplevote.module tests to see if the node is to display the teaser, if not it inserts the widget:

function simplevote_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':
      if ($teaser == false) {
        $node->body = theme('simplevote_widget', $node->nid, 'node') . $node->body;
      }
      break;
  }
}

try replacing with:

function simplevote_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':
      if ($teaser == false) {
        $node->body = theme('simplevote_widget', $node->nid, 'node') . $node->body;
      } elseif ($teaser == true {
        $node->teaser = theme('simplevote_widget', $node->nid, 'node') . $node->teaser;
      break;
  }
}

monjohn
www.claytonpc.com
m_wright’s picture

I just needed to add in a couple of brackets. And it works great. Thanks!

function simplevote_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':
      if ($teaser == false) {
        $node->body = theme('simplevote_widget', $node->nid, 'node') . $node->body;
		}
		elseif ($teaser == true) {
        $node->teaser = theme('simplevote_widget', $node->nid, 'node') . $node->teaser;
      	break;
  		}
		}
}
Roadskater.net’s picture

thanks for the help on this. i had been wandering in the dark, learning as i went, but i still missed a couple of key items. below is a hack just for me so i changed some stuff to disconfuse me, but it may help another newbie like me put up a x.x out of 5 rating with a count of the number of votes in brackets.

i'm still working on a way to show the vote AND some how indicate they haven't voted yet, either in one control or using two a la the voting module that uses flash.

here's the hack...i know zero php! sorry!

thanks again to all.

function theme_simplevote_widget($cid, $type) {
  // Get the current vote. It should come in as a percentage,
  $vote = votingapi_get_voting_result($type, $cid, 'percent', 'vote', 'average');
  
  if ($vote) {
    $stars = round($vote->value);
  }
  else {
    $stars = 0;
  }
  $score = number_format(round($stars / 20, 1), 1, '.', '');

  $count = votingapi_get_voting_result($type, $cid, 'percent', 'vote', 'count');
  $votes = $count->value;
  $output = '<div class="simplevote_widget">' . $score . ' [' . round($votes) . ']';

  for ($i = 20; $i <= 100; $i += 20) {
    $output .= theme('simplevote_icon', $type, $cid, $i, $stars >= $i);
  }
  $output .= '</div>';
  return $output;
}
Roadskater.net’s picture

hey thanks again to all for this thread. i don't know if this is of use to anyone or if it's any faster or better, but i made a mod and thought i'd share it.

my little star graphics are 14x14, not whatever the little stars were originally, so that explains some of the difference in numbers.

anyway, i made a single graphic called starsemptyfullhover.png that had a grey then a green then a magenta star object, representing empty, full and hover stars. this was instead of two graphics in the original module. my graphic is 42x14 WxH. i use code below to build the vote display.

i'm not knocking anything about the original and i can't say this is any better. i wondered if this idea might be ajaxable somehow. i don't know a thing about it. i'll learn. i've studied some of the mediumvote code but it's crypto to me.

another thought came to mind about how to represent partial star objects. i wondered if it might work to make a 140x14 with five green (full) stars on the left and five grey (empty) stars on the right. it seems one could grab as many pixel columns from the left as needed to get green wheels, then grab 70 minus that many pixel columns from the far right for the empty ones, abutting the two. at this point i think i might go with 10x10 stars for a 100x10 image. just a thought, and that leaves out hovering and some other issues. a single hover star, an eleventh star, could fit in between the left and right dual pairs of five if there was benefit in doing this with one graphic.

the same partial pixel grab should be possible even in the current iterative routine perhaps, as well. in other words, if the score is 3.5, perhaps on the fourth pass one could display 7 leftmost full pixel columns, then do 7 rightmost grey pixel columns (using my 14x14 setup as an example), then finish with empty stars if needed.

if i follow up on any of these i'll post results.

in the present state, i wonder if there's a way to make hover tooltip text that says either "please vote" or "you may change your vote of x.x" or whatever. i will learn these things.

here's the simplevote.css code such as i'm able to write it. i'm assuming those last two could be expressed more compactly as well, but i'm not very aware on php, css, mysql. yet.

.simplevote_widget {
  float:left;
  padding:1px;
  margin:1px;
  border:0px;
}

.vote-on, .vote-off {
  float:left;
  width:14px;
  height:14px;
  border:0px white solid;
  text-decoration: none;
}

.vote-on {
  background: url(staremptyfullhover.png) no-repeat -14px 0px;
}

.vote-off {
  background: url(staremptyfullhover.png) no-repeat -0px 0px;
}

a.vote-on:hover {
  background: url(staremptyfullhover.png) no-repeat -28px 0px;
}

a.vote-off:hover {
  background: url(staremptyfullhover.png) no-repeat -28px 0px;
}

i haven't tested but i assume from looking at some of the css above that i could replace the last two hovers with...

a.vote-on:hover, a.vote-off:hover {
  background: url(staremptyfullhover.png) no-repeat -28px 0px;
}

thanks again.

eaton’s picture

I'm looking at opportunities for a major new version of SimpleVote -- mostly to demonstrate some of the new capabilities VotingAPI will be getting, like cleanly integrated Actions support, and so on.

One of the things I want to do is make sure that SimpleVote supports a better voting widget -- something that degrades cleanly in browsers with sub-optimal CSS support. I've created a simple FormAPI widget that displays SimpleVote's five stars as radio buttons; clicking 'submit' casts the vote. The next step would be some sort of CSS/Javascript/AJAX magic to replace those radio butons with pretty rollover stars on browsers that support it.

It's a busy month or so for me right now, but I'm watching some of these modifications closely, and hope that the best of the bunch can be incorporated into the official release.

--
Jeff Eaton | I heart Drupal.

--
Eaton — Partner at Autogram

broncos88’s picture

is it possible in Simplevote to allow anonymous user to vote?