From 733bb761226ebc84690be5af05b5dd648c8e8cfe Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 25 Jun 2024 01:42:23 +0400 Subject: [PATCH] Export average scores to PDF --- app/Http/Controllers/StudentController.php | 6 +++ app/Policies/StudentPolicy.php | 5 +++ app/Services/StudentService.php | 15 +++++++ resources/views/students/avg-scores.blade.php | 43 +++++++++++++++++++ resources/views/subjects/index.blade.php | 7 ++- routes/web.php | 1 + 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 resources/views/students/avg-scores.blade.php diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php index f0ce8cf..5c0b0e8 100644 --- a/app/Http/Controllers/StudentController.php +++ b/app/Http/Controllers/StudentController.php @@ -6,6 +6,7 @@ use App\Http\Requests\StudentPostRequest; use App\Models\Grade; use App\Models\Student; use App\Models\Subject; +use App\Services\FileService; use App\Services\StudentService; use Illuminate\Http\RedirectResponse; use Illuminate\View\View; @@ -134,4 +135,9 @@ class StudentController extends Controller 'lessons' => $service->getDebts(), ]); } + + public function exportAvgScores(StudentService $service) + { + return $service->exportAvgScores(); + } } diff --git a/app/Policies/StudentPolicy.php b/app/Policies/StudentPolicy.php index f5a70c9..3d027da 100644 --- a/app/Policies/StudentPolicy.php +++ b/app/Policies/StudentPolicy.php @@ -52,4 +52,9 @@ class StudentPolicy { return $user->userable_type == Student::class; } + + public function avgScores(User $user): bool + { + return $user->userable_type == Student::class; + } } diff --git a/app/Services/StudentService.php b/app/Services/StudentService.php index 7e83f62..33a75f2 100644 --- a/app/Services/StudentService.php +++ b/app/Services/StudentService.php @@ -6,6 +6,7 @@ use App\Enums\ScoreEnum; use App\Models\Student; use App\Models\Subject; use App\Models\User; +use Barryvdh\DomPDF\Facade\Pdf; use Illuminate\Database\Eloquent\Collection; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\Auth; @@ -82,4 +83,18 @@ class StudentService return $student->lessons()->whereIn('score', ScoreEnum::getDebtScores())->get(); } + + public function exportAvgScores() + { + $subjects = Auth::user()->userable->grade->subjects; + $avgScores = collect(); + + $subjects->each(function ($subject) use ($avgScores) { + $avgScores->put($subject->name, $this->getAvgScore($subject)); + }); + + return Pdf::loadView('students.avg-scores', [ + 'avgScores' => $avgScores, + ])->download('Успеваемость.pdf'); + } } diff --git a/resources/views/students/avg-scores.blade.php b/resources/views/students/avg-scores.blade.php new file mode 100644 index 0000000..196a54a --- /dev/null +++ b/resources/views/students/avg-scores.blade.php @@ -0,0 +1,43 @@ + + + + + + Успеваемость ученика + + +

Информация об успеваемости

+ + + + + + + + + @foreach($avgScores as $key => $item) + + + + + @endforeach + +
ПредметСредняя оценка
{{ $key }}{{ $item }}
+ + diff --git a/resources/views/subjects/index.blade.php b/resources/views/subjects/index.blade.php index c01d208..3eccc69 100644 --- a/resources/views/subjects/index.blade.php +++ b/resources/views/subjects/index.blade.php @@ -18,7 +18,12 @@
-
{{__('Предметы')}}
+
+ {{__('Предметы')}} + @can('avgScores', \App\Models\Student::class) + Успеваемость + @endcan +
@if (count($subjects)) diff --git a/routes/web.php b/routes/web.php index bf8fbcd..30ffff6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -54,6 +54,7 @@ Route::middleware('auth')->group(function () { Route::get('subjects/{subject}/student-scores', [StudentController::class, 'scores'])->name('student-scores'); Route::get('student-debts', [StudentController::class, 'debts'])->name('student-debts'); Route::get('teachers/{teacher}/subjects', [SubjectTeacherController::class, 'index'])->name('teachers.subjects.index'); + Route::get('export-avg-scores', [StudentController::class, 'exportAvgScores'])->name('export-avg-scores'); }); });