Skip to content

Commit

Permalink
Fix more type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yungifez committed Apr 7, 2023
1 parent 666e33d commit 72dd69c
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 91 deletions.
10 changes: 10 additions & 0 deletions app/Exceptions/InvalidClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class InvalidClassException extends Exception
{
//
}
13 changes: 6 additions & 7 deletions app/Http/Livewire/Datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Livewire;

use App\Exceptions\InvalidClassException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -47,17 +48,15 @@ public function mount(string|Builder $model, array $columns, array $filters = []
*
* @param object $model
*
* @throws Exception
*
* @return bool
* @throws \App\Exceptions\InvalidClassException
*/
public function verifyIsModel($model)
public function verifyIsModel($model) : bool
{
if (!is_subclass_of($model, 'Illuminate\Database\Eloquent\Model')) {
throw new \Exception(sprintf('Class %s is not a model', $model), 1);
throw new InvalidClassException(sprintf('Class %s is not a model', get_class( $model)), 1);
}

return 1;
return true;
}

public function BuildPagination()
Expand Down Expand Up @@ -98,7 +97,7 @@ public function addSearchFilter($model)
$query = call_user_func_array([$query, 'orWhereRelation'], [$column['relation'], $column['columnName'] ?? $column['property'], 'LIKE', "%$this->search%"]);
} else {
//filter column
$query = call_user_func_array([$query, 'orWhere'], [$table.'.'.($column['columnName'] ?? $column['property']) ?? 'id', 'LIKE', "%$this->search%"]);
$query = call_user_func_array([$query, 'orWhere'], [$table.'.'.($column['columnName'] ?? $column['property']), 'LIKE', "%$this->search%"]);
}
}

Expand Down
22 changes: 12 additions & 10 deletions app/Models/AcademicYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class AcademicYear extends Model
{
use HasFactory;

public $name;

protected $fillable = [
'start_year',
'stop_year',
'school_id',
];

public function name()
{
return "$this->start_year - $this->stop_year";
}

public function getNameAttribute()
protected function name() : Attribute
{
return "$this->start_year - $this->stop_year";
return Attribute::make(
get: fn() => "$this->start_year - $this->stop_year",
);
}

public function school()
public function school() : BelongsTo
{
return $this->belongsTo(School::class);
}

public function semesters()
public function semesters() : HasMany
{
return $this->hasMany(Semester::class);
}
Expand Down
18 changes: 4 additions & 14 deletions app/Models/Exam.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Exam extends Model
{
Expand Down Expand Up @@ -33,25 +35,13 @@ class Exam extends Model
'publish_result' => 'boolean',
];

public function semester()
public function semester() : BelongsTo
{
return $this->belongsTo(Semester::class);
}

public function examSlots()
public function examSlots() : HasMany
{
return $this->hasMany(ExamSlot::class);
}

//accessor for start date
public function getStartDateAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d');
}

//accessor for stop date
public function getStopDateAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d');
}
}
30 changes: 13 additions & 17 deletions app/Models/School.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class School extends Model
{
Expand All @@ -13,57 +16,50 @@ class School extends Model
'name', 'address', 'code', 'initials', 'phone', 'email',
];

public function classGroups()
/**
* Get all the class groups in the school
*/
public function classGroups() : HasMany
{
return $this->hasMany(ClassGroup::class);
}

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

/**
* Get all of the MyClasses for the School.
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function myClasses()
public function myClasses() : HasManyThrough
{
return $this->hasManyThrough(MyClass::class, ClassGroup::class);
}

/**
* Get the AcademicYears for the School.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function academicYears()
public function academicYears() : HasMany
{
return $this->hasMany(AcademicYear::class);
}

/**
* Get the academicYear associated with the School.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function academicYear()
public function academicYear() : HasOne
{
return $this->hasOne(AcademicYear::class, 'id', 'academic_year_id');
}

/**
* Get the semester associated with the School.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function semester()
public function semester() : HasOne
{
return $this->hasOne(Semester::class, 'id', 'semester_id');
}
Expand Down
13 changes: 5 additions & 8 deletions app/Models/Timetable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Pivot;

class Timetable extends Pivot
Expand All @@ -18,22 +20,17 @@ class Timetable extends Pivot
'my_class_id',
];

public function semester()
public function semester() : BelongsTo
{
return $this->belongsTo(Semester::class);
}

public function MyClass()
public function myClass() : BelongsTo
{
return $this->belongsTo(MyClass::class);
}

/**
* Get all of the timeSlots for the Timetable.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function timeSlots()
public function timeSlots() : HasMany
{
return $this->hasMany(TimetableTimeSlot::class, 'timetable_id');
}
Expand Down
36 changes: 14 additions & 22 deletions app/Services/AcademicYear/AcademicYearService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\AcademicYear;
use App\Services\School\SchoolService;
use Illuminate\Database\Eloquent\Collection;

class AcademicYearService
{
Expand All @@ -19,10 +20,8 @@ public function __construct(SchoolService $schoolService)

/**
* Get all academic years.
*
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function getAllAcademicYears()
public function getAllAcademicYears() : Collection|static
{
return AcademicYear::where('school_id', auth()->user()->school_id)->get();
}
Expand All @@ -31,61 +30,54 @@ public function getAllAcademicYears()
* Get academic year by Id.
*
*@param int $id
*
* @return App\Models\AcademicYear
*/
public function getAcademicYearById($id)
public function getAcademicYearById($id) : AcademicYear
{
return AcademicYear::where('id', $id)->first();
return AcademicYear::find($id);
}

/**
* Create academic year.
*
* @param array|Collection $records
*
* @return AcademicYear
*/
public function createAcademicYear($records)
public function createAcademicYear($records) : AcademicYear
{
$records['school_id'] = auth()->user()->school_id;
$academicYear = AcademicYear::create($records);

return $academicYear;
}

/**
* Update Academic Year.
*
* @param array|Collection $records
*
* @return void
*/
public function updateAcademicYear(AcademicYear $academicYear, $records)
public function updateAcademicYear(AcademicYear $academicYear, $records) : AcademicYear
{
$academicYear->start_year = $records['start_year'];
$academicYear->stop_year = $records['stop_year'];
$academicYear->save();

return $academicYear;
}

/**
* Delete an academic year.
*
*
* @return void
*/
public function deleteAcademicYear(AcademicYear $academicYear)
public function deleteAcademicYear(AcademicYear $academicYear) : bool|null
{
$academicYear->delete();
return $academicYear->delete();
}

/**
* Set academic year as current.one in school.
*
* @param int $academicYearId
* @param int $schoolId
*
* @return void
*/
public function setAcademicYear($academicYearId, $schoolId = null)
public function setAcademicYear($academicYearId, $schoolId = null) : bool
{
if (!isset($schoolId)) {
$schoolId = auth()->user()->school_id;
Expand All @@ -94,6 +86,6 @@ public function setAcademicYear($academicYearId, $schoolId = null)
$school->academic_year_id = $academicYearId;
//set semester id to null
$school->semester_id = null;
$school->save();
return $school->save();
}
}
7 changes: 4 additions & 3 deletions app/Services/Exam/ExamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App\Services\Exam;

use App\Exceptions\EmptyRecordsException;
use App\Models\Exam;
use App\Models\Semester;
use App\Models\Subject;
use App\Models\User;
use App\Models\Subject;
use App\Models\Semester;
use App\Exceptions\EmptyRecordsException;
use Illuminate\Database\Eloquent\Collection;

class ExamService
{
Expand Down
6 changes: 3 additions & 3 deletions app/Traits/MarkTabulationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ trait MarkTabulationTrait
public Collection $students;

/**
* @param Collection<Subjects> $subjects
* @param Collection<Students> $students
* @param Collection<ExamSlots> $examSlots
* @param Collection<int, \App\Models\Subject> $subjects
* @param Collection<int, \App\Models\Student> $students
* @param Collection<int, \App\Models\ExamSlot> $examSlots
*
* @return Collection
*/
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/create-semester-form.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="card">
<div class="card-header">
<h3 class="card-title">Create semester in session {{auth()->user()->school->academicYear->name()}}</h3>
<h3 class="card-title">Create semester in session {{auth()->user()->school->academicYear->name}}</h3>
</div>
<div class="card-body">
<form action="{{route('semesters.store')}}" method="POST" class="md:w-1/2">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/layouts/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<h2 class="text-lg font-bold">{{auth()->user()->name}}</h2>
<p class="text-center">
@isset(auth()->user()->school)
Academic year: {{auth()->user()->school->academicYear?->name()}} <br>
Academic year: {{auth()->user()->school->academicYear?->name}} <br>
Semester: {{auth()->user()->school->semester?->name}}
@endif
</p>
Expand Down
Loading

0 comments on commit 72dd69c

Please sign in to comment.