CourseWork/app/Export/JournalExport.php
2024-06-24 22:04:44 +04:00

74 lines
1.8 KiB
PHP

<?php
namespace App\Export;
use App\Enums\ScoreEnum;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class JournalExport implements FromCollection, ShouldAutoSize, WithTitle, WithStyles
{
protected $lessons;
protected $students;
public function __construct($lessons, $students)
{
$this->lessons = $lessons;
$this->students = $students;
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
$result = collect();
$headRow = collect();
$headRow->push('');
$this->lessons->each(function ($lesson) use ($headRow) {
$headRow->push($lesson->date);
});
$result->push($headRow);
$headRow = collect();
$headRow->push('ФИО');
$this->lessons->each(function ($lesson) use ($headRow) {
$headRow->push($lesson->shortType);
});
$result->push($headRow);
$this->students->each(function ($student) use ($result){
$row = collect();
$row->push($student->fio);
$this->lessons->each(function ($lesson) use ($row, $student) {
$row->push($student->lessons->find($lesson->id)->pivot->score ?? ScoreEnum::WithoutScore);
});
$result->push($row);
});
return $result;
}
public function title(): string
{
return 'Журнал';
}
public function styles(Worksheet $sheet)
{
$sheet->getStyle($sheet->calculateWorksheetDimension())
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);
}
}