<?php # $Id: poll.php ,v 1.0 2004/03/31 07:49:07 pcool Exp $

/*
============================================================================== 
	Dokeos - elearning and course management software
	
	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2003 University of Ghent (UGent)
	Copyright (c) 2001 Universite catholique de Louvain (UCL)

	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".
	
	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, or (at your option) any later version.
	
	See the GNU General Public License for more details.
	
	Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
============================================================================== 
*/


/*
============================================================================== 
	   DOKEOS INIT 
============================================================================== 
*/ 

// setting the language file
$langFile="poll";

// global settings initialisation 
// also provides access to main api (inc/lib/main_api.lib.php)
// setting the global file that gets the general configuration, the databases, the languages, ...
include('../../inc/claro_init_global.inc.php');


/*
-----------------------------------------------------------
				Variable Definitions
-----------------------------------------------------------
*/ 
//$is_allowedToEdit = $is_courseAdmin;
$name = $_uid;

// setting the database variables
$poll_cnf["questionTbl"] = $_course["dbNameGlu"]."poll_questions"; 
$poll_cnf["answerTbl"] = $_course["dbNameGlu"]."poll_answers";
$poll_cnf["numberTbl"] = $_course["dbNameGlu"]."poll_number";
$tbl_user = $mysqlMainDb."`.`user";
   

/*
-----------------------------------------------------------
	Header
-----------------------------------------------------------
*/ 
$htmlHeadXtra[]="<script language=\"javascript\" src=\"calendar/popcalendar.js\"></script>";

$htmlHeadXtra[]="<script type=\"text/javascript\" language=\"JavaScript\">

function checkdelete ()
{
	var check=confirm(\"$langDelete\");
	if (check) {return true;}
	else {return false;}
}

function checkstop ()
{
	var check=confirm(\"$langStop\");
	if (check) {return true;}
	else {return false;}
}

</script>";


if(isset($_POST["action"]))
{
   include_once("poll.inc.php");
   export($qid);
   exit;
}

// setting the name of the tool
$nameTools=$langPoll;

/*
-----------------------------------------------------------
			Including some necessary files
-----------------------------------------------------------
*/ 
@include('../../inc/textLib.inc.php');
include_once("poll.inc.php");

Display::display_header($nameTools,'Poll');


/*
-----------------------------------------------------------
			Access Rights
-----------------------------------------------------------
*/ 
// permission stuff
$is_allowedToEdit 	= api_is_allowed_to_edit();
// notice for unauthorized people. 
//if ( !$is_courseAllowed) die ("<center>Not allowed</center>");

/*
============================================================================== 
		MAIN CODE
============================================================================== 
*/

/*
-----------------------------------------------------------
			Sanity Check 
-----------------------------------------------------------
*/ 
if (mysql_query( "SELECT id FROM `" . $poll_cnf["questionTbl"] . "`") == FALSE) 
{	//the poll_question table doesn't exist yet, so create all poll tables
	    mysql_query( "CREATE TABLE `" . $poll_cnf["questionTbl"] . "` (
	  					`questionID` int(11) NOT NULL auto_increment,
						`question` varchar(255) NOT NULL default '',
						`begin` datetime NOT NULL default '0000-00-00 00:00:00',
						`end` datetime NOT NULL default '0000-00-00 00:00:00',
						`answer` enum('0','1') NOT NULL default '1',
						PRIMARY KEY  (`questionID`)
						)");
	    mysql_query( "CREATE TABLE `" . $poll_cnf["answerTbl"] . "` (
	  					`answerID` int(11) NOT NULL auto_increment,
  						`questionID` int(11) NOT NULL default '0',
  						`result` int(10) NOT NULL default '0',
  						`text` varchar(255) NOT NULL default '',
  						`ordre` int(11) default NULL,
  						PRIMARY KEY  (`answerID`)
						)");
      mysql_query( "CREATE TABLE `" . $poll_cnf["numberTbl"] . "` (
	  					`numberID` int(11) NOT NULL auto_increment,
					  	`questionID` int(11) NOT NULL default '0',
					  	`login` char(8) NOT NULL default '',
					  	`answerID` int(11) NOT NULL default '0',
					  	PRIMARY KEY  (`numberID`)
						)");
}

if (mysql_query( "SELECT ordre FROM `" . $poll_cnf["answerTbl"] . "`") == FALSE)
{ 
      mysql_query( "ALTER TABLE `" . $poll_cnf["answerTbl"] . "` ADD `ordre` INT(11);");
}

if (mysql_query( "SELECT answer FROM `" . $poll_cnf["questionTbl"] . "`") == FALSE)
{
      mysql_query( "ALTER TABLE `" . $poll_cnf["questionTbl"] . "` ADD `answer` ENUM( '0', '1' ) default '1' NOT NULL;");
      mysql_query( "ALTER TABLE `" . $poll_cnf["numberTbl"] . "` ADD `answerID` int(11) NOT NULL;");
}

/*
-----------------------------------------------------------
		Admin:  Change visibility
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
  echo "<br><br>";
  
   if (!isset($_GET["res"])) 
   {
  
      /* make invisible */
      if ($_GET["mkInvisbl"])
      {
         make_invisble($qid);
         $message=$langMessageStop;
      }
    
      /* make visible */
      if ($_GET["mkVisbl"])
      {
         if (!isset($visbl))
         {
            echo "<form name=mkVisbl method=\"post\" action=\"".$SERVER[PHP_SELF]."?qid=$qid&mkVisbl=1\">",
                 "<table bgcolor=\"#FFCC00\">",
  	   	        "<tr>",
  		  	        "<td>$langMessageNewEnd</td>",
  		  	        "<td><input type=\"text\" name=\"enddate\" size=\"12\" value=\"".date("d-m-Y")."\">",
    		        " <img src=\"calendar/date.gif\" alt=\"Datum kiezen\" width=\"19\" height=\"17\" border=\"0\" onclick=\"popUpCalendar(this, mkVisbl.enddate, 'dd-mm-yyyy')\"  align=\"absmiddle\"></td>",
    			     "<td>&nbsp;</td>",
    			     "<td><input type=\"text\" name=\"hourb\" size=\"2\" maxlength=\"2\" value=\"".date("H",mktime())."\">:",
    			     "<input type=\"text\" name=\"minb\" size=\"2\" maxlength=\"2\" value=\"".date("i",mktime())."\">:",
    			     "<input type=\"text\" name=\"secb\" size=\"2\" maxlength=\"2\" value=\"".date("s",mktime())."\"> $langNewTime</td>",
    			     "</tr><tr><td bgcolor=\"#FFCC00\">",
                 "<input type=\"hidden\" value=\"1\" name=\"visbl\">",
                 "<input type=\"submit\" value=\"$langGo\" name=\"extra\"></td>",
  		  	        "</tr>",
  		  	        "</table>",
  		  	        "</form>";
         }
         else
         {
            $query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID='$qid'");
            while($res=mysql_fetch_array($query)) 
            { list($date1,$time1)=explode(' ', $res["begin"]); }
            list($y1,$m1,$d1)=explode('-', $date1);
            list($hour1,$min1,$sec1)=explode(':', $time1);
            $begindate=$y1."-".$m1."-".$d1." ".$hour1.":".$min1.":".$sec1; 
            list($d,$m,$y)=explode('-', $enddate);	
            $enddate=$y."-".$m."-".$d." ".$hourb.":".$minb.":".$secb; 
	
            if ($begindate>$enddate) 
            {
               $query=mysql_query("UPDATE `".$poll_cnf["questionTbl"]."` SET end='' WHERE questionID='$qid'");
               $message=$langMessageFalse;
            }
            else
            {
               $query=mysql_query("UPDATE `".$poll_cnf["questionTbl"]."` SET end='$enddate' WHERE questionID='$qid'");
               $message=$langMessageStart;
            }

         }
      }
   }
}

/*
-----------------------------------------------------------
		Admin:  ADD a Poll
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
   if (!isset($_GET["res"])) 
   {
      /* add poll */
      if ($_POST["start"]==$langGo1) 
  	   {
         $today=date("Y-m-d H:i:s",mktime());
         list($da,$ma,$ya)=explode('-', $startdate);	
         list($db,$mb,$yb)=explode('-', $enddate);	
         $begin=$ya."-".$ma."-".$da." ".$houra.":".$mina.":".$seca;
         $end=$yb."-".$mb."-".$db." ".$hourb.":".$minb.":".$secb;
         if ($begin>$end) 
         { 
            $message=$langMessageFalse;
            add_poll($question,$answers,$begin,$end="",$nr,$save);
         }
         else
         {
            $message=$langMessageNew;
            add_poll($question,$answers,$begin,$end,$nr,$save);
         }
  	   }
  	}
}			


/*
-----------------------------------------------------------
		Admin:  EDIT a Poll
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
   if (!isset($_GET["res"])) 
   {
      /* edit poll */
      if ($_POST["start"]==$langChange) 
      {
  	      list($da,$ma,$ya)=explode('-', $startdate);	
         list($db,$mb,$yb)=explode('-', $enddate);	
         $begin=$ya."-".$ma."-".$da." ".$houra.":".$mina.":".$seca;
         $end=$yb."-".$mb."-".$db." ".$hourb.":".$minb.":".$secb;
         if ($begin>$end) 
         { 
            $message=$langMessageFalse;
            $query=mysql_query("UPDATE `".$poll_cnf["questionTbl"]."` SET question='".addslashes($question)."' , begin='$begin' , end='', answer='$save' WHERE questionID='$qid'");
         }
         else
         {
            $message=$langMessageEdit;
            $query=mysql_query("UPDATE `".$poll_cnf["questionTbl"]."` SET question='".addslashes($question)."' , begin='$begin' , end='$end', answer='$save' WHERE questionID='$qid'");
            update_last_tooledit($_course, "Polls", $id, $langChanged, $_uid);
         }
  		
         $query=mysql_query("SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID=$qid ORDER BY ordre ASC");
         $tel=1;
         while($myrow = mysql_fetch_array($query)) 
         {
            $aid[$tel]=$myrow["answerID"];
            $ordre[$tel]=$myrow["ordre"];
            $tel++;
         }	
         for ($i=1;$i<$tel;$i++) 
         {
            $query=mysql_query("UPDATE `".$poll_cnf["answerTbl"]."` SET text='".addslashes($answers[$i])."' , ordre='".$ordre[$i]."' WHERE ((questionID='$qid')and(answerID='$aid[$i]'))");
         }
         mysql_query ("commit");
      }
   }
}

/*
-----------------------------------------------------------
		Admin:  DELETE a Poll
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
   if (!isset($_GET["res"])) 
   { 
      /* delete polls */
      if ($_GET["action"]=='delete') 
      {
         delete_poll($qid);
         unset($action);
         $message=$langMessageDelete;
      }
   }
}
  
/*
-----------------------------------------------------------
		Admin:  Result Message
-----------------------------------------------------------
*/ 
if (api_is_allowed_to_edit()) 
{
   if(isset($message))
   {
    Display::display_normal_message($message);
  	}
   unset($message);
}


/*
-----------------------------------------------------------
		Admin:  Active Polls
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
   if (!isset($_GET["res"])) 
   {
      
      /* active polls */
      echo "<h3>$langActive</h3>";
      switch ($sort1) {
         case "1" : $set="ORDER BY question ASC"; break;
         case "2" : $set="ORDER BY question DESC"; break;
         case "3" : $set="ORDER BY begin ASC"; break;
         case "4" : $set="ORDER BY begin DESC"; break;
         case "5" : $set="ORDER BY end ASC"; break;
         case "6" : $set="ORDER BY end DESC"; break;
      }
    
      $result=all_new_polls($set);  
  
      if (mysql_num_rows($result)!='0') 
      {
         echo "<table border=\"0\">",
              "<tr align=\"center\" bgcolor=\"#666666\">",
              "<td width=\"350\"><font color=\"white\"><i>$langQuestion",
  		  	     "<a href=\"javascript:document.location.href = 'poll.php?sort1=1';\">",
              "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
              "<a href=\"javascript:document.location.href = 'poll.php?sort1=2';\">",
              "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
              "</a></i></font>",
              "</td>",
              "<td width=\"55\"><font color=\"white\"><i>$langAnswers</i></font></td>",
              "<td width=\"200\"><font color=\"white\"><i>$langStart",
              "<a href=\"javascript:document.location.href = 'poll.php?sort1=3';\">",
              "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
              "<a href=\"javascript:document.location.href = 'poll.php?sort1=4';\">",
              "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
              "</a></i></font>",
  	  		     "</td>",
  		        "<td width=\"200\"><font color=\"white\"><i>$langEnd",
              "<a href=\"javascript:document.location.href = 'poll.php?sort1=5';\">",
              "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
              "<a href=\"javascript:document.location.href = 'poll.php?sort1=6';\">",
              "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
              "</a></i></font>",
  		  	     "</td>",
  	  		     "<td width=\"75\"><font color=\"white\"><i>$langVotes</i></font></td>",
  	  		     "<td width=\"60\"><font color=\"white\"><i>$langAnonymous</i></font></td>",
  	  		     "<td width=\"75\"><font color=\"white\"><i>$langOption</i></font></td>",
  	  		     //"<td width=\"60\"><font color=\"white\"><i>$langSave</i></font></td>",
  	  		     "</tr>";
  			 
         while($myrow = mysql_fetch_array($result)) 
         {
            echo "<tr>",
  			  	     "<td width=\"350\">".stripslashes($myrow["question"])."</td>",
  			  	     "<td width=\"55\" align=\"center\">".count_answer($myrow["questionID"])."</td>";
  
            echo "<td width=\"200\">".convert_date($myrow["begin"])."</td>",
  			  	     "<td width=\"200\">".convert_date($myrow["end"])."</td>";
  			
            $total=0; 
            $answer=all_answers($myrow["questionID"]);
     	      while($row=mysql_fetch_array($answer)) { $total=$total+$row["result"]; }
              
            echo "<td width=\"90\" align=\"center\"><a href=\"".$SERVER[PHP_SELF]."?res=yes&qid=".$myrow["questionID"]."\">$total</a>";
            echo "</td>";
            
            if (test_to_export($myrow["questionID"])) { echo "<td width=\"60\">$langYes</td>"; } 
            else { echo "<td width=\"60\">$langNo</td>"; }
            
  			  	echo "<td width=\"75\"><a href=\"".$SERVER[PHP_SELF]."?action=delete&qid=".$myrow["questionID"]."\" onClick=\"return checkdelete();\"><img src=\"../../img/delete.gif\" alt=\"$langImgDelete\" border=\"0\"></a>",
  			        "<a href=\"".$SERVER[PHP_SELF]."?mkInvisbl=1&qid=".$myrow["questionID"]."\" onClick=\"return checkstop();\"><img src=\"../../img/visible.gif\" alt=\"$langImgStop\" border=\"0\"></a>";
  			
            if (test_to_edit($myrow["questionID"])) 
            {
               echo "<a href=\"edit_poll.php?qid=".$myrow["questionID"]."\"><img src=\"../../img/edit.gif\" alt=\"$langImgEdit\" border=\"0\"></a>";
            }
  			    
  			   echo "</td></tr>"; 
  			
         }
         echo "</table><br>";		
      }
  	   else
  	   {
  		  echo $langNoActive;
     	   echo "<br><br>";
  	   }
  
      /* button to insert new poll */
      echo "<form method=\"post\" action=\"new_poll.php\"><input type=\"submit\" value=\"$langNew\"></form><br>";
   }
}

/*
-----------------------------------------------------------
		Admin:  Inactive Polls
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
   if (!isset($_GET["res"])) 
   {
      
      /* inactive polls */
      echo "<h3>$langEnded</h3>";
      switch ($sort2) {
         case "1" : $set2="ORDER BY question ASC"; break;
         case "2" : $set2="ORDER BY question DESC"; break;
         case "3" : $set2="ORDER BY begin ASC"; break;
         case "4" : $set2="ORDER BY begin DESC"; break;
         case "5" : $set2="ORDER BY end ASC"; break;
         case "6" : $set2="ORDER BY end DESC"; break;
      }

  	   $result=all_old_polls($set2);  
  
  	   if (mysql_num_rows($result)!='0') 
  	   {
         echo "<table border=\"0\">",
              "<tr align=\"center\" bgcolor=\"#666666\">",
              "<td width=\"350\"><font color=\"white\"><i>$langQuestion</i></font>",
              "<a href=\"javascript:document.location.href = 'poll.php?sort2=1';\">",
              "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
              "<a href=\"javascript:document.location.href = 'poll.php?sort2=2';\">",
              "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
              "</td>",
              "<td width=\"55\"><font color=\"white\"><i>$langAnswers</i></font></td>",
              "<td width=\"200\"><font color=\"white\"><i>$langStart</i></font>",
              "<a href=\"javascript:document.location.href = 'poll.php?sort2=3';\">",
              "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
              "<a href=\"javascript:document.location.href = 'poll.php?sort2=4';\">",
              "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
              "</td>",
              "<td width=\"200\"><font color=\"white\"><i>$langEnd</i></font>",
              "<a href=\"javascript:document.location.href = 'poll.php?sort2=5';\">",
              "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
              "<a href=\"javascript:document.location.href = 'poll.php?sort2=6';\">",
              "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
              "</td>",
              "<td width=\"75\"><font color=\"white\"><i>$langVotes</i></font></td>",
              "<td width=\"60\"><font color=\"white\"><i>$langAnonymous</i></font></td>",
  	  		     "<td width=\"75\"><font color=\"white\"><i>$langOption</i></font></td>",
              "</tr>";
  			 
         while($myrow = mysql_fetch_array($result)) 
         {
            echo "<tr>",
  			  	     "<td width=\"350\">".stripslashes($myrow["question"])."</td>",
  			  	     "<td width=\"55\" align=\"center\">".count_answer($myrow["questionID"])."</td>";
  				 
            echo "<td width=\"200\">".convert_date($myrow["begin"])."</td>";
  			
            if ($myrow["end"]=="0000-00-00 00:00:00")
            {
               echo "<td width=\"200\"><font color=\"red\"><i>".convert_date($myrow["end"])."</i></font></td>";
            }
            else
            {
               echo "<td width=\"200\">".convert_date($myrow["end"])."</td>";
            }
  			
            $total=0; 
            $answer=all_answers($myrow["questionID"]);
            while($row=mysql_fetch_array($answer)) { $total=$total+$row["result"]; }
              
            echo "<td width=\"90\" align=\"center\"><a href=\"".$SERVER[PHP_SELF]."?res=yes&qid=".$myrow["questionID"]."\">$total</a>";
            echo "</td>";
            
            if (test_to_export($myrow["questionID"])) { echo "<td width=\"60\">$langYes</td>"; } 
            else { echo "<td width=\"60\">$langNo</td>"; }
            
            
  			  	echo "<td width=\"75\"><a href=\"".$SERVER[PHP_SELF]."?action=delete&qid=".$myrow["questionID"]."\" onClick=\"return checkdelete();\"><img src=\"../../img/delete.gif\" alt=\"$langImgDelete\" border=\"0\"></a>",
  			        "<a href=\"".$SERVER[PHP_SELF]."?mkVisbl=1&qid=".$myrow["questionID"]."\"><img src=\"../../img/invisible.gif\" alt=\"$langImgStop\" border=\"0\"></a>";
  		  
            if (test_to_edit($myrow["questionID"])) 
            {
               echo "<a href=\"edit_poll.php?qid=".$myrow["questionID"]."\"><img src=\"../../img/edit.gif\" alt=\"$langImgEdit\" border=\"0\"></a>";
            }
        
            echo "</td></tr>"; 
  			
         }
         echo "</table><br>";		
      }
      else
      {
         echo $langNoEnded;
         echo "<br><br>";
      }
   }
}

/*
-----------------------------------------------------------
		Admin:  Result view
-----------------------------------------------------------
*/ 
if ($is_allowedToEdit) 
{
   if (isset($_GET["res"])) 
   {
   /* show result */
   	api_display_tool_title($langShowResult);
   	show($qid);
   	
   	if (!test_to_export($qid)) 
      {
      	echo "<br><hr><br>";
      	show_users($qid);
      	echo "<br><br>";
      	echo "<form method=\"post\" action=\"".$SERVER[PHP_SELF]."?qid=$qid\">";
         echo "<input type=\"submit\" name=\"action\" value=\"Exporteer als CSV\"></form>";
      }
   }
}



/*
-----------------------------------------------------------
		Student:  view Polls
-----------------------------------------------------------
*/ 
if (!$is_allowedToEdit) 
{
   echo "<br><br>";
  
   if (!isset($_GET["res"])) 
   {
      if (!isset($_POST["vote"])) 
      {
         if (!isset($qid)) 
			{ 
            api_display_tool_title($langAll);	
            $today=date("Y-m-d H:i:s",mktime());
    	  
       	   switch ($sort3) {
               case "1" : $set3="ORDER BY question ASC"; break;
               case "2" : $set3="ORDER BY question DESC"; break;
               case "3" : $set3="ORDER BY begin ASC"; break;
               case "4" : $set3="ORDER BY begin DESC"; break;
               case "5" : $set3="ORDER BY end ASC"; break;
               case "6" : $set3="ORDER BY end DESC"; break;
            }

            $query1=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE '".$today."' BETWEEN begin AND end $set3"); 
       		if (mysql_num_rows($query1)>="1") 
            {
               //echo "LET OP: Wanneer '<font color=\"red\"><b>!</b></font>' naast de poll staat, worden uw gegevens voor deze poll geregistreerd.<br><br>";
               
               echo "<table border=\"0\" width=\"800\">",
         		     "<tr align=\"center\" bgcolor=\"#666666\">",
         		     "<td width=\"350\"><font color=\"white\"><i>$langVotePoll</i>",
         		     "<a href=\"javascript:document.location.href = 'poll.php?sort3=1';\">",
                    "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
                    "<a href=\"javascript:document.location.href = 'poll.php?sort3=2';\">",
                    "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
         		     "</td><td width=\"200\"><font color=\"white\"><i>$langBeginDate</i>",
         		     "<a href=\"javascript:document.location.href = 'poll.php?sort3=3';\">",
                    "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
                    "<a href=\"javascript:document.location.href = 'poll.php?sort3=4';\">",
                    "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
         		     "</td><td width=\"200\"><font color=\"white\"><i>$langEndDate</i>",
         		     "<a href=\"javascript:document.location.href = 'poll.php?sort3=5';\">",
                    "<img src=\"../../img/down.gif\" alt=\"$langImgSort\" border=\"0\">",
                    "<a href=\"javascript:document.location.href = 'poll.php?sort3=6';\">",
                    "<img src=\"../../img/up.gif\" alt=\"$langImgSort\" border=\"0\">",
         		     "</td><td width=\"50\"><font color=\"white\"><i>$langOption</i></td></font>",
         		     "</tr>";
               
               while($myrow=mysql_fetch_array($query1)) 
               { 
                  $query2=mysql_query("SELECT * FROM `".$poll_cnf["numberTbl"]."` WHERE ((login='$name')AND(questionID='".$myrow["questionID"]."'))");
                  $f=0; 
                  while ($row=mysql_fetch_array($query2)) {$f=1;}
      			  
                  echo "<tr>";
                 
                  //if (test_to_export($myrow["questionID"])) { echo "<td><b><font color=\"red\">! </font></b></td>"; }
                  //else { echo "<td> </td>"; }
      			   
      			   echo "<td>";
      			   
      			   if ($f=="0") { echo "<a href=\"".$SERVER[PHP_SELF]."?qid=".$myrow["questionID"]."\">".stripslashes($myrow["question"])."</a>"; }
      			   else { echo stripslashes($myrow["question"]); }
      			   
      			   echo "</td>";	
      			  
      			   $begin=convert_date($myrow["begin"]); 
      			   $end=convert_date($myrow["end"]);
      			   echo "<td>$begin</td>",
      				     "<td>$end</td>";
      				  
            		if ($f=="1") { echo "<td><a href=\"".$SERVER[PHP_SELF]."?res=yes&qid=".$myrow["questionID"]."\">$langResult</a></td>"; }
      			   else { echo "<td>&nbsp;</td>"; }
                	
                	echo "</tr>";
                  $f=0;
                  
               } 
               
            }
            else
            {
               // no active polls
               echo "<table><tr>",
                    "<td>&nbsp;</td>",
       			     "<td>$langNoPolls</td>",
       			     "<td>&nbsp;</td>",
       			     "</tr></table>";
            }
			//}
            echo "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
    	
            // all active polls --> start later
            $query2=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE ((begin >= '".$today."')AND(begin<end)) $set3");
            if (mysql_num_rows($query2)>="1") 
            {
               if (mysql_num_rows($query1)=="0") 
               {
                  echo "<table border=\"0\" width=\"800\">",
            		     "<tr align=\"center\" bgcolor=\"#666666\">",
            		     "<td width=\"350\"><font color=\"white\"><i>$langVotePoll</i></td>",
            		     "<td width=\"200\"><font color=\"white\"><i>$langBeginDate</i></td>",
            		     "<td width=\"200\"><font color=\"white\"><i>$langEndDate</i></td>",
            		     "<td width=\"50\"><font color=\"white\"><i>$langOption</i></td></font>",
            		     "</tr>";
            	}
            
    	         while($myr=mysql_fetch_array($query2)) 
               { 
                  $begin=convert_date($myr["begin"]); 
                  $end=convert_date($myr["end"]);  
       			   echo "<tr>",
       			 	     "<td><font color=\"gray\">".stripslashes($myr["question"])."</td>",
       				     "<td><font color=\"gray\">$begin</td>",
       				     "<td><font color=\"gray\">$end</td>",
       				     "<td>&nbsp;</font>",
       				     "</tr>";
    		      }
            }
            echo "</table><br><br>";
         }

     
         // vote poll
   		if (isset($qid)) 
         {
            api_display_tool_title("Poll");	
            if (!test_to_export($qid)) 
            {
               echo "<font color=\"red\"><u>Opmerking</u>: deze poll is NIET anoniem.</font><br><br><br>";
            }
                	
            
            $query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE (questionID=".$qid.")"); 
            while($myrow=mysql_fetch_array($query)) 
            { 
               echo "<form action=\"".$SERVER[PHP_SELF]."?qid=$qid\" method=\"post\">",
			           "<table border=\"0\" width=\"500\">",
			           "<tr align=\"center\" bgcolor=\"#666666\">",
                    "<td colspan=\"2\"><font color=\"white\"><i>".stripslashes($myrow["question"])."</i></font></tr>";
               $i=0;
               $answer=mysql_query("SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID='$qid' ORDER BY ordre ASC");
               while($row=mysql_fetch_array($answer)) 
               {
                  $i++;
                  echo "<tr>",
				           "<td height=\"40\" width=\"30\"><input type=\"radio\" name=\"response\" value=\"".$row["answerID"]."\"></td>",
				           "<td height=\"40\" width=\"270\">".stripslashes($row["text"])."</td>",
				           "</tr>";
               }
               echo "</tr></table><br>",
			           "<table><tr>",
			           "<td valign=\"top\"><input type=\"submit\" name=\"vote\" value=\"$langVote\"><input type=\"hidden\" name=\"qid\" value=\"$qid\"></form></td>",
			           "<td valign=\"top\"><form action=\"poll.php\" method=\"post\">",
			           "<input type=\"submit\" name=\"back\" value=\"$langCancel\"></form></td>",
			           "</tr></table>";
            }
         }
      }

      /* voted	*/
      if (isset($_POST["vote"])) 
		{
         if (!isset($response)) 
			{
				echo "<META  HTTP-EQUIV=\"Refresh\" Content= \"0; URL=\"".$SERVER[PHP_SELF]."?qid=$qid\">";
			}
			else
			{
				api_display_tool_title($langShowResult);
			   show_result($name,$qid,$response);
			}
		}
	
   }
	else
	{
      /* show result */
		api_display_tool_title($langShowResult);	
		show($qid);
	}

}

/*
============================================================================== 
		DOKEOS FOOTER 
============================================================================== 
*/ 

@include('../../inc/claro_init_footer.inc.php');
?>
