<?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)

	Copyright (c) Jean "Example" Programmer (jean@somewhere.net)
	
	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'); 


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

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

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

</script>";

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

// breadcrumbs
$interbredcrump[]= array ("url"=>"poll.php", "name"=>"Polls");



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


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

/*
-----------------------------------------------------------
			Database Variables
-----------------------------------------------------------
*/ 
$poll_cnf["questionTbl"] = $_course["dbNameGlu"]."poll_questions"; 
$poll_cnf["answerTbl"] = $_course["dbNameGlu"]."poll_answers";
$poll_cnf["numberTbl"] = $_course["dbNameGlu"]."poll_number";


/*
============================================================================== 
		FUNCTIONS
============================================================================== 
*/ 

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;
} 	



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

	/*----------------------------------------
		 MOVE UP AND DOWN COMMANDS
	 --------------------------------------*/
	if ($down)
	{
		$thisAnswerId = $down;
		$sortDirection = "ASC";
	}

	if ($up)
	{
		$thisAnswerId = $up; 
		$sortDirection = "DESC";
	}

	if ($sortDirection)
	{
		if (!in_array(trim(strtoupper($sortDirection)), array('ASC', 'DESC'))) die("Bad sort direction used."); //sanity check of sortDirection var
		$query="SELECT answerId, ordre FROM `".$poll_cnf["answerTbl"]."` WHERE questionId='$qid' ORDER BY `ordre` $sortDirection";
		$result = mysql_query($query);

		while (list ($answerId, $answerOrder) = mysql_fetch_row($result))
		{

      // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER.
			//          COMMIT ORDER SWAP ON THE DB

			if (isset ($thisAnswerOrderFound)&&$thisAnswerOrderFound == true)
			{
				$nextAnswerId = $answerId;
				$nextAnswerOrder = $answerOrder;

				$q1="UPDATE `".$poll_cnf["answerTbl"]."` SET ordre = '$nextAnswerOrder' WHERE answerId =  '$thisAnswerId'";
            $res1 = mysql_query($q1);
				
				$q2="UPDATE `".$poll_cnf["answerTbl"]."` SET ordre = '$thisAnswerOrder' WHERE answerId =  '$nextAnswerId'";
				$res2 = mysql_query($q2);
				
				break;
			}
			// STEP 1 : FIND THE ORDER OF THE ANNOUNCEMENT

			if ($answerId == $thisAnswerId)
			{
				$thisAnswerOrder = $answerOrder;
				$thisAnswerOrderFound = true;
			}
			
			
		}
	}


echo "<br><br>";
api_display_tool_title($langChangePoll);	

if ($is_allowedToEdit) 
{
	if (!isset($_POST["start"])) 
	{
	
		/* add answers */
		if (isset($_POST["extra"])) 
		{
         $query="SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE questionId='$qid'";
         $result = mysql_query($query);
         $countAnswers=mysql_num_rows($result);
			$extra=0;
			$maxaid=maxAnswerId();
			for ($i==0;$i<$plus;$i++) 
			{
            $countAnswers++;
				$query=mysql_query("INSERT INTO `".$poll_cnf["answerTbl"]."` (answerID,questionID,result,text,ordre) VALUES ('$maxaid','$qid','0','','$countAnswers')"); 
				$maxaid=maxAnswerId();
			}
		}
		
		if (!isset($_GET["action"]))
      {
         echo "<table><tr>",
              "<td><a href=\"".$SERVER[PHP_SELF]."?action=add&qid=".$qid."\">$langClickAddAnswers</a></td>",
              "</tr></table>";
      }
      else
      {
         echo "<form method=\"post\" action=\"".$SERVER[PHP_SELF]."?qid=$qid\">",
	   	     "<table>",
	   		  "<tr>",
		  	     "<td bgcolor=\"#FFCC00\">$langEditQuestion <input type=\"text\" size=\"2\" name=\"plus\" value=\"0\"></td>",
		  	     "<td bgcolor=\"#FFCC00\"><input type=\"submit\" value=\"$langGo\" name=\"extra\"></td>",
		  	     "<td>&nbsp;</td><td>&nbsp;</td>",
		  	     "</tr>",
		  	     "</table>",
		  	     "</form>";
      }

      echo "<br>";	
		
		/* delete answers */
		if ($_GET["aid"]) 
		{
         $query=mysql_query("DELETE FROM `".$poll_cnf["answerTbl"]."` WHERE ((questionID='$qid')and(answerID='$aid'))");
			mysql_query ("commit");
		}
		
      /* form */	
         echo "<form method=\"post\" name=\"editpoll\" action=\"poll.php?qid=$qid\">",
              "<table>",
		  	     "<tr>",
		  	     "<td>$langNewQuestion</td>";
			 		
		$query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID=$qid");
		while($my = mysql_fetch_array($query)) 
		{
			echo "<td><input type=\"text\" name=\"question\" size=\"70\" value=\"".htmlspecialchars(stripslashes($my["question"]))."\"></td>",
				   "</tr>";
		}
		
		echo "<tr><td>&nbsp;</td></tr>",
			  "<tr><td>&nbsp;</td></tr>";
			  
		$query="SELECT * FROM `".$poll_cnf["answerTbl"]."` WHERE (questionID='$qid') ORDER BY ordre asc";
		$result = mysql_query($query);
		$i=1;$iterator=1;
		while($myrow = mysql_fetch_array($result)) 
		{
         $bottomAnswer=mysql_num_rows($result);
         echo "<tr>",
				  "<td>$langNewAnswer $i: </td>",
				  "<td><table><tr>",
				  "<td><input type=\"text\" name=\"answers[$i]\" value=\"".htmlspecialchars(stripslashes($myrow["text"]))."\">";
			
			if (mysql_num_rows($result)>2) 
         {
			   echo "</td><td><a href=\"".$SERVER[PHP_SELF]."?qid=".$myrow["questionID"]."&aid=".$myrow["answerID"]."\" onclick=\"checkdelete()\">",
			 	     "<img src=\"../../img/delete.gif\" alt=\"$langImgDelete\" border=\"0\"></a>";
			}
			
         // DISPLAY MOVE UP COMMAND only if it is not the top answer
			if($i != 1)
			{
			  echo	" <a href=\"".$SERVER[PHP_SELF]."?qid=".$myrow["questionID"]."&up=".$myrow["answerID"]."\">",
						"<img src=../../img/up.gif border=0 title=\"$langUp\" align=\"absmiddle\">",
						"</a>\n";
			}
			// DISPLAY MOVE DOWN COMMAND only if it is not the bottom answer
			if($i < $bottomAnswer)
			{
				echo	" <a href=\"".$SERVER[PHP_SELF]."?qid=".$myrow["questionID"]."&down=".$myrow["answerID"]."\">",
						"<img src=\"../../img/down.gif\" border=\"0\" title=\"$langDown\" align=\"absmiddle\">",
						"</a>\n";
			}
      
         echo "</td></tr></table></td>",
				  "</tr>";
			
			$i++;
		}
		
		echo "<tr><td>&nbsp;</td></tr>",
			  "<tr><td>&nbsp;</td></tr>";
			 
		$query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID=$qid");
		while($myrow = mysql_fetch_array($query)) 
		{
			list($datea,$timea)=explode(' ', $myrow["begin"]);
			list($ya,$ma,$da)=explode('-', $datea);
			list($houra,$mina,$seca)=explode(':', $timea);
						
			list($dateb,$timeb)=explode(' ', $myrow["end"]);
			list($yb,$mb,$db)=explode('-', $dateb);
			list($hourb,$minb,$secb)=explode(':', $timeb);
			if ($yb=='0000') $yb=date('Y');
			if ($mb=='00') $mb=date('m');
			if ($db=='00') $db=date('d');
			if ($hourb=='00') $hourb=date('H');
			if ($minb=='00') $minb=date('i');
			if ($secb=='00') $secb=date('s');
			
      		
			echo "<tr>",
				  "<td>$langNewStart</td>",
				  "<td><table width=\"100%\"><tr>",
				  "<td width=\"50%\"><input type=\"text\" name=\"startdate\" size=\"12\" value=\"".$da."-".$ma."-".$ya."\">",
			     " <img src=\"calendar/date.gif\" alt=\"".get_lang('DateRollover')."\" width=\"19\" height=\"17\" border=\"0\" onclick=\"popUpCalendar(this,editpoll.startdate, 'dd-mm-yyyy')\"  align=\"absmiddle\"></td>",
				  "<td>&nbsp;</td>",
				  "<td width=\"50%\"><input type=\"text\" name=\"houra\" size=\"2\" maxlength=\"2\" value=\"".$houra."\">:",
				  "<input type=\"text\" name=\"mina\" size=\"2\" maxlength=\"2\" value=\"".$mina."\">:",
				  "<input type=\"text\" name=\"seca\" size=\"2\" maxlength=\"2\" value=\"".$seca."\"> $langNewTime</td>",
				  "</tr></table></td>",
				  "</tr>";
			 
			echo "<tr>",
				  "<td>$langNewEnd</td>",
				  "<td><table width=\"100%\"><tr>",
				  "<td width=\"50%\"><input type=\"text\" name=\"enddate\" size=\"12\" value=\"".$db."-".$mb."-".$yb."\">",
			     " <img src=\"calendar/date.gif\" alt=\"".get_lang('DateRollover')."\" width=\"19\" height=\"17\" border=\"0\" onclick=\"popUpCalendar(this, editpoll.enddate, 'dd-mm-yyyy')\"  align=\"absmiddle\"></td>",
				  "<td>&nbsp;</td>",
				  "<td width=\"50%\"><input type=\"text\" name=\"hourb\" size=\"2\" maxlength=\"2\" value=\"".$hourb."\">:",
				  "<input type=\"text\" name=\"minb\" size=\"2\" maxlength=\"2\" value=\"".$minb."\">:",
				  "<input type=\"text\" name=\"secb\" size=\"2\" maxlength=\"2\" value=\"".$secb."\"> $langNewTime</td>",
				  "</tr></table></td></tr>";
				  
		}
		
		echo "<tr><td>&nbsp;</td></tr>",
			  "<tr><td>&nbsp;</td></tr>";
		
		$query=mysql_query("SELECT * FROM `".$poll_cnf["questionTbl"]."` WHERE questionID=$qid");
		while($myrow = mysql_fetch_array($query)) 
		{   
		   $text1= $myrow["answer"]=="1" ? "checked" : ""; 
         $text2= $myrow["answer"]=="0" ? "checked" : "";
         echo "<tr>",
              "<td>$langAnonymous1</td>",
              "<td><input type=\"radio\" name=\"save\" value=\"1\" $text1>NAI ",
              " <input type=\"radio\" name=\"save\" value=\"0\" $text2>OXI </td> ",
              "</tr>";
      }
    	
    	echo "<tr><td>&nbsp;</td></tr>",
			  "<tr><td>&nbsp;</td></tr>";
		    	
		echo "<tr>",
			  "<td valign=\"top\">",
           "<input type=\"submit\" value=\"$langChange\" name=\"start\"></form></td>",
			  "<td valign=\"top\"><form method=\"post\" action=\"poll.php\"><input type=\"submit\" value=\"$langCancel\" name=\"stop\"></td>",
			  "</tr>",
			  "</table>";
   }
}


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

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