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,71 @@
@extends('layouts.admin')
@section('title','Frage hinzufügen')
@section('content')
<div class="max-w-2xl">
<a href="{{ route('admin.quizzes.edit',$quiz) }}" class="text-sm text-slate-500 hover:text-slate-700 mb-4 inline-block"> Zurück zu {{ $quiz->title }}</a>
<div class="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
<h2 class="font-semibold text-slate-800 mb-5">Neue Frage hinzufügen</h2>
<form method="POST" action="{{ route('admin.quizzes.questions.store',$quiz) }}" class="space-y-4">
@csrf
@php $qtype='multiple_choice'; $correctIdx=0; $question_text=''; $time_limit=''; $correct_answer=''; $options=[]; @endphp
<div x-data="{ qtype: '{{ $qtype }}', correctIdx: {{ $correctIdx }} }">
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Fragetyp</label>
<select name="type" x-model="qtype" required
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
<option value="multiple_choice">Multiple Choice Fehlersuche</option>
<option value="exclusion">Ausschlussverfahren (welches gehört nicht dazu?)</option>
<option value="true_false">Wahr / Falsch</option>
<option value="free_text">Freitext Eingabe</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Frage</label>
<textarea name="question_text" required rows="3"
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm font-medium focus:ring-2 focus:ring-violet-500 outline-none">{{ $question_text ?? '' }}</textarea>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Zeitlimit in Sekunden (leer = kein Limit)</label>
<input name="time_limit" type="number" min="5" max="120" value="{{ $time_limit ?? '' }}" placeholder="z.B. 30"
class="w-32 border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
{{-- MC / Exclusion options --}}
<div x-show="qtype === 'multiple_choice' || qtype === 'exclusion'" class="space-y-2">
<label class="block text-sm font-medium text-slate-700">Antwortmöglichkeiten <span class="text-slate-400 font-normal">( = richtige Antwort)</span></label>
@for($i = 0; $i < 4; $i++)
<div class="flex items-center gap-3">
<input type="radio" name="correct_option" value="{{ $i }}" x-bind:checked="correctIdx === {{ $i }}"
@change="correctIdx = {{ $i }}"
class="text-violet-600 w-4 h-4 shrink-0">
<input type="text" name="options[{{ $i }}][text]" value="{{ $options[$i]['text'] ?? '' }}"
placeholder="Option {{ $i+1 }}"
class="flex-1 border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
@endfor
<p class="text-xs text-slate-400">Klicke den Radio-Button links neben der richtigen Antwort.</p>
</div>
{{-- True / False --}}
<div x-show="qtype === 'true_false'">
<label class="block text-sm font-medium text-slate-700 mb-1">Richtige Antwort</label>
<select name="correct_answer"
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
<option value="true" {{ ($correct_answer ?? '') === 'true' ? 'selected' : '' }}> Wahr</option>
<option value="false" {{ ($correct_answer ?? '') === 'false' ? 'selected' : '' }}> Falsch</option>
</select>
</div>
{{-- Free text --}}
<div x-show="qtype === 'free_text'">
<label class="block text-sm font-medium text-slate-700 mb-1">Richtige Antwort (exakter Text, Groß-/Kleinschreibung egal)</label>
<input type="text" name="correct_answer" value="{{ $correct_answer ?? '' }}" placeholder="z.B. 72"
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
</div>
<button type="submit" class="w-full bg-violet-600 hover:bg-violet-700 text-white py-2 rounded-lg font-medium text-sm">Frage hinzufügen</button>
</form>
</div>
</div>
@endsection
@@ -0,0 +1,80 @@
@extends('layouts.admin')
@section('title','Frage bearbeiten')
@section('content')
<div class="max-w-2xl">
<a href="{{ route('admin.quizzes.edit',$question->quiz) }}" class="text-sm text-slate-500 hover:text-slate-700 mb-4 inline-block"> Zurück zu {{ $question->quiz->title }}</a>
<div class="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
<h2 class="font-semibold text-slate-800 mb-5">Frage bearbeiten</h2>
<form method="POST" action="{{ route('admin.quiz-questions.update',$question) }}" class="space-y-4">
@csrf @method('PUT')
@php
$qtype = $question->type;
$question_text = $question->question_text;
$time_limit = $question->time_limit;
$correct_answer = $question->correct_answer;
$opts = $question->answerOptions->toArray();
$options = array_column($opts, null);
$correctIdx = 0;
foreach ($opts as $i => $o) { if ($o['is_correct']) { $correctIdx = $i; break; } }
@endphp
<div x-data="{ qtype: '{{ $qtype }}', correctIdx: {{ $correctIdx }} }">
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Fragetyp</label>
<select name="type" x-model="qtype" required
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
<option value="multiple_choice">Multiple Choice Fehlersuche</option>
<option value="exclusion">Ausschlussverfahren (welches gehört nicht dazu?)</option>
<option value="true_false">Wahr / Falsch</option>
<option value="free_text">Freitext Eingabe</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Frage</label>
<textarea name="question_text" required rows="3"
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm font-medium focus:ring-2 focus:ring-violet-500 outline-none">{{ $question_text ?? '' }}</textarea>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Zeitlimit in Sekunden (leer = kein Limit)</label>
<input name="time_limit" type="number" min="5" max="120" value="{{ $time_limit ?? '' }}" placeholder="z.B. 30"
class="w-32 border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
{{-- MC / Exclusion options --}}
<div x-show="qtype === 'multiple_choice' || qtype === 'exclusion'" class="space-y-2">
<label class="block text-sm font-medium text-slate-700">Antwortmöglichkeiten <span class="text-slate-400 font-normal">( = richtige Antwort)</span></label>
@for($i = 0; $i < 4; $i++)
<div class="flex items-center gap-3">
<input type="radio" name="correct_option" value="{{ $i }}" x-bind:checked="correctIdx === {{ $i }}"
@change="correctIdx = {{ $i }}"
class="text-violet-600 w-4 h-4 shrink-0">
<input type="text" name="options[{{ $i }}][text]" value="{{ $options[$i]['text'] ?? '' }}"
placeholder="Option {{ $i+1 }}"
class="flex-1 border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
@endfor
<p class="text-xs text-slate-400">Klicke den Radio-Button links neben der richtigen Antwort.</p>
</div>
{{-- True / False --}}
<div x-show="qtype === 'true_false'">
<label class="block text-sm font-medium text-slate-700 mb-1">Richtige Antwort</label>
<select name="correct_answer"
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
<option value="true" {{ ($correct_answer ?? '') === 'true' ? 'selected' : '' }}> Wahr</option>
<option value="false" {{ ($correct_answer ?? '') === 'false' ? 'selected' : '' }}> Falsch</option>
</select>
</div>
{{-- Free text --}}
<div x-show="qtype === 'free_text'">
<label class="block text-sm font-medium text-slate-700 mb-1">Richtige Antwort (exakter Text, Groß-/Kleinschreibung egal)</label>
<input type="text" name="correct_answer" value="{{ $correct_answer ?? '' }}" placeholder="z.B. 72"
class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
</div>
<button type="submit" class="w-full bg-violet-600 hover:bg-violet-700 text-white py-2 rounded-lg font-medium text-sm">Speichern</button>
</form>
</div>
</div>
@endsection
@@ -0,0 +1,28 @@
@extends('layouts.admin')
@section('title','Neues Quiz')
@section('content')
<div class="max-w-xl">
<a href="{{ route('admin.quizzes.index') }}" class="text-sm text-slate-500 hover:text-slate-700 mb-4 inline-block"> Zurück</a>
<div class="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
<h2 class="font-semibold text-slate-800 mb-5">Neues Quiz</h2>
<form method="POST" action="{{ route('admin.quizzes.store') }}" class="space-y-4">
@csrf
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Fach</label>
<select name="subject_id" required class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
@foreach($subjects as $s)<option value="{{ $s->id }}">{{ $s->icon }} {{ $s->name }}</option>@endforeach
</select>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Titel</label>
<input name="title" required value="{{ old('title') }}" class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Beschreibung (optional)</label>
<textarea name="description" rows="2" class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">{{ old('description') }}</textarea>
</div>
<button type="submit" class="w-full bg-violet-600 hover:bg-violet-700 text-white py-2 rounded-lg font-medium text-sm">Erstellen & Fragen hinzufügen </button>
</form>
</div>
</div>
@endsection
@@ -0,0 +1,72 @@
@extends('layouts.admin')
@section('title','Quiz bearbeiten')
@section('content')
<div class="max-w-3xl">
<a href="{{ route('admin.quizzes.index') }}" class="text-sm text-slate-500 hover:text-slate-700 mb-4 inline-block"> Zurück</a>
{{-- Metadata --}}
<div class="bg-white rounded-xl shadow-sm border border-slate-200 p-6 mb-6">
<h2 class="font-semibold text-slate-800 mb-5">Quiz-Einstellungen</h2>
<form method="POST" action="{{ route('admin.quizzes.update',$quiz) }}" class="space-y-4">
@csrf @method('PUT')
<div class="grid grid-cols-3 gap-4">
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Fach</label>
<select name="subject_id" required class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
@foreach($subjects as $s)<option value="{{ $s->id }}" {{ $s->id==$quiz->subject_id?'selected':'' }}>{{ $s->icon }} {{ $s->name }}</option>@endforeach
</select>
</div>
<div class="col-span-2">
<label class="block text-sm font-medium text-slate-700 mb-1">Titel</label>
<input name="title" required value="{{ $quiz->title }}" class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">
</div>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Beschreibung</label>
<textarea name="description" rows="2" class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-violet-500 outline-none">{{ $quiz->description }}</textarea>
</div>
<div class="flex items-center gap-2">
<input type="checkbox" name="active" id="active" value="1" {{ $quiz->active?'checked':'' }} class="w-4 h-4 text-violet-600">
<label for="active" class="text-sm text-slate-700">Quiz aktiv (für Kinder sichtbar)</label>
</div>
<button type="submit" class="bg-violet-600 hover:bg-violet-700 text-white px-6 py-2 rounded-lg font-medium text-sm">Speichern</button>
</form>
</div>
{{-- Questions --}}
<div class="flex justify-between items-center mb-3">
<h3 class="font-semibold text-slate-700">Fragen
<span class="{{ $questions->count() < 10 ? 'text-amber-500' : 'text-green-600' }} font-normal text-sm ml-1">({{ $questions->count() }}/10)</span>
</h3>
@if($questions->count() < 10)
<a href="{{ route('admin.quizzes.questions.create',$quiz) }}" class="bg-violet-600 hover:bg-violet-700 text-white px-4 py-2 rounded-lg text-sm font-medium">+ Frage hinzufügen</a>
@else
<span class="text-green-600 text-sm font-medium"> 10 Fragen vollständig</span>
@endif
</div>
@forelse($questions as $i => $q)
<div class="bg-white rounded-xl border border-slate-200 px-4 py-3 mb-2 flex items-start justify-between gap-4 hover:bg-slate-50">
<div class="flex items-start gap-3 flex-1 min-w-0">
<span class="text-slate-400 text-sm pt-0.5 shrink-0">{{ $i+1 }}.</span>
<div class="min-w-0">
<span class="text-xs bg-slate-100 text-slate-500 px-2 py-0.5 rounded mr-2">{{ $q->typeLabel() }}</span>
@if($q->time_limit)<span class="text-xs text-amber-600 mr-2"> {{ $q->time_limit }}s</span>@endif
<p class="text-sm text-slate-700 mt-1 line-clamp-2">{{ $q->question_text }}</p>
</div>
</div>
<div class="flex gap-3 text-sm shrink-0">
<a href="{{ route('admin.quiz-questions.edit',$q) }}" class="text-violet-600 hover:underline">Bearbeiten</a>
<form method="POST" action="{{ route('admin.quiz-questions.destroy',$q) }}" class="inline" onsubmit="return confirm('Frage löschen?')">
@csrf @method('DELETE')
<button class="text-red-500 hover:underline">Löschen</button>
</form>
</div>
</div>
@empty
<div class="bg-slate-50 rounded-xl border border-dashed border-slate-300 p-8 text-center text-slate-400 text-sm">
Noch keine Fragen. Füge bis zu 10 Fragen hinzu.
</div>
@endforelse
</div>
@endsection
@@ -0,0 +1,61 @@
@extends('layouts.admin')
@section('title','Quizzes')
@section('content')
<div class="flex justify-between items-center mb-6" x-data="{showImport:false}">
<h2 class="text-lg font-semibold text-slate-700">Quizzes</h2>
<div class="flex gap-2">
<button @click="showImport=!showImport"
class="flex items-center gap-1.5 bg-slate-100 hover:bg-slate-200 text-slate-700 px-3 py-2 rounded-lg text-sm font-medium"
:class="showImport && 'bg-amber-100 text-amber-700'"> Import</button>
<a href="{{ route('admin.quizzes.create') }}" class="bg-violet-600 hover:bg-violet-700 text-white px-4 py-2 rounded-lg text-sm font-medium">+ Neues Quiz</a>
</div>
<div x-show="showImport" x-cloak class="w-full mt-3">
<div class="bg-amber-50 border border-amber-200 rounded-xl p-4">
<p class="text-xs text-slate-500 mb-3">JSON im gleichen Format wie der Export. Erstellt immer ein neues Quiz.</p>
<form method="POST" action="{{ route('admin.quizzes.import') }}" enctype="multipart/form-data" class="flex gap-3">
@csrf
<input type="file" name="file" accept=".json" required
class="flex-1 text-sm text-slate-600 file:mr-3 file:py-1.5 file:px-3 file:rounded-lg file:border-0 file:text-sm file:font-medium file:bg-violet-100 file:text-violet-700">
<button type="submit" class="bg-violet-600 hover:bg-violet-700 text-white px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap">Importieren</button>
</form>
</div>
</div>
</div>
<div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-slate-50 border-b border-slate-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-slate-600">Fach</th>
<th class="text-left px-4 py-3 font-medium text-slate-600">Titel</th>
<th class="text-center px-4 py-3 font-medium text-slate-600">Fragen</th>
<th class="text-center px-4 py-3 font-medium text-slate-600">Status</th>
<th class="px-4 py-3"></th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100">
@forelse($quizzes as $quiz)
<tr class="hover:bg-slate-50">
<td class="px-4 py-3 text-slate-600">{{ $quiz->subject->icon }} {{ $quiz->subject->name }}</td>
<td class="px-4 py-3 font-medium text-slate-800">{{ $quiz->title }}</td>
<td class="px-4 py-3 text-center">
<span class="{{ $quiz->questions_count < 10 ? 'text-amber-600' : 'text-green-600' }} font-medium">
{{ $quiz->questions_count }}/10
</span>
</td>
<td class="px-4 py-3 text-center">{{ $quiz->active ? '✅' : '⏸️' }}</td>
<td class="px-4 py-3 text-right whitespace-nowrap">
<a href="{{ route('admin.quizzes.export',$quiz) }}" class="text-slate-500 hover:text-slate-700 mr-3 text-xs"> Export</a>
<a href="{{ route('admin.quizzes.edit',$quiz) }}" class="text-violet-600 hover:underline mr-3">Bearbeiten</a>
<form method="POST" action="{{ route('admin.quizzes.destroy',$quiz) }}" class="inline" onsubmit="return confirm('Quiz löschen?')">
@csrf @method('DELETE')
<button class="text-red-500 hover:underline">Löschen</button>
</form>
</td>
</tr>
@empty
<tr><td colspan="5" class="px-4 py-10 text-center text-slate-400">Noch keine Quizzes. <a href="{{ route('admin.quizzes.create') }}" class="text-violet-600 hover:underline">Erstes Quiz erstellen </a></td></tr>
@endforelse
</tbody>
</table>
</div>
@endsection