Ejemplo n.º 1
0
 /**
  * Check-in a team for this specific race.
  *
  * @Route("/team/{registration}/checkIn", name="race_start_checkin")
  * @Method({"GET", "POST"})
  * @Security("has_role('ROLE_REGISTRATION')")
  */
 public function checkInAction(Request $request, Registration $registration)
 {
     if ($registration->isCheckedIn()) {
         $this->addFlash('error', 'Starter ist bereits eingecheckt!');
         if (!is_null($request->headers->get('referer'))) {
             return $this->redirect($request->headers->get('referer'));
         } else {
             return $this->redirectToRoute('homepage');
         }
     }
     $data = array();
     $form = $this->createFormBuilder($data)->add('token', TextType::class, array('label' => 'Token'))->add('ref', HiddenType::class, array('data' => $request->headers->get('referer')))->getForm();
     if ($request->isMethod('POST')) {
         $form->handleRequest($request);
         // $data is a simply array with your form fields
         // like "query" and "category" as defined above.
         $data = $form->getData();
         // check if the token exists for another competitor
         $em = $this->getDoctrine()->getManager();
         /** @var RegistrationRepository $repo */
         $repo = $em->getRepository('AppBundle:Registration');
         if ($repo->isTokenExistent($data["token"])) {
             $this->addFlash('error', 'Token ist bereits eingecheckt!');
         } else {
             // if unique, then save
             $registration->setCheckedIn($data["token"]);
             $em = $this->getDoctrine()->getManager();
             $em->persist($registration);
             $em->flush();
         }
         return $this->redirect($data['ref']);
     }
     return $this->render('race/checkin.html.twig', array('group' => $registration, 'form' => $form->createView()));
 }
 public function setTime(Registration $registration, $timestamp, $checkpoint, LoggerInterface $logger = null)
 {
     if (is_null($checkpoint) || false == trim($checkpoint)) {
         // PHP evaluates an empty string to false
         throw new \InvalidArgumentException('Checkpoint must not be empty!');
     } elseif (is_null($timestamp) || false == trim($timestamp)) {
         // PHP evaluates an empty string to false
         throw new \InvalidArgumentException('Time must not be empty!');
     }
     /** @var EntityManager $em */
     $em = $this->getEntityManager();
     $checkpoint = trim($checkpoint);
     // expected format of the timestamp is unix timestamp plus microseconds
     $dtime = \DateTime::createFromFormat("U.u", $timestamp);
     // check validity of the given timestamp
     // if parsing did not work, the method returns false (else object)
     if ($dtime === false || false === $dtime->getTimestamp()) {
         throw new \InvalidArgumentException('Time parameter is not a valid timestamp!');
     }
     // check if the checkpoint does not exist for this registration
     /** @var Timing $t */
     foreach ($registration->getTimings() as $t) {
         if ($t->getCheckpoint() == $checkpoint) {
             throw new \InvalidArgumentException('There is already a timing for this checkpoint!');
         }
     }
     // validate if setting a checkpoint makes sense
     if ($checkpoint == Registration::CHECKPOINT_START) {
         if (!$registration->isCheckedIn()) {
             throw new \InvalidArgumentException($registration->getId() . ': ' . "Competitors on lane {$registration->getLane()} are not checked in for starting!");
         }
     } else {
         // some other checkpoint after the starting line
         if (!$registration->isStarted()) {
             throw new \InvalidArgumentException('Competitors are not on track!');
         }
     }
     // store time and checkpoint in database
     $timing = new Timing();
     $timing->setRegistration($registration)->setTime($dtime)->setCheckpoint($checkpoint);
     $em->persist($timing);
     if (!is_null($logger)) {
         $logger->debug("stored {$registration->getId()} with timestamp {$timestamp} for checkpoint {$checkpoint}");
     }
     if ($checkpoint == Registration::CHECKPOINT_START) {
         $registration->setStarted();
         $em->persist($registration);
     } elseif ($checkpoint == Registration::CHECKPOINT_FINISH) {
         $registration->setFinished();
         $em->persist($registration);
     }
     $em->flush();
 }