105 lines
5.0 KiB
PHP
105 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Ubah data akademik inti mahasiswa DB lama (t_mahasiswa) dalam cakupan program studi akun: prodi, kurikulum, angkatan,
|
|
* program, jalur masuk, dan status. Tidak ada tambah atau hapus mahasiswa, NIM tidak diubah (trigger
|
|
* t_mahasiswa_block_ubah_nim_trigger di DB asli juga menolaknya), dan akun login (t_account) tidak disentuh.
|
|
* Biodata (pribadi, kontak, orang tua, wali, sekolah) ditulis oleh StudentBiodata.
|
|
*/
|
|
final class StudentRecords
|
|
{
|
|
/** Status yang boleh dipilih operator; Lulus lewat yudisium sehingga tidak bisa ditetapkan di sini. */
|
|
public const array STATUS = [1 => 'Masih kuliah', 2 => 'Lulus', 3 => 'Mengundurkan diri', 4 => 'Keluar', 5 => 'Pindah'];
|
|
|
|
public const array GENDER = [1 => 'Laki-laki', 2 => 'Perempuan'];
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
|
|
/**
|
|
* Pilihan isian formulir (seluruh referensi, bukan hanya yang dipakai mahasiswa prodi).
|
|
*
|
|
* @return array{angkatan: array<int, string>, program: array<int, string>, jalur: array<int, string>}
|
|
*/
|
|
public function options(): array
|
|
{
|
|
return [
|
|
'angkatan' => $this->db()->table('t_angkatanmhs')->where('tahun', '<=', (int) date('Y') + 1)->where('tahun', '>=', 1990)
|
|
->orderByDesc('tahun')->orderBy('semester')->get(['idangkatan', 'tahun', 'semester'])
|
|
->mapWithKeys(fn (object $a): array => [(int) $a->idangkatan => $a->tahun.' '.([1 => 'Ganjil', 2 => 'Genap'][(int) $a->semester] ?? 'Semester '.$a->semester)])->all(),
|
|
'program' => $this->db()->table('t_jenisprogram')->orderBy('program')->pluck('program', 'idprogram')
|
|
->map(fn (string $nama): string => trim($nama))->all(),
|
|
'jalur' => $this->db()->table('t_jalurmasuk')->orderBy('nama_jalur_masuk')->pluck('nama_jalur_masuk', 'idjalur_siakad')
|
|
->map(fn (?string $nama): string => trim((string) $nama))->all(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $prodi
|
|
* @param array{prodi: mixed, kurikulum: mixed, angkatan: mixed, program: mixed, jalur?: mixed, status: mixed} $input
|
|
*/
|
|
public function update(array $prodi, int $id, array $input, string $actor, ?string $ip): void
|
|
{
|
|
$current = $this->owned($prodi)->where('m.idmahasiswa', $id)->first(['m.stataktif']);
|
|
if ($current === null) {
|
|
throw new PortalException('Mahasiswa tidak ditemukan.');
|
|
}
|
|
|
|
$prodiId = (int) ($input['prodi'] ?? 0);
|
|
if (! in_array($prodiId, $prodi, true)) {
|
|
throw new PortalException('Program studi harus dipilih dari program studi Anda.');
|
|
}
|
|
$curriculum = (int) ($input['kurikulum'] ?? 0);
|
|
if (! $this->db()->table('t_kurikulum')->where('idkurikulum', $curriculum)->where('idprogdi', $prodiId)->exists()) {
|
|
throw new PortalException('Kurikulum harus dipilih dari program studi mahasiswa.');
|
|
}
|
|
$batch = (int) ($input['angkatan'] ?? 0);
|
|
if (! $this->db()->table('t_angkatanmhs')->where('idangkatan', $batch)->exists()) {
|
|
throw new PortalException('Angkatan tidak ditemukan.');
|
|
}
|
|
$program = (int) ($input['program'] ?? 0);
|
|
if (! $this->db()->table('t_jenisprogram')->where('idprogram', $program)->exists()) {
|
|
throw new PortalException('Program kuliah tidak ditemukan.');
|
|
}
|
|
$entry = filled($input['jalur'] ?? null) ? (int) $input['jalur'] : null;
|
|
if ($entry !== null && ! $this->db()->table('t_jalurmasuk')->where('idjalur_siakad', $entry)->exists()) {
|
|
throw new PortalException('Jalur masuk tidak ditemukan.');
|
|
}
|
|
$status = (int) ($input['status'] ?? 1);
|
|
if (! isset(self::STATUS[$status])) {
|
|
throw new PortalException('Status mahasiswa tidak dikenal.');
|
|
}
|
|
if ($status === 2 && (int) $current->stataktif !== 2) {
|
|
throw new PortalException('Status Lulus ditetapkan lewat yudisium, bukan di sini.');
|
|
}
|
|
|
|
$values = ['idprogdi' => $prodiId, 'idkurikulum' => $curriculum, 'idangkatan' => $batch, 'idprogram' => $program, 'stataktif' => $status];
|
|
// Jalur masuk NOT NULL di DB asli: kosong berarti tidak diubah.
|
|
if ($entry !== null) {
|
|
$values['jalur_masuk'] = $entry;
|
|
}
|
|
|
|
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $id)->update($values + $this->audit($actor, $ip));
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function owned(array $prodi): Builder
|
|
{
|
|
return $this->db()->table('t_mahasiswa as m')->whereIn('m.idprogdi', $prodi);
|
|
}
|
|
|
|
/** Kolom audit seperti aplikasi lama: cname nama aplikasi, cip alamat IP, uname username (dipotong 15). */
|
|
private function audit(string $actor, ?string $ip): array
|
|
{
|
|
return ['cname' => ActivityTypes::APP_NAME, 'cip' => $ip ?? '', 'uname' => mb_substr($actor, 0, 15), 'tanggal' => now()];
|
|
}
|
|
}
|