Commit ff6c7648 authored by SuliacLavenant's avatar SuliacLavenant Committed by Regis
Browse files

Event Module

parent 613f47bb
This diff is collapsed.
......@@ -40,3 +40,4 @@ security:
- { path: ^/profile, roles: ROLE_USER }
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/planner, roles: ROLE_USER}
- { path: ^/my_event, roles: ROLE_USER}
window.addEventListener("load", function () {
msTimes = document.getElementsByClassName("msTime");
for (let i = 0; i < msTimes.length; i++) {
convertTime(msTimes[i]);
}
});
function convertTime(event) {
if (!event.innerHTML.includes("-")) {
let ms = event.innerHTML;
let sec = ms / 1000;
let min = 0;
let hou = 0;
let str = "";
if (sec >= 3600) {
hou = Math.floor(sec / 3600);
sec = sec % 3600;
}
if (sec >= 60) {
min = Math.floor(sec / 60);
sec = sec % 60;
}
if (hou.toString().length == 1) {
str = "0" + hou.toString() + "h";
} else {
str = hou.toString() + "h";
}
if (min.toString().length == 1) {
str += "0" + min.toString() + "m";
} else {
str += min.toString() + "m";
}
sec = Math.floor(sec);
if (sec.toString().length == 1) {
str += "0" + sec.toString() + "s";
} else {
str += sec.toString() + "s";
}
event.innerHTML = str;
}
}
\ No newline at end of file
......@@ -2,6 +2,12 @@
namespace App\Controller;
use App\Entity\Event;
use App\Entity\EventCourse;
use App\Entity\Participant;
use App\Entity\ParticipantMakeEventCourse;
use App\Entity\Track;
use PHPUnit\Util\Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\HeaderUtils;
......@@ -52,7 +58,7 @@ class DataController extends AbstractController
$response = new Response($fileContent);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$id."."."xml"
$id . "." . "xml"
);
$response->headers->set('Content-Disposition', $disposition);
......@@ -85,7 +91,7 @@ class DataController extends AbstractController
$response = new Response($fileContent);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$id."."."kml"
$id . "." . "kml"
);
$response->headers->set('Content-Disposition', $disposition);
......@@ -104,6 +110,11 @@ class DataController extends AbstractController
$this->getDoctrine()->getRepository(Course::class)
);
if ($isValid) {
try {
$this->checkAndAddToEvent($trackCreator->getTrack());
} catch (Exception $e) {
$error=$e;
}
return new JsonResponse(
[
'status' => 'ok',
......@@ -150,4 +161,25 @@ class DataController extends AbstractController
);
}
}
public function checkAndAddToEvent(Track $track)
{
$events = $this->getDoctrine()->getRepository(Event::class)->findAll();
foreach ($events as $event) {
foreach ($event->getEventCourses() as $eventCourse) {
if ($eventCourse->getCourse() === $track->getCourse()) {
foreach ($event->getParticipants() as $participant) {
if ($track->getName() === $participant->getNickname()) {
$participantMakeEventCourse = $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class)->find(array("eventCourse" => $eventCourse, "participant" => $participant));
$participantMakeEventCourse->setTrack($track);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($participantMakeEventCourse);
$entityManager->flush();
$event->update($entityManager, $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class));
}
}
}
}
}
}
}
<?php
namespace App\Controller;
use App\Entity\EventCourse;
use App\Entity\Event;
use App\Entity\Participant;
use App\Entity\ParticipantMakeEventCourse;
use App\Entity\User;
use App\Form\EventType;
use App\Form\EventCourseType;
use App\Form\ModifyParticipantMakeEventCourseType;
use App\Form\ParticipantType;
use App\Model\ModifyParticipantMakeEventCourse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
class EventController extends AbstractController
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* @Route("/my_event/show_all/{id}", name="my_events", defaults={"id": -1})
* @param int $id
* @return Response
*/
public function show_all(int $id): Response
{
if ($id == -1) {
$user = $this->security->getUser();
} else {
if (in_array('ROLE_ADMIN', $this->security->getUser()->getRoles())) {
$user = $this->getDoctrine()->getRepository(User::class)->find($id);
} else {
throw new AccessDeniedException();
}
}
return $this->render('event/my_events.html.twig',
[
'events' => $user->getEvents(),
]
);
}
/**
* @Route("/my_event/create", name="create_my_event")
* @param Request $request
* @return Response
*/
public function create_event(Request $request): Response
{
$event = new Event();
$event->setCreator($this->security->getUser());
$eventForm = $this->createForm(EventType::class, $event);
$eventForm->handleRequest($request);
if ($eventForm->isSubmitted() && $eventForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($event);
$entityManager->flush();
return $this->redirectToRoute('my_events');
}
return $this->render(
'event/my_event_create.html.twig',
[
'eventForm' => $eventForm->createView(),
]
);
}
/**
* @Route("/my_event/details/{id}", name="my_event_details")
* @param int $id
* @return Response
*/
public function details(int $id, Request $request): Response
{
$user = $this->security->getUser();
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
$formsView = [];
$participants = $event->getParticipants();
$eventCourses = $event->getEventCourses();
$participantMakeEventCourseRepository = $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class);
for ($i = 0; $i < sizeof($participants); $i++) {
$formsView[$i] = array();
for ($j = 0; $j < sizeof($eventCourses); $j++) {
$participantMakeEventCourse = $participantMakeEventCourseRepository->find(array("eventCourse" => $eventCourses->get($j), "participant" => $participants->get($i)));
$modifyParticipantMakeEventCourse = new ModifyParticipantMakeEventCourse();
$modifyParticipantMakeEventCourse->setParticipantMakeEventCourse($participantMakeEventCourse);
$modifyParticipantMakeEventCourseForm = $this->createForm(ModifyParticipantMakeEventCourseType::class, $modifyParticipantMakeEventCourse, ['course' => $eventCourses->get($j)->getCourse(), 'participant' => $participants->get($i)->getId(), 'eventCourse' => $eventCourses->get($j)->getId()]);
$formsView[$i][$j] = $modifyParticipantMakeEventCourseForm->createView();
$modifyParticipantMakeEventCourseForm->handleRequest($request);
if ($modifyParticipantMakeEventCourseForm->isSubmitted() && $participantMakeEventCourse === $participantMakeEventCourseRepository->find(array("eventCourse" => $this->getDoctrine()->getRepository(EventCourse::class)->find($modifyParticipantMakeEventCourse->getEventCourseId()), "participant" => $this->getDoctrine()->getRepository(Participant::class)->find($modifyParticipantMakeEventCourse->getParticipantId())))) {
if (true) {
$modifyParticipantMakeEventCourse->modify($this->getDoctrine()->getManager(), $participantMakeEventCourseRepository);
}
}
}
}
if (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
return $this->render(
'event/my_event_details.html.twig',
[
'event' => $event,
'eventCourses' => $event->getEventCourses(),
'participants' => $event->getParticipants(),
'ranking' => $this->extractRanking($id),
'forms' => $formsView,
]
);
} else {
throw new AccessDeniedException();
}
}
/**
* @Route("/my_event/update/{id}", name="my_event_update")
* @param $id
* @return RedirectResponse
*/
public function updateEvent($id): RedirectResponse
{
$user = $this->security->getUser();
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
if ($event != null && (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
$event->update($this->getDoctrine()->getManager(), $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class));
return $this->redirectToRoute('my_event_details', ['id' => $id]);
} else {
throw new AccessDeniedException();
}
}
/**
* @Route("/my_event/delete/{id}", name="my_event_delete")
* @param $id
* @return RedirectResponse
*/
public function deleteEvent($id): RedirectResponse
{
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
if ($event != null && (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
$event->remove($entityManager, $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class));
return $this->redirectToRoute('my_events');
} else {
throw new AccessDeniedException();
}
}
/**
* @Route("/my_event/{id}/participants", name="my_event_participants")
* @param int $id
* @param Request $request
* @return Response
*/
public function my_event_participants(int $id, Request $request): Response
{
$error = "";
$success = "";
$participant = new Participant();
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
$participantForm = $this->createForm(ParticipantType::class, $participant);
$participantForm->handleRequest($request);
if ($participantForm->isSubmitted() && $participantForm->isValid()) {
$error = $participant->create($event, $this->getDoctrine()->getManager());
if ($error == "") {
$success = "success";
}
}
return $this->render(
'event/my_event_manage_participants.html.twig',
[
'participantForm' => $participantForm->createView(),
'participants' => $this->getDoctrine()->getRepository(Event::class)->find($id)->getParticipants(),
'event' => $this->getDoctrine()->getRepository(Event::class)->find($id),
'error' => $error,
'success' => $success,
]
);
}
/**
* @Route("/my_event/{idEvent}/deleteParticipant/{id}", name="my_event_delete_participant")
* @param int $idEvent
* @param int $id
* @return RedirectResponse
*/
public function deleteParticipant(int $idEvent, int $id): RedirectResponse
{
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$participant = $this->getDoctrine()->getRepository(Participant::class)->find($id);
$event = $this->getDoctrine()->getRepository(Event::class)->find($idEvent);
if ($participant != null && (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
$participant->remove($entityManager, $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class));
return $this->redirectToRoute('my_event_participants', ['id' => $idEvent]);
} else {
throw new AccessDeniedException();
}
}
/**
* @Route("/my_event/{id}/event_courses", name="my_event_courses")
* @param int $id
* @param Request $request
* @return Response
*/
public function my_event_course(int $id, Request $request): Response
{
$eventCourse = new EventCourse();
$eventCourseForm = $this->createForm(EventCourseType::class, $eventCourse, ['user' => $this->security->getUser()]);
$eventCourseForm->handleRequest($request);
if ($eventCourseForm->isSubmitted() && $eventCourseForm->isValid()) {
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
$eventCourse->setEvent($event);
$eventCourse->convertTimeToMaxTime();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($eventCourse);
$participants = $event->getParticipants();
foreach ($participants as $participant) {
$participantMakeEventCourse = new ParticipantMakeEventCourse();
$participantMakeEventCourse->setParticipant($participant);
$participantMakeEventCourse->setEventCourse($eventCourse);
$entityManager->persist($participantMakeEventCourse);
}
$entityManager->flush();
}
return $this->render(
'event/my_event_add_course.html.twig',
[
'eventCourseForm' => $eventCourseForm->createView(),
'eventCourses' => $this->getDoctrine()->getRepository(Event::class)->find($id)->getEventCourses(),
'idEvent' => $id,
]
);
}
/**
* @Route("/my_event/{idEvent}/event_courses/removeCourse/{id}", name="my_event_delete_course")
* @param int $idEvent
* @param int $id
* @return RedirectResponse
*/
public function deleteEventCourse(int $idEvent, int $id): RedirectResponse
{
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$eventCourse = $this->getDoctrine()->getRepository(EventCourse::class)->find($id);
$event = $this->getDoctrine()->getRepository(Event::class)->find($idEvent);
if ($eventCourse != null && (in_array($event, $user->getEvents()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles()))) {
$eventCourse->remove($entityManager, $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class));
return $this->redirectToRoute('my_event_courses', ['id' => $idEvent]);
} else {
throw new AccessDeniedException();
}
}
/**
* @Route("/event/list/", name="events_list")
*/
public function show_list(): Response
{
$allEvents = $this->getDoctrine()->getRepository(Event::class)->findAll();
return $this->render('event/events_list.html.twig',
[
'events' => $allEvents,
]
);
}
/**
* @Route("/event/{id}", name="event_details")
* @param int $id
* @param Request $request
* @return Response
*/
public function event_details(int $id, Request $request): Response
{
$error = "";
$success = "";
$event = $this->getDoctrine()->getRepository(Event::class)->find($id);
$participant = new Participant();
$participantForm = $this->createForm(ParticipantType::class, $participant);
$participantForm->handleRequest($request);
if ($participantForm->isSubmitted() && $participantForm->isValid()) {
$error = $participant->create($event, $this->getDoctrine()->getManager());
if ($error == "") {
$success = "success";
}
}
return $this->render('event/event_info.html.twig',
[
'event' => $event,
'eventCourses' => $event->getEventCourses(),
'participants' => $event->getParticipants(),
'ranking' => $this->extractRanking($id),
'participantForm' => $participantForm->createView(),
'error' => $error,
'success' => $success,
]
);
}
public function extractRanking(int $eventId): array
{
$participants = $this->getDoctrine()->getRepository(Event::class)->find($eventId)->getParticipants();
$eventCourses = $this->getDoctrine()->getRepository(Event::class)->find($eventId)->getEventCourses();
$score = [];
$totalTime = [];
$ranking = [[], [], [], [], [], []];
if ($participants[0] !== null and $eventCourses[0] !== null) {
for ($i = 0; $i < sizeof($participants); $i++) {
$score[$i] = 0;
$totalTime[$i] = 0;
$ranking[2][$i] = 0;
$ranking[3][$i] = array();
$ranking[5][$i] = array();
foreach ($eventCourses as $eventCourse) {
$participantMakeEventCourse = $this->getDoctrine()->getRepository(ParticipantMakeEventCourse::class)->find(array("eventCourse" => $eventCourse, "participant" => $participants->get($i)));
if ($participantMakeEventCourse->getTrack() !== null) {
$eventCourseScore = $participantMakeEventCourse->getScore();
$eventCourseTime = $participantMakeEventCourse->getTrack()->getTotalTime() + ($participantMakeEventCourse->getNbMissingPunchPenalty() * $participantMakeEventCourse->getEventCourse()->getMissingPunchPenalty() * 60000);
if ($participantMakeEventCourse->getEventCourse()->getOverTimePenalty() === 0) {
$eventCourseTime += ($participantMakeEventCourse->getNbOverTimePenalty() * $participantMakeEventCourse->getEventCourse()->getOverTimePenalty() * 60000);
}
$ranking[2][$i] += 1;
$score[$i] += $eventCourseScore;
$totalTime[$i] += $eventCourseTime;
array_push($ranking[3][$i], array($eventCourseScore, $participantMakeEventCourse->getNbMissingPunchPenalty(), $participantMakeEventCourse->getNbOverTimePenalty(), $eventCourseTime));
} else {
array_push($ranking[3][$i], array("-", "-", "-", "-"));
}
array_push($ranking[5][$i], array($participantMakeEventCourse->isModified()));
}
$ranking[1][$i] = $score[$i];
$ranking[4][$i] = $totalTime[$i];
}
if ($eventCourses[0]->getEvent()->getType() === 0 or $eventCourses[0]->getEvent()->getType() === 1) {
while (sizeof($score) !== 0) {
$max = max($score);
array_push($ranking[0], array_search($max, $score));
unset($score[array_search($max, $score)]);
}
} elseif ($eventCourses[0]->getEvent()->getType() === 2) {
$zeros = array();
while (sizeof($totalTime) !== 0) {
$min = min($totalTime);
if ($min === 0) {
array_push($zeros, array_search($min, $totalTime));
} else {
array_push($ranking[0], array_search($min, $totalTime));
}
unset($totalTime[array_search($min, $totalTime)]);
}
foreach ($zeros as $zero) {
array_push($ranking[0], $zero);
}
}
}
return $ranking;
}
}
......@@ -93,6 +93,11 @@ class Course
*/
private ?string $club;
/**
* @ORM\OneToMany(targetEntity="App\Entity\EventCourse", mappedBy="course")
*/
private $eventCourses;
public function __construct()
{
$this->orienteer = new ArrayCollection();
......
<?php
namespace App\Entity;
use App\Repository\EventRepository;
use App\Repository\ParticipantMakeEventCourseRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Persistence\ObjectManager;
/**
* @ORM\Entity(repositoryClass=EventRepository::class)
*/
class Event
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id;
/**
* @ORM\Column(type="string", length=255)
*/
private string $name;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="events")
* @ORM\JoinColumn(nullable=false)
*/
private ?User $creator;
/**
* @ORM\Column(type="integer")
*/
private ?int $type;
/**
* @ORM\OneToMany(targetEntity="App\Entity\EventCourse", mappedBy="event", orphanRemoval=true)
*/
private $eventCourses;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Participant", mappedBy="event", orphanRemoval=true)
*/
private $participant;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCreator(): ?User
{
return $this->creator;
}
public function setCreator(?User $creator): self
{