コード例 #1
0
 /**
  * @Route("/question/random", name="skyeng_tt_question_random", requirements={ "_method" : "GET" })
  * @Template()
  */
 public function randomAction()
 {
     $request = $this->container->get('request_stack')->getCurrentRequest();
     $userId = $request->getSession()->get(Tool::ANONIMOUS_USER_ID);
     if ($userId) {
         $doctrine = $this->getDoctrine();
         $usersRepository = $doctrine->getRepository('SkyengTTSkyengTTBundle:AnonymousUsers');
         $user = $usersRepository->find($userId);
         if ($user) {
             return Tool::json(array('success' => true, 'score' => $user->getScore(), 'next' => Tool::getNextQuestion($request, $doctrine)));
         }
     }
     return Tool::json404(array('info' => 'Empty username'));
 }
コード例 #2
0
 /**
  * @Route("/appuser/create", name="skyeng_tt_appuser_create", requirements={ "_method" : "POST" })
  * @Template()
  */
 public function createAction()
 {
     $request = $this->container->get('request_stack')->getCurrentRequest();
     $username = $request->get('username');
     if ($username) {
         $doctrine = $this->getDoctrine();
         $em = $doctrine->getEntityManager();
         $user = new AnonymousUsers();
         $user->setName($username);
         $user->setTimestamp(time());
         $user->setScore(0);
         $em->persist($user);
         $em->flush();
         $request->getSession()->set(Tool::ANONIMOUS_USER_ID, $user->getId());
         $request->getSession()->set(Tool::CURRENT_QUESTION, 0);
         $request->getSession()->set(Tool::WRONG_ANSWER_COUNT, 0);
         $request->getSession()->set(Tool::LAST_QUESTIONS, array());
         return Tool::json(array('id' => $user->getId()));
     }
     return Tool::json404(array('info' => 'empty username'));
 }
コード例 #3
0
 /**
  * Добавление ответа на вопрос
  * @Route("/answer/add/{questionId}", name="skyeng_tt_answer_add", requirements={ "_method" : "POST" })
  * @Template()
  * @param int $question_id идентификатор вопроса, на который отправлен ответ
  */
 public function addAction($questionId)
 {
     $questionId = intval($questionId);
     if ($questionId) {
         $request = $this->container->get('request_stack')->getCurrentRequest();
         $anonymousAppUserId = $request->getSession()->get(Tool::ANONIMOUS_USER_ID);
         if (!$anonymousAppUserId) {
             return Tool::json404(array('info' => 'Lost session'));
         }
         $answerId = $request->get('answer_id');
         if (!$answerId) {
             return Tool::json404(array('info' => 'Empty answer!'));
         }
         $doctrine = $this->getDoctrine();
         $em = $doctrine->getEntityManager();
         $repository = $doctrine->getRepository('SkyengTTSkyengTTBundle:Vocabulary');
         $question = $repository->find($questionId);
         if (!$question) {
             return Tool::json404(array('info' => 'Question #' . $questionId . ' not found!'));
         }
         $answer = null;
         if ($question->getAnswerId() == $answerId) {
             $answer = $question;
         } else {
             $collection = $repository->findBy(array('answer_id' => $answerId));
             if ($collection) {
                 $answer = current($collection);
             }
         }
         if (!$answer) {
             return Tool::json404(array('info' => 'Answer #' . $answerId . ' not found!'));
         }
         //В базе есть вопрос и ответ с такими номерами, значит можно записать результат
         if ($question->getAnswerId() == $answerId) {
             $usersRepository = $doctrine->getRepository('SkyengTTSkyengTTBundle:AnonymousUsers');
             $user = $usersRepository->find($anonymousAppUserId);
             $user->setScore($user->getScore() + 1);
             //TODO получать из конфига
             $em->persist($user);
             $em->flush();
             return Tool::json(array('success' => true, 'score' => $user->getScore(), 'next' => Tool::getNextQuestion($request, $doctrine)));
         } else {
             $statisticRepository = $doctrine->getRepository('SkyengTTSkyengTTBundle:ErrorStat');
             $collection = $statisticRepository->findBy(array('quest_id' => $questionId, 'answer_id' => $answerId));
             $statModel = null;
             if (!count($collection)) {
                 $statModel = new ErrorStat();
                 $statModel->setQuestId($questionId);
                 $statModel->setAnswerId($answerId);
                 $statModel->setQuantity(0);
             } else {
                 $statModel = current($collection);
             }
             $statModel->setQuantity($statModel->getQuantity() + 1);
             $em->persist($statModel);
             $em->flush();
             $data = array('success' => false);
             $session = $request->getSession();
             $wrong = $session->get(Tool::WRONG_ANSWER_COUNT, 0);
             $wrong++;
             if ($wrong >= Tool::WRONG_ANSWER_LIMIT) {
                 $data['gameover'] = true;
                 $wrong = 0;
             }
             $session->set(Tool::WRONG_ANSWER_COUNT, $wrong);
             return Tool::json($data);
         }
     }
     return Tool::json404(array('info' => 'Question #' . $questionId . ' not found!'));
 }