fix_lp_id_to_iid.php 8.0 KB

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