<?php # $Id: template.php,v 1.9 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
============================================================================== 
*/



/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function all_new_polls($sort) 
{
	global $poll_cnf;

	$today=date("Y-m-d H:i:s",mktime());
	$query="SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE ('$today' < end) $sort";
	$result = mysql_query($query);
	return $result;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function all_old_polls($sort) 
{
	global $poll_cnf;

	$today=date("Y-m-d H:i:s",mktime());
	$query="SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE ('$today' >= end) $sort";
	$result = mysql_query($query);
	return $result;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function all_answers($id) 
{
	global $poll_cnf;

  $query="SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID=$id ORDER BY answerID ASC";
  $result=mysql_query($query);
  return $result;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function test_to_edit($qid)
{
	global $poll_cnf;
	
	$query=mysql_query("SELECT * FROM `".$poll_cnf["numberTbl"]."` WHERE questionID=$qid"); 
	if (mysql_num_rows($query)=="0") { $check=true; } else { $check=false; }
	return $check;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function test_to_export($qid)
{
	global $poll_cnf;
	
	$query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID=$qid"); 
	while($myrow = mysql_fetch_array($query)) 
   {
      if ($myrow["answer"]=="1") { $ex=true; } else { $ex=false; }
   	return $ex;
	}
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function convert_date($date)
{
	list($date,$time)=explode(' ', $date);
	list($y,$m,$d)=explode('-', $date);
	list($hour,$min,$sec)=explode(':', $time);
	$output=$d."-".$m."-".$y."  (".$hour.":".$min.":".$sec.")";
	return $output;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function delete_poll($qid)
{
	global $poll_cnf;
	
  $query=mysql_query("DELETE FROM `".$poll_cnf["questionTbl"]."` WHERE questionID='$qid'");
  $query=mysql_query("DELETE FROM `".$poll_cnf["answerTbl"]."` WHERE questionID='$qid'");
  $query=mysql_query("DELETE FROM `".$poll_cnf["numberTbl"]."` WHERE questionID='$qid'");
	mysql_query ("commit");
} 

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function count_answer($id)
{
	global $poll_cnf;
	$query=mysql_query("SELECT COUNT(answerID) FROM `".$poll_cnf["answerTbl"]."` WHERE questionID = $id");
   	$count=mysql_result($query,0,"COUNT(answerID)");
	return $count;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function maxQuestionId() 
{
	global $poll_cnf;
	$query=mysql_query("SELECT MAX(questionID) FROM `".$poll_cnf["questionTbl"]."`");
   	$res=mysql_result($query,0,"MAX(questionID)");
   	$maxqid=$res+1;
	return $maxqid;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function maxAnswerId() 
{
	global $poll_cnf;
   	$query=mysql_query("SELECT MAX(answerID) FROM `".$poll_cnf["answerTbl"]."`");
   	$res=mysql_result($query,0,"MAX(answerID)");
   	$maxaid=$res+1;	
	return $maxaid;
} 	

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function add_poll($question,$answers,$begin,$end,$nr,$save) 
{
	global $poll_cnf, $_uid, $_course, $langNewAdded; 
	$maxaid=maxAnswerId();
	$maxqid=maxQuestionId();
	$query=mysql_query("INSERT INTO `".$poll_cnf["questionTbl"]."` (questionID,question,begin,end,answer) VALUES ('$maxqid','".addslashes($question)."','$begin','$end','$save')"); 
	for ($i=1;$i<=$nr;$i++) 
	{
		$query=mysql_query("INSERT INTO `".$poll_cnf["answerTbl"]."` (answerID,questionID,result,text,ordre) VALUES ('$maxaid','$maxqid','0','".addslashes($answers[$i])."','$i')"); 
		$maxaid=maxAnswerId();
   	}
		update_last_tooledit($_course, "Polls", mysql_insert_id(), $langNewAdded, $_uid);
	$maxqid=maxQuestionId();
	mysql_query ("commit");
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function make_invisble($qid)
{
	global $poll_cnf;
	$today=date("Y-m-d H:i:s",mktime());
	$query=mysql_query("UPDATE `".$poll_cnf["questionTbl"]."` SET end='$today' WHERE questionID='$qid'");
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function show($qid) 
{
	global $poll_cnf;
	global $langTotal,$langBack;
	$query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID=$qid"); 
  while($myrow=mysql_fetch_array($query)) 
	{ 
   	echo "<table border=\"1\">",
		     "<tr>",
		     "<td><table border=\"0\" width=\"500\"><tr>",
		     "<td align=\"center\" bgcolor=\"#666666\"><font color=\"white\"><i>".stripslashes($myrow["question"])."</i></font></td>",
		     "</tr><tr>",
		     "<td><table>";
			 
	  $query1=mysql_query("SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID=".$myrow["questionID"]." ORDER BY ordre ASC");
	  $total = 0;
	  while($row=mysql_fetch_array($query1)) {$total=$total+$row["result"];}
	          	
		$query1=mysql_query("SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID=".$myrow["questionID"]." ORDER BY ordre ASC");
	  while($row=mysql_fetch_array($query1)) 
		{
	   	if ($total=="0") {$percent=0;} else {$percent=$row["result"]/$total;}
	    $percent=$percent*100; 
			echo "<tr>",
				   "<td height=\"40\" width=\"300\">".stripslashes($row["text"])."</td>",
				   "<td height=\"40\" width=\"200\"><img src=\"pollbar.gif\" height=\"10\" width=\"$percent\"> ".round($percent)."%   (".$row["result"].")</td>",
				   "</tr>";
	  }
		 
	  echo "</table></td>",
			   "</tr><tr>",
			   "<td align=\"center\"><font color=\"red\">$langTotal $total</td>",
			   "</tr></table></td></tr></table>";
		
		echo "<form method=\"post\" action=\"poll.php\"><p><input type=\"submit\" value=\"$langBack\"></p></form><br><br>";
	}
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function show_result($name,$qid,$response) 
{
	global $poll_cnf;
	global $langTotal,$langBack,$langAlreadyVoted;
	$query=mysql_query("SELECT * FROM `".$poll_cnf["numberTbl"]."` WHERE ((login='$name')AND(questionID='$qid'))");
   if (mysql_num_rows($query)>0) 
	{
		echo $langAlreadyVoted;
		echo "<br><br><form method=\"post\" action=\"poll.php\"><input type=\"submit\" value=\"$langBack\"></form>",
			   "<META  HTTP-EQUIV=\"Refresh\" Content= \"15; URL=poll.php\"><br><br>";
   }
   elseif (mysql_num_rows($query)==0) 
	{
	 	$query1=mysql_query("INSERT INTO `".$poll_cnf["numberTbl"]."` (questionID,login,answerID) VALUES ('$qid','$name','$response')");
   	$query1=mysql_query("UPDATE `".$poll_cnf["answerTbl"]."` SET result=result+1 WHERE answerID=$response");
   	//mysql_query ("commit");
	  show($qid);
 	}
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function get_results($qid)
{
   global $poll_cnf;

   $query=mysql_query("SELECT * FROM `".$poll_cnf["numberTbl"]."` WHERE questionID=$qid"); 
	while($myrow = mysql_fetch_array($query)) 
   {
      $user_id=$myrow["login"];
		$answer_id=$row["answerID"];
      $question_id=$row["questionID"];
      
   }
   return $r;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function show_users($qid)
{           
   global $poll_cnf,$tbl_user;
   
   $i=1;
   $query= "SELECT DISTINCT nr.login login, u.lastname lastName, u.firstname firstName 
	 		   FROM `".$poll_cnf["numberTbl"]."` nr, `".$tbl_user."` u
	         WHERE nr.login=u.user_id 
			   AND nr.questionID=$qid
			   ORDER BY lastName, firstName
            ";
   $result=mysql_query($query) or die(mysql_error());
   if (mysql_num_rows($result)>0) 
	{
	
      $data="<table><tr><td width=\"20\"></td>";
      $data.="<td width=\"175\"><b>Naam</b></td>";
      $data.="<td width=\"120\"><b>Voornaam</b></td>";
      $data.="<td width=\"300\"><b>Antwoord</b></td></tr>";

      while($myrow = mysql_fetch_array($result)) 
      {
	   
            $data .= "<tr><td>$i.</td><td>".$myrow["lastName"]."</td><td>".$myrow["firstName"]."</td>";
            $query1= "SELECT * FROM `".$poll_cnf["questionTbl"]."` q,`".$poll_cnf["answerTbl"]."` a,`".$poll_cnf["numberTbl"]."` n 
                     WHERE n.questionID=$qid
                     AND n.questionID=q.questionID
                     AND n.answerID=a.answerID
                     AND n.login=".$myrow["login"]."
                     ORDER BY text";  
            $result1=mysql_query($query1);
            while($rw = mysql_fetch_array($result1)) 
            {
               $data .= "<td>".$rw["text"]."</td></tr>";
            } 
            $i++;   
      }
   }
   else
   {
      echo "geen antwoorden";
   }
   $data.="</table>";

   echo $data;     

}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function show_table($qid) 
{
	global $poll_cnf;
	global $langTotal,$langBack;
	$query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID=$qid"); 
   while($myrow=mysql_fetch_array($query)) 
	{ 
   	$table="<table><tr>";
		$table.="<td align=\"center\" bgcolor=\"#666666\" height=\"20\"><font color=\"white\"><i>".stripslashes($myrow["question"])."</i></font></td>";
		$table.="</tr><tr>";
		$table.="<td><table border=\"1\">";
			 
	  $query1=mysql_query("SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID=".$myrow["questionID"]." ORDER BY ordre ASC");
	  $total = 0;
	  while($row=mysql_fetch_array($query1)) {$total=$total+$row["result"];}
	          	
		$query1=mysql_query("SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionID=".$myrow["questionID"]." ORDER BY ordre ASC");
	  while($row=mysql_fetch_array($query1)) 
		{
	   	if ($total=="0") {$percent=0;} else {$percent=$row["result"]/$total;}
	    $percent=$percent*100; 
			$table.="<tr>";
			$table.="<td height=\"20\">".stripslashes($row["text"])."</td>";
			$table.="<td height=\"20\">".round($percent)."%   (".$row["result"].")</td>";
			$table.="</tr>";
	  }
		 
	  $table.="</table></td>";
	  $table.="</tr><tr>";
	  $table.="<td align=\"center\"><font color=\"red\">$langTotal $total</td>";
	  $table.="</tr><tr></tr></table>";
   }
   return $table;
}

/**
* @author: Berit De Meester <Berit.DeMeester@UGent.be>, Ghent University
*/
function export($qid)
{           
   global $poll_cnf,$tbl_user;
   
   $data="<table><tr><td align=\"center\" bgcolor=\"#666666\" height=\"20\"><font color=\"white\"><i>Naam</i></td>";
   $data.="<td align=\"center\" bgcolor=\"#666666\" height=\"20\"><font color=\"white\"><i>Voornaam</i></td>";
   $data.="<td align=\"center\" bgcolor=\"#666666\" height=\"20\"><font color=\"white\"><i>Antwoord</i></td></tr>";
   
   $query= "SELECT DISTINCT nr.login login, u.lastname lastName, u.firstname firstName 
	 		   FROM `".$poll_cnf["numberTbl"]."` nr, `".$tbl_user."` u
	         WHERE nr.login=u.user_id 
			   AND nr.questionID=$qid
			   ORDER BY lastName, firstName
            "; 
   $result=mysql_query($query);
   while($myrow = mysql_fetch_array($result)) 
   {
      $data .= "<tr><td>".$myrow["lastName"]."</td><td>".$myrow["firstName"]."</td>";
      $query1= "SELECT * FROM `".$poll_cnf["questionTbl"]."` q,`".$poll_cnf["answerTbl"]."` a,`".$poll_cnf["numberTbl"]."` n 
                WHERE n.questionID=$qid
                AND n.questionID=q.questionID
                AND n.answerID=a.answerID
                AND n.login=".$myrow["login"]."
                ";  
      $result1=mysql_query($query1);
      while($myrow = mysql_fetch_array($result1)) 
      {
         $data .= "<td>".$myrow["text"]."</td></tr>"; 
      } 
   }   
   
   $data.="</table>";

   $today=date("Y-m-d_Hui",mktime());
      
   # This line will stream the file to the user rather than spray it across the screen
   header("Content-type: application/octet-stream");
      
   # replace excelfile.xls with whatever you want the filename to default to
   header("Content-Disposition: attachment; filename=".htmlspecialchars($today).".xls");
   header("Pragma: no-cache");
   header("Expires: 0");
      
   echo show_table($qid);
   echo "<br><br>";     
   echo $data;
}

?>
