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

class Start extends CI_Controller {	 
	public function __construct() {
        parent::__construct();
		if($this->session->userdata('students')['Logged_In']){
			if($this->session->userdata('students')['TestStatus']==1){
				redirect(BASE_URL .'students');
			}
			$this->load->model('mapmycareer/Custom_model','model');
			
			
		}else{
			redirect(BASE_URL);
		}
    }
	
	public function index(){
	/* 	$sid=$this->session->userdata('students')['StudentId'];
		$testId=$this->session->userdata('students')['TestId'];
		$test_types=$this->model->get_ext_test_type($testId);
		
		$allQuestion=0;
		$types_id=0;
		$lastId=0;
		foreach($test_types as $types){
			$types_id=$types['id'];
			$TypeText = $types['TestTypeName'];
			$total_qus=$this->model->student_question_total($sid,$testId,$types_id);
			$allQuestion =$allQuestion + $total_qus;
			if($total_qus<48){
				$lastId = $this->model->get_student_last_question($sid,$testId,$types_id);
				break;
			}
		}
		
		if($allQuestion>95){
			redirect(BASE_URL .'start/interest');
		}
		
		if($types_id!=0){
			$data['TestID'] = $testId;
			$data['StudentId'] = $sid;
			$data['TypeID'] = $types_id;
			$data['LastID'] = $lastId;
			$data['Questions']=$this->get_question_html($testId,$types_id,$lastId);
			$data['TypeText'] = $TypeText;
			$data['TestTypes']=$test_types;		
			$this->load->view('mapmycareer/includes/mapmycareer_header',$data);
			$this->load->view('mapmycareer/student_test_view');
			$this->load->view('mapmycareer/includes/mapmycareer_footer');
		}else{			
			redirect(BASE_URL .'students');
		} */
					redirect(BASE_URL .'students');	
	}
	
	function aptitude(){
		 
		$sid=$this->session->userdata('students')['StudentId'];
		$testId=$this->session->userdata('students')['TestId'];
		$test_types=$this->model->get_ext_test_type($testId);
		
		 
		$types_id=0;
		$lastId=0;
		 
		$types_id=$test_types[0]['id'];
		$TypeText = $test_types[0]['TestTypeName'];
		$total_qus=$this->model->student_question_total($sid,$testId,$types_id);
		
		if($total_qus>45){
			redirect(BASE_URL .'start/thanks/1/aptitude');
		}
		
		if($total_qus<48){
			$lastId = $this->model->get_student_last_question($sid,$testId,$types_id);
			 
		}
 
		
		if($types_id!=0){
			$data['URL_key'] ="aptitude";
			$data['TestID'] = $testId;
			$data['StudentId'] = $sid;
			$data['TypeID'] = $types_id;
			$data['LastID'] = $lastId;
			$data['Questions']=$this->get_question_html($testId,$types_id,$lastId);
			$data['TypeText'] = $TypeText;
			$data['TestTypes']=$test_types;		
			$this->load->view('mapmycareer/includes/mapmycareer_header',$data);
			$this->load->view('mapmycareer/student_test_view');
			$this->load->view('mapmycareer/includes/mapmycareer_footer');
		}else{			
			redirect(BASE_URL .'students');
		}
	}
	
	function personality(){
		
		$sid=$this->session->userdata('students')['StudentId'];
		$testId=$this->session->userdata('students')['TestId'];
		$test_types=$this->model->get_ext_test_type($testId);
				 
		$types_id=0;
		$lastId=0;
		 
		$types_id=$test_types[1]['id'];
		$TypeText = $test_types[1]['TestTypeName'];
		$total_qus=$this->model->student_question_total($sid,$testId,$types_id);
		
		if($total_qus>45){
			redirect(BASE_URL .'start/thanks/2/personality');
		}
		
		if($total_qus<48){
			$lastId = $this->model->get_student_last_question($sid,$testId,$types_id);
			 
		}
		if($types_id!=0){
			$data['URL_key'] ="personality";
			$data['TestID'] = $testId;
			$data['StudentId'] = $sid;
			$data['TypeID'] = $types_id;
			$data['LastID'] = $lastId;
			$data['Questions']=$this->get_question_html($testId,$types_id,$lastId);
			$data['TypeText'] = $TypeText;
			$data['TestTypes']=$test_types;		
			$this->load->view('mapmycareer/includes/mapmycareer_header',$data);
			$this->load->view('mapmycareer/student_test_view');
			$this->load->view('mapmycareer/includes/mapmycareer_footer');
		}else{			
			redirect(BASE_URL .'students');
		}
	}
	
	function instruction($type){
		if($type!=""){
			$data['Type']=$type;
			$this->load->view('mapmycareer/includes/mapmycareer_header', $data);
			if($this->session->userdata('students')['Language']==1){
				$this->load->view('mapmycareer/student_instruction_english_view');
			}else{
				$this->load->view('mapmycareer/student_instruction_hindi_view');
			}
			$this->load->view('mapmycareer/includes/mapmycareer_footer');
		}else{
			redirect(BASE_URL .'students');
		 				
		}
	}
	
	function get_questions(){
		$testId = $this->input->post('TestId');
		$typeId = $this->input->post('TypeId');
		$lastId = $this->input->post('LastId');
		$data_question = $this->get_question_html($testId,$typeId,$lastId);
	}
	
	function get_question_html($testId,$typeId,$lastId){
		$language=$this->session->userdata('students')['Language'];
		if($language==1){
			$this->lang->load('information','english');
		}else{
			$this->lang->load('information','hindi');
		}
		//var_dump($testId);var_dump($typeId);var_dump($lastId);die;
		$result_text='';
		$results = $this->model->get_six_question($testId,$typeId,$lastId);
		$answer_type=$this->model->get_ext_test_answer_type($testId,$typeId);
		
		if($answer_type==1){
			$result_text = '<table  id="Online_question_div" border="1px"><tr><th style="width:5%;">Q.NO</th><th style="width:50%;">'.$this->lang->line('msg_question').'</th><th style="width:10%;">'.$this->lang->line('msg_yes').'</th><th style="width:10%;">'.$this->lang->line('msg_most').'</th><th style="width:10%;">'.$this->lang->line('msg_sure').'</th><th style="width:10%;">'.$this->lang->line('msg_rarely').'</th><th style="width:10%;">'.$this->lang->line('msg_no').'</th></tr>';
			foreach($results as $row){
				$q_ans='length#'.$row['QuestionId'];
				if($language==1){
					$question=$row["QuestionText"];
				}else{
					$question=$row["QuestionTextHindi"];
				}
				$result_text .='<tr><td>'.$row['QuestionId'].'</td><td class="que_right">'.$question.'</td><td><div class="form-check "><label class="form-check-label radio"><input class="form-check-input hidden" type="radio" value="5" name="'.$q_ans.'"><span class="label"></span></div></div></td><td><div class="form-check "><label class="form-check-label radio"><input class="form-check-input hidden" type="radio" value="4" name="'.$q_ans.'"><span class="label"></span></div></div></td><td><div class="form-check "><label class="form-check-label radio"><input class="form-check-input hidden" type="radio" value="3" name="'.$q_ans.'"><span class="label"></span></div></div></td><td><div class="form-check "><label class="form-check-label radio"><input class="form-check-input hidden" type="radio" value="2" name="'.$q_ans.'"><span class="label"></span></div></div></td><td><div class="form-check "><label class="form-check-label radio"><input class="form-check-input hidden" type="radio" value="1" name="'.$q_ans.'"><span class="label"></span></div></div></td></tr>';
				 
			}
			$result_text .='</table>';
		}
		if($answer_type==2){
			$result_text = '<div class="row"><div class="col-md-10 col-md-push-1"><table id="question_second" border="1px"><tr><th style="width:5%;">Q.NO</th><th style="width:50%;">'.$this->lang->line("msg_question").'</th><th style="width:5%;"></th></tr>';
			 
			foreach($results as $row){
				 
				$q_ans='length#'.$row['QuestionId'];
				if($language==1){
					$question=$row["QuestionText"];
				}else{
					$question=$row["QuestionTextHindi"];
				}
				 
				$json_text=json_decode($question);
				//var_dump($json_text);die;
				$result_text .='<tr><td rowspan="2">'.$row['QuestionId'].'</td><td class="que_right">'.$json_text->a.'</td><td><div class="form-check "><label class="form-check-label radio"><input type="radio" value="5" class="form-check-input hidden" name="'.$q_ans.'"><span class="label"></span></div></div></td></tr><tr><td class="que_right">'.$json_text->b.'</td><td><div class="form-check "><label class="form-check-label radio"><input class="form-check-input hidden" type="radio" value="1" name="'.$q_ans.'"><span class="label"></span></div></div></td></tr>';				 
			}
			
			$result_text .='</table></div></div>';
		}
		
		return $result_text;
	}
	
	/* <div class="form-check ">
				  <label class="form-check-label radio">
					<input data-qid="1" class="form-check-input mobile_radio_chk hidden" type="radio" name="mobile_radio_chk_1" value="3">
					<span class="label"></span><span class="checkbox-text">3-5 Years</span>
				  </label>
				</div> */
	/* SELECT `id`, `StudentId`, `TestId`, `TestTypeId`, `QuestionId`, `QuestionMarks`, `IsActive`, `MarksDate` FROM `tblstudent_marks` WHERE 1 */
	function action(){
		 
		$lastId = $this->input->post('LastID');
		$StudentId = $this->input->post('StudentId');
		$TestId = $this->input->post('TestID');
		$TestTypeId = $this->input->post('TypeID');
		$URL_key = $this->input->post('URL_key');
		$toltal=$lastId+6;
		$j=$lastId+1;
		$k=0;
		$array_of=array();
		for($i=$j;$i<=$toltal;$i++){			
			if($this->input->post('length#'.$i)){
				$marks = $this->input->post('length#'.$i);
				$ins_array=array(
					'StudentId' => $StudentId,
					'TestId' => $TestId,
					'TestTypeId' => $TestTypeId,
					'QuestionId' =>$i,
					'QuestionMarks' =>$marks
				);
				$k++;
				$array_of[] = $ins_array;				
			}else{
				break;
			}			 
		}
		
		
		if($k>=6){
			$this->db->insert_batch('tblstudent_marks',$array_of);
		}
		 
		redirect(BASE_URL.'start/'.$URL_key);
	}
	
	
	function interest(){
		$language=$this->session->userdata('students')['Language'];
		if($language==1){
			$this->lang->load('information','english');
		}else{
			$this->lang->load('information','hindi');
		}
		$testId=$this->session->userdata('students')['TestId'];
		$data['interests'] = $this->model->get_careers_list($testId);
		$data['TestID'] = $testId;
		$this->load->view('mapmycareer/includes/mapmycareer_header',$data);
		$this->load->view('mapmycareer/student_intrest_view');
		$this->load->view('mapmycareer/includes/mapmycareer_footer');
	}
	
	function set_interest(){
		if($this->input->post()){
			$array_ins=$this->input->post('InterestChk');
			$testId=$this->input->post('TestID');
			$sid=$this->session->userdata('students')['StudentId'];
			foreach($array_ins as $ins){
				$ins_array=array(
					'StudentId' => $sid,
					'TestId' => $testId,
					'VocationalCareerId' => $ins					 
				); 
				
				$this->db->insert('tblstudent_career_interest_mapping',$ins_array);
			}
			
			redirect(BASE_URL .'start/thanks/3/interest');			
			 
		}
		redirect(BASE_URL.'students');
	}
	
	function thanks($type,$key){
		
		$userId = $this->session->userdata('students')['StudentId'];
		if($type==1){
			$up_data=array(
				'AptitudeStatus'=>1
			);
		}
		
		if($type==2){
			$up_data=array(
				'PersonalityStatus'=>1
			);
		}
		
		if($type==3){
			$up_data=array(
				'InterestStatus'=>1
			);
		}
		
		$this->db->where('StudentId',$userId);
		$this->db->update('tblstudents_test_status',$up_data);
		redirect(BASE_URL.'students');
		
		 
	}
	 
}