NP_Rate.php

NP_Rate is a rating system for your items. You can see it working at the Nucleus FAQ: choose an item and see the rating system at the end of the page. The plugin places a form where visitors can rate your items from 0 to 10. Results are displayed with a star graphic plus count of votes and the score.

General Plugin info
Author: moraes
Current Version: 0.4.1
Download: http://test.rantthisspace.com/files/rate/NP_Rate.zip
Code: code
Demo: -
Forum Thread: http://forum.nucleuscms.org/viewtopic.php?t=5169

Screenshots

Default Drop-down Select :rateselect.jpg
Optional Radio Buttons :rate1.jpg
After Voting :rate.jpg
Already Voted :rate0.jpg

There are two Paw rating graphic sets available, one based upon 5 Paws and the other based upon 10 Paws.

:plugins:4.gif from the 5 Paws set. GIF image (for Line 207 edit explained below) dimensions are width=“82” height=“14”. 5 Paws set zip file: :plugins:5paws.zip

:plugins:8paws.gif from the 10 Paws set. GIF image (for Line 207 edit explained below) dimensions are width=“167” height=“14”. 10 Paws set zip file: :plugins:10paws.zip

You can see it here: http://www.RecycledDogs.com/weblog/

Feedback

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

Instructions

  1. Download NP_Rate.zip and extract the contents to your plugins folder.
  2. OPTIONALLY: create your own unique images, named 0.gif, 1.gif, etc, and smile.png. Upload them to a folder on your server.
  3. Install the plugin.
  4. In the plugin's options, set the path to the images folder.
    • To use the included images use http://your_site.example/nucleus/plugins/rate/
    • To use your own images, set this path to whatever folder you put your images in. If your image are not the same size as the included images (70×14) you will need to edit NP_rate.php. On what should be line 207, change width=“70” and height=“14” to the width and height of your custom images.
    • Make sure your path ends with a trailing slash!
  5. Put <%Rate%> in your short (item) or full (index) templates.
  6. You can also use <%Rate(mostrated, 5)%> and <%Rate(toprated, 5)%> in your *skins* to display a list with the 5 top/most rated items.

Code

<?php
class NP_Rate extends NucleusPlugin {
    function getEventList() { return array('PreSendContentType'); }
    function getName() { return 'Rate'; }
    function getAuthor() { return 'moraes'; }
    function getURL() { return 'http://forum.nucleuscms.org'; }
    function getVersion() { return '0.4'; }
    function getDescription() { return 'Add a rating system to your items.'; }
    function supportsFeature($feature) {
        switch($feature) {
            case 'SqlTablePrefix': return 1;
            default: return 0;
        }
    }
    function install() {
        sql_query('CREATE TABLE IF NOT EXISTS ' . sql_table('plugin_rates') . ' (itemid int(11) NOT NULL default "0", votes int(11) NOT NULL default "0", total int(11) NOT NULL default "0", rate FLOAT(10,2) NOT NULL default "0.00")');
        $this->createOption('title', 'Title message', 'text', 'Please rate this item:');
        $this->createOption('thanks', 'Thanks message', 'text', 'Thanks for your vote!');
        $this->createOption('novotes', 'No votes message', 'text', 'This item was not rated yet.');
        $this->createOption('alreadyvoted', 'Already voted message', 'text', 'You have already rated this item.');
        $this->createOption('optionselect', 'Show select instead of radio buttons?', 'yesno', 'yes');
        $this->createOption('imagedir', 'Folder where the options gif\'s are located (end with /)', 'text', 'http://www.site.com/img/');
    	$this->createOption('totalvotes', 'Text displayed for total votes', 'text', 'Total Votes:');
    	$this->createOption('rating', 'Text displayed for rating', 'text', 'Rating:');
    	$this->createOption('send', 'Text for submit button', 'text', 'Send');
 
    }
    function unInstall() {
        mysql_query('DROP TABLE ' . sql_table('plugin_rates'));
    }
    function doSkinVar($skinType) {
        global $manager;
 
        $params = func_get_args();
        // set default number of items to show if no user input
        if (empty($params[2])) {
            $params[2] = 5;
        }
 
        /*****************************************************************
        // Most Rated
        *****************************************************************/
        /* // usage: <%Rate(mostrated [, NUMBER OF ITEMS] )%> -> number of items to show is optional, default is 5  */
        if ($params[1] == 'mostrated') {
 
            $query = "SELECT itemid, votes FROM " . sql_table('plugin_rates') . " ORDER by votes DESC LIMIT 0,".$params[2];
            $res = mysql_query($query);
            if(mysql_num_rows($res) > 0) {
                echo '<ul>';
                while($row = mysql_fetch_object($res)) {
                    $item_id = $row->itemid;
                    $votes   = $row->votes;
 
                    $item =& $manager->getItem($item_id,'','');
 
                    if(!is_null($item)) {
                        $title = strip_tags($item['title']);
                        echo '<li>';
                        echo '<a href="'.createItemLink($item_id, '').'" title="'.$title.'">'.$title.'</a> ('.$votes.' votes)';
                        echo '</li>';
                    }
                    else {
                        mysql_query("DELETE FROM " . sql_table('plugin_rates') . " WHERE id = ". $item_id);
                    }
                }
                echo '</ul>';
            }
        }
 
        /*****************************************************************
        // Top Rated
        *****************************************************************/
        /*  // usage: <%Rate(toprated [, NUMBER OF ITEMS] )%> -> number of items to show is optional, default is 5  */
        else if ($params[1] == 'toprated') {
 
            $query = "SELECT itemid, rate FROM " . sql_table('plugin_rates') . " ORDER by rate DESC LIMIT 0,".$params[2];
            $res = mysql_query($query);
            if(mysql_num_rows($res) > 0) {
                echo '<ul>';
                while($row = mysql_fetch_object($res)) {
                    $item_id = $row->itemid;
                    $rate    = $row->rate;
 
                    $item =& $manager->getItem($item_id,'','');
 
                    if(!is_null($item)) {
                        $title = strip_tags($item['title']);
                        echo '<li>';
                        echo '<a href="'.createItemLink($item_id, '').'" title="'.$title.'">'.$title.'</a> ('.$rate.')';
                        echo '</li>';
                    }
                    else {
                        mysql_query("DELETE FROM " . sql_table('plugin_rates') . " WHERE id = ". $item_id);
                    }
                }
                echo '</ul>';
            }
        }
    } // doSkinVar
 
    function doTemplateVar(&$item) {
        $params = func_get_args();
 
        $rate      = postVar('rate');
        $item_id   = $item->itemid;
        $item_post = postVar('item_id');
        $cookie    = cookieVar('NP_Rate-item'.$item_id);
 
        if ($item_id != 0) {
            $query = "SELECT votes, total FROM " . sql_table('plugin_rates') . " WHERE itemid=" . $item_id;
            $res = mysql_query($query);
            $row = mysql_fetch_object($res);
            $votes = intval($row->votes);
            $total = intval($row->total);
 
            // calculate votes
            if (mysql_num_rows($res) != 0) {
                $media = ($total / $votes);
                $current_rate = number_format($media, 2, '.', '');
                $ceil_rate = ceil($media);
            }
 
            if ($params[1] == 'short') {
                if (mysql_num_rows($res) == 0) {
                    echo ''.$this->getOption('thanks').'';
                }
                else {
                    echo $current_rate.'/10';;
                }
            }
            else {
                if (mysql_num_rows($res) == 0) {
                    if($item_post != $item_id) {
                        // no votes for this item yet
                        echo '<p>'.$this->getImg(0);
                        echo '<br />'.$this->getOption('geen stemmen').'</p>';
                    }
                    else {
                        // first vote
                        mysql_query("INSERT INTO " . sql_table('plugin_rates') . " VALUES ('$item_post', '1', '$rate', '".$rate.".00')");
                        echo '<p>'.$this->getImg($rate);
                        echo '<br /><strong>'.$this->getOption('totalvotes').'</strong> 1 - <strong>'.$this->getOption('rating').'</strong> '.$rate.'</p>';
                    }
                }
                else {
                    if($item_post != $item_id || !empty($cookie)) {
                        // already voted or $item_post is different from current item
                        echo '<p>'.$this->getImg($ceil_rate);
                        echo '<br /><strong>'.$this->getOption('totalvotes').'</strong> ' . $votes . ' - <strong>'.$this->getOption('rating').'</strong> '.$current_rate.'</p>';
                    }
                    else {
                        // update counter
                        $votes = $votes + 1;
                        $total = $total + $rate;
                        $media = ($total / $votes);
                        $ceil_rate = ceil($media);
                        $current_rate = number_format($media, 2, '.', '');
 
                        mysql_query("UPDATE " . sql_table('plugin_rates') . " SET votes=$votes, total='$total', rate='$current_rate' WHERE itemid=$item_id");
                        echo '<p>'.$this->getImg($ceil_rate);
                        echo '<br /><strong>'.$this->getOption('totalvotes').'</strong> '.$votes.' - <strong>'.$this->getOption('rating').'</strong> '.$current_rate.'</p>';
                    }
                }
 
                if($item_post == $item_id && empty($cookie)) {
                    // successfull vote. show 'thanks' message.
                    echo '<p><strong>'.$this->getOption('thanks').'</strong> ';
                    echo '<img src="'.$this->getOption('imagedir').'smile.png" width="15" height="15" alt="thanks!" title="thanks!"></p>';
                }
                elseif(!empty($cookie)) {
                    // cookie is set. show 'already voted' message.
                    echo '<p><strong>'.$this->getOption('alreadyvoted').'</strong> ';           
                }
                else {
                    // print the rate form
                    echo '<p><form method="post">';
                    echo '<input type="hidden" name="item_id" value="'.$item_id.'">';
                    echo '<h3>'.$this->getOption('title').'</h3>';
                    if($this->getOption('optionselect') == 'yes') {
                        echo '<select name="rate">';
                    }
						for($i=0; $i<=5; $i++) {
                        if($this->getOption('optionselect') == 'yes') {
                            // show select options
                            echo '<option value="'.$i.'"';
                            if($i == 5) {
                                echo ' selected';
                            }
                            echo '>'.$i.'</option>';
                        }
                        else {
                            // show radio buttons
                            echo '<input type="radio" name="rate" value="'.$i.'">'.$i.'&nbsp;';
                        }
                    }
                    if($this->getOption('optionselect') == 'yes') {
                        echo '</select>';
                    }
                    echo '<input type="submit" value="'.$this->getOption('send').'">';
                    echo '</form></p>';
                }
            }
        }
    } // doTemplateVar
 
    function getImg($rate) {
        return '<img src="'.$this->getOption('imagedir').$rate.'.gif" width="70" height="14" alt="item rate" title="item rate" />';
    }
 
    function event_PreSendContentType() {
        global $CONF;
        $rate = postVar('rate');
        /* check for $_POST info */
        if(!empty($rate)) {
            $timestamp=time();
            $expire = time()+2592000;
            $item_post = postVar('item_id');
            setCookie('NP_Rate-item'.$item_post,$item_post,$expire,$CONF['CookiePath'],$CONF['CookieDomain'],$CONF['CookieSecure']);
        }
    } // PreSendContentType
 
} // class
 
?>

History

  • Version 0.4.1 - September 1st, 2010. Fixed comments with example tags. (WillyP)
  • Version 0.4 - February 7th, 2005. Removed hardcoded language into options.
  • Version 0.2 - November 15, 2004. fix to use the rating system on the short template.
  • Version 0.1 - November 15, 2004. Initial version.

Plugin review

  • NP_Rate version 0.4 works with Nucleus CMS 3.31 - 2007-10-28 Armon
  • NP_Rate version 0.4.1 works with Nucleus CMS 3.51 - 2007-10-28 WillyP
rate.txt · Last modified: 2010/09/03 07:06 (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