144 lines
6.7 KiB
PHP
144 lines
6.7 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Operator;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\Legacy\Account;
|
||
use App\Support\Legacy\AcademicOverview;
|
||
use App\Support\Legacy\Consultations;
|
||
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;
|
||
use Illuminate\Support\Collection;
|
||
|
||
/**
|
||
* Portal > Konsultasi (slug list_konsultasi) untuk akun DB lama: percakapan pembimbing–mahasiswa asli (x_message)
|
||
* prodi akun, format tabel SATU. Filter/halaman lewat data() (JSON); Hapus = hapus lunak; Lihat = percakapan.
|
||
*/
|
||
class KonsultasiController extends Controller
|
||
{
|
||
private const string SLUG = 'list_konsultasi';
|
||
|
||
public function __construct(
|
||
private readonly AcademicOverview $overview,
|
||
private readonly Consultations $consultations,
|
||
) {}
|
||
|
||
public function __invoke(Request $request): View
|
||
{
|
||
$page = Catalog::page(self::SLUG);
|
||
$prodi = $this->overview->programs((int) $request->user()->getKey())->keys()->all();
|
||
$periods = $this->consultations->periods($prodi);
|
||
$default = $periods->keys()->first();
|
||
|
||
return view('operator.satu.remote-list', [
|
||
'page' => $page,
|
||
'crumbs' => ['Beranda' => route('operator.dashboard')] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => null],
|
||
'note' => 'Konsultasi pembimbing asli dari SIAKAD untuk program studi yang Anda kelola (pesan pembimbing akademik). Topik diambil dari awal pesan pertama; status tidak tercatat di sumber data.',
|
||
'empty' => $prodi === []
|
||
? ['icon' => 'building-library', 'title' => 'Akun belum terhubung ke program studi', 'description' => 'Daftar konsultasi tampil setelah akun Anda dipetakan ke program studi oleh Pusat Komputer.']
|
||
: ['icon' => 'chat-bubble-left-right', 'title' => 'Belum ada konsultasi', 'description' => 'Belum ada pesan pembimbing untuk mahasiswa program studi Anda.'],
|
||
'url' => route('operator.konsultasi.data'),
|
||
'viewUrl' => route('operator.konsultasi.show', '__ID__'),
|
||
'destroyUrl' => route('operator.konsultasi.destroy'),
|
||
'destroy' => ['title' => 'Hapus konsultasi terpilih?', 'message' => 'Seluruh pesan pada percakapan yang dipilih disembunyikan dari daftar. Pusat Komputer dapat memulihkannya kembali.'],
|
||
'filters' => [
|
||
['name' => 'periode', 'label' => $page['filters'][0], 'options' => $periods->all(), 'value' => $default],
|
||
],
|
||
'columns' => [
|
||
['key' => 'nim', 'label' => 'NIM'],
|
||
['key' => 'nama', 'label' => 'Nama Mahasiswa'],
|
||
['key' => 'nip', 'label' => 'NIP'],
|
||
['key' => 'pembimbing', 'label' => 'Nama Pembimbing'],
|
||
['key' => 'topik', 'label' => 'Topik'],
|
||
['key' => 'terakhir', 'label' => 'Post Terakhir'],
|
||
['key' => 'krs', 'label' => 'KRS Disetujui', 'badge' => true],
|
||
['key' => 'status', 'label' => 'Status'],
|
||
],
|
||
'perPage' => Consultations::PER_PAGE,
|
||
'data' => $default === null ? null : $this->payload($this->search($request, $prodi, $periods, $default)),
|
||
]);
|
||
}
|
||
|
||
public function data(Request $request): JsonResponse
|
||
{
|
||
[$prodi, $periods] = $this->scope($request);
|
||
|
||
return response()->json($this->payload($this->search($request, $prodi, $periods, $periods->keys()->first())));
|
||
}
|
||
|
||
/** Percakapan satu pasangan; `$pasangan` = "idmhs-iddosen" (id baris tabel). */
|
||
public function show(Request $request, string $pasangan): View
|
||
{
|
||
abort_unless($request->user() instanceof Account, 404);
|
||
[$mahasiswa, $pembimbing] = array_map(intval(...), explode('-', $pasangan));
|
||
$prodi = $this->overview->programs((int) $request->user()->getKey())->keys()->all();
|
||
$thread = $this->consultations->thread($prodi, $mahasiswa, $pembimbing) ?? abort(404);
|
||
$page = Catalog::page(self::SLUG);
|
||
|
||
return view('operator.satu.konsultasi-detail', [
|
||
'page' => $page,
|
||
'crumbs' => ['Beranda' => route('operator.dashboard')] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => route('operator.halaman', self::SLUG), 'Percakapan' => null],
|
||
'thread' => $thread,
|
||
'back' => route('operator.halaman', self::SLUG),
|
||
]);
|
||
}
|
||
|
||
/** Hapus lunak pasangan terpilih; JSON agar daftar dimuat ulang di tempat. */
|
||
public function destroy(Request $request): JsonResponse
|
||
{
|
||
[$prodi] = $this->scope($request);
|
||
$ids = $request->validate(['ids' => ['required', 'array', 'max:100'], 'ids.*' => ['string', 'regex:/^\d+-\d+$/']])['ids'];
|
||
|
||
try {
|
||
$count = $this->consultations->softDelete($prodi, $ids);
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json(['message' => $count === 0 ? 'Tidak ada konsultasi yang dihapus.' : "{$count} pesan konsultasi dihapus.", 'count' => $count]);
|
||
}
|
||
|
||
/**
|
||
* Hanya akun DB lama yang memegang prodi dengan data konsultasi.
|
||
*
|
||
* @return array{0: list<int>, 1: Collection<int, string>}
|
||
*/
|
||
private function scope(Request $request): array
|
||
{
|
||
abort_unless($request->user() instanceof Account, 404);
|
||
$prodi = $this->overview->programs((int) $request->user()->getKey())->keys()->all();
|
||
$periods = $this->consultations->periods($prodi);
|
||
abort_if($periods->isEmpty(), 404);
|
||
|
||
return [$prodi, $periods];
|
||
}
|
||
|
||
/**
|
||
* @param list<int> $prodi
|
||
* @param Collection<int, string> $periods
|
||
*/
|
||
private function search(Request $request, array $prodi, Collection $periods, int $default): LengthAwarePaginator
|
||
{
|
||
$periode = $request->filled('periode') && $periods->has($request->integer('periode')) ? $request->integer('periode') : $default;
|
||
$perPage = in_array($request->integer('per'), Consultations::PER_PAGE, true) ? $request->integer('per') : Consultations::PER_PAGE[0];
|
||
|
||
return $this->consultations->search($prodi, $periode, mb_substr(trim((string) $request->string('q')), 0, 100), $perPage);
|
||
}
|
||
|
||
/** @return array{rows: list<array<string, mixed>>, page: int, last_page: int, total: int, per_page: int} */
|
||
private function payload(LengthAwarePaginator $rows): array
|
||
{
|
||
return [
|
||
'rows' => array_values($rows->items()),
|
||
'page' => $rows->currentPage(),
|
||
'last_page' => $rows->lastPage(),
|
||
'total' => $rows->total(),
|
||
'per_page' => $rows->perPage(),
|
||
];
|
||
}
|
||
}
|