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{
$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
]);
}
}

View file

@ -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;
}
}

View file

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

View file

@ -33,7 +33,9 @@
</form>
</th>
<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>
</tr>
</thead>
@ -57,7 +59,10 @@
</td>
<td class="text-center">
<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>
{% endfor %}
{% endif %}