311 lines
13 KiB
PHP
311 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Penulisan Perkuliahan > Administrasi DB lama: pembimbing akademik (t_mahasiswa.idpa), cekal (t_mahasiswa_cekal),
|
|
* pengajuan cuti/berhenti studi (t_cuti), dan status mahasiswa keluar (t_mahasiswa.stataktif). Semua dibatasi ke
|
|
* program studi akun; kesalahan masukan dilempar sebagai PortalException (pesan Indonesia untuk 422).
|
|
*/
|
|
final class AdministrationWriter
|
|
{
|
|
private const int MASIH_KULIAH = 1;
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
|
|
// ---- Pembimbing Akademik ------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Dosen aktif yang home base-nya prodi akun, untuk pilihan PA: idpegawai => "NIP - Nama".
|
|
*
|
|
* @param list<int> $prodi
|
|
* @return array<int, string>
|
|
*/
|
|
public function advisorOptions(array $prodi): array
|
|
{
|
|
return $this->db()->table('t_dosen as d')->join('t_pegawai as p', 'p.idpegawai', '=', 'd.idpegawai')
|
|
->whereIn('d.idprogdi', $prodi)->where('p.statusaktif', 1)->orderBy('p.nama')
|
|
->get(['p.idpegawai', 'p.nip', 'p.nama', 'p.gelardepan', 'p.gelarbelakang'])
|
|
->mapWithKeys(fn (object $p): array => [(int) $p->idpegawai => trim((string) $p->nip).' - '.LegacyFormat::pegawaiName($p->gelardepan, $p->nama, $p->gelarbelakang)])->all();
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
public function assignAdvisor(array $prodi, string $nim, int $dosen): int
|
|
{
|
|
$student = $this->studentByNim($prodi, $nim);
|
|
$this->guardAdvisor($prodi, $dosen);
|
|
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $student->idmahasiswa)->update(['idpa' => $dosen]);
|
|
|
|
return (int) $student->idmahasiswa;
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
public function changeAdvisor(array $prodi, int $idmahasiswa, int $dosen): void
|
|
{
|
|
$student = $this->activeStudent($prodi, $idmahasiswa);
|
|
$this->guardAdvisor($prodi, $dosen);
|
|
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $student->idmahasiswa)->update(['idpa' => $dosen]);
|
|
}
|
|
|
|
/**
|
|
* Lepas PA mahasiswa yang masih kuliah di prodi akun.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param list<int> $ids
|
|
*/
|
|
public function releaseAdvisors(array $prodi, array $ids): int
|
|
{
|
|
$owned = $this->activeStudents($prodi)->whereIn('m.idmahasiswa', array_map('intval', $ids))->whereNotNull('m.idpa')->pluck('m.idmahasiswa')->map(fn ($id): int => (int) $id)->all();
|
|
if ($owned === []) {
|
|
throw new PortalException('Mahasiswa tidak ditemukan atau belum memiliki pembimbing akademik.');
|
|
}
|
|
|
|
return $this->db()->table('t_mahasiswa')->whereIn('idmahasiswa', $owned)->update(['idpa' => null]);
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function guardAdvisor(array $prodi, int $dosen): void
|
|
{
|
|
$valid = $this->db()->table('t_dosen as d')->join('t_pegawai as p', 'p.idpegawai', '=', 'd.idpegawai')
|
|
->whereIn('d.idprogdi', $prodi)->where('p.statusaktif', 1)->where('d.idpegawai', $dosen)->exists();
|
|
if (! $valid) {
|
|
throw new PortalException('Dosen harus dipilih dari dosen aktif program studi Anda.');
|
|
}
|
|
}
|
|
|
|
// ---- Cekal ---------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Cekal mahasiswa menurut NIM (Tambah): menolak bila sudah dicekal.
|
|
*
|
|
* @param list<int> $prodi
|
|
*/
|
|
public function addBlacklist(array $prodi, string $nim, bool $cekal): int
|
|
{
|
|
$student = $this->studentByNim($prodi, $nim);
|
|
if (! $cekal) {
|
|
throw new PortalException('Pilih "Ya" untuk mencekal mahasiswa; gunakan hapus untuk mencabut cekal.');
|
|
}
|
|
if ($this->db()->table('t_mahasiswa_cekal')->where('idmahasiswa', $student->idmahasiswa)->exists()) {
|
|
throw new PortalException('Mahasiswa sudah berstatus cekal.');
|
|
}
|
|
$this->db()->table('t_mahasiswa_cekal')->insert(['idmahasiswa' => $student->idmahasiswa, 'nim' => $student->nim]);
|
|
|
|
return (int) $student->idmahasiswa;
|
|
}
|
|
|
|
/**
|
|
* Ubah status cekal satu mahasiswa (Ubah): Ya menambahkan penanda, Tidak mencabutnya.
|
|
*
|
|
* @param list<int> $prodi
|
|
*/
|
|
public function setBlacklist(array $prodi, int $idmahasiswa, bool $cekal): void
|
|
{
|
|
$student = $this->activeStudent($prodi, $idmahasiswa);
|
|
$this->db()->transaction(function () use ($student, $cekal): void {
|
|
$exists = $this->db()->table('t_mahasiswa_cekal')->where('idmahasiswa', $student->idmahasiswa)->exists();
|
|
if ($cekal && ! $exists) {
|
|
$this->db()->table('t_mahasiswa_cekal')->insert(['idmahasiswa' => $student->idmahasiswa, 'nim' => $student->nim]);
|
|
} elseif (! $cekal && $exists) {
|
|
$this->db()->table('t_mahasiswa_cekal')->where('idmahasiswa', $student->idmahasiswa)->delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Cabut cekal mahasiswa prodi akun.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param list<int> $ids
|
|
*/
|
|
public function revokeBlacklist(array $prodi, array $ids): int
|
|
{
|
|
$owned = $this->db()->table('t_mahasiswa as m')->join('t_mahasiswa_cekal as c', 'c.idmahasiswa', '=', 'm.idmahasiswa')
|
|
->whereIn('m.idprogdi', $prodi)->whereIn('m.idmahasiswa', array_map('intval', $ids))->pluck('m.idmahasiswa')->map(fn ($id): int => (int) $id)->all();
|
|
if ($owned === []) {
|
|
throw new PortalException('Mahasiswa tidak ditemukan atau tidak sedang dicekal.');
|
|
}
|
|
|
|
return $this->db()->table('t_mahasiswa_cekal')->whereIn('idmahasiswa', $owned)->delete();
|
|
}
|
|
|
|
// ---- Berhenti Studi (pengajuan cuti) ------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @param list<int> $prodi
|
|
* @param array{nim: string, periode: int, alasan?: ?int, status: int} $input
|
|
*/
|
|
public function createLeave(array $prodi, array $input): int
|
|
{
|
|
$student = $this->studentByNim($prodi, $input['nim']);
|
|
$values = $this->leaveValues($input);
|
|
if ($this->db()->table('t_cuti')->where('idmahasiswa', $student->idmahasiswa)->where('mulai', $values['mulai'])->exists()) {
|
|
throw new PortalException('Mahasiswa sudah memiliki pengajuan cuti pada periode itu.');
|
|
}
|
|
|
|
return $this->db()->transaction(fn (): int => (int) $this->db()->table('t_cuti')->insertGetId(
|
|
['idmahasiswa' => $student->idmahasiswa, 'tglmohon' => now()->toDateString()] + $values, 'idcuti'));
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $prodi
|
|
* @param array{periode: int, alasan?: ?int, status: int} $input
|
|
*/
|
|
public function updateLeave(array $prodi, string $rowId, array $input): void
|
|
{
|
|
$id = $this->leaveId($rowId);
|
|
$leave = $this->ownedLeaves($prodi)->where('c.idcuti', $id)->first(['c.idcuti', 'c.idmahasiswa', 'c.mulai']);
|
|
if ($leave === null) {
|
|
throw new PortalException('Pengajuan cuti tidak ditemukan.');
|
|
}
|
|
$values = $this->leaveValues($input);
|
|
if ($this->db()->table('t_cuti')->where('idmahasiswa', $leave->idmahasiswa)->where('mulai', $values['mulai'])->where('idcuti', '<>', $id)->exists()) {
|
|
throw new PortalException('Mahasiswa sudah memiliki pengajuan cuti pada periode itu.');
|
|
}
|
|
$this->db()->table('t_cuti')->where('idcuti', $id)->update($values);
|
|
}
|
|
|
|
/**
|
|
* Hapus pengajuan cuti ("c-<id>"); baris mutasi DIKTI ("m-...") tidak boleh dihapus dari sini.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param list<string> $rowIds
|
|
*/
|
|
public function deleteLeaves(array $prodi, array $rowIds): int
|
|
{
|
|
$ids = array_map(fn (string $rowId): int => $this->leaveId($rowId), $rowIds);
|
|
$owned = $this->ownedLeaves($prodi)->whereIn('c.idcuti', $ids)->pluck('c.idcuti')->map(fn ($id): int => (int) $id)->all();
|
|
if ($owned === []) {
|
|
throw new PortalException('Pengajuan cuti tidak ditemukan.');
|
|
}
|
|
|
|
return $this->db()->table('t_cuti')->whereIn('idcuti', $owned)->delete();
|
|
}
|
|
|
|
private function leaveId(string $rowId): int
|
|
{
|
|
if (! preg_match('/^c-(\d+)$/', $rowId, $m)) {
|
|
throw new PortalException('Hanya pengajuan cuti yang dapat diubah atau dihapus; mutasi keluar berasal dari DIKTI.');
|
|
}
|
|
|
|
return (int) $m[1];
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function ownedLeaves(array $prodi): Builder
|
|
{
|
|
return $this->db()->table('t_cuti as c')->join('t_mahasiswa as m', 'm.idmahasiswa', '=', 'c.idmahasiswa')->whereIn('m.idprogdi', $prodi);
|
|
}
|
|
|
|
/**
|
|
* @param array{periode: int, alasan?: ?int, status: int} $input
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function leaveValues(array $input): array
|
|
{
|
|
if (! $this->db()->table('t_periodekuliah')->where('idperiode', $input['periode'])->exists()) {
|
|
throw new PortalException('Periode tidak ditemukan.');
|
|
}
|
|
if (! array_key_exists($input['status'], Administration::CUTI_STATUS)) {
|
|
throw new PortalException('Status pengajuan tidak dikenal.');
|
|
}
|
|
$alasan = $input['alasan'] ?? null;
|
|
if ($alasan !== null && ! $this->db()->table('xs_alasan_cuti')->where('idjalasan', $alasan)->exists()) {
|
|
throw new PortalException('Alasan cuti tidak ditemukan.');
|
|
}
|
|
|
|
return [
|
|
'mulai' => $input['periode'], 'sampai' => $input['periode'], 'alasancuti' => $alasan, 'stat' => $input['status'],
|
|
'tglverifikasi' => in_array($input['status'], [2, 3], true) ? now()->toDateString() : null,
|
|
];
|
|
}
|
|
|
|
/** @return array<int, string> */
|
|
public function leaveReasons(): array
|
|
{
|
|
return $this->db()->table('xs_alasan_cuti')->orderBy('idjalasan')->pluck('alasan', 'idjalasan')->map(fn ($v): string => trim((string) $v))->all();
|
|
}
|
|
|
|
// ---- Mahasiswa Keluar -----------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Tandai mahasiswa yang masih kuliah sebagai keluar (stataktif 3 s.d. 6).
|
|
*
|
|
* @param list<int> $prodi
|
|
*/
|
|
public function markLeaver(array $prodi, string $nim, int $status): int
|
|
{
|
|
$student = $this->studentByNim($prodi, $nim);
|
|
$this->guardLeaverStatus($status);
|
|
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $student->idmahasiswa)->update(['stataktif' => $status]);
|
|
|
|
return (int) $student->idmahasiswa;
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
public function changeLeaver(array $prodi, int $idmahasiswa, int $status): void
|
|
{
|
|
$this->guardLeaverStatus($status);
|
|
$exists = $this->db()->table('t_mahasiswa')->whereIn('idprogdi', $prodi)->where('idmahasiswa', $idmahasiswa)->whereIn('stataktif', array_keys(Administration::LEFT_STATUS))->exists();
|
|
if (! $exists) {
|
|
throw new PortalException('Mahasiswa keluar tidak ditemukan.');
|
|
}
|
|
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $idmahasiswa)->update(['stataktif' => $status]);
|
|
}
|
|
|
|
/**
|
|
* Batalkan status keluar: mahasiswa kembali berstatus masih kuliah.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @param list<int> $ids
|
|
*/
|
|
public function restoreLeavers(array $prodi, array $ids): int
|
|
{
|
|
$owned = $this->db()->table('t_mahasiswa')->whereIn('idprogdi', $prodi)->whereIn('idmahasiswa', array_map('intval', $ids))
|
|
->whereIn('stataktif', array_keys(Administration::LEFT_STATUS))->pluck('idmahasiswa')->map(fn ($id): int => (int) $id)->all();
|
|
if ($owned === []) {
|
|
throw new PortalException('Mahasiswa keluar tidak ditemukan.');
|
|
}
|
|
|
|
return $this->db()->table('t_mahasiswa')->whereIn('idmahasiswa', $owned)->update(['stataktif' => self::MASIH_KULIAH]);
|
|
}
|
|
|
|
private function guardLeaverStatus(int $status): void
|
|
{
|
|
if (! array_key_exists($status, Administration::LEFT_STATUS)) {
|
|
throw new PortalException('Status keluar tidak dikenal.');
|
|
}
|
|
}
|
|
|
|
// ---- Bagian bersama ---------------------------------------------------------------------------------------------
|
|
|
|
/** @param list<int> $prodi */
|
|
private function activeStudents(array $prodi): Builder
|
|
{
|
|
return $this->db()->table('t_mahasiswa as m')->whereIn('m.idprogdi', $prodi)->where('m.stataktif', self::MASIH_KULIAH);
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function activeStudent(array $prodi, int $idmahasiswa): object
|
|
{
|
|
return $this->activeStudents($prodi)->where('m.idmahasiswa', $idmahasiswa)->first(['m.idmahasiswa', 'm.nim'])
|
|
?? throw new PortalException('Mahasiswa tidak ditemukan atau sudah tidak berstatus masih kuliah.');
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function studentByNim(array $prodi, string $nim): object
|
|
{
|
|
$nim = mb_strtoupper(trim($nim));
|
|
|
|
return $this->activeStudents($prodi)->whereRaw('upper(trim(m.nim)) = ?', [$nim])->first(['m.idmahasiswa', 'm.nim'])
|
|
?? throw new PortalException("NIM {$nim} tidak ditemukan pada mahasiswa aktif program studi Anda.");
|
|
}
|
|
}
|