Modification Name: Thread Rating

Modification Version: 2.1

Modification Author: GuldantheWarlock (GuldantheWarlock@xmbgarage.com)

Modification Description:
This modification will give you the ability to rate threads based on a simple 1 through 5 star system.
Users are only allowed to vote once per thread. Any given thread's rating is based on the average of the votes.

Supported Version: XMB 1.9.8 SP3

Notes:

ATTENTION: This modification is now UID based. Please be aware of that when using any UID fix tools.
The code for that tool should be adjusted to prevent destroying data for this hack.

This modification is released under the GPL. A copy has been provided with this software package.

Please backup your files before installing this modification. Neither XMB Garage nor the author can be held  responsible if your board stops functioning properly due to you installing this modification.
=======================================================================================================
=======
Step 1:
=======
==============================
Go To Admin Panel -> Insert Raw SQL
==============================
=============================
Paste the following code and hit submit
=============================

ALTER TABLE $table_threads ADD ratingdata TEXT NOT NULL;
UPDATE $table_threads SET ratingdata='a:0:{}';

=======================================================================================================
=======
Step 2:
=======

=======================
Edit File: lang/English.lang.php
=======================
=======================
Add To End Of File Above ?>
=======================

// Thread Rating Mod Begin
$lang['textrating'] = 'Rating:';
$lang['textvotes'] = 'Votes';
$lang['rating_not_supported'] = 'Your browser does not support thread ratings!';
$lang['textloading'] = 'Loading...';
$lang['rating_not_logged_in'] = 'You must log in to rate.';
$lang['rating_please_login'] = 'Please login to rate threads.';
$lang['rating_already_voted'] = 'You have already voted.';
$lang['rating_thanks4voting'] = 'Thanks for voting!';
$lang['rating_notallowed'] = 'Sorry, you may not rate your own thread!';
$lang['ratingtext1'] = 'Poor';
$lang['ratingtext2'] = 'Average';
$lang['ratingtext3'] = 'Good';
$lang['ratingtext4'] = 'Great';
$lang['ratingtext5'] = 'Awesome!';
// Thread Rating Mod End

=======================================================================================================
=======
Step 3:
=======
=================
Edit File: header.php
=================
======
Find:
======

    define('X_GUEST', true);
        
===========
Add Below:
===========

    // Thread Rating Mod Begin
    $self['uid'] = 0;
    // Thread Rating Mod End

======
Find:
======

$wollocation = addslashes($url);
    
===========
Add Below:
===========

// Thread Rating Mod Begin
if (false !== strpos($url, '/viewthread.php') && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
    $wollocation = addslashes($url.'?tid='.intval($_POST['tid']));
}
// Thread Rating Mod End

=======================================================================================================
=======
Step 4:
=======
=====================
Edit File: forumdisplay.php
=====================
======
Find:
======

while($thread = $db->fetch_array($querytop)) {

===========
Add Below:
===========

    // Thread Rating Mod Begin
    $rdata = unserialize(stripslashes($thread['ratingdata']));
    
    $rating    = '';
    $ratingnum = count($rdata);
    if ($ratingnum > 0) {
        $trating = floor((array_sum($rdata) / $ratingnum));
        $rating  = str_repeat('<img src="'.$THEME['imgdir'].'/rating-star.png" border="0" alt="*" />', $trating);
        for($i = $trating;$i<5;$i++) {
            $rating .= '<img src="'.$THEME['imgdir'].'/blank-star.png" border="0" alt="*" />';
        }
    } else {
        $rating = str_repeat('<img src="'.$THEME['imgdir'].'/blank-star.png" border="0" alt="*" />', 5);
    }
    // Thread Rating Mod End

=======================================================================================================
=======
Step 5:
=======
===============
Edit File: post.php
===============
======
Find:
======

        $db->query("INSERT INTO ".X_PREFIX."threads (
        
==============
Find In This Line:
==============

) VALUES ('', 

===========
Add Before:
===========

, ratingdata

=========================
Add To End Of Line Before  )");
=========================

, 'a:0:{}'

=======================================================================================================
=======
Step 6:
=======
===================
Edit File: viewthread.php
===================
======
Find:
======

if ($goto == 'lastpost') {

===========
Add Above:
===========

// Thread Rating Mod Begin
if (isset($_POST['action']) && $_POST['action'] == 'threadrating') {
    $sub = isset($_POST['sub'])  ? $_POST['sub']  : '';
    $usr = isset($_POST['user']) ? $_POST['user'] : '';
    
    switch($sub) {
        case 'getrating':
            header('Content-Type: text/xml');
            
            $query  = $db->query("SELECT m.uid AS thread_author, t.ratingdata FROM ".X_PREFIX."members m LEFT JOIN $table_threads t ON m.username=t.author WHERE t.tid='$tid'");
            $thread = $db->fetch_array($query);
            $db->free_result($query);
            
            $threadrating = $hasvoted = $ratingnum = 0;
            if ($tid > 0) {
                $rdata = unserialize(stripslashes($thread['ratingdata']));
                
                if (isset($rdata[$usr])) {
                    $hasvoted = 1;
                }
                
                $ratingnum = count($rdata);
                if ($ratingnum > 0) {
                    $threadrating = floor((array_sum($rdata) / $ratingnum));
                }
            }
            echo '<?xml version="1.0" standalone="yes"?><response><ratingnum>'.$ratingnum.'</ratingnum><rating>'.$threadrating.'</rating><threadauthor>'.$thread['thread_author'].'</threadauthor><hasvoted>'.$hasvoted.'</hasvoted></response>';
            break;
            
        case 'castvote':
            $vote = isset($_POST['vote']) ? (int) $_POST['vote'] : 0;
            
            if ($vote == 0) {
                echo 'uh_oh'; // This won't actually do anything, but I felt some sort of human readable error was needed...
                exit;
            }
            
            if ($usr == '') {
                echo 'not_logged_in';
                exit;
            }
            
            $query  = $db->query("SELECT m.uid AS thread_author, t.ratingdata FROM ".X_PREFIX."members m LEFT JOIN $table_threads t ON m.username=t.author WHERE t.tid='$tid'");
            $thread = $db->fetch_array($query);
            $db->free_result($query);
            
            if ($thread['thread_author'] == $usr) {
                echo 'not_allowed';
                exit;
            }
            
            $rdata  = unserialize(stripslashes($thread['ratingdata']));
            
            if (isset($rdata[$usr])) {
                echo 'already_voted';
                exit;
            }
            
            $rdata[$usr] = $vote;
            
            $db->query("UPDATE ".X_PREFIX."threads SET ratingdata='".addslashes(serialize($rdata))."' WHERE tid='$tid'");
            
            echo 'ok';
            break;
    }
    exit;
}
// Thread Rating Mod End

=======================================================================================================
=======
Step 7:
=======
===============================
Go to Administration Panel --> Templates
===============================
====================
Edit Template: viewthread:
====================
====================
Add To Top Of Template
====================

<script language="JavaScript" type="text/javascript" src="./include/threadrating.js"></script>
<script language="JavaScript" type="text/javascript">
var ratingLang = {
    'not_supported': "$lang[rating_not_supported]",
    'loading': "$lang[textloading]",
    'not_logged_in': "$lang[rating_not_logged_in]",
    'please_login': "$lang[rating_please_login]",
    'already_voted': "$lang[rating_already_voted]",
    'thanks4voting': "$lang[rating_thanks4voting]",
    'textvotes': "$lang[textvotes]",
    'not_allowed': "$lang[rating_notallowed]"
};

var ratingMessages = {
    'poor':    "$lang[ratingtext1]",
    'average': "$lang[ratingtext2]",
    'good':    "$lang[ratingtext3]",
    'great':   "$lang[ratingtext4]",
    'awesome': "$lang[ratingtext5]"
};
</script>

======
Find:
======

<tr class="header">
<td width="18%">$lang[textauthor] </td>

============
Replace With:
============

<tr class="ratingtr">
<td width="18%" class="header">$lang[textauthor] </td>

======
Find:
======

<td>$lang[textsubject] $thread[subject]</td>

============
Replace With:
============

<td class="header">$lang[textsubject] $thread[subject]</td>
<td width="25%" valign="top" class="ratingtd">
<div id="ratingWrapper"><strong>$lang[textrating]</strong>
<span id="rating" style="font-size: 10px"></span>
&nbsp;<span id="ratingnum"></span>
<br /><span id="ratinginfo"></span></div>
<script>new Rating($tid, '$self[uid]', '$imgdir', ratingMessages);</script>
</td>

=======================================================================================================
=======
Step 8:
=======
===============================
Go to Administration Panel --> Templates
===============================
========================
Edit Template: viewthread_post
========================
======
Find:
======

<td valign="top" class="tablerow" style="height: 30px; width: 82%;">

============
Replace With:
============

<td colspan="2" valign="top" class="tablerow" style="height: 30px; width: 82%;">

======
Find:
======

<td class="tablerow" valign="top" style="height: 80px; width: 82%" >

============
Replace With:
============

<td colspan="2" class="tablerow" valign="top" style="height: 80px; width: 82%" >

======
Find:
======

<td class="tablerow" valign="bottom" style="height: 20px; width: 82%;">

============
Replace With:
============

<td colspan="2" class="tablerow" valign="bottom" style="height: 20px; width: 82%;">

=======================================================================================================
=======
Step 9:
=======
==============================
Go to Administration Panel -> Templates
==============================
===========================
Edit Template: viewthread_multipage
===========================
======
Find:
======

<td colspan="2" class="multi">

============
Replace With:
============

<td colspan="3" class="multi">

=======================================================================================================
======== 
Step 10:
========
==============================
Go to Administration Panel -> Templates
==============================
=====================
Edit Template: forumdisplay
=====================
=====
Find:
=====

<td width="47%" class="header">$lang[textsubject]</td>

============
Replace With:
============

<td width="33%" class="header">$lang[textsubject]</td>
<td width="14%" class="header" align="center">$lang[textrating]</td>

=======================================================================================================
========
Step 11:
========
==============================
Go to Administration Panel -> Templates
==============================
==========================
Edit Template: forumdisplay_admin
==========================
======
Find:
======

<td width="45%" class="header">$lang[textsubject]</td>

============
Replace With:
============

<td width="32%" class="header">$lang[textsubject]</td>
<td width="13%" class="header" align="center">$lang[textrating]</td>

=======================================================================================================
========
Step 12:
========
==============================
Go to Administration Panel -> Templates
==============================
==========================
Edit Template: forumdisplay_thread
==========================
======
Find:
======

<td bgcolor="$altbg2" class="tablerow"><font class="mediumtxt">$prefix<a href="viewthread.php?tid=$thread[tid]">$thread[subject]</a>  $multipage2</font></td>

===========
Add Below:
===========

<td bgcolor="$THEME[altbg1]" class="ctrtablerow">$rating</td>

=======================================================================================================
========
Step 13:
========
==============================
Go to Administration Panel -> Templates
==============================
===============================
Edit Template: forumdisplay_thread_admin
===============================
======
Find:
======

<td bgcolor="$altbg2" class="tablerow"><font class="mediumtxt">$prefix<a href="viewthread.php?tid=$thread[tid]">$thread[subject]</a>  $multipage2</font></td>

===========
Add Below:
===========

<td bgcolor="$THEME[altbg2]" class="ctrtablerow">$rating</td>

=======================================================================================================
========
Step 14:
========
==============================
Go to Administration Panel -> Templates
==============================
=============================
Edit Template: forumdisplay_nothreads
=============================
======
Find:
======
========================================
If the colspan value is different then just increase it by 1.
========================================

colspan="10"

============
Replace With:
============

colspan="11"

=======================================================================================================
========
Step 15:
========
==============================
Go to Administration Panel -> Templates
==============================
=============================
Edit Template: forumdisplay_multipage
=============================
======
Find:
======
========================================
If the colspan value is different then just increase it by 1.
========================================

colspan="10"

============
Replace With:
============

colspan="11"

=======================================================================================================
========
Step 16:
========
==============================
Go to Administration Panel -> Templates
==============================
===============
Edit Template: css
===============
======
Find:
======

.rghttablerow {
    color: $tabletext;
    font-family: $font;
    font-size: $fontsize;
    table-layout: fixed;
    text-align: right;
}

===========
Add Above:
===========

.ratingtd {
    background-color: $header;
    color: $headertext;
    font-family: Verdana;
    font-size: 10px;
    table-layout: fixed;
}

.ratingtr {
    height: 38px;
}

=======================================================================================================
======================================================================
If you have 'Topic Activity' installed, then please do ALL the steps below. Otherwise, skip to step 22.
======================================================================
========
Step 17:
========
=================
Edit File: activity.php
=================
======
Find:
======

while($thread = $db->fetch_array($query)) {

===========
Add Below:
===========

    // Thread Rating Mod Begin
    $rdata = unserialize(stripslashes($thread['ratingdata']));
    
    $rating    = '';
    $ratingnum = count($rdata);
    if ($ratingnum > 0) {
        $trating = floor((array_sum($rdata) / $ratingnum));
        $rating  = str_repeat('<img src="'.$THEME['imgdir'].'/rating-star.png" border="0" alt="*" />', $trating);
        for($i = $trating;$i<5;$i++) {
            $rating .= '<img src="'.$THEME['imgdir'].'/blank-star.png" border="0" alt="*" />';
        }
    } else {
        $rating = str_repeat('<img src="'.$THEME['imgdir'].'/blank-star.png" border="0" alt="*" />', 5);
    }
    // Thread Rating Mod End
    
=======================================================================================================
========
Step 18:
========
==============================
Go to Administration Panel -> Templates
==============================
======================
Edit Template: topic_activity
======================
======
Find:
======

<td align="center" width="43%">$lang[textsubject]</td>

============
Replace With:
============

<td align="center" width="29%">$lang[textsubject]</td>
<td align="center" width="14%">$lang[textrating]</td>

=======================================================================================================
========
Step 19:
========
==============================
Go to Administration Panel -> Templates
==============================
===========================
Edit Template: topic_activity_threads
===========================
======
Find:
======

<td class="tablerow" bgcolor="$THEME[altbg2]">$prefix<a href="viewthread.php?tid=$thread[tid]">$thread[subject]</a> $pages</td>

===========
Add Below:
===========

<td class="ctrtablerow" bgcolor="$THEME[altbg2]">$rating</td>

=======================================================================================================
========
Step 20:
========

==============================
Go to Administration Panel -> Templates
==============================
==========================
Edit Template: topic_activity_none
==========================
======
Find:
======

<td colspan="8">

============
Replace With:
============

<td colspan="9">

=======================================================================================================
========
Step 21:
========
==============================
Go to Administration Panel -> Templates
==============================
=============================
Edit Template: topic_activity_multipage
=============================
======
Find:
======

<td colspan="8" class="multi">

============
Replace With:
============

<td colspan="9" class="multi">

=======================================================================================================
========
Step 22:
========

Upload 'threadratings.js' from the 'Contents' folder to your forum's 'js' directory.
Upload 'rating-star.png' from the 'Contents' folder to your forum's theme directories.
Upload 'blank-star.png' from the 'Contents' folder to your forum's theme directories.

=======================================================================================================
Enjoy!