active.php 3.3 KB

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