src/Controller/StatisticsController.php line 196

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  5. use App\Service\SchoolYearService;
  6. use App\Repository\ClassRoomRepository;
  7. use App\Repository\StudentRepository;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use App\Entity\ClassRoom;
  16. use App\Entity\SchoolYear;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Knp\Snappy\Pdf;
  19. /**
  20. * Sequence controller.
  21. *
  22. * @Route("/admin/stats")
  23. */
  24. class StatisticsController extends AbstractController
  25. {
  26. private SchoolYearService $schoolYearService;
  27. private ClassRoomRepository $repo;
  28. private StudentRepository $stdRepo;
  29. private $em;
  30. public function __construct(EntityManagerInterface $em,StudentRepository $stdRepo,ClassRoomRepository $repo,SchoolYearService $schoolYearService)
  31. {
  32. $this->em = $em;
  33. $this->repo = $repo;
  34. $this->schoolYearService = $schoolYearService;
  35. $this->stdRepo = $stdRepo;
  36. }
  37. /**
  38. * Displays a graph updated.
  39. *
  40. * @Route("/update", name="admin_graph_update", options = { "expose" = true })
  41. * @Method("GET")
  42. * @Template()
  43. */
  44. public function updateGraphs(Request $request): JsonResponse
  45. {
  46. // URL de redirection et paramètres
  47. $url = $this->generateUrl('admin_statistics', ['id' => intval($request->query->get('id'))]);
  48. return new JsonResponse(['url' => $url]);
  49. }
  50. /**
  51. * Displays a pdf of students grouping by gender.
  52. *
  53. * @Route("printgr/{id}", name="admin_stat_print_gender_room", defaults={"id"=0} )
  54. * @Method("GET")
  55. * @Template()
  56. */
  57. public function genderRoomPdf( Pdf $pdf, int $id=0): Response
  58. {
  59. $year = $this->schoolYearService->sessionYearById();
  60. $rooms = $this->repo->findAll();
  61. if($id > 0){
  62. $rooms = $this->repo->findBy(array("id" => $id));
  63. $this->viewGender($id);
  64. } else {
  65. $this->viewGender();
  66. }
  67. $connection = $this->em->getConnection();
  68. $gender_datas = $connection->executeQuery("SELECT * FROM V_GENDER_ROOM ")->fetchAll();
  69. $html = $this->render('statistics/pdf/gender_room.html.twig', [
  70. "rooms"=>$rooms,
  71. 'year' => $year,
  72. "gender_datas"=>$gender_datas,
  73. ]);
  74. return new Response(
  75. $pdf->getOutputFromHtml($html),
  76. 200,
  77. array(
  78. 'Content-Type' => 'application/pdf',
  79. 'Content-Disposition' => 'inline; filename="stat_gender_room' .( count($rooms)==1 ? $rooms[0]->getName():"") . '.pdf"'
  80. )
  81. );
  82. }
  83. /**
  84. * Displays a pdf of students grouping by gender.
  85. *
  86. * @Route("printagr/{id}", name="admin_stat_print_age_room", defaults={"id"=0} )
  87. * @Method("GET")
  88. * @Template()
  89. */
  90. public function ageGroupRoomPdf( Pdf $pdf, int $id=0): Response
  91. {
  92. $year = $this->schoolYearService->sessionYearById();
  93. $rooms = $this->repo->findAll();
  94. if($id > 0){
  95. $rooms = $this->repo->findBy(array("id" => $id));
  96. $this->viewAgeGroup($id);
  97. } else {
  98. $this->viewAgeGroup();
  99. }
  100. $connection = $this->em->getConnection();
  101. $age_group_datas = $connection->executeQuery("SELECT * FROM V_AGE_GROUP_ROOM ")->fetchAll();
  102. $html = $this->render('statistics/pdf/age_group_room.html.twig', [
  103. "rooms"=>$rooms,
  104. 'year' => $year,
  105. "age_group_datas"=>$age_group_datas,
  106. ]);
  107. return new Response(
  108. $pdf->getOutputFromHtml($html),
  109. 200,
  110. array(
  111. 'Content-Type' => 'application/pdf',
  112. 'Content-Disposition' => 'inline; filename="stat_gender_room' .( count($rooms)==1 ? $rooms[0]->getName():"") . '.pdf"'
  113. )
  114. );
  115. }
  116. /**
  117. * Displays a pdf of students grouping by gender.
  118. *
  119. * @Route("printagrgen/{id}", name="admin_stat_print_age_room_gender", defaults={"id"=0} )
  120. * @Method("GET")
  121. * @Template()
  122. */
  123. public function ageGroupGenderRoomPdf( Pdf $pdf, int $id=0): Response
  124. {
  125. $year = $this->schoolYearService->sessionYearById();
  126. $rooms = $this->repo->findAll();
  127. $connection = $this->em->getConnection();
  128. if($id > 0){
  129. $rooms = $this->repo->findBy(array("id" => $id));
  130. $this->viewGenderAgeGroup($id);
  131. $age_group_gender_datas = $connection->executeQuery("SELECT * FROM V_AGE_GROUP_GENDER_ROOM ")->fetchAll();
  132. } else {
  133. $rooms = $this->repo->findAll();
  134. foreach($rooms as $key=>$room){
  135. $this->viewGenderAgeGroup($room->getId());
  136. $age_group_gender_datas[$room->getId()] = $connection->executeQuery("SELECT * FROM V_AGE_GROUP_GENDER_ROOM ")->fetchAll();
  137. }
  138. foreach ($rooms as $room) {
  139. $roomNames[$room->getId()] = $room->getName();
  140. }
  141. // dd($age_group_gender_datas);
  142. }
  143. $html = $this->render($id > 0 ? 'statistics/pdf/age_group_gender_room.html.twig' : 'statistics/pdf/age_group_gender_room_wide.html.twig' , [
  144. "rooms"=>$rooms,
  145. "roomNames"=>$roomNames,
  146. 'year' => $year,
  147. "minAge" => $this->findMinMaxAge()[0]["minAge"],
  148. "maxAge" => $this->findMinMaxAge()[0]["maxAge"],
  149. "age_group_gender_datas"=>$age_group_gender_datas,
  150. ]);
  151. $options = [
  152. 'orientation' => 'landscape',
  153. 'margin-top' => 0,
  154. 'margin-right' => 0,
  155. 'margin-bottom' => 0,
  156. 'margin-left' => 0,
  157. ];
  158. return new Response(
  159. $pdf->getOutputFromHtml($html, $options),
  160. 200,
  161. array(
  162. 'Content-Type' => 'application/pdf',
  163. 'Content-Disposition' => 'inline; filename="stat_gender_agegrp_room' .( count($rooms)==1 ? $rooms[0]->getName():"") . '.pdf"'
  164. )
  165. );
  166. }
  167. /**
  168. * Lists all Sequenceme entities.
  169. *
  170. * @Route("/{id}", name="admin_statistics", defaults={"id"=0})
  171. * @Method("GET")
  172. * @Template()
  173. */
  174. public function indexAction(Request $request,int $id=0 )
  175. {
  176. $rooms = $this->repo->findAll();
  177. $connection = $this->em->getConnection();
  178. // Extration des donnees de la BD
  179. if($id == 0){
  180. $this->viewGender();
  181. $this->viewAgeGroup();
  182. $this->viewGenderAgeGroup();
  183. } else {
  184. $this->viewGender($id);
  185. $this->viewAgeGroup($id);
  186. $this->viewGenderAgeGroup($id);
  187. }
  188. $gender_datas = $connection->executeQuery("SELECT * FROM V_GENDER_ROOM ")->fetchAll();
  189. $age_group_datas = $connection->executeQuery("SELECT * FROM V_AGE_GROUP_ROOM ")->fetchAll();
  190. $age_group_gender_datas = $connection->executeQuery("SELECT * FROM V_AGE_GROUP_GENDER_ROOM ")->fetchAll();
  191. // Traitements de donnees pour les graphes de repartition de sexe par classe
  192. foreach ($rooms as $room) {
  193. $roomNames[] = $room->getName();
  194. }
  195. $masculin = [];
  196. $feminin = [];
  197. foreach ($roomNames as $name) {
  198. foreach($gender_datas as $data){
  199. if(strcmp($data["room"], $name)==0 && strcmp($data["gender"], "0")==0){
  200. array_push($masculin , $data["workforce"]);
  201. }
  202. if(strcmp($data["room"], $name)==0 && strcmp($data["gender"], "1")==0){
  203. array_push($feminin , $data["workforce"]);
  204. }
  205. continue;
  206. }
  207. }
  208. // Traitement des donnees du graphes des groupes d'ages
  209. $age_groups_weight= [];
  210. $age_groups_label= [];
  211. $previousKey = null;
  212. foreach ($age_group_datas as $key=>$group) {
  213. array_push($age_groups_weight , $group["effectif"]);
  214. if ($previousKey == null) {
  215. array_push($age_groups_label , "0_".$group["tranche_age"]);
  216. } else {
  217. array_push($age_groups_label , $age_group_datas[$previousKey]["tranche_age"]."_".$group["tranche_age"]);
  218. }
  219. $previousKey = $key;
  220. }
  221. // Encodage Json
  222. $roomNames = json_encode($roomNames);
  223. if($id > 0){
  224. $roomNames = json_encode($this->repo->findOneById($id)->getName());
  225. }
  226. $ageGroupsWeight = json_encode($age_groups_weight);
  227. $ageGroupsLabel = json_encode($age_groups_label);
  228. $scatterData = $age_group_gender_datas;
  229. return $this->render('statistics/dashboard.html.twig', [
  230. "rooms"=>$rooms,
  231. "feminin"=>json_encode($feminin),
  232. "masculin"=> json_encode($masculin),
  233. "roomNames"=>$roomNames,
  234. "ageGroupsLabel"=>$ageGroupsLabel,
  235. "ageGroupsWeight"=>$ageGroupsWeight,
  236. 'scatterData' => json_encode($scatterData),
  237. ]);
  238. }
  239. // Cette fonction genere les vue d'effectif par tranche d'age par classe
  240. public function viewAgeGroup(int $room=0){
  241. $year = $this->schoolYearService->sessionYearById();
  242. $connection = $this->em->getConnection();
  243. if($room>0){
  244. $statement = $connection->prepare(
  245. " CREATE OR REPLACE VIEW V_AGE_GROUP_ROOM AS
  246. SELECT
  247. FLOOR(DATEDIFF(NOW(), birthday) / 365) AS tranche_age,
  248. COUNT(*) AS effectif
  249. FROM student std
  250. JOIN subscription sub ON sub.student_id = std.id
  251. JOIN class_room room ON sub.class_room_id = room.id
  252. WHERE sub.school_year_id =? AND room.id = ?
  253. GROUP BY
  254. tranche_age
  255. ORDER BY
  256. tranche_age;
  257. "
  258. );
  259. $statement->bindValue(2, $room);
  260. } else {
  261. $statement = $connection->prepare(
  262. " CREATE OR REPLACE VIEW V_AGE_GROUP_ROOM AS
  263. SELECT
  264. FLOOR(DATEDIFF(NOW(), birthday) / 365 ) AS tranche_age,
  265. COUNT(*) AS effectif
  266. FROM student std
  267. JOIN subscription sub ON sub.student_id = std.id
  268. JOIN class_room room ON sub.class_room_id = room.id
  269. WHERE sub.school_year_id =?
  270. GROUP BY
  271. tranche_age
  272. ORDER BY
  273. tranche_age;
  274. "
  275. );
  276. }
  277. $statement->bindValue(1, $year->getId());
  278. $statement->execute();
  279. }
  280. // Cette fonction genere les vues d'effectif par sexe par classe
  281. public function viewGender(int $room=0){
  282. $year = $this->schoolYearService->sessionYearById();
  283. $connection = $this->em->getConnection();
  284. if($room>0){
  285. $statement = $connection->prepare(
  286. " CREATE OR REPLACE VIEW V_GENDER_ROOM AS
  287. SELECT room.name as room , COUNT(std.id) as workforce, std.gender as gender
  288. FROM student std
  289. JOIN subscription sub ON sub.student_id = std.id
  290. JOIN class_room room ON sub.class_room_id = room.id
  291. WHERE sub.school_year_id =? AND room.id = ?
  292. GROUP BY gender; "
  293. );
  294. $statement->bindValue(2, $room);
  295. } else {
  296. $statement = $connection->prepare(
  297. " CREATE OR REPLACE VIEW V_GENDER_ROOM AS
  298. SELECT room.name as room , COUNT(std.id) as workforce, std.gender as gender
  299. FROM student std
  300. JOIN subscription sub ON sub.student_id = std.id
  301. JOIN class_room room ON sub.class_room_id = room.id
  302. WHERE sub.school_year_id =?
  303. GROUP BY room, gender; "
  304. );
  305. }
  306. $statement->bindValue(1, $year->getId());
  307. $statement->execute();
  308. }
  309. // Cette fonction genere les vues d'effectif par sexe par classe
  310. public function viewGenderAgeGroup(int $room=0){
  311. $year = $this->schoolYearService->sessionYearById();
  312. $connection = $this->em->getConnection();
  313. if($room>0){
  314. $statement = $connection->prepare(
  315. " CREATE OR REPLACE VIEW V_AGE_GROUP_GENDER_ROOM AS
  316. SELECT
  317. FLOOR(DATEDIFF(NOW(), birthday) / 365 ) AS age, std.gender as sexe,
  318. COUNT(std.id) AS poids
  319. FROM student std
  320. JOIN subscription sub ON sub.student_id = std.id
  321. JOIN class_room room ON sub.class_room_id = room.id
  322. WHERE sub.school_year_id =? AND room.id = ?
  323. GROUP BY
  324. std.gender, age
  325. ORDER BY
  326. age;
  327. "
  328. );
  329. $statement->bindValue(2, $room);
  330. } else {
  331. $statement = $connection->prepare(
  332. " CREATE OR REPLACE VIEW V_AGE_GROUP_GENDER_ROOM AS
  333. SELECT
  334. FLOOR(DATEDIFF(NOW(), birthday) / 365 ) AS age, std.gender as sexe,
  335. COUNT(std.id) AS poids
  336. FROM student std
  337. JOIN subscription sub ON sub.student_id = std.id
  338. JOIN class_room room ON sub.class_room_id = room.id
  339. WHERE sub.school_year_id =?
  340. GROUP BY
  341. std.gender, age
  342. ORDER BY
  343. age;
  344. "
  345. );
  346. }
  347. $statement->bindValue(1, $year->getId());
  348. $statement->execute();
  349. }
  350. public function findMinMaxAge()
  351. {
  352. $connection = $this->em->getConnection();
  353. $year = $this->schoolYearService->sessionYearById();
  354. $query = " SELECT MIN(YEAR(NOW()) - YEAR(std.birthday)-1) AS minAge,
  355. MAX(YEAR(NOW()) - YEAR(std.birthday)) AS maxAge
  356. FROM student std
  357. JOIN subscription sub ON sub.student_id = std.id
  358. WHERE sub.school_year_id = :year
  359. ";
  360. $parameters = ['year' => $year->getId()];
  361. $statement = $connection->prepare($query);
  362. $result = $statement->executeQuery($parameters)->fetchAll();
  363. return $result;
  364. }
  365. public function numberPerClass(){
  366. $connection = $this->em->getConnection();
  367. $year = $this->schoolYearService->sessionYearById();
  368. $query = " SELECT COUNT(std.id) AS poids , sub.class_room_id AS room
  369. FROM student std
  370. JOIN subscription sub ON sub.student_id = std.id
  371. WHERE sub.school_year_id = :year
  372. GROUP BY ( sub.class_room_id) ";
  373. $parameters = ['year' => $year->getId()];
  374. $statement = $connection->prepare($query);
  375. $result = $statement->executeQuery($parameters)->fetchAll();
  376. return $result;
  377. }
  378. }