Skip to content

Commit

Permalink
Merge pull request #257 from Hyleus/poll
Browse files Browse the repository at this point in the history
Improve the Poll System
  • Loading branch information
HDVinnie authored Apr 10, 2018
2 parents cca8a2c + 0fb3a56 commit 01ae9aa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
18 changes: 17 additions & 1 deletion app/Http/Controllers/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\Http\Requests;
use App\Http\Requests\StorePoll;
use App\Http\Requests\VoteOnPoll;
use App\Shoutbox;

use \Toastr;

Expand All @@ -45,6 +46,12 @@ public function index()
public function show($slug)
{
$poll = Poll::whereSlug($slug)->firstOrFail();
$user = auth()->user();
$user_has_voted = $poll->voters->where('user_id', $user->id)->isNotEmpty();

if ($user_has_voted) {
return $this->result($slug);
}

return view('poll.show', compact('poll'));
}
Expand Down Expand Up @@ -72,6 +79,11 @@ public function vote(VoteOnPoll $request)
]);
}

$slug = $poll->slug;
$url = config('app.url');
$title = $poll->title;
Shoutbox::create(["user" => 1, "mentions" => 1, "message" => "An user has voted on poll [url=${url}/poll/$slug]${title}[/url]"]);
cache()->forget("shoutbox_messages");
Toastr::success('Your vote has been counted.', 'Yay!', ['options']);

return redirect('poll/' . $poll->slug . '/result');
Expand All @@ -80,7 +92,11 @@ public function vote(VoteOnPoll $request)
public function result($slug)
{
$poll = Poll::whereSlug($slug)->firstOrFail();
$map = [
'poll' => $poll,
'total_votes' => $poll->totalVotes()
];

return view('poll.result', compact('poll'));
return view('poll.result', $map);
}
}
9 changes: 9 additions & 0 deletions app/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ public function makeSlugFromTitle($title)
$count = $this->where('slug', 'LIKE', "%$slug%")->count();
return $count ? "{$slug}-{$count}" : $slug;
}

public function totalVotes()
{
$result = 0;
foreach ($this->options as $option) {
$result += $option->votes;
}
return $result;
}
}
8 changes: 6 additions & 2 deletions resources/views/poll/result.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
</div>
<div class="panel-body">
@foreach ($poll->options as $option)
@php
$vote_value = $total_votes !== 0 ? ($option->votes / $total_votes) * 100 : 0;
$vote_value = round($vote_value, 2);
@endphp
<strong>{{ $option->name }}</strong><span class="pull-right">{{ $option->votes }} {{ trans('poll.votes') }}</span>
<div class="progress">
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="{{ $option->votes }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $option->votes }}%;">
{{ $option->votes }}
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="{{ $vote_value }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ $vote_value }}%;">
{{ $vote_value }}%
</div>
</div>
@endforeach
Expand Down

0 comments on commit 01ae9aa

Please sign in to comment.