forumfunction.inc.php 169 KB

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