Implement poll voting
This commit is contained in:
parent
562c45713d
commit
a2d2d6e557
|
@ -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)
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -167,6 +167,7 @@ return [
|
|||
App\Providers\RouteServiceProvider::class,
|
||||
|
||||
Stolz\HtmlTidy\ServiceProvider::class,
|
||||
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
@ -7,25 +7,34 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio";
|
|||
@endphp
|
||||
|
||||
@section('content')
|
||||
<form>
|
||||
<section class="grid grid--large">
|
||||
<div>
|
||||
@if($hasVoted)
|
||||
<section>
|
||||
<span>You have already voted on this poll.</span>
|
||||
|
||||
<br>
|
||||
|
||||
@if($poll->results_visible)
|
||||
<div class="some-top-margin">
|
||||
<span><a href="{{ action('PollController@viewResults', ['poll' => $poll]) }}">Results</a></span>
|
||||
</div>
|
||||
@endif
|
||||
</section>
|
||||
@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="option" value="{{$option->id}}">
|
||||
<input type="{{$type}}" name="options[]" 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>
|
||||
|
||||
<section class="some-top-margin">
|
||||
<input type="submit" class="btn" value="Submit vote">
|
||||
</section>
|
||||
</form>
|
||||
@endif
|
||||
@endsection
|
||||
|
|
|
@ -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…
Reference in New Issue