src/Controller/Api/AbsenceController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Absence;
  4. use App\Entity\Notifications;
  5. use App\Entity\StudentYear;
  6. use App\Repository\AbsenceRepository;
  7. use App\Repository\NotificationsRepository;
  8. use App\Repository\SchoolYearRepository;
  9. use App\Repository\StudentRepository;
  10. use App\Repository\StudentYearRepository;
  11. use App\Repository\UserRepository;
  12. use App\Repository\UserYearRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use JMS\Serializer\SerializationContext;
  15. use JMS\Serializer\SerializerInterface;
  16. use Knp\Component\Pager\PaginatorInterface;
  17. use OpenApi\Annotations as OA;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Twig\Environment;
  24. use Symfony\Component\Mercure\HubInterface;
  25. use Symfony\Component\Mercure\Update;
  26. use App\Repository\SubjectRepository;
  27. use App\Repository\ExamsRepository;
  28. use App\Service\PushNotificationService;
  29. use App\Entity\UserNotifications;
  30. use App\Repository\TheClassRepository;
  31. class AbsenceController extends AbstractController
  32. {
  33.      /**
  34.      * Absence parents index
  35.      * @OA\Tag(name="Absence")
  36.      */
  37.     #[Route("/api/parent/students/absences/",name'api_parent_absence_student_list'methods: ['GET'])]
  38.     public function parentsGetAbsenceIndex(
  39.         SchoolYearRepository $schoolYearRepository,
  40.         StudentYearRepository $studentYearRepository,
  41.         AbsenceRepository $absenceRepository,
  42.         SerializerInterface $serializer,
  43.         Request $request,
  44.         PaginatorInterface $paginator
  45.     ):JsonResponse
  46.     {
  47.         $user $this->getUser();
  48.         $year $schoolYearRepository->findOneBy(['active'=>true],[]);
  49.         $parentStudent $studentYearRepository->findParentStudents($user$this->getUser()->getSchool());
  50.         $studentsIds = [];
  51.         $userLateDetails = [];
  52.         foreach ($parentStudent as $value){
  53.             $studentsIds[] = $value->getStudent();
  54.             $studentAbsence $absenceRepository->findBy(['student'=>$value,'year'=>$year], ['id' => 'DESC']);
  55.             $totalHrs 0;
  56.             $totalJust 0;
  57.             $totalInjust 0;
  58.             foreach ($studentAbsence as $val){
  59.                 $totalHrs += $val->getDuration();
  60.                 $totalInjust += $val->getDuration() - $val->getJustified();
  61.                 $totalJust += $val->getJustified();
  62.             }
  63.             $absenceTotale = [
  64.                 'student' => [
  65.                     'id'=>$value->getStudent()->getId(),
  66.                     'firstName'=>$value->getStudent()->getFirstName(),
  67.                     'lastName'=>$value->getStudent()->getLastName()
  68.                 ],
  69.                 'totalHrs' =>$totalHrs,
  70.                 'totalInjust' => $totalInjust,
  71.                 'totalJust' => $totalJust
  72.             ];
  73.             $userLateDetails[] = $absenceTotale;
  74.         }
  75.         //dd($userLateDetails);
  76.         $parentAbsenceStudents $absenceRepository->findAbsenceStudentsParent($studentsIds);
  77.         $data $paginator->paginate($parentAbsenceStudents,
  78.             $request->query->getInt('page'1),40);
  79.         $getData = [
  80.             'current_page_number'=>$data->getCurrentPageNumber(),
  81.             'number_per_page'=>$data->getItemNumberPerPage(),
  82.             'total_count'=>$data->getTotalItemCount(),
  83.             'total_absence_hrs'=>$userLateDetails,
  84.             'data'=>$data->getItems(),
  85.         ];
  86.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  87.         $jsonPost $serializer->serialize($getData,'json',$context);
  88.         return new JsonResponse($jsonPost,Response::HTTP_OK,['accept'=>'application/json'],true);
  89.     }
  90.      /**
  91.      * Absence parents index
  92.      * @OA\Tag(name="Absence")
  93.      */
  94.     #[Route("/api/parent/students/absences/classe/{classe}/",name'api_parent_absence_student_list_classe'methods: ['GET'])]
  95.     public function parentsGetAbsenceIndexClasse(
  96.         SchoolYearRepository $schoolYearRepository,
  97.         StudentYearRepository $studentYearRepository,
  98.         AbsenceRepository $absenceRepository,
  99.         SerializerInterface $serializer,
  100.         Request $request,
  101.         PaginatorInterface $paginator,
  102.         $classe,
  103.         TheClassRepository $theClassRepository
  104.     ):JsonResponse
  105.     {
  106.         $user $this->getUser();
  107.         $year $schoolYearRepository->findOneBy(['active'=>true],[]);
  108.         $classRepo $theClassRepository->find($classe);
  109.         $parentStudent $studentYearRepository->findBy(["classe"=>$classRepo'year'=>$year'school'=>$user->getSchool()]);
  110.         $studentsIds = [];
  111.         $userLateDetails = [];
  112.         foreach ($parentStudent as $value){
  113.             $studentsIds[] = $value->getStudent();
  114.             $studentAbsence $absenceRepository->findBy(['student'=>$value,'year'=>$year], ['id' => 'DESC']);
  115.             $totalHrs 0;
  116.             $totalJust 0;
  117.             $totalInjust 0;
  118.             foreach ($studentAbsence as $val){
  119.                 $totalHrs += $val->getDuration();
  120.                 $totalInjust += $val->getDuration() - $val->getJustified();
  121.                 $totalJust += $val->getJustified();
  122.             }
  123.             $absenceTotale = [
  124.                 'student' => [
  125.                     'id'=>$value->getStudent()->getId(),
  126.                     'firstName'=>$value->getStudent()->getFirstName(),
  127.                     'lastName'=>$value->getStudent()->getLastName()
  128.                 ],
  129.                 'totalHrs' =>$totalHrs,
  130.                 'totalInjust' => $totalInjust,
  131.                 'totalJust' => $totalJust
  132.             ];
  133.             $userLateDetails[] = $absenceTotale;
  134.         }
  135.         //dd($userLateDetails);
  136.         $parentAbsenceStudents $absenceRepository->findBy(["classe"=>$classRepo'year'=>$year'school'=>$user->getSchool()], ['id' => 'DESC']);
  137.        
  138.         $data $paginator->paginate($parentAbsenceStudents,
  139.             $request->query->getInt('page'1),40);
  140.            // dd($data->getItems());
  141.         $resData = [];
  142.         foreach ($data as $item){
  143.             $absence = [];
  144.             $absence['id'] = $item->getId();
  145.             $studentAbsence $absenceRepository->find($item->getId());
  146.             $absence['student'] = $studentAbsence ? [
  147.                 'id'=>$studentAbsence->getStudent()->getId(),
  148.                 'first_name'=>$studentAbsence->getStudent()->getFirstName(),
  149.                 'last_name'=>$studentAbsence->getStudent()->getLastName()
  150.             ] : null;
  151.             $absence['subject'] = [
  152.                 'id'=>$item->getSubject()->getId(),
  153.                 'name'=>$item->getSubject()->getName()
  154.             ];
  155.             $absence['start_time'] = $item->getStartTime();
  156.             $absence['end_time'] = $item->getEndTime();
  157.             $absence['absence_date'] = $item->getAbsenceDate();
  158.             $absence['duration'] = $item->getDuration();
  159.             $absence['created_at'] = $item->getCreatedAt();
  160.             $resData[] = $absence;
  161.         }
  162.         $getData = [
  163.             'current_page_number'=>$data->getCurrentPageNumber(),
  164.             'number_per_page'=>$data->getItemNumberPerPage(),
  165.             'total_count'=>$data->getTotalItemCount(),
  166.             'total_absence_hrs'=>$userLateDetails,
  167.             'data'=>$resData,
  168.         ];
  169.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  170.         $jsonPost $serializer->serialize($getData,'json',$context);
  171.         return new JsonResponse($jsonPost,Response::HTTP_OK,['accept'=>'application/json'],true);
  172.     }
  173.      /**
  174.      * Absence parents index
  175.      * @OA\Tag(name="Absence")
  176.      */
  177.     #[Route("/api/user/homework/students/classe/{classe}/",name'api_parent_absence_student_list_classe_student_exam'methods: ['GET'])]
  178.     public function studentClass(
  179.         SchoolYearRepository $schoolYearRepository,
  180.         StudentYearRepository $studentYearRepository,
  181.         ExamsRepository $examsRepository,
  182.         $classe,
  183.         TheClassRepository $theClassRepository,
  184.         SubjectRepository $subjectRepository,
  185.     ):JsonResponse
  186.     {
  187.         $user $this->getUser();
  188.         $year $schoolYearRepository->findOneBy(['active'=>true],[]);
  189.         $classRepo $theClassRepository->find($classe);
  190.         $students $studentYearRepository->findBy(["classe"=>$classRepo'year'=>$year'school'=>$user->getSchool()]);
  191.         $examsRepo $examsRepository->findBy(['year'=>$year'school'=>$user->getSchool(), 'classe'=>$classRepo], ['id'=>'DESC']);
  192.         $examList = [];
  193.         foreach ($examsRepo as $exam){
  194.             $examList[] = [
  195.                 'id'=>$exam->getId(),
  196.                 'name'=>$exam->getName(),
  197.             ];
  198.         }
  199.         $studentList = [];
  200.         foreach ($students as $student) {
  201.             $studentList[] = [
  202.                 'id' => $student->getStudent()->getId(),
  203.                 'firstName' => $student->getStudent()->getFirstName(),
  204.                 'lastName' => $student->getStudent()->getLastName(),
  205.             ];
  206.         }
  207.         // Trier la liste des élèves par ordre alphabétique (lastName puis firstName)
  208.         usort($studentList, function($a$b) {
  209.             $cmp strcmp($a['lastName'], $b['lastName']);
  210.             if ($cmp === 0) {
  211.                 return strcmp($a['firstName'], $b['firstName']);
  212.             }
  213.             return $cmp;
  214.         });
  215.         $subjectRepo $subjectRepository->findBy(['classe' => $classRepo'year' => $year'school' => $user->getSchool()], ['id' => 'DESC']);
  216.         $subjectList = [];
  217.         foreach ($subjectRepo as $subject) {
  218.             $subjectList[] = [
  219.                 'id' => $subject->getId(),
  220.                 'name' => $subject->getName(),
  221.             ];
  222.         }
  223.         //dd($subjectList);
  224.         $jsonPost = [
  225.             'classe' => [
  226.                 'id' => $classRepo->getId(),
  227.                 'name' => $classRepo->getName(),
  228.             ],
  229.             'examList' => $examList,
  230.             'studentList' => $studentList,
  231.             'subjectList' => $subjectList
  232.         ];
  233.         //dd($studentList);
  234.         return  $this->json($jsonPost);
  235.     }
  236.     /**
  237.      * Absence index
  238.      * @OA\Tag(name="Absence")
  239.      */
  240.     #[Route(path'/api/absence/{id}/show'name"api_absence_show",methods: ['GET'])]
  241.     public  function show(Absence $absence,SerializerInterface $serializer):JsonResponse
  242.     {
  243.         if (!$this->getUser()){
  244.             return new JsonResponse(
  245.                 $serializer->serialize(['message'=>'you must be logged'],'json'),
  246.                 Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  247.                 true
  248.             );
  249.         }
  250.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  251.         $jsonPost $serializer->serialize($absence,'json',$context);
  252.         return new JsonResponse($jsonPost,Response::HTTP_OK,['accept'=>'application/json'],true);
  253.     }
  254.     /**
  255.      * Add new Absence via API
  256.      * @OA\Tag(name="Absence")
  257.      */
  258.     #[Route('/api/prof/absence/new/user/{id}/'name'api_absence_add_user_'methods: ['POST'])]
  259.     public function apiAddAbsence(
  260.         $id,
  261.         SubjectRepository $subjectRepository,
  262.         StudentRepository $studentRepository,
  263.         Request $request,
  264.         EntityManagerInterface $entityManager,
  265.         SchoolYearRepository $schoolYearRepository,
  266.         ExamsRepository $examRepo,
  267.         PushNotificationService $notification,
  268.         SerializerInterface $serializer,
  269.         TheClassRepository $theClassRepository,
  270.         StudentYearRepository $studentYearRepository
  271.     ): JsonResponse
  272.     {
  273.         $data $request->request->all();
  274.         $absence = new Absence();
  275.         $school $this->getUser()->getSchool();
  276.         $year $schoolYearRepository->findOneBy(["active"=>true],[]);
  277.        // $_student = $studentRepository->find($data["studentId"]);
  278.         
  279.         $subject $subjectRepository->findOneBy(['id' => $data["subject"], 'school' => $school]);
  280.         $studentYear $studentYearRepository->findOneBy(['student' => $id'year' => $year]);
  281.         $classe $studentYear->getClasse();
  282.         //$theClassRepo = $theClassRepository->find($data["classe"]);
  283.         $exam $examRepo->findOneBy(['id' => $data['exam'], 'school' => $school]);
  284.         $absence->setYear($year);
  285.         $absence->setStudent($studentYear->getStudent());
  286.         $absence->setDuration((float)$data["duration"]);
  287.         $absence->setSubject($subject);
  288.         $absence->setClasse($classe);
  289.         $absence->setSchool($school);
  290.         $absence->setAbsenceDate($data["lateDate"]);
  291.         $absence->setStartTime($data['normalTms']);
  292.         $absence->setEndTime($data['arriveTms']);
  293.         $absence->setExam($exam);
  294.         $absence->setAuthor($this->getUser());
  295.         //$this->getUser()->addAbsence($absence);
  296.         $entityManager->persist($absence);
  297.         $entityManager->persist($this->getUser());
  298.         $entityManager->flush();
  299.         
  300.         
  301.         $parent $studentYear->getStudent()->getParent();
  302.         $_student $studentYear->getStudent();
  303.         
  304.         if (!empty($parent)) {
  305.             $lang $parent->getLocalPhoneLang();
  306.             $token $parent->getUserToken();
  307.             $userId $parent->getId();
  308.             $route '/drawer/Absence';
  309.             $title $lang == null 'Absence signalée' 'Absence Signaled';
  310.             
  311.             if ($lang == null ) {
  312.                 $message = ($parent->getSexe() == 'M' 'Cher ' 'Chère ')
  313.                     . $parent->getFullName()
  314.                     . ' votre enfant '
  315.                     $_student->getFullName()
  316.                     . ' a été absent au cours de : '
  317.                     $subject->getName()
  318.                     . ' du '
  319.                     $absence->getAbsenceDate()
  320.                     . ' entre '
  321.                     $absence->getStartTime()
  322.                     . ' et '
  323.                     $absence->getEndTime();
  324.             } else {
  325.                 $message = ($parent->getSexe() == 'M' 'Dear ' 'Dear ')
  326.                     . $parent->getFullName()
  327.                     . ' your child '
  328.                     $_student->getFullName()
  329.                     . ' was absent in the course: '
  330.                     $subject->getName()
  331.                     . ' on '
  332.                     $absence->getAbsenceDate()
  333.                     . ' between '
  334.                     $absence->getStartTime()
  335.                     . ' and '
  336.                     $absence->getEndTime();
  337.             }
  338.             if ($token){
  339.                 $notificationUser = new UserNotifications;
  340.                 $notificationUser->setTitle($title);
  341.                 $notificationUser->setContent($message);
  342.                 $notificationUser->setUser($parent);
  343.                 $notificationUser->setExpoPushToken($token);
  344.                 $notificationUser->setType("absence");
  345.                 $entityManager->persist($notificationUser);
  346.                 $entityManager->flush();
  347.                 $notification->sendNotification($token$title$message$route$userId);
  348.             }
  349.         } 
  350.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  351.         $jsonPost $serializer->serialize($absence,'json',$context);
  352.         return new JsonResponse($jsonPostResponse::HTTP_OK, ['accept'=>'application/json'], true);
  353.     }
  354.        /**
  355.      * Absence notifications
  356.      * @OA\Tag(name="Absence")
  357.      */
  358.     #[Route(path'/api/absence/notification'name"api_absence_notifications",methods: ['GET'])]
  359.     public function absenceNotification(
  360.         Request $request,
  361.         PaginatorInterface $paginator,
  362.         StudentYearRepository $studentYearRepository,
  363.         SerializerInterface $serializer,
  364.         SchoolYearRepository $schoolYearRepository,
  365.         StudentRepository $studentRepository,
  366.         AbsenceRepository $absenceRepository,
  367.         NotificationsRepository $notificationsRepository,
  368.         EntityManagerInterface $entityManager):JsonResponse
  369.     {
  370.         $user $this->getUser();
  371.         $year $schoolYearRepository->findOneBy(['active'=>true],[]);
  372.         $parentStudent $studentYearRepository->findParentStudents($user$this->getUser()->getSchool());
  373.         $studentsIds = [];
  374.         $userLateDetails = [];
  375.         foreach ($parentStudent as $value){
  376.             $studentsIds[] = $value->getStudent();
  377.             $studentAbsence $absenceRepository->findBy(['student'=>$value,'year'=>$year]);
  378.             $totalHrs 0;
  379.             $totalJust 0;
  380.             $totalInjust 0;
  381.             foreach ($studentAbsence as $val){
  382.                 $totalHrs += $val->getDuration();
  383.                 $totalInjust += $val->getDuration() - $val->getJustified();
  384.                 $totalJust += $val->getJustified();
  385.             }
  386.             $absenceTotale = [
  387.                 'student' => [
  388.                     'id'=>$value->getStudent()->getId(),
  389.                     'firstName'=>$value->getStudent()->getFirstName(),
  390.                     'lastName'=>$value->getStudent()->getLastName()
  391.                 ],
  392.                 'totalHrs' =>$totalHrs,
  393.                 'totalInjust' => $totalInjust,
  394.                 'totalJust' => $totalJust
  395.             ];
  396.             $userLateDetails[] = $absenceTotale;
  397.         }
  398.         //dd($userLateDetails);
  399.         $parentAbsenceStudents $absenceRepository->findAbsenceStudentsParent($studentsIds);
  400.         $data $paginator->paginate($parentAbsenceStudents,
  401.             $request->query->getInt('page'1),40);
  402.         $notificationUserRow =  $notificationsRepository->findOneBy(['user'=>$user],[]);
  403.         if ($notificationUserRow == null){
  404.             $notif = new Notifications();
  405.             $notif->setUser($this->getUser());
  406.             $entityManager->persist($notif);
  407.             $entityManager->flush();
  408.         }
  409.         $absenceLastConnectionCount $notificationsRepository->findOneBy(['user'=>$user],[]);
  410.         $absenceLastConnectionCountValue $absenceLastConnectionCount->getAbsenceLastConnectionCount();
  411.         //if ($absenceLastConnectionCountValue !== null){
  412.         $absenceAdded $data->getTotalItemCount() - $absenceLastConnectionCountValue;
  413.         if ($absenceAdded>1){
  414.             $updateNotif $notificationsRepository->findOneBy(['user'=>$user],[]);
  415.             $updateNotif->setAbsenceLastConnectionCount($data->getTotalItemCount());
  416.             $entityManager->flush();
  417.             return new JsonResponse(
  418.                 $serializer->serialize(['title'=>'Absence(s)',"message" => $absenceAdded." Absences ont été ajoutée(s)" ],'json'),
  419.                 Response::HTTP_OK,['accept'=>'application/json'],
  420.                 true
  421.             );
  422.         }
  423.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  424.     }
  425.  /**
  426.      * Absence index
  427.      * @OA\Tag(name="Absence")
  428.      */
  429.     #[Route("/v1/api/parent/absence/{from}",name'api_absence'methods: ['GET'])]
  430.     public function index(Request $request,
  431.        PaginatorInterface $paginator,
  432.        StudentYearRepository $studentYearRepository,
  433.        SerializerInterface $serializer,
  434.        SchoolYearRepository $schoolYearRepository,
  435.        StudentRepository $studentRepository,
  436.        AbsenceRepository $absenceRepository,
  437.        NotificationsRepository $notificationsRepository,
  438.        EntityManagerInterface $entityManager
  439.     ):JsonResponse
  440.     {
  441.         if (!$this->getUser()){
  442.              return new JsonResponse(
  443.                  $serializer->serialize(['message'=>'you must be logged'],'json'),
  444.                  Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  445.                  true
  446.              );
  447.         }
  448.         $year $schoolYearRepository->findOneBy(["active"=>true]);
  449.         $user $this->getUser();
  450.         //$students = $studentYearRepository->findBy(['year'=>$year]);
  451.         $school $user->getSchool();
  452.         $_student $studentYearRepository->findBy(['year'=>$year'school' => $school],['id'=>'DESc']);
  453.         
  454.         $parentStudents $studentRepository->findByParent($user->getId(), $school->getId());
  455.         $absence $absenceRepository->findBy(['year' => $year'school' => $school], ["id"=>'DESC']);
  456.         $absenceContent = [];
  457.         foreach ($absence as $value){
  458.             if (in_array($value->getStudent(), $parentStudentstrue)){
  459.                 if ($value->getId() > $from) {
  460.                     $absenceContent[] = $value;
  461.                 }
  462.             }
  463.         }
  464.         $data $paginator->paginate($absenceContent,
  465.             $request->query->getInt('page'1),20);
  466.         $userLateDetails = [];
  467.         foreach ($parentStudents as $value){
  468.             $studentAbsence $absenceRepository->findBy(['student' => $value,'year' => $year]);
  469.             $totalHrs 0;
  470.             $totalJust 0;
  471.             $totalInjust 0;
  472.             foreach ($studentAbsence as $val){
  473.                 $totalHrs += $val->getDuration();
  474.                 $totalInjust += $val->getDuration() - $val->getJustified();
  475.                 $totalJust += $val->getJustified();
  476.             }
  477.             $absenceTotale = [
  478.                 'student' => $value,
  479.                 'totalHrs' =>$totalHrs,
  480.                 'totalInjust' => $totalInjust,
  481.                 'totalJust' => $totalJust
  482.             ];
  483.             $userLateDetails[] = $absenceTotale;
  484.         }
  485.         $getData = [
  486.             'current_page_number'=>$data->getCurrentPageNumber(),
  487.             'number_per_page'=>$data->getItemNumberPerPage(),
  488.             'total_count'=>$data->getTotalItemCount(),
  489.             'total_absence_hrs'=>$userLateDetails,
  490.             'data'=>$data->getItems(),
  491.         ];
  492.         $notificationUpdate $notificationsRepository->findOneBy(['user'=>$user]);
  493.         if ($notificationUpdate === null){
  494.             $notification = new Notifications();
  495.             $notification->setUser($user);
  496.             $notification->setAbsenceLastConnectionCount($data->getTotalItemCount());
  497.             $entityManager->persist($notification);
  498.             $entityManager->flush();
  499.         }else{
  500.             $notificationUpdate->setAbsenceLastConnectionCount($data->getTotalItemCount());
  501.             $entityManager->flush();
  502.         }
  503.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  504.         $jsonAbsence $serializer->serialize($getData,'json',$context);
  505.         return new JsonResponse($jsonAbsence,Response::HTTP_OK,['accept'=>'application/json'],true);
  506.     }
  507.     /**
  508.      * Absence notifications
  509.      * @OA\Tag(name="Absence")
  510.      */
  511.     #[Route("/v3/api/porter/absence/index/students/list",name'api_porter_absence_student_list'methods: ['GET'])]
  512.     public function getStudentListForAbsence(SchoolYearRepository $schoolYearRepository,
  513.         StudentYearRepository $studentYearRepository,
  514.         SerializerInterface $serializer
  515.     ):JsonResponse
  516.     {
  517.         $user $this->getUser();
  518.         $school $this->getUser()->getSchool();
  519.         $year =  $schoolYearRepository->findOneBy(['active'=>true]);
  520.         $students $studentYearRepository->findBy(['school' => $school,'year' => $year]);
  521.         $context SerializationContext::create()->setGroups(["getStudents"]);
  522.         $jsonStudents $serializer->serialize($students,'json',$context);
  523.         return new JsonResponse($jsonStudents,Response::HTTP_OK,['accept'=>'application/json'],true);
  524.     }
  525.     /**
  526.      * Absence parents index
  527.      * @OA\Tag(name="Absence")
  528.      */
  529.     #[Route("/v3/api/parent/students/absences/from/{from}/list",name'api_parent_absence_student_list_'methods: ['GET'])]
  530.     public function parentsGetAbsence$from,
  531.         SchoolYearRepository $schoolYearRepository,
  532.         StudentYearRepository $studentYearRepository,
  533.         AbsenceRepository $absenceRepository,
  534.         SerializerInterface $serializer
  535.     ):JsonResponse
  536.     {
  537.         $user $this->getUser();
  538.         $year $schoolYearRepository->findOneBy(['active'=>true],[]);
  539.         $parentStudent $studentYearRepository->findParentStudents($user);
  540.         $studentsIds = [];
  541.         foreach ($parentStudent as $value){
  542.             $studentsIds[] = $value->getStudent();
  543.         }
  544.         $parentAbsenceStudents $absenceRepository->findAbsenceStudents($from,$studentsIds);
  545.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  546.         $jsonPost $serializer->serialize($parentAbsenceStudents,'json',$context);
  547.         return new JsonResponse($jsonPost,Response::HTTP_OK,['accept'=>'application/json'],true);
  548.     }
  549.     /**
  550.      * Add new Absence
  551.      * @OA\Tag(name="Absence")
  552.      */
  553.     #[Route('/v3/api/prof/absence/new/user/{student}/'name'api_absence_add')]
  554.     public function addAbsence(
  555.         $student,
  556.         SubjectRepository $subjectRepository,
  557.         StudentRepository $studentRepository,
  558.         Request $request,
  559.         EntityManagerInterface $entityManager,
  560.         SchoolYearRepository $schoolYearRepository
  561.     ):Response
  562.     {
  563.         $absence = new Absence();
  564.         $year $schoolYearRepository->findOneBy(["active"=>true], []);
  565.         $_student $studentRepository->find($student);
  566.         $data $request->request->all();
  567.         $subject $subjectRepository->find($data["subject"]);
  568.         $absence->setYear($year);
  569.         $absence->setStudent($_student);
  570.         $absence->setDuration($data["duration"]);
  571.         $absence->setSubject($subject);
  572.         $absence->setAbsenceDate($data["lateDate"]);
  573.         $absence->setDuration((float)$data['duration']);
  574.         $absence->setStartTime($data['normalTms']);
  575.         $absence->setEndTime($data['arriveTms']);
  576.         $absence->setSchool($this->getUser()->getSchool());
  577.         $absence->setAuthor($this->getUser());
  578.         $this->getUser()->addAbsence($absence);
  579.         $entityManager->persist($absence);
  580.         $entityManager->persist($this->getUser());
  581.         $entityManager->flush();
  582.         $context SerializationContext::create()->setGroups(["getAbsence"]);
  583.         $jsonPost $serializer->serialize($absence,'json',$context);
  584.         return new JsonResponse($jsonPost,Response::HTTP_OK,['accept'=>'application/json'],true);
  585.     }
  586.     #[Route('/api/admin/absence/{id}/remove'name:"api_absence_remove")]
  587.     public function remove(
  588.         Absence $absence,
  589.         EntityManagerInterface $entityManager,
  590.         Request $request
  591.     ):Response
  592.     {
  593.         $entityManager->remove($absence);
  594.         $entityManager->flush();
  595.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  596.     }
  597.     /**
  598.      * Add new Absence
  599.      * @OA\Tag(name="Absence")
  600.      */
  601.     #[Route('/v3/api/prof/absence/justify/user/'name'api_absence_justify')]
  602.     public function justifyAbsence(
  603.         SubjectRepository $subjectRepository,
  604.         StudentRepository $studentRepository,
  605.         Request $request,
  606.         EntityManagerInterface $entityManager,
  607.         SchoolYearRepository $schoolYearRepository
  608.     ):Response
  609.     {
  610.         $data $request->request->all();
  611.         $school $this->getUser()->getSchool();
  612.         $absence $absenceRepository->findOneBy(['id' => $data['absence'], 'school' => $school]);
  613.         $year $schoolYearRepository->findOneBy(["active"=>true], []);
  614.         $_student $studentRepository->findOneBy(['id' => $data['student'], 'school' => $school]);
  615.         $numberToJustify $data['justify'];
  616.         $restToJustify $absence->getDuration() - $absence->getJustified();
  617.         if ($numberToJustify <= $restToJustify) {
  618.             $absence->setJustified($absence->getJustified() + $numberToJustify);
  619.             $entityManager->persist($absence);
  620.             $entityManager->flush();
  621.             $context SerializationContext::create()->setGroups(["getAbsence"]);
  622.             $jsonPost $serializer->serialize($absence,'json',$context);
  623.             return new JsonResponse($jsonPost,Response::HTTP_OK,['accept'=>'application/json'],true);
  624.         } else {
  625.             return new JsonResponse(
  626.                 $serializer->serialize(['message'=>'le nombre total d\'absence a déjà été justifier'],'json'),
  627.                 Response::HTTP_UNAUTHORIZED,['accept'=>'application/json'],
  628.                 true
  629.             );
  630.         }
  631.     }
  632.     /*
  633.      * Absence parents  mercure hub subscribe
  634.      * @OA\Tag(name="Absence")
  635.      */
  636.     #[Route(path'/v3/api/parent/{parent}/subscribe/absence/mercure/hub'name'api_parent_subscribe_mercure_hub'methods: ['GET'])]
  637.     public function getParentSubscribeMercureHub(
  638.         SerializerInterface $serializer,
  639.         Environment $twig,
  640.         $parent,
  641.         UserRepository $userRepository
  642.     ):JsonResponse
  643.     {
  644.         $_parent $userRepository->find($parent);
  645.         $parentId $_parent->getId();
  646.         $mercureHubSubscribeUrl 'https://armsyschool.com/absence/page/hub/' $parentId;
  647.         $result $twig->render('mercure/client_hub_url.html.twig',[
  648.             'url'=>$mercureHubSubscribeUrl
  649.         ]);
  650.         return new  JsonResponse($serializer->serialize([ 'mercure_client_hub_url'=>$result], 'json'),Response::HTTP_OK,['accept'=>'application/json'],true);
  651.     }
  652. }