registration.soap.php 239 KB

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