<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: pollFunctions.php,v 1.2.2.4 2004/08/01 17:04:18 kirilt Exp $

function pollMain ($pollID)
{
    global $phpEx, $maxOptions, $uimages, $pollcomm, $mpnTables, $user;

   // $mpn_poll_seperate_rows = 1; #Set to 1 to put the Vote and Resultbutton onto seperate rows.

    if ( !isset( $pollID ) ) { $pollID = 1; }
    if ( !isset( $url ) ) { $url = sprintf("pollBooth.$phpEx?op=results&pollID=%d", $pollID); }

    $result = mysql_query( "SELECT pollTitle, voters FROM ".$mpnTables['poll_desc']." WHERE pollID='$pollID'" );

    list( $pollTitle, $voters ) = mysql_fetch_row($result);

    $result = mysql_query( "SELECT optionText, voteID FROM ".$mpnTables['poll_data']." WHERE pollID='$pollID' AND optionText!=''" );

    for ( $i = 1; $i <= mysql_num_rows( $result ); $i++ )
    {
        $resultArray[] = mysql_fetch_row( $result );
    }

    $boxTitle = translate("Survey");

    $boxContent = "<form action=\"pollBooth.$phpEx\" method=\"post\">";

    $boxContent .= "<input type=\"hidden\" name=\"pollID\" value=\"".$pollID."\"></input>";
    $boxContent .= "<input type=\"hidden\" name=\"forwarder\" value=\"".$url."\"></input>";

    $boxContent .= "<center><font size=2><b>$pollTitle</b></font></center><br />\n";

    foreach( $resultArray as $v )
    {
        $boxContent .= "<div class='t-left'><input type=\"radio\" name=\"voteID\" value='$v[1]'></input>&nbsp;$v[0]</div>\n";
    }

    $boxContent .= "<br />";

    $boxContent .= "<center><button class='button' type='submit'>" . translate( 'Vote' ) . "!</button></center>";

    $boxContent .= "</form>\n";

    if ( _MPN_USER )
    {
        $cookie = cookiedecode();

        //$boxContent .= "<center><a href=\"pollBooth.$phpEx?op=results&pollID=$pollID&mode=$cookie[4]&order=$cookie[5]&thold=$cookie[6]\"><img src=\"$uimages/result.gif\" border = 0></a></center>";

        $boxContent .= "<center><a href=\"pollBooth.$phpEx?op=results&pollID=$pollID&mode=$cookie[4]&order=$cookie[5]&thold=$cookie[6]\"><b>" . translate( 'Results' ) . "</b></a></center>";
    }
    else
    {
        $boxContent .= "<center><a href=\"pollBooth.$phpEx?op=results&pollID=$pollID\"><b>" . translate( 'Results' ) . "</b></a></center>\n";
    }

    $result = mysql_query("SELECT SUM(optionCount) AS SUM FROM ".$mpnTables['poll_data']." WHERE pollID='$pollID'");

    $sum = (int)mysql_result($result, 0, "SUM");

    $boxContent .= "<br />";

    $boxContent .= "<center><font size=1><b><a href=\"pollBooth.$phpEx\">".translate("Past Surveys")."</a></b></font></center><br />";

    if ($pollcomm)
    {
        list($numcom) = mysql_fetch_row(mysql_query("select count(*) from ".$mpnTables['pollcomments']." where pollID=$pollID"));

        $boxContent .= "<center>".translate("Votes: ")."<b>$sum</b><br />".translate("comments:")."  <b>$numcom</b></center><br />\n";
    }
    else
    {
        $boxContent .= "<br /><center>".translate("Votes: ")."<b>$sum</b></center><br />\n";
    }

    themesidebox($boxTitle, $boxContent);
}


function pollLatest()
{
    global $mpnTables;
    $result = mysql_query("SELECT pollID FROM ".$mpnTables['poll_data']." ORDER BY pollID DESC");
    $pollID = mysql_fetch_row($result);
    return($pollID[0]);
}

function pollCollector($pollID, $voteID, $forwarder)
{
    global $maxOptions, $setCookies, $cookiePrefix, $HTTP_COOKIE_VARS, $mpnTables;
    $voteValid = 1;

    if( $setCookies > 0 )
    {
        // we have to check for cookies, so get timestamp of this poll
        $result = mysql_query("SELECT timeStamp FROM ".$mpnTables['poll_desc']." WHERE pollID=$pollID");
        $object = mysql_fetch_object($result);
        $timeStamp = $object -> timeStamp;
        $cookieName = $cookiePrefix . $timeStamp;

        // check if cookie exists
        if( isset( $HTTP_COOKIE_VARS[ $cookieName ] ) )
        {
            // cookie exists, invalidate this vote
            $warn = "You already voted today!";
            $voteValid = 0;
        }
        else
        {
            // cookie does not exist yet, set one now

            bakeCookie( $cookieName, 1, 86400 );
        }
    }
    // update database if the vote is valid
    if($voteValid>0) {
            @mysql_query("UPDATE ".$mpnTables['poll_data']." SET optionCount=optionCount+1 WHERE (pollID=$pollID) AND (voteID=$voteID)");
            @mysql_query("UPDATE ".$mpnTables['poll_desc']." SET voters=voters+1 WHERE pollID=$pollID");
    }

    $_SERVER['F_LIB'] -> redirectRequest( $forwarder, TRUE );
    // a lot of browsers can't handle it if there's an empty page
        echo "<html><head></head><body></body></html>";
}

function pollList()
{
    global $user, $cookie, $mpnTables,$phpEx;

    $result = mysql_query("SELECT pollID, pollTitle, timeStamp, voters FROM ".$mpnTables['poll_desc']." ORDER BY timeStamp");

    $counter = 0;
    echo "<table border=0 cellpadding=8><tr><td>";

    while($object = mysql_fetch_object($result)) {
            $resultArray[$counter] = array($object->pollID, $object->pollTitle, $object->timeStamp, $object->voters);
            $counter++;
    }


    for ($count = 0; $count < count($resultArray); $count++)
    {
            $id = $resultArray[$count][0];
            $pollTitle = $resultArray[$count][1];
            $voters = $resultArray[$count][3];
            $result2 = mysql_query("SELECT SUM(optionCount) AS SUM FROM ".$mpnTables['poll_data']." WHERE pollID=$id");
            $sum = (int)mysql_result($result2, 0, "SUM");

            echo("<li> <a href=\"pollBooth.$phpEx?pollID=$id\">$pollTitle</a> ");

            if ( _MPN_USER )
            {
                echo("[  <a href=\"pollBooth.$phpEx?op=results&pollID=$id&mode=$cookie[4]&order=$cookie[5]&thold=$cookie[6]\">".translate("Results")."</a> - $sum ".translate("votes")."  ]\n");
            }
            else
            {
                echo("[  <a href=\"pollBooth.$phpEx?op=results&pollID=$id&mode=thread&order=0&thold=0\">".translate("Results")."</a> - $sum ".translate("votes")."  ]\n");
            }

    }
    echo "</td></tr></table>";
}

function pollResults($pollID) {
    global $maxOptions, $BarScale, $resultTableBgColor, $resultBarFile, $setCookies, $mpnTables;
    global $phpEx;
    if(!isset($pollID)) $pollID = 1;
    $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM ".$mpnTables['poll_desc']." WHERE pollID=$pollID");
    $holdtitle = mysql_fetch_row($result);
    echo "<br /><b>$holdtitle[1]</b><br /><br />";
    mysql_free_result($result);
    $result = mysql_query("SELECT SUM(optionCount) AS SUM FROM ".$mpnTables['poll_data']." WHERE pollID=$pollID");
    $sum = (int)mysql_result($result, 0, "SUM");
    mysql_free_result($result);
    echo "<table>";
    // cycle through all options
    for($i = 1; $i <= $maxOptions; $i++) {
            // select next vote option
            $result = mysql_query("SELECT pollID, optionText, optionCount, voteID FROM ".$mpnTables['poll_data']." WHERE (pollID=$pollID) AND (voteID=$i)");
            $object = mysql_fetch_object($result);
            if(is_object($object)) {
                    $optionText = $object->optionText;
                    $optionCount = $object->optionCount;
                    if($optionText != "") {
                            echo "<td>";
                            echo "$optionText";
                            echo "</td>";
                            if($sum) {
                                    $percent = 100 * $optionCount / $sum;
                            } else {
                                    $percent = 0;
                            }
                            echo "<td>";
                            $percentInt = (int)$percent * 4 * $BarScale ;
                            $percent2 = (int)$percent;
                            if ($percent > 0) {
                                    echo "<img src=\"images/leftbar.gif\" height=14 width=7>";
                                    echo "<img src=\"images/mainbar.gif\" height=14 width=$percentInt Alt=\"$percent2 %\">";
                                    echo "<img src=\"images/rightbar.gif\" height=14 width=7>";
                            } else {
                                    echo "<img src=\"images/leftbar.gif\" height=14 width=7 Alt=\"$percent2 %\">";
                                    echo "<img src=\"images/mainbar.gif\" height=14 width=3 Alt=\"$percent2 %\">";
                                    echo "<img src=\"images/rightbar.gif\" height=14 width=7 Alt=\"$percent2 %\">";
                            }
                            printf(" %.2f %% (%d)", $percent, $optionCount);
                            echo "</td>";
                    }
            }
            echo "</tr>";
    }
    echo "</td></tr></table><br />";
    echo "<center><b>".translate("Total Votes: ")."$sum</b><br />";
    if($setCookies>0) {
        echo "<font size=1>".translate("We allow just one vote per day")."</font><br /><br /><font size=3>";
    } else {
        echo "<br /><br />";
    }
    $booth = $pollID;
    echo("[ <a href=\"pollBooth.$phpEx?pollID=$booth\">".translate("Voting Booth")."</a> | ");
    echo("<a href=\"pollBooth.$phpEx\">".translate("Other Polls")."</a> ]</font>");
    echo "</center>\n";
    return(1);
}

?>
