Files
lernapp/app/Models/User.php
T

36 lines
1.7 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
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 $hidden = ['password','remember_token'];
protected $casts = ['email_verified_at' => 'datetime', 'password' => 'hashed'];
public function isAdmin(): bool { return $this->role === 'admin'; }
public function isChild(): bool { return $this->role === 'child'; }
public function attempts() { return $this->hasMany(QuestionAttempt::class); }
public function redemptions() { return $this->hasMany(RewardRedemption::class); }
public function level(): array {
$p = $this->points;
if ($p >= 600) return ['label' => 'Meister', 'icon' => '👑', 'color' => 'text-yellow-500', 'next' => null, 'progress' => 100];
if ($p >= 300) return ['label' => 'Wissensprofi', 'icon' => '🌟', 'color' => 'text-purple-500', 'next' => 600, 'progress' => (int)(($p-300)/3)];
if ($p >= 100) return ['label' => 'Lernstar', 'icon' => '⭐', 'color' => 'text-blue-500', 'next' => 300, 'progress' => (int)(($p-100)/2)];
return ['label' => 'Anfänger', 'icon' => '🌱', 'color' => 'text-green-500', 'next' => 100, 'progress' => $p];
}
public function currentStreak(): int {
$last = $this->attempts()->latest('id')->take(10)->get();
$streak = 0;
foreach ($last as $a) {
if (!$a->is_correct) break;
$streak++;
}
return $streak;
}
}