From b088c3d624b41af5e01ca385d37af849349fd595 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Sun, 23 Jun 2024 20:26:00 +0400 Subject: [PATCH 1/3] Fix --- app/Policies/StudentPolicy.php | 5 ++ app/Policies/SubjectPolicy.php | 11 +++ app/Policies/TeacherPolicy.php | 5 ++ resources/views/layouts/navigation.blade.php | 94 +++++++++++++------- resources/views/subjects/index.blade.php | 2 + resources/views/teachers/index.blade.php | 18 ++-- 6 files changed, 95 insertions(+), 40 deletions(-) diff --git a/app/Policies/StudentPolicy.php b/app/Policies/StudentPolicy.php index b30c231..c0f2155 100644 --- a/app/Policies/StudentPolicy.php +++ b/app/Policies/StudentPolicy.php @@ -47,4 +47,9 @@ class StudentPolicy { return $user->userable_type != Admin::class; } + + public function debts(User $user): bool + { + return $user->userable_type == Student::class; + } } diff --git a/app/Policies/SubjectPolicy.php b/app/Policies/SubjectPolicy.php index d62acbb..425db37 100644 --- a/app/Policies/SubjectPolicy.php +++ b/app/Policies/SubjectPolicy.php @@ -3,11 +3,17 @@ namespace App\Policies; use App\Models\Admin; +use App\Models\Student; use App\Models\Subject; +use App\Models\Teacher; use App\Models\User; class SubjectPolicy { + public function viewAny(User $user): bool + { + return $user->userable_type != Teacher::class; + } /** * Determine whether the user can create models. */ @@ -31,4 +37,9 @@ class SubjectPolicy { return $user->userable_type == Admin::class; } + + public function scores(User $user, Subject $subject): bool + { + return $user->userable_type == Student::class; + } } diff --git a/app/Policies/TeacherPolicy.php b/app/Policies/TeacherPolicy.php index 209a5d4..cfe16d9 100644 --- a/app/Policies/TeacherPolicy.php +++ b/app/Policies/TeacherPolicy.php @@ -48,4 +48,9 @@ class TeacherPolicy { return $user->userable_type == Admin::class; } + + public function teacherSubjects(User $user, Teacher $teacher): bool + { + return $user->userable_type == Student::class; + } } diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 678f1be..18921fb 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -5,49 +5,49 @@
- - - +
- @if(\Illuminate\Support\Facades\Auth::user()->userable_type != \App\Models\Student::class) - - @endif + @can('viewAny', \App\Models\Grade::class) + + @endcan - + @can('viewAny', \App\Models\Subject::class) + + @endcan - @if(\Illuminate\Support\Facades\Auth::user()->userable_type != \App\Models\Student::class) - - @endif + @can('viewAny', \App\Models\Student::class) + + @endcan - @if(\Illuminate\Support\Facades\Auth::user()->userable_type != \App\Models\Teacher::class) - - @endif + @can('viewAny', \App\Models\Teacher::class) + + @endcan - @if(\Illuminate\Support\Facades\Auth::user()->userable_type == \App\Models\Student::class) + @can('debts', \App\Models\Student::class) - @endif + @endcan
@@ -99,9 +99,35 @@ + @can('scores', $subject)
Оценки
+ @endcan @can('update', $subject)
diff --git a/resources/views/teachers/index.blade.php b/resources/views/teachers/index.blade.php index 927aa8f..7490690 100644 --- a/resources/views/teachers/index.blade.php +++ b/resources/views/teachers/index.blade.php @@ -31,14 +31,20 @@
- {{ $teacher->fio }} -
- - -
- Предметы + @can('view', $teacher) + {{ $teacher->fio }} + @else + {{ $teacher->fio }} + @endcan
+ @can('teacherSubjects', $teacher) + +
+ Предметы +
+ + @endcan @can('update', $teacher)
-- 2.25.1 From d4cabb23b7a7868b56fc68b2be627bf36906a906 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 24 Jun 2024 00:51:05 +0400 Subject: [PATCH 2/3] Fix --- app/Http/Controllers/GradeController.php | 12 +++- app/Http/Controllers/LessonController.php | 5 +- app/Http/Middleware/TeacherAction.php | 4 +- app/Policies/LessonPolicy.php | 6 +- app/Policies/StudentPolicy.php | 8 +-- app/Services/FileService.php | 20 +++++++ app/Services/GradeService.php | 23 ++++++++ app/Services/LessonService.php | 23 ++++++++ resources/views/grade-lesson/form.blade.php | 4 +- resources/views/grade-lesson/index.blade.php | 4 +- resources/views/grades/index.blade.php | 35 ++++++++---- .../views/grades/list-students.blade.php | 56 +++++++++++++++++++ resources/views/grades/show.blade.php | 32 ++++++----- resources/views/layouts/navigation.blade.php | 4 +- resources/views/students/index.blade.php | 42 +++++++++----- routes/web.php | 1 + 16 files changed, 220 insertions(+), 59 deletions(-) create mode 100644 app/Services/GradeService.php create mode 100644 app/Services/LessonService.php create mode 100644 resources/views/grades/list-students.blade.php diff --git a/app/Http/Controllers/GradeController.php b/app/Http/Controllers/GradeController.php index 13f4f72..542e934 100644 --- a/app/Http/Controllers/GradeController.php +++ b/app/Http/Controllers/GradeController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use App\Http\Requests\GradePostRequest; use App\Models\Grade; +use App\Services\FileService; +use App\Services\GradeService; use Illuminate\Http\RedirectResponse; use Illuminate\View\View; @@ -12,14 +14,14 @@ class GradeController extends Controller /** * Display a listing of the resource. */ - public function index(): View + public function index(GradeService $service): View { if(request()->user()->cannot('viewAny', Grade::class)) { abort(403); } return view('grades.index', [ - 'grades' => Grade::filter()->paginate(5)->withQueryString(), + 'grades' => $service->getGrades(), ]); } @@ -96,9 +98,13 @@ class GradeController extends Controller if(request()->user()->cannot('delete', Grade::class)) { abort(403); } - $grade->delete(); return redirect()->route('grades.index'); } + + public function listStudents(Grade $grade, FileService $fileService) + { + return $fileService->exportStudents($grade); + } } diff --git a/app/Http/Controllers/LessonController.php b/app/Http/Controllers/LessonController.php index 3d109c3..0591b6a 100644 --- a/app/Http/Controllers/LessonController.php +++ b/app/Http/Controllers/LessonController.php @@ -7,6 +7,7 @@ use App\Enums\TypeLesson; use App\Http\Requests\LessonPostRequest; use App\Models\Grade; use App\Models\Lesson; +use App\Services\LessonService; use Illuminate\Http\RedirectResponse; use Illuminate\View\View; @@ -22,14 +23,14 @@ class LessonController extends Controller /** * Display a listing of the resource. */ - public function index(Grade $grade): View + public function index(Grade $grade, LessonService $service): View { if(request()->user()->cannot('viewAny', $grade)) { abort(403); } return view('grade-lesson.index', [ - 'lessons' => $grade->lessons()->filter()->get(), + 'lessons' => $service->getLessons($grade), 'grade' => $grade, 'subjects' => $grade->subjects, ]); diff --git a/app/Http/Middleware/TeacherAction.php b/app/Http/Middleware/TeacherAction.php index d3581cf..eb62975 100644 --- a/app/Http/Middleware/TeacherAction.php +++ b/app/Http/Middleware/TeacherAction.php @@ -2,7 +2,7 @@ namespace App\Http\Middleware; -use App\Models\Student; +use App\Models\Teacher; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -17,7 +17,7 @@ class TeacherAction */ public function handle(Request $request, Closure $next): Response { - if (Auth::user()->userable_type != Student::class) { + if (Auth::user()->userable_type != Teacher::class) { abort(403); } diff --git a/app/Policies/LessonPolicy.php b/app/Policies/LessonPolicy.php index 14d8503..75d12a7 100644 --- a/app/Policies/LessonPolicy.php +++ b/app/Policies/LessonPolicy.php @@ -31,7 +31,7 @@ class LessonPolicy */ public function create(User $user): bool { - return $user->userable_type == Admin::class; + return $user->userable_type != Student::class; } /** @@ -39,7 +39,7 @@ class LessonPolicy */ public function update(User $user, Lesson $lesson): bool { - return $user->userable_type == Admin::class; + return $user->userable_type != Student::class; } /** @@ -47,6 +47,6 @@ class LessonPolicy */ public function delete(User $user, Lesson $lesson): bool { - return $user->userable_type == Admin::class; + return $user->userable_type != Student::class; } } diff --git a/app/Policies/StudentPolicy.php b/app/Policies/StudentPolicy.php index c0f2155..f5a70c9 100644 --- a/app/Policies/StudentPolicy.php +++ b/app/Policies/StudentPolicy.php @@ -21,7 +21,7 @@ class StudentPolicy */ public function view(User $user, Student $student): bool { - return $user->userable_type != Student::class; + return $user->userable_type == Admin::class; } /** @@ -29,7 +29,7 @@ class StudentPolicy */ public function create(User $user): bool { - return $user->userable_type != Admin::class; + return $user->userable_type == Admin::class; } /** @@ -37,7 +37,7 @@ class StudentPolicy */ public function update(User $user, Student $student): bool { - return $user->userable_type != Admin::class; + return $user->userable_type == Admin::class; } /** @@ -45,7 +45,7 @@ class StudentPolicy */ public function delete(User $user, Student $student): bool { - return $user->userable_type != Admin::class; + return $user->userable_type == Admin::class; } public function debts(User $user): bool diff --git a/app/Services/FileService.php b/app/Services/FileService.php index 46bd75d..beaa2d0 100644 --- a/app/Services/FileService.php +++ b/app/Services/FileService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Models\Grade; use Barryvdh\DomPDF\Facade\Pdf; use Illuminate\Support\Facades\Auth; @@ -24,4 +25,23 @@ class FileService return Pdf::loadView('subjects.pdf', ['subjects' => $listSubjects])->download('Предметы.pdf'); } + + public function exportStudents(Grade $grade) + { + $excellentStudents = $this->getMinScore($grade, 5); + $goodStudents = $this->getMinScore($grade, 4); + + return Pdf::loadView('grades.list-students', [ + 'excellentStudents' => $excellentStudents, + 'goodStudents' => $goodStudents, + ])->download('Студенты.pdf'); + } + + public function getMinScore(Grade $grade, $minScore) + { + return $grade->students->filter(function ($student) use ($minScore) { + return $student->lessons->min('pivot.score') == $minScore; + }); + } + } diff --git a/app/Services/GradeService.php b/app/Services/GradeService.php new file mode 100644 index 0000000..78d969f --- /dev/null +++ b/app/Services/GradeService.php @@ -0,0 +1,23 @@ +userable_type == Teacher::class) { + return Grade::join('grade_teacher', 'grade_teacher.grade_id', '=', 'grades.id') + ->where('grade_teacher.teacher_id', Auth::user()->userable_id) + ->filter() + ->paginate(5) + ->withQueryString(); + } + + return Grade::filter()->paginate(5)->withQueryString(); + } +} diff --git a/app/Services/LessonService.php b/app/Services/LessonService.php new file mode 100644 index 0000000..940faa5 --- /dev/null +++ b/app/Services/LessonService.php @@ -0,0 +1,23 @@ +userable_type == Teacher::class) { + return $grade + ->lessons() + ->where('teacher_id', Auth::user()->userable_id) + ->filter() + ->get(); + } + + return $grade->lessons()->filter()->get(); + } +} diff --git a/resources/views/grade-lesson/form.blade.php b/resources/views/grade-lesson/form.blade.php index 73d4340..55261b4 100644 --- a/resources/views/grade-lesson/form.blade.php +++ b/resources/views/grade-lesson/form.blade.php @@ -14,7 +14,7 @@
- +
diff --git a/resources/views/grade-lesson/index.blade.php b/resources/views/grade-lesson/index.blade.php index bddb287..a34964b 100644 --- a/resources/views/grade-lesson/index.blade.php +++ b/resources/views/grade-lesson/index.blade.php @@ -1,4 +1,4 @@ -`@extends('layouts.application') +@extends('layouts.application') @section('content')
@@ -53,5 +53,3 @@
@endsection - -` diff --git a/resources/views/grades/index.blade.php b/resources/views/grades/index.blade.php index 23861b6..26d7fde 100644 --- a/resources/views/grades/index.blade.php +++ b/resources/views/grades/index.blade.php @@ -43,17 +43,26 @@
- Редактировать + Списки учеников
- -
- @csrf - @method('DELETE') - -
- + @can('update', $grade) + +
+ Редактировать +
+ + @endcan + @can('delete', $grade) + +
+ @csrf + @method('DELETE') + +
+ + @endcan @endforeach @@ -62,9 +71,11 @@

Классы отсутствуют

@endif - + @can('create', \App\Models\Grade::class) + + @endcan diff --git a/resources/views/grades/list-students.blade.php b/resources/views/grades/list-students.blade.php new file mode 100644 index 0000000..782020b --- /dev/null +++ b/resources/views/grades/list-students.blade.php @@ -0,0 +1,56 @@ + + + + + + Списки студентов + + +

Список отличников

+ + + + + + + + @foreach($excellentStudents as $student) + + + + @endforeach + +
ФИО
{{ $student->fio }}
+

Список хорошистов

+ + + + + + + + @foreach($goodStudents as $student) + + + + @endforeach + +
ФИО
{{ $student->fio }}
+ + diff --git a/resources/views/grades/show.blade.php b/resources/views/grades/show.blade.php index dc25c85..03b66c5 100644 --- a/resources/views/grades/show.blade.php +++ b/resources/views/grades/show.blade.php @@ -10,9 +10,11 @@
Название: {{ $grade->name }}
- + @can('update', $grade) + + @endcan
@@ -38,13 +40,15 @@ Журнал
- -
- @csrf - @method('DELETE') - -
- + @can('delete', $grade) + +
+ @csrf + @method('DELETE') + +
+ + @endcan @endforeach @@ -53,9 +57,11 @@

У класса отсутствуют предметы

@endif
- + @can('create', \App\Models\Grade::class) + + @endcan diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 18921fb..ad46c39 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -28,7 +28,7 @@ @can('viewAny', \App\Models\Student::class) @endcan @@ -113,7 +113,7 @@ @can('viewAny', \App\Models\Student::class) - {{ __('Студенты') }} + {{ __('Ученики') }} @endcan diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php index 73d5d60..e529fb3 100644 --- a/resources/views/students/index.blade.php +++ b/resources/views/students/index.blade.php @@ -24,6 +24,7 @@ + @@ -31,21 +32,34 @@ - - + @can('update', $student) + + @endcan + @can('delete', $student) + + @endcan @endforeach @@ -54,9 +68,11 @@

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

@endif - + @can('create', \App\Models\Student::class) + + @endcan diff --git a/routes/web.php b/routes/web.php index 65d291c..bf44d83 100644 --- a/routes/web.php +++ b/routes/web.php @@ -46,6 +46,7 @@ Route::middleware('auth')->group(function () { Route::get('lessons/{lesson}/scores', [ScoreController::class, 'show'])->name('lessons.scores.show'); Route::put('lessons/{lesson}/scores', [ScoreController::class, 'update'])->name('lessons.scores.update'); Route::get('grades/{grade}/subjects/{subject}/journal', [GradeSubjectController::class, 'journal'])->name('grades.subjects.journal'); + Route::get('grades/{grade}/list-students', [GradeController::class, 'listStudents'])->name('list-students'); }); Route::middleware([StudentAction::class])->group(function () { -- 2.25.1 From 769760f414a4a8558f2e2bb69f8ecf8fd02db74b Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Mon, 24 Jun 2024 01:28:58 +0400 Subject: [PATCH 3/3] Fix --- app/Http/Requests/StudentPostRequest.php | 4 +++- app/Http/Requests/TeacherPostRequest.php | 4 +++- app/Policies/GradePolicy.php | 11 +++++++++++ resources/views/grades/index.blade.php | 12 +++++++----- resources/views/grades/show.blade.php | 12 +++++++----- resources/views/teachers/index.blade.php | 2 +- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/Http/Requests/StudentPostRequest.php b/app/Http/Requests/StudentPostRequest.php index 132c34d..c726119 100644 --- a/app/Http/Requests/StudentPostRequest.php +++ b/app/Http/Requests/StudentPostRequest.php @@ -2,7 +2,9 @@ namespace App\Http\Requests; +use App\Models\User; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class StudentPostRequest extends FormRequest { @@ -27,7 +29,7 @@ class StudentPostRequest extends FormRequest 'middle_name' => 'required|max:255', 'birthday' => 'required|date', 'grade_id' => 'required|exists:grades,id', - 'email' => 'required|max:255|lowercase|unique:users,email', + 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->route('student')?->user->id)], 'password' => 'required|max:255', ]; } diff --git a/app/Http/Requests/TeacherPostRequest.php b/app/Http/Requests/TeacherPostRequest.php index e3aea8a..062dde2 100644 --- a/app/Http/Requests/TeacherPostRequest.php +++ b/app/Http/Requests/TeacherPostRequest.php @@ -2,7 +2,9 @@ namespace App\Http\Requests; +use App\Models\User; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class TeacherPostRequest extends FormRequest { @@ -26,7 +28,7 @@ class TeacherPostRequest extends FormRequest 'last_name' => 'required|max:255', 'middle_name' => 'required|max:255', 'birthday' => 'required|date', - 'email' => 'required|max:255|lowercase|unique:users,email', + 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->route('teacher')?->user->id)], 'password' => 'required|max:255', ]; } diff --git a/app/Policies/GradePolicy.php b/app/Policies/GradePolicy.php index 8c63ca2..6343761 100644 --- a/app/Policies/GradePolicy.php +++ b/app/Policies/GradePolicy.php @@ -5,6 +5,7 @@ namespace App\Policies; use App\Models\Admin; use App\Models\Grade; use App\Models\Student; +use App\Models\Teacher; use App\Models\User; class GradePolicy @@ -48,4 +49,14 @@ class GradePolicy { return $user->userable_type == Admin::class; } + + public function journal(User $user) + { + return $user->userable_type == Teacher::class; + } + + public function list(User $user) + { + return $user->userable_type == Teacher::class; + } } diff --git a/resources/views/grades/index.blade.php b/resources/views/grades/index.blade.php index 26d7fde..43440e4 100644 --- a/resources/views/grades/index.blade.php +++ b/resources/views/grades/index.blade.php @@ -41,11 +41,13 @@ Занятия - + @can('list', \App\Models\Grade::class) + + @endcan @can('update', $grade) - + @can('journal', \App\Models\Grade::class) + + @endcan @can('delete', $grade)
ФИОКласс  
- {{ $student->fio }} + @can('view', $student) + {{ $student->fio }} + @else + {{ $student->fio }} + @endcan
+
- Редактировать + {{ $student->grade->name }}
-
- @csrf - @method('DELETE') - -
-
+ + +
+ @csrf + @method('DELETE') + +
+
- - + +
diff --git a/resources/views/grades/show.blade.php b/resources/views/grades/show.blade.php index 03b66c5..98ffe23 100644 --- a/resources/views/grades/show.blade.php +++ b/resources/views/grades/show.blade.php @@ -35,11 +35,13 @@ {{ $subject->name }}
- - + +
diff --git a/resources/views/teachers/index.blade.php b/resources/views/teachers/index.blade.php index 7490690..92ac833 100644 --- a/resources/views/teachers/index.blade.php +++ b/resources/views/teachers/index.blade.php @@ -69,7 +69,7 @@

Учителя отсутствуют

@endif - @can('create', Teacher::class) + @can('create', \App\Models\Teacher::class) -- 2.25.1