fix_online_time.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Automatic fix online time procedure. If a user has been idle for $timeLimit
  5. * or more then the procedure adds $extraTime to his logout_course_date.
  6. *
  7. * How to use it:
  8. *
  9. * By default the script will fix only users with teacher status (COURSEMANAGER)
  10. *
  11. * php main/cron/fix_online_time.php
  12. *
  13. * If you want to add an specific status you can call the file with:
  14. *
  15. * php main/cron/fix_online_time.php status=5
  16. *
  17. * Where "5" is the value of the 'STUDENT' constant.
  18. *
  19. * @package chamilo.cron
  20. *
  21. * @author Imanol Losada <imanol.losada@beeznest.com>
  22. * @author Julio Montoya
  23. */
  24. require_once __DIR__.'/../inc/global.inc.php';
  25. if (php_sapi_name() != 'cli') {
  26. exit; //do not run from browser
  27. }
  28. /**
  29. * Get ids of users that are inside a course right now.
  30. *
  31. * @param int $status COURSEMANAGER|STUDENT constants
  32. *
  33. * @return array user id
  34. */
  35. function getUsersInCourseIds($status)
  36. {
  37. $status = (int) $status;
  38. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  39. $joinStatement = ' JOIN '.Database::get_main_table(TABLE_MAIN_USER).' ON login_user_id = user_id';
  40. return Database::select(
  41. 'login_user_id',
  42. $table.$joinStatement,
  43. [
  44. 'where' => [
  45. 'c_id IS NOT NULL AND status = ?' => [
  46. $status,
  47. ],
  48. ],
  49. ]
  50. );
  51. }
  52. /**
  53. * If a user has been idle for $timeLimit or more then
  54. * the procedure adds $extraTime to his logout_course_date.
  55. *
  56. * @param array $users user id list
  57. *
  58. * @return int
  59. */
  60. function updateUsersInCourseIdleForTimeLimit($users)
  61. {
  62. $timeLimit = '- 30 minute';
  63. $extraTime = '+ 5 minute';
  64. $utcResult = Database::fetch_array(Database::query('SELECT UTC_TIMESTAMP'));
  65. $dataBaseCurrentHour = array_shift($utcResult);
  66. $maximumIdleTimeInCourse = date(
  67. 'Y-m-d H:i:s',
  68. strtotime($dataBaseCurrentHour.' '.$timeLimit)
  69. );
  70. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  71. $onLineTrackTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  72. $count = 1;
  73. foreach ($users as $key => $value) {
  74. $value = array_shift($value);
  75. $logResult = Database::select(
  76. 'course_access_id, logout_course_date',
  77. $table,
  78. [
  79. 'where' => [
  80. 'user_id = ?' => [
  81. $value,
  82. ],
  83. ],
  84. 'order' => 'course_access_id DESC',
  85. 'limit' => '1',
  86. ]
  87. );
  88. $currentTeacherData = array_shift($logResult);
  89. Database::update(
  90. $table,
  91. [
  92. 'logout_course_date' => date(
  93. 'Y-m-d H:i:s',
  94. strtotime($currentTeacherData['logout_course_date'].' '.$extraTime)
  95. ),
  96. ],
  97. [
  98. 'user_id = ? AND logout_course_date < ? AND course_access_id = ?' => [
  99. $value,
  100. $maximumIdleTimeInCourse,
  101. $currentTeacherData['course_access_id'],
  102. ],
  103. ]
  104. );
  105. /*
  106. * (Avoid multiple updates)
  107. * When the user enters a course, this field is updated with the course code.
  108. * And when the user goes to another tool, returns to NULL
  109. */
  110. $userId = intval($value);
  111. $sql = "UPDATE $onLineTrackTable
  112. SET c_id = NULL
  113. WHERE login_user_id = $userId";
  114. Database::query($sql);
  115. $count++;
  116. }
  117. return $count;
  118. }
  119. // Default status when running script
  120. $status = COURSEMANAGER;
  121. if (!empty($argv) && isset($argv[1])) {
  122. // Get status from parameter
  123. parse_str($argv[1], $params);
  124. if (isset($params['status'])) {
  125. $status = (int) $params['status'];
  126. }
  127. }
  128. $statusToString = api_get_status_langvars();
  129. if (isset($statusToString[$status])) {
  130. echo "Fixing users with status: '$status' = ".$statusToString[$status].PHP_EOL;
  131. } else {
  132. echo "User status not '$status' not found. Try with status=1 or status=5";
  133. exit;
  134. }
  135. $users = getUsersInCourseIds($status);
  136. if (!empty($users)) {
  137. $result = updateUsersInCourseIdleForTimeLimit($users);
  138. echo "# users updated: $result".PHP_EOL;
  139. } else {
  140. echo "Nothing to update. No users found to be fixed.".PHP_EOL;
  141. }