<?php
class Assignment extends BM_Controller {
    public function get(){
        $res = $this->sp('sp_getAssignments', array(), 'LOCALDB')->result();
        return $this->load->view('json_view', array('json' => $res));
    }
    public function insert(){
        $post = $this->input->post();
        $filename = date("Y").'-'.date("m").'-'.$post['Filename'];
        $res = $this->sp('sp_insertAssignments', array(
            'AssignmentName' => $post['AssignmentName'],
            'AssignmentPath' => "storage/".$filename,
        ), 'LOCALDB')->result();
        if(count($res)==0){
            if (!file_exists('storage')) mkdir('storage');
            foreach ($_FILES as $file) file_put_contents("storage/".$filename, fopen($file["tmp_name"], "r"));
        }
        return $this->load->view('json_view', array('json' => array('success' => true)));
    }
    public function getByID(){
        $post = $this->input->post();
        $res = $this->sp('sp_getAssignmentByID', array(
            'AssignmentID' => $post['id'],
        ), 'LOCALDB')->result()[0];
        return $this->load->view('json_view', array('json' => $res));
    }
    public function update(){
        $post = $this->input->post();
        $filename = date("Y").'-'.date("m").'-'.$post['Filename'];
        $res = $this->sp('sp_updateAssignments', array(
            'AssignmentID' => $post['AssignmentID'],
            'AssignmentName' => $post['AssignmentName'],
            'AssignmentPath' => "storage/".$filename,
        ), 'LOCALDB')->result();
        if(count($res)==0){
            if (!file_exists('storage')) mkdir('storage');
            foreach ($_FILES as $file) file_put_contents("storage/".$filename, fopen($file["tmp_name"], "r"));
        }
        return $this->load->view('json_view', array('json' => array('success' => true)));
    }
}