sid/manager/repo/semestre.php

614 lines
22 KiB
PHP
Executable File

<?php require_once __ROOT__.'/manager/security.php';
/**************************************************************************************/
/* ____ _____ __ __ _____ ____ _____ ____ _____ ____ _____ ____ ___ */
/* / ___|| ____| \/ | ____/ ___|_ _| _ \| ____| | _ \| ____| _ \ / _ \ */
/* \___ \| _| | |\/| | _| \___ \ | | | |_) | _| | |_) | _| | |_) | | | | */
/* ___) | |___| | | | |___ ___) || | | _ <| |___ | _ <| |___| __/| |_| | */
/* |____/|_____|_| |_|_____|____/ |_| |_| \_\_____| |_| \_\_____|_| \___/ */
/* */
/**************************************************************************************/
/*
* CONTENU DU REPO
*
* [1] UID(formation, rang, annee)
* retourne l'UID d'un semestre pour un rang et une année donnée
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
class semestreRepo{
/* VERIFIE L'EXISTENCE D'UN SEMESTRE DANS LA BDD
*
* @formation<int> l'UID de la formation en question
* @rang<int> le rang du semestre dans le parcours (1<=>S1, 2<=>S2, ..)
* @annee<int> l'année du début du parcours
*
*
* @return UID<int> si le semestre est dans la BDD, retourne son UID (auto_incr)
* @return FALSE<Boolean> FAUX si aucun semestre avec ce rang et annee n'est pas présent dans la BDD
*
*/
public static function UID($formation, $rang, $annee){
/*** on cherche un semestre avec ce rang et cette année (qui est forcément unique) ***/
$getSemestreUID = DataBase::getPDO()->prepare("SELECT s.id_semestre as id
FROM semestre as s, formation as f
WHERE s.id_formation = f.id_formation
AND s.id_formation = :formation
AND s.rang = :rang
AND s.annee = :annee");
$getSemestreUID->execute(array(
':formation' => $formation,
':rang' => $rang,
':annee' => $annee
));
// on retourne le résultat : FAUX si aucun résultat, sinon la valeur de l'UID du semestre
return $getSemestreUID->fetch()['id'];
}
/* VERIFIE L'EXISTENCE D'UN SEMESTRE DANS LA BDD PAR SON ID_UNIQUE
*
* @semestreUID<int> l'UID du semestre en question
*
*
* @return exists<Boolean> VRAI si un semestre avec cet id existe
*
*/
public static function exists($semestreUID){
// on cherche si un semestre existe avec cet UID
$getSemestreUID = DataBase::getPDO()->prepare("SELECT id_semestre
FROM semestre
WHERE id_semestre = :semestreUID");
$getSemestreUID->execute(array(
':semestreUID' => $semestreUID
));
// on retourne si OUI/NON le semestre existe
return $getSemestreUID->fetch() !== FALSE;
}
/* retourne l'UID d'une formation et la créé en amont si elle n'existe pas
*
* @code<String> Le code de la formation
* @nom<String> Le nom de la formation
*
*
* @return UID<int> l'UID de la formation en question
*
*/
public static function includeFormation($code, $nom){
/* [1] On vérifie l'existence de la formation
======================================================*/
$getFormationUID = DataBase::getPDO()->prepare("SELECT id_formation as id
FROM formation
WHERE code = :code
AND nom = :nom");
$getFormationUID->execute(array( ':code' => $code, ':nom' => $nom ));
/* [2] Cas où la formation existe déjà, alors on retourne l'UID
======================================================*/
if( $formationUID = $getFormationUID->fetch()['id'] ) return $formationUID;
/* [3] Cas où la formation n'existe pas, alors on la créé
======================================================*/
$creerFormation = DataBase::getPDO()->prepare("INSERT INTO formation(id_formation, code, nom, nb_semestres)
VALUES(DEFAULT, :code, :nom, DEFAULT)");
$creerFormation->execute(array( ':code' => $code, ':nom' => $nom ));
/* [4] On vérifie que la formation a été créé et retourne son UID
======================================================*/
$getFormationUID = DataBase::getPDO()->prepare("SELECT id_formation as id
FROM formation
WHERE code = :code
AND nom = :nom");
$getFormationUID->execute(array( ':code' => $code, ':nom' => $nom ));
return $getFormationUID->fetch()['id'];
}
/* retourne l'UID d'un semestre le créé en amont si il n'existe pas
*
* @rang<int> Le rang du semestre en question
* @annee<int> L'année du semestre en question
* @formation<int> L'UID de la formation en question
*
*
* @return UID<int> l'UID du semestre en question
*
*/
public static function includeSemestre($rang, $annee, $formation){
/* [1] On vérifie l'existence du semestre
======================================================*/
$getSemestreUID = DataBase::getPDO()->prepare("SELECT id_semestre as id
FROM semestre
WHERE rang = :rang
AND annee = :annee
AND id_formation = :formation");
$getSemestreUID->execute(array( ':rang' => $rang, ':annee' => $annee, ':formation' => $formation ));
/* [2] Cas où le semestre existe déjà, alors on retourne l'UID
======================================================*/
if( $semestreUID = $getSemestreUID->fetch()['id'] ) return $semestreUID;
/* [3] Cas où le semestre n'existe pas, alors on le créé
======================================================*/
$creerSemestre = DataBase::getPDO()->prepare("INSERT INTO semestre(id_semestre, id_formation, nom, rang, annee)
VALUES(DEFAULT, :formation, :nom, :rang, :annee)"); // par défaut le nom du semestre = "Sn" où n est le rang
$creerSemestre->execute(array( ':formation' => $formation, ':nom' => 'S'.$rang, ':rang' => $rang, ':annee' => $annee ));
/* [4] On vérifie que la formation a été créé et retourne son UID
======================================================*/
$getSemestreUID = DataBase::getPDO()->prepare("SELECT id_semestre as id
FROM semestre
WHERE rang = :rang
AND annee = :annee
AND id_formation = :formation");
$getSemestreUID->execute(array( ':rang' => $rang, ':annee' => $annee, ':formation' => $formation ));
return $getSemestreUID->fetch()['id'];
}
/* RENVOIE LES INFORMATIONS D'UN SEMESTRE D'UID DONNÉ
*
* @semestreUID<int> l'UID du semestre duquel on veut les infos
*
* @return semestre<Array> tableau associatif contenant tout les champs de la BDD pour ce semestre
*
*/
public static function info($semestreUID){
// on considère que le semestre existe
$getSemestreInfo = DataBase::getPDO()->prepare("SELECT s.id_semestre as id, s.nom, s.rang, s.annee, f.id_formation, f.code as formation, f.nom as nom_formation
FROM semestre as s, formation as f
WHERE s.id_formation = f.id_formation
AND s.id_semestre = :semestreUID");
$getSemestreInfo->execute(array( ':semestreUID' => $semestreUID ));
// on retourne le résultat en supprimant les doublons à indices numériques
return $getSemestreInfo->fetch();
}
/* RENVOIE LA LISTE DES ANNÉES PRÉSENTES DANS LA BDD
*
* @return annees<Array> retourne la liste des années contenues dans la bdd
* @return FALSE<Boolean> retourne FALSE si aucune année n'est trouvée
*
*/
public static function getAnnees(){
$getAnneesList = DataBase::getPDO()->query("SELECT DISTINCT annee FROM semestre ORDER BY annee DESC");
return DataBase::delNumeric( $getAnneesList->fetchAll() );
}
/* retourne le semestre courant d'un étudiant
*
* @etudiant<String> l'identifiant (UID) de l'étudiant en question
* @semestre_pair<Boolean> VRAI si le semestre courant est pair
* @annee<String> l'année en cours
*
* @return etudie<Boolean> FALSE si l'étudiant n'est dans aucun semestre en cours
* @return semestre<Array> retourne les données relatives au semestre
*
*/
public static function forStudent($etudiant, $semestre_pair, $annee){
// on formate les variables
$semestre_pair = ($semestre_pair) ? '0' : '1';
$getSemestreInfos = DataBase::getPDO()->prepare("SELECT s.id_semestre as id, s.rang as rang, f.id_formation, f.code as formation, f.nom as nom_formation
FROM semestre as s, appartenance as app, formation as f
WHERE s.id_formation = f.id_formation
AND app.id_semestre = s.id_semestre
AND app.id_etudiant = :etudiant
AND s.annee = :annee
AND s.rang % 2 = :semestre_pair
ORDER BY s.rang DESC");
$getSemestreInfos->execute(array(
':etudiant' => $etudiant,
':annee' => $annee,
':semestre_pair' => $semestre_pair
));
// on retourne les infos du semestre courant
return $getSemestreInfos->fetch();
}
/* retourne le parcours d'un étudiant sous forme d'un tableau de semestres
*
* @etudiant<String> l'UID de l'étudiant en question
*
*
* @return semestres<Array> retourne la liste des semestres du parcours de l'étudiant (ordre chronologique)
*
*/
public static function getParcours($etudiant){
$getSemestreListe = DataBase::getPDO()->prepare("SELECT DISTINCT s.id_semestre as id, f.id_formation, f.code, f.nom as formation, f.nb_semestres, s.nom as semestre, s.rang, s.annee, app.mention, app.inactive
FROM formation as f, semestre as s, appartenance as app
WHERE s.id_formation = f.id_formation
AND app.id_semestre = s.id_semestre
AND app.id_etudiant = :etudiant
ORDER BY s.annee, s.rang");
$getSemestreListe->execute(array( ':etudiant' => $etudiant ));
return DataBase::delNumeric( $getSemestreListe->fetchAll() );
}
/* retourne la liste des semestres qu'à un enseignant
*
* @enseignant<String> l'UID de l'enseignant en question
* @semestre_pair<Boolean> VRAI si le semestre en cours est pair
* @annee<int> l'année en question
*
*
* @return semestres<Array> retourne la liste des semestres répondant aux critères
* @return FALSE<Boolean> retourne FALSE si aucun semestre ne correspond aux critères
*
*/
public static function forTeacher($enseignant, $semestre_pair, $annee){
$semestrePair0 = '0'; $semestrePair1 = '1';
if( is_bool($semestre_pair) ){ $semestrePair0 = ($semestre_pair)?'0':'1'; $semestrePair1 = ($semestre_pair)?'0':'1'; }
$getSemestreList = DataBase::getPDO()->prepare("SELECT DISTINCT s.id_semestre as id, f.id_formation, f.code as formation, f.nom as nom_formation, s.nom, s.rang, s.annee
FROM semestre as s, formation as f, enseignement as ens, mcc_module as mcc_m, mcc_ue
WHERE s.id_semestre = mcc_ue.id_semestre
AND s.id_formation = f.id_formation
AND mcc_ue.id_mcc_ue = mcc_m.id_mcc_ue
AND mcc_m.id_mcc_module = ens.id_mcc_module
AND ens.id_enseignant = :enseignant
AND (s.rang % 2 = :semestre_pair0 OR s.rang % 2 = :semestre_pair1)
AND s.annee = :annee
ORDER BY s.rang ASC");
$getSemestreList->execute(array( ':enseignant' => $enseignant, ':semestre_pair0' => $semestrePair0, ':semestre_pair1' => $semestrePair1, ':annee' => $annee ));
return DataBase::delNumeric( $getSemestreList->fetchAll() );
}
/* retourne la liste des semestres d'une année
*
* @annee<int> l'année en question
*
*
* @return semestres<Array> retourne la liste des semestres de cette année
* @return FALSE<Boolean> retourne FALSE si aucun semestre ne correspond aux critères
*
*/
public static function forYear($annee){
$getSemestreList = DataBase::getPDO()->prepare("SELECT DISTINCT s.id_semestre as id, f.id_formation, f.code as formation, f.nom as nom_formation, s.nom, s.rang, s.annee
FROM semestre as s, formation as f
WHERE s.id_formation = f.id_formation
AND annee = :annee
ORDER BY f.nom, annee, rang ASC");
$getSemestreList->execute(array( ':annee' => $annee ));
return DataBase::delNumeric( $getSemestreList->fetchAll() );
}
/* RETOURNE LE NOMBRE D'ÉLÈVES D'UN SEMESTRE
*
* @semestre<int> l'UID du semestre en question
*
*
* @return nbEtu<int> retourne le nombre d'étudiants du semestre
*
*/
public static function nbEtuForSemestre($semestre){
$getNbEtu = DataBase::getPDO()->prepare("SELECT count(distinct id_appartenance) as nb_etudiants
FROM appartenance
WHERE id_semestre = :semestre");
$getNbEtu->execute(array( ':semestre' => $semestre ));
if( !($nb = $getNbEtu->fetch()) )
return 0; // si on en trouve pas, on retourne 0
else
return (int) $nb['nb_etudiants']; // sinon on retourne le nombre
}
/* retourne l'UID d'un semestre s'il existe sinon créé tout ce qui a besoin et on retourne l'UID
*
* @codeFormation<String> le code/nom de la formation
* @nomFormation<String> le libellé de la formation
* @nomSemestre<String> le nom/libellé du semestre
* @rangSemestre<int> le rang du semestre dans la formation
* @annee<int> l'année du semestre
*
*
* @return UID<int> retourne l'UID du semestre si tout s'est bien passé
* @return FALSE<Boolean> retourne FALSE s'il y a eu une erreur
*
*/
public static function creer($codeFormation, $nomFormation, $nomSemestre, $rangSemestre, $annee){
if( $nomSemestre == null ) // si le nom n'est pas défini, on met "S" collé au rang
$nomSemestre = 'S'.$rangSemestre;
$count = 0; $maxLoop = 2;
/* [1] On vérifie l'existence de la formation (code uniquement)
====================================================================================*/
$getFormationUID = DataBase::getPDO()->prepare("SELECT f.id_formation as id
FROM formation as f
WHERE f.code = :code");
$getFormationUID->execute(array( ':code' => $codeFormation ));
while( !($formationUID=$getFormationUID->fetch()['id']) ){
/* [2] on créé la formation car elle n'existe pas
================================================================================*/
$creerFormation = DataBase::getPDO()->prepare("INSERT INTO formation(id_formation, code, nom, nb_semestres)
VALUES(DEFAULT, :code, :nom, 2)");
$creerFormation->execute(array( ':code' => $codeFormation, ':nom' => $nomFormation ));
if( $count >= $maxLoop ) return false;
$count++;
$getFormationUID = DataBase::getPDO()->prepare("SELECT f.id_formation as id
FROM formation as f
WHERE f.code = :code");
$getFormationUID->execute(array( ':code' => $codeFormation ));
}
/* [3] On vérifie l'existence du semestre (rang, annee, nom, formation)
====================================================================================*/
$getSemestreUID = DataBase::getPDO()->prepare("SELECT s.id_semestre as id
FROM semestre as s, formation as f
WHERE s.id_formation = f.id_formation
AND f.id_formation = :formationUID
AND s.nom = :nom
AND s.rang = :rang
AND s.annee = :annee");
$getSemestreUID->execute(array( ':formationUID' => $formationUID, ':nom' => $nomSemestre, ':rang' => $rangSemestre, ':annee' => $annee ));
while( !($semestreUID = $getSemestreUID->fetch()['id']) ){
/* [4] on créé le semestre car il n'existe pas
================================================================================*/
$creerSemestre = DataBase::getPDO()->prepare("INSERT INTO semestre(id_semestre, id_formation, nom, rang, annee)
VALUES(DEFAULT, :formationUID, :nom, :rang, :annee)");
$creerSemestre->execute(array( ':formationUID' => $formationUID, ':nom' => $nomSemestre, ':rang' => $rangSemestre, ':annee' => $annee ));
if( $count >= $maxLoop ) return false;
$count++;
$getSemestreUID = DataBase::getPDO()->prepare("SELECT s.id_semestre as id
FROM semestre as s, formation as f
WHERE s.id_formation = f.id_formation
AND f.id_formation = :formationUID
AND s.nom = :nom
AND s.rang = :rang
AND s.annee = :annee");
$getSemestreUID->execute(array( ':formationUID' => $formationUID, ':nom' => $nomSemestre, ':rang' => $rangSemestre, ':annee' => $annee ));
}
return $semestreUID;
}
/* RETOURNE LA DERNIERE ANNEE AYANT UN MCC
*
* @return annee<int> Retourne l'année en question
*
*/
public static function getLastMCCYear(){
$getLastMccYear = DataBase::getPDO()->query("SELECT DISTINCT max(s.annee) as annee
FROM semestre as s, mcc_ue
GROUP BY s.annee");
return $getLastMccYear->fetch();
}
/* RETOURNE LE SEMESTRE SUIVANT UN SEMESTRE
*
* @semestre<int> UID du semestre source
*
*
* @return next_semestre<int> Retourne l'UID du semestre de destination
* Retourne NULL si c'est le dernier semestre de la formation
*
*/
public static function next($semestreUID){
/* [1] On récupère les informations du semestre
=====================================================*/
$getSemestreInfo = DataBase::getPDO()->prepare("SELECT id_semestre, rang, id_formation, annee
FROM semestre
WHERE id_semestre = :semestreUID");
$getSemestreInfo->execute(array( ':semestreUID' => $semestreUID ));
// si le semestre n'existe pas, on retourne une erreur
if( !($semestre=$getSemestreInfo->fetch()) ) return 'unknown_semestre';
/* [2] On calcule le rang+annee du semestre suivant
=====================================================*/
$next = array(
'rang' => $semestre['rang']+1, // on incrémente le rang
'annee' => ($semestre['rang']%2==0) ? $semestre['annee']+1 : $semestre['annee']
);
/* [3] On récupère le nombre de semestres max de la formation
=====================================================*/
$getNbSemestreFormation = DataBase::getPDO()->prepare("SELECT nb_semestres
FROM formation
WHERE id_formation = :last_formation");
$getNbSemestreFormation->execute(array( ':last_formation' => $semestre['id_formation'] ));
$nbSemestres = $getNbSemestreFormation->fetch()['nb_semestres'];
// si c'était le dernier semestre, on retourne null
if( $semestre['rang'] == $nbSemestres ) return null;
/* [3] On cherche le semestre suivant
=====================================================*/
$req = DataBase::getPDO()->prepare("SELECT id_semestre
FROM semestre
WHERE id_formation = :id_formation
AND rang = :next_rang
AND annee = :next_annee
");
$req->execute(array(
':id_formation' => $semestre['id_formation'],
':next_rang' => $next['rang'],
':next_annee' => $next['annee']
));
$nextSemestres = DataBase::delNumeric( $req->fetchAll() );
/* [4] On retourne l'UID du semestre concerné
=====================================================*/
if( count($nextSemestres) > 0 ) // si au moins un semestre, on retourne le premier
return $nextSemestres[0]['id_semestre'];
else // si aucun semestre trouvé, il faut le créer, on retourne le rang et l'année de destination
return $next;
}
/* RETOURNE LE SEMESTRE PRÉCÉDENT UN SEMESTRE
*
* @semestre<int> UID du semestre source
*
*
* @return prev_semestre<int> Retourne l'UID du semestre de destination
* Retourne NULL si le semestre destination n'existe pas
*
*/
public static function prev($semestreUID){
/* [1] On récupère les informations du semestre
=====================================================*/
$getSemestreInfo = DataBase::getPDO()->prepare("SELECT id_semestre, rang, id_formation, annee
FROM semestre
WHERE id_semestre = :semestreUID");
$getSemestreInfo->execute(array( ':semestreUID' => $semestreUID ));
// si le semestre n'existe pas, on retourne une erreur
if( !($semestre=$getSemestreInfo->fetch()) ) return 'unknown_semestre';
// si le rang est impair, on retourne une erreur
if( $semestre['rang']%2==1 ) return 'not_even_rang';
/* [2] On calcule le rang+annee du semestre suivant
=====================================================*/
$prev = array(
'rang' => $semestre['rang']-1, // on décrémente le rang
'annee' => $semestre['annee']+1 // on incrémente l'année
);
/* [3] On cherche le semestre suivant
=====================================================*/
$req = DataBase::getPDO()->prepare("SELECT id_semestre
FROM semestre
WHERE id_formation = :id_formation
AND rang = :prev_rang
AND annee = :prev_annee
");
$req->execute(array(
':id_formation' => $semestre['id_formation'],
':prev_rang' => $prev['rang'],
':prev_annee' => $prev['annee']
));
$prevSemestres = DataBase::delNumeric( $req->fetchAll() );
/* [4] On retourne l'UID du semestre concerné
=====================================================*/
if( count($prevSemestres) > 0 ) // si au moins un semestre, on retourne le premier
return $prevSemestres[0]['id_semestre'];
else // si aucun semestre trouvé, il faut le créer, on retourne le rang et l'année de destination
return $prev;
}
/* RETOURNE LES SEMESTRES EQUIVALENTS (REDOUBLÉS)
*
* @semestre<int> UID du semestre de base
* @etudiant<String> UID de l'étudiant en question
*
*
* @return redoublements<Array> Retourne un tableau contenant les UIDs des semestre équivalents
* @return error<Boolean> Retourne FAUX si une erreur occure
*
*/
public static function getRedoublements($semestre, $etudiant){
/* [1] On récupère les informations du semestre
=================================================*/
$getSemestreInfo = DataBase::getPDO()->prepare("SELECT DISTINCT s.*
FROM semestre as s, appartenance as app
WHERE s.id_semestre = app.id_semestre
AND s.id_semestre = :semestre
AND app.id_etudiant = :etudiant");
$getSemestreInfo->execute(array( ':semestre' => $semestre, ':etudiant' => $etudiant ));
// aucun résultat, on retourne une erreur
if( !($semestreInfo=$getSemestreInfo->fetch()) ) return 'unknown_semestre';
/* [2] On cherche les semestre redoublés
=================================================*/
$getRedoublements = DataBase::getPDO()->prepare("SELECT s.id_semestre
FROM semestre as s, appartenance as app
WHERE s.id_semestre = app.id_semestre
AND s.id_formation = :formation
AND s.rang = :rang
AND app.id_etudiant = :etudiant
AND s.id_semestre <> :semestre
ORDER BY s.annee, s.rang ASC");
$getRedoublements->execute(array(
':formation' => $semestreInfo['id_formation'],
':rang' => $semestreInfo['rang'],
':etudiant' => $etudiant,
':semestre' => $semestre
));
return $getRedoublements->fetchAll();
}
}