registration.soap.php 239 KB

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