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

class QuestionController extends CI_Controller
{

    public function questionCategory()
    {
       
    
        $view_data['records']  =$this->mcommon->records_all('question_category');
        $data = array(
            'title' => 'Question Category',
            'page' => 'Question Category',
            'content' => $this->load->view('masters/questionCategory', $view_data, true),
        );
        $this->load->view('base/base_template', $data);
    }

    public function addEditQuestionCategory()
    {

        $question_category = $this->input->post('question_category');
        $id = $this->input->post('id');
        if ($id) {
            $update_array = array(
                'question_category'    => $question_category,
                'status'               => 1 ,
            );
            $update = $this->mcommon->common_edit('question_category', $update_array, array('id' => $id));


            if ($update) {


                $this->session->set_flashdata('alert_success', 'Question  Category Updated successfully!');
            } else {

                $this->session->set_flashdata('alert_danger', 'Something went wrong');
            }
        } else {
            $user_array = array(
                'question_category'    => $question_category,
                'status'               => 1 ,
            );
            $insert = $this->mcommon->common_insert('question_category', $user_array);


            if ($insert) {


                $this->session->set_flashdata('alert_success', 'Question Category Added successfully!');
            } else {

                $this->session->set_flashdata('alert_danger', 'Something went wrong');
            }
        }


        redirect('QuestionController/questionCategory');
    }
   
    public function viewQuestion()
    {
        
        $view_data['records'] =  $this->mcommon->records_all('question',array('exam_type',1));
        $view_data['exam_type'] =  $this->mcommon->records_all('exam_type',array('status',1));
        $view_data['academic_year'] =  $this->mcommon->records_all('accadamic_year',array('status',1));
        $view_data['question_category'] =  $this->mcommon->records_all('question_category',array('status',1));
        $view_data['category'] =  $this->mcommon->records_all('section_category',array('status',1));
        $data = array(
            'title' => 'View Question ',
            'page' => 'View Question',
            'content' => $this->load->view('questions/view', $view_data, true),
        );
        $this->load->view('base/base_template', $data);
    }
    public function questionAdd()
    {

        if(isset($_POST['submit'])){
          
            $academic = $this->input->post('academic');
            $exam_type = $this->input->post('exam_type');
            $category = $this->input->post('category');
            $question_category = $this->input->post('question_category');
            $question = $this->input->post('question');
            $option_a = $this->input->post('option_a');
            $option_b = $this->input->post('option_b');
            $option_c = $this->input->post('option_c');
            $option_d = $this->input->post('option_d');
            $answer = $this->input->post('answer');

            $record = $this->mcommon->specific_row('exam_type', array('id' => $exam_type));

            $totalQuestions = isset($record['total_questions']) ? $record['total_questions'] : 0;

            $count = $this->mcommon->getQuestionCount($exam_type,$category);

            if($totalQuestions == $count)
            {
                $this->session->set_flashdata('alert_danger', 'Question Limit Exceeded successfully!');
                redirect('QuestionController/viewQuestion');
                exit();
            }

            $insert_array = array(
                        'academic'          =>  $academic,
                        'exam_type'         =>  $exam_type,
                        'category'          =>  $category,
                        'question_category' => $question_category,
                        'question'          =>  $question,
                        'option_a'          =>  $option_a,
                        'option_b'          =>  $option_b,
                        'option_c'          =>  $option_c,
                        'option_d'          =>  $option_d,
                        'answer'            =>  $answer,
                       
                    );
            
                    $insert = $this->mcommon->common_insert('question', $insert_array);
            
                    if ($insert) {
            
                        $this->session->set_flashdata('alert_success', 'Question  added successfully!');
                        redirect('QuestionController/viewQuestion');
                    
                    } else {
                        $this->session->set_flashdata('alert_danger', 'Error in Add Question!');
                    }

        }else{

            $view_data['exam_type'] =  $this->mcommon->records_all('exam_type',array('status',1));
            $view_data['academic_year'] =  $this->mcommon->records_all('accadamic_year',array('status',1));
            $view_data['category'] =  $this->mcommon->records_all('section_category',array('status',1));
            $view_data['question_category'] =  $this->mcommon->records_all('question_category',array('status',1));

            $data = array(
                'title' => 'Add  Question',
                'page' => 'Add  Question',
                'content' => $this->load->view('questions/add', $view_data, true),
            );
            $this->load->view('base/base_template', $data);
        }
            
        }

        public function questionEdit($id){
         
           
        if(isset($_POST['submit'])){

            $academic = $this->input->post('academic');
            $exam_type = $this->input->post('exam_type');
            $category = $this->input->post('category');
            $question_category = $this->input->post('question_category');
            $question = $this->input->post('question');
            $option_a = $this->input->post('option_a');
            $option_b = $this->input->post('option_b');
            $option_c = $this->input->post('option_c');
            $option_d = $this->input->post('option_d');
            $answer = $this->input->post('answer');

            $update_array = array(

                        'academic'          =>  $academic,
                        'exam_type'         =>  $exam_type,
                        'category'          =>  $category,
                        'question_category' =>  $question_category,
                        'question'          =>  $question,
                        'option_a'          =>  $option_a,
                        'option_b'          =>  $option_b,
                        'option_c'          =>  $option_c,
                        'option_d'          =>  $option_d,
                        'answer'            =>  $answer,
                    );
                  
                    $update = $this->mcommon->common_edit('question',$update_array,array('id' => $id));
            
                    if ($update) {
            
                        $this->session->set_flashdata('alert_success', 'Question  Updated successfully!');
                        redirect('QuestionController/viewQuestion');
                    
                    } else {
                        $this->session->set_flashdata('alert_danger', 'Error in Updated Question!');
                    }

        }else{

            $view_data['default'] = $this->mcommon->specific_row('question', array('id' => $id));
            $view_data['exam_type'] =  $this->mcommon->records_all('exam_type',array('status',1));
            $view_data['academic_year'] =  $this->mcommon->records_all('accadamic_year',array('status',1));
            $view_data['category'] =  $this->mcommon->records_all('section_category',array('status',1));
            $view_data['question_category'] =  $this->mcommon->records_all('question_category',array('status',1));
            $data = array(
                'title' => 'Edit  Question',
                'page' => 'Edit  Question',
                'content' => $this->load->view('questions/edit', $view_data, true),
            );
            $this->load->view('base/base_template', $data);
        }

        }

        public function deleteQuestion($id)
        {
            $delete = $this->mcommon->common_delete('question', array('id' => $id));
    
            $this->session->set_flashdata('alert_danger', 'Record Deleted successfully!');
            redirect('ApplicationController/viewQuestion');
        }
    







        public function viewEssayQuestion()
        {
            
            $view_data['records'] =  $this->mcommon->records_all('question',array('exam_type',2));
            $view_data['exam_type'] =  $this->mcommon->records_all('exam_type',array('status',1));
            $view_data['academic_year'] =  $this->mcommon->records_all('accadamic_year',array('status',1));
            $view_data['question_category'] =  $this->mcommon->records_all('question_category',array('status',1));
            $view_data['category'] =  $this->mcommon->records_all('section_category',array('status',1));
            $data = array(
                'title' => 'View Question ',
                'page' => 'View Question',
                'content' => $this->load->view('essay_question/view', $view_data, true),
            );
            $this->load->view('base/base_template', $data);
        }
        public function essayquestionAdd()
        {
    
            if(isset($_POST['submit'])){
              
                $academic = $this->input->post('academic');
                $exam_type = $this->input->post('exam_type');
                $category = $this->input->post('category');
                $schedule = $this->input->post('schedule');
                $question = $this->input->post('question');
              
    
                $insert_array = array(
                            'academic'          =>  $academic,
                            'exam_type'         =>  $exam_type,
                            'category'          =>  $category,
                            'schedule_id'          => $schedule,
                            'question'          =>  $question,
                            
                           
                        );
                
                        $insert = $this->mcommon->common_insert('question', $insert_array);
                
                        if ($insert) {
                
                            $this->session->set_flashdata('alert_success', 'Question  added successfully!');
                            redirect('QuestionController/viewQuestion');
                        
                        } else {
                            $this->session->set_flashdata('alert_danger', 'Error in Add Question!');
                        }
    
            }else{
    
                $view_data['exam_type'] =  $this->mcommon->records_all('exam_type',array('status',1));
                $view_data['academic_year'] =  $this->mcommon->records_all('accadamic_year',array('status',1));
                $view_data['category'] =  $this->mcommon->records_all('section_category',array('status',1));
                $view_data['question_category'] =  $this->mcommon->records_all('question_category',array('status',1));
    
                $data = array(
                    'title' => 'Add  Question',
                    'page' => 'Add  Question',
                    'content' => $this->load->view('essay_question/add', $view_data, true),
                );
                $this->load->view('base/base_template', $data);
            }
                
            }
    
            public function essayquestionEdit($id){
             
               
            if(isset($_POST['submit'])){
    
                $academic = $this->input->post('academic');
                $exam_type = $this->input->post('exam_type');
                $category = $this->input->post('category');
                $schedule = $this->input->post('schedule');
                $question = $this->input->post('question');
                
    
                $update_array = array(
    
                            'academic'          =>  $academic,
                            'exam_type'         =>  $exam_type,
                            'category'          =>  $category,
                            'schedule_id'       =>  $schedule,
                            'question'          =>  $question,
                           
                        );
                      
                        $update = $this->mcommon->common_edit('question',$update_array,array('id' => $id));
                
                        if ($update) {
                
                            $this->session->set_flashdata('alert_success', 'Question  Updated successfully!');
                            redirect('QuestionController/viewQuestion');
                        
                        } else {
                            $this->session->set_flashdata('alert_danger', 'Error in Updated Question!');
                        }
    
            }else{
    
                $view_data['default'] = $this->mcommon->specific_row('question', array('id' => $id));
                $view_data['exam_type'] =  $this->mcommon->records_all('exam_type',array('status',1));
                $view_data['academic_year'] =  $this->mcommon->records_all('accadamic_year',array('status',1));
                $view_data['category'] =  $this->mcommon->records_all('section_category',array('status',1));
                $view_data['question_category'] =  $this->mcommon->records_all('question_category',array('status',1));
                $data = array(
                    'title' => 'Edit  Question',
                    'page' => 'Edit  Question',
                    'content' => $this->load->view('essay_question/edit', $view_data, true),
                );
                $this->load->view('base/base_template', $data);
            }
    
            }

        

     

}
