check_lp_total_time.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script checks and propose a query fix for LP items with high time values
  5. * Only if the total LP time is bigger than the total course time.
  6. */
  7. exit;
  8. require_once __DIR__.'/../../main/inc/global.inc.php';
  9. api_protect_admin_script();
  10. opcache_reset();
  11. $testSessionId = 182;
  12. $testCourseId = 97;
  13. $max = 10;
  14. $counter = 0;
  15. // Check Sessions
  16. $_configuration['access_url'] = 6;
  17. $sessions = SessionManager::formatSessionsAdminForGrid();
  18. foreach ($sessions as $session) {
  19. $sessionId = $session['id'];
  20. if (!empty($testSessionId)) {
  21. if ($sessionId != $testSessionId) {
  22. continue;
  23. }
  24. }
  25. $courses = SessionManager::getCoursesInSession($sessionId);
  26. foreach ($courses as $courseId) {
  27. if (!empty($testCourseId)) {
  28. if ($testCourseId != $courseId) {
  29. continue;
  30. }
  31. }
  32. $courseInfo = api_get_course_info_by_id($courseId);
  33. $courseCode = $courseInfo['code'];
  34. $users = CourseManager::get_user_list_from_course_code(
  35. $courseCode,
  36. $sessionId,
  37. null,
  38. null,
  39. 0
  40. );
  41. foreach ($users as $user) {
  42. $result = compareLpTimeAndCourseTime($user, $courseInfo, $sessionId);
  43. if ($result) {
  44. $counter++;
  45. }
  46. if ($counter > $max) {
  47. break 3;
  48. }
  49. }
  50. }
  51. }
  52. // Courses
  53. /*$courses = CourseManager::get_courses_list();
  54. foreach($courses as $courseInfo) {
  55. $courseCode = $courseInfo['code'];
  56. $courseInfo['real_id'] = $courseInfo['id'];
  57. $users = CourseManager::get_user_list_from_course_code($courseCode);
  58. foreach ($users as $user) {
  59. $userId = $user['id'];
  60. compareLpTimeAndCourseTime($userId, $courseInfo);
  61. }
  62. }*/
  63. /**
  64. * @param array $user
  65. * @param array $courseInfo
  66. * @param int $sessionId
  67. *
  68. * @return bool
  69. */
  70. function compareLpTimeAndCourseTime($user, $courseInfo, $sessionId = 0)
  71. {
  72. $userId = $user['user_id'];
  73. $defaultValue = 600; // 10 min
  74. $courseCode = $courseInfo['code'];
  75. $courseId = $courseInfo['real_id'];
  76. $totalLpTime = Tracking::get_time_spent_in_lp(
  77. $userId,
  78. $courseCode,
  79. [],
  80. $sessionId
  81. );
  82. if (empty($totalLpTime)) {
  83. return false;
  84. }
  85. $totalCourseTime = Tracking::get_time_spent_on_the_course(
  86. $userId,
  87. $courseId,
  88. $sessionId
  89. );
  90. $content = '';
  91. if ($totalLpTime > $totalCourseTime) {
  92. $totalCourseTimeFormatted = api_time_to_hms($totalCourseTime);
  93. $totalLpTimeFormatted = api_time_to_hms($totalLpTime);
  94. $diff = $totalLpTime - $totalCourseTime;
  95. $content = PHP_EOL."User: ".$user['user_id']." - Total course: $totalCourseTimeFormatted / Total LP: $totalLpTimeFormatted".PHP_EOL;
  96. $content .= PHP_EOL."Diff: ".api_time_to_hms($diff).PHP_EOL;
  97. $url = api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$userId.'&course='.$courseCode.'&id_session='.$sessionId;
  98. $content .= Display::url('Check', $url, ['target' => '_blank']);
  99. $content .= PHP_EOL;
  100. // Check possible records with high values
  101. $sql = "SELECT iv.iid, lp_id, total_time
  102. FROM c_lp_view v
  103. INNER JOIN c_lp_item_view iv
  104. ON (iv.c_id = v.c_id AND v.id = iv.lp_view_id)
  105. WHERE
  106. user_id = $userId AND
  107. v.c_id = $courseId AND
  108. session_id = $sessionId
  109. ORDER BY total_time desc
  110. LIMIT 1
  111. ";
  112. echo $sql.PHP_EOL;
  113. $result = Database::query($sql);
  114. $results = Database::store_result($result, 'ASSOC');
  115. if (!empty($results)) {
  116. $content .= 'Top 1 high lp item times'.PHP_EOL.PHP_EOL;
  117. foreach ($results as $item) {
  118. $lpId = $item['lp_id'];
  119. $link = api_get_path(WEB_CODE_PATH).'mySpace/lp_tracking.php?cidReq='.$courseCode.
  120. '&course='.$courseCode.'&origin=&lp_id='.$lpId.'&student_id='.$userId.'&id_session='.$sessionId;
  121. $content .= "total_time to be reduced = ".api_time_to_hms($item['total_time']).PHP_EOL;
  122. $content .= Display::url('See report before update', $link, ['target' => '_blank']).PHP_EOL;
  123. $content .= "SQL with possible fix:".PHP_EOL;
  124. if ($item['total_time'] < $defaultValue) {
  125. $content .= "Skip because total_time is too short. total_time: ".$item['total_time'].' value to rest'.$defaultValue.PHP_EOL;
  126. continue;
  127. }
  128. $content .= "UPDATE c_lp_item_view SET total_time = total_time - '$defaultValue' WHERE iid = ".$item['iid'].";".PHP_EOL.PHP_EOL;
  129. }
  130. }
  131. }
  132. echo nl2br($content);
  133. return true;
  134. }
  135. exit;