registration.soap.php 235 KB

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