country + status added and other changes
This commit is contained in:
@@ -19,7 +19,8 @@ class CommentController extends Controller
|
||||
'comment' => $c->comment,
|
||||
'author' => auth()->user()->name,
|
||||
'created_at_human' => $c->created_at->diffForHumans(),
|
||||
]);
|
||||
])
|
||||
->values(); // ← add this
|
||||
|
||||
return response()->json(['comments' => $comments]);
|
||||
}
|
||||
|
||||
@@ -2,26 +2,28 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Country;
|
||||
use App\Models\Registration;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
return match ($user->role) {
|
||||
'admin' => $this->adminDashboard(),
|
||||
'counselor' => $this->counselorDashboard(),
|
||||
'admin' => $this->adminDashboard($request),
|
||||
'counselor' => $this->counselorDashboard($request),
|
||||
default => abort(403, 'Unauthorized action.'),
|
||||
};
|
||||
}
|
||||
|
||||
public function adminDashboard()
|
||||
public function adminDashboard(Request $request)
|
||||
{
|
||||
$today = Carbon::today();
|
||||
$today = Carbon::today();
|
||||
$search = trim($request->input('search', ''));
|
||||
|
||||
$query = Registration::with([
|
||||
'sessions' => function ($q) use ($today) {
|
||||
@@ -30,16 +32,21 @@ class HomeController extends Controller
|
||||
])
|
||||
->whereHas('sessions', function ($q) use ($today) {
|
||||
$q->whereDate('play_date', $today);
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(10);
|
||||
});
|
||||
|
||||
if ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('phone', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$query = $query->orderBy('created_at', 'desc')->paginate(10)->withQueryString();
|
||||
|
||||
$registrations = $query->getCollection()->map(function ($student) {
|
||||
|
||||
$todaySession = $student->sessions->first();
|
||||
|
||||
$todayGoals = null;
|
||||
|
||||
$todayGoals = null;
|
||||
if ($todaySession) {
|
||||
$todayGoals = $todaySession->shots
|
||||
->sortBy('shot_number')
|
||||
@@ -47,7 +54,6 @@ class HomeController extends Controller
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $student->id,
|
||||
'name' => $student->name,
|
||||
@@ -56,41 +62,51 @@ class HomeController extends Controller
|
||||
'today_goals' => $todayGoals,
|
||||
'total_score' => $student->total_score,
|
||||
'session_id' => $todaySession?->id,
|
||||
'country_id' => $student->country_id,
|
||||
];
|
||||
});
|
||||
|
||||
$query->setCollection($registrations);
|
||||
|
||||
$data['registrations'] = $query;
|
||||
$data['search'] = $search;
|
||||
$data['total'] = Registration::count();
|
||||
$data['played'] = $registrations->filter(fn($r) => !empty($r['today_goals']) && count($r['today_goals']) >= 3)->count();
|
||||
$data['topScore'] = $registrations->max('total_score') ?? 0;
|
||||
$data['countries'] = Country::where('status', 1)->orderBy('title')->get(['id','title','country_flag']);
|
||||
|
||||
return view('dashboard.admin', $data);
|
||||
}
|
||||
|
||||
public function counselorDashboard()
|
||||
public function counselorDashboard(Request $request)
|
||||
{
|
||||
$today = Carbon::today();
|
||||
$today = Carbon::today();
|
||||
$search = trim($request->input('search', ''));
|
||||
|
||||
$query = Registration::with([
|
||||
'sessions' => function ($q) use ($today) {
|
||||
$q->whereDate('play_date', $today)->with('shots');
|
||||
},
|
||||
'comments'
|
||||
'comments',
|
||||
'country'
|
||||
])
|
||||
->whereHas('sessions', function ($q) use ($today) {
|
||||
$q->whereDate('play_date', $today);
|
||||
})
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(10);
|
||||
});
|
||||
|
||||
if ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('phone', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$query = $query->orderBy('created_at', 'desc')->paginate(10)->withQueryString();
|
||||
|
||||
$registrations = $query->getCollection()->map(function ($student) {
|
||||
|
||||
$todaySession = $student->sessions->first();
|
||||
|
||||
$todayGoals = null;
|
||||
|
||||
$todayGoals = null;
|
||||
if ($todaySession) {
|
||||
$todayGoals = $todaySession->shots
|
||||
->sortBy('shot_number')
|
||||
@@ -98,7 +114,6 @@ class HomeController extends Controller
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $student->id,
|
||||
'name' => $student->name,
|
||||
@@ -107,6 +122,10 @@ class HomeController extends Controller
|
||||
'today_goals' => $todayGoals,
|
||||
'total_score' => $student->total_score,
|
||||
'session_id' => $todaySession?->id,
|
||||
'country_id' => $student->country_id,
|
||||
'country_title' => $student->country?->title ?? '', // add
|
||||
'country_flag' => $student->country?->country_flag ?? '', // add
|
||||
'status' => $student->status ?? 'warm', // add if not there
|
||||
'comment_count' => $student->comments->count(),
|
||||
];
|
||||
});
|
||||
@@ -114,9 +133,11 @@ class HomeController extends Controller
|
||||
$query->setCollection($registrations);
|
||||
|
||||
$data['registrations'] = $query;
|
||||
$data['search'] = $search;
|
||||
$data['total'] = Registration::count();
|
||||
$data['played'] = $registrations->filter(fn($r) => !empty($r['today_goals']) && count($r['today_goals']) >= 3)->count();
|
||||
$data['topScore'] = $registrations->max('total_score') ?? 0;
|
||||
$data['countries'] = Country::where('status', 1)->orderBy('title')->get(['id','title','country_flag']);
|
||||
|
||||
return view('dashboard.counselor', $data);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Country;
|
||||
use App\Models\GameSession;
|
||||
use App\Models\GameShot;
|
||||
use App\Models\Registration;
|
||||
@@ -10,13 +11,28 @@ use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class RegistrationController extends Controller
|
||||
{
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
$registrations = Registration::orderBy('id', 'desc')->paginate(20);
|
||||
$search = trim($request->input('search', ''));
|
||||
|
||||
return view('registrations', compact('registrations'));
|
||||
$query = Registration::with(['sessions.shots', 'comments']);
|
||||
|
||||
if ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%")
|
||||
->orWhere('phone', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$registrations = $query->orderBy('id', 'desc')->paginate(20)->withQueryString();
|
||||
|
||||
$data['registrations'] = $registrations;
|
||||
$data['search'] = $search;
|
||||
$data['countries'] = Country::where('status', 1)->orderBy('title')->get(['id','title','country_flag']);
|
||||
|
||||
return view('registrations', $data);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('registrations.create');
|
||||
@@ -311,5 +327,51 @@ class RegistrationController extends Controller
|
||||
|
||||
return view('leaderboard', $data);
|
||||
}
|
||||
|
||||
public function history($id)
|
||||
{
|
||||
$reg = Registration::with(['sessions.shots'])->findOrFail($id);
|
||||
|
||||
$sessions = $reg->sessions
|
||||
->sortByDesc('play_date')
|
||||
->map(fn($s) => [
|
||||
'date' => $s->play_date,
|
||||
'score' => $s->score,
|
||||
'shots' => $s->shots->sortBy('shot_number')->map(fn($sh) => (bool)$sh->result)->values(),
|
||||
])->values();
|
||||
|
||||
return response()->json(['sessions' => $sessions]);
|
||||
}
|
||||
|
||||
public function updateCountry(Request $request, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'country_id' => 'required|exists:countries,id',
|
||||
]);
|
||||
|
||||
$registration = Registration::findOrFail($id);
|
||||
$registration->update(['country_id' => $request->country_id]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'ok',
|
||||
'country_id' => $registration->country_id,
|
||||
'country_name' => $registration->country->title,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateStatus(Request $request, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'status' => 'required|in:hot,warm,cold',
|
||||
]);
|
||||
|
||||
$registration = Registration::findOrFail($id);
|
||||
$registration->update(['status' => $request->status]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'ok',
|
||||
'value' => $registration->status,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Registration;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ScoreboardController extends Controller
|
||||
{
|
||||
@@ -15,34 +16,41 @@ class ScoreboardController extends Controller
|
||||
public function select(Request $request)
|
||||
{
|
||||
$request->validate(['registration_id' => 'required|exists:registrations,id']);
|
||||
session(['scoreboard_player_id' => $request->registration_id]);
|
||||
Cache::put('scoreboard_selected_player', $request->registration_id, now()->endOfDay());
|
||||
return response()->json(['status' => 'ok']);
|
||||
}
|
||||
|
||||
public function state()
|
||||
{
|
||||
$id = session('scoreboard_player_id');
|
||||
$id = Cache::get('scoreboard_selected_player');
|
||||
|
||||
if (!$id) {
|
||||
return response()->json(['player' => null, 'leaderboard' => $this->leaderboard()]);
|
||||
}
|
||||
|
||||
$reg = Registration::with([
|
||||
'sessions' => fn($q) => $q->whereDate('play_date', today())->with('shots')
|
||||
'sessions' => fn($q) => $q->orderByDesc('play_date')->with('shots')
|
||||
])->findOrFail($id);
|
||||
|
||||
$todaySession = $reg->sessions->first();
|
||||
$todaySession = $reg->sessions->firstWhere('play_date', today()->toDateString());
|
||||
$shots = $todaySession
|
||||
? $todaySession->shots->sortBy('shot_number')->map(fn($s) => (bool)$s->result)->values()->toArray()
|
||||
: [];
|
||||
|
||||
$history = $reg->sessions->map(fn($s) => [
|
||||
'date' => $s->play_date,
|
||||
'score' => $s->score,
|
||||
'shots' => $s->shots->sortBy('shot_number')->map(fn($sh) => (bool)$sh->result)->values()->toArray(),
|
||||
])->toArray();
|
||||
|
||||
return response()->json([
|
||||
'player' => [
|
||||
'id' => $reg->id,
|
||||
'name' => $reg->name,
|
||||
'total_score' => $reg->total_score,
|
||||
'shots' => $shots,
|
||||
'id' => $reg->id,
|
||||
'name' => $reg->name,
|
||||
'total_score' => $reg->total_score,
|
||||
'shots' => $shots,
|
||||
'session_score' => $todaySession?->calculateScore() ?? 0,
|
||||
'history' => $history,
|
||||
],
|
||||
'leaderboard' => $this->leaderboard(),
|
||||
]);
|
||||
@@ -55,9 +63,10 @@ class ScoreboardController extends Controller
|
||||
->get(['id', 'name', 'total_score'])
|
||||
->map(fn($r, $i) => [
|
||||
'rank' => $i + 1,
|
||||
'id' => $r->id,
|
||||
'name' => $r->name,
|
||||
'total_score' => $r->total_score,
|
||||
])
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Country extends Model
|
||||
{
|
||||
protected $fillable = ['title', 'country_flag', 'status'];
|
||||
|
||||
public function registrations()
|
||||
{
|
||||
return $this->hasMany(Registration::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ class Registration extends Model
|
||||
'name',
|
||||
'email',
|
||||
'phone',
|
||||
'country_id',
|
||||
'status',
|
||||
'total_score',
|
||||
];
|
||||
|
||||
@@ -31,6 +33,11 @@ class Registration extends Model
|
||||
return $this->hasMany(Comment::class);
|
||||
}
|
||||
|
||||
public function country()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Country::class);
|
||||
}
|
||||
|
||||
/*
|
||||
|---------------------------
|
||||
| Helper methods
|
||||
|
||||
Reference in New Issue
Block a user