171 lines
8.6 KiB
PHP
171 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Operator;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Operator\Concerns\ServesLegacyList;
|
|
use App\Support\Legacy\AcademicOverview;
|
|
use App\Support\Legacy\Curriculum;
|
|
use App\Support\Legacy\PortalException;
|
|
use App\Support\Prototype\Satu\Catalog;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
/**
|
|
* Perkuliahan > Manajemen Kurikulum > Mata Kuliah (slug list_matakuliah) untuk akun DB lama: t_matakuliah prodi akun;
|
|
* tambah/ubah lewat modal dan hapus (per baris atau massal) langsung ke DB lama.
|
|
*/
|
|
class MataKuliahController extends Controller
|
|
{
|
|
use ServesLegacyList;
|
|
|
|
private const string SLUG = 'list_matakuliah';
|
|
|
|
public function __construct(
|
|
private readonly AcademicOverview $overview,
|
|
private readonly Curriculum $curriculum,
|
|
) {}
|
|
|
|
public function __invoke(Request $request): View
|
|
{
|
|
$page = Catalog::page(self::SLUG);
|
|
$programs = $this->overview->programs((int) $request->user()->getKey());
|
|
$prodi = $programs->keys()->all();
|
|
[$labelTahun, $labelJenis, $labelProdi, $labelKelompok] = $page['filters'];
|
|
|
|
return view('operator.satu.remote-list', [
|
|
'page' => $page,
|
|
'crumbs' => $this->crumbs(self::SLUG, $page),
|
|
'note' => 'Mata kuliah asli kurikulum program studi yang Anda kelola (t_matakuliah). Tambah, ubah, dan hapus langsung tersimpan di basis data; mata kuliah yang sudah dipakai kelas, KRS, atau transkrip tidak bisa dihapus. Status pengisian RPS/CPL/CPMK tidak tercatat di sumber data.',
|
|
'empty' => ['icon' => 'building-library', 'title' => 'Akun belum terhubung ke program studi', 'description' => 'Daftar mata kuliah tampil setelah akun Anda dipetakan ke program studi oleh Pusat Komputer.'],
|
|
'url' => route('operator.matakuliah.data'),
|
|
'filters' => [
|
|
['name' => 'tahun', 'label' => $labelTahun, 'options' => $this->curriculum->years($prodi)->all()],
|
|
['name' => 'tipe', 'label' => $labelJenis, 'options' => Curriculum::TYPES],
|
|
['name' => 'prodi', 'label' => $labelProdi, 'options' => $programs->all()],
|
|
['name' => 'kelompok', 'label' => $labelKelompok, 'options' => $this->curriculum->groups($prodi)->all()],
|
|
],
|
|
'columns' => [
|
|
['key' => 'kurikulum', 'label' => 'Kurikulum'],
|
|
['key' => 'kode', 'label' => 'Kode MK'],
|
|
['key' => 'nama', 'label' => 'Nama Mata Kuliah'],
|
|
['key' => 'sks', 'label' => 'SKS', 'num' => true],
|
|
['key' => 'jenis', 'label' => 'Jenis MK'],
|
|
['key' => 'prodi', 'label' => 'Prodi Pengampu'],
|
|
['key' => 'status', 'label' => 'Status Pengisian'],
|
|
],
|
|
'perPage' => Curriculum::PER_PAGE,
|
|
'data' => $prodi === [] ? null : $this->payload($this->search($request, $prodi)),
|
|
'form' => $prodi === [] ? null : [
|
|
'title' => 'mata kuliah',
|
|
'storeUrl' => route('operator.matakuliah.store'),
|
|
'updateUrl' => route('operator.matakuliah.update', '__ID__'),
|
|
'fields' => [
|
|
['name' => 'kurikulum', 'label' => 'Kurikulum', 'type' => 'select', 'options' => $this->curriculum->curricula($prodi)->all(), 'required' => true],
|
|
['name' => 'jenis', 'label' => 'Kelompok mata kuliah', 'type' => 'select', 'options' => $this->curriculum->groupOptions($prodi)->all(), 'required' => true, 'hint' => 'Pilih kelompok dari kurikulum yang sama.'],
|
|
['name' => 'kode', 'label' => 'Kode MK', 'type' => 'text', 'required' => true, 'max' => 15],
|
|
['name' => 'nama', 'label' => 'Nama mata kuliah', 'type' => 'text', 'required' => true, 'max' => 50],
|
|
['name' => 'sks', 'label' => 'SKS', 'type' => 'number', 'required' => true, 'min' => 0, 'max' => 24, 'default' => 2],
|
|
['name' => 'semester', 'label' => 'Semester', 'type' => 'number', 'min' => 1, 'max' => 14],
|
|
['name' => 'tipe', 'label' => 'Jenis MK', 'type' => 'select', 'options' => Curriculum::TYPES, 'required' => true, 'default' => 0],
|
|
['name' => 'nilailulus', 'label' => 'Nilai lulus minimal', 'type' => 'select', 'options' => Curriculum::PASS_GRADES, 'required' => true, 'default' => 2],
|
|
],
|
|
],
|
|
'destroyUrl' => $prodi === [] ? null : route('operator.matakuliah.destroy'),
|
|
'destroy' => ['title' => 'Hapus mata kuliah terpilih?', 'message' => 'Mata kuliah akan dihapus dari kurikulum. Yang sudah dipakai kelas, KRS, atau transkrip akan ditolak.'],
|
|
]);
|
|
}
|
|
|
|
public function data(Request $request): JsonResponse
|
|
{
|
|
$prodi = $this->prodiOrFail($request);
|
|
|
|
return response()->json($this->payload($this->search($request, $prodi)));
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$prodi = $this->prodiOrFail($request);
|
|
$input = $this->validated($request);
|
|
|
|
try {
|
|
$id = $this->curriculum->createCourse($prodi, $input, (string) $request->user()->username, $request->ip());
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => 'Mata kuliah ditambahkan.', 'id' => $id], 201);
|
|
}
|
|
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
$prodi = $this->prodiOrFail($request);
|
|
$input = $this->validated($request);
|
|
|
|
try {
|
|
$this->curriculum->updateCourse($prodi, $id, $input, (string) $request->user()->username, $request->ip());
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => 'Mata kuliah diperbarui.']);
|
|
}
|
|
|
|
/** Hapus satu atau beberapa mata kuliah (POST {ids}). */
|
|
public function destroy(Request $request): JsonResponse
|
|
{
|
|
$prodi = $this->prodiOrFail($request);
|
|
$ids = $request->validate(['ids' => ['required', 'array', 'min:1'], 'ids.*' => ['integer']], [], ['ids' => 'mata kuliah'])['ids'];
|
|
|
|
try {
|
|
$deleted = $this->curriculum->deleteCourses($prodi, $ids);
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => "{$deleted} mata kuliah dihapus."]);
|
|
}
|
|
|
|
/** @return list<int> */
|
|
private function prodiOrFail(Request $request): array
|
|
{
|
|
$this->legacyOnly($request);
|
|
$prodi = $this->overview->programs((int) $request->user()->getKey())->keys()->all();
|
|
abort_if($prodi === [], 404);
|
|
|
|
return $prodi;
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
private function validated(Request $request): array
|
|
{
|
|
return $request->validate([
|
|
'kurikulum' => ['required', 'integer'],
|
|
'jenis' => ['required', 'integer'],
|
|
'kode' => ['required', 'string', 'max:15'],
|
|
'nama' => ['required', 'string', 'max:50'],
|
|
'sks' => ['required', 'integer', 'between:0,24'],
|
|
'semester' => ['nullable', 'integer', 'between:0,14'],
|
|
'tipe' => ['required', 'integer', 'in:'.implode(',', array_keys(Curriculum::TYPES))],
|
|
'nilailulus' => ['nullable', 'integer', 'in:'.implode(',', array_keys(Curriculum::PASS_GRADES))],
|
|
], [], [
|
|
'kurikulum' => 'kurikulum', 'jenis' => 'kelompok mata kuliah', 'kode' => 'kode MK', 'nama' => 'nama mata kuliah',
|
|
'sks' => 'SKS', 'semester' => 'semester', 'tipe' => 'jenis MK', 'nilailulus' => 'nilai lulus minimal',
|
|
]);
|
|
}
|
|
|
|
/** @param list<int> $prodi */
|
|
private function search(Request $request, array $prodi): LengthAwarePaginator
|
|
{
|
|
return $this->curriculum->courses($prodi, [
|
|
'tahun' => preg_match('/^\d{4}$/', (string) $request->string('tahun')) ? (string) $request->string('tahun') : null,
|
|
'tipe' => $request->filled('tipe') && isset(Curriculum::TYPES[$request->integer('tipe')]) ? $request->integer('tipe') : null,
|
|
'prodi' => $request->filled('prodi') && in_array($request->integer('prodi'), $prodi, true) ? $request->integer('prodi') : null,
|
|
'kelompok' => preg_match('/^[A-Za-z0-9 _-]{1,20}$/', (string) $request->string('kelompok')) ? (string) $request->string('kelompok') : null,
|
|
'q' => $this->query($request),
|
|
], $this->perPage($request, Curriculum::PER_PAGE));
|
|
}
|
|
}
|