registration.soap.php 239 KB

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