<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied block surveys MAXIMUS directly...');

if (defined('ADMIN_ALLOW')) {
	$block_retention  = CACHE_BL_SURVEYS;
	$block_name_cache = "SurveyAdmin";
	$extension        = "php";
} elseif (defined('USER_ALLOW')) {
	$block_retention  = CACHE_BL_SURVEYS;
	$block_name_cache = "SurveyUser";
	$extension        = "html";
} else {
	$block_retention  = CACHE_BL_SURVEYS;
	$block_name_cache = "Survey";
	$extension        = "html";
}

if ( $content = opencacheb ( "".$block_name_cache."", "".$extension."", "".$block_retention."" ) ) {
 } else {

global $multilingual, $currentlang, $boxTitle, $content, $pollcomm, $user, $cookie;

$multilingual = addslashes( $multilingual ) ;
$currentlang  = addslashes( $currentlang ) ;

if ($multilingual == 1) {
    $querylang = "WHERE `planguage`='$currentlang' AND `artid`='0'";
} else {
    $querylang = "WHERE `artid`='0'";
}

$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `pollID` FROM `".TABLE_POLL_DESC."` $querylang ORDER BY `pollID` DESC LIMIT 1"));
$pollID = $GLOBALS['db']->XMFnum($row['pollID']);
if ($pollID == 0 || $pollID == "") {
    $content = "";
} else {
    if (!isset($url)) {
	$url = sprintf("modules.php?name=Surveys&amp;op=results&amp;pollID=%d", $pollID);
    }
    $content = "<form action=\"modules.php?name=Surveys\" method=\"post\">";
    $content .= "<input type=\"hidden\" name=\"pollID\" value=\"".$pollID."\" />";
    $content .= "<input type=\"hidden\" name=\"forwarder\" value=\"".$url."\" />";
    $row2 = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `pollTitle`, `voters` FROM `".TABLE_POLL_DESC."` WHERE `pollID`='$pollID'"));
    $pollTitle = stripslashes($row2['pollTitle']);
    $voters   = $GLOBALS['db']->XMFnum($row2['voters']);
    $boxTitle = _SURVEY;
    $content .= "<font class=\"content\"><b>$pollTitle</b></font><br /><br />\n";
    $content .= "<table border=\"0\" width=\"100%\">";
    for($i = 1; $i <= 12; $i++) {
	$row3 = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `pollID`, `optionText`, `optionCount`, `voteID` FROM `".TABLE_POLL_DATA."` WHERE (`pollID`='$pollID') AND (`voteID`='$i')"));
	if(isset($row3)) {
	    $optionText = stripslashes($row3['optionText']);
	    if ($optionText != "") {
		$content .= "<tr><td valign=\"top\"><input type=\"radio\" name=\"voteID\" value=\"".$i."\"></td><td width=\"100%\"><font class=\"content\">$optionText</font></td></tr>\n";
	    }
	}
    }
    $content .= "</table><br /><center><font class=\"content\"><input type=\"submit\" value=\""._VOTE."\" /></font><br />";
    if (defined('USER_ALLOW')) {
	cookiedecode($user);
    }
    for($i = 0; $i < 12; $i++) {
	$pollID = addslashes( $pollID ) ;
	$i      = addslashes( $i ) ;
	$row4 = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `optionCount` FROM `".TABLE_POLL_DATA."` WHERE (`pollID`='$pollID') AND (`voteID`='$i')"));
	$optionCount = $GLOBALS['db']->XMFnum($row4['optionCount']);
	$sum = (int)$sum+$optionCount;
    }
    $content .= "<br /><font class=\"content\"><a href=\"modules.php?name=Surveys&amp;op=results&amp;pollID=$pollID&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\"><b>"._RESULTS."</b></a><br /><a href=\"modules.php?name=Surveys\"><b>"._POLLS."</b></a><br />";

    if ($pollcomm) {
	$numcom = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_POLLCOMMENTS."` WHERE `pollID`='$pollID'"));
	$content .= "<br />"._VOTES.": <b>$sum</b> <br /> "._PCOMMENTS." <b>$numcom</b>\n\n";
    } else {
	$content .= "<br />"._VOTES." <b>$sum</b>\n\n";
    }
    $content .= "</font></center></form>\n\n";
}

closecacheb ( "".$content."", "".$block_name_cache."", "".$extension."", "".$block_retention."" );
}