Sync SOP ke e-doc Plant & Add Subject dan penyempurnaan bagian matrix training karyawan
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Subject;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SubjectController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// Tarik data materi LMS
|
||||
$subjects = Subject::withCount('questions')->orderBy('name', 'asc')->paginate(20);
|
||||
|
||||
// Tarik data SOP dari database server 11 untuk opsi Dropdown
|
||||
$sops = \Illuminate\Support\Facades\DB::connection('plantdoc')
|
||||
->table('tblDocuments')
|
||||
->select('name as nomor', 'comment as judul')
|
||||
->orderBy('name', 'asc')
|
||||
->get();
|
||||
|
||||
// Pastikan $sops dikirim ke view
|
||||
return view('pages.admin.subjects.index', compact('subjects', 'sops'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255|unique:subjects,name',
|
||||
]);
|
||||
|
||||
Subject::create(['name' => $request->name]);
|
||||
return redirect()->route('admin.subjects.index')->with('success', 'Materi baru berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255|unique:subjects,name,' . $id,
|
||||
]);
|
||||
|
||||
$subject = Subject::findOrFail($id);
|
||||
$subject->update(['name' => $request->name]);
|
||||
|
||||
return redirect()->route('admin.subjects.index')->with('success', 'Nama materi berhasil diperbarui.');
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
// Tarik data materi beserta soal-soalnya secara menurun (terbaru di atas)
|
||||
$subject = \App\Models\Subject::with(['questions' => function($query) {
|
||||
$query->latest();
|
||||
}])->withCount('questions')->findOrFail($id);
|
||||
|
||||
return view('pages.admin.subjects.show', compact('subject'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$subject = Subject::findOrFail($id);
|
||||
return view('pages.admin.subjects.edit', compact('subject'));
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$subject = Subject::findOrFail($id);
|
||||
|
||||
// Cek apakah materi ini sudah dipakai di bank soal
|
||||
if ($subject->questions()->count() > 0) {
|
||||
return redirect()->route('admin.subjects.index')->with('error', 'Gagal menghapus! Materi ini sedang digunakan pada Bank Soal.');
|
||||
}
|
||||
|
||||
$subject->delete();
|
||||
return redirect()->route('admin.subjects.index')->with('success', 'Materi berhasil dihapus.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user