forumfunction.inc.php 102 KB

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