online.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. $reallyNow = time();
  29. $login_date = date("Y-m-d H:i:s",$reallyNow);
  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. function online_logout($user_id = null, $logout_redirect = false) {
  51. global $_configuration, $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 = intval($_GET['uid']);
  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. if (Database::num_rows($q_last_connection)>0) {
  66. $i_id_last_connection=Database::result($q_last_connection,0,"login_id");
  67. }
  68. if (!isset($_SESSION['login_as'])) {
  69. $current_date=date('Y-m-d H:i:s',time());
  70. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date='".$current_date."' WHERE login_id='$i_id_last_connection'";
  71. Database::query($s_sql_update_logout_date);
  72. }
  73. LoginDelete($user_id); //from inc/lib/online.inc.php - removes the "online" status
  74. //the following code enables the use of an external logout function.
  75. //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
  76. // then a function called ldap_logout() inside that file
  77. // (using *authent_name*_logout as the function name) and the following code
  78. // will find and execute it
  79. $uinfo = api_get_user_info($user_id);
  80. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  81. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  82. $subarray = $extAuthSource[$uinfo['auth_source']];
  83. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  84. require_once($subarray['logout']);
  85. $logout_function = $uinfo['auth_source'].'_logout';
  86. if (function_exists($logout_function)) {
  87. $logout_function($uinfo);
  88. }
  89. }
  90. }
  91. }
  92. require_once api_get_path(SYS_PATH) . 'main/chat/chat_functions.lib.php';
  93. exit_of_chat($user_id);
  94. Session::destroy();
  95. if ($logout_redirect) {
  96. header("Location: index.php");
  97. return;
  98. }
  99. }
  100. /**
  101. * Remove all login records from the track_e_online stats table, for the given user ID.
  102. * @param int User ID
  103. * @return void
  104. */
  105. function LoginDelete($user_id) {
  106. $online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  107. $user_id = intval($user_id);
  108. $query = "DELETE FROM ".$online_table ." WHERE login_user_id = '".$user_id."'";
  109. @Database::query($query);
  110. }
  111. function user_is_online($user_id) {
  112. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  113. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  114. $current_date = date('Y-m-d H:i:s',time());
  115. $access_url_id = api_get_current_access_url_id();
  116. $time_limit = api_get_setting('time_limit_whosonline');
  117. //$time_limit = 1; changing this value there is no time limit
  118. $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)
  119. WHERE track.access_url_id = $access_url_id AND
  120. DATE_ADD(login_date,INTERVAL $time_limit MINUTE) >= '".$current_date."' AND
  121. u.user_id = $user_id
  122. LIMIT 1 ";
  123. $result = Database::query($query);
  124. if (Database::num_rows($result)) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. /**
  130. * Gives a list of people online now (and in the last $valid minutes)
  131. * @return array For each line, a list of user IDs and login dates, or FALSE on error or empty results
  132. */
  133. function who_is_online($from, $number_of_items, $column = null, $direction = null, $time_limit = null, $friends = false) {
  134. // Time limit in seconds?
  135. if (empty($time_limit)) {
  136. $time_limit = api_get_setting('time_limit_whosonline');
  137. } else {
  138. $time_limit = intval($time_limit);
  139. }
  140. $from = intval($from);
  141. $number_of_items = intval($number_of_items);
  142. if (empty($column)) {
  143. $column = 'picture_uri';
  144. if ($friends) {
  145. $column = 'login_date';
  146. }
  147. }
  148. if (empty($direction)) {
  149. $direction = 'DESC';
  150. } else {
  151. if (!in_array(strtolower($direction), array('asc', 'desc'))) {
  152. $direction = 'DESC';
  153. }
  154. }
  155. $current_date = date('Y-m-d H:i:s',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 DATE_ADD(login_date,INTERVAL $time_limit MINUTE) >= '".$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 DATE_ADD(login_date,INTERVAL $time_limit MINUTE) >= '".$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. DATE_ADD(login_date,INTERVAL $time_limit MINUTE) >= '".$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. DATE_ADD(login_date,INTERVAL $time_limit MINUTE) >= '".$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) {
  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($valid = null, $friends = false) {
  223. if (empty($valid)) {
  224. $valid = api_get_setting('time_limit_whosonline');
  225. } else {
  226. $valid = intval($valid);
  227. }
  228. $current_date = date('Y-m-d H:i:s',time());
  229. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  230. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  231. $query = '';
  232. if ($friends) {
  233. // who friends from social network is online
  234. $query = "SELECT DISTINCT count(login_user_id) as count
  235. FROM $track_online_table INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  236. WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' AND user_id = '".api_get_user_id()."' ";
  237. } else {
  238. // All users online
  239. $query = "SELECT count(login_id) as count FROM ".$track_online_table ."
  240. WHERE login_user_id <> 2 AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ";
  241. }
  242. if (api_get_multiple_access_url()) {
  243. $access_url_id = api_get_current_access_url_id();
  244. if ($access_url_id != -1) {
  245. if ($friends) {
  246. // friends from social network is online
  247. $query = "SELECT DISTINCT count(login_user_id) as count
  248. FROM $track_online_table track
  249. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  250. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  251. } else {
  252. // all users online
  253. $query = "SELECT count(login_id) as count FROM $track_online_table track
  254. WHERE login_user_id <> 2 AND track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ";
  255. }
  256. }
  257. }
  258. //dev purposes show all users online
  259. /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
  260. $query = "SELECT count(*) as count FROM ".$table_user ." ";*/
  261. $result = Database::query($query);
  262. if (Database::num_rows($result) > 0) {
  263. $row = Database::fetch_array($result);
  264. return $row['count'];
  265. } else {
  266. return false;
  267. }
  268. }
  269. /**
  270. * Returns a list (array) of users who are online and in this course.
  271. * @param int User ID
  272. * @param int Number of minutes
  273. * @param string Course code (could be empty, but then the function returns false)
  274. * @return array Each line gives a user id and a login time
  275. */
  276. function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code) {
  277. if (empty($course_code)) return false;
  278. if (empty($time_limit)) {
  279. $time_limit = api_get_setting('time_limit_whosonline');
  280. } else {
  281. $time_limit = intval($time_limit);
  282. }
  283. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  284. $course_code = Database::escape_string($course_code);
  285. $from = intval($from);
  286. $number_of_items = intval($number_of_items);
  287. $query = "SELECT login_user_id, login_date FROM ".$track_online_table ."
  288. WHERE login_user_id <> 2 AND course='".$course_code."' AND DATE_ADD(login_date,INTERVAL $time_limit MINUTE) >= NOW()
  289. LIMIT $from, $number_of_items ";
  290. $result = Database::query($query);
  291. if ($result) {
  292. $valid_date_time = new DateTime();
  293. $diff = "PT".$time_limit.'M';
  294. $valid_date_time->sub(new DateInterval($diff));
  295. $users_online = array();
  296. while(list($login_user_id, $login_date) = Database::fetch_row($result)) {
  297. $user_login_date = new DateTime($login_date);
  298. if ($user_login_date > $valid_date_time) {
  299. $users_online[] = $login_user_id;
  300. }
  301. }
  302. return $users_online;
  303. } else {
  304. return false;
  305. }
  306. }
  307. function who_is_online_in_this_course_count($uid, $valid, $coursecode=null) {
  308. if(empty($coursecode)) return false;
  309. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  310. $coursecode = Database::escape_string($coursecode);
  311. $valid = Database::escape_string($valid);
  312. $query = "SELECT count(login_user_id) as count FROM ".$track_online_table ."
  313. WHERE login_user_id <> 2 AND course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  314. $result = Database::query($query);
  315. if (Database::num_rows($result) > 0) {
  316. $row = Database::fetch_array($result);
  317. return $row['count'];
  318. } else {
  319. return false;
  320. }
  321. }
  322. /**
  323. * Gets the full user name for a given user ID
  324. * @param int User ID
  325. * @return string The full username, elements separated by an HTML space
  326. * @deprecated user api_get_user_info($user_id)
  327. */
  328. function GetFullUserName($uid) {
  329. $uid = (int) $uid;
  330. $uid = Database::escape_string($uid);
  331. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  332. $query = "SELECT firstname, lastname FROM ".$user_table." WHERE user_id='$uid'";
  333. $result = @Database::query($query);
  334. if (count($result)>0) {
  335. $str = '';
  336. while(list($firstname,$lastname)= Database::fetch_array($result)) {
  337. $str = str_replace(' ', '&nbsp;', api_get_person_name($firstname, $lastname));
  338. return $str;
  339. }
  340. }
  341. }
  342. /**
  343. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  344. * @param none - taken from global space
  345. * @return string An HTML-formatted message
  346. */
  347. function chatcall() {
  348. global $_user, $_cid;
  349. if (!$_user['user_id']) {
  350. return (false);
  351. }
  352. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  353. $sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = '".$_user['user_id']."' )";
  354. $result=Database::query($sql);
  355. $row=Database::fetch_array($result);
  356. $login_date=$row['chatcall_date'];
  357. $hour = substr($login_date,11,2);
  358. $minute = substr($login_date,14,2);
  359. $secund = substr($login_date,17,2);
  360. $month = substr($login_date,5,2);
  361. $day = substr($login_date,8,2);
  362. $year = substr($login_date,0,4);
  363. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  364. $time = time();
  365. $time = date("Y-m-d H:i:s", $time);
  366. $minute_passed=5; //within this limit, the chat call request is valid
  367. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  368. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  369. $webpath=api_get_path(WEB_CODE_PATH);
  370. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  371. ."<p>"
  372. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  373. . get_lang("Yes")
  374. ."</a>"
  375. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  376. ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
  377. . get_lang("No")
  378. ."</a>"
  379. ."</p>";
  380. return($message);
  381. } else {
  382. return false;
  383. }
  384. }