fix_online_time.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Automatic fix online time procedure. If a COURSEMANAGER has been idle for $timeLimit
  5. * or more then the procedure adds $extraTime to his logout_course_date.
  6. * @package chamilo.cron
  7. * @author Imanol Losada <imanol.losada@beeznest.com>
  8. */
  9. require_once __DIR__ . '/../inc/global.inc.php';
  10. /**
  11. * Get ids of COURSEMANAGERs that are inside a course right now
  12. * @return array COURSEMANAGER's ids
  13. */
  14. function getTeachersInCourseIds()
  15. {
  16. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  17. $joinStatement = ' JOIN ' . Database::get_main_table(TABLE_MAIN_USER) . ' ON login_user_id = user_id';
  18. return Database::select(
  19. 'login_user_id', $table . $joinStatement,
  20. array(
  21. 'where' => array(
  22. 'c_id IS NOT NULL AND status = ?' => array(
  23. COURSEMANAGER
  24. )
  25. )
  26. )
  27. );
  28. }
  29. /**
  30. * If a COURSEMANAGER has been idle for $timeLimit or more then
  31. * the procedure adds $extraTime to his logout_course_date.
  32. * @param array COURSEMANAGER's ids
  33. * @return void
  34. */
  35. function updateTeachersInCourseIdleForTimeLimit($teachersInCourseIds)
  36. {
  37. $timeLimit = '- 30 minute';
  38. $extraTime = '+ 5 minute';
  39. $utcResult = Database::fetch_array(
  40. Database::query('SELECT UTC_TIMESTAMP')
  41. );
  42. $dataBaseCurrentHour = array_shift($utcResult);
  43. $maximumIdleTimeInCourse = date(
  44. 'Y-m-d H:i:s',
  45. strtotime($dataBaseCurrentHour . ' ' . $timeLimit)
  46. );
  47. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  48. $onLineTrackTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  49. foreach ($teachersInCourseIds as $key => $value) {
  50. $value = array_shift($value);
  51. $logResult = Database::select(
  52. 'course_access_id,logout_course_date',
  53. $table,
  54. array(
  55. 'where' => array(
  56. 'user_id = ?' => array(
  57. $value,
  58. )
  59. ),
  60. 'order' => 'course_access_id DESC',
  61. 'limit' => '1'
  62. )
  63. );
  64. $currentTeacherData = array_shift($logResult);
  65. Database::update(
  66. $table,
  67. array(
  68. 'logout_course_date' => date(
  69. 'Y-m-d H:i:s',
  70. strtotime($currentTeacherData['logout_course_date'] . ' ' . $extraTime)
  71. )
  72. ),
  73. array(
  74. 'user_id = ? AND logout_course_date < ? AND course_access_id = ?' => array(
  75. $value,
  76. $maximumIdleTimeInCourse,
  77. $currentTeacherData['course_access_id']
  78. )
  79. )
  80. );
  81. /*
  82. * (Avoid multiple updates)
  83. * When the user enters a course, this field is updated with the course code.
  84. * And when the user goes to another tool, returns to NULL
  85. */
  86. $userId = intval($value);
  87. $updateOnLineSql = "UPDATE $onLineTrackTable SET "
  88. . "c_id = NULL "
  89. . "WHERE login_user_id = $userId";
  90. Database::query($updateOnLineSql);
  91. }
  92. }
  93. /**
  94. * Initialization
  95. */
  96. if (php_sapi_name() != 'cli') {
  97. exit; //do not run from browser
  98. }
  99. $teachersInCourseIds = getTeachersInCourseIds();
  100. if (!empty($teachersInCourseIds)) {
  101. updateTeachersInCourseIdleForTimeLimit($teachersInCourseIds);
  102. }