/** * @param string $topicSlug * @param int $topicId * * @return \Illuminate\Http\RedirectResponse * * @throws \Exception */ public function undo($topicSlug, $topicId) { $topic = $this->topicRepository->find($topicId); if (!$topic) { throw new TopicNotFoundException(); } if (!$topic->has_poll) { throw new PollNotFoundException(); } $poll = $topic->poll; $pollPresenter = app()->make('MyBB\\Core\\Presenters\\Poll', [$poll]); if (!$this->guard->check()) { throw new PollNoGuestUndoException(); } if ($pollPresenter->is_closed) { throw new PollClosedException(); } $vote = $this->pollVoteRepository->findForUserPoll($this->guard->user(), $poll); if (!$vote) { // Error throw new PollNoUndoException(); } $votes = explode(',', $vote->vote); $options = $pollPresenter->options(); foreach ($votes as $option) { if (is_numeric($option) && 0 < $option && $option <= $pollPresenter->num_options()) { $options[$option - 1]['votes']--; } } $poll->update(['options' => $options]); $vote->delete(); return redirect()->route('polls.show', [$topicSlug, $topicId]); }
/** * @return mixed */ public function myVote() { if (!isset($this->cache['myVote'])) { if ($this->guard->check()) { $this->cache['myVote'] = $this->pollVoteRepository->findForUserPoll($this->guard->user(), $this->wrappedObject); } else { $this->cache['myVote'] = null; } } return $this->cache['myVote']; }