/** * Handle the event. * * @param VoteUpdated $event * @return void */ public function handle(VoteUpdated $event) { $vote = $event->vote; $dirty = $vote->getDirty(); if (!isset($dirty['position'])) { //Position hasn't been changed return true; } $original = $vote->getOriginal(); if ($original['position'] <= 0 && $dirty['position'] <= 0) { //Don't delete if changed from neutral to abstrain return true; } $oldCommentVotes = CommentVote::where('vote_id', $vote->id)->onCommentsOfPosition($original['position'])->notUser($vote->user_id)->delete(); // Comment Votes Cast by this user the original way $newCommentVotes = CommentVote::where('vote_id', $vote->id)->onCommentsOfPosition($vote['position'])->restore(); //Comment Votes Cast by this user the new way if ($vote->comment) { //echo $original['position']; Can not figure this out, just want to delete the // DB::enableQueryLog(); //$commentsToDelete = CommentVote::where('comment_id',$vote->comment->id)->onCommentsOfPosition($original['position'])->notUser($vote->user_id)->delete(); // Delete the comment votes of this User // return $commentsToDelete; CommentVote::where('comment_id', $vote->comment->id)->forceDelete(); // Delete the comment votes of this User // print_r(DB::getQueryLog()); } return true; }
public function store(Requests\CommentVoteRequest $request) { $commentId = $request->input('commentId'); $userId = $request->user()->id; $value = $request->input('value'); // Check to see if there is an existing vote $vote = CommentVote::whereCommentId($commentId)->whereUserId($userId)->first(); if (!$vote) { // First time the user is voting CommentVote::create(['comment_id' => $commentId, 'user_id' => $userId, 'value' => $value]); } else { $vote->value == $value ? $vote->delete() : $vote->update(['value' => $value]); } // AJAX JSON RESPONSE return response()->json(['status' => 'success', 'msg' => 'Vote has been added.']); }
/** * Commend a comment * * @param Request $request * @return Response (JSON) */ public function up(Request $request, Comment $comment) { $address = request()->ip(); foreach ($this->commentVotes->forComment($comment) as $vote) { if ($vote->address === $address) { return response()->json(['votes' => $comment->voteCount, 'error' => 'This IP Address has already commended this comment']); } } $vote = new CommentVote(); $vote->address = $address; $vote->comment_id = $comment->id; $vote->save(); $comment->voteCount = $comment->voteCount + 1; $comment->save(); return response()->json(['votes' => $comment->voteCount]); }
/** * Display a listing of the motion's comments, this code could almost certainly be done better * * @return Response */ public function index($motion) { $comments = array(); if (Auth::user()->can('view-comments')) { //A full admin who can see whatever $comments['agreeComments'] = Comment::with('vote.user', 'commentVotes')->where('motion_id', $motion->id)->agree()->get()->sortByDesc('commentRank')->toArray(); $comments['disagreeComments'] = Comment::with('vote.user', 'commentVotes')->where('motion_id', $motion->id)->disagree()->get()->sortByDesc('commentRank')->toArray(); } else { //Load the standard cached comments for the page $comments = Cache::remember('motion' . $motion->id . '_comments', Setting::get('comments.cachetime', 60), function () use($motion) { $comments['agreeComments'] = Comment::with('vote.user', 'commentVotes')->where('motion_id', $motion->id)->agree()->get()->sortByDesc('commentRank')->toArray(); $comments['disagreeComments'] = Comment::with('vote.user', 'commentVotes')->where('motion_id', $motion->id)->disagree()->get()->sortByDesc('commentRank')->toArray(); return $comments; }); } $comments['thisUsersComment'] = Comment::where('motion_id', $motion->id)->with('vote')->where('user_id', Auth::user()->id)->first(); $comments['thisUsersCommentVotes'] = CommentVote::where('motion_id', $motion->id)->where('user_id', Auth::user()->id)->get(); return $comments; }
/** * Get the votes for this comment * * @param Comment $comment * @return Integer */ public function forComment(Comment $comment) { return CommentVote::where('comment_id', $comment->id)->get(); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy(CommentVote $commentVote) { if (Auth::user()->id != $commentVote->user_id && !Auth::user()->can('delete-comment_votes')) { abort(401, "User does not have permission to delete this Comment Vote"); } $commentVote->forceDelete(); //There are no things relying on this and they might decide to create a new one return $commentVote; }