public function vote($isGood = false) { $vote = $this->myVote; if (empty($vote)) { $vote = new CommentVote(['commentID' => $this->id]); } $vote->operation = $isGood ? CommentVote::OPERATION_GOOD : CommentVote::OPERATION_BAD; $vote->save(false); return $vote->operation; }
public function dislikeCommentPost() { $id = Input::get('id'); $oppositeVote = CommentVote::where('type', 'like')->where('user_id', Auth::id())->where('comment_id', $id)->first(); if (!empty($oppositeVote)) { $oppositeVote->delete(); } $vote = new CommentVote(); $vote->user_id = Auth::id(); $vote->comment_id = $id; $vote->type = 'dislike'; $vote->save(); return 'success'; }
public function vote_down() { $id = Input::get('comment_id'); $comment_vote = CommentVote::where('user_id', '=', Auth::user()->id)->where('comment_id', '=', $id)->first(); if (isset($comment_vote->id)) { $comment_vote->up = 0; $comment_vote->down = 1; $comment_vote->save(); echo $comment_vote; } else { $vote = new CommentVote(); $vote->user_id = Auth::user()->id; $vote->comment_id = $id; $vote->down = 1; $vote->save(); echo $vote; } }
/** * Adds one point(either like or dislike) for the current model. * @param int $comment_id of the comment * @param int $vote wether like or dislike * @param int $user_id the user id * @return Array with the success(boolean indicating success or not), comment_id, likes and dislikes count */ public function add_vote($comment_id, $vote, $user_id) { $model = Comment::model()->findByPk((int) $comment_id); if ($model === null) { return array('success' => false, 'message' => Yii::t('comment', 'The comment_id was not found.')); } $likes = $model->likes; $dislikes = $model->dislikes; $model2 = new CommentVote(); $model2->comment_id = $comment_id; $model2->user_id = $user_id; $model2->vote = $vote == "like" ? 1 : 0; $model2->time = SiteLibrary::utc_time(); if (!$model2->save()) { return array('success' => false, 'message' => Yii::t('comment', 'Only one vote per user allowed.')); } if ($vote == "like") { $model->likes = $model->likes + 1; $likes = $model->likes; } else { $model->dislikes = $model->dislikes + 1; $dislikes = $model->dislikes; } $model->save(); //Update Live comments table if needed(if record doesnt exists, it simply wont update anything) Yii::app()->db->createCommand()->update('live_comment', array('likes' => $likes, 'dislikes' => $dislikes), 'comment_id=:comment_id', array(':comment_id' => $comment_id)); return array('success' => true, 'comment_id' => $comment_id, 'likes' => $likes, 'dislikes' => $dislikes); }