registration.soap.php 210 KB

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