From 687592c644021b8416f04a076fff610cb36f9fcf Mon Sep 17 00:00:00 2001 From: abiyasa05 Date: Sat, 21 Jun 2025 12:41:18 +0700 Subject: [PATCH] update: for view and controller assessment add story text --- .../Literacy/LiteracyAssessmentController.php | 101 +------- .../student/assessments/assessment.blade.php | 235 +++++++++--------- 2 files changed, 117 insertions(+), 219 deletions(-) diff --git a/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyAssessmentController.php b/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyAssessmentController.php index de06628..7bb07a5 100644 --- a/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyAssessmentController.php +++ b/Penilaian Literasi/iClOP-V2/app/Http/Controllers/Literacy/LiteracyAssessmentController.php @@ -88,7 +88,9 @@ public function show($id) ->with([ 'answers' => function ($query) use ($id) { $query->where('assessment_id', $id); - } + }, + 'options', + 'material.storyTexts' ]) ->get(); @@ -179,101 +181,6 @@ public function storeAnswer(Request $request, $assessmentId): JsonResponse return response()->json(['success' => true]); } - // public function submitAssessment($id) - // { - // try { - // $user = Auth::user(); - // $assessment = LiteracyAssessment::where('id', $id) - // ->where('user_id', $user->id) - // ->firstOrFail(); - - // if ($assessment->status !== 'in_progress') { - // return response()->json(['error' => 'Asesmen tidak sedang berjalan.'], 400); - // } - - // $answers = LiteracyAnswer::where('assessment_id', $id)->get(); - - // $correctWeight = 0; - // $totalWeight = 0; - // $essayThreshold = 60; - - // $normalizeText = function ($text) { - // $text = strtolower($text); - // $text = preg_replace('/[^\p{L}\p{N}\s]/u', '', $text); - // $text = preg_replace('/\s+/', ' ', $text); - // return trim($text); - // }; - - // $feedback = []; - - // foreach ($answers as $answer) { - // $question = LiteracyQuestion::find($answer->question_id); - // if (!$question) - // continue; - - // $weight = $question->weight ?? ($question->type === 'essay' ? 5 : 1); - // $totalWeight += $weight; - - // if ($question->type === 'multiple_choice') { - // if ( - // $answer->option_id && - // LiteracyOption::where('id', $answer->option_id) - // ->where('is_correct', true) - // ->exists() - // ) { - // $correctWeight += $weight; - // } - - // } elseif ($question->type === 'essay') { - // $userAnswer = $normalizeText($answer->answer_text ?? ''); - // $correctAnswerRaw = $question->essay_answer ?? ''; - // $correctAnswers = array_map($normalizeText, preg_split('/\r\n|\r|\n/', $correctAnswerRaw)); - // $maxMatchPercent = 0; - - // foreach ($correctAnswers as $correctAnswer) { - // similar_text($userAnswer, $correctAnswer, $percent); - // $maxMatchPercent = max($maxMatchPercent, $percent); - // } - - // if ($maxMatchPercent >= $essayThreshold) { - // $correctWeight += $weight; - // } - // } - // } - - // // Hitung skor akhir berdasarkan bobot - // $score = $totalWeight > 0 ? ($correctWeight / $totalWeight) * 100 : 0; - - // // Update asesmen yang sedang berjalan jadi completed - // $assessment->update([ - // 'score' => round($score, 2), - // 'status' => 'completed', - // 'assessed_at' => now(), - // 'feedback' => json_encode($feedback), // Menyimpan feedback ke database - // ]); - - // // Buat asesmen baru dengan status 'pending' untuk percakapan berikutnya - // LiteracyAssessment::create([ - // 'user_id' => $user->id, - // 'status' => 'pending', - // 'score' => null, - // 'feedback' => '', - // 'assessed_at' => null, - // 'created_at' => now(), - // 'updated_at' => now(), - // ]); - - // return response()->json([ - // 'message' => 'Asesmen berhasil diselesaikan.', - // 'score' => round($score, 2), - // 'feedback' => $feedback, // Mengembalikan feedback dalam respons - // ], 200); - - // } catch (\Exception $e) { - // return response()->json(['error' => 'Terjadi kesalahan: ' . $e->getMessage()], 500); - // } - // } - public function submitAssessment($id) { try { @@ -338,7 +245,7 @@ public function submitAssessment($id) $correctWeight += $weight; } else { try { - $apiResponse = Http::timeout(10)->post('http://127.0.0.1:8001/generate-feedback/', [ + $apiResponse = Http::timeout(10)->post('http://127.0.0.1:8010/generate-feedback/', [ 'user_answer' => $answer->answer_text, 'expected_answer' => $bestMatch, ]); diff --git a/Penilaian Literasi/iClOP-V2/resources/views/literacy/student/assessments/assessment.blade.php b/Penilaian Literasi/iClOP-V2/resources/views/literacy/student/assessments/assessment.blade.php index 0f3b4e0..e005a54 100644 --- a/Penilaian Literasi/iClOP-V2/resources/views/literacy/student/assessments/assessment.blade.php +++ b/Penilaian Literasi/iClOP-V2/resources/views/literacy/student/assessments/assessment.blade.php @@ -362,124 +362,126 @@ @php use Illuminate\Support\Str; - $multipleChoiceQuestions = $questions->filter(fn($q) => $q->options->isNotEmpty()); - $essayQuestions = $questions->filter(fn($q) => $q->options->isEmpty()); - $multipleChoiceNumber = 0; - $essayNumber = 0; + $questionsByMaterial = $questions->groupBy('material_id'); @endphp
- - @if ($multipleChoiceQuestions->isNotEmpty()) -
-
-

Bagian 1: Pilihan Ganda

- + + {{-- ===================== PILIHAN GANDA ===================== --}} +

Soal Pilihan Ganda

+ @php $mcGlobalNumber = 0; @endphp + @foreach ($questionsByMaterial as $materialId => $materialQuestions) + @php + $material = $materialQuestions->first()->material; + $storyTexts = $material->storyTexts ?? collect(); + $multipleChoiceQuestions = $materialQuestions->where('type', 'multiple_choice'); + @endphp + + @if ($multipleChoiceQuestions->isNotEmpty()) +
+
Teks Bacaan: {{ $material->title }}
+ @foreach ($storyTexts as $text) +
{{ $text->story_text }}
+ @endforeach
-
-

Pilih salah satu jawaban yang paling tepat.

- @foreach ($multipleChoiceQuestions as $question) - @php - $multipleChoiceNumber++; - $savedAnswer = optional($question->answers->where('assessment_id', $assessment->id)->first()); - $kutipan = null; - $pertanyaanBersih = $question->question_text; + @foreach ($multipleChoiceQuestions as $index => $question) + @php + $mcGlobalNumber++; + $savedAnswer = optional($question->answers->where('assessment_id', $assessment->id)->first()); + $kutipan = null; + $pertanyaanBersih = $question->question_text; - // Deteksi apakah soal memuat kutipan atau paragraf - if (Str::contains($question->question_text, 'Bacalah')) { - preg_match('/Bacalah (kutipan|paragraf) berikut:\s*"(.*?)"\s*(.*)/s', $question->question_text, $matches); - if (count($matches) >= 4) { - $kutipan = trim($matches[2]); - $pertanyaanBersih = trim($matches[3]); + if (Str::contains($question->question_text, 'Bacalah')) { + preg_match('/Bacalah (kutipan|paragraf) berikut:\s*\"(.*?)\"\s*(.*)/s', $question->question_text, $matches); + if (count($matches) >= 4) { + $kutipan = trim($matches[2]); + $pertanyaanBersih = trim($matches[3]); + } } - } - @endphp + @endphp -
-
- -
- {{ $multipleChoiceNumber }}. +
+
+
+ {{ $mcGlobalNumber }}. +
+
+ @if ($kutipan) +

Bacalah kutipan berikut:

+
+ "{{ $kutipan }}" +
+ @endif +

{{ $pertanyaanBersih }}

+
- -
- @if ($kutipan) -

Bacalah kutipan berikut:

-
- "{{ $kutipan }}" -
- @endif -

{{ $pertanyaanBersih }}

+ +
+
+ @foreach ($question->options as $option) + + @endforeach +
+ @endforeach + @endif + @endforeach -
-
- @foreach ($question->options as $option) - - @endforeach -
-
+ {{-- ===================== ISIAN / ESSAY ===================== --}} +

Soal Isian

+ @php $essayGlobalNumber = 0; @endphp + @foreach ($questionsByMaterial as $materialId => $materialQuestions) + @php + $material = $materialQuestions->first()->material; + $storyTexts = $material->storyTexts ?? collect(); + $essayQuestions = $materialQuestions->where('type', 'essay'); + @endphp + + @if ($essayQuestions->isNotEmpty()) +
+
Teks Bacaan: {{ $material->title }}
+ @foreach ($storyTexts as $text) +
{{ $text->story_text }}
+ @endforeach
- @endforeach - @endif - - @if ($essayQuestions->isNotEmpty()) -

Bagian 2: Isian

-

Jawablah pertanyaan berikut dengan jawaban yang sesuai.

+ @foreach ($essayQuestions as $index => $question) + @php + $essayGlobalNumber++; + $savedAnswer = optional($question->answers->where('assessment_id', $assessment->id)->first()); + @endphp - @foreach ($essayQuestions as $question) - @php - $essayNumber++; - $savedAnswer = optional($question->answers->where('assessment_id', $assessment->id)->first()); - $kutipan = null; - $pertanyaanBersih = $question->question_text; - - if (Str::contains($question->question_text, 'Bacalah')) { - preg_match('/Bacalah (kutipan|paragraf) berikut:\s*"(.*?)"\s*(.*)/s', $question->question_text, $matches); - if (count($matches) >= 4) { - $kutipan = trim($matches[2]); - $pertanyaanBersih = trim($matches[3]); - } - } - @endphp - -
-
- -
- {{ $essayNumber }}. +
+
+
+ {{ $essayGlobalNumber }}. +
+
+

{{ $question->question_text }}

+
- -
- @if ($kutipan) -

Bacalah kutipan berikut:

-
- "{{ $kutipan }}" -
- @endif -

{{ $pertanyaanBersih }}

+ +
+
-
- -
-
- @endforeach - @endif + @endforeach + @endif + @endforeach +
@@ -654,27 +656,25 @@ class="btn {{ $btnClass }} d-flex justify-content-center align-items-center" function checkUnansweredQuestions() { let unansweredCount = 0; - // Cek soal pilihan ganda (radio button) - @foreach ($multipleChoiceQuestions as $question) - if (!$("input[name='question_{{ $question->id }}']:checked").val()) { - unansweredCount++; - } + @foreach ($questions as $question) + @if ($question->type === 'multiple_choice') + if (!$("input[name='question_{{ $question->id }}']:checked").val()) { + unansweredCount++; + } + @elseif ($question->type === 'essay') + if (!$("textarea[name='question_{{ $question->id }}']").val().trim()) { + unansweredCount++; + } + @endif @endforeach - // Cek soal isian (textarea) - @foreach ($essayQuestions as $question) - if (!$("textarea[name='question_{{ $question->id }}']").val().trim()) { - unansweredCount++; - } - @endforeach - - console.log("Soal yang belum dijawab:", unansweredCount); // Debugging + console.log("Soal yang belum dijawab:", unansweredCount); if (unansweredCount > 0) { $("#unansweredCount").text(unansweredCount); - $("#unansweredWarningModal").modal("show"); // Tampilkan modal peringatan + $("#unansweredWarningModal").modal("show"); } else { - $("#confirmSubmitModal").modal("show"); // Tampilkan modal konfirmasi + $("#confirmSubmitModal").modal("show"); } } @@ -705,15 +705,6 @@ function checkUnansweredQuestions() { console.error("Gagal menyimpan jawaban:", error); } } - - // function updateNavigationButton(questionId) { - // const navButton = document.querySelector(`button[data-question-id='${questionId}']`); - - // if (navButton) { - // navButton.classList.remove("btn-outline-secondary"); - // navButton.classList.add("btn-primary", "border-white"); - // } - // }