<?php
class Upload extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('m_materi');
        $this->load->model('m_guru');
    }

    function index() {
        if ($this->session->status == '2' || empty($this->session->user)) return redirect('elearning/home');
        $id = $this->session->id;
        $data['guru'] = $this->m_guru->get_guru($id)->result()[0];
        $this->load->view('elearning/v_upload', $data);
    }

    function upload_materi() {
        $config['upload_path'] = './assets/files/materi';
        $config['allowed_types'] = 'pdf|docx|jpg|png|bmp|psd';
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = 2048;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('materi-konten')) {
            $err = $this->upload->display_errors();
            $this->session->set_flashdata('err', '<div class="mt-1 alert alert-danger">'.$err.'</div>');
            redirect('elearning/upload');
        } else {
            $guru_id = $this->session->id;
            $kelas_guru = array_column($this->m_guru->get_guru_kelas($guru_id)->result_array(), 'ajar_kelas_id');
            $materi_mapel = $this->input->post('materi-mapel');
            $materi_judul = $this->input->post('materi-judul');
            $konten = $this->upload->data()['file_name'];
            $materi_catatan = $this->input->post('materi-catatan');
            $this->m_materi->save_materi($guru_id, $kelas_guru, $materi_mapel, $materi_judul, $konten, $materi_catatan);
            $this->session->set_flashdata('msg', '<div class="mt-1 alert alert-success">Upload Materi Sukses</div>');
            redirect('elearning/home');
        }
    }
}
?>