<?php
include "scrinit.php";
include $menu;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Παιδαγωγικές Ομάδες</title>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid green;padding:2px;font-size:10pt; }
<p style="font-size:10px;"></p>
</style>

</head>

<body>

<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$usr=$_SESSION['usr'];
$lsn=$_GET['math'];
$ar=explode('/',$lsn);
$lesson=$ar[1];
$tmima=$ar[0];

echo "ΠΟΙΟΤΙΚΗ ΑΞΙΟΛΟΓΗΣΗ ΓΙΑ ΤΟ ";
echo " Τμήμα/Συνδ.: "."<b>".$tmima." / ".$lesson."</b>"."<br>"."<br>";

$result = mysqli_query($con,"SELECT s.mitroo,al.eponimo,al.onoma,al.tmima 
FROM syndidaskalies as s,allstudentinfo as al, syndcodes as sc 
WHERE sc.synd = '$tmima' AND s.mitroo=al.mitroo AND sc.syndid=s.syndid ORDER BY al.eponimo,al.onoma");
$crow1=mysqli_num_rows($result);

$cr=1;
while($row=mysqli_fetch_array($result))
{	
		$mt[$cr]=$row['mitroo'];
		$epo[$cr]=$row['eponimo']." ".$row['onoma'];	
		$tmim[$cr]=$row['tmima'];
		$cr++;
}
	
if ($crow1==0)
{		
$result = mysqli_query($con,"SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima='$tmima' ORDER BY eponimo,onoma  ");
$crow1=mysqli_num_rows($result);
    $i=1;
    while($row = mysqli_fetch_array($result))
     {
	 $mt[$i]=$row['mitroo'];
	 $epo[$i]=$row['eponimo']." ".$row['onoma'];
	 $tmim[$i]=$tmima;
	 $i++;
	 }
}	
//....................................
$sql=mysqli_query($con,"SELECT id FROM pedamaster WHERE lesson='$lesson' AND teacher='$usr' and tmima='$tmima' ");
$c=mysqli_num_rows($sql);
if ($c > 0) 
{
   $row = mysqli_fetch_array($sql);
   $code=$row['id'];
   $sql1=mysqli_query($con,"SELECT * FROM pedanew WHERE code='$code'");
   while($row1 = mysqli_fetch_array($sql1))
   {
    $k=	$row1['mitroo'];
    $p[$k][0]= $row1['sinepia'];
	$p[$k][1]=$row1['pitharxia'];
	$p[$k][2]=$row1['beltiosi'];
	$p[$k][3]=$row1['protoboulia'];
	$p[$k][4]=$row1['problimata'];
	$p[$k][5]=$row1['grade'];
   }
}
else
{
	mysqli_query($con,"INSERT INTO pedamaster (teacher,lesson,tmima,date) values ('$usr','$lesson','$tmima','$dt')");
    $code = mysqli_insert_id($con);
}

?>

<form method="POST" name = "eggrafi" action= >
 
<?php
echo "<table class='myTable' >
<tr style='border:1px solid green' bgcolor='#d6ff73'  >
<th>α/α</th>
<th>Α.Μ</th>
<th>Τμήμα</th>
<th>Όνοματεπώνυμο</th>"."<th>".wordwrap('ΣΥΝΕΠΕΙΑ',15,"<br>\n")."</th>"."<th>".wordwrap('ΠΕΙΘΑΡΧΙΑ',15,"<br>\n")."</th>"."<th>".wordwrap('ΠΡΟΣΠΑΘΕΙΑ/ ΒΕΛΤΙΩΣΗ',15,"<br>\n")."</th>"."<th>".wordwrap('ΠΡΩΤΟΒΟΥΛΙΑ/ ΣΥΝΕΙΣΦΟΡΑ',15,"<br>\n")."</th>"
."<th>".wordwrap('ΕΠΙΔΟΣΗ',15,"<br>\n")."</th>"."<th>".wordwrap('Προβλ.Υγείας/ Οικογεν.',15,"<br>\n")."</th>"."<th>".wordwrap('ΑΠΟΥΣΙΕΣ',15,"<br>\n")."</th>"."<th>".wordwrap('ΜΕΣΟΣ ΜΑΘΗΜ.',15,"<br>\n")."</th>"."<th>".wordwrap('ΓΕΝΙΚΗ ΕΙΚΟΝΑ',15,"<br>\n")."</th>"."</tr>";

echo "<tr bgcolor=#F5F89B>
<th colspan='4'>"."<th>".wordwrap('0-5',15,"<br>\n")."</th>"."<th>".wordwrap('0-5',15,"<br>\n")."</th>"."<th>".wordwrap('-3 μέχρι 3',15,"<br>\n")."</th>"."<th>".wordwrap('0-5',15,"<br>\n")."</th>"
. "<th>".wordwrap('1-20',15,"<br>\n")."</th>"."<th>".wordwrap('Π,Σ,Α',15,"<br>\n")."</th>"."<th>".wordwrap(' ',15,"<br>\n")."</th>"."<th>".wordwrap('.',15,"<br>\n")."</th>"."<th>".wordwrap('από/18',15,"<br>\n")."</th>"."</th>"."</tr>";

$i=1;
while($i<=$crow1)
     {
	$aver[$i]=0;$total[$i]=0; 
	echo "<tr>";
	echo "<td>" .$i;
	echo "<td>". $mt[$i] . "</td>";
	$sqla=mysqli_query($con,"select grade from pedanew where mitroo='$mt[$i]'");
	$pl=mysqli_num_rows($sqla);
	$sqlX = mysqli_query($con,"SELECT count(apousia) as capou FROM apousies WHERE apousia=1 and mitroo='$mt[$i]' group by mitroo");
	$row1=mysqli_fetch_array($sqlX);
	$apou[$i]=$row1['capou'];	
	$pr=mysqli_query($con,"select problima,stiriksi,aporos from problimata where mitroo='$mt[$i]'");
	$rowpr=mysqli_fetch_array($pr);
	$psa[$i]="";
	if ($rowpr['problima'] <> ""){$psa[$i]=$psa[$i]."Π";}
	if ($rowpr['stiriksi'] <> 0){$psa[$i]=$psa[$i]."Σ";}
	if ($rowpr['aporos'] <> 0){$psa[$i]=$psa[$i]."Α";}
//.......... ΣΤΑΤΙΣΤΙΚΗ	 
$sqlstat = mysqli_query($con,"SELECT count(grade) as cg,sum(grade) as sg,sum(sinepia) as ss,sum(pitharxia) 
          as sp,sum(beltiosi) as sb,sum(protoboulia)as spr FROM pedanew WHERE mitroo='$mt[$i]' ");
	$rowstat=mysqli_fetch_array($sqlstat);
	$c1=$rowstat['cg']; 
	if ($c1 > 0) 
	{
	   $aver[$i]=$rowstat['sg']/$pl;
	
       $total[$i]=($rowstat['ss']+$rowstat['sp']+$rowstat['sb']+$rowstat['spr'])/$pl;
	}	
//.........................................	 
	 echo "<td>". $tmim[$i] . "</td>";
	 echo "<td>".$epo[$i] . "</td>"; 
	 $mitroo= $mt[$i];
	 if ($c > 0) {
	   $a0=$p[$mitroo][0];
	   $a1=$p[$mitroo][1];
	   $a2=$p[$mitroo][2];
	   $a3=$p[$mitroo][3];
	   //$a4=$p[$mitroo][4];
	   $a5=$p[$mitroo][5];}
	 else
	 {
	   $a0=" ";
	   $a1=" ";
	   $a2=" ";
	   $a3=" ";
	   //$a4=" ";
	   $a5="";} 
	
	 echo "<td align='center'>". "<input type='text' name='att[$mitroo][0]' value='$a0' size=4 >" ."</td>"; 
	 echo "<td align='center'>". "<input type='text' name='att[$mitroo][1]' value='$a1' size=4 >" ."</td>";
	 echo "<td align='center' >". "<input type='text' name='att[$mitroo][2]' value='$a2'  size=4>" ."</td>";
	 echo "<td align='center'>". "<input type='text' name='att[$mitroo][3]' value='$a3' size=4>" ."</td>";
	 echo "<td>". "<input type='text' name='att[$mitroo][5]' value='$a5' size=4>" ."</td>";
	 echo "<td align='center'>". $psa[$i] ."</td>";
	 echo "<td>".$apou[$i] ."</td>";
	 $num=$aver[$i];
	 $num1=$total[$i];
	 if ($num > 10)
	 {
	    echo "<td align='center'  >". number_format($num,2)  ."</td>";
	 }
	 else
	 {
		echo "<td align='center' bgcolor='#FF0000'>". number_format($num,2)  ."</td>"; 
	 }
	 if ($num1 > 9)
	    {echo "<td align='center'>". number_format($num1,2) ."</td>";}
     else
        {echo "<td align='center' bgcolor='#FF0000' >". number_format($num1,2) ."</td>";}
     
	 echo "</tr>";
     $i++;
}
echo "</table>";
$i--;
?>
<input type='hidden' name='code' value=<?php echo $code; ?> >
<input type='hidden' name='lsn' value=<?php echo $lsn; ?> >
<br>
<input type="Submit" name="pedag" value="Καταχώρηση">
</form>
<?php
if (isset($_POST['pedag'])){
$p=$_POST['att'];
$code=$_POST['code'];
$lsn=$_POST['lsn'];

mysqli_query($con,"DELETE FROM pedanew WHERE code='$code'  ");

$i=1;
while($i<=$crow1){
  $k=$mt[$i];
  $a0=$p[$k][0];$a1=$p[$k][1];$a2=$p[$k][2];$a3=$p[$k][3];$a4=$p[$k][4];$a5=$p[$k][5];
  $sql="INSERT INTO pedanew (mitroo,sinepia,pitharxia,protoboulia,beltiosi,problimata,grade,code) VALUES ('$k','$a0','$a1','$a2','$a3','$a4','$a5','$code' )";
  if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));}
  $i++;
}
$i--;
echo $i." records added";
$sql=mysqli_query($con,"SELECT * FROM pedamaster WHERE id='$code'");
$row=mysqli_fetch_array($sql);
$lsn=$row['tmima']."/".$row['lesson'] ;
echo $lsn;
header("Location:pedadetnew.php?math=".$lsn);	
}
 
?>
<br />
<a  href=<?php echo $menu;?> style="color:blue "  >  Επιστροφή στο Μενού  </a>

</body>
</html>