registration.soap.php 186 KB

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