Browse Source

Implement poll creation

master
Les De Ridder 1 year ago
parent
commit
bf96efcc64

+ 51
- 19
app/Http/Controllers/PollController.php View File

@@ -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)

+ 40
- 9
app/Poll.php View File

@@ -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;
}
}

+ 17
- 0
app/PollOption.php View File

@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PollOption extends Model
{
public $timestamps = false;

protected $fillable = ['text'];

public function poll()
{
return $this->belongsTo('App\Poll');
}
}

+ 15
- 0
app/PollVote.php View File

@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PollVote extends Model
{
public $timestamps = false;

public function poll()
{
return $this->belongsTo('App\Poll');
}
}

+ 36
- 0
app/PollVotingCode.php View File

@@ -0,0 +1,36 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PollVotingCode extends Model
{
public $timestamps = false;

public $keyType = "string";

public function __construct()
{
parent::__construct();

$this->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');
}
}

+ 36
- 0
database/migrations/2018_10_15_235354_create_poll_options_table.php View File

@@ -0,0 +1,36 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePollOptionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('poll_options', function (Blueprint $table) {
$table->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');
}
}

+ 37
- 0
database/migrations/2018_10_15_235411_create_poll_voting_codes_table.php View File

@@ -0,0 +1,37 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePollVotingCodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('poll_voting_codes', function (Blueprint $table) {
$table->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');
}
}

+ 36
- 0
database/migrations/2018_10_15_235441_create_poll_votes_table.php View File

@@ -0,0 +1,36 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePollVotesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('poll_votes', function (Blueprint $table) {
$table->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');
}
}

+ 4
- 4
resources/views/create_poll.blade.php View File

@@ -19,13 +19,13 @@
@for ($i = 0; $i < 5; $i++)
<section class="grid grid--large">
<div class="textfield">
<input type="text" class="option" name="option[]" placeholder="Enter a poll option" @if($i == 0 ) required @endif>
<input type="text" class="option" name="options[]" placeholder="Enter a poll option" @if($i == 0 ) required @endif>
</div>
<div class="textfield">
<input type="text" class="option" name="option[]" placeholder="Enter a poll option" @if($i == 0 ) required @endif>
<input type="text" class="option" name="options[]" placeholder="Enter a poll option" @if($i == 0 ) required @endif>
</div>
<div class="textfield">
<input type="text" class="option" name="option[]" placeholder="Enter a poll option">
<input type="text" class="option" name="options[]" placeholder="Enter a poll option">
</div>
</section>
@endfor
@@ -33,7 +33,7 @@
<section class="grid grid--large">
<div class="some-top-margin">
<label class="checkbox no-bottom-margin">
<input type="checkbox" name="multiple_answers_allowed">
<input type="checkbox" name="allow_multiple_answers">
<span class="checkbox__label">Allow multiple answers</span>
</label>
<br>

Loading…
Cancel
Save