From bf96efcc64d2211239ee4bc7558729cfe062eb67 Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Tue, 16 Oct 2018 03:23:19 +0200 Subject: [PATCH] Implement poll creation --- app/Http/Controllers/PollController.php | 70 ++++++++++++++----- app/Poll.php | 49 ++++++++++--- app/PollOption.php | 17 +++++ app/PollVote.php | 15 ++++ app/PollVotingCode.php | 36 ++++++++++ ...10_15_235354_create_poll_options_table.php | 36 ++++++++++ ..._235411_create_poll_voting_codes_table.php | 37 ++++++++++ ...8_10_15_235441_create_poll_votes_table.php | 36 ++++++++++ resources/views/create_poll.blade.php | 8 +-- 9 files changed, 272 insertions(+), 32 deletions(-) create mode 100644 app/PollOption.php create mode 100644 app/PollVote.php create mode 100644 app/PollVotingCode.php create mode 100644 database/migrations/2018_10_15_235354_create_poll_options_table.php create mode 100644 database/migrations/2018_10_15_235411_create_poll_voting_codes_table.php create mode 100644 database/migrations/2018_10_15_235441_create_poll_votes_table.php diff --git a/app/Http/Controllers/PollController.php b/app/Http/Controllers/PollController.php index 72b2f29..6e731e3 100644 --- a/app/Http/Controllers/PollController.php +++ b/app/Http/Controllers/PollController.php @@ -3,8 +3,8 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; - use Carbon\Carbon; +use Validator; use App\Poll; @@ -17,35 +17,67 @@ class PollController extends Controller public function create(Request $request) { + if($request->has('options')) { + $request['options'] = array_filter($request->input('options'), function($i) { return $i !== null; }); + } + + $request['allow_multiple_answers'] = $request->has('allow_multiple_answers'); + $request['hide_results_until_closed'] = $request->has('hide_results_until_closed'); + $request['automatically_close_poll'] = $request->has('automatically_close_poll'); + $request['set_admin_password'] = $request->has('set_admin_password'); + $validatedInput = $request->validate([ 'question' => 'required|string', - 'option' => 'required|min:3|distinct' - - /* - question: Are traps gay? - option[]: Yes - option[]: No - multiple_answers_allowed: on - hide_results_until_closed: on - automatically_close_poll: on - automatically_close_poll_datetime: 2018-10-12T18:45 - set_admin_password: on - admin_password: sadasdasdasdasdas - duplicate_vote_checking: codes - number_of_codes: 10 - */ + 'options' => 'required|min:2|distinct', + 'allow_multiple_answers' => 'required|boolean', + 'hide_results_until_closed' => 'required|boolean', + 'automatically_close_poll' => 'required|boolean', + 'automatically_close_poll_datetime' => 'required_if:automatically_close_poll,true|date|after:now', + 'set_admin_password' => 'required|boolean', + 'admin_password' => 'required_if:set_admin_password,true|nullable|string', + 'duplicate_vote_checking' => 'required|in:none,cookies,codes', + 'number_of_codes' => 'required_if:duplicate_vote_checking,codes|integer|min:2' ]); - debug($validatedInput); - $poll = new Poll; + $poll->question = $validatedInput['question']; + $poll->duplicate_vote_checking = $validatedInput['duplicate_vote_checking']; + $poll->allow_multiple_answers = $validatedInput['allow_multiple_answers']; + $poll->hide_results_until_closed = $validatedInput['hide_results_until_closed']; $poll->created_at = Carbon::now(); + if($validatedInput['automatically_close_poll']) { + $poll->closes_at = Carbon::parse($validatedInput['automatically_close_poll_datetime']); + } + if($validatedInput['set_admin_password']) { + $poll->admin_password = $validatedInput['admin_password']; + } + $poll->save(); - return view('create_poll'); + $poll->options()->createMany(array_map(function($i) { return ['text' => $i]; }, $validatedInput['options'])); + $poll->save(); + + if($poll->duplicate_vote_checking == 'codes') { + $codes = $poll->createVotingCodes($validatedInput['number_of_codes']); + } + + return redirect()->action('PollController@view', ['poll' => $poll])->with('new', true); } public function view(Request $request, Poll $poll) { + $new = $request->session()->pull('new', false); + + if($request->format() == 'json') { + if($new) { + + } else { + + } + + return null; //TODO: Implement JSON output + } else { + return view('view_poll')->with('poll', $poll)->with('new', $new); + } } public function vote(Request $request, Poll $poll) diff --git a/app/Poll.php b/app/Poll.php index f4f24cc..014297f 100644 --- a/app/Poll.php +++ b/app/Poll.php @@ -6,23 +6,54 @@ use Illuminate\Database\Eloquent\Model; class Poll extends Model { + public $timestamps = false; + + public $keyType = "string"; + public function __construct() { parent::__construct(); - $this->id = Poll::create_id(); + $this->id = Poll::createId(); } - private static function create_id() + private static function createId() { //TODO: Check if id is unique - $characters = 'abcdefghijklmnopqrstuvwxyz'; - $id = ''; - for($i = 0; $i < 6; $i++) - { - $id .= $characters[rand(0, strlen($characters) - 1)]; - } - return $id; + $characters = 'abcdefghijklmnopqrstuvwxyz'; + $id = ''; + for($i = 0; $i < 6; $i++) { + $id .= $characters[rand(0, strlen($characters) - 1)]; + } + return $id; + } + + public function options() + { + return $this->hasMany('App\PollOption'); + } + + public function votes() + { + return $this->hasMany('App\PollVote'); + } + + public function voting_codes() + { + return $this->hasMany('App\PollVotingCode'); + + } + + public function createVotingCodes($n) + { + $codes = []; + for($i = 0; $i < $n; $i++) { + $codes[] = new PollVotingCode; + } + + $this->voting_codes()->saveMany($codes); + + return $codes; } } diff --git a/app/PollOption.php b/app/PollOption.php new file mode 100644 index 0000000..cf291e1 --- /dev/null +++ b/app/PollOption.php @@ -0,0 +1,17 @@ +belongsTo('App\Poll'); + } +} diff --git a/app/PollVote.php b/app/PollVote.php new file mode 100644 index 0000000..03ed12b --- /dev/null +++ b/app/PollVote.php @@ -0,0 +1,15 @@ +belongsTo('App\Poll'); + } +} diff --git a/app/PollVotingCode.php b/app/PollVotingCode.php new file mode 100644 index 0000000..bb71ace --- /dev/null +++ b/app/PollVotingCode.php @@ -0,0 +1,36 @@ +id = PollVotingCode::createId(); + } + + private static function createId() + { + //TODO: Check if id is unique + + $characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; + $id = ''; + for($i = 0; $i < 32; $i++) { + $id .= $characters[rand(0, strlen($characters) - 1)]; + } + return $id; + } + + public function poll() + { + return $this->belongsTo('App\Poll'); + } +} diff --git a/database/migrations/2018_10_15_235354_create_poll_options_table.php b/database/migrations/2018_10_15_235354_create_poll_options_table.php new file mode 100644 index 0000000..c790b5f --- /dev/null +++ b/database/migrations/2018_10_15_235354_create_poll_options_table.php @@ -0,0 +1,36 @@ +increments('id'); + $table->string('text'); + $table->char('poll_id', 6); + + $table->foreign('poll_id')->references('id')->on('polls'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $table->dropForeign(['poll_id']); + + Schema::dropIfExists('poll_options'); + } +} diff --git a/database/migrations/2018_10_15_235411_create_poll_voting_codes_table.php b/database/migrations/2018_10_15_235411_create_poll_voting_codes_table.php new file mode 100644 index 0000000..997cbd7 --- /dev/null +++ b/database/migrations/2018_10_15_235411_create_poll_voting_codes_table.php @@ -0,0 +1,37 @@ +char('id', 32); + $table->boolean('used')->default(false); + $table->char('poll_id', 6); + + $table->primary('id'); + $table->foreign('poll_id')->references('id')->on('polls'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $table->dropForeign(['poll_id']); + + Schema::dropIfExists('poll_voting_codes'); + } +} diff --git a/database/migrations/2018_10_15_235441_create_poll_votes_table.php b/database/migrations/2018_10_15_235441_create_poll_votes_table.php new file mode 100644 index 0000000..2126a82 --- /dev/null +++ b/database/migrations/2018_10_15_235441_create_poll_votes_table.php @@ -0,0 +1,36 @@ +char('poll_id', 6); + $table->unsignedInteger('poll_option_id'); + + $table->foreign('poll_id')->references('id')->on('polls'); + $table->foreign('poll_option_id')->references('id')->on('poll_options'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $table->dropForeign(['poll_id', 'poll_option_id']); + + Schema::dropIfExists('poll_votes'); + } +} diff --git a/resources/views/create_poll.blade.php b/resources/views/create_poll.blade.php index dec80a1..9b621d9 100644 --- a/resources/views/create_poll.blade.php +++ b/resources/views/create_poll.blade.php @@ -19,13 +19,13 @@ @for ($i = 0; $i < 5; $i++)
- +
- +
- +
@endfor @@ -33,7 +33,7 @@