Commit fe975ae9 authored by Clouard Regis's avatar Clouard Regis
Browse files

Revised missing control point.

parent 865f9910
......@@ -53,10 +53,10 @@ function renderTimeSheet(distances) {
name.innerHTML = orienteer[0];
row.appendChild(name);
orienteer[1].forEach(function (punchedPoint, index) {
let elCheckpoint = document.createElement("th");
elCheckpoint.setAttribute("scope", "col");
let elControlPoint = document.createElement("th");
elControlPoint.setAttribute("scope", "col");
if (punchedPoint.punchTime === 0) {
elCheckpoint.innerHTML = "---";
elControlPoint.innerHTML = "---";
} else {
let lastTime = 0;
let lastIndex = index;
......@@ -69,9 +69,9 @@ function renderTimeSheet(distances) {
let sec = Math.floor((time / 1000) % 60);
sec = sec.toString();
sec = (sec.length === 1 ? "0" + sec : sec);
elCheckpoint.innerHTML = min + ":" + sec;
elControlPoint.innerHTML = min + ":" + sec;
}
rows[index].appendChild(elCheckpoint);
rows[index].appendChild(elControlPoint);
});
});
elTable.appendChild(head);
......
......@@ -33,7 +33,6 @@ class CourseController extends AbstractController
public function track($id)
{
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
// TODO
$xml = $course->getXml();
$sxml = \simplexml_load_string($xml);
$controls = $sxml->RaceCourseData->Control;
......@@ -71,9 +70,9 @@ class CourseController extends AbstractController
}
/**
* @Route("/courses/checkpoint/{id}", name="checkpoint")
* @Route("/courses/control_point/{id}", name="control_point")
*/
function checkpoint($id)
function controlPoints($id)
{
$orienteer = $this->getDoctrine()->getRepository(Track::class)->find($id);
......
......@@ -11,9 +11,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Course;
use App\Model\CreateTrack;
use App\Entity\Track;
use App\Model\CreateMissingCheckpoint;
use App\Entity\MissingCheckpoint;
use App\Model\CreateMissingControlPoint;
class DataController extends AbstractController
{
......@@ -23,9 +21,7 @@ class DataController extends AbstractController
public function get_courses()
{
$courses = $this->getDoctrine()->getRepository(Course::class)->findAll();
$dataCourses = [];
foreach ($courses as $course) {
$info = array(
"id" => $course->getId(),
......@@ -46,23 +42,17 @@ class DataController extends AbstractController
public function get_xml($id)
{
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (!$course) {
return $this->json(["error" => "invalid id"]);
}
$fileContent = $course->getXml();
$response = new Response($fileContent);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$id."."."xml"
);
$response->headers->set('Content-Disposition', $disposition);
//$response->send();
return $response;
}
......@@ -72,7 +62,6 @@ class DataController extends AbstractController
public function get_image($id)
{
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (!$course) {
return $this->json(["error" => "invalid id"]);
}
......@@ -87,58 +76,31 @@ class DataController extends AbstractController
public function get_kml($id)
{
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (!$course) {
return $this->json(["error" => "invalid id"]);
}
$fileContent = $course->getKml();
$response = new Response($fileContent);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
$id."."."kml"
);
$response->headers->set('Content-Disposition', $disposition);
//$response->send();
return $response;
}
/**
* @Route("/data/send", name="send_race")
*/
public function to_delete()
{
$request = Request::createFromGlobals();
$trackCreator = new CreateTrack($request->getContent());
return new JsonResponse(
[
"reponse" =>
$trackCreator->addTrack(
$this->getDoctrine()->getManager(),
$this->getDoctrine()->getRepository(Course::class)
),
]
);
}
/**
* @Route("/data/race")
*/
public function post_race()
{
$request = Request::createFromGlobals();
$trackCreator = new CreateTrack($request->getContent());
return new JsonResponse(
[
"reponse" =>
"response" =>
$trackCreator->addTrack(
$this->getDoctrine()->getManager(),
$this->getDoctrine()->getRepository(Course::class)
......@@ -149,21 +111,23 @@ class DataController extends AbstractController
/**
* @Route("/data/missing-control-point")
*
* To simulate post request:
* curl -X POST -d \@toto.json https://127.0.0.1:8000/index.php/data/missing-control-point
*/
public function post_missing_controlpoint()
public function post_missing_control_point()
{
$request = Request::createFromGlobals();
$creator = new CreateMissingCheckpoint($request->getContent());
$creator = new CreateMissingControlPoint($request->getContent());
return new JsonResponse(
[
"reponse" =>
$creator->addCheckpoint(
$this->getDoctrine()->getManager(),
$this->getDoctrine()->getRepository(Course::class)
),
]
);
[
"response" =>
$creator->addControlPoint(
$this->getDoctrine()->getManager(),
$this->getDoctrine()->getRepository(Course::class)
),
]
);
}
}
......@@ -2,7 +2,7 @@
namespace App\Controller;
use App\Entity\MissingCheckpoint;
use App\Entity\MissingControlPoint;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
......@@ -16,6 +16,7 @@ use App\Entity\Course;
use App\Entity\User;
use App\Model\CreateCourse;
use App\Model\FileUploader;
use function in_array;
class PlannerController extends AbstractController
{
......@@ -34,7 +35,7 @@ class PlannerController extends AbstractController
if ($id == -1) {
$user = $this->security->getUser();
} else {
if (\in_array('ROLE_ADMIN', $this->security->getUser()->getRoles())) {
if (in_array('ROLE_ADMIN', $this->security->getUser()->getRoles())) {
$user = $this->getDoctrine()->getRepository(User::class)->find($id);
} else {
throw new AccessDeniedException();
......@@ -76,7 +77,7 @@ class PlannerController extends AbstractController
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (\in_array($course, $user->getCourses()->toArray()) || \in_array('ROLE_ADMIN', $user->getRoles())) {
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
$tracks = $course->getOrienteer()->toArray();
foreach ($tracks as $track) {
$entityManager->remove($track);
......@@ -90,22 +91,22 @@ class PlannerController extends AbstractController
}
/**
* @Route("/planner/missing_checkpoint/{id}", name="missing_checkpoint")
* @Route("/planner/missing_control_point/{id}", name="missing_control_point")
*/
public function missing_checkpoint($id)
public function missing_control_point($id)
{
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
/* $entityManager = $this->getDoctrine()->getManager();*/
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (\in_array($course, $user->getCourses()->toArray()) || \in_array('ROLE_ADMIN', $user->getRoles())) {
$checkpoints = $course->getMissingCheckpoints();
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
$controlPoints = $course->getMissingControlPoints();
return $this->render(
'planner/missing_checkpoint.html.twig',
'planner/missing_control_point.html.twig',
[
'missingCheckpoints' => $checkpoints,
'missingControlPoints' => $controlPoints,
]
);
} else {
......@@ -121,7 +122,7 @@ class PlannerController extends AbstractController
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (\in_array($course, $user->getCourses()->toArray()) || \in_array('ROLE_ADMIN', $user->getRoles())) {
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
$course->remove($entityManager);
return $this->redirectToRoute('your_courses');
......@@ -131,17 +132,16 @@ class PlannerController extends AbstractController
}
/**
* @Route("/planner/delete_checkpoint/{id}", name="delete_checkpoint")
* @Route("/planner/delete_missing_control_point/{id}", name="delete_missing_control_point")
*/
public function delete_checkpoint($id)
public function delete_missing_control_point($id)
{
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$checkpoint = $this->getDoctrine()->getRepository(MissingCheckpoint::class)->find($id);
$course = $checkpoint->getCourse();
if (\in_array($course, $user->getCourses()->toArray()) || \in_array('ROLE_ADMIN', $user->getRoles())) {
$course->remove($entityManager);
$entityManager->remove($checkpoint);
$controlPoint = $this->getDoctrine()->getRepository(MissingControlPoint::class)->find($id);
$course = $controlPoint->getCourse();
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
$entityManager->remove($controlPoint);
$entityManager->flush();
return $this->redirectToRoute('your_courses');
......@@ -158,7 +158,7 @@ class PlannerController extends AbstractController
$user = $this->security->getUser();
$entityManager = $this->getDoctrine()->getManager();
$course = $this->getDoctrine()->getRepository(Course::class)->find($id);
if (\in_array($course, $user->getCourses()->toArray()) || \in_array('ROLE_ADMIN', $user->getRoles())) {
if (in_array($course, $user->getCourses()->toArray()) || in_array('ROLE_ADMIN', $user->getRoles())) {
return $this->render(
'planner/preview_course.html.twig',
[
......
......@@ -10,7 +10,6 @@ use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Form\PlannerType;
use App\Form\PlannerPasswordType;
use App\Entity\User;
use App\Model\CreateUser;
use function in_array;
......
......@@ -46,12 +46,8 @@ class SecurityController extends AbstractController
/**
* @Route("/password-forgot", name="password_forgot")
*/
public function password_forgot(
Request $request,
TranslatorInterface $translator,
UserPasswordEncoderInterface $encoder,
\Swift_Mailer $mailer
) {
public function password_forgot(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $encoder, \Swift_Mailer $mailer)
{
$form = $this->createFormBuilder()
->add('username', TextType::class, ['label' => 'your.username'])
->add('save', SubmitType::class, ['label' => 'submit'])
......@@ -74,7 +70,7 @@ class SecurityController extends AbstractController
'security/password_forgot.html.twig',
[
'form' => $form->createView(),
'error' => $valid,
'error' => $valid, // TODO
]
);
}
......
......@@ -63,9 +63,9 @@ class Course
private $orienteer;
/**
* @ORM\OneToMany(targetEntity="App\Entity\MissingCheckpoint", mappedBy="course", orphanRemoval=true)
* @ORM\OneToMany(targetEntity="App\Entity\MissingControlPoint", mappedBy="course", orphanRemoval=true)
*/
private $missingCheckpoints;
private $missingControlPoints;
/**
* @ORM\Column(type="string", length=255)
......@@ -85,7 +85,7 @@ class Course
public function __construct()
{
$this->orienteer = new ArrayCollection();
$this->missingCheckpoints = new ArrayCollection();
$this->missingControlPoints = new ArrayCollection();
}
public function isValid(): bool
......@@ -225,30 +225,30 @@ class Course
}
/**
* @return Collection|MissingCheckpoint[]
* @return Collection|MissingControlPoint[]
*/
public function getMissingCheckpoints(): Collection
public function getMissingControlPoints(): Collection
{
return $this->missingCheckpoints;
return $this->missingControlPoints;
}
public function addMissingCheckpoint(MissingCheckpoint $missingCheckpoint): self
public function addMissingControlPoint(MissingControlPoint $missingControlPoint): self
{
if (!$this->missingCheckpoints->contains($missingCheckpoint)) {
$this->missingCheckpoints[] = $missingCheckpoint;
$missingCheckpoint->setCourse($this);
if (!$this->missingControlPoints->contains($missingControlPoint)) {
$this->missingControlPoints[] = $missingControlPoint;
$missingControlPoint->setCourse($this);
}
return $this;
}
public function removeMissingCheckpoint(MissingCheckpoint $missingCheckpoint): self
public function removeMissingControlPoint(MissingControlPoint $missingControlPoint): self
{
if ($this->missingCheckpoints->contains($missingCheckpoint)) {
$this->missingCheckpoints->removeElement($missingCheckpoint);
if ($this->missingControlPoints->contains($missingControlPoint)) {
$this->missingControlPoints->removeElement($missingControlPoint);
// set the owning side to null (unless already changed)
if ($missingCheckpoint->getCourse() === $this) {
$missingCheckpoint->setCourse(null);
if ($missingControlPoint->getCourse() === $this) {
$missingControlPoint->setCourse(null);
}
}
......
......@@ -5,27 +5,27 @@ namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\MissingCheckpointRepository")
* @ORM\Entity(repositoryClass="App\Repository\MissingControlPointRepository")
*/
class MissingCheckpoint
class MissingControlPoint
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
private ?int $id;
/**
* @ORM\Column(type="integer")
*/
private $controlPointId;
private ?int $controlPointId;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Course", inversedBy="missingCheckpoints")
* @ORM\ManyToOne(targetEntity="App\Entity\Course", inversedBy="missingControlPoints")
* @ORM\JoinColumn(nullable=false)
*/
private $course;
private ?Course $course;
public function getId(): ?int
{
......
......@@ -5,8 +5,6 @@ namespace App\Form;
use App\Entity\User;
use phpDocumentor\Reflection\Types\Boolean;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
......
......@@ -23,11 +23,11 @@ final class Version20200522171530 extends AbstractMigration
$this->addSql('CREATE TABLE track (id INT AUTO_INCREMENT NOT NULL, course_id INT NOT NULL, name VARCHAR(255) NOT NULL, total_time INT NOT NULL, format INT DEFAULT 0, control_points LONGTEXT NOT NULL COMMENT \'(DC2Type:array)\', trace LONGTEXT NOT NULL, INDEX IDX_D6E3F8A6591CC992 (course_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE course (id INT AUTO_INCREMENT NOT NULL, creator_id INT NOT NULL, xml TEXT NOT NULL, image VARCHAR(255) NOT NULL, kml VARCHAR(2000) NOT NULL, update_at DATETIME NOT NULL, name VARCHAR(255) NOT NULL, latitude VARCHAR(255) NOT NULL, longitude VARCHAR(255) NOT NULL, length INT NOT NULL, INDEX IDX_169E6FB961220EA6 (creator_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE missing_checkpoint (id INT AUTO_INCREMENT NOT NULL, course_id INT NOT NULL, control_point_id INT NOT NULL, INDEX IDX_D3F3931591CC992 (course_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE missing_control_point (id INT AUTO_INCREMENT NOT NULL, course_id INT NOT NULL, control_point_id INT NOT NULL, INDEX IDX_D3F3931591CC992 (course_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, email TEXT NOT NULL, phone TEXT DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D649F85E0677 (username), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE track ADD CONSTRAINT FK_D6E3F8A6591CC992 FOREIGN KEY (course_id) REFERENCES course (id)');
$this->addSql('ALTER TABLE course ADD CONSTRAINT FK_169E6FB961220EA6 FOREIGN KEY (creator_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE missing_checkpoint ADD CONSTRAINT FK_D3F3931591CC992 FOREIGN KEY (course_id) REFERENCES course (id)');
$this->addSql('ALTER TABLE missing_control_point ADD CONSTRAINT FK_D3F3931591CC992 FOREIGN KEY (course_id) REFERENCES course (id)');
}
public function down(Schema $schema) : void
......@@ -35,11 +35,11 @@ final class Version20200522171530 extends AbstractMigration
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE track DROP FOREIGN KEY FK_D6E3F8A6591CC992');
$this->addSql('ALTER TABLE missing_checkpoint DROP FOREIGN KEY FK_D3F3931591CC992');
$this->addSql('ALTER TABLE missing_control_point DROP FOREIGN KEY FK_D3F3931591CC992');
$this->addSql('ALTER TABLE course DROP FOREIGN KEY FK_169E6FB961220EA6');
$this->addSql('DROP TABLE track');
$this->addSql('DROP TABLE course');
$this->addSql('DROP TABLE missing_checkpoint');
$this->addSql('DROP TABLE missing_control_point');
$this->addSql('DROP TABLE user');
}
}
......@@ -271,7 +271,7 @@ class CreateCourse
case CourseValidator::$ERROR_XML_NO_START_END:
return $this->translator->trans("error.xml.no.start.end");
case CourseValidator::$ERROR_XML_INVALID_CONTROL_POINT:
return $this->translator->trans("error.xml.invalid.checkpoint");
return $this->translator->trans("error.xml.invalid.control_point");
case CourseValidator::$ERROR_KML_EMPTY:
return $this->translator->trans("error.kml.empty");
case CourseValidator::$ERROR_KML_INVALID:
......
......@@ -2,52 +2,47 @@
namespace App\Model;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\ORMException;
use PDOException;
use Doctrine\Persistence\ObjectManager;
use Exception;
use App\Entity\MissingCheckpoint;
use App\Entity\MissingControlPoint;
use function json_decode;
class CreateMissingCheckpoint
class CreateMissingControlPoint
{
private $checkpoint;
private MissingControlPoint $controlPoint;
private $data;
public function __construct(string $data)
{
$this->checkpoint = new MissingCheckpoint();
$this->data = \json_decode($data);
$this->controlPoint = new MissingControlPoint();
$this->data = json_decode($data);
}
public function addCheckpoint(EntityManagerInterface $em, $repositery)
public function addControlPoint(ObjectManager $entityManager, $repository)
{
$this->checkpoint->setControlPointId($this->data->controlPointId);
$this->controlPoint->setControlPointId($this->data->controlPointId);
if (filter_var($this->data->courseId, FILTER_VALIDATE_INT)) {
$course = $repositery->find(intval($this->data->courseId));
$course = $repository->find(intval($this->data->courseId));
if ($course == null) {
return "wrong id";
return "Wrong id";
}
} else {
return "wrong id";
return "Wrong id";
}
$this->controlPoint->setCourse($course);
try {
$em->persist($this->track);
$em->flush();
} catch (DBALException $e) {
$entityManager->persist($this->controlPoint);
$entityManager->flush();
} catch (Exception $e) {
if (strpos($e->getMessage(), "Integrity constraint violation")) {
return "Data does not fit expected value";
} else {
return $e->getMessage();
}
} catch (PDOException $e) {
return $e->getMessage();
} catch (ORMException $e) {
return $e->getMessage();
}
return true;
}
}
\ No newline at end of file
}
......@@ -2,30 +2,28 @@
namespace App\Model;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\ORMException;
use PDOException;
use Doctrine\Persistence\ObjectManager;
use Exception;
use App\Entity\Track;
use function json_decode;
class CreateTrack
{
private $track;
private Track $track;
private $data;
public function __construct(string $data)
{
$this->track = new Track();
$this->data = \json_decode($data);
$this->data = json_decode($data);
}
public function addTrack(EntityManagerInterface $em, $repositery)
public function addTrack(ObjectManager $entityManager, $repositery)
{
$this->track->setName($this->data->orienteer);
$this->track->setTotalTime($this->data->totalTime);
$this->track->setFormat($this->data->format);
if (filter_var($this->data->courseId, FILTER_VALIDATE_INT)) {
$course = $repositery->find(intval($this->data->courseId));
if ($course == null) {
......@@ -39,18 +37,14 @@ class CreateTrack
$this->track->setTrace($this->data->trace);
try {
$em->persist($this->track);
$em->flush();
} catch (DBALException $e) {
$entityManager->persist($this->track);
$entityManager->flush();
} catch (Exception $e) {
if (strpos($e->getMessage(), "Integrity constraint violation")) {
return "Data does not fit expected value";
} else {
return $e->getMessage();
}
} catch (PDOException $e) {
return $e->getMessage();
} catch (ORMException $e) {
return $e->getMessage();