74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Classification;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
public function dashboard()
|
|
{
|
|
$totalUsers = User::where('role', 'user')->count();
|
|
$totalClassifications = Classification::count();
|
|
|
|
$statistikKelas = Classification::select('label', DB::raw('count(*) as total'))
|
|
->groupBy('label')
|
|
->get();
|
|
|
|
return view('admin.dashboard', compact('totalUsers', 'totalClassifications', 'statistikKelas'));
|
|
}
|
|
|
|
public function users()
|
|
{
|
|
$users = User::where('role', 'user')->latest()->get();
|
|
return view('admin.users', compact('users'));
|
|
}
|
|
|
|
public function destroyUser($id)
|
|
{
|
|
$user = User::findOrFail($id);
|
|
|
|
if ($user->role === 'admin') {
|
|
return back()->withErrors(['error' => 'Akun admin tidak dapat dihapus.']);
|
|
}
|
|
|
|
$user->delete();
|
|
|
|
return back()->with('success', 'Pengguna berhasil dihapus.');
|
|
}
|
|
|
|
public function exportCsv()
|
|
{
|
|
$classifications = Classification::all();
|
|
|
|
$filename = "laporan_klasifikasi_kopi.csv";
|
|
$handle = fopen('php://memory', 'w');
|
|
|
|
// Membuat Baris Header CSV
|
|
fputcsv($handle, ['ID', 'User ID', 'Hasil Klasifikasi', 'Akurasi (%)', 'Tanggal Pemindaian']);
|
|
|
|
// Mengisi Baris Data
|
|
foreach ($classifications as $row) {
|
|
fputcsv($handle, [
|
|
$row->id,
|
|
$row->user_id,
|
|
$row->label,
|
|
$row->confidence,
|
|
$row->created_at
|
|
]);
|
|
}
|
|
|
|
fseek($handle, 0);
|
|
$headers = [
|
|
'Content-Type' => 'text/csv',
|
|
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
|
|
];
|
|
|
|
return response()->stream(function () use ($handle) {
|
|
fpassthru($handle);
|
|
}, 200, $headers);
|
|
}
|
|
} |