<?php
include "scrinit.php";
//include "logo.php";
?>
<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>


<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
include "logo.php";
echo "ΣΥΝΟΠΤΙΚΟ ΑΞΙΟΛΟΓΗΣΗΣ";
require "dbconnect.php";
$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$teacher=$_SESSION['usr'];
$tetramino=$_SESSION['tetramino'];
if (isset($_GET['data']))
{
    $code=$_GET['data'];
}
$sql1=mysqli_query($con,"select * from aksinfo where code='$code'");
$row=mysqli_fetch_array($sql1);
$typea=$row['type'];
$maxmark=$row['maxmark'];
$tmima = $row['tmima'];
$subject = $row['subject'];
$percent=$row['percent'];
$duration=$row['diarkia'];
$date=$row['date'];
$dateaks=$row['dateaks'];

$sql=mysqli_query($con,"SELECT * FROM aksiologisi WHERE code='$code' ");
$i=0;$grd=$mt=$epo=$tmim=array();
while($row1=mysqli_fetch_array($sql))
{
	$i++;
	$grd[$i]=$row1['bathmos'];
	$mt[$i]=$row1['mitroo'];
	$result = mysqli_query($con,"SELECT eponimo,onoma,tmima FROM allstudentinfo WHERE mitroo='$mt[$i]' ORDER BY eponimo,onoma  ");
	$crow1=mysqli_fetch_array($result);
	$epo[$i]=$crow1['eponimo']." ".$crow1['onoma'];	
	$tmim[$i]=$crow1['tmima'];
}
	
if (array_sum($grd) > 0)
{

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>".$i."</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>";
$ns=$i;
$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>