registration.soap.php 239 KB

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