src/Controller/EvaluationController.php line 104

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