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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. $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. if ($res === false) {
  85. $app['monolog']->addError('Error in '.$query.': '.iDatabase::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');
  100. } elseif (!in_array($dbNameForm,$dblist)) {
  101. error_log('Database '.$dbNameForm.' was not found, skipping');
  102. } else {
  103. iDatabase::select_db($dbNameForm);
  104. foreach ($m_q_list as $query) {
  105. if ($only_test) {
  106. error_log("iDatabase::query($dbNameForm,$query)");
  107. } else {
  108. $res = iDatabase::query($query);
  109. if ($log) {
  110. error_log("In $dbNameForm, executed: $query");
  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. $app['monolog']->addError('Database name '.$dbStatsForm.' is too long, skipping');
  127. } elseif (!in_array($dbStatsForm, $dblist)){
  128. $app['monolog']->addError('Database '.$dbStatsForm.' was not found, skipping');
  129. } else {
  130. iDatabase::select_db($dbStatsForm);
  131. foreach ($s_q_list as $query) {
  132. if ($only_test) {
  133. $app['monolog']->addInfo("iDatabase::query($dbStatsForm,$query)");
  134. } else {
  135. $res = iDatabase::query($query);
  136. if ($log) {
  137. $app['monolog']->addInfo("In $dbStatsForm, executed: $query");
  138. }
  139. if ($res === false) {
  140. $app['monolog']->addError('Error in '.$query.': '.iDatabase::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. $app['monolog']->addError('Database name '.$dbUserForm.' is too long, skipping');
  156. } elseif (!in_array($dbUserForm,$dblist)) {
  157. $app['monolog']->addError('Database '.$dbUserForm.' was not found, skipping');
  158. } else {
  159. iDatabase::select_db($dbUserForm);
  160. foreach ($u_q_list as $query) {
  161. if ($only_test) {
  162. $app['monolog']->addInfo("iDatabase::query($dbUserForm,$query)");
  163. $app['monolog']->addInfo("In $dbUserForm, executed: $query");
  164. } else {
  165. $res = iDatabase::query($query);
  166. if ($res === false) {
  167. $app['monolog']->addError('Error in '.$query.': '.iDatabase::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. $app['monolog']->addError('Database name '.$dbNameForm.' is too long, skipping');
  185. } elseif(!in_array($dbNameForm, $dblist)) {
  186. $app['monolog']->addError('Database '.$dbNameForm.' was not found, skipping');
  187. } else {
  188. iDatabase::select_db($dbNameForm);
  189. $res = iDatabase::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.7.1-1.8.8.inc.php'); }
  191. if (iDatabase::num_rows($res) > 0) {
  192. $i = 0;
  193. $list = array();
  194. while ($row = iDatabase::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. iDatabase::select_db($row_course['db_name']);
  206. }
  207. $app['monolog']->addInfo('Course db ' . $row_course['db_name']);
  208. foreach ($c_q_list as $query) {
  209. if ($singleDbForm) {
  210. $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);
  211. }
  212. if ($only_test) {
  213. $app['monolog']->addInfo("iDatabase::query(".$row_course['db_name'].",$query)");
  214. } else {
  215. $res = iDatabase::query($query);
  216. if ($log) {
  217. $app['monolog']->addInfo("In ".$row_course['db_name'].", executed: $query");
  218. }
  219. if ($res === false) {
  220. $app['monolog']->addError('Error in '.$query.': '.iDatabase::error());
  221. }
  222. }
  223. }
  224. $table_lp_item_view = $row_course['db_name'].".lp_item_view";
  225. $table_lp_view = $row_course['db_name'].".lp_view";
  226. $table_lp_item = $row_course['db_name'].".lp_item";
  227. if ($singleDbForm) {
  228. $table_lp_item_view = "$prefix{$row_course['db_name']}_lp_item_view";
  229. $table_lp_view = "$prefix{$row_course['db_name']}_lp_view";
  230. $table_lp_item = "$prefix{$row_course['db_name']}_lp_item";
  231. }
  232. // 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
  233. $query = "SELECT DISTINCT path as exercise_id, lp_item_id, lp_view_id, user_id, v.lp_id
  234. 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
  235. WHERE item_type = 'quiz'";
  236. $result = iDatabase::query($query);
  237. if (iDatabase::num_rows($result) > 0) {
  238. while ($row = iDatabase::fetch_array($result,'ASSOC')) {
  239. $sql = "SELECT exe_id FROM $dbStatsForm.track_e_exercices
  240. WHERE exe_user_id = {$row['user_id']} AND
  241. exe_cours_id = '{$row_course['code']}' AND
  242. exe_exo_id = {$row['exercise_id']} AND
  243. orig_lp_id = {$row['lp_id']} AND
  244. orig_lp_item_id = {$row['lp_item_id']} ";
  245. $sub_result = iDatabase::query($sql);
  246. $exe_list = array();
  247. while ($sub_row = iDatabase::fetch_array($sub_result,'ASSOC')) {
  248. $exe_list[] = $sub_row['exe_id'];
  249. }
  250. $sql = "SELECT iv.id, iv.view_count
  251. 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
  252. WHERE item_type = 'quiz' AND user_id = {$row['user_id']} AND path = {$row['exercise_id']} ";
  253. $sub_result = iDatabase::query($sql);
  254. $lp_item_view_id_list = array();
  255. while ($sub_row = iDatabase::fetch_array($sub_result,'ASSOC')) {
  256. $lp_item_view_id_list[] = $sub_row['id'];
  257. }
  258. $i = 0;
  259. foreach($exe_list as $exe_id) {
  260. $lp_item_view_id = $lp_item_view_id_list[$i];
  261. $update = "UPDATE $dbNameForm.track_e_exercices SET orig_lp_item_view_id = '$lp_item_view_id' WHERE exe_id = $exe_id ";
  262. iDatabase::query($update);
  263. $i++;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. //Adding notifications options
  270. $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') ";
  271. $result = iDatabase::query($sql);
  272. $id = iDatabase::insert_id();
  273. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '1', 'AtOnce',1) ";
  274. $result = iDatabase::query($sql);
  275. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '8', 'Daily',2) ";
  276. $result = iDatabase::query($sql);
  277. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '0', 'No',3) ";
  278. $result = iDatabase::query($sql);
  279. $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')";
  280. $result = iDatabase::query($sql);
  281. $id = iDatabase::insert_id();
  282. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '1', 'AtOnce',1) ";
  283. $result = iDatabase::query($sql);
  284. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '8', 'Daily',2) ";
  285. $result = iDatabase::query($sql);
  286. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '0', 'No',3) ";
  287. $result = iDatabase::query($sql);
  288. $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') ";
  289. $result = iDatabase::query($sql);
  290. $id = iDatabase::insert_id();
  291. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '1', 'AtOnce',1) ";
  292. $result = iDatabase::query($sql);
  293. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '8', 'Daily',2) ";
  294. $result = iDatabase::query($sql);
  295. $sql = "INSERT INTO $dbNameForm.user_field_options (field_id, option_value, option_display_text, option_order) values ($id, '0', 'No',3) ";
  296. $result = iDatabase::query($sql);
  297. //Fixing table access_url_rel_course if the platform have courses that were created in Dok€os 1.8.5
  298. if (!isset($_configuration['multiple_access_urls']) || $_configuration['multiple_access_urls'] == false) {
  299. $sql = "SELECT code FROM $dbNameForm.course";
  300. $result = iDatabase::query($sql);
  301. while ($row = iDatabase::fetch_array($result)) {
  302. //Adding course to default URL just in case
  303. $sql = "INSERT INTO $dbNameForm.access_url_rel_course SET course_code = '".iDatabase::escape_string($row['code'])."', access_url_id = '1' ";
  304. iDatabase::query($sql);
  305. }
  306. }
  307. }
  308. }
  309. /* // This fragment of code is not necessary so far.
  310. // Get the courses databases queries list (c_q_list)
  311. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-post.sql', 'course');
  312. if (count($c_q_list) > 0) {
  313. // Get the courses list
  314. if (strlen($dbNameForm) > 40) {
  315. $app['monolog']->addError('Database name '.$dbNameForm.' is too long, skipping');
  316. } elseif (!in_array($dbNameForm, $dblist)) {
  317. $app['monolog']->addError('Database '.$dbNameForm.' was not found, skipping');
  318. } else {
  319. iDatabase::select_db($dbNameForm);
  320. $res = iDatabase::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  321. if ($res === false) { die('Error while querying the courses list in update_db-1.8.7-1.8.8.inc.php'); }
  322. if (iDatabase::num_rows($res) > 0) {
  323. $i = 0;
  324. while ($row = iDatabase::fetch_array($res)) {
  325. $list[] = $row;
  326. $i++;
  327. }
  328. foreach ($list as $row) {
  329. // Now use the $c_q_list
  330. // We connect to the right DB first to make sure we can use the queries
  331. // without a database name
  332. $prefix_course = $prefix;
  333. if ($singleDbForm) {
  334. $prefix_course = $prefix.$row['db_name']."_";
  335. } else {
  336. iDatabase::select_db($row['db_name']);
  337. }
  338. foreach($c_q_list as $query) {
  339. if ($singleDbForm) { //otherwise just use the main one
  340. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix$2$3", $query);
  341. }
  342. if ($only_test) {
  343. $app['monolog']->addInfo("iDatabase::query(".$row['db_name'].",$query)");
  344. } else {
  345. $res = iDatabase::query($query);
  346. if ($log) {
  347. $app['monolog']->addInfo("In ".$row['db_name'].", executed: $query");
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. */
  356. } else {
  357. echo 'You are not allowed here !' . __FILE__;
  358. }