Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into #24-news
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
#	routes/web.php
  • Loading branch information
krzysztofrewak committed Aug 19, 2024
2 parents 73cfb10 + 14f66de commit 802bdbd
Show file tree
Hide file tree
Showing 27 changed files with 867 additions and 309 deletions.
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,
];
});
}
}
17 changes: 13 additions & 4 deletions app/Http/Controllers/Dashboard/GradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ public function storeGrade(UpdateGrade $request, CourseSemester $course, Group $

public function updateGrade(UpdateGrade $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse
{
$gradeColumn->grades()
$grade = $gradeColumn->grades()
->where("student_id", $request->get("student_id"))
->update($request->getData());
->first();

return redirect()->back()
->with("success", "Zaktualizowano ocenę");
if ($grade) {
$grade->update($request->getData());

return redirect()->back()
->with("success", "Zaktualizowano ocenę");
}

$gradeColumn->grades()
->create($request->getData());

return redirect()->back();
}

public function destroy(CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse
Expand Down
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(),
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/UpdateGrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getData(): array
{
return [
"student_id" => $this->get("student_id"),
"status" => $this->boolean("status"),
"status" => $this->get("status") === null ? null : $this->boolean("status"),
"value" => $this->get("value"),
];
}
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models;

use App\Observers\GradeObserver;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -40,4 +41,9 @@ public function student(): BelongsTo
{
return $this->belongsTo(Student::class);
}

protected static function booted(): void
{
self::observe(GradeObserver::class);
}
}
17 changes: 17 additions & 0 deletions app/Observers/GradeObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Observers;

use App\Models\Grade;

class GradeObserver
{
public function updating(Grade $grade): void
{
if ($grade->status === true && $grade->getOriginal("status") === false) {
$grade->status = null;
}
}
}
Loading

0 comments on commit 802bdbd

Please sign in to comment.