registration.soap.php 248 KB

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