Browse Source

Implement poll voting

master
Les De Ridder 3 years ago
parent
commit
a2d2d6e557
6 changed files with 119 additions and 21 deletions
  1. +79
    -1
      app/Http/Controllers/PollController.php
  2. +5
    -1
      app/Poll.php
  3. +2
    -0
      app/PollVote.php
  4. +1
    -0
      config/app.php
  5. +28
    -19
      resources/views/view_poll.blade.php
  6. +4
    -0
      routes/web.php

+ 79
- 1
app/Http/Controllers/PollController.php View File

@ -6,7 +6,10 @@ use Illuminate\Http\Request;
use Carbon\Carbon;
use Validator;
use DB;
use App\Poll;
use App\PollVote;
use App\PollVotingCode;
class PollController extends Controller
{
@ -77,12 +80,87 @@ class PollController extends Controller
//TODO: Implement JSON output
return null;
} else {
return view('view_poll')->with('poll', $poll)->with('new', $new);
return view('view_poll')
->with('poll', $poll)
->with('new', $new)
->with('hasVoted', $this->hasVoted($request, $poll));
}
}
public function viewResults(Request $request, Poll $poll)
{
if($poll->results_visible) {
} else {
}
}
private static function createPieChart(Poll $poll)
{
//TODO
}
public function hasVoted(Request $request, Poll $poll)
{
if($poll->duplicate_vote_checking == 'cookies') {
if($request->session()->exists($poll->id)) {
return true;
}
} else if($poll->duplicate_vote_checking == 'codes') {
$code = PollVotingCode::find($request->query('code'));
if($code == null || $code->used) {
return true;
}
}
return false;
}
public function vote(Request $request, Poll $poll)
{
if($this->hasVoted($request, $poll)) {
return null;
}
if($poll->allow_multiple_answers) {
$validatedInput = $request->validate([
'options' => 'required|distinct',
]);
} else {
$validatedInput = $request->validate([
'options' => 'required|distinct|min:1|max:1',
]);
}
DB::beginTransaction();
foreach($validatedInput['options'] as $option)
{
//TODO: Properly display errors
if($poll->options()->find($option) == null) {
DB::rollBack();
return null;
}
$vote = new PollVote;
$vote->poll_option_id = $option;
$poll->votes()->save($vote);
}
DB::commit();
if($poll->duplicate_vote_checking == 'cookies') {
$request->session()->put($poll->id, null);
} else if($poll->duplicate_vote_checking == 'codes') {
$code->used = true;
$code->save();
}
//return redirect('PollController@viewResults', ['poll' => $poll]);
return view('view_poll')->with('poll', $poll)->with('new', false);
}
public function admin(Request $request, Poll $poll)


+ 5
- 1
app/Poll.php View File

@ -42,7 +42,6 @@ class Poll extends Model
public function voting_codes()
{
return $this->hasMany('App\PollVotingCode');
}
public function createVotingCodes($n)
@ -56,4 +55,9 @@ class Poll extends Model
return $codes;
}
public function getResultsVisibleAttribute()
{
return !$this->hide_results_until_closed || ($this->closes_at != null && $this->closes_at->isPast());
}
}

+ 2
- 0
app/PollVote.php View File

@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Model;
class PollVote extends Model
{
protected $primaryKey = null;
public $incrementing = false;
public $timestamps = false;
public function poll()


+ 1
- 0
config/app.php View File

@ -167,6 +167,7 @@ return [
App\Providers\RouteServiceProvider::class,
Stolz\HtmlTidy\ServiceProvider::class,
],
/*


+ 28
- 19
resources/views/view_poll.blade.php View File

@ -7,25 +7,34 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio";
@endphp
@section('content')
<form>
<section class="grid grid--large">
<div>
@foreach ($poll->options as $option)
<label class="{{$type}} no-bottom-margin">
<input type="{{$type}}" name="option" value="{{$option->id}}">
<span class="{{$type}}__label">{{$option->text}}</span>
</label>
<br>
@endforeach
</div>
<div>
<!-- TODO: Pie chart (http://lavacharts.com/#example-pie) -->
</div>
</section>
@if($hasVoted)
<section>
<span>You have already voted on this poll.</span>
<br>
<section class="some-top-margin">
<input type="submit" class="btn" value="Submit vote">
@if($poll->results_visible)
<div class="some-top-margin">
<span><a href="{{ action('PollController@viewResults', ['poll' => $poll]) }}">Results</a></span>
</div>
@endif
</section>
</form>
@else
<form action"={{ action('PollController@vote', ['poll' => $poll]) }}" method="post">
@csrf
@foreach ($poll->options as $option)
<label class="{{$type}} no-bottom-margin">
<input type="{{$type}}" name="options[]" value="{{$option->id}}">
<span class="{{$type}}__label">{{$option->text}}</span>
</label>
<br>
@endforeach
<section class="some-top-margin">
<input type="submit" class="btn" value="Submit vote">
</section>
</form>
@endif
@endsection

+ 4
- 0
routes/web.php View File

@ -19,8 +19,12 @@ Route::get('/poll', 'PollController');
Route::post('/poll', 'PollController@create');
Route::get('/poll/{poll}', 'PollController@view');
//TODO: Add admin GET route
Route::patch('/poll/{poll}', 'PollController@edit');
Route::get('/poll/{poll}/results', 'PollController@viewResults');
Route::post('/poll/{poll}/vote', 'PollController@vote');
Route::get('/poll/{poll}/edit', 'PollController@admin');

Loading…
Cancel
Save