166 lines
5.4 KiB
PHP
166 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\StoreProgramRequest;
|
|
use App\Http\Requests\Admin\UpdateProgramRequest;
|
|
use App\Models\Program;
|
|
use App\Services\AuditLogService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ProgramController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$query = Program::with('creator')
|
|
->withCount(['attendances', 'programParticipants'])
|
|
->latest();
|
|
|
|
// Admin program hanya nampak program sendiri
|
|
if (auth()->user()->isAdminProgram()) {
|
|
$query->where('created_by', auth()->id());
|
|
}
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where(function ($q) use ($request) {
|
|
$q->where('title', 'like', '%' . $request->search . '%')
|
|
->orWhere('organizer', 'like', '%' . $request->search . '%')
|
|
->orWhere('location', 'like', '%' . $request->search . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->status);
|
|
}
|
|
|
|
$programs = $query->paginate(15)->withQueryString();
|
|
|
|
return view('admin.programs.index', compact('programs'));
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('admin.programs.create');
|
|
}
|
|
|
|
public function store(StoreProgramRequest $request): RedirectResponse
|
|
{
|
|
$program = Program::create([
|
|
...$request->validated(),
|
|
'created_by' => auth()->id(),
|
|
]);
|
|
|
|
AuditLogService::log('program.created', $program);
|
|
|
|
return redirect()
|
|
->route('admin.programs.show', $program)
|
|
->with('success', 'Program "' . $program->title . '" berjaya ditambah.');
|
|
}
|
|
|
|
public function show(Program $program): View
|
|
{
|
|
$this->authorize('view', $program);
|
|
|
|
$program->load([
|
|
'qrCode',
|
|
'certificateTemplate',
|
|
'questionnaire.questionnaireSet.questions',
|
|
]);
|
|
|
|
// Consolidate into 2 queries instead of 6 separate COUNTs
|
|
$ppStats = \DB::table('program_participants')
|
|
->where('program_id', $program->id)
|
|
->selectRaw("COUNT(*) as total, SUM(is_pre_registered) as pre_registered, SUM(registration_source = 'walk_in') as walk_in")
|
|
->first();
|
|
|
|
$certStats = \DB::table('certificates')
|
|
->where('program_id', $program->id)
|
|
->selectRaw("COUNT(*) as total, SUM(status IN ('generated','emailed','downloaded')) as cert_generated")
|
|
->first();
|
|
|
|
$stats = [
|
|
'total_participants' => (int) ($ppStats->total ?? 0),
|
|
'pre_registered' => (int) ($ppStats->pre_registered ?? 0),
|
|
'walk_in' => (int) ($ppStats->walk_in ?? 0),
|
|
'total_attendances' => $program->attendances()->count(),
|
|
'total_certificates' => (int) ($certStats->total ?? 0),
|
|
'generated_certificates' => (int) ($certStats->cert_generated ?? 0),
|
|
];
|
|
|
|
return view('admin.programs.show', compact('program', 'stats'));
|
|
}
|
|
|
|
public function edit(Program $program): View
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
return view('admin.programs.edit', compact('program'));
|
|
}
|
|
|
|
public function update(UpdateProgramRequest $request, Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
$old = $program->only([
|
|
'title', 'status', 'checkin_start_at', 'checkin_end_at',
|
|
'ecert_download_start_at', 'ecert_download_end_at',
|
|
]);
|
|
|
|
$program->update($request->validated());
|
|
|
|
AuditLogService::log('program.updated', $program, $old);
|
|
|
|
return redirect()
|
|
->route('admin.programs.show', $program)
|
|
->with('success', 'Maklumat program berjaya dikemas kini.');
|
|
}
|
|
|
|
public function destroy(Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $program);
|
|
|
|
if ($program->attendances()->exists()) {
|
|
return back()->with('error', 'Program tidak boleh dipadam kerana sudah ada rekod kehadiran.');
|
|
}
|
|
|
|
$title = $program->title;
|
|
AuditLogService::log('program.deleted', $program);
|
|
$program->delete();
|
|
|
|
return redirect()
|
|
->route('admin.programs.index')
|
|
->with('success', 'Program "' . $title . '" berjaya dipadam.');
|
|
}
|
|
|
|
public function publish(Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
if ($program->status !== 'draft') {
|
|
return back()->with('error', 'Hanya program berstatus Draf boleh diterbitkan.');
|
|
}
|
|
|
|
$program->update(['status' => 'published']);
|
|
AuditLogService::log('program.published', $program);
|
|
|
|
return back()->with('success', 'Program berjaya diterbitkan.');
|
|
}
|
|
|
|
public function close(Program $program): RedirectResponse
|
|
{
|
|
$this->authorize('update', $program);
|
|
|
|
if ($program->status !== 'published') {
|
|
return back()->with('error', 'Hanya program berstatus Diterbitkan boleh ditutup.');
|
|
}
|
|
|
|
$program->update(['status' => 'closed']);
|
|
AuditLogService::log('program.closed', $program);
|
|
|
|
return back()->with('success', 'Program berjaya ditutup.');
|
|
}
|
|
}
|