public static function copy($previousSemester = null, $currentSemester = null) { $previousSemester = $previousSemester ? $previousSemester : Semester::get_previous_semester()->id; $currentSemester = $currentSemester ? $currentSemester : Semester::get_active_semester()->id; $now = \Carbon\Carbon::now(); $prevKB = KetuntasanBelajar::where('id_semester', '=', $previousSemester)->get()->toArray(); $fail = 0; $success = 0; foreach ($prevKB as $KB) { try { KetuntasanBelajar::where('id_mapel', $KB['id_mapel'])->where('id_semester', $currentSemester)->delete(); KetuntasanBelajar::insert(['id_mapel' => $KB['id_mapel'], 'id_semester' => $currentSemester, 'kb_tingkat_1p' => $KB['kb_tingkat_1p'], 'kb_tingkat_1k' => $KB['kb_tingkat_1k'], 'kb_tingkat_2p' => $KB['kb_tingkat_2p'], 'kb_tingkat_2k' => $KB['kb_tingkat_2k'], 'kb_tingkat_3p' => $KB['kb_tingkat_3p'], 'kb_tingkat_3k' => $KB['kb_tingkat_3k']]); } catch (\Illuminate\Database\QueryException $e) { $fail++; continue; } $success++; } return ['fail' => $fail, 'success' => $success]; }
public function mass(Request $request) { if (!$request->ajax()) { abort(404); } $this->validate($request, ['type' => 'required|integer']); $semester = Semester::get_active_semester()->id; if (!($prevSemester = Semester::get_previous_semester())) { return response("Semester sebelumnya tidak dapat ditemukan.", 422); } $prevSemester = $prevSemester->id; if ($request->type == 1 || $request->type == 2) { $targets = Mapel::get_unset_kb(); foreach ($targets as $target) { $new = new KetuntasanBelajar(); $new->id_mapel = $target->id; $new->id_semester = $semester; if ($target->kelompok == 'A' | $target->kelompok == 'B' | $target->kelompok == 'C1') { $n = 60; } elseif ($target->kelompok == 'C2' | $target->kelompok == 'C3') { $n = 70; } $new->kb_tingkat_1p = $n; $new->kb_tingkat_2p = $n; $new->kb_tingkat_3p = $n; $new->kb_tingkat_1k = $n; $new->kb_tingkat_2k = $n; $new->kb_tingkat_3k = $n; $new->save(); } } if ($request->type == 2) { $targets = Mapel::get_set_kb(); foreach ($targets as $target) { $new = KetuntasanBelajar::where('id_mapel', '=', $target->id)->where('id_semester', '=', $semester)->first(); $new->id_semester = $semester; if ($target->kelompok == 'A' | $target->kelompok == 'B' | $target->kelompok == 'C1') { $n = 60; } elseif ($target->kelompok == 'C2' | $target->kelompok == 'C3') { $n = 70; } KetuntasanBelajar::where('id_mapel', '=', $target->id)->where('id_semester', '=', $semester)->update(['kb_tingkat_1p' => $n, 'kb_tingkat_2p' => $n, 'kb_tingkat_3p' => $n, 'kb_tingkat_1k' => $n, 'kb_tingkat_2k' => $n, 'kb_tingkat_3k' => $n]); } } if ($request->input('type') == 3) { $copy = null; try { $copy = KetuntasanBelajar::copy($prevSemester, $semester); } catch (Exception $e) { return response("Penyalinan aturan ketuntasan belajar dari semester sebelumnya gagal dilakukan.", 422); } } return 'KB berhasil diubah secara massal.'; }