registration.soap.php 239 KB

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