<?php
if(!isset($_SERVER['HTTP_REFERER'])){
    // redirect them to your desired location
    header('location:../index.php');
    exit;
}
?>
<?php 
include_once("db_connect.php"); 
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<?php include_once('../view/head.php'); ?>
<?php include_once('../view/header_teacher.php'); ?>
<?php include_once('../view/sidebar2.php'); ?>
<?php include_once('../view/alert.php'); ?>
<script type="text/javascript" src="dist/jquery.tabledit.js"></script>
<?php
include_once('../controller/config.php');
$index=$_SESSION["index_number"];
$connect = mysqli_connect("localhost", "e-learning", "E-learning@2020@ENSS", "std_db");
$anneeId = '';
$query = "SELECT anneeId  from affectation_module
inner join teacher
on affectation_module.ensignaintId=teacher.id
WHERE teacher.index_number='$index'
GROUP BY anneeId ORDER BY anneeId ASC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
 $anneeId .= '<option value="'.$row["anneeId"].'">'.$row["anneeId"].'</option>';
}
?>



<?php//*******************************************   BEGIN  *******************************************************?>
<div class="content-wrapper">
	<!-- Content Header (Page header) -->
    <section class="content-header">
    	<h1>
        	Notes par module
            <small>Aperçu</small>
        </h1>
        <ol class="breadcrumb">
        	<li><a href="#"><i class="fa fa-dashboard"></i> Accueil</a></li>
            <li><a href="#">Notes</a></li>
        </ol>
	</section>
	<!---------------------------------------------------  Afficher des notes ------------------------------------------------------------->
	<!--<h4 style="text-align:center; color:red;" class="box-title">La période de saisie des notes pour le premier semestre est terminée</h4>
	<!------------------------------------------------------------------------------------------------------------------------------------->
		<!-- Main content -->
    <section class="content">
    	<div class="row">
        	<!-- left column -->
            <div class="col-md-11">
            	<!-- general form elements -->
              	<div class="box box-primary">
                	<div class="box-header with-border">
                  		<h3 class="box-title">Note des étudiants</h3>
                	</div><!-- /.box-header -->
                  	<div class="box-body" >
                  	 	<div class="form-group" id="divGender">
                
                        
						<form>
	                        <div class="col-xs-1">
                      			<label for="exampleInputPassword1">Spécialité:</label>
                    		</div>
							
                    		<div class="col-xs-3" id="divGender1">	
                            <select name="anneeId" id="anneeId" class="form-control action">
                                       <option value="">Sélectionnez Spécialité..</option>
                                       <?php echo $anneeId; ?>
                            </select>
                            </div>
									
                       
							
							<div class="col-xs-1">
                      			<label for="exampleInputPassword1">Module:</label>
                    		</div>
							
                    		<div class="col-xs-3" id="divGender1">	
                                <select name="moduleId" id="moduleId" class="form-control action">
                                <option value="">Sélectionnez module..</option>
                                </select>
                            </div>
						   
						    
                       
							<div class="col-xs-1">
                      			<label for="exampleInputPassword1">Groupe:</label>
                    		</div>
							
							
                    		<div class="col-xs-3" id="divGender1">	
                             <select name="groupeId" id="groupeId" class="form-control">
                             <option value="">Sélectionnez groupe..</option>
                             </select>
                            </div>
							

					
<!--------------------------------------------------------------------------------------------------------------------------------------->							
                    	</div>
                  	</div><!-- /.box-body -->
					

                  	<div class="box-footer">
					    <button type="submit" value="Envoyer" class="btn btn-primary" >Afficher</button>						
                        </form>	
						
					</div>
					<div class="box-footer">	
					
            	</div><!-- /.box -->
				
        	</div>
			
							

    	</div>
	<!-- End of form section -->
	
</section>
<script type="text/javascript">
function printpage()
  {
  window.print()
  }
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
 $('.action').change(function(){
  if($(this).val() != '')
  {
   var action = $(this).attr("id");
   var query = $(this).val(); 
   var ind = "<?php echo $index; ?>";   
   var result = '';
   if(action == "anneeId")
   {
    result = 'moduleId';
   }
   else
   {
    result = 'groupeId';
   }
   $.ajax({
    //url:"fetch.php",
    url:"live_edit1.php",
    method:"POST",
    data:{action:action, query:query, ind:ind},
    success:function(data){
     $('#'+result).html(data);
    }
   })
  }
 });
});
 </script> 
<?php//******************************************  END   ********************************************************?>



<section class="content">
    	<div class="row">
        	<!-- left column -->
            <div class="col-md-11">
            	<!-- general form elements -->
              	<div class="box box-primary">
                	<div class="box-header with-border">
                  		<h3 class="box-title"></h3>
                	</div><!-- /.box-header -->
                  	<div class="box-body" >
 <div class="form-group" id="divGender4">                	 	
 <div class="table-responsive">
 
 
 
 

		

<?php 
//$moduleId1= 'ANG 40101';




if(isset($_GET['moduleId']) AND !empty($_GET['moduleId'])) //On vérifie que $_POST['moduleId'] existe et qu'il n'est pas vide
{
//******************************************************************
include_once('../controller/config.php');
$my_index= $_SESSION["index_number"];
$sql="SELECT * FROM teacher WHERE index_number='$my_index'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
$name=$row['i_name'];
$id=$row['id'];
//******************************************************************	
	
$connect = mysqli_connect("localhost", "e-learning", "E-learning@2020@ENSS", "std_db");
	$anneeId = $_GET['anneeId'] ;
	$moduleId = $_GET['moduleId'] ;
	$groupeId = $_GET['groupeId'];
	
   			$sql_query1 = "select annees.designation as anndes,annees.anneeId as AneeID,annees.speId as annspe,specialites.speId as sspeid,specialites.designation as spedes from annees,specialites
	        where  annees.speId=specialites.speId and annees.anneeId = '$anneeId'";
            $resultset1 = mysqli_query($conn, $sql_query1) or die("database error:". mysqli_error($conn));
			$row2 = mysqli_fetch_assoc($resultset1);
			//**************************************************************************************************************************************************************************
			$sql_query2 = "select * from  etudiants  , noteets ,groupes,modules
	        where etudiants.nins = noteets.nins and groupes.anneeId = etudiants.anneeId and modules.moduleId =noteets.moduleId and noteets.moduleId = '$moduleId' and etudiants.groupeId = '$groupeId'
			GROUP BY etudiants.nins ORDER BY   etudiants.nom  ASC";
           
			$resultset2 = mysqli_query($conn, $sql_query2) or die("database error:". mysqli_error($conn));
			$row1 = mysqli_fetch_assoc($resultset2);
			//**************************************************************************************************************************************************************************
			$sql_query = "select * from  etudiants  , noteets ,groupes,modules
	        where etudiants.nins = noteets.nins and groupes.anneeId = etudiants.anneeId and modules.moduleId =noteets.moduleId and noteets.moduleId = '$moduleId' and etudiants.groupeId = '$groupeId'
			GROUP BY etudiants.nins ORDER BY   etudiants.nom  ASC";
           
			$resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
			//$row = mysqli_fetch_assoc($resultset);
		 ?>	
<div align="right">
<h4 align="left"> <?php echo $row2['spedes']; echo '  -   '; echo $row2['anndes']; ?></h4> 
<h4 align="left" class="box-title"> Notes du module : <?php echo $row1['Designation'];?></h4>
<h5 align="left" class="box-title"> Enseignant:<strong><span style="color:#149414;"> <?php echo $name;?> </span></strong></h5>
<button  type="button" class="btn btn-primary"  onClick="printpage()">Imprimer les notes des étudiants</button> 
<h4></h4>
</div>	
		<table id="data_table" class="table table-bordered table-striped">
		<thead>
		
			<tr>
				<th>ID</th>
				<th>N°</th>
				<th>N° d'inscription</th>
				<th>Nom</th>
				<th>Prenom</th>
				<th style="text-align:center;">EMD1</th>	
				<th style="text-align:center;">EMD2</th>
				<?php//********************************* avoirTD *************************************************************?>
			   <?php
			   if ($row1 ['avoirTD']=="1" ) {
			   ?>
		        <th style="text-align:center;">TD</th>
			   <?php	 
	           } else {
			   ?>
		         <th width="0%" style="display:none;"></th>
				 <?php
				 }
				 ?>
			   <?php//***********************************************************************************************************?>
				<?php//******************************  avoirTP  *****************************************************************?>
			   <?php
			   if ($row1 ['avoirTP']=="1" ) {
			   ?>
		        <th style="text-align:center;">TP</th>
			   <?php	 
	           } else {
			   ?>
		         <th width="0%" style="display:none;"></th>
				 <?php
				 }
				?>
			   <?php//***********************************************************************************************************?>
				<th style="text-align:center;">SYN</th>
				<th style="text-align:center;">RATT</th>
				<th style="text-align:center;">Moyenne</th>
			</tr>
		</thead>
		<tbody>	
		
		<?php
		    $count = 0;
			while( $row = mysqli_fetch_assoc($resultset) ) {
				 $count++;
				     //$id =  $row['nins'];
					 $id = $row['nins'].";".$row['moduleId'];  # extract first and second entry of the row.
					 $nins =  $row['nins'];
                     $nom = $row['nom'];
                     $prenom = $row['prenom'];
	                 $EMD1 = $row['EMD1'];
	                 $TD1 = $row['TD1'];
	                 $EMD2 = $row['EMD2'];
	                // $TD2 = $row['TD2'];
	                 $TP = $row['TP'];
	                 $SYN = $row['SYN'];
	                 $RATT = $row['RATT'];
					 $TDD=doubleval($TD1);
					 $TPP=doubleval($TP);
					 //******************* Moyenne **********************
					 $EMDD1=doubleval($EMD1);
					 $EMDD2=doubleval($EMD2);
					 if(($row ['avoirTD']=="1" ) and ($row ['avoirTP']=="1" )) {
						 $TDD=doubleval($TD1);
						 $TPP=doubleval($TP);
		                 $CC=( $TDD +  ($TPP)*2)/3;
					     $CA=doubleval($CC);
					     $MOY1 =  $EMDD1 +  $EMDD2 + $CA;
						 $MOY =  ($MOY1/3) ;
	                 } else 
						 if(($row ['avoirTD']=="1" ) and ($row ['avoirTP']=="0" )) {
		                  $CC= doubleval($TD1); 
						  $CA=doubleval($CC);
		                  $MOY1 =  $EMDD1 +  $EMDD2 + $CA;
						  $MOY =  ($MOY1/3) ;
	                    } else 
							if(($row ['avoirTD']=="0" ) and ($row ['avoirTP']=="1" )) {
		                       $CC= doubleval($TP);
							   $CA=doubleval($CC);
							   $MOY1 =  $EMDD1 +  $EMDD2 + $CA;
						       $MOY =  ($MOY1/3) ;
	                        }else {
								if(($row ['avoirTD']=="0" ) and ($row ['avoirTP']=="0" ))
								$MOY1 =  $EMDD1 +  $EMDD2;
						        $MOY =  ($MOY1/2) ;
			                    }					 
					 
                     $MOY = number_format($MOY, 2);
					 //*************************************************
					 	
					  $avoirTD =	$row['avoirTD'];
					  $avoirTP =	$row['avoirTP'];
			?>
	
            <tr >
		       <td><?php echo $id; ?></td>
			   <td><?php echo $count; ?></td>
			   <td><?php echo $row['nins']?></td>
			   
			   
			   <td>
			   <a href="#modalViewform1"  onClick="showModal1(this)" class=""  data-id="<?php echo $row['nins'];echo ';'; echo $row['moduleId']; ?>" data-toggle="modal">
			   <?php echo $row['nom']; ?>
               </a>
			   </td>
			   
			    <td>
			   <a href="#modalViewform1"  onClick="showModal1(this)" class=""  data-id="<?php echo $row['nins'];echo ';'; echo $row['moduleId']; ?>" data-toggle="modal">
			   <?php echo $row['prenom']; ?>
               </a>
			   </td>
			   
			   
			   <?php//*****************************************  EMD1 **********************************************************?>
			   <?php
			   if ($EMDD1 > "0" ) {
			   ?>
		             <td  style="text-align:center; background-color:#90EE90;"><?php echo number_format(( $row ['EMD1']), 2, '.', ' '); ?></td> 
			   <?php	 
	           } else {
			   ?>
		            <td style="text-align:center;"><?php echo number_format(( $row ['EMD1']), 2, '.', ' '); ?></td>
			   <?php
	                                    } 
			   ?> 
			   <?php//***********************************************************************************************************?>
			    <?php//**************************************** EMD2 ***********************************************************?>
			   <?php
			   if ($EMDD2 > "0" ) {
			   ?>
		             <td style="text-align:center; background-color:#90EE90;"><?php echo number_format(( $row ['EMD2']), 2, '.', ' '); ?></td> 
			   <?php	 
	           } else {
			   ?>
		             <td style="text-align:center;"><?php echo number_format(( $row ['EMD2']), 2, '.', ' '); ?></td>
			   <?php
	                                    } 
			   ?> 
			   <?php//***********************************************************************************************************?>
			   	<?php//***********************************************************************************************************?>
			   <?php
			   if ($avoirTD =="1" ) {
			   ?>
			   
			   <?php//**************************************** TD ***********************************************************?>
			   <?php
			   if ($TDD > "0" ) {
			   ?>
		             <td style="text-align:center; background-color:#90EE90;"><?php echo number_format(( $row ['TD1']), 2, '.', ' '); ?></td> 
			   <?php	 
	           } else {
			   ?>
		             <td style="text-align:center;"><?php echo number_format(( $row ['TD1']), 2, '.', ' '); ?></td>
			   <?php
	                                    } 
			   ?> 
			   <?php//***********************************************************************************************************?>
 
			   <?php	 
	           } else {
			   ?>
		             <td width="0%" style="display:none;"></td>
			   <?php
	                                    } 
			   ?> 
			   <?php//***********************************************************************************************************?>
			   
			   
			   <?php
			   if ($avoirTP =="1" ) {
			   ?>
			    <?php//**************************************** TP ***********************************************************?>
			   <?php
			   if ($TPP > "0" ) {
			   ?>
		             <td style="text-align:center; background-color:#90EE90;"><?php echo number_format(( $row ['TP']), 2, '.', ' '); ?></td> 
			   <?php	 
	           } else {
			   ?>
		             <td style="text-align:center;"><?php echo number_format(( $row ['TP']), 2, '.', ' '); ?></td>
			   <?php
	                                    } 
			   ?> 
			   <?php//***********************************************************************************************************?>
			   <?php	 
	           } else {
			   ?>
		             <td width="0%" style="display:none;"></td>
			   <?php
	                                    } 
			   ?> 
			   
			 
			   <?php//*****************************************************************************************************?>

			   <?php
			 if ($MOY < 10 ) {
			  ?>
		             <td style="text-align:center;"><?php echo number_format(( $row ['SYN']), 2, '.', ' '); ?></td>
                     <?php					 
					 $SYN=doubleval($row ['SYN']);
					//$MOY=$SYN;
			               if ($SYN < 10 ) {
			               ?>
		                   <td style="text-align:center;"><?php echo number_format(( $row ['RATT']), 2, '.', ' '); ?></td> 
			               <?php	
						  // $MOY=$RATT;
	                       } else {
			               ?>
					       <td style="background-color:grey" ></td>
			               <?php
	                                    }       
	           } else {
			   ?>
		             <td style="background-color:grey" ></td>
					 <td style="background-color:grey" ></td>
			   <?php
	                                    } 
			   ?> 
			   <?php//***********************************************************************************************************?>
			  
			   <td style="text-align:center;"><?php echo '/'; ?></td>			  			    			   
		    </tr>

<?php } ?>



   
<?php

}
//****************************************************************************

 ?>
		</tbody>
		
    </table>
	<?php //<td><a class="btn btn-primary" href="export_excel.php">Enregistrer sous Excel</a></td>?>
</div>
</div>


</div>
</div>	
</section>
</div><!-- /.content-wrapper -->  








 
<?php

//****************************************************************************************************************************************************************************************
include_once("db_connect.php");
$input = filter_input_array(INPUT_POST);
$id_ser = $input["id"];
$id_array = preg_split("/;/",$id_ser);

if ($input['action'] == 'edit') {	
	$update_field='';
	if(isset($input['nom'])) {
		$update_field.= "nom='".$input['nom']."'";
	} else if(isset($input['prenom'])) {
		$update_field.= "prenom='".$input['prenom']."'";	
	} else if(isset($input['EMD1'])) {
		//-------------------------------------------------
		if (floatval($input['EMD1'])> 20){	 
            $update_field.= "EMD1='00'";
		}else {
		    $update_field.= "EMD1='".$input['EMD1']."'";
		}
		//--------------------------------------------------
	} else if(isset($input['EMD2'])) {
		
		//-------------------------------------------------
		if (floatval($input['EMD2'])> 20){	 
            $update_field.= "EMD2='00'";
		}else {
		    $update_field.= "EMD2='".$input['EMD2']."'";
		}
		//--------------------------------------------------
			
	} else if(isset($input['TD1'])) {
		
		//-------------------------------------------------
		if (floatval($input['TD1'])> 20){	 
            $update_field.= "TD1='00'";
		}else {
		    $update_field.= "TD1='".$input['TD1']."'";
		}
		//--------------------------------------------------
			
	}else if(isset($input['TP'])) {
		
		//--------------------------------------------------
		if (floatval($input['TP'])> 20){	 
            $update_field.= "TP='00'";
		}else {
		$update_field.= "TP='".$input['TP']."'";
		}
		//--------------------------------------------------
	
	} else if(isset($input['SYN'])) {
		
		//--------------------------------------------------
		if (floatval($input['SYN'])> 20){	 
            $update_field.= "SYN='00'";
		}else {
		$update_field.= "SYN='".$input['SYN']."'";
		}
		//--------------------------------------------------
		
	} else if(isset($input['RATT'])) {
		
		//--------------------------------------------------
		if (floatval($input['RATT'])> 20){	 
            $update_field.= "RATT='00'";
		}else {
		$update_field.= "RATT='".$input['RATT']."'";
		}
		//--------------------------------------------------
		
	}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
if($update_field && $input['id'] ) {

	
		   $sql_query = "UPDATE noteets SET $update_field WHERE   nins= '".$id_array[0]."' and  moduleId= '".$id_array[1]."'"; 
		    mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));	

	 		
}

}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 	
?> 
<script type="text/javascript" >

$(document).ready(function(){
	$('#data_table').Tabledit({
		deleteButton: false,
		editButton: false, 
//****************************************************
           eventType: 'click',
//***************************************************  		
		columns: {
		  identifier: [0, 'id'],                    
		//editable: [[1, 'nins'],[2, 'nom'], [3, 'prenom'], [4, 'EMD1'], [5, 'EMD2'], [6, 'TD1'], [7, 'TP'], [8, 'SYN'], [9, 'RATT']]

		editable: [ [5, 'EMD1'], [6, 'EMD2'], [7, 'TD1'], [8, 'TP'], [9, 'SYN'], [10, 'RATT']]
		
		//editable: [ [5, 'EMD1'], [7, 'TD1'], [8, 'TP']]
		// pour autoriser insersion des notes
		// editable: [ [5, 'EMD1']]
		},
		hideIdentifier: true,
		url: 'notes_module1.php'		
	});
});

</script>
<script>
function showModal1(Viewform){
	var Id = $(Viewform).data("id"); 	
	var path = "../"; 
	
	var xhttp = new XMLHttpRequest();//MSK-00149-Start Ajax  
  		xhttp.onreadystatechange = function() {
    		if (this.readyState == 4 && this.status == 200) {
				//MSK-00150
				var myArray1 = eval( xhttp.responseText );
				
				document.getElementById("nin2").innerHTML =myArray1[0];
				document.getElementById("nom2").innerHTML =myArray1[1];
				document.getElementById("prenom2").innerHTML =myArray1[2];
				document.getElementById("photo2").src ="../"+myArray1[3];	
                document.getElementById("countnj2").innerHTML =myArray1[4];
				document.getElementById("countju2").innerHTML =myArray1[5];
				document.getElementById("count2").innerHTML =myArray1[6];
				
				$('#id_Edit').data('id',myArray1[0]);
				$('#id_Delete').data('id',myArray1[0]);
    		}
			
  		};	
    xhttp.open("GET", "../model/get_myStuAtt.php?id=" + Id , true);												
  	xhttp.send();	 
};
</script>
<?php
//*************************************************************************   pour afficher les informations des absences *************************************************************

 ?> 	  	
	<div class="modal msk-fade" id="modalViewform1" tabindex="-1" role="dialog" aria-labelledby="insert_alert1" aria-hidden="true" data-backdrop="static" data-keyboard="false">
		<div class="modal-dialog"><!--modal-dialog -->  
			<div class="container col-lg-12 "><!--modal-content --> 
      			<div class="row">
                    <div class="panel panel-info"><!--panel --> 
                        <div class="panel-heading">
                        	 <button type="button"  class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
                            <h3 class="panel-title" > liste des absences</h3>
                        </div>
                        <div class="panel-body"><!--panel-body -->
                            <div class="row">
                                <div class="col-md-3"> 
                                	<img id="photo2" alt="User Pic" src="" class="img-circle img-responsive"> 
                                </div>
                               <div class=" col-md-9"> 
                                    <table class="table table-user-information">
                                        <tbody>
                                            
											<tr>
                                                <th>N° d'inscription:</th>
                                                <td id="nin2"> </td>
                                            </tr>
                                            <tr>
                                                <th>Nom et Prénom:</th>
                                                <td id="nom2"></td>
												<td id="prenom2"></td>
                                            </tr>

											<tr>
                                                <th>nbr des absences non justifiées:</th>
                                                <td id="countnj2"> </td>
                                            </tr>
											
                                            <tr>
                                                <th>nbr des absences justifiées:</th>
                                                <td id="countju2"> </td>
                                            </tr>

											<tr>
                                                <th>Nombre total des absences:</th>
                                                <td id="count2"> </td>
                                            </tr>

                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div><!--/.panel-body -->
                        <div class="panel-footer text-right"><!-- panel-footer-->
                           <span class="pull-right"> </span>
                        </div><!--/. panel-footer-->
                   </div><!--/. panel--> 
                </div><!--/.row --> 
            </div><!--/.modal-content -->
        </div><!--/.modal-dialog -->
    </div><!--/.modal -->      	


<?php
//**************************************************************************************************************************************************************************************
?>
 
 
 
 
<script>
(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/history");
history.pushState(null, document.title, location.pathname);

window.addEventListener("popstate", function() {
  if(location.hash === "#!/history") {
    history.replaceState(null, document.title, location.pathname);
    setTimeout(function(){
      location.replace("../index.php");//path to when click back button
    },0);
  }
}, false);
}(window, location));
 </script>
 <?php include_once('../view/footer.php');?>  
 </div><!-- /.content-wrapper -->    
 