コード例 #1
0
 public function answerToPetitition(UserPetition $userPetition, User $user, $optionId)
 {
     $this->errors = [];
     $currentDate = new \DateTime();
     if ($userPetition->getExpireAt() <= $currentDate) {
         $this->errors[] = 'You could not answer to expired micropetition';
         return false;
     }
     if ($userPetition->getUser() == $user) {
         $this->errors[] = 'You could not answer to your micropetition';
         return false;
     }
     if (array_search($optionId, $userPetition->getOptionsIds()) === false) {
         $this->errors[] = 'Incorrect answer\'s option';
         return false;
     }
     $answer = $this->entityManager->getRepository('CivixCoreBundle:Micropetitions\\Answer')->findOneBy(array('petition' => $userPetition, 'user' => $user));
     if ($answer && $answer->getOptionId() !== 3) {
         $this->errors[] = 'User is already answered this micropetition';
         return false;
     } else {
         if ($answer) {
             $this->entityManager->remove($answer);
         }
     }
     //add answers
     $answer = $this->entityManager->getRepository('CivixCoreBundle:Micropetitions\\Answer')->createAnswer($userPetition, $user, $optionId);
     $this->entityManager->persist($answer);
     $this->entityManager->flush();
     //update response count activity for this petition
     $this->activityUpdate->updateResponsesPetition($userPetition);
     //check if need to publish to activity
     if ($userPetition->getPublishStatus() == UserPetition::STATUS_USER) {
         if ($this->checkIfNeedPublish($userPetition)) {
             $this->activityUpdate->publishMicroPetitionToActivity($userPetition, true);
         }
     }
     return $answer;
 }
 public function getOptionsIds()
 {
     $this->__load();
     return parent::getOptionsIds();
 }