src/Controller/HomeController.php line 25
<?php
namespace App\Controller;
use App\Repository\QuizzRepository;
use App\Repository\ThemeRepository;
use App\Repository\TrackingRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
function __construct(
private UserRepository $userRepository,
private TrackingRepository $trackingRepository,
private ThemeRepository $themeRepository,
private QuizzRepository $quizzRepository
)
{}
#[Route('/', name: 'app_home')]
public function index(): Response
{
$user = $this->getUser();
if ($user && in_array('ROLE_STUDENT', $user->getRoles())) {
return $this->redirectToRoute('app_ui_home');
}
if ($user && in_array('ROLE_SUPERVISOR', $user->getRoles())) {
return $this->redirectToRoute('app_dashboard');
}
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}
#[Route('/not-found', name: 'app_not_found')]
public function notFound(): Response
{
return $this->render('not-found.html.twig', [
'controller_name' => 'HomeController',
]);
}
#[Route('/dashbord', name: 'app_dashboard')]
public function dashbord(): Response
{
$actifStudents = $this->userRepository->findActiveStudents();
$inactifStudents = $this->userRepository->findInactivStudents();
$totalStudents = $this->userRepository->countStudents();
$totalCourses = $this->themeRepository->countThemes();
$totalQuizz = $this->quizzRepository->countQuizz();
$results = $this->trackingRepository->getWeeklyAverageScores();
$weeklyAverages = [];
$weeklyCounts = [];
foreach ($results as $result) {
// Calculate week of the month
$weekOfYear = (int) $result['createdAt']->format('W');
$startOfMonth = (new \DateTime($result['createdAt']->format('Y-m-01')))->format('W');
$weekOfMonth = $weekOfYear - $startOfMonth + 1;
if (!isset($weeklyAverages[$weekOfMonth])) {
$weeklyAverages[$weekOfMonth] = 0;
$weeklyCounts[$weekOfMonth] = 0;
}
$weeklyAverages[$weekOfMonth] += $result['quizzNote'];
$weeklyCounts[$weekOfMonth]++;
}
foreach ($weeklyAverages as $week => $total) {
$weeklyAverages[$week] = $total / $weeklyCounts[$week];
}
return $this->render('home/dashboard.html.twig', [
'controller_name' => 'HomeController',
'actifStudents' => count($actifStudents),
'inactifStudents' => count($inactifStudents),
'totalStudents' => $totalStudents,
'totalCourses' => $totalCourses,
'totalQuizz' => $totalQuizz,
'weeklyAverages' => $weeklyAverages,
]);
}
}