91 lines
3.1 KiB
PHP
91 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Models\Department;
|
||
|
|
use App\Models\Position;
|
||
|
|
use App\Models\TrainingMatrix;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\QuestionBank;
|
||
|
|
|
||
|
|
class ExamManagementController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Menampilkan halaman Bank Soal dengan Filter Komprehensif
|
||
|
|
*/
|
||
|
|
public function questionBank(Request $request)
|
||
|
|
{
|
||
|
|
// 1. Siapkan Data untuk Dropdown Filter
|
||
|
|
$departments = Department::orderBy('name', 'asc')->get();
|
||
|
|
$positions = Position::orderBy('name', 'asc')->get();
|
||
|
|
// $matrices = TrainingMatrix::orderBy('title', 'asc')->get();
|
||
|
|
|
||
|
|
// Ambil daftar user yang pernah membuat soal (untuk filter 'Pembuat')
|
||
|
|
// Sesuaikan nama relasi/kolom jika berbeda
|
||
|
|
$creators = User::whereHas('roles', function($q){
|
||
|
|
$q->whereIn('name', ['admin', 'trainer']);
|
||
|
|
})->orderBy('first_name', 'asc')->get();
|
||
|
|
|
||
|
|
// 2. Query Utama dengan Relasi
|
||
|
|
$query = QuestionBank::with(['department', 'position', 'creator']);
|
||
|
|
|
||
|
|
// 3. Logika Filter Dinamis
|
||
|
|
if ($request->filled('search')) {
|
||
|
|
$query->where('question_text', 'like', '%' . $request->search . '%');
|
||
|
|
}
|
||
|
|
if ($request->filled('department_id')) {
|
||
|
|
$query->where('department_id', $request->department_id);
|
||
|
|
}
|
||
|
|
if ($request->filled('position_id')) {
|
||
|
|
$query->where('position_id', $request->position_id);
|
||
|
|
}
|
||
|
|
// if ($request->filled('matrix_id')) {
|
||
|
|
// $query->where('training_matrix_id', $request->matrix_id);
|
||
|
|
// }
|
||
|
|
if ($request->filled('question_type')) {
|
||
|
|
$query->where('type', $request->question_type);
|
||
|
|
}
|
||
|
|
if ($request->filled('question_level')) {
|
||
|
|
$query->where('level', $request->question_level);
|
||
|
|
}
|
||
|
|
if ($request->filled('created_by')) {
|
||
|
|
$query->where('created_by', $request->created_by);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. Eksekusi Query dengan Pagination
|
||
|
|
$questions = $query->latest()->paginate(15)->withQueryString();
|
||
|
|
$totalQuestions = $questions->total();
|
||
|
|
|
||
|
|
return view('pages.admin.exams.question-bank', compact(
|
||
|
|
'questions', 'totalQuestions', 'departments', 'positions', 'creators'
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Menghapus banyak soal sekaligus (Bulk Delete)
|
||
|
|
*/
|
||
|
|
public function bulkDelete(Request $request)
|
||
|
|
{
|
||
|
|
$request->validate([
|
||
|
|
'question_ids' => 'required|array',
|
||
|
|
'question_ids.*' => 'exists:questions,id'
|
||
|
|
]);
|
||
|
|
|
||
|
|
try {
|
||
|
|
Question::whereIn('id', $request->question_ids)->delete();
|
||
|
|
return back()->with('success', count($request->question_ids) . ' Soal berhasil dihapus secara massal.');
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
return back()->with('error', 'Gagal menghapus soal: ' . $e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Menampilkan Halaman Import Soal
|
||
|
|
*/
|
||
|
|
public function importView()
|
||
|
|
{
|
||
|
|
return view('pages.admin.exams.import-questions');
|
||
|
|
}
|
||
|
|
}
|