Files
lernapp/app/Http/Controllers/Child/LearnController.php
T
root 44f281514b 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
2026-05-06 07:19:17 +00:00

75 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\Child;
use App\Http\Controllers\Controller;
use App\Models\Subject;
use App\Models\Question;
use App\Models\QuestionAttempt;
use Illuminate\Http\Request;
class LearnController extends Controller {
public function subjects() {
$user = auth()->user();
$subjects = Subject::all()->map(function($s) use ($user) {
$s->correct = QuestionAttempt::whereHas('question', fn($q) => $q->where('subject_id',$s->id))
->where('user_id',$user->id)->where('is_correct',true)->count();
$s->total = $s->questions()->where('active',true)->count();
return $s;
});
return view('child.learn.subjects', compact('subjects'));
}
public function quiz(Subject $subject) {
$user = auth()->user();
// Bevorzuge Fragen die heute noch nicht beantwortet wurden
$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()
->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.');
}
// Optionen mischen
$question->answerOptions = $question->answerOptions->shuffle();
return view('child.learn.quiz', compact('subject','question'));
}
public function answer(Request $r, Subject $subject) {
$r->validate(['question_id'=>'required|exists:questions,id','answer'=>'required']);
$user = auth()->user();
$question = Question::with('answerOptions')->findOrFail($r->question_id);
$correct = $question->answerOptions->where('is_correct',true)->first();
$isRight = (string)$r->answer === (string)$correct->id;
$earned = 0;
if ($isRight) {
$earned = $question->points_value;
// Streak-Bonus: 3+ in Folge = +5
$streak = $user->currentStreak();
if ($streak >= 2) $earned += 5;
$user->increment('points', $earned);
}
QuestionAttempt::create([
'user_id' => $user->id,
'question_id' => $question->id,
'is_correct' => $isRight,
'points_earned'=> $earned,
]);
$newStreak = $user->fresh()->currentStreak();
return view('child.learn.result', compact('subject','question','correct','isRight','earned','newStreak'));
}
}