Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
battle.dao.php
Aller à la documentation de ce fichier.
1<?php
7class BattleDAO {
11 private ?PDO $pdo;
12
17 public function __construct(?PDO $pdo = null)
18 {
19 $this->pdo = $pdo;
20 }
21
26 public function findAll(): array
27 {
28 $sql = "SELECT * FROM battle";
29 $pdoStatement = $this->pdo->prepare($sql);
30 $pdoStatement->execute();
31 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
32 $tableau = $pdoStatement->fetchAll();
33 $battle = $this->hydrateMany($tableau);
34 return $battle;
35 }
36
42 public function find(int $id): Battle
43 {
44 $sql = "SELECT * FROM battle WHERE idBattle = :id";
45 $pdoStatement = $this->pdo->prepare($sql);
46 $pdoStatement->execute(array(
47 ':id' => $id
48 ));
49
50 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
51 $tableau = $pdoStatement->fetch();
52 $battle = $this->hydrate($tableau);
53 return $battle;
54 }
55
65 public function hydrate(array $data): Battle
66 {
67 $battle = new Battle();
68
69 $battle->setIdBattle(isset($data['idBattle']) ? (int)$data['idBattle'] : null);
70 $battle->setTitreBattle($data['titreBattle'] ?? null);
71
72 // Conversion sécurisée des dates SQL → objets DateTime
73 $battle->setDateDebutBattle(
74 !empty($data['dateDebutBattle']) ? new DateTime($data['dateDebutBattle']) : null
75 );
76
77 $battle->setDateFinBattle(
78 !empty($data['dateFinBattle']) ? new DateTime($data['dateFinBattle']) : null
79 );
80
81 // Conversion du statut (enum)
82 if (!empty($data['statutBattle'])) {
83 $battle->setStatutBattle(StatutBattle::from($data['statutBattle']));
84 }
85
86 $battle->setEmailCreateurBattle($data['emailCreateurBattle'] ?? null);
87 $battle->setEmailParticipantBattle($data['emailParticipantBattle'] ?? null);
88 $battle->setIdChansonCreateur($data['idChansonCreateur'] ?? null);
89 $battle->setIdChansonParticipant($data['idChansonParticipant'] ?? null);
90
91 // --- HYDRATATION DES OBJETS LIÉS (SÉCURITÉ PSEUDO/PHOTO) ---
92 $uDao = new UtilisateurDAO($this->pdo);
93 $cDao = new ChansonDAO($this->pdo);
94
95 // Hydratation du créateur
96 if (!empty($data['emailCreateurBattle'])) {
97 $battle->setCreateur($uDao->find($data['emailCreateurBattle']));
98 }
99
100 // Hydratation du participant
101 if (!empty($data['emailParticipantBattle'])) {
102 $battle->setParticipant($uDao->find($data['emailParticipantBattle']));
103 }
104
105 // Hydratation de la chanson du créateur
106 if (!empty($data['idChansonCreateur'])) {
107 $battle->setChansonCreateurObj($cDao->findId((int)$data['idChansonCreateur']));
108 }
109
110 // Hydratation de la chanson du participant
111 if (!empty($data['idChansonParticipant'])) {
112 $battle->setChansonParticipantObj($cDao->findId((int)$data['idChansonParticipant']));
113 }
114
115 return $battle;
116 }
117
123 public function hydrateMany(array $rows): array
124 {
125 $battles = [];
126 foreach ($rows as $row) {
127 $battles[] = $this->hydrate($row);
128 }
129 return $battles;
130 }
131
136 public function getPdo(): ?PDO
137 {
138 return $this->pdo;
139 }
140
146 public function setPdo(?PDO $pdo): void
147 {
148 $this->pdo = $pdo;
149 }
150
157 public function countBattlesWon(string $emailArtiste): int
158 {
159 $sql = "SELECT COUNT(*) as wins FROM (
160 SELECT
161 b.idBattle,
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
166 FROM battle b
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
171 ) AS battle_stats
172 WHERE
173 (emailCreateurBattle = :email3 AND votes_createur > votes_participant)
174 OR (emailParticipantBattle = :email4 AND votes_participant > votes_createur)";
175
176 $stmt = $this->pdo->prepare($sql);
177 $stmt->execute([
178 ':email1' => $emailArtiste,
179 ':email2' => $emailArtiste,
180 ':email3' => $emailArtiste,
181 ':email4' => $emailArtiste
182 ]);
183 return (int)$stmt->fetchColumn();
184 }
185
191 public function update(Battle $battle): bool {
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([
200 ':statut' => $battle->getStatutBattle()->value,
201 ':participant' => $battle->getEmailParticipantBattle(),
202 ':chansonCreateur' => $battle->getIdChansonCreateur(),
203 ':chansonPart' => $battle->getIdChansonParticipant(),
204 ':id' => $battle->getIdBattle()
205 ]);
206 }
207
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]);
219 }
220
226 public function insert(Battle $battle): bool {
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([
231 ':titre' => $battle->getTitreBattle(),
232 ':debut' => $battle->getDateDebutBattle()?->format('Y-m-d H:i:s'),
233 ':fin' => $battle->getDateFinBattle()?->format('Y-m-d H:i:s'),
234 ':statut' => $battle->getStatutBattle()->value,
235 ':createur' => $battle->getEmailCreateurBattle(),
236 ':participant' => $battle->getEmailParticipantBattle()
237 ]);
238 }
239
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]);
250 }
251
258 public function getVotesCount(int $idBattle, string $emailArtiste): int {
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();
263 }
264
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;
276 }
277
285 public function modifierChanson(int $idBattle, int $idChanson, bool $estCreateur): bool {
286
287 $nomColonne = $estCreateur ? "idChansonCreateur" : "idChansonParticipant";
288 $sql = "UPDATE `battle` SET `$nomColonne` = :idC WHERE `idBattle` = :idB";
289
290 $stmt = $this->pdo->prepare($sql);
291 return $stmt->execute([
292 ':idC' => $idChanson,
293 ':idB' => $idBattle
294 ]);
295}
296
303 public function modifierStatut(int $idBattle, string $nouveauStatut): bool {
304 $sql = "UPDATE battle SET statutBattle = :statut WHERE idBattle = :id";
305 $stmt = $this->pdo->prepare($sql);
306 return $stmt->execute([
307 ':statut' => $nouveauStatut,
308 ':id' => $idBattle
309 ]);
310 }
316 public function findAllByUser(string $email): array
317 {
318 // On utilise :email1 et :email2 pour éviter l'erreur de paramètre dupliqué
319 $sql = "SELECT * FROM battle
320 WHERE emailCreateurBattle = :email1
321 OR emailParticipantBattle = :email2
322 ORDER BY dateDebutBattle DESC";
323 $stmt = $this->pdo->prepare($sql);
324 $stmt->execute([
325 ':email1' => $email,
326 ':email2' => $email
327 ]);
328 return $this->hydrateMany($stmt->fetchAll(PDO::FETCH_ASSOC));
329 }
330
336 public function getStatsArtiste(string $email): array
337 {
338 $sql = "SELECT
339 SUM(CASE
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,
343 SUM(CASE
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
347 FROM (
348 SELECT
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
352 FROM battle b
353 WHERE (b.emailCreateurBattle = :email5 OR b.emailParticipantBattle = :email6)
354 AND b.statutBattle = 'terminee'
355 ) as stats";
356
357 $stmt = $this->pdo->prepare($sql);
358 $stmt->execute([
359 ':email1' => $email, ':email2' => $email,
360 ':email3' => $email, ':email4' => $email,
361 ':email5' => $email, ':email6' => $email
362 ]);
363
364 $res = $stmt->fetch(PDO::FETCH_ASSOC);
365 return [
366 'victoires' => (int)($res['victoires'] ?? 0),
367 'defaites' => (int)($res['defaites'] ?? 0)
368 ];
369 }
375 public function deleteBattle(int $idBattle): bool {
376 $sql = "DELETE FROM battle WHERE idBattle = :id AND statutBattle = 'en_attente'";
377 $stmt = $this->pdo->prepare($sql);
378 return $stmt->execute([':id' => $idBattle]);
379 }
380
381}
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.