Files
lms-v2/app/Http/Controllers/Admin/ExamManagementController.php
T

112 lines
4.0 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\Subject; // PASTIKAN SUBJECT DI-IMPORT
use App\Models\User;
use App\Models\QuestionBank;
class ExamManagementController extends Controller
{
public function questionBank(Request $request)
{
$departments = Department::orderBy('name', 'asc')->get();
$positions = Position::orderBy('name', 'asc')->get();
$subjects = Subject::orderBy('name', 'asc')->get(); // Mengambil data Subject
$creators = User::whereHas('roles', function($q){
$q->whereIn('name', ['admin', 'trainer']);
})->orderBy('first_name', 'asc')->get();
// Load relasi subject, department, position, dan creator
$query = QuestionBank::with(['subject', 'department', 'position', 'creator']);
// Logika Filter
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('subject_id')) { // Filter berdasarkan Subject
$query->where('subject_id', $request->subject_id);
}
if ($request->filled('question_type')) {
$query->where('question_type', $request->question_type);
}
if ($request->filled('question_level')) {
$query->where('question_level', $request->question_level);
}
if ($request->filled('created_by')) {
$query->where('created_by', $request->created_by);
}
$questions = $query->latest()->paginate(15)->withQueryString();
$totalQuestions = $questions->total();
return view('pages.admin.exams.question-bank', compact(
'questions', 'totalQuestions', 'departments', 'positions', 'subjects', 'creators'
));
}
// Fungsi untuk menampilkan halaman Tambah Soal
public function create()
{
$departments = Department::orderBy('name', 'asc')->get();
$positions = Position::orderBy('name', 'asc')->get();
$subjects = Subject::orderBy('name', 'asc')->get(); // Kirim Subject ke form
return view('pages.admin.exams.create', compact('departments', 'positions', 'subjects'));
}
// Fungsi untuk menampilkan halaman Edit Soal
public function edit($id)
{
$question = QuestionBank::findOrFail($id);
$departments = Department::orderBy('name', 'asc')->get();
$positions = Position::orderBy('name', 'asc')->get();
$subjects = Subject::orderBy('name', 'asc')->get(); // Kirim Subject ke form
return view('pages.admin.exams.edit', compact('question', 'departments', 'positions', 'subjects'));
}
// Fungsi Show untuk Detail Soal
public function show($id)
{
$question = QuestionBank::with(['subject', 'department', 'position', 'creator'])->findOrFail($id);
return view('pages.admin.exams.show', compact('question'));
}
/**
* 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.questions.import');
}
}