File "ClassSection.php"

Full Path: /home/trinadezambia/public_html/admin_panel/app/Models/ClassSection.php
File size: 6.25 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Models;

use App\Services\CachingService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use App\Traits\DateFormatTrait;

class ClassSection extends Model
{
    use SoftDeletes;
    use HasFactory, DateFormatTrait;

    protected $fillable = ['class_id', 'section_id', 'class_teacher_id', 'school_id', 'medium_id'];
    protected $appends = ['name', 'full_name'];
    protected $hidden = ['created_at', 'updated_at'];

    public function class()
    {
        return $this->belongsTo(ClassSchool::class)->withTrashed();
    }

    public function shift()
    {
        return $this->belongsTo(Shift::class)->withTrashed();
    }

    public function section()
    {
        return $this->belongsTo(Section::class)->withTrashed();
    }

    public function medium()
    {
        return $this->belongsTo(Mediums::class)->withTrashed();
    }

    public function class_teachers()
    {
        return $this->hasMany(ClassTeacher::class, 'class_section_id');
    }

    public function announcement()
    {
        return $this->morphMany(Announcement::class, 'table');
    }

    public function subject_teachers()
    {
        return $this->hasMany(SubjectTeacher::class);
    }

    public function timetable()
    {
        return $this->hasMany(Timetable::class)->orderBy('start_time');
    }

    public function scopeClassTeacher($query)
    {
        $user = Auth::user();
        if ($user->hasRole('Teacher')) {
            return $query->WhereHas('class_teachers', function ($q) use ($user) {
                $q->where('teacher_id', $user->id)
                ->where('session_year_id', app(CachingService::class)->getSessionYear()->id);
            });
        }
        return $query;
    }

    public function subjects()
    {
        return $this->belongsToMany(Subject::class, ClassSubject::class, 'class_id', 'subject_id', 'class_id')->withPivot('id as class_subject_id', 'semester_id')->withTrashed();
    }

    public function students()
    {
        return $this->hasMany(Students::class, 'class_section_id')->withTrashed()->owner();
    }

    /**
     * Get all of the attendance for the ClassSection
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function attendance()
    {
        return $this->hasMany(Attendance::class);
    }


    public function scopeOwner($query)
    {
        if (Auth::user() && Auth::user()->school_id) {
            if (Auth::user()->hasRole('School Admin')) {
                return $query->where('school_id', Auth::user()->school_id);
            }

            if (Auth::user()->hasRole('Teacher')) {
                $sessionYearId = app(CachingService::class)->getSessionYear()->id;
                $currentSemester = app(CachingService::class)->getDefaultSemesterData();
                $subjectTeacher = SubjectTeacher::where(['teacher_id' => Auth::user()->id, 'session_year_id' => $sessionYearId])->whereHas('class_subject', function ($q) use ($currentSemester) {
                    (!empty($currentSemester)) ? $q->where('semester_id', $currentSemester->id)->orWhereNull('semester_id') : $q->orWhereNull('semester_id');
                })->pluck('class_section_id');

                $classTeacher = ClassTeacher::where(['teacher_id' => Auth::user()->id, 'session_year_id' => $sessionYearId])->pluck('class_section_id');
                return $query->whereIn('id', array_merge($subjectTeacher->toArray(), $classTeacher->toArray()));
            }

            if (Auth::user()->hasRole('Student')) {
                return $query->where('school_id', Auth::user()->school_id);
            }
            return $query->where('school_id', Auth::user()->school_id);
        }
        if (Auth::user() && !Auth::user()->school_id) {
            if (Auth::user()->hasRole('Super Admin')) {
                return $query;
            }
            return $query;
        }

        return $query;
    }

    public function getNameAttribute()
    {
        $name = '';
        if ($this->relationLoaded('class')) {
            $name .= $this->class->name;
        }
        if ($this->relationLoaded('class.stream')) {
            $name .= !empty($this->class->stream->name) ? ' (' . $this->class->stream->name . ') ' : '';
        }
        if ($this->relationLoaded('section') && $this->section) {
            $name .= ' ' . $this->section->name;
        }
        // Add Shift Name
        if ($this->relationLoaded('class') && $this->class->relationLoaded('shift') && $this->class->shift && !empty($this->class->shift->name)) {
            $name .= ' (' . $this->class->shift->name . ')';
        }
        return $name;
    }

    public function getFullNameAttribute()
    {
        $name = '';
        if ($this->relationLoaded('class')) {
            $name .= $this->class->name;
        }

        if ($this->relationLoaded('section') && $this->section) {
            $name .= ' - ' . $this->section->name;
        }

        if ($this->relationLoaded('medium')) {
            $name .= ' ' . $this->medium->name;
        }

        if ($this->relationLoaded('class') && $this->class->relationLoaded('stream') && !empty($this->class->stream->name)) {
            $name .= ' (' . $this->class->stream->name . ')';
        }

        // Add Shift Name - check both class->shift and direct shift relationship
        if ($this->relationLoaded('class') && $this->class->relationLoaded('shift') && $this->class->shift && !empty($this->class->shift->name)) {
            $name .= ' (' . $this->class->shift->name . ')';
        } elseif ($this->relationLoaded('shift') && $this->shift && !empty($this->shift->name)) {
            $name .= ' (' . $this->shift->name . ')';
        }
        return $name;
    }

    /**
     * Get the class_subject that owns the ClassSection
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function class_subject()
    {
        return $this->belongsTo(ClassSubject::class, 'class_id', 'class_id');
    }

    public function getCreatedAtAttribute()
    {
        return $this->formatDateValue($this->getRawOriginal('created_at'));
    }

    public function getUpdatedAtAttribute()
    {
        return $this->formatDateValue($this->getRawOriginal('updated_at'));
    }
}