src/Controller/AbscenceSheetController.php line 121

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Sequence;
  4. use App\Entity\Abscence;
  5. use App\Entity\AbscenceSheet;
  6. use App\Service\SchoolYearService;
  7. use App\Filter\AbscenceSearch;
  8. use App\Form\Filter\AbscenceSheetSearchType;
  9. use App\Form\AbscenceSheetType;
  10. use App\Repository\AbscenceSheetRepository;
  11. use App\Repository\AbscenceRepository;
  12. use App\Repository\ClassRoomRepository;
  13. use App\Repository\SchoolYearRepository;
  14. use App\Repository\StudentRepository;
  15. use App\Repository\SequenceRepository;
  16. use App\Repository\QuaterRepository;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  25. /**
  26.  * AbscenceSheet controller.
  27.  *
  28.  * @Route("/abscence_sheet")
  29.  */
  30. class AbscenceSheetController extends AbstractController
  31. {
  32.     private $em;
  33.     private $repo;
  34.     private $absRepo;
  35.     private $seqRepo;
  36.     private $yearRepo;
  37.     private $clRepo;
  38.     private $stdRepo;
  39.     private $qtRepo;
  40.     private SchoolYearService $schoolYearService;
  41.     public function __construct(EntityManagerInterface $emQuaterRepository $qtRepoStudentRepository $stdRepoAbscenceSheetRepository $repoAbscenceRepository $absRepoSchoolYearRepository $yearRepoSequenceRepository $seqRepoClassRoomRepository $clRepo,         SchoolYearService $schoolYearService,    )
  42.     {
  43.         $this->em $em;
  44.         $this->repo $repo;
  45.         $this->absRepo $absRepo;
  46.         $this->seqRepo $seqRepo;
  47.         $this->qtRepo $qtRepo;
  48.         $this->stdRepo $stdRepo;
  49.         $this->yearRepo $yearRepo;
  50.         $this->clRepo $clRepo;
  51.         $this->schoolYearService $schoolYearService;
  52.     }
  53.     /**
  54.      * Lists all ClassRoomme entities.
  55.      *
  56.      * @Route("/", name="admin_abscences_sheet_index", methods={"GET"})
  57.      * @Template()
  58.      */
  59.     public function index(PaginatorInterface $paginatorRequest $requestAbscenceSheetRepository $abscenceSheetRepository): Response
  60.     {
  61.         $search = new AbscenceSearch();
  62.         $searchForm =  $this->createForm(AbscenceSheetSearchType::class, $search);
  63.         $searchForm->handleRequest($request);
  64.         $year $this->schoolYearService->sessionYearById();
  65.         if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  66.           
  67.             $room $this->clRepo->findOneBy(array("id" => $_GET['room']));
  68.             $quater $this->qtRepo->findOneBy(array("id" => $_GET['quater']));
  69.             $sequence $this->seqRepo->findOneBy(array("id" => $_GET['sequence']));
  70.             if($room != null  && $sequence != null ){
  71.                  $entities $this->repo->findBy(array("sequence" => $sequence"classRoom" => $room),   array('id' => 'DESC'));
  72.             } else {
  73.                 if($room != null)
  74.                 {
  75.                     $entities $this->repo->findBy(array("classRoom" => $room),   array('id' => 'DESC'));
  76.                 }
  77.                 if($sequence != null)
  78.                 {
  79.                     $entities $this->repo->findBy(array( "sequence" => $sequence),   array('id' => 'DESC'));
  80.                 }
  81.                 if($quater != null)
  82.                 {
  83.                     $entities $this->repo->findByQuater($quater);
  84.                 }
  85.             }
  86.           
  87.         } else {
  88.             $entities $this->repo->findAbsByYear($year);
  89.         }
  90.         uasort($entities, function($a$b){
  91.             if ($a->getUpdatedAt() == $b->getUpdatedAt()) {
  92.                 return 0;
  93.             }
  94.             return ($a->getUpdatedAt()< $b->getUpdatedAt()) ? : -1;
  95.         });
  96.         $sheets $paginator->paginate($entities$request->query->get('page'1), AbscenceSheet::NUM_ITEMS_PER_PAGE);
  97.         $sheets->setCustomParameters([
  98.             'position' => 'centered',
  99.             'size' => 'large',
  100.             'rounded' => true,
  101.         ]);
  102.         return $this->render('abscence_sheet/index.html.twig', ['pagination' => $sheets'searchForm' => $searchForm->createView()]);
  103.     }
  104.     #[Route('/new'name'admin_abscence_sheet_new'methods: ['GET''POST'])]
  105.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  106.     {
  107.         $abscenceSheet = new AbscenceSheet();
  108.         $form $this->createForm(AbscenceSheetType::class, $abscenceSheet);
  109.         return $this->render('abscence_sheet/new.html.twig', array(
  110.             'abscence_sheet' => $abscenceSheet,
  111.             'response' => null,
  112.             'form' => $form->createView(),
  113.         ));
  114.     }
  115.     #[Route('/{id}/edit'name'admin_abscence_sheet_edit'methods: ['GET''POST'])]
  116.     public function edit(Request $requestAbscenceSheet $abscenceSheetEntityManagerInterface $entityManager): Response
  117.     {
  118.         $form $this->createForm(AbscenceSheetType::class, $abscenceSheet, array(
  119.             'action' => $this->generateUrl('admin_abscence_sheet_update', array('id' => $abscenceSheet->getId())),
  120.             'method' => 'PUT',
  121.         ));
  122.         $form->handleRequest($request);
  123.         $notes  = array();
  124.         $abscences $this->absRepo->findBy(array("abscenceSheet" => $abscenceSheet));
  125.         $year $this->yearRepo->findOneBy(array("activated" => true));
  126.         $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($abscenceSheet->getClassRoom(), $year);
  127.         foreach ($studentsEnrolledInClass as $std) {
  128.             foreach ($abscences as $abs) {
  129.                 if ($abs->getStudent()->getId() == $std->getId()) {
  130.                     $notes[$std->getMatricule()] = $abs;
  131.                     break;
  132.                 }
  133.             }
  134.         }
  135.         // dd($notes);
  136.         return $this->render('abscence_sheet/edit.html.twig', [
  137.             'abscences' => $notes,
  138.             'students' => $studentsEnrolledInClass,
  139.             'abscence_sheet' => $abscenceSheet,
  140.             'edit_form' => $form->createView()
  141.         ]);
  142.     }
  143.     /**
  144.      * Edits an existing Evaluation entity.
  145.      *
  146.      * @Route("/{id}/update", name="admin_abscence_sheet_update", requirements={"id"="\d+"})
  147.      * @Method("PUT")
  148.      
  149.      */
  150.     public function updateAction(AbscenceSheet $sheetRequest $request)
  151.     {
  152.         $year $this->yearRepo->findOneBy(array("activated" => true));
  153.         $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($sheet->getClassRoom(), $year);
  154.         $abscences $this->absRepo->findBy(array("abscenceSheet" => $sheet));
  155.         $newStartDate = new \DateTime($request->request->get("abscence_sheet")["startDate"]);
  156.         $newEndDate = new \DateTime($request->request->get("abscence_sheet")["endDate"]);
  157.         $sequence $this->seqRepo->findOneBy(array("id" => $request->request->get("abscence_sheet")['sequence']));
  158.         if ($newStartDate >= $sequence->getStartDate() && $newEndDate <= $sequence->getEndDate()) {
  159.             foreach ($studentsEnrolledInClass as $std) {
  160.                 $raison $_POST[$std->getMatricule() . "raison"];
  161.                 $weight $_POST[$std->getMatricule() . "weight"];
  162.                 foreach ($abscences as $abs) {
  163.                     if ($abs->getStudent()->getMatricule() == $std->getMatricule()) {
  164.                         $abs->setReason($raison);
  165.                         $abs->setWeight($weight);
  166.                         $this->em->persist($abs);
  167.                         break;
  168.                     }
  169.                 }
  170.                 $this->em->flush();
  171.                 $this->addFlash('success''Sheet succesfully updated');
  172.             }
  173.         } else {
  174.             $this->addFlash('danger''Les dates ne sont pas incluse dans la session');
  175.         }
  176.         return $this->redirect($this->generateUrl('admin_abscences_sheet_index'));
  177.     }
  178.     /**
  179.      * Displays a form to create a new Evaluation entity.
  180.      *
  181.      * @Route("/fiche", name="admin_abscence_list_students",  options = { "expose" = true })
  182.      * @Method("POST")
  183.      * @Template()
  184.      */
  185.     public function listStudentsFicheAction(Request $request)
  186.     {
  187.         if ($_POST["idClassRoom"]) {
  188.             $idClassRoom $_POST["idClassRoom"];
  189.             if ($idClassRoom != null) {
  190.                 $year $this->yearRepo->findOneBy(array("activated" => true));
  191.                 $classRoom $this->clRepo->findOneById($idClassRoom);
  192.                 // Liste des élèves inscrit dans la salle de classe sélectionnée
  193.                 $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($classRoom$year);
  194.                 if ($studentsEnrolledInClass != null) {
  195.                     return $this->render('abscence_sheet/liststudents.html.twig', array('students' => $studentsEnrolledInClass));
  196.                 }
  197.             }
  198.         }
  199.         return new Response("No Students");
  200.     }
  201.     /**
  202.      * Creates a new Evaluation entity.
  203.      *
  204.      * @Route("/create", name="admin_abscences_sheet_create")
  205.      * @Method({"POST"})
  206.      * @Template()
  207.      */
  208.     public function create(Request $request)
  209.     {
  210.         /* if (!$this->getUser()) {
  211.             $this->addFlash('warning', 'You need login first!');
  212.             return $this->redirectToRoute('app_login');
  213.         }
  214.         if (!$this->getUser()->isVerified()) {
  215.             $this->addFlash('warning', 'You need to have a verified account!');
  216.             return $this->redirectToRoute('app_login');
  217.         } */
  218.         $abscenceSheet = new AbscenceSheet();
  219.         if ($content $request->getContent()) {
  220.             $abscences json_decode($_POST['abscences'], true);
  221.             $endDate json_decode($_POST['endDate'], true);
  222.             $startDate json_decode($_POST['startDate'], true);
  223.             $room $request->request->get('idRoom');
  224.             $classRoom $this->clRepo->findOneBy(array("id" => $room));
  225.             $idSequence $request->request->get('idSequence');
  226.             $sequence $this->seqRepo->findOneBy(array("id" => $idSequence));
  227.             $abscenceSheet->setClassRoom($classRoom);
  228.             $abscenceSheet->setSequence($sequence);
  229.             $abscenceSheet->setStartDate(new \DateTime($startDate));
  230.             $abscenceSheet->setEndDate(new \DateTime($endDate));
  231.             if ((new \DateTime($startDate) <= new \DateTime($endDate) || (new \DateTime($startDate) >= $sequence->getStartDate() && new \DateTime($endDate) <= $sequence->getEndDate()))) {
  232.                 foreach ($abscences as $record) {
  233.                     $abscence = new Abscence();
  234.                     $weight $record["weight"];
  235.                     $matricule $record["matricule"];
  236.                     $student $this->stdRepo->findOneByMatricule($matricule);
  237.                     $raison $record["raison"];
  238.                     $abscence->setWeight($weight);
  239.                     if ($weight && $raison != "RAS") {
  240.                         $abscence->setJustified(true);
  241.                     }
  242.                     $abscence->setStudent($student);
  243.                     $abscence->setAbscenceSheet($abscenceSheet);
  244.                     $abscence->setReason($raison);
  245.                     $abscenceSheet->addAbscence($abscence);
  246.                     $this->em->persist($abscence);
  247.                 }
  248.                 $this->em->persist($abscenceSheet);
  249.                 $this->em->flush();
  250.             } else {
  251.                 if (new \DateTime($startDate) <= new \DateTime($endDate))
  252.                     $this->addFlash('danger''Les dates ne sont pas incluse dans la session');
  253.                 else
  254.                     $this->addFlash('danger''La date de debut doit etre anterieure a la date de fin');
  255.             }
  256.         }
  257.         return $this->redirect($this->generateUrl('admin_abscence_sheet_new'));
  258.     }
  259.     #[Route('/{id}/delete'name'admin_abscences_sheet_delete')]
  260.     public function delete(Request $requestAbscenceSheet $abscenceSheet): Response
  261.     {
  262.         if (!$this->getUser()) {
  263.             $this->addFlash('warning''You need login first!');
  264.             return $this->redirectToRoute('app_login');
  265.         }
  266.         if (!$this->getUser()->isVerified()) {
  267.             $this->addFlash('warning''You need to have a verified account!');
  268.             return $this->redirectToRoute('app_login');
  269.         }
  270.        
  271.         if ($this->isCsrfTokenValid('abscence_sheet_deletion' $abscenceSheet->getId(), $request->request->get('csrf_token'))) {
  272.             
  273.             foreach ($abscenceSheet->getAbscences() as $abs) {
  274.                 $this->em->remove($abs);
  275.             }
  276.             $this->em->remove($abscenceSheet);
  277.             $this->em->flush();
  278.             $this->addFlash('success''Sheet succesfully deleted');
  279.         }
  280.         return $this->redirectToRoute('admin_abscences_sheet_index', [], Response::HTTP_SEE_OTHER);
  281.     }
  282.     #[Route('/{id}'name'admin_abscence_sheet_show'methods: ['GET'])]
  283.     public function show(AbscenceSheet $abscenceSheet): Response
  284.     {
  285.         return $this->render('abscence_sheet/show.html.twig', [
  286.             'abscence_sheet' => $abscenceSheet,
  287.         ]);
  288.     }
  289.     /**
  290.      * Finds and displays a Evaluation entity.
  291.      *
  292.      * @Route("/{id}/pdf", name="admin_abscence_sheet_pdf", requirements={"id"="\d+"})
  293.      * @Method("GET")
  294.      * @Template()
  295.      */
  296.     public function pdfAction(AbscenceSheet $abscenceSheet\Knp\Snappy\Pdf $snappy)
  297.     {
  298.         $html $this->renderView('abscence_sheet/pdf.html.twig', array(
  299.             'abscences' => $abscenceSheet,
  300.         ));
  301.         return new Response(
  302.             $snappy->getOutputFromHtml($html, array(
  303.                 'default-header' => false
  304.             )),
  305.             200,
  306.             array(
  307.                 'Content-Type' => 'application/pdf',
  308.                 'Content-Disposition' => 'attachment; filename="' $abscenceSheet->getSequence()->getWording() . '_' $abscenceSheet->getClassRoom()->getName() . '_' $abscenceSheet->getId() . '.pdf"',
  309.             )
  310.         );
  311.     }
  312. }