144 lines
3.5 KiB
PHP
144 lines
3.5 KiB
PHP
<?php
|
|
// app/Models/ActivityLog.php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ActivityLog extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'start_time',
|
|
'end_time',
|
|
'duration_minutes',
|
|
'mood',
|
|
'sleep_hours', // Nilai numerik 1-10 jam
|
|
'activity_date',
|
|
];
|
|
|
|
protected $casts = [
|
|
'activity_date' => 'date',
|
|
'start_time' => 'string',
|
|
'end_time' => 'string',
|
|
'sleep_hours' => 'float',
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function recommendation()
|
|
{
|
|
return $this->hasOne(Recommendation::class, 'activity_log_id');
|
|
}
|
|
|
|
// Hitung durasi otomatis
|
|
public function calculateDuration()
|
|
{
|
|
if ($this->start_time && $this->end_time) {
|
|
$start = \Carbon\Carbon::parse($this->start_time);
|
|
$end = \Carbon\Carbon::parse($this->end_time);
|
|
$this->duration_minutes = $end->diffInMinutes($start);
|
|
}
|
|
}
|
|
|
|
// Helper untuk mendapatkan kualitas tidur
|
|
public function getSleepQualityAttribute()
|
|
{
|
|
if ($this->sleep_hours < 5) return 'Kurang Tidur';
|
|
if ($this->sleep_hours < 7) return 'Kurang Ideal';
|
|
if ($this->sleep_hours <= 9) return 'Ideal';
|
|
return 'Berlebih';
|
|
}
|
|
|
|
public function getMoodColorAttribute()
|
|
{
|
|
return match($this->mood) {
|
|
'Bagus' => 'green',
|
|
'Lumayan' => 'blue',
|
|
'Biasa Saja' => 'gray',
|
|
'Cukup Jenuh' => 'yellow',
|
|
'Jenuh' => 'red',
|
|
default => 'gray'
|
|
};
|
|
}
|
|
public function scopeToday($query)
|
|
{
|
|
return $query->whereDate('activity_date', Carbon::now('Asia/Jakarta')->toDateString());
|
|
}
|
|
|
|
/**
|
|
* Scope untuk 7 hari terakhir
|
|
*/
|
|
public function scopeLastWeek($query)
|
|
{
|
|
return $query->whereDate('activity_date', '>=', Carbon::now('Asia/Jakarta')->subDays(7)->toDateString());
|
|
}
|
|
|
|
/**
|
|
* Scope untuk rentang tanggal tertentu
|
|
*/
|
|
public function scopeDateRange($query, $startDate, $endDate)
|
|
{
|
|
return $query->whereBetween('activity_date', [$startDate, $endDate]);
|
|
}
|
|
|
|
/**
|
|
* Scope untuk filter mood
|
|
*/
|
|
public function scopeByMood($query, $mood)
|
|
{
|
|
return $query->where('mood', $mood);
|
|
}
|
|
|
|
/**
|
|
* Cek apakah aktivitas untuk hari ini
|
|
*/
|
|
public function isToday()
|
|
{
|
|
return Carbon::parse($this->activity_date)->isToday();
|
|
}
|
|
|
|
/**
|
|
* Format tanggal untuk tampilan
|
|
*/
|
|
public function getFormattedDateAttribute()
|
|
{
|
|
return Carbon::parse($this->activity_date)->format('d M Y');
|
|
}
|
|
|
|
/**
|
|
* Format hari untuk tampilan
|
|
*/
|
|
public function getFormattedDayAttribute()
|
|
{
|
|
return Carbon::parse($this->activity_date)->isoFormat('dddd');
|
|
}
|
|
|
|
/**
|
|
* Accessor untuk sleep hours dengan default 7
|
|
*/
|
|
public function getSleepHoursAttribute($value)
|
|
{
|
|
return $value ?? 7;
|
|
}
|
|
|
|
/**
|
|
* Get durasi dalam format jam dan menit
|
|
*/
|
|
public function getDurationFormattedAttribute()
|
|
{
|
|
$hours = floor($this->duration_minutes / 60);
|
|
$minutes = $this->duration_minutes % 60;
|
|
|
|
if ($hours > 0) {
|
|
return $hours . ' jam ' . $minutes . ' menit';
|
|
}
|
|
return $minutes . ' menit';
|
|
}
|
|
} |