registration.soap.php 217 KB

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