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

function getage($req, &$tab2)
{
	# indice où se trouve l'année de naissance
	$indice = 0;
	$tab2 = array();
	$year = date("Y", my_local_time());
	
	$rows = main_db_query($req);
	$r=0;
	$tot=0;
	while($row = db_fetch_array($rows))
	{
		$t = explode("-", $row[Age]);
		
		# var_dump($t);
		# calcul de l'age moyen pour les personnes née entre 1900 et 2000
		# $tab2[0] = 0;
		if ($t[$indice] > 1900 and $t[$indice] < 2000)
		{
			$tot += $t[$indice];
			$r++;
			
			# calcul de la répartition
			$age = $year - $t[$indice];
			if ($age < 12) 
				$tab2[1]++;			
			elseif ($age < 18)
				$tab2[2]++;
			elseif ($age < 25)
				$tab2[3]++;
			else
				$tab2[4]++;
		}
		
		
		
	}

	return array(number_format($year - ($tot/$r), 2, ',', ' '), $r);
}

function getPourcent($critere, $trunc = 0)
{
	if ($trunc > 0)
		$rows = main_db_query("SELECT COUNT(*) AS total, LEFT($critere, $trunc) AS $critere FROM Members GROUP BY $critere");
	else
		$rows = main_db_query("SELECT COUNT(*) AS total, $critere FROM Members GROUP BY $critere");
//echo "SELECT COUNT(*), $critere AS total FROM Members GROUP BY $critere";
	$totalMises = 0;
	$nRows = 0;
	
	while($row = db_fetch_array($rows))
	{
		$totalMises += $row[total];
		$tab[$row[$critere]] = 100 * $row[total];
		$nRows++;
	}
		
	$which = $totalMises ;
	$tmpSum = 0;

	if ($nRows > 0)
	{
		while(list($mtaId, $res) = each($tab))
		{
			# pas de division par 0 !
			if ($which != 0)
			{
				# on arrive au dernier
				if ($n == $nRows)
				{
					$tab[$mtaId] = 100 - $tmpSum;
					break;
				}
				else
				{
					$tab[$mtaId] = number_format($res / $which, 2, '.', ' ');
					$tmpSum += $tab[$mtaId];
				}
			}
			$n++;
		}
	}	
/*	
	echo "<pre>";
	var_dump($tab);
	echo "</pre>";
*/
	return $tab;
}

function getPourcentFromTab($nElt, &$tab)
{
	while(list($key, $val) = each($tab))
		$t[$key] = number_format($val * 100 / $nElt, 2, ',', ' ');
	
	return $t;
}

function showPourcent($tab, $tabLib, $isOrder = true)
{
	echo "<table border=1><tr class=trTitle><td>critère</td><td align=right>%</td></tr>";
	reset($tab);

/*
	echo "<pre>";
	var_dump($tab);
	echo "</pre>";
*/

	for ( $r=0; $r < count($tab); $r++)
	{
		if ($isOrder)
		{
			$key = $r;
			$val = $tab[$r];
		}
		else
			list($key, $val) = each($tab);
		
		echo "<tr align=right><td>";
		if (count($tabLib) == 0)
			echo $key;
		else
			echo $tabLib[$key];
		echo "</td>";
		echo "<td>".$val."%</td></tr>";
	}
	echo "</table>";
}

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Joueurs</div>
<br><br>
<b>Inscriptions :</b><br><br>
<table border=1>
	<tr>
		<td>
Nombre d'inscrits opt-in:</td><td  align=right>
<?php
$row = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM Players LEFT JOIN Members ON Members.Id=Players.MemberId WHERE IsOff=0"));
echo $row['total'];
?>
</td></tr>

<tr><td>
J'accepte de recevoir la newsletter d'informations du KestuFoot :</td><td align=right>
<?php
$row = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM Members WHERE acceptNews = 1 AND IsOff=0"));
echo $row['total'];
?>
</td></tr>

<tr><td>
J'accepte de recevoir des informations des partenaires du KestuFoot  :</td><td align=right>
<?php
$row = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM Members WHERE acceptPartenaires = 1 AND IsOff=0"));
echo $row['total'];
?>

<tr><td>
Homme :</td><td align=right>
<?php
$row = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM Members WHERE Gender = 'M' AND IsOff=0"));
echo $row['total'];
?>
</td></tr>

<tr><td>
Femme :</td><td align=right>
<?php
$row = db_fetch_array(main_db_query("SELECT COUNT(*) AS total FROM Members WHERE Gender = 'F' OR Gender = 'L' AND IsOff=0"));
echo $row['total'];
?>
</td></tr>

</table><br>
<br><hr><br>
<b>Répartition en age :</b><br><br>
<?php

list($moy, $n) = getage("SELECT Birthdate AS Age FROM Members WHERE IsOff=0", $tabAge);
$t = array(
"1" => "<12",
"2" => "12-18",
"3" => "18-25",
"4" => ">25"
);
showPourcent(getPourcentFromTab($n, $tabAge), $t, false);

?>