<?php
/*
 * Created on 14.08.2007
 *
 * @copyright (c) 2010 animegame.eu
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
 *
 */
/******************************************************************************
* Dieser Skript enthält Funktionen für das Implementieren eines Auktionshauses 
* Ein Skript kann diese Funktionen verwenden, um Informationen darzustellen
*******************************************************************************
* Autor: Pascal Proksch
* Erstellungsdatum: 14.08.2007
* Zuletzt verändert: 28.09.2007
*******************************************************************************/

include_once "parse.inc.php";

function stimme($pollid, $polloptionid, $userid, $kommentar){
	if(mysql_num_rows(mysql_query('SELECT * FROM poll_votes WHERE pollid = '.$pollid.' and userid = '.$userid)) == 0){
		// NEU einfuegen!
		$sql = 'INSERT INTO poll_votes(pollid, polloptionid, userid, comment) values ('.$pollid.','.$polloptionid.','.$userid.',\''.$kommentar.'\')';
		mysql_query($sql);
		//echo $sql.'<br>';
	} else{
		mysql_query('UPDATE poll_votes SET polloptionid='.$polloptionid.', comment = \''.encodeNoHTMLNoBB($kommentar).'\' WHERE pollid='.$pollid.' and userid = '.$userid);		
	}
}

// Liefert den Datensatz der Stimme (Nr und eigener Kommentar)
function getStimme($pollid, $userid){
	$qry = mysql_query('SELECT * FROM poll_votes WHERE pollid = '.$pollid.' and userid = '.$userid);
	$result = mysql_fetch_assoc($qry);
	return $result;
}

function getUmfrage($pollid){
	$umfrage = array();
	$qry = mysql_query('SELECT p.thema, p.pollid, p.text, u.nickname FROM poll as p inner join user as u on p.ersteller = u.id WHERE pollid='.$pollid);
	$umfrage['poll'] = mysql_fetch_assoc($qry);
	$qry = mysql_query('SELECT * FROM poll_options WHERE pollid ='.$pollid);
	while($result = mysql_fetch_assoc($qry)){
		$umfrage['options'][$result['polloptionid']] = $result;
	}
	return $umfrage;
}

function getUmfragen($entries, $page){
	$returnArray = array();
	$qry = mysql_query('SELECT p.thema, p.pollid, u.nickname FROM poll as p inner join user as u on p.ersteller = u.id LIMIT '.$entries*$page.','.$entries);
	$i=0;
	while($result=mysql_fetch_assoc($qry)){
		$returnArray[$i++] = $result;
	}
	return $returnArray;
}

function getUmfragenCount(){
	$temp = mysql_fetch_assoc(mysql_query('SELECT count(*) as anzahl FROM poll'));
	return $temp[anzahl];
}

function getComments($pollid, $entries, $page){
	$returnArray = array();
	$sql = 'SELECT polloptionid, comment FROM poll_votes where comment != \'\' and pollid= '.$pollid.' LIMIT '.$entries*$page.','.$entries;
	$qry = mysql_query($sql);
//	echo '<br>'.$sql.'<br>';
	for($i=0;($result = mysql_fetch_assoc($qry)) != null && $i<$entries;$i++){
		$returnArray[$i] = $result;
	}
	return $returnArray;
}

function getCommentCount($pollid){
	$temp = mysql_fetch_assoc(mysql_query('Select count(*) as anzahl from poll_votes where comment != \'\' and pollid= '.$pollid));
	return $temp[anzahl];
}

function getPollResult($pollid){	
	$returnArray = array();
	$sql = 'SELECT text, pv.polloptionid, count(pv.polloptionid) as anzahl FROM poll_votes as pv inner join poll_options as po on pv.polloptionid = po.polloptionid and pv.pollid = po.pollid where pv.pollid = '.$pollid.' group by polloptionid';
	$qry = mysql_query($sql);
//	echo '<br>'.$sql.'<br>';
	while($result = mysql_fetch_assoc($qry)){
		$returnArray[$result[polloptionid]] = $result[anzahl];
	}
	return $returnArray;
}

function isOpen($pollid){
	$sql = 'SELECT count(*) as anzahl from poll where pollid ='.$pollid.' AND (close is null OR close > now())';

	$qry = mysql_query($sql);
	$row = mysql_fetch_assoc($qry);
	if($row['anzahl'] > 0){
		return true;
	}
	return false;
}
?>