update-db-1.8.7-1.8.8.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.7';
  19. $new_file_version = '1.8.8';
  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. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  72. } elseif (!in_array($dbNameForm, $dblist)) {
  73. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  74. } else {
  75. Database::select_db($dbNameForm);
  76. foreach ($m_q_list as $query){
  77. if ($only_test) {
  78. error_log("Database::query($dbNameForm,$query)", 0);
  79. } else {
  80. $res = Database::query($query);
  81. if ($log) {
  82. error_log("In $dbNameForm, executed: $query", 0);
  83. }
  84. if ($res === false) {
  85. error_log('Error in '.$query.': '.Database::error());
  86. }
  87. }
  88. }
  89. }
  90. }
  91. /* // This fragment of code is not necessary so far.
  92. // Get the main queries list (m_q_list)
  93. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-post.sql', 'main');
  94. if (count($m_q_list) > 0) {
  95. // Now use the $m_q_list
  96. // We connect to the right DB first to make sure we can use the queries
  97. // without a database name.
  98. if (strlen($dbNameForm) > 40) {
  99. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  100. } elseif (!in_array($dbNameForm,$dblist)) {
  101. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  102. } else {
  103. Database::select_db($dbNameForm);
  104. foreach ($m_q_list as $query) {
  105. if ($only_test) {
  106. error_log("Database::query($dbNameForm,$query)", 0);
  107. } else {
  108. $res = Database::query($query);
  109. if ($log) {
  110. error_log("In $dbNameForm, executed: $query", 0);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. */
  117. // Get the stats queries list (s_q_list)
  118. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  119. if (count($s_q_list) > 0) {
  120. // Now use the $s_q_list
  121. /**
  122. * We connect to the right DB first to make sure we can use the queries
  123. * without a database name
  124. */
  125. if (strlen($dbStatsForm) > 40) {
  126. error_log('Database name '.$dbStatsForm.' is too long, skipping', 0);
  127. } elseif (!in_array($dbStatsForm, $dblist)){
  128. error_log('Database '.$dbStatsForm.' was not found, skipping', 0);
  129. } else {
  130. Database::select_db($dbStatsForm);
  131. foreach ($s_q_list as $query) {
  132. if ($only_test) {
  133. error_log("Database::query($dbStatsForm,$query)", 0);
  134. } else {
  135. $res = Database::query($query);
  136. if ($log) {
  137. error_log("In $dbStatsForm, executed: $query", 0);
  138. }
  139. if ($res === false) {
  140. error_log('Error in '.$query.': '.Database::error());
  141. }
  142. }
  143. }
  144. }
  145. }
  146. // Get the user queries list (u_q_list)
  147. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'user');
  148. if (count($u_q_list) > 0) {
  149. // Now use the $u_q_list
  150. /**
  151. * We connect to the right DB first to make sure we can use the queries
  152. * without a database name
  153. */
  154. if (strlen($dbUserForm) > 40) {
  155. error_log('Database name '.$dbUserForm.' is too long, skipping', 0);
  156. } elseif (!in_array($dbUserForm,$dblist)) {
  157. error_log('Database '.$dbUserForm.' was not found, skipping', 0);
  158. } else {
  159. Database::select_db($dbUserForm);
  160. foreach ($u_q_list as $query) {
  161. if ($only_test) {
  162. error_log("Database::query($dbUserForm,$query)", 0);
  163. error_log("In $dbUserForm, executed: $query", 0);
  164. } else {
  165. $res = Database::query($query);
  166. if ($res === false) {
  167. error_log('Error in '.$query.': '.Database::error());
  168. }
  169. }
  170. }
  171. }
  172. }
  173. // The SCORM database doesn't need a change in the pre-migrate part - ignore
  174. }
  175. $prefix = '';
  176. if ($singleDbForm) {
  177. $prefix = get_config_param ('table_prefix');
  178. }
  179. // Get the courses databases queries list (c_q_list)
  180. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  181. if (count($c_q_list) > 0) {
  182. // Get the courses list
  183. if (strlen($dbNameForm) > 40) {
  184. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  185. } elseif(!in_array($dbNameForm, $dblist)) {
  186. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  187. } else {
  188. Database::select_db($dbNameForm);
  189. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  190. if ($res === false) { die('Error while querying the courses list in update_db-1.8.6.2-1.8.7.inc.php'); }
  191. if (Database::num_rows($res) > 0) {
  192. $i = 0;
  193. $list = array();
  194. while ($row = Database::fetch_array($res)) {
  195. $list[] = $row;
  196. $i++;
  197. }
  198. foreach ($list as $row_course) {
  199. // Now use the $c_q_list
  200. /**
  201. * We connect to the right DB first to make sure we can use the queries
  202. * without a database name
  203. */
  204. if (!$singleDbForm) { // otherwise just use the main one
  205. Database::select_db($row_course['db_name']);
  206. }
  207. foreach ($c_q_list as $query) {
  208. if ($singleDbForm) {
  209. $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);
  210. }
  211. if ($only_test) {
  212. error_log("Database::query(".$row_course['db_name'].",$query)", 0);
  213. } else {
  214. $res = Database::query($query);
  215. if ($log) {
  216. error_log("In ".$row_course['db_name'].", executed: $query", 0);
  217. }
  218. if ($res === false) {
  219. error_log('Error in '.$query.': '.Database::error());
  220. }
  221. }
  222. }
  223. $table_lp_item_view = $row_course['db_name'].".lp_item_view";
  224. $table_lp_view = $row_course['db_name'].".lp_view";
  225. $table_lp_item = $row_course['db_name'].".lp_item";
  226. if ($singleDbForm) {
  227. $table_lp_item_view = "$prefix{$row_course['db_name']}_lp_item_view";
  228. $table_lp_view = "$prefix{$row_course['db_name']}_lp_view";
  229. $table_lp_item = "$prefix{$row_course['db_name']}_lp_item";
  230. }
  231. // Filling the track_e_exercices.orig_lp_item_view_id field in order to have better traceability in exercises included in a LP see #3188
  232. $query = "SELECT DISTINCT path as exercise_id, lp_item_id, lp_view_id, user_id, v.lp_id
  233. FROM $table_lp_item_view iv INNER JOIN $table_lp_view v ON v.id = iv.lp_view_id INNER JOIN $table_lp_item i ON i.id = lp_item_id
  234. WHERE item_type = 'quiz'";
  235. $result = Database::query($query);
  236. if (Database::num_rows($result) > 0) {
  237. while ($row = Database::fetch_array($result,'ASSOC')) {
  238. $sql = "SELECT exe_id FROM $dbNameForm.track_e_exercices
  239. WHERE exe_user_id = {$row['user_id']} AND
  240. exe_cours_id = '{$row_course['code']}' AND
  241. exe_exo_id = {$row['exercise_id']} AND
  242. orig_lp_id = {$row['lp_id']} AND
  243. orig_lp_item_id = {$row['lp_item_id']} ";
  244. $sub_result = Database::query($sql);
  245. $exe_list = array();
  246. while ($sub_row = Database::fetch_array($sub_result,'ASSOC')) {
  247. $exe_list[] = $sub_row['exe_id'];
  248. }
  249. $sql = "SELECT iv.id, iv.view_count
  250. FROM $table_lp_item_view iv INNER JOIN $table_lp_view v ON v.id = iv.lp_view_id INNER JOIN $table_lp_item i ON i.id = lp_item_id
  251. WHERE item_type = 'quiz' AND user_id = {$row['user_id']} AND path = {$row['exercise_id']} ";
  252. $sub_result = Database::query($sql);
  253. $lp_item_view_id_list = array();
  254. while ($sub_row = Database::fetch_array($sub_result,'ASSOC')) {
  255. $lp_item_view_id_list[] = $sub_row['id'];
  256. }
  257. $i = 0;
  258. foreach($exe_list as $exe_id) {
  259. $lp_item_view_id = $lp_item_view_id_list[$i];
  260. $update = "UPDATE $dbNameForm.track_e_exercices SET orig_lp_item_view_id = '$lp_item_view_id' WHERE exe_id = $exe_id ";
  261. Database::query($update);
  262. $i++;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. //Adding notifications options
  269. $sql = "INSERT INTO $dbNameForm.user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_invitation', 'MailNotifyInvitation',1,1,'1') ";
  270. $result = Database::query($sql);
  271. $id = Database::insert_id();
  272. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '1', 'AtOnce',1) ";
  273. $result = Database::query($sql);
  274. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '8', 'Daily',2) ";
  275. $result = Database::query($sql);
  276. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '0', 'No',3) ";
  277. $result = Database::query($sql);
  278. $sql = "INSERT INTO $dbNameForm.user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_message', 'MailNotifyMessage',1,1,'1')";
  279. $result = Database::query($sql);
  280. $id = Database::insert_id();
  281. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '1', 'AtOnce',1) ";
  282. $result = Database::query($sql);
  283. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '8', 'Daily',2) ";
  284. $result = Database::query($sql);
  285. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '0', 'No',3) ";
  286. $result = Database::query($sql);
  287. $sql = "INSERT INTO $dbNameForm.user_field (field_type, field_variable, field_display_text, field_visible, field_changeable, field_default_value) values (4, 'mail_notify_group_message','MailNotifyGroupMessage',1,1,'1') ";
  288. $result = Database::query($sql);
  289. $id = Database::insert_id();
  290. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '1', 'AtOnce',1) ";
  291. $result = Database::query($sql);
  292. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '8', 'Daily',2) ";
  293. $result = Database::query($sql);
  294. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '0', 'No',3) ";
  295. $result = Database::query($sql);
  296. //Fixing table access_url_rel_course if the platform have courses that were created in Dok€os 1.8.5
  297. if (!isset($_configuration['multiple_access_urls']) || $_configuration['multiple_access_urls'] == false) {
  298. $sql = "SELECT code FROM $dbNameForm.course";
  299. $result = Database::query($sql);
  300. while ($row = Database::fetch_array($result)) {
  301. //Adding course to default URL just in case
  302. $sql = "INSERT INTO $dbNameForm.access_url_rel_course SET course_code = '".Database::escape_string($row['code'])."', access_url_id = '1' ";
  303. Database::query($sql);
  304. }
  305. }
  306. }
  307. }
  308. /* // This fragment of code is not necessary so far.
  309. // Get the courses databases queries list (c_q_list)
  310. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-post.sql', 'course');
  311. if (count($c_q_list) > 0) {
  312. // Get the courses list
  313. if (strlen($dbNameForm) > 40) {
  314. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  315. } elseif (!in_array($dbNameForm, $dblist)) {
  316. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  317. } else {
  318. Database::select_db($dbNameForm);
  319. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  320. if ($res === false) { die('Error while querying the courses list in update_db-1.8.7-1.8.8.inc.php'); }
  321. if (Database::num_rows($res) > 0) {
  322. $i = 0;
  323. while ($row = Database::fetch_array($res)) {
  324. $list[] = $row;
  325. $i++;
  326. }
  327. foreach ($list as $row) {
  328. // Now use the $c_q_list
  329. // We connect to the right DB first to make sure we can use the queries
  330. // without a database name
  331. $prefix_course = $prefix;
  332. if ($singleDbForm) {
  333. $prefix_course = $prefix.$row['db_name']."_";
  334. } else {
  335. Database::select_db($row['db_name']);
  336. }
  337. foreach($c_q_list as $query) {
  338. if ($singleDbForm) { //otherwise just use the main one
  339. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix$2$3", $query);
  340. }
  341. if ($only_test) {
  342. error_log("Database::query(".$row['db_name'].",$query)", 0);
  343. } else {
  344. $res = Database::query($query);
  345. if ($log) {
  346. error_log("In ".$row['db_name'].", executed: $query", 0);
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. */
  355. } else {
  356. echo 'You are not allowed here !';
  357. }