<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js"></script>
<script type = "text/javascript">
         google.charts.load('current', {packages: ['corechart']});     
</script>
<script type="text/javascript">
function popupCenter(pageURL, title, w, h) {
    var left = (screen.width / 2)  - (w / 2);
    var top  = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800, height=800, top='+top+', left='+left);
}

function submitform()
{
    document.forms["bath"].submit();
}

function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=800,height=800,directories=no,location=no') 
}

</script>

<script type="text/javascript">
var prompt=true;

		$(document).ready(function() {

				$('input:not(:button,:submit),textarea,select').change(function () {
					window.onbeforeunload = function () {
						if (prompt == true) 
							return 'Δεν έχετε αποθηκεύσει τους βαθμούς που έχετε καταχωρίσει';
						}
					});


				$('input:submit').click(function(e) {
					prompt = false;
					});
	    });
		
</script>

<style type="text/css">
input.right{  text-align:right;  }
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:1px;font-size:9pt; }
</style>

</head> 
<body>

<?php
echo "ΣΥΝΟΠΤΙΚΟ ΑΞΙΟΛΟΓΗΣΗΣ";
require "dbconnect.php";
$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$teacher=$_SESSION['usr'];
$tetramino=$_SESSION['tetramino'];
//
$tmima=$_POST['tmima'];
$subject=$_POST['subject'];
$grd=$_POST['grade']; 
$code=$_POST['code'];
if (array_sum($grd) > 0)
{

$maxmark=$_POST['maxmark'];
$duration=$_POST['diarkia'];
$typea=$_POST['typea'];
$date=$_SESSION['dt'];
$id=$_POST['aksioid'];
$epflag=$_POST['epistrofi'];
$testdate=$_POST['testdate'];

$sql="update declarations set code='$code' where id='$id'";	
if (mysqli_query($con, $sql)) 
		{
			        } 
		else 
		{
            echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }

$result = mysqli_query($con,"SELECT distinct 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);
if ($crow1 > 0)
{
	$cr=1;
	while($row=mysqli_fetch_array($result))
	{	
		$mt[$cr]=$row['mitroo'];
	    $epo[$cr]=$row['eponimo']." ".$row['onoma'];	
		$tmim[$cr]=$row['tmima'];
		$cr++;
	}
}
else
{	
$result = mysqli_query($con,"SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima='$tmima' ORDER BY eponimo,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]=$tmima;
	 $cr++;
	 } 
}
if (is_null($code) or $code==0)
{
	$sql="insert into aksinfo (pmpteacher,tmima,subject,tetramino,type,date,dateaks,declaid,maxmark,percent,diarkia) 
	values ('$teacher','$tmima','$subject','$tetramino','$typea','$dt','$testdate','$did','$maxmark','$percent','$diarkia')";	
    
	if (!mysqli_query($con, $sql)) 
		{
            echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }
	$code= $con->insert_id;	
   
}
else
{
   mysqli_query($con,"update aksinfo set maxmark='$maxmark',percent='$percent',type='$typea',date='$date',
             diarkia='$duration',declaid='$id' where code='$code'");
}
$ns=$cr-1;	
$sql=mysqli_query($con,"SELECT * FROM aksiologisi WHERE code='$code' ");
$c=mysqli_num_rows($sql);

if ($c > 0) {
mysqli_query($con,"DELETE FROM aksiologisi WHERE code='$code'");
}

$i=1;
while($i<=$ns)
{
  $k=$mt[$i];
  $sql="INSERT INTO aksiologisi (mitroo,bathmos,code) VALUES ('$k','$grd[$i]','$code' )";
  if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  }
  
  $i++;
}
 
$i--;

echo "<table class='myTable' style='font-size:9pt' >
<tr style='border:1px solid black' bgcolor='#ffcc00'  >
<th>Όνομα καθηγητή/τριας</th>
<th>Τμήμα / Μάθημα</th>
<th>Είδος Αξιολόγησης</th>
<th>Αρ. μαθητών/τριών</th>
<th>Τετράμηνο </th>
<th>Διάρκεια</th>
<th>Ημ. Καταχ.</th>";
echo "<tr>";
echo "<td>"."<b>".$name."</td>";
echo "<td>".$tmima." / ".$subject."</td>";
echo "<td>".$typea."</td>";
echo "<td>".$ns."</td>";
echo "<td>".$tetramino."</td>";
echo "<td>".$duration." λεπτά"."</td>";
echo "<td>".date('d-m-Y', strtotime($date))."</b>"."</td>";
echo "</tr>";
echo "</table>"; 

array_multisort($grd,SORT_DESC,$mt,$tmim,$epo);
echo "<br>";
?>
<table>
<thead>
<th>ΒΑΘΜΟΙ ΜΑΘΗΤΩΝ-ΤΡΙΩΝ</th>
<th>ΣΤΑΤΙΣΤΙΚΑ - ΚΑΤΑΝΟΜΗ</th>
</thead>
<tr>
<td>
<?php
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>α/α</th>
<th>Α.Μ</th>
<th>Τμήμα</th>
<th>Όνοματεπώνυμο</th>
<th>Βαθμός</th>
</tr>";

$i=0;
$apon=0;
$cnz=0;
$k=1;
while($i <$ns){
  	
  echo "<tr>";
  echo "<td>" .$k;
  echo "<td>". $mt[$i] . "</td>";
  echo "<td>". $tmim[$i] . "</td>";
  echo "<td>". $epo[$i] . "</td>";
  if ($grd[$i]==0)
  {
	$apon++;
	echo "<td>". "ΑΠΩΝ/ΟΥΣΑ"."</td>";  
  }
  else
  {
  $cnz++;
  echo "<td>"."<p align=right>".$grd[$i]."</td>"; 
  }
  $k++;
  $i++;
}
echo "</table>";
echo "</td>";
$ns=count($grd);
$b=array();
$kat=array();
if ($ns > 0) 
{
	for($j=1;$j<=5;$j++) {$kat[$j]=0;}
	
   $k=0;
   while($k<$ns)
   {
     $b[$k]= $grd[$k]*(20/$maxmark);
		if ($b[$k]<=0) {}
		elseif ($b[$k] < 10 ) 
		{
           $kat[1]++;
        } 
        elseif ($b[$k] < 12)
        {
          $kat[2]++;
        } 
        elseif ($b[$k] < 15)
        {
           $kat[3]++;
		} 
		elseif ($b[$k] < 18) 
		{
           $kat[4]++;
		}
		else  
		{ 
			$kat[5]++; 
		}
       
       $k++;
   }

echo "<td style='vertical-align:top'>";

echo "<table class='myTable' >
<tr style='border:1px solid black' >
<tr>
<th>ΜΑΘΗΤΕΣ</th>
<th>ΜΕΣΟΣ</th>
<th>ΤΥΠ.ΑΠΟΚΛ.</th>
<th>1-9</th>
<th>10-11</th>
<th>12-14</th>
<th>15-17</th>
<th>18-20</th>
</tr>";
$c=$ns-$apon;
$mesos=array_sum($b)/$cnz;
//   echo "ΜΕΣΟΣ ΟΡΟΣ ΤΜΗΜΑΤΟΣ : ".number_format($mesos,2)."<br>" ;
   $std=stats_standard_deviation($b);
//   echo "ΤΥΠΙΚΗ ΑΠΟΚΛΙΣΗ ΒΑΘΜΩΝ : ".number_format($std,2);
$tot=$c;
echo "<tr>";
echo "<td>".$tot ."</td>";
echo "<td>".number_format($mesos,2)."</td>";
echo "<td>".number_format($std,2)."</td>";

for($j=1;$j<=5;$j++) {echo "<td>".$kat[$j]."</td>";}
echo "</tr>"; echo "</table>";  
}
echo "<br>". "Σύνολο απόντων : ".$apon;
echo "</td>";echo "</tr>";echo "</table>"; 

$kk=array();
for($k=0;$k<=$ns;$k++)
{
  if ($grd[$k] > 0)
  {
    $b[$k]= $grd[$k]*20/$maxmark;
	$g=$b[$k];

    if     ($g <= 3 ) {$kk[3]++;} 
	elseif ($g <= 6)  {$kk[6]++;} 
	elseif ($g <= 10)  {$kk[10]++;} 
	elseif ($g <= 12) {$kk[12]++;}
	elseif ($g <= 15) {$kk[15]++;}
	elseif ($g <= 18) {$kk[18]++;}
	elseif ($g <= 20) {$kk[20]++;}
   }
}
$inx=array();
$inx=['3','6','10','12','15','18','20'];

for ($i=0;$i<7;$i++)
{
	if (empty($kk[$inx[$i]])){$kk[$inx[$i]]=0;}
	$mygrade[] = "[".$inx[$i].",".$kk[$inx[$i]]."]";
}

?>
<div id = "container" style = "width:700px; height: 300px;border: 1px solid black; ">
<script language = "JavaScript">
         function drawChart() {
            // Define the chart to be drawn.
            var data = new google.visualization.DataTable();
			var kk1 = <?php echo json_encode($kk); ?>;
            data.addColumn('string', 'Διάστημα');
            data.addColumn('number', 'Πλήθος Μαθητών');
            data.addRows([
            ['3', kk1[3]],
            ['6', kk1[6]],
            ['10', kk1[10]],
            ['12', kk1[12]],
            ['15', kk1[15]],
			['18', kk1[18]],
			['20', kk1[20]]
          ]);

	  
            // Set chart options
            var options = {'title':'ΚΑΤΑΝΟΜΗ ΒΑΘΜΩΝ ΑΞΙΟΛΟΓΗΣΗΣ',hAxis: {title: 'Βαθμός'},vAxis: {title: 'Πλήθος'}};

            // Instantiate and draw the chart.
            var chart = new google.visualization.ColumnChart(document.getElementById ('container'));
            chart.draw(data, options);
         }
         google.charts.setOnLoadCallback(drawChart);
      </script>
</div>
<?php
echo "<p>"."ΠΑΡΑΤΗΡΗΣΕΙΣ :"."<br>";
echo ".................................................................................................................................................................................................."."<br>";
echo ".................................................................................................................................................................................................."."<br>";
echo "</p>";

echo "Ημερομηνία: ..........................  Υπογραφή καθηγητή/τριας : ................................";
}
else
{
	 echo '<script>
	 window.close();
     alert("ΔΕΝ ΈΧΕΤΕ ΚΑΤΑΧΩΡΗΣΕΙ ΒΑΘΜΟΥΣ!");
	 
     </script>';
}
/// calculate standard deviation
function stats_standard_deviation(array $a) 
{
		$n=count($a);
		if ($n === 0) {
            trigger_error("The array has zero elements", E_USER_WARNING);
            return false;
        }
        if ($sample && $n === 1) {
            trigger_error("The array has only 1 element", E_USER_WARNING);
            return false;
        }
		$n=0;$sm=0;
        foreach ($a as $val)
		{
			if ($val > 0)
			{ 
				$n++;
			    $sm=$sm+$val;
			}
		}
		        
        $mean = $sm / $n;
        $carry = 0.0;
        foreach ($a as $val)
		{
			if ($val > 0)
			{
				$d = ($val) - $mean;
			    $carry += $d * $d;
			}
        };
        if ($sample) {
        --$n;
       }
        return sqrt($carry / $n);
}

?>
</body>
</html>