fix_course_spent_time.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script checks and updates (if you uncomment the query)
  5. * the records in track_e_course_access that is used to calculate the
  6. * total course time.
  7. */
  8. require_once __DIR__.'/../../main/inc/global.inc.php';
  9. exit;
  10. opcache_reset();
  11. $maxSeconds = 10 * 60 * 60; // Check records higher than 7 hours
  12. $addSecondsToLogin = 2 * 60 * 60; // Update this abusive records with 3 hours
  13. $limit = 10; // Only fix first 10
  14. $sendMessage = true;
  15. $userId = 868; // User id that will receive a report
  16. $update = false;
  17. $sql = "SELECT
  18. course_access_id,
  19. counter,
  20. UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date) diff,
  21. login_course_date,
  22. logout_course_date
  23. FROM track_e_course_access
  24. WHERE UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date)
  25. ORDER by diff DESC
  26. LIMIT $limit
  27. ";
  28. $result = Database::query($sql);
  29. $log = '';
  30. while ($row = Database::fetch_array($result)) {
  31. if ($row['diff'] >= $maxSeconds) {
  32. $id = $row['course_access_id'];
  33. $loginDate = $row['login_course_date'];
  34. $logoutDate = $row['logout_course_date'];
  35. $diff = round($row['diff'] / 60 / 60);
  36. $login = api_strtotime($row['login_course_date'], 'UTC') + $addSecondsToLogin;
  37. $newLogout = api_get_utc_datetime($login);
  38. $sql = "UPDATE track_e_course_access
  39. SET logout_course_date = '$newLogout'
  40. WHERE course_access_id = $id ;
  41. ";
  42. // Uncomment to fix
  43. if ($update) {
  44. Database::query($sql);
  45. }
  46. $report = "Login : $loginDate ";
  47. $report .= PHP_EOL;
  48. $report .= "Logout: $logoutDate Diff in hours: $diff";
  49. $report .= PHP_EOL;
  50. $report .= $sql;
  51. $report .= PHP_EOL;
  52. $report .= PHP_EOL;
  53. $log .= $report;
  54. echo $report;
  55. }
  56. }
  57. if ($sendMessage && !empty($log)) {
  58. $log = nl2br($log);
  59. MessageManager::send_message_simple(
  60. $userId,
  61. 'Course time spent fixes',
  62. $log,
  63. 1
  64. );
  65. }