update_db.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. /*
  3. THIS FILE IS DEPRECATED - ONLY REMAINING FOR LEGACY CODE STUDY FOR NOW - YW20070129
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2007 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) various contributors
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. ==============================================================================
  23. * Update the Dokeos database from an older version
  24. * Notice : This script has to be included by index.php or update_courses.php
  25. *
  26. * @package dokeos.install
  27. * @todo
  28. * - conditional changing of tables. Currently we execute for example
  29. * ALTER TABLE `$dbNameForm`.`cours` instructions without checking wether this is necessary.
  30. * - reorganise code into functions
  31. * @todo use database library
  32. ==============================================================================
  33. */
  34. //load helper functions
  35. require_once("install_upgrade.lib.php");
  36. /*
  37. ==============================================================================
  38. MAIN CODE
  39. ==============================================================================
  40. */
  41. //check if we come from index.php or update_courses.php - otherwise display error msg
  42. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  43. {
  44. //check if the current Dokeos install is elligible for update
  45. if (empty ($updateFromConfigFile) || !file_exists($_POST['updatePath'].$updateFromConfigFile) || !in_array(get_config_param('clarolineVersion'), $update_from_version))
  46. {
  47. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  48. '.get_lang('PleasGoBackToStep1').'.
  49. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  50. </td></tr></table></form></body></html>';
  51. exit ();
  52. }
  53. //get_config_param() comes from install_functions.inc.php and
  54. //actually gets the param from
  55. $_configuration['db_glue'] = get_config_param('dbGlu');
  56. if ($singleDbForm)
  57. {
  58. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  59. }
  60. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  61. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  62. {
  63. $dbScormForm = $dbPrefixForm.$dbScormForm;
  64. }
  65. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  66. {
  67. $dbScormForm = $dbPrefixForm.'scorm';
  68. }
  69. @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  70. //if error on connection to the database, show error and exit
  71. if (mysql_errno() > 0)
  72. {
  73. $no = mysql_errno();
  74. $msg = mysql_error();
  75. echo '<hr>['.$no.'] - '.$msg.'<hr>
  76. '.get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br><br>
  77. '.get_lang('PleaseCheckTheseValues').' :<br><br>
  78. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br>
  79. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br>
  80. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br><br>
  81. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  82. <p><buton class="back" type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  83. </td></tr></table></form></body></html>';
  84. exit ();
  85. }
  86. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5
  87. @mysql_query("set session sql_mode='';");
  88. /*
  89. -----------------------------------------------------------
  90. Normal upgrade procedure:
  91. start by updating main, statistic, user databases
  92. -----------------------------------------------------------
  93. */
  94. //if this script has been included by index.php, not update_courses.php, so
  95. // that we want to change the main databases as well...
  96. $only_test = false;
  97. if (defined('DOKEOS_INSTALL'))
  98. {
  99. /**
  100. * Update the databases "pre" migration
  101. */
  102. include ("../lang/english/create_course.inc.php");
  103. if ($languageForm != 'english')
  104. {
  105. //languageForm has been escaped in index.php
  106. include ("../lang/$languageForm/create_course.inc.php");
  107. }
  108. //TODO deal with migrate-db-1.6.x-1.8.0-pre.sql here
  109. //get the main queries list (m_q_list)
  110. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','main');
  111. if(count($m_q_list)>0)
  112. {
  113. //now use the $m_q_list
  114. /**
  115. * We connect to the right DB first to make sure we can use the queries
  116. * without a database name
  117. */
  118. mysql_select_db($dbNameForm);
  119. foreach($m_q_list as $query){
  120. if($only_test){
  121. echo "mysql_query($dbNameForm,$query)<br/>";
  122. }else{
  123. $res = mysql_query($query);
  124. }
  125. }
  126. }
  127. //manual updates in here
  128. //update all registration_date, expiration_date and active fields
  129. //$sql_upd = "UPDATE user SET registration_date=NOW()";
  130. //$res_upd = mysql_query($sql_upd);
  131. //end of manual updates
  132. //get the stats queries list (s_q_list)
  133. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','stats');
  134. if(count($s_q_list)>0)
  135. {
  136. //now use the $s_q_list
  137. /**
  138. * We connect to the right DB first to make sure we can use the queries
  139. * without a database name
  140. */
  141. mysql_select_db($dbStatsForm);
  142. foreach($s_q_list as $query){
  143. if($only_test){
  144. echo "mysql_query($dbStatsForm,$query)<br/>";
  145. }else{
  146. $res = mysql_query($query);
  147. }
  148. }
  149. }
  150. //get the user queries list (u_q_list)
  151. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','user');
  152. if(count($u_q_list)>0)
  153. {
  154. //now use the $u_q_list
  155. /**
  156. * We connect to the right DB first to make sure we can use the queries
  157. * without a database name
  158. */
  159. mysql_select_db($dbUserForm);
  160. foreach($u_q_list as $query){
  161. if($only_test){
  162. echo "mysql_query($dbUserForm,$query)<br/>";
  163. }else{
  164. $res = mysql_query($query);
  165. }
  166. }
  167. }
  168. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  169. die();
  170. //TODO only update this table
  171. /*
  172. $language_table = "`$dbNameForm`.`language`";
  173. fill_language_table($language_table);
  174. //set the settings from the form or the old config into config settings.
  175. //These settings are considered "safe" because they are entered by the admin
  176. $installation_settings['institution_form'] = $institutionForm;
  177. $installation_settings['institution_url_form'] = $institutionUrlForm;
  178. $installation_settings['campus_form'] = $campusForm;
  179. $installation_settings['email_form'] = $emailForm;
  180. $installation_settings['admin_last_name'] = $adminLastName;
  181. $installation_settings['admin_first_name'] = $adminFirstName;
  182. $installation_settings['language_form'] = $languageForm;
  183. $installation_settings['allow_self_registration'] = $allowSelfReg;
  184. $installation_settings['allow_teacher_self_registration'] = $allowSelfRegProf;
  185. $installation_settings['admin_phone_form'] = $adminPhoneForm;
  186. //put the settings into the settings table (taken from CSV file)
  187. $current_settings_table = "`$dbNameForm`.`settings_current`";
  188. fill_current_settings_table($current_settings_table, $installation_settings);
  189. //put the options into the options table (taken from CSV file)
  190. $settings_options_table = "`$dbNameForm`.`settings_options`";
  191. fill_settings_options_table($settings_options_table);
  192. //mysql_query("INSERT INTO `$dbNameForm`.`course_module` (`name`,`link`,`image`,`row`,`column`,`position`) VALUES
  193. // ('AddedLearnpath', NULL, 'scormbuilder.gif', 0, 0, 'external'),
  194. // ('".TOOL_BACKUP."', 'coursecopy/backup.php' , 'backup.gif', 2, 1, 'courseadmin'),
  195. // ('".TOOL_COPY_COURSE_CONTENT."', 'coursecopy/copy_course.php' , 'copy.gif', 2, 2, 'courseadmin'),
  196. // ('".TOOL_RECYCLE_COURSE."', 'coursecopy/recycle_course.php' , 'recycle.gif', 2, 3, 'courseadmin')");
  197. //...
  198. //mysql_query("UPDATE `$dbNameForm`.`course_module` SET name='".TOOL_LEARNPATH."' WHERE link LIKE 'scorm/%'");
  199. */
  200. }
  201. /*
  202. -----------------------------------------------------------
  203. Update the Dokeos course databases
  204. this part can be accessed in two ways:
  205. - from the normal upgrade process
  206. - from the script update_courses.php,
  207. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  208. Every time this script is accessed, only
  209. MAX_COURSE_TRANSFER courses are upgraded.
  210. -----------------------------------------------------------
  211. */
  212. //get the courses databases queries list (c_q_list)
  213. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
  214. if(count($c_q_list)>0)
  215. {
  216. //get the courses list
  217. mysql_select_db($dbNameForm);
  218. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  219. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  220. if(mysql_num_rows($res)>0)
  221. {
  222. while($row = mysql_fetch_array($res))
  223. {
  224. $list[] = $row;
  225. }
  226. foreach($list as $row)
  227. {
  228. //now use the $c_q_list
  229. /**
  230. * We connect to the right DB first to make sure we can use the queries
  231. * without a database name
  232. */
  233. mysql_select_db($row['db_name']);
  234. foreach($c_q_list as $query)
  235. {
  236. if($only_test)
  237. {
  238. echo "mysql_query(".$row['db_name'].",$query)<br/>";
  239. }else{
  240. $res = mysql_query($query);
  241. }
  242. }
  243. //update course manually
  244. //update group_category.forum_state
  245. //update group_info.tutor_id (put it in group_tutor table?)
  246. //update group_info.forum_state, forum_id
  247. //update forum tables (migrate from bb_ tables to forum_ tables)
  248. //migrate categories
  249. $sql_orig = "SELECT * FROM bb_categories";
  250. $res_orig = mysql_query($sql_orig);
  251. while($row = mysql_fetch_array($res_orig)){
  252. $sql = "INSERT INTO forum_category " .
  253. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  254. "('".$row['cat_id']."','".$row['cat_title']."','','".$row['cat_order']."',0)";
  255. $res = mysql_query($sql);
  256. }
  257. $sql_orig = "SELECT * FROM bb_forums ORDER BY forum_last_post_id desc";
  258. $res_orig = mysql_query($sql_orig);
  259. $order = 1;
  260. while($row = mysql_fetch_array($res_orig)){
  261. $sql = "INSERT INTO forum_forum " .
  262. "(forum_id,forum_category,allow_edit,forum_comment," .
  263. "forum_title," .
  264. "forum_last_post, forum_threads," .
  265. "locked, forum_posts, " .
  266. "allow_new_threads, forum_order) VALUES " .
  267. "('".$row['forum_id']."','".$row['cat_id']."',1,'".$row['forum_desc']."'," .
  268. "'".$row['forum_name']."'," .
  269. "'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
  270. "0,'".$row['forum_posts']."'," .
  271. "1,$order)";
  272. $res = mysql_query($sql);
  273. $order++;
  274. }
  275. $sql_orig = "SELECT * FROM bb_topics";
  276. $res_orig = mysql_query($sql_orig);
  277. while($row = mysql_fetch_array($res_orig)){
  278. //convert time from varchar to datetime
  279. $time = $row['topic_time'];
  280. $name = $row['prenom']." ".$row['nom'];
  281. $sql = "INSERT INTO forum_thread " .
  282. "(thread_id,forum_id,thread_poster_id," .
  283. "locked,thread_replies,thread_sticky,thread_title," .
  284. "thread_poster_name, thread_date, thread_last_post," .
  285. "thread_views) VALUES " .
  286. "('".$row['topic_id']."','".$row['forum_id']."','".$row['topic_poster']."'," .
  287. "0,'".$row['topic_replies']."',0,'".$row['topic_title']."'," .
  288. "'$name','$time','".$row['topic_last_post_id']."'," .
  289. "'".$row['topic_views']."')";
  290. $res = mysql_query($sql);
  291. }
  292. $sql_orig = "SELECT * FROM bb_posts, bb_posts_text WHERE bb_posts.post_id = bb_posts_text.post_id";
  293. $res_orig = mysql_query($sql_orig);
  294. while($row = mysql_fetch_array($res_orig)){
  295. //convert time from varchar to datetime
  296. $time = $row['post_time'];
  297. $name = $row['prenom']." ".$row['nom'];
  298. $sql = "INSERT INTO forum_post " .
  299. "(post_id,forum_id,thread_id," .
  300. "poster_id,post_parent_id,visible, " .
  301. "post_title,poster_name, post_text, " .
  302. "post_date, post_notification) VALUES " .
  303. "('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
  304. "'".$row['poster_id']."','".$row['parent_id']."',1," .
  305. "'".$row['post_title']."','$name', '".$row['post_text']."'," .
  306. "'$time',0)";
  307. $res = mysql_query($sql);
  308. }
  309. }
  310. }
  311. }
  312. $newPath = str_replace('\\', '/', realpath('../..')).'/';
  313. $coursePath = array ();
  314. $courseDB = array ();
  315. $nbr_courses = 0;
  316. if ($result = mysql_query("SELECT code,db_name,directory,course_language FROM `$dbNameForm`.`course` WHERE target_course_code IS NULL"))
  317. {
  318. $i = 0;
  319. $nbr_courses = mysql_num_rows($result);
  320. while ($i < MAX_COURSE_TRANSFER && (list ($course_code, $mysql_base_course, $directory, $languageCourse) = mysql_fetch_row($result)))
  321. {
  322. if (!file_exists($newPath.'courses/'.$directory))
  323. {
  324. if ($singleDbForm)
  325. {
  326. $prefix = $_configuration['table_prefix'].$mysql_base_course.$_configuration['db_glue'];
  327. $mysql_base_course = $dbNameForm.'`.`'.$_configuration['table_prefix'].$mysql_base_course;
  328. }
  329. else
  330. {
  331. $prefix = '';
  332. }
  333. $coursePath[$course_code] = $directory;
  334. $courseDB[$course_code] = $mysql_base_course;
  335. include ("../lang/english/create_course.inc.php");
  336. if ($languageCourse != 'english')
  337. {
  338. include ("../lang/$languageCourse/create_course.inc.php");
  339. }
  340. //TODO process the whole course database migration here. Call an external
  341. //script/function to keep this script tidy
  342. // Set item-properties of dropbox files
  343. /*
  344. $sql = "SELECT * FROM `$mysql_base_course".$_configuration['db_glue']."dropbox_file` f, `".$_configuration['db_glue']."_base_course".$_configuration['db_glue']."dropbox_post` p WHERE f.id = p.file_id";
  345. $res = mysql_query($sql);
  346. while ($obj = mysql_fetch_object($res))
  347. {
  348. $sql = "INSERT INTO `$mysql_base_course".$_configuration['db_glue']."item_property` SET ";
  349. $sql .= " tool = '".TOOL_DROPBOX."', ";
  350. $sql .= " insert_date = '".$obj->upload_date."', ";
  351. $sql .= " lastedit_date = '".$obj->last_upload_date."', ";
  352. $sql .= " ref = '".$obj->id."', ";
  353. $sql .= " lastedit_type = 'DropboxFileAdded', ";
  354. $sql .= " to_group_id = '0', ";
  355. $sql .= " to_user_id = '".$obj->dest_user_id."', ";
  356. $sql .= " insert_user_id = '".$obj->uploader_id."'";
  357. mysql_query($sql);
  358. }
  359. */
  360. $i ++;
  361. }
  362. else
  363. {
  364. $nbr_courses --;
  365. }
  366. }
  367. }
  368. }
  369. else
  370. {
  371. echo 'You are not allowed here !';
  372. }
  373. /**
  374. * This function stores the forum category in the database. The new category is added to the end.
  375. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  376. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  377. */
  378. function store_forumcategory($values)
  379. {
  380. global $table_categories;
  381. global $_course;
  382. global $_user;
  383. // find the max cat_order. The new forum category is added at the end => max cat_order + &
  384. $sql="SELECT MAX(cat_order) as sort_max FROM ".mysql_real_escape_string($table_categories);
  385. $result=Database::query($sql);
  386. $row=mysql_fetch_array($result);
  387. $new_max=$row['sort_max']+1;
  388. $sql="INSERT INTO ".$table_categories." (cat_title, cat_comment, cat_order) VALUES ('".mysql_real_escape_string($values['forum_category_title'])."','".mysql_real_escape_string($values['forum_category_comment'])."','".mysql_real_escape_string($new_max)."')";
  389. Database::query($sql);
  390. $last_id=mysql_insert_id();
  391. api_item_property_update($_course, TOOL_FORUM_CATEGORY, $last_id,"ForumCategoryAdded", $_user['user_id']);
  392. return array('id'=>$last_id,'title'=>$values['forum_category_title']) ;
  393. }
  394. /**
  395. * This function stores the forum in the database. The new forum is added to the end.
  396. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  397. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  398. */
  399. function store_forum($values)
  400. {
  401. global $table_forums;
  402. global $_course;
  403. global $_user;
  404. // find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
  405. $sql="SELECT MAX(forum_order) as sort_max FROM ".$table_forums." WHERE forum_category=".mysql_real_escape_string($values['forum_category']);
  406. $result=Database::query($sql);
  407. $row=mysql_fetch_array($result);
  408. $new_max=$row['sort_max']+1;
  409. $sql="INSERT INTO ".$table_forums."
  410. (forum_title, forum_comment, forum_category, allow_anonymous, allow_edit, approval_direct_post, allow_attachments, allow_new_threads, default_view, forum_of_group, forum_group_public_private, forum_order)
  411. VALUES ('".mysql_real_escape_string($values['forum_title'])."',
  412. '".mysql_real_escape_string($values['forum_comment'])."',
  413. '".mysql_real_escape_string($values['forum_category'])."',
  414. '".mysql_real_escape_string($values['allow_anonymous_group']['allow_anonymous'])."',
  415. '".mysql_real_escape_string($values['students_can_edit_group']['students_can_edit'])."',
  416. '".mysql_real_escape_string($values['approval_direct_group']['approval_direct'])."',
  417. '".mysql_real_escape_string($values['allow_attachments_group']['allow_attachments'])."',
  418. '".mysql_real_escape_string($values['allow_new_threads_group']['allow_new_threads'])."',
  419. '".mysql_real_escape_string($values['default_view_type_group']['default_view_type'])."',
  420. '".mysql_real_escape_string($values['group_forum'])."',
  421. '".mysql_real_escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."',
  422. '".mysql_real_escape_string($new_max)."')";
  423. Database::query($sql, __LINE__,__FILE__);
  424. $last_id=mysql_insert_id();
  425. api_item_property_update($_course, TOOL_FORUM, $last_id,"ForumCategoryAdded", $_user['user_id']);
  426. return array('id'=>$last_id, 'title'=>$values['forum_title']);
  427. }
  428. /**
  429. * This function stores a new thread. This is done through an entry in the forum_thread table AND
  430. * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet))
  431. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  432. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  433. */
  434. function store_thread($values)
  435. {
  436. global $table_threads;
  437. global $table_posts;
  438. global $_user;
  439. global $_course;
  440. global $current_forum;
  441. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table
  442. $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_views, thread_date, thread_sticky)
  443. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  444. '".mysql_real_escape_string($values['forum_id'])."',
  445. '".mysql_real_escape_string($values['user_id'])."',
  446. '".mysql_real_escape_string($values['poster_name'])."',
  447. '".mysql_real_escape_string($values['topic_views'])."',
  448. '".mysql_real_escape_string($values['post_date'])."',
  449. '".mysql_real_escape_string($values['thread_sticky'])."')";
  450. $result=Database::query($sql, __LINE__, __FILE__);
  451. $last_thread_id=mysql_insert_id();
  452. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"ForumThreadAdded", $_user['user_id']);
  453. // if the forum properties tell that the posts have to be approved we have to put the whole thread invisible
  454. // because otherwise the students will see the thread and not the post in the thread.
  455. // we also have to change $visible because the post itself has to be visible in this case (otherwise the teacher would have
  456. // to make the thread visible AND the post
  457. if ($values['visible']==0)
  458. {
  459. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"invisible", $_user['user_id']);
  460. $visible=1;
  461. }
  462. return $last_thread_id;
  463. }
  464. /**
  465. * This function migrates the threads of a given phpbb forum to a new forum of the new forum tool
  466. * @param $phpbb_forum_id the forum_id of the old (phpbb) forum
  467. * @param $new_forum_id the forum_id in the new forum
  468. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  469. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  470. */
  471. function migrate_threads_of_forum($phpbb_forum_id, $new_forum_id)
  472. {
  473. global $phpbb_threads;
  474. global $table_forums;
  475. $table_users = Database :: get_main_table(TABLE_MAIN_USER);
  476. $sql_phpbb_threads="SELECT forum.*, users.user_id
  477. FROM $phpbb_threads forum, $table_users users
  478. WHERE forum_id='".mysql_real_escape_string($phpbb_forum_id)."'
  479. AND forum.nom=users.lastname AND forum.prenom=users.firstname
  480. ";
  481. $result_phpbb_threads=Database::query($sql_phpbb_threads);
  482. $threads_counter=0;
  483. while ($row_phpbb_threads=mysql_fetch_array($result_phpbb_threads))
  484. {
  485. $values['post_title']=$row_phpbb_threads['topic_title'];
  486. $values['forum_id']=$new_forum_id;
  487. $values['user_id']=$row_phpbb_threads['user_id'];
  488. $values['poster_name']=0;
  489. $values['topic_views']=$row_phpbb_threads['topic_views'];
  490. $values['post_date']=$row_phpbb_threads['topic_time'];
  491. $values['thread_sticky']=0;
  492. $values['visible']=$row_phpbb_threads['visible'];
  493. //my_print_r($values);
  494. $new_forum_thread_id=store_thread($values);
  495. // now we migrate the posts of the given thread
  496. $posts_counter=$posts_counter+migrate_posts_of_thread($row_phpbb_threads['topic_id'], $new_forum_thread_id, $new_forum_id);
  497. $threads_counter++;
  498. }
  499. // Now we update the forum_forum table with the total number of posts for the given forum.
  500. $sql="UPDATE $table_forums
  501. SET forum_posts='".mysql_real_escape_string($posts_counter)."',
  502. forum_threads='".mysql_real_escape_string($threads_counter)."'
  503. WHERE forum_id='".mysql_real_escape_string($new_forum_id)."'";
  504. //echo $sql;
  505. $result=Database::query($sql);
  506. return array("threads"=>$threads_counter, "posts"=>$posts_counter);
  507. }
  508. /**
  509. * This function migrates the posts of a given phpbb thread (topic) to a thread in the new forum tool
  510. * @param $phpbb_forum_id the forum_id of the old (phpbb) forum
  511. * @param $new_forum_id the forum_id in the new forum
  512. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  513. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  514. */
  515. function migrate_posts_of_thread($phpbb_thread_id, $new_forum_thread_id, $new_forum_id)
  516. {
  517. global $phpbb_posts;
  518. global $phpbb_poststext;
  519. global $table_posts;
  520. global $table_threads;
  521. global $added_resources;
  522. $table_users = Database :: get_main_table(TABLE_MAIN_USER);
  523. $table_added_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
  524. $post_counter=0;
  525. $sql_phpbb_posts="SELECT posts.*, posts_text.*, users.user_id, users.lastname, users.firstname FROM $phpbb_posts posts, $phpbb_poststext posts_text, $table_users users
  526. WHERE posts.post_id=posts_text.post_id
  527. AND posts.nom=users.lastname
  528. AND posts.prenom=users.firstname
  529. AND posts.topic_id='".mysql_real_escape_string($phpbb_thread_id)."'
  530. ";
  531. $result_phpbb_posts=Database::query($sql_phpbb_posts);
  532. while($row_phpbb_posts=mysql_fetch_array($result_phpbb_posts))
  533. {
  534. $values=array();
  535. $values['post_title']=$row_phpbb_posts['post_title'];
  536. $values['post_text']=$row_phpbb_posts['post_text'];
  537. $values['thread_id']=$new_forum_thread_id;
  538. $values['forum_id']=$new_forum_id;
  539. $values['user_id']=$row_phpbb_posts['user_id'];
  540. $values['post_date']=$row_phpbb_posts['post_time'];
  541. $values['post_notification']=$row_phpbb_posts['topic_notify'];
  542. $values['post_parent_id']=0;
  543. $values['visible']=1;
  544. // We first store an entry in the forum_post table
  545. $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, post_date, post_notification, post_parent_id, visible)
  546. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  547. '".mysql_real_escape_string($values['post_text'])."',
  548. '".mysql_real_escape_string($values['thread_id'])."',
  549. '".mysql_real_escape_string($values['forum_id'])."',
  550. '".mysql_real_escape_string($values['user_id'])."',
  551. '".mysql_real_escape_string($values['post_date'])."',
  552. '".mysql_real_escape_string($values['post_notification'])."',
  553. '".mysql_real_escape_string($values['post_parent_id'])."',
  554. '".mysql_real_escape_string($values['visible'])."')";
  555. $result=Database::query($sql, __LINE__, __FILE__);
  556. $post_counter++;
  557. $last_post_id=mysql_insert_id();
  558. // We check if there added resources and if so we update them
  559. if (in_array($row_phpbb_posts['post_id'],$added_resources))
  560. {
  561. $sql_update_added_resource="UPDATE $table_added_resources
  562. SET source_type='forum_post', source_id='".mysql_real_escape_string($last_post_id)."'
  563. WHERE source_type='".mysql_real_escape_string(TOOL_BB_POST)."' AND source_id='".mysql_real_escape_string($row_phpbb_posts['post_id'])."'";
  564. echo $sql_update_added_resource;
  565. $result=Database::query($sql_update_added_resource, __LINE__, __FILE__);
  566. }
  567. }
  568. // update the thread_last_post of the post table AND the
  569. $sql="UPDATE $table_threads SET thread_last_post='".mysql_real_escape_string($last_post_id)."',
  570. thread_replies='".mysql_real_escape_string($post_counter-1)."'
  571. WHERE thread_id='".mysql_real_escape_string($new_forum_thread_id)."'";
  572. //echo $sql;
  573. $result=Database::query($sql, __LINE__, __FILE__);
  574. //echo $sql;
  575. return $post_counter;
  576. }
  577. /**
  578. * This function gets all the added resources for phpbb forum posts
  579. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  580. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  581. */
  582. function get_added_resources()
  583. {
  584. $table_added_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
  585. $return_array=array();
  586. // TODO: now we also migrate the added resources.
  587. $sql_added_resources="SELECT * FROM $table_added_resources WHERE source_type='".mysql_real_escape_string(TOOL_BB_POST)."'";
  588. $result=Database::query($sql_added_resources);
  589. while ($row=mysql_fetch_array($result))
  590. {
  591. $return_array[]=$row['source_id'];
  592. }
  593. return $return_array;
  594. }
  595. /**
  596. * This function gets the forum category information based on the name
  597. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  598. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  599. */
  600. function get_forumcategory_id_by_name($forum_category_name)
  601. {
  602. global $table_categories;
  603. $sql="SELECT cat_id FROM $table_categories WHERE cat_title='".mysql_real_escape_string($forum_category_name)."'";
  604. //echo $sql;
  605. $result=Database::query($sql,__LINE__,__FILE__);
  606. $row=mysql_fetch_array($result);
  607. //echo $row['cat_id'];
  608. return $row['cat_id'];
  609. }
  610. ?>