535 lines
19 KiB
PHP
535 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Imports\LogbookImport;
|
|
use App\Models\FotoLogbooks;
|
|
use App\Models\Guru;
|
|
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 Maatwebsite\Excel\Facades\Excel;
|
|
use Intervention\Image\Laravel\Facades\Image;
|
|
|
|
class LogbookController 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;
|
|
|
|
$guruPengajar = $data->guru_id ? Guru::select('id', 'nama', 'no_hp')->where('id', $data->guru_id)->first() : ($data->Kelas->guru ?? null);
|
|
$guruId = $data->guru_id ?? ($data->Kelas->guru_id ?? 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,
|
|
'kegiatan_minggu_ini' => $data->kegiatan_minggu_ini ?? null,
|
|
'perkembangan_siswa' => $data->perkembangan_siswa ?? null,
|
|
'kendala_dan_solusi' => $data->kendala_dan_solusi ?? null,
|
|
'kebutuhan_minggu_depan' => $data->kebutuhan_minggu_depan ?? null,
|
|
'hal_baik' => $data->hal_baik ?? null,
|
|
'catatan' => $data->catatan ?? null,
|
|
'pertemuan' => $data->pertemuan ?? null,
|
|
'guru' => $guruPengajar->nama ?? null,
|
|
'guru_id' => $guruId,
|
|
'nama_guru' => $guruPengajar->nama ?? null,
|
|
'no_hp_guru' => $guruPengajar->no_hp ?? null,
|
|
'ruangan_id' => $data->ruangan_id ?? null,
|
|
'logbooks' => $logbook,
|
|
'kelas_id' => $data->kelas_id ?? null,
|
|
'nama_kelas' => $data->Kelas->nama_kelas ?? null,
|
|
'status' => $data->status ?? null,
|
|
'sesi_id' => $data->sesi_id ?? null,
|
|
'nama_sesi' => $data->Sesi->nama_sesi ?? null,
|
|
'jam_sesi' => $data->Sesi->jam ?? null,
|
|
'jenis_libur' => $data->jenis_libur ?? null,
|
|
'jenis_libur_lainnya' => $data->jenis_libur_lainnya ?? null,
|
|
]);
|
|
}
|
|
|
|
public function duplicate($id){
|
|
$logbook = Logbooks::findOrFail($id);
|
|
|
|
$newLogbook = $logbook->replicate([
|
|
'created_at',
|
|
'updated_at',
|
|
'status'
|
|
]);
|
|
|
|
$newLogbook->status = 'draft';
|
|
|
|
if($logbook->status == 'libur'){
|
|
$newLogbook->status = 'libur';
|
|
}
|
|
|
|
$newLogbook->save();
|
|
|
|
$pengajarLama = PengajarLogbook::where('logbook_id', $logbook->id)->get();
|
|
|
|
foreach ($pengajarLama as $pengajar) {
|
|
PengajarLogbook::create([
|
|
'logbook_id' => $newLogbook->id,
|
|
'guru_id' => $pengajar->guru_id,
|
|
]);
|
|
}
|
|
|
|
$direct = '/admin/kelas/' .$logbook->kelas_id;
|
|
return redirect($direct)->with('success', 'Logbook berhasil ditambah');
|
|
}
|
|
|
|
public function store(Request $request){
|
|
$validated = $request->validate(
|
|
[
|
|
'kelas_id' => 'required',
|
|
'kode_kelas' => 'required',
|
|
'guru_id' => 'nullable|exists:guru,id',
|
|
'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',
|
|
]
|
|
);
|
|
|
|
// 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 = '/admin/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 = '/admin/kelas/' .$request->kelas_id;
|
|
return redirect($direct)->with('success', 'Logbook berhasil ditambah');
|
|
}
|
|
|
|
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
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id){
|
|
$data = Logbooks::where('id', $id)->first();
|
|
$validated = $request->validate(
|
|
[
|
|
'kelas_id' => 'required',
|
|
'guru_id' => 'nullable|exists:guru,id',
|
|
'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',
|
|
]
|
|
);
|
|
|
|
// 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/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;
|
|
$validated['jenis_libur_lainnya'] = null;
|
|
|
|
if ($request->has('guru_id')) {
|
|
$validated['guru_id'] = $request->guru_id;
|
|
}
|
|
|
|
$data->update($validated);
|
|
|
|
$direct = '/admin/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 = '/admin/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 = '/admin/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 = '/admin/kelas/' . $logbook->kelas_id;
|
|
return redirect($direct)->with('success', 'Data logbook berhasil dihapus');
|
|
|
|
}else{
|
|
$direct = '/admin/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 = '/admin/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',
|
|
'guru_id' => 'nullable|exists:guru,id',
|
|
'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',
|
|
]
|
|
);
|
|
|
|
// 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 = '/admin/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 = '/admin/jadwal/';
|
|
return redirect($direct)->with('success', 'Data logbook berhasil dihapus');
|
|
|
|
}else{
|
|
$direct = '/admin/jadwal/';
|
|
return redirect($direct)->with('error', 'Data tidak ditemukan');
|
|
}
|
|
}
|
|
}
|