<?php
session_start();
include "logo.php";include "dbconnect.php";
date_default_timezone_set("Europe/Athens");
?>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<head>
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></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);
}
</script>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:13px; }
<p style="font-size:14px;"></p>
</style>
</head>
<body>
<h3> ΑΠΟΤΕΛΕΣΜΑΤΑ ΔΙΑΓΩΝΙΣΜΑΤΟΣ</h3>
<?php 

$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$tetr="Α";
$lesson=$_SESSION['lesson'];
$subject=$_SESSION['subject'];
$s=explode("(", $subject);
$s1=$s[0];
$tdate=$_SESSION['tdate'];
$ms=$_SESSION['ms'];

$day = date('D', strtotime($dt));
switch ($day) {
  case "Mon":
    $d="ΔΕΥΤΕΡΑ";
    break;
  case "Tue":
    $d="ΤΡΙΤΗ";
    break;
  case "Wed":
    $d="ΤΕΤΑΡΤΗ";
    break;
  case "Thu":
    $d="ΠΕΜΠΤΗ";
    break;	
  case "Fri":
    $d="ΠΑΡΑΣΚΕΥΗ";
    break;
}


//if (isset($_POST['bathall'])){

$p=$_POST['att'];
$pc1=$_POST['pc1'];
$pc2=$_POST['pc2'];
$pc3=$_POST['pc3'];
$pc4=$_POST['pc4'];
$pc5=$_POST['pc5'];
$pc6=$_POST['pc6'];
$pc7=$_POST['pc7'];
$pc8=$_POST['pc8'];
$pc9=$_POST['pc9'];
$pc10=$_POST['pc10'];
$c1=$_POST['c1'];
$c2=$_POST['c2'];
$c3=$_POST['c3'];
$c4=$_POST['c4'];
$c5=$_POST['c5'];
$c6=$_POST['c6'];
$c7=$_POST['c7'];
$c8=$_POST['c8'];
$c9=$_POST['c9'];
$c10=$_POST['c10'];
$totper=$pc1+$pc2+$pc3+$pc4+$pc5+$pc6+$pc7+$pc8+$pc9+$pc10;
if ($totper > 100 OR $totper < 0) {
echo '<script type="text/javascript">';
        echo 'alert("Percentages do not add to 100")';
        echo '</script>';
		$time = 1; //Time (in seconds) to wait. 
        //$url = "loginguest.php"; //Location to send to. 
        //header("Refresh: $time; url=$url");
		//exit();	
}
$mt=$_POST['mit'];
$num=$_POST['num'];
$cr=count($mt);



$x=$pc1+$pc2+$pc3+$pc4+$pc5+$pc6+$pc7+$pc8+$pc9+$pc10;

mysqli_query($con,"DELETE FROM baros WHERE mathima='$lesson' AND subject='$subject' AND tetramino='$tetr'");
mysqli_query($con,"INSERT INTO baros (mathima, subject,tetramino,poso1, poso2, poso3, poso4, poso5, poso6, poso7,poso8,poso9,poso10,
apo1,apo2,apo3,apo4,apo5,apo6,apo7,apo8,apo9,apo10) VALUES ('$lesson','$subject','$tetramino',
'$pc1','$pc2','$pc3','$pc4','$pc5', '$pc6','$pc7','$pc8','$pc9','$pc10',
'$c1','$c2','$c3','$c4','$c5', '$c6','$c7','$c8','$c9','$c10') ");
	
/*echo '<script type="text/javascript">';
if ($x <> 100) {  
 echo 'alert("ΤΟ ΑΘΡΟΙΣΜΑ ΤΩΝ ΠΟΣΟΣΤΩΝ ΠΡΕΠΕΙ ΝΑ ΕΝΑΙ 100")';}
else
{  echo 'alert("ΤΟ ΑΘΡΟΙΣΜΑ ΤΩΝ ΠΟΣΟΣΤΩΝ ΕΝΑΙ 100 - OK")';}

   echo '</script>';	
   $time =1; //Time (in seconds) to wait. 
   $url =  "";
   header("Refresh: $time; url=$url");
*/
$sql=mysqli_query($con,"SELECT * FROM bathmologio WHERE mathima='$lesson' AND subject='$subject' AND tetramino='$tetr' ");
$c=mysqli_num_rows($sql);

if ($c > 0) {
mysqli_query($con,"DELETE FROM bathmologio WHERE mathima='$lesson' AND subject='$subject' AND tetramino='$tetr'");
}

$i=0;
$cr=count($mt);
while($i<$cr){
$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];$a6=$p[$k][6];$a7=$p[$k][7];$a8=$p[$k][8];$a9=$p[$k][9];
$sql="INSERT INTO bathmologio (mitroo,aks1,aks2,aks3,aks4,aks5,aks6,aks7,aks8,aks9,aks10,mathima,subject,tetramino) VALUES ('$k','$a0','$a1','$a2','$a3','$a4','$a5','$a6','$a7','$a8','$a9','$lesson','$subject','$tetramino' )";

if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  }


$i++;
}
 
  $i--;
//............................
$squ=mysqli_query($con,"SELECT duration FROM declarations WHERE testdate='$tdate' AND class='$lesson' AND (subject='$subject' OR subject='$s1')");
$rowu=mysqli_fetch_array($squ);$duration=$rowu['duration'];
mysqli_query($con,"UPDATE declarations SET returndate='$dt' WHERE testdate='$tdate' AND class='$lesson' AND (subject='$subject' OR subject='$s1')");

echo "<table class='myTable' >
<tr style='border:1px solid black' >
<tr>
<th>Όνομα καθηγητή/τριας</th>
<th>".$name."</th>
</tr>";

echo "<tr>";
echo "<th>"."Τμήμα / Συνδιδασκαλία : "."</th>";
echo "<th>".$lesson."</th>";
echo "</tr>";
echo "<tr>";
echo "<th>"."Μάθημα: "."</th>";
echo "<th>".$subject."</th>";
echo "</tr>";
echo "<tr>";
echo "<th>"."Αριθμός μαθητών/τριών : "."</th>";
echo "<th>".$cr."</th>";
echo "</tr>";
echo "</tr>";
echo "<th>"."Τετράμηνο : ".$tetramino."</th>";
echo "<th>"." Διάρκεια :".$duration."</th>";
echo "</tr>";
echo "</tr>";
echo "<th>". "Ημερομηνία διεξαγωγής :"."</th>";
echo "<th>".$tdate."</th>";
echo "</tr>"; 
echo "</table>";  
echo "<br>"."Απόντες μαθητές / τριες :"."<br>";


//*****************************************
$field=$_POST['bath'];
$sql=mysqli_query($con,"SELECT {$field},mitroo FROM bathmologio WHERE mathima='$lesson' AND subject='$subject' AND tetramino='$tetr' ORDER BY {$field} ASC");
$c=mysqli_num_rows($sql);

$n=substr($field, -1);
$cb=${'c'.$n};

$p=array();
$kat=array();
if ($c > 0) {
	for($j=1;$j<=5;$j++) {$kat[$j]=0;}
	
   $k=1;$apon=0;
   while($row = mysqli_fetch_array($sql)){
	   
       $p[$k]= $row["$field"]*20/$cb;
	   if ($p[$k]==0) 
	     { $apon++;
		    $mitroo=$row['mitroo'];
		   $result = mysqli_query($con,"SELECT eponimo,onoma FROM allstudentinfo WHERE mitroo='$mitroo'  ");
           $rowmit = mysqli_fetch_array($result);
           echo $mitroo. "  ".$rowmit['eponimo']." ".$rowmit['onoma']."<br>";}
		elseif ($p[$k] < 10 ) {
           $kat[1]++;
       } elseif ($p[$k] <12) {
          $kat[2]++;
       } elseif ($p[$k] < 15) {
           $kat[3]++;
		} elseif ($p[$k] < 18) {
           $kat[4]++;
		}
		else  { $kat[5]++; }
       $k++;
   }
   echo "<br>". "Σύνολο απόντων : ".$apon;
echo "<br>";
echo "<br>";  
?>
<h3> ΣΤΑΤΙΣΤΙΚΑ  -   ΚΑΤΑΝΟΜΗ ΒΑΘΜΩΝ</h3>
<?php
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>";

$mesos=array_sum ($p)/$c;
//   echo "ΜΕΣΟΣ ΟΡΟΣ ΤΜΗΜΑΤΟΣ : ".number_format($mesos,2)."<br>" ;
   $std=stats_standard_deviation($p);
//   echo "ΤΥΠΙΚΗ ΑΠΟΚΛΙΣΗ ΒΑΘΜΩΝ : ".number_format($std,2);
$tot=$c-$apon;
echo "<tr>";
echo "<th>".$tot ."</th>";
echo "<th>".number_format($mesos,2)."</th>";
echo "<th>".number_format($std,2)."</th>";

for($j=1;$j<=5;$j++) {echo "<th>".$kat[$j]."</th>";}
echo "</tr>"; echo "</table>";  
}
$j=3;$ns=$cr;
while($j<=20) {$kk[$j]=0;$j=$j+3;}
for($k=1;$k<=$ns;$k++)
{
  if ($p[$k] > 0)
  {
    $b[$k]= $p[$k]*20/$cb;
	$g=$b[$k];

   if      ($g <= 3 ) {$kk[3]++;} 
	elseif ($g <= 6)  {$kk[6]++;} 
	elseif ($g <= 9)  {$kk[9]++;} 
	elseif ($g <= 12) {$kk[12]++;}
	elseif ($g <= 15) {$kk[15]++;}
	elseif ($g <= 18) {$kk[18]++;}
	elseif ($g <= 20) {$kk[20]++;}
   }
}
print_r($kk);
//setup graph
$graph = new stdclass;
$graph->width = 900;
$graph->height = 350;
$graph->data=$kk;
$graph->setGradient = array('blue', 'green');
$graph->setLegend = 'false';
//$graph->setLegendTitle = '';
//$graph->setTitle = 'Κατανομή Βαθμολογίας';
$graph->setTitleLocation = 'left';
  
//JSON encode graph object
$encoded = urlencode(json_encode($graph));
  
//retrieve XML
$target = 'http://www.ebrueggeman.com/phpgraphlib/api/?g=' . $encoded . '&type=xml';
$xml_object =  new SimpleXMLElement($target, NULL, TRUE);
  
//if there are no errors, display graph
if (empty($xml_object->error)) {
  echo $xml_object->imageTag;
}
else {
  echo 'There was an error generating the graph: '. $xml_object->error;
}
//.....................  
//}

echo "<br>"."<br>";
echo "ΠΑΡΑΤΗΡΗΣΕΙΣ :"."<br>";
echo "................................................................................................."."<br>";
echo "................................................................................................."."<br>";
echo "................................................................................................."."<br>";
echo "................................................................................................."."<br>";
echo "<br>"."<br>";
echo "Ημερομηνία: ..........................  Υπογραφή καθηγητή/τριας : ................................";

/// calculate standrd 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;
        }
        $mean = array_sum($a) / $n;
        $carry = 0.0;
        foreach ($a as $val) {
            $d = ((double) $val) - $mean;
            $carry += $d * $d;
        };
        if ($sample) {
           --$n;
        }
        return sqrt($carry / $n);
    }

?>
</body>
</html>