online.inc.php 17 KB

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