fix_online_time.php 4.3 KB

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