/** * @Given /^User "([^"]*)" answered to questions$/ */ public function userAnsweredToQuestions($username, TableNode $table) { $em = $this->getEm(); $user = $em->getRepository(User::class)->findOneByUsername($username); $hash = $table->getHash(); foreach ($hash as $row) { /* @var $question Question */ $question = $em->getRepository(Question::class)->findOneBySubject($row['question']); $option = $question->getOptions()->first(); $answer = new Answer(); $answer->setQuestion($question); $answer->setOption($option); $answer->setUser($user); $answer->setComment(''); $em->persist($answer); $em->flush($answer); } }
public function load(ObjectManager $manager) { $petition = $this->getReference('representativePetitionPublished1'); $user1 = $this->getReference('user-mobile1'); $signOption = new Option(); $signOption->setValue('sign'); $petition->addOption($signOption); $manager->persist($signOption); $answer = new Answer(); $answer->setQuestion($petition); $answer->setPrivacy(Answer::PRIVACY_PUBLIC); $answer->setUser($user1); $answer->setComment('Test comment'); $answer->setOption($signOption); $manager->persist($answer); $petition->addAnswer($answer); $manager->persist($petition); $manager->flush(); }
public function setOption(\Civix\CoreBundle\Entity\Poll\Option $option = NULL) { $this->__load(); return parent::setOption($option); }
/** * Add new Answer * * @Route("/question/{question_id}/answer/add", requirements={"question_id"="\d+"}, name="api_answer_add") * @Method("POST") */ public function answerAddAction(Request $request) { $entityManager = $this->getDoctrine()->getManager(); $dispatcher = $this->get('event_dispatcher'); /** @var $user User */ $user = $this->getUser(); /** @var $question Question */ $question = $entityManager->getRepository('CivixCoreBundle:Poll\\Question')->find($request->get('question_id')); if (is_null($question)) { throw new BadRequestHttpException('Question not found'); } /** @var $option Option */ $option = $entityManager->getRepository('CivixCoreBundle:Poll\\Option')->find($request->get('option_id')); if (is_null($option) || $option->getQuestion()->getId() !== $question->getId()) { throw new BadRequestHttpException('Wrong option ID'); } $isCanAnswer = $this->get('civix_core.poll.answer_manager')->checkAccessAnswer($user, $question); if (!$isCanAnswer) { throw new AccessDeniedHttpException(); } /** @var $answer Answer */ $answer = $entityManager->getRepository('CivixCoreBundle:Poll\\Answer')->findOneBy(array('option' => $option, 'user' => $user)); if (!is_null($answer)) { throw new BadRequestHttpException('User is already answered this question'); } $answer = new Answer(); $answer->setQuestion($question); $answer->setOption($option); $answer->setUser($user); $answer->setComment($request->get('comment')); $answer->setPrivacy($request->get('privacy')); $answer->setPaymentAmount($request->get('payment_amount')); $errors = $this->getValidator()->validate($answer, array('api-poll')); $response = new Response(); $response->headers->set('Content-Type', 'application/json'); if (count($errors) > 0) { $response->setStatusCode(400)->setContent(json_encode(array('errors' => $this->transformErrors($errors)))); } else { if ($question instanceof PaymentRequest && !$question->getIsCrowdfunding() && $answer->getCurrentPaymentAmount()) { $this->get('civix_core.stripe')->chargeToPaymentRequest($question, $answer, $user); } $entityManager->persist($answer); $entityManager->flush(); if ($question instanceof PaymentRequest && $question->getIsCrowdfunding() && $answer->getCurrentPaymentAmount()) { $entityManager->getRepository(PaymentRequest::class)->updateCrowdfundingPledgedAmount($answer); } //update activity responses $this->get('civix_core.activity_update')->updateResponsesQuestion($question); $this->get('civix_core.poll.answer_manager')->setVisibleAnswersForRecipent($answer); $this->get('civix_core.poll.comment_manager')->addCommentByQuestionAnswer($answer); $this->get('civix_core.social_activity_manager')->noticeAnsweredToQuestion($answer); $response->setContent($this->jmsSerialization($answer, array('api-answers-list'))); } return $response; }