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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2007 Dokeos S.A.
  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, 44 rue des palais, 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. /*
  36. ==============================================================================
  37. MAIN CODE
  38. ==============================================================================
  39. */
  40. //check if we come from index.php or update_courses.php - otherwise display error msg
  41. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  42. {
  43. //check if the current Dokeos install is elligible for update
  44. if (empty ($updateFromConfigFile) || !file_exists($_POST['updatePath'].$updateFromConfigFile) || !in_array(get_config_param('clarolineVersion'), $update_from_version))
  45. {
  46. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  47. '.get_lang('PleasGoBackToStep1').'.
  48. <p><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></p>
  49. </td></tr></table></form></body></html>';
  50. exit ();
  51. }
  52. //get_config_param() comes from install_functions.inc.php and
  53. //actually gets the param from
  54. $_configuration['db_glue'] = get_config_param('dbGlu');
  55. if ($singleDbForm)
  56. {
  57. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  58. }
  59. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  60. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  61. {
  62. $dbScormForm = $dbPrefixForm.$dbScormForm;
  63. }
  64. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  65. {
  66. $dbScormForm = $dbPrefixForm.'scorm';
  67. }
  68. @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  69. //if error on connection to the database, show error and exit
  70. if (mysql_errno() > 0)
  71. {
  72. $no = mysql_errno();
  73. $msg = mysql_error();
  74. echo '<hr>['.$no.'] - '.$msg.'<hr>
  75. '.get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br><br>
  76. '.get_lang('PleaseCheckTheseValues').' :<br><br>
  77. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br>
  78. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br>
  79. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br><br>
  80. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  81. <p><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
  82. </td></tr></table></form></body></html>';
  83. exit ();
  84. }
  85. /*
  86. -----------------------------------------------------------
  87. Normal upgrade procedure:
  88. start by updating main, statistic, user databases
  89. -----------------------------------------------------------
  90. */
  91. //if this script has been included by index.php, not update_courses.php, so
  92. // that we want to change the main databases as well...
  93. $only_test = false;
  94. if (defined('DOKEOS_INSTALL'))
  95. {
  96. /**
  97. * Update the databases "pre" migration
  98. */
  99. include ("../lang/english/create_course.inc.php");
  100. if ($languageForm != 'english')
  101. {
  102. //languageForm has been escaped in index.php
  103. include ("../lang/$languageForm/create_course.inc.php");
  104. }
  105. //get the main queries list (m_q_list)
  106. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','main');
  107. if(count($m_q_list)>0)
  108. {
  109. //now use the $m_q_list
  110. /**
  111. * We connect to the right DB first to make sure we can use the queries
  112. * without a database name
  113. */
  114. mysql_select_db($dbNameForm);
  115. foreach($m_q_list as $query){
  116. if($only_test){
  117. error_log("mysql_query($dbNameForm,$query)",0);
  118. }else{
  119. $res = mysql_query($query);
  120. error_log("In $dbNameForm, executed: $query",0);
  121. }
  122. }
  123. }
  124. //get the stats queries list (s_q_list)
  125. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','stats');
  126. if(count($s_q_list)>0)
  127. {
  128. //now use the $s_q_list
  129. /**
  130. * We connect to the right DB first to make sure we can use the queries
  131. * without a database name
  132. */
  133. mysql_select_db($dbStatsForm);
  134. foreach($s_q_list as $query){
  135. if($only_test){
  136. error_log("mysql_query($dbStatsForm,$query)",0);
  137. }else{
  138. $res = mysql_query($query);
  139. error_log("In $dbStatsForm, executed: $query",0);
  140. }
  141. }
  142. }
  143. //get the user queries list (u_q_list)
  144. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','user');
  145. if(count($u_q_list)>0)
  146. {
  147. //now use the $u_q_list
  148. /**
  149. * We connect to the right DB first to make sure we can use the queries
  150. * without a database name
  151. */
  152. mysql_select_db($dbUserForm);
  153. foreach($u_q_list as $query){
  154. if($only_test){
  155. error_log("mysql_query($dbUserForm,$query)",0);
  156. error_log("In $dbUserForm, executed: $query",0);
  157. }else{
  158. $res = mysql_query($query);
  159. }
  160. }
  161. }
  162. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  163. }
  164. /*
  165. -----------------------------------------------------------
  166. Update the Dokeos course databases
  167. this part can be accessed in two ways:
  168. - from the normal upgrade process
  169. - from the script update_courses.php,
  170. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  171. Every time this script is accessed, only
  172. MAX_COURSE_TRANSFER courses are upgraded.
  173. -----------------------------------------------------------
  174. */
  175. //get the courses databases queries list (c_q_list)
  176. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
  177. if(count($c_q_list)>0)
  178. {
  179. //get the courses list
  180. mysql_select_db($dbNameForm);
  181. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  182. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  183. if(mysql_num_rows($res)>0)
  184. {
  185. $i=0;
  186. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  187. while($row = mysql_fetch_array($res))
  188. {
  189. $list[] = $row;
  190. $i++;
  191. }
  192. foreach($list as $row)
  193. {
  194. //now use the $c_q_list
  195. /**
  196. * We connect to the right DB first to make sure we can use the queries
  197. * without a database name
  198. */
  199. mysql_select_db($row['db_name']);
  200. foreach($c_q_list as $query)
  201. {
  202. if($only_test)
  203. {
  204. error_log("mysql_query(".$row['db_name'].",$query)",0);
  205. }else{
  206. $res = mysql_query($query);
  207. error_log("In ".$row['db_name'].", executed: $query",0);
  208. }
  209. }
  210. //update course manually
  211. //update group_category.forum_state ?
  212. //update group_info.tutor_id (put it in group_tutor table?) ?
  213. //update group_info.forum_state, forum_id ?
  214. //update forum tables (migrate from bb_ tables to forum_ tables)
  215. //migrate categories
  216. $sql_orig = "SELECT * FROM bb_categories";
  217. $res_orig = mysql_query($sql_orig);
  218. $order = 1;
  219. while($row = mysql_fetch_array($res_orig)){
  220. $myorder = (empty($row['cat_order'])?$order:$row['cat_order']);
  221. $sql = "INSERT INTO forum_category " .
  222. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  223. "('".$row['cat_id']."','".mysql_real_escape_string($row['cat_title'])."','','".$myorder."',0)";
  224. $res = mysql_query($sql);
  225. $lastcatid = mysql_insert_id();
  226. error_log($sql,0);
  227. $order ++;
  228. //add item_property - forum categories were not put into item_properties before
  229. $sql = "INSERT INTO item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  230. "VALUES ('forum_category','1','$lastcatid','ForumCategoryAdded','1','1')";
  231. $res = mysql_query($sql);
  232. error_log($sql,0);
  233. }
  234. $sql_orig = "SELECT * FROM bb_forums ORDER BY forum_last_post_id desc";
  235. $res_orig = mysql_query($sql_orig);
  236. $order = 1;
  237. while($row = mysql_fetch_array($res_orig)){
  238. $sql = "INSERT INTO forum_forum " .
  239. "(forum_id,forum_category,allow_edit,forum_comment," .
  240. "forum_title," .
  241. "forum_last_post, forum_threads," .
  242. "locked, forum_posts, " .
  243. "allow_new_threads, forum_order) VALUES " .
  244. "('".$row['forum_id']."','".$row['cat_id']."',1,'".mysql_real_escape_string($row['forum_desc'])."'," .
  245. "'".mysql_real_escape_string($row['forum_name'])."'," .
  246. "'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
  247. "0,'".$row['forum_posts']."'," .
  248. "1,$order)";
  249. error_log($sql,0);
  250. $res = mysql_query($sql);
  251. $lastforumid = mysql_insert_id();
  252. $order++;
  253. //add item_property - forums were not put into item_properties before
  254. $sql = "INSERT INTO item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  255. "VALUES ('forum','1','$lastforumid','ForumAdded','1','1')";
  256. $res = mysql_query($sql);
  257. error_log($sql,0);
  258. }
  259. $sql_orig = "SELECT * FROM bb_topics";
  260. $res_orig = mysql_query($sql_orig);
  261. while($row = mysql_fetch_array($res_orig)){
  262. //convert time from varchar to datetime
  263. $time = $row['topic_time'];
  264. $poster_id = ($row['topic_poster']==-1?1:$row['topic_poster']);
  265. $name = mysql_real_escape_string($row['prenom']." ".$row['nom']);
  266. $sql = "INSERT INTO forum_thread " .
  267. "(thread_id,forum_id,thread_poster_id," .
  268. "locked,thread_replies,thread_sticky,thread_title," .
  269. "thread_poster_name, thread_date, thread_last_post," .
  270. "thread_views) VALUES " .
  271. "('".$row['topic_id']."','".$row['forum_id']."','".$poster_id."'," .
  272. "0,'".$row['topic_replies']."',0,'".mysql_real_escape_string($row['topic_title'])."'," .
  273. "'$name','$time','".$row['topic_last_post_id']."'," .
  274. "'".$row['topic_views']."')";
  275. error_log($sql,0);
  276. $res = mysql_query($sql);
  277. $lastthreadid = mysql_insert_id();
  278. //add item_property - forum threads were not put into item_properties before
  279. $sql = "INSERT INTO item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  280. "VALUES ('forum_thread','1','$lastthreadid','ForumThreadAdded','1','1')";
  281. $res = mysql_query($sql);
  282. error_log($sql,0);
  283. }
  284. $sql_orig = "SELECT * FROM bb_posts, bb_posts_text WHERE bb_posts.post_id = bb_posts_text.post_id";
  285. $res_orig = mysql_query($sql_orig);
  286. while($row = mysql_fetch_array($res_orig)){
  287. //convert time from varchar to datetime
  288. $time = $row['post_time'];
  289. $poster_id = ($row['topic_poster']==-1?1:$row['topic_poster']);
  290. $name = mysql_real_escape_string($row['prenom']." ".$row['nom']);
  291. $sql = "INSERT INTO forum_post " .
  292. "(post_id,forum_id,thread_id," .
  293. "poster_id,post_parent_id,visible, " .
  294. "post_title,poster_name, post_text, " .
  295. "post_date, post_notification) VALUES " .
  296. "('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
  297. "'".$poster_id."','".$row['parent_id']."',1," .
  298. "'".mysql_real_escape_string($row['post_title'])."','$name', '".mysql_real_escape_string($row['post_text'])."'," .
  299. "'$time',0)";
  300. error_log($sql,0);
  301. $res = mysql_query($sql);
  302. $lastpostid = mysql_insert_id();
  303. //add item_property - forum threads were not put into item_properties before
  304. $sql = "INSERT INTO item_property(tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  305. "VALUES ('forum_post','1','$lastpostid','ForumPostAdded','1','1')";
  306. $res = mysql_query($sql);
  307. error_log($sql,0);
  308. }
  309. }
  310. }
  311. }
  312. //load the old-scorm to new-scorm migration script
  313. //TODO: deal with the fact that this should only act on MAX_COURSE_TRANSFER courses
  314. if(!$only_test){
  315. include('update-db-scorm-1.6.x-1.8.0.inc.php');
  316. }
  317. if (defined('DOKEOS_INSTALL'))
  318. {
  319. //deal with migrate-db-1.6.x-1.8.0-post.sql
  320. //get the main queries list (m_q_list)
  321. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','main');
  322. if(count($m_q_list)>0)
  323. {
  324. //now use the $m_q_list
  325. /**
  326. * We connect to the right DB first to make sure we can use the queries
  327. * without a database name
  328. */
  329. mysql_select_db($dbNameForm);
  330. foreach($m_q_list as $query){
  331. if($only_test){
  332. error_log("mysql_query($dbNameForm,$query)",0);
  333. }else{
  334. $res = mysql_query($query);
  335. error_log("In $dbNameForm, executed: $query",0);
  336. }
  337. }
  338. }
  339. //get the stats queries list (s_q_list)
  340. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','stats');
  341. if(count($s_q_list)>0)
  342. {
  343. //now use the $s_q_list
  344. /**
  345. * We connect to the right DB first to make sure we can use the queries
  346. * without a database name
  347. */
  348. mysql_select_db($dbStatsForm);
  349. foreach($s_q_list as $query){
  350. if($only_test){
  351. error_log("mysql_query($dbStatsForm,$query)",0);
  352. }else{
  353. $res = mysql_query($query);
  354. error_log("In $dbStatsForm, executed: $query",0);
  355. }
  356. }
  357. }
  358. //get the user queries list (u_q_list)
  359. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','user');
  360. if(count($u_q_list)>0)
  361. {
  362. //now use the $u_q_list
  363. /**
  364. * We connect to the right DB first to make sure we can use the queries
  365. * without a database name
  366. */
  367. mysql_select_db($dbUserForm);
  368. foreach($u_q_list as $query){
  369. if($only_test){
  370. error_log("mysql_query($dbUserForm,$query)",0);
  371. }else{
  372. $res = mysql_query($query);
  373. error_log("In $dbUserForm, executed: $query",0);
  374. }
  375. }
  376. }
  377. //the SCORM database should need a drop in the post-migrate part. However, we will keep these tables a bit more, just in case...
  378. }
  379. //get the courses databases queries list (c_q_list)
  380. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','course');
  381. if(count($c_q_list)>0)
  382. {
  383. //get the courses list
  384. mysql_select_db($dbNameForm);
  385. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  386. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  387. if(mysql_num_rows($res)>0)
  388. {
  389. $i=0;
  390. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  391. while($row = mysql_fetch_array($res))
  392. {
  393. $list[] = $row;
  394. $i++;
  395. }
  396. foreach($list as $row)
  397. {
  398. //now use the $c_q_list
  399. /**
  400. * We connect to the right DB first to make sure we can use the queries
  401. * without a database name
  402. */
  403. mysql_select_db($row['db_name']);
  404. foreach($c_q_list as $query)
  405. {
  406. if($only_test)
  407. {
  408. error_log("mysql_query(".$row['db_name'].",$query)",0);
  409. }else{
  410. $res = mysql_query($query);
  411. error_log("In ".$row['db_name'].", executed: $query",0);
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }
  418. else
  419. {
  420. echo 'You are not allowed here !';
  421. }
  422. ?>