diff --git a/app/Http/Controllers/PollController.php b/app/Http/Controllers/PollController.php index d17a468..422e7a5 100644 --- a/app/Http/Controllers/PollController.php +++ b/app/Http/Controllers/PollController.php @@ -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) diff --git a/app/Poll.php b/app/Poll.php index 014297f..14f2623 100644 --- a/app/Poll.php +++ b/app/Poll.php @@ -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()); + } } diff --git a/app/PollVote.php b/app/PollVote.php index 03ed12b..31c1cf6 100644 --- a/app/PollVote.php +++ b/app/PollVote.php @@ -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() diff --git a/config/app.php b/config/app.php index 6ebb328..335d546 100644 --- a/config/app.php +++ b/config/app.php @@ -167,6 +167,7 @@ return [ App\Providers\RouteServiceProvider::class, Stolz\HtmlTidy\ServiceProvider::class, + ], /* diff --git a/resources/views/view_poll.blade.php b/resources/views/view_poll.blade.php index 6397d40..467942a 100644 --- a/resources/views/view_poll.blade.php +++ b/resources/views/view_poll.blade.php @@ -7,25 +7,34 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio"; @endphp @section('content') -
-
-
- @foreach ($poll->options as $option) - + @if($hasVoted) +
+ You have already voted on this poll. -
- @endforeach -
-
- -
-
+
-
- + @if($poll->results_visible) +
+ Results +
+ @endif
-
+ @else +
$poll]) }}" method="post"> + @csrf + + @foreach ($poll->options as $option) + + +
+ @endforeach + +
+ +
+
+ @endif @endsection diff --git a/routes/web.php b/routes/web.php index 63de9b4..cacc23c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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');