Files
2026-11-17 08:46:36 +07:00

253 lines
13 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Support\Legacy\Reports;
use App\Support\Legacy\Curriculum;
use App\Support\Legacy\LegacyFormat;
use App\Support\Legacy\PortalException;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
/**
* Laporan kurikulum: daftar kurikulum prodi (repp_kurikulumprodi) dan daftar mata kuliah (repp_matakuliah) dari
* t_kurikulum, t_matakuliah, t_jenismatakuliah, t_konsentrasi. Hanya baca, selalu dibatasi prodi akun.
*
* Tanpa sumber di DB lama (dilaporkan koordinator): silabus/SAP (t_matakuliahsap hanya 3 baris tahun 2005-2006),
* RPS (tidak ada tabel), kuesioner/EDOM (tidak ada tabel jawaban), kegiatan pendukung, laporan manual.
*/
final class KurikulumReports implements ReportSource
{
private const string KURIKULUM = 'repp_kurikulumprodi';
private const string MATAKULIAH = 'repp_matakuliah';
/** Mata kuliah pilihan menurut kelompok MK (t_jenismatakuliah.sifat selalu kosong), sama dengan Curriculum. */
private const string ELECTIVE_SQL = "(lower(coalesce(j.keterangan, '')) like '%pilihan%' or upper(trim(coalesce(j.jenis, ''))) = 'MKP')";
private const array STATUS = ['1' => 'Aktif', '0' => 'Tidak aktif'];
public function __construct(private readonly Curriculum $curriculum) {}
public function slugs(): array
{
return [self::KURIKULUM, self::MATAKULIAH];
}
public function definition(string $slug, ReportContext $context): array
{
$prodi = ['name' => 'prodi', 'label' => 'Program Studi', 'type' => 'select', 'options' => $context->prodiNames];
$years = $context->prodi === [] ? [] : $this->curriculum->years($context->prodi)->all();
$tahun = ['name' => 'tahun', 'label' => 'Tahun Kurikulum', 'type' => 'select', 'options' => $years];
return match ($slug) {
self::KURIKULUM => ['export' => 'xlsx', 'params' => [
$prodi,
$tahun,
['name' => 'status', 'label' => 'Status Kurikulum', 'type' => 'select', 'options' => self::STATUS],
]],
self::MATAKULIAH => [
'note' => 'Koordinator mata kuliah tidak tercatat di data kurikulum sehingga tidak tersedia sebagai filter.',
'export' => 'xlsx',
// Kode/nama mata kuliah dicari lewat kotak cari standar di atas tabel (q), bukan isian di bilah filter.
'search' => true,
'params' => [
$tahun,
$prodi,
['name' => 'tipe', 'label' => 'Jenis Mata Kuliah', 'type' => 'select', 'options' => Curriculum::TYPES],
['name' => 'kelompok', 'label' => 'Kelompok Mata Kuliah', 'type' => 'select',
'options' => $context->prodi === [] ? [] : $this->curriculum->groups($context->prodi)->all()],
],
],
default => throw new PortalException('Laporan tidak ditemukan.'),
};
}
public function run(string $slug, array $params, ReportContext $context, int $limit): ReportResult
{
return match ($slug) {
self::KURIKULUM => $this->curricula($params, $context, $limit),
self::MATAKULIAH => $this->courses($params, $context, $limit),
default => throw new PortalException('Laporan tidak ditemukan.'),
};
}
private function db(): ConnectionInterface
{
return DB::connection('untan');
}
/** Prodi yang dilaporkan: pilihan operator bila termasuk prodi akun, selain itu semua prodi akun. */
private function scope(array $params, ReportContext $context): array
{
$chosen = filled($params['prodi'] ?? null) ? (int) $params['prodi'] : null;
return $chosen !== null && in_array($chosen, $context->prodi, true) ? [$chosen] : $context->prodi;
}
/** @param list<int> $prodi */
private function levels(array $prodi): array
{
return $this->db()->table('pdpt_tmst_program_studi as pp')->join('t_jenjangprogdi as jp', 'jp.kode', '=', 'pp.kode_jenjang_pendidikan')
->whereIn('pp.idbagianuntan', $prodi)->groupBy('pp.idbagianuntan')
->selectRaw('pp.idbagianuntan as id, min(jp.kdjenjang) as jenjang')->get()
->mapWithKeys(fn (object $r): array => [(int) $r->id => trim((string) $r->jenjang)])->all();
}
private function programLabel(?string $jenjang, ?string $name): string
{
return trim((filled($jenjang) ? $jenjang.' - ' : '').trim((string) $name));
}
private function curricula(array $params, ReportContext $context, int $limit): ReportResult
{
$columns = [
['key' => 'prodi', 'label' => 'Program Studi'],
['key' => 'kode', 'label' => 'Kode Prodi'],
['key' => 'kurikulum', 'label' => 'Kurikulum'],
['key' => 'tahun', 'label' => 'Tahun'],
['key' => 'berlaku', 'label' => 'Berlaku Mulai'],
['key' => 'program', 'label' => 'Program'],
['key' => 'status', 'label' => 'Status'],
['key' => 'jumlah_mk', 'label' => 'Jumlah MK', 'num' => true],
['key' => 'sks_wajib', 'label' => 'SKS Wajib', 'num' => true],
['key' => 'sks_pilihan', 'label' => 'SKS Pilihan', 'num' => true],
['key' => 'sks_total', 'label' => 'Total SKS', 'num' => true],
];
$prodi = $this->scope($params, $context);
if ($prodi === []) {
return new ReportResult($columns, [], 0);
}
$base = fn (): Builder => $this->db()->table('t_kurikulum as k')
->join('t_bagian as b', 'b.idbagian', '=', 'k.idprogdi')
->leftJoin('t_periodekuliah as p', 'p.idperiode', '=', 'k.idperiode')
->whereIn('k.idprogdi', $prodi)
->when(filled($params['tahun'] ?? null), fn (Builder $q) => $q->whereRaw('substr(p.thakad, 1, 4) = ?', [(string) $params['tahun']]))
->when(filled($params['status'] ?? null), fn (Builder $q) => $q->where('k.aktif', (string) $params['status'] === '1'));
$sks = fn (bool $elective): Builder => $this->db()->table('t_matakuliah as m')->leftJoin('t_jenismatakuliah as j', 'j.idjenis', '=', 'm.idjenis')
->whereColumn('m.idkurikulum', 'k.idkurikulum')
->whereRaw(($elective ? '' : 'not ').self::ELECTIVE_SQL)
->selectRaw('coalesce(sum(m.sks), 0)');
$count = $this->db()->table('t_matakuliah as m')->whereColumn('m.idkurikulum', 'k.idkurikulum')->selectRaw('count(*)');
$total = (int) $base()->count();
$levels = $this->levels($prodi);
$rows = $base()
->leftJoin('t_jenisprogram as jp', 'jp.idprogram', '=', 'k.idprogram')
->orderByDesc('p.thakad')->orderBy('b.namabagian')->orderByDesc('k.idkurikulum')
->limit($limit)
->select('k.idkurikulum', 'k.idprogdi', 'k.namakurikulum', 'k.aktif', 'p.thakad', 'p.semester', 'p.perkuliahan', 'b.kodebagian', 'b.namabagian', 'jp.program')
->selectSub($count, 'jumlah_mk')->selectSub($sks(false), 'sks_wajib')->selectSub($sks(true), 'sks_pilihan')
->get()
->map(function (object $k) use ($levels): array {
$wajib = (int) $k->sks_wajib;
$pilihan = (int) $k->sks_pilihan;
return [
'prodi' => $this->programLabel($levels[(int) $k->idprogdi] ?? null, $k->namabagian),
'kode' => filled($k->kodebagian) ? trim($k->kodebagian) : null,
'kurikulum' => filled($k->namakurikulum) ? trim($k->namakurikulum) : null,
'tahun' => $k->thakad === null ? null : substr(trim($k->thakad), 0, 4),
'berlaku' => $k->thakad === null ? null : LegacyFormat::periodLabel($k->thakad, $k->semester, $k->perkuliahan),
'program' => filled($k->program) ? trim($k->program) : null,
'status' => $k->aktif ? 'Aktif' : 'Tidak aktif',
'jumlah_mk' => (int) $k->jumlah_mk,
'sks_wajib' => $wajib,
'sks_pilihan' => $pilihan,
'sks_total' => $wajib + $pilihan,
];
})->all();
$summary = [$this->scopeSummary($prodi, $context)];
if (filled($params['tahun'] ?? null)) {
$summary[] = 'Tahun kurikulum: '.$params['tahun'];
}
if (filled($params['status'] ?? null)) {
$summary[] = 'Status: '.self::STATUS[(string) $params['status']];
}
$summary[] = 'Jumlah: '.$total.' kurikulum';
return new ReportResult($columns, $rows, $total, $summary, 'Pilihan = kelompok mata kuliah berketerangan "pilihan" atau berkode MKP; SKS dihitung dari seluruh mata kuliah kurikulum.');
}
private function courses(array $params, ReportContext $context, int $limit): ReportResult
{
$columns = [
['key' => 'kode', 'label' => 'Kode MK'],
['key' => 'nama', 'label' => 'Mata Kuliah'],
['key' => 'sks', 'label' => 'SKS', 'num' => true],
['key' => 'semester', 'label' => 'Semester', 'num' => true],
['key' => 'jenis', 'label' => 'Jenis'],
['key' => 'kelompok', 'label' => 'Kelompok'],
['key' => 'sifat', 'label' => 'Sifat'],
['key' => 'grup', 'label' => 'Grup MK'],
['key' => 'kurikulum', 'label' => 'Kurikulum'],
['key' => 'prodi', 'label' => 'Program Studi'],
];
$prodi = $this->scope($params, $context);
if ($prodi === []) {
return new ReportResult($columns, [], 0);
}
$q = mb_strtolower(trim((string) ($params['q'] ?? '')));
$base = fn (): Builder => $this->db()->table('t_matakuliah as m')
->join('t_kurikulum as k', 'k.idkurikulum', '=', 'm.idkurikulum')
->join('t_bagian as b', 'b.idbagian', '=', 'k.idprogdi')
->leftJoin('t_periodekuliah as p', 'p.idperiode', '=', 'k.idperiode')
->leftJoin('t_jenismatakuliah as j', 'j.idjenis', '=', 'm.idjenis')
->whereIn('k.idprogdi', $prodi)
->when(filled($params['tahun'] ?? null), fn (Builder $w) => $w->whereRaw('substr(p.thakad, 1, 4) = ?', [(string) $params['tahun']]))
->when(filled($params['tipe'] ?? null), fn (Builder $w) => $w->where('m.tipe', (int) $params['tipe']))
->when(filled($params['kelompok'] ?? null), fn (Builder $w) => $w->whereRaw('upper(trim(j.jenis)) = ?', [mb_strtoupper((string) $params['kelompok'])]))
->when($q !== '', fn (Builder $w) => $w->where(fn (Builder $x) => $x
->whereRaw('lower(m.kodemk) like ?', ["%{$q}%"])->orWhereRaw('lower(m.namamk) like ?', ["%{$q}%"])));
$totals = $base()->selectRaw('count(*) as n, coalesce(sum(m.sks), 0) as sks')->first();
$total = (int) $totals->n;
$levels = $this->levels($prodi);
$rows = $base()
->leftJoin('t_konsentrasi as c', 'c.idkonsentrasi', '=', 'm.idpeminatan')
->orderByDesc('p.thakad')->orderByDesc('k.idkurikulum')->orderBy('m.semester')->orderBy('m.kodemk')->orderBy('m.idmatakuliah')
->limit($limit)
->selectRaw('m.kodemk, m.namamk, m.sks, m.semester, m.tipe, j.jenis, c.namaminat, k.idprogdi, k.namakurikulum, p.thakad, b.namabagian, case when '.self::ELECTIVE_SQL.' then 1 else 0 end as pilihan')
->get()
->map(fn (object $m): array => [
'kode' => trim((string) $m->kodemk),
'nama' => trim((string) $m->namamk),
'sks' => (int) $m->sks,
'semester' => $m->semester === null || (int) $m->semester === 0 ? null : (int) $m->semester,
'jenis' => Curriculum::TYPES[(int) $m->tipe] ?? 'Kuliah',
'kelompok' => filled($m->jenis) && trim($m->jenis) !== '0' ? strtoupper(trim($m->jenis)) : null,
'sifat' => (int) $m->pilihan === 1 ? 'Pilihan' : 'Wajib',
'grup' => filled($m->namaminat) ? trim($m->namaminat) : null,
'kurikulum' => trim(substr(trim((string) $m->thakad), 0, 4).' – '.trim((string) $m->namakurikulum), ' –') ?: null,
'prodi' => $this->programLabel($levels[(int) $m->idprogdi] ?? null, $m->namabagian),
])->all();
$summary = [$this->scopeSummary($prodi, $context)];
if (filled($params['tahun'] ?? null)) {
$summary[] = 'Tahun kurikulum: '.$params['tahun'];
}
if (filled($params['tipe'] ?? null)) {
$summary[] = 'Jenis: '.(Curriculum::TYPES[(int) $params['tipe']] ?? $params['tipe']);
}
if (filled($params['kelompok'] ?? null)) {
$summary[] = 'Kelompok: '.mb_strtoupper((string) $params['kelompok']);
}
$summary[] = 'Jumlah: '.$total.' mata kuliah, '.(int) $totals->sks.' SKS';
return new ReportResult($columns, $rows, $total, $summary, 'Satu mata kuliah dihitung sekali per kurikulum; mata kuliah yang sama di beberapa kurikulum tampil per kurikulum.');
}
/** @param list<int> $prodi */
private function scopeSummary(array $prodi, ReportContext $context): string
{
$names = array_map(fn (int $id): string => $context->prodiNames[$id] ?? (string) $id, $prodi);
return 'Program Studi: '.implode(', ', $names);
}
}