registration.soap.php 240 KB

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