fix_lp_id_to_iid.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * This script fixes use of id instead of iid for the learning path
  4. */
  5. exit;
  6. require_once '../../main/inc/global.inc.php';
  7. /** @var int $courseId */
  8. $onlyCourseId = 0;
  9. /** @var int $lpId lp id */
  10. $lpId = 0;
  11. $courses = Database::select('id, title, code', Database::get_main_table(TABLE_MAIN_COURSE));
  12. $tblCLp = Database::get_course_table(TABLE_LP_MAIN);
  13. $tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
  14. $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
  15. $sessions = Database::select('id', Database::get_main_table(TABLE_MAIN_SESSION));
  16. if (!empty($sessions)) {
  17. $sessions = array_column($sessions, 'id');
  18. // Add session_id = 0
  19. $sessions[] = 0;
  20. } else {
  21. $sessions = [0];
  22. }
  23. foreach ($courses as $course) {
  24. if (!empty($onlyCourseId)) {
  25. if ($onlyCourseId != $course['id']) {
  26. continue;
  27. }
  28. }
  29. $courseId = $course['id'];
  30. $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId AND iid <> id ORDER by iid";
  31. echo 'Select all lps';
  32. var_dump($sql);
  33. error_log($sql);
  34. $result = Database::query($sql);
  35. $myOnlyLpList = [];
  36. if (Database::num_rows($result)) {
  37. while ($lpInfo = Database::fetch_array($result, 'ASSOC')) {
  38. $lpIid = $lpInfo['iid'];
  39. $oldId = $lpInfo['id'];
  40. $sql = "SELECT * FROM $tblCLpItem
  41. WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
  42. //echo "<h3>$sql</h3>";
  43. //echo "New lp.iid $lpIid / old lp.id $oldId";
  44. $items = Database::store_result(Database::query($sql),'ASSOC');
  45. $lpInfo['lp_list'] = $items;
  46. $myOnlyLpList[] = $lpInfo;
  47. }
  48. }
  49. if (!empty($myOnlyLpList)) {
  50. foreach ($myOnlyLpList as $lpInfo) {
  51. $lpIid = $lpInfo['iid'];
  52. $oldId = $lpInfo['id'];
  53. if (!empty($lpId)) {
  54. if ($lpId != $oldId) {
  55. continue;
  56. }
  57. }
  58. if (empty($lpInfo['lp_list'])) {
  59. continue;
  60. }
  61. $items = $lpInfo['lp_list'];
  62. $itemList = [];
  63. foreach ($items as $subItem) {
  64. $itemList[$subItem['id']] = $subItem['iid'];
  65. }
  66. $variablesToFix = [
  67. 'parent_item_id',
  68. 'next_item_id',
  69. 'prerequisite',
  70. 'previous_item_id'
  71. ];
  72. foreach ($sessions as $sessionId) {
  73. $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
  74. $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
  75. $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
  76. $sql = "UPDATE $toolTable
  77. SET link = '$correctLink'
  78. WHERE c_id = $courseId AND (link = '$link' OR link ='$secondLink')";
  79. Database::query($sql);
  80. }
  81. foreach ($items as $item) {
  82. $itemIid = $item['iid'];
  83. $itemId = $item['id'];
  84. foreach ($variablesToFix as $variable) {
  85. if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
  86. $newId = $itemList[$item[$variable]];
  87. $sql = "UPDATE $tblCLpItem SET $variable = $newId
  88. WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
  89. Database::query($sql);
  90. //var_dump($sql);
  91. }
  92. }
  93. if ($item['item_type'] == 'document' && !empty($item['path'])) {
  94. $oldDocumentId = $item['path'];
  95. $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $oldDocumentId";
  96. $resultDocument = Database::query($sql);
  97. if (Database::num_rows($resultDocument)) {
  98. $document = Database::fetch_array($resultDocument, 'ASSOC');
  99. $newDocumentId = $document['iid'];
  100. if (!empty($newDocumentId)) {
  101. $sql = "UPDATE $tblCLpItem SET path = $newDocumentId
  102. WHERE iid = $itemIid AND c_id = $courseId";
  103. Database::query($sql);
  104. //var_dump($sql);
  105. }
  106. }
  107. }
  108. // c_lp_view
  109. $sql = "UPDATE c_lp_view SET last_item = $itemIid
  110. WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
  111. Database::query($sql);
  112. //var_dump($sql);
  113. // c_lp_item_view
  114. $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid
  115. WHERE c_id = $courseId AND lp_item_id = $itemId ";
  116. Database::query($sql);
  117. //var_dump($sql);
  118. // Update track_exercises
  119. $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid
  120. WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
  121. Database::query($sql);
  122. //var_dump($sql);
  123. // c_forum_thread
  124. $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid
  125. WHERE c_id = $courseId AND lp_item_id = $itemId";
  126. Database::query($sql);
  127. //var_dump($sql);
  128. // orig_lp_item_view_id
  129. $sql = "SELECT * FROM c_lp_view
  130. WHERE c_id = $courseId AND lp_id = $oldId";
  131. $itemViewList = Database::store_result(Database::query($sql),'ASSOC');
  132. if ($itemViewList) {
  133. foreach ($itemViewList as $itemView) {
  134. $userId = $itemView['user_id'];
  135. $oldItemViewId = $itemView['id'];
  136. $newItemView = $itemView['iid'];
  137. if (empty($oldItemViewId)) {
  138. continue;
  139. }
  140. $sql = "UPDATE track_e_exercises
  141. SET orig_lp_item_view_id = $newItemView
  142. WHERE
  143. c_id = $courseId AND
  144. orig_lp_id = $oldId AND
  145. orig_lp_item_id = $itemIid AND
  146. orig_lp_item_view_id = $oldItemViewId AND
  147. exe_user_id = $userId
  148. ";
  149. Database::query($sql);
  150. //var_dump($sql);
  151. }
  152. }
  153. $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid
  154. WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
  155. Database::query($sql);
  156. //var_dump($sql);
  157. $sql = "UPDATE $tblCLpItem SET id = iid
  158. WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
  159. Database::query($sql);
  160. //var_dump($sql);
  161. }
  162. $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
  163. Database::query($sql);
  164. //var_dump($sql);
  165. $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
  166. Database::query($sql);
  167. //var_dump($sql);
  168. // Update track_exercises
  169. $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid
  170. WHERE c_id = $courseId AND orig_lp_id = $oldId";
  171. Database::query($sql);
  172. //var_dump($sql);
  173. $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
  174. Database::query($sql);
  175. //var_dump($sql);
  176. }
  177. }
  178. }
  179. echo 'finished';
  180. error_log('finished');