registration.soap.php 199 KB

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