Files
siakad/app/Support/Legacy/StudentBiodata.php
2026-11-11 09:28:34 +07:00

251 lines
12 KiB
PHP

<?php
namespace App\Support\Legacy;
use App\Support\Legacy\Concerns\BiodataFields;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
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
{
use BiodataFields;
/** 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 = $this->changes($section['fields'], $row, $input, $lookups);
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']));
}
/**
* 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,
];
}
/**
* Periksa isian satu bagian (hanya isian yang berubah diperiksa bentuknya; lihat BiodataFields::validateFields).
*
* @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.');
return $this->validateFields(self::sections()[$key]['fields'], $row, $input);
}
}