/** * fills in the poll * * @param Request $request * @param Poll $poll * @param $position * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response * @Route("/poll/{poll}/answer/{position}", name="answer") * @Security("has_role('ROLE_POLLSTER')") * */ public function fillInAction(Request $request, Poll $poll, $position) { /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); $question = new Question($em); $question->setPoll($poll); $questionDefinition = $this->get('poll.question_definition_provider')->getQuestionDefinition($poll, $position); if ($questionDefinition == null) { $poll->setCompleted(true); $em->flush(); return $this->redirect($this->generateUrl('poll_success', array('poll' => $poll->getId()))); } $question->setQuestionDefinition($questionDefinition); $form = $this->createForm(new QuestionType(), $question, array('method' => 'POST')); $form->add('submit', 'submit', array('label' => 'Dalej')); $form->handleRequest($request); if ($form->isValid()) { $poll->setLastAnsweredQuestion($position); $em->flush(); return $this->redirect($this->generateUrl('answer', array('poll' => $poll->getId(), 'position' => $position + 1))); } return $this->render('answer/form.html.twig', array('form' => $form->createView(), 'poll' => $poll->getId(), 'questionDefinition' => $questionDefinition)); }
/** * returns answers count given in the form * @param Question $question * @return int */ public function countAnswers($question) { $optionDefinitions = $question->getQuestionDefinition()->getOptionDefinitions(); $answers = $question->getPoll()->getAnswers(); $answersCount = 0; foreach ($optionDefinitions as $optionDefinition) { /** @var Answer $answer */ foreach ($answers as $answer) { if ($answer->getOptionDefinition()->getId() == $optionDefinition->getId() && $answer->getChecked()) { $answersCount += 1; } } } return $answersCount; }