fix_course_spent_time.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * This script checks and updates (if you uncomment the query)
  4. * the records in track_e_course_access that is used to calculate the
  5. * total course time.
  6. */
  7. exit;
  8. require_once __DIR__.'/../../main/inc/global.inc.php';
  9. $maxSeconds = 10 * 60 * 60; // Check records higher than X hours
  10. $addSecondsToLogin = 2 * 60 * 60; // Update this abusive records with X hours
  11. $limit = 10; // Only fix first 10
  12. $sendMessage = true;
  13. $userId = 1; // User id that will receive a report
  14. $update = false; // Update and fix the record
  15. $sql = "SELECT
  16. course_access_id,
  17. counter,
  18. UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date) diff,
  19. login_course_date,
  20. logout_course_date
  21. FROM track_e_course_access
  22. WHERE UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date)
  23. ORDER by diff DESC
  24. LIMIT $limit
  25. ";
  26. // contidos
  27. //SELECT course_access_id, counter, (UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) / 60 / 60 diff, login_course_date, logout_course_date FROM track_e_course_access WHERE UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date) order by diff DESC limit 10;
  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 SET logout_course_date = '$newLogout' WHERE course_access_id = $id;";
  39. if ($update) {
  40. Database::query($sql);
  41. }
  42. $report = "Login : $loginDate";
  43. $report .= PHP_EOL;
  44. $report .= "Logout: $logoutDate Diff in hours: $diff";
  45. $report .= PHP_EOL;
  46. $report .= $sql;
  47. $report .= PHP_EOL;
  48. $report .= PHP_EOL;
  49. $log .= $report;
  50. echo $report;
  51. }
  52. }
  53. if ($sendMessage && !empty($log)) {
  54. $log = nl2br($log);
  55. MessageManager::send_message_simple(
  56. $userId,
  57. 'Course time spent fixes',
  58. $log
  59. );
  60. }