online.inc.php 17 KB

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