update-db-1.8.6.1-1.8.6.2.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chamilo LMS
  5. *
  6. * Update the Chamilo database from an older Dokeos version
  7. * Notice : This script has to be included by index.php
  8. * or update_courses.php (deprecated).
  9. *
  10. * @package chamilo.install
  11. * @todo
  12. * - conditional changing of tables. Currently we execute for example
  13. * ALTER TABLE `$dbNameForm`.`cours`
  14. * instructions without checking wether this is necessary.
  15. * - reorganise code into functions
  16. * @todo use database library
  17. */
  18. $old_file_version = '1.8.6.1';
  19. $new_file_version = '1.8.6.2';
  20. // Check if we come from index.php or update_courses.php - otherwise display error msg
  21. if (defined('SYSTEM_INSTALLATION')) {
  22. // Check if the current Dokeos install is eligible for update
  23. if (!file_exists('../inc/conf/configuration.php')) {
  24. echo '<strong>'.get_lang('Error').' !</strong> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br /><br />
  25. '.get_lang('PleasGoBackToStep1').'.
  26. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  27. </td></tr></table></form></body></html>';
  28. exit ();
  29. }
  30. $_configuration['db_glue'] = get_config_param('dbGlu');
  31. if ($singleDbForm) {
  32. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  33. $_configuration['main_database'] = get_config_param('mainDbName');
  34. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  35. }
  36. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  37. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  38. $dbScormForm = $dbPrefixForm.$dbScormForm;
  39. }
  40. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  41. $dbScormForm = $dbPrefixForm.'scorm';
  42. }
  43. /* Normal upgrade procedure: start by updating main, statistic, user databases */
  44. // If this script has been included by index.php, not update_courses.php, so
  45. // that we want to change the main databases as well...
  46. $only_test = false;
  47. $log = 0;
  48. if (defined('SYSTEM_INSTALLATION')) {
  49. if ($singleDbForm) {
  50. $dbStatsForm = $dbNameForm;
  51. $dbScormForm = $dbNameForm;
  52. $dbUserForm = $dbNameForm;
  53. }
  54. /**
  55. * Update the databases "pre" migration
  56. */
  57. include '../lang/english/create_course.inc.php';
  58. if ($languageForm != 'english') {
  59. // languageForm has been escaped in index.php
  60. include '../lang/'.$languageForm.'/create_course.inc.php';
  61. }
  62. // Get the main queries list (m_q_list)
  63. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'main');
  64. if (count($m_q_list) > 0) {
  65. // Now use the $m_q_list
  66. /**
  67. * We connect to the right DB first to make sure we can use the queries
  68. * without a database name
  69. */
  70. if (strlen($dbNameForm) > 40) {
  71. $app['monolog']->addError('Database name '.$dbNameForm.' is too long, skipping');
  72. } elseif (!in_array($dbNameForm,$dblist)) {
  73. $app['monolog']->addError('Database '.$dbNameForm.' was not found, skipping');
  74. } else {
  75. iDatabase::select_db($dbNameForm);
  76. foreach ($m_q_list as $query) {
  77. if ($only_test) {
  78. $app['monolog']->addInfo("iDatabase::query($dbNameForm,$query)");
  79. } else {
  80. $res = iDatabase::query($query);
  81. if ($log) {
  82. $app['monolog']->addInfo("In $dbNameForm, executed: $query");
  83. }
  84. }
  85. }
  86. // There might now be multiple course coaches. This implies
  87. // moving the previous course coach elements from the
  88. // session_rel_course table to the session_rel_course_rel_user
  89. // table with status 2
  90. // Select all the current course coaches in sessions
  91. $sql = "SELECT id_session, course_code, id_coach
  92. FROM session_rel_course
  93. ORDER BY id_session, course_code";
  94. $res = iDatabase::query($sql);
  95. if ($res === false) {
  96. $app['monolog']->addError('Could not query session course coaches table: '.iDatabase::error());
  97. } else {
  98. // For each coach found, add him as a course coach in the
  99. // session_rel_course_rel_user table
  100. while ($row = iDatabase::fetch_array($res)) {
  101. // Check whether coach is a student
  102. $sql = "SELECT 1 FROM session_rel_course_rel_user
  103. WHERE id_session='{$row[id_session]}' AND course_code='{$row[course_code]}' AND id_user='{$row[id_coach]}'";
  104. $rs = iDatabase::query($sql);
  105. if (iDatabase::num_rows($rs) > 0) {
  106. $sql_upd = "UPDATE session_rel_course_rel_user SET status=2
  107. WHERE id_session='{$row[id_session]}' AND course_code='{$row[course_code]}' AND id_user='{$row[id_coach]}'";
  108. } else {
  109. $sql_ins = "INSERT INTO session_rel_course_rel_user(id_session, course_code, id_user, status)
  110. VALUES ('{$row[id_session]}','{$row[course_code]}','{$row[id_coach]}',2)";
  111. }
  112. $rs_coachs = iDatabase::query($sql_ins);
  113. if ($rs_coachs === false) {
  114. $app['monolog']->addError('Could not move course coach to new table: '.iDatabase::error());
  115. }
  116. }
  117. }
  118. // Remove duplicated rows for 'show_tutor_data' AND 'show_teacher_data' into settings_current table
  119. $sql = "SELECT id FROM settings_current WHERE variable='show_tutor_data' ORDER BY id";
  120. $rs_chk_id1 = iDatabase::query($sql);
  121. if ($rs_chk_id1 === false) {
  122. $app['monolog']->addError('Could not query settings_current ids table: '.iDatabase::error());
  123. } else {
  124. $i = 1;
  125. while ($row_id1 = iDatabase::fetch_array($rs_chk_id1)) {
  126. $id = $row_id1['id'];
  127. if ($i > 1) {
  128. $sql_del = "DELETE FROM settings_current WHERE id = '$id'";
  129. iDatabase::query($sql_del);
  130. }
  131. $i++;
  132. }
  133. }
  134. $sql = "SELECT id FROM settings_current WHERE variable='show_teacher_data' ORDER BY id";
  135. $rs_chk_id2 = iDatabase::query($sql);
  136. if ($rs_chk_id2 === false) {
  137. $app['monolog']->addError('Could not query settings_current ids table: '.iDatabase::error());
  138. } else {
  139. $i = 1;
  140. while ($row_id2 = iDatabase::fetch_array($rs_chk_id2)) {
  141. $id = $row_id2['id'];
  142. if ($i > 1) {
  143. $sql_del = "DELETE FROM settings_current WHERE id = '$id'";
  144. iDatabase::query($sql_del);
  145. }
  146. $i++;
  147. }
  148. }
  149. }
  150. }
  151. // Now clean the deprecated id_coach field from the session_rel_course table
  152. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-post.sql', 'main');
  153. if (count($m_q_list) > 0) {
  154. // Now use the $m_q_list
  155. /**
  156. * We connect to the right DB first to make sure we can use the queries
  157. * without a database name
  158. */
  159. if (strlen($dbNameForm) > 40) {
  160. $app['monolog']->addError('Database name '.$dbNameForm.' is too long, skipping');
  161. } elseif (!in_array($dbNameForm,$dblist)) {
  162. $app['monolog']->addError('Database '.$dbNameForm.' was not found, skipping');
  163. } else {
  164. iDatabase::select_db($dbNameForm);
  165. foreach ($m_q_list as $query) {
  166. if ($only_test) {
  167. $app['monolog']->addInfo("iDatabase::query($dbNameForm,$query)");
  168. } else {
  169. $res = iDatabase::query($query);
  170. if ($log) {
  171. $app['monolog']->addInfo("In $dbNameForm, executed: $query");
  172. }
  173. }
  174. }
  175. }
  176. }
  177. // Get the stats queries list (s_q_list)
  178. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  179. if (count($s_q_list) > 0) {
  180. // Now use the $s_q_list
  181. /**
  182. * We connect to the right DB first to make sure we can use the queries
  183. * without a database name
  184. */
  185. if (strlen($dbStatsForm) > 40) {
  186. $app['monolog']->addError('Database name '.$dbStatsForm.' is too long, skipping');
  187. } elseif (!in_array($dbStatsForm, $dblist)) {
  188. $app['monolog']->addError('Database '.$dbStatsForm.' was not found, skipping');
  189. } else {
  190. iDatabase::select_db($dbStatsForm);
  191. foreach ($s_q_list as $query) {
  192. if ($only_test) {
  193. $app['monolog']->addInfo("iDatabase::query($dbStatsForm,$query)");
  194. } else {
  195. $res = iDatabase::query($query);
  196. if ($log) {
  197. $app['monolog']->addInfo("In $dbStatsForm, executed: $query");
  198. }
  199. }
  200. }
  201. }
  202. }
  203. // Get the user queries list (u_q_list)
  204. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'user');
  205. if (count($u_q_list) > 0) {
  206. // Now use the $u_q_list
  207. /**
  208. * We connect to the right DB first to make sure we can use the queries
  209. * without a database name
  210. */
  211. if (strlen($dbUserForm) > 40) {
  212. $app['monolog']->addError('Database name '.$dbUserForm.' is too long, skipping');
  213. } elseif (!in_array($dbUserForm,$dblist)) {
  214. $app['monolog']->addError('Database '.$dbUserForm.' was not found, skipping');
  215. } else {
  216. iDatabase::select_db($dbUserForm);
  217. foreach ($u_q_list as $query) {
  218. if ($only_test){
  219. $app['monolog']->addInfo("iDatabase::query($dbUserForm,$query)");
  220. $app['monolog']->addInfo("In $dbUserForm, executed: $query");
  221. } else {
  222. $res = iDatabase::query($query);
  223. }
  224. }
  225. }
  226. }
  227. // The SCORM database doesn't need a change in the pre-migrate part - ignore
  228. }
  229. $prefix = '';
  230. if ($singleDbForm) {
  231. $prefix = get_config_param ('table_prefix');
  232. }
  233. // Get the courses databases queries list (c_q_list)
  234. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  235. if (count($c_q_list) > 0) {
  236. // Get the courses list
  237. if (strlen($dbNameForm) > 40) {
  238. $app['monolog']->addError('Database name '.$dbNameForm.' is too long, skipping');
  239. } elseif (!in_array($dbNameForm, $dblist)) {
  240. $app['monolog']->addError('Database '.$dbNameForm.' was not found, skipping');
  241. } else {
  242. iDatabase::select_db($dbNameForm);
  243. $res = iDatabase::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  244. if ($res === false) { die('Error while querying the courses list in update_db-1.8.6.1-1.8.6.2.inc.php'); }
  245. if (iDatabase::num_rows($res) > 0) {
  246. $i = 0;
  247. $list = array();
  248. while ($row = iDatabase::fetch_array($res)) {
  249. $list[] = $row;
  250. $i++;
  251. }
  252. foreach ($list as $row_course) {
  253. // Now use the $c_q_list
  254. /**
  255. * We connect to the right DB first to make sure we can use the queries
  256. * without a database name
  257. */
  258. if (!$singleDbForm) { //otherwise just use the main one
  259. iDatabase::select_db($row_course['db_name']);
  260. }
  261. $app['monolog']->addInfo('Course db ' . $row_course['db_name']);
  262. foreach ($c_q_list as $query) {
  263. if ($singleDbForm) {
  264. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix{$row_course['db_name']}_$2$3", $query);
  265. }
  266. if ($only_test) {
  267. $app['monolog']->addError("iDatabase::query(".$row_course['db_name'].",$query)");
  268. } else {
  269. $res = iDatabase::query($query);
  270. if ($log) {
  271. $app['monolog']->addError("In ".$row_course['db_name'].", executed: $query");
  272. }
  273. }
  274. }
  275. // Fill description type into course_description table
  276. $t_course_description = $row_course['db_name'].".course_description";
  277. if ($singleDbForm) {
  278. $t_course_description = "$prefix{$row_course['db_name']}_course_description";
  279. }
  280. // Get all ids and update description_type field with them from course_description table
  281. $sql_sel = "SELECT id FROM $t_course_description";
  282. $rs_sel = iDatabase::query($sql_sel);
  283. if ($rs_sel === false) {
  284. $app['monolog']->addError('Could not query course_description ids table: '.iDatabase::error());
  285. } else {
  286. if (iDatabase::num_rows($rs_sel) > 0) {
  287. while ($row_ids = iDatabase::fetch_array($rs_sel)) {
  288. $description_id = $row_ids['id'];
  289. $sql_upd = "UPDATE $t_course_description SET description_type='$description_id' WHERE id='$description_id'";
  290. iDatabase::query($sql_upd);
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. } else {
  299. echo 'You are not allowed here !' . __FILE__;
  300. }