src/Controller/IndexController.php line 130

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ExerciseResult;
  4. use App\Entity\ExerciseSimpleAnswer;
  5. use App\Entity\ExerciseSimpleAnswerResult;
  6. use App\Entity\Kpi;
  7. use App\Entity\Patient;
  8. use Monolog\Logger;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\Extension\Core\Type\DateType;
  12. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Validator\Constraints\DateTime;
  19. class IndexController extends AbstractController
  20. {
  21.     /** @var SessionInterface */
  22.     private $session;
  23.     /** @var ManagerRegistry */
  24.     private $managerRegistry;
  25.     /**
  26.      * @param SessionInterface $session
  27.      */
  28.     public function __construct(SessionInterface $sessionManagerRegistry $managerRegistry)
  29.     {
  30.         $this->session $session;
  31.         $this->managerRegistry $managerRegistry;
  32.     }
  33.     /**
  34.      * @Route("/", name="homepage")
  35.      */
  36.     public function index(){
  37.         return $this->redirectToRoute('index');
  38.     }
  39.     /**
  40.      * @Route("/dashboard/{dateStart}/{dateEnd}", name="index", defaults={ "dateStart" : null, "dateEnd" : null})
  41.      */
  42.     public function dashboard(Request $request$dateStart$dateEnd): Response
  43.     {
  44.         $caregiver $this->getUser();
  45.         $start = new \DateTime();
  46.         $end = new \DateTime();
  47.         if(!is_null($dateStart) && preg_match('/^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/'$dateStart)){
  48.             $start = \DateTime::createFromFormat('d-m-Y'$dateStart);
  49.         }else{
  50.             $start->modify('-1 year');
  51.         }
  52.         if(!is_null($dateEnd) && preg_match('/^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/'$dateEnd)){
  53.             $end = \DateTime::createFromFormat('d-m-Y'$dateEnd);
  54.         }
  55.         $patients $this->getUser()->getPatients();
  56.         $kpis $this->managerRegistry->getRepository(Kpi::class)->findAll();
  57. //        $tests = $this->managerRegistry->getRepository(ExerciseResult::class)->findAll();
  58.         $tests = [];
  59.         $kpiGroups = [];
  60.         $totalTests 0;
  61.         $patientStats = [];
  62.         foreach ($patients as $idx => $patient){
  63.             /** @var Patient $patient */
  64.             $patientStats[$patient->getId()] = isset($patientStats[$patient->getId()]) ? $patientStats[$patient->getId()] : [
  65.                 'patient' => $patient,
  66.                 'kpis' => [],
  67.                 'totalPlayed' => 0
  68.             ];
  69.             /** @var Patient $patient */
  70.             $exercises $this->getDoctrine()->getRepository(ExerciseResult::class)->findByPatientTypeInterval($patient,$start$end);
  71.             $patientStats[$patient->getId()]['totalPlayed'] = count($exercises);
  72.             foreach ($exercises as $exercise){
  73.                 /** @var ExerciseResult $exercise */
  74.                 $totalTests++;
  75.                     $patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()] =
  76.                         isset($patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]) ?
  77.                         $patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()] :
  78.                         [
  79.                             'kpi' => $exercise->getKpi(),
  80.                             'played' => 0,
  81.                             'exercises' => [],
  82.                             'totalEvaluation' => 0
  83.                         ]
  84.                 ;
  85.                 $patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]['played']++;
  86.                 $patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]['totalEvaluation'] += $exercise->getKpiEvaluation();
  87.                 $patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]['exercises'][] = $exercise;
  88.             }
  89.         }
  90.         return $this->render('index/index.html.twig', [
  91.             'patientStats' => $patientStats,
  92.             'patients' => $patients,
  93.             'dateStart' => $start,
  94.             'dateEnd' => $end,
  95.             'totalTests' => $totalTests,
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/keep-alive", name="keep_alive", methods={"GET","POST"})
  100.      */
  101.     public function keepAlive(): Response
  102.     {
  103.         $previous $this->session->get('app_keep_alive');
  104.         $this->session->set('app_keep_alive'microtime(true));
  105.         return new JsonResponse([
  106.             'result' => true,
  107.             'message' => 'Done! Previous time was '.$previous
  108.         ]);
  109.     }
  110. }