<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Acad&eacute;mie de Bordeaux)

This file is part of GiBii/Melia.

GiBii/Melia 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.

GiBii/Melia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GiBii/Melia; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

****************************************************************************/
include("../include/vars_adm.php");
include("../include/util.inc.php");
include("../include/get.inc.php");
include("../include/utiljs.php");
opensession("Sadmin",false);
set_time_limit(0);

$tot1=0;
$tot2=0;
$tot3=0;
$total1=0;
$total2=0;
$total3=0;
$total_eleves=0;

?>
<?php
    print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
       <title>Stats des &eacute;tabs</title>
<link rel="stylesheet" href="../include/general.css">
</head>
<body class="arial_sadmin" bgcolor="white" text="black" link="white" vlink="white" alink="blue">
<?php



function getnbb2i_edites($etab,$niveau) {   //calcule le nombre de b2i édités
//on suppose la connexion ouverte
$sql="SELECT count(imp_diplome) as nbb2i  
		FROM eleves 
		WHERE instr(imp_diplome,'$niveau')<>0 
		AND etab_id='$etab' and annee='".ascol()."' and tag<>-1";
$res=mysql_query($sql);
$tab=mysql_fetch_array($res);
$nbb2i=$tab['nbb2i'];
return($nbb2i);
}

function getnbb2i($etab) {   //calcule le nombre de b2i editables
//on suppose la connexion ouverte
$sql="SELECT niveau, count(bid) as nbb2i   FROM eleves,brevets 
	where eleves.eleve_id=brevets.eleve_id 
	and eleves.etab_id='$etab' and annee=".ascol()." and eleves.tag<>-1 GROUP BY niveau";
$res=query($sql);
$tabb2i=array(1=>0,2=>0,3=>0);

while (list($niv,$nb)=mysql_fetch_array($res)) {
 $tabb2i[$niv]=$nb;
}
return($tabb2i);
}

//On fabrique le tableau
print "<p class='titre'>Nombre de B2i &eacute;ditables (&eacute;dit&eacute;s) pour cette année scolaire</p>";
print "<p class='sous-titre'>Les attestations éditées peuvent dater d'une année antérieure</p>";
superflush();

switch(@$tri) {
   case "rne":
        $ordre="rne";
        break;
   case "nom":
        $ordre="nom";
        break;
   case "codepostal":
        $ordre="codepostal";
        break;
   case "ville":
        $ordre="ville";
        break;
   case "nbeleves":
        $ordre="nom";
        break;
   default :
        $ordre="etab_id";
}

if (@$order=="d") $ordre=$ordre." DESC";

$conn  = connecte($host,$user,$mdp,$db);
$sql="SELECT * FROM etablissements ORDER BY $ordre";
$resetab=query ($sql);
$nbetabs=0;

//alert($sql);
$PHP_SELF=basename($_SERVER['PHP_SELF']);
print "<table border='1' cellspacing='0' >\n";
print "<tr class='rubrique'>\n";
print "\t<td><a href=\"$PHP_SELF?tri=etab_id\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;N&deg;&nbsp;<a href=\"$PHP_SELF?tri=etab_id&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";

print "\t<td><a href=\"$PHP_SELF?tri=rne\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Rne&nbsp;<a href=\"$PHP_SELF?tri=rne&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=nom\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Nom&nbsp;<a href=\"$PHP_SELF?tri=nom&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=ville\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Ville&nbsp;<a href=\"$PHP_SELF?tri=ville&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=codepostal\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Code postal&nbsp;<a href=\"$PHP_SELF?tri=codepostal&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td>&nbsp;Nombre d'&eacute;l&egrave;ves&nbsp;</td>";
print "<td>&nbsp;Ecole&nbsp;</td>";
print "<td>&nbsp;Coll&egrave;ge&nbsp;</td>";
print "<td>&nbsp;Lyc&eacute;e CFA&nbsp;</td></tr>\n";

while ($rowetab=mysql_fetch_array($resetab)) {
      $etab_id=$rowetab["etab_id"];
      $nbetabs++;
      $rne=$rowetab["rne"];
      $nom=$rowetab["nom"];
      $adresse=$rowetab["adresse"];
      $codepost=$rowetab["codepostal"];
      $ville=$rowetab["ville"];
      $nbeleves=getnbeleves($etab_id);
	  $total_eleves=$total_eleves+$nbeleves;
      
      //Pour les editables
      $tabnb=getnbb2i($etab_id);
      $tot1+=$tabnb[1];
      $tot2+=$tabnb[2];
      $tot3+=$tabnb[3];
      //print_r($tabnb);
      
      //Pour les edites
      $nbb2i1=getnbb2i_edites($etab_id,1);
      $nbb2i2=getnbb2i_edites($etab_id,2);
      $nbb2i3=getnbb2i_edites($etab_id,3);
	  $total1+=$nbb2i1;
	  $total2+=$nbb2i2;
	  $total3+=$nbb2i3;
  
     //couleur de la ligne suivant sa parité
     $cl=($nbetabs%2==0) ? "_paire" : "_impaire";
	 $cltr='sadmin'.$cl;
	 $cltdm='stats_marron'.$cl;
	 $cltdr='stats_rose'.$cl;
	 $cltdo='stats_olive'.$cl;

     //Affichage d'une ligne
     print "\t<tr class='$cltr'>\n";
     print "\t<td >$etab_id</td>\n";
     print "\t<td >$rne</td>\n";
     print "\t<td >$nom</td>\n";
     print "\t<td >$ville</td>\n";
     print "\t<td >$codepost</td>\n";
     print "\t<td >$nbeleves</td>\n";
     if ($nbeleves<=1) print "<td class='$cltdm'>&nbsp;--</td><td class='$cltdr'>&nbsp;--</td><td class='$cltdo'>&nbsp;--</td>";
     else {
		print "\t<td class='$cltdm'>$tabnb[1]&nbsp;&nbsp;&nbsp; ($nbb2i1)</td>\n";
     	print "\t<td class='$cltdr'>$tabnb[2]&nbsp;&nbsp;&nbsp; ($nbb2i2)</td>\n";
     	print "\t<td class='$cltdo'>$tabnb[3]&nbsp;&nbsp;&nbsp; ($nbb2i3)</td>\n";
		}
	 
     
	 print "\t</tr>\n";
}
print "\t<tr><td  colspan='5' align=\"right\">Total :</td><td align=\"center\">$total_eleves</td><td align=\"center\">$tot1  ($total1)</td><td align=\"center\">$tot2  ($total2)</td><td align=\"center\">$tot3  ($total3)</td></tr>\n";
 print "</table><br>\n";
 print $nbetabs." &eacute;tablissements";


?>
</body>
</html>