<?php
###########################################
# add_categories.php3                     #
# ajout de catégories                     #
###########################################

include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_constants.php3");
//include($BIB."bib_admin.php3");

################################
# SQL FUNCTIONS
################################
function getCategory($CategoryId)
{
	return db_fetch_array ( main_db_query ("SELECT * FROM QCategories WHERE CategoryId='".$CategoryId."'"));
}

function deleteCategory($CategoryId)
{
	main_db_query ("DELETE FROM QCategories WHERE CategoryId='".$CategoryId."'");
}

function insertCategory($Container, $Name, $EndTime, $ShowTime)
{
	main_db_query ("INSERT INTO QCategories (Container, Name, Endtime, ShowTime) VALUES ('".$Container."', '".$Name."', '".$EndTime."', '".$ShowTime."')");
}

function updateCategory($Container, $Name, $EndTime, $ShowTime, $CategoryId)
{
	main_db_query ("UPDATE QCategories SET Container='".$Container."', Name='".$Name."', EndTime='".$EndTime."', ShowTime='".$ShowTime."' WHERE CategoryId='".$CategoryId."'");
}

function selectAllCategories()
{
	return main_db_query ("SELECT * FROM QCategories ORDER BY ShowTime DESC");
}

function countCategoryQuestions($CategoryId)
{
	return db_fetch_array(main_db_query("SELECT COUNT(*) AS Questions FROM QMotions WHERE Category='".$CategoryId."'"));
}

################################

# constants
$ADD_CATEGORIES_PAGE = $SERVER_URL."AdmQuiz/add_categories.php3";
$GERER_QUESTIONS_PAGE = $SERVER_URL."AdmQuiz/gerer_questions.php3";
$CHECK_QUESTIONS_PAGE = $SERVER_URL."AdmQuiz/check_questions.php3";

?>

<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Gestion des Semaines (catégories)</div>
<br>
<?php
switch ($submit)
{
case "Infos":
	$categorie = getCategory($CategoryId);
	$CategoryId = $categorie[CategoryId];
	$Container = $categorie[Container];
	$Name = $categorie[Name];
	$EndTime = $categorie[EndTime];
	$ShowTime = $categorie[ShowTime];
	$Image = $categorie[Image];
	break;
case "Supprimer":
	deleteCategory($CategoryId);
	$CategoryId=0;
	break;
case "Valider":
	if ((!isset($Name)) || ($Name == ""))
		$Name = "Semaine ".$ShowTime;
	if ($CategoryId==0)
	{
		insertCategory($Container, $Name, $EndTime, $ShowTime);
	}
	else
	{
		updateCategory($Container, $Name, $EndTime, $ShowTime, $CategoryId);
	}
	
	break;
}
?>



<div class=universBold>Ajout de question</div>
	<form name="AddFile" method="post" action="<?php echo $CHECK_QUESTIONS_PAGE?>" ENCTYPE="multipart/form-data">
<!--	<input type=hidden name="Nouveau" value="1">-->
	Ajouter un fichier de questions ŕ la Semaine No : <input type="text" size=5 name="CategoryId" value="<?php echo $CategoryId ?>"><br>
	Nom du fichier : <input type="file" name="fileName" size="50"><br><br>
<div align=left><input type=submit name=submit value="Ajouter" ></div>	
	</form>
<hr>


<?php

echo "<div class=universBold>Liste des Semaines (catégories)</div>";
echo "<br><table border=1>
		<tr class=trTitle>
			<td>Semaine N°</td>
			<td>Container</td>
			<td>Nom</td>
			<td>ShowTime (Début)</td>
			<td>EndTime (Fin)</td>
			<td>Nb de Questions</td>
		</tr>";

$categories = selectAllCategories();
while ($categorie = db_fetch_array($categories))
{
	$compte = countCategoryQuestions($categorie[CategoryId]);
	if (time_diff_now($categorie[ShowTime]) > 0 and time_diff_now($categorie[EndTime]) < 0)
		echo "<tr bgcolor=yellow>";
	else
		echo "<tr>";

	echo "<td>".$categorie[CategoryId]."</td>";
	echo "<td>".$categorie[Container]."</td>";
	echo "<td>".$categorie[Name]."</td>";
//	echo "<td>".$JOURS[getDay($categorie[ShowTime])]."</td>";
	echo "<td>".$categorie[ShowTime]."</td>";
	echo "<td>".$categorie[EndTime]."</td>";
	echo "<td>&nbsp;&nbsp;".($compte['Questions'])."</td>";
	echo "</tr>";
}
echo "</table>";
?>
<hr>
<br>
<div class=universBold>Editer une Semaine</div>
<br>
<form>
<input type=hidden name=etape value=1>
	<table>
		<tr>
			<td>Numéro de la Semaine :</td>
			<td><input type=text size=3 name=CategoryId value=<?php echo $CategoryId?>>&nbsp;(0 pour en ajouter une)</td>
		</tr>
		<input type=hidden name="Container" value="<?php echo $Container?>">
		<input type=hidden name="Name" value="<?php echo $Name?>">
		<tr>
			<td>Container:</td>
			<td><input type=text size=3 name=Container value="<?php echo $Container?>"></td>
		</tr>
		<tr>
			<td>Nom :</td>
			<td>
				<input type=text size=40 name=Name value="<?php echo $Name?>">
			</td>
		</tr>
		<tr>
			<td>ShowTime (début) :</td>
			<td><input type=text size=18 name=ShowTime value="<?php echo $ShowTime?>"> AAAA-MM-JJ HH:MM:SS</td>
		</tr>
		<tr>
			<td>EndTime (fin) :</td>
			<td><input type=text size=18 name=EndTime value="<?php echo $EndTime?>"> AAAA-MM-JJ HH:MM:SS</td>
		</tr>
	</table><br><br>
	<input type="submit" name="submit" value="Infos">&nbsp;&nbsp;&nbsp;
	<input type="submit" name="submit" value="Valider">&nbsp;&nbsp;&nbsp;
	<input type="submit" name="submit" value="Supprimer">
</form>
<hr>
<br>
<div class=universBold>Voir les questions</div>
<br>
	<form method="post" action="<?php echo $GERER_QUESTIONS_PAGE?>">
<!--	<input type=hidden name="Nouveau" value="1">-->
	Voir les questions de la Semaine (catégorie) No <input type="text" size=5 name="CategoryId" value="<?php echo $CategoryId ?>">
	<input type=submit name=submit value="Voir" ><br>
	</form>

</body>
</html>