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

class Model_recognition extends CI_Model {
	public function fetchRecognition($staff_id = null, $latest = false, $profiling_id = null)
	{

		//inner query 
		$this->db->select('MAX(a.profiling_id)');
		$this->db->from('user__recognition a');
		if($staff_id) $this->db->where('a.user_id',$staff_id);
		$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,
							c.action_efective_date,
							d.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);

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

		$query = $this->db->get('user__recognition a');

		return ($query->num_rows() > 0)? $query->result() : null;
	}

	public function createRecognition($input)
	{
		$this->db->insert('user__recognition',$input);
		return $this->db->insert_id();
	}

	public function editRecognition($input,$id)
	{
		$this->db->set($input);
		$this->db->where('id',$id);
		$this->db->update('user__recognition');
	}
}
?>