update-db-1.8.6.2-1.8.7.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chamilo LMS
  5. *
  6. * Update the Chamilo database from an older Chamilo 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.2';
  19. $new_file_version = '1.8.7';
  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 Chamilo install is eligible for update
  23. if (!file_exists('../inc/conf/configuration.php')) {
  24. echo '<strong>' . get_lang('Error') . ' !</strong> Chamilo ' . 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("Database::query($dbNameForm,$query)");
  79. } else {
  80. $res = iDatabase::query($query);
  81. if ($log) {
  82. $app['monolog']->addInfo("In $dbNameForm, executed: $query");
  83. }
  84. if ($res === false) {
  85. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  86. }
  87. }
  88. }
  89. $tables = iDatabase::get_tables($dbNameForm);
  90. foreach ($tables as $table) {
  91. $query = "ALTER TABLE `" . $table . "` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;";
  92. $res = iDatabase::query($query);
  93. if ($res === false) {
  94. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  95. }
  96. }
  97. $query = "ALTER DATABASE `" . $dbNameForm . "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;";
  98. $res = Database::query($query);
  99. if ($res === false) {
  100. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  101. }
  102. }
  103. }
  104. if (DATE_TIME_INSTALLED) {
  105. // Converting dates and times to UTC using the default timezone of PHP
  106. // Converting gradebook dates and times
  107. $timezone = date_default_timezone_get();
  108. // Calculating the offset
  109. $dateTimeZoneCurrent = new DateTimeZone($timezone);
  110. $dateTimeUTC = new DateTime("now", new DateTimeZone('UTC'));
  111. $timeOffsetSeconds = $dateTimeZoneCurrent->getOffset($dateTimeUTC);
  112. $timeOffsetHours = $timeOffsetSeconds / 3600;
  113. $timeOffsetString = "";
  114. if ($timeOffsetHours < 0) {
  115. $timeOffsetString .= "-";
  116. $timeOffsetHours = abs($timeOffsetHours);
  117. } else {
  118. $timeOffsetString .= "+";
  119. }
  120. if ($timeOffsetHours < 10) {
  121. $timeOffsetString .= "0";
  122. }
  123. $timeOffsetString .= "$timeOffsetHours";
  124. $timeOffsetString .= ":00";
  125. // Executing the queries to convert everything
  126. $queries[] = "UPDATE gradebook_certificate SET created_at = CONVERT_TZ(created_at, '" . $timeOffsetString . "', '+00:00');";
  127. $queries[] = "UPDATE gradebook_evaluation SET created_at = CONVERT_TZ(created_at, '" . $timeOffsetString . "', '+00:00');";
  128. $queries[] = "UPDATE gradebook_link SET created_at = CONVERT_TZ(created_at, '" . $timeOffsetString . "', '+00:00');";
  129. $queries[] = "UPDATE gradebook_linkeval_log SET created_at = CONVERT_TZ(created_at, '" . $timeOffsetString . "', '+00:00');";
  130. $queries[] = "UPDATE gradebook_result SET created_at = CONVERT_TZ(created_at, '" . $timeOffsetString . "', '+00:00');";
  131. $queries[] = "UPDATE gradebook_result_log SET created_at = CONVERT_TZ(created_at, '" . $timeOffsetString . "', '+00:00');";
  132. foreach ($queries as $query) {
  133. iDatabase::query($query);
  134. }
  135. }
  136. // Moving user followed by a human resource manager from hr_dept_id field to user_rel_user table
  137. $query = "SELECT user_id, hr_dept_id FROM $dbNameForm.user";
  138. $result = iDatabase::query($query);
  139. if (iDatabase::num_rows($result) > 0) {
  140. require_once api_get_path(LIBRARY_PATH) . 'usermanager.lib.php';
  141. while ($row = iDatabase::fetch_array($result, 'ASSOC')) {
  142. $user_id = $row['user_id'];
  143. $hr_dept_id = $row['hr_dept_id'];
  144. // moving data to user_rel_user table
  145. if (!empty($hr_dept_id)) {
  146. $sql = " SELECT id FROM $dbNameForm.user_rel_user WHERE user_id = $user_id AND friend_user_id = $hr_dept_id AND relation_type = " . USER_RELATION_TYPE_RRHH . " ";
  147. $rs = iDatabase::query($sql);
  148. if (iDatabase::num_rows($rs) == 0) {
  149. $ins = "INSERT INTO $dbNameForm.user_rel_user SET user_id = $user_id, friend_user_id = $hr_dept_id, relation_type = " . USER_RELATION_TYPE_RRHH . " ";
  150. iDatabase::query($ins);
  151. }
  152. }
  153. }
  154. // cleaning hr_dept_id field inside user table
  155. $upd = "UPDATE $dbNameForm.user SET hr_dept_id = 0";
  156. iDatabase::query($upd);
  157. }
  158. // Updating score display for each gradebook category
  159. // first we check if there already is migrated data to categoy_id field
  160. $query = "SELECT id FROM $dbNameForm.gradebook_score_display WHERE category_id = 0";
  161. $rs_check = iDatabase::query($query);
  162. if (iDatabase::num_rows($rs_check) > 0) {
  163. // get all gradebook categories id
  164. $a_categories = array();
  165. $query = "SELECT id FROM $dbNameForm.gradebook_category";
  166. $rs_gradebook = iDatabase::query($query);
  167. if (iDatabase::num_rows($rs_gradebook) > 0) {
  168. while ($row_gradebook = iDatabase::fetch_row($rs_gradebook)) {
  169. $a_categories[] = $row_gradebook[0];
  170. }
  171. }
  172. // get all gradebook score display
  173. $query = "SELECT * FROM $dbNameForm.gradebook_score_display";
  174. $rs_score_display = iDatabase::query($query);
  175. if (iDatabase::num_rows($rs_score_display) > 0) {
  176. $score_color_percent = api_get_setting('gradebook_score_display_colorsplit');
  177. while ($row_score_display = iDatabase::fetch_array($rs_score_display)) {
  178. $score = $row_score_display['score'];
  179. $display = $row_score_display['display'];
  180. foreach ($a_categories as $category_id) {
  181. $ins = "INSERT INTO $dbNameForm.gradebook_score_display(score, display, category_id, score_color_percent) VALUES('$score', '$display', $category_id, '$score_color_percent')";
  182. iDatabase::query($ins);
  183. }
  184. }
  185. // remove score display with category id = 0
  186. $del = "DELETE FROM $dbNameForm.gradebook_score_display WHERE category_id = 0";
  187. iDatabase::query($del);
  188. }
  189. }
  190. // Now clean the deprecated id_coach field from the session_rel_course table
  191. $m_q_list = get_sql_file_contents('migrate-db-' . $old_file_version . '-' . $new_file_version . '-post.sql', 'main');
  192. if (count($m_q_list) > 0) {
  193. // Now use the $m_q_list
  194. /**
  195. * We connect to the right DB first to make sure we can use the queries
  196. * without a database name
  197. */
  198. if (strlen($dbNameForm) > 40) {
  199. $app['monolog']->addError('Database name ' . $dbNameForm . ' is too long, skipping');
  200. } elseif (!in_array($dbNameForm, $dblist)) {
  201. $app['monolog']->addError('Database ' . $dbNameForm . ' was not found, skipping');
  202. } else {
  203. iDatabase::select_db($dbNameForm);
  204. foreach ($m_q_list as $query) {
  205. if ($only_test) {
  206. $app['monolog']->addInfo("Database::query($dbNameForm,$query)");
  207. } else {
  208. $res = iDatabase::query($query);
  209. if ($log) {
  210. $app['monolog']->addInfo("In $dbNameForm, executed: $query");
  211. }
  212. }
  213. }
  214. }
  215. }
  216. // Get the stats queries list (s_q_list)
  217. $s_q_list = get_sql_file_contents('migrate-db-' . $old_file_version . '-' . $new_file_version . '-pre.sql', 'stats');
  218. if (count($s_q_list) > 0) {
  219. // Now use the $s_q_list
  220. /**
  221. * We connect to the right DB first to make sure we can use the queries
  222. * without a database name
  223. */
  224. if (strlen($dbStatsForm) > 40) {
  225. $app['monolog']->addError('Database name ' . $dbStatsForm . ' is too long, skipping');
  226. } elseif (!in_array($dbStatsForm, $dblist)) {
  227. $app['monolog']->addError('Database ' . $dbStatsForm . ' was not found, skipping');
  228. } else {
  229. iDatabase::select_db($dbStatsForm);
  230. foreach ($s_q_list as $query) {
  231. if ($only_test) {
  232. $app['monolog']->addInfo("Database::query($dbStatsForm,$query)");
  233. } else {
  234. $res = iDatabase::query($query);
  235. if ($log) {
  236. $app['monolog']->addInfo("In $dbStatsForm, executed: $query");
  237. }
  238. if ($res === false) {
  239. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  240. }
  241. }
  242. }
  243. $tables = iDatabase::get_tables($dbStatsForm);
  244. foreach ($tables as $table) {
  245. $query = "ALTER TABLE `" . $table . "` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;";
  246. $res = iDatabase::query($query);
  247. if ($res === false) {
  248. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  249. }
  250. }
  251. $query = "ALTER DATABASE `" . $dbStatsForm . "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;";
  252. $res = iDatabase::query($query);
  253. if ($res === false) {
  254. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  255. }
  256. // chamilo_stat.track_e_attempt table update changing id by id_auto
  257. $sql = "SELECT exe_id, question_id, course_code, answer FROM $dbStatsForm.track_e_attempt";
  258. $result = iDatabase::query($sql);
  259. if (iDatabase::num_rows($result) > 0) {
  260. while ($row = iDatabase::fetch_array($result)) {
  261. $course_code = $row['course_code'];
  262. $course_info = api_get_course_info($course_code);
  263. if (empty($course_info)) {
  264. continue;
  265. }
  266. $my_course_db = $course_info['dbName'];
  267. $question_id = $row['question_id'];
  268. $answer = $row['answer'];
  269. $exe_id = $row['exe_id'];
  270. if(empty($answer)){
  271. continue;
  272. }
  273. //getting the type question id
  274. $sql_question = "SELECT type FROM $my_course_db.quiz_question where id = $question_id";
  275. $res_question = iDatabase::query($sql_question);
  276. $row = iDatabase::fetch_array($res_question);
  277. $type = $row['type'];
  278. require_once api_get_path(SYS_CODE_PATH) . 'exercice/question.class.php';
  279. //this type of questions produce problems in the track_e_attempt table
  280. if (in_array($type, array(UNIQUE_ANSWER, MULTIPLE_ANSWER, MATCHING, MULTIPLE_ANSWER_COMBINATION))) {
  281. $sql_question = "SELECT id_auto FROM $my_course_db.quiz_answer where question_id = $question_id and id = $answer";
  282. $res_question = iDatabase::query($sql_question);
  283. $row = iDatabase::fetch_array($res_question);
  284. if($row){
  285. $id_auto = $row['id_auto'];
  286. if (!empty($id_auto)) {
  287. $sql = "UPDATE $dbStatsForm.track_e_attempt SET answer = '$id_auto' WHERE exe_id = $exe_id AND question_id = $question_id AND course_code = '$course_code' and answer = $answer ";
  288. iDatabase::query($sql);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. // Get the user queries list (u_q_list)
  297. $u_q_list = get_sql_file_contents('migrate-db-' . $old_file_version . '-' . $new_file_version . '-pre.sql', 'user');
  298. if (count($u_q_list) > 0) {
  299. // Now use the $u_q_list
  300. /**
  301. * We connect to the right DB first to make sure we can use the queries
  302. * without a database name
  303. */
  304. if (strlen($dbUserForm) > 40) {
  305. $app['monolog']->addError('Database name ' . $dbUserForm . ' is too long, skipping');
  306. } elseif (!in_array($dbUserForm, $dblist)) {
  307. $app['monolog']->addError('Database ' . $dbUserForm . ' was not found, skipping');
  308. } else {
  309. iDatabase::select_db($dbUserForm);
  310. foreach ($u_q_list as $query) {
  311. if ($only_test) {
  312. $app['monolog']->addInfo("Database::query($dbUserForm,$query)");
  313. $app['monolog']->addInfo("In $dbUserForm, executed: $query");
  314. } else {
  315. $res = iDatabase::query($query);
  316. if ($res === false) {
  317. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  318. }
  319. }
  320. }
  321. $tables = iDatabase::get_tables($dbUserForm);
  322. foreach ($tables as $table) {
  323. $query = "ALTER TABLE `" . $table . "` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;";
  324. $res = iDatabase::query($query);
  325. if ($res === false) {
  326. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  327. }
  328. }
  329. $query = "ALTER DATABASE `" . $dbUserForm . "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;";
  330. $res = iDatabase::query($query);
  331. if ($res === false) {
  332. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  333. }
  334. }
  335. }
  336. // The SCORM database doesn't need a change in the pre-migrate part - ignore
  337. }
  338. $prefix = '';
  339. if ($singleDbForm) {
  340. $prefix = get_config_param('table_prefix');
  341. }
  342. // Get the courses databases queries list (c_q_list)
  343. $c_q_list = get_sql_file_contents('migrate-db-' . $old_file_version . '-' . $new_file_version . '-pre.sql', 'course');
  344. if (count($c_q_list) > 0) {
  345. // Get the courses list
  346. if (strlen($dbNameForm) > 40) {
  347. $app['monolog']->addError('Database name ' . $dbNameForm . ' is too long, skipping');
  348. } elseif (!in_array($dbNameForm, $dblist)) {
  349. $app['monolog']->addError('Database ' . $dbNameForm . ' was not found, skipping');
  350. } else {
  351. iDatabase::select_db($dbNameForm);
  352. $res = iDatabase::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  353. if ($res === false) {
  354. die('Error while querying the courses list in update_db-1.8.6.2-1.8.7.inc.php');
  355. }
  356. if (iDatabase::num_rows($res) > 0) {
  357. $i = 0;
  358. $list = array();
  359. while ($row = iDatabase::fetch_array($res)) {
  360. $list[] = $row;
  361. $i++;
  362. }
  363. foreach ($list as $row_course) {
  364. // Now use the $c_q_list
  365. /**
  366. * We connect to the right DB first to make sure we can use the queries
  367. * without a database name
  368. */
  369. if (!$singleDbForm) { // otherwise just use the main one
  370. iDatabase::select_db($row_course['db_name']);
  371. }
  372. foreach ($c_q_list as $query) {
  373. if ($singleDbForm) {
  374. $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);
  375. }
  376. if ($only_test) {
  377. $app['monolog']->addInfo("Database::query(" . $row_course['db_name'] . ",$query)");
  378. } else {
  379. $res = iDatabase::query($query);
  380. if ($log) {
  381. $app['monolog']->addInfo("In " . $row_course['db_name'] . ", executed: $query");
  382. }
  383. if ($res === false) {
  384. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  385. }
  386. }
  387. }
  388. if (!$singleDbForm) {
  389. $tables = iDatabase::get_tables($row_course['db_name']);
  390. foreach ($tables as $table) {
  391. $query = "ALTER TABLE `" . $table . "` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;";
  392. $res = iDatabase::query($query);
  393. if ($res === false) {
  394. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  395. }
  396. }
  397. $query = "ALTER DATABASE `" . $row_course['db_name'] . "` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;";
  398. $res = iDatabase::query($query);
  399. if ($res === false) {
  400. $app['monolog']->addError('Error in ' . $query . ': ' . iDatabase::error());
  401. }
  402. }
  403. $t_student_publication = $row_course['db_name'] . ".student_publication";
  404. $t_item_property = $row_course['db_name'] . ".item_property";
  405. if ($singleDbForm) {
  406. $t_student_publication = "$prefix{$row_course['db_name']}_student_publication";
  407. $t_item_property = "$prefix{$row_course['db_name']}_item_property";
  408. }
  409. $sql_insert_user = "SELECT ref, insert_user_id FROM $t_item_property WHERE tool='work'";
  410. $rs_insert_user = iDatabase::query($sql_insert_user);
  411. if ($rs_insert_user === false) {
  412. $app['monolog']->addError('Could not query insert_user_id table: ' . iDatabase::error());
  413. } else {
  414. if (iDatabase::num_rows($rs_insert_user) > 0) {
  415. while ($row_ids = iDatabase::fetch_array($rs_insert_user)) {
  416. $user_id = $row_ids['insert_user_id'];
  417. $ref = $row_ids['ref'];
  418. $sql_upd = "UPDATE $t_student_publication SET user_id='$user_id' WHERE id='$ref'";
  419. iDatabase::query($sql_upd);
  420. }
  421. }
  422. }
  423. //updating parent_id of the student_publication table
  424. $sql = 'SELECT id, url, parent_id FROM ' . $t_student_publication;
  425. $result = iDatabase::query($sql);
  426. if (iDatabase::num_rows($result) > 0) {
  427. $items = iDatabase::store_result($result);
  428. $directory_list = $file_list = array();
  429. foreach ($items as $item) {
  430. $student_slash = substr($item['url'], 0, 1);
  431. //means this is a directory
  432. if ($student_slash == '/') {
  433. $directory_list[$item['id']] = $item['url'];
  434. } else {
  435. // this is a file with no parents
  436. if ($item['parent_id'] == 0)
  437. $file_list [] = $item;
  438. }
  439. }
  440. if (is_array($file_list) && count($file_list) > 0) {
  441. foreach ($file_list as $file) {
  442. $parent_id = 0;
  443. if (is_array($directory_list) && count($directory_list) > 0) {
  444. foreach ($directory_list as $id => $dir) {
  445. $pos = strpos($file['url'], $dir . '/');
  446. if ($pos !== false) {
  447. $parent_id = $id;
  448. break;
  449. }
  450. }
  451. }
  452. if ($parent_id != 0) {
  453. $sql = 'UPDATE ' . $t_student_publication . ' SET parent_id = ' . $parent_id . ' WHERE id = ' . $file['id'] . '';
  454. iDatabase::query($sql);
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. // Get the courses databases queries list (c_q_list)
  464. $c_q_list = get_sql_file_contents('migrate-db-' . $old_file_version . '-' . $new_file_version . '-post.sql', 'course');
  465. if (count($c_q_list) > 0) {
  466. // Get the courses list
  467. if (strlen($dbNameForm) > 40) {
  468. $app['monolog']->addError('Database name ' . $dbNameForm . ' is too long, skipping');
  469. } elseif (!in_array($dbNameForm, $dblist)) {
  470. $app['monolog']->addError('Database ' . $dbNameForm . ' was not found, skipping');
  471. } else {
  472. iDatabase::select_db($dbNameForm);
  473. $res = iDatabase::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  474. if ($res === false) {
  475. die('Error while querying the courses list in update_db-1.8.6.2-1.8.7.inc.php');
  476. }
  477. if (iDatabase::num_rows($res) > 0) {
  478. $i = 0;
  479. while ($row = iDatabase::fetch_array($res)) {
  480. $list[] = $row;
  481. $i++;
  482. }
  483. foreach ($list as $row) {
  484. // Now use the $c_q_list
  485. /**
  486. * We connect to the right DB first to make sure we can use the queries
  487. * without a database name
  488. */
  489. $prefix_course = $prefix;
  490. if ($singleDbForm) {
  491. $prefix_course = $prefix . $row['db_name'] . "_";
  492. } else {
  493. iDatabase::select_db($row['db_name']);
  494. }
  495. foreach ($c_q_list as $query) {
  496. if ($singleDbForm) { //otherwise just use the main one
  497. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix$2$3", $query);
  498. }
  499. if ($only_test) {
  500. $app['monolog']->addInfo("Database::query(" . $row['db_name'] . ",$query)");
  501. } else {
  502. $res = iDatabase::query($query);
  503. if ($log) {
  504. $app['monolog']->addInfo("In " . $row['db_name'] . ", executed: $query");
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }
  511. }
  512. } else {
  513. echo 'You are not allowed here !' . __FILE__;
  514. }