275 lines
13 KiB
PHP
275 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Ringkasan akademik untuk dashboard operator dari DB lama, dibatasi pada program studi yang dipegang akun
|
|
* (t_unituser.idprogdi). Status semester (AKM) dari t_histori_mahasiswa.stat_akd_akhir.
|
|
*/
|
|
final class AcademicOverview
|
|
{
|
|
/**
|
|
* stat_akd_akhir menurut t_stat_akademik: 0 alpa kuliah, 1 aktif, 2 cuti, 3 lulus; 4 mengundurkan diri, 5 DO, 8 pindah,
|
|
* 11 putus kuliah, 12 wafat, 13 hilang, 14 alih fungsi dihitung "keluar". Kode administratif (6, 7, 9, 10) tidak dihitung.
|
|
*/
|
|
private const array STATUS = [0 => 'nonaktif', 1 => 'aktif', 2 => 'cuti', 3 => 'lulus', 4 => 'keluar', 5 => 'keluar', 8 => 'keluar', 11 => 'keluar', 12 => 'keluar', 13 => 'keluar', 14 => 'keluar'];
|
|
|
|
/** t_mahasiswa.stataktif: 1 masih tercatat aktif (2 lulus, lainnya keluar). */
|
|
private const int MAHASISWA_AKTIF = 1;
|
|
|
|
/** t_periodekuliah.perkuliahan: 1 semester reguler. */
|
|
private const int PERKULIAHAN_REGULER = 1;
|
|
|
|
/** t_kst.stat: 0 menunggu persetujuan, 2 batal. */
|
|
private const int KRS_MENUNGGU = 0;
|
|
|
|
private const int KRS_BATAL = 2;
|
|
|
|
/** t_matakuliah.tipe 2: tugas akhir (tanpa jadwal tatap muka). */
|
|
private const int MATAKULIAH_TUGAS_AKHIR = 2;
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
|
|
/**
|
|
* Program studi yang dipegang akun.
|
|
*
|
|
* @return Collection<int, string> idprogdi => nama
|
|
*/
|
|
public function programs(int $iduser): Collection
|
|
{
|
|
return $this->db()->table('t_unituser as u')
|
|
->join('t_bagian as b', 'b.idbagian', '=', 'u.idprogdi')
|
|
->where('u.iduser', $iduser)
|
|
->distinct()
|
|
->orderBy('b.namabagian')
|
|
->pluck('b.namabagian', 'b.idbagian')
|
|
->mapWithKeys(fn (string $nama, int|string $id): array => [(int) $id => trim($nama)]);
|
|
}
|
|
|
|
/**
|
|
* Semester reguler sampai semester aktif prodi, terbaru dulu.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @return Collection<int, string> idperiode => label
|
|
*/
|
|
public function periods(array $prodi, int $limit = 8): Collection
|
|
{
|
|
$active = $this->db()->table('t_periodekuliahaktif')->whereIn('idprogdi', $prodi)->max('idperiode');
|
|
|
|
if ($active === null) {
|
|
return collect();
|
|
}
|
|
|
|
return $this->db()->table('t_periodekuliah')
|
|
->where('perkuliahan', self::PERKULIAHAN_REGULER)
|
|
->where('idperiode', '<=', $active)
|
|
->orderByDesc('idperiode')
|
|
->limit($limit)
|
|
->get(['idperiode', 'thakad', 'semester'])
|
|
->mapWithKeys(fn (object $p): array => [(int) $p->idperiode => LegacyFormat::periodLabel($p->thakad, $p->semester)]);
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $prodi
|
|
* @return array{
|
|
* stats: array{aktif: int, aktif_lalu: ?int, periode_lalu: ?string, ips: ?float, ips_n: int, baru: int, angkatan: int, lulus: int},
|
|
* tanggungan: list<array{judul: string, teks: string}>,
|
|
* krs: array{mulai: ?Carbon, selesai: ?Carbon, terbuka: bool, aktif: int, pengisi: int, menunggu: int},
|
|
* angkatan: list<array<string, int>>,
|
|
* prodi: list<array<string, int|string>>,
|
|
* }
|
|
*/
|
|
public function summary(array $prodi, int $periode): array
|
|
{
|
|
$period = $this->db()->table('t_periodekuliah')->where('idperiode', $periode)->first(['thakad']);
|
|
$tahun = (int) substr((string) $period?->thakad, 0, 4);
|
|
$names = $this->db()->table('t_bagian')->whereIn('idbagian', $prodi)->pluck('namabagian', 'idbagian');
|
|
|
|
$akm = $this->akm($prodi, $periode);
|
|
$belum = $this->withoutStatus($prodi, $periode, $tahun);
|
|
$baru = $this->students($prodi)->where('g.tahun', $tahun)
|
|
->groupBy('m.idprogdi')->selectRaw('m.idprogdi, count(*) as jumlah')->pluck('jumlah', 'idprogdi');
|
|
|
|
$aktif = $akm->where('status', 'aktif');
|
|
$ipsN = (int) $aktif->sum('ips_n');
|
|
$previous = $this->db()->table('t_periodekuliah')
|
|
->where('perkuliahan', self::PERKULIAHAN_REGULER)->where('idperiode', '<', $periode)
|
|
->orderByDesc('idperiode')->first(['idperiode', 'thakad', 'semester']);
|
|
|
|
$krs = $this->krs($prodi, $periode) + ['aktif' => (int) $aktif->sum('jumlah')];
|
|
$kelas = $this->classes($prodi, $periode);
|
|
|
|
$empty = ['aktif' => 0, 'cuti' => 0, 'nonaktif' => 0, 'lulus' => 0, 'keluar' => 0];
|
|
|
|
$byAngkatan = $akm->whereNotNull('angkatan')->groupBy('angkatan')
|
|
->map(fn (Collection $rows): array => array_merge($empty, $rows->groupBy('status')->map->sum('jumlah')->all()));
|
|
foreach ($belum->groupBy('angkatan') as $angkatan => $rows) {
|
|
$byAngkatan[$angkatan] = ($byAngkatan[$angkatan] ?? $empty) + ['belum' => (int) $rows->sum('jumlah')];
|
|
}
|
|
|
|
$byProdi = $akm->groupBy('idprogdi')
|
|
->map(fn (Collection $rows): array => array_merge($empty, $rows->groupBy('status')->map->sum('jumlah')->all()));
|
|
|
|
$menungguKrs = $krs['menunggu'];
|
|
$tanpaStatus = (int) $belum->sum('jumlah');
|
|
|
|
return [
|
|
'stats' => [
|
|
'aktif' => $krs['aktif'],
|
|
'aktif_lalu' => $previous ? $this->activeCount($prodi, (int) $previous->idperiode) : null,
|
|
'periode_lalu' => $previous ? LegacyFormat::periodLabel($previous->thakad, $previous->semester) : null,
|
|
'ips' => $ipsN > 0 ? round($aktif->sum('ips_total') / $ipsN, 2) : null,
|
|
'ips_n' => $ipsN,
|
|
'baru' => (int) $baru->sum(),
|
|
'angkatan' => $tahun,
|
|
'lulus' => (int) $akm->where('status', 'lulus')->sum('jumlah'),
|
|
],
|
|
'tanggungan' => array_values(array_filter([
|
|
$menungguKrs > 0 ? ['judul' => $this->number($menungguKrs).' mahasiswa memiliki KRS yang belum disetujui', 'teks' => 'Menunggu persetujuan dosen pembimbing akademik.'] : null,
|
|
$tanpaStatus > 0 ? ['judul' => $this->number($tanpaStatus).' mahasiswa aktif belum memiliki status semester', 'teks' => 'Status kuliah (AKM) semester ini belum ditetapkan, berpengaruh pada pelaporan PDDIKTI.'] : null,
|
|
$kelas['tanpa_jadwal'] > 0 ? ['judul' => $this->number($kelas['tanpa_jadwal']).' kelas berpeserta belum memiliki jadwal', 'teks' => 'Tanpa hari dan jam tatap muka. Kelas tugas akhir tidak dihitung.'] : null,
|
|
$kelas['belum_terbit'] > 0 ? ['judul' => $this->number($kelas['belum_terbit']).' kelas belum menerbitkan nilai', 'teks' => 'Nilai kelas ini belum tampil di KHS mahasiswa.'] : null,
|
|
])),
|
|
'krs' => $krs,
|
|
'angkatan' => $byAngkatan->sortKeysDesc()
|
|
->map(fn (array $r, int|string $angkatan): array => ['angkatan' => (int) $angkatan] + $r + ['belum' => 0])
|
|
->map(fn (array $r): array => $r + ['total' => $r['aktif'] + $r['cuti'] + $r['nonaktif'] + $r['lulus'] + $r['keluar'] + $r['belum']])
|
|
->values()->all(),
|
|
'prodi' => collect($prodi)
|
|
->map(fn (int $id): array => ['prodi' => trim((string) ($names[$id] ?? $id))]
|
|
+ ($byProdi[$id] ?? $empty)
|
|
+ ['baru' => (int) ($baru[$id] ?? 0), 'belum' => (int) $belum->where('idprogdi', $id)->sum('jumlah')])
|
|
->sortBy('prodi')->values()->all(),
|
|
];
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function students(array $prodi): Builder
|
|
{
|
|
return $this->db()->table('t_mahasiswa as m')
|
|
->leftJoin('t_angkatanmhs as g', 'g.idangkatan', '=', 'm.idangkatan')
|
|
->whereIn('m.idprogdi', $prodi);
|
|
}
|
|
|
|
/**
|
|
* Jumlah status semester per prodi dan angkatan.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @return Collection<int, array{idprogdi: int, angkatan: ?int, status: string, jumlah: int, ips_total: float, ips_n: int}>
|
|
*/
|
|
private function akm(array $prodi, int $periode): Collection
|
|
{
|
|
return $this->students($prodi)
|
|
->join('t_histori_mahasiswa as h', 'h.idmahasiswa', '=', 'm.idmahasiswa')
|
|
->where('h.idperiode', $periode)
|
|
->whereIn('h.stat_akd_akhir', array_keys(self::STATUS))
|
|
->groupBy('m.idprogdi', 'g.tahun', 'h.stat_akd_akhir')
|
|
->selectRaw('m.idprogdi, g.tahun, h.stat_akd_akhir, count(*) as jumlah')
|
|
// IPS di histori diisi saat rekap nilai; 0 atau kosong berarti belum dihitung.
|
|
->selectRaw('sum(case when h.ips > 0 then h.ips else 0 end) as ips_total, sum(case when h.ips > 0 then 1 else 0 end) as ips_n')
|
|
->get()
|
|
->map(fn (object $r): array => [
|
|
'idprogdi' => (int) $r->idprogdi,
|
|
'angkatan' => $r->tahun === null ? null : (int) $r->tahun,
|
|
'status' => self::STATUS[(int) $r->stat_akd_akhir],
|
|
'jumlah' => (int) $r->jumlah,
|
|
'ips_total' => (float) $r->ips_total,
|
|
'ips_n' => (int) $r->ips_n,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Mahasiswa yang masih tercatat aktif tetapi belum punya status semester untuk periode ini.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @return Collection<int, array{idprogdi: int, angkatan: ?int, jumlah: int}>
|
|
*/
|
|
private function withoutStatus(array $prodi, int $periode, int $tahun): Collection
|
|
{
|
|
return $this->students($prodi)
|
|
->leftJoin('t_histori_mahasiswa as h', fn ($join) => $join->on('h.idmahasiswa', '=', 'm.idmahasiswa')->where('h.idperiode', $periode))
|
|
->where('m.stataktif', self::MAHASISWA_AKTIF)
|
|
->whereNull('h.stat_akd_akhir')
|
|
// Angkatan yang masuk setelah periode ini belum wajib berstatus.
|
|
->where('g.tahun', '<=', $tahun)
|
|
->groupBy('m.idprogdi', 'g.tahun')
|
|
->selectRaw('m.idprogdi, g.tahun, count(*) as jumlah')
|
|
->get()
|
|
->map(fn (object $r): array => ['idprogdi' => (int) $r->idprogdi, 'angkatan' => (int) $r->tahun, 'jumlah' => (int) $r->jumlah]);
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function activeCount(array $prodi, int $periode): int
|
|
{
|
|
return $this->students($prodi)
|
|
->join('t_histori_mahasiswa as h', 'h.idmahasiswa', '=', 'm.idmahasiswa')
|
|
->where('h.idperiode', $periode)
|
|
->where('h.stat_akd_akhir', 1)
|
|
->count();
|
|
}
|
|
|
|
/**
|
|
* Masa pengisian dan kemajuan KRS.
|
|
*
|
|
* @param list<int> $prodi
|
|
* @return array{mulai: ?Carbon, selesai: ?Carbon, terbuka: bool, pengisi: int, menunggu: int}
|
|
*/
|
|
private function krs(array $prodi, int $periode): array
|
|
{
|
|
$windows = $this->db()->table('t_periodelirs')->whereIn('idprogdi', $prodi)->where('idperiode', $periode)->get(['mulai', 'selesai']);
|
|
$today = today();
|
|
|
|
$progress = $this->students($prodi)
|
|
->join('t_kst as k', 'k.idmahasiswa', '=', 'm.idmahasiswa')
|
|
->leftJoin('t_histori_mahasiswa as h', fn ($join) => $join->on('h.idmahasiswa', '=', 'k.idmahasiswa')->where('h.idperiode', $periode))
|
|
->where('k.idperiode', $periode)
|
|
->where('k.stat', '<>', self::KRS_BATAL)
|
|
// Hanya mahasiswa berstatus aktif semester ini, agar sebanding dengan jumlah mahasiswa aktif.
|
|
->selectRaw('count(distinct case when h.stat_akd_akhir = 1 then k.idmahasiswa end) as pengisi')
|
|
->selectRaw('count(distinct case when k.stat = ? then k.idmahasiswa end) as menunggu', [self::KRS_MENUNGGU])
|
|
->first();
|
|
|
|
return [
|
|
'mulai' => $windows->min('mulai') ? Carbon::parse($windows->min('mulai')) : null,
|
|
'selesai' => $windows->max('selesai') ? Carbon::parse($windows->max('selesai')) : null,
|
|
'terbuka' => $windows->contains(fn (object $w): bool => $today->between(Carbon::parse($w->mulai), Carbon::parse($w->selesai))),
|
|
'pengisi' => (int) $progress?->pengisi,
|
|
'menunggu' => (int) $progress?->menunggu,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Kelas berpeserta tanpa jadwal tatap muka dan kelas yang nilainya belum terbit (t_jadwal.flag_cetak).
|
|
*
|
|
* @param list<int> $prodi
|
|
* @return array{tanpa_jadwal: int, belum_terbit: int}
|
|
*/
|
|
private function classes(array $prodi, int $periode): array
|
|
{
|
|
$row = $this->db()->table('t_jadwal as j')
|
|
->join('t_matakuliah as mk', 'mk.idmatakuliah', '=', 'j.idmatakuliah')
|
|
->where('j.idperiode', $periode)
|
|
->whereIn('j.idprogdi', $prodi)
|
|
->whereExists(fn (Builder $q) => $q->from('t_kst as k')->whereColumn('k.idjadwal', 'j.idjadwal')->where('k.stat', '<>', self::KRS_BATAL))
|
|
->selectRaw('sum(case when coalesce(mk.tipe, 0) <> ? and not exists (select 1 from t_jadwaltemu t where t.idjadwal = j.idjadwal) then 1 else 0 end) as tanpa_jadwal', [self::MATAKULIAH_TUGAS_AKHIR])
|
|
->selectRaw('sum(case when j.flag_cetak then 0 else 1 end) as belum_terbit')
|
|
->first();
|
|
|
|
return ['tanpa_jadwal' => (int) $row?->tanpa_jadwal, 'belum_terbit' => (int) $row?->belum_terbit];
|
|
}
|
|
|
|
private function number(int $value): string
|
|
{
|
|
return number_format($value, 0, ',', '.');
|
|
}
|
|
}
|