222 lines
10 KiB
PHP
222 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Tulis data inti mahasiswa DB lama (t_mahasiswa) dalam cakupan program studi akun: tambah, ubah, hapus.
|
|
* Akun login (t_account) tidak dibuat/diubah/dihapus; mahasiswa yang sudah punya data akademik, keuangan, atau akun
|
|
* tidak boleh dihapus. NIM tidak bisa diubah (trigger t_mahasiswa_block_ubah_nim_trigger di DB asli juga menolaknya).
|
|
*/
|
|
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'];
|
|
|
|
/** Tabel yang merujuk idmahasiswa; baris di salah satunya menghalangi penghapusan. */
|
|
private const array DEPENDENTS = [
|
|
't_kst', 't_transkrip', 't_keuanganmhs', 't_histori_mahasiswa', 't_evaluasi', 't_evaluasi_sp', 't_cuti', 't_skripsi',
|
|
't_mutasi_dikti', 't_mahasiswa_cekal', 't_pindah', 't_aktivasikeuangan', 't_beasiswa_mhs', 't_kelompokukt',
|
|
't_mahasiswa_masastudi', 't_update_biodata', 'pdpt_feeder_mahasiswa',
|
|
];
|
|
|
|
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{nim: string, nama: string, prodi: int, kurikulum: int, angkatan: int, program: int, jalur?: ?int, kelamin: int, tmplahir?: ?string, tgllahir?: ?string, hp?: ?string, email?: ?string, status: int} $input
|
|
* @return int idmahasiswa baru
|
|
*/
|
|
public function create(array $prodi, array $input, string $actor, ?string $ip): int
|
|
{
|
|
$values = $this->values($prodi, $input, null);
|
|
if ($values['stataktif'] === 2) {
|
|
throw new PortalException('Status Lulus ditetapkan lewat yudisium, bukan di sini.');
|
|
}
|
|
$nim = $values['nim'];
|
|
if (! preg_match('/^[A-Z0-9]{5,15}$/', $nim)) {
|
|
throw new PortalException('NIM hanya boleh huruf dan angka (5 sampai 15 karakter).');
|
|
}
|
|
if ($this->db()->table('t_mahasiswa')->whereRaw('upper(trim(nim)) = ?', [$nim])->exists()) {
|
|
throw new PortalException("NIM {$nim} sudah terdaftar.");
|
|
}
|
|
|
|
return (int) $this->db()->table('t_mahasiswa')->insertGetId($values + $this->audit($actor, $ip), 'idmahasiswa');
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $prodi
|
|
* @param array<string, 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.nim', 'm.stataktif']);
|
|
if ($current === null) {
|
|
throw new PortalException('Mahasiswa tidak ditemukan.');
|
|
}
|
|
$values = $this->values($prodi, $input, $current);
|
|
if (strtoupper(trim((string) $current->nim)) !== $values['nim']) {
|
|
throw new PortalException('NIM tidak dapat diubah. Buat mahasiswa baru dengan NIM yang sesuai.');
|
|
}
|
|
if ($values['stataktif'] === 2 && (int) $current->stataktif !== 2) {
|
|
throw new PortalException('Status Lulus ditetapkan lewat yudisium, bukan di sini.');
|
|
}
|
|
unset($values['nim']);
|
|
|
|
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $id)->update($values + $this->audit($actor, $ip));
|
|
}
|
|
|
|
/**
|
|
* Hapus mahasiswa tanpa data turunan. Gagal seluruhnya (tanpa menghapus sebagian) bila ada yang dirujuk.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param list<int> $ids
|
|
*/
|
|
public function delete(array $prodi, array $ids): int
|
|
{
|
|
$rows = $this->owned($prodi)->whereIn('m.idmahasiswa', array_map('intval', $ids))->get(['m.idmahasiswa', 'm.nim']);
|
|
if ($rows->isEmpty()) {
|
|
throw new PortalException('Mahasiswa tidak ditemukan.');
|
|
}
|
|
|
|
$blocked = $rows->filter(fn (object $m): bool => $this->isReferenced((int) $m->idmahasiswa))
|
|
->map(fn (object $m): string => trim((string) $m->nim))->values();
|
|
if ($blocked->isNotEmpty()) {
|
|
$list = $blocked->take(5)->implode(', ').($blocked->count() > 5 ? ', dan '.($blocked->count() - 5).' lainnya' : '');
|
|
throw new PortalException("Mahasiswa {$list} sudah punya data akademik, keuangan, atau akun login sehingga tidak boleh dihapus.");
|
|
}
|
|
|
|
$keys = $rows->pluck('idmahasiswa')->map(fn ($id): int => (int) $id)->all();
|
|
|
|
return $this->db()->transaction(function () use ($keys): int {
|
|
// Baris rekap kosong dibuat otomatis oleh trigger saat mahasiswa ditambahkan.
|
|
$this->db()->table('t_evaluasi_rekap')->whereIn('idmahasiswa', $keys)->delete();
|
|
|
|
return $this->db()->table('t_mahasiswa')->whereIn('idmahasiswa', $keys)->delete();
|
|
});
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function owned(array $prodi): Builder
|
|
{
|
|
return $this->db()->table('t_mahasiswa as m')->whereIn('m.idprogdi', $prodi);
|
|
}
|
|
|
|
private function isReferenced(int $id): bool
|
|
{
|
|
$schema = $this->db()->getSchemaBuilder();
|
|
foreach (self::DEPENDENTS as $table) {
|
|
if ($schema->hasTable($table) && $this->db()->table($table)->where('idmahasiswa', $id)->exists()) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return $this->db()->table('t_account')->where('jenis', 1)->where('identitas', $id)->exists();
|
|
}
|
|
|
|
/**
|
|
* Nilai kolom t_mahasiswa yang sudah diperiksa terhadap cakupan prodi dan tabel referensi.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param array<string, mixed> $input
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function values(array $prodi, array $input, ?object $current): array
|
|
{
|
|
$text = fn (mixed $v): ?string => ($v = trim((string) $v)) === '' ? null : $v;
|
|
$nim = strtoupper(trim((string) ($input['nim'] ?? '')));
|
|
$nama = trim((string) ($input['nama'] ?? ''));
|
|
if ($nim === '' || $nama === '') {
|
|
throw new PortalException('NIM dan nama wajib diisi.');
|
|
}
|
|
|
|
$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.');
|
|
}
|
|
$gender = (int) ($input['kelamin'] ?? 0);
|
|
if (! isset(self::GENDER[$gender])) {
|
|
throw new PortalException('Jenis kelamin harus dipilih.');
|
|
}
|
|
$status = (int) ($input['status'] ?? 1);
|
|
if (! isset(self::STATUS[$status])) {
|
|
throw new PortalException('Status mahasiswa tidak dikenal.');
|
|
}
|
|
$email = $text($input['email'] ?? null);
|
|
if ($email !== null && ! filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
throw new PortalException('Format email tidak valid.');
|
|
}
|
|
|
|
$values = [
|
|
'nim' => $nim,
|
|
'nama' => $nama,
|
|
'idprogdi' => $prodiId,
|
|
'idkurikulum' => $curriculum,
|
|
'idangkatan' => $batch,
|
|
'idprogram' => $program,
|
|
'kelamin' => $gender,
|
|
'tmplahir' => $text($input['tmplahir'] ?? null),
|
|
'tgllahir' => $text($input['tgllahir'] ?? null),
|
|
'hp' => $text($input['hp'] ?? null),
|
|
'email_mhs' => $email,
|
|
'stataktif' => $status,
|
|
];
|
|
// Jalur masuk NOT NULL (bawaan 1) di DB asli: kosong berarti tidak diubah pada ubah, bawaan pada tambah.
|
|
if ($entry !== null) {
|
|
$values['jalur_masuk'] = $entry;
|
|
} elseif ($current === null) {
|
|
$values['jalur_masuk'] = 1;
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
|
|
/** 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()];
|
|
}
|
|
}
|