<?php /* Rating System for PmWiki ========================= Copyright Statement ------------------- Copyright (c) 2006, Benjamin C. Wilson. This file extends PmWiki; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. You may contact the author at ameen@dausha.net. This software is maintained at http://www.dausha.net/Technical/Rating/. Introduction ------------ This Rating System is a simple markup add-on for PmWiki. Its purpose is to provide a quick method for rating an item and for tracking the average ratings. Installation ------------ Usage ------ Examples: ~~~~~~~~~ Variables --------- These are the variables that influence the behavior of the markup. The defaults are recommended for use in the configuration file for your PmWiki Installation. The markup options may be added by the author. Defaults ~~~~~~~~ Markup Options ~~~~~~~~~~~~~~ Release Notes ------------- * v0.1 - May 23, 2006: Initial Public Release by Ben Wilson. */ SDV($RatingStarDir, '/'); SDV($RatingMax, 5); SDV($RatingMin, 1); SDV($RatingUsefulTF, array('No','Yes')); SDV($RatingUseful, '<b>%d of %d found this useful.</b>'); SDV($RatingQuery, 'Was this review helpful to you?'); SDV($RatingDateFmt,'%B %d, %Y %H:%M'); SDV($RatingPermission, 'read'); $RatingAverage = array(); $UsefulVote = array(); Markup('ratingpos', '>inline', '/\(:reviewpos:\)/e', ''); Markup('ratingform', '<inline', '/\(:reviewform:\)/e', 'RatingReviewForm();'); Markup('reviewave', '<inline', '/\(:ratingave:\)/e', 'RatingAverage();'); Markup('rating', 'directive', '/\(:rating-(.*?):\)/e', 'Rating("$1");'); Markup('useful', 'directive', '/\(:useful-(.{32}(.*?)):\)/e', 'RatingUsefulForm("$1","$2");'); function RatingAverage() { global $RatingAverage, $RatingMin, $RatingMax, $FmtV; $c = count($RatingAverage); $t = 0; foreach ($RatingAverage as $r) $t += ($r > $RatingMax) ? $RatingMax : ($r < $RatingMin) ? $RatingMin : $r; $a = (int) ($t / $c); $FmtV['RatingsCount'] = $t; return RatingStars($a); } function RatingStars($r) { global $RatingStarDir, $RatingStar; return ".Attach:".$r."stars.gif"; } function Rating($r) { global $RatingAverage; array_push($RatingAverage, $r); return RatingStars($r); } #(:useful:) replace with (:useful-md5(time):) on first save. $ROSPatterns ["/\(:useful:\)/e"] = "RatingReplaceUseful();"; function RatingReplaceUseful() { return sprintf("(:useful-%s:)", md5(rand(1,99999999))); } function RatingUsefulForm($m, $i) { global $RatingUseful, $RatingUsefulTF, $RatingQuery; global $UsefulVote; $o = ParseArgs($i); $t = ($o['t']) ? $o['t'] : 0; $y = ($o['y']) ? $o['y'] : 0; if ($UsefulVote[$m]) list($t, $y) = RatingUsefulAppend($m, $UsefulVote[$m], $t, $y); $c = sprintf($RatingUseful, $y, $t); $f =<<<FORM <form> <input type='hidden' name='action' value='ratingvote'> <input type='hidden' name='votingid' value='$m'> <input type='submit' name='vote' value='$RatingUsefulTF[1]'> <input type='submit' name='vote' value='$RatingUsefulTF[0]'> </form> FORM; $f = keep($f); return "<div class='rq'>$c $RatingQuery $f</div>"; } function RatingUsefulAppend($m, $k, $t, $y) { global $pagename; global $RatingUsefulTF, $RatingPermission; $y += ($RatingUsefulTF[1] == $k) ? 1 : 0; $t++; $re = "/\(:useful-$m.*?:\)/"; $out = "(:useful-$m t=$t y=$y:)"; RatingReplace($re, $out); } function RatingReplace($r, $o) { global $pagename, $RatingPermission; $page = RetrieveAuthPage($pagename, $RatingPermission, true); if (!$page) Abort('?cannot read $pagename'); $new = $page; $new['text'] = preg_replace($r,$o,$page['text']); PostPage($pagename, $page, $new); Redirect($pagename); } function RatingReviewForm() { global $Author; global $HTMLStylesFmt; $HTMLStylesFmt[] =<<<CSS .review { border-top: 4px solid #CCF; margin-top: 1em; } #reviewform { padding: 1em; } #reviewform form label { display: block; width: 90px; float: left; font-weight: bold; text-align: right; padding-right: 1em; } CSS; $f =<<<FORM <div id='reviewform'><form method='POST'> <input type='hidden' name='action' value='addreview'> <label>Review Title:</label><input type='text' maxlength='100' name='title' size='50' value='' /><br /> <label>Author:</label><input type='text' maxlength='50' name='author' size='20' value='$Author' /><br /> <label>Rating:</label> <select name='rating'> <option selected value=''>Select...</option> <option>5 Stars</option> <option>4 Stars</option> <option>3 Stars</option> <option>2 Stars</option> <option>1 Stars</option> </select> <br /><b>Type your review here:</b><br /> <textarea wrap='virtual' name='review' rows='10' cols='65'></textarea> <input type='submit' name='submit' value='Add your Review'> </form></div> FORM; return Keep($f); } function RatingAddReview($r) { global $RatingDateFmt; $stars = substr($r['rating'],0,1); if ($stars == '') $stars = 1; $d = strftime($RatingDateFmt); $a = ($r['author'] != '') ? "\nReviewed by $r[author]\n" : "\n"; $useful = sprintf("(:useful-%s:)", md5(rand(1,99999999))); $out =<<<OUTPUT (:reviewpos:) >>review<< (:rating-$stars:) \\\ '''$r[title]''', $d $a $r[review] $useful >><< OUTPUT; ltrim($out, "\n"); $re = '/\(:reviewpos:\)/'; RatingReplace($re, $out); } #-------------------------------- # if ($_REQUEST['action']=='addreview') { RatingAddReview($_REQUEST); } if ($_REQUEST['action']=='ratingvote') { $UsefulVote[$_REQUEST['votingid']] = $_REQUEST['vote']; }