<?php
defined('BASEPATH') OR exit('No direct script access allowed');
date_default_timezone_set('Asia/karachi');
class Test extends CI_Controller {

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


    public function index()
    {
       // $distance=900;
       //$tim=floor($distance / 60000 % 60);
       // echo $tim;
       // die();
        $data['userInfo']=$this->User_model->userInfo();
        $student_id = $this->User_model->userInfo()['student_id'];
        $user_img=@$this->db->select('img')
	                              ->from('student')
	                              ->where('id',$student_id)
	                              ->get()->row_array();
	   //echo "<pre>";print_r($user_img);exit;
	   $data['user_img']=$user_img;
        $data['cadet_id']=$student_id = $this->User_model->userInfo()['student_id'];
        
        $branch=$this->db->select('branch')
                         ->from('student')
                         ->where('id',$student_id)
                         ->get()->row_array()['branch'];
                         
        $class=$this->db->select('class_id')
                         ->from('promotion')
                         ->where('student_id',$student_id)
                         ->where('is_active',1)
                         ->where('is_delete',0)
                         ->get()->row_array()['class_id'];
       
        $data['current_date']=$current_date=date('Y-m-d');
        
        $data['test_list']=$this->db->select('paper_history.id,paper_history.dateofpaper,(mcq+truefalse) as mcq,paper_history.totalmarks,paper_history.time,subject.name')
                                    ->from('paper_history')
                                    ->join('subject','subject.id=paper_history.subject')
                                    ->where('paper_history.class',$class)
                                    ->where('paper_history.branch',$branch)
                                    ->where('paper_history.is_delete',0)
                                    ->where('dateofpaper>=',$current_date)
                                    ->where('paper_history.is_publish',1)
                                    ->get()->result_array();
                                    // echo $this->db->last_query();
                                    // exit();
        // var_dump('<pre>',$data['test_list']);
        // die();
        $data['main'] = "Test/Test_list";
        $this->load->view('includes/main', $data);
    }
    
    public function result_list()
    {
        redirect('Test/index');
        $data['userInfo']=$this->User_model->userInfo();
        $data['cadet_id']=$student_id = $this->User_model->userInfo()['student_id'];
        
        $data['result_list']=$this->db->select('paper_history.id,paper_history.dateofpaper,(mcq+truefalse) as mcq,,paper_history.totalmarks,paper_history.mcq_marks,subject.name')
                                    ->from('test_log')
                                    ->join('paper_history','test_log.test_id=paper_history.id')
                                    ->join('subject','subject.id=paper_history.subject')
                                    ->where('paper_history.is_delete',0)
                                    ->where('test_log.cadet_id',$student_id)
                                    ->group_by('test_log.test_id')
                                    ->get()->result_array();
        $data['main'] = "Test/result_list";
        $this->load->view('includes/main', $data);
    }
    
    public function get_start_time()
    {
       $cadet_id=$_POST['cadet_id'];
       $test_id=$_POST['test_id'];
       
        $date_time=$this->db->select('date_time')
                       ->from('test_log')
                       ->where('cadet_id',$cadet_id)
                       ->where('test_id',$test_id)
                       ->get()->row_array()['date_time'];
                       echo $date_time; 
    }
    
    public function get_time()
    {
        
        $test_id=$_POST['test_id'];
        $cadet_id=$_POST['cadet_id'];
        
        $time=$this->db->select('time')
                       ->from('paper_history')
                       ->where('id',$test_id)
                       ->get()->row_array()['time'];
        
        $date_time=$this->db->select('date_time')
                       ->from('test_log')
                       ->where('cadet_id',$cadet_id)
                       ->where('test_id',$test_id)
                       ->get()->row_array()['date_time'];
        $date =strtotime($date_time);
        $p_time=$time*60;

        $calculated_time=$date+$p_time;
       // die(); 
        $cur_date=date('Y-m-d H:i:s');
        $date2 =strtotime($cur_date);
     
        $tim=$calculated_time-$date2;
        if($tim>0)
        {
          echo gmdate("i:s", $tim);  
        }else{
            echo "0:00";
            
        }
        
       // die();
      // echo $timer=round(($tim/60),2);
    }
    
    public function update_status()
    {
        $student_id = $this->User_model->userInfo()['student_id']; 
        $test=$_POST['test_id'];
        $data=array(
            'test_id'=>$test,
            'cadet_id'=>$student_id,
            'date_time'=>date('Y-m-d H:i:s')
            );
        $res=$this->db->insert('test_log',$data);
        if($res)
        {
           echo 1; 
        }else{
           echo 0;
        }
        
    }
    public function num_rows($test)
        {
            $q_ids=$this->db->select('question_id')
                        ->from('paper_questions')
                        ->where('paper_id',$test)
                        ->get();
             return $q_ids->num_rows();
            
        }
    
    public function fetch($limit,$offset,$test)
    {
        $q_ids=$this->db->select('question_id,is_mcq')
                        ->from('paper_questions')
                        ->where('paper_id',$test)
                        ->limit($limit,$offset)
                        ->get()->result_array();

                        
        $query_print=array();
        if(!empty($q_ids))
        {
            foreach($q_ids as $row)
            {
                if($row['is_mcq']==0){
                    $query_print[]=$this->db->select('*')
                                        ->from('mcq_questions')
                                        ->where('mcq_questions.id', $row['question_id'])
                                        ->get()->row_array(); 
                    $query_print['0']['is_mcq']=0;                    
                }else{
                    $query_print[]=$this->db->select('*')
                                        ->from('general_questions')
                                        ->where('general_questions.id', $row['question_id'])
                                        ->get()->row_array();
                    $query_print['0']['is_mcq']=1;                    
                }
                
            }
        }
        
        return $query_print;
    }
    
    public function start_test()
    {
        $data['userInfo']=$this->User_model->userInfo();
        $data['cadet_id']=$student_id = $this->User_model->userInfo()['student_id']; 
       // $test=$_GET['id']; 
        if($this->input->post())
        {
            $test=$this->input->post('paper_id');
            $question_id=$this->input->post('question_id');
            $mcq_validator=$this->input->post('mcq_validator');
             
            if(!empty($question_id))
            {
                
                $cadet_id=$this->input->post('cadet_id');
                $check_duplication=$this->db->select('id')
                                            ->from('paper_result')
                                            ->where('question_no',$question_id)
                                            ->where('test_id',$test)
                                            ->where('cadet_id',$cadet_id)
                                            ->get()->row_array()['id'];
                if(empty($check_duplication)) 
                {
                        $answer=$this->input->post('optradio');
                        $status="";
                        $check_answer="";
                        
                        // if($mcq_validator=="mcq")
                        // {
                        //     $check_answer=$this->db->select('correct_answer')
                        //                       ->from('mcq_questions')
                        //                       ->where('id',$question_id)
                        //                       ->where('is_delete',0)
                        //                       ->get()->row_array()['correct_answer'];
                        // }else{
                        //     $check_answer=$this->db->select('false_words')
                        //                       ->from('general_questions')
                        //                       ->where('id',$question_id)
                        //                       ->where('is_delete',0)
                        //                       ->get()->row_array()['false_words'];
                        // }
                        
                        
                        // if(!empty($check_answer))
                        // {
                        //   if($answer==$check_answer) 
                        //   {
                        //       $status="true";
                        //   }else{
                        //       $status="false";
                        //   }
                        // }
                        
                        $data_result=array(
                            'test_id'=>$test,
                            'question_no'=>$question_id,
                            'cadet_id'=>$cadet_id,
                            'answer'=>$answer,
                            'actual_answer'=>$check_answer,
                            'status'=>$status
                            );
                        $this->db->insert('paper_result',$data_result);
                }
                
               
            }
            
       
            $this->session->set_userdata('paper_id',$test);
        }else{
           $test=$this->session->userdata('paper_id');

        }
         $config=[
            'base_url'=>base_url('Test/start_test'),
            'per_page'=>1,
            'total_rows'=>$this->num_rows($test),
           // 'suffix' => '?' . http_build_query($_GET, '', "&"),
            'full_tag_open'=>"<ul class='pagination'>",
            'full_tag_close'=>"</ul>",
            'last_link'=>'Last',
            'first_link'=>'first',
            'first_tag_open'=>'<li>',
            'first_tag_close'=>'</li>',
            'last_tag_open'=>'<li>',
            'last_tag_close'=>'</li>',
            'next_tag_open'=>'<li>',
            'next_tag_close'=>'</li>',
            'prev_tag_open'=>'<li>',
            'prev_tag_close'=>'</li>',
            'num_tag_open'=>'<li>',
            'num_tag_close'=>'</li>',
            'cur_tag_open'=>"<li class='active'><a>",
            'cur_tag_close'=>'</a></li>',
         ];
         
         $this->pagination->initialize($config);
         $offset=$this->uri->segment(3);
        if(empty($offset)){
            $offset=0;
        }
        
        $limit=$config['per_page'];
        $data['paper']= $this->fetch($limit,$this->uri->segment(3),$test);
        
        $data['student_name']=$this->db->select('student_name')
                                       ->from('student')
                                       ->where('id',$student_id)
                                       ->get()->row_array()['student_name'];
                                       
        $data['paper_history']=$this->db->select('paper_history.time,subject.name,class.class_name,paper_history.id')
                                        ->from('paper_history')
                                        ->join('subject','subject.id=paper_history.subject')
                                        ->join('class','class.class_id=paper_history.class')
                                        ->where('paper_history.id',$test)
                                        ->get()->row_array();
        

        
        $data['main'] = "Test/paper";
        $this->load->view('includes/main', $data);
    }
    
   public function print_paper_sheet($id)
    {
      $branch=$this->user_model->getBranch();
      $data['data']=$data=$this->db->select('*')->from('paper_history')->where('id',$id)->get()->row_array();
      
      $class=$data['class'];
      
   	  
                                 
   	                                  
      $data['cadet_id']=$student_id = $this->User_model->userInfo()['student_id']; 
      $data['sid']=$this->db->select('sid')
   	                        ->from('student')
   	                        ->where('id',$student_id)
   	                        ->get()->row_array()['sid'];
      $auto_checker=0;
   	  $auto_checker=$this->db->select('count(id) as ids')
   	                        ->from('paper_result')
   	                        ->where('test_id',$id)
   	                        ->where('cadet_id',$student_id)
   	                        ->where('status',"true")
   	                        ->where('is_delete',0)
   	                        ->get()->row_array()['ids'];
      $data['auto_checker']=$auto_checker*$data['mcq_marks'];                 		                        
      $subject=$data['subject'];
      
          $data['b_header']=$this->db->query("SELECT * FROM `branch` WHERE is_delete='0' AND id='$branch'")->row_array();

          $data['classes']=$this->db->select('class_name')
                                    ->FROM('class')
                                    ->WHERE('class.is_delete',0)
                                    ->where('class_id',$class)
                                    ->get()->row_array()['class_name'];

          $data['subject']= $this->db->query("SELECT name FROM `subject`  WHERE subject.is_deleted='0'  AND id=$subject")->row_array();
          $this->load->view('Test/paper_print',$data);
    }

}

/* End of file datesheet.php */
/* Location: ./application/controllers/datesheet.php */