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

#12 - grade columns and grade crud #61

Merged
merged 9 commits into from
Oct 31, 2023
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
102 changes: 102 additions & 0 deletions app/Http/Controllers/Dashboard/GradeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateGrade;
use App\Http\Requests\UpdateGradeColumn;
use App\Http\Resources\CourseSemesterResource;
use App\Models\CourseSemester;
use App\Models\GradeColumn;
use App\Models\Group;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Response;

class GradeController extends Controller
{
public function index(Request $request, CourseSemester $course, Group $group): Response
{
$searchText = $request->query("search");
$students = $group->students()->when(
$searchText !== null,
fn(Builder $query): Builder => $query
->where("first_name", "ILIKE", "%$searchText%")
->orWhere("surname", "ILIKE", "%$searchText%")
->orWhere("index_number", "LIKE", "%$searchText%"),
)
->paginate()
->withQueryString();

return inertia("Dashboard/CourseSemester/Grade/Index", [
"course" => new CourseSemesterResource($course),
"group" => $group,
"total" => $group->students->count(),
"students" => $students,
"search" => $searchText,
"gradeColumns" => $group->gradeColumns()->with("grades")->orderBy("priority")->get(),
]);
}

public function store(UpdateGradeColumn $request, CourseSemester $course, Group $group): RedirectResponse
{
$group->gradeColumns()->create($request->getData());

return redirect()->back()
->with("success", "Dodano kolumnę");
}

public function update(UpdateGradeColumn $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse
{
$gradeColumn->update($request->getData());

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

public function storeGrade(UpdateGrade $request, CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse
{
$gradeColumn->grades()
->create($request->getData());

return redirect()->back();
}

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

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

public function destroy(CourseSemester $course, Group $group, GradeColumn $gradeColumn): RedirectResponse
{
$gradeColumn->delete();

return redirect()->back()
->with("success", "Usunięto kolumnę");
}

public function reorder(CourseSemester $course, Group $group, GradeColumn $gradeColumn, int $down): RedirectResponse
{
$tempColumnOrder = $gradeColumn->priority;
$columnToUpdate = $group->gradeColumns()->where("priority", $down ? "<" : ">", $gradeColumn->priority)->orderBy("priority", $down ? "desc" : "asc")->first();

if ($columnToUpdate) {
$gradeColumn->priority = $columnToUpdate->priority;
$columnToUpdate->priority = $tempColumnOrder;
$gradeColumn->save();
$columnToUpdate->save();
}

return redirect()
->back()
->with(["success" => "Kolejność zaktualizowana"]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function store(Request $request, CourseSemester $course, Group $group): R

public function destroy(CourseSemester $course, Group $group, Student $student): RedirectResponse
{
$student->grades()->where("student_id", $student->id)->get()->each->delete();
$group->students()->detach($student);

return redirect()->back()
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Requests/UpdateGrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateGrade extends FormRequest
{
public function rules(): array
{
return [
"student_id" => ["required", "exists:students,id"],
"value" => ["nullable", "max:10"],
];
}

public function getData(): array
{
return [
"student_id" => $this->get("student_id"),
"status" => $this->boolean("status"),
"value" => $this->get("value"),
];
}
}
25 changes: 25 additions & 0 deletions app/Http/Requests/UpdateGradeColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateGradeColumn extends FormRequest
{
public function rules(): array
{
return [
"name" => ["required", "max:20"],
];
}

public function getData(): array
{
return [
"name" => $this->get("name"),
"active" => $this->boolean("active"),
];
}
}
43 changes: 43 additions & 0 deletions app/Models/Grade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property string $id
* @property ?boolean $status
* @property ?string $value
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Grade extends Model
{
use HasFactory;
use HasUlids;

protected $fillable = [
"status",
"value",
"student_id",
];
protected $casts = [
"status" => "boolean",
];

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

public function student(): BelongsTo
{
return $this->belongsTo(Student::class);
}
}
54 changes: 54 additions & 0 deletions app/Models/GradeColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace App\Models;

use App\Observers\GradeColumnObserver;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;

/**
* @property string $id
* @property string $name
* @property int $priority
* @property bool $active
* @property Carbon $created_at
* @property Carbon $updated_at
* @property-read Group $group
* @property-read Collection<Grade> $grades
*/
class GradeColumn extends Model
{
use HasFactory;
use HasUlids;

protected $fillable = [
"name",
"priority",
"active",
];
protected $casts = [
"active" => "boolean",
];

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

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

protected static function booted(): void
{
self::observe(GradeColumnObserver::class);
}
}
6 changes: 6 additions & 0 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string $id
Expand Down Expand Up @@ -43,4 +44,9 @@ public function students(): BelongsToMany
{
return $this->belongsToMany(Student::class, "student_group");
}

public function gradeColumns(): HasMany
{
return $this->hasMany(GradeColumn::class);
}
}
8 changes: 7 additions & 1 deletion app/Models/Student.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string $id
Expand All @@ -29,8 +30,13 @@ class Student extends Model
"index_number",
];

public function students(): BelongsToMany
public function groups(): BelongsToMany
{
return $this->belongsToMany(Group::class, "student_group");
}

public function grades(): HasMany
{
return $this->hasMany(Grade::class);
}
}
16 changes: 16 additions & 0 deletions app/Observers/GradeColumnObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Observers;

use App\Models\GradeColumn;

class GradeColumnObserver
{
public function creating(GradeColumn $column): void
{
$maxOrder = $column->group->gradeColumns()->max("priority");
$column->priority = ++$maxOrder;
}
}
21 changes: 21 additions & 0 deletions database/factories/GradeColumnFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Group;
use Illuminate\Database\Eloquent\Factories\Factory;

class GradeColumnFactory extends Factory
{
public function definition(): array
{
return [
"name" => fake()->asciify("***"),
"active" => fake()->boolean,
"priority" => fake()->numberBetween(1, 100),
"group_id" => Group::factory(),
];
}
}
22 changes: 22 additions & 0 deletions database/factories/GradeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\GradeColumn;
use App\Models\Student;
use Illuminate\Database\Eloquent\Factories\Factory;

class GradeFactory extends Factory
{
public function definition(): array
{
return [
"value" => fake()->numberBetween(2, 5),
"status" => fake()->boolean,
"student_id" => Student::factory(),
"grade_column_id" => GradeColumn::factory(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public function up(): void
Schema::create("course_semester", function (Blueprint $table): void {
$table->ulid("id")->primary();
$table->string("course_id")->nullable();
$table->foreign("course_id")->references("id")->on("courses")->onDelete("set null");
$table->foreign("course_id")->references("id")->on("courses")->onDelete("CASCADE");
$table->string("semester_id")->nullable();
$table->foreign("semester_id")->references("id")->on("semesters")->onDelete("set null");
$table->foreign("semester_id")->references("id")->on("semesters")->onDelete("CASCADE");
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function up(): void
$table->string("name");
$table->string("form");
$table->string("course_semester_id")->nullable();
$table->foreign("course_semester_id")->references("id")->on("course_semester")->onDelete("SET NULL");
$table->foreign("course_semester_id")->references("id")->on("course_semester")->onDelete("CASCADE");
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public function up(): void
{
Schema::create("student_group", function (Blueprint $table): void {
$table->string("group_id")->nullable();
$table->foreign("group_id")->references("id")->on("groups")->onDelete("set null");
$table->foreign("group_id")->references("id")->on("groups")->onDelete("CASCADE");
$table->string("student_id")->nullable();
$table->foreign("student_id")->references("id")->on("students")->onDelete("set null");
$table->foreign("student_id")->references("id")->on("students")->onDelete("CASCADE");
$table->primary(["student_id", "group_id"], "student_group_id_primary");
});
}
Expand Down
Loading
Loading