api.lib.php 263 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CItemProperty;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Chamilo\UserBundle\Entity\User;
  7. use Chamilo\CoreBundle\Entity\Course;
  8. use Chamilo\CoreBundle\Framework\Container;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. /**
  11. * This is a code library for Chamilo.
  12. * It is included by default in every Chamilo file (through including the global.inc.php)
  13. * This library is in process of being transferred to src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.
  14. * Whenever a function is transferred to the ChamiloApi class, the places where it is used should include
  15. * the "use Chamilo\CoreBundle\Component\Utils\ChamiloApi;" statement.
  16. * @package chamilo.library
  17. */
  18. /**
  19. * Constants declaration
  20. */
  21. // PHP version requirement.
  22. define('REQUIRED_PHP_VERSION', '5.4');
  23. define('REQUIRED_MIN_MEMORY_LIMIT', '128');
  24. define('REQUIRED_MIN_UPLOAD_MAX_FILESIZE', '10');
  25. define('REQUIRED_MIN_POST_MAX_SIZE', '10');
  26. // USER STATUS CONSTANTS
  27. /** global status of a user: student */
  28. define('STUDENT', 5);
  29. /** global status of a user: course manager */
  30. define('COURSEMANAGER', 1);
  31. /** global status of a user: session admin */
  32. define('SESSIONADMIN', 3);
  33. /** global status of a user: human ressource manager */
  34. define('DRH', 4);
  35. /** global status of a user: human ressource manager */
  36. define('ANONYMOUS', 6);
  37. /** global status of a user: low security, necessary for inserting data from
  38. * the teacher through HTMLPurifier */
  39. define('COURSEMANAGERLOWSECURITY', 10);
  40. // Soft user status
  41. define('PLATFORM_ADMIN', 11);
  42. define('SESSION_COURSE_COACH', 12);
  43. define('SESSION_GENERAL_COACH', 13);
  44. define('COURSE_STUDENT', 14); //student subscribed in a course
  45. define('SESSION_STUDENT', 15); //student subscribed in a session course
  46. define('COURSE_TUTOR', 16); // student is tutor of a course (NOT in session)
  47. define('STUDENT_BOSS', 17); // student is boss
  48. define('INVITEE', 20);
  49. // Table of status
  50. $_status_list[COURSEMANAGER] = 'teacher'; // 1
  51. $_status_list[SESSIONADMIN] = 'session_admin'; // 3
  52. $_status_list[DRH] = 'drh'; // 4
  53. $_status_list[STUDENT] = 'user'; // 5
  54. $_status_list[ANONYMOUS] = 'anonymous'; // 6
  55. $_status_list[INVITEE] = 'invited'; // 20
  56. // COURSE VISIBILITY CONSTANTS
  57. /** only visible for course admin */
  58. define('COURSE_VISIBILITY_CLOSED', 0);
  59. /** only visible for users registered in the course */
  60. define('COURSE_VISIBILITY_REGISTERED', 1);
  61. /** Open for all registered users on the platform */
  62. define('COURSE_VISIBILITY_OPEN_PLATFORM', 2);
  63. /** Open for the whole world */
  64. define('COURSE_VISIBILITY_OPEN_WORLD', 3);
  65. /** Invisible to all except admin */
  66. define('COURSE_VISIBILITY_HIDDEN', 4);
  67. define('COURSE_REQUEST_PENDING', 0);
  68. define('COURSE_REQUEST_ACCEPTED', 1);
  69. define('COURSE_REQUEST_REJECTED', 2);
  70. define('DELETE_ACTION_ENABLED', false);
  71. // EMAIL SENDING RECIPIENT CONSTANTS
  72. define('SEND_EMAIL_EVERYONE', 1);
  73. define('SEND_EMAIL_STUDENTS', 2);
  74. define('SEND_EMAIL_TEACHERS', 3);
  75. // SESSION VISIBILITY CONSTANTS
  76. define('SESSION_VISIBLE_READ_ONLY', 1);
  77. define('SESSION_VISIBLE', 2);
  78. define('SESSION_INVISIBLE', 3); // not available
  79. define('SESSION_AVAILABLE', 4);
  80. define('SESSION_LINK_TARGET', '_self');
  81. define('SUBSCRIBE_ALLOWED', 1);
  82. define('SUBSCRIBE_NOT_ALLOWED', 0);
  83. define('UNSUBSCRIBE_ALLOWED', 1);
  84. define('UNSUBSCRIBE_NOT_ALLOWED', 0);
  85. // SURVEY VISIBILITY CONSTANTS
  86. define('SURVEY_VISIBLE_TUTOR', 0);
  87. define('SURVEY_VISIBLE_TUTOR_STUDENT', 1);
  88. define('SURVEY_VISIBLE_PUBLIC', 2);
  89. // CONSTANTS defining all tools, using the english version
  90. /* When you add a new tool you must add it into function api_get_tools_lists() too */
  91. define('TOOL_DOCUMENT', 'document');
  92. define('TOOL_LP_FINAL_ITEM', 'final_item');
  93. define('TOOL_THUMBNAIL', 'thumbnail');
  94. define('TOOL_HOTPOTATOES', 'hotpotatoes');
  95. define('TOOL_CALENDAR_EVENT', 'calendar_event');
  96. define('TOOL_LINK', 'link');
  97. define('TOOL_LINK_CATEGORY', 'link_category');
  98. define('TOOL_COURSE_DESCRIPTION', 'course_description');
  99. define('TOOL_SEARCH', 'search');
  100. define('TOOL_LEARNPATH', 'learnpath');
  101. define('TOOL_AGENDA', 'agenda');
  102. define('TOOL_ANNOUNCEMENT', 'announcement');
  103. define('TOOL_FORUM', 'forum');
  104. define('TOOL_FORUM_CATEGORY', 'forum_category');
  105. define('TOOL_FORUM_THREAD', 'forum_thread');
  106. define('TOOL_FORUM_POST', 'forum_post');
  107. define('TOOL_FORUM_ATTACH', 'forum_attachment');
  108. define('TOOL_FORUM_THREAD_QUALIFY', 'forum_thread_qualify');
  109. define('TOOL_THREAD', 'thread');
  110. define('TOOL_POST', 'post');
  111. define('TOOL_DROPBOX', 'dropbox');
  112. define('TOOL_QUIZ', 'quiz');
  113. define('TOOL_TEST_CATEGORY', 'test_category');
  114. define('TOOL_USER', 'user');
  115. define('TOOL_GROUP', 'group');
  116. define('TOOL_BLOGS', 'blog_management');
  117. define('TOOL_CHAT', 'chat');
  118. define('TOOL_STUDENTPUBLICATION', 'student_publication');
  119. define('TOOL_TRACKING', 'tracking');
  120. define('TOOL_HOMEPAGE_LINK', 'homepage_link');
  121. define('TOOL_COURSE_SETTING', 'course_setting');
  122. define('TOOL_BACKUP', 'backup');
  123. define('TOOL_COPY_COURSE_CONTENT', 'copy_course_content');
  124. define('TOOL_RECYCLE_COURSE', 'recycle_course');
  125. define('TOOL_COURSE_HOMEPAGE', 'course_homepage');
  126. define('TOOL_COURSE_RIGHTS_OVERVIEW', 'course_rights');
  127. define('TOOL_UPLOAD', 'file_upload');
  128. define('TOOL_COURSE_MAINTENANCE', 'course_maintenance');
  129. define('TOOL_SURVEY', 'survey');
  130. define('TOOL_WIKI', 'wiki');
  131. define('TOOL_GLOSSARY', 'glossary');
  132. define('TOOL_GRADEBOOK', 'gradebook');
  133. define('TOOL_NOTEBOOK', 'notebook');
  134. define('TOOL_ATTENDANCE', 'attendance');
  135. define('TOOL_COURSE_PROGRESS', 'course_progress');
  136. // CONSTANTS defining Chamilo interface sections
  137. define('SECTION_CAMPUS', 'mycampus');
  138. define('SECTION_COURSES', 'mycourses');
  139. define('SECTION_CATALOG', 'catalog');
  140. define('SECTION_MYPROFILE', 'myprofile');
  141. define('SECTION_MYAGENDA', 'myagenda');
  142. define('SECTION_COURSE_ADMIN', 'course_admin');
  143. define('SECTION_PLATFORM_ADMIN', 'platform_admin');
  144. define('SECTION_MYGRADEBOOK', 'mygradebook');
  145. define('SECTION_TRACKING', 'session_my_space');
  146. define('SECTION_SOCIAL', 'social-network');
  147. define('SECTION_DASHBOARD', 'dashboard');
  148. define('SECTION_REPORTS', 'reports');
  149. define('SECTION_GLOBAL', 'global');
  150. // CONSTANT name for local authentication source
  151. define('PLATFORM_AUTH_SOURCE', 'platform');
  152. define('CAS_AUTH_SOURCE', 'cas');
  153. define('LDAP_AUTH_SOURCE', 'extldap');
  154. // CONSTANT defining the default HotPotatoes files directory
  155. define('DIR_HOTPOTATOES', '/HotPotatoes_files');
  156. // event logs types
  157. define('LOG_COURSE_DELETE', 'course_deleted');
  158. define('LOG_COURSE_CREATE', 'course_created');
  159. // @todo replace 'soc_gr' with social_group
  160. define('LOG_GROUP_PORTAL_CREATED', 'soc_gr_created');
  161. define('LOG_GROUP_PORTAL_UPDATED', 'soc_gr_updated');
  162. define('LOG_GROUP_PORTAL_DELETED', 'soc_gr_deleted');
  163. define('LOG_GROUP_PORTAL_USER_DELETE_ALL', 'soc_gr_delete_users');
  164. define('LOG_GROUP_PORTAL_ID', 'soc_gr_portal_id');
  165. define('LOG_GROUP_PORTAL_REL_USER_ARRAY', 'soc_gr_user_array');
  166. define('LOG_GROUP_PORTAL_USER_SUBSCRIBED', 'soc_gr_u_subs');
  167. define('LOG_GROUP_PORTAL_USER_UNSUBSCRIBED', 'soc_gr_u_unsubs');
  168. define('LOG_GROUP_PORTAL_USER_UPDATE_ROLE', 'soc_gr_update_role');
  169. define('LOG_USER_DELETE', 'user_deleted');
  170. define('LOG_USER_CREATE', 'user_created');
  171. define('LOG_USER_ENABLE', 'user_enable');
  172. define('LOG_USER_DISABLE', 'user_disable');
  173. define('LOG_USER_FIELD_CREATE', 'user_field_created');
  174. define('LOG_USER_FIELD_DELETE', 'user_field_deleted');
  175. define('LOG_SESSION_CREATE', 'session_created');
  176. define('LOG_SESSION_DELETE', 'session_deleted');
  177. define('LOG_SESSION_ADD_USER_COURSE', 'session_add_user_course');
  178. define('LOG_SESSION_DELETE_USER_COURSE', 'session_delete_user_course');
  179. define('LOG_SESSION_DELETE_USER', 'session_delete_user');
  180. define('LOG_SESSION_ADD_COURSE', 'session_add_course');
  181. define('LOG_SESSION_DELETE_COURSE', 'session_delete_course');
  182. define('LOG_SESSION_CATEGORY_CREATE', 'session_cat_created'); //changed in 1.9.8
  183. define('LOG_SESSION_CATEGORY_DELETE', 'session_cat_deleted'); //changed in 1.9.8
  184. define('LOG_CONFIGURATION_SETTINGS_CHANGE', 'settings_changed');
  185. define('LOG_PLATFORM_LANGUAGE_CHANGE', 'platform_lng_changed'); //changed in 1.9.8
  186. define('LOG_SUBSCRIBE_USER_TO_COURSE', 'user_subscribed');
  187. define('LOG_UNSUBSCRIBE_USER_FROM_COURSE', 'user_unsubscribed');
  188. define('LOG_ATTEMPTED_FORCED_LOGIN', 'attempted_forced_login');
  189. define('LOG_HOMEPAGE_CHANGED', 'homepage_changed');
  190. define('LOG_PROMOTION_CREATE', 'promotion_created');
  191. define('LOG_PROMOTION_DELETE', 'promotion_deleted');
  192. define('LOG_CAREER_CREATE', 'career_created');
  193. define('LOG_CAREER_DELETE', 'career_deleted');
  194. define('LOG_USER_PERSONAL_DOC_DELETED', 'user_doc_deleted');
  195. define('LOG_WIKI_ACCESS', 'wiki_page_view');
  196. define('LOG_EXERCISE_RESULT_DELETE', 'exe_result_deleted');
  197. define('LOG_LP_ATTEMPT_DELETE', 'lp_attempt_deleted');
  198. define('LOG_QUESTION_RESULT_DELETE', 'qst_attempt_deleted');
  199. define('LOG_MY_FOLDER_CREATE', 'my_folder_created');
  200. define('LOG_MY_FOLDER_CHANGE', 'my_folder_changed');
  201. define('LOG_MY_FOLDER_DELETE', 'my_folder_deleted');
  202. define('LOG_MY_FOLDER_COPY', 'my_folder_copied');
  203. define('LOG_MY_FOLDER_CUT', 'my_folder_cut');
  204. define('LOG_MY_FOLDER_PASTE', 'my_folder_pasted');
  205. define('LOG_MY_FOLDER_UPLOAD', 'my_folder_uploaded');
  206. // Event logs data types (max 20 chars)
  207. define('LOG_COURSE_CODE', 'course_code');
  208. define('LOG_COURSE_ID', 'course_id');
  209. define('LOG_USER_ID', 'user_id');
  210. define('LOG_USER_OBJECT', 'user_object');
  211. define('LOG_USER_FIELD_VARIABLE', 'user_field_variable');
  212. define('LOG_SESSION_ID', 'session_id');
  213. define('LOG_SESSION_CATEGORY_ID', 'session_category_id');
  214. define('LOG_CONFIGURATION_SETTINGS_CATEGORY', 'settings_category');
  215. define('LOG_CONFIGURATION_SETTINGS_VARIABLE', 'settings_variable');
  216. define('LOG_PLATFORM_LANGUAGE', 'default_platform_language');
  217. define('LOG_CAREER_ID', 'career_id');
  218. define('LOG_PROMOTION_ID', 'promotion_id');
  219. define('LOG_GRADEBOOK_LOCKED', 'gradebook_locked');
  220. define('LOG_GRADEBOOK_UNLOCKED', 'gradebook_unlocked');
  221. define('LOG_GRADEBOOK_ID', 'gradebook_id');
  222. define('LOG_WIKI_PAGE_ID', 'wiki_page_id');
  223. define('LOG_EXERCISE_ID', 'exercise_id');
  224. define('LOG_EXERCISE_AND_USER_ID', 'exercise_and_user_id');
  225. define('LOG_LP_ID', 'lp_id');
  226. define('LOG_EXERCISE_ATTEMPT_QUESTION_ID', 'exercise_a_q_id');
  227. define('LOG_WORK_DIR_DELETE', 'work_dir_delete');
  228. define('LOG_WORK_FILE_DELETE', 'work_file_delete');
  229. define('LOG_WORK_DATA', 'work_data_array');
  230. define('LOG_MY_FOLDER_PATH', 'path');
  231. define('LOG_MY_FOLDER_NEW_PATH', 'new_path');
  232. define('USERNAME_PURIFIER', '/[^0-9A-Za-z_\.]/');
  233. //used when login_is_email setting is true
  234. define('USERNAME_PURIFIER_MAIL', '/[^0-9A-Za-z_\.@]/');
  235. define('USERNAME_PURIFIER_SHALLOW', '/\s/');
  236. // This constant is a result of Windows OS detection, it has a boolean value:
  237. // true whether the server runs on Windows OS, false otherwise.
  238. define('IS_WINDOWS_OS', api_is_windows_os());
  239. // Checks for installed optional php-extensions.
  240. define('INTL_INSTALLED', function_exists('intl_get_error_code')); // intl extension (from PECL), it is installed by default as of PHP 5.3.0
  241. define('ICONV_INSTALLED', function_exists('iconv')); // iconv extension, for PHP5 on Windows it is installed by default.
  242. define('MBSTRING_INSTALLED', function_exists('mb_strlen')); // mbstring extension.
  243. // Patterns for processing paths. // Examples:
  244. define('REPEATED_SLASHES_PURIFIER', '/\/{2,}/'); // $path = preg_replace(REPEATED_SLASHES_PURIFIER, '/', $path);
  245. define('VALID_WEB_PATH', '/https?:\/\/[^\/]*(\/.*)?/i'); // $is_valid_path = preg_match(VALID_WEB_PATH, $path);
  246. define('VALID_WEB_SERVER_BASE', '/https?:\/\/[^\/]*/i'); // $new_path = preg_replace(VALID_WEB_SERVER_BASE, $new_base, $path);
  247. // Constants for api_get_path() and api_get_path_type(), etc. - registered path types.
  248. // basic (leaf elements)
  249. define('REL_CODE_PATH', 'REL_CODE_PATH');
  250. define('REL_COURSE_PATH', 'REL_COURSE_PATH');
  251. define('REL_HOME_PATH', 'REL_HOME_PATH');
  252. // Constants for api_get_path() and api_get_path_type(), etc. - registered path types.
  253. define('WEB_PATH', 'WEB_PATH');
  254. define('WEB_APP_PATH', 'WEB_APP_PATH');
  255. define('SYS_PATH', 'SYS_PATH');
  256. define('SYS_APP_PATH', 'SYS_APP_PATH');
  257. define('SYS_UPLOAD_PATH', 'SYS_UPLOAD_PATH');
  258. define('WEB_UPLOAD_PATH', 'WEB_UPLOAD_PATH');
  259. define('REL_PATH', 'REL_PATH');
  260. define('WEB_SERVER_ROOT_PATH', 'WEB_SERVER_ROOT_PATH');
  261. define('SYS_SERVER_ROOT_PATH', 'SYS_SERVER_ROOT_PATH');
  262. define('WEB_COURSE_PATH', 'WEB_COURSE_PATH');
  263. define('SYS_COURSE_PATH', 'SYS_COURSE_PATH');
  264. define('REL_UPLOAD_PATH', 'REL_UPLOAD_PATH');
  265. define('WEB_CODE_PATH', 'WEB_CODE_PATH');
  266. define('SYS_CODE_PATH', 'SYS_CODE_PATH');
  267. define('SYS_LANG_PATH', 'SYS_LANG_PATH');
  268. define('WEB_IMG_PATH', 'WEB_IMG_PATH');
  269. define('SYS_IMG_PATH', 'SYS_IMG_PATH');
  270. define('WEB_CSS_PATH', 'WEB_CSS_PATH');
  271. define('WEB_PUBLIC_PATH', 'WEB_PUBLIC_PATH');
  272. define('SYS_CSS_PATH', 'SYS_CSS_PATH');
  273. define('SYS_PLUGIN_PATH', 'SYS_PLUGIN_PATH');
  274. define('WEB_PLUGIN_PATH', 'WEB_PLUGIN_PATH');
  275. define('SYS_ARCHIVE_PATH', 'SYS_ARCHIVE_PATH');
  276. define('WEB_ARCHIVE_PATH', 'WEB_ARCHIVE_PATH');
  277. define('SYS_INC_PATH', 'SYS_INC_PATH');
  278. define('LIBRARY_PATH', 'LIBRARY_PATH');
  279. define('CONFIGURATION_PATH', 'CONFIGURATION_PATH');
  280. define('WEB_LIBRARY_PATH', 'WEB_LIBRARY_PATH');
  281. define('WEB_LIBRARY_JS_PATH', 'WEB_LIBRARY_JS_PATH');
  282. define('WEB_AJAX_PATH', 'WEB_AJAX_PATH');
  283. define('SYS_TEST_PATH', 'SYS_TEST_PATH');
  284. define('WEB_TEMPLATE_PATH', 'WEB_TEMPLATE_PATH');
  285. define('SYS_TEMPLATE_PATH', 'SYS_TEMPLATE_PATH');
  286. define('SYS_PUBLIC_PATH', 'SYS_PUBLIC_PATH');
  287. define('SYS_HOME_PATH', 'SYS_HOME_PATH');
  288. define('WEB_HOME_PATH', 'WEB_HOME_PATH');
  289. define('WEB_FONTS_PATH', 'WEB_FONTS_PATH');
  290. define('SYS_FONTS_PATH', 'SYS_FONTS_PATH');
  291. define('SYS_DEFAULT_COURSE_DOCUMENT_PATH', 'SYS_DEFAULT_COURSE_DOCUMENT_PATH');
  292. define('REL_DEFAULT_COURSE_DOCUMENT_PATH', 'REL_DEFAULT_COURSE_DOCUMENT_PATH');
  293. define('WEB_DEFAULT_COURSE_DOCUMENT_PATH', 'WEB_DEFAULT_COURSE_DOCUMENT_PATH');
  294. // Relations type with Course manager
  295. define('COURSE_RELATION_TYPE_COURSE_MANAGER', 1);
  296. define('SESSION_RELATION_TYPE_COURSE_MANAGER', 1);
  297. // Relations type with Human resources manager
  298. define('COURSE_RELATION_TYPE_RRHH', 1);
  299. define('SESSION_RELATION_TYPE_RRHH', 1);
  300. //User image sizes
  301. define('USER_IMAGE_SIZE_ORIGINAL', 1);
  302. define('USER_IMAGE_SIZE_BIG', 2);
  303. define('USER_IMAGE_SIZE_MEDIUM', 3);
  304. define('USER_IMAGE_SIZE_SMALL', 4);
  305. // Relation type between users
  306. define('USER_UNKNOW', 0);
  307. define('USER_RELATION_TYPE_UNKNOW', 1);
  308. define('USER_RELATION_TYPE_PARENT', 2); // should be deprecated is useless
  309. define('USER_RELATION_TYPE_FRIEND', 3);
  310. define('USER_RELATION_TYPE_GOODFRIEND', 4); // should be deprecated is useless
  311. define('USER_RELATION_TYPE_ENEMY', 5); // should be deprecated is useless
  312. define('USER_RELATION_TYPE_DELETED', 6);
  313. define('USER_RELATION_TYPE_RRHH', 7);
  314. define('USER_RELATION_TYPE_BOSS', 8);
  315. // Gradebook link constants
  316. // Please do not change existing values, they are used in the database !
  317. define('GRADEBOOK_ITEM_LIMIT', 1000);
  318. define('LINK_EXERCISE', 1);
  319. define('LINK_DROPBOX', 2);
  320. define('LINK_STUDENTPUBLICATION', 3);
  321. define('LINK_LEARNPATH', 4);
  322. define('LINK_FORUM_THREAD', 5);
  323. //define('LINK_WORK',6);
  324. define('LINK_ATTENDANCE', 7);
  325. define('LINK_SURVEY', 8);
  326. define('LINK_HOTPOTATOES', 9);
  327. // Score display types constants
  328. define('SCORE_DIV', 1); // X / Y
  329. define('SCORE_PERCENT', 2); // XX %
  330. define('SCORE_DIV_PERCENT', 3); // X / Y (XX %)
  331. define('SCORE_AVERAGE', 4); // XX %
  332. define('SCORE_DECIMAL', 5); // 0.50 (X/Y)
  333. define('SCORE_BAR', 6); // Uses the Display::bar_progress function
  334. define('SCORE_SIMPLE', 7); // X
  335. define('SCORE_IGNORE_SPLIT', 8); // ??
  336. define('SCORE_DIV_PERCENT_WITH_CUSTOM', 9); // X / Y (XX %) - Good!
  337. define('SCORE_CUSTOM', 10); // Good!
  338. define('SCORE_DIV_SIMPLE_WITH_CUSTOM', 11); // X - Good!
  339. define('SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS', 12); // X - Good!
  340. define('SCORE_ONLY_SCORE', 13); // X - Good!
  341. define('SCORE_BOTH', 1);
  342. define('SCORE_ONLY_DEFAULT', 2);
  343. define('SCORE_ONLY_CUSTOM', 3);
  344. // From display.lib.php
  345. define('MAX_LENGTH_BREADCRUMB', 100);
  346. define('ICON_SIZE_ATOM', 8);
  347. define('ICON_SIZE_TINY', 16);
  348. define('ICON_SIZE_SMALL', 22);
  349. define('ICON_SIZE_MEDIUM', 32);
  350. define('ICON_SIZE_LARGE', 48);
  351. define('ICON_SIZE_BIG', 64);
  352. define('ICON_SIZE_HUGE', 128);
  353. define('SHOW_TEXT_NEAR_ICONS', false);
  354. // Session catalog
  355. define('CATALOG_COURSES', 0);
  356. define('CATALOG_SESSIONS', 1);
  357. define('CATALOG_COURSES_SESSIONS', 2);
  358. // Hook type events, pre-process and post-process.
  359. // All means to be executed for both hook event types
  360. define('HOOK_EVENT_TYPE_PRE', 0);
  361. define('HOOK_EVENT_TYPE_POST', 1);
  362. define('HOOK_EVENT_TYPE_ALL', 10);
  363. define('CAREER_STATUS_ACTIVE', 1);
  364. define('CAREER_STATUS_INACTIVE', 0);
  365. define('PROMOTION_STATUS_ACTIVE', 1);
  366. define('PROMOTION_STATUS_INACTIVE', 0);
  367. // Group permissions
  368. define('GROUP_PERMISSION_OPEN', '1');
  369. define('GROUP_PERMISSION_CLOSED', '2');
  370. // Group user permissions
  371. define('GROUP_USER_PERMISSION_ADMIN', '1'); // the admin of a group
  372. define('GROUP_USER_PERMISSION_READER', '2'); // a normal user
  373. define('GROUP_USER_PERMISSION_PENDING_INVITATION', '3'); // When an admin/moderator invites a user
  374. define('GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER', '4'); // an user joins a group
  375. define('GROUP_USER_PERMISSION_MODERATOR', '5'); // a moderator
  376. define('GROUP_USER_PERMISSION_ANONYMOUS', '6'); // an anonymous user
  377. define('GROUP_USER_PERMISSION_HRM', '7'); // a human resources manager
  378. define('GROUP_IMAGE_SIZE_ORIGINAL', 1);
  379. define('GROUP_IMAGE_SIZE_BIG', 2);
  380. define('GROUP_IMAGE_SIZE_MEDIUM', 3);
  381. define('GROUP_IMAGE_SIZE_SMALL', 4);
  382. define('GROUP_TITLE_LENGTH', 50);
  383. // Exercise
  384. // @todo move into a class
  385. define('ALL_ON_ONE_PAGE', 1);
  386. define('ONE_PER_PAGE', 2);
  387. define('EXERCISE_FEEDBACK_TYPE_END', 0); //Feedback - show score and expected answers
  388. define('EXERCISE_FEEDBACK_TYPE_DIRECT', 1); //DirectFeedback - Do not show score nor answers
  389. define('EXERCISE_FEEDBACK_TYPE_EXAM', 2); //NoFeedback - Show score only
  390. define('RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS', 0); //show score and expected answers
  391. define('RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS', 1); //Do not show score nor answers
  392. define('RESULT_DISABLE_SHOW_SCORE_ONLY', 2); //Show score only
  393. define('RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES', 3); //Show final score only with categories
  394. define('RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT', 4); //Show final score only with categories
  395. define('EXERCISE_MAX_NAME_SIZE', 80);
  396. // Question types (edit next array as well when adding values)
  397. // @todo move into a class
  398. define('UNIQUE_ANSWER', 1);
  399. define('MULTIPLE_ANSWER', 2);
  400. define('FILL_IN_BLANKS', 3);
  401. define('MATCHING', 4);
  402. define('FREE_ANSWER', 5);
  403. define('HOT_SPOT', 6);
  404. define('HOT_SPOT_ORDER', 7);
  405. define('HOT_SPOT_DELINEATION', 8);
  406. define('MULTIPLE_ANSWER_COMBINATION', 9);
  407. define('UNIQUE_ANSWER_NO_OPTION', 10);
  408. define('MULTIPLE_ANSWER_TRUE_FALSE', 11);
  409. define('MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE', 12);
  410. define('ORAL_EXPRESSION', 13);
  411. define('GLOBAL_MULTIPLE_ANSWER', 14);
  412. define('MEDIA_QUESTION', 15);
  413. define('CALCULATED_ANSWER', 16);
  414. define('UNIQUE_ANSWER_IMAGE', 17);
  415. define('DRAGGABLE', 18);
  416. define('MATCHING_DRAGGABLE', 19);
  417. define('EXERCISE_CATEGORY_RANDOM_SHUFFLED', 1);
  418. define('EXERCISE_CATEGORY_RANDOM_ORDERED', 2);
  419. define('EXERCISE_CATEGORY_RANDOM_DISABLED', 0);
  420. // Question selection type
  421. define('EX_Q_SELECTION_ORDERED', 1);
  422. define('EX_Q_SELECTION_RANDOM', 2);
  423. define('EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_ORDERED', 3);
  424. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED', 4);
  425. define('EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM', 5);
  426. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM', 6);
  427. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED', 7);
  428. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED', 8);
  429. define('EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_ORDERED', 9);
  430. define('EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_RANDOM', 10);
  431. // one big string with all question types, for the validator in pear/HTML/QuickForm/Rule/QuestionType
  432. define('QUESTION_TYPES',
  433. UNIQUE_ANSWER.':'.
  434. MULTIPLE_ANSWER.':'.
  435. FILL_IN_BLANKS.':'.
  436. MATCHING.':'.
  437. FREE_ANSWER.':'.
  438. HOT_SPOT.':'.
  439. HOT_SPOT_ORDER.':'.
  440. HOT_SPOT_DELINEATION.':'.
  441. MULTIPLE_ANSWER_COMBINATION.':'.
  442. UNIQUE_ANSWER_NO_OPTION.':'.
  443. MULTIPLE_ANSWER_TRUE_FALSE.':'.
  444. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE.':'.
  445. ORAL_EXPRESSION.':'.
  446. GLOBAL_MULTIPLE_ANSWER.':'.
  447. MEDIA_QUESTION.':'.
  448. CALCULATED_ANSWER.':'.
  449. UNIQUE_ANSWER_IMAGE.':'.
  450. DRAGGABLE.':'.
  451. MATCHING_DRAGGABLE
  452. );
  453. //Some alias used in the QTI exports
  454. define('MCUA', 1);
  455. define('TF', 1);
  456. define('MCMA', 2);
  457. define('FIB', 3);
  458. // Skills
  459. define('SKILL_TYPE_REQUIREMENT', 'required');
  460. define('SKILL_TYPE_ACQUIRED', 'acquired');
  461. define('SKILL_TYPE_BOTH', 'both');
  462. // Message
  463. define('MESSAGE_STATUS_NEW', '0');
  464. define('MESSAGE_STATUS_UNREAD', '1');
  465. //2 ??
  466. define('MESSAGE_STATUS_DELETED', '3');
  467. define('MESSAGE_STATUS_OUTBOX', '4');
  468. define('MESSAGE_STATUS_INVITATION_PENDING', '5');
  469. define('MESSAGE_STATUS_INVITATION_ACCEPTED', '6');
  470. define('MESSAGE_STATUS_INVITATION_DENIED', '7');
  471. define('MESSAGE_STATUS_WALL', '8');
  472. define('MESSAGE_STATUS_WALL_DELETE', '9');
  473. define('MESSAGE_STATUS_WALL_POST', '10');
  474. // Images
  475. define('IMAGE_WALL_SMALL_SIZE', 200);
  476. define('IMAGE_WALL_MEDIUM_SIZE', 500);
  477. define('IMAGE_WALL_BIG_SIZE', 2000);
  478. define('IMAGE_WALL_SMALL', 'small');
  479. define('IMAGE_WALL_MEDIUM', 'medium');
  480. define('IMAGE_WALL_BIG', 'big');
  481. // Social PLUGIN PLACES
  482. define('SOCIAL_LEFT_PLUGIN', 1);
  483. define('SOCIAL_CENTER_PLUGIN', 2);
  484. define('SOCIAL_RIGHT_PLUGIN', 3);
  485. define('CUT_GROUP_NAME', 50);
  486. /**
  487. * FormValidator Filter
  488. */
  489. define('NO_HTML', 1);
  490. define('STUDENT_HTML', 2);
  491. define('TEACHER_HTML', 3);
  492. define('STUDENT_HTML_FULLPAGE', 4);
  493. define('TEACHER_HTML_FULLPAGE', 5);
  494. // Timeline
  495. define('TIMELINE_STATUS_ACTIVE', '1');
  496. define('TIMELINE_STATUS_INACTIVE', '2');
  497. // Event email template class
  498. define('EVENT_EMAIL_TEMPLATE_ACTIVE', 1);
  499. define('EVENT_EMAIL_TEMPLATE_INACTIVE', 0);
  500. // Course home
  501. define('SHORTCUTS_HORIZONTAL', 0);
  502. define('SHORTCUTS_VERTICAL', 1);
  503. // Image class
  504. define('IMAGE_PROCESSOR', 'gd'); // 'imagick' or 'gd' strings
  505. // Course copy
  506. define('FILE_SKIP', 1);
  507. define('FILE_RENAME', 2);
  508. define('FILE_OVERWRITE', 3);
  509. define('UTF8_CONVERT', false); //false by default
  510. define('DOCUMENT','file');
  511. define('FOLDER','folder');
  512. define('RESOURCE_DOCUMENT', 'document');
  513. define('RESOURCE_GLOSSARY', 'glossary');
  514. define('RESOURCE_EVENT', 'calendar_event');
  515. define('RESOURCE_LINK', 'link');
  516. define('RESOURCE_COURSEDESCRIPTION', 'course_description');
  517. define('RESOURCE_LEARNPATH', 'learnpath');
  518. define('RESOURCE_ANNOUNCEMENT', 'announcement');
  519. define('RESOURCE_FORUM', 'forum');
  520. define('RESOURCE_FORUMTOPIC', 'thread');
  521. define('RESOURCE_FORUMPOST', 'post');
  522. define('RESOURCE_QUIZ', 'quiz');
  523. define('RESOURCE_TEST_CATEGORY', 'test_category');
  524. define('RESOURCE_QUIZQUESTION', 'Exercise_Question');
  525. define('RESOURCE_TOOL_INTRO', 'Tool introduction');
  526. define('RESOURCE_LINKCATEGORY', 'Link_Category');
  527. define('RESOURCE_FORUMCATEGORY', 'Forum_Category');
  528. define('RESOURCE_SCORM', 'Scorm');
  529. define('RESOURCE_SURVEY', 'survey');
  530. define('RESOURCE_SURVEYQUESTION', 'survey_question');
  531. define('RESOURCE_SURVEYINVITATION', 'survey_invitation');
  532. define('RESOURCE_WIKI', 'wiki');
  533. define('RESOURCE_THEMATIC', 'thematic');
  534. define('RESOURCE_ATTENDANCE', 'attendance');
  535. define('RESOURCE_WORK', 'work');
  536. define('RESOURCE_SESSION_COURSE', 'session_course');
  537. define('RESOURCE_GRADEBOOK', 'gradebook');
  538. define('ADD_THEMATIC_PLAN', 6);
  539. // Max online users to show per page (whoisonline)
  540. define('MAX_ONLINE_USERS', 12);
  541. // Make sure the CHAMILO_LOAD_WYSIWYG constant is defined
  542. // To remove CKeditor libs from HTML, set this constant to true before loading
  543. if (!defined('CHAMILO_LOAD_WYSIWYG')) {
  544. define('CHAMILO_LOAD_WYSIWYG', true);
  545. }
  546. define('TOOL_PUBLIC', 'Public');
  547. define('TOOL_PUBLIC_BUT_HIDDEN', 'PublicButHide');
  548. define('TOOL_COURSE_ADMIN', 'courseAdmin');
  549. define('TOOL_PLATFORM_ADMIN', 'platformAdmin');
  550. define('TOOL_AUTHORING', 'toolauthoring');
  551. define('TOOL_INTERACTION', 'toolinteraction');
  552. define('TOOL_COURSE_PLUGIN', 'toolcourseplugin'); //all plugins that can be enabled in courses
  553. define('TOOL_ADMIN', 'tooladmin');
  554. define('TOOL_ADMIN_PLATFORM', 'tooladminplatform');
  555. define('TOOL_DRH', 'tool_drh');
  556. define('TOOL_STUDENT_VIEW', 'toolstudentview');
  557. define('TOOL_ADMIN_VISIBLE', 'tooladminvisible');
  558. // CONSTANTS defining all tools, using the english version
  559. /* When you add a new tool you must add it into function api_get_tools_lists() too */
  560. define('TOOL_BLOG', 'blog');
  561. define('TOOL_CURRICULUM', 'curriculum');
  562. // Authentication, password
  563. define('CHECK_PASS_EASY_TO_FIND', false);
  564. // User photos
  565. define('PREFIX_IMAGE_FILENAME_WITH_UID', true); // If true, filename of images on server begin with uid of the user.
  566. // Replacing user photos
  567. define('KEEP_THE_NAME_WHEN_CHANGE_IMAGE', true);
  568. // true -> the new image have the name of previous.
  569. // false -> a new name is build for each upladed image.
  570. define('KEEP_THE_OLD_IMAGE_AFTER_CHANGE', true);
  571. // true -> if KEEP_THE_NAME_WHEN_CHANGE_IMAGE is true, the previous image is rename before.
  572. // false -> only the last image still on server.
  573. // Official code
  574. // Don't forget to change name of offical code in your organization
  575. // See $langOfficialCode within the language file 'registration'
  576. define('CONFVAL_ASK_FOR_OFFICIAL_CODE', true); // not used but name fixed
  577. define('FIRST_EXPIRATION_DELAY', 31536000); // <- 86400*365 // 60*60*24 = 1 jour = 86400
  578. /**
  579. * Inclusion of internationalization libraries
  580. */
  581. require_once __DIR__.'/internationalization.lib.php';
  582. /**
  583. * Returns a path to a certain resource within the Chamilo area, specifyed through a parameter.
  584. * Also, this function provides conversion between path types, in this case the input path points inside the Chamilo area too.
  585. *
  586. * See $_configuration['course_folder'] in the configuration.php to alter the WEB_COURSE_PATH and SYS_COURSE_PATH parameters.
  587. * @param string $path (optional) A path which type is to be converted. Also, it may be a defined constant for a path.
  588. * This parameter has meaning when $type parameter has one of the following values: TO_WEB, TO_SYS, TO_REL. Otherwise it is ignored.
  589. * @return string The requested path or the converted path.
  590. *
  591. *
  592. * Notes about the current behaviour model:
  593. * 1. Windows back-slashes are converted to slashes in the result.
  594. * 2. A semi-absolute web-path is detected by its leading slash. On Linux systems, absolute system paths start with
  595. * a slash too, so an additional check about presence of leading system server base is implemented. For example, the function is
  596. * able to distinguish type difference between /var/www/chamilo/courses/ (SYS) and /chamilo/courses/ (REL).
  597. * 3. The function api_get_path() returns only these three types of paths, which in some sense are absolute. The function has
  598. * no a mechanism for processing relative web/system paths, such as: lesson01.html, ./lesson01.html, ../css/my_styles.css.
  599. * It has not been identified as needed yet.
  600. * 4. Also, resolving the meta-symbols "." and ".." within paths has not been implemented, it is to be identified as needed.
  601. *
  602. * For examples go to: *
  603. * See main/admin/system_status.php?section=paths
  604. *
  605. * Vchamilo changes : allow using an alternate configuration
  606. * to get vchamilo instance paths
  607. */
  608. function api_get_path($path)
  609. {
  610. static $paths = array(
  611. WEB_PATH => '',
  612. SYS_PATH => '',
  613. REL_PATH => '',
  614. WEB_SERVER_ROOT_PATH => '',
  615. SYS_SERVER_ROOT_PATH => '',
  616. WEB_COURSE_PATH => '',
  617. SYS_COURSE_PATH => '',
  618. REL_COURSE_PATH => '',
  619. REL_CODE_PATH => '',
  620. WEB_CODE_PATH => '',
  621. SYS_CODE_PATH => '',
  622. SYS_LANG_PATH => 'lang/',
  623. WEB_IMG_PATH => 'web/bundles/chamilocore/img/',
  624. SYS_IMG_PATH => 'web/bundles/chamilocore/img/',
  625. WEB_CSS_PATH => 'web/css/',
  626. SYS_CSS_PATH => 'app/Resources/public/css/',
  627. SYS_PLUGIN_PATH => 'plugin/',
  628. WEB_PLUGIN_PATH => 'plugin/',
  629. SYS_ARCHIVE_PATH => 'app/cache/',
  630. WEB_ARCHIVE_PATH => 'app/cache/',
  631. SYS_HOME_PATH => 'app/home/',
  632. WEB_HOME_PATH => 'app/home/',
  633. REL_HOME_PATH => 'app/home/',
  634. SYS_APP_PATH => 'app/',
  635. WEB_APP_PATH => 'app/',
  636. SYS_UPLOAD_PATH => 'app/upload/',
  637. REL_UPLOAD_PATH => 'app/upload/',
  638. SYS_INC_PATH => 'inc/',
  639. LIBRARY_PATH => 'inc/lib/',
  640. CONFIGURATION_PATH => 'app/config/',
  641. WEB_LIBRARY_PATH => 'inc/lib/',
  642. WEB_LIBRARY_JS_PATH => 'web/bundles/chamilocore/js/',
  643. WEB_AJAX_PATH => 'inc/ajax/',
  644. SYS_TEST_PATH => 'tests/',
  645. WEB_TEMPLATE_PATH => 'template/',
  646. SYS_TEMPLATE_PATH => 'template/',
  647. WEB_UPLOAD_PATH => 'app/upload/',
  648. WEB_PUBLIC_PATH => 'web/',
  649. SYS_PUBLIC_PATH => 'web/',
  650. WEB_FONTS_PATH => 'fonts/',
  651. SYS_FONTS_PATH => 'fonts/',
  652. );
  653. static $is_this_function_initialized;
  654. static $server_base_web; // No trailing slash.
  655. static $server_base_sys; // No trailing slash.
  656. static $root_rel;
  657. $root_web = Container::getUrlGenerator()->generate(
  658. 'home',
  659. []
  660. );
  661. //var_dump($root_web);
  662. $rootDir = Container::getRootDir();
  663. // Configuration data for already installed system.
  664. $root_sys = $rootDir;
  665. $code_folder = 'main/';
  666. $course_folder = 'courses/';
  667. // Configuration data for already installed system.
  668. $load_new_config = false;
  669. // To avoid that the api_get_access_url() function fails since global.inc.php also calls the main_api.lib.php
  670. if ($path == WEB_PATH) {
  671. //$urlId = api_get_current_access_url_id();
  672. $urlId = 1;
  673. if ($urlId != 1) {
  674. //we look into the DB the function api_get_access_url
  675. $url_info = api_get_access_url($urlId);
  676. $root_web = $url_info['active'] == 1 ? $url_info['url'] : $root_web;
  677. $load_new_config = true;
  678. }
  679. }
  680. $root_rel = Container::getUrlAppend();
  681. // Dealing with trailing slashes.
  682. $root_web = api_add_trailing_slash($root_web);
  683. $root_sys = api_add_trailing_slash($root_sys);
  684. $root_rel = api_add_trailing_slash($root_rel);
  685. $code_folder = api_add_trailing_slash($code_folder);
  686. $course_folder = api_add_trailing_slash($course_folder);
  687. // Web server base and system server base.
  688. $server_base_web = preg_replace('@'.$root_rel.'$@', '', $root_web); // No trailing slash.
  689. $server_base_sys = preg_replace('@'.$root_rel.'$@', '', $root_sys); // No trailing slash.
  690. // Initialization of a table that contains common-purpose paths.
  691. $paths[WEB_PATH] = $root_web;
  692. $paths[SYS_PATH] = $root_sys;
  693. $paths[REL_PATH] = $root_rel;
  694. $paths[WEB_SERVER_ROOT_PATH] = $server_base_web.'/';
  695. $paths[SYS_SERVER_ROOT_PATH] = $server_base_sys.'/';
  696. $paths[WEB_COURSE_PATH] = $root_web.$course_folder;
  697. $paths[REL_COURSE_PATH] = $root_rel.$course_folder;
  698. $paths[REL_CODE_PATH] = $root_rel.$code_folder;
  699. $paths[WEB_CODE_PATH] = $root_web.$code_folder;
  700. $paths[SYS_CODE_PATH] = $root_sys.$code_folder;
  701. $paths[REL_UPLOAD_PATH] = $root_rel.$paths[SYS_UPLOAD_PATH];
  702. $paths[WEB_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[WEB_CODE_PATH].'default_course_document/';
  703. $paths[REL_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[REL_PATH].'main/default_course_document/';
  704. // Now we can switch into api_get_path() "terminology".
  705. $paths[SYS_LANG_PATH] = $paths[SYS_CODE_PATH].$paths[SYS_LANG_PATH];
  706. $paths[SYS_APP_PATH] = $paths[SYS_PATH].$paths[SYS_APP_PATH];
  707. $paths[WEB_APP_PATH] = $paths[WEB_PATH].$paths[WEB_APP_PATH];
  708. $paths[SYS_UPLOAD_PATH] = $paths[SYS_PATH].$paths[SYS_UPLOAD_PATH];
  709. $paths[SYS_PLUGIN_PATH] = $paths[SYS_PATH].$paths[SYS_PLUGIN_PATH];
  710. $paths[SYS_ARCHIVE_PATH] = Container::getTempDir();
  711. $paths[SYS_TEST_PATH] = $paths[SYS_PATH].$paths[SYS_TEST_PATH];
  712. $paths[SYS_TEMPLATE_PATH] = $paths[SYS_CODE_PATH].$paths[SYS_TEMPLATE_PATH];
  713. $paths[SYS_PUBLIC_PATH] = $paths[SYS_PATH].$paths[SYS_PUBLIC_PATH];
  714. $paths[SYS_CSS_PATH] = $paths[SYS_PATH].$paths[SYS_CSS_PATH];
  715. $paths[SYS_FONTS_PATH] = $paths[SYS_CODE_PATH].$paths[SYS_FONTS_PATH];
  716. $paths[WEB_CSS_PATH] = $paths[WEB_PATH].$paths[WEB_CSS_PATH];
  717. $bundleWebPath = Container::getAsset()->getUrl('bundles/chamilocore/');
  718. $paths[WEB_IMG_PATH] = $bundleWebPath.'img/';
  719. $paths[SYS_IMG_PATH] = $paths[SYS_PATH].$paths[SYS_IMG_PATH];
  720. $paths[WEB_LIBRARY_PATH] = $paths[WEB_CODE_PATH].$paths[WEB_LIBRARY_PATH];
  721. $paths[WEB_LIBRARY_JS_PATH] = $bundleWebPath.'js/';
  722. $paths[WEB_AJAX_PATH] = $paths[WEB_CODE_PATH].$paths[WEB_AJAX_PATH];
  723. $paths[WEB_FONTS_PATH] = $paths[WEB_CODE_PATH].$paths[WEB_FONTS_PATH];
  724. $paths[WEB_PLUGIN_PATH] = $paths[WEB_PATH].$paths[WEB_PLUGIN_PATH];
  725. $paths[WEB_ARCHIVE_PATH] = $paths[WEB_PATH].$paths[WEB_ARCHIVE_PATH];
  726. $paths[WEB_TEMPLATE_PATH] = $paths[WEB_CODE_PATH].$paths[WEB_TEMPLATE_PATH];
  727. $paths[WEB_UPLOAD_PATH] = $paths[WEB_PATH].$paths[WEB_UPLOAD_PATH];
  728. $paths[WEB_PUBLIC_PATH] = $paths[WEB_PATH].$paths[WEB_PUBLIC_PATH];
  729. $paths[SYS_INC_PATH] = $paths[SYS_CODE_PATH].$paths[SYS_INC_PATH];
  730. $paths[LIBRARY_PATH] = $paths[SYS_CODE_PATH].$paths[LIBRARY_PATH];
  731. $paths[CONFIGURATION_PATH] = $paths[SYS_PATH].$paths[CONFIGURATION_PATH];
  732. $paths[SYS_COURSE_PATH] = Container::getCourseDir();
  733. $is_this_function_initialized = true;
  734. //}
  735. // Shallow purification and validation of input parameters.
  736. $path = trim($path);
  737. // Common-purpose paths as a second parameter - recognition.
  738. if (isset($paths[$path])) {
  739. $path = $paths[$path];
  740. }
  741. // Second purification.
  742. // Replacing Windows back slashes.
  743. $path = str_replace('\\', '/', $path);
  744. // Detection of the input path type. Conversion to semi-absolute type ( /chamilo/main/inc/.... ).
  745. $courseCode = api_get_course_id();
  746. if (preg_match(VALID_WEB_PATH, $path)) {
  747. // A special case: When a URL points to the document download script directly, without
  748. // mod-rewrite translation, we have to translate it into an "ordinary" web path.
  749. // For example:
  750. // http://localhost/chamilo/main/document/download.php?doc_url=/image.png&cDir=/
  751. // becomes
  752. // http://localhost/chamilo/courses/TEST/document/image.png
  753. // TEST is a course directory name, so called "system course code".
  754. if (strpos($path, 'download.php') !== false) { // Fast detection first.
  755. $path = urldecode($path);
  756. if (preg_match('/(.*)main\/document\/download.php\?doc_url=\/(.*)&cDir=\/(.*)?/', $path, $matches)) {
  757. // User is inside a course?
  758. $sys_course_code = isset($courseCode)
  759. ? $courseCode // Yes, then use course's directory name.
  760. : '{SYS_COURSE_CODE}';
  761. // No, then use a fake code, it may be processed later.
  762. $path = $matches[1].'courses/'.$sys_course_code.'/document/'.str_replace('//', '/', $matches[3].'/'.$matches[2]);
  763. }
  764. }
  765. // Replacement of the present web server base with a slash '/'.
  766. $path = preg_replace(VALID_WEB_SERVER_BASE, '/', $path);
  767. }
  768. return $path;
  769. }
  770. /**
  771. * @return bool Return true if CAS authentification is activated
  772. *
  773. */
  774. function api_is_cas_activated() {
  775. return api_get_setting('cas_activate') == "true";
  776. }
  777. /**
  778. * @return bool Return true if LDAP authentification is activated
  779. *
  780. */
  781. function api_is_ldap_activated() {
  782. global $extAuthSource;
  783. return is_array($extAuthSource[LDAP_AUTH_SOURCE]);
  784. }
  785. /**
  786. * This function checks whether a given path points inside the system.
  787. * @param string $path The path to be tested.
  788. * It should be full path, web-absolute (WEB), semi-absolute (REL) or system-absolyte (SYS).
  789. * @return bool Returns true when the given path is inside the system, false otherwise.
  790. */
  791. function api_is_internal_path($path) {
  792. $path = str_replace('\\', '/', trim($path));
  793. if (empty($path)) {
  794. return false;
  795. }
  796. if (strpos($path, api_remove_trailing_slash(api_get_path(WEB_PATH))) === 0) {
  797. return true;
  798. }
  799. if (strpos($path, api_remove_trailing_slash(api_get_path(SYS_PATH))) === 0) {
  800. return true;
  801. }
  802. $server_base_web = api_remove_trailing_slash(api_get_path(REL_PATH));
  803. $server_base_web = empty($server_base_web) ? '/' : $server_base_web;
  804. if (strpos($path, $server_base_web) === 0) {
  805. return true;
  806. }
  807. return false;
  808. }
  809. /**
  810. * Adds to a given path a trailing slash if it is necessary (adds "/" character at the end of the string).
  811. * @param string $path The input path.
  812. * @return string Returns the modified path.
  813. */
  814. function api_add_trailing_slash($path) {
  815. return substr($path, -1) == '/' ? $path : $path.'/';
  816. }
  817. /**
  818. * Removes from a given path the trailing slash if it is necessary (removes "/" character from the end of the string).
  819. * @param string $path The input path.
  820. * @return string Returns the modified path.
  821. */
  822. function api_remove_trailing_slash($path) {
  823. return substr($path, -1) == '/' ? substr($path, 0, -1) : $path;
  824. }
  825. /**
  826. * Checks the RFC 3986 syntax of a given URL.
  827. * @param string $url The URL to be checked.
  828. * @param bool $absolute Whether the URL is absolute (beginning with a scheme such as "http:").
  829. * @return bool Returns the URL if it is valid, FALSE otherwise.
  830. * This function is an adaptation from the function valid_url(), Drupal CMS.
  831. * @link http://drupal.org
  832. * Note: The built-in function filter_var($urs, FILTER_VALIDATE_URL) has a bug for some versions of PHP.
  833. * @link http://bugs.php.net/51192
  834. */
  835. function api_valid_url($url, $absolute = false) {
  836. if ($absolute) {
  837. if (preg_match("
  838. /^ # Start at the beginning of the text
  839. (?:ftp|https?|feed):\/\/ # Look for ftp, http, https or feed schemes
  840. (?: # Userinfo (optional) which is typically
  841. (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
  842. (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
  843. )?
  844. (?:
  845. (?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
  846. |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
  847. )
  848. (?::[0-9]+)? # Server port number (optional)
  849. (?:[\/|\?]
  850. (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
  851. *)?
  852. $/xi", $url)) {
  853. return $url;
  854. }
  855. return false;
  856. } else {
  857. return preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url) ? $url : false;
  858. }
  859. }
  860. /**
  861. * Checks whether a given string looks roughly like an email address.
  862. *
  863. * @param string $address The e-mail address to be checked.
  864. * @return mixed Returns the e-mail if it is valid, FALSE otherwise.
  865. */
  866. function api_valid_email($address)
  867. {
  868. return filter_var($address, FILTER_VALIDATE_EMAIL);
  869. }
  870. /* PROTECTION FUNCTIONS
  871. Use these functions to protect your scripts. */
  872. /**
  873. * Function used to protect a course script.
  874. * The function blocks access when
  875. * - there is no $_SESSION["_course"] defined; or
  876. * - $is_allowed_in_course is set to false (this depends on the course
  877. * visibility and user status).
  878. *
  879. * This is only the first proposal, test and improve!
  880. * @param boolean Option to print headers when displaying error message. Default: false
  881. * @param boolean Whether session admins should be allowed or not.
  882. * @return boolean True if the user has access to the current course or is out of a course context, false otherwise
  883. * @todo replace global variable
  884. * @author Roan Embrechts
  885. */
  886. function api_protect_course_script($print_headers = false, $allow_session_admins = false, $allow_drh = false)
  887. {
  888. $is_allowed_in_course = api_is_allowed_in_course();
  889. $is_visible = false;
  890. $course_info = api_get_course_info();
  891. if (empty($course_info)) {
  892. api_not_allowed($print_headers);
  893. return false;
  894. }
  895. if (api_is_drh()) {
  896. return true;
  897. }
  898. if (api_is_platform_admin($allow_session_admins)) {
  899. return true;
  900. }
  901. if (isset($course_info) && isset($course_info['visibility'])) {
  902. switch ($course_info['visibility']) {
  903. default:
  904. case COURSE_VISIBILITY_CLOSED:
  905. // Completely closed: the course is only accessible to the teachers. - 0
  906. if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
  907. $is_visible = true;
  908. }
  909. break;
  910. case COURSE_VISIBILITY_REGISTERED:
  911. // Private - access authorized to course members only - 1
  912. if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
  913. $is_visible = true;
  914. }
  915. break;
  916. case COURSE_VISIBILITY_OPEN_PLATFORM:
  917. // Open - access allowed for users registered on the platform - 2
  918. if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
  919. $is_visible = true;
  920. }
  921. break;
  922. case COURSE_VISIBILITY_OPEN_WORLD:
  923. //Open - access allowed for the whole world - 3
  924. $is_visible = true;
  925. break;
  926. case COURSE_VISIBILITY_HIDDEN:
  927. //Completely closed: the course is only accessible to the teachers. - 0
  928. if (api_is_platform_admin()) {
  929. $is_visible = true;
  930. }
  931. break;
  932. }
  933. //If password is set and user is not registered to the course then the course is not visible
  934. if ($is_allowed_in_course == false &
  935. isset($course_info['registration_code']) &&
  936. !empty($course_info['registration_code'])
  937. ) {
  938. $is_visible = false;
  939. }
  940. }
  941. // Check session visibility
  942. $session_id = api_get_session_id();
  943. if (!empty($session_id)) {
  944. //$is_allowed_in_course was set in local.inc.php
  945. if (!$is_allowed_in_course) {
  946. $is_visible = false;
  947. }
  948. }
  949. if (!$is_visible) {
  950. api_not_allowed($print_headers);
  951. return false;
  952. }
  953. return true;
  954. }
  955. /**
  956. * Function used to protect an admin script.
  957. *
  958. * The function blocks access when the user has no platform admin rights
  959. * with an error message printed on default output
  960. * @param bool Whether to allow session admins as well
  961. * @param bool Whether to allow HR directors as well
  962. * @param string An optional message (already passed through get_lang)
  963. * @return bool True if user is allowed, false otherwise.
  964. * The function also outputs an error message in case not allowed
  965. * @author Roan Embrechts (original author)
  966. */
  967. function api_protect_admin_script($allow_sessions_admins = false, $allow_drh = false, $message = null)
  968. {
  969. if (!api_is_platform_admin($allow_sessions_admins, $allow_drh)) {
  970. api_not_allowed(true, $message);
  971. return false;
  972. }
  973. return true;
  974. }
  975. /**
  976. * Function used to protect a teacher script.
  977. * The function blocks access when the user has no teacher rights.
  978. *
  979. * @author Yoselyn Castillo
  980. */
  981. function api_protect_teacher_script($allow_sessions_admins = false)
  982. {
  983. if (!api_is_allowed_to_edit()) {
  984. api_not_allowed(true);
  985. return false;
  986. }
  987. return true;
  988. }
  989. /**
  990. * Function used to prevent anonymous users from accessing a script.
  991. * @param bool|true $printHeaders
  992. * @author Roan Embrechts
  993. *
  994. * @return bool
  995. */
  996. function api_block_anonymous_users($printHeaders = true)
  997. {
  998. $user = api_get_user_info();
  999. if (!(isset($user['user_id']) && $user['user_id']) || api_is_anonymous($user['user_id'], true)) {
  1000. api_not_allowed($printHeaders);
  1001. return false;
  1002. }
  1003. return true;
  1004. }
  1005. /**
  1006. * @return array with the navigator name and version
  1007. */
  1008. function api_get_navigator() {
  1009. $navigator = 'Unknown';
  1010. $version = 0;
  1011. if (!isset($_SERVER['HTTP_USER_AGENT'])) {
  1012. return array('name' => 'Unknown', 'version' => '0.0.0');
  1013. }
  1014. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false) {
  1015. $navigator = 'Opera';
  1016. list (, $version) = explode('Opera', $_SERVER['HTTP_USER_AGENT']);
  1017. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
  1018. $navigator = 'Internet Explorer';
  1019. list (, $version) = explode('MSIE', $_SERVER['HTTP_USER_AGENT']);
  1020. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) {
  1021. $navigator = 'Chrome';
  1022. list (, $version) = explode('Chrome', $_SERVER['HTTP_USER_AGENT']);
  1023. } elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) {
  1024. $navigator = 'Safari';
  1025. list (, $version) = explode('Version/', $_SERVER['HTTP_USER_AGENT']);
  1026. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false) {
  1027. $navigator = 'Mozilla';
  1028. list (, $version) = explode('; rv:', $_SERVER['HTTP_USER_AGENT']);
  1029. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') !== false) {
  1030. $navigator = 'Netscape';
  1031. list (, $version) = explode('Netscape', $_SERVER['HTTP_USER_AGENT']);
  1032. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Konqueror') !== false) {
  1033. $navigator = 'Konqueror';
  1034. list (, $version) = explode('Konqueror', $_SERVER['HTTP_USER_AGENT']);
  1035. } elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'applewebkit') !== false) {
  1036. $navigator = 'AppleWebKit';
  1037. list (, $version) = explode('Version/', $_SERVER['HTTP_USER_AGENT']);
  1038. }
  1039. $version = str_replace('/', '', $version);
  1040. if (strpos($version, '.') === false) {
  1041. $version = number_format(doubleval($version), 1);
  1042. }
  1043. $return = array('name' => $navigator, 'version' => $version);
  1044. return $return;
  1045. }
  1046. /**
  1047. * @return True if user self registration is allowed, false otherwise.
  1048. */
  1049. function api_is_self_registration_allowed()
  1050. {
  1051. return isset($GLOBALS['allowSelfReg']) ? $GLOBALS['allowSelfReg'] : false;
  1052. }
  1053. /**
  1054. * This function returns the id of the user which is stored in the $_user array.
  1055. *
  1056. * example: The function can be used to check if a user is logged in
  1057. * if (api_get_user_id())
  1058. * @return integer the id of the current user, 0 if is empty
  1059. */
  1060. function api_get_user_id()
  1061. {
  1062. $userInfo = Session::read('_user');
  1063. if ($userInfo && isset($userInfo['user_id'])) {
  1064. return $userInfo['user_id'];
  1065. }
  1066. return 0;
  1067. }
  1068. /**
  1069. * Gets the list of courses a specific user is subscribed to
  1070. * @param int User ID
  1071. * @param boolean $fetch_session Whether to get session courses or not - NOT YET IMPLEMENTED
  1072. * @return array Array of courses in the form [0]=>('code'=>xxx,'db'=>xxx,'dir'=>xxx,'status'=>d)
  1073. */
  1074. function api_get_user_courses($userid, $fetch_session = true)
  1075. {
  1076. // Get out if not integer
  1077. if ($userid != strval(intval($userid))) {
  1078. return array();
  1079. }
  1080. $t_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1081. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1082. $sql = "SELECT cc.id as real_id, cc.code code, cc.directory dir, cu.status status
  1083. FROM $t_course cc,
  1084. $t_course_user cu
  1085. WHERE
  1086. cc.id = cu.c_id AND
  1087. cu.user_id = '".$userid."' AND
  1088. cu.relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  1089. $result = Database::query($sql);
  1090. if ($result === false) {
  1091. return array();
  1092. }
  1093. $courses = array();
  1094. while ($row = Database::fetch_array($result)) {
  1095. // we only need the database name of the course
  1096. $courses[] = $row;
  1097. }
  1098. return $courses;
  1099. }
  1100. /**
  1101. * Formats user information into a standard array
  1102. * This function should be only used inside api_get_user_info()
  1103. *
  1104. * @param array Non-standard user array
  1105. * @param bool $add_password
  1106. *
  1107. * @return array Standard user array
  1108. */
  1109. function _api_format_user($user, $add_password = false)
  1110. {
  1111. $result = array();
  1112. $firstname = null;
  1113. $lastname = null;
  1114. if (isset($user['firstname']) && isset($user['lastname'])) {
  1115. $firstname = $user['firstname'];
  1116. $lastname = $user['lastname'];
  1117. } elseif (isset($user['firstName']) && isset($user['lastName'])) {
  1118. $firstname = isset($user['firstName']) ? $user['firstName'] : null;
  1119. $lastname = isset($user['lastName']) ? $user['lastName'] : null;
  1120. }
  1121. $result['complete_name'] = api_get_person_name($firstname, $lastname);
  1122. $result['complete_name_with_username'] = $result['complete_name'];
  1123. if (!empty($user['username'])) {
  1124. $result['complete_name_with_username'] = $result['complete_name'].' ('.$user['username'].')';
  1125. }
  1126. $result['firstname'] = $firstname;
  1127. $result['lastname'] = $lastname;
  1128. // Kept for historical reasons
  1129. $result['firstName'] = $firstname;
  1130. $result['lastName'] = $lastname;
  1131. $attributes = array(
  1132. 'phone',
  1133. 'address',
  1134. 'picture_uri',
  1135. 'official_code',
  1136. 'status',
  1137. 'active',
  1138. 'auth_source',
  1139. 'username',
  1140. 'theme',
  1141. 'language',
  1142. 'creator_id',
  1143. 'registration_date',
  1144. 'hr_dept_id',
  1145. 'expiration_date',
  1146. 'last_login'
  1147. );
  1148. if (api_get_setting('extended_profile') === 'true') {
  1149. $attributes[] = 'competences';
  1150. $attributes[] = 'diplomas';
  1151. $attributes[] = 'teach';
  1152. $attributes[] = 'openarea';
  1153. }
  1154. foreach ($attributes as $attribute) {
  1155. $result[$attribute] = isset($user[$attribute]) ? $user[$attribute] : null;
  1156. }
  1157. if (isset($user['email'])) {
  1158. $result['mail'] = isset($user['email']) ? $user['email'] : null;
  1159. $result['email'] = isset($user['email'])? $user['email'] : null;
  1160. } else {
  1161. $result['mail'] = isset($user['mail']) ? $user['mail'] : null;
  1162. $result['email'] = isset($user['mail'])? $user['mail'] : null;
  1163. }
  1164. $user_id = intval($user['user_id']);
  1165. // Maintain the user_id index for backwards compatibility
  1166. $result['user_id'] = $result['id'] = $user_id;
  1167. $result['last_login'] = $user['last_login'];
  1168. // Getting user avatar.
  1169. $originalFile = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_ORIGINAL, null, $result);
  1170. $smallFile = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_SMALL, null, $result);
  1171. $mediumFile = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_MEDIUM, null, $result);
  1172. $result['avatar'] = $originalFile;
  1173. $avatarString = explode('?', $originalFile);
  1174. $result['avatar_no_query'] = reset($avatarString);
  1175. $result['avatar_small'] = $smallFile;
  1176. $result['avatar_medium'] = $mediumFile;
  1177. if (isset($user['user_is_online'])) {
  1178. $result['user_is_online'] = $user['user_is_online'] == true ? 1 : 0;
  1179. }
  1180. if (isset($user['user_is_online_in_chat'])) {
  1181. $result['user_is_online_in_chat'] = intval($user['user_is_online_in_chat']);
  1182. }
  1183. if ($add_password) {
  1184. $result['password'] = $user['password'];
  1185. }
  1186. if (isset($result['profile_completed'])) {
  1187. $result['profile_completed'] = $user['profile_completed'];
  1188. }
  1189. $result['profile_url'] = api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user_id;
  1190. if (isset($user['extra'])) {
  1191. $result['extra'] = $user['extra'];
  1192. }
  1193. return $result;
  1194. }
  1195. /**
  1196. * Finds all the information about a user.
  1197. * If no parameter is passed you find all the information about the current user.
  1198. * @param int $user_id
  1199. * @param bool $checkIfUserOnline
  1200. * @param bool $showPassword
  1201. * @param bool $loadExtraData
  1202. * @param bool $loadOnlyVisibleExtraData Get the user extra fields that are visible
  1203. * @return array $user_info user_id, lastname, firstname, username, email, etc
  1204. * @author Patrick Cool <patrick.cool@UGent.be>
  1205. * @author Julio Montoya
  1206. * @version 21 September 2004
  1207. */
  1208. function api_get_user_info(
  1209. $user_id = 0,
  1210. $checkIfUserOnline = false,
  1211. $showPassword = false,
  1212. $loadExtraData = false,
  1213. $loadOnlyVisibleExtraData = false
  1214. ) {
  1215. if (empty($user_id)) {
  1216. $userFromSession = Session::read('_user');
  1217. if (isset($userFromSession)) {
  1218. return _api_format_user($userFromSession);
  1219. }
  1220. // @todo trigger an exception here
  1221. return false;
  1222. }
  1223. $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)."
  1224. WHERE id='".intval($user_id)."'";
  1225. $result = Database::query($sql);
  1226. if (Database::num_rows($result) > 0) {
  1227. $result_array = Database::fetch_array($result);
  1228. if ($checkIfUserOnline) {
  1229. $use_status_in_platform = UserManager::user_is_online($user_id);
  1230. $result_array['user_is_online'] = $use_status_in_platform;
  1231. $user_online_in_chat = 0;
  1232. if ($use_status_in_platform) {
  1233. $user_status = UserManager::get_extra_user_data_by_field(
  1234. $user_id,
  1235. 'user_chat_status',
  1236. false,
  1237. true
  1238. );
  1239. if (intval($user_status['user_chat_status']) == 1) {
  1240. $user_online_in_chat = 1;
  1241. }
  1242. }
  1243. $result_array['user_is_online_in_chat'] = $user_online_in_chat;
  1244. }
  1245. if ($loadExtraData) {
  1246. $fieldValue = new ExtraFieldValue('user');
  1247. $result_array['extra'] = $fieldValue->getAllValuesForAnItem(
  1248. $user_id,
  1249. $loadOnlyVisibleExtraData
  1250. );
  1251. }
  1252. $user = _api_format_user($result_array, $showPassword);
  1253. return $user;
  1254. }
  1255. return false;
  1256. }
  1257. /**
  1258. * @param int $userId
  1259. * @return User
  1260. */
  1261. function api_get_user_entity($userId)
  1262. {
  1263. return UserManager::getManager()->find($userId);
  1264. }
  1265. /**
  1266. * @param int $courseId
  1267. *
  1268. * @return Course
  1269. */
  1270. function api_get_user_course_entity($courseId = null)
  1271. {
  1272. if (empty($courseId)) {
  1273. $courseId = api_get_course_int_id();
  1274. }
  1275. return CourseManager::getManager()->find($courseId);
  1276. }
  1277. /**
  1278. * Finds all the information about a user from username instead of user id
  1279. * @param string $username
  1280. * @return array $user_info array user_id, lastname, firstname, username, email
  1281. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  1282. */
  1283. function api_get_user_info_from_username($username = '')
  1284. {
  1285. if (empty($username)) {
  1286. return false;
  1287. }
  1288. $username = trim($username);
  1289. $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)."
  1290. WHERE username='".Database::escape_string($username)."'";
  1291. $result = Database::query($sql);
  1292. if (Database::num_rows($result) > 0) {
  1293. $result_array = Database::fetch_array($result);
  1294. return _api_format_user($result_array);
  1295. }
  1296. return false;
  1297. }
  1298. /**
  1299. * Get first user with an email
  1300. * @param string $email
  1301. * @return array|bool
  1302. */
  1303. function api_get_user_info_from_email($email = '')
  1304. {
  1305. if (empty($email)) {
  1306. return false;
  1307. }
  1308. $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)."
  1309. WHERE email ='".Database::escape_string($email)."' LIMIT 1";
  1310. $result = Database::query($sql);
  1311. if (Database::num_rows($result) > 0) {
  1312. $result_array = Database::fetch_array($result);
  1313. return _api_format_user($result_array);
  1314. }
  1315. return false;
  1316. }
  1317. /**
  1318. * @return string
  1319. */
  1320. function api_get_course_id()
  1321. {
  1322. return Session::read('_cid');
  1323. }
  1324. /**
  1325. * Returns the current course id
  1326. * @return int
  1327. */
  1328. function api_get_real_course_id()
  1329. {
  1330. return api_get_course_int_id();
  1331. }
  1332. /**
  1333. * Returns the current course id (integer)
  1334. * @param string $code Optional course code
  1335. * @return int
  1336. */
  1337. function api_get_course_int_id($code = null)
  1338. {
  1339. if (!empty($code)) {
  1340. $code = Database::escape_string($code);
  1341. $row = Database::select(
  1342. 'id',
  1343. Database::get_main_table(TABLE_MAIN_COURSE),
  1344. array('where'=> array('code = ?' => array($code))),
  1345. 'first'
  1346. );
  1347. if (is_array($row) && isset($row['id'])) {
  1348. return $row['id'];
  1349. } else {
  1350. return false;
  1351. }
  1352. }
  1353. return Session::read('_real_cid', 0);
  1354. }
  1355. /**
  1356. * Returns the current course directory
  1357. *
  1358. * This function relies on api_get_course_info()
  1359. * @param string The course code - optional (takes it from session if not given)
  1360. * @return string The directory where the course is located inside the Chamilo "courses" directory
  1361. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  1362. */
  1363. function api_get_course_path($course_code = null)
  1364. {
  1365. $info = !empty($course_code) ? api_get_course_info($course_code) : api_get_course_info();
  1366. return $info['path'];
  1367. }
  1368. /**
  1369. * Gets a course setting from the current course_setting table. Try always using integer values.
  1370. * @param string The name of the setting we want from the table
  1371. * @param string Optional: course code
  1372. * @param string $setting_name
  1373. * @return mixed The value of that setting in that table. Return -1 if not found.
  1374. */
  1375. function api_get_course_setting($setting_name, $course_code = null)
  1376. {
  1377. $course_info = api_get_course_info($course_code);
  1378. $table = Database::get_course_table(TABLE_COURSE_SETTING);
  1379. $setting_name = Database::escape_string($setting_name);
  1380. if (!empty($course_info['real_id']) && !empty($setting_name)) {
  1381. $sql = "SELECT value FROM $table
  1382. WHERE c_id = {$course_info['real_id']} AND variable = '$setting_name'";
  1383. $res = Database::query($sql);
  1384. if (Database::num_rows($res) > 0) {
  1385. $row = Database::fetch_array($res);
  1386. return $row['value'];
  1387. }
  1388. }
  1389. return -1;
  1390. }
  1391. /**
  1392. * Gets an anonymous user ID
  1393. *
  1394. * For some tools that need tracking, like the learnpath tool, it is necessary
  1395. * to have a usable user-id to enable some kind of tracking, even if not
  1396. * perfect. An anonymous ID is taken from the users table by looking for a
  1397. * status of "6" (anonymous).
  1398. * @return int User ID of the anonymous user, or O if no anonymous user found
  1399. */
  1400. function api_get_anonymous_id()
  1401. {
  1402. $table = Database::get_main_table(TABLE_MAIN_USER);
  1403. $sql = "SELECT user_id FROM $table WHERE status = ".ANONYMOUS;
  1404. $res = Database::query($sql);
  1405. if (Database::num_rows($res) > 0) {
  1406. $row = Database::fetch_array($res);
  1407. return $row['user_id'];
  1408. }
  1409. // No anonymous user was found.
  1410. return 0;
  1411. }
  1412. /**
  1413. * @param string $courseCode
  1414. * @param int $sessionId
  1415. * @param int $groupId
  1416. * @return string
  1417. */
  1418. function api_get_cidreq_params($courseCode, $sessionId = 0, $groupId = 0)
  1419. {
  1420. $courseCode = !empty($courseCode) ? htmlspecialchars($courseCode) : '';
  1421. $sessionId = !empty($sessionId) ? (int) $sessionId : 0;
  1422. $groupId = !empty($groupId) ? (int) $groupId : 0;
  1423. $url = 'cidReq='.$courseCode;
  1424. $url .= '&id_session='.$sessionId;
  1425. $url .= '&gidReq='.$groupId;
  1426. return $url;
  1427. }
  1428. /**
  1429. * Returns the current course url part including session, group, and gradebook params
  1430. *
  1431. * @param bool $addSessionId
  1432. * @param bool $addGroupId
  1433. * @param string $origin
  1434. *
  1435. * @return string Course & session references to add to a URL
  1436. *
  1437. */
  1438. function api_get_cidreq($addSessionId = true, $addGroupId = true, $origin = '')
  1439. {
  1440. $courseCode = api_get_course_id();
  1441. $url = empty($courseCode) ? '' : 'cidReq='.htmlspecialchars($courseCode);
  1442. $origin = empty($origin) ? api_get_origin() : Security::remove_XSS($origin);
  1443. if ($addSessionId) {
  1444. if (!empty($url)) {
  1445. $url .= api_get_session_id() == 0 ? '&id_session=0' : '&id_session='.api_get_session_id();
  1446. }
  1447. }
  1448. if ($addGroupId) {
  1449. if (!empty($url)) {
  1450. $url .= api_get_group_id() == 0 ? '&gidReq=0' : '&gidReq='.api_get_group_id();
  1451. }
  1452. }
  1453. if (!empty($url)) {
  1454. $url .= '&gradebook='.intval(api_is_in_gradebook());
  1455. $url .= '&origin='.$origin;
  1456. }
  1457. return $url;
  1458. }
  1459. /**
  1460. * get gradebook in session
  1461. */
  1462. function api_is_in_gradebook()
  1463. {
  1464. return Session::read('in_gradebook', false);
  1465. }
  1466. /**
  1467. * set gradebook session
  1468. */
  1469. function api_set_in_gradebook()
  1470. {
  1471. Session::write('in_gradebook', true);
  1472. }
  1473. /**
  1474. * remove gradebook session
  1475. */
  1476. function api_remove_in_gradebook()
  1477. {
  1478. Session::erase('in_gradebook');
  1479. }
  1480. /**
  1481. * Returns the current course info array see api_format_course_array()
  1482. * If the course_code is given, the returned array gives info about that
  1483. * particular course, if none given it gets the course info from the session.
  1484. *
  1485. * @param string $course_code
  1486. * @param bool $strict
  1487. *
  1488. * @return array
  1489. */
  1490. function api_get_course_info($course_code = null, $strict = false)
  1491. {
  1492. if (!empty($course_code)) {
  1493. $course_code = Database::escape_string($course_code);
  1494. $courseId = api_get_course_int_id($course_code);
  1495. if (empty($courseId)) {
  1496. return array();
  1497. }
  1498. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1499. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  1500. $sql = "SELECT
  1501. course.*,
  1502. course_category.code faCode,
  1503. course_category.name faName
  1504. FROM $course_table
  1505. LEFT JOIN $course_cat_table
  1506. ON course.category_code = course_category.code
  1507. WHERE course.id = $courseId";
  1508. $result = Database::query($sql);
  1509. $courseInfo = array();
  1510. if (Database::num_rows($result) > 0) {
  1511. $data = Database::fetch_array($result);
  1512. $data['teacher_list'] = CourseManager::getTeacherListFromCourse(
  1513. $courseId
  1514. );
  1515. $data['teacher_list_formatted'] = CourseManager::formatUserListToString(
  1516. $data['teacher_list'],
  1517. null,
  1518. true
  1519. );
  1520. $courseInfo = api_format_course_array($data);
  1521. }
  1522. return $courseInfo;
  1523. }
  1524. $_course = Session::read('_course');
  1525. if ($_course == '-1') {
  1526. $_course = array();
  1527. }
  1528. return $_course;
  1529. }
  1530. /**
  1531. * Returns the current course info array.
  1532. * Now if the course_code is given, the returned array gives info about that
  1533. * particular course, not specially the current one.
  1534. * @param int $id Numeric ID of the course
  1535. * @return array The course info as an array formatted by api_format_course_array, including category.name
  1536. */
  1537. function api_get_course_info_by_id($id = null)
  1538. {
  1539. if (!empty($id)) {
  1540. $id = intval($id);
  1541. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1542. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  1543. $sql = "SELECT
  1544. course.*,
  1545. course_category.code faCode,
  1546. course_category.name faName
  1547. FROM $course_table
  1548. LEFT JOIN $course_cat_table
  1549. ON course.category_code = course_category.code
  1550. WHERE course.id = $id";
  1551. $result = Database::query($sql);
  1552. $_course = array();
  1553. if (Database::num_rows($result) > 0) {
  1554. $course_data = Database::fetch_array($result);
  1555. $_course = api_format_course_array($course_data);
  1556. }
  1557. return $_course;
  1558. }
  1559. global $_course;
  1560. if ($_course == '-1') $_course = array();
  1561. return $_course;
  1562. }
  1563. /**
  1564. * Reformat the course array (output by api_get_course_info()) in order, mostly,
  1565. * to switch from 'code' to 'id' in the array. This is a legacy feature and is
  1566. * now possibly causing massive confusion as a new "id" field has been added to
  1567. * the course table in 1.9.0.
  1568. * @param $course_data
  1569. * @return array
  1570. * @todo eradicate the false "id"=code field of the $_course array and use the int id
  1571. */
  1572. function api_format_course_array($course_data)
  1573. {
  1574. if (empty($course_data)) {
  1575. return array();
  1576. }
  1577. $_course = array();
  1578. $_course['id'] = $course_data['code'];
  1579. $_course['real_id'] = $course_data['id'];
  1580. // Added
  1581. $_course['code'] = $course_data['code'];
  1582. $_course['name'] = $course_data['title'];
  1583. $_course['title'] = $course_data['title'];
  1584. $_course['official_code'] = $course_data['visual_code'];
  1585. $_course['visual_code'] = $course_data['visual_code'];
  1586. $_course['sysCode'] = $course_data['code'];
  1587. $_course['path'] = $course_data['directory']; // Use as key in path.
  1588. $_course['directory'] = $course_data['directory'];
  1589. $_course['creation_date'] = $course_data['creation_date'];
  1590. $_course['titular'] = $course_data['tutor_name'];
  1591. $_course['language'] = $course_data['course_language'];
  1592. $_course['extLink']['url'] = $course_data['department_url'];
  1593. $_course['extLink']['name'] = $course_data['department_name'];
  1594. $_course['categoryCode'] = $course_data['faCode'];
  1595. $_course['categoryName'] = $course_data['faName'];
  1596. $_course['visibility'] = $course_data['visibility'];
  1597. $_course['subscribe_allowed'] = $course_data['subscribe'];
  1598. $_course['subscribe'] = $course_data['subscribe'];
  1599. $_course['unsubscribe'] = $course_data['unsubscribe'];
  1600. $_course['course_language'] = $course_data['course_language'];
  1601. $_course['activate_legal'] = isset($course_data['activate_legal']) ? $course_data['activate_legal'] : false;
  1602. $_course['legal'] = $course_data['legal'];
  1603. $_course['show_score'] = $course_data['show_score']; //used in the work tool
  1604. $_course['department_name'] = $course_data['department_name'];
  1605. $_course['department_url'] = $course_data['department_url'];
  1606. // Course password
  1607. $_course['registration_code'] = !empty($course_data['registration_code']) ? sha1($course_data['registration_code']) : null;
  1608. $_course['disk_quota'] = $course_data['disk_quota'];
  1609. $_course['course_public_url'] = api_get_path(WEB_COURSE_PATH).$course_data['directory'].'/index.php';
  1610. if (array_key_exists('add_teachers_to_sessions_courses', $course_data)) {
  1611. $_course['add_teachers_to_sessions_courses'] = $course_data['add_teachers_to_sessions_courses'];
  1612. }
  1613. if (file_exists(api_get_path(SYS_COURSE_PATH).$course_data['directory'].'/course-pic85x85.png')) {
  1614. $url_image = api_get_path(WEB_COURSE_PATH).$course_data['directory'].'/course-pic85x85.png';
  1615. } else {
  1616. $url_image = Display::return_icon('course.png', null, null, ICON_SIZE_BIG, null, true, false);
  1617. }
  1618. $_course['course_image'] = $url_image;
  1619. if (file_exists(api_get_path(SYS_COURSE_PATH).$course_data['directory'].'/course-pic.png')) {
  1620. $url_image = api_get_path(WEB_COURSE_PATH).$course_data['directory'].'/course-pic.png';
  1621. } else {
  1622. $url_image = Display::returnIconPath('session_default.png');
  1623. }
  1624. $_course['course_image_large'] = $url_image;
  1625. $_course['extra_fields'] = isset($course_data['extra_fields']) ? $course_data['extra_fields'] : array();
  1626. $_course['settings'] = isset($course_data['settings']) ? $course_data['settings'] : array();
  1627. $_course['teacher_list'] = isset($course_data['teacher_list']) ? $course_data['teacher_list'] : array();
  1628. $_course['teacher_list_formatted'] = isset($course_data['teacher_list_formatted']) ? $course_data['teacher_list_formatted'] : array();
  1629. return $_course;
  1630. }
  1631. /**
  1632. * Add a parameter to the existing URL. If this parameter already exists,
  1633. * just replace it with the new value
  1634. * @param string The URL
  1635. * @param string param=value string
  1636. * @param boolean Whether to filter XSS or not
  1637. * @return string The URL with the added parameter
  1638. */
  1639. function api_add_url_param($url, $param, $filter_xss = true) {
  1640. if (empty($param)) {
  1641. return $url;
  1642. }
  1643. if (strpos($url, '?') !== false) {
  1644. if ($param[0] != '&') {
  1645. $param = '&'.$param;
  1646. }
  1647. list (, $query_string) = explode('?', $url);
  1648. $param_list1 = explode('&', $param);
  1649. $param_list2 = explode('&', $query_string);
  1650. $param_list1_keys = $param_list1_vals = array();
  1651. foreach ($param_list1 as $key => $enreg) {
  1652. list ($param_list1_keys[$key], $param_list1_vals[$key]) = explode('=', $enreg);
  1653. }
  1654. $param_list1 = array ('keys' => $param_list1_keys, 'vals' => $param_list1_vals);
  1655. foreach ($param_list2 as $enreg) {
  1656. $enreg = explode('=', $enreg);
  1657. $key = array_search($enreg[0], $param_list1['keys']);
  1658. if (!is_null($key) && !is_bool($key)) {
  1659. $url = str_replace($enreg[0].'='.$enreg[1], $enreg[0].'='.$param_list1['vals'][$key], $url);
  1660. $param = str_replace('&'.$enreg[0].'='.$param_list1['vals'][$key], '', $param);
  1661. }
  1662. }
  1663. $url .= $param;
  1664. } else {
  1665. $url = $url.'?'.$param;
  1666. }
  1667. if ($filter_xss === true) {
  1668. $url = Security::remove_XSS(urldecode($url));
  1669. }
  1670. return $url;
  1671. }
  1672. /**
  1673. * Returns a difficult to guess password.
  1674. * @param int $length, the length of the password
  1675. * @return string the generated password
  1676. */
  1677. function api_generate_password($length = 8)
  1678. {
  1679. $characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
  1680. $numbers = '23456789';
  1681. if ($length < 2) {
  1682. $length = 2;
  1683. }
  1684. $password = '';
  1685. for ($i = 0; $i < $length; $i ++) {
  1686. $password .= $characters[rand() % strlen($characters)];
  1687. }
  1688. // At least 2 digits
  1689. for ($i = 0; $i < 2; $i ++) {
  1690. $password .= $numbers[rand() % strlen($numbers)];
  1691. }
  1692. return $password;
  1693. }
  1694. /**
  1695. * Checks a password to see wether it is OK to use.
  1696. * @param string $password
  1697. * @return boolean if the password is acceptable, false otherwise
  1698. * Notes about what a password "OK to use" is:
  1699. * 1. The password should be at least 5 characters long.
  1700. * 2. Only English letters (uppercase or lowercase, it doesn't matter) and digits are allowed.
  1701. * 3. The password should contain at least 3 letters.
  1702. * 4. It should contain at least 2 digits.
  1703. * 5. It should not contain 3 or more consequent (according to ASCII table) characters.
  1704. */
  1705. function api_check_password($password) {
  1706. $password_length = api_strlen($password);
  1707. if ($password_length < 5) {
  1708. return false;
  1709. }
  1710. $password = api_strtolower($password);
  1711. $letters = 0;
  1712. $digits = 0;
  1713. $consequent_characters = 0;
  1714. $previous_character_code = 0;
  1715. for ($i = 0; $i < $password_length; $i ++) {
  1716. $current_character_code = api_ord(api_substr($password, $i, 1));
  1717. if ($i && abs($current_character_code - $previous_character_code) <= 1) {
  1718. $consequent_characters ++;
  1719. if ($consequent_characters == 3) {
  1720. return false;
  1721. }
  1722. } else {
  1723. $consequent_characters = 1;
  1724. }
  1725. if ($current_character_code >= 97 && $current_character_code <= 122) {
  1726. $letters ++;
  1727. } elseif ($current_character_code >= 48 && $current_character_code <= 57) {
  1728. $digits ++;
  1729. } else {
  1730. return false;
  1731. }
  1732. $previous_character_code = $current_character_code;
  1733. }
  1734. return ($letters >= 3 && $digits >= 2);
  1735. }
  1736. /**
  1737. * Clears the user ID from the session if it was the anonymous user. Generally
  1738. * used on out-of-tools pages to remove a user ID that could otherwise be used
  1739. * in the wrong context.
  1740. * This function is to be used in conjunction with the api_set_anonymous()
  1741. * function to simulate the user existence in case of an anonymous visit.
  1742. * @param bool database check switch - passed to api_is_anonymous()
  1743. * @return bool true if succesfully unregistered, false if not anonymous.
  1744. */
  1745. function api_clear_anonymous($db_check = false)
  1746. {
  1747. global $_user;
  1748. if (api_is_anonymous($_user['user_id'], $db_check)) {
  1749. unset($_user['user_id']);
  1750. Session::erase('_uid');
  1751. return true;
  1752. }
  1753. return false;
  1754. }
  1755. /**
  1756. * Returns the status string corresponding to the status code
  1757. * @author Noel Dieschburg
  1758. * @param the int status code
  1759. */
  1760. function get_status_from_code($status_code)
  1761. {
  1762. switch ($status_code) {
  1763. case STUDENT:
  1764. return get_lang('Student', '');
  1765. case TEACHER:
  1766. return get_lang('Teacher', '');
  1767. case COURSEMANAGER:
  1768. return get_lang('Manager', '');
  1769. case SESSIONADMIN:
  1770. return get_lang('SessionsAdmin', '');
  1771. case DRH:
  1772. return get_lang('Drh', '');
  1773. }
  1774. }
  1775. /**
  1776. * Sets the current user as anonymous if it hasn't been identified yet. This
  1777. * function should be used inside a tool only. The function api_clear_anonymous()
  1778. * acts in the opposite direction by clearing the anonymous user's data every
  1779. * time we get on a course homepage or on a neutral page (index, admin, my space)
  1780. * @return bool true if set user as anonymous, false if user was already logged in or anonymous id could not be found
  1781. */
  1782. function api_set_anonymous() {
  1783. global $_user;
  1784. if (!empty($_user['user_id'])) {
  1785. return false;
  1786. }
  1787. $user_id = api_get_anonymous_id();
  1788. if ($user_id == 0) {
  1789. return false;
  1790. }
  1791. Session::erase('_user');
  1792. $_user['user_id'] = $user_id;
  1793. $_user['is_anonymous'] = true;
  1794. $GLOBALS['_user'] = $_user;
  1795. Session::write('_user', $_user);
  1796. return true;
  1797. }
  1798. /**
  1799. * Gets the current Chamilo (not PHP/cookie) session ID
  1800. * @return int O if no active session, the session ID otherwise
  1801. */
  1802. function api_get_session_id()
  1803. {
  1804. return Session::read('id_session', 0);
  1805. }
  1806. /**
  1807. * Gets the current Chamilo (not social network) group ID
  1808. * @return int O if no active session, the session ID otherwise
  1809. */
  1810. function api_get_group_id()
  1811. {
  1812. return Session::read('_gid', 0);
  1813. }
  1814. /**
  1815. * Gets the current or given session name
  1816. * @param int Session ID (optional)
  1817. * @return string The session name, or null if unfound
  1818. */
  1819. function api_get_session_name($session_id = 0)
  1820. {
  1821. if (empty($session_id)) {
  1822. $session_id = api_get_session_id();
  1823. if (empty($session_id)) {
  1824. return null;
  1825. }
  1826. }
  1827. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  1828. $s = "SELECT name FROM $t WHERE id = ".(int)$session_id;
  1829. $r = Database::query($s);
  1830. $c = Database::num_rows($r);
  1831. if ($c > 0) {
  1832. //technically, there can be only one, but anyway we take the first
  1833. $rec = Database::fetch_array($r);
  1834. return $rec['name'];
  1835. }
  1836. return null;
  1837. }
  1838. /**
  1839. * Gets the session info by id
  1840. * @param int Session ID
  1841. * @return array information of the session
  1842. */
  1843. function api_get_session_info($session_id)
  1844. {
  1845. return SessionManager::fetch($session_id);
  1846. }
  1847. /**
  1848. * Gets the session visibility by session id
  1849. * @param int $session_id
  1850. * @param int $courseId
  1851. * @param bool $ignore_visibility_for_admins
  1852. * @return int
  1853. * 0 = session still available,
  1854. * SESSION_VISIBLE_READ_ONLY = 1,
  1855. * SESSION_VISIBLE = 2,
  1856. * SESSION_INVISIBLE = 3
  1857. */
  1858. function api_get_session_visibility(
  1859. $session_id,
  1860. $courseId = null,
  1861. $ignore_visibility_for_admins = true
  1862. ) {
  1863. // Means that the session is still available.
  1864. $visibility = 0;
  1865. if (api_is_platform_admin()) {
  1866. if ($ignore_visibility_for_admins) {
  1867. return SESSION_AVAILABLE;
  1868. }
  1869. }
  1870. $now = time();
  1871. if (!empty($session_id)) {
  1872. $session_id = intval($session_id);
  1873. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1874. $sql = "SELECT * FROM $tbl_session
  1875. WHERE id = $session_id ";
  1876. $result = Database::query($sql);
  1877. if (Database::num_rows($result) > 0) {
  1878. $row = Database::fetch_array($result, 'ASSOC');
  1879. $visibility = $original_visibility = $row['visibility'];
  1880. // I don't care the session visibility.
  1881. if (empty($row['access_start_date']) && empty($row['access_end_date'])) {
  1882. // Session duration per student.
  1883. if (isset($row['duration']) && !empty($row['duration'])) {
  1884. $duration = $row['duration'] * 24 * 60 * 60;
  1885. $courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser(
  1886. $session_id,
  1887. api_get_user_id()
  1888. );
  1889. // If there is a session duration but there is no previous
  1890. // access by the user, then the session is still available
  1891. if (count($courseAccess) == 0) {
  1892. return SESSION_AVAILABLE;
  1893. }
  1894. $currentTime = time();
  1895. $firstAccess = 0;
  1896. if (isset($courseAccess['login_course_date'])) {
  1897. $firstAccess = api_strtotime(
  1898. $courseAccess['login_course_date'],
  1899. 'UTC'
  1900. );
  1901. }
  1902. $userDurationData = SessionManager::getUserSession(
  1903. api_get_user_id(),
  1904. $session_id
  1905. );
  1906. $userDuration = 0;
  1907. if (isset($userDurationData['duration'])) {
  1908. $userDuration = intval($userDurationData['duration']) * 24 * 60 * 60;
  1909. }
  1910. $totalDuration = $firstAccess + $duration + $userDuration;
  1911. if ($totalDuration > $currentTime) {
  1912. return SESSION_AVAILABLE;
  1913. } else {
  1914. return SESSION_INVISIBLE;
  1915. }
  1916. }
  1917. return SESSION_AVAILABLE;
  1918. } else {
  1919. // If start date was set.
  1920. if (!empty($row['access_start_date'])) {
  1921. if ($now > api_strtotime($row['access_start_date'], 'UTC')) {
  1922. $visibility = SESSION_AVAILABLE;
  1923. } else {
  1924. $visibility = SESSION_INVISIBLE;
  1925. }
  1926. }
  1927. // If the end date was set.
  1928. if (!empty($row['access_end_date'])) {
  1929. // Only if date_start said that it was ok
  1930. if ($visibility === SESSION_AVAILABLE) {
  1931. if ($now < api_strtotime($row['access_end_date'], 'UTC')) {
  1932. // Date still available
  1933. $visibility = SESSION_AVAILABLE;
  1934. } else {
  1935. // Session ends
  1936. $visibility = $row['visibility'];
  1937. }
  1938. }
  1939. }
  1940. }
  1941. /* If I'm a coach the visibility can change in my favor depending in
  1942. the coach dates */
  1943. $isCoach = api_is_coach($session_id, $courseId);
  1944. if ($isCoach) {
  1945. // Test end date.
  1946. if (!empty($row['coach_access_end_date'])) {
  1947. $endDateCoach = api_strtotime($row['coach_access_end_date'], 'UTC');
  1948. if ($endDateCoach >= $now) {
  1949. $visibility = SESSION_AVAILABLE;
  1950. } else {
  1951. $visibility = SESSION_INVISIBLE;
  1952. }
  1953. }
  1954. // Test start date.
  1955. if (!empty($row['coach_access_start_date'])) {
  1956. $start = api_strtotime($row['coach_access_start_date'], 'UTC');
  1957. if ($start < $now) {
  1958. $visibility = SESSION_AVAILABLE;
  1959. } else {
  1960. $visibility = SESSION_INVISIBLE;
  1961. }
  1962. }
  1963. }
  1964. } else {
  1965. $visibility = SESSION_INVISIBLE;
  1966. }
  1967. }
  1968. return $visibility;
  1969. }
  1970. /**
  1971. * This function returns a (star) session icon if the session is not null and
  1972. * the user is not a student
  1973. * @param int $session_id
  1974. * @param int $status_id User status id - if 5 (student), will return empty
  1975. * @return string Session icon
  1976. */
  1977. function api_get_session_image($session_id, $status_id)
  1978. {
  1979. $session_id = (int)$session_id;
  1980. $session_img = '';
  1981. if ((int)$status_id != 5) { //check whether is not a student
  1982. if ($session_id > 0) {
  1983. $session_img = "&nbsp;&nbsp;".Display::return_icon(
  1984. 'star.png',
  1985. get_lang('SessionSpecificResource'),
  1986. array('align' => 'absmiddle'),
  1987. ICON_SIZE_SMALL
  1988. );
  1989. }
  1990. }
  1991. return $session_img;
  1992. }
  1993. /**
  1994. * This function add an additional condition according to the session of the course
  1995. * @param int $session_id session id
  1996. * @param bool $and optional, true if more than one condition false if the only condition in the query
  1997. * @param bool $with_base_content optional, true to accept content with session=0 as well, false for strict session condition
  1998. * @return string condition of the session
  1999. */
  2000. function api_get_session_condition(
  2001. $session_id,
  2002. $and = true,
  2003. $with_base_content = false,
  2004. $session_field = "session_id"
  2005. ) {
  2006. $session_id = intval($session_id);
  2007. if (empty($session_field)) {
  2008. $session_field = "session_id";
  2009. }
  2010. // Condition to show resources by session
  2011. $condition_add = $and ? " AND " : " WHERE ";
  2012. if ($with_base_content) {
  2013. $condition_session = $condition_add." ( $session_field = $session_id OR $session_field = 0 OR $session_field IS NULL) ";
  2014. } else {
  2015. if (empty($session_id)) {
  2016. $condition_session = $condition_add." ($session_field = $session_id OR $session_field IS NULL)";
  2017. } else {
  2018. $condition_session = $condition_add." $session_field = $session_id ";
  2019. }
  2020. }
  2021. return $condition_session;
  2022. }
  2023. /**
  2024. * This function returns information about coaches from a course in session
  2025. * @param int optional, session id
  2026. * @param int $courseId
  2027. * @return array array containing user_id, lastname, firstname, username
  2028. * @deprecated use CourseManager::get_coaches_from_course
  2029. */
  2030. function api_get_coachs_from_course($session_id = 0, $courseId = '')
  2031. {
  2032. if (!empty($session_id)) {
  2033. $session_id = intval($session_id);
  2034. } else {
  2035. $session_id = api_get_session_id();
  2036. }
  2037. if (!empty($courseId)) {
  2038. $courseId = intval($courseId);
  2039. } else {
  2040. $courseId = api_get_course_int_id();
  2041. }
  2042. $tbl_user = Database:: get_main_table(TABLE_MAIN_USER);
  2043. $tbl_session_course_user = Database:: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2044. $coaches = array();
  2045. $sql = "SELECT
  2046. u.user_id,
  2047. u.lastname,
  2048. u.firstname,
  2049. u.username
  2050. FROM $tbl_user u, $tbl_session_course_user scu
  2051. WHERE
  2052. u.user_id = scu.user_id AND
  2053. scu.session_id = '$session_id' AND
  2054. scu.c_id = '$courseId' AND
  2055. scu.status = 2";
  2056. $rs = Database::query($sql);
  2057. if (Database::num_rows($rs) > 0) {
  2058. while ($row = Database::fetch_array($rs)) {
  2059. $coaches[] = $row;
  2060. }
  2061. return $coaches;
  2062. } else {
  2063. return false;
  2064. }
  2065. }
  2066. /**
  2067. * @param string $variable
  2068. * @param string $option
  2069. * @return bool
  2070. */
  2071. function api_get_setting_in_list($variable, $option)
  2072. {
  2073. $value = api_get_setting($variable);
  2074. return in_array($option, $value);
  2075. }
  2076. /**
  2077. * Returns the value of a setting from the web-adjustable admin config settings.
  2078. *
  2079. * WARNING true/false are stored as string, so when comparing you need to check e.g.
  2080. * if (api_get_setting('course.show_navigation_menu') == 'true') //CORRECT
  2081. * instead of
  2082. * if (api_get_setting('course.show_navigation_menu') == true) //INCORRECT
  2083. * @param string $variable The variable name
  2084. * @return string
  2085. *
  2086. * @author Julio Montoya
  2087. */
  2088. function api_get_setting($variable)
  2089. {
  2090. $variable = trim($variable);
  2091. switch ($variable) {
  2092. case 'header_extra_content':
  2093. $filename = api_get_path(SYS_PATH).api_get_home_path().'header_extra_content.txt';
  2094. if (file_exists($filename)) {
  2095. $value = file_get_contents($filename);
  2096. return $value ;
  2097. } else {
  2098. return '';
  2099. }
  2100. break;
  2101. case 'footer_extra_content':
  2102. $filename = api_get_path(SYS_PATH).api_get_home_path().'footer_extra_content.txt';
  2103. if (file_exists($filename)) {
  2104. $value = file_get_contents($filename);
  2105. return $value ;
  2106. } else {
  2107. return '';
  2108. }
  2109. break;
  2110. case 'server_type':
  2111. $test = ['dev', 'test'];
  2112. $environment = Container::getEnvironment();
  2113. if (in_array($environment, $test)) {
  2114. return 'test';
  2115. }
  2116. return 'prod';
  2117. case 'stylesheets':
  2118. $variable = 'platform.theme';
  2119. break;
  2120. default:
  2121. return Container::getSettingsManager()->getSetting($variable);
  2122. }
  2123. }
  2124. /**
  2125. * @param string $plugin
  2126. * @param string $variable
  2127. * @return string
  2128. */
  2129. function api_get_plugin_setting($plugin, $variable)
  2130. {
  2131. return '';
  2132. $variableName = $plugin.'_'.$variable;
  2133. $result = api_get_setting($variableName);
  2134. if (isset($result[$plugin])) {
  2135. return $result[$plugin];
  2136. }
  2137. return null;
  2138. }
  2139. /**
  2140. * Returns the value of a setting from the web-adjustable admin config settings.
  2141. **/
  2142. function api_get_settings_params($params) {
  2143. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  2144. $result = Database::select('*', $table, array('where' => $params));
  2145. return $result;
  2146. }
  2147. function api_get_settings_params_simple($params) {
  2148. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  2149. $result = Database::select('*', $table, array('where' => $params), 'one');
  2150. return $result;
  2151. }
  2152. /**
  2153. * Returns the value of a setting from the web-adjustable admin config settings.
  2154. **/
  2155. function api_delete_settings_params($params) {
  2156. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  2157. $result = Database::delete($table, $params);
  2158. return $result;
  2159. }
  2160. /**
  2161. * Returns an escaped version of $_SERVER['PHP_SELF'] to avoid XSS injection
  2162. * @return string Escaped version of $_SERVER['PHP_SELF']
  2163. */
  2164. function api_get_self() {
  2165. return htmlentities($_SERVER['PHP_SELF']);
  2166. }
  2167. /**
  2168. * Checks whether current user is a platform administrator
  2169. * @param boolean $allowSessionAdmins Whether session admins should be considered admins or not
  2170. * @param boolean $allowDrh Whether HR directors should be considered admins or not
  2171. * @return boolean True if the user has platform admin rights,
  2172. * false otherwise.
  2173. * @see usermanager::is_admin(user_id) for a user-id specific function
  2174. */
  2175. function api_is_platform_admin($allowSessionAdmins = false, $allowDrh = false)
  2176. {
  2177. $checker = Container::getAuthorizationChecker();
  2178. if ($checker) {
  2179. if ($checker->isGranted('ROLE_ADMIN')) {
  2180. return true;
  2181. }
  2182. if ($allow_sessions_admins) {
  2183. if ($checker->isGranted('ROLE_SESSION_MANAGER')) {
  2184. return true;
  2185. }
  2186. }
  2187. }
  2188. return false;
  2189. $isPlatformAdmin = Session::read('is_platformAdmin');
  2190. if ($isPlatformAdmin) {
  2191. return true;
  2192. }
  2193. $_user = api_get_user_info();
  2194. return
  2195. isset($_user['status']) &&
  2196. (
  2197. ($allow_sessions_admins && $_user['status'] == SESSIONADMIN) ||
  2198. ($allow_drh && $_user['status'] == DRH)
  2199. );
  2200. }
  2201. /**
  2202. * Checks whether the user given as user id is in the admin table.
  2203. * @param int $user_id. If none provided, will use current user
  2204. * @param int $url URL ID. If provided, also check if the user is active on given URL
  2205. * @result bool True if the user is admin, false otherwise
  2206. */
  2207. function api_is_platform_admin_by_id($user_id = null, $url = null)
  2208. {
  2209. $user_id = intval($user_id);
  2210. if (empty($user_id)) {
  2211. $user_id = api_get_user_id();
  2212. }
  2213. $em = Container::getEntityManager();
  2214. $user = $em->getRepository('ChamiloUserBundle:User')->find($user_id);
  2215. $is_admin = $user->hasRole('ROLE_ADMIN');
  2216. if (!$is_admin or !isset($url)) {
  2217. return $is_admin;
  2218. }
  2219. // We get here only if $url is set
  2220. $url = intval($url);
  2221. $url_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  2222. $sql = "SELECT * FROM $url_user_table
  2223. WHERE access_url_id = $url AND user_id = $user_id";
  2224. $res = Database::query($sql);
  2225. $is_on_url = Database::num_rows($res) === 1;
  2226. return $is_on_url;
  2227. }
  2228. /**
  2229. * Returns the user's numeric status ID from the users table
  2230. * @param int $user_id If none provided, will use current user
  2231. * @return int User's status (1 for teacher, 5 for student, etc)
  2232. */
  2233. function api_get_user_status($user_id = null)
  2234. {
  2235. $user_id = intval($user_id);
  2236. if (empty($user_id)) {
  2237. $user_id = api_get_user_id();
  2238. }
  2239. $table = Database::get_main_table(TABLE_MAIN_USER);
  2240. $sql = "SELECT status FROM $table WHERE user_id = $user_id ";
  2241. $result = Database::query($sql);
  2242. $status = null;
  2243. if (Database::num_rows($result)) {
  2244. $row = Database::fetch_array($result);
  2245. $status = $row['status'];
  2246. }
  2247. return $status;
  2248. }
  2249. /**
  2250. * Checks whether current user is allowed to create courses
  2251. * @return boolean True if the user has course creation rights,
  2252. * false otherwise.
  2253. */
  2254. function api_is_allowed_to_create_course()
  2255. {
  2256. if (api_is_platform_admin()) {
  2257. return true;
  2258. }
  2259. // Teachers can only create courses
  2260. if (api_is_teacher()) {
  2261. if (api_get_setting('allow_users_to_create_courses') === 'true') {
  2262. return true;
  2263. } else {
  2264. return false;
  2265. }
  2266. }
  2267. return Session::read('is_allowedCreateCourse');
  2268. }
  2269. /**
  2270. * Checks whether the current user is a course administrator
  2271. * @return boolean True if current user is a course administrator
  2272. */
  2273. function api_is_course_admin()
  2274. {
  2275. if (api_is_platform_admin()) {
  2276. return true;
  2277. }
  2278. return Session::read('is_courseAdmin');
  2279. }
  2280. /**
  2281. * Checks whether the current user is a course coach
  2282. * @return bool True if current user is a course coach
  2283. */
  2284. function api_is_course_coach()
  2285. {
  2286. return Session::read('is_courseCoach');
  2287. }
  2288. /**
  2289. * Checks whether the current user is a course tutor
  2290. * @return bool True if current user is a course tutor
  2291. */
  2292. function api_is_course_tutor()
  2293. {
  2294. return Session::read('is_courseTutor');
  2295. }
  2296. /**
  2297. * @param int $user_id
  2298. * @param int $courseId
  2299. * @param int $session_id
  2300. * @return bool
  2301. */
  2302. function api_is_course_session_coach($user_id, $courseId, $session_id)
  2303. {
  2304. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  2305. $session_rel_course_rel_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2306. $user_id = intval($user_id);
  2307. $session_id = intval($session_id);
  2308. $courseId = intval($courseId);
  2309. $sql = "SELECT DISTINCT session.id
  2310. FROM $session_table
  2311. INNER JOIN $session_rel_course_rel_user_table session_rc_ru
  2312. ON session.id = session_rc_ru.session_id
  2313. WHERE
  2314. session_rc_ru.user_id = '".$user_id."' AND
  2315. session_rc_ru.c_id = '$courseId' AND
  2316. session_rc_ru.status = 2 AND
  2317. session_rc_ru.session_id = '$session_id'";
  2318. $result = Database::query($sql);
  2319. return Database::num_rows($result) > 0;
  2320. }
  2321. /**
  2322. * Checks whether the current user is a course or session coach
  2323. * @param int $session_id
  2324. * @param int $courseId
  2325. * @param bool Check whether we are in student view and, if we are, return false
  2326. * @return boolean True if current user is a course or session coach
  2327. */
  2328. function api_is_coach($session_id = 0, $courseId = null, $check_student_view = true)
  2329. {
  2330. $userId = api_get_user_id();
  2331. if (!empty($session_id)) {
  2332. $session_id = intval($session_id);
  2333. } else {
  2334. $session_id = api_get_session_id();
  2335. }
  2336. $studentView = Session::read('studentview');
  2337. // The student preview was on
  2338. if ($check_student_view && $studentView == "studentview") {
  2339. return false;
  2340. }
  2341. if (!empty($courseId)) {
  2342. $courseId = intval($courseId);
  2343. } else {
  2344. $courseId = api_get_course_int_id();
  2345. }
  2346. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  2347. $session_rel_course_rel_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2348. $sessionIsCoach = null;
  2349. if (!empty($courseId)) {
  2350. $sql = "SELECT DISTINCT s.id, name, access_start_date, access_end_date
  2351. FROM $session_table s
  2352. INNER JOIN $session_rel_course_rel_user_table session_rc_ru
  2353. ON session_rc_ru.session_id = s.id AND session_rc_ru.user_id = '".$userId."'
  2354. WHERE
  2355. session_rc_ru.c_id = '$courseId' AND
  2356. session_rc_ru.status = 2 AND
  2357. session_rc_ru.session_id = '$session_id'";
  2358. $result = Database::query($sql);
  2359. $sessionIsCoach = Database::store_result($result);
  2360. }
  2361. if (!empty($session_id)) {
  2362. $sql = "SELECT DISTINCT id, name, access_start_date, access_end_date
  2363. FROM $session_table
  2364. WHERE session.id_coach = $userId AND id = $session_id
  2365. ORDER BY access_start_date, access_end_date, name";
  2366. $result = Database::query($sql);
  2367. if (!empty($sessionIsCoach)) {
  2368. $sessionIsCoach = array_merge($sessionIsCoach , Database::store_result($result));
  2369. } else {
  2370. $sessionIsCoach = Database::store_result($result);
  2371. }
  2372. }
  2373. return count($sessionIsCoach) > 0;
  2374. }
  2375. /**
  2376. * Checks whether the current user is a session administrator
  2377. * @return boolean True if current user is a course administrator
  2378. */
  2379. function api_is_session_admin()
  2380. {
  2381. $user = api_get_user_info();
  2382. return isset($user['status']) && $user['status'] == SESSIONADMIN;
  2383. }
  2384. /**
  2385. * Checks whether the current user is a human resources manager
  2386. * @return boolean True if current user is a human resources manager
  2387. */
  2388. function api_is_drh()
  2389. {
  2390. $user = api_get_user_info();
  2391. return isset($user['status']) && $user['status'] == DRH;
  2392. }
  2393. /**
  2394. * Checks whether the current user is a student
  2395. * @return boolean True if current user is a human resources manager
  2396. */
  2397. function api_is_student()
  2398. {
  2399. $user = api_get_user_info();
  2400. return isset($user['status']) && $user['status'] == STUDENT;
  2401. }
  2402. /**
  2403. * Checks whether the current user has the status 'teacher'
  2404. * @return boolean True if current user is a human resources manager
  2405. */
  2406. function api_is_teacher()
  2407. {
  2408. $user = api_get_user_info();
  2409. return isset($user['status']) && $user['status'] == COURSEMANAGER;
  2410. }
  2411. /**
  2412. * Checks whether the current user is a invited user
  2413. * @return boolean
  2414. */
  2415. function api_is_invitee()
  2416. {
  2417. $user = api_get_user_info();
  2418. return isset($user['status']) && $user['status'] == INVITEE;
  2419. }
  2420. /**
  2421. * This function checks whether a session is assigned into a category
  2422. * @param int - session id
  2423. * @param string - category name
  2424. * @return bool - true if is found, otherwise false
  2425. */
  2426. function api_is_session_in_category($session_id, $category_name)
  2427. {
  2428. $session_id = intval($session_id);
  2429. $category_name = Database::escape_string($category_name);
  2430. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2431. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  2432. $sql = "SELECT 1
  2433. FROM $tbl_session
  2434. WHERE $session_id IN (
  2435. SELECT s.id FROM $tbl_session s, $tbl_session_category sc
  2436. WHERE
  2437. s.session_category_id = sc.id AND
  2438. sc.name LIKE '%$category_name'
  2439. )";
  2440. $rs = Database::query($sql);
  2441. if (Database::num_rows($rs) > 0) {
  2442. return true;
  2443. } else {
  2444. return false;
  2445. }
  2446. }
  2447. /**
  2448. * Displays the title of a tool.
  2449. * Normal use: parameter is a string:
  2450. * api_display_tool_title("My Tool")
  2451. *
  2452. * Optionally, there can be a subtitle below
  2453. * the normal title, and / or a supra title above the normal title.
  2454. *
  2455. * e.g. supra title:
  2456. * group
  2457. * GROUP PROPERTIES
  2458. *
  2459. * e.g. subtitle:
  2460. * AGENDA
  2461. * calender & events tool
  2462. *
  2463. * @author Hugues Peeters <hugues.peeters@claroline.net>
  2464. * @param mixed $title_element - it could either be a string or an array
  2465. * containing 'supraTitle', 'mainTitle',
  2466. * 'subTitle'
  2467. * @return void
  2468. */
  2469. function api_display_tool_title($title_element) {
  2470. if (is_string($title_element)) {
  2471. $tit = $title_element;
  2472. unset ($title_element);
  2473. $title_element['mainTitle'] = $tit;
  2474. }
  2475. echo '<h3>';
  2476. if (!empty($title_element['supraTitle'])) {
  2477. echo '<small>'.$title_element['supraTitle'].'</small><br />';
  2478. }
  2479. if (!empty($title_element['mainTitle'])) {
  2480. echo $title_element['mainTitle'];
  2481. }
  2482. if (!empty($title_element['subTitle'])) {
  2483. echo '<br /><small>'.$title_element['subTitle'].'</small>';
  2484. }
  2485. echo '</h3>';
  2486. }
  2487. /**
  2488. * Displays options for switching between student view and course manager view
  2489. *
  2490. * Changes in version 1.2 (Patrick Cool)
  2491. * Student view switch now behaves as a real switch. It maintains its current state until the state
  2492. * is changed explicitly
  2493. *
  2494. * Changes in version 1.1 (Patrick Cool)
  2495. * student view now works correctly in subfolders of the document tool
  2496. * student view works correctly in the new links tool
  2497. *
  2498. * Example code for using this in your tools:
  2499. * //if ($is_courseAdmin && api_get_setting('student_view_enabled') == 'true') {
  2500. * // display_tool_view_option($isStudentView);
  2501. * //}
  2502. * //and in later sections, use api_is_allowed_to_edit()
  2503. *
  2504. * @author Roan Embrechts
  2505. * @author Patrick Cool
  2506. * @author Julio Montoya, changes added in Chamilo
  2507. * @version 1.2
  2508. * @todo rewrite code so it is easier to understand
  2509. */
  2510. function api_display_tool_view_option() {
  2511. if (api_get_setting('course.student_view_enabled') != 'true') {
  2512. return '';
  2513. }
  2514. $sourceurl = '';
  2515. $is_framed = false;
  2516. // Exceptions apply for all multi-frames pages
  2517. if (strpos($_SERVER['REQUEST_URI'], 'chat/chat_banner.php') !== false) {
  2518. // The chat is a multiframe bit that doesn't work too well with the student_view, so do not show the link
  2519. return '';
  2520. }
  2521. // Uncomment to remove student view link from document view page
  2522. if (strpos($_SERVER['REQUEST_URI'], 'lp/lp_header.php') !== false) {
  2523. if (empty($_GET['lp_id'])) {
  2524. return '';
  2525. }
  2526. $sourceurl = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?'));
  2527. $sourceurl = str_replace('lp/lp_header.php', 'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.intval($_GET['lp_id']).'&isStudentView='.($_SESSION['studentview']=='studentview' ? 'false' : 'true'), $sourceurl);
  2528. //showinframes doesn't handle student view anyway...
  2529. //return '';
  2530. $is_framed = true;
  2531. }
  2532. // Check whether the $_SERVER['REQUEST_URI'] contains already url parameters (thus a questionmark)
  2533. if (!$is_framed) {
  2534. if (strpos($_SERVER['REQUEST_URI'], '?') === false) {
  2535. $sourceurl = api_get_self().'?'.api_get_cidreq();
  2536. } else {
  2537. $sourceurl = $_SERVER['REQUEST_URI'];
  2538. }
  2539. }
  2540. $studentView = Session::read('studentview');
  2541. $output_string = '';
  2542. if (!empty($studentView)) {
  2543. if ($studentView == 'studentview') {
  2544. // We have to remove the isStudentView=true from the $sourceurl
  2545. $sourceurl = str_replace('&isStudentView=true', '', $sourceurl);
  2546. $sourceurl = str_replace('&isStudentView=false', '', $sourceurl);
  2547. $output_string .= '<a class="btn btn-primary btn-sm" href="'.$sourceurl.'&isStudentView=false" target="_self">'.Display::returnFontAwesomeIcon('eye').' '.get_lang('SwitchToTeacherView').'</a>';
  2548. } elseif ($studentView == 'teacherview') {
  2549. // Switching to teacherview
  2550. $sourceurl = str_replace('&isStudentView=true', '', $sourceurl);
  2551. $sourceurl = str_replace('&isStudentView=false', '', $sourceurl);
  2552. $output_string .= '<a class="btn btn-default btn-sm" href="'.$sourceurl.'&isStudentView=true" target="_self">'.Display::returnFontAwesomeIcon('eye').' '.get_lang('SwitchToStudentView').'</a>';
  2553. }
  2554. } else {
  2555. $output_string .= '<a class="btn btn-default btn-sm" href="'.$sourceurl.'&isStudentView=true" target="_self">'.Display::returnFontAwesomeIcon('eye').' '.get_lang('SwitchToStudentView').'</a>';
  2556. }
  2557. $html = Display::tag('div', $output_string, array('class'=>'view-options'));
  2558. return $html;
  2559. }
  2560. // TODO: This is for the permission section.
  2561. /**
  2562. * Function that removes the need to directly use is_courseAdmin global in
  2563. * tool scripts. It returns true or false depending on the user's rights in
  2564. * this particular course.
  2565. * Optionally checking for tutor and coach roles here allows us to use the
  2566. * student_view feature altogether with these roles as well.
  2567. * @param bool Whether to check if the user has the tutor role
  2568. * @param bool Whether to check if the user has the coach role
  2569. * @param bool Whether to check if the user has the session coach role
  2570. * @param bool check the student view or not
  2571. *
  2572. * @author Roan Embrechts
  2573. * @author Patrick Cool
  2574. * @author Julio Montoya
  2575. * @version 1.1, February 2004
  2576. * @return boolean true: the user has the rights to edit, false: he does not
  2577. */
  2578. function api_is_allowed_to_edit(
  2579. $tutor = false,
  2580. $coach = false,
  2581. $session_coach = false,
  2582. $check_student_view = true
  2583. ) {
  2584. $my_session_id = api_get_session_id();
  2585. $is_allowed_coach_to_edit = api_is_coach(null, null, $check_student_view);
  2586. $session_visibility = api_get_session_visibility($my_session_id);
  2587. $studentView = Session::read('studentview');
  2588. // Admins can edit anything.
  2589. if (api_is_platform_admin(false)) {
  2590. //The student preview was on
  2591. if ($check_student_view && $studentView == "studentview") {
  2592. return false;
  2593. } else {
  2594. return true;
  2595. }
  2596. }
  2597. $is_courseAdmin = api_is_course_admin();
  2598. if (!$is_courseAdmin && $tutor) {
  2599. // If we also want to check if the user is a tutor...
  2600. $is_courseAdmin = $is_courseAdmin || api_is_course_tutor();
  2601. }
  2602. if (!$is_courseAdmin && $coach) {
  2603. // If we also want to check if the user is a coach...';
  2604. // Check if session visibility is read only for coaches.
  2605. if ($session_visibility == SESSION_VISIBLE_READ_ONLY) {
  2606. $is_allowed_coach_to_edit = false;
  2607. }
  2608. if (api_get_setting('session.allow_coach_to_edit_course_session') == 'true') {
  2609. // Check if coach is allowed to edit a course.
  2610. $is_courseAdmin = $is_courseAdmin || $is_allowed_coach_to_edit;
  2611. } else {
  2612. $is_courseAdmin = $is_courseAdmin;
  2613. }
  2614. }
  2615. if (!$is_courseAdmin && $session_coach) {
  2616. $is_courseAdmin = $is_courseAdmin || $is_allowed_coach_to_edit;
  2617. }
  2618. // Check if the student_view is enabled, and if so, if it is activated.
  2619. if (api_get_setting('course.student_view_enabled') == 'true') {
  2620. if (!empty($my_session_id)) {
  2621. // Check if session visibility is read only for coaches.
  2622. if ($session_visibility == SESSION_VISIBLE_READ_ONLY) {
  2623. $is_allowed_coach_to_edit = false;
  2624. }
  2625. if (api_get_setting('session.allow_coach_to_edit_course_session') == 'true') {
  2626. // Check if coach is allowed to edit a course.
  2627. $is_allowed = $is_allowed_coach_to_edit;
  2628. } else {
  2629. $is_allowed = false;
  2630. }
  2631. if ($check_student_view) {
  2632. $is_allowed = $is_allowed && $studentView != 'studentview';
  2633. }
  2634. } else {
  2635. if ($check_student_view) {
  2636. $is_allowed = $is_courseAdmin && $studentView != 'studentview';
  2637. } else {
  2638. $is_allowed = $is_courseAdmin;
  2639. }
  2640. }
  2641. return $is_allowed;
  2642. } else {
  2643. return $is_courseAdmin;
  2644. }
  2645. }
  2646. /**
  2647. * Returns true if user is a course coach of at least one course in session
  2648. * @param int $sessionId
  2649. * @return bool
  2650. */
  2651. function api_is_coach_of_course_in_session($sessionId)
  2652. {
  2653. if (api_is_platform_admin()) {
  2654. return true;
  2655. }
  2656. $userId = api_get_user_id();
  2657. $courseList = UserManager::get_courses_list_by_session(
  2658. $userId,
  2659. $sessionId
  2660. );
  2661. // Session visibility.
  2662. $visibility = api_get_session_visibility(
  2663. $sessionId,
  2664. null,
  2665. false
  2666. );
  2667. if ($visibility != SESSION_VISIBLE && !empty($courseList)) {
  2668. // Course Coach session visibility.
  2669. $blockedCourseCount = 0;
  2670. $closedVisibilityList = array(
  2671. COURSE_VISIBILITY_CLOSED,
  2672. COURSE_VISIBILITY_HIDDEN
  2673. );
  2674. foreach ($courseList as $course) {
  2675. // Checking session visibility
  2676. $sessionCourseVisibility = api_get_session_visibility(
  2677. $sessionId,
  2678. $course['real_id'],
  2679. $ignore_visibility_for_admins
  2680. );
  2681. $courseIsVisible = !in_array(
  2682. $course['visibility'],
  2683. $closedVisibilityList
  2684. );
  2685. if ($courseIsVisible === false || $sessionCourseVisibility == SESSION_INVISIBLE) {
  2686. $blockedCourseCount++;
  2687. }
  2688. }
  2689. // If all courses are blocked then no show in the list.
  2690. if ($blockedCourseCount === count($courseList)) {
  2691. $visibility = SESSION_INVISIBLE;
  2692. } else {
  2693. $visibility = SESSION_VISIBLE;
  2694. }
  2695. }
  2696. switch ($visibility) {
  2697. case SESSION_VISIBLE_READ_ONLY:
  2698. case SESSION_VISIBLE:
  2699. case SESSION_AVAILABLE:
  2700. return true;
  2701. break;
  2702. case SESSION_INVISIBLE:
  2703. return false;
  2704. }
  2705. return false;
  2706. }
  2707. /**
  2708. * Checks if a student can edit contents in a session depending
  2709. * on the session visibility
  2710. * @param bool $tutor Whether to check if the user has the tutor role
  2711. * @param bool $coach Whether to check if the user has the coach role
  2712. * @return boolean true: the user has the rights to edit, false: he does not
  2713. */
  2714. function api_is_allowed_to_session_edit($tutor = false, $coach = false)
  2715. {
  2716. if (api_is_allowed_to_edit($tutor, $coach)) {
  2717. // If I'm a teacher, I will return true in order to not affect the normal behaviour of Chamilo tools.
  2718. return true;
  2719. } else {
  2720. if (api_get_session_id() == 0) {
  2721. // I'm not in a session so i will return true to not affect the normal behaviour of Chamilo tools.
  2722. return true;
  2723. } else {
  2724. // I'm in a session and I'm a student
  2725. $session_id = api_get_session_id();
  2726. // Get the session visibility
  2727. $session_visibility = api_get_session_visibility($session_id);
  2728. // if 5 the session is still available
  2729. //@todo We could load the session_rel_course_rel_user permission to increase the level of detail.
  2730. //echo api_get_user_id();
  2731. //echo api_get_course_id();
  2732. switch ($session_visibility) {
  2733. case SESSION_VISIBLE_READ_ONLY: // 1
  2734. return false;
  2735. case SESSION_VISIBLE: // 2
  2736. return true;
  2737. case SESSION_INVISIBLE: // 3
  2738. return false;
  2739. case SESSION_AVAILABLE: //5
  2740. return true;
  2741. }
  2742. }
  2743. }
  2744. }
  2745. /**
  2746. * Checks whether the user is allowed in a specific tool for a specific action
  2747. * @param string $tool the tool we are checking if the user has a certain permission
  2748. * @param string $action the action we are checking (add, edit, delete, move, visibility)
  2749. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2750. * @author Julio Montoya
  2751. * @version 1.0
  2752. */
  2753. function api_is_allowed($tool, $action, $task_id = 0)
  2754. {
  2755. $_user = api_get_user_info();
  2756. $_course = api_get_course_info();
  2757. if (api_is_course_admin()) {
  2758. return true;
  2759. }
  2760. //if (!$_SESSION['total_permissions'][$_course['code']] and $_course)
  2761. if (is_array($_course) and count($_course) > 0) {
  2762. require_once api_get_path(SYS_CODE_PATH).'blog/permissions/permissions_functions.inc.php';
  2763. // Getting the permissions of this user.
  2764. if ($task_id == 0) {
  2765. $user_permissions = get_permissions('user', $_user['user_id']);
  2766. $_SESSION['total_permissions'][$_course['code']] = $user_permissions;
  2767. }
  2768. // Getting the permissions of the task.
  2769. if ($task_id != 0) {
  2770. $task_permissions = get_permissions('task', $task_id);
  2771. /* !!! */$_SESSION['total_permissions'][$_course['code']] = $task_permissions;
  2772. }
  2773. //print_r($_SESSION['total_permissions']);
  2774. // Getting the permissions of the groups of the user
  2775. //$groups_of_user = GroupManager::get_group_ids($_course['db_name'], $_user['user_id']);
  2776. //foreach($groups_of_user as $group)
  2777. // $this_group_permissions = get_permissions('group', $group);
  2778. // Getting the permissions of the courseroles of the user
  2779. $user_courserole_permissions = get_roles_permissions('user', $_user['user_id']);
  2780. // Getting the permissions of the platformroles of the user
  2781. //$user_platformrole_permissions = get_roles_permissions('user', $_user['user_id'], ', platform');
  2782. // Getting the permissions of the roles of the groups of the user
  2783. //foreach($groups_of_user as $group)
  2784. // $this_group_courserole_permissions = get_roles_permissions('group', $group);
  2785. // Getting the permissions of the platformroles of the groups of the user
  2786. //foreach($groups_of_user as $group)
  2787. // $this_group_platformrole_permissions = get_roles_permissions('group', $group, 'platform');
  2788. }
  2789. // If the permissions are limited, we have to map the extended ones to the limited ones.
  2790. if (api_get_setting('permissions') == 'limited') {
  2791. if ($action == 'Visibility') {
  2792. $action = 'Edit';
  2793. }
  2794. if ($action == 'Move') {
  2795. $action = 'Edit';
  2796. }
  2797. }
  2798. // The session that contains all the permissions already exists for this course
  2799. // so there is no need to requery everything.
  2800. //my_print_r($_SESSION['total_permissions'][$_course['code']][$tool]);
  2801. if (is_array($_SESSION['total_permissions'][$_course['code']][$tool])) {
  2802. if (in_array($action, $_SESSION['total_permissions'][$_course['code']][$tool])) {
  2803. return true;
  2804. } else {
  2805. return false;
  2806. }
  2807. }
  2808. }
  2809. /**
  2810. * Tells whether this user is an anonymous user
  2811. * @param int $user_id User ID (optional, will take session ID if not provided)
  2812. * @param bool $db_check Whether to check in the database (true) or simply in
  2813. * the session (false) to see if the current user is the anonymous user
  2814. * @return bool true if this user is anonymous, false otherwise
  2815. */
  2816. function api_is_anonymous($user_id = null, $db_check = false) {
  2817. /*if (!isset($user_id)) {
  2818. $user_id = api_get_user_id();
  2819. }
  2820. if ($db_check) {
  2821. $info = api_get_user_info($user_id);
  2822. if ($info['status'] == ANONYMOUS) {
  2823. return true;
  2824. }
  2825. }
  2826. $_user = api_get_user_info();
  2827. if (isset($_user['status']) && $_user['status'] == ANONYMOUS) {
  2828. //if ($_user['user_id'] == 0) {
  2829. // In some cases, api_set_anonymous doesn't seem to be triggered in local.inc.php. Make sure it is.
  2830. // Occurs in agenda for admin links - YW
  2831. global $use_anonymous;
  2832. if (isset($use_anonymous) && $use_anonymous) {
  2833. api_set_anonymous();
  2834. }
  2835. return true;
  2836. }
  2837. return ((isset($_user['is_anonymous']) && $_user['is_anonymous'] === true) || $_user === false);*/
  2838. if (!isset($user_id)) {
  2839. $user_id = api_get_user_id();
  2840. }
  2841. if ($db_check) {
  2842. $info = api_get_user_info($user_id);
  2843. if ($info['status'] == 6 || $user_id == 0 || empty($info)) {
  2844. return true;
  2845. }
  2846. }
  2847. $_user = Session::read('_user');
  2848. if (!isset($_user) || (isset($_user['user_id']) && $_user['user_id'] == 0)) {
  2849. // In some cases, api_set_anonymous doesn't seem to be triggered in local.inc.php. Make sure it is.
  2850. // Occurs in agenda for admin links - YW
  2851. /*global $use_anonymous;
  2852. if (isset($use_anonymous) && $use_anonymous) {*/
  2853. api_set_anonymous();
  2854. //}
  2855. return true;
  2856. }
  2857. return isset($_user['is_anonymous']) && $_user['is_anonymous'] === true;
  2858. }
  2859. /**
  2860. * Displays message "You are not allowed here..." and exits the entire script.
  2861. * @param bool $print_headers Whether or not to print headers (default = false -> does not print them)
  2862. * @param string $message
  2863. */
  2864. function api_not_allowed($print_headers = false, $message = null)
  2865. {
  2866. $message = get_lang('NotAllowed');
  2867. throw new Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException($message);
  2868. if (api_get_setting('sso_authentication') === 'true') {
  2869. global $osso;
  2870. if ($osso) {
  2871. $osso->logout();
  2872. }
  2873. }
  2874. $home_url = api_get_path(WEB_PATH);
  2875. $user_id = api_get_user_id();
  2876. $course = api_get_course_id();
  2877. global $this_section;
  2878. if (CustomPages::enabled() && !isset($user_id)) {
  2879. if (empty($user_id)) {
  2880. // Why the CustomPages::enabled() need to be to set the request_uri
  2881. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  2882. }
  2883. CustomPages::display(CustomPages::INDEX_UNLOGGED);
  2884. }
  2885. $origin = isset($_GET['origin']) ? $_GET['origin'] : '';
  2886. $msg = null;
  2887. if (isset($message)) {
  2888. $msg = $message;
  2889. } else {
  2890. $msg = Display::return_message(
  2891. get_lang('NotAllowedClickBack').'<br/><br/><a href="'.$home_url.'">'.get_lang('ReturnToCourseHomepage').'</a>',
  2892. 'error',
  2893. false
  2894. );
  2895. }
  2896. $msg = Display::div($msg, array('align'=>'center'));
  2897. $show_headers = 0;
  2898. if ($print_headers && $origin != 'learnpath') {
  2899. $show_headers = 1;
  2900. }
  2901. $tpl = new Template(null, $show_headers, $show_headers);
  2902. $tpl->assign('hide_login_link', 1);
  2903. $tpl->assign('content', $msg);
  2904. if (($user_id != 0 && !api_is_anonymous()) &&
  2905. (!isset($course) || $course == -1) &&
  2906. empty($_GET['cidReq'])
  2907. ) {
  2908. // if the access is not authorized and there is some login information
  2909. // but the cidReq is not found, assume we are missing course data and send the user
  2910. // to the user_portal
  2911. $tpl->display_one_col_template();
  2912. exit;
  2913. }
  2914. if (!empty($_SERVER['REQUEST_URI']) &&
  2915. (!empty($_GET['cidReq']) ||
  2916. $this_section == SECTION_MYPROFILE ||
  2917. $this_section == SECTION_PLATFORM_ADMIN
  2918. )
  2919. ) {
  2920. $courseCode = api_get_course_id();
  2921. // Only display form and return to the previous URL if there was a course ID included
  2922. if ($user_id != 0 && !api_is_anonymous()) {
  2923. //if there is a user ID, then the user is not allowed but the session is still there. Say so and exit
  2924. $tpl->assign('content', $msg);
  2925. $tpl->display_one_col_template();
  2926. exit;
  2927. }
  2928. if (!is_null($courseCode)) {
  2929. api_set_firstpage_parameter($courseCode);
  2930. }
  2931. // If the user has no user ID, then his session has expired
  2932. $action = api_get_self().'?'.Security::remove_XSS($_SERVER['QUERY_STRING']);
  2933. $action = str_replace('&amp;', '&', $action);
  2934. $form = new FormValidator(
  2935. 'formLogin',
  2936. 'post',
  2937. $action,
  2938. null,
  2939. array(),
  2940. FormValidator::LAYOUT_BOX_NO_LABEL
  2941. );
  2942. $form->addElement(
  2943. 'text',
  2944. 'login',
  2945. null,
  2946. array('placeholder' => get_lang('UserName'), 'autocapitalize' => 'none')
  2947. );
  2948. $form->addElement(
  2949. 'password',
  2950. 'password',
  2951. null,
  2952. array('placeholder' => get_lang('Password'), 'autocapitalize' => 'none')
  2953. );
  2954. $form->addButton('submitAuth', get_lang('LoginEnter'), '', 'primary');
  2955. // see same text in auth/gotocourse.php and main_api.lib.php function api_not_allowed (above)
  2956. $content = Display::return_message(get_lang('NotAllowed'), 'error', false);
  2957. if (!empty($courseCode)) {
  2958. $content .= '<h4>'.get_lang('LoginToGoToThisCourse').'</h4>';
  2959. }
  2960. if (api_is_cas_activated()) {
  2961. $content .= Display::return_message(sprintf(get_lang('YouHaveAnInstitutionalAccount'), api_get_setting("Institution")), '', false);
  2962. $content .= Display::div("<br/><a href='".get_cas_direct_URL(api_get_course_id())."'>".sprintf(get_lang('LoginWithYourAccount'), api_get_setting("Institution"))."</a><br/><br/>", array('align'=>'center'));
  2963. $content .= Display::return_message(get_lang('YouDontHaveAnInstitutionAccount'));
  2964. $content .= "<p style='text-align:center'><a href='#' onclick='$(this).parent().next().toggle()'>".get_lang('LoginWithExternalAccount')."</a></p>";
  2965. $content .= "<div style='display:none;'>";
  2966. }
  2967. $content .= '<div class="well_login">';
  2968. $content .= $form->returnForm();
  2969. $content .='</div>';
  2970. if (api_is_cas_activated()) {
  2971. $content .= "</div>";
  2972. }
  2973. if (!empty($courseCode)) {
  2974. $content .= '<hr/><p style="text-align:center"><a href="'.$home_url.'">'.
  2975. get_lang('ReturnToCourseHomepage').'</a></p>';
  2976. } else {
  2977. $content .= '<hr/><p style="text-align:center"><a href="'.$home_url.'">'.
  2978. get_lang('BackHome').'</a></p>';
  2979. }
  2980. $tpl->setLoginBodyClass();
  2981. $tpl->assign('content', $content);
  2982. $tpl->display_one_col_template();
  2983. exit;
  2984. }
  2985. if ($user_id != 0 && !api_is_anonymous()) {
  2986. $tpl->display_one_col_template();
  2987. exit;
  2988. }
  2989. $msg = null;
  2990. // The session is over and we were not in a course,
  2991. // or we try to get directly to a private course without being logged
  2992. $courseId = api_get_course_int_id();
  2993. if (!empty($courseId)) {
  2994. api_set_firstpage_parameter(api_get_course_id());
  2995. $tpl->setLoginBodyClass();
  2996. $action = api_get_self().'?'.Security::remove_XSS($_SERVER['QUERY_STRING']);
  2997. $action = str_replace('&amp;', '&', $action);
  2998. $form = new FormValidator('formLogin', 'post', $action, null, array('class'=>'form-stacked'));
  2999. $form->addElement('text', 'login', null, array('autocapitalize' => 'none', 'placeholder' => get_lang('UserName'), 'class' => 'col-md-3'));
  3000. $form->addElement('password', 'password', null, array('placeholder' => get_lang('Password'), 'class' => 'col-md-3')); //new
  3001. $form->addButtonNext(get_lang('LoginEnter'), 'submitAuth');
  3002. // see same text in auth/gotocourse.php and main_api.lib.php function api_not_allowed (bellow)
  3003. $msg = Display::return_message(get_lang('NotAllowed'), 'error', false);
  3004. $msg .= '<h4>'.get_lang('LoginToGoToThisCourse').'</h4>';
  3005. $casEnabled = api_is_cas_activated();
  3006. if ($casEnabled) {
  3007. $msg .= Display::return_message(sprintf(get_lang('YouHaveAnInstitutionalAccount'), api_get_setting("Institution")), '', false);
  3008. $msg .= Display::div("<br/><a href='".get_cas_direct_URL(api_get_course_int_id())."'>".getCASLogoHTML()." ".sprintf(get_lang('LoginWithYourAccount'), api_get_setting("Institution"))."</a><br/><br/>", array('align'=>'center'));
  3009. $msg .= Display::return_message(get_lang('YouDontHaveAnInstitutionAccount'));
  3010. $msg .= "<p style='text-align:center'><a href='#' onclick='$(this).parent().next().toggle()'>".get_lang('LoginWithExternalAccount')."</a></p>";
  3011. $msg .= "<div style='display:none;'>";
  3012. }
  3013. $msg .= '<div class="well">';
  3014. $msg .= $form->returnForm();
  3015. $msg .='</div>';
  3016. if ($casEnabled) {
  3017. $msg .= "</div>";
  3018. }
  3019. } else {
  3020. // we were not in a course, return to home page
  3021. $msg = Display::return_message(
  3022. get_lang('NotAllowed').'<br/><br/><a href="'.$home_url.'">'.get_lang('BackHome').'</a><br />',
  3023. 'error',
  3024. false
  3025. );
  3026. }
  3027. $tpl->assign('content', $msg);
  3028. $tpl->display_one_col_template();
  3029. exit;
  3030. }
  3031. /**
  3032. * Gets a UNIX timestamp from a database (MySQL) datetime format string
  3033. * @param $last_post_datetime standard output date in a sql query
  3034. * @return integer timestamp
  3035. * @author Toon Van Hoecke <Toon.VanHoecke@UGent.be>
  3036. * @version October 2003
  3037. * @desc convert sql date to unix timestamp
  3038. */
  3039. function convert_sql_date($last_post_datetime) {
  3040. list ($last_post_date, $last_post_time) = explode(' ', $last_post_datetime);
  3041. list ($year, $month, $day) = explode('-', $last_post_date);
  3042. list ($hour, $min, $sec) = explode(':', $last_post_time);
  3043. return mktime((int)$hour, (int)$min, (int)$sec, (int)$month, (int)$day, (int)$year);
  3044. }
  3045. /**
  3046. * Gets item visibility from the item_property table
  3047. *
  3048. * Getting the visibility is done by getting the last updated visibility entry,
  3049. * using the largest session ID found if session 0 and another was found (meaning
  3050. * the only one that is actually from the session, in case there are results from
  3051. * session 0 *AND* session n).
  3052. * @param array Course properties array (result of api_get_course_info())
  3053. * @param string Tool (learnpath, document, etc)
  3054. * @param int The item ID in the given tool
  3055. * @param int The session ID (optional)
  3056. * @param string $tool
  3057. * @param integer $user_id
  3058. * @param string $type
  3059. * @return int -1 on error, 0 if invisible, 1 if visible
  3060. */
  3061. function api_get_item_visibility(
  3062. $_course,
  3063. $tool,
  3064. $id,
  3065. $session = 0,
  3066. $user_id = null,
  3067. $type = null,
  3068. $group_id = null
  3069. ) {
  3070. if (!is_array($_course) || count($_course) == 0 || empty($tool) || empty($id)) {
  3071. return -1;
  3072. }
  3073. $tool = Database::escape_string($tool);
  3074. $id = intval($id);
  3075. $session = (int) $session;
  3076. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3077. $course_id = intval($_course['real_id']);
  3078. $userCondition = '';
  3079. if (!empty($user_id)) {
  3080. $user_id = intval($user_id);
  3081. $userCondition = " AND to_user_id = $user_id ";
  3082. }
  3083. $typeCondition = '';
  3084. if (!empty($type)) {
  3085. $type = Database::escape_string($type);
  3086. $typeCondition = " AND lastedit_type = '$type' ";
  3087. }
  3088. $groupCondition = '';
  3089. if (!empty($group_id)) {
  3090. $group_id = intval($group_id);
  3091. $groupCondition = " AND to_group_id = '$group_id' ";
  3092. }
  3093. $sql = "SELECT visibility
  3094. FROM $TABLE_ITEMPROPERTY
  3095. WHERE
  3096. c_id = $course_id AND
  3097. tool = '$tool' AND
  3098. ref = $id AND
  3099. (session_id = $session OR session_id = 0 OR session_id IS NULL)
  3100. $userCondition $typeCondition $groupCondition
  3101. ORDER BY session_id DESC, lastedit_date DESC
  3102. LIMIT 1";
  3103. $res = Database::query($sql);
  3104. if ($res === false || Database::num_rows($res) == 0) {
  3105. return -1;
  3106. }
  3107. $row = Database::fetch_array($res);
  3108. return $row['visibility'];
  3109. }
  3110. /**
  3111. * Delete a row in the c_item_property table
  3112. *
  3113. * @param array $courseInfo
  3114. * @param string $tool
  3115. * @param int $itemId
  3116. * @param int $userId
  3117. * @param int $groupId
  3118. * @param int $sessionId
  3119. * @return false|null
  3120. */
  3121. function api_item_property_delete(
  3122. $courseInfo,
  3123. $tool,
  3124. $itemId,
  3125. $userId,
  3126. $groupId = 0,
  3127. $sessionId = 0
  3128. ) {
  3129. if (empty($courseInfo)) {
  3130. return false;
  3131. }
  3132. $courseId = intval($courseInfo['real_id']);
  3133. if (empty($courseId) || empty($tool) || empty($itemId)) {
  3134. return false;
  3135. }
  3136. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3137. $tool = Database::escape_string($tool);
  3138. $itemId = intval($itemId);
  3139. $userId = intval($userId);
  3140. $groupId = intval($groupId);
  3141. $sessionId = intval($sessionId);
  3142. $groupCondition = " AND to_group_id = $groupId ";
  3143. if (empty($groupId)) {
  3144. $groupCondition = " AND (to_group_id is NULL OR to_group_id = 0) ";
  3145. }
  3146. $userCondition = " AND to_user_id = $userId ";
  3147. if (empty($userId)) {
  3148. $userCondition = " AND (to_user_id is NULL OR to_user_id = 0) ";
  3149. }
  3150. $sessionCondition = api_get_session_condition($sessionId, true, false, 'session_id');
  3151. $sql = "DELETE FROM $table
  3152. WHERE
  3153. c_id = $courseId AND
  3154. tool = '$tool' AND
  3155. ref = $itemId
  3156. $sessionCondition
  3157. $userCondition
  3158. $groupCondition
  3159. ";
  3160. Database::query($sql);
  3161. }
  3162. /**
  3163. * Updates or adds item properties to the Item_propetry table
  3164. * Tool and lastedit_type are language independant strings (langvars->get_lang!)
  3165. *
  3166. * @param array $_course array with course properties
  3167. * @param string $tool tool id, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3168. * @param int $item_id id of the item itself, linked to key of every tool ('id', ...)
  3169. * @param string $last_edit_type add or update action
  3170. * (1) message to be translated (in trad4all) : e.g. DocumentAdded, DocumentUpdated;
  3171. * (2) "delete"
  3172. * (3) "visible"
  3173. * (4) "invisible"
  3174. * @param int $user_id id of the editing/adding user
  3175. * @param int $to_group_id group.iid
  3176. * @param int $to_user_id id of the intended user (always has priority over $to_group_id !), only relevant for $type (1)
  3177. * @param string $start_visible 0000-00-00 00:00:00 format
  3178. * @param string $end_visible 0000-00-00 00:00:00 format
  3179. * @param int $session_id The session ID, if any, otherwise will default to 0
  3180. * @return boolean False if update fails.
  3181. * @author Toon Van Hoecke <Toon.VanHoecke@UGent.be>, Ghent University
  3182. * @version January 2005
  3183. * @desc update the item_properties table (if entry not exists, insert) of the course
  3184. */
  3185. function api_item_property_update(
  3186. $_course,
  3187. $tool,
  3188. $item_id,
  3189. $last_edit_type,
  3190. $user_id,
  3191. $to_group_id = 0,
  3192. $to_user_id = null,
  3193. $start_visible = '',
  3194. $end_visible = '',
  3195. $session_id = 0
  3196. ) {
  3197. if (empty($_course)) {
  3198. return false;
  3199. }
  3200. $course_id = $_course['real_id'];
  3201. if (empty($course_id)) {
  3202. return false;
  3203. }
  3204. $em = Database::getManager();
  3205. // Definition of variables.
  3206. $tool = Database::escape_string($tool);
  3207. $item_id = intval($item_id);
  3208. $lastEditTypeNoFilter = $last_edit_type;
  3209. $last_edit_type = Database::escape_string($last_edit_type);
  3210. $user_id = intval($user_id);
  3211. $startVisible = "NULL";
  3212. if (!empty($start_visible)) {
  3213. $start_visible = Database::escape_string($start_visible);
  3214. $startVisible = "'$start_visible'";
  3215. }
  3216. $endVisible = "NULL";
  3217. if (!empty($end_visible)) {
  3218. $end_visible = Database::escape_string($end_visible);
  3219. $endVisible = "'$end_visible'";
  3220. }
  3221. $to_filter = '';
  3222. $time = api_get_utc_datetime();
  3223. if (!empty($session_id)) {
  3224. $session_id = intval($session_id);
  3225. } else {
  3226. $session_id = api_get_session_id();
  3227. }
  3228. // Definition of tables.
  3229. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3230. if ($to_user_id <= 0) {
  3231. $to_user_id = null; // No to_user_id set
  3232. }
  3233. if (!is_null($to_user_id)) {
  3234. // $to_user_id has more priority than $to_group_id
  3235. $to_user_id = intval($to_user_id);
  3236. $to_field = 'to_user_id';
  3237. $to_value = $to_user_id;
  3238. } else {
  3239. // $to_user_id is not set.
  3240. $to_field = 'to_group_id';
  3241. $to_value = $to_group_id;
  3242. }
  3243. $toValueCondition = empty($to_value) ? "NULL" : "'$to_value'";
  3244. // Set filters for $to_user_id and $to_group_id, with priority for $to_user_id
  3245. $condition_session = " AND session_id = $session_id ";
  3246. if (empty($session_id)) {
  3247. $condition_session = " AND (session_id = 0 OR session_id IS NULL) ";
  3248. }
  3249. $filter = " c_id = $course_id AND tool = '$tool' AND ref = $item_id $condition_session ";
  3250. // Check whether $to_user_id and $to_group_id are passed in the function call.
  3251. // If both are not passed (both are null) then it is a message for everybody and $to_group_id should be 0 !
  3252. if (is_null($to_user_id) && is_null($to_group_id)) {
  3253. $to_group_id = 0;
  3254. }
  3255. if (!is_null($to_user_id)) {
  3256. // Set filter to intended user.
  3257. $to_filter = " AND to_user_id = $to_user_id $condition_session";
  3258. } else {
  3259. // Set filter to intended group.
  3260. if (($to_group_id != 0) && $to_group_id == strval(intval($to_group_id))) {
  3261. $to_filter = " AND to_group_id = $to_group_id $condition_session";
  3262. }
  3263. }
  3264. // Adding filter if set.
  3265. $filter .= $to_filter;
  3266. // Update if possible
  3267. $set_type = '';
  3268. switch ($lastEditTypeNoFilter) {
  3269. case 'delete':
  3270. // delete = make item only visible for the platform admin.
  3271. $visibility = '2';
  3272. if (!empty($session_id)) {
  3273. // Check whether session id already exist into item_properties for updating visibility or add it.
  3274. $sql = "SELECT session_id FROM $tableItemProperty
  3275. WHERE
  3276. c_id = $course_id AND
  3277. tool = '$tool' AND
  3278. ref = $item_id AND
  3279. session_id = $session_id";
  3280. $rs = Database::query($sql);
  3281. if (Database::num_rows($rs) > 0) {
  3282. $sql = "UPDATE $tableItemProperty
  3283. SET lastedit_type = '".str_replace('_', '', ucwords($tool))."Deleted',
  3284. lastedit_date = '$time',
  3285. lastedit_user_id = $user_id,
  3286. visibility = $visibility,
  3287. session_id = $session_id $set_type
  3288. WHERE $filter";
  3289. $result = Database::query($sql);
  3290. } else {
  3291. $sql = "INSERT INTO $tableItemProperty (c_id, tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id, $to_field, visibility, start_visible, end_visible, session_id)
  3292. VALUES ($course_id, '$tool',$item_id, '$time', $user_id, '$time', '$last_edit_type',$user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $session_id)";
  3293. $result = Database::query($sql);
  3294. $id = Database::insert_id();
  3295. if ($id) {
  3296. $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
  3297. Database::query($sql);
  3298. }
  3299. }
  3300. } else {
  3301. $sql = "UPDATE $tableItemProperty
  3302. SET
  3303. lastedit_type='".str_replace('_', '', ucwords($tool))."Deleted',
  3304. lastedit_date='$time',
  3305. lastedit_user_id = $user_id,
  3306. visibility = $visibility $set_type
  3307. WHERE $filter";
  3308. $result = Database::query($sql);
  3309. }
  3310. break;
  3311. case 'visible' : // Change item to visible.
  3312. $visibility = '1';
  3313. if (!empty($session_id)) {
  3314. // Check whether session id already exist into item_properties for updating visibility or add it.
  3315. $sql = "SELECT session_id FROM $tableItemProperty
  3316. WHERE
  3317. c_id = $course_id AND
  3318. tool = '$tool' AND
  3319. ref = $item_id AND
  3320. session_id = $session_id";
  3321. $rs = Database::query($sql);
  3322. if (Database::num_rows($rs) > 0) {
  3323. $sql = "UPDATE $tableItemProperty
  3324. SET
  3325. lastedit_type='".str_replace('_', '', ucwords($tool))."Visible',
  3326. lastedit_date='$time',
  3327. lastedit_user_id = $user_id,
  3328. visibility = $visibility,
  3329. session_id = $session_id $set_type
  3330. WHERE $filter";
  3331. $result = Database::query($sql);
  3332. } else {
  3333. $sql = "INSERT INTO $tableItemProperty (c_id, tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id, $to_field, visibility, start_visible, end_visible, session_id)
  3334. VALUES ($course_id, '$tool', $item_id, '$time', $user_id, '$time', '$last_edit_type', $user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $session_id)";
  3335. $result = Database::query($sql);
  3336. $id = Database::insert_id();
  3337. if ($id) {
  3338. $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
  3339. Database::query($sql);
  3340. }
  3341. }
  3342. } else {
  3343. $sql = "UPDATE $tableItemProperty
  3344. SET
  3345. lastedit_type='".str_replace('_', '', ucwords($tool))."Visible',
  3346. lastedit_date='$time',
  3347. lastedit_user_id = $user_id,
  3348. visibility = $visibility $set_type
  3349. WHERE $filter";
  3350. $result = Database::query($sql);
  3351. }
  3352. break;
  3353. case 'invisible' : // Change item to invisible.
  3354. $visibility = '0';
  3355. if (!empty($session_id)) {
  3356. // Check whether session id already exist into item_properties for updating visibility or add it
  3357. $sql = "SELECT session_id FROM $tableItemProperty
  3358. WHERE
  3359. c_id = $course_id AND
  3360. tool = '$tool' AND
  3361. ref = $item_id AND
  3362. session_id = $session_id";
  3363. $rs = Database::query($sql);
  3364. if (Database::num_rows($rs) > 0) {
  3365. $sql = "UPDATE $tableItemProperty
  3366. SET
  3367. lastedit_type = '".str_replace('_', '', ucwords($tool))."Invisible',
  3368. lastedit_date = '$time',
  3369. lastedit_user_id = $user_id,
  3370. visibility = $visibility,
  3371. session_id = $session_id $set_type
  3372. WHERE $filter";
  3373. $result = Database::query($sql);
  3374. } else {
  3375. $sql = "INSERT INTO $tableItemProperty (c_id, tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id,$to_field, visibility, start_visible, end_visible, session_id)
  3376. VALUES ($course_id, '$tool', $item_id, '$time', $user_id, '$time', '$last_edit_type', $user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $session_id)";
  3377. $result = Database::query($sql);
  3378. $id = Database::insert_id();
  3379. if ($id) {
  3380. $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
  3381. Database::query($sql);
  3382. }
  3383. }
  3384. } else {
  3385. $sql = "UPDATE $tableItemProperty
  3386. SET
  3387. lastedit_type = '".str_replace('_', '', ucwords($tool))."Invisible',
  3388. lastedit_date = '$time',
  3389. lastedit_user_id = $user_id,
  3390. visibility = $visibility $set_type
  3391. WHERE $filter";
  3392. $result = Database::query($sql);
  3393. }
  3394. break;
  3395. default: // The item will be added or updated.
  3396. $set_type = ", lastedit_type = '$last_edit_type' ";
  3397. $visibility = '1';
  3398. //$filter .= $to_filter; already added
  3399. $sql = "UPDATE $tableItemProperty
  3400. SET
  3401. lastedit_date = '$time',
  3402. lastedit_user_id = $user_id $set_type
  3403. WHERE $filter";
  3404. $result = Database::query($sql);
  3405. }
  3406. // Insert if no entries are found (can only happen in case of $last_edit_type switch is 'default').
  3407. if ($result == false || Database::affected_rows($result) == 0) {
  3408. $objCourse = $em->find('ChamiloCoreBundle:Course', intval($course_id));
  3409. $objTime = new DateTime('now', new DateTimeZone('UTC'));
  3410. $objUser = $em->find('ChamiloUserBundle:User', intval($user_id));
  3411. $objGroup = $em->find('ChamiloCourseBundle:CGroupInfo', intval($to_group_id));
  3412. $objToUser = $em->find('ChamiloUserBundle:User', intval($to_user_id));
  3413. $objSession = $em->find('ChamiloCoreBundle:Session', intval($session_id));
  3414. $startVisibleDate = !empty($start_visible) ? new DateTime($start_visible, new DateTimeZone('UTC')) : null;
  3415. $endVisibleDate = !empty($endVisibleDate) ? new DateTime($endVisibleDate, new DateTimeZone('UTC')) : null;
  3416. $cItemProperty = new CItemProperty($objCourse);
  3417. $cItemProperty
  3418. ->setTool($tool)
  3419. ->setRef($item_id)
  3420. ->setInsertDate($objTime)
  3421. ->setInsertUser($objUser)
  3422. ->setLasteditDate($objTime)
  3423. ->setLasteditType($last_edit_type)
  3424. ->setGroup($objGroup)
  3425. ->setToUser($objToUser)
  3426. ->setVisibility($visibility)
  3427. ->setStartVisible($startVisibleDate)
  3428. ->setEndVisible($endVisibleDate)
  3429. ->setSession($objSession);
  3430. $em->persist($cItemProperty);
  3431. $em->flush();
  3432. $id = $cItemProperty->getIid();
  3433. if ($id) {
  3434. $cItemProperty->setId($id);
  3435. $em->merge($cItemProperty);
  3436. $em->flush();
  3437. return false;
  3438. }
  3439. }
  3440. return true;
  3441. }
  3442. /**
  3443. * Gets item property by tool
  3444. * @param string course code
  3445. * @param string tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3446. * @param int id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
  3447. * @param int $session_id
  3448. * @param string $tool
  3449. * @param string $course_code
  3450. * @return array All fields from c_item_property (all rows found) or empty array
  3451. */
  3452. function api_get_item_property_by_tool($tool, $course_code, $session_id = null)
  3453. {
  3454. $course_info = api_get_course_info($course_code);
  3455. $tool = Database::escape_string($tool);
  3456. // Definition of tables.
  3457. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3458. $session_id = intval($session_id);
  3459. $session_condition = ' AND session_id = '.$session_id;
  3460. if (empty($session_id)) {
  3461. $session_condition = " AND (session_id = 0 OR session_id IS NULL) ";
  3462. }
  3463. $course_id = $course_info['real_id'];
  3464. $sql = "SELECT * FROM $item_property_table
  3465. WHERE
  3466. c_id = $course_id AND
  3467. tool = '$tool'
  3468. $session_condition ";
  3469. $rs = Database::query($sql);
  3470. $list = array();
  3471. if (Database::num_rows($rs) > 0) {
  3472. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3473. $list[] = $row;
  3474. }
  3475. }
  3476. return $list;
  3477. }
  3478. /**
  3479. * Gets item property by tool and user
  3480. * @param int $userId
  3481. * @param int $tool
  3482. * @param int $courseId
  3483. * @param int $session_id
  3484. * @return array
  3485. */
  3486. function api_get_item_property_list_by_tool_by_user(
  3487. $userId,
  3488. $tool,
  3489. $courseId,
  3490. $session_id = 0
  3491. ) {
  3492. $userId = intval($userId);
  3493. $tool = Database::escape_string($tool);
  3494. $session_id = intval($session_id);
  3495. $courseId = intval($courseId);
  3496. // Definition of tables.
  3497. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3498. $session_condition = ' AND session_id = '.$session_id;
  3499. if (empty($session_id)) {
  3500. $session_condition = " AND (session_id = 0 OR session_id IS NULL) ";
  3501. }
  3502. $sql = "SELECT * FROM $item_property_table
  3503. WHERE
  3504. insert_user_id = $userId AND
  3505. c_id = $courseId AND
  3506. tool = '$tool'
  3507. $session_condition ";
  3508. $rs = Database::query($sql);
  3509. $list = array();
  3510. if (Database::num_rows($rs) > 0) {
  3511. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3512. $list[] = $row;
  3513. }
  3514. }
  3515. return $list;
  3516. }
  3517. /**
  3518. * Gets item property id from tool of a course
  3519. * @param string $course_code course code
  3520. * @param string $tool tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3521. * @param int $ref id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
  3522. * @param int $sessionId Session ID (optional)
  3523. * @return int
  3524. */
  3525. function api_get_item_property_id($course_code, $tool, $ref, $sessionId = 0)
  3526. {
  3527. $course_info = api_get_course_info($course_code);
  3528. $tool = Database::escape_string($tool);
  3529. $ref = intval($ref);
  3530. // Definition of tables.
  3531. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3532. $course_id = $course_info['real_id'];
  3533. $sessionId = (int) $sessionId;
  3534. $sessionCondition = " AND session_id = $sessionId ";
  3535. if (empty($sessionId)) {
  3536. $sessionCondition = " AND (session_id = 0 OR session_id IS NULL) ";
  3537. }
  3538. $sql = "SELECT id FROM $tableItemProperty
  3539. WHERE
  3540. c_id = $course_id AND
  3541. tool = '$tool' AND
  3542. ref = $ref
  3543. $sessionCondition";
  3544. $rs = Database::query($sql);
  3545. $item_property_id = '';
  3546. if (Database::num_rows($rs) > 0) {
  3547. $row = Database::fetch_array($rs);
  3548. $item_property_id = $row['id'];
  3549. }
  3550. return $item_property_id;
  3551. }
  3552. /**
  3553. * Inserts a record in the track_e_item_property table (No update)
  3554. * @param string $tool
  3555. * @param int $ref
  3556. * @param string $title
  3557. * @param string $content
  3558. * @param int $progress
  3559. * @return bool|int
  3560. */
  3561. function api_track_item_property_update($tool, $ref, $title, $content, $progress)
  3562. {
  3563. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  3564. $course_id = api_get_course_int_id(); //numeric
  3565. $course_code = api_get_course_id(); //alphanumeric
  3566. $item_property_id = api_get_item_property_id($course_code, $tool, $ref);
  3567. if (!empty($item_property_id)) {
  3568. $sql = "INSERT IGNORE INTO $tbl_stats_item_property SET
  3569. course_id = '$course_id',
  3570. item_property_id = '$item_property_id',
  3571. title = '".Database::escape_string($title)."',
  3572. content = '".Database::escape_string($content)."',
  3573. progress = '".intval($progress)."',
  3574. lastedit_date = '".api_get_utc_datetime()."',
  3575. lastedit_user_id = '".api_get_user_id()."',
  3576. session_id = '".api_get_session_id()."'";
  3577. $result = Database::query($sql);
  3578. $affected_rows = Database::affected_rows($result);
  3579. return $affected_rows;
  3580. }
  3581. return false;
  3582. }
  3583. /**
  3584. * @param string $tool
  3585. * @param int $ref
  3586. * @return array|resource
  3587. */
  3588. function api_get_track_item_property_history($tool, $ref)
  3589. {
  3590. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  3591. $course_id = api_get_course_int_id(); //numeric
  3592. $course_code = api_get_course_id(); //alphanumeric
  3593. $item_property_id = api_get_item_property_id($course_code, $tool, $ref);
  3594. $sql = "SELECT * FROM $tbl_stats_item_property
  3595. WHERE item_property_id = $item_property_id AND course_id = $course_id
  3596. ORDER BY lastedit_date DESC";
  3597. $result = Database::query($sql);
  3598. if ($result == false) {
  3599. $result = array();
  3600. } else {
  3601. $result = Database::store_result($result,'ASSOC');
  3602. }
  3603. return $result;
  3604. }
  3605. /**
  3606. * Gets item property data from tool of a course id
  3607. * @param int $course_id
  3608. * @param string $tool tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3609. * @param int $ref id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
  3610. * @param int $session_id
  3611. * @param int $groupId
  3612. *
  3613. * @return array Array with all fields from c_item_property, empty array if not found or false if course could not be found
  3614. */
  3615. function api_get_item_property_info($course_id, $tool, $ref, $session_id = 0, $groupId = 0)
  3616. {
  3617. $courseInfo = api_get_course_info_by_id($course_id);
  3618. if (empty($courseInfo)) {
  3619. return false;
  3620. }
  3621. $tool = Database::escape_string($tool);
  3622. $ref = intval($ref);
  3623. $course_id = $courseInfo['real_id'];
  3624. $session_id = intval($session_id);
  3625. $sessionCondition = " session_id = $session_id";
  3626. if (empty($session_id)) {
  3627. $sessionCondition = " (session_id = 0 OR session_id IS NULL) ";
  3628. }
  3629. // Definition of tables.
  3630. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3631. $sql = "SELECT * FROM $table
  3632. WHERE
  3633. c_id = $course_id AND
  3634. tool = '$tool' AND
  3635. ref = $ref AND
  3636. $sessionCondition ";
  3637. if (!empty($groupId)) {
  3638. $groupId = intval($groupId);
  3639. $sql .= " AND to_group_id = $groupId ";
  3640. }
  3641. $rs = Database::query($sql);
  3642. $row = array();
  3643. if (Database::num_rows($rs) > 0) {
  3644. $row = Database::fetch_array($rs,'ASSOC');
  3645. }
  3646. return $row;
  3647. }
  3648. /**
  3649. * Displays a combo box so the user can select his/her preferred language.
  3650. * @param string The desired name= value for the select
  3651. * @param bool Whether we use the JQuery Chozen library or not
  3652. * (in some cases, like the indexing language picker, it can alter the presentation)
  3653. * @return string
  3654. */
  3655. function api_get_languages_combo($name = 'language')
  3656. {
  3657. $ret = '';
  3658. $platformLanguage = api_get_setting('platformLanguage');
  3659. // Retrieve a complete list of all the languages.
  3660. $languages = api_get_languages();
  3661. if (count($languages) < 2) {
  3662. return $ret;
  3663. }
  3664. $default = api_get_user_language();
  3665. $ret .= '<select name="' . $name . '" id="language_chosen" class="selectpicker show-tick form-control">';
  3666. foreach ($languages as $iso => $value) {
  3667. if ($iso == $default) {
  3668. $selected = ' selected="selected"';
  3669. } else {
  3670. $selected = '';
  3671. }
  3672. $ret .= sprintf('<option value=%s" %s>%s</option>', $iso, $selected, $value);
  3673. }
  3674. $ret .= '</select>';
  3675. return $ret;
  3676. }
  3677. function api_get_user_language()
  3678. {
  3679. $user_language = null;
  3680. if (!api_is_anonymous()) {
  3681. $userInfo = api_get_user_info();
  3682. if (isset($userInfo['language'])) {
  3683. $user_language = $userInfo['language'];
  3684. }
  3685. }
  3686. // When this is use?
  3687. /*
  3688. if (isset($_POST['language_list']) && !empty($_POST['language_list'])) {
  3689. if (in_array($_GET['language'], $valid_languages)) {
  3690. $user_language = str_replace('index.php?language=', '', $_POST['language_list']);
  3691. }
  3692. }*/
  3693. if (isset($_REQUEST['language']) && !empty($_REQUEST['language'])) {
  3694. api_set_login_language($_REQUEST['language']);
  3695. }
  3696. // Last chance we get the platform language
  3697. if (empty($user_language)) {
  3698. $user_language = Container::getTranslator()->getLocale();
  3699. }
  3700. return $user_language;
  3701. }
  3702. /**
  3703. * Displays a form (drop down menu) so the user can select his/her preferred language.
  3704. * The form works with or without javascript
  3705. * @param boolean Hide form if only one language available (defaults to false = show the box anyway)
  3706. * @return void Display the box directly
  3707. */
  3708. function api_display_language_form($hide_if_no_choice = false)
  3709. {
  3710. // Retrieve a complete list of all the languages.
  3711. $language_list = api_get_languages();
  3712. if (count($language_list) <= 1 && $hide_if_no_choice) {
  3713. return; //don't show any form
  3714. }
  3715. $user_selected_language = api_get_language_selected_in_login();
  3716. $html = '
  3717. <script type="text/javascript">
  3718. <!--
  3719. $(document).ready(function() {
  3720. $("#language_list").change(function() {
  3721. jumpMenu("parent",this,0);
  3722. });
  3723. });
  3724. function jumpMenu(targ,selObj,restore){ // v3.0
  3725. eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
  3726. if (restore) selObj.selectedIndex=0;
  3727. }
  3728. //-->
  3729. </script>';
  3730. $html .= '<form id="lang_form" name="lang_form" method="post" action="'.api_get_self().'">';
  3731. $html .= '<label style="display: none;" for="language_list">' . get_lang('Language') . '</label>';
  3732. $html .= '<select id="language_list" class="selectpicker show-tick form-control" name="language_list" >';
  3733. foreach ($language_list as $iso => $value) {
  3734. if ($iso == $user_selected_language) {
  3735. $option_end = ' selected="selected" >';
  3736. } else {
  3737. $option_end = '>';
  3738. }
  3739. $html .= '<option value="'.api_get_self().'?language='.$iso.'"'.$option_end;
  3740. //echo substr($value, 0, 16); // Cut string to keep 800x600 aspect.
  3741. $html .= $value.'</option>';
  3742. }
  3743. $html .= '</select>';
  3744. $html .= '<noscript><input type="submit" name="user_select_language" value="'.get_lang('Ok').'" /></noscript>';
  3745. $html .= '</form>';
  3746. return $html;
  3747. }
  3748. function api_get_language_selected_in_login()
  3749. {
  3750. $language = api_get_setting('language.platform_language');
  3751. $userLanguageChoice = Session::read('user_language_choice');
  3752. if (isset($userLanguageChoice) && !empty($userLanguageChoice)) {
  3753. $language = $userLanguageChoice;
  3754. }
  3755. return $language;
  3756. }
  3757. /**
  3758. * Returns a list of all the languages that are made available by the admin.
  3759. * @return array An array with all languages. Structure of the array is
  3760. * array['name'] = An array with the name of every language
  3761. * array['folder'] = An array with the corresponding names of the language-folders in the filesystem
  3762. */
  3763. function api_get_languages() {
  3764. $tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  3765. $sql = "SELECT * FROM $tbl_language WHERE available='1'
  3766. ORDER BY original_name ASC";
  3767. $result = Database::query($sql);
  3768. $language_list = array();
  3769. while ($row = Database::fetch_array($result)) {
  3770. $language_list[$row['isocode']] = $row['original_name'];
  3771. }
  3772. return $language_list;
  3773. }
  3774. /**
  3775. * Returns a list of all the languages that are made available by the admin.
  3776. * @return array
  3777. */
  3778. function api_get_languages_to_array() {
  3779. $tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  3780. $sql = "SELECT * FROM $tbl_language WHERE available='1' ORDER BY original_name ASC";
  3781. $result = Database::query($sql);
  3782. $languages = array();
  3783. while ($row = Database::fetch_array($result)) {
  3784. $languages[$row['isocode']] = $row['original_name'];
  3785. }
  3786. return $languages;
  3787. }
  3788. /**
  3789. * Returns the id (the database id) of a language
  3790. * @param string language name (the corresponding name of the language-folder in the filesystem)
  3791. * @return int id of the language
  3792. */
  3793. function api_get_language_id($language)
  3794. {
  3795. $tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  3796. if (empty($language)) {
  3797. return null;
  3798. }
  3799. $language = Database::escape_string($language);
  3800. $sql = "SELECT id FROM $tbl_language
  3801. WHERE dokeos_folder = '$language' LIMIT 1";
  3802. $result = Database::query($sql);
  3803. $row = Database::fetch_array($result);
  3804. return $row['id'];
  3805. }
  3806. /**
  3807. * Gets language of the requested type for the current user. Types are :
  3808. * user_profil_lang : profile language of current user
  3809. * user_select_lang : language selected by user at login
  3810. * course_lang : language of the current course
  3811. * platform_lang : default platform language
  3812. * @param string $lang_type
  3813. * @return string
  3814. **/
  3815. function api_get_language_from_type($lang_type)
  3816. {
  3817. $return = false;
  3818. $language = false;
  3819. switch ($lang_type) {
  3820. case 'platform_lang':
  3821. $platformLanguage = api_get_setting('language.platform_language');
  3822. if (!empty($platformLanguage)) {
  3823. $language = $platformLanguage;
  3824. }
  3825. break;
  3826. case 'user_profil_lang':
  3827. $_user = Session::read('_user');
  3828. if (isset($_user['language']) && !empty($_user['language'])) {
  3829. $language = $_user['language'];
  3830. }
  3831. break;
  3832. case 'user_selected_lang':
  3833. $language = api_get_language_selected_in_login();
  3834. break;
  3835. case 'course_lang':
  3836. $_course = api_get_course_info();
  3837. if (isset($_course['language']) && !empty($_course['language'])) {
  3838. $language = $_course['language'];
  3839. }
  3840. break;
  3841. default:
  3842. $language = false;
  3843. break;
  3844. }
  3845. return $language;
  3846. }
  3847. /**
  3848. * Get the language information by its id
  3849. * @param int $languageId
  3850. * @return array
  3851. */
  3852. function api_get_language_info($languageId) {
  3853. $language = Database::getManager()
  3854. ->find('ChamiloCoreBundle:Language', intval($languageId));
  3855. if (!$language) {
  3856. return [];
  3857. }
  3858. return [
  3859. 'id' => $language->getId(),
  3860. 'original_name' => $language->getOriginalName(),
  3861. 'english_name' => $language->getEnglishName(),
  3862. 'isocode' => $language->getIsocode(),
  3863. 'dokeos_folder' => $language->getDokeosFolder(),
  3864. 'available' => $language->getAvailable(),
  3865. 'parent_id' => $language->getParent() ? $language->getParent()->getId() : null
  3866. ];
  3867. }
  3868. /**
  3869. * Returns the name of the visual (CSS) theme to be applied on the current page.
  3870. * The returned name depends on the platform, course or user -wide settings.
  3871. * @return string The visual theme's name, it is the name of a folder inside .../chamilo/main/css/
  3872. */
  3873. function api_get_visual_theme()
  3874. {
  3875. static $visual_theme;
  3876. if (!isset($visual_theme)) {
  3877. $platform_theme = api_get_setting('stylesheets');
  3878. // Platform's theme.
  3879. $visual_theme = $platform_theme;
  3880. if (api_get_setting('profile.user_selected_theme') == 'true') {
  3881. $user_info = api_get_user_info();
  3882. if (isset($user_info['theme'])) {
  3883. $user_theme = $user_info['theme'];
  3884. if (!empty($user_theme)) {
  3885. $visual_theme = $user_theme;
  3886. // User's theme.
  3887. }
  3888. }
  3889. }
  3890. $course_id = api_get_course_id();
  3891. if (!empty($course_id) && $course_id != -1) {
  3892. if (api_get_setting('course.allow_course_theme') == 'true') {
  3893. $course_theme = api_get_course_setting('course_theme');
  3894. if (!empty($course_theme) && $course_theme != -1) {
  3895. if (!empty($course_theme)) {
  3896. // Course's theme.
  3897. $visual_theme = $course_theme;
  3898. }
  3899. }
  3900. $allow_lp_theme = api_get_course_setting('allow_learning_path_theme');
  3901. if ($allow_lp_theme == 1) {
  3902. global $lp_theme_css, $lp_theme_config;
  3903. // These variables come from the file lp_controller.php.
  3904. if (!$lp_theme_config) {
  3905. if (!empty($lp_theme_css)) {
  3906. // LP's theme.
  3907. $visual_theme = $lp_theme_css;
  3908. }
  3909. }
  3910. }
  3911. }
  3912. }
  3913. if (empty($visual_theme)) {
  3914. $visual_theme = 'chamilo';
  3915. }
  3916. global $lp_theme_log;
  3917. if ($lp_theme_log) {
  3918. $visual_theme = $platform_theme;
  3919. }
  3920. }
  3921. return $visual_theme;
  3922. }
  3923. /**
  3924. * Returns a list of CSS themes currently available in the CSS folder
  3925. * @return array List of themes directories from the css folder
  3926. * Note: Directory names (names of themes) in the file system should contain ASCII-characters only.
  3927. */
  3928. function api_get_themes() {
  3929. $cssdir = api_get_path(SYS_CSS_PATH) . 'themes/';
  3930. $list_dir = array();
  3931. $list_name = array();
  3932. if (is_dir($cssdir)) {
  3933. $themes = @scandir($cssdir);
  3934. if (is_array($themes)) {
  3935. if ($themes !== false) {
  3936. sort($themes);
  3937. foreach ($themes as & $theme) {
  3938. if (substr($theme, 0, 1) == '.') {
  3939. continue;
  3940. } else {
  3941. if (is_dir($cssdir.$theme)) {
  3942. $list_dir[] = $theme;
  3943. $list_name[] = ucwords(str_replace('_', ' ', $theme));
  3944. }
  3945. }
  3946. }
  3947. }
  3948. }
  3949. }
  3950. return array($list_dir, $list_name);
  3951. }
  3952. /**
  3953. * Find the largest sort value in a given user_course_category
  3954. * This function is used when we are moving a course to a different category
  3955. * and also when a user subscribes to courses (the new course is added at the end of the main category
  3956. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3957. * @param int $user_course_category the id of the user_course_category
  3958. * @param integer $user_id
  3959. * @return int the value of the highest sort of the user_course_category
  3960. */
  3961. function api_max_sort_value($user_course_category, $user_id)
  3962. {
  3963. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3964. $sql = "SELECT max(sort) as max_sort FROM $tbl_course_user
  3965. WHERE
  3966. user_id='".intval($user_id)."' AND
  3967. relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  3968. user_course_cat='".intval($user_course_category)."'";
  3969. $result_max = Database::query($sql);
  3970. if (Database::num_rows($result_max) == 1) {
  3971. $row_max = Database::fetch_array($result_max);
  3972. return $row_max['max_sort'];
  3973. }
  3974. return 0;
  3975. }
  3976. /**
  3977. * Determines the number of plugins installed for a given location
  3978. */
  3979. function api_number_of_plugins($location) {
  3980. global $_plugins;
  3981. return isset($_plugins[$location]) && is_array($_plugins[$location]) ? count($_plugins[$location]) : 0;
  3982. }
  3983. /**
  3984. * Transforms a number of seconds in hh:mm:ss format
  3985. * @author Julian Prud'homme
  3986. * @param integer the number of seconds
  3987. * @return string the formated time
  3988. */
  3989. function api_time_to_hms($seconds)
  3990. {
  3991. // $seconds = -1 means that we have wrong data in the db.
  3992. if ($seconds == -1) {
  3993. return
  3994. get_lang('Unknown').
  3995. Display::return_icon(
  3996. 'info2.gif',
  3997. get_lang('WrongDatasForTimeSpentOnThePlatform'),
  3998. array('align' => 'absmiddle', 'hspace' => '3px')
  3999. );
  4000. }
  4001. // How many hours ?
  4002. $hours = floor($seconds / 3600);
  4003. // How many minutes ?
  4004. $min = floor(($seconds - ($hours * 3600)) / 60);
  4005. // How many seconds
  4006. $sec = floor($seconds - ($hours * 3600) - ($min * 60));
  4007. if ($sec < 10) {
  4008. $sec = "0$sec";
  4009. }
  4010. if ($min < 10) {
  4011. $min = "0$min";
  4012. }
  4013. return "$hours:$min:$sec";
  4014. }
  4015. /* FILE SYSTEM RELATED FUNCTIONS */
  4016. /**
  4017. * Returns the permissions to be assigned to every newly created directory by the web-server.
  4018. * The return value is based on the platform administrator's setting
  4019. * "Administration > Configuration settings > Security > Permissions for new directories".
  4020. * @return int Returns the permissions in the format "Owner-Group-Others, Read-Write-Execute", as an integer value.
  4021. */
  4022. function api_get_permissions_for_new_directories() {
  4023. static $permissions;
  4024. if (!isset($permissions)) {
  4025. $permissions = trim(
  4026. api_get_setting('document.permissions_for_new_directories')
  4027. );
  4028. // The default value 0777 is according to that in the platform administration panel after fresh system installation.
  4029. $permissions = octdec(!empty($permissions) ? $permissions : '0777');
  4030. }
  4031. return $permissions;
  4032. }
  4033. /**
  4034. * Returns the permissions to be assigned to every newly created directory by the web-server.
  4035. * The return value is based on the platform administrator's setting
  4036. * "Administration > Configuration settings > Security > Permissions for new files".
  4037. * @return int Returns the permissions in the format
  4038. * "Owner-Group-Others, Read-Write-Execute", as an integer value.
  4039. */
  4040. function api_get_permissions_for_new_files() {
  4041. static $permissions;
  4042. if (!isset($permissions)) {
  4043. $permissions = trim(
  4044. api_get_setting('document.permissions_for_new_files')
  4045. );
  4046. // The default value 0666 is according to that in the platform administration panel after fresh system installation.
  4047. $permissions = octdec(!empty($permissions) ? $permissions : '0666');
  4048. }
  4049. return $permissions;
  4050. }
  4051. /**
  4052. * Deletes a file, or a folder and its contents
  4053. *
  4054. * @author Aidan Lister <aidan@php.net>
  4055. * @version 1.0.3
  4056. * @param string $dirname Directory to delete
  4057. * @param bool Deletes only the content or not
  4058. * @param bool $strict if one folder/file fails stop the loop
  4059. * @return bool Returns TRUE on success, FALSE on failure
  4060. * @link http://aidanlister.com/2004/04/recursively-deleting-a-folder-in-php/
  4061. * @author Yannick Warnier, adaptation for the Chamilo LMS, April, 2008
  4062. * @author Ivan Tcholakov, a sanity check about Directory class creation has been added, September, 2009
  4063. */
  4064. function rmdirr($dirname, $delete_only_content_in_folder = false, $strict = false) {
  4065. $res = true;
  4066. // A sanity check.
  4067. if (!file_exists($dirname)) {
  4068. return false;
  4069. }
  4070. $php_errormsg = '';
  4071. // Simple delete for a file.
  4072. if (is_file($dirname) || is_link($dirname)) {
  4073. $res = unlink($dirname);
  4074. if ($res === false) {
  4075. error_log(__FILE__.' line '.__LINE__.': '.((bool)ini_get('track_errors') ? $php_errormsg : 'Error not recorded because track_errors is off in your php.ini'), 0);
  4076. }
  4077. return $res;
  4078. }
  4079. // Loop through the folder.
  4080. $dir = dir($dirname);
  4081. // A sanity check.
  4082. $is_object_dir = is_object($dir);
  4083. if ($is_object_dir) {
  4084. while (false !== $entry = $dir->read()) {
  4085. // Skip pointers.
  4086. if ($entry == '.' || $entry == '..') {
  4087. continue;
  4088. }
  4089. // Recurse.
  4090. if ($strict) {
  4091. $result = rmdirr("$dirname/$entry");
  4092. if ($result == false) {
  4093. $res = false;
  4094. break;
  4095. }
  4096. } else {
  4097. rmdirr("$dirname/$entry");
  4098. }
  4099. }
  4100. }
  4101. // Clean up.
  4102. if ($is_object_dir) {
  4103. $dir->close();
  4104. }
  4105. if ($delete_only_content_in_folder == false) {
  4106. $res = rmdir($dirname);
  4107. if ($res === false) {
  4108. error_log(__FILE__.' line '.__LINE__.': '.((bool)ini_get('track_errors') ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0);
  4109. }
  4110. }
  4111. return $res;
  4112. }
  4113. // TODO: This function is to be simplified. File access modes to be implemented.
  4114. /**
  4115. * function adapted from a php.net comment
  4116. * copy recursively a folder
  4117. * @param the source folder
  4118. * @param the dest folder
  4119. * @param an array of excluded file_name (without extension)
  4120. * @param copied_files the returned array of copied files
  4121. * @param string $source
  4122. * @param string $dest
  4123. */
  4124. function copyr($source, $dest, $exclude = array(), $copied_files = array()) {
  4125. if (empty($dest)) { return false; }
  4126. // Simple copy for a file
  4127. if (is_file($source)) {
  4128. $path_info = pathinfo($source);
  4129. if (!in_array($path_info['filename'], $exclude)) {
  4130. copy($source, $dest);
  4131. }
  4132. return true;
  4133. } elseif (!is_dir($source)) {
  4134. //then source is not a dir nor a file, return
  4135. return false;
  4136. }
  4137. // Make destination directory.
  4138. if (!is_dir($dest)) {
  4139. mkdir($dest, api_get_permissions_for_new_directories());
  4140. }
  4141. // Loop through the folder.
  4142. $dir = dir($source);
  4143. while (false !== $entry = $dir->read()) {
  4144. // Skip pointers
  4145. if ($entry == '.' || $entry == '..') {
  4146. continue;
  4147. }
  4148. // Deep copy directories.
  4149. if ($dest !== "$source/$entry") {
  4150. $files = copyr("$source/$entry", "$dest/$entry", $exclude, $copied_files);
  4151. }
  4152. }
  4153. // Clean up.
  4154. $dir->close();
  4155. return true;
  4156. }
  4157. // TODO: Using DIRECTORY_SEPARATOR is not recommended, this is an obsolete approach. Documentation header to be added here.
  4158. /**
  4159. * @param string $pathname
  4160. * @param string $base_path_document
  4161. * @param integer $session_id
  4162. */
  4163. function copy_folder_course_session(
  4164. $pathname,
  4165. $base_path_document,
  4166. $session_id,
  4167. $course_info,
  4168. $document,
  4169. $source_course_id
  4170. ) {
  4171. $table = Database :: get_course_table(TABLE_DOCUMENT);
  4172. $session_id = intval($session_id);
  4173. $source_course_id = intval($source_course_id);
  4174. // Check whether directory already exists.
  4175. if (is_dir($pathname) || empty($pathname)) {
  4176. return true;
  4177. }
  4178. // Ensure that a file with the same name does not already exist.
  4179. if (is_file($pathname)) {
  4180. trigger_error('copy_folder_course_session(): File exists', E_USER_WARNING);
  4181. return false;
  4182. }
  4183. $course_id = $course_info['real_id'];
  4184. $folders = explode(DIRECTORY_SEPARATOR,str_replace($base_path_document.DIRECTORY_SEPARATOR,'',$pathname));
  4185. $new_pathname = $base_path_document;
  4186. $path = '';
  4187. foreach ($folders as $folder) {
  4188. $new_pathname .= DIRECTORY_SEPARATOR.$folder;
  4189. $path .= DIRECTORY_SEPARATOR.$folder;
  4190. if (!file_exists($new_pathname)) {
  4191. $path = Database::escape_string($path);
  4192. $sql = "SELECT * FROM $table
  4193. WHERE
  4194. c_id = $source_course_id AND
  4195. path = '$path' AND
  4196. filetype = 'folder' AND
  4197. session_id = '$session_id'";
  4198. $rs1 = Database::query($sql);
  4199. $num_rows = Database::num_rows($rs1);
  4200. if ($num_rows == 0) {
  4201. mkdir($new_pathname, api_get_permissions_for_new_directories());
  4202. // Insert new folder with destination session_id.
  4203. $params = [
  4204. 'c_id' => $course_id,
  4205. 'path' => $path,
  4206. 'comment' => $document->comment,
  4207. 'title' => basename($new_pathname),
  4208. 'filetype' => 'folder',
  4209. 'size' => '0',
  4210. 'session_id' => $session_id
  4211. ];
  4212. $document_id = Database::insert($table, $params);
  4213. if ($document_id) {
  4214. $sql = "UPDATE $table SET id = iid WHERE iid = $document_id";
  4215. Database::query($sql);
  4216. api_item_property_update(
  4217. $course_info,
  4218. TOOL_DOCUMENT,
  4219. $document_id,
  4220. 'FolderCreated',
  4221. api_get_user_id(),
  4222. 0,
  4223. 0,
  4224. null,
  4225. null,
  4226. $session_id
  4227. );
  4228. }
  4229. }
  4230. }
  4231. } // en foreach
  4232. }
  4233. // TODO: chmodr() is a better name. Some corrections are needed. Documentation header to be added here.
  4234. /**
  4235. * @param string $path
  4236. */
  4237. function api_chmod_R($path, $filemode) {
  4238. if (!is_dir($path)) {
  4239. return chmod($path, $filemode);
  4240. }
  4241. $handler = opendir($path);
  4242. while ($file = readdir($handler)) {
  4243. if ($file != '.' && $file != '..') {
  4244. $fullpath = "$path/$file";
  4245. if (!is_dir($fullpath)) {
  4246. if (!chmod($fullpath, $filemode)) {
  4247. return false;
  4248. }
  4249. } else {
  4250. if (!api_chmod_R($fullpath, $filemode)) {
  4251. return false;
  4252. }
  4253. }
  4254. }
  4255. }
  4256. closedir($handler);
  4257. return chmod($path, $filemode);
  4258. }
  4259. // TODO: Where the following function has been copy/pased from? There is no information about author and license. Style, coding conventions...
  4260. /**
  4261. * Parse info file format. (e.g: file.info)
  4262. *
  4263. * Files should use an ini-like format to specify values.
  4264. * White-space generally doesn't matter, except inside values.
  4265. * e.g.
  4266. *
  4267. * @verbatim
  4268. * key = value
  4269. * key = "value"
  4270. * key = 'value'
  4271. * key = "multi-line
  4272. *
  4273. * value"
  4274. * key = 'multi-line
  4275. *
  4276. * value'
  4277. * key
  4278. * =
  4279. * 'value'
  4280. * @endverbatim
  4281. *
  4282. * Arrays are created using a GET-like syntax:
  4283. *
  4284. * @verbatim
  4285. * key[] = "numeric array"
  4286. * key[index] = "associative array"
  4287. * key[index][] = "nested numeric array"
  4288. * key[index][index] = "nested associative array"
  4289. * @endverbatim
  4290. *
  4291. * PHP constants are substituted in, but only when used as the entire value:
  4292. *
  4293. * Comments should start with a semi-colon at the beginning of a line.
  4294. *
  4295. * This function is NOT for placing arbitrary module-specific settings. Use
  4296. * variable_get() and variable_set() for that.
  4297. *
  4298. * Information stored in the module.info file:
  4299. * - name: The real name of the module for display purposes.
  4300. * - description: A brief description of the module.
  4301. * - dependencies: An array of shortnames of other modules this module depends on.
  4302. * - package: The name of the package of modules this module belongs to.
  4303. *
  4304. * Example of .info file:
  4305. * <code>
  4306. * @verbatim
  4307. * name = Forum
  4308. * description = Enables threaded discussions about general topics.
  4309. * dependencies[] = taxonomy
  4310. * dependencies[] = comment
  4311. * package = Core - optional
  4312. * version = VERSION
  4313. * @endverbatim
  4314. * </code>
  4315. * @param string $filename
  4316. * The file we are parsing. Accepts file with relative or absolute path.
  4317. * @return
  4318. * The info array.
  4319. */
  4320. function api_parse_info_file($filename) {
  4321. $info = array();
  4322. if (!file_exists($filename)) {
  4323. return $info;
  4324. }
  4325. $data = file_get_contents($filename);
  4326. if (preg_match_all('
  4327. @^\s* # Start at the beginning of a line, ignoring leading whitespace
  4328. ((?:
  4329. [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
  4330. \[[^\[\]]*\] # unless they are balanced and not nested
  4331. )+?)
  4332. \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
  4333. (?:
  4334. ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
  4335. (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
  4336. ([^\r\n]*?) # Non-quoted string
  4337. )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
  4338. @msx', $data, $matches, PREG_SET_ORDER)) {
  4339. $key = $value1 = $value2 = $value3 = '';
  4340. foreach ($matches as $match) {
  4341. // Fetch the key and value string.
  4342. $i = 0;
  4343. foreach (array('key', 'value1', 'value2', 'value3') as $var) {
  4344. $$var = isset($match[++$i]) ? $match[$i] : '';
  4345. }
  4346. $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
  4347. // Parse array syntax.
  4348. $keys = preg_split('/\]?\[/', rtrim($key, ']'));
  4349. $last = array_pop($keys);
  4350. $parent = &$info;
  4351. // Create nested arrays.
  4352. foreach ($keys as $key) {
  4353. if ($key == '') {
  4354. $key = count($parent);
  4355. }
  4356. if (!isset($parent[$key]) || !is_array($parent[$key])) {
  4357. $parent[$key] = array();
  4358. }
  4359. $parent = &$parent[$key];
  4360. }
  4361. // Handle PHP constants.
  4362. if (defined($value)) {
  4363. $value = constant($value);
  4364. }
  4365. // Insert actual value.
  4366. if ($last == '') {
  4367. $last = count($parent);
  4368. }
  4369. $parent[$last] = $value;
  4370. }
  4371. }
  4372. return $info;
  4373. }
  4374. /**
  4375. * Gets Chamilo version from the configuration files
  4376. * @return string A string of type "1.8.4", or an empty string if the version could not be found
  4377. */
  4378. function api_get_version()
  4379. {
  4380. return (string) api_get_configuration_value('system_version');
  4381. }
  4382. /**
  4383. * Gets the software name (the name/brand of the Chamilo-based customized system)
  4384. * @return string
  4385. */
  4386. function api_get_software_name() {
  4387. $name = api_get_configuration_value('software_name');
  4388. if (!empty($name)) {
  4389. return $name;
  4390. } else {
  4391. return 'Chamilo';
  4392. }
  4393. }
  4394. /**
  4395. * Checks whether status given in parameter exists in the platform
  4396. * @param mixed the status (can be either int either string)
  4397. * @return boolean if the status exists, else returns false
  4398. */
  4399. function api_status_exists($status_asked) {
  4400. global $_status_list;
  4401. return in_array($status_asked, $_status_list) ? true : isset($_status_list[$status_asked]);
  4402. }
  4403. /**
  4404. * Checks whether status given in parameter exists in the platform. The function
  4405. * returns the status ID or false if it does not exist, but given the fact there
  4406. * is no "0" status, the return value can be checked against
  4407. * if(api_status_key()) to know if it exists.
  4408. * @param mixed The status (can be either int or string)
  4409. * @return mixed Status ID if exists, false otherwise
  4410. */
  4411. function api_status_key($status) {
  4412. global $_status_list;
  4413. return isset($_status_list[$status]) ? $status : array_search($status, $_status_list);
  4414. }
  4415. /**
  4416. * Gets the status langvars list
  4417. * @return string[] the list of status with their translations
  4418. */
  4419. function api_get_status_langvars() {
  4420. return array(
  4421. COURSEMANAGER => get_lang('Teacher', ''),
  4422. SESSIONADMIN => get_lang('SessionsAdmin', ''),
  4423. DRH => get_lang('Drh', ''),
  4424. STUDENT => get_lang('Student', ''),
  4425. ANONYMOUS => get_lang('Anonymous', ''),
  4426. STUDENT_BOSS => get_lang('RoleStudentBoss', ''),
  4427. INVITEE => get_lang('Invited'),
  4428. );
  4429. }
  4430. /**
  4431. * The function that retrieves all the possible settings for a certain config setting
  4432. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4433. */
  4434. function api_get_settings_options($var) {
  4435. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  4436. $var = Database::escape_string($var);
  4437. $sql = "SELECT * FROM $table_settings_options
  4438. WHERE variable = '$var'
  4439. ORDER BY id";
  4440. $result = Database::query($sql);
  4441. $settings_options_array = array();
  4442. while ($row = Database::fetch_array($result, 'ASSOC')) {
  4443. $settings_options_array[] = $row;
  4444. }
  4445. return $settings_options_array;
  4446. }
  4447. /**
  4448. * @param array $params
  4449. */
  4450. function api_set_setting_option($params) {
  4451. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  4452. if (empty($params['id'])) {
  4453. Database::insert($table, $params);
  4454. } else {
  4455. Database::update($table, $params, array('id = ? '=> $params['id']));
  4456. }
  4457. }
  4458. /**
  4459. * @param array $params
  4460. */
  4461. function api_set_setting_simple($params) {
  4462. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  4463. $url_id = api_get_current_access_url_id();
  4464. if (empty($params['id'])) {
  4465. $params['access_url'] = $url_id;
  4466. Database::insert($table, $params);
  4467. } else {
  4468. Database::update($table, $params, array('id = ? '=> array($params['id'])));
  4469. }
  4470. }
  4471. /**
  4472. * @param int $id
  4473. */
  4474. function api_delete_setting_option($id) {
  4475. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  4476. if (!empty($id)) {
  4477. Database::delete($table, array('id = ? '=> $id));
  4478. }
  4479. }
  4480. /**
  4481. * Sets a platform configuration setting to a given value
  4482. * @param string The variable we want to update
  4483. * @param string The value we want to record
  4484. * @param string The sub-variable if any (in most cases, this will remain null)
  4485. * @param string The category if any (in most cases, this will remain null)
  4486. * @param int The access_url for which this parameter is valid
  4487. * @param string $cat
  4488. */
  4489. function api_set_setting($var, $value, $subvar = null, $cat = null, $access_url = 1)
  4490. {
  4491. if (empty($var)) {
  4492. return false;
  4493. }
  4494. $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  4495. $var = Database::escape_string($var);
  4496. $value = Database::escape_string($value);
  4497. $access_url = (int)$access_url;
  4498. if (empty($access_url)) { $access_url = 1; }
  4499. $select = "SELECT id FROM $t_settings WHERE variable = '$var' ";
  4500. if (!empty($subvar)) {
  4501. $subvar = Database::escape_string($subvar);
  4502. $select .= " AND subkey = '$subvar'";
  4503. }
  4504. if (!empty($cat)) {
  4505. $cat = Database::escape_string($cat);
  4506. $select .= " AND category = '$cat'";
  4507. }
  4508. if ($access_url > 1) {
  4509. $select .= " AND access_url = $access_url";
  4510. } else {
  4511. $select .= " AND access_url = 1 ";
  4512. }
  4513. $res = Database::query($select);
  4514. if (Database::num_rows($res) > 0) {
  4515. // Found item for this access_url.
  4516. $row = Database::fetch_array($res);
  4517. $sql = "UPDATE $t_settings SET selected_value = '$value'
  4518. WHERE id = ".$row['id'];
  4519. Database::query($sql);
  4520. } else {
  4521. // Item not found for this access_url, we have to check if it exist with access_url = 1
  4522. $select = "SELECT * FROM $t_settings
  4523. WHERE variable = '$var' AND access_url = 1 ";
  4524. // Just in case
  4525. if ($access_url == 1) {
  4526. if (!empty($subvar)) {
  4527. $select .= " AND subkey = '$subvar'";
  4528. }
  4529. if (!empty($cat)) {
  4530. $select .= " AND category = '$cat'";
  4531. }
  4532. $res = Database::query($select);
  4533. if (Database::num_rows($res) > 0) {
  4534. // We have a setting for access_url 1, but none for the current one, so create one.
  4535. $row = Database::fetch_array($res);
  4536. $insert = "INSERT INTO $t_settings (variable, subkey, type,category, selected_value, title, comment, scope, subkeytext, access_url)
  4537. VALUES
  4538. ('".$row['variable']."',".(!empty($row['subkey']) ? "'".$row['subkey']."'" : "NULL")."," .
  4539. "'".$row['type']."','".$row['category']."'," .
  4540. "'$value','".$row['title']."'," .
  4541. "".(!empty($row['comment']) ? "'".$row['comment']."'" : "NULL").",".(!empty($row['scope']) ? "'".$row['scope']."'" : "NULL")."," .
  4542. "".(!empty($row['subkeytext'])?"'".$row['subkeytext']."'":"NULL").",$access_url)";
  4543. Database::query($insert);
  4544. } else {
  4545. // Such a setting does not exist.
  4546. //error_log(__FILE__.':'.__LINE__.': Attempting to update setting '.$var.' ('.$subvar.') which does not exist at all', 0);
  4547. }
  4548. } else {
  4549. // Other access url.
  4550. if (!empty($subvar)) {
  4551. $select .= " AND subkey = '$subvar'";
  4552. }
  4553. if (!empty($cat)) {
  4554. $select .= " AND category = '$cat'";
  4555. }
  4556. $res = Database::query($select);
  4557. if (Database::num_rows($res) > 0) {
  4558. // We have a setting for access_url 1, but none for the current one, so create one.
  4559. $row = Database::fetch_array($res);
  4560. if ($row['access_url_changeable'] == 1) {
  4561. $insert = "INSERT INTO $t_settings (variable,subkey, type,category, selected_value,title, comment,scope, subkeytext,access_url, access_url_changeable) VALUES
  4562. ('".$row['variable']."',".
  4563. (!empty($row['subkey']) ? "'".$row['subkey']."'" : "NULL")."," .
  4564. "'".$row['type']."','".$row['category']."'," .
  4565. "'$value','".$row['title']."'," .
  4566. "".(!empty($row['comment']) ? "'".$row['comment']."'" : "NULL").",".
  4567. (!empty($row['scope']) ? "'".$row['scope']."'" : "NULL")."," .
  4568. "".(!empty($row['subkeytext']) ? "'".$row['subkeytext']."'" : "NULL").",$access_url,".$row['access_url_changeable'].")";
  4569. Database::query($insert);
  4570. }
  4571. } else { // Such a setting does not exist.
  4572. //error_log(__FILE__.':'.__LINE__.': Attempting to update setting '.$var.' ('.$subvar.') which does not exist at all. The access_url is: '.$access_url.' ',0);
  4573. }
  4574. }
  4575. }
  4576. }
  4577. /**
  4578. * Sets a whole category of settings to one specific value
  4579. * @param string Category
  4580. * @param string Value
  4581. * @param int Access URL. Optional. Defaults to 1
  4582. * @param array Optional array of filters on field type
  4583. * @param string $category
  4584. * @param string $value
  4585. */
  4586. function api_set_settings_category($category, $value = null, $access_url = 1, $fieldtype = array())
  4587. {
  4588. if (empty($category)) {
  4589. return false;
  4590. }
  4591. $category = Database::escape_string($category);
  4592. $t_s = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  4593. $access_url = (int) $access_url;
  4594. if (empty($access_url)) { $access_url = 1; }
  4595. if (isset($value)) {
  4596. $value = Database::escape_string($value);
  4597. $sql = "UPDATE $t_s SET selected_value = '$value'
  4598. WHERE category = '$category' AND access_url = $access_url";
  4599. if (is_array($fieldtype) && count($fieldtype)>0) {
  4600. $sql .= " AND ( ";
  4601. $i = 0;
  4602. foreach ($fieldtype as $type){
  4603. if ($i > 0) {
  4604. $sql .= ' OR ';
  4605. }
  4606. $type = Database::escape_string($type);
  4607. $sql .= " type='".$type."' ";
  4608. $i++;
  4609. }
  4610. $sql .= ")";
  4611. }
  4612. $res = Database::query($sql);
  4613. return $res !== false;
  4614. } else {
  4615. $sql = "UPDATE $t_s SET selected_value = NULL
  4616. WHERE category = '$category' AND access_url = $access_url";
  4617. if (is_array($fieldtype) && count($fieldtype)>0) {
  4618. $sql .= " AND ( ";
  4619. $i = 0;
  4620. foreach ($fieldtype as $type){
  4621. if ($i > 0) {
  4622. $sql .= ' OR ';
  4623. }
  4624. $type = Database::escape_string($type);
  4625. $sql .= " type='".$type."' ";
  4626. $i++;
  4627. }
  4628. $sql .= ")";
  4629. }
  4630. $res = Database::query($sql);
  4631. return $res !== false;
  4632. }
  4633. }
  4634. /**
  4635. * Gets all available access urls in an array (as in the database)
  4636. * @return array An array of database records
  4637. */
  4638. function api_get_access_urls($from = 0, $to = 1000000, $order = 'url', $direction = 'ASC')
  4639. {
  4640. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  4641. $from = (int) $from;
  4642. $to = (int) $to;
  4643. $order = Database::escape_string($order, null, false);
  4644. $direction = Database::escape_string($direction, null, false);
  4645. $sql = "SELECT id, url, description, active, created_by, tms
  4646. FROM $table
  4647. ORDER BY $order $direction
  4648. LIMIT $to OFFSET $from";
  4649. $res = Database::query($sql);
  4650. return Database::store_result($res);
  4651. }
  4652. /**
  4653. * Gets the access url info in an array
  4654. * @param int $id Id of the access url
  4655. * @param bool $returnDefault Set to false if you want the real URL if URL 1 is still 'http://localhost/'
  4656. * @return array All the info (url, description, active, created_by, tms)
  4657. * from the access_url table
  4658. * @author Julio Montoya
  4659. */
  4660. function api_get_access_url($id, $returnDefault = true)
  4661. {
  4662. $id = intval($id);
  4663. // Calling the Database:: library dont work this is handmade.
  4664. $table_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  4665. $sql = "SELECT url, description, active, created_by, tms
  4666. FROM $table_access_url WHERE id = '$id' ";
  4667. $res = Database::query($sql);
  4668. $result = @Database::fetch_array($res);
  4669. // If the result url is 'http://localhost/' (the default) and the root_web
  4670. // (=current url) is different, and the $id is = 1 (which might mean
  4671. // api_get_current_access_url_id() returned 1 by default), then return the
  4672. // root_web setting instead of the current URL
  4673. // This is provided as an option to avoid breaking the storage of URL-specific
  4674. // homepages in home/localhost/
  4675. if ($id === 1 && $returnDefault === false) {
  4676. $currentUrl = api_get_current_access_url_id();
  4677. // only do this if we are on the main URL (=1), otherwise we could get
  4678. // information on another URL instead of the one asked as parameter
  4679. if ($currentUrl === 1) {
  4680. $rootWeb = api_get_path(WEB_PATH);
  4681. $default = 'http://localhost/';
  4682. if ($result['url'] === $default && $rootWeb != $default) {
  4683. $result['url'] = $rootWeb;
  4684. }
  4685. }
  4686. }
  4687. return $result;
  4688. }
  4689. /**
  4690. * Gets all the current settings for a specific access url
  4691. * @param string The category, if any, that we want to get
  4692. * @param string Whether we want a simple list (display a category) or
  4693. * a grouped list (group by variable as in settings.php default). Values: 'list' or 'group'
  4694. * @param int Access URL's ID. Optional. Uses 1 by default, which is the unique URL
  4695. * @return array Array of database results for the current settings of the current access URL
  4696. */
  4697. function &api_get_settings($cat = null, $ordering = 'list', $access_url = 1, $url_changeable = 0)
  4698. {
  4699. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  4700. $access_url = (int) $access_url;
  4701. $where_condition = '';
  4702. if ($url_changeable == 1) {
  4703. $where_condition = " AND access_url_changeable= '1' ";
  4704. }
  4705. if (empty($access_url) || $access_url == -1) {
  4706. $access_url = 1;
  4707. }
  4708. $sql = "SELECT * FROM $table
  4709. WHERE access_url = $access_url $where_condition ";
  4710. if (!empty($cat)) {
  4711. $cat = Database::escape_string($cat);
  4712. $sql .= " AND category='$cat' ";
  4713. }
  4714. if ($ordering == 'group') {
  4715. $sql .= " ORDER BY id ASC";
  4716. } else {
  4717. $sql .= " ORDER BY 1,2 ASC";
  4718. }
  4719. $result = Database::query($sql);
  4720. $result = Database::store_result($result,'ASSOC');
  4721. return $result;
  4722. }
  4723. /**
  4724. * Sets a platform configuration setting to a given value
  4725. * @param string The value we want to record
  4726. * @param string The variable name we want to insert
  4727. * @param string The subkey for the variable we want to insert
  4728. * @param string The type for the variable we want to insert
  4729. * @param string The category for the variable we want to insert
  4730. * @param string The title
  4731. * @param string The comment
  4732. * @param string The scope
  4733. * @param string The subkey text
  4734. * @param int The access_url for which this parameter is valid
  4735. * @param int The changeability of this setting for non-master urls
  4736. * @param string $val
  4737. * @param string $var
  4738. * @param string $sk
  4739. * @param string $c
  4740. * @return boolean true on success, false on failure
  4741. */
  4742. function api_add_setting(
  4743. $val,
  4744. $var,
  4745. $sk = null,
  4746. $type = 'textfield',
  4747. $c = null,
  4748. $title = '',
  4749. $com = '',
  4750. $sc = null,
  4751. $skt = null,
  4752. $a = 1,
  4753. $v = 0
  4754. ) {
  4755. if (empty($var) || !isset($val)) { return false; }
  4756. $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  4757. $var = Database::escape_string($var);
  4758. $val = Database::escape_string($val);
  4759. $a = (int) $a;
  4760. if (empty($a)) { $a = 1; }
  4761. // Check if this variable doesn't exist already
  4762. $select = "SELECT id FROM $t_settings WHERE variable = '$var' ";
  4763. if (!empty($sk)) {
  4764. $sk = Database::escape_string($sk);
  4765. $select .= " AND subkey = '$sk'";
  4766. }
  4767. if ($a > 1) {
  4768. $select .= " AND access_url = $a";
  4769. } else {
  4770. $select .= " AND access_url = 1 ";
  4771. }
  4772. $res = Database::query($select);
  4773. if (Database::num_rows($res) > 0) { // Found item for this access_url.
  4774. $row = Database::fetch_array($res);
  4775. Database::update(
  4776. $t_settings,
  4777. array('selected_value' => $val),
  4778. array('id = ?' => array($row['id']))
  4779. );
  4780. return $row['id'];
  4781. }
  4782. // Item not found for this access_url, we have to check if the whole thing is missing
  4783. // (in which case we ignore the insert) or if there *is* a record but just for access_url = 1
  4784. $insert = "INSERT INTO $t_settings " .
  4785. "(variable,selected_value," .
  4786. "type,category," .
  4787. "subkey,title," .
  4788. "comment,scope," .
  4789. "subkeytext,access_url,access_url_changeable)" .
  4790. " VALUES ('$var','$val',";
  4791. if (isset($type)) {
  4792. $type = Database::escape_string($type);
  4793. $insert .= "'$type',";
  4794. } else {
  4795. $insert .= "NULL,";
  4796. }
  4797. if (isset($c)) { // Category
  4798. $c = Database::escape_string($c);
  4799. $insert .= "'$c',";
  4800. } else {
  4801. $insert .= "NULL,";
  4802. }
  4803. if (isset($sk)) { // Subkey
  4804. $sk = Database::escape_string($sk);
  4805. $insert .= "'$sk',";
  4806. } else {
  4807. $insert .= "NULL,";
  4808. }
  4809. if (isset($title)) { // Title
  4810. $title = Database::escape_string($title);
  4811. $insert .= "'$title',";
  4812. } else {
  4813. $insert .= "NULL,";
  4814. }
  4815. if (isset($com)) { // Comment
  4816. $com = Database::escape_string($com);
  4817. $insert .= "'$com',";
  4818. } else {
  4819. $insert .= "NULL,";
  4820. }
  4821. if (isset($sc)) { // Scope
  4822. $sc = Database::escape_string($sc);
  4823. $insert .= "'$sc',";
  4824. } else {
  4825. $insert .= "NULL,";
  4826. }
  4827. if (isset($skt)) { // Subkey text
  4828. $skt = Database::escape_string($skt);
  4829. $insert .= "'$skt',";
  4830. } else {
  4831. $insert .= "NULL,";
  4832. }
  4833. $insert .= "$a,$v)";
  4834. $res = Database::query($insert);
  4835. return $res;
  4836. }
  4837. /**
  4838. * Checks wether a user can or can't view the contents of a course.
  4839. *
  4840. * @param int $userid User id or NULL to get it from $_SESSION
  4841. * @param int $cid Course id to check whether the user is allowed.
  4842. * @return bool
  4843. */
  4844. function api_is_course_visible_for_user($userid = null, $cid = null) {
  4845. if ($userid === null) {
  4846. $userid = api_get_user_id();
  4847. }
  4848. if (empty($userid) || strval(intval($userid)) != $userid) {
  4849. if (api_is_anonymous()) {
  4850. $userid = api_get_anonymous_id();
  4851. } else {
  4852. return false;
  4853. }
  4854. }
  4855. $cid = Database::escape_string($cid);
  4856. $courseInfo = api_get_course_info($cid);
  4857. $courseId = $courseInfo['real_id'];
  4858. $is_platformAdmin = api_is_platform_admin();
  4859. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  4860. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  4861. $sql = "SELECT
  4862. $course_table.category_code,
  4863. $course_table.visibility,
  4864. $course_table.code,
  4865. $course_cat_table.code
  4866. FROM $course_table
  4867. LEFT JOIN $course_cat_table
  4868. ON $course_table.category_code = $course_cat_table.code
  4869. WHERE
  4870. $course_table.code = '$cid'
  4871. LIMIT 1";
  4872. $result = Database::query($sql);
  4873. if (Database::num_rows($result) > 0) {
  4874. $visibility = Database::fetch_array($result);
  4875. $visibility = $visibility['visibility'];
  4876. } else {
  4877. $visibility = 0;
  4878. }
  4879. // Shortcut permissions in case the visibility is "open to the world".
  4880. if ($visibility === COURSE_VISIBILITY_OPEN_WORLD) {
  4881. return true;
  4882. }
  4883. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  4884. $sql = "SELECT
  4885. is_tutor, status
  4886. FROM $tbl_course_user
  4887. WHERE
  4888. user_id = '$userid' AND
  4889. relation_type <> '".COURSE_RELATION_TYPE_RRHH."' AND
  4890. c_id = $courseId
  4891. LIMIT 1";
  4892. $result = Database::query($sql);
  4893. if (Database::num_rows($result) > 0) {
  4894. // This user has got a recorded state for this course.
  4895. $cuData = Database::fetch_array($result);
  4896. $is_courseMember = true;
  4897. $is_courseTutor = ($cuData['is_tutor'] == 1);
  4898. $is_courseAdmin = ($cuData['status'] == 1);
  4899. }
  4900. if (!$is_courseAdmin) {
  4901. // This user has no status related to this course.
  4902. // Is it the session coach or the session admin?
  4903. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  4904. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  4905. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  4906. $sql = "SELECT
  4907. session.id_coach, session_admin_id, session.id
  4908. FROM
  4909. $tbl_session as session
  4910. INNER JOIN $tbl_session_course
  4911. ON session_rel_course.session_id = session.id
  4912. AND session_rel_course.c_id = '$courseId'
  4913. LIMIT 1";
  4914. $result = Database::query($sql);
  4915. $row = Database::store_result($result);
  4916. if ($row[0]['id_coach'] == $userid) {
  4917. $is_courseMember = true;
  4918. $is_courseTutor = true;
  4919. $is_courseAdmin = false;
  4920. $is_courseCoach = true;
  4921. $is_sessionAdmin = false;
  4922. }
  4923. elseif ($row[0]['session_admin_id'] == $userid) {
  4924. $is_courseMember = false;
  4925. $is_courseTutor = false;
  4926. $is_courseAdmin = false;
  4927. $is_courseCoach = false;
  4928. $is_sessionAdmin = true;
  4929. } else {
  4930. // Check if the current user is the course coach.
  4931. $sql = "SELECT 1
  4932. FROM $tbl_session_course
  4933. WHERE session_rel_course.c_id = '$courseId'
  4934. AND session_rel_course.id_coach = '$userid'
  4935. LIMIT 1";
  4936. $result = Database::query($sql);
  4937. //if ($row = Database::fetch_array($result)) {
  4938. if (Database::num_rows($result) > 0 ) {
  4939. $is_courseMember = true;
  4940. $is_courseTutor = true;
  4941. $is_courseCoach = true;
  4942. $is_sessionAdmin = false;
  4943. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  4944. $sql = "SELECT status FROM $tbl_user
  4945. WHERE user_id = $userid
  4946. LIMIT 1";
  4947. $result = Database::query($sql);
  4948. if (Database::result($result, 0, 0) == 1) {
  4949. $is_courseAdmin = true;
  4950. } else {
  4951. $is_courseAdmin = false;
  4952. }
  4953. } else {
  4954. // Check if the user is a student is this session.
  4955. $sql = "SELECT id
  4956. FROM $tbl_session_course_user
  4957. WHERE
  4958. user_id = '$userid' AND
  4959. c_id = '$courseId'
  4960. LIMIT 1";
  4961. if (Database::num_rows($result) > 0) {
  4962. // This user haa got a recorded state for this course.
  4963. while ($row = Database::fetch_array($result)) {
  4964. $is_courseMember = true;
  4965. $is_courseTutor = false;
  4966. $is_courseAdmin = false;
  4967. $is_sessionAdmin = false;
  4968. }
  4969. }
  4970. }
  4971. }
  4972. }
  4973. switch ($visibility) {
  4974. case COURSE_VISIBILITY_OPEN_WORLD:
  4975. return true;
  4976. case COURSE_VISIBILITY_OPEN_PLATFORM:
  4977. return isset($userid);
  4978. case COURSE_VISIBILITY_REGISTERED:
  4979. case COURSE_VISIBILITY_CLOSED:
  4980. return $is_platformAdmin || $is_courseMember || $is_courseAdmin;
  4981. case COURSE_VISIBILITY_HIDDEN:
  4982. return $is_platformAdmin;
  4983. }
  4984. return false;
  4985. }
  4986. /**
  4987. * Returns whether an element (forum, message, survey ...) belongs to a session or not
  4988. * @param String the tool of the element
  4989. * @param int the element id in database
  4990. * @param int the session_id to compare with element session id
  4991. * @param string $tool
  4992. * @return boolean true if the element is in the session, false else
  4993. */
  4994. function api_is_element_in_the_session($tool, $element_id, $session_id = null) {
  4995. if (is_null($session_id)) {
  4996. $session_id = api_get_session_id();
  4997. }
  4998. // Get information to build query depending of the tool.
  4999. switch ($tool) {
  5000. case TOOL_SURVEY:
  5001. $table_tool = Database::get_course_table(TABLE_SURVEY);
  5002. $key_field = 'survey_id';
  5003. break;
  5004. case TOOL_ANNOUNCEMENT:
  5005. $table_tool = Database::get_course_table(TABLE_ANNOUNCEMENT);
  5006. $key_field = 'id';
  5007. break;
  5008. case TOOL_AGENDA:
  5009. $table_tool = Database::get_course_table(TABLE_AGENDA);
  5010. $key_field = 'id';
  5011. break;
  5012. case TOOL_GROUP:
  5013. $table_tool = Database::get_course_table(TABLE_GROUP);
  5014. $key_field = 'id';
  5015. break;
  5016. default:
  5017. return false;
  5018. }
  5019. $course_id = api_get_course_int_id();
  5020. $sql = "SELECT session_id
  5021. FROM $table_tool
  5022. WHERE c_id = $course_id AND $key_field = ".intval($element_id);
  5023. $rs = Database::query($sql);
  5024. if ($element_session_id = Database::result($rs, 0, 0)) {
  5025. if ($element_session_id == intval($session_id)) {
  5026. // The element belongs to the session.
  5027. return true;
  5028. }
  5029. }
  5030. return false;
  5031. }
  5032. /**
  5033. * Replaces "forbidden" characters in a filename string.
  5034. *
  5035. * @param string $filename
  5036. * @param bool $treat_spaces_as_hyphens
  5037. *
  5038. * @return string
  5039. */
  5040. function api_replace_dangerous_char($filename, $treat_spaces_as_hyphens = true)
  5041. {
  5042. return URLify::filter($filename, 250, '', true, true, false, false, $treat_spaces_as_hyphens);
  5043. }
  5044. /**
  5045. * Fixes the $_SERVER['REQUEST_URI'] that is empty in IIS6.
  5046. * @author Ivan Tcholakov, 28-JUN-2006.
  5047. */
  5048. function api_request_uri() {
  5049. if (!empty($_SERVER['REQUEST_URI'])) {
  5050. return $_SERVER['REQUEST_URI'];
  5051. }
  5052. $uri = $_SERVER['SCRIPT_NAME'];
  5053. if (!empty($_SERVER['QUERY_STRING'])) {
  5054. $uri .= '?'.$_SERVER['QUERY_STRING'];
  5055. }
  5056. $_SERVER['REQUEST_URI'] = $uri;
  5057. return $uri;
  5058. }
  5059. /** Gets the current access_url id of the Chamilo Platform
  5060. * @author Julio Montoya <gugli100@gmail.com>
  5061. * @return int access_url_id of the current Chamilo Installation
  5062. */
  5063. function api_get_current_access_url_id() {
  5064. $access_url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  5065. $path = Database::escape_string(api_get_path(WEB_PATH));
  5066. $sql = "SELECT id FROM $access_url_table WHERE url = '".$path."'";
  5067. $result = Database::query($sql);
  5068. if (Database::num_rows($result) > 0) {
  5069. $access_url_id = Database::result($result, 0, 0);
  5070. return $access_url_id;
  5071. }
  5072. //if the url in WEB_PATH was not found, it can only mean that there is
  5073. // either a configuration problem or the first URL has not been defined yet
  5074. // (by default it is http://localhost/). Thus the more sensible thing we can
  5075. // do is return 1 (the main URL) as the user cannot hack this value anyway
  5076. return 1;
  5077. }
  5078. /**
  5079. * Gets the registered urls from a given user id
  5080. * @author Julio Montoya <gugli100@gmail.com>
  5081. *
  5082. * @return array
  5083. */
  5084. function api_get_access_url_from_user($user_id) {
  5085. $user_id = intval($user_id);
  5086. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  5087. $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  5088. $sql = "SELECT access_url_id
  5089. FROM $table_url_rel_user url_rel_user
  5090. INNER JOIN $table_url u
  5091. ON (url_rel_user.access_url_id = u.id)
  5092. WHERE user_id = ".intval($user_id);
  5093. $result = Database::query($sql);
  5094. $list = array();
  5095. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5096. $list[] = $row['access_url_id'];
  5097. }
  5098. return $list;
  5099. }
  5100. /**
  5101. * Gets the status of a user in a course
  5102. * @param int $user_id
  5103. * @param int $courseId
  5104. * @return int user status
  5105. */
  5106. function api_get_status_of_user_in_course($user_id, $courseId)
  5107. {
  5108. $tbl_rel_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  5109. if (!empty($user_id) && !empty($courseId)) {
  5110. $user_id = intval($user_id);
  5111. $courseId = intval($courseId);
  5112. $sql = 'SELECT status
  5113. FROM '.$tbl_rel_course_user.'
  5114. WHERE user_id='.$user_id.' AND c_id = '.$courseId;
  5115. $result = Database::query($sql);
  5116. $row_status = Database::fetch_array($result, 'ASSOC');
  5117. return $row_status['status'];
  5118. } else {
  5119. return 0;
  5120. }
  5121. }
  5122. /**
  5123. * Checks whether the curent user is in a group or not.
  5124. *
  5125. * @param string The group id - optional (takes it from session if not given)
  5126. * @param string The course code - optional (no additional check by course if course code is not given)
  5127. * @return boolean
  5128. * @author Ivan Tcholakov
  5129. */
  5130. function api_is_in_group($groupIdParam = null, $courseCodeParam = null)
  5131. {
  5132. if (!empty($courseCodeParam)) {
  5133. $courseCode = api_get_course_id();
  5134. if (!empty($courseCode)) {
  5135. if ($courseCodeParam != $courseCode) {
  5136. return false;
  5137. }
  5138. } else {
  5139. return false;
  5140. }
  5141. }
  5142. $groupId = api_get_group_id();
  5143. if (isset($groupId) && $groupId != '') {
  5144. if (!empty($groupIdParam)) {
  5145. return $groupIdParam == $groupId;
  5146. } else {
  5147. return true;
  5148. }
  5149. }
  5150. return false;
  5151. }
  5152. /**
  5153. * Checks whether a secret key is valid
  5154. * @param string $original_key_secret - secret key from (webservice) client
  5155. * @param string $security_key - security key from Chamilo
  5156. * @return boolean - true if secret key is valid, false otherwise
  5157. */
  5158. function api_is_valid_secret_key($original_key_secret, $security_key)
  5159. {
  5160. return $original_key_secret == sha1($security_key);
  5161. }
  5162. /**
  5163. * Checks whether a user is into course
  5164. * @param int $course_id - the course id
  5165. * @param int $user_id - the user id
  5166. */
  5167. function api_is_user_of_course($course_id, $user_id) {
  5168. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  5169. $sql = 'SELECT user_id FROM '.$tbl_course_rel_user.'
  5170. WHERE
  5171. c_id ="'.intval($course_id).'" AND
  5172. user_id = "'.intval($user_id).'" AND
  5173. relation_type <> '.COURSE_RELATION_TYPE_RRHH.' ';
  5174. $result = Database::query($sql);
  5175. return Database::num_rows($result) == 1;
  5176. }
  5177. /**
  5178. * Checks whether the server's operating system is Windows (TM).
  5179. * @return boolean - true if the operating system is Windows, false otherwise
  5180. */
  5181. function api_is_windows_os() {
  5182. if (function_exists('php_uname')) {
  5183. // php_uname() exists as of PHP 4.0.2, according to the documentation.
  5184. // We expect that this function will always work for Chamilo 1.8.x.
  5185. $os = php_uname();
  5186. }
  5187. // The following methods are not needed, but let them stay, just in case.
  5188. elseif (isset($_ENV['OS'])) {
  5189. // Sometimes $_ENV['OS'] may not be present (bugs?)
  5190. $os = $_ENV['OS'];
  5191. }
  5192. elseif (defined('PHP_OS')) {
  5193. // PHP_OS means on which OS PHP was compiled, this is why
  5194. // using PHP_OS is the last choice for detection.
  5195. $os = PHP_OS;
  5196. } else {
  5197. return false;
  5198. }
  5199. return strtolower(substr((string)$os, 0, 3 )) == 'win';
  5200. }
  5201. /**
  5202. * This function informs whether the sent request is XMLHttpRequest
  5203. */
  5204. function api_is_xml_http_request() {
  5205. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  5206. }
  5207. /**
  5208. * This wrapper function has been implemented for avoiding some known problems about the function getimagesize().
  5209. * @link http://php.net/manual/en/function.getimagesize.php
  5210. * @link http://www.dokeos.com/forum/viewtopic.php?t=12345
  5211. * @link http://www.dokeos.com/forum/viewtopic.php?t=16355
  5212. * @return integer
  5213. */
  5214. function api_getimagesize($path) {
  5215. $image = new Image($path);
  5216. return $image->get_image_size();
  5217. }
  5218. /**
  5219. * This function resizes an image, with preserving its proportions (or aspect ratio).
  5220. * @author Ivan Tcholakov, MAY-2009.
  5221. * @param int $image System path or URL of the image
  5222. * @param int $target_width Targeted width
  5223. * @param int $target_height Targeted height
  5224. * @return array Calculated new width and height
  5225. */
  5226. function api_resize_image($image, $target_width, $target_height) {
  5227. $image_properties = api_getimagesize($image);
  5228. return api_calculate_image_size($image_properties['width'], $image_properties['height'], $target_width, $target_height);
  5229. }
  5230. /**
  5231. * This function calculates new image size, with preserving image's proportions (or aspect ratio).
  5232. * @author Ivan Tcholakov, MAY-2009.
  5233. * @author The initial idea has been taken from code by Patrick Cool, MAY-2004.
  5234. * @param int $image_width Initial width
  5235. * @param int $image_height Initial height
  5236. * @param int $target_width Targeted width
  5237. * @param int $target_height Targeted height
  5238. * @return array Calculated new width and height
  5239. */
  5240. function api_calculate_image_size($image_width, $image_height, $target_width, $target_height) {
  5241. // Only maths is here.
  5242. $result = array('width' => $image_width, 'height' => $image_height);
  5243. if ($image_width <= 0 || $image_height <= 0) {
  5244. return $result;
  5245. }
  5246. $resize_factor_width = $target_width / $image_width;
  5247. $resize_factor_height = $target_height / $image_height;
  5248. $delta_width = $target_width - $image_width * $resize_factor_height;
  5249. $delta_height = $target_height - $image_height * $resize_factor_width;
  5250. if ($delta_width > $delta_height) {
  5251. $result['width'] = ceil($image_width * $resize_factor_height);
  5252. $result['height'] = ceil($image_height * $resize_factor_height);
  5253. }
  5254. elseif ($delta_width < $delta_height) {
  5255. $result['width'] = ceil($image_width * $resize_factor_width);
  5256. $result['height'] = ceil($image_height * $resize_factor_width);
  5257. }
  5258. else {
  5259. $result['width'] = ceil($target_width);
  5260. $result['height'] = ceil($target_height);
  5261. }
  5262. return $result;
  5263. }
  5264. /**
  5265. * Returns a list of Chamilo's tools or
  5266. * checks whether a given identificator is a valid Chamilo's tool.
  5267. * @author Isaac flores paz
  5268. * @param string The tool name to filter
  5269. * @return mixed Filtered string or array
  5270. */
  5271. function api_get_tools_lists($my_tool = null) {
  5272. $tools_list = array(
  5273. TOOL_DOCUMENT,
  5274. TOOL_THUMBNAIL,
  5275. TOOL_HOTPOTATOES,
  5276. TOOL_CALENDAR_EVENT,
  5277. TOOL_LINK,
  5278. TOOL_COURSE_DESCRIPTION,
  5279. TOOL_SEARCH,
  5280. TOOL_LEARNPATH,
  5281. TOOL_ANNOUNCEMENT,
  5282. TOOL_FORUM,
  5283. TOOL_THREAD,
  5284. TOOL_POST,
  5285. TOOL_DROPBOX,
  5286. TOOL_QUIZ,
  5287. TOOL_USER,
  5288. TOOL_GROUP,
  5289. TOOL_BLOGS,
  5290. TOOL_CHAT,
  5291. TOOL_STUDENTPUBLICATION,
  5292. TOOL_TRACKING,
  5293. TOOL_HOMEPAGE_LINK,
  5294. TOOL_COURSE_SETTING,
  5295. TOOL_BACKUP,
  5296. TOOL_COPY_COURSE_CONTENT,
  5297. TOOL_RECYCLE_COURSE,
  5298. TOOL_COURSE_HOMEPAGE,
  5299. TOOL_COURSE_RIGHTS_OVERVIEW,
  5300. TOOL_UPLOAD,
  5301. TOOL_COURSE_MAINTENANCE,
  5302. TOOL_SURVEY,
  5303. TOOL_WIKI,
  5304. TOOL_GLOSSARY,
  5305. TOOL_GRADEBOOK,
  5306. TOOL_NOTEBOOK,
  5307. TOOL_ATTENDANCE,
  5308. TOOL_COURSE_PROGRESS,
  5309. );
  5310. if (empty($my_tool)) {
  5311. return $tools_list;
  5312. }
  5313. return in_array($my_tool, $tools_list) ? $my_tool : '';
  5314. }
  5315. /**
  5316. * Checks whether we already approved the last version term and condition
  5317. * @param int user id
  5318. * @return bool true if we pass false otherwise
  5319. */
  5320. function api_check_term_condition($user_id)
  5321. {
  5322. if (api_get_setting('registration.allow_terms_conditions') == 'true') {
  5323. //check if exists terms and conditions
  5324. if (LegalManager::count() == 0) {
  5325. return true;
  5326. }
  5327. $extraFieldValue = new ExtraFieldValue('user');
  5328. $data = $extraFieldValue->get_values_by_handler_and_field_variable(
  5329. $user_id,
  5330. 'legal_accept'
  5331. );
  5332. if (!empty($data) && isset($data['value'])) {
  5333. $rowv = $data['value'];
  5334. $user_conditions = explode(':', $rowv);
  5335. $version = $user_conditions[0];
  5336. $lang_id = $user_conditions[1];
  5337. $real_version = LegalManager::get_last_version($lang_id);
  5338. return $version >= $real_version;
  5339. }
  5340. return false;
  5341. }
  5342. return false;
  5343. }
  5344. /**
  5345. * Gets all information of a tool into course
  5346. * @param int The tool id
  5347. * @return array
  5348. */
  5349. function api_get_tool_information($tool_id) {
  5350. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  5351. $course_id = api_get_course_int_id();
  5352. $sql = "SELECT * FROM $t_tool WHERE c_id = $course_id AND iid = ".intval(
  5353. $tool_id
  5354. );
  5355. $rs = Database::query($sql);
  5356. return Database::fetch_array($rs);
  5357. }
  5358. /**
  5359. * Gets all information of a tool into course
  5360. * @param int The tool id
  5361. * @return array
  5362. */
  5363. function api_get_tool_information_by_name($name) {
  5364. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  5365. $course_id = api_get_course_int_id();
  5366. $sql = "SELECT * FROM $t_tool
  5367. WHERE c_id = $course_id AND name = '".Database::escape_string($name)."' ";
  5368. $rs = Database::query($sql);
  5369. return Database::fetch_array($rs, 'ASSOC');
  5370. }
  5371. /**
  5372. * Function used to protect a "global" admin script.
  5373. * The function blocks access when the user has no global platform admin rights.
  5374. * Global admins are the admins that are registered in the main.admin table
  5375. * AND the users who have access to the "principal" portal.
  5376. * That means that there is a record in the main.access_url_rel_user table
  5377. * with his user id and the access_url_id=1
  5378. *
  5379. * @author Julio Montoya
  5380. * @param integer $user_id
  5381. */
  5382. function api_is_global_platform_admin($user_id = null)
  5383. {
  5384. $user_id = intval($user_id);
  5385. if (empty($user_id)) {
  5386. $user_id = api_get_user_id();
  5387. }
  5388. if (api_is_platform_admin_by_id($user_id)) {
  5389. $urlList = api_get_access_url_from_user($user_id);
  5390. // The admin is registered in the first "main" site with access_url_id = 1
  5391. if (in_array(1, $urlList)) {
  5392. return true;
  5393. } else {
  5394. return false;
  5395. }
  5396. }
  5397. return false;
  5398. }
  5399. /**
  5400. * @param int $admin_id_to_check
  5401. * @param int $my_user_id
  5402. * @param bool $allow_session_admin
  5403. * @return bool
  5404. */
  5405. function api_global_admin_can_edit_admin($admin_id_to_check, $my_user_id = null, $allow_session_admin = false)
  5406. {
  5407. if (empty($my_user_id)) {
  5408. $my_user_id = api_get_user_id();
  5409. }
  5410. $iam_a_global_admin = api_is_global_platform_admin($my_user_id);
  5411. $user_is_global_admin = api_is_global_platform_admin($admin_id_to_check);
  5412. if ($iam_a_global_admin) {
  5413. // Global admin can edit everything
  5414. return true;
  5415. } else {
  5416. // If i'm a simple admin
  5417. $is_platform_admin = api_is_platform_admin_by_id($my_user_id);
  5418. if ($allow_session_admin) {
  5419. $is_platform_admin = api_is_platform_admin_by_id($my_user_id) || (api_get_user_status($my_user_id) == SESSIONADMIN);
  5420. }
  5421. if ($is_platform_admin) {
  5422. if ($user_is_global_admin) {
  5423. return false;
  5424. } else {
  5425. return true;
  5426. }
  5427. } else {
  5428. return false;
  5429. }
  5430. }
  5431. }
  5432. /**
  5433. * @param int $admin_id_to_check
  5434. * @param int $my_user_id
  5435. * @param bool $allow_session_admin
  5436. * @return boolean|null
  5437. */
  5438. function api_protect_super_admin($admin_id_to_check, $my_user_id = null, $allow_session_admin = false)
  5439. {
  5440. if (api_global_admin_can_edit_admin($admin_id_to_check, $my_user_id, $allow_session_admin)) {
  5441. return true;
  5442. } else {
  5443. api_not_allowed();
  5444. }
  5445. }
  5446. /**
  5447. * Function used to protect a global admin script.
  5448. * The function blocks access when the user has no global platform admin rights.
  5449. * See also the api_is_global_platform_admin() function wich defines who's a "global" admin
  5450. *
  5451. * @author Julio Montoya
  5452. */
  5453. function api_protect_global_admin_script() {
  5454. if (!api_is_global_platform_admin()) {
  5455. api_not_allowed();
  5456. return false;
  5457. }
  5458. return true;
  5459. }
  5460. /**
  5461. * Get active template
  5462. * @param string theme type (optional: default)
  5463. * @param string path absolute(abs) or relative(rel) (optional:rel)
  5464. * @return string actived template path
  5465. */
  5466. /*function api_get_template($path_type = 'rel') {
  5467. $path_types = array('rel', 'abs');
  5468. $template_path = '';
  5469. if (in_array($path_type, $path_types)) {
  5470. if ($path_type == 'rel') {
  5471. $template_path = api_get_path(SYS_TEMPLATE_PATH);
  5472. } else {
  5473. $template_path = api_get_path(WEB_TEMPLATE_PATH);
  5474. }
  5475. }
  5476. $actived_theme = 'default';
  5477. if (api_get_setting('active_template')) {
  5478. $actived_theme = api_get_setting('active_template');
  5479. }
  5480. $actived_theme_path = $template_path.$actived_theme.DIRECTORY_SEPARATOR;
  5481. return $actived_theme_path;
  5482. }*/
  5483. /**
  5484. * Check browser support for type files
  5485. * This function check if the users browser support a file format or
  5486. * return the current browser and major ver when $format=check_browser
  5487. * @param string $format
  5488. *
  5489. * @return bool or return text array if $format=check_browser
  5490. * @author Juan Carlos Raña Trabado
  5491. */
  5492. function api_browser_support($format = '')
  5493. {
  5494. $browser = new Browser();
  5495. $current_browser = $browser->getBrowser();
  5496. $a_versiontemp = explode('.', $browser->getVersion());
  5497. $current_majorver = $a_versiontemp[0];
  5498. // Native svg support
  5499. if ($format == 'svg') {
  5500. if (($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  5501. ($current_browser == 'Firefox' && $current_majorver > 1) ||
  5502. ($current_browser == 'Safari' && $current_majorver >= 4) ||
  5503. ($current_browser == 'Chrome' && $current_majorver >= 1) ||
  5504. ($current_browser == 'Opera' && $current_majorver >= 9)
  5505. ) {
  5506. return true;
  5507. } else {
  5508. return false;
  5509. }
  5510. } elseif ($format == 'pdf') {
  5511. //native pdf support
  5512. if ($current_browser == 'Chrome' && $current_majorver >= 6) {
  5513. return true;
  5514. } else {
  5515. return false;
  5516. }
  5517. } elseif ($format == 'tif' || $format == 'tiff') {
  5518. //native tif support
  5519. if ($current_browser == 'Safari' && $current_majorver >= 5) {
  5520. return true;
  5521. } else {
  5522. return false;
  5523. }
  5524. } elseif ($format == 'ogg' || $format == 'ogx' || $format == 'ogv' || $format == 'oga') {
  5525. //native ogg, ogv,oga support
  5526. if (($current_browser == 'Firefox' && $current_majorver >= 3) ||
  5527. ($current_browser == 'Chrome' && $current_majorver >= 3) ||
  5528. ($current_browser == 'Opera' && $current_majorver >= 9)) {
  5529. return true;
  5530. } else {
  5531. return false;
  5532. }
  5533. } elseif ($format == 'mpg' || $format == 'mpeg') {
  5534. //native mpg support
  5535. if (($current_browser == 'Safari' && $current_majorver >= 5)) {
  5536. return true;
  5537. } else {
  5538. return false;
  5539. }
  5540. } elseif ($format == 'mp4') {
  5541. //native mp4 support (TODO: Android, iPhone)
  5542. if ($current_browser == 'Android' || $current_browser == 'iPhone') {
  5543. return true;
  5544. } else {
  5545. return false;
  5546. }
  5547. } elseif ($format == 'mov') {
  5548. //native mov support( TODO:check iPhone)
  5549. if ($current_browser == 'Safari' && $current_majorver >= 5 || $current_browser == 'iPhone') {
  5550. return true;
  5551. } else {
  5552. return false;
  5553. }
  5554. } elseif ($format == 'avi') {
  5555. //native avi support
  5556. if ($current_browser == 'Safari' && $current_majorver >= 5) {
  5557. return true;
  5558. } else {
  5559. return false;
  5560. }
  5561. } elseif ($format == 'wmv') {
  5562. //native wmv support
  5563. if ($current_browser == 'Firefox' && $current_majorver >= 4) {
  5564. return true;
  5565. } else {
  5566. return false;
  5567. }
  5568. } elseif ($format == 'webm') {
  5569. //native webm support (TODO:check IE9, Chrome9, Android)
  5570. if (($current_browser == 'Firefox' && $current_majorver >= 4) ||
  5571. ($current_browser == 'Opera' && $current_majorver >= 9) ||
  5572. ($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  5573. ($current_browser == 'Chrome' && $current_majorver >= 9) ||
  5574. $current_browser == 'Android'
  5575. ) {
  5576. return true;
  5577. } else {
  5578. return false;
  5579. }
  5580. } elseif ($format == 'wav') {
  5581. //native wav support (only some codecs !)
  5582. if (($current_browser == 'Firefox' && $current_majorver >= 4) ||
  5583. ($current_browser == 'Safari' && $current_majorver >= 5) ||
  5584. ($current_browser == 'Opera' && $current_majorver >= 9) ||
  5585. ($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  5586. ($current_browser == 'Chrome' && $current_majorver > 9) ||
  5587. $current_browser == 'Android' ||
  5588. $current_browser == 'iPhone'
  5589. ) {
  5590. return true;
  5591. } else {
  5592. return false;
  5593. }
  5594. } elseif ($format == 'mid' || $format == 'kar') {
  5595. //native midi support (TODO:check Android)
  5596. if ($current_browser == 'Opera' && $current_majorver >= 9 || $current_browser == 'Android') {
  5597. return true;
  5598. } else {
  5599. return false;
  5600. }
  5601. } elseif ($format == 'wma') {
  5602. //native wma support
  5603. if ($current_browser == 'Firefox' && $current_majorver >= 4) {
  5604. return true;
  5605. } else {
  5606. return false;
  5607. }
  5608. } elseif ($format == 'au') {
  5609. //native au support
  5610. if ($current_browser == 'Safari' && $current_majorver >= 5) {
  5611. return true;
  5612. } else {
  5613. return false;
  5614. }
  5615. } elseif ($format == 'mp3') {
  5616. //native mp3 support (TODO:check Android, iPhone)
  5617. if (($current_browser == 'Safari' && $current_majorver >= 5) ||
  5618. ($current_browser == 'Chrome' && $current_majorver >= 6) ||
  5619. ($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  5620. $current_browser == 'Android' ||
  5621. $current_browser == 'iPhone' ||
  5622. $current_browser == 'Firefox'
  5623. ) {
  5624. return true;
  5625. } else {
  5626. return false;
  5627. }
  5628. } elseif ($format == "check_browser") {
  5629. $array_check_browser = array($current_browser, $current_majorver);
  5630. return $array_check_browser;
  5631. } else {
  5632. return false;
  5633. }
  5634. }
  5635. /**
  5636. * This function checks if exist path and file browscap.ini
  5637. * In order for this to work, your browscap configuration setting in php.ini
  5638. * must point to the correct location of the browscap.ini file on your system
  5639. * http://php.net/manual/en/function.get-browser.php
  5640. *
  5641. * @return bool
  5642. *
  5643. * @author Juan Carlos Raña Trabado
  5644. */
  5645. function api_check_browscap() {
  5646. $setting = ini_get('browscap');
  5647. if ($setting) {
  5648. $browser = get_browser($_SERVER['HTTP_USER_AGENT'], true);
  5649. if (strpos($setting, 'browscap.ini') && !empty($browser)) {
  5650. return true;
  5651. }
  5652. }
  5653. return false;
  5654. }
  5655. /**
  5656. * Returns the js header to include the jquery library
  5657. */
  5658. function api_get_jquery_js()
  5659. {
  5660. return api_get_js('components/jquery/dist/jquery.min.js');
  5661. }
  5662. /**
  5663. * Returns the <script> HTML tag
  5664. * @param string $file
  5665. * @param bool $getURL
  5666. * @return string
  5667. */
  5668. function api_get_js($file, $getURL = false)
  5669. {
  5670. $url = Container::getAsset()->getUrl("bundles/chamilocore/".$file);
  5671. if ($getURL) {
  5672. return $url;
  5673. }
  5674. return '<script type="text/javascript" src="'.$url.'"></script>'."\n";
  5675. }
  5676. /**
  5677. * Returns the <link> HTML tag
  5678. */
  5679. function api_get_css($file, $media = 'screen')
  5680. {
  5681. $url = Container::getAsset()->getUrl("bundles/chamilocore/".$file);
  5682. return '<link href="'.$url.'" rel="stylesheet" media="'.$media.'" type="text/css" />'."\n";
  5683. }
  5684. /**
  5685. * Returns the jquery path
  5686. * @return string
  5687. */
  5688. function api_get_jquery_web_path()
  5689. {
  5690. return Container::getAsset()->getUrl(
  5691. "bundles/chamilocore/components/jquery/dist/jquery.min.js"
  5692. );
  5693. }
  5694. /**
  5695. * @return string
  5696. */
  5697. function api_get_jquery_ui_js_web_path()
  5698. {
  5699. return Container::getAsset()->getUrl(
  5700. "bundles/chamilocore/components/jquery-ui/jquery-ui.min.js"
  5701. );
  5702. }
  5703. /**
  5704. * @return string
  5705. */
  5706. function api_get_jquery_ui_css_web_path()
  5707. {
  5708. return Container::getAsset()->getUrl(
  5709. "bundles/chamilocore/components/jquery-ui/themes/smoothness/jquery-ui.min.css"
  5710. );
  5711. }
  5712. /**
  5713. * Returns the jquery-ui library js headers
  5714. * @param bool add the jqgrid library
  5715. * @return string html tags
  5716. *
  5717. */
  5718. function api_get_jquery_ui_js($include_jqgrid = false) {
  5719. $libraries = array();
  5720. if ($include_jqgrid) {
  5721. $libraries[]='jqgrid';
  5722. }
  5723. return api_get_jquery_libraries_js($libraries);
  5724. }
  5725. function api_get_jqgrid_js() {
  5726. return api_get_jquery_libraries_js(array('jqgrid'));
  5727. }
  5728. /**
  5729. * Returns the jquery library js and css headers
  5730. *
  5731. * @param array list of jquery libraries supported jquery-ui, jqgrid
  5732. * @param bool add the jquery library
  5733. * @return string html tags
  5734. *
  5735. */
  5736. function api_get_jquery_libraries_js($libraries) {
  5737. $js = '';
  5738. //jqgrid js and css
  5739. if (in_array('jqgrid', $libraries)) {
  5740. $language = 'en';
  5741. $isoCode = strtolower(api_get_language_isocode());
  5742. // languages supported by jqgrid see files in main/inc/lib/javascript/jqgrid/js/i18n
  5743. $langs = array(
  5744. 'bg', 'bg1251', 'cat','cn','cs','da','de','el','en','es','fa','fi','fr','gl','he','hu','is','it','ja','nl','no','pl','pt-br','pt','ro','ru','sk','sr','sv','tr','ua'
  5745. );
  5746. if (in_array($isoCode, $langs)) {
  5747. $language = $isoCode;
  5748. }
  5749. $js .= api_get_css('components/jqgrid/css/ui.jqgrid.css');
  5750. $js .= api_get_js('components/jqgrid/js/minified/jquery.jqGrid.min.js');
  5751. $js .= api_get_js('components/jqgrid/js/i18n/grid.locale-'.$language.'.js');
  5752. }
  5753. // jquery datepicker
  5754. if (in_array('datepicker', $libraries)) {
  5755. $language = 'en-GB';
  5756. $isoCode = strtolower(api_get_language_isocode());
  5757. // languages supported by jqgrid see files in main/inc/lib/javascript/jqgrid/js/i18n
  5758. $datapicker_langs = array(
  5759. 'af', 'ar', 'ar-DZ', 'az', 'bg', 'bs', 'ca', 'cs', 'cy-GB', 'da', 'de', 'el', 'en-AU', 'en-GB', 'en-NZ', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'fr-CH', 'gl', 'he', 'hi', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kk', 'km', 'ko', 'lb', 'lt', 'lv', 'mk', 'ml', 'ms', 'nl', 'nl-BE', 'no', 'pl', 'pt', 'pt-BR', 'rm', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sr-SR', 'sv', 'ta', 'th', 'tj', 'tr', 'uk', 'vi', 'zh-CN', 'zh-HK', 'zh-TW'
  5760. );
  5761. if (in_array($isoCode, $datapicker_langs)) {
  5762. $language = $isoCode;
  5763. }
  5764. $js .= api_get_js('components/jquery-ui/jquery-ui-i18n.min.js');
  5765. $script = '<script>
  5766. $(function(){
  5767. $.datepicker.setDefaults($.datepicker.regional["'.$language.'"]);
  5768. $.datepicker.regional["local"] = $.datepicker.regional["'.$language.'"];
  5769. });
  5770. </script>
  5771. ';
  5772. $js .= $script;
  5773. }
  5774. return $js;
  5775. }
  5776. /**
  5777. * Returns the course's URL
  5778. *
  5779. * This function relies on api_get_course_info()
  5780. * @param string The course code - optional (takes it from session if not given)
  5781. * @param int The session id - optional (takes it from session if not given)
  5782. * @param integer $session_id
  5783. * @return string|null The URL of the course or null if something does not work
  5784. * @author Julio Montoya <gugli100@gmail.com>
  5785. */
  5786. function api_get_course_url($course_code = null, $session_id = null)
  5787. {
  5788. if (empty($course_code)) {
  5789. $course_info = api_get_course_info();
  5790. } else {
  5791. $course_info = api_get_course_info($course_code);
  5792. }
  5793. if (empty($session_id)) {
  5794. $session_url = '?id_session='.api_get_session_id();
  5795. } else {
  5796. $session_url = '?id_session='.intval($session_id);
  5797. }
  5798. /*
  5799. if (empty($group_id)) {
  5800. $group_url = '&gidReq='.api_get_group_id();
  5801. } else {
  5802. $group_url = '&gidReq='.intval($group_id);
  5803. }*/
  5804. if (!empty($course_info['path'])) {
  5805. return api_get_path(WEB_COURSE_PATH).$course_info['path'].'/index.php'.$session_url;
  5806. }
  5807. return null;
  5808. }
  5809. /**
  5810. *
  5811. * Check if the current portal has the $_configuration['multiple_access_urls'] parameter on
  5812. * @return bool true if multi site is enabled
  5813. *
  5814. * */
  5815. function api_get_multiple_access_url()
  5816. {
  5817. return api_get_configuration_value('multiple_access_urls');
  5818. }
  5819. /**
  5820. * @return bool
  5821. */
  5822. function api_is_multiple_url_enabled()
  5823. {
  5824. return api_get_multiple_access_url();
  5825. }
  5826. /**
  5827. * Returns a md5 unique id
  5828. * @todo add more parameters
  5829. */
  5830. function api_get_unique_id() {
  5831. $id = md5(time().uniqid().api_get_user_id().api_get_course_id().api_get_session_id());
  5832. return $id;
  5833. }
  5834. /**
  5835. * Get home path
  5836. * @return string
  5837. */
  5838. function api_get_home_path()
  5839. {
  5840. // FIX : Start the routing determination from central path definition
  5841. $home = api_get_path(SYS_HOME_PATH);
  5842. if (api_get_multiple_access_url()) {
  5843. $access_url_id = api_get_current_access_url_id();
  5844. $url_info = api_get_access_url($access_url_id);
  5845. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  5846. $clean_url = api_replace_dangerous_char($url);
  5847. $clean_url = str_replace('/', '-', $clean_url);
  5848. $clean_url .= '/';
  5849. if ($clean_url != 'localhost/') {
  5850. // means that the multiple URL was not well configured we don't rename the $home variable
  5851. return "{$home}{$clean_url}";
  5852. }
  5853. }
  5854. return $home;
  5855. }
  5856. /**
  5857. *
  5858. * @param int Course id
  5859. * @param int tool id: TOOL_QUIZ, TOOL_FORUM, TOOL_STUDENTPUBLICATION, TOOL_LEARNPATH
  5860. * @param int the item id (tool id, exercise id, lp id)
  5861. *
  5862. */
  5863. function api_resource_is_locked_by_gradebook($item_id, $link_type, $course_code = null) {
  5864. if (api_is_platform_admin()) {
  5865. return false;
  5866. }
  5867. if (api_get_setting('gradebook.gradebook_locking_enabled') == 'true') {
  5868. if (empty($course_code)) {
  5869. $course_code = api_get_course_id();
  5870. }
  5871. $em = Database::getManager();
  5872. $item_id = intval($item_id);
  5873. $link_type = intval($link_type);
  5874. $course_code = Database::escape_string($course_code);
  5875. $courseId = api_get_course_int_id($course_code);
  5876. $result = $em
  5877. ->getRepository('ChamiloCoreBundle:GradebookLink')
  5878. ->findBy([
  5879. 'locked' => 1,
  5880. 'refId' => $item_id,
  5881. 'type' => $link_type,
  5882. 'course' => $courseId
  5883. ]);
  5884. if (count($result)) {
  5885. return true;
  5886. }
  5887. }
  5888. return false;
  5889. }
  5890. /**
  5891. * Blocks a page if the item was added in a gradebook
  5892. *
  5893. * @param int exercise id, work id, thread id,
  5894. * @param int LINK_EXERCISE, LINK_STUDENTPUBLICATION, LINK_LEARNPATH LINK_FORUM_THREAD, LINK_ATTENDANCE
  5895. * see gradebook/lib/be/linkfactory
  5896. * @param string course code
  5897. * @return boolean
  5898. */
  5899. function api_block_course_item_locked_by_gradebook($item_id, $link_type, $course_code = null) {
  5900. if (api_is_platform_admin()) {
  5901. return false;
  5902. }
  5903. if (api_resource_is_locked_by_gradebook($item_id, $link_type, $course_code)) {
  5904. $message = Display::return_message(get_lang('ResourceLockedByGradebook'), 'warning');
  5905. api_not_allowed(true, $message);
  5906. }
  5907. }
  5908. /**
  5909. * Checks the PHP version installed is enough to run Chamilo
  5910. * @param string Include path (used to load the error page)
  5911. * @return void
  5912. */
  5913. function api_check_php_version($my_inc_path = null) {
  5914. if (!function_exists('version_compare') || version_compare( phpversion(), REQUIRED_PHP_VERSION, '<')) {
  5915. $global_error_code = 1;
  5916. // Incorrect PHP version
  5917. $global_page = $my_inc_path.'global_error_message.inc.php';
  5918. if (file_exists($global_page)) {
  5919. require $global_page;
  5920. }
  5921. exit;
  5922. }
  5923. }
  5924. /**
  5925. * Checks whether the Archive directory is present and writeable. If not,
  5926. * prints a warning message.
  5927. */
  5928. function api_check_archive_dir() {
  5929. if (is_dir(api_get_path(SYS_ARCHIVE_PATH)) && !is_writable(api_get_path(SYS_ARCHIVE_PATH))) {
  5930. $message = Display::return_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'),'warning');
  5931. api_not_allowed(true, $message);
  5932. }
  5933. }
  5934. /**
  5935. * Returns an array of global configuration settings which should be ignored
  5936. * when printing the configuration settings screens
  5937. * @return array Array of strings, each identifying one of the excluded settings
  5938. */
  5939. function api_get_locked_settings() {
  5940. return array(
  5941. 'server_type',
  5942. 'permanently_remove_deleted_files',
  5943. 'account_valid_duration',
  5944. 'service_ppt2lp',
  5945. 'wcag_anysurfer_public_pages',
  5946. 'upload_extensions_list_type',
  5947. 'upload_extensions_blacklist',
  5948. 'upload_extensions_whitelist',
  5949. 'upload_extensions_skip',
  5950. 'upload_extensions_replace_by',
  5951. 'hide_dltt_markup',
  5952. 'split_users_upload_directory',
  5953. 'permissions_for_new_directories',
  5954. 'permissions_for_new_files',
  5955. 'platform_charset',
  5956. 'ldap_description',
  5957. 'cas_activate',
  5958. 'cas_server',
  5959. 'cas_server_uri',
  5960. 'cas_port',
  5961. 'cas_protocol',
  5962. 'cas_add_user_activate',
  5963. 'update_user_info_cas_with_ldap',
  5964. 'languagePriority1',
  5965. 'languagePriority2',
  5966. 'languagePriority3',
  5967. 'languagePriority4',
  5968. 'login_is_email',
  5969. 'chamilo_database_version'
  5970. );
  5971. }
  5972. /**
  5973. * Checks if the user is corrently logged in. Returns the user ID if he is, or
  5974. * false if he isn't. If the user ID is given and is an integer, then the same
  5975. * ID is simply returned
  5976. * @param integer User ID
  5977. * @return boolean Integer User ID is logged in, or false otherwise
  5978. */
  5979. function api_user_is_login($user_id = null) {
  5980. $user_id = empty($user_id) ? api_get_user_id() : intval($user_id);
  5981. return $user_id && !api_is_anonymous();
  5982. }
  5983. /**
  5984. * Guess the real ip for register in the database, even in reverse proxy cases.
  5985. * To be recognized, the IP has to be found in either $_SERVER['REMOTE_ADDR'] or
  5986. * in $_SERVER['HTTP_X_FORWARDED_FOR'], which is in common use with rproxies.
  5987. * @return string the user's real ip (unsafe - escape it before inserting to db)
  5988. * @author Jorge Frisancho Jibaja <jrfdeft@gmail.com>, USIL - Some changes to allow the use of real IP using reverse proxy
  5989. * @version CEV CHANGE 24APR2012
  5990. */
  5991. function api_get_real_ip(){
  5992. // Guess the IP if behind a reverse proxy
  5993. global $debug;
  5994. $ip = trim($_SERVER['REMOTE_ADDR']);
  5995. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  5996. if (preg_match('/,/', $_SERVER['HTTP_X_FORWARDED_FOR'])) {
  5997. @list($ip1, $ip2) = @explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  5998. } else {
  5999. $ip1 = $_SERVER['HTTP_X_FORWARDED_FOR'];
  6000. }
  6001. $ip = trim($ip1);
  6002. }
  6003. if (!empty($debug)) error_log('Real IP: '.$ip);
  6004. return $ip;
  6005. }
  6006. /**
  6007. * Checks whether an IP is included inside an IP range
  6008. * @param string IP address
  6009. * @param string IP range
  6010. * @param string $ip
  6011. * @return bool True if IP is in the range, false otherwise
  6012. * @author claudiu at cnixs dot com on http://www.php.net/manual/fr/ref.network.php#55230
  6013. * @author Yannick Warnier for improvements and managment of multiple ranges
  6014. * @todo check for IPv6 support
  6015. */
  6016. function api_check_ip_in_range($ip,$range)
  6017. {
  6018. if (empty($ip) or empty($range)) {
  6019. return false;
  6020. }
  6021. $ip_ip = ip2long ($ip);
  6022. // divide range param into array of elements
  6023. if (strpos($range,',')!==false) {
  6024. $ranges = explode(',',$range);
  6025. } else {
  6026. $ranges = array($range);
  6027. }
  6028. foreach ($ranges as $range) {
  6029. $range = trim($range);
  6030. if (empty($range)) { continue; }
  6031. if (strpos($range,'/')===false) {
  6032. if (strcmp($ip,$range)===0) {
  6033. return true; // there is a direct IP match, return OK
  6034. }
  6035. continue; //otherwise, get to the next range
  6036. }
  6037. // the range contains a "/", so analyse completely
  6038. list ($net, $mask) = explode("/", $range);
  6039. $ip_net = ip2long ($net);
  6040. // mask binary magic
  6041. $ip_mask = ~((1 << (32 - $mask)) - 1);
  6042. $ip_ip_net = $ip_ip & $ip_mask;
  6043. if ($ip_ip_net == $ip_net) {
  6044. return true;
  6045. }
  6046. }
  6047. return false;
  6048. }
  6049. function api_check_user_access_to_legal($course_visibility) {
  6050. $course_visibility_list = array(COURSE_VISIBILITY_OPEN_WORLD, COURSE_VISIBILITY_OPEN_PLATFORM);
  6051. return in_array($course_visibility, $course_visibility_list) || api_is_drh();
  6052. }
  6053. /**
  6054. * Checks if the global chat is enabled or not
  6055. *
  6056. * @return bool
  6057. */
  6058. function api_is_global_chat_enabled()
  6059. {
  6060. return
  6061. !api_is_anonymous() &&
  6062. api_get_setting('chat.allow_global_chat') == 'true' &&
  6063. api_get_setting('social.allow_social_tool') == 'true';
  6064. }
  6065. /**
  6066. * @todo Fix tool_visible_by_default_at_creation labels
  6067. * @todo Add sessionId parameter to avoid using context
  6068. *
  6069. * @param int $item_id
  6070. * @param int $tool_id
  6071. * @param int $group_id iid
  6072. * @param array $courseInfo
  6073. */
  6074. function api_set_default_visibility(
  6075. $item_id,
  6076. $tool_id,
  6077. $group_id = 0,
  6078. $courseInfo = array(),
  6079. $sessionId = 0,
  6080. $userId = 0
  6081. ) {
  6082. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  6083. $courseId = $courseInfo['real_id'];
  6084. $courseCode = $courseInfo['code'];
  6085. $sessionId = empty($sessionId) ? api_get_session_id() : $sessionId;
  6086. $userId = empty($userId) ? api_get_user_id() : $userId;
  6087. if (empty($group_id)) {
  6088. $group_id = api_get_group_id();
  6089. }
  6090. $groupInfo = GroupManager::get_group_properties($group_id);
  6091. $groupIid = 0;
  6092. if ($groupInfo) {
  6093. $groupIid = $groupInfo['iid'];
  6094. }
  6095. $original_tool_id = $tool_id;
  6096. switch ($tool_id) {
  6097. case TOOL_LINK:
  6098. case TOOL_LINK_CATEGORY:
  6099. $tool_id = 'links';
  6100. break;
  6101. case TOOL_DOCUMENT:
  6102. $tool_id = 'documents';
  6103. break;
  6104. case TOOL_LEARNPATH:
  6105. $tool_id = 'learning';
  6106. break;
  6107. case TOOL_ANNOUNCEMENT:
  6108. $tool_id = 'announcements';
  6109. break;
  6110. case TOOL_FORUM:
  6111. case TOOL_FORUM_CATEGORY:
  6112. case TOOL_FORUM_THREAD:
  6113. $tool_id = 'forums';
  6114. break;
  6115. case TOOL_QUIZ:
  6116. $tool_id = 'quiz';
  6117. break;
  6118. }
  6119. $setting = api_get_setting('document.tool_visible_by_default_at_creation');
  6120. if (isset($setting[$tool_id])) {
  6121. $visibility = 'invisible';
  6122. if ($setting[$tool_id] == 'true') {
  6123. $visibility = 'visible';
  6124. }
  6125. // Read the portal and course default visibility
  6126. if ($tool_id == 'documents') {
  6127. $visibility = DocumentManager::getDocumentDefaultVisibility($courseCode);
  6128. }
  6129. api_item_property_update(
  6130. $courseInfo,
  6131. $original_tool_id,
  6132. $item_id,
  6133. $visibility,
  6134. $userId,
  6135. $groupIid,
  6136. null,
  6137. null,
  6138. null,
  6139. api_get_session_id()
  6140. );
  6141. // Fixes default visibility for tests
  6142. switch ($original_tool_id) {
  6143. case TOOL_QUIZ:
  6144. if (empty($sessionId)) {
  6145. $objExerciseTmp = new Exercise($courseId);
  6146. $objExerciseTmp->read($item_id);
  6147. if ($visibility == 'visible') {
  6148. $objExerciseTmp->enable();
  6149. $objExerciseTmp->save();
  6150. } else {
  6151. $objExerciseTmp->disable();
  6152. $objExerciseTmp->save();
  6153. }
  6154. }
  6155. break;
  6156. }
  6157. }
  6158. }
  6159. /**
  6160. * @return string
  6161. */
  6162. function api_get_security_key() {
  6163. return api_get_configuration_value('security_key');
  6164. }
  6165. /**
  6166. * @param int $user_id
  6167. * @param int $courseId
  6168. * @param int $session_id
  6169. * @return array
  6170. */
  6171. function api_detect_user_roles($user_id, $courseId, $session_id = 0)
  6172. {
  6173. $user_roles = array();
  6174. /*$user_info = api_get_user_info($user_id);
  6175. $user_roles[] = $user_info['status'];*/
  6176. $courseInfo = api_get_course_info_by_id($courseId);
  6177. $course_code = $courseInfo['code'];
  6178. $url_id = api_get_current_access_url_id();
  6179. if (api_is_platform_admin_by_id($user_id, $url_id)) {
  6180. $user_roles[] = PLATFORM_ADMIN;
  6181. }
  6182. /*if (api_is_drh()) {
  6183. $user_roles[] = DRH;
  6184. }*/
  6185. if (!empty($session_id)) {
  6186. if (SessionManager::user_is_general_coach($user_id, $session_id)) {
  6187. $user_roles[] = SESSION_GENERAL_COACH;
  6188. }
  6189. }
  6190. if (!empty($course_code)) {
  6191. if (empty($session_id)) {
  6192. if (CourseManager::is_course_teacher($user_id, $course_code)) {
  6193. $user_roles[] = COURSEMANAGER;
  6194. }
  6195. if (CourseManager::get_tutor_in_course_status($user_id, $courseInfo['real_id'])) {
  6196. $user_roles[] = COURSE_TUTOR;
  6197. }
  6198. if (CourseManager::is_user_subscribed_in_course($user_id, $course_code)) {
  6199. $user_roles[] = COURSE_STUDENT;
  6200. }
  6201. } else {
  6202. $user_status_in_session = SessionManager::get_user_status_in_course_session(
  6203. $user_id,
  6204. $courseId,
  6205. $session_id
  6206. );
  6207. if (!empty($user_status_in_session)) {
  6208. if ($user_status_in_session == 0) {
  6209. $user_roles[] = SESSION_STUDENT;
  6210. }
  6211. if ($user_status_in_session == 2) {
  6212. $user_roles[] = SESSION_COURSE_COACH;
  6213. }
  6214. }
  6215. /*if (api_is_course_session_coach($user_id, $course_code, $session_id)) {
  6216. $user_roles[] = SESSION_COURSE_COACH;
  6217. }*/
  6218. }
  6219. }
  6220. return $user_roles;
  6221. }
  6222. /**
  6223. * @param int $courseId
  6224. * @param int $session_id
  6225. * @return bool
  6226. */
  6227. function api_coach_can_edit_view_results($courseId = null, $session_id = null)
  6228. {
  6229. $user_id = api_get_user_id();
  6230. if (empty($courseId)) {
  6231. $courseId = api_get_course_int_id();
  6232. }
  6233. if (empty($session_id)) {
  6234. $session_id = api_get_session_id();
  6235. }
  6236. if (api_is_platform_admin()) {
  6237. return true;
  6238. }
  6239. $roles = api_detect_user_roles($user_id, $courseId, $session_id);
  6240. if (in_array(SESSION_COURSE_COACH, $roles)) {
  6241. //return api_get_setting('session_tutor_reports_visibility') == 'true';
  6242. return true;
  6243. } else {
  6244. if (in_array(COURSEMANAGER, $roles)) {
  6245. return true;
  6246. }
  6247. return false;
  6248. }
  6249. }
  6250. function api_set_setting_last_update()
  6251. {
  6252. // Saving latest refresh.
  6253. api_set_setting('settings_latest_update', api_get_utc_datetime());
  6254. }
  6255. /**
  6256. * Tries to set memory limit, if authorized and new limit is higher than current
  6257. * @param string New memory limit
  6258. * @param string $mem
  6259. * @return bool True on success, false on failure or current is higher than suggested
  6260. * @assert (null) === false
  6261. * @assert (-1) === false
  6262. * @assert (0) === true
  6263. * @assert ('1G') === true
  6264. */
  6265. function api_set_memory_limit($mem){
  6266. //if ini_set() not available, this function is useless
  6267. if (!function_exists('ini_set') || is_null($mem) || $mem == -1) {
  6268. return false;
  6269. }
  6270. $memory_limit = ini_get('memory_limit');
  6271. if (api_get_bytes_memory_limit($mem) > api_get_bytes_memory_limit($memory_limit)){
  6272. ini_set('memory_limit', $mem);
  6273. return true;
  6274. }
  6275. return false;
  6276. }
  6277. /**
  6278. * Gets memory limit in bytes
  6279. * @param string The memory size (128M, 1G, 1000K, etc)
  6280. * @return int
  6281. * @assert (null) === false
  6282. * @assert ('1t') === 1099511627776
  6283. * @assert ('1g') === 1073741824
  6284. * @assert ('1m') === 1048576
  6285. * @assert ('100k') === 102400
  6286. */
  6287. function api_get_bytes_memory_limit($mem){
  6288. $size = strtolower(substr($mem,-1));
  6289. switch ($size) {
  6290. case 't':
  6291. $mem = intval(substr($mem,-1))*1024*1024*1024*1024;
  6292. break;
  6293. case 'g':
  6294. $mem = intval(substr($mem,0,-1))*1024*1024*1024;
  6295. break;
  6296. case 'm':
  6297. $mem = intval(substr($mem,0,-1))*1024*1024;
  6298. break;
  6299. case 'k':
  6300. $mem = intval(substr($mem,0,-1))*1024;
  6301. break;
  6302. default:
  6303. // we assume it's integer only
  6304. $mem = intval($mem);
  6305. break;
  6306. }
  6307. return $mem;
  6308. }
  6309. /**
  6310. * Finds all the information about a user from username instead of user id
  6311. *
  6312. * @param string $officialCode
  6313. * @return array $user_info user_id, lastname, firstname, username, email, ...
  6314. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  6315. */
  6316. function api_get_user_info_from_official_code($officialCode)
  6317. {
  6318. if (empty($officialCode)) {
  6319. return false;
  6320. }
  6321. $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)."
  6322. WHERE official_code ='".Database::escape_string($officialCode)."'";
  6323. $result = Database::query($sql);
  6324. if (Database::num_rows($result) > 0) {
  6325. $result_array = Database::fetch_array($result);
  6326. return _api_format_user($result_array);
  6327. }
  6328. return false;
  6329. }
  6330. /**
  6331. * @param string $usernameInputId
  6332. * @param string $passwordInputId
  6333. * @return null|string
  6334. */
  6335. function api_get_password_checker_js($usernameInputId, $passwordInputId)
  6336. {
  6337. $checkPass = api_get_setting('security.allow_strength_pass_checker');
  6338. $useStrengthPassChecker = $checkPass === 'true';
  6339. if ($useStrengthPassChecker === false) {
  6340. return null;
  6341. }
  6342. $translations = [
  6343. 'wordLength' => get_lang('PasswordIsTooShort'),
  6344. 'wordNotEmail' => get_lang('YourPasswordCannotBeTheSameAsYourEmail'),
  6345. 'wordSimilarToUsername' => get_lang('YourPasswordCannotContainYourUsername'),
  6346. 'wordTwoCharacterClasses' => get_lang('WordTwoCharacterClasses'),
  6347. 'wordRepetitions' => get_lang('TooManyRepetitions'),
  6348. 'wordSequences' => get_lang('YourPasswordContainsSequences'),
  6349. 'errorList' => get_lang('ErrorsFound'),
  6350. 'veryWeak' => get_lang('PasswordVeryWeak'),
  6351. 'weak' => get_lang('PasswordWeak'),
  6352. 'normal' => get_lang('PasswordNormal'),
  6353. 'medium' => get_lang('PasswordMedium'),
  6354. 'strong' => get_lang('PasswordStrong'),
  6355. 'veryStrong' => get_lang('PasswordVeryStrong')
  6356. ];
  6357. $js = api_get_asset('pwstrength-bootstrap/dist/pwstrength-bootstrap.min.js');
  6358. $js .= "<script>
  6359. var errorMessages = {
  6360. password_to_short : \"" . get_lang('PasswordIsTooShort')."\",
  6361. same_as_username : \"".get_lang('YourPasswordCannotBeTheSameAsYourUsername')."\"
  6362. };
  6363. $(document).ready(function() {
  6364. var lang = ".json_encode($translations).";
  6365. var options = {
  6366. onLoad : function () {
  6367. //$('#messages').text('Start typing password');
  6368. },
  6369. onKeyUp: function (evt) {
  6370. $(evt.target).pwstrength('outputErrorList');
  6371. },
  6372. errorMessages : errorMessages,
  6373. viewports: {
  6374. progress: '#password_progress',
  6375. verdict: '#password-verdict',
  6376. errors: '#password-errors'
  6377. },
  6378. usernameField: '$usernameInputId'
  6379. };
  6380. options.i18n = {
  6381. t: function (key) {
  6382. var result = lang[key];
  6383. return result === key ? '' : result; // This assumes you return the
  6384. }
  6385. };
  6386. $('".$passwordInputId."').pwstrength(options);
  6387. });
  6388. </script>";
  6389. return $js;
  6390. }
  6391. /**
  6392. * @param string $username
  6393. * create an user extra field called 'captcha_blocked_until_date'
  6394. */
  6395. function api_block_account_captcha($username)
  6396. {
  6397. $userInfo = api_get_user_info_from_username($username);
  6398. if (empty($userInfo)) {
  6399. return false;
  6400. }
  6401. $minutesToBlock = api_get_setting('captcha_time_to_block');
  6402. $time = time() + $minutesToBlock*60;
  6403. UserManager::update_extra_field_value(
  6404. $userInfo['user_id'],
  6405. 'captcha_blocked_until_date',
  6406. api_get_utc_datetime($time)
  6407. );
  6408. }
  6409. /**
  6410. * @param string $username
  6411. */
  6412. function api_clean_account_captcha($username)
  6413. {
  6414. $userInfo = api_get_user_info_from_username($username);
  6415. if (empty($userInfo)) {
  6416. return false;
  6417. }
  6418. Session::erase('loginFailedCount');
  6419. UserManager::update_extra_field_value(
  6420. $userInfo['user_id'],
  6421. 'captcha_blocked_until_date',
  6422. null
  6423. );
  6424. }
  6425. /**
  6426. * @param string $username
  6427. * @return bool
  6428. */
  6429. function api_get_user_blocked_by_captcha($username)
  6430. {
  6431. $userInfo = api_get_user_info_from_username($username);
  6432. if (empty($userInfo)) {
  6433. return false;
  6434. }
  6435. $data = UserManager::get_extra_user_data_by_field(
  6436. $userInfo['user_id'],
  6437. 'captcha_blocked_until_date'
  6438. );
  6439. if (isset($data) && isset($data['captcha_blocked_until_date'])) {
  6440. return $data['captcha_blocked_until_date'];
  6441. }
  6442. return false;
  6443. }
  6444. /**
  6445. * Remove tags from HTML anf return the $in_number_char first non-HTML char
  6446. * Postfix the text with "..." if it has been truncated.
  6447. * @param integer $in_number_char
  6448. * @return string
  6449. * @author hubert borderiou
  6450. */
  6451. function api_get_short_text_from_html($in_html, $in_number_char)
  6452. {
  6453. $out_res = api_remove_tags_with_space($in_html, false);
  6454. $postfix = "...";
  6455. if (strlen($out_res) > $in_number_char) {
  6456. $out_res = substr($out_res, 0, $in_number_char).$postfix;
  6457. }
  6458. return $out_res;
  6459. }
  6460. /**
  6461. * Replace tags with a space in a text.
  6462. * If $in_double_quote_replace, replace " with '' (for HTML attribute purpose, for exemple)
  6463. * @return string
  6464. * @author hubert borderiou
  6465. */
  6466. function api_remove_tags_with_space($in_html, $in_double_quote_replace = true) {
  6467. $out_res = $in_html;
  6468. if ($in_double_quote_replace) {
  6469. $out_res = str_replace('"', "''", $out_res);
  6470. }
  6471. // avoid text stuck together when tags are removed, adding a space after >
  6472. $out_res = str_replace (">", "> ", $out_res);
  6473. $out_res = strip_tags($out_res);
  6474. return $out_res;
  6475. }
  6476. /**
  6477. * If true, the drh can access all content (courses, users) inside a session
  6478. * @return bool
  6479. */
  6480. function api_drh_can_access_all_session_content()
  6481. {
  6482. $value = api_get_setting('drh_can_access_all_session_content');
  6483. return $value === 'true';
  6484. }
  6485. /**
  6486. * @param string $tool
  6487. * @param string $setting
  6488. * @param integer $defaultValue
  6489. * @return string
  6490. */
  6491. function api_get_default_tool_setting($tool, $setting, $defaultValue)
  6492. {
  6493. $value = api_get_configuration_value($tool);
  6494. if (isset($value) &&
  6495. isset($value['default_settings']) &&
  6496. isset($value['default_settings'][$setting])
  6497. ) {
  6498. return $value['default_settings'][$setting];
  6499. }
  6500. return $defaultValue;
  6501. }
  6502. /**
  6503. * Checks if user can login as another user
  6504. *
  6505. * @param int $loginAsUserId the user id to log in
  6506. * @param int $userId my user id
  6507. * @return bool
  6508. */
  6509. function api_can_login_as($loginAsUserId, $userId = null)
  6510. {
  6511. if (empty($userId)) {
  6512. $userId = api_get_user_id();
  6513. }
  6514. if ($loginAsUserId == $userId) {
  6515. return false;
  6516. }
  6517. if (empty($loginAsUserId)) {
  6518. return false;
  6519. }
  6520. if ($loginAsUserId != strval(intval($loginAsUserId))) {
  6521. return false;
  6522. }
  6523. // Check if the user to login is an admin
  6524. if (api_is_platform_admin_by_id($loginAsUserId)) {
  6525. // Only super admins can login to admin accounts
  6526. if (!api_global_admin_can_edit_admin($loginAsUserId)) {
  6527. return false;
  6528. }
  6529. }
  6530. $userInfo = api_get_user_info($userId);
  6531. $isDrh = function() use($loginAsUserId) {
  6532. if (api_is_drh()) {
  6533. if (api_drh_can_access_all_session_content()) {
  6534. $users = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus('drh_all', api_get_user_id());
  6535. $userList = array();
  6536. if (is_array($users)) {
  6537. foreach ($users as $user) {
  6538. $userList[] = $user['user_id'];
  6539. }
  6540. }
  6541. if (in_array($loginAsUserId, $userList)) {
  6542. return true;
  6543. }
  6544. } else {
  6545. if (api_is_drh() && UserManager::is_user_followed_by_drh($loginAsUserId, api_get_user_id())) {
  6546. return true;
  6547. }
  6548. }
  6549. }
  6550. return false;
  6551. };
  6552. return api_is_platform_admin() || (api_is_session_admin() && $userInfo['status'] == 5) || $isDrh();
  6553. }
  6554. /**
  6555. * @return bool
  6556. */
  6557. function api_is_allowed_in_course()
  6558. {
  6559. return Session::read('is_allowed_in_course');
  6560. }
  6561. /**
  6562. * Set the cookie to go directly to the course code $in_firstpage
  6563. * after login
  6564. * @param string $in_firstpage is the course code of the course to go
  6565. */
  6566. function api_set_firstpage_parameter($in_firstpage)
  6567. {
  6568. setcookie('GotoCourse', $in_firstpage);
  6569. }
  6570. /**
  6571. * Delete the cookie to go directly to the course code $in_firstpage
  6572. * after login
  6573. */
  6574. function api_delete_firstpage_parameter()
  6575. {
  6576. setcookie('GotoCourse', '', time() - 3600);
  6577. }
  6578. /**
  6579. * @return boolean if course_code for direct course access after login is set
  6580. */
  6581. function exist_firstpage_parameter()
  6582. {
  6583. return (isset($_COOKIE['GotoCourse']) && $_COOKIE['GotoCourse'] != "");
  6584. }
  6585. /**
  6586. * @return return the course_code of the course where user login
  6587. */
  6588. function api_get_firstpage_parameter()
  6589. {
  6590. return $_COOKIE['GotoCourse'];
  6591. }
  6592. /**
  6593. * Return true on https install
  6594. * @return boolean
  6595. */
  6596. function api_is_https()
  6597. {
  6598. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off');
  6599. }
  6600. /**
  6601. * Return protocol (http or https)
  6602. * @return string
  6603. */
  6604. function api_get_protocol()
  6605. {
  6606. return api_is_https() ? 'https' : 'http';
  6607. }
  6608. /**
  6609. * Return a string where " are replaced with 2 '
  6610. * It is useful when you pass a PHP variable in a Javascript browser dialog
  6611. * e.g. : alert("<?php get_lang('Message') ?>");
  6612. * and message contains character "
  6613. *
  6614. * @param string $in_text
  6615. * @return string
  6616. */
  6617. function convert_double_quote_to_single($in_text)
  6618. {
  6619. return api_preg_replace('/"/', "''", $in_text);
  6620. }
  6621. /**
  6622. * Get origin
  6623. *
  6624. * @param string
  6625. * @return string
  6626. **/
  6627. function api_get_origin()
  6628. {
  6629. if (isset($_REQUEST['origin'])) {
  6630. return $_REQUEST['origin'] === 'learnpath' ? 'learnpath' : '';
  6631. }
  6632. return '';
  6633. }
  6634. /**
  6635. * Warns an user that the portal reach certain limit.
  6636. * @param string $limitName
  6637. */
  6638. function api_warn_hosting_contact($limitName, $limit)
  6639. {
  6640. $email = api_get_configuration_value('hosting_contact_mail');
  6641. if (!empty($email)) {
  6642. $subject = get_lang('HostingWarningReached');
  6643. $body = get_lang('PortalName').': '.api_get_path(WEB_PATH)." \n ";
  6644. $body .= get_lang('PortalLimitType').': '.$limitName." \n ";
  6645. $body .= get_lang('Value') . ': ' . $limit;
  6646. api_mail_html(null, $email, $subject, $body);
  6647. }
  6648. }
  6649. /**
  6650. * @param string $variable
  6651. * @return bool|mixed
  6652. */
  6653. function api_get_configuration_value($variable)
  6654. {
  6655. return Container::getParameter($variable);
  6656. }
  6657. /**
  6658. * Returns supported image extensions in the portal
  6659. * @param bool $supportVectors Whether vector images should also be accepted or not
  6660. * @return array Supported image extensions in the portal
  6661. */
  6662. function api_get_supported_image_extensions($supportVectors = true)
  6663. {
  6664. // jpg can also be called jpeg, jpe, jfif and jif. See https://en.wikipedia.org/wiki/JPEG#JPEG_filename_extensions
  6665. $supportedImageExtensions = array('jpg', 'jpeg', 'png', 'gif', 'jpe', 'jfif', 'jif');
  6666. if ($supportVectors) {
  6667. array_push($supportedImageExtensions, 'svg');
  6668. }
  6669. if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
  6670. array_push($supportedImageExtensions, 'webp');
  6671. }
  6672. return $supportedImageExtensions;
  6673. }
  6674. /**
  6675. * This setting changes the registration status for the campus
  6676. *
  6677. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  6678. * @version August 2006
  6679. * @param bool $listCampus Whether we authorize
  6680. * @todo the $_settings should be reloaded here. => write api function for this and use this in global.inc.php also.
  6681. */
  6682. function api_register_campus($listCampus = true) {
  6683. $tbl_settings = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  6684. $sql = "UPDATE $tbl_settings SET selected_value='true' WHERE variable='registered'";
  6685. Database::query($sql);
  6686. if (!$listCampus) {
  6687. $sql = "UPDATE $tbl_settings SET selected_value='true' WHERE variable='donotlistcampus'";
  6688. Database::query($sql);
  6689. }
  6690. }
  6691. /**
  6692. * Checks whether current user is a student boss
  6693. * @global array $_user
  6694. * @return boolean
  6695. */
  6696. function api_is_student_boss()
  6697. {
  6698. $_user = api_get_user_info();
  6699. return isset($_user['status']) && $_user['status'] == STUDENT_BOSS;
  6700. }
  6701. /**
  6702. * Check whether the user type should be exclude.
  6703. * Such as invited or anonymous users
  6704. * @param boolean $checkDB Optional. Whether check the user status
  6705. * @param int $userId Options. The user id
  6706. *
  6707. * @return boolean
  6708. */
  6709. function api_is_excluded_user_type($checkDB = false, $userId = 0)
  6710. {
  6711. if ($checkDB) {
  6712. $userId = empty($userId) ? api_get_user_id() : intval($userId);
  6713. if ($userId == 0) {
  6714. return true;
  6715. }
  6716. $userInfo = api_get_user_info($userId);
  6717. switch ($userInfo['status']) {
  6718. case INVITEE:
  6719. //no break;
  6720. case ANONYMOUS:
  6721. return true;
  6722. default:
  6723. return false;
  6724. }
  6725. }
  6726. $isInvited = api_is_invitee();
  6727. $isAnonymous = api_is_anonymous();
  6728. if ($isInvited || $isAnonymous) {
  6729. return true;
  6730. }
  6731. return false;
  6732. }
  6733. /**
  6734. * Get the user status to ignore in reports
  6735. * @param string $format Optional. The result type (array or string)
  6736. * @return array|string
  6737. */
  6738. function api_get_users_status_ignored_in_reports($format = 'array')
  6739. {
  6740. $excludedTypes = array(
  6741. INVITEE,
  6742. ANONYMOUS
  6743. );
  6744. if ($format == 'string') {
  6745. return implode(', ', $excludedTypes);
  6746. }
  6747. return $excludedTypes;
  6748. }
  6749. /**
  6750. * Set the Site Use Cookie Warning for 1 year
  6751. */
  6752. function api_set_site_use_cookie_warning_cookie()
  6753. {
  6754. setcookie('ChamiloUsesCookies', 'ok', time()+31556926);
  6755. }
  6756. /**
  6757. * Return true if the Site Use Cookie Warning Cookie warning exists
  6758. * @return bool
  6759. */
  6760. function api_site_use_cookie_warning_cookie_exist()
  6761. {
  6762. return isset($_COOKIE['ChamiloUsesCookies']);
  6763. }
  6764. /**
  6765. * Given a number of seconds, format the time to show hours, minutes and seconds
  6766. * @param int $time The time in seconds
  6767. * @param string $originFormat Optional. PHP o JS
  6768. * @return string (00h00'00")
  6769. */
  6770. function api_format_time($time, $originFormat = 'php')
  6771. {
  6772. $h = get_lang('h');
  6773. $hours = $time / 3600;
  6774. $mins = ($time % 3600) / 60;
  6775. $secs = ($time % 60);
  6776. if ($time < 0) {
  6777. $hours = 0;
  6778. $mins = 0;
  6779. $secs = 0;
  6780. }
  6781. if ($originFormat == 'js') {
  6782. $formattedTime = trim(sprintf("%02d : %02d : %02d", $hours, $mins, $secs));
  6783. } else {
  6784. $formattedTime = trim(sprintf("%02d$h%02d'%02d\"", $hours, $mins, $secs));
  6785. }
  6786. return $formattedTime;
  6787. }
  6788. /**
  6789. * Create a new empty directory with index.html file
  6790. * @param string $name The new directory name
  6791. * @param string $parentDirectory Directory parent directory name
  6792. * @return boolean Return true if the directory was create. Otherwise return false
  6793. */
  6794. function api_create_protected_dir($name, $parentDirectory)
  6795. {
  6796. $isCreated = false;
  6797. if (!is_writable($parentDirectory)) {
  6798. return false;
  6799. }
  6800. $fullPath = $parentDirectory . api_replace_dangerous_char($name);
  6801. if (mkdir($fullPath, api_get_permissions_for_new_directories(), true)) {
  6802. $fp = fopen($fullPath . '/index.html', 'w');
  6803. if ($fp) {
  6804. if (fwrite($fp, '<html><head></head><body></body></html>')) {
  6805. $isCreated = true;
  6806. }
  6807. }
  6808. fclose($fp);
  6809. }
  6810. return $isCreated;
  6811. }
  6812. /**
  6813. * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully)
  6814. * Sender name and email can be specified, if not specified
  6815. * name and email of the platform admin are used
  6816. *
  6817. * @author Bert Vanderkimpen ICT&O UGent
  6818. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  6819. *
  6820. * @param string name of recipient
  6821. * @param string email of recipient
  6822. * @param string email subject
  6823. * @param string email body
  6824. * @param string sender name
  6825. * @param string sender e-mail
  6826. * @param array extra headers in form $headers = array($name => $value) to allow parsing
  6827. * @param array data file (path and filename)
  6828. * @param bool True for attaching a embedded file inside content html (optional)
  6829. * @param array Additional parameters
  6830. * @return integer true if mail was sent
  6831. * @see class.phpmailer.php
  6832. */
  6833. function api_mail_html(
  6834. $recipient_name,
  6835. $recipient_email,
  6836. $subject,
  6837. $message,
  6838. $senderName = '',
  6839. $senderEmail = '',
  6840. $extra_headers = array(),
  6841. $data_file = array(),
  6842. $embedded_image = false,
  6843. $additionalParameters = array()
  6844. ) {
  6845. // Default values
  6846. $notification = new Notification();
  6847. $defaultEmail = $notification->getDefaultPlatformSenderEmail();
  6848. $defaultName = $notification->getDefaultPlatformSenderName();
  6849. // If the parameter is set don't use the admin.
  6850. $senderName = !empty($senderName) ? $senderName : $defaultName;
  6851. $senderEmail = !empty($senderEmail) ? $senderEmail : $defaultEmail;
  6852. $link = isset($additionalParameters['link']) ? $additionalParameters['link'] : '';
  6853. $swiftMessage = \Swift_Message::newInstance()
  6854. ->setSubject($subject)
  6855. ->setFrom($senderEmail, $senderName)
  6856. ->setTo($recipient_email, $recipient_name)
  6857. ->setBody(
  6858. Container::getTemplating()->render(
  6859. 'ChamiloCoreBundle:default/mail:mail.html.twig',
  6860. array('content' => $message, 'link' => $link)
  6861. ),
  6862. 'text/html'
  6863. )/*
  6864. * If you also want to include a plaintext version of the message
  6865. ->addPart(
  6866. $this->renderView(
  6867. 'Emails/registration.txt.twig',
  6868. array('name' => $name)
  6869. ),
  6870. 'text/plain'
  6871. )
  6872. */
  6873. ;
  6874. if (!empty($additionalParameters)) {
  6875. $plugin = new AppPlugin();
  6876. $smsPlugin = $plugin->getSMSPluginLibrary();
  6877. if ($smsPlugin) {
  6878. $smsPlugin->send($additionalParameters);
  6879. }
  6880. }
  6881. Container::getMailer()->send($swiftMessage);
  6882. return 1;
  6883. }
  6884. /**
  6885. * @param string $tool Possible values:
  6886. * GroupManager::GROUP_TOOL_*
  6887. *
  6888. */
  6889. function api_protect_course_group($tool, $showHeader = true)
  6890. {
  6891. $userId = api_get_user_id();
  6892. $groupId = api_get_group_id();
  6893. $groupInfo = GroupManager::get_group_properties($groupId);
  6894. if (!empty($groupInfo)) {
  6895. $allow = GroupManager::user_has_access(
  6896. $userId,
  6897. $groupInfo['iid'],
  6898. $tool
  6899. );
  6900. if (!$allow) {
  6901. api_not_allowed($showHeader);
  6902. }
  6903. }
  6904. }
  6905. /**
  6906. * Check if a date is in a date range
  6907. *
  6908. * @param datetime $startDate
  6909. * @param datetime $endDate
  6910. * @param datetime $currentDate
  6911. * @return bool true if date is in rage, false otherwise
  6912. */
  6913. function api_is_date_in_date_range($startDate, $endDate, $currentDate = null)
  6914. {
  6915. $startDate = strtotime(api_get_local_time($startDate));
  6916. $endDate = strtotime(api_get_local_time($endDate));
  6917. $currentDate = strtotime(api_get_local_time($currentDate));
  6918. if ($currentDate >= $startDate && $currentDate <= $endDate) {
  6919. return true;
  6920. }
  6921. return false;
  6922. }
  6923. /**
  6924. * Eliminate the duplicates of a multidimensional array by sending the key
  6925. *
  6926. * @param array $array multidimensional array
  6927. * @param int $key key to find to compare
  6928. * @return array
  6929. *
  6930. */
  6931. function api_unique_multidim_array($array, $key)
  6932. {
  6933. $temp_array = [];
  6934. $i = 0;
  6935. $key_array = [];
  6936. foreach($array as $val){
  6937. if(!in_array($val[$key],$key_array)){
  6938. $key_array[$i] = $val[$key];
  6939. $temp_array[$i] = $val;
  6940. }
  6941. $i++;
  6942. }
  6943. return $temp_array;
  6944. }
  6945. /**
  6946. * Limit the access to Session Admins wheen the limit_session_admin_role
  6947. * configuration variable is set to true
  6948. */
  6949. function api_protect_limit_for_session_admin()
  6950. {
  6951. $limitAdmin = api_get_setting('session.limit_session_admin_role');
  6952. if (api_is_session_admin() && $limitAdmin === 'true') {
  6953. api_not_allowed(true);
  6954. }
  6955. }
  6956. function api_is_student_view_active()
  6957. {
  6958. $studentView = Session::read('studentview');
  6959. return $studentView == "studentview";
  6960. }
  6961. /**
  6962. * Adds a file inside the upload/$type/id
  6963. *
  6964. * @param string $type
  6965. * @param array $file
  6966. * @param int $itemId
  6967. * @param string $cropParameters
  6968. * @return array|bool
  6969. */
  6970. function api_upload_file($type, $file, $itemId, $cropParameters = '')
  6971. {
  6972. $upload = process_uploaded_file($file);
  6973. if ($upload) {
  6974. $name = api_replace_dangerous_char($file['name']);
  6975. // No "dangerous" files
  6976. $name = disable_dangerous_file($name);
  6977. $pathId = '/'.substr((string)$itemId, 0, 1).'/'.$itemId.'/';
  6978. $path = api_get_path(SYS_UPLOAD_PATH).$type.$pathId;
  6979. if (!is_dir($path)) {
  6980. mkdir($path, api_get_permissions_for_new_directories(), true);
  6981. }
  6982. $pathToSave = $path.$name;
  6983. $result = move_uploaded_file($file['tmp_name'], $pathToSave);
  6984. if ($result) {
  6985. if (!empty($cropParameters)) {
  6986. $image = new Image($pathToSave);
  6987. $image->crop($cropParameters);
  6988. }
  6989. return ['path_to_save' => $pathId.$name];
  6990. }
  6991. return false;
  6992. }
  6993. }
  6994. /**
  6995. * @param string $type
  6996. * @param string $file
  6997. */
  6998. function api_remove_uploaded_file($type, $file)
  6999. {
  7000. $path = api_get_path(SYS_UPLOAD_PATH).$type.'/'.$file;
  7001. if (file_exists($path)) {
  7002. unlink($path);
  7003. }
  7004. }
  7005. /**
  7006. * @param array $session_info
  7007. * @param string $course_code
  7008. * @param bool $ignore_visibility_for_admins
  7009. * @param bool $check_coach_dates
  7010. * @return bool
  7011. */
  7012. function api_get_session_date_validation(
  7013. $session_info,
  7014. $course_code,
  7015. $ignore_visibility_for_admins = true,
  7016. $check_coach_dates = true
  7017. ) {
  7018. if (api_is_platform_admin()) {
  7019. if ($ignore_visibility_for_admins) {
  7020. return true;
  7021. }
  7022. }
  7023. $session_id = $session_info['id'];
  7024. $now = time();
  7025. $access = false;
  7026. if ($session_info) {
  7027. // I don't care the field visibility because there are not limit dates.
  7028. if (
  7029. (empty($session_info['access_start_date']) && empty($session_info['access_end_date'])) ||
  7030. ($session_info['access_start_date'] == '0000-00-00 00:00:00' && $session_info['access_end_date'] == '0000-00-00 00:00:00')
  7031. ) {
  7032. return true;
  7033. } else {
  7034. $accessStart = true;
  7035. // If access_start_date is set
  7036. if (!empty($session_info['access_start_date']) && $session_info['access_start_date'] != '0000-00-00 00:00:00') {
  7037. if ($now > api_strtotime($session_info['access_start_date'], 'UTC')) {
  7038. $access = true;
  7039. } else {
  7040. $access = false;
  7041. $accessStart = false;
  7042. }
  7043. }
  7044. if ($accessStart == true) {
  7045. //if access_end_date is set
  7046. if (!empty($session_info['access_end_date']) && $session_info['access_end_date'] != '0000-00-00 00:00:00') {
  7047. //only if access_end_date said that it was ok
  7048. if ($now <= api_strtotime($session_info['access_end_date'], 'UTC')) {
  7049. //date still available
  7050. $access = true;
  7051. } else {
  7052. //session ends
  7053. $access = false;
  7054. }
  7055. }
  7056. }
  7057. }
  7058. if ($check_coach_dates) {
  7059. // 2. If I'm a coach
  7060. $is_coach = api_is_coach($session_id, $course_code);
  7061. if ($is_coach) {
  7062. if (isset($session_info['access_end_date']) &&
  7063. !empty($session_info['access_end_date']) &&
  7064. $session_info['access_end_date'] != '0000-00-00 00:00:00' &&
  7065. isset($session_info['coach_access_end_date']) &&
  7066. !empty($session_info['coach_access_end_date']) &&
  7067. $session_info['coach_access_end_date'] != '0000-00-00 00:00:00'
  7068. ) {
  7069. $end_date_extra_for_coach = api_strtotime($session_info['coach_access_end_date'], 'UTC');
  7070. if ($now <= $end_date_extra_for_coach) {
  7071. $access = true;
  7072. } else {
  7073. $access = false;
  7074. }
  7075. }
  7076. // Test start date
  7077. if (isset($session_info['access_start_date']) && !empty($session_info['access_start_date']) && $session_info['access_start_date'] != '0000-00-00 00:00:00' &&
  7078. isset($session_info['coach_start_date']) && !empty($session_info['coach_start_date']) && $session_info['coach_start_date'] != '0000-00-00 00:00:00') {
  7079. $start_date_for_coach = api_strtotime($session_info['coach_start_date'], 'UTC');
  7080. if ($now > $start_date_for_coach) {
  7081. $access = true;
  7082. } else {
  7083. $access = false;
  7084. }
  7085. }
  7086. }
  7087. }
  7088. return $access;
  7089. }
  7090. }