registration.soap.php 201 KB

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