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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></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><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
  92. </td></tr></table></form></body></html>';
  93. exit ();
  94. }
  95. $dblistres = mysql_list_dbs();
  96. $dblist = array();
  97. while ($row = mysql_fetch_object($dblistres)) {
  98. $dblist[] = $row->Database;
  99. }
  100. /*
  101. -----------------------------------------------------------
  102. Normal upgrade procedure:
  103. start by updating main, statistic, user databases
  104. -----------------------------------------------------------
  105. */
  106. //if this script has been included by index.php, not update_courses.php, so
  107. // that we want to change the main databases as well...
  108. $only_test = false;
  109. $log = 0;
  110. if (defined('DOKEOS_INSTALL'))
  111. {
  112. if ($singleDbForm)
  113. {
  114. if(empty($dbStatsForm)) $dbStatsForm = $dbNameForm;
  115. if(empty($dbScormForm)) $dbScormForm = $dbNameForm;
  116. if(empty($dbUserForm)) $dbUserForm = $dbNameForm;
  117. }
  118. /**
  119. * Update the databases "pre" migration
  120. */
  121. include ("../lang/english/create_course.inc.php");
  122. if ($languageForm != 'english')
  123. {
  124. //languageForm has been escaped in index.php
  125. include ("../lang/$languageForm/create_course.inc.php");
  126. }
  127. //get the main queries list (m_q_list)
  128. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','main');
  129. if(count($m_q_list)>0)
  130. {
  131. //now use the $m_q_list
  132. /**
  133. * We connect to the right DB first to make sure we can use the queries
  134. * without a database name
  135. */
  136. if(strlen($dbNameForm)>40){
  137. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  138. }elseif(!in_array($dbNameForm,$dblist)){
  139. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  140. }else{
  141. mysql_select_db($dbNameForm);
  142. foreach($m_q_list as $query){
  143. if($only_test){
  144. error_log("mysql_query($dbNameForm,$query)",0);
  145. }else{
  146. $res = mysql_query($query);
  147. if($log)
  148. {
  149. error_log("In $dbNameForm, executed: $query",0);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. //get the stats queries list (s_q_list)
  156. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','stats');
  157. if(count($s_q_list)>0)
  158. {
  159. //now use the $s_q_list
  160. /**
  161. * We connect to the right DB first to make sure we can use the queries
  162. * without a database name
  163. */
  164. if(strlen($dbStatsForm)>40){
  165. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  166. }elseif(!in_array($dbStatsForm,$dblist)){
  167. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  168. }else{
  169. mysql_select_db($dbStatsForm);
  170. foreach($s_q_list as $query){
  171. if($only_test){
  172. error_log("mysql_query($dbStatsForm,$query)",0);
  173. }else{
  174. $res = mysql_query($query);
  175. if($log)
  176. {
  177. error_log("In $dbStatsForm, executed: $query",0);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. //get the user queries list (u_q_list)
  184. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','user');
  185. if(count($u_q_list)>0)
  186. {
  187. //now use the $u_q_list
  188. /**
  189. * We connect to the right DB first to make sure we can use the queries
  190. * without a database name
  191. */
  192. if(strlen($dbUserForm)>40){
  193. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  194. }elseif(!in_array($dbUserForm,$dblist)){
  195. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  196. }else{
  197. mysql_select_db($dbUserForm);
  198. foreach($u_q_list as $query){
  199. if($only_test){
  200. error_log("mysql_query($dbUserForm,$query)",0);
  201. error_log("In $dbUserForm, executed: $query",0);
  202. }else{
  203. $res = mysql_query($query);
  204. }
  205. }
  206. }
  207. }
  208. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  209. }
  210. /*
  211. -----------------------------------------------------------
  212. Update the Dokeos course databases
  213. this part can be accessed in two ways:
  214. - from the normal upgrade process
  215. - from the script update_courses.php,
  216. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  217. Every time this script is accessed, only
  218. MAX_COURSE_TRANSFER courses are upgraded.
  219. -----------------------------------------------------------
  220. */
  221. $prefix = '';
  222. if ($singleDbForm)
  223. {
  224. $prefix = $_configuration['table_prefix'];
  225. }
  226. //get the courses databases queries list (c_q_list)
  227. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
  228. if(count($c_q_list)>0)
  229. {
  230. //get the courses list
  231. if(strlen($dbNameForm)>40){
  232. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  233. }elseif(!in_array($dbNameForm,$dblist)){
  234. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  235. }else{
  236. mysql_select_db($dbNameForm);
  237. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  238. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  239. if(mysql_num_rows($res)>0)
  240. {
  241. $i=0;
  242. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  243. while($row = mysql_fetch_array($res))
  244. {
  245. $list[] = $row;
  246. $i++;
  247. }
  248. foreach($list as $row_course)
  249. {
  250. //now use the $c_q_list
  251. /**
  252. * We connect to the right DB first to make sure we can use the queries
  253. * without a database name
  254. */
  255. if (!$singleDbForm) //otherwise just use the main one
  256. {
  257. mysql_select_db($row_course['db_name']);
  258. }
  259. foreach($c_q_list as $query)
  260. {
  261. if ($singleDbForm) //otherwise just use the main one
  262. {
  263. $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);
  264. }
  265. if($only_test)
  266. {
  267. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  268. }else{
  269. $res = mysql_query($query);
  270. if($log)
  271. {
  272. error_log("In ".$row_course['db_name'].", executed: $query",0);
  273. }
  274. }
  275. }
  276. //prepare reusable users list to avoid repetition of the SQL query, but only select
  277. //users from the current course to avoid blowing the memory limit
  278. $users_list = array();
  279. $sql_uc = "SELECT u.user_id as ui, u.firstname as fn, u.lastname as ln " .
  280. " FROM $dbNameForm.user u, $dbNameForm.course_rel_user cu " .
  281. " WHERE cu.course_code = '".$row_course['code']."' " .
  282. " AND u.user_id = cu.user_id";
  283. $res_uc = mysql_query($sql_uc);
  284. while($user_row = mysql_fetch_array($res_uc))
  285. {
  286. $users_list[$user_row['fn'].' '.$user_row['ln']] = $user_row['ui'];
  287. }
  288. //update course manually
  289. //update group_category.forum_state ?
  290. //update group_info.tutor_id (put it in group_tutor table?) ?
  291. //update group_info.forum_state, forum_id ?
  292. //update forum tables (migrate from bb_ tables to forum_ tables)
  293. //migrate categories
  294. $prefix_course = $prefix;
  295. if($singleDbForm)
  296. {
  297. $prefix_course = $prefix.$row_course['db_name']."_";
  298. }
  299. $sql_orig = "SELECT * FROM ".$prefix_course."bb_categories";
  300. $res_orig = mysql_query($sql_orig);
  301. $order = 1;
  302. while($row = mysql_fetch_array($res_orig)){
  303. $myorder = (empty($row['cat_order'])?$order:$row['cat_order']);
  304. $sql = "INSERT INTO ".$prefix_course."forum_category " .
  305. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  306. "('".$row['cat_id']."','".mysql_real_escape_string($row['cat_title'])."','','".$myorder."',0)";
  307. $res = mysql_query($sql);
  308. $lastcatid = mysql_insert_id();
  309. //error_log($sql,0);
  310. $order ++;
  311. //add item_property - forum categories were not put into item_properties before
  312. $sql = "INSERT INTO ".$prefix_course."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  313. "VALUES ('forum_category','1','$lastcatid','ForumCategoryAdded','1','1')";
  314. $res = mysql_query($sql);
  315. //error_log($sql,0);
  316. }
  317. $sql_orig = "SELECT * FROM ".$prefix_course."bb_forums ORDER BY forum_last_post_id desc";
  318. $res_orig = mysql_query($sql_orig);
  319. $order = 1;
  320. while($row = mysql_fetch_array($res_orig)){
  321. $sql = "INSERT INTO ".$prefix_course."forum_forum " .
  322. "(forum_id,forum_category,allow_edit,forum_comment," .
  323. "forum_title," .
  324. "forum_last_post, forum_threads," .
  325. "locked, forum_posts, " .
  326. "allow_new_threads, forum_order) VALUES " .
  327. "('".$row['forum_id']."','".$row['cat_id']."',1,'".mysql_real_escape_string($row['forum_desc'])."'," .
  328. "'".mysql_real_escape_string($row['forum_name'])."'," .
  329. "'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
  330. "0,'".$row['forum_posts']."'," .
  331. "1,$order)";
  332. //error_log($sql,0);
  333. $res = mysql_query($sql);
  334. $lastforumid = mysql_insert_id();
  335. $order++;
  336. //add item_property - forums were not put into item_properties before
  337. $sql = "INSERT INTO ".$prefix_course."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  338. "VALUES ('forum','1','$lastforumid','ForumAdded','1','1')";
  339. $res = mysql_query($sql);
  340. //error_log($sql,0);
  341. }
  342. $sql_orig = "SELECT * FROM ".$prefix_course."bb_topics";
  343. $res_orig = mysql_query($sql_orig);
  344. while($row = mysql_fetch_array($res_orig)){
  345. $name = $row['prenom'].' '.$row['nom'];
  346. //check if user id is reusable
  347. if($row['topic_poster'] <= 1 )
  348. {
  349. if(isset($users_list[$name]))
  350. {
  351. $poster_id = $users_list[$name];
  352. }
  353. else
  354. {
  355. $poster_id = $row['topic_poster'];
  356. }
  357. }
  358. //convert time from varchar to datetime
  359. $time = $row['topic_time'];
  360. $name = mysql_real_escape_string($name);
  361. $sql = "INSERT INTO ".$prefix_course."forum_thread " .
  362. "(thread_id,forum_id,thread_poster_id," .
  363. "locked,thread_replies,thread_sticky,thread_title," .
  364. "thread_poster_name, thread_date, thread_last_post," .
  365. "thread_views) VALUES " .
  366. "('".$row['topic_id']."','".$row['forum_id']."','".$poster_id."'," .
  367. "0,'".$row['topic_replies']."',0,'".mysql_real_escape_string($row['topic_title'])."'," .
  368. "'$name','$time','".$row['topic_last_post_id']."'," .
  369. "'".$row['topic_views']."')";
  370. //error_log($sql,0);
  371. $res = mysql_query($sql);
  372. $lastthreadid = mysql_insert_id();
  373. //add item_property - forum threads were not put into item_properties before
  374. $sql = "INSERT INTO ".$prefix_course."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  375. "VALUES ('forum_thread','1','$lastthreadid','ForumThreadAdded','1','1')";
  376. $res = mysql_query($sql);
  377. //error_log($sql,0);
  378. }
  379. $sql_orig = "SELECT * FROM ".$prefix_course."bb_posts bp, ".$prefix_course."bb_posts_text bpt WHERE bp.post_id = bpt.post_id";
  380. $res_orig = mysql_query($sql_orig);
  381. while($row = mysql_fetch_array($res_orig)){
  382. $name = $row['prenom'].' '.$row['nom'];
  383. //check if user id is reusable
  384. if($row['poster_id'] <= 0 )
  385. {
  386. if(isset($users_list[$name]))
  387. {
  388. $poster_id = $users_list[$name];
  389. }
  390. else
  391. {
  392. $poster_id = $row['poster_id'];
  393. }
  394. }
  395. //convert time from varchar to datetime
  396. $time = $row['post_time'];
  397. $name = mysql_real_escape_string($name);
  398. $sql = "INSERT INTO ".$prefix_course."forum_post " .
  399. "(post_id,forum_id,thread_id," .
  400. "poster_id,post_parent_id,visible, " .
  401. "post_title,poster_name, post_text, " .
  402. "post_date, post_notification) VALUES " .
  403. "('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
  404. "'".$poster_id."','".$row['parent_id']."',1," .
  405. "'".mysql_real_escape_string($row['post_title'])."','$name', '".mysql_real_escape_string($row['post_text'])."'," .
  406. "'$time',0)";
  407. //error_log($sql,0);
  408. $res = mysql_query($sql);
  409. $lastpostid = mysql_insert_id();
  410. //add item_property - forum threads were not put into item_properties before
  411. $sql = "INSERT INTO ".$prefix_course."item_property(tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  412. "VALUES ('forum_post','1','$lastpostid','ForumPostAdded','1','1')";
  413. $res = mysql_query($sql);
  414. //error_log($sql,0);
  415. }
  416. unset($users_list);
  417. $sql_orig = "SELECT id, tutor_id FROM ".$prefix_course."group_info";
  418. $res_orig = mysql_query($sql_orig);
  419. $order = 1;
  420. while($row = mysql_fetch_array($res_orig)){
  421. $sql = "INSERT INTO ".$prefix_course."group_rel_tutor " .
  422. "(user_id,group_id) VALUES " .
  423. "('".$row['tutor_id']."','".$row['id']."')";
  424. $res = mysql_query($sql);
  425. }
  426. }
  427. }
  428. }
  429. }
  430. //load the old-scorm to new-scorm migration script
  431. //TODO: deal with the fact that this should only act on MAX_COURSE_TRANSFER courses
  432. if(!$only_test){
  433. include('update-db-scorm-1.6.x-1.8.0.inc.php');
  434. }
  435. if (defined('DOKEOS_INSTALL'))
  436. {
  437. if ($singleDbForm)
  438. {
  439. if(empty($dbStatsForm)) $dbStatsForm = $dbNameForm;
  440. if(empty($dbScormForm)) $dbScormForm = $dbNameForm;
  441. if(empty($dbUserForm)) $dbUserForm = $dbNameForm;
  442. }
  443. //deal with migrate-db-1.6.x-1.8.0-post.sql
  444. //get the main queries list (m_q_list)
  445. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','main');
  446. if(count($m_q_list)>0)
  447. {
  448. //now use the $m_q_list
  449. /**
  450. * We connect to the right DB first to make sure we can use the queries
  451. * without a database name
  452. */
  453. if(strlen($dbNameForm)>40){
  454. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  455. }elseif(!in_array($dbNameForm,$dblist)){
  456. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  457. }else{
  458. mysql_select_db($dbNameForm);
  459. foreach($m_q_list as $query){
  460. if($only_test){
  461. error_log("mysql_query($dbNameForm,$query)",0);
  462. }else{
  463. $res = mysql_query($query);
  464. if($log)
  465. {
  466. error_log("In $dbNameForm, executed: $query",0);
  467. }
  468. }
  469. }
  470. }
  471. }
  472. //get the stats queries list (s_q_list)
  473. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','stats');
  474. if(count($s_q_list)>0)
  475. {
  476. //now use the $s_q_list
  477. /**
  478. * We connect to the right DB first to make sure we can use the queries
  479. * without a database name
  480. */
  481. if(strlen($dbStatsForm)>40){
  482. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  483. }elseif(!in_array($dbNameForm,$dblist)){
  484. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  485. }else{
  486. mysql_select_db($dbStatsForm);
  487. foreach($s_q_list as $query){
  488. if($only_test){
  489. error_log("mysql_query($dbStatsForm,$query)",0);
  490. }else{
  491. $res = mysql_query($query);
  492. if($log)
  493. {
  494. error_log("In $dbStatsForm, executed: $query",0);
  495. }
  496. }
  497. }
  498. }
  499. }
  500. //get the user queries list (u_q_list)
  501. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','user');
  502. if(count($u_q_list)>0)
  503. {
  504. //now use the $u_q_list
  505. /**
  506. * We connect to the right DB first to make sure we can use the queries
  507. * without a database name
  508. */
  509. if(strlen($dbUserForm)>40){
  510. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  511. }elseif(!in_array($dbUserForm,$dblist)){
  512. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  513. }else{
  514. mysql_select_db($dbUserForm);
  515. foreach($u_q_list as $query){
  516. if($only_test){
  517. error_log("mysql_query($dbUserForm,$query)",0);
  518. }else{
  519. $res = mysql_query($query);
  520. if($log)
  521. {
  522. error_log("In $dbUserForm, executed: $query",0);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. //the SCORM database should need a drop in the post-migrate part. However, we will keep these tables a bit more, just in case...
  529. }
  530. //get the courses databases queries list (c_q_list)
  531. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','course');
  532. if(count($c_q_list)>0)
  533. {
  534. //get the courses list
  535. if(strlen($dbNameForm)>40){
  536. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  537. }elseif(!in_array($dbNameForm,$dblist)){
  538. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  539. }else{
  540. mysql_select_db($dbNameForm);
  541. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  542. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  543. if(mysql_num_rows($res)>0)
  544. {
  545. $i=0;
  546. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  547. while($row = mysql_fetch_array($res))
  548. {
  549. $list[] = $row;
  550. $i++;
  551. }
  552. foreach($list as $row)
  553. {
  554. //now use the $c_q_list
  555. /**
  556. * We connect to the right DB first to make sure we can use the queries
  557. * without a database name
  558. */
  559. $prefix_course = $prefix;
  560. if($singleDbForm)
  561. {
  562. $prefix_course = $prefix.$row['db_name']."_";
  563. }else{
  564. mysql_select_db($row['db_name']);
  565. }
  566. foreach($c_q_list as $query)
  567. {
  568. if ($singleDbForm) //otherwise just use the main one
  569. {
  570. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/',"$1 $prefix$2$3",$query);
  571. }
  572. if($only_test)
  573. {
  574. error_log("mysql_query(".$row['db_name'].",$query)",0);
  575. }else{
  576. $res = mysql_query($query);
  577. if($log)
  578. {
  579. error_log("In ".$row['db_name'].", executed: $query",0);
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. }
  587. // upgrade user categories sort
  588. $table_user_categories = $dbUserForm.'.user_course_category';
  589. $sql = 'SELECT * FROM '.$table_user_categories.' ORDER BY user_id, title';
  590. $rs = api_sql_query($sql, __FILE__, __LINE__);
  591. $sort = 0;
  592. $old_user = 0;
  593. while($cat = Database :: fetch_array($rs))
  594. {
  595. if($old_user != $cat['user_id'])
  596. {
  597. $old_user = $cat['user_id'];
  598. $sort = 0;
  599. }
  600. $sort++;
  601. $sql = 'UPDATE '.$table_user_categories.' SET
  602. sort = '.intval($sort).'
  603. WHERE id='.intval($cat['id']);
  604. api_sql_query($sql, __FILE__, __LINE__);
  605. }
  606. }
  607. else
  608. {
  609. echo 'You are not allowed here !';
  610. }
  611. ?>