<?php
include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_quiz_sql.php3");

$OPTIM_QUIZ_TEMPLATE = $BIB."bib_quiz_optim_template.php3";
//$OPTIM_QUIZ_TEMPLATE = $BIB."bib_test.php3";
$OPTIM_QUIZ_FILE = $ARCHIVES_DIR."bib_quiz_optim.php3";

function replaceTag($tag, $value, $msg)
{
	return ereg_replace($tag, $value, $msg);
}

/*
retourne la période active

finalcup --> not use
*/
function getActualPeriod()
{
	$date = date("Y-m-d H:i:s", my_local_time());
	
	# on détermine la période dans laquelle on se trouve
	$period = db_fetch_array(main_db_query("SELECT * FROM QPeriods WHERE ShowTime<'".$date."' AND EndTime>'".$date."'"));
	
	return $period['Id'];
}

/*
retourne les QCategories ouvertes
qui sont ouverts, sous forme de string
le paramètre est la date actuelle
*/
function getQActualCategory()
{
	$date = date("Y-m-d H:i:s", my_local_time());
	$rows = main_db_query("SELECT CategoryId FROM QCategories WHERE ShowTime<'".$date."' AND EndTime>'".$date."'");
	//echo "SELECT CategoryId FROM QCategories WHERE ShowTime<'".$date."' AND EndTime>'".$date."'";
	//return $row['CategoryId'];
	$s = "";
	while($row = db_fetch_array($rows))
	{
		$s .= $row['CategoryId'].",";
	}
	
	$s = substr($s, 0 , -1);
	
	return $s;
}

/*
retourne les QCategories (ie groupe de questions)
qui sont ouverts, sous forme de tableau
le paramètre est la période

finalcup --> not use
*/
function getQPeriodCategories($periodId)
{
	$rows = main_db_query("SELECT * FROM QCategories WHERE PeriodId='".$periodId."'");
	//echo "SELECT * FROM QCategories WHERE PeriodId='".$periodId."'";
	$s = "";
	while($row = db_fetch_array($rows))
	{
		$s .= '$tabPeriodCategories["'.$row["CategoryId"].'"]=array("CategoryId"=>"'.$row["CategoryId"].'", "Container" => "'.addslashes($row["Container"]).'", "Name" => "'.addslashes($row["Name"]).'", "ShowTime" => "'.$row["ShowTime"].'", "EndTime" => "'.$row["EndTime"].'");';
		$s .= "\n";
	}
	
	return $s;
}

/*
retourne les Id des QCategories (ie groupe de questions)
qui sont ouverts sous forme d'une string séparté par virgule
le paramètre est la période

finalcup --> not use
*/
function getQPeriodCategories_str($periodId)
{
	$rows = main_db_query("SELECT * FROM QCategories WHERE PeriodId='".$periodId."'");
	
	$s = "";
	while($row = db_fetch_array($rows))
	{
		$s .= $row['CategoryId'].",";
	}
	
	$s = substr($s, 0 , -1);
	
	return $s;
}

/*
retourne les QMotions (ie questions)
qui sont ouvertes sous forme de tableau
*/
function getQMotion($categoryId)
{
	$rows = main_db_query("SELECT * FROM QMotions WHERE Category IN (".$categoryId.")");
	
	$s = "";
	$r=0;
	while($row = db_fetch_array($rows))
	{
		$x = $r % 4;
		$s .= '$tab["'.$row["Category"].'"]["'.$x.'"] = array("MotionId" => "'.$row["MotionId"].'", "Category" => "'.$row["Category"].'", "Title" => "'.addslashes($row["Title"]).'", "Type" => "'.$row["Type"].'");';
		$s .= "\n";
		$r++;
	}
	
	return $s;
}

/*
retourne les Id des QMotions (ie questions)
qui sont ouvertes sous forme d'une string séparté par virgule
*/
function getQMotion_str($categoryId)
{
	$rows = main_db_query("SELECT * FROM QMotions WHERE Category IN (".$categoryId.")");
	
	$s = "";
	while($row = db_fetch_array($rows))
	{
		$s .= $row['MotionId'].",";
	}
	
	$s = substr($s, 0 , -1);
	return $s;
}


/*
retourne les réponses
*/
function get_optim_qanswers_tab($strMotions)
{
	$rows = main_db_query("SELECT * FROM QMotionTypesAnswers WHERE MotionId IN (".$strMotions.")");
	
	$s = "";
	while($row = db_fetch_array($rows))
	{
		$s .= '$optim_qanswers_tab["'.$row['MotionId'].'"]["'.$row['Id'].'"] = array("Id" => "'.$row['Id'].'", "Answer" => "'.addslashes($row['Answer']).'", "MotionId" => "'.$row['MotionId'].'", "Price" => "'.$row['Price'].'");';
		$s .= "\n";
	}
	
	return $s;
}

# on attend qqls micro-secondes pour être sur de l'heure
ob_start();

# on lit le template
$msg = "";
$tabIn = file($OPTIM_QUIZ_TEMPLATE);
for($k=0;$k<sizeof($tabIn);$k++)
    $msg = $msg.$tabIn[$k];

$actualCategoryId = getQActualCategory();

echo "actualCategoryId:".$actualCategoryId."<br>";
$msg = replaceTag("__GETQACTUALCATEGORY__", $actualCategoryId, $msg);

$actualPeriodId = getActualPeriod();
echo "actualPeriodId:".$actualPeriodId."<br>";
$msg = replaceTag("__GETQPERIODCATEGORIES__", getQPeriodCategories($actualPeriodId), $msg);
$msg = replaceTag("__GETQPERIODCATEGORIES_STR__", '"'.getQPeriodCategories_str($actualPeriodId).'"', $msg);

# ici optim pour avoir les motions de la catégorie précédents en même temps
/*
if ($actualCategoryId > 1)
	$actualCategoryId = ($actualCategoryId - 1).",".$actualCategoryId;
*/

$msg = replaceTag("__GETQMOTION__", getQMotion($actualCategoryId), $msg);
$msg = replaceTag("__OPTIM_QANSWERS_TAB__", get_optim_qanswers_tab(getQMotion_str($actualCategoryId)), $msg);

# ouverture du calendar.inc pour ecriture
$f_out = fopen($OPTIM_QUIZ_FILE,'w');
fwrite($f_out, $msg);
fclose($f_out);

echo "<font color=red><b>Terminé !</b></font>";
$xout = ob_get_contents();
ob_end_flush();

if ($DEBUG != 1)
{
	mail($ROOT_EMAIL, date("Y-m-d H:i:s", time())."_quiz", $SCRIPT_FILENAME."\n".$xout);
}

?>
