CourseWork/app/Services/FileService.php

59 lines
1.9 KiB
PHP
Raw Normal View History

2024-06-17 17:38:29 +04:00
<?php
namespace App\Services;
2024-06-24 22:04:44 +04:00
use App\Export\JournalExport;
2024-06-24 00:51:05 +04:00
use App\Models\Grade;
2024-06-24 22:04:44 +04:00
use App\Models\Subject;
2024-06-17 17:38:29 +04:00
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Auth;
2024-06-24 22:04:44 +04:00
use Maatwebsite\Excel\Facades\Excel;
2024-06-17 17:38:29 +04:00
class FileService
{
public function exportSubjects()
{
$listSubjects = collect();
$student = Auth::user()->userable;
$subjects = $student->grade->subjects;
$teachers = $student->grade->teachers;
$teachers->each(function ($teacher) use ($subjects, $listSubjects) {
$teacher->subjects->each(function ($subject) use ($subjects, $listSubjects, $teacher) {
if ($subjects->contains($subject)) {
$listSubjects->push(['subject' => $subject->name, 'teacher' => $teacher->fio]);
}
});
});
2024-06-23 17:02:18 +04:00
return Pdf::loadView('subjects.pdf', ['subjects' => $listSubjects])->download('Предметы.pdf');
2024-06-17 17:38:29 +04:00
}
2024-06-24 00:51:05 +04:00
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;
});
}
2024-06-24 22:04:44 +04:00
public function exportJournal(Grade $grade, Subject $subject)
{
$lessons = $grade->lessons()->where('subject_id', $subject->id)->with('students')->get();
$students = $grade->students()->orderBy('last_name')->get();
$fileName = $subject->name . '(' . $grade->name . ').xlsx';
return Excel::download(new JournalExport($lessons, $students), $fileName);
}
2024-06-17 17:38:29 +04:00
}