Simple Voting Extension

From Zedomax Wiki

Jump to: navigation, search

Contents

Simple Voting Extension v.0.1

Orignally by Mizanur Rahman, the author of MediaWiki Administrator Hacked by Max Lee from http://zedomax.com/wiki

Installation

Copy and paste the following into your Wiki database through phpMyAdmin

CREATE TABLE `articlerating` (
`articleID` int NOT NULL, 
`articleName` varchar(250) NOT NULL, 
`totalRatings` int NOT NULL, 
`totalPoints` int NOT NULL, 
PRIMARY KEY (`articleID`)
);

CREATE TABLE `articleRatinghistory`
(`articleID` INT NOT NULL, `userName` VARCHAR(50) NOT NULL, `articleRating` TINYINT NOT NULL);

copy saverating.php to the root directory of your wiki

SaveRating.php

<?php

require_once( 'includes/WebStart.php' );

$dbw =& wfGetDB( DB_MASTER );
$dbw->begin();


$sql = "update articlerating set
`totalRatings` = `totalRatings` + 1,
`totalPoints` = `totalPoints` + '$_POST[rating]'
where `articleID` = '$_POST[articleID]' ";
$res = $dbw->query($sql);


if($dbw->affectedRows()==0) {
	$sql = "insert into articlerating set
`articleID` = '$_POST[articleID]',
`articleName` = '$_POST[articleTitle]',
`totalRatings` = 1,
`totalPoints` = '$_POST[rating]' ";
$res = $dbw->query( $sql);
}

$sql = "insert into {$wgDBprefix}articleRatinghistory set
`articleID` = '$_POST[articleID]',
`userName` = '$_POST[userName]',
`articleRating` = '$_POST[rating]'";

$res = $dbw->query( $sql);

$dbw->commit();
header("Location: index.php?title=".$_POST['articleTitle']."&rated=successfully");
?>

Then put the RateArticle.php file in the extensions directory

To activate the extension, include it from your LocalSettings.php with: require_once("extensions/RateArticle.php"); After installation, Voting button should appear at the bottom of every page.

RateArticle.php

<?php
# Simple Voting extension v.0.1
#
# Orignally by Mizanur Rahman, the author of MediaWiki Administrator
# Hacked by Max Lee from http://zedomax.com/wiki 
#
# Copy the below SQL and insert it into your PHPMyAdmin SQL thingee:
/*
CREATE TABLE `articlerating` (
`articleID` int NOT NULL, 
`articleName` varchar(250) NOT NULL, 
`totalRatings` int NOT NULL, 
`totalPoints` int NOT NULL, 
PRIMARY KEY (`articleID`)
);

CREATE TABLE `articleRatinghistory`
(`articleID` INT NOT NULL, `userName` VARCHAR(50) NOT NULL, `articleRating` TINYINT NOT NULL);
*/


# To install it put this file in the extensions directory 
# To activate the extension, include it from your LocalSettings.php
# with: require_once("extensions/RateArticle.php");
#
# copy saverating.php to the root directory of your wiki
# After installation, Voting button should appear at the bottom of every page.

$wgExtensionCredits['parserhook'][] = array(
'name' => 'Voting Extension - 11/16/2007',
'author' => 'Max Lee http://zedomax.com/wiki',
'url' => 'http://zedomax.com/wiki',
'version' => 'v.0.1',
'description' => 'Voting extension for MediaWiki',
);
if ( ! defined( 'MEDIAWIKI' ) ) {

echo "DIE";
die();
}

require_once("DatabaseFunctions.php");

function wfRateArticleForm(&$out) {
global $wgArticle, $wgUser, $wgScriptPath,$wgTitle;
    if ($wgArticle == null) return $out;
    if ($wgArticle->getTitle()->mNamespace != 0 || $wgTitle->getArticleId()==66) return  $out;

if(isset($_GET['rated'])&& $_GET['rated'] == "successfully") {
	$wgArticle->purge();
}
$s = getArticleRating();
if($wgUser->isLoggedIn())
{
	if(!articleRated()) {
		$s .= '<form name="rank" class="inline" method="post"';
		$s .= 'action="'.$wgScriptPath.'/saverating.php">';
		$s .= '<br> Rate this Article:
<input type=radio name=rating value=1> 1
<input type=radio name=rating value=2> 2
<input type=radio name=rating value=3 checked> 3
<input type=radio name=rating value=4> 4
<input type=radio name=rating value=5> 5
<Br>';
		$s .= '<input type=hidden name=articleID value="'.$wgArticle->getID().'">';
		$s .= '<input type=hidden name=articleTitle value="'.$wgArticle->getTitle()->getPrefixedText().'">';
		$s .= '<input type=hidden name=userName value="'.$wgUser->getName().'">';
		$s .= '<input type="submit" name="go" value="Save your Rating" /> </form><br /><br />';
	}
}

 $out->mBodytext.=$s;
return $out;
//echo $s;
}

function getArticleRating() {
global $wgArticle, $wgDBprefix;
$articleID = $wgArticle->getID();
$totalRatings = 0;
$totalPoints = 0;
$averageRating = 0;
$sql = "SELECT totalRatings, totalPoints FROM {$wgDBprefix} articlerating WHERE articleID=$articleID" ;
$res = wfQuery( $sql, DB_READ );
if( $s = wfFetchObject( $res ) ) {
	$totalRatings = $s->totalRatings;
	$totalPoints = $s->totalPoints;
}
if($totalRatings>0) {
	$averageRating = $totalPoints/$totalRatings;
	$yoyoyo = 'Average Rating for this Article is ';
	$yoyoyo .= $averageRating.'</b>';
	$yoyoyo .= ' ( out of 5.00 ) after '.$totalRatings.' Ratings<br /><br />';
	return $yoyoyo;
}
else {
	return "No rating has been done for this Article. <br /> <br />";
}

}


function articleRated()
{
global $wgArticle, $wgUser, $wgDBprefix;
$articleID = $wgArticle->getID();
$userName = $wgUser->getName();
$sql = "SELECT * FROM articleRatinghistory WHERE
articleID=$articleID and userName = '$userName'" ;
$res = wfQuery( $sql, DB_READ );
if( $s = wfFetchObject( $res ) )
{
return true;
}
else
{
return false;
}
}


$wgHooks['BeforePageDisplay'][] = array("wfRateArticleForm");






?>
Personal tools