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 all 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,
];
});
}
}
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;
}
}
}
48 changes: 24 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion database/factories/GradeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GradeFactory extends Factory
public function definition(): array
{
return [
"value" => fake()->numberBetween(2, 5),
"value" => fake()->boolean ? fake()->numberBetween(2, 5) : null,
"status" => fake()->boolean,
"student_id" => Student::factory(),
"grade_column_id" => GradeColumn::factory(),
Expand Down
2 changes: 1 addition & 1 deletion database/factories/SemesterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SemesterFactory extends Factory
public function definition(): array
{
return [
"name" => "Semestr" . fake()->numberBetween(1, 7),
"name" => "Semestr " . fake()->numberBetween(1, 7),
"active" => fake()->boolean,
];
}
Expand Down
35 changes: 35 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace Database\Seeders;

use App\Models\CourseSemester;
use App\Models\Grade;
use App\Models\GradeColumn;
use App\Models\Group;
use App\Models\Section;
use App\Models\SectionSettings;
use App\Models\Setting;
use App\Models\Student;
use App\Models\User;
use Illuminate\Database\Seeder;

Expand All @@ -24,5 +29,35 @@ public function run(): void
"counters_enabled" => true,
"contact_enabled" => true,
]);
$this->seedGrades();
}

protected function seedGrades(): void
{
$courses = CourseSemester::factory(5)->create();
$students = Student::factory(100)->create();

foreach ($courses as $course) {
$groups = Group::factory(fake()->numberBetween(2, 6))->create([
"course_semester_id" => $course->id,
]);

/** @var Group $group */
foreach ($groups as $group) {
$group->students()->sync($students->random(15));
$columns = GradeColumn::factory(15)->create([
"group_id" => $group->id,
]);

foreach ($group->students as $student) {
foreach ($columns as $column) {
Grade::factory()->create([
"grade_column_id" => $column->id,
"student_id" => $student->id,
]);
}
}
}
}
}
}
12 changes: 7 additions & 5 deletions resources/js/Pages/Dashboard/CourseSemester/Grade/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const props = defineProps({
search: String,
total: Number,
})
const showName = ref(false)
const showModal = ref(false)
const showEditForm = ref(false)
const showCreateForm = ref(false)
Expand Down Expand Up @@ -71,10 +70,11 @@ function updateGrade(gradeColumnId, studentId, value, status) {
})
}

function createGrade(gradeColumnId, studentId, status) {
function createGrade(gradeColumnId, studentId, status, value) {
Inertia.post(`/dashboard/semester-courses/${props.course.data.id}/groups/${props.group.id}/grades/${gradeColumnId}/store`, {
status: status,
student_id: studentId,
value: value,
}, {
preserveScroll: true,
})
Expand Down Expand Up @@ -221,9 +221,11 @@ watch(searchForm, debounce(() => {
</template>
<template #body>
<TableRow v-for="student in students.data" :key="student.id">
<TableCell class="h-[70px] w-[120px] min-w-[120px] cursor-pointer border-2" @click="[showName = !showName]">
<span v-if="!showName">{{ student.index_number }}</span>
<span v-else>{{ student.first_name }} {{ student.surname }}</span>
<TableCell class="h-[70px] w-[120px] min-w-[120px] cursor-pointer flex-row border-2">
<div class="font-bold">
{{ student.first_name }} {{ student.surname }}
</div>
<div>({{ student.index_number }})</div>
</TableCell>
<GradeCell v-for="column in gradeColumns" :key="column.id"
:grade="column.grades.find(obj => obj.student_id === student.id)" :grade-column="column"
Expand Down
Loading
Loading