src/Controller/AbscenceSheetController.php line 135

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Sequence;
  4. use App\Entity\Abscence;
  5. use App\Entity\AbscenceSheet;
  6. use App\Service\SchoolYearService;
  7. use App\Filter\AbscenceSearch;
  8. use App\Form\Filter\AbscenceSheetSearchType;
  9. use App\Form\AbscenceSheetType;
  10. use App\Repository\AbscenceSheetRepository;
  11. use App\Repository\AbscenceRepository;
  12. use App\Repository\ClassRoomRepository;
  13. use App\Repository\SchoolYearRepository;
  14. use App\Repository\StudentRepository;
  15. use App\Repository\SequenceRepository;
  16. use App\Repository\QuaterRepository;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  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\Template;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  25. /**
  26. * AbscenceSheet controller.
  27. *
  28. * @Route("/abscence_sheet")
  29. */
  30. class AbscenceSheetController extends AbstractController
  31. {
  32. private $em;
  33. private $repo;
  34. private $absRepo;
  35. private $seqRepo;
  36. private $yearRepo;
  37. private $clRepo;
  38. private $stdRepo;
  39. private $qtRepo;
  40. private SchoolYearService $schoolYearService;
  41. public function __construct(EntityManagerInterface $em, QuaterRepository $qtRepo, StudentRepository $stdRepo, AbscenceSheetRepository $repo, AbscenceRepository $absRepo, SchoolYearRepository $yearRepo, SequenceRepository $seqRepo, ClassRoomRepository $clRepo, SchoolYearService $schoolYearService, )
  42. {
  43. $this->em = $em;
  44. $this->repo = $repo;
  45. $this->absRepo = $absRepo;
  46. $this->seqRepo = $seqRepo;
  47. $this->qtRepo = $qtRepo;
  48. $this->stdRepo = $stdRepo;
  49. $this->yearRepo = $yearRepo;
  50. $this->clRepo = $clRepo;
  51. $this->schoolYearService = $schoolYearService;
  52. }
  53. /**
  54. * Lists all ClassRoomme entities.
  55. *
  56. * @Route("/", name="admin_abscences_sheet_index", methods={"GET"})
  57. * @Template()
  58. */
  59. public function index(PaginatorInterface $paginator, Request $request, AbscenceSheetRepository $abscenceSheetRepository): Response
  60. {
  61. $search = new AbscenceSearch();
  62. $searchForm = $this->createForm(AbscenceSheetSearchType::class, $search);
  63. $searchForm->handleRequest($request);
  64. $year = $this->schoolYearService->sessionYearById();
  65. if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  66. $room = $this->clRepo->findOneBy(array("id" => $_GET['room']));
  67. $quater = $this->qtRepo->findOneBy(array("id" => $_GET['quater']));
  68. $sequence = $this->seqRepo->findOneBy(array("id" => $_GET['sequence']));
  69. if($room != null && $sequence != null ){
  70. $entities = $this->repo->findBy(array("sequence" => $sequence, "classRoom" => $room), array('id' => 'DESC'));
  71. } else {
  72. if($room != null)
  73. {
  74. $entities = $this->repo->findBy(array("classRoom" => $room), array('id' => 'DESC'));
  75. }
  76. if($sequence != null)
  77. {
  78. $entities = $this->repo->findBy(array( "sequence" => $sequence), array('id' => 'DESC'));
  79. }
  80. if($quater != null)
  81. {
  82. $entities = $this->repo->findByQuater($quater);
  83. }
  84. }
  85. } else {
  86. $entities = $this->repo->findAbsByYear($year);
  87. }
  88. uasort($entities, function($a, $b){
  89. if ($a->getUpdatedAt() == $b->getUpdatedAt()) {
  90. return 0;
  91. }
  92. return ($a->getUpdatedAt()< $b->getUpdatedAt()) ? 1 : -1;
  93. });
  94. $sheets = $paginator->paginate($entities, $request->query->get('page', 1), AbscenceSheet::NUM_ITEMS_PER_PAGE);
  95. $sheets->setCustomParameters([
  96. 'position' => 'centered',
  97. 'size' => 'large',
  98. 'rounded' => true,
  99. ]);
  100. return $this->render('abscence_sheet/index.html.twig', ['pagination' => $sheets, 'searchForm' => $searchForm->createView()]);
  101. }
  102. #[Route('/new', name: 'admin_abscence_sheet_new', methods: ['GET', 'POST'])]
  103. public function new(Request $request, EntityManagerInterface $entityManager): Response
  104. {
  105. $abscenceSheet = new AbscenceSheet();
  106. $form = $this->createForm(AbscenceSheetType::class, $abscenceSheet);
  107. return $this->render('abscence_sheet/new.html.twig', array(
  108. 'abscence_sheet' => $abscenceSheet,
  109. 'response' => null,
  110. 'form' => $form->createView(),
  111. ));
  112. }
  113. #[Route('/{id}/edit', name: 'admin_abscence_sheet_edit', methods: ['GET', 'POST'])]
  114. public function edit(Request $request, AbscenceSheet $abscenceSheet, EntityManagerInterface $entityManager): Response
  115. {
  116. $form = $this->createForm(AbscenceSheetType::class, $abscenceSheet, array(
  117. 'action' => $this->generateUrl('admin_abscence_sheet_update', array('id' => $abscenceSheet->getId())),
  118. 'method' => 'PUT',
  119. ));
  120. $form->handleRequest($request);
  121. $notes = array();
  122. $abscences = $this->absRepo->findBy(array("abscenceSheet" => $abscenceSheet));
  123. $year = $this->yearRepo->findOneBy(array("activated" => true));
  124. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($abscenceSheet->getClassRoom(), $year);
  125. foreach ($studentsEnrolledInClass as $std) {
  126. foreach ($abscences as $abs) {
  127. if ($abs->getStudent()->getId() == $std->getId()) {
  128. $notes[$std->getMatricule()] = $abs;
  129. break;
  130. }
  131. }
  132. }
  133. // dd($notes);
  134. return $this->render('abscence_sheet/edit.html.twig', [
  135. 'abscences' => $notes,
  136. 'students' => $studentsEnrolledInClass,
  137. 'abscence_sheet' => $abscenceSheet,
  138. 'edit_form' => $form->createView()
  139. ]);
  140. }
  141. /**
  142. * Edits an existing Evaluation entity.
  143. *
  144. * @Route("/{id}/update", name="admin_abscence_sheet_update", requirements={"id"="\d+"})
  145. * @Method("PUT")
  146. */
  147. public function updateAction(AbscenceSheet $sheet, Request $request)
  148. {
  149. $year = $this->yearRepo->findOneBy(array("activated" => true));
  150. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($sheet->getClassRoom(), $year);
  151. $abscences = $this->absRepo->findBy(array("abscenceSheet" => $sheet));
  152. $newStartDate = new \DateTime($request->request->get("abscence_sheet")["startDate"]);
  153. $newEndDate = new \DateTime($request->request->get("abscence_sheet")["endDate"]);
  154. $sequence = $this->seqRepo->findOneBy(array("id" => $request->request->get("abscence_sheet")['sequence']));
  155. if ($newStartDate >= $sequence->getStartDate() && $newEndDate <= $sequence->getEndDate()) {
  156. foreach ($studentsEnrolledInClass as $std) {
  157. $raison = $_POST[$std->getMatricule() . "raison"];
  158. $weight = $_POST[$std->getMatricule() . "weight"];
  159. foreach ($abscences as $abs) {
  160. if ($abs->getStudent()->getMatricule() == $std->getMatricule()) {
  161. $abs->setReason($raison);
  162. $abs->setWeight($weight);
  163. $this->em->persist($abs);
  164. break;
  165. }
  166. }
  167. $this->em->flush();
  168. $this->addFlash('success', 'Sheet succesfully updated');
  169. }
  170. } else {
  171. $this->addFlash('danger', 'Les dates ne sont pas incluse dans la session');
  172. }
  173. return $this->redirect($this->generateUrl('admin_abscences_sheet_index'));
  174. }
  175. /**
  176. * Displays a form to create a new Evaluation entity.
  177. *
  178. * @Route("/fiche", name="admin_abscence_list_students", options = { "expose" = true })
  179. * @Method("POST")
  180. * @Template()
  181. */
  182. public function listStudentsFicheAction(Request $request)
  183. {
  184. if ($_POST["idClassRoom"]) {
  185. $idClassRoom = $_POST["idClassRoom"];
  186. if ($idClassRoom != null) {
  187. $year = $this->yearRepo->findOneBy(array("activated" => true));
  188. $classRoom = $this->clRepo->findOneById($idClassRoom);
  189. // Liste des élèves inscrit dans la salle de classe sélectionnée
  190. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($classRoom, $year);
  191. if ($studentsEnrolledInClass != null) {
  192. return $this->render('abscence_sheet/liststudents.html.twig', array('students' => $studentsEnrolledInClass));
  193. }
  194. }
  195. }
  196. return new Response("No Students");
  197. }
  198. /**
  199. * Creates a new Evaluation entity.
  200. *
  201. * @Route("/create", name="admin_abscences_sheet_create")
  202. * @Method({"POST"})
  203. * @Template()
  204. */
  205. public function create(Request $request)
  206. {
  207. /* if (!$this->getUser()) {
  208. $this->addFlash('warning', 'You need login first!');
  209. return $this->redirectToRoute('app_login');
  210. }
  211. if (!$this->getUser()->isVerified()) {
  212. $this->addFlash('warning', 'You need to have a verified account!');
  213. return $this->redirectToRoute('app_login');
  214. } */
  215. $abscenceSheet = new AbscenceSheet();
  216. if ($content = $request->getContent()) {
  217. $abscences = json_decode($_POST['abscences'], true);
  218. $endDate = json_decode($_POST['endDate'], true);
  219. $startDate = json_decode($_POST['startDate'], true);
  220. $room = $request->request->get('idRoom');
  221. $classRoom = $this->clRepo->findOneBy(array("id" => $room));
  222. $idSequence = $request->request->get('idSequence');
  223. $sequence = $this->seqRepo->findOneBy(array("id" => $idSequence));
  224. $abscenceSheet->setClassRoom($classRoom);
  225. $abscenceSheet->setSequence($sequence);
  226. $abscenceSheet->setStartDate(new \DateTime($startDate));
  227. $abscenceSheet->setEndDate(new \DateTime($endDate));
  228. if ((new \DateTime($startDate) <= new \DateTime($endDate) || (new \DateTime($startDate) >= $sequence->getStartDate() && new \DateTime($endDate) <= $sequence->getEndDate()))) {
  229. foreach ($abscences as $record) {
  230. $abscence = new Abscence();
  231. $weight = $record["weight"];
  232. $matricule = $record["matricule"];
  233. $student = $this->stdRepo->findOneByMatricule($matricule);
  234. $raison = $record["raison"];
  235. $abscence->setWeight($weight);
  236. if ($weight > 0 && $raison != "RAS") {
  237. $abscence->setJustified(true);
  238. }
  239. $abscence->setStudent($student);
  240. $abscence->setAbscenceSheet($abscenceSheet);
  241. $abscence->setReason($raison);
  242. $abscenceSheet->addAbscence($abscence);
  243. $this->em->persist($abscence);
  244. }
  245. $this->em->persist($abscenceSheet);
  246. $this->em->flush();
  247. } else {
  248. if (new \DateTime($startDate) <= new \DateTime($endDate))
  249. $this->addFlash('danger', 'Les dates ne sont pas incluse dans la session');
  250. else
  251. $this->addFlash('danger', 'La date de debut doit etre anterieure a la date de fin');
  252. }
  253. }
  254. return $this->redirect($this->generateUrl('admin_abscence_sheet_new'));
  255. }
  256. #[Route('/{id}/delete', name: 'admin_abscences_sheet_delete')]
  257. public function delete(Request $request, AbscenceSheet $abscenceSheet): Response
  258. {
  259. if (!$this->getUser()) {
  260. $this->addFlash('warning', 'You need login first!');
  261. return $this->redirectToRoute('app_login');
  262. }
  263. if (!$this->getUser()->isVerified()) {
  264. $this->addFlash('warning', 'You need to have a verified account!');
  265. return $this->redirectToRoute('app_login');
  266. }
  267. if ($this->isCsrfTokenValid('abscence_sheet_deletion' . $abscenceSheet->getId(), $request->request->get('csrf_token'))) {
  268. foreach ($abscenceSheet->getAbscences() as $abs) {
  269. $this->em->remove($abs);
  270. }
  271. $this->em->remove($abscenceSheet);
  272. $this->em->flush();
  273. $this->addFlash('success', 'Sheet succesfully deleted');
  274. }
  275. return $this->redirectToRoute('admin_abscences_sheet_index', [], Response::HTTP_SEE_OTHER);
  276. }
  277. #[Route('/{id}', name: 'admin_abscence_sheet_show', methods: ['GET'])]
  278. public function show(AbscenceSheet $abscenceSheet): Response
  279. {
  280. return $this->render('abscence_sheet/show.html.twig', [
  281. 'abscence_sheet' => $abscenceSheet,
  282. ]);
  283. }
  284. /**
  285. * Finds and displays a Evaluation entity.
  286. *
  287. * @Route("/{id}/pdf", name="admin_abscence_sheet_pdf", requirements={"id"="\d+"})
  288. * @Method("GET")
  289. * @Template()
  290. */
  291. public function pdfAction(AbscenceSheet $abscenceSheet, \Knp\Snappy\Pdf $snappy)
  292. {
  293. $html = $this->renderView('abscence_sheet/pdf.html.twig', array(
  294. 'abscences' => $abscenceSheet,
  295. ));
  296. return new Response(
  297. $snappy->getOutputFromHtml($html, array(
  298. 'default-header' => false
  299. )),
  300. 200,
  301. array(
  302. 'Content-Type' => 'application/pdf',
  303. 'Content-Disposition' => 'attachment; filename="' . $abscenceSheet->getSequence()->getWording() . '_' . $abscenceSheet->getClassRoom()->getName() . '_' . $abscenceSheet->getId() . '.pdf"',
  304. )
  305. );
  306. }
  307. }