PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/php/main/install/update-db-1.8.6.1-1.8.6.2.inc.php

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