/** * Check whether the user can have the badge * * @param Users $user * @return boolean */ public function canHave(Users $user) { $canHave = PostsRepliesVotes::count(['users_id = ?0 AND vote = 1', 'bind' => [$user->id]]) > 0; $canHave = $canHave || PostsVotes::count(['users_id = ?0 AND vote = 1', 'bind' => [$user->id]]) > 0; return $canHave; }
/** * Votes a post down */ public function voteDownAction($id = 0) { $response = new Response(); /** * Find the post using get */ $postReply = PostsReplies::findFirstById($id); if (!$postReply) { $contentNotExist = array('status' => 'error', 'message' => 'Post reply does not exist'); return $response->setJsonContent($contentNotExist); } $user = Users::findFirstById($this->session->get('identity')); if (!$user) { $contentLogIn = array('status' => 'error', 'message' => 'You must log in first to vote'); return $response->setJsonContent($contentLogIn); } if ($user->votes <= 0) { $contentDontHave = array('status' => 'error', 'message' => 'You don\'t have enough votes available'); return $response->setJsonContent($contentDontHave); } $post = $postReply->post; if (!$post) { $contentPostNotExist = array('status' => 'error', 'message' => 'Post associated to the reply does not exist'); return $response->setJsonContent($contentPostNotExist); } if ($post->deleted) { $contentDeleted = array('status' => 'error', 'message' => 'Post associated to the reply is deleted'); return $response->setJsonContent($contentDeleted); } $parametersVoted = array('posts_replies_id = ?0 AND users_id = ?1', 'bind' => array($postReply->id, $user->id)); $voted = PostsRepliesVotes::count($parametersVoted); if ($voted) { $contentAleadyVoted = array('status' => 'error', 'message' => 'You have already voted this reply'); return $response->setJsonContent($contentAleadyVoted); } $postReplyVote = new PostsRepliesVotes(); $postReplyVote->posts_replies_id = $postReply->id; $postReplyVote->users_id = $user->id; $postReplyVote->vote = PostsRepliesVotes::VOTE_DOWN; if (!$postReplyVote->save()) { foreach ($postReplyVote->getMessages() as $message) { $contentError = array('status' => 'error', 'message' => $message->getMessage()); return $response->setJsonContent($contentError); } } $postReply->votes_down++; if ($postReply->users_id != $user->id) { if ($postReply->post->users_id == $user->id) { $karmaCount = intval(abs($user->karma - $postReply->user->karma) / 1000); $points = Karma::VOTE_DOWN_ON_MY_REPLY_ON_MY_POST + $karmaCount; } else { $points = Karma::VOTE_DOWN_ON_MY_REPLY + intval(abs($user->karma - $postReply->user->karma) / 1000); } $postReply->user->decreaseKarma($points); } if ($postReply->save()) { if ($postReply->users_id != $user->id) { $user->decreaseKarma(Karma::VOTE_DOWN_ON_SOMEONE_ELSE_REPLY); } $user->votes--; if (!$user->save()) { foreach ($user->getMessages() as $message) { $contentError = array('status' => 'error', 'message' => $message->getMessage()); return $response->setJsonContent($contentError); } } } $contentOk = array('status' => 'OK'); return $response->setJsonContent($contentOk); }
/** * Check whether the user can have the badge * * @param Users $user * @return boolean */ public function canHave(Users $user) { $canHave = PostsRepliesVotes::count(array('users_id = ?0 AND vote = -1', 'bind' => array($user->id))) > 0; $canHave = $canHave || PostsVotes::count(array('users_id = ?0 AND vote = -1', 'bind' => array($user->id))) > 0; return $canHave; }