<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Acadé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);

?>
<?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 é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 getitemsparniv($niveau) {     
  //On suppose la connexion ouverte
  static $tabitniv=array();
  if (empty($tabitniv)) {
	$sql="SELECT brevet_no, count(item_id) as nbitems FROM items GROUP BY brevet_no";
  	$res=query($sql);
  	while (list($bno, $nbo)=mysql_fetch_array($res)) {
		$tabitniv[$bno]=$nbo;
		}
  	}
  return($tabitniv[$niveau]);
}

function getitemsvalparniv($etab) {
global $sess_ascol;
static $nbval=array();
if (empty($nbval)) {
	//on suppose la connexion ouverte
	$sql="SELECT etab_id, brevet_no, count(resultats.item_id) as nbval ".
       " FROM eleves, resultats, items ".
       " WHERE eleves.annee='$sess_ascol' ".
       " and eleves.eleve_id=resultats.eleve_id ".
       " and items.item_id=resultats.item_id ".
       " GROUP BY etab_id,items.brevet_no";
  	$res=query($sql);
  	while (list ($et, $niv, $nbniv)=mysql_fetch_array($res)) {
        $nbval[$et][$niv]=$nbniv;
        }
    }
return ($nbval[$etab]);
}

//On fabrique le tableau
print "<p class='titre'>Avancement des items validés par établissement</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);
//début du tableau
$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°&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;Nb Elèves&nbsp;</td>";
print "<td>&nbsp;Graphiques<br>/item &nbsp;/classe</td>";
print "<td colspan=3>&nbsp;Items par élève<br> niv 1&nbsp;niv 2&nbsp;niv 3</td>";
//print "<td>&nbsp;It/élève niv 2&nbsp;</td>";
//print "<td>&nbsp;It/élève niv 3&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);
      unset($valides);
      $valides=@getitemsvalparniv($etab_id);
      unset($valid);
      $valid=array();
      if (!empty($valides))
         while (list($i,$nbi)=each($valides)) $valid[$i]=$nbi;

      //print_r($valid);
      for ($j=1; $j<=3; $j++) {
         $tmp="niv".$j;
         if (($nbeleves!=0) and isset($valid[$j]) and $valid[$j]) $$tmp=round($valid[$j]/$nbeleves,1);
             else $$tmp='&nbsp;--';         
         }

     //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";
     
     //Les graphiques !
     print "\t<td align='center' ><a href='../gestion/histo_items.php?etab_id=$etab_id' target='_blank'><img src='./images/graphit.gif' border='0' alt='Validations par item'></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='../gestion/histo_classes.php?etab_id=$etab_id' target='_blank'><img src='./images/graphcl.gif' border='0' alt='Validations par classe'></a> </td>\n";

     print "\t<td class='$cltdm'>$niv1</td>\n";
     print "\t<td class='$cltdr'>$niv2</td>\n";
     print "\t<td class='$cltdo'>$niv3</td>\n";
     print "\t</tr>\n";
}

 print "</table><br>\n";
 print $nbetabs." établissements";


?>
</body>
</html>
