Add Quiz feature: 10-question quizzes with progressive scoring (max 40 pts)

- Quizzes table with questions, answer options, attempts, answers
- Question types: multiple_choice, exclusion, true_false, free_text
- Progressive scoring: [1,1,2,2,3,3,4,6,8,10] = max 40 per quiz
- Alpine.js countdown timer per question with auto-submit on timeout
- Admin: CRUD for quizzes + per-question editor, JSON export/import
- Child: quiz overview with best scores, question view, result breakdown
- Nav: Quiz link in child header and admin sidebar
This commit is contained in:
root
2026-05-05 21:14:09 +00:00
parent 213d4b4832
commit 6c6dd26823
21 changed files with 984 additions and 1 deletions
@@ -0,0 +1,46 @@
@extends('layouts.child')
@section('content')
<h1 class="text-2xl font-bold text-indigo-700 mb-2">🧠 Quiz</h1>
<p class="text-slate-500 mb-6">Beantworte 10 Fragen max. 40 Münzen zu gewinnen!</p>
@foreach($subjects as $subjectId => $subject)
@php $group = $quizzes->get($subjectId, collect()) @endphp
@if($group->isNotEmpty())
<div class="mb-6">
<h2 class="font-bold text-slate-600 text-sm uppercase tracking-wide mb-3">{{ $subject->icon }} {{ $subject->name }}</h2>
<div class="grid gap-3">
@foreach($group as $quiz)
<div class="bg-white rounded-2xl border border-slate-200 shadow-sm px-5 py-4">
<div class="flex items-start justify-between gap-4">
<div class="flex-1 min-w-0">
<h3 class="font-bold text-slate-800 text-base">{{ $quiz->title }}</h3>
@if($quiz->description)<p class="text-sm text-slate-500 mt-0.5">{{ $quiz->description }}</p>@endif
<div class="flex items-center gap-3 mt-2 text-xs text-slate-400">
<span>📝 {{ $quiz->questions_count }} Fragen</span>
<span>🏆 max. 40 Münzen</span>
@if(isset($bestScores[$quiz->id]))
<span class="text-amber-600 font-medium">Bisher: {{ $bestScores[$quiz->id] }}/40 🪙</span>
@endif
</div>
</div>
<form method="POST" action="{{ route('quiz.start',$quiz) }}">
@csrf
<button class="bg-indigo-600 hover:bg-indigo-700 text-white px-5 py-2.5 rounded-xl text-sm font-bold whitespace-nowrap">
{{ isset($bestScores[$quiz->id]) ? 'Nochmal' : 'Starten' }}
</button>
</form>
</div>
</div>
@endforeach
</div>
</div>
@endif
@endforeach
@if($quizzes->isEmpty())
<div class="text-center py-16 text-slate-400">
<p class="text-4xl mb-3">🧩</p>
<p>Noch keine Quizzes verfügbar. Schau später nochmal vorbei!</p>
</div>
@endif
@endsection
@@ -0,0 +1,123 @@
@extends('layouts.child')
@section('content')
@php $tl = $question->time_limit ?? 0; @endphp
<div x-data="{
tl: {{ $tl }},
left: {{ $tl }},
timedOut: false,
timer: null,
init() {
if (this.tl > 0) {
this.timer = setInterval(() => {
this.left--;
if (this.left <= 0) {
clearInterval(this.timer);
this.timedOut = true;
this.$nextTick(() => {
document.querySelectorAll('[required]').forEach(e => e.removeAttribute('required'));
document.getElementById('timeout-input').value = '1';
document.getElementById('answer-form').submit();
});
}
}, 1000);
}
}
}">
{{-- Header bar --}}
<div class="flex items-center gap-3 mb-4">
<a href="{{ route('quiz.index') }}" class="text-slate-400 hover:text-slate-600 text-sm"> Quiz</a>
<div class="flex-1 bg-slate-200 rounded-full h-2.5">
<div class="bg-violet-500 h-2.5 rounded-full transition-all duration-300"
style="width: {{ ($attempt->current_question / max($attempt->quiz->questions()->count(),1)) * 100 }}%"></div>
</div>
<span class="text-sm font-semibold text-slate-600 whitespace-nowrap">
{{ $attempt->current_question + 1 }} / {{ $attempt->quiz->questions()->count() }}
</span>
</div>
{{-- Timer bar --}}
<template x-if="tl > 0">
<div class="mb-4">
<div class="bg-slate-200 rounded-full h-2">
<div class="h-2 rounded-full transition-all duration-1000"
:class="left > tl*0.5 ? 'bg-green-500' : (left > tl*0.25 ? 'bg-yellow-500' : 'bg-red-500')"
:style="'width:' + Math.max(0,(left/tl)*100) + '%'"></div>
</div>
<div class="text-right text-xs mt-1" :class="left <= 5 ? 'text-red-500 font-bold' : 'text-slate-400'">
<span x-text="left"></span>s
</div>
</div>
</template>
{{-- Last answer flash --}}
@if(session('last_answer'))
@php $la = session('last_answer'); @endphp
<div class="mb-4 rounded-xl px-4 py-3 text-sm font-medium flex items-center gap-2
{{ $la['timeout'] ? 'bg-slate-100 text-slate-600' : ($la['correct'] ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-700') }}">
@if($la['timeout']) Zeit abgelaufen 0 Münzen
@elseif($la['correct']) Richtig! +{{ $la['points'] }} 🪙
@else Leider falsch 0 Münzen
@endif
</div>
@endif
{{-- Points badge --}}
<div class="text-center mb-5">
<span class="bg-amber-100 text-amber-700 text-sm font-bold px-4 py-1.5 rounded-full">
+{{ $pointsIfCorrect }} 🪙 bei richtiger Antwort
</span>
</div>
{{-- Question card --}}
<div class="bg-white rounded-2xl shadow-sm border border-slate-200 p-6">
<h2 class="text-xl font-black text-slate-800 mb-6 leading-snug">{{ $question->question_text }}</h2>
<form method="POST" action="{{ route('quiz.answer',$attempt) }}" id="answer-form">
@csrf
<input type="hidden" name="timeout" value="0" id="timeout-input">
@if(in_array($question->type, ['multiple_choice','exclusion']))
<div class="grid grid-cols-2 gap-3 mb-6">
@foreach($question->answerOptions as $opt)
<label class="flex items-center gap-3 p-4 border-2 border-slate-200 rounded-xl cursor-pointer
hover:border-violet-300 hover:bg-violet-50 has-[:checked]:border-violet-500 has-[:checked]:bg-violet-50
transition-colors">
<input type="radio" name="answer" value="{{ $opt->id }}" required class="w-4 h-4 text-violet-600 shrink-0">
<span class="text-slate-700 font-medium text-sm">{{ $opt->text }}</span>
</label>
@endforeach
</div>
@elseif($question->type === 'true_false')
<div class="grid grid-cols-2 gap-4 mb-6">
<label class="flex items-center justify-center gap-3 p-5 border-2 border-slate-200 rounded-xl cursor-pointer
hover:border-green-400 hover:bg-green-50 has-[:checked]:border-green-500 has-[:checked]:bg-green-50
transition-colors">
<input type="radio" name="answer" value="true" required class="sr-only">
<span class="text-2xl"></span>
<span class="font-bold text-slate-700">Wahr</span>
</label>
<label class="flex items-center justify-center gap-3 p-5 border-2 border-slate-200 rounded-xl cursor-pointer
hover:border-red-400 hover:bg-red-50 has-[:checked]:border-red-500 has-[:checked]:bg-red-50
transition-colors">
<input type="radio" name="answer" value="false" required class="sr-only">
<span class="text-2xl"></span>
<span class="font-bold text-slate-700">Falsch</span>
</label>
</div>
@elseif($question->type === 'free_text')
<div class="mb-6">
<input type="text" name="answer" required autofocus placeholder="Deine Antwort eingeben …"
class="w-full border-2 border-slate-200 focus:border-violet-500 rounded-xl px-4 py-3 text-lg font-bold text-slate-800 outline-none text-center transition-colors">
</div>
@endif
<button type="submit" class="w-full bg-violet-600 hover:bg-violet-700 text-white py-3 rounded-xl font-bold text-base">
Antworten
</button>
</form>
</div>
</div>
@endsection
@@ -0,0 +1,63 @@
@extends('layouts.child')
@section('content')
<div class="text-center mb-8">
<div class="text-6xl mb-3">{{ $attempt->stars() }}</div>
<h1 class="text-3xl font-black text-slate-800">{{ $attempt->score }} / 40 Münzen</h1>
<p class="text-slate-500 mt-1">{{ $attempt->quiz->title }}</p>
<div class="mt-3 inline-flex items-center gap-2 bg-amber-100 text-amber-700 font-bold px-5 py-2 rounded-full text-lg">
+{{ $attempt->score }} 🪙 verdient!
</div>
</div>
{{-- Per-question breakdown --}}
<div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden mb-6">
<table class="w-full text-sm">
<thead class="bg-slate-50 border-b border-slate-100">
<tr>
<th class="px-4 py-3 text-left font-medium text-slate-500">#</th>
<th class="px-4 py-3 text-left font-medium text-slate-500">Frage</th>
<th class="px-4 py-3 text-center font-medium text-slate-500">Ergebnis</th>
<th class="px-4 py-3 text-center font-medium text-slate-500">Münzen</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100">
@foreach($answers as $i => $a)
@php
$display = $a->answer_given;
if ($display === '__timeout__') {
$display = '⏱ Zeit abgelaufen';
} elseif (in_array($a->question->type, ['multiple_choice','exclusion'])) {
$opt = $a->question->answerOptions->firstWhere('id', (int)$a->answer_given);
$display = $opt ? $opt->text : '';
} elseif ($a->question->type === 'true_false') {
$display = $display === 'true' ? 'Wahr' : 'Falsch';
}
$points_scale = \App\Models\QuizAttempt::POINTS;
@endphp
<tr class="{{ $a->is_correct ? 'bg-green-50' : '' }}">
<td class="px-4 py-3 text-slate-400 font-medium">{{ $i+1 }}</td>
<td class="px-4 py-3 text-slate-700">{{ Str::limit($a->question->question_text, 55) }}</td>
<td class="px-4 py-3 text-center">
@if($a->is_correct)
<span class="text-green-700 font-medium"> {{ $display }}</span>
@else
<span class="text-red-600"> {{ $display }}</span>
@endif
</td>
<td class="px-4 py-3 text-center font-bold {{ $a->is_correct ? 'text-amber-600' : 'text-slate-300' }}">
{{ $a->is_correct ? '+' . $a->points_earned : '0' }}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="flex gap-3">
<form method="POST" action="{{ route('quiz.start',$attempt->quiz) }}" class="flex-1">
@csrf
<button class="w-full bg-slate-100 hover:bg-slate-200 text-slate-700 py-3 rounded-xl font-medium">🔁 Nochmal spielen</button>
</form>
<a href="{{ route('quiz.index') }}" class="flex-1 bg-indigo-600 hover:bg-indigo-700 text-white py-3 rounded-xl font-bold text-center"> Zur Quiz-Übersicht</a>
</div>
@endsection