registration.soap.php 236 KB

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