usermanager.lib.php 240 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. use Chamilo\CoreBundle\Entity\Repository\AccessUrlRepository;
  5. use Chamilo\CoreBundle\Entity\SkillRelUser;
  6. use Chamilo\CoreBundle\Entity\SkillRelUserComment;
  7. use Chamilo\UserBundle\Entity\User;
  8. use Chamilo\UserBundle\Repository\UserRepository;
  9. use ChamiloSession as Session;
  10. use Symfony\Component\Security\Core\Encoder\EncoderFactory;
  11. /**
  12. * Class UserManager.
  13. *
  14. * This library provides functions for user management.
  15. * Include/require it in your code to use its functionality.
  16. *
  17. * @package chamilo.library
  18. *
  19. * @author Julio Montoya <gugli100@gmail.com> Social network groups added 2009/12
  20. */
  21. class UserManager
  22. {
  23. // This constants are deprecated use the constants located in ExtraField
  24. const USER_FIELD_TYPE_TEXT = 1;
  25. const USER_FIELD_TYPE_TEXTAREA = 2;
  26. const USER_FIELD_TYPE_RADIO = 3;
  27. const USER_FIELD_TYPE_SELECT = 4;
  28. const USER_FIELD_TYPE_SELECT_MULTIPLE = 5;
  29. const USER_FIELD_TYPE_DATE = 6;
  30. const USER_FIELD_TYPE_DATETIME = 7;
  31. const USER_FIELD_TYPE_DOUBLE_SELECT = 8;
  32. const USER_FIELD_TYPE_DIVIDER = 9;
  33. const USER_FIELD_TYPE_TAG = 10;
  34. const USER_FIELD_TYPE_TIMEZONE = 11;
  35. const USER_FIELD_TYPE_SOCIAL_PROFILE = 12;
  36. const USER_FIELD_TYPE_FILE = 13;
  37. const USER_FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  38. private static $encryptionMethod;
  39. /**
  40. * Constructor.
  41. *
  42. * @assert () === null
  43. */
  44. public function __construct()
  45. {
  46. }
  47. /**
  48. * Repository is use to query the DB, selects, etc.
  49. *
  50. * @return UserRepository
  51. */
  52. public static function getRepository()
  53. {
  54. /** @var UserRepository $userRepository */
  55. $userRepository = Database::getManager()->getRepository('ChamiloUserBundle:User');
  56. return $userRepository;
  57. }
  58. /**
  59. * Create/update/delete methods are available in the UserManager
  60. * (based in the Sonata\UserBundle\Entity\UserManager).
  61. *
  62. * @return Chamilo\UserBundle\Entity\Manager\UserManager
  63. */
  64. public static function getManager()
  65. {
  66. static $userManager;
  67. if (!isset($userManager)) {
  68. $encoderFactory = self::getEncoderFactory();
  69. $userManager = new Chamilo\UserBundle\Entity\Manager\UserManager(
  70. $encoderFactory,
  71. new \FOS\UserBundle\Util\Canonicalizer(),
  72. new \FOS\UserBundle\Util\Canonicalizer(),
  73. Database::getManager(),
  74. 'Chamilo\\UserBundle\\Entity\\User'
  75. );
  76. }
  77. return $userManager;
  78. }
  79. /**
  80. * @param string $encryptionMethod
  81. */
  82. public static function setPasswordEncryption($encryptionMethod)
  83. {
  84. self::$encryptionMethod = $encryptionMethod;
  85. }
  86. /**
  87. * @return bool|mixed
  88. */
  89. public static function getPasswordEncryption()
  90. {
  91. $encryptionMethod = self::$encryptionMethod;
  92. if (empty($encryptionMethod)) {
  93. $encryptionMethod = api_get_configuration_value('password_encryption');
  94. }
  95. return $encryptionMethod;
  96. }
  97. /**
  98. * Validates the password.
  99. *
  100. * @param $encoded
  101. * @param $raw
  102. * @param $salt
  103. *
  104. * @return bool
  105. */
  106. public static function isPasswordValid($encoded, $raw, $salt)
  107. {
  108. $encoder = new \Chamilo\UserBundle\Security\Encoder(self::getPasswordEncryption());
  109. $validPassword = $encoder->isPasswordValid($encoded, $raw, $salt);
  110. return $validPassword;
  111. }
  112. /**
  113. * @param string $raw
  114. * @param User $user
  115. *
  116. * @return string
  117. */
  118. public static function encryptPassword($raw, User $user)
  119. {
  120. $encoder = self::getEncoder($user);
  121. $encodedPassword = $encoder->encodePassword(
  122. $raw,
  123. $user->getSalt()
  124. );
  125. return $encodedPassword;
  126. }
  127. /**
  128. * @param int $userId
  129. * @param string $password
  130. */
  131. public static function updatePassword($userId, $password)
  132. {
  133. $repository = self::getRepository();
  134. /** @var User $user */
  135. $user = $repository->find($userId);
  136. $userManager = self::getManager();
  137. $user->setPlainPassword($password);
  138. $userManager->updateUser($user, true);
  139. }
  140. /**
  141. * Creates a new user for the platform.
  142. *
  143. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>,
  144. * @author Roan Embrechts <roan_embrechts@yahoo.com>
  145. *
  146. * @param string $firstName
  147. * @param string $lastName
  148. * @param int $status (1 for course tutor, 5 for student, 6 for anonymous)
  149. * @param string $email
  150. * @param string $loginName
  151. * @param string $password
  152. * @param string $official_code Any official code (optional)
  153. * @param string $language User language (optional)
  154. * @param string $phone Phone number (optional)
  155. * @param string $picture_uri Picture URI (optional)
  156. * @param string $authSource Authentication source (defaults to 'platform', dependind on constant)
  157. * @param string $expirationDate Account expiration date (optional, defaults to null)
  158. * @param int $active Whether the account is enabled or disabled by default
  159. * @param int $hr_dept_id The department of HR in which the user is registered (defaults to 0)
  160. * @param array $extra Extra fields
  161. * @param string $encrypt_method Used if password is given encrypted. Set to an empty string by default
  162. * @param bool $send_mail
  163. * @param bool $isAdmin
  164. * @param string $address
  165. * @param bool $sendEmailToAllAdmins
  166. * @param FormValidator $form
  167. * @param int $creatorId
  168. * @param array $emailTemplate
  169. * @param string $redirectToURLAfterLogin
  170. *
  171. * @return mixed new user id - if the new user creation succeeds, false otherwise
  172. * @desc The function tries to retrieve user id from the session.
  173. * If it exists, the current user id is the creator id. If a problem arises,
  174. * @assert ('Sam','Gamegie',5,'sam@example.com','jo','jo') > 1
  175. * @assert ('Pippin','Took',null,null,'jo','jo') === false
  176. */
  177. public static function create_user(
  178. $firstName,
  179. $lastName,
  180. $status,
  181. $email,
  182. $loginName,
  183. $password,
  184. $official_code = '',
  185. $language = '',
  186. $phone = '',
  187. $picture_uri = '',
  188. $authSource = PLATFORM_AUTH_SOURCE,
  189. $expirationDate = null,
  190. $active = 1,
  191. $hr_dept_id = 0,
  192. $extra = [],
  193. $encrypt_method = '',
  194. $send_mail = false,
  195. $isAdmin = false,
  196. $address = '',
  197. $sendEmailToAllAdmins = false,
  198. $form = null,
  199. $creatorId = 0,
  200. $emailTemplate = [],
  201. $redirectToURLAfterLogin = ''
  202. ) {
  203. $creatorId = empty($creatorId) ? api_get_user_id() : 0;
  204. $creatorInfo = api_get_user_info($creatorId);
  205. $creatorEmail = isset($creatorInfo['email']) ? $creatorInfo['email'] : '';
  206. $hook = HookCreateUser::create();
  207. if (!empty($hook)) {
  208. $hook->notifyCreateUser(HOOK_EVENT_TYPE_PRE);
  209. }
  210. // First check wether the login already exists
  211. if (!self::is_username_available($loginName)) {
  212. Display::addFlash(
  213. Display::return_message(get_lang('LoginAlreadyTaken'))
  214. );
  215. return false;
  216. }
  217. global $_configuration;
  218. $original_password = $password;
  219. $access_url_id = 1;
  220. if (api_get_multiple_access_url()) {
  221. $access_url_id = api_get_current_access_url_id();
  222. } else {
  223. // In some cases, the first access_url ID might be different from 1
  224. // for example when using a DB cluster or hacking the DB manually.
  225. // In this case, we want the first row, not necessarily "1".
  226. $dbm = Database::getManager();
  227. /** @var AccessUrlRepository $accessUrlRepository */
  228. $accessUrlRepository = $dbm->getRepository('ChamiloCoreBundle:AccessUrl');
  229. $accessUrl = $accessUrlRepository->getFirstId();
  230. if (!empty($accessUrl[0]) && !empty($accessUrl[0][1])) {
  231. $access_url_id = $accessUrl[0][1];
  232. }
  233. }
  234. if (isset($_configuration[$access_url_id]) &&
  235. is_array($_configuration[$access_url_id]) &&
  236. isset($_configuration[$access_url_id]['hosting_limit_users']) &&
  237. $_configuration[$access_url_id]['hosting_limit_users'] > 0) {
  238. $num = self::get_number_of_users();
  239. if ($num >= $_configuration[$access_url_id]['hosting_limit_users']) {
  240. api_warn_hosting_contact('hosting_limit_users');
  241. Display::addFlash(
  242. Display::return_message(
  243. get_lang('PortalUsersLimitReached'),
  244. 'warning'
  245. )
  246. );
  247. return false;
  248. }
  249. }
  250. if ($status === 1 &&
  251. isset($_configuration[$access_url_id]) &&
  252. is_array($_configuration[$access_url_id]) &&
  253. isset($_configuration[$access_url_id]['hosting_limit_teachers']) &&
  254. $_configuration[$access_url_id]['hosting_limit_teachers'] > 0
  255. ) {
  256. $num = self::get_number_of_users(1);
  257. if ($num >= $_configuration[$access_url_id]['hosting_limit_teachers']) {
  258. Display::addFlash(
  259. Display::return_message(
  260. get_lang('PortalTeachersLimitReached'),
  261. 'warning'
  262. )
  263. );
  264. api_warn_hosting_contact('hosting_limit_teachers');
  265. return false;
  266. }
  267. }
  268. if (empty($password)) {
  269. if ($authSource === PLATFORM_AUTH_SOURCE) {
  270. Display::addFlash(
  271. Display::return_message(
  272. get_lang('ThisFieldIsRequired').': '.get_lang(
  273. 'Password'
  274. ),
  275. 'warning'
  276. )
  277. );
  278. return false;
  279. }
  280. // We use the authSource as password.
  281. // The real validation will be by processed by the auth
  282. // source not Chamilo
  283. $password = $authSource;
  284. }
  285. // database table definition
  286. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  287. // Checking the user language
  288. $languages = api_get_languages();
  289. $language = strtolower($language);
  290. if (isset($languages['folder'])) {
  291. if (!in_array($language, $languages['folder'])) {
  292. $language = api_get_setting('platformLanguage');
  293. }
  294. }
  295. $currentDate = api_get_utc_datetime();
  296. $now = new DateTime();
  297. if (empty($expirationDate) || $expirationDate == '0000-00-00 00:00:00') {
  298. // Default expiration date
  299. // if there is a default duration of a valid account then
  300. // we have to change the expiration_date accordingly
  301. // Accept 0000-00-00 00:00:00 as a null value to avoid issues with
  302. // third party code using this method with the previous (pre-1.10)
  303. // value of 0000...
  304. if (api_get_setting('account_valid_duration') != '') {
  305. $expirationDate = new DateTime($currentDate);
  306. $days = (int) api_get_setting('account_valid_duration');
  307. $expirationDate->modify('+'.$days.' day');
  308. }
  309. } else {
  310. $expirationDate = api_get_utc_datetime($expirationDate);
  311. $expirationDate = new \DateTime($expirationDate, new DateTimeZone('UTC'));
  312. }
  313. $userManager = self::getManager();
  314. /** @var User $user */
  315. $user = $userManager->createUser();
  316. $user
  317. ->setLastname($lastName)
  318. ->setFirstname($firstName)
  319. ->setUsername($loginName)
  320. ->setStatus($status)
  321. ->setPlainPassword($password)
  322. ->setEmail($email)
  323. ->setOfficialCode($official_code)
  324. ->setPictureUri($picture_uri)
  325. ->setCreatorId($creatorId)
  326. ->setAuthSource($authSource)
  327. ->setPhone($phone)
  328. ->setAddress($address)
  329. ->setLanguage($language)
  330. ->setRegistrationDate($now)
  331. ->setHrDeptId($hr_dept_id)
  332. ->setActive($active)
  333. ->setEnabled($active)
  334. ;
  335. if (!empty($expirationDate)) {
  336. $user->setExpirationDate($expirationDate);
  337. }
  338. $userManager->updateUser($user);
  339. $userId = $user->getId();
  340. if (!empty($userId)) {
  341. $return = $userId;
  342. $sql = "UPDATE $table_user SET user_id = $return WHERE id = $return";
  343. Database::query($sql);
  344. if ($isAdmin) {
  345. self::add_user_as_admin($user);
  346. }
  347. if (api_get_multiple_access_url()) {
  348. UrlManager::add_user_to_url($userId, api_get_current_access_url_id());
  349. } else {
  350. //we are adding by default the access_url_user table with access_url_id = 1
  351. UrlManager::add_user_to_url($userId, 1);
  352. }
  353. $extra['item_id'] = $userId;
  354. if (is_array($extra) && count($extra) > 0) {
  355. $courseFieldValue = new ExtraFieldValue('user');
  356. $courseFieldValue->saveFieldValues($extra);
  357. } else {
  358. // Create notify settings by default
  359. self::update_extra_field_value(
  360. $userId,
  361. 'mail_notify_invitation',
  362. '1'
  363. );
  364. self::update_extra_field_value(
  365. $userId,
  366. 'mail_notify_message',
  367. '1'
  368. );
  369. self::update_extra_field_value(
  370. $userId,
  371. 'mail_notify_group_message',
  372. '1'
  373. );
  374. }
  375. self::update_extra_field_value(
  376. $userId,
  377. 'already_logged_in',
  378. 'false'
  379. );
  380. if (!empty($redirectToURLAfterLogin) && api_get_configuration_value('plugin_redirection_enabled')) {
  381. RedirectionPlugin::insert($userId, $redirectToURLAfterLogin);
  382. }
  383. if (!empty($email) && $send_mail) {
  384. $recipient_name = api_get_person_name(
  385. $firstName,
  386. $lastName,
  387. null,
  388. PERSON_NAME_EMAIL_ADDRESS
  389. );
  390. $tplSubject = new Template(
  391. null,
  392. false,
  393. false,
  394. false,
  395. false,
  396. false
  397. );
  398. $layoutSubject = $tplSubject->get_template('mail/subject_registration_platform.tpl');
  399. $emailSubject = $tplSubject->fetch($layoutSubject);
  400. $sender_name = api_get_person_name(
  401. api_get_setting('administratorName'),
  402. api_get_setting('administratorSurname'),
  403. null,
  404. PERSON_NAME_EMAIL_ADDRESS
  405. );
  406. $email_admin = api_get_setting('emailAdministrator');
  407. $url = api_get_path(WEB_PATH);
  408. if (api_is_multiple_url_enabled()) {
  409. $access_url_id = api_get_current_access_url_id();
  410. if ($access_url_id != -1) {
  411. $urlInfo = api_get_access_url($access_url_id);
  412. if ($urlInfo) {
  413. $url = $urlInfo['url'];
  414. }
  415. }
  416. }
  417. $tplContent = new Template(
  418. null,
  419. false,
  420. false,
  421. false,
  422. false,
  423. false
  424. );
  425. // variables for the default template
  426. $tplContent->assign('complete_name', stripslashes(api_get_person_name($firstName, $lastName)));
  427. $tplContent->assign('login_name', $loginName);
  428. $tplContent->assign('original_password', stripslashes($original_password));
  429. $tplContent->assign('mailWebPath', $url);
  430. $tplContent->assign('new_user', $user);
  431. // ofaj
  432. $urlSearch = api_get_path(WEB_PATH).'search.php';
  433. $linkSearch = Display::url($urlSearch, $urlSearch);
  434. $tplContent->assign('search_link', $linkSearch);
  435. $layoutContent = $tplContent->get_template('mail/content_registration_platform.tpl');
  436. $emailBody = $tplContent->fetch($layoutContent);
  437. $userInfo = api_get_user_info($userId);
  438. $mailTemplateManager = new MailTemplateManager();
  439. /* MANAGE EVENT WITH MAIL */
  440. if (EventsMail::check_if_using_class('user_registration')) {
  441. $values["about_user"] = $return;
  442. $values["password"] = $original_password;
  443. $values["send_to"] = [$return];
  444. $values["prior_lang"] = null;
  445. EventsDispatcher::events('user_registration', $values);
  446. } else {
  447. $phoneNumber = isset($extra['mobile_phone_number']) ? $extra['mobile_phone_number'] : null;
  448. $additionalParameters = [
  449. 'smsType' => SmsPlugin::WELCOME_LOGIN_PASSWORD,
  450. 'userId' => $return,
  451. 'mobilePhoneNumber' => $phoneNumber,
  452. 'password' => $original_password,
  453. ];
  454. $emailBodyTemplate = '';
  455. if (!empty($emailTemplate)) {
  456. if (isset($emailTemplate['content_registration_platform.tpl']) &&
  457. !empty($emailTemplate['content_registration_platform.tpl'])
  458. ) {
  459. $emailBodyTemplate = $mailTemplateManager->parseTemplate(
  460. $emailTemplate['content_registration_platform.tpl'],
  461. $userInfo
  462. );
  463. }
  464. }
  465. $twoEmail = api_get_configuration_value('send_two_inscription_confirmation_mail');
  466. if ($twoEmail === true) {
  467. $layoutContent = $tplContent->get_template('mail/new_user_first_email_confirmation.tpl');
  468. $emailBody = $tplContent->fetch($layoutContent);
  469. if (!empty($emailBodyTemplate) &&
  470. isset($emailTemplate['new_user_first_email_confirmation.tpl']) &&
  471. !empty($emailTemplate['new_user_first_email_confirmation.tpl'])
  472. ) {
  473. $emailBody = $mailTemplateManager->parseTemplate(
  474. $emailTemplate['new_user_first_email_confirmation.tpl'],
  475. $userInfo
  476. );
  477. }
  478. api_mail_html(
  479. $recipient_name,
  480. $email,
  481. $emailSubject,
  482. $emailBody,
  483. $sender_name,
  484. $email_admin,
  485. null,
  486. null,
  487. null,
  488. $additionalParameters,
  489. $creatorEmail
  490. );
  491. $layoutContent = $tplContent->get_template('mail/new_user_second_email_confirmation.tpl');
  492. $emailBody = $tplContent->fetch($layoutContent);
  493. if (!empty($emailBodyTemplate) &&
  494. isset($emailTemplate['new_user_second_email_confirmation.tpl']) &&
  495. !empty($emailTemplate['new_user_second_email_confirmation.tpl'])
  496. ) {
  497. $emailBody = $mailTemplateManager->parseTemplate(
  498. $emailTemplate['new_user_second_email_confirmation.tpl'],
  499. $userInfo
  500. );
  501. }
  502. api_mail_html(
  503. $recipient_name,
  504. $email,
  505. $emailSubject,
  506. $emailBody,
  507. $sender_name,
  508. $email_admin,
  509. null,
  510. null,
  511. null,
  512. $additionalParameters,
  513. $creatorEmail
  514. );
  515. } else {
  516. if (!empty($emailBodyTemplate)) {
  517. $emailBody = $emailBodyTemplate;
  518. }
  519. $sendToInbox = api_get_configuration_value('send_inscription_msg_to_inbox');
  520. if ($sendToInbox) {
  521. $adminList = self::get_all_administrators();
  522. $senderId = 1;
  523. if (!empty($adminList)) {
  524. $adminInfo = current($adminList);
  525. $senderId = $adminInfo['user_id'];
  526. }
  527. MessageManager::send_message_simple(
  528. $userId,
  529. $emailSubject,
  530. $emailBody,
  531. $senderId
  532. );
  533. } else {
  534. api_mail_html(
  535. $recipient_name,
  536. $email,
  537. $emailSubject,
  538. $emailBody,
  539. $sender_name,
  540. $email_admin,
  541. null,
  542. null,
  543. null,
  544. $additionalParameters,
  545. $creatorEmail
  546. );
  547. }
  548. }
  549. $notification = api_get_configuration_value('send_notification_when_user_added');
  550. if (!empty($notification) && isset($notification['admins']) && is_array($notification['admins'])) {
  551. foreach ($notification['admins'] as $adminId) {
  552. $emailSubjectToAdmin = get_lang('UserAdded').': '.api_get_person_name($firstName, $lastName);
  553. MessageManager::send_message_simple($adminId, $emailSubjectToAdmin, $emailBody, $userId);
  554. }
  555. }
  556. }
  557. if ($sendEmailToAllAdmins) {
  558. $adminList = self::get_all_administrators();
  559. $tplContent = new Template(
  560. null,
  561. false,
  562. false,
  563. false,
  564. false,
  565. false
  566. );
  567. // variables for the default template
  568. $tplContent->assign('complete_name', stripslashes(api_get_person_name($firstName, $lastName)));
  569. $langData = api_get_language_info(api_get_language_id($user->getLanguage()));
  570. $user->setLanguage($langData['original_name']);
  571. $renderer = FormValidator::getDefaultRenderer();
  572. $tplContent->assign('user_added', $user);
  573. // Form template
  574. $elementTemplate = ' {label}: {element} <br />';
  575. $renderer->setElementTemplate($elementTemplate);
  576. /** @var FormValidator $form */
  577. $form->freeze(null, $elementTemplate);
  578. $form->removeElement('submit');
  579. // $form->removeElement('password');
  580. $form->removeElement('pass1');
  581. $form->removeElement('pass2');
  582. $form->removeElement('search');
  583. $formData = $form->returnForm();
  584. $url = api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$user->getId();
  585. $tplContent->assign('link', Display::url($url, $url));
  586. $tplContent->assign('form', $formData);
  587. $layoutContent = $tplContent->get_template('mail/content_registration_platform_to_admin.tpl');
  588. $emailBody = $tplContent->fetch($layoutContent);
  589. if (!empty($emailBodyTemplate) &&
  590. isset($emailTemplate['content_registration_platform_to_admin.tpl']) &&
  591. !empty($emailTemplate['content_registration_platform_to_admin.tpl'])
  592. ) {
  593. $emailBody = $mailTemplateManager->parseTemplate(
  594. $emailTemplate['content_registration_platform_to_admin.tpl'],
  595. $userInfo
  596. );
  597. }
  598. $subject = '['.api_get_setting('siteName').'] '.get_lang('NewStudentRegistered');
  599. foreach ($adminList as $adminId => $data) {
  600. MessageManager::send_message_simple(
  601. $adminId,
  602. $subject,
  603. $emailBody,
  604. $userId
  605. );
  606. }
  607. }
  608. /* ENDS MANAGE EVENT WITH MAIL */
  609. }
  610. if (!empty($hook)) {
  611. $hook->setEventData([
  612. 'return' => $userId,
  613. 'originalPassword' => $original_password,
  614. ]);
  615. $hook->notifyCreateUser(HOOK_EVENT_TYPE_POST);
  616. }
  617. Event::addEvent(LOG_USER_CREATE, LOG_USER_ID, $userId);
  618. } else {
  619. Display::addFlash(
  620. Display::return_message(get_lang('ErrorContactPlatformAdmin'))
  621. );
  622. return false;
  623. }
  624. return $return;
  625. }
  626. /**
  627. * Can user be deleted? This function checks whether there's a course
  628. * in which the given user is the
  629. * only course administrator. If that is the case, the user can't be
  630. * deleted because the course would remain without a course admin.
  631. *
  632. * @param int $user_id The user id
  633. *
  634. * @return bool true if user can be deleted
  635. *
  636. * @assert (null) === false
  637. * @assert (-1) === false
  638. * @assert ('abc') === false
  639. */
  640. public static function canDeleteUser($user_id)
  641. {
  642. $deny = api_get_configuration_value('deny_delete_users');
  643. if ($deny) {
  644. return false;
  645. }
  646. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  647. $user_id = (int) $user_id;
  648. if (empty($user_id)) {
  649. return false;
  650. }
  651. $sql = "SELECT * FROM $table_course_user
  652. WHERE status = 1 AND user_id = ".$user_id;
  653. $res = Database::query($sql);
  654. while ($course = Database::fetch_object($res)) {
  655. $sql = "SELECT id FROM $table_course_user
  656. WHERE status=1 AND c_id = ".intval($course->c_id);
  657. $res2 = Database::query($sql);
  658. if (Database::num_rows($res2) == 1) {
  659. return false;
  660. }
  661. }
  662. return true;
  663. }
  664. /**
  665. * Delete a user from the platform, and all its belongings. This is a
  666. * very dangerous function that should only be accessible by
  667. * super-admins. Other roles should only be able to disable a user,
  668. * which removes access to the platform but doesn't delete anything.
  669. *
  670. * @param int The ID of th user to be deleted
  671. *
  672. * @throws Exception
  673. *
  674. * @return bool true if user is successfully deleted, false otherwise
  675. * @assert (null) === false
  676. * @assert ('abc') === false
  677. */
  678. public static function delete_user($user_id)
  679. {
  680. $user_id = (int) $user_id;
  681. if (empty($user_id)) {
  682. return false;
  683. }
  684. if (!self::canDeleteUser($user_id)) {
  685. return false;
  686. }
  687. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  688. $usergroup_rel_user = Database::get_main_table(TABLE_USERGROUP_REL_USER);
  689. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  690. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  691. $table_session = Database::get_main_table(TABLE_MAIN_SESSION);
  692. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  693. $table_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  694. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  695. $table_group = Database::get_course_table(TABLE_GROUP_USER);
  696. $table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  697. // Unsubscribe the user from all groups in all his courses
  698. $sql = "SELECT c.id
  699. FROM $table_course c
  700. INNER JOIN $table_course_user cu
  701. ON (c.id = cu.c_id)
  702. WHERE
  703. cu.user_id = '".$user_id."' AND
  704. relation_type<>".COURSE_RELATION_TYPE_RRHH."
  705. ";
  706. $res = Database::query($sql);
  707. while ($course = Database::fetch_object($res)) {
  708. $sql = "DELETE FROM $table_group
  709. WHERE c_id = {$course->id} AND user_id = $user_id";
  710. Database::query($sql);
  711. }
  712. // Unsubscribe user from usergroup_rel_user
  713. $sql = "DELETE FROM $usergroup_rel_user WHERE user_id = '".$user_id."'";
  714. Database::query($sql);
  715. // Unsubscribe user from all courses
  716. $sql = "DELETE FROM $table_course_user WHERE user_id = '".$user_id."'";
  717. Database::query($sql);
  718. // Unsubscribe user from all courses in sessions
  719. $sql = "DELETE FROM $table_session_course_user WHERE user_id = '".$user_id."'";
  720. Database::query($sql);
  721. // If the user was added as a id_coach then set the current admin as coach see BT#
  722. $currentUserId = api_get_user_id();
  723. $sql = "UPDATE $table_session SET id_coach = $currentUserId
  724. WHERE id_coach = '".$user_id."'";
  725. Database::query($sql);
  726. $sql = "UPDATE $table_session SET id_coach = $currentUserId
  727. WHERE session_admin_id = '".$user_id."'";
  728. Database::query($sql);
  729. // Unsubscribe user from all sessions
  730. $sql = "DELETE FROM $table_session_user
  731. WHERE user_id = '".$user_id."'";
  732. Database::query($sql);
  733. if (api_get_configuration_value('plugin_redirection_enabled')) {
  734. RedirectionPlugin::deleteUserRedirection($user_id);
  735. }
  736. // Delete user picture
  737. /* TODO: Logic about api_get_setting('split_users_upload_directory') == 'true'
  738. a user has 4 different sized photos to be deleted. */
  739. $user_info = api_get_user_info($user_id);
  740. if (strlen($user_info['picture_uri']) > 0) {
  741. $path = self::getUserPathById($user_id, 'system');
  742. $img_path = $path.$user_info['picture_uri'];
  743. if (file_exists($img_path)) {
  744. unlink($img_path);
  745. }
  746. }
  747. // Delete the personal course categories
  748. $course_cat_table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  749. $sql = "DELETE FROM $course_cat_table WHERE user_id = '".$user_id."'";
  750. Database::query($sql);
  751. // Delete user from the admin table
  752. $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'";
  753. Database::query($sql);
  754. // Delete the personal agenda-items from this user
  755. $agenda_table = Database::get_main_table(TABLE_PERSONAL_AGENDA);
  756. $sql = "DELETE FROM $agenda_table WHERE user = '".$user_id."'";
  757. Database::query($sql);
  758. $gradebook_results_table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  759. $sql = 'DELETE FROM '.$gradebook_results_table.' WHERE user_id = '.$user_id;
  760. Database::query($sql);
  761. $extraFieldValue = new ExtraFieldValue('user');
  762. $extraFieldValue->deleteValuesByItem($user_id);
  763. UrlManager::deleteUserFromAllUrls($user_id);
  764. if (api_get_setting('allow_social_tool') === 'true') {
  765. $userGroup = new UserGroup();
  766. //Delete user from portal groups
  767. $group_list = $userGroup->get_groups_by_user($user_id);
  768. if (!empty($group_list)) {
  769. foreach ($group_list as $group_id => $data) {
  770. $userGroup->delete_user_rel_group($user_id, $group_id);
  771. }
  772. }
  773. // Delete user from friend lists
  774. SocialManager::remove_user_rel_user($user_id, true);
  775. }
  776. // Removing survey invitation
  777. SurveyManager::delete_all_survey_invitations_by_user($user_id);
  778. // Delete students works
  779. $sql = "DELETE FROM $table_work WHERE user_id = $user_id AND c_id <> 0";
  780. Database::query($sql);
  781. $sql = "UPDATE c_item_property SET to_user_id = NULL
  782. WHERE to_user_id = '".$user_id."'";
  783. Database::query($sql);
  784. $sql = "UPDATE c_item_property SET insert_user_id = NULL
  785. WHERE insert_user_id = '".$user_id."'";
  786. Database::query($sql);
  787. $sql = "UPDATE c_item_property SET lastedit_user_id = NULL
  788. WHERE lastedit_user_id = '".$user_id."'";
  789. Database::query($sql);
  790. // Skills
  791. $em = Database::getManager();
  792. $criteria = ['user' => $user_id];
  793. $skills = $em->getRepository('ChamiloCoreBundle:SkillRelUser')->findBy($criteria);
  794. if ($skills) {
  795. /** @var SkillRelUser $skill */
  796. foreach ($skills as $skill) {
  797. $comments = $skill->getComments();
  798. if ($comments) {
  799. /** @var SkillRelUserComment $comment */
  800. foreach ($comments as $comment) {
  801. $em->remove($comment);
  802. }
  803. }
  804. $em->remove($skill);
  805. }
  806. $em->flush();
  807. }
  808. // ExtraFieldSavedSearch
  809. $criteria = ['user' => $user_id];
  810. $searchList = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findBy($criteria);
  811. if ($searchList) {
  812. foreach ($searchList as $search) {
  813. $em->remove($search);
  814. }
  815. $em->flush();
  816. }
  817. $connection = Database::getManager()->getConnection();
  818. $tableExists = $connection->getSchemaManager()->tablesExist(['plugin_bbb_room']);
  819. if ($tableExists) {
  820. // Delete user from database
  821. $sql = "DELETE FROM plugin_bbb_room WHERE participant_id = $user_id";
  822. Database::query($sql);
  823. }
  824. // Delete user/ticket relationships :(
  825. $tableExists = $connection->getSchemaManager()->tablesExist(['ticket_ticket']);
  826. if ($tableExists) {
  827. TicketManager::deleteUserFromTicketSystem($user_id);
  828. }
  829. $tableExists = $connection->getSchemaManager()->tablesExist(['c_lp_category_user']);
  830. if ($tableExists) {
  831. $sql = "DELETE FROM c_lp_category_user WHERE user_id = $user_id";
  832. Database::query($sql);
  833. }
  834. $app_plugin = new AppPlugin();
  835. $app_plugin->performActionsWhenDeletingItem('user', $user_id);
  836. // Delete user from database
  837. $sql = "DELETE FROM $table_user WHERE id = '".$user_id."'";
  838. Database::query($sql);
  839. // Add event to system log
  840. $user_id_manager = api_get_user_id();
  841. Event::addEvent(
  842. LOG_USER_DELETE,
  843. LOG_USER_ID,
  844. $user_id,
  845. api_get_utc_datetime(),
  846. $user_id_manager
  847. );
  848. Event::addEvent(
  849. LOG_USER_DELETE,
  850. LOG_USER_OBJECT,
  851. $user_info,
  852. api_get_utc_datetime(),
  853. $user_id_manager
  854. );
  855. $cacheAvailable = api_get_configuration_value('apc');
  856. if ($cacheAvailable === true) {
  857. $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$user_id;
  858. if (apcu_exists($apcVar)) {
  859. apcu_delete($apcVar);
  860. }
  861. }
  862. return true;
  863. }
  864. /**
  865. * Deletes users completely. Can be called either as:
  866. * - UserManager::delete_users(1, 2, 3); or
  867. * - UserManager::delete_users(array(1, 2, 3));.
  868. *
  869. * @param array|int $ids
  870. *
  871. * @return bool True if at least one user was successfuly deleted. False otherwise.
  872. *
  873. * @author Laurent Opprecht
  874. *
  875. * @uses \UserManager::delete_user() to actually delete each user
  876. * @assert (null) === false
  877. * @assert (-1) === false
  878. * @assert (array(-1)) === false
  879. */
  880. public static function delete_users($ids = [])
  881. {
  882. $result = false;
  883. $ids = is_array($ids) ? $ids : func_get_args();
  884. if (!is_array($ids) || count($ids) == 0) {
  885. return false;
  886. }
  887. $ids = array_map('intval', $ids);
  888. foreach ($ids as $id) {
  889. if (empty($id) || $id < 1) {
  890. continue;
  891. }
  892. $deleted = self::delete_user($id);
  893. $result = $deleted || $result;
  894. }
  895. return $result;
  896. }
  897. /**
  898. * Disable users. Can be called either as:
  899. * - UserManager::deactivate_users(1, 2, 3);
  900. * - UserManager::deactivate_users(array(1, 2, 3));.
  901. *
  902. * @param array|int $ids
  903. *
  904. * @return bool
  905. *
  906. * @author Laurent Opprecht
  907. * @assert (null) === false
  908. * @assert (array(-1)) === false
  909. */
  910. public static function deactivate_users($ids = [])
  911. {
  912. if (empty($ids)) {
  913. return false;
  914. }
  915. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  916. $ids = is_array($ids) ? $ids : func_get_args();
  917. $ids = array_map('intval', $ids);
  918. $ids = implode(',', $ids);
  919. $sql = "UPDATE $table_user SET active = 0 WHERE id IN ($ids)";
  920. $r = Database::query($sql);
  921. if ($r !== false) {
  922. Event::addEvent(LOG_USER_DISABLE, LOG_USER_ID, $ids);
  923. return true;
  924. }
  925. return false;
  926. }
  927. /**
  928. * Enable users. Can be called either as:
  929. * - UserManager::activate_users(1, 2, 3);
  930. * - UserManager::activate_users(array(1, 2, 3));.
  931. *
  932. * @param array|int IDs of the users to enable
  933. *
  934. * @return bool
  935. *
  936. * @author Laurent Opprecht
  937. * @assert (null) === false
  938. * @assert (array(-1)) === false
  939. */
  940. public static function activate_users($ids = [])
  941. {
  942. if (empty($ids)) {
  943. return false;
  944. }
  945. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  946. $ids = is_array($ids) ? $ids : func_get_args();
  947. $ids = array_map('intval', $ids);
  948. $ids = implode(',', $ids);
  949. $sql = "UPDATE $table_user SET active = 1 WHERE id IN ($ids)";
  950. $r = Database::query($sql);
  951. if ($r !== false) {
  952. Event::addEvent(LOG_USER_ENABLE, LOG_USER_ID, $ids);
  953. return true;
  954. }
  955. return false;
  956. }
  957. /**
  958. * Update user information with new openid.
  959. *
  960. * @param int $user_id
  961. * @param string $openid
  962. *
  963. * @return bool true if the user information was updated
  964. * @assert (false,'') === false
  965. * @assert (-1,'') === false
  966. */
  967. public static function update_openid($user_id, $openid)
  968. {
  969. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  970. if ($user_id != strval(intval($user_id))) {
  971. return false;
  972. }
  973. if ($user_id === false) {
  974. return false;
  975. }
  976. $sql = "UPDATE $table_user SET
  977. openid='".Database::escape_string($openid)."'";
  978. $sql .= " WHERE id= $user_id";
  979. if (Database::query($sql) !== false) {
  980. return true;
  981. }
  982. return false;
  983. }
  984. /**
  985. * Update user information with all the parameters passed to this function.
  986. *
  987. * @param int $user_id The ID of the user to be updated
  988. * @param string $firstname The user's firstname
  989. * @param string $lastname The user's lastname
  990. * @param string $username The user's username (login)
  991. * @param string $password The user's password
  992. * @param string $auth_source The authentication source (default: "platform")
  993. * @param string $email The user's e-mail address
  994. * @param int $status The user's status
  995. * @param string $official_code The user's official code (usually just an internal institutional code)
  996. * @param string $phone The user's phone number
  997. * @param string $picture_uri The user's picture URL (internal to the Chamilo directory)
  998. * @param string $expiration_date The date at which this user will be automatically disabled
  999. * @param int $active Whether this account needs to be enabled (1) or disabled (0)
  1000. * @param int $creator_id The user ID of the person who registered this user (optional, defaults to null)
  1001. * @param int $hr_dept_id The department of HR in which the user is registered (optional, defaults to 0)
  1002. * @param array $extra Additional fields to add to this user as extra fields (defaults to null)
  1003. * @param string $language The language to which the user account will be set
  1004. * @param string $encrypt_method The cipher method. This parameter is deprecated. It will use the system's default
  1005. * @param bool $send_email Whether to send an e-mail to the user after the update is complete
  1006. * @param int $reset_password Method used to reset password (0, 1, 2 or 3 - see usage examples for details)
  1007. * @param string $address
  1008. * @param array $emailTemplate
  1009. *
  1010. * @return bool|int False on error, or the user ID if the user information was updated
  1011. * @assert (false, false, false, false, false, false, false, false, false, false, false, false, false) === false
  1012. */
  1013. public static function update_user(
  1014. $user_id,
  1015. $firstname,
  1016. $lastname,
  1017. $username,
  1018. $password = null,
  1019. $auth_source = null,
  1020. $email,
  1021. $status,
  1022. $official_code,
  1023. $phone,
  1024. $picture_uri,
  1025. $expiration_date,
  1026. $active,
  1027. $creator_id = null,
  1028. $hr_dept_id = 0,
  1029. $extra = null,
  1030. $language = 'english',
  1031. $encrypt_method = '',
  1032. $send_email = false,
  1033. $reset_password = 0,
  1034. $address = null,
  1035. $emailTemplate = []
  1036. ) {
  1037. $hook = HookUpdateUser::create();
  1038. if (!empty($hook)) {
  1039. $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE);
  1040. }
  1041. $original_password = $password;
  1042. $user_id = (int) $user_id;
  1043. $creator_id = (int) $creator_id;
  1044. if (empty($user_id)) {
  1045. return false;
  1046. }
  1047. $userManager = self::getManager();
  1048. /** @var User $user */
  1049. $user = self::getRepository()->find($user_id);
  1050. if (empty($user)) {
  1051. return false;
  1052. }
  1053. if ($reset_password == 0) {
  1054. $password = null;
  1055. $auth_source = $user->getAuthSource();
  1056. } elseif ($reset_password == 1) {
  1057. $original_password = $password = api_generate_password();
  1058. $auth_source = PLATFORM_AUTH_SOURCE;
  1059. } elseif ($reset_password == 2) {
  1060. //$password = $password;
  1061. $auth_source = PLATFORM_AUTH_SOURCE;
  1062. } elseif ($reset_password == 3) {
  1063. //$password = $password;
  1064. //$auth_source = $auth_source;
  1065. }
  1066. // Checking the user language
  1067. $languages = api_get_languages();
  1068. if (!in_array($language, $languages['folder'])) {
  1069. $language = api_get_setting('platformLanguage');
  1070. }
  1071. $change_active = 0;
  1072. $isUserActive = $user->getActive();
  1073. if ($isUserActive != $active) {
  1074. $change_active = 1;
  1075. }
  1076. $originalUsername = $user->getUsername();
  1077. // If username is different from original then check if it exists.
  1078. if ($originalUsername !== $username) {
  1079. $available = self::is_username_available($username);
  1080. if ($available === false) {
  1081. return false;
  1082. }
  1083. }
  1084. if (!empty($expiration_date)) {
  1085. $expiration_date = api_get_utc_datetime($expiration_date);
  1086. $expiration_date = new \DateTime(
  1087. $expiration_date,
  1088. new DateTimeZone('UTC')
  1089. );
  1090. }
  1091. $user
  1092. ->setLastname($lastname)
  1093. ->setFirstname($firstname)
  1094. ->setUsername($username)
  1095. ->setStatus($status)
  1096. ->setAuthSource($auth_source)
  1097. ->setLanguage($language)
  1098. ->setEmail($email)
  1099. ->setOfficialCode($official_code)
  1100. ->setPhone($phone)
  1101. ->setAddress($address)
  1102. ->setPictureUri($picture_uri)
  1103. ->setExpirationDate($expiration_date)
  1104. ->setActive($active)
  1105. ->setEnabled($active)
  1106. ->setHrDeptId($hr_dept_id)
  1107. ;
  1108. if (!is_null($password)) {
  1109. $user->setPlainPassword($password);
  1110. }
  1111. $userManager->updateUser($user, true);
  1112. if ($change_active == 1) {
  1113. if ($active == 1) {
  1114. $event_title = LOG_USER_ENABLE;
  1115. } else {
  1116. $event_title = LOG_USER_DISABLE;
  1117. }
  1118. Event::addEvent($event_title, LOG_USER_ID, $user_id);
  1119. }
  1120. if (is_array($extra) && count($extra) > 0) {
  1121. $res = true;
  1122. foreach ($extra as $fname => $fvalue) {
  1123. $res = $res && self::update_extra_field_value(
  1124. $user_id,
  1125. $fname,
  1126. $fvalue
  1127. );
  1128. }
  1129. }
  1130. if (!empty($email) && $send_email) {
  1131. $recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS);
  1132. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  1133. $sender_name = api_get_person_name(
  1134. api_get_setting('administratorName'),
  1135. api_get_setting('administratorSurname'),
  1136. null,
  1137. PERSON_NAME_EMAIL_ADDRESS
  1138. );
  1139. $email_admin = api_get_setting('emailAdministrator');
  1140. $url = api_get_path(WEB_PATH);
  1141. if (api_is_multiple_url_enabled()) {
  1142. $access_url_id = api_get_current_access_url_id();
  1143. if ($access_url_id != -1) {
  1144. $url = api_get_access_url($access_url_id);
  1145. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".
  1146. get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".
  1147. get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n".
  1148. get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n".
  1149. get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$url['url']."\n\n".
  1150. get_lang('Problem')."\n\n".
  1151. get_lang('SignatureFormula').",\n\n".
  1152. api_get_person_name(
  1153. api_get_setting('administratorName'),
  1154. api_get_setting('administratorSurname')
  1155. )."\n".
  1156. get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".
  1157. get_lang('Email')." : ".api_get_setting('emailAdministrator');
  1158. }
  1159. } else {
  1160. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".
  1161. get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".
  1162. get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n".
  1163. get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n".
  1164. get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".api_get_path(WEB_PATH)."\n\n".
  1165. get_lang('Problem')."\n\n".
  1166. get_lang('SignatureFormula').",\n\n".
  1167. api_get_person_name(
  1168. api_get_setting('administratorName'),
  1169. api_get_setting('administratorSurname')
  1170. )."\n".
  1171. get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".
  1172. get_lang('Email')." : ".api_get_setting('emailAdministrator');
  1173. }
  1174. $emailbody = nl2br($emailbody);
  1175. api_mail_html(
  1176. $recipient_name,
  1177. $email,
  1178. $emailsubject,
  1179. $emailbody,
  1180. $sender_name,
  1181. $email_admin
  1182. );
  1183. }
  1184. if (!empty($hook)) {
  1185. $hook->setEventData(['user' => $user]);
  1186. $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST);
  1187. }
  1188. $cacheAvailable = api_get_configuration_value('apc');
  1189. if ($cacheAvailable === true) {
  1190. $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$user_id;
  1191. if (apcu_exists($apcVar)) {
  1192. apcu_delete($apcVar);
  1193. }
  1194. }
  1195. return $user->getId();
  1196. }
  1197. /**
  1198. * Disables a user.
  1199. *
  1200. * @param int User id
  1201. *
  1202. * @return bool
  1203. *
  1204. * @uses \UserManager::change_active_state() to actually disable the user
  1205. * @assert (0) === false
  1206. */
  1207. public static function disable($user_id)
  1208. {
  1209. if (empty($user_id)) {
  1210. return false;
  1211. }
  1212. self::change_active_state($user_id, 0);
  1213. return true;
  1214. }
  1215. /**
  1216. * Enable a user.
  1217. *
  1218. * @param int User id
  1219. *
  1220. * @return bool
  1221. *
  1222. * @uses \UserManager::change_active_state() to actually disable the user
  1223. * @assert (0) === false
  1224. */
  1225. public static function enable($user_id)
  1226. {
  1227. if (empty($user_id)) {
  1228. return false;
  1229. }
  1230. self::change_active_state($user_id, 1);
  1231. return true;
  1232. }
  1233. /**
  1234. * Returns the user's id based on the original id and field name in
  1235. * the extra fields. Returns 0 if no user was found. This function is
  1236. * mostly useful in the context of a web services-based sinchronization.
  1237. *
  1238. * @param string Original user id
  1239. * @param string Original field name
  1240. *
  1241. * @return int User id
  1242. * @assert ('0','---') === 0
  1243. */
  1244. public static function get_user_id_from_original_id(
  1245. $original_user_id_value,
  1246. $original_user_id_name
  1247. ) {
  1248. $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD);
  1249. $t_ufv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  1250. $extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  1251. $original_user_id_name = Database::escape_string($original_user_id_name);
  1252. $original_user_id_value = Database::escape_string($original_user_id_value);
  1253. $sql = "SELECT item_id as user_id
  1254. FROM $t_uf uf
  1255. INNER JOIN $t_ufv ufv
  1256. ON ufv.field_id = uf.id
  1257. WHERE
  1258. variable = '$original_user_id_name' AND
  1259. value = '$original_user_id_value' AND
  1260. extra_field_type = $extraFieldType
  1261. ";
  1262. $res = Database::query($sql);
  1263. $row = Database::fetch_object($res);
  1264. if ($row) {
  1265. return $row->user_id;
  1266. }
  1267. return 0;
  1268. }
  1269. /**
  1270. * Check if a username is available.
  1271. *
  1272. * @param string $username the wanted username
  1273. *
  1274. * @return bool true if the wanted username is available
  1275. * @assert ('') === false
  1276. * @assert ('xyzxyzxyz') === true
  1277. */
  1278. public static function is_username_available($username)
  1279. {
  1280. if (empty($username)) {
  1281. return false;
  1282. }
  1283. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1284. $sql = "SELECT username FROM $table_user
  1285. WHERE username = '".Database::escape_string($username)."'";
  1286. $res = Database::query($sql);
  1287. return Database::num_rows($res) == 0;
  1288. }
  1289. /**
  1290. * Creates a username using person's names, i.e. creates jmontoya from Julio Montoya.
  1291. *
  1292. * @param string $firstname the first name of the user
  1293. * @param string $lastname the last name of the user
  1294. *
  1295. * @return string suggests a username that contains only ASCII-letters and digits,
  1296. * without check for uniqueness within the system
  1297. *
  1298. * @author Julio Montoya Armas
  1299. * @author Ivan Tcholakov, 2009 - rework about internationalization.
  1300. * @assert ('','') === false
  1301. * @assert ('a','b') === 'ab'
  1302. */
  1303. public static function create_username($firstname, $lastname)
  1304. {
  1305. if (empty($firstname) && empty($lastname)) {
  1306. return false;
  1307. }
  1308. // The first letter only.
  1309. $firstname = api_substr(
  1310. preg_replace(USERNAME_PURIFIER, '', $firstname),
  1311. 0,
  1312. 1
  1313. );
  1314. //Looking for a space in the lastname
  1315. $pos = api_strpos($lastname, ' ');
  1316. if ($pos !== false) {
  1317. $lastname = api_substr($lastname, 0, $pos);
  1318. }
  1319. $lastname = preg_replace(USERNAME_PURIFIER, '', $lastname);
  1320. $username = $firstname.$lastname;
  1321. if (empty($username)) {
  1322. $username = 'user';
  1323. }
  1324. $username = URLify::transliterate($username);
  1325. return strtolower(substr($username, 0, USERNAME_MAX_LENGTH - 3));
  1326. }
  1327. /**
  1328. * Creates a unique username, using:
  1329. * 1. the first name and the last name of a user;
  1330. * 2. an already created username but not checked for uniqueness yet.
  1331. *
  1332. * @param string $firstname The first name of a given user. If the second parameter $lastname is NULL, then this
  1333. * parameter is treated as username which is to be checked f
  1334. * or uniqueness and to be modified when it is necessary.
  1335. * @param string $lastname the last name of the user
  1336. *
  1337. * @return string Returns a username that contains only ASCII-letters and digits and that is unique in the system.
  1338. * Note: When the method is called several times with same parameters,
  1339. * its results look like the following sequence: ivan, ivan2, ivan3, ivan4, ...
  1340. *
  1341. * @author Ivan Tcholakov, 2009
  1342. */
  1343. public static function create_unique_username($firstname, $lastname = null)
  1344. {
  1345. if (is_null($lastname)) {
  1346. // In this case the actual input parameter $firstname should contain ASCII-letters and digits only.
  1347. // For making this method tolerant of mistakes,
  1348. // let us transliterate and purify the suggested input username anyway.
  1349. // So, instead of the sentence $username = $firstname; we place the following:
  1350. $username = strtolower(preg_replace(USERNAME_PURIFIER, '', $firstname));
  1351. } else {
  1352. $username = self::create_username($firstname, $lastname);
  1353. }
  1354. if (!self::is_username_available($username)) {
  1355. $i = 2;
  1356. $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i;
  1357. while (!self::is_username_available($temp_username)) {
  1358. $i++;
  1359. $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i;
  1360. }
  1361. $username = $temp_username;
  1362. }
  1363. $username = URLify::transliterate($username);
  1364. return $username;
  1365. }
  1366. /**
  1367. * Modifies a given username accordingly to the specification for valid characters and length.
  1368. *
  1369. * @param $username string The input username
  1370. * @param bool $strict (optional) When this flag is TRUE, the result is guaranteed for full compliance,
  1371. * otherwise compliance may be partial. The default value is FALSE.
  1372. *
  1373. * @return string the resulting purified username
  1374. */
  1375. public static function purify_username($username, $strict = false)
  1376. {
  1377. if ($strict) {
  1378. // 1. Conversion of unacceptable letters (latinian letters with accents for example)
  1379. // into ASCII letters in order they not to be totally removed.
  1380. // 2. Applying the strict purifier.
  1381. // 3. Length limitation.
  1382. $return = api_get_setting('login_is_email') === 'true' ? substr(preg_replace(USERNAME_PURIFIER_MAIL, '', $username), 0, USERNAME_MAX_LENGTH) : substr(preg_replace(USERNAME_PURIFIER, '', $username), 0, USERNAME_MAX_LENGTH);
  1383. $return = URLify::transliterate($return);
  1384. return $return;
  1385. }
  1386. // 1. Applying the shallow purifier.
  1387. // 2. Length limitation.
  1388. return substr(
  1389. preg_replace(USERNAME_PURIFIER_SHALLOW, '', $username),
  1390. 0,
  1391. USERNAME_MAX_LENGTH
  1392. );
  1393. }
  1394. /**
  1395. * Checks whether the user id exists in the database.
  1396. *
  1397. * @param int $userId User id
  1398. *
  1399. * @return bool True if user id was found, false otherwise
  1400. */
  1401. public static function is_user_id_valid($userId)
  1402. {
  1403. $resultData = Database::select(
  1404. 'COUNT(1) AS count',
  1405. Database::get_main_table(TABLE_MAIN_USER),
  1406. [
  1407. 'where' => ['id = ?' => (int) $userId],
  1408. ],
  1409. 'first'
  1410. );
  1411. if ($resultData === false) {
  1412. return false;
  1413. }
  1414. return $resultData['count'] > 0;
  1415. }
  1416. /**
  1417. * Checks whether a given username matches to the specification strictly.
  1418. * The empty username is assumed here as invalid.
  1419. * Mostly this function is to be used in the user interface built-in validation routines
  1420. * for providing feedback while usernames are enterd manually.
  1421. *
  1422. * @param string $username the input username
  1423. *
  1424. * @return bool returns TRUE if the username is valid, FALSE otherwise
  1425. */
  1426. public static function is_username_valid($username)
  1427. {
  1428. return !empty($username) && $username == self::purify_username($username, true);
  1429. }
  1430. /**
  1431. * Checks whether a username is empty. If the username contains whitespace characters,
  1432. * such as spaces, tabulators, newlines, etc.,
  1433. * it is assumed as empty too. This function is safe for validation unpurified data (during importing).
  1434. *
  1435. * @param string $username the given username
  1436. *
  1437. * @return bool returns TRUE if length of the username exceeds the limit, FALSE otherwise
  1438. */
  1439. public static function is_username_empty($username)
  1440. {
  1441. return strlen(self::purify_username($username, false)) == 0;
  1442. }
  1443. /**
  1444. * Checks whether a username is too long or not.
  1445. *
  1446. * @param string $username the given username, it should contain only ASCII-letters and digits
  1447. *
  1448. * @return bool returns TRUE if length of the username exceeds the limit, FALSE otherwise
  1449. */
  1450. public static function is_username_too_long($username)
  1451. {
  1452. return strlen($username) > USERNAME_MAX_LENGTH;
  1453. }
  1454. /**
  1455. * Get the users by ID.
  1456. *
  1457. * @param array $ids student ids
  1458. * @param string $active
  1459. * @param string $order
  1460. * @param string $limit
  1461. *
  1462. * @return array $result student information
  1463. */
  1464. public static function get_user_list_by_ids($ids = [], $active = null, $order = null, $limit = null)
  1465. {
  1466. if (empty($ids)) {
  1467. return [];
  1468. }
  1469. $ids = is_array($ids) ? $ids : [$ids];
  1470. $ids = array_map('intval', $ids);
  1471. $ids = implode(',', $ids);
  1472. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1473. $sql = "SELECT * FROM $tbl_user WHERE id IN ($ids)";
  1474. if (!is_null($active)) {
  1475. $sql .= ' AND active='.($active ? '1' : '0');
  1476. }
  1477. if (!is_null($order)) {
  1478. $order = Database::escape_string($order);
  1479. $sql .= ' ORDER BY '.$order;
  1480. }
  1481. if (!is_null($limit)) {
  1482. $limit = Database::escape_string($limit);
  1483. $sql .= ' LIMIT '.$limit;
  1484. }
  1485. $rs = Database::query($sql);
  1486. $result = [];
  1487. while ($row = Database::fetch_array($rs)) {
  1488. $result[] = $row;
  1489. }
  1490. return $result;
  1491. }
  1492. /**
  1493. * Get a list of users of which the given conditions match with an = 'cond'.
  1494. *
  1495. * @param array $conditions a list of condition (example : status=>STUDENT)
  1496. * @param array $order_by a list of fields on which sort
  1497. *
  1498. * @return array an array with all users of the platform
  1499. *
  1500. * @todo security filter order by
  1501. */
  1502. public static function get_user_list(
  1503. $conditions = [],
  1504. $order_by = [],
  1505. $limit_from = false,
  1506. $limit_to = false,
  1507. $idCampus = null
  1508. ) {
  1509. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1510. $userUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  1511. $return_array = [];
  1512. $sql = "SELECT user.* FROM $user_table user ";
  1513. if (api_is_multiple_url_enabled()) {
  1514. if ($idCampus) {
  1515. $urlId = $idCampus;
  1516. } else {
  1517. $urlId = api_get_current_access_url_id();
  1518. }
  1519. $sql .= " INNER JOIN $userUrlTable url_user
  1520. ON (user.user_id = url_user.user_id)
  1521. WHERE url_user.access_url_id = $urlId";
  1522. } else {
  1523. $sql .= " WHERE 1=1 ";
  1524. }
  1525. if (count($conditions) > 0) {
  1526. foreach ($conditions as $field => $value) {
  1527. $field = Database::escape_string($field);
  1528. $value = Database::escape_string($value);
  1529. $sql .= " AND $field = '$value'";
  1530. }
  1531. }
  1532. if (count($order_by) > 0) {
  1533. $sql .= ' ORDER BY '.Database::escape_string(implode(',', $order_by));
  1534. }
  1535. if (is_numeric($limit_from) && is_numeric($limit_from)) {
  1536. $limit_from = (int) $limit_from;
  1537. $limit_to = (int) $limit_to;
  1538. $sql .= " LIMIT $limit_from, $limit_to";
  1539. }
  1540. $sql_result = Database::query($sql);
  1541. while ($result = Database::fetch_array($sql_result)) {
  1542. $result['complete_name'] = api_get_person_name($result['firstname'], $result['lastname']);
  1543. $return_array[] = $result;
  1544. }
  1545. return $return_array;
  1546. }
  1547. /**
  1548. * Get a list of users of which the given conditions match with a LIKE '%cond%'.
  1549. *
  1550. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  1551. * @param array $order_by a list of fields on which sort
  1552. * @param bool $simple_like Whether we want a simple LIKE 'abc' or a LIKE '%abc%'
  1553. * @param string $condition Whether we want the filters to be combined by AND or OR
  1554. * @param array $onlyThisUserList
  1555. *
  1556. * @return array an array with all users of the platform
  1557. *
  1558. * @todo optional course code parameter, optional sorting parameters...
  1559. * @todo security filter order_by
  1560. */
  1561. public static function getUserListLike(
  1562. $conditions = [],
  1563. $order_by = [],
  1564. $simple_like = false,
  1565. $condition = 'AND',
  1566. $onlyThisUserList = []
  1567. ) {
  1568. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1569. $tblAccessUrlRelUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  1570. $return_array = [];
  1571. $sql_query = "SELECT user.id FROM $user_table user ";
  1572. if (api_is_multiple_url_enabled()) {
  1573. $sql_query .= " INNER JOIN $tblAccessUrlRelUser auru ON auru.user_id = user.id ";
  1574. }
  1575. $sql_query .= ' WHERE 1 = 1 ';
  1576. if (count($conditions) > 0) {
  1577. $temp_conditions = [];
  1578. foreach ($conditions as $field => $value) {
  1579. $field = Database::escape_string($field);
  1580. $value = Database::escape_string($value);
  1581. if ($simple_like) {
  1582. $temp_conditions[] = $field." LIKE '$value%'";
  1583. } else {
  1584. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  1585. }
  1586. }
  1587. if (!empty($temp_conditions)) {
  1588. $sql_query .= ' AND '.implode(' '.$condition.' ', $temp_conditions);
  1589. }
  1590. if (api_is_multiple_url_enabled()) {
  1591. $sql_query .= ' AND auru.access_url_id = '.api_get_current_access_url_id();
  1592. }
  1593. } else {
  1594. if (api_is_multiple_url_enabled()) {
  1595. $sql_query .= ' AND auru.access_url_id = '.api_get_current_access_url_id();
  1596. }
  1597. }
  1598. if (!empty($onlyThisUserList)) {
  1599. $onlyThisUserListToString = implode("','", $onlyThisUserList);
  1600. $sql_query .= " AND user.id IN ('$onlyThisUserListToString') ";
  1601. }
  1602. if (count($order_by) > 0) {
  1603. $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by));
  1604. }
  1605. $sql_result = Database::query($sql_query);
  1606. while ($result = Database::fetch_array($sql_result)) {
  1607. $userInfo = api_get_user_info($result['id']);
  1608. $return_array[] = $userInfo;
  1609. }
  1610. return $return_array;
  1611. }
  1612. /**
  1613. * Get user picture URL or path from user ID (returns an array).
  1614. * The return format is a complete path, enabling recovery of the directory
  1615. * with dirname() or the file with basename(). This also works for the
  1616. * functions dealing with the user's productions, as they are located in
  1617. * the same directory.
  1618. *
  1619. * @param int $id User ID
  1620. * @param string $type Type of path to return (can be 'system', 'web')
  1621. * @param array $userInfo user information to avoid query the DB
  1622. * returns the /main/img/unknown.jpg image set it at true
  1623. *
  1624. * @return array Array of 2 elements: 'dir' and 'file' which contain
  1625. * the dir and file as the name implies if image does not exist it will
  1626. * return the unknow image if anonymous parameter is true if not it returns an empty array
  1627. */
  1628. public static function get_user_picture_path_by_id(
  1629. $id,
  1630. $type = 'web',
  1631. $userInfo = []
  1632. ) {
  1633. switch ($type) {
  1634. case 'system': // Base: absolute system path.
  1635. $base = api_get_path(SYS_CODE_PATH);
  1636. break;
  1637. case 'web': // Base: absolute web path.
  1638. default:
  1639. $base = api_get_path(WEB_CODE_PATH);
  1640. break;
  1641. }
  1642. $anonymousPath = [
  1643. 'dir' => $base.'img/',
  1644. 'file' => 'unknown.jpg',
  1645. 'email' => '',
  1646. ];
  1647. if (empty($id) || empty($type)) {
  1648. return $anonymousPath;
  1649. }
  1650. $id = (int) $id;
  1651. if (empty($userInfo)) {
  1652. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1653. $sql = "SELECT email, picture_uri FROM $user_table
  1654. WHERE id = ".$id;
  1655. $res = Database::query($sql);
  1656. if (!Database::num_rows($res)) {
  1657. return $anonymousPath;
  1658. }
  1659. $user = Database::fetch_array($res);
  1660. if (empty($user['picture_uri'])) {
  1661. return $anonymousPath;
  1662. }
  1663. } else {
  1664. $user = $userInfo;
  1665. }
  1666. $pictureFilename = trim($user['picture_uri']);
  1667. $dir = self::getUserPathById($id, $type);
  1668. return [
  1669. 'dir' => $dir,
  1670. 'file' => $pictureFilename,
  1671. 'email' => $user['email'],
  1672. ];
  1673. }
  1674. /**
  1675. * *** READ BEFORE REVIEW THIS FUNCTION ***
  1676. * This function is a exact copy from get_user_picture_path_by_id() and it was create it to avoid
  1677. * a recursive calls for get_user_picture_path_by_id() in another functions when you update a user picture
  1678. * in same script, so you can find this function usage in update_user_picture() function.
  1679. *
  1680. * @param int $id User ID
  1681. * @param string $type Type of path to return (can be 'system', 'web')
  1682. * @param array $userInfo user information to avoid query the DB
  1683. * returns the /main/img/unknown.jpg image set it at true
  1684. *
  1685. * @return array Array of 2 elements: 'dir' and 'file' which contain
  1686. * the dir and file as the name implies if image does not exist it will
  1687. * return the unknown image if anonymous parameter is true if not it returns an empty array
  1688. */
  1689. public static function getUserPicturePathById($id, $type = 'web', $userInfo = [])
  1690. {
  1691. switch ($type) {
  1692. case 'system': // Base: absolute system path.
  1693. $base = api_get_path(SYS_CODE_PATH);
  1694. break;
  1695. case 'web': // Base: absolute web path.
  1696. default:
  1697. $base = api_get_path(WEB_CODE_PATH);
  1698. break;
  1699. }
  1700. $anonymousPath = [
  1701. 'dir' => $base.'img/',
  1702. 'file' => 'unknown.jpg',
  1703. 'email' => '',
  1704. ];
  1705. if (empty($id) || empty($type)) {
  1706. return $anonymousPath;
  1707. }
  1708. $id = (int) $id;
  1709. if (empty($userInfo)) {
  1710. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1711. $sql = "SELECT email, picture_uri FROM $user_table WHERE id = $id";
  1712. $res = Database::query($sql);
  1713. if (!Database::num_rows($res)) {
  1714. return $anonymousPath;
  1715. }
  1716. $user = Database::fetch_array($res);
  1717. if (empty($user['picture_uri'])) {
  1718. return $anonymousPath;
  1719. }
  1720. } else {
  1721. $user = $userInfo;
  1722. }
  1723. $pictureFilename = trim($user['picture_uri']);
  1724. $dir = self::getUserPathById($id, $type);
  1725. return [
  1726. 'dir' => $dir,
  1727. 'file' => $pictureFilename,
  1728. 'email' => $user['email'],
  1729. ];
  1730. }
  1731. /**
  1732. * Get user path from user ID (returns an array).
  1733. * The return format is a complete path to a folder ending with "/"
  1734. * In case the first level of subdirectory of users/ does not exist, the
  1735. * function will attempt to create it. Probably not the right place to do it
  1736. * but at least it avoids headaches in many other places.
  1737. *
  1738. * @param int $id User ID
  1739. * @param string $type Type of path to return (can be 'system', 'web', 'last')
  1740. *
  1741. * @return string User folder path (i.e. /var/www/chamilo/app/upload/users/1/1/)
  1742. */
  1743. public static function getUserPathById($id, $type)
  1744. {
  1745. $id = (int) $id;
  1746. if (!$id) {
  1747. return null;
  1748. }
  1749. $userPath = "users/$id/";
  1750. if (api_get_setting('split_users_upload_directory') === 'true') {
  1751. $userPath = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/';
  1752. // In exceptional cases, on some portals, the intermediate base user
  1753. // directory might not have been created. Make sure it is before
  1754. // going further.
  1755. $rootPath = api_get_path(SYS_UPLOAD_PATH).'users/'.substr((string) $id, 0, 1);
  1756. if (!is_dir($rootPath)) {
  1757. $perm = api_get_permissions_for_new_directories();
  1758. try {
  1759. mkdir($rootPath, $perm);
  1760. } catch (Exception $e) {
  1761. error_log($e->getMessage());
  1762. }
  1763. }
  1764. }
  1765. switch ($type) {
  1766. case 'system': // Base: absolute system path.
  1767. $userPath = api_get_path(SYS_UPLOAD_PATH).$userPath;
  1768. break;
  1769. case 'web': // Base: absolute web path.
  1770. $userPath = api_get_path(WEB_UPLOAD_PATH).$userPath;
  1771. break;
  1772. case 'last': // Only the last part starting with users/
  1773. break;
  1774. }
  1775. return $userPath;
  1776. }
  1777. /**
  1778. * Gets the current user image.
  1779. *
  1780. * @param string $user_id
  1781. * @param int $size it can be USER_IMAGE_SIZE_SMALL,
  1782. * USER_IMAGE_SIZE_MEDIUM, USER_IMAGE_SIZE_BIG or USER_IMAGE_SIZE_ORIGINAL
  1783. * @param bool $addRandomId
  1784. * @param array $userInfo to avoid query the DB
  1785. *
  1786. * @return string
  1787. */
  1788. public static function getUserPicture(
  1789. $user_id,
  1790. $size = USER_IMAGE_SIZE_MEDIUM,
  1791. $addRandomId = true,
  1792. $userInfo = []
  1793. ) {
  1794. // Make sure userInfo is defined. Otherwise, define it!
  1795. if (empty($userInfo) || !is_array($userInfo) || count($userInfo) == 0) {
  1796. if (empty($user_id)) {
  1797. return '';
  1798. } else {
  1799. $userInfo = api_get_user_info($user_id);
  1800. }
  1801. }
  1802. $imageWebPath = self::get_user_picture_path_by_id(
  1803. $user_id,
  1804. 'web',
  1805. $userInfo
  1806. );
  1807. $pictureWebFile = $imageWebPath['file'];
  1808. $pictureWebDir = $imageWebPath['dir'];
  1809. $pictureAnonymousSize = '128';
  1810. $gravatarSize = 22;
  1811. $realSizeName = 'small_';
  1812. switch ($size) {
  1813. case USER_IMAGE_SIZE_SMALL:
  1814. $pictureAnonymousSize = '32';
  1815. $realSizeName = 'small_';
  1816. $gravatarSize = 32;
  1817. break;
  1818. case USER_IMAGE_SIZE_MEDIUM:
  1819. $pictureAnonymousSize = '64';
  1820. $realSizeName = 'medium_';
  1821. $gravatarSize = 64;
  1822. break;
  1823. case USER_IMAGE_SIZE_ORIGINAL:
  1824. $pictureAnonymousSize = '128';
  1825. $realSizeName = '';
  1826. $gravatarSize = 128;
  1827. break;
  1828. case USER_IMAGE_SIZE_BIG:
  1829. $pictureAnonymousSize = '128';
  1830. $realSizeName = 'big_';
  1831. $gravatarSize = 128;
  1832. break;
  1833. }
  1834. $gravatarEnabled = api_get_setting('gravatar_enabled');
  1835. $anonymousPath = Display::returnIconPath('unknown.png', $pictureAnonymousSize);
  1836. if ($pictureWebFile == 'unknown.jpg' || empty($pictureWebFile)) {
  1837. if ($gravatarEnabled === 'true') {
  1838. $file = self::getGravatar(
  1839. $imageWebPath['email'],
  1840. $gravatarSize,
  1841. api_get_setting('gravatar_type')
  1842. );
  1843. if ($addRandomId) {
  1844. $file .= '&rand='.uniqid();
  1845. }
  1846. return $file;
  1847. }
  1848. return $anonymousPath;
  1849. }
  1850. $pictureSysPath = self::get_user_picture_path_by_id($user_id, 'system');
  1851. $file = $pictureSysPath['dir'].$realSizeName.$pictureWebFile;
  1852. $picture = '';
  1853. if (file_exists($file)) {
  1854. $picture = $pictureWebDir.$realSizeName.$pictureWebFile;
  1855. } else {
  1856. $file = $pictureSysPath['dir'].$pictureWebFile;
  1857. if (file_exists($file) && !is_dir($file)) {
  1858. $picture = $pictureWebFile['dir'].$pictureWebFile;
  1859. }
  1860. }
  1861. if (empty($picture)) {
  1862. return $anonymousPath;
  1863. }
  1864. if ($addRandomId) {
  1865. $picture .= '?rand='.uniqid();
  1866. }
  1867. return $picture;
  1868. }
  1869. /**
  1870. * Creates new user photos in various sizes of a user, or deletes user photos.
  1871. * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php.
  1872. *
  1873. * @param int $user_id the user internal identification number
  1874. * @param string $file The common file name for the newly created photos.
  1875. * It will be checked and modified for compatibility with the file system.
  1876. * If full name is provided, path component is ignored.
  1877. * If an empty name is provided, then old user photos are deleted only,
  1878. *
  1879. * @see UserManager::delete_user_picture() as the prefered way for deletion.
  1880. *
  1881. * @param string $source_file the full system name of the image from which user photos will be created
  1882. * @param string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format
  1883. *
  1884. * @return bool Returns the resulting common file name of created images which usually should be stored in database.
  1885. * When deletion is requested returns empty string.
  1886. * In case of internal error or negative validation returns FALSE.
  1887. */
  1888. public static function update_user_picture(
  1889. $user_id,
  1890. $file = null,
  1891. $source_file = null,
  1892. $cropParameters = ''
  1893. ) {
  1894. if (empty($user_id)) {
  1895. return false;
  1896. }
  1897. $delete = empty($file);
  1898. if (empty($source_file)) {
  1899. $source_file = $file;
  1900. }
  1901. // User-reserved directory where photos have to be placed.
  1902. $path_info = self::getUserPicturePathById($user_id, 'system');
  1903. $path = $path_info['dir'];
  1904. // If this directory does not exist - we create it.
  1905. if (!file_exists($path)) {
  1906. mkdir($path, api_get_permissions_for_new_directories(), true);
  1907. }
  1908. // The old photos (if any).
  1909. $old_file = $path_info['file'];
  1910. // Let us delete them.
  1911. if ($old_file != 'unknown.jpg') {
  1912. if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
  1913. $prefix = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_';
  1914. @rename($path.'small_'.$old_file, $path.$prefix.'small_'.$old_file);
  1915. @rename($path.'medium_'.$old_file, $path.$prefix.'medium_'.$old_file);
  1916. @rename($path.'big_'.$old_file, $path.$prefix.'big_'.$old_file);
  1917. @rename($path.$old_file, $path.$prefix.$old_file);
  1918. } else {
  1919. @unlink($path.'small_'.$old_file);
  1920. @unlink($path.'medium_'.$old_file);
  1921. @unlink($path.'big_'.$old_file);
  1922. @unlink($path.$old_file);
  1923. }
  1924. }
  1925. // Exit if only deletion has been requested. Return an empty picture name.
  1926. if ($delete) {
  1927. return '';
  1928. }
  1929. // Validation 2.
  1930. $allowed_types = api_get_supported_image_extensions();
  1931. $file = str_replace('\\', '/', $file);
  1932. $filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file;
  1933. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  1934. if (!in_array($extension, $allowed_types)) {
  1935. return false;
  1936. }
  1937. // This is the common name for the new photos.
  1938. if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE && $old_file != 'unknown.jpg') {
  1939. $old_extension = strtolower(substr(strrchr($old_file, '.'), 1));
  1940. $filename = in_array($old_extension, $allowed_types) ? substr($old_file, 0, -strlen($old_extension)) : $old_file;
  1941. $filename = (substr($filename, -1) == '.') ? $filename.$extension : $filename.'.'.$extension;
  1942. } else {
  1943. $filename = api_replace_dangerous_char($filename);
  1944. if (PREFIX_IMAGE_FILENAME_WITH_UID) {
  1945. $filename = uniqid('').'_'.$filename;
  1946. }
  1947. // We always prefix user photos with user ids, so on setting
  1948. // api_get_setting('split_users_upload_directory') === 'true'
  1949. // the correspondent directories to be found successfully.
  1950. $filename = $user_id.'_'.$filename;
  1951. }
  1952. //Crop the image to adjust 1:1 ratio
  1953. $image = new Image($source_file);
  1954. $image->crop($cropParameters);
  1955. // Storing the new photos in 4 versions with various sizes.
  1956. $userPath = self::getUserPathById($user_id, 'system');
  1957. // If this path does not exist - we create it.
  1958. if (!file_exists($userPath)) {
  1959. mkdir($userPath, api_get_permissions_for_new_directories(), true);
  1960. }
  1961. $small = new Image($source_file);
  1962. $small->resize(32);
  1963. $small->send_image($userPath.'small_'.$filename);
  1964. $medium = new Image($source_file);
  1965. $medium->resize(85);
  1966. $medium->send_image($userPath.'medium_'.$filename);
  1967. $normal = new Image($source_file);
  1968. $normal->resize(200);
  1969. $normal->send_image($userPath.$filename);
  1970. $big = new Image($source_file); // This is the original picture.
  1971. $big->send_image($userPath.'big_'.$filename);
  1972. $result = $small && $medium && $normal && $big;
  1973. return $result ? $filename : false;
  1974. }
  1975. /**
  1976. * Update User extra field file type into {user_folder}/{$extra_field}.
  1977. *
  1978. * @param int $user_id The user internal identification number
  1979. * @param string $extra_field The $extra_field The extra field name
  1980. * @param null $file The filename
  1981. * @param null $source_file The temporal filename
  1982. *
  1983. * @return bool|null return filename if success, but false
  1984. */
  1985. public static function update_user_extra_file(
  1986. $user_id,
  1987. $extra_field = '',
  1988. $file = null,
  1989. $source_file = null
  1990. ) {
  1991. // Add Filter
  1992. $source_file = Security::filter_filename($source_file);
  1993. $file = Security::filter_filename($file);
  1994. if (empty($user_id)) {
  1995. return false;
  1996. }
  1997. if (empty($source_file)) {
  1998. $source_file = $file;
  1999. }
  2000. // User-reserved directory where extra file have to be placed.
  2001. $path_info = self::get_user_picture_path_by_id($user_id, 'system');
  2002. $path = $path_info['dir'];
  2003. if (!empty($extra_field)) {
  2004. $path .= $extra_field.'/';
  2005. }
  2006. // If this directory does not exist - we create it.
  2007. if (!file_exists($path)) {
  2008. @mkdir($path, api_get_permissions_for_new_directories(), true);
  2009. }
  2010. if (filter_extension($file)) {
  2011. if (@move_uploaded_file($source_file, $path.$file)) {
  2012. if ($extra_field) {
  2013. return $extra_field.'/'.$file;
  2014. } else {
  2015. return $file;
  2016. }
  2017. }
  2018. }
  2019. return false; // this should be returned if anything went wrong with the upload
  2020. }
  2021. /**
  2022. * Deletes user photos.
  2023. * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php.
  2024. *
  2025. * @param int $userId the user internal identification number
  2026. *
  2027. * @return mixed returns empty string on success, FALSE on error
  2028. */
  2029. public static function deleteUserPicture($userId)
  2030. {
  2031. return self::update_user_picture($userId);
  2032. }
  2033. /**
  2034. * Returns an XHTML formatted list of productions for a user, or FALSE if he
  2035. * doesn't have any.
  2036. *
  2037. * If there has been a request to remove a production, the function will return
  2038. * without building the list unless forced to do so by the optional second
  2039. * parameter. This increases performance by avoiding to read through the
  2040. * productions on the filesystem before the removal request has been carried
  2041. * out because they'll have to be re-read afterwards anyway.
  2042. *
  2043. * @param int $user_id User id
  2044. * @param bool $force Optional parameter to force building after a removal request
  2045. * @param bool $showDelete
  2046. *
  2047. * @return string A string containing the XHTML code to display the production list, or FALSE
  2048. */
  2049. public static function build_production_list($user_id, $force = false, $showDelete = false)
  2050. {
  2051. if (!$force && !empty($_POST['remove_production'])) {
  2052. return true; // postpone reading from the filesystem
  2053. }
  2054. $productions = self::get_user_productions($user_id);
  2055. if (empty($productions)) {
  2056. return false;
  2057. }
  2058. $production_dir = self::getUserPathById($user_id, 'web');
  2059. $del_image = Display::returnIconPath('delete.png');
  2060. $add_image = Display::returnIconPath('archive.png');
  2061. $del_text = get_lang('Delete');
  2062. $production_list = '';
  2063. if (count($productions) > 0) {
  2064. $production_list = '<div class="files-production"><ul id="productions">';
  2065. foreach ($productions as $file) {
  2066. $production_list .= '<li>
  2067. <img src="'.$add_image.'" />
  2068. <a href="'.$production_dir.urlencode($file).'" target="_blank">
  2069. '.htmlentities($file).'
  2070. </a>';
  2071. if ($showDelete) {
  2072. $production_list .= '&nbsp;&nbsp;
  2073. <input
  2074. style="width:16px;"
  2075. type="image"
  2076. name="remove_production['.urlencode($file).']"
  2077. src="'.$del_image.'"
  2078. alt="'.$del_text.'"
  2079. title="'.$del_text.' '.htmlentities($file).'"
  2080. onclick="javascript: return confirmation(\''.htmlentities($file).'\');" /></li>';
  2081. }
  2082. }
  2083. $production_list .= '</ul></div>';
  2084. }
  2085. return $production_list;
  2086. }
  2087. /**
  2088. * Returns an array with the user's productions.
  2089. *
  2090. * @param int $user_id User id
  2091. *
  2092. * @return array An array containing the user's productions
  2093. */
  2094. public static function get_user_productions($user_id)
  2095. {
  2096. $production_repository = self::getUserPathById($user_id, 'system');
  2097. $productions = [];
  2098. if (is_dir($production_repository)) {
  2099. $handle = opendir($production_repository);
  2100. while ($file = readdir($handle)) {
  2101. if ($file == '.' ||
  2102. $file == '..' ||
  2103. $file == '.htaccess' ||
  2104. is_dir($production_repository.$file)
  2105. ) {
  2106. // skip current/parent directory and .htaccess
  2107. continue;
  2108. }
  2109. if (preg_match('/('.$user_id.'|[0-9a-f]{13}|saved)_.+\.(png|jpg|jpeg|gif)$/i', $file)) {
  2110. // User's photos should not be listed as productions.
  2111. continue;
  2112. }
  2113. $productions[] = $file;
  2114. }
  2115. }
  2116. return $productions;
  2117. }
  2118. /**
  2119. * Remove a user production.
  2120. *
  2121. * @param int $user_id User id
  2122. * @param string $production The production to remove
  2123. *
  2124. * @return bool
  2125. */
  2126. public static function remove_user_production($user_id, $production)
  2127. {
  2128. $production_path = self::get_user_picture_path_by_id($user_id, 'system');
  2129. $production_file = $production_path['dir'].$production;
  2130. if (is_file($production_file)) {
  2131. unlink($production_file);
  2132. return true;
  2133. }
  2134. return false;
  2135. }
  2136. /**
  2137. * Update an extra field value for a given user.
  2138. *
  2139. * @param int $userId User ID
  2140. * @param string $variable Field variable name
  2141. * @param string $value Field value
  2142. *
  2143. * @return bool true if field updated, false otherwise
  2144. */
  2145. public static function update_extra_field_value($userId, $variable, $value = '')
  2146. {
  2147. $extraFieldValue = new ExtraFieldValue('user');
  2148. $params = [
  2149. 'item_id' => $userId,
  2150. 'variable' => $variable,
  2151. 'value' => $value,
  2152. ];
  2153. return $extraFieldValue->save($params);
  2154. }
  2155. /**
  2156. * Get an array of extra fields with field details (type, default value and options).
  2157. *
  2158. * @param int Offset (from which row)
  2159. * @param int Number of items
  2160. * @param int Column on which sorting is made
  2161. * @param string Sorting direction
  2162. * @param bool Optional. Whether we get all the fields or just the visible ones
  2163. * @param int Optional. Whether we get all the fields with field_filter 1 or 0 or everything
  2164. *
  2165. * @return array Extra fields details (e.g. $list[2]['type'], $list[4]['options'][2]['title']
  2166. */
  2167. public static function get_extra_fields(
  2168. $from = 0,
  2169. $number_of_items = 0,
  2170. $column = 5,
  2171. $direction = 'ASC',
  2172. $all_visibility = true,
  2173. $field_filter = null
  2174. ) {
  2175. $fields = [];
  2176. $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD);
  2177. $t_ufo = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  2178. $columns = [
  2179. 'id',
  2180. 'variable',
  2181. 'field_type',
  2182. 'display_text',
  2183. 'default_value',
  2184. 'field_order',
  2185. 'filter',
  2186. ];
  2187. $column = (int) $column;
  2188. $sort_direction = '';
  2189. if (in_array(strtoupper($direction), ['ASC', 'DESC'])) {
  2190. $sort_direction = strtoupper($direction);
  2191. }
  2192. $extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  2193. $sqlf = "SELECT * FROM $t_uf WHERE extra_field_type = $extraFieldType ";
  2194. if (!$all_visibility) {
  2195. $sqlf .= " AND visible_to_self = 1 ";
  2196. }
  2197. if (!is_null($field_filter)) {
  2198. $field_filter = (int) $field_filter;
  2199. $sqlf .= " AND filter = $field_filter ";
  2200. }
  2201. $sqlf .= " ORDER BY ".$columns[$column]." $sort_direction ";
  2202. if ($number_of_items != 0) {
  2203. $sqlf .= " LIMIT ".intval($from).','.intval($number_of_items);
  2204. }
  2205. $resf = Database::query($sqlf);
  2206. if (Database::num_rows($resf) > 0) {
  2207. while ($rowf = Database::fetch_array($resf)) {
  2208. $fields[$rowf['id']] = [
  2209. 0 => $rowf['id'],
  2210. 1 => $rowf['variable'],
  2211. 2 => $rowf['field_type'],
  2212. 3 => empty($rowf['display_text']) ? '' : $rowf['display_text'],
  2213. 4 => $rowf['default_value'],
  2214. 5 => $rowf['field_order'],
  2215. 6 => $rowf['visible_to_self'],
  2216. 7 => $rowf['changeable'],
  2217. 8 => $rowf['filter'],
  2218. 9 => [],
  2219. 10 => '<a name="'.$rowf['id'].'"></a>',
  2220. ];
  2221. $sqlo = "SELECT * FROM $t_ufo
  2222. WHERE field_id = ".$rowf['id']."
  2223. ORDER BY option_order ASC";
  2224. $reso = Database::query($sqlo);
  2225. if (Database::num_rows($reso) > 0) {
  2226. while ($rowo = Database::fetch_array($reso)) {
  2227. $fields[$rowf['id']][9][$rowo['id']] = [
  2228. 0 => $rowo['id'],
  2229. 1 => $rowo['option_value'],
  2230. 2 => empty($rowo['display_text']) ? '' : $rowo['display_text'],
  2231. 3 => $rowo['option_order'],
  2232. ];
  2233. }
  2234. }
  2235. }
  2236. }
  2237. return $fields;
  2238. }
  2239. /**
  2240. * Build a list of extra file already uploaded in $user_folder/{$extra_field}/.
  2241. *
  2242. * @param $user_id
  2243. * @param $extra_field
  2244. * @param bool $force
  2245. * @param bool $showDelete
  2246. *
  2247. * @return bool|string
  2248. */
  2249. public static function build_user_extra_file_list(
  2250. $user_id,
  2251. $extra_field,
  2252. $force = false,
  2253. $showDelete = false
  2254. ) {
  2255. if (!$force && !empty($_POST['remove_'.$extra_field])) {
  2256. return true; // postpone reading from the filesystem
  2257. }
  2258. $extra_files = self::get_user_extra_files($user_id, $extra_field);
  2259. if (empty($extra_files)) {
  2260. return false;
  2261. }
  2262. $path_info = self::get_user_picture_path_by_id($user_id, 'web');
  2263. $path = $path_info['dir'];
  2264. $del_image = Display::returnIconPath('delete.png');
  2265. $del_text = get_lang('Delete');
  2266. $extra_file_list = '';
  2267. if (count($extra_files) > 0) {
  2268. $extra_file_list = '<div class="files-production"><ul id="productions">';
  2269. foreach ($extra_files as $file) {
  2270. $filename = substr($file, strlen($extra_field) + 1);
  2271. $extra_file_list .= '<li>'.Display::return_icon('archive.png').
  2272. '<a href="'.$path.$extra_field.'/'.urlencode($filename).'" target="_blank">
  2273. '.htmlentities($filename).
  2274. '</a> ';
  2275. if ($showDelete) {
  2276. $extra_file_list .= '<input
  2277. style="width:16px;"
  2278. type="image"
  2279. name="remove_extra_'.$extra_field.'['.urlencode($file).']"
  2280. src="'.$del_image.'"
  2281. alt="'.$del_text.'"
  2282. title="'.$del_text.' '.htmlentities($filename).'"
  2283. onclick="javascript: return confirmation(\''.htmlentities($filename).'\');" /></li>';
  2284. }
  2285. }
  2286. $extra_file_list .= '</ul></div>';
  2287. }
  2288. return $extra_file_list;
  2289. }
  2290. /**
  2291. * Get valid filenames in $user_folder/{$extra_field}/.
  2292. *
  2293. * @param $user_id
  2294. * @param $extra_field
  2295. * @param bool $full_path
  2296. *
  2297. * @return array
  2298. */
  2299. public static function get_user_extra_files($user_id, $extra_field, $full_path = false)
  2300. {
  2301. if (!$full_path) {
  2302. // Nothing to do
  2303. } else {
  2304. $path_info = self::get_user_picture_path_by_id($user_id, 'system');
  2305. $path = $path_info['dir'];
  2306. }
  2307. $extra_data = self::get_extra_user_data_by_field($user_id, $extra_field);
  2308. $extra_files = $extra_data[$extra_field];
  2309. $files = [];
  2310. if (is_array($extra_files)) {
  2311. foreach ($extra_files as $key => $value) {
  2312. if (!$full_path) {
  2313. // Relative path from user folder
  2314. $files[] = $value;
  2315. } else {
  2316. $files[] = $path.$value;
  2317. }
  2318. }
  2319. } elseif (!empty($extra_files)) {
  2320. if (!$full_path) {
  2321. // Relative path from user folder
  2322. $files[] = $extra_files;
  2323. } else {
  2324. $files[] = $path.$extra_files;
  2325. }
  2326. }
  2327. return $files; // can be an empty array
  2328. }
  2329. /**
  2330. * Remove an {$extra_file} from the user folder $user_folder/{$extra_field}/.
  2331. *
  2332. * @param int $user_id
  2333. * @param string $extra_field
  2334. * @param string $extra_file
  2335. *
  2336. * @return bool
  2337. */
  2338. public static function remove_user_extra_file($user_id, $extra_field, $extra_file)
  2339. {
  2340. $extra_file = Security::filter_filename($extra_file);
  2341. $path_info = self::get_user_picture_path_by_id($user_id, 'system');
  2342. if (strpos($extra_file, $extra_field) !== false) {
  2343. $path_extra_file = $path_info['dir'].$extra_file;
  2344. } else {
  2345. $path_extra_file = $path_info['dir'].$extra_field.'/'.$extra_file;
  2346. }
  2347. if (is_file($path_extra_file)) {
  2348. unlink($path_extra_file);
  2349. return true;
  2350. }
  2351. return false;
  2352. }
  2353. /**
  2354. * Creates a new extra field.
  2355. *
  2356. * @param string $variable Field's internal variable name
  2357. * @param int $fieldType Field's type
  2358. * @param string $displayText Field's language var name
  2359. * @param string $default Field's default value
  2360. *
  2361. * @return int
  2362. */
  2363. public static function create_extra_field(
  2364. $variable,
  2365. $fieldType,
  2366. $displayText,
  2367. $default
  2368. ) {
  2369. $extraField = new ExtraField('user');
  2370. $params = [
  2371. 'variable' => $variable,
  2372. 'field_type' => $fieldType,
  2373. 'display_text' => $displayText,
  2374. 'default_value' => $default,
  2375. ];
  2376. return $extraField->save($params);
  2377. }
  2378. /**
  2379. * Check if a field is available.
  2380. *
  2381. * @param string $variable
  2382. *
  2383. * @return bool
  2384. */
  2385. public static function is_extra_field_available($variable)
  2386. {
  2387. $extraField = new ExtraField('user');
  2388. $data = $extraField->get_handler_field_info_by_field_variable($variable);
  2389. return !empty($data) ? true : false;
  2390. }
  2391. /**
  2392. * Gets user extra fields data.
  2393. *
  2394. * @param int User ID
  2395. * @param bool Whether to prefix the fields indexes with "extra_" (might be used by formvalidator)
  2396. * @param bool Whether to return invisible fields as well
  2397. * @param bool Whether to split multiple-selection fields or not
  2398. *
  2399. * @return array Array of fields => value for the given user
  2400. */
  2401. public static function get_extra_user_data(
  2402. $user_id,
  2403. $prefix = false,
  2404. $allVisibility = true,
  2405. $splitMultiple = false,
  2406. $fieldFilter = null
  2407. ) {
  2408. $user_id = (int) $user_id;
  2409. if (empty($user_id)) {
  2410. return [];
  2411. }
  2412. $extra_data = [];
  2413. $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD);
  2414. $t_ufv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  2415. $user_id = (int) $user_id;
  2416. $sql = "SELECT f.id as id, f.variable as fvar, f.field_type as type
  2417. FROM $t_uf f
  2418. WHERE
  2419. extra_field_type = ".EntityExtraField::USER_FIELD_TYPE."
  2420. ";
  2421. $filter_cond = '';
  2422. if (!$allVisibility) {
  2423. if (isset($fieldFilter)) {
  2424. $fieldFilter = (int) $fieldFilter;
  2425. $filter_cond .= " AND filter = $fieldFilter ";
  2426. }
  2427. $sql .= " AND f.visible_to_self = 1 $filter_cond ";
  2428. } else {
  2429. if (isset($fieldFilter)) {
  2430. $fieldFilter = (int) $fieldFilter;
  2431. $sql .= " AND filter = $fieldFilter ";
  2432. }
  2433. }
  2434. $sql .= ' ORDER BY f.field_order';
  2435. $res = Database::query($sql);
  2436. if (Database::num_rows($res) > 0) {
  2437. while ($row = Database::fetch_array($res)) {
  2438. if ($row['type'] == self::USER_FIELD_TYPE_TAG) {
  2439. $tags = self::get_user_tags_to_string($user_id, $row['id'], false);
  2440. $extra_data['extra_'.$row['fvar']] = $tags;
  2441. } else {
  2442. $sqlu = "SELECT value as fval
  2443. FROM $t_ufv
  2444. WHERE field_id=".$row['id']." AND item_id = ".$user_id;
  2445. $resu = Database::query($sqlu);
  2446. // get default value
  2447. $sql_df = "SELECT default_value as fval_df FROM $t_uf
  2448. WHERE id=".$row['id'];
  2449. $res_df = Database::query($sql_df);
  2450. if (Database::num_rows($resu) > 0) {
  2451. $rowu = Database::fetch_array($resu);
  2452. $fval = $rowu['fval'];
  2453. if ($row['type'] == self::USER_FIELD_TYPE_SELECT_MULTIPLE) {
  2454. $fval = explode(';', $rowu['fval']);
  2455. }
  2456. } else {
  2457. $row_df = Database::fetch_array($res_df);
  2458. $fval = $row_df['fval_df'];
  2459. }
  2460. // We get here (and fill the $extra_data array) even if there
  2461. // is no user with data (we fill it with default values)
  2462. if ($prefix) {
  2463. if ($row['type'] == self::USER_FIELD_TYPE_RADIO) {
  2464. $extra_data['extra_'.$row['fvar']]['extra_'.$row['fvar']] = $fval;
  2465. } else {
  2466. $extra_data['extra_'.$row['fvar']] = $fval;
  2467. }
  2468. } else {
  2469. if ($row['type'] == self::USER_FIELD_TYPE_RADIO) {
  2470. $extra_data['extra_'.$row['fvar']]['extra_'.$row['fvar']] = $fval;
  2471. } else {
  2472. $extra_data[$row['fvar']] = $fval;
  2473. }
  2474. }
  2475. }
  2476. }
  2477. }
  2478. return $extra_data;
  2479. }
  2480. /** Get extra user data by field
  2481. * @param int user ID
  2482. * @param string the internal variable name of the field
  2483. *
  2484. * @return array with extra data info of a user i.e array('field_variable'=>'value');
  2485. */
  2486. public static function get_extra_user_data_by_field(
  2487. $user_id,
  2488. $field_variable,
  2489. $prefix = false,
  2490. $all_visibility = true,
  2491. $splitmultiple = false
  2492. ) {
  2493. $user_id = (int) $user_id;
  2494. if (empty($user_id)) {
  2495. return [];
  2496. }
  2497. $extra_data = [];
  2498. $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD);
  2499. $t_ufv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  2500. $sql = "SELECT f.id as id, f.variable as fvar, f.field_type as type
  2501. FROM $t_uf f
  2502. WHERE f.variable = '$field_variable' ";
  2503. if (!$all_visibility) {
  2504. $sql .= " AND f.visible_to_self = 1 ";
  2505. }
  2506. $sql .= " AND extra_field_type = ".EntityExtraField::USER_FIELD_TYPE;
  2507. $sql .= " ORDER BY f.field_order ";
  2508. $res = Database::query($sql);
  2509. if (Database::num_rows($res) > 0) {
  2510. while ($row = Database::fetch_array($res)) {
  2511. $sqlu = "SELECT value as fval FROM $t_ufv v
  2512. INNER JOIN $t_uf f
  2513. ON (v.field_id = f.id)
  2514. WHERE
  2515. extra_field_type = ".EntityExtraField::USER_FIELD_TYPE." AND
  2516. field_id = ".$row['id']." AND
  2517. item_id = ".$user_id;
  2518. $resu = Database::query($sqlu);
  2519. $fval = '';
  2520. if (Database::num_rows($resu) > 0) {
  2521. $rowu = Database::fetch_array($resu);
  2522. $fval = $rowu['fval'];
  2523. if ($row['type'] == self::USER_FIELD_TYPE_SELECT_MULTIPLE) {
  2524. $fval = explode(';', $rowu['fval']);
  2525. }
  2526. }
  2527. if ($prefix) {
  2528. $extra_data['extra_'.$row['fvar']] = $fval;
  2529. } else {
  2530. $extra_data[$row['fvar']] = $fval;
  2531. }
  2532. }
  2533. }
  2534. return $extra_data;
  2535. }
  2536. /**
  2537. * Get the extra field information for a certain field (the options as well).
  2538. *
  2539. * @param int $variable The name of the field we want to know everything about
  2540. *
  2541. * @return array Array containing all the information about the extra profile field
  2542. * (first level of array contains field details, then 'options' sub-array contains options details,
  2543. * as returned by the database)
  2544. *
  2545. * @author Julio Montoya
  2546. *
  2547. * @since v1.8.6
  2548. */
  2549. public static function get_extra_field_information_by_name($variable)
  2550. {
  2551. $extraField = new ExtraField('user');
  2552. return $extraField->get_handler_field_info_by_field_variable($variable);
  2553. }
  2554. /**
  2555. * Get the extra field information for user tag (the options as well).
  2556. *
  2557. * @param int $variable The name of the field we want to know everything about
  2558. *
  2559. * @return array Array containing all the information about the extra profile field
  2560. * (first level of array contains field details, then 'options' sub-array contains options details,
  2561. * as returned by the database)
  2562. *
  2563. * @author José Loguercio
  2564. *
  2565. * @since v1.11.0
  2566. */
  2567. public static function get_extra_field_tags_information_by_name($variable)
  2568. {
  2569. $extraField = new ExtraField('user');
  2570. return $extraField->get_handler_field_info_by_tags($variable);
  2571. }
  2572. /**
  2573. * @param string $type
  2574. *
  2575. * @return array
  2576. */
  2577. public static function get_all_extra_field_by_type($type)
  2578. {
  2579. $extraField = new ExtraField('user');
  2580. return $extraField->get_all_extra_field_by_type($type);
  2581. }
  2582. /**
  2583. * Get all the extra field information of a certain field (also the options).
  2584. *
  2585. * @param int $fieldId the ID of the field we want to know everything of
  2586. *
  2587. * @return array $return containing all th information about the extra profile field
  2588. *
  2589. * @author Julio Montoya
  2590. *
  2591. * @deprecated
  2592. * @since v1.8.6
  2593. */
  2594. public static function get_extra_field_information($fieldId)
  2595. {
  2596. $extraField = new ExtraField('user');
  2597. return $extraField->getFieldInfoByFieldId($fieldId);
  2598. }
  2599. /**
  2600. * Get extra user data by value.
  2601. *
  2602. * @param string $variable the internal variable name of the field
  2603. * @param string $value the internal value of the field
  2604. * @param bool $all_visibility
  2605. *
  2606. * @return array with extra data info of a user i.e array('field_variable'=>'value');
  2607. */
  2608. public static function get_extra_user_data_by_value($variable, $value, $all_visibility = true)
  2609. {
  2610. $extraFieldValue = new ExtraFieldValue('user');
  2611. $extraField = new ExtraField('user');
  2612. $info = $extraField->get_handler_field_info_by_field_variable($variable);
  2613. if (false === $info) {
  2614. return [];
  2615. }
  2616. $data = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  2617. $variable,
  2618. $value,
  2619. false,
  2620. false,
  2621. true
  2622. );
  2623. $result = [];
  2624. if (!empty($data)) {
  2625. foreach ($data as $item) {
  2626. $result[] = $item['item_id'];
  2627. }
  2628. }
  2629. return $result;
  2630. }
  2631. /**
  2632. * Get extra user data by tags value.
  2633. *
  2634. * @param int $fieldId the ID of the field we want to know everything of
  2635. * @param string $tag the tag name for search
  2636. *
  2637. * @return array with extra data info of a user
  2638. *
  2639. * @author José Loguercio
  2640. *
  2641. * @since v1.11.0
  2642. */
  2643. public static function get_extra_user_data_by_tags($fieldId, $tag)
  2644. {
  2645. $extraField = new ExtraField('user');
  2646. $result = $extraField->getAllUserPerTag($fieldId, $tag);
  2647. $array = [];
  2648. foreach ($result as $index => $user) {
  2649. $array[] = $user['user_id'];
  2650. }
  2651. return $array;
  2652. }
  2653. /**
  2654. * Get extra user data by field variable.
  2655. *
  2656. * @param string $variable field variable
  2657. *
  2658. * @return array data
  2659. */
  2660. public static function get_extra_user_data_by_field_variable($variable)
  2661. {
  2662. $extraInfo = self::get_extra_field_information_by_name($variable);
  2663. $field_id = (int) $extraInfo['id'];
  2664. $extraField = new ExtraFieldValue('user');
  2665. $data = $extraField->getValuesByFieldId($field_id);
  2666. if (!empty($data)) {
  2667. foreach ($data as $row) {
  2668. $user_id = $row['item_id'];
  2669. $data[$user_id] = $row;
  2670. }
  2671. }
  2672. return $data;
  2673. }
  2674. /**
  2675. * Get extra user data tags by field variable.
  2676. *
  2677. * @param string $variable field variable
  2678. *
  2679. * @return array
  2680. */
  2681. public static function get_extra_user_data_for_tags($variable)
  2682. {
  2683. $data = self::get_extra_field_tags_information_by_name($variable);
  2684. return $data;
  2685. }
  2686. /**
  2687. * Gives a list of [session_category][session_id] for the current user.
  2688. *
  2689. * @param int $user_id
  2690. * @param bool $is_time_over whether to fill the first element or not
  2691. * (to give space for courses out of categories)
  2692. * @param bool $ignore_visibility_for_admins optional true if limit time from session is over, false otherwise
  2693. * @param bool $ignoreTimeLimit ignore time start/end
  2694. * @param bool $getCount
  2695. *
  2696. * @return array list of statuses [session_category][session_id]
  2697. *
  2698. * @todo ensure multiple access urls are managed correctly
  2699. */
  2700. public static function get_sessions_by_category(
  2701. $user_id,
  2702. $is_time_over = true,
  2703. $ignore_visibility_for_admins = false,
  2704. $ignoreTimeLimit = false,
  2705. $getCount = false
  2706. ) {
  2707. $user_id = (int) $user_id;
  2708. if (empty($user_id)) {
  2709. return [];
  2710. }
  2711. $allowOrder = api_get_configuration_value('session_list_order');
  2712. $position = '';
  2713. if ($allowOrder) {
  2714. $position = ', s.position AS position ';
  2715. }
  2716. // Get the list of sessions per user
  2717. $now = new DateTime('now', new DateTimeZone('UTC'));
  2718. // LEFT JOIN is used for session_rel_course_rel_user because an inner
  2719. // join would not catch session-courses where the user is general
  2720. // session coach but which do not have students nor coaches registered
  2721. $dqlSelect = ' COUNT(DISTINCT s.id) ';
  2722. if (!$getCount) {
  2723. $dqlSelect = " DISTINCT
  2724. s.id,
  2725. s.name,
  2726. s.accessStartDate AS access_start_date,
  2727. s.accessEndDate AS access_end_date,
  2728. s.duration,
  2729. sc.id AS session_category_id,
  2730. sc.name AS session_category_name,
  2731. sc.dateStart AS session_category_date_start,
  2732. sc.dateEnd AS session_category_date_end,
  2733. s.coachAccessStartDate AS coach_access_start_date,
  2734. s.coachAccessEndDate AS coach_access_end_date,
  2735. CASE WHEN s.accessEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull
  2736. $position
  2737. ";
  2738. }
  2739. $dql = "SELECT $dqlSelect
  2740. FROM ChamiloCoreBundle:Session AS s
  2741. LEFT JOIN ChamiloCoreBundle:SessionRelCourseRelUser AS scu WITH scu.session = s
  2742. INNER JOIN ChamiloCoreBundle:AccessUrlRelSession AS url WITH url.sessionId = s.id
  2743. LEFT JOIN ChamiloCoreBundle:SessionCategory AS sc WITH s.category = sc ";
  2744. // A single OR operation on scu.user = :user OR s.generalCoach = :user
  2745. // is awfully inefficient for large sets of data (1m25s for 58K
  2746. // sessions, BT#14115) but executing a similar query twice and grouping
  2747. // the results afterwards in PHP takes about 1/1000th of the time
  2748. // (0.1s + 0.0s) for the same set of data, so we do it this way...
  2749. $dqlStudent = $dql.' WHERE scu.user = :user AND url.accessUrlId = :url ';
  2750. $dqlCoach = $dql.' WHERE s.generalCoach = :user AND url.accessUrlId = :url ';
  2751. // Default order
  2752. $order = 'ORDER BY sc.name, s.name';
  2753. // Order by date if showing all sessions
  2754. $showAllSessions = api_get_configuration_value('show_all_sessions_on_my_course_page') === true;
  2755. if ($showAllSessions) {
  2756. $order = 'ORDER BY s.accessStartDate';
  2757. }
  2758. // Order by position
  2759. if ($allowOrder) {
  2760. $order = 'ORDER BY s.position';
  2761. }
  2762. // Order by dates according to settings
  2763. $orderBySettings = api_get_configuration_value('my_courses_session_order');
  2764. if (!empty($orderBySettings) && isset($orderBySettings['field']) && isset($orderBySettings['order'])) {
  2765. $field = $orderBySettings['field'];
  2766. $orderSetting = $orderBySettings['order'];
  2767. switch ($field) {
  2768. case 'start_date':
  2769. $order = " ORDER BY s.accessStartDate $orderSetting";
  2770. break;
  2771. case 'end_date':
  2772. $order = " ORDER BY s.accessEndDate $orderSetting ";
  2773. if ($orderSetting == 'asc') {
  2774. // Put null values at the end
  2775. // https://stackoverflow.com/questions/12652034/how-can-i-order-by-null-in-dql
  2776. $order = ' ORDER BY _isFieldNull asc, s.accessEndDate asc';
  2777. }
  2778. break;
  2779. }
  2780. }
  2781. $dqlStudent .= $order;
  2782. $dqlCoach .= $order;
  2783. $accessUrlId = api_get_current_access_url_id();
  2784. $dqlStudent = Database::getManager()
  2785. ->createQuery($dqlStudent)
  2786. ->setParameters(
  2787. ['user' => $user_id, 'url' => $accessUrlId]
  2788. )
  2789. ;
  2790. $dqlCoach = Database::getManager()
  2791. ->createQuery($dqlCoach)
  2792. ->setParameters(
  2793. ['user' => $user_id, 'url' => $accessUrlId]
  2794. )
  2795. ;
  2796. if ($getCount) {
  2797. return $dqlStudent->getSingleScalarResult() + $dqlCoach->getSingleScalarResult();
  2798. }
  2799. $sessionDataStudent = $dqlStudent->getResult();
  2800. $sessionDataCoach = $dqlCoach->getResult();
  2801. $sessionData = [];
  2802. // First fill $sessionData with student sessions
  2803. foreach ($sessionDataStudent as $row) {
  2804. $sessionData[$row['id']] = $row;
  2805. }
  2806. // Overwrite session data of the user as a student with session data
  2807. // of the user as a coach.
  2808. // There shouldn't be such duplicate rows, but just in case...
  2809. foreach ($sessionDataCoach as $row) {
  2810. $sessionData[$row['id']] = $row;
  2811. }
  2812. $collapsable = api_get_configuration_value('allow_user_session_collapsable');
  2813. $extraField = new ExtraFieldValue('session');
  2814. $collapsableLink = api_get_path(WEB_PATH).'user_portal.php?action=collapse_session';
  2815. $categories = [];
  2816. foreach ($sessionData as $row) {
  2817. $session_id = $row['id'];
  2818. $coachList = SessionManager::getCoachesBySession($session_id);
  2819. $categoryStart = $row['session_category_date_start'] ? $row['session_category_date_start']->format('Y-m-d') : '';
  2820. $categoryEnd = $row['session_category_date_end'] ? $row['session_category_date_end']->format('Y-m-d') : '';
  2821. $courseList = self::get_courses_list_by_session(
  2822. $user_id,
  2823. $session_id
  2824. );
  2825. $daysLeft = SessionManager::getDayLeftInSession($row, $user_id);
  2826. // User portal filters:
  2827. if ($ignoreTimeLimit === false) {
  2828. if ($is_time_over) {
  2829. // History
  2830. if ($row['duration']) {
  2831. if ($daysLeft >= 0) {
  2832. continue;
  2833. }
  2834. } else {
  2835. if (empty($row['access_end_date'])) {
  2836. continue;
  2837. } else {
  2838. if ($row['access_end_date'] > $now) {
  2839. continue;
  2840. }
  2841. }
  2842. }
  2843. } else {
  2844. // Current user portal
  2845. $isGeneralCoach = SessionManager::user_is_general_coach($user_id, $row['id']);
  2846. $isCoachOfCourse = in_array($user_id, $coachList);
  2847. if (api_is_platform_admin() || $isGeneralCoach || $isCoachOfCourse) {
  2848. // Teachers can access the session depending in the access_coach date
  2849. } else {
  2850. if ($row['duration']) {
  2851. if ($daysLeft <= 0) {
  2852. continue;
  2853. }
  2854. } else {
  2855. if (isset($row['access_end_date']) &&
  2856. !empty($row['access_end_date'])
  2857. ) {
  2858. if ($row['access_end_date'] <= $now) {
  2859. continue;
  2860. }
  2861. }
  2862. }
  2863. }
  2864. }
  2865. }
  2866. $categories[$row['session_category_id']]['session_category'] = [
  2867. 'id' => $row['session_category_id'],
  2868. 'name' => $row['session_category_name'],
  2869. 'date_start' => $categoryStart,
  2870. 'date_end' => $categoryEnd,
  2871. ];
  2872. $visibility = api_get_session_visibility(
  2873. $session_id,
  2874. null,
  2875. $ignore_visibility_for_admins
  2876. );
  2877. if ($visibility != SESSION_VISIBLE) {
  2878. // Course Coach session visibility.
  2879. $blockedCourseCount = 0;
  2880. $closedVisibilityList = [
  2881. COURSE_VISIBILITY_CLOSED,
  2882. COURSE_VISIBILITY_HIDDEN,
  2883. ];
  2884. foreach ($courseList as $course) {
  2885. // Checking session visibility
  2886. $sessionCourseVisibility = api_get_session_visibility(
  2887. $session_id,
  2888. $course['real_id'],
  2889. $ignore_visibility_for_admins
  2890. );
  2891. $courseIsVisible = !in_array($course['visibility'], $closedVisibilityList);
  2892. if ($courseIsVisible === false || $sessionCourseVisibility == SESSION_INVISIBLE) {
  2893. $blockedCourseCount++;
  2894. }
  2895. }
  2896. // If all courses are blocked then no show in the list.
  2897. if ($blockedCourseCount === count($courseList)) {
  2898. $visibility = SESSION_INVISIBLE;
  2899. } else {
  2900. $visibility = $sessionCourseVisibility;
  2901. }
  2902. }
  2903. switch ($visibility) {
  2904. case SESSION_VISIBLE_READ_ONLY:
  2905. case SESSION_VISIBLE:
  2906. case SESSION_AVAILABLE:
  2907. break;
  2908. case SESSION_INVISIBLE:
  2909. if ($ignore_visibility_for_admins === false) {
  2910. continue 2;
  2911. }
  2912. }
  2913. $collapsed = '';
  2914. $collapsedAction = '';
  2915. if ($collapsable) {
  2916. $collapsableData = SessionManager::getCollapsableData(
  2917. $user_id,
  2918. $session_id,
  2919. $extraField,
  2920. $collapsableLink
  2921. );
  2922. $collapsed = $collapsableData['collapsed'];
  2923. $collapsedAction = $collapsableData['collapsable_link'];
  2924. }
  2925. $categories[$row['session_category_id']]['sessions'][] = [
  2926. 'session_name' => $row['name'],
  2927. 'session_id' => $row['id'],
  2928. 'access_start_date' => $row['access_start_date'] ? $row['access_start_date']->format('Y-m-d H:i:s') : null,
  2929. 'access_end_date' => $row['access_end_date'] ? $row['access_end_date']->format('Y-m-d H:i:s') : null,
  2930. 'coach_access_start_date' => $row['coach_access_start_date'] ? $row['coach_access_start_date']->format('Y-m-d H:i:s') : null,
  2931. 'coach_access_end_date' => $row['coach_access_end_date'] ? $row['coach_access_end_date']->format('Y-m-d H:i:s') : null,
  2932. 'courses' => $courseList,
  2933. 'collapsed' => $collapsed,
  2934. 'collapsable_link' => $collapsedAction,
  2935. 'duration' => $row['duration'],
  2936. ];
  2937. }
  2938. return $categories;
  2939. }
  2940. /**
  2941. * Gives a list of [session_id-course_code] => [status] for the current user.
  2942. *
  2943. * @param int $user_id
  2944. * @param int $sessionLimit
  2945. *
  2946. * @return array list of statuses (session_id-course_code => status)
  2947. */
  2948. public static function get_personal_session_course_list($user_id, $sessionLimit = null)
  2949. {
  2950. // Database Table Definitions
  2951. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2952. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  2953. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2954. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  2955. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2956. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2957. $user_id = (int) $user_id;
  2958. if (empty($user_id)) {
  2959. return [];
  2960. }
  2961. // We filter the courses from the URL
  2962. $join_access_url = $where_access_url = '';
  2963. if (api_get_multiple_access_url()) {
  2964. $access_url_id = api_get_current_access_url_id();
  2965. if ($access_url_id != -1) {
  2966. $tbl_url_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2967. $join_access_url = "LEFT JOIN $tbl_url_course url_rel_course ON url_rel_course.c_id = course.id";
  2968. $where_access_url = " AND access_url_id = $access_url_id ";
  2969. }
  2970. }
  2971. // Courses in which we subscribed out of any session
  2972. $tbl_user_course_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  2973. $sql = "SELECT
  2974. course.code,
  2975. course_rel_user.status course_rel_status,
  2976. course_rel_user.sort sort,
  2977. course_rel_user.user_course_cat user_course_cat
  2978. FROM $tbl_course_user course_rel_user
  2979. LEFT JOIN $tbl_course course
  2980. ON course.id = course_rel_user.c_id
  2981. LEFT JOIN $tbl_user_course_category user_course_category
  2982. ON course_rel_user.user_course_cat = user_course_category.id
  2983. $join_access_url
  2984. WHERE
  2985. course_rel_user.user_id = '".$user_id."' AND
  2986. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  2987. $where_access_url
  2988. ORDER BY user_course_category.sort, course_rel_user.sort, course.title ASC";
  2989. $course_list_sql_result = Database::query($sql);
  2990. $personal_course_list = [];
  2991. if (Database::num_rows($course_list_sql_result) > 0) {
  2992. while ($result_row = Database::fetch_array($course_list_sql_result, 'ASSOC')) {
  2993. $course_info = api_get_course_info($result_row['code']);
  2994. $result_row['course_info'] = $course_info;
  2995. $personal_course_list[] = $result_row;
  2996. }
  2997. }
  2998. $coachCourseConditions = '';
  2999. // Getting sessions that are related to a coach in the session_rel_course_rel_user table
  3000. if (api_is_allowed_to_create_course()) {
  3001. $sessionListFromCourseCoach = [];
  3002. $sql = " SELECT DISTINCT session_id
  3003. FROM $tbl_session_course_user
  3004. WHERE user_id = $user_id AND status = 2 ";
  3005. $result = Database::query($sql);
  3006. if (Database::num_rows($result)) {
  3007. $result = Database::store_result($result);
  3008. foreach ($result as $session) {
  3009. $sessionListFromCourseCoach[] = $session['session_id'];
  3010. }
  3011. }
  3012. if (!empty($sessionListFromCourseCoach)) {
  3013. $condition = implode("','", $sessionListFromCourseCoach);
  3014. $coachCourseConditions = " OR ( s.id IN ('$condition'))";
  3015. }
  3016. }
  3017. // Get the list of sessions where the user is subscribed
  3018. // This is divided into two different queries
  3019. $sessions = [];
  3020. $sessionLimitRestriction = '';
  3021. if (!empty($sessionLimit)) {
  3022. $sessionLimit = (int) $sessionLimit;
  3023. $sessionLimitRestriction = "LIMIT $sessionLimit";
  3024. }
  3025. $sql = "SELECT DISTINCT s.id, name, access_start_date, access_end_date
  3026. FROM $tbl_session_user su INNER JOIN $tbl_session s
  3027. ON (s.id = su.session_id)
  3028. WHERE (
  3029. su.user_id = $user_id AND
  3030. su.relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  3031. )
  3032. $coachCourseConditions
  3033. ORDER BY access_start_date, access_end_date, name
  3034. $sessionLimitRestriction
  3035. ";
  3036. $result = Database::query($sql);
  3037. if (Database::num_rows($result) > 0) {
  3038. while ($row = Database::fetch_assoc($result)) {
  3039. $sessions[$row['id']] = $row;
  3040. }
  3041. }
  3042. $sql = "SELECT DISTINCT
  3043. id, name, access_start_date, access_end_date
  3044. FROM $tbl_session s
  3045. WHERE (
  3046. id_coach = $user_id
  3047. )
  3048. $coachCourseConditions
  3049. ORDER BY access_start_date, access_end_date, name";
  3050. $result = Database::query($sql);
  3051. if (Database::num_rows($result) > 0) {
  3052. while ($row = Database::fetch_assoc($result)) {
  3053. if (empty($sessions[$row['id']])) {
  3054. $sessions[$row['id']] = $row;
  3055. }
  3056. }
  3057. }
  3058. if (api_is_allowed_to_create_course()) {
  3059. foreach ($sessions as $enreg) {
  3060. $session_id = $enreg['id'];
  3061. $session_visibility = api_get_session_visibility($session_id);
  3062. if ($session_visibility == SESSION_INVISIBLE) {
  3063. continue;
  3064. }
  3065. // This query is horribly slow when more than a few thousand
  3066. // users and just a few sessions to which they are subscribed
  3067. $sql = "SELECT DISTINCT
  3068. course.code code,
  3069. course.title i,
  3070. ".(api_is_western_name_order() ? "CONCAT(user.firstname,' ',user.lastname)" : "CONCAT(user.lastname,' ',user.firstname)")." t,
  3071. email, course.course_language l,
  3072. 1 sort,
  3073. category_code user_course_cat,
  3074. access_start_date,
  3075. access_end_date,
  3076. session.id as session_id,
  3077. session.name as session_name
  3078. FROM $tbl_session_course_user as session_course_user
  3079. INNER JOIN $tbl_course AS course
  3080. ON course.id = session_course_user.c_id
  3081. INNER JOIN $tbl_session as session
  3082. ON session.id = session_course_user.session_id
  3083. LEFT JOIN $tbl_user as user
  3084. ON user.id = session_course_user.user_id OR session.id_coach = user.id
  3085. WHERE
  3086. session_course_user.session_id = $session_id AND (
  3087. (session_course_user.user_id = $user_id AND session_course_user.status = 2)
  3088. OR session.id_coach = $user_id
  3089. )
  3090. ORDER BY i";
  3091. $course_list_sql_result = Database::query($sql);
  3092. while ($result_row = Database::fetch_array($course_list_sql_result, 'ASSOC')) {
  3093. $result_row['course_info'] = api_get_course_info($result_row['code']);
  3094. $key = $result_row['session_id'].' - '.$result_row['code'];
  3095. $personal_course_list[$key] = $result_row;
  3096. }
  3097. }
  3098. }
  3099. foreach ($sessions as $enreg) {
  3100. $session_id = $enreg['id'];
  3101. $session_visibility = api_get_session_visibility($session_id);
  3102. if ($session_visibility == SESSION_INVISIBLE) {
  3103. continue;
  3104. }
  3105. /* This query is very similar to the above query,
  3106. but it will check the session_rel_course_user table if there are courses registered to our user or not */
  3107. $sql = "SELECT DISTINCT
  3108. course.code code,
  3109. course.title i, CONCAT(user.lastname,' ',user.firstname) t,
  3110. email,
  3111. course.course_language l,
  3112. 1 sort,
  3113. category_code user_course_cat,
  3114. access_start_date,
  3115. access_end_date,
  3116. session.id as session_id,
  3117. session.name as session_name,
  3118. IF((session_course_user.user_id = 3 AND session_course_user.status=2),'2', '5')
  3119. FROM $tbl_session_course_user as session_course_user
  3120. INNER JOIN $tbl_course AS course
  3121. ON course.id = session_course_user.c_id AND session_course_user.session_id = $session_id
  3122. INNER JOIN $tbl_session as session
  3123. ON session_course_user.session_id = session.id
  3124. LEFT JOIN $tbl_user as user ON user.id = session_course_user.user_id
  3125. WHERE session_course_user.user_id = $user_id
  3126. ORDER BY i";
  3127. $course_list_sql_result = Database::query($sql);
  3128. while ($result_row = Database::fetch_array($course_list_sql_result, 'ASSOC')) {
  3129. $result_row['course_info'] = api_get_course_info($result_row['code']);
  3130. $key = $result_row['session_id'].' - '.$result_row['code'];
  3131. if (!isset($personal_course_list[$key])) {
  3132. $personal_course_list[$key] = $result_row;
  3133. }
  3134. }
  3135. }
  3136. return $personal_course_list;
  3137. }
  3138. /**
  3139. * Gives a list of courses for the given user in the given session.
  3140. *
  3141. * @param int $user_id
  3142. * @param int $session_id
  3143. *
  3144. * @return array list of statuses (session_id-course_code => status)
  3145. */
  3146. public static function get_courses_list_by_session($user_id, $session_id)
  3147. {
  3148. // Database Table Definitions
  3149. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  3150. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  3151. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3152. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  3153. $user_id = (int) $user_id;
  3154. $session_id = (int) $session_id;
  3155. // We filter the courses from the URL
  3156. $join_access_url = $where_access_url = '';
  3157. if (api_get_multiple_access_url()) {
  3158. $urlId = api_get_current_access_url_id();
  3159. if ($urlId != -1) {
  3160. $tbl_url_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  3161. $join_access_url = " , $tbl_url_session url_rel_session ";
  3162. $where_access_url = " AND access_url_id = $urlId AND url_rel_session.session_id = $session_id ";
  3163. }
  3164. }
  3165. /* This query is very similar to the query below, but it will check the
  3166. session_rel_course_user table if there are courses registered
  3167. to our user or not */
  3168. $sql = "SELECT DISTINCT
  3169. c.visibility,
  3170. c.id as real_id,
  3171. c.code as course_code,
  3172. sc.position
  3173. FROM $tbl_session_course_user as scu
  3174. INNER JOIN $tbl_session_course sc
  3175. ON (scu.session_id = sc.session_id AND scu.c_id = sc.c_id)
  3176. INNER JOIN $tableCourse as c
  3177. ON (scu.c_id = c.id)
  3178. $join_access_url
  3179. WHERE
  3180. scu.user_id = $user_id AND
  3181. scu.session_id = $session_id
  3182. $where_access_url
  3183. ORDER BY sc.position ASC";
  3184. $myCourseList = [];
  3185. $courses = [];
  3186. $result = Database::query($sql);
  3187. if (Database::num_rows($result) > 0) {
  3188. while ($result_row = Database::fetch_array($result, 'ASSOC')) {
  3189. $result_row['status'] = 5;
  3190. if (!in_array($result_row['real_id'], $courses)) {
  3191. $position = $result_row['position'];
  3192. if (!isset($myCourseList[$position])) {
  3193. $myCourseList[$position] = $result_row;
  3194. } else {
  3195. $myCourseList[] = $result_row;
  3196. }
  3197. $courses[] = $result_row['real_id'];
  3198. }
  3199. }
  3200. }
  3201. if (api_is_allowed_to_create_course()) {
  3202. $sql = "SELECT DISTINCT
  3203. c.visibility,
  3204. c.id as real_id,
  3205. c.code as course_code,
  3206. sc.position
  3207. FROM $tbl_session_course_user as scu
  3208. INNER JOIN $tbl_session as s
  3209. ON (scu.session_id = s.id)
  3210. INNER JOIN $tbl_session_course sc
  3211. ON (scu.session_id = sc.session_id AND scu.c_id = sc.c_id)
  3212. INNER JOIN $tableCourse as c
  3213. ON (scu.c_id = c.id)
  3214. $join_access_url
  3215. WHERE
  3216. s.id = $session_id AND
  3217. (
  3218. (scu.user_id = $user_id AND scu.status = 2) OR
  3219. s.id_coach = $user_id
  3220. )
  3221. $where_access_url
  3222. ORDER BY sc.position ASC";
  3223. $result = Database::query($sql);
  3224. if (Database::num_rows($result) > 0) {
  3225. while ($result_row = Database::fetch_array($result, 'ASSOC')) {
  3226. $result_row['status'] = 2;
  3227. if (!in_array($result_row['real_id'], $courses)) {
  3228. $position = $result_row['position'];
  3229. if (!isset($myCourseList[$position])) {
  3230. $myCourseList[$position] = $result_row;
  3231. } else {
  3232. $myCourseList[] = $result_row;
  3233. }
  3234. $courses[] = $result_row['real_id'];
  3235. }
  3236. }
  3237. }
  3238. }
  3239. if (api_is_drh()) {
  3240. $sessionList = SessionManager::get_sessions_followed_by_drh($user_id);
  3241. $sessionList = array_keys($sessionList);
  3242. if (in_array($session_id, $sessionList)) {
  3243. $courseList = SessionManager::get_course_list_by_session_id($session_id);
  3244. if (!empty($courseList)) {
  3245. foreach ($courseList as $course) {
  3246. if (!in_array($course['id'], $courses)) {
  3247. $position = $course['position'];
  3248. if (!isset($myCourseList[$position])) {
  3249. $myCourseList[$position] = $course;
  3250. } else {
  3251. $myCourseList[] = $course;
  3252. }
  3253. }
  3254. }
  3255. }
  3256. }
  3257. } else {
  3258. //check if user is general coach for this session
  3259. $sessionInfo = api_get_session_info($session_id);
  3260. if ($sessionInfo['id_coach'] == $user_id) {
  3261. $courseList = SessionManager::get_course_list_by_session_id($session_id);
  3262. if (!empty($courseList)) {
  3263. foreach ($courseList as $course) {
  3264. if (!in_array($course['id'], $courses)) {
  3265. $position = $course['position'];
  3266. if (!isset($myCourseList[$position])) {
  3267. $myCourseList[$position] = $course;
  3268. } else {
  3269. $myCourseList[] = $course;
  3270. }
  3271. }
  3272. }
  3273. }
  3274. }
  3275. }
  3276. if (!empty($myCourseList)) {
  3277. ksort($myCourseList);
  3278. }
  3279. return $myCourseList;
  3280. }
  3281. /**
  3282. * Get user id from a username.
  3283. *
  3284. * @param string $username
  3285. *
  3286. * @return int User ID (or false if not found)
  3287. */
  3288. public static function get_user_id_from_username($username)
  3289. {
  3290. if (empty($username)) {
  3291. return false;
  3292. }
  3293. $username = trim($username);
  3294. $username = Database::escape_string($username);
  3295. $t_user = Database::get_main_table(TABLE_MAIN_USER);
  3296. $sql = "SELECT id FROM $t_user WHERE username = '$username'";
  3297. $res = Database::query($sql);
  3298. if ($res === false) {
  3299. return false;
  3300. }
  3301. if (Database::num_rows($res) !== 1) {
  3302. return false;
  3303. }
  3304. $row = Database::fetch_array($res);
  3305. return $row['id'];
  3306. }
  3307. /**
  3308. * Get the users files upload from his share_folder.
  3309. *
  3310. * @param string $user_id User ID
  3311. * @param string $course course directory
  3312. * @param string $resourceType resource type: images, all
  3313. *
  3314. * @return string
  3315. */
  3316. public static function get_user_upload_files_by_course(
  3317. $user_id,
  3318. $course,
  3319. $resourceType = 'all'
  3320. ) {
  3321. $return = '';
  3322. $user_id = (int) $user_id;
  3323. if (!empty($user_id) && !empty($course)) {
  3324. $path = api_get_path(SYS_COURSE_PATH).$course.'/document/shared_folder/sf_user_'.$user_id.'/';
  3325. $web_path = api_get_path(WEB_COURSE_PATH).$course.'/document/shared_folder/sf_user_'.$user_id.'/';
  3326. $file_list = [];
  3327. if (is_dir($path)) {
  3328. $handle = opendir($path);
  3329. while ($file = readdir($handle)) {
  3330. if ($file == '.' || $file == '..' || $file == '.htaccess' || is_dir($path.$file)) {
  3331. continue; // skip current/parent directory and .htaccess
  3332. }
  3333. $file_list[] = $file;
  3334. }
  3335. if (count($file_list) > 0) {
  3336. $return = "<h4>$course</h4>";
  3337. $return .= '<ul class="thumbnails">';
  3338. }
  3339. $extensionList = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tif'];
  3340. foreach ($file_list as $file) {
  3341. if ($resourceType == 'all') {
  3342. $return .= '<li>
  3343. <a href="'.$web_path.urlencode($file).'" target="_blank">'.htmlentities($file).'</a></li>';
  3344. } elseif ($resourceType == 'images') {
  3345. //get extension
  3346. $ext = explode('.', $file);
  3347. if (isset($ext[1]) && in_array($ext[1], $extensionList)) {
  3348. $return .= '<li class="span2">
  3349. <a class="thumbnail" href="'.$web_path.urlencode($file).'" target="_blank">
  3350. <img src="'.$web_path.urlencode($file).'" >
  3351. </a>
  3352. </li>';
  3353. }
  3354. }
  3355. }
  3356. if (count($file_list) > 0) {
  3357. $return .= '</ul>';
  3358. }
  3359. }
  3360. }
  3361. return $return;
  3362. }
  3363. /**
  3364. * Gets the API key (or keys) and return them into an array.
  3365. *
  3366. * @param int Optional user id (defaults to the result of api_get_user_id())
  3367. * @param string $api_service
  3368. *
  3369. * @return mixed Non-indexed array containing the list of API keys for this user, or FALSE on error
  3370. */
  3371. public static function get_api_keys($user_id = null, $api_service = 'dokeos')
  3372. {
  3373. if ($user_id != strval(intval($user_id))) {
  3374. return false;
  3375. }
  3376. if (empty($user_id)) {
  3377. $user_id = api_get_user_id();
  3378. }
  3379. if ($user_id === false) {
  3380. return false;
  3381. }
  3382. $service_name = Database::escape_string($api_service);
  3383. if (is_string($service_name) === false) {
  3384. return false;
  3385. }
  3386. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  3387. $sql = "SELECT * FROM $t_api WHERE user_id = $user_id AND api_service='$api_service';";
  3388. $res = Database::query($sql);
  3389. if ($res === false) {
  3390. return false;
  3391. } //error during query
  3392. $num = Database::num_rows($res);
  3393. if ($num == 0) {
  3394. return false;
  3395. }
  3396. $list = [];
  3397. while ($row = Database::fetch_array($res)) {
  3398. $list[$row['id']] = $row['api_key'];
  3399. }
  3400. return $list;
  3401. }
  3402. /**
  3403. * Adds a new API key to the users' account.
  3404. *
  3405. * @param int Optional user ID (defaults to the results of api_get_user_id())
  3406. * @param string $api_service
  3407. *
  3408. * @return bool True on success, false on failure
  3409. */
  3410. public static function add_api_key($user_id = null, $api_service = 'dokeos')
  3411. {
  3412. if ($user_id != strval(intval($user_id))) {
  3413. return false;
  3414. }
  3415. if (empty($user_id)) {
  3416. $user_id = api_get_user_id();
  3417. }
  3418. if ($user_id === false) {
  3419. return false;
  3420. }
  3421. $service_name = Database::escape_string($api_service);
  3422. if (is_string($service_name) === false) {
  3423. return false;
  3424. }
  3425. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  3426. $md5 = md5((time() + ($user_id * 5)) - rand(10000, 10000)); //generate some kind of random key
  3427. $sql = "INSERT INTO $t_api (user_id, api_key,api_service) VALUES ($user_id,'$md5','$service_name')";
  3428. $res = Database::query($sql);
  3429. if ($res === false) {
  3430. return false;
  3431. } //error during query
  3432. $num = Database::insert_id();
  3433. return $num == 0 ? false : $num;
  3434. }
  3435. /**
  3436. * Deletes an API key from the user's account.
  3437. *
  3438. * @param int API key's internal ID
  3439. *
  3440. * @return bool True on success, false on failure
  3441. */
  3442. public static function delete_api_key($key_id)
  3443. {
  3444. if ($key_id != strval(intval($key_id))) {
  3445. return false;
  3446. }
  3447. if ($key_id === false) {
  3448. return false;
  3449. }
  3450. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  3451. $sql = "SELECT * FROM $t_api WHERE id = ".$key_id;
  3452. $res = Database::query($sql);
  3453. if ($res === false) {
  3454. return false;
  3455. } //error during query
  3456. $num = Database::num_rows($res);
  3457. if ($num !== 1) {
  3458. return false;
  3459. }
  3460. $sql = "DELETE FROM $t_api WHERE id = ".$key_id;
  3461. $res = Database::query($sql);
  3462. if ($res === false) {
  3463. return false;
  3464. } //error during query
  3465. return true;
  3466. }
  3467. /**
  3468. * Regenerate an API key from the user's account.
  3469. *
  3470. * @param int user ID (defaults to the results of api_get_user_id())
  3471. * @param string API key's internal ID
  3472. *
  3473. * @return int num
  3474. */
  3475. public static function update_api_key($user_id, $api_service)
  3476. {
  3477. if ($user_id != strval(intval($user_id))) {
  3478. return false;
  3479. }
  3480. if ($user_id === false) {
  3481. return false;
  3482. }
  3483. $service_name = Database::escape_string($api_service);
  3484. if (is_string($service_name) === false) {
  3485. return false;
  3486. }
  3487. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  3488. $sql = "SELECT id FROM $t_api
  3489. WHERE user_id=".$user_id." AND api_service='".$api_service."'";
  3490. $res = Database::query($sql);
  3491. $num = Database::num_rows($res);
  3492. if ($num == 1) {
  3493. $id_key = Database::fetch_array($res, 'ASSOC');
  3494. self::delete_api_key($id_key['id']);
  3495. $num = self::add_api_key($user_id, $api_service);
  3496. } elseif ($num == 0) {
  3497. $num = self::add_api_key($user_id, $api_service);
  3498. }
  3499. return $num;
  3500. }
  3501. /**
  3502. * @param int user ID (defaults to the results of api_get_user_id())
  3503. * @param string API key's internal ID
  3504. *
  3505. * @return int row ID, or return false if not found
  3506. */
  3507. public static function get_api_key_id($user_id, $api_service)
  3508. {
  3509. if ($user_id != strval(intval($user_id))) {
  3510. return false;
  3511. }
  3512. if ($user_id === false) {
  3513. return false;
  3514. }
  3515. if (empty($api_service)) {
  3516. return false;
  3517. }
  3518. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  3519. $api_service = Database::escape_string($api_service);
  3520. $sql = "SELECT id FROM $t_api
  3521. WHERE user_id=".$user_id." AND api_service='".$api_service."'";
  3522. $res = Database::query($sql);
  3523. if (Database::num_rows($res) < 1) {
  3524. return false;
  3525. }
  3526. $row = Database::fetch_array($res, 'ASSOC');
  3527. return $row['id'];
  3528. }
  3529. /**
  3530. * Checks if a user_id is platform admin.
  3531. *
  3532. * @param int user ID
  3533. *
  3534. * @return bool True if is admin, false otherwise
  3535. *
  3536. * @see main_api.lib.php::api_is_platform_admin() for a context-based check
  3537. */
  3538. public static function is_admin($user_id)
  3539. {
  3540. $user_id = (int) $user_id;
  3541. if (empty($user_id)) {
  3542. return false;
  3543. }
  3544. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  3545. $sql = "SELECT * FROM $admin_table WHERE user_id = $user_id";
  3546. $res = Database::query($sql);
  3547. return Database::num_rows($res) === 1;
  3548. }
  3549. /**
  3550. * Get the total count of users.
  3551. *
  3552. * @param int Status of users to be counted
  3553. * @param int Access URL ID (optional)
  3554. *
  3555. * @return mixed Number of users or false on error
  3556. */
  3557. public static function get_number_of_users($status = 0, $access_url_id = 1)
  3558. {
  3559. $t_u = Database::get_main_table(TABLE_MAIN_USER);
  3560. $t_a = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3561. if (api_is_multiple_url_enabled()) {
  3562. $sql = "SELECT count(u.id)
  3563. FROM $t_u u
  3564. INNER JOIN $t_a url_user
  3565. ON (u.id = url_user.user_id)
  3566. WHERE url_user.access_url_id = $access_url_id
  3567. ";
  3568. } else {
  3569. $sql = "SELECT count(u.id)
  3570. FROM $t_u u
  3571. WHERE 1 = 1 ";
  3572. }
  3573. if (is_int($status) && $status > 0) {
  3574. $sql .= " AND u.status = $status ";
  3575. }
  3576. $res = Database::query($sql);
  3577. if (Database::num_rows($res) === 1) {
  3578. return (int) Database::result($res, 0, 0);
  3579. }
  3580. return false;
  3581. }
  3582. /**
  3583. * Gets the tags of a specific field_id
  3584. * USER TAGS.
  3585. *
  3586. * Instructions to create a new user tag by Julio Montoya <gugli100@gmail.com>
  3587. *
  3588. * 1. Create a new extra field in main/admin/user_fields.php with the "TAG" field type make it available and visible.
  3589. * Called it "books" for example.
  3590. * 2. Go to profile main/auth/profile.php There you will see a special input (facebook style) that will show suggestions of tags.
  3591. * 3. All the tags are registered in the user_tag table and the relationship between user and tags is in the user_rel_tag table
  3592. * 4. Tags are independent this means that tags can't be shared between tags + book + hobbies.
  3593. * 5. Test and enjoy.
  3594. *
  3595. * @param string $tag
  3596. * @param int $field_id field_id
  3597. * @param string $return_format how we are going to result value in array or in a string (json)
  3598. * @param $limit
  3599. *
  3600. * @return mixed
  3601. */
  3602. public static function get_tags($tag, $field_id, $return_format = 'json', $limit = 10)
  3603. {
  3604. // database table definition
  3605. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3606. $field_id = (int) $field_id;
  3607. $limit = (int) $limit;
  3608. $tag = trim(Database::escape_string($tag));
  3609. // all the information of the field
  3610. $sql = "SELECT DISTINCT id, tag from $table_user_tag
  3611. WHERE field_id = $field_id AND tag LIKE '$tag%' ORDER BY tag LIMIT $limit";
  3612. $result = Database::query($sql);
  3613. $return = [];
  3614. if (Database::num_rows($result) > 0) {
  3615. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3616. $return[] = ['id' => $row['tag'], 'text' => $row['tag']];
  3617. }
  3618. }
  3619. if ($return_format === 'json') {
  3620. $return = json_encode($return);
  3621. }
  3622. return $return;
  3623. }
  3624. /**
  3625. * @param int $field_id
  3626. * @param int $limit
  3627. *
  3628. * @return array
  3629. */
  3630. public static function get_top_tags($field_id, $limit = 100)
  3631. {
  3632. // database table definition
  3633. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3634. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3635. $field_id = (int) $field_id;
  3636. $limit = (int) $limit;
  3637. // all the information of the field
  3638. $sql = "SELECT count(*) count, tag FROM $table_user_tag_values uv
  3639. INNER JOIN $table_user_tag ut
  3640. ON (ut.id = uv.tag_id)
  3641. WHERE field_id = $field_id
  3642. GROUP BY tag_id
  3643. ORDER BY count DESC
  3644. LIMIT $limit";
  3645. $result = Database::query($sql);
  3646. $return = [];
  3647. if (Database::num_rows($result) > 0) {
  3648. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3649. $return[] = $row;
  3650. }
  3651. }
  3652. return $return;
  3653. }
  3654. /**
  3655. * Get user's tags.
  3656. *
  3657. * @param int $user_id
  3658. * @param int $field_id
  3659. *
  3660. * @return array
  3661. */
  3662. public static function get_user_tags($user_id, $field_id)
  3663. {
  3664. // database table definition
  3665. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3666. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3667. $field_id = (int) $field_id;
  3668. $user_id = (int) $user_id;
  3669. // all the information of the field
  3670. $sql = "SELECT ut.id, tag, count
  3671. FROM $table_user_tag ut
  3672. INNER JOIN $table_user_tag_values uv
  3673. ON (uv.tag_id=ut.ID)
  3674. WHERE field_id = $field_id AND user_id = $user_id
  3675. ORDER BY tag";
  3676. $result = Database::query($sql);
  3677. $return = [];
  3678. if (Database::num_rows($result) > 0) {
  3679. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3680. $return[$row['id']] = ['tag' => $row['tag'], 'count' => $row['count']];
  3681. }
  3682. }
  3683. return $return;
  3684. }
  3685. /**
  3686. * Get user's tags.
  3687. *
  3688. * @param int $user_id
  3689. * @param int $field_id
  3690. * @param bool $show_links show links or not
  3691. *
  3692. * @return string
  3693. */
  3694. public static function get_user_tags_to_string($user_id, $field_id, $show_links = true)
  3695. {
  3696. // database table definition
  3697. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3698. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3699. $field_id = (int) $field_id;
  3700. $user_id = (int) $user_id;
  3701. // all the information of the field
  3702. $sql = "SELECT ut.id, tag,count FROM $table_user_tag ut
  3703. INNER JOIN $table_user_tag_values uv
  3704. ON (uv.tag_id = ut.id)
  3705. WHERE field_id = $field_id AND user_id = $user_id
  3706. ORDER BY tag";
  3707. $result = Database::query($sql);
  3708. $return = [];
  3709. if (Database::num_rows($result) > 0) {
  3710. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3711. $return[$row['id']] = ['tag' => $row['tag'], 'count' => $row['count']];
  3712. }
  3713. }
  3714. $user_tags = $return;
  3715. $tag_tmp = [];
  3716. foreach ($user_tags as $tag) {
  3717. if ($show_links) {
  3718. $tag_tmp[] = '<a href="'.api_get_path(WEB_PATH).'main/search/index.php?q='.$tag['tag'].'">'.
  3719. $tag['tag'].
  3720. '</a>';
  3721. } else {
  3722. $tag_tmp[] = $tag['tag'];
  3723. }
  3724. }
  3725. if (is_array($user_tags) && count($user_tags) > 0) {
  3726. return implode(', ', $tag_tmp);
  3727. } else {
  3728. return '';
  3729. }
  3730. }
  3731. /**
  3732. * Get the tag id.
  3733. *
  3734. * @param int $tag
  3735. * @param int $field_id
  3736. *
  3737. * @return int returns 0 if fails otherwise the tag id
  3738. */
  3739. public static function get_tag_id($tag, $field_id)
  3740. {
  3741. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3742. $tag = Database::escape_string($tag);
  3743. $field_id = (int) $field_id;
  3744. //with COLLATE latin1_bin to select query in a case sensitive mode
  3745. $sql = "SELECT id FROM $table_user_tag
  3746. WHERE tag LIKE '$tag' AND field_id = $field_id";
  3747. $result = Database::query($sql);
  3748. if (Database::num_rows($result) > 0) {
  3749. $row = Database::fetch_array($result, 'ASSOC');
  3750. return $row['id'];
  3751. } else {
  3752. return 0;
  3753. }
  3754. }
  3755. /**
  3756. * Get the tag id.
  3757. *
  3758. * @param int $tag_id
  3759. * @param int $field_id
  3760. *
  3761. * @return int 0 if fails otherwise the tag id
  3762. */
  3763. public static function get_tag_id_from_id($tag_id, $field_id)
  3764. {
  3765. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3766. $tag_id = (int) $tag_id;
  3767. $field_id = (int) $field_id;
  3768. $sql = "SELECT id FROM $table_user_tag
  3769. WHERE id = '$tag_id' AND field_id = $field_id";
  3770. $result = Database::query($sql);
  3771. if (Database::num_rows($result) > 0) {
  3772. $row = Database::fetch_array($result, 'ASSOC');
  3773. return $row['id'];
  3774. } else {
  3775. return false;
  3776. }
  3777. }
  3778. /**
  3779. * Adds a user-tag value.
  3780. *
  3781. * @param mixed $tag
  3782. * @param int $user_id
  3783. * @param int $field_id field id of the tag
  3784. *
  3785. * @return bool True if the tag was inserted or updated. False otherwise.
  3786. * The return value doesn't take into account *values* added to the tag.
  3787. * Only the creation/update of the tag field itself.
  3788. */
  3789. public static function add_tag($tag, $user_id, $field_id)
  3790. {
  3791. // database table definition
  3792. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3793. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3794. $tag = trim(Database::escape_string($tag));
  3795. $user_id = (int) $user_id;
  3796. $field_id = (int) $field_id;
  3797. $tag_id = self::get_tag_id($tag, $field_id);
  3798. /* IMPORTANT
  3799. * @todo we don't create tags with numbers
  3800. *
  3801. */
  3802. if (is_numeric($tag)) {
  3803. //the form is sending an id this means that the user select it from the list so it MUST exists
  3804. /* $new_tag_id = self::get_tag_id_from_id($tag,$field_id);
  3805. if ($new_tag_id !== false) {
  3806. $sql = "UPDATE $table_user_tag SET count = count + 1 WHERE id = $new_tag_id";
  3807. $result = Database::query($sql);
  3808. $last_insert_id = $new_tag_id;
  3809. } else {
  3810. $sql = "INSERT INTO $table_user_tag (tag, field_id,count) VALUES ('$tag','$field_id', count + 1)";
  3811. $result = Database::query($sql);
  3812. $last_insert_id = Database::insert_id();
  3813. } */
  3814. }
  3815. //this is a new tag
  3816. if ($tag_id == 0) {
  3817. //the tag doesn't exist
  3818. $sql = "INSERT INTO $table_user_tag (tag, field_id,count) VALUES ('$tag','$field_id', count + 1)";
  3819. Database::query($sql);
  3820. $last_insert_id = Database::insert_id();
  3821. } else {
  3822. //the tag exists we update it
  3823. $sql = "UPDATE $table_user_tag SET count = count + 1 WHERE id = $tag_id";
  3824. Database::query($sql);
  3825. $last_insert_id = $tag_id;
  3826. }
  3827. if (!empty($last_insert_id) && ($last_insert_id != 0)) {
  3828. //we insert the relationship user-tag
  3829. $sql = "SELECT tag_id FROM $table_user_tag_values
  3830. WHERE user_id = $user_id AND tag_id = $last_insert_id ";
  3831. $result = Database::query($sql);
  3832. //if the relationship does not exist we create it
  3833. if (Database::num_rows($result) == 0) {
  3834. $sql = "INSERT INTO $table_user_tag_values SET user_id = $user_id, tag_id = $last_insert_id";
  3835. Database::query($sql);
  3836. }
  3837. return true;
  3838. }
  3839. return false;
  3840. }
  3841. /**
  3842. * Deletes an user tag.
  3843. *
  3844. * @param int $user_id
  3845. * @param int $field_id
  3846. */
  3847. public static function delete_user_tags($user_id, $field_id)
  3848. {
  3849. // database table definition
  3850. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3851. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3852. $user_id = (int) $user_id;
  3853. $tags = self::get_user_tags($user_id, $field_id);
  3854. if (is_array($tags) && count($tags) > 0) {
  3855. foreach ($tags as $key => $tag) {
  3856. if ($tag['count'] > '0') {
  3857. $sql = "UPDATE $table_user_tag SET count = count - 1 WHERE id = $key ";
  3858. Database::query($sql);
  3859. }
  3860. $sql = "DELETE FROM $table_user_tag_values
  3861. WHERE user_id = $user_id AND tag_id = $key";
  3862. Database::query($sql);
  3863. }
  3864. }
  3865. }
  3866. /**
  3867. * Process the tag list comes from the UserManager::update_extra_field_value() function.
  3868. *
  3869. * @param array $tags the tag list that will be added
  3870. * @param int $user_id
  3871. * @param int $field_id
  3872. *
  3873. * @return bool
  3874. */
  3875. public static function process_tags($tags, $user_id, $field_id)
  3876. {
  3877. // We loop the tags and add it to the DB
  3878. if (is_array($tags)) {
  3879. foreach ($tags as $tag) {
  3880. self::add_tag($tag, $user_id, $field_id);
  3881. }
  3882. } else {
  3883. self::add_tag($tags, $user_id, $field_id);
  3884. }
  3885. return true;
  3886. }
  3887. /**
  3888. * Returns a list of all administrators.
  3889. *
  3890. * @return array
  3891. */
  3892. public static function get_all_administrators()
  3893. {
  3894. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  3895. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  3896. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3897. $access_url_id = api_get_current_access_url_id();
  3898. if (api_get_multiple_access_url()) {
  3899. $sql = "SELECT admin.user_id, username, firstname, lastname, email, active
  3900. FROM $tbl_url_rel_user as url
  3901. INNER JOIN $table_admin as admin
  3902. ON (admin.user_id=url.user_id)
  3903. INNER JOIN $table_user u
  3904. ON (u.id=admin.user_id)
  3905. WHERE access_url_id ='".$access_url_id."'";
  3906. } else {
  3907. $sql = "SELECT admin.user_id, username, firstname, lastname, email, active
  3908. FROM $table_admin as admin
  3909. INNER JOIN $table_user u
  3910. ON (u.id=admin.user_id)";
  3911. }
  3912. $result = Database::query($sql);
  3913. $return = [];
  3914. if (Database::num_rows($result) > 0) {
  3915. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3916. $return[$row['user_id']] = $row;
  3917. }
  3918. }
  3919. return $return;
  3920. }
  3921. /**
  3922. * Search an user (tags, first name, last name and email ).
  3923. *
  3924. * @param string $tag
  3925. * @param int $field_id field id of the tag
  3926. * @param int $from where to start in the query
  3927. * @param int $number_of_items
  3928. * @param bool $getCount get count or not
  3929. *
  3930. * @return array
  3931. */
  3932. public static function get_all_user_tags(
  3933. $tag,
  3934. $field_id = 0,
  3935. $from = 0,
  3936. $number_of_items = 10,
  3937. $getCount = false
  3938. ) {
  3939. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  3940. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3941. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3942. $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3943. $field_id = intval($field_id);
  3944. $from = intval($from);
  3945. $number_of_items = intval($number_of_items);
  3946. $where_field = "";
  3947. $where_extra_fields = self::get_search_form_where_extra_fields();
  3948. if ($field_id != 0) {
  3949. $where_field = " field_id = $field_id AND ";
  3950. }
  3951. // all the information of the field
  3952. if ($getCount) {
  3953. $select = "SELECT count(DISTINCT u.id) count";
  3954. } else {
  3955. $select = "SELECT DISTINCT u.id, u.username, firstname, lastname, email, tag, picture_uri";
  3956. }
  3957. $sql = " $select
  3958. FROM $user_table u
  3959. INNER JOIN $access_url_rel_user_table url_rel_user
  3960. ON (u.id = url_rel_user.user_id)
  3961. LEFT JOIN $table_user_tag_values uv
  3962. ON (u.id AND uv.user_id AND uv.user_id = url_rel_user.user_id)
  3963. LEFT JOIN $table_user_tag ut ON (uv.tag_id = ut.id)
  3964. WHERE
  3965. ($where_field tag LIKE '".Database::escape_string($tag."%")."') OR
  3966. (
  3967. u.firstname LIKE '".Database::escape_string("%".$tag."%")."' OR
  3968. u.lastname LIKE '".Database::escape_string("%".$tag."%")."' OR
  3969. u.username LIKE '".Database::escape_string("%".$tag."%")."' OR
  3970. concat(u.firstname, ' ', u.lastname) LIKE '".Database::escape_string("%".$tag."%")."' OR
  3971. concat(u.lastname, ' ', u.firstname) LIKE '".Database::escape_string("%".$tag."%")."'
  3972. )
  3973. ".(!empty($where_extra_fields) ? $where_extra_fields : '')."
  3974. AND url_rel_user.access_url_id=".api_get_current_access_url_id();
  3975. $keyword_active = true;
  3976. // only active users
  3977. if ($keyword_active) {
  3978. $sql .= " AND u.active='1'";
  3979. }
  3980. // avoid anonymous
  3981. $sql .= " AND u.status <> 6 ";
  3982. $sql .= " ORDER BY username";
  3983. $sql .= " LIMIT $from , $number_of_items";
  3984. $result = Database::query($sql);
  3985. $return = [];
  3986. if (Database::num_rows($result) > 0) {
  3987. if ($getCount) {
  3988. $row = Database::fetch_array($result, 'ASSOC');
  3989. return $row['count'];
  3990. }
  3991. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3992. if (isset($return[$row['id']]) &&
  3993. !empty($return[$row['id']]['tag'])
  3994. ) {
  3995. $url = Display::url(
  3996. $row['tag'],
  3997. api_get_path(WEB_PATH).'main/social/search.php?q='.$row['tag'],
  3998. ['class' => 'tag']
  3999. );
  4000. $row['tag'] = $url;
  4001. }
  4002. $return[$row['id']] = $row;
  4003. }
  4004. }
  4005. return $return;
  4006. }
  4007. /**
  4008. * Get extra filterable user fields (only type select).
  4009. *
  4010. * @return array Array of extra fields as [int => ['name' => ..., 'variable' => ..., 'data' => ...]] (
  4011. * or empty array if no extra field)
  4012. */
  4013. public static function getExtraFilterableFields()
  4014. {
  4015. $extraFieldList = self::get_extra_fields();
  4016. $fields = [];
  4017. if (is_array($extraFieldList)) {
  4018. foreach ($extraFieldList as $extraField) {
  4019. // If is enabled to filter and is a "<select>" field type
  4020. if ($extraField[8] == 1 && $extraField[2] == 4) {
  4021. $fields[] = [
  4022. 'name' => $extraField[3],
  4023. 'variable' => $extraField[1],
  4024. 'data' => $extraField[9],
  4025. ];
  4026. }
  4027. }
  4028. }
  4029. return $fields;
  4030. }
  4031. /**
  4032. * Get extra where clauses for finding users based on extra filterable user fields (type select).
  4033. *
  4034. * @return string With AND clauses based on user's ID which have the values to search in extra user fields
  4035. * (or empty if no extra field exists)
  4036. */
  4037. public static function get_search_form_where_extra_fields()
  4038. {
  4039. $useExtraFields = false;
  4040. $extraFields = self::getExtraFilterableFields();
  4041. $extraFieldResult = [];
  4042. if (is_array($extraFields) && count($extraFields) > 0) {
  4043. foreach ($extraFields as $extraField) {
  4044. $varName = 'field_'.$extraField['variable'];
  4045. if (self::is_extra_field_available($extraField['variable'])) {
  4046. if (isset($_GET[$varName]) && $_GET[$varName] != '0') {
  4047. $useExtraFields = true;
  4048. $extraFieldResult[] = self::get_extra_user_data_by_value(
  4049. $extraField['variable'],
  4050. $_GET[$varName]
  4051. );
  4052. }
  4053. }
  4054. }
  4055. }
  4056. if ($useExtraFields) {
  4057. $finalResult = [];
  4058. if (count($extraFieldResult) > 1) {
  4059. for ($i = 0; $i < count($extraFieldResult) - 1; $i++) {
  4060. if (is_array($extraFieldResult[$i]) && is_array($extraFieldResult[$i + 1])) {
  4061. $finalResult = array_intersect($extraFieldResult[$i], $extraFieldResult[$i + 1]);
  4062. }
  4063. }
  4064. } else {
  4065. $finalResult = $extraFieldResult[0];
  4066. }
  4067. if (is_array($finalResult) && count($finalResult) > 0) {
  4068. $whereFilter = " AND u.id IN ('".implode("','", $finalResult)."') ";
  4069. } else {
  4070. //no results
  4071. $whereFilter = " AND u.id = -1 ";
  4072. }
  4073. return $whereFilter;
  4074. }
  4075. return '';
  4076. }
  4077. /**
  4078. * Show the search form.
  4079. *
  4080. * @param string $query the value of the search box
  4081. *
  4082. * @throws Exception
  4083. *
  4084. * @return string HTML form
  4085. */
  4086. public static function get_search_form($query, $defaultParams = [])
  4087. {
  4088. $searchType = isset($_GET['search_type']) ? $_GET['search_type'] : null;
  4089. $form = new FormValidator(
  4090. 'search_user',
  4091. 'get',
  4092. api_get_path(WEB_PATH).'main/social/search.php',
  4093. '',
  4094. [],
  4095. FormValidator::LAYOUT_HORIZONTAL
  4096. );
  4097. $query = Security::remove_XSS($query);
  4098. if (!empty($query)) {
  4099. $form->addHeader(get_lang('Results').' "'.$query.'"');
  4100. }
  4101. $form->addText(
  4102. 'q',
  4103. get_lang('UsersGroups'),
  4104. false,
  4105. [
  4106. 'id' => 'q',
  4107. ]
  4108. );
  4109. $options = [
  4110. 0 => get_lang('Select'),
  4111. 1 => get_lang('User'),
  4112. 2 => get_lang('Group'),
  4113. ];
  4114. $form->addSelect(
  4115. 'search_type',
  4116. get_lang('Type'),
  4117. $options,
  4118. ['onchange' => 'javascript: extra_field_toogle();', 'id' => 'search_type']
  4119. );
  4120. // Extra fields
  4121. //Ofaj
  4122. /*$extraFields = self::getExtraFilterableFields();
  4123. $defaults = [];
  4124. if (is_array($extraFields) && count($extraFields) > 0) {
  4125. foreach ($extraFields as $extraField) {
  4126. $varName = 'field_'.$extraField['variable'];
  4127. $options = [
  4128. 0 => get_lang('Select'),
  4129. ];
  4130. foreach ($extraField['data'] as $option) {
  4131. $checked = '';
  4132. if (isset($_GET[$varName])) {
  4133. if ($_GET[$varName] == $option[1]) {
  4134. $defaults[$option[1]] = true;
  4135. }
  4136. }
  4137. $options[$option[1]] = $option[1];
  4138. }
  4139. $form->addSelect($varName, $extraField['name'], $options);
  4140. }
  4141. }*/
  4142. $defaults['search_type'] = (int) $searchType;
  4143. $defaults['q'] = $query;
  4144. if (!empty($defaultParams)) {
  4145. $defaults = array_merge($defaults, $defaultParams);
  4146. }
  4147. $form->setDefaults($defaults);
  4148. $form->addButtonSearch(get_lang('Search'));
  4149. /*$js = '<script>
  4150. extra_field_toogle();
  4151. function extra_field_toogle() {
  4152. if (jQuery("select[name=search_type]").val() != "1") {
  4153. jQuery(".extra_field").hide();
  4154. } else {
  4155. jQuery(".extra_field").show();
  4156. }
  4157. }
  4158. </script>';*/
  4159. $js = '';
  4160. return $js.$form->returnForm();
  4161. }
  4162. /**
  4163. * Shows the user menu.
  4164. */
  4165. public static function show_menu()
  4166. {
  4167. echo '<div class="actions">';
  4168. echo '<a href="/main/auth/profile.php">'.
  4169. Display::return_icon('profile.png').' '.get_lang('PersonalData').'</a>';
  4170. echo '<a href="/main/messages/inbox.php">'.
  4171. Display::return_icon('inbox.png').' '.get_lang('Inbox').'</a>';
  4172. echo '<a href="/main/messages/outbox.php">'.
  4173. Display::return_icon('outbox.png').' '.get_lang('Outbox').'</a>';
  4174. echo '<span style="float:right; padding-top:7px;">'.
  4175. '<a href="/main/auth/profile.php?show=1">'.
  4176. Display::return_icon('edit.gif').' '.get_lang('Configuration').'</a>';
  4177. echo '</span>';
  4178. echo '</div>';
  4179. }
  4180. /**
  4181. * Allow to register contact to social network.
  4182. *
  4183. * @param int $friend_id user friend id
  4184. * @param int $my_user_id user id
  4185. * @param int $relation_type relation between users see constants definition
  4186. *
  4187. * @return bool
  4188. */
  4189. public static function relate_users($friend_id, $my_user_id, $relation_type)
  4190. {
  4191. $tbl_my_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  4192. $friend_id = (int) $friend_id;
  4193. $my_user_id = (int) $my_user_id;
  4194. $relation_type = (int) $relation_type;
  4195. $sql = 'SELECT COUNT(*) as count FROM '.$tbl_my_friend.'
  4196. WHERE
  4197. friend_user_id='.$friend_id.' AND
  4198. user_id='.$my_user_id.' AND
  4199. relation_type NOT IN('.USER_RELATION_TYPE_RRHH.', '.USER_RELATION_TYPE_BOSS.') ';
  4200. $result = Database::query($sql);
  4201. $row = Database::fetch_array($result, 'ASSOC');
  4202. $current_date = api_get_utc_datetime();
  4203. if ($row['count'] == 0) {
  4204. $sql = 'INSERT INTO '.$tbl_my_friend.'(friend_user_id,user_id,relation_type,last_edit)
  4205. VALUES ('.$friend_id.','.$my_user_id.','.$relation_type.',"'.$current_date.'")';
  4206. Database::query($sql);
  4207. return true;
  4208. }
  4209. $sql = 'SELECT COUNT(*) as count, relation_type FROM '.$tbl_my_friend.'
  4210. WHERE
  4211. friend_user_id='.$friend_id.' AND
  4212. user_id='.$my_user_id.' AND
  4213. relation_type NOT IN('.USER_RELATION_TYPE_RRHH.', '.USER_RELATION_TYPE_BOSS.') ';
  4214. $result = Database::query($sql);
  4215. $row = Database::fetch_array($result, 'ASSOC');
  4216. if ($row['count'] == 1) {
  4217. //only for the case of a RRHH or a Student BOSS
  4218. if ($row['relation_type'] != $relation_type &&
  4219. ($relation_type == USER_RELATION_TYPE_RRHH || $relation_type == USER_RELATION_TYPE_BOSS)
  4220. ) {
  4221. $sql = 'INSERT INTO '.$tbl_my_friend.'(friend_user_id,user_id,relation_type,last_edit)
  4222. VALUES ('.$friend_id.','.$my_user_id.','.$relation_type.',"'.$current_date.'")';
  4223. } else {
  4224. $sql = 'UPDATE '.$tbl_my_friend.' SET relation_type='.$relation_type.'
  4225. WHERE friend_user_id='.$friend_id.' AND user_id='.$my_user_id;
  4226. }
  4227. Database::query($sql);
  4228. return true;
  4229. }
  4230. return false;
  4231. }
  4232. /**
  4233. * Deletes a contact.
  4234. *
  4235. * @param bool $friend_id
  4236. * @param bool $real_removed true will delete ALL friends relationship
  4237. * @param string $with_status_condition
  4238. *
  4239. * @author isaac flores paz <isaac.flores@dokeos.com>
  4240. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  4241. */
  4242. public static function remove_user_rel_user(
  4243. $friend_id,
  4244. $real_removed = false,
  4245. $with_status_condition = ''
  4246. ) {
  4247. $tbl_my_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  4248. $tbl_my_message = Database::get_main_table(TABLE_MESSAGE);
  4249. $friend_id = (int) $friend_id;
  4250. $user_id = api_get_user_id();
  4251. if ($real_removed) {
  4252. $extra_condition = '';
  4253. if ($with_status_condition != '') {
  4254. $extra_condition = ' AND relation_type = '.intval($with_status_condition);
  4255. }
  4256. $sql = 'DELETE FROM '.$tbl_my_friend.'
  4257. WHERE
  4258. relation_type <> '.USER_RELATION_TYPE_RRHH.' AND
  4259. friend_user_id='.$friend_id.' '.$extra_condition;
  4260. Database::query($sql);
  4261. $sql = 'DELETE FROM '.$tbl_my_friend.'
  4262. WHERE
  4263. relation_type <> '.USER_RELATION_TYPE_RRHH.' AND
  4264. user_id='.$friend_id.' '.$extra_condition;
  4265. Database::query($sql);
  4266. } else {
  4267. $sql = 'SELECT COUNT(*) as count FROM '.$tbl_my_friend.'
  4268. WHERE
  4269. user_id='.$user_id.' AND
  4270. relation_type NOT IN('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND
  4271. friend_user_id='.$friend_id;
  4272. $result = Database::query($sql);
  4273. $row = Database::fetch_array($result, 'ASSOC');
  4274. if ($row['count'] == 1) {
  4275. //Delete user rel user
  4276. $sql_i = 'UPDATE '.$tbl_my_friend.' SET relation_type='.USER_RELATION_TYPE_DELETED.'
  4277. WHERE user_id='.$user_id.' AND friend_user_id='.$friend_id;
  4278. $sql_j = 'UPDATE '.$tbl_my_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_DENIED.'
  4279. WHERE
  4280. user_receiver_id='.$user_id.' AND
  4281. user_sender_id='.$friend_id.' AND update_date="0000-00-00 00:00:00" ';
  4282. // Delete user
  4283. $sql_ij = 'UPDATE '.$tbl_my_friend.' SET relation_type='.USER_RELATION_TYPE_DELETED.'
  4284. WHERE user_id='.$friend_id.' AND friend_user_id='.$user_id;
  4285. $sql_ji = 'UPDATE '.$tbl_my_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_DENIED.'
  4286. WHERE
  4287. user_receiver_id='.$friend_id.' AND
  4288. user_sender_id='.$user_id.' AND
  4289. update_date="0000-00-00 00:00:00" ';
  4290. Database::query($sql_i);
  4291. Database::query($sql_j);
  4292. Database::query($sql_ij);
  4293. Database::query($sql_ji);
  4294. }
  4295. }
  4296. // Delete accepted invitations
  4297. $sql = "DELETE FROM $tbl_my_message
  4298. WHERE
  4299. msg_status = ".MESSAGE_STATUS_INVITATION_ACCEPTED." AND
  4300. (
  4301. user_receiver_id = $user_id AND
  4302. user_sender_id = $friend_id
  4303. ) OR
  4304. (
  4305. user_sender_id = $user_id AND
  4306. user_receiver_id = $friend_id
  4307. )
  4308. ";
  4309. Database::query($sql);
  4310. }
  4311. /**
  4312. * @param int $userId
  4313. *
  4314. * @return array
  4315. */
  4316. public static function getDrhListFromUser($userId)
  4317. {
  4318. $tblUser = Database::get_main_table(TABLE_MAIN_USER);
  4319. $tblUserRelUser = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  4320. $tblUserRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  4321. $userId = (int) $userId;
  4322. $orderBy = null;
  4323. if (api_is_western_name_order()) {
  4324. $orderBy .= ' ORDER BY firstname, lastname ';
  4325. } else {
  4326. $orderBy .= ' ORDER BY lastname, firstname ';
  4327. }
  4328. $sql = "SELECT u.id, username, u.firstname, u.lastname
  4329. FROM $tblUser u
  4330. INNER JOIN $tblUserRelUser uru ON (uru.friend_user_id = u.id)
  4331. INNER JOIN $tblUserRelAccessUrl a ON (a.user_id = u.id)
  4332. WHERE
  4333. access_url_id = ".api_get_current_access_url_id()." AND
  4334. uru.user_id = '$userId' AND
  4335. relation_type = '".USER_RELATION_TYPE_RRHH."'
  4336. $orderBy
  4337. ";
  4338. $result = Database::query($sql);
  4339. return Database::store_result($result);
  4340. }
  4341. /**
  4342. * get users followed by human resource manager.
  4343. *
  4344. * @param int $userId
  4345. * @param int $userStatus (STUDENT, COURSEMANAGER, etc)
  4346. * @param bool $getOnlyUserId
  4347. * @param bool $getSql
  4348. * @param bool $getCount
  4349. * @param int $from
  4350. * @param int $numberItems
  4351. * @param int $column
  4352. * @param string $direction
  4353. * @param int $active
  4354. * @param string $lastConnectionDate
  4355. *
  4356. * @return array users
  4357. */
  4358. public static function get_users_followed_by_drh(
  4359. $userId,
  4360. $userStatus = 0,
  4361. $getOnlyUserId = false,
  4362. $getSql = false,
  4363. $getCount = false,
  4364. $from = null,
  4365. $numberItems = null,
  4366. $column = null,
  4367. $direction = null,
  4368. $active = null,
  4369. $lastConnectionDate = null
  4370. ) {
  4371. return self::getUsersFollowedByUser(
  4372. $userId,
  4373. $userStatus,
  4374. $getOnlyUserId,
  4375. $getSql,
  4376. $getCount,
  4377. $from,
  4378. $numberItems,
  4379. $column,
  4380. $direction,
  4381. $active,
  4382. $lastConnectionDate,
  4383. DRH
  4384. );
  4385. }
  4386. /**
  4387. * Get users followed by human resource manager.
  4388. *
  4389. * @param int $userId
  4390. * @param int $userStatus Filter users by status (STUDENT, COURSEMANAGER, etc)
  4391. * @param bool $getOnlyUserId
  4392. * @param bool $getSql
  4393. * @param bool $getCount
  4394. * @param int $from
  4395. * @param int $numberItems
  4396. * @param int $column
  4397. * @param string $direction
  4398. * @param int $active
  4399. * @param string $lastConnectionDate
  4400. * @param int $status the function is called by who? COURSEMANAGER, DRH?
  4401. * @param string $keyword
  4402. *
  4403. * @return mixed Users list (array) or the SQL query if $getSQL was set to true
  4404. */
  4405. public static function getUsersFollowedByUser(
  4406. $userId,
  4407. $userStatus = null,
  4408. $getOnlyUserId = false,
  4409. $getSql = false,
  4410. $getCount = false,
  4411. $from = null,
  4412. $numberItems = null,
  4413. $column = null,
  4414. $direction = null,
  4415. $active = null,
  4416. $lastConnectionDate = null,
  4417. $status = null,
  4418. $keyword = null
  4419. ) {
  4420. // Database Table Definitions
  4421. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  4422. $tbl_user_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  4423. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  4424. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  4425. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  4426. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  4427. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  4428. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  4429. $userId = (int) $userId;
  4430. $limitCondition = '';
  4431. if (isset($from) && isset($numberItems)) {
  4432. $from = (int) $from;
  4433. $numberItems = (int) $numberItems;
  4434. $limitCondition = "LIMIT $from, $numberItems";
  4435. }
  4436. $column = Database::escape_string($column);
  4437. $direction = in_array(strtolower($direction), ['asc', 'desc']) ? $direction : null;
  4438. $userConditions = '';
  4439. if (!empty($userStatus)) {
  4440. $userConditions .= ' AND u.status = '.intval($userStatus);
  4441. }
  4442. $select = " SELECT DISTINCT u.id user_id, u.username, u.lastname, u.firstname, u.email ";
  4443. if ($getOnlyUserId) {
  4444. $select = " SELECT DISTINCT u.id user_id";
  4445. }
  4446. $masterSelect = "SELECT DISTINCT * FROM ";
  4447. if ($getCount) {
  4448. $masterSelect = "SELECT COUNT(DISTINCT(user_id)) as count FROM ";
  4449. $select = " SELECT DISTINCT(u.id) user_id";
  4450. }
  4451. if (!is_null($active)) {
  4452. $active = intval($active);
  4453. $userConditions .= " AND u.active = $active ";
  4454. }
  4455. if (!empty($keyword)) {
  4456. $keyword = Database::escape_string($keyword);
  4457. $userConditions .= " AND (
  4458. u.username LIKE '%$keyword%' OR
  4459. u.firstname LIKE '%$keyword%' OR
  4460. u.lastname LIKE '%$keyword%' OR
  4461. u.official_code LIKE '%$keyword%' OR
  4462. u.email LIKE '%$keyword%'
  4463. )";
  4464. }
  4465. if (!empty($lastConnectionDate)) {
  4466. $lastConnectionDate = Database::escape_string($lastConnectionDate);
  4467. $userConditions .= " AND u.last_login <= '$lastConnectionDate' ";
  4468. }
  4469. $sessionConditionsCoach = null;
  4470. $sessionConditionsTeacher = null;
  4471. $drhConditions = null;
  4472. $teacherSelect = null;
  4473. switch ($status) {
  4474. case DRH:
  4475. $drhConditions .= " AND
  4476. friend_user_id = '$userId' AND
  4477. relation_type = '".USER_RELATION_TYPE_RRHH."'
  4478. ";
  4479. break;
  4480. case COURSEMANAGER:
  4481. $drhConditions .= " AND
  4482. friend_user_id = '$userId' AND
  4483. relation_type = '".USER_RELATION_TYPE_RRHH."'
  4484. ";
  4485. $sessionConditionsCoach .= " AND
  4486. (s.id_coach = '$userId')
  4487. ";
  4488. $sessionConditionsTeacher .= " AND
  4489. (scu.status = 2 AND scu.user_id = '$userId')
  4490. ";
  4491. $teacherSelect =
  4492. "UNION ALL (
  4493. $select
  4494. FROM $tbl_user u
  4495. INNER JOIN $tbl_session_rel_user sru ON (sru.user_id = u.id)
  4496. WHERE
  4497. (
  4498. sru.session_id IN (
  4499. SELECT DISTINCT(s.id) FROM $tbl_session s INNER JOIN
  4500. $tbl_session_rel_access_url session_rel_access_rel_user
  4501. ON session_rel_access_rel_user.session_id = s.id
  4502. WHERE access_url_id = ".api_get_current_access_url_id()."
  4503. $sessionConditionsCoach
  4504. ) OR sru.session_id IN (
  4505. SELECT DISTINCT(s.id) FROM $tbl_session s
  4506. INNER JOIN $tbl_session_rel_access_url url
  4507. ON (url.session_id = s.id)
  4508. INNER JOIN $tbl_session_rel_course_rel_user scu
  4509. ON (scu.session_id = s.id)
  4510. WHERE access_url_id = ".api_get_current_access_url_id()."
  4511. $sessionConditionsTeacher
  4512. )
  4513. )
  4514. $userConditions
  4515. )
  4516. UNION ALL(
  4517. $select
  4518. FROM $tbl_user u
  4519. INNER JOIN $tbl_course_user cu ON (cu.user_id = u.id)
  4520. WHERE cu.c_id IN (
  4521. SELECT DISTINCT(c_id) FROM $tbl_course_user
  4522. WHERE user_id = $userId AND status = ".COURSEMANAGER."
  4523. )
  4524. $userConditions
  4525. )"
  4526. ;
  4527. break;
  4528. case STUDENT_BOSS:
  4529. $drhConditions = " AND friend_user_id = $userId AND relation_type = ".USER_RELATION_TYPE_BOSS;
  4530. break;
  4531. case HRM_REQUEST:
  4532. $drhConditions .= " AND
  4533. friend_user_id = '$userId' AND
  4534. relation_type = '".USER_RELATION_TYPE_HRM_REQUEST."'
  4535. ";
  4536. break;
  4537. }
  4538. $join = null;
  4539. $sql = " $masterSelect
  4540. (
  4541. (
  4542. $select
  4543. FROM $tbl_user u
  4544. INNER JOIN $tbl_user_rel_user uru ON (uru.user_id = u.id)
  4545. LEFT JOIN $tbl_user_rel_access_url a ON (a.user_id = u.id)
  4546. $join
  4547. WHERE
  4548. access_url_id = ".api_get_current_access_url_id()."
  4549. $drhConditions
  4550. $userConditions
  4551. )
  4552. $teacherSelect
  4553. ) as t1";
  4554. if ($getSql) {
  4555. return $sql;
  4556. }
  4557. if ($getCount) {
  4558. $result = Database::query($sql);
  4559. $row = Database::fetch_array($result);
  4560. return $row['count'];
  4561. }
  4562. $orderBy = null;
  4563. if ($getOnlyUserId == false) {
  4564. if (api_is_western_name_order()) {
  4565. $orderBy .= " ORDER BY firstname, lastname ";
  4566. } else {
  4567. $orderBy .= " ORDER BY lastname, firstname ";
  4568. }
  4569. if (!empty($column) && !empty($direction)) {
  4570. // Fixing order due the UNIONs
  4571. $column = str_replace('u.', '', $column);
  4572. $orderBy = " ORDER BY $column $direction ";
  4573. }
  4574. }
  4575. $sql .= $orderBy;
  4576. $sql .= $limitCondition;
  4577. $result = Database::query($sql);
  4578. $users = [];
  4579. if (Database::num_rows($result) > 0) {
  4580. while ($row = Database::fetch_array($result)) {
  4581. $users[$row['user_id']] = $row;
  4582. }
  4583. }
  4584. return $users;
  4585. }
  4586. /**
  4587. * Subscribes users to human resource manager (Dashboard feature).
  4588. *
  4589. * @param int $hr_dept_id
  4590. * @param array $users_id
  4591. * @param bool $deleteOtherAssignedUsers
  4592. *
  4593. * @return int
  4594. */
  4595. public static function subscribeUsersToHRManager(
  4596. $hr_dept_id,
  4597. $users_id,
  4598. $deleteOtherAssignedUsers = true
  4599. ) {
  4600. return self::subscribeUsersToUser(
  4601. $hr_dept_id,
  4602. $users_id,
  4603. USER_RELATION_TYPE_RRHH,
  4604. false,
  4605. $deleteOtherAssignedUsers
  4606. );
  4607. }
  4608. /**
  4609. * Register request to assign users to HRM.
  4610. *
  4611. * @param int $hrmId The HRM ID
  4612. * @param array $usersId The users IDs
  4613. *
  4614. * @return int
  4615. */
  4616. public static function requestUsersToHRManager($hrmId, $usersId)
  4617. {
  4618. return self::subscribeUsersToUser(
  4619. $hrmId,
  4620. $usersId,
  4621. USER_RELATION_TYPE_HRM_REQUEST,
  4622. false,
  4623. false
  4624. );
  4625. }
  4626. /**
  4627. * Remove the requests for assign a user to a HRM.
  4628. *
  4629. * @param User $hrmId
  4630. * @param array $usersId List of user IDs from whom to remove all relations requests with HRM
  4631. */
  4632. public static function clearHrmRequestsForUser(User $hrmId, $usersId)
  4633. {
  4634. $users = implode(', ', $usersId);
  4635. Database::getManager()
  4636. ->createQuery('
  4637. DELETE FROM ChamiloCoreBundle:UserRelUser uru
  4638. WHERE uru.friendUserId = :hrm_id AND uru.relationType = :relation_type AND uru.userId IN (:users_ids)
  4639. ')
  4640. ->execute(['hrm_id' => $hrmId, 'relation_type' => USER_RELATION_TYPE_HRM_REQUEST, 'users_ids' => $users]);
  4641. }
  4642. /**
  4643. * Add subscribed users to a user by relation type.
  4644. *
  4645. * @param int $userId The user id
  4646. * @param array $subscribedUsersId The id of subscribed users
  4647. * @param string $relationType The relation type
  4648. * @param bool $deleteUsersBeforeInsert
  4649. * @param bool $deleteOtherAssignedUsers
  4650. *
  4651. * @return int
  4652. */
  4653. public static function subscribeUsersToUser(
  4654. $userId,
  4655. $subscribedUsersId,
  4656. $relationType,
  4657. $deleteUsersBeforeInsert = false,
  4658. $deleteOtherAssignedUsers = true
  4659. ) {
  4660. $userRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  4661. $userRelAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  4662. $userId = (int) $userId;
  4663. $relationType = (int) $relationType;
  4664. $affectedRows = 0;
  4665. if ($deleteOtherAssignedUsers) {
  4666. if (api_get_multiple_access_url()) {
  4667. // Deleting assigned users to hrm_id
  4668. $sql = "SELECT s.user_id
  4669. FROM $userRelUserTable s
  4670. INNER JOIN $userRelAccessUrlTable a
  4671. ON (a.user_id = s.user_id)
  4672. WHERE
  4673. friend_user_id = $userId AND
  4674. relation_type = $relationType AND
  4675. access_url_id = ".api_get_current_access_url_id();
  4676. } else {
  4677. $sql = "SELECT user_id
  4678. FROM $userRelUserTable
  4679. WHERE
  4680. friend_user_id = $userId AND
  4681. relation_type = $relationType";
  4682. }
  4683. $result = Database::query($sql);
  4684. if (Database::num_rows($result) > 0) {
  4685. while ($row = Database::fetch_array($result)) {
  4686. $sql = "DELETE FROM $userRelUserTable
  4687. WHERE
  4688. user_id = {$row['user_id']} AND
  4689. friend_user_id = $userId AND
  4690. relation_type = $relationType";
  4691. Database::query($sql);
  4692. }
  4693. }
  4694. }
  4695. if ($deleteUsersBeforeInsert) {
  4696. $sql = "DELETE FROM $userRelUserTable
  4697. WHERE
  4698. user_id = $userId AND
  4699. relation_type = $relationType";
  4700. Database::query($sql);
  4701. }
  4702. // Inserting new user list
  4703. if (is_array($subscribedUsersId)) {
  4704. foreach ($subscribedUsersId as $subscribedUserId) {
  4705. $subscribedUserId = (int) $subscribedUserId;
  4706. $sql = "SELECT id
  4707. FROM $userRelUserTable
  4708. WHERE
  4709. user_id = $subscribedUserId AND
  4710. friend_user_id = $userId AND
  4711. relation_type = $relationType";
  4712. $result = Database::query($sql);
  4713. $num = Database::num_rows($result);
  4714. if ($num === 0) {
  4715. $date = api_get_utc_datetime();
  4716. $sql = "INSERT INTO $userRelUserTable (user_id, friend_user_id, relation_type, last_edit)
  4717. VALUES ($subscribedUserId, $userId, $relationType, '$date')";
  4718. $result = Database::query($sql);
  4719. $affectedRows += Database::affected_rows($result);
  4720. }
  4721. }
  4722. }
  4723. return $affectedRows;
  4724. }
  4725. /**
  4726. * This function check if an user is followed by human resources manager.
  4727. *
  4728. * @param int $user_id
  4729. * @param int $hr_dept_id Human resources manager
  4730. *
  4731. * @return bool
  4732. */
  4733. public static function is_user_followed_by_drh($user_id, $hr_dept_id)
  4734. {
  4735. $tbl_user_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  4736. $user_id = (int) $user_id;
  4737. $hr_dept_id = (int) $hr_dept_id;
  4738. $result = false;
  4739. $sql = "SELECT user_id FROM $tbl_user_rel_user
  4740. WHERE
  4741. user_id = $user_id AND
  4742. friend_user_id = $hr_dept_id AND
  4743. relation_type = ".USER_RELATION_TYPE_RRHH;
  4744. $rs = Database::query($sql);
  4745. if (Database::num_rows($rs) > 0) {
  4746. $result = true;
  4747. }
  4748. return $result;
  4749. }
  4750. /**
  4751. * Return the user id of teacher or session administrator.
  4752. *
  4753. * @param array $courseInfo
  4754. *
  4755. * @return mixed The user id, or false if the session ID was negative
  4756. */
  4757. public static function get_user_id_of_course_admin_or_session_admin($courseInfo)
  4758. {
  4759. $session = api_get_session_id();
  4760. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  4761. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  4762. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  4763. if (empty($courseInfo)) {
  4764. return false;
  4765. }
  4766. $courseId = $courseInfo['real_id'];
  4767. if ($session == 0 || is_null($session)) {
  4768. $sql = 'SELECT u.id uid FROM '.$table_user.' u
  4769. INNER JOIN '.$table_course_user.' ru
  4770. ON ru.user_id = u.id
  4771. WHERE
  4772. ru.status = 1 AND
  4773. ru.c_id = "'.$courseId.'" ';
  4774. $rs = Database::query($sql);
  4775. $num_rows = Database::num_rows($rs);
  4776. if ($num_rows == 1) {
  4777. $row = Database::fetch_array($rs);
  4778. return $row['uid'];
  4779. } else {
  4780. $my_num_rows = $num_rows;
  4781. $my_user_id = Database::result($rs, $my_num_rows - 1, 'uid');
  4782. return $my_user_id;
  4783. }
  4784. } elseif ($session > 0) {
  4785. $sql = 'SELECT u.id uid FROM '.$table_user.' u
  4786. INNER JOIN '.$table_session_course_user.' sru
  4787. ON sru.user_id=u.id
  4788. WHERE
  4789. sru.c_id="'.$courseId.'" AND
  4790. sru.status=2';
  4791. $rs = Database::query($sql);
  4792. $row = Database::fetch_array($rs);
  4793. return $row['uid'];
  4794. }
  4795. return false;
  4796. }
  4797. /**
  4798. * Determines if a user is a gradebook certified.
  4799. *
  4800. * @param int $cat_id The category id of gradebook
  4801. * @param int $user_id The user id
  4802. *
  4803. * @return bool
  4804. */
  4805. public static function is_user_certified($cat_id, $user_id)
  4806. {
  4807. $cat_id = (int) $cat_id;
  4808. $user_id = (int) $user_id;
  4809. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  4810. $sql = 'SELECT path_certificate
  4811. FROM '.$table.'
  4812. WHERE
  4813. cat_id = "'.$cat_id.'" AND
  4814. user_id = "'.$user_id.'"';
  4815. $rs = Database::query($sql);
  4816. $row = Database::fetch_array($rs);
  4817. if ($row['path_certificate'] == '' || is_null($row['path_certificate'])) {
  4818. return false;
  4819. }
  4820. return true;
  4821. }
  4822. /**
  4823. * Gets the info about a gradebook certificate for a user by course.
  4824. *
  4825. * @param string $course_code The course code
  4826. * @param int $user_id The user id
  4827. *
  4828. * @return array if there is not information return false
  4829. */
  4830. public static function get_info_gradebook_certificate($course_code, $user_id)
  4831. {
  4832. $tbl_grade_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  4833. $tbl_grade_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  4834. $session_id = api_get_session_id();
  4835. if (empty($session_id)) {
  4836. $session_condition = ' AND (session_id = "" OR session_id = 0 OR session_id IS NULL )';
  4837. } else {
  4838. $session_condition = " AND session_id = $session_id";
  4839. }
  4840. $sql = 'SELECT * FROM '.$tbl_grade_certificate.'
  4841. WHERE cat_id = (
  4842. SELECT id FROM '.$tbl_grade_category.'
  4843. WHERE
  4844. course_code = "'.Database::escape_string($course_code).'" '.$session_condition.'
  4845. LIMIT 1
  4846. ) AND user_id='.intval($user_id);
  4847. $rs = Database::query($sql);
  4848. if (Database::num_rows($rs) > 0) {
  4849. $row = Database::fetch_array($rs, 'ASSOC');
  4850. $score = $row['score_certificate'];
  4851. $category_id = $row['cat_id'];
  4852. $cat = Category::load($category_id);
  4853. $displayscore = ScoreDisplay::instance();
  4854. if (isset($cat) && $displayscore->is_custom()) {
  4855. $grade = $displayscore->display_score(
  4856. [$score, $cat[0]->get_weight()],
  4857. SCORE_DIV_PERCENT_WITH_CUSTOM
  4858. );
  4859. } else {
  4860. $grade = $displayscore->display_score(
  4861. [$score, $cat[0]->get_weight()]
  4862. );
  4863. }
  4864. $row['grade'] = $grade;
  4865. return $row;
  4866. }
  4867. return false;
  4868. }
  4869. /**
  4870. * Gets the user path of user certificated.
  4871. *
  4872. * @param int The user id
  4873. *
  4874. * @return array containing path_certificate and cat_id
  4875. */
  4876. public static function get_user_path_certificate($user_id)
  4877. {
  4878. $my_certificate = [];
  4879. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  4880. $table_gradebook_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  4881. $session_id = api_get_session_id();
  4882. $user_id = (int) $user_id;
  4883. if ($session_id == 0 || is_null($session_id)) {
  4884. $sql_session = 'AND (session_id='.intval($session_id).' OR isnull(session_id)) ';
  4885. } elseif ($session_id > 0) {
  4886. $sql_session = 'AND session_id='.intval($session_id);
  4887. } else {
  4888. $sql_session = '';
  4889. }
  4890. $sql = "SELECT tc.path_certificate,tc.cat_id,tgc.course_code,tgc.name
  4891. FROM $table_certificate tc, $table_gradebook_category tgc
  4892. WHERE tgc.id = tc.cat_id AND tc.user_id = $user_id
  4893. ORDER BY tc.date_certificate DESC
  4894. LIMIT 5";
  4895. $rs = Database::query($sql);
  4896. while ($row = Database::fetch_array($rs)) {
  4897. $my_certificate[] = $row;
  4898. }
  4899. return $my_certificate;
  4900. }
  4901. /**
  4902. * This function check if the user is a coach inside session course.
  4903. *
  4904. * @param int $user_id User id
  4905. * @param int $courseId
  4906. * @param int $session_id
  4907. *
  4908. * @return bool True if the user is a coach
  4909. */
  4910. public static function is_session_course_coach($user_id, $courseId, $session_id)
  4911. {
  4912. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  4913. // Protect data
  4914. $user_id = intval($user_id);
  4915. $courseId = intval($courseId);
  4916. $session_id = intval($session_id);
  4917. $result = false;
  4918. $sql = "SELECT session_id FROM $table
  4919. WHERE
  4920. session_id = $session_id AND
  4921. c_id = $courseId AND
  4922. user_id = $user_id AND
  4923. status = 2 ";
  4924. $res = Database::query($sql);
  4925. if (Database::num_rows($res) > 0) {
  4926. $result = true;
  4927. }
  4928. return $result;
  4929. }
  4930. /**
  4931. * This function returns an icon path that represents the favicon of the website of which the url given.
  4932. * Defaults to the current Chamilo favicon.
  4933. *
  4934. * @param string $url1 URL of website where to look for favicon.ico
  4935. * @param string $url2 Optional second URL of website where to look for favicon.ico
  4936. *
  4937. * @return string Path of icon to load
  4938. */
  4939. public static function get_favicon_from_url($url1, $url2 = null)
  4940. {
  4941. $icon_link = '';
  4942. $url = $url1;
  4943. if (empty($url1)) {
  4944. $url = $url2;
  4945. if (empty($url)) {
  4946. $url = api_get_access_url(api_get_current_access_url_id());
  4947. $url = $url[0];
  4948. }
  4949. }
  4950. if (!empty($url)) {
  4951. $pieces = parse_url($url);
  4952. $icon_link = $pieces['scheme'].'://'.$pieces['host'].'/favicon.ico';
  4953. }
  4954. return $icon_link;
  4955. }
  4956. /**
  4957. * Returns an array of the different types of user extra fields [id => title translation].
  4958. *
  4959. * @return array
  4960. */
  4961. public static function get_user_field_types()
  4962. {
  4963. $types = [];
  4964. $types[self::USER_FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  4965. $types[self::USER_FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  4966. $types[self::USER_FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  4967. $types[self::USER_FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  4968. $types[self::USER_FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  4969. $types[self::USER_FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  4970. $types[self::USER_FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  4971. $types[self::USER_FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  4972. $types[self::USER_FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  4973. $types[self::USER_FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  4974. $types[self::USER_FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  4975. $types[self::USER_FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  4976. $types[self::USER_FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  4977. $types[self::USER_FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  4978. return $types;
  4979. }
  4980. /**
  4981. * @param User $user
  4982. */
  4983. public static function add_user_as_admin(User $user)
  4984. {
  4985. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  4986. if ($user) {
  4987. $userId = $user->getId();
  4988. if (!self::is_admin($userId)) {
  4989. $sql = "INSERT INTO $table_admin SET user_id = $userId";
  4990. Database::query($sql);
  4991. }
  4992. $user->addRole('ROLE_SUPER_ADMIN');
  4993. self::getManager()->updateUser($user, true);
  4994. }
  4995. }
  4996. /**
  4997. * @param int $userId
  4998. */
  4999. public static function remove_user_admin($userId)
  5000. {
  5001. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  5002. $userId = (int) $userId;
  5003. if (self::is_admin($userId)) {
  5004. $sql = "DELETE FROM $table_admin WHERE user_id = $userId";
  5005. Database::query($sql);
  5006. }
  5007. }
  5008. /**
  5009. * @param string $from
  5010. * @param string $to
  5011. */
  5012. public static function update_all_user_languages($from, $to)
  5013. {
  5014. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  5015. $from = Database::escape_string($from);
  5016. $to = Database::escape_string($to);
  5017. if (!empty($to) && !empty($from)) {
  5018. $sql = "UPDATE $table_user SET language = '$to'
  5019. WHERE language = '$from'";
  5020. Database::query($sql);
  5021. }
  5022. }
  5023. /**
  5024. * Subscribe boss to students.
  5025. *
  5026. * @param int $bossId The boss id
  5027. * @param array $usersId The users array
  5028. *
  5029. * @return int Affected rows
  5030. */
  5031. public static function subscribeBossToUsers($bossId, $usersId)
  5032. {
  5033. return self::subscribeUsersToUser(
  5034. $bossId,
  5035. $usersId,
  5036. USER_RELATION_TYPE_BOSS
  5037. );
  5038. }
  5039. /**
  5040. * @param int $userId
  5041. *
  5042. * @return bool
  5043. */
  5044. public static function removeAllBossFromStudent($userId)
  5045. {
  5046. $userId = (int) $userId;
  5047. if (empty($userId)) {
  5048. return false;
  5049. }
  5050. $userRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  5051. $sql = "DELETE FROM $userRelUserTable
  5052. WHERE user_id = $userId AND relation_type = ".USER_RELATION_TYPE_BOSS;
  5053. Database::query($sql);
  5054. return true;
  5055. }
  5056. /**
  5057. * Subscribe boss to students, if $bossList is empty then the boss list will be empty too.
  5058. *
  5059. * @param int $studentId
  5060. * @param array $bossList
  5061. * @param bool $sendNotification
  5062. *
  5063. * @return mixed Affected rows or false on failure
  5064. */
  5065. public static function subscribeUserToBossList(
  5066. $studentId,
  5067. $bossList,
  5068. $sendNotification = false
  5069. ) {
  5070. $inserted = 0;
  5071. if (!empty($bossList)) {
  5072. sort($bossList);
  5073. $studentId = (int) $studentId;
  5074. $studentInfo = api_get_user_info($studentId);
  5075. if (empty($studentInfo)) {
  5076. return false;
  5077. }
  5078. $previousBossList = self::getStudentBossList($studentId);
  5079. $previousBossList = !empty($previousBossList) ? array_column($previousBossList, 'boss_id') : [];
  5080. sort($previousBossList);
  5081. // Boss list is the same, nothing changed.
  5082. if ($bossList == $previousBossList) {
  5083. return false;
  5084. }
  5085. $userRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  5086. self::removeAllBossFromStudent($studentId);
  5087. foreach ($bossList as $bossId) {
  5088. $bossId = (int) $bossId;
  5089. $sql = "INSERT IGNORE INTO $userRelUserTable (user_id, friend_user_id, relation_type)
  5090. VALUES ($studentId, $bossId, ".USER_RELATION_TYPE_BOSS.")";
  5091. $insertId = Database::query($sql);
  5092. if ($insertId) {
  5093. if ($sendNotification) {
  5094. $name = $studentInfo['complete_name'];
  5095. $url = api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$studentId;
  5096. $url = Display::url($url, $url);
  5097. $subject = sprintf(get_lang('UserXHasBeenAssignedToBoss'), $name);
  5098. $message = sprintf(get_lang('UserXHasBeenAssignedToBossWithUrlX'), $name, $url);
  5099. MessageManager::send_message_simple(
  5100. $bossId,
  5101. $subject,
  5102. $message
  5103. );
  5104. }
  5105. $inserted++;
  5106. }
  5107. }
  5108. } else {
  5109. self::removeAllBossFromStudent($studentId);
  5110. }
  5111. return $inserted;
  5112. }
  5113. /**
  5114. * Get users followed by student boss.
  5115. *
  5116. * @param int $userId
  5117. * @param int $userStatus (STUDENT, COURSEMANAGER, etc)
  5118. * @param bool $getOnlyUserId
  5119. * @param bool $getSql
  5120. * @param bool $getCount
  5121. * @param int $from
  5122. * @param int $numberItems
  5123. * @param int $column
  5124. * @param string $direction
  5125. * @param int $active
  5126. * @param string $lastConnectionDate
  5127. *
  5128. * @return array users
  5129. */
  5130. public static function getUsersFollowedByStudentBoss(
  5131. $userId,
  5132. $userStatus = 0,
  5133. $getOnlyUserId = false,
  5134. $getSql = false,
  5135. $getCount = false,
  5136. $from = null,
  5137. $numberItems = null,
  5138. $column = null,
  5139. $direction = null,
  5140. $active = null,
  5141. $lastConnectionDate = null
  5142. ) {
  5143. return self::getUsersFollowedByUser(
  5144. $userId,
  5145. $userStatus,
  5146. $getOnlyUserId,
  5147. $getSql,
  5148. $getCount,
  5149. $from,
  5150. $numberItems,
  5151. $column,
  5152. $direction,
  5153. $active,
  5154. $lastConnectionDate,
  5155. STUDENT_BOSS
  5156. );
  5157. }
  5158. /**
  5159. * Get the teacher (users with COURSEMANGER status) list.
  5160. *
  5161. * @return array The list
  5162. */
  5163. public static function getTeachersList()
  5164. {
  5165. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  5166. $resultData = Database::select(
  5167. 'user_id, lastname, firstname, username',
  5168. $userTable,
  5169. [
  5170. 'where' => [
  5171. 'status = ?' => COURSEMANAGER,
  5172. ],
  5173. ]
  5174. );
  5175. foreach ($resultData as &$teacherData) {
  5176. $teacherData['completeName'] = api_get_person_name(
  5177. $teacherData['firstname'],
  5178. $teacherData['lastname']
  5179. );
  5180. }
  5181. return $resultData;
  5182. }
  5183. /**
  5184. * @return array
  5185. */
  5186. public static function getOfficialCodeGrouped()
  5187. {
  5188. $user = Database::get_main_table(TABLE_MAIN_USER);
  5189. $sql = "SELECT DISTINCT official_code
  5190. FROM $user
  5191. GROUP BY official_code";
  5192. $result = Database::query($sql);
  5193. $values = Database::store_result($result, 'ASSOC');
  5194. $result = [];
  5195. foreach ($values as $value) {
  5196. $result[$value['official_code']] = $value['official_code'];
  5197. }
  5198. return $result;
  5199. }
  5200. /**
  5201. * @param string $officialCode
  5202. *
  5203. * @return array
  5204. */
  5205. public static function getUsersByOfficialCode($officialCode)
  5206. {
  5207. $user = Database::get_main_table(TABLE_MAIN_USER);
  5208. $officialCode = Database::escape_string($officialCode);
  5209. $sql = "SELECT DISTINCT id
  5210. FROM $user
  5211. WHERE official_code = '$officialCode'
  5212. ";
  5213. $result = Database::query($sql);
  5214. $users = [];
  5215. while ($row = Database::fetch_array($result)) {
  5216. $users[] = $row['id'];
  5217. }
  5218. return $users;
  5219. }
  5220. /**
  5221. * Calc the expended time (in seconds) by a user in a course.
  5222. *
  5223. * @param int $userId The user id
  5224. * @param int $courseId The course id
  5225. * @param int $sessionId Optional. The session id
  5226. * @param string $from Optional. From date
  5227. * @param string $until Optional. Until date
  5228. *
  5229. * @return int The time
  5230. */
  5231. public static function getTimeSpentInCourses(
  5232. $userId,
  5233. $courseId,
  5234. $sessionId = 0,
  5235. $from = '',
  5236. $until = ''
  5237. ) {
  5238. $userId = (int) $userId;
  5239. $sessionId = (int) $sessionId;
  5240. $trackCourseAccessTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  5241. $whereConditions = [
  5242. 'user_id = ? ' => $userId,
  5243. 'AND c_id = ? ' => $courseId,
  5244. 'AND session_id = ? ' => $sessionId,
  5245. ];
  5246. if (!empty($from) && !empty($until)) {
  5247. $whereConditions["AND (login_course_date >= '?' "] = $from;
  5248. $whereConditions["AND logout_course_date <= DATE_ADD('?', INTERVAL 1 DAY)) "] = $until;
  5249. }
  5250. $trackResult = Database::select(
  5251. 'SUM(UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) as total_time',
  5252. $trackCourseAccessTable,
  5253. [
  5254. 'where' => $whereConditions,
  5255. ],
  5256. 'first'
  5257. );
  5258. if ($trackResult != false) {
  5259. return $trackResult['total_time'] ? $trackResult['total_time'] : 0;
  5260. }
  5261. return 0;
  5262. }
  5263. /**
  5264. * Get the boss user ID from a followed user id.
  5265. *
  5266. * @param $userId
  5267. *
  5268. * @return bool
  5269. */
  5270. public static function getFirstStudentBoss($userId)
  5271. {
  5272. $userId = (int) $userId;
  5273. if ($userId > 0) {
  5274. $userRelTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  5275. $row = Database::select(
  5276. 'DISTINCT friend_user_id AS boss_id',
  5277. $userRelTable,
  5278. [
  5279. 'where' => [
  5280. 'user_id = ? AND relation_type = ? LIMIT 1' => [
  5281. $userId,
  5282. USER_RELATION_TYPE_BOSS,
  5283. ],
  5284. ],
  5285. ]
  5286. );
  5287. if (!empty($row)) {
  5288. return $row[0]['boss_id'];
  5289. }
  5290. }
  5291. return false;
  5292. }
  5293. /**
  5294. * Get the boss user ID from a followed user id.
  5295. *
  5296. * @param int $userId student id
  5297. *
  5298. * @return array
  5299. */
  5300. public static function getStudentBossList($userId)
  5301. {
  5302. $userId = (int) $userId;
  5303. if ($userId > 0) {
  5304. $userRelTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  5305. $result = Database::select(
  5306. 'DISTINCT friend_user_id AS boss_id',
  5307. $userRelTable,
  5308. [
  5309. 'where' => [
  5310. 'user_id = ? AND relation_type = ? ' => [
  5311. $userId,
  5312. USER_RELATION_TYPE_BOSS,
  5313. ],
  5314. ],
  5315. ]
  5316. );
  5317. return $result;
  5318. }
  5319. return [];
  5320. }
  5321. /**
  5322. * @param int $bossId
  5323. * @param int $studentId
  5324. *
  5325. * @return bool
  5326. */
  5327. public static function userIsBossOfStudent($bossId, $studentId)
  5328. {
  5329. $result = false;
  5330. $bossList = self::getStudentBossList($studentId);
  5331. if (!empty($bossList)) {
  5332. $bossList = array_column($bossList, 'boss_id');
  5333. if (in_array($bossId, $bossList)) {
  5334. $result = true;
  5335. }
  5336. }
  5337. return $result;
  5338. }
  5339. /**
  5340. * Displays the name of the user and makes the link to the user profile.
  5341. *
  5342. * @param array $userInfo
  5343. *
  5344. * @return string
  5345. */
  5346. public static function getUserProfileLink($userInfo)
  5347. {
  5348. if (isset($userInfo) && isset($userInfo['user_id'])) {
  5349. return Display::url(
  5350. $userInfo['complete_name_with_username'],
  5351. $userInfo['profile_url']
  5352. );
  5353. }
  5354. return get_lang('Anonymous');
  5355. }
  5356. /**
  5357. * Displays the name of the user and makes the link to the user profile.
  5358. *
  5359. * @param $userInfo
  5360. *
  5361. * @return string
  5362. */
  5363. public static function getUserProfileLinkWithPicture($userInfo)
  5364. {
  5365. return Display::url(
  5366. Display::img($userInfo['avatar']),
  5367. $userInfo['profile_url']
  5368. );
  5369. }
  5370. /**
  5371. * Get users whose name matches $firstname and $lastname.
  5372. *
  5373. * @param string $firstname Firstname to search
  5374. * @param string $lastname Lastname to search
  5375. *
  5376. * @return array The user list
  5377. */
  5378. public static function getUsersByName($firstname, $lastname)
  5379. {
  5380. $firstname = Database::escape_string($firstname);
  5381. $lastname = Database::escape_string($lastname);
  5382. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  5383. $sql = <<<SQL
  5384. SELECT id, username, lastname, firstname
  5385. FROM $userTable
  5386. WHERE
  5387. firstname LIKE '$firstname%' AND
  5388. lastname LIKE '$lastname%'
  5389. SQL;
  5390. $result = Database::query($sql);
  5391. $users = [];
  5392. while ($resultData = Database::fetch_object($result)) {
  5393. $users[] = $resultData;
  5394. }
  5395. return $users;
  5396. }
  5397. /**
  5398. * @param int $optionSelected
  5399. *
  5400. * @return string
  5401. */
  5402. public static function getUserSubscriptionTab($optionSelected = 1)
  5403. {
  5404. $allowAdmin = api_get_setting('allow_user_course_subscription_by_course_admin');
  5405. if (($allowAdmin === 'true' && api_is_allowed_to_edit()) ||
  5406. api_is_platform_admin()
  5407. ) {
  5408. $userPath = api_get_path(WEB_CODE_PATH).'user/';
  5409. $headers = [
  5410. [
  5411. 'url' => $userPath.'user.php?'.api_get_cidreq().'&type='.STUDENT,
  5412. 'content' => get_lang('Students'),
  5413. ],
  5414. [
  5415. 'url' => $userPath.'user.php?'.api_get_cidreq().'&type='.COURSEMANAGER,
  5416. 'content' => get_lang('Teachers'),
  5417. ],
  5418. /*[
  5419. 'url' => $userPath.'subscribe_user.php?'.api_get_cidreq(),
  5420. 'content' => get_lang('Students'),
  5421. ],
  5422. [
  5423. 'url' => $userPath.'subscribe_user.php?type=teacher&'.api_get_cidreq(),
  5424. 'content' => get_lang('Teachers'),
  5425. ],*/
  5426. [
  5427. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  5428. 'content' => get_lang('Groups'),
  5429. ],
  5430. [
  5431. 'url' => $userPath.'class.php?'.api_get_cidreq(),
  5432. 'content' => get_lang('Classes'),
  5433. ],
  5434. ];
  5435. return Display::tabsOnlyLink($headers, $optionSelected);
  5436. }
  5437. return '';
  5438. }
  5439. /**
  5440. * Make sure this function is protected because it does NOT check password!
  5441. *
  5442. * This function defines globals.
  5443. *
  5444. * @param int $userId
  5445. * @param bool $checkIfUserCanLoginAs
  5446. *
  5447. * @return bool
  5448. *
  5449. * @author Evie Embrechts
  5450. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  5451. */
  5452. public static function loginAsUser($userId, $checkIfUserCanLoginAs = true)
  5453. {
  5454. $userId = (int) $userId;
  5455. $userInfo = api_get_user_info($userId);
  5456. // Check if the user is allowed to 'login_as'
  5457. $canLoginAs = true;
  5458. if ($checkIfUserCanLoginAs) {
  5459. $canLoginAs = api_can_login_as($userId);
  5460. }
  5461. if (!$canLoginAs || empty($userInfo)) {
  5462. return false;
  5463. }
  5464. if ($userId) {
  5465. $logInfo = [
  5466. 'tool' => 'logout',
  5467. 'tool_id' => 0,
  5468. 'tool_id_detail' => 0,
  5469. 'action' => '',
  5470. 'info' => 'Change user (login as)',
  5471. ];
  5472. Event::registerLog($logInfo);
  5473. // Logout the current user
  5474. self::loginDelete(api_get_user_id());
  5475. Session::erase('_user');
  5476. Session::erase('is_platformAdmin');
  5477. Session::erase('is_allowedCreateCourse');
  5478. Session::erase('_uid');
  5479. // Cleaning session variables
  5480. $_user['firstName'] = $userInfo['firstname'];
  5481. $_user['lastName'] = $userInfo['lastname'];
  5482. $_user['mail'] = $userInfo['email'];
  5483. $_user['official_code'] = $userInfo['official_code'];
  5484. $_user['picture_uri'] = $userInfo['picture_uri'];
  5485. $_user['user_id'] = $userId;
  5486. $_user['id'] = $userId;
  5487. $_user['status'] = $userInfo['status'];
  5488. // Filling session variables with new data
  5489. Session::write('_uid', $userId);
  5490. Session::write('_user', $userInfo);
  5491. Session::write('is_platformAdmin', (bool) self::is_admin($userId));
  5492. Session::write('is_allowedCreateCourse', $userInfo['status'] == 1);
  5493. // will be useful later to know if the user is actually an admin or not (example reporting)
  5494. Session::write('login_as', true);
  5495. $logInfo = [
  5496. 'tool' => 'login',
  5497. 'tool_id' => 0,
  5498. 'tool_id_detail' => 0,
  5499. 'info' => $userId,
  5500. ];
  5501. Event::registerLog($logInfo);
  5502. return true;
  5503. }
  5504. return false;
  5505. }
  5506. /**
  5507. * Remove all login records from the track_e_online stats table,
  5508. * for the given user ID.
  5509. *
  5510. * @param int $userId User ID
  5511. */
  5512. public static function loginDelete($userId)
  5513. {
  5514. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  5515. $userId = (int) $userId;
  5516. $query = "DELETE FROM $online_table WHERE login_user_id = $userId";
  5517. Database::query($query);
  5518. }
  5519. /**
  5520. * Login as first admin user registered in the platform.
  5521. *
  5522. * @return array
  5523. */
  5524. public static function logInAsFirstAdmin()
  5525. {
  5526. $adminList = self::get_all_administrators();
  5527. if (!empty($adminList)) {
  5528. $userInfo = current($adminList);
  5529. if (!empty($userInfo)) {
  5530. $result = self::loginAsUser($userInfo['user_id'], false);
  5531. if ($result && api_is_platform_admin()) {
  5532. return api_get_user_info();
  5533. }
  5534. }
  5535. }
  5536. return [];
  5537. }
  5538. /**
  5539. * Check if user is teacher of a student based in their courses.
  5540. *
  5541. * @param $teacherId
  5542. * @param $studentId
  5543. *
  5544. * @return array
  5545. */
  5546. public static function getCommonCoursesBetweenTeacherAndStudent($teacherId, $studentId)
  5547. {
  5548. $courses = CourseManager::getCoursesFollowedByUser(
  5549. $teacherId,
  5550. COURSEMANAGER
  5551. );
  5552. if (empty($courses)) {
  5553. return false;
  5554. }
  5555. $coursesFromUser = CourseManager::get_courses_list_by_user_id($studentId);
  5556. if (empty($coursesFromUser)) {
  5557. return false;
  5558. }
  5559. $coursesCodeList = array_column($courses, 'code');
  5560. $coursesCodeFromUserList = array_column($coursesFromUser, 'code');
  5561. $commonCourses = array_intersect($coursesCodeList, $coursesCodeFromUserList);
  5562. $commonCourses = array_filter($commonCourses);
  5563. if (!empty($commonCourses)) {
  5564. return $commonCourses;
  5565. }
  5566. return [];
  5567. }
  5568. /**
  5569. * @param int $teacherId
  5570. * @param int $studentId
  5571. *
  5572. * @return bool
  5573. */
  5574. public static function isTeacherOfStudent($teacherId, $studentId)
  5575. {
  5576. $courses = self::getCommonCoursesBetweenTeacherAndStudent(
  5577. $teacherId,
  5578. $studentId
  5579. );
  5580. if (!empty($courses)) {
  5581. return true;
  5582. }
  5583. return false;
  5584. }
  5585. /**
  5586. * Send user confirmation mail.
  5587. *
  5588. * @param User $user
  5589. *
  5590. * @throws Exception
  5591. */
  5592. public static function sendUserConfirmationMail(User $user)
  5593. {
  5594. $uniqueId = api_get_unique_id();
  5595. $user->setConfirmationToken($uniqueId);
  5596. Database::getManager()->persist($user);
  5597. Database::getManager()->flush();
  5598. // ofaj
  5599. $url = api_get_path(WEB_CODE_PATH).'auth/user_mail_confirmation.php?token='.$uniqueId;
  5600. $mailSubject = get_lang('RegistrationConfirmation');
  5601. // Check if the user was originally set for an automated subscription to a course or session
  5602. $mailBody = sprintf(
  5603. get_lang('ToCompleteYourPlatformRegistrationYouNeedToConfirmYourAccountByClickingTheFollowingLinkX'),
  5604. $url
  5605. );
  5606. api_mail_html(
  5607. $user->getCompleteName(),
  5608. $user->getEmail(),
  5609. $mailSubject,
  5610. $mailBody
  5611. );
  5612. Display::addFlash(Display::return_message(get_lang('CheckYourEmailAndFollowInstructions')));
  5613. }
  5614. /**
  5615. * Anonymize a user. Replace personal info by anonymous info.
  5616. *
  5617. * @param int $userId User id
  5618. * @param bool $deleteIP Whether to replace the IP address in logs tables by 127.0.0.1 or to leave as is
  5619. *
  5620. * @throws \Exception
  5621. *
  5622. * @return bool
  5623. * @assert (0) === false
  5624. */
  5625. public static function anonymize($userId, $deleteIP = true)
  5626. {
  5627. global $debug;
  5628. $userId = (int) $userId;
  5629. if (empty($userId)) {
  5630. return false;
  5631. }
  5632. $em = Database::getManager();
  5633. $user = api_get_user_entity($userId);
  5634. $uniqueId = uniqid('anon', true);
  5635. $user
  5636. ->setFirstname($uniqueId)
  5637. ->setLastname($uniqueId)
  5638. ->setBiography('')
  5639. ->setAddress('')
  5640. ->setCurriculumItems(null)
  5641. ->setDateOfBirth(null)
  5642. ->setCompetences('')
  5643. ->setDiplomas('')
  5644. ->setOpenarea('')
  5645. ->setTeach('')
  5646. ->setProductions(null)
  5647. ->setOpenid('')
  5648. ->setEmailCanonical($uniqueId.'@example.com')
  5649. ->setEmail($uniqueId.'@example.com')
  5650. ->setUsername($uniqueId)
  5651. ->setUsernameCanonical($uniqueId)
  5652. ->setPhone('')
  5653. ->setOfficialCode('')
  5654. ;
  5655. self::deleteUserPicture($userId);
  5656. self::cleanUserRequestsOfRemoval($userId);
  5657. // The IP address is a border-case personal data, as it does
  5658. // not directly allow for personal identification (it is not
  5659. // a completely safe value in most countries - the IP could
  5660. // be used by neighbours and crackers)
  5661. if ($deleteIP) {
  5662. $substitute = '127.0.0.1';
  5663. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  5664. $sql = "UPDATE $table set user_ip = '$substitute' WHERE access_user_id = $userId";
  5665. $res = Database::query($sql);
  5666. if ($res === false && $debug > 0) {
  5667. error_log("Could not anonymize IP address for user $userId ($sql)");
  5668. }
  5669. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  5670. $sql = "UPDATE $table set user_ip = '$substitute' WHERE user_id = $userId";
  5671. $res = Database::query($sql);
  5672. if ($res === false && $debug > 0) {
  5673. error_log("Could not anonymize IP address for user $userId ($sql)");
  5674. }
  5675. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  5676. $sql = "UPDATE $table set user_ip = '$substitute' WHERE exe_user_id = $userId";
  5677. $res = Database::query($sql);
  5678. if ($res === false && $debug > 0) {
  5679. error_log("Could not anonymize IP address for user $userId ($sql)");
  5680. }
  5681. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  5682. $sql = "UPDATE $table set user_ip = '$substitute' WHERE login_user_id = $userId";
  5683. $res = Database::query($sql);
  5684. if ($res === false && $debug > 0) {
  5685. error_log("Could not anonymize IP address for user $userId ($sql)");
  5686. }
  5687. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  5688. $sql = "UPDATE $table set user_ip = '$substitute' WHERE login_user_id = $userId";
  5689. $res = Database::query($sql);
  5690. if ($res === false && $debug > 0) {
  5691. error_log("Could not anonymize IP address for user $userId ($sql)");
  5692. }
  5693. $table = Database::get_course_table(TABLE_WIKI);
  5694. $sql = "UPDATE $table set user_ip = '$substitute' WHERE user_id = $userId";
  5695. $res = Database::query($sql);
  5696. if ($res === false && $debug > 0) {
  5697. error_log("Could not anonymize IP address for user $userId ($sql)");
  5698. }
  5699. $table = Database::get_main_table(TABLE_TICKET_MESSAGE);
  5700. $sql = "UPDATE $table set ip_address = '$substitute' WHERE sys_insert_user_id = $userId";
  5701. $res = Database::query($sql);
  5702. if ($res === false && $debug > 0) {
  5703. error_log("Could not anonymize IP address for user $userId ($sql)");
  5704. }
  5705. $table = Database::get_course_table(TABLE_WIKI);
  5706. $sql = "UPDATE $table set user_ip = '$substitute' WHERE user_id = $userId";
  5707. $res = Database::query($sql);
  5708. if ($res === false && $debug > 0) {
  5709. error_log("Could not anonymize IP address for user $userId ($sql)");
  5710. }
  5711. }
  5712. $em->persist($user);
  5713. $em->flush($user);
  5714. Event::addEvent(LOG_USER_ANONYMIZE, LOG_USER_ID, $userId);
  5715. return true;
  5716. }
  5717. /**
  5718. * @param int $userId
  5719. *
  5720. * @throws Exception
  5721. *
  5722. * @return string
  5723. */
  5724. public static function anonymizeUserWithVerification($userId)
  5725. {
  5726. $allowDelete = api_get_configuration_value('allow_delete_user_for_session_admin');
  5727. $message = '';
  5728. if (api_is_platform_admin() ||
  5729. ($allowDelete && api_is_session_admin())
  5730. ) {
  5731. $userToUpdateInfo = api_get_user_info($userId);
  5732. $currentUserId = api_get_user_id();
  5733. if ($userToUpdateInfo &&
  5734. api_global_admin_can_edit_admin($userId, null, $allowDelete)
  5735. ) {
  5736. if ($userId != $currentUserId &&
  5737. self::anonymize($userId)
  5738. ) {
  5739. $message = Display::return_message(
  5740. sprintf(get_lang('UserXAnonymized'), $userToUpdateInfo['complete_name_with_username']),
  5741. 'confirmation'
  5742. );
  5743. } else {
  5744. $message = Display::return_message(
  5745. sprintf(get_lang('CannotAnonymizeUserX'), $userToUpdateInfo['complete_name_with_username']),
  5746. 'error'
  5747. );
  5748. }
  5749. } else {
  5750. $message = Display::return_message(
  5751. sprintf(get_lang('NoPermissionToAnonymizeUserX'), $userToUpdateInfo['complete_name_with_username']),
  5752. 'error'
  5753. );
  5754. }
  5755. }
  5756. return $message;
  5757. }
  5758. /**
  5759. * @param int $userId
  5760. *
  5761. * @throws Exception
  5762. *
  5763. * @return string
  5764. */
  5765. public static function deleteUserWithVerification($userId)
  5766. {
  5767. $allowDelete = api_get_configuration_value('allow_delete_user_for_session_admin');
  5768. $message = Display::return_message(get_lang('CannotDeleteUser'), 'error');
  5769. $userToUpdateInfo = api_get_user_info($userId);
  5770. // User must exist.
  5771. if (empty($userToUpdateInfo)) {
  5772. return $message;
  5773. }
  5774. $currentUserId = api_get_user_id();
  5775. // Cannot delete myself.
  5776. if ($userId == $currentUserId) {
  5777. return $message;
  5778. }
  5779. if (api_is_platform_admin() ||
  5780. ($allowDelete && api_is_session_admin())
  5781. ) {
  5782. if (api_global_admin_can_edit_admin($userId, null, $allowDelete)) {
  5783. if (self::delete_user($userId)) {
  5784. $message = Display::return_message(
  5785. get_lang('UserDeleted').': '.$userToUpdateInfo['complete_name_with_username'],
  5786. 'confirmation'
  5787. );
  5788. } else {
  5789. $message = Display::return_message(get_lang('CannotDeleteUserBecauseOwnsCourse'), 'error');
  5790. }
  5791. }
  5792. }
  5793. return $message;
  5794. }
  5795. /**
  5796. * @return array
  5797. */
  5798. public static function createDataPrivacyExtraFields()
  5799. {
  5800. self::create_extra_field(
  5801. 'request_for_legal_agreement_consent_removal_justification',
  5802. 1, //text
  5803. 'Request for legal agreement consent removal justification ',
  5804. ''
  5805. );
  5806. self::create_extra_field(
  5807. 'request_for_delete_account_justification',
  5808. 1, //text
  5809. 'Request for delete account justification',
  5810. ''
  5811. );
  5812. $extraFieldId = self::create_extra_field(
  5813. 'request_for_legal_agreement_consent_removal',
  5814. 1, //text
  5815. 'Request for legal agreement consent removal',
  5816. ''
  5817. );
  5818. $extraFieldIdDeleteAccount = self::create_extra_field(
  5819. 'request_for_delete_account',
  5820. 1, //text
  5821. 'Request for delete user account',
  5822. ''
  5823. );
  5824. return [
  5825. 'delete_account_extra_field' => $extraFieldIdDeleteAccount,
  5826. 'delete_legal' => $extraFieldId,
  5827. ];
  5828. }
  5829. /**
  5830. * @param int $userId
  5831. */
  5832. public static function cleanUserRequestsOfRemoval($userId)
  5833. {
  5834. $userId = (int) $userId;
  5835. $extraFieldValue = new ExtraFieldValue('user');
  5836. $extraFieldsToDelete = [
  5837. 'legal_accept',
  5838. 'request_for_legal_agreement_consent_removal',
  5839. 'request_for_legal_agreement_consent_removal_justification',
  5840. 'request_for_delete_account_justification', // just in case delete also this
  5841. 'request_for_delete_account',
  5842. ];
  5843. foreach ($extraFieldsToDelete as $variable) {
  5844. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  5845. $userId,
  5846. $variable
  5847. );
  5848. if ($value && isset($value['id'])) {
  5849. $extraFieldValue->delete($value['id']);
  5850. }
  5851. }
  5852. }
  5853. /**
  5854. * @return int
  5855. */
  5856. public static function getCountActiveUsers()
  5857. {
  5858. $table = Database::get_main_table(TABLE_MAIN_USER);
  5859. $sql = "SELECT count(id) count FROM $table WHERE active = 1 AND status <> ".ANONYMOUS;
  5860. $result = Database::query($sql);
  5861. $row = Database::fetch_array($result);
  5862. return (int) $row['count'];
  5863. }
  5864. /**
  5865. * @param array $userInfo
  5866. * @param int $searchYear
  5867. *
  5868. * @throws Exception
  5869. *
  5870. * @return array
  5871. */
  5872. public static function getSubscribedSessionsByYear(array $userInfo, $searchYear)
  5873. {
  5874. $timezone = new DateTimeZone(api_get_timezone());
  5875. $sessions = [];
  5876. if (DRH == $userInfo['status']) {
  5877. $sessions = SessionManager::get_sessions_followed_by_drh($userInfo['id']);
  5878. } elseif (api_is_platform_admin(true)) {
  5879. $sessions = SessionManager::getSessionsForAdmin($userInfo['id']);
  5880. } else {
  5881. $sessionsByCategory = self::get_sessions_by_category($userInfo['id'], false, true, true);
  5882. $sessionsByCategory = array_column($sessionsByCategory, 'sessions');
  5883. foreach ($sessionsByCategory as $sessionsInCategory) {
  5884. $sessions = array_merge($sessions, $sessionsInCategory);
  5885. }
  5886. }
  5887. $sessions = array_map(
  5888. function ($sessionInfo) {
  5889. if (!isset($sessionInfo['session_id'])) {
  5890. $sessionInfo['session_id'] = $sessionInfo['id'];
  5891. }
  5892. if (!isset($sessionInfo['session_name'])) {
  5893. $sessionInfo['session_name'] = $sessionInfo['name'];
  5894. }
  5895. return $sessionInfo;
  5896. },
  5897. $sessions
  5898. );
  5899. $calendarSessions = [];
  5900. foreach ($sessions as $sessionInfo) {
  5901. if (!empty($sessionInfo['duration'])) {
  5902. $courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser(
  5903. $sessionInfo['session_id'],
  5904. $userInfo['id']
  5905. );
  5906. if (empty($courseAccess)) {
  5907. continue;
  5908. }
  5909. $firstAcessDate = new DateTime(api_get_local_time($courseAccess['login_course_date']), $timezone);
  5910. $lastAccessDate = clone $firstAcessDate;
  5911. $lastAccessDate->modify("+{$sessionInfo['duration']} days");
  5912. $firstAccessYear = (int) $firstAcessDate->format('Y');
  5913. $lastAccessYear = (int) $lastAccessDate->format('Y');
  5914. if ($firstAccessYear <= $searchYear && $lastAccessYear >= $searchYear) {
  5915. $calendarSessions[$sessionInfo['session_id']] = [
  5916. 'name' => $sessionInfo['session_name'],
  5917. 'access_start_date' => $firstAcessDate->format('Y-m-d h:i:s'),
  5918. 'access_end_date' => $lastAccessDate->format('Y-m-d h:i:s'),
  5919. ];
  5920. }
  5921. continue;
  5922. }
  5923. $accessStartDate = !empty($sessionInfo['access_start_date'])
  5924. ? new DateTime(api_get_local_time($sessionInfo['access_start_date']), $timezone)
  5925. : null;
  5926. $accessEndDate = !empty($sessionInfo['access_end_date'])
  5927. ? new DateTime(api_get_local_time($sessionInfo['access_end_date']), $timezone)
  5928. : null;
  5929. $accessStartYear = $accessStartDate ? (int) $accessStartDate->format('Y') : 0;
  5930. $accessEndYear = $accessEndDate ? (int) $accessEndDate->format('Y') : 0;
  5931. $isValid = false;
  5932. if ($accessStartYear && $accessEndYear) {
  5933. if ($accessStartYear <= $searchYear && $accessEndYear >= $searchYear) {
  5934. $isValid = true;
  5935. }
  5936. }
  5937. if ($accessStartYear && !$accessEndYear) {
  5938. if ($accessStartYear == $searchYear) {
  5939. $isValid = true;
  5940. }
  5941. }
  5942. if (!$accessStartYear && $accessEndYear) {
  5943. if ($accessEndYear == $searchYear) {
  5944. $isValid = true;
  5945. }
  5946. }
  5947. if ($isValid) {
  5948. $calendarSessions[$sessionInfo['session_id']] = [
  5949. 'name' => $sessionInfo['session_name'],
  5950. 'access_start_date' => $accessStartDate ? $accessStartDate->format('Y-m-d h:i:s') : null,
  5951. 'access_end_date' => $accessEndDate ? $accessEndDate->format('Y-m-d h:i:s') : null,
  5952. ];
  5953. }
  5954. }
  5955. return $calendarSessions;
  5956. }
  5957. /**
  5958. * Get sessions info for planification calendar.
  5959. *
  5960. * @param array $sessionsList Session list from UserManager::getSubscribedSessionsByYear
  5961. * @param int $searchYear
  5962. *
  5963. * @throws Exception
  5964. *
  5965. * @return array
  5966. */
  5967. public static function getSessionsCalendarByYear(array $sessionsList, $searchYear)
  5968. {
  5969. $timezone = new DateTimeZone(api_get_timezone());
  5970. $calendar = [];
  5971. foreach ($sessionsList as $sessionId => $sessionInfo) {
  5972. $startDate = $sessionInfo['access_start_date']
  5973. ? new DateTime(api_get_local_time($sessionInfo['access_start_date']), $timezone)
  5974. : null;
  5975. $endDate = $sessionInfo['access_end_date']
  5976. ? new DateTime(api_get_local_time($sessionInfo['access_end_date']), $timezone)
  5977. : null;
  5978. $startYear = $startDate ? (int) $startDate->format('Y') : 0;
  5979. $startWeekYear = $startDate ? (int) $startDate->format('o') : 0;
  5980. $startWeek = $startDate ? (int) $startDate->format('W') : 0;
  5981. $endYear = $endDate ? (int) $endDate->format('Y') : 0;
  5982. $endWeekYear = $endDate ? (int) $endDate->format('o') : 0;
  5983. $endWeek = $endDate ? (int) $endDate->format('W') : 0;
  5984. $start = $startWeekYear < $searchYear ? 0 : $startWeek - 1;
  5985. $duration = $endWeekYear > $searchYear ? 52 - $start : $endWeek - $start;
  5986. $calendar[] = [
  5987. 'id' => $sessionId,
  5988. 'name' => $sessionInfo['name'],
  5989. 'human_date' => SessionManager::convertSessionDateToString($startDate, $endDate, false, true),
  5990. 'start_in_last_year' => $startYear < $searchYear,
  5991. 'end_in_next_year' => $endYear > $searchYear,
  5992. 'no_start' => !$startWeek,
  5993. 'no_end' => !$endWeek,
  5994. 'start' => $start,
  5995. 'duration' => $duration > 0 ? $duration : 1,
  5996. ];
  5997. }
  5998. usort(
  5999. $calendar,
  6000. function ($sA, $sB) {
  6001. if ($sA['start'] == $sB['start']) {
  6002. return 0;
  6003. }
  6004. if ($sA['start'] < $sB['start']) {
  6005. return -1;
  6006. }
  6007. return 1;
  6008. }
  6009. );
  6010. return $calendar;
  6011. }
  6012. /**
  6013. * Return the user's full name. Optionally with the username.
  6014. *
  6015. * @param User $user
  6016. * @param bool $includeUsername Optional. By default username is not included.
  6017. *
  6018. * @return string
  6019. */
  6020. public static function formatUserFullName(User $user, $includeUsername = false)
  6021. {
  6022. $fullName = api_get_person_name($user->getFirstname(), $user->getLastname());
  6023. if ($includeUsername && api_get_configuration_value('hide_username_with_complete_name') !== true) {
  6024. $username = $user->getUsername();
  6025. return "$fullName ($username)";
  6026. }
  6027. return $fullName;
  6028. }
  6029. /**
  6030. * @return EncoderFactory
  6031. */
  6032. private static function getEncoderFactory()
  6033. {
  6034. $encryption = self::getPasswordEncryption();
  6035. $encoders = [
  6036. 'Chamilo\\UserBundle\\Entity\\User' => new \Chamilo\UserBundle\Security\Encoder($encryption),
  6037. ];
  6038. $encoderFactory = new EncoderFactory($encoders);
  6039. return $encoderFactory;
  6040. }
  6041. /**
  6042. * @param User $user
  6043. *
  6044. * @return \Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface
  6045. */
  6046. private static function getEncoder(User $user)
  6047. {
  6048. $encoderFactory = self::getEncoderFactory();
  6049. return $encoderFactory->getEncoder($user);
  6050. }
  6051. /**
  6052. * Disables or enables a user.
  6053. *
  6054. * @param int $user_id
  6055. * @param int $active Enable or disable
  6056. *
  6057. * @return bool True on success, false on failure
  6058. * @assert (-1,0) === false
  6059. * @assert (1,1) === true
  6060. */
  6061. private static function change_active_state($user_id, $active)
  6062. {
  6063. $user_id = (int) $user_id;
  6064. $active = (int) $active;
  6065. if (empty($user_id)) {
  6066. return false;
  6067. }
  6068. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  6069. $sql = "UPDATE $table_user SET active = '$active' WHERE id = $user_id";
  6070. $r = Database::query($sql);
  6071. $ev = LOG_USER_DISABLE;
  6072. if ($active == 1) {
  6073. $ev = LOG_USER_ENABLE;
  6074. }
  6075. if ($r !== false) {
  6076. Event::addEvent($ev, LOG_USER_ID, $user_id);
  6077. }
  6078. return $r;
  6079. }
  6080. /**
  6081. * Get either a Gravatar URL or complete image tag for a specified email address.
  6082. *
  6083. * @param string $email The email address
  6084. * @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ]
  6085. * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
  6086. * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
  6087. * @param bool $img True to return a complete IMG tag False for just the URL
  6088. * @param array $atts Optional, additional key/value attributes to include in the IMG tag
  6089. *
  6090. * @return string containing either just a URL or a complete image tag
  6091. * @source http://gravatar.com/site/implement/images/php/
  6092. */
  6093. private static function getGravatar(
  6094. $email,
  6095. $s = 80,
  6096. $d = 'mm',
  6097. $r = 'g',
  6098. $img = false,
  6099. $atts = []
  6100. ) {
  6101. $url = 'http://www.gravatar.com/avatar/';
  6102. if (!empty($_SERVER['HTTPS'])) {
  6103. $url = 'https://secure.gravatar.com/avatar/';
  6104. }
  6105. $url .= md5(strtolower(trim($email)));
  6106. $url .= "?s=$s&d=$d&r=$r";
  6107. if ($img) {
  6108. $url = '<img src="'.$url.'"';
  6109. foreach ($atts as $key => $val) {
  6110. $url .= ' '.$key.'="'.$val.'"';
  6111. }
  6112. $url .= ' />';
  6113. }
  6114. return $url;
  6115. }
  6116. }