online.inc.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) Istvan Mandak
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * Code library for showing Who is online
  21. *
  22. * @author Istvan Mandak, principal author
  23. * @author Denes Nagy, principal author
  24. * @author Bart Mollet
  25. * @author Roan Embrechts, cleaning and bugfixing
  26. * @package dokeos.whoisonline
  27. ==============================================================================
  28. */
  29. /**
  30. * Insert a login reference for the current user into the track_e_online stats table.
  31. * This table keeps trace of the last login. Nothing else matters (we don't keep traces of anything older)
  32. * @param int user id
  33. * @return void
  34. */
  35. function LoginCheck($uid)
  36. {
  37. global $_course;
  38. $uid = (int) $uid;
  39. $online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  40. if (!empty($uid))
  41. {
  42. $login_ip = '';
  43. if(!empty($_SERVER['REMOTE_ADDR']))
  44. {
  45. $login_ip = Database::escape_string($_SERVER['REMOTE_ADDR']);
  46. }
  47. $reallyNow = time();
  48. $login_date = date("Y-m-d H:i:s",$reallyNow);
  49. // 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
  50. // to have the x users in this course feature working
  51. if (is_array($_course) && count($_course)>0 && !empty($_course['id']))
  52. {
  53. $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,login_ip, course) VALUES ($uid,$uid,'$login_date','$login_ip', '".$_course['id']."')";
  54. }
  55. else
  56. {
  57. $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,login_ip) VALUES ($uid,$uid,'$login_date','$login_ip')";
  58. }
  59. @api_sql_query($query,__FILE__,__LINE__);
  60. }
  61. }
  62. /**
  63. * Remove all login records from the track_e_online stats table, for the given user ID.
  64. * @param int User ID
  65. * @return void
  66. */
  67. function LoginDelete($user_id)
  68. {
  69. $online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  70. $user_id = (int) $user_id;
  71. $query = "DELETE FROM ".$online_table ." WHERE login_user_id = '".$user_id."'";
  72. @api_sql_query($query,__FILE__,__LINE__);
  73. }
  74. /**
  75. * Gives a list of people online now (and in the last $valid minutes)
  76. * @param int User ID - useless
  77. * @param string Statistics database name - useless
  78. * @param int Number of minutes to account logins for
  79. * @return array For each line, a list of user IDs and login dates, or FALSE on error or empty results
  80. * @todo remove parameter $statistics_database which is no longer necessary
  81. */
  82. function WhoIsOnline($uid=0,$statistics_database='',$valid)
  83. {
  84. $valid = (int) $valid;
  85. $current_date=date('Y-m-d H:i:s',time());
  86. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  87. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ";
  88. global $_configuration;
  89. if ($_configuration['multiple_access_urls']==true) {
  90. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  91. $access_url_id = api_get_current_access_url_id();
  92. if ($access_url_id != -1){
  93. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." track
  94. INNER JOIN $tbl_user_rel_access_url user_rel_url
  95. ON (user_rel_url.user_id = track.login_user_id)
  96. WHERE access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ";
  97. }
  98. }
  99. $result = @api_sql_query($query,__FILE__,__LINE__);
  100. if (count($result)>0)
  101. {
  102. $rtime = time();
  103. $rdate = date("Y-m-d H:i:s",$rtime);
  104. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  105. $rarray = array();
  106. while(list($login_user_id,$login_date)= Database::fetch_row($result))
  107. {
  108. $barray = array();
  109. array_push($barray,$login_user_id);
  110. array_push($barray,$login_date);
  111. // YYYY-MM-DD HH:MM:SS, db date format
  112. $hour = substr($login_date,11,2);
  113. $minute = substr($login_date,14,2);
  114. $secund = substr($login_date,17,2);
  115. $month = substr($login_date,5,2);
  116. $day = substr($login_date,8,2);
  117. $year = substr($login_date,0,4);
  118. // db timestamp
  119. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  120. if ($dbtime>$validtime)
  121. {
  122. array_push($rarray,$barray);
  123. }
  124. }
  125. return $rarray;
  126. }
  127. else
  128. {
  129. return false;
  130. }
  131. }
  132. /**
  133. * Gets the full user name for a given user ID
  134. * @param int User ID
  135. * @return string The full username, elements separated by an HTML space
  136. */
  137. function GetFullUserName($uid)
  138. {
  139. $uid = (int) $uid;
  140. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  141. $query = "SELECT firstname,lastname FROM ".$user_table." WHERE user_id='$uid'";
  142. $result = @api_sql_query($query,__FILE__,__LINE__);
  143. if (count($result)>0)
  144. {
  145. $str = "";
  146. while(list($firstname,$lastname)= Database::fetch_array($result))
  147. {
  148. $str = $lastname."&nbsp;".$firstname;
  149. return $str;
  150. }
  151. }
  152. }
  153. /**
  154. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  155. * @param none - taken from global space
  156. * @return string An HTML-formatted message
  157. */
  158. function chatcall() {
  159. global $_user, $_cid;
  160. if (!$_user['user_id'])
  161. {
  162. return (false);
  163. }
  164. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  165. $sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = '".$_user['user_id']."' )";
  166. $result=api_sql_query($sql,__FILE__,__LINE__);
  167. $row=Database::fetch_array($result);
  168. $login_date=$row['chatcall_date'];
  169. $hour = substr($login_date,11,2);
  170. $minute = substr($login_date,14,2);
  171. $secund = substr($login_date,17,2);
  172. $month = substr($login_date,5,2);
  173. $day = substr($login_date,8,2);
  174. $year = substr($login_date,0,4);
  175. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  176. $time = time();
  177. $time = date("Y-m-d H:i:s", $time);
  178. $minute_passed=5; //within this limit, the chat call request is valid
  179. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  180. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  181. $webpath=api_get_path(WEB_CODE_PATH);
  182. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  183. ."<p>"
  184. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  185. . get_lang("Yes")
  186. ."</a>"
  187. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  188. ."<a href=\"".api_get_path('WEB_PATH')."webchatdeny.php\">"
  189. . get_lang("No")
  190. ."</a>"
  191. ."</p>";
  192. return($message);
  193. }
  194. else
  195. {
  196. return(false);
  197. }
  198. }
  199. /**
  200. * Returns a list (array) of users who are online and in this course.
  201. * @param int User ID
  202. * @param int Number of minutes
  203. * @param string Course code (could be empty, but then the function returns false)
  204. * @return array Each line gives a user id and a login time
  205. */
  206. function who_is_online_in_this_course($uid, $valid, $coursecode=null)
  207. {
  208. if(empty($coursecode)) return false;
  209. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  210. $coursecode = Database::escape_string($coursecode);
  211. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  212. $result = api_sql_query($query,__FILE__,__LINE__);
  213. if (count($result)>0)
  214. {
  215. $rtime = time();
  216. $rdate = date("Y-m-d H:i:s",$rtime);
  217. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  218. $rarray = array();
  219. while(list($login_user_id,$login_date)= mysql_fetch_row($result))
  220. {
  221. $barray = array();
  222. array_push($barray,$login_user_id);
  223. array_push($barray,$login_date);
  224. // YYYY-MM-DD HH:MM:SS, db date format
  225. $hour = substr($login_date,11,2);
  226. $minute = substr($login_date,14,2);
  227. $secund = substr($login_date,17,2);
  228. $month = substr($login_date,5,2);
  229. $day = substr($login_date,8,2);
  230. $year = substr($login_date,0,4);
  231. // db timestamp
  232. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  233. if ($dbtime >= $validtime)
  234. {
  235. array_push($rarray,$barray);
  236. }
  237. }
  238. return $rarray;
  239. }
  240. else
  241. {
  242. return false;
  243. }
  244. }