add poker planing for students
This commit is contained in:
93
app/Http/Controllers/PokerController.php
Normal file
93
app/Http/Controllers/PokerController.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\EstimationRound;
|
||||
use App\Models\Vote;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PokerController extends Controller
|
||||
{
|
||||
// Публичная страница голосования
|
||||
public function showForm(string $token)
|
||||
{
|
||||
$round = EstimationRound::where('token', $token)->firstOrFail();
|
||||
$votedCount = $round->votes()->count();
|
||||
|
||||
if ($votedCount >= $round->max_voters) {
|
||||
return response('Лимит голосов исчерпан', 403);
|
||||
}
|
||||
|
||||
return view('vote', compact('round'));
|
||||
}
|
||||
|
||||
// Отправка голоса
|
||||
public function submitVote(Request $request, string $token)
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:100',
|
||||
'score' => 'required|integer|min:1'
|
||||
]);
|
||||
|
||||
$session = EstimationRound::where('token', $token)->firstOrFail();
|
||||
|
||||
if ($session->votes()->count() >= $session->max_voters) {
|
||||
return back()->withErrors(['msg' => 'Лимит участников достигнут']);
|
||||
}
|
||||
|
||||
if ($request->score > $session->max_score) {
|
||||
return back()->withErrors(['score' => 'Оценка не должна превышать ' . $session->max_score]);
|
||||
}
|
||||
|
||||
Vote::create([
|
||||
'estimation_round_id' => $session->id,
|
||||
'name' => $request->name,
|
||||
'score' => $request->score
|
||||
]);
|
||||
|
||||
return redirect()->route('vote.thanks');
|
||||
}
|
||||
|
||||
public function thanks()
|
||||
{
|
||||
return view('thanks');
|
||||
}
|
||||
|
||||
// Админка: создание сессии
|
||||
public function createEstimationRoundForm()
|
||||
{
|
||||
return view('admin.create');
|
||||
}
|
||||
|
||||
public function createEstimationRound(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'max_score' => 'required|integer|min:1',
|
||||
'max_voters' => 'required|integer|min:1|max:100'
|
||||
]);
|
||||
|
||||
$session = EstimationRound::create([
|
||||
'token' => Str::random(12),
|
||||
'max_score' => $request->max_score,
|
||||
'max_voters' => $request->max_voters
|
||||
]);
|
||||
|
||||
return redirect()->route('admin.sessions')
|
||||
->with('success', 'Сессия создана. Ссылка: ' . url('/s/' . $session->token));
|
||||
}
|
||||
|
||||
// Админка: список сессий
|
||||
public function listEstimationRounds()
|
||||
{
|
||||
$sessions = EstimationRound::withCount('votes')->latest()->get();
|
||||
return view('admin.sessions', compact('sessions'));
|
||||
}
|
||||
|
||||
// Админка: детали сессии
|
||||
public function showEstimationRound($id)
|
||||
{
|
||||
$round = EstimationRound::with('votes')->findOrFail($id);
|
||||
return view('admin.session', compact('round'));
|
||||
}
|
||||
}
|
||||
24
app/Http/Middleware/EnsureAdminAuthenticated.php
Normal file
24
app/Http/Middleware/EnsureAdminAuthenticated.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class EnsureAdminAuthenticated
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (!session()->get('admin_logged_in')) {
|
||||
return redirect('/admin/login');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
20
app/Models/EstimationRound.php
Normal file
20
app/Models/EstimationRound.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class EstimationRound extends Model
|
||||
{
|
||||
protected $fillable = ['token', 'max_score', 'max_voters'];
|
||||
|
||||
public function votes()
|
||||
{
|
||||
return $this->hasMany(Vote::class);
|
||||
}
|
||||
|
||||
public function getAverageScoreAttribute()
|
||||
{
|
||||
return $this->votes->avg('score');
|
||||
}
|
||||
}
|
||||
15
app/Models/Vote.php
Normal file
15
app/Models/Vote.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Vote extends Model
|
||||
{
|
||||
protected $fillable = ['estimation_round_id', 'name', 'score'];
|
||||
|
||||
public function estimationRound()
|
||||
{
|
||||
return $this->belongsTo(EstimationRound::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('estimation_rounds', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('token')->unique();
|
||||
$table->integer('max_score');
|
||||
$table->integer('max_voters');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('estimation_rounds');
|
||||
}
|
||||
};
|
||||
30
database/migrations/2025_11_15_074151_create_votes_table.php
Normal file
30
database/migrations/2025_11_15_074151_create_votes_table.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('votes', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('estimation_round_id')->constrained('estimation_rounds')->onDelete('cascade');
|
||||
$table->string('name');
|
||||
$table->integer('score');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('votes');
|
||||
}
|
||||
};
|
||||
2376
package-lock.json
generated
Normal file
2376
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
resources/views/admin/create.blade.php
Normal file
40
resources/views/admin/create.blade.php
Normal file
@@ -0,0 +1,40 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="bg-white p-6 rounded-xl shadow-md">
|
||||
<h1 class="text-2xl font-bold text-gray-800 mb-4">Создать новую сессию</h1>
|
||||
|
||||
@if (session('success'))
|
||||
<div class="mb-4 p-3 bg-green-50 text-green-700 rounded-lg text-sm">
|
||||
{{ session('success') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form method="POST" action="{{ route('admin.sessions.store') }}" class="space-y-4">
|
||||
@csrf
|
||||
<div>
|
||||
<label class="block text-gray-700 mb-1">Максимальная оценка</label>
|
||||
<input type="number" name="max_score" min="2" required
|
||||
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
placeholder="Например: 10">
|
||||
<p class="text-sm text-gray-500 mt-1">Участники будут выбирать от 1 до этого числа</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-gray-700 mb-1">Максимум участников</label>
|
||||
<input type="number" name="max_voters" min="1" max="100" required
|
||||
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
placeholder="Например: 10">
|
||||
</div>
|
||||
|
||||
<button type="submit"
|
||||
class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2.5 px-4 rounded-lg transition">
|
||||
Создать сессию
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="mt-6 pt-6 border-t">
|
||||
<a href="{{ route('admin.sessions') }}" class="text-blue-600 hover:underline">← Назад к списку</a>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
35
resources/views/admin/session.blade.php
Normal file
35
resources/views/admin/session.blade.php
Normal file
@@ -0,0 +1,35 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="bg-white p-6 rounded-xl shadow-md">
|
||||
<div class="mb-4">
|
||||
<a href="{{ route('admin.sessions') }}" class="text-blue-600 hover:underline text-sm">← Назад</a>
|
||||
</div>
|
||||
|
||||
<h1 class="text-2xl font-bold text-gray-800 mb-2">Сессия #{{ $round->id }}</h1>
|
||||
<p class="text-gray-600 mb-4">
|
||||
Диапазон: 1–{{ $round->max_score }} |
|
||||
Участников: {{ $round->votes->count() }} / {{ $round->max_voters }}
|
||||
</p>
|
||||
|
||||
<div class="bg-blue-50 p-4 rounded-lg mb-6">
|
||||
<p class="text-lg font-semibold text-blue-800">
|
||||
Средняя оценка: <span class="text-2xl">{{ number_format($round->average_score, 2) }}</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@if($round->votes->isEmpty())
|
||||
<p class="text-gray-500">Пока никто не проголосовал.</p>
|
||||
@else
|
||||
<h2 class="text-lg font-medium text-gray-800 mb-3">Участники:</h2>
|
||||
<div class="space-y-2">
|
||||
@foreach($round->votes as $vote)
|
||||
<div class="flex justify-between items-center bg-gray-50 px-4 py-2 rounded">
|
||||
<span class="font-medium">{{ $vote->name }}</span>
|
||||
<span class="bg-white px-3 py-1 rounded-full font-bold text-blue-700">{{ $vote->score }}</span>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
@endsection
|
||||
38
resources/views/admin/sessions.blade.php
Normal file
38
resources/views/admin/sessions.blade.php
Normal file
@@ -0,0 +1,38 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="space-y-6">
|
||||
<div class="flex justify-between items-center">
|
||||
<h1 class="text-2xl font-bold text-gray-800">Сессии оценки</h1>
|
||||
<a href="{{ route('admin.session.create') }}"
|
||||
class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg text-sm font-medium">
|
||||
+ Новая сессия
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@if ($sessions->isEmpty())
|
||||
<div class="text-center py-10 text-gray-500">Нет созданных сессий</div>
|
||||
@else
|
||||
<div class="space-y-4">
|
||||
@foreach($sessions as $s)
|
||||
<div class="bg-white p-5 rounded-xl shadow-sm border border-gray-100">
|
||||
<div class="flex justify-between items-start">
|
||||
<div>
|
||||
<h3 class="font-medium text-gray-800">Сессия #{{ $s->id }}</h3>
|
||||
<p class="text-sm text-gray-600">
|
||||
Оценки: 1–{{ $s->max_score }} | Участников: {{ $s->votes_count }} / {{ $s->max_voters }}
|
||||
</p>
|
||||
</div>
|
||||
<a href="/admin/sessions/{{ $s->id }}"
|
||||
class="text-blue-600 hover:underline text-sm font-medium">Просмотр</a>
|
||||
</div>
|
||||
<div class="mt-3 p-3 bg-gray-50 rounded text-sm font-mono break-all">
|
||||
<strong>Ссылка:</strong> <a href="/s/{{ $s->token }}" target="_blank"
|
||||
class="text-blue-600 hover:underline">{{ url('/s/' . $s->token) }}</a>
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
@endsection
|
||||
17
resources/views/layouts/app.blade.php
Normal file
17
resources/views/layouts/app.blade.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Poker Planning</title>
|
||||
@vite(['resources/css/app.css', 'resources/js/app.js'])
|
||||
</head>
|
||||
<body class="bg-gray-50 min-h-screen flex flex-col">
|
||||
<main class="container mx-auto px-4 py-8 max-w-2xl flex-grow">
|
||||
@yield('content')
|
||||
</main>
|
||||
<footer class="text-center text-gray-500 text-sm py-4 border-t mt-auto">
|
||||
Poker Planning © {{ date('Y') }}
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
14
resources/views/thanks.blade.php
Normal file
14
resources/views/thanks.blade.php
Normal file
@@ -0,0 +1,14 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="text-center">
|
||||
<div class="bg-green-100 text-green-800 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-4">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
</div>
|
||||
<h1 class="text-2xl font-bold text-gray-800 mb-2">Спасибо за оценку!</h1>
|
||||
<p class="text-gray-600 mb-6">Ваш голос учтён.</p>
|
||||
<a href="/" class="text-blue-600 hover:underline">← Вернуться на главную</a>
|
||||
</div>
|
||||
@endsection
|
||||
39
resources/views/vote.blade.php
Normal file
39
resources/views/vote.blade.php
Normal file
@@ -0,0 +1,39 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="bg-white p-6 rounded-xl shadow-md">
|
||||
<h1 class="text-2xl font-bold text-gray-800 mb-2">Оцените задачу</h1>
|
||||
<p class="text-gray-600 mb-6">Выберите оценку от 1 до {{ $round->max_score }}</p>
|
||||
|
||||
@if ($errors->any())
|
||||
<div class="mb-4 p-3 bg-red-50 text-red-700 rounded-lg text-sm">
|
||||
{{ $errors->first() }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form method="POST" class="space-y-4">
|
||||
@csrf
|
||||
<div>
|
||||
<label class="block text-gray-700 mb-1">Ваше имя</label>
|
||||
<input type="text" name="name" required
|
||||
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
placeholder="Иван Петров">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-gray-700 mb-1">Оценка</label>
|
||||
<select name="score" required
|
||||
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
|
||||
@for ($i = 1; $i <= $round->max_score; $i++)
|
||||
<option value="{{ $i }}">{{ $i }}</option>
|
||||
@endfor
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button type="submit"
|
||||
class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2.5 px-4 rounded-lg transition">
|
||||
Отправить оценку
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -1,7 +1,35 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\PokerController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
|
||||
// Публичные маршруты
|
||||
Route::get('/s/{token}', [PokerController::class, 'showForm'])->name('vote.form');
|
||||
Route::post('/s/{token}', [PokerController::class, 'submitVote']);
|
||||
Route::get('/thanks', [PokerController::class, 'thanks'])->name('vote.thanks');
|
||||
|
||||
// Админка с базовой аутентификацией
|
||||
Route::prefix('admin')->group(function () {
|
||||
Route::match(['get', 'post'], '/login', function () {
|
||||
if (isset($_SERVER['PHP_AUTH_USER'])) {
|
||||
if ($_SERVER['PHP_AUTH_USER'] === env('ADMIN_USER') &&
|
||||
$_SERVER['PHP_AUTH_PW'] === env('ADMIN_PASS')) {
|
||||
session(['admin_logged_in' => true]);
|
||||
return redirect('/admin/sessions');
|
||||
}
|
||||
}
|
||||
header('WWW-Authenticate: Basic realm="Admin Login"');
|
||||
abort(401);
|
||||
});
|
||||
|
||||
Route::middleware([\App\Http\Middleware\EnsureAdminAuthenticated::class])->group(function () {
|
||||
Route::get('/sessions/create', [PokerController::class, 'createEstimationRoundForm'])->name('admin.session.create');
|
||||
Route::post('/sessions', [PokerController::class, 'createEstimationRound'])->name('admin.sessions.store');
|
||||
Route::get('/sessions', [PokerController::class, 'listEstimationRounds'])->name('admin.sessions');
|
||||
Route::get('/sessions/{id}', [PokerController::class, 'showEstimationRound']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user