src/Controller/StudentPreInscriptionController.php line 226

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\StudentPreInscription;
  4. use App\Entity\UserPreInscription;
  5. use App\Repository\SchoolYearRepository;
  6. use App\Form\StudentPreInscriptionType;
  7. use App\Entity\User;
  8. use App\Entity\UserYear;
  9. use App\Entity\Student;
  10. use App\Entity\StudentYear;
  11. use App\Repository\ScolarityRepository;
  12. use App\Entity\StudentScolarity;
  13. use App\Repository\StudentScolarityRepository;
  14. use App\Repository\StudentRepository;
  15. use App\Repository\UserRepository;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use App\Entity\TheClass;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use App\Repository\StudentPreInscriptionRepository;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use App\Service\UniqueSlugGeneratorService;
  26. class StudentPreInscriptionController extends AbstractController
  27. {
  28.     #[Route('/admin/pre/inscription/parent-list/{class}'name'app_student_pre_inscription_index_parent'methods: ['GET'])]
  29.     public function index(StudentPreInscriptionRepository $studentPreInscriptionRepository): Response
  30.     {
  31.         return $this->render('student_pre_inscription/index.html.twig', [
  32.             'student_pre_inscriptions' => $studentPreInscriptionRepository->findAll(),
  33.         ]);
  34.     }
  35.     #[Route('/admin/student/pre/inscription/student-list/{class}'name'app_student_pre_inscription_index_student'methods: ['GET'])]
  36.     public function indexStudent(StudentPreInscriptionRepository $studentPreInscriptionRepositorySchoolYearRepository $yearRepoTheClass $class): Response
  37.     {
  38.         return $this->render('student_pre_inscription/index.html.twig', [
  39.             'students' => $studentPreInscriptionRepository->findBy([
  40.                 'status' => 1,
  41.                 'classe' => $class
  42.             ]),
  43.             'class'=>$class,
  44.         ]);
  45.     }
  46.     #[Route('/admin/student/pre/inscription/add-stu/{id}/{class}'name'app_student_pre_inscription_validate'methods: ['GET'])]
  47.     public function addStudent(StudentRepository $studentRepositoryScolarityRepository $scolarityRepoStudentScolarityRepository $studentScolarityRepo,UniqueSlugGeneratorService $uniqueSlugGeneratorEntityManagerInterface $entityManager,UserPasswordHasherInterface $userPasswordHasher,UserRepository $userRepoSchoolYearRepository $yearRepoTranslatorInterface $translator,StudentPreInscription $student_preinscriptionTheClass $class): Response
  48.     {
  49.         $user $this->getUser();
  50.         $school $user->getSchool();
  51.         $schoolYear $yearRepo->findOneBy(['active' => true]);
  52.         $parentPreInscription $student_preinscription->getParent();
  53.         $parent1 $userRepo->findOneBy(['phone' => $parentPreInscription->getPhone()]);
  54.         if ($parent1) {
  55.             $parent $parent1;
  56.         } else {
  57.             //on commence par enregistrer le parent
  58.             $parent = new User();
  59.             $parent
  60.                     ->setFirstName($parentPreInscription->getFirstName())
  61.                     ->setLastName($parentPreInscription->getLastName())
  62.                     ->setPassword($userPasswordHasher->hashPassword($parent,'1234'))
  63.                     ->setRoles(["ROLE_PARENT"])
  64.                     ->setUserType('parent')
  65.                     ->setMatricule('1234')
  66.                     ->setAuthor($user)
  67.                     ->setSchool($school)
  68.             ;
  69.             if ($parentPreInscription->getEmail() != null) {
  70.                 $parent->setEmail($parentPreInscription->getEmail());
  71.             } 
  72.             if ($parentPreInscription->getPhone() != null) {
  73.                 $parent->setPhone($parentPreInscription->getPhone());
  74.             } else {
  75.                 $this->addFlash('error''Le parent pré-inscrit doit avoir un numéro de téléphone.');
  76.                 return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
  77.             }
  78.             $entityManager->persist($parent);
  79.             $entityManager->flush();
  80.             //on cree le UserYear
  81.             
  82.             $parentYear = new UserYear();
  83.             $parentYear->setUser($parent)
  84.                         ->setYear($schoolYear)
  85.                         ->setUserType('parent')
  86.                         ->setSchool($school)
  87.             ;
  88.             $entityManager->persist($parentYear);
  89.         }
  90.         $student = new Student();
  91.     
  92.         $student->setFirstName($student_preinscription->getFirstName())
  93.             ->setLastName($student_preinscription->getLastName())
  94.             ->setFather($student_preinscription->getFather())
  95.             ->setMother($student_preinscription->getMother())
  96.             ->setSchool($school)
  97.             ->setSexe($student_preinscription->getSexe())
  98.             ->setBornDay($student_preinscription->getBornDay())
  99.             ->setBornLocation($student_preinscription->getBornLocation())
  100.             ->setParent($parent)
  101.             ->setSchool($school)
  102.             ->setAuthor($user)
  103.         ;
  104.         if ($student_preinscription->getMatricule()) {
  105.             $student->setMatricule($student_preinscription->getMatricule());
  106.         }
  107.         $parent->addStudent($student);
  108.         $entityManager->persist($student);
  109.         $entityManager->persist($parent);
  110.         $entityManager->flush();
  111.         $studentYear = new StudentYear();
  112.         $studentYear->setClasse($class);
  113.         $studentYear->setYear($schoolYear);
  114.         $studentYear->setStudent($student);
  115.         $studentYear->setSchool($school);
  116.         $entityManager->persist($studentYear);
  117.         $entityManager->flush();
  118.         $studentYear->setSlug($uniqueSlugGenerator->generateUniqueStudentYearSlug(
  119.             $studentYear->getId(),
  120.             $studentYear->getStudent()->getFullName()
  121.         ));            
  122.         $entityManager->persist($studentYear);
  123.         $entityManager->flush();
  124.         // on cree les scolarites de l'eleve
  125.         //on recupere d'abord les tranches de scolarite de sa classe
  126.         $scolarities $scolarityRepo->findByClasse($studentYear->getClasse()->getId());
  127.         foreach ($scolarities as $scolarity) {
  128.             $studentScolarity = new StudentScolarity();
  129.             $studentScolarity->setScolarity($scolarity);
  130.             $studentScolarity->setStudent($studentYear);
  131.             $studentScolarity->setAmount(0);
  132.             $studentYear->addStudentScolarity($studentScolarity);
  133.             $scolarity->addStudentScolarity($studentScolarity);
  134.             $entityManager->persist($studentYear);
  135.             $entityManager->persist($scolarity);
  136.             $entityManager->persist($studentScolarity);
  137.             $entityManager->flush();
  138.         }
  139.         $updateStudent $studentRepository->find($student->getId());
  140.         if (empty($student->getMatricule())) {
  141.             $firstTwoDigits "F2";
  142.             $Id $student->getId();
  143.             $numberStr strval($Id);
  144.             if (strlen($numberStr) < 2) {
  145.                 // Ajouter un zéro après si le nombre a moins de deux chiffres
  146.                 $firstTwoDigits rand(1,9).$numberStr rand(10,99);
  147.             }elseif (strlen($numberStr) == 2) {
  148.                 // Ajouter un zéro après si le nombre a moins de deux chiffres
  149.                 $firstTwoDigits rand(1,9) . $numberStr rand(1,9);
  150.             } else {
  151.                 $firstTwoDigits rand(1,9) . substr($numberStr02) . rand(19);
  152.             }
  153.             $secondM "91";
  154.             $mots explode(' '$student->getFullName());
  155.             // Vérifier s'il y a au moins deux groupes de mots
  156.             if (isset($mots[1])) {
  157.                 // Récupérer le deuxième groupe de mots
  158.                 $deuxiemeGroupe $mots[1];
  159.                 $secondM $deuxiemeGroupe[0].rand(1,99);
  160.                 
  161.             } else {
  162.                 // Récupérer le deuxième groupe de mots
  163.                 $deuxiemeGroupe $mots[0];
  164.                 $secondM $deuxiemeGroupe[0].rand(1,99);
  165.             }
  166.             $student->setMatricule($firstTwoDigits $secondM);
  167.         }
  168.         $student_preinscription->setStatus(2);
  169.         $entityManager->persist($student_preinscription);
  170.         $entityManager->flush();
  171.         $this->addFlash("success",$translator->trans('votre élève et son parent on bien été ajouté'));
  172.         return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
  173.     }
  174.     #[Route('/stud/pre/inscription/next/{parent}'name'app_student_pre_inscription_next'methods: ['GET'])]
  175.     public function next(UserPreInscription $parent): Response
  176.     {
  177.         return $this->render('student_pre_inscription/next.html.twig', [
  178.             'parent' => $parent,
  179.         ]);
  180.     }
  181.     #[Route('/stud/pre/inscription/end'name'app_student_pre_inscription_end'methods: ['GET'])]
  182.     public function end(Request $request): Response
  183.     {
  184.         return $this->render('student_pre_inscription/end.html.twig');
  185.     }
  186.     #[Route('/stud/pre/inscription/new/{parent}'name'app_student_pre_inscription_new'methods: ['GET''POST'])]
  187.     public function new(Request $requestEntityManagerInterface $entityManagerUserPreInscription $parent): Response
  188.     {
  189.         $studentPreInscription = new StudentPreInscription();
  190.         $form $this->createForm(StudentPreInscriptionType::class, $studentPreInscription,['type' => 1]);
  191.         $form->handleRequest($request);
  192.         if ($form->isSubmitted() && $form->isValid()) {
  193.             $studentPreInscription->setParent($parent);
  194.             $studentPreInscription->setStatus(1);
  195.             $entityManager->persist($studentPreInscription);
  196.             $entityManager->flush();
  197.             return $this->redirectToRoute('app_student_pre_inscription_next', ['parent' => $parent->getId()], Response::HTTP_SEE_OTHER);
  198.         }
  199.         return $this->renderForm('student_pre_inscription/new.html.twig', [
  200.             'student_pre_inscription' => $studentPreInscription,
  201.             'form' => $form,
  202.         ]);
  203.     }
  204.     #[Route('/{id}'name'app_student_pre_inscription_show'methods: ['GET'])]
  205.     public function show(StudentPreInscription $studentPreInscription): Response
  206.     {
  207.         return $this->render('student_pre_inscription/show.html.twig', [
  208.             'student_pre_inscription' => $studentPreInscription,
  209.         ]);
  210.     }
  211.     #[Route('/admin/student/pre/inscription/{id}/edit/{class}'name'app_student_pre_inscription_edit'methods: ['GET''POST'])]
  212.     public function edit(Request $requestStudentPreInscription $studentPreInscription,TranslatorInterface $translatorEntityManagerInterface $entityManagerTheClass $class): Response
  213.     {
  214.         $form $this->createForm(StudentPreInscriptionType::class, $studentPreInscription, ['type' => 2]);
  215.         $form->handleRequest($request);
  216.         if ($form->isSubmitted() && $form->isValid()) {
  217.             $entityManager->flush();
  218.             $this->addFlash("success",$translator->trans('votre élève a été modifié avec succès'));
  219.             return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
  220.         }
  221.         return $this->renderForm('student_pre_inscription/edit.html.twig', [
  222.             'student_pre_inscription' => $studentPreInscription,
  223.             'form' => $form,
  224.             'class' => $class
  225.         ]);
  226.     }
  227.     #[Route('/admin/student/pre/inscription/{id}/{class}'name'app_student_pre_inscription_delete'methods: ['POST'])]
  228.     public function delete(TranslatorInterface $translatorRequest $requestStudentPreInscription $studentPreInscriptionEntityManagerInterface $entityManagerTheClass $class): Response
  229.     {
  230.         if ($this->isCsrfTokenValid('delete'.$studentPreInscription->getId(), $request->request->get('_token'))) {
  231.             $entityManager->remove($studentPreInscription);
  232.             $entityManager->flush();
  233.         }
  234.         
  235.         $this->addFlash("danger",$translator->trans('votre élève a été supprimé avec success'));
  236.         return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
  237.     }
  238. }