online.inc.php 17 KB

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