diff --git a/app/Enums/ScoreEnum.php b/app/Enums/ScoreEnum.php new file mode 100644 index 0000000..fd17bbe --- /dev/null +++ b/app/Enums/ScoreEnum.php @@ -0,0 +1,15 @@ + $this->service->getGrades(), + ]); + } + + /** + * Display a listing of the resource. + */ + public function index(Grade $grade): View + { + return view('grade-lesson.index', [ + 'lessons' => $this->service->getAll($grade), + 'grade' => $grade, + 'subjects' => $grade->subjects, + ]); + } + + /** + * Show the form for creating a new resource. + */ + public function create(Grade $grade): View + { + 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 + { + return redirect()->route( + 'grades.lessons.show', [ + $grade, + $this->service->create($request->validated()) + ] + ); + } + + /** + * Display the specified resource. + */ + public function show(Grade $grade, Lesson $lesson): View + { + 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 + { + 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 + { + return redirect()->route( + 'grades.lessons.show',[ + $grade, + $this->service->update($lesson, $request->validated()) + ] + ); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Grade $grade, Lesson $lesson): RedirectResponse + { + $this->service->delete($lesson); + + return redirect()->route('grades.lessons.index', $grade); + } +} diff --git a/app/Http/Controllers/ScoreController.php b/app/Http/Controllers/ScoreController.php new file mode 100644 index 0000000..838ab9b --- /dev/null +++ b/app/Http/Controllers/ScoreController.php @@ -0,0 +1,33 @@ + $this->service->getAll($lesson), + 'lesson' => $lesson, + 'scores' => ScoreEnum::cases(), + ]); + } + + public function update(Request $request, Lesson $lesson) + { + $this->service->update($lesson, $request->toArray()); + + return redirect()->route('grades.lessons.show', [$lesson->grade, $lesson]); + } +} diff --git a/app/Http/Requests/LessonPostRequest.php b/app/Http/Requests/LessonPostRequest.php new file mode 100644 index 0000000..174fe96 --- /dev/null +++ b/app/Http/Requests/LessonPostRequest.php @@ -0,0 +1,36 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string|max:255', + 'type' => ['required', Rule::in(TypeLesson::cases())], + 'lesson_date' => 'required|date', + 'description' => 'nullable|string|max:250', + 'grade_id' => 'required|exists:grades,id', + 'teacher_id' => 'required|exists:teachers,id', + 'subject_id' => 'required|exists:subjects,id', + ]; + } +} diff --git a/app/Http/Requests/ScorePostRequest.php b/app/Http/Requests/ScorePostRequest.php new file mode 100644 index 0000000..d5d9e13 --- /dev/null +++ b/app/Http/Requests/ScorePostRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Models/Grade.php b/app/Models/Grade.php index 04d734e..2d9d76c 100644 --- a/app/Models/Grade.php +++ b/app/Models/Grade.php @@ -31,6 +31,11 @@ class Grade extends Model return $this->belongsToMany(Teacher::class); } + public function lessons(): HasMany + { + return $this->hasMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Models/Lesson.php b/app/Models/Lesson.php new file mode 100644 index 0000000..060f07d --- /dev/null +++ b/app/Models/Lesson.php @@ -0,0 +1,54 @@ +belongsTo(Grade::class); + } + + public function teacher(): BelongsTo + { + return $this->belongsTo(Teacher::class); + } + + public function subject(): BelongsTo + { + return $this->belongsTo(Subject::class); + } + + public function students(): BelongsToMany + { + return $this->belongsToMany(Student::class)->withPivot('score'); + } + + public function scopeFilter(Builder $query): void + { + $subject_id = request('subject_id'); + + $query->when($subject_id, function (Builder $query, $subject_id) { + $query->where('subject_id', $subject_id); + }); + } +} diff --git a/app/Models/Student.php b/app/Models/Student.php index 43844ae..b7f93d0 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphOne; class Student extends Model @@ -31,6 +32,16 @@ class Student extends Model return $this->morphOne(User::class, 'userable'); } + public function subjects(): BelongsToMany + { + return $this->belongsToMany(Subject::class); + } + + public function lessons(): BelongsToMany + { + return $this->belongsToMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Models/Subject.php b/app/Models/Subject.php index 9e92711..ef256ed 100644 --- a/app/Models/Subject.php +++ b/app/Models/Subject.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; class Subject extends Model @@ -26,6 +27,16 @@ class Subject extends Model return $this->belongsToMany(Teacher::class); } + public function students(): BelongsToMany + { + return $this->belongsToMany(Student::class)->using(Score::class); + } + + public function lessons(): HasMany + { + return $this->hasMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Models/Teacher.php b/app/Models/Teacher.php index f18c015..0ce1e37 100644 --- a/app/Models/Teacher.php +++ b/app/Models/Teacher.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; class Teacher extends Model @@ -35,6 +36,11 @@ class Teacher extends Model return $this->morphOne(User::class, 'userable'); } + public function lessons(): HasMany + { + return $this->hasMany(Lesson::class); + } + public function scopeFilter(Builder $query): void { $name = request('name'); diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php index e7971b8..e2a8d11 100644 --- a/app/Providers/ModelServiceProvider.php +++ b/app/Providers/ModelServiceProvider.php @@ -5,6 +5,8 @@ namespace App\Providers; use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeTeacherController; +use App\Http\Controllers\LessonController; +use App\Http\Controllers\ScoreController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -12,6 +14,8 @@ use App\Http\Controllers\TeacherController; use App\Services\GradeService; use App\Services\GradeSubjectService; use App\Services\GradeTeacherService; +use App\Services\LessonService; +use App\Services\ScoreService; use App\Services\ServiceInterface; use App\Services\StudentService; use App\Services\SubjectService; @@ -67,6 +71,19 @@ class ModelServiceProvider extends ServiceProvider ->give(function () { return new GradeTeacherService(); }); + + $this->app->when(LessonController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new LessonService(); + }); + + + $this->app->when(ScoreController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new ScoreService(); + }); } /** diff --git a/app/Services/LessonService.php b/app/Services/LessonService.php new file mode 100644 index 0000000..dc59737 --- /dev/null +++ b/app/Services/LessonService.php @@ -0,0 +1,45 @@ +lessons()->filter()->get(); + } + + public function getGrades(): Collection + { + return Grade::all(); + } + + public function create(array $data): Lesson + { + $lesson = Lesson::create($data); + $lesson + ->students() + ->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]); + + return $lesson; + } + + public function update(Model $model, array $data): Lesson + { + $model->update($data); + + return $model; + } + + public function delete($model): void + { + $model->delete(); + } +} diff --git a/app/Services/ScoreService.php b/app/Services/ScoreService.php new file mode 100644 index 0000000..03d0a90 --- /dev/null +++ b/app/Services/ScoreService.php @@ -0,0 +1,36 @@ +students; + } + + public function update(Model $model, array $data) + { + $model->students->each(function ($item, $key) use ($data, $model) { + if ($data['score' . $item->id]) { + $model->students()->syncWithoutDetaching([$item->id => ['score' => $data['score' . $item->id]]]); + } + }); + + return $model; + } + + public function delete(Model $model) + { + // TODO: Implement delete() method. + } + + public function create(array $data) + { + // TODO: Implement create() method. + } +} diff --git a/app/Services/ServiceInterface.php b/app/Services/ServiceInterface.php index ef9a45c..d4b6d33 100644 --- a/app/Services/ServiceInterface.php +++ b/app/Services/ServiceInterface.php @@ -8,9 +8,9 @@ interface ServiceInterface { public function getAll(); - public function create(array $data): Model; + public function create(array $data); - public function update(Model $model, array $data): Model; + public function update(Model $model, array $data); public function delete(Model $model); } diff --git a/app/Services/StudentService.php b/app/Services/StudentService.php index 6aafb74..34d9ae4 100644 --- a/app/Services/StudentService.php +++ b/app/Services/StudentService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Enums\ScoreEnum; use App\Models\Student; use App\Models\User; use Illuminate\Pagination\LengthAwarePaginator; @@ -27,6 +28,9 @@ class StudentService implements ServiceInterface 'grade_id' => $data['grade_id'], ]); $student->user()->save($user); + $student + ->lessons() + ->syncWithPivotValues($student->grade->lessons->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]); return $student; } diff --git a/database/migrations/2024_05_07_123620_create_lessons_table.php b/database/migrations/2024_05_07_123620_create_lessons_table.php new file mode 100644 index 0000000..d281de2 --- /dev/null +++ b/database/migrations/2024_05_07_123620_create_lessons_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('name'); + $table->string('description')->nullable(); + $table->string('type'); + $table->date('lesson_date'); + $table->foreignId('grade_id')->constrained('grades')->onDelete('cascade'); + $table->foreignId('subject_id')->constrained('subjects')->onDelete('cascade'); + $table->foreignId('teacher_id')->constrained('teachers')->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('lessons'); + } +}; diff --git a/database/migrations/2024_05_13_102702_create_lesson_student_table.php b/database/migrations/2024_05_13_102702_create_lesson_student_table.php new file mode 100644 index 0000000..a264a23 --- /dev/null +++ b/database/migrations/2024_05_13_102702_create_lesson_student_table.php @@ -0,0 +1,29 @@ +foreignId('lesson_id')->constrained('lessons')->onDelete('cascade'); + $table->foreignId('student_id')->constrained('students')->onDelete('cascade'); + $table->string('score'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('lesson_student'); + } +}; diff --git a/resources/views/grade-lesson/create.blade.php b/resources/views/grade-lesson/create.blade.php new file mode 100644 index 0000000..10d197b --- /dev/null +++ b/resources/views/grade-lesson/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('grade-lesson.form', ['route' => route('grades.lessons.store', $grade), 'method' => 'POST']) +@endsection diff --git a/resources/views/grade-lesson/edit.blade.php b/resources/views/grade-lesson/edit.blade.php new file mode 100644 index 0000000..9866f01 --- /dev/null +++ b/resources/views/grade-lesson/edit.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('grade-lesson.form', ['route' => route('grades.lessons.update', [$grade, $lesson]), 'method' => 'PUT']) +@endsection diff --git a/resources/views/grade-lesson/form.blade.php b/resources/views/grade-lesson/form.blade.php new file mode 100644 index 0000000..2cb5e79 --- /dev/null +++ b/resources/views/grade-lesson/form.blade.php @@ -0,0 +1,51 @@ +
+
+
+
+
+ @csrf + @method($method) +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + {{-- заглушка --}} +
+ +
+
+
+
+
+
diff --git a/resources/views/grade-lesson/index.blade.php b/resources/views/grade-lesson/index.blade.php new file mode 100644 index 0000000..08d752c --- /dev/null +++ b/resources/views/grade-lesson/index.blade.php @@ -0,0 +1,57 @@ +`@extends('layouts.app') + +@section('content') +
+
+
+
{{__('Создание занятия')}}
+ +
+
+
{{__('Фильтрация')}}
+
+
+
+ + +
+
+ +
+
+
+
+ @if(count($lessons)) + @foreach($lessons as $lesson) + +
+
{{$lesson->subject->name}}
+
{{$lesson->type}}
+
+ {{ $lesson->name }} +
+ +
+
+ @endforeach + @endif +
+
+@endsection + +` diff --git a/resources/views/grade-lesson/show.blade.php b/resources/views/grade-lesson/show.blade.php new file mode 100644 index 0000000..a645a72 --- /dev/null +++ b/resources/views/grade-lesson/show.blade.php @@ -0,0 +1,31 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{ $lesson->type }}
+
+
+
Тема: {{ $lesson->name }}
+
+
+
Предмет: {{ $lesson->subject->name }}
+
+
+
Дата: {{ $lesson->lesson_date }}
+
+
+
Описание: {{ $lesson->description }}
+
+
+ +
+
+
+@endsection + + diff --git a/resources/views/grades/index.blade.php b/resources/views/grades/index.blade.php index a2dffb7..8cfb03a 100644 --- a/resources/views/grades/index.blade.php +++ b/resources/views/grades/index.blade.php @@ -36,16 +36,16 @@ class="btn btn-block col-8">{{ $grade->name }} + +
+ Занятия +
+
Редактировать
-{{-- --}} -{{--
--}} -{{-- Журнал--}} -{{--
--}} -{{-- --}}
diff --git a/resources/views/includes/header.blade.php b/resources/views/includes/header.blade.php index 9e59fab..38aa664 100644 --- a/resources/views/includes/header.blade.php +++ b/resources/views/includes/header.blade.php @@ -2,7 +2,7 @@
diff --git a/resources/views/scores/show.blade.php b/resources/views/scores/show.blade.php new file mode 100644 index 0000000..dad84cb --- /dev/null +++ b/resources/views/scores/show.blade.php @@ -0,0 +1,52 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+

{{ $lesson->name }}

+
+
+ + @csrf + @method('PUT') + @if(count($students)) + + + + + + + @foreach ($students as $student) + + + + + @endforeach + +
ФИООценка
+
+

{{ $student->fio }}

+
+
+
+ +
+
+ @else +

Ученики остутствуют

+ @endif + + +
+
+
+
+@endsection diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php index 822b81a..a3b9cf2 100644 --- a/resources/views/students/index.blade.php +++ b/resources/views/students/index.blade.php @@ -31,7 +31,7 @@
- {{ $student->last_name }} {{ $student->name }} {{ $student->middle_name }} + {{ $student->fio }}
diff --git a/routes/web.php b/routes/web.php index 59933d2..99f2059 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,8 @@ use App\Http\Controllers\GradeController; use App\Http\Controllers\GradeSubjectController; use App\Http\Controllers\GradeTeacherController; +use App\Http\Controllers\LessonController; +use App\Http\Controllers\ScoreController; use App\Http\Controllers\StudentController; use App\Http\Controllers\SubjectController; use App\Http\Controllers\SubjectTeacherController; @@ -18,8 +20,11 @@ Route::resources([ 'subjects' => SubjectController::class, 'students' => StudentController::class, 'teachers' => TeacherController::class, + 'grades.lessons' => LessonController::class, ]); Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index'); Route::resource('teachers.subjects.grades', GradeTeacherController::class)->except('index', 'show'); Route::resource('grades.subjects', GradeSubjectController::class)->except('index', 'show'); +Route::get('lessons/{lesson}/scores', [ScoreController::class, 'show'])->name('lessons.scores.show'); +Route::put('lessons/{lesson}/scores', [ScoreController::class, 'update'])->name('lessons.scores.update');