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

class Model_user_pursuing_study extends CI_Model {

	public function get_pursuing_study($staff_id = 0, $latest = true, $profiling_id = null, $pursuing_study_id = null)
	{
//inner query
        $this->db->select('MAX(profiling_id)');
        $this->db->from('user__pursuing_study ');
        if($staff_id) $this->db->where('user_id',$staff_id);
        $this->db->where('is_delete',0);
        $max = $this->db->get_compiled_select();

//outer query
        $this->db->select('a.*,
        	b.id as attachment_id,b.attachment_name,b.location,b.file_type,b.file_ext,b.mask_id,
                        d.action_efective_date,
                        e.type as action_type');

        if($staff_id) $this->db->where('a.user_id',$staff_id);
        if($latest) $this->db->where("a.profiling_id IN ($max)",'',false);
        if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);
        if($pursuing_study_id) $this->db->where('a.id',$pursuing_study_id);

        $this->db->where('a.is_delete',0);
        $this->db->join('user__pursuing_study_attachment b','b.professional_academic_id = a.id and b.is_delete = 0','left');
        $this->db->join('user__profiling d','d.id = a.profiling_id','left');
        $this->db->join('_ref__action_type e','e.id = d.action_type_id','left');

        $query = $this->db->get('user__pursuing_study a');
        
        return (($query) && $query->num_rows() > 0)?$query->result():null; 
	}

	public function createPursuingStudy($input)
	{
		$data['profiling_id'] 		= $input['profiling_id'];
		$data['user_id'] 			= $input['user_id'];
		$data['date_start'] 		= $input['date_start'];
		$data['date_end'] 			= $input['date_end'];
		$data['institution'] 		= $input['institution'];
		$data['program'] 			= $input['program'];
		$data['progress_paper']		= $input['progress_paper'];
		$data['status']				= $input['status'];

		$this->db->insert('user__pursuing_study',$data);

		return $this->db->insert_id();
	}

	public function editPursuingStudy($input, $pursuing_study_id = null, $profiling_id = null)
	{
        if($profiling_id) $this->db->where('profiling_id',$profiling_id);
		$this->db->set($input);
		$this->db->where('id',$pursuing_study_id);
		$this->db->update('user__pursuing_study');

		return true;
	}
}
?>