M2.2 : ajouter une fonctionnalité

This commit is contained in:
Erwann PHILIPPE 2026-01-16 17:29:38 +01:00
parent f9c0ed3a63
commit 7aa5c3d816
4 changed files with 76 additions and 14 deletions

View file

@ -51,9 +51,16 @@ class PlaylistsController extends AbstractController {
public function index(): Response{ public function index(): Response{
$playlists = $this->playlistRepository->findAllOrderByName('ASC'); $playlists = $this->playlistRepository->findAllOrderByName('ASC');
$categories = $this->categorieRepository->findAll(); $categories = $this->categorieRepository->findAll();
$nombreFormations = [];
foreach ($playlists as $p) {
$nombreFormations[$p->getId()] = $this->playlistRepository->countFormationsByPlaylist($p);
}
return $this->render("pages/playlists.html.twig", [ return $this->render("pages/playlists.html.twig", [
'playlists' => $playlists, 'playlists' => $playlists,
'categories' => $categories 'categories' => $categories,
'nombreFormations' => $nombreFormations
]); ]);
} }
@ -63,13 +70,23 @@ class PlaylistsController extends AbstractController {
case "name": case "name":
$playlists = $this->playlistRepository->findAllOrderByName($ordre); $playlists = $this->playlistRepository->findAllOrderByName($ordre);
break; break;
case "nbResult":
$playlists = $this->playlistRepository->findAllOrderByResultNb($ordre);
break;
default: default:
break; break;
} }
$categories = $this->categorieRepository->findAll(); $categories = $this->categorieRepository->findAll();
$nombreFormations = [];
foreach ($playlists as $p) {
$nombreFormations[$p->getId()] = $this->playlistRepository->countFormationsByPlaylist($p);
}
return $this->render($this->playlistPage, [ return $this->render($this->playlistPage, [
'playlists' => $playlists, 'playlists' => $playlists,
'categories' => $categories 'categories' => $categories,
'nombreFormations' => $nombreFormations
]); ]);
} }
@ -78,11 +95,18 @@ class PlaylistsController extends AbstractController {
$valeur = $request->get("recherche"); $valeur = $request->get("recherche");
$playlists = $this->playlistRepository->findByContainValue($champ, $valeur, $table); $playlists = $this->playlistRepository->findByContainValue($champ, $valeur, $table);
$categories = $this->categorieRepository->findAll(); $categories = $this->categorieRepository->findAll();
$nombreFormations = [];
foreach ($playlists as $p) {
$nombreFormations[$p->getId()] = $this->playlistRepository->countFormationsByPlaylist($p);
}
return $this->render($this->playlistPage, [ return $this->render($this->playlistPage, [
'playlists' => $playlists, 'playlists' => $playlists,
'categories' => $categories, 'categories' => $categories,
'valeur' => $valeur, 'valeur' => $valeur,
'table' => $table 'table' => $table,
'nombreFormations' => $nombreFormations
]); ]);
} }
@ -91,11 +115,12 @@ class PlaylistsController extends AbstractController {
$playlist = $this->playlistRepository->find($id); $playlist = $this->playlistRepository->find($id);
$playlistCategories = $this->categorieRepository->findAllForOnePlaylist($id); $playlistCategories = $this->categorieRepository->findAllForOnePlaylist($id);
$playlistFormations = $this->formationRepository->findAllForOnePlaylist($id); $playlistFormations = $this->formationRepository->findAllForOnePlaylist($id);
$nbFormations = $this->playlistRepository->countFormationsByPlaylist($playlist);
return $this->render("pages/playlist.html.twig", [ return $this->render("pages/playlist.html.twig", [
'playlist' => $playlist, 'playlist' => $playlist,
'playlistcategories' => $playlistCategories, 'playlistcategories' => $playlistCategories,
'playlistformations' => $playlistFormations 'playlistformations' => $playlistFormations,
'nombreFormations' => $nbFormations
]); ]);
} }
} }

View file

@ -30,7 +30,6 @@ class PlaylistRepository extends ServiceEntityRepository
/** /**
* Retourne toutes les playlists triées sur le nom de la playlist * Retourne toutes les playlists triées sur le nom de la playlist
* @param type $champ
* @param string $ordre * @param string $ordre
* @return Playlist[] * @return Playlist[]
*/ */
@ -46,9 +45,9 @@ class PlaylistRepository extends ServiceEntityRepository
/** /**
* Enregistrements dont un champ contient une valeur * Enregistrements dont un champ contient une valeur
* ou tous les enregistrements si la valeur est vide * ou tous les enregistrements si la valeur est vide
* @param type $champ * @param string $champ
* @param type $valeur * @param string $valeur
* @param type $table si $champ dans une autre table * @param string $table si $champ dans une autre table
* @return Playlist[] * @return Playlist[]
*/ */
public function findByContainValue($champ, $valeur, $table=""): array{ public function findByContainValue($champ, $valeur, $table=""): array{
@ -77,4 +76,36 @@ class PlaylistRepository extends ServiceEntityRepository
} }
} }
/**
* 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;
}
} }

View file

@ -11,7 +11,8 @@
{% endfor %} {% endfor %}
<br /><br /> <br /><br />
<strong>description :</strong><br /> <strong>description :</strong><br />
{{ playlist.description|nl2br }} {{ playlist.description|nl2br }}<br>
Nombre de formations : {{ nombreFormations }}
</div> </div>
<div class="col"> <div class="col">
<!-- boucle sur l'affichage des formations --> <!-- boucle sur l'affichage des formations -->
@ -20,7 +21,7 @@
<div class="col-md-auto"> <div class="col-md-auto">
{% if formation.miniature %} {% if formation.miniature %}
<a href="{{ path('formations.showone', {id:formation.id}) }}"> <a href="{{ path('formations.showone', {id:formation.id}) }}">
<img src="{{ formation.miniature }}"> <img src="{{ formation.miniature }}" alt="Miniature de la formation">
</a> </a>
{% endif %} {% endif %}
</div> </div>

View file

@ -33,7 +33,9 @@
</form> </form>
</th> </th>
<th class="text-center align-top" scope="col"> <th class="text-center align-top" scope="col">
&nbsp; Nb résultats
<a href="{{ path('playlists.sort', {champ:'nbResult', ordre:'ASC'}) }}" class="btn btn-info btn-sm active" role="button" aria-pressed="true"><</a>
<a href="{{ path('playlists.sort', {champ:'nbResult', ordre:'DESC'}) }}" class="btn btn-info btn-sm active" role="button" aria-pressed="true">></a>
</th> </th>
</tr> </tr>
</thead> </thead>
@ -58,6 +60,9 @@
<td class="text-center"> <td class="text-center">
<a href="{{ path('playlists.showone', {id:playlists[k].id}) }}" class="btn btn-secondary">Voir détail</a> <a href="{{ path('playlists.showone', {id:playlists[k].id}) }}" class="btn btn-secondary">Voir détail</a>
</td> </td>
<td>
{{ nombreFormations[playlists[k].id] ?? 0 }}
</td>
</tr> </tr>
{% endfor %} {% endfor %}
{% endif %} {% endif %}