CourseWork/app/Http/Controllers/LessonController.php

138 lines
3.3 KiB
PHP
Raw Normal View History

2024-05-08 12:59:29 +04:00
<?php
namespace App\Http\Controllers;
2024-06-16 12:20:48 +04:00
use App\Enums\ScoreEnum;
2024-05-08 13:34:54 +04:00
use App\Enums\TypeLesson;
2024-05-08 12:59:29 +04:00
use App\Http\Requests\LessonPostRequest;
2024-05-08 16:55:32 +04:00
use App\Models\Grade;
2024-05-08 12:59:29 +04:00
use App\Models\Lesson;
2024-06-24 00:51:05 +04:00
use App\Services\LessonService;
2024-05-08 12:59:29 +04:00
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class LessonController extends Controller
{
2024-05-08 16:55:32 +04:00
public function gradeList(): View
{
return view('grade-lesson.grades-list', [
2024-06-16 00:41:31 +04:00
'grades' => Grade::all(),
2024-05-08 16:55:32 +04:00
]);
}
2024-05-08 12:59:29 +04:00
/**
* Display a listing of the resource.
*/
2024-06-24 00:51:05 +04:00
public function index(Grade $grade, LessonService $service): View
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('viewAny', $grade)) {
abort(403);
}
2024-05-08 16:55:32 +04:00
return view('grade-lesson.index', [
2024-06-24 00:51:05 +04:00
'lessons' => $service->getLessons($grade),
2024-05-08 16:55:32 +04:00
'grade' => $grade,
2024-05-13 14:23:21 +04:00
'subjects' => $grade->subjects,
2024-05-08 12:59:29 +04:00
]);
}
/**
* Show the form for creating a new resource.
*/
2024-05-08 16:55:32 +04:00
public function create(Grade $grade): View
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('create', Lesson::class)) {
abort(403);
}
2024-05-08 16:55:32 +04:00
return view('grade-lesson.create', [
2024-05-08 13:34:54 +04:00
'types' => TypeLesson::cases(),
2024-05-08 16:55:32 +04:00
'grade' => $grade,
2024-05-08 13:34:54 +04:00
]);
2024-05-08 12:59:29 +04:00
}
/**
* Store a newly created resource in storage.
*/
2024-05-08 16:55:32 +04:00
public function store(LessonPostRequest $request, Grade $grade): RedirectResponse
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('create', [Lesson::class, $grade])) {
abort(403);
}
2024-06-16 12:20:48 +04:00
$lesson = Lesson::create($request->validated());
$lesson
->students()
->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
2024-05-08 12:59:29 +04:00
return redirect()->route(
2024-05-08 16:55:32 +04:00
'grades.lessons.show', [
$grade,
2024-06-16 12:20:48 +04:00
$lesson,
2024-05-08 16:55:32 +04:00
]
2024-05-08 12:59:29 +04:00
);
}
/**
* Display the specified resource.
*/
2024-05-08 16:55:32 +04:00
public function show(Grade $grade, Lesson $lesson): View
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('view', $lesson)) {
abort(403);
}
2024-05-08 16:55:32 +04:00
return view('grade-lesson.show', [
2024-05-08 12:59:29 +04:00
'lesson' => $lesson,
2024-05-08 16:55:32 +04:00
'grade' => $grade,
2024-05-08 12:59:29 +04:00
]);
}
/**
* Show the form for editing the specified resource.
*/
2024-05-08 16:55:32 +04:00
public function edit(Grade $grade, Lesson $lesson): View
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('update', $lesson)) {
abort(403);
}
2024-05-08 16:55:32 +04:00
return view('grade-lesson.edit', [
2024-05-08 12:59:29 +04:00
'lesson' => $lesson,
2024-05-08 16:55:32 +04:00
'grade' => $grade,
2024-05-08 13:34:54 +04:00
'types' => TypeLesson::cases(),
2024-05-08 12:59:29 +04:00
]);
}
/**
* Update the specified resource in storage.
*/
2024-05-08 16:55:32 +04:00
public function update(LessonPostRequest $request, Grade $grade, Lesson $lesson): RedirectResponse
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('update', $lesson)) {
abort(403);
}
2024-05-08 12:59:29 +04:00
return redirect()->route(
2024-05-08 16:55:32 +04:00
'grades.lessons.show',[
$grade,
2024-06-16 12:20:48 +04:00
$lesson->update($request->validated()),
2024-05-08 16:55:32 +04:00
]
2024-05-08 12:59:29 +04:00
);
}
/**
* Remove the specified resource from storage.
*/
2024-05-08 16:55:32 +04:00
public function destroy(Grade $grade, Lesson $lesson): RedirectResponse
2024-05-08 12:59:29 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('update', $lesson)) {
abort(403);
}
2024-06-16 12:20:48 +04:00
$lesson->delete();
2024-05-08 12:59:29 +04:00
2024-05-08 16:55:32 +04:00
return redirect()->route('grades.lessons.index', $grade);
2024-05-08 12:59:29 +04:00
}
}