Пример #1
0
 public function setVisibleAnswersForRecipent(Answer $answer)
 {
     /**
      * @var \Civix\CoreBundle\Entity\Poll\Question $question
      */
     $question = $answer->getQuestion();
     if ($question instanceof \Civix\CoreBundle\Entity\Poll\Question) {
         $specRepresentative = $question->getReportRecipient();
         $offTitleGroup = $question->getReportRecipientGroup();
         //Add specific representative to recipients of this question
         if (isset($specRepresentative)) {
             $question->addRecipient($specRepresentative);
         } elseif (isset($offTitleGroup)) {
             //check if user has representative with recient official title
             $districts = $answer->getUser()->getDistrictsIds();
             //check if user has districts (fill profile info)
             if (!empty($districts)) {
                 $representatives = $this->entityManager->getRepository('CivixCoreBundle:Representative')->getReprByDistrictsAndOffTitle($districts, $offTitleGroup);
                 //check if base has representatives with selected official title and districts
                 if ($representatives) {
                     foreach ($representatives as $recipient) {
                         if ($question->getUser() != $recipient) {
                             $question->addRecipient($recipient);
                         }
                     }
                 }
             }
         }
         $this->entityManager->persist($question);
         $this->entityManager->flush();
     }
 }
Пример #2
0
 public function load(ObjectManager $manager)
 {
     $question = $this->getReference('question1');
     $answers = array(array('reference' => 'answer1', 'comment' => 'comment1', 'privacy' => \Civix\CoreBundle\Entity\Poll\Answer::PRIVACY_PRIVATE, 'user' => $this->getReference('user-mobile1'), 'option' => $this->getReference('option-option1')), array('reference' => 'answer2', 'comment' => 'comment2', 'privacy' => \Civix\CoreBundle\Entity\Poll\Answer::PRIVACY_PUBLIC, 'user' => $this->getReference('user-mobile2'), 'option' => $this->getReference('option-option2')), array('reference' => 'answer3', 'comment' => 'comment3', 'privacy' => \Civix\CoreBundle\Entity\Poll\Answer::PRIVACY_PUBLIC, 'user' => $this->getReference('user-mobile3'), 'option' => $this->getReference('option-option3')), array('reference' => 'answer4', 'comment' => 'comment4', 'privacy' => \Civix\CoreBundle\Entity\Poll\Answer::PRIVACY_PUBLIC, 'user' => $this->getReference('user-mobile5'), 'option' => $this->getReference('option-option1')));
     foreach ($answers as $data) {
         $answer = new Answer();
         $answer->setComment($data['comment'])->setPrivacy($data['privacy'])->setUser($data['user'])->setOption($data['option'])->setQuestion($question);
         $this->addReference($data['reference'], $answer);
         $manager->persist($answer);
     }
     $manager->flush();
 }
 public function noticeAnsweredToQuestion(Answer $answer)
 {
     $question = $answer->getQuestion();
     if (!$question->getUser() instanceof Group) {
         return;
     }
     $target = ['id' => $question->getId(), 'type' => $question->getType()];
     $target['label'] = $this->getLabelByPoll($question);
     $target['preview'] = $this->getPreviewByPoll($question);
     $socialActivity = (new SocialActivity(SocialActivity::TYPE_ANSWERED, $answer->getUser(), $answer->getQuestion()->getUser()))->setTarget($target);
     $this->em->persist($socialActivity);
     $this->em->flush($socialActivity);
     return $socialActivity;
 }
Пример #4
0
 public function chargeToPaymentRequest(PaymentRequest $paymentRequest, Answer $answer, UserInterface $user)
 {
     $customerEntity = $this->cm->getCustomerByUser($user);
     /** @var Card $cardEntity */
     $cardEntity = $this->em->getRepository(Card::class)->findOneByCustomer($customerEntity);
     /** @var PaymentRequestOrder $orderEntity */
     $orderEntity = $this->em->getRepository(PaymentRequestOrder::class)->findOneByPaymentRequest($paymentRequest);
     $card = $this->bpc->getCard($cardEntity->getBalancedUri());
     $debitData = $card->debits->create(array('amount' => $answer->getCurrentPaymentAmount() * 100, 'order' => $orderEntity->getBalancedUri(), 'appears_on_statement_as' => 'PowerlinePay-' . $this->getAppearsOnStatement($paymentRequest->getUser()), 'description' => 'Powerline Payment: (' . $paymentRequest->getUser()->getOfficialName() . ') - (' . $paymentRequest->getTitle() . ')'));
     $paymentHistory = $this->saveToPaymentHistory($debitData, $customerEntity, $this->cm->getCustomerByUser($paymentRequest->getUser()), $answer->getCurrentPaymentAmount(), $paymentRequest->getId(), $orderEntity->getPublicId());
     if ($paymentHistory->isSucceeded()) {
         $this->es->sendPaymentRequestCharged($paymentHistory, $paymentRequest, $user);
     }
 }
Пример #5
0
 public function addCommentByQuestionAnswer(Answer $answer)
 {
     $parent = $this->em->getRepository('CivixCoreBundle:Poll\\Comment')->findOneBy(array('question' => $answer->getQuestion(), 'parentComment' => null));
     if ($answer->getComment()) {
         $comment = new Comment();
         $comment->setUser($answer->getUser())->setCommentBody($answer->getComment())->setQuestion($answer->getQuestion())->setPrivacy($answer->getPrivacy())->setParentComment($parent);
         return $this->saveNewComment($comment);
     }
 }
 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();
 }
Пример #7
0
 /**
  * @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 getCreatedAt()
 {
     $this->__load();
     return parent::getCreatedAt();
 }
 public function updateCrowdfundingPledgedAmount(Answer $answer)
 {
     $this->getEntityManager()->createQuery('UPDATE CivixCoreBundle:Poll\\Question\\PaymentRequest q
             SET q.crowdfundingPledgedAmount = COALESCE(q.crowdfundingPledgedAmount,0) + :amount WHERE q.id = :id')->setParameter('amount', $answer->getCurrentPaymentAmount())->setParameter('id', $answer->getQuestion()->getId())->getSingleScalarResult();
 }
Пример #10
0
 public function chargeToPaymentRequest(PaymentRequest $paymentRequest, Answer $answer, UserInterface $user)
 {
     $customer = $this->em->getRepository(Customer::getEntityClassByUser($user))->findOneBy(['user' => $user]);
     $account = $this->em->getRepository(Account::getEntityClassByUser($paymentRequest->getUser()))->findOneBy(['user' => $paymentRequest->getUser()]);
     $charge = new Charge($customer, $account, $paymentRequest->getId());
     $amount = $answer->getCurrentPaymentAmount() * 100;
     $sc = \Stripe\Charge::create(['amount' => $amount, 'application_fee' => ceil($amount * 0.021 + 50), 'currency' => 'usd', 'customer' => $customer->getStripeId(), 'statement_descriptor' => 'PowerlinePay-' . $this->getAppearsOnStatement($paymentRequest->getUser()), 'destination' => $account->getStripeId(), 'description' => 'Powerline Payment: (' . $paymentRequest->getUser()->getOfficialName() . ') - (' . $paymentRequest->getTitle() . ')']);
     $charge->updateStripeData($sc);
     $this->em->persist($charge);
     $this->em->flush($charge);
     return $charge;
 }
 /**
  * @Route("/answers/{id}/charges/")
  * @Method("GET")
  */
 public function charges(Answer $answer)
 {
     if ($answer->getUser() !== $this->getUser()) {
         throw $this->createNotFoundException();
     }
     $customer = $this->getDoctrine()->getRepository(Customer::getEntityClassByUser($this->getUser()))->findOneBy(['user' => $this->getUser()]);
     if (!$customer) {
         throw $this->createNotFoundException();
     }
     $charge = $this->getDoctrine()->getRepository(Charge::class)->findOneBy(['questionId' => $answer->getQuestion()->getId(), 'fromCustomer' => $customer]);
     return $this->createJSONResponse($this->jmsSerialization($charge->toArray(), ['api-answer-private']));
 }
Пример #12
0
 /**
  * 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;
 }