update_db.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 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. //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. //mysql_query("INSERT INTO `$dbNameForm`.`course_module` (`name`,`link`,`image`,`row`,`column`,`position`) VALUES
  185. // ('AddedLearnpath', NULL, 'scormbuilder.gif', 0, 0, 'external'),
  186. // ('".TOOL_BACKUP."', 'coursecopy/backup.php' , 'backup.gif', 2, 1, 'courseadmin'),
  187. // ('".TOOL_COPY_COURSE_CONTENT."', 'coursecopy/copy_course.php' , 'copy.gif', 2, 2, 'courseadmin'),
  188. // ('".TOOL_RECYCLE_COURSE."', 'coursecopy/recycle_course.php' , 'recycle.gif', 2, 3, 'courseadmin')");
  189. //...
  190. //mysql_query("UPDATE `$dbNameForm`.`course_module` SET name='".TOOL_LEARNPATH."' WHERE link LIKE 'scorm/%'");
  191. */
  192. }
  193. /*
  194. -----------------------------------------------------------
  195. Update the Dokeos course databases
  196. this part can be accessed in two ways:
  197. - from the normal upgrade process
  198. - from the script update_courses.php,
  199. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  200. Every time this script is accessed, only
  201. MAX_COURSE_TRANSFER courses are upgraded.
  202. -----------------------------------------------------------
  203. */
  204. //get the courses databases queries list (c_q_list)
  205. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
  206. if(count($c_q_list)>0)
  207. {
  208. //get the courses list
  209. mysql_select_db($dbNameForm);
  210. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  211. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  212. if(mysql_num_rows($res)>0)
  213. {
  214. while($row = mysql_fetch_array($res))
  215. {
  216. $list[] = $row;
  217. }
  218. foreach($list as $row)
  219. {
  220. //now use the $c_q_list
  221. /**
  222. * We connect to the right DB first to make sure we can use the queries
  223. * without a database name
  224. */
  225. mysql_select_db($row['db_name']);
  226. foreach($c_q_list as $query)
  227. {
  228. if($only_test)
  229. {
  230. echo "mysql_query(".$row['db_name'].",$query)<br/>";
  231. }else{
  232. $res = mysql_query($query);
  233. }
  234. }
  235. //update course manually
  236. //update group_category.forum_state
  237. //update group_info.tutor_id (put it in group_tutor table?)
  238. //update group_info.forum_state, forum_id
  239. //update forum tables (migrate from bb_ tables to forum_ tables)
  240. //migrate categories
  241. $sql_orig = "SELECT * FROM bb_categories";
  242. $res_orig = mysql_query($sql_orig);
  243. while($row = mysql_fetch_array($res_orig)){
  244. $sql = "INSERT INTO forum_category " .
  245. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  246. "('".$row['cat_id']."','".$row['cat_title']."','','".$row['cat_order']."',0)";
  247. $res = mysql_query($sql);
  248. }
  249. $sql_orig = "SELECT * FROM bb_forums ORDER BY forum_last_post_id desc";
  250. $res_orig = mysql_query($sql_orig);
  251. $order = 1;
  252. while($row = mysql_fetch_array($res_orig)){
  253. $sql = "INSERT INTO forum_forum " .
  254. "(forum_id,forum_category,allow_edit,forum_comment," .
  255. "forum_title," .
  256. "forum_last_post, forum_threads," .
  257. "locked, forum_posts, " .
  258. "allow_new_threads, forum_order) VALUES " .
  259. "('".$row['forum_id']."','".$row['cat_id']."',1,'".$row['forum_desc']."'," .
  260. "'".$row['forum_name']."'," .
  261. "'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
  262. "0,'".$row['forum_posts']."'," .
  263. "1,$order)";
  264. $res = mysql_query($sql);
  265. $order++;
  266. }
  267. $sql_orig = "SELECT * FROM bb_topics";
  268. $res_orig = mysql_query($sql_orig);
  269. while($row = mysql_fetch_array($res_orig)){
  270. //convert time from varchar to datetime
  271. $time = $row['topic_time'];
  272. $name = $row['prenom']." ".$row['nom'];
  273. $sql = "INSERT INTO forum_thread " .
  274. "(thread_id,forum_id,thread_poster_id," .
  275. "locked,thread_replies,thread_sticky,thread_title," .
  276. "thread_poster_name, thread_date, thread_last_post," .
  277. "thread_views) VALUES " .
  278. "('".$row['topic_id']."','".$row['forum_id']."','".$row['topic_poster']."'," .
  279. "0,'".$row['topic_replies']."',0,'".$row['topic_title']."'," .
  280. "'$name','$time','".$row['topic_last_post_id']."'," .
  281. "'".$row['topic_views']."')";
  282. $res = mysql_query($sql);
  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. $name = $row['prenom']." ".$row['nom'];
  290. $sql = "INSERT INTO forum_post " .
  291. "(post_id,forum_id,thread_id," .
  292. "poster_id,post_parent_id,visible, " .
  293. "post_title,poster_name, post_text, " .
  294. "post_date, post_notification) VALUES " .
  295. "('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
  296. "'".$row['poster_id']."','".$row['parent_id']."',1," .
  297. "'".$row['post_title']."','$name', '".$row['post_text']."'," .
  298. "'$time',0)";
  299. $res = mysql_query($sql);
  300. }
  301. }
  302. }
  303. }
  304. $newPath = str_replace('\\', '/', realpath('../..')).'/';
  305. $coursePath = array ();
  306. $courseDB = array ();
  307. $nbr_courses = 0;
  308. if ($result = mysql_query("SELECT code,db_name,directory,course_language FROM `$dbNameForm`.`course` WHERE target_course_code IS NULL"))
  309. {
  310. $i = 0;
  311. $nbr_courses = mysql_num_rows($result);
  312. while ($i < MAX_COURSE_TRANSFER && (list ($course_code, $mysql_base_course, $directory, $languageCourse) = mysql_fetch_row($result)))
  313. {
  314. if (!file_exists($newPath.'courses/'.$directory))
  315. {
  316. if ($singleDbForm)
  317. {
  318. $prefix = $_configuration['table_prefix'].$mysql_base_course.$_configuration['db_glue'];
  319. $mysql_base_course = $dbNameForm.'`.`'.$_configuration['table_prefix'].$mysql_base_course;
  320. }
  321. else
  322. {
  323. $prefix = '';
  324. }
  325. $coursePath[$course_code] = $directory;
  326. $courseDB[$course_code] = $mysql_base_course;
  327. include ("../lang/english/create_course.inc.php");
  328. if ($languageCourse != 'english')
  329. {
  330. include ("../lang/$languageCourse/create_course.inc.php");
  331. }
  332. //TODO process the whole course database migration here. Call an external
  333. //script/function to keep this script tidy
  334. // Set item-properties of dropbox files
  335. /*
  336. $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";
  337. $res = mysql_query($sql);
  338. while ($obj = mysql_fetch_object($res))
  339. {
  340. $sql = "INSERT INTO `$mysql_base_course".$_configuration['db_glue']."item_property` SET ";
  341. $sql .= " tool = '".TOOL_DROPBOX."', ";
  342. $sql .= " insert_date = '".$obj->upload_date."', ";
  343. $sql .= " lastedit_date = '".$obj->last_upload_date."', ";
  344. $sql .= " ref = '".$obj->id."', ";
  345. $sql .= " lastedit_type = 'DropboxFileAdded', ";
  346. $sql .= " to_group_id = '0', ";
  347. $sql .= " to_user_id = '".$obj->dest_user_id."', ";
  348. $sql .= " insert_user_id = '".$obj->uploader_id."'";
  349. mysql_query($sql);
  350. }
  351. */
  352. $i ++;
  353. }
  354. else
  355. {
  356. $nbr_courses --;
  357. }
  358. }
  359. }
  360. }
  361. else
  362. {
  363. echo 'You are not allowed here !';
  364. }
  365. /**
  366. * This function stores the forum category in the database. The new category is added to the end.
  367. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  368. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  369. */
  370. function store_forumcategory($values)
  371. {
  372. global $table_categories;
  373. global $_course;
  374. global $_user;
  375. // find the max cat_order. The new forum category is added at the end => max cat_order + &
  376. $sql="SELECT MAX(cat_order) as sort_max FROM ".mysql_real_escape_string($table_categories);
  377. $result=Database::query($sql);
  378. $row=mysql_fetch_array($result);
  379. $new_max=$row['sort_max']+1;
  380. $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)."')";
  381. Database::query($sql);
  382. $last_id=mysql_insert_id();
  383. api_item_property_update($_course, TOOL_FORUM_CATEGORY, $last_id,"ForumCategoryAdded", $_user['user_id']);
  384. return array('id'=>$last_id,'title'=>$values['forum_category_title']) ;
  385. }
  386. /**
  387. * This function stores the forum in the database. The new forum is added to the end.
  388. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  389. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  390. */
  391. function store_forum($values)
  392. {
  393. global $table_forums;
  394. global $_course;
  395. global $_user;
  396. // find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
  397. $sql="SELECT MAX(forum_order) as sort_max FROM ".$table_forums." WHERE forum_category=".mysql_real_escape_string($values['forum_category']);
  398. $result=Database::query($sql);
  399. $row=mysql_fetch_array($result);
  400. $new_max=$row['sort_max']+1;
  401. $sql="INSERT INTO ".$table_forums."
  402. (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)
  403. VALUES ('".mysql_real_escape_string($values['forum_title'])."',
  404. '".mysql_real_escape_string($values['forum_comment'])."',
  405. '".mysql_real_escape_string($values['forum_category'])."',
  406. '".mysql_real_escape_string($values['allow_anonymous_group']['allow_anonymous'])."',
  407. '".mysql_real_escape_string($values['students_can_edit_group']['students_can_edit'])."',
  408. '".mysql_real_escape_string($values['approval_direct_group']['approval_direct'])."',
  409. '".mysql_real_escape_string($values['allow_attachments_group']['allow_attachments'])."',
  410. '".mysql_real_escape_string($values['allow_new_threads_group']['allow_new_threads'])."',
  411. '".mysql_real_escape_string($values['default_view_type_group']['default_view_type'])."',
  412. '".mysql_real_escape_string($values['group_forum'])."',
  413. '".mysql_real_escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."',
  414. '".mysql_real_escape_string($new_max)."')";
  415. Database::query($sql, __LINE__,__FILE__);
  416. $last_id=mysql_insert_id();
  417. api_item_property_update($_course, TOOL_FORUM, $last_id,"ForumCategoryAdded", $_user['user_id']);
  418. return array('id'=>$last_id, 'title'=>$values['forum_title']);
  419. }
  420. /**
  421. * This function stores a new thread. This is done through an entry in the forum_thread table AND
  422. * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet))
  423. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  424. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  425. */
  426. function store_thread($values)
  427. {
  428. global $table_threads;
  429. global $table_posts;
  430. global $_user;
  431. global $_course;
  432. global $current_forum;
  433. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table
  434. $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_views, thread_date, thread_sticky)
  435. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  436. '".mysql_real_escape_string($values['forum_id'])."',
  437. '".mysql_real_escape_string($values['user_id'])."',
  438. '".mysql_real_escape_string($values['poster_name'])."',
  439. '".mysql_real_escape_string($values['topic_views'])."',
  440. '".mysql_real_escape_string($values['post_date'])."',
  441. '".mysql_real_escape_string($values['thread_sticky'])."')";
  442. $result=Database::query($sql, __LINE__, __FILE__);
  443. $last_thread_id=mysql_insert_id();
  444. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"ForumThreadAdded", $_user['user_id']);
  445. // if the forum properties tell that the posts have to be approved we have to put the whole thread invisible
  446. // because otherwise the students will see the thread and not the post in the thread.
  447. // we also have to change $visible because the post itself has to be visible in this case (otherwise the teacher would have
  448. // to make the thread visible AND the post
  449. if ($values['visible']==0)
  450. {
  451. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"invisible", $_user['user_id']);
  452. $visible=1;
  453. }
  454. return $last_thread_id;
  455. }
  456. /**
  457. * This function migrates the threads of a given phpbb forum to a new forum of the new forum tool
  458. * @param $phpbb_forum_id the forum_id of the old (phpbb) forum
  459. * @param $new_forum_id the forum_id in the new forum
  460. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  461. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  462. */
  463. function migrate_threads_of_forum($phpbb_forum_id, $new_forum_id)
  464. {
  465. global $phpbb_threads;
  466. global $table_forums;
  467. $table_users = Database :: get_main_table(TABLE_MAIN_USER);
  468. $sql_phpbb_threads="SELECT forum.*, users.user_id
  469. FROM $phpbb_threads forum, $table_users users
  470. WHERE forum_id='".mysql_real_escape_string($phpbb_forum_id)."'
  471. AND forum.nom=users.lastname AND forum.prenom=users.firstname
  472. ";
  473. $result_phpbb_threads=Database::query($sql_phpbb_threads);
  474. $threads_counter=0;
  475. while ($row_phpbb_threads=mysql_fetch_array($result_phpbb_threads))
  476. {
  477. $values['post_title']=$row_phpbb_threads['topic_title'];
  478. $values['forum_id']=$new_forum_id;
  479. $values['user_id']=$row_phpbb_threads['user_id'];
  480. $values['poster_name']=0;
  481. $values['topic_views']=$row_phpbb_threads['topic_views'];
  482. $values['post_date']=$row_phpbb_threads['topic_time'];
  483. $values['thread_sticky']=0;
  484. $values['visible']=$row_phpbb_threads['visible'];
  485. //my_print_r($values);
  486. $new_forum_thread_id=store_thread($values);
  487. // now we migrate the posts of the given thread
  488. $posts_counter=$posts_counter+migrate_posts_of_thread($row_phpbb_threads['topic_id'], $new_forum_thread_id, $new_forum_id);
  489. $threads_counter++;
  490. }
  491. // Now we update the forum_forum table with the total number of posts for the given forum.
  492. $sql="UPDATE $table_forums
  493. SET forum_posts='".mysql_real_escape_string($posts_counter)."',
  494. forum_threads='".mysql_real_escape_string($threads_counter)."'
  495. WHERE forum_id='".mysql_real_escape_string($new_forum_id)."'";
  496. //echo $sql;
  497. $result=Database::query($sql);
  498. return array("threads"=>$threads_counter, "posts"=>$posts_counter);
  499. }
  500. /**
  501. * This function migrates the posts of a given phpbb thread (topic) to a thread in the new forum tool
  502. * @param $phpbb_forum_id the forum_id of the old (phpbb) forum
  503. * @param $new_forum_id the forum_id in the new forum
  504. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  505. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  506. */
  507. function migrate_posts_of_thread($phpbb_thread_id, $new_forum_thread_id, $new_forum_id)
  508. {
  509. global $phpbb_posts;
  510. global $phpbb_poststext;
  511. global $table_posts;
  512. global $table_threads;
  513. global $added_resources;
  514. $table_users = Database :: get_main_table(TABLE_MAIN_USER);
  515. $table_added_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
  516. $post_counter=0;
  517. $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
  518. WHERE posts.post_id=posts_text.post_id
  519. AND posts.nom=users.lastname
  520. AND posts.prenom=users.firstname
  521. AND posts.topic_id='".mysql_real_escape_string($phpbb_thread_id)."'
  522. ";
  523. $result_phpbb_posts=Database::query($sql_phpbb_posts);
  524. while($row_phpbb_posts=mysql_fetch_array($result_phpbb_posts))
  525. {
  526. $values=array();
  527. $values['post_title']=$row_phpbb_posts['post_title'];
  528. $values['post_text']=$row_phpbb_posts['post_text'];
  529. $values['thread_id']=$new_forum_thread_id;
  530. $values['forum_id']=$new_forum_id;
  531. $values['user_id']=$row_phpbb_posts['user_id'];
  532. $values['post_date']=$row_phpbb_posts['post_time'];
  533. $values['post_notification']=$row_phpbb_posts['topic_notify'];
  534. $values['post_parent_id']=0;
  535. $values['visible']=1;
  536. // We first store an entry in the forum_post table
  537. $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, post_date, post_notification, post_parent_id, visible)
  538. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  539. '".mysql_real_escape_string($values['post_text'])."',
  540. '".mysql_real_escape_string($values['thread_id'])."',
  541. '".mysql_real_escape_string($values['forum_id'])."',
  542. '".mysql_real_escape_string($values['user_id'])."',
  543. '".mysql_real_escape_string($values['post_date'])."',
  544. '".mysql_real_escape_string($values['post_notification'])."',
  545. '".mysql_real_escape_string($values['post_parent_id'])."',
  546. '".mysql_real_escape_string($values['visible'])."')";
  547. $result=Database::query($sql, __LINE__, __FILE__);
  548. $post_counter++;
  549. $last_post_id=mysql_insert_id();
  550. // We check if there added resources and if so we update them
  551. if (in_array($row_phpbb_posts['post_id'],$added_resources))
  552. {
  553. $sql_update_added_resource="UPDATE $table_added_resources
  554. SET source_type='forum_post', source_id='".mysql_real_escape_string($last_post_id)."'
  555. WHERE source_type='".mysql_real_escape_string(TOOL_BB_POST)."' AND source_id='".mysql_real_escape_string($row_phpbb_posts['post_id'])."'";
  556. echo $sql_update_added_resource;
  557. $result=Database::query($sql_update_added_resource, __LINE__, __FILE__);
  558. }
  559. }
  560. // update the thread_last_post of the post table AND the
  561. $sql="UPDATE $table_threads SET thread_last_post='".mysql_real_escape_string($last_post_id)."',
  562. thread_replies='".mysql_real_escape_string($post_counter-1)."'
  563. WHERE thread_id='".mysql_real_escape_string($new_forum_thread_id)."'";
  564. //echo $sql;
  565. $result=Database::query($sql, __LINE__, __FILE__);
  566. //echo $sql;
  567. return $post_counter;
  568. }
  569. /**
  570. * This function gets all the added resources for phpbb forum posts
  571. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  572. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  573. */
  574. function get_added_resources()
  575. {
  576. $table_added_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
  577. $return_array=array();
  578. // TODO: now we also migrate the added resources.
  579. $sql_added_resources="SELECT * FROM $table_added_resources WHERE source_type='".mysql_real_escape_string(TOOL_BB_POST)."'";
  580. $result=Database::query($sql_added_resources);
  581. while ($row=mysql_fetch_array($result))
  582. {
  583. $return_array[]=$row['source_id'];
  584. }
  585. return $return_array;
  586. }
  587. /**
  588. * This function gets the forum category information based on the name
  589. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  590. * @todo is this the same function as in forumfunction.inc.php? If this is the case then it should not appear here.
  591. */
  592. function get_forumcategory_id_by_name($forum_category_name)
  593. {
  594. global $table_categories;
  595. $sql="SELECT cat_id FROM $table_categories WHERE cat_title='".mysql_real_escape_string($forum_category_name)."'";
  596. //echo $sql;
  597. $result=Database::query($sql,__LINE__,__FILE__);
  598. $row=mysql_fetch_array($result);
  599. //echo $row['cat_id'];
  600. return $row['cat_id'];
  601. }
  602. ?>