202 lines
6.6 KiB
PHP
Executable File
202 lines
6.6 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\Assignmentmatter as Entity;
|
|
use App\Entity\Assignment as Assignment;
|
|
use App\Form\AssignmentmatterType as Form;
|
|
|
|
class AssignmentmatterController extends AbstractController
|
|
{
|
|
private $data = "assignmentmatter";
|
|
private $route = "app_assignmentmatter";
|
|
private $render = "Assignmentmatter/";
|
|
private $entity = "App:Assignmentmatter";
|
|
|
|
public function list($id,Request $request)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$classroom=$em->getRepository("App:Classroom")->find($id);
|
|
if(!$classroom) return $this->redirectToRoute("app_classroom");
|
|
|
|
$datas = $em->getRepository($this->entity)->findBy(["classroom"=>$classroom]);
|
|
|
|
return $this->render($this->render.'list.html.twig',[
|
|
"classroom" => $classroom,
|
|
$this->data."s" => $datas,
|
|
"useheader" => true,
|
|
"usesidebar" => true,
|
|
]);
|
|
}
|
|
|
|
|
|
public function submit($id,Request $request) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$classroom=$em->getRepository("App:Classroom")->find($id);
|
|
if(!$classroom) return $this->redirectToRoute("app_classroom");
|
|
|
|
$datas = $em->getRepository($this->entity)->findBy(["classroom"=>$classroom]);
|
|
|
|
$data = new Entity();
|
|
$data->setClassroom($classroom);
|
|
$data->setCoefficient(1);
|
|
$data->setIsoption(false);
|
|
$data->setIspartial(false);
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class,$data,array("mode"=>"submit","idclassroom"=>$id));
|
|
|
|
// 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();
|
|
|
|
// On associe les teachers
|
|
$this->addTeacher($data);
|
|
|
|
$em->persist($data);
|
|
$em->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute($this->route,["id"=>$id]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
'classroom' => $classroom,
|
|
$this->data => $data,
|
|
'mode' => 'submit',
|
|
'form' => $form->createView()
|
|
]);
|
|
|
|
}
|
|
|
|
public function update($id,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","idclassroom"=>$data->getClassroom()->getId()));
|
|
|
|
// 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();
|
|
|
|
// On associe les teachers
|
|
$this->addTeacher($data);
|
|
|
|
// On enregistre
|
|
$em->persist($data);
|
|
$em->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute($this->route,["id"=>$data->getClassroom()->getId()]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
'classroom' => $data->getClassroom(),
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function delete($id,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($error) {
|
|
return $this->redirectToRoute($this->route."_update",["id"=>$id]);
|
|
}
|
|
else {
|
|
$classroomid=$data->getClassroom()->getId();
|
|
|
|
$em->remove($data);
|
|
$em->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute($this->route,["id"=>$classroomid]);
|
|
}
|
|
}
|
|
|
|
|
|
protected function addTeacher($data) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
// On génère les teachers
|
|
$tbassignments=[];
|
|
foreach($data->getTeachers() as $teacher) {
|
|
$assignmentteacher=$em->getRepository("App:Assignmentteacher")->findOneBy(["teacher"=>$teacher,"classroom"=>$data->getClassroom()]);
|
|
$assignment=$em->getRepository("App:Assignment")->findOneBy(["assignmentteacher"=>$assignmentteacher,"assignmentmatter"=>$data]);
|
|
if(!$assignment) {
|
|
$assignment=new Assignment();
|
|
$assignment->setAssignmentteacher($assignmentteacher);
|
|
$assignment->setAssignmentmatter($data);
|
|
$assignment->setClassroom($data->getClassroom());
|
|
$em->persist($assignment);
|
|
|
|
}
|
|
|
|
array_push($tbassignments,$assignment->getId());
|
|
}
|
|
|
|
// On purge les teachers
|
|
foreach($data->getAssignments() as $assignment) {
|
|
if(!in_array($assignment->getId(),$tbassignments)) {
|
|
$em->remove($assignment);
|
|
$em->flush();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function getErrorForm($id,$form,$request,$data,$mode) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
if ($form->get('submit')->isClicked() && $mode=="submit") {
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
}
|