The code of the UE containing the TD * @idProf [OPT] The ID of the prof who teaches the TD * @volume The number of hours (float) * @formations The list of formations for that TD (list of int) * @return created_code Code of the created UE (NULL on error) * * @return created_id UID of the created TD (NULL on error) * ---------------------------------------------------------*/ public function create(string $code, ?int $idProf, float $volume, array $formations) : int{ /* (1) Prepare statement */ $st = $this->pdo->prepare('INSERT INTO TD(UE_code, Professeur_idProfesseur, volume) VALUE(:UE, :idProf, :vol)'); /* (2) Manage statement error */ if( is_bool($st) ) return NULL; /* (3) Try to execute request */ $success = $st->execute([ ':UE' => $code, ':idProf' => $idProf, ':vol' => $volume ]); /* (4) Manage error */ if( !$success ) return NULL; /* (5) Store @created_id */ $created_id = (int) $this->pdo->lastInsertId(); /* (6) We are done if there is no formations */ if( count($formations) <= 0) return $created_id; /* (7) Else -> create each formation */ foreach($formations as $form_id){ // 1. Ignore if wrong format if( !is_int($form_id) || $form_id < 0 ) continue; // 2. Link formation to created TD $this->linkFormation($form_id, $created_id); } /* (7) Return @created_id */ return $created_id; } /* (2) Unlink a formation from a TD * * @idFormation Id of the formation * @idTD Id of the TD * * @return unlinked Whether it has been unlinked * ---------------------------------------------------------*/ public function unlinkFormation(int $idFormation, int $idTD) : bool{ /* (1) Prepare statement */ $st = $this->pdo->prepare('DELETE FROM GroupeTD WHERE TD_idTD = :td AND Formation_idFormation = :form'); /* (2) Manage statement error */ if( is_bool($st) ) return false; /* (3) EXecute and dispatch status */ return $st->execute([ ':td' => $idTD, ':form' => $idFormation ]); } /* (3) Link a formation to a TD * * @idFormation Id of the formation * @idTD Id of the TD * * @return linked Whether it has been linked * ---------------------------------------------------------*/ public function linkFormation(int $idFormation, int $idTD) : bool{ /* (1) Try to remove first if it already exists */ $this->unlinkFormation($idFormation, $idTD); /* (2) Prepare statement */ $st = $this->pdo->prepare('INSERT INTO GroupeTD(TD_idTD,Formation_idFormation) VALUE(:td, :form)'); /* (3) Manage statement error */ if( is_bool($st) ) return false; /* (4) EXecute and dispatch status */ return $st->execute([ ':td' => $idTD, ':form' => $idFormation ]); } /* (4.1) Updates an existing TD * * @idTD Id of the TD * @idProf [OPT] Id of the prof (NULL to set to NULL) * * @return updated Whether it has been updated * ---------------------------------------------------------*/ public function updateProf(int $idTD, ?int $idProf) : bool { /* (1) Prepare statement */ $st = $this->pdo->prepare('UPDATE TD SET Professeur_idProfesseur = :idProf WHERE idTD = :idTD'); /* (2) Manage statement error */ if( is_bool($st) ) return false; /* (3) Execute and dispatch status */ return $st->execute([ ':idTD' => $idTD, ':idProf' => $idProf ]); } /* (4.2) Updates an existing TD * * @idTD Id of the TD * @volume [OPT] The new number of hours * * @return updated Whether it has been updated * ---------------------------------------------------------*/ public function updateVolume(int $idTD, float $volume) : bool { /* (1) Prepare statement */ $st = $this->pdo->prepare('UPDATE TD SET volume = :volume WHERE idTD = :idTD'); /* (2) Manage statement error */ if( is_bool($st) ) return false; /* (3) Execute and dispatch status */ return $st->execute([ ':idTD' => $idTD, ':volume' => $volume ]); } /* (5) Deletes an existing td * * @idTD Id of the TD * * @return deleted Whether it has been deleted * ---------------------------------------------------------*/ public function delete(int $id) :bool { /* (1) Prepare statement */ $st = $this->pdo->prepare("DELETE FROM TD WHERE idTD = :id"); /* (2) Manage statement error */ if( is_bool($st) ) return false; /* (3) Execute and dispatch status */ return $st->execute([ ':id' => $id ]); } /* (x) Get groups for a specific UE * * @code UE code * * @return groups The list of groups for this UE * NULL on error * ---------------------------------------------------------*/ public function getGroups(String $code) : ?array{ /* (1) Prepare statement */ $st = $this->pdo->prepare("SELECT c.UE_code code, c.idTD, p.idProfesseur idProf, p.firstName, p.lastName, IFNULL(lform.flist, '[]') formations, c.volume FROM TD c LEFT JOIN ( SELECT gc.TD_idTD idTD, CONCAT('[', GROUP_CONCAT(DISTINCT gc.Formation_idFormation),']') flist FROM GroupeTD gc GROUP BY gc.TD_idTD ORDER BY gc.Formation_idFormation DESC ) lform ON c.idTD = lform.idTD LEFT JOIN Professeur p ON p.idProfesseur = c.Professeur_idProfesseur WHERE c.UE_code = :code;"); /* (2) Check statement */ if( is_bool($st) ) return NULL; /* (3) Execute statement */ $success = $st->execute([':code' => $code]); /* (4) Check error */ if( !$success ) return NULL; /* (5) Dispatch fetch result */ return $st->fetchAll(); } }