168 lines
7.7 KiB
PHP
168 lines
7.7 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Operator;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Controllers\Operator\Concerns\ServesLegacyList;
|
||
use App\Support\Legacy\AcademicOverview;
|
||
use App\Support\Legacy\Curriculum;
|
||
use App\Support\Legacy\PortalException;
|
||
use App\Support\Legacy\StudentBiodata;
|
||
use App\Support\Legacy\StudentDirectory;
|
||
use App\Support\Legacy\StudentRecords;
|
||
use App\Support\Prototype\Satu\Catalog;
|
||
use Illuminate\Contracts\View\View;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
|
||
/**
|
||
* Detail mahasiswa (aksi Lihat pada Portal > Mahasiswa) untuk akun DB lama: ringkasan akademik, biodata lengkap per
|
||
* bagian, dan data akademik, semuanya diubah per bagian lewat JSON. Satu-satunya tempat mengubah data mahasiswa;
|
||
* daftar hanya untuk melihat. Hanya mahasiswa prodi yang dipegang akun.
|
||
*/
|
||
class StudentDetailController extends Controller
|
||
{
|
||
use ServesLegacyList;
|
||
|
||
private const string SLUG = 'list_mahasiswa';
|
||
|
||
/** Kunci bagian data akademik (ditulis StudentRecords, bukan StudentBiodata). */
|
||
private const string ACADEMIC = 'akademik';
|
||
|
||
public function __construct(
|
||
private readonly AcademicOverview $overview,
|
||
private readonly StudentDirectory $directory,
|
||
private readonly StudentBiodata $biodata,
|
||
private readonly StudentRecords $records,
|
||
private readonly Curriculum $curriculum,
|
||
) {}
|
||
|
||
public function show(Request $request, int $id): View
|
||
{
|
||
$accountId = (int) $request->user()->getKey();
|
||
$prodi = $this->prodiOrFail($request);
|
||
$student = $this->directory->find($prodi, $id) ?? abort(404);
|
||
$bio = $this->biodata->find($prodi, $id) ?? abort(404);
|
||
$page = Catalog::page(self::SLUG);
|
||
|
||
// Data akademik tampil tepat sesudah data pribadi.
|
||
$sections = array_slice($bio['sections'], 0, 1, true)
|
||
+ [self::ACADEMIC => $this->academic($accountId, $student, $bio['extra'])]
|
||
+ array_slice($bio['sections'], 1, null, true);
|
||
|
||
return view('operator.satu.mahasiswa-detail', [
|
||
'page' => $page,
|
||
'crumbs' => ['Beranda' => route(auth()->user()->role->dashboardRoute())] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => route('operator.halaman', self::SLUG), 'Detail' => null],
|
||
'student' => $student,
|
||
'sections' => $sections,
|
||
'back' => route('operator.halaman', self::SLUG),
|
||
'saveUrl' => route('operator.list-mahasiswa.biodata', [$id, '__SECTION__']),
|
||
]);
|
||
}
|
||
|
||
public function update(Request $request, int $id, string $section): JsonResponse
|
||
{
|
||
$prodi = $this->prodiOrFail($request);
|
||
abort_unless(in_array($section, [...StudentBiodata::sectionKeys(), self::ACADEMIC], true) && $this->biodata->exists($prodi, $id), 404);
|
||
|
||
try {
|
||
if ($section === self::ACADEMIC) {
|
||
return $this->updateAcademic($request, $prodi, $id);
|
||
}
|
||
|
||
$input = $this->biodata->validated($prodi, $id, $section, $request->all());
|
||
$result = $this->biodata->update($prodi, $id, $section, $input, (string) $request->user()->username, $request->ip());
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json([
|
||
'message' => $result['changed'] === 0 ? 'Tidak ada perubahan.' : 'Biodata diperbarui.',
|
||
'values' => $result['section']['values'],
|
||
'display' => $result['section']['display'],
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* Ubah data akademik. Ringkasan di bagian atas halaman (prodi, status, semester) ikut berubah, jadi klien memuat
|
||
* ulang halaman; pesan berhasil dititipkan lewat sesi agar tampil sebagai toast di halaman baru.
|
||
*
|
||
* @param list<int> $prodi
|
||
*/
|
||
private function updateAcademic(Request $request, array $prodi, int $id): JsonResponse
|
||
{
|
||
$input = $request->validate([
|
||
'prodi' => ['required', 'integer'],
|
||
'kurikulum' => ['required', 'integer'],
|
||
'angkatan' => ['required', 'integer'],
|
||
'program' => ['required', 'integer'],
|
||
'jalur' => ['nullable', 'integer'],
|
||
'status' => ['required', 'integer', 'in:'.implode(',', array_keys(StudentRecords::STATUS))],
|
||
], [], ['prodi' => 'program studi', 'kurikulum' => 'kurikulum', 'angkatan' => 'angkatan', 'program' => 'program kuliah', 'jalur' => 'jalur masuk', 'status' => 'status']);
|
||
|
||
$this->records->update($prodi, $id, $input, (string) $request->user()->username, $request->ip());
|
||
$request->session()->flash('status', 'Data akademik diperbarui.');
|
||
|
||
return response()->json(['message' => 'Data akademik diperbarui.', 'reload' => true]);
|
||
}
|
||
|
||
/**
|
||
* Bagian data akademik dalam bentuk yang sama dengan bagian biodata: isian yang bisa diubah plus keterangan yang
|
||
* hanya dibaca (NIM, jenjang, dosen PA, tanggal lulus).
|
||
*
|
||
* @param array<string, mixed> $student baris StudentDirectory::find()
|
||
* @param array<string, mixed> $extra StudentBiodata::find()['extra']
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function academic(int $accountId, array $student, array $extra): array
|
||
{
|
||
$programs = $this->overview->programs($accountId)->all();
|
||
$options = $this->records->options();
|
||
$form = $student['form'];
|
||
|
||
$fields = [
|
||
['name' => 'prodi', 'label' => 'Program studi', 'type' => 'select', 'options' => $programs, 'required' => true],
|
||
['name' => 'kurikulum', 'label' => 'Kurikulum', 'type' => 'select', 'options' => $this->curriculum->curricula(array_keys($programs))->all(), 'required' => true],
|
||
['name' => 'angkatan', 'label' => 'Angkatan', 'type' => 'select', 'options' => $options['angkatan'], 'required' => true],
|
||
['name' => 'program', 'label' => 'Program kuliah', 'type' => 'select', 'options' => $options['program'], 'required' => true],
|
||
['name' => 'jalur', 'label' => 'Jalur masuk', 'type' => 'select', 'options' => $options['jalur']],
|
||
['name' => 'status', 'label' => 'Status', 'type' => 'select', 'options' => StudentRecords::STATUS, 'required' => true],
|
||
];
|
||
|
||
$values = [];
|
||
$display = ['nim' => $student['nim'], 'jenjang' => $student['jenjang'] ?? '–', 'pa' => $extra['pa'] ?? '–', 'lulus' => $extra['lulus'] ?? '–'];
|
||
foreach ($fields as &$field) {
|
||
$name = $field['name'];
|
||
$value = $form[$name];
|
||
// Kode yang tidak ada di tabel pilihan tetap tampil agar tidak hilang saat disimpan.
|
||
if ($value !== '' && ! isset($field['options'][$value])) {
|
||
$field['options'][$value] = 'Kode '.$value;
|
||
}
|
||
$values[$name] = $value;
|
||
$display[$name] = $value === '' ? '–' : $field['options'][$value];
|
||
}
|
||
unset($field);
|
||
|
||
return [
|
||
'key' => self::ACADEMIC,
|
||
'title' => 'Data akademik',
|
||
'icon' => 'identification',
|
||
'description' => 'NIM tidak dapat diubah dan status Lulus ditetapkan lewat yudisium.',
|
||
'fields' => $fields,
|
||
'values' => $values,
|
||
'display' => $display,
|
||
'readonly' => [['name' => 'nim', 'label' => 'NIM'], ['name' => 'jenjang', 'label' => 'Jenjang'], ['name' => 'pa', 'label' => 'Dosen pembimbing akademik'], ['name' => 'lulus', 'label' => 'Tanggal lulus']],
|
||
];
|
||
}
|
||
|
||
/** @return list<int> */
|
||
private function prodiOrFail(Request $request): array
|
||
{
|
||
$this->legacyOnly($request);
|
||
$prodi = $this->overview->programs((int) $request->user()->getKey())->keys()->all();
|
||
abort_if($prodi === [], 404);
|
||
|
||
return $prodi;
|
||
}
|
||
}
|