online.inc.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. $result = @api_sql_query($query,__FILE__,__LINE__);
  89. if (count($result)>0)
  90. {
  91. $rtime = time();
  92. $rdate = date("Y-m-d H:i:s",$rtime);
  93. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  94. $rarray = array();
  95. while(list($login_user_id,$login_date)= Database::fetch_row($result))
  96. {
  97. $barray = array();
  98. array_push($barray,$login_user_id);
  99. array_push($barray,$login_date);
  100. // YYYY-MM-DD HH:MM:SS, db date format
  101. $hour = substr($login_date,11,2);
  102. $minute = substr($login_date,14,2);
  103. $secund = substr($login_date,17,2);
  104. $month = substr($login_date,5,2);
  105. $day = substr($login_date,8,2);
  106. $year = substr($login_date,0,4);
  107. // db timestamp
  108. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  109. if ($dbtime>$validtime)
  110. {
  111. array_push($rarray,$barray);
  112. }
  113. }
  114. return $rarray;
  115. }
  116. else
  117. {
  118. return false;
  119. }
  120. }
  121. /**
  122. * Gets the full user name for a given user ID
  123. * @param int User ID
  124. * @return string The full username, elements separated by an HTML space
  125. */
  126. function GetFullUserName($uid)
  127. {
  128. $uid = (int) $uid;
  129. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  130. $query = "SELECT firstname,lastname FROM ".$user_table." WHERE user_id='$uid'";
  131. $result = @api_sql_query($query,__FILE__,__LINE__);
  132. if (count($result)>0)
  133. {
  134. $str = "";
  135. while(list($firstname,$lastname)= Database::fetch_array($result))
  136. {
  137. $str = $lastname."&nbsp;".$firstname;
  138. return $str;
  139. }
  140. }
  141. }
  142. /**
  143. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  144. * @param none - taken from global space
  145. * @return string An HTML-formatted message
  146. */
  147. function chatcall() {
  148. global $_user, $_cid;
  149. if (!$_user['user_id'])
  150. {
  151. return (false);
  152. }
  153. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  154. $sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = '".$_user['user_id']."' )";
  155. $result=api_sql_query($sql,__FILE__,__LINE__);
  156. $row=Database::fetch_array($result);
  157. $login_date=$row['chatcall_date'];
  158. $hour = substr($login_date,11,2);
  159. $minute = substr($login_date,14,2);
  160. $secund = substr($login_date,17,2);
  161. $month = substr($login_date,5,2);
  162. $day = substr($login_date,8,2);
  163. $year = substr($login_date,0,4);
  164. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  165. $time = time();
  166. $time = date("Y-m-d H:i:s", $time);
  167. $minute_passed=5; //within this limit, the chat call request is valid
  168. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  169. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  170. $webpath=api_get_path(WEB_CODE_PATH);
  171. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  172. ."<p>"
  173. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  174. . get_lang("Yes")
  175. ."</a>"
  176. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  177. ."<a href=\"".api_get_path('WEB_PATH')."webchatdeny.php\">"
  178. . get_lang("No")
  179. ."</a>"
  180. ."</p>";
  181. return($message);
  182. }
  183. else
  184. {
  185. return(false);
  186. }
  187. }
  188. /**
  189. * Returns a list (array) of users who are online and in this course.
  190. * @param int User ID
  191. * @param int Number of minutes
  192. * @param string Course code (could be empty, but then the function returns false)
  193. * @return array Each line gives a user id and a login time
  194. */
  195. function who_is_online_in_this_course($uid, $valid, $coursecode=null)
  196. {
  197. if(empty($coursecode)) return false;
  198. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  199. $coursecode = Database::escape_string($coursecode);
  200. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  201. $result = api_sql_query($query,__FILE__,__LINE__);
  202. if (count($result)>0)
  203. {
  204. $rtime = time();
  205. $rdate = date("Y-m-d H:i:s",$rtime);
  206. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  207. $rarray = array();
  208. while(list($login_user_id,$login_date)= mysql_fetch_row($result))
  209. {
  210. $barray = array();
  211. array_push($barray,$login_user_id);
  212. array_push($barray,$login_date);
  213. // YYYY-MM-DD HH:MM:SS, db date format
  214. $hour = substr($login_date,11,2);
  215. $minute = substr($login_date,14,2);
  216. $secund = substr($login_date,17,2);
  217. $month = substr($login_date,5,2);
  218. $day = substr($login_date,8,2);
  219. $year = substr($login_date,0,4);
  220. // db timestamp
  221. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  222. if ($dbtime >= $validtime)
  223. {
  224. array_push($rarray,$barray);
  225. }
  226. }
  227. return $rarray;
  228. }
  229. else
  230. {
  231. return false;
  232. }
  233. }