online.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Code library for showing Who is online
  5. *
  6. * @author Istvan Mandak, principal author
  7. * @author Denes Nagy, principal author
  8. * @author Bart Mollet
  9. * @author Roan Embrechts, cleaning and bugfixing
  10. * @package chamilo.whoisonline
  11. */
  12. /**
  13. * Insert a login reference for the current user into the track_e_online stats table.
  14. * This table keeps trace of the last login. Nothing else matters (we don't keep traces of anything older)
  15. * @param int user id
  16. * @return void
  17. */
  18. use \ChamiloSession as Session;
  19. class Online
  20. {
  21. /**
  22. * Checking user in DB
  23. * @param int $uid
  24. */
  25. public static function loginCheck($uid)
  26. {
  27. $_course = api_get_course_info();
  28. $uid = (int) $uid;
  29. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  30. if (!empty($uid)) {
  31. $login_ip = '';
  32. if (!empty($_SERVER['REMOTE_ADDR'])) {
  33. $login_ip = Database::escape_string($_SERVER['REMOTE_ADDR']);
  34. }
  35. $login_date = api_get_utc_datetime();
  36. $access_url_id = 1;
  37. if (api_get_multiple_access_url() && api_get_current_access_url_id()!=-1) {
  38. $access_url_id = api_get_current_access_url_id();
  39. }
  40. $session_id = api_get_session_id();
  41. // 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
  42. // to have the x users in this course feature working
  43. if (is_array($_course) && count($_course)>0 && !empty($_course['id'])) {
  44. $query = "REPLACE INTO ".$online_table ." (login_id, login_user_id, login_date, login_ip, course, session_id, access_url_id)
  45. VALUES ($uid, $uid, '$login_date', '$login_ip', '".$_course['id']."', '$session_id', '$access_url_id' )";
  46. } else {
  47. $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,login_ip, session_id, access_url_id)
  48. VALUES ($uid,$uid,'$login_date','$login_ip', '$session_id', '$access_url_id')";
  49. }
  50. Database::query($query);
  51. }
  52. }
  53. /**
  54. * This function handles the logout and is called whenever there is a $_GET['logout']
  55. * @return void Directly redirects the user or leaves him where he is, but doesn't return anything
  56. * @author Fernando P. García <fernando@develcuy.com>
  57. */
  58. public static function logout($user_id = null, $logout_redirect = false)
  59. {
  60. global $extAuthSource;
  61. // Database table definition
  62. $tbl_track_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  63. if (empty($user_id)) {
  64. $user_id = api_get_user_id();
  65. }
  66. //Changing global chat status to offline
  67. if (api_is_global_chat_enabled()) {
  68. $chat = new Chat();
  69. $chat->set_user_status(0);
  70. }
  71. // selecting the last login of the user
  72. $sql_last_connection = "SELECT login_id, login_date FROM $tbl_track_login
  73. WHERE login_user_id='$user_id' ORDER BY login_date DESC LIMIT 0,1";
  74. $q_last_connection=Database::query($sql_last_connection);
  75. $i_id_last_connection = null;
  76. if (Database::num_rows($q_last_connection)>0) {
  77. $i_id_last_connection = Database::result($q_last_connection,0,"login_id");
  78. }
  79. if (!isset($_SESSION['login_as']) && !empty($i_id_last_connection)) {
  80. $current_date = api_get_utc_datetime();
  81. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date='".$current_date."' WHERE login_id = '$i_id_last_connection'";
  82. Database::query($s_sql_update_logout_date);
  83. }
  84. Online::loginDelete($user_id); //from inc/lib/online.inc.php - removes the "online" status
  85. //the following code enables the use of an external logout function.
  86. //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
  87. // then a function called ldap_logout() inside that file
  88. // (using *authent_name*_logout as the function name) and the following code
  89. // will find and execute it
  90. $uinfo = api_get_user_info($user_id);
  91. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  92. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  93. $subarray = $extAuthSource[$uinfo['auth_source']];
  94. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  95. require_once($subarray['logout']);
  96. $logout_function = $uinfo['auth_source'].'_logout';
  97. if (function_exists($logout_function)) {
  98. $logout_function($uinfo);
  99. }
  100. }
  101. }
  102. }
  103. require_once api_get_path(SYS_PATH) . 'main/chat/chat_functions.lib.php';
  104. exit_of_chat($user_id);
  105. Session::destroy();
  106. if ($logout_redirect) {
  107. header("Location: index.php");
  108. exit;
  109. }
  110. }
  111. /**
  112. * Remove all login records from the track_e_online stats table, for the given user ID.
  113. * @param int User ID
  114. * @return bool
  115. */
  116. public static function loginDelete($user_id)
  117. {
  118. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  119. $user_id = intval($user_id);
  120. if (empty($user_id)) {
  121. return false;
  122. }
  123. $query = "DELETE FROM ".$online_table ." WHERE login_user_id = '".$user_id."'";
  124. Database::query($query);
  125. return true;
  126. }
  127. public static function user_is_online($user_id)
  128. {
  129. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  130. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  131. $access_url_id = api_get_current_access_url_id();
  132. $time_limit = api_get_setting('time_limit_whosonline');
  133. $online_time = time() - $time_limit*60;
  134. $limit_date = api_get_utc_datetime($online_time);
  135. $query = " SELECT login_user_id,login_date FROM ".$track_online_table ." track INNER JOIN ".$table_user ." u ON (u.user_id=track.login_user_id)
  136. WHERE track.access_url_id = $access_url_id AND
  137. login_date >= '".$limit_date."' AND
  138. u.user_id = $user_id
  139. LIMIT 1 ";
  140. $result = Database::query($query);
  141. if (Database::num_rows($result)) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Gives a list of people online now (and in the last $valid minutes)
  148. * @return array For each line, a list of user IDs and login dates, or FALSE on error or empty results
  149. */
  150. public static function who_is_online($from, $number_of_items, $column = null, $direction = null, $time_limit = null, $friends = false)
  151. {
  152. // Time limit in seconds?
  153. if (empty($time_limit)) {
  154. $time_limit = api_get_setting('time_limit_whosonline');
  155. } else {
  156. $time_limit = intval($time_limit);
  157. }
  158. $from = intval($from);
  159. $number_of_items = intval($number_of_items);
  160. if (empty($column)) {
  161. $column = 'picture_uri';
  162. if ($friends) {
  163. $column = 'login_date';
  164. }
  165. }
  166. if (empty($direction)) {
  167. $direction = 'DESC';
  168. } else {
  169. if (!in_array(strtolower($direction), array('asc', 'desc'))) {
  170. $direction = 'DESC';
  171. }
  172. }
  173. $online_time = time() - $time_limit*60;
  174. $current_date = api_get_utc_datetime($online_time);
  175. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  176. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  177. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  178. $query = '';
  179. if ($friends) {
  180. // who friends from social network is online
  181. $query = "SELECT DISTINCT login_user_id, login_date
  182. FROM $track_online_table INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  183. WHERE login_date >= '".$current_date."' AND
  184. friend_user_id <> '".api_get_user_id()."' AND
  185. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  186. user_id = '".api_get_user_id()."'
  187. ORDER BY $column $direction
  188. LIMIT $from, $number_of_items";
  189. } else {
  190. $query = "SELECT DISTINCT login_user_id, login_date FROM ".$track_online_table ." e INNER JOIN ".$table_user ." u ON (u.user_id=e.login_user_id)
  191. WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
  192. ORDER BY $column $direction
  193. LIMIT $from, $number_of_items";
  194. }
  195. if (api_get_multiple_access_url()) {
  196. $access_url_id = api_get_current_access_url_id();
  197. if ($access_url_id != -1) {
  198. if ($friends) {
  199. // friends from social network is online
  200. $query = "SELECT distinct login_user_id,login_date
  201. FROM $track_online_table track INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  202. WHERE track.access_url_id = $access_url_id AND
  203. login_date >= '".$current_date."' AND
  204. friend_user_id <> '".api_get_user_id()."' AND
  205. relation_type='".USER_RELATION_TYPE_FRIEND."'
  206. ORDER BY $column $direction
  207. LIMIT $from, $number_of_items";
  208. } else {
  209. // all users online
  210. $query = "SELECT login_user_id, login_date FROM ".$track_online_table ." track INNER JOIN ".$table_user ." u
  211. ON (u.user_id=track.login_user_id)
  212. WHERE u.status != ".ANONYMOUS." AND track.access_url_id = $access_url_id AND
  213. login_date >= '".$current_date."'
  214. ORDER BY $column $direction
  215. LIMIT $from, $number_of_items";
  216. }
  217. }
  218. }
  219. //This query will show all registered users. Only for dev purposes.
  220. /*$query = "SELECT DISTINCT u.user_id as login_user_id, login_date FROM ".$track_online_table ." e , $table_user u
  221. GROUP by u.user_id
  222. ORDER BY $column $direction
  223. LIMIT $from, $number_of_items"; */
  224. $result = Database::query($query);
  225. if ($result) {
  226. /*$valid_date_time = new DateTime();
  227. $diff = "PT".$time_limit.'M';
  228. $valid_date_time->sub(new DateInterval($diff));*/
  229. $users_online = array();
  230. while(list($login_user_id, $login_date) = Database::fetch_row($result)) {
  231. $users_online[] = $login_user_id;
  232. }
  233. return $users_online;
  234. } else {
  235. return false;
  236. }
  237. }
  238. public static function who_is_online_count($time_limit = null, $friends = false)
  239. {
  240. if (empty($time_limit)) {
  241. $time_limit = api_get_setting('time_limit_whosonline');
  242. } else {
  243. $time_limit = intval($time_limit);
  244. }
  245. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  246. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  247. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  248. $query = '';
  249. $online_time = time() - $time_limit*60;
  250. $current_date = api_get_utc_datetime($online_time);
  251. if ($friends) {
  252. // who friends from social network is online
  253. $query = "SELECT DISTINCT count(login_user_id) as count
  254. FROM $track_online_table INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  255. WHERE login_date >= '$current_date' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' AND user_id = '".api_get_user_id()."' ";
  256. } else {
  257. // All users online
  258. $query = "SELECT count(login_id) as count
  259. FROM $track_online_table track INNER JOIN $table_user u ON (u.user_id=track.login_user_id)
  260. WHERE u.status != ".ANONYMOUS." AND login_date >= '$current_date' ";
  261. }
  262. if (api_get_multiple_access_url()) {
  263. $access_url_id = api_get_current_access_url_id();
  264. if ($access_url_id != -1) {
  265. if ($friends) {
  266. // friends from social network is online
  267. $query = "SELECT DISTINCT count(login_user_id) as count
  268. FROM $track_online_table track
  269. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  270. WHERE track.access_url_id = $access_url_id AND login_date >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  271. } else {
  272. // all users online
  273. $query = "SELECT count(login_id) as count FROM $track_online_table track
  274. INNER JOIN $table_user u ON (u.user_id=track.login_user_id)
  275. WHERE u.status != ".ANONYMOUS." AND track.access_url_id = $access_url_id AND login_date >= '$current_date' ";
  276. }
  277. }
  278. }
  279. //Dev purposes show all users online
  280. /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
  281. $query = "SELECT count(*) as count FROM ".$table_user ." ";*/
  282. $result = Database::query($query);
  283. if (Database::num_rows($result) > 0) {
  284. $row = Database::fetch_array($result);
  285. return $row['count'];
  286. } else {
  287. return false;
  288. }
  289. }
  290. /**
  291. * Returns a list (array) of users who are online and in this course.
  292. * @param int User ID
  293. * @param int Number of minutes
  294. * @param string Course code (could be empty, but then the function returns false)
  295. * @return array Each line gives a user id and a login time
  296. */
  297. public static function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
  298. {
  299. if (empty($course_code)) return false;
  300. if (empty($time_limit)) {
  301. $time_limit = api_get_setting('time_limit_whosonline');
  302. } else {
  303. $time_limit = intval($time_limit);
  304. }
  305. $online_time = time() - $time_limit*60;
  306. $current_date = api_get_utc_datetime($online_time);
  307. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  308. $course_code = Database::escape_string($course_code);
  309. $from = intval($from);
  310. $number_of_items = intval($number_of_items);
  311. $query = "SELECT login_user_id, login_date FROM $track_online_table
  312. WHERE login_user_id <> 2 AND course='$course_code' AND login_date >= '$current_date'
  313. LIMIT $from, $number_of_items ";
  314. $result = Database::query($query);
  315. if ($result) {
  316. /*$valid_date_time = new DateTime();
  317. $diff = "PT".$time_limit.'M';
  318. $valid_date_time->sub(new DateInterval($diff));*/
  319. $users_online = array();
  320. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  321. /*$user_login_date = new DateTime($login_date);
  322. if ($user_login_date > $valid_date_time->format('Y-m-d H:i:s')) {*/
  323. $users_online[] = $login_user_id;
  324. }
  325. return $users_online;
  326. } else {
  327. return false;
  328. }
  329. }
  330. public static function who_is_online_in_this_course_count($uid, $time_limit, $coursecode=null)
  331. {
  332. if (empty($coursecode)) {
  333. return false;
  334. }
  335. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  336. $coursecode = Database::escape_string($coursecode);
  337. $time_limit = Database::escape_string($time_limit);
  338. $online_time = time() - $time_limit*60;
  339. $current_date = api_get_utc_datetime($online_time);
  340. $query = "SELECT count(login_user_id) as count FROM ".$track_online_table ."
  341. WHERE login_user_id <> 2 AND course='".$coursecode."' AND login_date >= '$current_date' ";
  342. $result = Database::query($query);
  343. if (Database::num_rows($result) > 0) {
  344. $row = Database::fetch_array($result);
  345. return $row['count'];
  346. } else {
  347. return false;
  348. }
  349. }
  350. }