121 lines
4.2 KiB
PHP
121 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\LogActivity;
|
|
use App\Models\MappingUnitKerjaPegawai;
|
|
use App\Models\FileDirectory;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class LogActivityController extends Controller
|
|
{
|
|
public function index(){
|
|
$data =[
|
|
'title' => 'Log Aktivitas'
|
|
];
|
|
return view('logActivity.index', $data);
|
|
}
|
|
|
|
public function datatable(){
|
|
$perPage = (int) request('per_page', 10);
|
|
$keyword = request('keyword');
|
|
$start = request('start_date');
|
|
$end = request('end_date');
|
|
$mapping = MappingUnitKerjaPegawai::where('statusenabled', true)
|
|
->where('objectpegawaifk', auth()->user()->dataUser->id)
|
|
->get(['objectunitkerjapegawaifk', 'objectsubunitkerjapegawaifk']);
|
|
$unitIds = $mapping->pluck('objectunitkerjapegawaifk')
|
|
->filter() // buang null
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
$query = FileDirectory::withCount(['viewLogs as total_views' => function($q){
|
|
$q->select(DB::raw('COUNT(DISTINCT pegawai_id_entry)'));
|
|
}])
|
|
->where('statusenabled', true)
|
|
->where('status_action', 'approved')
|
|
->whereIn('id_unit_kerja', $unitIds)
|
|
->orderBy('entry_at','desc');
|
|
|
|
if($keyword){
|
|
$query->where(function($q) use ($keyword){
|
|
$q->where('file', 'ILIKE', "%{$keyword}%")
|
|
->orWhere('no_dokumen', 'ILIKE', "%{$keyword}%");
|
|
});
|
|
}
|
|
|
|
if($start){
|
|
$query->whereDate('entry_at','>=',$start);
|
|
}
|
|
if($end){
|
|
$query->whereDate('entry_at','<=',$end);
|
|
}
|
|
|
|
$paginated = $query->paginate($perPage);
|
|
$data = $paginated->getCollection()->map(function($item){
|
|
$parts = array_values(array_filter(explode('/', $item->file)));
|
|
return [
|
|
'id' => $item->file_directory_id,
|
|
'file' => end($parts),
|
|
'no_dokumen' => $item->no_dokumen ?? '-',
|
|
'unit' => $parts[0],
|
|
'sub_unit' => $parts[1],
|
|
'kategori' => $parts[2],
|
|
'entry_at' => $item->entry_at,
|
|
'pengunggah' => $item->pegawai_nama_entry,
|
|
'total_views' => $item->total_views ?? 0,
|
|
];
|
|
});
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => $data,
|
|
'pagination' => [
|
|
'current_page' => $paginated->currentPage(),
|
|
'next_page' => $paginated->hasMorePages() ? $paginated->currentPage() + 1 : null,
|
|
'has_more' => $paginated->hasMorePages(),
|
|
'last_page' => $paginated->lastPage(),
|
|
'per_page' => $paginated->perPage(),
|
|
'total' => $paginated->total(),
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function detailByFile($fileDirectoryId)
|
|
{
|
|
$perPage = max(1, (int) request('per_page', 10));
|
|
$keyword = request('keyword');
|
|
|
|
$query = LogActivity::select(
|
|
'pegawai_id_entry',
|
|
'pegawai_nama_entry',
|
|
DB::raw('COUNT(*) as total_open'),
|
|
DB::raw('MAX(entry_at) as last_open')
|
|
)
|
|
->where('file_directory_id', $fileDirectoryId)
|
|
->where('statusenabled', true)
|
|
->where('action_type', 'Membuka Dokumen')
|
|
->groupBy('pegawai_id_entry', 'pegawai_nama_entry')
|
|
->orderByDesc('total_open');
|
|
|
|
if($keyword){
|
|
$query->havingRaw('pegawai_nama_entry ILIKE ?', ["%{$keyword}%"]);
|
|
}
|
|
|
|
$paginated = $query->paginate($perPage);
|
|
$logs = $paginated->items();
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => $logs,
|
|
'pagination' => [
|
|
'current_page' => $paginated->currentPage(),
|
|
'last_page' => $paginated->lastPage(),
|
|
'per_page' => $paginated->perPage(),
|
|
'total' => $paginated->total(),
|
|
]
|
|
]);
|
|
}
|
|
}
|