<?php
// $Id: poll.php,v 1.21 2003/12/15 03:04:10 eddieajau Exp $
/**
* Content code
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.21 $
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once( $mainframe->getPath( 'front_html' ) );
require_once( $mainframe->getPath( 'class' ) );

$tabclass = "sectiontableentry2,sectiontableentry1";
$polls_graphwidth = 200;
$polls_barheight = 2;
$polls_maxcolors = 5;
$polls_barcolor = 0;

$poll = new mosPoll( $database );

$id = mosGetParam( $_REQUEST, 'id', 0 );
$task = mosGetParam( $_REQUEST, 'task', '' );

switch ($task) {

	case "vote":
		pollAddVote( $id );
		break;

	default:
		pollresult( $id );
		break;
}

function pollAddVote( $uid ) {
	global $database, $mosConfig_offset;

	$sessioncookie = mosGetParam( $_REQUEST, 'sessioncookie', '' );

	if (!$sessioncookie) {
		echo "<h3>"._ALERT_ENABLED."</h3>";
		echo "<input class=\"button\" type=\"button\" value=\""._CMN_CONTINUE."\" onClick=\"window.history.go(-1);\">";
		return;
	}

	$poll = new mosPoll( $database );
	if (!$poll->load( $uid )) {
		echo "<h3>"._NOT_AUTH."</h3>";
		echo "<input class=\"button\" type=\"button\" value=\""._CMN_CONTINUE."\" onClick=\"window.history.go(-1);\">";
		return;
	}

	$cookiename = "voted$poll->id";
	$voted = mosGetParam( $_COOKIE, $cookiename, '0' );

	if ($voted) {
		echo "<h3>"._ALREADY_VOTE."</h3>";
		echo "<input class=\"button\" type=\"button\" value=\""._CMN_CONTINUE."\" onClick=\"window.history.go(-1);\">";
		return;
	}

	$voteid = mosGetParam( $_POST, 'voteid', 0 );
	if (!$voteid) {
		echo "<h3>"._NO_SELECTION."</h3>";
		echo "<input class=\"button\" type=\"button\" value=\""._CMN_CONTINUE."\" onClick=\"window.history.go(-1);\">";
		return;
	}

	setcookie( "$cookiename", '1', time()+$poll->lag );

	$database->setQuery( "UPDATE #__poll_data SET hits=hits + 1"
		."\n WHERE pollid='$poll->id' AND id='$voteid'");

	$database->query();

	$database->setQuery( "UPDATE #__polls SET voters=voters + 1"
		."\n WHERE id='$poll->id'");

	$database->query();

	$now = date("Y-m-d G:i:s");
	$database->setQuery( "INSERT INTO #__poll_date SET date='$now', vote_id='$voteid',	poll_id='$poll->id'");

	$database->query();
	//mosRedirect( sefRelToAbs("index.php?option=com_poll&task=results&id=$uid"), ""._THANKS."" );
	echo "<h3>"._THANKS."</h3>";
	echo "<form action=\"\" method=\"GET\">";
	echo "<input class=\"button\" type=\"button\" value=\""._BUTTON_RESULTS."\" onClick=\"window.location='".sefRelToAbs("index.php?option=com_poll&task=results&id=$uid")."'\">";
	echo "</form>";
}

function pollresult( $uid ) {
	global $database, $mosConfig_offset, $mosConfig_live_site;

	$poll = new mosPoll( $database );
	$poll->load( $uid );

	if (empty($poll->title)) {
		$poll->id = "";
		$poll->title = _SELECT_POLL;
	}

	$first_vote = "";
	$last_vote = "";

	if (isset($poll->id) && $poll->id != "") {
		$database->setQuery( "SELECT MIN(date) AS mindate, MAX(date) AS maxdate"
			."\n FROM #__poll_date"
			."\n WHERE poll_id='$poll->id'"
		);

		$dates = $database->loadObjectList();

		if (isset($dates[0]->mindate)) {
			$first_vote = mosFormatDate( $dates[0]->mindate, _DATE_FORMAT_LC2 );
			$last_vote = mosFormatDate( $dates[0]->maxdate, _DATE_FORMAT_LC2 );
		}
	}

	$database->setQuery( "SELECT a.text, count( DISTINCT b.id ) AS hits,"
		. "\n	count( DISTINCT b.id )/COUNT( DISTINCT c.id )*100.0 AS percent"
		. "\nFROM #__poll_data AS a"
		. "\nLEFT JOIN #__poll_date AS b ON b.vote_id = a.id"
		. "\nLEFT JOIN #__poll_date AS c ON c.poll_id = a.pollid"
		. "\nWHERE a.pollid='$poll->id' AND a.text <> ''"
		. "\nGROUP BY a.id"
		. "\nORDER BY a.id"
	);

	$votes = $database->loadObjectList();
	echo $database->getErrorMsg();

	$database->setQuery( "SELECT id, title"
		. "\nFROM #__polls"
		. "\nWHERE published=1"
		. "\nORDER BY id"
	);

	$polls = $database->loadObjectList();
	echo $database->getErrorMsg();

		reset( $polls );
		$link = sefRelToAbs('index.php?option=com_poll&amp;task=results&amp;id=\' + this.options[selectedIndex].value + \'');
		$pollist = "\n<select name=\"id\" class=\"inputbox\" size=\"1\" width=\"200\" style=\"WIDTH:200px\" onChange=\"if (this.options[selectedIndex].value != '') {document.location.href='".$link."'}\">";
		$pollist .= "\n\t<option value=\"\">"._SELECT_POLL."</option>";
		for ($i=0, $n=count( $polls ); $i < $n; $i++ ) {
			$k = $polls[$i]->id;
			$t = $polls[$i]->title;

			$sel = ($k == intval( $poll->id ) ? " selected=\"selected\"" : '');
			$pollist .= "\n\t<option value=\"".$k."\"$sel>" . $t . "</option>";
		}
		$pollist .= "\n</select>\n";


	poll_html::showResults( $poll, $votes, $first_vote, $last_vote, $pollist );
}
?>
