update_db.inc.php 27 KB

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