registration.soap.php 189 KB

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