From 84613496da635c549a890fd25674e85ed529e84f Mon Sep 17 00:00:00 2001 From: abiyasa05 Date: Sat, 21 Jun 2025 08:13:59 +0700 Subject: [PATCH] update literacy material controller for add story text --- .../Literacy/LiteracyMaterialController.php | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyMaterialController.php b/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyMaterialController.php index d388fa6..bdb0354 100644 --- a/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyMaterialController.php +++ b/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyMaterialController.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Controller; use App\Models\Literacy\LiteracyMaterial; use App\Models\Literacy\LiteracyQuestion; +use App\Models\Literacy\LiteracyStoryText; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; @@ -39,6 +40,7 @@ public function store(Request $request) { $request->validate([ 'title' => 'required|string', + 'story_text' => 'nullable|string', 'description' => 'nullable|string', 'file_path' => 'nullable|file|mimes:pdf,doc,docx|max:1048576' ]); @@ -51,13 +53,22 @@ public function store(Request $request) $filePath = str_replace('public/', '', $path); // Simpan path relatif } - LiteracyMaterial::create([ + // Buat materi utama + $material = LiteracyMaterial::create([ 'title' => $request->title, 'description' => $request->description, 'file_path' => $filePath, 'user_id' => auth()->id() ]); + // Simpan story_text jika ada + if (!empty(trim($request->story_text))) { + LiteracyStoryText::create([ + 'material_id' => $material->id, + 'story_text' => $request->story_text + ]); + } + return redirect()->route('literacy_teacher_materials'); } @@ -72,8 +83,11 @@ public function update(Request $request, $id) $request->validate([ 'title' => 'required|string', 'description' => 'nullable|string', - 'file_path' => 'nullable|file|mimes:pdf,doc,docx,txt|max:1048576' - + 'file_path' => 'nullable|file|mimes:pdf,doc,docx,txt|max:1048576', + 'story_texts' => 'array', + 'story_texts.*' => 'nullable|string', + 'story_text_ids' => 'array', + 'story_text_ids.*' => 'nullable|integer', ]); $material = LiteracyMaterial::findOrFail($id); @@ -92,9 +106,36 @@ public function update(Request $request, $id) $material->update([ 'title' => $request->title, 'description' => $request->description, - 'file_path' => $filePath + 'file_path' => $filePath, ]); + // Update, Tambah, dan Hapus Story Texts + $existingIds = $material->storyTexts->pluck('id')->toArray(); // semua ID lama + $sentIds = $request->input('story_text_ids', []); + $texts = $request->input('story_texts', []); + + foreach ($texts as $index => $text) { + $id = $sentIds[$index] ?? null; + $cleaned = trim($text); + + if ($id && in_array($id, $existingIds)) { + // Update + LiteracyStoryText::where('id', $id)->update([ + 'story_text' => $cleaned + ]); + } elseif (!$id && !empty($cleaned)) { + // Tambah baru + LiteracyStoryText::create([ + 'material_id' => $material->id, + 'story_text' => $cleaned + ]); + } + } + + // Hapus yang tidak dikirim lagi + $deletedIds = array_diff($existingIds, array_filter($sentIds)); + LiteracyStoryText::destroy($deletedIds); + return redirect()->route('literacy_teacher_materials'); }