247 lines
12 KiB
PHP
247 lines
12 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Operator;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Controllers\Operator\Concerns\ServesLegacyList;
|
||
use App\Models\Legacy\Account;
|
||
use App\Support\Legacy\AcademicOverview;
|
||
use App\Support\Legacy\Curriculum;
|
||
use App\Support\Legacy\PortalException;
|
||
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;
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use Illuminate\Support\Collection;
|
||
|
||
/**
|
||
* Portal > Mahasiswa (slug list_mahasiswa) untuk akun DB lama: daftar mahasiswa asli prodi yang dipegang akun,
|
||
* dengan format tabel SATU yang sama. Halaman dipanggil oleh SatuPageController (rute dan menu tetap `operator.halaman`);
|
||
* filter, pencarian, dan halaman berikutnya diambil lewat data() (JSON) sehingga alamat halaman tidak berubah.
|
||
*/
|
||
class StudentListController extends Controller
|
||
{
|
||
use ServesLegacyList;
|
||
|
||
private const string SLUG = 'list_mahasiswa';
|
||
|
||
public function __construct(
|
||
private readonly AcademicOverview $overview,
|
||
private readonly StudentDirectory $directory,
|
||
private readonly StudentRecords $records,
|
||
private readonly Curriculum $curriculum,
|
||
) {}
|
||
|
||
public function __invoke(Request $request): View
|
||
{
|
||
$page = Catalog::page(self::SLUG);
|
||
$programs = $this->overview->programs((int) $request->user()->getKey());
|
||
$options = $programs->isEmpty() ? null : $this->directory->options($programs->keys()->all());
|
||
[$labelProdi, $labelAngkatan, $labelStatus, $labelProgram, $labelJalur] = $page['filters'];
|
||
|
||
return view('operator.satu.remote-list', [
|
||
'page' => $page,
|
||
'crumbs' => ['Beranda' => route('operator.dashboard')] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => null],
|
||
'note' => 'Data mahasiswa asli dari SIAKAD untuk program studi yang Anda kelola. Tambah, ubah, dan hapus langsung tersimpan di basis data; mahasiswa yang sudah punya data akademik, keuangan, atau akun login tidak dapat dihapus, dan NIM tidak dapat diubah.',
|
||
'empty' => ['icon' => 'building-library', 'title' => 'Akun belum terhubung ke program studi', 'description' => 'Daftar mahasiswa tampil setelah akun Anda dipetakan ke program studi oleh Pusat Komputer.'],
|
||
'url' => route('operator.mahasiswa.data'),
|
||
'filters' => [
|
||
['name' => 'prodi', 'label' => $labelProdi, 'options' => $programs->all()],
|
||
['name' => 'angkatan', 'label' => $labelAngkatan, 'options' => $options['angkatan'] ?? []],
|
||
['name' => 'status', 'label' => $labelStatus, 'options' => $this->directory->statusOptions()->all()],
|
||
['name' => 'program', 'label' => $labelProgram, 'options' => $options['program'] ?? []],
|
||
['name' => 'jalur', 'label' => $labelJalur, 'options' => $options['jalur'] ?? []],
|
||
],
|
||
'columns' => [
|
||
['key' => 'nim', 'label' => 'NIM'],
|
||
['key' => 'nama', 'label' => 'Nama'],
|
||
['key' => 'jenjang', 'label' => 'Jenjang'],
|
||
['key' => 'prodi', 'label' => 'Program Studi'],
|
||
['key' => 'masuk', 'label' => 'Masuk'],
|
||
['key' => 'status', 'label' => 'Status', 'badge' => true],
|
||
['key' => 'smt', 'label' => 'Smt', 'num' => true],
|
||
['key' => 'sks', 'label' => 'SKS', 'num' => true],
|
||
['key' => 'ipk', 'label' => 'IPK', 'num' => true],
|
||
],
|
||
'perPage' => StudentDirectory::PER_PAGE,
|
||
'data' => $options === null ? null : $this->payload($this->search($request, $programs, $options)),
|
||
'form' => $options === null ? null : $this->form($programs->all()),
|
||
'destroyUrl' => $options === null ? null : route('operator.list-mahasiswa.destroy'),
|
||
'destroy' => ['title' => 'Hapus mahasiswa?', 'message' => 'Hanya mahasiswa tanpa data akademik, keuangan, dan akun login yang dapat dihapus.'],
|
||
]);
|
||
}
|
||
|
||
public function store(Request $request): JsonResponse
|
||
{
|
||
$prodi = $this->prodiOrFail($request);
|
||
$input = $this->validated($request);
|
||
|
||
try {
|
||
$id = $this->records->create($prodi, $input, (string) $request->user()->username, $request->ip());
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json(['message' => 'Mahasiswa ditambahkan.', 'id' => $id], 201);
|
||
}
|
||
|
||
public function update(Request $request, int $id): JsonResponse
|
||
{
|
||
$prodi = $this->prodiOrFail($request);
|
||
$input = $this->validated($request);
|
||
|
||
try {
|
||
$this->records->update($prodi, $id, $input, (string) $request->user()->username, $request->ip());
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json(['message' => 'Data mahasiswa diperbarui.']);
|
||
}
|
||
|
||
/** Hapus satu atau beberapa mahasiswa (POST {ids}). */
|
||
public function destroy(Request $request): JsonResponse
|
||
{
|
||
$prodi = $this->prodiOrFail($request);
|
||
$ids = $request->validate(['ids' => ['required', 'array', 'min:1', 'max:100'], 'ids.*' => ['integer']], [], ['ids' => 'mahasiswa'])['ids'];
|
||
|
||
try {
|
||
$deleted = $this->records->delete($prodi, $ids);
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json(['message' => "{$deleted} mahasiswa dihapus."]);
|
||
}
|
||
|
||
/** @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;
|
||
}
|
||
|
||
/** @return array<string, mixed> */
|
||
private function validated(Request $request): array
|
||
{
|
||
return $request->validate([
|
||
'nim' => ['required', 'string', 'max:15'],
|
||
'nama' => ['required', 'string', 'max:50'],
|
||
'prodi' => ['required', 'integer'],
|
||
'kurikulum' => ['required', 'integer'],
|
||
'angkatan' => ['required', 'integer'],
|
||
'program' => ['required', 'integer'],
|
||
'jalur' => ['nullable', 'integer'],
|
||
'kelamin' => ['required', 'integer', 'in:1,2'],
|
||
'tmplahir' => ['nullable', 'string', 'max:30'],
|
||
'tgllahir' => ['nullable', 'date'],
|
||
'hp' => ['nullable', 'string', 'max:35'],
|
||
'email' => ['nullable', 'string', 'email', 'max:50'],
|
||
'status' => ['required', 'integer', 'in:1,2,3,4,5'],
|
||
], [], [
|
||
'nim' => 'NIM', 'nama' => 'nama', 'prodi' => 'program studi', 'kurikulum' => 'kurikulum', 'angkatan' => 'angkatan',
|
||
'program' => 'program kuliah', 'jalur' => 'jalur masuk', 'kelamin' => 'jenis kelamin', 'tmplahir' => 'tempat lahir',
|
||
'tgllahir' => 'tanggal lahir', 'hp' => 'no. HP', 'email' => 'email', 'status' => 'status',
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* Formulir tambah/ubah (modal); pilihan kurikulum memuat seluruh kurikulum prodi akun.
|
||
*
|
||
* @param array<int, string> $programs
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function form(array $programs): array
|
||
{
|
||
$options = $this->records->options();
|
||
|
||
return [
|
||
'title' => 'mahasiswa',
|
||
'storeUrl' => route('operator.list-mahasiswa.store'),
|
||
'updateUrl' => route('operator.list-mahasiswa.update', '__ID__'),
|
||
'fields' => [
|
||
['name' => 'nim', 'label' => 'NIM', 'type' => 'text', 'required' => true, 'max' => 15, 'hint' => 'Tidak dapat diubah setelah disimpan.'],
|
||
['name' => 'nama', 'label' => 'Nama', 'type' => 'text', 'required' => true, 'max' => 50],
|
||
['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, 'hint' => 'Pilih kurikulum milik program studi yang sama.'],
|
||
['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' => 'kelamin', 'label' => 'Jenis kelamin', 'type' => 'select', 'options' => StudentRecords::GENDER, 'required' => true],
|
||
['name' => 'tmplahir', 'label' => 'Tempat lahir', 'type' => 'text', 'max' => 30],
|
||
['name' => 'tgllahir', 'label' => 'Tanggal lahir', 'type' => 'date'],
|
||
['name' => 'hp', 'label' => 'No. HP', 'type' => 'text', 'max' => 35],
|
||
['name' => 'email', 'label' => 'Email', 'type' => 'text', 'max' => 50],
|
||
['name' => 'status', 'label' => 'Status', 'type' => 'select', 'options' => StudentRecords::STATUS, 'required' => true, 'default' => 1, 'hint' => 'Status Lulus ditetapkan lewat yudisium.'],
|
||
],
|
||
];
|
||
}
|
||
|
||
/** JSON untuk tabel (satuRemoteTable). Hanya akun DB lama yang memegang prodi. */
|
||
public function data(Request $request): JsonResponse
|
||
{
|
||
abort_unless($request->user() instanceof Account, 404);
|
||
|
||
$programs = $this->overview->programs((int) $request->user()->getKey());
|
||
abort_if($programs->isEmpty(), 404);
|
||
|
||
return response()->json($this->payload($this->search($request, $programs, $this->directory->options($programs->keys()->all()))));
|
||
}
|
||
|
||
/**
|
||
* Nilai filter di luar pilihan (termasuk prodi milik akun lain) diabaikan.
|
||
*
|
||
* @param Collection<int, string> $programs
|
||
* @param array{angkatan: array<int, string>, program: array<int, string>, jalur: array<int, string>} $options
|
||
*/
|
||
private function search(Request $request, Collection $programs, array $options): LengthAwarePaginator
|
||
{
|
||
$prodi = $programs->has($request->integer('prodi')) ? $request->integer('prodi') : null;
|
||
$status = (string) $request->string('status');
|
||
$perPage = in_array($request->integer('per'), StudentDirectory::PER_PAGE, true) ? $request->integer('per') : StudentDirectory::PER_PAGE[0];
|
||
|
||
return $this->directory->search($prodi ? [$prodi] : $programs->keys()->all(), [
|
||
'q' => mb_substr(trim((string) $request->string('q')), 0, 100),
|
||
'angkatan' => isset($options['angkatan'][$request->integer('angkatan')]) ? $request->integer('angkatan') : null,
|
||
'status' => $this->directory->statusOptions()->has($status) ? $status : null,
|
||
'program' => isset($options['program'][$request->integer('program')]) ? $request->integer('program') : null,
|
||
'jalur' => isset($options['jalur'][$request->integer('jalur')]) ? $request->integer('jalur') : null,
|
||
], $perPage);
|
||
}
|
||
|
||
/**
|
||
* Baris siap tampil (angka sudah diformat) dan keterangan halaman.
|
||
*
|
||
* @return array{rows: list<array<string, mixed>>, page: int, last_page: int, total: int, per_page: int}
|
||
*/
|
||
private function payload(LengthAwarePaginator $rows): array
|
||
{
|
||
return [
|
||
'rows' => collect($rows->items())->map(fn (array $m): array => [
|
||
'id' => $m['id'],
|
||
'form' => $m['form'],
|
||
'nim' => $m['nim'],
|
||
'nama' => $m['nama'],
|
||
'jenjang' => $m['jenjang'] ?? '–',
|
||
'prodi' => $m['prodi'],
|
||
'masuk' => $m['angkatan'] === null ? '–' : (string) $m['angkatan'],
|
||
'status' => $m['status']['label'],
|
||
'tone' => $m['status']['tone'] === 'neutral' ? null : $m['status']['tone'],
|
||
'smt' => $m['semester'] === null ? '–' : (string) $m['semester'],
|
||
'sks' => number_format($m['sks_lulus'], 0, ',', '.'),
|
||
'ipk' => $m['ipk'] === null ? '–' : number_format($m['ipk'], 2, ',', ''),
|
||
])->all(),
|
||
'page' => $rows->currentPage(),
|
||
'last_page' => $rows->lastPage(),
|
||
'total' => $rows->total(),
|
||
'per_page' => $rows->perPage(),
|
||
];
|
||
}
|
||
}
|