active.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. {
  8. date_default_timezone_set("UTC");
  9. define("MAX_INTERVAL", 600000);
  10. define("MIN_INTERVAL", 60000);
  11. if (isset($_COOKIE["userid"])) {
  12. $dns = "" . _FILE_DB_USER_ACTIVE_;
  13. $dbh = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  14. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  15. // 查询上次编辑活跃结束时间
  16. $query = "SELECT id, end, start,hit FROM edit WHERE user_id = ? order by end DESC";
  17. $stmt = $dbh->prepare($query);
  18. $stmt->execute(array($_COOKIE["userid"]));
  19. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  20. $new_record = false;
  21. $currTime = mTime();
  22. if ($row) {
  23. //找到,判断是否超时,超时新建,未超时修改
  24. $id = (int) $row["id"];
  25. $start_time = (int) $row["start"];
  26. $endtime = (int) $row["end"];
  27. $hit = (int) $row["hit"];
  28. if ($currTime - $endtime > MAX_INTERVAL) {
  29. //超时新建
  30. $new_record = true;
  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. } 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. } else {
  57. #修改
  58. $this_active_time = $currTime - $endtime;
  59. $query = "UPDATE edit SET end = ? , duration = ? , hit = ? WHERE id = ? ";
  60. $sth = $dbh->prepare($query);
  61. $sth->execute(array($currTime, ($currTime - $start_time), ($hit + 1), $id));
  62. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  63. $error = $dbh->errorInfo();
  64. }
  65. }
  66. #更新经验总量表
  67. #计算客户端日期 unix时间戳 以毫秒计
  68. $client_currtime = $currTime + $client_timezone;
  69. $client_date = strtotime(gmdate("Y-m-d", $client_currtime / 1000)) * 1000;
  70. #查询是否存在
  71. $query = "SELECT id,duration,hit FROM active_index WHERE user_id = ? and date = ?";
  72. $sth = $dbh->prepare($query);
  73. $sth->execute(array($_COOKIE["userid"], $client_date));
  74. $row = $sth->fetch(PDO::FETCH_ASSOC);
  75. if ($row) {
  76. #更新
  77. $id = (int) $row["id"];
  78. $duration = (int) $row["duration"];
  79. $hit = (int) $row["hit"];
  80. #修改
  81. $query = "UPDATE active_index SET duration = ? , hit = ? WHERE id = ? ";
  82. $sth = $dbh->prepare($query);
  83. $sth->execute(array(($duration + $this_active_time), ($hit + 1), $id));
  84. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  85. $error = $dbh->errorInfo();
  86. }
  87. } else {
  88. #新建
  89. $query = "INSERT INTO active_index ( user_id, date , duration , hit ) VALUES ( ? , ? , ? , ? ) ";
  90. $sth = $dbh->prepare($query);
  91. #最小思考时间
  92. $sth->execute(array($_COOKIE["userid"], $client_date, MIN_INTERVAL, 1));
  93. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  94. $error = $dbh->errorInfo();
  95. }
  96. }
  97. #更新经验总量表结束
  98. #更新log
  99. if ($type > 0) {
  100. $dns = "" . _FILE_DB_USER_ACTIVE_LOG_;
  101. $dbh_log = new PDO($dns, "", "", array(PDO::ATTR_PERSISTENT => true));
  102. $dbh_log->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  103. $query = "INSERT INTO log ( user_id, active , content , time , timezone ) VALUES ( ? , ? , ? , ? ,? ) ";
  104. $sth = $dbh_log->prepare($query);
  105. $sth->execute(array($_COOKIE["uid"], $type, $data, $currTime, $client_timezone));
  106. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  107. $error = $dbh->errorInfo();
  108. }
  109. }
  110. #更新log结束
  111. }
  112. }