src/Controller/HomeController.php line 25

  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\QuizzRepository;
  4. use App\Repository\ThemeRepository;
  5. use App\Repository\TrackingRepository;
  6. use App\Repository\UserRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class HomeController extends AbstractController
  11. {
  12.     function __construct(
  13.         private UserRepository $userRepository,
  14.         private TrackingRepository $trackingRepository,
  15.         private ThemeRepository $themeRepository,
  16.         private QuizzRepository $quizzRepository
  17.     )
  18.     {}
  19.     #[Route('/'name'app_home')]
  20.     public function index(): Response
  21.     {
  22.         $user $this->getUser();
  23.         if ($user && in_array('ROLE_STUDENT'$user->getRoles())) {
  24.             return $this->redirectToRoute('app_ui_home');
  25.         }
  26.         if ($user && in_array('ROLE_SUPERVISOR'$user->getRoles())) {
  27.             return $this->redirectToRoute('app_dashboard');
  28.         }
  29.         return $this->render('home/index.html.twig', [
  30.             'controller_name' => 'HomeController',
  31.         ]);
  32.     }
  33.     #[Route('/not-found'name'app_not_found')]
  34.     public function notFound(): Response
  35.     {
  36.         return $this->render('not-found.html.twig', [
  37.             'controller_name' => 'HomeController',
  38.         ]);
  39.     }
  40.     #[Route('/dashbord'name'app_dashboard')]
  41.     public function dashbord(): Response
  42.     {
  43.         $actifStudents   $this->userRepository->findActiveStudents();
  44.         $inactifStudents $this->userRepository->findInactivStudents();
  45.         $totalStudents   $this->userRepository->countStudents();
  46.         $totalCourses    $this->themeRepository->countThemes();
  47.         $totalQuizz      $this->quizzRepository->countQuizz();
  48.         $results    $this->trackingRepository->getWeeklyAverageScores();
  49.         $weeklyAverages = [];
  50.         $weeklyCounts = [];
  51.         foreach ($results as $result) {
  52.             // Calculate week of the month
  53.             $weekOfYear = (int) $result['createdAt']->format('W');
  54.             $startOfMonth = (new \DateTime($result['createdAt']->format('Y-m-01')))->format('W');
  55.             $weekOfMonth $weekOfYear $startOfMonth 1;
  56.             if (!isset($weeklyAverages[$weekOfMonth])) {
  57.                 $weeklyAverages[$weekOfMonth] = 0;
  58.                 $weeklyCounts[$weekOfMonth] = 0;
  59.             }
  60.             $weeklyAverages[$weekOfMonth] += $result['quizzNote'];
  61.             $weeklyCounts[$weekOfMonth]++;
  62.         }
  63.         foreach ($weeklyAverages as $week => $total) {
  64.             $weeklyAverages[$week] = $total $weeklyCounts[$week];
  65.         }       
  66.         return $this->render('home/dashboard.html.twig', [
  67.             'controller_name' => 'HomeController',
  68.             'actifStudents'   => count($actifStudents),
  69.             'inactifStudents' => count($inactifStudents),
  70.             'totalStudents'   => $totalStudents,
  71.             'totalCourses'    => $totalCourses,
  72.             'totalQuizz'      => $totalQuizz,
  73.             'weeklyAverages' => $weeklyAverages,
  74.         ]);
  75.     }
  76. }