diff --git a/src/Controller/PlaylistsController.php b/src/Controller/PlaylistsController.php index 25fbefc..b38c761 100644 --- a/src/Controller/PlaylistsController.php +++ b/src/Controller/PlaylistsController.php @@ -51,9 +51,16 @@ class PlaylistsController extends AbstractController { public function index(): Response{ $playlists = $this->playlistRepository->findAllOrderByName('ASC'); $categories = $this->categorieRepository->findAll(); + + $nombreFormations = []; + foreach ($playlists as $p) { + $nombreFormations[$p->getId()] = $this->playlistRepository->countFormationsByPlaylist($p); + } + return $this->render("pages/playlists.html.twig", [ 'playlists' => $playlists, - 'categories' => $categories + 'categories' => $categories, + 'nombreFormations' => $nombreFormations ]); } @@ -63,13 +70,23 @@ class PlaylistsController extends AbstractController { case "name": $playlists = $this->playlistRepository->findAllOrderByName($ordre); break; + case "nbResult": + $playlists = $this->playlistRepository->findAllOrderByResultNb($ordre); + break; default: break; } $categories = $this->categorieRepository->findAll(); + + $nombreFormations = []; + foreach ($playlists as $p) { + $nombreFormations[$p->getId()] = $this->playlistRepository->countFormationsByPlaylist($p); + } + return $this->render($this->playlistPage, [ 'playlists' => $playlists, - 'categories' => $categories + 'categories' => $categories, + 'nombreFormations' => $nombreFormations ]); } @@ -78,11 +95,18 @@ class PlaylistsController extends AbstractController { $valeur = $request->get("recherche"); $playlists = $this->playlistRepository->findByContainValue($champ, $valeur, $table); $categories = $this->categorieRepository->findAll(); + + $nombreFormations = []; + foreach ($playlists as $p) { + $nombreFormations[$p->getId()] = $this->playlistRepository->countFormationsByPlaylist($p); + } + return $this->render($this->playlistPage, [ 'playlists' => $playlists, 'categories' => $categories, 'valeur' => $valeur, - 'table' => $table + 'table' => $table, + 'nombreFormations' => $nombreFormations ]); } @@ -91,11 +115,12 @@ class PlaylistsController extends AbstractController { $playlist = $this->playlistRepository->find($id); $playlistCategories = $this->categorieRepository->findAllForOnePlaylist($id); $playlistFormations = $this->formationRepository->findAllForOnePlaylist($id); + $nbFormations = $this->playlistRepository->countFormationsByPlaylist($playlist); return $this->render("pages/playlist.html.twig", [ 'playlist' => $playlist, 'playlistcategories' => $playlistCategories, - 'playlistformations' => $playlistFormations + 'playlistformations' => $playlistFormations, + 'nombreFormations' => $nbFormations ]); } - } diff --git a/src/Repository/PlaylistRepository.php b/src/Repository/PlaylistRepository.php index 68494ea..cb56c06 100644 --- a/src/Repository/PlaylistRepository.php +++ b/src/Repository/PlaylistRepository.php @@ -30,7 +30,6 @@ class PlaylistRepository extends ServiceEntityRepository /** * Retourne toutes les playlists triées sur le nom de la playlist - * @param type $champ * @param string $ordre * @return Playlist[] */ @@ -46,9 +45,9 @@ class PlaylistRepository extends ServiceEntityRepository /** * Enregistrements dont un champ contient une valeur * ou tous les enregistrements si la valeur est vide - * @param type $champ - * @param type $valeur - * @param type $table si $champ dans une autre table + * @param string $champ + * @param string $valeur + * @param string $table si $champ dans une autre table * @return Playlist[] */ public function findByContainValue($champ, $valeur, $table=""): array{ @@ -76,5 +75,37 @@ class PlaylistRepository extends ServiceEntityRepository ->getResult(); } } - + + /** + * Retourne toutes les playlists triées sur le nombre de résultats par playlist + * @param string $ordre + * @return Playlist[] + */ + public function findAllOrderByResultNb($ordre){ + return $this->createQueryBuilder('p') + ->select('p', 'COUNT(f.id) AS HIDDEN nbrFormations') + ->leftJoin('p.formations', 'f') + ->groupBy('p.id') + ->orderBy('nbrFormations', $ordre) + ->getQuery() + ->getResult(); + } + + /** + * Renvoie le nombre de formations pour une playlist donnée + * @param Playlist $playlist + * @return int + */ + public function countFormationsByPlaylist(Playlist $playlist): int + { + $nb = $this->createQueryBuilder('p') + ->select('COUNT(f.id)') + ->leftJoin('p.formations', 'f') + ->where('p = :playlist') + ->setParameter('playlist', $playlist) + ->getQuery() + ->getSingleScalarResult(); + + return (int) $nb; + } } diff --git a/templates/pages/playlist.html.twig b/templates/pages/playlist.html.twig index f68e297..22ffb0a 100644 --- a/templates/pages/playlist.html.twig +++ b/templates/pages/playlist.html.twig @@ -11,7 +11,8 @@ {% endfor %}

description :
- {{ playlist.description|nl2br }} + {{ playlist.description|nl2br }}
+ Nombre de formations : {{ nombreFormations }}
@@ -20,7 +21,7 @@
{% if formation.miniature %} - + Miniature de la formation {% endif %}
diff --git a/templates/pages/playlists.html.twig b/templates/pages/playlists.html.twig index 68d7eb3..61c3643 100644 --- a/templates/pages/playlists.html.twig +++ b/templates/pages/playlists.html.twig @@ -33,7 +33,9 @@ -   + Nb résultats + < + > @@ -57,7 +59,10 @@ Voir détail - + + + {{ nombreFormations[playlists[k].id] ?? 0 }} + {% endfor %} {% endif %}