[Operator] Perbaikan Laman Kegiatan
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Legacy;
|
||||
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Tulis kegiatan kalender akademik DB lama (t_kalender_kegiatan) untuk Portal > Kegiatan > Kalender Akademik. Satu aksi
|
||||
* membuat kegiatan sekaligus menetapkan TMT (tanggal mulai) dan TST (tanggal selesai) pada sebuah periode: nama kegiatan
|
||||
* dicocokkan dengan jenis kegiatan yang ada (tanpa memandang huruf besar/kecil) atau dibuat sebagai jenis baru bersama
|
||||
* kode dan warnanya (ActivityTypes), lalu kegiatan tampil di daftar kalender dan Monitoring Kalender periode itu. Tiap kegiatan milik satu unit (t_kalender_kegiatan
|
||||
* .idbagian): operator hanya menambah untuk unitnya (fakultas dan program studi akun) dan hanya mengubah atau menghapus
|
||||
* kegiatan unitnya; kegiatan pusat (mis. Rektorat) tidak disentuh.
|
||||
*/
|
||||
final class CalendarEvents
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AcademicCalendar $calendar,
|
||||
private readonly ActivityTypes $types,
|
||||
) {}
|
||||
|
||||
private function db(): ConnectionInterface
|
||||
{
|
||||
return DB::connection('untan');
|
||||
}
|
||||
|
||||
/**
|
||||
* Unit akun: fakultas lalu program studi dari t_unituser, beserta namanya.
|
||||
*
|
||||
* @return array<int, string> idbagian => nama
|
||||
*/
|
||||
public function units(int $iduser): array
|
||||
{
|
||||
$rows = $this->db()->table('t_unituser')->where('iduser', $iduser)->get(['idfak', 'idprogdi']);
|
||||
$ids = $rows->pluck('idfak')->merge($rows->pluck('idprogdi'))->filter()->map(fn ($id): int => (int) $id)->unique()->values();
|
||||
$names = $this->db()->table('t_bagian')->whereIn('idbagian', $ids->all())->pluck('namabagian', 'idbagian');
|
||||
|
||||
return $ids->mapWithKeys(fn (int $id): array => [$id => trim((string) ($names[$id] ?? '')) ?: 'Unit '.$id])->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $units
|
||||
* @param array{periode: mixed, nama: mixed, kode?: mixed, warna?: mixed, unit: mixed, mulai: mixed, selesai: mixed, keterangan?: mixed} $input periode wajib (pengendali mengisi periode aktif bila filter kosong)
|
||||
* @return int idkegiatan baru
|
||||
*/
|
||||
public function create(array $units, array $input, string $actor, ?string $ip): int
|
||||
{
|
||||
$values = $this->values($units, $input);
|
||||
|
||||
// Jenis kegiatan baru tidak boleh tertinggal bila penyimpanan kegiatannya gagal.
|
||||
return $this->db()->transaction(fn (): int => (int) $this->db()->table('t_kalender_kegiatan')->insertGetId(
|
||||
$values + ['idjeniskegiatan' => $this->resolveType($input, $actor, $ip)] + $this->audit($actor, $ip),
|
||||
'idkegiatan',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $units
|
||||
* @param array<string, mixed> $input
|
||||
*/
|
||||
public function update(array $units, int $id, array $input, string $actor, ?string $ip): void
|
||||
{
|
||||
$current = $this->owned($units, $id);
|
||||
// Periode kosong: kegiatan tetap di periodenya.
|
||||
$input['periode'] = filled($input['periode'] ?? null) ? $input['periode'] : $current->idperiodekuliah;
|
||||
|
||||
$values = $this->values($units, $input);
|
||||
|
||||
$this->db()->transaction(fn () => $this->db()->table('t_kalender_kegiatan')->where('idkegiatan', $id)
|
||||
->update($values + ['idjeniskegiatan' => $this->resolveType($input, $actor, $ip)] + $this->audit($actor, $ip)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Jenis kegiatan dari nama: yang sudah ada dipakai (kode dan warna yang diisi dan berbeda memperbarui jenis itu),
|
||||
* kalau belum ada dibuat. Kode dan warna kosong tidak mengubah apa pun.
|
||||
*
|
||||
* @param array<string, mixed> $input
|
||||
*/
|
||||
private function resolveType(array $input, string $actor, ?string $ip): int
|
||||
{
|
||||
$name = trim((string) $input['nama']);
|
||||
$code = trim((string) ($input['kode'] ?? ''));
|
||||
$color = strtoupper(trim((string) ($input['warna'] ?? '')));
|
||||
$existing = $this->types->all()->first(fn (array $type): bool => mb_strtolower($type['nama']) === mb_strtolower($name));
|
||||
|
||||
if ($existing === null) {
|
||||
return $this->types->create(['nama' => $name, 'kode' => $code, 'warna' => $color], $actor, $ip);
|
||||
}
|
||||
|
||||
$newCode = $code !== '' && $code !== $existing['kode'] ? $code : null;
|
||||
$newColor = $color !== '' && $color !== ($existing['warna'] ?? '') ? $color : null;
|
||||
|
||||
if ($newCode !== null || $newColor !== null) {
|
||||
$this->types->update($existing['id'], ['nama' => $existing['nama'], 'kode' => $newCode ?? $existing['kode'], 'warna' => $newColor ?? $existing['warna']], $actor, $ip);
|
||||
}
|
||||
|
||||
return $existing['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Hapus kegiatan. Gagal seluruhnya bila ada yang milik unit lain atau tidak ada.
|
||||
*
|
||||
* @param array<int, string> $units
|
||||
* @param list<int> $ids
|
||||
*/
|
||||
public function delete(array $units, array $ids): int
|
||||
{
|
||||
$ids = array_values(array_unique(array_map('intval', $ids)));
|
||||
$owned = $this->db()->table('t_kalender_kegiatan')->whereIn('idkegiatan', $ids)->whereIn('idbagian', array_keys($units))->pluck('idkegiatan');
|
||||
|
||||
if ($owned->isEmpty() || $owned->count() !== count($ids)) {
|
||||
throw new PortalException($owned->isEmpty()
|
||||
? 'Kegiatan tidak ditemukan.'
|
||||
: 'Sebagian kegiatan milik unit lain (mis. pusat) dan tidak dapat dihapus. Tidak ada yang dihapus.');
|
||||
}
|
||||
|
||||
return $this->db()->table('t_kalender_kegiatan')->whereIn('idkegiatan', $ids)->delete();
|
||||
}
|
||||
|
||||
/** @param array<int, string> $units */
|
||||
private function owned(array $units, int $id): object
|
||||
{
|
||||
$event = $this->db()->table('t_kalender_kegiatan')->where('idkegiatan', $id)->first(['idbagian', 'idperiodekuliah']);
|
||||
|
||||
if ($event === null) {
|
||||
throw new PortalException('Kegiatan tidak ditemukan.');
|
||||
}
|
||||
if (! isset($units[(int) $event->idbagian])) {
|
||||
throw new PortalException('Kegiatan milik unit lain (mis. pusat) dan tidak dapat diubah.');
|
||||
}
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nilai kolom yang sudah diperiksa terhadap unit akun, periode, jenis kegiatan, dan rentang tanggal.
|
||||
*
|
||||
* @param array<int, string> $units
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function values(array $units, array $input): array
|
||||
{
|
||||
$unit = (int) ($input['unit'] ?? 0);
|
||||
if (! isset($units[$unit])) {
|
||||
throw new PortalException('Unit harus dipilih dari fakultas atau program studi Anda.');
|
||||
}
|
||||
$period = (int) ($input['periode'] ?? 0);
|
||||
if (! $this->calendar->periods()->has($period)) {
|
||||
throw new PortalException('Periode akademik tidak ditemukan.');
|
||||
}
|
||||
if (trim((string) ($input['nama'] ?? '')) === '') {
|
||||
throw new PortalException('Nama kegiatan wajib diisi.');
|
||||
}
|
||||
|
||||
try {
|
||||
$start = Carbon::createFromFormat('Y-m-d', (string) ($input['mulai'] ?? ''))->startOfDay();
|
||||
$end = Carbon::createFromFormat('Y-m-d', (string) ($input['selesai'] ?? ''))->startOfDay();
|
||||
} catch (\Throwable) {
|
||||
throw new PortalException('Tanggal mulai dan selesai wajib diisi dengan benar.');
|
||||
}
|
||||
if ($end->lt($start)) {
|
||||
throw new PortalException('Tanggal selesai tidak boleh sebelum tanggal mulai.');
|
||||
}
|
||||
|
||||
return [
|
||||
'idperiodekuliah' => $period,
|
||||
'idbagian' => $unit,
|
||||
'mulai' => $start,
|
||||
'selesai' => $end,
|
||||
'deskripsi' => mb_substr(trim((string) ($input['keterangan'] ?? '')), 0, 255),
|
||||
];
|
||||
}
|
||||
|
||||
/** Kolom audit seperti aplikasi lama: cname nama aplikasi, cip alamat IP, uname username (dipotong 20). */
|
||||
private function audit(string $actor, ?string $ip): array
|
||||
{
|
||||
return ['cname' => ActivityTypes::APP_NAME, 'cip' => mb_substr((string) $ip, 0, 20), 'uname' => mb_substr($actor, 0, 20), 'tanggal' => now()];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user