Browse Source

Nb de films et commentaires soumis par mois

Sangfroid 5 months ago
parent
commit
136df0a9d4

+ 1 - 0
composer.json

@@ -6,6 +6,7 @@
         "php": "^8.2",
         "ext-ctype": "*",
         "ext-iconv": "*",
+        "beberlei/doctrineextensions": "^1.5",
         "composer/package-versions-deprecated": "1.11.99.5",
         "doctrine/annotations": "^2.0",
         "doctrine/doctrine-bundle": "^2.12",

+ 63 - 1
composer.lock

@@ -4,8 +4,70 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "572c52715190154517a04e502532f55c",
+    "content-hash": "6032b0a35ad44d84618d557f1ba08409",
     "packages": [
+        {
+            "name": "beberlei/doctrineextensions",
+            "version": "v1.5.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/beberlei/DoctrineExtensions.git",
+                "reference": "281f1650641c2f438b0a54d8eaa7ba50ac7e3eb6"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/beberlei/DoctrineExtensions/zipball/281f1650641c2f438b0a54d8eaa7ba50ac7e3eb6",
+                "reference": "281f1650641c2f438b0a54d8eaa7ba50ac7e3eb6",
+                "shasum": ""
+            },
+            "require": {
+                "doctrine/orm": "^2.19 || ^3.0",
+                "php": "^7.2 || ^8.0"
+            },
+            "require-dev": {
+                "doctrine/annotations": "^1.14 || ^2",
+                "doctrine/coding-standard": "^9.0.2 || ^12.0",
+                "nesbot/carbon": "^2.72 || ^3",
+                "phpstan/phpstan": "^1.10",
+                "phpunit/phpunit": "^8.5 || ^9.6",
+                "squizlabs/php_codesniffer": "^3.8",
+                "symfony/cache": "^5.4 || ^6.4 || ^7.0",
+                "symfony/yaml": "^5.4 || ^6.4 || ^7.0",
+                "vimeo/psalm": "^3.18 || ^5.22",
+                "zf1/zend-date": "^1.12",
+                "zf1/zend-registry": "^1.12"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "DoctrineExtensions\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "BSD-3-Clause"
+            ],
+            "authors": [
+                {
+                    "name": "Benjamin Eberlei",
+                    "email": "kontakt@beberlei.de"
+                },
+                {
+                    "name": "Steve Lacey",
+                    "email": "steve@steve.ly"
+                }
+            ],
+            "description": "A set of extensions to Doctrine 2 that add support for additional query functions available in MySQL, Oracle, PostgreSQL and SQLite.",
+            "keywords": [
+                "database",
+                "doctrine",
+                "orm"
+            ],
+            "support": {
+                "source": "https://github.com/beberlei/DoctrineExtensions/tree/v1.5.0"
+            },
+            "time": "2024-03-03T17:55:15+00:00"
+        },
         {
             "name": "composer/package-versions-deprecated",
             "version": "1.11.99.5",

+ 5 - 0
config/packages/doctrine.yaml

@@ -24,6 +24,11 @@ doctrine:
                 alias: App
         controller_resolver:
             auto_mapping: false
+        
+        dql:
+            datetime_functions:
+                DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat
+
 
 when@test:
     doctrine:

+ 70 - 1
src/Controller/DashboardController.php

@@ -2,6 +2,8 @@
 
 namespace App\Controller;
 
+use App\Repository\CommentaireRepository;
+use App\Repository\FilmRepository;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Attribute\Route;
@@ -11,8 +13,16 @@ use Symfony\UX\Chartjs\Model\Chart;
 class DashboardController extends AbstractController
 {
     #[Route('/dashboard', name: 'app_dashboard')]
-    public function index(ChartBuilderInterface $chartBuilderInterface): Response
+    public function index(
+        ChartBuilderInterface $chartBuilderInterface,
+        FilmRepository $filmRepository,
+        CommentaireRepository $commentaireRepository
+    ): Response
     {
+        $filmsVus = $filmRepository->countFilmsVusBy($this->getUser());
+        $filmsFavoris = $filmRepository->countFilmsFavorisBy($this->getUser());
+        $commentairesParMois = $commentaireRepository->countCommentairesParMois();
+
         $chart = $chartBuilderInterface->createChart(Chart::TYPE_DOUGHNUT);
         $chart->setData([
             'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
@@ -35,8 +45,67 @@ class DashboardController extends AbstractController
             ],
         ]);
 
+        $chartDatesAjout = $chartBuilderInterface->createChart(Chart::TYPE_LINE);
+        $ajoutsParMois = $filmRepository->countAjoutsParMois();
+
+        $months = [];
+        $entryCounts = [];
+        foreach ($ajoutsParMois as $data) {
+            $months[] = $data['month'];
+            $entryCounts[] = $data['entryCount'];
+        }
+
+
+        $chartDatesAjout->setData([
+            'labels' => $months,
+            'datasets' => [
+                [
+                    'label' => 'Nombre de films ajoutés par mois',
+                    'data'  => $entryCounts
+                ]
+            ]
+        ]);
+
+        $chart->setOptions([
+            'scales' => [
+                'y' => [
+                    'suggestedMin' => 0,
+                    'suggestedMax' => 100,
+                ],
+            ],
+        ]);
+
+        $months = [];
+        $entryCounts = [];
+        foreach ($commentairesParMois as $data) {
+            $months[] = $data['month'];
+            $entryCounts[] = $data['entryCount'];
+        }
+
+        $chartCommentairesAjout = $chartBuilderInterface->createChart(Chart::TYPE_LINE);
+        $chartCommentairesAjout->setData([
+            'labels' => $months,
+            'datasets' => [
+                [
+                    'label' => 'Nombre de films ajoutés par mois',
+                    'data'  => $entryCounts
+                ]
+            ]
+        ]);
+
+        $chart->setOptions([
+            'scales' => [
+                'y' => [
+                    'suggestedMin' => 0,
+                    'suggestedMax' => 100,
+                ],
+            ],
+        ]);
+
         return $this->render('dashboard/index.html.twig', [
             'chart' => $chart,
+            'chartAjoutsParMois' => $chartDatesAjout,
+            'chartCommentairesAjouts' => $chartCommentairesAjout
         ]);
     }
 }

+ 23 - 0
src/Repository/CommentaireRepository.php

@@ -29,4 +29,27 @@ class CommentaireRepository extends ServiceEntityRepository
             ->getQuery()
             ->getResult();
     }
+
+    public function countCommentairesParMois(): array
+    {
+        return $this->createQueryBuilder('c')
+            ->select("DATE_FORMAT(c.dateSubmitted, '%Y-%m') as month, COUNT(c.id) as entryCount")
+            ->groupBy('month')
+            ->orderBy('month', 'ASC')
+            ->getQuery()
+            ->getResult()
+        ;
+    }
+
+    public function findFirstDateAdded()
+    {
+        // Construire une requête pour obtenir la première date d'ajout
+        return $this->createQueryBuilder('c')
+            ->select('c.dateSubmitted')
+            ->orderBy('c.dateSubmitted', 'ASC')  // Trier par date croissante
+            ->setMaxResults(1)  // Limiter à 1 résultat (la première date)
+            ->getQuery()
+            ->getOneOrNullResult();  // Récupérer le premier résultat ou null
+    }
+
 }

+ 55 - 0
src/Repository/FilmRepository.php

@@ -3,6 +3,7 @@
 namespace App\Repository;
 
 use App\Entity\Film;
+use App\Entity\User;
 use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
 use Doctrine\ORM\QueryBuilder;
 use Doctrine\Persistence\ManagerRegistry;
@@ -220,4 +221,58 @@ class FilmRepository extends ServiceEntityRepository
             ->getResult()
         ;
     }
+
+    public function countAjoutsParMois(): array
+    {
+        return $this->createQueryBuilder('f')
+            ->select("DATE_FORMAT(f.dateSubmited, '%Y-%m') as month, COUNT(f.id) as entryCount")
+            ->groupBy('month')
+            ->orderBy('month', 'ASC')
+            ->getQuery()
+            ->getResult()
+        ;
+    }
+
+    public function countFilmsVusBy(User $user): int
+    {
+        $qb = $this->createQueryBuilder('f');
+        $qb
+            ->select('COUNT(f.id)')
+            ->leftJoin('f.usersWhoSeen', 'wan')
+            ->andWhere($qb->expr()->in('wan', ':user'))
+            ->setParameter('user', $user)
+        ;
+
+        return $qb
+            ->getQuery()
+            ->getSingleScalarResult()
+        ;
+    }
+
+    public function countFilmsFavorisBy(User $user): int
+    {
+        $qb = $this->createQueryBuilder('f');
+        $qb
+            ->select('COUNT(f.id)')
+            ->leftJoin('f.usersWantToView', 'wan')
+            ->andWhere($qb->expr()->in('wan', ':user'))
+            ->setParameter('user', $user)
+        ;
+
+        return $qb
+            ->getQuery()
+            ->getSingleScalarResult()
+        ;
+    }
+
+    public function findFirstDateAdded(): ?Film
+    {
+        // Construire une requête pour obtenir la première date d'ajout
+        return $this->createQueryBuilder('f')
+            ->select('f.dateSubmited')
+            ->orderBy('f.dateSubmited', 'ASC')  // Trier par date croissante
+            ->setMaxResults(1)  // Limiter à 1 résultat (la première date)
+            ->getQuery()
+            ->getOneOrNullResult();  // Récupérer le premier résultat ou null
+    }
 }

+ 26 - 3
templates/dashboard/index.html.twig

@@ -1,7 +1,30 @@
-{% extends 'base.html.twig' %}
+{% extends 'videotheque/base.html.twig' %}
 
-{% block title %}Hello DashboardController!{% endblock %}
+{% block title %}{{ parent() }} - Tableau de bord{% endblock %}
+{% block titre %}Tableau de bord{% endblock %}
 
 {% block body %}
-{{ render_chart(chart) }}
+<div class="row">
+    <div class="col">
+        <div class="card shadow mb-4">
+            <div class="cad-header">
+                Commentaires par mois
+            </div>
+            <div class="card-body">
+                {{ render_chart(chartCommentairesAjouts) }}
+            </div>
+        </div>
+    </div>
+    <div class="col">
+        <div class="card shadow mb-4">
+            <div class="cad-header">
+                Films ajoutés par mois
+            </div>
+            <div class="card-body">
+                {{ render_chart(chartAjoutsParMois) }}
+            </div>
+        </div>
+    </div>
+</div>
+
 {% endblock %}