<?php
namespace App\Controller;
use App\Entity\ExerciseResult;
use App\Entity\ExerciseSimpleAnswer;
use App\Entity\ExerciseSimpleAnswerResult;
use App\Entity\Kpi;
use App\Entity\Patient;
use Monolog\Logger;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\DateTime;
class IndexController extends AbstractController
{
/** @var SessionInterface */
private $session;
/** @var ManagerRegistry */
private $managerRegistry;
/**
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session, ManagerRegistry $managerRegistry)
{
$this->session = $session;
$this->managerRegistry = $managerRegistry;
}
/**
* @Route("/", name="homepage")
*/
public function index(){
return $this->redirectToRoute('index');
}
/**
* @Route("/dashboard/{dateStart}/{dateEnd}", name="index", defaults={ "dateStart" : null, "dateEnd" : null})
*/
public function dashboard(Request $request, $dateStart, $dateEnd): Response
{
$caregiver = $this->getUser();
$start = new \DateTime();
$end = new \DateTime();
if(!is_null($dateStart) && preg_match('/^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/', $dateStart)){
$start = \DateTime::createFromFormat('d-m-Y', $dateStart);
}else{
$start->modify('-1 year');
}
if(!is_null($dateEnd) && preg_match('/^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/', $dateEnd)){
$end = \DateTime::createFromFormat('d-m-Y', $dateEnd);
}
$patients = $this->getUser()->getPatients();
$kpis = $this->managerRegistry->getRepository(Kpi::class)->findAll();
// $tests = $this->managerRegistry->getRepository(ExerciseResult::class)->findAll();
$tests = [];
$kpiGroups = [];
$totalTests = 0;
$patientStats = [];
foreach ($patients as $idx => $patient){
/** @var Patient $patient */
$patientStats[$patient->getId()] = isset($patientStats[$patient->getId()]) ? $patientStats[$patient->getId()] : [
'patient' => $patient,
'kpis' => [],
'totalPlayed' => 0
];
/** @var Patient $patient */
$exercises = $this->getDoctrine()->getRepository(ExerciseResult::class)->findByPatientTypeInterval($patient,$start, $end);
$patientStats[$patient->getId()]['totalPlayed'] = count($exercises);
foreach ($exercises as $exercise){
/** @var ExerciseResult $exercise */
$totalTests++;
$patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()] =
isset($patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]) ?
$patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()] :
[
'kpi' => $exercise->getKpi(),
'played' => 0,
'exercises' => [],
'totalEvaluation' => 0
]
;
$patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]['played']++;
$patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]['totalEvaluation'] += $exercise->getKpiEvaluation();
$patientStats[$patient->getId()]['kpis'][$exercise->getKpi()->getId()]['exercises'][] = $exercise;
}
}
return $this->render('index/index.html.twig', [
'patientStats' => $patientStats,
'patients' => $patients,
'dateStart' => $start,
'dateEnd' => $end,
'totalTests' => $totalTests,
]);
}
/**
* @Route("/keep-alive", name="keep_alive", methods={"GET","POST"})
*/
public function keepAlive(): Response
{
$previous = $this->session->get('app_keep_alive');
$this->session->set('app_keep_alive', microtime(true));
return new JsonResponse([
'result' => true,
'message' => 'Done! Previous time was '.$previous
]);
}
}