registration.soap.php 240 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. use Chamilo\UserBundle\Entity\User;
  5. /**
  6. * @package chamilo.webservices
  7. */
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. $debug = true;
  10. define('WS_ERROR_SECRET_KEY', 1);
  11. define('WS_ERROR_NOT_FOUND_RESULT', 2);
  12. define('WS_ERROR_INVALID_INPUT', 3);
  13. define('WS_ERROR_SETTING', 4);
  14. define('DEFAULT_ADMIN_USER_ID', 1);
  15. /**
  16. * @param string $code
  17. *
  18. * @return soap_fault|null
  19. */
  20. function returnError($code)
  21. {
  22. $fault = null;
  23. switch ($code) {
  24. case WS_ERROR_SECRET_KEY:
  25. $fault = new soap_fault(
  26. 'Server',
  27. '',
  28. 'Secret key is not correct or params are not correctly set'
  29. );
  30. break;
  31. case WS_ERROR_NOT_FOUND_RESULT:
  32. $fault = new soap_fault(
  33. 'Server',
  34. '',
  35. 'No result was found for this query'
  36. );
  37. break;
  38. case WS_ERROR_INVALID_INPUT:
  39. $fault = new soap_fault(
  40. 'Server',
  41. '',
  42. 'The input variables are invalid o are not correctly set'
  43. );
  44. break;
  45. case WS_ERROR_SETTING:
  46. $fault = new soap_fault(
  47. 'Server',
  48. '',
  49. 'Please check the configuration for this webservice'
  50. );
  51. break;
  52. }
  53. return $fault;
  54. }
  55. /**
  56. * @param array $params
  57. *
  58. * @return bool
  59. */
  60. function WSHelperVerifyKey($params)
  61. {
  62. global $_configuration, $debug;
  63. if (is_array($params)) {
  64. $secret_key = $params['secret_key'];
  65. } else {
  66. $secret_key = $params;
  67. }
  68. //error_log(print_r($params,1));
  69. $check_ip = false;
  70. $ip_matches = false;
  71. $ip = trim($_SERVER['REMOTE_ADDR']);
  72. // if we are behind a reverse proxy, assume it will send the
  73. // HTTP_X_FORWARDED_FOR header and use this IP instead
  74. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  75. list($ip1) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  76. $ip = trim($ip1);
  77. }
  78. if ($debug) {
  79. error_log("ip: $ip");
  80. }
  81. // Check if a file that limits access from webservices exists and contains
  82. // the restraining check
  83. if (is_file('webservice-auth-ip.conf.php')) {
  84. include 'webservice-auth-ip.conf.php';
  85. if ($debug) {
  86. error_log("webservice-auth-ip.conf.php file included");
  87. }
  88. if (!empty($ws_auth_ip)) {
  89. $check_ip = true;
  90. $ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
  91. if ($debug) {
  92. error_log("ip_matches: $ip_matches");
  93. }
  94. }
  95. }
  96. if ($debug) {
  97. error_log("checkip ".intval($check_ip));
  98. }
  99. if ($check_ip) {
  100. $security_key = $_configuration['security_key'];
  101. } else {
  102. $security_key = $ip.$_configuration['security_key'];
  103. //error_log($ip.'-'.$secret_key.'-'.$security_key);
  104. }
  105. $result = api_is_valid_secret_key($secret_key, $security_key);
  106. if ($debug) {
  107. error_log('WSHelperVerifyKey result: '.intval($result));
  108. }
  109. return $result;
  110. }
  111. // Create the server instance
  112. $server = new soap_server();
  113. /** @var HookWSRegistration $hook */
  114. $hook = HookWSRegistration::create();
  115. if (!empty($hook)) {
  116. $hook->setEventData(['server' => $server]);
  117. $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE);
  118. if (!empty($res['server'])) {
  119. $server = $res['server'];
  120. }
  121. }
  122. $server->soap_defencoding = 'UTF-8';
  123. // Initialize WSDL support
  124. $server->configureWSDL('WSRegistration', 'urn:WSRegistration');
  125. /* Register WSCreateUsers function */
  126. // Register the data structures used by the service
  127. // Prepare input params
  128. $server->wsdl->addComplexType(
  129. 'extras',
  130. 'complexType',
  131. 'struct',
  132. 'all',
  133. '',
  134. [
  135. 'field_name' => ['name' => 'field_name', 'type' => 'xsd:string'],
  136. 'field_value' => ['name' => 'field_value', 'type' => 'xsd:string'],
  137. ]
  138. );
  139. $server->wsdl->addComplexType(
  140. 'extrasList',
  141. 'complexType',
  142. 'array',
  143. '',
  144. 'SOAP-ENC:Array',
  145. [],
  146. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:extras[]']],
  147. 'tns:extras'
  148. );
  149. $server->wsdl->addComplexType(
  150. 'usersParams',
  151. 'complexType',
  152. 'struct',
  153. 'all',
  154. '',
  155. [
  156. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  157. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  158. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  159. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  160. 'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
  161. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  162. 'language' => ['name' => 'language', 'type' => 'xsd:string'],
  163. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  164. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  165. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  166. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  167. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  168. ]
  169. );
  170. $server->wsdl->addComplexType(
  171. 'usersParamsList',
  172. 'complexType',
  173. 'array',
  174. '',
  175. 'SOAP-ENC:Array',
  176. [],
  177. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:usersParams[]']],
  178. 'tns:usersParams'
  179. );
  180. $server->wsdl->addComplexType(
  181. 'createUsers',
  182. 'complexType',
  183. 'struct',
  184. 'all',
  185. '',
  186. [
  187. 'users' => ['name' => 'users', 'type' => 'tns:usersParamsList'],
  188. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  189. ]
  190. );
  191. // Prepare output params, in this case will return an array
  192. $server->wsdl->addComplexType(
  193. 'result_createUsers',
  194. 'complexType',
  195. 'struct',
  196. 'all',
  197. '',
  198. [
  199. 'original_user_id_value' => [
  200. 'name' => 'original_user_id_value',
  201. 'type' => 'xsd:string',
  202. ],
  203. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  204. ]
  205. );
  206. $server->wsdl->addComplexType(
  207. 'results_createUsers',
  208. 'complexType',
  209. 'array',
  210. '',
  211. 'SOAP-ENC:Array',
  212. [],
  213. [
  214. [
  215. 'ref' => 'SOAP-ENC:arrayType',
  216. 'wsdl:arrayType' => 'tns:result_createUsers[]',
  217. ],
  218. ],
  219. 'tns:result_createUsers'
  220. );
  221. // Register the method to expose
  222. $server->register(
  223. 'WSCreateUsers', // method name
  224. ['createUsers' => 'tns:createUsers'], // input parameters
  225. ['return' => 'tns:results_createUsers'], // output parameters
  226. 'urn:WSRegistration', // namespace
  227. 'urn:WSRegistration#WSCreateUsers', // soapaction
  228. 'rpc', // style
  229. 'encoded', // use
  230. 'This service adds a user' // documentation
  231. );
  232. // Define the method WSCreateUsers
  233. function WSCreateUsers($params)
  234. {
  235. if (!WSHelperVerifyKey($params)) {
  236. return returnError(WS_ERROR_SECRET_KEY);
  237. }
  238. $users_params = $params['users'];
  239. $results = [];
  240. $orig_user_id_value = [];
  241. $userManager = UserManager::getManager();
  242. $userRepository = UserManager::getRepository();
  243. foreach ($users_params as $user_param) {
  244. $firstName = $user_param['firstname'];
  245. $lastName = $user_param['lastname'];
  246. $status = $user_param['status'];
  247. $email = $user_param['email'];
  248. $loginName = $user_param['loginname'];
  249. $password = $user_param['password'];
  250. $official_code = '';
  251. $language = '';
  252. $phone = '';
  253. $picture_uri = '';
  254. $auth_source = PLATFORM_AUTH_SOURCE;
  255. $expiration_date = '';
  256. $active = 1;
  257. $hr_dept_id = 0;
  258. $extra = null;
  259. $original_user_id_name = $user_param['original_user_id_name'];
  260. $original_user_id_value = $user_param['original_user_id_value'];
  261. $orig_user_id_value[] = $user_param['original_user_id_value'];
  262. $extra_list = $user_param['extra'];
  263. if (!empty($user_param['language'])) {
  264. $language = $user_param['language'];
  265. }
  266. if (!empty($user_param['phone'])) {
  267. $phone = $user_param['phone'];
  268. }
  269. if (!empty($user_param['expiration_date'])) {
  270. $expiration_date = $user_param['expiration_date'];
  271. }
  272. // Check if exits x_user_id into user_field_values table.
  273. $user_id = UserManager::get_user_id_from_original_id(
  274. $original_user_id_value,
  275. $original_user_id_name
  276. );
  277. if ($user_id > 0) {
  278. /** @var User $user */
  279. $user = $userRepository->find($user_id);
  280. if ($user && $user->isActive() == false) {
  281. if (!is_null($password)) {
  282. $user->setPlainPassword($password);
  283. }
  284. if (!is_null($auth_source)) {
  285. $user->setAuthSource($auth_source);
  286. }
  287. if (!empty($user_param['expiration_date'])) {
  288. $expiration_date = new DateTime($user_param['expiration_date']);
  289. }
  290. $user->setLastname($lastName)
  291. ->setFirstname($firstName)
  292. ->setUsername($loginName)
  293. ->setEmail($email)
  294. ->setStatus($status)
  295. ->setOfficialCode($official_code)
  296. ->setPhone($phone)
  297. ->setExpirationDate($expiration_date)
  298. ->setHrDeptId($hr_dept_id)
  299. ->setActive(true);
  300. $userManager->updateUser($user, true);
  301. $results[] = $user_id;
  302. continue;
  303. //return $r_check_user[0];
  304. } else {
  305. $results[] = 0;
  306. continue;
  307. //return 0;
  308. // user id already exits.
  309. }
  310. }
  311. // Default language.
  312. if (empty($language)) {
  313. $language = api_get_setting('platformLanguage');
  314. }
  315. $creatorId = DEFAULT_ADMIN_USER_ID;
  316. // First check wether the login already exists.
  317. if (!UserManager::is_username_available($loginName)) {
  318. $results[] = 0;
  319. continue;
  320. }
  321. $userId = UserManager::create_user(
  322. $firstName,
  323. $lastName,
  324. $status,
  325. $email,
  326. $loginName,
  327. $password,
  328. $official_code,
  329. $language,
  330. $phone,
  331. $picture_uri,
  332. $auth_source,
  333. $expiration_date,
  334. $active,
  335. $hr_dept_id,
  336. [],
  337. '',
  338. false,
  339. false,
  340. '',
  341. false,
  342. null,
  343. $creatorId
  344. );
  345. if ($userId) {
  346. if (api_is_multiple_url_enabled()) {
  347. if (api_get_current_access_url_id() != -1) {
  348. UrlManager::add_user_to_url(
  349. $userId,
  350. api_get_current_access_url_id()
  351. );
  352. } else {
  353. UrlManager::add_user_to_url($userId, 1);
  354. }
  355. } else {
  356. // We add by default the access_url_user table with access_url_id = 1
  357. UrlManager::add_user_to_url($userId, 1);
  358. }
  359. // Save new field label into user_field table.
  360. UserManager::create_extra_field(
  361. $original_user_id_name,
  362. 1,
  363. $original_user_id_name,
  364. ''
  365. );
  366. // Save the external system's id into user_field_value table.
  367. UserManager::update_extra_field_value(
  368. $userId,
  369. $original_user_id_name,
  370. $original_user_id_value
  371. );
  372. if (is_array($extra_list) && count($extra_list) > 0) {
  373. foreach ($extra_list as $extra) {
  374. $extra_field_name = $extra['field_name'];
  375. $extra_field_value = $extra['field_value'];
  376. // Save new field label into user_field table.
  377. UserManager::create_extra_field(
  378. $extra_field_name,
  379. 1,
  380. $extra_field_name,
  381. ''
  382. );
  383. // Save the external system's id into user_field_value table.
  384. UserManager::update_extra_field_value(
  385. $userId,
  386. $extra_field_name,
  387. $extra_field_value
  388. );
  389. }
  390. }
  391. } else {
  392. $results[] = 0;
  393. continue;
  394. }
  395. $results[] = $userId;
  396. } // end principal foreach
  397. $count_results = count($results);
  398. $output = [];
  399. for ($i = 0; $i < $count_results; $i++) {
  400. $output[] = [
  401. 'original_user_id_value' => $orig_user_id_value[$i],
  402. 'result' => $results[$i],
  403. ];
  404. }
  405. return $output;
  406. }
  407. /* Register WSCreateUser function */
  408. // Register the data structures used by the service
  409. $server->wsdl->addComplexType(
  410. 'createUser',
  411. 'complexType',
  412. 'struct',
  413. 'all',
  414. '',
  415. [
  416. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  417. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  418. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  419. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  420. 'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
  421. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  422. 'language' => ['name' => 'language', 'type' => 'xsd:string'],
  423. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  424. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  425. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  426. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  427. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  428. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  429. 'active' => ['name' => 'extra', 'type' => 'xsd:string'],
  430. ]
  431. );
  432. // Register the method to expose
  433. $server->register(
  434. 'WSCreateUser', // method name
  435. ['createUser' => 'tns:createUser'], // input parameters
  436. ['return' => 'xsd:string'], // output parameters
  437. 'urn:WSRegistration', // namespace
  438. 'urn:WSRegistration#WSCreateUser', // soapaction
  439. 'rpc', // style
  440. 'encoded', // use
  441. 'This service adds a user' // documentation
  442. );
  443. // Define the method WSCreateUser
  444. function WSCreateUser($params)
  445. {
  446. global $debug;
  447. if (!WSHelperVerifyKey($params)) {
  448. return returnError(WS_ERROR_SECRET_KEY);
  449. }
  450. $firstName = $params['firstname'];
  451. $lastName = $params['lastname'];
  452. $status = $params['status'];
  453. $email = $params['email'];
  454. $loginName = $params['loginname'];
  455. $password = $params['password'];
  456. $official_code = '';
  457. $language = '';
  458. $phone = '';
  459. $picture_uri = '';
  460. $auth_source = PLATFORM_AUTH_SOURCE;
  461. $expiration_date = null;
  462. $active = !isset($params['active']) || !intval($params['active']) ? 0 : 1;
  463. $hr_dept_id = 0;
  464. $extra = null;
  465. $original_user_id_name = $params['original_user_id_name'];
  466. $original_user_id_value = $params['original_user_id_value'];
  467. $extra_list = $params['extra'];
  468. if (!empty($params['language'])) {
  469. $language = $params['language'];
  470. }
  471. if (!empty($params['phone'])) {
  472. $phone = $params['phone'];
  473. }
  474. if (!empty($params['expiration_date'])) {
  475. $expiration_date = $params['expiration_date'];
  476. }
  477. // check if exits x_user_id into user_field_values table
  478. $user_id = UserManager::get_user_id_from_original_id(
  479. $original_user_id_value,
  480. $original_user_id_name
  481. );
  482. $userManager = UserManager::getManager();
  483. $userRepository = UserManager::getRepository();
  484. if ($user_id > 0) {
  485. /** @var User $user */
  486. $user = $userRepository->find($user_id);
  487. if ($user && $user->isActive() == false) {
  488. if (!is_null($password)) {
  489. $user->setPlainPassword($password);
  490. }
  491. if (!is_null($auth_source)) {
  492. $user->setAuthSource($auth_source);
  493. }
  494. if (!empty($params['expiration_date'])) {
  495. $expiration_date = new DateTime($params['expiration_date']);
  496. $user->setExpirationDate($expiration_date);
  497. }
  498. $user->setLastname($lastName)
  499. ->setFirstname($firstName)
  500. ->setUsername($loginName)
  501. ->setEmail($email)
  502. ->setStatus($status)
  503. ->setOfficialCode($official_code)
  504. ->setPhone($phone)
  505. ->setHrDeptId($hr_dept_id)
  506. ->setActive(true);
  507. $userManager->updateUser($user, true);
  508. return $user_id;
  509. } else {
  510. return 0;
  511. }
  512. }
  513. // Default language
  514. if (empty($language)) {
  515. $language = api_get_setting('platformLanguage');
  516. }
  517. $creatorId = DEFAULT_ADMIN_USER_ID;
  518. // First check wether the login already exists
  519. if (!UserManager::is_username_available($loginName)) {
  520. if ($debug) {
  521. error_log("Username $loginName is not available");
  522. }
  523. return 0;
  524. }
  525. if (isset($original_user_id_name) && isset($original_user_id_value)) {
  526. $_SESSION['ws_'.$original_user_id_name] = $original_user_id_value;
  527. }
  528. /** @var User $user */
  529. $userId = UserManager::create_user(
  530. $firstName,
  531. $lastName,
  532. $status,
  533. $email,
  534. $loginName,
  535. $password,
  536. $official_code,
  537. $language,
  538. $phone,
  539. $picture_uri,
  540. $auth_source,
  541. $expiration_date,
  542. $active,
  543. $hr_dept_id,
  544. [],
  545. '',
  546. false,
  547. false,
  548. '',
  549. false,
  550. null,
  551. $creatorId
  552. );
  553. if ($userId) {
  554. if (api_is_multiple_url_enabled()) {
  555. if (api_get_current_access_url_id() != -1) {
  556. UrlManager::add_user_to_url($userId, api_get_current_access_url_id());
  557. } else {
  558. UrlManager::add_user_to_url($userId, 1);
  559. }
  560. } else {
  561. // We add by default the access_url_user table with access_url_id = 1
  562. UrlManager::add_user_to_url($userId, 1);
  563. }
  564. // Save new fieldlabel into user_field table.
  565. UserManager::create_extra_field(
  566. $original_user_id_name,
  567. 1,
  568. $original_user_id_name,
  569. ''
  570. );
  571. // Save the external system's id into user_field_value table.
  572. UserManager::update_extra_field_value(
  573. $userId,
  574. $original_user_id_name,
  575. $original_user_id_value
  576. );
  577. if (isset($original_user_id_name) && isset($original_user_id_value)) {
  578. unset($_SESSION['ws_'.$original_user_id_name]);
  579. }
  580. if (is_array($extra_list) && count($extra_list) > 0) {
  581. foreach ($extra_list as $extra) {
  582. $extra_field_name = $extra['field_name'];
  583. $extra_field_value = $extra['field_value'];
  584. // Save new field label into user_field table.
  585. UserManager::create_extra_field(
  586. $extra_field_name,
  587. 1,
  588. $extra_field_name,
  589. ''
  590. );
  591. // Save the external system's id into user_field_value table.
  592. UserManager::update_extra_field_value(
  593. $userId,
  594. $extra_field_name,
  595. $extra_field_value
  596. );
  597. }
  598. }
  599. } else {
  600. return 0;
  601. }
  602. return $userId;
  603. }
  604. /* Register WSCreateUsersPasswordCrypted function */
  605. // Register the data structures used by the service
  606. // Prepare input params.
  607. // Input params for editing users
  608. $server->wsdl->addComplexType(
  609. 'createUsersPassEncryptParams',
  610. 'complexType',
  611. 'struct',
  612. 'all',
  613. '',
  614. [
  615. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  616. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  617. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  618. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  619. 'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
  620. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  621. 'encrypt_method' => ['name' => 'encrypt_method', 'type' => 'xsd:string'],
  622. 'language' => ['name' => 'language', 'type' => 'xsd:string'],
  623. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  624. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  625. 'official_code' => ['name' => 'official_code', 'type' => 'xsd:string'],
  626. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  627. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  628. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  629. ]
  630. );
  631. $server->wsdl->addComplexType(
  632. 'createUsersPassEncryptParamsList',
  633. 'complexType',
  634. 'array',
  635. '',
  636. 'SOAP-ENC:Array',
  637. [],
  638. [
  639. [
  640. 'ref' => 'SOAP-ENC:arrayType',
  641. 'wsdl:arrayType' => 'tns:createUsersPassEncryptParams[]',
  642. ],
  643. ],
  644. 'tns:createUsersPassEncryptParams'
  645. );
  646. // Register the data structures used by the service
  647. $server->wsdl->addComplexType(
  648. 'createUsersPasswordCrypted',
  649. 'complexType',
  650. 'struct',
  651. 'all',
  652. '',
  653. [
  654. 'users' => [
  655. 'name' => 'users',
  656. 'type' => 'tns:createUsersPassEncryptParamsList',
  657. ],
  658. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  659. ]
  660. );
  661. // Prepare output params, in this case will return an array
  662. $server->wsdl->addComplexType(
  663. 'result_createUsersPassEncrypt',
  664. 'complexType',
  665. 'struct',
  666. 'all',
  667. '',
  668. [
  669. 'original_user_id_value' => [
  670. 'name' => 'original_user_id_value',
  671. 'type' => 'xsd:string',
  672. ],
  673. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  674. ]
  675. );
  676. $server->wsdl->addComplexType(
  677. 'results_createUsersPassEncrypt',
  678. 'complexType',
  679. 'array',
  680. '',
  681. 'SOAP-ENC:Array',
  682. [],
  683. [
  684. [
  685. 'ref' => 'SOAP-ENC:arrayType',
  686. 'wsdl:arrayType' => 'tns:result_createUsersPassEncrypt[]',
  687. ],
  688. ],
  689. 'tns:result_createUsersPassEncrypt'
  690. );
  691. // Register the method to expose
  692. $server->register(
  693. 'WSCreateUsersPasswordCrypted', // method name
  694. ['createUsersPasswordCrypted' => 'tns:createUsersPasswordCrypted'], // input parameters
  695. ['return' => 'tns:results_createUsersPassEncrypt'], // output parameters
  696. 'urn:WSRegistration', // namespace
  697. 'urn:WSRegistration#WSCreateUsersPasswordCrypted', // soapaction
  698. 'rpc', // style
  699. 'encoded', // use
  700. 'This service adds users to the system' // documentation
  701. );
  702. // Define the method WSCreateUsersPasswordCrypted
  703. function WSCreateUsersPasswordCrypted($params)
  704. {
  705. global $_configuration;
  706. if (!WSHelperVerifyKey($params)) {
  707. return returnError(WS_ERROR_SECRET_KEY);
  708. }
  709. // database table definition
  710. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  711. $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD);
  712. $t_ufv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  713. $users_params = $params['users'];
  714. $results = [];
  715. $orig_user_id_value = [];
  716. foreach ($users_params as $user_param) {
  717. $password = $user_param['password'];
  718. $encrypt_method = $user_param['encrypt_method'];
  719. $firstName = $user_param['firstname'];
  720. $lastName = $user_param['lastname'];
  721. $status = $user_param['status'];
  722. $email = $user_param['email'];
  723. $loginName = $user_param['loginname'];
  724. $official_code = $user_param['official_code'];
  725. $language = '';
  726. $phone = '';
  727. $picture_uri = '';
  728. $auth_source = PLATFORM_AUTH_SOURCE;
  729. $expiration_date = '';
  730. $active = 1;
  731. $hr_dept_id = 0;
  732. $extra = null;
  733. $original_user_id_name = $user_param['original_user_id_name'];
  734. $original_user_id_value = $user_param['original_user_id_value'];
  735. $orig_user_id_value[] = $user_param['original_user_id_value'];
  736. $extra_list = $user_param['extra'];
  737. $salt = '';
  738. if (!empty($_configuration['password_encryption'])) {
  739. if ($_configuration['password_encryption'] === $encrypt_method) {
  740. if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
  741. $msg = "Encryption $encrypt_method is invalid";
  742. $results[] = $msg;
  743. continue;
  744. } elseif ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
  745. $msg = "Encryption $encrypt_method is invalid";
  746. $results[] = $msg;
  747. continue;
  748. }
  749. } else {
  750. $msg = "This encryption $encrypt_method is not configured";
  751. $results[] = $msg;
  752. continue;
  753. }
  754. } else {
  755. $msg = 'The chamilo setting $_configuration["password_encryption"] is not configured';
  756. $results[] = $msg;
  757. continue;
  758. }
  759. if (is_array($extra_list) && count($extra_list) > 0) {
  760. foreach ($extra_list as $extra) {
  761. if ($extra['field_name'] == 'salt') {
  762. $salt = $extra['field_value'];
  763. break;
  764. }
  765. }
  766. }
  767. if (!empty($user_param['language'])) {
  768. $language = $user_param['language'];
  769. }
  770. if (!empty($user_param['phone'])) {
  771. $phone = $user_param['phone'];
  772. }
  773. if (!empty($user_param['expiration_date'])) {
  774. $expiration_date = $user_param['expiration_date'];
  775. }
  776. $extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  777. // Check whether x_user_id exists into user_field_values table.
  778. $sql = "SELECT value as field_value,item_id as user_id
  779. FROM $t_uf uf, $t_ufv ufv
  780. WHERE
  781. uf.extra_field_type = $extraFieldType
  782. ufv.field_id=uf.id AND
  783. variable='$original_user_id_name' AND
  784. value ='$original_user_id_value'";
  785. $res = Database::query($sql);
  786. $row = Database::fetch_row($res);
  787. $count_row = Database::num_rows($res);
  788. if ($count_row > 0) {
  789. // Check if user is not active.
  790. $sql = "SELECT user_id FROM $table_user
  791. WHERE user_id ='".$row[1]."' AND active= '0'";
  792. $resu = Database::query($sql);
  793. $r_check_user = Database::fetch_row($resu);
  794. $count_check_user = Database::num_rows($resu);
  795. if ($count_check_user > 0) {
  796. $sql = "UPDATE $table_user SET
  797. lastname='".Database::escape_string($lastName)."',
  798. firstname='".Database::escape_string($firstName)."',
  799. username='".Database::escape_string($loginName)."',";
  800. if (!is_null($auth_source)) {
  801. $sql .= " auth_source='".Database::escape_string($auth_source)."',";
  802. }
  803. $sql .= "
  804. password='".Database::escape_string($password)."',
  805. email='".Database::escape_string($email)."',
  806. status='".Database::escape_string($status)."',
  807. official_code='".Database::escape_string($official_code)."',
  808. phone='".Database::escape_string($phone)."',
  809. expiration_date='".Database::escape_string($expiration_date)."',
  810. active='1',
  811. hr_dept_id=".intval($hr_dept_id);
  812. $sql .= " WHERE user_id='".$r_check_user[0]."'";
  813. Database::query($sql);
  814. if (is_array($extra_list) && count($extra_list) > 0) {
  815. foreach ($extra_list as $extra) {
  816. $extra_field_name = $extra['field_name'];
  817. $extra_field_value = $extra['field_value'];
  818. // Save the external system's id into user_field_value table.
  819. UserManager::update_extra_field_value(
  820. $r_check_user[0],
  821. $extra_field_name,
  822. $extra_field_value
  823. );
  824. }
  825. }
  826. $results[] = $r_check_user[0];
  827. continue;
  828. } else {
  829. $results[] = 0;
  830. continue; // User id already exits.
  831. }
  832. }
  833. // Default language.
  834. if (empty($language)) {
  835. $language = api_get_setting('platformLanguage');
  836. }
  837. $creator_id = DEFAULT_ADMIN_USER_ID;
  838. // First check wether the login already exists
  839. if (!UserManager::is_username_available($loginName)) {
  840. $results[] = 0;
  841. continue;
  842. }
  843. $sql = "INSERT INTO $table_user SET
  844. lastname = '".Database::escape_string(trim($lastName))."',
  845. firstname = '".Database::escape_string(trim($firstName))."',
  846. username = '".Database::escape_string(trim($loginName))."',
  847. status = '".Database::escape_string($status)."',
  848. password = '".Database::escape_string($password)."',
  849. email = '".Database::escape_string($email)."',
  850. official_code = '".Database::escape_string($official_code)."',
  851. picture_uri = '".Database::escape_string($picture_uri)."',
  852. creator_id = '".Database::escape_string($creator_id)."',
  853. auth_source = '".Database::escape_string($auth_source)."',
  854. phone = '".Database::escape_string($phone)."',
  855. language = '".Database::escape_string($language)."',
  856. registration_date = now(),
  857. expiration_date = '".Database::escape_string($expiration_date)."',
  858. hr_dept_id = '".Database::escape_string($hr_dept_id)."',
  859. active = '".Database::escape_string($active)."'";
  860. $result = Database::query($sql);
  861. if ($result) {
  862. //echo "id returned";
  863. $return = Database::insert_id();
  864. $sql = "UPDATE $table_user SET user_id = id WHERE id = $return";
  865. Database::query($sql);
  866. if (api_is_multiple_url_enabled()) {
  867. if (api_get_current_access_url_id() != -1) {
  868. UrlManager::add_user_to_url(
  869. $return,
  870. api_get_current_access_url_id()
  871. );
  872. } else {
  873. UrlManager::add_user_to_url($return, 1);
  874. }
  875. } else {
  876. // We add by default the access_url_user table with access_url_id = 1
  877. UrlManager::add_user_to_url($return, 1);
  878. }
  879. // Save new fieldlabel into user_field table.
  880. UserManager::create_extra_field(
  881. $original_user_id_name,
  882. 1,
  883. $original_user_id_name,
  884. ''
  885. );
  886. // Save the remote system's id into user_field_value table.
  887. UserManager::update_extra_field_value(
  888. $return,
  889. $original_user_id_name,
  890. $original_user_id_value
  891. );
  892. if (is_array($extra_list) && count($extra_list) > 0) {
  893. foreach ($extra_list as $extra) {
  894. $extra_field_name = $extra['field_name'];
  895. $extra_field_value = $extra['field_value'];
  896. // Save new fieldlabel into user_field table.
  897. UserManager::create_extra_field(
  898. $extra_field_name,
  899. 1,
  900. $extra_field_name,
  901. ''
  902. );
  903. // Save the external system's id into user_field_value table.
  904. UserManager::update_extra_field_value(
  905. $return,
  906. $extra_field_name,
  907. $extra_field_value
  908. );
  909. }
  910. }
  911. } else {
  912. $results[] = 0;
  913. continue;
  914. }
  915. $results[] = $return;
  916. } // end principal foreach
  917. $count_results = count($results);
  918. $output = [];
  919. for ($i = 0; $i < $count_results; $i++) {
  920. $output[] = [
  921. 'original_user_id_value' => $orig_user_id_value[$i],
  922. 'result' => $results[$i],
  923. ];
  924. }
  925. return $output;
  926. }
  927. // Subscribe / Unsubscribe Teacher to Session Course
  928. // Prepare Input params for Subscribe Teacher to SC
  929. $server->wsdl->addComplexType(
  930. 'TeacherToSessionCourse',
  931. 'complexType',
  932. 'struct',
  933. 'all',
  934. '',
  935. [
  936. 'user_id' => ['name' => 'course', 'type' => 'xsd:string'], // Chamilo user Id
  937. 'session_id' => ['name' => 'user_id', 'type' => 'xsd:string'], // Current Session course ID
  938. 'course_id' => ['name' => 'courseId', 'type' => 'xsd:string'], // Course Real Id
  939. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  940. // optional
  941. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  942. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  943. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  944. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  945. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  946. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  947. ]
  948. );
  949. /**
  950. * @param array $params
  951. *
  952. * @return array
  953. */
  954. function parseCourseSessionUserParams($params)
  955. {
  956. global $debug;
  957. $userId = isset($params['user_id']) ? $params['user_id'] : 0; // Chamilo user Id
  958. $sessionId = isset($params['session_id']) ? $params['session_id'] : 0; // Current Session course ID
  959. $courseId = isset($params['course_id']) ? $params['course_id'] : 0; // Course Real Id
  960. if (empty($userId) && empty($sessionId) && empty($courseId)) {
  961. // try original values
  962. if ($debug) {
  963. error_log('try original values');
  964. }
  965. $userIdName = isset($params['original_user_id_name']) ? $params['original_user_id_name'] : 0;
  966. $userIdValue = isset($params['original_user_id_value']) ? $params['original_user_id_value'] : 0;
  967. $courseIdName = isset($params['original_course_id_name']) ? $params['original_course_id_name'] : 0;
  968. $courseIdValue = isset($params['original_course_id_value']) ? $params['original_course_id_value'] : 0;
  969. $sessionIdName = isset($params['original_session_id_name']) ? $params['original_session_id_name'] : 0;
  970. $sessionIdValue = isset($params['original_session_id_value']) ? $params['original_session_id_value'] : 0;
  971. // Check if exits x_user_id into user_field_values table.
  972. $userId = UserManager::get_user_id_from_original_id(
  973. $userIdValue,
  974. $userIdName
  975. );
  976. // Check whether exits $x_course_code into user_field_values table.
  977. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  978. $courseIdValue,
  979. $courseIdName
  980. );
  981. $courseId = 0;
  982. if ($courseInfo) {
  983. $courseId = $courseInfo['real_id'];
  984. }
  985. $sessionId = SessionManager::getSessionIdFromOriginalId(
  986. $sessionIdValue,
  987. $sessionIdName
  988. );
  989. }
  990. if ($debug) {
  991. error_log('$userId found: '.$userId);
  992. error_log('$courseId found: '.$courseId);
  993. error_log('$sessionId found: '.$sessionId);
  994. }
  995. return [
  996. 'user_id' => $userId,
  997. 'course_id' => $courseId,
  998. 'session_id' => $sessionId,
  999. ];
  1000. }
  1001. $server->register(
  1002. 'WSSubscribeTeacherToSessionCourse',
  1003. ['SubscribeTeacherToSessionCourse' => 'tns:TeacherToSessionCourse'],
  1004. ['return' => 'xsd:string'],
  1005. 'urn:WSRegistration',
  1006. 'urn:WSRegistration#WSSubscribeTeacherToSessionCourse',
  1007. 'rpc',
  1008. 'encoded',
  1009. 'This webservice subscribe a teacher to a session course'
  1010. );
  1011. /**
  1012. * Subscribe teacher to a session course.
  1013. *
  1014. * @param array $params - WSFunction parameters (include VerifyKey)
  1015. *
  1016. * @return bool|soap_fault|null A simple boolean (true if teacher successful subscribed, false otherwise)
  1017. */
  1018. function WSSubscribeTeacherToSessionCourse($params)
  1019. {
  1020. global $debug;
  1021. if ($debug) {
  1022. error_log('WSSubscribeTeacherToSessionCourse');
  1023. }
  1024. if (!WSHelperVerifyKey($params)) {
  1025. return returnError(WS_ERROR_SECRET_KEY);
  1026. }
  1027. if ($debug) {
  1028. error_log('Params '.print_r($params, 1));
  1029. }
  1030. $params = parseCourseSessionUserParams($params);
  1031. $userId = $params['user_id'];
  1032. $courseId = $params['course_id'];
  1033. $sessionId = $params['session_id'];
  1034. SessionManager::set_coach_to_course_session($userId, $sessionId, $courseId);
  1035. $coaches = SessionManager::getCoachesByCourseSession($sessionId, $courseId);
  1036. $result = 0;
  1037. if (!empty($coaches)) {
  1038. if ($debug) {
  1039. error_log('Coaches: '.print_r($coaches, 1));
  1040. }
  1041. if (in_array($userId, $coaches)) {
  1042. $result = 1;
  1043. }
  1044. }
  1045. if ($debug) {
  1046. error_log('Result: '.$result);
  1047. }
  1048. return $result;
  1049. }
  1050. $server->register(
  1051. 'WSUnsubscribeTeacherFromSessionCourse',
  1052. ['UnsubscribeTeacherFromSessionCourse' => 'tns:TeacherToSessionCourse'],
  1053. ['return' => 'xsd:string'],
  1054. 'urn:WSRegistration',
  1055. 'urn:WSRegistration#WSUnsubscribeTeacherFromSessionCourse',
  1056. 'rpc',
  1057. 'encoded',
  1058. 'This webservice unsubscribe a teacher from a session course'
  1059. );
  1060. /**
  1061. * Subscribe teacher to a session course.
  1062. *
  1063. * @param array $params - WSFunction parameters (include VerifyKey)
  1064. *
  1065. * @return bool|soap_fault|null A simple boolean (true if teacher successful unsubscribed, false otherwise)
  1066. */
  1067. function WSUnsubscribeTeacherFromSessionCourse($params)
  1068. {
  1069. global $debug;
  1070. if ($debug) {
  1071. error_log('WSSubscribeTeacherToSessionCourse');
  1072. }
  1073. if (!WSHelperVerifyKey($params)) {
  1074. return returnError(WS_ERROR_SECRET_KEY);
  1075. }
  1076. if ($debug) {
  1077. error_log('Params '.print_r($params, 1));
  1078. }
  1079. $params = parseCourseSessionUserParams($params);
  1080. $userId = $params['user_id'];
  1081. $courseId = $params['course_id'];
  1082. $sessionId = $params['session_id'];
  1083. SessionManager::removeUsersFromCourseSession([$userId], $sessionId, $courseId);
  1084. $coaches = SessionManager::getCoachesByCourseSession($sessionId, $courseId);
  1085. $result = 0;
  1086. if (!empty($coaches)) {
  1087. if ($debug) {
  1088. error_log('Coaches: '.print_r($coaches, 1));
  1089. }
  1090. if (!in_array($userId, $coaches)) {
  1091. $result = 1;
  1092. }
  1093. } else {
  1094. $result = 1;
  1095. }
  1096. if ($debug) {
  1097. error_log('Final Result: '.$result);
  1098. }
  1099. return $result;
  1100. }
  1101. /* Register WSCreateUserPasswordCrypted function */
  1102. // Register the data structures used by the service
  1103. //prepare input params
  1104. // Input params for editing users
  1105. $server->wsdl->addComplexType(
  1106. 'createUserPasswordCrypted',
  1107. 'complexType',
  1108. 'struct',
  1109. 'all',
  1110. '',
  1111. [
  1112. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  1113. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  1114. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  1115. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  1116. 'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
  1117. 'password' => ['name' => 'password', 'type' => 'xsd:string'], //encripted password using the encrypt_method
  1118. 'encrypt_method' => ['name' => 'encrypt_method', 'type' => 'xsd:string'],
  1119. 'language' => ['name' => 'language', 'type' => 'xsd:string'],
  1120. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  1121. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  1122. 'official_code' => ['name' => 'official_code', 'type' => 'xsd:string'],
  1123. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  1124. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  1125. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  1126. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  1127. ]
  1128. );
  1129. // Register the method to expose
  1130. $server->register(
  1131. 'WSCreateUserPasswordCrypted', // method name
  1132. ['createUserPasswordCrypted' => 'tns:createUserPasswordCrypted'], // input parameters
  1133. ['return' => 'xsd:string'], // output parameters
  1134. 'urn:WSRegistration', // namespace
  1135. 'urn:WSRegistration#WSCreateUserPasswordCrypted', // soapaction
  1136. 'rpc', // style
  1137. 'encoded', // use
  1138. 'This service adds users' // documentation
  1139. );
  1140. // Define the method WSCreateUserPasswordCrypted
  1141. function WSCreateUserPasswordCrypted($params)
  1142. {
  1143. global $_configuration, $debug;
  1144. $debug = 1;
  1145. if ($debug) {
  1146. error_log('WSCreateUserPasswordCrypted');
  1147. error_log(print_r($params, 1));
  1148. }
  1149. if (!WSHelperVerifyKey($params)) {
  1150. return returnError(WS_ERROR_SECRET_KEY);
  1151. }
  1152. // Database table definition.
  1153. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1154. $password = $params['password'];
  1155. $encrypt_method = $params['encrypt_method'];
  1156. $firstName = $params['firstname'];
  1157. $lastName = $params['lastname'];
  1158. $status = $params['status'];
  1159. $email = $params['email'];
  1160. $loginName = $params['loginname'];
  1161. $official_code = isset($params['official_code']) ? $params['official_code'] : '';
  1162. $language = '';
  1163. $phone = isset($params['phone']) ? $params['phone'] : '';
  1164. $picture_uri = '';
  1165. $auth_source = PLATFORM_AUTH_SOURCE;
  1166. $expiration_date = '';
  1167. $active = 1;
  1168. $hr_dept_id = 0;
  1169. $extra = null;
  1170. $original_user_id_name = $params['original_user_id_name'];
  1171. $original_user_id_value = $params['original_user_id_value'];
  1172. $extra_list = isset($params['extra']) ? $params['extra'] : '';
  1173. if (!empty($_configuration['password_encryption'])) {
  1174. if ($_configuration['password_encryption'] === $encrypt_method) {
  1175. if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
  1176. $msg = "Encryption $encrypt_method is invalid";
  1177. if ($debug) {
  1178. error_log($msg);
  1179. }
  1180. return $msg;
  1181. } elseif ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
  1182. $msg = "Encryption $encrypt_method is invalid";
  1183. if ($debug) {
  1184. error_log($msg);
  1185. }
  1186. return $msg;
  1187. }
  1188. } else {
  1189. $msg = "This encryption $encrypt_method is not configured";
  1190. if ($debug) {
  1191. error_log($msg);
  1192. }
  1193. return $msg;
  1194. }
  1195. } else {
  1196. $msg = 'The chamilo setting $_configuration["password_encryption"] is not configured';
  1197. if ($debug) {
  1198. error_log($msg);
  1199. }
  1200. return $msg;
  1201. }
  1202. if (!empty($params['language'])) {
  1203. $language = $params['language'];
  1204. }
  1205. if (!empty($params['phone'])) {
  1206. $phone = $params['phone'];
  1207. }
  1208. if (!empty($params['expiration_date'])) {
  1209. $expiration_date = $params['expiration_date'];
  1210. }
  1211. // Check whether x_user_id exists into user_field_values table.
  1212. $user_id = UserManager::get_user_id_from_original_id(
  1213. $original_user_id_value,
  1214. $original_user_id_name
  1215. );
  1216. if ($debug) {
  1217. error_log('Ready to create user');
  1218. }
  1219. if ($user_id > 0) {
  1220. if ($debug) {
  1221. error_log('User found with id: '.$user_id);
  1222. }
  1223. // Check whether user is not active
  1224. //@todo why this condition exists??
  1225. $sql = "SELECT user_id FROM $table_user
  1226. WHERE user_id ='".$user_id."' AND active= '0' ";
  1227. $resu = Database::query($sql);
  1228. $r_check_user = Database::fetch_row($resu);
  1229. $count_check_user = Database::num_rows($resu);
  1230. if ($count_check_user > 0) {
  1231. if ($debug) {
  1232. error_log('User id: '.$user_id.' exists and is NOT active. Updating user and setting setting active = 1');
  1233. }
  1234. $sql = "UPDATE $table_user SET
  1235. lastname='".Database::escape_string($lastName)."',
  1236. firstname='".Database::escape_string($firstName)."',
  1237. username='".Database::escape_string($loginName)."',";
  1238. if (!is_null($auth_source)) {
  1239. $sql .= " auth_source='".Database::escape_string($auth_source)."',";
  1240. }
  1241. $sql .= "
  1242. password='".Database::escape_string($password)."',
  1243. email='".Database::escape_string($email)."',
  1244. status='".Database::escape_string($status)."',
  1245. official_code='".Database::escape_string($official_code)."',
  1246. phone='".Database::escape_string($phone)."',
  1247. expiration_date='".Database::escape_string($expiration_date)."',
  1248. active='1',
  1249. hr_dept_id=".intval($hr_dept_id)."
  1250. WHERE user_id='".$r_check_user[0]."'";
  1251. Database::query($sql);
  1252. if (is_array($extra_list) && count($extra_list) > 0) {
  1253. foreach ($extra_list as $extra) {
  1254. $extra_field_name = $extra['field_name'];
  1255. $extra_field_value = $extra['field_value'];
  1256. // Save the external system's id into user_field_value table.
  1257. UserManager::update_extra_field_value(
  1258. $r_check_user[0],
  1259. $extra_field_name,
  1260. $extra_field_value
  1261. );
  1262. }
  1263. }
  1264. return $r_check_user[0];
  1265. } else {
  1266. if ($debug) {
  1267. error_log('User exists but is active. Cant be updated');
  1268. }
  1269. return 0;
  1270. }
  1271. } else {
  1272. if ($debug) {
  1273. error_log(
  1274. "User not found with original_id = $original_user_id_value and original_name = $original_user_id_name"
  1275. );
  1276. }
  1277. }
  1278. // Default language.
  1279. if (empty($language)) {
  1280. $language = api_get_setting('platformLanguage');
  1281. }
  1282. $creator_id = DEFAULT_ADMIN_USER_ID;
  1283. // First check wether the login already exists
  1284. if (!UserManager::is_username_available($loginName)) {
  1285. if ($debug) {
  1286. error_log("Username $loginName is not available");
  1287. }
  1288. return 0;
  1289. }
  1290. $queryExpirationDate = '';
  1291. if (!empty($params['expiration_date'])) {
  1292. $queryExpirationDate = "expiration_date = '".Database::escape_string($expiration_date)."', ";
  1293. }
  1294. $sql = "INSERT INTO $table_user SET
  1295. lastname = '".Database::escape_string(trim($lastName))."',
  1296. firstname = '".Database::escape_string(trim($firstName))."',
  1297. username = '".Database::escape_string(trim($loginName))."',
  1298. username_canonical = '".Database::escape_string(api_strtolower(trim($loginName)))."',
  1299. status = '".Database::escape_string($status)."',
  1300. password = '".Database::escape_string($password)."',
  1301. email = '".Database::escape_string($email)."',
  1302. official_code = '".Database::escape_string($official_code)."',
  1303. picture_uri = '".Database::escape_string($picture_uri)."',
  1304. creator_id = '".Database::escape_string($creator_id)."',
  1305. auth_source = '".Database::escape_string($auth_source)."',
  1306. phone = '".Database::escape_string($phone)."',
  1307. language = '".Database::escape_string($language)."',
  1308. registration_date = '".api_get_utc_datetime()."',
  1309. roles = 'a:0:{}',
  1310. ".$queryExpirationDate."
  1311. hr_dept_id = '".Database::escape_string($hr_dept_id)."',
  1312. active = '".Database::escape_string($active)."'";
  1313. Database::query($sql);
  1314. $return = Database::insert_id();
  1315. if ($return) {
  1316. if ($debug) {
  1317. error_log("New user created. user_id = $return");
  1318. }
  1319. $sql = "UPDATE $table_user SET user_id = id WHERE id = $return";
  1320. Database::query($sql);
  1321. $url_id = api_get_current_access_url_id();
  1322. UrlManager::add_user_to_url($return, $url_id);
  1323. if ($debug) {
  1324. error_log("Adding user_id = $return to URL id $url_id ");
  1325. }
  1326. // Create extra field for the original_user_id_name
  1327. UserManager::create_extra_field(
  1328. $original_user_id_name,
  1329. 1,
  1330. $original_user_id_name,
  1331. ''
  1332. );
  1333. // Save the remote system's id into user_field_value table.
  1334. UserManager::update_extra_field_value(
  1335. $return,
  1336. $original_user_id_name,
  1337. $original_user_id_value
  1338. );
  1339. // Create extra fields
  1340. if (is_array($extra_list) && count($extra_list) > 0) {
  1341. foreach ($extra_list as $extra) {
  1342. $extra_field_name = $extra['field_name'];
  1343. $extra_field_value = $extra['field_value'];
  1344. // save new fieldlabel into user_field table
  1345. UserManager::create_extra_field(
  1346. $extra_field_name,
  1347. 1,
  1348. $extra_field_name,
  1349. ''
  1350. );
  1351. // save the external system's id into user_field_value table'
  1352. UserManager::update_extra_field_value(
  1353. $return,
  1354. $extra_field_name,
  1355. $extra_field_value
  1356. );
  1357. }
  1358. }
  1359. } else {
  1360. if ($debug) {
  1361. error_log('Error while inserting a user');
  1362. }
  1363. return 0;
  1364. }
  1365. if ($debug) {
  1366. error_log("Return value: $return");
  1367. }
  1368. return $return;
  1369. }
  1370. /* Register WSEditUsers function */
  1371. // Register the data structures used by the service
  1372. $server->wsdl->addComplexType(
  1373. 'editUsersParams',
  1374. 'complexType',
  1375. 'struct',
  1376. 'all',
  1377. '',
  1378. [
  1379. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  1380. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  1381. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  1382. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  1383. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  1384. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  1385. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  1386. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  1387. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  1388. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  1389. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  1390. ]
  1391. );
  1392. $server->wsdl->addComplexType(
  1393. 'editUsersParamsList',
  1394. 'complexType',
  1395. 'array',
  1396. '',
  1397. 'SOAP-ENC:Array',
  1398. [],
  1399. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:editUsersParams[]']],
  1400. 'tns:editUsersParams'
  1401. );
  1402. $server->wsdl->addComplexType(
  1403. 'editUsers',
  1404. 'complexType',
  1405. 'struct',
  1406. 'all',
  1407. '',
  1408. [
  1409. 'users' => ['name' => 'users', 'type' => 'tns:editUsersParamsList'],
  1410. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  1411. ]
  1412. );
  1413. /* Register WSEditUserCredentials function */
  1414. // Register the data structures used by the service
  1415. $server->wsdl->addComplexType(
  1416. 'editUserCredentials',
  1417. 'complexType',
  1418. 'struct',
  1419. 'all',
  1420. '',
  1421. [
  1422. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  1423. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  1424. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  1425. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  1426. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  1427. ]
  1428. );
  1429. // Register the method to expose
  1430. $server->register(
  1431. 'WSEditUserCredentials', // method name
  1432. ['editUserCredentials' => 'tns:editUserCredentials'], // input parameters
  1433. ['return' => 'xsd:string'], // output parameters
  1434. 'urn:WSRegistration', // namespace
  1435. 'urn:WSRegistration#WSEditUserCredentials', // soapaction
  1436. 'rpc', // style
  1437. 'encoded', // use
  1438. 'This service edits the username and password of a user' // documentation
  1439. );
  1440. /**
  1441. * Define the method WSEditUser.
  1442. *
  1443. * @param array $params
  1444. *
  1445. * @throws \Doctrine\DBAL\DBALException
  1446. *
  1447. * @return bool|int|soap_fault|null
  1448. */
  1449. function WSEditUserCredentials($params)
  1450. {
  1451. if (!WSHelperVerifyKey($params)) {
  1452. return returnError(WS_ERROR_SECRET_KEY);
  1453. }
  1454. $userManager = UserManager::getManager();
  1455. $userRepository = UserManager::getRepository();
  1456. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1457. $original_user_id_value = $params['original_user_id_value'];
  1458. $original_user_id_name = $params['original_user_id_name'];
  1459. $username = $params['username'];
  1460. $password = null;
  1461. if (!empty($params['password'])) {
  1462. $password = $params['password'];
  1463. }
  1464. // Get user id from the other system ID
  1465. $user_id = UserManager::get_user_id_from_original_id(
  1466. $original_user_id_value,
  1467. $original_user_id_name
  1468. );
  1469. if ($user_id == 0) {
  1470. return 0;
  1471. } else {
  1472. $sql = "SELECT user_id FROM $table_user
  1473. WHERE user_id ='$user_id' AND active= '0'";
  1474. $resu = Database::query($sql);
  1475. $r_check_user = Database::fetch_row($resu);
  1476. if (!empty($r_check_user[0])) {
  1477. return 0;
  1478. }
  1479. }
  1480. // Check whether username already exits.
  1481. $sql = "SELECT username FROM $table_user
  1482. WHERE username = '$username' AND user_id <> '$user_id'";
  1483. $res_un = Database::query($sql);
  1484. $r_username = Database::fetch_row($res_un);
  1485. if (!empty($r_username[0])) {
  1486. return 0;
  1487. }
  1488. /** @var User $user */
  1489. $user = $userRepository->find($user_id);
  1490. if ($user) {
  1491. $user->setUsername($username);
  1492. if (!is_null($password)) {
  1493. $user->setPlainPassword($password);
  1494. }
  1495. $userManager->updateUser($user, true);
  1496. return true;
  1497. }
  1498. return false;
  1499. }
  1500. // Prepare output params, in this case will return an array
  1501. $server->wsdl->addComplexType(
  1502. 'result_editUsers',
  1503. 'complexType',
  1504. 'struct',
  1505. 'all',
  1506. '',
  1507. [
  1508. 'original_user_id_value' => [
  1509. 'name' => 'original_user_id_value',
  1510. 'type' => 'xsd:string',
  1511. ],
  1512. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  1513. ]
  1514. );
  1515. $server->wsdl->addComplexType(
  1516. 'results_editUsers',
  1517. 'complexType',
  1518. 'array',
  1519. '',
  1520. 'SOAP-ENC:Array',
  1521. [],
  1522. [
  1523. [
  1524. 'ref' => 'SOAP-ENC:arrayType',
  1525. 'wsdl:arrayType' => 'tns:result_editUsers[]',
  1526. ],
  1527. ],
  1528. 'tns:result_editUsers'
  1529. );
  1530. // Register the method to expose
  1531. $server->register(
  1532. 'WSEditUsers', // method name
  1533. ['editUsers' => 'tns:editUsers'], // input parameters
  1534. ['return' => 'tns:results_editUsers'], // output parameters
  1535. 'urn:WSRegistration', // namespace
  1536. 'urn:WSRegistration#WSEditUsers', // soapaction
  1537. 'rpc', // style
  1538. 'encoded', // use
  1539. 'This service edits a user from wiener' // documentation
  1540. );
  1541. // Define the method WSEditUsers
  1542. function WSEditUsers($params)
  1543. {
  1544. if (!WSHelperVerifyKey($params)) {
  1545. return returnError(WS_ERROR_SECRET_KEY);
  1546. }
  1547. $userManager = UserManager::getManager();
  1548. $userRepository = UserManager::getRepository();
  1549. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1550. $users_params = $params['users'];
  1551. $results = [];
  1552. $orig_user_id_value = [];
  1553. foreach ($users_params as $user_param) {
  1554. $original_user_id_value = $user_param['original_user_id_value'];
  1555. $original_user_id_name = $user_param['original_user_id_name'];
  1556. $orig_user_id_value[] = $original_user_id_value;
  1557. $firstname = $user_param['firstname'];
  1558. $lastname = $user_param['lastname'];
  1559. $username = $user_param['username'];
  1560. $password = null;
  1561. $auth_source = null;
  1562. $email = $user_param['email'];
  1563. $status = $user_param['status'];
  1564. $official_code = '';
  1565. $phone = $user_param['phone'];
  1566. $expiration_date = $user_param['expiration_date'];
  1567. $creator_id = null;
  1568. $hr_dept_id = 0;
  1569. $extra = null;
  1570. $extra_list = $user_param['extra'];
  1571. if (!empty($user_param['password'])) {
  1572. $password = $user_param['password'];
  1573. }
  1574. // Get user id
  1575. $user_id = UserManager::get_user_id_from_original_id(
  1576. $original_user_id_value,
  1577. $original_user_id_name
  1578. );
  1579. if ($user_id == 0) {
  1580. $results[] = 0; // Original_user_id_value doesn't exist.
  1581. continue;
  1582. } else {
  1583. $sql = "SELECT user_id FROM $table_user
  1584. WHERE user_id ='$user_id' AND active= '0'";
  1585. $resu = Database::query($sql);
  1586. $r_check_user = Database::fetch_row($resu);
  1587. if (!empty($r_check_user[0])) {
  1588. $results[] = 0; // user_id is not active.
  1589. continue;
  1590. }
  1591. }
  1592. // Check whether username already exits.
  1593. $sql = "SELECT username FROM $table_user
  1594. WHERE username = '$username' AND user_id <> '$user_id'";
  1595. $res_un = Database::query($sql);
  1596. $r_username = Database::fetch_row($res_un);
  1597. if (!empty($r_username[0])) {
  1598. $results[] = 0; // username already exits.
  1599. continue;
  1600. }
  1601. // Edit lastname and firstname only if not empty
  1602. /** @var User $user */
  1603. $user = $userRepository->find($user_id);
  1604. if (!empty($lastname)) {
  1605. $user->setLastname($lastname);
  1606. }
  1607. if (!empty($firstname)) {
  1608. $user->setFirstname($firstname);
  1609. }
  1610. $user->setUsername($username);
  1611. if (!is_null($password)) {
  1612. $user->setPlainPassword($password);
  1613. }
  1614. if (!is_null($auth_source)) {
  1615. $user->setAuthSource($auth_source);
  1616. }
  1617. // Exception for admins in case no status is provided in WS call...
  1618. $t_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  1619. $sqladmin = "SELECT user_id FROM $t_admin WHERE user_id = ".intval($user_id);
  1620. $resadmin = Database::query($sqladmin);
  1621. $is_admin = Database::num_rows($resadmin);
  1622. if (empty($status)) {
  1623. $status = 5;
  1624. }
  1625. if ($is_admin) {
  1626. $status = 1;
  1627. }
  1628. if (!empty($expiration_date)) {
  1629. $expiration_date = new DateTime($expiration_date);
  1630. }
  1631. $user
  1632. ->setEmail($email)
  1633. ->setStatus($status)
  1634. ->setOfficialCode($official_code)
  1635. ->setPhone($phone)
  1636. ->setExpirationDate($expiration_date)
  1637. ->setHrDeptId($hr_dept_id)
  1638. ->setActive(true);
  1639. if (!is_null($creator_id)) {
  1640. $user->setCreatorId($creator_id);
  1641. }
  1642. $userManager->updateUser($user, true);
  1643. if (is_array($extra_list) && count($extra_list) > 0) {
  1644. foreach ($extra_list as $extra) {
  1645. $extra_field_name = $extra['field_name'];
  1646. $extra_field_value = $extra['field_value'];
  1647. // Save the external system's id into user_field_value table.
  1648. UserManager::update_extra_field_value(
  1649. $user_id,
  1650. $extra_field_name,
  1651. $extra_field_value
  1652. );
  1653. }
  1654. }
  1655. $results[] = $user->getId();
  1656. continue;
  1657. }
  1658. $count_results = count($results);
  1659. $output = [];
  1660. for ($i = 0; $i < $count_results; $i++) {
  1661. $output[] = [
  1662. 'original_user_id_value' => $orig_user_id_value[$i],
  1663. 'result' => $results[$i],
  1664. ];
  1665. }
  1666. return $output;
  1667. }
  1668. /* Register WSEditUser function */
  1669. // Register the data structures used by the service
  1670. $server->wsdl->addComplexType(
  1671. 'editUser',
  1672. 'complexType',
  1673. 'struct',
  1674. 'all',
  1675. '',
  1676. [
  1677. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  1678. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  1679. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  1680. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  1681. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  1682. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  1683. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  1684. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  1685. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  1686. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  1687. 'enable' => ['name' => 'enable', 'type' => 'xsd:boolean'],
  1688. 'language' => ['name' => 'language', 'type' => 'xsd:string'],
  1689. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  1690. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  1691. ]
  1692. );
  1693. // Register the method to expose
  1694. $server->register(
  1695. 'WSEditUser', // method name
  1696. ['editUser' => 'tns:editUser'], // input parameters
  1697. ['return' => 'xsd:string'], // output parameters
  1698. 'urn:WSRegistration', // namespace
  1699. 'urn:WSRegistration#WSEditUser', // soapaction
  1700. 'rpc', // style
  1701. 'encoded', // use
  1702. 'This service edits a user from wiener' // documentation
  1703. );
  1704. // Define the method WSEditUser
  1705. function WSEditUser($params)
  1706. {
  1707. if (!WSHelperVerifyKey($params)) {
  1708. return returnError(WS_ERROR_SECRET_KEY);
  1709. }
  1710. $userManager = UserManager::getManager();
  1711. $userRepository = UserManager::getRepository();
  1712. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1713. $original_user_id_value = $params['original_user_id_value'];
  1714. $original_user_id_name = $params['original_user_id_name'];
  1715. $firstname = $params['firstname'];
  1716. $lastname = $params['lastname'];
  1717. $username = $params['username'];
  1718. $password = null;
  1719. $auth_source = null;
  1720. $email = $params['email'];
  1721. $status = $params['status'];
  1722. $official_code = '';
  1723. $phone = $params['phone'];
  1724. $picture_uri = '';
  1725. $expiration_date = $params['expiration_date'];
  1726. $enable = $params['enable'];
  1727. $language = $params['language'];
  1728. $creator_id = null;
  1729. $hr_dept_id = 0;
  1730. $extra = null;
  1731. $extra_list = $params['extra'];
  1732. if (!empty($params['password'])) {
  1733. $password = $params['password'];
  1734. }
  1735. // Get user id from id wiener
  1736. $user_id = UserManager::get_user_id_from_original_id(
  1737. $original_user_id_value,
  1738. $original_user_id_name
  1739. );
  1740. if ($user_id == 0) {
  1741. return 0;
  1742. } elseif (empty($enable)) {
  1743. $sql = "SELECT user_id FROM $table_user
  1744. WHERE user_id ='$user_id' AND active= '0'";
  1745. $resu = Database::query($sql);
  1746. $r_check_user = Database::fetch_row($resu);
  1747. if (!empty($r_check_user[0])) {
  1748. return 0;
  1749. }
  1750. }
  1751. // Check whether username already exits.
  1752. $sql = "SELECT username FROM $table_user
  1753. WHERE username = '$username' AND user_id <> '$user_id'";
  1754. $res_un = Database::query($sql);
  1755. $r_username = Database::fetch_row($res_un);
  1756. if (!empty($r_username[0])) {
  1757. return 0;
  1758. }
  1759. /** @var User $user */
  1760. $user = $userRepository->find($user_id);
  1761. if (!empty($lastname)) {
  1762. $user->setLastname($lastname);
  1763. }
  1764. if (!empty($firstname)) {
  1765. $user->setFirstname($firstname);
  1766. }
  1767. $user->setUsername($username);
  1768. if (!is_null($password)) {
  1769. $user->setPlainPassword($password);
  1770. }
  1771. if (!is_null($auth_source)) {
  1772. $user->setAuthSource($auth_source);
  1773. }
  1774. // Exception for admins in case no status is provided in WS call...
  1775. $t_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  1776. $sqladmin = "SELECT user_id FROM $t_admin WHERE user_id = ".intval($user_id);
  1777. $resadmin = Database::query($sqladmin);
  1778. $is_admin = Database::num_rows($resadmin);
  1779. if (empty($status)) {
  1780. $status = 5;
  1781. }
  1782. if ($is_admin) {
  1783. $status = 1;
  1784. }
  1785. if (!empty($expiration_date)) {
  1786. $expiration_date = new DateTime($expiration_date);
  1787. $user->setExpirationDate($expiration_date);
  1788. }
  1789. if (!empty($language)) {
  1790. $user->setLanguage($language);
  1791. }
  1792. $user
  1793. ->setEmail($email)
  1794. ->setStatus($status)
  1795. ->setOfficialCode($official_code)
  1796. ->setPhone($phone)
  1797. ->setPictureUri($picture_uri)
  1798. ->setHrDeptId($hr_dept_id)
  1799. ->setActive(true);
  1800. if (!is_null($creator_id)) {
  1801. $user->setCreatorId($creator_id);
  1802. }
  1803. $userManager->updateUser($user, true);
  1804. if (is_array($extra_list) && count($extra_list) > 0) {
  1805. foreach ($extra_list as $extra) {
  1806. $extra_field_name = $extra['field_name'];
  1807. $extra_field_value = $extra['field_value'];
  1808. // Save the external system's id into user_field_value table.
  1809. UserManager::update_extra_field_value(
  1810. $user_id,
  1811. $extra_field_name,
  1812. $extra_field_value
  1813. );
  1814. }
  1815. }
  1816. return $user_id;
  1817. }
  1818. /* Register WSEditUserWithPicture function */
  1819. // Register the data structures used by the service
  1820. $server->wsdl->addComplexType(
  1821. 'editUserWithPicture',
  1822. 'complexType',
  1823. 'struct',
  1824. 'all',
  1825. '',
  1826. [
  1827. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  1828. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  1829. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  1830. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  1831. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  1832. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  1833. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  1834. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  1835. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  1836. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  1837. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  1838. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  1839. 'picture_url' => ['name' => 'picture_url', 'type' => 'xsd:string'],
  1840. ]
  1841. );
  1842. // Register the method to expose
  1843. $server->register(
  1844. 'WSEditUserWithPicture', // method name
  1845. ['editUserWithPicture' => 'tns:editUserWithPicture'], // input parameters
  1846. ['return' => 'xsd:string'], // output parameters
  1847. 'urn:WSRegistration', // namespace
  1848. 'urn:WSRegistration#WSEditUserWithPicture', // soapaction
  1849. 'rpc', // style
  1850. 'encoded', // use
  1851. 'This service edits a user from wiener' // documentation
  1852. );
  1853. // Define the method WSEditUserWithPicture
  1854. function WSEditUserWithPicture($params)
  1855. {
  1856. if (!WSHelperVerifyKey($params)) {
  1857. return returnError(WS_ERROR_SECRET_KEY);
  1858. }
  1859. $userManager = UserManager::getManager();
  1860. $userRepository = UserManager::getRepository();
  1861. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1862. $original_user_id_value = $params['original_user_id_value'];
  1863. $original_user_id_name = $params['original_user_id_name'];
  1864. $firstname = $params['firstname'];
  1865. $lastname = $params['lastname'];
  1866. $username = $params['username'];
  1867. $password = null;
  1868. $auth_source = null;
  1869. $email = $params['email'];
  1870. $expiration_date = null;
  1871. $status = $params['status'];
  1872. $phone = $params['phone'];
  1873. $picture_url = $params['picture_url'];
  1874. $pictureUri = '';
  1875. $creator_id = null;
  1876. $hr_dept_id = 0;
  1877. $extra = null;
  1878. $extra_list = $params['extra'];
  1879. if (!empty($params['expiration_date'])) {
  1880. $expiration_date = $params['expiration_date'];
  1881. }
  1882. if (!empty($params['password'])) {
  1883. $password = $params['password'];
  1884. }
  1885. // Get user id from external id
  1886. $user_id = UserManager::get_user_id_from_original_id(
  1887. $original_user_id_value,
  1888. $original_user_id_name
  1889. );
  1890. // Get picture and generate uri.
  1891. $filename = basename($picture_url);
  1892. $tempDir = api_get_path(SYS_ARCHIVE_PATH);
  1893. // Make sure the file download was OK by checking the HTTP headers for OK
  1894. if (strpos(get_headers($picture_url)[0], "OK")) {
  1895. file_put_contents($tempDir.$filename, file_get_contents($picture_url));
  1896. $pictureUri = UserManager::update_user_picture($user_id, $filename, $tempDir.$filename);
  1897. }
  1898. if ($user_id == 0) {
  1899. return 0;
  1900. } else {
  1901. $sql = "SELECT id FROM $table_user WHERE id =$user_id AND active= 0";
  1902. $resu = Database::query($sql);
  1903. $r_check_user = Database::fetch_row($resu);
  1904. if (!empty($r_check_user[0])) {
  1905. return 0;
  1906. }
  1907. }
  1908. // Check whether username already exits.
  1909. $sql = "SELECT username FROM $table_user
  1910. WHERE username = '$username' AND id <> $user_id";
  1911. $res_un = Database::query($sql);
  1912. $r_username = Database::fetch_row($res_un);
  1913. if (!empty($r_username[0])) {
  1914. return 0;
  1915. }
  1916. /** @var User $user */
  1917. $user = $userRepository->find($user_id);
  1918. if (!empty($lastname)) {
  1919. $user->setLastname($lastname);
  1920. }
  1921. if (!empty($firstname)) {
  1922. $user->setFirstname($firstname);
  1923. }
  1924. $user->setUsername($username);
  1925. if (!is_null($password)) {
  1926. $user->setPlainPassword($password);
  1927. }
  1928. if (!is_null($auth_source)) {
  1929. $user->setAuthSource($auth_source);
  1930. }
  1931. // Exception for admins in case no status is provided in WS call...
  1932. $t_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  1933. $sqladmin = "SELECT user_id FROM $t_admin WHERE user_id = ".intval($user_id);
  1934. $resadmin = Database::query($sqladmin);
  1935. $is_admin = Database::num_rows($resadmin);
  1936. if (empty($status)) {
  1937. $status = $user->getStatus();
  1938. }
  1939. if ($is_admin) {
  1940. $status = 1;
  1941. }
  1942. if (!empty($expiration_date)) {
  1943. $expiration_date = new DateTime($expiration_date);
  1944. }
  1945. $user
  1946. ->setEmail($email)
  1947. ->setStatus($status)
  1948. ->setPhone($phone)
  1949. ->setExpirationDate($expiration_date)
  1950. ->setHrDeptId($hr_dept_id)
  1951. ->setActive(true)
  1952. ->setPictureUri($pictureUri);
  1953. if (!is_null($creator_id)) {
  1954. $user->setCreatorId($creator_id);
  1955. }
  1956. $userManager->updateUser($user, true);
  1957. if (is_array($extra_list) && count($extra_list) > 0) {
  1958. foreach ($extra_list as $extra) {
  1959. $extra_field_name = $extra['field_name'];
  1960. $extra_field_value = $extra['field_value'];
  1961. // Save the external system's id into user_field_value table.
  1962. UserManager::update_extra_field_value(
  1963. $user_id,
  1964. $extra_field_name,
  1965. $extra_field_value
  1966. );
  1967. }
  1968. }
  1969. return $user_id;
  1970. }
  1971. /* Register WSEditUsersPasswordCrypted function */
  1972. // Register the data structures used by the service
  1973. $server->wsdl->addComplexType(
  1974. 'editUsersPasswordCryptedParams',
  1975. 'complexType',
  1976. 'struct',
  1977. 'all',
  1978. '',
  1979. [
  1980. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  1981. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  1982. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  1983. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  1984. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  1985. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  1986. 'encrypt_method' => ['name' => 'encrypt_method', 'type' => 'xsd:string'],
  1987. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  1988. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  1989. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  1990. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  1991. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  1992. ]
  1993. );
  1994. $server->wsdl->addComplexType(
  1995. 'editUsersPasswordCryptedParamsList',
  1996. 'complexType',
  1997. 'array',
  1998. '',
  1999. 'SOAP-ENC:Array',
  2000. [],
  2001. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:editUsersPasswordCryptedParams[]']],
  2002. 'tns:editUsersPasswordCryptedParams'
  2003. );
  2004. $server->wsdl->addComplexType(
  2005. 'editUsersPasswordCrypted',
  2006. 'complexType',
  2007. 'struct',
  2008. 'all',
  2009. '',
  2010. [
  2011. 'users' => ['name' => 'users', 'type' => 'tns:editUsersPasswordCryptedParamsList'],
  2012. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  2013. ]
  2014. );
  2015. // Prepare output params, in this case will return an array
  2016. $server->wsdl->addComplexType(
  2017. 'result_editUsersPasswordCrypted',
  2018. 'complexType',
  2019. 'struct',
  2020. 'all',
  2021. '',
  2022. [
  2023. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  2024. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  2025. ]
  2026. );
  2027. $server->wsdl->addComplexType(
  2028. 'results_editUsersPasswordCrypted',
  2029. 'complexType',
  2030. 'array',
  2031. '',
  2032. 'SOAP-ENC:Array',
  2033. [],
  2034. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_editUsersPasswordCrypted[]']],
  2035. 'tns:result_editUsersPasswordCrypted'
  2036. );
  2037. // Register the method to expose
  2038. $server->register(
  2039. 'WSEditUsersPasswordCrypted', // method name
  2040. ['editUsersPasswordCrypted' => 'tns:editUsersPasswordCrypted'], // input parameters
  2041. ['return' => 'tns:results_editUsersPasswordCrypted'], // output parameters
  2042. 'urn:WSRegistration', // namespace
  2043. 'urn:WSRegistration#WSEditUsersPasswordCrypted', // soapaction
  2044. 'rpc', // style
  2045. 'encoded', // use
  2046. 'This service edits a user' // documentation
  2047. );
  2048. // Define the method WSEditUsersPasswordCrypted
  2049. function WSEditUsersPasswordCrypted($params)
  2050. {
  2051. global $_configuration;
  2052. if (!WSHelperVerifyKey($params)) {
  2053. return returnError(WS_ERROR_SECRET_KEY);
  2054. }
  2055. // get user id from id of remote system
  2056. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2057. $users_params = $params['users'];
  2058. $results = [];
  2059. $orig_user_id_value = [];
  2060. foreach ($users_params as $user_param) {
  2061. $original_user_id_value = $user_param['original_user_id_value'];
  2062. $original_user_id_name = $user_param['original_user_id_name'];
  2063. $orig_user_id_value[] = $original_user_id_value;
  2064. $firstname = $user_param['firstname'];
  2065. $lastname = $user_param['lastname'];
  2066. $username = $user_param['username'];
  2067. $password = null;
  2068. $auth_source = null;
  2069. $email = $user_param['email'];
  2070. $status = $user_param['status'];
  2071. $official_code = '';
  2072. $phone = $user_param['phone'];
  2073. $picture_uri = '';
  2074. $expiration_date = $user_param['expiration_date'];
  2075. $active = 1;
  2076. $creator_id = null;
  2077. $hr_dept_id = 0;
  2078. $extra = null;
  2079. $extra_list = $user_param['extra'];
  2080. if (!empty($user_param['password']) && !empty($user_param['encrypt_method'])) {
  2081. $password = $user_param['password'];
  2082. $encrypt_method = $user_param['encrypt_method'];
  2083. if ($_configuration['password_encryption'] === $encrypt_method) {
  2084. if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
  2085. $msg = "Encryption $encrypt_method is invalid";
  2086. $results[] = $msg;
  2087. continue;
  2088. } elseif ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
  2089. $msg = "Encryption $encrypt_method is invalid";
  2090. $results[] = $msg;
  2091. continue;
  2092. }
  2093. } else {
  2094. $msg = "This encryption $encrypt_method is not configured";
  2095. $results[] = $msg;
  2096. continue;
  2097. }
  2098. } elseif (!empty($user_param['password']) && empty($user_param['encrypt_method'])) {
  2099. $msg = "If password is not empty the encrypt_method param is required ";
  2100. $results[] = $msg;
  2101. continue;
  2102. } elseif (empty($user_param['password']) && !empty($user_param['encrypt_method'])) {
  2103. $msg = "If encrypt_method is not empty the password param is required ";
  2104. $results[] = $msg;
  2105. continue;
  2106. }
  2107. $user_id = UserManager::get_user_id_from_original_id(
  2108. $original_user_id_value,
  2109. $original_user_id_name
  2110. );
  2111. if ($user_id == 0) {
  2112. $results[] = 0; // Original_user_id_value doesn't exist.
  2113. continue;
  2114. } else {
  2115. $sql = "SELECT user_id FROM $table_user
  2116. WHERE user_id ='$user_id' AND active= '0'";
  2117. $resu = Database::query($sql);
  2118. $r_check_user = Database::fetch_row($resu);
  2119. if (!empty($r_check_user[0])) {
  2120. $results[] = 0; // user_id is not active
  2121. continue;
  2122. }
  2123. }
  2124. // Check if username already exits.
  2125. $sql = "SELECT username FROM $table_user
  2126. WHERE username ='$username' AND user_id <> '$user_id'";
  2127. $res_un = Database::query($sql);
  2128. $r_username = Database::fetch_row($res_un);
  2129. if (!empty($r_username[0])) {
  2130. $results[] = 0;
  2131. continue; // username already exits
  2132. }
  2133. $sql = "UPDATE $table_user SET ";
  2134. if (!empty($lastname)) {
  2135. $sql .= " lastname='".Database::escape_string($lastname)."', ";
  2136. }
  2137. if (!empty($firstname)) {
  2138. $sql .= " firstname='".Database::escape_string($firstname)."', ";
  2139. }
  2140. $sql .= " username='".Database::escape_string($username)."',";
  2141. if (!is_null($password)) {
  2142. $sql .= " password='".Database::escape_string($password)."',";
  2143. }
  2144. if (!is_null($auth_source)) {
  2145. $sql .= " auth_source='".Database::escape_string($auth_source)."',";
  2146. }
  2147. // Exception for admins in case no status is provided in WS call...
  2148. $t_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  2149. $sqladmin = "SELECT user_id FROM $t_admin WHERE user_id = ".intval($user_id);
  2150. $resadmin = Database::query($sqladmin);
  2151. $is_admin = Database::num_rows($resadmin);
  2152. if (empty($status)) {
  2153. $status = 5;
  2154. }
  2155. if ($is_admin) {
  2156. $status = 1;
  2157. }
  2158. $sql .= "
  2159. email='".Database::escape_string($email)."',
  2160. status='".Database::escape_string($status)."',
  2161. official_code='".Database::escape_string($official_code)."',
  2162. phone='".Database::escape_string($phone)."',
  2163. picture_uri='".Database::escape_string($picture_uri)."',
  2164. expiration_date='".Database::escape_string($expiration_date)."',
  2165. active='".Database::escape_string($active)."',
  2166. hr_dept_id=".intval($hr_dept_id);
  2167. if (!is_null($creator_id)) {
  2168. $sql .= ", creator_id='".Database::escape_string($creator_id)."'";
  2169. }
  2170. $sql .= " WHERE user_id='$user_id'";
  2171. $return = @Database::query($sql);
  2172. if (is_array($extra_list) && count($extra_list) > 0) {
  2173. foreach ($extra_list as $extra) {
  2174. $extra_field_name = $extra['field_name'];
  2175. $extra_field_value = $extra['field_value'];
  2176. // Save the external system's id into user_field_value table.
  2177. UserManager::update_extra_field_value(
  2178. $user_id,
  2179. $extra_field_name,
  2180. $extra_field_value
  2181. );
  2182. }
  2183. }
  2184. $results[] = $return;
  2185. continue;
  2186. } //end principal foreach
  2187. $count_results = count($results);
  2188. $output = [];
  2189. for ($i = 0; $i < $count_results; $i++) {
  2190. $output[] = [
  2191. 'original_user_id_value' => $orig_user_id_value[$i],
  2192. 'result' => $results[$i],
  2193. ];
  2194. }
  2195. return $output;
  2196. }
  2197. /* Register WSEditUserPasswordCrypted function */
  2198. // Register the data structures used by the service
  2199. $server->wsdl->addComplexType(
  2200. 'editUserPasswordCrypted',
  2201. 'complexType',
  2202. 'struct',
  2203. 'all',
  2204. '',
  2205. [
  2206. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  2207. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  2208. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  2209. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  2210. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  2211. 'password' => ['name' => 'password', 'type' => 'xsd:string'],
  2212. 'encrypt_method' => ['name' => 'encrypt_method', 'type' => 'xsd:string'],
  2213. 'email' => ['name' => 'email', 'type' => 'xsd:string'],
  2214. 'status' => ['name' => 'status', 'type' => 'xsd:string'],
  2215. 'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
  2216. 'expiration_date' => ['name' => 'expiration_date', 'type' => 'xsd:string'],
  2217. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  2218. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  2219. ]
  2220. );
  2221. // Register the method to expose
  2222. $server->register(
  2223. 'WSEditUserPasswordCrypted', // method name
  2224. ['editUserPasswordCrypted' => 'tns:editUserPasswordCrypted'], // input parameters
  2225. ['return' => 'xsd:string'], // output parameters
  2226. 'urn:WSRegistration', // namespace
  2227. 'urn:WSRegistration#WSEditUserPasswordCrypted', // soapaction
  2228. 'rpc', // style
  2229. 'encoded', // use
  2230. 'This service edits a user' // documentation
  2231. );
  2232. // Define the method WSEditUserPasswordCrypted
  2233. function WSEditUserPasswordCrypted($params)
  2234. {
  2235. global $_configuration, $debug;
  2236. if (!WSHelperVerifyKey($params)) {
  2237. return returnError(WS_ERROR_SECRET_KEY);
  2238. }
  2239. if ($debug) {
  2240. error_log('WSEditUserPasswordCrypted');
  2241. }
  2242. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2243. $original_user_id_value = $params['original_user_id_value'];
  2244. $original_user_id_name = $params['original_user_id_name'];
  2245. $firstname = isset($params['firstname']) ? $params['firstname'] : '';
  2246. $lastname = isset($params['lastname']) ? $params['lastname'] : '';
  2247. $username = isset($params['username']) ? $params['username'] : '';
  2248. $password = null;
  2249. $auth_source = null;
  2250. $email = isset($params['email']) ? $params['email'] : '';
  2251. $status = isset($params['status']) ? $params['status'] : '';
  2252. $official_code = '';
  2253. $phone = isset($params['phone']) ? $params['phone'] : '';
  2254. $picture_uri = '';
  2255. $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : '';
  2256. $active = 1;
  2257. $creator_id = null;
  2258. $hr_dept_id = 0;
  2259. $extra = null;
  2260. $extra_list = isset($params['extra']) ? $params['extra'] : '';
  2261. $params['password'] = isset($params['password']) ? $params['password'] : '';
  2262. $params['encrypt_method'] = isset($params['encrypt_method']) ? $params['encrypt_method'] : '';
  2263. if (!empty($params['password']) && !empty($params['encrypt_method'])) {
  2264. $password = $params['password'];
  2265. $encrypt_method = $params['encrypt_method'];
  2266. if ($_configuration['password_encryption'] === $encrypt_method) {
  2267. if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
  2268. $msg = "Encryption $encrypt_method is invalid";
  2269. return $msg;
  2270. } elseif ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
  2271. $msg = "Encryption $encrypt_method is invalid";
  2272. return $msg;
  2273. }
  2274. } else {
  2275. $msg = "This encryption $encrypt_method is not configured";
  2276. return $msg;
  2277. }
  2278. } elseif (!empty($params['password']) && empty($params['encrypt_method'])) {
  2279. $msg = "If password is not empty the encrypt_method param is required ";
  2280. return $msg;
  2281. } elseif (empty($params['password']) && !empty($params['encrypt_method'])) {
  2282. $msg = "If encrypt_method is not empty the password param is required ";
  2283. return $msg;
  2284. }
  2285. $user_id = UserManager::get_user_id_from_original_id(
  2286. $original_user_id_value,
  2287. $original_user_id_name
  2288. );
  2289. if ($debug) {
  2290. error_log("user: $user_id");
  2291. }
  2292. if ($user_id == 0) {
  2293. return 0;
  2294. } else {
  2295. $sql = "SELECT user_id FROM $table_user
  2296. WHERE user_id ='$user_id' AND active= '0'";
  2297. $resu = Database::query($sql);
  2298. $r_check_user = Database::fetch_row($resu);
  2299. if (!empty($r_check_user[0])) {
  2300. return 0;
  2301. }
  2302. }
  2303. // Check whether username already exits.
  2304. $sql = "SELECT username FROM $table_user
  2305. WHERE username ='$username' AND user_id <> '$user_id'";
  2306. $res_un = Database::query($sql);
  2307. $r_username = Database::fetch_row($res_un);
  2308. if (!empty($r_username[0])) {
  2309. return 0;
  2310. }
  2311. // Edit lastname and firstname only if not empty
  2312. $sql = "UPDATE $table_user SET ";
  2313. if (!empty($lastname)) {
  2314. $sql .= " lastname='".Database::escape_string($lastname)."', ";
  2315. }
  2316. if (!empty($firstname)) {
  2317. $sql .= " firstname='".Database::escape_string($firstname)."', ";
  2318. }
  2319. $sql .= " username='".Database::escape_string($username)."',";
  2320. if (!empty($password)) {
  2321. $sql .= " password='".Database::escape_string($password)."',";
  2322. }
  2323. if (!empty($auth_source)) {
  2324. $sql .= " auth_source='".Database::escape_string($auth_source)."',";
  2325. }
  2326. // Exception for admins in case no status is provided in WS call...
  2327. $t_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  2328. $sqladmin = "SELECT user_id FROM $t_admin WHERE user_id = ".intval($user_id);
  2329. $resadmin = Database::query($sqladmin);
  2330. $is_admin = Database::num_rows($resadmin);
  2331. if (empty($status)) {
  2332. $status = 5;
  2333. }
  2334. if ($is_admin) {
  2335. $status = 1;
  2336. }
  2337. $sql .= "
  2338. email='".Database::escape_string($email)."',
  2339. status='".Database::escape_string($status)."',
  2340. official_code='".Database::escape_string($official_code)."',
  2341. phone='".Database::escape_string($phone)."',
  2342. picture_uri='".Database::escape_string($picture_uri)."',
  2343. expiration_date='".Database::escape_string($expiration_date)."',
  2344. active='".Database::escape_string($active)."',
  2345. hr_dept_id=".intval($hr_dept_id);
  2346. if (!is_null($creator_id)) {
  2347. $sql .= ", creator_id='".Database::escape_string($creator_id)."'";
  2348. }
  2349. $sql .= " WHERE user_id='$user_id'";
  2350. $return = @Database::query($sql);
  2351. if ($debug) {
  2352. error_log("SQL: $sql");
  2353. }
  2354. if (is_array($extra_list) && count($extra_list) > 0) {
  2355. foreach ($extra_list as $extra) {
  2356. $extra_field_name = $extra['field_name'];
  2357. $extra_field_value = $extra['field_value'];
  2358. // save the external system's id into user_field_value table'
  2359. UserManager::update_extra_field_value(
  2360. $user_id,
  2361. $extra_field_name,
  2362. $extra_field_value
  2363. );
  2364. }
  2365. }
  2366. if ($return) {
  2367. return 1;
  2368. }
  2369. return 0;
  2370. }
  2371. // Prepare output params for actions on users (delete, disable, enable), will return an array
  2372. $server->wsdl->addComplexType(
  2373. 'result_actionUsers',
  2374. 'complexType',
  2375. 'struct',
  2376. 'all',
  2377. '',
  2378. [
  2379. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  2380. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  2381. ]
  2382. );
  2383. $server->wsdl->addComplexType(
  2384. 'results_actionUsers',
  2385. 'complexType',
  2386. 'array',
  2387. '',
  2388. 'SOAP-ENC:Array',
  2389. [],
  2390. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_actionUsers[]']],
  2391. 'tns:result_actionUsers'
  2392. );
  2393. /** WSDeleteUsers */
  2394. $server->wsdl->addComplexType(
  2395. 'user_id',
  2396. 'complexType',
  2397. 'struct',
  2398. 'all',
  2399. '',
  2400. [
  2401. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  2402. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  2403. ]
  2404. );
  2405. $server->wsdl->addComplexType(
  2406. 'user_ids_array',
  2407. 'complexType',
  2408. 'array',
  2409. '',
  2410. 'SOAP-ENC:Array',
  2411. [],
  2412. [
  2413. [
  2414. 'ref' => 'SOAP-ENC:arrayType',
  2415. 'wsdl:arrayType' => 'tns:user_id[]',
  2416. ],
  2417. ],
  2418. 'tns:user_id'
  2419. );
  2420. $server->wsdl->addComplexType(
  2421. 'user_ids',
  2422. 'complexType',
  2423. 'struct',
  2424. 'all',
  2425. '',
  2426. [
  2427. 'ids' => ['name' => 'user_ids', 'type' => 'tns:user_ids_array'],
  2428. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  2429. ]
  2430. );
  2431. function WSHelperActionOnUsers($params, $type)
  2432. {
  2433. $debug = 1;
  2434. if ($debug) {
  2435. error_log("WSHelperActionOnUsers");
  2436. error_log(print_r($params, 1));
  2437. }
  2438. if (!WSHelperVerifyKey($params)) {
  2439. return returnError(WS_ERROR_SECRET_KEY);
  2440. }
  2441. $results = [];
  2442. $orig_user_id_value = [];
  2443. $original_user_ids = $params['ids'];
  2444. foreach ($original_user_ids as $original_user_id) {
  2445. $result = false;
  2446. $orig_user_id_value[] = $original_user_id['original_user_id_value'];
  2447. $user_id = UserManager::get_user_id_from_original_id(
  2448. $original_user_id['original_user_id_value'],
  2449. $original_user_id['original_user_id_name']
  2450. );
  2451. if ($user_id > 0) {
  2452. if ($debug) {
  2453. error_log("User found: $user_id");
  2454. }
  2455. if ($type == 'delete') {
  2456. $result = UserManager::delete_user($user_id);
  2457. } elseif ($type == "disable") {
  2458. $result = UserManager::disable($user_id);
  2459. } elseif ($type == "enable") {
  2460. $result = UserManager::enable($user_id);
  2461. }
  2462. } else {
  2463. if ($debug) {
  2464. error_log("User id not found: $user_id");
  2465. }
  2466. }
  2467. $results[] = $result ? 1 : 0;
  2468. }
  2469. $count_results = count($results);
  2470. $output = [];
  2471. for ($i = 0; $i < $count_results; $i++) {
  2472. $output[] = [
  2473. 'original_user_id_value' => $orig_user_id_value[$i],
  2474. 'result' => $results[$i],
  2475. ];
  2476. }
  2477. return $output;
  2478. }
  2479. $server->register(
  2480. 'WSDeleteUsers', // method name
  2481. ['user_ids' => 'tns:user_ids'], // input parameters
  2482. ['return' => 'tns:results_actionUsers'], // output parameters
  2483. 'urn:WSRegistration', // namespace
  2484. 'urn:WSRegistration#WSDeleteUsers', // soapaction
  2485. 'rpc', // style
  2486. 'encoded', // use
  2487. 'Deletes users provided as parameters from the system' // documentation
  2488. );
  2489. function WSDeleteUsers($params)
  2490. {
  2491. return WSHelperActionOnUsers($params, 'delete');
  2492. }
  2493. /** WSDisableUsers */
  2494. $server->register(
  2495. 'WSDisableUsers', // method name
  2496. ['user_ids' => 'tns:user_ids'], // input parameters
  2497. ['return' => 'tns:results_actionUsers'], // output parameters
  2498. 'urn:WSRegistration', // namespace
  2499. 'urn:WSRegistration#WSDisableUsers', // soapaction
  2500. 'rpc', // style
  2501. 'encoded', // use
  2502. 'Disables users provided as parameters from the system' // documentation
  2503. );
  2504. function WSDisableUsers($params)
  2505. {
  2506. return WSHelperActionOnUsers($params, "disable");
  2507. }
  2508. /** WSEnableUsers */
  2509. $server->register(
  2510. 'WSEnableUsers', // method name
  2511. ['user_ids' => 'tns:user_ids'], // input parameters
  2512. ['return' => 'tns:results_actionUsers'], // output parameters
  2513. 'urn:WSRegistration', // namespace
  2514. 'urn:WSRegistration#WSEnableUsers', // soapaction
  2515. 'rpc', // style
  2516. 'encoded', // use
  2517. 'Enables users provided as parameters' // documentation
  2518. );
  2519. function WSEnableUsers($params)
  2520. {
  2521. return WSHelperActionOnUsers($params, "enable");
  2522. }
  2523. /* Register WSCreateCourse function */
  2524. // Register the data structures used by the service
  2525. $server->wsdl->addComplexType(
  2526. 'course_id',
  2527. 'complexType',
  2528. 'struct',
  2529. 'all',
  2530. '',
  2531. [
  2532. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  2533. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  2534. ]
  2535. );
  2536. $server->wsdl->addComplexType(
  2537. 'createCourseParams',
  2538. 'complexType',
  2539. 'struct',
  2540. 'all',
  2541. '',
  2542. [
  2543. 'title' => ['name' => 'title', 'type' => 'xsd:string'],
  2544. 'category_code' => ['name' => 'category_code', 'type' => 'xsd:string'],
  2545. 'wanted_code' => ['name' => 'wanted_code', 'type' => 'xsd:string'],
  2546. 'tutor_name' => ['name' => 'tutor_name', 'type' => 'xsd:string'],
  2547. 'course_language' => ['name' => 'course_language', 'type' => 'xsd:string'],
  2548. 'disk_quota' => ['name' => 'disk_quota', 'type' => 'xsd:string'], // disk_quota in MB
  2549. 'subscribe' => ['name' => 'subscribe', 'type' => 'xsd:string'],
  2550. 'unsubscribe' => ['name' => 'unsubscribe', 'type' => 'xsd:string'],
  2551. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  2552. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  2553. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  2554. ]
  2555. );
  2556. $server->wsdl->addComplexType(
  2557. 'createCourseParamsList',
  2558. 'complexType',
  2559. 'array',
  2560. '',
  2561. 'SOAP-ENC:Array',
  2562. [],
  2563. [
  2564. [
  2565. 'ref' => 'SOAP-ENC:arrayType',
  2566. 'wsdl:arrayType' => 'tns:createCourseParams[]',
  2567. ],
  2568. ],
  2569. 'tns:createCourseParams'
  2570. );
  2571. // Register the data structures used by the service
  2572. $server->wsdl->addComplexType(
  2573. 'createCourse',
  2574. 'complexType',
  2575. 'struct',
  2576. 'all',
  2577. '',
  2578. [
  2579. 'courses' => ['name' => 'courses', 'type' => 'tns:createCourseParamsList'],
  2580. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  2581. ]
  2582. );
  2583. // Prepare output params, in this case will return an array
  2584. $server->wsdl->addComplexType(
  2585. 'result_createCourse',
  2586. 'complexType',
  2587. 'struct',
  2588. 'all',
  2589. '',
  2590. [
  2591. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  2592. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  2593. ]
  2594. );
  2595. $server->wsdl->addComplexType(
  2596. 'results_createCourse',
  2597. 'complexType',
  2598. 'array',
  2599. '',
  2600. 'SOAP-ENC:Array',
  2601. [],
  2602. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_createCourse[]']],
  2603. 'tns:result_createCourse'
  2604. );
  2605. // Register the method to expose
  2606. $server->register(
  2607. 'WSCreateCourse', // method name
  2608. ['createCourse' => 'tns:createCourse'], // input parameters
  2609. ['return' => 'tns:results_createCourse'], // output parameters
  2610. 'urn:WSRegistration', // namespace
  2611. 'urn:WSRegistration#WSCreateCourse', // soapaction
  2612. 'rpc', // style
  2613. 'encoded', // use
  2614. 'This service adds a course' // documentation
  2615. );
  2616. // Define the method WSCreateCourse
  2617. function WSCreateCourse($params)
  2618. {
  2619. if (!WSHelperVerifyKey($params)) {
  2620. return returnError(WS_ERROR_SECRET_KEY);
  2621. }
  2622. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2623. $courses_params = $params['courses'];
  2624. $results = [];
  2625. $sessionAdminId = DEFAULT_ADMIN_USER_ID;
  2626. $orig_course_id_value = [];
  2627. foreach ($courses_params as $course_param) {
  2628. $title = $course_param['title'];
  2629. $category_code = isset($course_param['category_code']) ? $course_param['category_code'] : '';
  2630. $wanted_code = $course_param['wanted_code'];
  2631. $tutor_name = isset($course_param['tutor_name']) ? $course_param['tutor_name'] : '';
  2632. $diskQuota = isset($course_param['disk_quota']) ? $course_param['disk_quota'] : '100';
  2633. // Convert to MB
  2634. $diskQuota = $diskQuota * 1024 * 1024;
  2635. $course_language = 'english'; // TODO: A hard-coded value.
  2636. $original_course_id_name = $course_param['original_course_id_name'];
  2637. $original_course_id_value = $course_param['original_course_id_value'];
  2638. $orig_course_id_value[] = $course_param['original_course_id_value'];
  2639. $visibility = null;
  2640. $subscribe = $course_param['subscribe'];
  2641. $unsubscribe = $course_param['unsubscribe'];
  2642. if (isset($course_param['visibility'])) {
  2643. if ($course_param['visibility'] &&
  2644. $course_param['visibility'] >= 0 &&
  2645. $course_param['visibility'] <= 3
  2646. ) {
  2647. $visibility = $course_param['visibility'];
  2648. }
  2649. }
  2650. $extra_list = isset($course_param['extra']) ? $course_param['extra'] : '';
  2651. // Check whether exits $x_course_code into user_field_values table.
  2652. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  2653. $course_param['original_course_id_value'],
  2654. $course_param['original_course_id_name']
  2655. );
  2656. if (!empty($courseInfo)) {
  2657. if ($courseInfo['visibility'] != 0) {
  2658. $sql = "UPDATE $table_course SET
  2659. course_language='".Database::escape_string($course_language)."',
  2660. title='".Database::escape_string($title)."',
  2661. category_code='".Database::escape_string($category_code)."',
  2662. tutor_name='".Database::escape_string($tutor_name)."',
  2663. visual_code='".Database::escape_string($wanted_code)."'";
  2664. if ($visibility !== null) {
  2665. $sql .= ", visibility = '$visibility' ";
  2666. }
  2667. $sql .= " WHERE id='".$courseInfo['real_id']."'";
  2668. Database::query($sql);
  2669. if (is_array($extra_list) && count($extra_list) > 0) {
  2670. foreach ($extra_list as $extra) {
  2671. $extra_field_name = $extra['field_name'];
  2672. $extra_field_value = $extra['field_value'];
  2673. // Save the external system's id into course_field_value table.
  2674. CourseManager::update_course_extra_field_value(
  2675. $courseInfo['code'],
  2676. $extra_field_name,
  2677. $extra_field_value
  2678. );
  2679. }
  2680. }
  2681. $results[] = $courseInfo['code'];
  2682. continue;
  2683. } else {
  2684. $results[] = 0;
  2685. continue; // Original course id already exits.
  2686. }
  2687. }
  2688. if (!empty($course_param['course_language'])) {
  2689. $course_language = $course_param['course_language'];
  2690. }
  2691. $params = [];
  2692. $params['title'] = $title;
  2693. $params['wanted_code'] = $wanted_code;
  2694. $params['category_code'] = $category_code;
  2695. $params['course_category'] = $category_code;
  2696. $params['tutor_name'] = $tutor_name;
  2697. $params['course_language'] = $course_language;
  2698. $params['user_id'] = $sessionAdminId;
  2699. $params['visibility'] = $visibility;
  2700. $params['disk_quota'] = $diskQuota;
  2701. if (isset($subscribe) && $subscribe != '') { // Valid values: 0, 1
  2702. $params['subscribe'] = $subscribe;
  2703. }
  2704. if (isset($unsubscribe) && $subscribe != '') { // Valid values: 0, 1
  2705. $params['unsubscribe'] = $unsubscribe;
  2706. }
  2707. $course_info = CourseManager::create_course($params, $sessionAdminId);
  2708. if (!empty($course_info)) {
  2709. $course_code = $course_info['code'];
  2710. // Save new field label into course_field table
  2711. CourseManager::create_course_extra_field(
  2712. $original_course_id_name,
  2713. 1,
  2714. $original_course_id_name,
  2715. ''
  2716. );
  2717. // Save the external system's id into user_field_value table.
  2718. CourseManager::update_course_extra_field_value(
  2719. $course_code,
  2720. $original_course_id_name,
  2721. $original_course_id_value
  2722. );
  2723. if (is_array($extra_list) && count($extra_list) > 0) {
  2724. foreach ($extra_list as $extra) {
  2725. $extra_field_name = $extra['field_name'];
  2726. $extra_field_value = $extra['field_value'];
  2727. // Save new fieldlabel into course_field table.
  2728. CourseManager::create_course_extra_field(
  2729. $extra_field_name,
  2730. 1,
  2731. $extra_field_name,
  2732. ''
  2733. );
  2734. // Save the external system's id into course_field_value table.
  2735. CourseManager::update_course_extra_field_value(
  2736. $course_code,
  2737. $extra_field_name,
  2738. $extra_field_value
  2739. );
  2740. }
  2741. }
  2742. $results[] = $course_code;
  2743. } else {
  2744. $results[] = 0;
  2745. }
  2746. } // end principal foreach
  2747. $count_results = count($results);
  2748. $output = [];
  2749. for ($i = 0; $i < $count_results; $i++) {
  2750. $output[] = [
  2751. 'original_course_id_value' => $orig_course_id_value[$i],
  2752. 'result' => $results[$i],
  2753. ];
  2754. }
  2755. return $output;
  2756. }
  2757. /* Register WSCreateCourseByTitle function */
  2758. // Register the data structures used by the service
  2759. $server->wsdl->addComplexType(
  2760. 'createCourseByTitleParams',
  2761. 'complexType',
  2762. 'struct',
  2763. 'all',
  2764. '',
  2765. [
  2766. 'title' => ['name' => 'title', 'type' => 'xsd:string'],
  2767. 'tutor_name' => ['name' => 'tutor_name', 'type' => 'xsd:string'],
  2768. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  2769. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  2770. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  2771. ]
  2772. );
  2773. $server->wsdl->addComplexType(
  2774. 'createCourseByTitleParamsList',
  2775. 'complexType',
  2776. 'array',
  2777. '',
  2778. 'SOAP-ENC:Array',
  2779. [],
  2780. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:createCourseByTitleParams[]']],
  2781. 'tns:createCourseByTitleParams'
  2782. );
  2783. // Register the data structures used by the service
  2784. $server->wsdl->addComplexType(
  2785. 'createCourseByTitle',
  2786. 'complexType',
  2787. 'struct',
  2788. 'all',
  2789. '',
  2790. [
  2791. 'courses' => ['name' => 'courses', 'type' => 'tns:createCourseByTitleParamsList'],
  2792. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  2793. ]
  2794. );
  2795. // Prepare output params, in this case will return an array
  2796. $server->wsdl->addComplexType(
  2797. 'result_createCourseByTitle',
  2798. 'complexType',
  2799. 'struct',
  2800. 'all',
  2801. '',
  2802. [
  2803. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  2804. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  2805. ]
  2806. );
  2807. $server->wsdl->addComplexType(
  2808. 'results_createCourseByTitle',
  2809. 'complexType',
  2810. 'array',
  2811. '',
  2812. 'SOAP-ENC:Array',
  2813. [],
  2814. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_createCourseByTitle[]']],
  2815. 'tns:result_createCourseByTitle'
  2816. );
  2817. // Register the method to expose
  2818. $server->register(
  2819. 'WSCreateCourseByTitle', // method name
  2820. ['createCourseByTitle' => 'tns:createCourseByTitle'], // input parameters
  2821. ['return' => 'tns:results_createCourseByTitle'], // output parameters
  2822. 'urn:WSRegistration', // namespace
  2823. 'urn:WSRegistration#WSCreateCourseByTitle', // soapaction
  2824. 'rpc', // style
  2825. 'encoded', // use
  2826. 'This service adds a course by title' // documentation
  2827. );
  2828. // Define the method WSCreateCourseByTitle
  2829. function WSCreateCourseByTitle($params)
  2830. {
  2831. global $_configuration;
  2832. if (!WSHelperVerifyKey($params)) {
  2833. return returnError(WS_ERROR_SECRET_KEY);
  2834. }
  2835. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2836. $sessionAdminId = DEFAULT_ADMIN_USER_ID;
  2837. $courses_params = $params['courses'];
  2838. $results = [];
  2839. $orig_course_id_value = [];
  2840. foreach ($courses_params as $course_param) {
  2841. $title = $course_param['title'];
  2842. $category_code = 'LANG'; // TODO: A hard-coded value.
  2843. $wanted_code = '';
  2844. $tutor_firstname = api_get_setting('administratorName');
  2845. $tutor_lastname = api_get_setting('administratorSurname');
  2846. $course_language = 'spanish'; // TODO: Incorrect default value, it should 'english'.
  2847. if (!empty($course_param['course_language'])) {
  2848. $course_language = $course_param['course_language'];
  2849. }
  2850. $tutor_name = api_get_person_name($tutor_firstname, $tutor_lastname, null, null, $course_language);
  2851. if (!empty($course_param['tutor_name'])) {
  2852. $tutor_name = $course_param['tutor_name'];
  2853. }
  2854. $original_course_id_name = $course_param['original_course_id_name'];
  2855. $original_course_id_value = $course_param['original_course_id_value'];
  2856. $orig_course_id_value[] = $course_param['original_course_id_value'];
  2857. $extra_list = $course_param['extra'];
  2858. // Ensure the database prefix + database name do not get over 40 characters
  2859. $maxlength = 40;
  2860. if (empty($wanted_code)) {
  2861. $wanted_code = CourseManager::generate_course_code(substr($title, 0, $maxlength));
  2862. }
  2863. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  2864. $original_course_id_value,
  2865. $original_course_id_name
  2866. );
  2867. if (!empty($courseInfo)) {
  2868. if ($courseInfo['visibility'] != 0) {
  2869. $sql = "UPDATE $table_course SET
  2870. course_language='".Database::escape_string($course_language)."',
  2871. title='".Database::escape_string($title)."',
  2872. category_code='".Database::escape_string($category_code)."',
  2873. tutor_name='".Database::escape_string($tutor_name)."',
  2874. visual_code='".Database::escape_string($wanted_code)."',
  2875. visibility = '3'
  2876. WHERE id ='".$courseInfo['real_id']."'";
  2877. Database::query($sql);
  2878. $results[] = $courseInfo['real_id'];
  2879. continue;
  2880. } else {
  2881. $results[] = 0;
  2882. continue;
  2883. }
  2884. }
  2885. $values['course_language'] = api_get_setting('platformLanguage');
  2886. $keys = AddCourse::define_course_keys($wanted_code, '', $_configuration['db_prefix']);
  2887. $sql_check = sprintf('SELECT * FROM '.$table_course.' WHERE visual_code = "%s"', Database :: escape_string($wanted_code));
  2888. $result_check = Database::query($sql_check); // I don't know why this api function doesn't work...
  2889. if (Database::num_rows($result_check) < 1) {
  2890. $params = [];
  2891. $params['title'] = $title;
  2892. $params['wanted_code'] = $wanted_code;
  2893. $params['category_code'] = $category_code;
  2894. $params['tutor_name'] = $tutor_name;
  2895. $params['course_language'] = $course_language;
  2896. $params['user_id'] = $sessionAdminId;
  2897. $course_info = CourseManager::create_course($params, $sessionAdminId);
  2898. if (!empty($course_info)) {
  2899. $course_code = $course_info['code'];
  2900. // Save new fieldlabel into course_field table.
  2901. CourseManager::create_course_extra_field(
  2902. $original_course_id_name,
  2903. 1,
  2904. $original_course_id_name,
  2905. ''
  2906. );
  2907. // Save the external system's id into user_field_value table.
  2908. CourseManager::update_course_extra_field_value(
  2909. $course_code,
  2910. $original_course_id_name,
  2911. $original_course_id_value
  2912. );
  2913. if (is_array($extra_list) && count($extra_list) > 0) {
  2914. foreach ($extra_list as $extra) {
  2915. $extra_field_name = $extra['field_name'];
  2916. $extra_field_value = $extra['field_value'];
  2917. // Save new fieldlabel into course_field table.
  2918. CourseManager::create_course_extra_field(
  2919. $extra_field_name,
  2920. 1,
  2921. $extra_field_name,
  2922. ''
  2923. );
  2924. // Save the external system's id into course_field_value table.
  2925. CourseManager::update_course_extra_field_value(
  2926. $course_code,
  2927. $extra_field_name,
  2928. $extra_field_value
  2929. );
  2930. }
  2931. }
  2932. }
  2933. $results[] = $course_code;
  2934. continue;
  2935. } else {
  2936. $results[] = 0;
  2937. continue;
  2938. }
  2939. } // end principal foreach
  2940. $count_results = count($results);
  2941. $output = [];
  2942. for ($i = 0; $i < $count_results; $i++) {
  2943. $output[] = [
  2944. 'original_course_id_value' => $orig_course_id_value[$i],
  2945. 'result' => $results[$i],
  2946. ];
  2947. }
  2948. return $output;
  2949. }
  2950. /* Register WSEditCourse function */
  2951. // Register the data structures used by the service
  2952. $server->wsdl->addComplexType(
  2953. 'editCourseParams',
  2954. 'complexType',
  2955. 'struct',
  2956. 'all',
  2957. '',
  2958. [
  2959. 'tutor_id' => ['name' => 'tutor_id', 'type' => 'xsd:string'],
  2960. 'title' => ['name' => 'title', 'type' => 'xsd:string'],
  2961. 'category_code' => ['name' => 'category_code', 'type' => 'xsd:string'],
  2962. 'department_name' => ['name' => 'department_name', 'type' => 'xsd:string'],
  2963. 'department_url' => ['name' => 'department_url', 'type' => 'xsd:string'],
  2964. 'course_language' => ['name' => 'course_language', 'type' => 'xsd:string'],
  2965. 'visibility' => ['name' => 'visibility', 'type' => 'xsd:string'],
  2966. 'subscribe' => ['name' => 'subscribe', 'type' => 'xsd:string'],
  2967. 'unsubscribe' => ['name' => 'unsubscribe', 'type' => 'xsd:string'],
  2968. 'visual_code' => ['name' => 'visual_code', 'type' => 'xsd:string'],
  2969. 'disk_quota' => ['name' => 'disk_quota', 'type' => 'xsd:string'], // disk_quota in MB
  2970. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  2971. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  2972. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  2973. ]
  2974. );
  2975. $server->wsdl->addComplexType(
  2976. 'editCourseParamsList',
  2977. 'complexType',
  2978. 'array',
  2979. '',
  2980. 'SOAP-ENC:Array',
  2981. [],
  2982. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:editCourseParams[]']],
  2983. 'tns:editCourseParams'
  2984. );
  2985. $server->wsdl->addComplexType(
  2986. 'editCourse',
  2987. 'complexType',
  2988. 'struct',
  2989. 'all',
  2990. '',
  2991. [
  2992. 'courses' => ['name' => 'courses', 'type' => 'tns:editCourseParamsList'],
  2993. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  2994. ]
  2995. );
  2996. // Prepare output params, in this case will return an array
  2997. $server->wsdl->addComplexType(
  2998. 'result_editCourse',
  2999. 'complexType',
  3000. 'struct',
  3001. 'all',
  3002. '',
  3003. [
  3004. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  3005. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  3006. ]
  3007. );
  3008. $server->wsdl->addComplexType(
  3009. 'results_editCourse',
  3010. 'complexType',
  3011. 'array',
  3012. '',
  3013. 'SOAP-ENC:Array',
  3014. [],
  3015. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_editCourse[]']],
  3016. 'tns:result_editCourse'
  3017. );
  3018. // Register the method to expose
  3019. $server->register(
  3020. 'WSEditCourse', // method name
  3021. ['editCourse' => 'tns:editCourse'], // input parameters
  3022. ['return' => 'tns:results_editCourse'], // output parameters
  3023. 'urn:WSRegistration', // namespace
  3024. 'urn:WSRegistration#WSEditCourse', // soapaction
  3025. 'rpc', // style
  3026. 'encoded', // use
  3027. 'This service edits a course' // documentation
  3028. );
  3029. // Define the method WSEditCourse
  3030. function WSEditCourse($params)
  3031. {
  3032. global $_configuration;
  3033. if (!WSHelperVerifyKey($params)) {
  3034. return returnError(WS_ERROR_SECRET_KEY);
  3035. }
  3036. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  3037. $courses_params = $params['courses'];
  3038. $results = [];
  3039. $orig_course_id_value = [];
  3040. foreach ($courses_params as $course_param) {
  3041. $tutor_id = isset($course_param['tutor_id']) ? $course_param['tutor_id'] : '';
  3042. $title = $course_param['title'];
  3043. $category_code = isset($course_param['category_code']) ? $course_param['category_code'] : '';
  3044. $department_name = isset($course_param['department_name']) ? $course_param['department_name'] : '';
  3045. $department_url = isset($course_param['department_url']) ? $course_param['department_url'] : '';
  3046. $course_language = $course_param['course_language'];
  3047. $visibility = $course_param['visibility'];
  3048. $subscribe = $course_param['subscribe'];
  3049. $unsubscribe = $course_param['unsubscribe'];
  3050. $visual_code = $course_param['visual_code'];
  3051. $diskQuota = isset($course_param['disk_quota']) ? $course_param['disk_quota'] : '100';
  3052. // Convert to MB
  3053. $diskQuota = $diskQuota * 1024 * 1024;
  3054. $original_course_id_name = $course_param['original_course_id_name'];
  3055. $original_course_id_value = $course_param['original_course_id_value'];
  3056. $orig_course_id_value[] = $original_course_id_value;
  3057. $extra_list = isset($course_param['extra']) ? $course_param['extra'] : null;
  3058. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  3059. $original_course_id_value,
  3060. $original_course_id_name
  3061. );
  3062. if (empty($courseInfo)) {
  3063. $results[] = 0; // Original_course_id_value doesn't exist.
  3064. continue;
  3065. }
  3066. $course_code = $courseInfo['code'];
  3067. $courseId = $courseInfo['real_id'];
  3068. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  3069. $sql = "SELECT concat(lastname,'',firstname) as tutor_name
  3070. FROM $table_user WHERE status='1' AND user_id = '$tutor_id'
  3071. ORDER BY lastname,firstname";
  3072. $res = Database::query($sql);
  3073. $tutor_name = Database::fetch_row($res);
  3074. $dbnamelength = strlen($_configuration['db_prefix']);
  3075. $maxlength = 40 - $dbnamelength;
  3076. if (empty($visual_code)) {
  3077. $visual_code = CourseManager::generate_course_code(substr($title, 0, $maxlength));
  3078. }
  3079. $tutor_name = $tutor_name[0];
  3080. $sql = "UPDATE $course_table SET
  3081. course_language='".Database::escape_string($course_language)."',
  3082. title='".Database::escape_string($title)."',
  3083. category_code='".Database::escape_string($category_code)."',
  3084. tutor_name='".Database::escape_string($tutor_name)."',
  3085. visual_code='".Database::escape_string($visual_code)."',
  3086. department_name='".Database::escape_string($department_name)."',
  3087. department_url='".Database::escape_string($department_url)."',
  3088. visibility = '".Database::escape_string($visibility)."',
  3089. subscribe = '".Database::escape_string($subscribe)."',
  3090. disk_quota='".Database::escape_string($diskQuota)."',
  3091. unsubscribe='".Database::escape_string($unsubscribe)."'
  3092. WHERE id ='".Database::escape_string($courseId)."'";
  3093. $res = Database::query($sql);
  3094. if (is_array($extra_list) && count($extra_list) > 0) {
  3095. foreach ($extra_list as $extra) {
  3096. $extra_field_name = $extra['field_name'];
  3097. $extra_field_value = $extra['field_value'];
  3098. // Save the external system's id into course_field_value table.
  3099. $res = CourseManager::update_course_extra_field_value(
  3100. $course_code,
  3101. $extra_field_name,
  3102. $extra_field_value
  3103. );
  3104. }
  3105. }
  3106. if ($res) {
  3107. $results[] = 1;
  3108. continue;
  3109. } else {
  3110. $results[] = 0;
  3111. continue;
  3112. }
  3113. } // end principal foreach
  3114. $count_results = count($results);
  3115. $output = [];
  3116. for ($i = 0; $i < $count_results; $i++) {
  3117. $output[] = [
  3118. 'original_course_id_value' => $orig_course_id_value[$i],
  3119. 'result' => $results[$i],
  3120. ];
  3121. }
  3122. return $output;
  3123. }
  3124. /* Register WSCourseDescription function */
  3125. // Register the data structures used by the service
  3126. $server->wsdl->addComplexType(
  3127. 'courseDescription',
  3128. 'complexType',
  3129. 'struct',
  3130. 'all',
  3131. '',
  3132. [
  3133. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  3134. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  3135. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  3136. ]
  3137. );
  3138. // Prepare output params, in this case will return an array
  3139. $server->wsdl->addComplexType(
  3140. 'fields_course_desc',
  3141. 'complexType',
  3142. 'struct',
  3143. 'all',
  3144. '',
  3145. [
  3146. 'course_desc_id' => ['name' => 'course_desc_id', 'type' => 'xsd:string'],
  3147. 'course_desc_default_title' => ['name' => 'course_desc_default_title', 'type' => 'xsd:string'],
  3148. 'course_desc_title' => ['name' => 'course_desc_title', 'type' => 'xsd:string'],
  3149. 'course_desc_content' => ['name' => 'course_desc_content', 'type' => 'xsd:string'],
  3150. ]
  3151. );
  3152. $server->wsdl->addComplexType(
  3153. 'fields_course_desc_list',
  3154. 'complexType',
  3155. 'array',
  3156. '',
  3157. 'SOAP-ENC:Array',
  3158. [],
  3159. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:fields_course_desc[]']],
  3160. 'tns:fields_course_desc'
  3161. );
  3162. // Register the method to expose
  3163. $server->register(
  3164. 'WSCourseDescription', // method name
  3165. ['courseDescription' => 'tns:courseDescription'], // input parameters
  3166. ['return' => 'tns:fields_course_desc_list'], // output parameters
  3167. 'urn:WSRegistration', // namespace
  3168. 'urn:WSRegistration#WSCourseDescription', // soapaction
  3169. 'rpc', // style
  3170. 'encoded', // use
  3171. 'This service edits a course description' // documentation
  3172. );
  3173. // Define the method WSCourseDescription
  3174. function WSCourseDescription($params)
  3175. {
  3176. if (!WSHelperVerifyKey($params)) {
  3177. return returnError(WS_ERROR_SECRET_KEY);
  3178. }
  3179. $array_course_desc_id = [];
  3180. $array_course_desc_title = [];
  3181. $array_course_desc_content = [];
  3182. $original_course_id_name = $params['original_course_id_name'];
  3183. $original_course_id_value = $params['original_course_id_value'];
  3184. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  3185. $original_course_id_value,
  3186. $original_course_id_name
  3187. );
  3188. if (empty($courseInfo) || (isset($courseInfo) && $courseInfo['visibility'] == 0)) {
  3189. return 0; // Original_course_id_value doesn't exist.
  3190. }
  3191. $t_course_desc = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  3192. $sql = "SELECT * FROM $t_course_desc WHERE c_id = {$courseInfo['real_id']} ";
  3193. $result = Database::query($sql);
  3194. $default_titles = [
  3195. get_lang('GeneralDescription'),
  3196. get_lang('Objectives'),
  3197. get_lang('Topics'),
  3198. get_lang('Methodology'),
  3199. get_lang('CourseMaterial'),
  3200. get_lang('HumanAndTechnicalResources'),
  3201. get_lang('Assessment'),
  3202. get_lang('AddCategory'),
  3203. ];
  3204. for ($x = 1; $x < 9; $x++) {
  3205. $array_course_desc_id[$x] = $x;
  3206. $array_course_desc_default_title[$x] = $default_titles[$x - 1];
  3207. $array_course_desc_title[$x] = '';
  3208. $array_course_desc_content[$x] = '';
  3209. }
  3210. while ($row = Database::fetch_array($result)) {
  3211. $ind = (int) $row['id'];
  3212. $array_course_desc_title[$ind] = $row['title'];
  3213. $array_course_desc_content[$ind] = $row['content'];
  3214. }
  3215. $count_results = count($default_titles);
  3216. $output = [];
  3217. for ($i = 1; $i <= $count_results; $i++) {
  3218. $output[] = [
  3219. 'course_desc_id' => $array_course_desc_id[$i],
  3220. 'course_desc_default_title' => $array_course_desc_default_title[$i],
  3221. 'course_desc_title' => $array_course_desc_title[$i],
  3222. 'course_desc_content' => $array_course_desc_content[$i],
  3223. ];
  3224. }
  3225. return $output;
  3226. }
  3227. /* Register WSEditCourseDescription function */
  3228. // Register the data structures used by the service
  3229. $server->wsdl->addComplexType(
  3230. 'editCourseDescriptionParams',
  3231. 'complexType',
  3232. 'struct',
  3233. 'all',
  3234. '',
  3235. [
  3236. 'course_desc_id' => ['name' => 'course_desc_id', 'type' => 'xsd:string'],
  3237. 'course_desc_title' => ['name' => 'course_desc_title', 'type' => 'xsd:string'],
  3238. 'course_desc_content' => ['name' => 'course_desc_content', 'type' => 'xsd:string'],
  3239. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  3240. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  3241. ]
  3242. );
  3243. $server->wsdl->addComplexType(
  3244. 'editCourseDescriptionParamsList',
  3245. 'complexType',
  3246. 'array',
  3247. '',
  3248. 'SOAP-ENC:Array',
  3249. [],
  3250. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:editCourseDescriptionParams[]']],
  3251. 'tns:editCourseDescriptionParams'
  3252. );
  3253. $server->wsdl->addComplexType(
  3254. 'editCourseDescription',
  3255. 'complexType',
  3256. 'struct',
  3257. 'all',
  3258. '',
  3259. [
  3260. 'course_desc' => ['name' => 'course_desc', 'type' => 'tns:editCourseDescriptionParamsList'],
  3261. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  3262. ]
  3263. );
  3264. // Prepare output params, in this case will return an array
  3265. $server->wsdl->addComplexType(
  3266. 'result_editCourseDescription',
  3267. 'complexType',
  3268. 'struct',
  3269. 'all',
  3270. '',
  3271. [
  3272. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  3273. 'course_desc_id' => ['name' => 'course_desc_id', 'type' => 'xsd:string'],
  3274. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  3275. ]
  3276. );
  3277. $server->wsdl->addComplexType(
  3278. 'results_editCourseDescription',
  3279. 'complexType',
  3280. 'array',
  3281. '',
  3282. 'SOAP-ENC:Array',
  3283. [],
  3284. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_editCourseDescription[]']],
  3285. 'tns:result_editCourseDescription'
  3286. );
  3287. // Register the method to expose
  3288. $server->register(
  3289. 'WSEditCourseDescription', // method name
  3290. ['editCourseDescription' => 'tns:editCourseDescription'], // input parameters
  3291. ['return' => 'tns:results_editCourseDescription'], // output parameters
  3292. 'urn:WSRegistration', // namespace
  3293. 'urn:WSRegistration#WSEditCourseDescription', // soapaction
  3294. 'rpc', // style
  3295. 'encoded', // use
  3296. 'This service edits a course description' // documentation
  3297. );
  3298. // Define the method WSEditCourseDescription
  3299. function WSEditCourseDescription($params)
  3300. {
  3301. if (!WSHelperVerifyKey($params)) {
  3302. return -1;
  3303. }
  3304. $courses_params = $params['course_desc'];
  3305. $results = [];
  3306. $orig_course_id_value = [];
  3307. $course_description_id = [];
  3308. $courseDescription = new CourseDescription();
  3309. $defaultDescTitle = $courseDescription->get_default_description_title();
  3310. foreach ($courses_params as $course_param) {
  3311. $original_course_id_name = $course_param['original_course_id_name'];
  3312. $original_course_id_value = $course_param['original_course_id_value'];
  3313. $course_desc_id = $course_param['course_desc_id'];
  3314. $course_desc_title = $course_param['course_desc_title'];
  3315. $course_desc_content = $course_param['course_desc_content'];
  3316. $orig_course_id_value[] = $original_course_id_value;
  3317. $course_description_id[] = $course_desc_id;
  3318. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  3319. $original_course_id_value,
  3320. $original_course_id_name
  3321. );
  3322. if (empty($courseInfo) || (isset($courseInfo) && $courseInfo['visibility'] == 0)) {
  3323. $results[] = 0;
  3324. continue; // Original_course_id_value doesn't exist.
  3325. }
  3326. $course_desc_id = Database::escape_string($course_desc_id);
  3327. $course_desc_title = Database::escape_string($course_desc_title);
  3328. $course_desc_content = Database::escape_string($course_desc_content);
  3329. $course_desc_id = (int) $course_desc_id;
  3330. if ($course_desc_id > 8 && $course_desc_id < 1) {
  3331. $results[] = 0; // course_desc_id invalid.
  3332. continue;
  3333. }
  3334. // if title is empty set default title instead
  3335. if (empty($course_desc_title)) {
  3336. $course_desc_title = $defaultDescTitle[$course_desc_id];
  3337. }
  3338. $courseId = $courseInfo['real_id'];
  3339. $courseDescription->set_id(null);
  3340. $courseDescription->set_course_id($courseId);
  3341. $courseDescription->set_session_id(0);
  3342. $courseDescription->set_title($course_desc_title);
  3343. $courseDescription->set_content($course_desc_content);
  3344. $courseDescription->set_description_type($course_desc_id);
  3345. $data = $courseDescription->get_data_by_description_type($course_desc_id, $courseId);
  3346. if ($data) {
  3347. // Update existing description
  3348. $courseDescription->set_id($data['id']);
  3349. $result = $courseDescription->update();
  3350. } else {
  3351. // Insert new description
  3352. $result = $courseDescription->insert();
  3353. }
  3354. $results[] = $result ? 1 : 0;
  3355. } // end principal foreach
  3356. $count_results = count($results);
  3357. $output = [];
  3358. for ($i = 0; $i < $count_results; $i++) {
  3359. $output[] = [
  3360. 'original_course_id_value' => $orig_course_id_value[$i],
  3361. 'course_desc_id' => $course_description_id[$i],
  3362. 'result' => $results[$i],
  3363. ];
  3364. }
  3365. return $output;
  3366. }
  3367. /* Register WSDeleteCourse function */
  3368. // Register the data structures used by the service
  3369. $server->wsdl->addComplexType(
  3370. 'deleteCourseParams',
  3371. 'complexType',
  3372. 'struct',
  3373. 'all',
  3374. '',
  3375. [
  3376. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  3377. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  3378. ]
  3379. );
  3380. $server->wsdl->addComplexType(
  3381. 'deleteCourseParamsList',
  3382. 'complexType',
  3383. 'array',
  3384. '',
  3385. 'SOAP-ENC:Array',
  3386. [],
  3387. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:deleteCourseParams[]']],
  3388. 'tns:deleteCourseParams'
  3389. );
  3390. // Register the data structures used by the service.
  3391. $server->wsdl->addComplexType(
  3392. 'deleteCourse',
  3393. 'complexType',
  3394. 'struct',
  3395. 'all',
  3396. '',
  3397. [
  3398. 'courses' => ['name' => 'courses', 'type' => 'tns:deleteCourseParamsList'],
  3399. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  3400. ]
  3401. );
  3402. // Prepare output params, in this case will return an array.
  3403. $server->wsdl->addComplexType(
  3404. 'result_deleteCourse',
  3405. 'complexType',
  3406. 'struct',
  3407. 'all',
  3408. '',
  3409. [
  3410. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  3411. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  3412. ]
  3413. );
  3414. $server->wsdl->addComplexType(
  3415. 'results_deleteCourse',
  3416. 'complexType',
  3417. 'array',
  3418. '',
  3419. 'SOAP-ENC:Array',
  3420. [],
  3421. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_deleteCourse[]']],
  3422. 'tns:result_deleteCourse'
  3423. );
  3424. $server->register(
  3425. 'WSDeleteCourse', // method name
  3426. ['deleteCourse' => 'tns:deleteCourse'], // input parameters
  3427. ['return' => 'tns:results_deleteCourse'], // output parameters
  3428. 'urn:WSRegistration', // namespace
  3429. 'urn:WSRegistration#WSDeleteCourse', // soapaction
  3430. 'rpc', // style
  3431. 'encoded', // use
  3432. 'This service deletes a course ' // documentation
  3433. );
  3434. // Define the method WSDeleteCourse
  3435. function WSDeleteCourse($params)
  3436. {
  3437. if (!WSHelperVerifyKey($params)) {
  3438. return returnError(WS_ERROR_SECRET_KEY);
  3439. }
  3440. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  3441. $courses_params = $params['courses'];
  3442. $results = [];
  3443. $orig_course_id_value = [];
  3444. foreach ($courses_params as $course_param) {
  3445. $original_course_id_value = $course_param['original_course_id_value'];
  3446. $original_course_id_name = $course_param['original_course_id_name'];
  3447. $orig_course_id_value[] = $original_course_id_value;
  3448. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  3449. $original_course_id_value,
  3450. $original_course_id_name
  3451. );
  3452. if (empty($courseInfo) || (isset($courseInfo) && $courseInfo['visibility'] == 0)) {
  3453. $results[] = 0;
  3454. continue; // Original_course_id_value doesn't exist.
  3455. }
  3456. $courseId = $courseInfo['real_id'];
  3457. $sql = "UPDATE $table_course SET visibility = '0' WHERE id = '$courseId'";
  3458. $return = Database::query($sql);
  3459. $results[] = $return;
  3460. }
  3461. $count_results = count($results);
  3462. $output = [];
  3463. for ($i = 0; $i < $count_results; $i++) {
  3464. $output[] = [
  3465. 'original_course_id_value' => $orig_course_id_value[$i],
  3466. 'result' => $results[$i],
  3467. ];
  3468. }
  3469. return $output;
  3470. }
  3471. /* Register WSCreateSession function */
  3472. // Register data structures used by the service.
  3473. $server->wsdl->addComplexType(
  3474. 'createSessionParam',
  3475. 'complexType',
  3476. 'struct',
  3477. 'all',
  3478. '',
  3479. [
  3480. 'name' => ['name' => 'name', 'type' => 'xsd:string'],
  3481. 'year_start' => ['name' => 'year_start', 'type' => 'xsd:string'],
  3482. 'month_start' => ['name' => 'month_start', 'type' => 'xsd:string'],
  3483. 'day_start' => ['name' => 'day_start', 'type' => 'xsd:string'],
  3484. 'year_end' => ['name' => 'year_end', 'type' => 'xsd:string'],
  3485. 'month_end' => ['name' => 'month_end', 'type' => 'xsd:string'],
  3486. 'day_end' => ['name' => 'day_end', 'type' => 'xsd:string'],
  3487. 'nb_days_access_before' => ['name' => 'nb_days_access_before', 'type' => 'xsd:string'],
  3488. 'nb_days_access_after' => ['name' => 'nb_days_access_after', 'type' => 'xsd:string'],
  3489. 'nolimit' => ['name' => 'nolimit', 'type' => 'xsd:string'],
  3490. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  3491. 'duration' => ['name' => 'duration', 'type' => 'xsd:string'],
  3492. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  3493. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  3494. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  3495. ]
  3496. );
  3497. $server->wsdl->addComplexType(
  3498. 'createSessionParamList',
  3499. 'complexType',
  3500. 'array',
  3501. '',
  3502. 'SOAP-ENC:Array',
  3503. [],
  3504. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:createSessionParam[]']],
  3505. 'tns:createSessionParamList'
  3506. );
  3507. // Register the data structures used by the service
  3508. $server->wsdl->addComplexType(
  3509. 'createSession',
  3510. 'complexType',
  3511. 'struct',
  3512. 'all',
  3513. '',
  3514. [
  3515. 'sessions' => ['name' => 'sessions', 'type' => 'tns:createSessionParamList'],
  3516. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  3517. ]
  3518. );
  3519. // Prepare output params, in this case will return an array
  3520. $server->wsdl->addComplexType(
  3521. 'result_createSession',
  3522. 'complexType',
  3523. 'struct',
  3524. 'all',
  3525. '',
  3526. [
  3527. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  3528. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  3529. ]
  3530. );
  3531. $server->wsdl->addComplexType(
  3532. 'results_createSession',
  3533. 'complexType',
  3534. 'array',
  3535. '',
  3536. 'SOAP-ENC:Array',
  3537. [],
  3538. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_createSession[]']],
  3539. 'tns:result_createSession'
  3540. );
  3541. // Register the method to expose
  3542. $server->register(
  3543. 'WSCreateSession', // method name
  3544. ['createSession' => 'tns:createSession'], // input parameters
  3545. ['return' => 'tns:results_createSession'], // output parameters
  3546. 'urn:WSRegistration', // namespace
  3547. 'urn:WSRegistration#WSCreateSession', // soapaction
  3548. 'rpc', // style
  3549. 'encoded', // use
  3550. 'This service edits a session' // documentation
  3551. );
  3552. // define the method WSCreateSession
  3553. function WSCreateSession($params)
  3554. {
  3555. global $debug;
  3556. $sessionAdminId = DEFAULT_ADMIN_USER_ID;
  3557. if (!WSHelperVerifyKey($params)) {
  3558. return returnError(WS_ERROR_SECRET_KEY);
  3559. }
  3560. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  3561. error_log(print_r($params, 1));
  3562. $sessions_params = $params['sessions'];
  3563. $results = [];
  3564. $orig_session_id_value = [];
  3565. foreach ($sessions_params as $session_param) {
  3566. $name = trim($session_param['name']);
  3567. $year_start = intval($session_param['year_start']);
  3568. $month_start = intval($session_param['month_start']);
  3569. $day_start = intval($session_param['day_start']);
  3570. $year_end = intval($session_param['year_end']);
  3571. $month_end = intval($session_param['month_end']);
  3572. $day_end = intval($session_param['day_end']);
  3573. $nb_days_access_before = intval($session_param['nb_days_access_before']);
  3574. $nb_days_access_after = intval($session_param['nb_days_access_after']);
  3575. $id_coach = $session_param['user_id'];
  3576. $nolimit = $session_param['nolimit'];
  3577. $duration = $session_param['duration'];
  3578. $original_session_id_name = $session_param['original_session_id_name'];
  3579. $original_session_id_value = $session_param['original_session_id_value'];
  3580. $orig_session_id_value[] = $session_param['original_session_id_value'];
  3581. $extra_list = isset($session_param['extra']) ? $session_param['extra'] : '';
  3582. $sessionId = SessionManager::getSessionIdFromOriginalId(
  3583. $original_session_id_value,
  3584. $original_session_id_name
  3585. );
  3586. if (!empty($sessionId)) {
  3587. if ($debug) {
  3588. error_log("session with external session id '$original_session_id_value' with '$name' exists");
  3589. }
  3590. $results[] = 0;
  3591. continue;
  3592. }
  3593. if (empty($nolimit)) {
  3594. $date_start = "$year_start-".(($month_start < 10) ? "0$month_start" : $month_start)."-".(($day_start < 10) ? "0$day_start" : $day_start).' 00:00:00';
  3595. $date_end = "$year_end-".(($month_end < 10) ? "0$month_end" : $month_end)."-".(($day_end < 10) ? "0$day_end" : $day_end).' 23:59:59';
  3596. } else {
  3597. $date_start = "";
  3598. $date_end = "";
  3599. }
  3600. if (empty($name)) {
  3601. if ($debug) {
  3602. error_log("session has no name");
  3603. }
  3604. $results[] = 0;
  3605. continue;
  3606. } elseif (empty($id_coach)) {
  3607. $results[] = 0;
  3608. if ($debug) {
  3609. error_log("Coach id must not be empty");
  3610. }
  3611. continue;
  3612. } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start))) {
  3613. if ($debug) {
  3614. error_log("There's an error with the start date: $month_start - $day_start - $year_start");
  3615. }
  3616. $results[] = 0;
  3617. continue;
  3618. } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end))) {
  3619. $results[] = 0;
  3620. if ($debug) {
  3621. error_log("There's an error with the end date: $month_end - $day_end - $year_end");
  3622. }
  3623. continue;
  3624. } elseif (empty($nolimit) && $date_start >= $date_end) {
  3625. $results[] = 0;
  3626. if ($debug) {
  3627. error_log("There's an error with the start and end date");
  3628. }
  3629. continue;
  3630. } else {
  3631. $rs = Database::query("SELECT 1 FROM $tbl_session WHERE name='".addslashes($name)."'");
  3632. if (Database::num_rows($rs)) {
  3633. if ($debug) {
  3634. error_log("Session with name '$name' already exists");
  3635. }
  3636. $results[] = 0;
  3637. continue;
  3638. } else {
  3639. $coachStartDate = '';
  3640. if ($date_start) {
  3641. $startDate = new DateTime($date_start);
  3642. $diffStart = new DateInterval("P".$nb_days_access_before."D");
  3643. $coachStartDate = $startDate->sub($diffStart);
  3644. $coachStartDate = $coachStartDate->format('Y-m-d H:i:s');
  3645. }
  3646. $coachEndDate = '';
  3647. if ($date_end) {
  3648. $endDate = new DateTime($date_end);
  3649. $diffEnd = new DateInterval("P".$nb_days_access_after."D");
  3650. $coachEndDate = $endDate->add($diffEnd);
  3651. $coachEndDate = $coachEndDate->format('Y-m-d H:i:s');
  3652. }
  3653. $id_session = SessionManager::create_session(
  3654. $name,
  3655. $date_start,
  3656. $date_end,
  3657. $date_start,
  3658. $date_end,
  3659. $coachStartDate,
  3660. $coachEndDate,
  3661. $id_coach,
  3662. 0,
  3663. 1,
  3664. false,
  3665. $duration,
  3666. null,
  3667. 0,
  3668. [],
  3669. $sessionAdminId
  3670. );
  3671. if ($id_session) {
  3672. if ($debug) {
  3673. error_log("Session created '$id_session' ");
  3674. }
  3675. // Save new field label into course_field table.
  3676. SessionManager::create_session_extra_field(
  3677. $original_session_id_name,
  3678. 1,
  3679. $original_session_id_name
  3680. );
  3681. // Save the external system's id into user_field_value table.
  3682. SessionManager::update_session_extra_field_value(
  3683. $id_session,
  3684. $original_session_id_name,
  3685. $original_session_id_value
  3686. );
  3687. if (is_array($extra_list) && count($extra_list) > 0) {
  3688. foreach ($extra_list as $extra) {
  3689. $extra_field_name = $extra['field_name'];
  3690. $extra_field_value = $extra['field_value'];
  3691. // Save new fieldlabel into course_field table.
  3692. SessionManager::create_session_extra_field(
  3693. $extra_field_name,
  3694. 1,
  3695. $extra_field_name
  3696. );
  3697. // Save the external system's id into course_field_value table.
  3698. SessionManager::update_session_extra_field_value(
  3699. $id_session,
  3700. $extra_field_name,
  3701. $extra_field_value
  3702. );
  3703. }
  3704. }
  3705. $results[] = $id_session;
  3706. } else {
  3707. if ($debug) {
  3708. error_log("There was an error when trying to save session with name $name");
  3709. }
  3710. }
  3711. }
  3712. }
  3713. } // end principal foreach
  3714. $count_results = count($results);
  3715. $output = [];
  3716. for ($i = 0; $i < $count_results; $i++) {
  3717. $output[] = [
  3718. 'original_session_id_value' => $orig_session_id_value[$i],
  3719. 'result' => $results[$i],
  3720. ];
  3721. }
  3722. return $output;
  3723. }
  3724. /* Register WSEditSession function */
  3725. // Register the data structures used by the service
  3726. $server->wsdl->addComplexType(
  3727. 'editSessionParams',
  3728. 'complexType',
  3729. 'struct',
  3730. 'all',
  3731. '',
  3732. [
  3733. 'name' => ['name' => 'name', 'type' => 'xsd:string'],
  3734. 'year_start' => ['name' => 'year_start', 'type' => 'xsd:string'],
  3735. 'month_start' => ['name' => 'month_start', 'type' => 'xsd:string'],
  3736. 'day_start' => ['name' => 'day_start', 'type' => 'xsd:string'],
  3737. 'year_end' => ['name' => 'year_end', 'type' => 'xsd:string'],
  3738. 'month_end' => ['name' => 'month_end', 'type' => 'xsd:string'],
  3739. 'day_end' => ['name' => 'day_end', 'type' => 'xsd:string'],
  3740. 'nb_days_access_before' => ['name' => 'nb_days_access_before', 'type' => 'xsd:string'],
  3741. 'nb_days_access_after' => ['name' => 'nb_days_access_after', 'type' => 'xsd:string'],
  3742. 'nolimit' => ['name' => 'nolimit', 'type' => 'xsd:string'],
  3743. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  3744. 'duration' => ['name' => 'duration', 'type' => 'xsd:string'],
  3745. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  3746. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  3747. 'extra' => ['name' => 'extra', 'type' => 'tns:extrasList'],
  3748. ]
  3749. );
  3750. $server->wsdl->addComplexType(
  3751. 'editSessionParamsList',
  3752. 'complexType',
  3753. 'array',
  3754. '',
  3755. 'SOAP-ENC:Array',
  3756. [],
  3757. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:editSessionParams[]']],
  3758. 'tns:editSessionParams'
  3759. );
  3760. $server->wsdl->addComplexType(
  3761. 'editSession',
  3762. 'complexType',
  3763. 'struct',
  3764. 'all',
  3765. '',
  3766. [
  3767. 'sessions' => ['name' => 'sessions', 'type' => 'tns:editSessionParamsList'],
  3768. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  3769. ]
  3770. );
  3771. // Prepare output params, in this case will return an array
  3772. $server->wsdl->addComplexType(
  3773. 'result_editSession',
  3774. 'complexType',
  3775. 'struct',
  3776. 'all',
  3777. '',
  3778. [
  3779. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  3780. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  3781. ]
  3782. );
  3783. $server->wsdl->addComplexType(
  3784. 'results_editSession',
  3785. 'complexType',
  3786. 'array',
  3787. '',
  3788. 'SOAP-ENC:Array',
  3789. [],
  3790. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_editSession[]']],
  3791. 'tns:result_editSession'
  3792. );
  3793. // Register the method to expose
  3794. $server->register(
  3795. 'WSEditSession', // method name
  3796. ['editSession' => 'tns:editSession'], // input parameters
  3797. ['return' => 'tns:results_editSession'], // output parameters
  3798. 'urn:WSRegistration', // namespace
  3799. 'urn:WSRegistration#WSEditSession', // soapaction
  3800. 'rpc', // style
  3801. 'encoded', // use
  3802. 'This service edits a session' // documentation
  3803. );
  3804. // define the method WSEditSession
  3805. function WSEditSession($params)
  3806. {
  3807. if (!WSHelperVerifyKey($params)) {
  3808. return returnError(WS_ERROR_SECRET_KEY);
  3809. }
  3810. $sessions_params = $params['sessions'];
  3811. $results = [];
  3812. $orig_session_id_value = [];
  3813. foreach ($sessions_params as $session_param) {
  3814. $name = trim($session_param['name']);
  3815. $year_start = intval($session_param['year_start']);
  3816. $month_start = intval($session_param['month_start']);
  3817. $day_start = intval($session_param['day_start']);
  3818. $year_end = intval($session_param['year_end']);
  3819. $month_end = intval($session_param['month_end']);
  3820. $day_end = intval($session_param['day_end']);
  3821. $nb_days_access_before = intval($session_param['nb_days_access_before']);
  3822. $nb_days_access_after = intval($session_param['nb_days_access_after']);
  3823. $original_session_id_value = $session_param['original_session_id_value'];
  3824. $original_session_id_name = $session_param['original_session_id_name'];
  3825. $orig_session_id_value[] = $original_session_id_value;
  3826. $coach_username = $session_param['coach_username'];
  3827. $nolimit = $session_param['nolimit'];
  3828. $id_coach = $session_param['user_id'];
  3829. $duration = intval($session_param['duration']);
  3830. $extra_list = $session_param['extra'];
  3831. $id = SessionManager::getSessionIdFromOriginalId(
  3832. $original_session_id_value,
  3833. $original_session_id_name
  3834. );
  3835. if (empty($id)) {
  3836. $results[] = 0;
  3837. continue;
  3838. }
  3839. if (empty($nolimit)) {
  3840. $date_start = "$year_start-".(($month_start < 10) ? "0$month_start" : $month_start)."-".(($day_start < 10) ? "0$day_start" : $day_start).' 00:00:00';
  3841. $date_end = "$year_end-".(($month_end < 10) ? "0$month_end" : $month_end)."-".(($day_end < 10) ? "0$day_end" : $day_end).' 23:59:59';
  3842. } else {
  3843. $date_start = '';
  3844. $date_end = '';
  3845. }
  3846. if (empty($name)) {
  3847. $results[] = 0; //SessionNameIsRequired
  3848. continue;
  3849. } elseif (empty($id_coach)) { // Session must have coach
  3850. $results[] = 0;
  3851. continue;
  3852. } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start))) {
  3853. $results[] = 0; //InvalidStartDate
  3854. continue;
  3855. } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end))) {
  3856. $results[] = 0; //InvalidEndDate
  3857. continue;
  3858. } elseif (empty($nolimit) && $date_start >= $date_end) {
  3859. $results[] = 0; //StartDateShouldBeBeforeEndDate
  3860. continue;
  3861. } else {
  3862. $coachStartDate = '';
  3863. if ($date_start) {
  3864. $startDate = new DateTime($date_start);
  3865. $diffStart = new DateInterval("P".$nb_days_access_before."D");
  3866. $coachStartDate = $startDate->sub($diffStart);
  3867. $coachStartDate = $coachStartDate->format('Y-m-d H:i:s');
  3868. }
  3869. $coachEndDate = '';
  3870. if ($date_end) {
  3871. $endDate = new DateTime($date_end);
  3872. $diffEnd = new DateInterval("P".$nb_days_access_after."D");
  3873. $coachEndDate = $endDate->add($diffEnd);
  3874. $coachEndDate = $coachEndDate->format('Y-m-d H:i:s');
  3875. }
  3876. $sessionInfo = api_get_session_info($id);
  3877. $editResult = SessionManager::edit_session(
  3878. $id,
  3879. $name,
  3880. $date_start,
  3881. $date_end,
  3882. $date_start,
  3883. $date_end,
  3884. $coachStartDate,
  3885. $coachEndDate,
  3886. $id_coach,
  3887. $sessionInfo['session_category_id'],
  3888. $sessionInfo['visibility'],
  3889. $sessionInfo['description'],
  3890. $sessionInfo['show_description'],
  3891. $duration,
  3892. null,
  3893. DEFAULT_ADMIN_USER_ID
  3894. );
  3895. if (is_array($extra_list) && count($extra_list) > 0) {
  3896. foreach ($extra_list as $extra) {
  3897. $extra_field_name = $extra['field_name'];
  3898. $extra_field_value = $extra['field_value'];
  3899. // Save the external system's id into session_field_value table.
  3900. SessionManager::update_session_extra_field_value(
  3901. $id,
  3902. $extra_field_name,
  3903. $extra_field_value
  3904. );
  3905. }
  3906. }
  3907. $results[] = $editResult ? 1 : 0;
  3908. continue;
  3909. }
  3910. } // end principal foreach
  3911. $count_results = count($results);
  3912. $output = [];
  3913. for ($i = 0; $i < $count_results; $i++) {
  3914. $output[] = [
  3915. 'original_session_id_value' => $orig_session_id_value[$i],
  3916. 'result' => $results[$i],
  3917. ];
  3918. }
  3919. return $output;
  3920. }
  3921. /* Register WSDeleteSession function */
  3922. $server->wsdl->addComplexType(
  3923. 'deleteSessionParams',
  3924. 'complexType',
  3925. 'struct',
  3926. 'all',
  3927. '',
  3928. [
  3929. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  3930. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  3931. ]
  3932. );
  3933. $server->wsdl->addComplexType(
  3934. 'deleteSessionParamsList',
  3935. 'complexType',
  3936. 'array',
  3937. '',
  3938. 'SOAP-ENC:Array',
  3939. [],
  3940. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:deleteSessionParams[]']],
  3941. 'tns:deleteSessionParams'
  3942. );
  3943. // Register the data structures used by the service
  3944. $server->wsdl->addComplexType(
  3945. 'deleteSession',
  3946. 'complexType',
  3947. 'struct',
  3948. 'all',
  3949. '',
  3950. [
  3951. 'sessions' => ['name' => 'sessions', 'type' => 'tns:deleteSessionParamsList'],
  3952. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  3953. ]
  3954. );
  3955. // Prepare output params, in this case will return an array
  3956. $server->wsdl->addComplexType(
  3957. 'result_deleteSession',
  3958. 'complexType',
  3959. 'struct',
  3960. 'all',
  3961. '',
  3962. [
  3963. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  3964. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  3965. ]
  3966. );
  3967. $server->wsdl->addComplexType(
  3968. 'results_deleteSession',
  3969. 'complexType',
  3970. 'array',
  3971. '',
  3972. 'SOAP-ENC:Array',
  3973. [],
  3974. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_deleteSession[]']],
  3975. 'tns:result_deleteSession'
  3976. );
  3977. $server->register(
  3978. 'WSDeleteSession', // method name
  3979. ['deleteSession' => 'tns:deleteSession'], // input parameters
  3980. ['return' => 'tns:results_deleteSession'], // output parameters
  3981. 'urn:WSRegistration', // namespace
  3982. 'urn:WSRegistration#WSDeleteSession', // soapaction
  3983. 'rpc', // style
  3984. 'encoded', // use
  3985. 'This service deletes a session ' // documentation
  3986. );
  3987. // define the method WSDeleteSession
  3988. function WSDeleteSession($params)
  3989. {
  3990. if (!WSHelperVerifyKey($params)) {
  3991. return returnError(WS_ERROR_SECRET_KEY);
  3992. }
  3993. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  3994. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  3995. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3996. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  3997. $session_params = $params['sessions'];
  3998. $results = [];
  3999. $orig_session_id_value = [];
  4000. foreach ($session_params as $session_param) {
  4001. $original_session_id_value = $session_param['original_session_id_value'];
  4002. $original_session_id_name = $session_param['original_session_id_name'];
  4003. $orig_session_id_value[] = $original_session_id_name;
  4004. $idChecked = SessionManager::getSessionIdFromOriginalId(
  4005. $original_session_id_value,
  4006. $original_session_id_name
  4007. );
  4008. if (empty($idChecked)) {
  4009. $results[] = 0;
  4010. continue;
  4011. }
  4012. $session_ids[] = $idChecked;
  4013. $sql = "DELETE FROM $tbl_session WHERE id = '$idChecked'";
  4014. Database::query($sql);
  4015. $sql = "DELETE FROM $tbl_session_rel_course WHERE session_id = '$idChecked'";
  4016. Database::query($sql);
  4017. $sql = "DELETE FROM $tbl_session_rel_course_rel_user WHERE session_id = '$idChecked'";
  4018. Database::query($sql);
  4019. $sql = "DELETE FROM $tbl_session_rel_user WHERE session_id = '$idChecked'";
  4020. Database::query($sql);
  4021. $results[] = 1;
  4022. continue;
  4023. }
  4024. $extraFieldValue = new ExtraFieldValue('session');
  4025. //delete from table_session_field_value from a given session_id
  4026. foreach ($session_ids as $session_id) {
  4027. $extraFieldValue->deleteValuesByItem($session_id);
  4028. }
  4029. // Preparing output.
  4030. $count_results = count($results);
  4031. $output = [];
  4032. for ($i = 0; $i < $count_results; $i++) {
  4033. $output[] = [
  4034. 'original_session_id_value' => $orig_session_id_value[$i],
  4035. 'result' => $results[$i],
  4036. ];
  4037. }
  4038. return $output;
  4039. }
  4040. /** WSSubscribeUserToCourse */
  4041. // Register the data structures used by the service
  4042. $server->wsdl->addComplexType(
  4043. 'user_course_status',
  4044. 'complexType',
  4045. 'struct',
  4046. 'all',
  4047. '',
  4048. [
  4049. 'course_id' => ['name' => 'course_id', 'type' => 'tns:course_id'],
  4050. 'user_id' => ['name' => 'user_id', 'type' => 'tns:user_id'],
  4051. 'status' => ['name' => 'status', 'type' => 'xsd:int'],
  4052. ]
  4053. );
  4054. $server->wsdl->addComplexType(
  4055. 'subscribeUserToCourse_arg',
  4056. 'complexType',
  4057. 'struct',
  4058. 'all',
  4059. '',
  4060. [
  4061. 'userscourses' => ['name' => 'userscourses', 'type' => 'tns:user_course_status_array'], //removed []
  4062. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4063. ]
  4064. );
  4065. $server->wsdl->addComplexType(
  4066. 'user_course_status_array',
  4067. 'complexType',
  4068. 'array',
  4069. '',
  4070. 'SOAP-ENC:Array',
  4071. [],
  4072. [
  4073. ['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_course_status[]'],
  4074. ],
  4075. 'tns:user_course_status'
  4076. );
  4077. $server->wsdl->addComplexType(
  4078. 'subscribeUserToCourse_return',
  4079. 'complexType',
  4080. 'struct',
  4081. 'all',
  4082. '',
  4083. [
  4084. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  4085. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4086. 'result' => ['name' => 'result', 'type' => 'xsd:int'],
  4087. ]
  4088. );
  4089. $server->wsdl->addComplexType(
  4090. 'subscribeUserToCourse_return_global',
  4091. 'complexType',
  4092. 'array',
  4093. '',
  4094. 'SOAP-ENC:Array',
  4095. [],
  4096. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:subscribeUserToCourse_return[]']],
  4097. 'tns:subscribeUserToCourse_return'
  4098. );
  4099. // Register the method to expose
  4100. $server->register(
  4101. 'WSSubscribeUserToCourse', // method name
  4102. ['subscribeUserToCourse' => 'tns:subscribeUserToCourse_arg'], // input parameters
  4103. ['return' => 'tns:subscribeUserToCourse_return_global'],
  4104. 'urn:WSRegistration', // namespace
  4105. 'urn:WSRegistration#WSSubscribeUserToCourse', // soapaction
  4106. 'rpc', // style
  4107. 'encoded', // use
  4108. 'This service subscribes a user to a course' // documentation
  4109. );
  4110. // define the method WSSubscribeUserToCourse
  4111. function WSSubscribeUserToCourse($params)
  4112. {
  4113. global $debug;
  4114. if (!WSHelperVerifyKey($params)) {
  4115. return returnError(WS_ERROR_SECRET_KEY);
  4116. }
  4117. if ($debug) {
  4118. error_log('WSSubscribeUserToCourse params: '.print_r($params, 1));
  4119. }
  4120. $results = [];
  4121. $userscourses = $params['userscourses'];
  4122. foreach ($userscourses as $usercourse) {
  4123. $original_course_id = $usercourse['course_id'];
  4124. $original_user_id = $usercourse['user_id'];
  4125. $status = STUDENT;
  4126. if ($usercourse['status']) {
  4127. $status = $usercourse['status'];
  4128. }
  4129. $resultValue = 0;
  4130. // Get user id
  4131. $user_id = UserManager::get_user_id_from_original_id(
  4132. $original_user_id['original_user_id_value'],
  4133. $original_user_id['original_user_id_name']
  4134. );
  4135. if ($debug) {
  4136. error_log('WSSubscribeUserToCourse user_id: '.$user_id);
  4137. }
  4138. if ($user_id == 0) {
  4139. // If user was not found, there was a problem
  4140. $resultValue = 0;
  4141. } else {
  4142. // User was found
  4143. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  4144. $original_course_id['original_course_id_value'],
  4145. $original_course_id['original_course_id_name']
  4146. );
  4147. $courseCode = isset($courseInfo['code']) ? $courseInfo['code'] : '';
  4148. if (empty($courseCode)) {
  4149. if ($debug) {
  4150. error_log('WSSubscribeUserToCourse course not found');
  4151. }
  4152. // Course was not found
  4153. $resultValue = 0;
  4154. } else {
  4155. if ($debug) {
  4156. error_log('WSSubscribeUserToCourse courseCode: '.$courseCode);
  4157. }
  4158. $result = CourseManager::subscribeUser($user_id, $courseCode, $status, 0, 0, false);
  4159. if ($result) {
  4160. $resultValue = 1;
  4161. if ($debug) {
  4162. error_log('WSSubscribeUserToCourse subscribed');
  4163. }
  4164. } else {
  4165. if ($debug) {
  4166. error_log('WSSubscribeUserToCourse NOT subscribed.');
  4167. }
  4168. }
  4169. }
  4170. }
  4171. $results[] = [
  4172. 'original_user_id_value' => $original_user_id['original_user_id_value'],
  4173. 'original_course_id_value' => $original_course_id['original_course_id_value'],
  4174. 'result' => $resultValue,
  4175. ];
  4176. }
  4177. return $results;
  4178. }
  4179. /** WSSubscribeUserToCourse */
  4180. // Register the data structures used by the service
  4181. $server->wsdl->addComplexType(
  4182. 'subscribeUserToCourseSimple_arg',
  4183. 'complexType',
  4184. 'struct',
  4185. 'all',
  4186. '',
  4187. [
  4188. 'course' => ['name' => 'course', 'type' => 'xsd:string'], //Course string code
  4189. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'], //Chamilo user_id
  4190. 'status' => ['name' => 'status', 'type' => 'xsd:int'],
  4191. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4192. ]
  4193. );
  4194. // Prepare output params, in this case will return an array
  4195. $server->wsdl->addComplexType(
  4196. 'Result',
  4197. 'complexType',
  4198. 'struct',
  4199. 'all',
  4200. '',
  4201. ['message' => ['name' => 'message', 'type' => 'xsd:string']]
  4202. );
  4203. // Register the method to expose
  4204. $server->register(
  4205. 'WSSubscribeUserToCourseSimple', // method name
  4206. ['subscribeUserToCourseSimple' => 'tns:subscribeUserToCourseSimple_arg'], // input parameters
  4207. ['return' => 'xsd:string'], // output parameters
  4208. 'urn:WSRegistration', // namespace
  4209. 'urn:WSRegistration#WSSubscribeUserToCourseSimple', // soapaction
  4210. 'rpc', // style
  4211. 'encoded', // use
  4212. 'This service subscribes a user to a course in a simple way' // documentation
  4213. );
  4214. /**
  4215. * define the method WSSubscribeUserToCourse.
  4216. *
  4217. * @param array $params
  4218. *
  4219. * @return array|int|soap_fault|string|null
  4220. */
  4221. function WSSubscribeUserToCourseSimple($params)
  4222. {
  4223. global $debug;
  4224. if ($debug) {
  4225. error_log('WSSubscribeUserToCourseSimple');
  4226. }
  4227. if ($debug) {
  4228. error_log('Params '.print_r($params, 1));
  4229. }
  4230. if (!WSHelperVerifyKey($params)) {
  4231. return returnError(WS_ERROR_SECRET_KEY);
  4232. }
  4233. $result = [];
  4234. $course_code = $params['course']; //Course code
  4235. $user_id = $params['user_id']; //chamilo user id
  4236. $status = STUDENT;
  4237. if (!empty($params['status'])) {
  4238. $status = $params['status'];
  4239. }
  4240. // Get user id
  4241. $user_data = api_get_user_info($user_id);
  4242. if (empty($user_data)) {
  4243. // If user was not found, there was a problem
  4244. $result = "User $user_id does not exist";
  4245. if ($debug) {
  4246. error_log($result);
  4247. }
  4248. return $result;
  4249. }
  4250. if (!empty($course_code)) {
  4251. $course_data = api_get_course_info($course_code);
  4252. if (empty($course_data)) {
  4253. // Course was not found
  4254. $result = "Course $course_code does not exist in the platform ";
  4255. if ($debug) {
  4256. error_log($result);
  4257. }
  4258. } else {
  4259. if ($debug) {
  4260. error_log('Try to register: user_id= '.$user_id.' to course: '.$course_data['code']);
  4261. }
  4262. if (!CourseManager::subscribeUser($user_id, $course_data['code'], $status, 0, false, false)) {
  4263. $result = 'User was not registered possible reasons: User already registered to the course,
  4264. Course visibility doesnt allow user subscriptions ';
  4265. if ($debug) {
  4266. error_log($result);
  4267. }
  4268. } else {
  4269. if ($debug) {
  4270. error_log('User registered to the course: '.$course_data['code']);
  4271. }
  4272. $result = 1;
  4273. }
  4274. }
  4275. }
  4276. return $result;
  4277. }
  4278. /* GetUser */
  4279. $server->wsdl->addComplexType(
  4280. 'GetUserArg',
  4281. 'complexType',
  4282. 'struct',
  4283. 'all',
  4284. '',
  4285. [
  4286. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  4287. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  4288. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4289. ]
  4290. );
  4291. // Prepare output params, in this case will return an array
  4292. $server->wsdl->addComplexType(
  4293. 'User',
  4294. 'complexType',
  4295. 'struct',
  4296. 'all',
  4297. '',
  4298. [
  4299. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  4300. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  4301. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  4302. ]
  4303. );
  4304. // Register the method to expose
  4305. $server->register(
  4306. 'WSGetUser', // method name
  4307. ['GetUser' => 'tns:GetUserArg'], // input parameters
  4308. ['return' => 'tns:User'], // output parameters
  4309. 'urn:WSRegistration', // namespace
  4310. 'urn:WSRegistration#WSGetUser', // soapaction
  4311. 'rpc', // style
  4312. 'encoded', // use
  4313. 'This service get user information by id' // documentation
  4314. );
  4315. // define the method WSGetUser
  4316. function WSGetUser($params)
  4317. {
  4318. global $debug;
  4319. if ($debug) {
  4320. error_log('WSGetUser');
  4321. }
  4322. if ($debug) {
  4323. error_log('$params: '.print_r($params, 1));
  4324. }
  4325. if (!WSHelperVerifyKey($params)) {
  4326. return returnError(WS_ERROR_SECRET_KEY);
  4327. }
  4328. $result = [];
  4329. // Get user id
  4330. $user_id = UserManager::get_user_id_from_original_id(
  4331. $params['original_user_id_value'],
  4332. $params['original_user_id_name']
  4333. );
  4334. $user_data = api_get_user_info($user_id);
  4335. if (empty($user_data)) {
  4336. // If user was not found, there was a problem
  4337. $result['user_id'] = '';
  4338. $result['firstname'] = '';
  4339. $result['lastname'] = '';
  4340. } else {
  4341. $result['user_id'] = $user_data['user_id'];
  4342. $result['firstname'] = $user_data['firstname'];
  4343. $result['lastname'] = $user_data['lastname'];
  4344. }
  4345. return $result;
  4346. }
  4347. $server->wsdl->addComplexType(
  4348. 'GetUserArgUsername',
  4349. 'complexType',
  4350. 'struct',
  4351. 'all',
  4352. '',
  4353. [
  4354. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  4355. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4356. ]
  4357. );
  4358. // Prepare output params, in this case will return an array
  4359. $server->wsdl->addComplexType(
  4360. 'UserWithExtraFields',
  4361. 'complexType',
  4362. 'struct',
  4363. 'all',
  4364. '',
  4365. [
  4366. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  4367. 'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
  4368. 'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
  4369. 'extra_fields' => ['name' => 'extra_fields', 'type' => 'xsd:string'],
  4370. ]
  4371. );
  4372. // Register the method to expose
  4373. $server->register(
  4374. 'WSGetUserFromUsername', // method name
  4375. ['GetUserFromUsername' => 'tns:GetUserArgUsername'], // input params
  4376. ['return' => 'tns:UserWithExtraFields'], // output parameters
  4377. 'urn:WSRegistration', // namespace
  4378. 'urn:WSRegistration#WSGetUserFromUsername', // soapaction
  4379. 'rpc', // style
  4380. 'encoded', // use
  4381. 'This service get user information by username' // documentation
  4382. );
  4383. // define the method WSGetUserFromUsername
  4384. function WSGetUserFromUsername($params)
  4385. {
  4386. global $debug;
  4387. if ($debug) {
  4388. error_log('WSGetUserFromUsername');
  4389. }
  4390. if ($debug) {
  4391. error_log('$params: '.print_r($params, 1));
  4392. }
  4393. if (!WSHelperVerifyKey($params)) {
  4394. return returnError(WS_ERROR_SECRET_KEY);
  4395. }
  4396. // Get user id
  4397. $user_data = api_get_user_info_from_username($params['username']);
  4398. $result = [];
  4399. $result['user_id'] = '';
  4400. $result['firstname'] = '';
  4401. $result['lastname'] = '';
  4402. $result['extra_fields'] = '';
  4403. if (empty($user_data)) {
  4404. // If user was not found, there was a problem
  4405. if ($debug) {
  4406. error_log('User not found :(');
  4407. }
  4408. } else {
  4409. $result['user_id'] = $user_data['user_id'];
  4410. $result['firstname'] = $user_data['firstname'];
  4411. $result['lastname'] = $user_data['lastname'];
  4412. $result['email'] = $user_data['email'];
  4413. // Get extra fields
  4414. $fieldValue = new ExtraFieldValue('user');
  4415. $extra = $fieldValue->getAllValuesByItem($result['user_id']);
  4416. $result['extra_fields'] = json_encode($extra);
  4417. if ($debug) {
  4418. error_log('User found :) return value '.print_r($result, 1));
  4419. }
  4420. }
  4421. return $result;
  4422. }
  4423. $server->wsdl->addComplexType(
  4424. 'GetUserArgUsernameWithOriginal',
  4425. 'complexType',
  4426. 'struct',
  4427. 'all',
  4428. '',
  4429. [
  4430. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  4431. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  4432. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  4433. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4434. ]
  4435. );
  4436. // Register the method to expose
  4437. $server->register(
  4438. 'WSUpdateUserOriginalIdFromUsername', // method name
  4439. ['WSUpdateUserOriginalIdFromUsername' => 'tns:GetUserArgUsernameWithOriginal'], // input params
  4440. ['return' => 'tns:User'], // output parameters
  4441. 'urn:WSRegistration', // namespace
  4442. 'urn:WSRegistration#WSGetUserFromUsername', // soapaction
  4443. 'rpc', // style
  4444. 'encoded', // use
  4445. 'This service get user information by username' // documentation
  4446. );
  4447. // define the method WSGetUserFromUsername
  4448. function WSUpdateUserOriginalIdFromUsername($params)
  4449. {
  4450. global $debug;
  4451. if ($debug) {
  4452. error_log('WSUpdateUserOriginalIdFromUsername');
  4453. }
  4454. if ($debug) {
  4455. error_log('$params: '.print_r($params, 1));
  4456. }
  4457. if (!WSHelperVerifyKey($params)) {
  4458. return returnError(WS_ERROR_SECRET_KEY);
  4459. }
  4460. $result = [];
  4461. // Get user id
  4462. $user_data = api_get_user_info_from_username($params['username']);
  4463. if (empty($user_data)) {
  4464. // If user was not found, there was a problem
  4465. $result['user_id'] = '';
  4466. $result['firstname'] = '';
  4467. $result['lastname'] = '';
  4468. if ($debug) {
  4469. error_log('User not found :(');
  4470. }
  4471. } else {
  4472. $result['user_id'] = $user_data['user_id'];
  4473. $result['firstname'] = $user_data['firstname'];
  4474. $result['lastname'] = $user_data['lastname'];
  4475. $result['email'] = $user_data['email'];
  4476. $resultUpdate = UserManager::update_extra_field_value(
  4477. $user_data['user_id'],
  4478. $params['original_user_id_name'],
  4479. $params['original_user_id_value']
  4480. );
  4481. $fieldValue = new ExtraFieldValue('user');
  4482. $extraList = $fieldValue->getAllValuesByItem(
  4483. $result['user_id']
  4484. );
  4485. $result['extra'] = $extraList;
  4486. if ($debug) {
  4487. if ($resultUpdate) {
  4488. error_log('User updated :) ');
  4489. } else {
  4490. error_log('User not updated :(');
  4491. }
  4492. error_log('$result: '.print_r($result, 1));
  4493. }
  4494. }
  4495. return $result;
  4496. }
  4497. /* Register WSUnsubscribeUserFromCourse function */
  4498. // Register the data structures used by the service
  4499. $server->wsdl->addComplexType(
  4500. 'unsuscribeUserFromCourseParams',
  4501. 'complexType',
  4502. 'struct',
  4503. 'all',
  4504. '',
  4505. [
  4506. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'],
  4507. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  4508. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4509. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  4510. ]
  4511. );
  4512. $server->wsdl->addComplexType(
  4513. 'unsuscribeUserFromCourseParamsList',
  4514. 'complexType',
  4515. 'array',
  4516. '',
  4517. 'SOAP-ENC:Array',
  4518. [],
  4519. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:unsuscribeUserFromCourseParams[]']],
  4520. 'tns:unsuscribeUserFromCourseParams'
  4521. );
  4522. $server->wsdl->addComplexType(
  4523. 'unsuscribeUserFromCourse',
  4524. 'complexType',
  4525. 'struct',
  4526. 'all',
  4527. '',
  4528. [
  4529. 'userscourses' => ['name' => 'userscourses', 'type' => 'tns:unsuscribeUserFromCourseParamsList'],
  4530. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4531. ]
  4532. );
  4533. // Prepare output params, in this case will return an array
  4534. $server->wsdl->addComplexType(
  4535. 'result_unsuscribeUserFromCourse',
  4536. 'complexType',
  4537. 'struct',
  4538. 'all',
  4539. '',
  4540. [
  4541. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'xsd:string'],
  4542. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4543. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  4544. ]
  4545. );
  4546. $server->wsdl->addComplexType(
  4547. 'results_unsuscribeUserFromCourse',
  4548. 'complexType',
  4549. 'array',
  4550. '',
  4551. 'SOAP-ENC:Array',
  4552. [],
  4553. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_unsuscribeUserFromCourse[]']],
  4554. 'tns:result_unsuscribeUserFromCourse'
  4555. );
  4556. // Register the method to expose
  4557. $server->register(
  4558. 'WSUnsubscribeUserFromCourse', // method name
  4559. ['unsuscribeUserFromCourse' => 'tns:unsuscribeUserFromCourse'], // input parameters
  4560. ['return' => 'tns:results_unsuscribeUserFromCourse'], // output parameters
  4561. 'urn:WSRegistration', // namespace
  4562. 'urn:WSRegistration#WSUnsubscribeUserFromCourse', // soapaction
  4563. 'rpc', // style
  4564. 'encoded', // use
  4565. 'This service unsubscribes a user from a course' // documentation
  4566. );
  4567. // define the method WSUnsubscribeUserFromCourse
  4568. function WSUnsubscribeUserFromCourse($params)
  4569. {
  4570. if (!WSHelperVerifyKey($params)) {
  4571. return returnError(WS_ERROR_SECRET_KEY);
  4572. }
  4573. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  4574. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  4575. $userscourses_params = $params['userscourses'];
  4576. $results = [];
  4577. $orig_user_id_value = [];
  4578. $orig_course_id_value = [];
  4579. foreach ($userscourses_params as $usercourse_param) {
  4580. $original_user_id_values = $usercourse_param['original_user_id_values'];
  4581. $original_user_id_name = $usercourse_param['original_user_id_name'];
  4582. $original_course_id_value = $usercourse_param['original_course_id_value'];
  4583. $original_course_id_name = $usercourse_param['original_course_id_name'];
  4584. $orig_course_id_value[] = $original_course_id_value;
  4585. // Get user id from original user id
  4586. $usersList = [];
  4587. foreach ($original_user_id_values as $key => $row_original_user_id) {
  4588. $user_id = UserManager::get_user_id_from_original_id(
  4589. $original_user_id_values[$key],
  4590. $original_user_id_name[$key]
  4591. );
  4592. if ($user_id == 0) {
  4593. continue; // user_id doesn't exist.
  4594. } else {
  4595. $sql = "SELECT user_id FROM $user_table WHERE user_id ='".$user_id."' AND active= '0'";
  4596. $resu = Database::query($sql);
  4597. $r_check_user = Database::fetch_row($resu);
  4598. if (!empty($r_check_user[0])) {
  4599. continue; // user_id is not active.
  4600. }
  4601. }
  4602. $usersList[] = $user_id;
  4603. }
  4604. $orig_user_id_value[] = implode(',', $usersList);
  4605. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  4606. $original_course_id_value,
  4607. $original_course_id_name
  4608. );
  4609. if (empty($courseInfo) ||
  4610. (isset($courseInfo) && $courseInfo['visibility'] == 0)
  4611. ) {
  4612. $results[] = 0;
  4613. continue; // Original_course_id_value doesn't exist.
  4614. }
  4615. $courseId = $courseInfo['real_id'];
  4616. if (count($usersList) == 0) {
  4617. $results[] = 0;
  4618. continue;
  4619. }
  4620. foreach ($usersList as $user_id) {
  4621. $sql = "DELETE FROM $table_course_user
  4622. WHERE user_id = '$user_id' AND c_id = '".$courseId."'";
  4623. $result = Database::query($sql);
  4624. Database::affected_rows($result);
  4625. }
  4626. $results[] = 1;
  4627. continue;
  4628. } // end principal foreach
  4629. $count_results = count($results);
  4630. $output = [];
  4631. for ($i = 0; $i < $count_results; $i++) {
  4632. $output[] = [
  4633. 'original_user_id_values' => $orig_user_id_value[$i],
  4634. 'original_course_id_value' => $orig_course_id_value[$i],
  4635. 'result' => $results[$i],
  4636. ];
  4637. }
  4638. return $output;
  4639. }
  4640. /* Register WSSuscribeUsersToSession function */
  4641. $server->wsdl->addComplexType(
  4642. 'unSubscribeUserFromCourseSimple',
  4643. 'complexType',
  4644. 'struct',
  4645. 'all',
  4646. '',
  4647. [
  4648. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  4649. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  4650. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4651. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  4652. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4653. ]
  4654. );
  4655. $server->wsdl->addComplexType(
  4656. 'unSubscribeUserToCourseSimple_return',
  4657. 'complexType',
  4658. 'struct',
  4659. 'all',
  4660. '',
  4661. [
  4662. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  4663. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4664. 'result' => ['name' => 'result', 'type' => 'xsd:int'],
  4665. ]
  4666. );
  4667. // Register the method to expose
  4668. $server->register(
  4669. 'WSUnSubscribeUserFromCourseSimple', // method name
  4670. ['unSubscribeUserFromCourseSimple' => 'tns:unSubscribeUserFromCourseSimple'], // input parameters
  4671. ['return' => 'tns:unSubscribeUserToCourseSimple_return'], // output parameters
  4672. 'urn:WSRegistration', // namespace
  4673. 'urn:WSRegistration#WSUnSubscribeUserFromCourseSimple', // soapaction
  4674. 'rpc', // style
  4675. 'encoded', // use
  4676. 'This service unsubscribe a user from a course' // documentation
  4677. );
  4678. /**
  4679. * @param array $params
  4680. *
  4681. * @return array|soap_fault|null
  4682. */
  4683. function WSUnSubscribeUserFromCourseSimple($params)
  4684. {
  4685. global $debug;
  4686. error_log('WSUnSubscribeUserFromCourseSimple');
  4687. if (!WSHelperVerifyKey($params)) {
  4688. return returnError(WS_ERROR_SECRET_KEY);
  4689. }
  4690. $original_user_id_value = $params['original_user_id_value'];
  4691. $original_user_id_name = $params['original_user_id_name'];
  4692. $original_course_id_value = $params['original_course_id_value'];
  4693. $original_course_id_name = $params['original_course_id_name'];
  4694. $result = [];
  4695. $result['original_user_id_value'] = $original_user_id_value;
  4696. $result['original_course_id_value'] = $original_course_id_value;
  4697. $result['result'] = 0;
  4698. $user_id = UserManager::get_user_id_from_original_id(
  4699. $original_user_id_value,
  4700. $original_user_id_name
  4701. );
  4702. if ($user_id) {
  4703. if ($debug) {
  4704. error_log("User $original_user_id_value, $original_user_id_name found");
  4705. error_log("Course $original_course_id_value, $original_course_id_name found");
  4706. }
  4707. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  4708. $original_course_id_value,
  4709. $original_course_id_name
  4710. );
  4711. $courseCode = $courseInfo['code'];
  4712. if (empty($courseCode)) {
  4713. // Course was not found
  4714. if ($debug) {
  4715. error_log("course not found");
  4716. }
  4717. } else {
  4718. if ($debug) {
  4719. error_log("Course $courseCode found");
  4720. }
  4721. CourseManager::unsubscribe_user($user_id, $courseCode, 0);
  4722. $result['result'] = 1;
  4723. }
  4724. } else {
  4725. if ($debug) {
  4726. error_log("User not found");
  4727. }
  4728. }
  4729. return $result;
  4730. }
  4731. $server->wsdl->addComplexType(
  4732. 'subscribeUserToCourseParams',
  4733. 'complexType',
  4734. 'struct',
  4735. 'all',
  4736. '',
  4737. [
  4738. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'],
  4739. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  4740. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4741. 'original_course_id_name' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  4742. ]
  4743. );
  4744. // Prepare output params, in this case will return an array.
  4745. $server->wsdl->addComplexType(
  4746. 'result_subscribeUsersToSession',
  4747. 'complexType',
  4748. 'struct',
  4749. 'all',
  4750. '',
  4751. [
  4752. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'xsd:string'],
  4753. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  4754. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  4755. ]
  4756. );
  4757. $server->wsdl->addComplexType(
  4758. 'results_subscribeUsersToSession',
  4759. 'complexType',
  4760. 'array',
  4761. '',
  4762. 'SOAP-ENC:Array',
  4763. [],
  4764. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_subscribeUsersToSession[]']],
  4765. 'tns:result_subscribeUsersToSession'
  4766. );
  4767. $server->wsdl->addComplexType(
  4768. 'originalUserItem',
  4769. 'complexType',
  4770. 'struct',
  4771. 'all',
  4772. '',
  4773. [
  4774. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  4775. ]
  4776. );
  4777. // Register the data structures used by the service
  4778. $server->wsdl->addComplexType(
  4779. 'originalUsersList',
  4780. 'complexType',
  4781. 'array',
  4782. '',
  4783. 'SOAP-ENC:Array',
  4784. [],
  4785. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:originalUserItem[]']],
  4786. 'tns:originalUserItem'
  4787. );
  4788. /* Register WSSuscribeUsersToSession function */
  4789. // Register the data structures used by the service
  4790. $server->wsdl->addComplexType(
  4791. 'subscribeUsersToSessionParams',
  4792. 'complexType',
  4793. 'struct',
  4794. 'all',
  4795. '',
  4796. [
  4797. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'],
  4798. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  4799. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  4800. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  4801. ]
  4802. );
  4803. $server->wsdl->addComplexType(
  4804. 'subscribeUsersToSessionParamsList',
  4805. 'complexType',
  4806. 'array',
  4807. '',
  4808. 'SOAP-ENC:Array',
  4809. [],
  4810. [
  4811. [
  4812. 'ref' => 'SOAP-ENC:arrayType',
  4813. 'wsdl:arrayType' => 'tns:subscribeUsersToSessionParams[]',
  4814. ],
  4815. ],
  4816. 'tns:subscribeUsersToSessionParams'
  4817. );
  4818. $server->wsdl->addComplexType(
  4819. 'subscribeUsersToSession',
  4820. 'complexType',
  4821. 'struct',
  4822. 'all',
  4823. '',
  4824. [
  4825. 'userssessions' => ['name' => 'userssessions', 'type' => 'tns:subscribeUsersToSessionParamsList'],
  4826. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4827. ]
  4828. );
  4829. // Register the method to expose
  4830. $server->register(
  4831. 'WSSuscribeUsersToSession', // method name
  4832. ['subscribeUsersToSession' => 'tns:subscribeUsersToSession'], // input parameters
  4833. ['return' => 'tns:results_subscribeUsersToSession'], // output parameters
  4834. 'urn:WSRegistration', // namespace
  4835. 'urn:WSRegistration#WSSuscribeUsersToSession', // soapaction
  4836. 'rpc', // style
  4837. 'encoded', // use
  4838. 'This service subscribes a user to a session' // documentation
  4839. );
  4840. // define the method WSSuscribeUsersToSession
  4841. function WSSuscribeUsersToSession($params)
  4842. {
  4843. global $debug;
  4844. if (!WSHelperVerifyKey($params)) {
  4845. return returnError(WS_ERROR_SECRET_KEY);
  4846. }
  4847. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  4848. $userssessions_params = $params['userssessions'];
  4849. if ($debug) {
  4850. error_log('WSSuscribeUsersToSession');
  4851. error_log(print_r($params, 1));
  4852. if (empty($userssessions_params)) {
  4853. error_log('userssessions is empty');
  4854. }
  4855. }
  4856. $results = [];
  4857. $orig_user_id_value = [];
  4858. $orig_session_id_value = [];
  4859. foreach ($userssessions_params as $usersession_params) {
  4860. $original_session_id_value = $usersession_params['original_session_id_value'];
  4861. $original_session_id_name = $usersession_params['original_session_id_name'];
  4862. $original_user_id_name = $usersession_params['original_user_id_name'];
  4863. $original_user_id_values = $usersession_params['original_user_id_values'];
  4864. $sessionId = SessionManager::getSessionIdFromOriginalId(
  4865. $original_session_id_value,
  4866. $original_session_id_name
  4867. );
  4868. if (empty($sessionId)) {
  4869. $orig_session_id_value[] = $original_session_id_value;
  4870. $results[] = 0;
  4871. continue;
  4872. }
  4873. foreach ($original_user_id_values as $key => $row_original_user_list) {
  4874. $orig_session_id_value[] = $original_session_id_value;
  4875. $orig_user_id_value[] = $row_original_user_list['original_user_id_value'];
  4876. $user_id = UserManager::get_user_id_from_original_id(
  4877. $row_original_user_list['original_user_id_value'],
  4878. $original_user_id_name
  4879. );
  4880. if ($debug) {
  4881. error_log("User to subscribe: $user_id");
  4882. }
  4883. if ($user_id == 0) {
  4884. $results[] = 0;
  4885. continue; // user_id doesn't exist.
  4886. } else {
  4887. $sql = "SELECT user_id FROM $user_table
  4888. WHERE user_id ='".$user_id."' AND active= '0'";
  4889. $resu = Database::query($sql);
  4890. $r_check_user = Database::fetch_row($resu);
  4891. if (!empty($r_check_user[0])) {
  4892. $results[] = 0;
  4893. continue; // user_id is not active.
  4894. }
  4895. SessionManager::subscribeUsersToSession(
  4896. $sessionId,
  4897. [$user_id],
  4898. SESSION_VISIBLE_READ_ONLY,
  4899. false
  4900. );
  4901. $results[] = 1;
  4902. if ($debug) {
  4903. error_log("subscribe user:$user_id to session $sessionId");
  4904. }
  4905. }
  4906. }
  4907. } // end principal foreach
  4908. $count_results = count($results);
  4909. $output = [];
  4910. for ($i = 0; $i < $count_results; $i++) {
  4911. $output[] = [
  4912. 'original_user_id_values' => $orig_user_id_value[$i],
  4913. 'original_session_id_value' => $orig_session_id_value[$i],
  4914. 'result' => $results[$i],
  4915. ];
  4916. }
  4917. return $output;
  4918. }
  4919. // WSSubscribeUserToSessionSimple
  4920. $server->wsdl->addComplexType(
  4921. 'subscribeUserToSessionSimple_arg',
  4922. 'complexType',
  4923. 'struct',
  4924. 'all',
  4925. '',
  4926. [
  4927. 'session' => ['name' => 'session', 'type' => 'xsd:string'], // Session ID
  4928. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'], // Chamilo user_id
  4929. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  4930. ]
  4931. );
  4932. $server->register(
  4933. 'WSSubscribeUserToSessionSimple', // method name
  4934. ['subscribeUserToSessionSimple' => 'tns:subscribeUserToSessionSimple_arg'], // input parameters
  4935. ['return' => 'xsd:string'], // output parameters
  4936. 'urn:WSRegistration', // namespace
  4937. 'urn:WSRegistration#WSSubscribeUserToSessionSimple', // soapaction
  4938. 'rpc', // style
  4939. 'encoded', // use
  4940. 'This service subscribes a user to a session in a simple way' // documentation
  4941. );
  4942. /**
  4943. * @param array $params
  4944. *
  4945. * @return int|soap_fault|string|null
  4946. */
  4947. function WSSubscribeUserToSessionSimple($params)
  4948. {
  4949. global $debug;
  4950. if ($debug) {
  4951. error_log('WSSubscribeUserToSessionSimple with params=['.serialize($params).']');
  4952. }
  4953. // Check security key
  4954. if (!WSHelperVerifyKey($params)) {
  4955. return returnError(WS_ERROR_SECRET_KEY);
  4956. }
  4957. // Get input parameters
  4958. $session_id = intval($params['session']); // Session ID
  4959. $user_id = intval($params['user_id']); // Chamilo user id
  4960. // Get user id
  4961. $user_data = api_get_user_info($user_id);
  4962. // Prepare answer
  4963. $result = 0;
  4964. if (empty($user_data)) {
  4965. $result = "User {$user_id} does not exist";
  4966. if ($debug) {
  4967. error_log($result);
  4968. }
  4969. return $result;
  4970. }
  4971. if (!empty($session_id) && is_numeric($session_id)) {
  4972. $session_data = api_get_session_info($session_id);
  4973. if (empty($session_data)) {
  4974. $result = "Session {$session_id} does not exist.";
  4975. if ($debug) {
  4976. error_log($result);
  4977. }
  4978. } else {
  4979. SessionManager::subscribeUsersToSession(
  4980. $session_id,
  4981. [$user_id],
  4982. SESSION_VISIBLE_READ_ONLY,
  4983. false
  4984. );
  4985. if ($debug) {
  4986. error_log('User registered to the course: '.$session_id);
  4987. }
  4988. $result = 1;
  4989. }
  4990. }
  4991. return $result;
  4992. }
  4993. /* Register WSUnsuscribeUsersFromSession function */
  4994. // Register the data structures used by the service
  4995. $server->wsdl->addComplexType(
  4996. 'unsubscribeUsersFromSessionParams',
  4997. 'complexType',
  4998. 'struct',
  4999. 'all',
  5000. '',
  5001. [
  5002. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'tns:originalUsersList'],
  5003. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  5004. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  5005. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  5006. ]
  5007. );
  5008. $server->wsdl->addComplexType(
  5009. 'unsubscribeUsersFromSessionParamsList',
  5010. 'complexType',
  5011. 'array',
  5012. '',
  5013. 'SOAP-ENC:Array',
  5014. [],
  5015. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:unsubscribeUsersFromSessionParams[]']],
  5016. 'tns:unsubscribeUsersFromSessionParams'
  5017. );
  5018. $server->wsdl->addComplexType(
  5019. 'unsubscribeUsersFromSession',
  5020. 'complexType',
  5021. 'struct',
  5022. 'all',
  5023. '',
  5024. [
  5025. 'userssessions' => ['name' => 'userssessions', 'type' => 'tns:subscribeUsersToSessionParamsList'],
  5026. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5027. ]
  5028. );
  5029. // Prepare output params, in this case will return an array
  5030. $server->wsdl->addComplexType(
  5031. 'result_unsubscribeUsersFromSession',
  5032. 'complexType',
  5033. 'struct',
  5034. 'all',
  5035. '',
  5036. [
  5037. 'original_user_id_values' => ['name' => 'original_user_id_values', 'type' => 'xsd:string'],
  5038. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  5039. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  5040. ]
  5041. );
  5042. $server->wsdl->addComplexType(
  5043. 'results_unsubscribeUsersFromSession',
  5044. 'complexType',
  5045. 'array',
  5046. '',
  5047. 'SOAP-ENC:Array',
  5048. [],
  5049. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_unsubscribeUsersFromSession[]']],
  5050. 'tns:result_unsubscribeUsersFromSession'
  5051. );
  5052. // Register the method to expose
  5053. $server->register(
  5054. 'WSUnsuscribeUsersFromSession', // method name
  5055. ['unsubscribeUsersFromSession' => 'tns:unsubscribeUsersFromSession'], // input parameters
  5056. ['return' => 'tns:results_unsubscribeUsersFromSession'], // output parameters
  5057. 'urn:WSRegistration', // namespace
  5058. 'urn:WSRegistration#WSUnsuscribeUsersFromSession', // soapaction
  5059. 'rpc', // style
  5060. 'encoded', // use
  5061. 'This service unsubscribes a user to a session' // documentation
  5062. );
  5063. // define the method WSUnsuscribeUsersFromSession
  5064. function WSUnsuscribeUsersFromSession($params)
  5065. {
  5066. if (!WSHelperVerifyKey($params)) {
  5067. return returnError(WS_ERROR_SECRET_KEY);
  5068. }
  5069. global $debug;
  5070. if ($debug) {
  5071. error_log('WSUnsuscribeUsersFromSession with params=['.serialize($params).']');
  5072. }
  5073. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  5074. $userssessions_params = $params['userssessions'];
  5075. $results = [];
  5076. $orig_user_id_value = [];
  5077. $orig_session_id_value = [];
  5078. foreach ($userssessions_params as $usersession_params) {
  5079. $original_session_id_value = $usersession_params['original_session_id_value'];
  5080. $original_session_id_name = $usersession_params['original_session_id_name'];
  5081. $original_user_id_name = $usersession_params['original_user_id_name'];
  5082. $original_user_id_values = $usersession_params['original_user_id_values'];
  5083. $id_session = SessionManager::getSessionIdFromOriginalId(
  5084. $original_session_id_value,
  5085. $original_session_id_name
  5086. );
  5087. if (empty($id_session)) {
  5088. $orig_session_id_value[] = $original_session_id_value;
  5089. $results[] = 0;
  5090. continue;
  5091. }
  5092. foreach ($original_user_id_values as $key => $row_original_user_list) {
  5093. $orig_session_id_value[] = $original_session_id_value;
  5094. $orig_user_id_value[] = $row_original_user_list['original_user_id_value'];
  5095. $user_id = UserManager::get_user_id_from_original_id(
  5096. $row_original_user_list['original_user_id_value'],
  5097. $original_user_id_name
  5098. );
  5099. if ($user_id == 0) {
  5100. $results[] = 0;
  5101. continue; // user_id doesn't exist.
  5102. } else {
  5103. $sql = "SELECT user_id FROM $user_table
  5104. WHERE user_id ='".$user_id."' AND active= '0'";
  5105. $resu = Database::query($sql);
  5106. $r_check_user = Database::fetch_row($resu);
  5107. if (!empty($r_check_user[0])) {
  5108. $results[] = 0;
  5109. continue; // user_id is not active.
  5110. }
  5111. SessionManager::unsubscribe_user_from_session(
  5112. $id_session,
  5113. $user_id
  5114. );
  5115. $results[] = 1;
  5116. if ($debug) {
  5117. error_log("Unsubscribe user:$user_id to session:$id_session");
  5118. }
  5119. }
  5120. }
  5121. } // end principal foreach
  5122. $count_results = count($results);
  5123. $output = [];
  5124. for ($i = 0; $i < $count_results; $i++) {
  5125. $output[] = [
  5126. 'original_user_id_values' => $orig_user_id_value[$i],
  5127. 'original_session_id_value' => $orig_session_id_value[$i],
  5128. 'result' => $results[$i],
  5129. ];
  5130. }
  5131. return $output;
  5132. }
  5133. /* Register WSSuscribeCoursesToSession function */
  5134. // Register the data structures used by the service
  5135. /*$server->wsdl->addComplexType(
  5136. 'originalCoursesList',
  5137. 'complexType',
  5138. 'array',
  5139. '',
  5140. 'SOAP-ENC:Array',
  5141. array(),
  5142. array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'string[]')),
  5143. 'xsd:string'
  5144. );*/
  5145. $server->wsdl->addComplexType(
  5146. 'course_code_type',
  5147. 'complexType',
  5148. 'struct',
  5149. 'all',
  5150. '',
  5151. [
  5152. 'course_code' => ['name' => 'course_code', 'type' => 'xsd:string'],
  5153. ]
  5154. );
  5155. $server->wsdl->addComplexType(
  5156. 'originalCoursesList',
  5157. 'complexType',
  5158. 'array',
  5159. '',
  5160. 'SOAP-ENC:Array',
  5161. [],
  5162. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_code_type[]']],
  5163. 'tns:course_code_type'
  5164. );
  5165. $server->wsdl->addComplexType(
  5166. 'subscribeCoursesToSessionParamsList',
  5167. 'complexType',
  5168. 'array',
  5169. '',
  5170. 'SOAP-ENC:Array',
  5171. [],
  5172. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:subscribeCoursesToSessionParams[]']],
  5173. 'tns:subscribeCoursesToSessionParams'
  5174. );
  5175. $server->wsdl->addComplexType(
  5176. 'subscribeCoursesToSessionParams',
  5177. 'complexType',
  5178. 'struct',
  5179. 'all',
  5180. '',
  5181. [
  5182. 'original_course_id_values' => ['name' => 'original_course_id_values', 'type' => 'tns:originalCoursesList'],
  5183. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  5184. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  5185. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  5186. ]
  5187. );
  5188. $server->wsdl->addComplexType(
  5189. 'subscribeCoursesToSessionParamsList',
  5190. 'complexType',
  5191. 'array',
  5192. '',
  5193. 'SOAP-ENC:Array',
  5194. [],
  5195. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:subscribeCoursesToSessionParams[]']],
  5196. 'tns:subscribeCoursesToSessionParams'
  5197. );
  5198. $server->wsdl->addComplexType(
  5199. 'subscribeCoursesToSession',
  5200. 'complexType',
  5201. 'struct',
  5202. 'all',
  5203. '',
  5204. [
  5205. 'coursessessions' => ['name' => 'coursessessions', 'type' => 'tns:subscribeCoursesToSessionParamsList'],
  5206. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5207. ]
  5208. );
  5209. // Prepare output params, in this case will return an array
  5210. $server->wsdl->addComplexType(
  5211. 'result_subscribeCoursesToSession',
  5212. 'complexType',
  5213. 'struct',
  5214. 'all',
  5215. '',
  5216. [
  5217. 'original_course_id_values' => ['name' => 'original_course_id_values', 'type' => 'xsd:string'],
  5218. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  5219. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  5220. ]
  5221. );
  5222. $server->wsdl->addComplexType(
  5223. 'results_subscribeCoursesToSession',
  5224. 'complexType',
  5225. 'array',
  5226. '',
  5227. 'SOAP-ENC:Array',
  5228. [],
  5229. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_subscribeCoursesToSession[]']],
  5230. 'tns:result_subscribeCoursesToSession'
  5231. );
  5232. // Register the method to expose
  5233. $server->register(
  5234. 'WSSuscribeCoursesToSession', // method name
  5235. ['subscribeCoursesToSession' => 'tns:subscribeCoursesToSession'], // input parameters
  5236. ['return' => 'tns:results_subscribeCoursesToSession'], // output parameters
  5237. 'urn:WSRegistration', // namespace
  5238. 'urn:WSRegistration#WSSuscribeCoursesToSession', // soapaction
  5239. 'rpc', // style
  5240. 'encoded', // use
  5241. 'This service subscribes a course to a session' // documentation
  5242. );
  5243. // Define the method WSSuscribeCoursesToSession
  5244. function WSSuscribeCoursesToSession($params)
  5245. {
  5246. global $debug;
  5247. if (!WSHelperVerifyKey($params)) {
  5248. return returnError(WS_ERROR_SECRET_KEY);
  5249. }
  5250. if ($debug) {
  5251. error_log('WSSuscribeCoursesToSession: '.print_r($params, 1));
  5252. }
  5253. $coursessessions_params = $params['coursessessions'];
  5254. $results = [];
  5255. $orig_course_id_value = [];
  5256. $orig_session_id_value = [];
  5257. foreach ($coursessessions_params as $coursesession_param) {
  5258. $original_session_id_value = $coursesession_param['original_session_id_value'];
  5259. $original_session_id_name = $coursesession_param['original_session_id_name'];
  5260. $original_course_id_name = $coursesession_param['original_course_id_name'];
  5261. $original_course_id_values = $coursesession_param['original_course_id_values'];
  5262. $sessionId = SessionManager::getSessionIdFromOriginalId(
  5263. $original_session_id_value,
  5264. $original_session_id_name
  5265. );
  5266. if (empty($sessionId)) {
  5267. $orig_session_id_value[] = $original_session_id_value;
  5268. $results[] = 0;
  5269. continue;
  5270. }
  5271. // Get course list from row_original_course_id_values
  5272. foreach ($original_course_id_values as $row_original_course_list) {
  5273. $orig_session_id_value[] = $original_session_id_value;
  5274. $orig_course_id_value[] = $row_original_course_list['course_code'];
  5275. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  5276. $row_original_course_list['course_code'],
  5277. $original_course_id_name
  5278. );
  5279. if (empty($courseInfo) ||
  5280. (isset($courseInfo) && $courseInfo['visibility'] == 0)
  5281. ) {
  5282. $results[] = 0;
  5283. continue; // Original_course_id_value doesn't exist.
  5284. } else {
  5285. $courseCode = $courseInfo['code'];
  5286. SessionManager::add_courses_to_session(
  5287. $sessionId,
  5288. [$courseInfo['real_id']],
  5289. false
  5290. );
  5291. if ($debug) {
  5292. error_log("add_courses_to_session: course:$courseCode to session:$sessionId");
  5293. }
  5294. $results[] = 1;
  5295. }
  5296. }
  5297. }
  5298. $count_results = count($results);
  5299. $output = [];
  5300. for ($i = 0; $i < $count_results; $i++) {
  5301. $output[] = [
  5302. 'original_course_id_values' => $orig_course_id_value[$i],
  5303. 'original_session_id_value' => $orig_session_id_value[$i],
  5304. 'result' => $results[$i],
  5305. ];
  5306. }
  5307. return $output;
  5308. }
  5309. /* Register WSUnsuscribeCoursesFromSession function */
  5310. // Register the data structures used by the service
  5311. $server->wsdl->addComplexType(
  5312. 'unsubscribeCoursesFromSessionParams',
  5313. 'complexType',
  5314. 'struct',
  5315. 'all',
  5316. '',
  5317. [
  5318. 'original_course_id_values' => ['name' => 'original_course_id_values', 'type' => 'tns:originalCoursesList'],
  5319. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  5320. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  5321. 'original_session_id_name' => ['name' => 'original_session_id_name', 'type' => 'xsd:string'],
  5322. ]
  5323. );
  5324. $server->wsdl->addComplexType(
  5325. 'unsubscribeCoursesFromSessionParamsList',
  5326. 'complexType',
  5327. 'array',
  5328. '',
  5329. 'SOAP-ENC:Array',
  5330. [],
  5331. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:unsubscribeCoursesFromSessionParams[]']],
  5332. 'tns:unsubscribeCoursesFromSessionParams'
  5333. );
  5334. $server->wsdl->addComplexType(
  5335. 'unsubscribeCoursesFromSession',
  5336. 'complexType',
  5337. 'struct',
  5338. 'all',
  5339. '',
  5340. [
  5341. 'coursessessions' => ['name' => 'coursessessions', 'type' => 'tns:unsubscribeCoursesFromSessionParamsList'],
  5342. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5343. ]
  5344. );
  5345. // Prepare output params, in this case will return an array
  5346. $server->wsdl->addComplexType(
  5347. 'result_unsubscribeCoursesFromSession',
  5348. 'complexType',
  5349. 'struct',
  5350. 'all',
  5351. '',
  5352. [
  5353. 'original_course_id_values' => ['name' => 'original_course_id_values', 'type' => 'xsd:string'],
  5354. 'original_session_id_value' => ['name' => 'original_session_id_value', 'type' => 'xsd:string'],
  5355. 'result' => ['name' => 'result', 'type' => 'xsd:string'],
  5356. ]
  5357. );
  5358. $server->wsdl->addComplexType(
  5359. 'results_unsubscribeCoursesFromSession',
  5360. 'complexType',
  5361. 'array',
  5362. '',
  5363. 'SOAP-ENC:Array',
  5364. [],
  5365. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:result_unsubscribeCoursesFromSession[]']],
  5366. 'tns:result_unsubscribeCoursesFromSession'
  5367. );
  5368. // Register the method to expose
  5369. $server->register(
  5370. 'WSUnsuscribeCoursesFromSession', // method name
  5371. ['unsubscribeCoursesFromSession' => 'tns:unsubscribeCoursesFromSession'], // input parameters
  5372. ['return' => 'tns:results_unsubscribeCoursesFromSession'], // output parameters
  5373. 'urn:WSRegistration', // namespace
  5374. 'urn:WSRegistration#WSUnsuscribeCoursesFromSession', // soapaction
  5375. 'rpc', // style
  5376. 'encoded', // use
  5377. 'This service subscribes a course to a session' // documentation
  5378. );
  5379. // define the method WSUnsuscribeCoursesFromSession
  5380. function WSUnsuscribeCoursesFromSession($params)
  5381. {
  5382. if (!WSHelperVerifyKey($params)) {
  5383. return returnError(WS_ERROR_SECRET_KEY);
  5384. }
  5385. $sessionAdminId = DEFAULT_ADMIN_USER_ID;
  5386. // Initialisation
  5387. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  5388. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  5389. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  5390. $coursessessions_params = $params['coursessessions'];
  5391. $results = [];
  5392. $orig_course_id_value = [];
  5393. $orig_session_id_value = [];
  5394. foreach ($coursessessions_params as $coursesession_param) {
  5395. $original_session_id_value = $coursesession_param['original_session_id_value'];
  5396. $original_session_id_name = $coursesession_param['original_session_id_name'];
  5397. $original_course_id_name = $coursesession_param['original_course_id_name'];
  5398. $original_course_id_values = $coursesession_param['original_course_id_values'];
  5399. $orig_session_id_value[] = $original_session_id_value;
  5400. $id_session = SessionManager::getSessionIdFromOriginalId(
  5401. $original_session_id_value,
  5402. $original_session_id_name
  5403. );
  5404. if (empty($id_session)) {
  5405. $results[] = 0;
  5406. continue;
  5407. }
  5408. // Get courses list from row_original_course_id_values
  5409. $course_list = [];
  5410. $courseIdList = [];
  5411. foreach ($original_course_id_values as $row_original_course_list) {
  5412. $course_code = Database::escape_string($row_original_course_list['course_code']);
  5413. // Check whether exits $x_course_code into user_field_values table.
  5414. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  5415. $row_original_course_list['course_code'],
  5416. $original_course_id_name
  5417. );
  5418. if (empty($courseInfo) || isset($courseInfo) &&
  5419. $courseInfo['visibility'] == 0
  5420. ) {
  5421. continue; // Course_code doesn't exist'
  5422. }
  5423. $course_list[] = $courseInfo['code'];
  5424. $courseIdList[] = $courseInfo['real_id'];
  5425. }
  5426. if (empty($course_list)) {
  5427. $results[] = 0;
  5428. continue;
  5429. }
  5430. $orig_course_id_value[] = implode(',', $course_list);
  5431. foreach ($courseIdList as $courseId) {
  5432. $courseId = intval($courseId);
  5433. Database::query("DELETE FROM $tbl_session_rel_course
  5434. WHERE c_id ='$courseId' AND session_id='$id_session'");
  5435. $result = Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE c_id='$courseId' AND session_id = '$id_session'");
  5436. Event::addEvent(
  5437. LOG_SESSION_DELETE_COURSE,
  5438. LOG_COURSE_ID,
  5439. $courseId,
  5440. api_get_utc_datetime(),
  5441. $sessionAdminId,
  5442. $courseId,
  5443. $id_session
  5444. );
  5445. $return = Database::affected_rows($result);
  5446. }
  5447. $nbr_courses = 0;
  5448. $sql = "SELECT nbr_courses FROM $tbl_session WHERE id = '$id_session'";
  5449. $res_nbr_courses = Database::query($sql);
  5450. $row_nbr_courses = Database::fetch_row($res_nbr_courses);
  5451. if (Database::num_rows($res_nbr_courses) > 0) {
  5452. $nbr_users = ($row_nbr_courses[0] - $return);
  5453. }
  5454. // Update number of users in the session.
  5455. $update_sql = "UPDATE $tbl_session SET nbr_courses= $nbr_courses WHERE id='$id_session' ";
  5456. Database::query($update_sql);
  5457. $results[] = 1;
  5458. continue;
  5459. }
  5460. $count_results = count($results);
  5461. $output = [];
  5462. for ($i = 0; $i < $count_results; $i++) {
  5463. $output[] = [
  5464. 'original_course_id_values' => $orig_course_id_value[$i],
  5465. 'original_session_id_value' => $orig_session_id_value[$i],
  5466. 'result' => $results[$i],
  5467. ];
  5468. }
  5469. return $output;
  5470. }
  5471. /** WSListCourses */
  5472. $server->wsdl->addComplexType(
  5473. 'listCourseInput',
  5474. 'complexType',
  5475. 'struct',
  5476. 'all',
  5477. '',
  5478. [
  5479. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5480. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  5481. 'from' => ['name' => 'from', 'type' => 'xsd:int'],
  5482. 'to' => ['name' => 'to', 'type' => 'xsd:int'],
  5483. ]
  5484. );
  5485. $server->wsdl->addComplexType(
  5486. 'course',
  5487. 'complexType',
  5488. 'struct',
  5489. 'all',
  5490. '',
  5491. [
  5492. 'id' => ['name' => 'id', 'type' => 'xsd:int'],
  5493. 'code' => ['name' => 'code', 'type' => 'xsd:string'],
  5494. 'external_course_id' => ['name' => 'external_course_id', 'type' => 'xsd:string'],
  5495. 'title' => ['name' => 'title', 'type' => 'xsd:string'],
  5496. 'language' => ['name' => 'language', 'type' => 'xsd:string'],
  5497. 'category_name' => ['name' => 'category_name', 'type' => 'xsd:string'],
  5498. 'visibility' => ['name' => 'visibility', 'type' => 'xsd:int'],
  5499. 'number_students' => ['name' => 'number_students', 'type' => 'xsd:int'],
  5500. ]
  5501. );
  5502. $server->wsdl->addComplexType(
  5503. 'courses',
  5504. 'complexType',
  5505. 'array',
  5506. '',
  5507. 'SOAP-ENC:Array',
  5508. [],
  5509. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course[]']],
  5510. 'tns:course'
  5511. );
  5512. // Register the method to expose
  5513. $server->register(
  5514. 'WSListCourses', // method name
  5515. ['listCourseInput' => 'tns:listCourseInput'], // input parameters
  5516. ['return' => 'tns:courses'], // output parameters
  5517. 'urn:WSRegistration', // namespace
  5518. 'urn:WSRegistration#WSListCourses', // soapaction
  5519. 'rpc', // style
  5520. 'encoded', // use
  5521. 'This service list courses available on the system' // documentation
  5522. );
  5523. // define the method WSListCourses
  5524. function WSListCourses($params)
  5525. {
  5526. global $debug;
  5527. if (!WSHelperVerifyKey($params)) {
  5528. return returnError(WS_ERROR_SECRET_KEY);
  5529. }
  5530. $course_field_name = isset($params['original_course_id_name']) ? $params['original_course_id_name'] : '';
  5531. $courses_result = [];
  5532. $category_names = [];
  5533. $from = isset($params['from']) ? $params['from'] : null;
  5534. $to = isset($params['to']) ? $params['to'] : null;
  5535. if ($debug) {
  5536. error_log(print_r($params, 1));
  5537. error_log($from);
  5538. error_log($to);
  5539. }
  5540. $courses = CourseManager::get_courses_list($from, $to);
  5541. foreach ($courses as $course) {
  5542. $course_tmp = [];
  5543. $course_tmp['id'] = $course['id'];
  5544. $course_tmp['code'] = $course['code'];
  5545. $course_tmp['title'] = $course['title'];
  5546. $course_tmp['language'] = $course['course_language'];
  5547. $course_tmp['visibility'] = $course['visibility'];
  5548. $course_tmp['category_name'] = '';
  5549. // Determining category name
  5550. if (!empty($course['category_code']) &&
  5551. isset($category_names[$course['category_code']])
  5552. ) {
  5553. $course_tmp['category_name'] = $category_names[$course['category_code']];
  5554. } else {
  5555. $category = CourseManager::get_course_category($course['category_code']);
  5556. if ($category) {
  5557. $category_names[$course['category_code']] = $category['name'];
  5558. $course_tmp['category_name'] = $category['name'];
  5559. }
  5560. }
  5561. // Determining number of students registered in course
  5562. $course_tmp['number_students'] = CourseManager::get_users_count_in_course(
  5563. $course['code']
  5564. );
  5565. // Determining external course id
  5566. $externalCourseId = '';
  5567. if ($course_field_name) {
  5568. $externalCourseId = CourseManager::get_course_extra_field_value(
  5569. $course_field_name,
  5570. $course['code']
  5571. );
  5572. }
  5573. $course_tmp['external_course_id'] = $externalCourseId;
  5574. $courses_result[] = $course_tmp;
  5575. }
  5576. return $courses_result;
  5577. }
  5578. /* Get user api key */
  5579. $server->wsdl->addComplexType(
  5580. 'userApiKey',
  5581. 'complexType',
  5582. 'struct',
  5583. 'all',
  5584. '',
  5585. [
  5586. 'original_user_id_name' => ['name' => 'original_user_id_name', 'type' => 'xsd:string'],
  5587. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  5588. 'chamilo_username' => ['name' => 'chamilo_username', 'type' => 'xsd:string'],
  5589. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5590. ]
  5591. );
  5592. // Register the method to expose
  5593. $server->register(
  5594. 'WSUpdateUserApiKey', // method name
  5595. ['userApiKey' => 'tns:userApiKey'], // input parameters
  5596. ['return' => 'xsd:string'], // output parameters
  5597. 'urn:WSRegistration', // namespace
  5598. 'urn:WSRegistration#WSListCourses', // soapaction
  5599. 'rpc', // style
  5600. 'encoded', // use
  5601. 'This service return user api key' // documentation
  5602. );
  5603. /**
  5604. * @param array $params
  5605. *
  5606. * @return int|soap_fault|null
  5607. */
  5608. function WSUpdateUserApiKey($params)
  5609. {
  5610. if (!WSHelperVerifyKey($params)) {
  5611. return returnError(WS_ERROR_SECRET_KEY);
  5612. }
  5613. $user_id = UserManager::get_user_id_from_original_id(
  5614. $params['original_user_id_value'],
  5615. $params['original_user_id_name']
  5616. );
  5617. if (!$user_id) {
  5618. if (!empty($params['chamilo_username'])) {
  5619. $info = api_get_user_info_from_username($params['chamilo_username']);
  5620. $user_id = $info['user_id'];
  5621. // Save new fieldlabel into user_field table.
  5622. UserManager::create_extra_field($params['original_user_id_name'], 1, $params['original_user_id_name'], '');
  5623. // Save the external system's id into user_field_value table.
  5624. UserManager::update_extra_field_value(
  5625. $user_id,
  5626. $params['original_user_id_name'],
  5627. $params['original_user_id_value']
  5628. );
  5629. } else {
  5630. return 0;
  5631. }
  5632. }
  5633. $list = UserManager::get_api_keys($user_id);
  5634. $key_id = UserManager::get_api_key_id($user_id, 'dokeos');
  5635. if (isset($list[$key_id])) {
  5636. $apikey = $list[$key_id];
  5637. } else {
  5638. $lastid = UserManager::update_api_key($user_id, 'dokeos');
  5639. if ($lastid) {
  5640. $apikeys = UserManager::get_api_keys($user_id);
  5641. $apikey = $apikeys[$lastid];
  5642. }
  5643. }
  5644. return $apikey;
  5645. }
  5646. /** WSListSessions */
  5647. $server->wsdl->addComplexType(
  5648. 'session_arg',
  5649. 'complexType',
  5650. 'struct',
  5651. 'all',
  5652. '',
  5653. [
  5654. 'from' => ['name' => 'from', 'type' => 'xsd:int'],
  5655. 'to' => ['name' => 'to', 'type' => 'xsd:int'],
  5656. 'date_start' => ['name' => 'date_start', 'type' => 'xsd:string'],
  5657. 'date_end' => ['name' => 'date_end', 'type' => 'xsd:string'],
  5658. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5659. ]
  5660. );
  5661. $server->wsdl->addComplexType(
  5662. 'session',
  5663. 'complexType',
  5664. 'struct',
  5665. 'all',
  5666. '',
  5667. [
  5668. 'id' => ['name' => 'id', 'type' => 'xsd:int'],
  5669. 'title' => ['name' => 'title', 'type' => 'xsd:string'],
  5670. 'url' => ['name' => 'url', 'type' => 'xsd:string'],
  5671. 'date_start' => ['name' => 'date_start', 'type' => 'xsd:string'],
  5672. 'date_end' => ['name' => 'date_end', 'type' => 'xsd:string'],
  5673. ]
  5674. );
  5675. $server->wsdl->addComplexType(
  5676. 'sessions',
  5677. 'complexType',
  5678. 'array',
  5679. '',
  5680. 'SOAP-ENC:Array',
  5681. [],
  5682. [
  5683. ['ref' => 'SOAP-ENC:arrayType',
  5684. 'wsdl:arrayType' => 'tns:session[]', ],
  5685. ],
  5686. 'tns:session'
  5687. );
  5688. // Register the method to expose
  5689. $server->register(
  5690. 'WSListSessions', // method name
  5691. ['input' => 'tns:session_arg'], // input parameters
  5692. ['return' => 'tns:sessions'], // output parameters
  5693. 'urn:WSRegistration', // namespace
  5694. 'urn:WSRegistration#WSListSessions', // soapaction
  5695. 'rpc', // style
  5696. 'encoded', // use
  5697. 'This service returns a list of sessions' // documentation
  5698. );
  5699. /**
  5700. * Get a list of sessions (id, title, url, date_start, date_end) and
  5701. * return to caller. Date start can be set to ask only for the sessions
  5702. * starting at or after this date. Date end can be set to ask only for the
  5703. * sessions ending before or at this date.
  5704. * Function registered as service. Returns strings in UTF-8.
  5705. *
  5706. * @param array List of parameters (security key, date_start and date_end)
  5707. *
  5708. * @return array Sessions list (id=>[title=>'title',url='http://...',date_start=>'...',date_end=>''])
  5709. */
  5710. function WSListSessions($params)
  5711. {
  5712. if (!WSHelperVerifyKey($params)) {
  5713. return returnError(WS_ERROR_SECRET_KEY);
  5714. }
  5715. $sql_params = [];
  5716. // Dates should be provided in YYYY-MM-DD format, UTC
  5717. if (!empty($params['date_start'])) {
  5718. $sql_params['s.access_start_date'] = ['operator' => '>=', 'value' => $params['date_start']];
  5719. }
  5720. if (!empty($params['date_end'])) {
  5721. $sql_params['s.access_end_date'] = ['operator' => '<=', 'value' => $params['date_end']];
  5722. }
  5723. $from = isset($params['from']) ? $params['from'] : null;
  5724. $to = isset($params['to']) ? $params['to'] : null;
  5725. $sessions_list = SessionManager::get_sessions_list($sql_params, null, $from, $to);
  5726. $return_list = [];
  5727. foreach ($sessions_list as $session) {
  5728. $return_list[] = [
  5729. 'id' => $session['id'],
  5730. 'title' => $session['name'],
  5731. // something like http://my.chamilo.net/main/session/index.php?session_id=5
  5732. 'url' => api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$session['id'],
  5733. 'date_start' => $session['access_start_date'],
  5734. 'date_end' => $session['access_end_date'],
  5735. ];
  5736. }
  5737. return $return_list;
  5738. }
  5739. /* Register WSUserSubscribedInCourse function */
  5740. // Register the data structures used by the service
  5741. //prepare input params
  5742. // Input params for editing users
  5743. $server->wsdl->addComplexType(
  5744. 'UserSubscribedInCourse',
  5745. 'complexType',
  5746. 'struct',
  5747. 'all',
  5748. '',
  5749. [
  5750. 'course' => ['name' => 'course', 'type' => 'xsd:string'],
  5751. //Course string code
  5752. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  5753. //Chamilo user_id
  5754. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5755. ]
  5756. );
  5757. // Register the method to expose
  5758. $server->register(
  5759. 'WSUserSubscribedInCourse', // method name
  5760. ['UserSubscribedInCourse' => 'tns:UserSubscribedInCourse'], // input parameters
  5761. ['return' => 'xsd:string'], // output parameters
  5762. 'urn:WSRegistration', // namespace
  5763. 'urn:WSRegistration#WSUserSubscribedInCourse', // soapaction
  5764. 'rpc', // style
  5765. 'encoded', // use
  5766. 'This service checks if user assigned to course' // documentation
  5767. );
  5768. /**
  5769. * Web service to tell if a given user is subscribed to the course.
  5770. *
  5771. * @param array $params Array of parameters (course and user_id)
  5772. *
  5773. * @return bool|soap_fault|null A simple boolean (true if user is subscribed, false otherwise)
  5774. */
  5775. function WSUserSubscribedInCourse($params)
  5776. {
  5777. global $debug;
  5778. if ($debug) {
  5779. error_log('WSUserSubscribedInCourse');
  5780. }
  5781. if ($debug) {
  5782. error_log('Params '.print_r($params, 1));
  5783. }
  5784. if (!WSHelperVerifyKey($params)) {
  5785. return returnError(WS_ERROR_SECRET_KEY);
  5786. }
  5787. $courseCode = $params['course']; //Course code
  5788. $userId = $params['user_id']; //chamilo user id
  5789. return CourseManager::is_user_subscribed_in_course($userId, $courseCode);
  5790. }
  5791. /* Search session Web Service start */
  5792. // Input params for WSSearchSession
  5793. $server->wsdl->addComplexType(
  5794. 'SearchSession',
  5795. 'complexType',
  5796. 'struct',
  5797. 'all',
  5798. '',
  5799. [
  5800. 'term' => ['name' => 'term', 'type' => 'xsd:string'],
  5801. 'extrafields' => ['name' => 'extrafields', 'type' => 'xsd:string'],
  5802. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5803. ]
  5804. );
  5805. //Output params for WSSearchSession
  5806. $server->wsdl->addComplexType(
  5807. 'searchedSessionExtra',
  5808. 'complexType',
  5809. 'struct',
  5810. 'all',
  5811. '',
  5812. [
  5813. 'variable' => ['name' => 'variable', 'type' => 'xsd:string'],
  5814. 'value' => ['name' => 'value', 'type' => 'xsd:string'],
  5815. ]
  5816. );
  5817. $server->wsdl->addComplexType(
  5818. 'searchedSessionExtras',
  5819. 'complexType',
  5820. 'array',
  5821. '',
  5822. 'SOAP-ENC:Array',
  5823. [],
  5824. [
  5825. ['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:searchedSessionExtra[]'],
  5826. ],
  5827. 'tns:searchedSessionExtra'
  5828. );
  5829. $server->wsdl->addComplexType(
  5830. 'searchedSession',
  5831. 'complexType',
  5832. 'struct',
  5833. 'all',
  5834. '',
  5835. [
  5836. 'id' => ['name' => 'id', 'type' => 'xsd:int'],
  5837. 'id_coach' => ['name' => 'id_coach', 'type' => 'xsd:int'],
  5838. 'name' => ['name' => 'name', 'type' => 'xsd:string'],
  5839. 'nbr_courses' => ['name' => 'nbr_courses', 'type' => 'xsd:int'],
  5840. 'nbr_users' => ['name' => 'nbr_users', 'type' => 'xsd:int'],
  5841. 'nbr_classes' => ['name' => 'nbr_classes', 'type' => 'xsd:int'],
  5842. 'date_start' => ['name' => 'date_start', 'type' => 'xsd:string'],
  5843. 'date_end' => ['name' => 'date_end', 'type' => 'xsd:string'],
  5844. 'nb_days_access_before_beginning' => ['name' => 'nb_days_access_before_beginning', 'type' => 'xsd:int'],
  5845. 'nb_days_access_after_end' => ['nb_days_access_after_end' => 'duration', 'type' => 'xsd:int'],
  5846. 'session_admin_id' => ['session_admin_id' => 'duration', 'type' => 'xsd:int'],
  5847. 'visibility' => ['visibility' => 'duration', 'type' => 'xsd:int'],
  5848. 'session_category_id' => ['session_category_id' => 'duration', 'type' => 'xsd:int'],
  5849. 'promotion_id' => ['promotion_id' => 'duration', 'type' => 'xsd:int'],
  5850. 'description' => ['name' => 'description', 'type' => 'xsd:string'],
  5851. 'show_description' => ['name' => 'description', 'type' => 'xsd:int'],
  5852. 'duration' => ['name' => 'duration', 'type' => 'xsd:string'],
  5853. 'extra' => ['name' => 'extra', 'type' => 'tns:searchedSessionExtras'],
  5854. ]
  5855. );
  5856. $server->wsdl->addComplexType(
  5857. 'searchedSessionList',
  5858. 'complexType',
  5859. 'array',
  5860. '',
  5861. 'SOAP-ENC:Array',
  5862. [],
  5863. [
  5864. ['ref' => 'SOAP-ENC:arrayType',
  5865. 'wsdl:arrayType' => 'tns:searchedSession[]', ],
  5866. ],
  5867. 'tns:searchedSession'
  5868. );
  5869. //Reister WSSearchSession
  5870. $server->register(
  5871. 'WSSearchSession',
  5872. ['SearchSession' => 'tns:SearchSession'], // input parameters
  5873. ['return' => 'tns:searchedSessionList'], // output parameters
  5874. 'urn:WSRegistration', // namespace
  5875. 'urn:WSRegistration#WSSearchSession', // soapaction
  5876. 'rpc', // style
  5877. 'encoded', // use
  5878. 'This service to get a session list filtered by name, description or short description extra field' // documentation
  5879. );
  5880. /**
  5881. * Web service to get a session list filtered by name, description or short description extra field.
  5882. *
  5883. * @param array $params Contains the following parameters
  5884. * string $params['term'] Search term
  5885. * string $params['extrafields'] Extrafields to include in request result
  5886. * string $params['secret_key'] Secret key to check
  5887. *
  5888. * @return array The list
  5889. */
  5890. function WSSearchSession($params)
  5891. {
  5892. if (!WSHelperVerifyKey($params['secret_key'])) {
  5893. return returnError(WS_ERROR_SECRET_KEY);
  5894. }
  5895. $fieldsToInclude = [];
  5896. if (!empty($params['extrafields'])) {
  5897. $fieldsToInclude = explode(',', $params['extrafields']);
  5898. foreach ($fieldsToInclude as &$field) {
  5899. if (empty($field)) {
  5900. continue;
  5901. }
  5902. $field = trim($field);
  5903. }
  5904. }
  5905. return SessionManager::searchSession($params['term'], $fieldsToInclude);
  5906. }
  5907. /* Search session Web Service end */
  5908. /* Fetch session Web Service start */
  5909. // Input params for WSFetchSession
  5910. $server->wsdl->addComplexType(
  5911. 'FetchSession',
  5912. 'complexType',
  5913. 'struct',
  5914. 'all',
  5915. '',
  5916. [
  5917. 'id' => ['name' => 'id', 'type' => 'xsd:int'],
  5918. 'extrafields' => ['name' => 'extrafields', 'type' => 'xsd:string'],
  5919. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  5920. ]
  5921. );
  5922. //Reister WSFetchSession
  5923. $server->register(
  5924. 'WSFetchSession',
  5925. ['SearchSession' => 'tns:FetchSession'], // input parameters
  5926. ['return' => 'tns:searchedSessionList'], // output parameters
  5927. 'urn:WSRegistration', // namespace
  5928. 'urn:WSRegistration#WSFetchSession', // soapaction
  5929. 'rpc', // style
  5930. 'encoded', // use
  5931. 'This service get a session by its id. Optionally can get its extra fields values' // documentation
  5932. );
  5933. /**
  5934. * Web service to get a session by its id. Optionally can get its extra fields values.
  5935. *
  5936. * @param array $params Contains the following parameters:
  5937. * int $params['id'] The session id
  5938. * string $params['extrafields'] Extrafields to include in request result
  5939. * string $params['secret_key'] Secret key to check
  5940. *
  5941. * @return array The session data
  5942. */
  5943. function WSFetchSession($params)
  5944. {
  5945. if (!WSHelperVerifyKey($params['secret_key'])) {
  5946. return returnError(WS_ERROR_SECRET_KEY);
  5947. }
  5948. $fieldsToInclude = explode(',', $params['extrafields']);
  5949. foreach ($fieldsToInclude as &$field) {
  5950. if (empty($field)) {
  5951. continue;
  5952. }
  5953. $field = trim($field);
  5954. }
  5955. $sessionData = SessionManager::fetch($params['id']);
  5956. if ($sessionData === false) {
  5957. return returnError(WS_ERROR_INVALID_INPUT);
  5958. }
  5959. if (!empty($extraFields)) {
  5960. $sessionData['extra'] = SessionManager::getFilteredExtraFields($params['id'], $fieldsToInclude);
  5961. }
  5962. return [$sessionData];
  5963. }
  5964. /* Fetch session Web Service end */
  5965. /* Register WSCertificatesList function */
  5966. // Register the data structures used by the service
  5967. $server->wsdl->addComplexType(
  5968. 'certificateDetails',
  5969. 'complexType',
  5970. 'struct',
  5971. 'all',
  5972. '',
  5973. [
  5974. 'id' => ['name' => 'id', 'type' => 'xsd:int'],
  5975. 'username' => ['name' => 'username', 'type' => 'xsd:string'],
  5976. 'course_code' => ['name' => 'course_code', 'type' => 'xsd:string'],
  5977. 'session_id' => ['name' => 'session_id', 'type' => 'xsd:int'],
  5978. 'cat_id' => ['name' => 'cat_id', 'type' => 'xsd:int'],
  5979. 'created_at' => ['name' => 'created_at', 'type' => 'xsd:string'],
  5980. 'path_certificate' => ['name' => 'path_certificate', 'type' => 'xsd:string'],
  5981. ]
  5982. );
  5983. $server->wsdl->addComplexType(
  5984. 'certificatesList',
  5985. 'complexType',
  5986. 'array',
  5987. '',
  5988. 'SOAP-ENC:Array',
  5989. [],
  5990. [
  5991. ['ref' => 'SOAP-ENC:arrayType',
  5992. 'wsdl:arrayType' => 'tns:certificateDetails[]', ],
  5993. ],
  5994. 'tns:certificateDetails'
  5995. );
  5996. // Register the method to expose
  5997. $server->register(
  5998. 'WSCertificatesList', // method name
  5999. [
  6000. 'startingDate' => 'xsd:string', // input parameters
  6001. 'endingDate' => 'xsd:string',
  6002. ],
  6003. ['return' => 'tns:certificatesList'], // output parameters
  6004. 'urn:WSRegistration', // namespace
  6005. 'urn:WSRegistration#WSCertificatesList', // soapaction
  6006. 'rpc', // style
  6007. 'encoded', // use
  6008. 'This service returns a list of certificates' // documentation
  6009. );
  6010. function WSCertificatesList($startingDate = '', $endingDate = '')
  6011. {
  6012. $certificatesCron = api_get_setting('add_gradebook_certificates_cron_task_enabled');
  6013. if ($certificatesCron === 'true') {
  6014. require_once api_get_path(SYS_CODE_PATH).'cron/add_gradebook_certificates.php';
  6015. }
  6016. $result = [];
  6017. $certificateTable = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  6018. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  6019. $categoryTable = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  6020. $query = "SELECT
  6021. certificate.id,
  6022. user.username,
  6023. category.course_code,
  6024. category.session_id,
  6025. certificate.user_id,
  6026. certificate.cat_id,
  6027. certificate.created_at,
  6028. certificate.path_certificate
  6029. FROM $certificateTable AS certificate
  6030. JOIN $userTable AS user
  6031. ON certificate.user_id = user.user_id
  6032. JOIN $categoryTable AS category
  6033. ON certificate.cat_id = category.id";
  6034. if (!empty($startingDate) && !empty($endingDate)) {
  6035. $query .= " WHERE certificate.created_at BETWEEN '$startingDate' AND '$endingDate'";
  6036. } elseif (!empty($startingDate)) {
  6037. $query .= " WHERE certificate.created_at >= '$startingDate'";
  6038. } elseif (!empty($endingDate)) {
  6039. $query .= " WHERE certificate.created_at <= '$endingDate'";
  6040. }
  6041. $queryResult = Database::query($query);
  6042. while ($row = Database::fetch_array($queryResult)) {
  6043. $userPath = USermanager::getUserPathById($row['user_id'], 'web');
  6044. $row['path_certificate'] = $userPath.'/certificate'.$row['path_certificate'];
  6045. $result[] = $row;
  6046. }
  6047. return $result;
  6048. }
  6049. /* Create group Web Service start */
  6050. // Register the data structures used by the service
  6051. // Input params for WSCreateGroup
  6052. $server->wsdl->addComplexType(
  6053. 'createGroup',
  6054. 'complexType',
  6055. 'struct',
  6056. 'all',
  6057. '',
  6058. [
  6059. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6060. 'name' => ['name' => 'name', 'type' => 'xsd:string'],
  6061. ]
  6062. );
  6063. // Register the method to expose
  6064. $server->register(
  6065. 'WSCreateGroup', // method name
  6066. ['createGroup' => 'tns:createGroup'], // input parameters
  6067. ['return' => 'xsd:string'], // output parameters
  6068. 'urn:WSRegistration', // namespace
  6069. 'urn:WSRegistration#WSCreateGroup', // soapaction
  6070. 'rpc', // style
  6071. 'encoded', // use
  6072. 'This service adds a group' // documentation
  6073. );
  6074. // Define the method WSCreateGroup
  6075. function WSCreateGroup($params)
  6076. {
  6077. if (!WSHelperVerifyKey($params['secret_key'])) {
  6078. return returnError(WS_ERROR_SECRET_KEY);
  6079. }
  6080. $userGroup = new UserGroup();
  6081. $params = [
  6082. 'name' => $params['name'],
  6083. ];
  6084. return $userGroup->save($params);
  6085. }
  6086. /* Create group Web Service end */
  6087. /* Update group Web Service start */
  6088. // Register the data structures used by the service
  6089. // Input params for WSUpdateGroup
  6090. $server->wsdl->addComplexType(
  6091. 'updateGroup',
  6092. 'complexType',
  6093. 'struct',
  6094. 'all',
  6095. '',
  6096. [
  6097. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6098. 'id' => ['name' => 'id', 'type' => 'xsd:string'],
  6099. 'name' => ['name' => 'name', 'type' => 'xsd:string'],
  6100. 'description' => ['name' => 'description', 'type' => 'xsd:string'],
  6101. 'url' => ['name' => 'url', 'type' => 'xsd:string'],
  6102. 'visibility' => ['name' => 'visibility', 'type' => 'xsd:string'],
  6103. 'picture_uri' => ['name' => 'picture_uri', 'type' => 'xsd:string'],
  6104. 'allow_member_group_to_leave' => ['name' => 'allow_member_group_to_leave', 'type' => 'xsd:string'],
  6105. ]
  6106. );
  6107. // Register the method to expose
  6108. $server->register(
  6109. 'WSUpdateGroup', // method name
  6110. ['updateGroup' => 'tns:updateGroup'], // input parameters
  6111. ['return' => 'xsd:string'], // output parameters
  6112. 'urn:WSRegistration', // namespace
  6113. 'urn:WSRegistration#WSUpdateGroup', // soapaction
  6114. 'rpc', // style
  6115. 'encoded', // use
  6116. 'This service updates a group' // documentation
  6117. );
  6118. // Define the method WSUpdateGroup
  6119. function WSUpdateGroup($params)
  6120. {
  6121. if (!WSHelperVerifyKey($params['secret_key'])) {
  6122. return returnError(WS_ERROR_SECRET_KEY);
  6123. }
  6124. $params['allow_member_group_to_leave'] = null;
  6125. $userGroup = new UserGroup();
  6126. return $userGroup->update($params);
  6127. }
  6128. /* Update group Web Service end */
  6129. /* Delete group Web Service start */
  6130. // Register the data structures used by the service
  6131. // Input params for WSDeleteGroup
  6132. $server->wsdl->addComplexType(
  6133. 'deleteGroup',
  6134. 'complexType',
  6135. 'struct',
  6136. 'all',
  6137. '',
  6138. [
  6139. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6140. 'id' => ['name' => 'id', 'type' => 'xsd:string'],
  6141. ]
  6142. );
  6143. // Register the method to expose
  6144. $server->register(
  6145. 'WSDeleteGroup', // method name
  6146. ['deleteGroup' => 'tns:deleteGroup'], // input parameters
  6147. ['return' => 'xsd:string'], // output parameters
  6148. 'urn:WSRegistration', // namespace
  6149. 'urn:WSRegistration#WSDeleteGroup', // soapaction
  6150. 'rpc', // style
  6151. 'encoded', // use
  6152. 'This service deletes a group' // documentation
  6153. );
  6154. // Define the method WSDeleteGroup
  6155. function WSDeleteGroup($params)
  6156. {
  6157. if (!WSHelperVerifyKey($params['secret_key'])) {
  6158. return returnError(WS_ERROR_SECRET_KEY);
  6159. }
  6160. $userGroup = new UserGroup();
  6161. return $userGroup->delete($params['id']);
  6162. }
  6163. /* Delete group Web Service end */
  6164. /* Bind group to parent Web Service start */
  6165. // Register the data structures used by the service
  6166. // Input params for GroupBindToParent
  6167. $server->wsdl->addComplexType(
  6168. 'groupBindToParent',
  6169. 'complexType',
  6170. 'struct',
  6171. 'all',
  6172. '',
  6173. [
  6174. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6175. 'id' => ['name' => 'id', 'type' => 'xsd:string'],
  6176. 'parent_id' => ['name' => 'parent_id', 'type' => 'xsd:string'],
  6177. ]
  6178. );
  6179. // Register the method to expose
  6180. $server->register(
  6181. 'GroupBindToParent', // method name
  6182. ['groupBindToParent' => 'tns:groupBindToParent'], // input parameters
  6183. ['return' => 'xsd:string'], // output parameters
  6184. 'urn:WSRegistration', // namespace
  6185. 'urn:WSRegistration#GroupBindToParent', // soapaction
  6186. 'rpc', // style
  6187. 'encoded', // use
  6188. 'This service binds a group to a parent' // documentation
  6189. );
  6190. // Define the method GroupBindToParent
  6191. function GroupBindToParent($params)
  6192. {
  6193. if (!WSHelperVerifyKey($params['secret_key'])) {
  6194. return returnError(WS_ERROR_SECRET_KEY);
  6195. }
  6196. $userGroup = new UserGroup();
  6197. return $userGroup->setParentGroup($params['id'], $params['parent_id']);
  6198. }
  6199. /* Bind group Web Service end */
  6200. /* Unbind group from parent Web Service start */
  6201. // Register the data structures used by the service
  6202. // Input params for GroupUnbindFromParent
  6203. $server->wsdl->addComplexType(
  6204. 'groupUnbindFromParent',
  6205. 'complexType',
  6206. 'struct',
  6207. 'all',
  6208. '',
  6209. [
  6210. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6211. 'id' => ['name' => 'id', 'type' => 'xsd:string'],
  6212. ]
  6213. );
  6214. // Register the method to expose
  6215. $server->register(
  6216. 'GroupUnbindFromParent', // method name
  6217. ['groupUnbindFromParent' => 'tns:groupUnbindFromParent'], // input parameters
  6218. ['return' => 'xsd:string'], // output parameters
  6219. 'urn:WSRegistration', // namespace
  6220. 'urn:WSRegistration#GroupUnbindFromParent', // soapaction
  6221. 'rpc', // style
  6222. 'encoded', // use
  6223. 'This service unbinds a group from its parent' // documentation
  6224. );
  6225. // Define the method GroupUnbindFromParent
  6226. function GroupUnbindFromParent($params)
  6227. {
  6228. if (!WSHelperVerifyKey($params['secret_key'])) {
  6229. return returnError(WS_ERROR_SECRET_KEY);
  6230. }
  6231. $userGroup = new UserGroup();
  6232. return $userGroup->setParentGroup($params['id'], 0);
  6233. }
  6234. /* Unbind group Web Service end */
  6235. /* Add user to group Web Service start */
  6236. // Register the data structures used by the service
  6237. // Input params for WSAddUserToGroup
  6238. $server->wsdl->addComplexType(
  6239. 'addUserToGroup',
  6240. 'complexType',
  6241. 'struct',
  6242. 'all',
  6243. '',
  6244. [
  6245. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6246. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  6247. 'group_id' => ['name' => 'group_id', 'type' => 'xsd:string'],
  6248. 'relation_type' => ['name' => 'relation_type', 'type' => 'xsd:string'],
  6249. ]
  6250. );
  6251. // Register the method to expose
  6252. $server->register(
  6253. 'WSAddUserToGroup', // method name
  6254. ['addUserToGroup' => 'tns:addUserToGroup'], // input parameters
  6255. ['return' => 'xsd:string'], // output parameters
  6256. 'urn:WSRegistration', // namespace
  6257. 'urn:WSRegistration#WSAddUserToGroup', // soapaction
  6258. 'rpc', // style
  6259. 'encoded', // use
  6260. 'This service adds a user to a group' // documentation
  6261. );
  6262. // Define the method WSAddUserToGroup
  6263. function WSAddUserToGroup($params)
  6264. {
  6265. if (!WSHelperVerifyKey($params['secret_key'])) {
  6266. return returnError(WS_ERROR_SECRET_KEY);
  6267. }
  6268. $userGroup = new UserGroup();
  6269. return $userGroup->subscribe_users_to_usergroup(
  6270. $params['group_id'],
  6271. [0 => $params['user_id']],
  6272. false,
  6273. $params['relation_type']
  6274. );
  6275. }
  6276. /* Add user to group Web Service end */
  6277. /* Update user role in group Web Service start */
  6278. // Register the data structures used by the service
  6279. // Input params for WSUpdateUserRoleInGroup
  6280. $server->wsdl->addComplexType(
  6281. 'updateUserRoleInGroup',
  6282. 'complexType',
  6283. 'struct',
  6284. 'all',
  6285. '',
  6286. [
  6287. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6288. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  6289. 'group_id' => ['name' => 'group_id', 'type' => 'xsd:string'],
  6290. 'relation_type' => ['name' => 'relation_type', 'type' => 'xsd:string'],
  6291. ]
  6292. );
  6293. // Register the method to expose
  6294. $server->register(
  6295. 'WSUpdateUserRoleInGroup', // method name
  6296. ['updateUserRoleInGroup' => 'tns:updateUserRoleInGroup'], // input parameters
  6297. ['return' => 'xsd:string'], // output parameters
  6298. 'urn:WSRegistration', // namespace
  6299. 'urn:WSRegistration#WSUpdateUserRoleInGroup', // soapaction
  6300. 'rpc', // style
  6301. 'encoded', // use
  6302. 'This service updates a user role in group' // documentation
  6303. );
  6304. // Define the method WSUpdateUserRoleInGroup
  6305. function WSUpdateUserRoleInGroup($params)
  6306. {
  6307. if (!WSHelperVerifyKey($params['secret_key'])) {
  6308. return returnError(WS_ERROR_SECRET_KEY);
  6309. }
  6310. $userGroup = new UserGroup();
  6311. return $userGroup->update_user_role(
  6312. $params['user_id'],
  6313. $params['group_id'],
  6314. $params['relation_type']
  6315. );
  6316. }
  6317. /* Update user role Web Service end */
  6318. /* Delete user from group Web Service start */
  6319. // Register the data structures used by the service
  6320. // Input params for WSDeleteUserFromGroup
  6321. $server->wsdl->addComplexType(
  6322. 'deleteUserFromGroup',
  6323. 'complexType',
  6324. 'struct',
  6325. 'all',
  6326. '',
  6327. [
  6328. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6329. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  6330. 'group_id' => ['name' => 'group_id', 'type' => 'xsd:string'],
  6331. ]
  6332. );
  6333. // Register the method to expose
  6334. $server->register(
  6335. 'WSDeleteUserFromGroup', // method name
  6336. ['deleteUserFromGroup' => 'tns:deleteUserFromGroup'], // input parameters
  6337. ['return' => 'xsd:string'], // output parameters
  6338. 'urn:WSRegistration', // namespace
  6339. 'urn:WSRegistration#WSDeleteUserFromGroup', // soapaction
  6340. 'rpc', // style
  6341. 'encoded', // use
  6342. 'This service deletes a user from a group' // documentation
  6343. );
  6344. // Define the method WSDeleteUserFromGroup
  6345. function WSDeleteUserFromGroup($params)
  6346. {
  6347. if (!WSHelperVerifyKey($params['secret_key'])) {
  6348. return returnError(WS_ERROR_SECRET_KEY);
  6349. }
  6350. $userGroup = new UserGroup();
  6351. return $userGroup->delete_user_rel_group(
  6352. $params['user_id'],
  6353. $params['group_id']
  6354. );
  6355. }
  6356. /* Delete user from group Web Service end */
  6357. /** WSRegisterUserVisibilityToCourseCatalogue */
  6358. // Register the data structures used by the service
  6359. $server->wsdl->addComplexType(
  6360. 'user_course_visibility',
  6361. 'complexType',
  6362. 'struct',
  6363. 'all',
  6364. '',
  6365. [
  6366. 'course_id' => ['name' => 'course_id', 'type' => 'tns:course_id'],
  6367. 'user_id' => ['name' => 'user_id', 'type' => 'tns:user_id'],
  6368. 'visible' => ['name' => 'status', 'type' => 'xsd:int'],
  6369. ]
  6370. );
  6371. $server->wsdl->addComplexType(
  6372. 'user_course_visibility_array',
  6373. 'complexType',
  6374. 'array',
  6375. '',
  6376. 'SOAP-ENC:Array',
  6377. [],
  6378. [
  6379. ['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user_course_visibility[]'],
  6380. ],
  6381. 'tns:user_course_visibility'
  6382. );
  6383. $server->wsdl->addComplexType(
  6384. 'registerUserToCourseCatalogue_arg',
  6385. 'complexType',
  6386. 'struct',
  6387. 'all',
  6388. '',
  6389. [
  6390. 'userscourses' => ['name' => 'userscourses', 'type' => 'tns:user_course_visibility_array'],
  6391. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  6392. ]
  6393. );
  6394. $server->wsdl->addComplexType(
  6395. 'registerUserToCourseCatalogue_return',
  6396. 'complexType',
  6397. 'struct',
  6398. 'all',
  6399. '',
  6400. [
  6401. 'original_user_id_value' => ['name' => 'original_user_id_value', 'type' => 'xsd:string'],
  6402. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  6403. 'visible' => ['name' => 'visible', 'type' => 'xsd:int'],
  6404. 'result' => ['name' => 'result', 'type' => 'xsd:int'],
  6405. ]
  6406. );
  6407. $server->wsdl->addComplexType(
  6408. 'registerUserToCourseCatalogue_return_global',
  6409. 'complexType',
  6410. 'array',
  6411. '',
  6412. 'SOAP-ENC:Array',
  6413. [],
  6414. [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:registerUserToCourseCatalogue_return[]']],
  6415. 'tns:registerUserToCourseCatalogue_return'
  6416. );
  6417. // Register the method to expose
  6418. $server->register(
  6419. 'WSAddUserVisibilityToCourseInCatalogue', // method name
  6420. ['registerUserToCourseCatalogue' => 'tns:registerUserToCourseCatalogue_arg'], // input parameters
  6421. ['return' => 'tns:registerUserToCourseCatalogue_return_global'],
  6422. 'urn:WSRegistration', // namespace
  6423. 'urn:WSRegistration#WSRegisterUserVisibilityToCourseCatalogue', // soapaction
  6424. 'rpc', // style
  6425. 'encoded', // use
  6426. 'This service registers the visibility of users to course in catalogue' // documentation
  6427. );
  6428. // define the method WSRegisterUserVisibilityToCourseInCatalogue
  6429. function WSAddUserVisibilityToCourseInCatalogue($params)
  6430. {
  6431. global $debug;
  6432. if (!WSHelperVerifyKey($params)) {
  6433. return returnError(WS_ERROR_SECRET_KEY);
  6434. }
  6435. if ($debug) {
  6436. error_log('WSAddUserVisibilityToCourseCatalogue params: '.print_r($params, 1));
  6437. }
  6438. $results = [];
  6439. $userscourses = $params['userscourses'];
  6440. foreach ($userscourses as $usercourse) {
  6441. $original_course_id = $usercourse['course_id'];
  6442. $original_user_id = $usercourse['user_id'];
  6443. $visible = $usercourse['visible'];
  6444. $resultValue = 0;
  6445. // Get user id
  6446. $userId = UserManager::get_user_id_from_original_id(
  6447. $original_user_id['original_user_id_value'],
  6448. $original_user_id['original_user_id_name']
  6449. );
  6450. if ($debug) {
  6451. error_log('WSAddUserVisibilityToCourseCatalogue userId: '.$userId);
  6452. }
  6453. if ($userId == 0) {
  6454. // If user was not found, there was a problem
  6455. $resultValue = 0;
  6456. } else {
  6457. // User was found
  6458. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  6459. $original_course_id['original_course_id_value'],
  6460. $original_course_id['original_course_id_name']
  6461. );
  6462. $courseCode = $courseInfo['code'];
  6463. if (empty($courseCode)) {
  6464. // Course was not found
  6465. $resultValue = 0;
  6466. } else {
  6467. if ($debug) {
  6468. error_log('WSAddUserVisibilityToCourseCatalogue courseCode: '.$courseCode);
  6469. }
  6470. $result = CourseManager::addUserVisibilityToCourseInCatalogue($userId, $courseCode, $visible);
  6471. if ($result) {
  6472. $resultValue = 1;
  6473. if ($debug) {
  6474. error_log('WSAddUserVisibilityToCourseCatalogue registered');
  6475. }
  6476. } else {
  6477. if ($debug) {
  6478. error_log('WSAddUserVisibilityToCourseCatalogue NOT registered: ');
  6479. }
  6480. }
  6481. }
  6482. }
  6483. $results[] = [
  6484. 'original_user_id_value' => $original_user_id['original_user_id_value'],
  6485. 'original_course_id_value' => $original_course_id['original_course_id_value'],
  6486. 'visible' => $visible,
  6487. 'result' => $resultValue,
  6488. ];
  6489. }
  6490. return $results;
  6491. }
  6492. // Register the method to expose
  6493. $server->register(
  6494. 'WSRemoveUserVisibilityToCourseInCatalogue', // method name
  6495. ['registerUserToCourseCatalogue' => 'tns:registerUserToCourseCatalogue_arg'], // input parameters
  6496. ['return' => 'tns:registerUserToCourseCatalogue_return_global'],
  6497. 'urn:WSRegistration', // namespace
  6498. 'urn:WSRegistration#WSRegisterUserVisibilityToCourseCatalogue', // soapaction
  6499. 'rpc', // style
  6500. 'encoded', // use
  6501. 'This service removes the visibility of users to course in catalogue' // documentation
  6502. );
  6503. // define the method WSRemoveUserVisibilityToCourseInCatalogue
  6504. function WSRemoveUserVisibilityToCourseInCatalogue($params)
  6505. {
  6506. global $debug;
  6507. if (!WSHelperVerifyKey($params)) {
  6508. return returnError(WS_ERROR_SECRET_KEY);
  6509. }
  6510. if ($debug) {
  6511. error_log('WSRemoveUserVisibilityToCourseInCatalogue params: '.print_r($params, 1));
  6512. }
  6513. $results = [];
  6514. $userscourses = $params['userscourses'];
  6515. foreach ($userscourses as $usercourse) {
  6516. $original_course_id = $usercourse['course_id'];
  6517. $original_user_id = $usercourse['user_id'];
  6518. $visible = $usercourse['visible'];
  6519. $resultValue = 0;
  6520. // Get user id
  6521. $userId = UserManager::get_user_id_from_original_id(
  6522. $original_user_id['original_user_id_value'],
  6523. $original_user_id['original_user_id_name']
  6524. );
  6525. if ($debug) {
  6526. error_log('WSRemoveUserVisibilityToCourseInCatalogue user_id: '.$userId);
  6527. }
  6528. if ($userId == 0) {
  6529. // If user was not found, there was a problem
  6530. $resultValue = 0;
  6531. } else {
  6532. // User was found
  6533. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  6534. $original_course_id['original_course_id_value'],
  6535. $original_course_id['original_course_id_name']
  6536. );
  6537. $courseCode = $courseInfo['code'];
  6538. if (empty($courseCode)) {
  6539. // Course was not found
  6540. $resultValue = 0;
  6541. } else {
  6542. if ($debug) {
  6543. error_log('WSRemoveUserVisibilityToCourseInCatalogue courseCode: '.$courseCode);
  6544. }
  6545. $result = CourseManager::removeUserVisibilityToCourseInCatalogue($userId, $courseCode, $visible);
  6546. if ($result) {
  6547. $resultValue = 1;
  6548. if ($debug) {
  6549. error_log('WSRemoveUserVisibilityToCourseInCatalogue removed');
  6550. }
  6551. } else {
  6552. if ($debug) {
  6553. error_log('WSRemoveUserVisibilityToCourseInCatalogue NOT removed: ');
  6554. }
  6555. }
  6556. }
  6557. }
  6558. $results[] = [
  6559. 'original_user_id_value' => $original_user_id['original_user_id_value'],
  6560. 'original_course_id_value' => $original_course_id['original_course_id_value'],
  6561. 'visible' => $visible,
  6562. 'result' => $resultValue,
  6563. ];
  6564. }
  6565. return $results;
  6566. }
  6567. // Add more webservices through hooks from plugins
  6568. if (!empty($hook)) {
  6569. $hook->setEventData(['server' => $server]);
  6570. $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_POST);
  6571. if (!empty($res['server'])) {
  6572. $server = $res['server'];
  6573. }
  6574. }
  6575. // Use the request to (try to) invoke the service
  6576. $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
  6577. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  6578. // If you send your data in utf8 then this value must be false.
  6579. $decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8');
  6580. if ($decodeUTF8 === 'true') {
  6581. $server->decode_utf8 = true;
  6582. } else {
  6583. $server->decode_utf8 = false;
  6584. }
  6585. $server->service($HTTP_RAW_POST_DATA);