Fix time recording issue where a user logging out from the portal directly from a course did not trigger a logout time update for the track_e_course_access table - refs BT#12939

pull/2487/head
Yannick Warnier 7 years ago
parent 94c85054f0
commit 0c7b862cd6
  1. 3
      index.php
  2. 2
      main/auth/shibboleth/lib/shibboleth_session.class.php
  3. 54
      main/inc/lib/login.lib.php
  4. 62
      main/inc/lib/online.inc.php
  5. 4
      main/inc/lib/userportal.lib.php
  6. 71
      main/inc/local.inc.php

@ -32,7 +32,8 @@ $loginFailed = isset($_GET['loginFailed']) ? true : isset($loginFailed);
if (!empty($_GET['logout'])) {
$redirect = !empty($_GET['no_redirect']) ? false : true;
$controller->logout($redirect);
// pass $logoutInfo defined in local.inc.php
$controller->logout($redirect, $logoutInfo);
}
/**

@ -42,6 +42,8 @@ class ShibbolethSession
{
$_SESSION['_user'] = array();
online_logout(null, false);
global $logoutInfo;
courseLogout($logoutInfo);
}
/**

@ -508,52 +508,14 @@ class Login
global $_dont_save_user_course_access;
if (isset($_dont_save_user_course_access) && $_dont_save_user_course_access == true) {
$save_course_access = false;
}
if ($save_course_access) {
$course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
/*
* When $_configuration['session_lifetime'] is too big 100 hours (in order to let users take exercises with no problems)
* the function Tracking::get_time_spent_on_the_course() returns big values (200h) due the condition:
* login_course_date > now() - INTERVAL $session_lifetime SECOND
*
*/
/*
if (isset($_configuration['session_lifetime'])) {
$session_lifetime = $_configuration['session_lifetime'];
} else {
$session_lifetime = 3600; // 1 hour
} */
$session_lifetime = 3600; // 1 hour
$time = api_get_utc_datetime();
if (isset($_user['user_id']) && !empty($_user['user_id'])) {
//We select the last record for the current course in the course tracking table
//But only if the login date is < than now + max_life_time
$sql = "SELECT course_access_id FROM $course_tracking_table
WHERE
user_id = ".intval($_user ['user_id'])." AND
c_id = '".api_get_course_int_id()."' AND
session_id = " . api_get_session_id()." AND
login_course_date > now() - INTERVAL $session_lifetime SECOND
ORDER BY login_course_date DESC LIMIT 0,1";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$i_course_access_id = Database::result($result, 0, 0);
//We update the course tracking table
$sql = "UPDATE $course_tracking_table
SET logout_course_date = '$time', counter = counter+1
WHERE course_access_id = ".intval($i_course_access_id)." AND session_id = ".api_get_session_id();
Database::query($sql);
} else {
$sql = "INSERT INTO $course_tracking_table (c_id, user_id, login_course_date, logout_course_date, counter, session_id)".
"VALUES('".api_get_course_int_id()."', '".$_user['user_id']."', '$time', '$time', '1','".api_get_session_id()."')";
Database::query($sql);
}
}
} else {
courseLogout(
[
'uid' => intval($_user ['user_id']),
'cid' => api_get_course_int_id(),
'sid' => api_get_session_id()
]
);
}
}
}

@ -90,6 +90,8 @@ function preventMultipleLogin($userId)
/**
* This function handles the logout and is called whenever there is a $_GET['logout']
* @param int $user_id
* @param bool $logout_redirect
* @return void Directly redirects the user or leaves him where he is, but doesn't return anything
* @author Fernando P. García <fernando@develcuy.com>
*/
@ -463,3 +465,63 @@ function who_is_online_in_this_course_count($uid, $time_limit, $coursecode = nul
return false;
}
}
/**
* Register the logout of the course (usually when logging out of the platform)
* from the track_e_course_access table
* @param array $logoutInfo Information stored by local.inc.php before new context ['uid'=> x, 'cid'=>y, 'sid'=>z]
* @return void
*/
function courseLogout($logoutInfo)
{
if (empty($logoutInfo['uid']) or empty($logoutInfo['cid'])) {
return;
}
if (isset($_configuration['session_lifetime'])) {
$sessionLifetime = $_configuration['session_lifetime'];
} else {
$sessionLifetime = 3600; // 1 hour
}
/*
* When $_configuration['session_lifetime'] is larger than ~100 hours (in order to let users take exercises with no problems)
* the function Tracking::get_time_spent_on_the_course() returns larger values (200h) due the condition:
* login_course_date > now() - INTERVAL $session_lifetime SECOND
*/
if ($sessionLifetime > 86400) {
$sessionLifetime = 3600; // 1 hour
}
if (!empty($logoutInfo) && !empty($logoutInfo['cid'])) {
$tableCourseAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
$userId = intval($logoutInfo['uid']);
$courseId = intval($logoutInfo['cid']);
$sessionId = 0;
if (!empty($logoutInfo['sid'])) {
$sessionId = intval($logoutInfo['sid']);
}
$currentDate = api_get_utc_datetime();
$sql = "SELECT course_access_id
FROM $tableCourseAccess
WHERE user_id = $userId AND
c_id = $courseId AND
session_id = $sessionId AND
login_course_date > '$currentDate' - INTERVAL $sessionLifetime SECOND
ORDER BY login_course_date DESC LIMIT 1";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$courseAccessId = Database::result($result, 0, 0);
$sql = "UPDATE $tableCourseAccess
SET logout_course_date = '$currentDate', counter = counter+1
WHERE course_access_id = $courseAccessId";
error_log($sql);
Database::query($sql);
} else {
$ip = api_get_real_ip();
$sql = "INSERT INTO $tableCourseAccess
(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
VALUES
($courseId, '$ip', $userId, '$currentDate', '$currentDate', 1, $sessionId)";
Database::query($sql);
}
}
}

@ -143,10 +143,12 @@ class IndexManager
/**
* Alias for the online_logout() function
* @param bool $redirect Whether to ask online_logout to redirect to index.php or not
* @param array $logoutInfo Information stored by local.inc.php before new context ['uid'=> x, 'cid'=>y, 'sid'=>z]
*/
public function logout($redirect = true)
public function logout($redirect = true, $logoutInfo = [])
{
online_logout($this->user_id, true);
courseLogout($logoutInfo);
}
/**

@ -139,6 +139,25 @@ if (isset($_SESSION['conditional_login']['uid']) && $_SESSION['conditional_login
$logout = isset($_GET["logout"]) ? $_GET["logout"] : '';
$gidReq = isset($_GET["gidReq"]) ? intval($_GET["gidReq"]) : '';
// Keep a trace of the course and session from which we are getting out, to
// enable proper course logout tracking in courseLogout()
$logoutInfo = [];
if (!empty($logout)) {
$uid = 0;
if (!empty($_SESSION['_user']) && !empty($_SESSION['_user']['user_id'])) {
$uid = $_SESSION['_user']['user_id'];
}
$cid = 0;
if (!empty($_SESSION['_cid'])) {
$cid = api_get_course_int_id($_SESSION['_cid']);
}
$logoutInfo = [
'uid' => $uid,
'cid' => $cid,
'sid' => api_get_session_id()
];
}
//this fixes some problems with generic functionalities like
//My Agenda & What's New icons linking to courses
// $cidReq can be set in the index.php file of a course-area
@ -400,6 +419,7 @@ if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) {
.'index.php?loginFailed=1&error=access_url_inactive';
if ($cas_login) {
cas_logout(null, $location);
courseLogout($logoutInfo);
} else {
header('Location: '.$location);
}
@ -632,6 +652,7 @@ if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) {
if ($logout) {
// Make custom redirect after logout
online_logout($_SESSION['_user']['user_id'], false);
courseLogout($logoutInfo);
$osso->logout(); //redirects and exits
}
} elseif (!$logout) {
@ -703,6 +724,7 @@ if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) {
//if there was an attempted logout without a previous login, log
// this anonymous user out as well but avoid redirect
online_logout(null, false);
courseLogout($logoutInfo);
$osso->logout(); //redirects and exits
}
} elseif (api_get_setting('openid_authentication') == 'true') {
@ -883,53 +905,8 @@ if (!isset($_SESSION['login_as'])) {
// Disables the updates in the TRACK_E_COURSE_ACCESS table
if (isset($_dont_save_user_course_access) && $_dont_save_user_course_access == true) {
$save_course_access = false;
}
if ($save_course_access) {
$course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
/*
* When $_configuration['session_lifetime'] is too big 100 hours (in order to let users take exercises with no problems)
* the function Tracking::get_time_spent_on_the_course() returns big values (200h) due the condition:
* login_course_date > now() - INTERVAL $session_lifetime SECOND
*
*/
/*
if (isset($_configuration['session_lifetime'])) {
$session_lifetime = $_configuration['session_lifetime'];
} else {
$session_lifetime = 3600; // 1 hour
}*/
$session_lifetime = 3600; // 1 hour
$time = api_get_utc_datetime();
if (isset($_user['user_id']) && !empty($_user['user_id'])) {
//We select the last record for the current course in the course tracking table
//But only if the login date is < than now + max_life_time
$sql = "SELECT course_access_id
FROM $course_tracking_table
WHERE
user_id = ".intval($_user['user_id'])." AND
c_id = ".$_course['real_id']." AND
session_id = ".api_get_session_id()." AND
login_course_date > '$time' - INTERVAL $session_lifetime SECOND
ORDER BY login_course_date DESC LIMIT 0,1";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$i_course_access_id = Database::result($result, 0, 0);
// We update the course tracking table
$sql = "UPDATE $course_tracking_table
SET logout_course_date = '$time', counter = counter+1
WHERE
course_access_id = ".intval($i_course_access_id)." AND
session_id = ".api_get_session_id();
Database::query($sql);
} else {
$ip = api_get_real_ip();
$sql = "INSERT INTO $course_tracking_table (c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
VALUES('".$_course['real_id']."', '".$ip."', '".$_user['user_id']."', '$time', '$time', '1','".api_get_session_id()."')";
Database::query($sql);
}
}
} else {
courseLogout($logoutInfo);
}
}
}

Loading…
Cancel
Save