fix_exercise_score_in_lp.php 4.9 KB

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