[Operator] Perbaikan Laman Portal -> Mahasiswa

This commit is contained in:
Ariyutta
2026-11-11 08:46:29 +07:00
parent ef46b84da8
commit 52d49cdb41
15 changed files with 1153 additions and 390 deletions
+372
View File
@@ -0,0 +1,372 @@
<?php
namespace App\Support\Legacy;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
/**
* Biodata lengkap mahasiswa DB lama (t_mahasiswa) untuk halaman detail operator: data pribadi, kontak dan alamat,
* orang tua, wali, serta asal sekolah. Dibaca dan ditulis per bagian dalam cakupan prodi akun. Hanya kolom yang nilainya
* berubah yang ditulis, jadi kolom kosong yang tidak disentuh tetap seperti semula. NIM, prodi, kurikulum, angkatan,
* program, jalur, dan status tidak diubah di sini (lewat Ubah pada daftar mahasiswa).
*/
final class StudentBiodata
{
/** Kode pilihan tanpa tabel referensi di DB; arti kode diambil dari komentar kolom t_mahasiswa. */
private const array MARITAL = [1 => 'Belum kawin', 2 => 'Kawin'];
private const array BLOOD = [0 => 'Tidak diisi', 1 => 'O', 2 => 'A', 3 => 'B', 4 => 'AB'];
/**
* Bagian halaman. Isian: name (kunci JSON dan kolom), label, type (text|textarea|date|number|select), max, lookup
* (nama pilihan untuk select), required, wide (lebar penuh), hint.
*
* @return array<string, array{title: string, icon: string, description: string, fields: list<array<string, mixed>>}>
*/
public static function sections(): array
{
return [
'pribadi' => [
'title' => 'Data pribadi',
'icon' => 'user',
'description' => 'Identitas diri dan kependudukan.',
'fields' => [
['name' => 'nama', 'label' => 'Nama lengkap', 'type' => 'text', 'max' => 50, 'required' => true],
['name' => 'kelamin', 'label' => 'Jenis kelamin', 'type' => 'select', 'lookup' => 'kelamin', 'required' => true],
['name' => 'tmplahir', 'label' => 'Tempat lahir', 'type' => 'text', 'max' => 30],
['name' => 'tgllahir', 'label' => 'Tanggal lahir', 'type' => 'date'],
['name' => 'agama', 'label' => 'Agama', 'type' => 'select', 'lookup' => 'agama'],
['name' => 'kewarganegaraan', 'label' => 'Kewarganegaraan', 'type' => 'select', 'lookup' => 'kewarganegaraan'],
['name' => 'stat_nikah', 'label' => 'Status pernikahan', 'type' => 'select', 'lookup' => 'nikah', 'required' => true],
['name' => 'gol_darah', 'label' => 'Golongan darah', 'type' => 'select', 'lookup' => 'darah'],
['name' => 'pekerjaan', 'label' => 'Pekerjaan', 'type' => 'select', 'lookup' => 'pekerjaan'],
['name' => 'no_id', 'label' => 'NIK', 'type' => 'text', 'max' => 20, 'alnum' => true],
['name' => 'no_kk', 'label' => 'Nomor KK', 'type' => 'text', 'max' => 20, 'alnum' => true],
['name' => 'nisn', 'label' => 'NISN', 'type' => 'text', 'max' => 10, 'alnum' => true],
],
],
'kontak' => [
'title' => 'Kontak dan alamat',
'icon' => 'map-pin',
'description' => 'Alamat tinggal, alamat asal, dan cara menghubungi.',
'fields' => [
['name' => 'hp', 'label' => 'No. HP', 'type' => 'text', 'max' => 35],
['name' => 'telepon', 'label' => 'Telepon', 'type' => 'text', 'max' => 35],
['name' => 'email_mhs', 'label' => 'Email', 'type' => 'email', 'max' => 50],
['name' => 'kodepos', 'label' => 'Kode pos', 'type' => 'text', 'max' => 5, 'alnum' => true],
['name' => 'alamat_mhs', 'label' => 'Alamat tinggal', 'type' => 'textarea', 'max' => 255, 'wide' => true],
['name' => 'asal_alamat', 'label' => 'Alamat asal', 'type' => 'textarea', 'max' => 255, 'wide' => true],
],
],
'ortu' => [
'title' => 'Orang tua',
'icon' => 'users',
'description' => 'Data ayah dan ibu.',
'fields' => [
['name' => 'namaayah', 'label' => 'Nama ayah', 'type' => 'text', 'max' => 50],
['name' => 'pend_ayah', 'label' => 'Pendidikan ayah', 'type' => 'select', 'lookup' => 'pendidikan'],
['name' => 'pekerjaan_ortu', 'label' => 'Pekerjaan ayah', 'type' => 'select', 'lookup' => 'pekerjaan'],
['name' => 'namaibu', 'label' => 'Nama ibu', 'type' => 'text', 'max' => 50],
['name' => 'pend_ibu', 'label' => 'Pendidikan ibu', 'type' => 'select', 'lookup' => 'pendidikan'],
['name' => 'pekerjaan_ibu', 'label' => 'Pekerjaan ibu', 'type' => 'select', 'lookup' => 'pekerjaan'],
['name' => 'penghasilan_ortu', 'label' => 'Penghasilan orang tua', 'type' => 'select', 'lookup' => 'penghasilan'],
['name' => 'tanggungan_ortu', 'label' => 'Jumlah tanggungan orang tua', 'type' => 'number', 'min' => 0, 'max' => 30],
['name' => 'alamat_ortu', 'label' => 'Alamat orang tua', 'type' => 'textarea', 'max' => 255, 'wide' => true],
],
],
'wali' => [
'title' => 'Wali',
'icon' => 'user-group',
'description' => 'Diisi bila mahasiswa memiliki wali selain orang tua.',
'fields' => [
['name' => 'nama_wali', 'label' => 'Nama wali', 'type' => 'text', 'max' => 50],
['name' => 'pend_wali', 'label' => 'Pendidikan wali', 'type' => 'select', 'lookup' => 'pendidikan'],
['name' => 'pekerjaan_wali', 'label' => 'Pekerjaan wali', 'type' => 'select', 'lookup' => 'pekerjaan'],
['name' => 'alamat_wali', 'label' => 'Alamat wali', 'type' => 'text', 'max' => 50],
],
],
'sekolah' => [
'title' => 'Asal sekolah',
'icon' => 'academic-cap',
'description' => 'Sekolah menengah atas atau sederajat.',
'fields' => [
['name' => 'namaslta', 'label' => 'Nama sekolah', 'type' => 'text', 'max' => 60],
['name' => 'npsn', 'label' => 'NPSN', 'type' => 'text', 'max' => 10, 'alnum' => true],
['name' => 'jurslta', 'label' => 'Jurusan', 'type' => 'text', 'max' => 30],
['name' => 'thnlulusslta', 'label' => 'Tahun lulus', 'type' => 'text', 'max' => 4, 'digits' => true],
],
],
];
}
/** @return list<string> */
public static function sectionKeys(): array
{
return array_keys(self::sections());
}
/** @var array<string, array<int, string>>|null */
private ?array $lookups = null;
private function db(): ConnectionInterface
{
return DB::connection('untan');
}
/**
* Pilihan select (kode => label). Kode 0 berarti "Tidak diisi" dan selalu ada pada kolom wajib di DB asli.
*
* @return array<string, array<int, string>>
*/
public function lookups(): array
{
if ($this->lookups !== null) {
return $this->lookups;
}
$zero = fn (array $options, string $label = 'Tidak diisi'): array => [0 => $label] + array_diff_key($options, [0 => true]);
$names = fn (string $table, string $key, string $label): array => $this->db()->table($table)->orderBy($key)->pluck($label, $key)
->mapWithKeys(fn (?string $name, int|string $id): array => [(int) $id => trim((string) $name)])->all();
return $this->lookups = [
'kelamin' => StudentRecords::GENDER,
'agama' => $zero($names('xs_agama', 'idagama', 'agama')),
'kewarganegaraan' => $zero($names('t_kewarganegaraan', 'idwarganegara', 'kewarganegaraan')),
'nikah' => self::MARITAL,
'darah' => self::BLOOD,
'pekerjaan' => $zero($names('t_pekerjaan', 'idpekerjaan', 'pekerjaan')),
'pendidikan' => $zero($names('t_tingkat_pendidikan', 'idtingkat', 'tingkatpendidikan')),
'penghasilan' => $zero($names('t_penghasilan', 'idpenghasilan', 'penghasilan')),
];
}
/** @param list<int> $prodi */
public function exists(array $prodi, int $id): bool
{
return $this->db()->table('t_mahasiswa')->whereIn('idprogdi', $prodi)->where('idmahasiswa', $id)->exists();
}
/**
* Biodata satu mahasiswa dalam cakupan prodi: per bagian, nilai isian dan teks tampil.
*
* @param list<int> $prodi
* @return array{sections: array<string, array<string, mixed>>, extra: array<string, mixed>}|null
*/
public function find(array $prodi, int $id): ?array
{
$row = $this->row($prodi, $id);
if ($row === null) {
return null;
}
$lookups = $this->lookups();
$sections = [];
foreach (self::sections() as $key => $section) {
$sections[$key] = $this->present($section, $row, $lookups) + ['key' => $key, 'title' => $section['title'], 'icon' => $section['icon'], 'description' => $section['description']];
}
return ['sections' => $sections, 'extra' => $this->extra($row)];
}
/**
* Simpan satu bagian. Mengembalikan bagian yang sudah diperbarui (nilai isian + teks tampil) dan banyak kolom yang berubah.
*
* @param list<int> $prodi
* @param array<string, mixed> $input
* @return array{section: array<string, mixed>, changed: int}
*/
public function update(array $prodi, int $id, string $key, array $input, string $actor, ?string $ip): array
{
$section = self::sections()[$key] ?? throw new PortalException('Bagian biodata tidak dikenal.');
$row = $this->row($prodi, $id) ?? throw new PortalException('Mahasiswa tidak ditemukan.');
$lookups = $this->lookups();
$changes = [];
foreach ($section['fields'] as $field) {
$name = $field['name'];
if (! array_key_exists($name, $input)) {
continue;
}
$new = $this->normalize($field, $input[$name], $lookups);
if ($field['name'] === 'nama' && $new === null) {
throw new PortalException('Nama wajib diisi.');
}
if ($new !== $this->normalize($field, $row->{$name}, $lookups)) {
// Kolom wajib di DB asli (NOT NULL) tidak pernah diisi null.
$changes[$name] = $new ?? (($field['type'] ?? '') === 'select' ? 0 : null);
}
}
if ($changes !== []) {
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $id)->update($changes + [
'cname' => ActivityTypes::APP_NAME, 'cip' => $ip ?? '', 'uname' => mb_substr($actor, 0, 15), 'tanggal' => now(),
'cip_updated' => mb_substr((string) $ip, 0, 20), 'last_updated' => now(),
]);
}
$fresh = $this->row($prodi, $id);
return [
'section' => $this->present($section, $fresh, $lookups),
'changed' => count($changes),
];
}
/** @param list<int> $prodi */
private function row(array $prodi, int $id): ?object
{
$columns = collect(self::sections())->pluck('fields')->flatten(1)->pluck('name')->all();
return $this->db()->table('t_mahasiswa as m')->whereIn('m.idprogdi', $prodi)->where('m.idmahasiswa', $id)
->first(array_merge(array_map(fn (string $c): string => 'm.'.$c, $columns), ['m.idpa', 'm.jalur_masuk', 'm.idkurikulum', 'm.stataktif', 'm.tgl_lulus']));
}
/**
* Nilai isian (untuk formulir) dan teks tampil satu bagian.
*
* @param array<string, mixed> $section
* @param array<string, array<int, string>> $lookups
* @return array{fields: list<array<string, mixed>>, values: array<string, mixed>, display: array<string, string>}
*/
private function present(array $section, object $row, array $lookups): array
{
$fields = [];
$values = [];
$display = [];
foreach ($section['fields'] as $field) {
$name = $field['name'];
$value = $this->normalize($field, $row->{$name}, $lookups);
if (($field['type'] ?? '') === 'select') {
$options = $lookups[$field['lookup']];
$current = (int) ($value ?? 0);
// Kode yang tidak ada di tabel referensi tetap tampil dan bisa dipertahankan.
$options += isset($options[$current]) ? [] : [$current => 'Kode '.$current];
$field['options'] = $options;
$values[$name] = $current;
$display[$name] = $current === 0 ? '–' : $options[$current];
} else {
$values[$name] = $value ?? '';
$display[$name] = match (true) {
$value === null => '–',
($field['type'] ?? '') === 'date' => Carbon::parse($value)->locale('id')->isoFormat('D MMMM YYYY'),
default => (string) $value,
};
}
$fields[] = $field;
}
return ['fields' => $fields, 'values' => $values, 'display' => $display];
}
/**
* Data akademik yang hanya dibaca: jalur masuk, kurikulum, dosen PA, tanggal lulus.
*
* @return array<string, mixed>
*/
private function extra(object $row): array
{
$jalur = $row->jalur_masuk === null ? null : $this->db()->table('t_jalurmasuk')->where('idjalur_siakad', $row->jalur_masuk)->value('nama_jalur_masuk');
$kurikulum = $row->idkurikulum === null ? null : $this->db()->table('t_kurikulum')->where('idkurikulum', $row->idkurikulum)->value('namakurikulum');
$pa = $row->idpa === null ? null : $this->db()->table('t_pegawai')->where('idpegawai', $row->idpa)->first(['gelardepan', 'nama', 'gelarbelakang']);
return [
'jalur' => filled($jalur) ? trim($jalur) : null,
'kurikulum' => filled($kurikulum) ? trim($kurikulum) : null,
'pa' => $pa === null ? null : LegacyFormat::pegawaiName($pa->gelardepan, $pa->nama, $pa->gelarbelakang),
'lulus' => (int) $row->stataktif === 2 && $row->tgl_lulus !== null ? Carbon::parse($row->tgl_lulus)->locale('id')->isoFormat('D MMMM YYYY') : null,
];
}
/**
* Nilai kolom dalam bentuk baku agar dua nilai bisa dibandingkan: teks dipangkas (kosong = null), tanggal Y-m-d,
* angka integer, pilihan integer (kosong = 0 pada pilihan yang punya kode 0).
*
* @param array<string, mixed> $field
* @param array<string, array<int, string>> $lookups
*/
private function normalize(array $field, mixed $value, array $lookups): string|int|null
{
$type = $field['type'] ?? 'text';
$text = is_scalar($value) ? trim((string) $value) : '';
return match ($type) {
'select' => (function () use ($text, $field, $lookups): ?int {
if ($text === '') {
return isset($lookups[$field['lookup']][0]) ? 0 : null;
}
return (int) $text;
})(),
'number' => $text === '' ? null : (int) $text,
'date' => $text === '' ? null : substr($text, 0, 10),
default => $text === '' ? null : $text,
};
}
/**
* Periksa isian satu bagian. Isian yang nilainya sama dengan yang tersimpan tidak diperiksa bentuknya (data lama
* boleh tidak seragam) dan tidak ikut dikembalikan; kolom wajib selalu diperiksa.
*
* @param list<int> $prodi
* @param array<string, mixed> $input
* @return array<string, mixed>
*
* @throws ValidationException
*/
public function validated(array $prodi, int $id, string $key, array $input): array
{
$row = $this->row($prodi, $id) ?? throw new PortalException('Mahasiswa tidak ditemukan.');
$lookups = $this->lookups();
$rules = $this->rules($key);
foreach (self::sections()[$key]['fields'] as $field) {
$name = $field['name'];
if (! ($field['required'] ?? false) && array_key_exists($name, $input)
&& $this->normalize($field, $input[$name], $lookups) === $this->normalize($field, $row->{$name}, $lookups)) {
unset($rules[$name]);
}
}
return Validator::make($input, $rules, [], $this->attributes($key))->validate();
}
/** @return array<string, list<string>> */
private function rules(string $key): array
{
$lookups = $this->lookups();
$rules = [];
foreach (self::sections()[$key]['fields'] as $field) {
$type = $field['type'] ?? 'text';
$base = ($field['required'] ?? false) ? ['required'] : ['nullable'];
$rules[$field['name']] = array_merge($base, match (true) {
$type === 'select' => ['integer', 'in:'.implode(',', array_keys($lookups[$field['lookup']]))],
$type === 'number' => ['integer', 'min:'.($field['min'] ?? 0), 'max:'.($field['max'] ?? 99)],
$type === 'date' => ['date_format:Y-m-d', 'before_or_equal:today'],
$type === 'email' => ['string', 'email', 'max:'.$field['max']],
default => array_values(array_filter([
'string',
'max:'.$field['max'],
($field['alnum'] ?? false) ? 'regex:/^[0-9A-Za-z]+$/' : null,
($field['digits'] ?? false) ? 'regex:/^[0-9]{4}$/' : null,
])),
});
}
return $rules;
}
/** @return array<string, string> */
private function attributes(string $key): array
{
return collect(self::sections()[$key]['fields'])->mapWithKeys(fn (array $f): array => [$f['name'] => mb_strtolower($f['label'])])->all();
}
}