fix_exercise_score_in_lp.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script synchronize the exercise score (track_e_exercises.exe_result)
  5. * with the LP score result (lp_item_view.score).
  6. * This script works only if 1 there's one attempt
  7. */
  8. exit;
  9. require_once '../../main/inc/global.inc.php';
  10. api_protect_admin_script();
  11. $tableExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  12. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  13. $sql = "SELECT
  14. exe_id,
  15. exe_result,
  16. exe_user_id,
  17. exe_result,
  18. exe_exo_id,
  19. orig_lp_id,
  20. orig_lp_item_view_id,
  21. c_id,
  22. c.code,
  23. c.id real_id,
  24. session_id
  25. FROM $tableExercise t INNER JOIN $tableCourse c
  26. ON c.id = t.c_id
  27. WHERE orig_lp_id != '' AND orig_lp_item_view_id = 0 AND status = ''
  28. ORDER by session_id, c_id, exe_user_id, orig_lp_id, exe_exo_id
  29. ";
  30. $result = Database::query($sql);
  31. $items = Database::store_result($result, 'ASSOC');
  32. if (!empty($items)) {
  33. foreach ($items as $item) {
  34. $exeId = $item['exe_id'];
  35. $lpId = $item['orig_lp_id'];
  36. $userId = $item['exe_user_id'];
  37. $courseId = $item['real_id'];
  38. $courseCode = $item['code'];
  39. $sessionId = $item['session_id'];
  40. $url = api_get_path(WEB_CODE_PATH)."mySpace/myStudents.php?student=$userId&details=true&course=$courseCode&origin=&id_session=$sessionId";
  41. echo "Check user page: ". Display::url($url, $url);
  42. echo '<br />';
  43. $lp = new learnpath($item['code'], $lpId, $userId);
  44. /** @var learnpathItem $lpItem */
  45. foreach ($lp->items as $lpItem) {
  46. if ($lpItem->get_type() == 'quiz' &&
  47. $lpItem->get_path() == $item['exe_exo_id']
  48. ) {
  49. $lpItemId = $lpItem->get_id();
  50. $table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  51. $tableView = Database::get_course_table(TABLE_LP_VIEW);
  52. $sql = "SELECT *, iv.id lp_item_view_id FROM $table iv INNER JOIN $tableView v
  53. ON iv.c_id = v.c_id AND iv.lp_view_id = v.id
  54. WHERE
  55. lp_item_id = $lpItemId AND
  56. iv.c_id = $courseId AND
  57. status = 'completed' AND
  58. user_id = $userId AND
  59. lp_id = $lpId AND
  60. session_id = $sessionId
  61. ";
  62. $result = Database::query($sql);
  63. $attempts = Database::store_result($result, 'ASSOC');
  64. var_dump($sql);
  65. echo '<br />';
  66. $count = count($attempts);
  67. if ($count == 1) {
  68. $attempt = current($attempts);
  69. $score = $item['exe_result'];
  70. /* The attempt has empty exe_result and the LP is good
  71. there must be another attempt, do nothing. */
  72. if ((empty($item['exe_result']) || $item['exe_result'] == 0) && !empty($attempt['score'])) {
  73. var_dump('Skipped');
  74. echo '<br />';
  75. continue;
  76. }
  77. echo "Score: ".$attempt['score']. ' - '.$item['exe_result'].'<br />';
  78. $itemViewId = $attempt['lp_item_view_id'];
  79. $sql = "UPDATE $table SET
  80. score = $score
  81. WHERE
  82. id = $itemViewId AND
  83. lp_item_id = $lpItemId AND
  84. c_id = $courseId AND
  85. ";
  86. //Database::query($sql);
  87. var_dump($sql);
  88. echo '<br />';
  89. $sql = "UPDATE $tableExercise
  90. SET orig_lp_item_view_id = $itemViewId
  91. WHERE exe_id = $exeId";
  92. //Database::query($sql);
  93. var_dump($sql);
  94. echo '<br />';
  95. } else {
  96. echo 'Cannot update multiple attempts checking attempts:<br />';
  97. foreach ($attempts as $attempt) {
  98. if ($attempt['score'] == $item['exe_result']) {
  99. /*echo "Score: ".$attempt['score']. ' - '.$item['exe_result'].'<br />';
  100. $itemViewId = $attempt['id'];
  101. $sql = "UPDATE $tableExercise
  102. SET orig_lp_item_view_id = $itemViewId
  103. WHERE exe_id = $exeId";
  104. //Database::query($sql);
  105. var_dump($sql);
  106. echo '<br />';*/
  107. }
  108. }
  109. }
  110. }
  111. }
  112. echo '<br />';
  113. }
  114. }