forumfunction.inc.php 165 KB

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