<?php
if (! defined ( 'BLOCK_FILE' )) {
	if (function_exists ( "location" ))
		location ( "../index.php" );
	else
		die ();
	exit ();
}

global $CURUSER, $tracker_lang, $ss_uri, $CURGROUP;
// Get current poll
list ( $blocktitle, $content ) = ReadCache ( 'polls', true, true );
if (! $content) {
	$res = sql_query ( "SELECT * FROM polls ORDER BY added DESC LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
	if ($pollok = (mysql_num_rows ( $res ))) {
		$arr = mysql_fetch_assoc ( $res );
		$pollid = $arr ["id"];
		$userid = $CURUSER ["id"];
		$question = $arr ["question"];
		$o = array ($arr ["option0"], $arr ["option1"], $arr ["option2"], $arr ["option3"], $arr ["option4"], $arr ["option5"], $arr ["option6"], $arr ["option7"], $arr ["option8"], $arr ["option9"], $arr ["option10"], $arr ["option11"], $arr ["option12"], $arr ["option13"], $arr ["option14"], $arr ["option15"], $arr ["option16"], $arr ["option17"], $arr ["option18"], $arr ["option19"] );
		
		// Check if user has already voted
		if ($CURGROUP ['can_vote']) {
			$res = sql_query ( "SELECT * FROM pollanswers WHERE pollid=$pollid AND userid=$userid" ) or sqlerr ( __FILE__, __LINE__ );
			$arr2 = mysql_fetch_assoc ( $res );
		}
	}
	$blocktitle = $tracker_lang ['poll'] . WriteCurgroup ( "<font class=\"small\"> - [<a class=\"altlink\" href=\"makepoll.php?returnto=main\"><b>" . $tracker_lang ['create'] . "</b></a>]" . WriteCurgroup ( $pollok ? " - [<a class=\"altlink\" href=\"makepoll.php?action=edit&pollid=$arr[id]&returnto=main\"><b>" . $tracker_lang ['edit'] . "</b></a>]" : "", "\$CURGROUP ['can_editpoll']" ) . WriteCurgroup ( $pollok ? " - [<a class=\"altlink\" href=\"polls.php?action=delete&pollid=$arr[id]&returnto=main\"><b>" . $tracker_lang ['delete'] . "</b></a>]" : "", "\$CURGROUP ['can_delpoll']" ) . "</font>", "\$CURGROUP ['can_poll']" );
	
	if ($pollok) {
		$content .= ("<p align=\"center\"><b>$question</b></p>\n");
		$voted = $arr2;
		if ($voted || ! $CURGROUP ['can_vote']) {
			// display results
			if ($arr ["selection"])
				$uservote = $arr ["selection"];
			else
				$uservote = - 1;
				// we reserve 255 for blank vote.
			$res = sql_query ( "SELECT selection FROM pollanswers WHERE pollid=$pollid AND selection < 20" ) or sqlerr ( __FILE__, __LINE__ );
			
			$tvotes = mysql_num_rows ( $res );
			
			$vs = array (); // array of
			$os = array ();
			
			// Count votes
			while ( $arr2 = mysql_fetch_row ( $res ) )
				$vs [$arr2 [0]] += 1;
			
			reset ( $o );
			for($i = 0; $i < count ( $o ); ++ $i)
				if ($o [$i])
					$os [$i] = array ($vs [$i], $o [$i] );
			
			function srt($a, $b) {
				if ($a [0] > $b [0])
					return - 1;
				if ($a [0] < $b [0])
					return 1;
				return 0;
			}
			
			// now os is an array like this: array(array(123, "Option 1"), array(45, "Option 2"))
			if ($arr ["sort"] == "yes")
				usort ( $os, srt );
			
			$content .= ("<table class=\"main\" align=\"center\" width=\"350\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
			$i = 0;
			while ( $a = $os [$i] ) {
				if ($i == $uservote)
					$a [1] .= "&nbsp;*";
				if ($tvotes == 0)
					$p = 0;
				else
					$p = round ( $a [0] / $tvotes * 100 );
				if ($i % 2)
					$c = "";
				else
					$c = " bgcolor=\"#eeeeee\"";
				$content .= ("<tr><td width=\"1%\" class=\"embedded\"$c><nobr>" . htmlspecialchars_uni ( $a [1] ) . "&nbsp;&nbsp;</nobr></td><td width=\"130%\" class=\"embedded\"$c><nobr>" . "<img src=\"./themes/$ss_uri/images/bar_left.gif\"><img src=\"./themes/$ss_uri/images/bar.gif\" height=\"12\" width=\"" . ($p * 3) . "\"><img src=\"./themes/$ss_uri/images/bar_right.gif\"> $p%</nobr></td></tr>\n");
				++ $i;
			}
			$content .= ("</table>\n");
			$tvotes = number_format ( $tvotes );
			$content .= ("<p align=\"center\">" . $tracker_lang ['votes'] . ": $tvotes</p>\n");
		} else {
			if ($CURGROUP ['can_vote']) {
				$content .= ("<form method=\"post\" action=\"index.php\">\n");
				$i = 0;
				
				while ( $a = $o [$i] ) {
					$content .= ("<input type=\"radio\" name=\"choice\" value=\"$i\">" . htmlspecialchars_uni ( $a ) . "<br />\n");
					++ $i;
				}
				
				$content .= ("<br />");
				$content .= ("<input type=\"radio\" name=\"choice\" value=\"255\">" . $tracker_lang ['blank_vote'] . "<br />\n");
				$content .= ("<p align=\"center\"><input type=\"submit\" value=\"" . $tracker_lang ['vote'] . "!\" class=\"btn\"></p>");
				$content .= ("</form>");
			}
		}
	} else {
		$content .= "<table class=\"main\" align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\"><tr><td class=\"text\">";
		$content .= "<div align=\"center\"><h3>" . $tracker_lang ['no_polls'] . "</h3></div>\n";
		$content .= "</td></tr></table>";
	}
	$content .= "<div align=\"center\"><a href=\"polls.php\">" . $tracker_lang ['old_polls'] . "</a></div>\n";
	$content = WriteCurgroup ( $content, '$CURGROUP["can_viewpoll"]' );
	$content .= WriteBlockTitle ( $blocktitle );
	$content = WriteCache ( $content );
	$blocktitle = $tracker_lang ['poll'] . ($CURGROUP ['can_poll'] ? "<font class=\"small\"> - [<a class=\"altlink\" href=\"makepoll.php?returnto=main\"><b>" . $tracker_lang ['create'] . "</b></a>]" . ($pollok && $CURGROUP ['can_editpoll'] ? " - [<a class=\"altlink\" href=\"makepoll.php?action=edit&pollid=$arr[id]&returnto=main\"><b>" . $tracker_lang ['edit'] . "</b></a>]" : "") . ($pollok && $CURGROUP ['can_delpoll'] ? " - [<a class=\"altlink\" href=\"polls.php?action=delete&pollid=$arr[id]&returnto=main\"><b>" . $tracker_lang ['delete'] . "</b></a>]" : "") . "</font>" : "");
}
?>