<?php if (!defined('PmWiki')) exit();

// Borrowed front-end css-only star format from a variety of sources stemming from the tutorials at
// http://komodomedia.com/

// All credit and brilliance for the CSS logic goes to them.

// All labor to integrate this ratings front-end with the PmWiki back-end goes to Crisses (XES)

/*

This script add a page-rank to PmWiki.  Individual page popularity, or the popularity of the item, concept, article, etc. presented on the page may be determined.

Usage:

Put pagerankstars.php in your cookbook folder.

Add to config.php:

	include_once("$FarmD/cookbook/pagerankstars.php");

Default use requires an authenticated user system. If you would like to use the PageRank in a specific group, add the following to GroupHeader or to GroupFooter:
	(:if authid :)
	(:PageRankStars:)
	(:ifend:)

To allow votes regardless of login

This behavior accumulates votes, regardless of whether the user is logged in or not. If a user votes again, it's counted as an additional vote.

In config.php, and BEFORE including star_ratings.php:

	$PRS_Vote['exclusive'] = false;

Add to GroupHeader or GroupFooter:

	(:PageRankStars:)
	
To display the page rating

This displays the page rating only if the page is rated, and is added to the GroupHeader or GroupFooter. A visual star interpretation will come in a later release.
	(:if ! equal {*$RatingAverage} "" :)
	Rating: {*$RatingAverage} / 5 ({*$RatingCount} votes)
	(:ifend:)


********************
Release Notes
********************
0.1 beta - Premature release
0.2 beta - fixed a bunch of bugs
0.3 beta - fixed another bug
0.4 beta - added type check for vote value, since this may be entered by the user.
*/

$HandleActions['PRS_ratings'] = 'PRS_SaveRatings';

SDVA($PRS_Vote, array (
'wording' => array ("none" => "no votes cast", "one" => "vote cast", "many" => "votes cast", "use" => "no votes cast"),
'star_width' => 20,
'current' => 2,
'exclusive' => true,
'max_stars' => 5,
'field' => 'image',
'hidden' => true,
'vote_image_url' => $PubDirUrl . "/images/pagerankstars.gif"));

$FmtPV['$RatingCount'] = '$page["RatingCount"]';
$FmtPV['$RatingAverage'] = 'number_format($page["RatingAverage"],2)';
$FmtPV['$RatingSum'] = '$page["RatingSum"]';
//$FmtPV['$RatingMessage'] = 'if ($page["Rating_$Author"]) {"Your previous rating is $page["Rating_$Author"].";} else {"You have never voted.";}';
 
$HTMLStylesFmt[] = "
/*             styles for the star rater                */
.star-rating{
	list-style:none;
	margin: 0px;
	padding:0px;
	width: " . $PRS_Vote['star_width'] * 5 . "px;
	height: " . $PRS_Vote['star_width'] . "px;
	position: relative;
	background: url(" . $PRS_Vote['vote_image_url'] . ") top left repeat-x;
}

.star-rating ul{
	top: 0px;
}
.star-rating li{
	padding:0px;
	margin:0px;
	/*\*/
	float: left;
	/* */
}
.star-rating li a{
	top: 0px;
	display:block;
	width:" . $PRS_Vote['star_width'] . "px;
	height: " . $PRS_Vote['star_width'] . "px;
	text-decoration: none;
	text-indent: -9000px;
	z-index: 20;
	position: absolute;
	padding: 0px;
}

.star-rating li.current-rating{
	background: url(PRS_Vote['vote_image_url']) left bottom;
	position: absolute;
	height: " . $PRS_Vote['star_width'] . "px;
	display: block;
	text-indent: -9000px;
	z-index: 1;
}
.star-rating li a:hover{
	background: url(" . $PRS_Vote['vote_image_url'] . ") left bottom;
	z-index: 1;
	left: 0px;
}
.star-rating a.one-star{
	left: 0px;
}
.star-rating a.one-star:hover{
	width:" . $PRS_Vote['star_width'] . "px;
}
.star-rating a.two-stars{
	left:" . $PRS_Vote['star_width'] . "px;
}
.star-rating a.two-stars:hover{
	width: " . $PRS_Vote['star_width'] * 2 . "px;
}
.star-rating a.three-stars:hover{
	width: " . $PRS_Vote['star_width'] * 3 . "px;
}
.star-rating a.three-stars{
	left: " . $PRS_Vote['star_width'] * 2  . "px;
}
.star-rating a.four-stars{
	left: " . $PRS_Vote['star_width'] * 3 . "px;
}	
.star-rating a.four-stars:hover{
	width: " . $PRS_Vote['star_width'] * 4 . "px;
}
.star-rating a.five-stars{
	left: " . $PRS_Vote['star_width'] * 4 . "px;
}
.star-rating a.five-stars:hover{
	width: " . $PRS_Vote['star_width'] * 5 . "px;
}
		";


//<p>Rating: <strong>".@number_format($current_rating/$count,2)."</strong> {{$count} {$wording} cast} <br />You have previously voted.</p>
Markup('PageRankResults', 'directive', '/\\(:PageRankResults:\\)/e', Keep("
<div class='rating'>
		<ul class='star-rating'>
		<li class='current-rating' style='width:" . $PRS_Vote['star_width'] * 5 . "px;'>Current rating.</li>
			<li class='one-star'>1</li>
			 <li class='two-stars'>2</li>
			 <li class='three-stars'>3</li>
			 <li class='four-stars'>4</li>
			 <li class='five-stars'>5</li>
		</ul>
		</div>
			"));

Markup('PageRankStars', 'directive', '/\\(:PageRankStars:\\)/e', Keep("
<div class='rating'>
		<p>Rate this page:</p>
		<ul class='star-rating'>
	<li class='current-rating' style='width:" . @number_format($RatingSum/$RatingCount,2)*$PRS_Vote['star_width'] ."px;'>Current rating</li>
			<li><a href='" . $ScriptUrl . "?n=" . $pagename . "&action=PRS_ratings&vote=1' title='Rate this 1 star out of 5' class='one-star'>1</a>
			</li>
			<li><a href='" . $ScriptUrl . "?n=" . $pagename . "&action=PRS_ratings&vote=2' title='Rate this 2 stars out of 5' class='two-stars' >2</a>
			</li>
			<li><a href='" . $ScriptUrl . "?n=" . $pagename . "&action=PRS_ratings&vote=3' title='Rate this 3 stars out of 5' class='three-stars' >3</a>
			</li>
			<li><a href='" . $ScriptUrl . "?n=" . $pagename . "&action=PRS_ratings&vote=4' title='Rate this 4 stars out of 5' class='four-stars' >4</a>
			</li>
			<li><a href='" . $ScriptUrl . "?n=" . $pagename . "&action=PRS_ratings&vote=5' title='Rate this 5 stars out of 5' class='five-stars' >5</a>
			</li>
		</ul>
		
		</div>
"));


//<p>Rating: <strong>".@number_format($RatingAverage,2)."</strong> ({$RatingCount} " . $PRS_Vote['wording']['use'] . " )</p>

function PRS_SaveRatings () {
	global $pagename, $PRS_Vote, $MessagesFmt,$Author;
	
	// validate vote info
	if ((!is_numeric($_GET['vote'])) || $_GET['vote'] < 1 || $_GET['vote'] > $PRS_Vote['max_stars']) {
		$MessagesFmt[] = "Only a vote from 1 to $PRS_Vote[max_stars] may be assigned.";
		HandleBrowse($pagename);
		exit();
	}
	
	// Read the old page, set up the new page
	$oldpage = ReadPage($pagename);
	$newpage = $oldpage;

	// Setting up search patterns and replace patterns
	
	// if the vote is "exclusive" users may only vote once.  We must track usernames and votes to be sure people don't re-vote.
	if ($PRS_Vote['exclusive']) {
		$newtext = "Rating_$Author:$_GET[vote]"; 
		$pattern = '/Rating_' . $Author . ':(\d+)/';
		if (preg_match($pattern, $newpage['text'],$matches)) {
			$newpage['text'] = preg_replace($pattern, $newtext, $newpage['text'], 1);
			// if the vote is exclusive, don't increase the #votes or add the sum incorrectly
			$newpage['RatingSum'] = $newpage['RatingSum'] - $matches[1] + $_GET['vote'];
		} else {
			$newpage['RatingCount']++;
			$newpage['RatingSum'] = $newpage['RatingSum'] + $_GET['vote'];
		}
	} else {
		// if the vote is not exclusive, we're not tracking votes by usernames.
		$newtext = 'Rating:' . $_GET['vote'];
		$newpage['RatingCount']++;
		$newpage['RatingSum'] = $newpage['RatingSum'] + $_GET['vote'];
	}
	
	// Add markup to page
	// and if the vote should be private
	if ($PRS_Vote['hidden']) {
		$newpage['text'] = $newpage['text'] . "\n(:$newtext:)";
	// or the vote should be public
	} else {
		$newpage['text'] = $newpage['text'] . "\n$newtext";
	}
	// copying page values to variables
	$PRS_Vote['count'] = $newpage['RatingCount'];//how many votes total
	@$newpage['RatingAverage'] = $newpage['RatingSum'] / $newpage['RatingCount']; // calculate the average

	// This is not really being used!!	
	$PRS_Vote['wording']['use']=($PRS_Vote['count']==1) ? $PRS_Vote['wording']['one'] : $PRS_Vote['wording']['many'];//plural form votes/vote
	// Make the page changes
	UpdatePage($pagename, $oldpage, $newpage);
	unset($_POST);
	HandleBrowse($pagename);
	exit();
}