Пример #1
0
 /**
  * User add badge
  *
  * @Route("/addbadges", name="test_addbadges")
  * @Template()
  */
 public function addbadgesAction()
 {
     $session = $this->getRequest()->getSession();
     $can_edit = ($session->get('admin') or $session->get('moderator'));
     if (!$can_edit) {
         return $this->redirect('/');
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $user_id = $request->request->get('user_id');
         $test_id = $request->request->get('test_id');
         $action = $request->request->get('action');
         $em = $this->getDoctrine()->getEntityManager();
         $user = $em->getRepository('ApplicationUserBundle:User')->find($user_id);
         if (!$user) {
             throw $this->createNotFoundException('Unable to find User entity.');
         }
         if ($action == 1) {
             // añadir test
             $entity = new TestUser();
             $entity->setUserId($user_id);
             $entity->setTestId($test_id);
             $entity->setDate(new \DateTime("now"));
             $em->persist($entity);
             $em->flush();
         } else {
             $query = $em->createQuery("DELETE FROM ApplicationTestBundle:TestUser tu WHERE tu.test_id = :test_id AND tu.user_id = :user_id");
             $query->setParameter('user_id', $user_id);
             $query->setParameter('test_id', $test_id);
             $total = $query->getResult();
         }
     } else {
         $user = false;
     }
     return array('user' => $user, 'action' => $action);
 }
Пример #2
0
 /**
  * finish test
  *
  * @Route("/{id}/result", name="test_result")
  * @Template("ApplicationTestBundle:Test:result.html.twig")
  * @Method("post")
  */
 public function resultAction($id)
 {
     $session = $this->getRequest()->getSession();
     $session_id = $session->get('id');
     if (!$session_id) {
         return $this->redirect($this->generateUrl('user_welcome', array('back' => $_SERVER['REQUEST_URI'])));
     }
     $request = $this->getRequest();
     $em = $this->getDoctrine()->getEntityManager();
     $entity = $em->getRepository('ApplicationTestBundle:Test')->find($id);
     if (!$entity) {
         throw $this->createNotFoundException('Unable to find Test entity.');
     }
     $ok = 0;
     $replies = explode(',', $entity->getReplies());
     $total = count($replies);
     for ($i = 0; $i < $total; $i++) {
         if ($request->get('question-' . ($i + 1)) == $replies[$i]) {
             $ok++;
         }
     }
     $result = $ok == $total;
     if ($result) {
         // contadores
         $query = $em->createQuery("SELECT COUNT(tu) as total FROM ApplicationTestBundle:TestUser tu WHERE tu.test_id = :test_id AND tu.user_id = :user_id");
         $query->setParameter('test_id', $id);
         $query->setParameter('user_id', $session_id);
         $total = current($query->getResult());
         if (!$total['total']) {
             // guardar badge
             $badge = new \Application\TestBundle\Entity\TestUser();
             $badge->setTestId($id);
             $badge->setUserId($session_id);
             $badge->setDate(new \DateTime("now"));
             $em->persist($badge);
             $em->flush();
         }
         // redirigir
         return $this->redirect($this->generateUrl('test_show', array('id' => $id)) . '#win');
     }
     return array('result' => $result, 'total' => $total, 'ok' => $ok, 'entity' => $entity);
 }