190 lines
8.0 KiB
PHP
190 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy;
|
|
|
|
use Illuminate\Database\Connection;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Data Lembar Isian Rencana Studi (LIRS/KRS cetak) satu periode dari DB lama, baca-saja. Isi KRS dari t_kst (KST batal
|
|
* tidak dicetak) dengan jadwal temu dan dosen pengajar; IPS/IPK sebelumnya dari hasil studi semester reguler sebelumnya;
|
|
* batas SKS dari f_maxsks; identitas dan dosen PA dari StudyReportCard.
|
|
*/
|
|
class StudyPlanSheet
|
|
{
|
|
private const int STAT_BATAL = 2;
|
|
|
|
public function __construct(
|
|
private readonly StudyReportCard $reportCard,
|
|
private readonly StudyResults $results,
|
|
) {}
|
|
|
|
/**
|
|
* Periode yang punya KRS, terbaru lebih dulu (idperiode naik menurut waktu).
|
|
*
|
|
* @return Collection<int, array{key: int, ke: int|null, pendek: bool, tab: string, label: string, tahun_akademik: string, jumlah: int, sks: int}>
|
|
*/
|
|
public function periods(int $idmahasiswa): Collection
|
|
{
|
|
$rows = $this->db()->table('t_kst as k')
|
|
->join('t_matakuliah as m', 'm.idmatakuliah', '=', 'k.idmatakuliah')
|
|
->join('t_periodekuliah as p', 'p.idperiode', '=', 'k.idperiode')
|
|
->where('k.idmahasiswa', $idmahasiswa)
|
|
->where('k.stat', '<>', self::STAT_BATAL)
|
|
->groupBy('k.idperiode', 'p.thakad', 'p.semester', 'p.perkuliahan')
|
|
->orderByDesc('k.idperiode')
|
|
->selectRaw('k.idperiode, p.thakad, p.semester, p.perkuliahan, count(*) as jumlah, sum(m.sks) as sks')
|
|
->get();
|
|
|
|
if ($rows->isEmpty()) {
|
|
return collect();
|
|
}
|
|
|
|
$entry = $this->db()->table('t_mahasiswa as s')
|
|
->leftJoin('t_angkatanmhs as g', 'g.idangkatan', '=', 's.idangkatan')
|
|
->where('s.idmahasiswa', $idmahasiswa)
|
|
->first(['g.tahun', 'g.semester']);
|
|
|
|
return $rows->map(function (object $row) use ($entry): array {
|
|
$pendek = (int) $row->perkuliahan === LegacyFormat::PERKULIAHAN_PENDEK;
|
|
$ke = $pendek ? null : LegacyFormat::semesterNumber($entry?->tahun, $entry?->semester, $row->thakad, $row->semester);
|
|
|
|
return [
|
|
'key' => (int) $row->idperiode,
|
|
'ke' => $ke,
|
|
'pendek' => $pendek,
|
|
'tab' => $ke === null ? 'Pendek '.LegacyFormat::periodLabel($row->thakad, $row->semester) : 'Semester '.$ke,
|
|
'label' => LegacyFormat::periodLabel($row->thakad, $row->semester, $row->perkuliahan),
|
|
'tahun_akademik' => LegacyFormat::academicYear($row->thakad, $row->semester).($pendek ? ' (Pendek)' : ''),
|
|
'jumlah' => (int) $row->jumlah,
|
|
'sks' => (int) $row->sks,
|
|
];
|
|
})->values();
|
|
}
|
|
|
|
/**
|
|
* LIRS satu periode (periode tidak dikenal atau kosong = KRS terbaru); null bila mahasiswa belum punya KRS sama sekali.
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function forPeriod(int $idmahasiswa, ?int $idperiode): ?array
|
|
{
|
|
$periods = $this->periods($idmahasiswa);
|
|
$period = $periods->firstWhere('key', $idperiode) ?? $periods->first();
|
|
|
|
if ($period === null) {
|
|
return null;
|
|
}
|
|
|
|
$student = $this->reportCard->student($idmahasiswa);
|
|
$matkul = $this->courses($idmahasiswa, $period['key']);
|
|
[$ips, $ipk] = $this->previous($idmahasiswa, $period['key']);
|
|
|
|
return [
|
|
...$student['identitas'],
|
|
'foto' => $student['foto'],
|
|
'semester' => $period['ke'],
|
|
'tahun_akademik' => $period['tahun_akademik'],
|
|
'matkul' => $matkul->all(),
|
|
'jumlah_sks' => $matkul->sum('sks'),
|
|
'ips_sebelumnya' => $ips,
|
|
'ipk_sebelumnya' => $ipk,
|
|
'maks_sks' => $this->maxCredits($idmahasiswa, $student['konteks'], $period['key']),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Baris KRS dengan nomor urut, jadwal ("Kamis - 13:30:00 s/d 16:00:00 (SISKOM 2)"), dan dosen pengajar.
|
|
*
|
|
* @return Collection<int, array<string, mixed>>
|
|
*/
|
|
private function courses(int $idmahasiswa, int $idperiode): Collection
|
|
{
|
|
$rows = $this->db()->table('t_kst as k')
|
|
->join('t_matakuliah as m', 'm.idmatakuliah', '=', 'k.idmatakuliah')
|
|
->join('t_jadwal as j', 'j.idjadwal', '=', 'k.idjadwal')
|
|
->where('k.idmahasiswa', $idmahasiswa)
|
|
->where('k.idperiode', $idperiode)
|
|
->where('k.stat', '<>', self::STAT_BATAL)
|
|
->orderBy('m.kodemk')->orderBy('j.kelas')
|
|
->get(['k.idjadwal', 'm.kodemk', 'm.namamk', 'm.sks', 'j.kelas']);
|
|
|
|
$ids = $rows->pluck('idjadwal')->unique()->values();
|
|
|
|
$jadwal = $ids->isEmpty() ? collect() : $this->db()->table('t_jadwaltemu as t')
|
|
->leftJoin('t_jadwalruangkuliah as r', 'r.idjadwaltemu', '=', 't.idjadwaltemu')
|
|
->leftJoin('t_ruang as ru', 'ru.idruang', '=', 'r.idruang')
|
|
->whereIn('t.idjadwal', $ids)
|
|
->orderBy('t.hari')->orderBy('t.mulai')
|
|
->get(['t.idjadwal', 't.hari', 't.mulai', 't.selesai', 'ru.namaruang'])
|
|
->filter(fn (object $row): bool => LegacyFormat::dayName($row->hari) !== null)
|
|
->groupBy('idjadwal')
|
|
->map(fn (Collection $temu): array => $temu->map(fn (object $row): string => LegacyFormat::dayName($row->hari)
|
|
.' - '.substr((string) $row->mulai, 0, 8).' s/d '.substr((string) $row->selesai, 0, 8)
|
|
.(filled($row->namaruang) ? ' ('.trim($row->namaruang).')' : ''))->unique()->values()->all());
|
|
|
|
$dosen = $ids->isEmpty() ? collect() : $this->db()->table('t_jadwaldosen as d')
|
|
->join('t_pegawai as p', 'p.idpegawai', '=', 'd.iddosen')
|
|
->whereIn('d.idjadwal', $ids)
|
|
->orderBy('d.dosenke')
|
|
->get(['d.idjadwal', 'p.gelardepan', 'p.nama', 'p.gelarbelakang'])
|
|
->groupBy('idjadwal')
|
|
->map(fn (Collection $pengajar): array => $pengajar
|
|
->map(fn (object $row): string => LegacyFormat::pegawaiName($row->gelardepan, $row->nama, $row->gelarbelakang))
|
|
->unique()->values()->all());
|
|
|
|
return $rows->values()->map(fn (object $row, int $i): array => [
|
|
'no' => $i + 1,
|
|
'kode' => trim((string) $row->kodemk),
|
|
'nama' => trim((string) $row->namamk),
|
|
'sks' => (int) $row->sks,
|
|
'kelas' => trim((string) $row->kelas),
|
|
'jadwal' => $jadwal[$row->idjadwal] ?? [],
|
|
'dosen' => $dosen[$row->idjadwal] ?? [],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* IPS semester reguler sebelum periode ini dan IPK sesudahnya; [null, null] bila belum ada hasil studi sebelumnya.
|
|
*
|
|
* @return array{0: float|null, 1: float|null}
|
|
*/
|
|
private function previous(int $idmahasiswa, int $idperiode): array
|
|
{
|
|
$semesters = $this->results->semesters($idmahasiswa);
|
|
$before = $semesters->first(fn (array $semester): bool => $semester['key'] < $idperiode && ! $semester['pendek']);
|
|
|
|
if ($before === null) {
|
|
return [null, null];
|
|
}
|
|
|
|
$card = $this->reportCard->compose($semesters, $before['key'], $this->reportCard->histori($idmahasiswa), []);
|
|
|
|
return [$card['semester_ini']['ip'], $card['kumulatif']['ip']];
|
|
}
|
|
|
|
/** "Maksimum SKS yang dapat diambil" pada periode itu (f_maxsks); null bila tak bisa dihitung. */
|
|
private function maxCredits(int $idmahasiswa, ?object $context, int $idperiode): ?int
|
|
{
|
|
if ($context === null) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$max = $this->db()->scalar('select f_maxsks(?, ?, ?, ?)', [$idmahasiswa, $context->idprogdi, $context->idkurikulum, $idperiode]);
|
|
} catch (QueryException) {
|
|
// Lembar tetap bisa dicetak tanpa batas SKS bila fungsi DB lama tidak bisa dihitung.
|
|
return null;
|
|
}
|
|
|
|
return $max === null ? null : (int) $max;
|
|
}
|
|
|
|
private function db(): Connection
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
}
|