forumfunction.inc.php 245 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937
  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 = '',
  1532. $course_code = '',
  1533. $includeGroupsForum = true,
  1534. $sessionId = 0
  1535. ) {
  1536. $course_info = api_get_course_info($course_code);
  1537. $table_forums = Database::get_course_table(TABLE_FORUM);
  1538. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1539. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1540. // Condition for the session
  1541. $session_id = intval($sessionId) ?: api_get_session_id();
  1542. $sessionIdLink = $session_id === 0 ? '' : ' AND threads.session_id = item_properties.session_id';
  1543. $condition_session = api_get_session_condition(
  1544. $session_id,
  1545. true,
  1546. false,
  1547. 'item_properties.session_id'
  1548. );
  1549. $course_id = $course_info['real_id'];
  1550. $forum_list = [];
  1551. $includeGroupsForumSelect = '';
  1552. if (!$includeGroupsForum) {
  1553. $includeGroupsForumSelect = " AND (forum_of_group = 0 OR forum_of_group IS NULL) ";
  1554. }
  1555. if ($id == '') {
  1556. // Student
  1557. // Select all the forum information of all forums (that are visible to students).
  1558. $sql = "SELECT item_properties.*, forum.*
  1559. FROM $table_forums forum
  1560. INNER JOIN $table_item_property item_properties
  1561. ON (
  1562. forum.forum_id = item_properties.ref AND
  1563. forum.c_id = item_properties.c_id
  1564. )
  1565. WHERE
  1566. item_properties.visibility = 1 AND
  1567. item_properties.tool = '".TOOL_FORUM."'
  1568. $condition_session AND
  1569. forum.c_id = $course_id AND
  1570. item_properties.c_id = $course_id
  1571. $includeGroupsForumSelect
  1572. ORDER BY forum.forum_order ASC";
  1573. // Select the number of threads of the forums (only the threads that are visible).
  1574. $sql2 = "SELECT count(*) AS number_of_threads, threads.forum_id
  1575. FROM $table_threads threads
  1576. INNER JOIN $table_item_property item_properties
  1577. ON (
  1578. threads.thread_id = item_properties.ref AND
  1579. threads.c_id = item_properties.c_id
  1580. $sessionIdLink
  1581. )
  1582. WHERE
  1583. item_properties.visibility=1 AND
  1584. item_properties.tool='".TOOL_FORUM_THREAD."' AND
  1585. threads.c_id = $course_id AND
  1586. item_properties.c_id = $course_id
  1587. GROUP BY threads.forum_id";
  1588. // Course Admin
  1589. if (api_is_allowed_to_edit()) {
  1590. // Select all the forum information of all forums (that are not deleted).
  1591. $sql = "SELECT item_properties.*, forum.*
  1592. FROM $table_forums forum
  1593. INNER JOIN $table_item_property item_properties
  1594. ON (
  1595. forum.forum_id = item_properties.ref AND
  1596. forum.c_id = item_properties.c_id
  1597. )
  1598. WHERE
  1599. item_properties.visibility <> 2 AND
  1600. item_properties.tool = '".TOOL_FORUM."'
  1601. $condition_session AND
  1602. forum.c_id = $course_id AND
  1603. item_properties.c_id = $course_id
  1604. $includeGroupsForumSelect
  1605. ORDER BY forum_order ASC";
  1606. // Select the number of threads of the forums (only the threads that are not deleted).
  1607. $sql2 = "SELECT count(*) AS number_of_threads, threads.forum_id
  1608. FROM $table_threads threads
  1609. INNER JOIN $table_item_property item_properties
  1610. ON (
  1611. threads.thread_id = item_properties.ref AND
  1612. threads.c_id = item_properties.c_id
  1613. $sessionIdLink
  1614. )
  1615. WHERE
  1616. item_properties.visibility<>2 AND
  1617. item_properties.tool='".TOOL_FORUM_THREAD."' AND
  1618. threads.c_id = $course_id AND
  1619. item_properties.c_id = $course_id
  1620. GROUP BY threads.forum_id";
  1621. }
  1622. } else {
  1623. // GETTING ONE SPECIFIC FORUM
  1624. /* We could do the splitup into student and course admin also but we want
  1625. to have as much as information about a certain forum as possible
  1626. so we do not take too much information into account. This function
  1627. (or this section of the function) is namely used to fill the forms
  1628. when editing a forum (and for the moment it is the only place where
  1629. we use this part of the function) */
  1630. // Select all the forum information of the given forum (that is not deleted).
  1631. $sql = "SELECT * FROM $table_item_property item_properties
  1632. INNER JOIN $table_forums forum
  1633. ON (forum.forum_id = item_properties.ref AND forum.c_id = item_properties.c_id)
  1634. WHERE
  1635. forum.forum_id = ".intval($id)." AND
  1636. forum.c_id = $course_id AND
  1637. item_properties.visibility != 2 AND
  1638. item_properties.tool = '".TOOL_FORUM."'
  1639. ORDER BY forum_order ASC";
  1640. // Select the number of threads of the forum.
  1641. $sql2 = "SELECT count(*) AS number_of_threads, forum_id
  1642. FROM $table_threads
  1643. WHERE
  1644. forum_id = ".intval($id)."
  1645. GROUP BY forum_id";
  1646. }
  1647. // Handling all the forum information.
  1648. $result = Database::query($sql);
  1649. while ($row = Database::fetch_assoc($result)) {
  1650. if ($id == '') {
  1651. $forum_list[$row['forum_id']] = $row;
  1652. } else {
  1653. $forum_list = $row;
  1654. }
  1655. }
  1656. // Handling the thread count information.
  1657. $result2 = Database::query($sql2);
  1658. while ($row2 = Database::fetch_array($result2)) {
  1659. if ($id == '') {
  1660. $forum_list[$row2['forum_id']]['number_of_threads'] = $row2['number_of_threads'];
  1661. } else {
  1662. $forum_list['number_of_threads'] = $row2['number_of_threads'];
  1663. }
  1664. }
  1665. /* Finding the last post information
  1666. (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)*/
  1667. if ($id == '') {
  1668. if (is_array($forum_list)) {
  1669. foreach ($forum_list as $key => $value) {
  1670. $last_post_info_of_forum = get_last_post_information(
  1671. $key,
  1672. api_is_allowed_to_edit(),
  1673. $course_id
  1674. );
  1675. if ($last_post_info_of_forum) {
  1676. $forum_list[$key]['last_post_id'] = $last_post_info_of_forum['last_post_id'];
  1677. $forum_list[$key]['last_poster_id'] = $last_post_info_of_forum['last_poster_id'];
  1678. $forum_list[$key]['last_post_date'] = $last_post_info_of_forum['last_post_date'];
  1679. $forum_list[$key]['last_poster_name'] = $last_post_info_of_forum['last_poster_name'];
  1680. $forum_list[$key]['last_poster_lastname'] = $last_post_info_of_forum['last_poster_lastname'];
  1681. $forum_list[$key]['last_poster_firstname'] = $last_post_info_of_forum['last_poster_firstname'];
  1682. $forum_list[$key]['last_post_title'] = $last_post_info_of_forum['last_post_title'];
  1683. $forum_list[$key]['last_post_text'] = $last_post_info_of_forum['last_post_text'];
  1684. }
  1685. }
  1686. } else {
  1687. $forum_list = [];
  1688. }
  1689. } else {
  1690. $last_post_info_of_forum = get_last_post_information(
  1691. $id,
  1692. api_is_allowed_to_edit(),
  1693. $course_id
  1694. );
  1695. if ($last_post_info_of_forum) {
  1696. $forum_list['last_post_id'] = $last_post_info_of_forum['last_post_id'];
  1697. $forum_list['last_poster_id'] = $last_post_info_of_forum['last_poster_id'];
  1698. $forum_list['last_post_date'] = $last_post_info_of_forum['last_post_date'];
  1699. $forum_list['last_poster_name'] = $last_post_info_of_forum['last_poster_name'];
  1700. $forum_list['last_poster_lastname'] = $last_post_info_of_forum['last_poster_lastname'];
  1701. $forum_list['last_poster_firstname'] = $last_post_info_of_forum['last_poster_firstname'];
  1702. $forum_list['last_post_title'] = $last_post_info_of_forum['last_post_title'];
  1703. $forum_list['last_post_text'] = $last_post_info_of_forum['last_post_text'];
  1704. }
  1705. }
  1706. return $forum_list;
  1707. }
  1708. /**
  1709. * @param int $course_id
  1710. * @param int $thread_id
  1711. * @param int $forum_id
  1712. * @param bool $show_visible
  1713. *
  1714. * @return array|bool
  1715. */
  1716. function get_last_post_by_thread($course_id, $thread_id, $forum_id, $show_visible = true)
  1717. {
  1718. if (empty($thread_id) || empty($forum_id) || empty($course_id)) {
  1719. return false;
  1720. }
  1721. $thread_id = intval($thread_id);
  1722. $forum_id = intval($forum_id);
  1723. $course_id = intval($course_id);
  1724. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  1725. $sql = "SELECT * FROM $table_posts
  1726. WHERE
  1727. c_id = $course_id AND
  1728. thread_id = $thread_id AND
  1729. forum_id = $forum_id";
  1730. if ($show_visible == false) {
  1731. $sql .= " AND visible = 1 ";
  1732. }
  1733. $sql .= " ORDER BY post_id DESC LIMIT 1";
  1734. $result = Database::query($sql);
  1735. if (Database::num_rows($result)) {
  1736. return Database::fetch_array($result, 'ASSOC');
  1737. } else {
  1738. return false;
  1739. }
  1740. }
  1741. /**
  1742. * This function gets all the last post information of a certain forum.
  1743. *
  1744. * @param int $forum_id the id of the forum we want to know the last post information of
  1745. * @param bool $show_invisibles
  1746. * @param string course db name
  1747. * @param int $sessionId Optional. The session id
  1748. *
  1749. * @return array containing all the information about the last post
  1750. * (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname)
  1751. *
  1752. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1753. *
  1754. * @version february 2006, dokeos 1.8
  1755. */
  1756. function get_last_post_information($forum_id, $show_invisibles = false, $course_id = null, $sessionId = 0)
  1757. {
  1758. if (!isset($course_id)) {
  1759. $course_id = api_get_course_int_id();
  1760. } else {
  1761. $course_id = intval($course_id);
  1762. }
  1763. $sessionId = $sessionId ? intval($sessionId) : api_get_session_id();
  1764. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  1765. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1766. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  1767. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1768. $forum_id = intval($forum_id);
  1769. $return_array = [];
  1770. // First get the threads to make sure there is no inconsistency in the
  1771. // database between forum and thread
  1772. $sql = "SELECT thread_id FROM $table_threads
  1773. WHERE
  1774. forum_id = $forum_id AND
  1775. c_id = $course_id AND
  1776. session_id = $sessionId";
  1777. $result = Database::query($sql);
  1778. if (Database::num_rows($result) == 0) {
  1779. // If there are no threads in this forum, then there are no posts
  1780. return [];
  1781. }
  1782. $threads = [];
  1783. while ($row = Database::fetch_row($result)) {
  1784. $threads[] = $row[0];
  1785. }
  1786. $threadsList = implode(',', $threads);
  1787. // Now get the posts that are linked to these threads
  1788. $sql = "SELECT
  1789. post.post_id,
  1790. post.forum_id,
  1791. post.poster_id,
  1792. post.poster_name,
  1793. post.post_date,
  1794. users.lastname,
  1795. users.firstname,
  1796. post.visible,
  1797. thread_properties.visibility AS thread_visibility,
  1798. forum_properties.visibility AS forum_visibility,
  1799. post.post_title,
  1800. post.post_text
  1801. FROM
  1802. $table_posts post,
  1803. $table_users users,
  1804. $table_item_property thread_properties,
  1805. $table_item_property forum_properties
  1806. WHERE
  1807. post.forum_id = $forum_id
  1808. AND post.thread_id IN ($threadsList)
  1809. AND post.poster_id = users.user_id
  1810. AND post.thread_id = thread_properties.ref
  1811. AND thread_properties.tool='".TOOL_FORUM_THREAD."'
  1812. AND post.forum_id=forum_properties.ref
  1813. AND forum_properties.tool='".TOOL_FORUM."'
  1814. AND post.c_id = $course_id AND
  1815. thread_properties.c_id = $course_id AND
  1816. forum_properties.c_id = $course_id
  1817. ORDER BY post.post_id DESC";
  1818. $result = Database::query($sql);
  1819. if ($show_invisibles) {
  1820. $row = Database::fetch_array($result);
  1821. $return_array['last_post_id'] = $row['post_id'];
  1822. $return_array['last_poster_id'] = $row['poster_id'];
  1823. $return_array['last_post_date'] = $row['post_date'];
  1824. $return_array['last_poster_name'] = $row['poster_name'];
  1825. $return_array['last_poster_lastname'] = $row['lastname'];
  1826. $return_array['last_poster_firstname'] = $row['firstname'];
  1827. $return_array['last_post_title'] = $row['post_title'];
  1828. $return_array['last_post_text'] = $row['post_text'];
  1829. return $return_array;
  1830. } else {
  1831. // We have to loop through the results to find the first one that is
  1832. // actually visible to students (forum_category, forum, thread AND post are visible).
  1833. while ($row = Database::fetch_array($result)) {
  1834. if ($row['visible'] == '1' && $row['thread_visibility'] == '1' && $row['forum_visibility'] == '1') {
  1835. $return_array['last_post_id'] = $row['post_id'];
  1836. $return_array['last_poster_id'] = $row['poster_id'];
  1837. $return_array['last_post_date'] = $row['post_date'];
  1838. $return_array['last_poster_name'] = $row['poster_name'];
  1839. $return_array['last_poster_lastname'] = $row['lastname'];
  1840. $return_array['last_poster_firstname'] = $row['firstname'];
  1841. $return_array['last_post_title'] = $row['post_title'];
  1842. $return_array['last_post_text'] = $row['post_text'];
  1843. return $return_array;
  1844. }
  1845. }
  1846. }
  1847. }
  1848. /**
  1849. * Retrieve all the threads of a given forum.
  1850. *
  1851. * @param int $forum_id
  1852. * @param int|null $courseId Optional If is null then it is considered the current course
  1853. * @param int|null $sessionId Optional. If is null then it is considered the current session
  1854. *
  1855. * @return array containing all the information about the threads
  1856. *
  1857. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1858. *
  1859. * @version february 2006, dokeos 1.8
  1860. */
  1861. function get_threads($forum_id, $courseId = null, $sessionId = null)
  1862. {
  1863. $groupId = api_get_group_id();
  1864. $sessionId = $sessionId !== null ? (int) $sessionId : api_get_session_id();
  1865. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1866. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  1867. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  1868. $courseId = $courseId !== null ? (int) $courseId : api_get_course_int_id();
  1869. $groupInfo = GroupManager::get_group_properties($groupId);
  1870. $groupCondition = '';
  1871. if (!empty($groupInfo)) {
  1872. $groupIid = $groupInfo['iid'];
  1873. $groupCondition = " AND item_properties.to_group_id = '$groupIid' ";
  1874. }
  1875. $sessionCondition = api_get_session_condition(
  1876. $sessionId,
  1877. true,
  1878. false,
  1879. 'item_properties.session_id'
  1880. );
  1881. // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement
  1882. // because we also have thread.* in it. This is because thread has a field locked and post also has the same field
  1883. // since we are merging these we would have the post.locked value but in fact we want the thread.locked value
  1884. // This is why it is added to the end of the field selection
  1885. $sql = "SELECT DISTINCT
  1886. item_properties.*,
  1887. users.firstname,
  1888. users.lastname,
  1889. users.user_id,
  1890. thread.locked as locked,
  1891. thread.*
  1892. FROM $table_threads thread
  1893. INNER JOIN $table_item_property item_properties
  1894. ON
  1895. thread.thread_id = item_properties.ref AND
  1896. item_properties.c_id = thread.c_id AND
  1897. item_properties.tool = '".TABLE_FORUM_THREAD."'
  1898. $groupCondition
  1899. $sessionCondition
  1900. LEFT JOIN $table_users users
  1901. ON thread.thread_poster_id = users.user_id
  1902. WHERE
  1903. item_properties.visibility='1' AND
  1904. thread.forum_id = ".intval($forum_id)." AND
  1905. thread.c_id = $courseId
  1906. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1907. if (api_is_allowed_to_edit()) {
  1908. $sql = "SELECT DISTINCT
  1909. item_properties.*,
  1910. users.firstname,
  1911. users.lastname,
  1912. users.user_id,
  1913. thread.locked as locked,
  1914. thread.*
  1915. FROM $table_threads thread
  1916. INNER JOIN $table_item_property item_properties
  1917. ON
  1918. thread.thread_id = item_properties.ref AND
  1919. item_properties.c_id = thread.c_id AND
  1920. item_properties.tool = '".TABLE_FORUM_THREAD."'
  1921. $groupCondition
  1922. $sessionCondition
  1923. LEFT JOIN $table_users users
  1924. ON thread.thread_poster_id=users.user_id
  1925. WHERE
  1926. item_properties.visibility<>2 AND
  1927. thread.forum_id = ".intval($forum_id)." AND
  1928. thread.c_id = $courseId
  1929. ORDER BY thread.thread_sticky DESC, thread.thread_date DESC";
  1930. }
  1931. $result = Database::query($sql);
  1932. $list = [];
  1933. $alreadyAdded = [];
  1934. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1935. if (in_array($row['thread_id'], $alreadyAdded)) {
  1936. continue;
  1937. }
  1938. $list[] = $row;
  1939. $alreadyAdded[] = $row['thread_id'];
  1940. }
  1941. return $list;
  1942. }
  1943. /**
  1944. * Get a thread by Id and course id.
  1945. *
  1946. * @param int $threadId the thread Id
  1947. * @param int $cId the course id
  1948. *
  1949. * @return array containing all the information about the thread
  1950. */
  1951. function getThreadInfo($threadId, $cId)
  1952. {
  1953. $repo = Database::getManager()->getRepository('ChamiloCourseBundle:CForumThread');
  1954. $forumThread = $repo->findOneBy(['threadId' => $threadId, 'cId' => $cId]);
  1955. $thread = [];
  1956. if ($forumThread) {
  1957. $thread['threadId'] = $forumThread->getThreadId();
  1958. $thread['threadTitle'] = $forumThread->getThreadTitle();
  1959. $thread['forumId'] = $forumThread->getForumId();
  1960. $thread['sessionId'] = $forumThread->getSessionId();
  1961. $thread['threadSticky'] = $forumThread->getThreadSticky();
  1962. $thread['locked'] = $forumThread->getLocked();
  1963. $thread['threadTitleQualify'] = $forumThread->getThreadTitleQualify();
  1964. $thread['threadQualifyMax'] = $forumThread->getThreadQualifyMax();
  1965. $thread['threadCloseDate'] = $forumThread->getThreadCloseDate();
  1966. $thread['threadWeight'] = $forumThread->getThreadWeight();
  1967. $thread['threadPeerQualify'] = $forumThread->isThreadPeerQualify();
  1968. }
  1969. return $thread;
  1970. }
  1971. /**
  1972. * Retrieve all posts of a given thread.
  1973. *
  1974. * @param array $forumInfo
  1975. * @param int $threadId The thread ID
  1976. * @param string $orderDirection Optional. The direction for sort the posts
  1977. * @param bool $recursive Optional. If the list is recursive
  1978. * @param int $postId Optional. The post ID for recursive list
  1979. * @param int $depth Optional. The depth to indicate the indent
  1980. *
  1981. * @todo move to a repository
  1982. *
  1983. * @return array containing all the information about the posts of a given thread
  1984. */
  1985. function getPosts(
  1986. $forumInfo,
  1987. $threadId,
  1988. $orderDirection = 'ASC',
  1989. $recursive = false,
  1990. $postId = null,
  1991. $depth = -1
  1992. ) {
  1993. $em = Database::getManager();
  1994. if (api_is_allowed_to_edit(false, true)) {
  1995. $visibleCriteria = Criteria::expr()->neq('visible', 2);
  1996. } else {
  1997. $visibleCriteria = Criteria::expr()->eq('visible', 1);
  1998. }
  1999. $criteria = Criteria::create();
  2000. $criteria
  2001. ->where(Criteria::expr()->eq('threadId', $threadId))
  2002. ->andWhere(Criteria::expr()->eq('cId', $forumInfo['c_id']))
  2003. ->andWhere($visibleCriteria)
  2004. ;
  2005. $groupId = api_get_group_id();
  2006. $groupInfo = GroupManager::get_group_properties($groupId);
  2007. $filterModerated = true;
  2008. if (empty($groupId)) {
  2009. if (api_is_allowed_to_edit()) {
  2010. $filterModerated = false;
  2011. }
  2012. } else {
  2013. if (GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo) ||
  2014. api_is_allowed_to_edit(false, true)
  2015. ) {
  2016. $filterModerated = false;
  2017. }
  2018. }
  2019. if ($recursive) {
  2020. $criteria->andWhere(Criteria::expr()->eq('postParentId', $postId));
  2021. }
  2022. $qb = $em->getRepository('ChamiloCourseBundle:CForumPost')->createQueryBuilder('p');
  2023. $qb->select('p')
  2024. ->addCriteria($criteria)
  2025. ->addOrderBy('p.postId', $orderDirection);
  2026. if ($filterModerated && $forumInfo['moderated'] == 1) {
  2027. if (!api_is_allowed_to_edit(false, true)) {
  2028. $userId = api_get_user_id();
  2029. $qb->andWhere(
  2030. "p.status = 1 OR
  2031. (p.status = ".CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = $userId) OR
  2032. (p.status = ".CForumPost::STATUS_REJECTED." AND p.posterId = $userId) OR
  2033. (p.status IS NULL AND p.posterId = $userId)
  2034. "
  2035. );
  2036. }
  2037. }
  2038. $posts = $qb->getQuery()->getResult();
  2039. $depth++;
  2040. $list = [];
  2041. /** @var CForumPost $post */
  2042. foreach ($posts as $post) {
  2043. $postInfo = [
  2044. 'iid' => $post->getIid(),
  2045. 'c_id' => $post->getCId(),
  2046. 'post_id' => $post->getPostId(),
  2047. 'post_title' => $post->getPostTitle(),
  2048. 'post_text' => $post->getPostText(),
  2049. 'thread_id' => $post->getThreadId(),
  2050. 'forum_id' => $post->getForumId(),
  2051. 'poster_id' => $post->getPosterId(),
  2052. 'poster_name' => $post->getPosterName(),
  2053. 'post_date' => $post->getPostDate(),
  2054. 'post_notification' => $post->getPostNotification(),
  2055. 'post_parent_id' => $post->getPostParentId(),
  2056. 'visible' => $post->getVisible(),
  2057. 'status' => $post->getStatus(),
  2058. 'indent_cnt' => $depth,
  2059. ];
  2060. $posterId = $post->getPosterId();
  2061. if (!empty($posterId)) {
  2062. $user = api_get_user_entity($posterId);
  2063. if ($user) {
  2064. $postInfo['user_id'] = $user->getUserId();
  2065. $postInfo['username'] = $user->getUsername();
  2066. $postInfo['username_canonical'] = $user->getUsernameCanonical();
  2067. $postInfo['lastname'] = $user->getLastname();
  2068. $postInfo['firstname'] = $user->getFirstname();
  2069. $postInfo['complete_name'] = $user->getCompleteName();
  2070. }
  2071. }
  2072. $list[] = $postInfo;
  2073. if (!$recursive) {
  2074. continue;
  2075. }
  2076. $list = array_merge(
  2077. $list,
  2078. getPosts(
  2079. $forumInfo,
  2080. $threadId,
  2081. $orderDirection,
  2082. $recursive,
  2083. $post->getPostId(),
  2084. $depth
  2085. )
  2086. );
  2087. }
  2088. return $list;
  2089. }
  2090. /**
  2091. * This function retrieves all the information of a post.
  2092. *
  2093. * @param int $post_id integer that indicates the forum
  2094. *
  2095. * @return array returns
  2096. *
  2097. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2098. *
  2099. * @version february 2006, dokeos 1.8
  2100. */
  2101. function get_post_information($post_id)
  2102. {
  2103. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  2104. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  2105. $course_id = api_get_course_int_id();
  2106. $post_id = (int) $post_id;
  2107. if (empty($post_id)) {
  2108. return [];
  2109. }
  2110. $sql = "SELECT posts.*, email FROM ".$table_posts." posts, ".$table_users." users
  2111. WHERE
  2112. c_id = $course_id AND
  2113. posts.poster_id=users.user_id AND
  2114. posts.post_id = ".$post_id;
  2115. $result = Database::query($sql);
  2116. $row = Database::fetch_array($result, 'ASSOC');
  2117. return $row;
  2118. }
  2119. /**
  2120. * This function retrieves all the information of a thread.
  2121. *
  2122. * @param int $forumId
  2123. * @param $thread_id integer that indicates the forum
  2124. * @param int|null $sessionId Optional. If is null then it is considered the current session
  2125. *
  2126. * @return array returns
  2127. *
  2128. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2129. *
  2130. * @version february 2006, dokeos 1.8
  2131. */
  2132. function get_thread_information($forumId, $thread_id, $sessionId = null)
  2133. {
  2134. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2135. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  2136. $thread_id = intval($thread_id);
  2137. $sessionId = $sessionId !== null ? intval($sessionId) : api_get_session_id();
  2138. $sessionCondition = api_get_session_condition(
  2139. $sessionId,
  2140. true,
  2141. false,
  2142. 'threads.session_id'
  2143. );
  2144. $forumCondition = '';
  2145. if (!empty($forumId)) {
  2146. $forumId = (int) $forumId;
  2147. $forumCondition = " threads.forum_id = $forumId AND ";
  2148. }
  2149. $sql = "SELECT * FROM $table_item_property item_properties
  2150. INNER JOIN
  2151. $table_threads threads
  2152. ON (item_properties.ref = threads.thread_id AND threads.c_id = item_properties.c_id)
  2153. WHERE
  2154. $forumCondition
  2155. item_properties.tool= '".TOOL_FORUM_THREAD."' AND
  2156. threads.thread_id = $thread_id
  2157. $sessionCondition
  2158. ";
  2159. $result = Database::query($sql);
  2160. $row = Database::fetch_assoc($result);
  2161. return $row;
  2162. }
  2163. /**
  2164. * This function retrieves forum thread users details.
  2165. *
  2166. * @param int Thread ID
  2167. * @param string Course DB name (optional)
  2168. *
  2169. * @return Doctrine\DBAL\Driver\Statement|null array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  2170. *
  2171. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  2172. *
  2173. * @todo this function need to be improved
  2174. *
  2175. * @version octubre 2008, dokeos 1.8
  2176. */
  2177. function get_thread_users_details($thread_id)
  2178. {
  2179. $t_posts = Database::get_course_table(TABLE_FORUM_POST);
  2180. $t_users = Database::get_main_table(TABLE_MAIN_USER);
  2181. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2182. $t_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2183. $course_id = api_get_course_int_id();
  2184. $is_western_name_order = api_is_western_name_order();
  2185. if ($is_western_name_order) {
  2186. $orderby = 'ORDER BY user.firstname, user.lastname ';
  2187. } else {
  2188. $orderby = 'ORDER BY user.lastname, user.firstname';
  2189. }
  2190. if (api_get_session_id()) {
  2191. $session_info = api_get_session_info(api_get_session_id());
  2192. $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'";
  2193. //not showing coaches
  2194. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, thread_id
  2195. FROM $t_posts p, $t_users user, $t_session_rel_user session_rel_user_rel_course
  2196. WHERE
  2197. p.poster_id = user.id AND
  2198. user.id = session_rel_user_rel_course.user_id AND
  2199. session_rel_user_rel_course.status<>'2' AND
  2200. session_rel_user_rel_course.user_id NOT IN ($user_to_avoid) AND
  2201. p.thread_id = ".intval($thread_id)." AND
  2202. session_id = ".api_get_session_id()." AND
  2203. p.c_id = $course_id AND
  2204. session_rel_user_rel_course.c_id = ".$course_id." $orderby ";
  2205. } else {
  2206. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, thread_id
  2207. FROM $t_posts p, $t_users user, $t_course_user course_user
  2208. WHERE
  2209. p.poster_id = user.id
  2210. AND user.id = course_user.user_id
  2211. AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  2212. AND p.thread_id = ".intval($thread_id)."
  2213. AND course_user.status NOT IN('1') AND
  2214. p.c_id = $course_id AND
  2215. course_user.c_id = ".$course_id." $orderby";
  2216. }
  2217. $result = Database::query($sql);
  2218. return $result;
  2219. }
  2220. /**
  2221. * This function retrieves forum thread users qualify.
  2222. *
  2223. * @param int Thread ID
  2224. * @param string Course DB name (optional)
  2225. *
  2226. * @return Doctrine\DBAL\Driver\Statement|null Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  2227. *
  2228. * @author Jhon Hinojosa
  2229. *
  2230. * @todo this function need to be improved
  2231. */
  2232. function get_thread_users_qualify($thread_id)
  2233. {
  2234. $t_posts = Database::get_course_table(TABLE_FORUM_POST);
  2235. $t_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  2236. $t_users = Database::get_main_table(TABLE_MAIN_USER);
  2237. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2238. $t_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2239. $course_id = api_get_course_int_id();
  2240. $sessionId = api_get_session_id();
  2241. $is_western_name_order = api_is_western_name_order();
  2242. if ($is_western_name_order) {
  2243. $orderby = 'ORDER BY user.firstname, user.lastname ';
  2244. } else {
  2245. $orderby = 'ORDER BY user.lastname, user.firstname';
  2246. }
  2247. if ($sessionId) {
  2248. $session_info = api_get_session_info($sessionId);
  2249. $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'";
  2250. //not showing coaches
  2251. $sql = "SELECT DISTINCT post.poster_id, user.lastname, user.firstname, post.thread_id,user.id,qualify.qualify
  2252. FROM $t_posts post , $t_users user, $t_session_rel_user scu, $t_qualify qualify
  2253. WHERE poster_id = user.id
  2254. AND post.poster_id = qualify.user_id
  2255. AND user.id = scu.user_id
  2256. AND scu.status<>'2'
  2257. AND scu.user_id NOT IN ($user_to_avoid)
  2258. AND qualify.thread_id = ".intval($thread_id)."
  2259. AND post.thread_id = ".intval($thread_id)."
  2260. AND scu.session_id = $sessionId
  2261. AND scu.c_id = ".$course_id." AND
  2262. qualify.c_id = $course_id AND
  2263. post.c_id = $course_id
  2264. $orderby ";
  2265. } else {
  2266. $sql = "SELECT DISTINCT post.poster_id, user.lastname, user.firstname, post.thread_id,user.id,qualify.qualify
  2267. FROM $t_posts post,
  2268. $t_qualify qualify,
  2269. $t_users user,
  2270. $t_course_user course_user
  2271. WHERE
  2272. post.poster_id = user.id
  2273. AND post.poster_id = qualify.user_id
  2274. AND user.id = course_user.user_id
  2275. AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  2276. AND qualify.thread_id = ".intval($thread_id)."
  2277. AND post.thread_id = ".intval($thread_id)."
  2278. AND course_user.status not in('1')
  2279. AND course_user.c_id = $course_id
  2280. AND qualify.c_id = $course_id
  2281. AND post.c_id = $course_id
  2282. $orderby ";
  2283. }
  2284. $result = Database::query($sql);
  2285. return $result;
  2286. }
  2287. /**
  2288. * This function retrieves forum thread users not qualify.
  2289. *
  2290. * @param int Thread ID
  2291. * @param string Course DB name (optional)
  2292. *
  2293. * @return Doctrine\DBAL\Driver\Statement|null Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[])
  2294. *
  2295. * @author Jhon Hinojosa<jhon.hinojosa@dokeos.com>,
  2296. *
  2297. * @version oct 2008, dokeos 1.8
  2298. */
  2299. function get_thread_users_not_qualify($thread_id)
  2300. {
  2301. $t_posts = Database::get_course_table(TABLE_FORUM_POST);
  2302. $t_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  2303. $t_users = Database::get_main_table(TABLE_MAIN_USER);
  2304. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2305. $t_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2306. $is_western_name_order = api_is_western_name_order();
  2307. if ($is_western_name_order) {
  2308. $orderby = 'ORDER BY user.firstname, user.lastname ';
  2309. } else {
  2310. $orderby = 'ORDER BY user.lastname, user.firstname';
  2311. }
  2312. $course_id = api_get_course_int_id();
  2313. $sql1 = "SELECT user_id FROM $t_qualify
  2314. WHERE c_id = $course_id AND thread_id = '".$thread_id."'";
  2315. $result1 = Database::query($sql1);
  2316. $cad = '';
  2317. while ($row = Database::fetch_array($result1)) {
  2318. $cad .= $row['user_id'].',';
  2319. }
  2320. if ($cad == '') {
  2321. $cad = '0';
  2322. } else {
  2323. $cad = substr($cad, 0, strlen($cad) - 1);
  2324. }
  2325. if (api_get_session_id()) {
  2326. $session_info = api_get_session_info(api_get_session_id());
  2327. $user_to_avoid = "'".$session_info['id_coach']."', '".$session_info['session_admin_id']."'";
  2328. //not showing coaches
  2329. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, post.thread_id
  2330. FROM $t_posts post , $t_users user, $t_session_rel_user session_rel_user_rel_course
  2331. WHERE poster_id = user.id
  2332. AND user.id NOT IN (".$cad.")
  2333. AND user.id = session_rel_user_rel_course.user_id
  2334. AND session_rel_user_rel_course.status<>'2'
  2335. AND session_rel_user_rel_course.user_id NOT IN ($user_to_avoid)
  2336. AND post.thread_id = ".intval($thread_id)."
  2337. AND session_id = ".api_get_session_id()."
  2338. AND session_rel_user_rel_course.c_id = $course_id AND post.c_id = $course_id $orderby ";
  2339. } else {
  2340. $sql = "SELECT DISTINCT user.id, user.lastname, user.firstname, post.thread_id
  2341. FROM $t_posts post, $t_users user,$t_course_user course_user
  2342. WHERE post.poster_id = user.id
  2343. AND user.id NOT IN (".$cad.")
  2344. AND user.id = course_user.user_id
  2345. AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  2346. AND post.thread_id = ".intval($thread_id)."
  2347. AND course_user.status not in('1')
  2348. AND course_user.c_id = $course_id AND post.c_id = $course_id $orderby";
  2349. }
  2350. $result = Database::query($sql);
  2351. return $result;
  2352. }
  2353. /**
  2354. * This function retrieves all the information of a given forum_id.
  2355. *
  2356. * @param $forum_id integer that indicates the forum
  2357. *
  2358. * @return array returns
  2359. *
  2360. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2361. *
  2362. * @version february 2006, dokeos 1.8
  2363. *
  2364. * @deprecated this functionality is now moved to get_forums($forum_id)
  2365. */
  2366. function get_forum_information($forum_id, $courseId = 0)
  2367. {
  2368. $table_forums = Database::get_course_table(TABLE_FORUM);
  2369. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2370. $courseId = empty($courseId) ? api_get_course_int_id() : intval($courseId);
  2371. $forum_id = intval($forum_id);
  2372. $sql = "SELECT *
  2373. FROM $table_forums forums
  2374. INNER JOIN $table_item_property item_properties
  2375. ON (forums.c_id = item_properties.c_id)
  2376. WHERE
  2377. item_properties.tool = '".TOOL_FORUM."' AND
  2378. item_properties.ref = '".$forum_id."' AND
  2379. forums.forum_id = '".$forum_id."' AND
  2380. forums.c_id = ".$courseId."
  2381. ";
  2382. $result = Database::query($sql);
  2383. $row = Database::fetch_array($result, 'ASSOC');
  2384. $row['approval_direct_post'] = 0;
  2385. // We can't anymore change this option, so it should always be activated.
  2386. return $row;
  2387. }
  2388. /**
  2389. * This function retrieves all the information of a given forumcategory id.
  2390. *
  2391. * @param $cat_id integer that indicates the forum
  2392. *
  2393. * @return array returns if there are category or bool returns if there aren't category
  2394. *
  2395. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2396. *
  2397. * @version february 2006, dokeos 1.8
  2398. */
  2399. function get_forumcategory_information($cat_id)
  2400. {
  2401. $table_categories = Database::get_course_table(TABLE_FORUM_CATEGORY);
  2402. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2403. $course_id = api_get_course_int_id();
  2404. $sql = "SELECT *
  2405. FROM $table_categories forumcategories
  2406. INNER JOIN $table_item_property item_properties
  2407. ON (forumcategories.c_id = item_properties.c_id)
  2408. WHERE
  2409. forumcategories.c_id = $course_id AND
  2410. item_properties.c_id = $course_id AND
  2411. item_properties.tool='".TOOL_FORUM_CATEGORY."' AND
  2412. item_properties.ref='".Database::escape_string($cat_id)."' AND
  2413. forumcategories.cat_id='".Database::escape_string($cat_id)."'";
  2414. $result = Database::query($sql);
  2415. $row = Database::fetch_array($result);
  2416. return $row;
  2417. }
  2418. /**
  2419. * This function counts the number of forums inside a given category.
  2420. *
  2421. * @param int $cat_id the id of the forum category
  2422. *
  2423. * @todo an additional parameter that takes the visibility into account. For instance $countinvisible=0 would return the number
  2424. * of visible forums, $countinvisible=1 would return the number of visible and invisible forums
  2425. *
  2426. * @return int the number of forums inside the given category
  2427. *
  2428. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2429. *
  2430. * @version february 2006, dokeos 1.8
  2431. */
  2432. function count_number_of_forums_in_category($cat_id)
  2433. {
  2434. $table_forums = Database::get_course_table(TABLE_FORUM);
  2435. $course_id = api_get_course_int_id();
  2436. $cat_id = (int) $cat_id;
  2437. $sql = "SELECT count(*) AS number_of_forums
  2438. FROM $table_forums
  2439. WHERE c_id = $course_id AND forum_category = $cat_id";
  2440. $result = Database::query($sql);
  2441. $row = Database::fetch_array($result);
  2442. return $row['number_of_forums'];
  2443. }
  2444. /**
  2445. * This function update a thread.
  2446. *
  2447. * @param array $values - The form Values
  2448. */
  2449. function updateThread($values)
  2450. {
  2451. if (!api_is_allowed_to_edit()) {
  2452. return '';
  2453. }
  2454. $logInfo = [
  2455. 'tool' => TOOL_FORUM,
  2456. 'tool_id' => $values['forum_id'],
  2457. 'tool_id_detail' => $values['thread_id'],
  2458. 'action' => 'edit-thread',
  2459. 'action_details' => 'thread',
  2460. 'info' => $values['thread_title'],
  2461. ];
  2462. Event::registerLog($logInfo);
  2463. $threadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  2464. $courseId = api_get_course_int_id();
  2465. $courseCode = api_get_course_id();
  2466. $sessionId = api_get_session_id();
  2467. // Simple update + set gradebook values to null
  2468. $params = [
  2469. 'thread_title' => $values['thread_title'],
  2470. 'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : 0,
  2471. ];
  2472. $where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
  2473. Database::update($threadTable, $params, $where);
  2474. $id = $values['thread_id'];
  2475. $linkInfo = GradebookUtils::isResourceInCourseGradebook(
  2476. $courseCode,
  2477. LINK_FORUM_THREAD,
  2478. $id,
  2479. $sessionId
  2480. );
  2481. $linkId = $linkInfo['id'];
  2482. $em = Database::getManager();
  2483. $gradebookLink = null;
  2484. if (!empty($linkId)) {
  2485. $gradebookLink = $em->getRepository('ChamiloCoreBundle:GradebookLink')->find($linkId);
  2486. }
  2487. // values 1 or 0
  2488. $check = isset($values['thread_qualify_gradebook']) ? $values['thread_qualify_gradebook'] : false;
  2489. if ($check) {
  2490. $title = Security::remove_XSS(stripslashes($values['calification_notebook_title']));
  2491. $value = isset($values['numeric_calification']) ? intval($values['numeric_calification']) : 0;
  2492. $weight = isset($values['weight_calification']) ? floatval($values['weight_calification']) : 0;
  2493. $description = '';
  2494. // Update title
  2495. $params = [
  2496. 'thread_title_qualify' => $values['calification_notebook_title'],
  2497. 'thread_qualify_max' => api_float_val($values['numeric_calification']),
  2498. 'thread_weight' => api_float_val($values['weight_calification']),
  2499. 'thread_peer_qualify' => $values['thread_peer_qualify'],
  2500. ];
  2501. $where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
  2502. Database::update($threadTable, $params, $where);
  2503. if (!$linkInfo) {
  2504. GradebookUtils::add_resource_to_course_gradebook(
  2505. $values['category_id'],
  2506. $courseCode,
  2507. LINK_FORUM_THREAD,
  2508. $id,
  2509. $title,
  2510. $weight,
  2511. $value,
  2512. $description,
  2513. 1,
  2514. $sessionId
  2515. );
  2516. } else {
  2517. if ($gradebookLink) {
  2518. $gradebookLink->setWeight($weight);
  2519. $em->persist($gradebookLink);
  2520. $em->flush();
  2521. }
  2522. }
  2523. } else {
  2524. $params = [
  2525. 'thread_title_qualify' => '',
  2526. 'thread_qualify_max' => '',
  2527. 'thread_weight' => '',
  2528. 'thread_peer_qualify' => '',
  2529. ];
  2530. $where = ['c_id = ? AND thread_id = ?' => [$courseId, $values['thread_id']]];
  2531. Database::update($threadTable, $params, $where);
  2532. if (!empty($linkInfo)) {
  2533. if ($gradebookLink) {
  2534. $em->remove($gradebookLink);
  2535. $em->flush();
  2536. }
  2537. }
  2538. }
  2539. $message = get_lang('EditPostStored').'<br />';
  2540. Display::addFlash(Display::return_message($message, 'confirmation', false));
  2541. }
  2542. /**
  2543. * This function stores a new thread. This is done through an entry in the forum_thread table AND
  2544. * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet)).
  2545. *
  2546. * @param array $current_forum
  2547. * @param array $values
  2548. * @param array $courseInfo
  2549. * @param bool $showMessage
  2550. * @param int $userId Optional. The user ID
  2551. * @param int $sessionId
  2552. *
  2553. * @return CForumThread
  2554. *
  2555. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2556. *
  2557. * @version february 2006, dokeos 1.8
  2558. */
  2559. function store_thread(
  2560. $current_forum,
  2561. $values,
  2562. $courseInfo = [],
  2563. $showMessage = true,
  2564. $userId = 0,
  2565. $sessionId = 0
  2566. ) {
  2567. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  2568. $userId = $userId ?: api_get_user_id();
  2569. $course_id = $courseInfo['real_id'];
  2570. $courseCode = $courseInfo['code'];
  2571. $groupId = api_get_group_id();
  2572. $groupInfo = GroupManager::get_group_properties($groupId);
  2573. $sessionId = $sessionId ?: api_get_session_id();
  2574. $em = Database::getManager();
  2575. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  2576. $upload_ok = 1;
  2577. $has_attachment = false;
  2578. if (!empty($_FILES['user_upload']['name'])) {
  2579. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  2580. $has_attachment = true;
  2581. }
  2582. if (!$upload_ok) {
  2583. if ($showMessage) {
  2584. Display::addFlash(
  2585. Display::return_message(
  2586. get_lang('UplNoFileUploaded'),
  2587. 'error',
  2588. false
  2589. )
  2590. );
  2591. }
  2592. return null;
  2593. }
  2594. $post_date = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  2595. $visible = 1;
  2596. if ($current_forum['approval_direct_post'] == '1' && !api_is_allowed_to_edit(null, true)) {
  2597. $visible = 0; // The post has not been approved yet.
  2598. }
  2599. $clean_post_title = $values['post_title'];
  2600. // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table.
  2601. $lastThread = new CForumThread();
  2602. $lastThread
  2603. ->setCId($course_id)
  2604. ->setThreadTitle($clean_post_title)
  2605. ->setForumId($values['forum_id'])
  2606. ->setThreadPosterId($userId)
  2607. ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
  2608. ->setThreadDate($post_date)
  2609. ->setThreadSticky(isset($values['thread_sticky']) ? $values['thread_sticky'] : 0)
  2610. ->setThreadTitleQualify(
  2611. isset($values['calification_notebook_title']) ? $values['calification_notebook_title'] : null
  2612. )
  2613. ->setThreadQualifyMax(isset($values['numeric_calification']) ? (int) $values['numeric_calification'] : 0)
  2614. ->setThreadWeight(isset($values['weight_calification']) ? (int) $values['weight_calification'] : 0)
  2615. ->setThreadPeerQualify(isset($values['thread_peer_qualify']) ? (bool) $values['thread_peer_qualify'] : false)
  2616. ->setSessionId($sessionId)
  2617. ->setLpItemId(isset($values['lp_item_id']) ? (int) $values['lp_item_id'] : 0)
  2618. ->setThreadId(0)
  2619. ->setLocked(0)
  2620. ;
  2621. $em->persist($lastThread);
  2622. $em->flush();
  2623. // Add option gradebook qualify.
  2624. if (isset($values['thread_qualify_gradebook']) &&
  2625. 1 == $values['thread_qualify_gradebook']
  2626. ) {
  2627. // Add function gradebook.
  2628. $resourcename = stripslashes($values['calification_notebook_title']);
  2629. GradebookUtils::add_resource_to_course_gradebook(
  2630. $values['category_id'],
  2631. $courseCode,
  2632. 5,
  2633. $lastThread->getIid(),
  2634. $resourcename,
  2635. $values['weight_calification'],
  2636. $values['numeric_calification'],
  2637. '',
  2638. 0,
  2639. $sessionId
  2640. );
  2641. }
  2642. if ($lastThread->getIid()) {
  2643. $lastThread->setThreadId($lastThread->getIid());
  2644. $em->merge($lastThread);
  2645. $em->flush();
  2646. api_item_property_update(
  2647. $courseInfo,
  2648. TOOL_FORUM_THREAD,
  2649. $lastThread->getIid(),
  2650. 'ForumThreadAdded',
  2651. $userId,
  2652. $groupInfo,
  2653. null,
  2654. null,
  2655. null,
  2656. $sessionId
  2657. );
  2658. // If the forum properties tell that the posts have to be approved
  2659. // we have to put the whole thread invisible,
  2660. // because otherwise the students will see the thread and not the post
  2661. // in the thread.
  2662. // We also have to change $visible because the post itself has to be
  2663. // visible in this case (otherwise the teacher would have
  2664. // to make the thread visible AND the post.
  2665. // Default behaviour
  2666. api_set_default_visibility(
  2667. $lastThread->getIid(),
  2668. TOOL_FORUM_THREAD,
  2669. $groupId,
  2670. $courseInfo,
  2671. $sessionId,
  2672. $userId
  2673. );
  2674. if ($visible == 0) {
  2675. api_item_property_update(
  2676. $courseInfo,
  2677. TOOL_FORUM_THREAD,
  2678. $lastThread->getIid(),
  2679. 'invisible',
  2680. $userId,
  2681. $groupInfo
  2682. );
  2683. $visible = 1;
  2684. }
  2685. $logInfo = [
  2686. 'tool' => TOOL_FORUM,
  2687. 'tool_id' => $values['forum_id'],
  2688. 'tool_id_detail' => $lastThread->getIid(),
  2689. 'action' => 'new-thread',
  2690. 'action_details' => '',
  2691. 'info' => $clean_post_title,
  2692. ];
  2693. Event::registerLog($logInfo);
  2694. }
  2695. // We now store the content in the table_post table.
  2696. $lastPost = new CForumPost();
  2697. $lastPost
  2698. ->setCId($course_id)
  2699. ->setPostTitle($clean_post_title)
  2700. ->setPostText($values['post_text'])
  2701. ->setThreadId($lastThread->getIid())
  2702. ->setForumId($values['forum_id'])
  2703. ->setPosterId($userId)
  2704. ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
  2705. ->setPostDate($post_date)
  2706. ->setPostNotification(isset($values['post_notification']) ? (int) $values['post_notification'] : null)
  2707. ->setPostParentId(null)
  2708. ->setVisible($visible)
  2709. ->setPostId(0)
  2710. ->setStatus(CForumPost::STATUS_VALIDATED);
  2711. if ($current_forum['moderated']) {
  2712. $lastPost->setStatus(
  2713. api_is_course_admin() ? CForumPost::STATUS_VALIDATED : CForumPost::STATUS_WAITING_MODERATION
  2714. );
  2715. }
  2716. $em->persist($lastPost);
  2717. $em->flush();
  2718. $lastPostId = $lastPost->getIid();
  2719. $lastThread->setThreadLastPost($lastPostId);
  2720. $em->merge($lastThread);
  2721. $em->flush();
  2722. $logInfo = [
  2723. 'tool' => TOOL_FORUM,
  2724. 'tool_id' => $values['forum_id'],
  2725. 'tool_id_detail' => $lastThread->getIid(),
  2726. 'action' => 'new-post',
  2727. 'info' => $clean_post_title,
  2728. ];
  2729. Event::registerLog($logInfo);
  2730. if ($lastPostId) {
  2731. $lastPost->setPostId($lastPostId);
  2732. $em->merge($lastPost);
  2733. $em->flush();
  2734. }
  2735. // Update attached files
  2736. if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
  2737. foreach ($_POST['file_ids'] as $key => $id) {
  2738. editAttachedFile(
  2739. [
  2740. 'comment' => $_POST['file_comments'][$key],
  2741. 'post_id' => $lastPostId,
  2742. ],
  2743. $id
  2744. );
  2745. }
  2746. }
  2747. // Now we have to update the thread table to fill the thread_last_post
  2748. // field (so that we know when the thread has been updated for the last time).
  2749. $sql = "UPDATE $table_threads
  2750. SET thread_last_post = '".Database::escape_string($lastPostId)."'
  2751. WHERE
  2752. c_id = $course_id AND
  2753. thread_id='".Database::escape_string($lastThread->getIid())."'";
  2754. $result = Database::query($sql);
  2755. $message = get_lang('NewThreadStored');
  2756. // Overwrite default message.
  2757. if ($current_forum['moderated'] &&
  2758. !api_is_allowed_to_edit(null, true)
  2759. ) {
  2760. $message = get_lang('MessageHasToBeApproved');
  2761. }
  2762. // Storing the attachments if any.
  2763. if ($has_attachment) {
  2764. // Try to add an extension to the file if it hasn't one.
  2765. $new_file_name = add_ext_on_mime(
  2766. stripslashes($_FILES['user_upload']['name']),
  2767. $_FILES['user_upload']['type']
  2768. );
  2769. if (!filter_extension($new_file_name)) {
  2770. if ($showMessage) {
  2771. Display::addFlash(Display::return_message(
  2772. get_lang('UplUnableToSaveFileFilteredExtension'),
  2773. 'error'
  2774. ));
  2775. }
  2776. } else {
  2777. if ($result) {
  2778. add_forum_attachment_file(
  2779. isset($values['file_comment']) ? $values['file_comment'] : null,
  2780. $lastPostId
  2781. );
  2782. }
  2783. }
  2784. } else {
  2785. $message .= '<br />';
  2786. }
  2787. if ($current_forum['approval_direct_post'] == '1' &&
  2788. !api_is_allowed_to_edit(null, true)
  2789. ) {
  2790. $message .= get_lang('MessageHasToBeApproved').'<br />';
  2791. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'">'.
  2792. get_lang('Forum').'</a><br />';
  2793. } else {
  2794. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'">'.
  2795. get_lang('Forum').'</a><br />';
  2796. $message .= get_lang('ReturnTo').' <a href="viewthread.php?'.api_get_cidreq().'&forum='.$values['forum_id'].'&thread='.$lastThread->getIid().'">'.
  2797. get_lang('Message').'</a>';
  2798. }
  2799. $reply_info['new_post_id'] = $lastPostId;
  2800. $my_post_notification = isset($values['post_notification']) ? $values['post_notification'] : null;
  2801. if ($my_post_notification == 1) {
  2802. set_notification('thread', $lastThread->getIid(), true);
  2803. }
  2804. send_notification_mails(
  2805. $current_forum['forum_id'],
  2806. $lastThread->getIid(),
  2807. $reply_info,
  2808. $courseInfo['code']
  2809. );
  2810. Session::erase('formelements');
  2811. Session::erase('origin');
  2812. Session::erase('breadcrumbs');
  2813. Session::erase('addedresource');
  2814. Session::erase('addedresourceid');
  2815. if ($showMessage) {
  2816. Display::addFlash(Display::return_message($message, 'success', false));
  2817. }
  2818. return $lastThread;
  2819. }
  2820. /**
  2821. * This function displays the form that is used to add a post. This can be a new thread or a reply.
  2822. *
  2823. * @param array $current_forum
  2824. * @param string $action is the parameter that determines if we are
  2825. * 1. newthread: adding a new thread (both empty) => No I-frame
  2826. * 2. replythread: Replying to a thread ($action = replythread) => I-frame with the complete thread (if enabled)
  2827. * 3. replymessage: Replying to a message ($action =replymessage) => I-frame with the complete thread (if enabled)
  2828. * (I first thought to put and I-frame with the message only)
  2829. * 4. quote: Quoting a message ($action= quotemessage) => I-frame with the complete thread (if enabled).
  2830. * The message will be in the reply. (I first thought not to put an I-frame here)
  2831. * @param array $form_values
  2832. * @param bool $showPreview
  2833. *
  2834. * @return FormValidator
  2835. *
  2836. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2837. *
  2838. * @version february 2006, dokeos 1.8
  2839. */
  2840. function show_add_post_form($current_forum, $action, $form_values = '', $showPreview = true)
  2841. {
  2842. $_user = api_get_user_info();
  2843. $action = isset($action) ? Security::remove_XSS($action) : '';
  2844. $myThread = isset($_GET['thread']) ? (int) $_GET['thread'] : '';
  2845. $forumId = isset($_GET['forum']) ? (int) $_GET['forum'] : '';
  2846. $my_post = isset($_GET['post']) ? (int) $_GET['post'] : '';
  2847. $giveRevision = isset($_GET['give_revision']) && $_GET['give_revision'] == 1;
  2848. $url = api_get_self().'?'.http_build_query(
  2849. [
  2850. 'action' => $action,
  2851. 'forum' => $forumId,
  2852. 'thread' => $myThread,
  2853. 'post' => $my_post,
  2854. ]
  2855. ).'&'.api_get_cidreq();
  2856. $form = new FormValidator(
  2857. 'thread',
  2858. 'post',
  2859. $url
  2860. );
  2861. $form->setConstants(['forum' => '5']);
  2862. // Setting the form elements.
  2863. $form->addElement('hidden', 'forum_id', $forumId);
  2864. $form->addElement('hidden', 'thread_id', $myThread);
  2865. $form->addElement('hidden', 'action', $action);
  2866. // If anonymous posts are allowed we also display a form to allow the user to put his name or username in.
  2867. if ($current_forum['allow_anonymous'] == 1 && !isset($_user['user_id'])) {
  2868. $form->addElement('text', 'poster_name', get_lang('Name'));
  2869. $form->applyFilter('poster_name', 'html_filter');
  2870. }
  2871. $form->addElement('text', 'post_title', get_lang('Title'));
  2872. $form->addHtmlEditor(
  2873. 'post_text',
  2874. get_lang('Text'),
  2875. true,
  2876. false,
  2877. api_is_allowed_to_edit(null, true) ? [
  2878. 'ToolbarSet' => 'Forum',
  2879. 'Width' => '100%',
  2880. 'Height' => '300',
  2881. ] : [
  2882. 'ToolbarSet' => 'ForumStudent',
  2883. 'Width' => '100%',
  2884. 'Height' => '300',
  2885. 'UserStatus' => 'student',
  2886. ]
  2887. );
  2888. $form->addRule('post_text', get_lang('ThisFieldIsRequired'), 'required');
  2889. if (in_array($action, ['newthread', 'replythread', 'replymessage', 'quote'])) {
  2890. $extraFields = new ExtraField('forum_post');
  2891. $extraFields->addElements(
  2892. $form,
  2893. null,
  2894. [], //exclude
  2895. false, // filter
  2896. false, // tag as select
  2897. ['ask_for_revision'], //show only fields
  2898. [], // order fields
  2899. [] // extra data);
  2900. );
  2901. }
  2902. $iframe = null;
  2903. if ($showPreview) {
  2904. $myThread = Security::remove_XSS($myThread);
  2905. if ($action != 'newthread' && !empty($myThread)) {
  2906. $iframe = "<iframe style=\"border: 1px solid black\" src=\"iframe_thread.php?".api_get_cidreq(
  2907. )."&forum=".$forumId."&thread=".$myThread."#".$my_post."\" width=\"100%\"></iframe>";
  2908. }
  2909. if (!empty($iframe)) {
  2910. $form->addElement('label', get_lang('Thread'), $iframe);
  2911. }
  2912. }
  2913. if (Gradebook::is_active() &&
  2914. (api_is_course_admin() || api_is_session_general_coach() || api_is_course_tutor()) && !($myThread)
  2915. ) {
  2916. $form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
  2917. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  2918. // Thread qualify
  2919. if (Gradebook::is_active()) {
  2920. //Loading gradebook select
  2921. GradebookUtils::load_gradebook_select_in_tool($form);
  2922. $form->addElement(
  2923. 'checkbox',
  2924. 'thread_qualify_gradebook',
  2925. '',
  2926. get_lang('QualifyThreadGradebook'),
  2927. 'onclick="javascript:if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"'
  2928. );
  2929. } else {
  2930. $form->addElement('hidden', 'thread_qualify_gradebook', false);
  2931. }
  2932. $form->addElement('html', '<div id="options_field" style="display:none">');
  2933. $form->addElement('text', 'numeric_calification', get_lang('QualificationNumeric'));
  2934. $form->applyFilter('numeric_calification', 'html_filter');
  2935. $form->addElement('text', 'calification_notebook_title', get_lang('TitleColumnGradebook'));
  2936. $form->applyFilter('calification_notebook_title', 'html_filter');
  2937. $form->addElement(
  2938. 'text',
  2939. 'weight_calification',
  2940. get_lang('QualifyWeight'),
  2941. ['value' => '0.00', 'onfocus' => "javascript: this.select();"]
  2942. );
  2943. $form->applyFilter('weight_calification', 'html_filter');
  2944. $group = [];
  2945. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('Yes'), 1);
  2946. $group[] = $form->createElement('radio', 'thread_peer_qualify', null, get_lang('No'), 0);
  2947. $form->addGroup(
  2948. $group,
  2949. '',
  2950. [
  2951. get_lang('ForumThreadPeerScoring'),
  2952. get_lang('ForumThreadPeerScoringComment'),
  2953. ]
  2954. );
  2955. $form->addElement('html', '</div>');
  2956. $form->addElement('html', '</div>');
  2957. }
  2958. if ($action === 'newthread') {
  2959. Skill::addSkillsToForm($form, ITEM_TYPE_FORUM_THREAD, 0);
  2960. }
  2961. if (api_is_allowed_to_edit(null, true) && $action == 'newthread') {
  2962. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  2963. }
  2964. if (in_array($action, ['quote', 'replymessage'])) {
  2965. $form->addFile('user_upload[]', get_lang('Attachment'));
  2966. $form->addButton(
  2967. 'add_attachment',
  2968. get_lang('AddAttachment'),
  2969. 'paperclip',
  2970. 'default',
  2971. 'default',
  2972. null,
  2973. ['id' => 'reply-add-attachment']
  2974. );
  2975. } else {
  2976. $form->addFile('user_upload', get_lang('Attachment'));
  2977. }
  2978. if ($giveRevision) {
  2979. $hide = api_get_configuration_value('hide_forum_post_revision_language');
  2980. $form->addHidden('give_revision', 1);
  2981. if ($hide === false) {
  2982. $extraField = new ExtraField('forum_post');
  2983. $extraField->addElements(
  2984. $form,
  2985. null,
  2986. [], //exclude
  2987. false, // filter
  2988. false, // tag as select
  2989. ['revision_language'], //show only fields
  2990. [], // order fields
  2991. [] // extra data
  2992. );
  2993. } else {
  2994. $form->addHidden('extra_revision_language', 1);
  2995. }
  2996. }
  2997. // Setting the class and text of the form title and submit button.
  2998. if ($action == 'quote') {
  2999. $form->addButtonCreate(get_lang('QuoteMessage'), 'SubmitPost');
  3000. } elseif ($action == 'replythread') {
  3001. $form->addButtonCreate(get_lang('ReplyToThread'), 'SubmitPost');
  3002. } elseif ($action == 'replymessage') {
  3003. $form->addButtonCreate(get_lang('ReplyToMessage'), 'SubmitPost');
  3004. } else {
  3005. $form->addButtonCreate(get_lang('CreateThread'), 'SubmitPost');
  3006. }
  3007. $defaults['thread_peer_qualify'] = 0;
  3008. if (!empty($form_values)) {
  3009. $defaults['post_title'] = prepare4display($form_values['post_title']);
  3010. $defaults['post_text'] = prepare4display($form_values['post_text']);
  3011. $defaults['post_notification'] = (int) $form_values['post_notification'];
  3012. $defaults['thread_sticky'] = (int) $form_values['thread_sticky'];
  3013. $defaults['thread_peer_qualify'] = (int) $form_values['thread_peer_qualify'];
  3014. }
  3015. // If we are quoting a message we have to retrieve the information of the post we are quoting so that
  3016. // we can add this as default to the textarea.
  3017. // We also need to put the parent_id of the post in a hidden form when
  3018. if (($action == 'quote' || $action == 'replymessage' || $giveRevision) && !empty($my_post)) {
  3019. // we are quoting or replying to a message (<> reply to a thread !!!)
  3020. $form->addHidden('post_parent_id', $my_post);
  3021. // If we are replying or are quoting then we display a default title.
  3022. $values = get_post_information($my_post);
  3023. $posterInfo = api_get_user_info($values['poster_id']);
  3024. $posterName = '';
  3025. if ($posterInfo) {
  3026. $posterName = $posterInfo['complete_name'];
  3027. }
  3028. $defaults['post_title'] = get_lang('ReplyShort').api_html_entity_decode($values['post_title'], ENT_QUOTES);
  3029. // When we are quoting a message then we have to put that message into the wysiwyg editor.
  3030. // Note: The style has to be hardcoded here because using class="quote" didn't work.
  3031. if ($action == 'quote') {
  3032. $defaults['post_text'] = '<div>&nbsp;</div>
  3033. <div style="margin: 5px;">
  3034. <div style="font-size: 90%; font-style: italic;">'.
  3035. get_lang('Quoting').' '.$posterName.':</div>
  3036. <div style="color: #006600; font-size: 90%; font-style: italic; background-color: #FAFAFA; border: #D1D7DC 1px solid; padding: 3px;">'.
  3037. prepare4display($values['post_text']).'
  3038. </div>
  3039. </div>
  3040. <div>&nbsp;</div>
  3041. <div>&nbsp;</div>
  3042. ';
  3043. }
  3044. if ($giveRevision) {
  3045. $defaults['post_text'] = prepare4display($values['post_text']);
  3046. }
  3047. }
  3048. $form->setDefaults(isset($defaults) ? $defaults : []);
  3049. // The course admin can make a thread sticky (=appears with special icon and always on top).
  3050. $form->addRule('post_title', get_lang('ThisFieldIsRequired'), 'required');
  3051. if ($current_forum['allow_anonymous'] == 1 && !isset($_user['user_id'])) {
  3052. $form->addRule(
  3053. 'poster_name',
  3054. get_lang('ThisFieldIsRequired'),
  3055. 'required'
  3056. );
  3057. }
  3058. // Validation or display
  3059. if ($form->validate()) {
  3060. $check = Security::check_token('post');
  3061. if ($check) {
  3062. $values = $form->getSubmitValues();
  3063. if (isset($values['thread_qualify_gradebook']) &&
  3064. $values['thread_qualify_gradebook'] == '1' &&
  3065. empty($values['weight_calification'])
  3066. ) {
  3067. Display::addFlash(
  3068. Display::return_message(
  3069. get_lang('YouMustAssignWeightOfQualification').'&nbsp;<a href="javascript:window.history.go(-1);">'.get_lang('Back').'</a>',
  3070. 'error',
  3071. false
  3072. )
  3073. );
  3074. return false;
  3075. }
  3076. $postId = 0;
  3077. $threadId = 0;
  3078. switch ($action) {
  3079. case 'newthread':
  3080. $myThread = store_thread($current_forum, $values);
  3081. if ($myThread) {
  3082. $threadId = $myThread->getIid();
  3083. Skill::saveSkills($form, ITEM_TYPE_FORUM_THREAD, $threadId);
  3084. $postId = $myThread->getThreadLastPost();
  3085. }
  3086. break;
  3087. case 'quote':
  3088. case 'replythread':
  3089. case 'replymessage':
  3090. $postId = store_reply($current_forum, $values);
  3091. break;
  3092. }
  3093. if ($postId) {
  3094. $postInfo = get_post_information($postId);
  3095. if ($postInfo) {
  3096. $threadId = $postInfo['thread_id'];
  3097. }
  3098. if (isset($values['give_revision']) && $values['give_revision'] == 1) {
  3099. $extraFieldValues = new ExtraFieldValue('forum_post');
  3100. $revisionLanguage = isset($values['extra_revision_language']) ? $values['extra_revision_language'] : '';
  3101. $params = [
  3102. 'item_id' => $postId,
  3103. 'extra_revision_language' => $revisionLanguage,
  3104. ];
  3105. $extraFieldValues->saveFieldValues(
  3106. $params,
  3107. false,
  3108. false,
  3109. ['revision_language']
  3110. );
  3111. }
  3112. if (in_array($action, ['newthread', 'replythread', 'replymessage', 'quote'])) {
  3113. $extraFieldValues = new ExtraFieldValue('forum_post');
  3114. $params = [
  3115. 'item_id' => $postId,
  3116. 'extra_ask_for_revision' => isset($values['extra_ask_for_revision']) ? $values['extra_ask_for_revision'] : '',
  3117. ];
  3118. $extraFieldValues->saveFieldValues(
  3119. $params,
  3120. false,
  3121. false,
  3122. ['ask_for_revision']
  3123. );
  3124. }
  3125. }
  3126. $url = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&'.http_build_query(
  3127. [
  3128. 'forum' => $forumId,
  3129. 'thread' => $threadId,
  3130. ]
  3131. );
  3132. Security::clear_token();
  3133. header('Location: '.$url);
  3134. exit;
  3135. }
  3136. } else {
  3137. $token = Security::get_token();
  3138. $form->addElement('hidden', 'sec_token');
  3139. $form->setConstants(['sec_token' => $token]);
  3140. // Delete from $_SESSION forum attachment from other posts
  3141. // and keep only attachments for new post
  3142. clearAttachedFiles(FORUM_NEW_POST);
  3143. // Get forum attachment ajax table to add it to form
  3144. $attachmentAjaxTable = getAttachmentsAjaxTable(0, $current_forum['forum_id']);
  3145. $ajaxHtml = $attachmentAjaxTable;
  3146. $form->addElement('html', $ajaxHtml);
  3147. return $form;
  3148. }
  3149. }
  3150. /**
  3151. * @param array $threadInfo
  3152. * @param int $user_id
  3153. * @param int $thread_id
  3154. * @param int $thread_qualify
  3155. * @param int $qualify_time
  3156. * @param int $session_id
  3157. *
  3158. * @return array optional
  3159. *
  3160. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3161. *
  3162. * @version October 2008, dokeos 1.8.6
  3163. */
  3164. function saveThreadScore(
  3165. $threadInfo,
  3166. $user_id,
  3167. $thread_id,
  3168. $thread_qualify = 0,
  3169. $qualify_time,
  3170. $session_id = 0
  3171. ) {
  3172. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3173. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3174. $course_id = api_get_course_int_id();
  3175. $session_id = intval($session_id);
  3176. $currentUserId = api_get_user_id();
  3177. if ($user_id == strval(intval($user_id)) &&
  3178. $thread_id == strval(intval($thread_id)) &&
  3179. $thread_qualify == strval(floatval($thread_qualify))
  3180. ) {
  3181. // Testing
  3182. $sql = "SELECT thread_qualify_max FROM $table_threads
  3183. WHERE c_id = $course_id AND thread_id=".$thread_id;
  3184. $res_string = Database::query($sql);
  3185. $row_string = Database::fetch_array($res_string);
  3186. if ($thread_qualify <= $row_string[0]) {
  3187. if ($threadInfo['thread_peer_qualify'] == 0) {
  3188. $sql = "SELECT COUNT(*) FROM $table_threads_qualify
  3189. WHERE
  3190. c_id = $course_id AND
  3191. user_id = $user_id AND
  3192. thread_id = ".$thread_id;
  3193. } else {
  3194. $sql = "SELECT COUNT(*) FROM $table_threads_qualify
  3195. WHERE
  3196. c_id = $course_id AND
  3197. user_id = $user_id AND
  3198. qualify_user_id = $currentUserId AND
  3199. thread_id = ".$thread_id;
  3200. }
  3201. $result = Database::query($sql);
  3202. $row = Database::fetch_array($result);
  3203. if ($row[0] == 0) {
  3204. $sql = "INSERT INTO $table_threads_qualify (c_id, user_id, thread_id,qualify,qualify_user_id,qualify_time,session_id)
  3205. VALUES (".$course_id.", '".$user_id."','".$thread_id."',".(float) $thread_qualify.", '".$currentUserId."','".$qualify_time."','".$session_id."')";
  3206. Database::query($sql);
  3207. $insertId = Database::insert_id();
  3208. if ($insertId) {
  3209. $sql = "UPDATE $table_threads_qualify SET id = iid
  3210. WHERE iid = $insertId";
  3211. Database::query($sql);
  3212. }
  3213. return 'insert';
  3214. } else {
  3215. saveThreadScoreHistory(
  3216. '1',
  3217. $course_id,
  3218. $user_id,
  3219. $thread_id
  3220. );
  3221. // Update
  3222. $sql = "UPDATE $table_threads_qualify
  3223. SET
  3224. qualify = '".$thread_qualify."',
  3225. qualify_time = '".$qualify_time."'
  3226. WHERE
  3227. c_id = $course_id AND
  3228. user_id=".$user_id." AND
  3229. thread_id=".$thread_id." AND
  3230. qualify_user_id = $currentUserId
  3231. ";
  3232. Database::query($sql);
  3233. return 'update';
  3234. }
  3235. } else {
  3236. return null;
  3237. }
  3238. }
  3239. }
  3240. /**
  3241. * This function shows qualify.
  3242. *
  3243. * @param string $option contains the information of option to run
  3244. * @param int $user_id contains the information the current user id
  3245. * @param int $thread_id contains the information the current thread id
  3246. *
  3247. * @return int qualify
  3248. * <code> $option=1 obtained the qualification of the current thread</code>
  3249. *
  3250. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3251. *
  3252. * @version October 2008, dokeos 1.8.6
  3253. */
  3254. function showQualify($option, $user_id, $thread_id)
  3255. {
  3256. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3257. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3258. $course_id = api_get_course_int_id();
  3259. $user_id = (int) $user_id;
  3260. $thread_id = (int) $thread_id;
  3261. if (empty($user_id) || empty($thread_id)) {
  3262. return false;
  3263. }
  3264. $sql = '';
  3265. switch ($option) {
  3266. case 1:
  3267. $sql = "SELECT qualify FROM $table_threads_qualify
  3268. WHERE
  3269. c_id = $course_id AND
  3270. user_id=".$user_id." AND
  3271. thread_id=".$thread_id;
  3272. break;
  3273. case 2:
  3274. $sql = "SELECT thread_qualify_max FROM $table_threads
  3275. WHERE c_id = $course_id AND thread_id=".$thread_id;
  3276. break;
  3277. }
  3278. if (!empty($sql)) {
  3279. $rs = Database::query($sql);
  3280. $row = Database::fetch_array($rs);
  3281. return $row[0];
  3282. }
  3283. return [];
  3284. }
  3285. /**
  3286. * This function gets qualify historical.
  3287. *
  3288. * @param int $user_id contains the information the current user id
  3289. * @param int $thread_id contains the information the current thread id
  3290. * @param bool $opt contains the information of option to run
  3291. *
  3292. * @return array
  3293. *
  3294. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  3295. * @author Isaac Flores <isaac.flores@dokeos.com>,
  3296. *
  3297. * @version October 2008, dokeos 1.8.6
  3298. */
  3299. function getThreadScoreHistory($user_id, $thread_id, $opt)
  3300. {
  3301. $user_id = (int) $user_id;
  3302. $thread_id = (int) $thread_id;
  3303. $table_threads_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG);
  3304. $course_id = api_get_course_int_id();
  3305. if ($opt == 'false') {
  3306. $sql = "SELECT * FROM $table_threads_qualify_log
  3307. WHERE
  3308. c_id = $course_id AND
  3309. thread_id='".$thread_id."' AND
  3310. user_id='".$user_id."'
  3311. ORDER BY qualify_time";
  3312. } else {
  3313. $sql = "SELECT * FROM $table_threads_qualify_log
  3314. WHERE
  3315. c_id = $course_id AND
  3316. thread_id='".$thread_id."' AND
  3317. user_id='".$user_id."'
  3318. ORDER BY qualify_time DESC";
  3319. }
  3320. $rs = Database::query($sql);
  3321. $log = [];
  3322. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3323. $log[] = $row;
  3324. }
  3325. return $log;
  3326. }
  3327. /**
  3328. * This function stores qualify historical.
  3329. *
  3330. * @param bool contains the information of option to run
  3331. * @param string contains the information the current course id
  3332. * @param int contains the information the current forum id
  3333. * @param int contains the information the current user id
  3334. * @param int contains the information the current thread id
  3335. * @param int contains the information the current qualify
  3336. * @param string $option
  3337. * @param int $course_id
  3338. * @param int $user_id
  3339. * @param int $thread_id
  3340. *
  3341. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3342. *
  3343. * @version October 2008, dokeos 1.8.6
  3344. */
  3345. function saveThreadScoreHistory(
  3346. $option,
  3347. $course_id,
  3348. $user_id,
  3349. $thread_id
  3350. ) {
  3351. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3352. $table_threads_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG);
  3353. $course_id = intval($course_id);
  3354. $qualify_user_id = api_get_user_id();
  3355. if ($user_id == strval(intval($user_id)) &&
  3356. $thread_id == strval(intval($thread_id)) && $option == 1
  3357. ) {
  3358. // Extract information of thread_qualify.
  3359. $sql = "SELECT qualify, qualify_time
  3360. FROM $table_threads_qualify
  3361. WHERE
  3362. c_id = $course_id AND
  3363. user_id = ".$user_id." AND
  3364. thread_id = ".$thread_id." AND
  3365. qualify_user_id = $qualify_user_id
  3366. ";
  3367. $rs = Database::query($sql);
  3368. $row = Database::fetch_array($rs);
  3369. // Insert thread_historical.
  3370. $sql = "INSERT INTO $table_threads_qualify_log (c_id, user_id, thread_id, qualify, qualify_user_id,qualify_time,session_id)
  3371. VALUES(".$course_id.", '".$user_id."','".$thread_id."',".(float) $row[0].", '".$qualify_user_id."','".$row[1]."','')";
  3372. Database::query($sql);
  3373. $insertId = Database::insert_id();
  3374. if ($insertId) {
  3375. $sql = "UPDATE $table_threads_qualify_log SET id = iid
  3376. WHERE iid = $insertId";
  3377. Database::query($sql);
  3378. }
  3379. }
  3380. }
  3381. /**
  3382. * This function shows current thread qualify .
  3383. *
  3384. * @param int $threadId
  3385. * @param int $sessionId
  3386. * @param int $userId
  3387. *
  3388. * @return array or null if is empty
  3389. *
  3390. * @author Isaac Flores <isaac.flores@dokeos.com>, U.N.A.S University
  3391. *
  3392. * @version December 2008, dokeos 1.8.6
  3393. */
  3394. function current_qualify_of_thread($threadId, $sessionId, $userId)
  3395. {
  3396. $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  3397. $course_id = api_get_course_int_id();
  3398. $currentUserId = api_get_user_id();
  3399. $sessionId = intval($sessionId);
  3400. $threadId = intval($threadId);
  3401. $sql = "SELECT qualify FROM $table_threads_qualify
  3402. WHERE
  3403. c_id = $course_id AND
  3404. thread_id = $threadId AND
  3405. session_id = $sessionId AND
  3406. qualify_user_id = $currentUserId AND
  3407. user_id = $userId
  3408. ";
  3409. $res = Database::query($sql);
  3410. $row = Database::fetch_array($res, 'ASSOC');
  3411. return $row['qualify'];
  3412. }
  3413. /**
  3414. * This function stores a reply in the forum_post table.
  3415. * It also updates the forum_threads table (thread_replies +1 , thread_last_post, thread_date).
  3416. *
  3417. * @param array $current_forum
  3418. * @param array $values
  3419. * @param int $courseId Optional
  3420. * @param int $userId Optional
  3421. *
  3422. * @return int post id
  3423. *
  3424. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3425. *
  3426. * @version february 2006, dokeos 1.8
  3427. */
  3428. function store_reply($current_forum, $values, $courseId = 0, $userId = 0)
  3429. {
  3430. $courseId = !empty($courseId) ? $courseId : api_get_course_int_id();
  3431. $_course = api_get_course_info_by_id($courseId);
  3432. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3433. $post_date = api_get_utc_datetime();
  3434. $userId = $userId ?: api_get_user_id();
  3435. if ($current_forum['allow_anonymous'] == 1) {
  3436. if (api_is_anonymous() && empty($userId)) {
  3437. $userId = api_get_anonymous_id();
  3438. }
  3439. }
  3440. if (empty($userId)) {
  3441. return false;
  3442. }
  3443. $visible = 1;
  3444. if ($current_forum['approval_direct_post'] == '1' &&
  3445. !api_is_allowed_to_edit(null, true)
  3446. ) {
  3447. $visible = 0;
  3448. }
  3449. $upload_ok = 1;
  3450. $new_post_id = 0;
  3451. if ($upload_ok) {
  3452. // We first store an entry in the forum_post table.
  3453. $new_post_id = Database::insert(
  3454. $table_posts,
  3455. [
  3456. 'c_id' => $courseId,
  3457. 'post_title' => $values['post_title'],
  3458. 'post_text' => isset($values['post_text']) ? ($values['post_text']) : null,
  3459. 'thread_id' => $values['thread_id'],
  3460. 'forum_id' => $values['forum_id'],
  3461. 'poster_id' => $userId,
  3462. 'post_id' => 0,
  3463. 'post_date' => $post_date,
  3464. 'post_notification' => isset($values['post_notification']) ? $values['post_notification'] : null,
  3465. 'post_parent_id' => isset($values['post_parent_id']) ? $values['post_parent_id'] : null,
  3466. 'visible' => $visible,
  3467. ]
  3468. );
  3469. if ($new_post_id) {
  3470. $sql = "UPDATE $table_posts SET post_id = iid WHERE iid = $new_post_id";
  3471. Database::query($sql);
  3472. $values['new_post_id'] = $new_post_id;
  3473. $message = get_lang('ReplyAdded');
  3474. if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
  3475. foreach ($_POST['file_ids'] as $key => $id) {
  3476. editAttachedFile(
  3477. [
  3478. 'comment' => $_POST['file_comments'][$key],
  3479. 'post_id' => $new_post_id,
  3480. ],
  3481. $id
  3482. );
  3483. }
  3484. }
  3485. // Update the thread.
  3486. updateThreadInfo($values['thread_id'], $new_post_id, $post_date);
  3487. // Update the forum.
  3488. api_item_property_update(
  3489. $_course,
  3490. TOOL_FORUM,
  3491. $values['forum_id'],
  3492. 'NewMessageInForum',
  3493. $userId
  3494. );
  3495. // Insert post
  3496. api_item_property_update(
  3497. $_course,
  3498. TOOL_FORUM_POST,
  3499. $new_post_id,
  3500. 'NewPost',
  3501. $userId
  3502. );
  3503. if ($current_forum['approval_direct_post'] == '1' &&
  3504. !api_is_allowed_to_edit(null, true)
  3505. ) {
  3506. $message .= '<br />'.get_lang('MessageHasToBeApproved').'<br />';
  3507. }
  3508. if ($current_forum['moderated'] &&
  3509. !api_is_allowed_to_edit(null, true)
  3510. ) {
  3511. $message .= '<br />'.get_lang('MessageHasToBeApproved').'<br />';
  3512. }
  3513. // Setting the notification correctly.
  3514. $my_post_notification = isset($values['post_notification']) ? $values['post_notification'] : null;
  3515. if ($my_post_notification == 1) {
  3516. set_notification('thread', $values['thread_id'], true);
  3517. }
  3518. send_notification_mails(
  3519. $values['forum_id'],
  3520. $values['thread_id'],
  3521. $values
  3522. );
  3523. add_forum_attachment_file('', $new_post_id);
  3524. $logInfo = [
  3525. 'tool' => TOOL_FORUM,
  3526. 'tool_id' => $values['forum_id'],
  3527. 'tool_id_detail' => $values['thread_id'],
  3528. 'action' => 'new-post',
  3529. 'action_details' => $values['action'],
  3530. 'info' => $values['post_title'],
  3531. ];
  3532. Event::registerLog($logInfo);
  3533. }
  3534. Session::erase('formelements');
  3535. Session::erase('origin');
  3536. Session::erase('breadcrumbs');
  3537. Session::erase('addedresource');
  3538. Session::erase('addedresourceid');
  3539. Display::addFlash(Display::return_message($message, 'confirmation', false));
  3540. } else {
  3541. Display::addFlash(
  3542. Display::return_message(
  3543. get_lang('UplNoFileUploaded').' '.get_lang('UplSelectFileFirst'),
  3544. 'error'
  3545. )
  3546. );
  3547. }
  3548. return $new_post_id;
  3549. }
  3550. /**
  3551. * This function displays the form that is used to edit a post. This can be a new thread or a reply.
  3552. *
  3553. * @param array contains all the information about the current post
  3554. * @param array contains all the information about the current thread
  3555. * @param array contains all info about the current forum (to check if attachments are allowed)
  3556. * @param array contains the default values to fill the form
  3557. *
  3558. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3559. *
  3560. * @version february 2006, dokeos 1.8
  3561. */
  3562. function show_edit_post_form(
  3563. $current_post,
  3564. $current_thread,
  3565. $current_forum,
  3566. $form_values = '',
  3567. $id_attach = 0
  3568. ) {
  3569. // Initialize the object.
  3570. $form = new FormValidator(
  3571. 'edit_post',
  3572. 'post',
  3573. api_get_self().'?'.api_get_cidreq().'&forum='.intval($_GET['forum']).'&thread='.intval($_GET['thread']).'&post='.intval($_GET['post'])
  3574. );
  3575. $form->addElement('header', get_lang('EditPost'));
  3576. // Setting the form elements.
  3577. $form->addElement('hidden', 'post_id', $current_post['post_id']);
  3578. $form->addElement('hidden', 'thread_id', $current_thread['thread_id']);
  3579. $form->addElement('hidden', 'id_attach', $id_attach);
  3580. if (empty($current_post['post_parent_id'])) {
  3581. $form->addElement('hidden', 'is_first_post_of_thread', '1');
  3582. }
  3583. $form->addElement('text', 'post_title', get_lang('Title'));
  3584. $form->applyFilter('post_title', 'html_filter');
  3585. $form->addElement(
  3586. 'html_editor',
  3587. 'post_text',
  3588. get_lang('Text'),
  3589. null,
  3590. api_is_allowed_to_edit(null, true) ? [
  3591. 'ToolbarSet' => 'Forum',
  3592. 'Width' => '100%',
  3593. 'Height' => '400',
  3594. ] : [
  3595. 'ToolbarSet' => 'ForumStudent',
  3596. 'Width' => '100%',
  3597. 'Height' => '400',
  3598. 'UserStatus' => 'student',
  3599. ]
  3600. );
  3601. $form->addRule('post_text', get_lang('ThisFieldIsRequired'), 'required');
  3602. $extraFields = new ExtraField('forum_post');
  3603. $extraFields->addElements($form, $current_post['post_id']);
  3604. $form->addButtonAdvancedSettings('advanced_params');
  3605. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  3606. if ($current_forum['moderated'] && api_is_allowed_to_edit(null, true)) {
  3607. $group = [];
  3608. $group[] = $form->createElement(
  3609. 'radio',
  3610. 'status',
  3611. null,
  3612. get_lang('Validated'),
  3613. 1
  3614. );
  3615. $group[] = $form->createElement(
  3616. 'radio',
  3617. 'status',
  3618. null,
  3619. get_lang('WaitingModeration'),
  3620. 2
  3621. );
  3622. $group[] = $form->createElement(
  3623. 'radio',
  3624. 'status',
  3625. null,
  3626. get_lang('Rejected'),
  3627. 3
  3628. );
  3629. $form->addGroup($group, 'status', get_lang('Status'));
  3630. }
  3631. $defaults['status']['status'] = isset($current_post['status']) && !empty($current_post['status']) ? $current_post['status'] : 2;
  3632. $form->addElement(
  3633. 'checkbox',
  3634. 'post_notification',
  3635. '',
  3636. get_lang('NotifyByEmail').' ('.$current_post['email'].')'
  3637. );
  3638. if (api_is_allowed_to_edit(null, true) &&
  3639. empty($current_post['post_parent_id'])
  3640. ) {
  3641. // The sticky checkbox only appears when it is the first post of a thread.
  3642. $form->addElement('checkbox', 'thread_sticky', '', get_lang('StickyPost'));
  3643. if ($current_thread['thread_sticky'] == 1) {
  3644. $defaults['thread_sticky'] = true;
  3645. }
  3646. }
  3647. $form->addElement('html', '</div>');
  3648. $form->addFile('user_upload[]', get_lang('Attachment'));
  3649. $form->addButton(
  3650. 'add_attachment',
  3651. get_lang('AddAttachment'),
  3652. 'paperclip',
  3653. 'default',
  3654. 'default',
  3655. null,
  3656. ['id' => 'reply-add-attachment']
  3657. );
  3658. $form->addButtonUpdate(get_lang('Modify'), 'SubmitPost');
  3659. // Setting the default values for the form elements.
  3660. $defaults['post_title'] = $current_post['post_title'];
  3661. $defaults['post_text'] = $current_post['post_text'];
  3662. if ($current_post['post_notification'] == 1) {
  3663. $defaults['post_notification'] = true;
  3664. }
  3665. if (!empty($form_values)) {
  3666. $defaults['post_notification'] = Security::remove_XSS($form_values['post_notification']);
  3667. $defaults['thread_sticky'] = Security::remove_XSS($form_values['thread_sticky']);
  3668. }
  3669. $form->setDefaults($defaults);
  3670. // The course admin can make a thread sticky (=appears with special icon and always on top).
  3671. $form->addRule('post_title', get_lang('ThisFieldIsRequired'), 'required');
  3672. // Validation or display
  3673. if ($form->validate()) {
  3674. $values = $form->exportValues();
  3675. $values['item_id'] = $current_post['post_id'];
  3676. $extraFieldValues = new ExtraFieldValue('forum_post');
  3677. $extraFieldValues->saveFieldValues($values);
  3678. store_edit_post($current_forum, $values);
  3679. } else {
  3680. // Delete from $_SESSION forum attachment from other posts
  3681. clearAttachedFiles($current_post['post_id']);
  3682. // Get forum attachment ajax table to add it to form
  3683. $fileData = getAttachmentsAjaxTable($current_post['post_id'], $current_forum['forum_id']);
  3684. $form->addElement('html', $fileData);
  3685. $form->display();
  3686. }
  3687. }
  3688. /**
  3689. * This function stores the edit of a post in the forum_post table.
  3690. *
  3691. * @param array
  3692. *
  3693. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3694. *
  3695. * @version february 2006, dokeos 1.8
  3696. */
  3697. function store_edit_post($forumInfo, $values)
  3698. {
  3699. $logInfo = [
  3700. 'tool' => TOOL_FORUM,
  3701. 'tool_id' => $_GET['forum'],
  3702. 'tool_id_detail' => $values['thread_id'],
  3703. 'action' => 'edit-post',
  3704. 'action_details' => 'post',
  3705. 'info' => $values['post_title'],
  3706. ];
  3707. Event::registerLog($logInfo);
  3708. $threadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  3709. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3710. $course_id = api_get_course_int_id();
  3711. //check if this post is the first of the thread
  3712. // First we check if the change affects the thread and if so we commit
  3713. // the changes (sticky and post_title=thread_title are relevant).
  3714. $posts = getPosts($forumInfo, $values['thread_id']);
  3715. $first_post = null;
  3716. if (!empty($posts) && count($posts) > 0 && isset($posts[0])) {
  3717. $first_post = $posts[0];
  3718. }
  3719. if (!empty($first_post) && $first_post['post_id'] == $values['post_id']) {
  3720. // Simple edit
  3721. $params = [
  3722. 'thread_title' => $values['post_title'],
  3723. 'thread_sticky' => isset($values['thread_sticky']) ? $values['thread_sticky'] : 0,
  3724. ];
  3725. $where = ['c_id = ? AND thread_id = ?' => [$course_id, $values['thread_id']]];
  3726. Database::update($threadTable, $params, $where);
  3727. }
  3728. $status = '';
  3729. $updateStatus = false;
  3730. if ($forumInfo['moderated']) {
  3731. if (api_is_allowed_to_edit(null, true)) {
  3732. $status = $values['status']['status'];
  3733. $updateStatus = true;
  3734. } else {
  3735. $status = CForumPost::STATUS_WAITING_MODERATION;
  3736. $updateStatus = true;
  3737. }
  3738. }
  3739. // Update the post_title and the post_text.
  3740. $params = [
  3741. 'post_title' => $values['post_title'],
  3742. 'post_text' => $values['post_text'],
  3743. 'post_notification' => isset($values['post_notification']) ? $values['post_notification'] : '',
  3744. ];
  3745. if ($updateStatus) {
  3746. $params['status'] = $status;
  3747. }
  3748. $where = ['c_id = ? AND post_id = ?' => [$course_id, $values['post_id']]];
  3749. Database::update($table_posts, $params, $where);
  3750. // Update attached files
  3751. if (!empty($_POST['file_ids']) && is_array($_POST['file_ids'])) {
  3752. foreach ($_POST['file_ids'] as $key => $id) {
  3753. editAttachedFile(
  3754. [
  3755. 'comment' => $_POST['file_comments'][$key],
  3756. 'post_id' => $values['post_id'],
  3757. ],
  3758. $id
  3759. );
  3760. }
  3761. }
  3762. if (!empty($values['remove_attach'])) {
  3763. delete_attachment($values['post_id']);
  3764. }
  3765. if (empty($values['id_attach'])) {
  3766. add_forum_attachment_file(
  3767. isset($values['file_comment']) ? $values['file_comment'] : null,
  3768. $values['post_id']
  3769. );
  3770. } else {
  3771. edit_forum_attachment_file(
  3772. isset($values['file_comment']) ? $values['file_comment'] : null,
  3773. $values['post_id'],
  3774. $values['id_attach']
  3775. );
  3776. }
  3777. $message = get_lang('EditPostStored').'<br />';
  3778. $message .= get_lang('ReturnTo').' <a href="viewforum.php?'.api_get_cidreq().'&forum='.intval($_GET['forum']).'&">'.get_lang('Forum').'</a><br />';
  3779. $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>';
  3780. Session::erase('formelements');
  3781. Session::erase('origin');
  3782. Session::erase('breadcrumbs');
  3783. Session::erase('addedresource');
  3784. Session::erase('addedresourceid');
  3785. echo Display::return_message($message, 'confirmation', false);
  3786. }
  3787. /**
  3788. * This function displays the firstname and lastname of the user as a link to the user tool.
  3789. *
  3790. * @param string names
  3791. * @ in_title : title tootip
  3792. *
  3793. * @return string HTML
  3794. *
  3795. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3796. *
  3797. * @version february 2006, dokeos 1.8
  3798. */
  3799. function display_user_link($user_id, $name, $origin = '', $in_title = '')
  3800. {
  3801. if ($user_id != 0) {
  3802. $userInfo = api_get_user_info($user_id);
  3803. return '<a href="'.$userInfo['profile_url'].'">'.Security::remove_XSS($userInfo['complete_name']).'</a>';
  3804. } else {
  3805. return $name.' ('.get_lang('Anonymous').')';
  3806. }
  3807. }
  3808. /**
  3809. * This function displays the user image from the profile, with a link to the user's details.
  3810. *
  3811. * @param int User's database ID
  3812. * @param string User's name
  3813. * @param string the origin where the forum is called (example : learnpath)
  3814. *
  3815. * @return string An HTML with the anchor and the image of the user
  3816. *
  3817. * @author Julio Montoya <gugli100@gmail.com>
  3818. */
  3819. function display_user_image($user_id, $name, $origin = '')
  3820. {
  3821. $userInfo = api_get_user_info($user_id);
  3822. $link = '<a href="'.(!empty($origin) ? '#' : $userInfo['profile_url']).'" '.(!empty($origin) ? 'target="_self"' : '').'>';
  3823. if ($user_id != 0) {
  3824. return $link.'<img src="'.$userInfo['avatar'].'" alt="'.$name.'" title="'.$name.'" /></a>';
  3825. } else {
  3826. return $link.Display::return_icon('unknown.jpg', $name).'</a>';
  3827. }
  3828. }
  3829. /**
  3830. * The thread view counter gets increased every time someone looks at the thread.
  3831. *
  3832. * @param int
  3833. *
  3834. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3835. *
  3836. * @version february 2006, dokeos 1.8
  3837. */
  3838. function increase_thread_view($thread_id)
  3839. {
  3840. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3841. $course_id = api_get_course_int_id();
  3842. $sql = "UPDATE $table_threads
  3843. SET thread_views = thread_views + 1
  3844. WHERE
  3845. c_id = $course_id AND
  3846. thread_id = '".intval($thread_id)."'";
  3847. Database::query($sql);
  3848. }
  3849. /**
  3850. * The relies counter gets increased every time somebody replies to the thread.
  3851. *
  3852. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3853. *
  3854. * @version february 2006, dokeos 1.8
  3855. *
  3856. * @param int $threadId
  3857. * @param string $lastPostId
  3858. * @param string $post_date
  3859. */
  3860. function updateThreadInfo($threadId, $lastPostId, $post_date)
  3861. {
  3862. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  3863. $course_id = api_get_course_int_id();
  3864. $sql = "UPDATE $table_threads SET
  3865. thread_replies = thread_replies+1,
  3866. thread_last_post = '".Database::escape_string($lastPostId)."',
  3867. thread_date = '".Database::escape_string($post_date)."'
  3868. WHERE
  3869. c_id = $course_id AND
  3870. thread_id='".Database::escape_string($threadId)."'"; // this needs to be cleaned first
  3871. Database::query($sql);
  3872. }
  3873. /**
  3874. * This function is used to find all the information about what's new in the forum tool.
  3875. *
  3876. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3877. *
  3878. * @version february 2006, dokeos 1.8
  3879. */
  3880. function get_whats_new()
  3881. {
  3882. $userId = api_get_user_id();
  3883. $course_id = api_get_course_int_id();
  3884. if (empty($course_id) || empty($userId)) {
  3885. return false;
  3886. }
  3887. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3888. $tracking_last_tool_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  3889. $tool = TOOL_FORUM;
  3890. $lastForumAccess = Session::read('last_forum_access');
  3891. if (!$lastForumAccess) {
  3892. $sql = "SELECT * FROM $tracking_last_tool_access
  3893. WHERE
  3894. access_user_id = $userId AND
  3895. c_id = $course_id AND
  3896. access_tool = '".Database::escape_string($tool)."'";
  3897. $result = Database::query($sql);
  3898. $row = Database::fetch_array($result);
  3899. Session::write('last_forum_access', $row['access_date']);
  3900. $lastForumAccess = $row['access_date'];
  3901. }
  3902. $whatsNew = Session::read('whatsnew_post_info');
  3903. if (!$whatsNew) {
  3904. if ($lastForumAccess != '') {
  3905. $postInfo = [];
  3906. $sql = "SELECT * FROM $table_posts
  3907. WHERE
  3908. c_id = $course_id AND
  3909. visible = 1 AND
  3910. post_date > '".Database::escape_string($lastForumAccess)."'";
  3911. $result = Database::query($sql);
  3912. while ($row = Database::fetch_array($result)) {
  3913. $postInfo[$row['forum_id']][$row['thread_id']][$row['post_id']] = $row['post_date'];
  3914. }
  3915. Session::write('whatsnew_post_info', $postInfo);
  3916. }
  3917. }
  3918. }
  3919. /**
  3920. * This function approves a post = change.
  3921. *
  3922. * @param int $post_id the id of the post that will be deleted
  3923. * @param string $action make the post visible or invisible
  3924. *
  3925. * @return string language variable
  3926. *
  3927. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3928. *
  3929. * @version february 2006, dokeos 1.8
  3930. */
  3931. function approve_post($post_id, $action)
  3932. {
  3933. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3934. $course_id = api_get_course_int_id();
  3935. if ($action == 'invisible') {
  3936. $visibility_value = 0;
  3937. }
  3938. if ($action == 'visible') {
  3939. $visibility_value = 1;
  3940. handle_mail_cue('post', $post_id);
  3941. }
  3942. $sql = "UPDATE $table_posts SET
  3943. visible='".Database::escape_string($visibility_value)."'
  3944. WHERE c_id = $course_id AND post_id='".Database::escape_string($post_id)."'";
  3945. $return = Database::query($sql);
  3946. if ($return) {
  3947. return 'PostVisibilityChanged';
  3948. }
  3949. }
  3950. /**
  3951. * This function retrieves all the unapproved messages for a given forum
  3952. * This is needed to display the icon that there are unapproved messages in that thread (only the courseadmin can see this).
  3953. *
  3954. * @param $forum_id the forum where we want to know the unapproved messages of
  3955. *
  3956. * @return array returns
  3957. *
  3958. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3959. *
  3960. * @version february 2006, dokeos 1.8
  3961. */
  3962. function get_unaproved_messages($forum_id)
  3963. {
  3964. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  3965. $course_id = api_get_course_int_id();
  3966. $return_array = [];
  3967. $sql = "SELECT DISTINCT thread_id FROM $table_posts
  3968. WHERE
  3969. c_id = $course_id AND
  3970. forum_id='".Database::escape_string($forum_id)."' AND
  3971. visible='0' ";
  3972. $result = Database::query($sql);
  3973. while ($row = Database::fetch_array($result)) {
  3974. $return_array[] = $row['thread_id'];
  3975. }
  3976. return $return_array;
  3977. }
  3978. /**
  3979. * This function sends the notification mails to everybody who stated that they wanted to be informed when a new post
  3980. * was added to a given thread.
  3981. *
  3982. * @param array reply information
  3983. *
  3984. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3985. *
  3986. * @version february 2006, dokeos 1.8
  3987. */
  3988. function send_notification_mails($forumId, $thread_id, $reply_info)
  3989. {
  3990. $table = Database::get_course_table(TABLE_FORUM_MAIL_QUEUE);
  3991. // First we need to check if
  3992. // 1. the forum category is visible
  3993. // 2. the forum is visible
  3994. // 3. the thread is visible
  3995. // 4. the reply is visible (=when there is)
  3996. $current_thread = get_thread_information($forumId, $thread_id);
  3997. $current_forum = get_forum_information($current_thread['forum_id'], $current_thread['c_id']);
  3998. $current_forum_category = null;
  3999. if (isset($current_forum['forum_category'])) {
  4000. $current_forum_category = get_forumcategory_information($current_forum['forum_category']);
  4001. }
  4002. $send_mails = false;
  4003. if ($current_thread['visibility'] == '1' &&
  4004. $current_forum['visibility'] == '1' &&
  4005. ($current_forum_category && $current_forum_category['visibility'] == '1') &&
  4006. $current_forum['approval_direct_post'] != '1'
  4007. ) {
  4008. $send_mails = true;
  4009. }
  4010. // The forum category, the forum, the thread and the reply are visible to the user
  4011. if ($send_mails && !empty($forumId)) {
  4012. $postId = isset($reply_info['new_post_id']) ? $reply_info['new_post_id'] : 0;
  4013. send_notifications($forumId, $thread_id, $postId);
  4014. } else {
  4015. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  4016. if (isset($current_forum['forum_id'])) {
  4017. $sql = "SELECT * FROM $table_notification
  4018. WHERE
  4019. c_id = ".api_get_course_int_id()." AND
  4020. (
  4021. forum_id = '".intval($current_forum['forum_id'])."' OR
  4022. thread_id = '".intval($thread_id)."'
  4023. ) ";
  4024. $result = Database::query($sql);
  4025. $user_id = api_get_user_id();
  4026. while ($row = Database::fetch_array($result)) {
  4027. $sql = "INSERT INTO $table (c_id, thread_id, post_id, user_id)
  4028. VALUES (".api_get_course_int_id().", '".intval($thread_id)."', '".intval($reply_info['new_post_id'])."', '$user_id' )";
  4029. Database::query($sql);
  4030. }
  4031. }
  4032. }
  4033. }
  4034. /**
  4035. * This function is called whenever something is made visible because there might
  4036. * be new posts and the user might have indicated that (s)he wanted to be
  4037. * informed about the new posts by mail.
  4038. *
  4039. * @param string $content Content type (post, thread, forum, forum_category)
  4040. * @param int $id Item DB ID of the corresponding content type
  4041. *
  4042. * @return string language variable
  4043. *
  4044. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4045. *
  4046. * @version february 2006, dokeos 1.8
  4047. */
  4048. function handle_mail_cue($content, $id)
  4049. {
  4050. $table_mailcue = Database::get_course_table(TABLE_FORUM_MAIL_QUEUE);
  4051. $table_forums = Database::get_course_table(TABLE_FORUM);
  4052. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4053. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4054. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  4055. $course_id = api_get_course_int_id();
  4056. $id = (int) $id;
  4057. /* If the post is made visible we only have to send mails to the people
  4058. who indicated that they wanted to be informed for that thread.*/
  4059. if ($content == 'post') {
  4060. // Getting the information about the post (need the thread_id).
  4061. $post_info = get_post_information($id);
  4062. $thread_id = (int) $post_info['thread_id'];
  4063. // Sending the mail to all the users that wanted to be informed for replies on this thread.
  4064. $sql = "SELECT users.firstname, users.lastname, users.user_id, users.email
  4065. FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  4066. WHERE
  4067. posts.c_id = $course_id AND
  4068. mailcue.c_id = $course_id AND
  4069. posts.thread_id = $thread_id AND
  4070. posts.post_notification = '1' AND
  4071. mailcue.thread_id = $thread_id AND
  4072. users.user_id = posts.poster_id AND
  4073. users.active = 1
  4074. GROUP BY users.email";
  4075. $result = Database::query($sql);
  4076. while ($row = Database::fetch_array($result)) {
  4077. $forumInfo = get_forum_information($post_info['forum_id']);
  4078. send_mail($row, $forumInfo, get_thread_information($post_info['forum_id'], $post_info['thread_id']), $post_info);
  4079. }
  4080. } elseif ($content == 'thread') {
  4081. // Sending the mail to all the users that wanted to be informed for replies on this thread.
  4082. $sql = "SELECT users.firstname, users.lastname, users.user_id, users.email, posts.forum_id
  4083. FROM $table_mailcue mailcue, $table_posts posts, $table_users users
  4084. WHERE
  4085. posts.c_id = $course_id AND
  4086. mailcue.c_id = $course_id AND
  4087. posts.thread_id = $id AND
  4088. posts.post_notification = '1' AND
  4089. mailcue.thread_id = $id AND
  4090. users.user_id = posts.poster_id AND
  4091. users.active = 1
  4092. GROUP BY users.email";
  4093. $result = Database::query($sql);
  4094. while ($row = Database::fetch_array($result)) {
  4095. $forumInfo = get_forum_information($row['forum_id']);
  4096. send_mail($row, $forumInfo, get_thread_information($row['forum_id'], $id));
  4097. }
  4098. // Deleting the relevant entries from the mailcue.
  4099. $sql = "DELETE FROM $table_mailcue
  4100. WHERE c_id = $course_id AND thread_id = $id";
  4101. Database::query($sql);
  4102. } elseif ($content == 'forum') {
  4103. $sql = "SELECT thread_id FROM $table_threads
  4104. WHERE c_id = $course_id AND forum_id = $id";
  4105. $result = Database::query($sql);
  4106. while ($row = Database::fetch_array($result)) {
  4107. handle_mail_cue('thread', $row['thread_id']);
  4108. }
  4109. } elseif ($content == 'forum_category') {
  4110. $sql = "SELECT forum_id FROM $table_forums
  4111. WHERE c_id = $course_id AND forum_category = $id";
  4112. $result = Database::query($sql);
  4113. while ($row = Database::fetch_array($result)) {
  4114. handle_mail_cue('forum', $row['forum_id']);
  4115. }
  4116. } else {
  4117. return get_lang('Error');
  4118. }
  4119. }
  4120. /**
  4121. * This function sends the mails for the mail notification.
  4122. *
  4123. * @param array
  4124. * @param array
  4125. * @param array
  4126. * @param array
  4127. *
  4128. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4129. *
  4130. * @version february 2006, dokeos 1.8
  4131. */
  4132. function send_mail($userInfo, $forumInfo, $thread_information, $postInfo = [])
  4133. {
  4134. if (empty($userInfo) || empty($forumInfo) || empty($thread_information)) {
  4135. return false;
  4136. }
  4137. $_course = api_get_course_info();
  4138. $user_id = api_get_user_id();
  4139. $thread_link = '';
  4140. if (isset($thread_information) && is_array($thread_information)) {
  4141. $thread_link = api_get_path(WEB_CODE_PATH).
  4142. 'forum/viewthread.php?'.api_get_cidreq().'&forum='.$thread_information['forum_id'].'&thread='.$thread_information['thread_id'];
  4143. }
  4144. $email_body = get_lang('Dear').' '.api_get_person_name($userInfo['firstname'], $userInfo['lastname'], null, PERSON_NAME_EMAIL_ADDRESS).", <br />\n\r";
  4145. $email_body .= get_lang('NewForumPost').': '.$forumInfo['forum_title'].' - '.$thread_information['thread_title']." <br />\n";
  4146. $courseId = api_get_configuration_value('global_forums_course_id');
  4147. $subject = get_lang('NewForumPost').' - '.$_course['official_code'].': '.$forumInfo['forum_title'].' - '.$thread_information['thread_title']." <br />\n";
  4148. $courseInfoTitle = get_lang('Course').': '.$_course['name'].' - ['.$_course['official_code']."] - <br />\n";
  4149. if (!empty($courseId) && $_course['real_id'] == $courseId) {
  4150. $subject = get_lang('NewForumPost').': '.$forumInfo['forum_title'].' - '.$thread_information['thread_title']." <br />\n";
  4151. $courseInfoTitle = " <br />\n";
  4152. }
  4153. $email_body .= $courseInfoTitle;
  4154. if (!empty($postInfo) && isset($postInfo['post_text'])) {
  4155. $text = cut(strip_tags($postInfo['post_text']), 100);
  4156. if (!empty($text)) {
  4157. $email_body .= get_lang('Message').": <br />\n ";
  4158. $email_body .= $text;
  4159. $email_body .= "<br /><br />\n";
  4160. }
  4161. }
  4162. $email_body .= get_lang('YouWantedToStayInformed')."<br />\n";
  4163. if (!empty($thread_link)) {
  4164. $email_body .= get_lang('ThreadCanBeFoundHere')." : <br /><a href=\"".$thread_link."\">".$thread_link."</a>\n";
  4165. }
  4166. if ($userInfo['user_id'] != $user_id) {
  4167. MessageManager::send_message(
  4168. $userInfo['user_id'],
  4169. $subject,
  4170. $email_body,
  4171. [],
  4172. [],
  4173. null,
  4174. null,
  4175. null,
  4176. null,
  4177. $user_id
  4178. );
  4179. }
  4180. }
  4181. /**
  4182. * This function displays the form for moving a thread to a different (already existing) forum.
  4183. *
  4184. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4185. *
  4186. * @version february 2006, dokeos 1.8
  4187. */
  4188. function move_thread_form()
  4189. {
  4190. $form = new FormValidator(
  4191. 'movepost',
  4192. 'post',
  4193. api_get_self().'?forum='.intval($_GET['forum']).'&thread='.intval($_GET['thread']).'&action='.Security::remove_XSS($_GET['action']).'&'.api_get_cidreq()
  4194. );
  4195. // The header for the form
  4196. $form->addElement('header', get_lang('MoveThread'));
  4197. // Invisible form: the thread_id
  4198. $form->addElement('hidden', 'thread_id', intval($_GET['thread']));
  4199. // the fora
  4200. $forum_categories = get_forum_categories();
  4201. $forums = get_forums();
  4202. $htmlcontent = '<div class="row">
  4203. <div class="label">
  4204. <span class="form_required">*</span>'.get_lang('MoveTo').'
  4205. </div>
  4206. <div class="formw">';
  4207. $htmlcontent .= '<select name="forum">';
  4208. foreach ($forum_categories as $key => $category) {
  4209. $htmlcontent .= '<optgroup label="'.$category['cat_title'].'">';
  4210. foreach ($forums as $key => $forum) {
  4211. if (isset($forum['forum_category'])) {
  4212. if ($forum['forum_category'] == $category['cat_id']) {
  4213. $htmlcontent .= '<option value="'.$forum['forum_id'].'">'.$forum['forum_title'].'</option>';
  4214. }
  4215. }
  4216. }
  4217. $htmlcontent .= '</optgroup>';
  4218. }
  4219. $htmlcontent .= "</select>";
  4220. $htmlcontent .= ' </div>
  4221. </div>';
  4222. $form->addElement('html', $htmlcontent);
  4223. // The OK button
  4224. $form->addButtonSave(get_lang('MoveThread'), 'SubmitForum');
  4225. // Validation or display
  4226. if ($form->validate()) {
  4227. $values = $form->exportValues();
  4228. if (isset($_POST['forum'])) {
  4229. store_move_thread($values);
  4230. }
  4231. } else {
  4232. $form->display();
  4233. }
  4234. }
  4235. /**
  4236. * This function displays the form for moving a post message to a different (already existing) or a new thread.
  4237. *
  4238. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4239. *
  4240. * @version february 2006, dokeos 1.8
  4241. */
  4242. function move_post_form()
  4243. {
  4244. $form = new FormValidator(
  4245. 'movepost',
  4246. 'post',
  4247. 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'])
  4248. );
  4249. // The header for the form
  4250. $form->addElement('header', '', get_lang('MovePost'));
  4251. // Invisible form: the post_id
  4252. $form->addElement('hidden', 'post_id', intval($_GET['post']));
  4253. // Dropdown list: Threads of this forum
  4254. $threads = get_threads($_GET['forum']);
  4255. //my_print_r($threads);
  4256. $threads_list[0] = get_lang('ANewThread');
  4257. foreach ($threads as $key => $value) {
  4258. $threads_list[$value['thread_id']] = $value['thread_title'];
  4259. }
  4260. $form->addElement('select', 'thread', get_lang('MoveToThread'), $threads_list);
  4261. $form->applyFilter('thread', 'html_filter');
  4262. // The OK button
  4263. $form->addButtonSave(get_lang('MovePost'), 'submit');
  4264. // Setting the rules
  4265. $form->addRule('thread', get_lang('ThisFieldIsRequired'), 'required');
  4266. // Validation or display
  4267. if ($form->validate()) {
  4268. $values = $form->exportValues();
  4269. store_move_post($values);
  4270. } else {
  4271. $form->display();
  4272. }
  4273. }
  4274. /**
  4275. * @param array
  4276. *
  4277. * @return string HTML language variable
  4278. *
  4279. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4280. *
  4281. * @version february 2006, dokeos 1.8
  4282. */
  4283. function store_move_post($values)
  4284. {
  4285. $_course = api_get_course_info();
  4286. $course_id = api_get_course_int_id();
  4287. $table_forums = Database::get_course_table(TABLE_FORUM);
  4288. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4289. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4290. if ($values['thread'] == '0') {
  4291. $current_post = get_post_information($values['post_id']);
  4292. // Storing a new thread.
  4293. $params = [
  4294. 'c_id' => $course_id,
  4295. 'thread_title' => $current_post['post_title'],
  4296. 'forum_id' => $current_post['forum_id'],
  4297. 'thread_poster_id' => $current_post['poster_id'],
  4298. 'thread_poster_name' => $current_post['poster_name'],
  4299. 'thread_last_post' => $values['post_id'],
  4300. 'thread_date' => $current_post['post_date'],
  4301. ];
  4302. $new_thread_id = Database::insert($table_threads, $params);
  4303. api_item_property_update(
  4304. $_course,
  4305. TOOL_FORUM_THREAD,
  4306. $new_thread_id,
  4307. 'visible',
  4308. $current_post['poster_id']
  4309. );
  4310. // Moving the post to the newly created thread.
  4311. $sql = "UPDATE $table_posts SET thread_id='".intval($new_thread_id)."', post_parent_id = NULL
  4312. WHERE c_id = $course_id AND post_id='".intval($values['post_id'])."'";
  4313. Database::query($sql);
  4314. // Resetting the parent_id of the thread to 0 for all those who had this moved post as parent.
  4315. $sql = "UPDATE $table_posts SET post_parent_id = NULL
  4316. WHERE c_id = $course_id AND post_parent_id='".intval($values['post_id'])."'";
  4317. Database::query($sql);
  4318. // Updating updating the number of threads in the forum.
  4319. $sql = "UPDATE $table_forums SET forum_threads=forum_threads+1
  4320. WHERE c_id = $course_id AND forum_id='".intval($current_post['forum_id'])."'";
  4321. Database::query($sql);
  4322. // Resetting the last post of the old thread and decreasing the number of replies and the thread.
  4323. $sql = "SELECT * FROM $table_posts
  4324. WHERE c_id = $course_id AND thread_id='".intval($current_post['thread_id'])."'
  4325. ORDER BY post_id DESC";
  4326. $result = Database::query($sql);
  4327. $row = Database::fetch_array($result);
  4328. $sql = "UPDATE $table_threads SET
  4329. thread_last_post='".$row['post_id']."',
  4330. thread_replies=thread_replies-1
  4331. WHERE
  4332. c_id = $course_id AND
  4333. thread_id='".intval($current_post['thread_id'])."'";
  4334. Database::query($sql);
  4335. } else {
  4336. // Moving to the chosen thread.
  4337. $sql = "SELECT thread_id FROM ".$table_posts."
  4338. WHERE c_id = $course_id AND post_id = '".$values['post_id']."' ";
  4339. $result = Database::query($sql);
  4340. $row = Database::fetch_array($result);
  4341. $original_thread_id = $row['thread_id'];
  4342. $sql = "SELECT thread_last_post FROM ".$table_threads."
  4343. WHERE c_id = $course_id AND thread_id = '".$original_thread_id."' ";
  4344. $result = Database::query($sql);
  4345. $row = Database::fetch_array($result);
  4346. $thread_is_last_post = $row['thread_last_post'];
  4347. // If is this thread, update the thread_last_post with the last one.
  4348. if ($thread_is_last_post == $values['post_id']) {
  4349. $sql = "SELECT post_id FROM ".$table_posts."
  4350. WHERE c_id = $course_id AND thread_id = '".$original_thread_id."' AND post_id <> '".$values['post_id']."'
  4351. ORDER BY post_date DESC LIMIT 1";
  4352. $result = Database::query($sql);
  4353. $row = Database::fetch_array($result);
  4354. $thread_new_last_post = $row['post_id'];
  4355. $sql = "UPDATE ".$table_threads." SET thread_last_post = '".$thread_new_last_post."'
  4356. WHERE c_id = $course_id AND thread_id = '".$original_thread_id."' ";
  4357. Database::query($sql);
  4358. }
  4359. $sql = "UPDATE $table_threads SET thread_replies=thread_replies-1
  4360. WHERE c_id = $course_id AND thread_id='".$original_thread_id."'";
  4361. Database::query($sql);
  4362. // moving to the chosen thread
  4363. $sql = "UPDATE $table_posts SET thread_id='".intval($_POST['thread'])."', post_parent_id = NULL
  4364. WHERE c_id = $course_id AND post_id='".intval($values['post_id'])."'";
  4365. Database::query($sql);
  4366. // resetting the parent_id of the thread to 0 for all those who had this moved post as parent
  4367. $sql = "UPDATE $table_posts SET post_parent_id = NULL
  4368. WHERE c_id = $course_id AND post_parent_id='".intval($values['post_id'])."'";
  4369. Database::query($sql);
  4370. $sql = "UPDATE $table_threads SET thread_replies=thread_replies+1
  4371. WHERE c_id = $course_id AND thread_id='".intval($_POST['thread'])."'";
  4372. Database::query($sql);
  4373. }
  4374. return get_lang('ThreadMoved');
  4375. }
  4376. /**
  4377. * @param array
  4378. *
  4379. * @return string HTML language variable
  4380. *
  4381. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4382. *
  4383. * @version february 2006, dokeos 1.8
  4384. */
  4385. function store_move_thread($values)
  4386. {
  4387. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4388. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4389. $courseId = api_get_course_int_id();
  4390. $sessionId = api_get_session_id();
  4391. $forumId = intval($_POST['forum']);
  4392. $threadId = intval($_POST['thread_id']);
  4393. $forumInfo = get_forums($forumId);
  4394. // Change the thread table: Setting the forum_id to the new forum.
  4395. $sql = "UPDATE $table_threads SET forum_id = $forumId
  4396. WHERE c_id = $courseId AND thread_id = $threadId";
  4397. Database::query($sql);
  4398. // Changing all the posts of the thread: setting the forum_id to the new forum.
  4399. $sql = "UPDATE $table_posts SET forum_id = $forumId
  4400. WHERE c_id = $courseId AND thread_id= $threadId";
  4401. Database::query($sql);
  4402. // Fix group id, if forum is moved to a different group
  4403. if (!empty($forumInfo['to_group_id'])) {
  4404. $groupId = $forumInfo['to_group_id'];
  4405. $item = api_get_item_property_info(
  4406. $courseId,
  4407. TABLE_FORUM_THREAD,
  4408. $threadId,
  4409. $sessionId,
  4410. $groupId
  4411. );
  4412. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4413. $sessionCondition = api_get_session_condition($sessionId);
  4414. if (!empty($item)) {
  4415. if ($item['to_group_id'] != $groupId) {
  4416. $sql = "UPDATE $table
  4417. SET to_group_id = $groupId
  4418. WHERE
  4419. tool = '".TABLE_FORUM_THREAD."' AND
  4420. c_id = $courseId AND
  4421. ref = ".$item['ref']."
  4422. $sessionCondition
  4423. ";
  4424. Database::query($sql);
  4425. }
  4426. } else {
  4427. $sql = "UPDATE $table
  4428. SET to_group_id = $groupId
  4429. WHERE
  4430. tool = '".TABLE_FORUM_THREAD."' AND
  4431. c_id = $courseId AND
  4432. ref = ".$threadId."
  4433. $sessionCondition
  4434. ";
  4435. Database::query($sql);
  4436. }
  4437. }
  4438. return get_lang('ThreadMoved');
  4439. }
  4440. /**
  4441. * Prepares a string for displaying by highlighting the search results inside, if any.
  4442. *
  4443. * @param string $input the input string
  4444. *
  4445. * @return string the same string with highlighted hits inside
  4446. *
  4447. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, February 2006 - the initial version.
  4448. * @author Ivan Tcholakov, March 2011 - adaptation for Chamilo LMS.
  4449. */
  4450. function prepare4display($input)
  4451. {
  4452. static $highlightcolors = ['yellow', '#33CC33', '#3399CC', '#9999FF', '#33CC33'];
  4453. static $search;
  4454. if (!isset($search)) {
  4455. if (isset($_POST['search_term'])) {
  4456. $search = $_POST['search_term']; // No html at all.
  4457. } elseif (isset($_GET['search'])) {
  4458. $search = $_GET['search'];
  4459. } else {
  4460. $search = '';
  4461. }
  4462. }
  4463. if (!empty($search)) {
  4464. if (strstr($search, '+')) {
  4465. $search_terms = explode('+', $search);
  4466. } else {
  4467. $search_terms[] = trim($search);
  4468. }
  4469. $counter = 0;
  4470. foreach ($search_terms as $key => $search_term) {
  4471. $input = api_preg_replace(
  4472. '/'.preg_quote(trim($search_term), '/').'/i',
  4473. '<span style="background-color: '.$highlightcolors[$counter].'">$0</span>',
  4474. $input
  4475. );
  4476. $counter++;
  4477. }
  4478. }
  4479. // TODO: Security should be implemented outside this function.
  4480. // Change this to COURSEMANAGERLOWSECURITY or COURSEMANAGER to lower filtering and allow more styles
  4481. // (see comments of Security::remove_XSS() method to learn about other levels).
  4482. return Security::remove_XSS($input, STUDENT, true);
  4483. }
  4484. /**
  4485. * Display the search form for the forum and display the search results.
  4486. *
  4487. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  4488. *
  4489. * @version march 2008, dokeos 1.8.5
  4490. */
  4491. function forum_search()
  4492. {
  4493. $form = new FormValidator(
  4494. 'forumsearch',
  4495. 'post',
  4496. 'forumsearch.php?'.api_get_cidreq()
  4497. );
  4498. // Setting the form elements.
  4499. $form->addElement('header', '', get_lang('ForumSearch'));
  4500. $form->addElement('text', 'search_term', get_lang('SearchTerm'), ['autofocus']);
  4501. $form->applyFilter('search_term', 'html_filter');
  4502. $form->addElement('static', 'search_information', '', get_lang('ForumSearchInformation'));
  4503. $form->addButtonSearch(get_lang('Search'));
  4504. // Setting the rules.
  4505. $form->addRule('search_term', get_lang('ThisFieldIsRequired'), 'required');
  4506. $form->addRule('search_term', get_lang('TooShort'), 'minlength', 3);
  4507. // Validation or display.
  4508. if ($form->validate()) {
  4509. $values = $form->exportValues();
  4510. $form->setDefaults($values);
  4511. $form->display();
  4512. // Display the search results.
  4513. display_forum_search_results(stripslashes($values['search_term']));
  4514. } else {
  4515. $form->display();
  4516. }
  4517. }
  4518. /**
  4519. * Display the search results.
  4520. *
  4521. * @param string
  4522. * @param string $search_term
  4523. *
  4524. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  4525. *
  4526. * @version march 2008, dokeos 1.8.5
  4527. */
  4528. function display_forum_search_results($search_term)
  4529. {
  4530. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4531. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4532. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4533. $session_id = api_get_session_id();
  4534. $course_id = api_get_course_int_id();
  4535. // Defining the search strings as an array.
  4536. if (strstr($search_term, '+')) {
  4537. $search_terms = explode('+', $search_term);
  4538. } else {
  4539. $search_terms[] = $search_term;
  4540. }
  4541. // Search restriction.
  4542. foreach ($search_terms as $value) {
  4543. $search_restriction[] = "
  4544. (
  4545. posts.post_title LIKE '%".Database::escape_string(trim($value))."%' OR
  4546. posts.post_text LIKE '%".Database::escape_string(trim($value))."%'
  4547. )";
  4548. }
  4549. $sessionCondition = api_get_session_condition(
  4550. $session_id,
  4551. true,
  4552. false,
  4553. 'item_property.session_id'
  4554. );
  4555. $sql = "SELECT posts.*
  4556. FROM $table_posts posts
  4557. INNER JOIN $table_threads threads
  4558. ON (posts.thread_id = threads.thread_id AND posts.c_id = threads.c_id)
  4559. INNER JOIN $table_item_property item_property
  4560. ON (item_property.ref = threads.thread_id AND item_property.c_id = threads.c_id)
  4561. WHERE
  4562. posts.c_id = $course_id AND
  4563. item_property.c_id = $course_id AND
  4564. item_property.visibility = 1
  4565. $sessionCondition AND
  4566. posts.visible = 1 AND
  4567. item_property.tool = '".TOOL_FORUM_THREAD."' AND
  4568. ".implode(' AND ', $search_restriction)."
  4569. GROUP BY posts.post_id";
  4570. // Getting all the information of the forum categories.
  4571. $forum_categories_list = get_forum_categories();
  4572. // Getting all the information of the forums.
  4573. $forum_list = get_forums();
  4574. $result = Database::query($sql);
  4575. $search_results = [];
  4576. while ($row = Database::fetch_array($result, 'ASSOC')) {
  4577. $forumId = $row['forum_id'];
  4578. $forumData = get_forums($forumId);
  4579. $category = isset($forum_categories_list[$forumData['forum_category']]) ? $forum_categories_list[$forumData['forum_category']] : null;
  4580. $display_result = false;
  4581. /*
  4582. We only show it when
  4583. 1. forum category is visible
  4584. 2. forum is visible
  4585. 3. thread is visible (to do)
  4586. 4. post is visible
  4587. */
  4588. if (!api_is_allowed_to_edit(null, true)) {
  4589. if (!empty($category)) {
  4590. if ($category['visibility'] == '1' && $forumData['visibility'] == '1') {
  4591. $display_result = true;
  4592. }
  4593. } else {
  4594. if ($forumData['visible'] == '1') {
  4595. $display_result = true;
  4596. }
  4597. }
  4598. } else {
  4599. $display_result = true;
  4600. }
  4601. if ($display_result) {
  4602. $categoryName = !empty($category) ? $category['cat_title'] : '';
  4603. $search_results_item = '<li><a href="viewforumcategory.php?'.api_get_cidreq().'&forumcategory='.$forumData['forum_category'].'&search='.urlencode($search_term).'">'.
  4604. prepare4display($categoryName).'</a> &gt; ';
  4605. $search_results_item .= '<a href="viewforum.php?'.api_get_cidreq().'&forum='.$forumId.'&search='.urlencode($search_term).'">'.
  4606. prepare4display($forum_list[$row['forum_id']]['forum_title']).'</a> &gt; ';
  4607. $search_results_item .= '<a href="viewthread.php?'.api_get_cidreq().'&forum='.$forumId.'&thread='.$row['thread_id'].'&search='.urlencode($search_term).'">'.
  4608. prepare4display($row['post_title']).'</a>';
  4609. $search_results_item .= '<br />';
  4610. if (api_strlen($row['post_title']) > 200) {
  4611. $search_results_item .= prepare4display(api_substr(strip_tags($row['post_title']), 0, 200)).'...';
  4612. } else {
  4613. $search_results_item .= prepare4display($row['post_title']);
  4614. }
  4615. $search_results_item .= '</li>';
  4616. $search_results[] = $search_results_item;
  4617. }
  4618. }
  4619. echo '<legend>'.count($search_results).' '.get_lang('ForumSearchResults').'</legend>';
  4620. echo '<ol>';
  4621. if ($search_results) {
  4622. echo implode($search_results);
  4623. }
  4624. echo '</ol>';
  4625. }
  4626. /**
  4627. * Return the link to the forum search page.
  4628. *
  4629. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  4630. *
  4631. * @version April 2008, dokeos 1.8.5
  4632. */
  4633. function search_link()
  4634. {
  4635. $return = '';
  4636. $origin = api_get_origin();
  4637. if ($origin != 'learnpath') {
  4638. $return = '<a href="forumsearch.php?'.api_get_cidreq().'&action=search"> ';
  4639. $return .= Display::return_icon('search.png', get_lang('Search'), '', ICON_SIZE_MEDIUM).'</a>';
  4640. if (!empty($_GET['search'])) {
  4641. $return .= ': '.Security::remove_XSS($_GET['search']).' ';
  4642. $url = api_get_self().'?';
  4643. $url_parameter = [];
  4644. foreach ($_GET as $key => $value) {
  4645. if ($key != 'search') {
  4646. $url_parameter[] = Security::remove_XSS($key).'='.Security::remove_XSS($value);
  4647. }
  4648. }
  4649. $url = $url.implode('&', $url_parameter);
  4650. $return .= '<a href="'.$url.'">'.Display::return_icon('delete.gif', get_lang('RemoveSearchResults')).'</a>';
  4651. }
  4652. }
  4653. return $return;
  4654. }
  4655. /**
  4656. * This function adds an attachment file into a forum.
  4657. *
  4658. * @param string $file_comment a comment about file
  4659. * @param int $last_id from forum_post table
  4660. *
  4661. * @return false|null
  4662. */
  4663. function add_forum_attachment_file($file_comment, $last_id)
  4664. {
  4665. $_course = api_get_course_info();
  4666. $agenda_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4667. if (empty($_FILES['user_upload'])) {
  4668. return false;
  4669. }
  4670. $filesData = [];
  4671. if (!is_array($_FILES['user_upload']['name'])) {
  4672. $filesData[] = $_FILES['user_upload'];
  4673. } else {
  4674. $fileCount = count($_FILES['user_upload']['name']);
  4675. $fileKeys = array_keys($_FILES['user_upload']);
  4676. for ($i = 0; $i < $fileCount; $i++) {
  4677. foreach ($fileKeys as $key) {
  4678. $filesData[$i][$key] = $_FILES['user_upload'][$key][$i];
  4679. }
  4680. }
  4681. }
  4682. foreach ($filesData as $attachment) {
  4683. if (empty($attachment['name'])) {
  4684. continue;
  4685. }
  4686. $upload_ok = process_uploaded_file($attachment);
  4687. if (!$upload_ok) {
  4688. continue;
  4689. }
  4690. $course_dir = $_course['path'].'/upload/forum';
  4691. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4692. $updir = $sys_course_path.$course_dir;
  4693. // Try to add an extension to the file if it hasn't one.
  4694. $new_file_name = add_ext_on_mime(
  4695. stripslashes($attachment['name']),
  4696. $attachment['type']
  4697. );
  4698. // User's file name
  4699. $file_name = $attachment['name'];
  4700. if (!filter_extension($new_file_name)) {
  4701. Display::addFlash(
  4702. Display::return_message(
  4703. get_lang('UplUnableToSaveFileFilteredExtension'),
  4704. 'error'
  4705. )
  4706. );
  4707. return;
  4708. }
  4709. $new_file_name = uniqid('');
  4710. $new_path = $updir.'/'.$new_file_name;
  4711. $result = @move_uploaded_file($attachment['tmp_name'], $new_path);
  4712. $safe_file_comment = Database::escape_string($file_comment);
  4713. $safe_file_name = Database::escape_string($file_name);
  4714. $safe_new_file_name = Database::escape_string($new_file_name);
  4715. $last_id = intval($last_id);
  4716. // Storing the attachments if any.
  4717. if (!$result) {
  4718. return;
  4719. }
  4720. $last_id_file = Database::insert(
  4721. $agenda_forum_attachment,
  4722. [
  4723. 'c_id' => api_get_course_int_id(),
  4724. 'filename' => $safe_file_name,
  4725. 'comment' => $safe_file_comment,
  4726. 'path' => $safe_new_file_name,
  4727. 'post_id' => $last_id,
  4728. 'size' => intval($attachment['size']),
  4729. ]
  4730. );
  4731. api_item_property_update(
  4732. $_course,
  4733. TOOL_FORUM_ATTACH,
  4734. $last_id_file,
  4735. 'ForumAttachmentAdded',
  4736. api_get_user_id()
  4737. );
  4738. }
  4739. }
  4740. /**
  4741. * This function edits an attachment file into a forum.
  4742. *
  4743. * @param string $file_comment a comment about file
  4744. * @param int $post_id
  4745. * @param int $id_attach attachment file Id
  4746. */
  4747. function edit_forum_attachment_file($file_comment, $post_id, $id_attach)
  4748. {
  4749. $_course = api_get_course_info();
  4750. $table_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4751. $course_id = api_get_course_int_id();
  4752. $filesData = [];
  4753. if (!is_array($_FILES['user_upload']['name'])) {
  4754. $filesData[] = $_FILES['user_upload'];
  4755. } else {
  4756. $fileCount = count($_FILES['user_upload']['name']);
  4757. $fileKeys = array_keys($_FILES['user_upload']);
  4758. for ($i = 0; $i < $fileCount; $i++) {
  4759. foreach ($fileKeys as $key) {
  4760. $filesData[$i][$key] = $_FILES['user_upload'][$key][$i];
  4761. }
  4762. }
  4763. }
  4764. foreach ($filesData as $attachment) {
  4765. if (empty($attachment['name'])) {
  4766. continue;
  4767. }
  4768. $upload_ok = process_uploaded_file($attachment);
  4769. if (!$upload_ok) {
  4770. continue;
  4771. }
  4772. $course_dir = $_course['path'].'/upload/forum';
  4773. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4774. $updir = $sys_course_path.$course_dir;
  4775. // Try to add an extension to the file if it hasn't one.
  4776. $new_file_name = add_ext_on_mime(stripslashes($attachment['name']), $attachment['type']);
  4777. // User's file name
  4778. $file_name = $attachment['name'];
  4779. if (!filter_extension($new_file_name)) {
  4780. Display::addFlash(
  4781. Display::return_message(
  4782. get_lang('UplUnableToSaveFileFilteredExtension'),
  4783. 'error'
  4784. )
  4785. );
  4786. } else {
  4787. $new_file_name = uniqid('');
  4788. $new_path = $updir.'/'.$new_file_name;
  4789. $result = @move_uploaded_file($attachment['tmp_name'], $new_path);
  4790. $safe_file_comment = Database::escape_string($file_comment);
  4791. $safe_file_name = Database::escape_string($file_name);
  4792. $safe_new_file_name = Database::escape_string($new_file_name);
  4793. $safe_post_id = (int) $post_id;
  4794. $safe_id_attach = (int) $id_attach;
  4795. // Storing the attachments if any.
  4796. if ($result) {
  4797. $sql = "UPDATE $table_forum_attachment
  4798. SET
  4799. filename = '$safe_file_name',
  4800. comment = '$safe_file_comment',
  4801. path = '$safe_new_file_name',
  4802. post_id = '$safe_post_id',
  4803. size ='".$attachment['size']."'
  4804. WHERE c_id = $course_id AND id = '$safe_id_attach'";
  4805. Database::query($sql);
  4806. api_item_property_update(
  4807. $_course,
  4808. TOOL_FORUM_ATTACH,
  4809. $safe_id_attach,
  4810. 'ForumAttachmentUpdated',
  4811. api_get_user_id()
  4812. );
  4813. }
  4814. }
  4815. }
  4816. }
  4817. /**
  4818. * Show a list with all the attachments according to the post's id.
  4819. *
  4820. * @param int $postId
  4821. *
  4822. * @return array with the post info
  4823. *
  4824. * @author Julio Montoya
  4825. *
  4826. * @version avril 2008, dokeos 1.8.5
  4827. */
  4828. function get_attachment($postId)
  4829. {
  4830. $table = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4831. $course_id = api_get_course_int_id();
  4832. $row = [];
  4833. $postId = (int) $postId;
  4834. if (empty($postId)) {
  4835. return [];
  4836. }
  4837. $sql = "SELECT iid, path, filename, comment
  4838. FROM $table
  4839. WHERE c_id = $course_id AND post_id = $postId";
  4840. $result = Database::query($sql);
  4841. if (Database::num_rows($result) != 0) {
  4842. $row = Database::fetch_array($result);
  4843. }
  4844. return $row;
  4845. }
  4846. /**
  4847. * @param int $postId
  4848. *
  4849. * @return array
  4850. */
  4851. function getAllAttachment($postId)
  4852. {
  4853. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4854. $courseId = api_get_course_int_id();
  4855. $postId = (int) $postId;
  4856. if (empty($postId)) {
  4857. return [];
  4858. }
  4859. $columns = ['iid', 'path', 'filename', 'comment'];
  4860. $conditions = [
  4861. 'where' => [
  4862. 'c_id = ? AND post_id = ?' => [$courseId, $postId],
  4863. ],
  4864. ];
  4865. $array = Database::select(
  4866. $columns,
  4867. $forumAttachmentTable,
  4868. $conditions,
  4869. 'all',
  4870. 'ASSOC'
  4871. );
  4872. return $array;
  4873. }
  4874. /**
  4875. * Delete the all the attachments from the DB and the file according to the post's id or attach id(optional).
  4876. *
  4877. * @param int $post_id
  4878. * @param int $id_attach
  4879. *
  4880. * @return int
  4881. *
  4882. * @author Julio Montoya
  4883. *
  4884. * @version october 2014, chamilo 1.9.8
  4885. */
  4886. function delete_attachment($post_id, $id_attach = 0)
  4887. {
  4888. $_course = api_get_course_info();
  4889. $forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  4890. $course_id = api_get_course_int_id();
  4891. $cond = (!empty($id_attach)) ? " iid = ".(int) $id_attach."" : " post_id = ".(int) $post_id."";
  4892. $sql = "SELECT path FROM $forum_table_attachment WHERE c_id = $course_id AND $cond";
  4893. $res = Database::query($sql);
  4894. $row = Database::fetch_array($res);
  4895. $course_dir = $_course['path'].'/upload/forum';
  4896. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  4897. $updir = $sys_course_path.$course_dir;
  4898. $my_path = isset($row['path']) ? $row['path'] : null;
  4899. $file = $updir.'/'.$my_path;
  4900. if (Security::check_abs_path($file, $updir)) {
  4901. @unlink($file);
  4902. }
  4903. // Delete from forum_attachment table.
  4904. $sql = "DELETE FROM $forum_table_attachment
  4905. WHERE c_id = $course_id AND $cond ";
  4906. $result = Database::query($sql);
  4907. if ($result !== false) {
  4908. $affectedRows = Database::affected_rows($result);
  4909. } else {
  4910. $affectedRows = 0;
  4911. }
  4912. // Update item_property.
  4913. api_item_property_update(
  4914. $_course,
  4915. TOOL_FORUM_ATTACH,
  4916. $id_attach,
  4917. 'ForumAttachmentDelete',
  4918. api_get_user_id()
  4919. );
  4920. if (!empty($result) && !empty($id_attach)) {
  4921. Display::addFlash(Display::return_message(get_lang('AttachmentFileDeleteSuccess'), 'confirmation'));
  4922. }
  4923. return $affectedRows;
  4924. }
  4925. /**
  4926. * This function gets all the forum information of the all the forum of the group.
  4927. *
  4928. * @param array $groupInfo the id of the group we need the fora of (see forum.forum_of_group)
  4929. *
  4930. * @return array
  4931. *
  4932. * @todo this is basically the same code as the get_forums function. Consider merging the two.
  4933. */
  4934. function get_forums_of_group($groupInfo)
  4935. {
  4936. $table_forums = Database::get_course_table(TABLE_FORUM);
  4937. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD);
  4938. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  4939. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4940. $course_id = api_get_course_int_id();
  4941. $groupId = (int) $groupInfo['id'];
  4942. // Student
  4943. // Select all the forum information of all forums (that are visible to students).
  4944. $sql = "SELECT * FROM $table_forums forum
  4945. INNER JOIN $table_item_property item_properties
  4946. ON (forum.forum_id = item_properties.ref AND item_properties.c_id = forum.c_id)
  4947. WHERE
  4948. forum.forum_of_group = $groupId AND
  4949. forum.c_id = $course_id AND
  4950. item_properties.c_id = $course_id AND
  4951. item_properties.visibility = 1 AND
  4952. item_properties.tool = '".TOOL_FORUM."'
  4953. ORDER BY forum.forum_order ASC";
  4954. // Select the number of threads of the forums (only the threads that are visible).
  4955. $sql2 = "SELECT
  4956. count(thread_id) AS number_of_threads,
  4957. threads.forum_id
  4958. FROM $table_threads threads
  4959. INNER JOIN $table_item_property item_properties
  4960. ON (threads.thread_id = item_properties.ref AND item_properties.c_id = threads.c_id)
  4961. WHERE
  4962. threads.c_id = $course_id AND
  4963. item_properties.c_id = $course_id AND
  4964. item_properties.visibility = 1 AND
  4965. item_properties.tool='".TOOL_FORUM_THREAD."'
  4966. GROUP BY threads.forum_id";
  4967. // Select the number of posts of the forum (post that are visible and that are in a thread that is visible).
  4968. $sql3 = "SELECT count(post_id) AS number_of_posts, posts.forum_id
  4969. FROM $table_posts posts
  4970. INNER JOIN $table_threads threads
  4971. ON (posts.thread_id = threads.thread_id AND posts.c_id = threads.c_id)
  4972. INNER JOIN $table_item_property item_properties
  4973. ON (threads.thread_id = item_properties.ref AND item_properties.c_id = threads.c_id)
  4974. WHERE
  4975. posts.visible=1 AND
  4976. posts.c_id = $course_id AND
  4977. item_properties.c_id = $course_id AND
  4978. threads.c_id = $course_id AND
  4979. item_properties.visibility = 1 AND
  4980. item_properties.tool='".TOOL_FORUM_THREAD."'
  4981. GROUP BY threads.forum_id";
  4982. // Course Admin
  4983. if (api_is_allowed_to_edit()) {
  4984. // Select all the forum information of all forums (that are not deleted).
  4985. $sql = "SELECT *
  4986. FROM $table_forums forum
  4987. INNER JOIN $table_item_property item_properties
  4988. ON (forum.forum_id = item_properties.ref AND item_properties.c_id = forum.c_id)
  4989. WHERE
  4990. forum.forum_of_group = $groupId AND
  4991. forum.c_id = $course_id AND
  4992. item_properties.c_id = $course_id AND
  4993. item_properties.visibility <> 2 AND
  4994. item_properties.tool = '".TOOL_FORUM."'
  4995. ORDER BY forum_order ASC";
  4996. // Select the number of threads of the forums (only the threads that are not deleted).
  4997. $sql2 = "SELECT count(thread_id) AS number_of_threads, threads.forum_id
  4998. FROM $table_threads threads
  4999. INNER JOIN $table_item_property item_properties
  5000. ON (threads.thread_id=item_properties.ref AND item_properties.c_id = threads.c_id)
  5001. WHERE
  5002. threads.c_id = $course_id AND
  5003. item_properties.c_id = $course_id AND
  5004. item_properties.visibility <> 2 AND
  5005. item_properties.tool='".TOOL_FORUM_THREAD."'
  5006. GROUP BY threads.forum_id";
  5007. // Select the number of posts of the forum.
  5008. $sql3 = "SELECT count(post_id) AS number_of_posts, forum_id
  5009. FROM $table_posts
  5010. WHERE c_id = $course_id
  5011. GROUP BY forum_id";
  5012. }
  5013. // Handling all the forum information.
  5014. $result = Database::query($sql);
  5015. $forum_list = [];
  5016. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5017. $forum_list[$row['forum_id']] = $row;
  5018. }
  5019. // Handling the thread count information.
  5020. $result2 = Database::query($sql2);
  5021. while ($row2 = Database::fetch_array($result2, 'ASSOC')) {
  5022. if (is_array($forum_list)) {
  5023. if (array_key_exists($row2['forum_id'], $forum_list)) {
  5024. $forum_list[$row2['forum_id']]['number_of_threads'] = $row2['number_of_threads'];
  5025. }
  5026. }
  5027. }
  5028. // Handling the post count information.
  5029. $result3 = Database::query($sql3);
  5030. while ($row3 = Database::fetch_array($result3, 'ASSOC')) {
  5031. if (is_array($forum_list)) {
  5032. if (array_key_exists($row3['forum_id'], $forum_list)) {
  5033. // This is needed because sql3 takes also the deleted forums into account.
  5034. $forum_list[$row3['forum_id']]['number_of_posts'] = $row3['number_of_posts'];
  5035. }
  5036. }
  5037. }
  5038. // Finding the last post information
  5039. // (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname).
  5040. if (!empty($forum_list)) {
  5041. foreach ($forum_list as $key => $value) {
  5042. $last_post_info_of_forum = get_last_post_information($key, api_is_allowed_to_edit());
  5043. if ($last_post_info_of_forum) {
  5044. $forum_list[$key]['last_post_id'] = $last_post_info_of_forum['last_post_id'];
  5045. $forum_list[$key]['last_poster_id'] = $last_post_info_of_forum['last_poster_id'];
  5046. $forum_list[$key]['last_post_date'] = $last_post_info_of_forum['last_post_date'];
  5047. $forum_list[$key]['last_poster_name'] = $last_post_info_of_forum['last_poster_name'];
  5048. $forum_list[$key]['last_poster_lastname'] = $last_post_info_of_forum['last_poster_lastname'];
  5049. $forum_list[$key]['last_poster_firstname'] = $last_post_info_of_forum['last_poster_firstname'];
  5050. }
  5051. }
  5052. }
  5053. return $forum_list;
  5054. }
  5055. /**
  5056. * This function stores which users have to be notified of which forums or threads.
  5057. *
  5058. * @param string $content does the user want to be notified about a forum or about a thread
  5059. * @param int $id the id of the forum or thread
  5060. *
  5061. * @return string language variable
  5062. *
  5063. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5064. *
  5065. * @version May 2008, dokeos 1.8.5
  5066. *
  5067. * @since May 2008, dokeos 1.8.5
  5068. */
  5069. function set_notification($content, $id, $add_only = false)
  5070. {
  5071. $_user = api_get_user_info();
  5072. // Database table definition
  5073. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  5074. $course_id = api_get_course_int_id();
  5075. // Which database field do we have to store the id in?
  5076. if ($content == 'forum') {
  5077. $database_field = 'forum_id';
  5078. } else {
  5079. $database_field = 'thread_id';
  5080. }
  5081. // First we check if the notification is already set for this.
  5082. $sql = "SELECT * FROM $table_notification
  5083. WHERE
  5084. c_id = $course_id AND
  5085. $database_field = '".Database::escape_string($id)."' AND
  5086. user_id = '".intval($_user['user_id'])."'";
  5087. $result = Database::query($sql);
  5088. $total = Database::num_rows($result);
  5089. // If the user did not indicate that (s)he wanted to be notified already
  5090. // then we store the notification request (to prevent double notification requests).
  5091. if ($total <= 0) {
  5092. $sql = "INSERT INTO $table_notification (c_id, $database_field, user_id)
  5093. VALUES (".$course_id.", '".Database::escape_string($id)."','".intval($_user['user_id'])."')";
  5094. Database::query($sql);
  5095. Session::erase('forum_notification');
  5096. getNotificationsPerUser(0, true);
  5097. return get_lang('YouWillBeNotifiedOfNewPosts');
  5098. } else {
  5099. if (!$add_only) {
  5100. $sql = "DELETE FROM $table_notification
  5101. WHERE
  5102. c_id = $course_id AND
  5103. $database_field = '".Database::escape_string($id)."' AND
  5104. user_id = '".intval($_user['user_id'])."'";
  5105. Database::query($sql);
  5106. Session::erase('forum_notification');
  5107. getNotificationsPerUser(0, true);
  5108. return get_lang('YouWillNoLongerBeNotifiedOfNewPosts');
  5109. }
  5110. }
  5111. }
  5112. /**
  5113. * This function retrieves all the email adresses of the users who wanted to be notified
  5114. * about a new post in a certain forum or thread.
  5115. *
  5116. * @param string $content does the user want to be notified about a forum or about a thread
  5117. * @param int $id the id of the forum or thread
  5118. *
  5119. * @return array returns
  5120. *
  5121. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5122. *
  5123. * @version May 2008, dokeos 1.8.5
  5124. *
  5125. * @since May 2008, dokeos 1.8.5
  5126. */
  5127. function get_notifications($content, $id)
  5128. {
  5129. // Database table definition
  5130. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  5131. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  5132. $course_id = api_get_course_int_id();
  5133. // Which database field contains the notification?
  5134. if ($content == 'forum') {
  5135. $database_field = 'forum_id';
  5136. } else {
  5137. $database_field = 'thread_id';
  5138. }
  5139. $sql = "SELECT user.user_id, user.firstname, user.lastname, user.email, user.user_id user
  5140. FROM $table_users user, $table_notification notification
  5141. WHERE
  5142. notification.c_id = $course_id AND user.active = 1 AND
  5143. user.user_id = notification.user_id AND
  5144. notification.$database_field= '".Database::escape_string($id)."'";
  5145. $result = Database::query($sql);
  5146. $return = [];
  5147. while ($row = Database::fetch_array($result)) {
  5148. $return['user'.$row['user_id']] = ['email' => $row['email'], 'user_id' => $row['user_id']];
  5149. }
  5150. return $return;
  5151. }
  5152. /**
  5153. * Get all the users who need to receive a notification of a new post (those subscribed to
  5154. * the forum or the thread).
  5155. *
  5156. * @param int $forum_id the id of the forum
  5157. * @param int $thread_id the id of the thread
  5158. * @param int $post_id the id of the post
  5159. *
  5160. * @return false|null
  5161. *
  5162. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5163. *
  5164. * @version May 2008, dokeos 1.8.5
  5165. *
  5166. * @since May 2008, dokeos 1.8.5
  5167. */
  5168. function send_notifications($forum_id = 0, $thread_id = 0, $post_id = 0)
  5169. {
  5170. //$_course = api_get_course_info();
  5171. /*$forumCourseId = api_get_configuration_value('global_forums_course_id');
  5172. if (!empty($forumCourseId)) {
  5173. if ($_course['real_id'] == $forumCourseId) {
  5174. return false;
  5175. }
  5176. }*/
  5177. $forum_id = (int) $forum_id;
  5178. // The content of the mail
  5179. //$thread_link = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&forum='.$forum_id.'&thread='.$thread_id;
  5180. // Users who subscribed to the forum
  5181. if ($forum_id != 0) {
  5182. $users_to_be_notified_by_forum = get_notifications('forum', $forum_id);
  5183. } else {
  5184. return false;
  5185. }
  5186. $current_thread = get_thread_information($forum_id, $thread_id);
  5187. //$current_forum = get_forum_information($current_thread['forum_id']);
  5188. //$subject = get_lang('NewForumPost').' - '.$_course['official_code'].' - '.$current_forum['forum_title'].' - '.$current_thread['thread_title'];
  5189. // User who subscribed to the thread
  5190. if ($thread_id != 0) {
  5191. $users_to_be_notified_by_thread = get_notifications('thread', $thread_id);
  5192. }
  5193. $postInfo = [];
  5194. if (!empty($post_id)) {
  5195. $postInfo = get_post_information($post_id);
  5196. }
  5197. // Merging the two
  5198. $users_to_be_notified = array_merge($users_to_be_notified_by_forum, $users_to_be_notified_by_thread);
  5199. $forumInfo = get_forum_information($forum_id);
  5200. if (is_array($users_to_be_notified)) {
  5201. foreach ($users_to_be_notified as $value) {
  5202. $userInfo = api_get_user_info($value['user_id']);
  5203. send_mail($userInfo, $forumInfo, $current_thread, $postInfo);
  5204. }
  5205. }
  5206. }
  5207. /**
  5208. * Get all the notification subscriptions of the user
  5209. * = which forums and which threads does the user wants to be informed of when a new
  5210. * post is added to this thread.
  5211. *
  5212. * @param int $user_id the user_id of a user (default = 0 => the current user)
  5213. * @param bool $force force get the notification subscriptions (even if the information is already in the session
  5214. *
  5215. * @return array
  5216. *
  5217. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  5218. *
  5219. * @version May 2008, dokeos 1.8.5
  5220. *
  5221. * @since May 2008, dokeos 1.8.5
  5222. */
  5223. function getNotificationsPerUser($user_id = 0, $force = false, $course_id = 0)
  5224. {
  5225. // Database table definition
  5226. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  5227. $course_id = empty($course_id) ? api_get_course_int_id() : (int) $course_id;
  5228. if (empty($course_id) || $course_id == -1) {
  5229. return null;
  5230. }
  5231. $user_id = empty($user_id) ? api_get_user_id() : (int) $user_id;
  5232. if (!isset($_SESSION['forum_notification']) ||
  5233. $_SESSION['forum_notification']['course'] != $course_id ||
  5234. $force == true
  5235. ) {
  5236. $_SESSION['forum_notification']['course'] = $course_id;
  5237. $sql = "SELECT * FROM $table_notification
  5238. WHERE c_id = $course_id AND user_id='".$user_id."'";
  5239. $result = Database::query($sql);
  5240. while ($row = Database::fetch_array($result)) {
  5241. if (!is_null($row['forum_id'])) {
  5242. $_SESSION['forum_notification']['forum'][] = $row['forum_id'];
  5243. }
  5244. if (!is_null($row['thread_id'])) {
  5245. $_SESSION['forum_notification']['thread'][] = $row['thread_id'];
  5246. }
  5247. }
  5248. }
  5249. }
  5250. /**
  5251. * This function counts the number of post inside a thread.
  5252. *
  5253. * @param int $thread_id
  5254. *
  5255. * @return int the number of post inside a thread
  5256. *
  5257. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  5258. *
  5259. * @version octubre 2008, dokeos 1.8
  5260. */
  5261. function count_number_of_post_in_thread($thread_id)
  5262. {
  5263. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5264. $course_id = api_get_course_int_id();
  5265. if (empty($course_id)) {
  5266. return 0;
  5267. }
  5268. $sql = "SELECT count(*) count FROM $table_posts
  5269. WHERE
  5270. c_id = $course_id AND
  5271. thread_id='".intval($thread_id)."' ";
  5272. $result = Database::query($sql);
  5273. $count = 0;
  5274. if (Database::num_rows($result) > 0) {
  5275. $row = Database::fetch_array($result);
  5276. $count = $row['count'];
  5277. }
  5278. return $count;
  5279. }
  5280. /**
  5281. * This function counts the number of post inside a thread user.
  5282. *
  5283. * @param int $thread_id
  5284. * @param int $user_id
  5285. *
  5286. * @return int the number of post inside a thread user
  5287. */
  5288. function count_number_of_post_for_user_thread($thread_id, $user_id)
  5289. {
  5290. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5291. $course_id = api_get_course_int_id();
  5292. $sql = "SELECT count(iid) as count
  5293. FROM $table_posts
  5294. WHERE c_id = $course_id AND
  5295. thread_id=".intval($thread_id)." AND
  5296. poster_id = ".intval($user_id)." AND visible = 1 ";
  5297. $result = Database::query($sql);
  5298. $count = 0;
  5299. if (Database::num_rows($result) > 0) {
  5300. $count = Database::fetch_array($result);
  5301. $count = $count['count'];
  5302. }
  5303. return $count;
  5304. }
  5305. /**
  5306. * This function retrieves information of statistical.
  5307. *
  5308. * @param int $thread_id
  5309. * @param int $user_id
  5310. * @param int $course_id
  5311. *
  5312. * @return array the information of statistical
  5313. *
  5314. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  5315. *
  5316. * @version oct 2008, dokeos 1.8
  5317. */
  5318. function get_statistical_information($thread_id, $user_id, $course_id)
  5319. {
  5320. $result = [];
  5321. $courseInfo = api_get_course_info_by_id($course_id);
  5322. $result['user_course'] = CourseManager::get_users_count_in_course($courseInfo['code']);
  5323. $result['post'] = count_number_of_post_in_thread($thread_id);
  5324. $result['user_post'] = count_number_of_post_for_user_thread($thread_id, $user_id);
  5325. return $result;
  5326. }
  5327. /**
  5328. * This function return the posts inside a thread from a given user.
  5329. *
  5330. * @param string $course_code
  5331. * @param int $thread_id
  5332. * @param int $user_id
  5333. *
  5334. * @return array posts inside a thread
  5335. *
  5336. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>,
  5337. *
  5338. * @version oct 2008, dokeos 1.8
  5339. */
  5340. function get_thread_user_post($course_code, $thread_id, $user_id)
  5341. {
  5342. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5343. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  5344. $thread_id = intval($thread_id);
  5345. $user_id = intval($user_id);
  5346. $course_info = api_get_user_info($course_code);
  5347. $course_id = $course_info['real_id'];
  5348. if (empty($course_id)) {
  5349. $course_id = api_get_course_int_id();
  5350. }
  5351. $sql = "SELECT * FROM $table_posts posts
  5352. LEFT JOIN $table_users users
  5353. ON posts.poster_id=users.user_id
  5354. WHERE
  5355. posts.c_id = $course_id AND
  5356. posts.thread_id='$thread_id'
  5357. AND posts.poster_id='$user_id'
  5358. ORDER BY posts.post_id ASC";
  5359. $result = Database::query($sql);
  5360. $post_list = [];
  5361. while ($row = Database::fetch_array($result)) {
  5362. $row['status'] = '1';
  5363. $post_list[] = $row;
  5364. $sql = "SELECT * FROM $table_posts posts
  5365. LEFT JOIN $table_users users
  5366. ON (posts.poster_id=users.user_id)
  5367. WHERE
  5368. posts.c_id = $course_id AND
  5369. posts.thread_id='$thread_id'
  5370. AND posts.post_parent_id='".$row['post_id']."'
  5371. ORDER BY posts.post_id ASC";
  5372. $result2 = Database::query($sql);
  5373. while ($row2 = Database::fetch_array($result2)) {
  5374. $row2['status'] = '0';
  5375. $post_list[] = $row2;
  5376. }
  5377. }
  5378. return $post_list;
  5379. }
  5380. /**
  5381. * This function get the name of an thread by id.
  5382. *
  5383. * @param int thread_id
  5384. *
  5385. * @return string
  5386. *
  5387. * @author Christian Fasanando
  5388. * @author Julio Montoya <gugli100@gmail.com> Adding security
  5389. */
  5390. function get_name_thread_by_id($thread_id)
  5391. {
  5392. $t_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  5393. $course_id = api_get_course_int_id();
  5394. $sql = "SELECT thread_title
  5395. FROM $t_forum_thread
  5396. WHERE c_id = $course_id AND thread_id = '".intval($thread_id)."' ";
  5397. $result = Database::query($sql);
  5398. $row = Database::fetch_array($result);
  5399. return $row[0];
  5400. }
  5401. /**
  5402. * This function gets all the post written by an user.
  5403. *
  5404. * @param int $user_id
  5405. * @param string $course_code
  5406. *
  5407. * @return string
  5408. */
  5409. function get_all_post_from_user($user_id, $course_code)
  5410. {
  5411. $j = 0;
  5412. $forums = get_forums('', $course_code);
  5413. krsort($forums);
  5414. $forum_results = '';
  5415. foreach ($forums as $forum) {
  5416. if ($forum['visibility'] == 0) {
  5417. continue;
  5418. }
  5419. if ($j <= 4) {
  5420. $threads = get_threads($forum['forum_id']);
  5421. if (is_array($threads)) {
  5422. $i = 0;
  5423. $hand_forums = '';
  5424. $post_counter = 0;
  5425. foreach ($threads as $thread) {
  5426. if ($thread['visibility'] == 0) {
  5427. continue;
  5428. }
  5429. if ($i <= 4) {
  5430. $post_list = get_thread_user_post_limit(
  5431. $course_code,
  5432. $thread['thread_id'],
  5433. $user_id,
  5434. 1
  5435. );
  5436. $post_counter = count($post_list);
  5437. if (is_array($post_list) && count($post_list) > 0) {
  5438. $hand_forums .= '<div id="social-thread">';
  5439. $hand_forums .= Display::return_icon(
  5440. 'thread.png',
  5441. get_lang('Thread'),
  5442. '',
  5443. ICON_SIZE_MEDIUM
  5444. );
  5445. $hand_forums .= '&nbsp;'.Security::remove_XSS($thread['thread_title'], STUDENT);
  5446. $hand_forums .= '</div>';
  5447. foreach ($post_list as $posts) {
  5448. $hand_forums .= '<div id="social-post">';
  5449. $hand_forums .= '<strong>'.Security::remove_XSS($posts['post_title'], STUDENT).'</strong>';
  5450. $hand_forums .= '<br / >';
  5451. $hand_forums .= Security::remove_XSS($posts['post_text'], STUDENT);
  5452. $hand_forums .= '</div>';
  5453. $hand_forums .= '<br / >';
  5454. }
  5455. }
  5456. }
  5457. $i++;
  5458. }
  5459. $forum_results .= '<div id="social-forum">';
  5460. $forum_results .= '<div class="clear"></div><br />';
  5461. $forum_results .= '<div id="social-forum-title">'.
  5462. Display::return_icon('forum.gif', get_lang('Forum')).'&nbsp;'.Security::remove_XSS($forum['forum_title'], STUDENT).
  5463. '<div style="float:right;margin-top:-35px">
  5464. <a href="../forum/viewforum.php?'.api_get_cidreq_params($course_code).'&forum='.$forum['forum_id'].' " >'.
  5465. get_lang('SeeForum').'
  5466. </a>
  5467. </div></div>';
  5468. $forum_results .= '<br / >';
  5469. if ($post_counter > 0) {
  5470. $forum_results .= $hand_forums;
  5471. }
  5472. $forum_results .= '</div>';
  5473. }
  5474. $j++;
  5475. }
  5476. }
  5477. return $forum_results;
  5478. }
  5479. /**
  5480. * @param string $course_code
  5481. * @param int $thread_id
  5482. * @param int $user_id
  5483. * @param int $limit
  5484. *
  5485. * @return array
  5486. */
  5487. function get_thread_user_post_limit($course_code, $thread_id, $user_id, $limit = 10)
  5488. {
  5489. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  5490. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  5491. $course_info = api_get_course_info($course_code);
  5492. $course_id = $course_info['real_id'];
  5493. $sql = "SELECT * FROM $table_posts posts
  5494. LEFT JOIN $table_users users
  5495. ON posts.poster_id=users.user_id
  5496. WHERE
  5497. posts.c_id = $course_id AND
  5498. posts.thread_id='".Database::escape_string($thread_id)."' AND
  5499. posts.poster_id='".Database::escape_string($user_id)."'
  5500. ORDER BY posts.post_id DESC LIMIT $limit ";
  5501. $result = Database::query($sql);
  5502. $post_list = [];
  5503. while ($row = Database::fetch_array($result)) {
  5504. $row['status'] = '1';
  5505. $post_list[] = $row;
  5506. }
  5507. return $post_list;
  5508. }
  5509. /**
  5510. * @param string $user_id
  5511. * @param int $courseId
  5512. * @param int $sessionId
  5513. *
  5514. * @return array
  5515. */
  5516. function getForumCreatedByUser($user_id, $courseId, $sessionId)
  5517. {
  5518. $items = api_get_item_property_list_by_tool_by_user(
  5519. $user_id,
  5520. 'forum',
  5521. $courseId,
  5522. $sessionId
  5523. );
  5524. $courseInfo = api_get_course_info_by_id($courseId);
  5525. $forumList = [];
  5526. if (!empty($items)) {
  5527. foreach ($items as $forum) {
  5528. $forumInfo = get_forums(
  5529. $forum['ref'],
  5530. $courseInfo['code'],
  5531. true,
  5532. $sessionId
  5533. );
  5534. if (!empty($forumInfo)) {
  5535. $forumList[] = [
  5536. $forumInfo['forum_title'],
  5537. api_get_local_time($forum['insert_date']),
  5538. api_get_local_time($forum['lastedit_date']),
  5539. ];
  5540. }
  5541. }
  5542. }
  5543. return $forumList;
  5544. }
  5545. /**
  5546. * This function builds an array of all the posts in a given thread
  5547. * where the key of the array is the post_id
  5548. * It also adds an element children to the array which itself is an array
  5549. * that contains all the id's of the first-level children.
  5550. *
  5551. * @return array $rows containing all the information on the posts of a thread
  5552. *
  5553. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  5554. */
  5555. function calculate_children($rows)
  5556. {
  5557. $sorted_rows = [0 => []];
  5558. if (!empty($rows)) {
  5559. foreach ($rows as $row) {
  5560. $rows_with_children[$row['post_id']] = $row;
  5561. $rows_with_children[$row['post_parent_id']]['children'][] = $row['post_id'];
  5562. }
  5563. $rows = $rows_with_children;
  5564. forumRecursiveSort($rows, $sorted_rows);
  5565. unset($sorted_rows[0]);
  5566. }
  5567. return $sorted_rows;
  5568. }
  5569. /**
  5570. * @param $rows
  5571. * @param $threads
  5572. * @param int $seed
  5573. * @param int $indent
  5574. */
  5575. function forumRecursiveSort($rows, &$threads, $seed = 0, $indent = 0)
  5576. {
  5577. if ($seed > 0) {
  5578. $threads[$rows[$seed]['post_id']] = $rows[$seed];
  5579. $threads[$rows[$seed]['post_id']]['indent_cnt'] = $indent;
  5580. $indent++;
  5581. }
  5582. if (isset($rows[$seed]['children'])) {
  5583. foreach ($rows[$seed]['children'] as $child) {
  5584. forumRecursiveSort($rows, $threads, $child, $indent);
  5585. }
  5586. }
  5587. }
  5588. /**
  5589. * Update forum attachment data, used to update comment and post ID.
  5590. *
  5591. * @param $array array (field => value) to update forum attachment row
  5592. * @param $id attach ID to find row to update
  5593. * @param null $courseId course ID to find row to update
  5594. *
  5595. * @return int number of affected rows
  5596. */
  5597. function editAttachedFile($array, $id, $courseId = null)
  5598. {
  5599. // Init variables
  5600. $setString = '';
  5601. $id = intval($id);
  5602. $courseId = intval($courseId);
  5603. if (empty($courseId)) {
  5604. // $courseId can be null, use api method
  5605. $courseId = api_get_course_int_id();
  5606. }
  5607. /*
  5608. * Check if Attachment ID and Course ID are greater than zero
  5609. * and array of field values is not empty
  5610. */
  5611. if ($id > 0 && $courseId > 0 && !empty($array) && is_array($array)) {
  5612. foreach ($array as $key => &$item) {
  5613. $item = Database::escape_string($item);
  5614. $setString .= $key.' = "'.$item.'", ';
  5615. }
  5616. // Delete last comma
  5617. $setString = substr($setString, 0, strlen($setString) - 2);
  5618. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  5619. $sql = "UPDATE $forumAttachmentTable
  5620. SET $setString WHERE c_id = $courseId AND id = $id";
  5621. $result = Database::query($sql);
  5622. if ($result !== false) {
  5623. $affectedRows = Database::affected_rows($result);
  5624. if ($affectedRows > 0) {
  5625. /*
  5626. * If exist in $_SESSION variable, then delete them from it
  5627. * because they would be deprecated
  5628. */
  5629. if (!empty($_SESSION['forum']['upload_file'][$courseId][$id])) {
  5630. unset($_SESSION['forum']['upload_file'][$courseId][$id]);
  5631. }
  5632. }
  5633. return $affectedRows;
  5634. }
  5635. }
  5636. return 0;
  5637. }
  5638. /**
  5639. * Return a table where the attachments will be set.
  5640. *
  5641. * @param int $postId Forum Post ID
  5642. *
  5643. * @return string The Forum Attachments Ajax Table
  5644. */
  5645. function getAttachmentsAjaxTable($postId = 0)
  5646. {
  5647. // Init variables
  5648. $postId = intval($postId);
  5649. $courseId = api_get_course_int_id();
  5650. $attachIds = getAttachmentIdsByPostId($postId, $courseId);
  5651. $fileDataContent = '';
  5652. // Update comment to show if form did not pass validation
  5653. if (!empty($_REQUEST['file_ids']) && is_array($_REQUEST['file_ids'])) {
  5654. // 'file_ids is the name from forum attachment ajax form
  5655. foreach ($_REQUEST['file_ids'] as $key => $attachId) {
  5656. if (!empty($_SESSION['forum']['upload_file'][$courseId][$attachId]) &&
  5657. is_array($_SESSION['forum']['upload_file'][$courseId][$attachId])
  5658. ) {
  5659. // If exist forum attachment then update into $_SESSION data
  5660. $_SESSION['forum']['upload_file'][$courseId][$attachId]['comment'] = $_POST['file_comments'][$key];
  5661. }
  5662. }
  5663. }
  5664. // Get data to fill into attachment files table
  5665. if (!empty($_SESSION['forum']['upload_file'][$courseId]) &&
  5666. is_array($_SESSION['forum']['upload_file'][$courseId])
  5667. ) {
  5668. $uploadedFiles = $_SESSION['forum']['upload_file'][$courseId];
  5669. foreach ($uploadedFiles as $k => $uploadedFile) {
  5670. if (!empty($uploadedFile) && in_array($uploadedFile['id'], $attachIds)) {
  5671. // Buil html table including an input with attachmentID
  5672. $fileDataContent .= '<tr id="'.$uploadedFile['id'].'" ><td>'.$uploadedFile['name'].'</td><td>'.$uploadedFile['size'].'</td><td>&nbsp;'.$uploadedFile['result'].
  5673. ' </td><td> <input style="width:90%;" type="text" value="'.$uploadedFile['comment'].'" name="file_comments[]"> </td><td>'.
  5674. $uploadedFile['delete'].'</td>'.
  5675. '<input type="hidden" value="'.$uploadedFile['id'].'" name="file_ids[]">'.'</tr>';
  5676. } else {
  5677. /*
  5678. * If attachment data is empty, then delete it from $_SESSION
  5679. * because could generate and empty row into html table
  5680. */
  5681. unset($_SESSION['forum']['upload_file'][$courseId][$k]);
  5682. }
  5683. }
  5684. }
  5685. $style = empty($fileDataContent) ? 'display: none;' : '';
  5686. // Forum attachment Ajax table
  5687. $fileData = '
  5688. <div class="control-group " style="'.$style.'">
  5689. <label class="control-label">'.get_lang('AttachmentList').'</label>
  5690. <div class="controls">
  5691. <table id="attachmentFileList" class="files data_table span10">
  5692. <tr>
  5693. <th>'.get_lang('FileName').'</th>
  5694. <th>'.get_lang('Size').'</th>
  5695. <th>'.get_lang('Status').'</th>
  5696. <th>'.get_lang('Comment').'</th>
  5697. <th>'.get_lang('Delete').'</th>
  5698. </tr>
  5699. '.$fileDataContent.'
  5700. </table>
  5701. </div>
  5702. </div>';
  5703. return $fileData;
  5704. }
  5705. /**
  5706. * Return an array of prepared attachment data to build forum attachment table
  5707. * Also, save this array into $_SESSION to do available the attachment data.
  5708. *
  5709. * @param int $forumId
  5710. * @param int $threadId
  5711. * @param int $postId
  5712. * @param int $attachId
  5713. * @param int $courseId
  5714. *
  5715. * @return array
  5716. */
  5717. function getAttachedFiles(
  5718. $forumId,
  5719. $threadId,
  5720. $postId = 0,
  5721. $attachId = 0,
  5722. $courseId = 0
  5723. ) {
  5724. $forumId = intval($forumId);
  5725. $courseId = intval($courseId);
  5726. $attachId = intval($attachId);
  5727. $postId = intval($postId);
  5728. $threadId = !empty($threadId) ? intval($threadId) : isset($_REQUEST['thread']) ? intval($_REQUEST['thread']) : '';
  5729. if (empty($courseId)) {
  5730. // $courseId can be null, use api method
  5731. $courseId = api_get_course_int_id();
  5732. }
  5733. if (empty($forumId)) {
  5734. if (!empty($_REQUEST['forum'])) {
  5735. $forumId = intval($_REQUEST['forum']);
  5736. } else {
  5737. // if forum ID is empty, cannot generate delete url
  5738. return [];
  5739. }
  5740. }
  5741. // Check if exist at least one of them to filter forum attachment select query
  5742. if (empty($postId) && empty($attachId)) {
  5743. return [];
  5744. } elseif (empty($postId)) {
  5745. $filter = "AND iid = $attachId";
  5746. } elseif (empty($attachId)) {
  5747. $filter = "AND post_id = $postId";
  5748. } else {
  5749. $filter = "AND post_id = $postId AND iid = $attachId";
  5750. }
  5751. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  5752. $sql = "SELECT iid, comment, filename, path, size
  5753. FROM $forumAttachmentTable
  5754. WHERE c_id = $courseId $filter";
  5755. $result = Database::query($sql);
  5756. $json = [];
  5757. if ($result !== false && Database::num_rows($result) > 0) {
  5758. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5759. // name contains an URL to download attachment file and its filename
  5760. $json['name'] = Display::url(
  5761. api_htmlentities($row['filename']),
  5762. api_get_path(WEB_CODE_PATH).'forum/download.php?file='.$row['path'].'&'.api_get_cidreq(),
  5763. ['target' => '_blank', 'class' => 'attachFilename']
  5764. );
  5765. $json['id'] = $row['iid'];
  5766. $json['comment'] = $row['comment'];
  5767. // Format file size
  5768. $json['size'] = format_file_size($row['size']);
  5769. // Check if $row is consistent
  5770. if (!empty($row) && is_array($row)) {
  5771. // Set result as success and bring delete URL
  5772. $json['result'] = Display::return_icon('accept.png', get_lang('Uploaded'));
  5773. $url = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.api_get_cidreq().'&action=delete_attach&forum='.$forumId.'&thread='.$threadId.'&id_attach='.$row['iid'];
  5774. $json['delete'] = Display::url(
  5775. Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL),
  5776. $url,
  5777. ['class' => 'deleteLink']
  5778. );
  5779. } else {
  5780. // If not, set an exclamation result
  5781. $json['result'] = Display::return_icon('exclamation.png', get_lang('Error'));
  5782. }
  5783. // Store array data into $_SESSION
  5784. $_SESSION['forum']['upload_file'][$courseId][$json['id']] = $json;
  5785. }
  5786. }
  5787. return $json;
  5788. }
  5789. /**
  5790. * Clear forum attachment data stored in $_SESSION,
  5791. * If is not defined post, it will clear all forum attachment data from course.
  5792. *
  5793. * @param int $postId -1 : Clear all attachments from course stored in $_SESSION
  5794. * 0 : Clear attachments from course, except from temporal post "0"
  5795. * but without delete them from file system and database
  5796. * Other values : Clear attachments from course except specified post
  5797. * and delete them from file system and database
  5798. * @param int $courseId : Course ID, if it is null, will use api_get_course_int_id()
  5799. *
  5800. * @return array
  5801. */
  5802. function clearAttachedFiles($postId = null, $courseId = null)
  5803. {
  5804. // Init variables
  5805. $courseId = intval($courseId);
  5806. $postId = intval($postId);
  5807. $array = [];
  5808. if (empty($courseId)) {
  5809. // $courseId can be null, use api method
  5810. $courseId = api_get_course_int_id();
  5811. }
  5812. if ($postId === -1) {
  5813. // If post ID is -1 then delete course's attachment data from $_SESSION
  5814. if (!empty($_SESSION['forum']['upload_file'][$courseId])) {
  5815. $array = array_keys($_SESSION['forum']['upload_file'][$courseId]);
  5816. unset($_SESSION['forum']['upload_file'][$courseId]);
  5817. }
  5818. } else {
  5819. $attachIds = getAttachmentIdsByPostId($postId, $courseId);
  5820. if (!empty($_SESSION['forum']['upload_file'][$courseId]) &&
  5821. is_array($_SESSION['forum']['upload_file'][$courseId])) {
  5822. foreach ($_SESSION['forum']['upload_file'][$courseId] as $attachId => $attach) {
  5823. if (!in_array($attachId, $attachIds)) {
  5824. // If attach ID is not into specified post, delete attachment
  5825. // Save deleted attachment ID
  5826. $array[] = $attachId;
  5827. if ($postId !== 0) {
  5828. // Post 0 is temporal, delete them from file system and DB
  5829. delete_attachment(0, $attachId);
  5830. }
  5831. // Delete attachment data from $_SESSION
  5832. unset($_SESSION['forum']['upload_file'][$courseId][$attachId]);
  5833. }
  5834. }
  5835. }
  5836. }
  5837. return $array;
  5838. }
  5839. /**
  5840. * Returns an array of forum attachment ids into a course and forum post.
  5841. *
  5842. * @param int $postId
  5843. * @param int $courseId
  5844. *
  5845. * @return array
  5846. */
  5847. function getAttachmentIdsByPostId($postId, $courseId = 0)
  5848. {
  5849. $array = [];
  5850. $courseId = intval($courseId);
  5851. $postId = intval($postId);
  5852. if (empty($courseId)) {
  5853. // $courseId can be null, use api method
  5854. $courseId = api_get_course_int_id();
  5855. }
  5856. if ($courseId > 0) {
  5857. $forumAttachmentTable = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  5858. $sql = "SELECT id FROM $forumAttachmentTable
  5859. WHERE c_id = $courseId AND post_id = $postId";
  5860. $result = Database::query($sql);
  5861. if ($result !== false && Database::num_rows($result) > 0) {
  5862. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5863. $array[] = $row['id'];
  5864. }
  5865. }
  5866. }
  5867. return $array;
  5868. }
  5869. /**
  5870. * Check if the forum category exists looking for its title.
  5871. *
  5872. * @param string $title The forum category title
  5873. * @param int $courseId The course ID
  5874. * @param int $sessionId Optional. The session ID
  5875. *
  5876. * @return bool
  5877. */
  5878. function getForumCategoryByTitle($title, $courseId, $sessionId = 0)
  5879. {
  5880. $sessionId = intval($sessionId);
  5881. $forumCategoryTable = Database::get_course_table(TABLE_FORUM_CATEGORY);
  5882. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5883. $fakeFrom = "$forumCategoryTable fc
  5884. INNER JOIN $itemProperty ip ";
  5885. if ($sessionId === 0) {
  5886. $fakeFrom .= "
  5887. ON (
  5888. 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)
  5889. )
  5890. ";
  5891. } else {
  5892. $fakeFrom .= "
  5893. ON (
  5894. fc.cat_id = ip.ref AND fc.c_id = ip.c_id AND fc.session_id = ip.session_id
  5895. )
  5896. ";
  5897. }
  5898. $resultData = Database::select(
  5899. 'fc.*',
  5900. $fakeFrom,
  5901. [
  5902. 'where' => [
  5903. 'ip.visibility != ? AND ' => 2,
  5904. 'ip.tool = ? AND ' => TOOL_FORUM_CATEGORY,
  5905. 'fc.session_id = ? AND ' => $sessionId,
  5906. 'fc.cat_title = ? AND ' => $title,
  5907. 'fc.c_id = ?' => intval($courseId),
  5908. ],
  5909. ],
  5910. 'first'
  5911. );
  5912. if (empty($resultData)) {
  5913. return false;
  5914. }
  5915. return $resultData;
  5916. }
  5917. /**
  5918. * @param array $current_forum
  5919. * @param array $row
  5920. * @param bool $addWrapper
  5921. *
  5922. * @return string
  5923. */
  5924. function getPostStatus($current_forum, $row, $addWrapper = true)
  5925. {
  5926. $statusIcon = '';
  5927. if ($current_forum['moderated']) {
  5928. if ($addWrapper) {
  5929. $statusIcon = '<br /><br /><span id="status_post_'.$row['iid'].'">';
  5930. }
  5931. $row['status'] = empty($row['status']) ? 2 : $row['status'];
  5932. $addUrl = false;
  5933. $showStatus = false;
  5934. if (api_is_allowed_to_edit(false, true)) {
  5935. $addUrl = true;
  5936. } else {
  5937. if ($row['user_id'] == api_get_user_id()) {
  5938. $showStatus = true;
  5939. }
  5940. }
  5941. $label = '';
  5942. $icon = '';
  5943. $buttonType = '';
  5944. switch ($row['status']) {
  5945. case CForumPost::STATUS_VALIDATED:
  5946. $label = get_lang('Validated');
  5947. $icon = 'check-circle';
  5948. $buttonType = 'success';
  5949. break;
  5950. case CForumPost::STATUS_WAITING_MODERATION:
  5951. $label = get_lang('WaitingModeration');
  5952. $icon = 'warning';
  5953. $buttonType = 'warning';
  5954. break;
  5955. case CForumPost::STATUS_REJECTED:
  5956. $label = get_lang('Rejected');
  5957. $icon = 'minus-circle';
  5958. $buttonType = 'danger';
  5959. break;
  5960. }
  5961. if ($addUrl) {
  5962. $statusIcon .= Display::toolbarButton(
  5963. $label.'&nbsp;',
  5964. 'javascript:void(0)',
  5965. $icon,
  5966. $buttonType,
  5967. ['class' => 'change_post_status']
  5968. );
  5969. } else {
  5970. if ($showStatus) {
  5971. $statusIcon .= Display::label(
  5972. Display::returnFontAwesomeIcon($icon).$label,
  5973. $buttonType
  5974. );
  5975. }
  5976. }
  5977. if ($addWrapper) {
  5978. $statusIcon .= '</span>';
  5979. }
  5980. }
  5981. return $statusIcon;
  5982. }
  5983. /**
  5984. * @param array $forumInfo
  5985. * @param int $threadId
  5986. * @param int $status
  5987. *
  5988. * @return mixed
  5989. */
  5990. function getCountPostsWithStatus($status, $forumInfo, $threadId = null)
  5991. {
  5992. $em = Database::getManager();
  5993. $criteria = Criteria::create();
  5994. $criteria
  5995. ->where(Criteria::expr()->eq('status', $status))
  5996. ->andWhere(Criteria::expr()->eq('cId', $forumInfo['c_id']))
  5997. ->andWhere(Criteria::expr()->eq('visible', 1))
  5998. ;
  5999. if (!empty($threadId)) {
  6000. $criteria->andWhere(Criteria::expr()->eq('threadId', $threadId));
  6001. }
  6002. $qb = $em->getRepository('ChamiloCourseBundle:CForumPost')->createQueryBuilder('p');
  6003. $qb->select('count(p.iid)')
  6004. ->addCriteria($criteria);
  6005. return $qb->getQuery()->getSingleScalarResult();
  6006. }
  6007. /**
  6008. * @param array $forum
  6009. * @param array $post
  6010. *
  6011. * @return bool
  6012. */
  6013. function postIsEditableByStudent($forum, $post)
  6014. {
  6015. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  6016. return true;
  6017. }
  6018. if ($forum['moderated'] == 1) {
  6019. if (is_null($post['status'])) {
  6020. return true;
  6021. } else {
  6022. return in_array(
  6023. $post['status'],
  6024. [
  6025. CForumPost::STATUS_WAITING_MODERATION,
  6026. CForumPost::STATUS_REJECTED,
  6027. ]
  6028. );
  6029. }
  6030. } else {
  6031. return true;
  6032. }
  6033. }
  6034. /**
  6035. * @param int $postId
  6036. *
  6037. * @return bool
  6038. */
  6039. function savePostRevision($postId)
  6040. {
  6041. $postData = get_post_information($postId);
  6042. if (empty($postData)) {
  6043. return false;
  6044. }
  6045. $userId = api_get_user_id();
  6046. if ($postData['poster_id'] != $userId) {
  6047. return false;
  6048. }
  6049. $status = (int) !postNeedsRevision($postId);
  6050. $extraFieldValue = new ExtraFieldValue('forum_post');
  6051. $params = [
  6052. 'item_id' => $postId,
  6053. 'extra_ask_for_revision' => ['extra_ask_for_revision' => $status],
  6054. ];
  6055. if (empty($status)) {
  6056. unset($params['extra_ask_for_revision']);
  6057. }
  6058. $extraFieldValue->saveFieldValues(
  6059. $params,
  6060. true,
  6061. false,
  6062. ['ask_for_revision']
  6063. );
  6064. }
  6065. /**
  6066. * @param int $postId
  6067. *
  6068. * @return string
  6069. */
  6070. function getPostRevision($postId)
  6071. {
  6072. $extraFieldValue = new ExtraFieldValue('forum_post');
  6073. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6074. $postId,
  6075. 'revision_language'
  6076. );
  6077. $revision = '';
  6078. if ($value && isset($value['value'])) {
  6079. $revision = $value['value'];
  6080. }
  6081. return $revision;
  6082. }
  6083. /**
  6084. * @param int $postId
  6085. *
  6086. * @return bool
  6087. */
  6088. function postNeedsRevision($postId)
  6089. {
  6090. $extraFieldValue = new ExtraFieldValue('forum_post');
  6091. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6092. $postId,
  6093. 'ask_for_revision'
  6094. );
  6095. $hasRevision = false;
  6096. if ($value && isset($value['value'])) {
  6097. return $value['value'] == 1;
  6098. }
  6099. return $hasRevision;
  6100. }
  6101. /**
  6102. * @param int $postId
  6103. * @param array $threadInfo
  6104. *
  6105. * @return string
  6106. */
  6107. function getAskRevisionButton($postId, $threadInfo)
  6108. {
  6109. if (api_get_configuration_value('allow_forum_post_revisions') === false) {
  6110. return '';
  6111. }
  6112. $postId = (int) $postId;
  6113. $status = 'btn-default';
  6114. if (postNeedsRevision($postId)) {
  6115. $status = 'btn-success';
  6116. }
  6117. return Display::url(
  6118. get_lang('AskRevision'),
  6119. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.
  6120. api_get_cidreq().'&action=ask_revision&post_id='.$postId.'&forum='.$threadInfo['forum_id'].'&thread='.$threadInfo['thread_id'],
  6121. ['class' => "btn $status", 'title' => get_lang('AskRevision')]
  6122. );
  6123. }
  6124. /**
  6125. * @param int $postId
  6126. * @param array $threadInfo
  6127. *
  6128. * @return string
  6129. */
  6130. function giveRevisionButton($postId, $threadInfo)
  6131. {
  6132. $postId = (int) $postId;
  6133. return Display::toolbarButton(
  6134. get_lang('GiveRevision'),
  6135. api_get_path(WEB_CODE_PATH).'forum/reply.php?'.api_get_cidreq().'&'.http_build_query(
  6136. [
  6137. 'forum' => $threadInfo['forum_id'],
  6138. 'thread' => $threadInfo['thread_id'],
  6139. 'post' => $postId = (int) $postId,
  6140. 'action' => 'replymessage',
  6141. 'give_revision' => 1,
  6142. ]
  6143. ),
  6144. 'reply',
  6145. 'primary',
  6146. ['id' => "reply-to-post-{$postId}"]
  6147. );
  6148. }
  6149. /**
  6150. * @param int $postId
  6151. * @param array $threadInfo
  6152. *
  6153. * @return string
  6154. */
  6155. function getReportButton($postId, $threadInfo)
  6156. {
  6157. $postId = (int) $postId;
  6158. return Display::url(
  6159. Display::returnFontAwesomeIcon('flag'),
  6160. api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.
  6161. api_get_cidreq().'&action=report&post_id='.$postId.'&forum='.$threadInfo['forum_id'].'&thread='.$threadInfo['thread_id'],
  6162. ['class' => 'btn btn-danger', 'title' => get_lang('Report')]
  6163. );
  6164. }
  6165. /**
  6166. * @return bool
  6167. */
  6168. function reportAvailable()
  6169. {
  6170. $extraFieldValue = new ExtraFieldValue('course');
  6171. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6172. api_get_course_int_id(),
  6173. 'allow_forum_report_button'
  6174. );
  6175. $allowReport = false;
  6176. if ($value && isset($value['value']) && $value['value'] == 1) {
  6177. $allowReport = true;
  6178. }
  6179. return $allowReport;
  6180. }
  6181. /**
  6182. * @return array
  6183. */
  6184. function getReportRecipients()
  6185. {
  6186. $extraFieldValue = new ExtraFieldValue('course');
  6187. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  6188. api_get_course_int_id(),
  6189. 'forum_report_recipients'
  6190. );
  6191. $users = [];
  6192. if ($value && isset($value['value'])) {
  6193. $usersType = explode(';', $value['value']);
  6194. foreach ($usersType as $type) {
  6195. switch ($type) {
  6196. case 'teachers':
  6197. $teachers = CourseManager::get_teacher_list_from_course_code(api_get_course_id());
  6198. if (!empty($teachers)) {
  6199. $users = array_merge($users, array_column($teachers, 'user_id'));
  6200. }
  6201. break;
  6202. case 'admins':
  6203. $admins = UserManager::get_all_administrators();
  6204. if (!empty($admins)) {
  6205. $users = array_merge($users, array_column($admins, 'user_id'));
  6206. }
  6207. break;
  6208. case 'community_managers':
  6209. $managers = api_get_configuration_value('community_managers_user_list');
  6210. if (!empty($managers) && isset($managers['users'])) {
  6211. $users = array_merge($users, $managers['users']);
  6212. }
  6213. break;
  6214. }
  6215. }
  6216. $users = array_unique(array_filter($users));
  6217. }
  6218. return $users;
  6219. }
  6220. /**
  6221. * @param int $postId
  6222. * @param array $forumInfo
  6223. * @param array $threadInfo
  6224. *
  6225. * @return bool
  6226. */
  6227. function reportPost($postId, $forumInfo, $threadInfo)
  6228. {
  6229. if (!reportAvailable()) {
  6230. return false;
  6231. }
  6232. if (empty($forumInfo) || empty($threadInfo)) {
  6233. return false;
  6234. }
  6235. $postId = (int) $postId;
  6236. $postData = get_post_information($postId);
  6237. $currentUser = api_get_user_info();
  6238. if (!empty($postData)) {
  6239. $users = getReportRecipients();
  6240. if (!empty($users)) {
  6241. $url = api_get_path(WEB_CODE_PATH).
  6242. 'forum/viewthread.php?forum='.$threadInfo['forum_id'].'&thread='.$threadInfo['thread_id'].'&'.api_get_cidreq().'&post_id='.$postId.'#post_id_'.$postId;
  6243. $postLink = Display::url(
  6244. $postData['post_title'],
  6245. $url
  6246. );
  6247. $subject = get_lang('ForumPostReported');
  6248. $content = sprintf(
  6249. get_lang('UserXReportedPostXInForumX'),
  6250. $currentUser['complete_name'],
  6251. $postLink,
  6252. $forumInfo['forum_title']
  6253. );
  6254. foreach ($users as $userId) {
  6255. MessageManager::send_message_simple($userId, $subject, $content);
  6256. }
  6257. }
  6258. }
  6259. }