to enable rating on this page * Note: The wikilens theme must be enabled, to enable this plugin! * Or use a sidebar based theme with the box method. * to show my ratings * to show my buddies * * * @author: Dan Frankowski (wikilens author), Reini Urban (as plugin) * * TODO: * - fix RATING_STORAGE = WIKIPAGE * - fix smart caching * - finish mysuggest.c (external engine with data from mysql) * - add php_prediction */ require_once("lib/WikiPlugin.php"); require_once("lib/wikilens/RatingsDb.php"); class WikiPlugin_RateIt extends WikiPlugin { function getName() { return _("RateIt"); } function getDescription() { return _("Rating system. Store user ratings per page"); } function getVersion() { return preg_replace("/[Revision: $]/", '', "\$Revision: 1.14 $"); } function RatingWidgetJavascript() { global $WikiTheme; $img = substr($WikiTheme->_findData("images/RateItNk0.png"),0,-7); $urlprefix = WikiURL("",0,1); $js = " function displayRating(imgPrefix, ratingvalue, pred) { var cancel = imgPrefix + 'Cancel'; for (i=1; i<=10; i++) { var imgName = imgPrefix + i; var imgSrc = '".$img."'; document[imgName].title = '"._("Your rating ")."'+ratingvalue; if (i<=(ratingvalue*2)) { document[imgName].src = imgSrc + ((i%2) ? 'Ok1' : 'Ok0') + '.png'; } else { document[imgName].src = imgSrc + ((i%2) ? 'Nk1' : 'Nk0') + '.png'; } } //document[cancel].src = imgSrc + 'Cancel.png'; } function click(actionImg, pagename, version, imgPrefix, dimension, rating) { if (rating == 'X') { deleteRating(actionImg, pagename, dimension); displayRating(imgPrefix, 0, 0); } else { submitRating(actionImg, pagename, version, dimension, rating); displayRating(imgPrefix, rating, 0); } } function submitRating(actionImg, page, version, dimension, rating) { var myRand = Math.round(Math.random()*(1000000)); var imgSrc = escape(page) + '?version=' + version + '&action=".urlencode(_("RateIt"))."&mode=add&rating=' + rating + '&dimension=' + dimension + '&nopurge=cache&rand=' + myRand; //alert('submitRating(' + page + ', ' + version + ', ' + dimension + ', ' + rating + ') => '+imgSrc); document[actionImg].src= imgSrc; } function deleteRating(actionImg, page, dimension) { var myRand = Math.round(Math.random()*(1000000)); var imgSrc = '".$urlprefix."' + escape(page) + '?action=".urlencode(_("RateIt"))."&mode=delete&dimension=' + dimension + '&nopurge=cache&rand=' + myRand; //alert('deleteRating(' + page + ', ' + version + ', ' + dimension + ')'); document[actionImg].src= imgSrc; } "; return JavaScript($js); } function actionImgPath() { global $WikiTheme; return $WikiTheme->_findFile("images/RateItAction.png"); } /** * Take a string and quote it sufficiently to be passed as a Javascript * string between ''s */ function _javascript_quote_string($s) { return str_replace("'", "\'", $s); } function getDefaultArguments() { return array( 'pagename' => '[pagename]', 'version' => false, 'id' => 'rateit', 'imgPrefix' => '', 'dimension' => false, 'small' => false, 'show' => false, 'mode' => false, ); } function head() { // early side-effects (before body) global $WikiTheme; $WikiTheme->addMoreHeaders($this->RatingWidgetJavascript()); } // todo: only for signed users // todo: set rating dbi for external rating database function run($dbi, $argstr, &$request, $basepage) { global $WikiTheme; //$this->_request = & $request; //$this->_dbi = & $dbi; $user = & $request->getUser(); //FIXME: fails on test with DumpHtml:RateIt if (!is_object($user)) return HTML(); $this->userid = $user->UserName(); $args = $this->getArgs($argstr, $request); $this->dimension = $args['dimension']; if ($this->dimension == '') { $this->dimension = 0; $args['dimension'] = 0; } if ($args['pagename']) { // Expand relative page names. $page = new WikiPageName($args['pagename'], $basepage); $args['pagename'] = $page->name; } if (empty($args['pagename'])) { return $this->error(_("no page specified")); } $this->pagename = $args['pagename']; $rdbi = RatingsDb::getTheRatingsDb(); $this->_rdbi =& $rdbi; if ($args['mode'] === 'add') { if (!$user->isSignedIn()) return $this->error(_("You must sign in")); global $WikiTheme; $actionImg = $WikiTheme->_path . $this->actionImgPath(); $rdbi->addRating($request->getArg('rating'), $user->getId(), $this->pagename, $this->dimension); ob_end_clean(); // discard any previous output // delete the cache $page = $request->getPage(); $page->set('_cached_html', false); $request->cacheControl('MUST-REVALIDATE'); $dbi->touch(); //fake validators without args $request->appendValidators(array('wikiname' => WIKI_NAME, 'args' => hash(''))); header('Content-type: image/png'); readfile($actionImg); exit(); } elseif ($args['mode'] === 'delete') { if (!$user->isSignedIn()) return $this->error(_("You must sign in")); global $WikiTheme; $actionImg = $WikiTheme->_path . $this->actionImgPath(); $rdbi->deleteRating($user->getId(), $this->pagename, $this->dimension); ob_end_clean(); // discard any previous output // delete the cache $page = $request->getPage(); $page->set('_cached_html', false); $request->cacheControl('MUST-REVALIDATE'); $dbi->touch(); //fake validators without args $request->appendValidators(array('wikiname' => WIKI_NAME, 'args' => hash(''))); header('Content-type: image/png'); readfile($actionImg); exit(); } elseif (! $args['show'] ) { // we must use the head method instead, because is already printed. // $WikiTheme->addMoreHeaders($this->RatingWidgetJavascript()); // or we change the header in the ob_buffer. //Todo: add a validator based on the users last rating mtime //$rating = $rdbi->getRating(); /* static $validated = 0; if (!$validated) { //$page = $request->getPage(); //$page->set('_cached_html', false); $request->cacheControl('REVALIDATE'); $validated = 1; } */ //$args['rating'] = $rating; return $this->RatingWidgetHtml($args['pagename'], $args['version'], $args['imgPrefix'], $args['dimension'], $args['small']); } else { if (!$user->isSignedIn()) return $this->error(_("You must sign in")); extract($args); $rating = $rdbi->getRating(); $html = HTML::p(sprintf(_("Rated by %d users | Average rating %.1f stars"), $rdbi->getNumUsers($this->pagename, $this->dimension), $rdbi->getAvg($this->pagename, $this->dimension)), HTML::br()); if ($rating) { $html->pushContent(sprintf(_("Your rating was %.1f"), $rating)); } else { $pred = $rdbi->getPrediction($this->userid, $this->pagename, $this->dimension); if (is_string($pred)) $html->pushContent(sprintf(_("%s prediction for you is %s stars"), WIKI_NAME, $pred)); elseif ($pred) $html->pushContent(sprintf(_("%s prediction for you is %.1f stars"), WIKI_NAME, $pred)); } $html->pushContent(HTML::p()); $html->pushContent(HTML::em("(Experimental: This is entirely bogus data)")); return $html; } } // box is used to display a fixed-width, narrow version with common header function box($args=false, $request=false, $basepage=false) { if (!$request) $request =& $GLOBALS['request']; if (!$request->_user->isSignedIn()) return; if (!isset($args)) $args = array(); $args['small'] = 1; $argstr = ''; foreach ($args as $key => $value) $argstr .= $key."=".$value; $widget = $this->run($request->_dbi, $argstr, $request, $basepage); return $this->makeBox(WikiLink(_("RateIt"),'',_("Rate It")), $widget); } /** * HTML widget display * * This needs to be put in the section of the page. * * @param pagename Name of the page to rate * @param version Version of the page to rate (may be "" for current) * @param imgPrefix Prefix of the names of the images that display the rating * You can have two widgets for the same page displayed at * once iff the imgPrefix-s are different. * @param dimension Id of the dimension to rate * @param small Makes a smaller ratings widget if non-false * * Limitations: Currently this can only print the current users ratings. * And only the widget, but no value (for buddies) also. */ function RatingWidgetHtml($pagename, $version, $imgPrefix, $dimension, $small = false) { global $WikiTheme, $request; $imgPrefix = $pagename . $imgPrefix; $actionImgName = $imgPrefix . 'RateItAction'; $dbi =& $GLOBALS['request']->getDbh(); $version = $dbi->_backend->get_latest_version($pagename); //$rdbi =& $this->_rdbi; $rdbi = RatingsDb::getTheRatingsDb(); $id = 'rateit'; // Protect against 's, though not \r or \n $reImgPrefix = WikiPlugin_RateIt::_javascript_quote_string($imgPrefix); $reActionImgName = WikiPlugin_RateIt::_javascript_quote_string($actionImgName); $rePagename = WikiPlugin_RateIt::_javascript_quote_string($pagename); //$dimension = $args['pagename'] . "rat"; $html = HTML::span(array("id" => $id)); for ($i=0; $i < 2; $i++) { $nk[$i] = $WikiTheme->_findData("images/RateItNk$i.png"); $none[$i] = $WikiTheme->_findData("images/RateItRk$i.png"); } $user = $request->getUser(); $userid = $user->getId(); //if (!isset($args['rating'])) $rating = $rdbi->getRating($userid, $pagename, $dimension); if (!$rating) { $pred = $rdbi->getPrediction($userid,$pagename,$dimension); } for ($i = 1; $i <= 10; $i++) { $a1 = HTML::a(array('href' => 'javascript:click(\'' . $reActionImgName . '\',\'' . $rePagename . '\',\'' . $version . '\',\'' . $reImgPrefix . '\',\'' . $dimension . '\',' . ($i/2) . ')')); $img_attr = array(); $img_attr['src'] = $nk[$i%2]; //if (!$rating and !$pred) // $img_attr['src'] = $none[$i%2]; $img_attr['name'] = $imgPrefix . $i; $img_attr['border'] = 0; $a1->pushContent(HTML::img($img_attr)); $a1->addToolTip(_("Rate the topic of this page")); $html->pushContent($a1); //This adds a space between the rating smilies: // if (($i%2) == 0) $html->pushContent(' '); } $html->pushContent(HTML::Raw(' ')); $a0 = HTML::a(array('href' => 'javascript:click(\'' . $reActionImgName . '\',\'' . $rePagename . '\',\'' . $version . '\',\'' . $reImgPrefix . '\',\'' . $dimension . '\',\'X\')')); $msg = _("Cancel rating"); $a0->pushContent(HTML::img(array('src' => $WikiTheme->getImageUrl("RateItCancel"), 'name'=> $imgPrefix.'Cancel', 'alt' => $msg))); $a0->addToolTip($msg); $html->pushContent($a0); /*} elseif ($pred) { $msg = _("No opinion"); $html->pushContent(HTML::img(array('src' => $WikiTheme->getImageUrl("RateItCancelN"), 'name'=> $imgPrefix.'Cancel', 'alt' => $msg))); //$a0->addToolTip($msg); //$html->pushContent($a0); }*/ $img_attr = array(); $img_attr['src'] = $WikiTheme->_findData("images/RateItAction.png"); $img_attr['name'] = $actionImgName; //$img_attr['class'] = 'k' . $i; $img_attr['border'] = 0; $html->pushContent(HTML::img($img_attr)); // Display the current rating if there is one if ($rating) $html->pushContent(JavaScript('displayRating(\'' . $reImgPrefix . '\','.$rating .',0)')); elseif ($pred) $html->pushContent(JavaScript('displayRating(\'' . $reImgPrefix . '\','.$pred .',1)')); else $html->pushContent(JavaScript('displayRating(\'' . $reImgPrefix . '\',0,0)')); return $html; } }; // $Log: not supported by cvs2svn $ // Revision 1.12 2004/06/30 19:59:07 dfrankow // Make changes suitable so that wikilens theme (and wikilens.org) work properly. // + Remove predictions (for now) // + Use new RatingsDb singleton. // + Change RatingWidgetHtml() to use parameters like a normal PHP function // so we can have PHP check that we're passing the right # of them. // + Change RatingWidgetHtml() to be callable static-ally // (without a plugin object) // + Remove the "RateIt" button for now, because we don't use it on wikilens.org. // Maybe if someone wants the button, there can be an arg or flag for it. // + Always show the cancel button, because UI widgets should not hide. // + Remove the "No opinion" button for now, because we don't yet store that. // This is a useful thing, tho, for the future. // // Revision 1.11 2004/06/19 10:22:41 rurban // outcomment the pear specific methods to let all pages load // // Revision 1.10 2004/06/18 14:42:17 rurban // added wikilens libs (not yet merged good enough, some work for DanFr) // // Revision 1.9 2004/06/14 11:31:39 rurban // renamed global $Theme to $WikiTheme (gforge nameclash) // inherit PageList default options from PageList // default sortby=pagename // use options in PageList_Selectable (limit, sortby, ...) // added action revert, with button at action=diff // added option regex to WikiAdminSearchReplace // // Revision 1.8 2004/06/01 15:28:01 rurban // AdminUser only ADMIN_USER not member of Administrators // some RateIt improvements by dfrankow // edit_toolbar buttons // // Revision _1.2 2004/04/29 17:55:03 dfrankow // Check in escape() changes to protect against leading spaces in pagename. // This is untested with Reini's _("RateIt") additions to this plugin. // // Revision 1.7 2004/04/21 04:29:50 rurban // write WikiURL consistently (not WikiUrl) // // Revision 1.6 2004/04/12 14:07:12 rurban // more docs // // Revision 1.5 2004/04/11 10:42:02 rurban // pgsrc/CreatePagePlugin // // Revision 1.4 2004/04/06 20:00:11 rurban // Cleanup of special PageList column types // Added support of plugin and theme specific Pagelist Types // Added support for theme specific UserPreferences // Added session support for ip-based throttling // sql table schema change: ALTER TABLE session ADD sess_ip CHAR(15); // Enhanced postgres schema // Added DB_Session_dba support // // Revision 1.3 2004/04/01 06:29:51 rurban // better wording // RateIt also for ADODB // // Revision 1.2 2004/03/31 06:22:22 rurban // shorter javascript, // added prediction buttons and display logic, // empty HTML if not signed in. // fixed deleting (empty dimension => 0) // // Revision 1.1 2004/03/30 02:38:06 rurban // RateIt support (currently no recommendation engine yet) // // For emacs users // Local Variables: // mode: php // tab-width: 8 // c-basic-offset: 4 // c-hanging-comment-ender-p: nil // indent-tabs-mode: nil // End: ?>