Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: [ADDON] Article Rating [sNews 1.5]  (Read 35554 times)

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
[ADDON] Article Rating [sNews 1.5]
« on: December 21, 2006, 02:16:47 AM »

Version 1.00

Inspired by this original post
http://snewscms.com/forum/index.php?topic=2495.0
and my concerns about that script mentioned... so, here is my version.

Produces output showing user ratings of articles.
-- restricts voting based on first 3 blocks of ip... limits the liklihood of multiple votes.
-- stores ip numbers per article as text files which are retrieved on call.
-- quantity of ip numbers stored is configurable. Oldest is dropped from the storage file once the quantity is exceeded
-- calculations are performed at voting time only (as opposed to every time the other script is called)
-- stores results per article as an array file.


Images and naming convention.
Will require 11 images for 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5
Name the images with a prefix.. eg star_
Multiply the rating number(see above) by 10 and append to the prefix eg star_15, star_40, star_05, star_00
Don't forget to do the file extension :)  star_15.gif 

css classes in script, and use. (information only)
.snews_rate_wrap   wrapper around all rating content.
.rating            surrounds the rating image, and filler text.
.reviewcount      text and numbers inside the filler text
.rating_button      is the form button for submission.
.rate_item         for the text above the image display
.rate_article_name   for the article name when displayed as part of the rate_item area.

Onwards...
Make a new file.. eg rating.php (must have the php extension) and save.
Copy the code below, and paste into the new file and save.
Place this new file in a new directory in snews root .
Place your image set directory in this same new directory
Alter the user settings...

Code: [Select]
<?php
$ratingsOn
=1;
if(
$ratingsOn==1){
//inspired by rating application from http://www.gr0w.com/articles/code/php_5_star_rating_system_using_hreview/index.php
// Completely rewritten for use in sNews1.5 by philmoz
////////////////////////////////////////
// User settings. //
$sNews_rate_dir ='rating/';   // location of this script.
$sNews_rate_dir_images 'gnomes/';  //name of directory with the image set to use in it.
$sNews_rate_image_prefix 'gnomes-';  // name your images with a prefix, (eg, star_ for star_00.png). Allows you to swap image set quickly.
$sNews_rate_rank_descriptor "gnomes"// describes the means of voting... eg, stars, bullet, houses... whatever the images imply
$sNews_rate_image_ext 'gif';    // change to jpg, gif, png as required for your images.
$sNews_rate_button 'Please Rate'// text for rating form button
$sNews_EOL "\n";   //(may need to alter to  '\r\n' if that is what your system requires.)
$max_ip_stored 5;  // maximum ip numbers to be stored for each rating.Adjust as comfortable. Probably no more than 100
$sNews_not_rated_txt="Not rated";
// Messages -- Write as incomplete messages as they will be completed in the script.
$sNews_not_rated_msg 'No rating for '// will have the article name appended further down.
$sNews_already_rated_msg 'It appears you have already rated ';
$sNews_thank_you_msg 'Thank you for rating '// will have the article name appended further down.
$sNews_not_selected_msg ='You have not selected a rating value for ';// will have the article name appended further down.
// other text
$sNews_rate_generic_text 'this item'// in the event that the article name fails to arrive.
////*********///
// for rated articles, and for short displayed articles. Alter html only.
$sNews_rated_heading='<p class="rate_item"><span class="rate_article_name">'.$sNews_item_name.'</span> rated by viewers as:-</p>';
// for unrated full articles.  Alter html only.
$sNews_unrated_heading'<p class="rate_item">Please rate <span class="rate_article_name">'.$sNews_item_name.'</span></p>';
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//System vars //
$sNews_rate_results=$sNews_rate_dir.'results.res.php'// results file.
$sNews_rate_ipfile=$sNews_rate_dir.'item_'.$sNews_rate_id.".rating"// unique ip files.
if(!isset($sNews_rate_id)) {$sNews_rate_id='1';}
if(!isset(
$sNews_item_name)) {$sNews_item_name=$sNews_rate_generic_text;}
if (
file_exists($sNews_rate_results)){include($sNews_rate_results);}
//set vars to initial values.////////////////////
$sNews_rate_txt ='';
$sNews_stars ="";
$sNews_rate_txt="";
$rate_rating=0;
$rate_value=0;
$rater_msg="";
$sNews_rate_images=$sNews_rate_dir.$sNews_rate_dir_images.$sNews_rate_image_prefix;
////START WORKING/////
$rater_ip2 $rater_ip getenv("REMOTE_ADDR"); //voters ip
$ip_sepsexplode('.',$rater_ip);$rater_ip =$ip_seps[0].".".$ip_seps[1].".".$ip_seps[2].".";//truncate ip to first 3 blocks.
if (is_file($sNews_rate_ipfile)) {
$rate_array=explode($sNews_EOL,file_get_contents($sNews_rate_ipfile));
$count_rates=count(array_count_values($rate_array));
if (
in_array($rater_ip,$rate_array)) {
$rated='true';
$rater_msg=$sNews_already_rated_msg;
}}
// posting a rating. // voting form should be unavailable if already ip identified.
if (isset($_REQUEST["sNewsrate".$sNews_rate_id]) && !isset($_REQUEST["rating_".$sNews_rate_id])){
$rater_msg$sNews_not_selected_msg;
$rated='false';
}elseif(
$rated=='false'&& isset($_REQUEST["sNewsrate".$sNews_rate_id])){
if (!isset($rating_results[$sNews_rate_id])){$rate_value=0;$rate_numbers=0;$rate_total_score=0;
}else{
$rate_numbers $rating_results[$sNews_rate_id][0];$rate_value $rating_results[$sNews_rate_id][1];$rate_total_score$rating_results[$sNews_rate_id][2];}
$new_numbers=$rate_numbers+1;
$rater_vote $_REQUEST["rating_".$sNews_rate_id];
$new_score=$rate_total_score+$rater_vote[0];
$new_values=$new_score/$new_numbers;
if ($new_values>(intval($new_values)+0.85)) {$new_values=intval($new_values)+1;// high biased round up
}elseif ($new_values<(intval($new_values)+0.35)){$new_values=intval($new_values);// high biased round down
}else{$new_values=intval($new_values)+0.5; //if in range of  x.35>Vote>x.85 make it x.5

$rating_results[$sNews_rate_id][0]=$new_numbers;
$rating_results[$sNews_rate_id][1]=$new_values;
$rating_results[$sNews_rate_id][2]=$new_score;
//write results file.
$writer "<?php\n";
foreach (array_keys($rating_results)as $key){
$writer .= "\$rating_results[$key]=array('".$rating_results[$key][0]."','".$rating_results[$key][1]."','".$rating_results[$key][2]."');\n";}
$writer .= "?>
\n";
// file_put_contents($sNews_rate_results,$writer); // file_put_contents is php5 only...
$f=@fopen($sNews_rate_results,"w+");
   fwrite($f,$writer);
   fclose($f);
//write ip file.
if (($max_ip_stored)<=$count_rates){  // checks for max ips
array_shift($rate_array);
$count_rates=$count_rates-1;
}
$rate_array[$count_rates]=$rater_ip;
// file_put_contents($sNews_rate_ipfile, implode($sNews_EOL, $rate_array));
$f=@fopen($sNews_rate_ipfile,"w+");
$write_ips = implode($sNews_EOL, $rate_array);
   fwrite($f,$write_ips);
   fclose($f);
$rater_msg= $sNews_thank_you_msg;
$rated='true';
}

// SHOW RATING RESULT
if (!isset($rating_results[$sNews_rate_id])|| !is_file($sNews_rate_ipfile)){
$rate_value=0;$rate_numbers=0;
}else{
$rate_numbers = $rating_results[$sNews_rate_id][0];
$rate_value = $rating_results[$sNews_rate_id][1];}
if ($rate_value <= 0 || !isset($rating_results[$sNews_rate_id])){
$sNews_rate_txt=$sNews_not_rated_txt;
$digits = '00';}else{
$sNews_rate_txt = $rate_value;}
if ($rate_value>0){
if ($rate_value=='0.5' ){$digits = '05';}
else{$digits = $rate_value*10;}
}
// Assign image
$sNews_stars = $sNews_rate_images.$digits.'.'.$sNews_rate_image_ext;
// Output
$echo= $sNews_EOL.$sNews_EOL.'<div class="snews_rate_wrap">'.$sNews_EOL;
$echo.= '<form method="post" action="'.db('website').$categorySEF.'/'.$r['seftitle'].'/">';
if($rated=="true" && isset($rating_results[$sNews_rate_id])){$echo.= $sNews_rated_heading;}else{$echo.= $sNews_unrated_heading;}
$echo.= '<div>';
$echo.= '<span  class="rating"><img src="'.$sNews_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rate_value.' stars" /> Average rating:~
</span><span class="reviewcount">'.$sNews_rate_txt.'</span>';
if ($sNews_rate_txt!=$sNews_not_rated_txt){
$echo.= ' <span class="rating">'.$sNews_rate_rank_descriptor.' from </span><span class="reviewcount">'.$rate_numbers.'</span><span class="rating"> votes.</span>
';}
$echo.= '</div>'.$sNews_EOL;
if($rater_msg=="" && $rater_msg!=$sNews_already_rated_msg && $rated=="false" ||$rater_msg==$sNews_not_selected_msg){
$echo.= '<div style="clear:both;">';
$echo.= '<label for="rate1_'.$sNews_rate_id.'"><input type="radio" value="0" name="rating_'.$sNews_rate_id.'[]" id="rate1_'.$sNews_rate_id.'" />Hmm?</label> '.$sNews_EOL;
$echo.= '<label for="rate2_'.$sNews_rate_id.'"><input type="radio" value="1" name="rating_'.$sNews_rate_id.'[]" id="rate2_'.$sNews_rate_id.'" />Poor</label> '.$sNews_EOL;
$echo.= '<label for="rate3_'.$sNews_rate_id.'"><input type="radio" value="2" name="rating_'.$sNews_rate_id.'[]" id="rate3_'.$sNews_rate_id.'" />Fair</label> '.$sNews_EOL;
$echo.= '<label for="rate4_'.$sNews_rate_id.'"><input type="radio" value="3" name="rating_'.$sNews_rate_id.'[]" id="rate4_'.$sNews_rate_id.'" />Good</label> '.$sNews_EOL;
$echo.= '<label for="rate5_'.$sNews_rate_id.'"><input type="radio" value="4" name="rating_'.$sNews_rate_id.'[]" id="rate5_'.$sNews_rate_id.'" />Very Good</label> '.$sNews_EOL;
$echo.= '<label for="rate6_'.$sNews_rate_id.'"><input type="radio" value="5" name="rating_'.$sNews_rate_id.'[]" id="rate1_'.$sNews_rate_id.'" />Excellent</label> '.$sNews_EOL;
$echo.= '<input type="hidden" name="rs_id" value="'.$sNews_rate_id.'" />'.$sNews_EOL;
$echo.= '<input class="rating_button"type="submit" name="'.'sNewsrate'.$sNews_rate_id.'" value="'.$sNews_rate_button.'" />'.$sNews_EOL;
$echo.= '</div>'.$sNews_EOL;}
if($rater_msg!="") $echo.= '<div style="clear:both;"><p class="rate_item">'.$rater_msg.'<span class="rate_article_name">'.$sNews_item_name.'</span> ('.$rater_ip2.')</p></div>'.$sNews_EOL;
$echo.= '</form>'.$sNews_EOL;
$echo.= '</div>'.$sNews_EOL.$sNews_EOL;
echo $echo;
}
?>
To plug into snews, locate
//CENTER
then find the case ($tag != 'readmore' .. after infoline
Code: [Select]
if ($infoline == true) {
foreach ($tag as $tag) {
...[snipped for brevity]...
case ($tag != 'readmore' && $tag != 'comments' && $tag != 'edit'): echo $tag; break;
}
}
}
else if ($
and add the extra
Code: [Select]
if ($infoline == true) {
foreach ($tag as $tag) {
...[snipped for brevity]...
case ($tag != 'readmore' && $tag != 'comments' && $tag != 'edit'): echo $tag; break;
}
/*rating script -- */ }$sNews_rate_id=$r['id'];$sNews_item_name=$r['title'];$rated="true";include("rater/rater.php"); ///place rated articles here.
}
else if ($
Alter "rater/rater.php" to your rating directory, and the name you called the file...
Then,
A bit further down,
Code: [Select]
if ($infoline == true) {
$tag = explode(',', tags('infoline'));
...[snipped for brevity]...
default: echo $tag;
}
}
}
else if ($_SESSION[db('website').'Logged_In'] == 'True') {echo '<p>'.$edit_link.'</p>';}
}
}
and alter the end bit to this...
Code: [Select]
if ($infoline == true) {
$tag = explode(',', tags('infoline'));
...[snipped for brevity]...
default: echo $tag;
}
/*rating script -- */ }$sNews_rate_id=$r['id'];$sNews_item_name=$r['title'];$rated="false";include("rater/rater.php");
}
else if ($_SESSION[db('website').'Logged_In'] == 'True') {echo '<p>'.$edit_link.'</p>';}
}
}
Again, alter "rater/rater.php" to your rating directory, and the name you called the file...

Some graphic sets can be downloaded from my site... http://www.fiddlenfolk.com/downloads/downloads/
Updates:~
Tweaked instructions -- no code change
« Last Edit: August 28, 2007, 04:17:29 AM by Phil Martin (Philmoz) »
Logged
Of all the things I have lost, it is my mind that I miss the most.

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4957
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
[ADDON] Article Rating [sNews 1.5]
« Reply #1 on: December 21, 2006, 08:34:24 AM »

God almighty, Phil... You're a productive bugger :lol: Good work, and kudos to ye for making this section very much alive.
Logged
My sNews site
"It's only dead fish that goes with the flow... "
Updated, online again - Free designs

4Sandrix

  • Hero Member
  • *****
  • Karma: -13
  • Posts: 613
    • http://fedrick.deviantart.com
[ADDON] Article Rating [sNews 1.5]
« Reply #2 on: December 21, 2006, 12:13:43 PM »

Very cute mod :) thx  ;)
Logged
I'm kind of like Han Solo always strokin' my own Wookiee
I'm the root of all that's evil yeah but you can call me Cookie

BBBB

  • Newbie
  • *
  • Karma: 0
  • Posts: 32
[ADDON] Article Rating [sNews 1.5]
« Reply #3 on: December 23, 2006, 12:39:49 AM »

This mod works great even in php4. Thanks! ;)
Logged

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
[ADDON] Article Rating [sNews 1.5]
« Reply #4 on: December 23, 2006, 12:57:11 AM »

Quote from: BBBB
This mod works great even in php4. Thanks! ;)
Yes, my test server is php5. When I got it working, chuffed as I was, I put it online.
It failed... online server is php4  :|
You will note in the code that there is a part I have commented out -- that bit only works for php5... ;) and is used to write to the file (php4 version directly after it).
Logged
Of all the things I have lost, it is my mind that I miss the most.

harry

  • Newbie
  • *
  • Karma: 1
  • Posts: 37
    • http://www.thebusinesscardshop.net/order/
[ADDON] Article Rating [sNews 1.5]
« Reply #5 on: December 27, 2006, 11:51:01 AM »

Phil,

Have tried playing around with this but no luck so far. Am trying to get different appearances on the category page and on the actual article page (where the user can rate.).

On the category i only want to display the stars with  rating. nothing else.
And on the article page, i want to display the whole ratings box, with heading etc.

Any help in how i can go about this. At the moment the closest i can get is just having all the information the same on both pages.

Thanks

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
[ADDON] Article Rating [sNews 1.5]
« Reply #6 on: January 04, 2007, 11:30:28 PM »

am on holidays... ;) about 1500 miles from home and test server.
Unless someone is willing to have a go, this will have to wait till late feb before I can have a look at it.
Logged
Of all the things I have lost, it is my mind that I miss the most.

michael kennedy

  • Full Member
  • ***
  • Karma: 3
  • Posts: 210
    • SpektreDesign
[ADDON] Article Rating [sNews 1.5]
« Reply #7 on: January 12, 2007, 10:12:34 PM »

Are there any options that can be set with this mod?  Such as, lets say I only want the admin to be able to rate an article himself.  Or, for example, can he turn off/disable the ratings on a per-article basis?

Keyrocks

  • Doug
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 451
  • Posts: 6290
  • Semantically Challenged
    • snews.ca
[ADDON] Article Rating [sNews 1.5]
« Reply #8 on: January 12, 2007, 11:03:19 PM »

Quote from: philmoz
am on holidays... ;) about 1500 miles from home and test server.
Unless someone is willing to have a go, this will have to wait till late feb before I can have a look at it.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Geox

  • Newbie
  • *
  • Karma: 0
  • Posts: 3
[ADDON] Article Rating [sNews 1.5]
« Reply #9 on: January 20, 2007, 06:07:50 PM »

I am taking this error:
Warning: fwrite(): supplied argument is not a valid stream resource in /ratings.php on line 81

Warning: fclose(): supplied argument is not a valid stream resource in /ratings.php on line 82

Warning: fwrite(): supplied argument is not a valid stream resource in /ratings.php on line 92

Warning: fclose(): supplied argument is not a valid stream resource in /ratings.php on line 93

can someone help?
Logged

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
[ADDON] Article Rating [sNews 1.5]
« Reply #10 on: January 20, 2007, 06:45:16 PM »

If I remember correctly, this error appears if the rater folder's permissions are not set to 777 -- permissions need to be 777 for the script to work correctly.

michael kennedy

  • Full Member
  • ***
  • Karma: 3
  • Posts: 210
    • SpektreDesign
[ADDON] Article Rating [sNews 1.5]
« Reply #11 on: January 26, 2007, 02:12:19 AM »

Thanks philmoz, this is a awesome mod.  :cool:

Check this out though, do you think it would be possible (when you get back) to make it more like this?

http://www.komodomedia.com/blog/

Most web sites allow the user to actually click on the stars and rate an article. (Like on netflix.com) the stars may also highlight a darker color on hover. Just an suggestion.

dracko

  • Jr. Member
  • **
  • Karma: 0
  • Posts: 58
    • http://www.gritala.com
[ADDON] Article Rating [sNews 1.5]
« Reply #12 on: January 26, 2007, 07:07:09 PM »

hey mike, i used that tutorial and created a star rating sistem for snews, check this out

http://www.solucija.com/forum/viewtopic.php?id=3150
Logged

michael kennedy

  • Full Member
  • ***
  • Karma: 3
  • Posts: 210
    • SpektreDesign
[ADDON] Article Rating [sNews 1.5]
« Reply #13 on: March 09, 2007, 07:46:34 PM »

This mod works with 1.5.31 yes?

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
[ADDON] Article Rating [sNews 1.5]
« Reply #14 on: March 09, 2007, 10:00:26 PM »

yes,
... but haven't looked at possile alterations that are mentioned further up this thread.
Logged
Of all the things I have lost, it is my mind that I miss the most.
Pages: [1] 2 3