<?php
 require "include/bittorrent.php";
 dbconn(false);
 loggedinorreturn();


function cleanit($array, $index, $maxlength)
  {
    if (isset($array["{$index}"]))
    {
       $input = substr($array["{$index}"], 0, $maxlength);
       $input = mysql_real_escape_string($input);
       return ($input);
    }
    return NULL;
  }
 
 //$action = $_GET["action"];
 $pollid = cleanit($_GET, "id", 2);
 //$returnto = $_GET["returnto"];
 
 if (get_user_class() <= UC_MODERATOR)
   stderr("Error", "Permission denied.");
   
 stdhead("Polls Overview");
 
 if (!(isset($_GET['id']))) {
   
$sql = mysql_query("SELECT id, added, question FROM polls ORDER BY id DESC") or sqlerr();
//$sql = db_query("SELECT id, added, question FROM polls ORDER BY id DESC");


?>

<table cellpadding="4" cellspacing="1" border="0" style="width:750px" class="tablea">
 <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%"><span class="normalfont"><center><b>..::Hier werden alle User aufgelistet die an denn Umfragen teilgenommen haben::..</b></center></span></td> 
 </tr><tr><td width="100%" class="tablea">
<?

print("<p><table width=750 border=0 cellspacing=0 cellpadding=5><tr>\n" .
 "<td class=tableb align=center>ID</td><td class=tableb>Umfrage hinzugefügt am</td><td class=tableb>Frage</td></tr>\n");
 
 if (mysql_num_rows($sql) == 0) {
  print("<tr><td colspan=2>Sorry...There are no users that voted!</td></tr></table>");
  stdfoot();
  exit;
  }
 
 while ($poll = mysql_fetch_assoc($sql))
 {
  $added = gmdate("Y-m-d h-i-s",strtotime($poll['added'])) . " GMT (" . (get_elapsed_time(sql_timestamp_to_unix_timestamp($poll["added"]))) . " ago)";
  print("<tr><td align=center><a href=\"polloverview.php?id={$poll['id']}\">{$poll['id']}</a></td><td>{$added}</td><td><a href=\"polloverview.php?id={$poll['id']}\">{$poll['question']}</a></td></tr>\n");
 
 }
 
 print("</table>\n");
 
} else {

if (isset($_GET['id'])) {
   
$sql = mysql_query("SELECT * FROM polls WHERE id = {$pollid} ORDER BY id DESC") or sqlerr();



?>

<table cellpadding="4" cellspacing="1" border="0" style="width:750px" class="tablea">
 <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%"><span class="normalfont"><center><b>Umfrage</b></center></span></td> 
 </tr><tr><td width="100%" class="tablea">
<?

print("<p><table width=750 border=0 cellspacing=0 cellpadding=5><tr>\n" .
 "<td class=tableb align=center>ID</td><td class=tableb>Hinzugefügt am</td><td class=tableb>Frage</td></tr>\n");
 
 if (mysql_num_rows($sql) == 0) {
  print("<tr><td colspan=2>Sorry...There are no polls with that ID!</td></tr></table>");
  stdfoot();
  exit;
  }
 
 while ($poll = mysql_fetch_assoc($sql))
 {
  $o = array($poll["option0"], $poll["option1"], $poll["option2"], $poll["option3"], $poll["option4"],
   $poll["option5"], $poll["option6"], $poll["option7"], $poll["option8"], $poll["option9"],
   $poll["option10"], $poll["option11"], $poll["option12"], $poll["option13"], $poll["option14"],
   $poll["option15"], $poll["option16"], $poll["option17"], $poll["option18"], $poll["option19"]);
   
  $added = gmdate("Y-m-d h-i-s",strtotime($poll['added'])) . " GMT (" . (get_elapsed_time(sql_timestamp_to_unix_timestamp($poll["added"]))) . " ago)";
  print("<tr><td align=center><a href=\"polloverview.php?id={$poll['id']}\">{$poll['id']}</a></td><td>{$added}</td><td><a href=\"polloverview.php?id={$poll['id']}\">{$poll['question']}</a></td></tr>\n");
 
 }
 
 print("</table><br>\n");
 
 ?>
</table><br>
<table cellpadding="4" cellspacing="1" border="0" style="width:750px" class="tablea">
 <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%"><span class="normalfont"><center><b>Antworten der Umfrage</b></center></span></td> 
 </tr><tr><td width="100%" class="tablea">
<?
 print("<table width=750 border=0 cellspacing=0 cellpadding=5><tr><td class=tableb>Nr</td><td class=tableb>Antworten</td></tr>\n");
 foreach($o as $key=>$value) {
  if($value != "")
  print("<tr><td>{$key}</td><td>{$value}</td></tr>\n");
  }
 print("</table>\n");
 //print_r($o);
 
 $sql2 = mysql_query("SELECT pollanswers. * , users.username FROM pollanswers LEFT JOIN users ON users.id = pollanswers.userid WHERE pollid = {$pollid} AND selection < 20 ORDER  BY users.id DESC ") or sqlerr();
 
 ?>
</table><br>
<table cellpadding="4" cellspacing="1" border="0" style="width:750px" class="tablea">
 <tr class="tabletitle" width="100%">
  <td colspan="10" width="100%"><span class="normalfont"><center><b>Welcher User hat was gevotet?</b></center></span></td> 
 </tr><tr><td width="100%" class="tablea">
<?

print("<p><table width=750 border=0 cellspacing=0 cellpadding=5><tr>\n" .
 "<td class=tableb align=center>Benutzer ID</td><td class=tableb>Antwort</td></tr>\n");
 
 if (mysql_num_rows($sql2) == 0) {
  print("<tr><td colspan=2>Sorry...There are no users that voted!</td></tr></table>");
  stdfoot();
  exit;
  }
 
 while ($useras = mysql_fetch_assoc($sql2))
 {
  $username  = ($useras['username'] ? $useras['username'] : "Unknown");
  //$useras['selection']--;
  print("<tr><td>{$username}</td><td>{$o[$useras['selection']]}</td></tr>\n");
 }
 print("</table>\n");

}
}



stdfoot();
?>