<?php
namespace App\Controller;
use App\Entity\StudentPreInscription;
use App\Entity\UserPreInscription;
use App\Repository\SchoolYearRepository;
use App\Form\StudentPreInscriptionType;
use App\Entity\User;
use App\Entity\UserYear;
use App\Entity\Student;
use App\Entity\StudentYear;
use App\Repository\ScolarityRepository;
use App\Entity\StudentScolarity;
use App\Repository\StudentScolarityRepository;
use App\Repository\StudentRepository;
use App\Repository\UserRepository;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use App\Entity\TheClass;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Repository\StudentPreInscriptionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\UniqueSlugGeneratorService;
class StudentPreInscriptionController extends AbstractController
{
#[Route('/admin/pre/inscription/parent-list/{class}', name: 'app_student_pre_inscription_index_parent', methods: ['GET'])]
public function index(StudentPreInscriptionRepository $studentPreInscriptionRepository): Response
{
return $this->render('student_pre_inscription/index.html.twig', [
'student_pre_inscriptions' => $studentPreInscriptionRepository->findAll(),
]);
}
#[Route('/admin/student/pre/inscription/student-list/{class}', name: 'app_student_pre_inscription_index_student', methods: ['GET'])]
public function indexStudent(StudentPreInscriptionRepository $studentPreInscriptionRepository, SchoolYearRepository $yearRepo, TheClass $class): Response
{
return $this->render('student_pre_inscription/index.html.twig', [
'students' => $studentPreInscriptionRepository->findBy([
'status' => 1,
'classe' => $class
]),
'class'=>$class,
]);
}
#[Route('/admin/student/pre/inscription/add-stu/{id}/{class}', name: 'app_student_pre_inscription_validate', methods: ['GET'])]
public function addStudent(StudentRepository $studentRepository, ScolarityRepository $scolarityRepo, StudentScolarityRepository $studentScolarityRepo,UniqueSlugGeneratorService $uniqueSlugGenerator, EntityManagerInterface $entityManager,UserPasswordHasherInterface $userPasswordHasher,UserRepository $userRepo, SchoolYearRepository $yearRepo, TranslatorInterface $translator,StudentPreInscription $student_preinscription, TheClass $class): Response
{
$user = $this->getUser();
$school = $user->getSchool();
$schoolYear = $yearRepo->findOneBy(['active' => true]);
$parentPreInscription = $student_preinscription->getParent();
$parent1 = $userRepo->findOneBy(['phone' => $parentPreInscription->getPhone()]);
if ($parent1) {
$parent = $parent1;
} else {
//on commence par enregistrer le parent
$parent = new User();
$parent
->setFirstName($parentPreInscription->getFirstName())
->setLastName($parentPreInscription->getLastName())
->setPassword($userPasswordHasher->hashPassword($parent,'1234'))
->setRoles(["ROLE_PARENT"])
->setUserType('parent')
->setMatricule('1234')
->setAuthor($user)
->setSchool($school)
;
if ($parentPreInscription->getEmail() != null) {
$parent->setEmail($parentPreInscription->getEmail());
}
if ($parentPreInscription->getPhone() != null) {
$parent->setPhone($parentPreInscription->getPhone());
} else {
$this->addFlash('error', 'Le parent pré-inscrit doit avoir un numéro de téléphone.');
return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
}
$entityManager->persist($parent);
$entityManager->flush();
//on cree le UserYear
$parentYear = new UserYear();
$parentYear->setUser($parent)
->setYear($schoolYear)
->setUserType('parent')
->setSchool($school)
;
$entityManager->persist($parentYear);
}
$student = new Student();
$student->setFirstName($student_preinscription->getFirstName())
->setLastName($student_preinscription->getLastName())
->setFather($student_preinscription->getFather())
->setMother($student_preinscription->getMother())
->setSchool($school)
->setSexe($student_preinscription->getSexe())
->setBornDay($student_preinscription->getBornDay())
->setBornLocation($student_preinscription->getBornLocation())
->setParent($parent)
->setSchool($school)
->setAuthor($user)
;
if ($student_preinscription->getMatricule()) {
$student->setMatricule($student_preinscription->getMatricule());
}
$parent->addStudent($student);
$entityManager->persist($student);
$entityManager->persist($parent);
$entityManager->flush();
$studentYear = new StudentYear();
$studentYear->setClasse($class);
$studentYear->setYear($schoolYear);
$studentYear->setStudent($student);
$studentYear->setSchool($school);
$entityManager->persist($studentYear);
$entityManager->flush();
$studentYear->setSlug($uniqueSlugGenerator->generateUniqueStudentYearSlug(
$studentYear->getId(),
$studentYear->getStudent()->getFullName()
));
$entityManager->persist($studentYear);
$entityManager->flush();
// on cree les scolarites de l'eleve
//on recupere d'abord les tranches de scolarite de sa classe
$scolarities = $scolarityRepo->findByClasse($studentYear->getClasse()->getId());
foreach ($scolarities as $scolarity) {
$studentScolarity = new StudentScolarity();
$studentScolarity->setScolarity($scolarity);
$studentScolarity->setStudent($studentYear);
$studentScolarity->setAmount(0);
$studentYear->addStudentScolarity($studentScolarity);
$scolarity->addStudentScolarity($studentScolarity);
$entityManager->persist($studentYear);
$entityManager->persist($scolarity);
$entityManager->persist($studentScolarity);
$entityManager->flush();
}
$updateStudent = $studentRepository->find($student->getId());
if (empty($student->getMatricule())) {
$firstTwoDigits = "F2";
$Id = $student->getId();
$numberStr = strval($Id);
if (strlen($numberStr) < 2) {
// Ajouter un zéro après si le nombre a moins de deux chiffres
$firstTwoDigits = rand(1,9).$numberStr . rand(10,99);
}elseif (strlen($numberStr) == 2) {
// Ajouter un zéro après si le nombre a moins de deux chiffres
$firstTwoDigits = rand(1,9) . $numberStr . rand(1,9);
} else {
$firstTwoDigits = rand(1,9) . substr($numberStr, 0, 2) . rand(1, 9);
}
$secondM = "91";
$mots = explode(' ', $student->getFullName());
// Vérifier s'il y a au moins deux groupes de mots
if (isset($mots[1])) {
// Récupérer le deuxième groupe de mots
$deuxiemeGroupe = $mots[1];
$secondM = $deuxiemeGroupe[0].rand(1,99);
} else {
// Récupérer le deuxième groupe de mots
$deuxiemeGroupe = $mots[0];
$secondM = $deuxiemeGroupe[0].rand(1,99);
}
$student->setMatricule($firstTwoDigits . $secondM);
}
$student_preinscription->setStatus(2);
$entityManager->persist($student_preinscription);
$entityManager->flush();
$this->addFlash("success",$translator->trans('votre élève et son parent on bien été ajouté'));
return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
}
#[Route('/stud/pre/inscription/next/{parent}', name: 'app_student_pre_inscription_next', methods: ['GET'])]
public function next(UserPreInscription $parent): Response
{
return $this->render('student_pre_inscription/next.html.twig', [
'parent' => $parent,
]);
}
#[Route('/stud/pre/inscription/end', name: 'app_student_pre_inscription_end', methods: ['GET'])]
public function end(Request $request): Response
{
return $this->render('student_pre_inscription/end.html.twig');
}
#[Route('/stud/pre/inscription/new/{parent}', name: 'app_student_pre_inscription_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager, UserPreInscription $parent): Response
{
$studentPreInscription = new StudentPreInscription();
$form = $this->createForm(StudentPreInscriptionType::class, $studentPreInscription,['type' => 1]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$studentPreInscription->setParent($parent);
$studentPreInscription->setStatus(1);
$entityManager->persist($studentPreInscription);
$entityManager->flush();
return $this->redirectToRoute('app_student_pre_inscription_next', ['parent' => $parent->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('student_pre_inscription/new.html.twig', [
'student_pre_inscription' => $studentPreInscription,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_student_pre_inscription_show', methods: ['GET'])]
public function show(StudentPreInscription $studentPreInscription): Response
{
return $this->render('student_pre_inscription/show.html.twig', [
'student_pre_inscription' => $studentPreInscription,
]);
}
#[Route('/admin/student/pre/inscription/{id}/edit/{class}', name: 'app_student_pre_inscription_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, StudentPreInscription $studentPreInscription,TranslatorInterface $translator, EntityManagerInterface $entityManager, TheClass $class): Response
{
$form = $this->createForm(StudentPreInscriptionType::class, $studentPreInscription, ['type' => 2]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
$this->addFlash("success",$translator->trans('votre élève a été modifié avec succès'));
return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('student_pre_inscription/edit.html.twig', [
'student_pre_inscription' => $studentPreInscription,
'form' => $form,
'class' => $class
]);
}
#[Route('/admin/student/pre/inscription/{id}/{class}', name: 'app_student_pre_inscription_delete', methods: ['POST'])]
public function delete(TranslatorInterface $translator, Request $request, StudentPreInscription $studentPreInscription, EntityManagerInterface $entityManager, TheClass $class): Response
{
if ($this->isCsrfTokenValid('delete'.$studentPreInscription->getId(), $request->request->get('_token'))) {
$entityManager->remove($studentPreInscription);
$entityManager->flush();
}
$this->addFlash("danger",$translator->trans('votre élève a été supprimé avec success'));
return $this->redirectToRoute('app_student_pre_inscription_index_student', ['class' => $class->getId()], Response::HTTP_SEE_OTHER);
}
}