src/Controller/EvaluationController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Mark;
  4. use App\Entity\Evaluation;
  5. use App\Filter\EvaluationSearch;
  6. use App\Form\EvaluationType;
  7. use App\Form\Filter\EvaluationSearchType;
  8. use App\Repository\CourseRepository;
  9. use App\Repository\StudentRepository;
  10. use App\Repository\AttributionRepository;
  11. use App\Repository\SequenceRepository;
  12. use App\Repository\ClassRoomRepository;
  13. use App\Repository\EvaluationRepository;
  14. use App\Repository\SchoolYearRepository;
  15. use App\Repository\MarkRepository;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use Knp\Snappy\Pdf;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. use App\Service\SchoolYearService;
  27. /**
  28.  * Evaluationme controller.
  29.  *
  30.  * @Route("/evaluations")
  31.  */
  32. class EvaluationController extends AbstractController
  33. {
  34.     private $em;
  35.     private EvaluationRepository $repo;
  36.     private $scRepo;
  37.     private StudentRepository $stdRepo;
  38.     private $clRepo;
  39.     private CourseRepository $crsRepo;
  40.     private $seqRepo;
  41.     private AttributionRepository $attrRepo;
  42.     private  $notes 
  43.     private MarkRepository $markRepo;
  44.     private SchoolYearService $schoolYearService;
  45.     public function __construct(
  46.         SchoolYearService $schoolYearService,
  47.         EntityManagerInterface $em,
  48.         EvaluationRepository $repo,
  49.         StudentRepository $stdRepo,
  50.         CourseRepository $crsRepo,
  51.         SchoolYearRepository $scRepo,
  52.         ClassRoomRepository $clRepo,
  53.         SequenceRepository $seqRepo,
  54.         AttributionRepository $attrRepo,
  55.         MarkRepository $markRepo
  56.     ) {
  57.         $this->em $em;
  58.         $this->repo $repo;
  59.         $this->scRepo $scRepo;
  60.         $this->stdRepo $stdRepo;
  61.         $this->notes = array();
  62.         $this->clRepo $clRepo;
  63.         $this->crsRepo $crsRepo;
  64.         $this->seqRepo $seqRepo;
  65.         $this->schoolYearService $schoolYearService;
  66.         $this->markRepo $markRepo;
  67.         $this->attrRepo $attrRepo;
  68.     }
  69.     /**
  70.      * Lists all Evaluationme entities.
  71.      *
  72.      * @Route("/", name="admin_evaluations")
  73.      * @Method("GET")
  74.      * @Template()
  75.      */
  76.     public function indexAction(PaginatorInterface $paginatorRequest $requestSessionInterface $session)
  77.     {
  78.         if (!$this->getUser()) {
  79.             $this->addFlash('warning''You need login first!');
  80.             return $this->redirectToRoute('app_login');
  81.         }
  82.         if (!$this->getUser()->isVerified()) {
  83.             $this->addFlash('warning''You need to have a verified account!');
  84.             return $this->redirectToRoute('app_login');
  85.         }
  86.         $search = new EvaluationSearch();
  87.         $searchForm =  $this->createForm(EvaluationSearchType::class, $search);
  88.         $year $this->schoolYearService->sessionYearById();
  89.         $searchForm->handleRequest($request);
  90.         if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  91.             $room $this->clRepo->findOneBy(array("id" => $_GET['room']));
  92.             $sequence $this->seqRepo->findOneBy(array("id" => $_GET['sequence']));
  93.             $course $this->crsRepo->findOneBy(array("id" => $_GET['course']));
  94.             $entities $this->repo->findEvaluations($year->getId(), $room$sequence$course);
  95.         } else {
  96.            
  97.             $entities $this->repo->findAnnualEvaluations($year->getId());
  98.         }
  99.         $evaluations $paginator->paginate($entities$request->query->get('page'1), Evaluation::NUM_ITEMS_PER_PAGE);
  100.         $evaluations->setCustomParameters([
  101.             'position' => 'centered',
  102.             'size' => 'large',
  103.             'rounded' => true,
  104.         ]);
  105.         return $this->render('evaluation/index.html.twig', ['pagination' => $evaluations'searchForm' => $searchForm->createView()]);
  106.     }
  107.     /**
  108.      * Finds and displays a Evaluationme entity.
  109.      *
  110.      * @Route("/{id}/show", name="admin_evaluations_show", requirements={"id"="\d+"})
  111.      * @Method("GET")
  112.      * @Template()
  113.      */
  114.     public function showAction(Evaluation $evaluationSessionInterface $session)
  115.     {
  116.         if (!$this->getUser()) {
  117.             $this->addFlash('warning''You need login first!');
  118.             return $this->redirectToRoute('app_login');
  119.         }
  120.         if (!$this->getUser()->isVerified()) {
  121.             $this->addFlash('warning''You need to have a verified account!');
  122.             return $this->redirectToRoute('app_login');
  123.         }
  124.         $year $this->schoolYearService->sessionYearById();
  125.         $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  126.         return $this->render('evaluation/show.html.twig', ['studentEnrolled' => $studentsEnrolledInClass'evaluation' => $evaluation]);
  127.     }
  128.     /**
  129.      * @Route("/new",name= "admin_evaluations_new", methods={"GET"})
  130.      */
  131.     public function new(Request $requestSessionInterface $session): Response
  132.     {
  133.         
  134.         if (!$this->getUser()) {
  135.             $this->addFlash('warning''You need login first!');
  136.             return $this->redirectToRoute('app_login');
  137.         }
  138.         if (!$this->getUser()->isVerified()) {
  139.             $this->addFlash('warning''You need to have a verified account!');
  140.             return $this->redirectToRoute('app_login');
  141.         }
  142.         $year $this->schoolYearService->sessionYearById();
  143.         
  144.         $evaluation = new Evaluation();
  145.         $form $this->createForm(EvaluationType::class, $evaluation);
  146.         return $this->render('evaluation/new.html.twig', array(
  147.             'evaluation' => $evaluation,
  148.             'response' => null,
  149.             'form' => $form->createView(),
  150.         ));
  151.     }
  152.     /**
  153.      * Creates a new Evaluation entity.
  154.      *
  155.      * @Route("/create", name="admin_evaluations_create")
  156.      * @Method({"POST"})
  157.      * @Template()
  158.      */
  159.     public function create(Request $requestSessionInterface $session)
  160.     {
  161.         if (!$this->getUser()) {
  162.             $this->addFlash('warning''You need login first!');
  163.             return $this->redirectToRoute('app_login');
  164.         }
  165.         if (!$this->getUser()->isVerified()) {
  166.             $this->addFlash('warning''You need to have a verified account!');
  167.             return $this->redirectToRoute('app_login');
  168.         }
  169.         $evaluation = new Evaluation();
  170.         if ($content $request->getContent()) {
  171.             $marks json_decode($_POST['marks'], true);
  172.             $notes = array();
  173.             $effectif 0;
  174.             $total 0;
  175.             $pos 0;
  176.             $room $request->request->get('idroom');
  177.             $instant $request->request->get('instant');
  178.             $idcourse $request->request->get('idcourse');
  179.             $idsequence $request->request->get('idsequence');
  180.             $competence $request->request->get('competence');
  181.             $year $this->schoolYearService->sessionYearById();
  182.             $classRoom $this->clRepo->findOneBy(array("id" => $room));
  183.             $course $this->crsRepo->findOneBy(array("id" => $idcourse));
  184.             $sequence $this->seqRepo->findOneBy(array("id" => $idsequence));
  185.             if($sequence == null)
  186.             {
  187.                 $sequence $this->seqRepo->findOneBy(array("activated" => true));
  188.             }
  189.             $evaluation->setCourse($course);
  190.             $evaluation->setClassRoom($classRoom);
  191.             $evaluation->setSequence($sequence);
  192.             $evaluation->setCompetence($competence);
  193.             foreach ($marks as $record) {
  194.                 $mark = new Mark();
  195.                 $matricule $record["matricule"];
  196.                 $note $record["note"];
  197.                 $poids $record["weight"];
  198.                 $appreciation $record["appreciation"];
  199.                 $student $this->stdRepo->findOneByMatricule($matricule);
  200.                 if (strcmp($student->getGender(), "M") == 0) {
  201.                     if ($note 10) {
  202.                         $evaluation->addFailluresH();
  203.                     } else {
  204.                         $evaluation->addSuccessH();
  205.                     }
  206.                 } else {
  207.                     if ($note 10) {
  208.                         $evaluation->addFailluresf();
  209.                     } else {
  210.                         $evaluation->addSuccessF();
  211.                     }
  212.                 }
  213.                 if ($poids == 0) {
  214.                     $evaluation->addAbscent();
  215.                 } else {
  216.                     $effectif++;
  217.                     $total += $note;
  218.                 }
  219.                 $mark->setValue($note);
  220.                 $mark->setWeight($poids);
  221.                 $mark->setAppreciation($appreciation);
  222.                 $mark->setEvaluation($evaluation);
  223.                 $mark->setStudent($student);
  224.                 $notes[$pos++] = $mark// Construction d'un arrayList pour trie
  225.                 $this->em->persist($mark);
  226.                 $evaluation->addMark($mark);
  227.             }
  228.             // analysons si l'utilisateur est autorise a enregistrer les notes sur la matiere
  229.             
  230.             // disposition des rang dans les notes
  231.             usort($notes, function ($a$b) {
  232.                 if ($a->getValue() == $b->getValue()) {
  233.                     return 0;
  234.                 }
  235.                 return ($a->getValue() < $b->getValue()) ? -1;
  236.             });
  237.             foreach ($notes as $mark) {
  238.                 $mark->setRank2($pos);
  239.                 $pos--;
  240.             }
  241.             if ($effectif != 0) {
  242.                 $evaluation->setMoyenne($total $effectif);
  243.             } else {
  244.                 $evaluation->setMoyenne(0);
  245.             }
  246.             $this->em->persist($evaluation);
  247.             $this->em->flush();
  248.         }
  249.         return $this->redirect($this->generateUrl('admin_evaluations_new'));
  250.     }
  251.     /**
  252.      * Displays a form to edit an existing Evaluationme entity.
  253.      *
  254.      * @Route("/{id}/edit", name="admin_evaluations_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  255.      * @Template()
  256.      */
  257.     public function edit(Request $requestEvaluation $evaluationSessionInterface $session): Response
  258.     {
  259.          if (!$this->getUser()) {
  260.             $this->addFlash('warning''You need login first!');
  261.             return $this->redirectToRoute('app_login');
  262.         }
  263.         if (!$this->getUser()->isVerified()) {
  264.             $this->addFlash('warning''You need to have a verified account!');
  265.             return $this->redirectToRoute('app_login');
  266.         }
  267.         /* if(($evaluation->getTeacher()!=$this->getUser()) && !($this->get('security.context')->isGranted('ROLE_ADMIN')))
  268.         {
  269.             $this->addFlash('warning', 'Access forbidden!');
  270.             return $this->redirectToRoute('app_home');
  271.         }*/
  272.         $form  $this->createForm(EvaluationType::class, $evaluation, array(
  273.             'action' => $this->generateUrl('prof_evaluations_update', array('id' => $evaluation->getId())),
  274.             'method' => 'PUT',
  275.         ));
  276.         $form->handleRequest($request);
  277.         $idcourse $request->request->get('idcourse');
  278.         $idsequence $request->request->get('idsequence');
  279.         $competence $request->request->get('competence');
  280.         $course $this->crsRepo->findOneBy(array("id" => $idcourse));
  281.         $sequence $this->seqRepo->findOneBy(array("id" => $idsequence));
  282.         if($sequence == null)
  283.         {
  284.                 $sequence $this->seqRepo->findOneBy(array("activated" => true));
  285.         }
  286.         $marks $this->markRepo->findBy(array("evaluation" => $evaluation));
  287.         $notes  = array();
  288.         $year $this->schoolYearService->sessionYearById();
  289.         $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  290.         foreach ($studentsEnrolledInClass as $std) {
  291.             foreach ($marks as $mark) {
  292.                 if ($mark->getStudent()->getId() == $std->getId()) {
  293.                     $notes[$std->getMatricule()] = $mark;
  294.                     break;
  295.                 }
  296.             }
  297.         }
  298.         // dd($marks);
  299.         /*  if($form->isSubmitted() && $form->isValid())
  300.         {
  301.             $year = $this->scRepo->findOneBy(array("activated" => true));
  302.             
  303.             $this->em->flush();
  304.             $this->addFlash('success', 'Evaluation succesfully updated');
  305.             return $this->redirectToRoute('admin_evaluations');
  306.         }*/
  307.         return $this->render('evaluation/edit.html.twig', [
  308.             'marks' => $notes,
  309.             'students' => $studentsEnrolledInClass,
  310.             'evaluation' => $evaluation,
  311.             'edit_form' => $form->createView()
  312.         ]);
  313.     }
  314.     /**
  315.      * Update a mark on an evaluation entity if the student is not absent or add a new mark if the student was absent.
  316.      */
  317.     public function editMark(Request $requestEvaluation $evaluationString $matricule)
  318.     {
  319.         if (!$this->getUser()) {
  320.             $this->addFlash('warning''You need login first!');
  321.             return $this->redirectToRoute('app_login');
  322.         }
  323.         if (!$this->getUser()->isVerified()) {
  324.             $this->addFlash('warning''You need to have a verified account!');
  325.             return $this->redirectToRoute('app_login');
  326.         }
  327.         $year $this->schoolYearService->sessionYearById();
  328.         $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  329.         $marks $this->markRepo->findBy(array("evaluation" => $evaluation));
  330.         $note $_POST[$matricule."note"];
  331.         $appr $_POST[$matricule."appr"];
  332.         $weight $_POST[$matricule."weight"];
  333.         $pos 0;
  334.         $index=0;
  335.         $found false;
  336.         while($index count($marks) && !$found)
  337.         {
  338.             if($marks[$index]->getStudent()->getMatricule() == $matricule)
  339.             {
  340.                 $found true;
  341.                 $marks[$index]->setValue($note);
  342.                 $marks[$index]->setWeight($weight);
  343.                 $marks[$index]->setAppreciation($appr);
  344.                 $this->em->persist($marks[$index]);
  345.                 $this->notes[$pos++] = $marks[$index]; // Construction d'un arrayList pour trie
  346.             }
  347.             else
  348.             {
  349.                 $index++;
  350.             }
  351.         }
  352.         if(!$found)
  353.         {
  354.             $newMark = new Mark();
  355.             $student $this->stdRepo->findOneByMatricule($matricule);
  356.             $newMark->setValue($note);
  357.             $newMark->setWeight($weight);
  358.             $newMark->setAppreciation($appr);
  359.             $newMark->setEvaluation($evaluation);
  360.             $newMark->setStudent($student);
  361.             $evaluation->addMark($newMark);
  362.             $this->em->persist($newMark);
  363.             $this->notes[$pos++] = $newMark// Construction d'un arrayList pour trie
  364.         }
  365.         $this->em->persist($evaluation);
  366.         $this->em->flush();
  367.         
  368.        
  369.     }
  370.     /**
  371.      * Edits an existing Evaluation entity.
  372.      *
  373.      * @Route("/{id}/update", name="prof_evaluations_update", requirements={"id"="\d+"})
  374.      * @Method("PUT")
  375.      
  376.      */
  377.     public function updateAction(Evaluation $evaluationRequest $requestSessionInterface $session)
  378.     {
  379.         if (!$this->getUser()) {
  380.             $this->addFlash('warning''You need login first!');
  381.             return $this->redirectToRoute('app_login');
  382.         }
  383.         if (!$this->getUser()->isVerified()) {
  384.             $this->addFlash('warning''You need to have a verified account!');
  385.             return $this->redirectToRoute('app_login');
  386.         }
  387.         $year $this->schoolYearService->sessionYearById();
  388.         $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  389.       
  390.         if ($content $request->getContent()) {
  391.             $evaluation->setFailluresF(0);
  392.             $evaluation->setFailluresH(0);
  393.             $evaluation->setSuccessF(0);
  394.             $evaluation->setSuccessH(0);
  395.             $evaluation->setAbscent(0);
  396.             $effectif 0;
  397.             $total 0;
  398.             foreach ($studentsEnrolledInClass as $std) {
  399.                 $this->editMark($request$evaluation$std->getMatricule());
  400.                 $note $_POST[$std->getMatricule()."note"];
  401.                 $weight $_POST[$std->getMatricule() . "weight"];
  402.                 if (strcmp($std->getGender(), "M") == 0) {
  403.                     if ($note 10) {
  404.                         $evaluation->addFailluresH();
  405.                     } else {
  406.                         $evaluation->addSuccessH();
  407.                     }
  408.                 } else {
  409.                     if ($note 10) {
  410.                         $evaluation->addFailluresH();
  411.                     } else {
  412.                         $evaluation->addSuccessF();
  413.                     }
  414.                 }
  415.                 if ($weight == 0) {
  416.                     $evaluation->addAbscent();
  417.                 } else {
  418.                     $effectif++;
  419.                     $total += $note;
  420.                 }
  421.              
  422.             }
  423.         }
  424.         // disposition des rang dans les notes
  425.         usort($this->notes, function ($a$b) {
  426.             if ($a->getValue() == $b->getValue()) {
  427.                 return 0;
  428.             }
  429.             return ($a->getValue() < $b->getValue()) ? -1;
  430.         });
  431.         $pos count($this->notes);
  432.         foreach ($this->notes as $mark) {
  433.             $mark->setRank2($pos);
  434.             $pos--;
  435.         }
  436.         if ($effectif != 0) {
  437.             $evaluation->setMoyenne($total $effectif);
  438.         }
  439.         $this->em->flush();
  440.         $this->addFlash('success''Evaluation succesfully updated');
  441.         return $this->redirect($this->generateUrl('admin_evaluations'));
  442.     }
  443.     /**
  444.      * Deletes a Evaluationme entity.
  445.      *
  446.      * @Route("/{id}/delete", name="admin_evaluations_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  447.      */
  448.     public function delete(Evaluation $evaluationRequest $request): Response
  449.     {
  450.         if (!$this->getUser()) {
  451.             $this->addFlash('warning''You need login first!');
  452.             return $this->redirectToRoute('app_login');
  453.         }
  454.         if (!$this->getUser()->isVerified()) {
  455.             $this->addFlash('warning''You need to have a verified account!');
  456.             return $this->redirectToRoute('app_login');
  457.         }
  458.         if (!$this->getUser()) {
  459.             $this->addFlash('warning''You need login first!');
  460.             return $this->redirectToRoute('app_login');
  461.         }
  462.         if (!$this->getUser()->isVerified()) {
  463.             $this->addFlash('warning''You need to have a verified account!');
  464.             return $this->redirectToRoute('app_login');
  465.         }
  466.         /* if($evaluation->getTeacher()!=$this->getUser())
  467.         {
  468.             $this->addFlash('warning', 'Access forbidden!');
  469.             return $this->redirectToRoute('app_home');
  470.         }*/
  471.         //   dd($this->isCsrfTokenValid('evaluations_deletion'.$evaluation->getId(), $request->request->get('csrf_token') ));
  472.         // if($this->isCsrfTokenValid('evaluations_deletion'.$evaluation->getId(), $request->request->get('csrf_token') )){
  473.         foreach ($evaluation->getMarks() as $mark) {
  474.             $this->em->remove($mark);
  475.         }
  476.         $this->em->remove($evaluation);
  477.         $this->em->flush();
  478.         $this->addFlash('info''Evaluation succesfully deleted');
  479.         // }
  480.         return $this->redirectToRoute('admin_evaluations');
  481.     }
  482.     /**
  483.      * Displays a form to create a new Evaluation entity.
  484.      *
  485.      * @Route("/fiche", name="admin_classroom_students",  options = { "expose" = true })
  486.      * @Method("POST")
  487.      * @Template()
  488.      */
  489.     public function listStudentsFicheAction(Request $requestSessionInterface $session)
  490.     {
  491.         if ($_POST["idclassroom"]) {
  492.             $idclassroom $_POST["idclassroom"];
  493.             if ($idclassroom != null) {
  494.                 $year $this->schoolYearService->sessionYearById();
  495.                 $classRoom $this->clRepo->findOneById($idclassroom);
  496.                 $coursesOfRoom $this->crsRepo->findProgrammedCoursesInClass($classRoom);
  497.                 $coursesOfConnectedUser $this->getUser()->getCourses($year);
  498.                 $courses array_intersect($coursesOfRoom$coursesOfConnectedUser);
  499.                 if ($this->isGranted('ROLE_PROF')) {
  500.                     $courses $coursesOfConnectedUser;
  501.                 }
  502.                 if ($this->isGranted('ROLE_ADMIN')) {
  503.                     $courses $coursesOfRoom;
  504.                 }
  505.                 // Liste des élèves inscrit dans la salle de classe sélectionnée
  506.                 $studentsEnrolledInClass $this->stdRepo->findEnrolledStudentsThisYearInClass($classRoom$year);
  507.                 if ($studentsEnrolledInClass != null) {
  508.                     return $this->render('evaluation/liststudents.html.twig', array('students' => $studentsEnrolledInClass'courses' => $courses));
  509.                 }
  510.             }
  511.         }
  512.         return new Response("No Students");
  513.     }
  514.     /**
  515.      * Finds and displays a Evaluation entity.
  516.      *
  517.      * @Route("/{id}/pdf", name="admin_evaluations_pdf", requirements={"id"="\d+"})
  518.      * @Method("GET")
  519.      * @Template()
  520.      */
  521.     public function pdfAction(Evaluation $evaluation\Knp\Snappy\Pdf $snappy)
  522.     {
  523.         if (!$this->getUser()) {
  524.             $this->addFlash('warning''You need login first!');
  525.             return $this->redirectToRoute('app_login');
  526.         }
  527.         if (!$this->getUser()->isVerified()) {
  528.             $this->addFlash('warning''You need to have a verified account!');
  529.             return $this->redirectToRoute('app_login');
  530.         }
  531.         $html $this->renderView('evaluation/pdf.html.twig', array(
  532.             'evaluation' => $evaluation,
  533.         ));
  534.         return new Response(
  535.             $snappy->getOutputFromHtml($html, array(
  536.                 'default-header' => false
  537.             )),
  538.             200,
  539.             array(
  540.                 'Content-Type' => 'application/pdf',
  541.                 'Content-Disposition' => 'attachment; filename="' $evaluation->getSequence()->getWording() . '_' $evaluation->getClassRoom()->getName() . '_' $evaluation->getId() . '.pdf"',
  542.             )
  543.         );
  544.     }
  545. }