forumfunction.inc.php 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * These files are a complete rework of the forum. The database structure is
  22. * based on phpBB but all the code is rewritten. A lot of new functionalities
  23. * are added:
  24. * - forum categories and forums can be sorted up or down, locked or made invisible
  25. * - consistent and integrated forum administration
  26. * - forum options: are students allowed to edit their post?
  27. * moderation of posts (approval)
  28. * reply only forums (students cannot create new threads)
  29. * multiple forums per group
  30. * - sticky messages
  31. * - new view option: nested view
  32. * - quoting a message
  33. *
  34. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  35. * @copyright Ghent University
  36. * @copyright Patrick Cool
  37. * @author Julio Montoya <gugli100@gmail.com>, Dokeos Several fixes
  38. * @package dokeos.forum
  39. *
  40. * @todo several functions have to be moved to the itemmanager library
  41. * @todo displaying icons => display library
  42. * @todo complete the missing phpdoc the correct order should be
  43. *
  44. * some explanation of the function
  45. *
  46. * @param
  47. * @return
  48. *
  49. @todo
  50. *
  51. * @author firstname lastname <email>, organisation
  52. * @version (day) month year
  53. *
  54. * @deprecated
  55. */
  56. /**
  57. **************************************************************************
  58. * IMPORTANT NOTICE
  59. * Please do not change anything is this code yet because there are still
  60. * some significant code that need to happen and I do not have the time to
  61. * merge files and test it all over again. So for the moment, please do not
  62. * touch the code
  63. * -- Patrick Cool <patrick.cool@UGent.be>
  64. **************************************************************************
  65. */
  66. require_once(api_get_path(LIBRARY_PATH).'mail.lib.inc.php');
  67. require_once(api_get_path(LIBRARY_PATH).'text.lib.php');
  68. require_once(api_get_path(INCLUDE_PATH).'/conf/mail.conf.php');
  69. require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  70. require_once(api_get_path(LIBRARY_PATH).'text.lib.php');
  71. get_notifications_of_user();
  72. /**
  73. * This function handles all the forum and forumcategories actions. This is a wrapper for the
  74. * forum and forum categories. All this code code could go into the section where this function is
  75. * called but this make the code there cleaner.
  76. * @param
  77. * @return
  78. *
  79. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  80. * @version february 2006, dokeos 1.8
  81. */
  82. function handle_forum_and_forumcategories() {
  83. $action_forum_cat = isset($_GET['action']) ? $_GET['action'] : '';
  84. $post_submit_cat= isset($_POST['SubmitForumCategory']) ? true : false;
  85. $post_submit_forum= isset($_POST['SubmitForum']) ? true : false;
  86. $get_id=isset($_GET['id']) ? $_GET['id'] : '';
  87. // Adding a forum category
  88. if (($action_forum_cat=='add' && $_GET['content']=='forumcategory') || $post_submit_cat ) {
  89. show_add_forumcategory_form();
  90. }
  91. // Adding a forum
  92. if ((($action_forum_cat=='add' || $action_forum_cat=='edit') && $_GET['content']=='forum') || $post_submit_forum ) {
  93. if ($action_forum_cat=='edit' && $get_id || $post_submit_forum ) {
  94. $inputvalues=get_forums(strval(intval($get_id))); // note: this has to be cleaned first
  95. } else {
  96. $inputvalues='';
  97. }
  98. show_add_forum_form($inputvalues);
  99. }
  100. // Edit a forum category
  101. if (($action_forum_cat=='edit' && $_GET['content']=='forumcategory' && isset($_GET['id'])) || (isset($_POST['SubmitEditForumCategory'])) ? true : false )
  102. {
  103. $forum_category=get_forum_categories(strval(intval($_GET['id']))); // note: this has to be cleaned first
  104. show_edit_forumcategory_form($forum_category);
  105. }
  106. // Delete a forum category
  107. if (( isset($_GET['action']) && $_GET['action']=='delete') && isset($_GET['content']) && $get_id) {
  108. $id_forum=Security::remove_XSS($get_id);
  109. $list_threads=get_threads($id_forum);
  110. for ( $i=0; $i < count($list_threads); $i++ ) {
  111. $messaje=delete_forum_forumcategory_thread('thread',$list_threads[$i]['thread_id']);
  112. $table_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  113. $sql_link='DELETE FROM '.$table_link.' WHERE ref_id='.$list_threads[$i]['thread_id'].' and type=5 and course_code="'.api_get_course_id().'";';
  114. api_sql_query($sql_link,__FILE__,__LINE__);
  115. }
  116. $return_message=delete_forum_forumcategory_thread($_GET['content'],$_GET['id']);
  117. Display :: display_confirmation_message($return_message,false);
  118. }
  119. // Change visibility of a forum or a forum category
  120. if (($action_forum_cat=='invisible' || $action_forum_cat=='visible') && isset($_GET['content']) && isset($_GET['id'])) {
  121. $return_message=change_visibility($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first
  122. Display :: display_confirmation_message($return_message,false);
  123. }
  124. // Change lock status of a forum or a forum category
  125. if (($action_forum_cat=='lock' || $action_forum_cat=='unlock') && isset($_GET['content']) && isset($_GET['id'])) {
  126. $return_message=change_lock_status($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first
  127. Display :: display_confirmation_message($return_message,false);
  128. }
  129. // Move a forum or a forum category
  130. if ($action_forum_cat=='move' && isset($_GET['content']) && isset($_GET['id']) && isset($_GET['direction'])) {
  131. $return_message=move_up_down($_GET['content'], $_GET['direction'], $_GET['id']);// note: this has to be cleaned first
  132. Display :: display_confirmation_message($return_message,false);
  133. }
  134. }
  135. /**
  136. * This function displays the form that is used to add a forum category.
  137. *
  138. * @param
  139. * @return
  140. *
  141. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  142. * @version february 2006, dokeos 1.8
  143. */
  144. function show_add_forumcategory_form($inputvalues=array()) {
  145. $gradebook=Security::remove_XSS($_GET['gradebook']);
  146. // initiate the object
  147. $form = new FormValidator('forumcategory','post','index.php?&gradebook='.$gradebook.'');
  148. // settting the form elements
  149. $form->addElement('header', '', get_lang('AddForumCategory'));
  150. $form->addElement('text', 'forum_category_title', get_lang('Title'),'class="input_titles"');
  151. //$form->applyFilter('forum_category_title', 'html_filter');
  152. $form->addElement('html_editor', 'forum_category_comment', get_lang('Comment'));
  153. //$form->applyFilter('forum_category_comment', 'html_filter');
  154. $form->addElement('style_submit_button', 'SubmitForumCategory', get_lang('CreateCategory'), 'class="add"');
  155. // setting the rules
  156. $form->addRule('forum_category_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  157. // The validation or display
  158. if ( $form->validate() ) {
  159. $check = Security::check_token('post');
  160. if ($check) {
  161. $values = $form->exportValues();
  162. store_forumcategory($values);
  163. }
  164. Security::clear_token();
  165. } else {
  166. $token = Security::get_token();
  167. $form->addElement('hidden','sec_token');
  168. $form->setConstants(array('sec_token' => $token));
  169. $form->display();
  170. }
  171. }
  172. /**
  173. * This function displays the form that is used to add a forum category.
  174. *
  175. * @param
  176. * @return
  177. *
  178. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  179. * @version february 2006, dokeos 1.8
  180. */
  181. function show_add_forum_form($inputvalues=array()) {
  182. global $_course;
  183. $gradebook=Security::remove_XSS($_GET['gradebook']);
  184. // initiate the object
  185. $form = new FormValidator('forumcategory', 'post', 'index.php?gradebook='.$gradebook.'');
  186. // the header for the form
  187. if (!empty($inputvalues)) {
  188. $form_title = get_lang('EditForum');
  189. } else {
  190. $form_title = get_lang('AddForum');
  191. }
  192. $session_header = isset($_SESSION['session_name']) ? ' ('.$_SESSION['session_name'].') ' : '';
  193. $form->addElement('header', '', $form_title.$session_header);
  194. // we have a hidden field if we are editing
  195. if (is_array($inputvalues)) {
  196. $my_forum_id=isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
  197. $form->addElement('hidden', 'forum_id', $my_forum_id);
  198. }
  199. // The title of the forum
  200. $form->addElement('text', 'forum_title', get_lang('Title'),'class="input_titles"');
  201. //$form->applyFilter('forum_title', 'html_filter');
  202. // The comment of the forum
  203. $form->addElement('html_editor', 'forum_comment', get_lang('Comment'));
  204. //$form->applyFilter('forum_comment', 'html_filter');
  205. // dropdown list: Forum Categories
  206. $forum_categories=get_forum_categories();
  207. foreach ($forum_categories as $key=>$value) {
  208. $forum_categories_titles[$value['cat_id']]=$value['cat_title'];
  209. }
  210. $form->addElement('select', 'forum_category', get_lang('InForumCategory'), $forum_categories_titles);
  211. $form->applyFilter('forum_category', 'html_filter');
  212. if ($_course['visibility']==COURSE_VISIBILITY_OPEN_WORLD) {
  213. // This is for vertical
  214. //$form->addElement('radio', 'allow_anonymous', get_lang('AllowAnonymousPosts'), get_lang('Yes'), 1);
  215. //$form->addElement('radio', 'allow_anonymous', '', get_lang('No'), 0);
  216. // This is for horizontal
  217. $group='';
  218. $group[] =& HTML_QuickForm::createElement('radio', 'allow_anonymous',null,get_lang('Yes'),1);
  219. $group[] =& HTML_QuickForm::createElement('radio', 'allow_anonymous',null,get_lang('No'),0);
  220. $form->addGroup($group, 'allow_anonymous_group', get_lang('AllowAnonymousPosts'), '&nbsp;');
  221. }
  222. // This is for vertical
  223. //$form->addElement('radio', 'students_can_edit', get_lang('StudentsCanEdit'), get_lang('Yes'), 1);
  224. //$form->addElement('radio', 'students_can_edit', '', get_lang('No'), 0);
  225. // This is for horizontal
  226. /* if (document.getElementById('id_qualify').style.display == 'none') {
  227. document.getElementById('id_qualify').style.display = 'block';
  228. document.getElementById('plus').innerHTML='&nbsp;<img src=\"../img/div_hide.gif\" alt=\"\" />&nbsp;".get_lang('AddAnAttachment')."';
  229. } else {
  230. document.getElementById('options').style.display = 'none';
  231. document.getElementById('plus').innerHTML='&nbsp;<img src=\"../img/div_show.gif\" alt=\"\" />&nbsp;".get_lang('AddAnAttachment')."';
  232. }*/
  233. $form->addElement('html', '<div class="row"><div class="label"></div><div class="formw">');
  234. $form->addElement('html', '<br /><div id="plus"><a href="javascript://" onclick="advanced_parameters()" ><span id="plus_minus">&nbsp;<img src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</span></a></div>','');
  235. $form->addElement('html','</div></div>');
  236. $form->addElement('html','<div id="options" style="display:none">');
  237. $group='';
  238. $group[] =& HTML_QuickForm::createElement('radio', 'students_can_edit',null,get_lang('Yes'),1);
  239. $group[] =& HTML_QuickForm::createElement('radio', 'students_can_edit',null,get_lang('No'),0);
  240. $form->addGroup($group, 'students_can_edit_group', get_lang('StudentsCanEdit'), '&nbsp;');
  241. // This is for vertical
  242. //$form->addElement('radio', 'approval_direct', get_lang('ApprovalDirect'), get_lang('Approval'), 1);
  243. //$form->addElement('radio', 'approval_direct', '', get_lang('Direct'), 0);
  244. // This is for horizontal
  245. $group='';
  246. $group[] =& HTML_QuickForm::createElement('radio', 'approval_direct',null,get_lang('Approval'),1);
  247. $group[] =& HTML_QuickForm::createElement('radio', 'approval_direct',null,get_lang('Direct'),0);
  248. //$form->addGroup($group, 'approval_direct_group', get_lang('ApprovalDirect'), '&nbsp;');
  249. // This is for vertical
  250. //$form->addElement('radio', 'allow_attachments', get_lang('AllowAttachments'), get_lang('Yes'), 1);
  251. //$form->addElement('radio', 'allow_attachments', '', get_lang('No'), 0);
  252. // This is for horizontal
  253. $group='';
  254. $group[] =& HTML_QuickForm::createElement('radio', 'allow_attachments',null,get_lang('Yes'),1);
  255. $group[] =& HTML_QuickForm::createElement('radio', 'allow_attachments',null,get_lang('No'),0);
  256. //$form->addGroup($group, 'allow_attachments_group', get_lang('AllowAttachments'), '&nbsp;');
  257. // This is for vertical
  258. //$form->addElement('radio', 'allow_new_threads', get_lang('AllowNewThreads'), 1, get_lang('Yes'));
  259. //$form->addElement('radio', 'allow_new_threads', '', 0, get_lang('No'));
  260. // This is for horizontal
  261. $group='';
  262. $group[] =& HTML_QuickForm::createElement('radio', 'allow_new_threads',null, get_lang('Yes'),1);
  263. $group[] =& HTML_QuickForm::createElement('radio', 'allow_new_threads',null, get_lang('No'),0);
  264. $form->addGroup($group, 'allow_new_threads_group', get_lang('AllowNewThreads'), '&nbsp;');
  265. $group='';
  266. $group[] =& HTML_QuickForm::createElement('radio', 'default_view_type', null, get_lang('Flat'), 'flat');
  267. $group[] =& HTML_QuickForm::createElement('radio', 'default_view_type', null, get_lang('Threaded'), 'threaded');
  268. $group[] =& HTML_QuickForm::createElement('radio', 'default_view_type', null, get_lang('Nested'), 'nested');
  269. $form->addGroup($group, 'default_view_type_group', get_lang('DefaultViewType'), '&nbsp;');
  270. $form->addElement('static','Group', '<br /><strong>'.get_lang('GroupSettings').'</strong>');
  271. // dropdown list: Groups
  272. $groups=GroupManager::get_group_list();
  273. $groups_titles[0]=get_lang('NotAGroupForum');
  274. foreach ($groups as $key=>$value) {
  275. $groups_titles[$value['id']]=$value['name'];
  276. }
  277. $form->addElement('select', 'group_forum', get_lang('ForGroup'), $groups_titles);
  278. // Public or private group forum
  279. $group='';
  280. $group[] =& HTML_QuickForm::createElement('radio', 'public_private_group_forum', null, get_lang('Public'), 'public');
  281. $group[] =& HTML_QuickForm::createElement('radio', 'public_private_group_forum', null, get_lang('Private'), 'private');
  282. $form->addGroup($group, 'public_private_group_forum_group', get_lang('PublicPrivateGroupForum'), '&nbsp;');
  283. // Forum image
  284. $form->add_progress_bar();
  285. if (isset($inputvalues['forum_image']) && strlen($inputvalues['forum_image']) > 0) {
  286. $image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$inputvalues['forum_image'];
  287. /*
  288. $image_size = @getimagesize($image_path);
  289. */
  290. $image_size = @getimagesize(api_url_to_local_path($image_path));
  291. $img_attributes = '';
  292. if (!empty($image_size)) {
  293. if ($image_size[0] > 100 || $image_size[1] > 100) {
  294. //limit display width and height to 100px
  295. $img_attributes = 'width="100" height="100"';
  296. }
  297. $show_preview_image='<img src="'.$image_path.'" '.$img_attributes.'>';
  298. $div = '<div class="row">
  299. <div class="label">'.get_lang('PreviewImage').'</div>
  300. <div class="formw">
  301. '.$show_preview_image.'
  302. </div>
  303. </div>';
  304. $form->addElement('html', $div .'<br/>');
  305. $form->addElement('checkbox', 'remove_picture', null, get_lang('DelImage'));
  306. }
  307. }
  308. $forum_image=isset($inputvalues['forum_image']) ? $inputvalues['forum_image'] : '';
  309. $form->addElement('file', 'picture', ($forum_image != '' ? get_lang('UpdateImage') : get_lang('AddImage')));
  310. $form->addRule('picture', get_lang('OnlyImagesAllowed'), 'filetype', array ('jpg', 'jpeg', 'png', 'gif'));
  311. $form->addElement('html','</div>');
  312. // The OK button
  313. if (isset($_GET['id']) && $_GET['action']=='edit'){
  314. $class='save';
  315. $text=get_lang('ModifyForum');
  316. }else{
  317. $class='add';
  318. $text=get_lang('CreateForum');
  319. }
  320. $form->addElement('style_submit_button', 'SubmitForum', $text, 'class="'.$class.'"');
  321. // setting the rules
  322. $form->addRule('forum_title', get_lang('ThisFieldIsRequired'), 'required');
  323. $form->addRule('forum_category', get_lang('ThisFieldIsRequired'), 'required');
  324. global $charset;
  325. // settings the defaults
  326. if (!is_array($inputvalues)) {
  327. $defaults['allow_anonymous_group']['allow_anonymous']=0;
  328. $defaults['students_can_edit_group']['students_can_edit']=0;
  329. $defaults['approval_direct_group']['approval_direct']=0;
  330. $defaults['allow_attachments_group']['allow_attachments']=1;
  331. $defaults['allow_new_threads_group']['allow_new_threads']=0;
  332. $defaults['default_view_type_group']['default_view_type']=api_get_setting('default_forum_view');
  333. $defaults['public_private_group_forum_group']['public_private_group_forum']='public';
  334. if (isset($_GET['forumcategory'])) {
  335. $defaults['forum_category']=Security::remove_XSS($_GET['forumcategory']);
  336. }
  337. } else { // the default values when editing = the data in the table
  338. $defaults['forum_id']=isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
  339. $defaults['forum_title']=prepare4display(api_html_entity_decode(isset($inputvalues['forum_title']) ? $inputvalues['forum_title'] : null,ENT_QUOTES,$charset));
  340. $defaults['forum_comment']=prepare4display(isset($inputvalues['forum_comment'])?$inputvalues['forum_comment']:null);
  341. $defaults['forum_category']=isset($inputvalues['forum_category']) ? $inputvalues['forum_category'] : null;
  342. $defaults['allow_anonymous_group']['allow_anonymous']=isset($inputvalues['allow_anonymous']) ? $inputvalues['allow_anonymous'] :null;
  343. $defaults['students_can_edit_group']['students_can_edit']=isset($inputvalues['allow_edit'])?$inputvalues['allow_edit']:null;
  344. $defaults['approval_direct_group']['approval_direct']=isset($inputvalues['approval_direct_post'])?$inputvalues['approval_direct_post']:null;
  345. $defaults['allow_attachments_group']['allow_attachments']=isset($inputvalues['allow_attachments'])?$inputvalues['allow_attachments']:null;
  346. $defaults['allow_new_threads_group']['allow_new_threads']=isset($inputvalues['allow_new_threads'])?$inputvalues['allow_new_threads']:null;
  347. $defaults['default_view_type_group']['default_view_type']=isset($inputvalues['default_view'])?$inputvalues['default_view']:null;
  348. $defaults['public_private_group_forum_group']['public_private_group_forum']=isset($inputvalues['forum_group_public_private'])?$inputvalues['forum_group_public_private']:null;
  349. $defaults['group_forum']=isset($inputvalues['forum_of_group'])?$inputvalues['forum_of_group']:null;
  350. }
  351. $form->setDefaults($defaults);
  352. // The validation or display
  353. if( $form->validate() ) {
  354. $check = Security::check_token('post');
  355. if ($check) {
  356. $values = $form->exportValues();
  357. $return_message = store_forum($values);
  358. Display :: display_confirmation_message($return_message);
  359. }
  360. Security::clear_token();
  361. } else {
  362. $token = Security::get_token();
  363. $form->addElement('hidden','sec_token');
  364. $form->setConstants(array('sec_token' => $token));
  365. $form->display();
  366. }
  367. }
  368. /**
  369. * This function deletes the forum image if exists
  370. *
  371. * @param int forum id
  372. * @return boolean true if success
  373. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  374. * @version february 2006, dokeos 1.8
  375. */
  376. function delete_forum_image($forum_id)
  377. {
  378. $table_forums = Database::get_course_table(TABLE_FORUM);
  379. echo '<br />';
  380. $sql="SELECT forum_image FROM $table_forums WHERE forum_id = '".$forum_id."' ";
  381. $result=api_sql_query($sql,__FILE__,__LINE__);
  382. $row=Database::fetch_array($result);
  383. if ($row['forum_image']!='') {
  384. $del_file = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$row['forum_image'];
  385. return @unlink($del_file);
  386. } else {
  387. return false;
  388. }
  389. }
  390. /**
  391. * This function displays the form that is used to edit a forum category.
  392. * This is more or less a copy from the show_add_forumcategory_form function with the only difference that is uses
  393. * some default values. I tried to have both in one function but this gave problems with the handle_forum_and_forumcategories function
  394. * (storing was done twice)
  395. *
  396. * @param
  397. * @return
  398. *
  399. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  400. * @version february 2006, dokeos 1.8
  401. */
  402. function show_edit_forumcategory_form($inputvalues=array()) {
  403. // initiate the object
  404. $gradebook=Security::remove_XSS($_GET['gradebook']);
  405. $form = new FormValidator('forumcategory','post','index.php?&gradebook='.$gradebook.'');
  406. // settting the form elements
  407. $form->addElement('header', '', get_lang('EditForumCategory'));
  408. $form->addElement('hidden', 'forum_category_id');
  409. $form->addElement('text', 'forum_category_title', get_lang('Title'),'class="input_titles"');
  410. //$form->applyFilter('forum_category_title', 'html_filter');
  411. $form->addElement('html_editor', 'forum_category_comment', get_lang('Comment'));
  412. //$form->applyFilter('forum_category_comment', 'html_filter');
  413. $form->addElement('style_submit_button', 'SubmitEditForumCategory',get_lang('ModifyCategory'), 'class="save"');
  414. global $charset;
  415. // setting the default values
  416. $defaultvalues['forum_category_id']=$inputvalues['cat_id'];
  417. $defaultvalues['forum_category_title']=prepare4display(api_html_entity_decode($inputvalues['cat_title'],ENT_QUOTES,$charset));
  418. $defaultvalues['forum_category_comment']=prepare4display($inputvalues['cat_comment']);
  419. $form->setDefaults($defaultvalues);
  420. // setting the rules
  421. $form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required');
  422. // The validation or display
  423. if ( $form->validate() ) {
  424. $check = Security::check_token('post');
  425. if ($check) {
  426. $values = $form->exportValues();
  427. store_forumcategory($values);
  428. }
  429. Security::clear_token();
  430. } else {
  431. $token = Security::get_token();
  432. $form->addElement('hidden','sec_token');
  433. $form->setConstants(array('sec_token' => $token));
  434. $form->display();
  435. }
  436. }
  437. /**
  438. * This function stores the forum category in the database. The new category is added to the end.
  439. *
  440. * @param
  441. * @return
  442. *
  443. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  444. * @version february 2006, dokeos 1.8
  445. */
  446. function store_forumcategory($values) {
  447. global $table_categories;
  448. global $_course;
  449. global $_user;
  450. // find the max cat_order. The new forum category is added at the end => max cat_order + &
  451. $sql="SELECT MAX(cat_order) as sort_max FROM ".Database::escape_string($table_categories);
  452. $result=api_sql_query($sql,__FILE__,__LINE__);
  453. $row=Database::fetch_array($result);
  454. $new_max=$row['sort_max']+1;
  455. $clean_cat_title=Database::escape_string(Security::remove_XSS($values['forum_category_title']));
  456. if (isset($values['forum_category_id'])) { // storing an edit
  457. $sql="UPDATE ".$table_categories." SET cat_title='".$clean_cat_title."', cat_comment='".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_category_comment'])),COURSEMANAGER))."' WHERE cat_id='".Database::escape_string($values['forum_category_id'])."'";
  458. api_sql_query($sql,__FILE__,__LINE__);
  459. $last_id=Database::get_last_insert_id();
  460. api_item_property_update($_course, TOOL_FORUM_CATEGORY, $values['forum_category_id'],"ForumCategoryAdded", api_get_user_id());
  461. $return_message=get_lang('ForumCategoryEdited');
  462. } else {
  463. $sql="INSERT INTO ".$table_categories." (cat_title, cat_comment, cat_order) VALUES ('".$clean_cat_title."','".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_category_comment'])),COURSEMANAGER))."','".Database::escape_string($new_max)."')";
  464. api_sql_query($sql,__FILE__,__LINE__);
  465. $last_id=Database::get_last_insert_id();
  466. api_item_property_update($_course, TOOL_FORUM_CATEGORY, $last_id,"ForumCategoryAdded", api_get_user_id());
  467. $return_message=get_lang('ForumCategoryAdded');
  468. }
  469. Display :: display_confirmation_message($return_message);
  470. }
  471. /**
  472. * This function stores the forum in the database. The new forum is added to the end.
  473. *
  474. * @param
  475. * @return
  476. *
  477. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  478. * @version february 2006, dokeos 1.8
  479. */
  480. function store_forum($values) {
  481. global $_course;
  482. global $_user;
  483. $table_forums = Database::get_course_table(TABLE_FORUM);
  484. // find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
  485. if (is_null($values['forum_category'])) {
  486. $new_max=null;
  487. } else {
  488. $sql="SELECT MAX(forum_order) as sort_max FROM ".$table_forums." WHERE forum_category=".Database::escape_string($values['forum_category']);
  489. $result=api_sql_query($sql,__FILE__,__LINE__);
  490. $row=Database::fetch_array($result);
  491. $new_max=$row['sort_max']+1;
  492. }
  493. $session_id = isset($_SESSION['id_session']) ? $_SESSION['id_session'] : 0;
  494. $clean_title=Database::escape_string(Security::remove_XSS($values['forum_title']));
  495. // forum images
  496. $image_moved=false;
  497. if (!empty($_FILES['picture']['name'])) {
  498. $upload_ok = process_uploaded_file($_FILES['picture']);
  499. $has_attachment=true;
  500. } else {
  501. $image_moved=true;
  502. }
  503. // remove existing picture if asked
  504. if (!empty($_POST['remove_picture'])) {
  505. delete_forum_image($values['forum_id']);
  506. }
  507. if (isset($upload_ok)) {
  508. if ($has_attachment) {
  509. $courseDir = $_course['path'].'/upload/forum/images';
  510. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  511. $updir = $sys_course_path.$courseDir;
  512. // Try to add an extension to the file if it hasn't one
  513. $new_file_name = add_ext_on_mime(Database::escape_string($_FILES['picture']['name']), $_FILES['picture']['type']);
  514. // user's file name
  515. $file_name =$_FILES['picture']['name'];
  516. if (!filter_extension($new_file_name)) {
  517. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  518. $image_moved=false;
  519. } else {
  520. $file_extension = explode('.', $_FILES['picture']['name']);
  521. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  522. $new_file_name = uniqid('').'.'.$file_extension;
  523. $new_path=$updir.'/'.$new_file_name;
  524. $result= @move_uploaded_file($_FILES['picture']['tmp_name'], $new_path);
  525. // Storing the attachments if any
  526. if ($result) {
  527. $image_moved=true;
  528. }
  529. }
  530. }
  531. }
  532. if (isset($values['forum_id'])) {
  533. $sql_image=isset($sql_image)?$sql_image:'';
  534. $new_file_name=isset($new_file_name) ? $new_file_name:'';
  535. if ($image_moved) {
  536. if(empty($_FILES['picture']['name'])){
  537. $sql_image=" ";
  538. } else {
  539. $sql_image=" forum_image='".Database::escape_string($new_file_name)."', ";
  540. delete_forum_image($values['forum_id']);
  541. }
  542. }
  543. // storing an edit
  544. $sql="UPDATE ".$table_forums." SET
  545. forum_title='".$clean_title."',
  546. ".$sql_image."
  547. forum_comment='".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_comment'])),COURSEMANAGER))."',
  548. forum_category='".Database::escape_string($values['forum_category'])."',
  549. allow_anonymous='".Database::escape_string(isset($values['allow_anonymous_group']['allow_anonymous'])?$values['allow_anonymous_group']['allow_anonymous']:null)."',
  550. allow_edit='".Database::escape_string($values['students_can_edit_group']['students_can_edit'])."',
  551. approval_direct_post='".Database::escape_string(isset($values['approval_direct_group']['approval_direct'])?$values['approval_direct_group']['approval_direct']:null)."',
  552. allow_attachments='".Database::escape_string(isset($values['allow_attachments_group']['allow_attachments'])?$values['allow_attachments_group']['allow_attachments']:null)."',
  553. allow_new_threads='".Database::escape_string($values['allow_new_threads_group']['allow_new_threads'])."',
  554. forum_group_public_private='".Database::escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."',
  555. default_view='".Database::escape_string($values['default_view_type_group']['default_view_type'])."',
  556. forum_of_group='".Database::escape_string($values['group_forum'])."'
  557. WHERE forum_id='".Database::escape_string($values['forum_id'])."'";
  558. api_sql_query($sql,__FILE__,__LINE__);
  559. $return_message=get_lang('ForumEdited');
  560. } else {
  561. $sql_image='';
  562. if ($image_moved) {
  563. $new_file_name=isset($new_file_name)?$new_file_name:'';
  564. $sql_image="'".$new_file_name."', ";
  565. }
  566. $b=$values['forum_comment'];
  567. $sql="INSERT INTO ".$table_forums."
  568. (forum_title, forum_image, 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)
  569. VALUES ('".Security::remove_XSS($clean_title)."',
  570. ".$sql_image."
  571. '".Database::escape_string(isset($values['forum_comment'])?Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_comment'])),COURSEMANAGER):null)."',
  572. '".Database::escape_string(isset($values['forum_category'])?$values['forum_category']:null)."',
  573. '".Database::escape_string(isset($values['allow_anonymous_group']['allow_anonymous'])?$values['allow_anonymous_group']['allow_anonymous']:null)."',
  574. '".Database::escape_string(isset($values['students_can_edit_group']['students_can_edit'])?$values['students_can_edit_group']['students_can_edit']:null)."',
  575. '".Database::escape_string(isset($values['approval_direct_group']['approval_direct'])?$values['approval_direct_group']['approval_direct']:null)."',
  576. '".Database::escape_string(isset($values['allow_attachments_group']['allow_attachments'])?$values['allow_attachments_group']['allow_attachments']:null)."',
  577. '".Database::escape_string(isset($values['allow_new_threads_group']['allow_new_threads'])?$values['allow_new_threads_group']['allow_new_threads']:null)."',
  578. '".Database::escape_string(isset($values['default_view_type_group']['default_view_type'])?$values['default_view_type_group']['default_view_type']:null)."',
  579. '".Database::escape_string(isset($values['group_forum'])?$values['group_forum']:null)."',
  580. '".Database::escape_string(isset($values['public_private_group_forum_group']['public_private_group_forum'])?$values['public_private_group_forum_group']['public_private_group_forum']:null)."',
  581. '".Database::escape_string(isset($new_max)?$new_max:null)."',
  582. ".intval($session_id).")";
  583. api_sql_query($sql,__FILE__,__LINE__);
  584. $last_id=Database::get_last_insert_id();
  585. api_item_property_update($_course, TOOL_FORUM, $last_id,"ForumCategoryAdded", api_get_user_id());
  586. $return_message=get_lang('ForumAdded');
  587. }
  588. return $return_message;
  589. }
  590. /**
  591. * This function deletes a forum or a forum category
  592. * This function currently does not delete the forums inside the category, nor the threads and replies inside these forums.
  593. * For the moment this is the easiest method and it has the advantage that it allows to recover fora that were acidently deleted
  594. * when the forum category got deleted.
  595. *
  596. * @param $content = what we are deleting (a forum or a forum category)
  597. * @param $id The id of the forum category that has to be deleted.
  598. *
  599. * @todo write the code for the cascading deletion of the forums inside a forum category and also the threads and replies inside these forums
  600. * @todo config setting for recovery or not (see also the documents tool: real delete or not).
  601. *
  602. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  603. * @version february 2006, dokeos 1.8
  604. */
  605. function delete_forum_forumcategory_thread($content, $id) {
  606. global $_course;
  607. $table_forums = Database::get_course_table(TABLE_FORUM);
  608. $table_forums_post = Database::get_course_table(TABLE_FORUM_POST);
  609. $table_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  610. // delete all attachment file about this tread id
  611. $sql = "SELECT post_id FROM $table_forums_post WHERE thread_id = '".(int)$id."' ";
  612. $res = api_sql_query($sql,__FILE__,__LINE__);
  613. while ($poster_id = Database::fetch_row($res)) {
  614. delete_attachment($poster_id[0]);
  615. }
  616. if ($content=='forumcategory') {
  617. $tool_constant=TOOL_FORUM_CATEGORY;
  618. $return_message=get_lang('ForumCategoryDeleted');
  619. if (!empty($forum_list)){
  620. $sql="SELECT forum_id FROM ". $table_forums . "WHERE forum_category='".$id."'";
  621. $result = api_sql_query($sql, __FILE__, __LINE__);
  622. $row = Database::fetch_array($result);
  623. foreach ($row as $arr_forum) {
  624. $forum_id = $arr_forum['forum_id'];
  625. api_item_property_update($_course,'forum',$forum_id,'delete',api_get_user_id());
  626. }
  627. }
  628. }
  629. if ($content=='forum') {
  630. $tool_constant=TOOL_FORUM;
  631. $return_message=get_lang('ForumDeleted');
  632. if (!empty($number_threads)){
  633. $sql="SELECT thread_id FROM". $table_forum_thread . "WHERE forum_id='".$id."'";
  634. $result = api_sql_query($sql, __FILE__, __LINE__);
  635. $row = Database::fetch_array($result);
  636. foreach ($row as $arr_forum) {
  637. $forum_id = $arr_forum['thread_id'];
  638. api_item_property_update($_course,'forum_thread',$forum_id,'delete',api_get_user_id());
  639. }
  640. }
  641. }
  642. if ($content=='thread') {
  643. $tool_constant=TOOL_FORUM_THREAD;
  644. $return_message=get_lang('ThreadDeleted');
  645. }
  646. 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;
  647. return $return_message;
  648. }
  649. /**
  650. * This function deletes a forum post. This separate function is needed because forum posts do not appear in the item_property table (yet)
  651. * and because deleting a post also has consequence on the posts that have this post as parent_id (they are also deleted).
  652. * an alternative would be to store the posts also in item_property and mark this post as deleted (visibility = 2).
  653. * We also have to decrease the number of replies in the thread table
  654. *
  655. * @param $post_id the id of the post that will be deleted
  656. *
  657. * @todo write recursive function that deletes all the posts that have this message as parent
  658. *
  659. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  660. * @version february 2006, dokeos 1.8
  661. */
  662. function delete_post($post_id) {
  663. global $table_posts;
  664. global $table_threads;
  665. $sql="DELETE FROM $table_posts WHERE post_id='".Database::escape_string($post_id)."'"; // note: this has to be a recursive function that deletes all of the posts in this block.
  666. api_sql_query($sql,__FILE__,__LINE__);
  667. //delete attachment file about this post id
  668. delete_attachment($post_id);
  669. $last_post_of_thread=check_if_last_post_of_thread(strval(intval($_GET['thread'])));
  670. if (is_array($last_post_of_thread)) {
  671. // Decreasing the number of replies for this thread and also changing the last post information
  672. $sql="UPDATE $table_threads SET thread_replies=thread_replies-1,
  673. thread_last_post='".Database::escape_string($last_post_of_thread['post_id'])."',
  674. thread_date='".Database::escape_string($last_post_of_thread['post_date'])."'
  675. WHERE thread_id='".Database::escape_string($_GET['thread'])."'";
  676. api_sql_query($sql,__FILE__,__LINE__);
  677. return 'PostDeleted';
  678. }
  679. if ($last_post_of_thread==false) {
  680. // we deleted the very single post of the thread so we need to delete the entry in the thread table also.
  681. $sql="DELETE FROM $table_threads WHERE thread_id='".Database::escape_string($_GET['thread'])."'";
  682. api_sql_query($sql,__FILE__,__LINE__);
  683. return 'PostDeletedSpecial';
  684. }
  685. }
  686. /**
  687. * This function gets the all information of the last (=most recent) post of the thread
  688. * This can be done by sorting the posts that have the field thread_id=$thread_id and sort them by post_date
  689. *
  690. * @param $thread_id the id of the thread we want to know the last post of.
  691. * @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
  692. *
  693. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  694. * @version february 2006, dokeos 1.8
  695. */
  696. function check_if_last_post_of_thread($thread_id) {
  697. global $table_posts;
  698. $sql="SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($thread_id)."' ORDER BY post_date DESC";
  699. $result=api_sql_query($sql,__FILE__,__LINE__);
  700. if ( Database::num_rows($result)>0 ) {
  701. $row=Database::fetch_array($result);
  702. return $row;
  703. } else {
  704. return false;
  705. }
  706. }
  707. /**
  708. * This function takes care of the display of the visibility icon
  709. *
  710. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  711. * @param $id the id of the content we want to make invisible
  712. * @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible)
  713. * @return
  714. *
  715. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  716. * @version february 2006, dokeos 1.8
  717. */
  718. function display_visible_invisible_icon($content, $id, $current_visibility_status, $additional_url_parameters='') {
  719. global $origin;
  720. $gradebook=Security::remove_XSS($_GET['gradebook']);
  721. $id = Security::remove_XSS($id);
  722. if ($current_visibility_status=='1') {
  723. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  724. if (is_array($additional_url_parameters)) {
  725. foreach ($additional_url_parameters as $key=>$value) {
  726. echo $key.'='.$value.'&amp;';
  727. }
  728. }
  729. echo 'action=invisible&amp;content='.$content.'&amp;id='.$id.'&gradebook='.$gradebook.'&amp;origin='.$origin.'">'.icon('../img/visible.gif',get_lang('MakeInvisible')).'</a>';
  730. }
  731. if ($current_visibility_status=='0') {
  732. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  733. if (is_array($additional_url_parameters)) {
  734. foreach ($additional_url_parameters as $key=>$value) {
  735. echo $key.'='.$value.'&amp;';
  736. }
  737. }
  738. echo 'action=visible&amp;content='.$content.'&amp;id='.$id.'&gradebook='.$gradebook.'&amp;origin='.$origin.'">'.icon('../img/invisible.gif',get_lang('MakeVisible')).'</a>';
  739. }
  740. }
  741. /**
  742. * This function takes care of the display of the lock icon
  743. *
  744. * @param $content what is it that we want to (un)lock: forum category, forum, thread, post
  745. * @param $id the id of the content we want to (un)lock
  746. * @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible)
  747. * @return
  748. *
  749. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  750. * @version february 2006, dokeos 1.8
  751. */
  752. function display_lock_unlock_icon($content, $id, $current_lock_status, $additional_url_parameters='')
  753. {
  754. $id = Security::remove_XSS($id);
  755. if ($current_lock_status=='1')
  756. {
  757. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  758. if (is_array($additional_url_parameters))
  759. {
  760. foreach ($additional_url_parameters as $key=>$value)
  761. {
  762. echo $key.'='.$value.'&amp;';
  763. }
  764. }
  765. echo 'action=unlock&amp;content='.$content.'&amp;id='.$id.'">'.icon('../img/lock.gif',get_lang('Unlock')).'</a>';
  766. }
  767. if ($current_lock_status=='0')
  768. {
  769. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  770. if (is_array($additional_url_parameters))
  771. {
  772. foreach ($additional_url_parameters as $key=>$value)
  773. {
  774. echo $key.'='.$value.'&amp;';
  775. }
  776. }
  777. echo 'action=lock&amp;content='.$content.'&amp;id='.$id.'">'.icon('../img/unlock.gif',get_lang('Lock')).'</a>';
  778. }
  779. }
  780. /**
  781. * This function takes care of the display of the up and down icon
  782. *
  783. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  784. * @param $id is the id of the item we want to display the icons for
  785. * @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)
  786. * The key of this $list array is the id of the item.
  787. *
  788. * @return
  789. *
  790. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  791. * @version february 2006, dokeos 1.8
  792. */
  793. function display_up_down_icon($content, $id, $list) {
  794. $id = strval(intval($id));
  795. $total_items=count($list);
  796. $position = 0;
  797. $internal_counter=0;
  798. if(is_array($list)) {
  799. foreach ($list as $key=>$listitem) {
  800. $internal_counter++;
  801. if ($id==$key) {
  802. $position=$internal_counter;
  803. }
  804. }
  805. }
  806. if ($position>1) {
  807. $return_value='<a href="'.api_get_self().'?'.api_get_cidreq().'&action=move&amp;direction=up&amp;content='.$content.'&amp;forumcategory='.Security::remove_XSS($_GET['forumcategory']).'&amp;id='.$id.'" title="'.get_lang('MoveUp').'"><img src="../img/up.gif" /></a>';
  808. } else {
  809. $return_value='<img src="../img/up_na.gif" />';
  810. }
  811. if ($position<$total_items) {
  812. $return_value.='<a href="'.api_get_self().'?'.api_get_cidreq().'&action=move&amp;direction=down&amp;content='.$content.'&amp;forumcategory='.Security::remove_XSS($_GET['forumcategory']).'&amp;id='.$id.'" title="'.get_lang('MoveDown').'" ><img src="../img/down.gif" /></a>';
  813. } else {
  814. $return_value.='<img src="../img/down_na.gif" />';
  815. }
  816. echo $return_value;
  817. }
  818. /**
  819. * This function changes the visibility in the database (item_property)
  820. *
  821. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  822. * @param $id the id of the content we want to make invisible
  823. * @param $target_visibility what is the current status of the visibility (0 = invisible, 1 = visible)
  824. *
  825. * @todo change the get parameter so that it matches the tool constants.
  826. * @todo check if api_item_property_update returns true or false => returnmessage depends on it.
  827. * @todo move to itemmanager
  828. *
  829. * @return
  830. *
  831. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  832. * @version february 2006, dokeos 1.8
  833. */
  834. function change_visibility($content, $id, $target_visibility) {
  835. global $_course;
  836. $constants=array('forumcategory'=>TOOL_FORUM_CATEGORY, 'forum'=>TOOL_FORUM, 'thread'=>TOOL_FORUM_THREAD);
  837. 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.
  838. if ($target_visibility=='visible') {
  839. handle_mail_cue($content, $id);
  840. }
  841. return get_lang('VisibilityChanged');
  842. }
  843. /**
  844. * This function changes the lock status in the database
  845. *
  846. * @param $content what is it that we want to (un)lock: forum category, forum, thread, post
  847. * @param $id the id of the content we want to (un)lock
  848. * @param $action do we lock (=>locked value in db = 1) or unlock (=> locked value in db = 0)
  849. * @return string, language variable
  850. *
  851. * @todo move to itemmanager
  852. *
  853. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  854. * @version february 2006, dokeos 1.8
  855. */
  856. function change_lock_status($content, $id, $action) {
  857. global $table_categories;
  858. global $table_forums;
  859. global $table_threads;
  860. global $table_posts;
  861. // Determine the relevant table
  862. if ($content=='forumcategory') {
  863. $table=$table_categories;
  864. $id_field='cat_id';
  865. } elseif ($content=='forum') {
  866. $table=$table_forums;
  867. $id_field='forum_id';
  868. } elseif ($content=='thread') {
  869. $table=$table_threads;
  870. $id_field='thread_id';
  871. } else {
  872. return get_lang('Error');
  873. }
  874. // Determine what we are doing => defines the value for the database and the return message
  875. if ($action=='lock') {
  876. $db_locked=1;
  877. $return_message=get_lang('Locked');
  878. } elseif ($action=='unlock') {
  879. $db_locked=0;
  880. $return_message=get_lang('Unlocked');
  881. } else {
  882. return get_lang('Error');
  883. }
  884. // Doing the change in the database
  885. $sql="UPDATE $table SET locked='".Database::escape_string($db_locked)."' WHERE $id_field='".Database::escape_string($id)."'";
  886. if (api_sql_query($sql,__FILE__,__LINE__)) {
  887. return $return_message;
  888. } else {
  889. return get_lang('Error');
  890. }
  891. }
  892. /**
  893. * This function moves a forum or a forum category up or down
  894. *
  895. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  896. * @param $direction do we want to move it up or down.
  897. * @param $id the id of the content we want to make invisible
  898. * @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
  899. * the item_property table but does have one in the forum table.
  900. * @return
  901. *
  902. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  903. * @version february 2006, dokeos 1.8
  904. */
  905. function move_up_down($content, $direction, $id) {
  906. global $table_categories;
  907. global $table_forums;
  908. global $table_item_property;
  909. // Determine which field holds the sort order
  910. if ($content=='forumcategory') {
  911. $table=$table_categories;
  912. $sort_column='cat_order';
  913. $id_column='cat_id';
  914. $sort_column='cat_order';
  915. } elseif ($content=='forum') {
  916. $table=$table_forums;
  917. $sort_column='forum_order';
  918. $id_column='forum_id';
  919. $sort_column='forum_order';
  920. // we also need the forum_category of this forum
  921. $sql="SELECT forum_category FROM $table_forums WHERE forum_id=".Database::escape_string($id);
  922. $result=api_sql_query($sql,__FILE__,__LINE__);
  923. $row=Database::fetch_array($result);
  924. $forum_category=$row['forum_category'];
  925. } else {
  926. return get_lang('Error');
  927. }
  928. // determine if need to sort ascending or descending
  929. if ($direction=='down') {
  930. $sort_direction='ASC';
  931. } elseif ($direction=='up') {
  932. $sort_direction='DESC';
  933. } else {
  934. return get_lang('Error');
  935. }
  936. // The SQL statement
  937. if ($content=='forumcategory') {
  938. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  939. WHERE forum_categories.cat_id=item_properties.ref
  940. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  941. ORDER BY forum_categories.cat_order $sort_direction";
  942. }
  943. if ($content=='forum') {
  944. $sql="SELECT * FROM".$table." WHERE forum_category='".Database::escape_string($forum_category)."' ORDER BY forum_order $sort_direction";
  945. }
  946. // echo $sql.'<br />';
  947. // finding the items that need to be switched
  948. $result=api_sql_query($sql,__FILE__,__LINE__);
  949. $found=false;
  950. while ($row=Database::fetch_array($result)) {
  951. //echo $row[$id_column].'-';
  952. if ($found==true) {
  953. $next_id=$row[$id_column];
  954. $next_sort=$row[$sort_column];
  955. $found=false;
  956. }
  957. if($id==$row[$id_column]) {
  958. $this_id=$id;
  959. $this_sort=$row[$sort_column];
  960. $found=true;
  961. }
  962. }
  963. // Committing the switch
  964. // we do an extra check if we do not have illegal values. If your remove this if statment you will
  965. // be able to mess with the sorting by refreshing the page over and over again.
  966. if ($this_sort<>'' && $next_sort<>'' && $next_id<>'' && $this_id<>'') {
  967. $sql_update1="UPDATE $table SET $sort_column='".Database::escape_string($this_sort)."' WHERE $id_column='".Database::escape_string($next_id)."'";
  968. $sql_update2="UPDATE $table SET $sort_column='".Database::escape_string($next_sort)."' WHERE $id_column='".Database::escape_string($this_id)."'";
  969. api_sql_query($sql_update1,__FILE__,__LINE__);
  970. api_sql_query($sql_update2,__FILE__,__LINE__);
  971. }
  972. return get_lang(ucfirst($content).'Moved');
  973. }
  974. /**
  975. * This function returns a piece of html code that make the links grey (=invisible for the student)
  976. *
  977. * @param boolean 0/1: 0 = invisible, 1 = visible
  978. * @return string
  979. *
  980. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  981. * @version february 2006, dokeos 1.8
  982. */
  983. function class_visible_invisible($current_visibility_status) {
  984. if ($current_visibility_status=='0') {
  985. return "class='invisible'";
  986. }
  987. }
  988. /**
  989. * Retrieve all the information off the forum categories (or one specific) for the current course.
  990. * The categories are sorted according to their sorting order (cat_order
  991. *
  992. * @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.
  993. * @return an array containing all the information about all the forum categories
  994. *
  995. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  996. * @version february 2006, dokeos 1.8
  997. */
  998. function get_forum_categories($id='') {
  999. $table_categories = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  1000. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1001. $forum_categories_list=array();
  1002. if ($id=='') {
  1003. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  1004. WHERE forum_categories.cat_id=item_properties.ref
  1005. AND item_properties.visibility=1
  1006. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1007. ORDER BY forum_categories.cat_order ASC";
  1008. if (is_allowed_to_edit()) {
  1009. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  1010. WHERE forum_categories.cat_id=item_properties.ref
  1011. AND item_properties.visibility<>2
  1012. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1013. ORDER BY forum_categories.cat_order ASC";
  1014. }
  1015. } else {
  1016. $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties
  1017. WHERE forum_categories.cat_id=item_properties.ref
  1018. AND item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1019. AND forum_categories.cat_id='".Database::escape_string($id)."'
  1020. ORDER BY forum_categories.cat_order ASC";
  1021. }
  1022. $result=api_sql_query($sql,__FILE__,__LINE__);
  1023. while ($row=Database::fetch_array($result)) {
  1024. if ($id=='') {
  1025. $forum_categories_list[$row['cat_id']]=$row;
  1026. } else {
  1027. $forum_categories_list=$row;
  1028. }
  1029. }
  1030. return $forum_categories_list;
  1031. }
  1032. /**
  1033. * This function retrieves all the fora in a given forum category
  1034. *
  1035. * @param integer $cat_id the id of the forum category
  1036. * @return an array containing all the information about the forums (regardless of their category)
  1037. *
  1038. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1039. * @version february 2006, dokeos 1.8
  1040. */
  1041. function get_forums_in_category($cat_id)
  1042. {
  1043. global $table_forums;
  1044. global $table_item_property;
  1045. $forum_list=array();
  1046. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  1047. WHERE forum.forum_category='".Database::escape_string($cat_id)."'
  1048. AND forum.forum_id=item_properties.ref
  1049. AND item_properties.visibility=1
  1050. AND item_properties.tool='".TOOL_FORUM."'
  1051. ORDER BY forum.forum_order ASC";
  1052. if (is_allowed_to_edit()) {
  1053. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  1054. WHERE forum.forum_category='".Database::escape_string($cat_id)."'
  1055. AND forum.forum_id=item_properties.ref
  1056. AND item_properties.visibility<>2
  1057. AND item_properties.tool='".TOOL_FORUM."'
  1058. ORDER BY forum_order ASC";
  1059. }
  1060. $result=api_sql_query($sql,__FILE__,__LINE__);
  1061. while ($row=Database::fetch_array($result)) {
  1062. $forum_list[$row['forum_id']]=$row;
  1063. }
  1064. return $forum_list;
  1065. }
  1066. /**
  1067. * Retrieve all the forums (regardless of their category) or of only one. The forums are sorted according to the forum_order.
  1068. * Since it does not take the forum category into account there probably will be two or more forums that have forum_order=1, ...
  1069. *
  1070. * @return an array containing all the information about the forums (regardless of their category)
  1071. * @todo check $sql4 because this one really looks fishy.
  1072. *
  1073. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1074. * @version february 2006, dokeos 1.8
  1075. */
  1076. function get_forums($id='') {
  1077. global $table_forums;
  1078. global $table_threads;
  1079. global $table_posts;
  1080. global $table_item_property;
  1081. global $table_users;
  1082. // **************** GETTING ALL THE FORUMS ************************* //
  1083. $session_condition = isset($_SESSION['id_session']) ? 'AND forum.session_id IN (0,'.intval($_SESSION['id_session']).')' : '';
  1084. $forum_list = array();
  1085. if ($id=='') {
  1086. //-------------- Student -----------------//
  1087. // select all the forum information of all forums (that are visible to students)
  1088. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  1089. WHERE forum.forum_id=item_properties.ref
  1090. AND item_properties.visibility=1
  1091. AND item_properties.tool='".TOOL_FORUM."'
  1092. $session_condition
  1093. ORDER BY forum.forum_order ASC";
  1094. // select the number of threads of the forums (only the threads that are visible)
  1095. $sql2="SELECT count(*) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties
  1096. WHERE threads.thread_id=item_properties.ref
  1097. AND item_properties.visibility=1
  1098. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  1099. GROUP BY threads.forum_id";
  1100. // select the number of posts of the forum (post that are visible and that are in a thread that is visible)
  1101. $sql3="SELECT count(*) AS number_of_posts, posts.forum_id FROM $table_posts posts, $table_threads threads, ".$table_item_property." item_properties
  1102. WHERE posts.visible=1
  1103. AND posts.thread_id=threads.thread_id
  1104. AND threads.thread_id=item_properties.ref
  1105. AND item_properties.visibility=1
  1106. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  1107. GROUP BY threads.forum_id";
  1108. //-------------- Course Admin -----------------//
  1109. if (is_allowed_to_edit()) {
  1110. // select all the forum information of all forums (that are not deleted)
  1111. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  1112. WHERE forum.forum_id=item_properties.ref
  1113. AND item_properties.visibility<>2
  1114. AND item_properties.tool='".TOOL_FORUM."'
  1115. $session_condition
  1116. ORDER BY forum_order ASC";
  1117. //echo $sql.'<hr>';
  1118. // select the number of threads of the forums (only the threads that are not deleted)
  1119. $sql2="SELECT count(*) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties
  1120. WHERE threads.thread_id=item_properties.ref
  1121. AND item_properties.visibility<>2
  1122. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  1123. GROUP BY threads.forum_id";
  1124. //echo $sql2.'<hr>';
  1125. // select the number of posts of the forum
  1126. $sql3="SELECT count(*) AS number_of_posts, posts.forum_id FROM $table_posts posts, $table_threads threads, ".$table_item_property." item_properties
  1127. WHERE posts.thread_id=threads.thread_id
  1128. AND threads.thread_id=item_properties.ref
  1129. AND item_properties.visibility=1
  1130. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  1131. GROUP BY threads.forum_id";
  1132. //echo $sql3.'<hr>';
  1133. }
  1134. }
  1135. // **************** GETTING ONE SPECIFIC FORUM ************************* //
  1136. // 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
  1137. // 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
  1138. // when editing a forum (and for the moment it is the only place where we use this part of the function)
  1139. else {
  1140. // select all the forum information of the given forum (that is not deleted)
  1141. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  1142. WHERE forum.forum_id=item_properties.ref
  1143. AND forum_id='".Database::escape_string($id)."'
  1144. AND item_properties.visibility<>2
  1145. AND item_properties.tool='".TOOL_FORUM."'
  1146. $session_condition
  1147. ORDER BY forum_order ASC";
  1148. // select the number of threads of the forum
  1149. $sql2="SELECT count(*) AS number_of_threads, forum_id FROM $table_threads WHERE forum_id=".Database::escape_string($id)." GROUP BY forum_id";
  1150. // select the number of posts of the forum
  1151. $sql3="SELECT count(*) AS number_of_posts, forum_id FROM $table_posts WHERE forum_id=".Database::escape_string($id)." GROUP BY forum_id";
  1152. // select the last post and the poster (note: this is probably no longer needed)
  1153. $sql4="SELECT post.post_id, post.forum_id, post.poster_id, post.poster_name, post.post_date, users.lastname, users.firstname
  1154. FROM $table_posts post, $table_users users
  1155. WHERE forum_id=".Database::escape_string($id)."
  1156. AND post.poster_id=users.user_id
  1157. GROUP BY post.forum_id
  1158. ORDER BY post.post_id ASC";
  1159. }
  1160. // handling all the forum information
  1161. $result=api_sql_query($sql,__FILE__,__LINE__);
  1162. while ($row=Database::fetch_array($result)) {
  1163. if ($id=='') {
  1164. $forum_list[$row['forum_id']]=$row;
  1165. } else {
  1166. $forum_list=$row;
  1167. }
  1168. }
  1169. // handling the threadcount information
  1170. $result2=api_sql_query($sql2,__FILE__,__LINE__);
  1171. while ($row2=Database::fetch_array($result2)) {
  1172. if ($id=='') {
  1173. $forum_list[$row2['forum_id']]['number_of_threads']=$row2['number_of_threads'];
  1174. } else {
  1175. $forum_list['number_of_threads']=$row2['number_of_threads'];;
  1176. }
  1177. }
  1178. // handling the postcount information
  1179. $result3=api_sql_query($sql3,__FILE__,__LINE__);
  1180. while ($row3=Database::fetch_array($result3)) {
  1181. if ($id=='') {
  1182. if (array_key_exists($row3['forum_id'],$forum_list)) {// this is needed because sql3 takes also the deleted forums into account
  1183. $forum_list[$row3['forum_id']]['number_of_posts']=$row3['number_of_posts'];
  1184. }
  1185. } else {
  1186. $forum_list['number_of_posts']=$row3['number_of_posts'];
  1187. }
  1188. }
  1189. // finding the last post information (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)
  1190. if ($id=='') {
  1191. if(is_array($forum_list)) {
  1192. foreach ($forum_list as $key=>$value) {
  1193. $last_post_info_of_forum=get_last_post_information($key,is_allowed_to_edit());
  1194. $forum_list[$key]['last_post_id']=$last_post_info_of_forum['last_post_id'];
  1195. $forum_list[$key]['last_poster_id']=$last_post_info_of_forum['last_poster_id'];
  1196. $forum_list[$key]['last_post_date']=$last_post_info_of_forum['last_post_date'];
  1197. $forum_list[$key]['last_poster_name']=$last_post_info_of_forum['last_poster_name'];
  1198. $forum_list[$key]['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname'];
  1199. $forum_list[$key]['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname'];
  1200. }
  1201. } else {
  1202. $forum_list = array();
  1203. }
  1204. } else {
  1205. $last_post_info_of_forum=get_last_post_information($id,is_allowed_to_edit());
  1206. $forum_list['last_post_id']=$last_post_info_of_forum['last_post_id'];
  1207. $forum_list['last_poster_id']=$last_post_info_of_forum['last_poster_id'];
  1208. $forum_list['last_post_date']=$last_post_info_of_forum['last_post_date'];
  1209. $forum_list['last_poster_name']=$last_post_info_of_forum['last_poster_name'];
  1210. $forum_list['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname'];
  1211. $forum_list['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname'];
  1212. }
  1213. return $forum_list;
  1214. }
  1215. /**
  1216. * This functions gets all the last post information of a certain forum
  1217. *
  1218. * @param $forum_id the id of the forum we want to know the last post information of.
  1219. * @param $show_invisibles
  1220. * @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)
  1221. *
  1222. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1223. * @version february 2006, dokeos 1.8
  1224. */
  1225. function get_last_post_information($forum_id, $show_invisibles=false) {
  1226. global $table_forums;
  1227. global $table_threads;
  1228. global $table_posts;
  1229. global $table_item_property;
  1230. global $table_users;
  1231. $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
  1232. FROM $table_posts post, $table_users users, $table_item_property thread_properties, $table_item_property forum_properties
  1233. WHERE post.forum_id=".Database::escape_string($forum_id)."
  1234. AND post.poster_id=users.user_id
  1235. AND post.thread_id=thread_properties.ref
  1236. AND thread_properties.tool='".TOOL_FORUM_THREAD."'
  1237. AND post.forum_id=forum_properties.ref
  1238. AND forum_properties.tool='".TOOL_FORUM."'
  1239. ORDER BY post.post_id DESC";
  1240. $result=api_sql_query($sql,__FILE__,__LINE__);
  1241. if ($show_invisibles==true) {
  1242. $row=Database::fetch_array($result);
  1243. $return_array['last_post_id']=$row['post_id'];
  1244. $return_array['last_poster_id']=$row['poster_id'];
  1245. $return_array['last_post_date']=$row['post_date'];
  1246. $return_array['last_poster_name']=$row['poster_name'];
  1247. $return_array['last_poster_lastname']=$row['lastname'];
  1248. $return_array['last_poster_firstname']=$row['firstname'];
  1249. return $return_array;
  1250. } else {
  1251. // 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)
  1252. while ($row=Database::fetch_array($result)) {
  1253. if ($row['visible']=='1' AND $row['thread_visibility']=='1' AND $row['forum_visibility']=='1') {
  1254. $return_array['last_post_id']=$row['post_id'];
  1255. $return_array['last_poster_id']=$row['poster_id'];
  1256. $return_array['last_post_date']=$row['post_date'];
  1257. $return_array['last_poster_name']=$row['poster_name'];
  1258. $return_array['last_poster_lastname']=$row['lastname'];
  1259. $return_array['last_poster_firstname']=$row['firstname'];
  1260. return $return_array;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. /**
  1266. * Retrieve all the threads of a given forum
  1267. *
  1268. * @param
  1269. * @return an array containing all the information about the threads
  1270. *
  1271. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1272. * @version february 2006, dokeos 1.8
  1273. */
  1274. function get_threads($forum_id) {
  1275. global $table_item_property;
  1276. global $table_threads;
  1277. global $table_posts;
  1278. global $table_users;
  1279. $thread_list=array();
  1280. // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement
  1281. // because we also have thread.* in it. This is because thread has a field locked and post also has the same field
  1282. // since we are merging these we would have the post.locked value but in fact we want the thread.locked value
  1283. // This is why it is added to the end of the field selection
  1284. $sql = "SELECT thread.*, item_properties.*, post.*, users.firstname, users.lastname, users.user_id,
  1285. 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
  1286. FROM $table_threads thread
  1287. INNER JOIN $table_item_property item_properties
  1288. ON thread.thread_id=item_properties.ref
  1289. AND item_properties.visibility='1'
  1290. AND item_properties.tool='".TABLE_FORUM_THREAD."'
  1291. LEFT JOIN $table_users users
  1292. ON thread.thread_poster_id=users.user_id
  1293. LEFT JOIN $table_posts post
  1294. ON thread.thread_last_post = post.post_id
  1295. LEFT JOIN $table_users last_poster_users
  1296. ON post.poster_id= last_poster_users.user_id
  1297. WHERE thread.forum_id='".Database::escape_string($forum_id)."'
  1298. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1299. if (is_allowed_to_edit()) {
  1300. // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement
  1301. // because we also have thread.* in it. This is because thread has a field locked and post also has the same field
  1302. // since we are merging these we would have the post.locked value but in fact we want the thread.locked value
  1303. // This is why it is added to the end of the field selection
  1304. $sql = "SELECT thread.*, item_properties.*, post.*, users.firstname, users.lastname, users.user_id,
  1305. 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
  1306. FROM $table_threads thread
  1307. INNER JOIN $table_item_property item_properties
  1308. ON thread.thread_id=item_properties.ref
  1309. AND item_properties.visibility<>2
  1310. AND item_properties.tool='".TABLE_FORUM_THREAD."'
  1311. LEFT JOIN $table_users users
  1312. ON thread.thread_poster_id=users.user_id
  1313. LEFT JOIN $table_posts post
  1314. ON thread.thread_last_post = post.post_id
  1315. LEFT JOIN $table_users last_poster_users
  1316. ON post.poster_id= last_poster_users.user_id
  1317. WHERE thread.forum_id='".Database::escape_string($forum_id)."'
  1318. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1319. }
  1320. $result=api_sql_query($sql, __FILE__, __LINE__);
  1321. while ( $row=Database::fetch_array($result,'ASSOC') ) {
  1322. $thread_list[]=$row;
  1323. }
  1324. return $thread_list;
  1325. }
  1326. /**
  1327. * Retrieve all posts of a given thread
  1328. *
  1329. * @return an array containing all the information about the posts of a given thread
  1330. *
  1331. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1332. * @version february 2006, dokeos 1.8
  1333. */
  1334. function get_posts($thread_id) {
  1335. global $table_posts;
  1336. global $table_users;
  1337. // note: change these SQL so that only the relevant fields of the user table are used
  1338. if (api_is_allowed_to_edit()) {
  1339. $sql = "SELECT * FROM $table_posts posts
  1340. LEFT JOIN $table_users users
  1341. ON posts.poster_id=users.user_id
  1342. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  1343. ORDER BY posts.post_id ASC";
  1344. } else {
  1345. // students can only se the posts that are approved (posts.visible='1')
  1346. $sql = "SELECT * FROM $table_posts posts
  1347. LEFT JOIN $table_users users
  1348. ON posts.poster_id=users.user_id
  1349. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  1350. AND posts.visible='1'
  1351. ORDER BY posts.post_id ASC";
  1352. }
  1353. $result=api_sql_query($sql, __FILE__, __LINE__);
  1354. while ($row=Database::fetch_array($result)) {
  1355. $post_list[]=$row;
  1356. }
  1357. return $post_list;
  1358. }
  1359. /**
  1360. * This function return the html syntax for the image
  1361. *
  1362. * @param $image_url The url of the image (absolute or relative)
  1363. * @param $alt The alt text (when the images cannot be displayed). http://www.w3.org/TR/html4/struct/objects.html#adef-alt
  1364. * @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
  1365. *
  1366. * @todo this is the same as the Display::xxx function, so it can be removed => all calls have to be changed also
  1367. *
  1368. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1369. * @version february 2006, dokeos 1.8
  1370. */
  1371. function icon($image_url,$alt='',$title='') {
  1372. if ($title=='') {
  1373. $title=$alt;
  1374. }
  1375. return '<img src="'.$image_url.'" alt="'.$alt.'" title="'.$title.'" />';
  1376. }
  1377. /**************************************************************************
  1378. NEW TOPIC FUNCTIONS
  1379. **************************************************************************/
  1380. /**
  1381. * This function retrieves all the information of a post
  1382. *
  1383. * @param $forum_id integer that indicates the forum
  1384. * @return array returns
  1385. *
  1386. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1387. * @version february 2006, dokeos 1.8
  1388. */
  1389. function get_post_information($post_id) {
  1390. global $table_posts;
  1391. global $table_users;
  1392. $sql="SELECT * FROM ".$table_posts."posts, ".$table_users." users WHERE posts.poster_id=users.user_id AND posts.post_id='".Database::escape_string($post_id)."'";
  1393. $result=api_sql_query($sql, __FILE__, __LINE__);
  1394. $row=Database::fetch_array($result);
  1395. return $row;
  1396. }
  1397. /**
  1398. * This function retrieves all the information of a thread
  1399. *
  1400. * @param $forum_id integer that indicates the forum
  1401. * @return array returns
  1402. *
  1403. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1404. * @version february 2006, dokeos 1.8
  1405. */
  1406. function get_thread_information($thread_id) {
  1407. global $table_threads;
  1408. global $table_item_property;
  1409. $sql="SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
  1410. WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
  1411. AND item_properties.ref='".Database::escape_string($thread_id)."'
  1412. AND threads.thread_id='".Database::escape_string($thread_id)."'";
  1413. $result=api_sql_query($sql, __FILE__, __LINE__);
  1414. $row=Database::fetch_array($result);
  1415. return $row;
  1416. }
  1417. /**
  1418. * This function retrieves forum thread users details
  1419. * @param int Thread ID
  1420. * @param string Course DB name (optional)
  1421. * @return array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  1422. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  1423. * @version octubre 2008, dokeos 1.8
  1424. */
  1425. function get_thread_users_details($thread_id, $db_name = null) {
  1426. $t_posts = Database :: get_course_table(TABLE_FORUM_POST, (empty($db_name)?null:$db_name));
  1427. $t_users = Database :: get_main_table(TABLE_MAIN_USER);
  1428. $t_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1429. $sql = "SELECT DISTINCT user.user_id, user.lastname, user.firstname, thread_id
  1430. FROM $t_posts , $t_users user, $t_course_user course_user
  1431. WHERE poster_id = user.user_id
  1432. AND user.user_id = course_user.user_id
  1433. AND thread_id = '".Database::escape_string($thread_id)."'
  1434. AND course_user.status NOT IN('1')
  1435. AND course_code = '".api_get_course_id()."'";
  1436. $result = api_sql_query($sql, __FILE__, __LINE__);
  1437. return $result;
  1438. }
  1439. /**
  1440. * This function retrieves forum thread users qualify
  1441. * @param int Thread ID
  1442. * @param string Course DB name (optional)
  1443. * @return array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  1444. * @author Jhon Hinojosa<jhon.hinojosa@dokeos.com>,
  1445. * @version octubre 2008, dokeos 1.8
  1446. */
  1447. function get_thread_users_qualify($thread_id, $db_name = null) {
  1448. $t_posts = Database :: get_course_table(TABLE_FORUM_POST, (empty($db_name)?null:$db_name));
  1449. $t_qualify = Database :: get_course_table(TABLE_FORUM_THREAD_QUALIFY, (empty($db_name)?null:$db_name));
  1450. $t_users = Database :: get_main_table(TABLE_MAIN_USER);
  1451. $t_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1452. $sql = "SELECT post.poster_id, user.lastname, user.firstname, post.thread_id,user.user_id,qualify.qualify
  1453. FROM $t_posts post,
  1454. $t_qualify qualify,
  1455. $t_users user,
  1456. $t_course_user course_user
  1457. WHERE
  1458. post.poster_id = user.user_id
  1459. AND post.poster_id = qualify.user_id
  1460. AND user.user_id = course_user.user_id
  1461. AND qualify.thread_id = '".Database::escape_string($thread_id)."'
  1462. AND course_user.status not in('1')
  1463. AND course_code = '".api_get_course_id()."'
  1464. GROUP BY post.poster_id ";
  1465. $result = api_sql_query($sql, __FILE__, __LINE__);
  1466. return $result;
  1467. }
  1468. /**
  1469. * This function retrieves forum thread users not qualify
  1470. * @param int Thread ID
  1471. * @param string Course DB name (optional)
  1472. * @return array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  1473. * @author Jhon Hinojosa<jhon.hinojosa@dokeos.com>,
  1474. * @version octubre 2008, dokeos 1.8
  1475. */
  1476. function get_thread_users_not_qualify($thread_id, $db_name = null) {
  1477. $t_posts = Database :: get_course_table(TABLE_FORUM_POST, (empty($db_name)?null:$db_name));
  1478. $t_qualify = Database :: get_course_table(TABLE_FORUM_THREAD_QUALIFY, (empty($db_name)?null:$db_name));
  1479. $t_users = Database :: get_main_table(TABLE_MAIN_USER);
  1480. $t_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1481. $sql1 = "select user_id FROM $t_qualify WHERE thread_id = '".$thread_id."'";
  1482. $result1 = api_sql_query($sql1,__FILE__,__LINE__);
  1483. $cad='';
  1484. while ($row=Database::fetch_array($result1)) {
  1485. $cad .= $row['user_id'].',';
  1486. }
  1487. if($cad=='') {
  1488. $cad='0';
  1489. } else {
  1490. $cad=substr($cad,0,strlen($cad)-1);
  1491. }
  1492. $sql = "SELECT DISTINCT user.user_id, user.lastname, user.firstname, post.thread_id
  1493. FROM $t_posts post, $t_users user,$t_course_user course_user
  1494. WHERE post.poster_id = user.user_id
  1495. AND user.user_id NOT IN (".$cad.")
  1496. AND user.user_id = course_user.user_id
  1497. AND post.thread_id = '".Database::escape_string($thread_id)."'
  1498. AND course_user.status not in('1')
  1499. AND course_code = '".api_get_course_id()."'";
  1500. $result = api_sql_query($sql, __FILE__, __LINE__);
  1501. return $result;
  1502. }
  1503. /**
  1504. * This function retrieves all the information of a given forum_id
  1505. *
  1506. * @param $forum_id integer that indicates the forum
  1507. * @return array returns
  1508. *
  1509. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1510. * @version february 2006, dokeos 1.8
  1511. *
  1512. * @deprecated this functionality is now moved to get_forums($forum_id)
  1513. */
  1514. function get_forum_information($forum_id) {
  1515. global $table_forums;
  1516. global $table_item_property;
  1517. $sql="SELECT * FROM ".$table_forums." forums, ".$table_item_property." item_properties
  1518. WHERE item_properties.tool='".TOOL_FORUM."'
  1519. AND item_properties.ref='".Database::escape_string($forum_id)."'
  1520. AND forums.forum_id='".Database::escape_string($forum_id)."'";
  1521. $result=api_sql_query($sql, __FILE__, __LINE__);
  1522. $row=Database::fetch_array($result);
  1523. $row['approval_direct_post'] = 0; // we can't anymore change this option, so it should always be activated
  1524. return $row;
  1525. }
  1526. /**
  1527. * This function retrieves all the information of a given forumcategory id
  1528. *
  1529. * @param $forum_id integer that indicates the forum
  1530. * @return array returns
  1531. *
  1532. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1533. * @version february 2006, dokeos 1.8
  1534. */
  1535. function get_forumcategory_information($cat_id) {
  1536. global $table_categories;
  1537. global $table_item_property;
  1538. $sql="SELECT * FROM ".$table_categories." forumcategories, ".$table_item_property." item_properties
  1539. WHERE item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1540. AND item_properties.ref='".Database::escape_string($cat_id)."'
  1541. AND forumcategories.cat_id='".Database::escape_string($cat_id)."'";
  1542. $result=api_sql_query($sql, __FILE__, __LINE__);
  1543. $row=Database::fetch_array($result);
  1544. return $row;
  1545. }
  1546. /**
  1547. * This function counts the number of forums inside a given category
  1548. *
  1549. * @param $cat_id the id of the forum category
  1550. * @todo an additional parameter that takes the visibility into account. For instance $countinvisible=0 would return the number
  1551. * of visible forums, $countinvisible=1 would return the number of visible and invisible forums
  1552. * @return int the number of forums inside the given category
  1553. *
  1554. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1555. * @version february 2006, dokeos 1.8
  1556. */
  1557. function count_number_of_forums_in_category($cat_id) {
  1558. global $table_forums;
  1559. $sql="SELECT count(*) AS number_of_forums FROM ".$table_forums." WHERE forum_category='".Database::escape_string($cat_id)."'";
  1560. $result=api_sql_query($sql, __FILE__, __LINE__);
  1561. $row=Database::fetch_array($result);
  1562. return $row['number_of_forums'];
  1563. }
  1564. /**
  1565. * This function stores a new thread. This is done through an entry in the forum_thread table AND
  1566. * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet))
  1567. *
  1568. * @param
  1569. * @return
  1570. *
  1571. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1572. * @version february 2006, dokeos 1.8
  1573. */
  1574. function store_thread($values) {
  1575. global $table_threads;
  1576. global $table_posts;
  1577. global $_user;
  1578. global $_course;
  1579. global $current_forum;
  1580. global $origin;
  1581. global $forum_table_attachment;
  1582. $gradebook=Security::remove_XSS($_GET['gradebook']);
  1583. $upload_ok=1;
  1584. $has_attachment=false;
  1585. if(!empty($_FILES['user_upload']['name'])) {
  1586. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  1587. $has_attachment=true;
  1588. }
  1589. if($upload_ok) {
  1590. $post_date=date('Y-m-d H:i:s');
  1591. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit()) {
  1592. $visible=0; // the post is not approved yet.
  1593. } else {
  1594. $visible=1;
  1595. }
  1596. $clean_post_title=Database::escape_string(Security::remove_XSS($values['post_title']));
  1597. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table
  1598. $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_date, thread_sticky,thread_title_qualify,thread_qualify_max,thread_weight,session_id)
  1599. VALUES ('".$clean_post_title."',
  1600. '".Database::escape_string($values['forum_id'])."',
  1601. '".Database::escape_string($_user['user_id'])."',
  1602. '".Database::escape_string(isset($values['poster_name'])?$values['poster_name']:null)."',
  1603. '".Database::escape_string($post_date)."',
  1604. '".Database::escape_string(isset($values['thread_sticky'])?$values['thread_sticky']:null)."'," .
  1605. "'".Database::escape_string($values['calification_notebook_title'])."'," .
  1606. "'".Database::escape_string($values['numeric_calification'])."'," .
  1607. "'".Database::escape_string($values['weight_calification'])."'," .
  1608. "'".api_get_session_id()."')";
  1609. $result=api_sql_query($sql, __LINE__, __FILE__);
  1610. $last_thread_id=Database::insert_id();
  1611. //add option gradebook qualify
  1612. if(isset($values['thread_qualify_gradebook']) && 1==$values['thread_qualify_gradebook']) {
  1613. //add function gradebook
  1614. $coursecode=api_get_course_id();
  1615. $resourcetype=5;
  1616. $resourceid=$last_thread_id;
  1617. $resourcename=$values['calification_notebook_title'];
  1618. $maxqualify=$values['numeric_calification'];
  1619. $weigthqualify=$values['weight_calification'];
  1620. $resourcedescription='';
  1621. $date=time();
  1622. //is_resource_in_course_gradebook($course_code, $resource_type, $resource_id);
  1623. add_resource_to_course_gradebook($coursecode,$resourcetype,$resourceid,$resourcename,$weigthqualify,$maxqualify,$resourcedescription,$date,0,api_get_session_id());
  1624. }
  1625. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"ForumThreadAdded", api_get_user_id());
  1626. // if the forum properties tell that the posts have to be approved we have to put the whole thread invisible
  1627. // because otherwise the students will see the thread and not the post in the thread.
  1628. // we also have to change $visible because the post itself has to be visible in this case (otherwise the teacher would have
  1629. // to make the thread visible AND the post
  1630. if ($visible==0) {
  1631. api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"invisible", api_get_user_id());
  1632. $visible=1;
  1633. }
  1634. // We now store the content in the table_post table
  1635. $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)
  1636. VALUES ('".$clean_post_title."',
  1637. '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['post_text'])),COURSEMANAGER))."',
  1638. '".Database::escape_string($last_thread_id)."',
  1639. '".Database::escape_string($values['forum_id'])."',
  1640. '".Database::escape_string($_user['user_id'])."',
  1641. '".Database::escape_string(isset($values['poster_name'])?$values['poster_name']:null)."',
  1642. '".Database::escape_string($post_date)."',
  1643. '".Database::escape_string(isset($values['post_notification'])?$values['post_notification']:null)."','0',
  1644. '".Database::escape_string($visible)."')";
  1645. api_sql_query($sql, __FILE__,__LINE__);
  1646. $last_post_id=Database::insert_id();
  1647. // 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)
  1648. $sql="UPDATE $table_threads SET thread_last_post='".Database::escape_string($last_post_id)."' WHERE thread_id='".Database::escape_string($last_thread_id)."'";
  1649. $result=api_sql_query($sql, __LINE__, __FILE__);
  1650. $message=get_lang('NewThreadStored');
  1651. // Storing the attachments if any
  1652. if ($has_attachment) {
  1653. $courseDir = $_course['path'].'/upload/forum';
  1654. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1655. $updir = $sys_course_path.$courseDir;
  1656. // Try to add an extension to the file if it hasn't one
  1657. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  1658. // user's file name
  1659. $file_name =$_FILES['user_upload']['name'];
  1660. if (!filter_extension($new_file_name)) {
  1661. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1662. } else {
  1663. if ($result) {
  1664. $comment = Database::escape_string($comment);
  1665. add_forum_attachment_file($comment,$last_post_id);
  1666. }
  1667. }
  1668. } else {
  1669. $message.='<br />';
  1670. }
  1671. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit()) {
  1672. $message.=get_lang('MessageHasToBeApproved').'<br />';
  1673. $message.=get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&gidReq='.$_SESSION['toolgroup'].'&origin='.$origin.'">'.get_lang('Forum').'</a><br />';
  1674. } else {
  1675. $message.=get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&gidReq='.$_SESSION['toolgroup'].'&origin='.$origin.'">'.get_lang('Forum').'</a><br />';
  1676. $message.=get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;thread='.$last_thread_id.'">'.get_lang('Message').'</a>';
  1677. }
  1678. $reply_info['new_post_id'] = $last_post_id;
  1679. $my_post_notification=isset($values['post_notification']) ? $values['post_notification'] : null;
  1680. if ($my_post_notification == 1) {
  1681. set_notification('thread',$last_thread_id, true);
  1682. }
  1683. send_notification_mails($last_thread_id,$reply_info);
  1684. session_unregister('formelements');
  1685. session_unregister('origin');
  1686. session_unregister('breadcrumbs');
  1687. session_unregister('addedresource');
  1688. session_unregister('addedresourceid');
  1689. Display :: display_confirmation_message($message,false);
  1690. } else {
  1691. Display::display_error_message(get_lang('UplNoFileUploaded'));
  1692. }
  1693. }
  1694. /**
  1695. * This function displays the form that is used to add a post. This can be a new thread or a reply.
  1696. * @param $action is the parameter that determines if we are
  1697. * 1. newthread: adding a new thread (both empty) => No I-frame
  1698. * 2. replythread: Replying to a thread ($action = replythread) => I-frame with the complete thread (if enabled)
  1699. * 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)
  1700. * 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)
  1701. *
  1702. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1703. * @version february 2006, dokeos 1.8
  1704. */
  1705. function show_add_post_form($action='', $id='', $form_values='') {
  1706. global $forum_setting;
  1707. global $current_forum;
  1708. global $_user;
  1709. global $origin;
  1710. global $charset;
  1711. $gradebook=Security::remove_XSS($_GET['gradebook']);
  1712. // setting the class and text of the form title and submit button
  1713. if ($_GET['action']=='quote'){
  1714. $class='save';
  1715. $text=get_lang('QuoteMessage');
  1716. }elseif ($_GET['action'] == 'replythread'){
  1717. $class='save';
  1718. $text=get_lang('ReplyToThread');
  1719. }elseif ($_GET['action']=='replymessage'){
  1720. $class='save';
  1721. $text=get_lang('ReplyToMessage');
  1722. }else {
  1723. $class='add';
  1724. $text=get_lang('CreateThread');
  1725. }
  1726. // initiate the object
  1727. $my_thread = isset($_GET['thread']) ? $_GET['thread']:'';
  1728. $my_forum = isset($_GET['forum']) ? $_GET['forum']:'';
  1729. $my_action = isset($_GET['action']) ? $_GET['action']:'';
  1730. $my_post = isset($_GET['post']) ? $_GET['post']:'';
  1731. $my_gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']):'';
  1732. $form = new FormValidator('thread', 'post', api_get_self().'?forum='.Security::remove_XSS($my_forum).'&gradebook='.$gradebook.'&thread='.Security::remove_XSS($my_thread).'&post='.Security::remove_XSS($my_post).'&action='.Security::remove_XSS($my_action).'&origin='.$origin);
  1733. $form->setConstants(array('forum' => '5'));
  1734. $form->addElement('header', '', $text);
  1735. // settting the form elements
  1736. $form->addElement('hidden', 'forum_id', strval(intval($my_forum)));
  1737. $form->addElement('hidden', 'thread_id', strval(intval($my_thread)));
  1738. $form->addElement('hidden', 'gradebook', $my_gradebook);
  1739. // if anonymous posts are allowed we also display a form to allow the user to put his name or username in
  1740. if ($current_forum['allow_anonymous']==1 AND !isset($_user['user_id'])) {
  1741. $form->addElement('text', 'poster_name', get_lang('Name'));
  1742. $form->applyFilter('poster_name', 'html_filter');
  1743. }
  1744. $form->addElement('text', 'post_title', get_lang('Title'),'class="input_titles"');
  1745. //$form->applyFilter('post_title', 'html_filter');
  1746. $form->addElement('html_editor', 'post_text', get_lang('Text'));
  1747. //$form->applyFilter('post_text', 'html_filter');
  1748. $form->addElement('html', '<div class="row"><div class="label"></div><div class="formw">');
  1749. $form->addElement('html','<a href="javascript://" onclick="return advanced_parameters()"><span id="img_plus_and_minus"><img src="../img/div_show.gif" alt="" /> '.get_lang('AdvancedParameters').'</span></a>','');
  1750. $form->addElement('html', '</div></div>');
  1751. $form->addElement('html','<div id="id_qualify" style="display:none">');
  1752. if( (api_is_course_admin() || api_is_course_coach() || api_is_course_tutor()) && !($my_thread) ){
  1753. // thread qualify
  1754. $form->addElement('static','Group', '<br /><strong>'.get_lang('AlterQualifyThread').'</strong>');
  1755. $form->addElement('text', 'numeric_calification', get_lang('QualifyNumeric'),'Style="width:40px"');
  1756. $form->applyFilter('numeric_calification', 'html_filter');
  1757. $form->addElement('checkbox', 'thread_qualify_gradebook', '', get_lang('QualifyThreadGradebook'),'onclick="javascript:if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"');
  1758. $form -> addElement('html','<div id="options_field" style="display:none">');
  1759. $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'));
  1760. $form->applyFilter('calification_notebook_title', 'html_filter');
  1761. $form->addElement('text', 'weight_calification', get_lang('QualifyWeight'),'value="0.00" Style="width:40px" onfocus="this.select();"');
  1762. $form->applyFilter('weight_calification', 'html_filter');
  1763. $form->addElement('html','</div>');
  1764. }
  1765. if ($forum_setting['allow_post_notificiation'] AND isset($_user['user_id'])) {
  1766. $form->addElement('checkbox', 'post_notification', '', get_lang('NotifyByEmail').' ('.$_user['mail'].')');
  1767. }
  1768. if ($forum_setting['allow_sticky'] AND api_is_allowed_to_edit() AND $action=='newthread') {
  1769. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  1770. }
  1771. if ($current_forum['allow_attachments']=='1' OR api_is_allowed_to_edit()) {
  1772. //$form->add_resource_button();
  1773. $values = $form->exportValues();
  1774. }
  1775. // user upload
  1776. $form->addElement('html','<br /><b><div class="row"><div class="label">'.get_lang('AddAnAttachment').'</div></div></b><br /><br />');
  1777. $form->addElement('file','user_upload',get_lang('FileName'),'');
  1778. $form->addElement('textarea','file_comment',get_lang('FileComment'),array ('rows' => 4, 'cols' => 34));
  1779. $form->applyFilter('file_comment', 'html_filter');
  1780. $form->addElement('html','</div>');
  1781. $userid =api_get_user_id();
  1782. $info =api_get_user_info($userid);
  1783. $courseid=api_get_course_id();
  1784. $form->addElement('style_submit_button', 'SubmitPost', $text, 'class="'.$class.'"');
  1785. $form->add_real_progress_bar('DocumentUpload','user_upload');
  1786. if ( !empty($form_values) ) {
  1787. $defaults['post_title']=prepare4display(Security::remove_XSS($form_values['post_title']));
  1788. $defaults['post_text']=prepare4display(Security::remove_XSS($form_values['post_text']));
  1789. $defaults['post_notification']=Security::remove_XSS($form_values['post_notification']);
  1790. $defaults['thread_sticky']=Security::remove_XSS($form_values['thread_sticky']);
  1791. }
  1792. // if we are quoting a message we have to retrieve the information of the post we are quoting so that
  1793. // we can add this as default to the textarea
  1794. if (($action=='quote' || $action=='replymessage') && isset($my_post)) {
  1795. // 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 !!!)
  1796. $form->addElement('hidden', 'post_parent_id', strval(intval($my_post))); // note this has to be cleaned first
  1797. // if we are replying or are quoting then we display a default title.
  1798. $values=get_post_information($my_post); // note: this has to be cleaned first
  1799. $defaults['post_title']=get_lang('ReplyShort').api_html_entity_decode($values['post_title'],ENT_QUOTES,$charset);
  1800. // When we are quoting a message then we have to put that message into the wysiwyg editor.
  1801. // note: the style has to be hardcoded here because using class="quote" didn't work
  1802. if($action=='quote') {
  1803. $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>';
  1804. }
  1805. }
  1806. $form->setDefaults(isset($defaults)?$defaults:null);
  1807. // the course admin can make a thread sticky (=appears with special icon and always on top)
  1808. $form->addRule('post_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1809. if ($current_forum['allow_anonymous']==1 AND !isset($_user['user_id'])) {
  1810. $form->addRule('poster_name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1811. }
  1812. // The validation or display
  1813. if( $form->validate() ) {
  1814. $check = Security::check_token('post');
  1815. if ($check) {
  1816. $values = $form->exportValues();
  1817. if($values['thread_qualify_gradebook']=='1' && empty($values['weight_calification'])){
  1818. Display::display_error_message(get_lang('YouMustAssignWeightOfQualification').'&nbsp;<a href="javascript:window.back()">'.get_lang('Back').'</a>',false);
  1819. return false;
  1820. }
  1821. Security::clear_token();
  1822. return $values;
  1823. }
  1824. } else {
  1825. $token = Security::get_token();
  1826. $form->addElement('hidden','sec_token');
  1827. $form->setConstants(array('sec_token' => $token));
  1828. $form->display();
  1829. echo '<br />';
  1830. if ($forum_setting['show_thread_iframe_on_reply'] and $action<>'newthread')
  1831. {
  1832. echo '<div class="row">
  1833. <div class="label">'.get_lang('Thread').'
  1834. </div>
  1835. <div class="formw">';
  1836. echo "<iframe style=\"border: 1px solid black\" src=\"iframe_thread.php?forum=".Security::remove_XSS($my_forum)."&amp;thread=".Security::remove_XSS($my_thread)."#".Security::remove_XSS($my_post)."\" width=\"100%\"></iframe>";
  1837. echo ' </div>
  1838. </div>';
  1839. }
  1840. }
  1841. }
  1842. /**
  1843. * @param integer contains the information of user id
  1844. * @param integer contains the information of thread id
  1845. * @param integer contains the information of thread qualify
  1846. * @param integer contains the information of user id of qualifier
  1847. * @param integer contains the information of time
  1848. * @param integer contains the information of session id
  1849. * @return Array() optional
  1850. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  1851. * @version October 2008, dokeos 1.8.6
  1852. **/
  1853. function store_theme_qualify($user_id,$thread_id,$thread_qualify=0,$qualify_user_id=0,$qualify_time,$session_id=null) {
  1854. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,'');
  1855. $table_threads =Database::get_course_table(TABLE_FORUM_THREAD,'');
  1856. if ($user_id==strval(intval($user_id)) && $thread_id==strval(intval($thread_id)) && $thread_qualify==strval(floatval($thread_qualify))) {
  1857. //testing
  1858. $sql_string="SELECT thread_qualify_max FROM ". $table_threads ." WHERE thread_id=".$thread_id.";";
  1859. $res_string=api_sql_query($sql_string,__FILE__,__LINE__);
  1860. $row_string=Database::fetch_array($res_string);
  1861. if ($thread_qualify<=$row_string[0]) {
  1862. $sql1="SELECT COUNT(*) FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";";
  1863. $res1=api_sql_query($sql1);
  1864. $row=Database::fetch_array($res1);
  1865. if ($row[0]==0) {
  1866. $sql="INSERT INTO $table_threads_qualify (user_id," .
  1867. "thread_id,qualify,qualify_user_id,qualify_time,session_id)" .
  1868. "VALUES('".$user_id."','".$thread_id."',".(float)$thread_qualify."," .
  1869. "'".$qualify_user_id."','".$qualify_time."','".$session_id."')";
  1870. $res=api_sql_query($sql,__FILE__,__LINE__);
  1871. return $res;
  1872. } else {
  1873. $sql1="SELECT qualify FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";";
  1874. $rs=api_sql_query($sql1,__FILE__,__LINE__);
  1875. $row=Database::fetch_array($rs);
  1876. $row[1]="update";
  1877. return $row;
  1878. }
  1879. }else{
  1880. return null;
  1881. }
  1882. }
  1883. }
  1884. /**
  1885. * This function show qualify.
  1886. * @param string contains the information of option to run
  1887. * @param string contains the information the current course id
  1888. * @param integer contains the information the current forum id
  1889. * @param integer contains the information the current user id
  1890. * @param integer contains the information the current thread id
  1891. * @return integer qualify
  1892. * @example $option=1 obtained the qualification of the current thread
  1893. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  1894. * @version October 2008, dokeos 1.8.6
  1895. */
  1896. function show_qualify($option,$couser_id,$forum_id,$user_id,$thread_id){
  1897. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,'');
  1898. $table_threads =Database::get_course_table(TABLE_FORUM_THREAD,'');
  1899. if ($user_id==strval(intval($user_id)) && $thread_id==strval(intval($thread_id)) && $option==1) {
  1900. $sql="SELECT qualify FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";";
  1901. $rs=api_sql_query($sql,__FILE__,__LINE__);
  1902. $row=Database::fetch_array($rs);
  1903. return $row[0];
  1904. }
  1905. if ($user_id==strval(intval($user_id)) && $option==2) {
  1906. $sql="SELECT thread_qualify_max FROM ".$table_threads." WHERE thread_id=".$thread_id.";";
  1907. $rs=api_sql_query($sql,__FILE__,__LINE__);
  1908. $row=Database::fetch_array($rs);
  1909. return $row[0];
  1910. }
  1911. }
  1912. /**
  1913. *
  1914. * This function get qualify historical.
  1915. * @param integer contains the information the current user id
  1916. * @param integer contains the information the current thread id
  1917. * @param boolean contains the information of option to run
  1918. * @return array()
  1919. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  1920. * @author Isaac Flores <isaac.flores@dokeos.com>,
  1921. * @version October 2008, dokeos 1.8.6
  1922. */
  1923. function get_historical_qualify($user_id,$thread_id,$opt) {
  1924. $my_qualify_log=array();
  1925. $table_threads_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG,'');
  1926. $opt = Database::escape_string($opt);
  1927. if ($opt=='false') {
  1928. $sql="SELECT * FROM ".$table_threads_qualify_log." WHERE thread_id='".Database::escape_string($thread_id)."' and user_id='".Database::escape_string($user_id)."' ORDER BY qualify_time";
  1929. } else {
  1930. $sql="SELECT * FROM ".$table_threads_qualify_log." WHERE thread_id='".Database::escape_string($thread_id)."' and user_id='".Database::escape_string($user_id)."' ORDER BY qualify_time DESC";
  1931. }
  1932. $rs=api_sql_query($sql,__FILE__,__LINE__);
  1933. while ($row=Database::fetch_array($rs,'ASSOC')) {
  1934. $my_qualify_log[]=$row;
  1935. }
  1936. return $my_qualify_log;
  1937. }
  1938. /**
  1939. *
  1940. * This function store qualify historical.
  1941. * @param boolean contains the information of option to run
  1942. * @param string contains the information the current course id
  1943. * @param integer contains the information the current forum id
  1944. * @param integer contains the information the current user id
  1945. * @param integer contains the information the current thread id
  1946. * @param integer contains the information the current qualify
  1947. * @return void
  1948. * @example $option=1 obtained the qualification of the current thread
  1949. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  1950. * @version October 2008, dokeos 1.8.6
  1951. */
  1952. function store_qualify_historical($option,$couser_id,$forum_id,$user_id,$thread_id,$current_qualify,$qualify_user_id) {
  1953. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,'');
  1954. $table_threads =Database::get_course_table(TABLE_FORUM_THREAD,'');
  1955. $table_threads_qualify_log=Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG,'');
  1956. $current_date=date('Y-m-d H:i:s');
  1957. if ($user_id==strval(intval($user_id)) && $thread_id==strval(intval($thread_id)) && $option==1) {
  1958. //extract information of thread_qualify
  1959. $sql="SELECT qualify,qualify_time FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";";
  1960. $rs=api_sql_query($sql,__FILE__,__LINE__);
  1961. $row=Database::fetch_array($rs);
  1962. //insert thread_historical
  1963. $sql1="INSERT INTO $table_threads_qualify_log (user_id," .
  1964. "thread_id,qualify,qualify_user_id,qualify_time,session_id)" .
  1965. "VALUES('".$user_id."','".$thread_id."',".(float)$row[0]."," .
  1966. "'".$qualify_user_id."','".$row[1]."','')";
  1967. api_sql_query($sql1,__FILE__,__LINE__);
  1968. //update
  1969. $sql2="UPDATE ".$table_threads_qualify." SET qualify=".$current_qualify.",qualify_time='".$current_date."' WHERE user_id=".$user_id." and thread_id=".$thread_id.";";
  1970. api_sql_query($sql2,__FILE__,__LINE__);
  1971. }
  1972. }
  1973. /**
  1974. *
  1975. * This function show current thread qualify .
  1976. * @param integer contains the information the current thread id
  1977. * @param integer contains the information the current session id
  1978. * @return integer
  1979. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  1980. * @version December 2008, dokeos 1.8.6
  1981. */
  1982. function current_qualify_of_thread($thread_id,$session_id) {
  1983. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,'');
  1984. $res=api_sql_query('SELECT qualify FROM '.$table_threads_qualify.' WHERE thread_id='.$thread_id.' AND session_id='.$session_id);
  1985. $row=Database::fetch_array($res,'ASSOC');
  1986. return $row['qualify'];
  1987. }
  1988. /**
  1989. * This function stores a reply in the forum_post table.
  1990. * It also updates the forum_threads table (thread_replies +1 , thread_last_post, thread_date)
  1991. *
  1992. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1993. * @version february 2006, dokeos 1.8
  1994. */
  1995. function store_reply($values) {
  1996. global $table_threads;
  1997. global $table_posts;
  1998. global $forum_table_attachment;
  1999. global $_user;
  2000. global $_course;
  2001. global $current_forum;
  2002. global $origin;
  2003. $gradebook=Security::remove_XSS($_GET['gradebook']);
  2004. $post_date=date('Y-m-d H:i:s');
  2005. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit()) {
  2006. $visible=0; // the post is not approved yet.
  2007. } else {
  2008. $visible=1;
  2009. }
  2010. $upload_ok=1;
  2011. $has_attachment=false;
  2012. if (!empty($_FILES['user_upload']['name'])) {
  2013. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  2014. $has_attachment=true;
  2015. }
  2016. if ($upload_ok) {
  2017. // We first store an entry in the forum_post table
  2018. $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, post_date, post_notification, post_parent_id, visible)
  2019. VALUES ('".Database::escape_string(Security::remove_XSS($values['post_title']))."',
  2020. '".Database::escape_string(isset($values['post_text']) ? Security::remove_XSS(stripslashes(api_html_entity_decode($values['post_text'])),COURSEMANAGER) : null)."',
  2021. '".Database::escape_string($values['thread_id'])."',
  2022. '".Database::escape_string($values['forum_id'])."',
  2023. '".Database::escape_string($_user['user_id'])."',
  2024. '".Database::escape_string($post_date)."',
  2025. '".Database::escape_string(isset($values['post_notification'])?$values['post_notification']:null)."',
  2026. '".Database::escape_string(isset($values['post_parent_id'])?$values['post_parent_id']:null)."',
  2027. '".Database::escape_string($visible)."')";
  2028. $result=api_sql_query($sql, __LINE__, __FILE__);
  2029. $new_post_id=Database::insert_id();
  2030. $values['new_post_id']=$new_post_id;
  2031. $message=get_lang('ReplyAdded');
  2032. if ($has_attachment) {
  2033. $courseDir = $_course['path'].'/upload/forum';
  2034. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2035. $updir = $sys_course_path.$courseDir;
  2036. // Try to add an extension to the file if it hasn't one
  2037. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  2038. // user's file name
  2039. $file_name =$_FILES['user_upload']['name'];
  2040. if (!filter_extension($new_file_name)) {
  2041. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  2042. } else {
  2043. $new_file_name = uniqid('');
  2044. $new_path=$updir.'/'.$new_file_name;
  2045. $result= @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  2046. $comment=$values['file_comment'];
  2047. // Storing the attachments if any
  2048. if ($result) {
  2049. $sql='INSERT INTO '.$forum_table_attachment.'(filename,comment, path, post_id,size) '.
  2050. "VALUES ( '".Database::escape_string($file_name)."', '".Database::escape_string($comment)."', '".Database::escape_string($new_file_name)."' , '".$new_post_id."', '".$_FILES['user_upload']['size']."' )";
  2051. $result=api_sql_query($sql, __LINE__, __FILE__);
  2052. $message.=' / '.get_lang('FileUploadSucces');
  2053. $last_id=Database::insert_id();
  2054. api_item_property_update($_course, TOOL_FORUM_ATTACH, $last_id ,'ForumAttachmentAdded', api_get_user_id());
  2055. }
  2056. }
  2057. }
  2058. // update the thread
  2059. update_thread($values['thread_id'], $new_post_id,$post_date);
  2060. // update the forum
  2061. api_item_property_update($_course, TOOL_FORUM, $values['forum_id'],"NewMessageInForum", api_get_user_id());
  2062. if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit()) {
  2063. $message.='<br />'.get_lang('MessageHasToBeApproved').'<br />';
  2064. }
  2065. $message.='<br />'.get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&origin='.$origin.'">'.get_lang('Forum').'</a><br />';
  2066. $message.=get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&amp;thread='.$values['thread_id'].'&origin='.$origin.'&amp;gradebook='.$gradebook.'">'.get_lang('Message').'</a>';
  2067. // setting the notification correctly
  2068. $my_post_notification=isset($values['post_notification']) ? $values['post_notification'] :null;
  2069. if ($my_post_notification == 1) {
  2070. set_notification('thread',$values['thread_id'], true);
  2071. }
  2072. send_notification_mails($values['thread_id'], $values);
  2073. session_unregister('formelements');
  2074. session_unregister('origin');
  2075. session_unregister('breadcrumbs');
  2076. session_unregister('addedresource');
  2077. session_unregister('addedresourceid');
  2078. Display :: display_confirmation_message($message,false);
  2079. } else {
  2080. Display::display_error_message(get_lang('UplNoFileUploaded')." ". get_lang('UplSelectFileFirst'));
  2081. }
  2082. }
  2083. /**
  2084. * This function displays the form that is used to edit a post. This can be a new thread or a reply.
  2085. * @param array contains all the information about the current post
  2086. * @param array contains all the information about the current thread
  2087. * @param array contains all info about the current forum (to check if attachments are allowed)
  2088. * @param array contains the default values to fill the form
  2089. * @return void
  2090. *
  2091. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2092. * @version february 2006, dokeos 1.8
  2093. */
  2094. function show_edit_post_form($current_post, $current_thread, $current_forum, $form_values='',$id_attach=0) {
  2095. global $forum_setting;
  2096. global $_user;
  2097. global $origin;
  2098. $gradebook=Security::remove_XSS($_GET['gradebook']);
  2099. // initiate the object
  2100. $form = new FormValidator('edit_post', 'post', api_get_self().'?forum='.Security::remove_XSS($_GET['forum']).'&gradebook='.$gradebook.'&origin='.$origin.'&thread='.Security::remove_XSS($_GET['thread']).'&post='.Security::remove_XSS($_GET['post']));
  2101. $form->addElement('header', '', get_lang('EditPost'));
  2102. // settting the form elements
  2103. $form->addElement('hidden', 'post_id', $current_post['post_id']);
  2104. $form->addElement('hidden', 'thread_id', $current_thread['thread_id']);
  2105. $form->addElement('hidden', 'id_attach', $id_attach);
  2106. if ($current_post['post_parent_id']==0) {
  2107. $form->addElement('hidden', 'is_first_post_of_thread', '1');
  2108. }
  2109. $form->addElement('text', 'post_title', get_lang('Title'),'class="input_titles"');
  2110. $form->applyFilter('post_title', 'html_filter');
  2111. $form->addElement('html_editor', 'post_text', get_lang('Text'));
  2112. //$form->applyFilter('post_text', 'html_filter');
  2113. $form->addElement('html', '<div class="row"><div class="label"></div><div class="formw">');
  2114. $form->addElement('HTML','<a href="javascript://" onclick="return advanced_parameters()"><span id="img_plus_and_minus"><img src="../img/div_show.gif" alt="" />'.get_lang('AdvancedParameters').'</span></a>','');
  2115. $form->addElement('html','</div></div>');
  2116. $form->addElement('html','<div id="id_qualify" style="display:none">');
  2117. if (!isset($_GET['edit'])) {
  2118. $form->addElement('static','Group','<strong>'.get_lang('AlterQualifyThread').'</strong>');
  2119. $form->addElement('text', 'numeric_calification', get_lang('QualifyNumeric'),'value="'.$current_thread['thread_qualify_max'].'" Style="width:40px"');
  2120. $form->applyFilter('numeric_calification', 'html_filter');
  2121. $form->addElement('checkbox', 'thread_qualify_gradebook', '', get_lang('QualifyThreadGradebook'),'onclick="javascript:if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"');
  2122. $defaults['thread_qualify_gradebook']=is_resource_in_course_gradebook(api_get_course_id(),5,$_GET['thread'],api_get_session_id());
  2123. if (!empty($defaults['thread_qualify_gradebook'])) {
  2124. $form -> addElement('html','<div id="options_field" style="display:block">');
  2125. } else {
  2126. $form -> addElement('html','<div id="options_field" style="display:none">');
  2127. }
  2128. $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'),'value="'.$current_thread['thread_title_qualify'].'"');
  2129. $form->applyFilter('calification_notebook_title', 'html_filter');
  2130. $form->addElement('text', 'weight_calification', get_lang('QualifyWeight'),'value="'.$current_thread['thread_weight'].'" Style="width:40px"');
  2131. $form->applyFilter('weight_calification', 'html_filter');
  2132. $form->addElement('html','</div>');
  2133. //add gradebook
  2134. }
  2135. if ($forum_setting['allow_post_notificiation']) {
  2136. $form->addElement('checkbox', 'post_notification', '', get_lang('NotifyByEmail').' ('.$current_post['email'].')');
  2137. }
  2138. 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
  2139. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  2140. if ( $current_thread['thread_sticky']==1 ) {
  2141. $defaults['thread_sticky']=true;
  2142. }
  2143. }
  2144. $attachment_list=get_attachment($current_post['post_id']);
  2145. $message=get_lang('AddAnAttachment');
  2146. if (!empty($attachment_list)) {
  2147. $message=get_lang('EditAnAttachment');
  2148. $form->addElement('static','Group','','<br />'.Display::return_icon('attachment.gif',get_lang('Attachment')).'&nbsp;'.$attachment_list['filename'].(!empty($attachment_list['comment'])?'('.$attachment_list['comment'].')':''));
  2149. $form->addElement('checkbox', 'remove_attach', null, get_lang('DeleteAttachmentFile'));
  2150. }
  2151. // user upload
  2152. $form->addElement('html','<br /><b><div class="row"><div class="label">'.$message.'</div></div></b><br /><br />');
  2153. $form->addElement('file','user_upload',get_lang('FileName'),'');
  2154. $form->addElement('textarea','file_comment',get_lang('FileComment'),array ('rows' => 4, 'cols' => 34));
  2155. $form->applyFilter('file_comment', 'html_filter');
  2156. $form->addElement('html','</div><br /><br />');
  2157. if ($current_forum['allow_attachments']=='1' OR api_is_allowed_to_edit()) {
  2158. if (empty($form_values) AND !isset($_POST['SubmitPost'])) {
  2159. //edit_added_resources('forum_post',$current_post['post_id']);
  2160. }
  2161. //$form->add_resource_button();
  2162. $values = $form->exportValues();
  2163. }
  2164. $form->addElement('style_submit_button', 'SubmitPost', get_lang('ModifyThread'), 'class="save"');
  2165. global $charset;
  2166. // setting the default values for the form elements
  2167. $defaults['post_title']=prepare4display(api_html_entity_decode($current_post['post_title'],ENT_QUOTES,$charset));
  2168. $defaults['post_text']=prepare4display($current_post['post_text']);
  2169. if ( $current_post['post_notification']==1 ) {
  2170. $defaults['post_notification']=true;
  2171. }
  2172. if (!empty($form_values)) {
  2173. //$defaults['post_title']=Security::remove_XSS($form_values['post_title']);
  2174. //$defaults['post_text']=Security::remove_XSS($form_values['post_text']);
  2175. $defaults['post_notification']=Security::remove_XSS($form_values['post_notification']);
  2176. $defaults['thread_sticky']=Security::remove_XSS($form_values['thread_sticky']);
  2177. }
  2178. $form->setDefaults($defaults);
  2179. // the course admin can make a thread sticky (=appears with special icon and always on top)
  2180. $form->addRule('post_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  2181. // The validation or display
  2182. if( $form->validate() ) {
  2183. $values = $form->exportValues();
  2184. if($values['thread_qualify_gradebook']=='1' && empty($values['weight_calification'])){
  2185. Display::display_error_message(get_lang('YouMustAssignWeightOfQualification').'&nbsp;<a href="javascript:window.back()">'.get_lang('Back').'</a>',false);
  2186. return false;
  2187. }
  2188. return $values;
  2189. } else {
  2190. $form->display();
  2191. }
  2192. }
  2193. /**
  2194. * This function stores the edit of a post in the forum_post table.
  2195. *
  2196. * @param
  2197. * @return
  2198. *
  2199. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2200. * @version february 2006, dokeos 1.8
  2201. */
  2202. function store_edit_post($values) {
  2203. global $table_threads;
  2204. global $table_posts;
  2205. global $origin;
  2206. $gradebook=Security::remove_XSS($_GET['gradebook']);
  2207. // first we check if the change affects the thread and if so we commit the changes (sticky and post_title=thread_title are relevant)
  2208. //if (array_key_exists('is_first_post_of_thread',$values) AND $values['is_first_post_of_thread']=='1') {
  2209. $sql="UPDATE $table_threads SET thread_title='".Database::escape_string($values['post_title'])."',
  2210. thread_sticky='".Database::escape_string(isset($values['thread_sticky']) ? $values['thread_sticky'] : null)."'," .
  2211. "thread_title_qualify='".Database::escape_string($values['calification_notebook_title'])."'," .
  2212. "thread_qualify_max='".Database::escape_string($values['numeric_calification'])."',".
  2213. "thread_weight='".Database::escape_string($values['weight_calification'])."'".
  2214. " WHERE thread_id='".Database::escape_string($values['thread_id'])."'";
  2215. api_sql_query($sql,__FILE__, __LINE__);
  2216. //}
  2217. // update the post_title and the post_text
  2218. $sql="UPDATE $table_posts SET post_title='".Database::escape_string(Security::remove_XSS($values['post_title']))."',
  2219. post_text='".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['post_text'])),COURSEMANAGER))."',
  2220. post_notification='".Database::escape_string(isset($values['post_notification'])?$values['post_notification']:null)."'
  2221. WHERE post_id='".Database::escape_string($values['post_id'])."'";
  2222. //error_log($sql);
  2223. api_sql_query($sql,__FILE__, __LINE__);
  2224. if (!empty($values['remove_attach'])) {
  2225. delete_attachment($values['post_id']);
  2226. }
  2227. if (empty($values['id_attach'])) {
  2228. add_forum_attachment_file($values['file_comment'],$values['post_id']);
  2229. } else {
  2230. edit_forum_attachment_file($values['file_comment'],$values['post_id'],$values['id_attach']);
  2231. }
  2232. if (api_is_course_admin()==true) {
  2233. $ccode = api_get_course_id();
  2234. $sid = api_get_session_id();
  2235. $link_id = is_resource_in_course_gradebook($ccode,5,$values['thread_id'],$sid);
  2236. $thread_qualify_gradebook=isset($values['thread_qualify_gradebook']) ? $values['thread_qualify_gradebook'] : null;
  2237. if ($thread_qualify_gradebook!=1) {
  2238. if ($link_id !== false) {
  2239. remove_resource_from_course_gradebook($link_id);
  2240. }
  2241. } else {
  2242. if ($link_id === false && !$_GET['thread']) {
  2243. //$date_in_gradebook=date('Y-m-d H:i:s');
  2244. $date_in_gradebook=null;
  2245. $weigthqualify=$values['weight_calification'];
  2246. add_resource_to_course_gradebook($ccode,5,$values['thread_id'],Database::escape_string($values['calification_notebook_title']),$weigthqualify,$values['numeric_calification'],null,$date_in_gradebook,0,$sid);
  2247. }
  2248. }
  2249. }
  2250. // Storing the attachments if any
  2251. //update_added_resources('forum_post',$values['post_id']);
  2252. $message=get_lang('EditPostStored').'<br />';
  2253. $message.=get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.Security::remove_XSS($_GET['forum']).'&amp;gidReq='.$_SESSION['toolgroup'].'&amp;origin='.$origin.'">'.get_lang('Forum').'</a><br />';
  2254. $message.=get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.Security::remove_XSS($_GET['forum']).'&amp;gidReq='.$_SESSION['toolgroup'].'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;thread='.$values['thread_id'].'&amp;post='.Security::remove_XSS($_GET['post']).'">'.get_lang('Message').'</a>';
  2255. session_unregister('formelements');
  2256. session_unregister('origin');
  2257. session_unregister('breadcrumbs');
  2258. session_unregister('addedresource');
  2259. session_unregister('addedresourceid');
  2260. Display :: display_confirmation_message($message,false);
  2261. }
  2262. /**
  2263. * This function displays the firstname and lastname of the user as a link to the user tool.
  2264. *
  2265. * @param
  2266. * @return
  2267. *
  2268. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2269. * @version february 2006, dokeos 1.8
  2270. */
  2271. function display_user_link($user_id, $name, $origin='') {
  2272. if ($user_id<>0) {
  2273. return '<a href="../user/userInfo.php?uInfo='.$user_id.'" '. (!empty($origin)? 'target="_top"': '') .'>'.$name.'</a>';
  2274. } else {
  2275. return $name.' ('.get_lang('Anonymous').')';
  2276. }
  2277. }
  2278. /**
  2279. * This function displays the user image from the profile, with a link to the user's details.
  2280. * @param int User's database ID
  2281. * @param str User's name
  2282. * @return string An HTML with the anchor and the image of the user
  2283. * @author Julio Montoya <julio.montoya@dokeos.com>
  2284. */
  2285. function display_user_image($user_id,$name, $origin='') {
  2286. $link='<a href="../user/userInfo.php?uInfo='.$user_id.'" '. (!empty($origin)? 'target="_top"': '') .'>';
  2287. $attrb=array();
  2288. if ($user_id<>0) {
  2289. $image_path = UserManager::get_user_picture_path_by_id($user_id,'web',false, true);
  2290. $image_repository = $image_path['dir'];
  2291. $existing_image = $image_path['file'];
  2292. $friends_profile = UserManager::get_picture_user($user_id, $image_path['file'], 0, 'medium_' , 'width="96" height="96" ');
  2293. return $link.'<img src="'.$friends_profile['file'].'" '.$friends_profile['style'].' alt="'.$name.'" title="'.$name.'" /></a>';
  2294. } else {
  2295. return $link.'<img src="'.api_get_path(WEB_CODE_PATH)."img/unknown.jpg".'" alt="'.$name.'" title="'.$name.'" /></a>';
  2296. }
  2297. }
  2298. /**
  2299. * The thread view counter gets increased every time someone looks at the thread
  2300. *
  2301. * @param
  2302. * @return
  2303. *
  2304. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2305. * @version february 2006, dokeos 1.8
  2306. */
  2307. function increase_thread_view($thread_id) {
  2308. global $table_threads;
  2309. $sql="UPDATE $table_threads SET thread_views=thread_views+1 WHERE thread_id='".Database::escape_string($thread_id)."'"; // this needs to be cleaned first
  2310. $result=api_sql_query($sql, __LINE__, __FILE__);
  2311. }
  2312. /**
  2313. * The relies counter gets increased every time somebody replies to the thread
  2314. *
  2315. * @param
  2316. * @return
  2317. *
  2318. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2319. * @version february 2006, dokeos 1.8
  2320. */
  2321. function update_thread($thread_id, $last_post_id,$post_date) {
  2322. global $table_threads;
  2323. $sql="UPDATE $table_threads SET thread_replies=thread_replies+1,
  2324. thread_last_post='".Database::escape_string($last_post_id)."',
  2325. thread_date='".Database::escape_string($post_date)."' WHERE thread_id='".Database::escape_string($thread_id)."'"; // this needs to be cleaned first
  2326. $result=api_sql_query($sql, __LINE__, __FILE__);
  2327. }
  2328. /**
  2329. * This function is called when the user is not allowed in this forum/thread/...
  2330. *
  2331. * @param
  2332. * @return
  2333. *
  2334. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2335. * @version february 2006, dokeos 1.8
  2336. */
  2337. function forum_not_allowed_here() {
  2338. Display :: display_error_message(get_lang('NotAllowedHere'));
  2339. Display :: display_footer();
  2340. exit;
  2341. }
  2342. /**
  2343. * This function is used to find all the information about what's new in the forum tool
  2344. *
  2345. * @param
  2346. * @return
  2347. *
  2348. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2349. * @version february 2006, dokeos 1.8
  2350. */
  2351. function get_whats_new() {
  2352. global $_user;
  2353. global $_course;
  2354. global $table_posts;
  2355. // 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.
  2356. //$tool=TOOL_FORUM;
  2357. $tool=TOOL_FORUM; //
  2358. // to do: remove this. For testing purposes only
  2359. //session_unregister('last_forum_access');
  2360. //session_unregister('whatsnew_post_info');
  2361. if (!$_SESSION['last_forum_access']) {
  2362. $tracking_last_tool_access=Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  2363. $sql="SELECT * FROM ".$tracking_last_tool_access." WHERE access_user_id='".Database::escape_string($_user['user_id'])."' AND access_cours_code='".Database::escape_string($_course['sysCode'])."' AND access_tool='".Database::escape_string($tool)."'";
  2364. $result=api_sql_query($sql,__FILE__,__LINE__);
  2365. $row=Database::fetch_array($result);
  2366. $_SESSION['last_forum_access']=$row['access_date'];
  2367. }
  2368. if (!$_SESSION['whatsnew_post_info']) {
  2369. if ($_SESSION['last_forum_access']<>'') {
  2370. $whatsnew_post_info = array();
  2371. $sql="SELECT * FROM".$table_posts."WHERE post_date>'".Database::escape_string($_SESSION['last_forum_access'])."'"; // note: check the performance of this query.
  2372. $result=api_sql_query($sql,__FILE__,__LINE__);
  2373. while ($row=Database::fetch_array($result)) {
  2374. $whatsnew_post_info[$row['forum_id']][$row['thread_id']][$row['post_id']]=$row['post_date'];
  2375. }
  2376. $_SESSION['whatsnew_post_info']=$whatsnew_post_info;
  2377. }
  2378. }
  2379. }
  2380. /**
  2381. * With this function we find the number of posts and topics in a given forum.
  2382. *
  2383. * @param
  2384. * @return
  2385. *
  2386. * @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
  2387. * 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)
  2388. * @todo consider merging both in one query.
  2389. *
  2390. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2391. * @version february 2006, dokeos 1.8
  2392. *
  2393. * @deprecated the counting mechanism is now inside the function get_forums
  2394. */
  2395. function get_post_topics_of_forum($forum_id) {
  2396. global $table_posts;
  2397. global $table_threads;
  2398. global $table_item_property;
  2399. $sql="SELECT count(*) as number_of_posts FROM $table_posts WHERE forum_id='".$forum_id."'";
  2400. if (api_is_allowed_to_edit()) {
  2401. $sql="SELECT count(*) as number_of_posts
  2402. FROM $table_posts posts, $table_threads threads, $table_item_property item_property
  2403. WHERE posts.forum_id='".Database::escape_string($forum_id)."'
  2404. AND posts.thread_id=threads.thread_id
  2405. AND item_property.ref=threads.thread_id
  2406. AND item_property.visibility<>2
  2407. AND item_property.tool='".TOOL_FORUM_THREAD."'
  2408. ";
  2409. } else {
  2410. $sql="SELECT count(*) as number_of_posts
  2411. FROM $table_posts posts, $table_threads threads, $table_item_property item_property
  2412. WHERE posts.forum_id='".Database::escape_string($forum_id)."'
  2413. AND posts.thread_id=threads.thread_id
  2414. AND item_property.ref=threads.thread_id
  2415. AND item_property.visibility=1
  2416. AND posts.visible=1
  2417. AND item_property.tool='".TOOL_FORUM_THREAD."'
  2418. ";
  2419. }
  2420. $result=api_sql_query($sql, __FILE__, __LINE__);
  2421. $row=Database::fetch_array($result);
  2422. $number_of_posts=$row['number_of_posts'];
  2423. // 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
  2424. if (api_is_allowed_to_edit()) {
  2425. $sql="SELECT count(*) as number_of_topics
  2426. FROM $table_threads threads, $table_item_property item_property
  2427. WHERE threads.forum_id='".Database::escape_string($forum_id)."'
  2428. AND item_property.ref=threads.thread_id
  2429. AND item_property.visibility<>2
  2430. AND item_property.tool='".TOOL_FORUM_THREAD."'
  2431. ";
  2432. } else {
  2433. $sql="SELECT count(*) as number_of_topics
  2434. FROM $table_threads threads, $table_item_property item_property
  2435. WHERE threads.forum_id='".Database::escape_string($forum_id)."'
  2436. AND item_property.ref=threads.thread_id
  2437. AND item_property.visibility=1
  2438. AND item_property.tool='".TOOL_FORUM_THREAD."'
  2439. ";
  2440. }
  2441. $result=api_sql_query($sql, __FILE__, __LINE__);
  2442. $row=Database::fetch_array($result);
  2443. $number_of_topics=$row['number_of_topics'];
  2444. if ($number_of_topics=='') {
  2445. $number_of_topics=0; // due to the nature of the group by this can result in an empty string.
  2446. }
  2447. $return=array('number_of_topics'=>$number_of_topics, 'number_of_posts'=>$number_of_posts);
  2448. return $return;
  2449. }
  2450. /**
  2451. * This function approves a post = change
  2452. *
  2453. * @param $post_id the id of the post that will be deleted
  2454. * @param $action make the post visible or invisible
  2455. * @return
  2456. *
  2457. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2458. * @version february 2006, dokeos 1.8
  2459. */
  2460. function approve_post($post_id, $action) {
  2461. global $table_posts;
  2462. if ($action=='invisible') {
  2463. $visibility_value=0;
  2464. }
  2465. if ($action=='visible') {
  2466. $visibility_value=1;
  2467. handle_mail_cue('post',$post_id);
  2468. }
  2469. $sql="UPDATE $table_posts SET visible='".Database::escape_string($visibility_value)."' WHERE post_id='".Database::escape_string($post_id)."'";
  2470. $return=api_sql_query($sql, __FILE__, __LINE__);
  2471. if ($return) {
  2472. return 'PostVisibilityChanged';
  2473. }
  2474. }
  2475. /**
  2476. * This function retrieves all the unapproved messages for a given forum
  2477. * This is needed to display the icon that there are unapproved messages in that thread (only the courseadmin can see this)
  2478. *
  2479. * @param $forum_id the forum where we want to know the unapproved messages of
  2480. * @return
  2481. *
  2482. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2483. * @version february 2006, dokeos 1.8
  2484. */
  2485. function get_unaproved_messages($forum_id) {
  2486. global $table_posts;
  2487. $return_array=array();
  2488. $sql="SELECT DISTINCT thread_id FROM $table_posts WHERE forum_id='".Database::escape_string($forum_id)."' AND visible='0'";
  2489. $result=api_sql_query($sql, __FILE__, __LINE__);
  2490. while($row=Database::fetch_array($result)) {
  2491. $return_array[]=$row['thread_id'];
  2492. }
  2493. return $return_array;
  2494. }
  2495. /**
  2496. * This function sends the notification mails to everybody who stated that they wanted to be informed when a new post
  2497. * was added to a given thread.
  2498. *
  2499. * @param
  2500. * @return
  2501. *
  2502. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2503. * @version february 2006, dokeos 1.8
  2504. */
  2505. function send_notification_mails($thread_id, $reply_info) {
  2506. global $table_posts;
  2507. global $table_user;
  2508. global $table_mailcue;
  2509. // First we need to check if
  2510. // 1. the forum category is visible
  2511. // 2. the forum is visible
  2512. // 3. the thread is visible
  2513. // 4. the reply is visible (=when there is
  2514. $current_thread=get_thread_information($thread_id);
  2515. $current_forum=get_forum_information($current_thread['forum_id']);
  2516. $current_forum_category=get_forumcategory_information($current_forum['forum_category']);
  2517. if($current_thread['visibility']=='1' AND $current_forum['visibility']=='1' AND $current_forum_category['visibility']=='1' AND $current_forum['approval_direct_post']!='1') {
  2518. $send_mails=true;
  2519. } else {
  2520. $send_mails=false;
  2521. }
  2522. // the forum category, the forum, the thread and the reply are visible to the user
  2523. if ($send_mails==true) {
  2524. send_notifications($current_thread['forum_id'],$thread_id);
  2525. /*
  2526. $sql="SELECT DISTINCT user.firstname, user.lastname, user.email, user.user_id
  2527. FROM $table_posts post, $table_user user
  2528. WHERE post.thread_id='".Database::escape_string($thread_id)."'
  2529. AND post.post_notification='1'
  2530. AND post.poster_id=user.user_id";
  2531. $result=api_sql_query($sql, __LINE__, __FILE__);
  2532. while ($row=Database::fetch_array($result))
  2533. {
  2534. send_mail($row, $current_thread);
  2535. }
  2536. */
  2537. } else {
  2538. /*
  2539. $sql="SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($thread_id)."' AND post_notification='1'";
  2540. $result=api_sql_query($sql, __LINE__, __FILE__);
  2541. */
  2542. $table_notification = Database::get_course_table('forum_notification');
  2543. $sql = "SELECT * FROM $table_notification WHERE forum_id = '".Database::escape_string($current_forum['forum_id'])."' OR thread_id = '".Database::escape_string($thread_id)."'";
  2544. $result=api_sql_query($sql, __FILE__, __LINE__);
  2545. while ($row=Database::fetch_array($result)) {
  2546. $sql_mailcue="INSERT INTO $table_mailcue (thread_id, post_id) VALUES ('".Database::escape_string($thread_id)."', '".Database::escape_string($reply_info['new_post_id'])."')";
  2547. $result_mailcue=api_sql_query($sql_mailcue, __LINE__, __FILE__);
  2548. }
  2549. }
  2550. }
  2551. /**
  2552. * 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
  2553. * to be informed about the new posts by mail.
  2554. *
  2555. * @param
  2556. * @return
  2557. *
  2558. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2559. * @version february 2006, dokeos 1.8
  2560. */
  2561. function handle_mail_cue($content, $id) {
  2562. global $table_mailcue;
  2563. global $table_forums;
  2564. global $table_threads;
  2565. global $table_posts;
  2566. global $table_users;
  2567. // 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.
  2568. if ($content=='post') {
  2569. // getting the information about the post (need the thread_id)
  2570. $post_info=get_post_information($id);
  2571. // sending the mail to all the users that wanted to be informed for replies on this thread.
  2572. $sql="SELECT users.firstname, users.lastname, users.user_id, users.email FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  2573. WHERE posts.thread_id='".Database::escape_string($post_info['thread_id'])."'
  2574. AND posts.post_notification='1'
  2575. AND mailcue.thread_id='".Database::escape_string($post_info['thread_id'])."'
  2576. AND users.user_id=posts.poster_id
  2577. GROUP BY users.email";
  2578. $result=api_sql_query($sql, __FILE__, __LINE__);
  2579. while ($row=Database::fetch_array($result)) {
  2580. send_mail($row, get_thread_information($post_info['thread_id']));
  2581. }
  2582. // deleting the relevant entries from the mailcue
  2583. $sql_delete_mailcue="DELETE FROM $table_mailcue WHERE post_id='".Database::escape_string($id)."' AND thread_id='".Database::escape_string($post_info['thread_id'])."'";
  2584. //$result=api_sql_query($sql_delete_mailcue, __LINE__, __FILE__);
  2585. } elseif ($content=='thread') {
  2586. // sending the mail to all the users that wanted to be informed for replies on this thread.
  2587. $sql="SELECT users.firstname, users.lastname, users.user_id, users.email FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  2588. WHERE posts.thread_id='".Database::escape_string($id)."'
  2589. AND posts.post_notification='1'
  2590. AND mailcue.thread_id='".Database::escape_string($id)."'
  2591. AND users.user_id=posts.poster_id
  2592. GROUP BY users.email";
  2593. $result=api_sql_query($sql,__FILE__, __LINE__);
  2594. while ($row=Database::fetch_array($result)) {
  2595. send_mail($row, get_thread_information($id));
  2596. }
  2597. // deleting the relevant entries from the mailcue
  2598. $sql_delete_mailcue="DELETE FROM $table_mailcue WHERE thread_id='".Database::escape_string($id)."'";
  2599. $result=api_sql_query($sql_delete_mailcue, __FILE__, __LINE__);
  2600. } elseif ($content=='forum') {
  2601. $sql="SELECT * FROM $table_threads WHERE forum_id='".Database::escape_string($id)."'";
  2602. $result=api_sql_query($sql, __FILE__, __LINE__);
  2603. while ($row=Database::fetch_array($result)) {
  2604. handle_mail_cue('thread',$row['thread_id']);
  2605. }
  2606. } elseif ($content=='forum_category') {
  2607. $sql="SELECT * FROM $table_forums WHERE forum_category ='".Database::escape_string($id)."'";
  2608. $result=api_sql_query($sql, __FILE__, __LINE__);
  2609. while ($row=Database::fetch_array($result)) {
  2610. handle_mail_cue('forum',$row['forum_id']);
  2611. }
  2612. } else {
  2613. return get_lang('Error');
  2614. }
  2615. }
  2616. /**
  2617. * This function sends the mails for the mail notification
  2618. *
  2619. * @param
  2620. * @return
  2621. *
  2622. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2623. * @version february 2006, dokeos 1.8
  2624. */
  2625. function send_mail($user_info=array(), $thread_information=array()) {
  2626. global $_course;
  2627. global $_user;
  2628. $email_subject = get_lang('NewForumPost')." - ".$_course['official_code'];
  2629. if (isset($thread_information) and is_array($thread_information)) {
  2630. $thread_link= api_get_path('WEB_CODE_PATH').'forum/viewthread.php?'.api_get_cidreq().'&forum='.$thread_information['forum_id'].'&thread='.$thread_information['thread_id'];
  2631. }
  2632. $email_body= $user_info['firstname']." ".$user_info['lastname']."\n\r";
  2633. $email_body .= '['.$_course['official_code'].'] - ['.$_course['name']."]<br>\n";
  2634. $email_body .= get_lang('NewForumPost')."\n";
  2635. $email_body .= get_lang('YouWantedToStayInformed')."<br><br>\n";
  2636. $email_body .= get_lang('ThreadCanBeFoundHere')." : <a href=\"".$thread_link."\">".$thread_link."</a>\n";
  2637. //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first)
  2638. //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case)
  2639. if(empty($charset)) {
  2640. $charset='ISO-8859-1';
  2641. }
  2642. if ($user_info['user_id']<>$_user['user_id']) {
  2643. $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']);
  2644. }
  2645. }
  2646. /**
  2647. * This function displays the form for moving a thread to a different (already existing) forum
  2648. *
  2649. * @param
  2650. * @return
  2651. *
  2652. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2653. * @version february 2006, dokeos 1.8
  2654. */
  2655. function move_thread_form() {
  2656. global $origin;
  2657. $gradebook=Security::remove_XSS($_GET['gradebook']);
  2658. // initiate the object
  2659. $form = new FormValidator('movepost', 'post', api_get_self().'?forum='.Security::remove_XSS($_GET['forum']).'&gradebook='.$gradebook.'&thread='.Security::remove_XSS($_GET['thread']).'&action='.Security::remove_XSS($_GET['action']).'&origin='.$origin);
  2660. // the header for the form
  2661. $form->addElement('header', '', get_lang('MoveThread'));
  2662. // invisible form: the thread_id
  2663. $form->addElement('hidden', 'thread_id', strval(intval($_GET['thread']))); // note: this has to be cleaned first
  2664. // the fora
  2665. $forum_categories=get_forum_categories();
  2666. $forums=get_forums();
  2667. $htmlcontent .= ' <div class="row">
  2668. <div class="label">
  2669. <span class="form_required">*</span>'.get_lang('MoveTo').'
  2670. </div>
  2671. <div class="formw">';
  2672. $htmlcontent .= "<SELECT NAME='forum'>\n";
  2673. foreach ($forum_categories as $key=>$category) {
  2674. $htmlcontent.="\t<OPTGROUP LABEL=\"".$category['cat_title']."\">\n";
  2675. foreach ($forums as $key=>$forum) {
  2676. if ($forum['forum_category']==$category['cat_id']) {
  2677. $htmlcontent.="\t\t<OPTION VALUE='".$forum['forum_id']."'>".$forum['forum_title']."</OPTION>\n";
  2678. }
  2679. }
  2680. $htmlcontent.="\t</OPTGROUP>\n";
  2681. }
  2682. $htmlcontent.="</SELECT>\n";
  2683. $htmlcontent .= ' </div>
  2684. </div>';
  2685. $form->addElement('html',$htmlcontent);
  2686. // The OK button
  2687. $form->addElement('style_submit_button', 'SubmitForum',get_lang('MoveThread'),'class="save"');
  2688. // The validation or display
  2689. if( $form->validate()) {
  2690. $values = $form->exportValues();
  2691. if (isset($_POST['forum'])) {
  2692. store_move_thread($values);
  2693. }
  2694. } else {
  2695. $form->display();
  2696. }
  2697. }
  2698. /**
  2699. * This function displays the form for moving a post message to a different (already existing) or a new thread.
  2700. *
  2701. * @param
  2702. * @return
  2703. *
  2704. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2705. * @version february 2006, dokeos 1.8
  2706. */
  2707. function move_post_form() {
  2708. global $origin;
  2709. $gradebook=Security::remove_XSS($_GET['gradebook']);
  2710. // initiate the object
  2711. $form = new FormValidator('movepost', 'post', api_get_self().'?forum='.Security::remove_XSS($_GET['forum']).'&thread='.Security::remove_XSS($_GET['thread']).'&origin='.$origin.'&gradebook='.$gradebook.'&post='.Security::remove_XSS($_GET['post']).'&action='.Security::remove_XSS($_GET['action']).'&post='.Security::remove_XSS($_GET['post']));
  2712. // the header for the form
  2713. $form->addElement('header', '', get_lang('MovePost'));
  2714. // invisible form: the post_id
  2715. $form->addElement('hidden', 'post_id', strval(intval($_GET['post']))); // note: this has to be cleaned first
  2716. // dropdown list: Threads of this forum
  2717. $threads=get_threads(strval(intval($_GET['forum']))); // note: this has to be cleaned
  2718. //my_print_r($threads);
  2719. $threads_list[0]=get_lang('ANewThread');
  2720. foreach ($threads as $key=>$value) {
  2721. $threads_list[$value['thread_id']]=$value['thread_title'];
  2722. }
  2723. $form->addElement('select', 'thread', get_lang('MoveToThread'), $threads_list);
  2724. $form->applyFilter('thread', 'html_filter');
  2725. // The OK button
  2726. $form->addElement('style_submit_button', 'submit',get_lang('MovePost'),'class="save"');
  2727. // setting the rules
  2728. $form->addRule('thread', get_lang('ThisFieldIsRequired'), 'required');
  2729. // The validation or display
  2730. if( $form->validate() ) {
  2731. $values = $form->exportValues();
  2732. store_move_post($values);
  2733. } else {
  2734. $form->display();
  2735. }
  2736. }
  2737. /**
  2738. *
  2739. * @param
  2740. * @return
  2741. *
  2742. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2743. * @version february 2006, dokeos 1.8
  2744. */
  2745. function store_move_post($values) {
  2746. global $table_posts;
  2747. global $table_threads;
  2748. global $table_forums;
  2749. global $_course;
  2750. if ($values['thread']=='0') {
  2751. $current_post=get_post_information($values['post_id']);
  2752. // storing a new thread
  2753. $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_last_post, thread_date)
  2754. VALUES (
  2755. '".Database::escape_string($current_post['post_title'])."',
  2756. '".Database::escape_string($current_post['forum_id'])."',
  2757. '".Database::escape_string($current_post['poster_id'])."',
  2758. '".Database::escape_string($current_post['poster_name'])."',
  2759. '".Database::escape_string($values['post_id'])."',
  2760. '".Database::escape_string($current_post['post_date'])."'
  2761. )";
  2762. $result=api_sql_query($sql, __FILE__, __LINE__);
  2763. $new_thread_id=Database::get_last_insert_id();
  2764. api_item_property_update($_course, TOOL_FORUM_THREAD, $new_thread_id,"visible", $current_post['poster_id']);
  2765. // moving the post to the newly created thread
  2766. $sql="UPDATE $table_posts SET thread_id='".Database::escape_string($new_thread_id)."', post_parent_id='0' WHERE post_id='".Database::escape_string($values['post_id'])."'";
  2767. $result=api_sql_query($sql,__FILE__, __LINE__);
  2768. //echo $sql.'<br />';
  2769. // resetting the parent_id of the thread to 0 for all those who had this moved post as parent
  2770. $sql="UPDATE $table_posts SET post_parent_id='0' WHERE post_parent_id='".Database::escape_string($values['post_id'])."'";
  2771. $result=api_sql_query($sql, __FILE__, __LINE__);
  2772. //echo $sql.'<br />';
  2773. // updating updating the number of threads in the forum
  2774. $sql="UPDATE $table_forums SET forum_threads=forum_threads+1 WHERE forum_id='".Database::escape_string($current_post['forum_id'])."'";
  2775. $result=api_sql_query($sql, __FILE__, __LINE__);
  2776. //echo $sql.'<br />';
  2777. // resetting the last post of the old thread and decreasing the number of replies and the thread
  2778. $sql="SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($current_post['thread_id'])."' ORDER BY post_id DESC";
  2779. //echo $sql.'<br />';
  2780. $result=api_sql_query($sql, __FILE__, __LINE__);
  2781. $row=Database::fetch_array($result);
  2782. //my_print_r($row);
  2783. $sql="UPDATE $table_threads SET thread_last_post='".$row['post_id']."', thread_replies=thread_replies-1 WHERE thread_id='".Database::escape_string($current_post['thread_id'])."'";
  2784. $result=api_sql_query($sql, __FILE__, __LINE__);
  2785. //echo $sql.'<br />';
  2786. } else {
  2787. // moving to the chosen thread
  2788. $sql="UPDATE $table_posts SET thread_id='".Database::escape_string($_POST['thread'])."', post_parent_id='0' WHERE post_id='".Database::escape_string($values['post_id'])."'";
  2789. $result=api_sql_query($sql, __FILE__, __LINE__);
  2790. // resetting the parent_id of the thread to 0 for all those who had this moved post as parent
  2791. $sql="UPDATE $table_posts SET post_parent_id='0' WHERE post_parent_id='".Database::escape_string($values['post_id'])."'";
  2792. $result=api_sql_query($sql, __FILE__, __LINE__);
  2793. }
  2794. return get_lang('ThreadMoved');
  2795. }
  2796. /**
  2797. *
  2798. * @param
  2799. * @return
  2800. *
  2801. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2802. * @version february 2006, dokeos 1.8
  2803. */
  2804. function store_move_thread($values) {
  2805. global $table_posts;
  2806. global $table_threads;
  2807. global $table_forums;
  2808. global $_course;
  2809. // change the thread table: setting the forum_id to the new forum
  2810. $sql="UPDATE $table_threads SET forum_id='".Database::escape_string($_POST['forum'])."' WHERE thread_id='".Database::escape_string($_POST['thread_id'])."'";
  2811. $result=api_sql_query($sql, __FILE__, __LINE__);
  2812. // changing all the posts of the thread: setting the forum_id to the new forum
  2813. $sql="UPDATE $table_posts SET forum_id='".Database::escape_string($_POST['forum'])."' WHERE thread_id='".Database::escape_string($_POST['thread_id'])."'";
  2814. $result=api_sql_query($sql, __FILE__, __LINE__);
  2815. return get_lang('ThreadMoved');
  2816. }
  2817. /**
  2818. * Prepares a string or an array of strings for display by stripping slashes
  2819. * @param mixed String or array of strings
  2820. * @return mixed String or array of strings
  2821. *
  2822. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2823. * @version february 2006, dokeos 1.8
  2824. */
  2825. function prepare4display($input='') {
  2826. $highlightcolors = array('yellow', '#33CC33','#3399CC', '#9999FF', '#33CC33');
  2827. if (!is_array($input)) {
  2828. if (!empty($_GET['search'])) {
  2829. if (strstr($_GET['search'],'+')) {
  2830. $search_terms = explode('+',$_GET['search']);
  2831. } else {
  2832. $search_terms[] = trim($_GET['search']);
  2833. }
  2834. $counter = 0;
  2835. foreach ($search_terms as $key=>$search_term) {
  2836. $input = str_replace(trim(api_html_entity_decode($search_term)),'<span style="background-color: '.$highlightcolors[$counter].'">'.trim(api_html_entity_decode($search_term)).'</span>',$input);
  2837. $counter++;
  2838. }
  2839. }
  2840. return api_html_entity_decode(stripslashes($input));
  2841. } else {
  2842. /*foreach ($input as $key=>$value)
  2843. {
  2844. $returnarray[$key]=stripslashes($value);
  2845. }*/
  2846. $returnarray=array_walk($input, 'api_html_entity_decode');
  2847. $returnarray=array_walk($input, 'stripslashes');
  2848. return $returnarray;
  2849. }
  2850. }
  2851. /**
  2852. * Display the search form for the forum and display the search results
  2853. *
  2854. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  2855. * @version march 2008, dokeos 1.8.5
  2856. */
  2857. function forum_search() {
  2858. global $origin;
  2859. // initiate the object
  2860. $form = new FormValidator('forumsearch','post','forumsearch.php?origin='.$origin.'');
  2861. // settting the form elements
  2862. $form->addElement('header', '', get_lang('ForumSearch'));
  2863. $form->addElement('text', 'search_term', get_lang('SearchTerm'),'class="input_titles"');
  2864. $form->applyFilter('search_term', 'html_filter');
  2865. $form->addElement('static', 'search_information', '', get_lang('ForumSearchInformation')/*, $dissertation[$_GET['opleidingsonderdeelcode']]['code']*/);
  2866. $form->addElement('style_submit_button', 'SubmitForumSearch', get_lang('Search'), 'class="search"');
  2867. // setting the rules
  2868. $form->addRule('search_term', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  2869. $form->addRule('search_term', get_lang('TooShort'),'minlength',3);
  2870. // The validation or display
  2871. if( $form->validate() ) {
  2872. $values = $form->exportValues();
  2873. $form->setDefaults($values);
  2874. $form->display();
  2875. // display the search results
  2876. display_forum_search_results($values['search_term']);
  2877. } else {
  2878. $form->display();
  2879. }
  2880. }
  2881. /**
  2882. * Display the search results
  2883. *
  2884. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  2885. * @version march 2008, dokeos 1.8.5
  2886. */
  2887. function display_forum_search_results($search_term) {
  2888. global $table_categories, $table_forums, $table_threads, $table_posts;
  2889. global $origin;
  2890. $gradebook=Security::remove_XSS($_GET['gradebook']);
  2891. // defining the search strings as an array
  2892. if (strstr($search_term,'+')) {
  2893. $search_terms = explode('+',$search_term);
  2894. } else {
  2895. $search_terms[] = $search_term;
  2896. }
  2897. // search restriction
  2898. foreach ($search_terms as $key => $value) {
  2899. $search_restriction[] = "(posts.post_title LIKE '%".Database::escape_string(trim($value))."%'
  2900. OR posts.post_text LIKE '%".Database::escape_string(trim($value))."%')";
  2901. }
  2902. $sql = "SELECT * FROM $table_posts posts
  2903. WHERE ".implode(' AND ',$search_restriction)."
  2904. /*AND posts.thread_id = threads.thread_id*/
  2905. GROUP BY posts.post_id";
  2906. // getting all the information of the forum categories
  2907. $forum_categories_list=get_forum_categories();
  2908. // getting all the information of the forums
  2909. $forum_list=get_forums();
  2910. $result = api_sql_query($sql, __FILE__, __LINE__);
  2911. while ($row = Database::fetch_array($result,'ASSOC')) {
  2912. $display_result = false;
  2913. /*
  2914. we only show it when
  2915. 1. forum cateogory is visible
  2916. 2. forum is visible
  2917. 3. thread is visible (to do)
  2918. 4. post is visible
  2919. */
  2920. if (!api_is_allowed_to_edit()) {
  2921. if ($forum_categories_list[$row['forum_id']['forum_category']]['visibility'] == '1' AND $forum_list[$row['forum_id']]['visibility'] == '1' AND $row['visible'] == '1') {
  2922. $display_result = true;
  2923. }
  2924. } else {
  2925. $display_result = true;
  2926. }
  2927. if ($display_result == true) {
  2928. $search_results_item = '<li><a href="viewforumcategory.php?forumcategory='.$forum_list[$row['forum_id']]['forum_category'].'&origin='.$origin.'&amp;search='.urlencode($search_term).'">'.$forum_categories_list[$row['forum_id']['forum_category']]['cat_title'].'</a> > ';
  2929. $search_results_item .= '<a href="viewforum.php?forum='.$row['forum_id'].'&amp;origin='.$origin.'&amp;search='.urlencode($search_term).'">'.$forum_list[$row['forum_id']]['forum_title'].'</a> > ';
  2930. //$search_results_item .= '<a href="">THREAD</a> > ';
  2931. $search_results_item .= '<a href="viewthread.php?forum='.$row['forum_id'].'&gradebook='.$gradebook.'&amp;origin='.$origin.'&amp;thread='.$row['thread_id'].'&amp;search='.urlencode($search_term).'">'.$row['post_title'].'</a>';
  2932. $search_results_item .= '<br />';
  2933. if (api_strlen($row['post_title']) > 200 ) {
  2934. $search_results_item .= api_substr(strip_tags($row['post_title']),0,200).'...';
  2935. } else {
  2936. $search_results_item .= $row['post_title'];
  2937. }
  2938. $search_results_item .= '</li>';
  2939. $search_results[] = $search_results_item;
  2940. }
  2941. }
  2942. echo '<div class="row"><div class="form_header">'.count($search_results).' '.get_lang('ForumSearchResults').'</div></div>';
  2943. echo '<ol>';
  2944. if($search_results) {
  2945. echo implode($search_results);
  2946. }
  2947. echo '</ol>';
  2948. }
  2949. /**
  2950. * Return the link to the forum search page
  2951. *
  2952. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  2953. * @version April 2008, dokeos 1.8.5
  2954. */
  2955. function search_link() {
  2956. global $origin;
  2957. $return = '';
  2958. if ($origin != 'learnpath') {
  2959. $return = '<a href="forumsearch.php?'.api_get_cidreq().'&action=search&origin='.$origin.'"> '.Display::return_icon('search.gif', get_lang('Search')).' '.get_lang('Search').'</a>';
  2960. if (!empty($_GET['search'])) {
  2961. $return .= ': '.Security::remove_XSS($_GET['search']).' ';
  2962. $url = api_get_self().'?';
  2963. foreach ($_GET as $key=>$value) {
  2964. if ($key<>'search') {
  2965. $url_parameter[]=Security::remove_XSS($key).'='.Security::remove_XSS($value);
  2966. }
  2967. }
  2968. $url = $url.implode('&amp;',$url_parameter);
  2969. $return .= '<a href="'.$url.'">'.Display::return_icon('delete.gif', get_lang('RemoveSearchResults')).'</a>';
  2970. }
  2971. }
  2972. return $return;
  2973. }
  2974. /**
  2975. * This function add a attachment file into forum
  2976. * @param string a comment about file
  2977. * @param int last id from forum_post table
  2978. *
  2979. */
  2980. function add_forum_attachment_file($file_comment,$last_id) {
  2981. global $_course;
  2982. $agenda_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  2983. // Storing the attachments
  2984. if(!empty($_FILES['user_upload']['name'])) {
  2985. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  2986. }
  2987. if (!empty($upload_ok)) {
  2988. $courseDir = $_course['path'].'/upload/forum';
  2989. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2990. $updir = $sys_course_path.$courseDir;
  2991. // Try to add an extension to the file if it hasn't one
  2992. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  2993. // user's file name
  2994. $file_name =$_FILES['user_upload']['name'];
  2995. if (!filter_extension($new_file_name)) {
  2996. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  2997. } else {
  2998. $new_file_name = uniqid('');
  2999. $new_path=$updir.'/'.$new_file_name;
  3000. $result= @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  3001. $safe_file_comment= Database::escape_string($file_comment);
  3002. $safe_file_name = Database::escape_string($file_name);
  3003. $safe_new_file_name = Database::escape_string($new_file_name);
  3004. // Storing the attachments if any
  3005. if ($result) {
  3006. $sql="INSERT INTO $agenda_forum_attachment(filename,comment, path,post_id,size)
  3007. VALUES ( '$safe_file_name', '$safe_file_comment', '$safe_new_file_name' , '$last_id', '".$_FILES['user_upload']['size']."' )";
  3008. $result=api_sql_query($sql, __LINE__, __FILE__);
  3009. $message.=' / '.get_lang('FileUploadSucces').'<br />';
  3010. $last_id_file=Database::insert_id();
  3011. api_item_property_update($_course, TOOL_FORUM_ATTACH, $last_id_file ,'ForumAttachmentAdded', api_get_user_id());
  3012. }
  3013. }
  3014. }
  3015. }
  3016. /**
  3017. * This function edit a attachment file into forum
  3018. * @param string a comment about file
  3019. * @param int Post Id
  3020. * @param int attachment file Id
  3021. */
  3022. function edit_forum_attachment_file($file_comment,$post_id,$id_attach) {
  3023. global $_course;
  3024. $table_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  3025. // Storing the attachments
  3026. if(!empty($_FILES['user_upload']['name'])) {
  3027. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  3028. }
  3029. if (!empty($upload_ok)) {
  3030. $courseDir = $_course['path'].'/upload/forum';
  3031. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  3032. $updir = $sys_course_path.$courseDir;
  3033. // Try to add an extension to the file if it hasn't one
  3034. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  3035. // user's file name
  3036. $file_name =$_FILES['user_upload']['name'];
  3037. if (!filter_extension($new_file_name)) {
  3038. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  3039. } else {
  3040. $new_file_name = uniqid('');
  3041. $new_path=$updir.'/'.$new_file_name;
  3042. $result= @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  3043. $safe_file_comment= Database::escape_string($file_comment);
  3044. $safe_file_name = Database::escape_string($file_name);
  3045. $safe_new_file_name = Database::escape_string($new_file_name);
  3046. $safe_post_id = (int)$post_id;
  3047. $safe_id_attach = (int)$id_attach;
  3048. // Storing the attachments if any
  3049. if ($result) {
  3050. $sql="UPDATE $table_forum_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', post_id = '$safe_post_id', size ='".$_FILES['user_upload']['size']."'
  3051. WHERE id = '$safe_id_attach'";
  3052. $result=api_sql_query($sql, __LINE__, __FILE__);
  3053. api_item_property_update($_course, TOOL_FORUM_ATTACH, $safe_id_attach ,'ForumAttachmentUpdated', api_get_user_id());
  3054. }
  3055. }
  3056. }
  3057. }
  3058. /**
  3059. * Show a list with all the attachments according to the post's id
  3060. * @param the post's id
  3061. * @return array with the post info
  3062. * @author Julio Montoya Dokeos
  3063. * @version avril 2008, dokeos 1.8.5
  3064. */
  3065. function get_attachment($post_id) {
  3066. global $forum_table_attachment;
  3067. $row=array();
  3068. $sql = 'SELECT id, path, filename,comment FROM '. $forum_table_attachment.' WHERE post_id ="'.$post_id.'"';
  3069. $result=api_sql_query($sql, __FILE__, __LINE__);
  3070. if (Database::num_rows($result)!=0) {
  3071. $row=Database::fetch_array($result);
  3072. }
  3073. return $row;
  3074. }
  3075. /**
  3076. * Delete the all the attachments from the DB and the file according to the post's id or attach id(optional)
  3077. * @param post id
  3078. * @param attach id (optional)
  3079. * @author Julio Montoya Dokeos
  3080. * @version avril 2008, dokeos 1.8.5
  3081. */
  3082. function delete_attachment($post_id,$id_attach=0) {
  3083. global $_course;
  3084. $forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  3085. $cond = (!empty($id_attach))?" id = ".(int)$id_attach."" : " post_id = ".(int)$post_id."";
  3086. $sql="SELECT path FROM $forum_table_attachment WHERE $cond";
  3087. $res=api_sql_query($sql,__FILE__,__LINE__);
  3088. $row=Database::fetch_array($res);
  3089. $courseDir = $_course['path'].'/upload/forum';
  3090. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  3091. $updir = $sys_course_path.$courseDir;
  3092. $my_path =isset($row['path']) ? $row['path'] : null;
  3093. $file =$updir.'/'.$my_path;
  3094. if (Security::check_abs_path($file,$updir) ) {
  3095. @unlink($file);
  3096. }
  3097. //Delete from forum_attachment table
  3098. $sql="DELETE FROM $forum_table_attachment WHERE $cond ";
  3099. $result=api_sql_query($sql, __LINE__, __FILE__);
  3100. $last_id_file=Database::insert_id();
  3101. // update item_property
  3102. api_item_property_update($_course, TOOL_FORUM_ATTACH, $id_attach ,'ForumAttachmentDelete', api_get_user_id());
  3103. if (!empty($result) && !empty($id_attach)) {
  3104. $message=get_lang(get_lang('AttachmentFileDeleteSuccess'));
  3105. Display::display_confirmation_message($message);
  3106. }
  3107. }
  3108. /**
  3109. * This function gets all the forum information of the all the forum of the group
  3110. *
  3111. * @param integer $group_id the id of the group we need the fora of (see forum.forum_of_group)
  3112. * @return array
  3113. *
  3114. * @todo this is basically the same code as the get_forums function. Consider merging the two.
  3115. */
  3116. function get_forums_of_group($group_id) {
  3117. global $table_forums;
  3118. global $table_threads;
  3119. global $table_posts;
  3120. global $table_item_property;
  3121. global $table_users;
  3122. //-------------- Student -----------------//
  3123. // select all the forum information of all forums (that are visible to students)
  3124. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  3125. WHERE forum.forum_of_group = '".Database::escape_string($group_id)."'
  3126. AND forum.forum_id=item_properties.ref
  3127. AND item_properties.visibility=1
  3128. AND item_properties.tool='".TOOL_FORUM."'
  3129. ORDER BY forum.forum_order ASC";
  3130. // select the number of threads of the forums (only the threads that are visible)
  3131. $sql2="SELECT count(thread_id) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties
  3132. WHERE threads.thread_id=item_properties.ref
  3133. AND item_properties.visibility=1
  3134. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  3135. GROUP BY threads.forum_id";
  3136. // select the number of posts of the forum (post that are visible and that are in a thread that is visible)
  3137. $sql3="SELECT count(post_id) AS number_of_posts, posts.forum_id FROM $table_posts posts, $table_threads threads, ".$table_item_property." item_properties
  3138. WHERE posts.visible=1
  3139. AND posts.thread_id=threads.thread_id
  3140. AND threads.thread_id=item_properties.ref
  3141. AND item_properties.visibility=1
  3142. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  3143. GROUP BY threads.forum_id";
  3144. //-------------- Course Admin -----------------//
  3145. if (is_allowed_to_edit()) {
  3146. // select all the forum information of all forums (that are not deleted)
  3147. $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties
  3148. WHERE forum.forum_of_group = '".Database::escape_string($group_id)."'
  3149. AND forum.forum_id=item_properties.ref
  3150. AND item_properties.visibility<>2
  3151. AND item_properties.tool='".TOOL_FORUM."'
  3152. ORDER BY forum_order ASC";
  3153. //echo $sql.'<hr>';
  3154. // select the number of threads of the forums (only the threads that are not deleted)
  3155. $sql2="SELECT count(thread_id) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties
  3156. WHERE threads.thread_id=item_properties.ref
  3157. AND item_properties.visibility<>2
  3158. AND item_properties.tool='".TOOL_FORUM_THREAD."'
  3159. GROUP BY threads.forum_id";
  3160. //echo $sql2.'<hr>';
  3161. // select the number of posts of the forum
  3162. $sql3="SELECT count(post_id) AS number_of_posts, forum_id FROM $table_posts GROUP BY forum_id";
  3163. //echo $sql3.'<hr>';
  3164. }
  3165. // handling all the forum information
  3166. $result=api_sql_query($sql, __FILE__, __LINE__);
  3167. while ($row=Database::fetch_array($result,'ASSOC')) {
  3168. $forum_list[$row['forum_id']]=$row;
  3169. }
  3170. // handling the threadcount information
  3171. $result2=api_sql_query($sql2, __FILE__, __LINE__);
  3172. while ($row2=Database::fetch_array($result2,'ASSOC')) {
  3173. if (is_array($forum_list)) {
  3174. if (array_key_exists($row2['forum_id'],$forum_list)) {
  3175. $forum_list[$row2['forum_id']]['number_of_threads']=$row2['number_of_threads'];
  3176. }
  3177. }
  3178. }
  3179. // handling the postcount information
  3180. $result3=api_sql_query($sql3, __FILE__, __LINE__);
  3181. while ($row3=Database::fetch_array($result3,'ASSOC')) {
  3182. if (is_array($forum_list)) {
  3183. if (array_key_exists($row3['forum_id'],$forum_list)) {// this is needed because sql3 takes also the deleted forums into account
  3184. $forum_list[$row3['forum_id']]['number_of_posts']=$row3['number_of_posts'];
  3185. }
  3186. }
  3187. }
  3188. // finding the last post information (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)
  3189. if (is_array($forum_list)) {
  3190. foreach ($forum_list as $key=>$value) {
  3191. $last_post_info_of_forum=get_last_post_information($key,is_allowed_to_edit());
  3192. $forum_list[$key]['last_post_id']=$last_post_info_of_forum['last_post_id'];
  3193. $forum_list[$key]['last_poster_id']=$last_post_info_of_forum['last_poster_id'];
  3194. $forum_list[$key]['last_post_date']=$last_post_info_of_forum['last_post_date'];
  3195. $forum_list[$key]['last_poster_name']=$last_post_info_of_forum['last_poster_name'];
  3196. $forum_list[$key]['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname'];
  3197. $forum_list[$key]['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname'];
  3198. }
  3199. }
  3200. return $forum_list;
  3201. }
  3202. /**
  3203. * This function stores which users have to be notified of which forums or threads
  3204. *
  3205. * @param string $content does the user want to be notified about a forum or about a thread
  3206. * @param integer $id the id of the forum or thread
  3207. *
  3208. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  3209. * @version May 2008, dokeos 1.8.5
  3210. * @since May 2008, dokeos 1.8.5
  3211. */
  3212. function set_notification($content,$id, $add_only = false) {
  3213. global $_user;
  3214. // which database field do we have to store the id in?
  3215. if ($content == 'forum') {
  3216. $database_field = 'forum_id';
  3217. } else {
  3218. $database_field = 'thread_id';
  3219. }
  3220. // database table definition
  3221. $table_notification = Database::get_course_table('forum_notification');
  3222. // first we check if the notification is already set for this
  3223. $sql = "SELECT * FROM $table_notification WHERE $database_field = '".Database::escape_string($id)."' AND user_id = '".Database::escape_string($_user['user_id'])."'";
  3224. $result=api_sql_query($sql, __FILE__, __LINE__);
  3225. $total = mysql_num_rows($result);
  3226. // if the user did not indicate that (s)he wanted to be notified already then we store the notification request (to prevent double notification requests)
  3227. if ($total <= 0) {
  3228. $sql = "INSERT INTO $table_notification ($database_field, user_id) VALUES ('".Database::escape_string($id)."','".Database::escape_string($_user['user_id'])."')";
  3229. $result=api_sql_query($sql, __FILE__, __LINE__);
  3230. api_session_unregister('forum_notification');
  3231. get_notifications_of_user(0,true);
  3232. return get_lang('YouWillBeNotifiedOfNewPosts');
  3233. } else {
  3234. if (!$add_only) {
  3235. $sql = "DELETE FROM $table_notification WHERE $database_field = '".Database::escape_string($id)."' AND user_id = '".Database::escape_string($_user['user_id'])."'";
  3236. $result=api_sql_query($sql, __FILE__, __LINE__);
  3237. api_session_unregister('forum_notification');
  3238. get_notifications_of_user(0,true);
  3239. return get_lang('YouWillNoLongerBeNotifiedOfNewPosts');
  3240. }
  3241. }
  3242. }
  3243. /**
  3244. * This function retrieves all the email adresses of the users who wanted to be notified
  3245. * about a new post in a certain forum or thread
  3246. *
  3247. * @param string $content does the user want to be notified about a forum or about a thread
  3248. * @param integer $id the id of the forum or thread
  3249. *
  3250. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  3251. * @version May 2008, dokeos 1.8.5
  3252. * @since May 2008, dokeos 1.8.5
  3253. */
  3254. function get_notifications($content,$id) {
  3255. global $table_users;
  3256. // which database field contains the notification?
  3257. if ($content == 'forum') {
  3258. $database_field = 'forum_id';
  3259. } else {
  3260. $database_field = 'thread_id';
  3261. }
  3262. // database table definition
  3263. $table_notification = Database::get_course_table('forum_notification');
  3264. $sql = "SELECT user.user_id, user.firstname, user.lastname, user.email, user.user_id user FROM $table_users user, $table_notification notification
  3265. WHERE user.user_id = notification.user_id
  3266. AND notification.$database_field= '".Database::escape_string($id)."'";
  3267. $result=api_sql_query($sql, __FILE__, __LINE__);
  3268. $return = array();
  3269. while ($row=Database::fetch_array($result)) {
  3270. $return['user'.$row['user_id']]=array('email' => $row['email'], 'user_id' => $row['user_id']);
  3271. }
  3272. return $return;
  3273. }
  3274. /**
  3275. * Get all the users who need to receive a notification of a new post (those subscribed to
  3276. * the forum or the thread)
  3277. *
  3278. * @param integer $forum_id the id of the forum
  3279. * @param integer $thread_id the id of the thread
  3280. * @param integer $post_id the id of the post
  3281. * @return unknown
  3282. *
  3283. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  3284. * @version May 2008, dokeos 1.8.5
  3285. * @since May 2008, dokeos 1.8.5
  3286. */
  3287. function send_notifications($forum_id=0, $thread_id=0, $post_id=0) {
  3288. global $_course;
  3289. // the content of the mail
  3290. $email_subject = get_lang('NewForumPost')." - ".$_course['official_code'];
  3291. $thread_link= api_get_path('WEB_CODE_PATH').'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum_id.'&thread='.$thread_id;
  3292. $my_link=isset($link)?$link:'';
  3293. $my_message=isset($message)?$message:'';
  3294. $my_message .= $my_link;
  3295. // users who subscribed to the forum
  3296. if ($forum_id<>0) {
  3297. $users_to_be_notified_by_forum = get_notifications('forum',$forum_id);
  3298. } else {
  3299. return false;
  3300. }
  3301. // user who subscribed to the thread
  3302. if ($thread_id<>0) {
  3303. $users_to_be_notified_by_thread = get_notifications('thread',$thread_id);
  3304. }
  3305. // merging the two
  3306. $users_to_be_notified = array_merge($users_to_be_notified_by_forum, $users_to_be_notified_by_thread);
  3307. if (is_array($users_to_be_notified)) {
  3308. foreach ($users_to_be_notified as $key=>$value) {
  3309. if ($value['email'] <> $_user['email']) {
  3310. $email_body= $value['firstname']." ".$value['lastname']."\n\r";
  3311. $email_body .= '['.$_course['official_code'].'] - ['.$_course['name']."]<br>\n";
  3312. $email_body .= get_lang('NewForumPost')."\n";
  3313. $email_body .= get_lang('YouWantedToStayInformed')."<br><br>\n";
  3314. $email_body .= get_lang('ThreadCanBeFoundHere')." : <a href=\"".$thread_link."\">".$thread_link."</a>\n";
  3315. //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first)
  3316. //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case)
  3317. if(empty($charset)) {
  3318. $charset='ISO-8859-1';
  3319. }
  3320. $newmail = api_mail_html($value['lastname'].' '.$value['firstname'], $value['email'], $email_subject, $email_body, $_SESSION['_user']['lastName'].' '.$_SESSION['_user']['firstName'], $_SESSION['_user']['mail']);
  3321. }
  3322. }
  3323. }
  3324. }
  3325. /**
  3326. * Get all the notification subscriptions of the user
  3327. * = which forums and which threads does the user wants to be informed of when a new
  3328. * post is added to this thread
  3329. *
  3330. * @param integer $user_id the user_id of a user (default = 0 => the current user)
  3331. * @param boolean $force force get the notification subscriptions (even if the information is already in the session
  3332. *
  3333. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  3334. * @version May 2008, dokeos 1.8.5
  3335. * @since May 2008, dokeos 1.8.5
  3336. */
  3337. function get_notifications_of_user($user_id = 0, $force = false) {
  3338. global $_course;
  3339. $course = api_get_course_id();
  3340. if (empty($course) OR $course==-1) {
  3341. return null;
  3342. }
  3343. if ($user_id == 0) {
  3344. global $_user;
  3345. $user_id = $_user['user_id'];
  3346. }
  3347. // database table definition
  3348. $table_notification = Database::get_course_table('forum_notification');
  3349. $my_code = isset($_course['code']) ? $_course['code'] : '';
  3350. if (!isset($_SESSION['forum_notification']) OR $_SESSION['forum_notification']['course'] <> $my_code OR $force=true) {
  3351. $_SESSION['forum_notification']['course'] = $my_code;
  3352. $sql = "SELECT * FROM $table_notification WHERE user_id='".Database::escape_string($user_id)."'";
  3353. $result=api_sql_query($sql, __FILE__, __LINE__);
  3354. while ($row=Database::fetch_array($result)) {
  3355. if (!is_null($row['forum_id'])) {
  3356. $_SESSION['forum_notification']['forum'][] = $row['forum_id'];
  3357. }
  3358. if (!is_null($row['thread_id'])) {
  3359. $_SESSION['forum_notification']['thread'][] = $row['thread_id'];
  3360. }
  3361. }
  3362. }
  3363. }
  3364. /**
  3365. * This function counts the number of post inside a thread
  3366. * @param int Thread ID
  3367. * @return int the number of post inside a thread
  3368. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  3369. * @version octubre 2008, dokeos 1.8
  3370. */
  3371. function count_number_of_post_in_thread($thread_id) {
  3372. global $table_posts;
  3373. $sql = "SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($thread_id)."' ";
  3374. $result = api_sql_query($sql, __FILE__, __LINE__);
  3375. return count(api_store_result($result));
  3376. }
  3377. /**
  3378. * This function counts the number of post inside a thread user
  3379. * @param int Thread ID
  3380. * @param int User ID
  3381. * @return int the number of post inside a thread user
  3382. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  3383. * @version octubre 2008, dokeos 1.8
  3384. */
  3385. function count_number_of_post_for_user_thread($thread_id, $user_id) {
  3386. global $table_posts;
  3387. $sql = "SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($thread_id)."'
  3388. AND poster_id = '".Database::escape_string($user_id)."' ";
  3389. $result = api_sql_query($sql, __FILE__, __LINE__);
  3390. return count(api_store_result($result));
  3391. }
  3392. /**
  3393. * This function counts the number of user register in course
  3394. * @param int Course ID
  3395. * @return int the number of user register in course
  3396. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  3397. * @version octubre 2008, dokeos 1.8
  3398. */
  3399. function count_number_of_user_in_course($course_id) {
  3400. $table_course_rel_user = Database::get_main_table("course_rel_user");
  3401. $sql = "SELECT * FROM $table_course_rel_user WHERE course_code ='".Database::escape_string($course_id)."' ";
  3402. $result = api_sql_query($sql, __FILE__, __LINE__);
  3403. return count(api_store_result($result));
  3404. }
  3405. /**
  3406. * This function retrieves information of statistical
  3407. * @param int Thread ID
  3408. * @param int User ID
  3409. * @param int Course ID
  3410. * @return array the information of statistical
  3411. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  3412. * @version octubre 2008, dokeos 1.8
  3413. */
  3414. function get_statistical_information($thread_id, $user_id, $course_id) {
  3415. $stadistic = array();
  3416. $stadistic['user_course'] = count_number_of_user_in_course($course_id);
  3417. $stadistic['post'] = count_number_of_post_in_thread($thread_id);
  3418. $stadistic['user_post'] = count_number_of_post_for_user_thread($thread_id, $user_id);
  3419. //$stadistic['average'] = get_average_of_thread_post_user();
  3420. return $stadistic;
  3421. }
  3422. /**
  3423. * This function return the posts inside a thread from a given user
  3424. * @param course code
  3425. * @param int Thread ID
  3426. * @param int User ID
  3427. * @return int the number of post inside a thread
  3428. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  3429. * @version octubre 2008, dokeos 1.8
  3430. */
  3431. function get_thread_user_post($course_db, $thread_id, $user_id )
  3432. {
  3433. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db);
  3434. global $table_users;
  3435. $sql = "SELECT * FROM $table_posts posts
  3436. LEFT JOIN $table_users users
  3437. ON posts.poster_id=users.user_id
  3438. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  3439. AND posts.poster_id='".Database::escape_string($user_id)."'
  3440. ORDER BY posts.post_id ASC";
  3441. $result=api_sql_query($sql, __FILE__, __LINE__);
  3442. while ($row=Database::fetch_array($result)) {
  3443. $row['status'] = '1';
  3444. $post_list[]=$row;
  3445. $sql = "SELECT * FROM $table_posts posts
  3446. LEFT JOIN $table_users users
  3447. ON posts.poster_id=users.user_id
  3448. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  3449. AND posts.post_parent_id='".$row['post_id']."'
  3450. ORDER BY posts.post_id ASC";
  3451. $result2=api_sql_query($sql, __FILE__, __LINE__);
  3452. while ($row2=Database::fetch_array($result2))
  3453. {
  3454. $row2['status'] = '0';
  3455. $post_list[] = $row2;
  3456. }
  3457. }
  3458. return $post_list;
  3459. }
  3460. /* This function get the name of an user by id
  3461. * @param user_id int
  3462. * return String
  3463. * @author Christian Fasanando
  3464. */
  3465. function get_name_user_by_id($user_id) {
  3466. $t_users = Database :: get_main_table(TABLE_MAIN_USER);
  3467. $sql ="SELECT CONCAT(firstname,' ',lastname) FROM ".$t_users." WHERE user_id = '".$user_id."' ";
  3468. $result = api_sql_query($sql, __FILE__, __LINE__);
  3469. $row = Database::fetch_array($result);
  3470. return $row[0];
  3471. }
  3472. /** This function get the name of an thread by id
  3473. * @param int thread_id
  3474. * @return String
  3475. * @author Christian Fasanando
  3476. **/
  3477. function get_name_thread_by_id($thread_id) {
  3478. $t_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD,'');
  3479. $sql ="SELECT thread_title FROM ".$t_forum_thread." WHERE thread_id = '".$thread_id."' ";
  3480. $result = api_sql_query($sql, __FILE__, __LINE__);
  3481. $row = Database::fetch_array($result);
  3482. return $row[0];
  3483. }
  3484. /** This function gets all the post written by an user
  3485. * @param int user id
  3486. * @param string db course name
  3487. * @return string
  3488. * @author Christian Fasanando / J. M
  3489. **/
  3490. function get_all_post_from_user($user_id, $course_db)
  3491. {
  3492. $j=0;
  3493. $forums = get_forums();
  3494. krsort($forums);
  3495. $forum_results = '';
  3496. foreach($forums as $forum) {
  3497. if ($j<=4) {
  3498. $threads = get_threads($forum['forum_id']);
  3499. if (is_array($threads)) {
  3500. /*echo Display::return_icon('forum.gif');
  3501. echo $forum['forum_title'];*/
  3502. $my_course_db=explode('_',$course_db);
  3503. $my_course_code=$my_course_db[1];
  3504. $i=0;
  3505. $hand_forums = '';
  3506. $post_counter = 0;
  3507. foreach($threads as $thread) {
  3508. if ($i<=4) {
  3509. $post_list = get_thread_user_post_limit($course_db, $thread['thread_id'], $user_id, 1);
  3510. $post_counter = count($post_list);
  3511. if (is_array($post_list) && count($post_list)>0) {
  3512. $hand_forums.= '<div id="social-thread">';
  3513. $hand_forums.= Display::return_icon('forumthread.gif');
  3514. $hand_forums.= $thread['thread_title'].' ';
  3515. foreach($post_list as $posts) {
  3516. $hand_forums.= '<div id="social-post">';
  3517. $hand_forums.= '<strong>'.$posts['post_title'].'</strong>';
  3518. $hand_forums.= '<br / >';
  3519. $hand_forums.= cut($posts['post_text'], 150);
  3520. $hand_forums.= '</div>';
  3521. $hand_forums.= '<br / >';
  3522. }
  3523. }
  3524. $hand_forums.= '</div>';
  3525. }
  3526. $i++;
  3527. }
  3528. if ($post_counter > 0 ) {
  3529. $forum_results .='<div id="social-forum">';
  3530. $forum_results .='<div class="clear"></div><br />';
  3531. $forum_results .='<div class="actions" style="margin-left:5px;margin-right:5px;">'.Display::return_icon('forum.gif').'&nbsp;&nbsp;&nbsp;&nbsp;'.$forum['forum_title'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div style="float:right;margin-top:-18px"><a href="../forum/viewforum.php?cidReq='.$my_course_code.'&gidReq=&forum='.$forum['forum_id'].' " >'.get_lang('SeeForum').'</a></div></div>';
  3532. $forum_results .='<br / >';
  3533. $forum_results .=$hand_forums;
  3534. $forum_results .='</div>';
  3535. }
  3536. }
  3537. } $j++;
  3538. }
  3539. return $forum_results;
  3540. }
  3541. function get_thread_user_post_limit($course_db, $thread_id, $user_id, $limit=10)
  3542. {
  3543. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db);
  3544. global $table_users;
  3545. $sql = "SELECT * FROM $table_posts posts
  3546. LEFT JOIN $table_users users
  3547. ON posts.poster_id=users.user_id
  3548. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  3549. AND posts.poster_id='".Database::escape_string($user_id)."'
  3550. ORDER BY posts.post_id DESC LIMIT $limit ";
  3551. $result=api_sql_query($sql, __FILE__, __LINE__);
  3552. while ($row=Database::fetch_array($result)) {
  3553. $row['status'] = '1';
  3554. $post_list[]=$row;
  3555. }
  3556. return $post_list;
  3557. }