171 lines
6.6 KiB
PHP
171 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Services\C45Service;
|
|
use App\Services\PreprocessingService;
|
|
use App\Models\DataTesting;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class C45Controller extends Controller
|
|
{
|
|
protected $c45Service;
|
|
protected $preprocessingService;
|
|
|
|
public function __construct(C45Service $c45Service, PreprocessingService $preprocessingService)
|
|
{
|
|
$this->c45Service = $c45Service;
|
|
$this->preprocessingService = $preprocessingService;
|
|
}
|
|
|
|
/**
|
|
* 1. Menampilkan Halaman Dashboard & Perhitungan Algoritma C4.5
|
|
*/
|
|
public function index()
|
|
{
|
|
$rootNode = $this->c45Service->getRootNode();
|
|
$totalData = $this->c45Service->getTotalData();
|
|
$entropyTotal = $this->c45Service->entropyTotal();
|
|
$jumlahJurusan = $this->c45Service->getJumlahJurusan();
|
|
|
|
$gainNilai = $this->c45Service->hitungGain('nilai');
|
|
$gainMapelFavorit = $this->c45Service->hitungGain('mapel_favorit');
|
|
$gainMinat = $this->c45Service->hitungGain('minat');
|
|
$gainCita = $this->c45Service->hitungGain('cita_cita');
|
|
$gainPreferensi = $this->c45Service->hitungGain('preferensi_studi');
|
|
|
|
$splitNilai = $this->c45Service->hitungSplitInfo('nilai');
|
|
$splitMapelFavorit = $this->c45Service->hitungSplitInfo('mapel_favorit');
|
|
$splitMinat = $this->c45Service->hitungSplitInfo('minat');
|
|
$splitCita = $this->c45Service->hitungSplitInfo('cita_cita');
|
|
$splitPreferensi = $this->c45Service->hitungSplitInfo('preferensi_studi');
|
|
|
|
$entropyRootAttribute = $this->c45Service->hitungEntropyAtribut($rootNode['root'] ?? 'nilai');
|
|
|
|
$tree = $this->c45Service->buildTree();
|
|
$treeText = $this->c45Service->tampilTree($tree);
|
|
$detailSubsets = $this->c45Service->getDetailSubsets();
|
|
$rules = $this->c45Service->generateRules($tree);
|
|
|
|
return view('c45.index', compact(
|
|
'totalData',
|
|
'entropyTotal',
|
|
'rootNode',
|
|
'jumlahJurusan',
|
|
'gainNilai',
|
|
'gainMapelFavorit',
|
|
'gainMinat',
|
|
'gainCita',
|
|
'gainPreferensi',
|
|
'splitNilai',
|
|
'splitMapelFavorit',
|
|
'splitMinat',
|
|
'splitCita',
|
|
'splitPreferensi',
|
|
'entropyRootAttribute',
|
|
'tree',
|
|
'treeText',
|
|
'detailSubsets',
|
|
'rules'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* 2. Menampilkan Form Input Cek Rekomendasi Admin
|
|
*/
|
|
public function create()
|
|
{
|
|
// PERBAIKAN: Ambil daftar siswa dari database yang memiliki role 'siswa'
|
|
$daftar_siswa = User::where('role', 'siswa')->orderBy('name', 'asc')->get();
|
|
|
|
return view('c45.create', compact('daftar_siswa'));
|
|
}
|
|
|
|
/**
|
|
* 3. Menampilkan Halaman Hasil Rekomendasi
|
|
*/
|
|
public function hasil()
|
|
{
|
|
return view('c45.hasil');
|
|
}
|
|
|
|
/**
|
|
* 4. Memproses Data Input Kriteria Siswa
|
|
*/
|
|
public function hitungRekomendasi(Request $request)
|
|
{
|
|
$validatedData = $request->validate([
|
|
'nama' => 'nullable|string|max:255',
|
|
'nilai' => 'required|numeric|min:0|max:100',
|
|
'mapel_favorit' => 'required|string',
|
|
'preferensi_studi' => 'required|string',
|
|
'cita_cita' => 'required|string',
|
|
'minat' => 'required|string',
|
|
'siswa_id' => 'nullable|exists:users,id',
|
|
]);
|
|
|
|
$inputRaw = [
|
|
'nilai' => $validatedData['nilai'],
|
|
'mapel_favorit' => $validatedData['mapel_favorit'],
|
|
'minat' => $validatedData['minat'],
|
|
'cita_cita' => $validatedData['cita_cita'],
|
|
'preferensi_studi' => $validatedData['preferensi_studi']
|
|
];
|
|
|
|
// 1. Preprocessing Input
|
|
$dataBersih = $this->preprocessingService->preprocess((object) $inputRaw);
|
|
|
|
// 2. Prediksi Algoritma C4.5
|
|
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']
|
|
];
|
|
}
|
|
|
|
$getVal = fn($key) => is_array($dataBersih) ? ($dataBersih[$key] ?? '') : ($dataBersih->{$key} ?? '');
|
|
|
|
// 3. 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);
|
|
|
|
// 4. Simpan ke Database
|
|
$dataTesting = DataTesting::create([
|
|
'user_id' => $userId,
|
|
'created_by' => $createdBy,
|
|
'nama' => $request->input('nama', $currentUser->name ?? 'Calon Mahasiswa'),
|
|
'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,
|
|
]);
|
|
|
|
// 5. Redirect ke Halaman Hasil Rekomendasi
|
|
return redirect()->route('c45.hasil')->with([
|
|
'hasil_prediksi' => $dataTesting,
|
|
'hasilJurusan' => $hasilJurusan,
|
|
'rulePath' => $rulePath,
|
|
'dataBersih' => $dataBersih,
|
|
'success' => 'Analisis rekomendasi C4.5 berhasil diproses!'
|
|
]);
|
|
}
|
|
} |