registration.soap.php 239 KB

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