forumfunction.inc.php 244 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CForumPost;
  4. use Chamilo\CourseBundle\Entity\CForumThread;
  5. use ChamiloSession as Session;
  6. use Doctrine\Common\Collections\Criteria;
  7. /**
  8. * These files are a complete rework of the forum. The database structure is
  9. * based on phpBB but all the code is rewritten. A lot of new functionalities
  10. * are added:
  11. * - forum categories and forums can be sorted up or down, locked or made invisible
  12. * - consistent and integrated forum administration
  13. * - forum options: are students allowed to edit their post?
  14. * moderation of posts (approval)
  15. * reply only forums (students cannot create new threads)
  16. * multiple forums per group
  17. * - sticky messages
  18. * - new view option: nested view
  19. * - quoting a message.
  20. *
  21. * @package chamilo.forum
  22. *
  23. * @todo convert into a class
  24. */
  25. define('FORUM_NEW_POST', 0);
  26. getNotificationsPerUser();
  27. $htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']);
  28. $htmlHeadXtra[] = '<script>
  29. function check_unzip() {
  30. if (document.upload.unzip.checked){
  31. document.upload.if_exists[0].disabled=true;
  32. document.upload.if_exists[1].checked=true;
  33. document.upload.if_exists[2].disabled=true;
  34. } else {
  35. document.upload.if_exists[0].checked=true;
  36. document.upload.if_exists[0].disabled=false;
  37. document.upload.if_exists[2].disabled=false;
  38. }
  39. }
  40. function setFocus() {
  41. $("#title_file").focus();
  42. }
  43. </script>';
  44. // The next javascript script is to manage ajax upload file
  45. $htmlHeadXtra[] = api_get_jquery_libraries_js(['jquery-ui', 'jquery-upload']);
  46. // Recover Thread ID, will be used to generate delete attachment URL to do ajax
  47. $threadId = isset($_REQUEST['thread']) ? intval($_REQUEST['thread']) : 0;
  48. $forumId = isset($_REQUEST['forum']) ? intval($_REQUEST['forum']) : 0;
  49. $ajaxUrl = api_get_path(WEB_AJAX_PATH).'forum.ajax.php?'.api_get_cidreq();
  50. // The next javascript script is to delete file by ajax
  51. $htmlHeadXtra[] = '<script>
  52. $(function () {
  53. $(document).on("click", ".deleteLink", function(e) {
  54. e.preventDefault();
  55. e.stopPropagation();
  56. var l = $(this);
  57. var id = l.closest("tr").attr("id");
  58. var filename = l.closest("tr").find(".attachFilename").html();
  59. if (confirm("'.get_lang('AreYouSureToDeleteJS').'", filename)) {
  60. $.ajax({
  61. type: "POST",
  62. url: "'.$ajaxUrl.'&a=delete_file&attachId=" + id +"&thread='.$threadId.'&forum='.$forumId.'",
  63. dataType: "json",
  64. success: function(data) {
  65. if (data.error == false) {
  66. l.closest("tr").remove();
  67. if ($(".files td").length < 1) {
  68. $(".files").closest(".control-group").hide();
  69. }
  70. }
  71. }
  72. })
  73. }
  74. });
  75. });
  76. </script>';
  77. /**
  78. * This function handles all the forum and forum categories actions. This is a wrapper for the
  79. * forum and forum categories. All this code code could go into the section where this function is
  80. * called but this make the code there cleaner.
  81. *
  82. * @param int $lp_id Learning path Id
  83. *
  84. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  85. * @author Juan Carlos Raña Trabado (return to lp_id)
  86. *
  87. * @version may 2011, Chamilo 1.8.8
  88. */
  89. function handle_forum_and_forumcategories($lp_id = null)
  90. {
  91. $action_forum_cat = isset($_GET['action']) ? $_GET['action'] : '';
  92. $get_content = isset($_GET['content']) ? $_GET['content'] : '';
  93. $post_submit_cat = isset($_POST['SubmitForumCategory']) ? true : false;
  94. $post_submit_forum = isset($_POST['SubmitForum']) ? true : false;
  95. $get_id = isset($_GET['id']) ? intval($_GET['id']) : '';
  96. $forum_categories_list = get_forum_categories();
  97. // Verify if forum category exists
  98. if (empty($forum_categories_list)) {
  99. $get_content = 'forumcategory';
  100. }
  101. $content = '';
  102. // Adding a forum category
  103. if (($action_forum_cat == 'add' && $get_content == 'forumcategory') || $post_submit_cat) {
  104. $content = show_add_forumcategory_form([], $lp_id); //$lp_id when is called from learning path
  105. }
  106. // Adding a forum
  107. if ((($action_forum_cat == 'add' || $action_forum_cat == 'edit') && $get_content == 'forum') ||
  108. $post_submit_forum
  109. ) {
  110. $inputvalues = [];
  111. if ($action_forum_cat == 'edit' && $get_id || $post_submit_forum) {
  112. $inputvalues = get_forums($get_id);
  113. }
  114. $content = show_add_forum_form($inputvalues, $lp_id);
  115. }
  116. // Edit a forum category
  117. if (($action_forum_cat == 'edit' && $get_content == 'forumcategory') ||
  118. (isset($_POST['SubmitEditForumCategory'])) ? true : false
  119. ) {
  120. $forum_category = get_forum_categories($get_id);
  121. $content = show_edit_forumcategory_form($forum_category);
  122. }
  123. // Delete a forum category
  124. if ($action_forum_cat == 'delete') {
  125. $id_forum = intval($get_id);
  126. $list_threads = get_threads($id_forum);
  127. for ($i = 0; $i < count($list_threads); $i++) {
  128. deleteForumCategoryThread('thread', $list_threads[$i]['thread_id']);
  129. $link_info = GradebookUtils::isResourceInCourseGradebook(
  130. api_get_course_id(),
  131. 5,
  132. $list_threads[$i]['thread_id'],
  133. api_get_session_id()
  134. );
  135. if ($link_info !== false) {
  136. GradebookUtils::remove_resource_from_course_gradebook($link_info['id']);
  137. }
  138. }
  139. deleteForumCategoryThread($get_content, $get_id);
  140. }
  141. // Change visibility of a forum or a forum category.
  142. if ($action_forum_cat == 'invisible' || $action_forum_cat == 'visible') {
  143. $return_message = change_visibility($get_content, $get_id, $action_forum_cat);
  144. Display::addFlash(
  145. Display::return_message($return_message, 'confirmation', false)
  146. );
  147. }
  148. // Change lock status of a forum or a forum category.
  149. if ($action_forum_cat == 'lock' || $action_forum_cat == 'unlock') {
  150. $return_message = change_lock_status($get_content, $get_id, $action_forum_cat);
  151. Display::addFlash(
  152. Display::return_message($return_message, 'confirmation', false)
  153. );
  154. }
  155. // Move a forum or a forum category.
  156. if ($action_forum_cat == 'move' && isset($_GET['direction'])) {
  157. $return_message = move_up_down($get_content, $_GET['direction'], $get_id);
  158. Display::addFlash(
  159. Display::return_message($return_message, 'confirmation', false)
  160. );
  161. }
  162. return $content;
  163. }
  164. /**
  165. * This function displays the form that is used to add a forum category.
  166. *
  167. * @param array $inputvalues (deprecated, set to null when calling)
  168. * @param int $lp_id Learning path ID
  169. *
  170. * @return string
  171. *
  172. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  173. * @author Juan Carlos Raña Trabado (return to lp_id)
  174. *
  175. * @version may 2011, Chamilo 1.8.8
  176. */
  177. function show_add_forumcategory_form($inputvalues = [], $lp_id)
  178. {
  179. $form = new FormValidator(
  180. 'forumcategory',
  181. 'post',
  182. 'index.php?'.api_get_cidreq()
  183. );
  184. // hidden field if from learning path
  185. $form->addElement('hidden', 'lp_id', $lp_id);
  186. // Setting the form elements.
  187. $form->addElement('header', get_lang('AddForumCategory'));
  188. $form->addElement('text', 'forum_category_title', get_lang('Title'), ['autofocus']);
  189. $form->addElement(
  190. 'html_editor',
  191. 'forum_category_comment',
  192. get_lang('Description'),
  193. null,
  194. ['ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200']
  195. );
  196. $extraField = new ExtraField('forum_category');
  197. $returnParams = $extraField->addElements(
  198. $form,
  199. null,
  200. [], //exclude
  201. false, // filter
  202. false, // tag as select
  203. [], //show only fields
  204. [], // order fields
  205. [] // extra data
  206. );
  207. $form->addButtonCreate(get_lang('CreateCategory'), 'SubmitForumCategory');
  208. // Setting the rules.
  209. $form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required');
  210. // The validation or display
  211. if ($form->validate()) {
  212. $check = Security::check_token('post');
  213. if ($check) {
  214. $values = $form->exportValues();
  215. store_forumcategory($values);
  216. }
  217. Security::clear_token();
  218. } else {
  219. $token = Security::get_token();
  220. $form->addElement('hidden', 'sec_token');
  221. $form->setConstants(['sec_token' => $token]);
  222. return $form->returnForm();
  223. }
  224. }
  225. /**
  226. * This function displays the form that is used to add a forum category.
  227. *
  228. * @param array $inputvalues
  229. * @param int $lp_id
  230. *
  231. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  232. * @author Juan Carlos Raña Trabado (return to lp_id)
  233. *
  234. * @version may 2011, Chamilo 1.8.8
  235. */
  236. function show_add_forum_form($inputvalues = [], $lp_id)
  237. {
  238. $_course = api_get_course_info();
  239. $form = new FormValidator('forumcategory', 'post', 'index.php?'.api_get_cidreq());
  240. // The header for the form
  241. $form_title = get_lang('AddForum');
  242. if (!empty($inputvalues)) {
  243. $form_title = get_lang('EditForum');
  244. }
  245. $form->addElement('header', $form_title);
  246. // We have a hidden field if we are editing.
  247. if (!empty($inputvalues) && is_array($inputvalues)) {
  248. $my_forum_id = isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
  249. $form->addElement('hidden', 'forum_id', $my_forum_id);
  250. }
  251. $lp_id = intval($lp_id);
  252. // hidden field if from learning path
  253. $form->addElement('hidden', 'lp_id', $lp_id);
  254. // The title of the forum
  255. $form->addElement('text', 'forum_title', get_lang('Title'), ['autofocus']);
  256. // The comment of the forum.
  257. $form->addElement(
  258. 'html_editor',
  259. 'forum_comment',
  260. get_lang('Description'),
  261. null,
  262. ['ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200']
  263. );
  264. // Dropdown list: Forum categories
  265. $forum_categories = get_forum_categories();
  266. foreach ($forum_categories as $key => $value) {
  267. $forum_categories_titles[$value['cat_id']] = $value['cat_title'];
  268. }
  269. $form->addElement(
  270. 'select',
  271. 'forum_category',
  272. get_lang('InForumCategory'),
  273. $forum_categories_titles
  274. );
  275. $form->applyFilter('forum_category', 'html_filter');
  276. if ($_course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD) {
  277. // This is for horizontal
  278. $group = [];
  279. $group[] = $form->createElement('radio', 'allow_anonymous', null, get_lang('Yes'), 1);
  280. $group[] = $form->createElement('radio', 'allow_anonymous', null, get_lang('No'), 0);
  281. $form->addGroup($group, 'allow_anonymous_group', get_lang('AllowAnonymousPosts'));
  282. }
  283. $form->addButtonAdvancedSettings('advanced_params');
  284. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  285. $form->addDateTimePicker(
  286. 'start_time',
  287. [get_lang('ForumStartDate'), get_lang('ForumStartDateComment')],
  288. ['id' => 'start_time']
  289. );
  290. $form->addDateTimePicker(
  291. 'end_time',
  292. [get_lang('ForumEndDate'), get_lang('ForumEndDateComment')],
  293. ['id' => 'end_time']
  294. );
  295. $form->addRule(
  296. ['start_time', 'end_time'],
  297. get_lang('StartDateMustBeBeforeTheEndDate'),
  298. 'compare_datetime_text',
  299. '< allow_empty'
  300. );
  301. $group = [];
  302. $group[] = $form->createElement('radio', 'moderated', null, get_lang('Yes'), 1);
  303. $group[] = $form->createElement('radio', 'moderated', null, get_lang('No'), 0);
  304. $form->addGroup($group, 'moderated', get_lang('ModeratedForum'));
  305. $group = [];
  306. $group[] = $form->createElement('radio', 'students_can_edit', null, get_lang('Yes'), 1);
  307. $group[] = $form->createElement('radio', 'students_can_edit', null, get_lang('No'), 0);
  308. $form->addGroup($group, 'students_can_edit_group', get_lang('StudentsCanEdit'));
  309. $group = [];
  310. $group[] = $form->createElement('radio', 'approval_direct', null, get_lang('Approval'), 1);
  311. $group[] = $form->createElement('radio', 'approval_direct', null, get_lang('Direct'), 0);
  312. $group = [];
  313. $group[] = $form->createElement('radio', 'allow_attachments', null, get_lang('Yes'), 1);
  314. $group[] = $form->createElement('radio', 'allow_attachments', null, get_lang('No'), 0);
  315. $group = [];
  316. $group[] = $form->createElement('radio', 'allow_new_threads', null, get_lang('Yes'), 1);
  317. $group[] = $form->createElement('radio', 'allow_new_threads', null, get_lang('No'), 0);
  318. $form->addGroup($group, 'allow_new_threads_group', get_lang('AllowNewThreads'));
  319. $group = [];
  320. $group[] = $form->createElement('radio', 'default_view_type', null, get_lang('Flat'), 'flat');
  321. $group[] = $form->createElement('radio', 'default_view_type', null, get_lang('Threaded'), 'threaded');
  322. $group[] = $form->createElement('radio', 'default_view_type', null, get_lang('Nested'), 'nested');
  323. $form->addGroup($group, 'default_view_type_group', get_lang('DefaultViewType'));
  324. // Drop down list: Groups
  325. $groups = GroupManager::get_group_list();
  326. $groups_titles[0] = get_lang('NotAGroupForum');
  327. foreach ($groups as $key => $value) {
  328. $groups_titles[$value['id']] = $value['name'];
  329. }
  330. $form->addElement('select', 'group_forum', get_lang('ForGroup'), $groups_titles);
  331. // Public or private group forum
  332. $group = [];
  333. $group[] = $form->createElement('radio', 'public_private_group_forum', null, get_lang('Public'), 'public');
  334. $group[] = $form->createElement('radio', 'public_private_group_forum', null, get_lang('Private'), 'private');
  335. $form->addGroup($group, 'public_private_group_forum_group', get_lang('PublicPrivateGroupForum'));
  336. // Forum image
  337. $form->addProgress();
  338. if (!empty($inputvalues['forum_image'])) {
  339. $baseImagePath = api_get_course_path().'/upload/forum/images/'.$inputvalues['forum_image'];
  340. $image_path = api_get_path(WEB_COURSE_PATH).$baseImagePath;
  341. $sysImagePath = api_get_path(SYS_COURSE_PATH).$baseImagePath;
  342. if (file_exists($sysImagePath)) {
  343. $show_preview_image = Display::img(
  344. $image_path,
  345. null,
  346. ['class' => 'img-responsive']
  347. );
  348. $form->addElement('label', get_lang('PreviewImage'), $show_preview_image);
  349. $form->addElement('checkbox', 'remove_picture', null, get_lang('DelImage'));
  350. }
  351. }
  352. $forum_image = isset($inputvalues['forum_image']) ? $inputvalues['forum_image'] : '';
  353. $form->addElement('file', 'picture', ($forum_image != '' ? get_lang('UpdateImage') : get_lang('AddImage')));
  354. $form->addRule(
  355. 'picture',
  356. get_lang('OnlyImagesAllowed'),
  357. 'filetype',
  358. ['jpg', 'jpeg', 'png', 'gif']
  359. );
  360. //$forumId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  361. //$skillList = Skill::addSkillsToForm($form, ITEM_TYPE_FORUM, $forumId);
  362. $form->addElement('html', '</div>');
  363. // The OK button
  364. if (isset($_GET['id']) && $_GET['action'] == 'edit') {
  365. $form->addButtonUpdate(get_lang('ModifyForum'), 'SubmitForum');
  366. } else {
  367. $form->addButtonCreate(get_lang('CreateForum'), 'SubmitForum');
  368. }
  369. // setting the rules
  370. $form->addRule('forum_title', get_lang('ThisFieldIsRequired'), 'required');
  371. $form->addRule('forum_category', get_lang('ThisFieldIsRequired'), 'required');
  372. $defaultSettingAllowNewThreads = api_get_default_tool_setting('forum', 'allow_new_threads', 0);
  373. // Settings the defaults
  374. if (empty($inputvalues) || !is_array($inputvalues)) {
  375. $defaults['moderated']['moderated'] = 0;
  376. $defaults['allow_anonymous_group']['allow_anonymous'] = 0;
  377. $defaults['students_can_edit_group']['students_can_edit'] = 0;
  378. $defaults['approval_direct_group']['approval_direct'] = 0;
  379. $defaults['allow_attachments_group']['allow_attachments'] = 1;
  380. $defaults['allow_new_threads_group']['allow_new_threads'] = $defaultSettingAllowNewThreads;
  381. $defaults['default_view_type_group']['default_view_type'] = api_get_setting('default_forum_view');
  382. $defaults['public_private_group_forum_group']['public_private_group_forum'] = 'public';
  383. if (isset($_GET['forumcategory'])) {
  384. $defaults['forum_category'] = Security::remove_XSS($_GET['forumcategory']);
  385. }
  386. } else {
  387. // the default values when editing = the data in the table
  388. $defaults['forum_id'] = isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null;
  389. $defaults['forum_title'] = prepare4display(isset($inputvalues['forum_title']) ? $inputvalues['forum_title'] : null);
  390. $defaults['forum_comment'] = prepare4display(isset($inputvalues['forum_comment']) ? $inputvalues['forum_comment'] : null);
  391. $defaults['start_time'] = isset($inputvalues['start_time']) ? api_get_local_time($inputvalues['start_time']) : null;
  392. $defaults['end_time'] = isset($inputvalues['end_time']) ? api_get_local_time($inputvalues['end_time']) : null;
  393. $defaults['moderated']['moderated'] = isset($inputvalues['moderated']) ? $inputvalues['moderated'] : 0;
  394. $defaults['forum_category'] = isset($inputvalues['forum_category']) ? $inputvalues['forum_category'] : null;
  395. $defaults['allow_anonymous_group']['allow_anonymous'] = isset($inputvalues['allow_anonymous']) ? $inputvalues['allow_anonymous'] : null;
  396. $defaults['students_can_edit_group']['students_can_edit'] = isset($inputvalues['allow_edit']) ? $inputvalues['allow_edit'] : null;
  397. $defaults['approval_direct_group']['approval_direct'] = isset($inputvalues['approval_direct_post']) ? $inputvalues['approval_direct_post'] : null;
  398. $defaults['allow_attachments_group']['allow_attachments'] = isset($inputvalues['allow_attachments']) ? $inputvalues['allow_attachments'] : null;
  399. $defaults['allow_new_threads_group']['allow_new_threads'] = isset($inputvalues['allow_new_threads']) ? $inputvalues['allow_new_threads'] : $defaultSettingAllowNewThreads;
  400. $defaults['default_view_type_group']['default_view_type'] = isset($inputvalues['default_view']) ? $inputvalues['default_view'] : null;
  401. $defaults['public_private_group_forum_group']['public_private_group_forum'] = isset($inputvalues['forum_group_public_private']) ? $inputvalues['forum_group_public_private'] : null;
  402. $defaults['group_forum'] = isset($inputvalues['forum_of_group']) ? $inputvalues['forum_of_group'] : null;
  403. }
  404. $form->setDefaults($defaults);
  405. // Validation or display
  406. if ($form->validate()) {
  407. $check = Security::check_token('post');
  408. if ($check) {
  409. $values = $form->getSubmitValues();
  410. $forumId = store_forum($values, '', true);
  411. if ($forumId) {
  412. // Skill::saveSkills($form, ITEM_TYPE_FORUM, $forumId);
  413. if (isset($values['forum_id'])) {
  414. Display::addFlash(Display::return_message(get_lang('ForumEdited'), 'confirmation'));
  415. } else {
  416. Display::addFlash(Display::return_message(get_lang('ForumAdded'), 'confirmation'));
  417. }
  418. }
  419. }
  420. Security::clear_token();
  421. } else {
  422. $token = Security::get_token();
  423. $form->addElement('hidden', 'sec_token');
  424. $form->setConstants(['sec_token' => $token]);
  425. return $form->returnForm();
  426. }
  427. }
  428. /**
  429. * This function deletes the forum image if exists.
  430. *
  431. * @param int forum id
  432. *
  433. * @return bool true if success
  434. *
  435. * @author Julio Montoya <gugli100@gmail.com>
  436. *
  437. * @version february 2006, dokeos 1.8
  438. */
  439. function delete_forum_image($forum_id)
  440. {
  441. $table_forums = Database::get_course_table(TABLE_FORUM);
  442. $course_id = api_get_course_int_id();
  443. $forum_id = intval($forum_id);
  444. $sql = "SELECT forum_image FROM $table_forums
  445. WHERE forum_id = $forum_id AND c_id = $course_id";
  446. $result = Database::query($sql);
  447. $row = Database::fetch_array($result);
  448. if ($row['forum_image'] != '') {
  449. $file = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$row['forum_image'];
  450. if (file_exists($file)) {
  451. unlink($file);
  452. }
  453. return true;
  454. } else {
  455. return false;
  456. }
  457. }
  458. /**
  459. * This function displays the form that is used to edit a forum category.
  460. *
  461. * @param array
  462. *
  463. * @return string
  464. *
  465. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  466. *
  467. * @version february 2006, dokeos 1.8
  468. */
  469. function show_edit_forumcategory_form($inputvalues = [])
  470. {
  471. $categoryId = $inputvalues['cat_id'];
  472. $form = new FormValidator('forumcategory', 'post', 'index.php?'.api_get_cidreq().'&id='.$categoryId);
  473. // Setting the form elements.
  474. $form->addElement('header', '', get_lang('EditForumCategory'));
  475. $form->addElement('hidden', 'forum_category_id');
  476. $form->addElement('text', 'forum_category_title', get_lang('Title'));
  477. $form->addElement(
  478. 'html_editor',
  479. 'forum_category_comment',
  480. get_lang('Comment'),
  481. null,
  482. ['ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200']
  483. );
  484. $extraField = new ExtraField('forum_category');
  485. $returnParams = $extraField->addElements(
  486. $form,
  487. $categoryId,
  488. [], //exclude
  489. false, // filter
  490. false, // tag as select
  491. [], //show only fields
  492. [], // order fields
  493. [] // extra data
  494. );
  495. $form->addButtonUpdate(get_lang('ModifyCategory'), 'SubmitEditForumCategory');
  496. // Setting the default values.
  497. $defaultvalues['forum_category_id'] = $inputvalues['cat_id'];
  498. $defaultvalues['forum_category_title'] = $inputvalues['cat_title'];
  499. $defaultvalues['forum_category_comment'] = $inputvalues['cat_comment'];
  500. $form->setDefaults($defaultvalues);
  501. // Setting the rules.
  502. $form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required');
  503. // Validation or display
  504. if ($form->validate()) {
  505. $check = Security::check_token('post');
  506. if ($check) {
  507. $values = $form->exportValues();
  508. store_forumcategory($values);
  509. }
  510. Security::clear_token();
  511. } else {
  512. $token = Security::get_token();
  513. $form->addElement('hidden', 'sec_token');
  514. $form->setConstants(['sec_token' => $token]);
  515. return $form->returnForm();
  516. }
  517. }
  518. /**
  519. * This function stores the forum category in the database.
  520. * The new category is added to the end.
  521. *
  522. * @param array $values
  523. * @param array $courseInfo
  524. * @param bool $showMessage
  525. *
  526. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  527. *
  528. * @version february 2006, dokeos 1.8
  529. */
  530. function store_forumcategory($values, $courseInfo = [], $showMessage = true)
  531. {
  532. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  533. $course_id = $courseInfo['real_id'];
  534. $table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
  535. // Find the max cat_order. The new forum category is added at the end => max cat_order + &
  536. $sql = "SELECT MAX(cat_order) as sort_max
  537. FROM $table_categories
  538. WHERE c_id = $course_id";
  539. $result = Database::query($sql);
  540. $row = Database::fetch_array($result);
  541. $new_max = $row['sort_max'] + 1;
  542. $session_id = api_get_session_id();
  543. $clean_cat_title = $values['forum_category_title'];
  544. $last_id = null;
  545. if (isset($values['forum_category_id'])) {
  546. // Storing after edition.
  547. $params = [
  548. 'cat_title' => $clean_cat_title,
  549. 'cat_comment' => isset($values['forum_category_comment']) ? $values['forum_category_comment'] : '',
  550. ];
  551. Database::update(
  552. $table_categories,
  553. $params,
  554. [
  555. 'c_id = ? AND cat_id = ?' => [
  556. $course_id,
  557. $values['forum_category_id'],
  558. ],
  559. ]
  560. );
  561. api_item_property_update(
  562. $courseInfo,
  563. TOOL_FORUM_CATEGORY,
  564. $values['forum_category_id'],
  565. 'ForumCategoryUpdated',
  566. api_get_user_id()
  567. );
  568. $return_message = get_lang('ForumCategoryEdited');
  569. $logInfo = [
  570. 'tool' => TOOL_FORUM,
  571. 'tool_id' => 0,
  572. 'tool_id_detail' => 0,
  573. 'action' => 'update-forumcategory',
  574. 'action_details' => 'forumcategory',
  575. 'info' => $clean_cat_title,
  576. ];
  577. Event::registerLog($logInfo);
  578. $values['item_id'] = $values['forum_category_id'];
  579. } else {
  580. $params = [
  581. 'c_id' => $course_id,
  582. 'cat_title' => $clean_cat_title,
  583. 'cat_comment' => isset($values['forum_category_comment']) ? $values['forum_category_comment'] : '',
  584. 'cat_order' => $new_max,
  585. 'session_id' => $session_id,
  586. 'locked' => 0,
  587. 'cat_id' => 0,
  588. ];
  589. $last_id = Database::insert($table_categories, $params);
  590. if ($last_id > 0) {
  591. $sql = "UPDATE $table_categories SET cat_id = $last_id WHERE iid = $last_id";
  592. Database::query($sql);
  593. api_item_property_update(
  594. $courseInfo,
  595. TOOL_FORUM_CATEGORY,
  596. $last_id,
  597. 'ForumCategoryAdded',
  598. api_get_user_id()
  599. );
  600. api_set_default_visibility(
  601. $last_id,
  602. TOOL_FORUM_CATEGORY,
  603. 0,
  604. $courseInfo
  605. );
  606. }
  607. $return_message = get_lang('ForumCategoryAdded');
  608. $logInfo = [
  609. 'tool' => TOOL_FORUM,
  610. 'tool_id' => 0,
  611. 'tool_id_detail' => 0,
  612. 'action' => 'new-forumcategory',
  613. 'action_details' => 'forumcategory',
  614. 'info' => $clean_cat_title,
  615. ];
  616. Event::registerLog($logInfo);
  617. $values['item_id'] = $last_id;
  618. }
  619. $extraFieldValue = new ExtraFieldValue('forum_category');
  620. $extraFieldValue->saveFieldValues($values);
  621. if ($showMessage) {
  622. Display::addFlash(Display::return_message($return_message, 'confirmation'));
  623. }
  624. return $last_id;
  625. }
  626. /**
  627. * This function stores the forum in the database. The new forum is added to the end.
  628. *
  629. * @param array $values
  630. * @param array $courseInfo
  631. * @param bool $returnId
  632. *
  633. * @return string language variable
  634. *
  635. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  636. *
  637. * @version february 2006, dokeos 1.8
  638. */
  639. function store_forum($values, $courseInfo = [], $returnId = false)
  640. {
  641. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  642. $courseId = $courseInfo['real_id'];
  643. $session_id = api_get_session_id();
  644. $group_id = api_get_group_id();
  645. if (isset($values['group_id']) && !empty($values['group_id'])) {
  646. $group_id = $values['group_id'];
  647. }
  648. $groupInfo = [];
  649. if (!empty($group_id)) {
  650. $groupInfo = GroupManager::get_group_properties($group_id);
  651. }
  652. $table_forums = Database::get_course_table(TABLE_FORUM);
  653. // Find the max forum_order for the given category. The new forum is added at the end => max cat_order + &
  654. if (is_null($values['forum_category'])) {
  655. $new_max = null;
  656. } else {
  657. $sql = "SELECT MAX(forum_order) as sort_max
  658. FROM $table_forums
  659. WHERE
  660. c_id = $courseId AND
  661. forum_category='".Database::escape_string($values['forum_category'])."'";
  662. $result = Database::query($sql);
  663. $row = Database::fetch_array($result);
  664. $new_max = $row['sort_max'] + 1;
  665. }
  666. // Forum images
  667. $has_attachment = false;
  668. $image_moved = true;
  669. if (!empty($_FILES['picture']['name'])) {
  670. $upload_ok = process_uploaded_file($_FILES['picture']);
  671. $has_attachment = true;
  672. }
  673. // Remove existing picture if it was requested.
  674. if (!empty($_POST['remove_picture'])) {
  675. delete_forum_image($values['forum_id']);
  676. }
  677. $new_file_name = '';
  678. if (isset($upload_ok)) {
  679. if ($has_attachment) {
  680. $course_dir = $courseInfo['path'].'/upload/forum/images';
  681. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  682. $updir = $sys_course_path.$course_dir;
  683. // Try to add an extension to the file if it hasn't one.
  684. $new_file_name = add_ext_on_mime(
  685. Database::escape_string($_FILES['picture']['name']),
  686. $_FILES['picture']['type']
  687. );
  688. if (!filter_extension($new_file_name)) {
  689. //Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'));
  690. $image_moved = false;
  691. } else {
  692. $file_extension = explode('.', $_FILES['picture']['name']);
  693. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  694. $new_file_name = uniqid('').'.'.$file_extension;
  695. $new_path = $updir.'/'.$new_file_name;
  696. $result = @move_uploaded_file($_FILES['picture']['tmp_name'], $new_path);
  697. // Storing the attachments if any
  698. if ($result) {
  699. $image_moved = true;
  700. }
  701. }
  702. }
  703. }
  704. if (isset($values['forum_id'])) {
  705. // Storing after edition.
  706. $params = [
  707. 'forum_title' => $values['forum_title'],
  708. 'forum_comment' => isset($values['forum_comment']) ? $values['forum_comment'] : null,
  709. 'forum_category' => isset($values['forum_category']) ? $values['forum_category'] : null,
  710. 'allow_anonymous' => isset($values['allow_anonymous_group']['allow_anonymous']) ? $values['allow_anonymous_group']['allow_anonymous'] : null,
  711. 'allow_edit' => isset($values['students_can_edit_group']['students_can_edit']) ? $values['students_can_edit_group']['students_can_edit'] : null,
  712. 'approval_direct_post' => isset($values['approval_direct_group']['approval_direct']) ? $values['approval_direct_group']['approval_direct'] : null,
  713. 'allow_attachments' => isset($values['allow_attachments_group']['allow_attachments']) ? $values['allow_attachments_group']['allow_attachments'] : null,
  714. 'allow_new_threads' => isset($values['allow_new_threads_group']['allow_new_threads']) ? $values['allow_new_threads_group']['allow_new_threads'] : null,
  715. 'default_view' => isset($values['default_view_type_group']['default_view_type']) ? $values['default_view_type_group']['default_view_type'] : null,
  716. 'forum_of_group' => isset($values['group_forum']) ? $values['group_forum'] : null,
  717. 'forum_group_public_private' => isset($values['public_private_group_forum_group']['public_private_group_forum']) ? $values['public_private_group_forum_group']['public_private_group_forum'] : null,
  718. 'moderated' => $values['moderated']['moderated'],
  719. 'start_time' => !empty($values['start_time']) ? api_get_utc_datetime($values['start_time']) : null,
  720. 'end_time' => !empty($values['end_time']) ? api_get_utc_datetime($values['end_time']) : null,
  721. 'session_id' => $session_id,
  722. 'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : 0,
  723. ];
  724. if (isset($upload_ok)) {
  725. if ($has_attachment) {
  726. $params['forum_image'] = $new_file_name;
  727. }
  728. }
  729. if (isset($values['remove_picture']) && $values['remove_picture'] == 1) {
  730. $params['forum_image'] = '';
  731. delete_forum_image($values['forum_id']);
  732. }
  733. // Move groups from one group to another
  734. if (isset($values['group_forum'])) {
  735. $forumData = get_forums($values['forum_id']);
  736. $currentGroupId = $forumData['forum_of_group'];
  737. if ($currentGroupId != $values['group_forum']) {
  738. $threads = get_threads($values['forum_id']);
  739. $toGroupId = 'NULL';
  740. if (!empty($values['group_forum'])) {
  741. $toGroupId = $values['group_forum'];
  742. }
  743. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  744. foreach ($threads as $thread) {
  745. $sql = "UPDATE $tableItemProperty
  746. SET to_group_id = $toGroupId
  747. WHERE
  748. tool = '".TOOL_FORUM_THREAD."' AND
  749. ref = ".$thread['thread_id']." AND
  750. c_id = ".$courseId;
  751. Database::query($sql);
  752. $posts = getPosts(
  753. $forumData,
  754. $thread['thread_id']
  755. );
  756. foreach ($posts as $post) {
  757. $postId = $post['post_id'];
  758. $attachMentList = getAllAttachment($postId);
  759. if (!empty($attachMentList)) {
  760. foreach ($attachMentList as $attachMent) {
  761. $sql = "UPDATE $tableItemProperty
  762. SET to_group_id = $toGroupId
  763. WHERE
  764. tool = '".TOOL_FORUM_ATTACH."' AND
  765. ref = ".$attachMent['iid']." AND
  766. c_id = ".$courseId;
  767. Database::query($sql);
  768. }
  769. }
  770. $sql = "UPDATE $tableItemProperty
  771. SET to_group_id = $toGroupId
  772. WHERE
  773. tool = '".TOOL_FORUM_POST."' AND
  774. ref = $postId AND
  775. c_id = $courseId";
  776. Database::query($sql);
  777. }
  778. }
  779. }
  780. }
  781. Database::update(
  782. $table_forums,
  783. $params,
  784. ['c_id = ? AND forum_id = ?' => [$courseId, $values['forum_id']]]
  785. );
  786. api_item_property_update(
  787. $courseInfo,
  788. TOOL_FORUM,
  789. Database::escape_string($values['forum_id']),
  790. 'ForumUpdated',
  791. api_get_user_id(),
  792. $groupInfo
  793. );
  794. $return_message = get_lang('ForumEdited');
  795. $forumId = $values['forum_id'];
  796. $logInfo = [
  797. 'tool' => TOOL_FORUM,
  798. 'tool_id' => $values['forum_id'],
  799. 'tool_id_detail' => 0,
  800. 'action' => 'update-forum',
  801. 'action_details' => 'forum',
  802. 'info' => $values['forum_title'],
  803. ];
  804. Event::registerLog($logInfo);
  805. } else {
  806. if ($image_moved) {
  807. $new_file_name = isset($new_file_name) ? $new_file_name : '';
  808. }
  809. $params = [
  810. 'c_id' => $courseId,
  811. 'forum_title' => $values['forum_title'],
  812. 'forum_image' => $new_file_name,
  813. 'forum_comment' => isset($values['forum_comment']) ? $values['forum_comment'] : null,
  814. 'forum_category' => isset($values['forum_category']) ? $values['forum_category'] : null,
  815. 'allow_anonymous' => isset($values['allow_anonymous_group']['allow_anonymous']) ? $values['allow_anonymous_group']['allow_anonymous'] : null,
  816. 'allow_edit' => isset($values['students_can_edit_group']['students_can_edit']) ? $values['students_can_edit_group']['students_can_edit'] : null,
  817. 'approval_direct_post' => isset($values['approval_direct_group']['approval_direct']) ? $values['approval_direct_group']['approval_direct'] : null,
  818. 'allow_attachments' => isset($values['allow_attachments_group']['allow_attachments']) ? $values['allow_attachments_group']['allow_attachments'] : null,
  819. 'allow_new_threads' => isset($values['allow_new_threads_group']['allow_new_threads']) ? $values['allow_new_threads_group']['allow_new_threads'] : null,
  820. 'default_view' => isset($values['default_view_type_group']['default_view_type']) ? $values['default_view_type_group']['default_view_type'] : null,
  821. 'forum_of_group' => isset($values['group_forum']) ? $values['group_forum'] : null,
  822. 'forum_group_public_private' => isset($values['public_private_group_forum_group']['public_private_group_forum']) ? $values['public_private_group_forum_group']['public_private_group_forum'] : null,
  823. 'moderated' => isset($values['moderated']['moderated']) ? (int) $values['moderated']['moderated'] : 0,
  824. 'start_time' => !empty($values['start_time']) ? api_get_utc_datetime($values['start_time']) : null,
  825. 'end_time' => !empty($values['end_time']) ? api_get_utc_datetime($values['end_time']) : null,
  826. 'forum_order' => isset($new_max) ? $new_max : null,
  827. 'session_id' => $session_id,
  828. 'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : 0,
  829. 'locked' => 0,
  830. 'forum_id' => 0,
  831. ];
  832. $forumId = Database::insert($table_forums, $params);
  833. if ($forumId > 0) {
  834. $sql = "UPDATE $table_forums SET forum_id = iid WHERE iid = $forumId";
  835. Database::query($sql);
  836. api_item_property_update(
  837. $courseInfo,
  838. TOOL_FORUM,
  839. $forumId,
  840. 'ForumAdded',
  841. api_get_user_id(),
  842. $groupInfo
  843. );
  844. api_set_default_visibility(
  845. $forumId,
  846. TOOL_FORUM,
  847. $group_id,
  848. $courseInfo
  849. );
  850. $logInfo = [
  851. 'tool' => TOOL_FORUM,
  852. 'tool_id' => $forumId,
  853. 'tool_id_detail' => 0,
  854. 'action' => 'new-forum',
  855. 'action_details' => 'forum',
  856. 'info' => $values['forum_title'],
  857. ];
  858. Event::registerLog($logInfo);
  859. }
  860. $return_message = get_lang('ForumAdded');
  861. }
  862. if ($returnId) {
  863. return $forumId;
  864. }
  865. return $return_message;
  866. }
  867. /**
  868. * This function deletes a forum or a forum category
  869. * This function currently does not delete the forums inside the category,
  870. * nor the threads and replies inside these forums.
  871. * For the moment this is the easiest method and it has the advantage that it
  872. * allows to recover fora that were acidently deleted
  873. * when the forum category got deleted.
  874. *
  875. * @param $content = what we are deleting (a forum or a forum category)
  876. * @param $id the id of the forum category that has to be deleted
  877. *
  878. * @todo write the code for the cascading deletion of the forums inside a
  879. * forum category and also the threads and replies inside these forums
  880. * @todo config setting for recovery or not
  881. * (see also the documents tool: real delete or not).
  882. *
  883. * @return string
  884. *
  885. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  886. *
  887. * @version february 2006, dokeos 1.8
  888. */
  889. function deleteForumCategoryThread($content, $id)
  890. {
  891. $_course = api_get_course_info();
  892. $table_forums = Database::get_course_table(TABLE_FORUM);
  893. $table_forums_post = Database::get_course_table(TABLE_FORUM_POST);
  894. $table_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  895. $course_id = api_get_course_int_id();
  896. $groupId = api_get_group_id();
  897. $groupInfo = GroupManager::get_group_properties($groupId);
  898. $userId = api_get_user_id();
  899. $id = intval($id);
  900. // Delete all attachment file about this tread id.
  901. $sql = "SELECT post_id FROM $table_forums_post
  902. WHERE c_id = $course_id AND thread_id = '".$id."' ";
  903. $res = Database::query($sql);
  904. while ($poster_id = Database::fetch_row($res)) {
  905. delete_attachment($poster_id[0]);
  906. }
  907. $tool_constant = null;
  908. $return_message = '';
  909. if ($content == 'forumcategory') {
  910. $tool_constant = TOOL_FORUM_CATEGORY;
  911. $return_message = get_lang('ForumCategoryDeleted');
  912. if (!empty($forum_list)) {
  913. $sql = "SELECT forum_id FROM $table_forums
  914. WHERE c_id = $course_id AND forum_category='".$id."'";
  915. $result = Database::query($sql);
  916. $row = Database::fetch_array($result);
  917. foreach ($row as $arr_forum) {
  918. $forum_id = $arr_forum['forum_id'];
  919. api_item_property_update(
  920. $_course,
  921. 'forum',
  922. $forum_id,
  923. 'delete',
  924. api_get_user_id()
  925. );
  926. }
  927. }
  928. }
  929. if ($content == 'forum') {
  930. $tool_constant = TOOL_FORUM;
  931. $return_message = get_lang('ForumDeleted');
  932. if (!empty($number_threads)) {
  933. $sql = "SELECT thread_id FROM $table_forum_thread
  934. WHERE c_id = $course_id AND forum_id = $id ";
  935. $result = Database::query($sql);
  936. $row = Database::fetch_array($result);
  937. foreach ($row as $arr_forum) {
  938. $forum_id = $arr_forum['thread_id'];
  939. api_item_property_update(
  940. $_course,
  941. 'forum_thread',
  942. $forum_id,
  943. 'delete',
  944. api_get_user_id()
  945. );
  946. }
  947. }
  948. }
  949. if ($content == 'thread') {
  950. $tool_constant = TOOL_FORUM_THREAD;
  951. $return_message = get_lang('ThreadDeleted');
  952. Skill::deleteSkillsFromItem($id, ITEM_TYPE_FORUM_THREAD);
  953. }
  954. api_item_property_update(
  955. $_course,
  956. $tool_constant,
  957. $id,
  958. 'delete',
  959. $userId,
  960. $groupInfo
  961. );
  962. // Check if this returns a true and if so => return $return_message, if not => return false;
  963. if (!empty($return_message)) {
  964. Display::addFlash(Display::return_message($return_message, 'confirmation', false));
  965. }
  966. return $return_message;
  967. }
  968. /**
  969. * This function deletes a forum post. This separate function is needed because forum posts do not appear
  970. * in the item_property table (yet)
  971. * and because deleting a post also has consequence on the posts that have this post as parent_id
  972. * (they are also deleted).
  973. * an alternative would be to store the posts also in item_property and mark this post as deleted (visibility = 2).
  974. * We also have to decrease the number of replies in the thread table.
  975. *
  976. * @param $post_id the id of the post that will be deleted
  977. *
  978. * @todo write recursive function that deletes all the posts that have this message as parent
  979. *
  980. * @return string language variable
  981. *
  982. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  983. * @author Hubert Borderiou Function cleanead and fixed
  984. *
  985. * @version february 2006
  986. */
  987. function delete_post($post_id)
  988. {
  989. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  990. $post_id = intval($post_id);
  991. $course_id = api_get_course_int_id();
  992. $em = Database::getManager();
  993. $post = $em
  994. ->getRepository('ChamiloCourseBundle:CForumPost')
  995. ->findOneBy(['cId' => $course_id, 'postId' => $post_id]);
  996. if ($post) {
  997. $em
  998. ->createQuery('
  999. UPDATE ChamiloCourseBundle:CForumPost p
  1000. SET p.postParentId = :parent_of_deleted_post
  1001. WHERE
  1002. p.cId = :course AND
  1003. p.postParentId = :post AND
  1004. p.threadId = :thread_of_deleted_post AND
  1005. p.forumId = :forum_of_deleted_post
  1006. ')
  1007. ->execute([
  1008. 'parent_of_deleted_post' => $post->getPostParentId(),
  1009. 'course' => $course_id,
  1010. 'post' => $post->getPostId(),
  1011. 'thread_of_deleted_post' => $post->getThreadId(),
  1012. 'forum_of_deleted_post' => $post->getForumId(),
  1013. ]);
  1014. $em->remove($post);
  1015. $em->flush();
  1016. // Delete attachment file about this post id.
  1017. delete_attachment($post_id);
  1018. }
  1019. $last_post_of_thread = check_if_last_post_of_thread($_GET['thread']);
  1020. if (is_array($last_post_of_thread)) {
  1021. // Decreasing the number of replies for this thread and also changing the last post information.
  1022. $sql = "UPDATE $table_threads
  1023. SET
  1024. thread_replies = thread_replies - 1,
  1025. thread_last_post = ".intval($last_post_of_thread['post_id']).",
  1026. thread_date='".Database::escape_string($last_post_of_thread['post_date'])."'
  1027. WHERE c_id = $course_id AND thread_id = ".intval($_GET['thread']);
  1028. Database::query($sql);
  1029. return 'PostDeleted';
  1030. }
  1031. if (!$last_post_of_thread) {
  1032. // We deleted the very single post of the thread so we need to delete the entry in the thread table also.
  1033. $sql = "DELETE FROM $table_threads
  1034. WHERE c_id = $course_id AND thread_id = ".intval($_GET['thread']);
  1035. Database::query($sql);
  1036. return 'PostDeletedSpecial';
  1037. }
  1038. }
  1039. /**
  1040. * This function gets the all information of the last (=most recent) post of the thread
  1041. * This can be done by sorting the posts that have the field thread_id=$thread_id and sort them by post_date.
  1042. *
  1043. * @param $thread_id the id of the thread we want to know the last post of
  1044. *
  1045. * @return an array or bool if there is a last post found, false if there is
  1046. * no post entry linked to that thread => thread will be deleted
  1047. *
  1048. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1049. *
  1050. * @version february 2006, dokeos 1.8
  1051. */
  1052. function check_if_last_post_of_thread($thread_id)
  1053. {
  1054. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  1055. $course_id = api_get_course_int_id();
  1056. $sql = "SELECT * FROM $table_posts
  1057. WHERE c_id = $course_id AND thread_id = ".intval($thread_id)."
  1058. ORDER BY post_date DESC";
  1059. $result = Database::query($sql);
  1060. if (Database::num_rows($result) > 0) {
  1061. $row = Database::fetch_array($result);
  1062. return $row;
  1063. } else {
  1064. return false;
  1065. }
  1066. }
  1067. /**
  1068. * @param string $content Type of content forum category, forum, thread, post
  1069. * @param int $id the id of the content we want to make invisible
  1070. * @param int $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible)
  1071. * @param array $additional_url_parameters
  1072. *
  1073. * @return string HTML
  1074. */
  1075. function return_visible_invisible_icon(
  1076. $content,
  1077. $id,
  1078. $current_visibility_status,
  1079. $additional_url_parameters = ''
  1080. ) {
  1081. $html = '';
  1082. $id = (int) $id;
  1083. $current_visibility_status = (int) $current_visibility_status;
  1084. if ($current_visibility_status == 1) {
  1085. $html .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  1086. if (is_array($additional_url_parameters)) {
  1087. foreach ($additional_url_parameters as $key => $value) {
  1088. $html .= $key.'='.$value.'&';
  1089. }
  1090. }
  1091. $html .= 'action=invisible&content='.$content.'&id='.$id.'">'.
  1092. Display::return_icon('visible.png', get_lang('MakeInvisible'), [], ICON_SIZE_SMALL).'</a>';
  1093. }
  1094. if ($current_visibility_status == 0) {
  1095. $html .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  1096. if (is_array($additional_url_parameters)) {
  1097. foreach ($additional_url_parameters as $key => $value) {
  1098. $html .= $key.'='.$value.'&';
  1099. }
  1100. }
  1101. $html .= 'action=visible&content='.$content.'&id='.$id.'">'.
  1102. Display::return_icon('invisible.png', get_lang('MakeVisible'), [], ICON_SIZE_SMALL).'</a>';
  1103. }
  1104. return $html;
  1105. }
  1106. /**
  1107. * @param $content
  1108. * @param $id
  1109. * @param $current_lock_status
  1110. * @param string $additional_url_parameters
  1111. *
  1112. * @return string
  1113. */
  1114. function return_lock_unlock_icon($content, $id, $current_lock_status, $additional_url_parameters = '')
  1115. {
  1116. $html = '';
  1117. $id = intval($id);
  1118. //check if the forum is blocked due
  1119. if ($content == 'thread') {
  1120. if (api_resource_is_locked_by_gradebook($id, LINK_FORUM_THREAD)) {
  1121. $html .= Display::return_icon(
  1122. 'lock_na.png',
  1123. get_lang('ResourceLockedByGradebook'),
  1124. [],
  1125. ICON_SIZE_SMALL
  1126. );
  1127. return $html;
  1128. }
  1129. }
  1130. if ($current_lock_status == '1') {
  1131. $html .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  1132. if (is_array($additional_url_parameters)) {
  1133. foreach ($additional_url_parameters as $key => $value) {
  1134. $html .= $key.'='.$value.'&';
  1135. }
  1136. }
  1137. $html .= 'action=unlock&content='.$content.'&id='.$id.'">'.
  1138. Display::return_icon('lock.png', get_lang('Unlock'), [], ICON_SIZE_SMALL).'</a>';
  1139. }
  1140. if ($current_lock_status == '0') {
  1141. $html .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&';
  1142. if (is_array($additional_url_parameters)) {
  1143. foreach ($additional_url_parameters as $key => $value) {
  1144. $html .= $key.'='.$value.'&';
  1145. }
  1146. }
  1147. $html .= 'action=lock&content='.$content.'&id='.$id.'">'.
  1148. Display::return_icon('unlock.png', get_lang('Lock'), [], ICON_SIZE_SMALL).'</a>';
  1149. }
  1150. return $html;
  1151. }
  1152. /**
  1153. * This function takes care of the display of the up and down icon.
  1154. *
  1155. * @param string $content what is it that we want to make (in)visible: forum category, forum, thread, post
  1156. * @param int $id is the id of the item we want to display the icons for
  1157. * @param array $list is an array of all the items. All items in this list should have
  1158. * an up and down icon except for the first (no up icon) and the last (no down icon)
  1159. * The key of this $list array is the id of the item.
  1160. *
  1161. * @return string HTML
  1162. */
  1163. function return_up_down_icon($content, $id, $list)
  1164. {
  1165. $id = (int) $id;
  1166. $total_items = count($list);
  1167. $position = 0;
  1168. $internal_counter = 0;
  1169. $forumCategory = isset($_GET['forumcategory']) ? Security::remove_XSS($_GET['forumcategory']) : null;
  1170. if (is_array($list)) {
  1171. foreach ($list as $key => $listitem) {
  1172. $internal_counter++;
  1173. if ($id == $key) {
  1174. $position = $internal_counter;
  1175. }
  1176. }
  1177. }
  1178. if ($position > 1) {
  1179. $return_value = '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=move&direction=up&content='.$content.'&forumcategory='.$forumCategory.'&id='.$id.'" title="'.get_lang('MoveUp').'">'.
  1180. Display::return_icon('up.png', get_lang('MoveUp'), [], ICON_SIZE_SMALL).'</a>';
  1181. } else {
  1182. $return_value = Display::return_icon('up_na.png', '-', [], ICON_SIZE_SMALL);
  1183. }
  1184. if ($position < $total_items) {
  1185. $return_value .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=move&direction=down&content='.$content.'&forumcategory='.$forumCategory.'&id='.$id.'" title="'.get_lang('MoveDown').'" >'.
  1186. Display::return_icon('down.png', get_lang('MoveDown'), [], ICON_SIZE_SMALL).'</a>';
  1187. } else {
  1188. $return_value .= Display::return_icon('down_na.png', '-', [], ICON_SIZE_SMALL);
  1189. }
  1190. return $return_value;
  1191. }
  1192. /**
  1193. * This function changes the visibility in the database (item_property).
  1194. *
  1195. * @param string $content what is it that we want to make (in)visible: forum category, forum, thread, post
  1196. * @param int $id the id of the content we want to make invisible
  1197. * @param string $target_visibility what is the current status of the visibility (0 = invisible, 1 = visible)
  1198. *
  1199. * @todo change the get parameter so that it matches the tool constants.
  1200. * @todo check if api_item_property_update returns true or false => returnmessage depends on it.
  1201. * @todo move to itemmanager
  1202. *
  1203. * @return string language variable
  1204. *
  1205. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1206. *
  1207. * @version february 2006, dokeos 1.8
  1208. */
  1209. function change_visibility($content, $id, $target_visibility)
  1210. {
  1211. $_course = api_get_course_info();
  1212. $constants = [
  1213. 'forumcategory' => TOOL_FORUM_CATEGORY,
  1214. 'forum' => TOOL_FORUM,
  1215. 'thread' => TOOL_FORUM_THREAD,
  1216. ];
  1217. api_item_property_update(
  1218. $_course,
  1219. $constants[$content],
  1220. $id,
  1221. $target_visibility,
  1222. api_get_user_id()
  1223. );
  1224. if ($target_visibility == 'visible') {
  1225. handle_mail_cue($content, $id);
  1226. }
  1227. return get_lang('VisibilityChanged');
  1228. }
  1229. /**
  1230. * This function changes the lock status in the database.
  1231. *
  1232. * @param string $content what is it that we want to (un)lock: forum category, forum, thread, post
  1233. * @param int $id the id of the content we want to (un)lock
  1234. * @param string $action do we lock (=>locked value in db = 1) or unlock (=> locked value in db = 0)
  1235. *
  1236. * @return string language variable
  1237. *
  1238. * @todo move to item manager
  1239. *
  1240. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1241. *
  1242. * @version february 2006, dokeos 1.8
  1243. */
  1244. function change_lock_status($content, $id, $action)
  1245. {
  1246. $table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
  1247. $table_forums = Database::get_course_table(TABLE_FORUM);
  1248. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1249. // Determine the relevant table.
  1250. if ($content == 'forumcategory') {
  1251. $table = $table_categories;
  1252. $id_field = 'cat_id';
  1253. } elseif ($content == 'forum') {
  1254. $table = $table_forums;
  1255. $id_field = 'forum_id';
  1256. } elseif ($content == 'thread') {
  1257. $table = $table_threads;
  1258. $id_field = 'thread_id';
  1259. } else {
  1260. return get_lang('Error');
  1261. }
  1262. // Determine what we are doing => defines the value for the database and the return message.
  1263. if ($action == 'lock') {
  1264. $db_locked = 1;
  1265. $return_message = get_lang('Locked');
  1266. } elseif ($action == 'unlock') {
  1267. $db_locked = 0;
  1268. $return_message = get_lang('Unlocked');
  1269. } else {
  1270. return get_lang('Error');
  1271. }
  1272. $course_id = api_get_course_int_id();
  1273. // Doing the change in the database
  1274. $sql = "UPDATE $table SET locked='".Database::escape_string($db_locked)."'
  1275. WHERE c_id = $course_id AND $id_field='".Database::escape_string($id)."'";
  1276. if (Database::query($sql)) {
  1277. return $return_message;
  1278. } else {
  1279. return get_lang('Error');
  1280. }
  1281. }
  1282. /**
  1283. * This function moves a forum or a forum category up or down.
  1284. *
  1285. * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post
  1286. * @param $direction do we want to move it up or down
  1287. * @param $id the id of the content we want to make invisible
  1288. *
  1289. * @todo consider removing the table_item_property calls here but this can
  1290. * prevent unwanted side effects when a forum does not have an entry in
  1291. * the item_property table but does have one in the forum table.
  1292. *
  1293. * @return string language variable
  1294. *
  1295. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1296. *
  1297. * @version february 2006, dokeos 1.8
  1298. */
  1299. function move_up_down($content, $direction, $id)
  1300. {
  1301. $table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
  1302. $table_forums = Database::get_course_table(TABLE_FORUM);
  1303. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1304. $course_id = api_get_course_int_id();
  1305. $id = intval($id);
  1306. // Determine which field holds the sort order.
  1307. if ($content == 'forumcategory') {
  1308. $table = $table_categories;
  1309. $sort_column = 'cat_order';
  1310. $id_column = 'cat_id';
  1311. $sort_column = 'cat_order';
  1312. } elseif ($content == 'forum') {
  1313. $table = $table_forums;
  1314. $sort_column = 'forum_order';
  1315. $id_column = 'forum_id';
  1316. $sort_column = 'forum_order';
  1317. // We also need the forum_category of this forum.
  1318. $sql = "SELECT forum_category FROM $table_forums
  1319. WHERE c_id = $course_id AND forum_id = ".intval($id);
  1320. $result = Database::query($sql);
  1321. $row = Database::fetch_array($result);
  1322. $forum_category = $row['forum_category'];
  1323. } else {
  1324. return get_lang('Error');
  1325. }
  1326. // Determine the need for sorting ascending or descending order.
  1327. if ($direction == 'down') {
  1328. $sort_direction = 'ASC';
  1329. } elseif ($direction == 'up') {
  1330. $sort_direction = 'DESC';
  1331. } else {
  1332. return get_lang('Error');
  1333. }
  1334. // The SQL statement
  1335. if ($content == 'forumcategory') {
  1336. $sql = "SELECT *
  1337. FROM $table_categories forum_categories, $table_item_property item_properties
  1338. WHERE
  1339. forum_categories.c_id = $course_id AND
  1340. item_properties.c_id = $course_id AND
  1341. forum_categories.cat_id=item_properties.ref AND
  1342. item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1343. ORDER BY forum_categories.cat_order $sort_direction";
  1344. }
  1345. if ($content == 'forum') {
  1346. $sql = "SELECT *
  1347. FROM $table
  1348. WHERE
  1349. c_id = $course_id AND
  1350. forum_category='".Database::escape_string($forum_category)."'
  1351. ORDER BY forum_order $sort_direction";
  1352. }
  1353. // Finding the items that need to be switched.
  1354. $result = Database::query($sql);
  1355. $found = false;
  1356. while ($row = Database::fetch_array($result)) {
  1357. if ($found) {
  1358. $next_id = $row[$id_column];
  1359. $next_sort = $row[$sort_column];
  1360. $found = false;
  1361. }
  1362. if ($id == $row[$id_column]) {
  1363. $this_id = $id;
  1364. $this_sort = $row[$sort_column];
  1365. $found = true;
  1366. }
  1367. }
  1368. // Committing the switch.
  1369. // We do an extra check if we do not have illegal values. If your remove this if statement you will
  1370. // be able to mess with the sorting by refreshing the page over and over again.
  1371. if ($this_sort != '' && $next_sort != '' && $next_id != '' && $this_id != '') {
  1372. $sql = "UPDATE $table SET $sort_column='".Database::escape_string($this_sort)."'
  1373. WHERE c_id = $course_id AND $id_column='".Database::escape_string($next_id)."'";
  1374. Database::query($sql);
  1375. $sql = "UPDATE $table SET $sort_column='".Database::escape_string($next_sort)."'
  1376. WHERE c_id = $course_id AND $id_column='".Database::escape_string($this_id)."'";
  1377. Database::query($sql);
  1378. }
  1379. return get_lang(ucfirst($content).'Moved');
  1380. }
  1381. /**
  1382. * Retrieve all the information off the forum categories (or one specific) for the current course.
  1383. * The categories are sorted according to their sorting order (cat_order.
  1384. *
  1385. * @param int|string $id default ''. When an id is passed we only find the information
  1386. * about that specific forum category. If no id is passed we get all the forum categories.
  1387. * @param int $courseId Optional. The course ID
  1388. * @param int $sessionId Optional. The session ID
  1389. *
  1390. * @return array containing all the information about all the forum categories
  1391. *
  1392. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1393. *
  1394. * @version february 2006, dokeos 1.8
  1395. */
  1396. function get_forum_categories($id = '', $courseId = 0, $sessionId = 0)
  1397. {
  1398. $table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
  1399. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1400. // Condition for the session
  1401. $session_id = $sessionId ?: api_get_session_id();
  1402. $course_id = $courseId ?: api_get_course_int_id();
  1403. $condition_session = api_get_session_condition(
  1404. $session_id,
  1405. true,
  1406. true,
  1407. 'forum_categories.session_id'
  1408. );
  1409. $condition_session .= " AND forum_categories.c_id = $course_id AND item_properties.c_id = $course_id";
  1410. if (empty($id)) {
  1411. $sql = "SELECT *
  1412. FROM $table_item_property item_properties
  1413. INNER JOIN $table_categories forum_categories
  1414. ON (
  1415. forum_categories.cat_id = item_properties.ref AND
  1416. item_properties.c_id = forum_categories.c_id
  1417. )
  1418. WHERE
  1419. item_properties.visibility = 1 AND
  1420. item_properties.tool = '".TOOL_FORUM_CATEGORY."'
  1421. $condition_session
  1422. ORDER BY forum_categories.cat_order ASC";
  1423. if (api_is_allowed_to_edit()) {
  1424. $sql = "SELECT *
  1425. FROM $table_item_property item_properties
  1426. INNER JOIN $table_categories forum_categories
  1427. ON (
  1428. forum_categories.cat_id = item_properties.ref AND
  1429. item_properties.c_id = forum_categories.c_id
  1430. )
  1431. WHERE
  1432. item_properties.visibility<>2 AND
  1433. item_properties.tool='".TOOL_FORUM_CATEGORY."'
  1434. $condition_session
  1435. ORDER BY forum_categories.cat_order ASC";
  1436. }
  1437. } else {
  1438. $sql = "SELECT *
  1439. FROM $table_item_property item_properties
  1440. INNER JOIN $table_categories forum_categories
  1441. ON (
  1442. forum_categories.cat_id = item_properties.ref AND
  1443. item_properties.c_id = forum_categories.c_id
  1444. )
  1445. WHERE
  1446. item_properties.tool='".TOOL_FORUM_CATEGORY."' AND
  1447. forum_categories.cat_id = ".intval($id)."
  1448. $condition_session
  1449. ORDER BY forum_categories.cat_order ASC";
  1450. }
  1451. $result = Database::query($sql);
  1452. $forum_categories_list = [];
  1453. $extraFieldValue = new ExtraFieldValue('forum_category');
  1454. while ($row = Database::fetch_assoc($result)) {
  1455. $row['extra_fields'] = $extraFieldValue->getAllValuesByItem($row['cat_id']);
  1456. if (empty($id)) {
  1457. $forum_categories_list[$row['cat_id']] = $row;
  1458. } else {
  1459. $forum_categories_list = $row;
  1460. }
  1461. }
  1462. return $forum_categories_list;
  1463. }
  1464. /**
  1465. * This function retrieves all the fora in a given forum category.
  1466. *
  1467. * @param int $cat_id the id of the forum category
  1468. * @param int $courseId Optional. The course ID
  1469. *
  1470. * @return array containing all the information about the forums (regardless of their category)
  1471. *
  1472. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1473. *
  1474. * @version february 2006, dokeos 1.8
  1475. */
  1476. function get_forums_in_category($cat_id, $courseId = 0)
  1477. {
  1478. $table_forums = Database::get_course_table(TABLE_FORUM);
  1479. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1480. $forum_list = [];
  1481. $course_id = $courseId ?: api_get_course_int_id();
  1482. $cat_id = (int) $cat_id;
  1483. $sql = "SELECT * FROM $table_forums forum
  1484. INNER JOIN $table_item_property item_properties
  1485. ON (forum.forum_id = item_properties.ref AND item_properties.c_id = forum.c_id)
  1486. WHERE
  1487. forum.forum_category = '".$cat_id."' AND
  1488. item_properties.visibility = 1 AND
  1489. forum.c_id = $course_id AND
  1490. item_properties.c_id = $course_id AND
  1491. item_properties.tool = '".TOOL_FORUM."'
  1492. ORDER BY forum.forum_order ASC";
  1493. if (api_is_allowed_to_edit()) {
  1494. $sql = "SELECT * FROM $table_forums forum
  1495. INNER JOIN $table_item_property item_properties
  1496. ON (forum.forum_id = item_properties.ref AND item_properties.c_id = forum.c_id)
  1497. WHERE
  1498. forum.forum_category = '".$cat_id."' AND
  1499. item_properties.visibility <> 2 AND
  1500. item_properties.tool = '".TOOL_FORUM."' AND
  1501. item_properties.c_id = $course_id AND
  1502. forum.c_id = $course_id
  1503. ORDER BY forum_order ASC";
  1504. }
  1505. $result = Database::query($sql);
  1506. while ($row = Database::fetch_array($result)) {
  1507. $forum_list[$row['forum_id']] = $row;
  1508. }
  1509. return $forum_list;
  1510. }
  1511. /**
  1512. * Retrieve all the forums (regardless of their category) or of only one.
  1513. * The forums are sorted according to the forum_order.
  1514. * Since it does not take the forum category into account there probably
  1515. * will be two or more forums that have forum_order=1, ...
  1516. *
  1517. * @param int $id forum id
  1518. * @param string $course_code
  1519. * @param bool $includeGroupsForum
  1520. * @param int $sessionId
  1521. *
  1522. * @return array an array containing all the information about the forums (regardless of their category)
  1523. *
  1524. * @todo check $sql4 because this one really looks fishy.
  1525. *
  1526. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1527. *
  1528. * @version february 2006, dokeos 1.8
  1529. */
  1530. function get_forums(
  1531. $id = 0,
  1532. $course_code = '',
  1533. $includeGroupsForum = true,
  1534. $sessionId = 0
  1535. ) {
  1536. $id = (int) $id;
  1537. $course_info = api_get_course_info($course_code);
  1538. $table_forums = Database::get_course_table(TABLE_FORUM);
  1539. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1540. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1541. // Condition for the session
  1542. $session_id = intval($sessionId) ?: api_get_session_id();
  1543. $sessionIdLink = $session_id === 0 ? '' : ' AND threads.session_id = item_properties.session_id';
  1544. $condition_session = api_get_session_condition(
  1545. $session_id,
  1546. true,
  1547. false,
  1548. 'item_properties.session_id'
  1549. );
  1550. $course_id = $course_info['real_id'];
  1551. $forum_list = [];
  1552. $includeGroupsForumSelect = '';
  1553. if (!$includeGroupsForum) {
  1554. $includeGroupsForumSelect = " AND (forum_of_group = 0 OR forum_of_group IS NULL) ";
  1555. }
  1556. $allowToEdit = api_is_allowed_to_edit();
  1557. if (empty($id)) {
  1558. // Student
  1559. // Select all the forum information of all forums (that are visible to students).
  1560. $sql = "SELECT item_properties.*, forum.*
  1561. FROM $table_forums forum
  1562. INNER JOIN $table_item_property item_properties
  1563. ON (
  1564. forum.forum_id = item_properties.ref AND
  1565. forum.c_id = item_properties.c_id
  1566. )
  1567. WHERE
  1568. item_properties.visibility = 1 AND
  1569. item_properties.tool = '".TOOL_FORUM."'
  1570. $condition_session AND
  1571. forum.c_id = $course_id AND
  1572. item_properties.c_id = $course_id
  1573. $includeGroupsForumSelect
  1574. ORDER BY forum.forum_order ASC";
  1575. // Select the number of threads of the forums (only the threads that are visible).
  1576. $sql2 = "SELECT count(*) AS number_of_threads, threads.forum_id
  1577. FROM $table_threads threads
  1578. INNER JOIN $table_item_property item_properties
  1579. ON (
  1580. threads.thread_id = item_properties.ref AND
  1581. threads.c_id = item_properties.c_id
  1582. $sessionIdLink
  1583. )
  1584. WHERE
  1585. item_properties.visibility=1 AND
  1586. item_properties.tool='".TOOL_FORUM_THREAD."' AND
  1587. threads.c_id = $course_id AND
  1588. item_properties.c_id = $course_id
  1589. GROUP BY threads.forum_id";
  1590. // Course Admin
  1591. if ($allowToEdit) {
  1592. // Select all the forum information of all forums (that are not deleted).
  1593. $sql = "SELECT item_properties.*, forum.*
  1594. FROM $table_forums forum
  1595. INNER JOIN $table_item_property item_properties
  1596. ON (
  1597. forum.forum_id = item_properties.ref AND
  1598. forum.c_id = item_properties.c_id
  1599. )
  1600. WHERE
  1601. item_properties.visibility <> 2 AND
  1602. item_properties.tool = '".TOOL_FORUM."'
  1603. $condition_session AND
  1604. forum.c_id = $course_id AND
  1605. item_properties.c_id = $course_id
  1606. $includeGroupsForumSelect
  1607. ORDER BY forum_order ASC";
  1608. // Select the number of threads of the forums (only the threads that are not deleted).
  1609. $sql2 = "SELECT count(*) AS number_of_threads, threads.forum_id
  1610. FROM $table_threads threads
  1611. INNER JOIN $table_item_property item_properties
  1612. ON (
  1613. threads.thread_id = item_properties.ref AND
  1614. threads.c_id = item_properties.c_id
  1615. $sessionIdLink
  1616. )
  1617. WHERE
  1618. item_properties.visibility<>2 AND
  1619. item_properties.tool='".TOOL_FORUM_THREAD."' AND
  1620. threads.c_id = $course_id AND
  1621. item_properties.c_id = $course_id
  1622. GROUP BY threads.forum_id";
  1623. }
  1624. } else {
  1625. // GETTING ONE SPECIFIC FORUM
  1626. /* We could do the splitup into student and course admin also but we want
  1627. to have as much as information about a certain forum as possible
  1628. so we do not take too much information into account. This function
  1629. (or this section of the function) is namely used to fill the forms
  1630. when editing a forum (and for the moment it is the only place where
  1631. we use this part of the function) */
  1632. // Select all the forum information of the given forum (that is not deleted).
  1633. $sql = "SELECT * FROM $table_item_property item_properties
  1634. INNER JOIN $table_forums forum
  1635. ON (forum.forum_id = item_properties.ref AND forum.c_id = item_properties.c_id)
  1636. WHERE
  1637. forum.forum_id = $id AND
  1638. forum.c_id = $course_id AND
  1639. item_properties.visibility != 2 AND
  1640. item_properties.tool = '".TOOL_FORUM."'
  1641. ORDER BY forum_order ASC";
  1642. // Select the number of threads of the forum.
  1643. $sql2 = "SELECT count(*) AS number_of_threads, forum_id
  1644. FROM $table_threads
  1645. WHERE
  1646. forum_id = $id
  1647. GROUP BY forum_id";
  1648. }
  1649. // Handling all the forum information.
  1650. $result = Database::query($sql);
  1651. while ($row = Database::fetch_assoc($result)) {
  1652. if (empty($id)) {
  1653. $forum_list[$row['forum_id']] = $row;
  1654. } else {
  1655. $forum_list = $row;
  1656. }
  1657. }
  1658. // Handling the thread count information.
  1659. $result2 = Database::query($sql2);
  1660. while ($row2 = Database::fetch_array($result2)) {
  1661. if (empty($id)) {
  1662. $forum_list[$row2['forum_id']]['number_of_threads'] = $row2['number_of_threads'];
  1663. } else {
  1664. $forum_list['number_of_threads'] = $row2['number_of_threads'];
  1665. }
  1666. }
  1667. /* Finding the last post information
  1668. (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)*/
  1669. if (empty($id)) {
  1670. if (is_array($forum_list)) {
  1671. foreach ($forum_list as $key => $value) {
  1672. $lastPost = get_last_post_information(
  1673. $key,
  1674. $allowToEdit,
  1675. $course_id
  1676. );
  1677. if ($lastPost) {
  1678. $forum_list[$key]['last_post_id'] = $lastPost['last_post_id'];
  1679. $forum_list[$key]['last_poster_id'] = $lastPost['last_poster_id'];
  1680. $forum_list[$key]['last_post_date'] = $lastPost['last_post_date'];
  1681. $forum_list[$key]['last_poster_name'] = $lastPost['last_poster_name'];
  1682. $forum_list[$key]['last_poster_lastname'] = $lastPost['last_poster_lastname'];
  1683. $forum_list[$key]['last_poster_firstname'] = $lastPost['last_poster_firstname'];
  1684. $forum_list[$key]['last_post_title'] = $lastPost['last_post_title'];
  1685. $forum_list[$key]['last_post_text'] = $lastPost['last_post_text'];
  1686. }
  1687. }
  1688. } else {
  1689. $forum_list = [];
  1690. }
  1691. } else {
  1692. $lastPost = get_last_post_information(
  1693. $id,
  1694. $allowToEdit,
  1695. $course_id
  1696. );
  1697. if ($lastPost) {
  1698. $forum_list['last_post_id'] = $lastPost['last_post_id'];
  1699. $forum_list['last_poster_id'] = $lastPost['last_poster_id'];
  1700. $forum_list['last_post_date'] = $lastPost['last_post_date'];
  1701. $forum_list['last_poster_name'] = $lastPost['last_poster_name'];
  1702. $forum_list['last_poster_lastname'] = $lastPost['last_poster_lastname'];
  1703. $forum_list['last_poster_firstname'] = $lastPost['last_poster_firstname'];
  1704. $forum_list['last_post_title'] = $lastPost['last_post_title'];
  1705. $forum_list['last_post_text'] = $lastPost['last_post_text'];
  1706. }
  1707. }
  1708. return $forum_list;
  1709. }
  1710. /**
  1711. * @param int $course_id
  1712. * @param int $thread_id
  1713. * @param int $forum_id
  1714. * @param bool $show_visible
  1715. *
  1716. * @return array|bool
  1717. */
  1718. function get_last_post_by_thread($course_id, $thread_id, $forum_id, $show_visible = true)
  1719. {
  1720. if (empty($thread_id) || empty($forum_id) || empty($course_id)) {
  1721. return false;
  1722. }
  1723. $thread_id = intval($thread_id);
  1724. $forum_id = intval($forum_id);
  1725. $course_id = intval($course_id);
  1726. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  1727. $sql = "SELECT * FROM $table_posts
  1728. WHERE
  1729. c_id = $course_id AND
  1730. thread_id = $thread_id AND
  1731. forum_id = $forum_id";
  1732. if ($show_visible == false) {
  1733. $sql .= " AND visible = 1 ";
  1734. }
  1735. $sql .= " ORDER BY post_id DESC LIMIT 1";
  1736. $result = Database::query($sql);
  1737. if (Database::num_rows($result)) {
  1738. return Database::fetch_array($result, 'ASSOC');
  1739. } else {
  1740. return false;
  1741. }
  1742. }
  1743. /**
  1744. * This function gets all the last post information of a certain forum.
  1745. *
  1746. * @param int $forum_id the id of the forum we want to know the last post information of
  1747. * @param bool $show_invisibles
  1748. * @param string course db name
  1749. * @param int $sessionId Optional. The session id
  1750. *
  1751. * @return array containing all the information about the last post
  1752. * (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)
  1753. *
  1754. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1755. *
  1756. * @version february 2006, dokeos 1.8
  1757. */
  1758. function get_last_post_information($forum_id, $show_invisibles = false, $course_id = null, $sessionId = 0)
  1759. {
  1760. if (!isset($course_id)) {
  1761. $course_id = api_get_course_int_id();
  1762. } else {
  1763. $course_id = intval($course_id);
  1764. }
  1765. $sessionId = $sessionId ? intval($sessionId) : api_get_session_id();
  1766. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  1767. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1768. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  1769. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1770. $forum_id = intval($forum_id);
  1771. $return_array = [];
  1772. // First get the threads to make sure there is no inconsistency in the
  1773. // database between forum and thread
  1774. $sql = "SELECT thread_id FROM $table_threads
  1775. WHERE
  1776. forum_id = $forum_id AND
  1777. c_id = $course_id AND
  1778. session_id = $sessionId";
  1779. $result = Database::query($sql);
  1780. if (Database::num_rows($result) == 0) {
  1781. // If there are no threads in this forum, then there are no posts
  1782. return [];
  1783. }
  1784. $threads = [];
  1785. while ($row = Database::fetch_row($result)) {
  1786. $threads[] = $row[0];
  1787. }
  1788. $threadsList = implode(',', $threads);
  1789. // Now get the posts that are linked to these threads
  1790. $sql = "SELECT
  1791. post.post_id,
  1792. post.forum_id,
  1793. post.poster_id,
  1794. post.poster_name,
  1795. post.post_date,
  1796. users.lastname,
  1797. users.firstname,
  1798. post.visible,
  1799. thread_properties.visibility AS thread_visibility,
  1800. forum_properties.visibility AS forum_visibility,
  1801. post.post_title,
  1802. post.post_text
  1803. FROM
  1804. $table_posts post,
  1805. $table_users users,
  1806. $table_item_property thread_properties,
  1807. $table_item_property forum_properties
  1808. WHERE
  1809. post.forum_id = $forum_id
  1810. AND post.thread_id IN ($threadsList)
  1811. AND post.poster_id = users.user_id
  1812. AND post.thread_id = thread_properties.ref
  1813. AND thread_properties.tool='".TOOL_FORUM_THREAD."'
  1814. AND post.forum_id=forum_properties.ref
  1815. AND forum_properties.tool='".TOOL_FORUM."'
  1816. AND post.c_id = $course_id AND
  1817. thread_properties.c_id = $course_id AND
  1818. forum_properties.c_id = $course_id
  1819. ORDER BY post.post_id DESC";
  1820. $result = Database::query($sql);
  1821. if ($show_invisibles) {
  1822. $row = Database::fetch_array($result);
  1823. $return_array['last_post_id'] = $row['post_id'];
  1824. $return_array['last_poster_id'] = $row['poster_id'];
  1825. $return_array['last_post_date'] = $row['post_date'];
  1826. $return_array['last_poster_name'] = $row['poster_name'];
  1827. $return_array['last_poster_lastname'] = $row['lastname'];
  1828. $return_array['last_poster_firstname'] = $row['firstname'];
  1829. $return_array['last_post_title'] = $row['post_title'];
  1830. $return_array['last_post_text'] = $row['post_text'];
  1831. return $return_array;
  1832. } else {
  1833. // We have to loop through the results to find the first one that is
  1834. // actually visible to students (forum_category, forum, thread AND post are visible).
  1835. while ($row = Database::fetch_array($result)) {
  1836. if ($row['visible'] == '1' && $row['thread_visibility'] == '1' && $row['forum_visibility'] == '1') {
  1837. $return_array['last_post_id'] = $row['post_id'];
  1838. $return_array['last_poster_id'] = $row['poster_id'];
  1839. $return_array['last_post_date'] = $row['post_date'];
  1840. $return_array['last_poster_name'] = $row['poster_name'];
  1841. $return_array['last_poster_lastname'] = $row['lastname'];
  1842. $return_array['last_poster_firstname'] = $row['firstname'];
  1843. $return_array['last_post_title'] = $row['post_title'];
  1844. $return_array['last_post_text'] = $row['post_text'];
  1845. return $return_array;
  1846. }
  1847. }
  1848. }
  1849. }
  1850. /**
  1851. * Retrieve all the threads of a given forum.
  1852. *
  1853. * @param int $forum_id
  1854. * @param int|null $courseId Optional If is null then it is considered the current course
  1855. * @param int|null $sessionId Optional. If is null then it is considered the current session
  1856. *
  1857. * @return array containing all the information about the threads
  1858. *
  1859. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1860. *
  1861. * @version february 2006, dokeos 1.8
  1862. */
  1863. function get_threads($forum_id, $courseId = null, $sessionId = null)
  1864. {
  1865. $groupId = api_get_group_id();
  1866. $sessionId = $sessionId !== null ? (int) $sessionId : api_get_session_id();
  1867. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1868. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1869. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  1870. $courseId = $courseId !== null ? (int) $courseId : api_get_course_int_id();
  1871. $groupInfo = GroupManager::get_group_properties($groupId);
  1872. $groupCondition = '';
  1873. if (!empty($groupInfo)) {
  1874. $groupIid = $groupInfo['iid'];
  1875. $groupCondition = " AND item_properties.to_group_id = '$groupIid' ";
  1876. }
  1877. $sessionCondition = api_get_session_condition(
  1878. $sessionId,
  1879. true,
  1880. false,
  1881. 'item_properties.session_id'
  1882. );
  1883. // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement
  1884. // because we also have thread.* in it. This is because thread has a field locked and post also has the same field
  1885. // since we are merging these we would have the post.locked value but in fact we want the thread.locked value
  1886. // This is why it is added to the end of the field selection
  1887. $sql = "SELECT DISTINCT
  1888. item_properties.*,
  1889. users.firstname,
  1890. users.lastname,
  1891. users.user_id,
  1892. thread.locked as locked,
  1893. thread.*
  1894. FROM $table_threads thread
  1895. INNER JOIN $table_item_property item_properties
  1896. ON
  1897. thread.thread_id = item_properties.ref AND
  1898. item_properties.c_id = thread.c_id AND
  1899. item_properties.tool = '".TABLE_FORUM_THREAD."'
  1900. $groupCondition
  1901. $sessionCondition
  1902. LEFT JOIN $table_users users
  1903. ON thread.thread_poster_id = users.user_id
  1904. WHERE
  1905. item_properties.visibility='1' AND
  1906. thread.forum_id = ".intval($forum_id)." AND
  1907. thread.c_id = $courseId
  1908. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1909. if (api_is_allowed_to_edit()) {
  1910. $sql = "SELECT DISTINCT
  1911. item_properties.*,
  1912. users.firstname,
  1913. users.lastname,
  1914. users.user_id,
  1915. thread.locked as locked,
  1916. thread.*
  1917. FROM $table_threads thread
  1918. INNER JOIN $table_item_property item_properties
  1919. ON
  1920. thread.thread_id = item_properties.ref AND
  1921. item_properties.c_id = thread.c_id AND
  1922. item_properties.tool = '".TABLE_FORUM_THREAD."'
  1923. $groupCondition
  1924. $sessionCondition
  1925. LEFT JOIN $table_users users
  1926. ON thread.thread_poster_id=users.user_id
  1927. WHERE
  1928. item_properties.visibility<>2 AND
  1929. thread.forum_id = ".intval($forum_id)." AND
  1930. thread.c_id = $courseId
  1931. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1932. }
  1933. $result = Database::query($sql);
  1934. $list = [];
  1935. $alreadyAdded = [];
  1936. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1937. if (in_array($row['thread_id'], $alreadyAdded)) {
  1938. continue;
  1939. }
  1940. $list[] = $row;
  1941. $alreadyAdded[] = $row['thread_id'];
  1942. }
  1943. return $list;
  1944. }
  1945. /**
  1946. * Get a thread by Id and course id.
  1947. *
  1948. * @param int $threadId the thread Id
  1949. * @param int $cId the course id
  1950. *
  1951. * @return array containing all the information about the thread
  1952. */
  1953. function getThreadInfo($threadId, $cId)
  1954. {
  1955. $repo = Database::getManager()->getRepository('ChamiloCourseBundle:CForumThread');
  1956. $forumThread = $repo->findOneBy(['threadId' => $threadId, 'cId' => $cId]);
  1957. $thread = [];
  1958. if ($forumThread) {
  1959. $thread['threadId'] = $forumThread->getThreadId();
  1960. $thread['threadTitle'] = $forumThread->getThreadTitle();
  1961. $thread['forumId'] = $forumThread->getForumId();
  1962. $thread['sessionId'] = $forumThread->getSessionId();
  1963. $thread['threadSticky'] = $forumThread->getThreadSticky();
  1964. $thread['locked'] = $forumThread->getLocked();
  1965. $thread['threadTitleQualify'] = $forumThread->getThreadTitleQualify();
  1966. $thread['threadQualifyMax'] = $forumThread->getThreadQualifyMax();
  1967. $thread['threadCloseDate'] = $forumThread->getThreadCloseDate();
  1968. $thread['threadWeight'] = $forumThread->getThreadWeight();
  1969. $thread['threadPeerQualify'] = $forumThread->isThreadPeerQualify();
  1970. }
  1971. return $thread;
  1972. }
  1973. /**
  1974. * Retrieve all posts of a given thread.
  1975. *
  1976. * @param array $forumInfo
  1977. * @param int $threadId The thread ID
  1978. * @param string $orderDirection Optional. The direction for sort the posts
  1979. * @param bool $recursive Optional. If the list is recursive
  1980. * @param int $postId Optional. The post ID for recursive list
  1981. * @param int $depth Optional. The depth to indicate the indent
  1982. *
  1983. * @todo move to a repository
  1984. *
  1985. * @return array containing all the information about the posts of a given thread
  1986. */
  1987. function getPosts(
  1988. $forumInfo,
  1989. $threadId,
  1990. $orderDirection = 'ASC',
  1991. $recursive = false,
  1992. $postId = null,
  1993. $depth = -1
  1994. ) {
  1995. $em = Database::getManager();
  1996. if (api_is_allowed_to_edit(false, true)) {
  1997. $visibleCriteria = Criteria::expr()->neq('visible', 2);
  1998. } else {
  1999. $visibleCriteria = Criteria::expr()->eq('visible', 1);
  2000. }
  2001. $criteria = Criteria::create();
  2002. $criteria
  2003. ->where(Criteria::expr()->eq('threadId', $threadId))
  2004. ->andWhere(Criteria::expr()->eq('cId', $forumInfo['c_id']))
  2005. ->andWhere($visibleCriteria)
  2006. ;
  2007. $groupId = api_get_group_id();
  2008. $groupInfo = GroupManager::get_group_properties($groupId);
  2009. $filterModerated = true;
  2010. if (empty($groupId)) {
  2011. if (api_is_allowed_to_edit()) {
  2012. $filterModerated = false;
  2013. }
  2014. } else {
  2015. if (GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo) ||
  2016. api_is_allowed_to_edit(false, true)
  2017. ) {
  2018. $filterModerated = false;
  2019. }
  2020. }
  2021. if ($recursive) {
  2022. $criteria->andWhere(Criteria::expr()->eq('postParentId', $postId));
  2023. }
  2024. $qb = $em->getRepository('ChamiloCourseBundle:CForumPost')->createQueryBuilder('p');
  2025. $qb->select('p')
  2026. ->addCriteria($criteria)
  2027. ->addOrderBy('p.postId', $orderDirection);
  2028. if ($filterModerated && $forumInfo['moderated'] == 1) {
  2029. if (!api_is_allowed_to_edit(false, true)) {
  2030. $userId = api_get_user_id();
  2031. $qb->andWhere(
  2032. "p.status = 1 OR
  2033. (p.status = ".CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = $userId) OR
  2034. (p.status = ".CForumPost::STATUS_REJECTED." AND p.posterId = $userId) OR
  2035. (p.status IS NULL AND p.posterId = $userId)
  2036. "
  2037. );
  2038. }
  2039. }
  2040. $posts = $qb->getQuery()->getResult();
  2041. $depth++;
  2042. $list = [];
  2043. /** @var CForumPost $post */
  2044. foreach ($posts as $post) {
  2045. $postInfo = [
  2046. 'iid' => $post->getIid(),
  2047. 'c_id' => $post->getCId(),
  2048. 'post_id' => $post->getPostId(),
  2049. 'post_title' => $post->getPostTitle(),
  2050. 'post_text' => $post->getPostText(),
  2051. 'thread_id' => $post->getThreadId(),
  2052. 'forum_id' => $post->getForumId(),
  2053. 'poster_id' => $post->getPosterId(),
  2054. 'poster_name' => $post->getPosterName(),
  2055. 'post_date' => $post->getPostDate(),
  2056. 'post_notification' => $post->getPostNotification(),
  2057. 'post_parent_id' => $post->getPostParentId(),
  2058. 'visible' => $post->getVisible(),
  2059. 'status' => $post->getStatus(),
  2060. 'indent_cnt' => $depth,
  2061. ];
  2062. $posterId = $post->getPosterId();
  2063. if (!empty($posterId)) {
  2064. $user = api_get_user_entity($posterId);
  2065. if ($user) {
  2066. $postInfo['user_id'] = $user->getUserId();
  2067. $postInfo['username'] = $user->getUsername();
  2068. $postInfo['username_canonical'] = $user->getUsernameCanonical();
  2069. $postInfo['lastname'] = $user->getLastname();
  2070. $postInfo['firstname'] = $user->getFirstname();
  2071. $postInfo['complete_name'] = $user->getCompleteName();
  2072. }
  2073. }
  2074. $list[] = $postInfo;
  2075. if (!$recursive) {
  2076. continue;
  2077. }
  2078. $list = array_merge(
  2079. $list,
  2080. getPosts(
  2081. $forumInfo,
  2082. $threadId,
  2083. $orderDirection,
  2084. $recursive,
  2085. $post->getPostId(),
  2086. $depth
  2087. )
  2088. );
  2089. }
  2090. return $list;
  2091. }
  2092. /**
  2093. * This function retrieves all the information of a post.
  2094. *
  2095. * @param int $post_id integer that indicates the forum
  2096. *
  2097. * @return array returns
  2098. *
  2099. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2100. *
  2101. * @version february 2006, dokeos 1.8
  2102. */
  2103. function get_post_information($post_id)
  2104. {
  2105. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  2106. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  2107. $course_id = api_get_course_int_id();
  2108. $post_id = (int) $post_id;
  2109. if (empty($post_id)) {
  2110. return [];
  2111. }
  2112. $sql = "SELECT posts.*, email FROM ".$table_posts." posts, ".$table_users." users
  2113. WHERE
  2114. c_id = $course_id AND
  2115. posts.poster_id=users.user_id AND
  2116. posts.post_id = ".$post_id;
  2117. $result = Database::query($sql);
  2118. $row = Database::fetch_array($result, 'ASSOC');
  2119. return $row;
  2120. }
  2121. /**
  2122. * This function retrieves all the information of a thread.
  2123. *
  2124. * @param int $forumId
  2125. * @param $thread_id integer that indicates the forum
  2126. * @param int|null $sessionId Optional. If is null then it is considered the current session
  2127. *
  2128. * @return array returns
  2129. *
  2130. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2131. *
  2132. * @version february 2006, dokeos 1.8
  2133. */
  2134. function get_thread_information($forumId, $thread_id, $sessionId = null)
  2135. {
  2136. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2137. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  2138. $thread_id = intval($thread_id);
  2139. $sessionId = $sessionId !== null ? intval($sessionId) : api_get_session_id();
  2140. $sessionCondition = api_get_session_condition(
  2141. $sessionId,
  2142. true,
  2143. false,
  2144. 'threads.session_id'
  2145. );
  2146. $forumCondition = '';
  2147. if (!empty($forumId)) {
  2148. $forumId = (int) $forumId;
  2149. $forumCondition = " threads.forum_id = $forumId AND ";
  2150. }
  2151. $sql = "SELECT * FROM $table_item_property item_properties
  2152. INNER JOIN
  2153. $table_threads threads
  2154. ON (item_properties.ref = threads.thread_id AND threads.c_id = item_properties.c_id)
  2155. WHERE
  2156. $forumCondition
  2157. item_properties.tool= '".TOOL_FORUM_THREAD."' AND
  2158. threads.thread_id = $thread_id
  2159. $sessionCondition
  2160. ";
  2161. $result = Database::query($sql);
  2162. $row = Database::fetch_assoc($result);
  2163. return $row;
  2164. }
  2165. /**
  2166. * This function retrieves forum thread users details.
  2167. *
  2168. * @param int Thread ID
  2169. * @param string Course DB name (optional)
  2170. *
  2171. * @return Doctrine\DBAL\Driver\Statement|null array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  2172. *
  2173. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  2174. *
  2175. * @todo this function need to be improved
  2176. *
  2177. * @version octubre 2008, dokeos 1.8
  2178. */
  2179. function get_thread_users_details($thread_id)
  2180. {
  2181. $t_posts = Database::get_course_table(TABLE_FORUM_POST);
  2182. $t_users = Database::get_main_table(TABLE_MAIN_USER);
  2183. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2184. $t_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2185. $course_id = api_get_course_int_id();
  2186. $is_western_name_order = api_is_western_name_order();
  2187. if ($is_western_name_order) {
  2188. $orderby = 'ORDER BY user.firstname, user.lastname ';
  2189. } else {
  2190. $orderby = 'ORDER BY user.lastname, user.firstname';
  2191. }
  2192. if (api_get_session_id()) {
  2193. $session_info = api_get_session_info(api_get_session_id());
  2194. $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'";
  2195. //not showing coaches
  2196. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, thread_id
  2197. FROM $t_posts p, $t_users user, $t_session_rel_user session_rel_user_rel_course
  2198. WHERE
  2199. p.poster_id = user.id AND
  2200. user.id = session_rel_user_rel_course.user_id AND
  2201. session_rel_user_rel_course.status<>'2' AND
  2202. session_rel_user_rel_course.user_id NOT IN ($user_to_avoid) AND
  2203. p.thread_id = ".intval($thread_id)." AND
  2204. session_id = ".api_get_session_id()." AND
  2205. p.c_id = $course_id AND
  2206. session_rel_user_rel_course.c_id = ".$course_id." $orderby ";
  2207. } else {
  2208. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, thread_id
  2209. FROM $t_posts p, $t_users user, $t_course_user course_user
  2210. WHERE
  2211. p.poster_id = user.id
  2212. AND user.id = course_user.user_id
  2213. AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  2214. AND p.thread_id = ".intval($thread_id)."
  2215. AND course_user.status NOT IN('1') AND
  2216. p.c_id = $course_id AND
  2217. course_user.c_id = ".$course_id." $orderby";
  2218. }
  2219. $result = Database::query($sql);
  2220. return $result;
  2221. }
  2222. /**
  2223. * This function retrieves forum thread users qualify.
  2224. *
  2225. * @param int Thread ID
  2226. * @param string Course DB name (optional)
  2227. *
  2228. * @return Doctrine\DBAL\Driver\Statement|null Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  2229. *
  2230. * @author Jhon Hinojosa
  2231. *
  2232. * @todo this function need to be improved
  2233. */
  2234. function get_thread_users_qualify($thread_id)
  2235. {
  2236. $t_posts = Database::get_course_table(TABLE_FORUM_POST);
  2237. $t_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  2238. $t_users = Database::get_main_table(TABLE_MAIN_USER);
  2239. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2240. $t_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2241. $course_id = api_get_course_int_id();
  2242. $sessionId = api_get_session_id();
  2243. $is_western_name_order = api_is_western_name_order();
  2244. if ($is_western_name_order) {
  2245. $orderby = 'ORDER BY user.firstname, user.lastname ';
  2246. } else {
  2247. $orderby = 'ORDER BY user.lastname, user.firstname';
  2248. }
  2249. if ($sessionId) {
  2250. $session_info = api_get_session_info($sessionId);
  2251. $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'";
  2252. //not showing coaches
  2253. $sql = "SELECT DISTINCT post.poster_id, user.lastname, user.firstname, post.thread_id,user.id,qualify.qualify
  2254. FROM $t_posts post , $t_users user, $t_session_rel_user scu, $t_qualify qualify
  2255. WHERE poster_id = user.id
  2256. AND post.poster_id = qualify.user_id
  2257. AND user.id = scu.user_id
  2258. AND scu.status<>'2'
  2259. AND scu.user_id NOT IN ($user_to_avoid)
  2260. AND qualify.thread_id = ".intval($thread_id)."
  2261. AND post.thread_id = ".intval($thread_id)."
  2262. AND scu.session_id = $sessionId
  2263. AND scu.c_id = ".$course_id." AND
  2264. qualify.c_id = $course_id AND
  2265. post.c_id = $course_id
  2266. $orderby ";
  2267. } else {
  2268. $sql = "SELECT DISTINCT post.poster_id, user.lastname, user.firstname, post.thread_id,user.id,qualify.qualify
  2269. FROM $t_posts post,
  2270. $t_qualify qualify,
  2271. $t_users user,
  2272. $t_course_user course_user
  2273. WHERE
  2274. post.poster_id = user.id
  2275. AND post.poster_id = qualify.user_id
  2276. AND user.id = course_user.user_id
  2277. AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  2278. AND qualify.thread_id = ".intval($thread_id)."
  2279. AND post.thread_id = ".intval($thread_id)."
  2280. AND course_user.status not in('1')
  2281. AND course_user.c_id = $course_id
  2282. AND qualify.c_id = $course_id
  2283. AND post.c_id = $course_id
  2284. $orderby ";
  2285. }
  2286. $result = Database::query($sql);
  2287. return $result;
  2288. }
  2289. /**
  2290. * This function retrieves forum thread users not qualify.
  2291. *
  2292. * @param int Thread ID
  2293. * @param string Course DB name (optional)
  2294. *
  2295. * @return Doctrine\DBAL\Driver\Statement|null Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  2296. *
  2297. * @author Jhon Hinojosa<jhon.hinojosa@dokeos.com>,
  2298. *
  2299. * @version oct 2008, dokeos 1.8
  2300. */
  2301. function get_thread_users_not_qualify($thread_id)
  2302. {
  2303. $t_posts = Database::get_course_table(TABLE_FORUM_POST);
  2304. $t_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  2305. $t_users = Database::get_main_table(TABLE_MAIN_USER);
  2306. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2307. $t_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2308. $is_western_name_order = api_is_western_name_order();
  2309. if ($is_western_name_order) {
  2310. $orderby = 'ORDER BY user.firstname, user.lastname ';
  2311. } else {
  2312. $orderby = 'ORDER BY user.lastname, user.firstname';
  2313. }
  2314. $course_id = api_get_course_int_id();
  2315. $sql1 = "SELECT user_id FROM $t_qualify
  2316. WHERE c_id = $course_id AND thread_id = '".$thread_id."'";
  2317. $result1 = Database::query($sql1);
  2318. $cad = '';
  2319. while ($row = Database::fetch_array($result1)) {
  2320. $cad .= $row['user_id'].',';
  2321. }
  2322. if ($cad == '') {
  2323. $cad = '0';
  2324. } else {
  2325. $cad = substr($cad, 0, strlen($cad) - 1);
  2326. }
  2327. if (api_get_session_id()) {
  2328. $session_info = api_get_session_info(api_get_session_id());
  2329. $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'";
  2330. //not showing coaches
  2331. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, post.thread_id
  2332. FROM $t_posts post , $t_users user, $t_session_rel_user session_rel_user_rel_course
  2333. WHERE poster_id = user.id
  2334. AND user.id NOT IN (".$cad.")
  2335. AND user.id = session_rel_user_rel_course.user_id
  2336. AND session_rel_user_rel_course.status<>'2'
  2337. AND session_rel_user_rel_course.user_id NOT IN ($user_to_avoid)
  2338. AND post.thread_id = ".intval($thread_id)."
  2339. AND session_id = ".api_get_session_id()."
  2340. AND session_rel_user_rel_course.c_id = $course_id AND post.c_id = $course_id $orderby ";
  2341. } else {
  2342. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, post.thread_id
  2343. FROM $t_posts post, $t_users user,$t_course_user course_user
  2344. WHERE post.poster_id = user.id
  2345. AND user.id NOT IN (".$cad.")
  2346. AND user.id = course_user.user_id
  2347. AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  2348. AND post.thread_id = ".intval($thread_id)."
  2349. AND course_user.status not in('1')
  2350. AND course_user.c_id = $course_id AND post.c_id = $course_id $orderby";
  2351. }
  2352. $result = Database::query($sql);
  2353. return $result;
  2354. }
  2355. /**
  2356. * This function retrieves all the information of a given forum_id.
  2357. *
  2358. * @param $forum_id integer that indicates the forum
  2359. *
  2360. * @return array returns
  2361. *
  2362. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2363. *
  2364. * @version february 2006, dokeos 1.8
  2365. *
  2366. * @deprecated this functionality is now moved to get_forums($forum_id)
  2367. */
  2368. function get_forum_information($forum_id, $courseId = 0)
  2369. {
  2370. $table_forums = Database::get_course_table(TABLE_FORUM);
  2371. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2372. $courseId = empty($courseId) ? api_get_course_int_id() : intval($courseId);
  2373. $forum_id = intval($forum_id);
  2374. $sql = "SELECT *
  2375. FROM $table_forums forums
  2376. INNER JOIN $table_item_property item_properties
  2377. ON (forums.c_id = item_properties.c_id)
  2378. WHERE
  2379. item_properties.tool = '".TOOL_FORUM."' AND
  2380. item_properties.ref = '".$forum_id."' AND
  2381. forums.forum_id = '".$forum_id."' AND
  2382. forums.c_id = ".$courseId."
  2383. ";
  2384. $result = Database::query($sql);
  2385. $row = Database::fetch_array($result, 'ASSOC');
  2386. $row['approval_direct_post'] = 0;
  2387. // We can't anymore change this option, so it should always be activated.
  2388. return $row;
  2389. }
  2390. /**
  2391. * This function retrieves all the information of a given forumcategory id.
  2392. *
  2393. * @param $cat_id integer that indicates the forum
  2394. *
  2395. * @return array returns if there are category or bool returns if there aren't category
  2396. *
  2397. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2398. *
  2399. * @version february 2006, dokeos 1.8
  2400. */
  2401. function get_forumcategory_information($cat_id)
  2402. {
  2403. $table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
  2404. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2405. $course_id = api_get_course_int_id();
  2406. $sql = "SELECT *
  2407. FROM $table_categories forumcategories
  2408. INNER JOIN $table_item_property item_properties
  2409. ON (forumcategories.c_id = item_properties.c_id)
  2410. WHERE
  2411. forumcategories.c_id = $course_id AND
  2412. item_properties.c_id = $course_id AND
  2413. item_properties.tool='".TOOL_FORUM_CATEGORY."' AND
  2414. item_properties.ref='".Database::escape_string($cat_id)."' AND
  2415. forumcategories.cat_id='".Database::escape_string($cat_id)."'";
  2416. $result = Database::query($sql);
  2417. $row = Database::fetch_array($result);
  2418. return $row;
  2419. }
  2420. /**
  2421. * This function counts the number of forums inside a given category.
  2422. *
  2423. * @param int $cat_id the id of the forum category
  2424. *
  2425. * @todo an additional parameter that takes the visibility into account. For instance $countinvisible=0 would return the number
  2426. * of visible forums, $countinvisible=1 would return the number of visible and invisible forums
  2427. *
  2428. * @return int the number of forums inside the given category
  2429. *
  2430. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2431. *
  2432. * @version february 2006, dokeos 1.8
  2433. */
  2434. function count_number_of_forums_in_category($cat_id)
  2435. {
  2436. $table_forums = Database::get_course_table(TABLE_FORUM);
  2437. $course_id = api_get_course_int_id();
  2438. $cat_id = (int) $cat_id;
  2439. $sql = "SELECT count(*) AS number_of_forums
  2440. FROM $table_forums
  2441. WHERE c_id = $course_id AND forum_category = $cat_id";
  2442. $result = Database::query($sql);
  2443. $row = Database::fetch_array($result);
  2444. return $row['number_of_forums'];
  2445. }
  2446. /**
  2447. * This function update a thread.
  2448. *
  2449. * @param array $values - The form Values
  2450. */
  2451. function updateThread($values)
  2452. {
  2453. if (!api_is_allowed_to_edit()) {
  2454. return '';
  2455. }
  2456. $logInfo = [
  2457. 'tool' => TOOL_FORUM,
  2458. 'tool_id' => $values['forum_id'],
  2459. 'tool_id_detail' => $values['thread_id'],
  2460. 'action' => 'edit-thread',
  2461. 'action_details' => 'thread',
  2462. 'info' => $values['thread_title'],
  2463. ];
  2464. Event::registerLog($logInfo);
  2465. $threadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  2466. $courseId = api_get_course_int_id();
  2467. $courseCode = api_get_course_id();
  2468. $sessionId = api_get_session_id();
  2469. // Simple update + set gradebook values to null
  2470. $params = [
  2471. 'thread_title' => $values['thread_title'],
  2472. 'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : 0,
  2473. ];
  2474. $where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
  2475. Database::update($threadTable, $params, $where);
  2476. $id = $values['thread_id'];
  2477. $linkInfo = GradebookUtils::isResourceInCourseGradebook(
  2478. $courseCode,
  2479. LINK_FORUM_THREAD,
  2480. $id,
  2481. $sessionId
  2482. );
  2483. $linkId = $linkInfo['id'];
  2484. $em = Database::getManager();
  2485. $gradebookLink = null;
  2486. if (!empty($linkId)) {
  2487. $gradebookLink = $em->getRepository('ChamiloCoreBundle:GradebookLink')->find($linkId);
  2488. }
  2489. // values 1 or 0
  2490. $check = isset($values['thread_qualify_gradebook']) ? $values['thread_qualify_gradebook'] : false;
  2491. if ($check) {
  2492. $title = Security::remove_XSS(stripslashes($values['calification_notebook_title']));
  2493. $value = isset($values['numeric_calification']) ? intval($values['numeric_calification']) : 0;
  2494. $weight = isset($values['weight_calification']) ? floatval($values['weight_calification']) : 0;
  2495. $description = '';
  2496. // Update title
  2497. $params = [
  2498. 'thread_title_qualify' => $values['calification_notebook_title'],
  2499. 'thread_qualify_max' => api_float_val($values['numeric_calification']),
  2500. 'thread_weight' => api_float_val($values['weight_calification']),
  2501. 'thread_peer_qualify' => $values['thread_peer_qualify'],
  2502. ];
  2503. $where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
  2504. Database::update($threadTable, $params, $where);
  2505. if (!$linkInfo) {
  2506. GradebookUtils::add_resource_to_course_gradebook(
  2507. $values['category_id'],
  2508. $courseCode,
  2509. LINK_FORUM_THREAD,
  2510. $id,
  2511. $title,
  2512. $weight,
  2513. $value,
  2514. $description,
  2515. 1,
  2516. $sessionId
  2517. );
  2518. } else {
  2519. if ($gradebookLink) {
  2520. $gradebookLink->setWeight($weight);
  2521. $em->persist($gradebookLink);
  2522. $em->flush();
  2523. }
  2524. }
  2525. } else {
  2526. $params = [
  2527. 'thread_title_qualify' => '',
  2528. 'thread_qualify_max' => '',
  2529. 'thread_weight' => '',
  2530. 'thread_peer_qualify' => '',
  2531. ];
  2532. $where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
  2533. Database::update($threadTable, $params, $where);
  2534. if (!empty($linkInfo)) {
  2535. if ($gradebookLink) {
  2536. $em->remove($gradebookLink);
  2537. $em->flush();
  2538. }
  2539. }
  2540. }
  2541. $message = get_lang('EditPostStored').'<br />';
  2542. Display::addFlash(Display::return_message($message, 'confirmation', false));
  2543. }
  2544. /**
  2545. * This function stores a new thread. This is done through an entry in the forum_thread table AND
  2546. * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet)).
  2547. *
  2548. * @param array $current_forum
  2549. * @param array $values
  2550. * @param array $courseInfo
  2551. * @param bool $showMessage
  2552. * @param int $userId Optional. The user ID
  2553. * @param int $sessionId
  2554. *
  2555. * @return CForumThread
  2556. *
  2557. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2558. *
  2559. * @version february 2006, dokeos 1.8
  2560. */
  2561. function store_thread(
  2562. $current_forum,
  2563. $values,
  2564. $courseInfo = [],
  2565. $showMessage = true,
  2566. $userId = 0,
  2567. $sessionId = 0
  2568. ) {
  2569. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  2570. $userId = $userId ?: api_get_user_id();
  2571. $course_id = $courseInfo['real_id'];
  2572. $courseCode = $courseInfo['code'];
  2573. $groupId = api_get_group_id();
  2574. $groupInfo = GroupManager::get_group_properties($groupId);
  2575. $sessionId = $sessionId ?: api_get_session_id();
  2576. $em = Database::getManager();
  2577. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  2578. $upload_ok = 1;
  2579. $has_attachment = false;
  2580. if (!empty($_FILES['user_upload']['name'])) {
  2581. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  2582. $has_attachment = true;
  2583. }
  2584. if (!$upload_ok) {
  2585. if ($showMessage) {
  2586. Display::addFlash(
  2587. Display::return_message(
  2588. get_lang('UplNoFileUploaded'),
  2589. 'error',
  2590. false
  2591. )
  2592. );
  2593. }
  2594. return null;
  2595. }
  2596. $post_date = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  2597. $visible = 1;
  2598. if ($current_forum['approval_direct_post'] == '1' && !api_is_allowed_to_edit(null, true)) {
  2599. $visible = 0; // The post has not been approved yet.
  2600. }
  2601. $clean_post_title = $values['post_title'];
  2602. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table.
  2603. $lastThread = new CForumThread();
  2604. $lastThread
  2605. ->setCId($course_id)
  2606. ->setThreadTitle($clean_post_title)
  2607. ->setForumId($values['forum_id'])
  2608. ->setThreadPosterId($userId)
  2609. ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
  2610. ->setThreadDate($post_date)
  2611. ->setThreadSticky(isset($values['thread_sticky']) ? $values['thread_sticky'] : 0)
  2612. ->setThreadTitleQualify(
  2613. isset($values['calification_notebook_title']) ? $values['calification_notebook_title'] : null
  2614. )
  2615. ->setThreadQualifyMax(isset($values['numeric_calification']) ? (int) $values['numeric_calification'] : 0)
  2616. ->setThreadWeight(isset($values['weight_calification']) ? (int) $values['weight_calification'] : 0)
  2617. ->setThreadPeerQualify(isset($values['thread_peer_qualify']) ? (bool) $values['thread_peer_qualify'] : false)
  2618. ->setSessionId($sessionId)
  2619. ->setLpItemId(isset($values['lp_item_id']) ? (int) $values['lp_item_id'] : 0)
  2620. ->setThreadId(0)
  2621. ->setLocked(0)
  2622. ;
  2623. $em->persist($lastThread);
  2624. $em->flush();
  2625. // Add option gradebook qualify.
  2626. if (isset($values['thread_qualify_gradebook']) &&
  2627. 1 == $values['thread_qualify_gradebook']
  2628. ) {
  2629. // Add function gradebook.
  2630. $resourcename = stripslashes($values['calification_notebook_title']);
  2631. GradebookUtils::add_resource_to_course_gradebook(
  2632. $values['category_id'],
  2633. $courseCode,
  2634. 5,
  2635. $lastThread->getIid(),
  2636. $resourcename,
  2637. $values['weight_calification'],
  2638. $values['numeric_calification'],
  2639. '',
  2640. 0,
  2641. $sessionId
  2642. );
  2643. }
  2644. if ($lastThread->getIid()) {
  2645. $lastThread->setThreadId($lastThread->getIid());
  2646. $em->merge($lastThread);
  2647. $em->flush();
  2648. api_item_property_update(
  2649. $courseInfo,
  2650. TOOL_FORUM_THREAD,
  2651. $lastThread->getIid(),
  2652. 'ForumThreadAdded',
  2653. $userId,
  2654. $groupInfo,
  2655. null,
  2656. null,
  2657. null,
  2658. $sessionId
  2659. );
  2660. // If the forum properties tell that the posts have to be approved
  2661. // we have to put the whole thread invisible,
  2662. // because otherwise the students will see the thread and not the post
  2663. // in the thread.
  2664. // We also have to change $visible because the post itself has to be
  2665. // visible in this case (otherwise the teacher would have
  2666. // to make the thread visible AND the post.
  2667. // Default behaviour
  2668. api_set_default_visibility(
  2669. $lastThread->getIid(),
  2670. TOOL_FORUM_THREAD,
  2671. $groupId,
  2672. $courseInfo,
  2673. $sessionId,
  2674. $userId
  2675. );
  2676. if ($visible == 0) {
  2677. api_item_property_update(
  2678. $courseInfo,
  2679. TOOL_FORUM_THREAD,
  2680. $lastThread->getIid(),
  2681. 'invisible',
  2682. $userId,
  2683. $groupInfo
  2684. );
  2685. $visible = 1;
  2686. }
  2687. $logInfo = [
  2688. 'tool' => TOOL_FORUM,
  2689. 'tool_id' => $values['forum_id'],
  2690. 'tool_id_detail' => $lastThread->getIid(),
  2691. 'action' => 'new-thread',
  2692. 'action_details' => '',
  2693. 'info' => $clean_post_title,
  2694. ];
  2695. Event::registerLog($logInfo);
  2696. }
  2697. // We now store the content in the table_post table.
  2698. $lastPost = new CForumPost();
  2699. $lastPost
  2700. ->setCId($course_id)
  2701. ->setPostTitle($clean_post_title)
  2702. ->setPostText($values['post_text'])
  2703. ->setThreadId($lastThread->getIid())
  2704. ->setForumId($values['forum_id'])
  2705. ->setPosterId($userId)
  2706. ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
  2707. ->setPostDate($post_date)
  2708. ->setPostNotification(isset($values['post_notification']) ? (int) $values['post_notification'] : null)
  2709. ->setPostParentId(null)
  2710. ->setVisible($visible)
  2711. ->setPostId(0)
  2712. ->setStatus(CForumPost::STATUS_VALIDATED);
  2713. if ($current_forum['moderated']) {
  2714. $lastPost->setStatus(
  2715. api_is_course_admin() ? CForumPost::STATUS_VALIDATED : CForumPost::STATUS_WAITING_MODERATION
  2716. );
  2717. }
  2718. $em->persist($lastPost);
  2719. $em->flush();
  2720. $lastPostId = $lastPost->getIid();
  2721. $lastThread->setThreadLastPost($lastPostId);
  2722. $em->merge($lastThread);
  2723. $em->flush();
  2724. $logInfo = [
  2725. 'tool' => TOOL_FORUM,
  2726. 'tool_id' => $values['forum_id'],
  2727. 'tool_id_detail' => $lastThread->getIid(),
  2728. 'action' => 'new-post',
  2729. 'info' => $clean_post_title,
  2730. ];
  2731. Event::registerLog($logInfo);
  2732. if ($lastPostId) {
  2733. $lastPost->setPostId($lastPostId);
  2734. $em->merge($lastPost);
  2735. $em->flush();
  2736. }
  2737. // Update attached files
  2738. if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
  2739. foreach ($_POST['file_ids'] as $key => $id) {
  2740. editAttachedFile(
  2741. [
  2742. 'comment' => $_POST['file_comments'][$key],
  2743. 'post_id' => $lastPostId,
  2744. ],
  2745. $id
  2746. );
  2747. }
  2748. }
  2749. // Now we have to update the thread table to fill the thread_last_post
  2750. // field (so that we know when the thread has been updated for the last time).
  2751. $sql = "UPDATE $table_threads
  2752. SET thread_last_post = '".Database::escape_string($lastPostId)."'
  2753. WHERE
  2754. c_id = $course_id AND
  2755. thread_id='".Database::escape_string($lastThread->getIid())."'";
  2756. $result = Database::query($sql);
  2757. $message = get_lang('NewThreadStored');
  2758. // Overwrite default message.
  2759. if ($current_forum['moderated'] &&
  2760. !api_is_allowed_to_edit(null, true)
  2761. ) {
  2762. $message = get_lang('MessageHasToBeApproved');
  2763. }
  2764. // Storing the attachments if any.
  2765. if ($has_attachment) {
  2766. // Try to add an extension to the file if it hasn't one.
  2767. $new_file_name = add_ext_on_mime(
  2768. stripslashes($_FILES['user_upload']['name']),
  2769. $_FILES['user_upload']['type']
  2770. );
  2771. if (!filter_extension($new_file_name)) {
  2772. if ($showMessage) {
  2773. Display::addFlash(Display::return_message(
  2774. get_lang('UplUnableToSaveFileFilteredExtension'),
  2775. 'error'
  2776. ));
  2777. }
  2778. } else {
  2779. if ($result) {
  2780. add_forum_attachment_file(
  2781. isset($values['file_comment']) ? $values['file_comment'] : null,
  2782. $lastPostId
  2783. );
  2784. }
  2785. }
  2786. } else {
  2787. $message .= '<br />';
  2788. }
  2789. if ($current_forum['approval_direct_post'] == '1' &&
  2790. !api_is_allowed_to_edit(null, true)
  2791. ) {
  2792. $message .= get_lang('MessageHasToBeApproved').'<br />';
  2793. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'">'.
  2794. get_lang('Forum').'</a><br />';
  2795. } else {
  2796. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'">'.
  2797. get_lang('Forum').'</a><br />';
  2798. $message .= get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&thread='.$lastThread->getIid().'">'.
  2799. get_lang('Message').'</a>';
  2800. }
  2801. $reply_info['new_post_id'] = $lastPostId;
  2802. $my_post_notification = isset($values['post_notification']) ? $values['post_notification'] : null;
  2803. if ($my_post_notification == 1) {
  2804. set_notification('thread', $lastThread->getIid(), true);
  2805. }
  2806. send_notification_mails(
  2807. $current_forum['forum_id'],
  2808. $lastThread->getIid(),
  2809. $reply_info,
  2810. $courseInfo['code']
  2811. );
  2812. Session::erase('formelements');
  2813. Session::erase('origin');
  2814. Session::erase('breadcrumbs');
  2815. Session::erase('addedresource');
  2816. Session::erase('addedresourceid');
  2817. if ($showMessage) {
  2818. Display::addFlash(Display::return_message($message, 'success', false));
  2819. }
  2820. return $lastThread;
  2821. }
  2822. /**
  2823. * This function displays the form that is used to add a post. This can be a new thread or a reply.
  2824. *
  2825. * @param array $current_forum
  2826. * @param string $action is the parameter that determines if we are
  2827. * 1. newthread: adding a new thread (both empty) => No I-frame
  2828. * 2. replythread: Replying to a thread ($action = replythread) => I-frame with the complete thread (if enabled)
  2829. * 3. replymessage: Replying to a message ($action =replymessage) => I-frame with the complete thread (if enabled)
  2830. * (I first thought to put and I-frame with the message only)
  2831. * 4. quote: Quoting a message ($action= quotemessage) => I-frame with the complete thread (if enabled).
  2832. * The message will be in the reply. (I first thought not to put an I-frame here)
  2833. * @param array $form_values
  2834. * @param bool $showPreview
  2835. *
  2836. * @return FormValidator
  2837. *
  2838. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2839. *
  2840. * @version february 2006, dokeos 1.8
  2841. */
  2842. function show_add_post_form($current_forum, $action, $form_values = '', $showPreview = true)
  2843. {
  2844. $_user = api_get_user_info();
  2845. $action = isset($action) ? Security::remove_XSS($action) : '';
  2846. $myThread = isset($_GET['thread']) ? (int) $_GET['thread'] : '';
  2847. $forumId = isset($_GET['forum']) ? (int) $_GET['forum'] : '';
  2848. $my_post = isset($_GET['post']) ? (int) $_GET['post'] : '';
  2849. $giveRevision = isset($_GET['give_revision']) && $_GET['give_revision'] == 1;
  2850. $url = api_get_self().'?'.http_build_query(
  2851. [
  2852. 'action' => $action,
  2853. 'forum' => $forumId,
  2854. 'thread' => $myThread,
  2855. 'post' => $my_post,
  2856. ]
  2857. ).'&'.api_get_cidreq();
  2858. $form = new FormValidator(
  2859. 'thread',
  2860. 'post',
  2861. $url
  2862. );
  2863. $form->setConstants(['forum' => '5']);
  2864. // Setting the form elements.
  2865. $form->addElement('hidden', 'forum_id', $forumId);
  2866. $form->addElement('hidden', 'thread_id', $myThread);
  2867. $form->addElement('hidden', 'action', $action);
  2868. // If anonymous posts are allowed we also display a form to allow the user to put his name or username in.
  2869. if ($current_forum['allow_anonymous'] == 1 && !isset($_user['user_id'])) {
  2870. $form->addElement('text', 'poster_name', get_lang('Name'));
  2871. $form->applyFilter('poster_name', 'html_filter');
  2872. }
  2873. $form->addElement('text', 'post_title', get_lang('Title'));
  2874. $form->addHtmlEditor(
  2875. 'post_text',
  2876. get_lang('Text'),
  2877. true,
  2878. false,
  2879. api_is_allowed_to_edit(null, true) ? [
  2880. 'ToolbarSet' => 'Forum',
  2881. 'Width' => '100%',
  2882. 'Height' => '300',
  2883. ] : [
  2884. 'ToolbarSet' => 'ForumStudent',
  2885. 'Width' => '100%',
  2886. 'Height' => '300',
  2887. 'UserStatus' => 'student',
  2888. ]
  2889. );
  2890. $form->addRule('post_text', get_lang('ThisFieldIsRequired'), 'required');
  2891. if (in_array($action, ['newthread', 'replythread', 'replymessage', 'quote'])) {
  2892. $extraFields = new ExtraField('forum_post');
  2893. $extraFields->addElements(
  2894. $form,
  2895. null,
  2896. [], //exclude
  2897. false, // filter
  2898. false, // tag as select
  2899. ['ask_for_revision'], //show only fields
  2900. [], // order fields
  2901. [] // extra data);
  2902. );
  2903. }
  2904. $iframe = null;
  2905. if ($showPreview) {
  2906. $myThread = Security::remove_XSS($myThread);
  2907. if ($action != 'newthread' && !empty($myThread)) {
  2908. $iframe = "<iframe style=\"border: 1px solid black\" src=\"iframe_thread.php?".api_get_cidreq(
  2909. )."&forum=".$forumId."&thread=".$myThread."#".$my_post."\" width=\"100%\"></iframe>";
  2910. }
  2911. if (!empty($iframe)) {
  2912. $form->addElement('label', get_lang('Thread'), $iframe);
  2913. }
  2914. }
  2915. if (Gradebook::is_active() &&
  2916. (api_is_course_admin() || api_is_session_general_coach() || api_is_course_tutor()) && !($myThread)
  2917. ) {
  2918. $form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
  2919. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  2920. // Thread qualify
  2921. if (Gradebook::is_active()) {
  2922. //Loading gradebook select
  2923. GradebookUtils::load_gradebook_select_in_tool($form);
  2924. $form->addElement(
  2925. 'checkbox',
  2926. 'thread_qualify_gradebook',
  2927. '',
  2928. get_lang('QualifyThreadGradebook'),
  2929. 'onclick="javascript:if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"'
  2930. );
  2931. } else {
  2932. $form->addElement('hidden', 'thread_qualify_gradebook', false);
  2933. }
  2934. $form->addElement('html', '<div id="options_field" style="display:none">');
  2935. $form->addElement('text', 'numeric_calification', get_lang('QualificationNumeric'));
  2936. $form->applyFilter('numeric_calification', 'html_filter');
  2937. $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'));
  2938. $form->applyFilter('calification_notebook_title', 'html_filter');
  2939. $form->addElement(
  2940. 'text',
  2941. 'weight_calification',
  2942. get_lang('QualifyWeight'),
  2943. ['value' => '0.00', 'onfocus' => "javascript: this.select();"]
  2944. );
  2945. $form->applyFilter('weight_calification', 'html_filter');
  2946. $group = [];
  2947. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('Yes'), 1);
  2948. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('No'), 0);
  2949. $form->addGroup(
  2950. $group,
  2951. '',
  2952. [
  2953. get_lang('ForumThreadPeerScoring'),
  2954. get_lang('ForumThreadPeerScoringComment'),
  2955. ]
  2956. );
  2957. $form->addElement('html', '</div>');
  2958. $form->addElement('html', '</div>');
  2959. }
  2960. if ($action === 'newthread') {
  2961. Skill::addSkillsToForm($form, ITEM_TYPE_FORUM_THREAD, 0);
  2962. }
  2963. if (api_is_allowed_to_edit(null, true) && $action == 'newthread') {
  2964. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  2965. }
  2966. if (in_array($action, ['quote', 'replymessage'])) {
  2967. $form->addFile('user_upload[]', get_lang('Attachment'));
  2968. $form->addButton(
  2969. 'add_attachment',
  2970. get_lang('AddAttachment'),
  2971. 'paperclip',
  2972. 'default',
  2973. 'default',
  2974. null,
  2975. ['id' => 'reply-add-attachment']
  2976. );
  2977. } else {
  2978. $form->addFile('user_upload', get_lang('Attachment'));
  2979. }
  2980. if ($giveRevision) {
  2981. $hide = api_get_configuration_value('hide_forum_post_revision_language');
  2982. $form->addHidden('give_revision', 1);
  2983. if ($hide === false) {
  2984. $extraField = new ExtraField('forum_post');
  2985. $extraField->addElements(
  2986. $form,
  2987. null,
  2988. [], //exclude
  2989. false, // filter
  2990. false, // tag as select
  2991. ['revision_language'], //show only fields
  2992. [], // order fields
  2993. [] // extra data
  2994. );
  2995. } else {
  2996. $form->addHidden('extra_revision_language', 1);
  2997. }
  2998. }
  2999. // Setting the class and text of the form title and submit button.
  3000. if ($action == 'quote') {
  3001. $form->addButtonCreate(get_lang('QuoteMessage'), 'SubmitPost');
  3002. } elseif ($action == 'replythread') {
  3003. $form->addButtonCreate(get_lang('ReplyToThread'), 'SubmitPost');
  3004. } elseif ($action == 'replymessage') {
  3005. $form->addButtonCreate(get_lang('ReplyToMessage'), 'SubmitPost');
  3006. } else {
  3007. $form->addButtonCreate(get_lang('CreateThread'), 'SubmitPost');
  3008. }
  3009. $defaults['thread_peer_qualify'] = 0;
  3010. if (!empty($form_values)) {
  3011. $defaults['post_title'] = prepare4display($form_values['post_title']);
  3012. $defaults['post_text'] = prepare4display($form_values['post_text']);
  3013. $defaults['post_notification'] = (int) $form_values['post_notification'];
  3014. $defaults['thread_sticky'] = (int) $form_values['thread_sticky'];
  3015. $defaults['thread_peer_qualify'] = (int) $form_values['thread_peer_qualify'];
  3016. }
  3017. // If we are quoting a message we have to retrieve the information of the post we are quoting so that
  3018. // we can add this as default to the textarea.
  3019. // We also need to put the parent_id of the post in a hidden form when
  3020. if (($action == 'quote' || $action == 'replymessage' || $giveRevision) && !empty($my_post)) {
  3021. // we are quoting or replying to a message (<> reply to a thread !!!)
  3022. $form->addHidden('post_parent_id', $my_post);
  3023. // If we are replying or are quoting then we display a default title.
  3024. $values = get_post_information($my_post);
  3025. $posterInfo = api_get_user_info($values['poster_id']);
  3026. $posterName = '';
  3027. if ($posterInfo) {
  3028. $posterName = $posterInfo['complete_name'];
  3029. }
  3030. $defaults['post_title'] = get_lang('ReplyShort').api_html_entity_decode($values['post_title'], ENT_QUOTES);
  3031. // When we are quoting a message then we have to put that message into the wysiwyg editor.
  3032. // Note: The style has to be hardcoded here because using class="quote" didn't work.
  3033. if ($action == 'quote') {
  3034. $defaults['post_text'] = '<div>&nbsp;</div>
  3035. <div style="margin: 5px;">
  3036. <div style="font-size: 90%; font-style: italic;">'.
  3037. get_lang('Quoting').' '.$posterName.':</div>
  3038. <div style="color: #006600; font-size: 90%; font-style: italic; background-color: #FAFAFA; border: #D1D7DC 1px solid; padding: 3px;">'.
  3039. prepare4display($values['post_text']).'
  3040. </div>
  3041. </div>
  3042. <div>&nbsp;</div>
  3043. <div>&nbsp;</div>
  3044. ';
  3045. }
  3046. if ($giveRevision) {
  3047. $defaults['post_text'] = prepare4display($values['post_text']);
  3048. }
  3049. }
  3050. $form->setDefaults(isset($defaults) ? $defaults : []);
  3051. // The course admin can make a thread sticky (=appears with special icon and always on top).
  3052. $form->addRule('post_title', get_lang('ThisFieldIsRequired'), 'required');
  3053. if ($current_forum['allow_anonymous'] == 1 && !isset($_user['user_id'])) {
  3054. $form->addRule(
  3055. 'poster_name',
  3056. get_lang('ThisFieldIsRequired'),
  3057. 'required'
  3058. );
  3059. }
  3060. // Validation or display
  3061. if ($form->validate()) {
  3062. $check = Security::check_token('post');
  3063. if ($check) {
  3064. $values = $form->getSubmitValues();
  3065. if (isset($values['thread_qualify_gradebook']) &&
  3066. $values['thread_qualify_gradebook'] == '1' &&
  3067. empty($values['weight_calification'])
  3068. ) {
  3069. Display::addFlash(
  3070. Display::return_message(
  3071. get_lang('YouMustAssignWeightOfQualification').'&nbsp;<a href="javascript:window.history.go(-1);">'.get_lang('Back').'</a>',
  3072. 'error',
  3073. false
  3074. )
  3075. );
  3076. return false;
  3077. }
  3078. $postId = 0;
  3079. $threadId = 0;
  3080. switch ($action) {
  3081. case 'newthread':
  3082. $myThread = store_thread($current_forum, $values);
  3083. if ($myThread) {
  3084. $threadId = $myThread->getIid();
  3085. Skill::saveSkills($form, ITEM_TYPE_FORUM_THREAD, $threadId);
  3086. $postId = $myThread->getThreadLastPost();
  3087. }
  3088. break;
  3089. case 'quote':
  3090. case 'replythread':
  3091. case 'replymessage':
  3092. $postId = store_reply($current_forum, $values);
  3093. break;
  3094. }
  3095. if ($postId) {
  3096. $postInfo = get_post_information($postId);
  3097. if ($postInfo) {
  3098. $threadId = $postInfo['thread_id'];
  3099. }
  3100. if (isset($values['give_revision']) && $values['give_revision'] == 1) {
  3101. $extraFieldValues = new ExtraFieldValue('forum_post');
  3102. $revisionLanguage = isset($values['extra_revision_language']) ? $values['extra_revision_language'] : '';
  3103. $params = [
  3104. 'item_id' => $postId,
  3105. 'extra_revision_language' => $revisionLanguage,
  3106. ];
  3107. $extraFieldValues->saveFieldValues(
  3108. $params,
  3109. false,
  3110. false,
  3111. ['revision_language']
  3112. );
  3113. }
  3114. if (in_array($action, ['newthread', 'replythread', 'replymessage', 'quote'])) {
  3115. $extraFieldValues = new ExtraFieldValue('forum_post');
  3116. $params = [
  3117. 'item_id' => $postId,
  3118. 'extra_ask_for_revision' => isset($values['extra_ask_for_revision']) ? $values['extra_ask_for_revision'] : '',
  3119. ];
  3120. $extraFieldValues->saveFieldValues(
  3121. $params,
  3122. false,
  3123. false,
  3124. ['ask_for_revision']
  3125. );
  3126. }
  3127. }
  3128. $url = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&'.http_build_query(
  3129. [
  3130. 'forum' => $forumId,
  3131. 'thread' => $threadId,
  3132. ]
  3133. );
  3134. Security::clear_token();
  3135. header('Location: '.$url);
  3136. exit;
  3137. }
  3138. } else {
  3139. $token = Security::get_token();
  3140. $form->addElement('hidden', 'sec_token');
  3141. $form->setConstants(['sec_token' => $token]);
  3142. // Delete from $_SESSION forum attachment from other posts
  3143. // and keep only attachments for new post
  3144. clearAttachedFiles(FORUM_NEW_POST);
  3145. // Get forum attachment ajax table to add it to form
  3146. $attachmentAjaxTable = getAttachmentsAjaxTable(0, $current_forum['forum_id']);
  3147. $ajaxHtml = $attachmentAjaxTable;
  3148. $form->addElement('html', $ajaxHtml);
  3149. return $form;
  3150. }
  3151. }
  3152. /**
  3153. * @param array $threadInfo
  3154. * @param int $user_id
  3155. * @param int $thread_id
  3156. * @param int $thread_qualify
  3157. * @param int $qualify_time
  3158. * @param int $session_id
  3159. *
  3160. * @return array optional
  3161. *
  3162. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3163. *
  3164. * @version October 2008, dokeos 1.8.6
  3165. */
  3166. function saveThreadScore(
  3167. $threadInfo,
  3168. $user_id,
  3169. $thread_id,
  3170. $thread_qualify = 0,
  3171. $qualify_time,
  3172. $session_id = 0
  3173. ) {
  3174. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3175. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3176. $course_id = api_get_course_int_id();
  3177. $session_id = intval($session_id);
  3178. $currentUserId = api_get_user_id();
  3179. if ($user_id == strval(intval($user_id)) &&
  3180. $thread_id == strval(intval($thread_id)) &&
  3181. $thread_qualify == strval(floatval($thread_qualify))
  3182. ) {
  3183. // Testing
  3184. $sql = "SELECT thread_qualify_max FROM $table_threads
  3185. WHERE c_id = $course_id AND thread_id=".$thread_id;
  3186. $res_string = Database::query($sql);
  3187. $row_string = Database::fetch_array($res_string);
  3188. if ($thread_qualify <= $row_string[0]) {
  3189. if ($threadInfo['thread_peer_qualify'] == 0) {
  3190. $sql = "SELECT COUNT(*) FROM $table_threads_qualify
  3191. WHERE
  3192. c_id = $course_id AND
  3193. user_id = $user_id AND
  3194. thread_id = ".$thread_id;
  3195. } else {
  3196. $sql = "SELECT COUNT(*) FROM $table_threads_qualify
  3197. WHERE
  3198. c_id = $course_id AND
  3199. user_id = $user_id AND
  3200. qualify_user_id = $currentUserId AND
  3201. thread_id = ".$thread_id;
  3202. }
  3203. $result = Database::query($sql);
  3204. $row = Database::fetch_array($result);
  3205. if ($row[0] == 0) {
  3206. $sql = "INSERT INTO $table_threads_qualify (c_id, user_id, thread_id,qualify,qualify_user_id,qualify_time,session_id)
  3207. VALUES (".$course_id.", '".$user_id."','".$thread_id."',".(float) $thread_qualify.", '".$currentUserId."','".$qualify_time."','".$session_id."')";
  3208. Database::query($sql);
  3209. $insertId = Database::insert_id();
  3210. if ($insertId) {
  3211. $sql = "UPDATE $table_threads_qualify SET id = iid
  3212. WHERE iid = $insertId";
  3213. Database::query($sql);
  3214. }
  3215. return 'insert';
  3216. } else {
  3217. saveThreadScoreHistory(
  3218. '1',
  3219. $course_id,
  3220. $user_id,
  3221. $thread_id
  3222. );
  3223. // Update
  3224. $sql = "UPDATE $table_threads_qualify
  3225. SET
  3226. qualify = '".$thread_qualify."',
  3227. qualify_time = '".$qualify_time."'
  3228. WHERE
  3229. c_id = $course_id AND
  3230. user_id=".$user_id." AND
  3231. thread_id=".$thread_id." AND
  3232. qualify_user_id = $currentUserId
  3233. ";
  3234. Database::query($sql);
  3235. return 'update';
  3236. }
  3237. } else {
  3238. return null;
  3239. }
  3240. }
  3241. }
  3242. /**
  3243. * This function shows qualify.
  3244. *
  3245. * @param string $option contains the information of option to run
  3246. * @param int $user_id contains the information the current user id
  3247. * @param int $thread_id contains the information the current thread id
  3248. *
  3249. * @return int qualify
  3250. * <code> $option=1 obtained the qualification of the current thread</code>
  3251. *
  3252. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3253. *
  3254. * @version October 2008, dokeos 1.8.6
  3255. */
  3256. function showQualify($option, $user_id, $thread_id)
  3257. {
  3258. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3259. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3260. $course_id = api_get_course_int_id();
  3261. $user_id = (int) $user_id;
  3262. $thread_id = (int) $thread_id;
  3263. if (empty($user_id) || empty($thread_id)) {
  3264. return false;
  3265. }
  3266. $sql = '';
  3267. switch ($option) {
  3268. case 1:
  3269. $sql = "SELECT qualify FROM $table_threads_qualify
  3270. WHERE
  3271. c_id = $course_id AND
  3272. user_id=".$user_id." AND
  3273. thread_id=".$thread_id;
  3274. break;
  3275. case 2:
  3276. $sql = "SELECT thread_qualify_max FROM $table_threads
  3277. WHERE c_id = $course_id AND thread_id=".$thread_id;
  3278. break;
  3279. }
  3280. if (!empty($sql)) {
  3281. $rs = Database::query($sql);
  3282. $row = Database::fetch_array($rs);
  3283. return $row[0];
  3284. }
  3285. return [];
  3286. }
  3287. /**
  3288. * This function gets qualify historical.
  3289. *
  3290. * @param int $user_id contains the information the current user id
  3291. * @param int $thread_id contains the information the current thread id
  3292. * @param bool $opt contains the information of option to run
  3293. *
  3294. * @return array
  3295. *
  3296. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  3297. * @author Isaac Flores <isaac.flores@dokeos.com>,
  3298. *
  3299. * @version October 2008, dokeos 1.8.6
  3300. */
  3301. function getThreadScoreHistory($user_id, $thread_id, $opt)
  3302. {
  3303. $user_id = (int) $user_id;
  3304. $thread_id = (int) $thread_id;
  3305. $table_threads_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG);
  3306. $course_id = api_get_course_int_id();
  3307. if ($opt == 'false') {
  3308. $sql = "SELECT * FROM $table_threads_qualify_log
  3309. WHERE
  3310. c_id = $course_id AND
  3311. thread_id='".$thread_id."' AND
  3312. user_id='".$user_id."'
  3313. ORDER BY qualify_time";
  3314. } else {
  3315. $sql = "SELECT * FROM $table_threads_qualify_log
  3316. WHERE
  3317. c_id = $course_id AND
  3318. thread_id='".$thread_id."' AND
  3319. user_id='".$user_id."'
  3320. ORDER BY qualify_time DESC";
  3321. }
  3322. $rs = Database::query($sql);
  3323. $log = [];
  3324. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3325. $log[] = $row;
  3326. }
  3327. return $log;
  3328. }
  3329. /**
  3330. * This function stores qualify historical.
  3331. *
  3332. * @param bool contains the information of option to run
  3333. * @param string contains the information the current course id
  3334. * @param int contains the information the current forum id
  3335. * @param int contains the information the current user id
  3336. * @param int contains the information the current thread id
  3337. * @param int contains the information the current qualify
  3338. * @param string $option
  3339. * @param int $course_id
  3340. * @param int $user_id
  3341. * @param int $thread_id
  3342. *
  3343. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3344. *
  3345. * @version October 2008, dokeos 1.8.6
  3346. */
  3347. function saveThreadScoreHistory(
  3348. $option,
  3349. $course_id,
  3350. $user_id,
  3351. $thread_id
  3352. ) {
  3353. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3354. $table_threads_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG);
  3355. $course_id = intval($course_id);
  3356. $qualify_user_id = api_get_user_id();
  3357. if ($user_id == strval(intval($user_id)) &&
  3358. $thread_id == strval(intval($thread_id)) && $option == 1
  3359. ) {
  3360. // Extract information of thread_qualify.
  3361. $sql = "SELECT qualify, qualify_time
  3362. FROM $table_threads_qualify
  3363. WHERE
  3364. c_id = $course_id AND
  3365. user_id = ".$user_id." AND
  3366. thread_id = ".$thread_id." AND
  3367. qualify_user_id = $qualify_user_id
  3368. ";
  3369. $rs = Database::query($sql);
  3370. $row = Database::fetch_array($rs);
  3371. // Insert thread_historical.
  3372. $sql = "INSERT INTO $table_threads_qualify_log (c_id, user_id, thread_id, qualify, qualify_user_id,qualify_time,session_id)
  3373. VALUES(".$course_id.", '".$user_id."','".$thread_id."',".(float) $row[0].", '".$qualify_user_id."','".$row[1]."','')";
  3374. Database::query($sql);
  3375. $insertId = Database::insert_id();
  3376. if ($insertId) {
  3377. $sql = "UPDATE $table_threads_qualify_log SET id = iid
  3378. WHERE iid = $insertId";
  3379. Database::query($sql);
  3380. }
  3381. }
  3382. }
  3383. /**
  3384. * This function shows current thread qualify .
  3385. *
  3386. * @param int $threadId
  3387. * @param int $sessionId
  3388. * @param int $userId
  3389. *
  3390. * @return array or null if is empty
  3391. *
  3392. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3393. *
  3394. * @version December 2008, dokeos 1.8.6
  3395. */
  3396. function current_qualify_of_thread($threadId, $sessionId, $userId)
  3397. {
  3398. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3399. $course_id = api_get_course_int_id();
  3400. $currentUserId = api_get_user_id();
  3401. $sessionId = intval($sessionId);
  3402. $threadId = intval($threadId);
  3403. $sql = "SELECT qualify FROM $table_threads_qualify
  3404. WHERE
  3405. c_id = $course_id AND
  3406. thread_id = $threadId AND
  3407. session_id = $sessionId AND
  3408. qualify_user_id = $currentUserId AND
  3409. user_id = $userId
  3410. ";
  3411. $res = Database::query($sql);
  3412. $row = Database::fetch_array($res, 'ASSOC');
  3413. return $row['qualify'];
  3414. }
  3415. /**
  3416. * This function stores a reply in the forum_post table.
  3417. * It also updates the forum_threads table (thread_replies +1 , thread_last_post, thread_date).
  3418. *
  3419. * @param array $current_forum
  3420. * @param array $values
  3421. * @param int $courseId Optional
  3422. * @param int $userId Optional
  3423. *
  3424. * @return int post id
  3425. *
  3426. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3427. *
  3428. * @version february 2006, dokeos 1.8
  3429. */
  3430. function store_reply($current_forum, $values, $courseId = 0, $userId = 0)
  3431. {
  3432. $courseId = !empty($courseId) ? $courseId : api_get_course_int_id();
  3433. $_course = api_get_course_info_by_id($courseId);
  3434. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3435. $post_date = api_get_utc_datetime();
  3436. $userId = $userId ?: api_get_user_id();
  3437. if ($current_forum['allow_anonymous'] == 1) {
  3438. if (api_is_anonymous() && empty($userId)) {
  3439. $userId = api_get_anonymous_id();
  3440. }
  3441. }
  3442. if (empty($userId)) {
  3443. return false;
  3444. }
  3445. $visible = 1;
  3446. if ($current_forum['approval_direct_post'] == '1' &&
  3447. !api_is_allowed_to_edit(null, true)
  3448. ) {
  3449. $visible = 0;
  3450. }
  3451. $upload_ok = 1;
  3452. $new_post_id = 0;
  3453. if ($upload_ok) {
  3454. // We first store an entry in the forum_post table.
  3455. $new_post_id = Database::insert(
  3456. $table_posts,
  3457. [
  3458. 'c_id' => $courseId,
  3459. 'post_title' => $values['post_title'],
  3460. 'post_text' => isset($values['post_text']) ? ($values['post_text']) : null,
  3461. 'thread_id' => $values['thread_id'],
  3462. 'forum_id' => $values['forum_id'],
  3463. 'poster_id' => $userId,
  3464. 'post_id' => 0,
  3465. 'post_date' => $post_date,
  3466. 'post_notification' => isset($values['post_notification']) ? $values['post_notification'] : null,
  3467. 'post_parent_id' => isset($values['post_parent_id']) ? $values['post_parent_id'] : null,
  3468. 'visible' => $visible,
  3469. ]
  3470. );
  3471. if ($new_post_id) {
  3472. $sql = "UPDATE $table_posts SET post_id = iid WHERE iid = $new_post_id";
  3473. Database::query($sql);
  3474. $values['new_post_id'] = $new_post_id;
  3475. $message = get_lang('ReplyAdded');
  3476. if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
  3477. foreach ($_POST['file_ids'] as $key => $id) {
  3478. editAttachedFile(
  3479. [
  3480. 'comment' => $_POST['file_comments'][$key],
  3481. 'post_id' => $new_post_id,
  3482. ],
  3483. $id
  3484. );
  3485. }
  3486. }
  3487. // Update the thread.
  3488. updateThreadInfo($values['thread_id'], $new_post_id, $post_date);
  3489. // Update the forum.
  3490. api_item_property_update(
  3491. $_course,
  3492. TOOL_FORUM,
  3493. $values['forum_id'],
  3494. 'NewMessageInForum',
  3495. $userId
  3496. );
  3497. // Insert post
  3498. api_item_property_update(
  3499. $_course,
  3500. TOOL_FORUM_POST,
  3501. $new_post_id,
  3502. 'NewPost',
  3503. $userId
  3504. );
  3505. if ($current_forum['approval_direct_post'] == '1' &&
  3506. !api_is_allowed_to_edit(null, true)
  3507. ) {
  3508. $message .= '<br />'.get_lang('MessageHasToBeApproved').'<br />';
  3509. }
  3510. if ($current_forum['moderated'] &&
  3511. !api_is_allowed_to_edit(null, true)
  3512. ) {
  3513. $message .= '<br />'.get_lang('MessageHasToBeApproved').'<br />';
  3514. }
  3515. // Setting the notification correctly.
  3516. $my_post_notification = isset($values['post_notification']) ? $values['post_notification'] : null;
  3517. if ($my_post_notification == 1) {
  3518. set_notification('thread', $values['thread_id'], true);
  3519. }
  3520. send_notification_mails(
  3521. $values['forum_id'],
  3522. $values['thread_id'],
  3523. $values
  3524. );
  3525. add_forum_attachment_file('', $new_post_id);
  3526. $logInfo = [
  3527. 'tool' => TOOL_FORUM,
  3528. 'tool_id' => $values['forum_id'],
  3529. 'tool_id_detail' => $values['thread_id'],
  3530. 'action' => 'new-post',
  3531. 'action_details' => $values['action'],
  3532. 'info' => $values['post_title'],
  3533. ];
  3534. Event::registerLog($logInfo);
  3535. }
  3536. Session::erase('formelements');
  3537. Session::erase('origin');
  3538. Session::erase('breadcrumbs');
  3539. Session::erase('addedresource');
  3540. Session::erase('addedresourceid');
  3541. Display::addFlash(Display::return_message($message, 'confirmation', false));
  3542. } else {
  3543. Display::addFlash(
  3544. Display::return_message(
  3545. get_lang('UplNoFileUploaded').' '.get_lang('UplSelectFileFirst'),
  3546. 'error'
  3547. )
  3548. );
  3549. }
  3550. return $new_post_id;
  3551. }
  3552. /**
  3553. * This function displays the form that is used to edit a post. This can be a new thread or a reply.
  3554. *
  3555. * @param array contains all the information about the current post
  3556. * @param array contains all the information about the current thread
  3557. * @param array contains all info about the current forum (to check if attachments are allowed)
  3558. * @param array contains the default values to fill the form
  3559. *
  3560. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3561. *
  3562. * @version february 2006, dokeos 1.8
  3563. */
  3564. function show_edit_post_form(
  3565. $current_post,
  3566. $current_thread,
  3567. $current_forum,
  3568. $form_values = '',
  3569. $id_attach = 0
  3570. ) {
  3571. // Initialize the object.
  3572. $form = new FormValidator(
  3573. 'edit_post',
  3574. 'post',
  3575. api_get_self().'?'.api_get_cidreq().'&forum='.intval($_GET['forum']).'&thread='.intval($_GET['thread']).'&post='.intval($_GET['post'])
  3576. );
  3577. $form->addElement('header', get_lang('EditPost'));
  3578. // Setting the form elements.
  3579. $form->addElement('hidden', 'post_id', $current_post['post_id']);
  3580. $form->addElement('hidden', 'thread_id', $current_thread['thread_id']);
  3581. $form->addElement('hidden', 'id_attach', $id_attach);
  3582. if (empty($current_post['post_parent_id'])) {
  3583. $form->addElement('hidden', 'is_first_post_of_thread', '1');
  3584. }
  3585. $form->addElement('text', 'post_title', get_lang('Title'));
  3586. $form->applyFilter('post_title', 'html_filter');
  3587. $form->addElement(
  3588. 'html_editor',
  3589. 'post_text',
  3590. get_lang('Text'),
  3591. null,
  3592. api_is_allowed_to_edit(null, true) ? [
  3593. 'ToolbarSet' => 'Forum',
  3594. 'Width' => '100%',
  3595. 'Height' => '400',
  3596. ] : [
  3597. 'ToolbarSet' => 'ForumStudent',
  3598. 'Width' => '100%',
  3599. 'Height' => '400',
  3600. 'UserStatus' => 'student',
  3601. ]
  3602. );
  3603. $form->addRule('post_text', get_lang('ThisFieldIsRequired'), 'required');
  3604. $extraFields = new ExtraField('forum_post');
  3605. $extraFields->addElements($form, $current_post['post_id']);
  3606. $form->addButtonAdvancedSettings('advanced_params');
  3607. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  3608. if ($current_forum['moderated'] && api_is_allowed_to_edit(null, true)) {
  3609. $group = [];
  3610. $group[] = $form->createElement(
  3611. 'radio',
  3612. 'status',
  3613. null,
  3614. get_lang('Validated'),
  3615. 1
  3616. );
  3617. $group[] = $form->createElement(
  3618. 'radio',
  3619. 'status',
  3620. null,
  3621. get_lang('WaitingModeration'),
  3622. 2
  3623. );
  3624. $group[] = $form->createElement(
  3625. 'radio',
  3626. 'status',
  3627. null,
  3628. get_lang('Rejected'),
  3629. 3
  3630. );
  3631. $form->addGroup($group, 'status', get_lang('Status'));
  3632. }
  3633. $defaults['status']['status'] = isset($current_post['status']) && !empty($current_post['status']) ? $current_post['status'] : 2;
  3634. $form->addElement(
  3635. 'checkbox',
  3636. 'post_notification',
  3637. '',
  3638. get_lang('NotifyByEmail').' ('.$current_post['email'].')'
  3639. );
  3640. if (api_is_allowed_to_edit(null, true) &&
  3641. empty($current_post['post_parent_id'])
  3642. ) {
  3643. // The sticky checkbox only appears when it is the first post of a thread.
  3644. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  3645. if ($current_thread['thread_sticky'] == 1) {
  3646. $defaults['thread_sticky'] = true;
  3647. }
  3648. }
  3649. $form->addElement('html', '</div>');
  3650. $form->addFile('user_upload[]', get_lang('Attachment'));
  3651. $form->addButton(
  3652. 'add_attachment',
  3653. get_lang('AddAttachment'),
  3654. 'paperclip',
  3655. 'default',
  3656. 'default',
  3657. null,
  3658. ['id' => 'reply-add-attachment']
  3659. );
  3660. $form->addButtonUpdate(get_lang('Modify'), 'SubmitPost');
  3661. // Setting the default values for the form elements.
  3662. $defaults['post_title'] = $current_post['post_title'];
  3663. $defaults['post_text'] = $current_post['post_text'];
  3664. if ($current_post['post_notification'] == 1) {
  3665. $defaults['post_notification'] = true;
  3666. }
  3667. if (!empty($form_values)) {
  3668. $defaults['post_notification'] = Security::remove_XSS($form_values['post_notification']);
  3669. $defaults['thread_sticky'] = Security::remove_XSS($form_values['thread_sticky']);
  3670. }
  3671. $form->setDefaults($defaults);
  3672. // The course admin can make a thread sticky (=appears with special icon and always on top).
  3673. $form->addRule('post_title', get_lang('ThisFieldIsRequired'), 'required');
  3674. // Validation or display
  3675. if ($form->validate()) {
  3676. $values = $form->exportValues();
  3677. $values['item_id'] = $current_post['post_id'];
  3678. $extraFieldValues = new ExtraFieldValue('forum_post');
  3679. $extraFieldValues->saveFieldValues($values);
  3680. store_edit_post($current_forum, $values);
  3681. } else {
  3682. // Delete from $_SESSION forum attachment from other posts
  3683. clearAttachedFiles($current_post['post_id']);
  3684. // Get forum attachment ajax table to add it to form
  3685. $fileData = getAttachmentsAjaxTable($current_post['post_id'], $current_forum['forum_id']);
  3686. $form->addElement('html', $fileData);
  3687. $form->display();
  3688. }
  3689. }
  3690. /**
  3691. * This function stores the edit of a post in the forum_post table.
  3692. *
  3693. * @param array
  3694. *
  3695. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3696. *
  3697. * @version february 2006, dokeos 1.8
  3698. */
  3699. function store_edit_post($forumInfo, $values)
  3700. {
  3701. $logInfo = [
  3702. 'tool' => TOOL_FORUM,
  3703. 'tool_id' => $_GET['forum'],
  3704. 'tool_id_detail' => $values['thread_id'],
  3705. 'action' => 'edit-post',
  3706. 'action_details' => 'post',
  3707. 'info' => $values['post_title'],
  3708. ];
  3709. Event::registerLog($logInfo);
  3710. $threadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  3711. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3712. $course_id = api_get_course_int_id();
  3713. //check if this post is the first of the thread
  3714. // First we check if the change affects the thread and if so we commit
  3715. // the changes (sticky and post_title=thread_title are relevant).
  3716. $posts = getPosts($forumInfo, $values['thread_id']);
  3717. $first_post = null;
  3718. if (!empty($posts) && count($posts) > 0 && isset($posts[0])) {
  3719. $first_post = $posts[0];
  3720. }
  3721. if (!empty($first_post) && $first_post['post_id'] == $values['post_id']) {
  3722. // Simple edit
  3723. $params = [
  3724. 'thread_title' => $values['post_title'],
  3725. 'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : 0,
  3726. ];
  3727. $where = ['c_id = ? AND thread_id = ?' => [$course_id, $values['thread_id']]];
  3728. Database::update($threadTable, $params, $where);
  3729. }
  3730. $status = '';
  3731. $updateStatus = false;
  3732. if ($forumInfo['moderated']) {
  3733. if (api_is_allowed_to_edit(null, true)) {
  3734. $status = $values['status']['status'];
  3735. $updateStatus = true;
  3736. } else {
  3737. $status = CForumPost::STATUS_WAITING_MODERATION;
  3738. $updateStatus = true;
  3739. }
  3740. }
  3741. // Update the post_title and the post_text.
  3742. $params = [
  3743. 'post_title' => $values['post_title'],
  3744. 'post_text' => $values['post_text'],
  3745. 'post_notification' => isset($values['post_notification']) ? $values['post_notification'] : '',
  3746. ];
  3747. if ($updateStatus) {
  3748. $params['status'] = $status;
  3749. }
  3750. $where = ['c_id = ? AND post_id = ?' => [$course_id, $values['post_id']]];
  3751. Database::update($table_posts, $params, $where);
  3752. // Update attached files
  3753. if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
  3754. foreach ($_POST['file_ids'] as $key => $id) {
  3755. editAttachedFile(
  3756. [
  3757. 'comment' => $_POST['file_comments'][$key],
  3758. 'post_id' => $values['post_id'],
  3759. ],
  3760. $id
  3761. );
  3762. }
  3763. }
  3764. if (!empty($values['remove_attach'])) {
  3765. delete_attachment($values['post_id']);
  3766. }
  3767. if (empty($values['id_attach'])) {
  3768. add_forum_attachment_file(
  3769. isset($values['file_comment']) ? $values['file_comment'] : null,
  3770. $values['post_id']
  3771. );
  3772. } else {
  3773. edit_forum_attachment_file(
  3774. isset($values['file_comment']) ? $values['file_comment'] : null,
  3775. $values['post_id'],
  3776. $values['id_attach']
  3777. );
  3778. }
  3779. $message = get_lang('EditPostStored').'<br />';
  3780. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.intval($_GET['forum']).'&">'.get_lang('Forum').'</a><br />';
  3781. $message .= get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.intval($_GET['forum']).'&thread='.$values['thread_id'].'&post='.Security::remove_XSS($_GET['post']).'">'.get_lang('Message').'</a>';
  3782. Session::erase('formelements');
  3783. Session::erase('origin');
  3784. Session::erase('breadcrumbs');
  3785. Session::erase('addedresource');
  3786. Session::erase('addedresourceid');
  3787. echo Display::return_message($message, 'confirmation', false);
  3788. }
  3789. /**
  3790. * This function displays the firstname and lastname of the user as a link to the user tool.
  3791. *
  3792. * @param string names
  3793. * @ in_title : title tootip
  3794. *
  3795. * @return string HTML
  3796. *
  3797. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3798. *
  3799. * @version february 2006, dokeos 1.8
  3800. */
  3801. function display_user_link($user_id, $name, $origin = '', $in_title = '')
  3802. {
  3803. if ($user_id != 0) {
  3804. $userInfo = api_get_user_info($user_id);
  3805. return '<a href="'.$userInfo['profile_url'].'">'.Security::remove_XSS($userInfo['complete_name']).'</a>';
  3806. } else {
  3807. return $name.' ('.get_lang('Anonymous').')';
  3808. }
  3809. }
  3810. /**
  3811. * This function displays the user image from the profile, with a link to the user's details.
  3812. *
  3813. * @param int User's database ID
  3814. * @param string User's name
  3815. * @param string the origin where the forum is called (example : learnpath)
  3816. *
  3817. * @return string An HTML with the anchor and the image of the user
  3818. *
  3819. * @author Julio Montoya <gugli100@gmail.com>
  3820. */
  3821. function display_user_image($user_id, $name, $origin = '')
  3822. {
  3823. $userInfo = api_get_user_info($user_id);
  3824. $link = '<a href="'.(!empty($origin) ? '#' : $userInfo['profile_url']).'" '.(!empty($origin) ? 'target="_self"' : '').'>';
  3825. if ($user_id != 0) {
  3826. return $link.'<img src="'.$userInfo['avatar'].'" alt="'.$name.'" title="'.$name.'" /></a>';
  3827. } else {
  3828. return $link.Display::return_icon('unknown.jpg', $name).'</a>';
  3829. }
  3830. }
  3831. /**
  3832. * The thread view counter gets increased every time someone looks at the thread.
  3833. *
  3834. * @param int
  3835. *
  3836. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3837. *
  3838. * @version february 2006, dokeos 1.8
  3839. */
  3840. function increase_thread_view($thread_id)
  3841. {
  3842. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3843. $course_id = api_get_course_int_id();
  3844. $sql = "UPDATE $table_threads
  3845. SET thread_views = thread_views + 1
  3846. WHERE
  3847. c_id = $course_id AND
  3848. thread_id = '".intval($thread_id)."'";
  3849. Database::query($sql);
  3850. }
  3851. /**
  3852. * The relies counter gets increased every time somebody replies to the thread.
  3853. *
  3854. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3855. *
  3856. * @version february 2006, dokeos 1.8
  3857. *
  3858. * @param int $threadId
  3859. * @param string $lastPostId
  3860. * @param string $post_date
  3861. */
  3862. function updateThreadInfo($threadId, $lastPostId, $post_date)
  3863. {
  3864. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3865. $course_id = api_get_course_int_id();
  3866. $sql = "UPDATE $table_threads SET
  3867. thread_replies = thread_replies+1,
  3868. thread_last_post = '".Database::escape_string($lastPostId)."',
  3869. thread_date = '".Database::escape_string($post_date)."'
  3870. WHERE
  3871. c_id = $course_id AND
  3872. thread_id='".Database::escape_string($threadId)."'"; // this needs to be cleaned first
  3873. Database::query($sql);
  3874. }
  3875. /**
  3876. * This function is used to find all the information about what's new in the forum tool.
  3877. *
  3878. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3879. *
  3880. * @version february 2006, dokeos 1.8
  3881. */
  3882. function get_whats_new()
  3883. {
  3884. $userId = api_get_user_id();
  3885. $course_id = api_get_course_int_id();
  3886. if (empty($course_id) || empty($userId)) {
  3887. return false;
  3888. }
  3889. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3890. $tracking_last_tool_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  3891. $tool = TOOL_FORUM;
  3892. $lastForumAccess = Session::read('last_forum_access');
  3893. if (!$lastForumAccess) {
  3894. $sql = "SELECT * FROM $tracking_last_tool_access
  3895. WHERE
  3896. access_user_id = $userId AND
  3897. c_id = $course_id AND
  3898. access_tool = '".Database::escape_string($tool)."'";
  3899. $result = Database::query($sql);
  3900. $row = Database::fetch_array($result);
  3901. Session::write('last_forum_access', $row['access_date']);
  3902. $lastForumAccess = $row['access_date'];
  3903. }
  3904. $whatsNew = Session::read('whatsnew_post_info');
  3905. if (!$whatsNew) {
  3906. if ($lastForumAccess != '') {
  3907. $postInfo = [];
  3908. $sql = "SELECT * FROM $table_posts
  3909. WHERE
  3910. c_id = $course_id AND
  3911. visible = 1 AND
  3912. post_date > '".Database::escape_string($lastForumAccess)."'";
  3913. $result = Database::query($sql);
  3914. while ($row = Database::fetch_array($result)) {
  3915. $postInfo[$row['forum_id']][$row['thread_id']][$row['post_id']] = $row['post_date'];
  3916. }
  3917. Session::write('whatsnew_post_info', $postInfo);
  3918. }
  3919. }
  3920. }
  3921. /**
  3922. * This function approves a post = change.
  3923. *
  3924. * @param int $post_id the id of the post that will be deleted
  3925. * @param string $action make the post visible or invisible
  3926. *
  3927. * @return string language variable
  3928. *
  3929. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3930. *
  3931. * @version february 2006, dokeos 1.8
  3932. */
  3933. function approve_post($post_id, $action)
  3934. {
  3935. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3936. $course_id = api_get_course_int_id();
  3937. if ($action == 'invisible') {
  3938. $visibility_value = 0;
  3939. }
  3940. if ($action == 'visible') {
  3941. $visibility_value = 1;
  3942. handle_mail_cue('post', $post_id);
  3943. }
  3944. $sql = "UPDATE $table_posts SET
  3945. visible='".Database::escape_string($visibility_value)."'
  3946. WHERE c_id = $course_id AND post_id='".Database::escape_string($post_id)."'";
  3947. $return = Database::query($sql);
  3948. if ($return) {
  3949. return 'PostVisibilityChanged';
  3950. }
  3951. }
  3952. /**
  3953. * This function retrieves all the unapproved messages for a given forum
  3954. * This is needed to display the icon that there are unapproved messages in that thread (only the courseadmin can see this).
  3955. *
  3956. * @param $forum_id the forum where we want to know the unapproved messages of
  3957. *
  3958. * @return array returns
  3959. *
  3960. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3961. *
  3962. * @version february 2006, dokeos 1.8
  3963. */
  3964. function get_unaproved_messages($forum_id)
  3965. {
  3966. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3967. $course_id = api_get_course_int_id();
  3968. $return_array = [];
  3969. $sql = "SELECT DISTINCT thread_id FROM $table_posts
  3970. WHERE
  3971. c_id = $course_id AND
  3972. forum_id='".Database::escape_string($forum_id)."' AND
  3973. visible='0' ";
  3974. $result = Database::query($sql);
  3975. while ($row = Database::fetch_array($result)) {
  3976. $return_array[] = $row['thread_id'];
  3977. }
  3978. return $return_array;
  3979. }
  3980. /**
  3981. * This function sends the notification mails to everybody who stated that they wanted to be informed when a new post
  3982. * was added to a given thread.
  3983. *
  3984. * @param array reply information
  3985. *
  3986. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3987. *
  3988. * @version february 2006, dokeos 1.8
  3989. */
  3990. function send_notification_mails($forumId, $thread_id, $reply_info)
  3991. {
  3992. $table = Database::get_course_table(TABLE_FORUM_MAIL_QUEUE);
  3993. // First we need to check if
  3994. // 1. the forum category is visible
  3995. // 2. the forum is visible
  3996. // 3. the thread is visible
  3997. // 4. the reply is visible (=when there is)
  3998. $current_thread = get_thread_information($forumId, $thread_id);
  3999. $current_forum = get_forum_information($current_thread['forum_id'], $current_thread['c_id']);
  4000. $current_forum_category = null;
  4001. if (isset($current_forum['forum_category'])) {
  4002. $current_forum_category = get_forumcategory_information($current_forum['forum_category']);
  4003. }
  4004. $send_mails = false;
  4005. if ($current_thread['visibility'] == '1' &&
  4006. $current_forum['visibility'] == '1' &&
  4007. ($current_forum_category && $current_forum_category['visibility'] == '1') &&
  4008. $current_forum['approval_direct_post'] != '1'
  4009. ) {
  4010. $send_mails = true;
  4011. }
  4012. // The forum category, the forum, the thread and the reply are visible to the user
  4013. if ($send_mails && !empty($forumId)) {
  4014. $postId = isset($reply_info['new_post_id']) ? $reply_info['new_post_id'] : 0;
  4015. send_notifications($forumId, $thread_id, $postId);
  4016. } else {
  4017. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  4018. if (isset($current_forum['forum_id'])) {
  4019. $sql = "SELECT * FROM $table_notification
  4020. WHERE
  4021. c_id = ".api_get_course_int_id()." AND
  4022. (
  4023. forum_id = '".intval($current_forum['forum_id'])."' OR
  4024. thread_id = '".intval($thread_id)."'
  4025. ) ";
  4026. $result = Database::query($sql);
  4027. $user_id = api_get_user_id();
  4028. while ($row = Database::fetch_array($result)) {
  4029. $sql = "INSERT INTO $table (c_id, thread_id, post_id, user_id)
  4030. VALUES (".api_get_course_int_id().", '".intval($thread_id)."', '".intval($reply_info['new_post_id'])."', '$user_id' )";
  4031. Database::query($sql);
  4032. }
  4033. }
  4034. }
  4035. }
  4036. /**
  4037. * This function is called whenever something is made visible because there might
  4038. * be new posts and the user might have indicated that (s)he wanted to be
  4039. * informed about the new posts by mail.
  4040. *
  4041. * @param string $content Content type (post, thread, forum, forum_category)
  4042. * @param int $id Item DB ID of the corresponding content type
  4043. *
  4044. * @return string language variable
  4045. *
  4046. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4047. *
  4048. * @version february 2006, dokeos 1.8
  4049. */
  4050. function handle_mail_cue($content, $id)
  4051. {
  4052. $table_mailcue = Database::get_course_table(TABLE_FORUM_MAIL_QUEUE);
  4053. $table_forums = Database::get_course_table(TABLE_FORUM);
  4054. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4055. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4056. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  4057. $course_id = api_get_course_int_id();
  4058. $id = (int) $id;
  4059. /* If the post is made visible we only have to send mails to the people
  4060. who indicated that they wanted to be informed for that thread.*/
  4061. if ($content == 'post') {
  4062. // Getting the information about the post (need the thread_id).
  4063. $post_info = get_post_information($id);
  4064. $thread_id = (int) $post_info['thread_id'];
  4065. // Sending the mail to all the users that wanted to be informed for replies on this thread.
  4066. $sql = "SELECT users.firstname, users.lastname, users.user_id, users.email
  4067. FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  4068. WHERE
  4069. posts.c_id = $course_id AND
  4070. mailcue.c_id = $course_id AND
  4071. posts.thread_id = $thread_id AND
  4072. posts.post_notification = '1' AND
  4073. mailcue.thread_id = $thread_id AND
  4074. users.user_id = posts.poster_id AND
  4075. users.active = 1
  4076. GROUP BY users.email";
  4077. $result = Database::query($sql);
  4078. while ($row = Database::fetch_array($result)) {
  4079. $forumInfo = get_forum_information($post_info['forum_id']);
  4080. send_mail($row, $forumInfo, get_thread_information($post_info['forum_id'], $post_info['thread_id']), $post_info);
  4081. }
  4082. } elseif ($content == 'thread') {
  4083. // Sending the mail to all the users that wanted to be informed for replies on this thread.
  4084. $sql = "SELECT users.firstname, users.lastname, users.user_id, users.email, posts.forum_id
  4085. FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  4086. WHERE
  4087. posts.c_id = $course_id AND
  4088. mailcue.c_id = $course_id AND
  4089. posts.thread_id = $id AND
  4090. posts.post_notification = '1' AND
  4091. mailcue.thread_id = $id AND
  4092. users.user_id = posts.poster_id AND
  4093. users.active = 1
  4094. GROUP BY users.email";
  4095. $result = Database::query($sql);
  4096. while ($row = Database::fetch_array($result)) {
  4097. $forumInfo = get_forum_information($row['forum_id']);
  4098. send_mail($row, $forumInfo, get_thread_information($row['forum_id'], $id));
  4099. }
  4100. // Deleting the relevant entries from the mailcue.
  4101. $sql = "DELETE FROM $table_mailcue
  4102. WHERE c_id = $course_id AND thread_id = $id";
  4103. Database::query($sql);
  4104. } elseif ($content == 'forum') {
  4105. $sql = "SELECT thread_id FROM $table_threads
  4106. WHERE c_id = $course_id AND forum_id = $id";
  4107. $result = Database::query($sql);
  4108. while ($row = Database::fetch_array($result)) {
  4109. handle_mail_cue('thread', $row['thread_id']);
  4110. }
  4111. } elseif ($content == 'forum_category') {
  4112. $sql = "SELECT forum_id FROM $table_forums
  4113. WHERE c_id = $course_id AND forum_category = $id";
  4114. $result = Database::query($sql);
  4115. while ($row = Database::fetch_array($result)) {
  4116. handle_mail_cue('forum', $row['forum_id']);
  4117. }
  4118. } else {
  4119. return get_lang('Error');
  4120. }
  4121. }
  4122. /**
  4123. * This function sends the mails for the mail notification.
  4124. *
  4125. * @param array
  4126. * @param array
  4127. * @param array
  4128. * @param array
  4129. *
  4130. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4131. *
  4132. * @version february 2006, dokeos 1.8
  4133. */
  4134. function send_mail($userInfo, $forumInfo, $thread_information, $postInfo = [])
  4135. {
  4136. if (empty($userInfo) || empty($forumInfo) || empty($thread_information)) {
  4137. return false;
  4138. }
  4139. $_course = api_get_course_info();
  4140. $user_id = api_get_user_id();
  4141. $thread_link = '';
  4142. if (isset($thread_information) && is_array($thread_information)) {
  4143. $thread_link = api_get_path(WEB_CODE_PATH).
  4144. 'forum/viewthread.php?'.api_get_cidreq().'&forum='.$thread_information['forum_id'].'&thread='.$thread_information['thread_id'];
  4145. }
  4146. $email_body = get_lang('Dear').' '.api_get_person_name($userInfo['firstname'], $userInfo['lastname'], null, PERSON_NAME_EMAIL_ADDRESS).", <br />\n\r";
  4147. $email_body .= get_lang('NewForumPost').': '.$forumInfo['forum_title'].' - '.$thread_information['thread_title']." <br />\n";
  4148. $courseId = api_get_configuration_value('global_forums_course_id');
  4149. $subject = get_lang('NewForumPost').' - '.$_course['official_code'].': '.$forumInfo['forum_title'].' - '.$thread_information['thread_title']." <br />\n";
  4150. $courseInfoTitle = get_lang('Course').': '.$_course['name'].' - ['.$_course['official_code']."] - <br />\n";
  4151. if (!empty($courseId) && $_course['real_id'] == $courseId) {
  4152. $subject = get_lang('NewForumPost').': '.$forumInfo['forum_title'].' - '.$thread_information['thread_title']." <br />\n";
  4153. $courseInfoTitle = " <br />\n";
  4154. }
  4155. $email_body .= $courseInfoTitle;
  4156. if (!empty($postInfo) && isset($postInfo['post_text'])) {
  4157. $text = cut(strip_tags($postInfo['post_text']), 100);
  4158. if (!empty($text)) {
  4159. $email_body .= get_lang('Message').": <br />\n ";
  4160. $email_body .= $text;
  4161. $email_body .= "<br /><br />\n";
  4162. }
  4163. }
  4164. $email_body .= get_lang('YouWantedToStayInformed')."<br />\n";
  4165. if (!empty($thread_link)) {
  4166. $email_body .= get_lang('ThreadCanBeFoundHere')." : <br /><a href=\"".$thread_link."\">".$thread_link."</a>\n";
  4167. }
  4168. if ($userInfo['user_id'] != $user_id) {
  4169. MessageManager::send_message(
  4170. $userInfo['user_id'],
  4171. $subject,
  4172. $email_body,
  4173. [],
  4174. [],
  4175. null,
  4176. null,
  4177. null,
  4178. null,
  4179. $user_id
  4180. );
  4181. }
  4182. }
  4183. /**
  4184. * This function displays the form for moving a thread to a different (already existing) forum.
  4185. *
  4186. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4187. *
  4188. * @version february 2006, dokeos 1.8
  4189. */
  4190. function move_thread_form()
  4191. {
  4192. $form = new FormValidator(
  4193. 'movepost',
  4194. 'post',
  4195. api_get_self().'?forum='.intval($_GET['forum']).'&thread='.intval($_GET['thread']).'&action='.Security::remove_XSS($_GET['action']).'&'.api_get_cidreq()
  4196. );
  4197. // The header for the form
  4198. $form->addElement('header', get_lang('MoveThread'));
  4199. // Invisible form: the thread_id
  4200. $form->addElement('hidden', 'thread_id', intval($_GET['thread']));
  4201. // the fora
  4202. $forum_categories = get_forum_categories();
  4203. $forums = get_forums();
  4204. $htmlcontent = '<div class="row">
  4205. <div class="label">
  4206. <span class="form_required">*</span>'.get_lang('MoveTo').'
  4207. </div>
  4208. <div class="formw">';
  4209. $htmlcontent .= '<select name="forum">';
  4210. foreach ($forum_categories as $key => $category) {
  4211. $htmlcontent .= '<optgroup label="'.$category['cat_title'].'">';
  4212. foreach ($forums as $key => $forum) {
  4213. if (isset($forum['forum_category'])) {
  4214. if ($forum['forum_category'] == $category['cat_id']) {
  4215. $htmlcontent .= '<option value="'.$forum['forum_id'].'">'.$forum['forum_title'].'</option>';
  4216. }
  4217. }
  4218. }
  4219. $htmlcontent .= '</optgroup>';
  4220. }
  4221. $htmlcontent .= "</select>";
  4222. $htmlcontent .= ' </div>
  4223. </div>';
  4224. $form->addElement('html', $htmlcontent);
  4225. // The OK button
  4226. $form->addButtonSave(get_lang('MoveThread'), 'SubmitForum');
  4227. // Validation or display
  4228. if ($form->validate()) {
  4229. $values = $form->exportValues();
  4230. if (isset($_POST['forum'])) {
  4231. store_move_thread($values);
  4232. }
  4233. } else {
  4234. $form->display();
  4235. }
  4236. }
  4237. /**
  4238. * This function displays the form for moving a post message to a different (already existing) or a new thread.
  4239. *
  4240. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4241. *
  4242. * @version february 2006, dokeos 1.8
  4243. */
  4244. function move_post_form()
  4245. {
  4246. $form = new FormValidator(
  4247. 'movepost',
  4248. 'post',
  4249. api_get_self().'?'.api_get_cidreq().'&forum='.intval($_GET['forum']).'&thread='.intval($_GET['thread']).'&post='.Security::remove_XSS($_GET['post']).'&action='.Security::remove_XSS($_GET['action']).'&post='.Security::remove_XSS($_GET['post'])
  4250. );
  4251. // The header for the form
  4252. $form->addElement('header', '', get_lang('MovePost'));
  4253. // Invisible form: the post_id
  4254. $form->addElement('hidden', 'post_id', intval($_GET['post']));
  4255. // Dropdown list: Threads of this forum
  4256. $threads = get_threads($_GET['forum']);
  4257. //my_print_r($threads);
  4258. $threads_list[0] = get_lang('ANewThread');
  4259. foreach ($threads as $key => $value) {
  4260. $threads_list[$value['thread_id']] = $value['thread_title'];
  4261. }
  4262. $form->addElement('select', 'thread', get_lang('MoveToThread'), $threads_list);
  4263. $form->applyFilter('thread', 'html_filter');
  4264. // The OK button
  4265. $form->addButtonSave(get_lang('MovePost'), 'submit');
  4266. // Setting the rules
  4267. $form->addRule('thread', get_lang('ThisFieldIsRequired'), 'required');
  4268. // Validation or display
  4269. if ($form->validate()) {
  4270. $values = $form->exportValues();
  4271. store_move_post($values);
  4272. } else {
  4273. $form->display();
  4274. }
  4275. }
  4276. /**
  4277. * @param array
  4278. *
  4279. * @return string HTML language variable
  4280. *
  4281. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4282. *
  4283. * @version february 2006, dokeos 1.8
  4284. */
  4285. function store_move_post($values)
  4286. {
  4287. $_course = api_get_course_info();
  4288. $course_id = api_get_course_int_id();
  4289. $table_forums = Database::get_course_table(TABLE_FORUM);
  4290. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4291. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4292. if ($values['thread'] == '0') {
  4293. $current_post = get_post_information($values['post_id']);
  4294. // Storing a new thread.
  4295. $params = [
  4296. 'c_id' => $course_id,
  4297. 'thread_title' => $current_post['post_title'],
  4298. 'forum_id' => $current_post['forum_id'],
  4299. 'thread_poster_id' => $current_post['poster_id'],
  4300. 'thread_poster_name' => $current_post['poster_name'],
  4301. 'thread_last_post' => $values['post_id'],
  4302. 'thread_date' => $current_post['post_date'],
  4303. ];
  4304. $new_thread_id = Database::insert($table_threads, $params);
  4305. api_item_property_update(
  4306. $_course,
  4307. TOOL_FORUM_THREAD,
  4308. $new_thread_id,
  4309. 'visible',
  4310. $current_post['poster_id']
  4311. );
  4312. // Moving the post to the newly created thread.
  4313. $sql = "UPDATE $table_posts SET thread_id='".intval($new_thread_id)."', post_parent_id = NULL
  4314. WHERE c_id = $course_id AND post_id='".intval($values['post_id'])."'";
  4315. Database::query($sql);
  4316. // Resetting the parent_id of the thread to 0 for all those who had this moved post as parent.
  4317. $sql = "UPDATE $table_posts SET post_parent_id = NULL
  4318. WHERE c_id = $course_id AND post_parent_id='".intval($values['post_id'])."'";
  4319. Database::query($sql);
  4320. // Updating updating the number of threads in the forum.
  4321. $sql = "UPDATE $table_forums SET forum_threads=forum_threads+1
  4322. WHERE c_id = $course_id AND forum_id='".intval($current_post['forum_id'])."'";
  4323. Database::query($sql);
  4324. // Resetting the last post of the old thread and decreasing the number of replies and the thread.
  4325. $sql = "SELECT * FROM $table_posts
  4326. WHERE c_id = $course_id AND thread_id='".intval($current_post['thread_id'])."'
  4327. ORDER BY post_id DESC";
  4328. $result = Database::query($sql);
  4329. $row = Database::fetch_array($result);
  4330. $sql = "UPDATE $table_threads SET
  4331. thread_last_post='".$row['post_id']."',
  4332. thread_replies=thread_replies-1
  4333. WHERE
  4334. c_id = $course_id AND
  4335. thread_id='".intval($current_post['thread_id'])."'";
  4336. Database::query($sql);
  4337. } else {
  4338. // Moving to the chosen thread.
  4339. $sql = "SELECT thread_id FROM ".$table_posts."
  4340. WHERE c_id = $course_id AND post_id = '".$values['post_id']."' ";
  4341. $result = Database::query($sql);
  4342. $row = Database::fetch_array($result);
  4343. $original_thread_id = $row['thread_id'];
  4344. $sql = "SELECT thread_last_post FROM ".$table_threads."
  4345. WHERE c_id = $course_id AND thread_id = '".$original_thread_id."' ";
  4346. $result = Database::query($sql);
  4347. $row = Database::fetch_array($result);
  4348. $thread_is_last_post = $row['thread_last_post'];
  4349. // If is this thread, update the thread_last_post with the last one.
  4350. if ($thread_is_last_post == $values['post_id']) {
  4351. $sql = "SELECT post_id FROM ".$table_posts."
  4352. WHERE c_id = $course_id AND thread_id = '".$original_thread_id."' AND post_id <> '".$values['post_id']."'
  4353. ORDER BY post_date DESC LIMIT 1";
  4354. $result = Database::query($sql);
  4355. $row = Database::fetch_array($result);
  4356. $thread_new_last_post = $row['post_id'];
  4357. $sql = "UPDATE ".$table_threads." SET thread_last_post = '".$thread_new_last_post."'
  4358. WHERE c_id = $course_id AND thread_id = '".$original_thread_id."' ";
  4359. Database::query($sql);
  4360. }
  4361. $sql = "UPDATE $table_threads SET thread_replies=thread_replies-1
  4362. WHERE c_id = $course_id AND thread_id='".$original_thread_id."'";
  4363. Database::query($sql);
  4364. // moving to the chosen thread
  4365. $sql = "UPDATE $table_posts SET thread_id='".intval($_POST['thread'])."', post_parent_id = NULL
  4366. WHERE c_id = $course_id AND post_id='".intval($values['post_id'])."'";
  4367. Database::query($sql);
  4368. // resetting the parent_id of the thread to 0 for all those who had this moved post as parent
  4369. $sql = "UPDATE $table_posts SET post_parent_id = NULL
  4370. WHERE c_id = $course_id AND post_parent_id='".intval($values['post_id'])."'";
  4371. Database::query($sql);
  4372. $sql = "UPDATE $table_threads SET thread_replies=thread_replies+1
  4373. WHERE c_id = $course_id AND thread_id='".intval($_POST['thread'])."'";
  4374. Database::query($sql);
  4375. }
  4376. return get_lang('ThreadMoved');
  4377. }
  4378. /**
  4379. * @param array
  4380. *
  4381. * @return string HTML language variable
  4382. *
  4383. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4384. *
  4385. * @version february 2006, dokeos 1.8
  4386. */
  4387. function store_move_thread($values)
  4388. {
  4389. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4390. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4391. $courseId = api_get_course_int_id();
  4392. $sessionId = api_get_session_id();
  4393. $forumId = intval($_POST['forum']);
  4394. $threadId = intval($_POST['thread_id']);
  4395. $forumInfo = get_forums($forumId);
  4396. // Change the thread table: Setting the forum_id to the new forum.
  4397. $sql = "UPDATE $table_threads SET forum_id = $forumId
  4398. WHERE c_id = $courseId AND thread_id = $threadId";
  4399. Database::query($sql);
  4400. // Changing all the posts of the thread: setting the forum_id to the new forum.
  4401. $sql = "UPDATE $table_posts SET forum_id = $forumId
  4402. WHERE c_id = $courseId AND thread_id= $threadId";
  4403. Database::query($sql);
  4404. // Fix group id, if forum is moved to a different group
  4405. if (!empty($forumInfo['to_group_id'])) {
  4406. $groupId = $forumInfo['to_group_id'];
  4407. $item = api_get_item_property_info(
  4408. $courseId,
  4409. TABLE_FORUM_THREAD,
  4410. $threadId,
  4411. $sessionId,
  4412. $groupId
  4413. );
  4414. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4415. $sessionCondition = api_get_session_condition($sessionId);
  4416. if (!empty($item)) {
  4417. if ($item['to_group_id'] != $groupId) {
  4418. $sql = "UPDATE $table
  4419. SET to_group_id = $groupId
  4420. WHERE
  4421. tool = '".TABLE_FORUM_THREAD."' AND
  4422. c_id = $courseId AND
  4423. ref = ".$item['ref']."
  4424. $sessionCondition
  4425. ";
  4426. Database::query($sql);
  4427. }
  4428. } else {
  4429. $sql = "UPDATE $table
  4430. SET to_group_id = $groupId
  4431. WHERE
  4432. tool = '".TABLE_FORUM_THREAD."' AND
  4433. c_id = $courseId AND
  4434. ref = ".$threadId."
  4435. $sessionCondition
  4436. ";
  4437. Database::query($sql);
  4438. }
  4439. }
  4440. return get_lang('ThreadMoved');
  4441. }
  4442. /**
  4443. * Prepares a string for displaying by highlighting the search results inside, if any.
  4444. *
  4445. * @param string $input the input string
  4446. *
  4447. * @return string the same string with highlighted hits inside
  4448. *
  4449. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, February 2006 - the initial version.
  4450. * @author Ivan Tcholakov, March 2011 - adaptation for Chamilo LMS.
  4451. */
  4452. function prepare4display($input)
  4453. {
  4454. static $highlightcolors = ['yellow', '#33CC33', '#3399CC', '#9999FF', '#33CC33'];
  4455. static $search;
  4456. if (!isset($search)) {
  4457. if (isset($_POST['search_term'])) {
  4458. $search = $_POST['search_term']; // No html at all.
  4459. } elseif (isset($_GET['search'])) {
  4460. $search = $_GET['search'];
  4461. } else {
  4462. $search = '';
  4463. }
  4464. }
  4465. if (!empty($search)) {
  4466. if (strstr($search, '+')) {
  4467. $search_terms = explode('+', $search);
  4468. } else {
  4469. $search_terms[] = trim($search);
  4470. }
  4471. $counter = 0;
  4472. foreach ($search_terms as $key => $search_term) {
  4473. $input = api_preg_replace(
  4474. '/'.preg_quote(trim($search_term), '/').'/i',
  4475. '<span style="background-color: '.$highlightcolors[$counter].'">$0</span>',
  4476. $input
  4477. );
  4478. $counter++;
  4479. }
  4480. }
  4481. // TODO: Security should be implemented outside this function.
  4482. // Change this to COURSEMANAGERLOWSECURITY or COURSEMANAGER to lower filtering and allow more styles
  4483. // (see comments of Security::remove_XSS() method to learn about other levels).
  4484. return Security::remove_XSS($input, STUDENT, true);
  4485. }
  4486. /**
  4487. * Display the search form for the forum and display the search results.
  4488. *
  4489. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  4490. *
  4491. * @version march 2008, dokeos 1.8.5
  4492. */
  4493. function forum_search()
  4494. {
  4495. $form = new FormValidator(
  4496. 'forumsearch',
  4497. 'post',
  4498. 'forumsearch.php?'.api_get_cidreq()
  4499. );
  4500. // Setting the form elements.
  4501. $form->addElement('header', '', get_lang('ForumSearch'));
  4502. $form->addElement('text', 'search_term', get_lang('SearchTerm'), ['autofocus']);
  4503. $form->applyFilter('search_term', 'html_filter');
  4504. $form->addElement('static', 'search_information', '', get_lang('ForumSearchInformation'));
  4505. $form->addButtonSearch(get_lang('Search'));
  4506. // Setting the rules.
  4507. $form->addRule('search_term', get_lang('ThisFieldIsRequired'), 'required');
  4508. $form->addRule('search_term', get_lang('TooShort'), 'minlength', 3);
  4509. // Validation or display.
  4510. if ($form->validate()) {
  4511. $values = $form->exportValues();
  4512. $form->setDefaults($values);
  4513. $form->display();
  4514. // Display the search results.
  4515. display_forum_search_results(stripslashes($values['search_term']));
  4516. } else {
  4517. $form->display();
  4518. }
  4519. }
  4520. /**
  4521. * Display the search results.
  4522. *
  4523. * @param string
  4524. * @param string $search_term
  4525. *
  4526. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  4527. *
  4528. * @version march 2008, dokeos 1.8.5
  4529. */
  4530. function display_forum_search_results($search_term)
  4531. {
  4532. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4533. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4534. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4535. $session_id = api_get_session_id();
  4536. $course_id = api_get_course_int_id();
  4537. // Defining the search strings as an array.
  4538. if (strstr($search_term, '+')) {
  4539. $search_terms = explode('+', $search_term);
  4540. } else {
  4541. $search_terms[] = $search_term;
  4542. }
  4543. // Search restriction.
  4544. foreach ($search_terms as $value) {
  4545. $search_restriction[] = "
  4546. (
  4547. posts.post_title LIKE '%".Database::escape_string(trim($value))."%' OR
  4548. posts.post_text LIKE '%".Database::escape_string(trim($value))."%'
  4549. )";
  4550. }
  4551. $sessionCondition = api_get_session_condition(
  4552. $session_id,
  4553. true,
  4554. false,
  4555. 'item_property.session_id'
  4556. );
  4557. $sql = "SELECT posts.*
  4558. FROM $table_posts posts
  4559. INNER JOIN $table_threads threads
  4560. ON (posts.thread_id = threads.thread_id AND posts.c_id = threads.c_id)
  4561. INNER JOIN $table_item_property item_property
  4562. ON (item_property.ref = threads.thread_id AND item_property.c_id = threads.c_id)
  4563. WHERE
  4564. posts.c_id = $course_id AND
  4565. item_property.c_id = $course_id AND
  4566. item_property.visibility = 1
  4567. $sessionCondition AND
  4568. posts.visible = 1 AND
  4569. item_property.tool = '".TOOL_FORUM_THREAD."' AND
  4570. ".implode(' AND ', $search_restriction)."
  4571. GROUP BY posts.post_id";
  4572. // Getting all the information of the forum categories.
  4573. $forum_categories_list = get_forum_categories();
  4574. // Getting all the information of the forums.
  4575. $forum_list = get_forums();
  4576. $result = Database::query($sql);
  4577. $search_results = [];
  4578. while ($row = Database::fetch_array($result, 'ASSOC')) {
  4579. $forumId = $row['forum_id'];
  4580. $forumData = get_forums($forumId);
  4581. $category = isset($forum_categories_list[$forumData['forum_category']]) ? $forum_categories_list[$forumData['forum_category']] : null;
  4582. $display_result = false;
  4583. /*
  4584. We only show it when
  4585. 1. forum category is visible
  4586. 2. forum is visible
  4587. 3. thread is visible (to do)
  4588. 4. post is visible
  4589. */
  4590. if (!api_is_allowed_to_edit(null, true)) {
  4591. if (!empty($category)) {
  4592. if ($category['visibility'] == '1' && $forumData['visibility'] == '1') {
  4593. $display_result = true;
  4594. }
  4595. } else {
  4596. if ($forumData['visible'] == '1') {
  4597. $display_result = true;
  4598. }
  4599. }
  4600. } else {
  4601. $display_result = true;
  4602. }
  4603. if ($display_result) {
  4604. $categoryName = !empty($category) ? $category['cat_title'] : '';
  4605. $search_results_item = '<li><a href="viewforumcategory.php?'.api_get_cidreq().'&forumcategory='.$forumData['forum_category'].'&search='.urlencode($search_term).'">'.
  4606. prepare4display($categoryName).'</a> &gt; ';
  4607. $search_results_item .= '<a href="viewforum.php?'.api_get_cidreq().'&forum='.$forumId.'&search='.urlencode($search_term).'">'.
  4608. prepare4display($forum_list[$row['forum_id']]['forum_title']).'</a> &gt; ';
  4609. $search_results_item .= '<a href="viewthread.php?'.api_get_cidreq().'&forum='.$forumId.'&thread='.$row['thread_id'].'&search='.urlencode($search_term).'">'.
  4610. prepare4display($row['post_title']).'</a>';
  4611. $search_results_item .= '<br />';
  4612. if (api_strlen($row['post_title']) > 200) {
  4613. $search_results_item .= prepare4display(api_substr(strip_tags($row['post_title']), 0, 200)).'...';
  4614. } else {
  4615. $search_results_item .= prepare4display($row['post_title']);
  4616. }
  4617. $search_results_item .= '</li>';
  4618. $search_results[] = $search_results_item;
  4619. }
  4620. }
  4621. echo '<legend>'.count($search_results).' '.get_lang('ForumSearchResults').'</legend>';
  4622. echo '<ol>';
  4623. if ($search_results) {
  4624. echo implode($search_results);
  4625. }
  4626. echo '</ol>';
  4627. }
  4628. /**
  4629. * Return the link to the forum search page.
  4630. *
  4631. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  4632. *
  4633. * @version April 2008, dokeos 1.8.5
  4634. */
  4635. function search_link()
  4636. {
  4637. $return = '';
  4638. $origin = api_get_origin();
  4639. if ($origin != 'learnpath') {
  4640. $return = '<a href="forumsearch.php?'.api_get_cidreq().'&action=search"> ';
  4641. $return .= Display::return_icon('search.png', get_lang('Search'), '', ICON_SIZE_MEDIUM).'</a>';
  4642. if (!empty($_GET['search'])) {
  4643. $return .= ': '.Security::remove_XSS($_GET['search']).' ';
  4644. $url = api_get_self().'?';
  4645. $url_parameter = [];
  4646. foreach ($_GET as $key => $value) {
  4647. if ($key != 'search') {
  4648. $url_parameter[] = Security::remove_XSS($key).'='.Security::remove_XSS($value);
  4649. }
  4650. }
  4651. $url = $url.implode('&', $url_parameter);
  4652. $return .= '<a href="'.$url.'">'.Display::return_icon('delete.gif', get_lang('RemoveSearchResults')).'</a>';
  4653. }
  4654. }
  4655. return $return;
  4656. }
  4657. /**
  4658. * This function adds an attachment file into a forum.
  4659. *
  4660. * @param string $file_comment a comment about file
  4661. * @param int $last_id from forum_post table
  4662. *
  4663. * @return false|null
  4664. */
  4665. function add_forum_attachment_file($file_comment, $last_id)
  4666. {
  4667. $_course = api_get_course_info();
  4668. $agenda_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4669. if (empty($_FILES['user_upload'])) {
  4670. return false;
  4671. }
  4672. $filesData = [];
  4673. if (!is_array($_FILES['user_upload']['name'])) {
  4674. $filesData[] = $_FILES['user_upload'];
  4675. } else {
  4676. $fileCount = count($_FILES['user_upload']['name']);
  4677. $fileKeys = array_keys($_FILES['user_upload']);
  4678. for ($i = 0; $i < $fileCount; $i++) {
  4679. foreach ($fileKeys as $key) {
  4680. $filesData[$i][$key] = $_FILES['user_upload'][$key][$i];
  4681. }
  4682. }
  4683. }
  4684. foreach ($filesData as $attachment) {
  4685. if (empty($attachment['name'])) {
  4686. continue;
  4687. }
  4688. $upload_ok = process_uploaded_file($attachment);
  4689. if (!$upload_ok) {
  4690. continue;
  4691. }
  4692. $course_dir = $_course['path'].'/upload/forum';
  4693. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4694. $updir = $sys_course_path.$course_dir;
  4695. // Try to add an extension to the file if it hasn't one.
  4696. $new_file_name = add_ext_on_mime(
  4697. stripslashes($attachment['name']),
  4698. $attachment['type']
  4699. );
  4700. // User's file name
  4701. $file_name = $attachment['name'];
  4702. if (!filter_extension($new_file_name)) {
  4703. Display::addFlash(
  4704. Display::return_message(
  4705. get_lang('UplUnableToSaveFileFilteredExtension'),
  4706. 'error'
  4707. )
  4708. );
  4709. return;
  4710. }
  4711. $new_file_name = uniqid('');
  4712. $new_path = $updir.'/'.$new_file_name;
  4713. $result = @move_uploaded_file($attachment['tmp_name'], $new_path);
  4714. $safe_file_comment = Database::escape_string($file_comment);
  4715. $safe_file_name = Database::escape_string($file_name);
  4716. $safe_new_file_name = Database::escape_string($new_file_name);
  4717. $last_id = intval($last_id);
  4718. // Storing the attachments if any.
  4719. if (!$result) {
  4720. return;
  4721. }
  4722. $last_id_file = Database::insert(
  4723. $agenda_forum_attachment,
  4724. [
  4725. 'c_id' => api_get_course_int_id(),
  4726. 'filename' => $safe_file_name,
  4727. 'comment' => $safe_file_comment,
  4728. 'path' => $safe_new_file_name,
  4729. 'post_id' => $last_id,
  4730. 'size' => intval($attachment['size']),
  4731. ]
  4732. );
  4733. api_item_property_update(
  4734. $_course,
  4735. TOOL_FORUM_ATTACH,
  4736. $last_id_file,
  4737. 'ForumAttachmentAdded',
  4738. api_get_user_id()
  4739. );
  4740. }
  4741. }
  4742. /**
  4743. * This function edits an attachment file into a forum.
  4744. *
  4745. * @param string $file_comment a comment about file
  4746. * @param int $post_id
  4747. * @param int $id_attach attachment file Id
  4748. */
  4749. function edit_forum_attachment_file($file_comment, $post_id, $id_attach)
  4750. {
  4751. $_course = api_get_course_info();
  4752. $table_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4753. $course_id = api_get_course_int_id();
  4754. $filesData = [];
  4755. if (!is_array($_FILES['user_upload']['name'])) {
  4756. $filesData[] = $_FILES['user_upload'];
  4757. } else {
  4758. $fileCount = count($_FILES['user_upload']['name']);
  4759. $fileKeys = array_keys($_FILES['user_upload']);
  4760. for ($i = 0; $i < $fileCount; $i++) {
  4761. foreach ($fileKeys as $key) {
  4762. $filesData[$i][$key] = $_FILES['user_upload'][$key][$i];
  4763. }
  4764. }
  4765. }
  4766. foreach ($filesData as $attachment) {
  4767. if (empty($attachment['name'])) {
  4768. continue;
  4769. }
  4770. $upload_ok = process_uploaded_file($attachment);
  4771. if (!$upload_ok) {
  4772. continue;
  4773. }
  4774. $course_dir = $_course['path'].'/upload/forum';
  4775. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4776. $updir = $sys_course_path.$course_dir;
  4777. // Try to add an extension to the file if it hasn't one.
  4778. $new_file_name = add_ext_on_mime(stripslashes($attachment['name']), $attachment['type']);
  4779. // User's file name
  4780. $file_name = $attachment['name'];
  4781. if (!filter_extension($new_file_name)) {
  4782. Display::addFlash(
  4783. Display::return_message(
  4784. get_lang('UplUnableToSaveFileFilteredExtension'),
  4785. 'error'
  4786. )
  4787. );
  4788. } else {
  4789. $new_file_name = uniqid('');
  4790. $new_path = $updir.'/'.$new_file_name;
  4791. $result = @move_uploaded_file($attachment['tmp_name'], $new_path);
  4792. $safe_file_comment = Database::escape_string($file_comment);
  4793. $safe_file_name = Database::escape_string($file_name);
  4794. $safe_new_file_name = Database::escape_string($new_file_name);
  4795. $safe_post_id = (int) $post_id;
  4796. $safe_id_attach = (int) $id_attach;
  4797. // Storing the attachments if any.
  4798. if ($result) {
  4799. $sql = "UPDATE $table_forum_attachment
  4800. SET
  4801. filename = '$safe_file_name',
  4802. comment = '$safe_file_comment',
  4803. path = '$safe_new_file_name',
  4804. post_id = '$safe_post_id',
  4805. size ='".$attachment['size']."'
  4806. WHERE c_id = $course_id AND id = '$safe_id_attach'";
  4807. Database::query($sql);
  4808. api_item_property_update(
  4809. $_course,
  4810. TOOL_FORUM_ATTACH,
  4811. $safe_id_attach,
  4812. 'ForumAttachmentUpdated',
  4813. api_get_user_id()
  4814. );
  4815. }
  4816. }
  4817. }
  4818. }
  4819. /**
  4820. * Show a list with all the attachments according to the post's id.
  4821. *
  4822. * @param int $postId
  4823. *
  4824. * @return array with the post info
  4825. *
  4826. * @author Julio Montoya
  4827. *
  4828. * @version avril 2008, dokeos 1.8.5
  4829. */
  4830. function get_attachment($postId)
  4831. {
  4832. $table = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4833. $course_id = api_get_course_int_id();
  4834. $row = [];
  4835. $postId = (int) $postId;
  4836. if (empty($postId)) {
  4837. return [];
  4838. }
  4839. $sql = "SELECT iid, path, filename, comment
  4840. FROM $table
  4841. WHERE c_id = $course_id AND post_id = $postId";
  4842. $result = Database::query($sql);
  4843. if (Database::num_rows($result) != 0) {
  4844. $row = Database::fetch_array($result);
  4845. }
  4846. return $row;
  4847. }
  4848. /**
  4849. * @param int $postId
  4850. *
  4851. * @return array
  4852. */
  4853. function getAllAttachment($postId)
  4854. {
  4855. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4856. $courseId = api_get_course_int_id();
  4857. $postId = (int) $postId;
  4858. if (empty($postId)) {
  4859. return [];
  4860. }
  4861. $columns = ['iid', 'path', 'filename', 'comment'];
  4862. $conditions = [
  4863. 'where' => [
  4864. 'c_id = ? AND post_id = ?' => [$courseId, $postId],
  4865. ],
  4866. ];
  4867. $array = Database::select(
  4868. $columns,
  4869. $forumAttachmentTable,
  4870. $conditions,
  4871. 'all',
  4872. 'ASSOC'
  4873. );
  4874. return $array;
  4875. }
  4876. /**
  4877. * Delete the all the attachments from the DB and the file according to the post's id or attach id(optional).
  4878. *
  4879. * @param int $post_id
  4880. * @param int $id_attach
  4881. *
  4882. * @return int
  4883. *
  4884. * @author Julio Montoya
  4885. *
  4886. * @version october 2014, chamilo 1.9.8
  4887. */
  4888. function delete_attachment($post_id, $id_attach = 0)
  4889. {
  4890. $_course = api_get_course_info();
  4891. $forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4892. $course_id = api_get_course_int_id();
  4893. $cond = (!empty($id_attach)) ? " iid = ".(int) $id_attach."" : " post_id = ".(int) $post_id."";
  4894. $sql = "SELECT path FROM $forum_table_attachment WHERE c_id = $course_id AND $cond";
  4895. $res = Database::query($sql);
  4896. $row = Database::fetch_array($res);
  4897. $course_dir = $_course['path'].'/upload/forum';
  4898. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4899. $updir = $sys_course_path.$course_dir;
  4900. $my_path = isset($row['path']) ? $row['path'] : null;
  4901. $file = $updir.'/'.$my_path;
  4902. if (Security::check_abs_path($file, $updir)) {
  4903. @unlink($file);
  4904. }
  4905. // Delete from forum_attachment table.
  4906. $sql = "DELETE FROM $forum_table_attachment
  4907. WHERE c_id = $course_id AND $cond ";
  4908. $result = Database::query($sql);
  4909. if ($result !== false) {
  4910. $affectedRows = Database::affected_rows($result);
  4911. } else {
  4912. $affectedRows = 0;
  4913. }
  4914. // Update item_property.
  4915. api_item_property_update(
  4916. $_course,
  4917. TOOL_FORUM_ATTACH,
  4918. $id_attach,
  4919. 'ForumAttachmentDelete',
  4920. api_get_user_id()
  4921. );
  4922. if (!empty($result) && !empty($id_attach)) {
  4923. Display::addFlash(Display::return_message(get_lang('AttachmentFileDeleteSuccess'), 'confirmation'));
  4924. }
  4925. return $affectedRows;
  4926. }
  4927. /**
  4928. * This function gets all the forum information of the all the forum of the group.
  4929. *
  4930. * @param array $groupInfo the id of the group we need the fora of (see forum.forum_of_group)
  4931. *
  4932. * @return array
  4933. *
  4934. * @todo this is basically the same code as the get_forums function. Consider merging the two.
  4935. */
  4936. function get_forums_of_group($groupInfo)
  4937. {
  4938. $table_forums = Database::get_course_table(TABLE_FORUM);
  4939. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4940. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4941. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4942. $course_id = api_get_course_int_id();
  4943. $groupId = (int) $groupInfo['id'];
  4944. // Student
  4945. // Select all the forum information of all forums (that are visible to students).
  4946. $sql = "SELECT * FROM $table_forums forum
  4947. INNER JOIN $table_item_property item_properties
  4948. ON (forum.forum_id = item_properties.ref AND item_properties.c_id = forum.c_id)
  4949. WHERE
  4950. forum.forum_of_group = $groupId AND
  4951. forum.c_id = $course_id AND
  4952. item_properties.c_id = $course_id AND
  4953. item_properties.visibility = 1 AND
  4954. item_properties.tool = '".TOOL_FORUM."'
  4955. ORDER BY forum.forum_order ASC";
  4956. // Select the number of threads of the forums (only the threads that are visible).
  4957. $sql2 = "SELECT
  4958. count(thread_id) AS number_of_threads,
  4959. threads.forum_id
  4960. FROM $table_threads threads
  4961. INNER JOIN $table_item_property item_properties
  4962. ON (threads.thread_id = item_properties.ref AND item_properties.c_id = threads.c_id)
  4963. WHERE
  4964. threads.c_id = $course_id AND
  4965. item_properties.c_id = $course_id AND
  4966. item_properties.visibility = 1 AND
  4967. item_properties.tool='".TOOL_FORUM_THREAD."'
  4968. GROUP BY threads.forum_id";
  4969. // Select the number of posts of the forum (post that are visible and that are in a thread that is visible).
  4970. $sql3 = "SELECT count(post_id) AS number_of_posts, posts.forum_id
  4971. FROM $table_posts posts
  4972. INNER JOIN $table_threads threads
  4973. ON (posts.thread_id = threads.thread_id AND posts.c_id = threads.c_id)
  4974. INNER JOIN $table_item_property item_properties
  4975. ON (threads.thread_id = item_properties.ref AND item_properties.c_id = threads.c_id)
  4976. WHERE
  4977. posts.visible=1 AND
  4978. posts.c_id = $course_id AND
  4979. item_properties.c_id = $course_id AND
  4980. threads.c_id = $course_id AND
  4981. item_properties.visibility = 1 AND
  4982. item_properties.tool='".TOOL_FORUM_THREAD."'
  4983. GROUP BY threads.forum_id";
  4984. // Course Admin
  4985. if (api_is_allowed_to_edit()) {
  4986. // Select all the forum information of all forums (that are not deleted).
  4987. $sql = "SELECT *
  4988. FROM $table_forums forum
  4989. INNER JOIN $table_item_property item_properties
  4990. ON (forum.forum_id = item_properties.ref AND item_properties.c_id = forum.c_id)
  4991. WHERE
  4992. forum.forum_of_group = $groupId AND
  4993. forum.c_id = $course_id AND
  4994. item_properties.c_id = $course_id AND
  4995. item_properties.visibility <> 2 AND
  4996. item_properties.tool = '".TOOL_FORUM."'
  4997. ORDER BY forum_order ASC";
  4998. // Select the number of threads of the forums (only the threads that are not deleted).
  4999. $sql2 = "SELECT count(thread_id) AS number_of_threads, threads.forum_id
  5000. FROM $table_threads threads
  5001. INNER JOIN $table_item_property item_properties
  5002. ON (threads.thread_id=item_properties.ref AND item_properties.c_id = threads.c_id)
  5003. WHERE
  5004. threads.c_id = $course_id AND
  5005. item_properties.c_id = $course_id AND
  5006. item_properties.visibility <> 2 AND
  5007. item_properties.tool='".TOOL_FORUM_THREAD."'
  5008. GROUP BY threads.forum_id";
  5009. // Select the number of posts of the forum.
  5010. $sql3 = "SELECT count(post_id) AS number_of_posts, forum_id
  5011. FROM $table_posts
  5012. WHERE c_id = $course_id
  5013. GROUP BY forum_id";
  5014. }
  5015. // Handling all the forum information.
  5016. $result = Database::query($sql);
  5017. $forum_list = [];
  5018. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5019. $forum_list[$row['forum_id']] = $row;
  5020. }
  5021. // Handling the thread count information.
  5022. $result2 = Database::query($sql2);
  5023. while ($row2 = Database::fetch_array($result2, 'ASSOC')) {
  5024. if (is_array($forum_list)) {
  5025. if (array_key_exists($row2['forum_id'], $forum_list)) {
  5026. $forum_list[$row2['forum_id']]['number_of_threads'] = $row2['number_of_threads'];
  5027. }
  5028. }
  5029. }
  5030. // Handling the post count information.
  5031. $result3 = Database::query($sql3);
  5032. while ($row3 = Database::fetch_array($result3, 'ASSOC')) {
  5033. if (is_array($forum_list)) {
  5034. if (array_key_exists($row3['forum_id'], $forum_list)) {
  5035. // This is needed because sql3 takes also the deleted forums into account.
  5036. $forum_list[$row3['forum_id']]['number_of_posts'] = $row3['number_of_posts'];
  5037. }
  5038. }
  5039. }
  5040. // Finding the last post information
  5041. // (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname).
  5042. if (!empty($forum_list)) {
  5043. foreach ($forum_list as $key => $value) {
  5044. $lastPost = get_last_post_information($key, api_is_allowed_to_edit());
  5045. if ($lastPost) {
  5046. $forum_list[$key]['last_post_id'] = $lastPost['last_post_id'];
  5047. $forum_list[$key]['last_poster_id'] = $lastPost['last_poster_id'];
  5048. $forum_list[$key]['last_post_date'] = $lastPost['last_post_date'];
  5049. $forum_list[$key]['last_poster_name'] = $lastPost['last_poster_name'];
  5050. $forum_list[$key]['last_poster_lastname'] = $lastPost['last_poster_lastname'];
  5051. $forum_list[$key]['last_poster_firstname'] = $lastPost['last_poster_firstname'];
  5052. }
  5053. }
  5054. }
  5055. return $forum_list;
  5056. }
  5057. /**
  5058. * This function stores which users have to be notified of which forums or threads.
  5059. *
  5060. * @param string $content does the user want to be notified about a forum or about a thread
  5061. * @param int $id the id of the forum or thread
  5062. *
  5063. * @return string language variable
  5064. *
  5065. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5066. *
  5067. * @version May 2008, dokeos 1.8.5
  5068. *
  5069. * @since May 2008, dokeos 1.8.5
  5070. */
  5071. function set_notification($content, $id, $add_only = false)
  5072. {
  5073. $_user = api_get_user_info();
  5074. // Database table definition
  5075. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  5076. $course_id = api_get_course_int_id();
  5077. // Which database field do we have to store the id in?
  5078. if ($content == 'forum') {
  5079. $database_field = 'forum_id';
  5080. } else {
  5081. $database_field = 'thread_id';
  5082. }
  5083. // First we check if the notification is already set for this.
  5084. $sql = "SELECT * FROM $table_notification
  5085. WHERE
  5086. c_id = $course_id AND
  5087. $database_field = '".Database::escape_string($id)."' AND
  5088. user_id = '".intval($_user['user_id'])."'";
  5089. $result = Database::query($sql);
  5090. $total = Database::num_rows($result);
  5091. // If the user did not indicate that (s)he wanted to be notified already
  5092. // then we store the notification request (to prevent double notification requests).
  5093. if ($total <= 0) {
  5094. $sql = "INSERT INTO $table_notification (c_id, $database_field, user_id)
  5095. VALUES (".$course_id.", '".Database::escape_string($id)."','".intval($_user['user_id'])."')";
  5096. Database::query($sql);
  5097. Session::erase('forum_notification');
  5098. getNotificationsPerUser(0, true);
  5099. return get_lang('YouWillBeNotifiedOfNewPosts');
  5100. } else {
  5101. if (!$add_only) {
  5102. $sql = "DELETE FROM $table_notification
  5103. WHERE
  5104. c_id = $course_id AND
  5105. $database_field = '".Database::escape_string($id)."' AND
  5106. user_id = '".intval($_user['user_id'])."'";
  5107. Database::query($sql);
  5108. Session::erase('forum_notification');
  5109. getNotificationsPerUser(0, true);
  5110. return get_lang('YouWillNoLongerBeNotifiedOfNewPosts');
  5111. }
  5112. }
  5113. }
  5114. /**
  5115. * This function retrieves all the email adresses of the users who wanted to be notified
  5116. * about a new post in a certain forum or thread.
  5117. *
  5118. * @param string $content does the user want to be notified about a forum or about a thread
  5119. * @param int $id the id of the forum or thread
  5120. *
  5121. * @return array returns
  5122. *
  5123. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5124. *
  5125. * @version May 2008, dokeos 1.8.5
  5126. *
  5127. * @since May 2008, dokeos 1.8.5
  5128. */
  5129. function get_notifications($content, $id)
  5130. {
  5131. // Database table definition
  5132. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  5133. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  5134. $course_id = api_get_course_int_id();
  5135. // Which database field contains the notification?
  5136. if ($content == 'forum') {
  5137. $database_field = 'forum_id';
  5138. } else {
  5139. $database_field = 'thread_id';
  5140. }
  5141. $sql = "SELECT user.user_id, user.firstname, user.lastname, user.email, user.user_id user
  5142. FROM $table_users user, $table_notification notification
  5143. WHERE
  5144. notification.c_id = $course_id AND user.active = 1 AND
  5145. user.user_id = notification.user_id AND
  5146. notification.$database_field= '".Database::escape_string($id)."'";
  5147. $result = Database::query($sql);
  5148. $return = [];
  5149. while ($row = Database::fetch_array($result)) {
  5150. $return['user'.$row['user_id']] = ['email' => $row['email'], 'user_id' => $row['user_id']];
  5151. }
  5152. return $return;
  5153. }
  5154. /**
  5155. * Get all the users who need to receive a notification of a new post (those subscribed to
  5156. * the forum or the thread).
  5157. *
  5158. * @param int $forum_id the id of the forum
  5159. * @param int $thread_id the id of the thread
  5160. * @param int $post_id the id of the post
  5161. *
  5162. * @return false|null
  5163. *
  5164. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5165. *
  5166. * @version May 2008, dokeos 1.8.5
  5167. *
  5168. * @since May 2008, dokeos 1.8.5
  5169. */
  5170. function send_notifications($forum_id = 0, $thread_id = 0, $post_id = 0)
  5171. {
  5172. //$_course = api_get_course_info();
  5173. /*$forumCourseId = api_get_configuration_value('global_forums_course_id');
  5174. if (!empty($forumCourseId)) {
  5175. if ($_course['real_id'] == $forumCourseId) {
  5176. return false;
  5177. }
  5178. }*/
  5179. $forum_id = (int) $forum_id;
  5180. // The content of the mail
  5181. //$thread_link = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum_id.'&thread='.$thread_id;
  5182. // Users who subscribed to the forum
  5183. if ($forum_id != 0) {
  5184. $users_to_be_notified_by_forum = get_notifications('forum', $forum_id);
  5185. } else {
  5186. return false;
  5187. }
  5188. $current_thread = get_thread_information($forum_id, $thread_id);
  5189. //$current_forum = get_forum_information($current_thread['forum_id']);
  5190. //$subject = get_lang('NewForumPost').' - '.$_course['official_code'].' - '.$current_forum['forum_title'].' - '.$current_thread['thread_title'];
  5191. // User who subscribed to the thread
  5192. if ($thread_id != 0) {
  5193. $users_to_be_notified_by_thread = get_notifications('thread', $thread_id);
  5194. }
  5195. $postInfo = [];
  5196. if (!empty($post_id)) {
  5197. $postInfo = get_post_information($post_id);
  5198. }
  5199. // Merging the two
  5200. $users_to_be_notified = array_merge($users_to_be_notified_by_forum, $users_to_be_notified_by_thread);
  5201. $forumInfo = get_forum_information($forum_id);
  5202. if (is_array($users_to_be_notified)) {
  5203. foreach ($users_to_be_notified as $value) {
  5204. $userInfo = api_get_user_info($value['user_id']);
  5205. send_mail($userInfo, $forumInfo, $current_thread, $postInfo);
  5206. }
  5207. }
  5208. }
  5209. /**
  5210. * Get all the notification subscriptions of the user
  5211. * = which forums and which threads does the user wants to be informed of when a new
  5212. * post is added to this thread.
  5213. *
  5214. * @param int $user_id the user_id of a user (default = 0 => the current user)
  5215. * @param bool $force force get the notification subscriptions (even if the information is already in the session
  5216. *
  5217. * @return array
  5218. *
  5219. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5220. *
  5221. * @version May 2008, dokeos 1.8.5
  5222. *
  5223. * @since May 2008, dokeos 1.8.5
  5224. */
  5225. function getNotificationsPerUser($user_id = 0, $force = false, $course_id = 0)
  5226. {
  5227. // Database table definition
  5228. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  5229. $course_id = empty($course_id) ? api_get_course_int_id() : (int) $course_id;
  5230. if (empty($course_id) || $course_id == -1) {
  5231. return null;
  5232. }
  5233. $user_id = empty($user_id) ? api_get_user_id() : (int) $user_id;
  5234. if (!isset($_SESSION['forum_notification']) ||
  5235. $_SESSION['forum_notification']['course'] != $course_id ||
  5236. $force == true
  5237. ) {
  5238. $_SESSION['forum_notification']['course'] = $course_id;
  5239. $sql = "SELECT * FROM $table_notification
  5240. WHERE c_id = $course_id AND user_id='".$user_id."'";
  5241. $result = Database::query($sql);
  5242. while ($row = Database::fetch_array($result)) {
  5243. if (!is_null($row['forum_id'])) {
  5244. $_SESSION['forum_notification']['forum'][] = $row['forum_id'];
  5245. }
  5246. if (!is_null($row['thread_id'])) {
  5247. $_SESSION['forum_notification']['thread'][] = $row['thread_id'];
  5248. }
  5249. }
  5250. }
  5251. }
  5252. /**
  5253. * This function counts the number of post inside a thread.
  5254. *
  5255. * @param int $thread_id
  5256. *
  5257. * @return int the number of post inside a thread
  5258. *
  5259. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  5260. *
  5261. * @version octubre 2008, dokeos 1.8
  5262. */
  5263. function count_number_of_post_in_thread($thread_id)
  5264. {
  5265. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5266. $course_id = api_get_course_int_id();
  5267. if (empty($course_id)) {
  5268. return 0;
  5269. }
  5270. $sql = "SELECT count(*) count FROM $table_posts
  5271. WHERE
  5272. c_id = $course_id AND
  5273. thread_id='".intval($thread_id)."' ";
  5274. $result = Database::query($sql);
  5275. $count = 0;
  5276. if (Database::num_rows($result) > 0) {
  5277. $row = Database::fetch_array($result);
  5278. $count = $row['count'];
  5279. }
  5280. return $count;
  5281. }
  5282. /**
  5283. * This function counts the number of post inside a thread user.
  5284. *
  5285. * @param int $thread_id
  5286. * @param int $user_id
  5287. *
  5288. * @return int the number of post inside a thread user
  5289. */
  5290. function count_number_of_post_for_user_thread($thread_id, $user_id)
  5291. {
  5292. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5293. $course_id = api_get_course_int_id();
  5294. $sql = "SELECT count(iid) as count
  5295. FROM $table_posts
  5296. WHERE c_id = $course_id AND
  5297. thread_id=".intval($thread_id)." AND
  5298. poster_id = ".intval($user_id)." AND visible = 1 ";
  5299. $result = Database::query($sql);
  5300. $count = 0;
  5301. if (Database::num_rows($result) > 0) {
  5302. $count = Database::fetch_array($result);
  5303. $count = $count['count'];
  5304. }
  5305. return $count;
  5306. }
  5307. /**
  5308. * This function retrieves information of statistical.
  5309. *
  5310. * @param int $thread_id
  5311. * @param int $user_id
  5312. * @param int $course_id
  5313. *
  5314. * @return array the information of statistical
  5315. *
  5316. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  5317. *
  5318. * @version oct 2008, dokeos 1.8
  5319. */
  5320. function get_statistical_information($thread_id, $user_id, $course_id)
  5321. {
  5322. $result = [];
  5323. $courseInfo = api_get_course_info_by_id($course_id);
  5324. $result['user_course'] = CourseManager::get_users_count_in_course($courseInfo['code']);
  5325. $result['post'] = count_number_of_post_in_thread($thread_id);
  5326. $result['user_post'] = count_number_of_post_for_user_thread($thread_id, $user_id);
  5327. return $result;
  5328. }
  5329. /**
  5330. * This function return the posts inside a thread from a given user.
  5331. *
  5332. * @param string $course_code
  5333. * @param int $thread_id
  5334. * @param int $user_id
  5335. *
  5336. * @return array posts inside a thread
  5337. *
  5338. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  5339. *
  5340. * @version oct 2008, dokeos 1.8
  5341. */
  5342. function get_thread_user_post($course_code, $thread_id, $user_id)
  5343. {
  5344. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5345. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  5346. $thread_id = intval($thread_id);
  5347. $user_id = intval($user_id);
  5348. $course_info = api_get_user_info($course_code);
  5349. $course_id = $course_info['real_id'];
  5350. if (empty($course_id)) {
  5351. $course_id = api_get_course_int_id();
  5352. }
  5353. $sql = "SELECT * FROM $table_posts posts
  5354. LEFT JOIN $table_users users
  5355. ON posts.poster_id=users.user_id
  5356. WHERE
  5357. posts.c_id = $course_id AND
  5358. posts.thread_id='$thread_id'
  5359. AND posts.poster_id='$user_id'
  5360. ORDER BY posts.post_id ASC";
  5361. $result = Database::query($sql);
  5362. $post_list = [];
  5363. while ($row = Database::fetch_array($result)) {
  5364. $row['status'] = '1';
  5365. $post_list[] = $row;
  5366. $sql = "SELECT * FROM $table_posts posts
  5367. LEFT JOIN $table_users users
  5368. ON (posts.poster_id=users.user_id)
  5369. WHERE
  5370. posts.c_id = $course_id AND
  5371. posts.thread_id='$thread_id'
  5372. AND posts.post_parent_id='".$row['post_id']."'
  5373. ORDER BY posts.post_id ASC";
  5374. $result2 = Database::query($sql);
  5375. while ($row2 = Database::fetch_array($result2)) {
  5376. $row2['status'] = '0';
  5377. $post_list[] = $row2;
  5378. }
  5379. }
  5380. return $post_list;
  5381. }
  5382. /**
  5383. * This function get the name of an thread by id.
  5384. *
  5385. * @param int thread_id
  5386. *
  5387. * @return string
  5388. *
  5389. * @author Christian Fasanando
  5390. * @author Julio Montoya <gugli100@gmail.com> Adding security
  5391. */
  5392. function get_name_thread_by_id($thread_id)
  5393. {
  5394. $t_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  5395. $course_id = api_get_course_int_id();
  5396. $sql = "SELECT thread_title
  5397. FROM $t_forum_thread
  5398. WHERE c_id = $course_id AND thread_id = '".intval($thread_id)."' ";
  5399. $result = Database::query($sql);
  5400. $row = Database::fetch_array($result);
  5401. return $row[0];
  5402. }
  5403. /**
  5404. * This function gets all the post written by an user.
  5405. *
  5406. * @param int $user_id
  5407. * @param string $course_code
  5408. *
  5409. * @return string
  5410. */
  5411. function get_all_post_from_user($user_id, $course_code)
  5412. {
  5413. $j = 0;
  5414. $forums = get_forums('', $course_code);
  5415. krsort($forums);
  5416. $forum_results = '';
  5417. foreach ($forums as $forum) {
  5418. if ($forum['visibility'] == 0) {
  5419. continue;
  5420. }
  5421. if ($j <= 4) {
  5422. $threads = get_threads($forum['forum_id']);
  5423. if (is_array($threads)) {
  5424. $i = 0;
  5425. $hand_forums = '';
  5426. $post_counter = 0;
  5427. foreach ($threads as $thread) {
  5428. if ($thread['visibility'] == 0) {
  5429. continue;
  5430. }
  5431. if ($i <= 4) {
  5432. $post_list = get_thread_user_post_limit(
  5433. $course_code,
  5434. $thread['thread_id'],
  5435. $user_id,
  5436. 1
  5437. );
  5438. $post_counter = count($post_list);
  5439. if (is_array($post_list) && count($post_list) > 0) {
  5440. $hand_forums .= '<div id="social-thread">';
  5441. $hand_forums .= Display::return_icon(
  5442. 'thread.png',
  5443. get_lang('Thread'),
  5444. '',
  5445. ICON_SIZE_MEDIUM
  5446. );
  5447. $hand_forums .= '&nbsp;'.Security::remove_XSS($thread['thread_title'], STUDENT);
  5448. $hand_forums .= '</div>';
  5449. foreach ($post_list as $posts) {
  5450. $hand_forums .= '<div id="social-post">';
  5451. $hand_forums .= '<strong>'.Security::remove_XSS($posts['post_title'], STUDENT).'</strong>';
  5452. $hand_forums .= '<br / >';
  5453. $hand_forums .= Security::remove_XSS($posts['post_text'], STUDENT);
  5454. $hand_forums .= '</div>';
  5455. $hand_forums .= '<br / >';
  5456. }
  5457. }
  5458. }
  5459. $i++;
  5460. }
  5461. $forum_results .= '<div id="social-forum">';
  5462. $forum_results .= '<div class="clear"></div><br />';
  5463. $forum_results .= '<div id="social-forum-title">'.
  5464. Display::return_icon('forum.gif', get_lang('Forum')).'&nbsp;'.Security::remove_XSS($forum['forum_title'], STUDENT).
  5465. '<div style="float:right;margin-top:-35px">
  5466. <a href="../forum/viewforum.php?'.api_get_cidreq_params($course_code).'&forum='.$forum['forum_id'].' " >'.
  5467. get_lang('SeeForum').'
  5468. </a>
  5469. </div></div>';
  5470. $forum_results .= '<br / >';
  5471. if ($post_counter > 0) {
  5472. $forum_results .= $hand_forums;
  5473. }
  5474. $forum_results .= '</div>';
  5475. }
  5476. $j++;
  5477. }
  5478. }
  5479. return $forum_results;
  5480. }
  5481. /**
  5482. * @param string $course_code
  5483. * @param int $thread_id
  5484. * @param int $user_id
  5485. * @param int $limit
  5486. *
  5487. * @return array
  5488. */
  5489. function get_thread_user_post_limit($course_code, $thread_id, $user_id, $limit = 10)
  5490. {
  5491. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5492. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  5493. $course_info = api_get_course_info($course_code);
  5494. $course_id = $course_info['real_id'];
  5495. $sql = "SELECT * FROM $table_posts posts
  5496. LEFT JOIN $table_users users
  5497. ON posts.poster_id=users.user_id
  5498. WHERE
  5499. posts.c_id = $course_id AND
  5500. posts.thread_id='".Database::escape_string($thread_id)."' AND
  5501. posts.poster_id='".Database::escape_string($user_id)."'
  5502. ORDER BY posts.post_id DESC LIMIT $limit ";
  5503. $result = Database::query($sql);
  5504. $post_list = [];
  5505. while ($row = Database::fetch_array($result)) {
  5506. $row['status'] = '1';
  5507. $post_list[] = $row;
  5508. }
  5509. return $post_list;
  5510. }
  5511. /**
  5512. * @param string $userId
  5513. * @param array $courseInfo
  5514. * @param int $sessionId
  5515. *
  5516. * @return array
  5517. */
  5518. function getForumCreatedByUser($userId, $courseInfo, $sessionId)
  5519. {
  5520. if (empty($userId) || empty($courseInfo)) {
  5521. return [];
  5522. }
  5523. $courseId = $courseInfo['real_id'];
  5524. $items = api_get_item_property_list_by_tool_by_user(
  5525. $userId,
  5526. 'forum',
  5527. $courseId,
  5528. $sessionId
  5529. );
  5530. $forumList = [];
  5531. if (!empty($items)) {
  5532. foreach ($items as $forum) {
  5533. $forumInfo = get_forums(
  5534. $forum['ref'],
  5535. $courseInfo['code'],
  5536. true,
  5537. $sessionId
  5538. );
  5539. if (!empty($forumInfo) && isset($forumInfo['forum_title'])) {
  5540. $forumList[] = [
  5541. $forumInfo['forum_title'],
  5542. api_get_local_time($forum['insert_date']),
  5543. api_get_local_time($forum['lastedit_date']),
  5544. ];
  5545. }
  5546. }
  5547. }
  5548. return $forumList;
  5549. }
  5550. /**
  5551. * This function builds an array of all the posts in a given thread
  5552. * where the key of the array is the post_id
  5553. * It also adds an element children to the array which itself is an array
  5554. * that contains all the id's of the first-level children.
  5555. *
  5556. * @return array $rows containing all the information on the posts of a thread
  5557. *
  5558. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  5559. */
  5560. function calculate_children($rows)
  5561. {
  5562. $sorted_rows = [0 => []];
  5563. if (!empty($rows)) {
  5564. foreach ($rows as $row) {
  5565. $rows_with_children[$row['post_id']] = $row;
  5566. $rows_with_children[$row['post_parent_id']]['children'][] = $row['post_id'];
  5567. }
  5568. $rows = $rows_with_children;
  5569. forumRecursiveSort($rows, $sorted_rows);
  5570. unset($sorted_rows[0]);
  5571. }
  5572. return $sorted_rows;
  5573. }
  5574. /**
  5575. * @param $rows
  5576. * @param $threads
  5577. * @param int $seed
  5578. * @param int $indent
  5579. */
  5580. function forumRecursiveSort($rows, &$threads, $seed = 0, $indent = 0)
  5581. {
  5582. if ($seed > 0) {
  5583. $threads[$rows[$seed]['post_id']] = $rows[$seed];
  5584. $threads[$rows[$seed]['post_id']]['indent_cnt'] = $indent;
  5585. $indent++;
  5586. }
  5587. if (isset($rows[$seed]['children'])) {
  5588. foreach ($rows[$seed]['children'] as $child) {
  5589. forumRecursiveSort($rows, $threads, $child, $indent);
  5590. }
  5591. }
  5592. }
  5593. /**
  5594. * Update forum attachment data, used to update comment and post ID.
  5595. *
  5596. * @param $array array (field => value) to update forum attachment row
  5597. * @param $id attach ID to find row to update
  5598. * @param null $courseId course ID to find row to update
  5599. *
  5600. * @return int number of affected rows
  5601. */
  5602. function editAttachedFile($array, $id, $courseId = null)
  5603. {
  5604. // Init variables
  5605. $setString = '';
  5606. $id = intval($id);
  5607. $courseId = intval($courseId);
  5608. if (empty($courseId)) {
  5609. // $courseId can be null, use api method
  5610. $courseId = api_get_course_int_id();
  5611. }
  5612. /*
  5613. * Check if Attachment ID and Course ID are greater than zero
  5614. * and array of field values is not empty
  5615. */
  5616. if ($id > 0 && $courseId > 0 && !empty($array) && is_array($array)) {
  5617. foreach ($array as $key => &$item) {
  5618. $item = Database::escape_string($item);
  5619. $setString .= $key.' = "'.$item.'", ';
  5620. }
  5621. // Delete last comma
  5622. $setString = substr($setString, 0, strlen($setString) - 2);
  5623. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  5624. $sql = "UPDATE $forumAttachmentTable
  5625. SET $setString WHERE c_id = $courseId AND id = $id";
  5626. $result = Database::query($sql);
  5627. if ($result !== false) {
  5628. $affectedRows = Database::affected_rows($result);
  5629. if ($affectedRows > 0) {
  5630. /*
  5631. * If exist in $_SESSION variable, then delete them from it
  5632. * because they would be deprecated
  5633. */
  5634. if (!empty($_SESSION['forum']['upload_file'][$courseId][$id])) {
  5635. unset($_SESSION['forum']['upload_file'][$courseId][$id]);
  5636. }
  5637. }
  5638. return $affectedRows;
  5639. }
  5640. }
  5641. return 0;
  5642. }
  5643. /**
  5644. * Return a table where the attachments will be set.
  5645. *
  5646. * @param int $postId Forum Post ID
  5647. *
  5648. * @return string The Forum Attachments Ajax Table
  5649. */
  5650. function getAttachmentsAjaxTable($postId = 0)
  5651. {
  5652. // Init variables
  5653. $postId = intval($postId);
  5654. $courseId = api_get_course_int_id();
  5655. $attachIds = getAttachmentIdsByPostId($postId, $courseId);
  5656. $fileDataContent = '';
  5657. // Update comment to show if form did not pass validation
  5658. if (!empty($_REQUEST['file_ids']) && is_array($_REQUEST['file_ids'])) {
  5659. // 'file_ids is the name from forum attachment ajax form
  5660. foreach ($_REQUEST['file_ids'] as $key => $attachId) {
  5661. if (!empty($_SESSION['forum']['upload_file'][$courseId][$attachId]) &&
  5662. is_array($_SESSION['forum']['upload_file'][$courseId][$attachId])
  5663. ) {
  5664. // If exist forum attachment then update into $_SESSION data
  5665. $_SESSION['forum']['upload_file'][$courseId][$attachId]['comment'] = $_POST['file_comments'][$key];
  5666. }
  5667. }
  5668. }
  5669. // Get data to fill into attachment files table
  5670. if (!empty($_SESSION['forum']['upload_file'][$courseId]) &&
  5671. is_array($_SESSION['forum']['upload_file'][$courseId])
  5672. ) {
  5673. $uploadedFiles = $_SESSION['forum']['upload_file'][$courseId];
  5674. foreach ($uploadedFiles as $k => $uploadedFile) {
  5675. if (!empty($uploadedFile) && in_array($uploadedFile['id'], $attachIds)) {
  5676. // Buil html table including an input with attachmentID
  5677. $fileDataContent .= '<tr id="'.$uploadedFile['id'].'" ><td>'.$uploadedFile['name'].'</td><td>'.$uploadedFile['size'].'</td><td>&nbsp;'.$uploadedFile['result'].
  5678. ' </td><td> <input style="width:90%;" type="text" value="'.$uploadedFile['comment'].'" name="file_comments[]"> </td><td>'.
  5679. $uploadedFile['delete'].'</td>'.
  5680. '<input type="hidden" value="'.$uploadedFile['id'].'" name="file_ids[]">'.'</tr>';
  5681. } else {
  5682. /*
  5683. * If attachment data is empty, then delete it from $_SESSION
  5684. * because could generate and empty row into html table
  5685. */
  5686. unset($_SESSION['forum']['upload_file'][$courseId][$k]);
  5687. }
  5688. }
  5689. }
  5690. $style = empty($fileDataContent) ? 'display: none;' : '';
  5691. // Forum attachment Ajax table
  5692. $fileData = '
  5693. <div class="control-group " style="'.$style.'">
  5694. <label class="control-label">'.get_lang('AttachmentList').'</label>
  5695. <div class="controls">
  5696. <table id="attachmentFileList" class="files data_table span10">
  5697. <tr>
  5698. <th>'.get_lang('FileName').'</th>
  5699. <th>'.get_lang('Size').'</th>
  5700. <th>'.get_lang('Status').'</th>
  5701. <th>'.get_lang('Comment').'</th>
  5702. <th>'.get_lang('Delete').'</th>
  5703. </tr>
  5704. '.$fileDataContent.'
  5705. </table>
  5706. </div>
  5707. </div>';
  5708. return $fileData;
  5709. }
  5710. /**
  5711. * Return an array of prepared attachment data to build forum attachment table
  5712. * Also, save this array into $_SESSION to do available the attachment data.
  5713. *
  5714. * @param int $forumId
  5715. * @param int $threadId
  5716. * @param int $postId
  5717. * @param int $attachId
  5718. * @param int $courseId
  5719. *
  5720. * @return array
  5721. */
  5722. function getAttachedFiles(
  5723. $forumId,
  5724. $threadId,
  5725. $postId = 0,
  5726. $attachId = 0,
  5727. $courseId = 0
  5728. ) {
  5729. $forumId = intval($forumId);
  5730. $courseId = intval($courseId);
  5731. $attachId = intval($attachId);
  5732. $postId = intval($postId);
  5733. $threadId = !empty($threadId) ? intval($threadId) : isset($_REQUEST['thread']) ? intval($_REQUEST['thread']) : '';
  5734. if (empty($courseId)) {
  5735. // $courseId can be null, use api method
  5736. $courseId = api_get_course_int_id();
  5737. }
  5738. if (empty($forumId)) {
  5739. if (!empty($_REQUEST['forum'])) {
  5740. $forumId = intval($_REQUEST['forum']);
  5741. } else {
  5742. // if forum ID is empty, cannot generate delete url
  5743. return [];
  5744. }
  5745. }
  5746. // Check if exist at least one of them to filter forum attachment select query
  5747. if (empty($postId) && empty($attachId)) {
  5748. return [];
  5749. } elseif (empty($postId)) {
  5750. $filter = "AND iid = $attachId";
  5751. } elseif (empty($attachId)) {
  5752. $filter = "AND post_id = $postId";
  5753. } else {
  5754. $filter = "AND post_id = $postId AND iid = $attachId";
  5755. }
  5756. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  5757. $sql = "SELECT iid, comment, filename, path, size
  5758. FROM $forumAttachmentTable
  5759. WHERE c_id = $courseId $filter";
  5760. $result = Database::query($sql);
  5761. $json = [];
  5762. if ($result !== false && Database::num_rows($result) > 0) {
  5763. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5764. // name contains an URL to download attachment file and its filename
  5765. $json['name'] = Display::url(
  5766. api_htmlentities($row['filename']),
  5767. api_get_path(WEB_CODE_PATH).'forum/download.php?file='.$row['path'].'&'.api_get_cidreq(),
  5768. ['target' => '_blank', 'class' => 'attachFilename']
  5769. );
  5770. $json['id'] = $row['iid'];
  5771. $json['comment'] = $row['comment'];
  5772. // Format file size
  5773. $json['size'] = format_file_size($row['size']);
  5774. // Check if $row is consistent
  5775. if (!empty($row) && is_array($row)) {
  5776. // Set result as success and bring delete URL
  5777. $json['result'] = Display::return_icon('accept.png', get_lang('Uploaded'));
  5778. $url = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&action=delete_attach&forum='.$forumId.'&thread='.$threadId.'&id_attach='.$row['iid'];
  5779. $json['delete'] = Display::url(
  5780. Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL),
  5781. $url,
  5782. ['class' => 'deleteLink']
  5783. );
  5784. } else {
  5785. // If not, set an exclamation result
  5786. $json['result'] = Display::return_icon('exclamation.png', get_lang('Error'));
  5787. }
  5788. // Store array data into $_SESSION
  5789. $_SESSION['forum']['upload_file'][$courseId][$json['id']] = $json;
  5790. }
  5791. }
  5792. return $json;
  5793. }
  5794. /**
  5795. * Clear forum attachment data stored in $_SESSION,
  5796. * If is not defined post, it will clear all forum attachment data from course.
  5797. *
  5798. * @param int $postId -1 : Clear all attachments from course stored in $_SESSION
  5799. * 0 : Clear attachments from course, except from temporal post "0"
  5800. * but without delete them from file system and database
  5801. * Other values : Clear attachments from course except specified post
  5802. * and delete them from file system and database
  5803. * @param int $courseId : Course ID, if it is null, will use api_get_course_int_id()
  5804. *
  5805. * @return array
  5806. */
  5807. function clearAttachedFiles($postId = null, $courseId = null)
  5808. {
  5809. // Init variables
  5810. $courseId = intval($courseId);
  5811. $postId = intval($postId);
  5812. $array = [];
  5813. if (empty($courseId)) {
  5814. // $courseId can be null, use api method
  5815. $courseId = api_get_course_int_id();
  5816. }
  5817. if ($postId === -1) {
  5818. // If post ID is -1 then delete course's attachment data from $_SESSION
  5819. if (!empty($_SESSION['forum']['upload_file'][$courseId])) {
  5820. $array = array_keys($_SESSION['forum']['upload_file'][$courseId]);
  5821. unset($_SESSION['forum']['upload_file'][$courseId]);
  5822. }
  5823. } else {
  5824. $attachIds = getAttachmentIdsByPostId($postId, $courseId);
  5825. if (!empty($_SESSION['forum']['upload_file'][$courseId]) &&
  5826. is_array($_SESSION['forum']['upload_file'][$courseId])) {
  5827. foreach ($_SESSION['forum']['upload_file'][$courseId] as $attachId => $attach) {
  5828. if (!in_array($attachId, $attachIds)) {
  5829. // If attach ID is not into specified post, delete attachment
  5830. // Save deleted attachment ID
  5831. $array[] = $attachId;
  5832. if ($postId !== 0) {
  5833. // Post 0 is temporal, delete them from file system and DB
  5834. delete_attachment(0, $attachId);
  5835. }
  5836. // Delete attachment data from $_SESSION
  5837. unset($_SESSION['forum']['upload_file'][$courseId][$attachId]);
  5838. }
  5839. }
  5840. }
  5841. }
  5842. return $array;
  5843. }
  5844. /**
  5845. * Returns an array of forum attachment ids into a course and forum post.
  5846. *
  5847. * @param int $postId
  5848. * @param int $courseId
  5849. *
  5850. * @return array
  5851. */
  5852. function getAttachmentIdsByPostId($postId, $courseId = 0)
  5853. {
  5854. $array = [];
  5855. $courseId = intval($courseId);
  5856. $postId = intval($postId);
  5857. if (empty($courseId)) {
  5858. // $courseId can be null, use api method
  5859. $courseId = api_get_course_int_id();
  5860. }
  5861. if ($courseId > 0) {
  5862. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  5863. $sql = "SELECT id FROM $forumAttachmentTable
  5864. WHERE c_id = $courseId AND post_id = $postId";
  5865. $result = Database::query($sql);
  5866. if ($result !== false && Database::num_rows($result) > 0) {
  5867. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5868. $array[] = $row['id'];
  5869. }
  5870. }
  5871. }
  5872. return $array;
  5873. }
  5874. /**
  5875. * Check if the forum category exists looking for its title.
  5876. *
  5877. * @param string $title The forum category title
  5878. * @param int $courseId The course ID
  5879. * @param int $sessionId Optional. The session ID
  5880. *
  5881. * @return bool
  5882. */
  5883. function getForumCategoryByTitle($title, $courseId, $sessionId = 0)
  5884. {
  5885. $sessionId = intval($sessionId);
  5886. $forumCategoryTable = Database::get_course_table(TABLE_FORUM_CATEGORY);
  5887. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5888. $fakeFrom = "$forumCategoryTable fc
  5889. INNER JOIN $itemProperty ip ";
  5890. if ($sessionId === 0) {
  5891. $fakeFrom .= "
  5892. ON (
  5893. fc.cat_id = ip.ref AND fc.c_id = ip.c_id AND (fc.session_id = ip.session_id OR ip.session_id IS NULL)
  5894. )
  5895. ";
  5896. } else {
  5897. $fakeFrom .= "
  5898. ON (
  5899. fc.cat_id = ip.ref AND fc.c_id = ip.c_id AND fc.session_id = ip.session_id
  5900. )
  5901. ";
  5902. }
  5903. $resultData = Database::select(
  5904. 'fc.*',
  5905. $fakeFrom,
  5906. [
  5907. 'where' => [
  5908. 'ip.visibility != ? AND ' => 2,
  5909. 'ip.tool = ? AND ' => TOOL_FORUM_CATEGORY,
  5910. 'fc.session_id = ? AND ' => $sessionId,
  5911. 'fc.cat_title = ? AND ' => $title,
  5912. 'fc.c_id = ?' => intval($courseId),
  5913. ],
  5914. ],
  5915. 'first'
  5916. );
  5917. if (empty($resultData)) {
  5918. return false;
  5919. }
  5920. return $resultData;
  5921. }
  5922. /**
  5923. * @param array $current_forum
  5924. * @param array $row
  5925. * @param bool $addWrapper
  5926. *
  5927. * @return string
  5928. */
  5929. function getPostStatus($current_forum, $row, $addWrapper = true)
  5930. {
  5931. $statusIcon = '';
  5932. if ($current_forum['moderated']) {
  5933. if ($addWrapper) {
  5934. $statusIcon = '<br /><br /><span id="status_post_'.$row['iid'].'">';
  5935. }
  5936. $row['status'] = empty($row['status']) ? 2 : $row['status'];
  5937. $addUrl = false;
  5938. $showStatus = false;
  5939. if (api_is_allowed_to_edit(false, true)) {
  5940. $addUrl = true;
  5941. } else {
  5942. if ($row['user_id'] == api_get_user_id()) {
  5943. $showStatus = true;
  5944. }
  5945. }
  5946. $label = '';
  5947. $icon = '';
  5948. $buttonType = '';
  5949. switch ($row['status']) {
  5950. case CForumPost::STATUS_VALIDATED:
  5951. $label = get_lang('Validated');
  5952. $icon = 'check-circle';
  5953. $buttonType = 'success';
  5954. break;
  5955. case CForumPost::STATUS_WAITING_MODERATION:
  5956. $label = get_lang('WaitingModeration');
  5957. $icon = 'warning';
  5958. $buttonType = 'warning';
  5959. break;
  5960. case CForumPost::STATUS_REJECTED:
  5961. $label = get_lang('Rejected');
  5962. $icon = 'minus-circle';
  5963. $buttonType = 'danger';
  5964. break;
  5965. }
  5966. if ($addUrl) {
  5967. $statusIcon .= Display::toolbarButton(
  5968. $label.'&nbsp;',
  5969. 'javascript:void(0)',
  5970. $icon,
  5971. $buttonType,
  5972. ['class' => 'change_post_status']
  5973. );
  5974. } else {
  5975. if ($showStatus) {
  5976. $statusIcon .= Display::label(
  5977. Display::returnFontAwesomeIcon($icon).$label,
  5978. $buttonType
  5979. );
  5980. }
  5981. }
  5982. if ($addWrapper) {
  5983. $statusIcon .= '</span>';
  5984. }
  5985. }
  5986. return $statusIcon;
  5987. }
  5988. /**
  5989. * @param array $forumInfo
  5990. * @param int $threadId
  5991. * @param int $status
  5992. *
  5993. * @return mixed
  5994. */
  5995. function getCountPostsWithStatus($status, $forumInfo, $threadId = null)
  5996. {
  5997. $em = Database::getManager();
  5998. $criteria = Criteria::create();
  5999. $criteria
  6000. ->where(Criteria::expr()->eq('status', $status))
  6001. ->andWhere(Criteria::expr()->eq('cId', $forumInfo['c_id']))
  6002. ->andWhere(Criteria::expr()->eq('visible', 1))
  6003. ;
  6004. if (!empty($threadId)) {
  6005. $criteria->andWhere(Criteria::expr()->eq('threadId', $threadId));
  6006. }
  6007. $qb = $em->getRepository('ChamiloCourseBundle:CForumPost')->createQueryBuilder('p');
  6008. $qb->select('count(p.iid)')
  6009. ->addCriteria($criteria);
  6010. return $qb->getQuery()->getSingleScalarResult();
  6011. }
  6012. /**
  6013. * @param array $forum
  6014. * @param array $post
  6015. *
  6016. * @return bool
  6017. */
  6018. function postIsEditableByStudent($forum, $post)
  6019. {
  6020. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  6021. return true;
  6022. }
  6023. if ($forum['moderated'] == 1) {
  6024. if (is_null($post['status'])) {
  6025. return true;
  6026. } else {
  6027. return in_array(
  6028. $post['status'],
  6029. [
  6030. CForumPost::STATUS_WAITING_MODERATION,
  6031. CForumPost::STATUS_REJECTED,
  6032. ]
  6033. );
  6034. }
  6035. } else {
  6036. return true;
  6037. }
  6038. }
  6039. /**
  6040. * @param int $postId
  6041. *
  6042. * @return bool
  6043. */
  6044. function savePostRevision($postId)
  6045. {
  6046. $postData = get_post_information($postId);
  6047. if (empty($postData)) {
  6048. return false;
  6049. }
  6050. $userId = api_get_user_id();
  6051. if ($postData['poster_id'] != $userId) {
  6052. return false;
  6053. }
  6054. $status = (int) !postNeedsRevision($postId);
  6055. $extraFieldValue = new ExtraFieldValue('forum_post');
  6056. $params = [
  6057. 'item_id' => $postId,
  6058. 'extra_ask_for_revision' => ['extra_ask_for_revision' => $status],
  6059. ];
  6060. if (empty($status)) {
  6061. unset($params['extra_ask_for_revision']);
  6062. }
  6063. $extraFieldValue->saveFieldValues(
  6064. $params,
  6065. true,
  6066. false,
  6067. ['ask_for_revision']
  6068. );
  6069. }
  6070. /**
  6071. * @param int $postId
  6072. *
  6073. * @return string
  6074. */
  6075. function getPostRevision($postId)
  6076. {
  6077. $extraFieldValue = new ExtraFieldValue('forum_post');
  6078. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6079. $postId,
  6080. 'revision_language'
  6081. );
  6082. $revision = '';
  6083. if ($value && isset($value['value'])) {
  6084. $revision = $value['value'];
  6085. }
  6086. return $revision;
  6087. }
  6088. /**
  6089. * @param int $postId
  6090. *
  6091. * @return bool
  6092. */
  6093. function postNeedsRevision($postId)
  6094. {
  6095. $extraFieldValue = new ExtraFieldValue('forum_post');
  6096. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6097. $postId,
  6098. 'ask_for_revision'
  6099. );
  6100. $hasRevision = false;
  6101. if ($value && isset($value['value'])) {
  6102. return $value['value'] == 1;
  6103. }
  6104. return $hasRevision;
  6105. }
  6106. /**
  6107. * @param int $postId
  6108. * @param array $threadInfo
  6109. *
  6110. * @return string
  6111. */
  6112. function getAskRevisionButton($postId, $threadInfo)
  6113. {
  6114. if (api_get_configuration_value('allow_forum_post_revisions') === false) {
  6115. return '';
  6116. }
  6117. $postId = (int) $postId;
  6118. $status = 'btn-default';
  6119. if (postNeedsRevision($postId)) {
  6120. $status = 'btn-success';
  6121. }
  6122. return Display::url(
  6123. get_lang('AskRevision'),
  6124. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.
  6125. api_get_cidreq().'&action=ask_revision&post_id='.$postId.'&forum='.$threadInfo['forum_id'].'&thread='.$threadInfo['thread_id'],
  6126. ['class' => "btn $status", 'title' => get_lang('AskRevision')]
  6127. );
  6128. }
  6129. /**
  6130. * @param int $postId
  6131. * @param array $threadInfo
  6132. *
  6133. * @return string
  6134. */
  6135. function giveRevisionButton($postId, $threadInfo)
  6136. {
  6137. $postId = (int) $postId;
  6138. return Display::toolbarButton(
  6139. get_lang('GiveRevision'),
  6140. api_get_path(WEB_CODE_PATH).'forum/reply.php?'.api_get_cidreq().'&'.http_build_query(
  6141. [
  6142. 'forum' => $threadInfo['forum_id'],
  6143. 'thread' => $threadInfo['thread_id'],
  6144. 'post' => $postId = (int) $postId,
  6145. 'action' => 'replymessage',
  6146. 'give_revision' => 1,
  6147. ]
  6148. ),
  6149. 'reply',
  6150. 'primary',
  6151. ['id' => "reply-to-post-{$postId}"]
  6152. );
  6153. }
  6154. /**
  6155. * @param int $postId
  6156. * @param array $threadInfo
  6157. *
  6158. * @return string
  6159. */
  6160. function getReportButton($postId, $threadInfo)
  6161. {
  6162. $postId = (int) $postId;
  6163. return Display::url(
  6164. Display::returnFontAwesomeIcon('flag'),
  6165. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.
  6166. api_get_cidreq().'&action=report&post_id='.$postId.'&forum='.$threadInfo['forum_id'].'&thread='.$threadInfo['thread_id'],
  6167. ['class' => 'btn btn-danger', 'title' => get_lang('Report')]
  6168. );
  6169. }
  6170. /**
  6171. * @return bool
  6172. */
  6173. function reportAvailable()
  6174. {
  6175. $extraFieldValue = new ExtraFieldValue('course');
  6176. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6177. api_get_course_int_id(),
  6178. 'allow_forum_report_button'
  6179. );
  6180. $allowReport = false;
  6181. if ($value && isset($value['value']) && $value['value'] == 1) {
  6182. $allowReport = true;
  6183. }
  6184. return $allowReport;
  6185. }
  6186. /**
  6187. * @return array
  6188. */
  6189. function getReportRecipients()
  6190. {
  6191. $extraFieldValue = new ExtraFieldValue('course');
  6192. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6193. api_get_course_int_id(),
  6194. 'forum_report_recipients'
  6195. );
  6196. $users = [];
  6197. if ($value && isset($value['value'])) {
  6198. $usersType = explode(';', $value['value']);
  6199. foreach ($usersType as $type) {
  6200. switch ($type) {
  6201. case 'teachers':
  6202. $teachers = CourseManager::get_teacher_list_from_course_code(api_get_course_id());
  6203. if (!empty($teachers)) {
  6204. $users = array_merge($users, array_column($teachers, 'user_id'));
  6205. }
  6206. break;
  6207. case 'admins':
  6208. $admins = UserManager::get_all_administrators();
  6209. if (!empty($admins)) {
  6210. $users = array_merge($users, array_column($admins, 'user_id'));
  6211. }
  6212. break;
  6213. case 'community_managers':
  6214. $managers = api_get_configuration_value('community_managers_user_list');
  6215. if (!empty($managers) && isset($managers['users'])) {
  6216. $users = array_merge($users, $managers['users']);
  6217. }
  6218. break;
  6219. }
  6220. }
  6221. $users = array_unique(array_filter($users));
  6222. }
  6223. return $users;
  6224. }
  6225. /**
  6226. * @param int $postId
  6227. * @param array $forumInfo
  6228. * @param array $threadInfo
  6229. *
  6230. * @return bool
  6231. */
  6232. function reportPost($postId, $forumInfo, $threadInfo)
  6233. {
  6234. if (!reportAvailable()) {
  6235. return false;
  6236. }
  6237. if (empty($forumInfo) || empty($threadInfo)) {
  6238. return false;
  6239. }
  6240. $postId = (int) $postId;
  6241. $postData = get_post_information($postId);
  6242. $currentUser = api_get_user_info();
  6243. if (!empty($postData)) {
  6244. $users = getReportRecipients();
  6245. if (!empty($users)) {
  6246. $url = api_get_path(WEB_CODE_PATH).
  6247. 'forum/viewthread.php?forum='.$threadInfo['forum_id'].'&thread='.$threadInfo['thread_id'].'&'.api_get_cidreq().'&post_id='.$postId.'#post_id_'.$postId;
  6248. $postLink = Display::url(
  6249. $postData['post_title'],
  6250. $url
  6251. );
  6252. $subject = get_lang('ForumPostReported');
  6253. $content = sprintf(
  6254. get_lang('UserXReportedPostXInForumX'),
  6255. $currentUser['complete_name'],
  6256. $postLink,
  6257. $forumInfo['forum_title']
  6258. );
  6259. foreach ($users as $userId) {
  6260. MessageManager::send_message_simple($userId, $subject, $content);
  6261. }
  6262. }
  6263. }
  6264. }