src/Controller/AttributionController.php line 127

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\MainTeacher;
  4. use App\Entity\Attribution;
  5. use App\Form\AttributionType;
  6. use App\Repository\SchoolYearRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Repository\AttributionRepository;
  9. use App\Repository\MainTeacherRepository;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use App\Service\SchoolYearService;
  18. /**
  19.  * Attribution controller.
  20.  *
  21.  * @Route("/admin/attributions")
  22.  */
  23. class AttributionController extends AbstractController
  24. {
  25.     private $em;
  26.     private $repo;
  27.     private $scRepo;
  28.     private SessionInterface $session;
  29.     private SchoolYearService $schoolYearService;
  30.     private MainTeacherRepository $mainTeacherRepo;
  31.     public function __construct(MainTeacherRepository $mainTeacherRepoSchoolYearService $schoolYearService,EntityManagerInterface $emAttributionRepository $repoSchoolYearRepository $scRepoSessionInterface $session)
  32.     {
  33.         $this->em $em;
  34.         $this->repo $repo;
  35.         $this->scRepo $scRepo;
  36.         $this->session $session;
  37.         $this->schoolYearService $schoolYearService;
  38.         $this->mainTeacherRepo $mainTeacherRepo;
  39.     }
  40.     /**
  41.      * Lists all Attribution entities.
  42.      *
  43.      * @Route("/", name="admin_attributions")
  44.      * @Method("GET")
  45.      * @Template()
  46.      */
  47.     public function indexAction()
  48.     {
  49.         if (!$this->getUser()) {
  50.             $this->addFlash('warning''You need login first!');
  51.             return $this->redirectToRoute('app_login');
  52.         }
  53.         if (!$this->getUser()->isVerified()) {
  54.             $this->addFlash('warning''You need to have a verified account!');
  55.             return $this->redirectToRoute('app_login');
  56.         }
  57.         $year $this->schoolYearService->sessionYearById();
  58.         $entities $this->repo->findAllThisYear($year);
  59.         return $this->render('attribution/index.html.twig', array(
  60.             'entities' => $entities,
  61.             'year' => $year,
  62.         ));
  63.     }
  64.     public function setAttributionAction( )
  65.     {
  66.         if (!$this->getUser()) {
  67.             $this->addFlash('warning''You need login first!');
  68.             return $this->redirectToRoute('app_login');
  69.         }
  70.         if (!$this->getUser()->isVerified()) {
  71.             $this->addFlash('warning''You need to have a verified account!');
  72.             return $this->redirectToRoute('app_login');
  73.         }
  74.         $em $this->getDoctrine()->getManager();
  75.         $year $this->schoolYearService->sessionYearByCode();
  76.         $entities $this->repo->findAllThisYear($year);
  77.         foreach ($entities as $attribution) {
  78.             if ($attribution->getCourse()->getAttributions()->contains($attribution)) {
  79.                 $attribution->getCourse()->setAttributed(True);
  80.                 $em->persist($attribution->getCourse());
  81.             }
  82.         }
  83.         $em->flush();
  84.     }
  85.     /**
  86.      * Finds and displays a Attribution entity.
  87.      *
  88.      * @Route("/{id}/show", name="admin_attributions_show", requirements={"id"="\d+"})
  89.      * @Method("GET")
  90.      * @Template()
  91.      */
  92.     public function showAction(Attribution $attribution)
  93.     {
  94.         if (!$this->getUser()) {
  95.             $this->addFlash('warning''You need login first!');
  96.             return $this->redirectToRoute('app_login');
  97.         }
  98.         if (!$this->getUser()->isVerified()) {
  99.             $this->addFlash('warning''You need to have a verified account!');
  100.             return $this->redirectToRoute('app_login');
  101.         }
  102.         $deleteForm $this->createDeleteForm($attribution->getId(), 'admin_attributions_delete');
  103.         return $this->render('attribution/show.html.twig', array(
  104.             'attribution' => $attribution,
  105.             'delete_form' => $deleteForm->createView(),
  106.         ));
  107.     }
  108.    
  109.     /**
  110.      * Creates a new Section entity.
  111.      *
  112.      * @Route("/create", name="admin_attributions_new")
  113.      * @Method("POST")
  114.      */
  115.     public function createAction(Request $request)
  116.     {
  117.         
  118.         if (!$this->getUser()) {
  119.             $this->addFlash('warning''You need login first!');
  120.             return $this->redirectToRoute('app_login');
  121.         }
  122.         $attribution = new Attribution();
  123.         $form $this->createForm(AttributionType::class, $attribution);
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             $year $this->schoolYearService->sessionYearById();
  127.             $attribution->setSchoolYear($year);
  128.             $attribution->getTeacher()->addAttribution($attribution);
  129.             $attribution->getCourse()->addAttribution($attribution);
  130.             $this->setMainTeacher($attribution);
  131.             $this->em->persist($attribution);
  132.             $this->em->flush();
  133.             return $this->redirect($this->generateUrl('admin_attributions'));
  134.         }
  135.         return $this->render(
  136.             'attribution/new.html.twig',
  137.             ['form' => $form->createView()]
  138.         );
  139.     }
  140.     public function setMainTeacher(Attribution $attribution){
  141.         $year $this->schoolYearService->sessionYearById();
  142.         if($attribution->isHeadTeacher()){
  143.             $mainTeacher=$this->mainTeacherRepo->findOneBy(array("classRoom"=> $attribution->getCourse()->getModule()->getRoom(), "schoolYear"=> $this->schoolYearService->sessionYearById()));
  144.             if($mainTeacher===null){ // If there is not yet a full teacher
  145.                 $mainTeacher = new MainTeacher();
  146.                 $mainTeacher->setClassRoom($attribution->getCourse()->getModule()->getRoom());
  147.                 $mainTeacher->setSchoolYear($year);
  148.                 $attribution->getCourse()->getModule()->getRoom()->addMainTeacher($mainTeacher);
  149.             } 
  150.             $mainTeacher->setTeacher($attribution->getTeacher());
  151.             $this->em->persist($mainTeacher);
  152.         }
  153.     }
  154.     /**
  155.      * Displays a form to edit an existing Programme entity.
  156.      *
  157.      * @Route("/{id}/edit", name="admin_attributions_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  158.      * @Template()
  159.      */
  160.     public function edit(Request $requestAttribution $attribution): Response
  161.     {
  162.         if (!$this->getUser()) {
  163.             $this->addFlash('warning''You need login first!');
  164.             return $this->redirectToRoute('app_login');
  165.         }
  166.         $form $this->createForm(AttributionType::class, $attribution, [
  167.             'method' => 'PUT'
  168.         ]);
  169.         $old_attribution =  $attribution;
  170.         $form->handleRequest($request);
  171.         if ($form->isSubmitted() && $form->isValid()) {
  172.             if ($old_attribution->getTeacher()->getId() !== $attribution->getTeacher()->getId()) {
  173.                 $old_attribution->getTeacher()->removeAttribution($old_attribution);
  174.                 $attribution->getTeacher()->addAttribution($attribution);
  175.                 $this->em->persist($attribution->getTeacher());
  176.             }
  177.             $this->setMainTeacher($attribution);
  178.             if ($old_attribution->getCourse()->getId() !== $attribution->getCourse()->getId()) {
  179.                 $old_attribution->getCourse()->setAttributed(false);
  180.                 $attribution->getCourse()->setAttributed(true);
  181.                 $this->em->persist($attribution->getCourse());
  182.             }
  183.             $this->em->persist($attribution);
  184.             $this->em->flush();
  185.             $this->addFlash('success''Attribution succesfully updated');
  186.             return $this->redirectToRoute('admin_attributions');
  187.         }
  188.         return $this->render('attribution/edit.html.twig', [
  189.             'attribution' => $attribution,
  190.             'form' => $form->createView()
  191.         ]);
  192.     }
  193.     /**
  194.      * Deletes a Programme entity.
  195.      *
  196.      * @Route("/{id}/delete", name="admin_attributions_delete", requirements={"id"="\d+"}, methods={"GET","DELETE"})
  197.      */
  198.     public function delete(Attribution $attributionRequest $request): Response
  199.     {
  200.         // if($this->isCsrfTokenValid('sections_deletion'.$section->getId(), $request->request->get('crsf_token') )){
  201.         $attribution->getCourse()->removeAttribution($attribution);
  202.         $attribution->getTeacher()->removeAttribution($attribution);
  203.         $attribution->getCourse()->setAttributed(false);
  204.         $this->em->persist($attribution->getCourse());
  205.         $this->em->persist($attribution->getTeacher());
  206.         $this->em->remove($attribution);
  207.         $this->em->flush();
  208.         $this->addFlash('info''Attribution succesfully deleted');
  209.         //    }
  210.         return $this->redirectToRoute('admin_attributions');
  211.     }
  212. }