fix_lp_id_to_iid_v2.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // Start custom changes
  17. // Delete inconsistencies from old base
  18. $sql = 'DELETE FROM c_lp_item_view WHERE c_id = 0';
  19. Database::query($sql);
  20. var_dump($sql);
  21. error_log($sql);
  22. // This is a custom change, probably you don't needed it in your script (removing an empty attempt)
  23. $sql = 'DELETE FROM c_lp_item_view WHERE lp_view_id = 18 and c_id = 4';
  24. Database::query($sql);
  25. var_dump($sql);
  26. error_log($sql);
  27. $sql = 'DELETE FROM c_lp_view where id = 18 and c_id = 4';
  28. Database::query($sql);
  29. var_dump($sql);
  30. error_log($sql);
  31. ///update c_lp_item_view set status = 'not attempted', suspend_data = null where iid = 2148;
  32. // end custom changes
  33. $sessions = Database::select('id', Database::get_main_table(TABLE_MAIN_SESSION));
  34. if (!empty($sessions)) {
  35. $sessions = array_column($sessions, 'id');
  36. // Add session_id = 0
  37. $sessions[] = 0;
  38. } else {
  39. $sessions = [0];
  40. }
  41. foreach ($courses as $course) {
  42. if (!empty($onlyCourseId)) {
  43. if ($onlyCourseId != $course['id']) {
  44. continue;
  45. }
  46. }
  47. $courseId = $course['id'];
  48. $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId ORDER by iid";
  49. echo 'Select all lps';
  50. var_dump($sql);
  51. error_log($sql);
  52. $result = Database::query($sql);
  53. $myOnlyLpList = [];
  54. if (Database::num_rows($result)) {
  55. while ($lpInfo = Database::fetch_array($result, 'ASSOC')) {
  56. $lpIid = $lpInfo['iid'];
  57. $oldId = $lpInfo['id'];
  58. $sql = "SELECT * FROM $tblCLpItem
  59. WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
  60. //echo "<h3>$sql</h3>";
  61. //echo "New lp.iid $lpIid / old lp.id $oldId";
  62. $items = Database::store_result(Database::query($sql),'ASSOC');
  63. $lpInfo['lp_list'] = $items;
  64. $myOnlyLpList[] = $lpInfo;
  65. }
  66. }
  67. if (!empty($myOnlyLpList)) {
  68. foreach ($myOnlyLpList as $lpInfo) {
  69. $lpIid = $lpInfo['iid'];
  70. $oldId = $lpInfo['id'];
  71. if (!empty($lpId)) {
  72. if ($lpId != $oldId) {
  73. continue;
  74. }
  75. }
  76. if (empty($lpInfo['lp_list'])) {
  77. continue;
  78. }
  79. $items = $lpInfo['lp_list'];
  80. $itemList = [];
  81. foreach ($items as $subItem) {
  82. $itemList[$subItem['id']] = $subItem['iid'];
  83. }
  84. $variablesToFix = [
  85. 'parent_item_id',
  86. 'next_item_id',
  87. 'prerequisite',
  88. 'previous_item_id'
  89. ];
  90. foreach ($sessions as $sessionId) {
  91. $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
  92. $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
  93. $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
  94. $sql = "UPDATE $toolTable
  95. SET link = '$correctLink'
  96. WHERE
  97. c_id = $courseId AND
  98. (link = '$link' OR link ='$secondLink' )";
  99. Database::query($sql);
  100. }
  101. foreach ($items as $item) {
  102. $itemIid = $item['iid'];
  103. $itemId = $item['id'];
  104. foreach ($variablesToFix as $variable) {
  105. if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
  106. $newId = $itemList[$item[$variable]];
  107. $sql = "UPDATE $tblCLpItem SET $variable = $newId
  108. WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
  109. Database::query($sql);
  110. var_dump($sql);
  111. }
  112. }
  113. // c_lp_view
  114. $sql = "UPDATE c_lp_view SET last_item = $itemIid
  115. WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
  116. Database::query($sql);
  117. var_dump($sql);
  118. // c_lp_item_view
  119. $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid
  120. WHERE c_id = $courseId AND lp_item_id = $itemId ";
  121. Database::query($sql);
  122. var_dump($sql);
  123. // Update track_exercises
  124. $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid
  125. WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
  126. Database::query($sql);
  127. var_dump($sql);
  128. // c_forum_thread
  129. $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid
  130. WHERE c_id = $courseId AND lp_item_id = $itemId";
  131. Database::query($sql);
  132. var_dump($sql);
  133. // orig_lp_item_view_id
  134. $sql = "SELECT * FROM c_lp_view
  135. WHERE c_id = $courseId AND lp_id = $oldId AND id <> iid";
  136. error_log($sql);
  137. $viewList = Database::store_result(Database::query($sql),'ASSOC');
  138. if ($viewList) {
  139. error_log("c_lp_view list: ".count( $viewList));
  140. foreach ($viewList as $view) {
  141. $oldViewId = $view['id'];
  142. $newViewId = $prefixViewId = $view['iid'];
  143. $userId = $view['user_id'];
  144. if (empty($oldViewId)) {
  145. continue;
  146. }
  147. $view['iid'] = null;
  148. // Create new c_lp_view to avoid conflicts
  149. $newViewId = Database::insert('c_lp_view', $view);
  150. var_dump($newViewId);
  151. if (empty($newViewId)) {
  152. continue;
  153. }
  154. $sql = "UPDATE c_lp_view SET id = iid WHERE iid = $newViewId";
  155. Database::query($sql);
  156. // Delete old c_lp_view
  157. $sql = "DELETE FROM c_lp_view WHERE id = $oldViewId AND iid = $prefixViewId ";
  158. Database::query($sql);
  159. $sql = "UPDATE track_e_exercises
  160. SET orig_lp_item_view_id = $newViewId
  161. WHERE
  162. c_id = $courseId AND
  163. orig_lp_id = $oldId AND
  164. orig_lp_item_id = $itemIid AND
  165. orig_lp_item_view_id = $oldViewId AND
  166. exe_user_id = $userId
  167. ";
  168. Database::query($sql);
  169. var_dump($sql);
  170. $sql = "SELECT * FROM c_lp_item_view WHERE lp_view_id = $oldViewId AND c_id = $courseId ";
  171. error_log($sql);
  172. $list = Database::store_result(Database::query($sql),'ASSOC');
  173. if (!empty($list)) {
  174. foreach ($list as $itemView) {
  175. $itemView['lp_view_id'] = $newViewId;
  176. $itemView['iid'] = null;
  177. //var_dump($itemView);
  178. $itemViewId = Database::insert('c_lp_item_view', $itemView);
  179. if ($itemViewId) {
  180. $sql = "UPDATE c_lp_item_view SET id = iid WHERE iid = $itemViewId";
  181. var_dump($sql);
  182. Database::query($sql);
  183. }
  184. }
  185. $sql = "DELETE FROM c_lp_item_view WHERE lp_view_id = $oldViewId AND c_id = $courseId";
  186. Database::query($sql);
  187. var_dump($sql);
  188. }
  189. /*$sql = "UPDATE c_lp_item_view
  190. SET lp_view_id = $newViewId
  191. WHERE
  192. lp_view_id = $oldViewId AND
  193. lp_item_id = $itemIid AND
  194. c_id = $courseId
  195. ";
  196. Database::query($sql);*/
  197. /*$sql = "UPDATE c_lp_view SET id = iid
  198. WHERE id = $oldViewId ";
  199. Database::query($sql);*/
  200. }
  201. }
  202. $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid
  203. WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
  204. Database::query($sql);
  205. var_dump($sql);
  206. $sql = "UPDATE $tblCLpItem SET id = iid
  207. WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
  208. Database::query($sql);
  209. var_dump($sql);
  210. }
  211. $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
  212. Database::query($sql);
  213. var_dump($sql);
  214. $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
  215. Database::query($sql);
  216. var_dump($sql);
  217. // Update track_exercises
  218. $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid
  219. WHERE c_id = $courseId AND orig_lp_id = $oldId";
  220. Database::query($sql);
  221. var_dump($sql);
  222. $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
  223. Database::query($sql);
  224. var_dump($sql);
  225. }
  226. }
  227. }
  228. echo 'finished';
  229. error_log('finished');