CourseWork/app/Http/Controllers/LessonController.php
2024-06-22 23:59:16 +04:00

137 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\ScoreEnum;
use App\Enums\TypeLesson;
use App\Http\Requests\LessonPostRequest;
use App\Models\Grade;
use App\Models\Lesson;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class LessonController extends Controller
{
public function gradeList(): View
{
return view('grade-lesson.grades-list', [
'grades' => Grade::all(),
]);
}
/**
* Display a listing of the resource.
*/
public function index(Grade $grade): View
{
if(request()->user()->cannot('viewAny', $grade)) {
abort(403);
}
return view('grade-lesson.index', [
'lessons' => $grade->lessons()->filter()->get(),
'grade' => $grade,
'subjects' => $grade->subjects,
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(Grade $grade): View
{
if(request()->user()->cannot('create', Lesson::class)) {
abort(403);
}
return view('grade-lesson.create', [
'types' => TypeLesson::cases(),
'grade' => $grade,
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(LessonPostRequest $request, Grade $grade): RedirectResponse
{
if(request()->user()->cannot('create', [Lesson::class, $grade])) {
abort(403);
}
$lesson = Lesson::create($request->validated());
$lesson
->students()
->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
return redirect()->route(
'grades.lessons.show', [
$grade,
$lesson,
]
);
}
/**
* Display the specified resource.
*/
public function show(Grade $grade, Lesson $lesson): View
{
if(request()->user()->cannot('view', $lesson)) {
abort(403);
}
return view('grade-lesson.show', [
'lesson' => $lesson,
'grade' => $grade,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Grade $grade, Lesson $lesson): View
{
if(request()->user()->cannot('update', $lesson)) {
abort(403);
}
return view('grade-lesson.edit', [
'lesson' => $lesson,
'grade' => $grade,
'types' => TypeLesson::cases(),
]);
}
/**
* Update the specified resource in storage.
*/
public function update(LessonPostRequest $request, Grade $grade, Lesson $lesson): RedirectResponse
{
if(request()->user()->cannot('update', $lesson)) {
abort(403);
}
return redirect()->route(
'grades.lessons.show',[
$grade,
$lesson->update($request->validated()),
]
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Grade $grade, Lesson $lesson): RedirectResponse
{
if(request()->user()->cannot('update', $lesson)) {
abort(403);
}
$lesson->delete();
return redirect()->route('grades.lessons.index', $grade);
}
}