update-db-1.9.0-1.10.0.inc.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.9.0';
  20. $new_file_version = '1.10.0';
  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. 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. Log::error('Database name '.$dbNameForm.' is too long, skipping');
  72. } elseif (!in_array($dbNameForm, $dblist)) {
  73. Log::error('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. Log::notice("iDatabase::query($dbNameForm,$query)");
  79. } else {
  80. $res = iDatabase::query($query);
  81. if ($res === false) {
  82. Log::error('Error in '.$query.': '.iDatabase::error());
  83. }
  84. }
  85. }
  86. }
  87. }
  88. if (INSTALL_TYPE_UPDATE == 'update') {
  89. $session_table = "$dbNameForm.session";
  90. $session_rel_course_table = "$dbNameForm.session_rel_course";
  91. $session_rel_course_rel_user_table = "$dbNameForm.session_rel_course_rel_user";
  92. $course_table = "$dbNameForm.course";
  93. //Fixes new changes in sessions
  94. $sql = "SELECT id, date_start, date_end, nb_days_access_before_beginning, nb_days_access_after_end FROM $session_table ";
  95. $result = iDatabase::query($sql);
  96. while ($session = Database::fetch_array($result)) {
  97. $session_id = $session['id'];
  98. //Fixing date_start
  99. if (isset($session['date_start']) && !empty($session['date_start']) && $session['date_start'] != '0000-00-00') {
  100. $datetime = $session['date_start'].' 00:00:00';
  101. $update_sql = "UPDATE $session_table SET display_start_date = '$datetime' , access_start_date = '$datetime' WHERE id = $session_id";
  102. iDatabase::query($update_sql);
  103. //Fixing nb_days_access_before_beginning
  104. if (!empty($session['nb_days_access_before_beginning'])) {
  105. $datetime = api_strtotime($datetime, 'UTC') - (86400*$session['nb_days_access_before_beginning']);
  106. $datetime = api_get_utc_datetime($datetime);
  107. $update_sql = "UPDATE $session_table SET coach_access_start_date = '$datetime' WHERE id = $session_id";
  108. iDatabase::query($update_sql);
  109. }
  110. }
  111. //Fixing end_date
  112. if (isset($session['date_end']) && !empty($session['date_end']) && $session['date_end'] != '0000-00-00') {
  113. $datetime = $session['date_end'].' 00:00:00';
  114. $update_sql = "UPDATE $session_table SET display_end_date = '$datetime' , access_end_date = '$datetime' WHERE id = $session_id";
  115. iDatabase::query($update_sql);
  116. //Fixing nb_days_access_before_beginning
  117. if (!empty($session['nb_days_access_after_end'])) {
  118. $datetime = api_strtotime($datetime, 'UTC') + (86400*$session['nb_days_access_after_end']);
  119. $datetime = api_get_utc_datetime($datetime);
  120. $update_sql = "UPDATE $session_table SET coach_access_end_date = '$datetime' WHERE id = $session_id";
  121. iDatabase::query($update_sql);
  122. }
  123. }
  124. }
  125. //Fixes new changes session_rel_course
  126. $sql = "SELECT id_session, sc.course_code, c.id FROM $course_table c INNER JOIN $session_rel_course_table sc ON sc.course_code = c.code";
  127. $result = iDatabase::query($sql);
  128. while ($row = Database::fetch_array($result)) {
  129. $sql = "UPDATE $session_rel_course_table SET course_id = {$row['id']} WHERE course_code = {$row['course_code']} AND id_session = {$row['id_session']} ";
  130. iDatabase::query($sql);
  131. }
  132. //Fixes new changes in session_rel_course_rel_user
  133. $sql = "SELECT id_session, sc.course_code, c.id FROM $course_table c INNER JOIN $session_rel_course_rel_user_table sc ON sc.course_code = c.code";
  134. $result = iDatabase::query($sql);
  135. while ($row = Database::fetch_array($result)) {
  136. $sql = "UPDATE $session_rel_course_rel_user_table SET course_id = {$row['id']} WHERE course_code = {$row['course_code']} AND id_session = {$row['id_session']} ";
  137. iDatabase::query($sql);
  138. }
  139. }
  140. }
  141. } else {
  142. echo 'You are not allowed here !' . __FILE__;
  143. }