freenetis-github/application/models/login_log.php @ 53cf4ce8
8baed187 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|||
* This file is part of open source system FreenetIS
|
|||
* and it is release under GPLv3 licence.
|
|||
*
|
|||
* More info about licence can be found:
|
|||
* http://www.gnu.org/licenses/gpl-3.0.html
|
|||
*
|
|||
* More info about project can be found:
|
|||
* http://www.freenetis.org/
|
|||
*
|
|||
*/
|
|||
/**
|
|||
* Login logs.
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @package Model
|
|||
*
|
|||
* @property int $id
|
|||
* @property int $user_id
|
|||
* @property User_Model $user
|
|||
* @property datetime $time
|
|||
* @property string $IP_address
|
|||
*/
|
|||
class Login_log_Model extends ORM
|
|||
{
|
|||
protected $belongs_to = array('user');
|
|||
/**
|
|||
* Contruct of app, shutdown action logs by default
|
|||
* @param type $id
|
|||
*/
|
|||
public function __construct($id = false)
|
|||
{
|
|||
parent::__construct($id);
|
|||
// turn off action log
|
|||
$this->set_logger(FALSE);
|
|||
}
|
|||
/**
|
|||
* Returns last login of all user
|
|||
*
|
|||
* @author Michal Kliment
|
|||
c1bdc1c4 | Michal Kliment | * @param int $sql_offset
|
|
* @param int $limit_results
|
|||
* @param string $order_by
|
|||
* @param string $order_by_direction
|
|||
* @param string $filter_sql
|
|||
8baed187 | Michal Kliment | * @return Mysql_Result
|
|
*/
|
|||
c1bdc1c4 | Michal Kliment | public function get_all_login_logs($sql_offset = 0, $limit_results = 500,
|
|
$order_by = 'last_time', $order_by_direction = 'DESC', $filter_sql = '')
|
|||
8baed187 | Michal Kliment | {
|
|
c1bdc1c4 | Michal Kliment | $where = '';
|
|
if (!empty($filter_sql))
|
|||
{
|
|||
$where = 'WHERE ' . $filter_sql;
|
|||
}
|
|||
if (strtolower($order_by_direction) != 'asc')
|
|||
{
|
|||
$order_by_direction = 'desc';
|
|||
}
|
|||
if (!in_array($order_by, array('id', 'name', 'last_time')))
|
|||
{
|
|||
$order_by = 'last_time';
|
|||
}
|
|||
if ($order_by == 'last_time')
|
|||
{
|
|||
$order_by = "IF(last_time IS NULL, 0, 1) $order_by_direction, last_time";
|
|||
}
|
|||
return $this->db->query("
|
|||
8baed187 | Michal Kliment | SELECT id, name, IFNULL(last_time,?) as last_time
|
|
FROM (
|
|||
SELECT *
|
|||
FROM (
|
|||
c1bdc1c4 | Michal Kliment | SELECT u.id, CONCAT(u.name, ' ', u.surname) AS name,
|
|
8baed187 | Michal Kliment | l.time AS last_time
|
|
FROM users u
|
|||
LEFT JOIN login_logs l ON u.id = l.user_id
|
|||
ORDER BY time DESC
|
|||
) AS q1
|
|||
c1bdc1c4 | Michal Kliment | $where
|
|
8baed187 | Michal Kliment | GROUP BY q1.id
|
|
) AS q3
|
|||
c1bdc1c4 | Michal Kliment | ORDER BY $order_by $order_by_direction
|
|
LIMIT " . intval($sql_offset) . ", " . intval($limit_results) . "
|
|||
", __('Never'));
|
|||
8baed187 | Michal Kliment | }
|
|
/**
|
|||
c1bdc1c4 | Michal Kliment | * Returns count of last login of all user
|
|
8baed187 | Michal Kliment | *
|
|
c1bdc1c4 | Michal Kliment | * @param string $filter_sql
|
|
8baed187 | Michal Kliment | * @author Michal Kliment
|
|
* @return integer
|
|||
*/
|
|||
c1bdc1c4 | Michal Kliment | public function count_all_login_logs($filter_sql)
|
|
8baed187 | Michal Kliment | {
|
|
c1bdc1c4 | Michal Kliment | ||
$where = '';
|
|||
if (!empty($filter_sql))
|
|||
{
|
|||
$where = 'WHERE ' . $filter_sql;
|
|||
}
|
|||
else
|
|||
{
|
|||
return ORM::factory('user')->count_all();
|
|||
}
|
|||
return $this->db->query("
|
|||
SELECT COUNT(*) AS total
|
|||
FROM (
|
|||
SELECT *
|
|||
FROM (
|
|||
SELECT u.id, CONCAT(u.name, ' ', u.surname) AS name,
|
|||
l.time AS last_time
|
|||
FROM users u
|
|||
LEFT JOIN login_logs l ON u.id = l.user_id
|
|||
ORDER BY time DESC
|
|||
) AS q1
|
|||
$where
|
|||
GROUP BY q1.id
|
|||
) AS q3
|
|||
")->current()->total;
|
|||
8baed187 | Michal Kliment | }
|
|
/**
|
|||
* Gets all logins by user
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param integer $user_id
|
|||
c1bdc1c4 | Michal Kliment | * @param int $sql_offset
|
|
* @param int $limit_results
|
|||
* @param string $order_by
|
|||
* @param string $order_by_direction
|
|||
* @param string $filter_sql
|
|||
8baed187 | Michal Kliment | * @return Mysql_Result
|
|
*/
|
|||
public function get_all_login_logs_by_user($user_id = 0, $sql_offset = 0,
|
|||
c1bdc1c4 | Michal Kliment | $limit_results = 500, $order_by = 'time', $order_by_direction = 'desc',
|
|
$filter_sql = '')
|
|||
8baed187 | Michal Kliment | {
|
|
// order by check
|
|||
if (!$this->has_column($order_by))
|
|||
{
|
|||
$order_by = 'time';
|
|||
}
|
|||
// order by direction check
|
|||
if (strtolower($order_by_direction) != 'desc')
|
|||
{
|
|||
$order_by_direction = 'asc';
|
|||
}
|
|||
c1bdc1c4 | Michal Kliment | // where
|
|
$where = '';
|
|||
if (!empty($filter_sql))
|
|||
{
|
|||
$where = 'AND ' . $filter_sql;
|
|||
}
|
|||
8baed187 | Michal Kliment | // query
|
|
return $this->db->query("
|
|||
SELECT id, time, ip_address
|
|||
FROM login_logs
|
|||
c1bdc1c4 | Michal Kliment | WHERE user_id = ? $where
|
|
8baed187 | Michal Kliment | ORDER BY $order_by $order_by_direction
|
|
LIMIT " . intval($sql_offset) . ", " . intval($limit_results) . "
|
|||
", $user_id);
|
|||
}
|
|||
/**
|
|||
* Returns count of logins by user
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param integer $user_id
|
|||
c1bdc1c4 | Michal Kliment | * @param string $filter_sql
|
|
8baed187 | Michal Kliment | * @return integer
|
|
*/
|
|||
c1bdc1c4 | Michal Kliment | public function count_all_login_logs_by_user($user_id = 0, $filter_sql = '')
|
|
8baed187 | Michal Kliment | {
|
|
c1bdc1c4 | Michal Kliment | // where
|
|
$where = '';
|
|||
if (!empty($filter_sql))
|
|||
{
|
|||
$where = 'AND ' . $filter_sql;
|
|||
}
|
|||
// query
|
|||
8baed187 | Michal Kliment | return $this->db->query("
|
|
SELECT COUNT(*) AS count
|
|||
FROM login_logs
|
|||
c1bdc1c4 | Michal Kliment | WHERE user_id = ? $where
|
|
8baed187 | Michal Kliment | ", $user_id)->current()->count;
|
|
}
|
|||
}
|