============================================================================================================================
Modification Name: Improved Polls

Version: 1.1

Description: This modification will replace the current poll system by one that looks exactly the same.
But the polls will no longer make use of members' usernames, but of the members' UserID's instead.
So now the spaces in usernames are no longer an issue.
What is also an option is that Administrators can see who voted for what answer.

Compatibility: 1.9.5 SP1

Version: 1.1

Code Designed By: FunForum

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

Notes:
+ For your own safety, backup the effected PHP files and templates before proceeding with this modification.
    --  viewthread.php
    --  post.php
    --  viewthread_poll template
    --  viewthread_poll_options template
    --  viewthread_poll_options_view template
    --  viewthread_poll_submitbutton template
+ This modification will add 1 query in the viewthread.php IF your thread contains a Poll.
+ This modification can be extended with some great extensions, if someone feels the need to write them that is.
   This is also the reason the polls.php was created instead of moving the 'view' function into an existing file.
   If you do feel the need to write an extension, PLEASE, inform me in case I might want a copy of the extension myself ;-)

Version History:
- 2006 / 06 / 14 : Initial release.
- 2006 / 07 / 09 : Update: Error when topic was closed, no results were shown. Added instructions for Portal.

If you have the "Portal System v2.4" installed on your board and the "XMB Portal Poll Block v1.0" installed on your portal system,
then please check out the "XMB_Portal_Poll_Block_v1.0-Update.txt" which was included into this ZIP file.
============================================================================================================================
=======
Step 1.
=======
=================
Edit File: header.php
=================
(You can skip this step if the sanitize.inc.php file is already in your include folder)
(If you don't have this already then you will need to upload the provide file located in the Contents folder)
=========================================================================
==========
Find Code:
==========

require ROOT.'xmb.php';

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

require ROOT.'include/sanitize.inc.php';

============================================================================================================================
=======
Step 2.
=======
====================================
Go To Administration Panel --> Insert Raw SQL
====================================
========================
Add Code and Submit Changes:
========================

CREATE TABLE `$table_polls` (
    `pod` INT( 6 ) NOT NULL AUTO_INCREMENT,
    `tid` INT( 10 ) NOT NULL,
    `answer` TEXT NOT NULL,
    `votes` INT( 6 ) NOT NULL,
    `voters` TEXT NOT NULL,
    PRIMARY KEY ( `pod` )
) TYPE=MyISAM COMMENT='FunForum: Table containing Poll-data';

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

// Improved Polls Mod Begin
$lang['polls_01'] = 'Poll - Menu'; $lang['polls_02'] = 'Check who voted for what'; $lang['polls_03'] = 'The members who voted for this answer:'; $lang['polls_04'] = '<em>No members.</em>'; $lang['polls_05'] = 'Back to the menu ...'; $lang['polls_06'] = 'In total there were '; $lang['polls_07'] = ' votes in this poll, '; $lang['polls_08'] = ' of those votes were for this option. That is '; $lang['polls_09'] = '% of all votes.'; $lang['polls_10'] = 'Options'; $lang['polls_11'] = 'View the Poll ...'; $lang['polls_12'] = 'ATTENTION: this is an old poll that got converted into the new system.<br />Because of this we can only display WHO voted in this poll, but not WHAT they voted for.<br /><br />The voters were:<br />';
// Improved Polls Mod End

============================================================================================================================
=======
Step 4:
=======

Upload provided PHP Files to your forum: polls.php and polls_converter.php

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

        if ( $forums['pollstatus'] != "off") {
            if (isset($pollanswers)) {
                $pollanswers = checkInput($pollanswers);
                if (strpos($pollanswers, '#|#') !== false || strpos($pollanswers, '||~|~||') !== false) {
                    $pollanswers = '';
                }else{
                    $pollops = explode("\n", $pollanswers);
                    $pollanswers = "";
                    $pnumnum = count($pollops);
                    if ( $pnumnum < 2 && $pollanswers != '') {
                        error($lang['too_few_pollopts']);
                    }
                    for($pnum = 0; $pnum < $pnumnum; $pnum++) {
                        if ( $pollops[$pnum] != "") {
                            $pollanswers .= "$pollops[$pnum]||~|~|| 0#|#";
                        }
                    }

                    $pollanswers = str_replace("\n", '', $pollanswers);
                }
            } else {
                $pollanswers = '';
            }
        }

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

        // Improved Polls Mod Begin
        $pollstatus = '';
        if ($forums['pollstatus'] != 'off') {
            $pollanswers = fetchFromRequest('pollanswers', X_POST);
            if (!is_null($pollanswers)) {
                $pollanswers = checkInput($pollanswers);
                $pollops = explode("\n", $pollanswers);
                $pnumnum = count($pollops);
                if ($pnumnum < 2 && $pollanswers != '') {
                    error($lang['too_few_pollopts']);
                } $answers = array();
                   for($pnum = 0;
                   $pnum < $pnumnum; $pnum++) {
                   $answers[] = ($pollops[$pnum] == "" ? "" : $pollops[$pnum] );
               } $pollstatus = 'open';
            }
        }
        // Improved Polls Mod End

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

'$pollanswers'

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

'$pollstatus'

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

        $tid = $db->insert_id();

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

        // Improved Polls Mod Begin
        if ($pollstatus == 'open') {
            foreach ($answers as $ans) {
                $db->query("INSERT INTO ".$tablepre."polls ( pod, tid, answer, votes, voters ) VALUES ('', '$tid', '$ans', '0', '|#|')");
            }
        }
        // Improved Polls Mod End

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

                    $db->query("DELETE FROM $table_threads WHERE tid='$tid'");

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

                    // Improved Polls Mod Begin
                    $db->query("DELETE FROM ".$tablepre."polls WHERE tid='$tid'");
                    // Improved Polls Mod End

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

    // Start polls
    $that = array();
    $poll = '';

    if ($thread['pollopts'] != '' && $forum['pollstatus'] != 'off' && $thread['closed'] != 'yes') {
        $pollbar = '';
        $num = array();
        $pollhtml = '';

        $options = explode("#|#", $thread['pollopts']);
        $num_options = count($options);

        $viewresults = '';
        if (false !== strpos(' '.$options[$num_options-1].' ', ' '.$xmbuser.' ') || $viewresults == 'yes') {
            //show the 'voted' look
            if ($viewresults == 'yes') {
                $results = "[<a href=\"./viewthread.php?tid=$tid\">$lang[backtovote]</a>]";
            } else {
                $results = '';
            }

            $num_votes = 0;

            for ($i=0; $i < ($num_options-1); $i++) {
                $that = explode('||~|~||', $options[$i]);
                $num_votes += $that[1];
                $poll[$i]['name'] = postify($that[0], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                $poll[$i]['votes'] = $that[1];
            }

            foreach ($poll as $num=>$array) {
                $pollimgnum = 0;
                $pollbar = '';

                if ($array['votes'] > 0) {
                    $orig = round($array['votes']/$num_votes*100, 2);
                    $percentage = round($orig, 2);
                    $poll_length = round($orig/3, 2);
                    $pollbar = str_repeat('<img src="'.$imgdir.'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />', $poll_length);
                    $percentage .= '%';
                } else {
                    $percentage = '0%';
                }
                eval("\$pollhtml .= \"".template('viewthread_poll_options_view')."\";");

            }

            $buttoncode = '';
        } else {
            $results = '[<a href="./viewthread.php?tid='.$tid.'&viewresults=yes">'.$lang['viewresults'].'</a>]';
            for ($i=0;$i<($num_options-1);$i++) {
                $that = explode('||~|~||', $options[$i]);
                $poll['name'] = postify($that[0], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                eval("\$pollhtml .= \"".template('viewthread_poll_options')."\";");
            }
            eval("\$buttoncode = \"".template('viewthread_poll_submitbutton')."\";");
        }
        eval("\$poll = \"".template('viewthread_poll')."\";");

    } elseif ($thread['closed'] == 'yes' && $thread['pollopts'] != '') {
        $pollbar = '';
        $pollhtml = '';

        $options = explode("#|#", $thread['pollopts']);
        $num_options = count($options);
        $num_votes = 0;

        for ($i=0; $i < ($num_options-1); $i++) {
            $that = explode('||~|~||', $options[$i]);
            $num_votes += $that[1];
            $poll[$i]['name'] = postify($that[0], 'no', 'no', 'yes', 'no', 'yes', 'yes');
            $poll[$i]['votes'] = $that[1];
        }

        foreach ($poll as $array) {
            $pollimgnum = 0;
            $pollbar = '';

            if ($array['votes'] > 0) {
                $percentage = round(round(($array['votes'])/$num_votes*100,2)/3, 2);
                for($num = 0; $num < $percentage; $num++) {
                    $pollbar .= '<img src="'.$imgdir.'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />';
                }
                $percentage .= '%';
            } else {
                $percentage = '0%';
            }
            eval("\$pollhtml .= \"".template('viewthread_poll_options_view')."\";");
        }

        $buttoncode = '';
        eval("\$poll = \"".template('viewthread_poll')."\";");
    }
    // End Polls

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

    // Improved Polls Mod Begin
    // Start Polls
    if ($thread['pollopts'] != '') {
        $tpolls = array();
        $totalvotes = 0;
        $allvoters = $pollbar = $pollhtml = '';
        $querypoll = $db->query("SELECT * FROM ".$tablepre."polls WHERE tid='$tid' ORDER BY pod ASC");
        while ($post = $db->fetch_array($querypoll)) {
            if (!empty($post['answer'])) {
                $tpolls[] = $post;
            }
            $totalvotes += $post['votes'];
            $allvoters .= $post['voters'].'|#|';
        }
        $db->free_result($querypoll);

        if ($forum['pollstatus'] != 'off' && $thread['closed'] != 'yes') {
            $viewresults = fetchFromRequest('viewresults', X_GET|X_POST);
            if (false !== strpos($allvoters, '|#|'.$self['uid'].'|#|') || $viewresults == 'yes') {
                //show the 'voted' look
                if ($viewresults == 'yes') {
                    $results = '[<a href="./viewthread.php?tid='.$tid.'">'.$lang['backtovote'].'</a>]';
                } else {
                    $results = '';
                }
                foreach ($tpolls as $answer) {
                    $pollimgnum = 0;
                    $pollbar = '';

                    if ($answer['votes'] > 0) {
                        $orig = round($answer['votes']/$totalvotes*100, 2);
                        $percentage = round($orig, 2);
                        $poll_length = round($orig/3, 2);
                        $pollbar = str_repeat('<img src="'.$imgdir.'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />', $poll_length);
                        $percentage .= '%';
                    } else {
                        $percentage = '0%';
                    }
                    $answer['answer'] = postify($answer['answer'], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                    eval('$pollhtml .= "'.template('viewthread_poll_options_view').'";');
                }
                $buttoncode = '';
            } else {
                $results = '[<a href="./viewthread.php?tid='.$tid.'&viewresults=yes">'.$lang['viewresults'].'</a>]';
                foreach ($tpolls as $answer) {
                    $answer['answer'] = postify($answer['answer'], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                    eval('$pollhtml .= "'.template('viewthread_poll_options').'";');
                }
                eval('$buttoncode = "'.template('viewthread_poll_submitbutton').'";');
            }
            $results .= ( X_ADMIN ? ' [<a href="./polls.php?tid='.$tid.'&action=menu">'.$lang['polls_10'].'</a>]' : '' );
            eval('$poll = "'.template('viewthread_poll').'";');
        } elseif ( $thread['closed'] == 'yes' ) {
            $buttoncode = '';
            $pollbar = '';
            $pollhtml = '';
            foreach ($tpolls as $answer) {
                $pollimgnum = 0;
                $pollbar = '';
                if ( $answer['votes'] > 0 ) {
                    $poll_length = round(round($answer['votes']/$totalvotes*100, 2)/3, 2);
                    $pollbar = str_repeat('<img src="'.$imgdir.'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />', $poll_length);
                    $percentage .= '%';
                } else {
                    $percentage = '0%';
                }
                $answer['answer'] = postify($answer['answer'], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                eval('$pollhtml .= "'.template('viewthread_poll_options_view').'";');
            }
            $results = (X_ADMIN ? ' [<a href="./polls.php?tid='.$tid.'&action=menu">'.$lang['polls_10'].'</a>]' : '');
            eval('$poll = "'.template('viewthread_poll').'";');
        }
    }
    // End Polls
    // Improved Polls Mod End

============================================================================================================================
=======
Step 7.
=======
===================
Edit File: topicadmin.php
===================
==================
Find Code and Delete:
==================

    case 'votepoll':
        nav($lang['textvote']);
        break;

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

            $db->query("DELETE FROM $table_threads WHERE tid='$tid'");

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

            // Improved Polls Mod Begin
            $db->query("DELETE FROM ".$tablepre."polls WHERE tid='$tid'");
            // Improved Polls Mod End

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

            $db->query("DELETE FROM $table_threads WHERE tid='$othertid'");

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

            // Improved Polls Mod Begin
            $db->query("DELETE FROM ".$tablepre."polls WHERE tid='$othertid'");
            // Improved Polls Mod End

============================================================================================================================
=======
Step 8.
=======
==============
Edit File: cp.php
==============
==========
Find Code:
==========

                        $db->query("DELETE FROM $table_threads WHERE tid='$thread[tid]'");

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

                        // Improved Polls Mod Begin
                        $db->query("DELETE FROM ".$tablepre."polls WHERE tid='$thread[tid]'");
                        // Improved Polls Mod End

============================================================================================================================
=======
Step 9.
=======
===============
Edit File: cp2.php
===============
==========
Find Code:
==========

                $db->query("DELETE FROM $table_threads WHERE tid IN($tids)");

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

                // Improved Polls Mod Begin
                $db->query("DELETE FROM ".$tablepre."polls WHERE tid IN($tids)");
                // Improved Polls Mod End

============================================================================================================================
========
Step 10.
========
===============================
Go To Administration Panel --> Templates
===============================
=======================
Edit Template: viewthread_poll
=======================
====================
Replace All Content With:
====================

<form method="post" name="poll" action="polls.php?action=votepoll&fid=$fid&tid=$tid">
<table cellspacing="0" cellpadding="0" border="0" width="$tablewidth" align="center">
<tr>
<td bgcolor="$bordercolor"><table border="0" cellspacing="$borderwidth" cellpadding="$tablespace" width="100%">
<tr>
<td class ="category" colspan="3">
<font color="$cattext"><strong>$lang[textpoll] $thread[subject] $results</strong></font>
</td>
</tr>
$pollhtml
$buttoncode
</table>
</td>
</tr>
</table>
</form>
<br />

============================================================================================================================
========
Step 11.
========
===============================
Go To Administration Panel --> Templates
===============================
=============================
Edit Template: viewthread_poll_options
=============================
====================
Replace All Content With:
====================

<tr class="tablerow">
<td bgcolor="$altbg2" colspan="3"><input type="radio" name="postopnum" value="$answer[pod]" />
<font class="smalltxt">$answer[answer]</font></td>
</tr>

============================================================================================================================
========
Step 11.
========
===============================
Go To Administration Panel --> Templates
===============================
=================================
Edit Template: viewthread_poll_options_view
=================================
====================
Replace All Content With:
====================

<tr class="tablerow">
<td bgcolor="$altbg2" width="30%">
<font class="smalltxt">$answer[answer]</font>
</td>
<td bgcolor="$altbg2" width="60%">
<img src="$imgdir/pollbar-s.gif" alt="$lang[altpollpercentage]" />$pollbar<img src="$imgdir/pollbar-e.gif" alt="$lang[altpollpercentage]" />
</td>
<td bgcolor="$altbg2" width="10%">
<font class="smalltxt">$answer[votes] ($percentage)</font>
</td>
</tr>

============================================================================================================================
========
Step 12.
========
===============================
Go To Administration Panel --> Templates
===============================
=================================
Edit Template: viewthread_poll_submitbutton
=================================
====================
Replace All Content With:
====================

<tr class="ctrtablerow">
<td bgcolor="$altbg2" colspan="3"> <input type="submit" class="submit" value="$lang[submitvote]" />
</td>
</tr>

============================================================================================================================
========
Step 13.
========

Visit the "polls_convertor.php", eg; http://www.yourforum.ext/polls_convertor.php
Delete this file after it has been run.

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