/** * Обрабатывает форму опроса * * @Route("/submit-survey/{surveyId}", name = "submit_survey", requirements = {"surveyId" = "\d+"}) * @Secure(roles = "IS_AUTHENTICATED_REMEMBERED") */ public function submitSurveyAction($surveyId) { $em = $this->getDoctrine()->getManager(); $completed = $em->getRepository('EvrikaMainBundle:Poll')->didUserAnswer($this->getUser(), $surveyId); if (!$completed) { $answersArray = $this->getRequest()->request->get('survey'); $survey = $em->find('EvrikaMainBundle:Poll', $surveyId); $questions = $survey->getQuestions(); $gotAllAnswers = true; foreach ($questions as $question) { $answer = new PollAnswer(); if (isset($answersArray[$question->getId()])) { foreach ($question->getChoices() as $choice) { if ($answersArray[$question->getId()] == $choice->getId()) { if ($choice->isCustomTextAllowed()) { if (!empty($answersArray['custom_text_' . $choice->getId()])) { $answer->setCustomText($answersArray['custom_text_' . $choice->getId()]); } else { $gotAllAnswers = false; } } $answer->setChoice($choice); break; } } } else { $gotAllAnswers = false; } if (!$gotAllAnswers) { return $this->render('EvrikaMainBundle:User:survey.html.twig', array('survey' => $survey, 'answersArray' => $answersArray, 'error' => 'Пожалуйста, ответьте на все вопросы')); } $answer->setUser($this->getUser()); $answer->setPoll($survey); $answer->setQuestion($question); $em->persist($answer); } $em->flush(); } return $this->redirect($this->generateUrl('survey', array('surveyId' => $surveyId))); }
/** @Route("/opros/voda", name="opros_borjomi") */ public function oprosBorjomiAction(Request $request) { $pollId = 26; $checkName = 'poll_voted_'; $em = $this->getDoctrine()->getManager(); $poll = $em->getRepository('EvrikaMainBundle:Poll')->findOneById($pollId); if (!$poll) { throw $this->createNotFoundException(); } if (!$request) { $request = $this->getRequest(); } $cookies = $request->cookies; $session = $request->getSession(); $checkName = $checkName . $pollId; $alreadyVoted = $cookies->has($checkName) || $session->has($checkName); if ($user = $this->getUser()) { $alreadyVoted = $em->getRepository('EvrikaMainBundle:PollAnswer')->checkVote($pollId, $user->getId()); } $params = array('poll' => $poll, 'hasVoted' => $alreadyVoted, 'request' => $request, 'title' => 'Лечебные минеральные воды'); if (!$alreadyVoted) { $pollFormBorjomi = new PollFormBorjomi(); $form = $this->createFormBuilder($pollFormBorjomi)->add('age', 'text', array('label' => 'Ваш возраст'))->add('gender', 'choice', array('label' => 'Ваш пол', 'empty_value' => 'выберите', 'choices' => PollFormBorjomi::getGenderOptions()))->add('job', 'choice', array('label' => 'Место работы', 'empty_value' => 'выберите', 'choices' => PollFormBorjomi::getJobOptions()))->add('specialty', null, array('label' => 'Специальность', 'empty_value' => 'выберите'))->add('specializations', null, array('label' => 'Специализации', 'required' => false))->getForm(); } if (!$alreadyVoted && $request->isMethod('POST')) { $form->bind($request); $params['form'] = $form->createView(); if (!$form->isValid()) { return $this->render('EvrikaMainBundle:Info:opros_borjomi.html.twig', $params); } $em->persist($pollFormBorjomi); $questions = $request->request->get('questions'); $questionsMultiple = $request->request->get('questions_multiple'); $asks = $request->request->get('asks'); $questionIds = array(); $askIds = array(); # добавляем голоса варианту и самому вопросу if (!empty($questions)) { foreach ($questions as $questionId => $choiceId) { $question = $em->getRepository('EvrikaMainBundle:PollQuestion')->findOneById($questionId); $choice = $em->getRepository('EvrikaMainBundle:PollChoice')->findOneById($choiceId); $question->addVote(); $choice->addVote(); $questionIds[] = $questionId; if ($user) { $pollAnswer = new PollAnswer(); $pollAnswer->setChoice($choice); $pollAnswer->setQuestion($question); $pollAnswer->setPoll($poll); $pollAnswer->setUser($user); $em->persist($pollAnswer); } } } # добавляем голоса множественным вариантам и самому вопросу if (!empty($questionsMultiple)) { foreach ($questionsMultiple as $questionId => $choices) { $question = $em->getRepository('EvrikaMainBundle:PollQuestion')->findOneById($questionId); $question->addVote(); $questionIds[] = $questionId; foreach ($choices as $choiceId => $on) { $choice = $em->getRepository('EvrikaMainBundle:PollChoice')->findOneById($choiceId); $choice->addVote(); } } } # добавляем свободные ответы на свободные вопросы (PollAsk) if (!empty($asks)) { foreach ($asks as $askId => $text) { $ask = $em->getRepository('EvrikaMainBundle:PollAsk')->findOneById($askId); $text = trim($text); if ($ask && !empty($text) && $ask->getPoll()->getId() == $pollId) { $comment = new PollComment(); $comment->setAsk($ask); $comment->setText($text); $em->persist($comment); } $askIds[] = $askId; } } # проверяем, что ответили на все обязательные выборочные вопросы foreach ($poll->getQuestions() as $question) { if ($question->getRequired() && !in_array($question->getId(), $questionIds)) { $this->get('session')->getFlashBag()->add('blank', ''); return $this->render('EvrikaMainBundle:Info:opros_borjomi.html.twig', $params); } } # проверяем, что ответили на все обязательные открытые вопросы foreach ($poll->getAsks() as $ask) { if ($ask->getRequired() && !in_array($ask->getId(), $askIds)) { $this->get('session')->getFlashBag()->add('blank', ''); return $this->render('EvrikaMainBundle:Info:opros_borjomi.html.twig', $params); } } # сохраняем голос в базе данных, выставляем печеньку и сессию о голосовании, добавляем оповещение $response = new RedirectResponse($this->generateUrl('opros_borjomi')); $em->flush(); $session->set($checkName, ''); $cookie = new Cookie($checkName, '', time() + 3600 * 24 * 90); $response->headers->setCookie($cookie); $this->get('session')->getFlashBag()->add('notice', ''); return $response; } if (!$alreadyVoted) { $params['form'] = $form->createView(); } return $this->render('EvrikaMainBundle:Info:opros_borjomi.html.twig', $params); }