Skip to content

Commit

Permalink
Few refactoring bits after cindyli pull request comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anvk committed Feb 15, 2013
1 parent 08d8e79 commit 90858e7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
14 changes: 8 additions & 6 deletions include/header.inc.php
Expand Up @@ -99,6 +99,9 @@
$session_timeout = intVal($_at_timeout) * 1000;
$session_warning = 300 * 1000; // 5 minutes

$session_timeout = 40000;
$session_warning = 20000;

$custom_head .= '
<link rel="stylesheet" href="'.AT_print($_base_path, 'url.base').'jscripts/lib/jquery-ui.css" />
<script src="'.AT_print($_base_path, 'url.base').'jscripts/infusion/lib/jquery/core/js/jquery.js" type="text/javascript"></script>
Expand All @@ -108,14 +111,13 @@
<script type="text/javascript">
$(document).ready(function() {
ATutor.autoLogout.pageNavigate({
logoutTime : '.$session_timeout.',
warningBeforeLogoutTime : '.$session_warning.',
timeLogout : '.$session_timeout.',
timeWarningBeforeLogout : '.$session_warning.',
logoutUrl : "'.AT_print($_base_path, 'url.base').'logout.php",
title : "'._AT('session_timeout_title').'",
button_1 : "'._AT('session_timeout_logout_now').'",
button_2 : "'._AT('session_timeout_stay_connected').'",
message : "'._AT('session_will_expire').'",
cookieTimeoutName : "userActivity"
textButtonLogout : "'._AT('session_timeout_logout_now').'",
textButtonStayConnected : "'._AT('session_timeout_stay_connected').'",
message : "'._AT('session_will_expire').'"
});
});
Expand Down
3 changes: 0 additions & 3 deletions include/session_keepalive.php

This file was deleted.

81 changes: 55 additions & 26 deletions jscripts/ATutorAutoLogout.js
Expand Up @@ -12,30 +12,42 @@ ATutor.autoLogout = ATutor.autoLogout || {};
(function() {
"use strict";

// function which is called once at the page start to set the times and start the session check process
/**
* Function which is called once at the page start to set the times and start the session check process
* @options
* timeLogout - Time in seconds when user will be logged out
* timeWarningBeforeLogout - Time in seconds when warning dialog will be shown to the user before being logged out
* logoutUrl - URL where use will be bounced upon logout
* title - Title of the dialog
* textButtonLogout - Text on Log Out button inside of the dialog
* textButtonStayConnected - Text on the button which will make user stay connected inside of the dialog
* message - Message text inside of the dialog
* @author Alexey Novak
*/
ATutor.autoLogout.pageNavigate = function (options) {
options = options || {};
var logoutTime = options.logoutTime,
warningBeforeLogoutTime = options.warningBeforeLogoutTime,
button_1 = options.button_1,
button_2 = options.button_2,
options.cookieTimeoutName = "userActivity"; // Cookie name which will be used for tracking activity time
var timeLogout = options.timeLogout,
timeWarningBeforeLogout = options.timeWarningBeforeLogout,
textButtonLogout = options.textButtonLogout,
textButtonStayConnected = options.textButtonStayConnected,
buttonOptions = {};

// If times are invalid then just stop right there
if (warningBeforeLogoutTime >= logoutTime) {
if (timeWarningBeforeLogout >= timeLogout) {
return;
}
// Calculate time for the warning timer since user passes how many seconds before logout user should see the message popup
options.warningBeforeLogoutTime = logoutTime - warningBeforeLogoutTime;
options.timeWarningBeforeLogout = timeLogout - timeWarningBeforeLogout;

// Set starting options
ATutor.autoLogout = $.extend(ATutor.autoLogout, options);

// Buttons for the sessionTimeout dialog
buttonOptions[button_1] = function() {
buttonOptions[textButtonLogout] = function() {
window.location = autoLogout.logoutUrl;
};
buttonOptions[button_2] = function() {
buttonOptions[textButtonStayConnected] = function() {
$(this).dialog("close");
autoLogout.writeCookieTime();
autoLogout.startLogoutProcess();
Expand All @@ -60,7 +72,10 @@ ATutor.autoLogout = ATutor.autoLogout || {};
autoLogout.startLogoutProcess();
};

// Function which will write the activity time into cookie and will update our JS activity variable
/**
* Function which will write the activity time into cookie and will update our JS activity variable
* @author Alexey Novak
*/
ATutor.autoLogout.writeCookieTime = function () {
var autoLogout = ATutor.autoLogout,
now = new Date();
Expand All @@ -70,33 +85,42 @@ ATutor.autoLogout = ATutor.autoLogout || {};
$.cookie(autoLogout.cookieTimeoutName, now.toString());
};

// Function which will start timeouts
/**
* Function which will start timeouts
* @author Alexey Novak
*/
ATutor.autoLogout.startLogoutProcess = function () {
var autoLogout = ATutor.autoLogout;
var autoLogout = ATutor.autoLogout,
warningCallback = function () {
// open a warning dialog
autoLogout.sessionTimeoutDialog.dialog("open");
},
logoutCallback = function () {
// Logout user
window.location = autoLogout.logoutUrl;
};

// Clear all timers first if they are set
clearTimeout(autoLogout.warningTimeout);
clearTimeout(autoLogout.logoutTimeout);

// Set the timeout for warning
ATutor.autoLogout.warningTimeout = setTimeout(function () {
autoLogout.logoutUpdate(function () {
// open a warning dialog
autoLogout.sessionTimeoutDialog.dialog("open");
});
}, autoLogout.warningBeforeLogoutTime);
autoLogout.activityCheck(warningCallback);
}, autoLogout.timeWarningBeforeLogout);

// Set the timeout for logout
ATutor.autoLogout.logoutTimeout = setTimeout(function () {
autoLogout.logoutUpdate(function () {
// Logout user
window.location = autoLogout.logoutUrl;
});
}, autoLogout.logoutTime);
autoLogout.activityCheck(logoutCallback);
}, autoLogout.timeLogout);
};

// Function which will check if user is active and either execute a callback or start the session logout process all over again
ATutor.autoLogout.logoutUpdate = function (callback) {
/**
* Function which will check if user is active and either execute a callback or start the session logout process all over again
* @param a function which is called if user is not active after the check
* @author Alexey Novak
*/
ATutor.autoLogout.activityCheck = function (callback) {
var autoLogout = ATutor.autoLogout;
if (autoLogout.checkIfActive(autoLogout.activityTime)) {
// Close the warning dialog and start the session logout process again
Expand All @@ -109,12 +133,17 @@ ATutor.autoLogout = ATutor.autoLogout || {};
}
};

// Function which returns true or false depending if user is active by comparing JS activity variable with the cookie one.
// If user is active we update our JS activity variable.
/**
* Function which returns true or false depending if user is active. The check is accomplished by comparing JS activity variable with the cookie one.
* @param timeStamp which will be stored into a cookie
* @return true if user is active and cookie time is newer. false if user is still inactive and cookie time is old or the same
* @author Alexey Novak
*/
ATutor.autoLogout.checkIfActive = function (timeStamp) {
var autoLogout = ATutor.autoLogout,
cookieActiveTimeStamp = new Date($.cookie(autoLogout.cookieTimeoutName));
if (timeStamp < cookieActiveTimeStamp) {
// Overwrite our JS activity variable with the one which is in a cookie
ATutor.autoLogout.activityTime = cookieActiveTimeStamp;
return true;
}
Expand Down

0 comments on commit 90858e7

Please sign in to comment.