<!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">
<link rel="stylesheet" href="w3.css">

<title>Bulletin</title>
<style type="text/css">
<!--
l@media print  { .noprint  { display: none; }
.page-break  { display:block; page-break-after:always; }
@page  
{ 
   size: 210mm 297mm; 
  /* Chrome sets own margins, we change these printer settings */
  margin: 5mm 5mm 5mm 5mm;
        }

}
body,td,th {
font-family: "Times New Roman", Times, serif;
size:12px;
}
	

-->
</style>
</head>

<body bgcolor="#FFFFFF">

<form method="post" action="_statfinal2.php" >
<?php
echo "<div class='noprint'>";
include 'header.php';
echo "</div>";
$cycle=$_GET['cycle'];
include '_serveur.php';?>
<input name="cycle" type="hidden" value="<?php $cycle=$_REQUEST['cycle']; echo $cycle; ?>">
<?php
$sql = 'SELECT * from classes where cycleID="'.$cycle.'" order by id DESC';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
echo '<div class="noprint">';
echo '<label class="w3-text-teal" >Classe:</label><select name="cla" class="w3-select w3-border w3-light-grey">';

 while($data = mysqli_fetch_array($req)) {
$classe=$data['name'];
$classeid=$data['id'];
$classe=strtoupper($classe);
 echo ' <option value="'.$classeid.";".$classe.'">'.$classe.'</option>';
        };

echo '</select>';

 echo'<input name="chercher" type="submit" class="style19" value="Envoyer" bgcolor="#CC99FF" /></form>';
echo'<button type="button" class="noprint" onclick="window.print()" /><img src="imprimante.png" height="32" width="40" /></div>';

if (isset ($_POST['chercher'])){
	$cla1=$_POST['cla'];
	$cellcla=explode(";",$cla1);
$classeID=$cellcla[0];
$classe=$cellcla[1];
echo "Classe:".$classe."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date :".date('d/m/Y');
$sql = 'SELECT * from classes inner JOIN classesSemesters on classes.id=classesSemesters.classID
                               inner JOIN semesters on semesters.id=classesSemesters.semesterID where classesSemesters.classID="'.$classeID.'" order by semesters.id' ;

   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                                   }
$req=  mysqli_query($link, $sql);
$i=0;
  while($data = mysqli_fetch_array($req)){
   $cont[$i]=$data[8];
   $i=$i+1;
  }
$sql = 'SELECT * from gradesSubjects inner join subSubjects on gradesSubjects.subSubjectID=subSubjects.id
							   inner join subjects on subSubjects.subjectID=subjects.id
							   where gradesSubjects.classID="'.$classeID.'" order by subjects.ordering,gradesSubjects.ordering  ' ;
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);

$i=0;
  while($data = mysqli_fetch_array($req)){
	  $coefficient1[$i]=$data[2];
	  $sousmatid1[$i]=$data[4];
	  $matsous[$i]=$data[5];
	  $sousmat1[$i]=$data[6];
	  $matid1[$i]=$data[7];
	  $mat1[$i]=$data[8];
	  
	 // echo  $matid1[$i]." ".$mat1[$i]." ".$sousmatid1[$i]." ".$sousmat1[$i]." ".$coefficient1[$i]."<br>";
  $i=$i+1;
   }
   $matid2 = array_unique($matid1, SORT_REGULAR);

   $mat2=array_unique($mat1, SORT_REGULAR);
   
$sql = 'SELECT Distinct semesters.id,semesters.name from semesters 
                        inner join classesSemesters on semesters.id=classesSemesters.semesterID
                        inner join grades on grades.semesterID=semesters.id and grades.classID=classesSemesters.classID
						 where  classesSemesters.classID="'.$classeid.'" order by semesters.id ASC';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                                   }
$req=  mysqli_query($link, $sql);
$k=0;
echo '<table width="98%" border="1" align="center" cellspacing="0">';
echo '<tr><td width="10%"></td>';

 while($data = mysqli_fetch_array($req)) {
$sem[$k]=$data[1];
$semid[$k]=$data[0];
          echo "<td width='7%'> ".$sem[$k]."</td>";
$k++;

        };

echo "<td width='7%'>Moyenne</td></tr>";	 
$sql = 'SELECT * from  classes  left join students on  students.classID=classes.id
                                left  join grades on students.id=grades.studentID and students.classID=grades.classID 
                                left join gradesSubjects on grades.subSubjectID=gradesSubjects.subSubjectID and grades.classID=gradesSubjects.classID
							     left join subSubjects on grades.subSubjectID=subSubjects.id
							    left join subjects on subSubjects.subjectID=subjects.id
							    left join classesSemesters on classesSemesters.classId=classes.id and grades.semesterID=classesSemesters.semesterID
							    left join semesters on semesters.id=classesSemesters.semesterID 
								left join users on users.id=students.userID
                                where classes.id="'.$classeID.'" order by students.firstName,students.lastName,students.id,semesters.id ,subjects.id,subSubjects.id,subjects.id, subjects.ordering' ;
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$i=0;
  while($data = mysqli_fetch_array($req)){
		 $studentid[$i]=$data[3]; 
		 $prenom[$i]=$data[5];
	  	 $famille[$i]=$data[6];
		 $pere[$i]=$data[32];
		$grade[$i]=$data[13];
	  $discipcoef[$i]=$data[16];	
	  $discipid[$i]=$data[18];
	  $discip[$i]=$data[20];
	  $matid[$i]=$data[21];
	  $mat[$i]=$data[22];
	  $orderm[$i]=$data[23];
	  $ctrlid[$i]=$data[28];
	  $ctrl[$i]=$data[29];
	  $cl[$i]=$data[1];
	  $clid[$i]=$data[0];
//	echo $studentid[$i]." ".$prenom[$i]." ".$famille[$i]." ".$ctrlid[$i]." ".$ctrl[$i]." ".$mat[$i]." ".$discip[$i]." ".$grade[$i]." ". $discipcoef[$i]."<br>";

	  $i=$i+1;
  }
  $i=0;
  $nb=0;
$summat=0;
$coefmat=0;
$sumcla=0;
$coeft=0;
$j=1;
$trig=0;
$num=1;
$moyensumat=0;
$conter=0;
$num1=0;

$check=0;
$conterctrl=array();
while($i<sizeof($studentid)){

//echo $studentid[$i]." ". $matid[$i]. " " .$discipid[$i]. " ". $discipcoef[$i]."<br>";
			if(($studentid[$i]==$studentid[$j])){
				         if($trig==0){
	                        echo "<tr><td width='18%'>".$num.".  ".$prenom[$i]." ".$pere[$i]." ".$famille[$i]."</td>";
						     
							 $trig=1;
							 $num++;
							 $cel1++;
							       }
				  if(($mat[$i]==$mat[$j])){
					  if($grade[$i]<>-1){
	                  $summat=$summat+$grade[$i];
					    $coefmat=$coefmat+$discipcoef[$i];
					  $summat1=$summat1+$grade[$i];
	                  $coeft=$coeft+$discipcoef[$i];	
					                   }
		                               }
		                   else
                                   { 
				 if(($grade[$i]<>-1)){
					  $summat=$summat+$grade[$i];
					  $coefmat=$coefmat+$discipcoef[$i];
					  $summat1=$summat1+$grade[$i];
					  $coeft=$coeft+$discipcoef[$i];	
				                        }
				                   
		                            } 
			                }
						
	if(($ctrlid[$i]<>$ctrlid[$j])||($studentid[$i]<>$studentid[$j]))
					{ 
				   //	if(($grade[$i]<>-1)){*/
                    
				    //  $summat1=$summat1+$grade[$i];
					//  $coeft=$coeft+$discipcoef[$i];
					if($coeft==0) $coeft=1;
					$mont[$check][$num1]=round($summat1*20/$coeft,1);
					  echo "<td align='center'><b>".round($summat1*20/$coeft,1)."</b></td>";
                         $mont[$check][$num1]=round($summat1*20/$coeft,1); 
						//  echo "mont[".$check."][".$num1."]=". $mont[$check][$num1]."<br>";
						  $check=$check+1;
					  $summat=$summat +$summat1 ; 
					  $moyensumat=$moyensumat+round($summat1*20/$coeft,1); 
					  if(round($summat1*20/$coeft,1)<>0) $conter++;
					  $summat1=0;$coeft=0;	
					
								  //   }
				    
				}
				 
				$conter1=$conter;    
	if(($studentid[$i]<>$studentid[$j])) {$trig=0;
	   while($k>$conter1) {echo "<td></td>"; $conter1++;}
	     $texte[$num1]=$prenom[$i]." ".$famille[$i];
		 if($conter==0) $conter=1;
		 $donn[$num1]=round($moyensumat/$conter,1); //echo "<td>".$summat1."</td>";
		 $num1=$num1+1;
	echo "<td>".round($moyensumat/$conter,1)."</td></tr>";$moyensumat=0;$summat=0; $conter=0;
	     $check=0;
     }
  $i=$i+1;
 $j=$j+1;	

	}
echo "</table>";
$moyencl=0;

  for($m=0;$m<$num1;$m++)
             $dataPoints1[$m] =array("y" => $mont[0][$m], "label" => $texte[$m]);
 for($m=0;$m<$num1;$m++)
             $dataPoints2[$m] =array("y" => $mont[1][$m], "label" => $texte[$m]);

echo '</div>';

}
?>
<script>
window.onload = function () {
 
var chart = new CanvasJS.Chart("chartContainer", {
	title: {
		text: "Nom des élèves"
	},
	axisY: {
		
		title: "Moyenne des Elèves"
	},
	axisX:
	{
		labelAutoFit: true,
		labelAngle: 90,
		labelFontSize: 14,
		 interval: 1,
	},
	data: [{ 
	    markerType: "square",  //"circle", "square", "cross", "none"
        markerSize: 10,
		 markerColor: "red",
        type: "line",
		name: <?php echo json_encode($sem[0], JSON_NUMERIC_CHECK); ?>,
		showInLegend: true,
    
		dataPoints: <?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?>
	}
	,{   markerType: "cross",  //"circle", "square", "cross", "none"
        markerSize: 10,
		 markerColor: "green",
        type: "line", 
				name: <?php echo json_encode($sem[1], JSON_NUMERIC_CHECK); ?>,
		showInLegend: true,
		dataPoints: <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>
	}
	
	]
}

);
chart.render();

}
</script>

<div id="chartContainer" style="height: 600px; width: 95%;"></div>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>


</body>
</html>