online.inc.php 17 KB

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