learnpath.class.php 454 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class defines the parent attributes and methods for Chamilo learnpaths and SCORM
  5. * learnpaths. It is used by the scorm class.
  6. *
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  10. */
  11. /**
  12. * Defines the learnpath parent class
  13. * @package chamilo.learnpath
  14. */
  15. class learnpath
  16. {
  17. public $attempt = 0; // The number for the current ID view.
  18. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  19. public $current; // Id of the current item the user is viewing.
  20. public $current_score; // The score of the current item.
  21. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  22. public $current_time_stop; // The time the user closed this resource.
  23. public $default_status = 'not attempted';
  24. public $encoding = 'UTF-8';
  25. public $error = '';
  26. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  27. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  28. public $index; // The index of the active learnpath_item in $ordered_items array.
  29. public $items = array();
  30. public $last; // item_id of last item viewed in the learning path.
  31. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  32. public $license; // Which license this course has been given - not used yet on 20060522.
  33. public $lp_id; // DB ID for this learnpath.
  34. public $lp_view_id; // DB ID for lp_view
  35. public $log_file; // File where to log learnpath API msg.
  36. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  37. public $message = '';
  38. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  39. public $name; // Learnpath name (they generally have one).
  40. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  41. public $path = ''; // Path inside the scorm directory (if scorm).
  42. public $theme; // The current theme of the learning path.
  43. public $preview_image; // The current image of the learning path.
  44. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  45. public $prevent_reinit = 1;
  46. // Describes the mode of progress bar display.
  47. public $seriousgame_mode = 0;
  48. public $progress_bar_mode = '%';
  49. // Percentage progress as saved in the db.
  50. public $progress_db = '0';
  51. public $proximity; // Wether the content is distant or local or unknown.
  52. public $refs_list = array(); //list of items by ref => db_id. Used only for prerequisites match.
  53. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  54. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  55. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  56. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  57. public $user_id; //ID of the user that is viewing/using the course
  58. public $update_queue = array();
  59. public $scorm_debug = 0;
  60. public $arrMenu = array(); // Array for the menu items.
  61. public $debug = 0; // Logging level.
  62. public $lp_session_id = 0;
  63. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  64. public $prerequisite = 0;
  65. public $use_max_score = 1; // 1 or 0
  66. public $subscribe_users = 0;
  67. public $created_on = '';
  68. public $modified_on = '';
  69. public $publicated_on = '';
  70. public $expired_on = '';
  71. public $ref = null;
  72. public $course_int_id;
  73. public $course_info = array();
  74. public function get_course_int_id()
  75. {
  76. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  77. }
  78. public function set_course_int_id($course_id)
  79. {
  80. return $this->course_int_id = intval($course_id);
  81. }
  82. /**
  83. * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
  84. * Also builds the list of items into $this->items.
  85. * @param string Course code
  86. * @param integer Learnpath ID
  87. * @param integer User ID
  88. * @return boolean True on success, false on error
  89. */
  90. public function __construct($course, $lp_id, $user_id)
  91. {
  92. $this->encoding = api_get_system_encoding(); // Chamilo 1.8.8: We intend always to use the system encoding.
  93. // Check course code.
  94. $course_id = api_get_course_int_id();
  95. if ($this->debug > 0) {
  96. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  97. }
  98. if (empty($course)) {
  99. $this->error = 'Course code is empty';
  100. return false;
  101. } else {
  102. $course_info = api_get_course_info($course);
  103. if (!empty($course_info)) {
  104. $this->cc = $course_info['code'];
  105. $this->course_info = $course_info;
  106. $course_id = $course_info['real_id'];
  107. } else {
  108. $this->error = 'Course code does not exist in database';
  109. return false;
  110. }
  111. }
  112. $this->set_course_int_id($course_id);
  113. // Check learnpath ID.
  114. if (empty($lp_id)) {
  115. $this->error = 'Learnpath ID is empty';
  116. return false;
  117. } else {
  118. // TODO: Make it flexible to use any course_code (still using env course code here).
  119. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  120. $lp_id = intval($lp_id);
  121. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id' AND c_id = $course_id";
  122. if ($this->debug > 2) {
  123. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  124. }
  125. $res = Database::query($sql);
  126. if (Database::num_rows($res) > 0) {
  127. $this->lp_id = $lp_id;
  128. $row = Database::fetch_array($res);
  129. $this->type = $row['lp_type'];
  130. $this->name = stripslashes($row['name']);
  131. //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
  132. $this->proximity = $row['content_local'];
  133. $this->theme = $row['theme'];
  134. $this->maker = $row['content_maker'];
  135. $this->prevent_reinit = $row['prevent_reinit'];
  136. $this->seriousgame_mode = $row['seriousgame_mode'];
  137. $this->license = $row['content_license'];
  138. $this->scorm_debug = $row['debug'];
  139. $this->js_lib = $row['js_lib'];
  140. $this->path = $row['path'];
  141. $this->preview_image = $row['preview_image'];
  142. $this->author = $row['author'];
  143. $this->hide_toc_frame = $row['hide_toc_frame'];
  144. $this->lp_session_id = $row['session_id'];
  145. $this->use_max_score = $row['use_max_score'];
  146. $this->subscribe_users = $row['subscribe_users'];
  147. $this->created_on = $row['created_on'];
  148. $this->modified_on = $row['modified_on'];
  149. $this->ref = $row['ref'];
  150. $this->category_id = $row['category_id'];
  151. $this->max_attempts = $row['max_attempts'];
  152. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  153. $this->publicated_on = $row['publicated_on'];
  154. }
  155. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  156. $this->expired_on = $row['expired_on'];
  157. }
  158. if ($this->type == 2) {
  159. if ($row['force_commit'] == 1) {
  160. $this->force_commit = true;
  161. }
  162. }
  163. $this->mode = $row['default_view_mod'];
  164. } else {
  165. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  166. return false;
  167. }
  168. }
  169. // Check user ID.
  170. if (empty($user_id)) {
  171. $this->error = 'User ID is empty';
  172. return false;
  173. } else {
  174. $user_info = api_get_user_info($user_id);
  175. if (!empty($user_info)) {
  176. $this->user_id = $user_info['user_id'];
  177. } else {
  178. $this->error = 'User ID does not exist in database ('.$sql.')';
  179. return false;
  180. }
  181. }
  182. // End of variables checking.
  183. $session_id = api_get_session_id();
  184. // Get the session condition for learning paths of the base + session.
  185. $session = api_get_session_condition($session_id);
  186. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  187. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  188. // Selecting by view_count descending allows to get the highest view_count first.
  189. $sql = "SELECT * FROM $lp_table WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session ORDER BY view_count DESC";
  190. $res = Database::query($sql);
  191. if ($this->debug > 2) {
  192. error_log('New LP - learnpath::__construct() '.__LINE__.' - querying lp_view: '.$sql, 0);
  193. }
  194. if (Database :: num_rows($res) > 0) {
  195. if ($this->debug > 2) {
  196. error_log('New LP - learnpath::__construct() '.__LINE__.' - Found previous view', 0);
  197. }
  198. $row = Database :: fetch_array($res);
  199. $this->attempt = $row['view_count'];
  200. $this->lp_view_id = $row['id'];
  201. $this->last_item_seen = $row['last_item'];
  202. $this->progress_db = $row['progress'];
  203. $this->lp_view_session_id = $row['session_id'];
  204. } else {
  205. if ($this->debug > 2) {
  206. error_log('New LP - learnpath::__construct() '.__LINE__.' - NOT Found previous view', 0);
  207. }
  208. $this->attempt = 1;
  209. $sql_ins = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id) VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  210. $res_ins = Database::query($sql_ins);
  211. $this->lp_view_id = Database :: insert_id();
  212. if ($this->debug > 2) {
  213. error_log('New LP - learnpath::__construct() '.__LINE__.' - inserting new lp_view: '.$sql_ins, 0);
  214. }
  215. }
  216. // Initialise items.
  217. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  218. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' ORDER BY parent_item_id, display_order";
  219. $res = Database::query($sql);
  220. if ($this->debug > 2) {
  221. error_log('New LP - learnpath::__construct() '.__LINE__.' - query lp items: '.$sql, 0);
  222. error_log('-- Start while--', 0);
  223. }
  224. $lp_item_id_list = array();
  225. while ($row = Database::fetch_array($res)) {
  226. $oItem = '';
  227. $lp_item_id_list[] = $row['id'];
  228. switch ($this->type) {
  229. case 3: //aicc
  230. $oItem = new aiccItem('db', $row['id'], $course_id);
  231. if (is_object($oItem)) {
  232. $my_item_id = $oItem->get_id();
  233. $oItem->set_lp_view($this->lp_view_id, $course_id);
  234. $oItem->set_prevent_reinit($this->prevent_reinit);
  235. // Don't use reference here as the next loop will make the pointed object change.
  236. $this->items[$my_item_id] = $oItem;
  237. $this->refs_list[$oItem->ref] = $my_item_id;
  238. if ($this->debug > 2) {
  239. error_log(
  240. 'New LP - learnpath::__construct() - aicc object with id '.$my_item_id.' set in items[]',
  241. 0
  242. );
  243. }
  244. }
  245. break;
  246. case 2:
  247. require_once 'scorm.class.php';
  248. require_once 'scormItem.class.php';
  249. $oItem = new scormItem('db', $row['id'], $course_id);
  250. if (is_object($oItem)) {
  251. $my_item_id = $oItem->get_id();
  252. $oItem->set_lp_view($this->lp_view_id, $course_id);
  253. $oItem->set_prevent_reinit($this->prevent_reinit);
  254. // Don't use reference here as the next loop will make the pointed object change.
  255. $this->items[$my_item_id] = $oItem;
  256. $this->refs_list[$oItem->ref] = $my_item_id;
  257. if ($this->debug > 2) {
  258. error_log('New LP - object with id '.$my_item_id.' set in items[]', 0);
  259. }
  260. }
  261. break;
  262. case 1:
  263. default:
  264. if ($this->debug > 2) {
  265. error_log('New LP - learnpath::__construct() '.__LINE__.' - calling learnpathItem', 0);
  266. }
  267. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  268. if ($this->debug > 2) {
  269. error_log('New LP - learnpath::__construct() '.__LINE__.' - end calling learnpathItem', 0);
  270. }
  271. if (is_object($oItem)) {
  272. $my_item_id = $oItem->get_id();
  273. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  274. $oItem->set_prevent_reinit($this->prevent_reinit);
  275. // Don't use reference here as the next loop will make the pointed object change.
  276. $this->items[$my_item_id] = $oItem;
  277. $this->refs_list[$my_item_id] = $my_item_id;
  278. if ($this->debug > 2) {
  279. error_log(
  280. 'New LP - learnpath::__construct() '.__LINE__.' - object with id '.$my_item_id.' set in items[]',
  281. 0
  282. );
  283. }
  284. }
  285. break;
  286. }
  287. // Items is a list of pointers to all items, classified by DB ID, not SCO id.
  288. if ($row['parent_item_id'] == 0 || empty ($this->items[$row['parent_item_id']])) {
  289. if (is_object($this->items[$row['id']])) {
  290. $this->items[$row['id']]->set_level(0);
  291. }
  292. } else {
  293. $level = $this->items[$row['parent_item_id']]->get_level() + 1;
  294. $this->items[$row['id']]->set_level($level);
  295. if (is_object($this->items[$row['parent_item_id']])) {
  296. // Items is a list of pointers from item DB ids to item objects.
  297. $this->items[$row['parent_item_id']]->add_child($row['id']);
  298. } else {
  299. if ($this->debug > 2) {
  300. error_log(
  301. 'New LP - learnpath::__construct() '.__LINE__.' - The parent item ('.$row['parent_item_id'].') of item '.$row['id'].' could not be found',
  302. 0
  303. );
  304. }
  305. }
  306. }
  307. // Setting the view in the item object.
  308. if (is_object($this->items[$row['id']])) {
  309. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  310. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  311. $this->items[$row['id']]->current_start_time = 0;
  312. $this->items[$row['id']]->current_stop_time = 0;
  313. }
  314. }
  315. }
  316. if ($this->debug > 2) {
  317. error_log('New LP - learnpath::__construct() '.__LINE__.' ----- end while ----', 0);
  318. }
  319. if (!empty($lp_item_id_list)) {
  320. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  321. // Get last viewing vars.
  322. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  323. // This query should only return one or zero result.
  324. $sql = "SELECT lp_item_id, status FROM $lp_item_view_table
  325. WHERE c_id = $course_id AND lp_view_id = ".$this->lp_view_id." AND lp_item_id IN ('".$lp_item_id_list_to_string."')
  326. ORDER BY view_count DESC ";
  327. if ($this->debug > 2) {
  328. error_log('New LP - learnpath::__construct() - Selecting item_views: '.$sql, 0);
  329. }
  330. $status_list = array();
  331. $res = Database::query($sql);
  332. while ($row = Database :: fetch_array($res)) {
  333. $status_list[$row['lp_item_id']] = $row['status'];
  334. }
  335. foreach ($lp_item_id_list as $item_id) {
  336. if (isset($status_list[$item_id])) {
  337. $status = $status_list[$item_id];
  338. if (is_object($this->items[$item_id])) {
  339. $this->items[$item_id]->set_status($status);
  340. if (empty($status)) {
  341. $this->items[$item_id]->set_status($this->default_status);
  342. }
  343. }
  344. } else {
  345. if (is_object($this->items[$item_id])) {
  346. $this->items[$item_id]->set_status($this->default_status);
  347. }
  348. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  349. $sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  350. VALUES ($course_id, ".$item_id.",".$this->lp_view_id.", 1, 'not attempted')";
  351. if ($this->debug > 2) {
  352. error_log(
  353. 'New LP - learnpath::__construct() '.__LINE__.' - Inserting blank item_view : '.$sql_ins,
  354. 0
  355. );
  356. }
  357. $res_ins = Database::query($sql_ins);
  358. }
  359. }
  360. }
  361. $this->ordered_items = self::get_flat_ordered_items_list($this->get_id(), 0, $course_id);
  362. $this->max_ordered_items = 0;
  363. foreach ($this->ordered_items as $index => $dummy) {
  364. if ($index > $this->max_ordered_items && !empty($dummy)) {
  365. $this->max_ordered_items = $index;
  366. }
  367. }
  368. // TODO: Define the current item better.
  369. $this->first();
  370. if ($this->debug > 2) {
  371. error_log(
  372. 'New LP - learnpath::__construct() '.__LINE__.' - End of learnpath constructor for learnpath '.$this->get_id(
  373. ),
  374. 0
  375. );
  376. }
  377. }
  378. /**
  379. * Function rewritten based on old_add_item() from Yannick Warnier. Due the fact that users can decide where the item should come, I had to overlook this function and
  380. * I found it better to rewrite it. Old function is still available. Added also the possibility to add a description.
  381. *
  382. * @param int $parent
  383. * @param int $previous
  384. * @param string $type
  385. * @param int resource ID (ref)
  386. * @param string $title
  387. * @param string $description
  388. * @return int
  389. */
  390. public function add_item(
  391. $parent,
  392. $previous,
  393. $type = 'dokeos_chapter',
  394. $id,
  395. $title,
  396. $description,
  397. $prerequisites = 0,
  398. $max_time_allowed = 0
  399. ) {
  400. $course_id = api_get_course_int_id();
  401. if ($this->debug > 0) {
  402. error_log('New LP - In learnpath::add_item('.$parent.','.$previous.','.$type.','.$id.','.$title.')', 0);
  403. }
  404. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  405. $parent = intval($parent);
  406. $previous = intval($previous);
  407. $type = Database::escape_string($type);
  408. $id = intval($id);
  409. $max_time_allowed = Database::escape_string(htmlentities($max_time_allowed));
  410. if (empty ($max_time_allowed)) {
  411. $max_time_allowed = 0;
  412. }
  413. $title = Database::escape_string($title);
  414. $description = Database::escape_string($description);
  415. $sql_count = " SELECT COUNT(id) AS num
  416. FROM $tbl_lp_item
  417. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND parent_item_id = ".$parent;
  418. $res_count = Database::query($sql_count);
  419. $row = Database :: fetch_array($res_count);
  420. $num = $row['num'];
  421. if ($num > 0) {
  422. if ($previous == 0) {
  423. $sql = "SELECT id, next_item_id, display_order
  424. FROM ".$tbl_lp_item."
  425. WHERE c_id = $course_id AND
  426. lp_id = ".$this->get_id()." AND
  427. parent_item_id = ".$parent." AND
  428. previous_item_id = 0 OR previous_item_id=".$parent;
  429. $result = Database::query($sql);
  430. $row = Database :: fetch_array($result);
  431. $tmp_previous = 0;
  432. $next = $row['id'];
  433. $display_order = 0;
  434. } else {
  435. $previous = (int)$previous;
  436. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  437. FROM $tbl_lp_item
  438. WHERE c_id = $course_id AND lp_id = ".$this->get_id()." AND id = ".$previous;
  439. $result = Database::query($sql);
  440. $row = Database :: fetch_array($result);
  441. $tmp_previous = $row['id'];
  442. $next = $row['next_item_id'];
  443. $display_order = $row['display_order'];
  444. }
  445. } else {
  446. $tmp_previous = 0;
  447. $next = 0;
  448. $display_order = 0;
  449. }
  450. $new_item_id = -1;
  451. $id = Database::escape_string($id);
  452. if ($type == 'quiz') {
  453. $sql = 'SELECT SUM(ponderation)
  454. FROM '.Database :: get_course_table(TABLE_QUIZ_QUESTION).' as quiz_question
  455. INNER JOIN '.Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION).' as quiz_rel_question
  456. ON quiz_question.id = quiz_rel_question.question_id
  457. WHERE quiz_rel_question.exercice_id = '.$id." AND
  458. quiz_question.c_id = $course_id AND
  459. quiz_rel_question.c_id = $course_id ";
  460. $rsQuiz = Database::query($sql);
  461. $max_score = Database :: result($rsQuiz, 0, 0);
  462. //Disabling the exercise if we add it inside a LP
  463. $exercise = new Exercise();
  464. $exercise->read($id);
  465. $exercise->disable();
  466. $exercise->save();
  467. } else {
  468. $max_score = 100;
  469. }
  470. if ($prerequisites != 0) {
  471. $sql_ins = "INSERT INTO ".$tbl_lp_item." (
  472. c_id,
  473. lp_id, ".
  474. "item_type, ".
  475. "ref, ".
  476. "title, ".
  477. "description, ".
  478. "path, ".
  479. "max_score, ".
  480. "parent_item_id, ".
  481. "previous_item_id, ".
  482. "next_item_id, ".
  483. "display_order, ".
  484. "prerequisite, ".
  485. "max_time_allowed ".
  486. ") VALUES (
  487. $course_id ,
  488. ".$this->get_id().", ".
  489. "'".$type."', ".
  490. "'', ".
  491. "'".$title."', ".
  492. "'".$description."', ".
  493. "'".$id."', ".
  494. "'".$max_score."', ".
  495. $parent.", ".
  496. $previous.", ".
  497. $next.", ".
  498. ($display_order + 1).", ".
  499. $prerequisites.", ".
  500. $max_time_allowed.
  501. ")";
  502. } else {
  503. // Insert new item.
  504. $sql_ins = "
  505. INSERT INTO ".$tbl_lp_item." ( ".
  506. "c_id, ".
  507. "lp_id, ".
  508. "item_type, ".
  509. "ref, ".
  510. "title, ".
  511. "description, ".
  512. "path, ".
  513. "max_score, ".
  514. "parent_item_id, ".
  515. "previous_item_id, ".
  516. "next_item_id, ".
  517. "display_order, ".
  518. "max_time_allowed ".
  519. ") VALUES (".
  520. $course_id.",".
  521. $this->get_id().",".
  522. "'".$type."',".
  523. "'',".
  524. "'".$title."',".
  525. "'".$description."',".
  526. "'".$id."',".
  527. "'".$max_score."',".
  528. $parent.",".
  529. $previous.",".
  530. $next.",".
  531. ($display_order + 1).",".
  532. $max_time_allowed.
  533. ")";
  534. }
  535. if ($this->debug > 2) {
  536. error_log('New LP - Inserting dokeos_chapter: '.$sql_ins, 0);
  537. }
  538. $res_ins = Database::query($sql_ins);
  539. if ($res_ins > 0) {
  540. $new_item_id = Database :: insert_id($res_ins);
  541. // Update the item that should come after the new item.
  542. $sql_update_next = "
  543. UPDATE ".$tbl_lp_item."
  544. SET previous_item_id = ".$new_item_id."
  545. WHERE c_id = $course_id AND id = ".$next;
  546. Database::query($sql_update_next);
  547. // Update the item that should be before the new item.
  548. $sql_update_previous = "
  549. UPDATE ".$tbl_lp_item."
  550. SET next_item_id = ".$new_item_id."
  551. WHERE c_id = $course_id AND id = ".$tmp_previous;
  552. Database::query($sql_update_previous);
  553. // Update all the items after the new item.
  554. $sql_update_order = "
  555. UPDATE ".$tbl_lp_item."
  556. SET display_order = display_order + 1
  557. WHERE
  558. c_id = $course_id AND
  559. lp_id = ".$this->get_id()." AND
  560. id <> ".$new_item_id." AND
  561. parent_item_id = ".$parent." AND
  562. display_order > ".$display_order;
  563. Database::query($sql_update_order);
  564. // Update the item that should come after the new item.
  565. $sql_update_ref = "UPDATE ".$tbl_lp_item."
  566. SET ref = ".$new_item_id."
  567. WHERE c_id = $course_id AND id = ".$new_item_id;
  568. Database::query($sql_update_ref);
  569. }
  570. // Upload audio.
  571. if (!empty ($_FILES['mp3']['name'])) {
  572. // Create the audio folder if it does not exist yet.
  573. global $_course;
  574. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  575. if (!is_dir($filepath.'audio')) {
  576. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  577. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  578. api_item_property_update(
  579. $_course,
  580. TOOL_DOCUMENT,
  581. $audio_id,
  582. 'FolderCreated',
  583. api_get_user_id(),
  584. null,
  585. null,
  586. null,
  587. null,
  588. api_get_session_id()
  589. );
  590. api_item_property_update(
  591. $_course,
  592. TOOL_DOCUMENT,
  593. $audio_id,
  594. 'invisible',
  595. api_get_user_id(),
  596. null,
  597. null,
  598. null,
  599. null,
  600. api_get_session_id()
  601. );
  602. }
  603. // Upload the file in the documents tool.
  604. $file_path = FileManager::handle_uploaded_document(
  605. $_course,
  606. $_FILES['mp3'],
  607. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  608. '/audio',
  609. api_get_user_id(),
  610. '',
  611. '',
  612. '',
  613. '',
  614. false
  615. );
  616. // Getting the filename only.
  617. $file_components = explode('/', $file_path);
  618. $file = $file_components[count($file_components) - 1];
  619. // Store the mp3 file in the lp_item table.
  620. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string(
  621. $file
  622. )."' WHERE id = '".Database::escape_string($new_item_id)."'";
  623. Database::query($sql_insert_audio);
  624. }
  625. return $new_item_id;
  626. }
  627. /**
  628. * Static admin function allowing addition of a learnpath to a course.
  629. * @param string Course code
  630. * @param string Learnpath name
  631. * @param string Learnpath description string, if provided
  632. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  633. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  634. * @param string Zip file containing the learnpath or directory containing the learnpath
  635. * @return integer The new learnpath ID on success, 0 on failure
  636. */
  637. public static function add_lp(
  638. $course,
  639. $name,
  640. $description = '',
  641. $learnpath = 'guess',
  642. $origin = 'zip',
  643. $zipname = '',
  644. $publicated_on = '',
  645. $expired_on = '',
  646. $category_id = 0
  647. ) {
  648. global $charset;
  649. $course_id = api_get_course_int_id();
  650. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  651. // Check course code exists.
  652. // Check lp_name doesn't exist, otherwise append something.
  653. $i = 0;
  654. $name = Database::escape_string($name);
  655. $category_id = intval($category_id);
  656. // Session id.
  657. $session_id = api_get_session_id();
  658. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  659. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  660. $res_name = Database::query($check_name);
  661. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  662. //by default the publication date is the same that the creation date
  663. //The behaviour above was changed due BT#2800
  664. global $_custom;
  665. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  666. $publicated_on = '';
  667. } else {
  668. $publicated_on = api_get_utc_datetime();
  669. }
  670. } else {
  671. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  672. }
  673. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  674. $expired_on = '';
  675. } else {
  676. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  677. }
  678. while (Database :: num_rows($res_name)) {
  679. // There is already one such name, update the current one a bit.
  680. $i++;
  681. $name = $name.' - '.$i;
  682. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  683. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  684. $res_name = Database::query($check_name);
  685. }
  686. // New name does not exist yet; keep it.
  687. // Escape description.
  688. $description = Database::escape_string(
  689. api_htmlentities($description, ENT_QUOTES, $charset)
  690. ); // Kevin: added htmlentities().
  691. $type = 1;
  692. switch ($learnpath) {
  693. case 'guess':
  694. break;
  695. case 'dokeos':
  696. case 'chamilo':
  697. $type = 1;
  698. break;
  699. case 'aicc':
  700. break;
  701. }
  702. switch ($origin) {
  703. case 'zip':
  704. // Check zipname string. If empty, we are currently creating a new Chamilo learnpath.
  705. break;
  706. case 'manual':
  707. default:
  708. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  709. $res_max = Database::query($get_max);
  710. if (Database :: num_rows($res_max) < 1) {
  711. $dsp = 1;
  712. } else {
  713. $row = Database :: fetch_array($res_max);
  714. $dsp = $row[0] + 1;
  715. }
  716. $sql_insert = "INSERT INTO $tbl_lp (c_id, lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on, category_id) ".
  717. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime(
  718. )."' , '".$publicated_on."' , '".$expired_on."', $category_id)";
  719. Database::query($sql_insert);
  720. $id = Database :: insert_id();
  721. if ($id > 0) {
  722. $course_info = api_get_course_info();
  723. // Insert into item_property.
  724. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  725. api_set_default_visibility($id, TOOL_LEARNPATH);
  726. return $id;
  727. }
  728. break;
  729. }
  730. }
  731. /**
  732. * Appends a message to the message attribute
  733. * @param string Message to append.
  734. */
  735. public function append_message($string)
  736. {
  737. if ($this->debug > 0) {
  738. error_log('New LP - In learnpath::append_message()', 0);
  739. }
  740. $this->message .= $string;
  741. }
  742. /**
  743. * Autocompletes the parents of an item in case it's been completed or passed
  744. * @param integer Optional ID of the item from which to look for parents
  745. */
  746. public function autocomplete_parents($item)
  747. {
  748. $debug = $this->debug;
  749. if ($debug) {
  750. error_log('Learnpath::autocomplete_parents()', 0);
  751. }
  752. if (empty($item)) {
  753. $item = $this->current;
  754. }
  755. $parent_id = $this->items[$item]->get_parent();
  756. if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) {
  757. // if $item points to an object and there is a parent.
  758. if ($debug) {
  759. error_log('Autocompleting parent of item '.$item.' (item '.$parent_id.')', 0);
  760. }
  761. $current_item = $this->items[$item];
  762. $parent = $this->items[$parent_id]; // Get the parent.
  763. // New experiment including failed and browsed in completed status.
  764. $current_status = $current_item->get_status();
  765. if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  766. // If the current item is completed or passes or succeeded.
  767. $completed = true;
  768. if ($debug) {
  769. error_log('Status of current item is alright', 0);
  770. }
  771. foreach ($parent->get_children() as $child) {
  772. // Check all his brothers (parent's children) for completion status.
  773. if ($child != $item) {
  774. if ($debug) {
  775. error_log(
  776. 'Looking at brother with ID '.$child.', status is '.$this->items[$child]->get_status(),
  777. 0
  778. );
  779. }
  780. //if($this->items[$child]->status_is(array('completed','passed','succeeded')))
  781. // Trying completing parents of failed and browsed items as well.
  782. if ($this->items[$child]->status_is(
  783. array(
  784. 'completed',
  785. 'passed',
  786. 'succeeded',
  787. 'browsed',
  788. 'failed'
  789. )
  790. )
  791. ) {
  792. // Keep completion status to true.
  793. } else {
  794. if ($this->debug > 2) {
  795. error_log(
  796. 'Found one incomplete child of '.$parent_id.': '.$child.' is '.$this->items[$child]->get_status(
  797. ),
  798. 0
  799. );
  800. }
  801. $completed = false;
  802. }
  803. }
  804. }
  805. if ($completed) { // If all the children were completed:
  806. $parent->set_status('completed');
  807. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  808. $this->update_queue[$parent->get_id()] = $parent->get_status();
  809. if ($debug) {
  810. error_log('Added parent to update queue '.print_r($this->update_queue, true), 0);
  811. }
  812. $this->autocomplete_parents($parent->get_id()); // Recursive call.
  813. }
  814. } else {
  815. //error_log('New LP - status of current item is not enough to get bothered with it', 0);
  816. }
  817. } else {
  818. if ($debug) {
  819. error_log("#$item is an item doesn't have parents");
  820. }
  821. }
  822. }
  823. /**
  824. * Autosaves the current results into the database for the whole learnpath
  825. */
  826. public function autosave()
  827. {
  828. if ($this->debug > 0) {
  829. error_log('New LP - In learnpath::autosave()', 0);
  830. }
  831. // TODO: Add save operations for the learnpath itself.
  832. }
  833. /**
  834. * Clears the message attribute
  835. */
  836. public function clear_message()
  837. {
  838. if ($this->debug > 0) {
  839. error_log('New LP - In learnpath::clear_message()', 0);
  840. }
  841. $this->message = '';
  842. }
  843. /**
  844. * Closes the current resource
  845. *
  846. * Stops the timer
  847. * Saves into the database if required
  848. * Clears the current resource data from this object
  849. * @return boolean True on success, false on failure
  850. */
  851. public function close()
  852. {
  853. $course_id = api_get_course_int_id();
  854. if ($this->debug > 0) {
  855. error_log('New LP - In learnpath::close()', 0);
  856. }
  857. if (empty ($this->lp_id)) {
  858. $this->error = 'Trying to close this learnpath but no ID is set';
  859. return false;
  860. }
  861. $this->current_time_stop = time();
  862. if ($this->save) {
  863. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  864. /*
  865. $sql = "UPDATE $learnpath_view_table " .
  866. "SET " .
  867. "stop_time = ".$this->current_time_stop.", " .
  868. "score = ".$this->current_score.", ".
  869. "WHERE learnpath_id = '".$this->lp_id."'";
  870. //$res = Database::query($sql);
  871. $res = Database::query($res);
  872. if (Database::affected_rows($res) < 1) {
  873. $this->error = 'Could not update learnpath_view table while closing learnpath';
  874. return false;
  875. }
  876. */
  877. }
  878. $this->ordered_items = array();
  879. $this->index = 0;
  880. unset ($this->lp_id);
  881. //unset other stuff
  882. return true;
  883. }
  884. /**
  885. * Static admin function allowing removal of a learnpath
  886. * @param string Course code
  887. * @param integer Learnpath ID
  888. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  889. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  890. */
  891. public function delete($course = null, $id = null, $delete = 'keep')
  892. {
  893. $course_id = api_get_course_int_id();
  894. // TODO: Implement a way of getting this to work when the current object is not set.
  895. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  896. //if (empty($course)) { $course = api_get_course_id(); }
  897. //if (empty($id)) { $id = $this->get_id(); }
  898. // If an ID is specifically given and the current LP is not the same, prevent delete.
  899. if (!empty ($id) && ($id != $this->lp_id)) {
  900. return false;
  901. }
  902. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  903. $lp_item = Database :: get_course_table(TABLE_LP_ITEM); // Proposed by Christophe (clefevre), see below.
  904. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  905. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  906. //if ($this->debug > 0) { error_log('New LP - In learnpath::delete()', 0); }
  907. // Delete lp item id.
  908. foreach ($this->items as $id => $dummy) {
  909. //$this->items[$id]->delete();
  910. $sql_del_view = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '".$id."'";
  911. $res_del_item_view = Database::query($sql_del_view);
  912. }
  913. // Proposed by Christophe (nickname: clefevre), see http://www.dokeos.com/forum/viewtopic.php?t=29673
  914. $sql_del_item = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  915. $res_del_item = Database::query($sql_del_item);
  916. $sql_del_view = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  917. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  918. $res_del_view = Database::query($sql_del_view);
  919. self::toggle_publish($this->lp_id, 'i');
  920. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  921. if ($this->type == 2 || $this->type == 3) {
  922. // This is a scorm learning path, delete the files as well.
  923. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  924. $res = Database::query($sql);
  925. if (Database :: num_rows($res) > 0) {
  926. $row = Database :: fetch_array($res);
  927. $path = $row['path'];
  928. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != ".$this->lp_id;
  929. $res = Database::query($sql);
  930. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  931. if ($this->debug > 2) {
  932. error_log(
  933. 'New LP - In learnpath::delete(), found other LP using path '.$path.', keeping directory',
  934. 0
  935. );
  936. }
  937. } else {
  938. // No other LP uses that directory, delete it.
  939. $course_rel_dir = api_get_course_path().'/scorm/'; // scorm dir web path starting from /courses
  940. $course_scorm_dir = api_get_path(
  941. SYS_COURSE_PATH
  942. ).$course_rel_dir; // The absolute system path for this course.
  943. if ($delete == 'remove' && is_dir($course_scorm_dir.$path) and !empty ($course_scorm_dir)) {
  944. if ($this->debug > 2) {
  945. error_log(
  946. 'New LP - In learnpath::delete(), found SCORM, deleting directory: '.$course_scorm_dir.$path,
  947. 0
  948. );
  949. }
  950. // Proposed by Christophe (clefevre).
  951. if (strcmp(substr($path, -2), "/.") == 0) {
  952. $path = substr($path, 0, -1); // Remove "." at the end.
  953. }
  954. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  955. rmdirr($course_scorm_dir.$path);
  956. }
  957. }
  958. }
  959. }
  960. $sql_del_lp = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = ".$this->lp_id;
  961. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.': '.$sql_del_lp, 0); }
  962. $res_del_lp = Database::query($sql_del_lp);
  963. $this->update_display_order(); // Updates the display order of all lps.
  964. api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $this->lp_id, 'delete', api_get_user_id());
  965. require_once '../gradebook/lib/be.inc.php';
  966. // Delete link of gradebook tool
  967. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  968. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  969. $result = Database::query($sql);
  970. $row = Database :: fetch_array($result, 'ASSOC');*/
  971. // Fixing gradebook link deleted see #5229.
  972. /*
  973. if (!empty($row['id'])) {
  974. $link = LinkFactory :: load($row['id']);
  975. if ($link[0] != null) {
  976. $link[0]->delete();
  977. }
  978. }*/
  979. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  980. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4, $id, api_get_session_id());
  981. if ($link_info !== false) {
  982. remove_resource_from_course_gradebook($link_info['id']);
  983. }
  984. if (api_get_setting('search_enabled') == 'true') {
  985. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  986. $r = delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  987. }
  988. }
  989. /**
  990. * Removes all the children of one item - dangerous!
  991. * @param integer Element ID of which children have to be removed
  992. * @return integer Total number of children removed
  993. */
  994. public function delete_children_items($id)
  995. {
  996. $course_id = api_get_course_int_id();
  997. if ($this->debug > 0) {
  998. error_log('New LP - In learnpath::delete_children_items('.$id.')', 0);
  999. }
  1000. $num = 0;
  1001. if (empty ($id) || $id != strval(intval($id))) {
  1002. return false;
  1003. }
  1004. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1005. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1006. $res = Database::query($sql);
  1007. while ($row = Database :: fetch_array($res)) {
  1008. $num += $this->delete_children_items($row['id']);
  1009. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = ".$row['id'];
  1010. $res_del = Database::query($sql_del);
  1011. $num++;
  1012. }
  1013. return $num;
  1014. }
  1015. /**
  1016. * Removes an item from the current learnpath
  1017. * @param integer Elem ID (0 if first)
  1018. * @param integer Whether to remove the resource/data from the system or leave it (default: 'keep', others 'remove')
  1019. * @return integer Number of elements moved
  1020. * @todo implement resource removal
  1021. */
  1022. public function delete_item($id, $remove = 'keep')
  1023. {
  1024. $course_id = api_get_course_int_id();
  1025. if ($this->debug > 0) {
  1026. error_log('New LP - In learnpath::delete_item()', 0);
  1027. }
  1028. // TODO: Implement the resource removal.
  1029. if (empty ($id) || $id != strval(intval($id))) {
  1030. return false;
  1031. }
  1032. // First select item to get previous, next, and display order.
  1033. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1034. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1035. $res_sel = Database::query($sql_sel);
  1036. if (Database :: num_rows($res_sel) < 1) {
  1037. return false;
  1038. }
  1039. $row = Database :: fetch_array($res_sel);
  1040. $previous = $row['previous_item_id'];
  1041. $next = $row['next_item_id'];
  1042. $display = $row['display_order'];
  1043. $parent = $row['parent_item_id'];
  1044. $lp = $row['lp_id'];
  1045. // Delete children items.
  1046. $num = $this->delete_children_items($id);
  1047. if ($this->debug > 2) {
  1048. error_log('New LP - learnpath::delete_item() - deleted '.$num.' children of element '.$id, 0);
  1049. }
  1050. // Now delete the item.
  1051. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1052. if ($this->debug > 2) {
  1053. error_log('New LP - Deleting item: '.$sql_del, 0);
  1054. }
  1055. $res_del = Database::query($sql_del);
  1056. // Now update surrounding items.
  1057. $sql_upd = "UPDATE $lp_item SET next_item_id = $next WHERE c_id = ".$course_id." AND id = $previous";
  1058. $res_upd = Database::query($sql_upd);
  1059. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous WHERE c_id = ".$course_id." AND id = $next";
  1060. $res_upd = Database::query($sql_upd);
  1061. // Now update all following items with new display order.
  1062. $sql_all = "UPDATE $lp_item SET display_order = display_order-1 WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1063. $res_all = Database::query($sql_all);
  1064. //Removing prerequisites since the item will not longer exist
  1065. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1066. $res_all = Database::query($sql_all);
  1067. // Remove from search engine if enabled.
  1068. if (api_get_setting('search_enabled') == 'true') {
  1069. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1070. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1071. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1072. $res = Database::query($sql);
  1073. if (Database :: num_rows($res) > 0) {
  1074. $row2 = Database :: fetch_array($res);
  1075. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1076. $di = new ChamiloIndexer();
  1077. $di->remove_document((int)$row2['search_did']);
  1078. }
  1079. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  1080. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1081. Database::query($sql);
  1082. }
  1083. }
  1084. /**
  1085. * Updates an item's content in place
  1086. * @param integer Element ID
  1087. * @param integer Parent item ID
  1088. * @param integer Previous item ID
  1089. * @param string Item title
  1090. * @param string Item description
  1091. * @param string Prerequisites (optional)
  1092. * @param string Indexing terms (optional)
  1093. * @param array The array resulting of the $_FILES[mp3] element
  1094. * @return boolean True on success, false on error
  1095. */
  1096. public function edit_item(
  1097. $id,
  1098. $parent,
  1099. $previous,
  1100. $title,
  1101. $description,
  1102. $prerequisites = 0,
  1103. $audio = null,
  1104. $max_time_allowed = 0
  1105. ) {
  1106. $course_id = api_get_course_int_id();
  1107. if ($this->debug > 0) {
  1108. error_log('New LP - In learnpath::edit_item()', 0);
  1109. }
  1110. if (empty ($max_time_allowed)) {
  1111. $max_time_allowed = 0;
  1112. }
  1113. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1114. return false;
  1115. }
  1116. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1117. $sql_select = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND id = ".$id;
  1118. $res_select = Database::query($sql_select);
  1119. $row_select = Database :: fetch_array($res_select);
  1120. $audio_update_sql = '';
  1121. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1122. // Create the audio folder if it does not exist yet.
  1123. global $_course;
  1124. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  1125. if (!is_dir($filepath.'audio')) {
  1126. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  1127. $audio_id = FileManager::add_document($_course, '/audio', 'folder', 0, 'audio');
  1128. api_item_property_update(
  1129. $_course,
  1130. TOOL_DOCUMENT,
  1131. $audio_id,
  1132. 'FolderCreated',
  1133. api_get_user_id(),
  1134. null,
  1135. null,
  1136. null,
  1137. null,
  1138. api_get_session_id()
  1139. );
  1140. api_item_property_update(
  1141. $_course,
  1142. TOOL_DOCUMENT,
  1143. $audio_id,
  1144. 'invisible',
  1145. api_get_user_id(),
  1146. null,
  1147. null,
  1148. null,
  1149. null,
  1150. api_get_session_id()
  1151. );
  1152. }
  1153. // Upload file in documents.
  1154. $pi = pathinfo($audio['name']);
  1155. if ($pi['extension'] == 'mp3') {
  1156. $c_det = api_get_course_info($this->cc);
  1157. $bp = api_get_path(SYS_COURSE_PATH).$c_det['path'].'/document';
  1158. $path = FileManager::handle_uploaded_document(
  1159. $c_det,
  1160. $audio,
  1161. $bp,
  1162. '/audio',
  1163. api_get_user_id(),
  1164. 0,
  1165. null,
  1166. 0,
  1167. 'rename',
  1168. false,
  1169. 0
  1170. );
  1171. $path = substr($path, 7);
  1172. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1173. $audio_update_sql = ", audio = '".Database::escape_string($path)."' ";
  1174. }
  1175. }
  1176. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1177. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1178. // TODO: htmlspecialchars to be checked for encoding related problems.
  1179. if ($same_parent && $same_previous) {
  1180. // Only update title and description.
  1181. $sql_update = " UPDATE ".$tbl_lp_item."
  1182. SET title = '".Database::escape_string($title)."',
  1183. prerequisite = '".$prerequisites."',
  1184. description = '".Database::escape_string($description)."'
  1185. ".$audio_update_sql.",
  1186. max_time_allowed = '".Database::escape_string($max_time_allowed)."'
  1187. WHERE c_id = ".$course_id." AND id = ".$id;
  1188. $res_update = Database::query($sql_update);
  1189. } else {
  1190. $old_parent = $row_select['parent_item_id'];
  1191. $old_previous = $row_select['previous_item_id'];
  1192. $old_next = $row_select['next_item_id'];
  1193. $old_order = $row_select['display_order'];
  1194. $old_prerequisite = $row_select['prerequisite'];
  1195. $old_max_time_allowed = $row_select['max_time_allowed'];
  1196. /* BEGIN -- virtually remove the current item id */
  1197. /* for the next and previous item it is like the current item doesn't exist anymore */
  1198. if ($old_previous != 0) {
  1199. $sql_update_next = "
  1200. UPDATE ".$tbl_lp_item."
  1201. SET next_item_id = ".$old_next."
  1202. WHERE c_id = ".$course_id." AND id = ".$old_previous;
  1203. $res_update_next = Database::query($sql_update_next);
  1204. //echo '<p>' . $sql_update_next . '</p>';
  1205. }
  1206. if ($old_next != 0) {
  1207. $sql_update_previous = "
  1208. UPDATE ".$tbl_lp_item."
  1209. SET previous_item_id = ".$old_previous."
  1210. WHERE c_id = ".$course_id." AND id = ".$old_next;
  1211. $res_update_previous = Database::query($sql_update_previous);
  1212. //echo '<p>' . $sql_update_previous . '</p>';
  1213. }
  1214. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1215. $sql_update_order = "
  1216. UPDATE ".$tbl_lp_item."
  1217. SET display_order = display_order - 1
  1218. WHERE
  1219. c_id = ".$course_id." AND
  1220. display_order > ".$old_order." AND lp_id = ".$this->lp_id." AND
  1221. parent_item_id = ".$old_parent;
  1222. $res_update_order = Database::query($sql_update_order);
  1223. //echo '<p>' . $sql_update_order . '</p>';
  1224. /* END -- virtually remove the current item id */
  1225. /* BEGIN -- update the current item id to his new location */
  1226. if ($previous == 0) {
  1227. // Select the data of the item that should come after the current item.
  1228. $sql_select_old = "SELECT id, display_order
  1229. FROM ".$tbl_lp_item."
  1230. WHERE
  1231. c_id = ".$course_id." AND
  1232. lp_id = ".$this->lp_id." AND
  1233. parent_item_id = ".$parent." AND
  1234. previous_item_id = ".$previous;
  1235. $res_select_old = Database::query($sql_select_old);
  1236. $row_select_old = Database :: fetch_array($res_select_old);
  1237. //echo '<p>' . $sql_select_old . '</p>';
  1238. // If the new parent didn't have children before.
  1239. if (Database :: num_rows($res_select_old) == 0) {
  1240. $new_next = 0;
  1241. $new_order = 1;
  1242. } else {
  1243. $new_next = $row_select_old['id'];
  1244. $new_order = $row_select_old['display_order'];
  1245. }
  1246. //echo 'New next_item_id of current item: ' . $new_next . '<br />';
  1247. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1248. //echo 'New display_order of current item: ' . $new_order . '<br />';
  1249. } else {
  1250. // Select the data of the item that should come before the current item.
  1251. $sql_select_old = " SELECT next_item_id, display_order
  1252. FROM ".$tbl_lp_item."
  1253. WHERE c_id = ".$course_id." AND id = ".$previous;
  1254. $res_select_old = Database::query($sql_select_old);
  1255. $row_select_old = Database :: fetch_array($res_select_old);
  1256. //echo '<p>' . $sql_select_old . '</p>';
  1257. //echo 'New next_item_id of current item: ' . $row_select_old['next_item_id'] . '<br />';
  1258. //echo 'New previous_item_id of current item: ' . $previous . '<br />';
  1259. //echo 'New display_order of current item: ' . ($row_select_old['display_order'] + 1) . '<br />';
  1260. $new_next = $row_select_old['next_item_id'];
  1261. $new_order = $row_select_old['display_order'] + 1;
  1262. }
  1263. // TODO: htmlspecialchars to be checked for encoding related problems.
  1264. // Update the current item with the new data.
  1265. $sql_update = "UPDATE ".$tbl_lp_item."
  1266. SET
  1267. title = '".Database::escape_string($title)."',
  1268. description = '".Database::escape_string($description)."',
  1269. parent_item_id = ".$parent.",
  1270. previous_item_id = ".$previous.",
  1271. next_item_id = ".$new_next.",
  1272. display_order = ".$new_order."
  1273. ".$audio_update_sql."
  1274. WHERE c_id = ".$course_id." AND id = ".$id;
  1275. $res_update_next = Database::query($sql_update);
  1276. //echo '<p>' . $sql_update . '</p>';
  1277. if ($previous != 0) {
  1278. // Update the previous item's next_item_id.
  1279. $sql_update_previous = "
  1280. UPDATE ".$tbl_lp_item."
  1281. SET next_item_id = ".$id."
  1282. WHERE c_id = ".$course_id." AND id = ".$previous;
  1283. $res_update_next = Database::query($sql_update_previous);
  1284. //echo '<p>' . $sql_update_previous . '</p>';
  1285. }
  1286. if ($new_next != 0) {
  1287. // Update the next item's previous_item_id.
  1288. $sql_update_next = "
  1289. UPDATE ".$tbl_lp_item."
  1290. SET previous_item_id = ".$id."
  1291. WHERE c_id = ".$course_id." AND id = ".$new_next;
  1292. $res_update_next = Database::query($sql_update_next);
  1293. //echo '<p>' . $sql_update_next . '</p>';
  1294. }
  1295. if ($old_prerequisite != $prerequisites) {
  1296. $sql_update_next = "
  1297. UPDATE ".$tbl_lp_item."
  1298. SET prerequisite = ".$prerequisites."
  1299. WHERE c_id = ".$course_id." AND id = ".$id;
  1300. $res_update_next = Database::query($sql_update_next);
  1301. }
  1302. if ($old_max_time_allowed != $max_time_allowed) {
  1303. $sql_update_max_time_allowed = "
  1304. UPDATE ".$tbl_lp_item."
  1305. SET max_time_allowed = ".$max_time_allowed."
  1306. WHERE c_id = ".$course_id." AND id = ".$id;
  1307. $res_update_max_time_allowed = Database::query($sql_update_max_time_allowed);
  1308. }
  1309. // Update all the items with the same or a bigger display_order than the current item.
  1310. $sql_update_order = "
  1311. UPDATE ".$tbl_lp_item."
  1312. SET display_order = display_order + 1
  1313. WHERE
  1314. c_id = ".$course_id." AND
  1315. lp_id = ".$this->get_id()." AND
  1316. id <> ".$id." AND
  1317. parent_item_id = ".$parent." AND
  1318. display_order >= ".$new_order;
  1319. $res_update_next = Database::query($sql_update_order);
  1320. }
  1321. }
  1322. /**
  1323. * Updates an item's prereq in place
  1324. * @param integer Element ID
  1325. * @param string Prerequisite Element ID
  1326. * @param string Prerequisite item type
  1327. * @param string Prerequisite min score
  1328. * @param string Prerequisite max score
  1329. * @return boolean True on success, false on error
  1330. */
  1331. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1332. {
  1333. $course_id = api_get_course_int_id();
  1334. if ($this->debug > 0) {
  1335. error_log(
  1336. 'New LP - In learnpath::edit_item_prereq('.$id.','.$prerequisite_id.','.$mastery_score.','.$max_score.')',
  1337. 0
  1338. );
  1339. }
  1340. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1341. return false;
  1342. }
  1343. $prerequisite_id = Database::escape_string($prerequisite_id);
  1344. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1345. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1346. $mastery_score = 0;
  1347. }
  1348. if (!is_numeric($max_score) || $max_score < 0) {
  1349. $max_score = 100;
  1350. }
  1351. if ($mastery_score > $max_score) {
  1352. $max_score = $mastery_score;
  1353. }
  1354. if (!is_numeric($prerequisite_id)) {
  1355. $prerequisite_id = 'NULL';
  1356. }
  1357. $sql_upd = " UPDATE ".$tbl_lp_item."
  1358. SET prerequisite = ".$prerequisite_id." WHERE c_id = ".$course_id." AND id = ".$id;
  1359. $res_upd = Database::query($sql_upd);
  1360. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1361. $sql_upd = " UPDATE ".$tbl_lp_item." SET
  1362. mastery_score = ".$mastery_score.
  1363. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1364. " WHERE c_id = ".$course_id." AND ref = '".$prerequisite_id."'"; // Will this be enough to ensure unicity?
  1365. Database::query($sql_upd);
  1366. }
  1367. // TODO: Update the item object (can be ignored for now because refreshed).
  1368. return true;
  1369. }
  1370. /**
  1371. * Escapes a string with the available database escape function
  1372. * @param string String to escape
  1373. * @return string String escaped
  1374. * @deprecated use Database::escape_string
  1375. */
  1376. public function escape_string($string)
  1377. {
  1378. return Database::escape_string($string);
  1379. }
  1380. /**
  1381. * Static admin function exporting a learnpath into a zip file
  1382. * @param string Export type (scorm, zip, cd)
  1383. * @param string Course code
  1384. * @param integer Learnpath ID
  1385. * @param string Zip file name
  1386. * @return string Zip file path (or false on error)
  1387. */
  1388. public function export_lp($type, $course, $id, $zipname)
  1389. {
  1390. //if ($this->debug > 0) { error_log('New LP - In learnpath::export_lp()', 0); }
  1391. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1392. return false;
  1393. }
  1394. $url = '';
  1395. switch ($type) {
  1396. case 'scorm':
  1397. break;
  1398. case 'zip':
  1399. break;
  1400. case 'cdrom':
  1401. break;
  1402. }
  1403. return $url;
  1404. }
  1405. /**
  1406. * Gets all the chapters belonging to the same parent as the item/chapter given
  1407. * Can also be called as abstract method
  1408. * @param integer Item ID
  1409. * @return array A list of all the "brother items" (or an empty array on failure)
  1410. */
  1411. public function get_brother_chapters($id)
  1412. {
  1413. $course_id = api_get_course_int_id();
  1414. if ($this->debug > 0) {
  1415. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1416. }
  1417. if (empty ($id) OR $id != strval(intval($id))) {
  1418. return array();
  1419. }
  1420. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1421. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1422. $res_parent = Database::query($sql_parent);
  1423. if (Database :: num_rows($res_parent) > 0) {
  1424. $row_parent = Database :: fetch_array($res_parent);
  1425. $parent = $row_parent['parent_item_id'];
  1426. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent AND id = $id AND item_type='dokeos_chapter' ORDER BY display_order";
  1427. $res_bros = Database::query($sql_bros);
  1428. $list = array();
  1429. while ($row_bro = Database :: fetch_array($res_bros)) {
  1430. $list[] = $row_bro;
  1431. }
  1432. return $list;
  1433. }
  1434. return array();
  1435. }
  1436. /**
  1437. * Gets all the items belonging to the same parent as the item given
  1438. * Can also be called as abstract method
  1439. * @param integer Item ID
  1440. * @return array A list of all the "brother items" (or an empty array on failure)
  1441. */
  1442. public function get_brother_items($id)
  1443. {
  1444. $course_id = api_get_course_int_id();
  1445. if ($this->debug > 0) {
  1446. error_log('New LP - In learnpath::get_brother_items('.$id.')', 0);
  1447. }
  1448. if (empty ($id) OR $id != strval(intval($id))) {
  1449. return array();
  1450. }
  1451. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1452. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1453. $res_parent = Database::query($sql_parent);
  1454. if (Database :: num_rows($res_parent) > 0) {
  1455. $row_parent = Database :: fetch_array($res_parent);
  1456. $parent = $row_parent['parent_item_id'];
  1457. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent ORDER BY display_order";
  1458. $res_bros = Database::query($sql_bros);
  1459. $list = array();
  1460. while ($row_bro = Database :: fetch_array($res_bros)) {
  1461. $list[] = $row_bro;
  1462. }
  1463. return $list;
  1464. }
  1465. return array();
  1466. }
  1467. /**
  1468. * Get the specific prefix index terms of this learning path
  1469. * @return array Array of terms
  1470. */
  1471. public function get_common_index_terms_by_prefix($prefix)
  1472. {
  1473. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1474. $terms = get_specific_field_values_list_by_prefix($prefix, $this->cc, TOOL_LEARNPATH, $this->lp_id);
  1475. $prefix_terms = array();
  1476. if (!empty($terms)) {
  1477. foreach ($terms as $term) {
  1478. $prefix_terms[] = $term['value'];
  1479. }
  1480. }
  1481. return $prefix_terms;
  1482. }
  1483. /**
  1484. * Gets the number of items currently completed
  1485. * @return integer The number of items currently completed
  1486. */
  1487. public function get_complete_items_count()
  1488. {
  1489. if ($this->debug > 0) {
  1490. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1491. }
  1492. $i = 0;
  1493. foreach ($this->items as $id => $dummy) {
  1494. // Trying failed and browsed considered "progressed" as well.
  1495. if ($this->items[$id]->status_is(
  1496. array(
  1497. 'completed',
  1498. 'passed',
  1499. 'succeeded',
  1500. 'browsed',
  1501. 'failed'
  1502. )
  1503. ) && $this->items[$id]->get_type() != 'dokeos_chapter' && $this->items[$id]->get_type() != 'dir'
  1504. ) {
  1505. $i++;
  1506. }
  1507. }
  1508. return $i;
  1509. }
  1510. /**
  1511. * Gets the current item ID
  1512. * @return integer The current learnpath item id
  1513. */
  1514. public function get_current_item_id()
  1515. {
  1516. $current = 0;
  1517. if ($this->debug > 0) {
  1518. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1519. }
  1520. if (!empty ($this->current)) {
  1521. $current = $this->current;
  1522. }
  1523. if ($this->debug > 2) {
  1524. error_log('New LP - In learnpath::get_current_item_id() - Returning '.$current, 0);
  1525. }
  1526. return $current;
  1527. }
  1528. /**
  1529. * Force to get the first learnpath item id
  1530. * @return integer The current learnpath item id
  1531. */
  1532. public function get_first_item_id()
  1533. {
  1534. $current = 0;
  1535. if (is_array($this->ordered_items)) {
  1536. $current = $this->ordered_items[0];
  1537. }
  1538. return $current;
  1539. }
  1540. /**
  1541. * Gets the total number of items available for viewing in this SCORM
  1542. * @return integer The total number of items
  1543. */
  1544. public function get_total_items_count()
  1545. {
  1546. if ($this->debug > 0) {
  1547. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1548. }
  1549. return count($this->items);
  1550. }
  1551. /**
  1552. * Gets the total number of items available for viewing in this SCORM but without chapters
  1553. * @return integer The total no-chapters number of items
  1554. */
  1555. public function get_total_items_count_without_chapters()
  1556. {
  1557. if ($this->debug > 0) {
  1558. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1559. }
  1560. $total = 0;
  1561. foreach ($this->items as $temp2) {
  1562. if (!in_array(
  1563. $temp2->get_type(),
  1564. array(
  1565. 'dokeos_chapter',
  1566. 'chapter',
  1567. 'dir'
  1568. )
  1569. )
  1570. ) {
  1571. $total++;
  1572. }
  1573. }
  1574. return $total;
  1575. }
  1576. /**
  1577. * Gets the first element URL.
  1578. * @return string URL to load into the viewer
  1579. */
  1580. public function first()
  1581. {
  1582. if ($this->debug > 0) {
  1583. error_log('New LP - In learnpath::first()', 0);
  1584. error_log('$this->last_item_seen '.$this->last_item_seen);
  1585. //error_log('$this->items '.print_r($this->items, 1));
  1586. //error_log('$this->ordered_items '.print_r($this->ordered_items, 1));
  1587. }
  1588. // Test if the last_item_seen exists and is not a dir.
  1589. if (count($this->ordered_items) == 0) {
  1590. $this->index = 0;
  1591. }
  1592. if ($this->debug > 0) {
  1593. if (isset($this->items[$this->last_item_seen])) {
  1594. $status = $this->items[$this->last_item_seen]->get_status();
  1595. }
  1596. error_log('status '.$status);
  1597. }
  1598. if (!empty($this->last_item_seen) &&
  1599. !empty($this->items[$this->last_item_seen]) &&
  1600. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1601. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1602. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1603. //&& !$this->items[$this->last_item_seen]->is_done()
  1604. ) {
  1605. if ($this->debug > 2) {
  1606. error_log(
  1607. 'New LP - In learnpath::first() - Last item seen is '.$this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(
  1608. ),
  1609. 0
  1610. );
  1611. }
  1612. $index = -1;
  1613. foreach ($this->ordered_items as $myindex => $item_id) {
  1614. if ($item_id == $this->last_item_seen) {
  1615. $index = $myindex;
  1616. break;
  1617. }
  1618. }
  1619. if ($index == -1) {
  1620. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1621. if ($this->debug > 2) {
  1622. error_log(
  1623. 'New LP - Last item ('.$this->last_item_seen.') was found in items but not in ordered_items, panic!',
  1624. 0
  1625. );
  1626. }
  1627. return false;
  1628. } else {
  1629. $this->last = $this->last_item_seen;
  1630. $this->current = $this->last_item_seen;
  1631. $this->index = $index;
  1632. }
  1633. } else {
  1634. if ($this->debug > 2) {
  1635. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1636. }
  1637. $index = 0;
  1638. // Loop through all ordered items and stop at the first item that is
  1639. // not a directory *and* that has not been completed yet.
  1640. while (!empty($this->ordered_items[$index]) AND
  1641. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1642. (
  1643. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1644. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1645. $this->items[$this->ordered_items[$index]]->is_done() === true
  1646. ) AND $index < $this->max_ordered_items) {
  1647. $index++;
  1648. }
  1649. $this->last = $this->current;
  1650. // current is
  1651. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1652. $this->index = $index;
  1653. if ($this->debug > 2) {
  1654. error_log('$index '.$index);
  1655. }
  1656. if ($this->debug > 2) {
  1657. error_log(
  1658. 'New LP - In learnpath::first() - No last item seen. New last = '.$this->last.'('.$this->ordered_items[$index].')',
  1659. 0
  1660. );
  1661. }
  1662. }
  1663. if ($this->debug > 2) {
  1664. error_log('New LP - In learnpath::first() - First item is '.$this->get_current_item_id());
  1665. }
  1666. }
  1667. /**
  1668. * Gets the information about an item in a format usable as JavaScript to update
  1669. * the JS API by just printing this content into the <head> section of the message frame
  1670. * @param integer Item ID
  1671. * @return string
  1672. */
  1673. public function get_js_info($item_id = '')
  1674. {
  1675. if ($this->debug > 0) {
  1676. error_log('New LP - In learnpath::get_js_info('.$item_id.')', 0);
  1677. }
  1678. $info = '';
  1679. $item_id = Database::escape_string($item_id);
  1680. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1681. //if item is defined, return values from DB
  1682. $oItem = $this->items[$item_id];
  1683. $info .= '<script language="javascript">';
  1684. $info .= "top.set_score(".$oItem->get_score().");\n";
  1685. $info .= "top.set_max(".$oItem->get_max().");\n";
  1686. $info .= "top.set_min(".$oItem->get_min().");\n";
  1687. $info .= "top.set_lesson_status('".$oItem->get_status()."');";
  1688. $info .= "top.set_session_time('".$oItem->get_scorm_time('js')."');";
  1689. $info .= "top.set_suspend_data('".$oItem->get_suspend_data()."');";
  1690. $info .= "top.set_saved_lesson_status('".$oItem->get_status()."');";
  1691. $info .= "top.set_flag_synchronized();";
  1692. $info .= '</script>';
  1693. if ($this->debug > 2) {
  1694. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1695. }
  1696. return $info;
  1697. } else {
  1698. // If item_id is empty, just update to default SCORM data.
  1699. $info .= '<script language="javascript">';
  1700. $info .= "top.set_score(".learnpathItem :: get_score().");\n";
  1701. $info .= "top.set_max(".learnpathItem :: get_max().");\n";
  1702. $info .= "top.set_min(".learnpathItem :: get_min().");\n";
  1703. $info .= "top.set_lesson_status('".learnpathItem :: get_status()."');";
  1704. $info .= "top.set_session_time('".learnpathItem :: get_scorm_time('js')."');";
  1705. $info .= "top.set_suspend_data('".learnpathItem :: get_suspend_data()."');";
  1706. $info .= "top.set_saved_lesson_status('".learnpathItem :: get_status()."');";
  1707. $info .= "top.set_flag_synchronized();";
  1708. $info .= '</script>';
  1709. if ($this->debug > 2) {
  1710. error_log('New LP - in learnpath::get_js_info('.$item_id.') - returning: '.$info, 0);
  1711. }
  1712. return $info;
  1713. }
  1714. }
  1715. /**
  1716. * Gets the js library from the database
  1717. * @return string The name of the javascript library to be used
  1718. */
  1719. public function get_js_lib()
  1720. {
  1721. $lib = '';
  1722. if (!empty ($this->js_lib)) {
  1723. $lib = $this->js_lib;
  1724. }
  1725. return $lib;
  1726. }
  1727. /**
  1728. * Gets the learnpath database ID
  1729. * @return integer Learnpath ID in the lp table
  1730. */
  1731. public function get_id()
  1732. {
  1733. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_id()', 0); }
  1734. if (!empty ($this->lp_id)) {
  1735. return $this->lp_id;
  1736. } else {
  1737. return 0;
  1738. }
  1739. }
  1740. /**
  1741. * Gets the last element URL.
  1742. * @return string URL to load into the viewer
  1743. */
  1744. public function get_last()
  1745. {
  1746. if ($this->debug > 0) {
  1747. error_log('New LP - In learnpath::get_last()', 0);
  1748. }
  1749. $this->index = count($this->ordered_items) - 1;
  1750. return $this->ordered_items[$this->index];
  1751. }
  1752. /**
  1753. * Gets the navigation bar for the learnpath display screen
  1754. * @return string The HTML string to use as a navigation bar
  1755. */
  1756. public function get_navigation_bar()
  1757. {
  1758. if ($this->debug > 0) {
  1759. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1760. }
  1761. $navbar = null;
  1762. $lp_id = $this->lp_id;
  1763. $mycurrentitemid = $this->get_current_item_id();
  1764. if ($this->mode == 'fullscreen') {
  1765. $navbar = '
  1766. <div class="buttons">
  1767. <a href="lp_controller.php?action=stats&'.api_get_cidreq(
  1768. true
  1769. ).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name_blank" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="'.get_lang(
  1770. 'Reporting'
  1771. ).'"></a>
  1772. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="'.get_lang(
  1773. 'ScormPrevious'
  1774. ).'"></a>
  1775. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="'.get_lang(
  1776. 'ScormNext'
  1777. ).'"></a>.
  1778. <a href="lp_controller.php?action=mode&mode=embedded" target="_top" title="embedded mode"><img border="0" src="../img/view_choose.gif" title="'.get_lang(
  1779. 'ScormExitFullScreen'
  1780. ).'"></a>
  1781. </div>';
  1782. } else {
  1783. $navbar = '
  1784. <div class="buttons">
  1785. <a href="lp_controller.php?action=stats&'.api_get_cidreq(
  1786. true
  1787. ).'&lp_id='.$lp_id.'" onClick="window.parent.API.save_asset();return true;" target="content_name" title="stats" id="stats_link"><img border="0" src="../img/lp_stats.gif" title="'.get_lang(
  1788. 'Reporting'
  1789. ).'"></a>
  1790. <a href="" onClick="switch_item('.$mycurrentitemid.',\'previous\');return false;" title="previous"><img border="0" src="../img/lp_leftarrow.gif" title="'.get_lang(
  1791. 'ScormPrevious'
  1792. ).'"></a>
  1793. <a href="" onClick="switch_item('.$mycurrentitemid.',\'next\');return false;" title="next" ><img border="0" src="../img/lp_rightarrow.gif" title="'.get_lang(
  1794. 'ScormNext'
  1795. ).'"></a>
  1796. </div>';
  1797. }
  1798. return $navbar;
  1799. }
  1800. /**
  1801. * Gets the next resource in queue (url).
  1802. * @return string URL to load into the viewer
  1803. */
  1804. public function get_next_index()
  1805. {
  1806. if ($this->debug > 0) {
  1807. error_log('New LP - In learnpath::get_next_index()', 0);
  1808. }
  1809. // TODO
  1810. $index = $this->index;
  1811. $index++;
  1812. if ($this->debug > 2) {
  1813. error_log(
  1814. 'New LP - Now looking at ordered_items['.($index).'] - type is '.$this->items[$this->ordered_items[$index]]->type,
  1815. 0
  1816. );
  1817. }
  1818. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1819. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type(
  1820. ) == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1821. $index++;
  1822. if ($index == $this->max_ordered_items) {
  1823. if ($this->items[$this->ordered_items[$index]]->get_type(
  1824. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter'
  1825. ) {
  1826. return $this->index;
  1827. } else {
  1828. return $index;
  1829. }
  1830. }
  1831. }
  1832. if (empty ($this->ordered_items[$index])) {
  1833. return $this->index;
  1834. }
  1835. if ($this->debug > 2) {
  1836. error_log('New LP - index is now '.$index, 0);
  1837. }
  1838. return $index;
  1839. }
  1840. /**
  1841. * Gets item_id for the next element
  1842. * @return integer Next item (DB) ID
  1843. */
  1844. public function get_next_item_id()
  1845. {
  1846. if ($this->debug > 0) {
  1847. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1848. }
  1849. $new_index = $this->get_next_index();
  1850. if (!empty ($new_index)) {
  1851. if (isset ($this->ordered_items[$new_index])) {
  1852. if ($this->debug > 2) {
  1853. error_log(
  1854. 'New LP - In learnpath::get_next_index() - Returning '.$this->ordered_items[$new_index],
  1855. 0
  1856. );
  1857. }
  1858. return $this->ordered_items[$new_index];
  1859. }
  1860. }
  1861. if ($this->debug > 2) {
  1862. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1863. }
  1864. return 0;
  1865. }
  1866. /**
  1867. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1868. *
  1869. * Generally, the package provided is in the form of a zip file, so the function
  1870. * has been written to test a zip file. If not a zip, the function will return the
  1871. * default return value: ''
  1872. * @param string the path to the file
  1873. * @param string the original name of the file
  1874. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1875. */
  1876. public static function get_package_type($file_path, $file_name)
  1877. {
  1878. // Get name of the zip file without the extension.
  1879. $file_info = pathinfo($file_name);
  1880. $filename = $file_info['basename']; // Name including extension.
  1881. $extension = $file_info['extension']; // Extension only.
  1882. if (!empty($_POST['ppt2lp']) && !in_array(
  1883. strtolower($extension),
  1884. array(
  1885. 'dll',
  1886. 'exe'
  1887. )
  1888. )
  1889. ) {
  1890. return 'oogie';
  1891. }
  1892. if (!empty($_POST['woogie']) && !in_array(
  1893. strtolower($extension),
  1894. array(
  1895. 'dll',
  1896. 'exe'
  1897. )
  1898. )
  1899. ) {
  1900. return 'woogie';
  1901. }
  1902. $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
  1903. $zipFile = new PclZip($file_path);
  1904. // Check the zip content (real size and file extension).
  1905. $zipContentArray = $zipFile->listContent();
  1906. $package_type = '';
  1907. $at_root = false;
  1908. $manifest = '';
  1909. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1910. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1911. foreach ($zipContentArray as $thisContent) {
  1912. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1913. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1914. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1915. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1916. $package_type = 'scorm';
  1917. break; // Exit the foreach loop.
  1918. } elseif (preg_match('/aicc\//i', $thisContent['filename'])) {
  1919. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1920. $package_type = 'aicc';
  1921. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1922. } else {
  1923. $package_type = '';
  1924. }
  1925. }
  1926. }
  1927. return $package_type;
  1928. }
  1929. /**
  1930. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1931. * @return string URL to load into the viewer
  1932. */
  1933. public function get_previous_index()
  1934. {
  1935. if ($this->debug > 0) {
  1936. error_log('New LP - In learnpath::get_previous_index()', 0);
  1937. }
  1938. $index = $this->index;
  1939. if (isset ($this->ordered_items[$index - 1])) {
  1940. $index--;
  1941. while (isset ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type(
  1942. ) == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1943. $index--;
  1944. if ($index < 0) {
  1945. return $this->index;
  1946. }
  1947. }
  1948. } else {
  1949. if ($this->debug > 2) {
  1950. error_log('New LP - get_previous_index() - there was no previous index available, reusing '.$index, 0);
  1951. }
  1952. // There is no previous item.
  1953. }
  1954. return $index;
  1955. }
  1956. /**
  1957. * Gets item_id for the next element
  1958. * @return integer Previous item (DB) ID
  1959. */
  1960. public function get_previous_item_id()
  1961. {
  1962. if ($this->debug > 0) {
  1963. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1964. }
  1965. $new_index = $this->get_previous_index();
  1966. return $this->ordered_items[$new_index];
  1967. }
  1968. /**
  1969. * Gets the progress value from the progress_db attribute
  1970. * @return integer Current progress value
  1971. */
  1972. public function get_progress()
  1973. {
  1974. if ($this->debug > 0) {
  1975. error_log('New LP - In learnpath::get_progress()', 0);
  1976. }
  1977. if (!empty ($this->progress_db)) {
  1978. return $this->progress_db;
  1979. }
  1980. return 0;
  1981. }
  1982. /**
  1983. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1984. * @param integer Learnpath ID
  1985. * @param integer User ID
  1986. * @param string Mode of display ('%','abs' or 'both')
  1987. * @param string Course database name (optional, defaults to '')
  1988. * @param boolean Whether to return null if no record was found (true), or 0 (false) (optional, defaults to false)
  1989. * @return integer Current progress value as found in the database
  1990. */
  1991. public static function get_db_progress(
  1992. $lp_id,
  1993. $user_id,
  1994. $mode = '%',
  1995. $course_code = '',
  1996. $sincere = false,
  1997. $session_id = 0
  1998. ) {
  1999. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_db_progress()', 0); }
  2000. $session_id = intval($session_id);
  2001. $course_info = api_get_course_info($course_code);
  2002. $session_condition = api_get_session_condition($session_id);
  2003. $course_id = $course_info['real_id'];
  2004. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2005. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  2006. $res = Database::query($sql);
  2007. $view_id = 0;
  2008. if (Database :: num_rows($res) > 0) {
  2009. $row = Database :: fetch_array($res);
  2010. $progress = $row['progress'];
  2011. $view_id = $row['id'];
  2012. } else {
  2013. if ($sincere) {
  2014. return null;
  2015. }
  2016. }
  2017. if (empty ($progress)) {
  2018. $progress = '0';
  2019. }
  2020. if ($mode == '%') {
  2021. return $progress.'%';
  2022. } else {
  2023. // Get the number of items completed and the number of items total.
  2024. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2025. $sql = "SELECT count(*) FROM $tbl
  2026. WHERE c_id = $course_id AND c_id = ".$course_id." AND lp_id = ".$lp_id." AND item_type NOT IN('dokeos_chapter','chapter','dir')";
  2027. $res = Database::query($sql);
  2028. $row = Database :: fetch_array($res);
  2029. $total = $row[0];
  2030. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2031. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2032. //$sql = "SELECT count(distinct(lp_item_id)) FROM $tbl WHERE lp_view_id = ".$view_id." AND status IN ('passed','completed','succeeded')";
  2033. // Trying as also counting browsed and failed items.
  2034. $sql = "SELECT count(distinct(lp_item_id))
  2035. FROM $tbl_item_view as item_view
  2036. INNER JOIN $tbl_item as item
  2037. ON item.id = item_view.lp_item_id
  2038. AND item_type NOT IN('dokeos_chapter','chapter','dir')
  2039. WHERE
  2040. item_view.c_id = $course_id AND
  2041. item.c_id = $course_id AND
  2042. lp_view_id = ".$view_id." AND
  2043. status IN ('passed','completed','succeeded','browsed','failed')"; //echo '<br />';
  2044. $res = Database::query($sql);
  2045. $row = Database :: fetch_array($res);
  2046. $completed = $row[0];
  2047. if ($mode == 'abs') {
  2048. return $completed.'/'.$total;
  2049. } elseif ($mode == 'both') {
  2050. if ($progress < ($completed / ($total ? $total : 1))) {
  2051. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2052. }
  2053. return $progress.'% ('.$completed.'/'.$total.')';
  2054. }
  2055. }
  2056. return $progress;
  2057. }
  2058. /**
  2059. * Returns the HTML necessary to print a mediaplayer block inside a page
  2060. * @return string The mediaplayer HTML
  2061. */
  2062. public function get_mediaplayer($autostart = 'true')
  2063. {
  2064. $course_id = api_get_course_int_id();
  2065. global $_course;
  2066. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2067. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2068. // Getting all the information about the item.
  2069. $sql = "SELECT * FROM ".$tbl_lp_item." as lp INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id ".
  2070. "WHERE lp.id = '".$_SESSION['oLP']->current."' AND
  2071. lp.c_id = $course_id AND
  2072. lp_view.c_id = $course_id";
  2073. $result = Database::query($sql);
  2074. $row = Database::fetch_assoc($result);
  2075. $output = '';
  2076. if (!empty ($row['audio'])) {
  2077. $list = $_SESSION['oLP']->get_toc();
  2078. $type_quiz = false;
  2079. foreach ($list as $toc) {
  2080. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type'] == 'quiz')) {
  2081. $type_quiz = true;
  2082. }
  2083. }
  2084. if ($type_quiz) {
  2085. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2086. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2087. } else {
  2088. $autostart_audio = $autostart;
  2089. }
  2090. } else {
  2091. $autostart_audio = 'true';
  2092. }
  2093. // The mp3 player.
  2094. $output = '<div id="container">';
  2095. $output .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  2096. $output .= '<script type="text/javascript">
  2097. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  2098. s1.addParam("allowscriptaccess","always");
  2099. s1.addParam("flashvars","file='.api_get_path(
  2100. WEB_COURSE_PATH
  2101. ).$_course['path'].'/document/audio/'.$row['audio'].'&autostart='.$autostart_audio.'");
  2102. s1.write("container");
  2103. </script>
  2104. </div>';
  2105. }
  2106. return $output;
  2107. }
  2108. /**
  2109. * This function checks if the learnpath is visible for student after the progress of its prerequisite is completed, and considering time availability
  2110. * @param int Learnpath id
  2111. * @param int Student id
  2112. * @param string Course code (optional)
  2113. * @return bool True if
  2114. */
  2115. public static function is_lp_visible_for_student($lp_id, $student_id, $course_code = null)
  2116. {
  2117. $lp_id = (int)$lp_id;
  2118. $course_info = api_get_course_info($course_code);
  2119. $tbl_learnpath = Database :: get_course_table(TABLE_LP_MAIN);
  2120. // Get current prerequisite
  2121. $sql = "SELECT id, prerequisite, subscribe_users, publicated_on, expired_on
  2122. FROM $tbl_learnpath WHERE c_id = ".$course_info['real_id']." AND id = $lp_id";
  2123. $rs = Database::query($sql);
  2124. $now = time();
  2125. $session_id = api_get_session_id();
  2126. if (Database::num_rows($rs) > 0) {
  2127. $row = Database::fetch_array($rs, 'ASSOC');
  2128. $prerequisite = $row['prerequisite'];
  2129. $is_visible = true;
  2130. $progress = 0;
  2131. //Checking LP prerequisites
  2132. if (!empty($prerequisite)) {
  2133. $progress = intval(self::get_db_progress($prerequisite, $student_id, '%', '', false, $session_id));
  2134. if ($progress < 100) {
  2135. $is_visible = false;
  2136. }
  2137. }
  2138. // Also check the time availability of the LP
  2139. if ($is_visible) {
  2140. //Adding visibility restrictions
  2141. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  2142. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2143. //api_not_allowed();
  2144. $is_visible = false;
  2145. }
  2146. }
  2147. //Blocking empty start times see BT#2800
  2148. global $_custom;
  2149. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  2150. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2151. //api_not_allowed();
  2152. $is_visible = false;
  2153. }
  2154. }
  2155. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2156. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2157. //api_not_allowed();
  2158. $is_visible = false;
  2159. }
  2160. }
  2161. }
  2162. //Check if the subscription users/group to a LP is ON
  2163. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
  2164. //Checking only the user visibility
  2165. $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
  2166. if ($userVisibility == 1) {
  2167. $is_visible = true;
  2168. } else {
  2169. $is_visible = false;
  2170. }
  2171. //the group visibility is not going to be checked because the user *must* be related to the LP
  2172. /*$groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
  2173. if (!empty($groupList)) {
  2174. foreach($groupList as $groupId) {
  2175. $groupVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, 0, 'LearnpathSubscription', $groupId);
  2176. if ($groupVisibility == 1) {
  2177. $is_visible = true;
  2178. break;
  2179. }
  2180. }
  2181. }*/
  2182. }
  2183. return $is_visible;
  2184. }
  2185. return false;
  2186. }
  2187. /**
  2188. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  2189. * completed so far.
  2190. * @param string Mode in which we want the values
  2191. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  2192. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  2193. * @param boolean true if it comes from a Diplay LP view
  2194. * @return string HTML string containing the progress bar
  2195. */
  2196. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false)
  2197. {
  2198. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  2199. global $lp_theme_css;
  2200. // Setting up the CSS path of the current style if exists.
  2201. if (!empty ($lp_theme_css)) {
  2202. $css_path = api_get_path(WEB_CODE_PATH).'css/'.$lp_theme_css.'/images/';
  2203. } else {
  2204. $css_path = '../img/';
  2205. }
  2206. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  2207. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  2208. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2209. }
  2210. $text = $percentage.$text_add;
  2211. //@todo use Display::display_progress();
  2212. $output = '<div class="progress progress-striped">
  2213. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  2214. </div>
  2215. <div class="progresstext" id="progress_text">'.$text.'</div>';
  2216. return $output;
  2217. }
  2218. /**
  2219. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  2220. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  2221. * @param integer Additional steps to fake as completed
  2222. * @return list Percentage or number and symbol (% or /xx)
  2223. */
  2224. public function get_progress_bar_text($mode = '', $add = 0)
  2225. {
  2226. if ($this->debug > 0) {
  2227. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2228. }
  2229. if (empty ($mode)) {
  2230. $mode = $this->progress_bar_mode;
  2231. }
  2232. $total_items = $this->get_total_items_count_without_chapters();
  2233. if ($this->debug > 2) {
  2234. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2235. }
  2236. $i = $this->get_complete_items_count();
  2237. if ($this->debug > 2) {
  2238. error_log('New LP - Items completed so far: '.$i, 0);
  2239. }
  2240. if ($add != 0) {
  2241. $i += $add;
  2242. if ($this->debug > 2) {
  2243. error_log('New LP - Items completed so far (+modifier): '.$i, 0);
  2244. }
  2245. }
  2246. $text = '';
  2247. if ($i > $total_items) {
  2248. $i = $total_items;
  2249. }
  2250. if ($mode == '%') {
  2251. if ($total_items > 0) {
  2252. $percentage = ((float)$i / (float)$total_items) * 100;
  2253. } else {
  2254. $percentage = 0;
  2255. }
  2256. $percentage = number_format($percentage, 0);
  2257. $text = '%';
  2258. } elseif ($mode == 'abs') {
  2259. $percentage = $i;
  2260. $text = '/'.$total_items;
  2261. }
  2262. return array(
  2263. $percentage,
  2264. $text
  2265. );
  2266. }
  2267. /**
  2268. * Gets the progress bar mode
  2269. * @return string The progress bar mode attribute
  2270. */
  2271. public function get_progress_bar_mode()
  2272. {
  2273. if ($this->debug > 0) {
  2274. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2275. }
  2276. if (!empty ($this->progress_bar_mode)) {
  2277. return $this->progress_bar_mode;
  2278. } else {
  2279. return '%';
  2280. }
  2281. }
  2282. /**
  2283. * Gets the learnpath proximity (remote or local)
  2284. * @return string Learnpath proximity
  2285. */
  2286. public function get_proximity()
  2287. {
  2288. if ($this->debug > 0) {
  2289. error_log('New LP - In learnpath::get_proximity()', 0);
  2290. }
  2291. if (!empty ($this->proximity)) {
  2292. return $this->proximity;
  2293. } else {
  2294. return '';
  2295. }
  2296. }
  2297. /**
  2298. * Gets the learnpath theme (remote or local)
  2299. * @return string Learnpath theme
  2300. */
  2301. public function get_theme()
  2302. {
  2303. if ($this->debug > 0) {
  2304. error_log('New LP - In learnpath::get_theme()', 0);
  2305. }
  2306. if (!empty ($this->theme)) {
  2307. return $this->theme;
  2308. } else {
  2309. return '';
  2310. }
  2311. }
  2312. /**
  2313. * Gets the learnpath session id
  2314. * @return string Learnpath theme
  2315. */
  2316. public function get_lp_session_id()
  2317. {
  2318. if ($this->debug > 0) {
  2319. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2320. }
  2321. if (!empty ($this->lp_session_id)) {
  2322. return $this->lp_session_id;
  2323. } else {
  2324. return 0;
  2325. }
  2326. }
  2327. /**
  2328. * Gets the learnpath image
  2329. * @return string Web URL of the LP image
  2330. */
  2331. public function get_preview_image()
  2332. {
  2333. if ($this->debug > 0) {
  2334. error_log('New LP - In learnpath::get_preview_image()', 0);
  2335. }
  2336. if (!empty($this->preview_image)) {
  2337. return $this->preview_image;
  2338. } else {
  2339. return '';
  2340. }
  2341. }
  2342. public function get_preview_image_path($size = null, $path_type = 'web')
  2343. {
  2344. $preview_image = $this->get_preview_image();
  2345. if (isset($preview_image) && !empty($preview_image)) {
  2346. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2347. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2348. if (isset($size)) {
  2349. $info = pathinfo($preview_image);
  2350. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2351. if (file_exists($image_sys_path.$image_custom_size)) {
  2352. if ($path_type == 'web') {
  2353. return $image_path.$image_custom_size;
  2354. } else {
  2355. return $image_sys_path.$image_custom_size;
  2356. }
  2357. }
  2358. } else {
  2359. if ($path_type == 'web') {
  2360. return $image_path.$preview_image;
  2361. } else {
  2362. return $image_sys_path.$preview_image;
  2363. }
  2364. }
  2365. }
  2366. return false;
  2367. }
  2368. public function get_category_id()
  2369. {
  2370. return $this->category_id;
  2371. }
  2372. /**
  2373. * Gets the learnpath author
  2374. * @return string LP's author
  2375. */
  2376. public function get_author()
  2377. {
  2378. if ($this->debug > 0) {
  2379. error_log('New LP - In learnpath::get_author()', 0);
  2380. }
  2381. if (!empty ($this->author)) {
  2382. return $this->author;
  2383. } else {
  2384. return '';
  2385. }
  2386. }
  2387. /**
  2388. * Gets the learnpath author
  2389. * @return string LP's author
  2390. */
  2391. public function get_hide_toc_frame()
  2392. {
  2393. if ($this->debug > 0) {
  2394. error_log('New LP - In learnpath::get_author()', 0);
  2395. }
  2396. if (!empty ($this->hide_toc_frame)) {
  2397. return $this->hide_toc_frame;
  2398. } else {
  2399. return '';
  2400. }
  2401. }
  2402. /**
  2403. * Generate a new prerequisites string for a given item. If this item was a sco and
  2404. * its prerequisites were strings (instead of IDs), then transform those strings into
  2405. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2406. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2407. * same rule as the scorm_export() method
  2408. * @param integer Item ID
  2409. * @return string Prerequisites string ready for the export as SCORM
  2410. */
  2411. public function get_scorm_prereq_string($item_id)
  2412. {
  2413. if ($this->debug > 0) {
  2414. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2415. }
  2416. if (!is_object($this->items[$item_id])) {
  2417. return false;
  2418. }
  2419. $oItem = $this->items[$item_id];
  2420. $prereq = $oItem->get_prereq_string();
  2421. if (empty($prereq)) {
  2422. return '';
  2423. }
  2424. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2425. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2426. // then simply return it (with the ITEM_ prefix).
  2427. //return 'ITEM_' . $prereq;
  2428. return $this->items[$prereq]->ref;
  2429. } else {
  2430. if (isset($this->refs_list[$prereq])) {
  2431. // It's a simple string item from which the ID can be found in the refs list,
  2432. // so we can transform it directly to an ID for export.
  2433. return $this->items[$this->refs_list[$prereq]]->ref;
  2434. } else {
  2435. if (isset($this->refs_list['ITEM_'.$prereq])) {
  2436. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2437. } else {
  2438. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2439. // and replace them, one by one, by the internal IDs (chamilo db)
  2440. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2441. // by a space as well.
  2442. $find = array(
  2443. '&',
  2444. '|',
  2445. '~',
  2446. '=',
  2447. '<>',
  2448. '{',
  2449. '}',
  2450. '*',
  2451. '(',
  2452. ')'
  2453. );
  2454. $replace = array(
  2455. ' ',
  2456. ' ',
  2457. ' ',
  2458. ' ',
  2459. ' ',
  2460. ' ',
  2461. ' ',
  2462. ' ',
  2463. ' ',
  2464. ' '
  2465. );
  2466. $prereq_mod = str_replace($find, $replace, $prereq);
  2467. $ids = split(' ', $prereq_mod);
  2468. foreach ($ids as $id) {
  2469. $id = trim($id);
  2470. if (isset ($this->refs_list[$id])) {
  2471. $prereq = preg_replace(
  2472. '/[^a-zA-Z_0-9]('.$id.')[^a-zA-Z_0-9]/',
  2473. 'ITEM_'.$this->refs_list[$id],
  2474. $prereq
  2475. );
  2476. }
  2477. }
  2478. error_log(
  2479. 'New LP - In learnpath::get_scorm_prereq_string(): returning modified string: '.$prereq,
  2480. 0
  2481. );
  2482. return $prereq;
  2483. }
  2484. }
  2485. }
  2486. }
  2487. /**
  2488. * Returns the XML DOM document's node
  2489. * @param resource Reference to a list of objects to search for the given ITEM_*
  2490. * @param string The identifier to look for
  2491. * @return mixed The reference to the element found with that identifier. False if not found
  2492. */
  2493. public function get_scorm_xml_node(& $children, $id)
  2494. {
  2495. for ($i = 0; $i < $children->length; $i++) {
  2496. $item_temp = $children->item($i);
  2497. if ($item_temp->nodeName == 'item') {
  2498. if ($item_temp->getAttribute('identifier') == $id) {
  2499. return $item_temp;
  2500. }
  2501. }
  2502. $subchildren = $item_temp->childNodes;
  2503. if ($subchildren->length > 0) {
  2504. $val = $this->get_scorm_xml_node($subchildren, $id);
  2505. if (is_object($val)) {
  2506. return $val;
  2507. }
  2508. }
  2509. }
  2510. return false;
  2511. }
  2512. /**
  2513. * Returns a usable array of stats related to the current learnpath and user
  2514. * @return array Well-formatted array containing status for the current learnpath
  2515. */
  2516. public function get_stats()
  2517. {
  2518. if ($this->debug > 0) {
  2519. error_log('New LP - In learnpath::get_stats()', 0);
  2520. }
  2521. // TODO
  2522. }
  2523. /**
  2524. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2525. * the given course (for all learnpaths and all users)
  2526. * @param string Course code
  2527. * @return array Well-formatted array containing status for the course's learnpaths
  2528. */
  2529. public function get_stats_course($course)
  2530. {
  2531. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2532. // TODO
  2533. }
  2534. /**
  2535. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2536. * the given course and learnpath (for all users)
  2537. * @param string Course code
  2538. * @param integer Learnpath ID
  2539. * @return array Well-formatted array containing status for the specified learnpath
  2540. */
  2541. public function get_stats_lp($course, $lp)
  2542. {
  2543. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2544. // TODO
  2545. }
  2546. /**
  2547. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2548. * the given course, learnpath and user.
  2549. * @param string Course code
  2550. * @param integer Learnpath ID
  2551. * @param integer User ID
  2552. * @return array Well-formatted array containing status for the specified learnpath and user
  2553. */
  2554. public function get_stats_lp_user($course, $lp, $user)
  2555. {
  2556. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2557. // TODO
  2558. }
  2559. /**
  2560. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2561. * the given course and learnpath (for all users)
  2562. * @param string Course code
  2563. * @param integer User ID
  2564. * @return array Well-formatted array containing status for the user's learnpaths
  2565. */
  2566. public function get_stats_user($course, $user)
  2567. {
  2568. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2569. // TODO
  2570. }
  2571. /**
  2572. * Gets the status list for all LP's items
  2573. * @return array Array of [index] => [item ID => current status]
  2574. */
  2575. public function get_items_status_list()
  2576. {
  2577. if ($this->debug > 0) {
  2578. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2579. }
  2580. $list = array();
  2581. foreach ($this->ordered_items as $item_id) {
  2582. $list[] = array(
  2583. $item_id => $this->items[$item_id]->get_status()
  2584. );
  2585. }
  2586. return $list;
  2587. }
  2588. /**
  2589. * Return the number of interactions for the given learnpath Item View ID.
  2590. * This method can be used as static.
  2591. * @param integer Item View ID
  2592. * @param integer course id
  2593. * @return integer Number of interactions
  2594. */
  2595. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2596. {
  2597. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2598. $lp_iv_id = intval($lp_iv_id);
  2599. $course_id = intval($course_id);
  2600. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2601. $res = Database::query($sql);
  2602. $res = 0;
  2603. $num = 0;
  2604. if (Database::num_rows($res)) {
  2605. $row = Database::fetch_array($res);
  2606. $num = $row[0];
  2607. }
  2608. return $num;
  2609. }
  2610. /**
  2611. * Return the interactions as an array for the given lp_iv_id.
  2612. * This method can be used as static.
  2613. * @param integer Learnpath Item View ID
  2614. * @return array
  2615. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2616. */
  2617. public static function get_iv_interactions_array($lp_iv_id = 0)
  2618. {
  2619. $course_id = api_get_course_int_id();
  2620. $list = array();
  2621. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2622. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2623. $res = Database::query($sql);
  2624. $num = Database :: num_rows($res);
  2625. if ($num > 0) {
  2626. $list[] = array(
  2627. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2628. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2629. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2630. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2631. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2632. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2633. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2634. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2635. );
  2636. while ($row = Database :: fetch_array($res)) {
  2637. $list[] = array(
  2638. 'order_id' => ($row['order_id'] + 1),
  2639. 'id' => urldecode($row['interaction_id']),
  2640. //urldecode because they often have %2F or stuff like that
  2641. 'type' => $row['interaction_type'],
  2642. 'time' => $row['completion_time'],
  2643. //'correct_responses' => $row['correct_responses'],
  2644. 'correct_responses' => '',
  2645. // Hide correct responses from students.
  2646. 'student_response' => $row['student_response'],
  2647. 'result' => $row['result'],
  2648. 'latency' => $row['latency']
  2649. );
  2650. }
  2651. }
  2652. return $list;
  2653. }
  2654. /**
  2655. * Return the number of objectives for the given learnpath Item View ID.
  2656. * This method can be used as static.
  2657. * @param integer Item View ID
  2658. * @return integer Number of objectives
  2659. */
  2660. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2661. {
  2662. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2663. $course_id = intval($course_id);
  2664. $lp_iv_id = intval($lp_iv_id);
  2665. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2666. //@todo seems that this always returns 0
  2667. $res = Database::query($sql);
  2668. $res = 0;
  2669. $num = 0;
  2670. if (Database::num_rows($res)) {
  2671. $row = Database :: fetch_array($res);
  2672. $num = $row[0];
  2673. }
  2674. return $num;
  2675. }
  2676. /**
  2677. * Return the objectives as an array for the given lp_iv_id.
  2678. * This method can be used as static.
  2679. * @param integer Learnpath Item View ID
  2680. * @return array
  2681. * @todo Translate labels
  2682. */
  2683. public function get_iv_objectives_array($lp_iv_id = 0)
  2684. {
  2685. $course_id = api_get_course_int_id();
  2686. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2687. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2688. $res = Database::query($sql);
  2689. $num = Database :: num_rows($res);
  2690. $list = array();
  2691. if ($num > 0) {
  2692. $list[] = array(
  2693. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2694. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2695. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2696. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2697. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2698. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2699. );
  2700. while ($row = Database :: fetch_array($res)) {
  2701. $list[] = array(
  2702. 'order_id' => ($row['order_id'] + 1),
  2703. 'objective_id' => urldecode($row['objective_id']),
  2704. // urldecode() because they often have %2F or stuff like that.
  2705. 'score_raw' => $row['score_raw'],
  2706. 'score_max' => $row['score_max'],
  2707. 'score_min' => $row['score_min'],
  2708. 'status' => $row['status']
  2709. );
  2710. }
  2711. }
  2712. return $list;
  2713. }
  2714. /**
  2715. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2716. * used by get_html_toc() to be ready to display
  2717. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2718. */
  2719. public function get_toc()
  2720. {
  2721. if ($this->debug > 0) {
  2722. error_log('learnpath::get_toc()', 0);
  2723. }
  2724. $toc = array();
  2725. //echo "<pre>".print_r($this->items,true)."</pre>";
  2726. foreach ($this->ordered_items as $item_id) {
  2727. if ($this->debug > 2) {
  2728. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2729. }
  2730. // TODO: Change this link generation and use new function instead.
  2731. $toc[] = array(
  2732. 'id' => $item_id,
  2733. 'title' => $this->items[$item_id]->get_title(),
  2734. 'status' => $this->items[$item_id]->get_status(),
  2735. 'level' => $this->items[$item_id]->get_level(),
  2736. 'type' => $this->items[$item_id]->get_type(),
  2737. 'description' => $this->items[$item_id]->get_description(),
  2738. 'path' => $this->items[$item_id]->get_path(),
  2739. );
  2740. }
  2741. if ($this->debug > 2) {
  2742. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2743. }
  2744. return $toc;
  2745. }
  2746. /**
  2747. * Generate and return the table of contents for this learnpath. The JS
  2748. * table returned is used inside of scorm_api.php
  2749. * @return string A JS array vairiable construction
  2750. */
  2751. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2752. {
  2753. if ($this->debug > 0) {
  2754. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2755. }
  2756. $toc = $varname.' = new Array();';
  2757. //echo "<pre>".print_r($this->items,true)."</pre>";
  2758. foreach ($this->ordered_items as $item_id) {
  2759. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2760. }
  2761. if ($this->debug > 2) {
  2762. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2763. }
  2764. return $toc;
  2765. }
  2766. /**
  2767. * Gets the learning path type
  2768. * @param boolean Return the name? If false, return the ID. Default is false.
  2769. * @return mixed Type ID or name, depending on the parameter
  2770. */
  2771. public function get_type($get_name = false)
  2772. {
  2773. $res = false;
  2774. if ($this->debug > 0) {
  2775. error_log('New LP - In learnpath::get_type()', 0);
  2776. }
  2777. if (!empty ($this->type)) {
  2778. if ($get_name) {
  2779. // Get it from the lp_type table in main db.
  2780. } else {
  2781. $res = $this->type;
  2782. }
  2783. }
  2784. if ($this->debug > 2) {
  2785. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2786. }
  2787. return $res;
  2788. }
  2789. /**
  2790. * Gets the learning path type as static method
  2791. * @param boolean Return the name? If false, return the ID. Default is false.
  2792. * @return mixed Type ID or name, depending on the parameter
  2793. */
  2794. public static function get_type_static($lp_id = 0)
  2795. {
  2796. $course_id = api_get_course_int_id();
  2797. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2798. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '".$lp_id."'";
  2799. $res = Database::query($sql);
  2800. if ($res === false) {
  2801. return null;
  2802. }
  2803. if (Database :: num_rows($res) <= 0) {
  2804. return null;
  2805. }
  2806. $row = Database :: fetch_array($res);
  2807. return $row['lp_type'];
  2808. }
  2809. /**
  2810. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2811. * This method can be used as abstract and is recursive
  2812. * @param integer Learnpath ID
  2813. * @param integer Parent ID of the items to look for
  2814. * @return mixed Ordered list of item IDs or false on error
  2815. */
  2816. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2817. {
  2818. if (empty($course_id)) {
  2819. $course_id = api_get_course_int_id();
  2820. } else {
  2821. $course_id = intval($course_id);
  2822. }
  2823. $list = array();
  2824. if (empty ($lp)) {
  2825. return false;
  2826. }
  2827. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2828. $sql = "SELECT id FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent ORDER BY display_order";
  2829. $res = Database::query($sql);
  2830. while ($row = Database :: fetch_array($res)) {
  2831. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2832. $list[] = $row['id'];
  2833. foreach ($sublist as $item) {
  2834. $list[] = $item;
  2835. }
  2836. }
  2837. return $list;
  2838. }
  2839. /**
  2840. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2841. * @return string HTML TOC ready to display
  2842. */
  2843. public function get_html_toc($toc_list = null)
  2844. {
  2845. global $_configuration;
  2846. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2847. if ($this->debug > 0) {
  2848. error_log('In learnpath::get_html_toc()', 0);
  2849. }
  2850. if (empty($toc_list)) {
  2851. $toc_list = $this->get_toc();
  2852. }
  2853. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()).'</div>';
  2854. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2855. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2856. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2857. if ($this->get_lp_session_id() == api_get_session_id()) {
  2858. $html .= '<div id="actions_lp" class="actions_lp">';
  2859. $html .= '<div class="btn-group">';
  2860. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2861. )."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2862. 'Overview'
  2863. )."</a>";
  2864. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2865. )."&amp;action=add_item&amp;type=step&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2866. 'Edit'
  2867. )."</a>";
  2868. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq(
  2869. )."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=".$this->lp_id.'">'.get_lang('Settings').'</a>';
  2870. $html .= '</div>';
  2871. $html .= '</div>';
  2872. }
  2873. }
  2874. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2875. require_once 'resourcelinker.inc.php';
  2876. // Temporary variables.
  2877. $mycurrentitemid = $this->get_current_item_id();
  2878. $color_counter = 0;
  2879. $i = 0;
  2880. foreach ($toc_list as $item) {
  2881. // TODO: Complete this
  2882. $icon_name = array(
  2883. 'not attempted' => '../img/notattempted.gif',
  2884. 'incomplete' => '../img/incomplete.png',
  2885. 'failed' => '../img/delete.png',
  2886. 'completed' => '../img/completed.png',
  2887. 'passed' => '../img/passed.png',
  2888. 'succeeded' => '../img/succeeded.png',
  2889. 'browsed' => '../img/completed.png',
  2890. );
  2891. $style = 'scorm_item';
  2892. $scorm_color_background = 'scorm_item';
  2893. $style_item = 'scorm_item';
  2894. $current = false;
  2895. if ($item['id'] == $this->current) {
  2896. $style = 'scorm_item_highlight';
  2897. $scorm_color_background = 'scorm_item_highlight';
  2898. } else {
  2899. if ($color_counter % 2 == 0) {
  2900. $scorm_color_background = 'scorm_item_1';
  2901. } else {
  2902. $scorm_color_background = 'scorm_item_2';
  2903. }
  2904. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2905. $scorm_color_background = ' scorm_item_section ';
  2906. }
  2907. }
  2908. if ($scorm_color_background != '') {
  2909. $html .= '<div id="toc_'.$item['id'].'" class="'.$scorm_color_background.'">';
  2910. }
  2911. // The anchor will let us center the TOC on the currently viewed item &^D
  2912. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2913. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 1.5).'em; padding-right:'.($item['level'] / 2).'em" title="'.$item['description'].'" >';
  2914. $html .= '<a name="atoc_'.$item['id'].'" />';
  2915. } else {
  2916. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 2).'em; padding-right:'.($item['level'] * 1.5).'em" title="'.$item['description'].'" >';
  2917. }
  2918. $title = $item['title'];
  2919. if (empty ($title)) {
  2920. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2921. }
  2922. $title = Security::remove_XSS($title);
  2923. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2924. //$html .= "<a href='lp_controller.php?".api_get_cidreq()."&action=content&lp_id=".$this->get_id()."&item_id=".$item['id']."' target='lp_content_frame_name'>".$title."</a>" ;
  2925. $url = $this->get_link('http', $item['id'], $toc_list);
  2926. //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
  2927. //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
  2928. //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
  2929. $html .= '<a href="" onClick="switch_item('.$mycurrentitemid.','.$item['id'].');'.'return false;" >'.stripslashes(
  2930. $title
  2931. ).'</a>';
  2932. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2933. $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;".stripslashes(
  2934. $title
  2935. );
  2936. } elseif ($item['type'] == 'dir') {
  2937. $html .= stripslashes($title);
  2938. }
  2939. /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  2940. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2941. $user_id = api_get_user_id();
  2942. $sql = "SELECT path FROM $tbl_track_e_exercises, $tbl_lp_item
  2943. WHERE c_id = $course_id AND
  2944. path = '" . $item['path'] . "' AND
  2945. exe_user_id = '$user_id' AND
  2946. exe_cours_id = '$course_code' AND
  2947. path = exe_exo_id AND
  2948. status <> 'incomplete'";
  2949. $result = Database::query($sql);
  2950. $count = Database :: num_rows($result);*/
  2951. if ($item['type'] == 'quiz') {
  2952. error_log("1-->>>>>>>>>>>>>>>>");
  2953. error_log($item['status']);
  2954. if ($item['status'] == 'completed') {
  2955. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr(
  2956. $item['status'],
  2957. 0,
  2958. 1
  2959. )."' width='14' />";
  2960. } else {
  2961. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name['not attempted']."' alt='".substr(
  2962. 'not attempted',
  2963. 0,
  2964. 1
  2965. )."' width='14' />";
  2966. }
  2967. } else {
  2968. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2969. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr(
  2970. $item['status'],
  2971. 0,
  2972. 1
  2973. )."' width='14' />";
  2974. }
  2975. }
  2976. $html .= "</div>";
  2977. if ($scorm_color_background != '') {
  2978. $html .= '</div>';
  2979. }
  2980. $color_counter++;
  2981. }
  2982. $html .= "</div>";
  2983. return $html;
  2984. }
  2985. /**
  2986. * Gets the learnpath maker name - generally the editor's name
  2987. * @return string Learnpath maker name
  2988. */
  2989. public function get_maker()
  2990. {
  2991. if ($this->debug > 0) {
  2992. error_log('New LP - In learnpath::get_maker()', 0);
  2993. }
  2994. if (!empty ($this->maker)) {
  2995. return $this->maker;
  2996. } else {
  2997. return '';
  2998. }
  2999. }
  3000. /**
  3001. * Gets the user-friendly message stored in $this->message
  3002. * @return string Message
  3003. */
  3004. public function get_message()
  3005. {
  3006. if ($this->debug > 0) {
  3007. error_log('New LP - In learnpath::get_message()', 0);
  3008. }
  3009. return $this->message;
  3010. }
  3011. /**
  3012. * Gets the learnpath name/title
  3013. * @return string Learnpath name/title
  3014. */
  3015. public function get_name()
  3016. {
  3017. if ($this->debug > 0) {
  3018. error_log('New LP - In learnpath::get_name()', 0);
  3019. }
  3020. if (!empty ($this->name)) {
  3021. return $this->name;
  3022. } else {
  3023. return 'N/A';
  3024. }
  3025. }
  3026. /**
  3027. * Gets a link to the resource from the present location, depending on item ID.
  3028. * @param string Type of link expected
  3029. * @param integer Learnpath item ID
  3030. * @return string Link to the lp_item resource
  3031. */
  3032. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3033. {
  3034. $course_id = $this->get_course_int_id();
  3035. if ($this->debug > 0) {
  3036. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3037. }
  3038. if (empty($item_id)) {
  3039. if ($this->debug > 2) {
  3040. error_log(
  3041. 'New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(
  3042. ),
  3043. 0
  3044. );
  3045. }
  3046. $item_id = $this->get_current_item_id();
  3047. }
  3048. if (empty($item_id)) {
  3049. if ($this->debug > 2) {
  3050. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3051. }
  3052. //still empty, this means there was no item_id given and we are not in an object context or
  3053. //the object property is empty, return empty link
  3054. $item_id = $this->first();
  3055. return '';
  3056. }
  3057. $file = '';
  3058. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3059. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3060. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3061. $item_id = Database::escape_string($item_id);
  3062. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3063. FROM $lp_table l
  3064. INNER JOIN $lp_item_table li
  3065. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3066. WHERE li.id = $item_id ";
  3067. if ($this->debug > 2) {
  3068. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3069. }
  3070. $res = Database::query($sql);
  3071. if (Database :: num_rows($res) > 0) {
  3072. $row = Database :: fetch_array($res);
  3073. $lp_type = $row['ltype'];
  3074. $lp_path = $row['lpath'];
  3075. $lp_item_type = $row['litype'];
  3076. $lp_item_path = $row['lipath'];
  3077. $lp_item_params = $row['liparams'];
  3078. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3079. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3080. }
  3081. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3082. if ($type == 'http') {
  3083. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3084. } else {
  3085. $course_path = $sys_course_path; //system path
  3086. }
  3087. // Fixed issue BT#1272 - If the item type is a Chamilo Item (quiz, link, etc), then change the lp type to thread it as a normal Chamilo LP not a SCO.
  3088. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3089. $lp_type = 1;
  3090. }
  3091. if ($this->debug > 2) {
  3092. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3093. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3094. }
  3095. // Now go through the specific cases to get the end of the path
  3096. // @todo Use constants instead of int values.
  3097. switch ($lp_type) {
  3098. case 1 :
  3099. if ($lp_item_type == 'dokeos_chapter') {
  3100. $file = 'lp_content.php?type=dir';
  3101. } else {
  3102. require_once 'resourcelinker.inc.php';
  3103. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3104. if ($this->debug > 0) {
  3105. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3106. }
  3107. if ($lp_item_type == 'link') {
  3108. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3109. if (is_youtube_link($file)) {
  3110. $src = get_youtube_video_id($file);
  3111. $file = 'embed.php?type=youtube&src='.$src;
  3112. }
  3113. } else {
  3114. // check how much attempts of a exercise exits in lp
  3115. $lp_item_id = $this->get_current_item_id();
  3116. $lp_view_id = $this->get_view_id();
  3117. $prevent_reinit = null;
  3118. if (isset($this->items[$this->current])) {
  3119. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3120. }
  3121. if (empty($provided_toc)) {
  3122. if ($this->debug > 0) {
  3123. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3124. }
  3125. $list = $this->get_toc();
  3126. } else {
  3127. if ($this->debug > 0) {
  3128. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3129. }
  3130. $list = $provided_toc;
  3131. }
  3132. $type_quiz = false;
  3133. foreach ($list as $toc) {
  3134. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3135. $type_quiz = true;
  3136. }
  3137. }
  3138. if ($type_quiz) {
  3139. $lp_item_id = Database::escape_string($lp_item_id);
  3140. $lp_view_id = Database::escape_string($lp_view_id);
  3141. $sql = "SELECT count(*) FROM $lp_item_view_table
  3142. WHERE c_id = $course_id AND lp_item_id='".(int)$lp_item_id."' AND lp_view_id ='".(int)$lp_view_id."' AND status='completed'";
  3143. $result = Database::query($sql);
  3144. $row_count = Database :: fetch_row($result);
  3145. $count_item_view = (int)$row_count[0];
  3146. $not_multiple_attempt = 0;
  3147. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3148. $not_multiple_attempt = 1;
  3149. }
  3150. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3151. }
  3152. $tmp_array = explode('/', $file);
  3153. $document_name = $tmp_array[count($tmp_array) - 1];
  3154. if (strpos($document_name, '_DELETED_')) {
  3155. $file = 'blank.php?error=document_deleted';
  3156. }
  3157. }
  3158. }
  3159. break;
  3160. case 2 :
  3161. if ($this->debug > 2) {
  3162. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3163. }
  3164. if ($lp_item_type != 'dir') {
  3165. // Quite complex here:
  3166. // We want to make sure 'http://' (and similar) links can
  3167. // be loaded as is (withouth the Chamilo path in front) but
  3168. // some contents use this form: resource.htm?resource=http://blablabla
  3169. // which means we have to find a protocol at the path's start, otherwise
  3170. // it should not be considered as an external URL.
  3171. //if ($this->prerequisites_match($item_id)) {
  3172. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3173. if ($this->debug > 2) {
  3174. error_log(
  3175. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3176. 0
  3177. );
  3178. }
  3179. // Distant url, return as is.
  3180. $file = $lp_item_path;
  3181. } else {
  3182. if ($this->debug > 2) {
  3183. error_log(
  3184. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3185. 0
  3186. );
  3187. }
  3188. // Prevent getting untranslatable urls.
  3189. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3190. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3191. // Prepare the path.
  3192. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3193. // TODO: Fix this for urls with protocol header.
  3194. $file = str_replace('//', '/', $file);
  3195. $file = str_replace(':/', '://', $file);
  3196. if (substr($lp_path, -1) == '/') {
  3197. $lp_path = substr($lp_path, 0, -1);
  3198. }
  3199. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3200. // if file not found.
  3201. $decoded = html_entity_decode($lp_item_path);
  3202. list ($decoded) = explode('?', $decoded);
  3203. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3204. require_once 'resourcelinker.inc.php';
  3205. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3206. if (empty($file)) {
  3207. $file = 'blank.php?error=document_not_found';
  3208. } else {
  3209. $tmp_array = explode('/', $file);
  3210. $document_name = $tmp_array[count($tmp_array) - 1];
  3211. if (strpos($document_name, '_DELETED_')) {
  3212. $file = 'blank.php?error=document_deleted';
  3213. } else {
  3214. $file = 'blank.php?error=document_not_found';
  3215. }
  3216. }
  3217. } else {
  3218. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3219. }
  3220. }
  3221. }
  3222. //}else{
  3223. //prerequisites did not match
  3224. //$file = 'blank.php';
  3225. //}
  3226. // We want to use parameters if they were defined in the imsmanifest
  3227. if (strpos($file, 'blank.php') === false) {
  3228. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3229. }
  3230. } else {
  3231. $file = 'lp_content.php?type=dir';
  3232. }
  3233. break;
  3234. case 3 :
  3235. if ($this->debug > 2) {
  3236. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3237. }
  3238. // Formatting AICC HACP append URL.
  3239. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(
  3240. api_get_path(WEB_CODE_PATH).'newscorm/aicc_hacp.php'
  3241. ).'&';
  3242. if ($lp_item_type != 'dir') {
  3243. // Quite complex here:
  3244. // We want to make sure 'http://' (and similar) links can
  3245. // be loaded as is (withouth the Chamilo path in front) but
  3246. // some contents use this form: resource.htm?resource=http://blablabla
  3247. // which means we have to find a protocol at the path's start, otherwise
  3248. // it should not be considered as an external URL.
  3249. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3250. if ($this->debug > 2) {
  3251. error_log(
  3252. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3253. 0
  3254. );
  3255. }
  3256. // Distant url, return as is.
  3257. $file = $lp_item_path;
  3258. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3259. /*
  3260. if (stristr($file,'<servername>') !== false) {
  3261. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3262. }
  3263. */
  3264. if (stripos($file, '<servername>') !== false) {
  3265. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3266. $web_course_path = str_replace(
  3267. 'https://',
  3268. '',
  3269. str_replace('http://', '', $course_path)
  3270. );
  3271. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3272. }
  3273. //
  3274. $file .= $aicc_append;
  3275. } else {
  3276. if ($this->debug > 2) {
  3277. error_log(
  3278. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3279. 0
  3280. );
  3281. }
  3282. // Prevent getting untranslatable urls.
  3283. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3284. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3285. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3286. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3287. // TODO: Fix this for urls with protocol header.
  3288. $file = str_replace('//', '/', $file);
  3289. $file = str_replace(':/', '://', $file);
  3290. $file .= $aicc_append;
  3291. }
  3292. } else {
  3293. $file = 'lp_content.php?type=dir';
  3294. }
  3295. break;
  3296. case 4 :
  3297. break;
  3298. default :
  3299. break;
  3300. }
  3301. }
  3302. if ($this->debug > 2) {
  3303. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3304. }
  3305. return $file;
  3306. }
  3307. /**
  3308. * Gets the latest usable view or generate a new one
  3309. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3310. * @return integer DB lp_view id
  3311. */
  3312. public function get_view($attempt_num = 0)
  3313. {
  3314. if ($this->debug > 0) {
  3315. error_log('New LP - In learnpath::get_view()', 0);
  3316. }
  3317. $search = '';
  3318. // Use $attempt_num to enable multi-views management (disabled so far).
  3319. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3320. $search = 'AND view_count = '.$attempt_num;
  3321. }
  3322. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3323. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3324. $course_id = api_get_course_int_id();
  3325. $sql = "SELECT id, view_count FROM $lp_view_table
  3326. WHERE c_id = ".$course_id." AND lp_id = ".$this->get_id()." AND user_id = ".$this->get_user_id(
  3327. )." ".$search.
  3328. " ORDER BY view_count DESC";
  3329. $res = Database::query($sql);
  3330. if (Database :: num_rows($res) > 0) {
  3331. $row = Database :: fetch_array($res);
  3332. $this->lp_view_id = $row['id'];
  3333. } else {
  3334. // There is no database record, create one.
  3335. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  3336. ($course_id, ".$this->get_id().",".$this->get_user_id().",1)";
  3337. $res = Database::query($sql);
  3338. $id = Database :: insert_id();
  3339. $this->lp_view_id = $id;
  3340. }
  3341. return $this->lp_view_id;
  3342. }
  3343. /**
  3344. * Gets the current view id
  3345. * @return integer View ID (from lp_view)
  3346. */
  3347. public function get_view_id()
  3348. {
  3349. if ($this->debug > 0) {
  3350. error_log('New LP - In learnpath::get_view_id()', 0);
  3351. }
  3352. if (!empty ($this->lp_view_id)) {
  3353. return $this->lp_view_id;
  3354. } else {
  3355. return 0;
  3356. }
  3357. }
  3358. /**
  3359. * Gets the update queue
  3360. * @return array Array containing IDs of items to be updated by JavaScript
  3361. */
  3362. public function get_update_queue()
  3363. {
  3364. if ($this->debug > 0) {
  3365. error_log('New LP - In learnpath::get_update_queue()', 0);
  3366. }
  3367. return $this->update_queue;
  3368. }
  3369. /**
  3370. * Gets the user ID
  3371. * @return integer User ID
  3372. */
  3373. public function get_user_id()
  3374. {
  3375. if ($this->debug > 0) {
  3376. error_log('New LP - In learnpath::get_user_id()', 0);
  3377. }
  3378. if (!empty ($this->user_id)) {
  3379. return $this->user_id;
  3380. } else {
  3381. return false;
  3382. }
  3383. }
  3384. /**
  3385. * Checks if any of the items has an audio element attached
  3386. * @return bool True or false
  3387. */
  3388. public function has_audio()
  3389. {
  3390. if ($this->debug > 1) {
  3391. error_log('New LP - In learnpath::has_audio()', 0);
  3392. }
  3393. $has = false;
  3394. foreach ($this->items as $i => $item) {
  3395. if (!empty ($this->items[$i]->audio)) {
  3396. $has = true;
  3397. break;
  3398. }
  3399. }
  3400. return $has;
  3401. }
  3402. /**
  3403. * Logs a message into a file
  3404. * @param string Message to log
  3405. * @return boolean True on success, false on error or if msg empty
  3406. */
  3407. public function log($msg)
  3408. {
  3409. if ($this->debug > 0) {
  3410. error_log('New LP - In learnpath::log()', 0);
  3411. }
  3412. // TODO
  3413. $this->error .= $msg;
  3414. return true;
  3415. }
  3416. /**
  3417. * Moves an item up and down at its level
  3418. * @param integer Item to move up and down
  3419. * @param string Direction 'up' or 'down'
  3420. * @return integer New display order, or false on error
  3421. */
  3422. public function move_item($id, $direction)
  3423. {
  3424. $course_id = api_get_course_int_id();
  3425. if ($this->debug > 0) {
  3426. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3427. }
  3428. if (empty ($id) or empty ($direction)) {
  3429. return false;
  3430. }
  3431. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3432. $sql_sel = "SELECT *
  3433. FROM ".$tbl_lp_item."
  3434. WHERE c_id = ".$course_id." AND id = ".$id;
  3435. $res_sel = Database::query($sql_sel);
  3436. // Check if elem exists.
  3437. if (Database :: num_rows($res_sel) < 1) {
  3438. return false;
  3439. }
  3440. // Gather data.
  3441. $row = Database :: fetch_array($res_sel);
  3442. $previous = $row['previous_item_id'];
  3443. $next = $row['next_item_id'];
  3444. $display = $row['display_order'];
  3445. $parent = $row['parent_item_id'];
  3446. $lp = $row['lp_id'];
  3447. // Update the item (switch with previous/next one).
  3448. switch ($direction) {
  3449. case 'up' :
  3450. if ($this->debug > 2) {
  3451. error_log('Movement up detected', 0);
  3452. }
  3453. if ($display <= 1) { /*do nothing*/
  3454. } else {
  3455. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3456. WHERE c_id = ".$course_id." AND id = $previous";
  3457. if ($this->debug > 2) {
  3458. error_log('Selecting previous: '.$sql_sel2, 0);
  3459. }
  3460. $res_sel2 = Database::query($sql_sel2);
  3461. if (Database :: num_rows($res_sel2) < 1) {
  3462. $previous_previous = 0;
  3463. }
  3464. // Gather data.
  3465. $row2 = Database :: fetch_array($res_sel2);
  3466. $previous_previous = $row2['previous_item_id'];
  3467. // Update previous_previous item (switch "next" with current).
  3468. if ($previous_previous != 0) {
  3469. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3470. if ($this->debug > 2) {
  3471. error_log($sql_upd2, 0);
  3472. }
  3473. $res_upd2 = Database::query($sql_upd2);
  3474. }
  3475. // Update previous item (switch with current).
  3476. if ($previous != 0) {
  3477. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3478. WHERE c_id = ".$course_id." AND id = $previous";
  3479. if ($this->debug > 2) {
  3480. error_log($sql_upd2, 0);
  3481. }
  3482. $res_upd2 = Database::query($sql_upd2);
  3483. }
  3484. // Update current item (switch with previous).
  3485. if ($id != 0) {
  3486. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3487. WHERE c_id = ".$course_id." AND id = $id";
  3488. if ($this->debug > 2) {
  3489. error_log($sql_upd2, 0);
  3490. }
  3491. $res_upd2 = Database::query($sql_upd2);
  3492. }
  3493. // Update next item (new previous item).
  3494. if ($next != 0) {
  3495. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3496. WHERE c_id = ".$course_id." AND id = $next";
  3497. if ($this->debug > 2) {
  3498. error_log($sql_upd2, 0);
  3499. }
  3500. $res_upd2 = Database::query($sql_upd2);
  3501. }
  3502. $display = $display - 1;
  3503. }
  3504. break;
  3505. case 'down' :
  3506. if ($this->debug > 2) {
  3507. error_log('Movement down detected', 0);
  3508. }
  3509. if ($next == 0) { /* Do nothing. */
  3510. } else {
  3511. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3512. if ($this->debug > 2) {
  3513. error_log('Selecting next: '.$sql_sel2, 0);
  3514. }
  3515. $res_sel2 = Database::query($sql_sel2);
  3516. if (Database :: num_rows($res_sel2) < 1) {
  3517. $next_next = 0;
  3518. }
  3519. // Gather data.
  3520. $row2 = Database :: fetch_array($res_sel2);
  3521. $next_next = $row2['next_item_id'];
  3522. // Update previous item (switch with current).
  3523. if ($previous != 0) {
  3524. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3525. WHERE c_id = ".$course_id." AND id = $previous";
  3526. $res_upd2 = Database::query($sql_upd2);
  3527. }
  3528. // Update current item (switch with previous).
  3529. if ($id != 0) {
  3530. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3531. WHERE c_id = ".$course_id." AND id = $id";
  3532. $res_upd2 = Database::query($sql_upd2);
  3533. }
  3534. // Update next item (new previous item).
  3535. if ($next != 0) {
  3536. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3537. WHERE c_id = ".$course_id." AND id = $next";
  3538. $res_upd2 = Database::query($sql_upd2);
  3539. }
  3540. // Update next_next item (switch "previous" with current).
  3541. if ($next_next != 0) {
  3542. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3543. WHERE c_id = ".$course_id." AND id = $next_next";
  3544. $res_upd2 = Database::query($sql_upd2);
  3545. }
  3546. $display = $display + 1;
  3547. }
  3548. break;
  3549. default :
  3550. return false;
  3551. }
  3552. return $display;
  3553. }
  3554. /**
  3555. * Move a learnpath up (display_order)
  3556. * @param integer Learnpath ID
  3557. */
  3558. public function move_up($lp_id)
  3559. {
  3560. $course_id = api_get_course_int_id();
  3561. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3562. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3563. $res = Database::query($sql);
  3564. if ($res === false) {
  3565. return false;
  3566. }
  3567. $lps = array();
  3568. $lp_order = array();
  3569. $num = Database :: num_rows($res);
  3570. // First check the order is correct, globally (might be wrong because
  3571. // of versions < 1.8.4)
  3572. if ($num > 0) {
  3573. $i = 1;
  3574. while ($row = Database :: fetch_array($res)) {
  3575. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3576. $need_fix = true;
  3577. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3578. $res_u = Database::query($sql_u);
  3579. }
  3580. $row['display_order'] = $i;
  3581. $lps[$row['id']] = $row;
  3582. $lp_order[$i] = $row['id'];
  3583. $i++;
  3584. }
  3585. }
  3586. if ($num > 1) { // If there's only one element, no need to sort.
  3587. $order = $lps[$lp_id]['display_order'];
  3588. if ($order > 1) { // If it's the first element, no need to move up.
  3589. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = ".$lp_order[$order - 1];
  3590. $res_u1 = Database::query($sql_u1);
  3591. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order - 1)." WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3592. $res_u2 = Database::query($sql_u2);
  3593. }
  3594. }
  3595. }
  3596. /**
  3597. * Move a learnpath down (display_order)
  3598. * @param integer Learnpath ID
  3599. */
  3600. public function move_down($lp_id)
  3601. {
  3602. $course_id = api_get_course_int_id();
  3603. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3604. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3605. $res = Database::query($sql);
  3606. if ($res === false) {
  3607. return false;
  3608. }
  3609. $lps = array();
  3610. $lp_order = array();
  3611. $num = Database :: num_rows($res);
  3612. $max = 0;
  3613. // First check the order is correct, globally (might be wrong because
  3614. // of versions < 1.8.4).
  3615. if ($num > 0) {
  3616. $i = 1;
  3617. while ($row = Database :: fetch_array($res)) {
  3618. $max = $i;
  3619. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3620. $need_fix = true;
  3621. $sql_u = "UPDATE $lp_table SET display_order = $i
  3622. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3623. $res_u = Database::query($sql_u);
  3624. }
  3625. $row['display_order'] = $i;
  3626. $lps[$row['id']] = $row;
  3627. $lp_order[$i] = $row['id'];
  3628. $i++;
  3629. }
  3630. }
  3631. if ($num > 1) { // If there's only one element, no need to sort.
  3632. $order = $lps[$lp_id]['display_order'];
  3633. if ($order < $max) { // If it's the first element, no need to move up.
  3634. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3635. WHERE c_id = ".$course_id." AND id = ".$lp_order[$order + 1];
  3636. $res_u1 = Database::query($sql_u1);
  3637. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3638. WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3639. $res_u2 = Database::query($sql_u2);
  3640. }
  3641. }
  3642. }
  3643. /**
  3644. * Updates learnpath attributes to point to the next element
  3645. * The last part is similar to set_current_item but processing the other way around
  3646. */
  3647. public function next()
  3648. {
  3649. if ($this->debug > 0) {
  3650. error_log('New LP - In learnpath::next()', 0);
  3651. }
  3652. $this->last = $this->get_current_item_id();
  3653. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3654. $this->autocomplete_parents($this->last);
  3655. $new_index = $this->get_next_index();
  3656. if ($this->debug > 2) {
  3657. error_log('New LP - New index: '.$new_index, 0);
  3658. }
  3659. $this->index = $new_index;
  3660. if ($this->debug > 2) {
  3661. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3662. }
  3663. $this->current = $this->ordered_items[$new_index];
  3664. if ($this->debug > 2) {
  3665. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3666. }
  3667. }
  3668. /**
  3669. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3670. * class, this might be redefined to allow several behaviours depending on the document type.
  3671. * @param integer Resource ID
  3672. * @return boolean True on success, false otherwise
  3673. */
  3674. public function open($id)
  3675. {
  3676. if ($this->debug > 0) {
  3677. error_log('New LP - In learnpath::open()', 0);
  3678. }
  3679. // TODO:
  3680. // set the current resource attribute to this resource
  3681. // switch on element type (redefine in child class?)
  3682. // set status for this item to "opened"
  3683. // start timer
  3684. // initialise score
  3685. $this->index = 0; //or = the last item seen (see $this->last)
  3686. }
  3687. /**
  3688. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3689. * and the prerequisite string on error.
  3690. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3691. * @param integer Optional item ID. If none given, uses the current open item.
  3692. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3693. */
  3694. public function prerequisites_match($item = null)
  3695. {
  3696. if ($this->debug > 0) {
  3697. error_log('In learnpath::prerequisites_match()', 0);
  3698. }
  3699. if (empty($item)) {
  3700. $item = $this->current;
  3701. }
  3702. if (isset($this->items[$item]) && is_object($this->items[$item])) {
  3703. if ($this->type == 2) {
  3704. //Getting prereq from scorm
  3705. $prereq_string = $this->get_scorm_prereq_string($item);
  3706. } else {
  3707. $prereq_string = $this->items[$item]->get_prereq_string();
  3708. }
  3709. if (empty($prereq_string)) {
  3710. return true;
  3711. }
  3712. // Clean spaces.
  3713. $prereq_string = str_replace(' ', '', $prereq_string);
  3714. if ($this->debug > 0) {
  3715. error_log('Found prereq_string: '.$prereq_string, 0);
  3716. }
  3717. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3718. $result = $this->items[$item]->parse_prereq(
  3719. $prereq_string,
  3720. $this->items,
  3721. $this->refs_list,
  3722. $this->get_user_id()
  3723. );
  3724. if ($result === false) {
  3725. $this->set_error_msg($this->items[$item]->prereq_alert);
  3726. }
  3727. } else {
  3728. $result = true;
  3729. if ($this->debug > 1) {
  3730. error_log('$this->items['.$item.'] was not an object', 0);
  3731. }
  3732. }
  3733. if ($this->debug > 1) {
  3734. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3735. }
  3736. return $result;
  3737. }
  3738. /**
  3739. * Updates learnpath attributes to point to the previous element
  3740. * The last part is similar to set_current_item but processing the other way around
  3741. */
  3742. public function previous()
  3743. {
  3744. if ($this->debug > 0) {
  3745. error_log('New LP - In learnpath::previous()', 0);
  3746. }
  3747. $this->last = $this->get_current_item_id();
  3748. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3749. $this->autocomplete_parents($this->last);
  3750. $new_index = $this->get_previous_index();
  3751. $this->index = $new_index;
  3752. $this->current = $this->ordered_items[$new_index];
  3753. }
  3754. /**
  3755. * Publishes a learnpath. This basically means show or hide the learnpath
  3756. * to normal users.
  3757. * Can be used as abstract
  3758. * @param integer Learnpath ID
  3759. * @param string New visibility
  3760. */
  3761. public function toggle_visibility($lp_id, $set_visibility = 1)
  3762. {
  3763. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3764. $action = 'visible';
  3765. if ($set_visibility != 1) {
  3766. $action = 'invisible';
  3767. }
  3768. return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3769. }
  3770. /**
  3771. * Publishes a learnpath. This basically means show or hide the learnpath
  3772. * on the course homepage
  3773. * Can be used as abstract
  3774. * @param integer Learnpath id
  3775. * @param string New visibility (v/s - visible/invisible)
  3776. */
  3777. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3778. {
  3779. $course_id = api_get_course_int_id();
  3780. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3781. $lp_id = Database::escape_string($lp_id);
  3782. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3783. $result = Database::query($sql);
  3784. if (Database::num_rows($result)) {
  3785. $row = Database :: fetch_array($result);
  3786. $name = Text::domesticate($row['name']);
  3787. if ($set_visibility == 'i') {
  3788. $s = $name." ".get_lang('LearnpathNotPublished');
  3789. $dialogBox = $s;
  3790. $v = 0;
  3791. }
  3792. if ($set_visibility == 'v') {
  3793. $s = $name." ".get_lang('LearnpathPublished');
  3794. $dialogBox = $s;
  3795. $v = 1;
  3796. }
  3797. } else {
  3798. return false;
  3799. }
  3800. $session_id = api_get_session_id();
  3801. $session_condition = api_get_session_condition($session_id);
  3802. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3803. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3804. $linkNoSessionId = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id;
  3805. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3806. $result = Database::query($sql);
  3807. $num = Database :: num_rows($result);
  3808. if (($set_visibility == 'i') && ($num > 0)) {
  3809. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3810. Database::query($sql);
  3811. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$linkNoSessionId' and image='scormbuilder.gif' $session_condition)";
  3812. Database::query($sql);
  3813. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3814. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3815. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3816. Database::query($sql);
  3817. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3818. $sql = "UPDATE $tbl_tool SET c_id = $course_id, name = '$name', link = '$link', image = 'scormbuilder.gif', visibility = '$v', admin = '0', address = 'pastillegris.gif', added_tool = 0, session_id = $session_id
  3819. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3820. Database::query($sql);
  3821. } else {
  3822. // Parameter and database incompatible, do nothing, exit.
  3823. return false;
  3824. }
  3825. }
  3826. /**
  3827. * Restart the whole learnpath. Return the URL of the first element.
  3828. * Make sure the results are saved with anoter method. This method should probably be
  3829. * redefined in children classes.
  3830. * To use a similar method statically, use the create_new_attempt() method
  3831. * @return string URL to load in the viewer
  3832. */
  3833. public function restart()
  3834. {
  3835. if ($this->debug > 0) {
  3836. error_log('New LP - In learnpath::restart()', 0);
  3837. }
  3838. // TODO
  3839. // Call autosave method to save the current progress.
  3840. //$this->index = 0;
  3841. $session_id = api_get_session_id();
  3842. $course_id = api_get_course_int_id();
  3843. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3844. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) ".
  3845. "VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  3846. if ($this->debug > 2) {
  3847. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  3848. }
  3849. $res = Database::query($sql);
  3850. if ($view_id = Database :: insert_id($res)) {
  3851. $this->lp_view_id = $view_id;
  3852. $this->attempt = $this->attempt + 1;
  3853. } else {
  3854. $this->error = 'Could not insert into item_view table...';
  3855. return false;
  3856. }
  3857. $this->autocomplete_parents($this->current);
  3858. foreach ($this->items as $index => $dummy) {
  3859. $this->items[$index]->restart();
  3860. $this->items[$index]->set_lp_view($this->lp_view_id);
  3861. }
  3862. $this->first();
  3863. return true;
  3864. }
  3865. /**
  3866. * Saves the current item
  3867. * @return boolean
  3868. */
  3869. public function save_current()
  3870. {
  3871. if ($this->debug > 0) {
  3872. error_log('learnpath::save_current()', 0);
  3873. }
  3874. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3875. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3876. if ($this->debug > 2) {
  3877. error_log('New LP - save_current() saving item '.$this->current, 0);
  3878. }
  3879. if ($this->debug > 2) {
  3880. error_log(''.print_r($this->items, true), 0);
  3881. }
  3882. if (is_object($this->items[$this->current])) {
  3883. //$res = $this->items[$this->current]->save(false);
  3884. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3885. $this->autocomplete_parents($this->current);
  3886. $status = $this->items[$this->current]->get_status();
  3887. $this->append_message('new_item_status: '.$status);
  3888. $this->update_queue[$this->current] = $status;
  3889. return $res;
  3890. }
  3891. return false;
  3892. }
  3893. /**
  3894. * Saves the given item
  3895. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3896. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3897. * @return boolean
  3898. */
  3899. public function save_item($item_id = null, $from_outside = true)
  3900. {
  3901. $debug = $this->debug;
  3902. if ($debug) {
  3903. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  3904. }
  3905. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3906. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3907. if (empty($item_id)) {
  3908. $item_id = intval($_REQUEST['id']);
  3909. }
  3910. if (empty($item_id)) {
  3911. $item_id = $this->get_current_item_id();
  3912. }
  3913. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3914. if ($debug) {
  3915. error_log('Object exists');
  3916. }
  3917. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3918. if ($debug) {
  3919. error_log('update_queue before:');
  3920. error_log(print_r($this->update_queue, 1));
  3921. }
  3922. $this->autocomplete_parents($item_id);
  3923. $status = $this->items[$item_id]->get_status();
  3924. $this->update_queue[$item_id] = $status;
  3925. if ($debug) {
  3926. error_log('get_status(): '.$status);
  3927. error_log('update_queue after:');
  3928. error_log(print_r($this->update_queue, 1));
  3929. }
  3930. return $res;
  3931. }
  3932. return false;
  3933. }
  3934. /**
  3935. * Saves the last item seen's ID only in case
  3936. */
  3937. public function save_last()
  3938. {
  3939. $course_id = api_get_course_int_id();
  3940. if ($this->debug > 0) {
  3941. error_log('New LP - In learnpath::save_last()', 0);
  3942. }
  3943. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3944. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3945. if (isset($this->current)) {
  3946. if ($this->debug > 2) {
  3947. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  3948. }
  3949. $sql = "UPDATE $table SET last_item = ".Database::escape_string($this->get_current_item_id())."
  3950. WHERE c_id = $course_id AND
  3951. lp_id = ".$this->get_id()." AND
  3952. user_id = ".$this->get_user_id()." ".$session_condition;
  3953. if ($this->debug > 2) {
  3954. error_log('New LP - Saving last item seen : '.$sql, 0);
  3955. }
  3956. $res = Database::query($sql);
  3957. }
  3958. // Save progress.
  3959. list($progress, $text) = $this->get_progress_bar_text('%');
  3960. if ($progress >= 0 && $progress <= 100) {
  3961. $progress = (int)$progress;
  3962. $sql = "UPDATE $table SET progress = $progress
  3963. WHERE c_id = ".$course_id." AND
  3964. lp_id = ".$this->get_id()." AND
  3965. user_id = ".$this->get_user_id()." ".$session_condition;
  3966. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3967. $this->progress_db = $progress;
  3968. }
  3969. }
  3970. /**
  3971. * Sets the current item ID (checks if valid and authorized first)
  3972. * @param integer New item ID. If not given or not authorized, defaults to current
  3973. */
  3974. public function set_current_item($item_id = null)
  3975. {
  3976. if ($this->debug > 0) {
  3977. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  3978. }
  3979. if (empty ($item_id)) {
  3980. if ($this->debug > 2) {
  3981. error_log('New LP - No new current item given, ignore...', 0);
  3982. }
  3983. // Do nothing.
  3984. } else {
  3985. if ($this->debug > 2) {
  3986. error_log('New LP - New current item given is '.$item_id.'...', 0);
  3987. }
  3988. if (is_numeric($item_id)) {
  3989. $item_id = Database::escape_string($item_id);
  3990. // TODO: Check in database here.
  3991. $this->last = $this->current;
  3992. $this->current = $item_id;
  3993. // TODO: Update $this->index as well.
  3994. foreach ($this->ordered_items as $index => $item) {
  3995. if ($item == $this->current) {
  3996. $this->index = $index;
  3997. break;
  3998. }
  3999. }
  4000. if ($this->debug > 2) {
  4001. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  4002. }
  4003. } else {
  4004. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  4005. }
  4006. }
  4007. }
  4008. /**
  4009. * Sets the encoding
  4010. * @param string New encoding
  4011. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4012. */
  4013. public function set_encoding($enc = 'UTF-8')
  4014. {
  4015. if ($this->debug > 0) {
  4016. error_log('New LP - In learnpath::set_encoding()', 0);
  4017. }
  4018. $course_id = api_get_course_int_id();
  4019. /* // Deprecated code (Chamilo 1.8.8).
  4020. $enc = strtoupper($enc);
  4021. $encodings = array (
  4022. 'UTF-8',
  4023. 'ISO-8859-1',
  4024. 'ISO-8859-15',
  4025. 'cp1251',
  4026. 'cp1252',
  4027. 'KOI8-R',
  4028. 'BIG5',
  4029. 'GB2312',
  4030. 'Shift_JIS',
  4031. 'EUC-JP',
  4032. ''
  4033. );
  4034. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  4035. $lp = $this->get_id();
  4036. if ($lp != 0) {
  4037. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4038. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  4039. $res = Database::query($sql);
  4040. return $res;
  4041. }
  4042. }
  4043. return false;
  4044. */
  4045. $enc = api_refine_encoding_id($enc);
  4046. if (empty($enc)) {
  4047. $enc = api_get_system_encoding();
  4048. }
  4049. if (api_is_encoding_supported($enc)) {
  4050. $lp = $this->get_id();
  4051. if ($lp != 0) {
  4052. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4053. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4054. $res = Database::query($sql);
  4055. return $res;
  4056. }
  4057. }
  4058. return false;
  4059. }
  4060. /**
  4061. * Sets the JS lib setting in the database directly.
  4062. * This is the JavaScript library file this lp needs to load on startup
  4063. * @param string Proximity setting
  4064. * @return boolean True on update success. False otherwise.
  4065. */
  4066. public function set_jslib($lib = '')
  4067. {
  4068. if ($this->debug > 0) {
  4069. error_log('New LP - In learnpath::set_jslib()', 0);
  4070. }
  4071. $lp = $this->get_id();
  4072. $course_id = api_get_course_int_id();
  4073. if ($lp != 0) {
  4074. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4075. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = ".$lp;
  4076. $res = Database::query($sql);
  4077. return $res;
  4078. } else {
  4079. return false;
  4080. }
  4081. }
  4082. /**
  4083. * Sets the name of the LP maker (publisher) (and save)
  4084. * @param string Optional string giving the new content_maker of this learnpath
  4085. * @return boolean True
  4086. */
  4087. public function set_maker($name = '')
  4088. {
  4089. if ($this->debug > 0) {
  4090. error_log('New LP - In learnpath::set_maker()', 0);
  4091. }
  4092. if (empty ($name)) {
  4093. return false;
  4094. }
  4095. $this->maker = Database::escape_string($name);
  4096. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4097. $course_id = api_get_course_int_id();
  4098. $lp_id = $this->get_id();
  4099. $sql = "UPDATE $lp_table SET content_maker = '".$this->maker."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4100. if ($this->debug > 2) {
  4101. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4102. }
  4103. Database::query($sql);
  4104. return true;
  4105. }
  4106. /**
  4107. * Sets the name of the current learnpath (and save)
  4108. * @param string Optional string giving the new name of this learnpath
  4109. * @return boolean True/False
  4110. */
  4111. public function set_name($name = null)
  4112. {
  4113. if ($this->debug > 0) {
  4114. error_log('New LP - In learnpath::set_name()', 0);
  4115. }
  4116. if (empty($name)) {
  4117. return false;
  4118. }
  4119. $this->name = Database::escape_string($name);
  4120. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4121. $lp_id = $this->get_id();
  4122. $course_id = api_get_course_int_id();
  4123. $sql = "UPDATE $lp_table SET name = '".$this->name."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4124. if ($this->debug > 2) {
  4125. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4126. }
  4127. Database::query($sql);
  4128. // If the lp is visible on the homepage, change his name there.
  4129. if (Database::affected_rows()) {
  4130. $session_id = api_get_session_id();
  4131. $session_condition = api_get_session_condition($session_id);
  4132. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4133. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4134. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4135. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4136. Database::query($sql);
  4137. return true;
  4138. } else {
  4139. return false;
  4140. }
  4141. }
  4142. /**
  4143. * Set index specified prefix terms for all items in this path
  4144. * @param string Comma-separated list of terms
  4145. * @param char Xapian term prefix
  4146. * @return boolean False on error, true otherwise
  4147. */
  4148. public function set_terms_by_prefix($terms_string, $prefix)
  4149. {
  4150. $course_id = api_get_course_int_id();
  4151. if (api_get_setting('search_enabled') !== 'true') {
  4152. return false;
  4153. }
  4154. if (!extension_loaded('xapian')) {
  4155. return false;
  4156. }
  4157. $terms_string = trim($terms_string);
  4158. $terms = explode(',', $terms_string);
  4159. array_walk($terms, 'trim_value');
  4160. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4161. // Don't do anything if no change, verify only at DB, not the search engine.
  4162. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4163. return false;
  4164. }
  4165. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4166. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4167. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4168. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4169. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4170. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4171. $lp_id = intval($_POST['lp_id']);
  4172. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4173. $result = Database::query($sql);
  4174. $di = new ChamiloIndexer();
  4175. while ($lp_item = Database :: fetch_array($result)) {
  4176. // Get search_did.
  4177. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4178. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
  4179. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4180. //echo $sql; echo '<br>';
  4181. $res = Database::query($sql);
  4182. if (Database::num_rows($res) > 0) {
  4183. $se_ref = Database :: fetch_array($res);
  4184. // Compare terms.
  4185. $doc = $di->get_document($se_ref['search_did']);
  4186. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4187. $xterms = array();
  4188. foreach ($xapian_terms as $xapian_term) {
  4189. $xterms[] = substr($xapian_term['name'], 1);
  4190. }
  4191. $dterms = $terms;
  4192. $missing_terms = array_diff($dterms, $xterms);
  4193. $deprecated_terms = array_diff($xterms, $dterms);
  4194. // Save it to search engine.
  4195. foreach ($missing_terms as $term) {
  4196. $doc->add_term($prefix.$term, 1);
  4197. }
  4198. foreach ($deprecated_terms as $term) {
  4199. $doc->remove_term($prefix.$term);
  4200. }
  4201. $di->getDb()->replace_document((int)$se_ref['search_did'], $doc);
  4202. $di->getDb()->flush();
  4203. } else {
  4204. //@todo What we should do here?
  4205. }
  4206. }
  4207. return true;
  4208. }
  4209. /**
  4210. * Sets the theme of the LP (local/remote) (and save)
  4211. * @param string Optional string giving the new theme of this learnpath
  4212. * @return bool Returns true if theme name is not empty
  4213. */
  4214. public function set_theme($name = '')
  4215. {
  4216. $course_id = api_get_course_int_id();
  4217. if ($this->debug > 0) {
  4218. error_log('New LP - In learnpath::set_theme()', 0);
  4219. }
  4220. $this->theme = Database::escape_string($name);
  4221. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4222. $lp_id = $this->get_id();
  4223. $sql = "UPDATE $lp_table SET theme = '".$this->theme."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4224. if ($this->debug > 2) {
  4225. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4226. }
  4227. Database::query($sql);
  4228. return true;
  4229. }
  4230. /**
  4231. * Sets the image of an LP (and save)
  4232. * @param string Optional string giving the new image of this learnpath
  4233. * @return bool Returns true if theme name is not empty
  4234. */
  4235. public function set_preview_image($name = '')
  4236. {
  4237. $course_id = api_get_course_int_id();
  4238. if ($this->debug > 0) {
  4239. error_log('New LP - In learnpath::set_preview_image()', 0);
  4240. }
  4241. $this->preview_image = Database::escape_string($name);
  4242. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4243. $lp_id = $this->get_id();
  4244. $sql = "UPDATE $lp_table SET preview_image = '".$this->preview_image."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4245. if ($this->debug > 2) {
  4246. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4247. }
  4248. Database::query($sql);
  4249. return true;
  4250. }
  4251. /**
  4252. * Sets the author of a LP (and save)
  4253. * @param string Optional string giving the new author of this learnpath
  4254. * @return bool Returns true if author's name is not empty
  4255. */
  4256. public function set_author($name = '')
  4257. {
  4258. $course_id = api_get_course_int_id();
  4259. if ($this->debug > 0) {
  4260. error_log('New LP - In learnpath::set_author()', 0);
  4261. }
  4262. $this->author = Database::escape_string($name);
  4263. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4264. $lp_id = $this->get_id();
  4265. $sql = "UPDATE $lp_table SET author = '".$this->author."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4266. if ($this->debug > 2) {
  4267. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4268. }
  4269. Database::query($sql);
  4270. return true;
  4271. }
  4272. /**
  4273. * Sets the hide_toc_frame parameter of a LP (and save)
  4274. * @param int 1 if frame is hiddent 0 thenelse
  4275. * @return bool Returns true if author's name is not empty
  4276. */
  4277. public function set_hide_toc_frame($hide)
  4278. {
  4279. $course_id = api_get_course_int_id();
  4280. if ($this->debug > 0) {
  4281. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4282. }
  4283. if (intval($hide) == $hide) {
  4284. $this->hide_toc_frame = $hide;
  4285. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4286. $lp_id = $this->get_id();
  4287. $sql = "UPDATE $lp_table SET hide_toc_frame = '".$this->hide_toc_frame."'
  4288. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4289. if ($this->debug > 2) {
  4290. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4291. }
  4292. Database::query($sql);
  4293. return true;
  4294. } else {
  4295. return false;
  4296. }
  4297. }
  4298. /**
  4299. * Sets the prerequisite of a LP (and save)
  4300. * @param int integer giving the new prerequisite of this learnpath
  4301. * @return bool returns true if prerequisite is not empty
  4302. */
  4303. public function set_prerequisite($prerequisite)
  4304. {
  4305. $course_id = api_get_course_int_id();
  4306. if ($this->debug > 0) {
  4307. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4308. }
  4309. $this->prerequisite = intval($prerequisite);
  4310. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4311. $lp_id = $this->get_id();
  4312. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4313. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4314. if ($this->debug > 2) {
  4315. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4316. }
  4317. Database::query($sql);
  4318. return true;
  4319. }
  4320. /**
  4321. * Sets the location/proximity of the LP (local/remote) (and save)
  4322. * @param string Optional string giving the new location of this learnpath
  4323. * @return boolean True on success / False on error
  4324. */
  4325. public function set_proximity($name = '')
  4326. {
  4327. $course_id = api_get_course_int_id();
  4328. if ($this->debug > 0) {
  4329. error_log('New LP - In learnpath::set_proximity()', 0);
  4330. }
  4331. if (empty ($name)) {
  4332. return false;
  4333. }
  4334. $this->proximity = Database::escape_string($name);
  4335. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4336. $lp_id = $this->get_id();
  4337. $sql = "UPDATE $lp_table SET content_local = '".$this->proximity."'
  4338. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4339. if ($this->debug > 2) {
  4340. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4341. }
  4342. Database::query($sql);
  4343. return true;
  4344. }
  4345. /**
  4346. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4347. * @param integer DB ID of the item
  4348. */
  4349. public function set_previous_item($id)
  4350. {
  4351. if ($this->debug > 0) {
  4352. error_log('New LP - In learnpath::set_previous_item()', 0);
  4353. }
  4354. $this->last = $id;
  4355. }
  4356. public function get_max_attempts()
  4357. {
  4358. return $this->max_attempts;
  4359. }
  4360. public function get_subscribe_users()
  4361. {
  4362. return $this->subscribe_users;
  4363. }
  4364. public function check_attempts()
  4365. {
  4366. $max_attempts = $this->get_max_attempts();
  4367. switch ($max_attempts) {
  4368. case 0: //unlimited
  4369. return true;
  4370. break;
  4371. case $max_attempts >= 1:
  4372. if ($this->attempt <= $max_attempts) {
  4373. return true;
  4374. }
  4375. break;
  4376. }
  4377. return false;
  4378. }
  4379. function check_item_attempts($item_id)
  4380. {
  4381. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4382. $count = $this->items[$item_id]->get_view_count();
  4383. $max_attempts = $this->get_max_attempts();
  4384. switch ($max_attempts) {
  4385. case 0: //unlimited
  4386. return true;
  4387. break;
  4388. case $max_attempts >= 1:
  4389. if ($count <= $max_attempts) {
  4390. return true;
  4391. }
  4392. break;
  4393. }
  4394. return false;
  4395. }
  4396. }
  4397. public function set_max_attempts($attempts)
  4398. {
  4399. $mode = 'multiple';
  4400. if ($attempts == 1) {
  4401. $mode = 'single';
  4402. }
  4403. $this->max_attempts = intval($attempts);
  4404. $lp_id = $this->get_id();
  4405. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4406. $sql = "UPDATE $lp_table SET max_attempts = '".$this->max_attempts."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4407. Database::query($sql);
  4408. $this->set_attempt_mode($mode);
  4409. }
  4410. /**
  4411. * Sets subscribe_users
  4412. * @param string $value Optional string giving the new location of this learnpath
  4413. * @return bool True on success / False on error
  4414. */
  4415. public function set_subscribe_users($value = 0)
  4416. {
  4417. if ($this->debug > 0) {
  4418. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  4419. }
  4420. $this->subscribe_users = intval($value);;
  4421. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4422. $lp_id = $this->get_id();
  4423. $sql = "UPDATE $lp_table SET subscribe_users = '".$this->subscribe_users."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4424. if ($this->debug > 2) {
  4425. error_log('New LP - lp updated with new set_subscribe_users : '.$this->subscribe_users, 0);
  4426. }
  4427. Database::query($sql);
  4428. return true;
  4429. }
  4430. /**
  4431. * Sets use_max_score
  4432. * @param string Optional string giving the new location of this learnpath
  4433. * @return boolean True on success / False on error
  4434. */
  4435. public function set_use_max_score($use_max_score = 1)
  4436. {
  4437. if ($this->debug > 0) {
  4438. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4439. }
  4440. $this->use_max_score = intval($use_max_score);
  4441. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4442. $lp_id = $this->get_id();
  4443. $sql = "UPDATE $lp_table SET use_max_score = '".$this->use_max_score."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4444. if ($this->debug > 2) {
  4445. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4446. }
  4447. Database::query($sql);
  4448. return true;
  4449. }
  4450. /**
  4451. * Sets and saves the expired_on date
  4452. * @param string Optional string giving the new author of this learnpath
  4453. * @return bool Returns true if author's name is not empty
  4454. */
  4455. public function set_expired_on($expired_on)
  4456. {
  4457. $course_id = api_get_course_int_id();
  4458. if ($this->debug > 0) {
  4459. error_log('New LP - In learnpath::set_expired_on()', 0);
  4460. }
  4461. if (!empty($expired_on)) {
  4462. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  4463. } else {
  4464. $this->expired_on = '';
  4465. }
  4466. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4467. $lp_id = $this->get_id();
  4468. $sql = "UPDATE $lp_table SET expired_on = '".$this->expired_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4469. if ($this->debug > 2) {
  4470. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4471. }
  4472. Database::query($sql);
  4473. return true;
  4474. }
  4475. /**
  4476. * Sets and saves the publicated_on date
  4477. * @param string Optional string giving the new author of this learnpath
  4478. * @return bool Returns true if author's name is not empty
  4479. */
  4480. public function set_publicated_on($publicated_on)
  4481. {
  4482. $course_id = api_get_course_int_id();
  4483. if ($this->debug > 0) {
  4484. error_log('New LP - In learnpath::set_expired_on()', 0);
  4485. }
  4486. if (!empty($publicated_on)) {
  4487. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4488. } else {
  4489. $this->publicated_on = '';
  4490. }
  4491. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4492. $lp_id = $this->get_id();
  4493. $sql = "UPDATE $lp_table SET publicated_on = '".$this->publicated_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4494. if ($this->debug > 2) {
  4495. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4496. }
  4497. Database::query($sql);
  4498. return true;
  4499. }
  4500. public function set_category_id($category_id)
  4501. {
  4502. $this->category_id = $category_id;
  4503. $course_id = api_get_course_int_id();
  4504. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4505. $lp_id = $this->get_id();
  4506. $sql = "UPDATE $lp_table SET category_id = '".intval(
  4507. $category_id
  4508. )."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4509. Database::query($sql);
  4510. return true;
  4511. }
  4512. /**
  4513. * Sets and saves the expired_on date
  4514. * @param string Optional string giving the new author of this learnpath
  4515. * @return bool Returns true if author's name is not empty
  4516. */
  4517. public function set_modified_on()
  4518. {
  4519. $course_id = api_get_course_int_id();
  4520. if ($this->debug > 0) {
  4521. error_log('New LP - In learnpath::set_expired_on()', 0);
  4522. }
  4523. $this->modified_on = api_get_utc_datetime();
  4524. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4525. $lp_id = $this->get_id();
  4526. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4527. if ($this->debug > 2) {
  4528. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4529. }
  4530. Database::query($sql);
  4531. return true;
  4532. }
  4533. /**
  4534. * Sets the object's error message
  4535. * @param string Error message. If empty, reinits the error string
  4536. * @return void
  4537. */
  4538. public function set_error_msg($error = '')
  4539. {
  4540. if ($this->debug > 0) {
  4541. error_log('New LP - In learnpath::set_error_msg()', 0);
  4542. }
  4543. if (empty ($error)) {
  4544. $this->error = '';
  4545. } else {
  4546. $this->error .= $error;
  4547. }
  4548. }
  4549. /**
  4550. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4551. * @param boolean Whether to allow a new attempt or not
  4552. * @return boolean True
  4553. */
  4554. public function start_current_item($allow_new_attempt = false)
  4555. {
  4556. if ($this->debug > 0) {
  4557. error_log('New LP - In learnpath::start_current_item()', 0);
  4558. }
  4559. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4560. $type = $this->get_type();
  4561. $item_type = $this->items[$this->current]->get_type();
  4562. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4563. $this->items[$this->current]->open($allow_new_attempt);
  4564. $this->autocomplete_parents($this->current);
  4565. $prereq_check = $this->prerequisites_match($this->current);
  4566. $this->items[$this->current]->save(false, $prereq_check);
  4567. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4568. } else {
  4569. // If sco, then it is supposed to have been updated by some other call.
  4570. }
  4571. if ($item_type == 'sco') {
  4572. $this->items[$this->current]->restart();
  4573. }
  4574. }
  4575. if ($this->debug > 0) {
  4576. error_log('New LP - End of learnpath::start_current_item()', 0);
  4577. }
  4578. return true;
  4579. }
  4580. /**
  4581. * Stops the processing and counters for the old item (as held in $this->last)
  4582. * @return boolean True/False
  4583. */
  4584. public function stop_previous_item()
  4585. {
  4586. if ($this->debug > 0) {
  4587. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4588. }
  4589. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4590. if ($this->debug > 2) {
  4591. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4592. }
  4593. switch ($this->get_type()) {
  4594. case '3' :
  4595. if ($this->items[$this->last]->get_type() != 'au') {
  4596. if ($this->debug > 2) {
  4597. error_log(
  4598. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au',
  4599. 0
  4600. );
  4601. }
  4602. $this->items[$this->last]->close();
  4603. //$this->autocomplete_parents($this->last);
  4604. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4605. } else {
  4606. if ($this->debug > 2) {
  4607. error_log(
  4608. 'New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals',
  4609. 0
  4610. );
  4611. }
  4612. }
  4613. case '2' :
  4614. if ($this->items[$this->last]->get_type() != 'sco') {
  4615. if ($this->debug > 2) {
  4616. error_log(
  4617. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco',
  4618. 0
  4619. );
  4620. }
  4621. $this->items[$this->last]->close();
  4622. //$this->autocomplete_parents($this->last);
  4623. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4624. } else {
  4625. if ($this->debug > 2) {
  4626. error_log(
  4627. 'New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals',
  4628. 0
  4629. );
  4630. }
  4631. }
  4632. break;
  4633. case '1' :
  4634. default :
  4635. if ($this->debug > 2) {
  4636. error_log(
  4637. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset',
  4638. 0
  4639. );
  4640. }
  4641. $this->items[$this->last]->close();
  4642. break;
  4643. }
  4644. } else {
  4645. if ($this->debug > 2) {
  4646. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4647. }
  4648. return false;
  4649. }
  4650. return true;
  4651. }
  4652. /**
  4653. * Updates the default view mode from fullscreen to embedded and inversely
  4654. * @return string The current default view mode ('fullscreen' or 'embedded')
  4655. */
  4656. public function update_default_view_mode()
  4657. {
  4658. $course_id = api_get_course_int_id();
  4659. if ($this->debug > 0) {
  4660. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4661. }
  4662. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4663. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4664. $res = Database::query($sql);
  4665. if (Database :: num_rows($res) > 0) {
  4666. $row = Database :: fetch_array($res);
  4667. $default_view_mode = $row['default_view_mod'];
  4668. $view_mode = $default_view_mode;
  4669. switch ($default_view_mode) {
  4670. case 'fullscreen':
  4671. $view_mode = 'embedded';
  4672. break;
  4673. case 'embedded':
  4674. $view_mode = 'embedframe';
  4675. break;
  4676. case 'embedframe':
  4677. $view_mode = 'impress';
  4678. break;
  4679. case 'impress':
  4680. $view_mode = 'fullscreen';
  4681. break;
  4682. }
  4683. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4684. );
  4685. $res = Database::query($sql);
  4686. $this->mode = $view_mode;
  4687. return $view_mode;
  4688. } else {
  4689. if ($this->debug > 2) {
  4690. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4691. }
  4692. }
  4693. return -1;
  4694. }
  4695. /**
  4696. * Updates the default behaviour about auto-commiting SCORM updates
  4697. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4698. */
  4699. public function update_default_scorm_commit()
  4700. {
  4701. $course_id = api_get_course_int_id();
  4702. if ($this->debug > 0) {
  4703. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4704. }
  4705. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4706. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4707. $res = Database::query($sql);
  4708. if (Database :: num_rows($res) > 0) {
  4709. $row = Database :: fetch_array($res);
  4710. $force = $row['force_commit'];
  4711. if ($force == 1) {
  4712. $force = 0;
  4713. $force_return = false;
  4714. } elseif ($force == 0) {
  4715. $force = 1;
  4716. $force_return = true;
  4717. }
  4718. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4719. $res = Database::query($sql);
  4720. $this->force_commit = $force_return;
  4721. return $force_return;
  4722. } else {
  4723. if ($this->debug > 2) {
  4724. error_log(
  4725. 'New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB',
  4726. 0
  4727. );
  4728. }
  4729. }
  4730. return -1;
  4731. }
  4732. /**
  4733. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4734. * @return bool True on success, false on failure
  4735. */
  4736. public function update_display_order()
  4737. {
  4738. $course_id = api_get_course_int_id();
  4739. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4740. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4741. $res = Database::query($sql);
  4742. if ($res === false) {
  4743. return false;
  4744. }
  4745. $lps = array();
  4746. $lp_order = array();
  4747. $num = Database :: num_rows($res);
  4748. // First check the order is correct, globally (might be wrong because
  4749. // of versions < 1.8.4).
  4750. if ($num > 0) {
  4751. $i = 1;
  4752. while ($row = Database :: fetch_array($res)) {
  4753. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4754. $need_fix = true;
  4755. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4756. $res_u = Database::query($sql_u);
  4757. }
  4758. $i++;
  4759. }
  4760. }
  4761. return true;
  4762. }
  4763. /**
  4764. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4765. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4766. */
  4767. public function update_reinit()
  4768. {
  4769. $course_id = api_get_course_int_id();
  4770. if ($this->debug > 0) {
  4771. error_log('New LP - In learnpath::update_reinit()', 0);
  4772. }
  4773. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4774. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4775. $res = Database::query($sql);
  4776. if (Database :: num_rows($res) > 0) {
  4777. $row = Database :: fetch_array($res);
  4778. $force = $row['prevent_reinit'];
  4779. if ($force == 1) {
  4780. $force = 0;
  4781. } elseif ($force == 0) {
  4782. $force = 1;
  4783. }
  4784. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4785. $res = Database::query($sql);
  4786. $this->prevent_reinit = $force;
  4787. return $force;
  4788. } else {
  4789. if ($this->debug > 2) {
  4790. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4791. }
  4792. }
  4793. return -1;
  4794. }
  4795. /**
  4796. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4797. *
  4798. * @return string 'single', 'multi' or 'seriousgame'
  4799. * @author ndiechburg <noel@cblue.be>
  4800. **/
  4801. public function get_attempt_mode()
  4802. {
  4803. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4804. $this->seriousgame_mode = 0;
  4805. }
  4806. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4807. $this->prevent_reinit = 1;
  4808. }
  4809. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4810. return 'seriousgame';
  4811. }
  4812. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4813. return 'single';
  4814. }
  4815. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4816. return 'multiple';
  4817. }
  4818. return 'single';
  4819. }
  4820. /**
  4821. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4822. *
  4823. * @param string 'seriousgame', 'single' or 'multiple'
  4824. * @return boolean
  4825. * @author ndiechburg <noel@cblue.be>
  4826. **/
  4827. public function set_attempt_mode($mode)
  4828. {
  4829. $course_id = api_get_course_int_id();
  4830. switch ($mode) {
  4831. case 'seriousgame' :
  4832. $sg_mode = 1;
  4833. $prevent_reinit = 1;
  4834. break;
  4835. case 'single' :
  4836. $sg_mode = 0;
  4837. $prevent_reinit = 1;
  4838. break;
  4839. case 'multiple' :
  4840. $sg_mode = 0;
  4841. $prevent_reinit = 0;
  4842. break;
  4843. default :
  4844. $sg_mode = 0;
  4845. $prevent_reinit = 0;
  4846. break;
  4847. }
  4848. $this->prevent_reinit = $prevent_reinit;
  4849. $this->seriousgame_mode = $sg_mode;
  4850. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4851. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4852. );
  4853. $res = Database::query($sql);
  4854. if ($res) {
  4855. return true;
  4856. } else {
  4857. return false;
  4858. }
  4859. }
  4860. /**
  4861. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4862. *
  4863. * @return boolean
  4864. * @author ndiechburg <noel@cblue.be>
  4865. **/
  4866. public function switch_attempt_mode()
  4867. {
  4868. if ($this->debug > 0) {
  4869. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4870. }
  4871. $mode = $this->get_attempt_mode();
  4872. switch ($mode) {
  4873. case 'single' :
  4874. $next_mode = 'multiple';
  4875. break;
  4876. case 'multiple' :
  4877. $next_mode = 'seriousgame';
  4878. break;
  4879. case 'seriousgame' :
  4880. $next_mode = 'single';
  4881. break;
  4882. default :
  4883. $next_mode = 'single';
  4884. break;
  4885. }
  4886. $this->set_attempt_mode($next_mode);
  4887. }
  4888. /**
  4889. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4890. *
  4891. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4892. * @author ndiechburg <noel@cblue.be>
  4893. **/
  4894. public function set_seriousgame_mode()
  4895. {
  4896. $course_id = api_get_course_int_id();
  4897. if ($this->debug > 0) {
  4898. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4899. }
  4900. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4901. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4902. $res = Database::query($sql);
  4903. if (Database :: num_rows($res) > 0) {
  4904. $row = Database :: fetch_array($res);
  4905. $force = $row['seriousgame_mode'];
  4906. if ($force == 1) {
  4907. $force = 0;
  4908. } elseif ($force == 0) {
  4909. $force = 1;
  4910. }
  4911. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4912. );
  4913. $res = Database::query($sql);
  4914. $this->seriousgame_mode = $force;
  4915. return $force;
  4916. } else {
  4917. if ($this->debug > 2) {
  4918. error_log(
  4919. 'New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB',
  4920. 0
  4921. );
  4922. }
  4923. }
  4924. return -1;
  4925. }
  4926. /**
  4927. * Updates the "scorm_debug" value that shows or hide the debug window
  4928. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4929. */
  4930. public function update_scorm_debug()
  4931. {
  4932. $course_id = api_get_course_int_id();
  4933. if ($this->debug > 0) {
  4934. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4935. }
  4936. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4937. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4938. $res = Database::query($sql);
  4939. if (Database :: num_rows($res) > 0) {
  4940. $row = Database :: fetch_array($res);
  4941. $force = $row['debug'];
  4942. if ($force == 1) {
  4943. $force = 0;
  4944. } elseif ($force == 0) {
  4945. $force = 1;
  4946. }
  4947. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4948. $res = Database::query($sql);
  4949. $this->scorm_debug = $force;
  4950. return $force;
  4951. } else {
  4952. if ($this->debug > 2) {
  4953. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  4954. }
  4955. }
  4956. return -1;
  4957. }
  4958. /**
  4959. * Function that makes a call to the function sort_tree_array and create_tree_array
  4960. * @author Kevin Van Den Haute
  4961. * @param array
  4962. */
  4963. public function tree_array($array)
  4964. {
  4965. if ($this->debug > 1) {
  4966. error_log('New LP - In learnpath::tree_array()', 0);
  4967. }
  4968. $array = $this->sort_tree_array($array);
  4969. $this->create_tree_array($array);
  4970. }
  4971. /**
  4972. * Creates an array with the elements of the learning path tree in it
  4973. *
  4974. * @author Kevin Van Den Haute
  4975. * @param array $array
  4976. * @param int $parent
  4977. * @param int $depth
  4978. * @param array $tmp
  4979. */
  4980. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  4981. {
  4982. if ($this->debug > 1) {
  4983. error_log('New LP - In learnpath::create_tree_array())', 0);
  4984. }
  4985. if (is_array($array)) {
  4986. for ($i = 0; $i < count($array); $i++) {
  4987. if ($array[$i]['parent_item_id'] == $parent) {
  4988. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4989. $tmp[] = $array[$i]['parent_item_id'];
  4990. $depth++;
  4991. }
  4992. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4993. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4994. $this->arrMenu[] = array(
  4995. 'id' => $array[$i]['id'],
  4996. 'item_type' => $array[$i]['item_type'],
  4997. 'title' => $array[$i]['title'],
  4998. 'path' => $array[$i]['path'],
  4999. 'description' => $array[$i]['description'],
  5000. 'parent_item_id' => $array[$i]['parent_item_id'],
  5001. 'previous_item_id' => $array[$i]['previous_item_id'],
  5002. 'next_item_id' => $array[$i]['next_item_id'],
  5003. 'min_score' => $array[$i]['min_score'],
  5004. 'max_score' => $array[$i]['max_score'],
  5005. 'mastery_score' => $array[$i]['mastery_score'],
  5006. 'display_order' => $array[$i]['display_order'],
  5007. 'prerequisite' => $preq,
  5008. 'depth' => $depth,
  5009. 'audio' => $audio
  5010. );
  5011. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5012. }
  5013. }
  5014. }
  5015. }
  5016. /**
  5017. * Sorts a multi dimensional array by parent id and display order
  5018. * @author Kevin Van Den Haute
  5019. *
  5020. * @param array $array (array with al the learning path items in it)
  5021. *
  5022. * @return array
  5023. */
  5024. public function sort_tree_array($array)
  5025. {
  5026. foreach ($array as $key => $row) {
  5027. $parent[$key] = $row['parent_item_id'];
  5028. $position[$key] = $row['display_order'];
  5029. }
  5030. if (count($array) > 0) {
  5031. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5032. }
  5033. return $array;
  5034. }
  5035. /**
  5036. * Function that creates a table structure with a learning path his modules, chapters and documents.
  5037. * Also the actions for the modules, chapters and documents are in this table.
  5038. * @author Kevin Van Den Haute
  5039. * @param int $lp_id
  5040. * @return string
  5041. */
  5042. public function overview()
  5043. {
  5044. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5045. if ($this->debug > 0) {
  5046. error_log('New LP - In learnpath::overview()', 0);
  5047. }
  5048. global $_course;
  5049. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5050. $return = '';
  5051. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5052. // we need to start a form when we want to update all the mp3 files
  5053. if ($update_audio == 'true') {
  5054. $return .= '<form action="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5055. $_GET['cidReq']
  5056. ).'&amp;updateaudio='.Security :: remove_XSS($_GET['updateaudio']).'&amp;action='.Security :: remove_XSS(
  5057. $_GET['action']
  5058. ).'&amp;lp_id='.$_SESSION['oLP']->lp_id.'" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5059. }
  5060. $return .= '<div id="message"></div>';
  5061. $return_audio = '<table class="data_table">';
  5062. $return_audio .= '<tr>';
  5063. $return_audio .= '<th width="60%">'.get_lang('Title').'</th>';
  5064. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5065. $return_audio .= '</tr>';
  5066. if ($update_audio != 'true') {
  5067. $return .= '<div class="span12">';
  5068. $return .= self::return_new_tree($update_audio);
  5069. $return .= '</div>';
  5070. $return .= Display::div(
  5071. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn')),
  5072. array('style' => 'float:left; margin-top:15px;width:100%')
  5073. );
  5074. } else {
  5075. $return_audio .= self::return_new_tree($update_audio);
  5076. $return .= $return_audio.'</table>';
  5077. }
  5078. // We need to close the form when we are updating the mp3 files.
  5079. if ($update_audio == 'true') {
  5080. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">'.get_lang(
  5081. 'SaveAudioAndOrganization'
  5082. ).'</button></div>'; // TODO: What kind of language variable is this?
  5083. }
  5084. // We need to close the form when we are updating the mp3 files.
  5085. if ($update_audio == 'true' && count($arrLP) != 0) {
  5086. $return .= '</form>';
  5087. }
  5088. return $return;
  5089. }
  5090. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5091. {
  5092. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  5093. echo '<script>
  5094. var newOrderData= "";
  5095. function processChildren(parentId) {
  5096. //Loop through the children of the UL element defined by the parentId
  5097. var ulParentID= "UL_" + parentId;
  5098. $("#" + ulParentID).children().each(function () {
  5099. /*Only process elements with an id attribute (in order to skip the blank,
  5100. unmovable <li> elements.*/
  5101. if ($(this).attr("id")) {
  5102. /*Build a string of data with the childs ID and parent ID,
  5103. using the "|" as a delimiter between the two IDs and the "^"
  5104. as a record delimiter (these delimiters were chosen in case the data
  5105. involved includes more common delimiters like commas within the content)
  5106. */
  5107. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  5108. //Determine if this child is a containter
  5109. if ($(this).is(".container")) {
  5110. //Process the child elements of the container
  5111. processChildren($(this).attr("id"));
  5112. }
  5113. }
  5114. }); //end of children loop
  5115. } //end of processChildren function
  5116. $(function() {
  5117. $(".item_data").live("mouseover", function(event) {
  5118. $(".button_actions", this).show();
  5119. });
  5120. $(".item_data").live("mouseout", function() {
  5121. $(".button_actions",this).hide();
  5122. });
  5123. $(".button_actions").hide();
  5124. $( ".lp_resource" ).sortable({
  5125. items: ".lp_resource_element ",
  5126. handle: ".moved", //only the class "moved"
  5127. cursor: "move",
  5128. connectWith: "#lp_item_list",
  5129. placeholder: "ui-state-highlight", //defines the yellow highlight
  5130. start: function(event, ui) {
  5131. $(ui.item).css("width", "160px");
  5132. $(ui.item).find(".item_data").attr("style", "");
  5133. },
  5134. stop: function(event, ui) {
  5135. $(ui.item).css("width", "100%");
  5136. },
  5137. });
  5138. $("#lp_item_list").sortable({
  5139. items: "li",
  5140. handle: ".moved", //only the class "moved"
  5141. cursor: "move",
  5142. placeholder: "ui-state-highlight", //defines the yellow highlight
  5143. update: function(event, ui) {
  5144. //Walk through the direct descendants of the lp_item_list <ul>
  5145. $("#lp_item_list").children().each(function () {
  5146. /*Only process elements with an id attribute (in order to skip the blank,
  5147. unmovable <li> elements.*/
  5148. if ($(this).attr("id")) {
  5149. /*Build a string of data with the child s ID and parent ID,
  5150. using the "|" as a delimiter between the two IDs and the "^"
  5151. as a record delimiter (these delimiters were chosen in case the data
  5152. involved includes more common delimiters like commas within the content)
  5153. */
  5154. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  5155. //Determine if this child is a containter
  5156. if ($(this).is(".li_container")) {
  5157. //Process the child elements of the container
  5158. processChildren($(this).attr("id"));
  5159. }
  5160. }
  5161. }); //end of lp_item_list children loop
  5162. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  5163. $.post("'.$ajax_url.'", order, function(reponse){
  5164. $("#message").html(reponse);
  5165. });
  5166. },
  5167. receive: function(event, ui) {
  5168. var id = $(ui.item).attr("data_id");
  5169. var type = $(ui.item).attr("data_type");
  5170. var title = $(ui.item).attr("title");
  5171. if (ui.item.parent()[0]) {
  5172. var parent_id = $(ui.item.parent()[0]).attr("id");
  5173. var previous_id = $(ui.item.prev()).attr("id");
  5174. if (parent_id) {
  5175. parent_id = parent_id.split("_")[1];
  5176. var params = {
  5177. "a": "add_lp_item",
  5178. "id": id,
  5179. "parent_id": parent_id,
  5180. "previous_id": previous_id,
  5181. "type": type,
  5182. "title" : title
  5183. };
  5184. $.ajax({
  5185. type: "GET",
  5186. url: "'.$ajax_url.'",
  5187. data: params,
  5188. async: false,
  5189. success: function(data) {
  5190. if (data == -1) {
  5191. } else {
  5192. $(".normal-message").hide();
  5193. $(ui.item).attr("id", data);
  5194. $(ui.item).addClass("lp_resource_element_new");
  5195. $(ui.item).find(".item_data").attr("style", "");
  5196. $(ui.item).addClass("record li_container");
  5197. $(ui.item).removeClass("lp_resource_element");
  5198. $(ui.item).removeClass("doc_resource");
  5199. }
  5200. }
  5201. });
  5202. }
  5203. }//
  5204. }//end receive
  5205. });
  5206. });
  5207. </script>';
  5208. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5209. $course_id = api_get_course_int_id();
  5210. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5211. $sql = "SELECT * FROM $tbl_lp_item
  5212. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5213. $result = Database::query($sql);
  5214. $arrLP = array();
  5215. while ($row = Database :: fetch_array($result)) {
  5216. $row['title'] = Security :: remove_XSS($row['title']);
  5217. $row['description'] = Security :: remove_XSS($row['description']);
  5218. $arrLP[] = array(
  5219. 'id' => $row['id'],
  5220. 'item_type' => $row['item_type'],
  5221. 'title' => $row['title'],
  5222. 'path' => $row['path'],
  5223. 'description' => $row['description'],
  5224. 'parent_item_id' => $row['parent_item_id'],
  5225. 'previous_item_id' => $row['previous_item_id'],
  5226. 'next_item_id' => $row['next_item_id'],
  5227. 'max_score' => $row['max_score'],
  5228. 'min_score' => $row['min_score'],
  5229. 'mastery_score' => $row['mastery_score'],
  5230. 'prerequisite' => $row['prerequisite'],
  5231. 'display_order' => $row['display_order'],
  5232. 'audio' => $row['audio']
  5233. );
  5234. }
  5235. $this->tree_array($arrLP);
  5236. $arrLP = $this->arrMenu;
  5237. unset ($this->arrMenu);
  5238. $default_data = null;
  5239. $default_content = null;
  5240. $elements = array();
  5241. $return_audio = null;
  5242. for ($i = 0; $i < count($arrLP); $i++) {
  5243. $title = $arrLP[$i]['title'];
  5244. $title_cut = Text::cut($arrLP[$i]['title'], 25);
  5245. //Link for the documents
  5246. if ($arrLP[$i]['item_type'] == 'document') {
  5247. $url = api_get_self().'?'.api_get_cidreq(
  5248. ).'&amp;action=view_item&amp;mode=preview_document&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id;
  5249. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5250. }
  5251. if (($i % 2) == 0) {
  5252. $oddclass = 'row_odd';
  5253. } else {
  5254. $oddclass = 'row_even';
  5255. }
  5256. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddclass.'">';
  5257. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5258. $icon = '';
  5259. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5260. $icon = '<img src="../img/lp_'.$icon_name.'.png" />';
  5261. } else {
  5262. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5263. $icon = '<img src="../img/lp_'.$icon_name.'.gif" />';
  5264. } else {
  5265. $icon = '<img src="../img/folder_document.gif" />';
  5266. }
  5267. }
  5268. // The audio column.
  5269. $return_audio .= '<td align="center">';
  5270. $audio = '';
  5271. if (!$update_audio OR $update_audio <> 'true') {
  5272. if (!empty($arrLP[$i]['audio'])) {
  5273. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5274. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5275. $audio .= '<script type="text/javascript">
  5276. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5277. s1.addParam("allowscriptaccess","always");
  5278. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5279. s1.write("container' . $i . '");
  5280. </script>';*/
  5281. } else {
  5282. $audio .= '';
  5283. }
  5284. } else {
  5285. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  5286. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5287. if (!empty ($arrLP[$i]['audio'])) {
  5288. $audio .= '<br />'.Security::remove_XSS(
  5289. $arrLP[$i]['audio']
  5290. ).'<br /><input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang(
  5291. 'RemoveAudio'
  5292. );
  5293. }
  5294. }
  5295. }
  5296. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style' => ''));
  5297. $return_audio .= '</td>';
  5298. $move_icon = '';
  5299. $move_item_icon = '';
  5300. $edit_icon = '';
  5301. $delete_icon = '';
  5302. $audio_icon = '';
  5303. $prerequisities_icon = '';
  5304. if ($is_allowed_to_edit) {
  5305. if (!$update_audio OR $update_audio <> 'true') {
  5306. $move_icon .= '<a class="moved" href="#">';
  5307. $move_icon .= Display::return_icon(
  5308. 'move_everywhere.png',
  5309. get_lang('Move'),
  5310. array(),
  5311. ICON_SIZE_TINY
  5312. );
  5313. $move_icon .= '</a>';
  5314. }
  5315. // No edit for this item types
  5316. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5317. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5318. $edit_icon .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5319. $_GET['cidReq']
  5320. ).'&amp;action=edit_item&amp;view=build&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5321. $edit_icon .= Display::return_icon(
  5322. 'edit.png',
  5323. get_lang('LearnpathEditModule'),
  5324. array(),
  5325. ICON_SIZE_TINY
  5326. );
  5327. $edit_icon .= '</a>';
  5328. } else {
  5329. $edit_icon .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5330. $_GET['cidReq']
  5331. ).'&amp;action=edit_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5332. $edit_icon .= Display::return_icon(
  5333. 'edit.png',
  5334. get_lang('LearnpathEditModule'),
  5335. array(),
  5336. ICON_SIZE_TINY
  5337. );
  5338. $edit_icon .= '</a>';
  5339. }
  5340. }
  5341. $delete_icon .= ' <a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5342. $_GET['cidReq']
  5343. ).'&amp;action=delete_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'" onClick="return confirmation(\''.addslashes(
  5344. $title
  5345. ).'\');">';
  5346. $delete_icon .= Display::return_icon(
  5347. 'delete.png',
  5348. get_lang('LearnpathDeleteModule'),
  5349. array(),
  5350. ICON_SIZE_TINY
  5351. );
  5352. $delete_icon .= '</a>';
  5353. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  5354. $_GET['cidReq']
  5355. ).'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5356. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5357. $prerequisities_icon = Display::url(
  5358. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5359. $url.'&action=edit_item_prereq'
  5360. );
  5361. $move_item_icon = Display::url(
  5362. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5363. $url.'&action=move_item'
  5364. );
  5365. $audio_icon = Display::url(
  5366. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5367. $url.'&action=add_audio'
  5368. );
  5369. }
  5370. }
  5371. if ($update_audio != 'true') {
  5372. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span(
  5373. $audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon,
  5374. array('class' => 'button_actions')
  5375. );
  5376. } else {
  5377. $row = Display::span($title.$icon).Display::span($audio, array('class' => 'button_actions'));
  5378. }
  5379. $parent_id = $arrLP[$i]['parent_item_id'];
  5380. $default_data[$arrLP[$i]['id']] = $row;
  5381. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5382. if (empty($parent_id)) {
  5383. $elements[$arrLP[$i]['id']]['data'] = $row;
  5384. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5385. } else {
  5386. $parent_arrays = array();
  5387. if ($arrLP[$i]['depth'] > 1) {
  5388. //Getting list of parents
  5389. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5390. foreach ($arrLP as $item) {
  5391. if ($item['id'] == $parent_id) {
  5392. if ($item['parent_item_id'] == 0) {
  5393. $parent_id = $item['id'];
  5394. break;
  5395. } else {
  5396. $parent_id = $item['parent_item_id'];
  5397. if (empty($parent_arrays)) {
  5398. $parent_arrays[] = intval($item['id']);
  5399. }
  5400. $parent_arrays[] = $parent_id;
  5401. break;
  5402. }
  5403. }
  5404. }
  5405. }
  5406. }
  5407. if (!empty($parent_arrays)) {
  5408. $parent_arrays = array_reverse($parent_arrays);
  5409. $val = '$elements';
  5410. $x = 0;
  5411. foreach ($parent_arrays as $item) {
  5412. if ($x != count($parent_arrays) - 1) {
  5413. $val .= '["'.$item.'"]["children"]';
  5414. } else {
  5415. $val .= '["'.$item.'"]["children"]';
  5416. }
  5417. $x++;
  5418. }
  5419. $val .= "";
  5420. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5421. eval($code_str);
  5422. } else {
  5423. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5424. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5425. }
  5426. }
  5427. }
  5428. $return = '<div class="lp_tree well">';
  5429. $return .= '<ul id="lp_item_list">';
  5430. $return .= '<h4>'.$this->name.'</h4><br>';
  5431. $tree = self::print_recursive($elements, $default_data, $default_content);
  5432. if (!empty($tree)) {
  5433. $return .= $tree;
  5434. } else {
  5435. if ($drop_element_here) {
  5436. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5437. }
  5438. }
  5439. $return .= '</ul>';
  5440. if ($update_audio == 'true') {
  5441. $return = $return_audio;
  5442. } else {
  5443. $return .= '</div>';
  5444. }
  5445. return $return;
  5446. }
  5447. function print_recursive($elements, $default_data, $default_content)
  5448. {
  5449. $return = '';
  5450. foreach ($elements as $key => $item) {
  5451. if (isset($item['load_data']) || empty($item['data'])) {
  5452. $item['data'] = $default_data[$item['load_data']];
  5453. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5454. }
  5455. $sub_list = '';
  5456. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5457. $sub_list = Display::tag('li', '', array('class' => 'sub_item empty')); // empty value
  5458. }
  5459. if (empty($item['children'])) {
  5460. $sub_list = Display::tag('ul', $sub_list, array('id' => 'UL_'.$key, 'class' => 'record li_container'));
  5461. $active = null;
  5462. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5463. $active = 'active';
  5464. }
  5465. $return .= Display::tag(
  5466. 'li',
  5467. Display::div($item['data'], array('class' => "item_data $active")).$sub_list,
  5468. array('id' => $key, 'class' => 'record li_container')
  5469. );
  5470. } else {
  5471. //sections
  5472. if (isset($item['children'])) {
  5473. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5474. }
  5475. $sub_list = Display::tag(
  5476. 'ul',
  5477. $sub_list.$data,
  5478. array('id' => 'UL_'.$key, 'class' => 'record li_container')
  5479. );
  5480. $return .= Display::tag(
  5481. 'li',
  5482. Display::div($item['data'], array('class' => 'item_data')).$sub_list,
  5483. array('id' => $key, 'class' => 'record li_container')
  5484. );
  5485. }
  5486. }
  5487. return $return;
  5488. }
  5489. /**
  5490. * This function builds the action menu
  5491. * @return void
  5492. */
  5493. public function build_action_menu()
  5494. {
  5495. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5496. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5497. $return = '<div class="actions">';
  5498. //$return .= '<a href="lp_controller.php?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&action=build&lp_id=' . $this->lp_id . '">' . Display :: return_icon('home.png', get_lang('Build'),'',ICON_SIZE_MEDIUM).'</a>';
  5499. //$return .= '<a href="' . api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('BasicOverview') . '">' . Display :: return_icon('move_learnpath.png', get_lang('BasicOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  5500. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5501. ).'&amp;gradebook='.$gradebook.'&action=view&lp_id='.$_SESSION['oLP']->lp_id.'&isStudentView=true">'.Display :: return_icon(
  5502. 'preview_view.png',
  5503. get_lang('Display'),
  5504. '',
  5505. ICON_SIZE_MEDIUM
  5506. ).'</a> ';
  5507. //$return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=step&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewStep') . '">' . Display :: return_icon('new_learnigpath_object.png', get_lang('NewStep'),'',ICON_SIZE_MEDIUM).'</a>';
  5508. // echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&amp;action=add_item&amp;type=chapter&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" title="' . get_lang('NewChapter') . '">' . Display :: return_icon('add_learnpath_section.png', get_lang('NewChapter'),'',ICON_SIZE_MEDIUM).'</a>';
  5509. /*$return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5510. ).'&amp;action=admin_view&amp;lp_id='.$_SESSION['oLP']->lp_id.'&amp;updateaudio=true">'.Display :: return_icon(
  5511. 'upload_audio.png',
  5512. get_lang('UpdateAllAudioFragments'),
  5513. '',
  5514. ICON_SIZE_MEDIUM
  5515. ).'</a>';*/
  5516. $return .= '<a href="lp_controller.php?referer=add_item&'.api_get_cidreq().'&amp;action=edit&amp;lp_id='.$_SESSION['oLP']->lp_id.'">'.Display :: return_icon(
  5517. 'settings.png',
  5518. get_lang('CourseSettings'),
  5519. '',
  5520. ICON_SIZE_MEDIUM
  5521. ).'</a>';
  5522. $buttons = array(
  5523. array(
  5524. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5525. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5526. ).'&amp;action=set_previous_step_as_prerequisite&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5527. ),
  5528. array(
  5529. 'title' => get_lang('ClearAllPrerequisites'),
  5530. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5531. ).'&amp;action=clear_prerequisites&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5532. ),
  5533. );
  5534. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5535. $return .= '</div>';
  5536. echo $return;
  5537. }
  5538. /**
  5539. * This functions builds the LP tree based on data from the database.
  5540. * @return string
  5541. * @deprecated use the return_new_tree() function
  5542. * @uses dtree.js :: necessary javascript for building this tree
  5543. */
  5544. public function build_tree()
  5545. {
  5546. $course_id = api_get_course_int_id();
  5547. $return = "<script type=\"text/javascript\">\n";
  5548. $return .= "\tm = new dTree('m');\n\n";
  5549. $return .= "\tm.config.folderLinks = true;\n";
  5550. $return .= "\tm.config.useCookies = true;\n";
  5551. $return .= "\tm.config.useIcons = true;\n";
  5552. $return .= "\tm.config.useLines = true;\n";
  5553. $return .= "\tm.config.useSelection = true;\n";
  5554. $return .= "\tm.config.useStatustext = false;\n\n";
  5555. $menu = 0;
  5556. $parent = '';
  5557. $return .= "\tm.add(".$menu.", -1, '".addslashes(Security::remove_XSS(($this->name)))."');\n";
  5558. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5559. $sql = " SELECT id, title, description, item_type, path, parent_item_id, previous_item_id, next_item_id, max_score, min_score, mastery_score, display_order
  5560. FROM $tbl_lp_item
  5561. WHERE c_id = ".$course_id." AND lp_id = ".Database::escape_string($this->lp_id);
  5562. $result = Database::query($sql);
  5563. $arrLP = array();
  5564. while ($row = Database :: fetch_array($result)) {
  5565. $row['title'] = Security :: remove_XSS($row['title']);
  5566. $row['description'] = Security :: remove_XSS($row['description']);
  5567. $arrLP[] = array(
  5568. 'id' => $row['id'],
  5569. 'item_type' => $row['item_type'],
  5570. 'title' => $row['title'],
  5571. 'path' => $row['path'],
  5572. 'description' => $row['description'],
  5573. 'parent_item_id' => $row['parent_item_id'],
  5574. 'previous_item_id' => $row['previous_item_id'],
  5575. 'next_item_id' => $row['next_item_id'],
  5576. 'max_score' => $row['max_score'],
  5577. 'min_score' => $row['min_score'],
  5578. 'mastery_score' => $row['mastery_score'],
  5579. 'display_order' => $row['display_order']
  5580. );
  5581. }
  5582. $this->tree_array($arrLP);
  5583. $arrLP = $this->arrMenu;
  5584. unset ($this->arrMenu);
  5585. $title = '';
  5586. for ($i = 0; $i < count($arrLP); $i++) {
  5587. $title = addslashes($arrLP[$i]['title']);
  5588. $menu_page = api_get_self().'?cidReq='.Security :: remove_XSS(
  5589. $_GET['cidReq']
  5590. ).'&amp;action=view_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$_SESSION['oLP']->lp_id;
  5591. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5592. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5593. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/lp_".$icon_name.".png', '../img/lp_".$icon_name.".png');\n";
  5594. } else {
  5595. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5596. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/lp_".$icon_name.".gif', '../img/lp_".$icon_name.".gif');\n";
  5597. } else {
  5598. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  5599. }
  5600. }
  5601. if ($menu < $arrLP[$i]['id']) {
  5602. $menu = $arrLP[$i]['id'];
  5603. }
  5604. }
  5605. $return .= "\n\tdocument.write(m);\n";
  5606. $return .= "\t if(!m.selectedNode) m.s(1);";
  5607. $return .= "</script>\n";
  5608. return $return;
  5609. }
  5610. /**
  5611. * Creates the default learning path folder
  5612. */
  5613. public static function generate_learning_path_folder($course)
  5614. {
  5615. //Creating learning_path folder
  5616. $dir = '/learning_path';
  5617. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5618. $folder = false;
  5619. if (!is_dir($filepath.'/'.$dir)) {
  5620. $folder = FileManager::create_unexisting_directory(
  5621. $course,
  5622. api_get_user_id(),
  5623. api_get_session_id(),
  5624. 0,
  5625. 0,
  5626. $filepath,
  5627. $dir,
  5628. get_lang('LearningPaths'),
  5629. 0
  5630. );
  5631. } else {
  5632. $folder = true;
  5633. }
  5634. return $folder;
  5635. }
  5636. public function generate_lp_folder($course, $lp_name = null)
  5637. {
  5638. $filepath = '';
  5639. $dir = '/learning_path/';
  5640. if (empty($lp_name)) {
  5641. $lp_name = $this->name;
  5642. }
  5643. $folder = self::generate_learning_path_folder($course);
  5644. //Creating LP folder
  5645. if ($folder) {
  5646. //Limits title size
  5647. $title = api_substr(replace_dangerous_char($lp_name), 0, 80);
  5648. $dir = $dir.$title;
  5649. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5650. if (!is_dir($filepath.'/'.$dir)) {
  5651. $folder = FileManager::create_unexisting_directory(
  5652. $course,
  5653. api_get_user_id(),
  5654. 0,
  5655. 0,
  5656. 0,
  5657. $filepath,
  5658. $dir,
  5659. $lp_name
  5660. );
  5661. } else {
  5662. $folder = true;
  5663. }
  5664. $dir = $dir.'/';
  5665. if ($folder) {
  5666. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5667. }
  5668. }
  5669. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5670. return $array;
  5671. }
  5672. /**
  5673. * Create a new document //still needs some finetuning
  5674. * @param array $_course
  5675. * @return string
  5676. */
  5677. public function create_document($_course)
  5678. {
  5679. $course_id = api_get_course_int_id();
  5680. global $charset;
  5681. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5682. if (strstr($dir, '..')) {
  5683. $dir = '/';
  5684. }
  5685. if ($dir[0] == '.') {
  5686. $dir = substr($dir, 1);
  5687. }
  5688. if ($dir[0] != '/') {
  5689. $dir = '/'.$dir;
  5690. }
  5691. if ($dir[strlen($dir) - 1] != '/') {
  5692. $dir .= '/';
  5693. }
  5694. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5695. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5696. //Generates folder
  5697. $result = $this->generate_lp_folder($_course);
  5698. $dir = $result['dir'];
  5699. $filepath = $result['filepath'];
  5700. }
  5701. if (!is_dir($filepath)) {
  5702. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5703. $dir = '/';
  5704. }
  5705. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  5706. // is already escaped twice when it gets here.
  5707. $title = replace_dangerous_char(stripslashes($_POST['title']));
  5708. $title = FileManager::disable_dangerous_file($title);
  5709. $filename = $title;
  5710. $content = $_POST['content_lp'];
  5711. $tmp_filename = $filename;
  5712. $i = 0;
  5713. while (file_exists($filepath.$tmp_filename.'.html')) {
  5714. $tmp_filename = $filename.'_'.++$i;
  5715. }
  5716. $filename = $tmp_filename.'.html';
  5717. $content = stripslashes($content);
  5718. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5719. // Change the path of mp3 to absolute.
  5720. // The first regexp deals with ../../../ urls.
  5721. $content = preg_replace(
  5722. "|(flashvars=\"file=)(\.+/)+|",
  5723. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5724. $content
  5725. );
  5726. // The second regexp deals with audio/ urls.
  5727. $content = preg_replace(
  5728. "|(flashvars=\"file=)([^/]+)/|",
  5729. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5730. $content
  5731. );
  5732. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5733. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5734. if (!file_exists($filepath.$filename)) {
  5735. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5736. fputs($fp, $content);
  5737. fclose($fp);
  5738. $file_size = filesize($filepath.$filename);
  5739. $save_file_path = $dir.$filename;
  5740. $document_id = FileManager::add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5741. if ($document_id) {
  5742. api_item_property_update(
  5743. $_course,
  5744. TOOL_DOCUMENT,
  5745. $document_id,
  5746. 'DocumentAdded',
  5747. api_get_user_id(),
  5748. null,
  5749. null,
  5750. null,
  5751. null,
  5752. api_get_session_id()
  5753. );
  5754. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5755. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5756. if ($new_comment || $new_title) {
  5757. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5758. $ct = '';
  5759. if ($new_comment) {
  5760. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5761. }
  5762. if ($new_title) {
  5763. $ct .= ", title='".Database::escape_string(
  5764. htmlspecialchars($new_title, ENT_QUOTES, $charset)
  5765. )."' ";
  5766. }
  5767. $sql_update = "UPDATE ".$tbl_doc." SET ".substr(
  5768. $ct,
  5769. 1
  5770. )." WHERE c_id = ".$course_id." AND id = ".$document_id;
  5771. Database::query($sql_update);
  5772. }
  5773. }
  5774. return $document_id;
  5775. }
  5776. }
  5777. }
  5778. /**
  5779. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5780. * @param array $_course array
  5781. * @return void
  5782. */
  5783. public function edit_document($_course)
  5784. {
  5785. $course_id = api_get_course_int_id();
  5786. global $_configuration;
  5787. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5788. if (strstr($dir, '..')) {
  5789. $dir = '/';
  5790. }
  5791. if ($dir[0] == '.') {
  5792. $dir = substr($dir, 1);
  5793. }
  5794. if ($dir[0] != '/') {
  5795. $dir = '/'.$dir;
  5796. }
  5797. if ($dir[strlen($dir) - 1] != '/') {
  5798. $dir .= '/';
  5799. }
  5800. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5801. if (!is_dir($filepath)) {
  5802. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5803. $dir = '/';
  5804. }
  5805. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5806. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5807. $document_id = intval($_POST['path']);
  5808. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = $course_id AND id = ".$document_id;
  5809. $res = Database::query($sql);
  5810. $row = Database :: fetch_array($res);
  5811. $content = stripslashes($_POST['content_lp']);
  5812. $file = $filepath.$row['path'];
  5813. if ($fp = @ fopen($file, 'w')) {
  5814. $content = str_replace(
  5815. api_get_path(WEB_COURSE_PATH),
  5816. $_configuration['url_append'].'/courses/',
  5817. $content
  5818. );
  5819. // Change the path of mp3 to absolute.
  5820. // The first regexp deals with ../../../ urls.
  5821. $content = preg_replace(
  5822. "|(flashvars=\"file=)(\.+/)+|",
  5823. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5824. $content
  5825. );
  5826. // The second regexp deals with audio/ urls.
  5827. $content = preg_replace(
  5828. "|(flashvars=\"file=)([^/]+)/|",
  5829. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5830. $content
  5831. );
  5832. fputs($fp, $content);
  5833. fclose($fp);
  5834. $sql_update = "UPDATE ".$table_doc." SET title='".Database::escape_string(
  5835. $_POST['title']
  5836. )."' WHERE c_id = ".$course_id." AND id = ".$document_id;
  5837. Database::query($sql_update);
  5838. }
  5839. }
  5840. }
  5841. /**
  5842. * Displays the selected item, with a panel for manipulating the item
  5843. * @param int $item_id
  5844. * @param string $msg
  5845. * @return string
  5846. */
  5847. public function display_item($item_id, $msg = null, $show_actions = true)
  5848. {
  5849. $course_id = api_get_course_int_id();
  5850. $return = '';
  5851. if (is_numeric($item_id)) {
  5852. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5853. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5854. $sql = "SELECT lp.* FROM ".$tbl_lp_item." as lp
  5855. WHERE c_id = ".$course_id." AND lp.id = ".Database::escape_string($item_id);
  5856. $result = Database::query($sql);
  5857. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  5858. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5859. // Prevents wrong parent selection for document, see Bug#1251.
  5860. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5861. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5862. }
  5863. if ($show_actions) {
  5864. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5865. }
  5866. $return .= '<div style="padding:10px;">';
  5867. if ($msg != '') {
  5868. $return .= $msg;
  5869. }
  5870. $return .= '<h3>'.$row['title'].'</h3>';
  5871. switch ($row['item_type']) {
  5872. case TOOL_QUIZ:
  5873. if (!empty($row['path'])) {
  5874. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5875. $exercise = new Exercise();
  5876. $exercise->read($row['path']);
  5877. $return .= $exercise->description.'<br />';
  5878. }
  5879. break;
  5880. case TOOL_DOCUMENT:
  5881. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5882. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  5883. $row['path']
  5884. );
  5885. $result = Database::query($sql_doc);
  5886. $path_file = Database::result($result, 0, 0);
  5887. $path_parts = pathinfo($path_file);
  5888. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5889. if (in_array(
  5890. $path_parts['extension'],
  5891. array(
  5892. 'html',
  5893. 'txt',
  5894. 'png',
  5895. 'jpg',
  5896. 'JPG',
  5897. 'jpeg',
  5898. 'JPEG',
  5899. 'gif',
  5900. 'swf'
  5901. )
  5902. )
  5903. ) {
  5904. $return .= $this->display_document($row['path'], true, true);
  5905. }
  5906. break;
  5907. }
  5908. $return .= '</div>';
  5909. }
  5910. }
  5911. return $return;
  5912. }
  5913. /**
  5914. * Shows the needed forms for editing a specific item
  5915. * @param int $item_id
  5916. * @return string
  5917. */
  5918. public function display_edit_item($item_id)
  5919. {
  5920. global $_course; // It will disappear.
  5921. $course_id = api_get_course_int_id();
  5922. $return = '';
  5923. if (is_numeric($item_id)) {
  5924. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5925. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = ".Database::escape_string($item_id);
  5926. $res = Database::query($sql);
  5927. $row = Database::fetch_array($res);
  5928. switch ($row['item_type']) {
  5929. case 'dokeos_chapter' :
  5930. case 'dir' :
  5931. case 'asset' :
  5932. case 'sco' :
  5933. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5934. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5935. $return .= $this->display_item_form(
  5936. $row['item_type'],
  5937. get_lang('EditCurrentChapter').' :',
  5938. 'edit',
  5939. $item_id,
  5940. $row
  5941. );
  5942. } else {
  5943. $return .= $this->display_item_small_form(
  5944. $row['item_type'],
  5945. get_lang('EditCurrentChapter').' :',
  5946. $row
  5947. );
  5948. }
  5949. break;
  5950. case TOOL_DOCUMENT :
  5951. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5952. $sql_step = " SELECT lp.*, doc.path as dir
  5953. FROM ".$tbl_lp_item." as lp
  5954. LEFT JOIN ".$tbl_doc." as doc ON doc.id = lp.path
  5955. WHERE lp.c_id = $course_id AND
  5956. doc.c_id = $course_id AND
  5957. lp.id = ".Database::escape_string($item_id);
  5958. $res_step = Database::query($sql_step);
  5959. $row_step = Database :: fetch_array($res_step);
  5960. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5961. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5962. break;
  5963. case TOOL_LINK :
  5964. $link_id = (string)$row['path'];
  5965. if (ctype_digit($link_id)) {
  5966. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5967. $sql_select = 'SELECT url FROM '.$tbl_link.' WHERE c_id = '.$course_id.' AND id = '.Database::escape_string(
  5968. $link_id
  5969. );
  5970. $res_link = Database::query($sql_select);
  5971. $row_link = Database :: fetch_array($res_link);
  5972. if (is_array($row_link)) {
  5973. $row['url'] = $row_link['url'];
  5974. }
  5975. }
  5976. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5977. $return .= $this->display_link_form('edit', $item_id, $row);
  5978. break;
  5979. case 'dokeos_module' :
  5980. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5981. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5982. $return .= $this->display_item_form(
  5983. $row['item_type'],
  5984. get_lang('EditCurrentModule').' :',
  5985. 'edit',
  5986. $item_id,
  5987. $row
  5988. );
  5989. } else {
  5990. $return .= $this->display_item_small_form(
  5991. $row['item_type'],
  5992. get_lang('EditCurrentModule').' :',
  5993. $row
  5994. );
  5995. }
  5996. break;
  5997. case TOOL_QUIZ :
  5998. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5999. $return .= $this->display_quiz_form('edit', $item_id, $row);
  6000. break;
  6001. case TOOL_HOTPOTATOES :
  6002. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6003. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6004. break;
  6005. case TOOL_STUDENTPUBLICATION :
  6006. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6007. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6008. break;
  6009. case TOOL_FORUM :
  6010. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6011. $return .= $this->display_forum_form('edit', $item_id, $row);
  6012. break;
  6013. case TOOL_THREAD :
  6014. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6015. $return .= $this->display_thread_form('edit', $item_id, $row);
  6016. break;
  6017. }
  6018. }
  6019. return $return;
  6020. }
  6021. /**
  6022. * Function that displays a list with al the resources that could be added to the learning path
  6023. * @return string
  6024. */
  6025. public function display_resources()
  6026. {
  6027. global $_course; // TODO: Don't use globals.
  6028. $course_code = api_get_course_id();
  6029. //Get all the docs
  6030. $documents = $this->get_documents();
  6031. //Get all the exercises
  6032. $exercises = $this->get_exercises();
  6033. // Get all the links
  6034. $links = $this->get_links();
  6035. //Get al the student publications
  6036. $works = $this->get_student_publications();
  6037. //Get al the forums
  6038. $forums = $this->get_forums(null, $course_code);
  6039. $headers = array(
  6040. Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  6041. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  6042. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  6043. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  6044. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  6045. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  6046. );
  6047. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6048. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  6049. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  6050. return true;
  6051. }
  6052. /**
  6053. * Returns the extension of a document
  6054. * @param string filename
  6055. * @return string Extension (part after the last dot)
  6056. */
  6057. public function get_extension($filename)
  6058. {
  6059. $explode = explode('.', $filename);
  6060. return $explode[count($explode) - 1];
  6061. }
  6062. /**
  6063. * Displays a document by id
  6064. *
  6065. * @param unknown_type $id
  6066. * @return unknown
  6067. */
  6068. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6069. {
  6070. global $_course; // It is temporary.
  6071. $course_id = api_get_course_int_id();
  6072. $return = '';
  6073. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6074. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6075. WHERE c_id = ".$course_id." AND id = ".$id;
  6076. $res_doc = Database::query($sql_doc);
  6077. $row_doc = Database :: fetch_array($res_doc);
  6078. // TODO: Add a path filter.
  6079. if ($iframe) {
  6080. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="'.api_get_path(
  6081. WEB_COURSE_PATH
  6082. ).$_course['path'].'/document'.str_replace('%2F', '/', urlencode($row_doc['path'])).'?'.api_get_cidreq(
  6083. ).'"></iframe>';
  6084. } else {
  6085. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row_doc['path']);
  6086. }
  6087. return $return;
  6088. }
  6089. /**
  6090. * Return HTML form to add/edit a quiz
  6091. * @param string Action (add/edit)
  6092. * @param integer Item ID if already exists
  6093. * @param mixed Extra information (quiz ID if integer)
  6094. * @return string HTML form
  6095. */
  6096. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6097. {
  6098. $course_id = api_get_course_int_id();
  6099. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6100. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6101. if ($id != 0 && is_array($extra_info)) {
  6102. $item_title = $extra_info['title'];
  6103. $item_description = $extra_info['description'];
  6104. } elseif (is_numeric($extra_info)) {
  6105. $sql_quiz = "SELECT title, description FROM ".$tbl_quiz." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  6106. $result = Database::query($sql_quiz);
  6107. $row = Database :: fetch_array($result);
  6108. $item_title = $row['title'];
  6109. $item_description = $row['description'];
  6110. } else {
  6111. $item_title = '';
  6112. $item_description = '';
  6113. }
  6114. $item_title = Security::remove_XSS($item_title);
  6115. $item_description = Security::remove_XSS($item_description);
  6116. $legend = '<legend>';
  6117. if ($id != 0 && is_array($extra_info)) {
  6118. $parent = $extra_info['parent_item_id'];
  6119. } else {
  6120. $parent = 0;
  6121. }
  6122. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6123. $result = Database::query($sql);
  6124. $arrLP = array();
  6125. while ($row = Database :: fetch_array($result)) {
  6126. $arrLP[] = array(
  6127. 'id' => $row['id'],
  6128. 'item_type' => $row['item_type'],
  6129. 'title' => $row['title'],
  6130. 'path' => $row['path'],
  6131. 'description' => $row['description'],
  6132. 'parent_item_id' => $row['parent_item_id'],
  6133. 'previous_item_id' => $row['previous_item_id'],
  6134. 'next_item_id' => $row['next_item_id'],
  6135. 'display_order' => $row['display_order'],
  6136. 'max_score' => $row['max_score'],
  6137. 'min_score' => $row['min_score'],
  6138. 'mastery_score' => $row['mastery_score'],
  6139. 'prerequisite' => $row['prerequisite'],
  6140. 'max_time_allowed' => $row['max_time_allowed']
  6141. );
  6142. }
  6143. $this->tree_array($arrLP);
  6144. $arrLP = $this->arrMenu;
  6145. unset ($this->arrMenu);
  6146. if ($action == 'add') {
  6147. $legend .= get_lang('CreateTheExercise').'&nbsp;:';
  6148. } elseif ($action == 'move') {
  6149. $legend .= get_lang('MoveTheCurrentExercise').'&nbsp;:';
  6150. } else {
  6151. $legend .= get_lang('EditCurrentExecice').'&nbsp;:';
  6152. }
  6153. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6154. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6155. }
  6156. $legend .= '</legend>';
  6157. $return .= '<div class="sectioncomment">';
  6158. $return .= '<form method="POST">';
  6159. $return .= $legend;
  6160. $return .= '<table class="lp_form">';
  6161. if ($action != 'move') {
  6162. $return .= '<tr>';
  6163. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6164. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" /></td>';
  6165. $return .= '</tr>';
  6166. }
  6167. $return .= '<tr>';
  6168. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6169. $return .= '<td class="input">';
  6170. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6171. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6172. $arrHide = array(
  6173. $id
  6174. );
  6175. //$parent_item_id = $_SESSION['parent_item_id'];
  6176. for ($i = 0; $i < count($arrLP); $i++) {
  6177. if ($action != 'add') {
  6178. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6179. $arrLP[$i]['id'],
  6180. $arrHide
  6181. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6182. ) {
  6183. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6184. } else {
  6185. $arrHide[] = $arrLP[$i]['id'];
  6186. }
  6187. } else {
  6188. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6189. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6190. }
  6191. }
  6192. }
  6193. if (is_array($arrLP)) {
  6194. reset($arrLP);
  6195. }
  6196. $return .= '</select>';
  6197. $return .= '</td>';
  6198. $return .= '</tr>';
  6199. $return .= '<tr>';
  6200. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6201. $return .= '<td class="input">';
  6202. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6203. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6204. for ($i = 0; $i < count($arrLP); $i++) {
  6205. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6206. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6207. $selected = 'selected="selected" ';
  6208. } elseif ($action == 'add') {
  6209. $selected = 'selected="selected" ';
  6210. } else {
  6211. $selected = '';
  6212. }
  6213. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6214. 'After'
  6215. ).' "'.$arrLP[$i]['title'].'"</option>';
  6216. }
  6217. }
  6218. $return .= '</select>';
  6219. $return .= '</td>';
  6220. $return .= '</tr>';
  6221. if ($action != 'move') {
  6222. $id_prerequisite = 0;
  6223. if (is_array($arrLP)) {
  6224. foreach ($arrLP as $key => $value) {
  6225. if ($value['id'] == $id) {
  6226. $id_prerequisite = $value['prerequisite'];
  6227. break;
  6228. }
  6229. }
  6230. }
  6231. $arrHide = array();
  6232. for ($i = 0; $i < count($arrLP); $i++) {
  6233. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6234. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6235. $s_selected_position = $arrLP[$i]['id'];
  6236. } elseif ($action == 'add') {
  6237. $s_selected_position = 0;
  6238. }
  6239. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6240. }
  6241. }
  6242. /*// Commented the prerequisites, only visible in edit (exercise).
  6243. $return .= '<tr>';
  6244. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6245. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6246. foreach($arrHide as $key => $value){
  6247. if($key==$s_selected_position && $action == 'add'){
  6248. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6249. }
  6250. elseif($key==$id_prerequisite && $action == 'edit'){
  6251. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6252. }
  6253. else{
  6254. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6255. }
  6256. }
  6257. $return .= "</select></td>";
  6258. */
  6259. $return .= '</tr>';
  6260. /*$return .= '<tr>';
  6261. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6262. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6263. // Remove temporarily the test description.
  6264. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6265. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6266. $return .= '</tr>'; */
  6267. }
  6268. $return .= '<tr>';
  6269. if ($action == 'add') {
  6270. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6271. 'AddExercise'
  6272. ).'</button></td>';
  6273. } else {
  6274. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6275. 'EditCurrentExecice'
  6276. ).'</button></td>';
  6277. }
  6278. $return .= '</tr>';
  6279. $return .= '</table>';
  6280. if ($action == 'move') {
  6281. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6282. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6283. }
  6284. if (is_numeric($extra_info)) {
  6285. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6286. } elseif (is_array($extra_info)) {
  6287. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6288. }
  6289. $return .= '<input name="type" type="hidden" value="'.TOOL_QUIZ.'" />';
  6290. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6291. $return .= '</form>';
  6292. $return .= '</div>';
  6293. return $return;
  6294. }
  6295. /**
  6296. * Addition of Hotpotatoes tests
  6297. * @param string Action
  6298. * @param integer Internal ID of the item
  6299. * @param mixed Extra information - can be an array with title and description indexes
  6300. * @return string HTML structure to display the hotpotatoes addition formular
  6301. */
  6302. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6303. {
  6304. $course_id = api_get_course_int_id();
  6305. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6306. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6307. if ($id != 0 && is_array($extra_info)) {
  6308. $item_title = stripslashes($extra_info['title']);
  6309. $item_description = stripslashes($extra_info['description']);
  6310. } elseif (is_numeric($extra_info)) {
  6311. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6312. $sql_hot = "SELECT * FROM ".$TBL_DOCUMENT."
  6313. WHERE c_id = ".$course_id." AND
  6314. path LIKE '".$uploadPath."/%/%htm%' AND
  6315. id = ".(int)$extra_info."
  6316. ORDER BY id ASC";
  6317. $res_hot = Database::query($sql_hot);
  6318. $row = Database::fetch_array($res_hot);
  6319. $item_title = $row['title'];
  6320. $item_description = $row['description'];
  6321. if (!empty ($row['comment'])) {
  6322. $item_title = $row['comment'];
  6323. }
  6324. } else {
  6325. $item_title = '';
  6326. $item_description = '';
  6327. }
  6328. if ($id != 0 && is_array($extra_info)) {
  6329. $parent = $extra_info['parent_item_id'];
  6330. } else {
  6331. $parent = 0;
  6332. }
  6333. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6334. $result = Database::query($sql);
  6335. $arrLP = array();
  6336. while ($row = Database :: fetch_array($result)) {
  6337. $arrLP[] = array(
  6338. 'id' => $row['id'],
  6339. 'item_type' => $row['item_type'],
  6340. 'title' => $row['title'],
  6341. 'path' => $row['path'],
  6342. 'description' => $row['description'],
  6343. 'parent_item_id' => $row['parent_item_id'],
  6344. 'previous_item_id' => $row['previous_item_id'],
  6345. 'next_item_id' => $row['next_item_id'],
  6346. 'display_order' => $row['display_order'],
  6347. 'max_score' => $row['max_score'],
  6348. 'min_score' => $row['min_score'],
  6349. 'mastery_score' => $row['mastery_score'],
  6350. 'prerequisite' => $row['prerequisite'],
  6351. 'max_time_allowed' => $row['max_time_allowed']
  6352. );
  6353. }
  6354. $legend = '<legend>';
  6355. if ($action == 'add') {
  6356. $legend .= get_lang('CreateTheExercise');
  6357. } elseif ($action == 'move') {
  6358. $legend .= get_lang('MoveTheCurrentExercise');
  6359. } else {
  6360. $legend .= get_lang('EditCurrentExecice');
  6361. }
  6362. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6363. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6364. }
  6365. $legend .= '</legend>';
  6366. $return .= '<form method="POST">';
  6367. $return .= $legend;
  6368. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6369. $return .= '<tr>';
  6370. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6371. $return .= '<td class="input">';
  6372. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6373. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6374. $arrHide = array(
  6375. $id
  6376. );
  6377. if (count($arrLP) > 0) {
  6378. for ($i = 0; $i < count($arrLP); $i++) {
  6379. if ($action != 'add') {
  6380. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6381. $arrLP[$i]['id'],
  6382. $arrHide
  6383. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6384. ) {
  6385. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6386. } else {
  6387. $arrHide[] = $arrLP[$i]['id'];
  6388. }
  6389. } else {
  6390. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6391. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6392. }
  6393. }
  6394. }
  6395. reset($arrLP);
  6396. }
  6397. $return .= '</select>';
  6398. $return .= '</td>';
  6399. $return .= '</tr>';
  6400. $return .= '<tr>';
  6401. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6402. $return .= '<td class="input">';
  6403. $return .= '<select id="previous" name="previous" size="1">';
  6404. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6405. for ($i = 0; $i < count($arrLP); $i++) {
  6406. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6407. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6408. $selected = 'selected="selected" ';
  6409. } elseif ($action == 'add') {
  6410. $selected = 'selected="selected" ';
  6411. } else {
  6412. $selected = '';
  6413. }
  6414. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6415. 'After'
  6416. ).' "'.$arrLP[$i]['title'].'"</option>';
  6417. }
  6418. }
  6419. $return .= '</select>';
  6420. $return .= '</td>';
  6421. $return .= '</tr>';
  6422. if ($action != 'move') {
  6423. $return .= '<tr>';
  6424. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6425. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6426. $return .= '</tr>';
  6427. $id_prerequisite = 0;
  6428. if (is_array($arrLP) && count($arrLP) > 0) {
  6429. foreach ($arrLP as $key => $value) {
  6430. if ($value['id'] == $id) {
  6431. $id_prerequisite = $value['prerequisite'];
  6432. break;
  6433. }
  6434. }
  6435. $arrHide = array();
  6436. for ($i = 0; $i < count($arrLP); $i++) {
  6437. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6438. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6439. $s_selected_position = $arrLP[$i]['id'];
  6440. } elseif ($action == 'add') {
  6441. $s_selected_position = 0;
  6442. }
  6443. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6444. }
  6445. }
  6446. }
  6447. }
  6448. $return .= '<tr>';
  6449. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang(
  6450. 'SaveHotpotatoes'
  6451. ).'</button></td>';
  6452. $return .= '</tr>';
  6453. $return .= '</table>';
  6454. if ($action == 'move') {
  6455. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6456. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6457. }
  6458. if (is_numeric($extra_info)) {
  6459. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6460. } elseif (is_array($extra_info)) {
  6461. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6462. }
  6463. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6464. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6465. $return .= '</form>';
  6466. return $return;
  6467. }
  6468. /**
  6469. * Return the form to display the forum edit/add option
  6470. * @param string Action (add/edit)
  6471. * @param integer ID of the lp_item if already exists
  6472. * @param mixed Forum ID or title
  6473. * @return string HTML form
  6474. */
  6475. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6476. {
  6477. $course_id = api_get_course_int_id();
  6478. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6479. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6480. if ($id != 0 && is_array($extra_info)) {
  6481. $item_title = stripslashes($extra_info['title']);
  6482. } elseif (is_numeric($extra_info)) {
  6483. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6484. FROM ".$tbl_forum."
  6485. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6486. $result = Database::query($sql_forum);
  6487. $row = Database :: fetch_array($result);
  6488. $item_title = $row['title'];
  6489. $item_description = $row['comment'];
  6490. } else {
  6491. $item_title = '';
  6492. $item_description = '';
  6493. }
  6494. $legend = '<legend>';
  6495. if ($id != 0 && is_array($extra_info)) {
  6496. $parent = $extra_info['parent_item_id'];
  6497. } else {
  6498. $parent = 0;
  6499. }
  6500. $sql = "SELECT * FROM ".$tbl_lp_item."
  6501. WHERE c_id = ".$course_id." AND
  6502. lp_id = ".$this->lp_id;
  6503. $result = Database::query($sql);
  6504. $arrLP = array();
  6505. while ($row = Database :: fetch_array($result)) {
  6506. $arrLP[] = array(
  6507. 'id' => $row['id'],
  6508. 'item_type' => $row['item_type'],
  6509. 'title' => $row['title'],
  6510. 'path' => $row['path'],
  6511. 'description' => $row['description'],
  6512. 'parent_item_id' => $row['parent_item_id'],
  6513. 'previous_item_id' => $row['previous_item_id'],
  6514. 'next_item_id' => $row['next_item_id'],
  6515. 'display_order' => $row['display_order'],
  6516. 'max_score' => $row['max_score'],
  6517. 'min_score' => $row['min_score'],
  6518. 'mastery_score' => $row['mastery_score'],
  6519. 'prerequisite' => $row['prerequisite']
  6520. );
  6521. }
  6522. $this->tree_array($arrLP);
  6523. $arrLP = $this->arrMenu;
  6524. unset ($this->arrMenu);
  6525. if ($action == 'add') {
  6526. $legend .= get_lang('CreateTheForum').'&nbsp;:';
  6527. } elseif ($action == 'move') {
  6528. $legend .= get_lang('MoveTheCurrentForum').'&nbsp;:';
  6529. } else {
  6530. $legend .= get_lang('EditCurrentForum').'&nbsp;:';
  6531. }
  6532. $legend .= '</legend>';
  6533. $return .= '<div class="sectioncomment">';
  6534. $return .= '<form method="POST">';
  6535. $return .= $legend;
  6536. $return .= '<table class="lp_form">';
  6537. if ($action != 'move') {
  6538. $return .= '<tr>';
  6539. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6540. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  6541. $return .= '</tr>';
  6542. }
  6543. $return .= '<tr>';
  6544. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6545. $return .= '<td class="input">';
  6546. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6547. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6548. $arrHide = array(
  6549. $id
  6550. );
  6551. //$parent_item_id = $_SESSION['parent_item_id'];
  6552. for ($i = 0; $i < count($arrLP); $i++) {
  6553. if ($action != 'add') {
  6554. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6555. $arrLP[$i]['id'],
  6556. $arrHide
  6557. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6558. ) {
  6559. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6560. } else {
  6561. $arrHide[] = $arrLP[$i]['id'];
  6562. }
  6563. } else {
  6564. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6565. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6566. }
  6567. }
  6568. }
  6569. if (is_array($arrLP)) {
  6570. reset($arrLP);
  6571. }
  6572. $return .= "\t\t\t\t".'</select>';
  6573. $return .= '</td>';
  6574. $return .= '</tr>';
  6575. $return .= '<tr>';
  6576. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6577. $return .= '<td class="input">';
  6578. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6579. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6580. for ($i = 0; $i < count($arrLP); $i++) {
  6581. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6582. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6583. $selected = 'selected="selected" ';
  6584. } elseif ($action == 'add') {
  6585. $selected = 'selected="selected" ';
  6586. } else {
  6587. $selected = '';
  6588. }
  6589. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6590. 'After'
  6591. ).' "'.$arrLP[$i]['title'].'"</option>';
  6592. }
  6593. }
  6594. $return .= "\t\t\t\t".'</select>';
  6595. $return .= '</td>';
  6596. $return .= '</tr>';
  6597. if ($action != 'move') {
  6598. $return .= '<tr>';
  6599. $return .= '</tr>';
  6600. $id_prerequisite = 0;
  6601. if (is_array($arrLP)) {
  6602. foreach ($arrLP as $key => $value) {
  6603. if ($value['id'] == $id) {
  6604. $id_prerequisite = $value['prerequisite'];
  6605. break;
  6606. }
  6607. }
  6608. }
  6609. $arrHide = array();
  6610. for ($i = 0; $i < count($arrLP); $i++) {
  6611. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6612. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6613. $s_selected_position = $arrLP[$i]['id'];
  6614. } elseif ($action == 'add') {
  6615. $s_selected_position = 0;
  6616. }
  6617. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6618. }
  6619. }
  6620. $return .= '</tr>';
  6621. }
  6622. $return .= '<tr>';
  6623. if ($action == 'add') {
  6624. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6625. 'AddForumToCourse'
  6626. ).' </button></td>';
  6627. } else {
  6628. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6629. 'EditCurrentForum'
  6630. ).' </button></td>';
  6631. }
  6632. $return .= '</tr>';
  6633. $return .= '</table>';
  6634. if ($action == 'move') {
  6635. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6636. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6637. }
  6638. if (is_numeric($extra_info)) {
  6639. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6640. } elseif (is_array($extra_info)) {
  6641. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6642. }
  6643. $return .= '<input name="type" type="hidden" value="'.TOOL_FORUM.'" />';
  6644. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6645. $return .= '</form>';
  6646. $return .= '</div>';
  6647. return $return;
  6648. }
  6649. /**
  6650. * Return HTML form to add/edit forum threads
  6651. * @param string Action (add/edit)
  6652. * @param integer Item ID if already exists in learning path
  6653. * @param mixed Extra information (thread ID if integer)
  6654. * @return string HTML form
  6655. */
  6656. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6657. {
  6658. $course_id = api_get_course_int_id();
  6659. if (empty($course_id)) {
  6660. return null;
  6661. }
  6662. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6663. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6664. if ($id != 0 && is_array($extra_info)) {
  6665. $item_title = stripslashes($extra_info['title']);
  6666. } elseif (is_numeric($extra_info)) {
  6667. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6668. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6669. $result = Database::query($sql_forum);
  6670. $row = Database :: fetch_array($result);
  6671. $item_title = $row['title'];
  6672. $item_description = '';
  6673. } else {
  6674. $item_title = '';
  6675. $item_description = '';
  6676. }
  6677. $return = null;
  6678. if ($id != 0 && is_array($extra_info)) {
  6679. $parent = $extra_info['parent_item_id'];
  6680. } else {
  6681. $parent = 0;
  6682. }
  6683. $sql = "SELECT * FROM ".$tbl_lp_item."
  6684. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6685. $result = Database::query($sql);
  6686. $arrLP = array();
  6687. while ($row = Database :: fetch_array($result)) {
  6688. $arrLP[] = array(
  6689. 'id' => $row['id'],
  6690. 'item_type' => $row['item_type'],
  6691. 'title' => $row['title'],
  6692. 'path' => $row['path'],
  6693. 'description' => $row['description'],
  6694. 'parent_item_id' => $row['parent_item_id'],
  6695. 'previous_item_id' => $row['previous_item_id'],
  6696. 'next_item_id' => $row['next_item_id'],
  6697. 'display_order' => $row['display_order'],
  6698. 'max_score' => $row['max_score'],
  6699. 'min_score' => $row['min_score'],
  6700. 'mastery_score' => $row['mastery_score'],
  6701. 'prerequisite' => $row['prerequisite']
  6702. );
  6703. }
  6704. $this->tree_array($arrLP);
  6705. $arrLP = $this->arrMenu;
  6706. unset ($this->arrMenu);
  6707. $return .= '<form method="POST">';
  6708. if ($action == 'add') {
  6709. $return .= '<legend>'.get_lang('CreateTheForum').'</legend>';
  6710. } elseif ($action == 'move') {
  6711. $return .= '<p class="lp_title">'.get_lang('MoveTheCurrentForum').'&nbsp;:</p>';
  6712. } else {
  6713. $return .= '<legend>'.get_lang('EditCurrentForum').'</legend>';
  6714. }
  6715. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6716. $return .= '<tr>';
  6717. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6718. $return .= '<td class="input">';
  6719. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6720. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6721. $arrHide = array(
  6722. $id
  6723. );
  6724. for ($i = 0; $i < count($arrLP); $i++) {
  6725. if ($action != 'add') {
  6726. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6727. $arrLP[$i]['id'],
  6728. $arrHide
  6729. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6730. ) {
  6731. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6732. } else {
  6733. $arrHide[] = $arrLP[$i]['id'];
  6734. }
  6735. } else {
  6736. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6737. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6738. }
  6739. }
  6740. }
  6741. if ($arrLP != null) {
  6742. reset($arrLP);
  6743. }
  6744. $return .= '</select>';
  6745. $return .= '</td>';
  6746. $return .= '</tr>';
  6747. $return .= '<tr>';
  6748. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6749. $return .= '<td class="input">';
  6750. $return .= "\t\t\t\t".'<select id="previous" name="previous" size="1">';
  6751. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6752. for ($i = 0; $i < count($arrLP); $i++) {
  6753. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6754. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6755. $selected = 'selected="selected" ';
  6756. } elseif ($action == 'add') {
  6757. $selected = 'selected="selected" ';
  6758. } else {
  6759. $selected = '';
  6760. }
  6761. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6762. 'After'
  6763. ).' "'.$arrLP[$i]['title'].'"</option>';
  6764. }
  6765. }
  6766. $return .= "\t\t\t\t".'</select>';
  6767. $return .= '</td>';
  6768. $return .= '</tr>';
  6769. if ($action != 'move') {
  6770. $return .= '<tr>';
  6771. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6772. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6773. $return .= '</tr>';
  6774. $return .= '<tr>';
  6775. $return .= '</tr>';
  6776. $id_prerequisite = 0;
  6777. if ($arrLP != null) {
  6778. foreach ($arrLP as $key => $value) {
  6779. if ($value['id'] == $id) {
  6780. $id_prerequisite = $value['prerequisite'];
  6781. break;
  6782. }
  6783. }
  6784. }
  6785. $arrHide = array();
  6786. for ($i = 0; $i < count($arrLP); $i++) {
  6787. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6788. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6789. $s_selected_position = $arrLP[$i]['id'];
  6790. } elseif ($action == 'add') {
  6791. $s_selected_position = 0;
  6792. }
  6793. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6794. }
  6795. }
  6796. $return .= '<tr>';
  6797. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang(
  6798. 'LearnpathPrerequisites'
  6799. ).'</label></td>';
  6800. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang(
  6801. 'NoPrerequisites'
  6802. ).'</option>';
  6803. foreach ($arrHide as $key => $value) {
  6804. if ($key == $s_selected_position && $action == 'add') {
  6805. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6806. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6807. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6808. } else {
  6809. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6810. }
  6811. }
  6812. $return .= "</select></td>";
  6813. $return .= '</tr>';
  6814. }
  6815. $return .= '<tr>';
  6816. $return .= '<td></td><td>
  6817. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang(
  6818. 'Ok'
  6819. ).'</button></td>';
  6820. $return .= '</tr>';
  6821. $return .= '</table>';
  6822. if ($action == 'move') {
  6823. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6824. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6825. }
  6826. if (is_numeric($extra_info)) {
  6827. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6828. } elseif (is_array($extra_info)) {
  6829. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6830. }
  6831. $return .= '<input name="type" type="hidden" value="'.TOOL_THREAD.'" />';
  6832. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6833. $return .= '</form>';
  6834. $return .= '</div>';
  6835. return $return;
  6836. }
  6837. /**
  6838. * Return the HTML form to display an item (generally a section/module item)
  6839. * @param string Item type (module/dokeos_module)
  6840. * @param string Title (optional, only when creating)
  6841. * @param string Action ('add'/'edit')
  6842. * @param integer lp_item ID
  6843. * @param mixed Extra info
  6844. * @return string HTML form
  6845. */
  6846. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6847. {
  6848. $course_id = api_get_course_int_id();
  6849. global $_course;
  6850. global $charset;
  6851. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6852. if ($id != 0 && is_array($extra_info)) {
  6853. $item_title = $extra_info['title'];
  6854. $item_description = $extra_info['description'];
  6855. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes(
  6856. $extra_info['path']
  6857. );
  6858. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6859. } else {
  6860. $item_title = '';
  6861. $item_description = '';
  6862. $item_path_fck = '';
  6863. }
  6864. if ($id != 0 && is_array($extra_info)) {
  6865. $parent = $extra_info['parent_item_id'];
  6866. } else {
  6867. $parent = 0;
  6868. }
  6869. $id = intval($id);
  6870. $sql = "SELECT * FROM ".$tbl_lp_item."
  6871. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND id != $id";
  6872. if ($item_type == 'module') {
  6873. $sql .= " AND parent_item_id = 0";
  6874. }
  6875. $result = Database::query($sql);
  6876. $arrLP = array();
  6877. while ($row = Database :: fetch_array($result)) {
  6878. $arrLP[] = array(
  6879. 'id' => $row['id'],
  6880. 'item_type' => $row['item_type'],
  6881. 'title' => $row['title'],
  6882. 'path' => $row['path'],
  6883. 'description' => $row['description'],
  6884. 'parent_item_id' => $row['parent_item_id'],
  6885. 'previous_item_id' => $row['previous_item_id'],
  6886. 'next_item_id' => $row['next_item_id'],
  6887. 'max_score' => $row['max_score'],
  6888. 'min_score' => $row['min_score'],
  6889. 'mastery_score' => $row['mastery_score'],
  6890. 'prerequisite' => $row['prerequisite'],
  6891. 'display_order' => $row['display_order']
  6892. );
  6893. }
  6894. $this->tree_array($arrLP);
  6895. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6896. unset ($this->arrMenu);
  6897. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6898. $url = api_get_self().'?'.api_get_cidreq(
  6899. ).'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6900. $form = new FormValidator('form', 'POST', $url);
  6901. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6902. $defaults['description'] = $item_description;
  6903. $form->addElement('header', $title);
  6904. //$arrHide = array($id);
  6905. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6906. $arrHide[0]['padding'] = 3;
  6907. $charset = api_get_system_encoding();
  6908. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6909. for ($i = 0; $i < count($arrLP); $i++) {
  6910. if ($action != 'add') {
  6911. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6912. $arrLP[$i]['id'],
  6913. $arrHide
  6914. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6915. ) {
  6916. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6917. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6918. if ($parent == $arrLP[$i]['id']) {
  6919. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6920. }
  6921. }
  6922. } else {
  6923. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6924. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6925. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6926. if ($parent == $arrLP[$i]['id']) {
  6927. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6928. }
  6929. }
  6930. }
  6931. }
  6932. if ($action != 'move') {
  6933. $form->addElement(
  6934. 'text',
  6935. 'title',
  6936. get_lang('Title'),
  6937. 'id="idTitle" class="learnpath_chapter_form" size="40%"'
  6938. );
  6939. $form->applyFilter('title', 'html_filter');
  6940. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6941. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6942. } else {
  6943. $form->addElement('hidden', 'title');
  6944. }
  6945. $parent_select = $form->addElement(
  6946. 'select',
  6947. 'parent',
  6948. get_lang('Parent'),
  6949. '',
  6950. 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"'
  6951. );
  6952. foreach ($arrHide as $key => $value) {
  6953. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6954. }
  6955. if (!empty($s_selected_parent)) {
  6956. $parent_select->setSelected($s_selected_parent);
  6957. }
  6958. }
  6959. if (is_array($arrLP)) {
  6960. reset($arrLP);
  6961. }
  6962. $arrHide = array();
  6963. // POSITION
  6964. for ($i = 0; $i < count($arrLP); $i++) {
  6965. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6966. //this is the same!
  6967. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6968. $s_selected_position = $arrLP[$i]['id'];
  6969. } elseif ($action == 'add') {
  6970. $s_selected_position = $arrLP[$i]['id'];
  6971. }
  6972. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6973. }
  6974. }
  6975. $position = $form->addElement(
  6976. 'select',
  6977. 'previous',
  6978. get_lang('Position'),
  6979. '',
  6980. 'id="previous" class="learnpath_chapter_form" style="width:37%;"'
  6981. );
  6982. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6983. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6984. foreach ($arrHide as $key => $value) {
  6985. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  6986. }
  6987. if (!empty ($s_selected_position)) {
  6988. $position->setSelected($s_selected_position);
  6989. }
  6990. if (is_array($arrLP)) {
  6991. reset($arrLP);
  6992. }
  6993. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6994. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6995. $form->addElement('hidden', 'parent', '0');
  6996. }
  6997. //fix in order to use the tab
  6998. if ($item_type == 'chapter') {
  6999. $form->addElement('hidden', 'type', 'chapter');
  7000. }
  7001. $extension = null;
  7002. if (!empty($item_path)) {
  7003. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  7004. }
  7005. //assets can't be modified
  7006. //$item_type == 'asset' ||
  7007. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7008. if ($item_type == 'sco') {
  7009. $form->addElement(
  7010. 'html',
  7011. '<script type="text/javascript">alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  7012. );
  7013. }
  7014. $renderer = $form->defaultRenderer();
  7015. $renderer->setElementTemplate(
  7016. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7017. 'content_lp'
  7018. );
  7019. $relative_prefix = '';
  7020. $editor_config = array(
  7021. 'ToolbarSet' => 'LearningPathDocuments',
  7022. 'Width' => '100%',
  7023. 'Height' => '500',
  7024. 'FullPage' => true,
  7025. 'CreateDocumentDir' => $relative_prefix,
  7026. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7027. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7028. );
  7029. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7030. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  7031. //$defaults['content_lp'] = file_get_contents($item_path);
  7032. $defaults['content_lp'] = file_get_contents($content_path);
  7033. }
  7034. $form->addElement('hidden', 'type', 'dokeos_'.$item_type);
  7035. $form->addElement('hidden', 'post_time', time());
  7036. $form->setDefaults($defaults);
  7037. return $form->return_form();
  7038. }
  7039. /**
  7040. * Returns the form to update or create a document
  7041. * @param string Action (add/edit)
  7042. * @param integer ID of the lp_item (if already exists)
  7043. * @param mixed Integer if document ID, string if info ('new')
  7044. * @return string HTML form
  7045. */
  7046. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7047. {
  7048. $course_id = api_get_course_int_id();
  7049. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7050. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7051. $no_display_edit_textarea = false;
  7052. //If action==edit document
  7053. //We don't display the document form if it's not an editable document (html or txt file)
  7054. if ($action == "edit") {
  7055. if (is_array($extra_info)) {
  7056. $path_parts = pathinfo($extra_info['dir']);
  7057. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7058. $no_display_edit_textarea = true;
  7059. }
  7060. }
  7061. }
  7062. $no_display_add = false;
  7063. // If action==add an existing document
  7064. // We don't display the document form if it's not an editable document (html or txt file).
  7065. if ($action == "add") {
  7066. if (is_numeric($extra_info)) {
  7067. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  7068. $extra_info
  7069. );
  7070. $result = Database::query($sql_doc);
  7071. $path_file = Database :: result($result, 0, 0);
  7072. $path_parts = pathinfo($path_file);
  7073. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  7074. $no_display_add = true;
  7075. }
  7076. }
  7077. }
  7078. if ($id != 0 && is_array($extra_info)) {
  7079. $item_title = stripslashes($extra_info['title']);
  7080. $item_description = stripslashes($extra_info['description']);
  7081. $item_terms = stripslashes($extra_info['terms']);
  7082. if (empty ($item_title)) {
  7083. $path_parts = pathinfo($extra_info['path']);
  7084. $item_title = stripslashes($path_parts['filename']);
  7085. }
  7086. } elseif (is_numeric($extra_info)) {
  7087. $sql_doc = "SELECT path, title FROM ".$tbl_doc."
  7088. WHERE c_id = ".$course_id." AND id = ".Database::escape_string($extra_info);
  7089. $result = Database::query($sql_doc);
  7090. $row = Database::fetch_array($result);
  7091. $explode = explode('.', $row['title']);
  7092. if (count($explode) > 1) {
  7093. for ($i = 0; $i < count($explode) - 1; $i++) {
  7094. $item_title .= $explode[$i];
  7095. }
  7096. } else {
  7097. $item_title = $row['title'];
  7098. }
  7099. $item_title = str_replace('_', ' ', $item_title);
  7100. if (empty ($item_title)) {
  7101. $path_parts = pathinfo($row['path']);
  7102. $item_title = stripslashes($path_parts['filename']);
  7103. }
  7104. } else {
  7105. $item_title = '';
  7106. $item_description = '';
  7107. }
  7108. $return = '<legend>';
  7109. if ($id != 0 && is_array($extra_info)) {
  7110. $parent = $extra_info['parent_item_id'];
  7111. } else {
  7112. $parent = 0;
  7113. }
  7114. $sql = "SELECT * FROM ".$tbl_lp_item."
  7115. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7116. $result = Database::query($sql);
  7117. $arrLP = array();
  7118. while ($row = Database :: fetch_array($result)) {
  7119. $arrLP[] = array(
  7120. 'id' => $row['id'],
  7121. 'item_type' => $row['item_type'],
  7122. 'title' => $row['title'],
  7123. 'path' => $row['path'],
  7124. 'description' => $row['description'],
  7125. 'parent_item_id' => $row['parent_item_id'],
  7126. 'previous_item_id' => $row['previous_item_id'],
  7127. 'next_item_id' => $row['next_item_id'],
  7128. 'display_order' => $row['display_order'],
  7129. 'max_score' => $row['max_score'],
  7130. 'min_score' => $row['min_score'],
  7131. 'mastery_score' => $row['mastery_score'],
  7132. 'prerequisite' => $row['prerequisite']
  7133. );
  7134. }
  7135. $this->tree_array($arrLP);
  7136. $arrLP = $this->arrMenu;
  7137. unset ($this->arrMenu);
  7138. if ($action == 'add') {
  7139. $return .= get_lang('CreateTheDocument');
  7140. } elseif ($action == 'move') {
  7141. $return .= get_lang('MoveTheCurrentDocument');
  7142. } else {
  7143. $return .= get_lang('EditTheCurrentDocument');
  7144. }
  7145. $return .= '</legend>';
  7146. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  7147. $return .= Display :: return_warning_message(
  7148. '<strong>'.get_lang('Warning').' !</strong><br />'.get_lang('WarningEditingDocument'),
  7149. false
  7150. );
  7151. }
  7152. $form = new FormValidator('form', 'POST', api_get_self(
  7153. ).'?'.$_SERVER['QUERY_STRING'], '', array('enctype' => "multipart/form-data"));
  7154. $defaults['title'] = Security :: remove_XSS($item_title);
  7155. if (empty($item_title)) {
  7156. $defaults['title'] = Security::remove_XSS($item_title);
  7157. }
  7158. $defaults['description'] = $item_description;
  7159. $form->addElement('html', $return);
  7160. if ($action != 'move') {
  7161. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  7162. $form->applyFilter('title', 'html_filter');
  7163. }
  7164. //$arrHide = array($id);
  7165. $arrHide[0]['value'] = $this->name;
  7166. $arrHide[0]['padding'] = 3;
  7167. for ($i = 0; $i < count($arrLP); $i++) {
  7168. if ($action != 'add') {
  7169. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7170. $arrLP[$i]['id'],
  7171. $arrHide
  7172. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7173. ) {
  7174. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7175. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7176. if ($parent == $arrLP[$i]['id']) {
  7177. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7178. }
  7179. }
  7180. } else {
  7181. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7182. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7183. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  7184. if ($parent == $arrLP[$i]['id']) {
  7185. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  7186. }
  7187. }
  7188. }
  7189. }
  7190. $parent_select = $form->addElement(
  7191. 'select',
  7192. 'parent',
  7193. get_lang('Parent'),
  7194. '',
  7195. 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"'
  7196. );
  7197. $my_count = 0;
  7198. foreach ($arrHide as $key => $value) {
  7199. if ($my_count != 0) {
  7200. // The LP name is also the first section and is not in the same charset like the other sections.
  7201. $value['value'] = Security :: remove_XSS($value['value']);
  7202. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7203. } else {
  7204. $value['value'] = Security :: remove_XSS($value['value']);
  7205. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  7206. }
  7207. $my_count++;
  7208. }
  7209. if (!empty($id)) {
  7210. $parent_select->setSelected($parent);
  7211. } else {
  7212. $parent_item_id = $_SESSION['parent_item_id'];
  7213. $parent_select->setSelected($parent_item_id);
  7214. }
  7215. if (is_array($arrLP)) {
  7216. reset($arrLP);
  7217. }
  7218. $arrHide = array();
  7219. //POSITION
  7220. for ($i = 0; $i < count($arrLP); $i++) {
  7221. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7222. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7223. $s_selected_position = $arrLP[$i]['id'];
  7224. } elseif ($action == 'add') {
  7225. $s_selected_position = $arrLP[$i]['id'];
  7226. }
  7227. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  7228. }
  7229. }
  7230. $position = $form->addElement(
  7231. 'select',
  7232. 'previous',
  7233. get_lang('Position'),
  7234. '',
  7235. 'id="previous" class="learnpath_item_form" style="width:40%;"'
  7236. );
  7237. $position->addOption(get_lang('FirstPosition'), 0);
  7238. foreach ($arrHide as $key => $value) {
  7239. $padding = isset($value['padding']) ? $value['padding'] : 0;
  7240. $position->addOption($value['value'], $key, 'style="padding-left:'.$padding.'px;"');
  7241. }
  7242. $position->setSelected($s_selected_position);
  7243. if (is_array($arrLP)) {
  7244. reset($arrLP);
  7245. }
  7246. if ($action != 'move') {
  7247. $id_prerequisite = 0;
  7248. if (is_array($arrLP)) {
  7249. foreach ($arrLP as $key => $value) {
  7250. if ($value['id'] == $id) {
  7251. $id_prerequisite = $value['prerequisite'];
  7252. break;
  7253. }
  7254. }
  7255. }
  7256. $arrHide = array();
  7257. for ($i = 0; $i < count($arrLP); $i++) {
  7258. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7259. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7260. $s_selected_position = $arrLP[$i]['id'];
  7261. } elseif ($action == 'add') {
  7262. $s_selected_position = $arrLP[$i]['id'];
  7263. }
  7264. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7265. }
  7266. }
  7267. if (!$no_display_add) {
  7268. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  7269. if (isset ($_POST['content'])) {
  7270. $content = stripslashes($_POST['content']);
  7271. } elseif (is_array($extra_info)) {
  7272. //If it's an html document or a text file
  7273. if (!$no_display_edit_textarea) {
  7274. $content = $this->display_document($extra_info['path'], false, false);
  7275. }
  7276. } elseif (is_numeric($extra_info)) {
  7277. $content = $this->display_document($extra_info, false, false);
  7278. } else {
  7279. $content = '';
  7280. }
  7281. if (!$no_display_edit_textarea) {
  7282. // We need to calculate here some specific settings for the online editor.
  7283. // The calculated settings work for documents in the Documents tool
  7284. // (on the root or in subfolders).
  7285. // For documents in native scorm packages it is unclear whether the
  7286. // online editor should be activated or not.
  7287. // A new document, it is in the root of the repository.
  7288. $relative_path = '';
  7289. $relative_prefix = '';
  7290. if (is_array($extra_info) && $extra_info != 'new') {
  7291. // The document already exists. Whe have to determine its relative path towards the repository root.
  7292. $relative_path = explode('/', $extra_info['dir']);
  7293. $cnt = count($relative_path) - 2;
  7294. if ($cnt < 0) {
  7295. $cnt = 0;
  7296. }
  7297. $relative_prefix = str_repeat('../', $cnt);
  7298. $relative_path = array_slice($relative_path, 1, $cnt);
  7299. $relative_path = implode('/', $relative_path);
  7300. if (strlen($relative_path) > 0) {
  7301. $relative_path = $relative_path.'/';
  7302. }
  7303. } else {
  7304. global $_course;
  7305. $result = $this->generate_lp_folder($_course);
  7306. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  7307. $relative_prefix = '../../';
  7308. }
  7309. $editor_config = array(
  7310. 'ToolbarSet' => 'LearningPathDocuments',
  7311. 'Width' => '100%',
  7312. 'Height' => '500',
  7313. 'FullPage' => true,
  7314. 'CreateDocumentDir' => $relative_prefix,
  7315. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/',
  7316. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path(
  7317. ).'/document/'.$relative_path
  7318. );
  7319. if ($_GET['action'] == 'add_item') {
  7320. $class = 'add';
  7321. $text = get_lang('LPCreateDocument');
  7322. } else {
  7323. if ($_GET['action'] == 'edit_item') {
  7324. $class = 'save';
  7325. $text = get_lang('SaveDocument');
  7326. }
  7327. }
  7328. $form->addElement('style_submit_button', 'submit_button', $text, 'class="'.$class.'"');
  7329. $renderer = $form->defaultRenderer();
  7330. $renderer->setElementTemplate(
  7331. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7332. 'content_lp'
  7333. );
  7334. $form->addElement('html', '<div>');
  7335. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7336. $form->addElement('html', '</div>');
  7337. $defaults['content_lp'] = $content;
  7338. }
  7339. } elseif (is_numeric($extra_info)) {
  7340. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7341. $return = $this->display_document($extra_info, true, true, true);
  7342. $form->addElement('html', $return);
  7343. }
  7344. }
  7345. }
  7346. if ($action == 'move') {
  7347. $form->addElement('hidden', 'title', $item_title);
  7348. $form->addElement('hidden', 'description', $item_description);
  7349. }
  7350. if (is_numeric($extra_info)) {
  7351. $form->addElement(
  7352. 'style_submit_button',
  7353. 'submit_button',
  7354. get_lang('SaveDocument'),
  7355. 'value="submit_button", class="save"'
  7356. );
  7357. $form->addElement('hidden', 'path', $extra_info);
  7358. } elseif (is_array($extra_info)) {
  7359. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  7360. $form->addElement('hidden', 'path', $extra_info['path']);
  7361. }
  7362. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  7363. $form->addElement('hidden', 'post_time', time());
  7364. $form->setDefaults($defaults);
  7365. return $form->return_form();
  7366. }
  7367. /**
  7368. * Return HTML form to add/edit a link item
  7369. * @param string Action (add/edit)
  7370. * @param integer Item ID if exists
  7371. * @param mixed Extra info
  7372. * @return string HTML form
  7373. */
  7374. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  7375. {
  7376. $course_id = api_get_course_int_id();
  7377. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7378. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7379. if ($id != 0 && is_array($extra_info)) {
  7380. $item_title = stripslashes($extra_info['title']);
  7381. $item_description = stripslashes($extra_info['description']);
  7382. $item_url = stripslashes($extra_info['url']);
  7383. } elseif (is_numeric($extra_info)) {
  7384. $sql_link = "SELECT title, description, url FROM ".$tbl_link." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7385. $result = Database::query($sql_link);
  7386. $row = Database :: fetch_array($result);
  7387. $item_title = $row['title'];
  7388. $item_description = $row['description'];
  7389. $item_url = $row['url'];
  7390. } else {
  7391. $item_title = '';
  7392. $item_description = '';
  7393. $item_url = '';
  7394. }
  7395. $legend = '<legend>';
  7396. if ($id != 0 && is_array($extra_info)) {
  7397. $parent = $extra_info['parent_item_id'];
  7398. } else {
  7399. $parent = 0;
  7400. }
  7401. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7402. $result = Database::query($sql);
  7403. $arrLP = array();
  7404. while ($row = Database :: fetch_array($result)) {
  7405. $arrLP[] = array(
  7406. 'id' => $row['id'],
  7407. 'item_type' => $row['item_type'],
  7408. 'title' => $row['title'],
  7409. 'path' => $row['path'],
  7410. 'description' => $row['description'],
  7411. 'parent_item_id' => $row['parent_item_id'],
  7412. 'previous_item_id' => $row['previous_item_id'],
  7413. 'next_item_id' => $row['next_item_id'],
  7414. 'display_order' => $row['display_order'],
  7415. 'max_score' => $row['max_score'],
  7416. 'min_score' => $row['min_score'],
  7417. 'mastery_score' => $row['mastery_score'],
  7418. 'prerequisite' => $row['prerequisite']
  7419. );
  7420. }
  7421. $this->tree_array($arrLP);
  7422. $arrLP = $this->arrMenu;
  7423. unset ($this->arrMenu);
  7424. if ($action == 'add') {
  7425. $legend .= get_lang('CreateTheLink').'&nbsp;:';
  7426. } elseif ($action == 'move') {
  7427. $legend .= get_lang('MoveCurrentLink').'&nbsp;:';
  7428. } else {
  7429. $legend .= get_lang('EditCurrentLink').'&nbsp;:';
  7430. }
  7431. $legend .= '</legend>';
  7432. $return .= '<div class="sectioncomment">';
  7433. $return .= '<form method="POST">';
  7434. $return .= $legend;
  7435. $return .= '<table>';
  7436. if ($action != 'move') {
  7437. $return .= '<tr>';
  7438. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7439. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form"/></td>';
  7440. $return .= '</tr>';
  7441. }
  7442. $return .= '<tr>';
  7443. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7444. $return .= '<td class="input">';
  7445. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7446. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7447. $arrHide = array(
  7448. $id
  7449. );
  7450. $parent_item_id = $_SESSION['parent_item_id'];
  7451. for ($i = 0; $i < count($arrLP); $i++) {
  7452. if ($action != 'add') {
  7453. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7454. $arrLP[$i]['id'],
  7455. $arrHide
  7456. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7457. ) {
  7458. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7459. } else {
  7460. $arrHide[] = $arrLP[$i]['id'];
  7461. }
  7462. } else {
  7463. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7464. $return .= '<option '.(($parent_item_id == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7465. }
  7466. }
  7467. }
  7468. if (is_array($arrLP)) {
  7469. reset($arrLP);
  7470. }
  7471. $return .= '</select>';
  7472. $return .= '</td>';
  7473. $return .= '</tr>';
  7474. $return .= '<tr>';
  7475. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7476. $return .= '<td class="input">';
  7477. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7478. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7479. for ($i = 0; $i < count($arrLP); $i++) {
  7480. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  7481. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7482. $selected = 'selected="selected" ';
  7483. } elseif ($action == 'add') {
  7484. $selected = 'selected="selected" ';
  7485. } else {
  7486. $selected = '';
  7487. }
  7488. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7489. 'After'
  7490. ).' "'.$arrLP[$i]['title'].'"</option>';
  7491. }
  7492. }
  7493. $return .= '</select>';
  7494. $return .= '</td>';
  7495. $return .= '</tr>';
  7496. if ($action != 'move') {
  7497. $return .= '<tr>';
  7498. $return .= '<td class="label"><label for="idURL">'.get_lang('Url').'</label></td>';
  7499. $return .= '<td class="input"><input'.(is_numeric(
  7500. $extra_info
  7501. ) ? ' disabled="disabled"' : '').' id="idURL" name="url" style="width:99%;" type="text" value="'.$item_url.'" class="learnpath_item_form" /></td>';
  7502. $return .= '</tr>';
  7503. $id_prerequisite = 0;
  7504. if (is_array($arrLP)) {
  7505. foreach ($arrLP as $key => $value) {
  7506. if ($value['id'] == $id) {
  7507. $id_prerequisite = $value['prerequisite'];
  7508. break;
  7509. }
  7510. }
  7511. }
  7512. $arrHide = array();
  7513. for ($i = 0; $i < count($arrLP); $i++) {
  7514. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7515. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7516. $s_selected_position = $arrLP[$i]['id'];
  7517. } elseif ($action == 'add') {
  7518. $s_selected_position = 0;
  7519. }
  7520. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7521. }
  7522. }
  7523. $return .= '</tr>';
  7524. }
  7525. $return .= '<tr>';
  7526. if ($action == 'add') {
  7527. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7528. 'AddLinkToCourse'
  7529. ).'</button></td>';
  7530. } else {
  7531. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7532. 'EditCurrentLink'
  7533. ).'</button></td>';
  7534. }
  7535. $return .= '</tr>';
  7536. $return .= '</table>';
  7537. if ($action == 'move') {
  7538. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7539. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7540. }
  7541. if (is_numeric($extra_info)) {
  7542. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7543. } elseif (is_array($extra_info)) {
  7544. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7545. }
  7546. $return .= '<input name="type" type="hidden" value="'.TOOL_LINK.'" />';
  7547. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7548. $return .= '</form>';
  7549. $return .= '</div>';
  7550. return $return;
  7551. }
  7552. /**
  7553. * Return HTML form to add/edit a student publication (work)
  7554. * @param string Action (add/edit)
  7555. * @param integer Item ID if already exists
  7556. * @param mixed Extra info (work ID if integer)
  7557. * @return string HTML form
  7558. */
  7559. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7560. {
  7561. $course_id = api_get_course_int_id();
  7562. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7563. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7564. if ($id != 0 && is_array($extra_info)) {
  7565. $item_title = stripslashes($extra_info['title']);
  7566. $item_description = stripslashes($extra_info['description']);
  7567. } elseif (is_numeric($extra_info)) {
  7568. $sql_publication = "SELECT title, description FROM ".$tbl_publication."
  7569. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7570. $result = Database::query($sql_publication);
  7571. $row = Database :: fetch_array($result);
  7572. $item_title = $row['title'];
  7573. } else {
  7574. $item_title = get_lang('Student_publication');
  7575. }
  7576. $legend = '<legend>';
  7577. if ($id != 0 && is_array($extra_info)) {
  7578. $parent = $extra_info['parent_item_id'];
  7579. } else {
  7580. $parent = 0;
  7581. }
  7582. $sql = "SELECT * FROM ".$tbl_lp_item."
  7583. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7584. $result = Database::query($sql);
  7585. $arrLP = array();
  7586. while ($row = Database :: fetch_array($result)) {
  7587. $arrLP[] = array(
  7588. 'id' => $row['id'],
  7589. 'item_type' => $row['item_type'],
  7590. 'title' => $row['title'],
  7591. 'path' => $row['path'],
  7592. 'description' => $row['description'],
  7593. 'parent_item_id' => $row['parent_item_id'],
  7594. 'previous_item_id' => $row['previous_item_id'],
  7595. 'next_item_id' => $row['next_item_id'],
  7596. 'display_order' => $row['display_order'],
  7597. 'max_score' => $row['max_score'],
  7598. 'min_score' => $row['min_score'],
  7599. 'mastery_score' => $row['mastery_score'],
  7600. 'prerequisite' => $row['prerequisite']
  7601. );
  7602. }
  7603. $this->tree_array($arrLP);
  7604. $arrLP = $this->arrMenu;
  7605. unset ($this->arrMenu);
  7606. if ($action == 'add') {
  7607. $legend .= get_lang('Student_publication').'&nbsp;:'."\n";
  7608. } elseif ($action == 'move') {
  7609. $legend .= get_lang('MoveCurrentStudentPublication').'&nbsp;:'."\n";
  7610. } else {
  7611. $legend .= get_lang('EditCurrentStudentPublication').'&nbsp;:'."\n";
  7612. }
  7613. $legend .= '</legend>';
  7614. $return .= '<div class="sectioncomment">';
  7615. $return .= '<form method="POST">';
  7616. $return .= $legend;
  7617. $return .= '<table class="lp_form">';
  7618. if ($action != 'move') {
  7619. $return .= '<tr>';
  7620. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7621. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  7622. $return .= '</tr>';
  7623. }
  7624. $return .= '<tr>';
  7625. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7626. $return .= '<td class="input">';
  7627. $return .= "\t\t\t\t".'<select id="idParent" name="parent" style="width:100%;" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7628. //$parent_item_id = $_SESSION['parent_item_id'];
  7629. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7630. $arrHide = array(
  7631. $id
  7632. );
  7633. for ($i = 0; $i < count($arrLP); $i++) {
  7634. if ($action != 'add') {
  7635. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7636. $arrLP[$i]['id'],
  7637. $arrHide
  7638. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7639. ) {
  7640. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7641. } else {
  7642. $arrHide[] = $arrLP[$i]['id'];
  7643. }
  7644. } else {
  7645. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7646. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7647. }
  7648. }
  7649. }
  7650. if (is_array($arrLP)) {
  7651. reset($arrLP);
  7652. }
  7653. $return .= "\t\t\t\t".'</select>';
  7654. $return .= '</td>';
  7655. $return .= '</tr>';
  7656. $return .= '<tr>';
  7657. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7658. $return .= '<td class="input">';
  7659. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7660. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7661. for ($i = 0; $i < count($arrLP); $i++) {
  7662. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7663. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7664. $selected = 'selected="selected" ';
  7665. } elseif ($action == 'add') {
  7666. $selected = 'selected="selected" ';
  7667. } else {
  7668. $selected = '';
  7669. }
  7670. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7671. 'After'
  7672. ).' "'.$arrLP[$i]['title'].'"</option>';
  7673. }
  7674. }
  7675. $return .= "\t\t\t\t".'</select>';
  7676. $return .= '</td>';
  7677. $return .= '</tr>';
  7678. if ($action != 'move') {
  7679. $id_prerequisite = 0;
  7680. if (is_array($arrLP)) {
  7681. foreach ($arrLP as $key => $value) {
  7682. if ($value['id'] == $id) {
  7683. $id_prerequisite = $value['prerequisite'];
  7684. break;
  7685. }
  7686. }
  7687. }
  7688. $arrHide = array();
  7689. for ($i = 0; $i < count($arrLP); $i++) {
  7690. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7691. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7692. $s_selected_position = $arrLP[$i]['id'];
  7693. } elseif ($action == 'add') {
  7694. $s_selected_position = 0;
  7695. }
  7696. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7697. }
  7698. }
  7699. // Commented the prerequisites, only visible in edit (work).
  7700. /*
  7701. $return .= '<tr>';
  7702. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7703. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7704. foreach($arrHide as $key => $value) {
  7705. if ($key == $s_selected_position && $action == 'add') {
  7706. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7707. }
  7708. elseif ($key == $id_prerequisite && $action == 'edit') {
  7709. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7710. }
  7711. else {
  7712. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7713. }
  7714. }
  7715. $return .= "</select></td>";
  7716. */
  7717. $return .= '</tr>';
  7718. }
  7719. $return .= '<tr>';
  7720. if ($action == 'add') {
  7721. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7722. 'AddAssignmentToCourse'
  7723. ).'</button></td>';
  7724. } else {
  7725. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7726. 'EditCurrentStudentPublication'
  7727. ).'</button></td>';
  7728. }
  7729. $return .= '</tr>';
  7730. $return .= '</table>';
  7731. if ($action == 'move') {
  7732. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7733. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7734. }
  7735. if (is_numeric($extra_info)) {
  7736. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7737. } elseif (is_array($extra_info)) {
  7738. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7739. }
  7740. $return .= '<input name="type" type="hidden" value="'.TOOL_STUDENTPUBLICATION.'" />';
  7741. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7742. $return .= '</form>';
  7743. $return .= '</div>';
  7744. return $return;
  7745. }
  7746. /**
  7747. * Displays the menu for manipulating a step
  7748. * @return string html
  7749. */
  7750. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7751. {
  7752. $course_id = api_get_course_int_id();
  7753. $course_code = api_get_course_id();
  7754. global $_course;
  7755. $return = '<div class="actions">';
  7756. switch ($item_type) {
  7757. case 'dokeos_chapter' :
  7758. case 'chapter' :
  7759. // Commented the message cause should not show it.
  7760. //$lang = get_lang('TitleManipulateChapter');
  7761. break;
  7762. case 'dokeos_module' :
  7763. case 'module' :
  7764. // Commented the message cause should not show it.
  7765. //$lang = get_lang('TitleManipulateModule');
  7766. break;
  7767. case TOOL_DOCUMENT :
  7768. // Commented the message cause should not show it.
  7769. //$lang = get_lang('TitleManipulateDocument');
  7770. break;
  7771. case TOOL_LINK :
  7772. case 'link' :
  7773. // Commented the message cause should not show it.
  7774. //$lang = get_lang('TitleManipulateLink');
  7775. break;
  7776. case TOOL_QUIZ :
  7777. // Commented the message cause should not show it.
  7778. //$lang = get_lang('TitleManipulateQuiz');
  7779. break;
  7780. case TOOL_STUDENTPUBLICATION :
  7781. // Commented the message cause should not show it.
  7782. //$lang = get_lang('TitleManipulateStudentPublication');
  7783. break;
  7784. }
  7785. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7786. $item_id = intval($item_id);
  7787. $sql = "SELECT * FROM ".$tbl_lp_item." as lp WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7788. $result = Database::query($sql);
  7789. $row = Database::fetch_assoc($result);
  7790. $audio_player = null;
  7791. // We display an audio player if needed.
  7792. if (!empty($row['audio'])) {
  7793. $audio_player .= '<div class="lp_mediaplayer" id="container"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
  7794. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7795. $audio_player .= '<script>
  7796. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7797. s1.addParam("allowscriptaccess","always");
  7798. s1.addParam("flashvars","file=../../courses/'.$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7799. s1.write("container");
  7800. </script>';
  7801. }
  7802. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  7803. $_GET['cidReq']
  7804. ).'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7805. $return .= Display::url(
  7806. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7807. $url.'&action=edit_item&path_item='.$row['path']
  7808. );
  7809. $return .= Display::url(
  7810. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7811. $url.'&action=move_item'
  7812. );
  7813. // Commented for now as prerequisites cannot be added to chapters.
  7814. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7815. $return .= Display::url(
  7816. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7817. $url.'&action=edit_item_prereq'
  7818. );
  7819. }
  7820. $return .= Display::url(
  7821. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7822. $url.'&action=delete_item'
  7823. );
  7824. if ($item_type == TOOL_HOTPOTATOES) {
  7825. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7826. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7827. }
  7828. if ($item_type == TOOL_DOCUMENT) {
  7829. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7830. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7831. }
  7832. $return .= '</div>';
  7833. if (!empty($audio_player)) {
  7834. $return .= '<br />'.$audio_player;
  7835. }
  7836. return $return;
  7837. }
  7838. /**
  7839. * Creates the javascript needed for filling up the checkboxes without page reload
  7840. *
  7841. * @return string
  7842. */
  7843. public function get_js_dropdown_array()
  7844. {
  7845. $course_id = api_get_course_int_id();
  7846. $return = 'var child_name = new Array();'."\n";
  7847. $return .= 'var child_value = new Array();'."\n\n";
  7848. $return .= 'child_name[0] = new Array();'."\n";
  7849. $return .= 'child_value[0] = new Array();'."\n\n";
  7850. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7851. $sql_zero = "SELECT * FROM ".$tbl_lp_item."
  7852. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7853. ORDER BY display_order ASC";
  7854. $res_zero = Database::query($sql_zero);
  7855. $i = 0;
  7856. while ($row_zero = Database :: fetch_array($res_zero)) {
  7857. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7858. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7859. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7860. }
  7861. $return .= "\n";
  7862. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7863. $res = Database::query($sql);
  7864. while ($row = Database :: fetch_array($res)) {
  7865. $sql_parent = "
  7866. SELECT * FROM ".$tbl_lp_item."
  7867. WHERE c_id = ".$course_id." AND parent_item_id = ".$row['id']."
  7868. ORDER BY display_order ASC";
  7869. $res_parent = Database::query($sql_parent);
  7870. $i = 0;
  7871. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7872. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7873. while ($row_parent = Database :: fetch_array($res_parent)) {
  7874. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7875. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7876. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7877. }
  7878. $return .= "\n";
  7879. }
  7880. return $return;
  7881. }
  7882. /**
  7883. * Display the form to allow moving an item
  7884. * @param integer Item ID
  7885. * @return string HTML form
  7886. */
  7887. public function display_move_item($item_id)
  7888. {
  7889. $course_id = api_get_course_int_id();
  7890. $return = '';
  7891. if (is_numeric($item_id)) {
  7892. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7893. $sql = "SELECT * FROM ".$tbl_lp_item."
  7894. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7895. $res = Database::query($sql);
  7896. $row = Database :: fetch_array($res);
  7897. switch ($row['item_type']) {
  7898. case 'dokeos_chapter' :
  7899. case 'dir' :
  7900. case 'asset' :
  7901. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7902. $return .= $this->display_item_form(
  7903. $row['item_type'],
  7904. get_lang('MoveCurrentChapter'),
  7905. 'move',
  7906. $item_id,
  7907. $row
  7908. );
  7909. break;
  7910. case 'dokeos_module' :
  7911. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7912. $return .= $this->display_item_form(
  7913. $row['item_type'],
  7914. 'Move th current module:',
  7915. 'move',
  7916. $item_id,
  7917. $row
  7918. );
  7919. break;
  7920. case TOOL_DOCUMENT :
  7921. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7922. $return .= $this->display_document_form('move', $item_id, $row);
  7923. break;
  7924. case TOOL_LINK :
  7925. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7926. $return .= $this->display_link_form('move', $item_id, $row);
  7927. break;
  7928. case TOOL_HOTPOTATOES :
  7929. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7930. $return .= $this->display_link_form('move', $item_id, $row);
  7931. break;
  7932. case TOOL_QUIZ :
  7933. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7934. $return .= $this->display_quiz_form('move', $item_id, $row);
  7935. break;
  7936. case TOOL_STUDENTPUBLICATION :
  7937. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7938. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7939. break;
  7940. case TOOL_FORUM :
  7941. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7942. $return .= $this->display_forum_form('move', $item_id, $row);
  7943. break;
  7944. case TOOL_THREAD :
  7945. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7946. $return .= $this->display_forum_form('move', $item_id, $row);
  7947. break;
  7948. }
  7949. }
  7950. return $return;
  7951. }
  7952. /**
  7953. * Displays a basic form on the overview page for changing the item title and the item description.
  7954. * @param string $item_type
  7955. * @param string $title
  7956. * @param array $data
  7957. * @return string
  7958. */
  7959. public function display_item_small_form($item_type, $title = '', $data = array())
  7960. {
  7961. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7962. $form = new FormValidator('small_form', 'post', $url);
  7963. $form->addElement('header', $title);
  7964. $form->addElement('text', 'title', get_lang('Title'));
  7965. $form->addElement('button', 'submit_button', get_lang('Save'));
  7966. $form->addElement('hidden', 'id', $data['id']);
  7967. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7968. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7969. $form->setDefaults(array('title' => $data['title']));
  7970. return $form->toHtml();
  7971. }
  7972. /**
  7973. * Return HTML form to allow prerequisites selection
  7974. * @param integer Item ID
  7975. * @return string HTML form
  7976. */
  7977. public function display_item_prerequisites_form($item_id)
  7978. {
  7979. $course_id = api_get_course_int_id();
  7980. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7981. $item_id = intval($item_id);
  7982. /* Current prerequisite */
  7983. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = ".$item_id;
  7984. $result = Database::query($sql);
  7985. $row = Database::fetch_array($result);
  7986. $preq_id = $row['prerequisite'];
  7987. $return = '<legend>';
  7988. $return .= get_lang('AddEditPrerequisites');
  7989. $return .= '</legend>';
  7990. $return .= '<form method="POST">';
  7991. $return .= '<table class="data_table">';
  7992. $return .= '<tr>';
  7993. $return .= '<th height="24">'.get_lang('LearnpathPrerequisites').'</th>';
  7994. $return .= '<th width="70" height="24">'.get_lang('Minimum').'</th>';
  7995. $return .= '<th width="70" height="24">'.get_lang('Maximum').'</th>';
  7996. $return .= '</tr>';
  7997. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7998. $return .= '<tr >';
  7999. $return .= '<td colspan="3" class="radio">';
  8000. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  8001. $return .= '<label for="idNone">'.get_lang('None').'</label>';
  8002. $return .= '</tr>';
  8003. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  8004. $result = Database::query($sql);
  8005. $arrLP = array();
  8006. while ($row = Database :: fetch_array($result)) {
  8007. $arrLP[] = array(
  8008. 'id' => $row['id'],
  8009. 'item_type' => $row['item_type'],
  8010. 'title' => $row['title'],
  8011. 'ref' => $row['ref'],
  8012. 'description' => $row['description'],
  8013. 'parent_item_id' => $row['parent_item_id'],
  8014. 'previous_item_id' => $row['previous_item_id'],
  8015. 'next_item_id' => $row['next_item_id'],
  8016. 'max_score' => $row['max_score'],
  8017. 'min_score' => $row['min_score'],
  8018. 'mastery_score' => $row['mastery_score'],
  8019. 'prerequisite' => $row['prerequisite'],
  8020. 'next_item_id' => $row['next_item_id'],
  8021. 'display_order' => $row['display_order']
  8022. );
  8023. if ($row['ref'] == $preq_id) {
  8024. $preq_mastery = $row['mastery_score'];
  8025. $preq_max = $row['max_score'];
  8026. }
  8027. }
  8028. $this->tree_array($arrLP);
  8029. $arrLP = $this->arrMenu;
  8030. unset ($this->arrMenu);
  8031. for ($i = 0; $i < count($arrLP); $i++) {
  8032. if ($arrLP[$i]['id'] == $item_id) {
  8033. break;
  8034. }
  8035. $return .= '<tr>';
  8036. $return .= '<td class="radio"'.(($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8037. $return .= '<label for="id'.$arrLP[$i]['id'].'">';
  8038. $depth_multi = $arrLP[$i]['depth'] * 10;
  8039. $return .= '<input'.(($arrLP[$i]['id'] == $preq_id) ? ' checked="checked" ' : '').(($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter') ? ' disabled="disabled" ' : ' ').'id="id'.$arrLP[$i]['id'].'" name="prerequisites" style="margin-left:'.$depth_multi.'px; margin-right:10px;" type="radio" value="'.$arrLP[$i]['id'].'" />';
  8040. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  8041. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8042. $return .= '<img alt="" src="../img/lp_'.$icon_name.'.png" style="margin-right:5px;" title="" />';
  8043. } else {
  8044. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  8045. $return .= '<img alt="" src="../img/lp_'.$icon_name.'.gif" style="margin-right:5px;" title="" />';
  8046. } else {
  8047. $return .= Display::return_icon(
  8048. 'folder_document.gif',
  8049. '',
  8050. array('style' => 'margin-right:5px;')
  8051. );
  8052. }
  8053. }
  8054. $return .= $arrLP[$i]['title'].'</label>';
  8055. $return .= '</td>';
  8056. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  8057. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8058. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8059. $return .= '</td>';
  8060. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8061. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8062. $return .= '</td>';
  8063. }
  8064. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  8065. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8066. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8067. $return .= '</td>';
  8068. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8069. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8070. $return .= '</td>';
  8071. }
  8072. $return .= '</tr>';
  8073. }
  8074. $return .= '<tr>';
  8075. $return .= '</tr>';
  8076. $return .= '</table>';
  8077. $return .= '<div style="padding-top:3px;">';
  8078. $return .= '<button class="save" name="submit_button" type="submit">'.get_lang(
  8079. 'ModifyPrerequisites'
  8080. ).'</button>';
  8081. $return .= '</form>';
  8082. return $return;
  8083. }
  8084. /**
  8085. * Return HTML list to allow prerequisites selection for lp
  8086. * @param integer Item ID
  8087. * @return string HTML form
  8088. */
  8089. public function display_lp_prerequisites_list()
  8090. {
  8091. $course_id = api_get_course_int_id();
  8092. $lp_id = $this->lp_id;
  8093. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  8094. // get current prerequisite
  8095. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8096. $result = Database::query($sql);
  8097. $row = Database :: fetch_array($result);
  8098. $preq_id = $row['prerequisite'];
  8099. $session_id = api_get_session_id();
  8100. $session_condition = api_get_session_condition($session_id);
  8101. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  8102. $rs = Database::query($sql);
  8103. $return = '';
  8104. $return .= '<select name="prerequisites" >';
  8105. $return .= '<option value="0">'.get_lang('None').'</option>';
  8106. if (Database::num_rows($rs) > 0) {
  8107. while ($row = Database::fetch_array($rs)) {
  8108. if ($row['id'] == $lp_id) {
  8109. continue;
  8110. }
  8111. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $preq_id) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8112. }
  8113. }
  8114. $return .= '</select>';
  8115. return $return;
  8116. }
  8117. /**
  8118. * Creates a list with all the documents in it
  8119. * @return string
  8120. */
  8121. public function get_documents()
  8122. {
  8123. $course_info = api_get_course_info();
  8124. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  8125. return $document_tree;
  8126. }
  8127. /**
  8128. * Creates a list with all the exercises (quiz) in it
  8129. * @return string
  8130. */
  8131. public function get_exercises()
  8132. {
  8133. $course_id = api_get_course_int_id();
  8134. // New for hotpotatoes.
  8135. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8136. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8137. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8138. $session_id = api_get_session_id();
  8139. $condition_session = api_get_session_condition($session_id);
  8140. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  8141. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC";
  8142. $res_quiz = Database::query($sql_quiz);
  8143. $res_hot = Database::query($sql_hot);
  8144. $return = '<ul class="lp_resource">';
  8145. $return .= '<li class="lp_resource_element">';
  8146. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  8147. $return .= '<a href="'.api_get_path(
  8148. REL_CODE_PATH
  8149. ).'exercice/exercise_admin.php?lp_id='.$this->lp_id.'">'.get_lang('NewExercise').'</a>';
  8150. $return .= '</li>';
  8151. // Display hotpotatoes
  8152. while ($row_hot = Database :: fetch_array($res_hot)) {
  8153. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8154. $return .= '<a class="moved" href="#">';
  8155. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8156. $return .= '</a> ';
  8157. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  8158. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8159. ).'&amp;action=add_item&amp;type='.TOOL_HOTPOTATOES.'&amp;file='.$row_hot['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8160. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS(
  8161. $row_hot['title']
  8162. )).'</a>';
  8163. $return .= '</li>';
  8164. }
  8165. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8166. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8167. $return .= '<a class="moved" href="#">';
  8168. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8169. $return .= '</a> ';
  8170. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  8171. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8172. ).'&amp;action=add_item&amp;type='.TOOL_QUIZ.'&amp;file='.$row_quiz['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8173. Security :: remove_XSS(Text::cut($row_quiz['title'], 80)).
  8174. '</a>';
  8175. $return .= '</li>';
  8176. }
  8177. $return .= '</ul>';
  8178. return $return;
  8179. }
  8180. /**
  8181. * Creates a list with all the links in it
  8182. * @return string
  8183. */
  8184. public function get_links()
  8185. {
  8186. $course_id = api_get_course_int_id();
  8187. $tbl_link = Database :: get_course_table(TABLE_LINK);
  8188. $session_id = api_get_session_id();
  8189. $condition_session = api_get_session_condition($session_id);
  8190. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  8191. $res_link = Database::query($sql_link);
  8192. $return = '<ul class="lp_resource">';
  8193. $return .= '<li class="lp_resource_element">';
  8194. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  8195. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.api_get_cidreq(
  8196. ).'&action=addlink&amp;lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>';
  8197. $return .= '</li>';
  8198. $course_info = api_get_course_info();
  8199. while ($row_link = Database :: fetch_array($res_link)) {
  8200. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  8201. if ($item_visibility != 2) {
  8202. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  8203. $return .= '<a class="moved" href="#">';
  8204. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8205. $return .= '</a> ';
  8206. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  8207. $return .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  8208. $_GET['cidReq']
  8209. ).'&amp;action=add_item&amp;type='.TOOL_LINK.'&amp;file='.$row_link['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8210. $row_link['title'].
  8211. '</a>';
  8212. $return .= '</li>';
  8213. }
  8214. }
  8215. $return .= '</ul>';
  8216. return $return;
  8217. }
  8218. /**
  8219. * Creates a list with all the student publications in it
  8220. * @return unknown
  8221. */
  8222. public function get_student_publications()
  8223. {
  8224. $return = '<div class="lp_resource" >';
  8225. $return .= '<div class="lp_resource_element">';
  8226. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  8227. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8228. ).'&amp;action=add_item&amp;type='.TOOL_STUDENTPUBLICATION.'&amp;lp_id='.$this->lp_id.'">'.get_lang(
  8229. 'AddAssignmentPage'
  8230. ).'</a>';
  8231. $return .= '</div>';
  8232. $return .= '</div>';
  8233. return $return;
  8234. }
  8235. /**
  8236. * Creates a list with all the forums in it
  8237. * @return string
  8238. */
  8239. public function get_forums()
  8240. {
  8241. require_once '../forum/forumfunction.inc.php';
  8242. require_once '../forum/forumconfig.inc.php';
  8243. $a_forums = get_forums();
  8244. $return = '<ul class="lp_resource">';
  8245. //First add link
  8246. $return .= '<li class="lp_resource_element">';
  8247. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  8248. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'forum/index.php?'.api_get_cidreq(
  8249. ).'&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id='.$this->lp_id.'" title="'.get_lang(
  8250. 'CreateANewForum'
  8251. ).'">'.get_lang('CreateANewForum').'</a>';
  8252. $return .= '</li>';
  8253. $return .= '<script>
  8254. function toggle_forum(forum_id){
  8255. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8256. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8257. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'remove.gif";
  8258. } else {
  8259. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8260. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'add.gif";
  8261. }
  8262. }
  8263. </script>';
  8264. foreach ($a_forums as $forum) {
  8265. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8266. if (!empty($forum['forum_id'])) {
  8267. $return .= '<a class="moved" href="#">';
  8268. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8269. $return .= ' </a>';
  8270. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  8271. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum('.$forum['forum_id'].')" style="vertical-align:middle">
  8272. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8273. </a>
  8274. <a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  8275. $_GET['cidReq']
  8276. ).'&amp;action=add_item&amp;type='.TOOL_FORUM.'&amp;forum_id='.$forum['forum_id'].'&amp;lp_id='.$this->lp_id.'" style="vertical-align:middle">'.Security :: remove_XSS(
  8277. $forum['forum_title']
  8278. ).'</a>';
  8279. }
  8280. $return .= '</li>';
  8281. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8282. $a_threads = get_threads($forum['forum_id']);
  8283. if (is_array($a_threads)) {
  8284. foreach ($a_threads as $thread) {
  8285. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8286. $return .= '&nbsp;<a class="moved" href="#">';
  8287. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8288. $return .= ' </a>';
  8289. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8290. $return .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  8291. $_GET['cidReq']
  8292. ).'&amp;action=add_item&amp;type='.TOOL_THREAD.'&amp;thread_id='.$thread['thread_id'].'&amp;lp_id='.$this->lp_id.'">'.Security :: remove_XSS(
  8293. $thread['thread_title']
  8294. ).'</a>';
  8295. $return .= '</li>';
  8296. }
  8297. }
  8298. $return .= '</div>';
  8299. }
  8300. $return .= '</ul>';
  8301. return $return;
  8302. }
  8303. /**
  8304. * // TODO: The output encoding should be equal to the system encoding.
  8305. *
  8306. * Exports the learning path as a SCORM package. This is the main function that
  8307. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8308. * whole thing and returns the zip.
  8309. *
  8310. * This method needs to be called in PHP5, as it will fail with non-adequate
  8311. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8312. * you need to call it on a learnpath object.
  8313. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8314. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8315. * path has been modified, it should use the generic method here below.
  8316. * @TODO link this function with the export_lp() function in the same class
  8317. * @param string Optional name of zip file. If none, title of learnpath is
  8318. * domesticated and trailed with ".zip"
  8319. * @return string Returns the zip package string, or null if error
  8320. */
  8321. public function scorm_export()
  8322. {
  8323. global $_course;
  8324. $course_id = api_get_course_int_id();
  8325. $links_to_create = null;
  8326. // Remove memory and time limits as much as possible as this might be a long process...
  8327. if (function_exists('ini_set')) {
  8328. api_set_memory_limit('128M');
  8329. ini_set('max_execution_time', 600);
  8330. }
  8331. // Create the zip handler (this will remain available throughout the method).
  8332. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8333. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8334. $temp_dir_short = uniqid();
  8335. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8336. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8337. $zip_folder = new PclZip($temp_zip_file);
  8338. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8339. $root_path = $main_path = api_get_path(SYS_PATH);
  8340. $files_cleanup = array();
  8341. // Place to temporarily stash the zipfiles.
  8342. // create the temp dir if it doesn't exist
  8343. // or do a cleanup befor creating the zipfile.
  8344. if (!is_dir($temp_zip_dir)) {
  8345. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8346. } else {
  8347. // Cleanup: Check the temp dir for old files and delete them.
  8348. $handle = opendir($temp_zip_dir);
  8349. while (false !== ($file = readdir($handle))) {
  8350. if ($file != '.' && $file != '..') {
  8351. unlink("$temp_zip_dir/$file");
  8352. }
  8353. }
  8354. closedir($handle);
  8355. }
  8356. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8357. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file(
  8358. $current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml'
  8359. )
  8360. ) {
  8361. // Remove the possible . at the end of the path.
  8362. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8363. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8364. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8365. $zip_files_dist = copyr(
  8366. $current_course_path.'/scorm/'.$this->path,
  8367. $dest_path_to_scorm_folder,
  8368. array('imsmanifest'),
  8369. $zip_files
  8370. );
  8371. }
  8372. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  8373. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8374. // Aggregation Model official document, secion "2.3 Content Packaging".
  8375. $xmldoc = new DOMDocument('1.0'); // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  8376. $root = $xmldoc->createElement('manifest');
  8377. $root->setAttribute('identifier', 'SingleCourseManifest');
  8378. $root->setAttribute('version', '1.1');
  8379. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8380. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8381. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8382. $root->setAttribute(
  8383. 'xsi:schemaLocation',
  8384. 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'
  8385. );
  8386. // Build mandatory sub-root container elements.
  8387. $metadata = $xmldoc->createElement('metadata');
  8388. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8389. $metadata->appendChild($md_schema);
  8390. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8391. $metadata->appendChild($md_schemaversion);
  8392. $root->appendChild($metadata);
  8393. $organizations = $xmldoc->createElement('organizations');
  8394. $resources = $xmldoc->createElement('resources');
  8395. // Build the only organization we will use in building our learnpaths.
  8396. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8397. $organization = $xmldoc->createElement('organization');
  8398. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8399. // To set the title of the SCORM entity (=organization), we take the name given
  8400. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8401. // learning path charset) as it is the encoding that defines how it is stored
  8402. // in the database. Then we convert it to HTML entities again as the "&" character
  8403. // alone is not authorized in XML (must be &amp;).
  8404. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8405. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8406. $organization->appendChild($org_title);
  8407. $folder_name = 'document';
  8408. // Removes the learning_path/scorm_folder path when exporting see #4841
  8409. $path_to_remove = null;
  8410. $result = $this->generate_lp_folder($_course);
  8411. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8412. $path_to_remove = 'document'.$result['dir'];
  8413. $path_to_replace = $folder_name.'/';
  8414. }
  8415. //Fixes chamilo scorm exports
  8416. if ($this->ref == 'chamilo_scorm_export') {
  8417. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8418. }
  8419. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8420. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8421. $link_updates = array();
  8422. foreach ($this->items as $index => $item) {
  8423. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8424. // Get included documents from this item.
  8425. if ($item->type == 'sco') {
  8426. $inc_docs = $item->get_resources_from_source(
  8427. null,
  8428. api_get_path(SYS_COURSE_PATH).api_get_course_path(
  8429. ).'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8430. );
  8431. } else {
  8432. $inc_docs = $item->get_resources_from_source();
  8433. }
  8434. // Give a child element <item> to the <organization> element.
  8435. $my_item_id = $item->get_id();
  8436. $my_item = $xmldoc->createElement('item');
  8437. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8438. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8439. $my_item->setAttribute('isvisible', 'true');
  8440. // Give a child element <title> to the <item> element.
  8441. $my_title = $xmldoc->createElement(
  8442. 'title',
  8443. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8444. );
  8445. $my_item->appendChild($my_title);
  8446. // Give a child element <adlcp:prerequisites> to the <item> element.
  8447. $my_prereqs = $xmldoc->createElement(
  8448. 'adlcp:prerequisites',
  8449. $this->get_scorm_prereq_string($my_item_id)
  8450. );
  8451. $my_prereqs->setAttribute('type', 'aicc_script');
  8452. $my_item->appendChild($my_prereqs);
  8453. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8454. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8455. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8456. //$xmldoc->createElement('adlcp:timelimitaction','');
  8457. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8458. //$xmldoc->createElement('adlcp:datafromlms','');
  8459. // Give a child element <adlcp:masteryscore> to the <item> element.
  8460. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8461. $my_item->appendChild($my_masteryscore);
  8462. // Attach this item to the organization element or hits parent if there is one.
  8463. if (!empty($item->parent) && $item->parent != 0) {
  8464. $children = $organization->childNodes;
  8465. $possible_parent = & $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8466. if (is_object($possible_parent)) {
  8467. $possible_parent->appendChild($my_item);
  8468. } else {
  8469. if ($this->debug > 0) {
  8470. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  8471. }
  8472. }
  8473. } else {
  8474. if ($this->debug > 0) {
  8475. error_log('No parent');
  8476. }
  8477. $organization->appendChild($my_item);
  8478. }
  8479. // Get the path of the file(s) from the course directory root.
  8480. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8481. if (!empty($path_to_remove)) {
  8482. //From docs
  8483. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8484. //From quiz
  8485. if ($this->ref == 'chamilo_scorm_export') {
  8486. $path_to_remove = 'scorm/'.$this->path.'/';
  8487. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8488. }
  8489. } else {
  8490. $my_xml_file_path = $my_file_path;
  8491. }
  8492. $my_sub_dir = dirname($my_file_path);
  8493. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8494. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8495. $my_xml_sub_dir = $my_sub_dir;
  8496. // Give a <resource> child to the <resources> element
  8497. $my_resource = $xmldoc->createElement('resource');
  8498. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8499. $my_resource->setAttribute('type', 'webcontent');
  8500. $my_resource->setAttribute('href', $my_xml_file_path);
  8501. // adlcp:scormtype can be either 'sco' or 'asset'.
  8502. if ($item->type == 'sco') {
  8503. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8504. } else {
  8505. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8506. }
  8507. // xml:base is the base directory to find the files declared in this resource.
  8508. $my_resource->setAttribute('xml:base', '');
  8509. // Give a <file> child to the <resource> element.
  8510. $my_file = $xmldoc->createElement('file');
  8511. $my_file->setAttribute('href', $my_xml_file_path);
  8512. $my_resource->appendChild($my_file);
  8513. // Dependency to other files - not yet supported.
  8514. $i = 1;
  8515. foreach ($inc_docs as $doc_info) {
  8516. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8517. continue;
  8518. }
  8519. $my_dep = $xmldoc->createElement('resource');
  8520. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8521. $my_dep->setAttribute('identifier', $res_id);
  8522. $my_dep->setAttribute('type', 'webcontent');
  8523. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8524. $my_dep_file = $xmldoc->createElement('file');
  8525. // Check type of URL.
  8526. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8527. if ($doc_info[1] == 'remote') {
  8528. // Remote file. Save url as is.
  8529. $my_dep_file->setAttribute('href', $doc_info[0]);
  8530. $my_dep->setAttribute('xml:base', '');
  8531. } elseif ($doc_info[1] == 'local') {
  8532. switch ($doc_info[2]) {
  8533. case 'url': // Local URL - save path as url for now, don't zip file.
  8534. $abs_path = api_get_path(SYS_PATH).str_replace(
  8535. api_get_path(WEB_PATH),
  8536. '',
  8537. $doc_info[0]
  8538. );
  8539. $current_dir = dirname($abs_path);
  8540. $current_dir = str_replace('\\', '/', $current_dir);
  8541. $file_path = realpath($abs_path);
  8542. $file_path = str_replace('\\', '/', $file_path);
  8543. $my_dep_file->setAttribute('href', $file_path);
  8544. $my_dep->setAttribute('xml:base', '');
  8545. if (strstr($file_path, $main_path) !== false) {
  8546. // The calculated real path is really inside Chamilo's root path.
  8547. // Reduce file path to what's under the DocumentRoot.
  8548. $file_path = substr($file_path, strlen($root_path) - 1);
  8549. //echo $file_path;echo '<br /><br />';
  8550. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8551. $zip_files_abs[] = $file_path;
  8552. $link_updates[$my_file_path][] = array(
  8553. 'orig' => $doc_info[0],
  8554. 'dest' => $file_path
  8555. );
  8556. $my_dep_file->setAttribute('href', $file_path);
  8557. $my_dep->setAttribute('xml:base', '');
  8558. } elseif (empty($file_path)) {
  8559. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8560. if (strpos($document_root, -1) == '/') {
  8561. $document_root = substr(0, -1, $document_root);
  8562. }*/
  8563. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8564. $file_path = str_replace('//', '/', $file_path);
  8565. if (file_exists($file_path)) {
  8566. $file_path = substr(
  8567. $file_path,
  8568. strlen($current_dir)
  8569. ); // We get the relative path.
  8570. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8571. $link_updates[$my_file_path][] = array(
  8572. 'orig' => $doc_info[0],
  8573. 'dest' => $file_path
  8574. );
  8575. $my_dep_file->setAttribute('href', $file_path);
  8576. $my_dep->setAttribute('xml:base', '');
  8577. }
  8578. }
  8579. break;
  8580. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8581. $my_dep_file->setAttribute('href', $doc_info[0]);
  8582. $my_dep->setAttribute('xml:base', '');
  8583. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  8584. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8585. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8586. $abs_img_path_without_subdir = $doc_info[0];
  8587. $relp = api_get_path(REL_PATH); // The url-append config param.
  8588. $pos = strpos($abs_img_path_without_subdir, $relp);
  8589. if ($pos === 0) {
  8590. $abs_img_path_without_subdir = '/'.substr(
  8591. $abs_img_path_without_subdir,
  8592. strlen($relp)
  8593. );
  8594. }
  8595. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  8596. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8597. $file_path = str_replace('\\', '/', $file_path);
  8598. $file_path = str_replace('//', '/', $file_path);
  8599. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  8600. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8601. $cur_path = substr(
  8602. $current_course_path,
  8603. -1
  8604. ) == '/' ? $current_course_path : $current_course_path.'/';
  8605. // Check if the current document is in that path.
  8606. if (strstr($file_path, $cur_path) !== false) {
  8607. // The document is in that path, now get the relative path
  8608. // to the containing document.
  8609. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8610. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8611. $relative_path = '';
  8612. if (strstr($file_path, $cur_path) !== false) {
  8613. $relative_path = substr($file_path, strlen($orig_file_path));
  8614. $file_path = substr($file_path, strlen($cur_path));
  8615. } else {
  8616. // This case is still a problem as it's difficult to calculate a relative path easily
  8617. // might still generate wrong links.
  8618. //$file_path = substr($file_path,strlen($cur_path));
  8619. // Calculate the directory path to the current file (without trailing slash).
  8620. $my_relative_path = dirname($file_path);
  8621. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8622. $my_relative_file = basename($file_path);
  8623. // Calculate the directory path to the containing file (without trailing slash).
  8624. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8625. $dotdot = '';
  8626. $subdir = '';
  8627. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen(
  8628. $my_orig_file_path
  8629. ) > 1) && (strlen($my_relative_path) > 1)) {
  8630. $my_relative_path2 = dirname($my_relative_path);
  8631. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8632. $my_orig_file_path = dirname($my_orig_file_path);
  8633. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8634. $subdir = substr(
  8635. $my_relative_path,
  8636. strlen($my_relative_path2) + 1
  8637. ).'/'.$subdir;
  8638. $dotdot += '../';
  8639. $my_relative_path = $my_relative_path2;
  8640. }
  8641. $relative_path = $dotdot.$subdir.$my_relative_file;
  8642. }
  8643. // Put the current document in the zip (this array is the array
  8644. // that will manage documents already in the course folder - relative).
  8645. $zip_files[] = $file_path;
  8646. // Update the links to the current document in the containing document (make them relative).
  8647. $link_updates[$my_file_path][] = array(
  8648. 'orig' => $doc_info[0],
  8649. 'dest' => $relative_path
  8650. );
  8651. $my_dep_file->setAttribute('href', $file_path);
  8652. $my_dep->setAttribute('xml:base', '');
  8653. } elseif (strstr($file_path, $main_path) !== false) {
  8654. // The calculated real path is really inside Chamilo's root path.
  8655. // Reduce file path to what's under the DocumentRoot.
  8656. $file_path = substr($file_path, strlen($root_path));
  8657. //echo $file_path;echo '<br /><br />';
  8658. //error_log('Reduced path: '.$file_path, 0);
  8659. $zip_files_abs[] = $file_path;
  8660. $link_updates[$my_file_path][] = array(
  8661. 'orig' => $doc_info[0],
  8662. 'dest' => $file_path
  8663. );
  8664. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8665. $my_dep->setAttribute('xml:base', '');
  8666. } elseif (empty($file_path)) {
  8667. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8668. if(strpos($document_root,-1) == '/') {
  8669. $document_root = substr(0, -1, $document_root);
  8670. }*/
  8671. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8672. $file_path = str_replace('//', '/', $file_path);
  8673. if (file_exists($file_path)) {
  8674. $file_path = substr(
  8675. $file_path,
  8676. strlen($current_dir)
  8677. ); // We get the relative path.
  8678. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8679. $link_updates[$my_file_path][] = array(
  8680. 'orig' => $doc_info[0],
  8681. 'dest' => $file_path
  8682. );
  8683. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8684. $my_dep->setAttribute('xml:base', '');
  8685. }
  8686. }
  8687. break;
  8688. case 'rel': // Path relative to the current document. Save xml:base as current document's directory and save file in zip as subdir.file_path
  8689. if (substr($doc_info[0], 0, 2) == '..') {
  8690. // Relative path going up.
  8691. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8692. $current_dir = str_replace('\\', '/', $current_dir);
  8693. $file_path = realpath($current_dir.$doc_info[0]);
  8694. $file_path = str_replace('\\', '/', $file_path);
  8695. //error_log($file_path.' <-> '.$main_path,0);
  8696. if (strstr($file_path, $main_path) !== false) {
  8697. // The calculated real path is really inside Chamilo's root path.
  8698. // Reduce file path to what's under the DocumentRoot.
  8699. $file_path = substr($file_path, strlen($root_path));
  8700. //error_log('Reduced path: '.$file_path, 0);
  8701. $zip_files_abs[] = $file_path;
  8702. $link_updates[$my_file_path][] = array(
  8703. 'orig' => $doc_info[0],
  8704. 'dest' => $file_path
  8705. );
  8706. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8707. $my_dep->setAttribute('xml:base', '');
  8708. }
  8709. } else {
  8710. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8711. $my_dep_file->setAttribute('href', $doc_info[0]);
  8712. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8713. }
  8714. break;
  8715. default:
  8716. $my_dep_file->setAttribute('href', $doc_info[0]);
  8717. $my_dep->setAttribute('xml:base', '');
  8718. break;
  8719. }
  8720. }
  8721. $my_dep->appendChild($my_dep_file);
  8722. $resources->appendChild($my_dep);
  8723. $dependency = $xmldoc->createElement('dependency');
  8724. $dependency->setAttribute('identifierref', $res_id);
  8725. $my_resource->appendChild($dependency);
  8726. $i++;
  8727. }
  8728. //$my_dependency = $xmldoc->createElement('dependency');
  8729. //$my_dependency->setAttribute('identifierref', '');
  8730. $resources->appendChild($my_resource);
  8731. $zip_files[] = $my_file_path;
  8732. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  8733. } else {
  8734. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8735. switch ($item->type) {
  8736. case TOOL_LINK:
  8737. $my_item = $xmldoc->createElement('item');
  8738. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8739. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8740. $my_item->setAttribute('isvisible', 'true');
  8741. // Give a child element <title> to the <item> element.
  8742. $my_title = $xmldoc->createElement(
  8743. 'title',
  8744. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8745. );
  8746. $my_item->appendChild($my_title);
  8747. // Give a child element <adlcp:prerequisites> to the <item> element.
  8748. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8749. $my_prereqs->setAttribute('type', 'aicc_script');
  8750. $my_item->appendChild($my_prereqs);
  8751. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8752. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8753. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8754. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8755. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8756. //$xmldoc->createElement('adlcp:datafromlms', '');
  8757. // Give a child element <adlcp:masteryscore> to the <item> element.
  8758. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8759. $my_item->appendChild($my_masteryscore);
  8760. // Attach this item to the organization element or its parent if there is one.
  8761. if (!empty($item->parent) && $item->parent != 0) {
  8762. $children = $organization->childNodes;
  8763. for ($i = 0; $i < $children->length; $i++) {
  8764. $item_temp = $children->item($i);
  8765. if ($item_temp->nodeName == 'item') {
  8766. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8767. $item_temp->appendChild($my_item);
  8768. }
  8769. }
  8770. }
  8771. } else {
  8772. $organization->appendChild($my_item);
  8773. }
  8774. $my_file_path = 'link_'.$item->get_id().'.html';
  8775. $sql = 'SELECT url, title FROM '.Database :: get_course_table(
  8776. TABLE_LINK
  8777. ).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  8778. $rs = Database::query($sql);
  8779. if ($link = Database :: fetch_array($rs)) {
  8780. $url = $link['url'];
  8781. $title = stripslashes($link['title']);
  8782. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8783. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8784. $my_xml_file_path = $my_file_path;
  8785. $my_sub_dir = dirname($my_file_path);
  8786. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8787. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8788. $my_xml_sub_dir = $my_sub_dir;
  8789. // Give a <resource> child to the <resources> element.
  8790. $my_resource = $xmldoc->createElement('resource');
  8791. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8792. $my_resource->setAttribute('type', 'webcontent');
  8793. $my_resource->setAttribute('href', $my_xml_file_path);
  8794. // adlcp:scormtype can be either 'sco' or 'asset'.
  8795. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8796. // xml:base is the base directory to find the files declared in this resource.
  8797. $my_resource->setAttribute('xml:base', '');
  8798. // give a <file> child to the <resource> element.
  8799. $my_file = $xmldoc->createElement('file');
  8800. $my_file->setAttribute('href', $my_xml_file_path);
  8801. $my_resource->appendChild($my_file);
  8802. $resources->appendChild($my_resource);
  8803. }
  8804. break;
  8805. case TOOL_QUIZ:
  8806. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8807. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8808. $exe = new Exercise();
  8809. $exe->read($exe_id);
  8810. $my_item = $xmldoc->createElement('item');
  8811. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8812. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8813. $my_item->setAttribute('isvisible', 'true');
  8814. // Give a child element <title> to the <item> element.
  8815. $my_title = $xmldoc->createElement(
  8816. 'title',
  8817. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8818. );
  8819. $my_item->appendChild($my_title);
  8820. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8821. //$my_item->appendChild($my_max_score);
  8822. // Give a child element <adlcp:prerequisites> to the <item> element.
  8823. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8824. $my_prereqs->setAttribute('type', 'aicc_script');
  8825. $my_item->appendChild($my_prereqs);
  8826. // Give a child element <adlcp:masteryscore> to the <item> element.
  8827. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8828. $my_item->appendChild($my_masteryscore);
  8829. // Attach this item to the organization element or hits parent if there is one.
  8830. if (!empty($item->parent) && $item->parent != 0) {
  8831. $children = $organization->childNodes;
  8832. for ($i = 0; $i < $children->length; $i++) {
  8833. $item_temp = $children->item($i);
  8834. if ($item_temp->nodeName == 'item') {
  8835. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8836. $item_temp->appendChild($my_item);
  8837. }
  8838. }
  8839. }
  8840. } else {
  8841. $organization->appendChild($my_item);
  8842. }
  8843. // Include export scripts.
  8844. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8845. // Get the path of the file(s) from the course directory root
  8846. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8847. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8848. // Write the contents of the exported exercise into a (big) html file
  8849. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8850. $contents = export_exercise($exe_id, true);
  8851. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8852. $res = file_put_contents($tmp_file_path, $contents);
  8853. if ($res === false) {
  8854. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8855. }
  8856. $files_cleanup[] = $tmp_file_path;
  8857. //error_log($tmp_path); die();
  8858. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8859. $my_xml_file_path = $my_file_path;
  8860. $my_sub_dir = dirname($my_file_path);
  8861. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8862. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8863. $my_xml_sub_dir = $my_sub_dir;
  8864. // Give a <resource> child to the <resources> element.
  8865. $my_resource = $xmldoc->createElement('resource');
  8866. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8867. $my_resource->setAttribute('type', 'webcontent');
  8868. $my_resource->setAttribute('href', $my_xml_file_path);
  8869. // adlcp:scormtype can be either 'sco' or 'asset'.
  8870. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8871. // xml:base is the base directory to find the files declared in this resource.
  8872. $my_resource->setAttribute('xml:base', '');
  8873. // Give a <file> child to the <resource> element.
  8874. $my_file = $xmldoc->createElement('file');
  8875. $my_file->setAttribute('href', $my_xml_file_path);
  8876. $my_resource->appendChild($my_file);
  8877. // Get included docs.
  8878. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  8879. // Dependency to other files - not yet supported.
  8880. $i = 1;
  8881. foreach ($inc_docs as $doc_info) {
  8882. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8883. continue;
  8884. }
  8885. $my_dep = $xmldoc->createElement('resource');
  8886. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8887. $my_dep->setAttribute('identifier', $res_id);
  8888. $my_dep->setAttribute('type', 'webcontent');
  8889. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8890. $my_dep_file = $xmldoc->createElement('file');
  8891. // Check type of URL.
  8892. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8893. if ($doc_info[1] == 'remote') {
  8894. // Remote file. Save url as is.
  8895. $my_dep_file->setAttribute('href', $doc_info[0]);
  8896. $my_dep->setAttribute('xml:base', '');
  8897. } elseif ($doc_info[1] == 'local') {
  8898. switch ($doc_info[2]) {
  8899. case 'url': // Local URL - save path as url for now, don't zip file.
  8900. // Save file but as local file (retrieve from URL).
  8901. $abs_path = api_get_path(SYS_PATH).str_replace(
  8902. api_get_path(WEB_PATH),
  8903. '',
  8904. $doc_info[0]
  8905. );
  8906. $current_dir = dirname($abs_path);
  8907. $current_dir = str_replace('\\', '/', $current_dir);
  8908. $file_path = realpath($abs_path);
  8909. $file_path = str_replace('\\', '/', $file_path);
  8910. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8911. $my_dep->setAttribute('xml:base', '');
  8912. if (strstr($file_path, $main_path) !== false) {
  8913. // The calculated real path is really inside the chamilo root path.
  8914. // Reduce file path to what's under the DocumentRoot.
  8915. $file_path = substr($file_path, strlen($root_path));
  8916. //echo $file_path;echo '<br /><br />';
  8917. //error_log('Reduced path: '.$file_path, 0);
  8918. $zip_files_abs[] = $file_path;
  8919. $link_updates[$my_file_path][] = array(
  8920. 'orig' => $doc_info[0],
  8921. 'dest' => 'document/'.$file_path
  8922. );
  8923. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8924. $my_dep->setAttribute('xml:base', '');
  8925. } elseif (empty($file_path)) {
  8926. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8927. if (strpos($document_root,-1) == '/') {
  8928. $document_root = substr(0, -1, $document_root);
  8929. }*/
  8930. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8931. $file_path = str_replace('//', '/', $file_path);
  8932. if (file_exists($file_path)) {
  8933. $file_path = substr(
  8934. $file_path,
  8935. strlen($current_dir)
  8936. ); // We get the relative path.
  8937. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8938. $link_updates[$my_file_path][] = array(
  8939. 'orig' => $doc_info[0],
  8940. 'dest' => 'document/'.$file_path
  8941. );
  8942. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8943. $my_dep->setAttribute('xml:base', '');
  8944. }
  8945. }
  8946. break;
  8947. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8948. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8949. $current_dir = str_replace('\\', '/', $current_dir);
  8950. $file_path = realpath($doc_info[0]);
  8951. $file_path = str_replace('\\', '/', $file_path);
  8952. $my_dep_file->setAttribute('href', $file_path);
  8953. $my_dep->setAttribute('xml:base', '');
  8954. if (strstr($file_path, $main_path) !== false) {
  8955. // The calculated real path is really inside the chamilo root path.
  8956. // Reduce file path to what's under the DocumentRoot.
  8957. $file_path = substr($file_path, strlen($root_path));
  8958. //echo $file_path;echo '<br /><br />';
  8959. //error_log('Reduced path: '.$file_path, 0);
  8960. $zip_files_abs[] = $file_path;
  8961. $link_updates[$my_file_path][] = array(
  8962. 'orig' => $doc_info[0],
  8963. 'dest' => $file_path
  8964. );
  8965. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8966. $my_dep->setAttribute('xml:base', '');
  8967. } elseif (empty($file_path)) {
  8968. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8969. if (strpos($document_root,-1) == '/') {
  8970. $document_root = substr(0, -1, $document_root);
  8971. }*/
  8972. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8973. $file_path = str_replace('//', '/', $file_path);
  8974. if (file_exists($file_path)) {
  8975. $file_path = substr(
  8976. $file_path,
  8977. strlen($current_dir)
  8978. ); // We get the relative path.
  8979. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8980. $link_updates[$my_file_path][] = array(
  8981. 'orig' => $doc_info[0],
  8982. 'dest' => $file_path
  8983. );
  8984. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8985. $my_dep->setAttribute('xml:base', '');
  8986. }
  8987. }
  8988. break;
  8989. case 'rel': // Path relative to the current document. Save xml:base as current document's directory and save file in zip as subdir.file_path
  8990. if (substr($doc_info[0], 0, 2) == '..') {
  8991. // Relative path going up.
  8992. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8993. $current_dir = str_replace('\\', '/', $current_dir);
  8994. $file_path = realpath($current_dir.$doc_info[0]);
  8995. $file_path = str_replace('\\', '/', $file_path);
  8996. //error_log($file_path.' <-> '.$main_path, 0);
  8997. if (strstr($file_path, $main_path) !== false) {
  8998. // The calculated real path is really inside Chamilo's root path.
  8999. // Reduce file path to what's under the DocumentRoot.
  9000. $file_path = substr($file_path, strlen($root_path));
  9001. $file_path_dest = $file_path;
  9002. // File path is courses/CHAMILO/document/....
  9003. $info_file_path = explode('/', $file_path);
  9004. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9005. $file_path_dest = 'document/'.$file_path;
  9006. }
  9007. //error_log('Reduced path: '.$file_path, 0);
  9008. $zip_files_abs[] = $file_path;
  9009. $link_updates[$my_file_path][] = array(
  9010. 'orig' => $doc_info[0],
  9011. 'dest' => $file_path_dest
  9012. );
  9013. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9014. $my_dep->setAttribute('xml:base', '');
  9015. }
  9016. } else {
  9017. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9018. $my_dep_file->setAttribute('href', $doc_info[0]);
  9019. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9020. }
  9021. break;
  9022. default:
  9023. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9024. $my_dep->setAttribute('xml:base', '');
  9025. break;
  9026. }
  9027. }
  9028. $my_dep->appendChild($my_dep_file);
  9029. $resources->appendChild($my_dep);
  9030. $dependency = $xmldoc->createElement('dependency');
  9031. $dependency->setAttribute('identifierref', $res_id);
  9032. $my_resource->appendChild($dependency);
  9033. $i++;
  9034. }
  9035. $resources->appendChild($my_resource);
  9036. $zip_files[] = $my_file_path;
  9037. break;
  9038. default:
  9039. // Get the path of the file(s) from the course directory root
  9040. $my_file_path = 'non_exportable.html';
  9041. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9042. $my_xml_file_path = $my_file_path;
  9043. $my_sub_dir = dirname($my_file_path);
  9044. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9045. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9046. $my_xml_sub_dir = $my_sub_dir;
  9047. // Give a <resource> child to the <resources> element.
  9048. $my_resource = $xmldoc->createElement('resource');
  9049. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9050. $my_resource->setAttribute('type', 'webcontent');
  9051. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9052. // adlcp:scormtype can be either 'sco' or 'asset'.
  9053. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9054. // xml:base is the base directory to find the files declared in this resource.
  9055. $my_resource->setAttribute('xml:base', '');
  9056. // Give a <file> child to the <resource> element.
  9057. $my_file = $xmldoc->createElement('file');
  9058. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9059. $my_resource->appendChild($my_file);
  9060. $resources->appendChild($my_resource);
  9061. break;
  9062. }
  9063. }
  9064. }
  9065. $organizations->appendChild($organization);
  9066. $root->appendChild($organizations);
  9067. $root->appendChild($resources);
  9068. $xmldoc->appendChild($root);
  9069. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9070. // then add the file to the zip, then destroy the file (this is done automatically).
  9071. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9072. //error_log(print_r($zip_files,true), 0);
  9073. foreach ($zip_files as $file_path) {
  9074. if (empty($file_path)) {
  9075. continue;
  9076. }
  9077. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  9078. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9079. $this->create_path($dest_file);
  9080. //error_log('copy '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/'.$file_path.' to '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/'.$file_path,0);
  9081. //echo $main_path.$file_path.'<br />';
  9082. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9083. // Check if the file needs a link update.
  9084. if (in_array($file_path, array_keys($link_updates))) {
  9085. $string = file_get_contents($dest_file);
  9086. unlink($dest_file);
  9087. foreach ($link_updates[$file_path] as $old_new) {
  9088. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9089. // This is an ugly hack that allows .flv files to be found by the flv player that
  9090. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9091. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9092. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9093. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9094. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9095. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9096. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9097. }
  9098. //Fix to avoid problems with default_course_document
  9099. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9100. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  9101. } else {
  9102. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  9103. $new_dest = $old_new['dest'];
  9104. }
  9105. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9106. $string = str_replace($old_new['orig'], $new_dest, $string);
  9107. //Add files inside the HTMLs
  9108. $new_path = str_replace('/courses/', '', $old_new['orig']);
  9109. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  9110. if (file_exists($sys_course_path.$new_path)) {
  9111. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  9112. }
  9113. }
  9114. file_put_contents($dest_file, $string);
  9115. }
  9116. }
  9117. foreach ($zip_files_abs as $file_path) {
  9118. if (empty($file_path)) {
  9119. continue;
  9120. }
  9121. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  9122. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9123. continue;
  9124. }
  9125. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  9126. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9127. $this->create_path($dest_file);
  9128. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  9129. //error_log('copy '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/'.$file_path.' to '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/'.$file_path, 0);
  9130. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  9131. copy($main_path.$file_path, $dest_file);
  9132. // Check if the file needs a link update.
  9133. if (in_array($file_path, array_keys($link_updates))) {
  9134. $string = file_get_contents($dest_file);
  9135. unlink($dest_file);
  9136. foreach ($link_updates[$file_path] as $old_new) {
  9137. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9138. // This is an ugly hack that allows .flv files to be found by the flv player that
  9139. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9140. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9141. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9142. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9143. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9144. }
  9145. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9146. }
  9147. file_put_contents($dest_file, $string);
  9148. }
  9149. }
  9150. if (is_array($links_to_create)) {
  9151. foreach ($links_to_create as $file => $link) {
  9152. $file_content = '<!DOCTYPE html>
  9153. <head>
  9154. <meta charset="'.api_get_language_isocode().'" />
  9155. <title>'.$link['title'].'</title>
  9156. </head>
  9157. <body dir="'.api_get_text_direction().'">
  9158. <div style="text-align:center">
  9159. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9160. </body>
  9161. </html>';
  9162. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9163. }
  9164. }
  9165. // Add non exportable message explanation.
  9166. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9167. $file_content = '<!DOCTYPE html>
  9168. <head>
  9169. <meta charset="'.api_get_language_isocode().'" />
  9170. <title>'.$lang_not_exportable.'</title>
  9171. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9172. </head>
  9173. <body dir="'.api_get_text_direction().'">';
  9174. $file_content .=
  9175. <<<EOD
  9176. <style>
  9177. .error-message {
  9178. font-family: arial, verdana, helvetica, sans-serif;
  9179. border-width: 1px;
  9180. border-style: solid;
  9181. left: 50%;
  9182. margin: 10px auto;
  9183. min-height: 30px;
  9184. padding: 5px;
  9185. right: 50%;
  9186. width: 500px;
  9187. background-color: #FFD1D1;
  9188. border-color: #FF0000;
  9189. color: #000;
  9190. }
  9191. </style>
  9192. <body>
  9193. <div class="error-message">
  9194. $lang_not_exportable
  9195. </div>
  9196. </body>
  9197. </html>
  9198. EOD;
  9199. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9200. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9201. }
  9202. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9203. // Add the extra files that go along with a SCORM package.
  9204. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9205. $extra_files = scandir($main_code_path);
  9206. foreach ($extra_files as $extra_file) {
  9207. if (strpos($extra_file, '.') === 0) {
  9208. continue;
  9209. } else {
  9210. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9211. $this->create_path($dest_file);
  9212. copy($main_code_path.$extra_file, $dest_file);
  9213. }
  9214. }
  9215. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9216. $manifest = @$xmldoc->saveXML();
  9217. $manifest = Text::api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9218. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9219. $zip_folder->add(
  9220. $archive_path.'/'.$temp_dir_short,
  9221. PCLZIP_OPT_REMOVE_PATH,
  9222. $archive_path.'/'.$temp_dir_short.'/'
  9223. );
  9224. // Clean possible temporary files.
  9225. foreach ($files_cleanup as $file) {
  9226. $res = unlink($file);
  9227. if ($res === false) {
  9228. error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0);
  9229. }
  9230. }
  9231. // Send file to client
  9232. $name = replace_dangerous_char($this->get_name()).'.zip';
  9233. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9234. }
  9235. public function scorm_export_to_pdf($lp_id)
  9236. {
  9237. $lp_id = intval($lp_id);
  9238. $files_to_export = array();
  9239. $course_data = api_get_course_info($this->cc);
  9240. if (!empty($course_data)) {
  9241. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9242. $list = self::get_flat_ordered_items_list($lp_id);
  9243. if (!empty($list)) {
  9244. foreach ($list as $item_id) {
  9245. $item = $this->items[$item_id];
  9246. switch ($item->type) {
  9247. case 'document':
  9248. //Getting documents from a LP with chamilo documents
  9249. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9250. $file_path = api_get_path(
  9251. SYS_COURSE_PATH
  9252. ).$course_data['path'].'/document'.$file_data['path'];
  9253. if (file_exists($file_path)) {
  9254. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9255. }
  9256. break;
  9257. case 'asset': //commes from a scorm package generated by chamilo
  9258. case 'sco':
  9259. $file_path = $scorm_path.'/'.$item->path;
  9260. if (file_exists($file_path)) {
  9261. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9262. }
  9263. break;
  9264. case 'dokeos_chapter':
  9265. case 'dir':
  9266. case 'chapter':
  9267. $files_to_export[] = array('title' => $item->get_title(), 'path' => null);
  9268. break;
  9269. }
  9270. }
  9271. }
  9272. $pdf = new PDF();
  9273. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9274. return $result;
  9275. }
  9276. return false;
  9277. }
  9278. /**
  9279. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  9280. * if it doesn't exist
  9281. */
  9282. public function create_path($path)
  9283. {
  9284. $path_bits = explode('/', dirname($path));
  9285. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9286. $path_built = IS_WINDOWS_OS ? '' : '/';
  9287. foreach ($path_bits as $bit) {
  9288. if (!empty ($bit)) {
  9289. $new_path = $path_built.$bit;
  9290. if (is_dir($new_path)) {
  9291. $path_built = $new_path.'/';
  9292. } else {
  9293. mkdir($new_path, api_get_permissions_for_new_directories());
  9294. $path_built = $new_path.'/';
  9295. }
  9296. }
  9297. }
  9298. }
  9299. /**
  9300. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9301. * @return boolean The results of the unlink function, or false if there was no image to start with
  9302. */
  9303. public function delete_lp_image()
  9304. {
  9305. $img = $this->get_preview_image();
  9306. if ($img != '') {
  9307. $del_file = $this->get_preview_image_path(null, 'sys');
  9308. if (isset($del_file) && file_exists($del_file)) {
  9309. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9310. if (file_exists($del_file_2)) {
  9311. unlink($del_file_2);
  9312. }
  9313. $this->set_preview_image('');
  9314. return @unlink($del_file);
  9315. }
  9316. }
  9317. return false;
  9318. }
  9319. /**
  9320. * Uploads an author image to the upload/learning_path/images directory
  9321. * @param array The image array, coming from the $_FILES superglobal
  9322. * @return boolean True on success, false on error
  9323. */
  9324. public function upload_image($image_array)
  9325. {
  9326. $image_moved = false;
  9327. if (!empty ($image_array['name'])) {
  9328. $upload_ok = FileManager::process_uploaded_file($image_array);
  9329. $has_attachment = true;
  9330. } else {
  9331. $image_moved = true;
  9332. }
  9333. if ($upload_ok) {
  9334. if ($has_attachment) {
  9335. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9336. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9337. $updir = $sys_course_path.$courseDir;
  9338. // Try to add an extension to the file if it hasn't one.
  9339. $new_file_name = FileManager::add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9340. if (!FileManager::filter_extension($new_file_name)) {
  9341. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9342. $image_moved = false;
  9343. } else {
  9344. $file_extension = explode('.', $image_array['name']);
  9345. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9346. $filename = uniqid('');
  9347. $new_file_name = $filename.'.'.$file_extension;
  9348. $new_path = $updir.'/'.$new_file_name;
  9349. // Resize the image.
  9350. $temp = new Image($image_array['tmp_name']);
  9351. $picture_infos = $temp->get_image_info();
  9352. if ($picture_infos['width'] > 104) {
  9353. $thumbwidth = 104;
  9354. } else {
  9355. $thumbwidth = $picture_infos['width'];
  9356. }
  9357. if ($picture_infos['height'] > 96) {
  9358. $new_height = 96;
  9359. } else {
  9360. $new_height = $picture_infos['height'];
  9361. }
  9362. $temp->resize($thumbwidth, $new_height, 0);
  9363. $result = $temp->send_image($new_path);
  9364. // Storing the image filename.
  9365. if ($result) {
  9366. $image_moved = true;
  9367. $this->set_preview_image($new_file_name);
  9368. //Resize to 32
  9369. $temp->resize(64, 64, 0);
  9370. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9371. return true;
  9372. }
  9373. }
  9374. }
  9375. }
  9376. return false;
  9377. }
  9378. public function set_autolunch($lp_id, $status)
  9379. {
  9380. $course_id = api_get_course_int_id();
  9381. $lp_id = intval($lp_id);
  9382. $status = intval($status);
  9383. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9384. //Setting everything to autolunch = 0
  9385. $attributes['autolunch'] = 0;
  9386. $where = array('session_id = ? AND c_id = ? ' => array(api_get_session_id(), $course_id));
  9387. Database::update($lp_table, $attributes, $where);
  9388. if ($status == 1) {
  9389. //Setting my lp_id to autolunch = 1
  9390. $attributes['autolunch'] = 1;
  9391. $where = array('id = ? AND session_id = ? AND c_id = ?' => array($lp_id, api_get_session_id(), $course_id));
  9392. Database::update($lp_table, $attributes, $where);
  9393. }
  9394. }
  9395. /**
  9396. * Gets previous_item_id for the next element of the lp_item table
  9397. * @author Isaac flores paz
  9398. * @return integer Previous item ID
  9399. */
  9400. function select_previous_item_id()
  9401. {
  9402. $course_id = api_get_course_int_id();
  9403. if ($this->debug > 0) {
  9404. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9405. }
  9406. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9407. // Get the max order of the items
  9408. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'";
  9409. $rs_max_order = Database::query($sql_max_order);
  9410. $row_max_order = Database::fetch_object($rs_max_order);
  9411. $max_order = $row_max_order->display_order;
  9412. // Get the previous item ID
  9413. $sql_max = "SELECT id as previous FROM $table_lp_item WHERE c_id = $course_id AND lp_id = '".$this->lp_id."' AND display_order = '".$max_order."' ";
  9414. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  9415. $row_max = Database::fetch_object($rs_max);
  9416. // Return the previous item ID
  9417. return $row_max->previous;
  9418. }
  9419. function copy()
  9420. {
  9421. $main_path = api_get_path(SYS_CODE_PATH);
  9422. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9423. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9424. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9425. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9426. //Course builder
  9427. $cb = new CourseBuilder();
  9428. //Setting tools that will be copied
  9429. $cb->set_tools_to_build(array('learnpaths'));
  9430. //Setting elements that will be copied
  9431. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9432. $course = $cb->build();
  9433. //Course restorer
  9434. $course_restorer = new CourseRestorer($course);
  9435. $course_restorer->set_add_text_in_items(true);
  9436. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9437. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9438. }
  9439. function verify_document_size($s)
  9440. {
  9441. $post_max = ini_get('post_max_size');
  9442. $upl_max = ini_get('upload_max_filesize');
  9443. $documents_total_space = DocumentManager::documents_total_space();
  9444. $course_max_space = DocumentManager::get_course_quota();
  9445. $total_size = filesize($s) + $documents_total_space;
  9446. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9447. return true;
  9448. } else {
  9449. return false;
  9450. }
  9451. }
  9452. function clear_prerequisites()
  9453. {
  9454. $course_id = $this->get_course_int_id();
  9455. if ($this->debug > 0) {
  9456. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9457. }
  9458. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9459. $lp_id = $this->get_id();
  9460. //Cleaning prerequisites
  9461. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9462. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9463. Database::query($sql);
  9464. //Cleaning mastery score for exercises
  9465. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9466. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9467. Database::query($sql);
  9468. }
  9469. function set_previous_step_as_prerequisite_for_all_items()
  9470. {
  9471. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9472. $course_id = $this->get_course_int_id();
  9473. $lp_id = $this->get_id();
  9474. if (!empty($this->items)) {
  9475. $old_id = null;
  9476. $old_max = 0;
  9477. $old_type = null;
  9478. foreach ($this->items as $item) {
  9479. if (!empty($old_id)) {
  9480. $current_item_id = $item->get_id();
  9481. if ($old_type == 'quiz') {
  9482. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$old_max' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$old_id'";
  9483. Database::query($sql);
  9484. }
  9485. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$old_id' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9486. Database::query($sql);
  9487. }
  9488. $old_id = $item->get_id();
  9489. $old_max = $item->get_max();
  9490. $old_type = $item->get_type();
  9491. }
  9492. }
  9493. }
  9494. static function create_category($params)
  9495. {
  9496. global $app;
  9497. $em = $app['orm.em'];
  9498. $item = new Entity\CLpCategory();
  9499. $item->setName($params['name']);
  9500. $item->setCId($params['c_id']);
  9501. $em->persist($item);
  9502. $em->flush();
  9503. }
  9504. static function update_category($params)
  9505. {
  9506. global $app;
  9507. $em = $app['orm.em'];
  9508. $item = $em->find('Entity\CLpCategory', $params['id']);
  9509. if ($item) {
  9510. $item->setName($params['name']);
  9511. $item->setCId($params['c_id']);
  9512. $em->persist($item);
  9513. $em->flush();
  9514. }
  9515. }
  9516. static function move_up_category($id)
  9517. {
  9518. global $app;
  9519. $em = $app['orm.em'];
  9520. $item = $em->find('Entity\CLpCategory', $id);
  9521. if ($item) {
  9522. $position = $item->getPosition() - 1;
  9523. $item->setPosition($position);
  9524. $em->persist($item);
  9525. $em->flush();
  9526. }
  9527. }
  9528. static function move_down_category($id)
  9529. {
  9530. global $app;
  9531. $em = $app['orm.em'];
  9532. $item = $em->find('Entity\CLpCategory', $id);
  9533. if ($item) {
  9534. $position = $item->getPosition() + 1;
  9535. $item->setPosition($position);
  9536. $em->persist($item);
  9537. $em->flush();
  9538. }
  9539. }
  9540. static function get_count_categories($course_id)
  9541. {
  9542. global $app;
  9543. if (empty($course_id)) {
  9544. return 0;
  9545. }
  9546. $em = $app['orm.em'];
  9547. $query = $em->createQuery('SELECT COUNT(u.id) FROM Entity\CLpCategory u WHERE u.cId = :id');
  9548. $query->setParameter('id', $course_id);
  9549. return $query->getSingleScalarResult();
  9550. }
  9551. static function get_categories($course_id)
  9552. {
  9553. global $app;
  9554. $em = $app['orm.em'];
  9555. //Default behaviour
  9556. /*$items = $em->getRepository('Entity\CLpCategory')->findBy(
  9557. array('cId' => $course_id),
  9558. array('name' => 'ASC')
  9559. );*/
  9560. //Using doctrine extensions
  9561. $items = $em->getRepository('Entity\CLpCategory')->getBySortableGroupsQuery(
  9562. array('cId' => $course_id)
  9563. )->getResult();
  9564. return $items;
  9565. }
  9566. static function get_category($id)
  9567. {
  9568. global $app;
  9569. $em = $app['orm.em'];
  9570. $item = $em->find('Entity\CLpCategory', $id);
  9571. return $item;
  9572. }
  9573. static function get_category_by_course($course_id)
  9574. {
  9575. global $app;
  9576. $items = $app['orm.em']->getRepository('Entity\CLpCategory')->findBy(array('cId' => $course_id));
  9577. return $items;
  9578. }
  9579. static function delete_category($id)
  9580. {
  9581. global $app;
  9582. $em = $app['orm.em'];
  9583. $item = $em->find('Entity\CLpCategory', $id);
  9584. if ($item) {
  9585. $courseId = $item->getCId();
  9586. $query = $em->createQuery('SELECT u FROM Entity\CLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9587. $query->setParameter('id', $courseId);
  9588. $query->setParameter('catId', $item->getId());
  9589. $lps = $query->getResult();
  9590. //Setting category = 0
  9591. if ($lps) {
  9592. foreach ($lps as $lpItem) {
  9593. $lpItem->setCategoryId(0);
  9594. }
  9595. }
  9596. //Removing category
  9597. $em->remove($item);
  9598. $em->flush();
  9599. }
  9600. }
  9601. static function get_category_from_course_into_select($course_id, $addSelectOption = false)
  9602. {
  9603. $items = self::get_category_by_course($course_id);
  9604. $cats = array();
  9605. if ($addSelectOption) {
  9606. $cats = array(get_lang('SelectACategory'));
  9607. }
  9608. if (!empty($items)) {
  9609. foreach ($items as $cat) {
  9610. $cats[$cat->getId()] = $cat->getName();
  9611. }
  9612. }
  9613. return $cats;
  9614. }
  9615. }
  9616. if (!function_exists('trim_value')) {
  9617. function trim_value(& $value)
  9618. {
  9619. $value = trim($value);
  9620. }
  9621. }