fix_course_spent_time.php 2.3 KB

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