feat: Fragen nach Fach getrennt mit Tab-Navigation

This commit is contained in:
root
2026-05-05 15:14:45 +00:00
parent 03b59f3e77
commit 9986f69e9e
4 changed files with 67 additions and 36 deletions
@@ -8,15 +8,16 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class QuestionController extends Controller { class QuestionController extends Controller {
public function index(Request $r) { public function index(Request $r) {
$subjects = Subject::all(); $subjects = Subject::withCount(['questions as total', 'activeQuestions as active'])->get();
$query = Question::with('subject')->latest(); $activeSubject = $subjects->firstWhere('id', $r->subject) ?? $subjects->first();
if ($r->filled('subject')) $query->where('subject_id', $r->subject); $questions = Question::where('subject_id', $activeSubject->id)
$questions = $query->paginate(20)->withQueryString(); ->latest()->paginate(25)->withQueryString();
return view('admin.questions.index', compact('questions','subjects')); return view('admin.questions.index', compact('questions','subjects','activeSubject'));
} }
public function create() { public function create(Request $r) {
$subjects = Subject::all(); $subjects = Subject::all();
return view('admin.questions.create', compact('subjects')); $activeSubject = $subjects->firstWhere('id', $r->subject) ?? $subjects->first();
return view('admin.questions.create', compact('subjects','activeSubject'));
} }
public function store(Request $r) { public function store(Request $r) {
$r->validate([ $r->validate([
@@ -2,7 +2,7 @@
@section('title','Neue Frage') @section('title','Neue Frage')
@section('content') @section('content')
<div class="max-w-2xl"> <div class="max-w-2xl">
<a href="{{ route('admin.questions.index') }}" class="text-sm text-slate-500 hover:text-slate-700 mb-4 inline-block"> Zurück</a> <a href="{{ route('admin.questions.index', ['subject'=>$activeSubject->id]) }}" 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" x-data="{type:'multiple_choice'}"> <div class="bg-white rounded-xl shadow-sm border border-slate-200 p-6" x-data="{type:'multiple_choice'}">
<h2 class="font-semibold text-slate-800 mb-5">Neue Frage erstellen</h2> <h2 class="font-semibold text-slate-800 mb-5">Neue Frage erstellen</h2>
<form method="POST" action="{{ route('admin.questions.store') }}" class="space-y-5"> <form method="POST" action="{{ route('admin.questions.store') }}" class="space-y-5">
@@ -11,7 +11,7 @@
<div> <div>
<label class="block text-sm font-medium text-slate-700 mb-1">Fach</label> <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"> <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 @foreach($subjects as $s)<option value="{{ $s->id }}" {{ $s->id==$activeSubject->id?'selected':'' }}>{{ $s->icon }} {{ $s->name }}</option>@endforeach
</select> </select>
</div> </div>
<div> <div>
@@ -2,7 +2,7 @@
@section('title','Frage bearbeiten') @section('title','Frage bearbeiten')
@section('content') @section('content')
<div class="max-w-2xl"> <div class="max-w-2xl">
<a href="{{ route('admin.questions.index') }}" class="text-sm text-slate-500 hover:text-slate-700 mb-4 inline-block"> Zurück</a> <a href="{{ route('admin.questions.index', ['subject'=>$question->subject_id]) }}" 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"> <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> <h2 class="font-semibold text-slate-800 mb-5">Frage bearbeiten</h2>
<form method="POST" action="{{ route('admin.questions.update',$question) }}" class="space-y-5"> <form method="POST" action="{{ route('admin.questions.update',$question) }}" class="space-y-5">
+55 -25
View File
@@ -1,33 +1,54 @@
@extends('layouts.admin') @extends('layouts.admin')
@section('title','Fragen') @section('title','Fragen')
@section('content') @section('content')
<div class="flex flex-wrap gap-3 items-center justify-between mb-6" x-data="{showImport:false}">
<form method="GET" class="flex gap-2"> {{-- Subject Tabs --}}
<select name="subject" class="border border-slate-300 rounded-lg px-3 py-2 text-sm"> <div class="flex gap-1 mb-6 bg-slate-100 p-1 rounded-xl w-fit">
<option value="">Alle Fächer</option> @foreach($subjects as $s)
@foreach($subjects as $s)<option value="{{ $s->id }}" {{ request('subject')==$s->id?'selected':'' }}>{{ $s->icon }} {{ $s->name }}</option>@endforeach <a href="{{ route('admin.questions.index', ['subject'=>$s->id]) }}"
</select> class="flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors
<button class="bg-slate-700 text-white px-4 py-2 rounded-lg text-sm">Filtern</button> {{ $s->id == $activeSubject->id
</form> ? 'bg-white text-slate-800 shadow-sm'
: 'text-slate-500 hover:text-slate-700' }}">
<span>{{ $s->icon }}</span>
<span>{{ $s->name }}</span>
<span class="text-xs {{ $s->id == $activeSubject->id ? 'text-violet-600 font-bold' : 'text-slate-400' }}">
{{ $s->total }}
</span>
</a>
@endforeach
</div>
{{-- Toolbar for active subject --}}
<div class="flex flex-wrap gap-2 items-center justify-between mb-5" x-data="{showImport:false}">
<h2 class="font-semibold text-slate-700 text-base flex items-center gap-2">
<span class="text-xl">{{ $activeSubject->icon }}</span> {{ $activeSubject->name }}
<span class="text-slate-400 font-normal text-sm">({{ $questions->total() }} Fragen)</span>
</h2>
<div class="flex gap-2 flex-wrap"> <div class="flex gap-2 flex-wrap">
{{-- Export --}} <a href="{{ route('admin.questions.export', ['subject'=>$activeSubject->id]) }}"
<a href="{{ route('admin.questions.export', request()->only('subject')) }}" 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="flex items-center gap-1.5 bg-slate-100 hover:bg-slate-200 text-slate-700 px-4 py-2 rounded-lg text-sm font-medium"> Export
Export JSON
</a> </a>
{{-- Import trigger --}}
<button @click="showImport=!showImport" <button @click="showImport=!showImport"
class="flex items-center gap-1.5 bg-slate-100 hover:bg-slate-200 text-slate-700 px-4 py-2 rounded-lg text-sm font-medium"> 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"
Import JSON :class="showImport && 'bg-amber-100 text-amber-700'">
Import
</button> </button>
<a href="{{ route('admin.questions.create') }}" class="bg-violet-600 hover:bg-violet-700 text-white px-4 py-2 rounded-lg text-sm font-medium">+ Neue Frage</a> <a href="{{ route('admin.questions.create', ['subject'=>$activeSubject->id]) }}"
class="bg-violet-600 hover:bg-violet-700 text-white px-4 py-2 rounded-lg text-sm font-medium">
+ Neue Frage
</a>
</div> </div>
{{-- Import panel --}} {{-- Import panel --}}
<div x-show="showImport" x-cloak class="w-full mt-1"> <div x-show="showImport" x-cloak class="w-full mt-1">
<div class="bg-amber-50 border border-amber-200 rounded-xl p-5"> <div class="bg-amber-50 border border-amber-200 rounded-xl p-5">
<h3 class="font-semibold text-slate-800 mb-1">Fragen importieren</h3> <h3 class="font-semibold text-slate-800 mb-1">{{ $activeSubject->name }}-Fragen importieren</h3>
<p class="text-xs text-slate-500 mb-3">JSON-Datei im gleichen Format wie der Export. Bestehende Fragen bleiben erhalten.</p> <p class="text-xs text-slate-500 mb-3">
Alle bestehenden <strong>{{ $activeSubject->name }}</strong>-Fragen werden durch den Import ersetzt.
Andere Fächer bleiben unberührt.
</p>
<form method="POST" action="{{ route('admin.questions.import') }}" enctype="multipart/form-data" class="flex items-center gap-3"> <form method="POST" action="{{ route('admin.questions.import') }}" enctype="multipart/form-data" class="flex items-center gap-3">
@csrf @csrf
<input type="file" name="file" accept=".json" required <input type="file" name="file" accept=".json" required
@@ -39,37 +60,46 @@
</div> </div>
</div> </div>
</div> </div>
{{-- Questions table --}}
<div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden"> <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
<table class="w-full text-sm"> <table class="w-full text-sm">
<thead class="bg-slate-50 border-b border-slate-200"> <thead class="bg-slate-50 border-b border-slate-200">
<tr> <tr>
<th class="text-left px-4 py-3 font-medium text-slate-600">Frage</th> <th class="text-left px-4 py-3 font-medium text-slate-600">Frage</th>
<th class="text-left px-4 py-3 font-medium text-slate-600">Fach</th>
<th class="text-center px-4 py-3 font-medium text-slate-600">Schwierigkeit</th> <th class="text-center px-4 py-3 font-medium text-slate-600">Schwierigkeit</th>
<th class="text-center px-4 py-3 font-medium text-slate-600">Aktiv</th> <th class="text-center px-4 py-3 font-medium text-slate-600 hidden sm:table-cell">Aktiv</th>
<th class="px-4 py-3"></th> <th class="px-4 py-3"></th>
</tr> </tr>
</thead> </thead>
<tbody class="divide-y divide-slate-100"> <tbody class="divide-y divide-slate-100">
@forelse($questions as $q) @forelse($questions as $q)
<tr class="hover:bg-slate-50"> <tr class="hover:bg-slate-50">
<td class="px-4 py-3 text-slate-700 max-w-xs truncate">{{ $q->question_text }}</td> <td class="px-4 py-3 text-slate-700 max-w-xs">
<td class="px-4 py-3"><span class="text-base">{{ $q->subject->icon }}</span> {{ $q->subject->name }}</td> <span class="line-clamp-2">{{ $q->question_text }}</span>
</td>
<td class="px-4 py-3 text-center">{{ $q->difficultyStars() }}</td> <td class="px-4 py-3 text-center">{{ $q->difficultyStars() }}</td>
<td class="px-4 py-3 text-center">{{ $q->active ? '✅' : '⏸️' }}</td> <td class="px-4 py-3 text-center hidden sm:table-cell">{{ $q->active ? '✅' : '⏸️' }}</td>
<td class="px-4 py-3 text-right whitespace-nowrap"> <td class="px-4 py-3 text-right whitespace-nowrap">
<a href="{{ route('admin.questions.edit',$q) }}" class="text-violet-600 hover:underline mr-3">Bearbeiten</a> <a href="{{ route('admin.questions.edit',$q) }}" class="text-violet-600 hover:underline mr-3">Bearbeiten</a>
<form method="POST" action="{{ route('admin.questions.destroy',$q) }}" class="inline" onsubmit="return confirm('Löschen?')"> <form method="POST" action="{{ route('admin.questions.destroy',$q) }}" class="inline"
onsubmit="return confirm('Frage löschen?')">
@csrf @method('DELETE') @csrf @method('DELETE')
<button class="text-red-500 hover:underline">Löschen</button> <button class="text-red-500 hover:underline">Löschen</button>
</form> </form>
</td> </td>
</tr> </tr>
@empty @empty
<tr><td colspan="5" class="px-4 py-8 text-center text-slate-400">Keine Fragen vorhanden.</td></tr> <tr>
<td colspan="4" class="px-4 py-10 text-center text-slate-400">
Noch keine Fragen für {{ $activeSubject->name }}.
<a href="{{ route('admin.questions.create', ['subject'=>$activeSubject->id]) }}" class="text-violet-600 hover:underline ml-1">Erste Frage erstellen </a>
</td>
</tr>
@endforelse @endforelse
</tbody> </tbody>
</table> </table>
</div> </div>
<div class="mt-4">{{ $questions->links() }}</div> <div class="mt-4">{{ $questions->links() }}</div>
@endsection @endsection