============================================================================================================================
Modification Name: Thread Rating

Version: 2.1

Author: GuldantheWarlock (GuldantheWarlock@xmbgarage.com)

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.5 SP1

License Note: This modification is released under the GPL. A copy is provided with this software package.

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 modification.
Please backup your files before installing this mod.
Neither XMB Garage nor the author can be held responsible if your board stops functioning properly due to you installing this hack.

Author Note:
You downloaded this modification from XMBGarage.com, the #1 source for XMB related downloads.
Please visit http://www.xmbgarage.com/ for support.
============================================================================================================================
=======
Step 1:
=======

====================================
Go To Administration Panel --> Insert Raw SQL
====================================
===================================
Paste The Following Code and Submit Changes:
===================================

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 Code:
==========

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

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

$wollocation = addslashes($url);
    
===============
Add Code 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 Code:
==========

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

===============
Add Code 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="'.$imgdir.'/rating-star.png" border="0" alt="*" />', $trating);
        for($i = $trating;$i<5;$i++) {
            $rating .= '<img src="'.$imgdir.'/blank-star.png" border="0" alt="*" />';
        }
    } else {
        $rating = str_repeat('<img src="'.$imgdir.'/blank-star.png" border="0" alt="*" />', 5);
    }
    // Thread Rating Mod End

============================================================================================================================
=======
Step 5:
=======
===================
Edit File: viewthread.php
===================
==========
Find Code:
==========

if (isset($goto) && $goto == 'lastpost') {

===============
Add Code 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 $table_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 $table_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 $table_threads SET ratingdata='".addslashes(serialize($rdata))."' WHERE tid='$tid'");

            echo 'ok';
            break;
    }
    exit;
}
// Thread Rating Mod End

============================================================================================================================
=======
Step 6:
=======
===============
Edit File: post.php
===============
==========
Find Code:
==========

        $db->query("INSERT INTO $table_threads (
        
==================
Find Code In This Line:
==================

) VALUES ('', 

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

, ratingdata

===========================
Add To End Of Line BEFORE  ')");
===========================

, 'a:0:{}'

============================================================================================================================
=======
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 Code:
==========

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

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

<tr class="ratingtr">
<td width="18%" class="header">$lang[textauthor] </td>
<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 Code:
==========

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

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

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

==========
Find Code:
==========

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

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

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

==========
Find Code:
==========

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

================
Replace Code 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 Code:
==========

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

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

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

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

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

================
Replace Code 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 Code:
==========

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

================
Replace Code 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 Code:
==========

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

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

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

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

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

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

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

============================================================================================================================
========
Step 14:
========
===============================
Go To Administration Panel --> Templates
===============================
=============================
Edit Template: forumdisplay_nothreads
=============================
==========
Find Code:
==========

colspan="10"

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

colspan="11"

============================================================================================================================
========
Step 15:
========
===============================
Go To Administration Panel --> Templates
===============================
=============================
Edit Template: forumdisplay_multipage
=============================
==========
Find Code:
==========

colspan="7"

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

colspan="8"

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

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

===============
Add Code 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 Code:
==========

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

===============
Add Code 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="'.$imgdir.'/rating-star.png" border="0" alt="*" />', $trating);
        for($i = $trating;$i<5;$i++) {
            $rating .= '<img src="'.$imgdir.'/blank-star.png" border="0" alt="*" />';
        }
    } else {
        $rating = str_repeat('<img src="'.$imgdir.'/blank-star.png" border="0" alt="*" />', 5);
    }
    // Thread Rating Mod End
    
============================================================================================================================
========
Step 18:
========
===============================
Go To Administration Panel --> Templates
===============================
======================
Edit Template: topic_activity
======================
==========
Find Code:
==========

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

================
Replace Code 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 Code:
==========

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

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

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

============================================================================================================================
========
Step 20:
========
===============================
Go To Administration Panel --> Templates
===============================
==========================
Edit Template: topic_activity_none
==========================
==========
Find Code:
==========

<td colspan="8">

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

<td colspan="9">

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

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

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

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

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

Upload 'threadratings.js' from the 'Contents' folder to your forum's 'include' 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!