123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Code library for showing Who is online
- *
- * @author Istvan Mandak, principal author
- * @author Denes Nagy, principal author
- * @author Bart Mollet
- * @author Roan Embrechts, cleaning and bugfixing
- * @package chamilo.whoisonline
- */
- use ChamiloSession as Session;
- /**
- * Insert a login reference for the current user into the track_e_online stats table.
- * This table keeps trace of the last login. Nothing else matters (we don't keep traces of anything older)
- * @param int user id
- * @return void
- */
- function LoginCheck($uid)
- {
- $_course = api_get_course_info();
- $uid = (int) $uid;
- $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
- if (!empty($uid)) {
- $user_ip = '';
- if (!empty($_SERVER['REMOTE_ADDR'])) {
- $user_ip = Database::escape_string(api_get_real_ip());
- }
- $login_date = api_get_utc_datetime();
- $access_url_id = 1;
- if (api_get_multiple_access_url() && api_get_current_access_url_id()!=-1) {
- $access_url_id = api_get_current_access_url_id();
- }
- $session_id = api_get_session_id();
- // if the $_course array exists this means we are in a course and we have to store this in the who's online table also
- // to have the x users in this course feature working
- if (is_array($_course) && count($_course)>0 && !empty($_course['id'])) {
- $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,user_ip, c_id, session_id, access_url_id)
- VALUES ($uid,$uid,'$login_date','$user_ip', '".$_course['real_id']."' , '$session_id' , '$access_url_id' )";
- } else {
- $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,user_ip, c_id, session_id, access_url_id)
- VALUES ($uid,$uid,'$login_date','$user_ip', 0, '$session_id', '$access_url_id')";
- }
- Database::query($query);
- }
- }
- /**
- * @param int $userId
- */
- function preventMultipleLogin($userId)
- {
- $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
- $userId = intval($userId);
- if (api_get_settings('prevent_multiple_simultaneous_login') === 'true') {
- if (!empty($userId) && !api_is_anonymous()) {
- $isFirstLogin = Session::read('first_user_login');
- if (empty($isFirstLogin)) {
- $sql = "SELECT login_id FROM $table
- WHERE login_user_id = " . $userId . " LIMIT 1";
- $result = Database::query($sql);
- $loginData = array();
- if (Database::num_rows($result)) {
- $loginData = Database::fetch_array($result);
- }
- $userIsReallyOnline = user_is_online($userId);
- // Trying double login.
- if (!empty($loginData) && $userIsReallyOnline == true) {
- session_regenerate_id();
- Session::destroy();
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=multiple_connection_not_allowed');
- exit;
- } else {
- // First time
- Session::write('first_user_login', 1);
- }
- }
- }
- }
- }
- /**
- * This function handles the logout and is called whenever there is a $_GET['logout']
- * @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>
- */
- function online_logout($user_id = null, $logout_redirect = false)
- {
- global $extAuthSource;
- // Database table definition
- $tbl_track_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
- if (empty($user_id)) {
- $user_id = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
- }
- //Changing global chat status to offline
- if (api_is_global_chat_enabled()) {
- $chat = new Chat();
- $chat->setUserStatus(0);
- }
- // selecting the last login of the user
- $sql = "SELECT login_id, login_date
- FROM $tbl_track_login
- WHERE login_user_id = $user_id
- ORDER BY login_date DESC
- LIMIT 0,1";
- $q_last_connection = Database::query($sql);
- if (Database::num_rows($q_last_connection)>0) {
- $i_id_last_connection = Database::result($q_last_connection,0,"login_id");
- }
- if (!isset($_SESSION['login_as'])) {
- $current_date = api_get_utc_datetime();
- $sql = "UPDATE $tbl_track_login SET logout_date='".$current_date."'
- WHERE login_id='$i_id_last_connection'";
- Database::query($sql);
- }
- //LoginDelete($user_id); //from inc/lib/online.inc.php - removes the "online" status
- //the following code enables the use of an external logout function.
- //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
- // then a function called ldap_logout() inside that file
- // (using *authent_name*_logout as the function name) and the following code
- // will find and execute it
- $uinfo = api_get_user_info($user_id);
- if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
- if (is_array($extAuthSource[$uinfo['auth_source']])) {
- $subarray = $extAuthSource[$uinfo['auth_source']];
- if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
- require_once($subarray['logout']);
- $logout_function = $uinfo['auth_source'].'_logout';
- if (function_exists($logout_function)) {
- $logout_function($uinfo);
- }
- }
- }
- }
- require_once api_get_path(SYS_PATH) . 'main/chat/chat_functions.lib.php';
- exit_of_chat($user_id);
- session_regenerate_id();
- Session::destroy();
- if ($logout_redirect) {
- header("Location: ".api_get_path(WEB_PATH)."index.php");
- return;
- }
- }
- /**
- * Returns a list (array) of users who are online and in this course.
- * @param int User ID
- * @param int Number of minutes
- * @param string Course code (could be empty, but then the function returns false)
- * @return array Each line gives a user id and a login time
- */
- function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
- {
- if (empty($course_code)) return false;
- if (empty($time_limit)) {
- $time_limit = api_get_setting('display.time_limit_whosonline');
- } else {
- $time_limit = intval($time_limit);
- }
- $online_time = time() - $time_limit * 60;
- $current_date = api_get_utc_datetime($online_time);
- $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
- $course_code = Database::escape_string($course_code);
- $courseInfo = api_get_course_info($course_code);
- $courseId = $courseInfo['real_id'];
- $from = intval($from);
- $number_of_items = intval($number_of_items);
- $query = "SELECT login_user_id, login_date FROM $track_online_table
- WHERE login_user_id <> 2 AND c_id = $courseId AND login_date >= '$current_date'
- LIMIT $from, $number_of_items ";
- $result = Database::query($query);
- if ($result) {
- $users_online = array();
- while(list($login_user_id, $login_date) = Database::fetch_row($result)) {
- $users_online[] = $login_user_id;
- }
- return $users_online;
- } else {
- return false;
- }
- }
- function who_is_online_in_this_course_count($uid, $time_limit, $coursecode=null)
- {
- if (empty($coursecode)) {
- return false;
- }
- $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
- $coursecode = Database::escape_string($coursecode);
- $time_limit = Database::escape_string($time_limit);
- $online_time = time() - $time_limit * 60;
- $current_date = api_get_utc_datetime($online_time);
- $courseId = api_get_course_int_id($coursecode);
- if (empty($courseId)) {
- return false;
- }
- $query = "SELECT count(login_user_id) as count
- FROM $track_online_table
- WHERE login_user_id <> 2 AND c_id = $courseId AND login_date >= '$current_date' ";
- $result = Database::query($query);
- if (Database::num_rows($result) > 0) {
- $row = Database::fetch_array($result);
- return $row['count'];
- } else {
- return false;
- }
- }
- /**
- * Gets a list of chat calls made by others to the current user (info kept in main.user table)
- * @param none - taken from global space
- * @return string An HTML-formatted message
- */
- function chatcall() {
- $_cid = api_get_course_id();
- $_user = api_get_user_info();
- if (!$_user['user_id']) {
- return (false);
- }
- $userId = intval($_user['user_id']);
- $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
- $sql="SELECT chatcall_user_id, chatcall_date FROM $track_user_table
- WHERE ( id = $userId )";
- $result=Database::query($sql);
- $row=Database::fetch_array($result);
- $login_date=$row['chatcall_date'];
- $hour = substr($login_date,11,2);
- $minute = substr($login_date,14,2);
- $second = substr($login_date,17,2);
- $month = substr($login_date,5,2);
- $day = substr($login_date,8,2);
- $year = substr($login_date,0,4);
- $calltime = mktime($hour,$minute,$second,$month,$day,$year);
- $time = api_get_utc_datetime();
- $minute_passed=5; //within this limit, the chat call request is valid
- $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
- if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
- $webpath = api_get_path(WEB_CODE_PATH);
- $userInfo = api_get_user_info($row['chatcall_user_id']);
- $message=get_lang('YouWereCalled').' : '.$userInfo['complete_name'].'<br>'.get_lang('DoYouAccept')
- ."<p>"
- ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
- . get_lang("Yes")
- ."</a>"
- ." | "
- ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
- . get_lang("No")
- ."</a>"
- ."</p>";
- return($message);
- } else {
- return false;
- }
- }
|