Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#29 - grades page #70

Merged
merged 19 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/DTOs/StudentData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\DTOs;

use App\Models\Grade;
use App\Models\GradeColumn;
use App\Models\Student;
use Illuminate\Support\Collection;

readonly class StudentData
{
public function __construct(
public string $student,
public Collection $grades,
) {}

public static function fromModels(Student $student, Student $studentByIndex, Collection $gradeColumns): self
{
return new self(
student: $student->id === $studentByIndex->id ? $student->index_number : "",
grades: self::prepareGrades($student, $gradeColumns),
);
}

public static function prepareGrades(Student $student, Collection $gradeColumns): Collection
{
return $gradeColumns->map(function (GradeColumn $column) use ($student): array {
/** @var Grade $grade */
$grade = Grade::query()
->where("grade_column_id", $column->id)
->where("student_id", $student->id)
->first();

return $grade
? [
"present" => $grade->status,
"value" => $grade->value,
]
: [
"present" => null,
"value" => null,
];
});
}
}
75 changes: 75 additions & 0 deletions app/Http/Controllers/Public/GradeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Public;

use App\DTOs\StudentData;
use App\Http\Controllers\Controller;
use App\Models\CourseSemester;
use App\Models\Group;
use App\Models\Semester;
use App\Models\Student;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\Request;
use Inertia\Response;

class GradeController extends Controller
{
public function __invoke(Request $request, ?Semester $semester = null, ?CourseSemester $course = null, ?Group $group = null, ?string $index = null): Response
{
$courses = [];
$groups = [];
$gradeColumns = [];
$students = [];
$studentByIndex = null;

if ($semester) {
$courses = $semester
->courses()
->with("course", fn(BelongsTo $query): BelongsTo => $query->select(["name", "id"]))
->get();
}

if ($course) {
$groups = $course
->groups()
->get(["name", "id"]);
}

if ($group && $index) {
$studentByIndex = $group->students()
->where("index_number", $index)
->first();

if ($studentByIndex?->exists()) {
$gradeColumns = $group
->gradeColumns()
->where("active", true)
->orderBy("priority")
->get();
$students = $group->students()
->whereNot("index_number", $index)
->inRandomOrder()
->take(8)
->get()
->push($studentByIndex)
->sortBy("index_number")
->map(fn(Student $student): StudentData => StudentData::fromModels($student, $studentByIndex, $gradeColumns));
}
}

return inertia("Public/Grade", [
"semesters" => Semester::query()->get(["name", "id"]),
"semester" => $semester,
"courses" => $courses,
"course" => $course,
"groups" => $groups,
"group" => $group,
"index" => $index ?? "",
"gradeColumns" => $gradeColumns,
"students" => $students,
"indexExists" => $studentByIndex?->exists(),
]);
}
}
Loading
Loading