118 lines
4.3 KiB
PHP
118 lines
4.3 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;
|
|
use App\Models\User;
|
|
use App\Models\QuestionBank;
|
|
|
|
class ExamManagementController extends Controller
|
|
{
|
|
public function questionBank(Request $request)
|
|
{
|
|
// 1. Ambil ID unik siapa saja yang PERNAH membuat soal di tabel question_banks
|
|
$creatorIds = \App\Models\QuestionBank::whereNotNull('created_by')
|
|
->distinct()
|
|
->pluck('created_by');
|
|
|
|
// 2. Tarik data User HANYA berdasarkan ID pembuat soal tersebut
|
|
$creators = \App\Models\User::whereIn('id', $creatorIds)
|
|
->orderBy('first_name', 'asc')
|
|
->get();
|
|
|
|
// 3. Tarik data pendukung lainnya untuk filter & dropdown
|
|
$subjects = \App\Models\Subject::orderBy('name', 'asc')->get();
|
|
$departments = \App\Models\Department::orderBy('name', 'asc')->get();
|
|
$positions = \App\Models\Position::orderBy('name', 'asc')->get();
|
|
|
|
// 4. Hitung Total Soal Keseluruhan (TANPA FILTER) untuk Badge di View
|
|
$totalQuestions = \App\Models\QuestionBank::count(); // <--- TAMBAHKAN BARIS INI
|
|
|
|
// 5. Query utama Bank Soal (dengan fitur pencarian/filter)
|
|
$query = \App\Models\QuestionBank::with(['subject', 'department', 'creator', 'position']);
|
|
|
|
// Filter berdasarkan pembuat soal
|
|
if ($request->filled('creator_id')) {
|
|
$query->where('created_by', $request->creator_id);
|
|
}
|
|
|
|
// Filter berdasarkan subject/materi
|
|
if ($request->filled('subject_id')) {
|
|
$query->where('subject_id', $request->subject_id);
|
|
}
|
|
|
|
// Filter berdasarkan departemen
|
|
if ($request->filled('department_id')) {
|
|
$query->where('department_id', $request->department_id);
|
|
}
|
|
|
|
// Filter berdasarkan posisi/jabatan
|
|
if ($request->filled('position_id')) {
|
|
$query->where('position_id', $request->position_id);
|
|
}
|
|
|
|
// Pagination
|
|
$questions = $query->latest()->paginate(20);
|
|
|
|
// Pastikan $totalQuestions ikut dikirim ke compact
|
|
return view('pages.admin.question-banks.question-bank', compact('questions', 'creators', 'subjects', 'departments', 'positions', 'totalQuestions'));
|
|
}
|
|
|
|
// 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.question-banks.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.question-banks.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.question-banks.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');
|
|
}
|
|
} |