129 lines
5.9 KiB
PHP
129 lines
5.9 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\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). Hanya lihat: daftar dan riwayat percakapan (Lihat); tanpa
|
||
* hapus, kotak centang, dan kolom status.
|
||
*/
|
||
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 = $this->consultations->defaultPeriod($prodi, $periods);
|
||
|
||
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. Pilih Lihat untuk membuka riwayat percakapan.',
|
||
'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__'),
|
||
// Hanya lihat: tanpa tombol bilah (Hapus) dan tanpa kotak centang.
|
||
'tools' => [],
|
||
'selectable' => false,
|
||
'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],
|
||
],
|
||
'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, $this->consultations->defaultPeriod($prodi, $periods))));
|
||
}
|
||
|
||
/** 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),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 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(),
|
||
];
|
||
}
|
||
}
|