Add grade/Klasse system: assign class levels to users, questions, and quizzes
- users.grade: set per child in admin (Klasse 1–10) - quizzes.grade, questions.grade: optional target class (null = all) - Children only see content matching their grade or without grade set - Admin views show grade badge in user list, quiz list, questions list - Quiz create/edit and user create/edit have Klasse dropdown
This commit is contained in:
@@ -18,8 +18,8 @@ class QuizController extends Controller {
|
||||
}
|
||||
|
||||
public function store(Request $r) {
|
||||
$r->validate(['title'=>'required|string|max:120','subject_id'=>'required|exists:subjects,id','description'=>'nullable|string|max:500']);
|
||||
$quiz = Quiz::create($r->only('title','subject_id','description') + ['active'=>true]);
|
||||
$r->validate(['title'=>'required|string|max:120','subject_id'=>'required|exists:subjects,id','description'=>'nullable|string|max:500','grade'=>'nullable|integer|min:1|max:13']);
|
||||
$quiz = Quiz::create($r->only('title','subject_id','description') + ['active'=>true,'grade'=>$r->grade ?: null]);
|
||||
return redirect()->route('admin.quizzes.edit', $quiz)->with('success','Quiz erstellt – jetzt Fragen hinzufügen.');
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ class QuizController extends Controller {
|
||||
}
|
||||
|
||||
public function update(Request $r, Quiz $quiz) {
|
||||
$r->validate(['title'=>'required|string|max:120','subject_id'=>'required|exists:subjects,id','description'=>'nullable|string|max:500']);
|
||||
$quiz->update($r->only('title','subject_id','description') + ['active'=>$r->boolean('active')]);
|
||||
$r->validate(['title'=>'required|string|max:120','subject_id'=>'required|exists:subjects,id','description'=>'nullable|string|max:500','grade'=>'nullable|integer|min:1|max:13']);
|
||||
$quiz->update($r->only('title','subject_id','description') + ['active'=>$r->boolean('active'),'grade'=>$r->grade ?: null]);
|
||||
return back()->with('success','Quiz gespeichert.');
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class UserController extends Controller {
|
||||
'name' => 'required|string|max:60',
|
||||
'email' => 'required|email|unique:users',
|
||||
'password' => 'required|min:6',
|
||||
'grade' => 'nullable|integer|min:1|max:13',
|
||||
]);
|
||||
User::create([
|
||||
'name' => $r->name,
|
||||
@@ -22,6 +23,7 @@ class UserController extends Controller {
|
||||
'password' => Hash::make($r->password),
|
||||
'role' => 'child',
|
||||
'points' => 0,
|
||||
'grade' => $r->grade ?: null,
|
||||
]);
|
||||
return redirect()->route('admin.users.index')->with('success','Kind-Konto erstellt.');
|
||||
}
|
||||
@@ -32,8 +34,9 @@ class UserController extends Controller {
|
||||
'email' => 'required|email|unique:users,email,'.$user->id,
|
||||
'password' => 'nullable|min:6',
|
||||
'points' => 'required|integer|min:0',
|
||||
'grade' => 'nullable|integer|min:1|max:13',
|
||||
]);
|
||||
$user->fill(['name'=>$r->name,'email'=>$r->email,'points'=>$r->points]);
|
||||
$user->fill(['name'=>$r->name,'email'=>$r->email,'points'=>$r->points,'grade'=>$r->grade ?: null]);
|
||||
if ($r->filled('password')) $user->password = Hash::make($r->password);
|
||||
$user->save();
|
||||
return redirect()->route('admin.users.index')->with('success','Gespeichert.');
|
||||
|
||||
@@ -22,14 +22,24 @@ class LearnController extends Controller {
|
||||
$answeredToday = QuestionAttempt::where('user_id',$user->id)
|
||||
->whereDate('created_at', today())
|
||||
->pluck('question_id');
|
||||
$grade = $user->grade;
|
||||
$question = $subject->activeQuestions()
|
||||
->whereNotIn('id', $answeredToday)
|
||||
->where(function($q) use ($grade) {
|
||||
$q->whereNull('grade');
|
||||
if ($grade) $q->orWhere('grade', $grade);
|
||||
})
|
||||
->with('answerOptions')
|
||||
->inRandomOrder()
|
||||
->first();
|
||||
// Falls alle beantwortet: ganz random
|
||||
if (!$question) {
|
||||
$question = $subject->activeQuestions()->with('answerOptions')->inRandomOrder()->first();
|
||||
$question = $subject->activeQuestions()
|
||||
->where(function($q) use ($grade) {
|
||||
$q->whereNull('grade');
|
||||
if ($grade) $q->orWhere('grade', $grade);
|
||||
})
|
||||
->with('answerOptions')->inRandomOrder()->first();
|
||||
}
|
||||
if (!$question) {
|
||||
return redirect()->route('learn.subjects')->with('info','Noch keine Fragen für dieses Fach.');
|
||||
|
||||
@@ -8,7 +8,12 @@ class QuizController extends Controller {
|
||||
|
||||
public function index() {
|
||||
$subjects = Subject::all()->keyBy('id');
|
||||
$grade = auth()->user()->grade;
|
||||
$quizzes = Quiz::with('subject')->where('active',true)
|
||||
->where(function($q) use ($grade) {
|
||||
$q->whereNull('grade');
|
||||
if ($grade) $q->orWhere('grade', $grade);
|
||||
})
|
||||
->withCount('questions')->get()->groupBy('subject_id');
|
||||
$bestScores = QuizAttempt::where('user_id',auth()->id())
|
||||
->where('status','completed')
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
class User extends Authenticatable {
|
||||
use HasFactory, Notifiable;
|
||||
protected $fillable = ['name','email','password','role','points'];
|
||||
protected $fillable = ['name','email','password','role','points','grade'];
|
||||
protected $hidden = ['password','remember_token'];
|
||||
protected $casts = ['email_verified_at' => 'datetime', 'password' => 'hashed'];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user