forumfunction.inc.php 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2006 Dokeos S.A.
  6. Copyright (c) 2006 Ghent University (UGent)
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. * These files are a complete rework of the forum. The database structure is
  20. * based on phpBB but all the code is rewritten. A lot of new functionalities
  21. * are added:
  22. * - forum categories and forums can be sorted up or down, locked or made invisible
  23. * - consistent and integrated forum administration
  24. * - forum options: are students allowed to edit their post?
  25. * moderation of posts (approval)
  26. * reply only forums (students cannot create new threads)
  27. * multiple forums per group
  28. * - sticky messages
  29. * - new view option: nested view
  30. * - quoting a message
  31. *
  32. * @Author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  33. * @Copyright Ghent University
  34. * @Copyright Patrick Cool
  35. *
  36. * @package dokeos.forum
  37. *
  38. * @todo several functions have to be moved to the itemmanager library
  39. * @todo displaying icons => display library
  40. * @todo complete the missing phpdoc the correct order should be
  41. *
  42. * some explanation of the function
  43. *
  44. * @param
  45. * @return
  46. *
  47. @todo
  48. *
  49. * @author firstname lastname <email>, organisation
  50. * @version (day) month year
  51. *
  52. * @deprecated
  53. */
  54. /**
  55. **************************************************************************
  56. * IMPORTANT NOTICE
  57. * Please do not change anything is this code yet because there are still
  58. * some significant code that need to happen and I do not have the time to
  59. * merge files and test it all over again. So for the moment, please do not
  60. * touch the code
  61. * -- Patrick Cool <patrick.cool@UGent.be>
  62. **************************************************************************
  63. */
  64. include(api_get_path(INCLUDE_PATH).'/lib/mail.lib.inc.php');
  65. include(api_get_path(INCLUDE_PATH).'/conf/mail.conf.php');
  66. /**
  67. * This function handles all the forum and forumcategories actions. This is a wrapper for the
  68. * forum and forum categories. All this code code could go into the section where this function is
  69. * called but this make the code there cleaner.
  70. *
  71. * @param
  72. * @return
  73. *
  74. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  75. * @version february 2006, dokeos 1.8
  76. */
  77. function handle_forum_and_forumcategories()
  78. {
  79. // Adding a forum category
  80. if (($_GET['action']=='add' AND $_GET['content']=='forumcategory') OR $_POST['SubmitForumCategory'] )
  81. {
  82. show_add_forumcategory_form();
  83. }
  84. // Adding a forum
  85. if ((($_GET['action']=='add' OR $_GET['action']=='edit') AND $_GET['content']=='forum') OR $_POST['SubmitForum'] )
  86. {
  87. if ($_GET['action']=='edit' and isset($_GET['id']) OR $_POST['SubmitForum'] )
  88. {
  89. $inputvalues=get_forums($_GET['id']); // note: this has to be cleaned first
  90. }
  91. show_add_forum_form($inputvalues);
  92. }
  93. // Edit a forum category
  94. if (($_GET['action']=='edit' AND $_GET['content']=='forumcategory' AND isset($_GET['id'])) OR $_POST['SubmitEditForumCategory'] )
  95. {
  96. $forum_category=get_forum_categories($_GET['id']); // note: this has to be cleaned first
  97. show_edit_forumcategory_form($forum_category);
  98. }
  99. // Delete a forum category
  100. if ($_GET['action']=='delete' AND isset($_GET['content']) AND isset($_GET['id']))
  101. {
  102. $return_message=delete_forum_forumcategory_thread($_GET['content'],$_GET['id']);// note: this has to be cleaned first
  103. Display :: display_normal_message($return_message);
  104. }
  105. // Change visibility of a forum or a forum category
  106. if (($_GET['action']=='invisible' OR $_GET['action']=='visible') AND isset($_GET['content']) AND isset($_GET['id']))
  107. {
  108. $return_message=change_visibility($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first
  109. Display :: display_normal_message($return_message);
  110. }
  111. // Change lock status of a forum or a forum category
  112. if (($_GET['action']=='lock' OR $_GET['action']=='unlock') AND isset($_GET['content']) AND isset($_GET['id']))
  113. {
  114. $return_message=change_lock_status($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first
  115. Display :: display_normal_message($return_message);
  116. }
  117. // Move a forum or a forum category
  118. if ($_GET['action']=='move' AND isset($_GET['content']) AND isset($_GET['id']) AND isset($_GET['direction']))
  119. {
  120. $return_message=move_up_down($_GET['content'], $_GET['direction'], $_GET['id']);// note: this has to be cleaned first
  121. Display :: display_normal_message($return_message);
  122. }
  123. }
  124. /**
  125. * This function displays the form that is used to add a forum category.
  126. *
  127. * @param
  128. * @return
  129. *
  130. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  131. * @version february 2006, dokeos 1.8
  132. */
  133. function show_add_forumcategory_form($inputvalues=array())
  134. {
  135. // initiate the object
  136. $form = new FormValidator('forumcategory');
  137. // settting the form elements
  138. $form->addElement('header', '', get_lang('AddForumCategory'));
  139. $form->addElement('text', 'forum_category_title', get_lang('Title'));
  140. $form->addElement('html_editor', 'forum_category_comment', get_lang('Comment'));
  141. $form->addElement('submit', 'SubmitForumCategory', 'OK');
  142. // setting the rules
  143. $form->addRule('forum_category_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  144. // The validation or display
  145. if( $form->validate() )
  146. {
  147. $values = $form->exportValues();
  148. store_forumcategory($values);
  149. }
  150. else
  151. {
  152. $form->display();
  153. }
  154. }
  155. /**
  156. * This function displays the form that is used to add a forum category.
  157. *
  158. * @param
  159. * @return
  160. *
  161. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  162. * @version february 2006, dokeos 1.8
  163. */
  164. function show_add_forum_form($inputvalues=array())
  165. {
  166. global $_course;
  167. // initiate the object
  168. $form = new FormValidator('forumcategory', 'post', 'index.php');
  169. // the header for the form
  170. $form->addElement('header', '', get_lang('AddForum'));
  171. // we have a hidden field if we are editing
  172. if (is_array($inputvalues))
  173. {
  174. $form->addElement('hidden', 'forum_id', $inputvalues['forum_id']);
  175. }
  176. // The title of the forum
  177. $form->addElement('text', 'forum_title', get_lang('Title'));
  178. // The comment of the forum
  179. $form->addElement('html_editor', 'forum_comment', get_lang('Comment'));
  180. // dropdown list: Forum Categories
  181. $forum_categories=get_forum_categories();
  182. foreach ($forum_categories as $key=>$value)
  183. {
  184. $forum_categories_titles[$value['cat_id']]=$value['cat_title'];
  185. }
  186. $form->addElement('select', 'forum_category', get_lang('InForumCategory'), $forum_categories_titles);
  187. if ($_course['visibility']==COURSE_VISIBILITY_OPEN_WORLD)
  188. {
  189. // This is for vertical
  190. //$form->addElement('radio', 'allow_anonymous', get_lang('AllowAnonymousPosts'), get_lang('Yes'), 1);
  191. //$form->addElement('radio', 'allow_anonymous', '', get_lang('No'), 0);
  192. // This is for horizontal
  193. $group='';
  194. $group[] =& HTML_QuickForm::createElement('radio', 'allow_anonymous',null,get_lang('Yes'),1);
  195. $group[] =& HTML_QuickForm::createElement('radio', 'allow_anonymous',null,get_lang('No'),0);
  196. $form->addGroup($group, 'allow_anonymous_group', get_lang('AllowAnonymousPosts'), '&nbsp;');
  197. }
  198. // This is for vertical
  199. //$form->addElement('radio', 'students_can_edit', get_lang('StudentsCanEdit'), get_lang('Yes'), 1);
  200. //$form->addElement('radio', 'students_can_edit', '', get_lang('No'), 0);
  201. // This is for horizontal
  202. $group='';
  203. $group[] =& HTML_QuickForm::createElement('radio', 'students_can_edit',null,get_lang('Yes'),1);
  204. $group[] =& HTML_QuickForm::createElement('radio', 'students_can_edit',null,get_lang('No'),0);
  205. $form->addGroup($group, 'students_can_edit_group', get_lang('StudentsCanEdit'), '&nbsp;');
  206. // This is for vertical
  207. //$form->addElement('radio', 'approval_direct', get_lang('ApprovalDirect'), get_lang('Approval'), 1);
  208. //$form->addElement('radio', 'approval_direct', '', get_lang('Direct'), 0);
  209. // This is for horizontal
  210. $group='';
  211. $group[] =& HTML_QuickForm::createElement('radio', 'approval_direct',null,get_lang('Approval'),1);
  212. $group[] =& HTML_QuickForm::createElement('radio', 'approval_direct',null,get_lang('Direct'),0);
  213. $form->addGroup($group, 'approval_direct_group', get_lang('ApprovalDirect'), '&nbsp;');
  214. // This is for vertical
  215. //$form->addElement('radio', 'allow_attachments', get_lang('AllowAttachments'), get_lang('Yes'), 1);
  216. //$form->addElement('radio', 'allow_attachments', '', get_lang('No'), 0);
  217. // This is for horizontal
  218. $group='';
  219. $group[] =& HTML_QuickForm::createElement('radio', 'allow_attachments',null,get_lang('Yes'),1);
  220. $group[] =& HTML_QuickForm::createElement('radio', 'allow_attachments',null,get_lang('No'),0);
  221. $form->addGroup($group, 'allow_attachments_group', get_lang('AllowAttachments'), '&nbsp;');
  222. // This is for vertical
  223. //$form->addElement('radio', 'allow_new_threads', get_lang('AllowNewThreads'), 1, get_lang('Yes'));
  224. //$form->addElement('radio', 'allow_new_threads', '', 0, get_lang('No'));
  225. // This is for horizontal
  226. $group='';
  227. $group[] =& HTML_QuickForm::createElement('radio', 'allow_new_threads',null, get_lang('Yes'),1);
  228. $group[] =& HTML_QuickForm::createElement('radio', 'allow_new_threads',null, get_lang('No'),0);
  229. $form->addGroup($group, 'allow_new_threads_group', get_lang('AllowNewThreads'), '&nbsp;');
  230. $group='';
  231. $group[] =& HTML_QuickForm::createElement('radio', 'default_view_type', null, get_lang('Flat'), 'flat');
  232. $group[] =& HTML_QuickForm::createElement('radio', 'default_view_type', null, get_lang('Threaded'), 'threaded');
  233. $group[] =& HTML_QuickForm::createElement('radio', 'default_view_type', null, get_lang('Nested'), 'nested');
  234. $form->addGroup($group, 'default_view_type_group', get_lang('DefaultViewType'), '&nbsp;');
  235. $form->addElement('static','Group', '<br /><strong>'.get_lang('GroupSettings').'</strong>');
  236. // dropdown list: Groups
  237. $groups=GroupManager::get_group_list();
  238. $groups_titles[0]=get_lang('NotAGroupForum');
  239. foreach ($groups as $key=>$value)
  240. {
  241. $groups_titles[$value['id']]=$value['name'];
  242. }
  243. $form->addElement('select', 'group_forum', get_lang('ForGroup'), $groups_titles);
  244. // Public or private group forum
  245. $group='';
  246. $group[] =& HTML_QuickForm::createElement('radio', 'public_private_group_forum', null, get_lang('Public'), 'public');
  247. $group[] =& HTML_QuickForm::createElement('radio', 'public_private_group_forum', null, get_lang('Private'), 'private');
  248. $form->addGroup($group, 'public_private_group_forum_group', get_lang('PublicPrivateGroupForum'), '&nbsp;');
  249. // The OK button
  250. $form->addElement('submit', 'SubmitForum', 'OK');
  251. // setting the rules
  252. $form->addRule('forum_title', get_lang('ThisFieldIsRequired'), 'required');
  253. $form->addRule('forum_category', get_lang('ThisFieldIsRequired'), 'required');
  254. // settings the defaults
  255. if (!is_array($inputvalues))
  256. {
  257. $defaults['allow_anonymous_group']['allow_anonymous']=0;
  258. $defaults['students_can_edit_group']['students_can_edit']=0;
  259. $defaults['approval_direct_group']['approval_direct']=0;
  260. $defaults['allow_attachments_group']['allow_attachments']=1;
  261. $defaults['allow_new_threads_group']['allow_new_threads']=1;
  262. $defaults['default_view_type_group']['default_view_type']='flat';
  263. $defaults['public_private_group_forum_group']['public_private_group_forum']='public';
  264. if (isset($_GET['forumcategory']))
  265. {
  266. $defaults['forum_category']=$_GET['forumcategory'];
  267. }
  268. }
  269. else // the default values when editing = the data in the table
  270. {
  271. $defaults['forum_id']=$inputvalues['forum_id'];
  272. $defaults['forum_title']=prepare4display($inputvalues['forum_title']);
  273. $defaults['forum_comment']=prepare4display($inputvalues['forum_comment']);
  274. $defaults['forum_category']=$inputvalues['forum_category'];
  275. $defaults['allow_anonymous_group']['allow_anonymous']=$inputvalues['allow_anonymous'];
  276. $defaults['students_can_edit_group']['students_can_edit']=$inputvalues['allow_edit'];
  277. $defaults['approval_direct_group']['approval_direct']=$inputvalues['approval_direct_post'];
  278. $defaults['allow_attachments_group']['allow_attachments']=$inputvalues['allow_attachments'];
  279. $defaults['allow_new_threads_group']['allow_new_threads']=$inputvalues['allow_new_threads'];
  280. $defaults['default_view_type_group']['default_view_type']=$inputvalues['default_view'];
  281. $defaults['public_private_group_forum_group']['public_private_group_forum']=$inputvalues['forum_group_public_private'];
  282. $defaults['group_forum']=$inputvalues['forum_of_group'];
  283. }
  284. $form->setDefaults($defaults);
  285. // The validation or display
  286. if( $form->validate() )
  287. {
  288. $values = $form->exportValues();
  289. store_forum($values);
  290. }
  291. else
  292. {
  293. $form->display();
  294. }
  295. }
  296. /**
  297. * This function displays the form that is used to edit a forum category.
  298. * This is more or less a copy from the show_add_forumcategory_form function with the only difference that is uses
  299. * some default values. I tried to have both in one function but this gave problems with the handle_forum_and_forumcategories function
  300. * (storing was done twice)
  301. *
  302. * @param
  303. * @return
  304. *
  305. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  306. * @version february 2006, dokeos 1.8
  307. */
  308. function show_edit_forumcategory_form($inputvalues=array())
  309. {
  310. // initiate the object
  311. $form = new FormValidator('forumcategory');
  312. // settting the form elements
  313. $form->addElement('header', '', get_lang('EditForumCategory'));
  314. $form->addElement('hidden', 'forum_category_id');
  315. $form->addElement('text', 'forum_category_title', get_lang('Title'));
  316. $form->addElement('html_editor', 'forum_category_comment', get_lang('Comment'));
  317. $form->addElement('submit', 'SubmitEditForumCategory', 'OK');
  318. // setting the default values
  319. $defaultvalues['forum_category_id']=$inputvalues['cat_id'];
  320. $defaultvalues['forum_category_title']=prepare4display($inputvalues['cat_title']);
  321. $defaultvalues['forum_category_comment']=prepare4display($inputvalues['cat_comment']);
  322. $form->setDefaults($defaultvalues);
  323. // setting the rules
  324. $form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required');
  325. // The validation or display
  326. if( $form->validate() )
  327. {
  328. $values = $form->exportValues();
  329. store_forumcategory($values);
  330. }
  331. else
  332. {
  333. $form->display();
  334. }
  335. }
  336. /**
  337. * This function stores the forum category in the database. The new category is added to the end.
  338. *
  339. * @param
  340. * @return
  341. *
  342. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  343. * @version february 2006, dokeos 1.8
  344. */
  345. function store_forumcategory($values)
  346. {
  347. global $table_categories;
  348. global $_course;
  349. global $_uid;
  350. // find the max cat_order. The new forum category is added at the end => max cat_order + &
  351. $sql="SELECT MAX(cat_order) as sort_max FROM ".mysql_real_escape_string($table_categories);
  352. $result=api_sql_query($sql);
  353. $row=mysql_fetch_array($result);
  354. $new_max=$row['sort_max']+1;
  355. if (isset($values['forum_category_id']))
  356. { // storing an edit
  357. $sql="UPDATE ".$table_categories." SET cat_title='".mysql_real_escape_string($values['forum_category_title'])."', cat_comment='".mysql_real_escape_string($values['forum_category_comment'])."' WHERE cat_id='".mysql_real_escape_string($values['forum_category_id'])."'";
  358. api_sql_query($sql);
  359. $last_id=mysql_insert_id();
  360. api_item_property_update($_course, TOOL_FORUM_CATEGORY, $values['forum_category_id'],"ForumCategoryAdded", $_uid);
  361. $return_message=get_lang('ForumCategoryEdited');
  362. }
  363. else
  364. {
  365. $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)."')";
  366. api_sql_query($sql);
  367. $last_id=mysql_insert_id();
  368. api_item_property_update($_course, TOOL_FORUM_CATEGORY, $last_id,"ForumCategoryAdded", $_uid);
  369. $return_message=get_lang('ForumCategoryAdded');
  370. }
  371. Display :: display_confirmation_message($return_message);
  372. }
  373. /**
  374. * This function stores the forum in the database. The new forum is added to the end.
  375. *
  376. * @param
  377. * @return
  378. *
  379. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  380. * @version february 2006, dokeos 1.8
  381. */
  382. function store_forum($values)
  383. {
  384. global $table_forums;
  385. global $_course;
  386. global $_uid;
  387. // find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
  388. $sql="SELECT MAX(forum_order) as sort_max FROM ".$table_forums." WHERE forum_category=".mysql_real_escape_string($values['forum_category']);
  389. $result=api_sql_query($sql);
  390. $row=mysql_fetch_array($result);
  391. $new_max=$row['sort_max']+1;
  392. if (isset($values['forum_id']))
  393. { // storing an edit
  394. $sql="UPDATE ".$table_forums." SET
  395. forum_title='".mysql_real_escape_string($values['forum_title'])."',
  396. forum_comment='".mysql_real_escape_string($values['forum_comment'])."',
  397. forum_category='".mysql_real_escape_string($values['forum_category'])."',
  398. allow_anonymous='".mysql_real_escape_string($values['allow_anonymous_group']['allow_anonymous'])."',
  399. allow_edit='".mysql_real_escape_string($values['students_can_edit_group']['students_can_edit'])."',
  400. approval_direct_post='".mysql_real_escape_string($values['approval_direct_group']['approval_direct'])."',
  401. allow_attachments='".mysql_real_escape_string($values['allow_attachments_group']['allow_attachments'])."',
  402. allow_new_threads='".mysql_real_escape_string($values['allow_new_threads_group']['allow_new_threads'])."',
  403. forum_group_public_private='".mysql_real_escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."',
  404. default_view='".mysql_real_escape_string($values['default_view_type_group']['default_view_type'])."',
  405. forum_of_group='".mysql_real_escape_string($values['group_forum'])."'
  406. WHERE forum_id='".mysql_real_escape_string($values['forum_id'])."'";
  407. mysql_query($sql) or die(mysql_error());
  408. $return_message=get_lang('ForumEdited');
  409. }
  410. else
  411. {
  412. $sql="INSERT INTO ".$table_forums."
  413. (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)
  414. VALUES ('".mysql_real_escape_string($values['forum_title'])."',
  415. '".mysql_real_escape_string($values['forum_comment'])."',
  416. '".mysql_real_escape_string($values['forum_category'])."',
  417. '".mysql_real_escape_string($values['allow_anonymous_group']['allow_anonymous'])."',
  418. '".mysql_real_escape_string($values['students_can_edit_group']['students_can_edit'])."',
  419. '".mysql_real_escape_string($values['approval_direct_group']['approval_direct'])."',
  420. '".mysql_real_escape_string($values['allow_attachments_group']['allow_attachments'])."',
  421. '".mysql_real_escape_string($values['allow_new_threads_group']['allow_new_threads'])."',
  422. '".mysql_real_escape_string($values['default_view_type_group']['default_view_type'])."',
  423. '".mysql_real_escape_string($values['group_forum'])."',
  424. '".mysql_real_escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."',
  425. '".mysql_real_escape_string($new_max)."')";
  426. api_sql_query($sql, __LINE__,__FILE__);
  427. $last_id=mysql_insert_id();
  428. api_item_property_update($_course, TOOL_FORUM, $last_id,"ForumCategoryAdded", $_uid);
  429. $return_message=get_lang('ForumAdded');
  430. }
  431. Display :: display_normal_message($return_message);
  432. }
  433. /**
  434. * This function deletes a forum or a forum category
  435. * This function currently does not delete the forums inside the category, nor the threads and replies inside these forums.
  436. * For the moment this is the easiest method and it has the advantage that it allows to recover fora that were acidently deleted
  437. * when the forum category got deleted.
  438. *
  439. * @param $content = what we are deleting (a forum or a forum category)
  440. * @param $id The id of the forum category that has to be deleted.
  441. *
  442. * @todo write the code for the cascading deletion of the forums inside a forum category and also the threads and replies inside these forums
  443. * @todo config setting for recovery or not (see also the documents tool: real delete or not).
  444. *
  445. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  446. * @version february 2006, dokeos 1.8
  447. */
  448. function delete_forum_forumcategory_thread($content, $id)
  449. {
  450. global $_course;
  451. if ($content=='forumcategory')
  452. {
  453. $tool_constant=TOOL_FORUM_CATEGORY;
  454. $return_message=get_lang('ForumCategoryDeleted');
  455. }
  456. if ($content=='forum')
  457. {
  458. $tool_constant=TOOL_FORUM;
  459. $return_message=get_lang('ForumDeleted');
  460. }
  461. if ($content=='thread')
  462. {
  463. $tool_constant=TOOL_FORUM_THREAD;
  464. $return_message=get_lang('ThreadDeleted');
  465. }
  466. api_item_property_update($_course,$tool_constant,$id,"delete"); // note: check if this returns a true and if so => return $return_message, if not => return false;
  467. return $return_message;
  468. }
  469. /**
  470. * This function deletes a forum post. This separate function is needed because forum posts do not appear in the item_property table (yet)
  471. * and because deleting a post also has consequence on the posts that have this post as parent_id (they are also deleted).
  472. * an alternative would be to store the posts also in item_property and mark this post as deleted (visibility = 2).
  473. * We also have to decrease the number of replies in the thread table
  474. *
  475. * @param $post_id the id of the post that will be deleted
  476. *
  477. * @todo write recursive function that deletes all the posts that have this message as parent
  478. *
  479. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  480. * @version february 2006, dokeos 1.8
  481. */
  482. function delete_post($post_id)
  483. {
  484. global $table_posts;
  485. global $table_threads;
  486. $sql="DELETE FROM $table_posts WHERE post_id='".mysql_real_escape_string($post_id)."'"; // note: this has to be a recursive function that deletes all of the posts in this block.
  487. api_sql_query($sql,__FILE__,__LINE__);
  488. $last_post_of_thread=check_if_last_post_of_thread($_GET['thread']); // note: clean the $_GET['thread']
  489. if (is_array($last_post_of_thread))
  490. {
  491. // Decreasing the number of replies for this thread and also changing the last post information
  492. $sql="UPDATE $table_threads SET thread_replies=thread_replies-1,
  493. thread_poster_id='".mysql_real_escape_string($last_post_of_thread['poster_id'])."',
  494. thread_last_post='".mysql_real_escape_string($last_post_of_thread['post_id'])."',
  495. thread_date='".mysql_real_escape_string($last_post_of_thread['post_date'])."'
  496. WHERE thread_id='".mysql_real_escape_string($_GET['thread'])."'"; // note: clean the $_GET['thread']
  497. api_sql_query($sql,__FILE__,__LINE__);
  498. return 'PostDeleted';
  499. }
  500. if ($last_post_of_thread==false)
  501. {
  502. // we deleted the very single post of the thread so we need to delete the entry in the thread table also.
  503. $sql="DELETE FROM $table_threads WHERE thread_id='".mysql_real_escape_string($_GET['thread'])."'"; // note: clean the $_GET['thread']
  504. api_sql_query($sql,__FILE__,__LINE__);
  505. return 'PostDeletedSpecial';
  506. }
  507. }
  508. /**
  509. * This function gets the all information of the last (=most recent) post of the thread
  510. * This can be done by sorting the posts that have the field thread_id=$thread_id and sort them by post_date
  511. *
  512. * @param $thread_id the id of the thread we want to know the last post of.
  513. * @return an array if there is a last post found, false if there is no post entry linked to that thread => thread will be deleted
  514. *
  515. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  516. * @version february 2006, dokeos 1.8
  517. */
  518. function check_if_last_post_of_thread($thread_id)
  519. {
  520. global $table_posts;
  521. $sql="SELECT * FROM $table_posts WHERE thread_id='".mysql_real_escape_string($thread_id)."' ORDER BY post_date DESC";
  522. $result=api_sql_query($sql,__FILE__,__LINE__);
  523. if (mysql_num_rows($result)>0)
  524. {
  525. $row=mysql_fetch_array($result);
  526. return $row;
  527. }
  528. else
  529. {
  530. return false;
  531. }
  532. }
  533. /**
  534. * This function takes care of the display of the visibility icon
  535. *
  536. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  537. * @param $id the id of the content we want to make invisible
  538. * @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible)
  539. * @return
  540. *
  541. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  542. * @version february 2006, dokeos 1.8
  543. */
  544. function display_visible_invisible_icon($content, $id, $current_visibility_status, $additional_url_parameters='')
  545. {
  546. if ($current_visibility_status=='1')
  547. {
  548. echo '<a href="'.$_SERVER['PHP_SELF'].'?';
  549. if (is_array($additional_url_parameters))
  550. {
  551. foreach ($additional_url_parameters as $key=>$value)
  552. {
  553. echo $key.'='.$value.'&amp;';
  554. }
  555. }
  556. echo 'action=invisible&amp;content='.$content.'&amp;id='.$id.'">'.icon('../img/visible.gif',get_lang('MakeInvisible')).'</a>';
  557. }
  558. if ($current_visibility_status=='0')
  559. {
  560. echo '<a href="'.$_SERVER['PHP_SELF'].'?';
  561. if (is_array($additional_url_parameters))
  562. {
  563. foreach ($additional_url_parameters as $key=>$value)
  564. {
  565. echo $key.'='.$value.'&amp;';
  566. }
  567. }
  568. echo 'action=visible&amp;content='.$content.'&amp;id='.$id.'">'.icon('../img/invisible.gif',get_lang('MakeVisible')).'</a>';
  569. }
  570. }
  571. /**
  572. * This function takes care of the display of the lock icon
  573. *
  574. * @param $content what is it that we want to (un)lock: forum category, forum, thread, post
  575. * @param $id the id of the content we want to (un)lock
  576. * @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible)
  577. * @return
  578. *
  579. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  580. * @version february 2006, dokeos 1.8
  581. */
  582. function display_lock_unlock_icon($content, $id, $current_lock_status, $additional_url_parameters='')
  583. {
  584. if ($current_lock_status=='1')
  585. {
  586. echo '<a href="'.$_SERVER['PHP_SELF'].'?';
  587. if (is_array($additional_url_parameters))
  588. {
  589. foreach ($additional_url_parameters as $key=>$value)
  590. {
  591. echo $key.'='.$value.'&amp;';
  592. }
  593. }
  594. echo 'action=unlock&amp;content='.$content.'&amp;id='.$id.'">'.icon('../img/lock.gif',get_lang('Lock')).'</a>';
  595. }
  596. if ($current_lock_status=='0')
  597. {
  598. echo '<a href="'.$_SERVER['PHP_SELF'].'?';
  599. if (is_array($additional_url_parameters))
  600. {
  601. foreach ($additional_url_parameters as $key=>$value)
  602. {
  603. echo $key.'='.$value.'&amp;';
  604. }
  605. }
  606. echo 'action=lock&amp;content='.$content.'&amp;id='.$id.'">'.icon('../img/unlock.gif',get_lang('Unlock')).'</a>';
  607. }
  608. }
  609. /**
  610. * This function takes care of the display of the up and down icon
  611. *
  612. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  613. * @param $id is the id of the item we want to display the icons for
  614. * @param $list is an array of all the items. All items in this list should have an up and down icon except for the first (no up icon) and the last (no down icon)
  615. * The key of this $list array is the id of the item.
  616. *
  617. * @return
  618. *
  619. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  620. * @version february 2006, dokeos 1.8
  621. */
  622. function display_up_down_icon($content, $id, $list)
  623. {
  624. $total_items=count($list);
  625. $internal_counter=0;
  626. foreach ($list as $key=>$listitem)
  627. {
  628. $internal_counter++;
  629. if ($id==$key)
  630. {
  631. $position=$internal_counter;
  632. }
  633. }
  634. if ($position>1)
  635. {
  636. $return_value='<a href="'.$_SERVER['PHP_SELF'].'?action=move&amp;direction=up&amp;content='.$content.'&amp;id='.$id.'"><img src="../img/up.gif" /></a>';
  637. }
  638. if ($position<$total_items)
  639. {
  640. $return_value.='<a href="'.$_SERVER['PHP_SELF'].'?action=move&amp;direction=down&amp;content='.$content.'&amp;id='.$id.'"><img src="../img/down.gif" /></a>';
  641. }
  642. echo $return_value;
  643. }
  644. /**
  645. * This function changes the visibility in the database (item_property)
  646. *
  647. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  648. * @param $id the id of the content we want to make invisible
  649. * @param $target_visibility what is the current status of the visibility (0 = invisible, 1 = visible)
  650. *
  651. * @todo change the get parameter so that it matches the tool constants.
  652. * @todo check if api_item_property_update returns true or false => returnmessage depends on it.
  653. * @todo move to itemmanager
  654. *
  655. * @return
  656. *
  657. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  658. * @version february 2006, dokeos 1.8
  659. */
  660. function change_visibility($content, $id, $target_visibility)
  661. {
  662. $constants=array('forumcategory'=>TOOL_FORUM_CATEGORY, 'forum'=>TOOL_FORUM, 'thread'=>TOOL_FORUM_THREAD);
  663. api_item_property_update($_course,$constants[$content],$id,$target_visibility); // note: check if this returns true or false => returnmessage depends on it.
  664. if ($target_visibility=='visible')
  665. {
  666. handle_mail_cue($content, $id);
  667. }
  668. return get_lang('VisibilityChanged');
  669. }
  670. /**
  671. * This function changes the lock status in the database
  672. *
  673. * @param $content what is it that we want to (un)lock: forum category, forum, thread, post
  674. * @param $id the id of the content we want to (un)lock
  675. * @param $action do we lock (=>locked value in db = 1) or unlock (=> locked value in db = 0)
  676. * @return string, language variable
  677. *
  678. * @todo move to itemmanager
  679. *
  680. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  681. * @version february 2006, dokeos 1.8
  682. */
  683. function change_lock_status($content, $id, $action)
  684. {
  685. global $table_categories;
  686. global $table_forums;
  687. global $table_threads;
  688. global $table_posts;
  689. // Determine the relevant table
  690. if ($content=='forumcategory')
  691. {
  692. $table=$table_categories;
  693. $id_field='cat_id';
  694. }
  695. elseif ($content=='forum')
  696. {
  697. $table=$table_forums;
  698. $id_field='forum_id';
  699. }
  700. elseif ($content=='thread')
  701. {
  702. $table=$table_threads;
  703. $id_field='thread_id';
  704. }
  705. else
  706. {
  707. return get_lang('Error');
  708. }
  709. // Determine what we are doing => defines the value for the database and the return message
  710. if ($action=='lock')
  711. {
  712. $db_locked=1;
  713. $return_message=get_lang('Locked');
  714. }
  715. elseif ($action=='unlock')
  716. {
  717. $db_locked=0;
  718. $return_message=get_lang('Unlocked');
  719. }
  720. else
  721. {
  722. return get_lang('Error');
  723. }
  724. // Doing the change in the database
  725. $sql="UPDATE $table SET locked='".mysql_real_escape_string($db_locked)."' WHERE $id_field='".mysql_real_escape_string($id)."'";
  726. if (api_sql_query($sql))
  727. {
  728. return $return_message;
  729. }
  730. else
  731. {
  732. return get_lang('Error');
  733. }
  734. }
  735. /**
  736. * This function moves a forum or a forum category up or down
  737. *
  738. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  739. * @param $direction do we want to move it up or down.
  740. * @param $id the id of the content we want to make invisible
  741. * @todo consider removing the table_item_property calls here but this can prevent unwanted side effects when a forum does not have an entry in
  742. * the item_property table but does have one in the forum table.
  743. * @return
  744. *
  745. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  746. * @version february 2006, dokeos 1.8
  747. */
  748. function move_up_down($content, $direction, $id)
  749. {
  750. global $table_categories;
  751. global $table_forums;
  752. global $table_item_property;
  753. // Determine which field holds the sort order
  754. if ($content=='forumcategory')
  755. {
  756. $table=$table_categories;
  757. $sort_column='cat_order';
  758. $id_column='cat_id';
  759. $sort_column='cat_order';
  760. }
  761. elseif ($content=='forum')
  762. {
  763. $table=$table_forums;
  764. $sort_column='forum_order';
  765. $id_column='forum_id';
  766. $sort_column='forum_order';
  767. // we also need the forum_category of this forum
  768. $sql="SELECT forum_category FROM $table_forums WHERE forum_id=".mysql_real_escape_string($id);
  769. $result=api_sql_query($sql);
  770. $row=mysql_fetch_array($result);
  771. $forum_category=$row['forum_category'];
  772. }
  773. else
  774. {
  775. return get_lang('Error');
  776. }
  777. // determine if need to sort ascending or descending
  778. if ($direction=='down')
  779. {
  780. $sort_direction='ASC';
  781. }
  782. elseif ($direction=='up')
  783. {
  784. $sort_direction='DESC';
  785. }
  786. else
  787. {
  788. return get_lang('Error');
  789. }
  790. // The SQL statement
  791. if ($content=='forumcategory')
  792. {
  793. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  794. WHERE forum_categories.cat_id=item_properties.ref
  795. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  796. ORDER BY forum_categories.cat_order $sort_direction";
  797. }
  798. if ($content=='forum')
  799. {
  800. $sql="SELECT * FROM".$table." WHERE forum_category='".mysql_real_escape_string($forum_category)."' ORDER BY forum_order $sort_direction";
  801. }
  802. // echo $sql.'<br />';
  803. // finding the items that need to be switched
  804. $result=api_sql_query($sql);
  805. $found=false;
  806. while ($row=mysql_fetch_array($result))
  807. {
  808. //echo $row[$id_column].'-';
  809. if ($found==true)
  810. {
  811. $next_id=$row[$id_column];
  812. $next_sort=$row[$sort_column];
  813. $found=false;
  814. }
  815. if($id==$row[$id_column])
  816. {
  817. $this_id=$id;
  818. $this_sort=$row[$sort_column];
  819. $found=true;
  820. }
  821. }
  822. // Committing the switch
  823. // we do an extra check if we do not have illegal values. If your remove this if statment you will
  824. // be able to mess with the sorting by refreshing the page over and over again.
  825. if ($this_sort<>'' AND $next_sort<>'' AND $next_id<>'' AND $this_id<>'')
  826. {
  827. $sql_update1="UPDATE $table SET $sort_column='".mysql_real_escape_string($this_sort)."' WHERE $id_column='".mysql_real_escape_string($next_id)."'";
  828. $sql_update2="UPDATE $table SET $sort_column='".mysql_real_escape_string($next_sort)."' WHERE $id_column='".mysql_real_escape_string($this_id)."'";
  829. api_sql_query($sql_update1);
  830. api_sql_query($sql_update2);
  831. }
  832. return get_lang(ucfirst($content).'Moved');
  833. }
  834. /**
  835. * This function returns a piece of html code that make the links grey (=invisible for the student)
  836. *
  837. * @param boolean 0/1: 0 = invisible, 1 = visible
  838. * @return string
  839. *
  840. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  841. * @version february 2006, dokeos 1.8
  842. */
  843. function class_visible_invisible($current_visibility_status)
  844. {
  845. if ($current_visibility_status=='0')
  846. {
  847. return "class='invisible'";
  848. }
  849. }
  850. /**
  851. * Retrieve all the information off the forum categories (or one specific) for the current course.
  852. * The categories are sorted according to their sorting order (cat_order
  853. *
  854. * @param $id default ''. When an id is passed we only find the information about that specific forum category. If no id is passed we get all the forum categories.
  855. * @return an array containing all the information about all the forum categories
  856. *
  857. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  858. * @version february 2006, dokeos 1.8
  859. */
  860. function get_forum_categories($id='')
  861. {
  862. global $table_categories;
  863. global $table_item_property;
  864. if ($id=='')
  865. {
  866. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  867. WHERE forum_categories.cat_id=item_properties.ref
  868. AND item_properties.visibility=1
  869. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  870. ORDER BY forum_categories.cat_order ASC";
  871. if (is_allowed_to_edit())
  872. {
  873. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  874. WHERE forum_categories.cat_id=item_properties.ref
  875. AND item_properties.visibility<>2
  876. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  877. ORDER BY forum_categories.cat_order ASC";
  878. }
  879. }
  880. else
  881. {
  882. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  883. WHERE forum_categories.cat_id=item_properties.ref
  884. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  885. AND forum_categories.cat_id='".mysql_real_escape_string($id)."'
  886. ORDER BY forum_categories.cat_order ASC";
  887. }
  888. $result=api_sql_query($sql);
  889. while ($row=mysql_fetch_array($result))
  890. {
  891. if ($id=='')
  892. {
  893. $forum_categories_list[$row['cat_id']]=$row;
  894. }
  895. else
  896. {
  897. $forum_categories_list=$row;
  898. }
  899. }
  900. return $forum_categories_list;
  901. }
  902. /**
  903. * This function retrieves all the fora in a given forum category
  904. *
  905. * @param integer $cat_id the id of the forum category
  906. * @return an array containing all the information about the forums (regardless of their category)
  907. *
  908. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  909. * @version february 2006, dokeos 1.8
  910. */
  911. function get_forums_in_category($cat_id)
  912. {
  913. global $table_forums;
  914. global $table_item_property;
  915. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  916. WHERE forum.forum_category='".mysql_real_escape_string($cat_id)."'
  917. AND forum.forum_id=item_properties.ref
  918. AND item_properties.visibility=1
  919. AND item_properties.tool='".TOOL_FORUM."'
  920. ORDER BY forum.forum_order ASC";
  921. if (is_allowed_to_edit())
  922. {
  923. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  924. WHERE forum.forum_category='".mysql_real_escape_string($cat_id)."'
  925. AND forum.forum_id=item_properties.ref
  926. AND item_properties.visibility<>2
  927. AND item_properties.tool='".TOOL_FORUM."'
  928. ORDER BY forum_order ASC";
  929. }
  930. $result=api_sql_query($sql);
  931. while ($row=mysql_fetch_array($result))
  932. {
  933. $forum_list[$row['forum_id']]=$row;
  934. }
  935. return $forum_list;
  936. }
  937. /**
  938. * Retrieve all the forums (regardless of their category) or of only one. The forums are sorted according to the forum_order.
  939. * Since it does not take the forum category into account there probably will be two or more forums that have forum_order=1, ...
  940. *
  941. * @return an array containing all the information about the forums (regardless of their category)
  942. * @todo check $sql4 because this one really looks fishy.
  943. *
  944. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  945. * @version february 2006, dokeos 1.8
  946. */
  947. function get_forums($id='')
  948. {
  949. global $table_forums;
  950. global $table_threads;
  951. global $table_posts;
  952. global $table_item_property;
  953. global $table_users;
  954. // **************** GETTING ALL THE FORUMS ************************* //
  955. if ($id=='')
  956. {
  957. //-------------- Student -----------------//
  958. // select all the forum information of all forums (that are visible to students)
  959. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  960. WHERE forum.forum_id=item_properties.ref
  961. AND item_properties.visibility=1
  962. AND item_properties.tool='".TOOL_FORUM."'
  963. ORDER BY forum.forum_order ASC";
  964. // select the number of threads of the forums (only the threads that are visible)
  965. $sql2="SELECT count(*) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties
  966. WHERE threads.thread_id=item_properties.ref
  967. AND item_properties.visibility=1
  968. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  969. GROUP BY threads.forum_id";
  970. // select the number of posts of the forum (post that are visible and that are in a thread that is visible)
  971. $sql3="SELECT count(*) AS number_of_posts, posts.forum_id FROM $table_posts posts, $table_threads threads, ".$table_item_property." item_properties
  972. WHERE posts.visible=1
  973. AND posts.thread_id=threads.thread_id
  974. AND threads.thread_id=item_properties.ref
  975. AND item_properties.visibility=1
  976. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  977. GROUP BY threads.forum_id";
  978. //-------------- Course Admin -----------------//
  979. if (is_allowed_to_edit())
  980. {
  981. // select all the forum information of all forums (that are not deleted)
  982. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  983. WHERE forum.forum_id=item_properties.ref
  984. AND item_properties.visibility<>2
  985. AND item_properties.tool='".TOOL_FORUM."'
  986. ORDER BY forum_order ASC";
  987. //echo $sql.'<hr>';
  988. // select the number of threads of the forums (only the threads that are not deleted)
  989. $sql2="SELECT count(*) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties
  990. WHERE threads.thread_id=item_properties.ref
  991. AND item_properties.visibility<>2
  992. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  993. GROUP BY threads.forum_id";
  994. //echo $sql2.'<hr>';
  995. // select the number of posts of the forum
  996. $sql3="SELECT count(*) AS number_of_posts, forum_id FROM $table_posts GROUP BY forum_id";
  997. //echo $sql3.'<hr>';
  998. }
  999. }
  1000. // **************** GETTING ONE SPECIFIC FORUM ************************* //
  1001. // We could do the splitup into student and course admin also but we want to have as much as information about a certain forum as possible
  1002. // so we do not take too much information into account. This function (or this section of the function) is namely used to fill the forms
  1003. // when editing a forum (and for the moment it is the only place where we use this part of the function)
  1004. else
  1005. {
  1006. // select all the forum information of the given forum (that is not deleted)
  1007. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  1008. WHERE forum.forum_id=item_properties.ref
  1009. AND forum_id='".mysql_real_escape_string($id)."'
  1010. AND item_properties.visibility<>2
  1011. AND item_properties.tool='".TOOL_FORUM."'
  1012. ORDER BY forum_order ASC";
  1013. // select the number of threads of the forum
  1014. $sql2="SELECT count(*) AS number_of_threads, forum_id FROM $table_threads WHERE forum_id=".mysql_real_escape_string($id)." GROUP BY forum_id";
  1015. // select the number of posts of the forum
  1016. $sql3="SELECT count(*) AS number_of_posts, forum_id FROM $table_posts WHERE forum_id=".mysql_real_escape_string($id)." GROUP BY forum_id";
  1017. // select the last post and the poster (note: this is probably no longer needed)
  1018. $sql4="SELECT post.post_id, post.forum_id, post.poster_id, post.poster_name, post.post_date, users.lastname, users.firstname
  1019. FROM $table_posts post, $table_users users
  1020. WHERE forum_id=".mysql_real_escape_string($id)."
  1021. AND post.poster_id=users.user_id
  1022. GROUP BY post.forum_id
  1023. ORDER BY post.post_id ASC";
  1024. }
  1025. // handling all the forum information
  1026. $result=api_sql_query($sql);
  1027. while ($row=mysql_fetch_array($result))
  1028. {
  1029. if ($id=='')
  1030. {
  1031. $forum_list[$row['forum_id']]=$row;
  1032. }
  1033. else
  1034. {
  1035. $forum_list=$row;
  1036. }
  1037. }
  1038. // handling the threadcount information
  1039. $result2=api_sql_query($sql2);
  1040. while ($row2=mysql_fetch_array($result2))
  1041. {
  1042. if ($id=='')
  1043. {
  1044. $forum_list[$row2['forum_id']]['number_of_threads']=$row2['number_of_threads'];
  1045. }
  1046. else
  1047. {
  1048. $forum_list['number_of_threads']=$row2['number_of_threads'];;
  1049. }
  1050. }
  1051. // handling the postcount information
  1052. $result3=api_sql_query($sql3);
  1053. while ($row3=mysql_fetch_array($result3))
  1054. {
  1055. if ($id=='')
  1056. {
  1057. if (array_key_exists($row3['forum_id'],$forum_list)) // this is needed because sql3 takes also the deleted forums into account
  1058. {
  1059. $forum_list[$row3['forum_id']]['number_of_posts']=$row3['number_of_posts'];
  1060. }
  1061. }
  1062. else
  1063. {
  1064. $forum_list['number_of_posts']=$row3['number_of_posts'];
  1065. }
  1066. }
  1067. // finding the last post information (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)
  1068. if ($id=='')
  1069. {
  1070. foreach ($forum_list as $key=>$value)
  1071. {
  1072. $last_post_info_of_forum=get_last_post_information($key,is_allowed_to_edit());
  1073. $forum_list[$key]['last_post_id']=$last_post_info_of_forum['last_post_id'];
  1074. $forum_list[$key]['last_poster_id']=$last_post_info_of_forum['last_poster_id'];
  1075. $forum_list[$key]['last_post_date']=$last_post_info_of_forum['last_post_date'];
  1076. $forum_list[$key]['last_poster_name']=$last_post_info_of_forum['last_poster_name'];
  1077. $forum_list[$key]['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname'];
  1078. $forum_list[$key]['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname'];
  1079. }
  1080. }
  1081. else
  1082. {
  1083. $last_post_info_of_forum=get_last_post_information($id,is_allowed_to_edit());
  1084. $forum_list['last_post_id']=$last_post_info_of_forum['last_post_id'];
  1085. $forum_list['last_poster_id']=$last_post_info_of_forum['last_poster_id'];
  1086. $forum_list['last_post_date']=$last_post_info_of_forum['last_post_date'];
  1087. $forum_list['last_poster_name']=$last_post_info_of_forum['last_poster_name'];
  1088. $forum_list['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname'];
  1089. $forum_list['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname'];
  1090. }
  1091. return $forum_list;
  1092. }
  1093. /**
  1094. * This functions gets all the last post information of a certain forum
  1095. *
  1096. * @param $forum_id the id of the forum we want to know the last post information of.
  1097. * @param $show_invisibles
  1098. * @return array containing all the information about the last post (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)
  1099. *
  1100. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1101. * @version february 2006, dokeos 1.8
  1102. */
  1103. function get_last_post_information($forum_id, $show_invisibles=false)
  1104. {
  1105. global $table_forums;
  1106. global $table_threads;
  1107. global $table_posts;
  1108. global $table_item_property;
  1109. global $table_users;
  1110. $sql="SELECT post.post_id, post.forum_id, post.poster_id, post.poster_name, post.post_date, users.lastname, users.firstname, post.visible, thread_properties.visibility AS thread_visibility, forum_properties.visibility AS forum_visibility
  1111. FROM $table_posts post, $table_users users, $table_item_property thread_properties, $table_item_property forum_properties
  1112. WHERE post.forum_id=".mysql_real_escape_string($forum_id)."
  1113. AND post.poster_id=users.user_id
  1114. AND post.thread_id=thread_properties.ref
  1115. AND thread_properties.tool='".TOOL_FORUM_THREAD."'
  1116. AND post.forum_id=forum_properties.ref
  1117. AND forum_properties.tool='".TOOL_FORUM."'
  1118. ORDER BY post.post_id DESC";
  1119. $result=api_sql_query($sql,__LINE__,__FILE__);
  1120. if ($show_invisibles==true)
  1121. {
  1122. $row=mysql_fetch_array($result);
  1123. $return_array['last_post_id']=$row['post_id'];
  1124. $return_array['last_poster_id']=$row['poster_id'];
  1125. $return_array['last_post_date']=$row['post_date'];
  1126. $return_array['last_poster_name']=$row['poster_name'];
  1127. $return_array['last_poster_lastname']=$row['lastname'];
  1128. $return_array['last_poster_firstname']=$row['firstname'];
  1129. return $return_array;
  1130. }
  1131. else
  1132. {
  1133. // we have to loop through the results to find the first one that is actually visible to students (forum_category, forum, thread AND post are visible)
  1134. while ($row=mysql_fetch_array($result))
  1135. {
  1136. if ($row['visible']=='1' AND $row['thread_visibility']=='1' AND $row['forum_visibility']=='1')
  1137. {
  1138. $return_array['last_post_id']=$row['post_id'];
  1139. $return_array['last_poster_id']=$row['poster_id'];
  1140. $return_array['last_post_date']=$row['post_date'];
  1141. $return_array['last_poster_name']=$row['poster_name'];
  1142. $return_array['last_poster_lastname']=$row['lastname'];
  1143. $return_array['last_poster_firstname']=$row['firstname'];
  1144. return $return_array;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. /**
  1150. * Retrieve all the threads of a given forum
  1151. *
  1152. * @param
  1153. * @return an array containing all the information about the threads
  1154. *
  1155. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1156. * @version february 2006, dokeos 1.8
  1157. */
  1158. function get_threads($forum_id)
  1159. {
  1160. global $table_item_property;
  1161. global $table_threads;
  1162. global $table_posts;
  1163. global $table_users;
  1164. // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement
  1165. // because we also have thread.* in it. This is because thread has a field locked and post also has the same field
  1166. // since we are merging these we would have the post.locked value but in fact we want the thread.locked value
  1167. // This is why it is added to the end of the field selection
  1168. $sql="SELECT thread.*, item_properties.*, post.*, users.firstname, users.lastname, users.user_id,
  1169. last_poster_users.firstname as last_poster_firstname , last_poster_users.lastname as last_poster_lastname, last_poster_users.user_id as last_poster_user_id, thread.locked as locked
  1170. FROM ".$table_threads." thread , ".$table_item_property." item_properties, $table_users users, $table_users last_poster_users, $table_posts post
  1171. WHERE thread.forum_id='".mysql_real_escape_string($forum_id)."'
  1172. AND thread.thread_last_post = post.post_id
  1173. AND post.poster_id= last_poster_users.user_id
  1174. AND thread.thread_poster_id=users.user_id
  1175. AND thread.thread_id=item_properties.ref
  1176. AND item_properties.visibility='1'
  1177. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  1178. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1179. if (is_allowed_to_edit())
  1180. {
  1181. // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement
  1182. // because we also have thread.* in it. This is because thread has a field locked and post also has the same field
  1183. // since we are merging these we would have the post.locked value but in fact we want the thread.locked value
  1184. // This is why it is added to the end of the field selection
  1185. $sql="SELECT thread.*, item_properties.*, post.*, users.firstname, users.lastname, users.user_id,
  1186. last_poster_users.firstname as last_poster_firstname , last_poster_users.lastname as last_poster_lastname, last_poster_users.user_id as last_poster_user_id, thread.locked as locked
  1187. FROM ".$table_threads." thread , ".$table_item_property." item_properties, $table_users users, $table_users last_poster_users, $table_posts post
  1188. WHERE thread.forum_id='".mysql_real_escape_string($forum_id)."'
  1189. AND thread.thread_last_post = post.post_id
  1190. AND post.poster_id= last_poster_users.user_id
  1191. AND thread.thread_poster_id=users.user_id
  1192. AND thread.thread_id=item_properties.ref
  1193. AND item_properties.visibility<>2
  1194. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  1195. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1196. }
  1197. $result=api_sql_query($sql);
  1198. while ($row=mysql_fetch_assoc($result))
  1199. {
  1200. $thread_list[]=$row;
  1201. }
  1202. return $thread_list;
  1203. }
  1204. /**
  1205. * Retrieve all posts of a given thread
  1206. *
  1207. * @return an array containing all the information about the posts of a given thread
  1208. *
  1209. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1210. * @version february 2006, dokeos 1.8
  1211. */
  1212. function get_posts($thread_id)
  1213. {
  1214. global $table_posts;
  1215. global $table_users;
  1216. // note: change these SQL so that only the relevant fields of the user table are used
  1217. if (api_is_allowed_to_edit())
  1218. {
  1219. $sql="SELECT * FROM $table_posts posts, $table_users users
  1220. WHERE posts.thread_id='".mysql_real_escape_string($thread_id)."'
  1221. AND posts.poster_id=users.user_id
  1222. ORDER BY posts.post_id ASC";
  1223. }
  1224. else
  1225. {
  1226. // students can only se the posts that are approved (posts.visible='1')
  1227. $sql="SELECT * FROM $table_posts posts, $table_users users
  1228. WHERE posts.thread_id='".mysql_real_escape_string($thread_id)."'
  1229. AND posts.visible='1'
  1230. AND posts.poster_id=users.user_id
  1231. ORDER BY posts.post_id ASC";
  1232. }
  1233. $result=api_sql_query($sql, __FILE__, __LINE__);
  1234. while ($row=mysql_fetch_array($result))
  1235. {
  1236. $post_list[]=$row;
  1237. }
  1238. return $post_list;
  1239. }
  1240. /**
  1241. * This function return the html syntax for the image
  1242. *
  1243. * @param $image_url The url of the image (absolute or relative)
  1244. * @param $alt The alt text (when the images cannot be displayed). http://www.w3.org/TR/html4/struct/objects.html#adef-alt
  1245. * @param $title The title of the image. Most browsers display this as 'tool tip'. http://www.w3.org/TR/html4/struct/global.html#adef-title
  1246. *
  1247. * @todo this is the same as the Display::xxx function, so it can be removed => all calls have to be changed also
  1248. *
  1249. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1250. * @version february 2006, dokeos 1.8
  1251. */
  1252. function icon($image_url,$alt='',$title='')
  1253. {
  1254. if ($title=='')
  1255. {
  1256. $title=$alt;
  1257. }
  1258. return '<img src="'.$image_url.'" alt="'.$alt.'" title="'.$title.'" />';
  1259. }
  1260. /**************************************************************************
  1261. NEW TOPIC FUNCTIONS
  1262. **************************************************************************/
  1263. /**
  1264. * This function retrieves all the information of a post
  1265. *
  1266. * @param $forum_id integer that indicates the forum
  1267. * @return array returns
  1268. *
  1269. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1270. * @version february 2006, dokeos 1.8
  1271. */
  1272. function get_post_information($post_id)
  1273. {
  1274. global $table_posts;
  1275. global $table_users;
  1276. $sql="SELECT * FROM ".$table_posts."posts, ".$table_users." users WHERE posts.poster_id=users.user_id AND posts.post_id='".mysql_real_escape_string($post_id)."'";
  1277. $result=api_sql_query($sql);
  1278. $row=mysql_fetch_array($result);
  1279. return $row;
  1280. }
  1281. /**
  1282. * This function retrieves all the information of a thread
  1283. *
  1284. * @param $forum_id integer that indicates the forum
  1285. * @return array returns
  1286. *
  1287. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1288. * @version february 2006, dokeos 1.8
  1289. */
  1290. function get_thread_information($thread_id)
  1291. {
  1292. global $table_threads;
  1293. global $table_item_property;
  1294. $sql="SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
  1295. WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
  1296. AND item_properties.ref='".mysql_real_escape_string($thread_id)."'
  1297. AND threads.thread_id='".mysql_real_escape_string($thread_id)."'";
  1298. $result=api_sql_query($sql);
  1299. $row=mysql_fetch_array($result);
  1300. return $row;
  1301. }
  1302. /**
  1303. * This function retrieves all the information of a given forum_id
  1304. *
  1305. * @param $forum_id integer that indicates the forum
  1306. * @return array returns
  1307. *
  1308. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1309. * @version february 2006, dokeos 1.8
  1310. *
  1311. * @deprecated this functionality is now moved to get_forums($forum_id)
  1312. */
  1313. function get_forum_information($forum_id)
  1314. {
  1315. global $table_forums;
  1316. global $table_item_property;
  1317. $sql="SELECT * FROM ".$table_forums." forums, ".$table_item_property." item_properties
  1318. WHERE item_properties.tool='".TOOL_FORUM."'
  1319. AND item_properties.ref='".mysql_real_escape_string($forum_id)."'
  1320. AND forums.forum_id='".mysql_real_escape_string($forum_id)."'";
  1321. $result=api_sql_query($sql);
  1322. $row=mysql_fetch_array($result);
  1323. return $row;
  1324. }
  1325. /**
  1326. * This function retrieves all the information of a given forumcategory id
  1327. *
  1328. * @param $forum_id integer that indicates the forum
  1329. * @return array returns
  1330. *
  1331. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1332. * @version february 2006, dokeos 1.8
  1333. */
  1334. function get_forumcategory_information($cat_id)
  1335. {
  1336. global $table_categories;
  1337. global $table_item_property;
  1338. $sql="SELECT * FROM ".$table_categories." forumcategories, ".$table_item_property." item_properties
  1339. WHERE item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1340. AND item_properties.ref='".mysql_real_escape_string($cat_id)."'
  1341. AND forumcategories.cat_id='".mysql_real_escape_string($cat_id)."'";
  1342. $result=api_sql_query($sql);
  1343. $row=mysql_fetch_array($result);
  1344. return $row;
  1345. }
  1346. /**
  1347. * This function counts the number of forums inside a given category
  1348. *
  1349. * @param $cat_id the id of the forum category
  1350. * @todo an additional parameter that takes the visibility into account. For instance $countinvisible=0 would return the number
  1351. * of visible forums, $countinvisible=1 would return the number of visible and invisible forums
  1352. * @return int the number of forums inside the given category
  1353. *
  1354. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1355. * @version february 2006, dokeos 1.8
  1356. */
  1357. function count_number_of_forums_in_category($cat_id)
  1358. {
  1359. global $table_forums;
  1360. $sql="SELECT count(*) AS number_of_forums FROM ".$table_forums." WHERE forum_category='".mysql_real_escape_string($cat_id)."'";
  1361. $result=api_sql_query($sql);
  1362. $row=mysql_fetch_array($result);
  1363. return $row['number_of_forums'];
  1364. }
  1365. /**
  1366. * This function stores a new thread. This is done through an entry in the forum_thread table AND
  1367. * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet))
  1368. *
  1369. * @param
  1370. * @return
  1371. *
  1372. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1373. * @version february 2006, dokeos 1.8
  1374. */
  1375. function store_thread($values)
  1376. {
  1377. global $table_threads;
  1378. global $table_posts;
  1379. global $_uid;
  1380. global $_course;
  1381. global $current_forum;
  1382. $post_date=date('Y-m-d H:i:s');
  1383. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit())
  1384. {
  1385. $visible=0; // the post is not approved yet.
  1386. }
  1387. else
  1388. {
  1389. $visible=1;
  1390. }
  1391. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table
  1392. $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_date, thread_sticky)
  1393. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  1394. '".mysql_real_escape_string($values['forum_id'])."',
  1395. '".mysql_real_escape_string($_uid)."',
  1396. '".mysql_real_escape_string($values['poster_name'])."',
  1397. '".mysql_real_escape_string($post_date)."',
  1398. '".mysql_real_escape_string($values['thread_sticky'])."')";
  1399. $result=api_sql_query($sql, __LINE__, __FILE__);
  1400. $last_thread_id=mysql_insert_id();
  1401. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"ForumThreadAdded", $_uid);
  1402. // if the forum properties tell that the posts have to be approved we have to put the whole thread invisible
  1403. // because otherwise the students will see the thread and not the post in the thread.
  1404. // we also have to change $visible because the post itself has to be visible in this case (otherwise the teacher would have
  1405. // to make the thread visible AND the post
  1406. if ($visible==0)
  1407. {
  1408. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"invisible", $_uid);
  1409. $visible=1;
  1410. }
  1411. // We now store the content in the table_post table
  1412. $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, poster_name, post_date, post_notification, post_parent_id, visible)
  1413. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  1414. '".mysql_real_escape_string($values['post_text'])."',
  1415. '".mysql_real_escape_string($last_thread_id)."',
  1416. '".mysql_real_escape_string($values['forum_id'])."',
  1417. '".mysql_real_escape_string($_uid)."',
  1418. '".mysql_real_escape_string($values['poster_name'])."',
  1419. '".mysql_real_escape_string($post_date)."',
  1420. '".mysql_real_escape_string($values['post_notification'])."','0',
  1421. '".mysql_real_escape_string($visible)."')";
  1422. $result=api_sql_query($sql, __LINE__, __FILE__);
  1423. $last_post_id=mysql_insert_id();
  1424. // Storing the attachments if any
  1425. // store_resources('forum_post',$last_post_id);
  1426. // now have to update the thread table to fill the thread_last_post field (so that we know when the thread has been updated for the last time)
  1427. $sql="UPDATE $table_threads SET thread_last_post='".mysql_real_escape_string($last_post_id)."' WHERE thread_id='".mysql_real_escape_string($last_thread_id)."'";
  1428. $result=api_sql_query($sql, __LINE__, __FILE__);
  1429. $message=get_lang('NewThreadStored').'<br />';
  1430. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit())
  1431. {
  1432. $message.=get_lang('MessageHasToBeApproved').'<br />';
  1433. $message.=get_lang('ReturnTo').'<a href="viewforum.php?forum='.$values['forum_id'].'">'.get_lang('Forum').'</a><br />';
  1434. }
  1435. else
  1436. {
  1437. $message.=get_lang('ReturnTo').'<a href="viewforum.php?forum='.$values['forum_id'].'">'.get_lang('Forum').'</a><br />';
  1438. $message.=get_lang('ReturnTo').'<a href="viewthread.php?forum='.$values['forum_id'].'&amp;thread='.$last_thread_id.'">'.get_lang('Message').'</a>';
  1439. }
  1440. session_unregister('formelements');
  1441. session_unregister('origin');
  1442. session_unregister('breadcrumbs');
  1443. session_unregister('addedresource');
  1444. session_unregister('addedresourceid');
  1445. Display :: display_normal_message($message);
  1446. }
  1447. /**
  1448. * This function displays the form that is used to add a post. This can be a new thread or a reply.
  1449. * @param $action is the parameter that determines if we are
  1450. * 1. newthread: adding a new thread (both empty) => No I-frame
  1451. * 2. replythread: Replying to a thread ($action = replythread) => I-frame with the complete thread (if enabled)
  1452. * 3. replymessage: Replying to a message ($action =replymessage) => I-frame with the complete thread (if enabled) (I first thought to put and I-frame with the message only)
  1453. * 4. quote: Quoting a message ($action= quotemessage) => I-frame with the complete thread (if enabled). The message will be in the reply. (I first thought not to put an I-frame here)
  1454. *
  1455. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1456. * @version february 2006, dokeos 1.8
  1457. */
  1458. function show_add_post_form($action='', $id='', $form_values='')
  1459. {
  1460. global $forum_setting;
  1461. global $current_forum;
  1462. global $_user;
  1463. global $_uid;
  1464. // initiate the object
  1465. $form = new FormValidator('thread', 'post', $_SERVER['PHP_SELF'].'?forum='.$_GET['forum'].'&thread='.$_GET['thread'].'&post='.$_GET['post'].'&action='.$_GET['action']);
  1466. $form->setConstants(array('forum' => '5'));
  1467. // settting the form elements
  1468. $form->addElement('hidden', 'forum_id', $_GET['forum']);
  1469. $form->addElement('hidden', 'thread_id', $_GET['thread']);
  1470. // if anonymous posts are allowed we also display a form to allow the user to put his name or username in
  1471. if ($current_forum['allow_anonymous']==1 AND !isset($_uid))
  1472. {
  1473. $form->addElement('text', 'poster_name', get_lang('Name'));
  1474. }
  1475. $form->addElement('text', 'post_title', get_lang('Title'));
  1476. $form->addElement('html_editor', 'post_text', get_lang('Text'));
  1477. if ($forum_setting['allow_post_notificiation'] AND isset($_uid))
  1478. {
  1479. $form->addElement('checkbox', 'post_notification', '', get_lang('NotifyByEmail').' ('.$_user['mail'].')');
  1480. }
  1481. if ($forum_setting['allow_sticky'] AND api_is_allowed_to_edit() AND $action=='newthread')
  1482. {
  1483. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  1484. }
  1485. if ($current_forum['allow_attachments']=='1' OR api_is_allowed_to_edit())
  1486. {
  1487. //$form->add_resource_button();
  1488. $values = $form->exportValues();
  1489. }
  1490. $form->addElement('submit', 'SubmitPost', get_lang('Ok'));
  1491. if (!empty($form_values))
  1492. {
  1493. $defaults['post_title']=prepare4display($form_values['post_title']);
  1494. $defaults['post_text']=prepare4display($form_values['post_text']);
  1495. $defaults['post_notification']=$form_values['post_notification'];
  1496. $defaults['thread_sticky']=$form_values['thread_sticky'];
  1497. }
  1498. // if we are quoting a message we have to retrieve the information of the post we are quoting so that
  1499. // we can add this as default to the textarea
  1500. if (($action=='quote' OR $action=='replymessage') and isset($_GET['post']))
  1501. {
  1502. // we also need to put the parent_id of the post in a hidden form when we are quoting or replying to a message (<> reply to a thread !!!)
  1503. $form->addElement('hidden', 'post_parent_id', $_GET['post']); // note this has to be cleaned first
  1504. // if we are replying or are quoting then we display a default title.
  1505. $values=get_post_information($_GET['post']); // note: this has to be cleaned first
  1506. $defaults['post_title']=get_lang('ReplyShort').$values['post_title'];
  1507. // When we are quoting a message then we have to put that message into the wysiwyg editor.
  1508. // note: the style has to be hardcoded here because using class="quote" didn't work
  1509. if($action=='quote')
  1510. {
  1511. $defaults['post_text']='<div>&nbsp;</div><div style="margin: 5px;"><div style="font-size: 90%; font-style: italic;">'.get_lang('Quoting').' '.$values['firstname'].' '.$values['lastname'].':</div><div style="color: #006600; font-size: 90%; font-style: italic; background-color: #FAFAFA; border: #D1D7DC 1px solid; padding: 3px;">'.prepare4display($values['post_text']).'</div></div><div>&nbsp;</div><div>&nbsp;</div>';
  1512. }
  1513. }
  1514. $form->setDefaults($defaults);
  1515. // the course admin can make a thread sticky (=appears with special icon and always on top)
  1516. $form->addRule('post_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1517. if ($current_forum['allow_anonymous']==1 AND !isset($_uid))
  1518. {
  1519. $form->addRule('poster_name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1520. }
  1521. // The validation or display
  1522. if( $form->validate() )
  1523. {
  1524. $values = $form->exportValues();
  1525. return $values;
  1526. }
  1527. else
  1528. {
  1529. $form->display();
  1530. if ($forum_setting['show_thread_iframe_on_reply'] and $action<>'newthread')
  1531. {
  1532. echo "<iframe src=\"iframe_thread.php?forum=".$_GET['forum']."&amp;thread=".$_GET['thread']."#".$_GET['post']."\" width=\"80%\"></iframe>";
  1533. }
  1534. }
  1535. }
  1536. /**
  1537. * This function stores a reply in the forum_post table.
  1538. * It also updates the forum_threads table (thread_replies +1 , thread_last_post, thread_date)
  1539. *
  1540. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1541. * @version february 2006, dokeos 1.8
  1542. */
  1543. function store_reply($values)
  1544. {
  1545. global $table_threads;
  1546. global $table_posts;
  1547. global $_uid;
  1548. global $_course;
  1549. global $current_forum;
  1550. $post_date=date('Y-m-d H:i:s');
  1551. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit())
  1552. {
  1553. $visible=0; // the post is not approved yet.
  1554. }
  1555. else
  1556. {
  1557. $visible=1;
  1558. }
  1559. // We first store an entry in the forum_post table
  1560. $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, post_date, post_notification, post_parent_id, visible)
  1561. VALUES ('".mysql_real_escape_string($values['post_title'])."',
  1562. '".mysql_real_escape_string($values['post_text'])."',
  1563. '".mysql_real_escape_string($values['thread_id'])."',
  1564. '".mysql_real_escape_string($values['forum_id'])."',
  1565. '".mysql_real_escape_string($_uid)."',
  1566. '".mysql_real_escape_string($post_date)."',
  1567. '".mysql_real_escape_string($values['post_notification'])."',
  1568. '".mysql_real_escape_string($values['post_parent_id'])."',
  1569. '".mysql_real_escape_string($visible)."')";
  1570. $result=api_sql_query($sql, __LINE__, __FILE__);
  1571. $new_post_id=mysql_insert_id();
  1572. $values['new_post_id']=$new_post_id;
  1573. // Storing the attachments if any
  1574. // store_resources('forum_post',$new_post_id);
  1575. // update the thread
  1576. update_thread($values['thread_id'], $new_post_id,$post_date);
  1577. // update the forum
  1578. api_item_property_update($_course, TOOL_FORUM, $values['forum_id'],"NewMessageInForum", $_uid);
  1579. $message=get_lang('ReplyAdded').'<br />';
  1580. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit())
  1581. {
  1582. $message.=get_lang('MessageHasToBeApproved').'<br />';
  1583. }
  1584. $message.=get_lang('ReturnTo').'<a href="viewforum.php?forum='.$values['forum_id'].'">'.get_lang('Forum').'</a><br />';
  1585. $message.=get_lang('ReturnTo').'<a href="viewthread.php?forum='.$values['forum_id'].'&amp;thread='.$values['thread_id'].'">'.get_lang('Message').'</a>';
  1586. send_notification_mails($values['thread_id'], $values);
  1587. session_unregister('formelements');
  1588. session_unregister('origin');
  1589. session_unregister('breadcrumbs');
  1590. session_unregister('addedresource');
  1591. session_unregister('addedresourceid');
  1592. Display :: display_normal_message($message);
  1593. }
  1594. /**
  1595. * This function displays the form that is used to edit a post. This can be a new thread or a reply.
  1596. * @param $current_post array that contains all the information about the current post
  1597. * @param $current_thread array that contains all the information about the current thread
  1598. * @return
  1599. *
  1600. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1601. * @version february 2006, dokeos 1.8
  1602. */
  1603. function show_edit_post_form($current_post, $current_thread, $form_values='')
  1604. {
  1605. global $forum_setting;
  1606. global $_user;
  1607. // initiate the object
  1608. $form = new FormValidator('edit_post', 'post', $_SERVER['PHP_SELF'].'?forum='.$_GET['forum'].'&thread='.$_GET['thread'].'&post='.$_GET['post']);
  1609. // settting the form elements
  1610. $form->addElement('hidden', 'post_id', $current_post['post_id']);
  1611. $form->addElement('hidden', 'thread_id', $current_thread['thread_id']);
  1612. if ($current_post['post_parent_id']==0)
  1613. {
  1614. $form->addElement('hidden', 'is_first_post_of_thread', '1');
  1615. }
  1616. $form->addElement('text', 'post_title', get_lang('Title'));
  1617. $form->addElement('html_editor', 'post_text', get_lang('Text'));
  1618. if ($forum_setting['allow_post_notificiation'])
  1619. {
  1620. $form->addElement('checkbox', 'post_notification', '', get_lang('NotifyByEmail').' ('.$current_post['email'].')');
  1621. }
  1622. if ($forum_setting['allow_sticky'] and api_is_allowed_to_edit() and $current_post['post_parent_id']==0) // the sticky checkbox only appears when it is the first post of a thread
  1623. {
  1624. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  1625. if ($current_thread['thread_sticky']==1)
  1626. {
  1627. $defaults['thread_sticky']=true;
  1628. }
  1629. }
  1630. if ($current_forum['allow_attachments']=='1' OR api_is_allowed_to_edit())
  1631. {
  1632. if (empty($form_values) AND !$_POST['SubmitPost'])
  1633. {
  1634. //edit_added_resources('forum_post',$current_post['post_id']);
  1635. }
  1636. //$form->add_resource_button();
  1637. $values = $form->exportValues();
  1638. }
  1639. $form->addElement('submit', 'SubmitPost', get_lang('Ok'));
  1640. // setting the default values for the form elements
  1641. $defaults['post_title']=prepare4display($current_post['post_title']);
  1642. $defaults['post_text']=prepare4display($current_post['post_text']);
  1643. if ($current_post['post_notification']==1)
  1644. {
  1645. $defaults['post_notification']=true;
  1646. }
  1647. if (!empty($form_values))
  1648. {
  1649. $defaults['post_title']=$form_values['post_title'];
  1650. $defaults['post_text']=$form_values['post_text'];
  1651. $defaults['post_notification']=$form_values['post_notification'];
  1652. $defaults['thread_sticky']=$form_values['thread_sticky'];
  1653. }
  1654. $form->setDefaults($defaults);
  1655. // the course admin can make a thread sticky (=appears with special icon and always on top)
  1656. $form->addRule('post_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1657. // The validation or display
  1658. if( $form->validate() )
  1659. {
  1660. $values = $form->exportValues();
  1661. return $values;
  1662. }
  1663. else
  1664. {
  1665. $form->display();
  1666. }
  1667. }
  1668. /**
  1669. * This function stores the edit of a post in the forum_post table.
  1670. *
  1671. * @param
  1672. * @return
  1673. *
  1674. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1675. * @version february 2006, dokeos 1.8
  1676. */
  1677. function store_edit_post($values)
  1678. {
  1679. global $table_threads;
  1680. global $table_posts;
  1681. // first we check if the change affects the thread and if so we commit the changes (sticky and post_title=thread_title are relevant)
  1682. if (array_key_exists('is_first_post_of_thread',$values) AND $values['is_first_post_of_thread']=='1')
  1683. {
  1684. $sql="UPDATE $table_threads SET thread_title='".mysql_real_escape_string($values['post_title'])."',
  1685. thread_sticky='".mysql_real_escape_string($values['thread_sticky'])."'
  1686. WHERE thread_id='".mysql_real_escape_string($values['thread_id'])."'";
  1687. api_sql_query($sql,__FILE__, __LINE__);
  1688. }
  1689. // update the post_title and the post_text
  1690. $sql="UPDATE $table_posts SET post_title='".mysql_real_escape_string($values['post_title'])."',
  1691. post_text='".mysql_real_escape_string($values['post_text'])."',
  1692. post_notification='".mysql_real_escape_string($values['post_notification'])."'
  1693. WHERE post_id='".mysql_real_escape_string($values['post_id'])."'";
  1694. api_sql_query($sql,__FILE__, __LINE__);
  1695. // Storing the attachments if any
  1696. //update_added_resources('forum_post',$values['post_id']);
  1697. $message=get_lang('EditPostStored').'<br />';
  1698. $message.=get_lang('ReturnTo').'<a href="viewforum.php?forum='.$_GET['forum'].'">'.get_lang('Forum').'</a><br />';
  1699. $message.=get_lang('ReturnTo').'<a href="viewthread.php?forum='.$_GET['forum'].'&amp;thread='.$values['thread_id'].'&amp;post='.$_GET['post'].'">'.get_lang('Message').'</a>';
  1700. session_unregister('formelements');
  1701. session_unregister('origin');
  1702. session_unregister('breadcrumbs');
  1703. session_unregister('addedresource');
  1704. session_unregister('addedresourceid');
  1705. Display :: display_normal_message($message);
  1706. }
  1707. /**
  1708. * This function displays the firstname and lastname of the user as a link to the user tool.
  1709. *
  1710. * @param
  1711. * @return
  1712. *
  1713. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1714. * @version february 2006, dokeos 1.8
  1715. */
  1716. function display_user_link($user_id, $name)
  1717. {
  1718. if ($user_id<>0)
  1719. {
  1720. return "<a href=\"../user/userInfo.php?uInfo=".$user_id."\">".$name."</a>";
  1721. }
  1722. else
  1723. {
  1724. return $name.' ('.get_lang('Anonymous').')';
  1725. }
  1726. }
  1727. /**
  1728. * The thread view counter gets increased every time someone looks at the thread
  1729. *
  1730. * @param
  1731. * @return
  1732. *
  1733. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1734. * @version february 2006, dokeos 1.8
  1735. */
  1736. function increase_thread_view($thread_id)
  1737. {
  1738. global $table_threads;
  1739. $sql="UPDATE $table_threads SET thread_views=thread_views+1 WHERE thread_id='".mysql_real_escape_string($thread_id)."'"; // this needs to be cleaned first
  1740. $result=api_sql_query($sql, __LINE__, __FILE__);
  1741. }
  1742. /**
  1743. * The relies counter gets increased every time somebody replies to the thread
  1744. *
  1745. * @param
  1746. * @return
  1747. *
  1748. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1749. * @version february 2006, dokeos 1.8
  1750. */
  1751. function update_thread($thread_id, $last_post_id,$post_date)
  1752. {
  1753. global $table_threads;
  1754. $sql="UPDATE $table_threads SET thread_replies=thread_replies+1,
  1755. thread_last_post='".mysql_real_escape_string($last_post_id)."',
  1756. thread_date='".mysql_real_escape_string($post_date)."' WHERE thread_id='".mysql_real_escape_string($thread_id)."'"; // this needs to be cleaned first
  1757. $result=api_sql_query($sql, __LINE__, __FILE__);
  1758. }
  1759. /**
  1760. * This function is called when the user is not allowed in this forum/thread/...
  1761. *
  1762. * @param
  1763. * @return
  1764. *
  1765. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1766. * @version february 2006, dokeos 1.8
  1767. */
  1768. function forum_not_allowed_here()
  1769. {
  1770. Display :: display_error_message(get_lang('NotAllowedHere'));
  1771. Display :: display_footer();
  1772. exit;
  1773. }
  1774. /**
  1775. * This function is used to find all the information about what's new in the forum tool
  1776. *
  1777. * @param
  1778. * @return
  1779. *
  1780. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1781. * @version february 2006, dokeos 1.8
  1782. */
  1783. function get_whats_new()
  1784. {
  1785. global $_uid;
  1786. global $_course;
  1787. global $table_posts;
  1788. // note this has later to be replaced by the tool constant. But temporarily bb_forum is used since this is the only thing that is in the tracking currently.
  1789. //$tool=TOOL_FORUM;
  1790. $tool=TOOL_FORUM; //
  1791. // to do: remove this. For testing purposes only
  1792. //session_unregister('last_forum_access');
  1793. //session_unregister('whatsnew_post_info');
  1794. if (!$_SESSION['last_forum_access'])
  1795. {
  1796. $tracking_last_tool_access=Database::get_statistic_table(STATISTIC_TRACK_E_LASTACCESS_TABLE);
  1797. $sql="SELECT * FROM ".$tracking_last_tool_access." WHERE access_user_id='".mysql_real_escape_string($_uid)."' AND access_cours_code='".mysql_real_escape_string($_course['sysCode'])."' AND access_tool='".mysql_real_escape_string($tool)."'";
  1798. $result=api_sql_query($sql,__FILE__,__LINE__);
  1799. $row=mysql_fetch_array($result);
  1800. $_SESSION['last_forum_access']=$row['access_date'];
  1801. }
  1802. if (!$_SESSION['whatsnew_post_info'])
  1803. {
  1804. if ($_SESSION['last_forum_access']<>'')
  1805. {
  1806. $sql="SELECT * FROM".$table_posts."WHERE post_date>'".mysql_real_escape_string($_SESSION['last_forum_access'])."'"; // note: check the performance of this query.
  1807. $result=api_sql_query($sql,__FILE__,__LINE__);
  1808. while ($row=mysql_fetch_array($result))
  1809. {
  1810. $whatsnew_post_info[$row['forum_id']][$row['thread_id']][$row['post_id']]=$row['post_date'];
  1811. }
  1812. $_SESSION['whatsnew_post_info']=$whatsnew_post_info;
  1813. }
  1814. }
  1815. }
  1816. /**
  1817. * With this function we find the number of posts and topics in a given forum.
  1818. *
  1819. * @param
  1820. * @return
  1821. *
  1822. * @todo consider to call this function only once and let it return an array where the key is the forum id and the value is an array with number_of_topics and number of post
  1823. * as key of this array and the value as a value. This could reduce the number of queries needed (especially when there are more forums)
  1824. * @todo consider merging both in one query.
  1825. *
  1826. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1827. * @version february 2006, dokeos 1.8
  1828. *
  1829. * @deprecated the counting mechanism is now inside the function get_forums
  1830. */
  1831. function get_post_topics_of_forum($forum_id)
  1832. {
  1833. global $table_posts;
  1834. global $table_threads;
  1835. global $table_item_property;
  1836. $sql="SELECT count(*) as number_of_posts FROM $table_posts WHERE forum_id='".$forum_id."'";
  1837. if (api_is_allowed_to_edit())
  1838. {
  1839. $sql="SELECT count(*) as number_of_posts
  1840. FROM $table_posts posts, $table_threads threads, $table_item_property item_property
  1841. WHERE posts.forum_id='".mysql_real_escape_string($forum_id)."'
  1842. AND posts.thread_id=threads.thread_id
  1843. AND item_property.ref=threads.thread_id
  1844. AND item_property.visibility<>2
  1845. AND item_property.tool='".TOOL_FORUM_THREAD."'
  1846. ";
  1847. }
  1848. else
  1849. {
  1850. $sql="SELECT count(*) as number_of_posts
  1851. FROM $table_posts posts, $table_threads threads, $table_item_property item_property
  1852. WHERE posts.forum_id='".mysql_real_escape_string($forum_id)."'
  1853. AND posts.thread_id=threads.thread_id
  1854. AND item_property.ref=threads.thread_id
  1855. AND item_property.visibility=1
  1856. AND posts.visible=1
  1857. AND item_property.tool='".TOOL_FORUM_THREAD."'
  1858. ";
  1859. }
  1860. $result=api_sql_query($sql);
  1861. $row=mysql_fetch_array($result);
  1862. $number_of_posts=$row['number_of_posts'];
  1863. // we could loop through the result array and count the number of different group_ids but I have chosen to use a second sql statement
  1864. if (api_is_allowed_to_edit())
  1865. {
  1866. $sql="SELECT count(*) as number_of_topics
  1867. FROM $table_threads threads, $table_item_property item_property
  1868. WHERE threads.forum_id='".mysql_real_escape_string($forum_id)."'
  1869. AND item_property.ref=threads.thread_id
  1870. AND item_property.visibility<>2
  1871. AND item_property.tool='".TOOL_FORUM_THREAD."'
  1872. ";
  1873. }
  1874. else
  1875. {
  1876. $sql="SELECT count(*) as number_of_topics
  1877. FROM $table_threads threads, $table_item_property item_property
  1878. WHERE threads.forum_id='".mysql_real_escape_string($forum_id)."'
  1879. AND item_property.ref=threads.thread_id
  1880. AND item_property.visibility=1
  1881. AND item_property.tool='".TOOL_FORUM_THREAD."'
  1882. ";
  1883. }
  1884. $result=api_sql_query($sql);
  1885. $row=mysql_fetch_array($result);
  1886. $number_of_topics=$row['number_of_topics'];
  1887. if ($number_of_topics=='')
  1888. {
  1889. $number_of_topics=0; // due to the nature of the group by this can result in an empty string.
  1890. }
  1891. $return=array('number_of_topics'=>$number_of_topics, 'number_of_posts'=>$number_of_posts);
  1892. return $return;
  1893. }
  1894. /**
  1895. * This function approves a post = change
  1896. *
  1897. * @param $post_id the id of the post that will be deleted
  1898. * @param $action make the post visible or invisible
  1899. * @return
  1900. *
  1901. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1902. * @version february 2006, dokeos 1.8
  1903. */
  1904. function approve_post($post_id, $action)
  1905. {
  1906. global $table_posts;
  1907. if ($action=='invisible')
  1908. {
  1909. $visibility_value=0;
  1910. }
  1911. if ($action=='visible')
  1912. {
  1913. $visibility_value=1;
  1914. handle_mail_cue('post',$post_id);
  1915. }
  1916. $sql="UPDATE $table_posts SET visible='".mysql_real_escape_string($visibility_value)."' WHERE post_id='".mysql_real_escape_string($post_id)."'";
  1917. $return=api_sql_query($sql);
  1918. if ($return)
  1919. {
  1920. return 'PostVisibilityChanged';
  1921. }
  1922. }
  1923. /**
  1924. * This function retrieves all the unapproved messages for a given forum
  1925. * This is needed to display the icon that there are unapproved messages in that thread (only the courseadmin can see this)
  1926. *
  1927. * @param $forum_id the forum where we want to know the unapproved messages of
  1928. * @return
  1929. *
  1930. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1931. * @version february 2006, dokeos 1.8
  1932. */
  1933. function get_unaproved_messages($forum_id)
  1934. {
  1935. global $table_posts;
  1936. $return_array=array();
  1937. $sql="SELECT DISTINCT thread_id FROM $table_posts WHERE forum_id='".mysql_real_escape_string($forum_id)."' AND visible='0'";
  1938. $result=api_sql_query($sql);
  1939. while($row=mysql_fetch_array($result))
  1940. {
  1941. $return_array[]=$row['thread_id'];
  1942. }
  1943. return $return_array;
  1944. }
  1945. /**
  1946. * This function sends the notification mails to everybody who stated that they wanted to be informed when a new post
  1947. * was added to a given thread.
  1948. *
  1949. * @param
  1950. * @return
  1951. *
  1952. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1953. * @version february 2006, dokeos 1.8
  1954. */
  1955. function send_notification_mails($thread_id, $reply_info)
  1956. {
  1957. global $table_posts;
  1958. global $table_user;
  1959. global $table_mailcue;
  1960. // First we need to check if
  1961. // 1. the forum category is visible
  1962. // 2. the forum is visible
  1963. // 3. the thread is visible
  1964. // 4. the reply is visible (=when there is
  1965. $current_thread=get_thread_information($thread_id);
  1966. $current_forum=get_forum_information($current_thread['forum_id']);
  1967. $current_forum_category=get_forumcategory_information($current_forum['forum_category']);
  1968. if($current_thread['visibility']=='1' AND $current_forum['visibility']=='1' AND $current_forum_category['visibility']=='1' AND $current_forum['approval_direct_post']=='0')
  1969. {
  1970. $send_mails=true;
  1971. }
  1972. else
  1973. {
  1974. $send_mails=false;
  1975. }
  1976. // the forum category, the forum, the thread and the reply are visible to the user
  1977. if ($send_mails==true)
  1978. {
  1979. $sql="SELECT user.firstname, user.lastname, user.email, user.user_id
  1980. FROM $table_posts post, $table_user user
  1981. WHERE post.thread_id='".mysql_real_escape_string($thread_id)."'
  1982. AND post.post_notification='1'
  1983. AND post.poster_id=user.user_id";
  1984. $result=api_sql_query($sql, __LINE__, __FILE__);
  1985. while ($row=mysql_fetch_array($result))
  1986. {
  1987. send_mail($row, $current_thread);
  1988. }
  1989. }
  1990. else
  1991. {
  1992. $sql="SELECT * FROM $table_posts WHERE thread_id='".mysql_real_escape_string($thread_id)."' AND post_notification='1'";
  1993. $result=api_sql_query($sql, __LINE__, __FILE__);
  1994. while ($row=mysql_fetch_array($result))
  1995. {
  1996. $sql_mailcue="INSERT INTO $table_mailcue (thread_id, post_id) VALUES ('".mysql_real_escape_string($thread_id)."', '".mysql_real_escape_string($reply_info['new_post_id'])."')";
  1997. $result_mailcue=api_sql_query($sql_mailcue, __LINE__, __FILE__);
  1998. }
  1999. }
  2000. }
  2001. /**
  2002. * This function is called whenever something is made visible because there might be new posts and the user might have indicated that (s)he wanted
  2003. * to be informed about the new posts by mail.
  2004. *
  2005. * @param
  2006. * @return
  2007. *
  2008. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2009. * @version february 2006, dokeos 1.8
  2010. */
  2011. function handle_mail_cue($content, $id)
  2012. {
  2013. global $table_mailcue;
  2014. global $table_forums;
  2015. global $table_threads;
  2016. global $table_posts;
  2017. global $table_users;
  2018. // if the post is made visible we only have to send mails to the people who indicated that they wanted to be informed for that thread.
  2019. if ($content=='post')
  2020. {
  2021. // getting the information about the post (need the thread_id)
  2022. $post_info=get_post_information($id);
  2023. // sending the mail to all the users that wanted to be informed for replies on this thread.
  2024. $sql="SELECT users.firstname, users.lastname, users.user_id, users.email FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  2025. WHERE posts.thread_id='".mysql_real_escape_string($post_info['thread_id'])."'
  2026. AND posts.post_notification='1'
  2027. AND mailcue.thread_id='".mysql_real_escape_string($post_info['thread_id'])."'
  2028. AND users.user_id=posts.poster_id
  2029. GROUP BY users.email";
  2030. $result=api_sql_query($sql, __LINE__, __FILE__);
  2031. while ($row=mysql_fetch_array($result))
  2032. {
  2033. send_mail($row, get_thread_information($post_info['thread_id']));
  2034. }
  2035. // deleting the relevant entries from the mailcue
  2036. $sql_delete_mailcue="DELETE FROM $table_mailcue WHERE post_id='".mysql_real_escape_string($id)."' AND thread_id='".mysql_real_escape_string($post_info['thread_id'])."'";
  2037. //$result=api_sql_query($sql_delete_mailcue, __LINE__, __FILE__);
  2038. }
  2039. elseif ($content=='thread')
  2040. {
  2041. // sending the mail to all the users that wanted to be informed for replies on this thread.
  2042. $sql="SELECT users.firstname, users.lastname, users.user_id, users.email FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  2043. WHERE posts.thread_id='".mysql_real_escape_string($id)."'
  2044. AND posts.post_notification='1'
  2045. AND mailcue.thread_id='".mysql_real_escape_string($id)."'
  2046. AND users.user_id=posts.poster_id
  2047. GROUP BY users.email";
  2048. $result=api_sql_query($sql, __LINE__, __FILE__);
  2049. while ($row=mysql_fetch_array($result))
  2050. {
  2051. send_mail($row, get_thread_information($id));
  2052. }
  2053. // deleting the relevant entries from the mailcue
  2054. $sql_delete_mailcue="DELETE FROM $table_mailcue WHERE thread_id='".mysql_real_escape_string($id)."'";
  2055. $result=api_sql_query($sql_delete_mailcue, __LINE__, __FILE__);
  2056. }
  2057. elseif ($content=='forum')
  2058. {
  2059. $sql="SELECT * FROM $table_threads WHERE forum_id='".mysql_real_escape_string($id)."'";
  2060. $result=api_sql_query($sql, __LINE__, __FILE__);
  2061. while ($row=mysql_fetch_array($result))
  2062. {
  2063. handle_mail_cue('thread',$row['thread_id']);
  2064. }
  2065. }
  2066. elseif ($content=='forum_category')
  2067. {
  2068. $sql="SELECT * FROM $table_forums WHERE forum_category ='".mysql_real_escape_string($id)."'";
  2069. $result=api_sql_query($sql, __LINE__, __FILE__);
  2070. while ($row=mysql_fetch_array($result))
  2071. {
  2072. handle_mail_cue('forum',$row['forum_id']);
  2073. }
  2074. }
  2075. else
  2076. {
  2077. return get_lang('Error');
  2078. }
  2079. }
  2080. /**
  2081. * This function sends the mails for the mail notification
  2082. *
  2083. * @param
  2084. * @return
  2085. *
  2086. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2087. * @version february 2006, dokeos 1.8
  2088. */
  2089. function send_mail($user_info=array(), $thread_information=array())
  2090. {
  2091. global $_course;
  2092. global $_uid;
  2093. $email_subject = get_lang('NewForumPost')." - ".$_course['official_code'];
  2094. if (isset($thread_information) and is_array($thread_information))
  2095. {
  2096. $thread_link= api_get_path('WEB_CODE_PATH').'forum/viewthread.php?forum='.$thread_information['forum_id'].'&thread='.$thread_information['thread_id'];
  2097. //http://157.193.57.110/dokeos_cvs/claroline/forum/viewthread.php?forum=12&thread=49
  2098. }
  2099. $email_body=get_lang('Dear')." ".$user_info['firstname']." ".$user_info['lastname']."\n\r";
  2100. $email_body .= '['.$_course['official_code'].'] - ['.$_course['name']."]\n";
  2101. $email_body .= get_lang('NewForumPost')."\n";
  2102. $email_body .= get_lang('YouWantedToStayInformed')."\n";
  2103. $email_body .= get_lang('ThreadCanBeFoundHere').":".$thread_link."\n";
  2104. //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first)
  2105. //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case)
  2106. if(empty($charset)){$charset='ISO-8859-1';}
  2107. $encoding = 'Content-Type: text/html; charset='. $charset;
  2108. if ($user_info['user_id']<>$_uid)
  2109. {
  2110. $newmail = api_mail($user_info["lastname"].' '.$user_info["firstname"], $user_info["email"], $email_subject, $email_body, $_SESSION['_user']['lastName'].' '.$_SESSION['_user']['firstName'], $_SESSION['_user']['mail'],$encoding);
  2111. }
  2112. }
  2113. /**
  2114. * This function displays the form for moving a thread to a different (already existing) forum
  2115. *
  2116. * @param
  2117. * @return
  2118. *
  2119. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2120. * @version february 2006, dokeos 1.8
  2121. */
  2122. function move_thread_form()
  2123. {
  2124. // initiate the object
  2125. $form = new FormValidator('movepost', 'post', $_SERVER['PHP_SELF'].'?forum='.$_GET['forum'].'&thread='.$_GET['thread'].'&action='.$_GET['action']);
  2126. // the header for the form
  2127. $form->addElement('header', '', get_lang('MoveThread'));
  2128. // invisible form: the thread_id
  2129. $form->addElement('hidden', 'thread_id', $_GET['thread']); // note: this has to be cleaned first
  2130. // the fora
  2131. $forum_categories=get_forum_categories();
  2132. $forums=get_forums();
  2133. $htmlcontent="\n<tr>\n<td></td>\n<td>\n<SELECT NAME='forum'>\n";
  2134. foreach ($forum_categories as $key=>$category)
  2135. {
  2136. $htmlcontent.="\t<OPTGROUP LABEL=\"".$category['cat_title']."\">\n";
  2137. foreach ($forums as $key=>$forum)
  2138. {
  2139. if ($forum['forum_category']==$category['cat_id'])
  2140. {
  2141. $htmlcontent.="\t\t<OPTION VALUE='".$forum['forum_id']."'>".$forum['forum_title']."</OPTION>\n";
  2142. }
  2143. }
  2144. $htmlcontent.="\t</OPTGROUP>\n";
  2145. }
  2146. $htmlcontent.="</SELECT>\n</td></tr>";
  2147. $form->addElement('html',$htmlcontent);
  2148. // The OK button
  2149. $form->addElement('submit', 'SubmitForum', 'OK');
  2150. // The validation or display
  2151. if( $form->validate())
  2152. {
  2153. $values = $form->exportValues();
  2154. if (isset($_POST['forum']))
  2155. {
  2156. store_move_thread($values);
  2157. }
  2158. }
  2159. else
  2160. {
  2161. $form->display();
  2162. }
  2163. }
  2164. /**
  2165. * This function displays the form for moving a post message to a different (already existing) or a new thread.
  2166. *
  2167. * @param
  2168. * @return
  2169. *
  2170. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2171. * @version february 2006, dokeos 1.8
  2172. */
  2173. function move_post_form()
  2174. {
  2175. // initiate the object
  2176. $form = new FormValidator('movepost', 'post', $_SERVER['PHP_SELF'].'?forum='.$_GET['forum'].'&thread='.$_GET['thread'].'&post='.$_GET['post'].'&action='.$_GET['action'].'&post='.$_GET['post']);
  2177. // the header for the form
  2178. $form->addElement('header', '', get_lang('MovePost'));
  2179. // invisible form: the post_id
  2180. $form->addElement('hidden', 'post_id', $_GET['post']); // note: this has to be cleaned first
  2181. // dropdown list: Threads of this forum
  2182. $threads=get_threads($_GET['forum']); // note: this has to be cleaned
  2183. //my_print_r($threads);
  2184. $threads_list[0]=get_lang('ANewThread');
  2185. foreach ($threads as $key=>$value)
  2186. {
  2187. $threads_list[$value['thread_id']]=$value['thread_title'];
  2188. }
  2189. $form->addElement('select', 'thread', get_lang('MoveToThread'), $threads_list);
  2190. // The OK button
  2191. $form->addElement('submit', 'SubmitForum', 'OK');
  2192. // setting the rules
  2193. $form->addRule('thread', get_lang('ThisFieldIsRequired'), 'required');
  2194. // The validation or display
  2195. if( $form->validate() )
  2196. {
  2197. $values = $form->exportValues();
  2198. store_move_post($values);
  2199. }
  2200. else
  2201. {
  2202. $form->display();
  2203. }
  2204. }
  2205. /**
  2206. *
  2207. * @param
  2208. * @return
  2209. *
  2210. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2211. * @version february 2006, dokeos 1.8
  2212. */
  2213. function store_move_post($values)
  2214. {
  2215. global $table_posts;
  2216. global $table_threads;
  2217. global $table_forums;
  2218. global $_course;
  2219. if ($values['thread']=='0')
  2220. {
  2221. $current_post=get_post_information($values['post_id']);
  2222. // storing a new thread
  2223. $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_last_post, thread_date)
  2224. VALUES (
  2225. '".mysql_real_escape_string($current_post['post_title'])."',
  2226. '".mysql_real_escape_string($current_post['forum_id'])."',
  2227. '".mysql_real_escape_string($current_post['poster_id'])."',
  2228. '".mysql_real_escape_string($current_post['poster_name'])."',
  2229. '".mysql_real_escape_string($values['post_id'])."',
  2230. '".mysql_real_escape_string($current_post['post_date'])."'
  2231. )";
  2232. //echo $sql.'<br />';
  2233. $result=api_sql_query($sql, __LINE__, __FILE__);
  2234. $new_thread_id=mysql_insert_id();
  2235. api_item_property_update($_course, TOOL_FORUM_THREAD, $new_thread_id,"visible", $current_post['poster_id']);
  2236. // moving the post to the newly created thread
  2237. $sql="UPDATE $table_posts SET thread_id='".mysql_real_escape_string($new_thread_id)."', post_parent_id='0' WHERE post_id='".mysql_real_escape_string($values['post_id'])."'";
  2238. $result=api_sql_query($sql, __LINE__, __FILE__);
  2239. //echo $sql.'<br />';
  2240. // resetting the parent_id of the thread to 0 for all those who had this moved post as parent
  2241. $sql="UPDATE $table_posts SET post_parent_id='0' WHERE post_parent_id='".mysql_real_escape_string($values['post_id'])."'";
  2242. $result=api_sql_query($sql, __LINE__, __FILE__);
  2243. //echo $sql.'<br />';
  2244. // updating updating the number of threads in the forum
  2245. $sql="UPDATE $table_forums SET forum_threads=forum_threads+1 WHERE forum_id='".mysql_real_escape_string($current_post['forum_id'])."'";
  2246. $result=api_sql_query($sql, __LINE__, __FILE__);
  2247. //echo $sql.'<br />';
  2248. // resetting the last post of the old thread and decreasing the number of replies and the thread
  2249. $sql="SELECT * FROM $table_posts WHERE thread_id='".mysql_real_escape_string($current_post['thread_id'])."' ORDER BY post_id DESC";
  2250. //echo $sql.'<br />';
  2251. $result=api_sql_query($sql, __LINE__, __FILE__);
  2252. $row=mysql_fetch_array($result);
  2253. //my_print_r($row);
  2254. $sql="UPDATE $table_threads SET thread_last_post='".$row['post_id']."', thread_replies=thread_replies-1 WHERE thread_id='".mysql_real_escape_string($current_post['thread_id'])."'";
  2255. $result=api_sql_query($sql, __LINE__, __FILE__);
  2256. //echo $sql.'<br />';
  2257. }
  2258. else
  2259. {
  2260. // moving to the chosen thread
  2261. $sql="UPDATE $table_posts SET thread_id='".mysql_real_escape_string($_POST['thread'])."', post_parent_id='0' WHERE post_id='".mysql_real_escape_string($values['post_id'])."'";
  2262. $result=api_sql_query($sql, __LINE__, __FILE__);
  2263. // resetting the parent_id of the thread to 0 for all those who had this moved post as parent
  2264. $sql="UPDATE $table_posts SET post_parent_id='0' WHERE post_parent_id='".mysql_real_escape_string($values['post_id'])."'";
  2265. $result=api_sql_query($sql, __LINE__, __FILE__);
  2266. }
  2267. return get_lang('ThreadMoved');
  2268. }
  2269. /**
  2270. *
  2271. * @param
  2272. * @return
  2273. *
  2274. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2275. * @version february 2006, dokeos 1.8
  2276. */
  2277. function store_move_thread($values)
  2278. {
  2279. global $table_posts;
  2280. global $table_threads;
  2281. global $table_forums;
  2282. global $_course;
  2283. // change the thread table: setting the forum_id to the new forum
  2284. $sql="UPDATE $table_threads SET forum_id='".mysql_real_escape_string($_POST['forum'])."' WHERE thread_id='".mysql_real_escape_string($_POST['thread_id'])."'";
  2285. $result=api_sql_query($sql, __LINE__, __FILE__);
  2286. // changing all the posts of the thread: setting the forum_id to the new forum
  2287. $sql="UPDATE $table_posts SET forum_id='".mysql_real_escape_string($_POST['forum'])."' WHERE thread_id='".mysql_real_escape_string($_POST['thread_id'])."'";
  2288. $result=api_sql_query($sql, __LINE__, __FILE__);
  2289. return get_lang('ThreadMoved');
  2290. }
  2291. /**
  2292. *
  2293. * @param
  2294. * @return
  2295. *
  2296. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2297. * @version february 2006, dokeos 1.8
  2298. */
  2299. function prepare4display($input='')
  2300. {
  2301. if (!is_array($input))
  2302. {
  2303. return stripslashes($input);
  2304. }
  2305. else
  2306. {
  2307. /*foreach ($input as $key=>$value)
  2308. {
  2309. $returnarray[$key]=stripslashes($value);
  2310. }*/
  2311. $returnarray=array_walk($input, 'stripslashes');
  2312. return $returnarray;
  2313. }
  2314. }
  2315. ?>