registration.soap.php 240 KB

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