siakad-itm/app/Models/User.php
Yoga Pangestu 692b84cc1b feat: add open semesters badge to academic terms and remove course registration pages
- Added a new column to display open semesters in the academic terms table with badges.
- Removed course registration related pages and components including index, create, and show.
- Updated course registration types to reflect changes in the data structure.
- Refactored admin routes for course registrations to streamline submission handling.
2026-09-01 21:39:46 +07:00

116 lines
3.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Passkeys\PasskeyAuthenticatable;
use Spatie\Permission\Traits\HasRoles;
#[Hidden(['password'])]
#[Guarded(['id', 'last_login_at'])]
#[Appends(['full_name'])]
class User extends Authenticatable
{
use HasFactory, HasRoles, Notifiable, PasskeyAuthenticatable, SoftDeletes, TwoFactorAuthenticatable;
protected function casts(): array
{
return [
'is_active' => 'boolean',
'last_login_at' => 'datetime',
'password' => 'hashed',
'two_factor_confirmed_at' => 'datetime',
];
}
protected function fullName(): Attribute
{
return Attribute::get(fn () => $this->profile?->full_name ?? $this->username);
}
public function profile(): HasOne
{
return $this->hasOne(UserProfile::class);
}
public function student(): HasOne
{
return $this->hasOne(Student::class);
}
public function lecturer(): HasOne
{
return $this->hasOne(Lecturer::class);
}
public function notifications(): HasMany
{
return $this->hasMany(Notification::class);
}
public function feedbacks(): HasMany
{
return $this->hasMany(Feedback::class);
}
public function isAdvisorOf(Student $student): bool
{
return $this->hasRole('dosen') && $this->lecturer && $student->academic_advisor_id === $this->lecturer->id;
}
public function isKaprodiOf(Student $student): bool
{
return $this->hasRole('kaprodi') && $this->lecturer && $student->department?->currentLeader?->lecturer_id === $this->lecturer->id;
}
/**
* Gates page access: the student's academic advisor, the kaprodi of
* their department, or staff roles may view a student's KRS.
*/
public function canAccessCourseRegistrationOf(Student $student): bool
{
if ($this->hasRole('dosen') || $this->hasRole('kaprodi')) {
return $this->isAdvisorOf($student) || $this->isKaprodiOf($student);
}
return true;
}
/**
* Only the student's academic advisor may approve/reject; the kaprodi
* signs but does not decide. Other staff roles retain override access.
*/
public function canReviewCourseRegistrationOf(Student $student): bool
{
if ($this->hasRole('dosen')) {
return $this->isAdvisorOf($student);
}
if ($this->hasRole('kaprodi')) {
return false;
}
return true;
}
/**
* @return array<int, int>
*/
public function ledDepartmentIds(): array
{
return $this->lecturer
? $this->lecturer->leaderships()->whereNull('ended_at')->pluck('department_id')->all()
: [];
}
}