<?php
defined('BASEPATH') or exit('No direct script access allowed');
// created by : KuduSinau_Hendrik_081275532220 (TERIMAKASIH UNTUK MENDAPATKAN KEBERKAHAN, DENGAN TIDAK MERUBAH NAMA HAK CIPTA, PLAGIAT, DAN TIDAK MEMPERJUALBELIKAN APLIKASI INI, SEMOGA KITA SEMUA MENDAPAT MANFAAT DAN KEBERKAHAN DARI APLIKASI INI)
// created by : KuduSinau_Hendrik_081275532220 (UNTUK MASUKAN DAN SARAN SERTA PENGEMBANGAN BISA MENGHUBUNGI NOMOR)
class Guru extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        kudu_sinau();
        $kd_level = $this->session->userdata('kd_level');
        $Level = $this->uri->segment(1);
        $queryLevel = $this->db->get_where('t_level', ['nm_level' => $Level])->row_array();
        $idLevel = $queryLevel['id_level'];
        if (($kd_level == 4 and $idLevel == 4) || ($kd_level == 3 and $idLevel == 4) || ($kd_level == 2 and $idLevel == 4)) {
        } else {
            if ($kd_level == 1) {
                redirect('admin/block');
            } elseif ($kd_level == 2) {
                redirect('kamad/block');
            } elseif ($kd_level == 3) {
                redirect('walas/block');
            } elseif ($kd_level == 5) {
                redirect('siswa/block');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function index()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $kdTapel = $dataTapel['id_tapel'];
        } else {
            $kdTapel = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $this->db->from('t_students');;
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->where('status_lulus', 0);
        $jumlahsiswa = $this->db->count_all_results();
        $this->db->from('t_students');;
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->where('status_lulus', 3);
        $jumlahlulus = $this->db->count_all_results();
        $this->db->from('t_teachers');;
        $this->db->where('kd_lembaga', $newKdlembaga);
        $jumlahguru = $this->db->count_all_results();
        $this->db->from('t_kelas');;
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->where('kd_tapel', $kdTapel);
        $jumlahrombel = $this->db->count_all_results();
        $this->db->from('t_user');;
        $this->db->where('kd_user', $newKdlembaga);
        $jumlahuser = $this->db->count_all_results();
        $this->db->from('t_eskul');;
        $this->db->where('kd_lembaga', $newKdlembaga);
        $jumlaheskul = $this->db->count_all_results();
        $data['jumlahSiswa'] = $jumlahsiswa;
        $data['jumlahLulus'] = $jumlahlulus;
        $data['jumlahGuru'] = $jumlahguru;
        $data['jumlahRombel'] = $jumlahrombel;
        $data['jumlahUser'] = $jumlahuser;
        $data['jumlahEskul'] = $jumlaheskul;
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Dashboard';
        $this->load->view('template/header', $data);
        $this->load->view('template/sidebar', $data);
        $this->load->view('template/headertop', $data);
        $this->load->view('template/mobile', $data);
        $this->load->view('guru/dashboard', $data);
        $this->load->view('template/footer', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function profil_user()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $dtUsername = $data['nmUser']['username'];
        $dtKdUser = $data['nmUser']['kd_user'];
        $dtKdGuru = $data['nmUser']['kd_teachers'];
        $dtEmail = $data['nmUser']['email_user'];
        $dtImageUser = $data['nmUser']['image_user'];
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $dtKdUser);
        $data['Lembaga'] = $this->db->get()->row_array();
        $this->db->select('*');
        $this->db->from('t_teachers');
        $this->db->where('t_teachers.id_teachers', $dtKdGuru);
        $this->db->where('t_teachers.kd_lembaga',  $dtKdUser);
        $guru = $this->db->get()->row_array();
        if (!is_null($guru)) {
            $data['Guru'] = $guru;
        }
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Profil User';
        $this->form_validation->set_rules('gelar_depan', 'Gelar Depan', 'trim');
        $this->form_validation->set_rules('nm_teachers', 'Nama Teacher', 'required|trim');
        $this->form_validation->set_rules('gelar_belakang', 'Gelar Belakang', 'trim');
        $this->form_validation->set_rules('alamat_teachers', 'Alamat', 'required|trim');
        $this->form_validation->set_rules('desa_teachers', 'Desa/Kelurahan', 'required|trim');
        $this->form_validation->set_rules('kec_teachers', 'Kecamatan', 'required|trim');
        $this->form_validation->set_rules('kab_teachers', 'Kota/Kabupaten', 'required|trim');
        $this->form_validation->set_rules('prov_teachers', 'Provinsi', 'required|trim');
        $this->form_validation->set_rules('tmt_teachers', 'TMT Teacher', 'required|trim');
        $this->form_validation->set_rules(
            'email_teachers',
            'Email Teacher',
            'trim|valid_email',
            [
                'valid_email' => 'Penulisan Email Belum Benar !'
            ]
        );
        $this->form_validation->set_rules('telp_teachers', 'Telepon Teacher', 'required|trim');
        if ($this->form_validation->run() == false) {
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/profil_user', $data);
            $this->load->view('template/footer', $data);
        } else {
            $gelarDepan = htmlspecialchars($this->input->post('gelar_depan'), true);
            $namaGuru = strtoupper(htmlspecialchars($this->input->post('nm_teachers'), true));
            $gelarBelakang = htmlspecialchars($this->input->post('gelar_belakang'), true);
            $alamat = htmlspecialchars($this->input->post('alamat_teachers'), true);
            $desa = htmlspecialchars($this->input->post('desa_teachers'), true);
            $kec = htmlspecialchars($this->input->post('kec_teachers'), true);
            $kab = htmlspecialchars($this->input->post('kab_teachers'), true);
            $prov = htmlspecialchars($this->input->post('prov_teachers'), true);
            $tmt = htmlspecialchars($this->input->post('tmt_teachers'), true);
            $email = htmlspecialchars($this->input->post('email_teachers'), true);
            $telp = htmlspecialchars($this->input->post('telp_teachers'), true);
            $data = [
                'gelar_depan' => $gelarDepan,
                'nm_teachers' => $namaGuru,
                'gelar_belakang' => $gelarBelakang,
                'alamat_teachers' => ucwords(strtolower($alamat)),
                'desa_teachers' => ucwords(strtolower($desa)),
                'kec_teachers' => ucwords(strtolower($kec)),
                'kab_teachers' => ucwords(strtolower($kab)),
                'prov_teachers' => ucwords(strtolower($prov)),
                'tmt_teachers' => $tmt,
                'email_teachers' => $email,
                'telp_teachers' => $telp
            ];
            $upload_image = $_FILES['image_user']['name'];
            if ($upload_image) {
                $config['allowed_types'] = 'jpg|png';
                $config['max_size'] = '512';
                $config['upload_path'] = './assets/rapor/img/users/';
                $this->upload->initialize($config);
                if ($this->upload->do_upload('image_user')) {
                    $old_image = $dtImageUser;
                    if ($old_image != 'user.png') {
                        unlink(FCPATH . 'assets/rapor/img/users/' . $old_image);
                    }
                    $new_image = $this->upload->data('file_name');
                    $this->db->set('image_user', $new_image);
                    $this->db->where('username',  $dtUsername);
                    $this->db->where('kd_teachers', $dtKdGuru);
                    $this->db->where('kd_user',  $dtKdUser);
                    $this->db->update('t_user');
                } else {
                    $this->session->set_flashdata('messageError', 'Jenis file jpg/png, size < 500 kb !');
                    redirect('guru/profil_user');
                }
            }
            $this->db->where('id_teachers',  $dtKdGuru);
            $this->db->where('kd_lembaga',   $dtKdUser);
            $this->db->update('t_teachers', $data);
            $this->db->set('nm_user', $namaGuru);
            $this->db->where('username', $dtUsername);
            $this->db->where('kd_teachers', $dtKdGuru);
            $this->db->where('kd_user',  $dtKdUser);
            $this->db->update('t_user');
            $this->session->set_flashdata('messageSave', 'Data ' . $namaGuru . ' berhasil diupdate !');
            redirect('guru/profil_user');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function prosesNilaiAkhirp()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $newKdstudents = $this->input->post('kd_students[]');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $data = array();
        foreach ($newKdstudents as $nrs => $rstd) {
            $data[$nrs]['kd_students'] = $rstd;
            $cekIdakhirp = $this->db->get_where('t_akhirp', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            $cekIdIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
        }
        if ($cekIdakhirp > 0) {
            $dt = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $idAkhirp = $this->db->get_where('t_akhirp', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->row_array();
                $dt[$nrs]['id_akhirp'] = $idAkhirp['id_akhirp'];
                $dt[$nrs]['kd_students'] = $rs;
                $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                $dt[$nrs]['aktif_p'] = 1;
                $dt[$nrs]['kd_tapel'] = $newTapel;
                $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
            }
            foreach ($newKdstudents as $nrs => $rsa) {
                $this->db->select_avg('n_p', 'rata');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $newTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $this->db->where('tipe_n',  'p');
                $query = $this->db->get('t_nilaip')->row_array();
                if (is_null($query)) {
                    $newNakhirp = 0;
                } else {
                    $newNakhirp =  round($query['rata'], 2);
                }
                $query1 = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'q' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query1)) {
                    $newNilaipts = 0;
                } else {
                    $newNilaipts = round($query1['n_p']);
                }
                $query2 = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'r' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query2)) {
                    $newNilaipas = 0;
                } else {
                    $newNilaipas = round($query2['n_p']);
                }
                $query3 = $this->db->query("SELECT n_kkmkls, b_hph, b_hpts, b_hpas FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query3)) {
                    $kkmkelas = 0;
                    $bobotHph = 0;
                    $bobotHpts = 0;
                    $bobotHpas = 0;
                } else {
                    $kkmkelas = round($query3['n_kkmkls']);
                    $bobotHph = round($query3['b_hph']);
                    $bobotHpts = round($query3['b_hpts']);
                    $bobotHpas = round($query3['b_hpas']);
                }
                $newNilaiHph = ($newNakhirp * $bobotHph) / 100;
                $newNilaiHpts = ($newNilaipts * $bobotHpts) / 100;
                $newNilaiHpas = ($newNilaipas * $bobotHpas) / 100;
                $nilaiAkhirp = $newNilaiHph + $newNilaiHpts + $newNilaiHpas;
                $input_kkm =  $kkmkelas;
                $intrvl_nli = round((100 - $input_kkm) / 3);
                $c = ($input_kkm + $intrvl_nli) - 1;
                $b1 = $input_kkm + $intrvl_nli;
                $b2 = ($b1 + $intrvl_nli) - 1;
                $a = $b1 + $intrvl_nli;
                $kkm_kelas = $input_kkm;
                $predikat =  $nilaiAkhirp;
                if ($predikat >= $a) {
                    $grade = "A";
                } elseif ($predikat >= $b1) {
                    $grade = "B";
                } elseif ($predikat >= $kkm_kelas) {
                    $grade = "C";
                } else {
                    $grade = "D";
                }
                $predikat = $grade;
                if ($predikat == 'A') {
                    $awaldesk = 'Ananda memiliki kemampuan sangat baik dalam ';
                } elseif ($predikat == 'B') {
                    $awaldesk = 'Ananda memiliki kemampuan yang baik dalam ';
                } elseif ($predikat == 'C') {
                    $awaldesk = 'Ananda perlu memaksimalkan kemampuan dalam ';
                } else {
                    $awaldesk = 'Ananda perlu memaksimalkan kemampuan dalam ';
                }
                $this->db->select_max('n_p', 'max');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $newTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $this->db->where('tipe_n',  'p');
                $query5 = $this->db->get('t_nilaip')->row_array();
                $deskp1 =  $query5['max'];
                $query6 = $this->db->query("SELECT deskp_n FROM t_nilaip WHERE n_p = ' $deskp1' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'p' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query6)) {
                    $newdespket1 = 'belum ada nilai pengetahuan dan deskripsi !';
                } else {
                    $newdespket1 = $query6['deskp_n'];
                }
                $this->db->select_min('n_p', 'min');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $newTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $this->db->where('tipe_n',  'p');
                $query7 = $this->db->get('t_nilaip')->row_array();
                $deskp2 =  $query7['min'];
                $query8 = $this->db->query("SELECT deskp_n FROM t_nilaip WHERE n_p = ' $deskp2' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'p' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query8)) {
                    $newdespket2 = 'belum ada nilai pengetahuan dan deskripsi !';
                } else {
                    $newdespket2 = $query8['deskp_n'];
                }
                if ($deskp1 == $deskp2) {
                    $newDeskripsipengetahuan = $awaldesk . $newdespket1;
                } elseif ($grade == 'C' or $grade == 'D') {
                    $newDeskripsipengetahuan = $awaldesk . $newdespket1 . ' dan kemampuan dalam ' . $newdespket2;
                } else {
                    $newDeskripsipengetahuan = $awaldesk . $newdespket1 . ' dan perlu memaksimalkan kemampuan dalam ' . $newdespket2;
                }
                $dt[$nrs]['deskripsi_p'] = $newDeskripsipengetahuan;
                $dt[$nrs]['n_akhirp'] = round($nilaiAkhirp);
                $dt[$nrs]['predikat_p'] = $grade;
            }
            $this->m_guru->updateDataNilaiAkhirp($dt, 'id_akhirp');
            $data = array();
            foreach ($newKdstudents as $nrs => $rstd) {
                $data[$nrs]['kd_students'] = $rstd;
                $cekIdIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            }
            if ($cekIdIjazah > 0) {
                foreach ($newKdstudents as $nrs => $rsu) {
                    $this->db->select(' t_kelas.nm_kelas as kelas,
                                        t_tapel.kd_smstr as semester
                    ');
                    $this->db->from('t_kelas');
                    $this->db->join('t_gmapel', 't_gmapel.kd_kelas = t_kelas.id_kelas');
                    $this->db->join('t_rombelsiswa', 't_kelas.id_kelas = t_rombelsiswa.kd_kelas');
                    $this->db->join('t_students', 't_rombelsiswa.kd_students = t_students.id_students');
                    $this->db->join('t_tapel', 't_tapel.id_tapel = t_rombelsiswa.kd_tapel');
                    $this->db->where('t_rombelsiswa.kd_students', $rsu);
                    $this->db->where('t_kelas.kd_tapel', $newTapel);
                    $this->db->where('t_kelas.kd_lembaga', $newKdlembaga);
                    $kelas_smstr = $this->db->get()->row_array();
                    if (!is_null($kelas_smstr)) {
                        $newKelas2 = $kelas_smstr['kelas'];
                        $newSemester2 = $kelas_smstr['semester'];
                    }
                    $newKelas = $newKelas2;
                    $newSemester = $newSemester2;
                    if (($newKelas == 'VI') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $idIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                            $dti[$nrs]['id_ijazah'] = $idIjazah['id_ijazah'];
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir41 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir41)) {
                                $np41 = $nilai_akhir41['nilaip'];
                                $nk41 = $nilai_akhir41['nilaik'];
                            } else {
                                $np41 = 0;
                                $nk41 = 0;
                            }
                            $p41 = $np41;
                            $k41 = $nk41;
                            $total_41 = round(($p41 +  $k41) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir42 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir42)) {
                                $np42 =  $nilai_akhir42['nilaip'];
                                $nk42 =  $nilai_akhir42['nilaik'];
                            } else {
                                $np42 =  0;
                                $nk42 =  0;
                            }
                            $p42 = $np42;
                            $k42 = $nk42;
                            $total_42 = round(($p42 +  $k42) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir51 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir51)) {
                                $np51 =  $nilai_akhir51['nilaip'];
                                $nk51 =  $nilai_akhir51['nilaik'];
                            } else {
                                $np51 = 0;
                                $nk51 = 0;
                            }
                            $p51 = $np51;
                            $k51 = $nk51;
                            $total_51 = round(($p51 +  $k51) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir52 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir52)) {
                                $np52 =  $nilai_akhir52['nilaip'];
                                $nk52 =  $nilai_akhir52['nilaik'];
                            } else {
                                $np52 =  0;
                                $nk52 =  0;
                            }
                            $p52 = $np52;
                            $k52 = $nk52;
                            $total_52 = round(($p52 +  $k52) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir61 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir61)) {
                                $np61 = $nilai_akhir61['nilaip'];
                                $nk61 = $nilai_akhir61['nilaik'];
                            } else {
                                $np61 = 0;
                                $nk61 = 0;
                            }
                            $p61 = $np61;
                            $k61 = $nk61;
                            $total_61 = round(($p61 +  $k61) / 2, 2);
                            $rata_rapor = round(($total_41 + $total_42 + $total_51 + $total_52 + $total_61) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->updateDataNilaiIjazah($dti, 'id_ijazah');
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil diupdate !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'IX') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $idIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                            $dti[$nrs]['id_ijazah'] = $idIjazah['id_ijazah'];
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir71 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir71)) {
                                $np71 = $nilai_akhir71['nilaip'];
                                $nk71 = $nilai_akhir71['nilaik'];
                            } else {
                                $np71 = 0;
                                $nk71 = 0;
                            }
                            $p71 = $np71;
                            $k71 = $nk71;
                            $total_71 = round(($p71 +  $k71) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir72 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir72)) {
                                $np72 = $nilai_akhir72['nilaip'];
                                $nk72 = $nilai_akhir72['nilaik'];
                            } else {
                                $np72 = 0;
                                $nk72 = 0;
                            }
                            $p72 = $np72;
                            $k72 = $nk72;
                            $total_72 = round(($p72 + $k72) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir81 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir81)) {
                                $np81 = $nilai_akhir81['nilaip'];
                                $nk81 = $nilai_akhir81['nilaik'];
                            } else {
                                $np81 = 0;
                                $nk81 = 0;
                            }
                            $p81 = $np81;
                            $k81 = $nk81;
                            $total_81 = round(($p81 + $k81) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir82 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir82)) {
                                $np82 = $nilai_akhir82['nilaip'];
                                $nk82 = $nilai_akhir82['nilaik'];
                            } else {
                                $np82 = 0;
                                $nk82 = 0;
                            }
                            $p82 = $np82;
                            $k82 = $nk82;
                            $total_82 = round(($p82 +  $k82) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IX');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir91 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir91)) {
                                $np91 = $nilai_akhir91['nilaip'];
                                $nk91 = $nilai_akhir91['nilaik'];
                            } else {
                                $np91 = 0;
                                $nk91 = 0;
                            }
                            $p91 = $np91;
                            $k91 = $nk91;
                            $total_91 = round(($p91 +  $k91) / 2, 2);
                            $rata_rapor = round(($total_71 + $total_72 + $total_81 + $total_82 + $total_91) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->updateDataNilaiIjazah($dti, 'id_ijazah');
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil diupdate !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'XII') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $idIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                            $dti[$nrs]['id_ijazah'] = $idIjazah['id_ijazah'];
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir101 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir101)) {
                                $np101 = $nilai_akhir101['nilaip'];
                                $nk101 = $nilai_akhir101['nilaik'];
                            } else {
                                $np101 = 0;
                                $nk101 = 0;
                            }
                            $p101 = $np101;
                            $k101 = $nk101;
                            $total_101 = round(($p101 +  $k101) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir102 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir102)) {
                                $np102 = $nilai_akhir102['nilaip'];
                                $nk102 = $nilai_akhir102['nilaik'];
                            } else {
                                $np102 = 0;
                                $nk102 = 0;
                            }
                            $p102 = $np102;
                            $k102 = $nk102;
                            $total_102 = round(($p102 +  $k102) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir111 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir111)) {
                                $np111 = $nilai_akhir111['nilaip'];
                                $nk111 = $nilai_akhir111['nilaik'];
                            } else {
                                $np111 = 0;
                                $nk111 = 0;
                            }
                            $p111 = $np111;
                            $k111 = $nk111;
                            $total_111 = round(($p111 +  $k111) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir112 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir112)) {
                                $np112 = $nilai_akhir112['nilaip'];
                                $nk112 = $nilai_akhir112['nilaik'];
                            } else {
                                $np112 = 0;
                                $nk112 = 0;
                            }
                            $p112 = $np112;
                            $k112 = $nk112;
                            $total_112 = round(($p112 +  $k112) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir121 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir121)) {
                                $np121 = $nilai_akhir121['nilaip'];
                                $nk121 = $nilai_akhir121['nilaik'];
                            } else {
                                $np121 = 0;
                                $nk121 = 0;
                            }
                            $p121 = $np121;
                            $k121 = $nk121;
                            $total_121 = round(($p121 +  $k121) / 2, 2);
                            $rata_rapor = round(($total_101 + $total_102 + $total_111 + $total_112 + $total_121) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->updateDataNilaiIjazah($dti, 'id_ijazah');
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil diupdate !');
                        redirect('guru/nilai_p');
                    }
                }
            } else {
                foreach ($newKdstudents as $nrs => $rsu) {
                    $this->db->select(' t_kelas.nm_kelas as kelas,
                                        t_tapel.kd_smstr as semester
                    ');
                    $this->db->from('t_kelas');
                    $this->db->join('t_gmapel', 't_gmapel.kd_kelas = t_kelas.id_kelas');
                    $this->db->join('t_rombelsiswa', 't_kelas.id_kelas = t_rombelsiswa.kd_kelas');
                    $this->db->join('t_students', 't_rombelsiswa.kd_students = t_students.id_students');
                    $this->db->join('t_tapel', 't_tapel.id_tapel = t_rombelsiswa.kd_tapel');
                    $this->db->where('t_rombelsiswa.kd_students', $rsu);
                    $this->db->where('t_kelas.kd_tapel', $newTapel);
                    $this->db->where('t_kelas.kd_lembaga', $newKdlembaga);
                    $kelas_smstr = $this->db->get()->row_array();
                    if (!is_null($kelas_smstr)) {
                        $newKelas2 = $kelas_smstr['kelas'];
                        $newSemester2 = $kelas_smstr['semester'];
                    }
                    $newKelas = $newKelas2;
                    $newSemester = $newSemester2;
                    if (($newKelas == 'VI') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir41 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir41)) {
                                $np41 = $nilai_akhir41['nilaip'];
                                $nk41 = $nilai_akhir41['nilaik'];
                            } else {
                                $np41 = 0;
                                $nk41 = 0;
                            }
                            $p41 = $np41;
                            $k41 = $nk41;
                            $total_41 = round(($p41 +  $k41) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir42 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir42)) {
                                $np42 =  $nilai_akhir42['nilaip'];
                                $nk42 =  $nilai_akhir42['nilaik'];
                            } else {
                                $np42 =  0;
                                $nk42 =  0;
                            }
                            $p42 = $np42;
                            $k42 = $nk42;
                            $total_42 = round(($p42 +  $k42) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir51 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir51)) {
                                $np51 =  $nilai_akhir51['nilaip'];
                                $nk51 =  $nilai_akhir51['nilaik'];
                            } else {
                                $np51 = 0;
                                $nk51 = 0;
                            }
                            $p51 = $np51;
                            $k51 = $nk51;
                            $total_51 = round(($p51 +  $k51) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir52 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir52)) {
                                $np52 =  $nilai_akhir52['nilaip'];
                                $nk52 =  $nilai_akhir52['nilaik'];
                            } else {
                                $np52 =  0;
                                $nk52 =  0;
                            }
                            $p52 = $np52;
                            $k52 = $nk52;
                            $total_52 = round(($p52 +  $k52) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir61 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir61)) {
                                $np61 = $nilai_akhir61['nilaip'];
                                $nk61 = $nilai_akhir61['nilaik'];
                            } else {
                                $np61 = 0;
                                $nk61 = 0;
                            }
                            $p61 = $np61;
                            $k61 = $nk61;
                            $total_61 = round(($p61 +  $k61) / 2, 2);
                            $rata_rapor = round(($total_41 + $total_42 + $total_51 + $total_52 + $total_61) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->inputDataNilaiIjazah($dti);
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil ditambahkan !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'IX') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir71 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir71)) {
                                $np71 = $nilai_akhir71['nilaip'];
                                $nk71 = $nilai_akhir71['nilaik'];
                            } else {
                                $np71 = 0;
                                $nk71 = 0;
                            }
                            $p71 = $np71;
                            $k71 = $nk71;
                            $total_71 = round(($p71 +  $k71) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir72 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir72)) {
                                $np72 = $nilai_akhir72['nilaip'];
                                $nk72 = $nilai_akhir72['nilaik'];
                            } else {
                                $np72 = 0;
                                $nk72 = 0;
                            }
                            $p72 = $np72;
                            $k72 = $nk72;
                            $total_72 = round(($p72 + $k72) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir81 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir81)) {
                                $np81 = $nilai_akhir81['nilaip'];
                                $nk81 = $nilai_akhir81['nilaik'];
                            } else {
                                $np81 = 0;
                                $nk81 = 0;
                            }
                            $p81 = $np81;
                            $k81 = $nk81;
                            $total_81 = round(($p81 + $k81) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir82 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir82)) {
                                $np82 = $nilai_akhir82['nilaip'];
                                $nk82 = $nilai_akhir82['nilaik'];
                            } else {
                                $np82 = 0;
                                $nk82 = 0;
                            }
                            $p82 = $np82;
                            $k82 = $nk82;
                            $total_82 = round(($p82 +  $k82) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IX');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir91 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir91)) {
                                $np91 = $nilai_akhir91['nilaip'];
                                $nk91 = $nilai_akhir91['nilaik'];
                            } else {
                                $np91 = 0;
                                $nk91 = 0;
                            }
                            $p91 = $np91;
                            $k91 = $nk91;
                            $total_91 = round(($p91 +  $k91) / 2, 2);
                            $rata_rapor = round(($total_71 + $total_72 + $total_81 + $total_82 + $total_91) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->inputDataNilaiIjazah($dti);
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil ditambahkan !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'XII') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir101 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir101)) {
                                $np101 = $nilai_akhir101['nilaip'];
                                $nk101 = $nilai_akhir101['nilaik'];
                            } else {
                                $np101 = 0;
                                $nk101 = 0;
                            }
                            $p101 = $np101;
                            $k101 = $nk101;
                            $total_101 = round(($p101 +  $k101) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir102 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir102)) {
                                $np102 = $nilai_akhir102['nilaip'];
                                $nk102 = $nilai_akhir102['nilaik'];
                            } else {
                                $np102 = 0;
                                $nk102 = 0;
                            }
                            $p102 = $np102;
                            $k102 = $nk102;
                            $total_102 = round(($p102 +  $k102) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir111 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir111)) {
                                $np111 = $nilai_akhir111['nilaip'];
                                $nk111 = $nilai_akhir111['nilaik'];
                            } else {
                                $np111 = 0;
                                $nk111 = 0;
                            }
                            $p111 = $np111;
                            $k111 = $nk111;
                            $total_111 = round(($p111 +  $k111) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir112 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir112)) {
                                $np112 = $nilai_akhir112['nilaip'];
                                $nk112 = $nilai_akhir112['nilaik'];
                            } else {
                                $np112 = 0;
                                $nk112 = 0;
                            }
                            $p112 = $np112;
                            $k112 = $nk112;
                            $total_112 = round(($p112 +  $k112) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir121 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir121)) {
                                $np121 = $nilai_akhir121['nilaip'];
                                $nk121 = $nilai_akhir121['nilaik'];
                            } else {
                                $np121 = 0;
                                $nk121 = 0;
                            }
                            $p121 = $np121;
                            $k121 = $nk121;
                            $total_121 = round(($p121 +  $k121) / 2, 2);
                            $rata_rapor = round(($total_101 + $total_102 + $total_111 + $total_112 + $total_121) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->inputDataNilaiIjazah($dti);
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil ditambahkan !');
                        redirect('guru/nilai_p');
                    }
                }
            }
            $this->session->set_flashdata('messageSuccess', 'Data nilai akhir berhasil diupdate !');
            redirect('guru/nilai_p');
        } else {
            $dt = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $dt[$nrs]['kd_students'] = $rs;
                $dt[$nrs]['aktif_p'] = 1;
                $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                $dt[$nrs]['kd_tapel'] = $newTapel;
                $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
            }
            foreach ($newKdstudents as $nrs => $rsa) {
                $this->db->select_avg('n_p', 'rata');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $newTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $query = $this->db->get('t_nilaip')->row_array();
                if (is_null($query)) {
                    $newNakhirp = 0;
                } else {
                    $newNakhirp =  round($query['rata'], 2);
                }
                $query1 = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'q' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query1)) {
                    $newNilaipts = 0;
                } else {
                    $newNilaipts = round($query1['n_p']);
                }
                $query2 = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'r' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query2)) {
                    $newNilaipas = 0;
                } else {
                    $newNilaipas = round($query2['n_p']);
                }
                $query3 = $this->db->query("SELECT n_kkmkls, b_hph, b_hpts, b_hpas FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query3)) {
                    $kkmkelas = 0;
                    $bobotHph = 0;
                    $bobotHpts = 0;
                    $bobotHpas = 0;
                } else {
                    $kkmkelas = round($query3['n_kkmkls']);
                    $bobotHph = round($query3['b_hph']);
                    $bobotHpts = round($query3['b_hpts']);
                    $bobotHpas = round($query3['b_hpas']);
                }
                $newNilaiHph = ($newNakhirp * $bobotHph) / 100;
                $newNilaiHpts = ($newNilaipts * $bobotHpts) / 100;
                $newNilaiHpas = ($newNilaipas * $bobotHpas) / 100;
                $nilaiAkhirp = $newNilaiHph + $newNilaiHpts + $newNilaiHpas;
                $input_kkm =  $kkmkelas;
                $intrvl_nli = round((100 - $input_kkm) / 3);
                $c = ($input_kkm + $intrvl_nli) - 1;
                $b1 = $input_kkm + $intrvl_nli;
                $b2 = ($b1 + $intrvl_nli) - 1;
                $a = $b1 + $intrvl_nli;
                $kkm_kelas = $input_kkm;
                $predikat =  $nilaiAkhirp;
                if ($predikat >= $a) {
                    $grade = "A";
                } elseif ($predikat >= $b1) {
                    $grade = "B";
                } elseif ($predikat >= $kkm_kelas) {
                    $grade = "C";
                } else {
                    $grade = "D";
                }
                $predikat = $grade;
                if ($predikat == 'A') {
                    $awaldesk = 'Ananda memiliki kemampuan sangat baik dalam ';
                } elseif ($predikat == 'B') {
                    $awaldesk = 'Ananda memiliki kemampuan yang baik dalam ';
                } elseif ($predikat == 'C') {
                    $awaldesk = 'Ananda perlu memaksimalkan kemampuan dalam ';
                } else {
                    $awaldesk = 'Ananda perlu memaksimalkan kemampuan dalam ';
                }
                $this->db->select_max('n_p', 'max');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $newTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $this->db->where('tipe_n',  'p');
                $query5 = $this->db->get('t_nilaip')->row_array();
                $deskp1 =  $query5['max'];
                $query6 = $this->db->query("SELECT deskp_n FROM t_nilaip WHERE n_p = ' $deskp1' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'p' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                $newdespket1 = $query6['deskp_n'];
                $this->db->select_min('n_p', 'min');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $newTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $this->db->where('tipe_n',  'p');
                $query7 = $this->db->get('t_nilaip')->row_array();
                $deskp2 =  $query7['min'];
                $query8 = $this->db->query("SELECT deskp_n FROM t_nilaip WHERE n_p = ' $deskp2' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND tipe_n = 'p' AND kd_tapel = '$newTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                $newdespket2 = $query8['deskp_n'];
                if ($deskp1 == $deskp2) {
                    $newDeskripsipengetahuan = $awaldesk . $newdespket1;
                } elseif ($grade == 'C' or $grade == 'D') {
                    $newDeskripsipengetahuan = $awaldesk . $newdespket1 . ' dan kemampuan dalam ' . $newdespket2;
                } else {
                    $newDeskripsipengetahuan = $awaldesk . $newdespket1 . ' dan perlu memaksimalkan kemampuan dalam ' . $newdespket2;
                }
                $dt[$nrs]['deskripsi_p'] = $newDeskripsipengetahuan;
                $dt[$nrs]['n_akhirp'] = round($nilaiAkhirp);
                $dt[$nrs]['predikat_p'] = $grade;
            }
            $this->m_guru->inputDataNilaiAkhirp($dt);
            $data = array();
            foreach ($newKdstudents as $nrs => $rstd) {
                $data[$nrs]['kd_students'] = $rstd;
                $cekIdIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            }
            if ($cekIdIjazah > 0) {
                foreach ($newKdstudents as $nrs => $rsu) {
                    $this->db->select(' t_kelas.nm_kelas as kelas,
                                        t_tapel.kd_smstr as semester
                    ');
                    $this->db->from('t_kelas');
                    $this->db->join('t_gmapel', 't_gmapel.kd_kelas = t_kelas.id_kelas');
                    $this->db->join('t_rombelsiswa', 't_kelas.id_kelas = t_rombelsiswa.kd_kelas');
                    $this->db->join('t_students', 't_rombelsiswa.kd_students = t_students.id_students');
                    $this->db->join('t_tapel', 't_tapel.id_tapel = t_rombelsiswa.kd_tapel');
                    $this->db->where('t_rombelsiswa.kd_students', $rsu);
                    $this->db->where('t_kelas.kd_tapel', $newTapel);
                    $this->db->where('t_kelas.kd_lembaga', $newKdlembaga);
                    $kelas_smstr = $this->db->get()->row_array();
                    if (!is_null($kelas_smstr)) {
                        $newKelas2 = $kelas_smstr['kelas'];
                        $newSemester2 = $kelas_smstr['semester'];
                    }
                    $newKelas = $newKelas2;
                    $newSemester = $newSemester2;
                    if (($newKelas == 'VI') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $idIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                            $dti[$nrs]['id_ijazah'] = $idIjazah['id_ijazah'];
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir41 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir41)) {
                                $np41 = $nilai_akhir41['nilaip'];
                                $nk41 = $nilai_akhir41['nilaik'];
                            } else {
                                $np41 = 0;
                                $nk41 = 0;
                            }
                            $p41 = $np41;
                            $k41 = $nk41;
                            $total_41 = round(($p41 +  $k41) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir42 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir42)) {
                                $np42 =  $nilai_akhir42['nilaip'];
                                $nk42 =  $nilai_akhir42['nilaik'];
                            } else {
                                $np42 =  0;
                                $nk42 =  0;
                            }
                            $p42 = $np42;
                            $k42 = $nk42;
                            $total_42 = round(($p42 +  $k42) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir51 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir51)) {
                                $np51 =  $nilai_akhir51['nilaip'];
                                $nk51 =  $nilai_akhir51['nilaik'];
                            } else {
                                $np51 = 0;
                                $nk51 = 0;
                            }
                            $p51 = $np51;
                            $k51 = $nk51;
                            $total_51 = round(($p51 +  $k51) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir52 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir52)) {
                                $np52 =  $nilai_akhir52['nilaip'];
                                $nk52 =  $nilai_akhir52['nilaik'];
                            } else {
                                $np52 =  0;
                                $nk52 =  0;
                            }
                            $p52 = $np52;
                            $k52 = $nk52;
                            $total_52 = round(($p52 +  $k52) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir61 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir61)) {
                                $np61 = $nilai_akhir61['nilaip'];
                                $nk61 = $nilai_akhir61['nilaik'];
                            } else {
                                $np61 = 0;
                                $nk61 = 0;
                            }
                            $p61 = $np61;
                            $k61 = $nk61;
                            $total_61 = round(($p61 +  $k61) / 2, 2);
                            $rata_rapor = round(($total_41 + $total_42 + $total_51 + $total_52 + $total_61) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->updateDataNilaiIjazah($dti, 'id_ijazah');
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil diupdate !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'IX') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $idIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                            $dti[$nrs]['id_ijazah'] = $idIjazah['id_ijazah'];
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir71 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir71)) {
                                $np71 = $nilai_akhir71['nilaip'];
                                $nk71 = $nilai_akhir71['nilaik'];
                            } else {
                                $np71 = 0;
                                $nk71 = 0;
                            }
                            $p71 = $np71;
                            $k71 = $nk71;
                            $total_71 = round(($p71 +  $k71) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir72 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir72)) {
                                $np72 = $nilai_akhir72['nilaip'];
                                $nk72 = $nilai_akhir72['nilaik'];
                            } else {
                                $np72 = 0;
                                $nk72 = 0;
                            }
                            $p72 = $np72;
                            $k72 = $nk72;
                            $total_72 = round(($p72 + $k72) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir81 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir81)) {
                                $np81 = $nilai_akhir81['nilaip'];
                                $nk81 = $nilai_akhir81['nilaik'];
                            } else {
                                $np81 = 0;
                                $nk81 = 0;
                            }
                            $p81 = $np81;
                            $k81 = $nk81;
                            $total_81 = round(($p81 + $k81) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir82 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir82)) {
                                $np82 = $nilai_akhir82['nilaip'];
                                $nk82 = $nilai_akhir82['nilaik'];
                            } else {
                                $np82 = 0;
                                $nk82 = 0;
                            }
                            $p82 = $np82;
                            $k82 = $nk82;
                            $total_82 = round(($p82 +  $k82) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IX');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir91 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir91)) {
                                $np91 = $nilai_akhir91['nilaip'];
                                $nk91 = $nilai_akhir91['nilaik'];
                            } else {
                                $np91 = 0;
                                $nk91 = 0;
                            }
                            $p91 = $np91;
                            $k91 = $nk91;
                            $total_91 = round(($p91 +  $k91) / 2, 2);
                            $rata_rapor = round(($total_71 + $total_72 + $total_81 + $total_82 + $total_91) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->updateDataNilaiIjazah($dti, 'id_ijazah');
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil diupdate !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'XII') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $idIjazah = $this->db->get_where('t_ijazah', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                            $dti[$nrs]['id_ijazah'] = $idIjazah['id_ijazah'];
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir101 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir101)) {
                                $np101 = $nilai_akhir101['nilaip'];
                                $nk101 = $nilai_akhir101['nilaik'];
                            } else {
                                $np101 = 0;
                                $nk101 = 0;
                            }
                            $p101 = $np101;
                            $k101 = $nk101;
                            $total_101 = round(($p101 +  $k101) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir102 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir102)) {
                                $np102 = $nilai_akhir102['nilaip'];
                                $nk102 = $nilai_akhir102['nilaik'];
                            } else {
                                $np102 = 0;
                                $nk102 = 0;
                            }
                            $p102 = $np102;
                            $k102 = $nk102;
                            $total_102 = round(($p102 +  $k102) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir111 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir111)) {
                                $np111 = $nilai_akhir111['nilaip'];
                                $nk111 = $nilai_akhir111['nilaik'];
                            } else {
                                $np111 = 0;
                                $nk111 = 0;
                            }
                            $p111 = $np111;
                            $k111 = $nk111;
                            $total_111 = round(($p111 +  $k111) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir112 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir112)) {
                                $np112 = $nilai_akhir112['nilaip'];
                                $nk112 = $nilai_akhir112['nilaik'];
                            } else {
                                $np112 = 0;
                                $nk112 = 0;
                            }
                            $p112 = $np112;
                            $k112 = $nk112;
                            $total_112 = round(($p112 +  $k112) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir121 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir121)) {
                                $np121 = $nilai_akhir121['nilaip'];
                                $nk121 = $nilai_akhir121['nilaik'];
                            } else {
                                $np121 = 0;
                                $nk121 = 0;
                            }
                            $p121 = $np121;
                            $k121 = $nk121;
                            $total_121 = round(($p121 +  $k121) / 2, 2);
                            $rata_rapor = round(($total_101 + $total_102 + $total_111 + $total_112 + $total_121) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->updateDataNilaiIjazah($dti, 'id_ijazah');
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil diupdate !');
                        redirect('guru/nilai_p');
                    }
                }
            } else {
                foreach ($newKdstudents as $nrs => $rsu) {
                    $this->db->select(' t_kelas.nm_kelas as kelas,
                                        t_tapel.kd_smstr as semester
                    ');
                    $this->db->from('t_kelas');
                    $this->db->join('t_gmapel', 't_gmapel.kd_kelas = t_kelas.id_kelas');
                    $this->db->join('t_rombelsiswa', 't_kelas.id_kelas = t_rombelsiswa.kd_kelas');
                    $this->db->join('t_students', 't_rombelsiswa.kd_students = t_students.id_students');
                    $this->db->join('t_tapel', 't_tapel.id_tapel = t_rombelsiswa.kd_tapel');
                    $this->db->where('t_rombelsiswa.kd_students', $rsu);
                    $this->db->where('t_kelas.kd_tapel', $newTapel);
                    $this->db->where('t_kelas.kd_lembaga', $newKdlembaga);
                    $kelas_smstr = $this->db->get()->row_array();
                    if (!is_null($kelas_smstr)) {
                        $newKelas2 = $kelas_smstr['kelas'];
                        $newSemester2 = $kelas_smstr['semester'];
                    }
                    $newKelas = $newKelas2;
                    $newSemester = $newSemester2;
                    if (($newKelas == 'VI') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir41 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir41)) {
                                $np41 = $nilai_akhir41['nilaip'];
                                $nk41 = $nilai_akhir41['nilaik'];
                            } else {
                                $np41 = 0;
                                $nk41 = 0;
                            }
                            $p41 = $np41;
                            $k41 = $nk41;
                            $total_41 = round(($p41 +  $k41) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'IV');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir42 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir42)) {
                                $np42 =  $nilai_akhir42['nilaip'];
                                $nk42 =  $nilai_akhir42['nilaik'];
                            } else {
                                $np42 =  0;
                                $nk42 =  0;
                            }
                            $p42 = $np42;
                            $k42 = $nk42;
                            $total_42 = round(($p42 +  $k42) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir51 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir51)) {
                                $np51 =  $nilai_akhir51['nilaip'];
                                $nk51 =  $nilai_akhir51['nilaik'];
                            } else {
                                $np51 = 0;
                                $nk51 = 0;
                            }
                            $p51 = $np51;
                            $k51 = $nk51;
                            $total_51 = round(($p51 +  $k51) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'V');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir52 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir52)) {
                                $np52 =  $nilai_akhir52['nilaip'];
                                $nk52 =  $nilai_akhir52['nilaik'];
                            } else {
                                $np52 =  0;
                                $nk52 =  0;
                            }
                            $p52 = $np52;
                            $k52 = $nk52;
                            $total_52 = round(($p52 +  $k52) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir61 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir61)) {
                                $np61 = $nilai_akhir61['nilaip'];
                                $nk61 = $nilai_akhir61['nilaik'];
                            } else {
                                $np61 = 0;
                                $nk61 = 0;
                            }
                            $p61 = $np61;
                            $k61 = $nk61;
                            $total_61 = round(($p61 +  $k61) / 2, 2);
                            $rata_rapor = round(($total_41 + $total_42 + $total_51 + $total_52 + $total_61) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->inputDataNilaiIjazah($dti);
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil ditambahkan !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'IX') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir71 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir71)) {
                                $np71 = $nilai_akhir71['nilaip'];
                                $nk71 = $nilai_akhir71['nilaik'];
                            } else {
                                $np71 = 0;
                                $nk71 = 0;
                            }
                            $p71 = $np71;
                            $k71 = $nk71;
                            $total_71 = round(($p71 +  $k71) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir72 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir72)) {
                                $np72 = $nilai_akhir72['nilaip'];
                                $nk72 = $nilai_akhir72['nilaik'];
                            } else {
                                $np72 = 0;
                                $nk72 = 0;
                            }
                            $p72 = $np72;
                            $k72 = $nk72;
                            $total_72 = round(($p72 + $k72) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir81 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir81)) {
                                $np81 = $nilai_akhir81['nilaip'];
                                $nk81 = $nilai_akhir81['nilaik'];
                            } else {
                                $np81 = 0;
                                $nk81 = 0;
                            }
                            $p81 = $np81;
                            $k81 = $nk81;
                            $total_81 = round(($p81 + $k81) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'VIII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir82 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir82)) {
                                $np82 = $nilai_akhir82['nilaip'];
                                $nk82 = $nilai_akhir82['nilaik'];
                            } else {
                                $np82 = 0;
                                $nk82 = 0;
                            }
                            $p82 = $np82;
                            $k82 = $nk82;
                            $total_82 = round(($p82 +  $k82) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'IX');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir91 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir91)) {
                                $np91 = $nilai_akhir91['nilaip'];
                                $nk91 = $nilai_akhir91['nilaik'];
                            } else {
                                $np91 = 0;
                                $nk91 = 0;
                            }
                            $p91 = $np91;
                            $k91 = $nk91;
                            $total_91 = round(($p91 +  $k91) / 2, 2);
                            $rata_rapor = round(($total_71 + $total_72 + $total_81 + $total_82 + $total_91) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->inputDataNilaiIjazah($dti);
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil ditambahkan !');
                        redirect('guru/nilai_p');
                    } elseif (($newKelas == 'XII') && ($newSemester == 'Genap')) {
                        $dti = array();
                        foreach ($newKdstudents as $nrs => $rs) {
                            $dti[$nrs]['kd_students'] = $rs;
                            $dti[$nrs]['kd_gmapel'] =  $newKdgmapel;
                            $dti[$nrs]['aktif_i'] = 1;
                            $dti[$nrs]['kd_tapel'] = $newTapel;
                            $dti[$nrs]['kd_lembaga'] = $newKdlembaga;
                        }
                        foreach ($newKdstudents as $nrs => $rsa) {
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir101 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir101)) {
                                $np101 = $nilai_akhir101['nilaip'];
                                $nk101 = $nilai_akhir101['nilaik'];
                            } else {
                                $np101 = 0;
                                $nk101 = 0;
                            }
                            $p101 = $np101;
                            $k101 = $nk101;
                            $total_101 = round(($p101 +  $k101) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'X');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir102 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir102)) {
                                $np102 = $nilai_akhir102['nilaip'];
                                $nk102 = $nilai_akhir102['nilaik'];
                            } else {
                                $np102 = 0;
                                $nk102 = 0;
                            }
                            $p102 = $np102;
                            $k102 = $nk102;
                            $total_102 = round(($p102 +  $k102) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir111 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir111)) {
                                $np111 = $nilai_akhir111['nilaip'];
                                $nk111 = $nilai_akhir111['nilaik'];
                            } else {
                                $np111 = 0;
                                $nk111 = 0;
                            }
                            $p111 = $np111;
                            $k111 = $nk111;
                            $total_111 = round(($p111 +  $k111) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Genap');
                            $this->db->where('t_kelas.nm_kelas', 'XI');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir112 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir112)) {
                                $np112 = $nilai_akhir112['nilaip'];
                                $nk112 = $nilai_akhir112['nilaik'];
                            } else {
                                $np112 = 0;
                                $nk112 = 0;
                            }
                            $p112 = $np112;
                            $k112 = $nk112;
                            $total_112 = round(($p112 +  $k112) / 2, 2);
                            $this->db->select(' t_akhirp.n_akhirp as nilaip,
                                                t_akhirk.n_akhirk as nilaik
                            ');
                            $this->db->from('t_akhirk');
                            $this->db->join('t_gmapel', 't_akhirk.kd_gmapel = t_gmapel.id_gmapel');
                            $this->db->join('t_kelas', 't_kelas.id_kelas = t_gmapel.kd_kelas');
                            $this->db->join('t_bidstudy', 't_gmapel.kd_bidstudy = t_bidstudy.id_bidstudy');
                            $this->db->join('t_akhirp', 't_akhirp.kd_gmapel = t_akhirk.kd_gmapel');
                            $this->db->join('t_students', 't_akhirk.kd_students = t_students.id_students');
                            $this->db->join('t_tapel', 't_tapel.id_tapel = t_gmapel.kd_tapel');
                            $this->db->where('t_tapel.kd_smstr', 'Ganjil');
                            $this->db->where('t_kelas.nm_kelas', 'XII');
                            $this->db->where('t_akhirp.kd_students',  $rsa);
                            $this->db->where('t_akhirk.kd_students',  $rsa);
                            $this->db->where('t_akhirp.kd_lembaga', $newKdlembaga);
                            $this->db->where('t_akhirk.kd_lembaga', $newKdlembaga);
                            $nilai_akhir121 = $this->db->get()->row_array();
                            if (!is_null($nilai_akhir121)) {
                                $np121 = $nilai_akhir121['nilaip'];
                                $nk121 = $nilai_akhir121['nilaik'];
                            } else {
                                $np121 = 0;
                                $nk121 = 0;
                            }
                            $p121 = $np121;
                            $k121 = $nk121;
                            $total_121 = round(($p121 +  $k121) / 2, 2);
                            $rata_rapor = round(($total_101 + $total_102 + $total_111 + $total_112 + $total_121) / 5, 2);
                            $this->db->select(' t_bobot.rapor_bobot as rapor,
                                                t_bobot.um_bobot as um
                            ');
                            $this->db->from('t_bobot');
                            $this->db->where('kd_tapel', $newTapel);
                            $this->db->where('kd_lembaga', $newKdlembaga);
                            $bobot = $this->db->get()->row_array();
                            if (!is_null($bobot)) {
                                $newBobotUmn = $bobot['um'];
                                $newBobotRaporn = $bobot['rapor'];
                            } else {
                                $newBobotUmn = 0;
                                $newBobotRaporn = 0;
                            }
                            $BobotRaporNew =  $newBobotRaporn;
                            $BobotUmNew =  $newBobotUmn;
                            $nilaiUm = $this->db->query("SELECT n_p FROM t_nilaip WHERE kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$newTapel' AND tipe_n = 's' AND kd_lembaga = '$newKdlembaga'")->row_array();
                            if (!is_null($nilaiUm)) {
                                $newNilaiUm = round($nilaiUm['n_p']);
                            } else {
                                $newNilaiUm = 0;
                            }
                            $nilaiRataRapor = ($rata_rapor * $BobotRaporNew) / 100;
                            $nilaiUjianMadrasah = ($newNilaiUm * $BobotUmNew) / 100;
                            $nilaiIjazah = $nilaiRataRapor + $nilaiUjianMadrasah;
                            $terbilangIjazah = ucwords(number_ke_words(round($nilaiIjazah, 0)));
                            $dti[$nrs]['rata_rapor']    = round($rata_rapor, 2);
                            $dti[$nrs]['n_um']          = round($newNilaiUm);
                            $dti[$nrs]['n_ijazah']      = round($nilaiIjazah);
                            $dti[$nrs]['terbilang']     = $terbilangIjazah;
                        }
                        $this->m_guru->inputDataNilaiIjazah($dti);
                        $this->session->set_flashdata('messageSuccess', 'Data nilai akhir & nilai ijazah berhasil ditambahkan !');
                        redirect('guru/nilai_p');
                    }
                }
            }
            $this->session->set_flashdata('messageSuccess', 'Data nilai akhir berhasil ditambahkan !');
            redirect('guru/nilai_p');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function inputNilaipts()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('kd_students[]', 'Kode Siswa', 'required|trim');
        $this->form_validation->set_rules('kd_gmapel', 'Kode Guru Mapel', 'required|trim');
        $this->form_validation->set_rules('n_p[]', 'Nilai Pengetahuan', 'required|numeric|trim');
        $newKdstudents = $this->input->post('kd_students');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $newNilaip = $this->input->post('n_p');
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $data = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $data[$nrs]['kd_students'] = $rs;
            }
            $cekKdp = $this->db->get_where('t_nilaip', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga, 'tipe_n' => 'q'))->num_rows();
            if ($cekKdp > 0) {
                $data =  $this->input->post();
                for ($i = 0; $i < count($data['id_nilaip']); $i++) {
                    $update[] = array(
                        'id_nilaip'     => $data['id_nilaip'][$i],
                        'kd_students'   => $data['kd_students'][$i],
                        'kd_gmapel'     => $newKdgmapel,
                        'n_p'           => $data['n_p'][$i],
                        'tipe_n'        => 'q',
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga,
                    );
                }
                $this->m_guru->updateDataNilaip($update, 'id_nilaip');
                $this->session->set_flashdata('messageSave', 'Penilaian tengah semester berhasil diupdate !');
                redirect('guru/nilai_p');
            } else {
                $data = array();
                foreach ($newKdstudents as $nrs => $rs) {
                    $data[$nrs]['kd_students'] = $rs;
                    $data[$nrs]['kd_gmapel'] =  $newKdgmapel;
                    $data[$nrs]['n_p'] = $newNilaip;
                    $data[$nrs]['tipe_n'] = 'q';
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                foreach ($newNilaip as $nrs => $rs) {
                    $data[$nrs]['n_p'] = $rs;
                }
                $this->m_guru->inputDataNilaip($data);
                $this->session->set_flashdata('messageSave', 'Penilaian tengah semester berhasil ditambahkan !');
                redirect('guru/nilai_p');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function nilai_k()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $newKdGuru = $data['nmUser']['kd_teachers'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel;
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKdk'] = $this->db->order_by('no_kdk', SORT_ASC)->get('t_kdk')->result_array();
        $data['kdk'] = $this->db->get('t_nokdk')->result_array();
        $data['nmKdk'] = $this->db->get('t_nmkdk')->result_array();
        $data['nilaik'] = $this->db->get('t_nilaik')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['dtNilaik'] = $this->m_guru->viewDataNilaik();
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Nilai Keterampilan';
        $cekRombelSiswa = $this->db->get_where('t_rombelsiswa', array('kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']['id_tapel']))->num_rows();
        $kdGmapel = $this->db->get_where('t_gmapel', array('kd_teachers' => $newKdGuru, 'kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']['id_tapel']))->row_array();
        $cekKkm = $this->db->get_where('t_kkm', array('kd_gmapel' => $kdGmapel['id_gmapel'], 'kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']['id_tapel']))->num_rows();
        $this->load->view('template/header', $data);
        $this->load->view('template/sidebar', $data);
        $this->load->view('template/headertop', $data);
        $this->load->view('template/mobile', $data);
        if ($cekRombelSiswa > 0) {
            if ($cekKkm > 0) {
                $this->load->view('guru/nilai_k', $data);
            } else {
                $this->session->set_flashdata('messageWarning', 'KKM anda belum di seting !');
                $this->load->view('guru/nilai_k', $data);
            }
        } else {
            $this->session->set_flashdata('messageInfo2', 'Anda belum mempunyai rombel siswa !');
            $this->load->view('guru/nilai_k', $data);
        }
        $this->load->view('template/footer', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function inputNilaik()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
            $kdTapel = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('kd_students[]', 'Kode Siswa', 'required|trim');
        $this->form_validation->set_rules('kd_gmapel', 'Kode Guru Mapel', 'required|trim');
        $this->form_validation->set_rules('kd_kdk', 'Kode KD', 'required|trim');
        $this->form_validation->set_rules('n_k[]', 'Nilai Keterampilan', 'required|numeric|trim');
        $this->form_validation->set_rules('deskk_n', 'Deskripsi KD', 'required|trim');
        $newKdstudents = $this->input->post('kd_students');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $newKdk = htmlspecialchars($this->input->post('kd_kdk'));
        $newNilaik = $this->input->post('n_k');
        $newDeskk = htmlspecialchars($this->input->post('deskk_n'));
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_k');
        } else {
            $data = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $data[$nrs]['kd_students'] = $rs;
            }
            $cekKdk = $this->db->get_where('t_nilaik', array('kd_gmapel' => $newKdgmapel, 'kd_kdk' => $newKdk, 'kd_students' => $rs, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga, 'tipe_n' => 'k'))->num_rows();
            if ($cekKdk > 0) {
                $data =  $this->input->post();
                for ($i = 0; $i < count($data['id_nilaik']); $i++) {
                    $update[] = array(
                        'id_nilaik'     => $data['id_nilaik'][$i],
                        'kd_students'   => $data['kd_students'][$i],
                        'kd_gmapel'     => $newKdgmapel,
                        'kd_kdk'        => $newKdk,
                        'n_k'           => $data['n_k'][$i],
                        'deskk_n'       => $newDeskk,
                        'tipe_n'        => 'k',
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga,
                    );
                }
                $this->m_guru->updateDataNilaik($update, 'id_nilaik');
                $data = array();
                foreach ($newKdstudents as $nrs => $rstd) {
                    $data[$nrs]['kd_students'] = $rstd;
                    $cekIdakhir = $this->db->get_where('t_akhirk', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                }
                if ($cekIdakhir > 0) {
                    $dt = array();
                    foreach ($newKdstudents as $nrs => $rs) {
                        $idAkhirk = $this->db->get_where('t_akhirk', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->row_array();
                        $dt[$nrs]['id_akhirk'] = $idAkhirk['id_akhirk'];
                        $dt[$nrs]['kd_students'] = $rs;
                        $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                        $dt[$nrs]['aktif_k'] = 1;
                        $dt[$nrs]['kd_tapel'] = $newTapel;
                        $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
                    }
                    foreach ($newKdstudents as $nrs => $rsa) {
                        $this->db->select_avg('n_k', 'rata');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query = $this->db->get('t_nilaik')->row_array();
                        if (is_null($query)) {
                            $newNakhirk = 0;
                        } else {
                            $newNakhirk =  round($query['rata'], 2);
                        }
                        $query1 = $this->db->query("SELECT n_kkmkls FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query1)) {
                            $kkmkelas = 0;
                        } else {
                            $kkmkelas = round($query1['n_kkmkls']);
                        }
                        $input_kkm = $kkmkelas;
                        $intrvl_nli = round((100 - $input_kkm) / 3);
                        $c = ($input_kkm + $intrvl_nli) - 1;
                        $b1 = $input_kkm + $intrvl_nli;
                        $b2 = ($b1 + $intrvl_nli) - 1;
                        $a = $b1 + $intrvl_nli;
                        $kkm_kelas = $input_kkm;
                        $predikat = $newNakhirk;
                        if ($predikat >= $a) {
                            $grade = "A";
                        } elseif ($predikat >= $b1) {
                            $grade = "B";
                        } elseif ($predikat >= $kkm_kelas) {
                            $grade = "C";
                        } else {
                            $grade = "D";
                        }
                        $predikat = $grade;
                        if ($predikat == 'A') {
                            $awaldesk = 'Ananda sangat terampil dalam ';
                        } elseif ($predikat == 'B') {
                            $awaldesk = 'Ananda terampil dalam ';
                        } elseif ($predikat == 'C') {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        } else {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        }
                        $this->db->select_max('n_k', 'max');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query2 = $this->db->get('t_nilaik')->row_array();
                        $deskk =  $query2['max'];
                        $query3 = $this->db->query("SELECT deskk_n FROM t_nilaik WHERE n_k = ' $deskk' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query3)) {
                            $newdeskket = 'belum ada nilai keterampilan dan deskripsi !';
                        } else {
                            $newdeskket = $query3['deskk_n'];
                        }
                        $newDeskripsiketerampailan = $awaldesk . $newdeskket;
                        $dt[$nrs]['deskripsi_k'] = $newDeskripsiketerampailan;
                        $dt[$nrs]['n_akhirk'] = round($newNakhirk);
                        $dt[$nrs]['predikat_k'] = $grade;
                    }
                    $this->m_guru->updateDataNilaiAkhirk($dt, 'id_akhirk');
                    $this->session->set_flashdata('messageSuccess', 'Data nilai keterampilan berhasil diupdate !');
                    redirect('guru/nilai_k');
                } else {
                    $dt = array();
                    foreach ($newKdstudents as $nrs => $rs) {
                        $dt[$nrs]['kd_students'] = $rs;
                        $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                        $dt[$nrs]['aktif_k'] = 1;
                        $dt[$nrs]['kd_tapel'] = $newTapel;
                        $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
                    }
                    foreach ($newKdstudents as $nrs => $rsa) {
                        $this->db->select_avg('n_k', 'rata');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query = $this->db->get('t_nilaik')->row_array();
                        if (is_null($query)) {
                            $newNakhirk = 0;
                        } else {
                            $newNakhirk =  round($query['rata'], 2);
                        }
                        $query1 = $this->db->query("SELECT n_kkmkls FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query1)) {
                            $kkmkelas = 0;
                        } else {
                            $kkmkelas = round($query1['n_kkmkls']);
                        }
                        $input_kkm = $kkmkelas;
                        $intrvl_nli = round((100 - $input_kkm) / 3);
                        $c = ($input_kkm + $intrvl_nli) - 1;
                        $b1 = $input_kkm + $intrvl_nli;
                        $b2 = ($b1 + $intrvl_nli) - 1;
                        $a = $b1 + $intrvl_nli;
                        $kkm_kelas = $input_kkm;
                        $predikat = $newNakhirk;
                        if ($predikat >= $a) {
                            $grade = "A";
                        } elseif ($predikat >= $b1) {
                            $grade = "B";
                        } elseif ($predikat >= $kkm_kelas) {
                            $grade = "C";
                        } else {
                            $grade = "D";
                        }
                        $predikat = $grade;
                        if ($predikat == 'A') {
                            $awaldesk = 'Ananda sangat terampil dalam ';
                        } elseif ($predikat == 'B') {
                            $awaldesk = 'Ananda terampil dalam ';
                        } elseif ($predikat == 'C') {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        } else {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        }
                        $this->db->select_max('n_k', 'max');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query2 = $this->db->get('t_nilaik')->row_array();
                        $deskk =  $query2['max'];
                        $query3 = $this->db->query("SELECT deskk_n FROM t_nilaik WHERE n_k = ' $deskk' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query3)) {
                            $newdeskket = 'belum ada nilai keterampilan dan deskripsi !';
                        } else {
                            $newdeskket = $query3['deskk_n'];
                        }
                        $newDeskripsiketerampailan = $awaldesk . $newdeskket;
                        $dt[$nrs]['deskripsi_k'] = $newDeskripsiketerampailan;
                        $dt[$nrs]['n_akhirk'] = round($newNakhirk);
                        $dt[$nrs]['predikat_k'] = $grade;
                    }
                    $this->m_guru->inputDataNilaiAkhirk($dt);
                    $this->session->set_flashdata('messageSuccess', 'Data nilai keterampilan berhasil diupdate !');
                    redirect('guru/nilai_k');
                }
            } else {
                $data = array();
                foreach ($newKdstudents as $nrs => $rs) {
                    $data[$nrs]['kd_students'] = $rs;
                    $data[$nrs]['kd_gmapel'] =  $newKdgmapel;
                    $data[$nrs]['kd_kdk'] = $newKdk;
                    $data[$nrs]['n_k'] = $newNilaik;
                    $data[$nrs]['deskk_n'] = $newDeskk;
                    $data[$nrs]['tipe_n'] = 'k';
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                foreach ($newNilaik as $nrs => $rs) {
                    $data[$nrs]['n_k'] = $rs;
                }
                $this->m_guru->inputDataNilaik($data);
                $data = array();
                foreach ($newKdstudents as $nrs => $rstd) {
                    $data[$nrs]['kd_students'] = $rstd;
                    $cekIdakhir = $this->db->get_where('t_akhirk', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                }
                if ($cekIdakhir > 0) {
                    $dt = array();
                    foreach ($newKdstudents as $nrs => $rs) {
                        $idAkhirk = $this->db->get_where('t_akhirk', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->row_array();
                        $dt[$nrs]['id_akhirk'] = $idAkhirk['id_akhirk'];
                        $dt[$nrs]['kd_students'] = $rs;
                        $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                        $dt[$nrs]['aktif_k'] = 1;
                        $dt[$nrs]['kd_tapel'] = $newTapel;
                        $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
                    }
                    foreach ($newKdstudents as $nrs => $rsa) {
                        $this->db->select_avg('n_k', 'rata');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query = $this->db->get('t_nilaik')->row_array();
                        if (is_null($query)) {
                            $newNakhirk = 0;
                        } else {
                            $newNakhirk =  round($query['rata'], 2);
                        }
                        $query1 = $this->db->query("SELECT n_kkmkls FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query1)) {
                            $kkmkelas = 0;
                        } else {
                            $kkmkelas = round($query1['n_kkmkls']);
                        }
                        $input_kkm = $kkmkelas;
                        $intrvl_nli = round((100 - $input_kkm) / 3);
                        $c = ($input_kkm + $intrvl_nli) - 1;
                        $b1 = $input_kkm + $intrvl_nli;
                        $b2 = ($b1 + $intrvl_nli) - 1;
                        $a = $b1 + $intrvl_nli;
                        $kkm_kelas = $input_kkm;
                        $predikat = $newNakhirk;
                        if ($predikat >= $a) {
                            $grade = "A";
                        } elseif ($predikat >= $b1) {
                            $grade = "B";
                        } elseif ($predikat >= $kkm_kelas) {
                            $grade = "C";
                        } else {
                            $grade = "D";
                        }
                        $predikat = $grade;
                        if ($predikat == 'A') {
                            $awaldesk = 'Ananda sangat terampil dalam ';
                        } elseif ($predikat == 'B') {
                            $awaldesk = 'Ananda terampil dalam ';
                        } elseif ($predikat == 'C') {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        } else {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        }
                        $this->db->select_max('n_k', 'max');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query2 = $this->db->get('t_nilaik')->row_array();
                        $deskk =  $query2['max'];
                        $query3 = $this->db->query("SELECT deskk_n FROM t_nilaik WHERE n_k = ' $deskk' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query3)) {
                            $newdeskket = 'belum ada nilai keterampilan dan deskripsi !';
                        } else {
                            $newdeskket = $query3['deskk_n'];
                        }
                        $newDeskripsiketerampailan = $awaldesk . $newdeskket;
                        $dt[$nrs]['deskripsi_k'] = $newDeskripsiketerampailan;
                        $dt[$nrs]['n_akhirk'] = round($newNakhirk);
                        $dt[$nrs]['predikat_k'] = $grade;
                    }
                    $this->m_guru->updateDataNilaiAkhirk($dt, 'id_akhirk');
                    $this->session->set_flashdata('messageSuccess', 'Data nilai keterampilan berhasil ditambahkan !');
                    redirect('guru/nilai_k');
                } else {
                    $dt = array();
                    foreach ($newKdstudents as $nrs => $rs) {
                        $dt[$nrs]['kd_students'] = $rs;
                        $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                        $dt[$nrs]['aktif_k'] = 1;
                        $dt[$nrs]['kd_tapel'] = $newTapel;
                        $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
                    }
                    foreach ($newKdstudents as $nrs => $rsa) {
                        $this->db->select_avg('n_k', 'rata');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query = $this->db->get('t_nilaik')->row_array();
                        if (is_null($query)) {
                            $newNakhirk = 0;
                        } else {
                            $newNakhirk =  round($query['rata'], 2);
                        }
                        $query1 = $this->db->query("SELECT n_kkmkls FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query1)) {
                            $kkmkelas = 0;
                        } else {
                            $kkmkelas = round($query1['n_kkmkls']);
                        }
                        $input_kkm = $kkmkelas;
                        $intrvl_nli = round((100 - $input_kkm) / 3);
                        $c = ($input_kkm + $intrvl_nli) - 1;
                        $b1 = $input_kkm + $intrvl_nli;
                        $b2 = ($b1 + $intrvl_nli) - 1;
                        $a = $b1 + $intrvl_nli;
                        $kkm_kelas = $input_kkm;
                        $predikat = $newNakhirk;
                        if ($predikat >= $a) {
                            $grade = "A";
                        } elseif ($predikat >= $b1) {
                            $grade = "B";
                        } elseif ($predikat >= $kkm_kelas) {
                            $grade = "C";
                        } else {
                            $grade = "D";
                        }
                        $predikat = $grade;
                        if ($predikat == 'A') {
                            $awaldesk = 'Ananda sangat terampil dalam ';
                        } elseif ($predikat == 'B') {
                            $awaldesk = 'Ananda terampil dalam ';
                        } elseif ($predikat == 'C') {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        } else {
                            $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                        }
                        $this->db->select_max('n_k', 'max');
                        $this->db->where('kd_students', $rsa);
                        $this->db->where('kd_gmapel',  $newKdgmapel);
                        $this->db->where('kd_tapel',  $kdTapel);
                        $this->db->where('kd_lembaga',  $newKdlembaga);
                        $query2 = $this->db->get('t_nilaik')->row_array();
                        $deskk =  $query2['max'];
                        $query3 = $this->db->query("SELECT deskk_n FROM t_nilaik WHERE n_k = ' $deskk' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                        if (is_null($query3)) {
                            $newdeskket = 'belum ada nilai keterampilan dan deskripsi !';
                        } else {
                            $newdeskket = $query3['deskk_n'];
                        }
                        $newDeskripsiketerampailan = $awaldesk . $newdeskket;
                        $dt[$nrs]['deskripsi_k'] = $newDeskripsiketerampailan;
                        $dt[$nrs]['n_akhirk'] = round($newNakhirk);
                        $dt[$nrs]['predikat_k'] = $grade;
                    }
                    $this->m_guru->inputDataNilaiAkhirk($dt);
                    $this->session->set_flashdata('messageSuccess', 'Data nilai keterampilan berhasil ditambahkan !');
                    redirect('guru/nilai_k');
                    // created by : KuduSinau_Hendrik_081275532220
                }
            }
        }
    }
    public function prosesNilaiAkhirk()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idGmapel['id_gmapel'] = $this->input->post("id_gmapel");
        $newIdGmapel = $idGmapel['id_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
            $kdTapel = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $newKdstudents = $this->input->post('kd_students');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $data = array();
        foreach ($newKdstudents as $nrs => $rstd) {
            $data[$nrs]['kd_students'] = $rstd;
            $cekIdakhir = $this->db->get_where('t_akhirk', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rstd, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
        }
        if ($cekIdakhir > 0) {
            $dt = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $idAkhirk = $this->db->get_where('t_akhirk', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->row_array();
                $dt[$nrs]['id_akhirk'] = $idAkhirk['id_akhirk'];
                $dt[$nrs]['kd_students'] = $rs;
                $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                $dt[$nrs]['aktif_k'] = 1;
                $dt[$nrs]['kd_tapel'] = $newTapel;
                $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
            }
            foreach ($newKdstudents as $nrs => $rsa) {
                $this->db->select_avg('n_k', 'rata');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $kdTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $query = $this->db->get('t_nilaik')->row_array();
                if (is_null($query)) {
                    $newNakhirk = 0;
                } else {
                    $newNakhirk =  round($query['rata'], 2);
                }
                $query1 = $this->db->query("SELECT n_kkmkls FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query1)) {
                    $kkmkelas = 0;
                } else {
                    $kkmkelas = round($query1['n_kkmkls']);
                }
                $input_kkm = $kkmkelas;
                $intrvl_nli = round((100 - $input_kkm) / 3);
                $c = ($input_kkm + $intrvl_nli) - 1;
                $b1 = $input_kkm + $intrvl_nli;
                $b2 = ($b1 + $intrvl_nli) - 1;
                $a = $b1 + $intrvl_nli;
                $kkm_kelas = $input_kkm;
                $predikat = $newNakhirk;
                if ($predikat >= $a) {
                    $grade = "A";
                } elseif ($predikat >= $b1) {
                    $grade = "B";
                } elseif ($predikat >= $kkm_kelas) {
                    $grade = "C";
                } else {
                    $grade = "D";
                }
                $predikat = $grade;
                if ($predikat == 'A') {
                    $awaldesk = 'Ananda sangat terampil dalam ';
                } elseif ($predikat == 'B') {
                    $awaldesk = 'Ananda terampil dalam ';
                } elseif ($predikat == 'C') {
                    $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                } else {
                    $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                }
                $this->db->select_max('n_k', 'max');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $kdTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $query2 = $this->db->get('t_nilaik')->row_array();
                $deskk =  $query2['max'];
                $query3 = $this->db->query("SELECT deskk_n FROM t_nilaik WHERE n_k = ' $deskk' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query3)) {
                    $newdeskket = 'belum ada nilai keterampilan dan deskripsi !';
                } else {
                    $newdeskket = $query3['deskk_n'];
                }
                $newDeskripsiketerampailan = $awaldesk . $newdeskket;
                $dt[$nrs]['deskripsi_k'] = $newDeskripsiketerampailan;
                $dt[$nrs]['n_akhirk'] = round($newNakhirk);
                $dt[$nrs]['predikat_k'] = $grade;
            }
            $this->m_guru->updateDataNilaiAkhirk($dt, 'id_akhirk');
            $this->session->set_flashdata('messageSuccess', 'Data nilai keterampilan berhasil diupdate !');
            redirect('guru/nilai_k');
        } else {
            $dt = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $dt[$nrs]['kd_students'] = $rs;
                $dt[$nrs]['kd_gmapel'] =  $newKdgmapel;
                $dt[$nrs]['aktif_k'] = 1;
                $dt[$nrs]['kd_tapel'] = $newTapel;
                $dt[$nrs]['kd_lembaga'] = $newKdlembaga;
            }
            foreach ($newKdstudents as $nrs => $rsa) {
                $this->db->select_avg('n_k', 'rata');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $kdTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $query = $this->db->get('t_nilaik')->row_array();
                if (is_null($query)) {
                    $newNakhirk = 0;
                } else {
                    $newNakhirk =  round($query['rata'], 2);
                }
                $query1 = $this->db->query("SELECT n_kkmkls FROM t_kkm WHERE kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query1)) {
                    $kkmkelas = 0;
                } else {
                    $kkmkelas = round($query1['n_kkmkls']);
                }
                $input_kkm = $kkmkelas;
                $intrvl_nli = round((100 - $input_kkm) / 3);
                $c = ($input_kkm + $intrvl_nli) - 1;
                $b1 = $input_kkm + $intrvl_nli;
                $b2 = ($b1 + $intrvl_nli) - 1;
                $a = $b1 + $intrvl_nli;
                $kkm_kelas = $input_kkm;
                $predikat = $newNakhirk;
                if ($predikat >= $a) {
                    $grade = "A";
                } elseif ($predikat >= $b1) {
                    $grade = "B";
                } elseif ($predikat >= $kkm_kelas) {
                    $grade = "C";
                } else {
                    $grade = "D";
                }
                $predikat = $grade;
                if ($predikat == 'A') {
                    $awaldesk = 'Ananda sangat terampil dalam ';
                } elseif ($predikat == 'B') {
                    $awaldesk = 'Ananda terampil dalam ';
                } elseif ($predikat == 'C') {
                    $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                } else {
                    $awaldesk = 'Ananda perlu meningkatkan keterampilan dalam ';
                }
                $this->db->select_max('n_k', 'max');
                $this->db->where('kd_students', $rsa);
                $this->db->where('kd_gmapel',  $newKdgmapel);
                $this->db->where('kd_tapel',  $kdTapel);
                $this->db->where('kd_lembaga',  $newKdlembaga);
                $query2 = $this->db->get('t_nilaik')->row_array();
                $deskk =  $query2['max'];
                $query3 = $this->db->query("SELECT deskk_n FROM t_nilaik WHERE n_k = ' $deskk' AND kd_students = '$rsa' AND kd_gmapel = '$newKdgmapel' AND kd_tapel = '$kdTapel' AND kd_lembaga = '$newKdlembaga'")->row_array();
                if (is_null($query3)) {
                    $newdeskket = 'belum ada nilai keterampilan dan deskripsi !';
                } else {
                    $newdeskket = $query3['deskk_n'];
                }
                $newDeskripsiketerampailan = $awaldesk . $newdeskket;
                $dt[$nrs]['deskripsi_k'] = $newDeskripsiketerampailan;
                $dt[$nrs]['n_akhirk'] = round($newNakhirk);
                $dt[$nrs]['predikat_k'] = $grade;
            }
            $this->m_guru->inputDataNilaiAkhirk($dt);
            $this->session->set_flashdata('messageSuccess', 'Data nilai keterampilan berhasil ditambahkan !');
            redirect('guru/nilai_k');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function lihat_p()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKdp'] = $this->db->get('t_kdp')->result_array();
        $data['kdp'] = $this->db->get('t_nokdp')->result_array();
        $data['nilaip'] = $this->db->get('t_nilaip')->result_array();
        $data['akhirp'] = $this->db->get('t_akhirp')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['dtNilaip'] = $this->m_guru->viewDataNilaip();
        $data['rSiswap'] = $this->m_guru->viewDataRsiswa();
        $data['nSiswap'] = $this->m_guru->nilaiSiswap();
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Nilai Pengetahuan';
        $cekRombelSiswa = $this->db->get_where('t_rombelsiswa', array('kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']))->num_rows();
        if ($cekRombelSiswa > 0) {
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/lihat_p', $data);
            $this->load->view('template/footer', $data);
        } else {
            $this->session->set_flashdata('messageInfo2', 'Anda belum mempunyai rombel siswa !');
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/lihat_p', $data);
            $this->load->view('template/footer', $data);
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function lihat_k()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKdk'] = $this->db->get('t_kdk')->result_array();
        $data['kdk'] = $this->db->get('t_nokdk')->result_array();
        $data['nilaik'] = $this->db->get('t_nilaik')->result_array();
        $data['akhirk'] = $this->db->get('t_akhirk')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['dtNilaik'] = $this->m_guru->viewDataNilaik();
        $data['rSiswak'] = $this->m_guru->viewDataRsiswa();
        $data['nSiswak'] = $this->m_guru->nilaiSiswak();
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Nilai Keterampilan';
        $cekRombelSiswa = $this->db->get_where('t_rombelsiswa', array('kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']))->num_rows();
        if ($cekRombelSiswa > 0) {
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/lihat_k', $data);
            $this->load->view('template/footer', $data);
        } else {
            $this->session->set_flashdata('messageInfo2', 'Anda belum mempunyai rombel siswa !');
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/lihat_k', $data);
            $this->load->view('template/footer', $data);
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function cetak_p($idm)
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKdp'] = $this->db->get('t_kdp')->result_array();
        $data['kdp'] = $this->db->get('t_nokdp')->result_array();
        $data['nilaip'] = $this->db->get('t_nilaip')->result_array();
        $data['akhirp'] = $this->db->get('t_akhirp')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['dtNilaip'] = $this->m_guru->viewDataNilaip();
        $data['rSiswap'] = $this->m_guru->viewDataRsiswa();
        $data['nSiswap'] = $this->m_guru->nilaiSiswap();
        $data['tgl'] = $this->m_guru->viewDataTgl();
        $data['id_gmapel'] = $idm;
        $this->db->select('t_kelas.nm_kelas as kelas');
        $this->db->from('t_gmapel');
        $this->db->join('t_kelas', ' t_gmapel.kd_kelas = t_kelas.id_kelas', 'right');
        $this->db->where('t_gmapel.id_gmapel', $data['id_gmapel']);
        $this->db->where('t_gmapel.kd_tapel', $data['kdTapel']);
        $this->db->where('t_gmapel.kd_lembaga', $newKdlembaga);
        $kelas = $this->db->get()->row_array();
        $newKelas = $kelas['kelas'];
        $this->db->select('t_tapel.kd_smstr as semester');
        $this->db->from('t_gmapel');
        $this->db->join('t_tapel', ' t_gmapel.kd_tapel = t_tapel.id_tapel', 'right');
        $this->db->where('t_gmapel.id_gmapel', $data['id_gmapel']);
        $this->db->where('t_gmapel.kd_tapel', $data['kdTapel']);
        $this->db->where('t_gmapel.kd_lembaga', $newKdlembaga);
        $smstr = $this->db->get()->row_array();
        $newSmstr = $smstr['semester'];
        $data['kelasNew'] = $newKelas;
        $data['smstrNew'] = $newSmstr;
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Print Nilai Pengetahuan';
        $this->load->view('template/header', $data);
        $this->load->view('guru/cetak_p', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function cetak_k($idm)
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKdk'] = $this->db->get('t_kdk')->result_array();
        $data['kdk'] = $this->db->get('t_nokdk')->result_array();
        $data['nilaik'] = $this->db->get('t_nilaik')->result_array();
        $data['akhirk'] = $this->db->get('t_akhirk')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['dtNilaik'] = $this->m_guru->viewDataNilaik();
        $data['rSiswak'] = $this->m_guru->viewDataRsiswa();
        $data['nSiswak'] = $this->m_guru->nilaiSiswak();
        $data['tgl'] = $this->m_guru->viewDataTgl();
        $data['id_gmapel'] = $idm;
        $this->db->select('t_kelas.nm_kelas as kelas');
        $this->db->from('t_gmapel');
        $this->db->join('t_kelas', ' t_gmapel.kd_kelas = t_kelas.id_kelas', 'right');
        $this->db->where('t_gmapel.id_gmapel', $data['id_gmapel']);
        $this->db->where('t_gmapel.kd_tapel', $data['kdTapel']);
        $this->db->where('t_gmapel.kd_lembaga', $newKdlembaga);
        $kelas = $this->db->get()->row_array();
        $newKelas = $kelas['kelas'];
        $this->db->select('t_tapel.kd_smstr as semester');
        $this->db->from('t_gmapel');
        $this->db->join('t_tapel', ' t_gmapel.kd_tapel = t_tapel.id_tapel', 'right');
        $this->db->where('t_gmapel.id_gmapel', $data['id_gmapel']);
        $this->db->where('t_gmapel.kd_tapel', $data['kdTapel']);
        $this->db->where('t_gmapel.kd_lembaga', $newKdlembaga);
        $smstr = $this->db->get()->row_array();
        $newSmstr = $smstr['semester'];
        $data['kelasNew'] = $newKelas;
        $data['smstrNew'] = $newSmstr;
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Print Nilai Keterampilan';
        $this->load->view('template/header', $data);
        $this->load->view('guru/cetak_k', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function ganti_password()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Change Password';
        $this->load->view('template/header', $data);
        $this->load->view('template/sidebar', $data);
        $this->load->view('template/headertop', $data);
        $this->load->view('template/mobile', $data);
        $this->load->view('guru/ganti_password', $data);
        $this->load->view('template/footer', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function changeGuruPassword()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdUser = $dataUser['kd_user'];
        $oldPassword = $dataUser['password'];
        $kdTeachers = $dataUser['kd_teachers'];
        $this->form_validation->set_rules('current_password', 'Password Lama', 'required|trim');
        $this->form_validation->set_rules('new_password2', 'Ulangi Password', 'required|trim');
        $this->form_validation->set_rules('new_password1', 'Password Baru', 'required|trim|min_length[6]');
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Minimal 6 Karakter !');
            redirect('guru/ganti_password');
        } else {
            $this->form_validation->set_rules('new_password2', 'Ulangi Password', 'required|trim|matches[new_password1]');
            if ($this->form_validation->run() == false) {
                $this->session->set_flashdata('messageWarning', 'Password tidak sama !');
                redirect('guru/ganti_password');
            } else {
                $currentPassword = $this->input->post('current_password');
                $newPassword = $this->input->post('new_password1');
                if (!password_verify($currentPassword, $oldPassword)) {
                    $this->session->set_flashdata('messageError', 'Password salah !');
                    redirect('guru/ganti_password');
                } else {
                    if ($currentPassword == $newPassword) {
                        $this->session->set_flashdata('messageError', 'Password baru tidak boleh sama dengan password lama !');
                        redirect('guru/ganti_password');
                    } else {
                        $passwordHash = password_hash($newPassword, PASSWORD_DEFAULT);
                        $this->db->set('password', $passwordHash);
                        $this->db->where('username', $this->session->userdata('username'));
                        $this->db->where('kd_teachers', $kdTeachers);
                        $this->db->where('kd_user', $newKdUser);
                        $this->db->update('t_user');
                        $this->session->set_flashdata('messageSuccess', 'Password berhasil dirubah !');
                        redirect('guru/ganti_password');
                        // created by : KuduSinau_Hendrik_081275532220
                    }
                }
            }
        }
    }
    public function block()
    {
        $data['title'] = 'D-Rapor | Blocked';
        $this->load->view('guru/block', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function updateDataNilaiKkm()
    {
        $idGmapel['id_kkm'] = $this->input->post("id_kkm");
        $newIdGmapel = $idGmapel['id_kkm'];
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('n_kkmkls', 'Nilai KKM Kelas', 'required|numeric|trim');
        $this->form_validation->set_rules('b_hph', 'Bobot HPH', 'required|numeric|trim');
        $this->form_validation->set_rules('b_hpts', 'Bobot HPTS', 'required|numeric|trim');
        $this->form_validation->set_rules('b_hpas', 'Bobot HPAS', 'required|numeric|trim');
        $this->form_validation->set_rules('jp_mapel', 'Jam Pelajaran', 'required|numeric|trim');
        $nKkm = htmlspecialchars($this->input->post('n_kkmkls'), true);
        $nHph = htmlspecialchars($this->input->post('b_hph'), true);
        $nHpts = htmlspecialchars($this->input->post('b_hpts'), true);
        $nHpas = htmlspecialchars($this->input->post('b_hpas'), true);
        $nJp = htmlspecialchars($this->input->post('jp_mapel'), true);
        $totalHp = $nHph + $nHpts + $nHpas;
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/kkm_kelas');
        } else {
            if (!($totalHp == 100)) {
                $this->session->set_flashdata('messageError', 'Total bobot penilaian tidak boleh lebih atau kurang dari 100 persen !');
                redirect('guru/kkm_kelas');
            } else {
                $input_kkm = $nKkm;
                $intrvl_nli = round((100 - $input_kkm) / 3);
                $c = ($input_kkm + $intrvl_nli) - 1;
                $b1 = $input_kkm + $intrvl_nli;
                $b2 = ($b1 + $intrvl_nli) - 1;
                $a = $b1 + $intrvl_nli;
                $cekKkm = $this->db->get_where('t_kkm', array('kd_gmapel' => $newIdGmapel, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
                if ($cekKkm > 0) {
                    $data = [
                        'kd_gmapel'     => $newIdGmapel,
                        'n_kkmkls'      => round($nKkm),
                        'd'             => htmlspecialchars('< ' . round($nKkm), true),
                        'c'             => htmlspecialchars(round($nKkm) . ' - ' . round($c), true),
                        'b'             => htmlspecialchars(round($b1) . ' - ' . round($b2), true),
                        'a'             => htmlspecialchars(round($a) . ' - ' . '100', true),
                        'b_hph'         => round($nHph),
                        'b_hpts'        => round($nHpts),
                        'b_hpas'        => round($nHpas),
                        'jp_mapel'      => round($nJp),
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga
                    ];
                    $this->db->where('kd_gmapel', $newIdGmapel);
                    $this->db->where('kd_tapel', $newTapel);
                    $this->db->where('kd_lembaga', $newKdlembaga);
                    $this->db->update('t_kkm', $data);
                    $this->session->set_flashdata('messageSave', 'Nilai KKM Kelas berhasil diupdate !');
                    redirect('guru/kkm_kelas');
                } else {
                    $input_kkm = $nKkm;
                    $intrvl_nli = round((100 - $input_kkm) / 3);
                    $c = ($input_kkm + $intrvl_nli) - 1;
                    $b1 = $input_kkm + $intrvl_nli;
                    $b2 = ($b1 + $intrvl_nli) - 1;
                    $a = $b1 + $intrvl_nli;
                    $data = [
                        'kd_gmapel' => $newIdGmapel,
                        'n_kkmkls' => round($nKkm),
                        'd'             => htmlspecialchars('< ' . round($nKkm), true),
                        'c'             => htmlspecialchars(round($nKkm) . ' - ' . round($c), true),
                        'b'             => htmlspecialchars(round($b1) . ' - ' . round($b2), true),
                        'a'             => htmlspecialchars(round($a) . ' - ' . '100', true),
                        'b_hph'         => round($nHph),
                        'b_hpts'        => round($nHpts),
                        'b_hpas'        => round($nHpas),
                        'jp_mapel'      => round($nJp),
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga
                    ];
                    $this->m_guru->inputDataKkmKelas($data);
                    $this->session->set_flashdata('messageSave', 'Nilai KKM Kelas berhasil ditambahkan !');
                    redirect('guru/kkm_kelas');
                    // created by : KuduSinau_Hendrik_081275532220
                }
            }
        }
    }
    public function nilai_p()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $newKdGuru = $data['nmUser']['kd_teachers'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel;
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $this->db->select('*');
        $this->db->from('t_nmkdp');
        $data['kdp'] = $this->db->get()->result_array();
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKdp'] = $this->db->order_by('no_kdp', SORT_ASC)->get('t_kdp')->result_array();
        $data['kdp'] = $this->db->get('t_nokdp')->result_array();
        $data['nilaip'] = $this->db->get('t_nilaip')->result_array();
        $data['nmKdp'] = $this->db->get('t_nmkdp')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['dtNilaip'] = $this->m_guru->viewDataNilaip();
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | Nilai Pengetahuan';
        $cekRombelSiswa = $this->db->get_where('t_rombelsiswa', array('kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']['id_tapel']))->num_rows();
        $kdGmapel = $this->db->get_where('t_gmapel', array('kd_teachers' => $newKdGuru, 'kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']['id_tapel']))->row_array();
        $cekKkm = $this->db->get_where('t_kkm', array('kd_gmapel' => $kdGmapel['id_gmapel'], 'kd_lembaga' => $newKdlembaga, 'kd_tapel' => $data['kdTapel']['id_tapel']))->num_rows();
        $this->load->view('template/header', $data);
        $this->load->view('template/sidebar', $data);
        $this->load->view('template/headertop', $data);
        $this->load->view('template/mobile', $data);
        if ($cekRombelSiswa > 0) {
            if ($cekKkm > 0) {
                $this->load->view('guru/nilai_p', $data);
            } else {
                $this->session->set_flashdata('messageWarning', 'KKM anda belum di seting !');
                $this->load->view('guru/nilai_p', $data);
            }
        } else {
            $this->session->set_flashdata('messageInfo2', 'Anda belum mempunyai rombel siswa !');
            $this->load->view('guru/nilai_p', $data);
        }
        $this->load->view('template/footer', $data);
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function tambahDataKdp()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idGmapel['id_gmapel'] = $this->input->post("id_gmapel");
        $newIdGmapel = $idGmapel['id_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('no_kdp', 'Nomor KD', 'required|trim');
        $this->form_validation->set_rules('nm_kdp', 'Kompetensi Dasar', 'required|trim');
        $this->form_validation->set_rules('nm_deskp', 'Deskripsi KD', 'required|trim');
        $noKdp = htmlspecialchars($this->input->post('no_kdp'), true);
        $nmKdp = htmlspecialchars($this->input->post('nm_kdp'), true);
        $deskKdp = htmlspecialchars($this->input->post('nm_deskp'), true);
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $cekKdp = $this->db->get_where('t_kdp', array('no_kdp' => $noKdp, 'kd_gmapel' => $newIdGmapel, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            if ($cekKdp > 0) {
                $this->session->set_flashdata('messageError', 'Nomor KD ' . $noKdp . ' sudah terdaftar !');
                redirect('guru/nilai_p');
            } else {
                $data = [
                    'kd_gmapel' => $newIdGmapel,
                    'no_kdp' => $noKdp,
                    'nm_kdp' => $nmKdp,
                    'nm_deskp' => $deskKdp,
                    'kd_tapel' => $newTapel,
                    'kd_lembaga' => $newKdlembaga
                ];
                $this->m_guru->inputDataKdp($data);
                $this->session->set_flashdata('messageSave', 'KD ' . $noKdp . ' berhasil ditambahkan !');
                redirect('guru/nilai_p');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function updateDataKdp()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idKdp['id_kdp'] = $this->input->post("id_kdp");
        $IdKdp = $idKdp['id_kdp'];
        $dataKdp = $this->db->get_where('t_kdp', ['id_kdp' => $IdKdp])->row_array();
        $newIdGmapel = $dataKdp['kd_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('no_kdp', 'Nomor KD', 'required|trim');
        $this->form_validation->set_rules('nm_kdp', 'Kompetensi Dasar', 'required|trim');
        $this->form_validation->set_rules('nm_deskp', 'Deskripsi KD', 'required|trim');
        $noKdp = htmlspecialchars($this->input->post('no_kdp'), true);
        $nmKdp = htmlspecialchars($this->input->post('nm_kdp'), true);
        $deskKdp = htmlspecialchars($this->input->post('nm_deskp'), true);
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $data = [
                'kd_gmapel' => $newIdGmapel,
                'no_kdp' => $noKdp,
                'nm_kdp' => $nmKdp,
                'nm_deskp' => $deskKdp,
                'kd_tapel' => $newTapel,
                'kd_lembaga' => $newKdlembaga
            ];
            $this->db->where('id_kdp', $IdKdp);
            $this->db->where('kd_gmapel', $newIdGmapel);
            $this->db->where('kd_tapel', $newTapel);
            $this->db->where('kd_lembaga', $newKdlembaga);
            $this->db->update('t_kdp', $data);
            $this->db->set('deskp_n', $deskKdp);
            $this->db->where('kd_kdp', $IdKdp);
            $this->db->where('kd_gmapel', $newIdGmapel);
            $this->db->where('kd_tapel', $newTapel);
            $this->db->where('kd_lembaga', $newKdlembaga);
            $this->db->update('t_nilaip');
            $this->session->set_flashdata('messageSave', 'KD ' . $noKdp . ' berhasil diupdate !');
            redirect('guru/nilai_p');
        }
    }
    public function hapusDataKdp($idKdp)
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $query = $this->db->query("SELECT no_kdp FROM t_kdp WHERE id_kdp = '$idKdp' AND kd_lembaga = '$newKdlembaga' AND kd_tapel = '$newTapel'")->row_array();
        $noKd = $query['no_kdp'];
        $query = $this->db->query("SELECT kd_gmapel FROM t_kdp WHERE id_kdp = '$idKdp' AND kd_lembaga = '$newKdlembaga' AND kd_tapel = '$newTapel'")->row_array();
        $kdGmapel = $query['kd_gmapel'];
        $where = array('id_kdp' => $idKdp);
        $this->m_guru->deleteDataKdp($where, 't_kdp');
        $this->db->where('kd_kdp', $idKdp);
        $this->db->where('kd_gmapel', $kdGmapel);
        $this->db->where('kd_tapel', $newTapel);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->delete('t_nilaip');
        $this->session->set_flashdata('messageSuccess', 'KD ' . $noKd . ' berhasil dihapus !');
        redirect('guru/nilai_p');
    }
    public function inputNilaip()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('kd_students[]', 'Kode Siswa', 'required|trim');
        $this->form_validation->set_rules('kd_gmapel', 'Kode Guru Mapel', 'required|trim');
        $this->form_validation->set_rules('kd_kdp', 'Kode KD', 'required|trim');
        $this->form_validation->set_rules('n_p[]', 'Nilai Pengetahuan', 'required|numeric|trim');
        $this->form_validation->set_rules('deskp_n', 'Deskripsi KD', 'required|trim');
        $newKdstudents = $this->input->post('kd_students');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $newKdp = htmlspecialchars($this->input->post('kd_kdp'));
        $newNilaip = $this->input->post('n_p');
        $newDeskp = htmlspecialchars($this->input->post('deskp_n'));
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $data = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $data[$nrs]['kd_students'] = $rs;
            }
            $cekKdp = $this->db->get_where('t_nilaip', array('kd_gmapel' => $newKdgmapel, 'kd_kdp' => $newKdp, 'kd_students' => $rs, 'kd_tapel' =>  $newTapel, 'kd_lembaga' => $newKdlembaga, 'tipe_n' => 'p'))->num_rows();
            if ($cekKdp > 0) {
                $data =  $this->input->post();
                for ($i = 0; $i < count($data['id_nilaip']); $i++) {
                    $update[] = array(
                        'id_nilaip'     => $data['id_nilaip'][$i],
                        'kd_students'   => $data['kd_students'][$i],
                        'kd_gmapel'     => $newKdgmapel,
                        'kd_kdp'        => $newKdp,
                        'n_p'           => $data['n_p'][$i],
                        'deskp_n'           => $newDeskp,
                        'tipe_n'        => 'p',
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga,
                    );
                }
                $this->m_guru->updateDataNilaip($update, 'id_nilaip');
                $this->session->set_flashdata('messageSave', 'Data nilai pengetahuan berhasil diupdate !');
                redirect('guru/nilai_p');
            } else {
                $data = array();
                foreach ($newKdstudents as $nrs => $rs) {
                    $data[$nrs]['kd_students'] = $rs;
                    $data[$nrs]['kd_gmapel'] =  $newKdgmapel;
                    $data[$nrs]['kd_kdp'] = $newKdp;
                    $data[$nrs]['n_p'] = $newNilaip;
                    $data[$nrs]['deskp_n'] = $newDeskp;
                    $data[$nrs]['tipe_n'] = 'p';
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                foreach ($newNilaip as $nrs => $rs) {
                    $data[$nrs]['n_p'] = $rs;
                }
                $this->m_guru->inputDataNilaip($data);
                $this->session->set_flashdata('messageSave', 'Data nilai pengetahuan berhasil ditambahkan !');
                redirect('guru/nilai_p');
            }
        }
    }
    public function tambahDataKdk()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idGmapel['id_gmapel'] = $this->input->post("id_gmapel");
        $newIdGmapel = $idGmapel['id_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
            $kdTapel = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('no_kdk', 'Nomor KD', 'required|trim');
        $this->form_validation->set_rules('nm_kdk', 'Kompetensi Dasar', 'required|trim');
        $this->form_validation->set_rules('nm_deskk', 'Deskripsi KD', 'required|trim');
        $noKdk = htmlspecialchars($this->input->post('no_kdk'), true);
        $nmKdk = htmlspecialchars($this->input->post('nm_kdk'), true);
        $deskKdk = htmlspecialchars($this->input->post('nm_deskk'), true);
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_k');
        } else {
            $cekKdk = $this->db->get_where('t_kdk', array('kd_gmapel' => $newIdGmapel, 'no_kdk' => $noKdk, 'kd_tapel' => $kdTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            if ($cekKdk > 0) {
                $this->session->set_flashdata('messageError', 'Nomor KD ' . $noKdk . ' sudah terdaftar !');
                redirect('guru/nilai_k');
            } else {
                $data = [
                    'kd_gmapel' => $newIdGmapel,
                    'no_kdk' => $noKdk,
                    'nm_kdk' => $nmKdk,
                    'nm_deskk' => $deskKdk,
                    'kd_tapel' => $newTapel,
                    'kd_lembaga' => $newKdlembaga
                ];
                $this->m_guru->inputDataKdk($data);
                $this->session->set_flashdata('messageSave', 'KD ' . $noKdk . ' berhasil ditambahkan !');
                redirect('guru/nilai_k');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function tambahAllDataKdk()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idGmapel['id_gmapel'] = $this->input->post("id_gmapel");
        $newIdGmapel = $idGmapel['id_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $jenjang = $dataUser['kd_jenjang'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('no_kdk[]', 'Nomor KD', 'required|trim');
        $noKdk = $this->input->post('no_kdk');
        $nmMapel = $this->input->post('nm_mapel');
        $nmKelas = $this->input->post('nm_kelas');
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_k');
        } else {
            $data = array();
            foreach ($noKdk as $nrs => $rs) {
                $data[$nrs]['no_kdk'] = $rs;
                $cekKdk = $this->db->get_where('t_kdk', array('no_kdk' => $rs, 'kd_gmapel' => $newIdGmapel, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            }
            if ($cekKdk > 0) {
                $this->session->set_flashdata('messageError', 'Nomor KD ada yang sudah terdaftar !');
                redirect('guru/nilai_k');
            } else {
                $data = array();
                foreach ($noKdk as $nrs => $rs) {
                    $data[$nrs]['no_kdk'] = $rs;
                    $jlh[] = $rs;
                    $this->db->select('*');
                    $this->db->where('kd_kdk', $rs);
                    $this->db->where('nm_jenjang', $jenjang);
                    $this->db->where('nm_kelas',  $nmKelas);
                    $this->db->where('nm_mapel', $nmMapel);
                    $this->db->from('t_nmkdk');
                    $query = $this->db->get()->row_array();
                    $nmKD   = $query['nm_kdk'];
                    $nmDesk = $query['nm_deskripsi'];
                    $data[$nrs]['nm_kdk'] = $nmKD;
                    $data[$nrs]['nm_deskk'] = $nmDesk;
                    $data[$nrs]['kd_gmapel'] = $newIdGmapel;
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                $jumlah = count($jlh);
                $this->m_guru->inputAllDataKdk($data);
                $this->session->set_flashdata('messageSave', $jumlah . ' KD Keterampilan berhasil ditambahkan !');
                redirect('guru/nilai_k');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function updateDataKdk()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idKdk['id_kdk'] = $this->input->post("id_kdk");
        $IdKdk = $idKdk['id_kdk'];
        $dataKdk = $this->db->get_where('t_kdk', ['id_kdk' => $IdKdk])->row_array();
        $newIdGmapel = $dataKdk['kd_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
            $kdTapel = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('no_kdk', 'Nomor KD', 'required|trim');
        $this->form_validation->set_rules('nm_kdk', 'Kompetensi Dasar', 'required|trim');
        $this->form_validation->set_rules('nm_deskk', 'Deskripsi KD', 'required|trim');
        $noKdk = htmlspecialchars($this->input->post('no_kdk'), true);
        $nmKdk = htmlspecialchars($this->input->post('nm_kdk'), true);
        $deskKdk = htmlspecialchars($this->input->post('nm_deskk'), true);
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_k');
        } else {
            $data = [
                'kd_gmapel' => $newIdGmapel,
                'no_kdk' => $noKdk,
                'nm_kdk' => $nmKdk,
                'nm_deskk' => $deskKdk,
                'kd_tapel' => $newTapel,
                'kd_lembaga' => $newKdlembaga
            ];
            $this->db->where('id_kdk', $IdKdk);
            $this->db->where('kd_gmapel', $newIdGmapel);
            $this->db->where('kd_tapel',  $kdTapel);
            $this->db->where('kd_lembaga',  $newKdlembaga);
            $this->db->update('t_kdk', $data);
            $this->db->set('deskk_n', $deskKdk);
            $this->db->where('kd_kdk',  $IdKdk);
            $this->db->where('kd_gmapel', $newIdGmapel);
            $this->db->where('kd_tapel', $kdTapel);
            $this->db->where('kd_lembaga', $newKdlembaga);
            $this->db->update('t_nilaik');
            $this->session->set_flashdata('messageSave', 'KD ' . $noKdk . ' berhasil diupdate !');
            redirect('guru/nilai_k');
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function hapusDataKdk($idKdk)
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
            $kdTapel = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $query = $this->db->query("SELECT no_kdk FROM t_kdk WHERE id_kdk = '$idKdk' AND kd_lembaga = '$newKdlembaga' AND kd_tapel = '$kdTapel'")->row_array();
        $noKd = $query['no_kdk'];
        $query = $this->db->query("SELECT kd_gmapel FROM t_kdk WHERE id_kdk = '$idKdk' AND kd_lembaga = '$newKdlembaga' AND kd_tapel = '$kdTapel'")->row_array();
        $kdGmapel = $query['kd_gmapel'];
        $where = array('id_kdk' => $idKdk);
        $this->m_guru->deleteDataKdk($where, 't_kdk');
        $this->db->where('kd_kdk', $idKdk);
        $this->db->where('kd_gmapel', $kdGmapel);
        $this->db->where('kd_tapel',  $kdTapel);
        $this->db->where('kd_lembaga',  $newKdlembaga);
        $this->db->delete('t_nilaik');
        $this->session->set_flashdata('messageSuccess', 'KD ' . $noKd . ' berhasil dihapus !');
        redirect('guru/nilai_k');
        // created by : KuduSinau_Hendrik_081275532220
    }
    public function kkm_kelas()
    {
        $data['nmUser'] = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $data['nmJenjang'] = $this->db->get_where('t_jenjang', ['id_jenjang' => $this->session->userdata('kd_jenjang')])->row_array();
        $data['nmLevel'] = $this->db->get_where('t_level', ['id_level' => $this->session->userdata('kd_level')])->row_array();
        $data['nmStatus'] = $this->db->get_where('t_status', ['id_status' => $this->session->userdata('status_lembaga')])->row_array();
        $data['nmLembaga'] = $this->db->get_where('t_lembaga', ['id_lembaga' => $this->session->userdata('kd_user')])->row_array();
        $data['nmTapel'] = $this->db->get_where('t_tapel', ['kd_lembaga' => $this->session->userdata('kd_user')])->result_array();
        $newKdlembaga = $data['nmUser']['kd_user'];
        $newKdGuru = $data['nmUser']['kd_teachers'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        if (!is_null($dataTapel)) {
            $data['kdTapel'] = $dataTapel['id_tapel'];
        } else {
            $data['kdTapel'] = 'null';
        }
        $this->db->select('*');
        $this->db->from('t_lembaga');
        $this->db->join('t_jenjang', 't_lembaga.kd_jenjang = t_jenjang.id_jenjang');
        $this->db->join('t_status', 't_lembaga.kd_status = t_status.id_status');
        $this->db->where('t_lembaga.id_lembaga', $newKdlembaga);
        $data['Lembaga'] = $this->db->get()->row_array();
        $data['gMapel'] = $this->db->get('t_gmapel')->result_array();
        $data['kkm'] = $this->db->get('t_kkm')->result_array();
        $data['dtKkm'] = $this->m_guru->viewDataKkm();
        $data['nama'] = 'Digital Rapor';
        $data['footer'] = 'KuduSinau';
        $data['title'] = 'D-Rapor | KKM';
        $cekGuruMapel = $this->db->get_where('t_gmapel', array('kd_lembaga' => $newKdlembaga, 'kd_teachers' => $newKdGuru, 'kd_tapel' => $data['kdTapel']))->num_rows();
        if ($cekGuruMapel > 0) {
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/kkm_kelas', $data);
            $this->load->view('template/footer', $data);
        } else {
            $this->session->set_flashdata('messageInfo2', 'Anda belum mempunyai mapel yang diampu !');
            $this->load->view('template/header', $data);
            $this->load->view('template/sidebar', $data);
            $this->load->view('template/headertop', $data);
            $this->load->view('template/mobile', $data);
            $this->load->view('guru/kkm_kelas', $data);
            $this->load->view('template/footer', $data);
            // created by : KuduSinau_Hendrik_081275532220
        }
    }
    public function tambahAllDataKdp()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $idGmapel['id_gmapel'] = $this->input->post("id_gmapel");
        $newIdGmapel = $idGmapel['id_gmapel'];
        $newKdlembaga = $dataUser['kd_user'];
        $jenjang = $dataUser['kd_jenjang'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('no_kdp[]', 'Nomor KD', 'required|trim');
        $noKdp = $this->input->post('no_kdp');
        $nmMapel = $this->input->post('nm_mapel');
        $nmKelas = $this->input->post('nm_kelas');
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $data = array();
            foreach ($noKdp as $nrs => $rs) {
                $data[$nrs]['no_kdp'] = $rs;
                $cekKdp = $this->db->get_where('t_kdp', array('no_kdp' => $rs, 'kd_gmapel' => $newIdGmapel, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga))->num_rows();
            }
            if ($cekKdp > 0) {
                $this->session->set_flashdata('messageError', 'Nomor KD ada yang sudah terdaftar !');
                redirect('guru/nilai_p');
            } else {
                $data = array();
                foreach ($noKdp as $nrs => $rs) {
                    $data[$nrs]['no_kdp'] = $rs;
                    $jlh[] = $rs;
                    $this->db->select('*');
                    $this->db->where('kd_kdp', $rs);
                    $this->db->where('nm_jenjang', $jenjang);
                    $this->db->where('nm_kelas',  $nmKelas);
                    $this->db->where('nm_mapel', $nmMapel);
                    $this->db->from('t_nmkdp');
                    $query = $this->db->get()->row_array();
                    $nmKD   = $query['nm_kdp'];
                    $nmDesk = $query['nm_deskripsi'];
                    $data[$nrs]['nm_kdp'] = $nmKD;
                    $data[$nrs]['nm_deskp'] = $nmDesk;
                    $data[$nrs]['kd_gmapel'] = $newIdGmapel;
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                $jumlah = count($jlh);
                $this->m_guru->inputAllDataKdp($data);
                $this->session->set_flashdata('messageSave', $jumlah . ' KD Pengetahuan berhasil ditambahkan !');
                redirect('guru/nilai_p');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function inputNilaipas()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $smstr = $dataTapel['kd_smstr'];
        $this->form_validation->set_rules('kd_students[]', 'Kode Siswa', 'required|trim');
        $this->form_validation->set_rules('kd_gmapel', 'Kode Guru Mapel', 'required|trim');
        $this->form_validation->set_rules('n_p[]', 'Nilai Pengetahuan', 'required|numeric|trim');
        $newKdstudents = $this->input->post('kd_students');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $newNilaip = $this->input->post('n_p');
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $data = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $data[$nrs]['kd_students'] = $rs;
            }
            $cekKdp = $this->db->get_where('t_nilaip', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga, 'tipe_n' => 'r'))->num_rows();
            if ($cekKdp > 0) {
                $data =  $this->input->post();
                for ($i = 0; $i < count($data['id_nilaip']); $i++) {
                    $update[] = array(
                        'id_nilaip'     => $data['id_nilaip'][$i],
                        'kd_students'   => $data['kd_students'][$i],
                        'kd_gmapel'     => $newKdgmapel,
                        'n_p'           => $data['n_p'][$i],
                        'tipe_n'        => 'r',
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga,
                    );
                }
                $this->m_guru->updateDataNilaip($update, 'id_nilaip');
                if ($smstr == 'Ganjil') {
                    $this->session->set_flashdata('messageSave', 'Penilain akhir semester berhasil diupdate !');
                } else {
                    $this->session->set_flashdata('messageSave', 'Penilain akhir tahun berhasil diupdate !');
                }
                redirect('guru/nilai_p');
            } else {
                $data = array();
                foreach ($newKdstudents as $nrs => $rs) {
                    $data[$nrs]['kd_students'] = $rs;
                    $data[$nrs]['kd_gmapel'] =  $newKdgmapel;
                    $data[$nrs]['n_p'] = $newNilaip;
                    $data[$nrs]['tipe_n'] = 'r';
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                foreach ($newNilaip as $nrs => $rs) {
                    $data[$nrs]['n_p'] = $rs;
                }
                $this->m_guru->inputDataNilaip($data);
                if ($smstr == 'Ganjil') {
                    $this->session->set_flashdata('messageSave', 'Penilain akhir semester berhasil ditambahkan !');
                } else {
                    $this->session->set_flashdata('messageSave', 'Penilain akhir tahun berhasil ditambahkan !');
                }
                redirect('guru/nilai_p');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
    public function inputNilaiUm()
    {
        $dataUser = $this->db->get_where('t_user', ['username' => $this->session->userdata('username')])->row_array();
        $newKdlembaga = $dataUser['kd_user'];
        $this->db->select('*');
        $this->db->from('t_tapel');
        $this->db->where('sts_tapel', 1);
        $this->db->where('kd_lembaga', $newKdlembaga);
        $this->db->order_by('t_tapel.kd_tahun', SORT_ASC);
        $dataTapel = $this->db->get()->row_array();
        $newTapel = $dataTapel['id_tapel'];
        $this->form_validation->set_rules('kd_students[]', 'Kode Siswa', 'required|trim');
        $this->form_validation->set_rules('kd_gmapel', 'Kode Guru Mapel', 'required|trim');
        $this->form_validation->set_rules('n_p[]', 'Nilai Pengetahuan Ujian Madrasah', 'required|numeric|trim');
        $newKdstudents = $this->input->post('kd_students');
        $newKdgmapel = htmlspecialchars($this->input->post('kd_gmapel'));
        $newNilaip = $this->input->post('n_p');
        if ($this->form_validation->run() == false) {
            $this->session->set_flashdata('messageWarning', 'Data tidak boleh kosong !');
            redirect('guru/nilai_p');
        } else {
            $data = array();
            foreach ($newKdstudents as $nrs => $rs) {
                $data[$nrs]['kd_students'] = $rs;
            }
            $cekKdp = $this->db->get_where('t_nilaip', array('kd_gmapel' => $newKdgmapel, 'kd_students' => $rs, 'kd_tapel' => $newTapel, 'kd_lembaga' => $newKdlembaga, 'tipe_n' => 's'))->num_rows();
            if ($cekKdp > 0) {
                $data =  $this->input->post();
                for ($i = 0; $i < count($data['id_nilaip']); $i++) {
                    $update[] = array(
                        'id_nilaip'     => $data['id_nilaip'][$i],
                        'kd_students'   => $data['kd_students'][$i],
                        'kd_gmapel'     => $newKdgmapel,
                        'n_p'           => $data['n_p'][$i],
                        'tipe_n'        => 's',
                        'kd_tapel'      => $newTapel,
                        'kd_lembaga'    => $newKdlembaga,
                    );
                }
                $this->m_guru->updateDataNilaip($update, 'id_nilaip');
                $this->session->set_flashdata('messageSave', 'Nilai Ujian madrasah berhasil diupdate !');
                redirect('guru/nilai_p');
            } else {
                $data = array();
                foreach ($newKdstudents as $nrs => $rs) {
                    $data[$nrs]['kd_students'] = $rs;
                    $data[$nrs]['kd_gmapel'] =  $newKdgmapel;
                    $data[$nrs]['n_p'] = $newNilaip;
                    $data[$nrs]['tipe_n'] = 's';
                    $data[$nrs]['kd_tapel'] = $newTapel;
                    $data[$nrs]['kd_lembaga'] = $newKdlembaga;
                }
                foreach ($newNilaip as $nrs => $rs) {
                    $data[$nrs]['n_p'] = $rs;
                }
                $this->m_guru->inputDataNilaip($data);
                $this->session->set_flashdata('messageSave', 'Nilai ujian madrasah berhasil ditambahkan !');
                redirect('guru/nilai_p');
                // created by : KuduSinau_Hendrik_081275532220
            }
        }
    }
}
// created by : KuduSinau_Hendrik_081275532220 (TERIMAKASIH UNTUK MENDAPATKAN KEBERKAHAN, DENGAN TIDAK MERUBAH NAMA HAK CIPTA, PLAGIAT, DAN TIDAK MEMPERJUALBELIKAN APLIKASI INI, SEMOGA KITA SEMUA MENDAPAT MANFAAT DAN KEBERKAHAN DARI APLIKASI INI)
