active.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. //统计用户经验值
  3. require_once '../path.php';
  4. require_once "../public/function.php";
  5. require_once "../public/php/define.php";
  6. function add_edit_event($type=0,$data=null){
  7. date_default_timezone_set("UTC");
  8. define("MAX_INTERVAL",600000);
  9. define("MIN_INTERVAL",10000);
  10. if(isset($_COOKIE["userid"])){
  11. $dns = "sqlite:"._FILE_DB_USER_ACTIVE_;
  12. $dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  13. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  14. // 查询上次编辑活跃结束时间
  15. $query = "SELECT id, end, start,hit FROM edit WHERE user_id = ? order by end DESC";
  16. $stmt = $dbh->prepare($query);
  17. $stmt->execute(array($_COOKIE["userid"]));
  18. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  19. $new_record = false;
  20. $currTime = mTime();
  21. if ($row) {
  22. //找到,判断是否超时,超时新建,未超时修改
  23. $id = (int)$row["id"];
  24. $start_time = (int)$row["start"];
  25. $endtime = (int)$row["end"];
  26. $hit = (int)$row["hit"];
  27. if($currTime-$endtime>MAX_INTERVAL){
  28. //超时新建
  29. $new_record = true;
  30. }
  31. else{
  32. //未超时修改
  33. $new_record = false;
  34. }
  35. } else {
  36. //没找到,新建
  37. $new_record = true;
  38. }
  39. #获取客户端时区偏移 beijing = +8
  40. if(isset($_COOKIE["timezone"])){
  41. $client_timezone = (0-(int)$_COOKIE["timezone"])*60*1000;
  42. }
  43. else{
  44. $client_timezone = 0;
  45. }
  46. $this_active_time=0;//时间增量
  47. if($new_record){
  48. #新建
  49. $query="INSERT INTO edit ( user_id, start , end , duration , hit , timezone ) VALUES ( ? , ? , ? , ? , ? ,?) ";
  50. $sth = $dbh->prepare($query);
  51. #最小思考时间
  52. $sth->execute(array($_COOKIE["userid"] , ($currTime-MIN_INTERVAL) ,$currTime , MIN_INTERVAL,1 ,$client_timezone ) );
  53. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  54. $error = $dbh->errorInfo();
  55. }
  56. $this_active_time=MIN_INTERVAL;
  57. }
  58. else{
  59. #修改
  60. $this_active_time=$currTime-$endtime;
  61. $query="UPDATE edit SET end = ? , duration = ? , hit = ? WHERE id = ? ";
  62. $sth = $dbh->prepare($query);
  63. $sth->execute( array($currTime,($currTime-$start_time), ($hit+1),$id));
  64. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  65. $error = $dbh->errorInfo();
  66. }
  67. }
  68. #更新经验总量表
  69. #计算客户端日期 unix时间戳 以毫秒计
  70. $client_currtime = $currTime + $client_timezone;
  71. $client_date = strtotime(gmdate("Y-m-d",$client_currtime/1000))*1000;
  72. #查询是否存在
  73. $query = "SELECT id,duration,hit FROM active_index WHERE user_id = ? and date = ?";
  74. $sth = $dbh->prepare($query);
  75. $sth->execute(array($_COOKIE["userid"],$client_date));
  76. $row = $sth->fetch(PDO::FETCH_ASSOC);
  77. if ($row) {
  78. #更新
  79. $id = (int)$row["id"];
  80. $duration = (int)$row["duration"];
  81. $hit = (int)$row["hit"];
  82. #修改
  83. $query="UPDATE active_index SET duration = ? , hit = ? WHERE id = ? ";
  84. $sth = $dbh->prepare($query);
  85. $sth->execute( array(($duration+$this_active_time), ($hit+1),$id));
  86. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  87. $error = $dbh->errorInfo();
  88. }
  89. }
  90. else{
  91. #新建
  92. $query="INSERT INTO active_index ( user_id, date , duration , hit ) VALUES ( ? , ? , ? , ? ) ";
  93. $sth = $dbh->prepare($query);
  94. #最小思考时间
  95. $sth->execute(array($_COOKIE["userid"] , $client_date , MIN_INTERVAL,1 ) );
  96. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  97. $error = $dbh->errorInfo();
  98. }
  99. }
  100. #更新经验总量表结束
  101. #更新log
  102. if($type > 0){
  103. $dns = "sqlite:"._FILE_DB_USER_ACTIVE_LOG_;
  104. $dbh_log = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  105. $dbh_log->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  106. $query="INSERT INTO log ( user_id, active , content , time,timezone ) VALUES ( ? , ? , ? , ? ,? ) ";
  107. $sth = $dbh_log->prepare($query);
  108. $sth->execute(array($_COOKIE["uid"] , $type , $data, $currTime,$client_timezone ) );
  109. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  110. $error = $dbh->errorInfo();
  111. }
  112. }
  113. #更新log结束
  114. }
  115. }
  116. ?>