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

class Question extends Admin_Core_Controller
{

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


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

    function add_question()
    {
        if ($this->course->add_question()) {
            $this->session->set_userdata('success', "Question Successfully Added");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Question Added Failed");
            redirect($this->agent->referrer());
        }
    }

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

    public function fetch_all_qns()
    {
        $all_question = $this->course->view_all_questions();
        $data = array();
        $i = 1;
        foreach ($all_question as $question) {
            $row = array(
                '<input name="checkbox[]" class="sub_chk_pop" type="checkbox" data-id="' . $question->q_id . '">',
                $i,
                $question->q_question,
                $question->q_ans_a,
                $question->q_ans_b,
                $question->q_ans_c,
                $question->q_ans_d,
                $question->q_ans_correct,
                ($question->q_type == 2) ? 'True / False' : 'Objective',
                $question->subject_name,
                '<div class="input-group mb-3">
                          <div class="input-group-prepend">
                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                            Action
                            </button>
                            <div class="dropdown-menu">
                              <a class="dropdown-item" href="' . base_url() . '/admin/question/edit_question/' . $question->q_id . '"><i class="fas fa-edit"></i>&nbsp;&nbsp;Edit</a>
                              <div class="dropdown-divider"></div>
                              <a class="dropdown-item" onclick="return confirm(\'Are you sure?\')" href="' . base_url() . '/admin/question/delete_question/' . $question->q_id . '"><i class="fas fa-trash"></i>&nbsp;&nbsp;Delete</a>
                            </div>
                          </div>
                        </div>',
            );
            $data[] = $row;
            $i++;
        }

        // Prepare the response to be sent as JSON
        $response = array(
            'data' => $data,
        );

        // Return the response as JSON
        echo json_encode($response);
    }


    public function edit_question($id)
    {
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        // $data['all_programm'] = $this->course->get_all_course_programm();
        $data['question'] = $this->course->get_question_by_id($id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/question/edit_question');
        $this->load->view('admin/include/footer');
    }

    public function update_question($id)
    {
        if ($this->course->update_question($id)) {
            $this->session->set_userdata('success', "Question Successfully Updated");
            redirect(base_url() . 'admin/question/view_all_question');
        } else {
            $this->session->set_userdata('error', " Question Updated Failed");
            redirect($this->agent->referrer());
        }
    }

    public function delete_question($id)
    {
        if ($this->course->delete_question($id)) {
            $this->session->set_userdata('success', "Question Successfully Deleted");
            redirect(base_url() . 'admin/question/view_all_question');
        } else {
            $this->session->set_userdata('error', " Question Deleted Failed");
            redirect($this->agent->referrer());
        }
    }

    function delete_multiple_qns()
    {
        echo $this->course->delete_multiple_qns();
    }
}
