forumfunction.inc.php 201 KB

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