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

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