Location: PHPKode > projects > EmploiNet : a timetable web manager > EmploiNet/v0.8.0/edt/Planification/planifie_jour.php
<?
include("../../BarreDeMenu.php3");
include("../../date.php3");
include("../../Session/security.php");

// Entete
if (!isset($display)) $display=1;
buildHeader("Planification d&eacute;taill&eacute;e par jour",$display);

// Verification de la session
$autorisation_list[0]=0; // Administrateur
$autorisation_list[1]=3; // Gestionnaire
$user_type = validateSession($id_session);
if ($user_type == -1 || !security($id_session,$autorisation_list)) 
{
  print("Accès refusé");
  print("<DIV ALIGN=RIGHT><A HREF=\"javascript:history.go(-1);\">Retour</A></DIV>\n");
}
else
{
  // Barre de menu
  buildMenuBar($id_session,$user_type,"planifie_jour.php",false);
  // Ouverture de la connexion avec la base de donnees
  $connection = openConnection("emploi_du_temps");

  // Verifie si le nombre de promotions est egal a 1

  if ($user_type==3 && !isset($id_promotion[0])) 
    {
      $user_id = getUserID($id_session);
      $connection = openConnection("emploi_du_temps");
      $result = mysql_query("SELECT distinct(promotion.id_promotion),diplome.libelle as L1,promotion.libelle as L2 FROM promotion,diplome,gerer WHERE gerer.id_gestionnaire=$user_id AND promotion.id_promotion=gerer.id_promotion AND diplome.id_diplome=promotion.id_diplome ORDER BY diplome.libelle");
      $index_max = mysql_num_rows($result); 
      if ($index_max == 1)
	{
	  $a_record = mysql_fetch_array($result);
	  $id_promotion[0] = $a_record["id_promotion"];
	  $id_groupe = -1;
	}
    }

  // Construction d'un creneau horaire au format xxhmm - xxhmm

  function makeHeure($i)
    {
      if (floor($i) == $i) return $i."h - ".$i."h30";
      else return floor($i)."h30 - ".($i+0.5)."h";
    }
		
  // Conversion du format hh:mm en hh.mm		
		
  function convertHeure($h)
    {
      $h1 = explode(":",$h);
      $x = 0.0 + $h1[0];
      $y = 0.0 + $h1[1];
      $x += ($y==30)?0.5:0; 
      return $x;
    }
		
  // Formate l'heure au format hh:mm a partir du format hh.mm

  function formateHeure($h)
    {
      return date("H:i:s",mktime(floor($h),($h-floor($h)>0)?30:0,0,1,1,2000));
    }

  /*******************************************************************************/	
  /********************** Gestion des creneaux selectionnes **********************/
  /*******************************************************************************/	

  // Chaine de selection des creneaux
  $s_select;
  // Chaine de selection des matieres
  $s_select2;
  // Nombre de semaines visibles
  $nb_weeks=7; 
	
  function clearCreneauHoraire()
    {	
      global $s_select;
      global $nb_weeks;
		
      $s_select = "";
      for ($i=8;$i<20;$i+=0.5)
	{
	  for ($j=1;$j<=$nb_weeks;$j++)
	    {
	      $s_select .= "-";
	    }
	}	
    }
	
  function clearMatiere()
    {
      global $s_select2;
		
      $s_select2 = "----------------------------------------------------------";
    }


  $request = "SELECT numero_semaine_debut,nb_semaines FROM periode WHERE id_periode = $id_periode";
  $result = mysql_query($request);
  if ($result)
    {
      $a_record = mysql_fetch_array($result);
      $max2 = $a_record["numero_semaine_debut"] + $a_record["nb_semaines"];
      $premiere_semaine = $a_record["numero_semaine_debut"];
      $nombre_semaines = $a_record["nb_semaines"];
    }

  if (!isset($s_select) || $s_select == "") // aucun creneau n'est selectionne
    clearCreneauHoraire();

  // Traitement des evenements Promotion et Groupe

  $G_id_matiere;
  $G_nom;
  $G_nb_matieres;
	
  $n = count($id_promotion);

  switch ($choice)
    {
    case -1: // changement de promotion
      {
	unset($s_semaine);
	unset($id_periode);
	$id_groupe = -1;
	break;
      }
    case -2: // changement de groupe
      {
	unset($s_semaine);
	if ($id_groupe != -1)
	  {
	    $result2 = mysql_query("SELECT * FROM associer WHERE id_groupe=$id_groupe");
	    $n = mysql_num_rows($result2); 
	    for ($i=0;$i<$n;$i++)
	      {
		$une_promotion = mysql_fetch_array($result2);
		$id_promotion[$i] = $une_promotion["id_promotion"];
	      }
	  }	 
	break; 
      }
    case -3: // changement de periode
      {
	unset($s_semaine);
	break;
      }
    case -4: // changement de jour
      {
	clearMatiere();
	clearCreneauHoraire();
	break;
      }
    case -5: // Decalage de la fenetre
      {
	clearMatiere();
	clearCreneauHoraire();
	$shift_week-=$nb_weeks-1;
	if ($shift_week<0) $shift_week=0;
	break;
      }
    case -6: // Decalage de la fenetre
      {
	clearMatiere();
	clearCreneauHoraire();
	$shift_week+=$nb_weeks-1;
	break;
      }
    }

  // Chargement de la liste des matieres suivies par la promotion ou le groupe

  // Si une promotion ou un groupe est selectionne
  if ($n > 0)
    {	
      // Si un groupe est selectionne
      if ($id_groupe != -1) $result = mysql_query("SELECT matiere.id_matiere,matiere.nom FROM matiere,suivipargroupe WHERE suivipargroupe.id_groupe = $id_groupe AND suivipargroupe.id_matiere = matiere.id_matiere"); 		
      // Si une promotion est selectionnee
      else $result = mysql_query("SELECT matiere.id_matiere,matiere.nom FROM matiere,suiviparpromotion WHERE suiviparpromotion.id_promotion = $id_promotion[0] AND suiviparpromotion.id_matiere = matiere.id_matiere"); 

      // Chargement des matieres dans les variables globales G_id_matiere et G_nom
      $G_nb_matieres = mysql_num_rows($result); 
      for ($i = 0;$i < $G_nb_matieres;$i++)
	{
	  $une_matiere = mysql_fetch_array($result);
	  $G_id_matiere[$i] = $une_matiere["id_matiere"];
	  $G_nom[$i] = $une_matiere["nom"];
	}
    }

  if ($n > 0 && $G_nb_matieres > 0 && isset($s_semaine) && isset($id_periode))
    {

      /*******************************************************************************/	
      /************************ Traitement des modifications *************************/
      /*******************************************************************************/	

      switch ($choice)
	{
	case 1: // nouvel enseignement programme
	  {
	    // calcul de l'heure du debut du cours et de sa duree
	    $found = false;
	    $stop = false;
	    $j = 0;
	    while (!$stop && $j <  $nb_weeks) // pour toutes les semaines
	      {
		$i = 0;
		while (!$stop && $i < 24) // pour tous les creneaux
		  {
		    if (substr($s_select,$i*$nb_weeks+$j, 1) == 'x')
		      {
			if (!$found)
			  {
			    $found = true;
			    $deb_h = 8 + $i/2;
			    $s_jour_semaine = $s_semaine; 
			    $s_premiere_semaine=$j;
			    $list_weeks[0] = $j;
			  }
		      }
		    else
		      {
			if ($found)
			  {
			    $stop = true;
			    $s_nb_heures = $i/2 - $deb_h + 8;
			  }
		      }
		    if (!$stop) $i++;
		  }
		if (!$stop) $j++;
	      }
				
	    // Determination du nombre de jours a programmer sur le meme creneau 
	    //  que la premiere semaine
	    if ($j < $nb_weeks)
	      {
		$nb_days = 1;
		$stop=false;
		while ($j < $nb_weeks)
		  {
		    $j++;
		    $i = $deb_h;
		    while (!$stop && $i < $deb_h+$s_nb_heures)
		      {
			$stop = (substr($s_select,($i-8)*2*$nb_weeks+$j,1) == '-');
			$i+=0.5;
		      }
		    // Les creneaux existent la semaine suivante
		    if (!$stop) 
		      {
			$list_weeks[$nb_days] = $j;
			$nb_days++;
		      }
		    else $stop=false;
		  }
	      }

	    // Introduction du decalage
	    $semaine_reelle = $premiere_semaine + $shift_week;

	    // si l'heure de debut et la duree sont correctement definis
	    if (isset($deb_h) && isset($s_nb_heures))
	      {	
		for ($m = 0;$m < $nb_days;$m++)
		  {
		    $k1 = $semaine_reelle + $list_weeks[$m];
		    if ($k1 > 52) $k= $k1 - 52; 
		    else $k = $k1;

		    // mise en forme de l'heure de debut et de fin
		    $heure_debut = formateHeure($deb_h);
		    $heure_fin = formateHeure($deb_h+$s_nb_heures);

		    // preparation d'une partie des requetes suivantes
		    $request2 = "(((TIME_TO_SEC('$heure_fin') - TIME_TO_SEC(heure_debut) > 0) AND (TIME_TO_SEC(heure_fin) - TIME_TO_SEC('$heure_fin') > 0)) OR ((TIME_TO_SEC(heure_fin) - TIME_TO_SEC('$heure_debut') > 0) AND (TIME_TO_SEC('$heure_debut') - TIME_TO_SEC(heure_debut) > 0)) OR (TIME_TO_SEC('$heure_debut') - TIME_TO_SEC(heure_debut) >= 0 AND TIME_TO_SEC(heure_fin) - TIME_TO_SEC('$heure_fin') >= 0))";
					
		    // la salle est-elle libre ?
		    $request = "SELECT COUNT(*) FROM planifie WHERE id_salle=$s_id_salle AND semaine=".($k)." AND jour_semaine=$s_jour_semaine";
		    $request .= " AND ".$request2;
					
		    $result = mysql_query($request);
		    $a_record = mysql_fetch_array($result);  
		    if ($s_id_salle==-1 || $a_record["COUNT(*)"] == 0) // la salle est libre
		      {
			// le prof est-il libre ?
			$request = "SELECT COUNT(*) FROM planifie WHERE id_professeur=$s_id_professeur AND semaine=".($k)." AND jour_semaine=$s_jour_semaine";
			$request .= " AND ".$request2;
			$result = mysql_query($request);
			$a_record = mysql_fetch_array($result);  
			if ($s_id_professeur==-1 || $a_record["COUNT(*)"] == 0) // le prof est libre
			  {
			    // determination de la cle de la table planifie
			    $result = mysql_query("select * from id");
			    $id = mysql_fetch_array($result);  
			    mysql_query("update id set id_planifie=".($id["id_planifie"]+1));
							
			    // mise a jour de la table planfie
			    $request = "INSERT INTO planifie VALUES (".($id["id_planifie"]+1).",$s_id_matiere,$s_id_type_seance,$s_id_professeur,$s_id_salle,".($k).",$s_semaine,'$heure_debut','$heure_fin')";
			    mysql_query($request);
					
			    // mise a jour de la table planifiegroupe ou planifiepromotion
			    // selon si on programme un cours a un groupe ou a une promotion
			    if ($id_groupe!=-1)
			      mysql_query("INSERT INTO planifiegroupe VALUES (".$id_groupe.",".($id["id_planifie"]+1).")");
			    else 
			      mysql_query("INSERT INTO planifiepromotion VALUES (".$id_promotion[0].",".($id["id_planifie"]+1).")"); 
			  }
			else
			  {
			    print("<script language=JavaScript>\n");
			    print("window.alert(\"L'enseignant n'est pas libre ,un creneau n'a pas été programmé.\");\n");
			    print("</script>\n");
			  }
		      }
		    else
		      {
			print("<script language=JavaScript>\n");
			print("window.alert(\"La salle n'est pas libre ,un creneau n'a pas été programmé.\");\n");
			print("</script>\n");
		      }
		  }
		// annule la selection des creneaux horaires
		clearCreneauHoraire();
		clearMatiere();
		$s_id_matiere = -1;
		$s_id_professeur = -1;
		$s_id_salle = -1;
	      }
	    break;
	  }
	case 2: // suppression d'un enseignement programme
	  {
	    $tableau=explode(";",$s_id_planifie);
	    $nbtab=count($tableau);
	    for ($i=1;$i<$nbtab;$i++)
	      {
		mysql_query("DELETE FROM planifie WHERE id_planifie = $tableau[$i]");
		if ($id_groupe != -1)
		  mysql_query("DELETE FROM planifiegroupe WHERE id_planifie = $tableau[$i]");
		else
		  mysql_query("DELETE FROM planifiepromotion WHERE id_planifie = $tableau[$i]");
	      }
	    clearCreneauHoraire();
	    clearMatiere();
	    $s_id_matiere = -1;
	    $s_id_professeur = -1;
	    $s_id_salle = -1;
	    break;
	  }
	case 3: // modification d'un enseignement programme
	  {
	    $tableau=explode(";",$s_id_planifie);
	    $nbtab=count($tableau);
	    for ($j=1;$j<$nbtab;$j++)
	      {
		// Selection de la plage de l'enseignement a modifier
		$result = mysql_query("SELECT id_matiere,id_type_seance,id_professeur,id_salle,semaine,jour_semaine,heure_debut,heure_fin FROM planifie WHERE id_planifie = $tableau[$j]");
		$a_record = mysql_fetch_array($result);
		$x = convertHeure($a_record["heure_debut"])-8;
		$y = convertHeure($a_record["heure_fin"])-8;
		$z = ($y - $x) * 2;
		for ($i = 0;$i < $z;$i++)
		  {
		    $k = $a_record["semaine"] - $shift_week - $premiere_semaine;
		    $s_select = substr($s_select,0,$k+$nb_weeks*($x*2+$i))."x".substr($s_select,$k+$nb_weeks*($x*2+$i)+1); 
		  }
		$s_id_matiere = $a_record["id_matiere"];
		$s_id_type_seance = $a_record["id_type_seance"];
		$s_id_professeur = $a_record["id_professeur"];
		$s_id_salle = $a_record["id_salle"];
		mysql_query("DELETE FROM planifie WHERE id_planifie = $tableau[$j]");
		if ($id_groupe != -1)
		  mysql_query("DELETE FROM planifiegroupe WHERE id_planifie = $tableau[$j]");
		else 
		  mysql_query("DELETE FROM planifiepromotion WHERE id_planifie = $tableau[$j]"); 
	      }
	    clearMatiere();
	    break;
	  }
	}
    }
	
  /*******************************************************************************/	
  /****************************** Formulaire *************************************/
  /*******************************************************************************/	

  print("<form name=\"form1\" action=\"planifie_jour.php\" method=post>\n");
  print("      <input type=\"hidden\" name=\"login\" value=\"$login\">\n");
  print("      <input type=\"hidden\" name=\"choice\" value=\"-1\">\n");
  print("      <input type=\"hidden\" name=\"id_session\" value=\"$id_session\">\n");
  print("      <input type=\"hidden\" name=\"user_type\" value=\"$user_type\">\n");
  print("      <input type=\"hidden\" name=\"user_id\" value=\"$user_id\">\n");
  print("      <input type=\"hidden\" name=\"display\" value=\"$display\">\n");
	
  print("      <input type=\"hidden\" name=\"s_x\">\n");
  print("      <input type=\"hidden\" name=\"s_y\">\n");

  if ($choice!=-1)
    {
      print("      <input type=\"hidden\" name=\"s_select2\" value=\"$s_select2\">\n"); // selection de la matiere
      print("      <input type=\"hidden\" name=\"s_id_planifie\">\n");
      if ($choice!=3) 
	{
	  print("      <input type=\"hidden\" name=\"s_id_matiere\">\n");
	  print("      <input type=\"hidden\" name=\"s_id_type_seance\">\n");
	}
      else 
	{
	  print("      <input type=\"hidden\" name=\"s_id_matiere\" value=$s_id_matiere>\n");
	  print("      <input type=\"hidden\" name=\"s_id_type_seance\" value=$s_id_type_seance>\n");
	}
    }
  else
    {
      print("      <input type=\"hidden\" name=\"s_select2\" value=\"$s_select2\">\n"); // selection de la matiere
      print("      <input type=\"hidden\" name=\"s_id_planifie\" value=$s_id_planifie>\n");
      print("      <input type=\"hidden\" name=\"s_id_matiere\" value=$s_id_matiere>\n");
      print("      <input type=\"hidden\" name=\"s_id_type_seance\" value=$s_id_type_seance>\n");
    }
  print("      <input type=\"hidden\" name=\"s_select\" value=\"$s_select\">\n");   // chaine de selection des creneaux

  // Variables pour le scrolling
  if (!isset($shift_week)) $shift_week=0;
  print("      <input type=\"hidden\" name=\"shift_week\" value=\"$shift_week\">\n");
	
  print("<table align=center border=0 cellspacing=0 cellpading=0 width=700 bgcolor=\"#C0C000\">\n");
  print("  <tr>\n");

  // Liste des promotions	
  print("          <td valign=top width=100 nowrap rowspan=2><font size=3><i>Promotion :</i></font></td>\n");
  print("          <td width=250 rowspan=2 valign=top>\n");
  include("../../Liste/liste_promotion.php");
  print("          </td>\n");

  // Liste des groupes associes a la promotion selectionnee
  print("          <td valign=top width=100 nowrap><font size=3><i>Groupe :</i></font></td>\n");
  print("          <td valign=top width=250>\n");
  include("../../Liste/liste_groupe.php");
  print("          </td>\n");
  print("  </tr>\n");

  // Periode
  print("  <tr>\n");
  print("          <td valign=top width=100 nowrap><font size=3><i>P&eacute;riode :</i></font></td>\n");
  print("          <td valign=top width=250>\n");
  include("../../Liste/liste_periode.php");
  print("          </td>\n");
  print("  </tr>\n"); 

  // Semaine	
  print("  <tr>\n");
  print("          <td colspan=4 align=center>\n");
  include("../../Liste/liste_jour_semaine.php");
  print("          </td>\n");
  print("  </tr>\n"); 
	
  print("</table><br>\n");

  if ($n > 0 && $G_nb_matieres > 0 && isset($s_semaine) && isset($id_periode))
    {
	
      /*******************************************************************************/	
      /************************** Fonctions JavaScript *******************************/
      /******/	
      /******/	
		
      print("<script language=\"JavaScript\">\n");
	
      print("function selectCreneau(x,y,z,id,s)\n");
      print("{\n");
      print("  str = document.form1.s_select.value;\n");
      print("  if (str.substr(x+z*y,1) == '-')\n");
      print("  {\n");
      print("    str = str.substr(0,x+z*y)+'x'+str.substr(x+z*y+1);\n");
      print("    document.form1.s_select.value = str;\n");
      print("    if (id>0) document.form1.s_id_planifie.value += \";\"+id;\n");
      print("    s.bgColor=\"yellow\";\n");
      print("  }\n");
      print("  else\n");
      print("  {\n");
      print("    str = str.substr(0,x+z*y)+'-'+str.substr(x+z*y+1);\n");
      print("    document.form1.s_select.value = str;\n");
      print("    if (id>0) document.form1.s_id_planifie.value = document.form1.s_id_planifie.value.replace(\";\"+id,\"\");\n");
      print("    s.bgColor=\"white\";\n");
      print("  }\n");
      print("}\n\n");

	
      print("function selectMatiere(index,id1,id2,id3,s)\n");
      print("{\n");
      print("  str = document.form1.s_select2.value;\n");
      print("  if (str.length<=index)\n");
      print("  {\n");
      print("    l = str.length;\n");
      print("    for (i=l+1;i<=index+1;i++)\n");
      print("      str+='-';\n");
      print("  }\n");
      print("  if (str.substr(index,1) == '-')\n");
      print("  {\n");
      print("    str = str.substr(0,index)+\"x\"+str.substr(index+1);\n");
      print("    document.form1.s_select2.value = str;\n");
      print("    document.form1.s_id_matiere.value = id1;\n");
      print("    document.form1.s_id_type_seance.value = id2;\n");
      print("    document.form1.s_id_professeur.value = id3;\n");
      print("    s.bgColor=\"yellow\";\n");
      print("  }\n");
      print("  else\n");
      print("  {\n");
      print("    str = str.substr(0,index)+\"-\"+str.substr(index+1);\n");
      print("    document.form1.s_select2.value = str;\n");
      print("    document.form1.s_id_professeur.value = -1;\n");
      print("    s.bgColor=\"white\";\n");
      print("  }\n"); 
      print("}\n\n");

      print("</script>\n\n");		

      // Masquage de l'entete
      if ($display==1)      
	{
	  print("<SCRIPT LANGUAGE=\"JavaScript\">\n");            
	  print("  function masquer_entete()\n");
	  print("  {\n");
	  print("    document.form1.display.value=0;\n");
	  print("    document.form1.choice.value=0;\n");
	  print("    document.form1.submit();\n");
	  print("  }\n");
	  print("</SCRIPT>\n\n");
    
	  print("<DIV ALIGN=RIGHT><FONT SIZE=2><I><A HREF=\"javascript:masquer_entete();\">Masquer ^</A></I></FONT></DIV><BR>\n");
	}
		
      /*******************************************************************************/	
      /************************** Trace du planning **********************************/
      /******/	
      /******/	

      // Tableau des jours de la semaine		
      $jour_semaine = array(1=>"Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");

      // Calcul de la date		
      $sem_actu = weekToDay($premiere_semaine,$connection)+($s_semaine-1);

      // Entete du planning : numero de la semaine + jours de la semaine
      print("  <table align=center border=1 cellspacing=0 cellpading=0 bgcolor=\"#C0C000\">\n");
      print("    <tr valign=BOTTOM>\n");
      print("      <td valign=center align=center nowrap><font size=2><i>Jour </i></font></td>\n");
      print("      <td><input type=\"image\" border=0 src=\"../../img/Previous.gif\" alt=\"Pr&eacute;c&eacute;dent\" OnClick=\"document.form1.choice.value=-5; submit();\"></td>\n");
      for ($i = $shift_week;$i < $shift_week+$nb_weeks;$i++)
	{
	  $trans = mysql_query("SELECT FROM_DAYS(".($sem_actu+7*$i).")");
	  $result_trans = mysql_fetch_row($trans);
	  $trans2 = mysql_query("SELECT DATE_FORMAT(\"$result_trans[0]\",'%d/%m/%Y')");
	  $result_trans2 = mysql_fetch_row($trans2);
	  print("      <td align=center><font size=2><i>"." ".$result_trans2[0]."</i></font></td>\n");
	}
      print("          <td><input type=\"image\" border=0 src=\"../../img/Next.gif\" alt=\"Suivant\" OnClick=\"document.form1.choice.value=-6; submit();\"></td>\n");

      // Colonne de droite avec les enseignements a programmer		
		
      $enseignement;
		
      print("      <td valign=top align=center rowspan=25 bgcolor=#C0C0C0>\n");
      $k = 0;

      print("<TABLE WIDTH=80% BORDER=0 CELLSPACING=1 BGCOLOR=\"#C0C000\">\n");
				
      // Pour chaque matiere associer a la promotion ou au groupe, recherche des heures prevues
      for ($i=0;$i<$G_nb_matieres;$i++)
	{
	  $request = "SELECT type,nb_heures,typeseance.id_type_seance FROM typeseance,reparti WHERE id_matiere=".$G_id_matiere[$i]." AND typeseance.id_type_seance=reparti.id_type_seance AND reparti.id_groupe=$id_groupe ORDER BY reparti.id_type_seance";
	  $result = mysql_query($request);
	  $max[$i] = mysql_num_rows($result);
	  $coursaprogrammer = $max[$i];
	  $a_record = mysql_fetch_array($result);
	  $coursaprogrammer--;
	  while ($coursaprogrammer > 0)
	    {
	      $nb_heures = $a_record["nb_heures"];
	      $enseignement[$k]["id_matiere"] = $G_id_matiere[$i];
	      $enseignement[$k]["id_type_seance"] = $a_record["id_type_seance"];
	      $enseignement[$k]["nb_heures"] = $nb_heures;
	      $a_record = mysql_fetch_array($result);
	      $coursaprogrammer--;
	      $type=$a_record["type"];
	      while ($a_record["id_type_seance"]==$enseignement[$k]["id_type_seance"])
		{
		  $nb_heures += $a_record["nb_heures"];
		  $enseignement[$k]["nb_heures"] = $nb_heures;
		  $a_record = mysql_fetch_array($result);
		  $coursaprogrammer--;
		}
			
	      // Combien d'heures sont deja programmees ?
	      if ($id_groupe!=-1)
		{
		  $request = "SELECT heure_debut,heure_fin 
								FROM planifie,planifiegroupe 
								WHERE planifie.id_matiere=".$G_id_matiere[$i]." 
									AND planifie.id_type_seance=".$enseignement[$k]["id_type_seance"]." 
									AND planifiegroupe.id_groupe=$id_groupe
									AND planifiegroupe.id_planifie=planifie.id_planifie"; 
		}
	      else $request = "SELECT heure_debut,heure_fin FROM planifie WHERE id_matiere=".$G_id_matiere[$i]." AND id_type_seance=".$enseignement[$k]["id_type_seance"];
	      $result3 = mysql_query($request);
	      if ($result3!=0 && mysql_num_rows($result3)>0)
		{
		  for ($l=0;$l<mysql_num_rows($result3);$l++)
		    {
		      $a_record3 = mysql_fetch_array($result3);
		      $x1 = convertHeure($a_record3["heure_fin"]);
		      $x2 = convertHeure($a_record3["heure_debut"]);
		      $nb_heures -= ($x1 - $x2); // Nombre d'heures restants a programmer
		    }
		}

	      if ($nb_heures != 0)
		{
		  // Qui assure ce type de seance pour la matiere courante ?
		  $request = "SELECT professeur.id_professeur AS T1,initiales FROM assurer,professeur WHERE id_matiere=".$G_id_matiere[$i]." AND id_type_seance=".$enseignement[$k]["id_type_seance"]." AND assurer.id_professeur=professeur.id_professeur";
		  $result2 = mysql_query($request);
				
		  // S'il existe un seul prof
		  if ($result2!=0 && mysql_num_rows($result2)==1)
		    {
		      $a_record2 = mysql_fetch_array($result2);
		      $enseignement[$k]["id_professeur"] = $a_record2["T1"];
		      $text = strtr($G_nom[$i],"%20","&nbsp;")." ".$type." ".(floor($nb_heures)-$nb_heures==0?floor($nb_heures):$nb_heures)."&nbsp;h&nbsp;-&nbsp;".$a_record2["initiales"];				
		    }
		  else
		    {
		      $enseignement[$k]["id_professeur"] = -1;
		      $text = strtr($G_nom[$i],"%20","&nbsp;")." ".$type." ".(floor($nb_heures)-$nb_heures==0?floor($nb_heures):$nb_heures)."&nbsp;h";
		    }
					
		  print("<TR><TD VALIGN=MIDDLE ALIGN=CENTER onMouseUp=\"selectMatiere($k,".($enseignement[$k]["id_matiere"]).",".($enseignement[$k]["id_type_seance"]).",".($enseignement[$k]["id_professeur"]).",this);\" ");
		  // Si la matiere est selectionee alors fond jaune
		  if (substr($s_select2,$k,1) == "x" || (isset($s_id_matiere) && $enseignement[$k]["id_matiere"] == $s_id_matiere)) 
		    print("bgColor=\"yellow\"><font size=-1><b><i>$text</i></b></font>\n");
		  else
		    print("bgColor=\"white\"><font size=-1><b><i>$text</i></b></font>\n");
		  print("</TD></TR>\n");
		  $k++;
		}
	    }
	}
      print("</TABLE>\n");

      // Liste des professeurs

      print("<font size=2><i><div align=left>Professeur :</i></font><BR>");
      print("<select tabindex=-1 name=\"s_id_professeur\" size=1 width=200>\n");
      print("<option value=-1>\n");
      $request = "SELECT id_professeur,nom,prenom FROM professeur ORDER BY nom";
      $result = mysql_query($request);
      $G_nb_professeurs = mysql_num_rows($result); 
      for ($i=0;$i<$G_nb_professeurs;$i++)
	{
	  $a_record = mysql_fetch_array($result);
	  if ($a_record["id_professeur"] == $s_id_professeur) print("  <option value=".$a_record["id_professeur"]." selected>");
	  else print("  <option value=".$a_record["id_professeur"].">");
	  print($a_record["nom"]." ".$a_record["prenom"]."\n");
	}
      print("</select></div>\n");

      // Liste des salles		
		
      print("<font size=2><i><div align=left>Salle :</i></font><BR>");
      print("<select tabindex=-1 name=\"s_id_salle\" size=1 width=200>\n");
      print("<option value=-1>\n");
      $request = "SELECT id_salle,numero FROM salle ORDER BY numero";
      $result = mysql_query($request);
      $G_nb_salles = mysql_num_rows($result); 
      for ($i=0;$i<$G_nb_salles;$i++)
	{
	  $a_record = mysql_fetch_array($result);
	  if ($a_record["id_salle"] == $s_id_salle) print("  <option value=".$a_record["id_salle"]." selected>");
	  else print("  <option value=".$a_record["id_salle"].">");
	  print($a_record["numero"]."\n");
	}
      print("</select></div>\n");

      // Bouton Programmer ! et Supprimer !

      print("<BR>\n");
      print("<input type=\"button\" value=\"Programmer\" OnClick=\"document.form1.choice.value=1; submit();\">\n");
      print("<BR><BR>\n");
      print("<input type=\"button\" value=\"Modifier\" OnClick=\"document.form1.choice.value=3; submit();\">\n");
      print("<BR><BR>\n");
      print("<input type=\"button\" value=\"Supprimer\" OnClick=\"document.form1.choice.value=2; submit();\">\n");
		
      print("      </td>\n");
      print("    </tr>\n");
		
      // Creation des creneaux horaires		
		
      $creneau;
      $g = 0;
		
      // Pour tout creneau de 8h a 20h
      for ($i=8;$i<20;$i+=0.5)
	{
	  print("    <tr valign=BOTTOM>\n");
	  print("      <td valign=middle align=center nowrap><font size=2><i>".(makeHeure($i))."</i></font></td>\n");
			
	  // Pour toutes les semaines de la periode
	  for ($j = 0;$j < $nb_weeks;$j++)
	    {
	      if ($j==0 || $j==$nb_weeks-1) $colspan=2;
	      else $colspan=1;
	      $semaine_reelle = $premiere_semaine + $j + $shift_week;
	      if ($semaine_reelle>52) $semaine_reelle-=52;
	      if (!isset($creneau[($i-8)*2][$j]))
		{
		  $occupe = false;
		  $z = $i-8;
		  $h = date("H:i",mktime(8+floor($z),($z-floor($z)>0)?30:0,0,1,1,2000));
		  if ($id_groupe!=-1)
		    {
		      /*						$request = "SELECT planifie.id_planifie AS T0,matiere.nom AS T1,typeseance.type AS T2,heure_fin,professeur.initiales AS T3 
						FROM planifie,matiere,typeseance,planifiegroupe,planifiepromotion,professeur 
						WHERE planifie.semaine=$semaine_reelle AND planifie.jour_semaine=$s_semaine AND planifie.heure_debut='$h' 
						AND (
							(planifiegroupe.id_groupe = $id_groupe AND planifiegroupe.id_planifie = planifie.id_planifie) 
							OR (planifiepromotion.id_promotion = $id_promotion[0] AND planifiepromotion.id_planifie = planifie.id_planifie)
						) 
						AND planifie.id_matiere=matiere.id_matiere AND planifie.id_type_seance=typeseance.id_type_seance 
						AND planifie.id_professeur=professeur.id_professeur";*/
		      $request = "SELECT planifie.id_planifie AS T0,matiere.nom AS T1,typeseance.type AS T2,heure_fin
						FROM planifie,matiere,typeseance,planifiegroupe 
						WHERE planifie.semaine=$semaine_reelle AND planifie.jour_semaine=$s_semaine AND planifie.heure_debut='$h' 
						AND planifiegroupe.id_groupe = $id_groupe AND planifiegroupe.id_planifie = planifie.id_planifie
						AND planifie.id_matiere=matiere.id_matiere AND planifie.id_type_seance=typeseance.id_type_seance 
						UNION
						SELECT planifie.id_planifie AS T0,matiere.nom AS T1,typeseance.type AS T2,heure_fin
						FROM planifie,matiere,typeseance,planifiepromotion 
						WHERE planifie.semaine=$semaine_reelle AND planifie.jour_semaine=$s_semaine AND planifie.heure_debut='$h' 
						AND planifiepromotion.id_promotion = $id_promotion[0] AND planifiepromotion.id_planifie = planifie.id_planifie
						AND planifie.id_matiere=matiere.id_matiere AND planifie.id_type_seance=typeseance.id_type_seance";
						
		    }
		  else
		    {
		      $request = "SELECT planifie.id_planifie AS T0,matiere.nom AS T1,typeseance.type AS T2,heure_fin 
						FROM planifie,matiere,typeseance,planifiepromotion
						WHERE planifie.semaine=$semaine_reelle AND planifie.jour_semaine=$s_semaine AND planifie.heure_debut='$h' 
						AND planifiepromotion.id_promotion = $id_promotion[0] AND planifiepromotion.id_planifie = planifie.id_planifie 
						AND planifie.id_matiere=matiere.id_matiere AND planifie.id_type_seance=typeseance.id_type_seance";					
		    }
		  $result = mysql_query($request);
		  if ($result!=0 && mysql_num_rows($result)>0)
		    {
		      // Si affichage d'un creneau utilise par un groupe de la promotion
		      if ($occupe)
			{
			  // Si une seule matiere est programmee
			  if (mysql_num_rows($result) == 1)
			    {
			      $a_record = mysql_fetch_array($result);
			      $x = convertHeure($a_record["heure_fin"]);
						
			      // Calcul du nombre de creneaux horaires utilises
			      $y = $x - $i;
			    }
			  else // Plusieurs matieres sont programmees -> recherche de la duree max
			    {
			      $max = 0;
			      $zmax = mysql_num_rows($result);
			      for ($z = 0;$z < $zmax ;$z++)
				{
				  $a_record = mysql_fetch_array($result);
				  $x = convertHeure($a_record["heure_fin"]) - $i;
				  $max = ($max < $x)?$x:$max;
				}
			      $y = $max;
			    }
							
			  // Si le nombre de creneaux horaires est superieur a 1 alors utilisation de rowspan
			  if ($y * 2 > 1) print("      <td valign=middle align=center width=90 bgcolor=\"#c0c0c0\" rowspan=".($y*2)." colspan=$colspan>");
			  else print("      <td valign=middle align=center width=90 bgcolor=\"#c0c0c0\" colspan=$colspan>");

			}
		      else
			{
			  $a_record = mysql_fetch_array($result);
			  $x = convertHeure($a_record["heure_fin"]);
						
			  // Calcul du nombre de creneaux horaires utilises
			  $y = $x - $i;
							
			  // Generation du texte associe au creneau horaire
			  /* --- F. Nolot : Ajout de cette partie pour que la case s'affiche correctement, même quand aucun professeur n'est affecté
à cette matière */
			  $prof="SELECT professeur.initiales AS ini FROM professeur,planifie
							WHERE planifie.id_planifie=".$a_record["T0"]." AND planifie.id_professeur=professeur.id_professeur";
			  $result_prof=mysql_query($prof);
					
			  if ($result_prof!=0 && mysql_num_rows($result_prof)>0) 
			    {
			      $ini_prof=mysql_fetch_array($result_prof);
			      $string = strtr($a_record["T1"],"%20","&nbsp;")."<BR>".$a_record["T2"]."<br>".$ini_prof["ini"];
			    }
			  else 
			    $string = strtr($a_record["T1"],"%20","&nbsp;")."<BR>".$a_record["T2"];

			  // Si le nombre de creneaux horaires est superieur a 1 alors utilisation de rowspan
			  if ($y * 2 > 1) 
			    {
			      if (substr($s_select,$g,1)=="x")
				print("      <td valign=middle align=center width=90 bgcolor=\"yellow\" rowspan=".($y*2)." onMouseUp=\"selectCreneau($j,".(($i-8)*2).",$nb_weeks,".$a_record["T0"].",this)\" colspan=$colspan>$string");
			      else
				print("      <td valign=middle align=center width=90 bgcolor=\"white\" rowspan=".($y*2)." onMouseUp=\"selectCreneau($j,".(($i-8)*2).",$nb_weeks,".$a_record["T0"].",this)\" colspan=$colspan>$string");
			    }
			  else 
			    {
			      if (substr($s_select,$g,1)=="x")
				print("      <td valign=middle align=center width=90 bgcolor=\"yellow\" onMouseUp=\"selectCreneau($j,".(($i-8)*2).",$nb_weeks,".$a_record["T0"].",this)\" colspan=$colspan>$string");
			      else
				print("      <td valign=middle align=center width=90 bgcolor=\"white\" onMouseUp=\"selectCreneau($j,".(($i-8)*2).",$nb_weeks,".$a_record["T0"].",this)\" colspan=$colspan>$string");
			    }
			} 
		      print("</td>\n");
					
		      for ($k=($i-8)*2;$k<($i-8)*2+$y*2;$k++)
			$creneau[$k][$j] = true;
		    }
		  else 
		    {
		      if (substr($s_select,$g,1)=="x")
			print("      <td align=center valign=middle width=90 bgcolor=\"yellow\" onMouseUp=\"selectCreneau($j,".(($i-8)*2).",$nb_weeks,-1,this);\" colspan=$colspan>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n"); 
		      else
			print("      <td align=center valign=middle width=90 bgcolor=\"white\" onMouseUp=\"selectCreneau($j,".(($i-8)*2).",$nb_weeks,-1,this);\" colspan=$colspan>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n"); 
		    }
		}
	      $g++;
	    }
	  print("    </tr>\n");
	}
      print("  </table>\n");

      /******/	
      /******/	
      /*******************************************************************************/	
      /*******************************************************************************/	
		
    }		
}	
print("</form>\n");
buildEndOfPage();
?>
Return current item: EmploiNet : a timetable web manager