public function subactionComplete(SubactionProgressEvent $event, $name, EventDispatcher $dispatcher) { $action = $event->getSubaction()->getAction(); $user = $event->getUser(); $userAction = $this->em->getRepository('AcmeEdelaBundle:UserAction')->findOneBy(array('user' => $user, 'action' => $action)); $userTime = $user->getCurrentDateTime(); $dayStart = clone $userTime; $dayFinish = clone $userTime; $dayStart->modify('today midnight'); $dayFinish->modify('tomorrow midnight'); $existingProgress = $this->em->getRepository('AcmeEdelaBundle:UserActionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('userAction', $userAction))->andWhere(Criteria::expr()->gte('createdAt', $dayStart))->andWhere(Criteria::expr()->lte('createdAt', $dayFinish))); $dispatch = null; if (!$existingProgress->count() && $action->getSubactions()->count() == count($this->em->getRepository('AcmeEdelaBundle:Action')->getSubactions($action->getId(), $user, null, true))) { $progress = new UserActionProgress(); $progress->setUserAction($userAction); $progress->setResult(1); $this->em->persist($progress); $dispatch = true; } elseif ($existingProgress->count()) { $this->em->remove($existingProgress->first()); $dispatch = false; } $this->em->flush(); if ($dispatch !== null) { $event = new ActionEvent($action, $user, $dispatch); $dispatcher->dispatch($this->completeEvent, $event); } }
public function executesubAction2($id) { /** @var Request $request */ $request = $this->container->get('request_stack')->getCurrentRequest(); $em = $this->getDoctrine()->getManager(); $user = $this->container->get('security.context')->getToken()->getUser(); $subaction = $em->find('AcmeEdelaBundle:Subaction', $id); $action = $subaction->getAction(); $userAction = $em->getRepository('AcmeEdelaBundle:UserAction')->findOneBy(array('user' => $user, 'action' => $action)); $success = false; try { if (!$userAction || !$userAction->getIsDayIncluded(new \DateTime())) { throw new \Exception(); } $existingSubprogress = $em->getRepository('AcmeEdelaBundle:UserSubactionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('user', $user))->andWhere(Criteria::expr()->eq('subaction', $subaction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingSubprogress) { throw new \Exception(); } $existingProgress = $em->getRepository('AcmeEdelaBundle:UserActionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('userAction', $userAction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingProgress > 0) { throw new \Exception(); } $subprogress = new UserSubactionProgress(); $subprogress->setUser($user)->setSubaction($subaction); $em->persist($subprogress); $em->flush(); if ($action->getSubactions()->count() == count($em->getRepository('AcmeEdelaBundle:Action')->getSubactions($action, $user, null, true))) { $progress = new UserActionProgress(); $progress->setUserAction($userAction); $progress->setResult($request->get('result', 1)); $em->persist($progress); } $em->flush(); return ['success' => true]; } catch (\Exception $e) { return ['success' => false]; } }
public function executesubAction($subaction_id) { /** @var Request $request */ $request = $this->container->get('request_stack')->getCurrentRequest(); $em = $this->getDoctrine()->getManager(); $user = $this->container->get('security.context')->getToken()->getUser(); $subaction = $em->find('AcmeEdelaBundle:Subaction', $subaction_id); $action = $subaction->getAction(); $userAction = $em->getRepository('AcmeEdelaBundle:UserAction')->findOneBy(array('user' => $user, 'action' => $action)); $success = false; try { if (!$userAction || !$userAction->getIsDayIncluded(new \DateTime())) { throw new \Exception(); } $existingSubprogress = $em->getRepository('AcmeEdelaBundle:UserSubactionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('user', $user))->andWhere(Criteria::expr()->eq('subaction', $subaction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingSubprogress) { throw new \Exception(); } $existingProgress = $em->getRepository('AcmeEdelaBundle:UserActionProgress')->matching(Criteria::create()->where(Criteria::expr()->eq('userAction', $userAction))->andWhere(Criteria::expr()->gte('createdAt', new \DateTime('today midnight')))->andWhere(Criteria::expr()->lte('createdAt', new \DateTime('tomorrow midnight'))))->count(); if ($existingProgress > 0) { throw new \Exception(); } $subprogress = new UserSubactionProgress(); $subprogress->setUser($user)->setSubaction($subaction); $em->persist($subprogress); $em->flush(); if ($action->getSubactions()->count() == count($em->getRepository('AcmeEdelaBundle:Action')->getSubactions($action, $user, null, true))) { $progress = new UserActionProgress(); $progress->setUserAction($userAction); $progress->setResult($request->get('result', 1)); $em->persist($progress); } $em->flush(); $success = true; } catch (\Exception $e) { $success = false; } if ($success) { if ($request->isXmlHttpRequest()) { return new JsonResponse(['success' => true, 'text' => $this->renderView('AcmeEdelaBundle:Actions:_one_block.html.twig', array('action' => $action, 'progress' => $progress))]); } else { return new RedirectResponse($this->container->get('router')->generate('tasks_edit', array('task_id' => $action->getId()))); } } else { return new JsonResponse(['success' => false]); } }