Skip to content

Commit

Permalink
Polls
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhowdy committed Feb 17, 2023
1 parent 798719f commit e37caa2
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 4 deletions.
24 changes: 24 additions & 0 deletions app/Http/Controllers/AdminPollController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Poll;

class AdminPollController extends Controller
{
/**
* Show the list of members
*
* @return Illuminate\View\View
*/
public function index()
{
$polls = Poll::latest()
->get();

return view('admin.polls-index', [
'polls' => $polls,
]);
}
}
59 changes: 58 additions & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use App\Mail\Contact;
use App\Models\ViewWhatsNewUpdate;
use App\Models\User;
use App\Models\Poll;
use App\Models\PollOption;
use App\Models\PollVote;
use Carbon\Carbon;

class HomeController extends Controller
Expand Down Expand Up @@ -50,9 +53,63 @@ public function home()
->orderBy('updated_at')
->paginate(30);

$poll = Poll::latest()->first();

$pollData = $poll->toArray();

$pollData['options'] = [];
$pollData['total_votes'] = 0;

foreach ($poll->options as $option)
{
$pollData['options'][$option->id] = $option->toArray();
$pollData['options'][$option->id]['total_votes'] = $pollData['options'][$option->id]['votes'];
$pollData['options'][$option->id]['votes'] = [];
}

foreach ($poll->votes as $vote)
{
$pollData['options'][$vote->option_id]['votes'][] = $vote->toArray();
$pollData['total_votes'] += 1;

if ($vote->created_user_id == $user->id)
{
$pollData['current_user_voted'] = true;
}
}

return view('home', [
'updates' => $updates
'updates' => $updates,
'poll' => $pollData
]);
}

/**
* vote
*
* @param Illuminate\Http\Request $request
* @return Illuminate\View\View
*/
public function vote(Request $request)
{
$request->validate([
'option' => ['required', 'int'],
]);

$option = PollOption::findOrFail($request->option);

$option->votes++;
$option->save();

$vote = new PollVote();

$vote->option_id = $request->option;
$vote->poll_id = $option->poll_id;
$vote->created_user_id = Auth()->user()->id;
$vote->updated_user_id = Auth()->user()->id;
$vote->save();

return redirect()->route('home');
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/InstallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public function adminStore(Request $request)
$admin->dob_month = $request->bmonth;
$admin->dob_day = $request->bday;
$admin->activated = true;
$admin->access = 1;

if ($request->has('lname'))
{
Expand Down
27 changes: 27 additions & 0 deletions app/Models/Poll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Poll extends Model
{
use HasFactory;

/**
* Get the options of the poll
*/
public function options()
{
return $this->hasMany(PollOption::class);
}

/**
* Get the votes the poll
*/
public function votes()
{
return $this->hasMany(PollVote::class);
}
}
11 changes: 11 additions & 0 deletions app/Models/PollComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PollComment extends Model
{
use HasFactory;
}
13 changes: 13 additions & 0 deletions app/Models/PollOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PollOption extends Model
{
use HasFactory;

public $timestamps = false;
}
11 changes: 11 additions & 0 deletions app/Models/PollVote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PollVote extends Model
{
use HasFactory;
}
28 changes: 27 additions & 1 deletion database/migrations/2022_11_07_194405_install_4_0_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Models\User;
use App\Models\Event;
use App\Models\EventCategory;
use App\Models\Poll;
use App\Models\PollOption;

class Install400 extends Migration
{
Expand Down Expand Up @@ -348,7 +350,7 @@ public function up()

Schema::create('poll_votes', function (Blueprint $table) {
$table->id();
$table->foreignId('option');
$table->foreignId('option_id');
$table->foreignId('poll_id');
$table->foreignId('created_user_id');
$table->foreignId('updated_user_id');
Expand All @@ -363,6 +365,30 @@ public function up()
$table->timestamps();
});

$poll = new Poll();

$poll->question = __('What do you think of Family Connections?');
$poll->created_user_id = 1;
$poll->updated_user_id = 1;

$poll->save();

$pollOptionData = [
__('Easy to use!'),
__('Visually appealing!'),
__('Just what our family needed!'),
];

foreach ($pollOptionData as $i => $option)
{
$pollOption = new PollOption();

$pollOption->poll_id = 1;
$pollOption->option = $option;

$pollOption->save();
}

Schema::create('prayers', function (Blueprint $table) {
$table->id();
$table->string('for', 50);
Expand Down
31 changes: 31 additions & 0 deletions resources/views/admin/polls-index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@extends('layouts.main')
@section('body-id', 'admin-members')

@section('content')
<div class="p-5">
<h2>{{ __('Polls') }}</h2>

<table class="table table-hover">
<thead>
<tr>
<th>{{ __('Id') }}</th>
<th>{{ __('Question') }}</th>
<th>{{ __('Date') }}</th>
<th>{{ __('Votes') }}</th>
</tr>
</thead>
<tbody>
@foreach ($polls as $poll)
<tr>
<td>{{ $poll->id }}</td>
<td>
<span class="fw-bold text-purple">{{ $poll->question }}</span>
</td>
<td>{{ $poll->created_at->format('M j, Y') }}</td>
<td>{{ count($poll->votes) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
36 changes: 35 additions & 1 deletion resources/views/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,41 @@
@endfor
</div>
<div class="col-auto col-3 p-5">
right sidebar
<div class="card mb-3">
<div class="card-header">
{{ __('Latest Poll') }}
</div>
<div class="card-body">
<h5 class="card-title">{{ $poll['question'] }}</h5>
@if (isset($poll['current_user_voted']))
@foreach ($poll['options'] as $option)
@php($percent = round(($option['total_votes'] / $poll['total_votes'] * 100), 0))
<div class="mb-3">
<div class="d-flex justify-content-between">
<div class="fw-bold">{{ $option['option'] }}</div>
<div class="small text-muted">{{ $option['total_votes'] }}</div>
</div>
<div class="progress">
<div class="progress-bar bg-info" role="progressbar" style="width: {{ $percent }}%;" aria-valuenow="{{ $percent }}" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
@endforeach
@else
<form action="{{ route('poll.vote') }}" method="post">
@csrf
<div class="mb-3">
@foreach ($poll['options'] as $option)
<div class="form-check">
<input class="form-check-input" type="radio" name="option" id="option-{{ $option['id'] }}" value="{{ $option['id'] }}">
<label class="form-check-label" for="option-{{ $option['id'] }}">{{ $option['option'] }}</label>
</div>
@endforeach
</div>
<button type="submit" class="btn btn-sm btn-info">{{ __('Vote') }}</button>
</form>
@endif
</div><!-- /.card-body -->
</div><!-- /.card -->
</div>
</div>
@endsection
12 changes: 11 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Http\Controllers\VideoController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\AdminMemberController;
use App\Http\Controllers\AdminPollController;
use Illuminate\Support\Facades\Route;

/*
Expand Down Expand Up @@ -51,6 +52,8 @@
Route::middleware(['auth'])->group(function () {
Route::any( '/home', [ HomeController::class, 'home' ])->name('home');

Route::post('/poll/vote', [ HomeController::class, 'vote' ])->name('poll.vote');

Route::get( '/me/profile', [ MeController::class, 'profileCreate' ])->name('my.profile');
Route::post('/me/profile', [ MeController::class, 'profileStore' ]);
Route::get( '/me/profile/avatar', [ MeController::class, 'avatarCreate' ])->name('my.avatar');
Expand Down Expand Up @@ -145,7 +148,14 @@
Route::post('/members/{id}/edit', [ AdminMemberController::class, 'update' ])->name('admin.members.update');

Route::get( '/photos', [ HomeController::class, 'home' ])->name('admin.photos');
Route::get( '/polls', [ HomeController::class, 'home' ])->name('admin.polls');

Route::get( '/polls', [ AdminPollController::class, 'index' ])->name('admin.polls');
Route::get( '/create', [ AdminPollController::class, 'create' ])->name('admin.polls.create');
Route::post('/create', [ AdminPollController::class, 'store' ]);
Route::get( '/polls/{id}', [ AdminPollController::class, 'show' ])->name('admin.polls.show');
Route::get( '/polls/{id}/edit', [ AdminPollController::class, 'edit' ])->name('admin.polls.show');
Route::post('/polls/{id}/edit', [ AdminPollController::class, 'update' ]);

Route::get( '/facebook', [ HomeController::class, 'home' ])->name('admin.facebook');
Route::get( '/google', [ HomeController::class, 'home' ])->name('admin.google');
Route::get( '/instagram', [ HomeController::class, 'home' ])->name('admin.instagram');
Expand Down

0 comments on commit e37caa2

Please sign in to comment.