update-db-1.6.x-1.8.0.inc.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * Update the Dokeos database from an older version
  23. * Notice : This script has to be included by index.php or update_courses.php
  24. *
  25. * @package dokeos.install
  26. * @todo
  27. * - conditional changing of tables. Currently we execute for example
  28. * ALTER TABLE `$dbNameForm`.`cours` instructions without checking wether this is necessary.
  29. * - reorganise code into functions
  30. * @todo use database library
  31. ==============================================================================
  32. */
  33. //load helper functions
  34. require_once("install_upgrade.lib.php");
  35. //remove memory and time limits as much as possible as this might be a long process...
  36. if(function_exists('ini_set'))
  37. {
  38. ini_set('memory_limit',-1);
  39. ini_set('max_execution_time',0);
  40. }else{
  41. error_log('Update-db script: could not change memory and time limits',0);
  42. }
  43. /*
  44. ==============================================================================
  45. MAIN CODE
  46. ==============================================================================
  47. */
  48. //check if we come from index.php or update_courses.php - otherwise display error msg
  49. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  50. {
  51. //check if the current Dokeos install is elligible for update
  52. if (empty ($updateFromConfigFile) || !file_exists($_POST['updatePath'].$updateFromConfigFile) || !in_array(get_config_param('clarolineVersion'), $update_from_version_6))
  53. {
  54. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  55. '.get_lang('PleasGoBackToStep1').'.
  56. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  57. </td></tr></table></form></body></html>';
  58. exit ();
  59. }
  60. //get_config_param() comes from install_functions.inc.php and
  61. //actually gets the param from
  62. $_configuration['db_glue'] = get_config_param('dbGlu');
  63. if ($singleDbForm)
  64. {
  65. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  66. $_configuration['main_database'] = get_config_param('mainDbName');
  67. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  68. }
  69. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  70. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  71. {
  72. $dbScormForm = $dbPrefixForm.$dbScormForm;
  73. }
  74. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  75. {
  76. $dbScormForm = $dbPrefixForm.'scorm';
  77. }
  78. $res = @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  79. //if error on connection to the database, show error and exit
  80. if ($res === false)
  81. {
  82. //$no = mysql_errno();
  83. //$msg = mysql_error();
  84. //echo '<hr>['.$no.'] - '.$msg.'<hr>';
  85. echo get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br /><br />
  86. '.get_lang('PleaseCheckTheseValues').' :<br /><br />
  87. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br />
  88. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br />
  89. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br /><br />
  90. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  91. <p><button type="submit" class="back" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  92. </td></tr></table></form></body></html>';
  93. exit ();
  94. }
  95. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5
  96. @mysql_query("set session sql_mode='';");
  97. $dblistres = mysql_list_dbs();
  98. $dblist = array();
  99. while ($row = mysql_fetch_object($dblistres)) {
  100. $dblist[] = $row->Database;
  101. }
  102. /*
  103. -----------------------------------------------------------
  104. Normal upgrade procedure:
  105. start by updating main, statistic, user databases
  106. -----------------------------------------------------------
  107. */
  108. //if this script has been included by index.php, not update_courses.php, so
  109. // that we want to change the main databases as well...
  110. $only_test = false;
  111. $log = 0;
  112. if (defined('DOKEOS_INSTALL'))
  113. {
  114. if ($singleDbForm)
  115. {
  116. if(empty($dbStatsForm)) $dbStatsForm = $dbNameForm;
  117. if(empty($dbScormForm)) $dbScormForm = $dbNameForm;
  118. if(empty($dbUserForm)) $dbUserForm = $dbNameForm;
  119. }
  120. /**
  121. * Update the databases "pre" migration
  122. */
  123. include ("../lang/english/create_course.inc.php");
  124. if ($languageForm != 'english')
  125. {
  126. //languageForm has been escaped in index.php
  127. include ("../lang/$languageForm/create_course.inc.php");
  128. }
  129. //get the main queries list (m_q_list)
  130. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','main');
  131. if(count($m_q_list)>0)
  132. {
  133. //now use the $m_q_list
  134. /**
  135. * We connect to the right DB first to make sure we can use the queries
  136. * without a database name
  137. */
  138. if(strlen($dbNameForm)>40){
  139. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  140. }elseif(!in_array($dbNameForm,$dblist)){
  141. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  142. }else{
  143. mysql_select_db($dbNameForm);
  144. foreach($m_q_list as $query){
  145. if($only_test){
  146. error_log("mysql_query($dbNameForm,$query)",0);
  147. }else{
  148. $res = mysql_query($query);
  149. if($log)
  150. {
  151. error_log("In $dbNameForm, executed: $query",0);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. //get the stats queries list (s_q_list)
  158. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','stats');
  159. if(count($s_q_list)>0)
  160. {
  161. //now use the $s_q_list
  162. /**
  163. * We connect to the right DB first to make sure we can use the queries
  164. * without a database name
  165. */
  166. if(strlen($dbStatsForm)>40){
  167. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  168. }elseif(!in_array($dbStatsForm,$dblist)){
  169. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  170. }else{
  171. mysql_select_db($dbStatsForm);
  172. foreach($s_q_list as $query){
  173. if($only_test){
  174. error_log("mysql_query($dbStatsForm,$query)",0);
  175. }else{
  176. $res = mysql_query($query);
  177. if($log)
  178. {
  179. error_log("In $dbStatsForm, executed: $query",0);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. //get the user queries list (u_q_list)
  186. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','user');
  187. if(count($u_q_list)>0)
  188. {
  189. //now use the $u_q_list
  190. /**
  191. * We connect to the right DB first to make sure we can use the queries
  192. * without a database name
  193. */
  194. if(strlen($dbUserForm)>40){
  195. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  196. }elseif(!in_array($dbUserForm,$dblist)){
  197. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  198. }else{
  199. mysql_select_db($dbUserForm);
  200. foreach($u_q_list as $query){
  201. if($only_test){
  202. error_log("mysql_query($dbUserForm,$query)",0);
  203. error_log("In $dbUserForm, executed: $query",0);
  204. }else{
  205. $res = mysql_query($query);
  206. }
  207. }
  208. }
  209. }
  210. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  211. }
  212. /*
  213. -----------------------------------------------------------
  214. Update the Dokeos course databases
  215. this part can be accessed in two ways:
  216. - from the normal upgrade process
  217. - from the script update_courses.php,
  218. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  219. Every time this script is accessed, only
  220. MAX_COURSE_TRANSFER courses are upgraded.
  221. -----------------------------------------------------------
  222. */
  223. $prefix = '';
  224. if ($singleDbForm)
  225. {
  226. $prefix = $_configuration['table_prefix'];
  227. }
  228. //get the courses databases queries list (c_q_list)
  229. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
  230. if(count($c_q_list)>0)
  231. {
  232. //get the courses list
  233. if(strlen($dbNameForm)>40){
  234. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  235. }elseif(!in_array($dbNameForm,$dblist)){
  236. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  237. }else{
  238. mysql_select_db($dbNameForm);
  239. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  240. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  241. if(mysql_num_rows($res)>0)
  242. {
  243. $i=0;
  244. $list = array();
  245. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  246. while($row = mysql_fetch_array($res))
  247. {
  248. $list[] = $row;
  249. $i++;
  250. }
  251. foreach($list as $row_course)
  252. {
  253. //now use the $c_q_list
  254. /**
  255. * We connect to the right DB first to make sure we can use the queries
  256. * without a database name
  257. */
  258. if (!$singleDbForm) //otherwise just use the main one
  259. {
  260. mysql_select_db($row_course['db_name']);
  261. }
  262. foreach($c_q_list as $query)
  263. {
  264. if ($singleDbForm) //otherwise just use the main one
  265. {
  266. $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);
  267. }
  268. if($only_test)
  269. {
  270. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  271. }else{
  272. $res = mysql_query($query);
  273. if($log)
  274. {
  275. error_log("In ".$row_course['db_name'].", executed: $query",0);
  276. }
  277. }
  278. }
  279. //prepare reusable users list to avoid repetition of the SQL query, but only select
  280. //users from the current course to avoid blowing the memory limit
  281. $users_list = array();
  282. $sql_uc = "SELECT u.user_id as ui, u.firstname as fn, u.lastname as ln " .
  283. " FROM $dbNameForm.user u, $dbNameForm.course_rel_user cu " .
  284. " WHERE cu.course_code = '".$row_course['code']."' " .
  285. " AND u.user_id = cu.user_id";
  286. $res_uc = mysql_query($sql_uc);
  287. while($user_row = mysql_fetch_array($res_uc))
  288. {
  289. $users_list[$user_row['fn'].' '.$user_row['ln']] = $user_row['ui'];
  290. }
  291. //update course manually
  292. //update group_category.forum_state ?
  293. //update group_info.tutor_id (put it in group_tutor table?) ?
  294. //update group_info.forum_state, forum_id ?
  295. //update forum tables (migrate from bb_ tables to forum_ tables)
  296. //migrate categories
  297. $prefix_course = $prefix;
  298. if($singleDbForm)
  299. {
  300. $prefix_course = $prefix.$row_course['db_name']."_";
  301. }
  302. $sql_orig = "SELECT * FROM ".$prefix_course."bb_categories";
  303. $res_orig = mysql_query($sql_orig);
  304. $order = 1;
  305. while($row = mysql_fetch_array($res_orig)){
  306. $myorder = (empty($row['cat_order'])?$order:$row['cat_order']);
  307. $sql = "INSERT INTO ".$prefix_course."forum_category " .
  308. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  309. "('".$row['cat_id']."','".mysql_real_escape_string($row['cat_title'])."','','".$myorder."',0)";
  310. $res = mysql_query($sql);
  311. $lastcatid = mysql_insert_id();
  312. //error_log($sql,0);
  313. $order ++;
  314. //add item_property - forum categories were not put into item_properties before
  315. $sql = "INSERT INTO ".$prefix_course."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  316. "VALUES ('forum_category','1','$lastcatid','ForumCategoryAdded','1','1')";
  317. $res = mysql_query($sql);
  318. //error_log($sql,0);
  319. }
  320. $sql_orig = "SELECT * FROM ".$prefix_course."bb_forums ORDER BY forum_last_post_id desc";
  321. $res_orig = mysql_query($sql_orig);
  322. $order = 1;
  323. while($row = mysql_fetch_array($res_orig)){
  324. $sql = "INSERT INTO ".$prefix_course."forum_forum " .
  325. "(forum_id,forum_category,allow_edit,forum_comment," .
  326. "forum_title," .
  327. "forum_last_post, forum_threads," .
  328. "locked, forum_posts, " .
  329. "allow_new_threads, forum_order) VALUES " .
  330. "('".$row['forum_id']."','".$row['cat_id']."',1,'".mysql_real_escape_string($row['forum_desc'])."'," .
  331. "'".mysql_real_escape_string($row['forum_name'])."'," .
  332. "'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
  333. "0,'".$row['forum_posts']."'," .
  334. "1,$order)";
  335. //error_log($sql,0);
  336. $res = mysql_query($sql);
  337. $lastforumid = mysql_insert_id();
  338. $order++;
  339. //add item_property - forums were not put into item_properties before
  340. $sql = "INSERT INTO ".$prefix_course."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  341. "VALUES ('forum','1','$lastforumid','ForumAdded','1','1')";
  342. $res = mysql_query($sql);
  343. //error_log($sql,0);
  344. }
  345. $sql_orig = "SELECT * FROM ".$prefix_course."bb_topics";
  346. $res_orig = mysql_query($sql_orig);
  347. while($row = mysql_fetch_array($res_orig)){
  348. $name = $row['prenom'].' '.$row['nom'];
  349. //check if user id is reusable
  350. if($row['topic_poster'] <= 1 )
  351. {
  352. if(isset($users_list[$name]))
  353. {
  354. $poster_id = $users_list[$name];
  355. }
  356. else
  357. {
  358. $poster_id = $row['topic_poster'];
  359. }
  360. }
  361. //convert time from varchar to datetime
  362. $time = $row['topic_time'];
  363. $name = mysql_real_escape_string($name);
  364. $sql = "INSERT INTO ".$prefix_course."forum_thread " .
  365. "(thread_id,forum_id,thread_poster_id," .
  366. "locked,thread_replies,thread_sticky,thread_title," .
  367. "thread_poster_name, thread_date, thread_last_post," .
  368. "thread_views) VALUES " .
  369. "('".$row['topic_id']."','".$row['forum_id']."','".$poster_id."'," .
  370. "0,'".$row['topic_replies']."',0,'".mysql_real_escape_string($row['topic_title'])."'," .
  371. "'$name','$time','".$row['topic_last_post_id']."'," .
  372. "'".$row['topic_views']."')";
  373. //error_log($sql,0);
  374. $res = mysql_query($sql);
  375. $lastthreadid = mysql_insert_id();
  376. //add item_property - forum threads were not put into item_properties before
  377. $sql = "INSERT INTO ".$prefix_course."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  378. "VALUES ('forum_thread','1','$lastthreadid','ForumThreadAdded','1','1')";
  379. $res = mysql_query($sql);
  380. //error_log($sql,0);
  381. }
  382. $sql_orig = "SELECT * FROM ".$prefix_course."bb_posts bp, ".$prefix_course."bb_posts_text bpt WHERE bp.post_id = bpt.post_id";
  383. $res_orig = mysql_query($sql_orig);
  384. while($row = mysql_fetch_array($res_orig)){
  385. $name = $row['prenom'].' '.$row['nom'];
  386. //check if user id is reusable
  387. if($row['poster_id'] <= 0 )
  388. {
  389. if(isset($users_list[$name]))
  390. {
  391. $poster_id = $users_list[$name];
  392. }
  393. else
  394. {
  395. $poster_id = $row['poster_id'];
  396. }
  397. }
  398. //convert time from varchar to datetime
  399. $time = $row['post_time'];
  400. $name = mysql_real_escape_string($name);
  401. $sql = "INSERT INTO ".$prefix_course."forum_post " .
  402. "(post_id,forum_id,thread_id," .
  403. "poster_id,post_parent_id,visible, " .
  404. "post_title,poster_name, post_text, " .
  405. "post_date, post_notification) VALUES " .
  406. "('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
  407. "'".$poster_id."','".$row['parent_id']."',1," .
  408. "'".mysql_real_escape_string($row['post_title'])."','$name', '".mysql_real_escape_string($row['post_text'])."'," .
  409. "'$time',0)";
  410. //error_log($sql,0);
  411. $res = mysql_query($sql);
  412. $lastpostid = mysql_insert_id();
  413. //add item_property - forum threads were not put into item_properties before
  414. $sql = "INSERT INTO ".$prefix_course."item_property(tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  415. "VALUES ('forum_post','1','$lastpostid','ForumPostAdded','1','1')";
  416. $res = mysql_query($sql);
  417. //error_log($sql,0);
  418. }
  419. unset($users_list);
  420. $sql_orig = "SELECT id, tutor_id FROM ".$prefix_course."group_info";
  421. $res_orig = mysql_query($sql_orig);
  422. $order = 1;
  423. while($row = mysql_fetch_array($res_orig)){
  424. $sql = "INSERT INTO ".$prefix_course."group_rel_tutor " .
  425. "(user_id,group_id) VALUES " .
  426. "('".$row['tutor_id']."','".$row['id']."')";
  427. $res = mysql_query($sql);
  428. }
  429. }
  430. }
  431. }
  432. }
  433. //load the old-scorm to new-scorm migration script
  434. //TODO: deal with the fact that this should only act on MAX_COURSE_TRANSFER courses
  435. if(!$only_test){
  436. include('update-db-scorm-1.6.x-1.8.0.inc.php');
  437. }
  438. if (defined('DOKEOS_INSTALL'))
  439. {
  440. if ($singleDbForm)
  441. {
  442. if(empty($dbStatsForm)) $dbStatsForm = $dbNameForm;
  443. if(empty($dbScormForm)) $dbScormForm = $dbNameForm;
  444. if(empty($dbUserForm)) $dbUserForm = $dbNameForm;
  445. }
  446. //deal with migrate-db-1.6.x-1.8.0-post.sql
  447. //get the main queries list (m_q_list)
  448. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','main');
  449. if(count($m_q_list)>0)
  450. {
  451. //now use the $m_q_list
  452. /**
  453. * We connect to the right DB first to make sure we can use the queries
  454. * without a database name
  455. */
  456. if(strlen($dbNameForm)>40){
  457. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  458. }elseif(!in_array($dbNameForm,$dblist)){
  459. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  460. }else{
  461. mysql_select_db($dbNameForm);
  462. foreach($m_q_list as $query){
  463. if($only_test){
  464. error_log("mysql_query($dbNameForm,$query)",0);
  465. }else{
  466. $res = mysql_query($query);
  467. if($log)
  468. {
  469. error_log("In $dbNameForm, executed: $query",0);
  470. }
  471. }
  472. }
  473. }
  474. }
  475. //get the stats queries list (s_q_list)
  476. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','stats');
  477. if(count($s_q_list)>0)
  478. {
  479. //now use the $s_q_list
  480. /**
  481. * We connect to the right DB first to make sure we can use the queries
  482. * without a database name
  483. */
  484. if(strlen($dbStatsForm)>40){
  485. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  486. }elseif(!in_array($dbNameForm,$dblist)){
  487. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  488. }else{
  489. mysql_select_db($dbStatsForm);
  490. foreach($s_q_list as $query){
  491. if($only_test){
  492. error_log("mysql_query($dbStatsForm,$query)",0);
  493. }else{
  494. $res = mysql_query($query);
  495. if($log)
  496. {
  497. error_log("In $dbStatsForm, executed: $query",0);
  498. }
  499. }
  500. }
  501. }
  502. }
  503. //get the user queries list (u_q_list)
  504. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','user');
  505. if(count($u_q_list)>0)
  506. {
  507. //now use the $u_q_list
  508. /**
  509. * We connect to the right DB first to make sure we can use the queries
  510. * without a database name
  511. */
  512. if(strlen($dbUserForm)>40){
  513. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  514. }elseif(!in_array($dbUserForm,$dblist)){
  515. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  516. }else{
  517. mysql_select_db($dbUserForm);
  518. foreach($u_q_list as $query){
  519. if($only_test){
  520. error_log("mysql_query($dbUserForm,$query)",0);
  521. }else{
  522. $res = mysql_query($query);
  523. if($log)
  524. {
  525. error_log("In $dbUserForm, executed: $query",0);
  526. }
  527. }
  528. }
  529. }
  530. }
  531. //the SCORM database should need a drop in the post-migrate part. However, we will keep these tables a bit more, just in case...
  532. }
  533. //get the courses databases queries list (c_q_list)
  534. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','course');
  535. if(count($c_q_list)>0)
  536. {
  537. //get the courses list
  538. if(strlen($dbNameForm)>40){
  539. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  540. }elseif(!in_array($dbNameForm,$dblist)){
  541. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  542. }else{
  543. mysql_select_db($dbNameForm);
  544. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  545. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  546. if(mysql_num_rows($res)>0)
  547. {
  548. $i=0;
  549. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  550. while($row = mysql_fetch_array($res))
  551. {
  552. $list[] = $row;
  553. $i++;
  554. }
  555. foreach($list as $row)
  556. {
  557. //now use the $c_q_list
  558. /**
  559. * We connect to the right DB first to make sure we can use the queries
  560. * without a database name
  561. */
  562. $prefix_course = $prefix;
  563. if($singleDbForm)
  564. {
  565. $prefix_course = $prefix.$row['db_name']."_";
  566. }else{
  567. mysql_select_db($row['db_name']);
  568. }
  569. foreach($c_q_list as $query)
  570. {
  571. if ($singleDbForm) //otherwise just use the main one
  572. {
  573. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/',"$1 $prefix$2$3",$query);
  574. }
  575. if($only_test)
  576. {
  577. error_log("mysql_query(".$row['db_name'].",$query)",0);
  578. }else{
  579. $res = mysql_query($query);
  580. if($log)
  581. {
  582. error_log("In ".$row['db_name'].", executed: $query",0);
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. }
  590. // upgrade user categories sort
  591. $table_user_categories = $dbUserForm.'.user_course_category';
  592. $sql = 'SELECT * FROM '.$table_user_categories.' ORDER BY user_id, title';
  593. $rs = Database::query($sql, __FILE__, __LINE__);
  594. $sort = 0;
  595. $old_user = 0;
  596. while($cat = Database :: fetch_array($rs))
  597. {
  598. if($old_user != $cat['user_id'])
  599. {
  600. $old_user = $cat['user_id'];
  601. $sort = 0;
  602. }
  603. $sort++;
  604. $sql = 'UPDATE '.$table_user_categories.' SET
  605. sort = '.intval($sort).'
  606. WHERE id='.intval($cat['id']);
  607. Database::query($sql, __FILE__, __LINE__);
  608. }
  609. }
  610. else
  611. {
  612. echo 'You are not allowed here !';
  613. }
  614. ?>