NP_Reviews.php

This plugin displays current items rating, from 1-10 in graphics, uses reviews-directory with files 1.gif to 10.gif). For example to rate music / movie reviews or simply just articles itself.

Originally made by Ihra for BlogCMS (no changes needed to work with NucleusCMS): http://forum.blogcms.com/viewtopic.php?id=582

General Plugin info
Author: Ihra aka Jussi Josefsson
Current Version: 1.1
Download: -
Code: code
Demo: -
Forum Thread: here

Feedback

Post bug reports and suggestions here at the Nucleus forum: http://forum.nucleuscms.org/viewtopic.php?t=4731

Instructions

1. Save the code below as NP_Reviews.php and upload it to nucleus/plugins directory
2. create nucleus/plugins/reviews directory and place your graphics in the directory (files should be named 1.gif to 10.gif)
3. install plugin through admin (interfaceplugins → install new plugin)
4. put <%Reviews%> in your template (admin→templates), normally default and detailed templates and item footer box is a good place
5. edit nucleus/forms/commentform-loggedin.template and/or nucleus/forms/commentform-loggedin.template to insert dropdown-box with “crating” as a value. Example:

<h4>Rating</h4>
<select name="crating">
 <option name="crating" value="0">Not Shown</option>
 <option name="crating" value="1">1</option>
 <option name="crating" value="2">2</option>
 <option name="crating" value="3">3</option>
 <option name="crating" value="4">4</option>
 <option name="crating" value="5">5</option>
 <option name="crating" value="6">6</option>
 <option name="crating" value="7">7</option>
 <option name="crating" value="8">8</option>
 <option name="crating" value="9">9</option>
 <option name="crating" value="10">10</option>
</select>
</fieldset>

6. rate your items. If rating is zero (or not between 1 to 10) nothing is shown in the item.

Reviews plugin shows dropdown box with “Rating” above it in your AddItem/EditItem forms. They are in the extra-part of the wysiwyg editor.

Code

<?php
/*
------------------
NP_Reviews by Ihra
******************
originally made for BlogCMS (no changes needed to work with NucleusCMS)
http://forum.blogcms.com/viewtopic.php?id=582
*/
 
class NP_Reviews extends NucleusPlugin {
    function getName() { return 'Reviews'; }
	function getAuthor() { return 'Ihra aka Jussi Josefsson'; }
	function getMinNucleusVersion() { return '200'; }
	function getURL() { return 'http://forum.blogcms.com/'; }
	function getVersion() { return '1.1'; }
	function getDescription() {
		return "This plugin displays current items rating, from 1-10 "
		."(in graphics, uses reviews-directory with files 1.gif to 10.gif). "
		."For example to rate music / movie reviews or simply just articles itself."; }
    function supportsFeature($feature) {
		switch($feature) {
			case 'SqlTablePrefix': return 1;
			default: return 0;
		}
	}
	function getEventList() {
		return array('PrepareItemForEdit','EditItemFormExtras', 'AddItemFormExtras', 'PostAddItem', 
			'PreUpdateItem', 'PostAddComment'); }
 
    // when adding item, add dropdown box from 0-10 (0=dont show rating)
	function event_AddItemFormExtras($data) {
		echo "<h4>Rating</h4>";
		echo "<select  name=\"irating\">";
		echo "<option name=\"irating\" value=0>not shown</option>";
		for ($i=1; $i<11; $i++) {
			echo "<option name=\"irating\" value=$i>$i</option>"; }
		echo "</select>";
    }
 
    // inject rating-field to item and comment-database (nucleus_item)
	function install () {
        $query = 'ALTER TABLE '. sql_table('item') . ' ADD `irating` TINYINT NOT NULL DEFAULT 0';
        sql_query($query);
        $query = 'ALTER TABLE '. sql_table('comment') . ' ADD `crating` TINYINT NOT NULL DEFAULT 0';
        sql_query($query);
    }
 
    // remove rating-field from item and comment-database (nucleus_item) when uninstalling
    function uninstall () {
        $query = 'ALTER TABLE '. sql_table('item') .' DROP `irating`';
        sql_query($query);
        $query = 'ALTER TABLE '. sql_table('comment') .' DROP `crating`';
        sql_query($query);
    }
 
    // when new article is to be saved
    function event_PostAddItem($data) {
            $irating = requestVar('irating');
            // if not in variables, try fetching POST-variable itself
            if (!$irating) { $irating=$_POST['irating']; };
            $curItem = $data['itemid'];
            $query = "UPDATE ". sql_table('item')." SET irating=".$irating." WHERE inumber=".$curItem;
            $result = mysql_query($query);
    }
 
    // save value of crating
    function event_PostAddComment($data) {
            $crating = requestVar('crating');
                        // take last insert which should be our comment
            $commentid = mysql_insert_id();
            if (!isset($crating)) { $crating=$_POST['crating']; };
            if (!isset($crating)) return;
            $query = "UPDATE ".sql_table('comment')." SET crating=".$crating." WHERE cnumber=".$commentid;
            $result = mysql_query($query);
 
    }
 
    // when article is edited and saved
    function event_PreUpdateItem($data) {
            $irating = requestVar('irating');
            if (!$irating) { $irating=$_POST['irating']; };
            if (!$irating) return;
            $itemid=$data['itemid'];
            sql_query("UPDATE ".sql_table('item')." SET irating='$irating' WHERE inumber='$itemid'");
    }
 
    // show simple dropdown box to user. 0 = no rating (for some categories etc.)
    function event_EditItemFormExtras($data) 
    {
            $curItem = $item->itemid;
            $query = "SELECT irating from ". sql_table('item')." WHERE inumber=".strval($data['itemid']);
            $result = mysql_query($query);
            $row = mysql_fetch_object($result);
            $rating = $row->irating;
            echo "<h4>Rating</h4>";
            echo "<select  name=\"irating\">";
            echo "<option name=\"irating\" value=0>not shown</option>";
            for ($i=1; $i<11; $i++)
            {
              if ($i==$rating) { echo "<option name=\"irating\" value=$i selected>$i</option>"; }
               else { echo "<option name=\"irating\" value=$i>$i</option>"; };
            };
            echo "</select>";
    }
 
    // show <%Reviews%> at the comments-page, where user want to show graphs (for example in comment-footer, beside time)
    function doTemplateCommentsVar(&$item, &$comment)
    {
        $commentId = $comment['commentid'];
            $query = "SELECT crating from ". sql_table('comment')." WHERE cnumber=".$commentId;
            $result = mysql_query($query);
            $row = mysql_fetch_object($result);
            $rating = $row->crating;
            $url = $this->getAdminURL( );
            if (($rating>0) && (rating<=10)) { echo "<br /><img src=$url$rating.gif>"; };
    }
 
    // show <%Reviews%> at the template-page, where user want to show graphs (for example in item-footer)
    function doTemplateVar(&$item) 
    {
        global $blogid;
 
            $curItem = $item->itemid;
            $query = "SELECT irating from ". sql_table('item')." WHERE inumber=".$curItem." AND iblog=".$blogid;
            $result = mysql_query($query);
            $row = mysql_fetch_object($result);
            $rating = $row->irating;
            $url = $this->getAdminURL( );
            if (($rating>0) && (rating<=10)) { echo "<br /><img src=$url$rating.gif>"; };
     }
 
}
?>

History

  • Version 1.1 (31.08.04) Added ratings to comments also. If you don't want anyone to rate your article, set “allow only members to post comments” on in your settings.

You need also insert dropdown-box or graphics bar to your comment-template (loggedin and/or notloggedin depending if you want anonymous ratings).

  • Version 1.0 (17.08.04) Initial release, drop dead tired from work

Todo

  • total rating? sum of the all ratings divided with number of ratings (average)
  • changeable graphics (multiple sets? only prefix is needed, like swords_ (uses swords_1.gif etc.)
  • admin options? (allow ratings in comments on/off)
  • graphic rating? like in phpNuke (“rate this article 1-5 stars”)

Alternative Plugins

reviews.txt · Last modified: 2006/07/05 13:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki