170 lines
6.5 KiB
PHP
170 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Services\C45Service;
|
|
use App\Services\PreprocessingService;
|
|
use App\Models\DataTesting;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class PrediksiController extends Controller
|
|
{
|
|
protected $c45Service;
|
|
protected $preprocessingService;
|
|
|
|
public function __construct(C45Service $c45Service, PreprocessingService $preprocessingService)
|
|
{
|
|
$this->c45Service = $c45Service;
|
|
$this->preprocessingService = $preprocessingService;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$previousInput = [
|
|
'nama' => $request->query('nama'),
|
|
'nilai' => $request->query('nilai'),
|
|
'mapel_favorit' => $request->query('mapel_favorit'),
|
|
'minat' => $request->query('minat'),
|
|
'cita_cita' => $request->query('cita_cita'),
|
|
'preferensi_studi' => $request->query('preferensi_studi'),
|
|
'siswa_id' => $request->query('siswa_id'),
|
|
];
|
|
|
|
return view('prediksi.index', compact('previousInput'));
|
|
}
|
|
|
|
public function hitungRekomendasi(Request $request)
|
|
{
|
|
// 1. VALIDASI INPUT FORM
|
|
$validatedData = $request->validate([
|
|
'nama' => 'nullable|string|max:255',
|
|
'nilai' => 'required|numeric|min:0|max:100',
|
|
'mapel_favorit' => 'required|string',
|
|
'minat' => 'required|string',
|
|
'cita_cita' => 'required|string',
|
|
'preferensi_studi' => 'required|string',
|
|
'siswa_id' => 'nullable|exists:users,id',
|
|
]);
|
|
|
|
// 2. TANGKAP INPUT MENTAH (RAW)
|
|
$inputRaw = [
|
|
'nilai' => $validatedData['nilai'],
|
|
'mapel_favorit' => $validatedData['mapel_favorit'],
|
|
'minat' => $validatedData['minat'],
|
|
'cita_cita' => $validatedData['cita_cita'],
|
|
'preferensi_studi' => $validatedData['preferensi_studi']
|
|
];
|
|
|
|
// 3. PROSES PREPROCESSING
|
|
$dataBersih = $this->preprocessingService->preprocess((object) $inputRaw);
|
|
|
|
// 4. JALANKAN PREDIKSI C4.5 LENGKAP
|
|
if (method_exists($this->c45Service, 'predictDetail')) {
|
|
$hasilPrediksi = $this->c45Service->predictDetail($inputRaw);
|
|
$hasilJurusan = $hasilPrediksi['rekomendasi_utama'] ?? 'Teknologi Informasi';
|
|
$rulePath = $hasilPrediksi['rule_path'] ?? [];
|
|
} else {
|
|
$hasilJurusan = 'Teknologi Informasi';
|
|
$rulePath = [
|
|
'Nilai = ' . ($dataBersih['nilai'] ?? $inputRaw['nilai']),
|
|
'Mapel Favorit = ' . $inputRaw['mapel_favorit'],
|
|
'Preferensi = ' . $inputRaw['preferensi_studi'],
|
|
'Minat = ' . $inputRaw['minat']
|
|
];
|
|
}
|
|
|
|
// Helper akses data bersih
|
|
$getVal = fn($key) => is_array($dataBersih) ? ($dataBersih[$key] ?? '') : ($dataBersih->{$key} ?? '');
|
|
|
|
// 5. PENENTUAN USER_ID DAN CREATED_BY
|
|
$currentUser = Auth::user();
|
|
$createdBy = $currentUser ? $currentUser->id : null;
|
|
$userId = ($currentUser && $currentUser->role === 'admin')
|
|
? $request->input('siswa_id', null)
|
|
: ($currentUser->id ?? null);
|
|
|
|
// 6. SIMPAN KE DATABASE
|
|
$dataTesting = DataTesting::create([
|
|
'user_id' => $userId,
|
|
'created_by' => $createdBy,
|
|
'nama' => $validatedData['nama'] ?? ($currentUser->name ?? 'Siswa/User'),
|
|
'nilai_raw' => $inputRaw['nilai'],
|
|
'mapel_favorit_raw' => $inputRaw['mapel_favorit'],
|
|
'minat_raw' => $inputRaw['minat'],
|
|
'cita_cita_raw' => $inputRaw['cita_cita'],
|
|
'preferensi_studi_raw' => $inputRaw['preferensi_studi'],
|
|
'nilai' => $getVal('nilai'),
|
|
'mapel_favorit' => $getVal('mapel_favorit'),
|
|
'minat' => $getVal('minat'),
|
|
'cita_cita' => $getVal('cita_cita'),
|
|
'preferensi_studi' => $getVal('preferensi_studi'),
|
|
'hasil_rekomendasi' => $hasilJurusan,
|
|
]);
|
|
|
|
// 7. DIRECT VIEW BERDASARKAN ROLE USER
|
|
$viewData = [
|
|
'dataTesting' => $dataTesting,
|
|
'hasilJurusan' => $hasilJurusan,
|
|
'rulePath' => $rulePath,
|
|
'dataBersih' => $dataBersih,
|
|
];
|
|
|
|
if ($currentUser && $currentUser->role === 'admin') {
|
|
$viewData['isAdmin'] = true;
|
|
}
|
|
|
|
return view('prediksi.hasil', $viewData);
|
|
}
|
|
|
|
public function riwayat()
|
|
{
|
|
$riwayats = DataTesting::where('user_id', Auth::id())
|
|
->latest()
|
|
->paginate(10);
|
|
|
|
return view('siswa.riwayat', compact('riwayats'));
|
|
}
|
|
|
|
public function showRiwayat($id)
|
|
{
|
|
$query = DataTesting::query();
|
|
if (Auth::user()->role !== 'admin') {
|
|
$query->where('user_id', Auth::id());
|
|
}
|
|
|
|
$dataTesting = $query->findOrFail($id);
|
|
|
|
$inputRaw = [
|
|
'nilai' => $dataTesting->nilai_raw ?? $dataTesting->nilai,
|
|
'mapel_favorit' => $dataTesting->mapel_favorit_raw ?? $dataTesting->mapel_favorit,
|
|
'minat' => $dataTesting->minat_raw ?? $dataTesting->minat,
|
|
'cita_cita' => $dataTesting->cita_cita_raw ?? $dataTesting->cita_cita,
|
|
'preferensi_studi' => $dataTesting->preferensi_studi_raw ?? $dataTesting->preferensi_studi,
|
|
];
|
|
|
|
$dataBersih = $this->preprocessingService->preprocess((object) $inputRaw);
|
|
|
|
if (method_exists($this->c45Service, 'predictDetail')) {
|
|
$hasilPrediksi = $this->c45Service->predictDetail($inputRaw);
|
|
$rulePath = $hasilPrediksi['rule_path'] ?? [];
|
|
} else {
|
|
$rulePath = [];
|
|
}
|
|
|
|
return view('prediksi.hasil', [
|
|
'dataTesting' => $dataTesting,
|
|
'hasilJurusan' => $dataTesting->hasil_rekomendasi,
|
|
'rulePath' => $rulePath,
|
|
'dataBersih' => $dataBersih,
|
|
]);
|
|
}
|
|
|
|
public function destroyRiwayat($id)
|
|
{
|
|
$data = DataTesting::where('user_id', Auth::id())->findOrFail($id);
|
|
$data->delete();
|
|
|
|
return redirect()->route('riwayat.index')->with('success', 'Catatan riwayat rekomendasi berhasil dihapus.');
|
|
}
|
|
} |