added chart for displaying health metrix

This commit is contained in:
ghassan committed 2026-01-24 12:40:01 +03:00
1 parent a325919cd0
commit 87051e179d
22 files changed
+1383 -63

No files matched your search

+96 -1
View File
@@ -6,6 +6,8 @@ use App\Models\User;
use App\Models\UserRelationship;
use App\Models\HealthRecord;
use App\Models\Invoice;
use App\Models\TournamentEvent;
use App\Models\Goal;
use App\Services\FamilyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@@ -55,6 +57,29 @@ class FamilyController extends Controller
// Fetch invoices
$invoices = Invoice::where('student_user_id', $user->id)->orWhere('payer_user_id', $user->id)->with(['student', 'tenant'])->get();
// Fetch tournament data
$tournamentEvents = $user->tournamentEvents()
->with(['performanceResults', 'notesMedia'])
->orderBy('date', 'desc')
->get();
// Calculate award counts
$awardCounts = [
'special' => $tournamentEvents->flatMap->performanceResults->where('medal_type', 'special')->count(),
'1st' => $tournamentEvents->flatMap->performanceResults->where('medal_type', '1st')->count(),
'2nd' => $tournamentEvents->flatMap->performanceResults->where('medal_type', '2nd')->count(),
'3rd' => $tournamentEvents->flatMap->performanceResults->where('medal_type', '3rd')->count(),
];
// Get unique sports for filter
$sports = $tournamentEvents->pluck('sport')->unique()->sort()->values();
// Fetch goals data
$goals = $user->goals()->orderBy('created_at', 'desc')->get();
$activeGoalsCount = $goals->where('status', 'active')->count();
$completedGoalsCount = $goals->where('status', 'completed')->count();
$successRate = $goals->count() > 0 ? round(($completedGoalsCount / $goals->count()) * 100) : 0;
// Pass user directly and a flag to indicate it's the current user's profile
return view('family.show', [
'relationship' => (object)[
@@ -67,6 +92,13 @@ class FamilyController extends Controller
'healthRecords' => $healthRecords,
'comparisonRecords' => $comparisonRecords,
'invoices' => $invoices,
'tournamentEvents' => $tournamentEvents,
'awardCounts' => $awardCounts,
'sports' => $sports,
'goals' => $goals,
'activeGoalsCount' => $activeGoalsCount,
'completedGoalsCount' => $completedGoalsCount,
'successRate' => $successRate,
]);
}
@@ -234,7 +266,30 @@ class FamilyController extends Controller
// Fetch invoices for the dependent
$invoices = Invoice::where('student_user_id', $relationship->dependent->id)->orWhere('payer_user_id', $relationship->dependent->id)->with(['student', 'tenant'])->get();
return view('family.show', compact('relationship', 'latestHealthRecord', 'healthRecords', 'comparisonRecords', 'invoices'));
// Fetch tournament data for the dependent
$tournamentEvents = $relationship->dependent->tournamentEvents()
->with(['performanceResults', 'notesMedia'])
->orderBy('date', 'desc')
->get();
// Calculate award counts
$awardCounts = [
'special' => $tournamentEvents->flatMap->performanceResults->where('medal_type', 'special')->count(),
'1st' => $tournamentEvents->flatMap->performanceResults->where('medal_type', '1st')->count(),
'2nd' => $tournamentEvents->flatMap->performanceResults->where('medal_type', '2nd')->count(),
'3rd' => $tournamentEvents->flatMap->performanceResults->where('medal_type', '3rd')->count(),
];
// Get unique sports for filter
$sports = $tournamentEvents->pluck('sport')->unique()->sort()->values();
// Fetch goals data for the dependent
$goals = $relationship->dependent->goals()->orderBy('created_at', 'desc')->get();
$activeGoalsCount = $goals->where('status', 'active')->count();
$completedGoalsCount = $goals->where('status', 'completed')->count();
$successRate = $goals->count() > 0 ? round(($completedGoalsCount / $goals->count()) * 100) : 0;
return view('family.show', compact('relationship', 'latestHealthRecord', 'healthRecords', 'comparisonRecords', 'invoices', 'tournamentEvents', 'awardCounts', 'sports', 'goals', 'activeGoalsCount', 'completedGoalsCount', 'successRate'));
}
/**
@@ -386,6 +441,7 @@ class FamilyController extends Controller
{
$validated = $request->validate([
'recorded_at' => 'required|date',
'height' => 'nullable|numeric|min:50|max:250',
'weight' => 'nullable|numeric|min:0|max:999.9',
'body_fat_percentage' => 'nullable|numeric|min:0|max:100',
'bmi' => 'nullable|numeric|min:0|max:100',
@@ -456,6 +512,7 @@ class FamilyController extends Controller
{
$validated = $request->validate([
'recorded_at' => 'required|date',
'height' => 'nullable|numeric|min:50|max:250',
'weight' => 'nullable|numeric|min:0|max:999.9',
'body_fat_percentage' => 'nullable|numeric|min:0|max:100',
'bmi' => 'nullable|numeric|min:0|max:100',
@@ -520,6 +577,44 @@ class FamilyController extends Controller
->with('success', 'Health record updated successfully.');
}
/**
* Update the specified goal.
*
* @param \Illuminate\Http\Request $request
* @param int $goalId
* @return \Illuminate\Http\JsonResponse
*/
public function updateGoal(Request $request, $goalId)
{
$user = Auth::user();
// Find the goal
$goal = Goal::findOrFail($goalId);
// Check if user is authorized to update this goal
if ($goal->user_id !== $user->id) {
// Check if user is guardian of the goal owner
$relationship = UserRelationship::where('guardian_user_id', $user->id)
->where('dependent_user_id', $goal->user_id)
->first();
if (!$relationship) {
return response()->json(['success' => false, 'message' => 'Unauthorized'], 403);
}
}
// Validate the request
$validated = $request->validate([
'current_progress_value' => 'required|numeric|min:0',
'status' => 'required|in:active,completed',
]);
// Update the goal
$goal->update($validated);
return response()->json(['success' => true, 'message' => 'Goal updated successfully']);
}
/**
* Remove the specified family member from storage.
*
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Goal extends Model
{
protected $fillable = [
'user_id',
'title',
'description',
'start_date',
'target_date',
'current_progress_value',
'target_value',
'status',
'priority_level',
'unit',
'icon_type',
];
protected $casts = [
'start_date' => 'date',
'target_date' => 'date',
'current_progress_value' => 'decimal:2',
'target_value' => 'decimal:2',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function getProgressPercentageAttribute(): float
{
if ($this->target_value == 0) {
return 0;
}
return min(100, ($this->current_progress_value / $this->target_value) * 100);
}
public function getIsCompletedAttribute(): bool
{
return $this->status === 'completed' || $this->current_progress_value >= $this->target_value;
}
}
+2
View File
@@ -11,6 +11,7 @@ class HealthRecord extends Model
protected $fillable = [
'user_id',
'height',
'recorded_at',
'weight',
'body_fat_percentage',
@@ -26,6 +27,7 @@ class HealthRecord extends Model
protected $casts = [
'recorded_at' => 'date',
'height' => 'decimal:2',
'weight' => 'decimal:2',
'body_fat_percentage' => 'decimal:2',
'bmi' => 'decimal:2',
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class NotesMedia extends Model
{
protected $fillable = [
'tournament_event_id',
'note_text',
'media_link',
];
public function tournamentEvent(): BelongsTo
{
return $this->belongsTo(TournamentEvent::class);
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PerformanceResult extends Model
{
protected $fillable = [
'tournament_event_id',
'description',
'medal_type',
'points',
];
public function tournamentEvent(): BelongsTo
{
return $this->belongsTo(TournamentEvent::class);
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class TournamentEvent extends Model
{
protected $fillable = [
'user_id',
'title',
'type',
'sport',
'date',
'time',
'location',
'participants_count',
];
protected $casts = [
'date' => 'date',
'time' => 'datetime:H:i',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function performanceResults(): HasMany
{
return $this->hasMany(PerformanceResult::class);
}
public function notesMedia(): HasMany
{
return $this->hasMany(NotesMedia::class);
}
}
+16
View File
@@ -226,6 +226,22 @@ class User extends Authenticatable
return $this->hasMany(HealthRecord::class);
}
/**
* Get the tournament events for the user.
*/
public function tournamentEvents(): HasMany
{
return $this->hasMany(TournamentEvent::class);
}
/**
* Get the goals for the user.
*/
public function goals(): HasMany
{
return $this->hasMany(Goal::class);
}
/**
* Send the email verification notification.
* Override to prevent sending the default Laravel notification.