<?php
namespace App\Controller;
use App\Entity\ExerciseSimpleAnswer;
use App\Repository\ExerciseSimpleAnswerRepository;
use App\Repository\PatientRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/langchain-exercise")
*/
class LangchainExerciseController extends AbstractController
{
/**
* @Route("/get-random-exercise-langchain", name="get_random_exercise_langchain", methods={"GET"})
*/
public function randomExercise(ExerciseSimpleAnswerRepository $exerciseSimpleAnswerRepository): JsonResponse
{
$ids = $exerciseSimpleAnswerRepository->getAllIds();
if (empty($ids)) {
return new JsonResponse(['error' => 'No exercises available'], 404);
}
$randomId = $ids[array_rand($ids)];
$exercise = $exerciseSimpleAnswerRepository->find($randomId);
if (!$exercise) {
return new JsonResponse(['error' => 'Exercise not found'], 404);
}
return new JsonResponse([
'name' => $exercise->getName(),
'kpi' => $exercise->getKpi()->getName(),
'description' => $exercise->getDescription(),
'question' => $exercise->getQuestion(),
'answer' => $exercise->getAnswer(),
]);
}
/**
* @Route("/exercise-by-kpi", name="exercise_by_kpi", methods={"POST"})
*/
public function exerciseByKpi(Request $request, ExerciseSimpleAnswerRepository $exerciseSimpleAnswerRepository): JsonResponse
{
$data = json_decode($request->getContent(), true);
if (!isset($data['kpi']) || empty($data['kpi'])) {
return new JsonResponse(['error' => 'KPI is required'], 400);
}
$kpi = $data['kpi'];
$exercises = $exerciseSimpleAnswerRepository->findByKpiName($kpi);
if (empty($exercises)) {
return new JsonResponse(['error' => 'No exercises found for the specified KPI'], 404);
}
$randomExercise = $exercises[array_rand($exercises)];
return new JsonResponse([
'name' => $randomExercise->getName(),
'kpi' => $randomExercise->getKpi()->getName(),
'description' => $randomExercise->getDescription(),
'question' => $randomExercise->getQuestion(),
'answer' => $randomExercise->getAnswer(),
]);
}
/**
* @Route("/increase-solved-exercises", name="increase_solved_exercises", methods={"POST"})
*/
public function increaseSolvedExercises(Request $request, PatientRepository $patientRepository): JsonResponse
{
$data = json_decode($request->getContent(), true);
if (!isset($data['patient_id']) || empty($data['patient_id'])) {
return new JsonResponse(['error' => 'Patient ID is required'], 400);
}
if (!isset($data['kpi']) || empty($data['kpi'])) {
return new JsonResponse(['error' => 'KPI is required'], 400);
}
$patientId = $data['patient_id'];
$kpi = $data['kpi'];
$patient = $patientRepository->find($patientId);
if (!$patient) {
return new JsonResponse(['error' => 'Patient not found'], 404);
}
$solvedExercises = $patient->getSolvedExercises();
if (!$solvedExercises) {
$solvedExercises = [
'Stimolazione del linguaggio' => 0,
'Stimolazione del ragionamento' => 0,
'Stimolazione dell\'attenzione' => 0,
'Stimolazione dell\'orientamento' => 0
];
} else {
$solvedExercises = json_decode($solvedExercises, true);
}
if (isset($solvedExercises[$kpi])) {
$solvedExercises[$kpi]++;
} else {
return new JsonResponse(['error' => 'Invalid KPI'], 400);
}
$patient->setSolvedExercises(json_encode($solvedExercises));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return new JsonResponse(['message' => 'Solved exercises updated successfully']);
}
/**
* @Route("/{id}", name="get_simple_answer_exercise_langchain", methods={"GET"})
*/
public function exerciseById(ExerciseSimpleAnswer $exerciseSimpleAnswer): JsonResponse
{
return new JsonResponse([
'name' => $exerciseSimpleAnswer->getName(),
'kpi' => $exerciseSimpleAnswer->getKpi()->getName(),
'description' => $exerciseSimpleAnswer->getDescription(),
'question' => $exerciseSimpleAnswer->getQuestion(),
'answer' => $exerciseSimpleAnswer->getAnswer(),
]);
}
}