DS-Xtra:Rating Script

From DS-Xtra

Jump to: navigation, search

Overview

This is a modification of Leerwiki's AJAX rating script (which is in turn a modification of masugadesign's script).
It allows you to locate the rating bar anywhere that you can use wikicode by using the following:

<rating />

It will grab the articleid of the page that the rating is displayed on. (thanks EB)
You can also just display the rating results by using this:

<rating-result>x</rating-result>

Where x is the article number of the page who's rating you want to display. Example: DS_Homebrew_Directory

Demo

Rating bar for this article

3.3/5 (119 votes)

Result only bar for this Article

  • Currently 3.25/5


Contents


Installation

As per Leerwiki's instructions.

Download and Extract

  • Download the Unobtrusive AJAX Star Rating Bar from here.
  • Create a directory /Ratings in the extensions Directory.
  • Extract the downloaded files from Masugadesign.com in here.

Add SQL tables

Put the following SQL query in PhpMyAdmin under the Sql tab, at your Database with the MediaWiki tables. Choose your prefix (in this example it is ratings, but for a clear database your can use for example LeerWikiRatings.)

CREATE TABLE `ratings` (
`id` varchar(11) NOT NULL,
`total_votes` int(11) NOT NULL default '0',
`total_value` int(11) NOT NULL default '0',
`used_ips` longtext,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

Edit RateArticle.php

Save this file to your wiki's extension folder as 'RateArticle.php':

<?php
require('Ratings/_drawrating.php'); 
    
$wgExtensionFunctions[] = 'wfRating';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'AJAX RATING BAR',
        'description' => 'Give and display a page`s rating',
        'author' => 'Boudewijn Vahrmeijer Modified by ChuckMcB',
        'url' => 'http://www.leerwiki.nl',
	'version' => '1.9.3/1.9.2/1.8.2'
);

function wfRating() {
        global $wgParser;
        $wgParser->setHook('rating', 'wfRateArticleForm');
        $wgParser->setHook('rating-result', 'wfRateArticleResultForm');
}

function wfRateArticleForm() {
		global $wgTitle;
		$output=rating_bar($wgTitle->mArticleID,5);
		return $output;
}

function wfRateArticleResultForm($AID) {
		global $wgTitle;
		$output=rating_bar($AID,5,1);
		return $output;
}

?>

Edit _drawrating.php

<?php
/*
Page:           _drawrating.php
Created:        Aug 2006
The function that draws the rating bar.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
modified by Boudewijn Vahrmeijer http://www.leerwiki.nl
zwijntje82@hotmail.com
--------------------------------------------------------- */
function rating_bar($id,$units='',$resultonly='') { 

require('_config-rating.php'); // get the db connection info
	
//set some variables
$ip = $_SERVER['REMOTE_ADDR'];
if (!$units) {$units = 10;}

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='$id' ")or die(" Error: ".mysql_error());
$LASTURL=$_SERVER['REQUEST_URI'];
if (mysql_num_rows($query)==0) {
$sql = "INSERT INTO $tableName (`id`,
`total_votes`, `total_value`, `used_ips`)
VALUES ('$id', '0', '0', '')";
$result = mysql_query($sql);
}

$numbers=mysql_fetch_assoc($query);
$count=$numbers['total_votes']; //how many votes total
$current_rating=$numbers['total_value']; //total number of rating added together and stored
$tense=($count==1) ? " vote" : " votes"; //plural form votes/vote

// determine whether the user has voted, so we know how to draw the ul/li
$voted=mysql_num_rows(mysql_query("SELECT used_ips FROM $tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' ")); 

// now draw the rating bar

//$RatOP='<script type="text/javascript" language="javascript" src="extensions/Ratings/js/behavior.js"></script>';
//$RatOP.='<script type="text/javascript" language="javascript" src="extensions/Ratings/js/rating.js"></script>';
$RatOP.='<link rel="stylesheet" type="text/css" href="extensions/Ratings/css/rating.css" />';
$RatOP.='<div class="ratingblock"><div id="unit_long';
$RatOP.=$id;
$RatOP.='"><ul id="unit_ul';
$RatOP.=$id;
$RatOP.='" class="unit-rating" style="width:';
$RatOP.=$unitwidth*$units;
$RatOP.='px;"><li class="current-rating" style="width:';
$RatOP.=@number_format($current_rating/$count,2)*$unitwidth;


$RatOP.='px;">Currently ';
$RatOP.=@number_format($current_rating/$count,2); 
$RatOP.='/';
$RatOP.=$units;
$RatOP.='</li>';

if(!$resultonly) { // show only results

for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
		if(!$voted) { // if the user hasn't yet voted, draw the voting stars 
		
$RatOP.='<li><a href="extensions/Ratings/db.php?url=';
$RatOP.=$LASTURL;
$RatOP.='&j=';
$RatOP.=$ncount;
$RatOP.='&q=';
$RatOP.=$id;
$RatOP.='&t=';
$RatOP.=$ip;
$RatOP.='&c=';
$RatOP.=$units;
$RatOP.='" title="';
$RatOP.=$ncount;
$RatOP.=' out of ';
$RatOP.=$units;
$RatOP.='" class="r';
$RatOP.=$ncount;
$RatOP.='-unit rater">';
$RatOP.=$ncount;
$RatOP.='</a></li>';

	 } 
  }
	$ncount=0; // resets the count

$RatOP.='</ul><p';
if($voted){
$RatOP.=' class="voted"';
} ;
$RatOP.='>';

$RatOP.='<strong>';
$RatOP.=@number_format($current_rating/$count,1);
$RatOP.='</strong>/';
$RatOP.=$units;
$RatOP.=' (';
$RatOP.=$count.$tense;
$RatOP.= ')';

}

$RatOP.= '</p></div></div>';

return($RatOP);


}

?>

Edit localsettings.php

Add this line

require_once("extensions/RateArticle.php");

Todo/Bugs

  • Mediawiki caches pages

Even with the purge code sometimes it takes a manual refresh of a page for the new results to be see.

  • If you rename a page the 'vote' points to the 'old' page
Personal tools