222 lines
7.4 KiB
PHP
222 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Kelas;
|
|
use App\Models\Logbooks;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class LogbookKelasMultiExport implements WithMultipleSheets
|
|
{
|
|
protected $request;
|
|
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->request = $request;
|
|
}
|
|
|
|
public function sheets(): array
|
|
{
|
|
$sheets = [];
|
|
|
|
$query = Kelas::query()
|
|
->when($this->request->filled('semester'), function ($q) {
|
|
$q->where('semester', $this->request->semester);
|
|
})
|
|
->when($this->request->filled('tahun'), function ($q) {
|
|
$q->where('tahun_ajaran', $this->request->tahun);
|
|
})
|
|
->when($this->request->filled('is_ekstrakurikuler'), function ($q) {
|
|
$q->where('is_ekstrakurikuler', $this->request->is_ekstrakurikuler);
|
|
})
|
|
->when($this->request->filled('program_id'), function ($q) {
|
|
$q->where('program_id', $this->request->program_id);
|
|
});
|
|
|
|
$allKelas = $query->orderBy('id', 'asc')->get();
|
|
|
|
if ($allKelas->count() <= 0) {
|
|
$sheets[] = new EmptySheetExport();
|
|
return $sheets;
|
|
}
|
|
|
|
foreach ($allKelas as $kelas) {
|
|
$sheets[] = new LogbookKelasSheetExport($kelas->id);
|
|
}
|
|
|
|
return $sheets;
|
|
}
|
|
}
|
|
|
|
class LogbookKelasSheetExport implements FromCollection, WithHeadings, WithTitle, ShouldAutoSize, WithStyles, WithEvents
|
|
{
|
|
protected $kelasId;
|
|
protected $kelas;
|
|
|
|
public function __construct($kelasId)
|
|
{
|
|
$this->kelasId = $kelasId;
|
|
$this->kelas = Kelas::with(['programs', 'sekolah'])->find($kelasId);
|
|
}
|
|
|
|
public function title(): string
|
|
{
|
|
if (!$this->kelas) {
|
|
return 'Kelas ' . $this->kelasId;
|
|
}
|
|
|
|
// Buat nama sheet max 31 karakter
|
|
$title = $this->kelas->nama_kelas;
|
|
$title = preg_replace('/[\\\\*?:\[\]\/]/', '', $title);
|
|
if (mb_strlen($title) > 31) {
|
|
$title = mb_substr($title, 0, 28) . '...';
|
|
}
|
|
return $title;
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'No',
|
|
'Pertemuan / Sesi',
|
|
'Tanggal',
|
|
'Jam',
|
|
'Materi',
|
|
'Pengajar',
|
|
'Kegiatan',
|
|
'Perkembangan Siswa',
|
|
'Kendala & Solusi',
|
|
'Kebutuhan',
|
|
'Hal Baik',
|
|
'Catatan',
|
|
'Status',
|
|
];
|
|
}
|
|
|
|
public function collection()
|
|
{
|
|
$logbooks = Logbooks::with(['Guru', 'Sesi'])
|
|
->where('kelas_id', $this->kelasId)
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
|
|
$rows = collect();
|
|
$no = 1;
|
|
|
|
foreach ($logbooks as $log) {
|
|
$pengajar = $log->Guru ? $log->Guru->nama : '-';
|
|
|
|
$jam = '-';
|
|
if ($log->start && $log->end) {
|
|
$jam = date('H:i', strtotime($log->start)) . ' - ' . date('H:i', strtotime($log->end));
|
|
} elseif ($log->Sesi) {
|
|
$jam = $log->Sesi->jam;
|
|
}
|
|
|
|
$tgl = '-';
|
|
if ($log->tanggal) {
|
|
$tgl = date('d M Y', strtotime($log->tanggal));
|
|
}
|
|
|
|
$rows->push([
|
|
'no' => $no++,
|
|
'pertemuan' => $log->pertemuan ?? '-',
|
|
'tanggal' => $tgl,
|
|
'jam' => $jam,
|
|
'materi' => $log->materi ?? '-',
|
|
'pengajar' => $pengajar,
|
|
'kegiatan' => $log->kegiatan_minggu_ini ?? '-',
|
|
'perkembangan' => $log->perkembangan_siswa ?? '-',
|
|
'kendala' => $log->kendala_dan_solusi ?? '-',
|
|
'kebutuhan' => $log->kebutuhan_minggu_depan ?? '-',
|
|
'hal_baik' => $log->hal_baik ?? '-',
|
|
'catatan' => $log->catatan ?? '-',
|
|
'status' => $log->status ?? '-',
|
|
]);
|
|
}
|
|
|
|
if ($logbooks->count() == 0) {
|
|
$rows->push([
|
|
'no' => 1,
|
|
'pertemuan' => '-',
|
|
'tanggal' => '-',
|
|
'jam' => '-',
|
|
'materi' => 'Belum ada logbook',
|
|
'pengajar' => '-',
|
|
'kegiatan' => '-',
|
|
'perkembangan' => '-',
|
|
'kendala' => '-',
|
|
'kebutuhan' => '-',
|
|
'hal_baik' => '-',
|
|
'catatan' => '-',
|
|
'status' => '-',
|
|
]);
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
return [
|
|
6 => ['font' => ['bold' => true]],
|
|
];
|
|
}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function (AfterSheet $event) {
|
|
$sheet = $event->sheet->getDelegate();
|
|
|
|
$namaKelas = strtoupper($this->kelas ? $this->kelas->nama_kelas : 'LOGBOOK KELAS');
|
|
|
|
// Insert 5 baris di paling atas untuk Header Dokumen LKP Robotku
|
|
$sheet->insertNewRowBefore(1, 5);
|
|
|
|
// Judul Utama (Row 2)
|
|
$sheet->mergeCells('A2:M2');
|
|
$sheet->setCellValue('A2', 'LOGBOOK ' . $namaKelas);
|
|
$sheet->getStyle('A2')->getFont()->setBold(true)->setSize(14);
|
|
$sheet->getStyle('A2')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
|
|
// Subtitle 1 (Row 3)
|
|
$sheet->mergeCells('A3:M3');
|
|
$sheet->setCellValue('A3', 'LKP Robotku');
|
|
$sheet->getStyle('A3')->getFont()->setBold(true)->setSize(11);
|
|
$sheet->getStyle('A3')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
|
|
// Subtitle 2 (Row 4)
|
|
$sheet->mergeCells('A4:M4');
|
|
$sheet->setCellValue('A4', 'Kota Probolinggo');
|
|
$sheet->getStyle('A4')->getFont()->setBold(true)->setSize(10);
|
|
$sheet->getStyle('A4')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
|
|
// Style Header Tabel (Row 6)
|
|
$sheet->getStyle('A6:M6')->getFill()
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
|
->getStartColor()->setARGB('FFF2F2F2');
|
|
|
|
$sheet->getStyle('A6:M6')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
$sheet->getStyle('A6:M6')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
|
|
|
|
// Border Seluruh Tabel
|
|
$highestRow = $sheet->getHighestRow();
|
|
if ($highestRow >= 6) {
|
|
$sheet->getStyle("A6:M{$highestRow}")->getBorders()->getAllBorders()
|
|
->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|