59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Export\JournalExport;
|
|
use App\Models\Grade;
|
|
use App\Models\Subject;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
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]);
|
|
}
|
|
});
|
|
});
|
|
|
|
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;
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|