src/Controller/EvaluationController.php line 94

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Mark;
  4. use App\Entity\Evaluation;
  5. use App\Filter\EvaluationSearch;
  6. use App\Form\EvaluationType;
  7. use App\Form\Filter\EvaluationSearchType;
  8. use App\Repository\UserRepository;
  9. use App\Repository\CourseRepository;
  10. use App\Repository\StudentRepository;
  11. use App\Repository\AttributionRepository;
  12. use App\Repository\SequenceRepository;
  13. use App\Repository\ClassRoomRepository;
  14. use App\Repository\EvaluationRepository;
  15. use App\Repository\SchoolYearRepository;
  16. use App\Repository\MarkRepository;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use Knp\Snappy\Pdf;
  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\Method;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use App\Service\SchoolYearService;
  28. use Symfony\Component\Form\Forms;
  29. /**
  30. * Evaluationme controller.
  31. *
  32. * @Route("/evaluations")
  33. */
  34. class EvaluationController extends AbstractController
  35. {
  36. private $em;
  37. private EvaluationRepository $repo;
  38. private UserRepository $userRepo;
  39. private $scRepo;
  40. private StudentRepository $stdRepo;
  41. private $clRepo;
  42. private CourseRepository $crsRepo;
  43. private $seqRepo;
  44. private AttributionRepository $attrRepo;
  45. private $notes ;
  46. private MarkRepository $markRepo;
  47. private SchoolYearService $schoolYearService;
  48. public function __construct(
  49. UserRepository $userRepo,
  50. SchoolYearService $schoolYearService,
  51. EntityManagerInterface $em,
  52. EvaluationRepository $repo,
  53. StudentRepository $stdRepo,
  54. CourseRepository $crsRepo,
  55. SchoolYearRepository $scRepo,
  56. ClassRoomRepository $clRepo,
  57. SequenceRepository $seqRepo,
  58. AttributionRepository $attrRepo,
  59. MarkRepository $markRepo
  60. ) {
  61. $this->em = $em;
  62. $this->repo = $repo;
  63. $this->scRepo = $scRepo;
  64. $this->stdRepo = $stdRepo;
  65. $this->notes = array();
  66. $this->clRepo = $clRepo;
  67. $this->crsRepo = $crsRepo;
  68. $this->seqRepo = $seqRepo;
  69. $this->schoolYearService = $schoolYearService;
  70. $this->markRepo = $markRepo;
  71. $this->attrRepo = $attrRepo;
  72. $this->userRepo = $userRepo;
  73. }
  74. /**
  75. * Lists all Evaluationme entities.
  76. *
  77. * @Route("/", name="admin_evaluations")
  78. * @Method("GET")
  79. * @Template()
  80. */
  81. public function indexAction(PaginatorInterface $paginator, Request $request, SessionInterface $session)
  82. {
  83. if (!$this->getUser()) {
  84. $this->addFlash('warning', 'You need login first!');
  85. return $this->redirectToRoute('app_login');
  86. }
  87. if (!$this->getUser()->isVerified()) {
  88. $this->addFlash('warning', 'You need to have a verified account!');
  89. return $this->redirectToRoute('app_login');
  90. }
  91. $search = new EvaluationSearch();
  92. $searchForm = $this->createForm(EvaluationSearchType::class, $search);
  93. $year = $this->schoolYearService->sessionYearById();
  94. $searchForm->handleRequest($request);
  95. if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  96. $room = $this->clRepo->findOneBy(array("id" => $_GET['room']));
  97. $sequence = $this->seqRepo->findOneBy(array("id" => $_GET['sequence']));
  98. $course = $this->crsRepo->findOneBy(array("id" => $_GET['course']));
  99. $entities = $this->repo->findEvaluations($year->getId(), $room, $sequence, $course);
  100. } else {
  101. $entities = $this->repo->findAnnualEvaluations($year->getId());
  102. }
  103. $evaluations = $paginator->paginate($entities, $request->query->get('page', 1), Evaluation::NUM_ITEMS_PER_PAGE);
  104. $evaluations->setCustomParameters([
  105. 'position' => 'centered',
  106. 'size' => 'large',
  107. 'rounded' => true,
  108. ]);
  109. return $this->render('evaluation/index.html.twig', ['pagination' => $evaluations, 'searchForm' => $searchForm->createView()]);
  110. }
  111. /**
  112. * Finds and displays a Evaluationme entity.
  113. *
  114. * @Route("/{id}/show", name="admin_evaluations_show", requirements={"id"="\d+"})
  115. * @Method("GET")
  116. * @Template()
  117. */
  118. public function showAction(Evaluation $evaluation, SessionInterface $session)
  119. {
  120. if (!$this->getUser()) {
  121. $this->addFlash('warning', 'You need login first!');
  122. return $this->redirectToRoute('app_login');
  123. }
  124. if (!$this->getUser()->isVerified()) {
  125. $this->addFlash('warning', 'You need to have a verified account!');
  126. return $this->redirectToRoute('app_login');
  127. }
  128. $year = $this->schoolYearService->sessionYearById();
  129. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  130. return $this->render('evaluation/show.html.twig', ['studentEnrolled' => $studentsEnrolledInClass, 'evaluation' => $evaluation]);
  131. }
  132. /**
  133. * @Route("/new",name= "admin_evaluations_new", methods={"GET"})
  134. */
  135. public function new(Request $request, SessionInterface $session): Response
  136. {
  137. if (!$this->getUser()) {
  138. $this->addFlash('warning', 'You need login first!');
  139. return $this->redirectToRoute('app_login');
  140. }
  141. if (!$this->getUser()->isVerified()) {
  142. $this->addFlash('warning', 'You need to have a verified account!');
  143. return $this->redirectToRoute('app_login');
  144. }
  145. $year = $this->schoolYearService->sessionYearById();
  146. $evaluation = new Evaluation();
  147. $form = $this->createForm(EvaluationType::class, $evaluation);
  148. return $this->render('evaluation/new.html.twig', array(
  149. 'evaluation' => $evaluation,
  150. 'response' => null,
  151. 'form' => $form->createView(),
  152. ));
  153. }
  154. /**
  155. * Creates a new Evaluation entity.
  156. *
  157. * @Route("/create", name="admin_evaluations_create")
  158. * @Method({"POST"})
  159. * @Template()
  160. */
  161. public function create(Request $request, SessionInterface $session)
  162. {
  163. if (!$this->getUser()) {
  164. $this->addFlash('warning', 'You need login first!');
  165. return $this->redirectToRoute('app_login');
  166. }
  167. if (!$this->getUser()->isVerified()) {
  168. $this->addFlash('warning', 'You need to have a verified account!');
  169. return $this->redirectToRoute('app_login');
  170. }
  171. $evaluation = new Evaluation();
  172. if ($content = $request->getContent()) {
  173. $marks = json_decode($_POST['marks'], true);
  174. $notes = array();
  175. $effectif = 0;
  176. $total = 0;
  177. $pos = 0;
  178. $room = $request->request->get('idroom');
  179. $idcourse = $request->request->get('idcourse');
  180. $idsequence = $request->request->get('idsequence');
  181. $competence = $request->request->get('competence');
  182. $year = $this->schoolYearService->sessionYearById();
  183. $classRoom = $this->clRepo->findOneBy(array("id" => $room));
  184. $course = $this->crsRepo->findOneBy(array("id" => $idcourse));
  185. $sequence = $this->seqRepo->findOneBy(array("id" => $idsequence));
  186. if($sequence == null)
  187. {
  188. $sequence = $this->seqRepo->findOneBy(array("activated" => true));
  189. }
  190. $evaluation->setCourse($course);
  191. $evaluation->setAuthor($this->getUser());
  192. $evaluation->setClassRoom($classRoom);
  193. $evaluation->setSequence($sequence);
  194. $evaluation->setCompetence($competence);
  195. foreach ($marks as $record) {
  196. $mark = new Mark();
  197. $matricule = $record["matricule"];
  198. $note = $record["note"];
  199. $poids = $record["weight"];
  200. $appreciation = $record["appreciation"];
  201. $student = $this->stdRepo->findOneByMatricule($matricule);
  202. if (strcmp($student->getGender(), "M") == 0) {
  203. if ($note < 10) {
  204. $evaluation->addFailluresH();
  205. } else {
  206. $evaluation->addSuccessH();
  207. }
  208. } else {
  209. if ($note < 10) {
  210. $evaluation->addFailluresf();
  211. } else {
  212. $evaluation->addSuccessF();
  213. }
  214. }
  215. if ($poids == 0) {
  216. $evaluation->addAbscent();
  217. } else {
  218. $effectif++;
  219. $total += $note;
  220. }
  221. $mark->setValue($note);
  222. $mark->setWeight($poids);
  223. $mark->setAppreciation($appreciation);
  224. $mark->setEvaluation($evaluation);
  225. $mark->setStudent($student);
  226. $notes[$pos++] = $mark; // Construction d'un arrayList pour trie
  227. $this->em->persist($mark);
  228. $evaluation->addMark($mark);
  229. }
  230. // analysons si l'utilisateur est autorise a enregistrer les notes sur la matiere
  231. // disposition des rang dans les notes
  232. usort($notes, function ($a, $b) {
  233. if ($a->getValue() == $b->getValue()) {
  234. return 0;
  235. }
  236. return ($a->getValue() < $b->getValue()) ? -1 : 1;
  237. });
  238. $evaluation->setMini($notes[0]->getValue());
  239. if($effectif>1){
  240. $evaluation->setMaxi($notes[$effectif-1]->getValue());
  241. } else {
  242. $evaluation->setMaxi(0);
  243. }
  244. foreach ($notes as $mark) {
  245. $mark->setRank2($pos);
  246. $pos--;
  247. }
  248. if ($effectif != 0) {
  249. $evaluation->setMoyenne($total / $effectif);
  250. } else {
  251. $evaluation->setMoyenne(0);
  252. }
  253. $this->em->persist($evaluation);
  254. $this->em->flush();
  255. }
  256. return $this->redirect($this->generateUrl('admin_evaluations_new'));
  257. }
  258. /**
  259. * Displays a form to edit an existing Evaluationme entity.
  260. *
  261. * @Route("/{id}/edit", name="admin_evaluations_edit", requirements={"id"="\d+"}, methods={"GET","PUT"})
  262. * @Template()
  263. */
  264. public function edit(Request $request, Evaluation $evaluation, SessionInterface $session): Response
  265. {
  266. if (!$this->getUser()) {
  267. $this->addFlash('warning', 'You need login first!');
  268. return $this->redirectToRoute('app_login');
  269. }
  270. if (!$this->getUser()->isVerified()) {
  271. $this->addFlash('warning', 'You need to have a verified account!');
  272. return $this->redirectToRoute('app_login');
  273. }
  274. if(($evaluation->getAuthor()!=$this->getUser()) && !($this->isGranted('ROLE_ADMIN')))
  275. {
  276. $this->addFlash('warning', 'Access forbidden!');
  277. return $this->redirectToRoute('admin_evaluations');
  278. }
  279. $form = $this->createForm(EvaluationType::class, $evaluation, array(
  280. 'action' => $this->generateUrl('prof_evaluations_update', array('id' => $evaluation->getId())),
  281. 'method' => 'PUT',
  282. ));
  283. $form->handleRequest($request);
  284. $sequence = $evaluation->getSequence();
  285. $marks = $this->markRepo->findBy(array("evaluation" => $evaluation));
  286. $notes = array();
  287. $year = $this->schoolYearService->sessionYearById();
  288. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  289. foreach ($studentsEnrolledInClass as $std) {
  290. foreach ($marks as $mark) {
  291. if ($mark->getStudent()->getId() == $std->getId()) {
  292. $notes[$std->getMatricule()] = $mark;
  293. break;
  294. }
  295. }
  296. }
  297. return $this->render('evaluation/edit.html.twig', [
  298. 'marks' => $notes,
  299. 'students' => $studentsEnrolledInClass,
  300. 'evaluation' => $evaluation,
  301. 'edit_form' => $form->createView()
  302. ]);
  303. }
  304. /**
  305. * Update a mark on an evaluation entity if the student is not absent or add a new mark if the student was absent.
  306. */
  307. public function editMark(Request $request, Evaluation $evaluation, String $matricule)
  308. {
  309. if (!$this->getUser()) {
  310. $this->addFlash('warning', 'You need login first!');
  311. return $this->redirectToRoute('app_login');
  312. }
  313. if (!$this->getUser()->isVerified()) {
  314. $this->addFlash('warning', 'You need to have a verified account!');
  315. return $this->redirectToRoute('app_login');
  316. }
  317. $year = $this->schoolYearService->sessionYearById();
  318. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  319. $marks = $this->markRepo->findBy(array("evaluation" => $evaluation));
  320. $note = $_POST[$matricule."note"];
  321. $appr = $_POST[$matricule."appr"];
  322. $weight = $_POST[$matricule."weight"];
  323. $pos = 0;
  324. $index=0;
  325. $found = false;
  326. while($index < count($marks) && !$found)
  327. {
  328. if($marks[$index]->getStudent()->getMatricule() == $matricule)
  329. {
  330. $found = true;
  331. $marks[$index]->setValue($note);
  332. $marks[$index]->setWeight($weight);
  333. $marks[$index]->setAppreciation($appr);
  334. $this->em->persist($marks[$index]);
  335. $this->notes[$pos++] = $marks[$index]; // Construction d'un arrayList pour trie
  336. }
  337. else
  338. {
  339. $index++;
  340. }
  341. }
  342. if(!$found)
  343. {
  344. $newMark = new Mark();
  345. $student = $this->stdRepo->findOneByMatricule($matricule);
  346. $newMark->setValue($note);
  347. $newMark->setWeight($weight);
  348. $newMark->setAppreciation($appr);
  349. $newMark->setEvaluation($evaluation);
  350. $newMark->setStudent($student);
  351. $evaluation->addMark($newMark);
  352. $this->em->persist($newMark);
  353. $this->notes[$pos++] = $newMark; // Construction d'un arrayList pour trie
  354. }
  355. $evaluation->setMini($this->notes[0]->getValue());
  356. $evaluation->setMaxi($this->notes[$pos-1]->getValue());
  357. $evaluation->setAuthor($this->getUser());
  358. $this->em->persist($evaluation);
  359. $this->em->flush();
  360. }
  361. /**
  362. * Edits an existing Evaluation entity.
  363. *
  364. * @Route("/{id}/update", name="prof_evaluations_update", requirements={"id"="\d+"})
  365. * @Method("PUT")
  366. */
  367. public function updateAction(Evaluation $evaluation, Request $request, SessionInterface $session)
  368. {
  369. if (!$this->getUser()) {
  370. $this->addFlash('warning', 'You need login first!');
  371. return $this->redirectToRoute('app_login');
  372. }
  373. if (!$this->getUser()->isVerified()) {
  374. $this->addFlash('warning', 'You need to have a verified account!');
  375. return $this->redirectToRoute('app_login');
  376. }
  377. $year = $this->schoolYearService->sessionYearById();
  378. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($evaluation->getClassRoom(), $year);
  379. if ($content = $request->getContent()) {
  380. $competence = ($request->request->get("evaluation")["competence"]);
  381. $evaluation->setCompetence($competence);
  382. $evaluation->setFailluresF(0);
  383. $evaluation->setFailluresH(0);
  384. $evaluation->setSuccessF(0);
  385. $evaluation->setSuccessH(0);
  386. $evaluation->setAbscent(0);
  387. $effectif = 0;
  388. $total = 0;
  389. foreach ($studentsEnrolledInClass as $std) {
  390. $this->editMark($request, $evaluation, $std->getMatricule());
  391. $note = $_POST[$std->getMatricule()."note"];
  392. $weight = $_POST[$std->getMatricule() . "weight"];
  393. if (strcmp($std->getGender(), "M") == 0) {
  394. if ($note < 10) {
  395. $evaluation->addFailluresH();
  396. } else {
  397. $evaluation->addSuccessH();
  398. }
  399. } else {
  400. if ($note < 10) {
  401. $evaluation->addFailluresH();
  402. } else {
  403. $evaluation->addSuccessF();
  404. }
  405. }
  406. if ($weight == 0) {
  407. $evaluation->addAbscent();
  408. } else {
  409. $effectif++;
  410. $total += $note;
  411. }
  412. }
  413. }
  414. // disposition des rang dans les notes
  415. usort($this->notes, function ($a, $b) {
  416. if ($a->getValue() == $b->getValue()) {
  417. return 0;
  418. }
  419. return ($a->getValue() < $b->getValue()) ? -1 : 1;
  420. });
  421. $pos = count($this->notes);
  422. foreach ($this->notes as $mark) {
  423. $mark->setRank2($pos);
  424. $pos--;
  425. }
  426. if ($effectif != 0) {
  427. $evaluation->setMoyenne($total / $effectif);
  428. }
  429. $this->em->flush();
  430. $this->addFlash('success', 'Evaluation succesfully updated');
  431. return $this->redirect($this->generateUrl('admin_evaluations'));
  432. }
  433. /**
  434. * Deletes a Evaluationme entity.
  435. *
  436. * @Route("/{id}/delete", name="admin_evaluations_delete", requirements={"id"="\d+"}, methods={"DELETE"})
  437. */
  438. public function delete(Evaluation $evaluation, Request $request): Response
  439. {
  440. if (!$this->getUser()) {
  441. $this->addFlash('warning', 'You need login first!');
  442. return $this->redirectToRoute('app_login');
  443. }
  444. if (!$this->getUser()->isVerified()) {
  445. $this->addFlash('warning', 'You need to have a verified account!');
  446. return $this->redirectToRoute('app_login');
  447. }
  448. if (!$this->getUser()) {
  449. $this->addFlash('warning', 'You need login first!');
  450. return $this->redirectToRoute('app_login');
  451. }
  452. if (!$this->getUser()->isVerified()) {
  453. $this->addFlash('warning', 'You need to have a verified account!');
  454. return $this->redirectToRoute('app_login');
  455. }
  456. /* if($evaluation->getTeacher()!=$this->getUser())
  457. {
  458. $this->addFlash('warning', 'Access forbidden!');
  459. return $this->redirectToRoute('app_home');
  460. }*/
  461. // dd($this->isCsrfTokenValid('evaluations_deletion'.$evaluation->getId(), $request->request->get('csrf_token') ));
  462. // if($this->isCsrfTokenValid('evaluations_deletion'.$evaluation->getId(), $request->request->get('csrf_token') )){
  463. foreach ($evaluation->getMarks() as $mark) {
  464. $this->em->remove($mark);
  465. }
  466. $this->em->remove($evaluation);
  467. $this->em->flush();
  468. $this->addFlash('info', 'Evaluation succesfully deleted');
  469. // }
  470. return $this->redirectToRoute('admin_evaluations');
  471. }
  472. /**
  473. * Displays a form to create a new Evaluation entity.
  474. *
  475. * @Route("/fiche", name="admin_classroom_students", options = { "expose" = true })
  476. * @Method("POST")
  477. * @Template()
  478. */
  479. public function listStudentsFicheAction(Request $request, SessionInterface $session)
  480. {
  481. if ($_POST["idclassroom"] ) {
  482. $idclassroom = $_POST["idclassroom"];
  483. if ($idclassroom != null) {
  484. $idsequence = isset($_POST['idsequence']) ? $_POST["idsequence"] : null;
  485. $year = $this->schoolYearService->sessionYearById();
  486. $classRoom = $this->clRepo->findOneById($idclassroom);
  487. $sequence = ($idsequence != null)? $this->seqRepo->findOneById($idsequence) : $this->seqRepo->findOneBy(array("activated" => true));
  488. $coursesOfRoom = $this->crsRepo->findProgrammedCoursesInClassAndNoYetEvaluated($classRoom, $sequence);
  489. $coursesOfConnectedUser = $this->getUser()->getCourses($year);
  490. if ($this->isGranted('ROLE_PROF')) {
  491. $courses = array_intersect($coursesOfRoom, $coursesOfConnectedUser);
  492. }
  493. if ($this->isGranted('ROLE_ADMIN')) {
  494. $courses = $coursesOfRoom;
  495. }
  496. // Liste des élèves inscrit dans la salle de classe sélectionnée
  497. $studentsEnrolledInClass = $this->stdRepo->findEnrolledStudentsThisYearInClass($classRoom, $year);
  498. if ($studentsEnrolledInClass != null) {
  499. return $this->render('evaluation/liststudents.html.twig', array('students' => $studentsEnrolledInClass, 'courses' => $courses));
  500. }
  501. }
  502. }
  503. return new Response("No Students");
  504. }
  505. /**
  506. * Finds and displays a Evaluation entity.
  507. *
  508. * @Route("/{id}/pdf", name="admin_evaluations_pdf", requirements={"id"="\d+"})
  509. * @Method("GET")
  510. * @Template()
  511. */
  512. public function pdfAction(Evaluation $evaluation, \Knp\Snappy\Pdf $snappy)
  513. {
  514. if (!$this->getUser()) {
  515. $this->addFlash('warning', 'You need login first!');
  516. return $this->redirectToRoute('app_login');
  517. }
  518. if (!$this->getUser()->isVerified()) {
  519. $this->addFlash('warning', 'You need to have a verified account!');
  520. return $this->redirectToRoute('app_login');
  521. }
  522. $author = $this->userRepo->findOneBy(["id"=>$evaluation->getAuthor()->getId()]);
  523. $html = $this->renderView('evaluation/pdf.html.twig', array(
  524. 'evaluation' => $evaluation,
  525. 'author' => $author
  526. ));
  527. return new Response(
  528. $snappy->getOutputFromHtml($html, array(
  529. 'default-header' => false
  530. )),
  531. 200,
  532. array(
  533. 'Content-Type' => 'application/pdf',
  534. 'Content-Disposition' => 'attachment; filename="' . $evaluation->getSequence()->getWording() . '_' . $evaluation->getClassRoom()->getName() . '_' . $evaluation->getId() . '.pdf"',
  535. )
  536. );
  537. }
  538. }