[Operator] Integrasi view laman Dashboard, Mahasiswa, dan Pegawai
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Legacy;
|
||||
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Daftar pegawai DB lama untuk operator: dosen yang home base-nya prodi akun (t_dosen.idprogdi) dan pegawai
|
||||
* yang memegang jabatan aktif di prodi atau fakultas akun (t_jabatanpegawai). Hanya baca.
|
||||
*/
|
||||
final class EmployeeDirectory
|
||||
{
|
||||
/** t_pegawai.statusaktif tanpa tabel referensi; fungsi jadwal DB lama hanya menolak kode 0 (tidak aktif). */
|
||||
private const int INACTIVE = 0;
|
||||
|
||||
/** t_statuspegawai: 2 Tetap, 3 Tidak Tetap, 1 tanpa status. Pilihan 0 = kolom belum diisi. */
|
||||
private const int NO_STATUS = 0;
|
||||
|
||||
private const array STATUS_TONES = [2 => 'success', 3 => 'gold'];
|
||||
|
||||
public const array ACTIVE_OPTIONS = ['aktif' => 'Aktif', 'nonaktif' => 'Tidak aktif'];
|
||||
|
||||
/** Tidak ada kuota PA di DB lama; yang tersedia adalah dosen PA mahasiswa aktif (t_mahasiswa.idpa). */
|
||||
public const array ADVISOR_OPTIONS = ['ada' => 'Membimbing mahasiswa aktif', 'tidak' => 'Tidak membimbing mahasiswa aktif'];
|
||||
|
||||
public const array PER_PAGE = StudentDirectory::PER_PAGE;
|
||||
|
||||
private function db(): ConnectionInterface
|
||||
{
|
||||
return DB::connection('untan');
|
||||
}
|
||||
|
||||
/** @return list<int> fakultas akun (t_unituser.idfak) */
|
||||
public function faculties(int $iduser): array
|
||||
{
|
||||
return $this->db()->table('t_unituser')->where('iduser', $iduser)->whereNotNull('idfak')
|
||||
->distinct()->pluck('idfak')->map(fn (int|string $id): int => (int) $id)->values()->all();
|
||||
}
|
||||
|
||||
/** @return Collection<int, string> */
|
||||
public function statusOptions(): Collection
|
||||
{
|
||||
return $this->db()->table('t_statuspegawai')->orderBy('idstatuspegawai')
|
||||
->pluck('statuspegawai', 'idstatuspegawai')
|
||||
->mapWithKeys(fn (string $label, int|string $id): array => [(int) $id => ucfirst(trim($label))])
|
||||
->put(self::NO_STATUS, 'Belum diisi');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $prodi
|
||||
* @param list<int> $faculties
|
||||
* @param array{q?: ?string, advisor?: ?string, active?: ?string, status?: ?int} $filter
|
||||
* @return LengthAwarePaginator<int, array<string, mixed>>
|
||||
*/
|
||||
public function search(array $prodi, array $faculties, array $filter, int $perPage = self::PER_PAGE[0]): LengthAwarePaginator
|
||||
{
|
||||
$q = trim((string) ($filter['q'] ?? ''));
|
||||
$statuses = $this->statusOptions();
|
||||
|
||||
return $this->db()->table('t_pegawai as p')
|
||||
->where(fn (Builder $scope) => $scope
|
||||
->whereExists(fn (Builder $d) => $d->selectRaw('1')->from('t_dosen as d')
|
||||
->whereColumn('d.idpegawai', 'p.idpegawai')->whereIn('d.idprogdi', $prodi))
|
||||
->orWhereExists(fn (Builder $j) => $j->selectRaw('1')->from('t_jabatanpegawai as jp')
|
||||
->join('t_jabatanbagian as jb', 'jb.idjabatanbagian', '=', 'jp.idjabatanbagian')
|
||||
->whereColumn('jp.idpegawai', 'p.idpegawai')->where('jp.aktif', 1)
|
||||
->whereIn('jb.idbagian', [...$prodi, ...$faculties])))
|
||||
->when($q !== '', fn (Builder $query) => $query->where(fn (Builder $w) => $w
|
||||
->whereRaw('upper(p.nip) like ?', ['%'.mb_strtoupper($q).'%'])
|
||||
->orWhereRaw('upper(p.nama) like ?', ['%'.mb_strtoupper($q).'%'])
|
||||
->orWhere('p.nidn', 'like', '%'.$q.'%')))
|
||||
->when($filter['advisor'] ?? null, function (Builder $query, string $advisor): void {
|
||||
$query->{$advisor === 'ada' ? 'whereExists' : 'whereNotExists'}(fn (Builder $m) => $m->selectRaw('1')
|
||||
->from('t_mahasiswa as m')->whereColumn('m.idpa', 'p.idpegawai')->whereIn('m.stataktif', StudentDirectory::STATUS['aktif']));
|
||||
})
|
||||
->when($filter['active'] ?? null, fn (Builder $query, string $active) => $active === 'nonaktif'
|
||||
? $query->where('p.statusaktif', self::INACTIVE)
|
||||
: $query->whereRaw('coalesce(p.statusaktif, 1) <> ?', [self::INACTIVE]))
|
||||
->when($filter['status'] ?? null, fn (Builder $query, int $status) => $query->where('p.idstatuspegawai', $status))
|
||||
->when(($filter['status'] ?? null) === self::NO_STATUS, fn (Builder $query) => $query->whereNull('p.idstatuspegawai'))
|
||||
->orderBy('p.nama')->orderBy('p.idpegawai')
|
||||
->select('p.idpegawai', 'p.nip', 'p.nama', 'p.gelardepan', 'p.gelarbelakang', 'p.kelamin', 'p.nidn', 'p.hp', 'p.telepon', 'p.email', 'p.idstatuspegawai')
|
||||
->paginate($perPage)
|
||||
->through(fn (object $p): array => $this->present($p, $statuses));
|
||||
}
|
||||
|
||||
/** @param Collection<int, string> $statuses */
|
||||
private function present(object $p, Collection $statuses): array
|
||||
{
|
||||
$filled = fn (?string $value): ?string => ($value = trim((string) $value)) === '' ? null : $value;
|
||||
$suffix = $filled($p->gelarbelakang);
|
||||
$status = $p->idstatuspegawai === null ? self::NO_STATUS : (int) $p->idstatuspegawai;
|
||||
|
||||
return [
|
||||
'id' => (int) $p->idpegawai,
|
||||
'nip' => $filled($p->nip),
|
||||
'nama' => trim(($filled($p->gelardepan) ?? '').' '.trim($p->nama)).($suffix === null ? '' : ', '.ltrim($suffix, ', ')),
|
||||
'kelamin' => [1 => 'L', 2 => 'P'][(int) $p->kelamin] ?? null,
|
||||
'nidn' => $filled($p->nidn),
|
||||
'telepon' => $filled($p->hp) ?? $filled($p->telepon),
|
||||
'email' => $filled($p->email),
|
||||
'status' => ['label' => $statuses[$status] ?? 'Belum diisi', 'tone' => self::STATUS_TONES[$status] ?? 'neutral'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user