usermanager.lib.php 240 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826
  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. * Get active users without anon users
  5855. * @return int
  5856. */
  5857. public static function getCountActiveUsers()
  5858. {
  5859. $table = Database::get_main_table(TABLE_MAIN_USER);
  5860. $sql = "SELECT count(id) count FROM $table WHERE active = 1 AND status <> ".ANONYMOUS;
  5861. $result = Database::query($sql);
  5862. $row = Database::fetch_array($result);
  5863. return (int) $row['count'];
  5864. }
  5865. /**
  5866. * @param array $userInfo
  5867. * @param int $searchYear
  5868. *
  5869. * @throws Exception
  5870. *
  5871. * @return array
  5872. */
  5873. public static function getSubscribedSessionsByYear(array $userInfo, $searchYear)
  5874. {
  5875. $timezone = new DateTimeZone(api_get_timezone());
  5876. $sessions = [];
  5877. if (DRH == $userInfo['status']) {
  5878. $sessions = SessionManager::get_sessions_followed_by_drh($userInfo['id']);
  5879. } elseif (api_is_platform_admin(true)) {
  5880. $sessions = SessionManager::getSessionsForAdmin($userInfo['id']);
  5881. } else {
  5882. $sessionsByCategory = self::get_sessions_by_category($userInfo['id'], false, true, true);
  5883. $sessionsByCategory = array_column($sessionsByCategory, 'sessions');
  5884. foreach ($sessionsByCategory as $sessionsInCategory) {
  5885. $sessions = array_merge($sessions, $sessionsInCategory);
  5886. }
  5887. }
  5888. $sessions = array_map(
  5889. function ($sessionInfo) {
  5890. if (!isset($sessionInfo['session_id'])) {
  5891. $sessionInfo['session_id'] = $sessionInfo['id'];
  5892. }
  5893. if (!isset($sessionInfo['session_name'])) {
  5894. $sessionInfo['session_name'] = $sessionInfo['name'];
  5895. }
  5896. return $sessionInfo;
  5897. },
  5898. $sessions
  5899. );
  5900. $calendarSessions = [];
  5901. foreach ($sessions as $sessionInfo) {
  5902. if (!empty($sessionInfo['duration'])) {
  5903. $courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser(
  5904. $sessionInfo['session_id'],
  5905. $userInfo['id']
  5906. );
  5907. if (empty($courseAccess)) {
  5908. continue;
  5909. }
  5910. $firstAcessDate = new DateTime(api_get_local_time($courseAccess['login_course_date']), $timezone);
  5911. $lastAccessDate = clone $firstAcessDate;
  5912. $lastAccessDate->modify("+{$sessionInfo['duration']} days");
  5913. $firstAccessYear = (int) $firstAcessDate->format('Y');
  5914. $lastAccessYear = (int) $lastAccessDate->format('Y');
  5915. if ($firstAccessYear <= $searchYear && $lastAccessYear >= $searchYear) {
  5916. $calendarSessions[$sessionInfo['session_id']] = [
  5917. 'name' => $sessionInfo['session_name'],
  5918. 'access_start_date' => $firstAcessDate->format('Y-m-d h:i:s'),
  5919. 'access_end_date' => $lastAccessDate->format('Y-m-d h:i:s'),
  5920. ];
  5921. }
  5922. continue;
  5923. }
  5924. $accessStartDate = !empty($sessionInfo['access_start_date'])
  5925. ? new DateTime(api_get_local_time($sessionInfo['access_start_date']), $timezone)
  5926. : null;
  5927. $accessEndDate = !empty($sessionInfo['access_end_date'])
  5928. ? new DateTime(api_get_local_time($sessionInfo['access_end_date']), $timezone)
  5929. : null;
  5930. $accessStartYear = $accessStartDate ? (int) $accessStartDate->format('Y') : 0;
  5931. $accessEndYear = $accessEndDate ? (int) $accessEndDate->format('Y') : 0;
  5932. $isValid = false;
  5933. if ($accessStartYear && $accessEndYear) {
  5934. if ($accessStartYear <= $searchYear && $accessEndYear >= $searchYear) {
  5935. $isValid = true;
  5936. }
  5937. }
  5938. if ($accessStartYear && !$accessEndYear) {
  5939. if ($accessStartYear == $searchYear) {
  5940. $isValid = true;
  5941. }
  5942. }
  5943. if (!$accessStartYear && $accessEndYear) {
  5944. if ($accessEndYear == $searchYear) {
  5945. $isValid = true;
  5946. }
  5947. }
  5948. if ($isValid) {
  5949. $calendarSessions[$sessionInfo['session_id']] = [
  5950. 'name' => $sessionInfo['session_name'],
  5951. 'access_start_date' => $accessStartDate ? $accessStartDate->format('Y-m-d h:i:s') : null,
  5952. 'access_end_date' => $accessEndDate ? $accessEndDate->format('Y-m-d h:i:s') : null,
  5953. ];
  5954. }
  5955. }
  5956. return $calendarSessions;
  5957. }
  5958. /**
  5959. * Get sessions info for planification calendar.
  5960. *
  5961. * @param array $sessionsList Session list from UserManager::getSubscribedSessionsByYear
  5962. * @param int $searchYear
  5963. *
  5964. * @throws Exception
  5965. *
  5966. * @return array
  5967. */
  5968. public static function getSessionsCalendarByYear(array $sessionsList, $searchYear)
  5969. {
  5970. $timezone = new DateTimeZone(api_get_timezone());
  5971. $calendar = [];
  5972. foreach ($sessionsList as $sessionId => $sessionInfo) {
  5973. $startDate = $sessionInfo['access_start_date']
  5974. ? new DateTime(api_get_local_time($sessionInfo['access_start_date']), $timezone)
  5975. : null;
  5976. $endDate = $sessionInfo['access_end_date']
  5977. ? new DateTime(api_get_local_time($sessionInfo['access_end_date']), $timezone)
  5978. : null;
  5979. $startYear = $startDate ? (int) $startDate->format('Y') : 0;
  5980. $startWeekYear = $startDate ? (int) $startDate->format('o') : 0;
  5981. $startWeek = $startDate ? (int) $startDate->format('W') : 0;
  5982. $endYear = $endDate ? (int) $endDate->format('Y') : 0;
  5983. $endWeekYear = $endDate ? (int) $endDate->format('o') : 0;
  5984. $endWeek = $endDate ? (int) $endDate->format('W') : 0;
  5985. $start = $startWeekYear < $searchYear ? 0 : $startWeek - 1;
  5986. $duration = $endWeekYear > $searchYear ? 52 - $start : $endWeek - $start;
  5987. $calendar[] = [
  5988. 'id' => $sessionId,
  5989. 'name' => $sessionInfo['name'],
  5990. 'human_date' => SessionManager::convertSessionDateToString($startDate, $endDate, false, true),
  5991. 'start_in_last_year' => $startYear < $searchYear,
  5992. 'end_in_next_year' => $endYear > $searchYear,
  5993. 'no_start' => !$startWeek,
  5994. 'no_end' => !$endWeek,
  5995. 'start' => $start,
  5996. 'duration' => $duration > 0 ? $duration : 1,
  5997. ];
  5998. }
  5999. usort(
  6000. $calendar,
  6001. function ($sA, $sB) {
  6002. if ($sA['start'] == $sB['start']) {
  6003. return 0;
  6004. }
  6005. if ($sA['start'] < $sB['start']) {
  6006. return -1;
  6007. }
  6008. return 1;
  6009. }
  6010. );
  6011. return $calendar;
  6012. }
  6013. /**
  6014. * Return the user's full name. Optionally with the username.
  6015. *
  6016. * @param User $user
  6017. * @param bool $includeUsername Optional. By default username is not included.
  6018. *
  6019. * @return string
  6020. */
  6021. public static function formatUserFullName(User $user, $includeUsername = false)
  6022. {
  6023. $fullName = api_get_person_name($user->getFirstname(), $user->getLastname());
  6024. if ($includeUsername && api_get_configuration_value('hide_username_with_complete_name') !== true) {
  6025. $username = $user->getUsername();
  6026. return "$fullName ($username)";
  6027. }
  6028. return $fullName;
  6029. }
  6030. /**
  6031. * @return EncoderFactory
  6032. */
  6033. private static function getEncoderFactory()
  6034. {
  6035. $encryption = self::getPasswordEncryption();
  6036. $encoders = [
  6037. 'Chamilo\\UserBundle\\Entity\\User' => new \Chamilo\UserBundle\Security\Encoder($encryption),
  6038. ];
  6039. $encoderFactory = new EncoderFactory($encoders);
  6040. return $encoderFactory;
  6041. }
  6042. /**
  6043. * @param User $user
  6044. *
  6045. * @return \Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface
  6046. */
  6047. private static function getEncoder(User $user)
  6048. {
  6049. $encoderFactory = self::getEncoderFactory();
  6050. return $encoderFactory->getEncoder($user);
  6051. }
  6052. /**
  6053. * Disables or enables a user.
  6054. *
  6055. * @param int $user_id
  6056. * @param int $active Enable or disable
  6057. *
  6058. * @return bool True on success, false on failure
  6059. * @assert (-1,0) === false
  6060. * @assert (1,1) === true
  6061. */
  6062. private static function change_active_state($user_id, $active)
  6063. {
  6064. $user_id = (int) $user_id;
  6065. $active = (int) $active;
  6066. if (empty($user_id)) {
  6067. return false;
  6068. }
  6069. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  6070. $sql = "UPDATE $table_user SET active = '$active' WHERE id = $user_id";
  6071. $r = Database::query($sql);
  6072. $ev = LOG_USER_DISABLE;
  6073. if ($active == 1) {
  6074. $ev = LOG_USER_ENABLE;
  6075. }
  6076. if ($r !== false) {
  6077. Event::addEvent($ev, LOG_USER_ID, $user_id);
  6078. }
  6079. return $r;
  6080. }
  6081. /**
  6082. * Get either a Gravatar URL or complete image tag for a specified email address.
  6083. *
  6084. * @param string $email The email address
  6085. * @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ]
  6086. * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
  6087. * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
  6088. * @param bool $img True to return a complete IMG tag False for just the URL
  6089. * @param array $atts Optional, additional key/value attributes to include in the IMG tag
  6090. *
  6091. * @return string containing either just a URL or a complete image tag
  6092. * @source http://gravatar.com/site/implement/images/php/
  6093. */
  6094. private static function getGravatar(
  6095. $email,
  6096. $s = 80,
  6097. $d = 'mm',
  6098. $r = 'g',
  6099. $img = false,
  6100. $atts = []
  6101. ) {
  6102. $url = 'http://www.gravatar.com/avatar/';
  6103. if (!empty($_SERVER['HTTPS'])) {
  6104. $url = 'https://secure.gravatar.com/avatar/';
  6105. }
  6106. $url .= md5(strtolower(trim($email)));
  6107. $url .= "?s=$s&d=$d&r=$r";
  6108. if ($img) {
  6109. $url = '<img src="'.$url.'"';
  6110. foreach ($atts as $key => $val) {
  6111. $url .= ' '.$key.'="'.$val.'"';
  6112. }
  6113. $url .= ' />';
  6114. }
  6115. return $url;
  6116. }
  6117. }