registration.soap.php 193 KB

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