src/Controller/CourseController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Course;
  4. use App\Form\CourseType;
  5. use App\Repository\CourseRepository;
  6. use App\Repository\MainTeacherRepository;
  7. use App\Repository\AttributionRepository;
  8. use App\Service\SchoolYearService;
  9. use App\Repository\ClassRoomRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. /**
  19.  * Course controller.
  20.  *
  21.  * @Route("/prof/courses")
  22.  */
  23. class CourseController extends AbstractController
  24. {
  25.     private $em;
  26.     private $repo;
  27.     private $clRepo;
  28.     private MainTeacherRepository $mainTeacherRepo;
  29.     private AttributionRepository $attRepo;
  30.     private SchoolYearService $schoolYearService;
  31.     public function __construct(AttributionRepository $attRepoMainTeacherRepository $mainTeacherRepoSchoolYearService $schoolYearServiceEntityManagerInterface $emCourseRepository $repoClassRoomRepository $clRepo)
  32.     {
  33.         $this->em $em;
  34.         $this->repo $repo;
  35.         $this->clRepo $clRepo;
  36.         $this->attRepo $attRepo;
  37.         $this->mainTeacherRepo $mainTeacherRepo;
  38.         $this->schoolYearService $schoolYearService;
  39.     }
  40.     /**
  41.      * Lists all Course entities.
  42.      *
  43.      * @Route("/", name="admin_courses")
  44.      * @Method("GET")
  45.      * @Template()
  46.      */
  47.     public function indexAction()
  48.     {
  49.         $rooms $this->clRepo->findAll();
  50.         return $this->render('course/index.html.twig'compact("rooms"));
  51.     }
  52.     /**
  53.      * Finds and displays a Course entity.
  54.      *
  55.      * @Route("/{id}/show", name="admin_courses_show", requirements={"id"="\d+"})
  56.      * @Method("GET")
  57.      * @Template()
  58.      */
  59.     public function showAction(Course $course)
  60.     {
  61.         return $this->render('course/show.html.twig'compact("course"));
  62.     }
  63.      
  64.    
  65.     /**
  66.      * @Route("/create",name="admin_courses_new", methods={"GET","POST"})
  67.      */
  68.     public function create(Request $request): Response
  69.     {
  70.         if (!$this->getUser()) {
  71.             $this->addFlash('warning''You need login first!');
  72.             return $this->redirectToRoute('app_login');
  73.         }
  74.         $course = new Course();
  75.         $form $this->createForm(CourseType::class, $course);
  76.         $form->handleRequest($request);
  77.         if ($form->isSubmitted() && $form->isValid()) {
  78.             $this->em->persist($course);
  79.             $this->em->flush();
  80.             $this->addFlash('success''Course succesfully created');
  81.             return $this->redirectToRoute('admin_courses');
  82.         }
  83.         return $this->render(
  84.             'course/new.html.twig',
  85.             ['form' => $form->createView()]
  86.         );
  87.     }
  88.     /**
  89.      * Creates a new Course entity.
  90.      *
  91.      * @Route("/create", name="admin_courses_create")
  92.      * @Method("POST")
  93.     
  94.      */
  95.     public function createAction(Request $request)
  96.     {
  97.         $course = new Course();
  98.         $form $this->createForm(new CourseType(), $course);
  99.         if ($form->handleRequest($request)->isValid()) {
  100.             $em $this->getDoctrine()->getManager();
  101.             $em->persist($course);
  102.             $em->flush();
  103.             return $this->redirect($this->generateUrl('admin_courses_show', array('id' => $course->getId())));
  104.         }
  105.         return array(
  106.             'course' => $course,
  107.             'form'   => $form->createView(),
  108.         );
  109.     }
  110.     /**
  111.      * Displays a form to edit an existing Course entity.
  112.      *
  113.      * @Route("/{id}/edit", name="admin_courses_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  114.      * @Template()
  115.      */
  116.     public function edit(Request $requestCourse $course): Response
  117.     {
  118.         $form $this->createForm(CourseType::class, $course, [
  119.             'method' => 'PUT'
  120.         ]);
  121.         $form->handleRequest($request);
  122.         if ($form->isSubmitted() && $form->isValid()) {
  123.             $this->em->flush();
  124.             $this->addFlash('success''Course succesfully updated');
  125.             return $this->redirectToRoute('admin_courses');
  126.         }
  127.         return $this->render('course/edit.html.twig', [
  128.             'course' => $course,
  129.             'form' => $form->createView()
  130.         ]);
  131.     }
  132.     /**
  133.      * Deletes a Programme entity.
  134.      *
  135.      * @Route("/{id}/delete", name="admin_courses_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  136.      */
  137.     public function delete(Course $courseRequest $request): Response
  138.     {
  139.         if ($this->isCsrfTokenValid('courses_deletion' $course->getId(), $request->request->get('csrf_token'))) {
  140.             $this->em->remove($course);
  141.             $this->em->flush();
  142.             $this->addFlash('info''Course succesfully deleted');
  143.         }
  144.         return $this->redirectToRoute('admin_courses');
  145.     }
  146.     /**
  147.      *      Check if the classroom with this course already has a main teacher 
  148.      * @Route("/check_main_teacher", name="check_main_teacher", methods={"POST"})
  149.      */
  150.     public function checkMainTeacher(Request $request)
  151.     {
  152.         $courseId $request->request->get('course_id');
  153.         $course $this->repo->findOneBy(array("id"=> $courseId));
  154.         $mainTeacher $this->mainTeacherRepo->findOneBy(array("classRoom"=> $course->getModule()->getRoom(), "schoolYear"=> $this->schoolYearService->sessionYearById()));
  155.         return new JsonResponse(['checking' => $mainTeacher===null ]);
  156.     }
  157. }