Location: PHPKode > projects > ProNuke. Postnuke To Oracle > postnuke/html/includes/blocks/poll.php
<?php
// File: $Id: poll.php,v 1.30 2001/12/04 13:07:44 jgm Exp $ $Name:  $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 by the Post-Nuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Francisco Burzi
// Purpose of file:
// ----------------------------------------------------------------------

$blocks_modules['poll'] = array(
    'func_display' => 'blocks_poll_block',
    'func_edit' => 'blocks_poll_select',
    'func_update' => 'blocks_poll_update',
    'text_type' => 'Poll',
    'text_type_long' => 'Display poll',
    'allow_multiple' => true,
    'form_content' => false,
    'form_refresh' => false,
    'show_preview' => true
);

addinstanceschemainfo('Pollblock::', 'Block title::');

/*********************************************************/
/* poll functions                                        */
/*********************************************************/

//FTO Fonction call changed to get block position
//function pollMain($pollID, $title="") {
function pollMain($pollID, $blockrow) {
    global $boxTitle, $pollcomm, $user, $cookie, $pntable, $dbconn;
    if(!isset($pollID)) {
        $pollID = 1;
    }
//FTO Title
    $title = $blockrow['title'];

    if(!isset($url)) {
        $url = sprintf("modules.php?op=modload&amp;name=NS-Polls&amp;file=index&amp;req=results&amp;pollID=%d", $pollID);
    }
    $boxContent = "<form action=\"modules.php?op=modload&amp;name=NS-Polls&amp;file=index\" method=\"post\">";
    $boxContent .= "<input type=\"hidden\" name=\"pollID\" value=\"".$pollID."\">";
    $boxContent .= "<input type=\"hidden\" name=\"forwarder\" value=\"".$url."\">";
    $column = &$pntable['poll_desc_column'];
    $result = $dbconn->Execute("SELECT $column[polltitle], $column[voters]
                              FROM $pntable[poll_desc]
                              WHERE $column[pollid]=$pollID");
    if ($result->EOF) {
        return;
    }

    list($pollTitle, $voters) = $result->fields;
    $result->Close();

    if ((!authorised(0, 'Polls::', "$title::$pollID", ACCESS_OVERVIEW)) ||
        (!authorised(0, 'Pollblock::', "$title::$pollID", ACCESS_READ))) {
        return;
    }
 
    $boxContent .= "<font class=\"pn-normal\"><b>$pollTitle</b></font><br><br>\n"; 

    $column = &$pntable['poll_data_column'];
    $result = $dbconn->Execute("SELECT $column[voteid], $column[optiontext]
                                FROM $pntable[poll_data]
                                WHERE ($column[pollid]=$pollID
                                  AND $column[optiontext] IS NOT NULL ) ORDER BY $column[voteid]");


//FTO. Deleted LIKE clause because result is not portable with other databases
// Empty option text are tested in the following loop
//    $result = $dbconn->Execute("SELECT $column[optiontext], $column[optioncount] FROM $pntable[poll_data] WHERE ($column[pollid]=$pollID AND $column[optiontext] NOT LIKE \"\") ORDER BY $column[voteid]");


//FTO. Check EOF and database error

    if (!$result) {
        PN_DBMsgError($dbconn, __FILE__, __LINE__, "An error ocurred");
        die();
    }

    while(!$result->EOF) {
      list($voteid, $optionText) = $result->fields;
      //FTO Check empty option text
      $optionText=trim($optionText);
      if (!empty($optionText))
      {
        if (authorised(0, 'Polls::', "$title::$pollID", ACCESS_COMMENT)) {
            $boxContent .= "<input type=\"radio\" name=\"voteID\" value=\"$voteid\" class=\"r-button\"> <font class=\"pn-normal\">$optionText</font><br>\n";
        } else {
            $boxContent .= "&middot;&nbsp;<font class=\"pn-normal\">$optionText</font><br>\n";
        }
      }

      $result->MoveNext();

    }
      if (authorised(0, 'Polls::', "$title::$pollID", ACCESS_COMMENT)) {
        $boxContent .= "<br><center><font class=\"pn-normal\"><input class=\"pn-button\" type=\"submit\" value=\""._VOTE."\"></font></center><br>";
      }

    if (is_user($user)) {
        cookiedecode($user);
    } else {
        $cookie = array();
        $cookie = array_pad($cookie, 7, '');
    }
    $column = &$pntable['poll_data_column'];
    $result = $dbconn->Execute("SELECT SUM($column[optioncount]) AS sum
                              FROM $pntable[poll_data]
                              WHERE $column[pollid]=$pollID");
    list($sum) = $result->fields;
    $boxContent .=  '<center><font class="pn-normal">[ ';
    if (authorised(0, 'Polls::', "$title::$pollID", ACCESS_READ)) {
        $boxContent .= "<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=NS-Polls&amp;file=index&amp;req=results&amp;pollID=$pollID&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\"><b>"._RESULTS."</b></a> | ";
    }
    $boxContent .= '<a class="pn-normal" href="modules.php?op=modload&amp;name=NS-Polls&amp;file=index"><b>'._POLLS.'</b></a> ]</font></center><br>';
    if ($pollcomm) {
        $column = &$pntable['pollcomments_column'];
        $comres = $dbconn->Execute("SELECT count(*) FROM $pntable[pollcomments] WHERE $column[pollid]=$pollID");
        list($numcom) = $comres->fields;
        $boxContent .= "<center><font class=\"pn-normal\">"._NUMVOTES.": <b>$sum</b> | "._PCOMMENTS." <b>$numcom</b></font></center>\n\n";
    } else {
        $boxContent .= "<center><font class=\"pn-normal\">"._NUMVOTES.": <b>$sum</b></font></center>\n\n";
    }
    $boxContent .= "</form>\n\n";
    $row['title'] = _SURVEY.": $title";
    $row['content'] = $boxContent;
//FTO Add position
    $row['position'] = $blockrow['position'];
//FTO Add bid
    $row['bid'] = $blockrow['bid'];

    themesideblock($row);
}

function pollLatest() {
    global $pntable, $dbconn, $multilingual, $currentlang; /* added multilingual globals */
    if ($multilingual == 1) {
        $column = &$pntable['poll_desc_column'];
// FTO Add IS NUll for Oracle
// ORACLEDONE
        $querylang = "WHERE ($column[planguage]='$currentlang' OR $column[planguage]='' OR $column[planguage] IS NULL)";
    } else {
        $querylang = '';
    }
    $column = &$pntable['poll_desc_column'];
	$sql = "SELECT $column[pollid] FROM $pntable[poll_desc] $querylang ORDER BY $column[pollid] DESC";
    $result = $dbconn->SelectLimit($sql,1);
	
    $pollID = $result->fields;
    return($pollID[0]);
}

function pollNewest() {
    $pollID = pollLatest();
    pollMain($pollID);
}

function pollCollector($pollID, $voteID, $forwarder) {
    global $setCookies, $cookiePrefix, $HTTP_COOKIE_VARS, $pntable, $dbconn;
    /* Fix for lamers that like to cheat on polls */
    $ip = getenv("REMOTE_ADDR");
    $past = time()-1800;
    $column = &$pntable['poll_check_column'];
    $dbconn->Execute("DELETE FROM $pntable[poll_check] WHERE $column[time]<$past");
    $result = $dbconn->Execute("SELECT $column[ip] FROM $pntable[poll_check] WHERE $column[ip]='$ip'");
    list($ips) = $result->fields;
    $ctime = time();
    if ($ip == $ips) {
        $voteValid = 0;
    } else {
        $column = &$pntable['poll_check_column'];
        $dbconn->Execute("INSERT INTO $pntable[poll_check] ($column[ip], $column[time]) VALUES ('$ip', '$ctime')");
        $voteValid = "1";
    }
    /* Fix end */

    if($setCookies>0) {
        /* we have to check for cookies, so get timestamp of this poll */
        $column = &$pntable['poll_desc_column'];
        $result = $dbconn->Execute("SELECT $column[timestamp] FROM $pntable[poll_desc] WHERE $column[pollid]=$pollID");
        list($timeStamp) = $result->fields;
        $cookieName = $cookiePrefix.$timeStamp;
        /* check if cookie exists */
        if($HTTP_COOKIE_VARS["$cookieName"] == "1") {
            /* cookie exists, invalidate this vote */
            $warn = "You already voted today!";
            $voteValid = "0";
        } else {
            /* cookie does not exist yet, set one now */
            $cvalue = "1";
            setcookie("$cookieName",$cvalue,time()+86400);
        }
    }
    /* update database if the vote is valid */
    if($voteValid>0) {
        $column = &$pntable['poll_data_column'];
        $dbconn->Execute("UPDATE $pntable[poll_data] SET $column[optioncount]=$column[optioncount]+1 WHERE ($column[pollid]=$pollID) AND ($column[voteid]=$voteID)");
        $column = &$pntable['poll_desc_column'];
        $dbconn->Execute("UPDATE $pntable[poll_desc] SET $column[voters]=$column[voters]+1 WHERE $column[pollid]=$pollID");
    }

    pnRedirect($forwarder);
}

function pollList() {
    global $user, $cookie, $pntable, $dbconn, $multilingual, $currentlang; /* added multilingual globals */
    
    if (!authorised(0, 'Polls::', "::", ACCESS_OVERVIEW)) {
        return;
    }

    if ($multilingual == 1) {
        $column = &$pntable['poll_desc_column'];
// FTO Add IS NUll for Oracle
// ORACLEDONE

        $querylang =  "WHERE ($column[planguage]='$currentlang'  OR $column[planguage]='' OR $column[planguage] IS NULL)";
    } else {
        $querylang = "";
    }
    $column = &$pntable['poll_desc_column'];
    $result = $dbconn->Execute("SELECT $column[pollid], $column[polltitle], $column[timestamp], $column[voters] FROM $pntable[poll_desc] $querylang ORDER BY $column[timestamp]");
    OpenTable();
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._PASTSURVEYS."</b></font></center>";
    CloseTable();
    echo "<br>";
    echo "<table border=\"0\" cellpadding=\"8\"><tr><td>";
    echo "<font class=\"pn-normal\">";
    $counter = 0;

    //FTO : Test EOF and database error
    if (!$result) {
        PN_DBMsgError($dbconn, __FILE__, __LINE__, "An error ocurred");
        die();
    }
    while(!$result->EOF) {

        $thisresult = $result->fields;
        $result->MoveNext();
        $resultArray[$counter] = $thisresult;
        $counter++;
    }
    for ($count = 0; $count < count($resultArray); $count++) {
        $id = $resultArray[$count][0];
        $pollTitle = $resultArray[$count][1];
        $voters = $resultArray[$count][3];
        $column = &$pntable['poll_data_column'];
        $result2 = $dbconn->Execute("SELECT SUM($column[optioncount]) AS sum FROM $pntable[poll_data] WHERE $column[pollid]=$id");
        list($sum) = $result2->fields;
        echo "<strong><big>&middot;</big></strong>&nbsp;<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=NS-Polls&amp;file=index&amp;pollID=$id\">$pollTitle</a> ";
        echo "(<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=NS-Polls&amp;file=index&amp;req=results&amp;pollID=$id&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\">"._RESULTS."</a> - $sum "._LVOTES.")<br>\n";
    }
    echo "</td></tr></table>";
    CloseTable();
}

function pollResults($pollID) {
    global $BarScale, $resultTableBgColor, $resultBarFile, $setCookies, $Default_Theme, $user, $cookie, $pntable, $dbconn;
    if(!isset($pollID)) $pollID = 1;
    $column = &$pntable['poll_desc_column'];
    $result = $dbconn->Execute("SELECT $column[polltitle] FROM $pntable[poll_desc] WHERE $column[pollid]=$pollID");
    list($holdtitle) = $result->fields;
    echo "<br><font class=\"pn-normal\"><b>$holdtitle</b></font><br><br>";
    $result->Close();
    $column = &$pntable['poll_data_column'];
    $result = $dbconn->Execute("SELECT SUM($column[optioncount]) AS sum FROM $pntable[poll_data] WHERE $column[pollid]=$pollID");
    list($sum) = $result->fields;
    $result->Close();
    echo "<table border=\"0\">";
    /* cycle through all options */
    $column = &$pntable['poll_data_column'];

    $result = $dbconn->Execute("SELECT $column[optiontext],
                                 $column[optioncount] FROM $pntable[poll_data]
                               WHERE ($column[pollid]=$pollID
                                 AND $column[optiontext] IS NOT NULL )
                                 ORDER BY $column[voteid]");

//FTO. Deleted LIKE clause because result is not portable with other databases
// Empty option text are tested in the following loop
//    $result = $dbconn->Execute("SELECT $column[optiontext], $column[optioncount] FROM $pntable[poll_data] WHERE ($column[pollid]=$pollID AND $column[optiontext] NOT LIKE \"\") ORDER BY $column[voteid]");

//FTO Check database error

    if (!$result) {
        PN_DBMsgError($dbconn, __FILE__, __LINE__, "An error ocurred");
        die();
    }

    while(!$result->EOF) {
      list($optionText, $optionCount) = $result->fields;
      $result->MoveNext();

      //FTO Remove empty option text from the list
      $optionText=trim($optionText);
      if ( !empty($optionText) )
       {
        echo "<tr><td>";
        echo "<font class=\"pn-normal\">$optionText</font>";
        echo "</td>";
        if($sum) {
            $percent = 100 * $optionCount / $sum;
        } else {
            $percent = 0;
        }
        echo "<td>";
        $percentInt = (int)$percent * 4 * $BarScale;
        $percent2 = (int)$percent;
        if(is_user($user)) {
            if($cookie[9]=="") $cookie[9]=$Default_Theme;
            if(!$file=@opendir("themes/$cookie[9]")) {
                $ThemeSel = $Default_Theme;
            } else {
                $ThemeSel = $cookie[9];
            }
        } else {
            $ThemeSel = $Default_Theme;
        }
        if ($percent > 0) {
            echo "<img src=\"themes/$ThemeSel/images/leftbar.gif\" height=\"15\" width=\"7\" Alt=\"$percent2 %\">";
            echo "<img src=\"themes/$ThemeSel/images/mainbar.gif\" height=\"15\" width=\"$percentInt\" Alt=\"$percent2 %\">";
            echo "<img src=\"themes/$ThemeSel/images/rightbar.gif\" height=\"15\" width=\"7\" Alt=\"$percent2 %\">";
        } else {
            echo "<img src=\"themes/$ThemeSel/images/leftbar.gif\" height=\"15\" width=\"7\" Alt=\"$percent2 %\">";
            echo "<img src=\"themes/$ThemeSel/images/mainbar.gif\" height=\"15\" width=\"3\" Alt=\"$percent2 %\">";
            echo "<img src=\"themes/$ThemeSel/images/rightbar.gif\" height=\"15\" width=\"7\" Alt=\"$percent2 %\">";
        }
        printf("<font class=pn-normal> %.2f %% (%d)</font>", $percent, $optionCount);
        echo "</td></tr>";
      }
    }
    echo "</table><br>";
    echo "<center><font class=\"pn-normal\">";
    echo "<b>"._TOTALVOTES." $sum</b><br>";
    if($setCookies>0) {
        echo "</font><font class=\"pn-sub\">"._ONEPERDAY."</font><font class=\"pn-normal\"><br><br>";
    } else {
        echo "<br><br>";
    }
    $booth = $pollID;
    echo("[ <a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=NS-Polls&amp;file=index&amp;pollID=$booth\">"._VOTING."</a> | ");
    echo("<a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=NS-Polls&amp;file=index\">"._OTHERPOLLS."</a> ]</font></center>");
    return(1);
}

function blocks_poll_block($row) {
// for MSSQL that alwasys have an space
    $row['content'] = trim($row['content']);
    if (!empty($row['content'])) {
        $pollID = $row['content'];
    } else {
        $pollID = pollLatest();
    }
//FTO call chenged to get position
    pollMain($pollID, $row);
}

function blocks_poll_select($row)
{

    $zerochecked = "";
    $onechecked = "";

    if (!empty($row['content'])) {
        $pollID = $row['content'];
        $showspecific = 1;
        $onechecked = "checked";
    } else {
        $showspecific = 0;
        $zerochecked = "checked";
    }

    $output = "<tr><td class=\"pn-title\">"._POLL_DISPLAY.":</td></tr>";
    $output .= "<tr><td class=\"pn-normal\">"._POLL_LATEST."</td><td><input type=\"radio\" name=\"polltype\" value=\"0\" $zerochecked></td></tr>";
    $output .= "<tr><td class=\"pn-normal\">"._POLL_SPECIFIC."</td><td><input type=\"radio\" name=\"polltype\" value=\"1\" $onechecked>&nbsp;&nbsp; Poll ID:<INPUT TYPE=\"TEXTBOX\" NAME=\"pollid\" VALUE=\"$row[content]\" SIZE=\"3\"></tr>";

    return $output;
}

function blocks_poll_update($row) {

    if (($row['polltype'] == 1) && (!empty($row['pollid']))) {
        $row['content'] = $row['pollid'];
    } else {
        $row['content'] = "";
    }

    return($row);
}

?>
Return current item: ProNuke. Postnuke To Oracle