<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Course extends Admin_Core_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('course_model', 'course');
	}

	// Course Category Start---------------------------------------

    function all_category()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_category'] = $this->course->get_all_course_category();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/all_course_category');
        $this->load->view('admin/include/footer');
    }

    function add_course_category()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        // $data['all_programm'] = $this->course->get_all_course_programm();
        $data['all_category'] = $this->course->get_all_course_category();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/add_course_category');
        $this->load->view('admin/include/footer');
    }

    function add_new_course_category()
    {
        if ($this->course->add_new_course_category()) 
        {
            $this->session->set_userdata('success', "Category Successfully Added");
            redirect(base_url().'admin/course/all_category');
        } else {
            $this->session->set_userdata('error', " Category Added Failed");
            redirect($this->agent->referrer());
        }
    }

    function edit_course_category($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['category'] = $this->course->get_course_category_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/edit_course_category');
        $this->load->view('admin/include/footer');
    }

    function update_course_category($id)
    {
        if ($this->course->update_course_category($id)) 
        {
            $this->session->set_userdata('success', "Category Successfully Updated");
            redirect(base_url().'admin/course/all_category');
        } else {
            $this->session->set_userdata('error', " Category Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_course_category($id)
    {
        if ($this->course->delete_course_category($id)) 
        {
            $this->session->set_userdata('success', "Category Successfully Delete");
            redirect(base_url().'admin/course/all_category');
        } else {
            $this->session->set_userdata('error', " Category Delete Failed");
            redirect($this->agent->referrer());
        }
    }
    // Course Category End

    // Course Add Start

    public function view_all_course()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting(); 
        $data['get_all_course'] = $this->course_model->get_all_course();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/view_all_course');
        $this->load->view('admin/include/footer');
    }

    public function add_new_course()
	{
		$data['app_setting'] = $this->setting_model->get_all_app_setting();	
       	$data['all_category'] = $this->course->get_all_course_category();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/add_new_course');
        $this->load->view('admin/include/footer');
	}

	public function add_course()
    {
        if ($this->course_model->add_course()) {
            $this->session->set_userdata('success', "Course Successfully Added");
             redirect(base_url().'admin/course/view_all_course');
        } else {
            $this->session->set_userdata('error', "Course Added Faild");
            redirect($this->agent->referrer());
        }
    }

    public function edit_course($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting(); 
        $data['course'] = $this->course_model->get_course_by_id($id);
        $data['all_category'] = $this->course->get_all_course_category();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/edit_course');
        $this->load->view('admin/include/footer');
    }

    public function update_course($id)
    {
        if ($this->course_model->edit_course($id)) {
            $this->session->set_userdata('success', "Course Successfully Updated");
            redirect(base_url().'admin/course/view_all_course');
        } else {
            $this->session->set_userdata('error', "Course Updated Faild");
            redirect($this->agent->referrer());
        }
    }

    public function delete_course($id)
    {
        if ($this->course_model->delete_course($id)) {
            $this->session->set_userdata('success', "Course Successfully delete");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Course delete Faild");
            redirect($this->agent->referrer());
        }
    }

    // Course Add End 

    // Course Lession Start

    public function course_lession()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting(); 
        $data['get_all_course'] = $this->course_model->get_all_course();
        $data['get_all_lession'] = $this->course_model->get_all_lession();
        
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/course_lession');
        $this->load->view('admin/include/footer');
    }

    
    public function add_new_lession()
    {
        if ($this->course_model->add_new_lession()) {
            $this->session->set_userdata('success', "Lession Successfully Added");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Lession Added Faild");
            redirect($this->agent->referrer());
        }
    }

    public function update_lession()
    {
        if ($this->course_model->update_lession()) {
            $this->session->set_userdata('success', "Lession Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Lession Updated Faild");
            redirect($this->agent->referrer());
        }
    }

    public function delete_lession($id)
    {
        if ($this->course_model->delete_lession($id)) {
            $this->session->set_userdata('success', "Lession Successfully delete");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Lession delete Faild");
            redirect($this->agent->referrer());
        }
    }

    function delete_ass_pdf($id)
    {
        echo $this->course_model->delete_ass_pdf($id);
    }

    function delete_mini_pdf($id)
    {
        echo $this->course_model->delete_mini_pdf($id);
    }

    // Course lession End

    // Add Subject Start---------------------------------------
    function all_subject()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_subject'] = $this->course->get_all_subject();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/all_subject');
        $this->load->view('admin/include/footer');
    }
    function add_new_subject()
    {
        if ($this->course->add_new_subject()) 
        {
            $this->session->set_userdata('success', "Subject Successfully Added");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Subject Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function update_subject($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['subject'] = $this->course->get_subject_by_id($id);
        // $data['all_course'] = $this->course->get_all_course();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/update_subject');
        $this->load->view('admin/include/footer');   
    }

    function edit_subject($id)
    {
        if ($this->course->edit_subject($id)) 
        {
            $this->session->set_userdata('success', "Subject Successfully Updated");
            redirect(base_url().'admin/course/all_subject');
        } else {
            $this->session->set_userdata('error', " Subject Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_subject($id)
    {
        $this->db->select('course_subject');
        $all_course = $this->db->get('course')->result();

        $data = [];
        foreach ($all_course as $course) 
        {
            foreach (json_decode($course->course_subject) as $course_subject) 
            {
                $data[] = $course_subject;
            }
        }
        if (($key = array_search($id,$data)) !== FALSE) 
        {
            $this->session->set_userdata('error', "Please Delete Course First");
             redirect($this->agent->referrer());
        }
        else
        {
            if ($this->course->delete_subject($id)) 
            {
                $this->session->set_userdata('success', "Subject Successfully Deleted");
                redirect($this->agent->referrer());
            } else {
                $this->session->set_userdata('error', " Subject Deleted Failed");
                redirect($this->agent->referrer());
            }
        }

        // if($no >= 0)
        // {
        //     echo 5;
        // }
        // if(!$no)
        // {
        //     
        // }
        // else
        // {
        //     
        // }
    }

    function get_subject_chapter_by_sub_id($id)
    {
        $this->db->from('course_subject');
        $this->db->where('subject_id', $id);
        $data = $this->db->get()->row();

        $option = '<option value="">Select Chapter From List</option>';

        foreach (json_decode($data->subject_chapter) as $key => $main_d) 
        {
            $option .= '<option value="'.$key.'">'.$main_d.'</option>';
        }


        echo $option;
    }
    // Add Subject End

    // Course Outline Start

    public function all_lession_and_chapter()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_lession'] = $this->course->get_all_lession_and_chapter();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/all_lession_and_chapter');
        $this->load->view('admin/include/footer'); 
        // echo '<pre>';
        // print_r($data['all_lession']);
    }

    public function add_new_lession_and_chapter()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->course->get_all_course_for_adding_lession();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/add_new_lession_and_chapter');
        $this->load->view('admin/include/footer'); 

    }

    public function add_lession_and_chapter()
    {
        if ($this->course->add_lession_and_chapter()) 
        {
            $this->session->set_userdata('success', "Lession & Chapter Successfully Added");
            redirect(base_url().'admin/course/all_lession_and_chapter');
        } else {
            $this->session->set_userdata('error', " Lession & Chapter Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function delete_course_lession($id)
    {
        if ($this->course->delete_course_lession($id)) 
        {
            $this->session->set_userdata('success', "Lession & Chapter Successfully Deleted");
            redirect(base_url().'admin/course/all_lession_and_chapter');
        } else {
            $this->session->set_userdata('error', " Lession & Chapter Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    public function edit_course_lession($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->course->get_all_course_for_adding_lession();
        $data['all_lession'] = $this->course->get_lession_and_chapter_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/edit_course_lession');
        $this->load->view('admin/include/footer'); 
    }

    public function update_lession_and_chapter()
    {
        if ($this->course->update_lession_and_chapter()) 
        {
            $this->session->set_userdata('success', "Lession & Chapter Successfully Updated");
            redirect(base_url().'admin/course/all_lession_and_chapter');
        } else {
            $this->session->set_userdata('error', " Lession & Chapter Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    // Course Outline End

    // My Task Start
    public function my_task_details($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_lession'] = $this->course->get_all_lession_by_course_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/my_task_details');
        $this->load->view('admin/include/footer'); 
    }

    public function update_lession_status_for_attendance()
    {
        $key = $this->input->post('key');
        $chapter_id = $this->input->post('lession_id');
        $inst_note = $this->input->post('inst_note');
        $data = array(
            'inst_att_chapter_id' => $chapter_id, 
            'inst_att_lession_id' => $key,
            'inst_att_lession_status' => 1,
            'inst_note' => $inst_note,
            'inst_att_course_id' => $this->input->post('course_id'),
            'inst_att_user_id' => user()->user_id,
            'inst_att_date_time' => date('Y-m-d H:i:s')
        );

        $response = $this->db->insert('instructor_attendance', $data);
        if($response)
        {
            echo 1;
        }
        else
        {
            echo 0;
        }
    }

    public function update_lession_status_for_attendance_unread()
    {
        $key = $this->input->post('key');
        $chapter_id = $this->input->post('lession_id');
        $user_id = user()->user_id;

        $this->db->where('inst_att_user_id', $user_id);
        $this->db->where('inst_att_chapter_id', $chapter_id);
        $this->db->where('inst_att_lession_id', $key);
        $response = $this->db->delete('instructor_attendance');
        if($response)
        {
            echo 1;
        }
        else
        {
            echo 0;
        }
    }

    function get_all_study_note()
    {
        $user_id = user()->user_id;
        $date = $this->input->post('date');
        // $date = '2023-01-16';

        $this->db->where('DATE(inst_att_date_time)', $date);
        $this->db->where('inst_att_user_id', $user_id);
        $all_notes = $this->db->get('instructor_attendance')->result();

        $option = '';
        $i = 1;
        foreach ($all_notes as $note) 
        {
            $this->db->select('title');
            $this->db->where('id', $note->inst_att_course_id);
            $course = $this->db->get('course')->row();

            $option .= '<b>Course name : '.$course->title.'</b>
                        <br/>
                        <div>'.$i.') '.$note->inst_note.'</div>
                        ';
        $i++;}

        echo $option;
    }

    function student_doubt()
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_doubt'] = $this->course->get_all_doubt_by_batch_id();
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/course/student_doubt');
        $this->load->view('admin/include/footer'); 
    }

    function submit_relplay_message_for_doubt()
    {
        if ($this->course->submit_relplay_message_for_doubt()) 
        {
            $this->session->set_userdata('success', "Message Successfully Updated");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', "Message Updated Failed");
            redirect($this->agent->referrer());
        }
    }
    // My Task End

    function get_subject_for_question_paper_set()
    {
        $course = $this->input->post('course_id');

        // $course = $id;

        $this->db->where('id', $course);
        $course_d = $this->db->get('course')->row();

        foreach(json_decode($course_d->course_subject) as $subject)
        {
            $this->db->where('subject_id', $subject);
            $subject = $this->db->get('course_subject')->row();

            $this->db->select('q_id');
            $this->db->where('q_subject', $subject->subject_id);
            $is_exits = $this->db->get('exam_questions')->row();
            if($is_exits)
            {
                $option_2 .= '<div class="col-md-6">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Subject Name </label>
                       <div class="col-sm-12">
                          <input class="form-control" value="'.$subject->subject_name.'" readonly>
                          <input class="form-control" type="hidden" value="'.$subject->subject_id.'" name="paper_subject_id[]" placeholder="Enter Question No" required>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Question Limit <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input onkeyup="check_qns(this.value)" class="form-control qns_limit" placeholder="Enter Question Limit" name="paper_question_limit[]" value="0" min="0">
                       </div>
                    </div>
                  </div>';
              }
        }
        $option_2 .= '<div class="col-md-12" id="btn_add">
            <div class="form-group">
            <button name="form1" style="float: right" type="submit" class="btn btn-success">Add</button>
            </div>
        </div>';

        $this->db->where('paper_course_id', $course);
        $this->db->group_by('paper_unique_id');
        $this->db->order_by('DATE(paper_date_time)', 'DESC');
        $all_paper = $this->db->get('question_paper')->result();

        $option = '<table class="table table-bordered" style="overflow-x:auto;border-collapse: collapse;">';

        $a = 1; foreach ($all_paper as $key => $paper) 
        {
            $this->db->select('a.*, b.*');
            $this->db->from('question_paper a');
            $this->db->join('course_subject b', 'b.subject_id = a.paper_subject_id');
            $this->db->where('a.paper_unique_id', $paper->paper_unique_id);
            $paper_data = $this->db->get()->result();

            $option .= '<tr>';
            $option .= '<th>Subject Name <i class="fa fa-arrow-right" aria-hidden="true"></i></th>';
            foreach ($paper_data as $data_paper)
            {
                $option .= '<th>'.$data_paper->subject_name.'</th>';
            }
            $option .= '<th>Status</th>';

            $option .= '</tr>';

            $option .= '<tr>';
            $option .= '<td>Qns. Limit <i class="fa fa-arrow-right" aria-hidden="true"></i></td>';
            foreach ($paper_data as $data_paper)
            {
                $option .= '<td>'.$data_paper->paper_question_limit.'</td>';
            }
            if($a == 1)
            {
                $option .= '<td><span class="badge badge-success">Active</span></td>';
            }
            else
            {
                $option .= '<td><span class="badge badge-danger">Deactive</span></td>';
            }
            
            $option .= '</tr>';
        $a++;}

        $option .= '</table>';

        $rrt = array(
            'total_qns'         => $course_d->total_questions,
            'subject_details'   => $option_2,
            'subject_table'     => $option 
        );

        echo json_encode($rrt);
    }
}
