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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user