<?php
require_once 'include/db_connect.php';
$com = new DbConnect();	
include_once 'sendnotification.php';

$annee = $_POST['annee'];
$niveau = $_POST['niveau'];
$idFiliere = $_POST['filiere'];
$academique = $_POST['academique'];

//echo "annee = $annee; niveau = $niveau; idFiliere = $idFiliere";
$message = $_POST['message'];
$sujet = $_POST['sujet'];
$matiere = $_POST['matiere'];
$val = "aaaa";
if($_POST['matiere']){
	$val = "";
	foreach($matiere as $valeur) {
		$val .= $valeur;
		$val .= "=";
		//echo "$valeur<br>";
	}
	$n=strrpos($val,"=");
	$val=substr_replace($val,"",$n,1);	
}

$sql1 = "select matricule  from tabetudiant where idFiliere = '$idFiliere' and annee = '$annee' and 
niveauFormation = '$niveau' and annee_acad = '$academique'";
$result1 = mysqli_query($com->getDb(), $sql1);	
$serverObject = new SendNotification();	
while($tab = mysqli_fetch_array($result1, MYSQL_ASSOC)){
	$matricule = $tab['matricule'];
	$sql2 = "select token  from firebase where matricule = '$matricule' and idFiliere = '$idFiliere'";
	$result2 = mysqli_query($com->getDb(), $sql2);	
	$nb = mysqli_num_rows($result2);
	if($nb != 0) {
		$tab2 = mysqli_fetch_array($result2, MYSQL_ASSOC);
		$token = $tab2['token'];
		$jsonString = $serverObject->sendPushNotificationToGCMSever($token, $message, $sujet, $val);
		$jsonObject = json_decode($jsonString);	
	}			
}
include_once 'success.php';
?>