penjadwalan-ga/app/Http/Controllers/Guru/LogbookGuruController.php

524 lines
20 KiB
PHP

<?php
namespace App\Http\Controllers\Guru;
use App\Http\Controllers\Controller;
use App\Imports\LogbookImport;
use App\Models\FotoLogbooks;
use App\Models\Kelas;
use App\Models\Logbooks;
use App\Models\PengajarLogbook;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Laravel\Facades\Image;
use Maatwebsite\Excel\Facades\Excel;
class LogbookGuruController extends Controller
{
public function getLogbook($id){
$data = Logbooks::where('id', $id)->first();
$logbook = FotoLogbooks::where('logbook_id', $data->id)->get()->map(function ($logbook) {
return [
'id' => $logbook->id,
'foto' => $logbook->foto,
];
});
$tanggalparse = $data->tanggal
? Carbon::parse($data->tanggal)->locale('id')->translatedFormat('l, d F Y')
: null;
$startparse = $data->start
? Carbon::parse($data->start)->locale('id')->translatedFormat('H:i')
: null;
$endparse = $data->end
? Carbon::parse($data->end)->locale('id')->translatedFormat('H:i')
: null;
return response()->json([
'id' => $data->id,
'tanggal' => $data->tanggal ?? null,
'start' => $data->start ?? null,
'start_parse' => $startparse ?? null,
'end' => $data->end ?? null,
'end_parse' => $endparse ?? null,
'tanggalparse' => $tanggalparse ?? null,
'materi' => $data->materi ?? null,
'catatan' => $data->catatan ?? null,
'pertemuan' => $data->pertemuan ?? null,
'logbooks' => $logbook,
'kelas_id' => $data->kelas_id ?? null,
'sesi_id' => $data->sesi_id ?? null,
'nama_sesi' => $data->Sesi->nama_sesi ?? null,
'jam_sesi' => $data->Sesi->jam ?? null,
]);
}
public function store(Request $request){
$validated = $request->validate(
[
'kelas_id' => 'required',
'kode_kelas' => 'required',
'sesi_id' => 'nullable|exists:sesi,id',
'pertemuan' => 'nullable',
'tanggal' => 'nullable',
'start' => 'nullable',
'end' => 'nullable',
'materi' => 'nullable',
'catatan' => 'nullable',
'kegiatan_minggu_ini' => 'nullable',
'perkembangan_siswa' => 'nullable',
'kendala_dan_solusi' => 'nullable',
'kebutuhan_minggu_depan' => 'nullable',
'hal_baik' => 'nullable',
],
[
'kelas_id.required' => 'ID kelas tidak ditemukan',
]
);
// ── Validasi Deadline Pengisian Logbook ─────────────────────────────
$tanggalInput = $request->tanggal;
if ($tanggalInput) {
$parsedTanggal = Carbon::parse($tanggalInput);
if ($parsedTanggal->lt(Carbon::today())) {
$direct = '/guru/kelas/' . $request->kelas_id;
return redirect($direct)->with('error', 'Logbook dengan tanggal lampau tidak dapat diisi. Pengisian hanya bisa dilakukan pada hari yang sama.');
}
if ($parsedTanggal->isToday() && Carbon::now()->greaterThan(Carbon::today()->setTime(19, 0, 0))) {
$direct = '/guru/kelas/' . $request->kelas_id;
return redirect($direct)->with('error', 'Batas waktu pengisian logbook hari ini telah berakhir (19:00 WIB). Silakan hubungi koordinator jika ada kendala.');
}
}
// ────────────────────────────────────────────────────────────────────
// Kondisi jika libur
if ($request->status_libur && $request->status_libur == "on"){
$validated['status'] = "libur";
$validated['jenis_libur'] = $request->jenis_libur ?? null;
$validated['jenis_libur_lainnya'] = $request->jenis_libur_lainnya ?? null;
$logbook = Logbooks::create($validated);
$direct = '/guru/kelas/' .$request->kelas_id;
return redirect($direct)->with('success', 'Logbook berhasil ditambah');
}
$validated['status'] = "Draft";
$validated['jenis_libur'] = null;
$validated['jenis_libur_lainnya'] = null;
$logbook = Logbooks::create($validated);
$logbook_id = $logbook->id;
$uploadedFiles = [];
if ($request->hasFile('fotos')) {
foreach ($request->file('fotos') as $file) {
if ($file->isValid()) {
$hashedName = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('foto-logbook', $hashedName, 'public');
$fullPath = Storage::disk('public')->path($path);
// Resize gambar menggunakan Intervention Image
Image::read($fullPath)
->orient()
->scaleDown(width: 800)
->save($fullPath, quality: 85);
FotoLogbooks::create([
'logbook_id' => $logbook_id,
'keterangan' => '',
'foto' => 'foto-logbook/' . $hashedName,
]);
$uploadedFiles[] = 'foto-logbook/' . $hashedName;
}
}
}
$checkUploadFile = FotoLogbooks::where('logbook_id', $logbook_id)->count();
if ($checkUploadFile > 0) {
Logbooks::where('id', $logbook_id)->update(['status' => 'completed']);
}
if ($request->has('guru_id')) {
$logbook->update(['guru_id' => $request->guru_id]);
}
$direct = '/guru/kelas/' .$request->kelas_id;
return redirect($direct)->with('success', 'Logbook berhasil ditambah');
}
public function update(Request $request, $id){
$data = Logbooks::where('id', $id)->first();
$validated = $request->validate(
[
'kelas_id' => 'required',
'sesi_id' => 'nullable|exists:sesi,id',
'pertemuan' => 'nullable',
'tanggal' => 'nullable',
'start' => 'nullable',
'end' => 'nullable',
'materi' => 'nullable',
'catatan' => 'nullable',
'kegiatan_minggu_ini' => 'nullable',
'perkembangan_siswa' => 'nullable',
'kendala_dan_solusi' => 'nullable',
'kebutuhan_minggu_depan' => 'nullable',
'hal_baik' => 'nullable',
],
[
'kelas_id.required' => 'ID kelas tidak ditemukan',
]
);
// ── Validasi Deadline Pengisian Logbook ─────────────────────────────
$tanggalLogbook = $request->tanggal ?? $data->tanggal;
if ($tanggalLogbook) {
$parsedTanggal = Carbon::parse($tanggalLogbook);
if ($parsedTanggal->lt(Carbon::today())) {
$direct = '/guru/kelas/' . $request->kelas_id;
return redirect($direct)->with('error', 'Logbook dengan tanggal lampau tidak dapat diubah. Pengisian hanya bisa dilakukan pada hari yang sama.');
}
if ($parsedTanggal->isToday() && Carbon::now()->greaterThan(Carbon::today()->setTime(19, 0, 0))) {
$direct = '/guru/kelas/' . $request->kelas_id;
return redirect($direct)->with('error', 'Batas waktu pengisian logbook hari ini telah berakhir (19:00 WIB). Silakan hubungi koordinator jika ada kendala.');
}
}
// ────────────────────────────────────────────────────────────────────
// Kondisi jika libur
if ($request->status_libur && $request->status_libur == "on"){
$validated['status'] = "libur";
$validated['jenis_libur'] = $request->jenis_libur ?? null;
$validated['jenis_libur_lainnya'] = $request->jenis_libur_lainnya ?? null;
$data->update($validated);
$direct = '/guru/kelas/' .$request->kelas_id;
return redirect($direct)->with('success', 'Logbook berhasil diubah');
}
$validated['status'] = "Draft";
$logbook_id = $data->id;
$uploadedFiles = [];
if ($request->hasFile('fotos')) {
foreach ($request->file('fotos') as $file) {
if ($file->isValid()) {
$hashedName = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('foto-logbook', $hashedName, 'public');
$fullPath = Storage::disk('public')->path($path);
Image::read($fullPath)
->orient()
->scaleDown(width: 800)
->save($fullPath, quality: 85);
// Simpan ke database
FotoLogbooks::create([
'logbook_id' => $logbook_id,
'keterangan' => '',
'foto' => 'foto-logbook/' . $hashedName,
]);
$uploadedFiles[] = 'foto-logbook/' . $hashedName;
}
}
}
$checkUploadFile = FotoLogbooks::where('logbook_id', $logbook_id)->count();
if ($checkUploadFile > 0) {
$validated['status'] = "completed";
}
$validated['jenis_libur'] = null;
if ($request->has('guru_id')) {
$validated['guru_id'] = $request->guru_id;
}
$data->update($validated);
$direct = '/guru/kelas/' .$request->kelas_id;
return redirect($direct)->with('success', 'Logbook berhasil diubah');
}
public function destroy($id){
$data = Logbooks::findOrFail($id);
$kelas_id = $data->kelas_id;
$fotoLogbook = FotoLogbooks::where('logbook_id', $id)->get();
if($fotoLogbook->count() > 0){
for($b = 0; $b < count($fotoLogbook); $b++){
if ($fotoLogbook[$b]->foto) {
$imagePath = storage_path('app/public/' . $fotoLogbook[$b]->foto);
if (file_exists($imagePath)) {
unlink($imagePath);
}
}
$fotoLogbook[$b]->delete();
}
}
$data->delete();
$direct = '/guru/kelas/' . $kelas_id;
return redirect($direct)->with('success', 'Data logbook berhasil dihapus');
}
public function destroySelected(Request $request){
$logbook_id = $request->id;
$kelas_id = Logbooks::where('id', $logbook_id[0])->first();
for ($i = 0; $i < count($logbook_id); $i++){
$logbook = Logbooks::where('id', $logbook_id[$i])->first();
$fotoLogbook = FotoLogbooks::where('logbook_id', $logbook->id)->get();
if($fotoLogbook->count() > 0){
for($b = 0; $b < count($fotoLogbook); $b++){
if ($fotoLogbook[$b]->foto) {
$imagePath = storage_path('app/public/' . $fotoLogbook[$b]->foto);
if (file_exists($imagePath)) {
unlink($imagePath);
}
}
$fotoLogbook[$b]->delete();
}
}
$logbook->delete();
}
$direct = '/guru/kelas/' . $kelas_id->kelas_id;
return redirect($direct)->with('success', 'Data logbook berhasil dihapus');
}
public function destroyFoto($id){
$dataFoto = FotoLogbooks::findOrFail($id);
if ($dataFoto){
$logbook = Logbooks::where('id', $dataFoto->logbook_id)->select('id', 'kelas_id')->first();
$filePath = 'storage/' . $dataFoto->foto;
if ($filePath) {
unlink($filePath);
}
$dataFoto->delete();
$checkUploadFile = FotoLogbooks::where('logbook_id', $dataFoto->logbook_id)->count();
if ($checkUploadFile > 0) {
Logbooks::where('id', $logbook->id)->update(['status' => 'completed']);
}else{
Logbooks::where('id', $logbook->id)->update(['status' => 'draft']);
}
$direct = '/guru/kelas/' . $logbook->kelas_id;
return redirect($direct)->with('success', 'Data logbook berhasil dihapus');
}else{
$direct = '/guru/kelas';
return redirect($direct)->with('error', 'Data tidak ditemukan');
}
}
function import(Request $request)
{
$request->validate(
[
'file' => 'required|mimes:csv,xls,xlsx'
],
[
'file.required' => 'File tidak boleh kosong',
'file.mimes' => 'File harus berbentuk csv',
]
);
Excel::import(new LogbookImport, $request->file('file'));
$direct = '/guru/kelas/' .$request->kelas_id;
return redirect($direct)->with('success', 'Import logbook telah berhasil');
}
// edit and delete logbook from jadwal
public function updateLog(Request $request, $id){
$data = Logbooks::where('id', $id)->first();
$validated = $request->validate(
[
'kelas_id' => 'required',
'kegiatan_minggu_ini' => 'nullable',
'perkembangan_siswa' => 'nullable',
'kendala_dan_solusi' => 'nullable',
'kebutuhan_minggu_depan' => 'nullable',
'hal_baik' => 'nullable',
],
[
'kelas_id.required' => 'ID kelas tidak ditemukan',
]
);
// ── Validasi Deadline Pengisian Logbook ─────────────────────────────
$tanggalLogbook = $data->tanggal;
if ($tanggalLogbook) {
$parsedTanggal = Carbon::parse($tanggalLogbook);
if ($parsedTanggal->lt(Carbon::today())) {
return redirect('/guru/jadwal')->with('error', 'Logbook dengan tanggal lampau tidak dapat diubah. Pengisian hanya bisa dilakukan pada hari yang sama.');
}
if ($parsedTanggal->isToday() && Carbon::now()->greaterThan(Carbon::today()->setTime(19, 0, 0))) {
return redirect('/guru/jadwal')->with('error', 'Batas waktu pengisian logbook hari ini telah berakhir (19:00 WIB). Silakan hubungi koordinator jika ada kendala.');
}
}
// ────────────────────────────────────────────────────────────────────
// Kondisi jika libur
if ($request->status_libur && $request->status_libur == "on"){
$validated['status'] = "libur";
$validated['jenis_libur'] = $request->jenis_libur ?? null;
$validated['jenis_libur_lainnya'] = $request->jenis_libur_lainnya ?? null;
$data->update($validated);
$direct = '/admin/jadwal/';
return redirect($direct)->with('success', 'Logbook berhasil diubah');
}
if ($request->catatan !== null || $request->catatan !== ''){ $validated['catatan'] = $request->catatan; }
if ($request->materi !== null || $request->materi !== ''){ $validated['materi'] = $request->materi; }
$validated['status'] = "Draft";
$logbook_id = $data->id;
$uploadedFiles = [];
if ($request->hasFile('fotos')) {
foreach ($request->file('fotos') as $file) {
if ($file->isValid()) {
$hashedName = md5(time() . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
$path = $file->storeAs('foto-logbook', $hashedName, 'public');
$fullPath = Storage::disk('public')->path($path);
Image::read($fullPath)
->orient()
->scaleDown(width: 800)
->save($fullPath, quality: 85);
// Simpan ke database
FotoLogbooks::create([
'logbook_id' => $logbook_id,
'keterangan' => '',
'foto' => 'foto-logbook/' . $hashedName,
]);
$uploadedFiles[] = 'foto-logbook/' . $hashedName;
}
}
}
$checkUploadFile = FotoLogbooks::where('logbook_id', $logbook_id)->count();
if ($checkUploadFile > 0) {
$validated['status'] = "completed";
}
$validated['jenis_libur'] = null;
$validated['jenis_libur_lainnya'] = null;
if ($request->has('guru_id')) {
$validated['guru_id'] = $request->guru_id;
}
$data->update($validated);
$direct = '/guru/jadwal/';
return redirect($direct)->with('success', 'Logbook berhasil diubah');
}
public function destroyFotoLog($id){
$dataFoto = FotoLogbooks::findOrFail($id);
if ($dataFoto){
$logbook = Logbooks::where('id', $dataFoto->logbook_id)->select('id', 'kelas_id')->first();
$filePath = 'storage/' . $dataFoto->foto;
if ($filePath) {
unlink($filePath);
}
$dataFoto->delete();
$checkUploadFile = FotoLogbooks::where('logbook_id', $dataFoto->logbook_id)->count();
if ($checkUploadFile > 0) {
Logbooks::where('id', $logbook->id)->update(['status' => 'completed']);
}else{
Logbooks::where('id', $logbook->id)->update(['status' => 'draft']);
}
$direct = '/guru/jadwal/';
return redirect($direct)->with('success', 'Data logbook berhasil dihapus');
}else{
$direct = '/guru/jadwal/';
return redirect($direct)->with('error', 'Data tidak ditemukan');
}
}
public function addrow(Request $request)
{
$validate = $request->validate([
'tanggal' => 'nullable|date',
'start' => 'nullable|date_format:H:i',
'end' => 'nullable|date_format:H:i',
'pertemuan' => 'nullable|numeric',
'materi' => 'nullable|string',
'catatan' => 'nullable|string',
'kelas_id' => 'required|exists:kelas,id',
]);
$kelas = Kelas::find($validate['kelas_id']);
if (!$kelas) {
return response()->json([
'status' => 'error',
'message' => 'Kelas tidak ditemukan',
'data' => null
]);
}
$validate['kode_kelas'] = $kelas->kode_kelas;
if (!empty($validate['tanggal'])) {
$tanggal = Carbon::parse($validate['tanggal']);
if (!empty($validate['end'])) {
$endTime = Carbon::createFromFormat('H:i', $validate['end']);
$combinedEnd = $tanggal->copy()->setTimeFrom($endTime);
$validate['status'] = $combinedEnd->isPast() ? 'perlu diisi' : 'Draft';
} else {
$validate['status'] = $tanggal->lessThanOrEqualTo(Carbon::today()) ? 'perlu diisi' : 'Draft';
}
} else {
$validate['status'] = 'Draft';
}
$logbook = Logbooks::create($validate);
return response()->json([
'status' => 'success',
'message' => 'Sesi berhasil ditambahkan',
'data' => $logbook
]);
}
}