<?php
/*
* Generated by CRUDigniter v3.2
* www.crudigniter.com
*/
class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
/*
* Get user by id
*/
function get_user($id)
{
return $this->db->get_where('users',array('id'=>$id))->row_array();
}
function get_user_interim($id)
{
$user = $this->db->get_where('users',array('id'=>$id))->row_array();
return $this->db->get_where('users',array('id'=> $user["interim_id"]))->row_array();
}
function get_user_responsable($id)
{
$user = $this->db->get_where('users',array('id'=>$id))->row_array();
return $this->db->get_where('users',array('id'=> $user["responsable_id"]))->row_array();
}
/*
* Verifier si le responsable est présent(e)
*/
function isResponsableOut($idresponsable){
$conge = $this->db->query("select id from conge where users_id='".$idresponsable."' and STR_TO_DATE('".date("Y-m-d")."', '%Y-%m-%d')>=datedebut and STR_TO_DATE('".date("Y-m-d")."', '%Y-%m-%d ')<=datefin and etat=1")->num_rows()>0;
$mission = $this->db->query("select id from mission where users_id='".$idresponsable."' and STR_TO_DATE('".date("Y-m-d")."', '%Y-%m-%d')>=datedebut and STR_TO_DATE('".date("Y-m-d")."', '%Y-%m-%d ')<=datefin and etat=1")->num_rows()>0;
return $conge || $mission;
}
/*
* Verifier si l'utilisateur est présent(e)
*/
function isUserOut($iduser,$datedebut,$datefin=null){
$conge = $this->db->query("select id from conge where users_id='".$iduser."' and ((( STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')>= cast(concat(datedebut, ' ', tempdebut) as datetime) and STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')<=cast(concat(datefin, ' ', tempfin) as datetime)) or (STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')>=cast(concat(datedebut, ' ', tempdebut) as datetime) and STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')<=cast(concat(datefin, ' ', tempfin) as datetime) ) ) or (NOT (cast(concat(datefin, ' ', tempfin) as datetime) <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or cast(concat(datefin, ' ', tempfin) as datetime) >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T '))) )and etat=1")->num_rows()>0;
$autorisation = $this->db->query("select id from autorisation where users_id='".$iduser."' and (datedemande=STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T') or (NOT (cast(concat(datedemande, ' ', heursfin) as datetime)) <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or cast(concat(datedemande, ' ', heursdebut) as datetime) >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T '))) and etat=1")->num_rows()>0;
$mission = $this->db->query("select id from mission where users_id='".$iduser."' and ((( STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')>=datedebut and STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')<=datefin) or (STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')>=datedebut and STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')<=datefin ) ) or (NOT (datefin <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or datefin >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')))) and etat=1")->num_rows()>0;
return $conge || $mission || $autorisation;
}
/*
* Compter le nombre des demande non confirmer pour un utilisateur
*/
function HasDemande($iduser,$datedebut,$datefin=null){
$conge = $this->db->query("select id from conge where users_id='".$iduser."' and ((( STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')>= cast(concat(datedebut, ' ', tempdebut) as datetime) and STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')<=cast(concat(datefin, ' ', tempfin) as datetime)) or (STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')>=cast(concat(datedebut, ' ', tempdebut) as datetime) and STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')<=cast(concat(datefin, ' ', tempfin) as datetime) ) ) or (NOT (cast(concat(datefin, ' ', tempfin) as datetime)) <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or cast(concat(datefin, ' ', tempfin) as datetime) >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')) ) and etat=0")->num_rows();
$autorisation = $this->db->query("select id from autorisation where users_id='".$iduser."' and (datedemande=STR_TO_DATE('".$datedebut."', '%Y-%m-%d') or (NOT (cast(concat(datedemande, ' ', heursfin) as datetime)) <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or cast(concat(datedemande, ' ', heursdebut) as datetime) >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T '))) and etat=0")->num_rows();
$mission = $this->db->query("select id from mission where users_id='".$iduser."' and ((( STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')>=datedebut and STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')<=datefin) or (STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')>=datedebut and STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')<=datefin ) ) or (NOT (datefin <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or datefin >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')))) and etat=0")->num_rows();
return ($conge + $mission + $autorisation);
}
/* function isUserOut($iduser,$datedebut,$datefin=null){
$conge = $this->db->query("select id from conge where users_id='".$iduser."' and (( STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')>= cast(concat(datedebut, ' ', tempdebut) as datetime) and STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')<=cast(concat(datefin, ' ', tempfin) as datetime)) or (STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')>=cast(concat(datedebut, ' ', tempdebut) as datetime) and STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')<=cast(concat(datefin, ' ', tempfin) as datetime) ) ) or (NOT (cast(concat(datefin, ' ', tempfin) as datetime)) <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or cast(concat(datefin, ' ', tempfin) as datetime) >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')) and etat=1")->num_rows()>0;
$autorisation = $this->db->query("select id from autorisation where users_id='".$iduser."' and datedemande=STR_TO_DATE('".$datedebut."', '%Y-%m-%d') or (NOT (cast(concat(datedemande, ' ', heursfin) as datetime)) <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or cast(concat(datedemande, ' ', heursdebut) as datetime) >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T ')) and etat=1")->num_rows()>0;
$mission = $this->db->query("select id from mission where users_id='".$iduser."' and (( STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')>=datedebut and STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T')<=datefin) or (STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')>=datedebut and STR_TO_DATE('".$datefin."', '%Y-%m-%d %T')<=datefin ) ) or (NOT (datefin <= STR_TO_DATE('".$datedebut."', '%Y-%m-%d %T ') or datefin >= STR_TO_DATE('".$datefin."', '%Y-%m-%d %T '))) and etat=1")->num_rows()>0;
return $conge || $mission || $autorisation;
}*/
/*
* Get all users count
*/
function get_all_users_count()
{
$this->db->from('users');
return $this->db->count_all_results();
}
/*
* Get all users
*/
function get_all_users($params = array())
{
if(isset($params) && !empty($params))
{
$this->db->limit($params['limit'], $params['offset']);
}
$this->db->select("*,user.id as idUtilisateur, concat(user.nom,' ',user.prenom) as utilisateur ,(select concat(nom,' ',prenom) from users where id =user.responsable_id) as responsable, ,(select concat(nom,' ',prenom) from users where id =user.interim_id) as interim");
$this->db->join('affectation', 'user.id = affectation.users_id');
$this->db->from('users user');
$this->db->order_by('user.type', 'ASC');
$query = $this->db->get()->result_array();
return $query;
}
/*
* recuperer tous les utilisateur sous un responsable avec l'id de responsable
*/
function get_all_users_for_responsable($id)
{
$this->db->where("responsable_id",$id);
$this->db->select("*");
$this->db->from('users user');
$query = $this->db->get()->result_array();
return $query;
}
/*
* recuperer tous les utilisateur dans le méme service
*/
function get_all_users_same_service($id)
{
$this->db->where("affectation.service_id",$id);
$this->db->select("*, users.id as iduser");
$this->db->join('affectation','affectation.users_id = users.id');
$this->db->from('users');
$query = $this->db->get()->result_array();
return $query;
}
/*
* function to add new user
*/
function add_user($params)
{
$this->db->insert('users',$params);
return $this->db->insert_id();
}
/*
* function to update user
*/
function update_user($id,$params)
{
$this->db->where('id',$id);
return $this->db->update('users',$params);
}
/*
* function to delete user
*/
function delete_user($id)
{
return $this->db->delete('users',array('id'=>$id));
}
function connect($user,$pass){
$this->db->select('*,users.id as iduser, affectation.id as idaffectation, service.libelle as equipe , service.id as idequipe, societe.nom as entreprise,societe.id as iddepart, users.nom as name, fonction.libelle as poste');
$this->db->from('users');
$this->db->join('affectation', 'affectation.users_id = users.id');
$this->db->join('contrat', 'contrat.users_id = users.id');
$this->db->join('fonction', 'fonction.id = affectation.fonction_id');
$this->db->join('societe', 'societe.id = affectation.societe_id');
$this->db->join('service', 'service.id = affectation.service_id');
$this->db->where('users.login', $user);
$this->db->where('users.password', $pass);
$this->db->where('users.active', 1);
$query = $this->db->get()->result_array();
return $query;
}
function connectMail($email){
$this->db->select('*,users.id as iduser, affectation.id as idaffectation, service.libelle as equipe , service.id as idequipe, societe.nom as entreprise,societe.id as iddepart, users.nom as name, fonction.libelle as poste');
$this->db->from('users');
$this->db->join('affectation', 'affectation.users_id = users.id');
$this->db->join('contrat', 'contrat.users_id = users.id');
$this->db->join('fonction', 'fonction.id = affectation.fonction_id');
$this->db->join('societe', 'societe.id = affectation.societe_id');
$this->db->join('service', 'service.id = affectation.service_id');
$this->db->where('md5(users.email)', $email);
$query = $this->db->get()->result_array();
return $query;
}
function getForList(){
return $this->db->query("select id, concat(nom,' ',prenom) as text from users")->result();
}
function getForListResponsable(){
$user =$this->session->userdata("auth");
return $this->db->query("select id, concat(nom,' ',prenom) as text from users where responsable_id='".$user["iduser"]."'")->result();
}
}