Task-11 #12

Merged
klllst merged 3 commits from feature/task-11 into develop 2024-06-24 17:36:55 +04:00
23 changed files with 344 additions and 109 deletions

View File

@ -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);
}
}

View File

@ -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,
]);

View File

@ -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);
}

View File

@ -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',
];
}

View File

@ -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',
];
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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,6 +45,11 @@ 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
{
return $user->userable_type == Student::class;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
});
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Services;
use App\Models\Grade;
use App\Models\Teacher;
use Illuminate\Support\Facades\Auth;
class GradeService
{
public function getGrades()
{
if (Auth::user()->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();
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Services;
use App\Models\Grade;
use App\Models\Teacher;
use Illuminate\Support\Facades\Auth;
class LessonService
{
public function getLessons(Grade $grade)
{
if (Auth::user()->userable_type == Teacher::class) {
return $grade
->lessons()
->where('teacher_id', Auth::user()->userable_id)
->filter()
->get();
}
return $grade->lessons()->filter()->get();
}
}

View File

@ -14,7 +14,7 @@
<select id="type" name="type" class="form-select" required>
<option value="">Выберите тип деятельности</option>
@foreach($types as $type)
<option value="{{ $type }}" {{ isset($lesson) && $lesson->type == $type ? 'selected' : old('type') }}>
<option value="{{ $type }}" {{ isset($lesson) && $lesson->type == $type->value ? 'selected' : old('type') }}>
{{ $type }}
</option>
@endforeach
@ -36,7 +36,7 @@
<textarea class="form-control" id="description" name="description" rows="5" >{{ isset($lesson) ? $lesson->description : old('description') }}</textarea>
</div>
<div class="mb-3">
<label for="lesson_date" class="form-label">Дата рождения:</label>
<label for="lesson_date" class="form-label">Дата занятия:</label>
<input type="date" id="lesson_date" name="lesson_date" class="form-control" value="{{ isset($lesson) ? $lesson->lesson_date : old('lesson_date') }}" required>
</div>
<input type="hidden" name="grade_id" value="{{ $grade->id }}">

View File

@ -1,4 +1,4 @@
`@extends('layouts.application')
@extends('layouts.application')
@section('content')
<div class="container col-md-5">
@ -53,5 +53,3 @@
</div>
</div>
@endsection
`

View File

@ -41,19 +41,30 @@
<a href="{{ route('grades.lessons.index', $grade) }}" class="btn btn-primary">Занятия</a>
</div>
</td>
<td>
<div>
<a href="{{ route('grades.edit', $grade) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('grades.destroy', $grade) }}" method="POST"
style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
@can('list', \App\Models\Grade::class)
<td>
<div>
<a href="{{ route('list-students', $grade) }}" class="btn btn-info">Списки учеников</a>
</div>
</td>
@endcan
@can('update', $grade)
<td>
<div>
<a href="{{ route('grades.edit', $grade) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
@endcan
@can('delete', $grade)
<td>
<form action="{{ route('grades.destroy', $grade) }}" method="POST"
style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
@endcan
</tr>
@endforeach
</tbody>
@ -62,9 +73,11 @@
<p>Классы отсутствуют</p>
@endif
</div>
<div class="card-footer">
<a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a>
</div>
@can('create', \App\Models\Grade::class)
<div class="card-footer">
<a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a>
</div>
@endcan
<div class="card-footer">{{ $grades->links() }}</div>
</div>
</div>

View File

@ -0,0 +1,56 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
* { font-family: DejaVu Sans !important; }
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
<title>Списки студентов</title>
</head>
<body>
<h3>Список отличников</h3>
<table>
<thead>
<tr>
<th>ФИО</th>
</tr>
</thead>
<tbody>
@foreach($excellentStudents as $student)
<tr>
<td>{{ $student->fio }}</td>
</tr>
@endforeach
</tbody>
</table>
<h3>Список хорошистов</h3>
<table>
<thead>
<tr>
<th>ФИО</th>
</tr>
</thead>
<tbody>
@foreach($goodStudents as $student)
<tr>
<td>{{ $student->fio }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>

View File

@ -10,9 +10,11 @@
<h5><strong>Название: </strong>{{ $grade->name }}</h5>
</div>
</div>
<div class="card-footer">
<a href="{{ route('grades.edit', $grade) }}" class="btn btn-primary">Редактировать</a>
</div>
@can('update', $grade)
<div class="card-footer">
<a href="{{ route('grades.edit', $grade) }}" class="btn btn-primary">Редактировать</a>
</div>
@endcan
</div>
<div class="card mt-4">
<div class="card-header">
@ -33,18 +35,22 @@
{{ $subject->name }}
</div>
</td>
<td>
<div>
<a href="{{ route('grades.subjects.journal', [$grade, $subject]) }}" class="btn btn-primary">Журнал</a>
</div>
</td>
<td>
<form action="{{ route('grades.subjects.destroy', [$grade, $subject]) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
@can('journal', \App\Models\Grade::class)
<td>
<div>
<a href="{{ route('grades.subjects.journal', [$grade, $subject]) }}" class="btn btn-primary">Журнал</a>
</div>
</td>
@endcan
@can('delete', $grade)
<td>
<form action="{{ route('grades.subjects.destroy', [$grade, $subject]) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
@endcan
</tr>
@endforeach
</tbody>
@ -53,9 +59,11 @@
<p>У класса отсутствуют предметы</p>
@endif
</div>
<div class="card-footer">
<a href="{{ route('grades.subjects.create', $grade) }}" class="btn btn-success">Добавить</a>
</div>
@can('create', \App\Models\Grade::class)
<div class="card-footer">
<a href="{{ route('grades.subjects.create', $grade) }}" class="btn btn-success">Добавить</a>
</div>
@endcan
</div>
</div>
</div>

View File

@ -5,49 +5,49 @@
<div class="flex">
<!-- Logo -->
<div class="shrink-0 flex items-center">
<a href="{{ route('grades.index') }}">
<x-application-logo class="block h-9 w-auto fill-current text-gray-800" />
</a>
<x-application-logo class="block h-9 w-auto fill-current text-gray-800" />
</div>
<!-- Navigation Links -->
@if(\Illuminate\Support\Facades\Auth::user()->userable_type != \App\Models\Student::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('grades.index')" :active="request()->routeIs('grades.index')">
{{ __('Классы') }}
</x-nav-link>
</div>
@endif
@can('viewAny', \App\Models\Grade::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('grades.index')" :active="request()->routeIs('grades.index')">
{{ __('Классы') }}
</x-nav-link>
</div>
@endcan
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('subjects.index')" :active="request()->routeIs('subjects.index')">
{{ __('Предметы') }}
</x-nav-link>
</div>
@can('viewAny', \App\Models\Subject::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('subjects.index')" :active="request()->routeIs('subjects.index')">
{{ __('Предметы') }}
</x-nav-link>
</div>
@endcan
@if(\Illuminate\Support\Facades\Auth::user()->userable_type != \App\Models\Student::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('students.index')" :active="request()->routeIs('students.index')">
{{ __('Студенты') }}
</x-nav-link>
</div>
@endif
@can('viewAny', \App\Models\Student::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('students.index')" :active="request()->routeIs('students.index')">
{{ __('Ученики') }}
</x-nav-link>
</div>
@endcan
@if(\Illuminate\Support\Facades\Auth::user()->userable_type != \App\Models\Teacher::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('teachers.index')" :active="request()->routeIs('teachers.index')">
{{ __('Учителя') }}
</x-nav-link>
</div>
@endif
@can('viewAny', \App\Models\Teacher::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('teachers.index')" :active="request()->routeIs('teachers.index')">
{{ __('Учителя') }}
</x-nav-link>
</div>
@endcan
@if(\Illuminate\Support\Facades\Auth::user()->userable_type == \App\Models\Student::class)
@can('debts', \App\Models\Student::class)
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
<x-nav-link :href="route('student-debts')" :active="request()->routeIs('student-debts')">
{{ __('Задолженности') }}
</x-nav-link>
</div>
@endif
@endcan
</div>
<!-- Settings Dropdown -->
@ -99,9 +99,35 @@
<!-- Responsive Navigation Menu -->
<div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
<div class="pt-2 pb-3 space-y-1">
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
{{ __('Dashboard') }}
</x-responsive-nav-link>
@can('viewAny', \App\Models\Grade::class)
<x-responsive-nav-link :href="route('grades.index')" :active="request()->routeIs('grades.index')">
{{ __('Классы') }}
</x-responsive-nav-link>
@endcan
@can('viewAny', \App\Models\Subject::class)
<x-responsive-nav-link :href="route('subjects.index')" :active="request()->routeIs('subjects.index')">
{{ __('Предметы') }}
</x-responsive-nav-link>
@endcan
@can('viewAny', \App\Models\Student::class)
<x-responsive-nav-link :href="route('students.index')" :active="request()->routeIs('students.index')">
{{ __('Ученики') }}
</x-responsive-nav-link>
@endcan
@can('viewAny', \App\Models\Teacher::class)
<x-responsive-nav-link :href="route('teachers.index')" :active="request()->routeIs('teachers.index')">
{{ __('Учителя') }}
</x-responsive-nav-link>
@endcan
@can('debts', \App\Models\Student::class)
<x-responsive-nav-link :href="route('student-debts')" :active="request()->routeIs('student-debts')">
{{ __('Задолженности') }}
</x-responsive-nav-link>
@endcan
</div>
<!-- Responsive Settings Options -->

View File

@ -24,6 +24,7 @@
<table class="table table-striped">
<thead>
<th>ФИО</th>
<th>Класс</th>
<th>&nbsp;</th>
</thead>
<tbody>
@ -31,21 +32,34 @@
<tr>
<td class="table-text">
<div>
<a href="{{ route('students.show', $student) }}" class="btn btn-block col-8">{{ $student->fio }}</a>
@can('view', $student)
<a href="{{ route('students.show', $student) }}" class="btn btn-block col-8">{{ $student->fio }}</a>
@else
{{ $student->fio }}
@endcan
</div>
</td>
<td>
<td class="table-text">
<div>
<a href="{{ route('students.edit', $student) }}" class="btn btn-warning">Редактировать</a>
{{ $student->grade->name }}
</div>
</td>
<td>
<form action="{{ route('students.destroy', $student) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
@can('update', $student)
<td>
<div>
<a href="{{ route('students.edit', $student) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
@endcan
@can('delete', $student)
<td>
<form action="{{ route('students.destroy', $student) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
@endcan
</tr>
@endforeach
</tbody>
@ -54,9 +68,11 @@
<p>Ученики остутствуют</p>
@endif
</div>
<div class="card-footer">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div>
@can('create', \App\Models\Student::class)
<div class="card-footer">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div>
@endcan
<div class="card-footer">{{ $students->links() }}</div>
</div>
</div>

View File

@ -34,11 +34,13 @@
{{ $subject->name }}
</div>
</td>
@can('scores', $subject)
<td>
<div>
<a href="{{ route('student-scores', $subject) }}" class="btn btn-success">Оценки</a>
</div>
</td>
@endcan
@can('update', $subject)
<td>
<div>

View File

@ -31,14 +31,20 @@
<tr>
<td class="table-text">
<div>
{{ $teacher->fio }}
</div>
</td>
<td>
<div>
<a href="{{ route('teachers.subjects.index', $teacher) }}" class="btn btn-warning">Предметы</a>
@can('view', $teacher)
<a href="{{ route('teachers.show', $teacher) }}" class="btn btn-block">{{ $teacher->fio }}</a>
@else
{{ $teacher->fio }}
@endcan
</div>
</td>
@can('teacherSubjects', $teacher)
<td>
<div>
<a href="{{ route('teachers.subjects.index', $teacher) }}" class="btn btn-warning">Предметы</a>
</div>
</td>
@endcan
@can('update', $teacher)
<td>
<div>
@ -63,7 +69,7 @@
<p>Учителя отсутствуют</p>
@endif
</div>
@can('create', Teacher::class)
@can('create', \App\Models\Teacher::class)
<div class="card-footer">
<a href="{{ route('teachers.create') }}" class="btn btn-success">Добавить</a>
</div>

View File

@ -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 () {