uid][$sid] = array('created' => $now) ; variable_set('session_aggregator', $session_aggregator); if (session_aggregator_user_exists($account->uid)) { db_query("UPDATE session_aggregator SET unique_sessions = unique_sessions + 1 WHERE uid = %d", $account->uid); } else { db_query("INSERT INTO session_aggregator (uid, unique_sessions, since) VALUES (%d, 1, %d)", $account->uid, $now); } break; case 'logout': // Update record if (session_aggregator_user_exists($account->uid)) { $session_time = time() - $account->login; db_query("UPDATE session_aggregator SET elapsed_time = elapsed_time + %d WHERE uid = %d", $session_time, $account->uid); // Remove the session data from variable table. There won't be a // session collision or hung session since user is being logged out. $session_aggregator = variable_get('session_aggregator', array()); unset($session_aggregator[$account->uid]); variable_set('session_aggregator', $session_aggregator); } break; } } /** * Determine if a user has records in the session_aggregator table * @param int $uid * The id of the user to check * @return boolean */ function session_aggregator_user_exists($uid) { $sql = "SELECT COUNT(*) FROM {session_aggregator} sa WHERE sa.uid = %d"; $result = db_result(db_query($sql, $uid)); return $result ? TRUE : FALSE; } /** * Implementation of hook_views_api(). */ function session_aggregator_views_api() { return array( 'api' => 2, 'path' => drupal_get_path('module', 'session_aggregator') . '/views', ); } /** * Implementation of hook_session_limit. */ function session_aggregator_session_limit($sid, $op) { // When a session is terminated, update the elapsed time from the old session. if ($op == 'disconnect') { global $user; $session_aggregator = variable_get('session_aggregator', array()); $now = time(); $sess_created = $session_aggregator[$user->uid][$sid]['created']; $session_length = $now - $sess_created; $timeout = variable_get('autologout_timeout', 1800); $timeout_padding = variable_get('autologout_padding', 10); // Determine how much time to append to elapsed time. $session_time = ($session_length > ($timeout + (int)$timeout_padding)) ? $timeout + $timeout_padding : $session_length; // Terminate the old session when a collision happens and append the time // of the old session to the elapsed time. db_query("UPDATE session_aggregator SET elapsed_time = elapsed_time + %d WHERE uid = %d", $session_time, $user->uid); } }