registration.soap.php 240 KB

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