You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
6.6 KiB

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event_concurrent_process_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function lock_or_add_process($user_id, $login_id, $process_type, $event_id){
// Check if process_exist for non paid event
if ($this->input->post('event_category') && $this->input->post('event_category') != 'PAID_EVENT') {
if($this->check_process($user_id, $process_type, $event_id) > 0){
return array("status"=>false);
} else {
return $this->add_process($user_id, $login_id, $process_type, $event_id);
}
}
return array("status"=> true);
}
public function add_process($user_id, $login_id, $process_type, $event_id){
$this->db->query("INSERT INTO
event_concurrent_process(login_id, process_reference, process_type, process_expiration, process_status)
VALUES(?, ?, ?, NOW(), 1)", array(
$login_id,
$event_id,
$process_type));
return array("status"=>true, "process_id" => $this->db->insert_id());
}
public function update_process($user_id, $login_id, $event_id, $process_type, $process_id){
$this->db->query("UPDATE event_concurrent_process
SET process_expiration = NOW()
WHERE process_type = ?
AND process_reference = ?
AND login_id = ?
AND process_id = ?
AND process_status = 1",array(
$process_type,
$event_id,
$login_id,
$process_id));
return $this->db->affected_rows();
}
public function ma_update_process($update_process){
if($update_process) {
foreach($update_process as $value){
$this->db->query("UPDATE event_concurrent_process
SET process_expiration = NOW()
WHERE process_type = ?
AND login_id = ?
AND process_id = ?
AND process_status = 1",array(
$value['process_type'],
$value['login_id'],
$value['process_id']));
}
}
return true;
}
public function check_process_id_validity($login_id, $process_id, $process_type, $event_id){
$this->db->select("process_id");
$this->db->from("event_concurrent_process");
$this->db->where("process_reference", $event_id);
$this->db->where("login_id", $login_id);
$this->db->where("process_id", $process_id);
$this->db->where("process_type", $process_type);
$this->db->where("process_status", 1);
$this->db->order_by("process_id", "desc");
$this->db->limit(1);
return $this->db->get()->num_rows();
}
private function check_process($user_id, $process_type, $event_id){
$this->db->select("ecp.process_id");
$this->db->from("event_concurrent_process ecp");
$this->db->join("user_login_history ulh", "ulh.history_id = ecp.login_id", "left");
$this->db->where("process_reference", $event_id);
$this->db->where("process_type", $process_type);
$this->db->where("ulh.user_id", $user_id);
$this->db->where("ecp.process_status", 1);
$this->db->order_by("ecp.process_id", "desc");
$this->db->limit(1);
return $this->db->get()->num_rows();
}
private function _query_activity($user_id,$login_id,$process_type, $event_id=null){
$where = ($event_id!=null)?" AND (ecp.login_id != '".$this->db->escape_str($login_id)."') AND ecp.process_reference='".$this->db->escape_str($event_id)."'"
:"AND (ecp.login_id != '".$this->db->escape_str($login_id)."' OR ecp.login_id = '".$this->db->escape_str($login_id)."')";
return "SELECT ecp.*, ulh.user_id, ulh.status
FROM event_concurrent_process ecp
LEFT JOIN user_login_history ulh
ON ulh.history_id = ecp.login_id
WHERE ulh.user_id = '".$this->db->escape_str($user_id)."'
AND ecp.process_status = 1
AND ecp.process_type = '".$this->db->escape_str($process_type)."'
".$where;
}
/*Check if has activity */
public function has_activity( $user_id, $login_id ,$process_type, $event_id=null){
// Check if process_exist for non paid event
if ($this->input->post('event_category') && $this->input->post('event_category') != 'PAID_EVENT') {
$record = $this->db->query($this->_query_activity($user_id, $login_id, $process_type, $event_id));
return $record->num_rows() > 0;
}
return false;
}
/*End of check if has activity */
public function unlock_action($user_id, $login_id, $event_id, $process_type){
$this->db->where('login_id', $login_id)
->where('process_reference', $event_id)
->where('process_type', $process_type)
->where('process_status',1)
->update('event_concurrent_process', array('process_status' => 0));
return true;
}
public function unlock_action_by_process_id($login_id, $process_type, $process_id){
$this->db->where('login_id', $login_id)
->where('process_id', $process_id)
->where('process_type', $process_type)
->where('process_status',1)
->update('event_concurrent_process', array('process_status' => 0));
return true;
}
public function unlock_all_action($update_process){
if(countVal($update_process) > 0) {
$this->db->trans_start();
$this->db->update_batch('event_concurrent_process', $update_process, 'process_id');
$this->db->trans_complete();
return ($this->db->trans_status() === FALSE)? FALSE:TRUE;
}else {
return true;
}
}
public function cron_unlock_fo_actions(){
//then, unlock process status
$this->db->query("UPDATE event_concurrent_process
SET process_status = 0
WHERE timestampdiff(MINUTE, process_expiration, NOW()) > 2
AND process_status = 1");
$updated_rows = $this->db->affected_rows();
if($updated_rows > 0){
return $updated_rows;
}
return false;
}
public function check_process_exists($process_id, $login_id, $event_id) {
$this->db->select("*");
$this->db->from("event_concurrent_process");
$this->db->where("process_reference", $event_id);
$this->db->where("process_id", $process_id);
$this->db->where("login_id", $login_id);
if($this->db->get()->num_rows()>0) {
return true;
}
return false;
}
}