Skip to content

Commit

Permalink
Added academic year student history
Browse files Browse the repository at this point in the history
  • Loading branch information
yungifez committed Sep 27, 2022
1 parent 3233b2e commit 24672a6
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 22 deletions.
Empty file modified .editorconfig
100644 → 100755
Empty file.
Empty file modified .env.example
100644 → 100755
Empty file.
Empty file modified .gitattributes
100644 → 100755
Empty file.
Empty file modified .github/workflows/laravel-tests.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
17 changes: 7 additions & 10 deletions app/Http/Livewire/ResultChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function checkResult(Semester $semester, User $student)
//set name that would be used in view
$this->studentName = $student->name;
// fetch all exams, subjects and exam records for user in semester

$this->exams = $semester->exams()->where('publish_result', true)->get();
if ($this->exams->isEmpty()) {
$this->status = 'There are no exams with published results for now';
Expand All @@ -108,17 +109,13 @@ public function checkResult(Semester $semester, User $student)
//fetch all students exam records in semester
$this->examRecords = app("App\Services\Exam\ExamRecordService")->getAllUserExamRecordInSemester($semester, $student->id);

if ($this->academicYear != auth()->user()->school->academicYear->id) {

$this->subjects = $this->examRecords->load('subject')->map(function ($examRecord)
{
return $examRecord->subject;
});

$this->subjects = $this->subjects->unique()->filter();
}else{
$this->subjects = $student->studentRecord->myClass->subjects;
$academicYearsWithStudentRecords = $student->studentRecord->academicYears()->where('academic_year_id', $this->academicYear)->first();
if (is_null($academicYearsWithStudentRecords)) {
$this->status ="No records this academic year, make sure user has been promoted this year";
$this->preparedResults = false;
return;
}
$this->subjects = $academicYearsWithStudentRecords->studentAcademicYearBasedRecords->class->subjects;

if ($this->subjects->isEmpty()) {
$this->status ="Subjects not present";
Expand Down
14 changes: 13 additions & 1 deletion app/Models/AcademicYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\StudentRecord;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class AcademicYear extends Model
{
Expand All @@ -30,4 +32,14 @@ public function semesters()
{
return $this->hasMany(Semester::class);
}

/**
* The studentRecords that belong to the AcademicYear
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function studentRecords(): BelongsToMany
{
return $this->belongsToMany(StudentRecord::class)->as('studentAcademicYearBasedRecords')->using(AcademicYearStudentRecord::class)->withPivot('my_class_id', 'section_id');
}
}
49 changes: 49 additions & 0 deletions app/Models/AcademicYearStudentRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Models;

use App\Models\MyClass;
use App\Models\Section;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AcademicYearStudentRecord extends Pivot
{
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
protected $fillable = ['my_class_id', 'section_id'];

/**
* Get the studentRecord that owns the AcademicYearStudentRecord
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function studentRecord(): BelongsTo
{
return $this->belongsTo(StudentRecord::class, 'student_record_id', 'id');
}

/**
* Get the class that owns the AcademicYearStudentRecord
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function class(): BelongsTo
{
return $this->belongsTo(MyClass::class, 'my_class_id', 'id');
}

/**
* Get the section that owns the AcademicYearStudentRecord
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function section(): BelongsTo
{
return $this->belongsTo(Section::class, 'section_id', 'id');
}
}
2 changes: 1 addition & 1 deletion app/Models/MyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class MyClass extends Model
{
Expand Down
30 changes: 22 additions & 8 deletions app/Models/StudentRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\AcademicYear;
use Illuminate\Database\Eloquent\Model;
use App\Models\AcademicYearStudentRecord;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class StudentRecord extends Model
{
Expand Down Expand Up @@ -39,22 +42,33 @@ public function myClass()
}

/**
* Get the section that owns the StudentRecord.
* Get the user that owns the StudentRecord.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function section()
public function user()
{
return $this->belongsTo(Section::class);
return $this->belongsTo(User::class);
}

/**
* The academicYears that belong to the StudentRecord
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function academicYears(): BelongsToMany
{
return $this->belongsToMany(AcademicYear::class)->as('studentAcademicYearBasedRecords')->using(AcademicYearStudentRecord::class)->withPivot('my_class_id', 'section_id');
}

/**
* Get the user that owns the StudentRecord.
* Get current academic year
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function user()
public function currentAcademicYear()
{
return $this->belongsTo(User::class);
return $this->academicYears()->wherePivot('academic_year_id', $this->user->school->academicYear->id);
}

}
16 changes: 16 additions & 0 deletions app/Services/Student/StudentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public function createStudent($record)
'admission_date' => $record['admission_date'],
]);
DB::commit();

$currentAcademicYear = auth()->user()->school->academicYear;
$student->studentRecord->load("academicYears")->academicYears()->sync([$currentAcademicYear->id => [
'my_class_id' => $record['my_class_id'],
'section_id' => $record['section_id'],
]]);
session()->flash('success', 'Student Created Successfully');
}

Expand Down Expand Up @@ -171,13 +177,18 @@ public function promoteStudents($records)
return session()->flash('danger', 'No students to promote');
}

$currentAcademicYear = auth()->user()->school->academicYear;
// update each student's class
foreach ($students as $student) {
if (in_array($student->id, $records['student_id'])) {
$student->studentRecord()->update([
'my_class_id' => $records['new_class_id'],
'section_id' => $records['new_section_id'],
]);
$student->studentRecord->load("academicYears")->academicYears()->syncWithoutDetaching([$currentAcademicYear->id => [
'my_class_id' => $records['new_class_id'],
'section_id' => $records['new_section_id'],
]]);
}
}

Expand Down Expand Up @@ -218,6 +229,11 @@ public function resetPromotion($promotion)
'my_class_id' => $promotion->old_class_id,
'section_id' => $promotion->old_section_id,
]);

$student->studentRecord->load("academicYears")->academicYears()->syncWithoutDetaching([$currentAcademicYear->id => [
'my_class_id' => $promotion->old_class_id,
'section_id' => $promotion->old_section_id,
]]);
}

$promotion->delete();
Expand Down
2 changes: 1 addition & 1 deletion config/adminlte.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
*/

'preloader' => [
'enabled' => true,
'enabled' => false,
'img' => [
'path' => env('LOGO_PATH', 'vendor/adminlte/dist/img/AdminLTELogo.png'),
'alt' => env('APP_NAME'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('academic_year_student_record', static function (Blueprint $table) {
$table->id();
$table->foreignId("academic_year_id")->notNullable()->onDelete("cascade")->onUpdate("cascade");
$table->foreignId("student_record_id")->notNullable()->onDelete("cascade")->onUpdate("cascade");
$table->foreignId("my_class_id")->notNullable()->onDelete("cascade")->onUpdate("cascade");
$table->foreignId("section_id")->notNullable()->onDelete("cascade")->onUpdate("cascade");
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('academic_year_student_record');
}
};
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
Route::post('semesters/set', ['App\Http\Controllers\SemesterController', 'setSemester'])->name('semesters.set-semester');
});

Route::middleware(['App\Http\Middleware\EnsureSemesterIsSet'])->group(function () {
Route::middleware(['App\Http\Middleware\EnsureSemesterIsSet', 'App\Http\Middleware\EnsureStudentHasAcademicYearRecord'])->group(function () {
//syllabi route
Route::resource('syllabi', SyllabusController::class);

Expand Down

0 comments on commit 24672a6

Please sign in to comment.