src/Controller/EvaluationController.php line 90

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