Jump to content

Rudimentary 5 star rating system


onjegolders
 Share

Recommended Posts

Just though I'd post in case anyone else was interested in creating a 5 star rating system.

It is very rudimentary I should point out and am sure there are thousands of better ways but it seems to get the job done OK for me.

It's only using cookies to check if someone's already voted so it's not at all foolproof!

If any advanced users have any pointers to improve it, that would be great!

<?php include("./header_plain.inc"); ?>
<div id="new_blog_view" class="row">
<div id="blog_header">
 <a href="<?php echo $config->urls->root; ?>"><img src="<?php echo $config->urls->templates; ?>styles/images/ag_logo_2.png" alt="" /></a>
 <h4><a href="<?php echo $page->parent->url; ?>">From the Blog</a></h4>
</div>
<h1><?php echo $page->title; ?></h1>
<h6><?php echo $page->entry_date; ?></h6>
<?php if ($page->image) { ?>
 <img src="<?php echo $page->image->url; ?>" alt="<?php echo $page->title; ?>" id="blog_image" />
<?php } ?>
<?php $class = "new_blog_view_content";
  if (!$page->image) {
   $class .= " divide";
  }
  ?>
<div class="<?php echo $class; ?>">
 <?php echo $page->body; ?>
</div>

<?php echo $page->comments->render(); ?>
<?php echo $page->comments->renderForm(); ?>
<?php if ($page->vote_count > 0) {
 $vote_average = round($page->vote_score / $page->vote_count, 2);
  if ($vote_average > 0 && $vote_average < 1) {
   $url = $config->urls->templates . "styles/images/stars_0.png";
  }
  elseif ($vote_average >= 1 && $vote_average < 2) {
   $url = $config->urls->templates . "styles/images/stars_1.png";
  }
  elseif ($vote_average >= 2 && $vote_average < 3) {
   $url = $config->urls->templates . "styles/images/stars_2.png";
  }
  elseif ($vote_average >= 3 && $vote_average < 4) {
   $url = $config->urls->templates . "styles/images/stars_3.png";
  }
  elseif ($vote_average >= 4 && $vote_average < 5) {
   $url = $config->urls->templates . "styles/images/stars_4.png";
  }
  elseif ($vote_average = 5) {
   $url = $config->urls->templates . "styles/images/stars_5.png";
  }
 ?>
 <h5>Current rating:</h5>
 <p><img src="<?php echo $url; ?>" width="100" height="25" alt="<?php echo $page->name . " - " . $vote_average . " stars"; ?>"/><span> (<?php echo $vote_average . "/5"; ?>)</span></p>
<?php } ?>
<?php
$rating = $page->vote_score;
$votes = $page->vote_count;
$out = "";
if (isset($input->post->submit_rating) && $input->post->test == "") {
 $out = "<h5>Thank you for rating!</h5>";
 $new_rating = $input->post->rating + $rating;
 $votes++;
 $page->of(false);
 $page->set("vote_score", $new_rating);
 $page->set("vote_count", $votes);
 $page->save();
 $voted = true;
 $page->of(true);
 // set cookie to ensure only 1 vote (not foolproof!)
 setcookie('voted',$page->name,time() + (86400 * 7));
 echo $out;
?>
<?php } elseif(isset($_COOKIE['voted']) && $_COOKIE['voted'] == $page->name) {
 echo "<br>You have already voted, thank you!";
} else {
 ?>
  <div id="rating_box">
   <h4>Please feel free to rate this article!</h4>

  <form action="" method="post">
   <select name="rating" id="rating">Your rating
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 <option value="5">5</option>
   </select>
   <input type="text" id="test" name="test">
   <input type="submit" id="rating_submit" name="submit_rating" value="Submit rating">
  </form>
  </div>

<?php } ?>
<a href="<?php echo $pages->get("/blog/")->url; ?>" id="back_blog_link">← Return to Blog</a>
</div>
<?php include("./footer_plain.inc"); ?>
  • Like 1
Link to comment
Share on other sites

Thanks for posting this. I've thought a few times that we should have a five-start type voting module at some point and this looks like a good start on that. One thing I want to mention is that you might want to add validation for your $input->post->rating, because it looks like it should be limited to 1-5 and it appears that someone could manipulate the post variable to have any number in it, whether "-500" or "99999", etc. So I'd suggest adding some validation, perhaps something like this:

$r = (int) $input->post->rating;
if($r > 0 && $r <= 5) $new_rating = $page->vote_score + $r; 
 else $new_rating = $page->vote_score; 
Link to comment
Share on other sites

  • 1 month later...

Yep - I think a jQuery rating system (as Luis has linked to) as part of a module as ryan says would be great in the longer term and it would be easy to rate ANY content since it's all Pages and just a case of adding the functionality to each template ;)

Obviously it would need to take into consideration things like multiple votes from one IP address, flood control (set by session and/or cookie) so someone can't spoof different IP addresses and spam it but ultimately all the checks in the world won't stop someone from finding a way around multiple voting unless you require them to log in first - that's a bit much for most sites' needs though!

In the meantime, this code looks great and does the job nicely :)

Link to comment
Share on other sites

  • 3 weeks later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...