forumfunction.inc.php 166 KB

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