ion_auth->logged_in()) { redirect('auth'); } else if (!$this->ion_auth->is_admin()) { show_error('Hanya Administrator yang diberi hak untuk mengakses halaman ini, Kembali ke menu awal', 403, 'Akses Terlarang'); } $this->load->library(['datatables', 'form_validation']); // Load Library Ignited-Datatables $this->load->model('Master_model', 'master'); $this->form_validation->set_error_delimiters('', ''); } public function output_json($data, $encode = true) { if ($encode) $data = json_encode($data); $this->output->set_content_type('application/json')->set_output($data); } public function index() { $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Kelompok Soal', 'subjudul' => 'Data Kelompok Soal' ]; $this->load->view('_templates/dashboard/_header.php', $data); $this->load->view('master/ksoal/data'); $this->load->view('_templates/dashboard/_footer.php'); } public function data() { $this->output_json($this->master->getDataKsoal(), false); } public function add() { $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Tambah Kelompok Soal', 'subjudul' => 'Tambah Data Kelompok Soal', 'banyak' => $this->input->post('banyak', true) ]; $this->load->view('_templates/dashboard/_header.php', $data); $this->load->view('master/ksoal/add'); $this->load->view('_templates/dashboard/_footer.php'); } public function edit() { $chk = $this->input->post('checked', true); if (!$chk) { redirect('ksoal'); } else { $ksoal = $this->master->getKsoalById($chk); $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Edit Kelompok Soal', 'subjudul' => 'Edit Data Kelompok Soal', 'ksoal' => $ksoal ]; $this->load->view('_templates/dashboard/_header.php', $data); $this->load->view('master/ksoal/edit'); $this->load->view('_templates/dashboard/_footer.php'); } } public function save() { $rows = count($this->input->post('nama_ksoal', true)); $mode = $this->input->post('mode', true); for ($i = 1; $i <= $rows; $i++) { $nama_ksoal = 'nama_ksoal[' . $i . ']'; $this->form_validation->set_rules($nama_ksoal, 'Mata Kuliah', 'required'); $this->form_validation->set_message('required', '{field} Wajib diisi'); if ($this->form_validation->run() === FALSE) { $error[] = [ $nama_ksoal => form_error($nama_ksoal) ]; $status = FALSE; } else { if ($mode == 'add') { $insert[] = [ 'nama_ksoal' => $this->input->post($nama_ksoal, true) ]; } else if ($mode == 'edit') { $update[] = array( 'id_ksoal' => $this->input->post('id_ksoal[' . $i . ']', true), 'nama_ksoal' => $this->input->post($nama_ksoal, true) ); } $status = TRUE; } } if ($status) { if ($mode == 'add') { $this->master->create('mkwu_ksoal', $insert, true); $data['insert'] = $insert; } else if ($mode == 'edit') { $this->master->update('mkwu_ksoal', $update, 'id_ksoal', null, true); $data['update'] = $update; } } else { if (isset($error)) { $data['errors'] = $error; } } $data['status'] = $status; $this->output_json($data); } public function delete() { $chk = $this->input->post('checked', true); if (!$chk) { $this->output_json(['status' => false]); } else { if ($this->master->delete('mkwu_ksoal', $chk, 'id_ksoal')) { $this->output_json(['status' => true, 'total' => count($chk)]); } } } public function import($import_data = null) { $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Kelompok Soal', 'subjudul' => 'Import Kelompok Soal' ]; if ($import_data != null) $data['import'] = $import_data; $this->load->view('_templates/dashboard/_header', $data); $this->load->view('master/ksoal/import'); $this->load->view('_templates/dashboard/_footer'); } public function preview() { $config['upload_path'] = './uploads/import/'; $config['allowed_types'] = 'xls|xlsx|csv'; $config['max_size'] = 2048; $config['encrypt_name'] = true; $this->load->library('upload', $config); if (!$this->upload->do_upload('upload_file')) { $error = $this->upload->display_errors(); echo $error; die; } else { $file = $this->upload->data('full_path'); $ext = $this->upload->data('file_ext'); switch ($ext) { case '.xlsx': $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); break; case '.xls': $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls(); break; case '.csv': $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv(); break; default: echo "unknown file ext"; die; } $spreadsheet = $reader->load($file); $sheetData = $spreadsheet->getActiveSheet()->toArray(); $ksoal = []; for ($i = 1; $i < count($sheetData); $i++) { if ($sheetData[$i][0] != null) { $ksoal[] = $sheetData[$i][0]; } } unlink($file); $this->import($ksoal); } } public function do_import() { $data = json_decode($this->input->post('ksoal', true)); $jurusan = []; foreach ($data as $j) { $jurusan[] = ['nama_ksoal' => $j]; } $save = $this->master->create('mkwu_ksoal', $jurusan, true); if ($save) { redirect('ksoal'); } else { redirect('ksoal/import'); } } }