<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>notes</title>
</head>

<body>
<form method='post' action='continuernotes.php' >
<?php
include("_serveur.php");
include 'header.php';

echo "<a href=\"javascript:history.go(-1)\"><button type='button' >Retourner</button></a>";
$classeID=$_SESSION['classe1'];
$controle1=$_SESSION['ctrl1'];
$subSubjectID1=$_SESSION['subSubjectID'];
$cellctr=explode(";",$controle1);
$ctrlID=$cellctr[0];
$controle1=$cellctr[1];
$sql = 'SELECT * from classes where id = "'.$classeID.'"  ';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
 while($data = mysqli_fetch_array($req)) {
$classe=$data['1'];
 }
$sql = 'SELECT * from subSubjects where id = "'.$subSubjectID1.'"  ';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
 while($data = mysqli_fetch_array($req)) {
$subSubject=$data['2'];
 }

echo '<div class="w3-panel w3-blue w3-round-xlarge"><p>'.$classe.'    '.$controle1.'    '.$subSubject.'</p></div>';

$sql = 'SELECT * from gradesSubjects where classID = "'.$classeID.'" and subSubjectID="'.$subSubjectID1.'" ';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
 while($data = mysqli_fetch_array($req)) {
$gradesID=$data['id'];
$coef=$data['coeficient'];	
 }
$mat1="";
$sql1 = 'SELECT * from grades where classID = "'.$classeID.'" and semesterID="'.$ctrlID.'" and subSubjectID = "'.$subSubjectID1.'"';
   if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req1=  mysqli_query($link, $sql1);
$mat=0;
 while($data1 = mysqli_fetch_array($req1)) {
          $mat=$data1['grade'];
              }
 if($mat==0)
{

$sql = 'SELECT * from students inner join users on students.userID=users.id
                                where classID="'.$classeID.'"  order by students.firstName,students.lastName,users.firstName';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$i=0;
echo '<table class="w3-table w3-striped w3-bordered">';
 while($data = mysqli_fetch_array($req)) {
	 $studentID[$i]=$data[0];
	 $firstName[$i]=$data[2];
	 $lastName[$i]=$data[3];
	 $middleName[$i]=$data[8];
	 $num=$i+1;
	 echo "<tr><td><strong>".$num."</td></strong><td><strong>".$firstName[$i]."</strong> </td><td><strong>".$middleName[$i]."</strong> </td><td><strong>".$lastName[$i]."</strong> </td>";;
echo "<td>
<input name='note[]' type='number' step='any'></td>";
 echo"<td>".$coef."</td></tr>";
   $i=$i+1;

 }
echo '</table>'; 
echo "<tr><td colspan='3'><input type='submit' id='publish' name='publish' value='Publier' /></td></tr></form>";

if(isset($_POST['publish']))
{
$note1=array();	
$note1=$_POST['note'];
$j=0 ;
while($j<sizeof($note1)){
	if(($note1[$j]>$coef)or($note1[$j]<-1)or($note1[$j]==0)){	
echo "<script type='text/javascript'>alert('Valeur inacceptable !!!')</script>";
    //echo '<META HTTP-EQUIV="Refresh" Content="0; URL=continuernotes.php">';
	exit(0); 
	}
	$j++;
}


echo "</table>";
$j=0;
while($j<=sizeof($note1))
{
	if($note1[$j]<>0){ 
   $sql = 'INSERT INTO grades VALUES(DEFAULT,"'.$studentID[$j].'","'.$classeID.'","'.$ctrlID.'","'.$subSubjectID1.'","'.$note1[$j].'")';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
	
                                }	
                       }

$j=$j+1;

}

}
}

else
{
		echo "<script type='text/javascript'>alert('Déjà rempli !!!')</script>";
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=_ecrirenoteparmatiere1.php">';    
  

}
?>

 
</body>
</html>