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

class Examination_modal extends CI_Model 
{

    // Course Programm Start---------------------------------------

    function add_new_assign_exam()
    {
         $course_id = $this->input->post('st_course_course_id');
        $exam_date = $this->input->post('st_student_exam_date');
        $exam_time = $this->input->post('st_student_exam_time');

        for ($i=0; $i < count($this->input->post('st_course_id')); $i++) 
        { 
            $data = array(
                'st_course_is_exam_approve' => 1,
                'st_student_exam_date' => $exam_date,
                'st_student_exam_time' => $exam_time,
                'st_course_status' => 4
                );
            $this->db->where('st_course_id', $this->input->post('st_course_id')[$i]);
            $this->db->update('student_course', $data);
        }

        return true;
    }

    function add_new_assign_exam_exam()
    {
        $course_id = $this->input->post('st_course_course_id');
        $exam_date = $this->input->post('st_student_exam_date');
        $exam_time = $this->input->post('st_student_exam_time');
        $viva_exam_time = $this->input->post('st_student_viva_exam_date');
        $viva_exam_time = $this->input->post('st_student_viva_exam_time');    

        for ($i=0; $i < count($this->input->post('st_course_id')); $i++) 
        { 
            $data = array(
                'st_course_is_exam_approve' => 1,
                'st_student_exam_date' => $exam_date,
                'st_student_exam_time' => $exam_time,
                'st_student_viva_exam_date' => $viva_exam_time,
                'st_student_viva_exam_time' => $viva_exam_time,
                'st_course_status' => 4
                );
            $this->db->where('st_course_id', $this->input->post('st_course_id')[$i]);
            $this->db->update('student_course', $data);
        }

        return true;
    }

    function add_new_assign_re_exam()
    {
        $course_id = $this->input->post('st_course_course_id');
        $exam_date = $this->input->post('st_student_exam_date');
        $exam_time = $this->input->post('st_student_exam_time');

        for ($i=0; $i < count($this->input->post('st_course_id')); $i++) 
        {
            $this->db->where('exam_id', $this->input->post('st_course_id')[$i]); 
            $student_exam_answer = $this->db->get('student_exam_answer')->result();

            foreach ($student_exam_answer as $key => $exam_ans) 
            {
                $this->db->where('student_exam_answer_id', $exam_ans->student_exam_answer_id); 
                $this->db->delete('student_exam_answer');
            }


            $data = array(
                'st_course_is_exam_approve' => 1,
                'st_student_exam_date' => $exam_date,
                'st_student_exam_time' => $exam_time,
                'st_course_status' => 4
                );
            $this->db->where('st_course_id', $this->input->post('st_course_id')[$i]);
            $this->db->update('student_course', $data);
        }

        return true;
    }

    public function get_all_assign_exam_students($atp_id)
    {
        $this->db->select('a.*, b.user_name, b.user_reg_id, c.title, c.practical_exam_marks, c.viva_exam_marks');
        $this->db->from('student_course a');
        $this->db->join('users b', 'b.user_id = a.st_course_student_id', 'left');
        $this->db->join('course c', 'c.id = a.st_course_course_id', 'left');
        $this->db->where('a.st_course_student_atp_id', $atp_id);
        $this->db->where('a.st_course_status > ', 3);
        $this->db->where('a.st_course_is_exam_approve > ', 0);
        return $this->db->get()->result();
    }

    public function add_viva_prc_marks($id)
    {
        $data = array(
            'prc_obtained_marks' => $this->input->post('prc_obtained_marks'), 
            'viva_obtained_marks' => $this->input->post('viva_obtained_marks'), 
        );

        $this->db->where('st_course_id', $id);
        return $this->db->update('student_course', $data);
    }

    public function save_exam_answer()
    {
        $qno=$this->input->post('qno');
        $ans=$this->input->post('ans');
        $cid=$this->input->post('cid');
        $eid=$this->input->post('eid');
        $reg=$this->input->post('reg');
        $chkq=$this->db->query("select * from student_exam_answer where course_id=".$cid." and student_id=".$reg." and question_id=".$qno." ");
        
        if($chkq->num_rows() == 0)
        {
            $data=array(
                "student_id"        => $reg,
                "course_id"         => $cid,
                "question_id"       => $qno,
                "answer"            => $ans,
                "exam_id"           => $eid
            );            
            $this->db->insert('student_exam_answer', $data);            
        }else{

            $this->db->set('answer',$ans);            
            $this->db->where('student_id', $reg);
            $this->db->where('course_id', $cid);            
            $this->db->where('question_id', $qno);  
            $this->db->where('exam_id', $eid);  
            $this->db->update('student_exam_answer');                       
            
        }

        return true;
    }

    public function save_exam_answer_end_exam()
    {
        $qno=$this->input->post('qno');
        $ans=$this->input->post('ans');
        $cid=$this->input->post('cid');
        $eid=$this->input->post('eid');
        $reg=$this->input->post('reg');
        $chkq=$this->db->query("select * from student_exam_answer where course_id=".$cid." and student_id=".$reg." and question_id=".$qno." ");
        
        if($chkq->num_rows() == 0)
        {
            $data=array(
                "student_id"        => $reg,
                "course_id"         => $cid,
                "question_id"       => $qno,
                "answer"            => $ans,
                "exam_id"           => $eid
            );            
            $this->db->insert('student_exam_answer', $data);            
        }else{

            $this->db->set('answer',$ans);            
            $this->db->where('student_id', $reg);
            $this->db->where('course_id', $cid);            
            $this->db->where('question_id', $qno);  
            $this->db->where('exam_id', $eid);  
            $this->db->update('student_exam_answer');                       
            
        }

        $this->db->where('st_course_id', $eid);
        $this->db->update('student_course', ['st_course_is_exam_approve' => 2, 'st_course_status' => 5]);

        return true;
    }

    public function save_demo_exam_answer()
    {
        $qno=$this->input->post('qno');
        $ans=$this->input->post('ans');
        $cid=$this->input->post('cid');
        $eid=$this->input->post('eid');
        $reg=$this->input->post('reg');
        $chkq=$this->db->query("select * from student_demo_exam_answer where course_id=".$cid." and student_id=".$reg." and question_id=".$qno." and exam_id=".$eid." ");
        
        if($chkq->num_rows() == 0)
        {
            $data=array(
                "student_id"        => $reg,
                "course_id"         => $cid,
                "question_id"       => $qno,
                "answer"            => $ans,
                "exam_id"           => $eid
            );            
            $this->db->insert('student_demo_exam_answer', $data);            
        }else{

            $this->db->set('answer',$ans);            
            $this->db->where('student_id', $reg);
            $this->db->where('course_id', $cid);            
            $this->db->where('question_id', $qno);  
            $this->db->where('exam_id', $eid);  
            $this->db->update('student_demo_exam_answer');                       
            
        }

        return true;
    }

    public function save_demo_exam_answer_end_exam()
    {
        $qno=$this->input->post('qno');
        $ans=$this->input->post('ans');
        $cid=$this->input->post('cid');
        $eid=$this->input->post('eid');
        $reg=$this->input->post('reg');
        $chkq=$this->db->query("select * from student_demo_exam_answer where course_id=".$cid." and student_id=".$reg." and question_id=".$qno." and exam_id=".$eid." ");
        
        if($chkq->num_rows() == 0)
        {
            $data=array(
                "student_id"        => $reg,
                "course_id"         => $cid,
                "question_id"       => $qno,
                "answer"            => $ans,
                "exam_id"           => $eid
            );            
            $this->db->insert('student_demo_exam_answer', $data);            
        }else{

            $this->db->set('answer',$ans);            
            $this->db->where('student_id', $reg);
            $this->db->where('course_id', $cid);            
            $this->db->where('question_id', $qno);  
            $this->db->where('exam_id', $eid);  
            $this->db->update('student_demo_exam_answer');                       
            
        }
        return true;
    }

    public function get_all_student_for_print_certificate_and_marksheet($id)
    {
        // $this->db->select('a.*, b.user_name, b.user_gender, b.user_dob, b.user_photo, b.user_reg_id, c.*, d.title, d.id');
        // $this->db->from('student_exam_answer a');
        // $this->db->join('users b', 'b.user_id = a.student_id');
        // $this->db->join('student_course c', 'c.st_course_id = a.exam_id');
        // $this->db->join('course d', 'd.id = c.st_course_course_id');
        // $this->db->group_by('a.student_id');
        // $this->db->where('c.st_course_is_exam_approve', 2);
        // return $this->db->get()->result();

        $this->db->select('b.user_name, b.user_gender, b.user_dob, b.user_photo, b.user_reg_id, c.*, d.title, d.id');
        $this->db->from('student_course c');
        $this->db->where('c.st_course_student_atp_id', $id);
        $this->db->join('users b', 'b.user_id = c.st_course_student_id');
        $this->db->join('course d', 'd.id = c.st_course_course_id');
        $this->db->where('c.st_course_is_exam_approve', 2);
        return $this->db->get()->result();
    }

    public function get_all_student_for_current_exam($id)
    {
        // $this->db->select('a.*, b.user_name, b.user_gender, b.user_dob, b.user_photo, b.user_reg_id, c.*, d.title, d.id');
        // $this->db->from('student_exam_answer a');
        // $this->db->join('users b', 'b.user_id = a.student_id');
        // $this->db->join('student_course c', 'c.st_course_id = a.exam_id');
        // $this->db->join('course d', 'd.id = c.st_course_course_id');
        // $this->db->group_by('a.student_id');
        // $this->db->where('c.st_course_is_exam_approve', 2);
        // return $this->db->get()->result();

        $this->db->select('b.user_name, b.user_gender, b.user_dob, b.user_photo, b.user_reg_id, c.*, d.title, d.id');
        $this->db->from('student_course c');
        $this->db->join('users b', 'b.user_id = c.st_course_student_id');
        $this->db->join('course d', 'd.id = c.st_course_course_id');
        $this->db->where('c.st_course_status', 4);
        $this->db->where('c.st_course_student_atp_id', $id);
        return $this->db->get()->result();
    }

    public function get_all_student_for_hall_ticket_print($atp_id)
    {
        $this->db->select('a.*, b.user_id, b.user_name, b.user_gender, b.user_dob, b.user_photo, b.user_reg_id, d.title, d.id');
        $this->db->from('student_course a');
        $this->db->join('users b', 'b.user_id = a.st_course_student_id');
        $this->db->join('course d', 'd.id = a.st_course_course_id');
        $this->db->where('a.st_course_student_atp_id', $atp_id);
        // $this->db->where('a.st_course_course_id', $course_id);
        // $this->db->where('a.st_course_student_id', $user_id);
        $this->db->where('a.st_course_is_exam_approve > ', 0);
        return $this->db->get()->result();
    }

    public function add_question_paper()
    {
        $unique_id = rand();
        foreach ($this->input->post('paper_subject_id') as $key => $subject) 
        {
            $data = array(
                'paper_course_id'           => $this->input->post('paper_course_id'), 
                'paper_subject_id'          => $subject, 
                'paper_question_limit'      => $this->input->post('paper_question_limit')[$key], 
                'paper_unique_id'           => $unique_id,
                'paper_date_time'           => date('Y-m-d H:i:s')
            );
            $this->db->insert('question_paper', $data);
        }

        return true;
        
    }

    public function get_student_for_hall_ticket_print_by_reg_id($user_id, $course_id)
    {
        $this->db->select('a.*, b.user_id, b.user_name, b.user_gender, b.user_dob, b.user_photo, b.user_reg_id, d.title, d.id');
        $this->db->from('student_course a');
        $this->db->join('users b', 'b.user_id = a.st_course_student_id');
        $this->db->join('course d', 'd.id = a.st_course_course_id');
        $this->db->where('a.st_course_course_id', $course_id);
        $this->db->where('a.st_course_student_id', $user_id);
        $this->db->where('a.st_course_is_exam_approve > ', 0);
        return $this->db->get()->row();
    }
   
}
