update: change controller for literacy questions
This commit is contained in:
parent
de05919e72
commit
5de96db598
|
|
@ -162,4 +162,15 @@ public function show_materials($id)
|
|||
|
||||
return response()->file($path);
|
||||
}
|
||||
|
||||
public function getStoryTexts($id)
|
||||
{
|
||||
$material = LiteracyMaterial::with('storyTexts')->find($id);
|
||||
|
||||
if (!$material) {
|
||||
return response()->json([], 404); // tanggapi dengan error jika tidak ditemukan
|
||||
}
|
||||
|
||||
return response()->json($material->storyTexts);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Literacy\LiteracyQuestion;
|
||||
use App\Models\Literacy\LiteracyAnswer;
|
||||
use App\Models\Literacy\LiteracyStoryText;
|
||||
use App\Models\Literacy\LiteracyMaterial;
|
||||
use App\Models\Literacy\LiteracyOption;
|
||||
use App\Models\Literacy\LiteracyAssessment;
|
||||
|
|
@ -17,7 +17,10 @@ class LiteracyQuestionController extends Controller
|
|||
{
|
||||
public function questions()
|
||||
{
|
||||
$questions = LiteracyQuestion::orderBy('created_at', 'desc')->get();
|
||||
$questions = LiteracyQuestion::with(['material', 'storyTexts', 'options'])->orderBy('created_at', 'desc')->get();
|
||||
foreach ($questions as $question) {
|
||||
$question->first_story_text = $question->storyTexts->first();
|
||||
}
|
||||
$materials = LiteracyMaterial::all();
|
||||
$users = User::all();
|
||||
return view('literacy.teacher.questions.index', [
|
||||
|
|
@ -41,24 +44,28 @@ public function create()
|
|||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'material_id' => 'required|exists:literacy_materials,id',
|
||||
'story_text_id' => 'nullable|exists:literacy_story_texts,id',
|
||||
'question_text' => 'required|string',
|
||||
'type' => 'required|in:multiple_choice,essay',
|
||||
]);
|
||||
|
||||
$question = LiteracyQuestion::create($validated);
|
||||
$question = LiteracyQuestion::create([
|
||||
'material_id' => $validated['material_id'],
|
||||
'story_text_id' => $validated['story_text_id'] ?? null,
|
||||
'question_text' => $validated['question_text'],
|
||||
'type' => $validated['type'],
|
||||
]);
|
||||
|
||||
// Jika tipe soal adalah pilihan ganda, simpan opsi jawaban
|
||||
if ($request->type === 'multiple_choice' && $request->has('options')) {
|
||||
foreach ($request->options as $option) {
|
||||
$question->options()->create([
|
||||
'option_text' => $option['text'] ?? null, // Gunakan null jika tidak ada teks
|
||||
'score' => $option['score'] ?? 0, // Pastikan skor selalu ada
|
||||
'option_text' => $option['text'] ?? null,
|
||||
'score' => $option['score'] ?? 0,
|
||||
'is_correct' => isset($option['is_correct']) ? 1 : 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
// Jika tipe soal adalah isian (essay), simpan skor & jawaban yang benar
|
||||
elseif ($request->type === 'essay') {
|
||||
} elseif ($request->type === 'essay') {
|
||||
$question->update([
|
||||
'essay_score' => $request->essay_score ?? 0,
|
||||
'essay_answer' => $request->essay_answer ?? null,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user