update-db-1.8.4-1.8.5.inc.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.4';
  20. $new_file_version = '1.8.5';
  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. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  73. } elseif (!in_array($dbNameForm, $dblist)) {
  74. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  75. } else {
  76. Database::select_db($dbNameForm);
  77. foreach ($m_q_list as $query) {
  78. if ($only_test) {
  79. error_log("Database::query($dbNameForm,$query)", 0);
  80. } else {
  81. $res = Database::query($query);
  82. if ($log) {
  83. error_log("In $dbNameForm, executed: $query", 0);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. // Get the stats queries list (s_q_list)
  90. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  91. if (count($s_q_list) > 0) {
  92. // Now use the $s_q_list
  93. /**
  94. * We connect to the right DB first to make sure we can use the queries
  95. * without a database name
  96. */
  97. if (strlen($dbStatsForm) > 40) {
  98. error_log('Database name '.$dbStatsForm.' is too long, skipping', 0);
  99. } elseif (!in_array($dbStatsForm, $dblist)) {
  100. error_log('Database '.$dbStatsForm.' was not found, skipping', 0);
  101. } else {
  102. Database::select_db($dbStatsForm);
  103. foreach ($s_q_list as $query) {
  104. if ($only_test) {
  105. error_log("Database::query($dbStatsForm,$query)", 0);
  106. } else {
  107. $res = Database::query($query);
  108. if ($log) {
  109. error_log("In $dbStatsForm, executed: $query", 0);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // Get the user queries list (u_q_list)
  116. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'user');
  117. if (count($u_q_list) > 0) {
  118. // Now use the $u_q_list
  119. /**
  120. * We connect to the right DB first to make sure we can use the queries
  121. * without a database name
  122. */
  123. if (strlen($dbUserForm) > 40) {
  124. error_log('Database name '.$dbUserForm.' is too long, skipping', 0);
  125. } elseif (!in_array($dbUserForm, $dblist)) {
  126. error_log('Database '.$dbUserForm.' was not found, skipping', 0);
  127. } else {
  128. Database::select_db($dbUserForm);
  129. foreach ($u_q_list as $query){
  130. if ($only_test) {
  131. error_log("Database::query($dbUserForm,$query)", 0);
  132. error_log("In $dbUserForm, executed: $query", 0);
  133. } else {
  134. $res = Database::query($query);
  135. }
  136. }
  137. }
  138. }
  139. // The SCORM database doesn't need a change in the pre-migrate part - ignore
  140. }
  141. $prefix = '';
  142. if ($singleDbForm) {
  143. $prefix = get_config_param ('table_prefix');
  144. }
  145. // Get the courses databases queries list (c_q_list)
  146. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  147. if (count($c_q_list) > 0) {
  148. //get the courses list
  149. if (strlen($dbNameForm) > 40) {
  150. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  151. } elseif (!in_array($dbNameForm, $dblist)) {
  152. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  153. } else {
  154. Database::select_db($dbNameForm);
  155. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  156. if ($res === false) { die('Error while querying the courses list in update_db-1.8.4-1.8.5.inc.php'); }
  157. if (Database::num_rows($res) > 0) {
  158. $i = 0;
  159. $list = array();
  160. while($row = Database::fetch_array($res)) {
  161. $list[] = $row;
  162. $i++;
  163. }
  164. foreach ($list as $row_course) {
  165. // Now use the $c_q_list
  166. /**
  167. * We connect to the right DB first to make sure we can use the queries
  168. * without a database name
  169. */
  170. if (!$singleDbForm) { //otherwise just use the main one
  171. Database::select_db($row_course['db_name']);
  172. }
  173. Log::notice('Course db ' . $row_course['db_name']);
  174. foreach ($c_q_list as $query) {
  175. if ($singleDbForm) {
  176. $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);
  177. }
  178. if ($only_test) {
  179. error_log("Database::query(".$row_course['db_name'].",$query)", 0);
  180. } else {
  181. $res = Database::query($query);
  182. if ($log) {
  183. error_log("In ".$row_course['db_name'].", executed: $query", 0);
  184. }
  185. }
  186. }
  187. $mytable = $row_course['db_name'].".lp_item";
  188. if ($singleDbForm) {
  189. $mytable = "$prefix{$row_course['db_name']}_lp_item";
  190. }
  191. $mysql = "SELECT * FROM $mytable WHERE min_score != 0 AND prerequisite != ''";
  192. $myres = Database::query($query);
  193. if ($myres !== false && Database::num_rows($myres) > 0) {
  194. while ($myrow = Database::fetch_array($myres)) {
  195. if (is_numeric($myrow['prerequisite'])) {
  196. $mysql2 = "UPDATE $mytable SET mastery_score = '".$myrow['min_score']."' WHERE id = '".$myrow['prerequisite']."'";
  197. $myres2 = Database::query($mysql2);
  198. //echo $mysql2."<br />";
  199. if ($myres2 !== false) {
  200. $mysql3 = "UPDATE $mytable SET min_score = 0 WHERE id = '".$myrow['id']."'";
  201. $myres3 = Database::query($mysql3);
  202. //echo $mysql3."<br />";
  203. }
  204. }
  205. }
  206. }
  207. // Work Tool Folder Update
  208. // We search into DB all the folders in the work tool
  209. if ($singleDbForm) {
  210. $my_course_table = "$prefix{$row_course['db_name']}_student_publication";
  211. } else {
  212. $my_course_table = $row_course['db_name'].".student_publication";
  213. }
  214. $sys_course_path = $_configuration['root_sys'].$_configuration['course_folder'];
  215. $course_dir = $sys_course_path.$row_course['directory'].'/work';
  216. $dir_to_array = my_directory_to_array($course_dir, true);
  217. $only_dir = array();
  218. $sql_select = "SELECT filetype FROM " . $my_course_table . " WHERE filetype = 'folder'";
  219. $result = Database::query($sql_select);
  220. $num_row = Database::num_rows($result);
  221. // Check if there are already folder registered
  222. if ($num_row == 0) {
  223. for ($i = 0; $i < count($dir_to_array); $i++) {
  224. $only_dir[] = substr($dir_to_array[$i], strlen($course_dir), strlen($dir_to_array[$i]));
  225. }
  226. for ($i = 0; $i < count($only_dir); $i++) {
  227. $sql_insert_all= "INSERT INTO " . $my_course_table . " SET url = '" . $only_dir[$i] . "', " .
  228. "title = '',
  229. description = '',
  230. author = '',
  231. active = '0',
  232. accepted = '1',
  233. filetype = 'folder',
  234. post_group_id = '0',
  235. sent_date = '0000-00-00 00:00:00' ";
  236. Database::query($sql_insert_all);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. } else {
  244. echo 'You are not allowed here !' . __FILE__;
  245. }