registration.soap.php 189 KB

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