28 $sql =
"SELECT * FROM battle";
29 $pdoStatement = $this->pdo->prepare($sql);
30 $pdoStatement->execute();
31 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
32 $tableau = $pdoStatement->fetchAll();
44 $sql =
"SELECT * FROM battle WHERE idBattle = :id";
45 $pdoStatement = $this->pdo->prepare($sql);
46 $pdoStatement->execute(array(
50 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
51 $tableau = $pdoStatement->fetch();
52 $battle = $this->
hydrate($tableau);
69 $battle->setIdBattle(isset($data[
'idBattle']) ? (
int)$data[
'idBattle'] :
null);
70 $battle->setTitreBattle($data[
'titreBattle'] ??
null);
73 $battle->setDateDebutBattle(
74 !empty($data[
'dateDebutBattle']) ?
new DateTime($data[
'dateDebutBattle']) : null
77 $battle->setDateFinBattle(
78 !empty($data[
'dateFinBattle']) ?
new DateTime($data[
'dateFinBattle']) : null
82 if (!empty($data[
'statutBattle'])) {
83 $battle->setStatutBattle(StatutBattle::from($data[
'statutBattle']));
86 $battle->setEmailCreateurBattle($data[
'emailCreateurBattle'] ??
null);
87 $battle->setEmailParticipantBattle($data[
'emailParticipantBattle'] ??
null);
88 $battle->setIdChansonCreateur($data[
'idChansonCreateur'] ??
null);
89 $battle->setIdChansonParticipant($data[
'idChansonParticipant'] ??
null);
96 if (!empty($data[
'emailCreateurBattle'])) {
97 $battle->setCreateur($uDao->find($data[
'emailCreateurBattle']));
101 if (!empty($data[
'emailParticipantBattle'])) {
102 $battle->setParticipant($uDao->find($data[
'emailParticipantBattle']));
106 if (!empty($data[
'idChansonCreateur'])) {
107 $battle->setChansonCreateurObj($cDao->findId((
int)$data[
'idChansonCreateur']));
111 if (!empty($data[
'idChansonParticipant'])) {
112 $battle->setChansonParticipantObj($cDao->findId((
int)$data[
'idChansonParticipant']));
126 foreach ($rows as $row) {
127 $battles[] = $this->
hydrate($row);
159 $sql =
"SELECT COUNT(*) as wins FROM (
162 b.emailCreateurBattle,
163 b.emailParticipantBattle,
164 COALESCE(SUM(CASE WHEN v.emailVotee = b.emailCreateurBattle THEN 1 ELSE 0 END), 0) as votes_createur,
165 COALESCE(SUM(CASE WHEN v.emailVotee = b.emailParticipantBattle THEN 1 ELSE 0 END), 0) as votes_participant
167 LEFT JOIN vote v ON b.idBattle = v.idBattle
168 WHERE b.statutBattle = 'terminee'
169 AND (b.emailCreateurBattle = :email1 OR b.emailParticipantBattle = :email2)
170 GROUP BY b.idBattle, b.emailCreateurBattle, b.emailParticipantBattle
173 (emailCreateurBattle = :email3 AND votes_createur > votes_participant)
174 OR (emailParticipantBattle = :email4 AND votes_participant > votes_createur)";
176 $stmt = $this->pdo->prepare($sql);
178 ':email1' => $emailArtiste,
179 ':email2' => $emailArtiste,
180 ':email3' => $emailArtiste,
181 ':email4' => $emailArtiste
183 return (
int)$stmt->fetchColumn();
192 $sql =
"UPDATE battle SET
193 statutBattle = :statut,
194 emailParticipantBattle = :participant,
195 idChansonCreateur = :chansonCreateur,
196 idChansonParticipant = :chansonPart
197 WHERE idBattle = :id";
198 $stmt = $this->pdo->prepare($sql);
199 return $stmt->execute([
215 public function addVote(
string $emailVotant,
int $idBattle,
string $emailVotee): bool {
216 $sql =
"INSERT INTO vote (emailVotant, idBattle, emailVotee) VALUES (:votant, :idB, :votee)";
217 $stmt = $this->pdo->prepare($sql);
218 return $stmt->execute([
':votant' => $emailVotant,
':idB' => $idBattle,
':votee' => $emailVotee]);
227 $sql =
"INSERT INTO battle (titreBattle, dateDebutBattle, dateFinBattle, statutBattle, emailCreateurBattle, emailParticipantBattle)
228 VALUES (:titre, :debut, :fin, :statut, :createur, :participant)";
229 $stmt = $this->pdo->prepare($sql);
230 return $stmt->execute([
247 public function updateSongs(
int $idBattle, ?
int $idChansonCreateur, ?
int $idChansonParticipant): bool {
248 $sql =
"UPDATE battle SET idChansonCreateur = :c, idChansonParticipant = :p WHERE idBattle = :id";
249 return $this->pdo->prepare($sql)->execute([
':c' => $idChansonCreateur,
':p' => $idChansonParticipant,
':id' => $idBattle]);
259 $sql =
"SELECT COUNT(*) FROM vote WHERE idBattle = :idB AND emailVotee = :email";
260 $stmt = $this->pdo->prepare($sql);
261 $stmt->execute([
':idB' => $idBattle,
':email' => $emailArtiste]);
262 return (
int)$stmt->fetchColumn();
271 public function hasUserVoted(
int $idBattle,
string $emailVotant): bool {
272 $sql =
"SELECT COUNT(*) FROM vote WHERE idBattle = :idB AND emailVotant = :email";
273 $stmt = $this->pdo->prepare($sql);
274 $stmt->execute([
':idB' => $idBattle,
':email' => $emailVotant]);
275 return $stmt->fetchColumn() > 0;
285 public function modifierChanson(
int $idBattle,
int $idChanson,
bool $estCreateur): bool {
287 $nomColonne = $estCreateur ?
"idChansonCreateur" :
"idChansonParticipant";
288 $sql =
"UPDATE `battle` SET `$nomColonne` = :idC WHERE `idBattle` = :idB";
290 $stmt = $this->pdo->prepare($sql);
291 return $stmt->execute([
292 ':idC' => $idChanson,
304 $sql =
"UPDATE battle SET statutBattle = :statut WHERE idBattle = :id";
305 $stmt = $this->pdo->prepare($sql);
306 return $stmt->execute([
307 ':statut' => $nouveauStatut,
319 $sql =
"SELECT * FROM battle
320 WHERE emailCreateurBattle = :email1
321 OR emailParticipantBattle = :email2
322 ORDER BY dateDebutBattle DESC";
323 $stmt = $this->pdo->prepare($sql);
328 return $this->hydrateMany($stmt->fetchAll(PDO::FETCH_ASSOC));
340 WHEN (emailCreateurBattle = :email1 AND votes_createur > votes_participant) OR
341 (emailParticipantBattle = :email2 AND votes_participant > votes_createur)
342 THEN 1 ELSE 0 END) as victoires,
344 WHEN (emailCreateurBattle = :email3 AND votes_createur < votes_participant) OR
345 (emailParticipantBattle = :email4 AND votes_participant < votes_createur)
346 THEN 1 ELSE 0 END) as defaites
349 b.idBattle, b.emailCreateurBattle, b.emailParticipantBattle,
350 (SELECT COUNT(*) FROM vote v WHERE v.idBattle = b.idBattle AND v.emailVotee = b.emailCreateurBattle) as votes_createur,
351 (SELECT COUNT(*) FROM vote v WHERE v.idBattle = b.idBattle AND v.emailVotee = b.emailParticipantBattle) as votes_participant
353 WHERE (b.emailCreateurBattle = :email5 OR b.emailParticipantBattle = :email6)
354 AND b.statutBattle = 'terminee'
357 $stmt = $this->pdo->prepare($sql);
359 ':email1' => $email,
':email2' => $email,
360 ':email3' => $email,
':email4' => $email,
361 ':email5' => $email,
':email6' => $email
364 $res = $stmt->fetch(PDO::FETCH_ASSOC);
366 'victoires' => (int)($res[
'victoires'] ?? 0),
367 'defaites' => (int)($res[
'defaites'] ?? 0)
376 $sql =
"DELETE FROM battle WHERE idBattle = :id AND statutBattle = 'en_attente'";
377 $stmt = $this->pdo->prepare($sql);
378 return $stmt->execute([
':id' => $idBattle]);
countBattlesWon(string $emailArtiste)
Compte le nombre de battles gagnées par un artiste.
insert(Battle $battle)
Insère une nouvelle battle dans la base de données.
hydrate(array $data)
Hydrate une battle à partir d'un tableau associatif.
updateSongs(int $idBattle, ?int $idChansonCreateur, ?int $idChansonParticipant)
Met à jour les chansons liées à une battle.
update(Battle $battle)
Met à jour les informations d'une battle existante.
modifierStatut(int $idBattle, string $nouveauStatut)
Met à jour le statut d'une battle.
findAllByUser(string $email)
Récupère toutes les battles liées à un utilisateur.
findAll()
Récupère toutes les battles de la base de données.
hasUserVoted(int $idBattle, string $emailVotant)
Vérifie si un utilisateur a déjà voté dans une battle spécifique.
getStatsArtiste(string $email)
Calcule les statistiques de victoires et défaites d'un artiste.
setPdo(?PDO $pdo)
Setter pour la pdo.
deleteBattle(int $idBattle)
Supprime une battle si elle est encore en attente.
find(int $id)
Récupère une battle par son identifiant.
__construct(?PDO $pdo=null)
Constructeur de la classe BattleDAO.
getPdo()
Getter pour la pdo.
hydrateMany(array $rows)
Hydrate plusieurs battles à partir d'un tableau de tableaux associatifs.
addVote(string $emailVotant, int $idBattle, string $emailVotee)
Enregistre un vote pour une battle.
modifierChanson(int $idBattle, int $idChanson, bool $estCreateur)
Met à jour la chanson d'un participant (créateur ou invité).
getVotesCount(int $idBattle, string $emailArtiste)
Récupère le nombre de votes pour un artiste dans une battle spécifique.
Classe représentant une battle musicale.
getEmailCreateurBattle()
Getter pour emailCreateurBattle.
getTitreBattle()
Getter pour titreBattle.
getIdChansonParticipant()
Getter pour idChansonParticipant.
getEmailParticipantBattle()
Getter pour emailParticipantBattle.
getStatutBattle()
Getter pour statutBattle.
getDateFinBattle()
Getter pour dateFinBattle.
getIdChansonCreateur()
Getter pour idChansonCreateur.
getDateDebutBattle()
Getter pour dateDebutBattle.
getIdBattle()
Getter pour idBattle.