Пример #1
0
 /**
  * Method for voting a task
  *
  * @return mixed
  */
 public function voteAction()
 {
     $this->view->disable();
     if (!$this->request->isPost()) {
         return $this->response->redirect($this->router->getControllerName());
     }
     $way = 'positive';
     if ($this->request->getPost('way') == 'negative') {
         $way = 'negative';
     }
     $objectId = $this->request->getPost('objectId');
     $object = $this->request->getPost('object');
     $vote = Vote::vote($objectId, $object, $way);
     $user = Users::findFirstById($this->auth->getAuth()['id']);
     $this->setJsonResponse();
     if (!$user) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'You need to login first'];
         return $this->jsonMessages;
     }
     if ($object == Vote::OBJECT_POSTS) {
         if (!($post = Posts::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post does not exist'];
             return $this->jsonMessages;
         }
         $this->setPointPost($way, $user, $post);
         //Adding notification when you have receive vote on the post, and not for now for post replies
         if ($user->getId() != $post->getUsersId()) {
             $this->setActivityNotifications($user, $post);
         }
     }
     if ($object == Vote::OBJECT_POSTS_REPLY) {
         if (!($postReply = PostsReply::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post reply does not exist'];
             return $this->jsonMessages;
         }
         //Set karam Voting someone else's post (positive or negative) on posts reply
         $this->setPointReply($way, $user, $postReply);
     }
     if (!$vote) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Vote have a problem :)'];
         return $this->jsonMessages;
     }
     if ($user->getVote() <= 0) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => t('You don\'t have enough votes available :)')];
         return $this->jsonMessages;
     }
     //checking the user have already voted this post yet
     if (is_array($vote)) {
         $this->jsonMessages['messages'][] = $vote;
         return $this->jsonMessages;
     }
     if ($this->request->isAjax()) {
         $vote = (new Vote())->getVotes($objectId, $object);
         return ['data' => $vote['positive'] - $vote['negative']];
     }
     echo 0;
     return 0;
 }
Пример #2
0
 /**
  * Add Vote up or down for the post
  *
  * @param  integer $objectId this is id of posts
  * @param  string  $object   there are some object comments, posts, postsReplay see constant the above
  * @param  string  $way      positive or negative
  * @return boolen|array
  */
 public static function vote($objectId, $object, $way)
 {
     $auth = \Phalcon\DI::getDefault()->getAuth();
     $conditions = ['usersId = :usersId: AND objectId = :objectId: AND object = :object:', 'bind' => ['usersId' => $auth->getAuth()['id'], 'objectId' => $objectId, 'object' => $object]];
     if (!($vote = Vote::findFirst($conditions))) {
         $vote = new self();
     }
     // When the posts have vote then user is existing
     if ($vote->getUsersId()) {
         switch ($object) {
             case Vote::OBJECT_POSTS:
                 return ['type' => 'error', 'content' => t('You have already voted this post')];
                 break;
             default:
                 return ['type' => 'error', 'content' => t('You have already voted this post reply')];
                 break;
         }
     }
     if ($way == 'positive') {
         $vote->voteUp($objectId, $object);
     }
     if ($way == 'negative') {
         $vote->voteDown($objectId, $object);
     }
     if (!$vote->save()) {
         foreach ($vote->getMessages() as $m) {
             error_log($m->getMessage());
             //return ['type' => 'error','content' => $m->getMessage()];
         }
         return false;
     }
     return true;
 }
Пример #3
0
 /**
  * Check whether the user can have the badge
  *
  * @param  Users $user
  * @return boolean
  */
 public function canHave(Users $user)
 {
     $canHave = Vote::count(['usersId = ?0 AND positive = 1', 'bind' => [$user->getId()]]) > 0;
     return $canHave;
 }