323 lines
11 KiB
PHP
Executable File
323 lines
11 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Form\FormError;
|
|
|
|
use App\Entity\Student as Entity;
|
|
use App\Entity\Responsible as Responsible;
|
|
use App\Form\StudentType as Form;
|
|
|
|
class StudentController extends AbstractController
|
|
{
|
|
private $data = "student";
|
|
private $route = "app_student";
|
|
private $render = "Student/";
|
|
private $entity = "App:Student";
|
|
|
|
public function list(Request $request)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$yearof=$em->getRepository("App:User")->getUserpreference($this->getUser(),"global","yearof");
|
|
$year=$em->getRepository("App:Year")->find($yearof);
|
|
|
|
$datas = $em->getRepository($this->entity)->findBy(["year"=>$year]);
|
|
|
|
return $this->render($this->render.'list.html.twig',[
|
|
$this->data."s" => $datas,
|
|
"useheader" => true,
|
|
"usesidebar" => true,
|
|
]);
|
|
}
|
|
|
|
|
|
public function submit($id,$by,Request $request) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
// Pas normal = idclassroom ne correspond pas un enregistrement
|
|
if($by=="classroom") {
|
|
$classroom=$em->getRepository("App:Classroom")->find($id);
|
|
if(!$classroom) return $this->redirectToRoute($this->route);
|
|
$year=$classroom->getYear();
|
|
}
|
|
else {
|
|
$yearof=$em->getRepository("App:User")->getUserpreference($this->getUser(),"global","yearof");
|
|
$year=$em->getRepository("App:Year")->find($yearof);
|
|
}
|
|
|
|
$data = new Entity();
|
|
$data->setYear($year);
|
|
if($by=="classroom") $data->setClassroom($classroom);
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class,$data,array("mode"=>"submit","by"=>$by));
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
$this->getErrorForm(null,$form,$request,$data,"submit");
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
// Gestion des responsables
|
|
$this->addResponsible($data);
|
|
|
|
// On enregistre
|
|
$em->persist($data);
|
|
$em->flush();
|
|
|
|
// Purge des responsable
|
|
$this->purgeResponsible();
|
|
|
|
// Retour à la liste
|
|
if($by=="list")
|
|
return $this->redirectToRoute($this->route);
|
|
else
|
|
return $this->redirectToRoute("app_classroom_student",["id"=>$id]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'mode' => 'submit',
|
|
'by' => $by,
|
|
'form' => $form->createView()
|
|
]);
|
|
|
|
}
|
|
|
|
public function update($id,$by,Request $request)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data=$em->getRepository($this->entity)->find($id);
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class,$data,array("mode"=>"update","by"=>$by));
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
$this->getErrorForm(null,$form,$request,$data,"update");
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
// Gestion des responsables
|
|
$this->addResponsible($data);
|
|
|
|
// On enregistre
|
|
$em->persist($data);
|
|
$em->flush();
|
|
|
|
// Purge des responsable
|
|
$this->purgeResponsible();
|
|
|
|
// Retour à la liste
|
|
if($by=="list")
|
|
return $this->redirectToRoute($this->route);
|
|
else
|
|
return $this->redirectToRoute("app_classroom_student",["id"=>$data->getClassroom()->getId()]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'by' => $by,
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function delete($id,$by,Request $request)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data=$em->getRepository($this->entity)->find($id);
|
|
|
|
// Controle avant suppression
|
|
$error=false;
|
|
if($id<0) $error=true;
|
|
|
|
if(empty($data->getScores()->isEmpty())) {
|
|
$error=true;
|
|
$request->getSession()->getFlashBag()->add("error","Impossible de supprimer cet élève : il possède des notes");
|
|
}
|
|
if($error) {
|
|
return $this->redirectToRoute($this->route."_update",["id"=>$id,"by"=>$by]);
|
|
}
|
|
else {
|
|
$idclassroom=$data->getClassroom()->getId();
|
|
|
|
$em->remove($data);
|
|
$em->flush();
|
|
|
|
// Purge des responsable
|
|
$this->purgeResponsible();
|
|
|
|
// Retour à la liste
|
|
if($by=="list")
|
|
return $this->redirectToRoute($this->route);
|
|
else
|
|
return $this->redirectToRoute("app_classroom_student",["id"=>$idclassroom]);
|
|
}
|
|
}
|
|
|
|
|
|
public function select(Request $request)
|
|
{
|
|
// S'assurer que c'est un appel ajax
|
|
if (!$request->isXmlHttpRequest()) {
|
|
return new JsonResponse(array('message' => 'Interdit'), 400);
|
|
}
|
|
|
|
$output=array();
|
|
$em = $this->getDoctrine()->getManager();
|
|
$page_limit=$request->query->get('page_limit');
|
|
$q=$request->query->get('q');
|
|
|
|
$qb = $em->createQueryBuilder();
|
|
$qb->select('student')->from($this->entity,'student')->from("App:User",'user')->from("App:Classroom",'classroom')
|
|
->where('student.user = user')
|
|
->where('student.classroom =classroom')
|
|
->where('user.lastname LIKE :value')
|
|
->where('user.firstname LIKE :value')
|
|
->setParameter("value", "%".$q."%")
|
|
->orderBy('user.firstname')
|
|
->orderBy('user.lastname');
|
|
|
|
$datas=$qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach($datas as $data) {
|
|
array_push($output,array("id"=>$data->getId(),"text"=>$data->getDisplayname()));
|
|
}
|
|
|
|
$ret_string["results"]=$output;
|
|
$response = new Response(json_encode($ret_string));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
return $response;
|
|
}
|
|
|
|
public function selectclassroom($id,Request $request)
|
|
{
|
|
// S'assurer que c'est un appel ajax
|
|
if (!$request->isXmlHttpRequest()) {
|
|
return new JsonResponse(array('message' => 'Interdit'), 400);
|
|
}
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$classroom=$em->getRepository("App:Classroom")->find($id);
|
|
if(!$classroom) return new JsonResponse(array('message' => 'Interdit'), 400);
|
|
|
|
$output=array();
|
|
$page_limit=$request->query->get('page_limit');
|
|
$q=$request->query->get('q');
|
|
|
|
$qb = $em->createQueryBuilder();
|
|
$qb->select('student')->from($this->entity,'student')->from("App:User",'user')->from("App:Classroom",'classroom')
|
|
->where('student.user = user')
|
|
->where('student.classroom = classroom');
|
|
if($q) {
|
|
$qb ->where('user.lastname LIKE :value')
|
|
->where('user.firstname LIKE :value')
|
|
->setParameter("value", "%".$q."%");
|
|
}
|
|
$qb->andwhere('classroom.id = :idclassroom')
|
|
->setParameter("idclassroom", $id)
|
|
->orderBy('user.firstname')
|
|
->orderBy('user.lastname');
|
|
|
|
$datas=$qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach($datas as $data) {
|
|
array_push($output,array("id"=>$data->getId(),"text"=>$data->getDisplayname()));
|
|
}
|
|
|
|
$ret_string["results"]=$output;
|
|
$response = new Response(json_encode($ret_string));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
return $response;
|
|
}
|
|
|
|
protected function addResponsible($data) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
// On génère les responsables
|
|
$tbresponsibles=[];
|
|
foreach($data->getResponsibleusers() as $user) {
|
|
|
|
$responsible=$em->getRepository("App:Responsible")->findOneBy(["user"=>$user,"year"=>$data->getYear()]);
|
|
if(!$responsible) {
|
|
$responsible=new Responsible();
|
|
$responsible->setYear($data->getYear());
|
|
$responsible->setUser($user);
|
|
}
|
|
|
|
$responsible->addStudent($data);
|
|
$em->persist($responsible);
|
|
array_push($tbresponsibles,$responsible->getId());
|
|
}
|
|
|
|
// On purge les responsables
|
|
foreach($data->getResponsibles() as $responsible) {
|
|
if(!in_array($responsible->getId(),$tbresponsibles)) {
|
|
$responsible->removeStudent($data);
|
|
$em->persist($responsible);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function purgeResponsible() {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
// On purge l'ensemble des responsables sans élève
|
|
$conn = $em->getConnection();
|
|
$sql = 'SELECT * FROM responsible WHERE NOT EXISTS(SELECT * FROM studentresponsible WHERE studentresponsible.responsible=responsible.id)';
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->execute();
|
|
$responsibles=$stmt->fetchAll();
|
|
foreach($responsibles as $responsableid) {
|
|
$responsible=$responsible=$em->getRepository("App:Responsible")->find($responsableid["id"]);
|
|
if($responsible) {
|
|
$em->remove($responsible);
|
|
$em->flush();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getErrorForm($id,$form,$request,$data,$mode) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
if ($form->get('submit')->isClicked() && $mode=="submit") {
|
|
// Vérifier que l'utilisateur n'est pas déjà un student
|
|
$student=$em->getRepository("App:Student")->findOneBy(["user"=>$data->getUser(),"year"=>$data->getYear()]);
|
|
if($student) {
|
|
$form->addError(new FormError('Cet utilisateur est déjà un élève'));
|
|
}
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && ($mode=="submit" || $mode=="update")) {
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && !$form->isValid()) {
|
|
$this->get('session')->getFlashBag()->clear();
|
|
|
|
$errors = $form->getErrors();
|
|
foreach( $errors as $error ) {
|
|
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|