learnpath.class.php 454 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699
  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. if (isset($row['subscribe_users']) && $row['subscribe_users'] == 1 ) {
  2163. $userVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, $student_id, 'LearnpathSubscription');
  2164. if ($userVisibility == 1) {
  2165. $is_visible = true;
  2166. } else {
  2167. $is_visible = false;
  2168. }
  2169. $groupList = GroupManager::get_group_ids($course_info['real_id'], $student_id);
  2170. if (!empty($groupList)) {
  2171. foreach($groupList as $groupId) {
  2172. $groupVisibility = api_get_item_visibility($course_info, 'learnpath', $row['id'], $session_id, 0, 'LearnpathSubscription', $groupId);
  2173. if ($groupVisibility == 1) {
  2174. $is_visible = true;
  2175. break;
  2176. }
  2177. }
  2178. }
  2179. }
  2180. return $is_visible;
  2181. }
  2182. return false;
  2183. }
  2184. /**
  2185. * Gets a progress bar for the learnpath by counting the number of items in it and the number of items
  2186. * completed so far.
  2187. * @param string Mode in which we want the values
  2188. * @param integer Progress value to display (optional but mandatory if used in abstract context)
  2189. * @param string Text to display near the progress value (optional but mandatory in abstract context)
  2190. * @param boolean true if it comes from a Diplay LP view
  2191. * @return string HTML string containing the progress bar
  2192. */
  2193. public function get_progress_bar($mode = '', $percentage = -1, $text_add = '', $from_lp = false)
  2194. {
  2195. //if ($this->debug > 0) {error_log('New LP - In learnpath::get_progress_bar('.$mode.','.$percentage.','.$text_add.','.$from_lp.')', 0); }
  2196. global $lp_theme_css;
  2197. // Setting up the CSS path of the current style if exists.
  2198. if (!empty ($lp_theme_css)) {
  2199. $css_path = api_get_path(WEB_CODE_PATH).'css/'.$lp_theme_css.'/images/';
  2200. } else {
  2201. $css_path = '../img/';
  2202. }
  2203. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_progress_bar()', 0); }
  2204. if (isset($this) && is_object($this) && ($percentage == '-1' OR $text_add == '')) {
  2205. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2206. }
  2207. $text = $percentage.$text_add;
  2208. //@todo use Display::display_progress();
  2209. $output = '<div class="progress progress-striped">
  2210. <div id="progress_bar_value" class="bar" style="width: '.$text.';"></div>
  2211. </div>
  2212. <div class="progresstext" id="progress_text">'.$text.'</div>';
  2213. return $output;
  2214. }
  2215. /**
  2216. * Gets the progress bar info to display inside the progress bar. Also used by scorm_api.php
  2217. * @param string Mode of display (can be '%' or 'abs').abs means we display a number of completed elements per total elements
  2218. * @param integer Additional steps to fake as completed
  2219. * @return list Percentage or number and symbol (% or /xx)
  2220. */
  2221. public function get_progress_bar_text($mode = '', $add = 0)
  2222. {
  2223. if ($this->debug > 0) {
  2224. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2225. }
  2226. if (empty ($mode)) {
  2227. $mode = $this->progress_bar_mode;
  2228. }
  2229. $total_items = $this->get_total_items_count_without_chapters();
  2230. if ($this->debug > 2) {
  2231. error_log('New LP - Total items available in this learnpath: '.$total_items, 0);
  2232. }
  2233. $i = $this->get_complete_items_count();
  2234. if ($this->debug > 2) {
  2235. error_log('New LP - Items completed so far: '.$i, 0);
  2236. }
  2237. if ($add != 0) {
  2238. $i += $add;
  2239. if ($this->debug > 2) {
  2240. error_log('New LP - Items completed so far (+modifier): '.$i, 0);
  2241. }
  2242. }
  2243. $text = '';
  2244. if ($i > $total_items) {
  2245. $i = $total_items;
  2246. }
  2247. if ($mode == '%') {
  2248. if ($total_items > 0) {
  2249. $percentage = ((float)$i / (float)$total_items) * 100;
  2250. } else {
  2251. $percentage = 0;
  2252. }
  2253. $percentage = number_format($percentage, 0);
  2254. $text = '%';
  2255. } elseif ($mode == 'abs') {
  2256. $percentage = $i;
  2257. $text = '/'.$total_items;
  2258. }
  2259. return array(
  2260. $percentage,
  2261. $text
  2262. );
  2263. }
  2264. /**
  2265. * Gets the progress bar mode
  2266. * @return string The progress bar mode attribute
  2267. */
  2268. public function get_progress_bar_mode()
  2269. {
  2270. if ($this->debug > 0) {
  2271. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2272. }
  2273. if (!empty ($this->progress_bar_mode)) {
  2274. return $this->progress_bar_mode;
  2275. } else {
  2276. return '%';
  2277. }
  2278. }
  2279. /**
  2280. * Gets the learnpath proximity (remote or local)
  2281. * @return string Learnpath proximity
  2282. */
  2283. public function get_proximity()
  2284. {
  2285. if ($this->debug > 0) {
  2286. error_log('New LP - In learnpath::get_proximity()', 0);
  2287. }
  2288. if (!empty ($this->proximity)) {
  2289. return $this->proximity;
  2290. } else {
  2291. return '';
  2292. }
  2293. }
  2294. /**
  2295. * Gets the learnpath theme (remote or local)
  2296. * @return string Learnpath theme
  2297. */
  2298. public function get_theme()
  2299. {
  2300. if ($this->debug > 0) {
  2301. error_log('New LP - In learnpath::get_theme()', 0);
  2302. }
  2303. if (!empty ($this->theme)) {
  2304. return $this->theme;
  2305. } else {
  2306. return '';
  2307. }
  2308. }
  2309. /**
  2310. * Gets the learnpath session id
  2311. * @return string Learnpath theme
  2312. */
  2313. public function get_lp_session_id()
  2314. {
  2315. if ($this->debug > 0) {
  2316. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2317. }
  2318. if (!empty ($this->lp_session_id)) {
  2319. return $this->lp_session_id;
  2320. } else {
  2321. return 0;
  2322. }
  2323. }
  2324. /**
  2325. * Gets the learnpath image
  2326. * @return string Web URL of the LP image
  2327. */
  2328. public function get_preview_image()
  2329. {
  2330. if ($this->debug > 0) {
  2331. error_log('New LP - In learnpath::get_preview_image()', 0);
  2332. }
  2333. if (!empty($this->preview_image)) {
  2334. return $this->preview_image;
  2335. } else {
  2336. return '';
  2337. }
  2338. }
  2339. public function get_preview_image_path($size = null, $path_type = 'web')
  2340. {
  2341. $preview_image = $this->get_preview_image();
  2342. if (isset($preview_image) && !empty($preview_image)) {
  2343. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2344. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2345. if (isset($size)) {
  2346. $info = pathinfo($preview_image);
  2347. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2348. if (file_exists($image_sys_path.$image_custom_size)) {
  2349. if ($path_type == 'web') {
  2350. return $image_path.$image_custom_size;
  2351. } else {
  2352. return $image_sys_path.$image_custom_size;
  2353. }
  2354. }
  2355. } else {
  2356. if ($path_type == 'web') {
  2357. return $image_path.$preview_image;
  2358. } else {
  2359. return $image_sys_path.$preview_image;
  2360. }
  2361. }
  2362. }
  2363. return false;
  2364. }
  2365. public function get_category_id()
  2366. {
  2367. return $this->category_id;
  2368. }
  2369. /**
  2370. * Gets the learnpath author
  2371. * @return string LP's author
  2372. */
  2373. public function get_author()
  2374. {
  2375. if ($this->debug > 0) {
  2376. error_log('New LP - In learnpath::get_author()', 0);
  2377. }
  2378. if (!empty ($this->author)) {
  2379. return $this->author;
  2380. } else {
  2381. return '';
  2382. }
  2383. }
  2384. /**
  2385. * Gets the learnpath author
  2386. * @return string LP's author
  2387. */
  2388. public function get_hide_toc_frame()
  2389. {
  2390. if ($this->debug > 0) {
  2391. error_log('New LP - In learnpath::get_author()', 0);
  2392. }
  2393. if (!empty ($this->hide_toc_frame)) {
  2394. return $this->hide_toc_frame;
  2395. } else {
  2396. return '';
  2397. }
  2398. }
  2399. /**
  2400. * Generate a new prerequisites string for a given item. If this item was a sco and
  2401. * its prerequisites were strings (instead of IDs), then transform those strings into
  2402. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2403. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2404. * same rule as the scorm_export() method
  2405. * @param integer Item ID
  2406. * @return string Prerequisites string ready for the export as SCORM
  2407. */
  2408. public function get_scorm_prereq_string($item_id)
  2409. {
  2410. if ($this->debug > 0) {
  2411. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2412. }
  2413. if (!is_object($this->items[$item_id])) {
  2414. return false;
  2415. }
  2416. $oItem = $this->items[$item_id];
  2417. $prereq = $oItem->get_prereq_string();
  2418. if (empty($prereq)) {
  2419. return '';
  2420. }
  2421. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2422. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2423. // then simply return it (with the ITEM_ prefix).
  2424. //return 'ITEM_' . $prereq;
  2425. return $this->items[$prereq]->ref;
  2426. } else {
  2427. if (isset($this->refs_list[$prereq])) {
  2428. // It's a simple string item from which the ID can be found in the refs list,
  2429. // so we can transform it directly to an ID for export.
  2430. return $this->items[$this->refs_list[$prereq]]->ref;
  2431. } else {
  2432. if (isset($this->refs_list['ITEM_'.$prereq])) {
  2433. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2434. } else {
  2435. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2436. // and replace them, one by one, by the internal IDs (chamilo db)
  2437. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2438. // by a space as well.
  2439. $find = array(
  2440. '&',
  2441. '|',
  2442. '~',
  2443. '=',
  2444. '<>',
  2445. '{',
  2446. '}',
  2447. '*',
  2448. '(',
  2449. ')'
  2450. );
  2451. $replace = array(
  2452. ' ',
  2453. ' ',
  2454. ' ',
  2455. ' ',
  2456. ' ',
  2457. ' ',
  2458. ' ',
  2459. ' ',
  2460. ' ',
  2461. ' '
  2462. );
  2463. $prereq_mod = str_replace($find, $replace, $prereq);
  2464. $ids = split(' ', $prereq_mod);
  2465. foreach ($ids as $id) {
  2466. $id = trim($id);
  2467. if (isset ($this->refs_list[$id])) {
  2468. $prereq = preg_replace(
  2469. '/[^a-zA-Z_0-9]('.$id.')[^a-zA-Z_0-9]/',
  2470. 'ITEM_'.$this->refs_list[$id],
  2471. $prereq
  2472. );
  2473. }
  2474. }
  2475. error_log(
  2476. 'New LP - In learnpath::get_scorm_prereq_string(): returning modified string: '.$prereq,
  2477. 0
  2478. );
  2479. return $prereq;
  2480. }
  2481. }
  2482. }
  2483. }
  2484. /**
  2485. * Returns the XML DOM document's node
  2486. * @param resource Reference to a list of objects to search for the given ITEM_*
  2487. * @param string The identifier to look for
  2488. * @return mixed The reference to the element found with that identifier. False if not found
  2489. */
  2490. public function get_scorm_xml_node(& $children, $id)
  2491. {
  2492. for ($i = 0; $i < $children->length; $i++) {
  2493. $item_temp = $children->item($i);
  2494. if ($item_temp->nodeName == 'item') {
  2495. if ($item_temp->getAttribute('identifier') == $id) {
  2496. return $item_temp;
  2497. }
  2498. }
  2499. $subchildren = $item_temp->childNodes;
  2500. if ($subchildren->length > 0) {
  2501. $val = $this->get_scorm_xml_node($subchildren, $id);
  2502. if (is_object($val)) {
  2503. return $val;
  2504. }
  2505. }
  2506. }
  2507. return false;
  2508. }
  2509. /**
  2510. * Returns a usable array of stats related to the current learnpath and user
  2511. * @return array Well-formatted array containing status for the current learnpath
  2512. */
  2513. public function get_stats()
  2514. {
  2515. if ($this->debug > 0) {
  2516. error_log('New LP - In learnpath::get_stats()', 0);
  2517. }
  2518. // TODO
  2519. }
  2520. /**
  2521. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2522. * the given course (for all learnpaths and all users)
  2523. * @param string Course code
  2524. * @return array Well-formatted array containing status for the course's learnpaths
  2525. */
  2526. public function get_stats_course($course)
  2527. {
  2528. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2529. // TODO
  2530. }
  2531. /**
  2532. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2533. * the given course and learnpath (for all users)
  2534. * @param string Course code
  2535. * @param integer Learnpath ID
  2536. * @return array Well-formatted array containing status for the specified learnpath
  2537. */
  2538. public function get_stats_lp($course, $lp)
  2539. {
  2540. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2541. // TODO
  2542. }
  2543. /**
  2544. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2545. * the given course, learnpath and user.
  2546. * @param string Course code
  2547. * @param integer Learnpath ID
  2548. * @param integer User ID
  2549. * @return array Well-formatted array containing status for the specified learnpath and user
  2550. */
  2551. public function get_stats_lp_user($course, $lp, $user)
  2552. {
  2553. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2554. // TODO
  2555. }
  2556. /**
  2557. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2558. * the given course and learnpath (for all users)
  2559. * @param string Course code
  2560. * @param integer User ID
  2561. * @return array Well-formatted array containing status for the user's learnpaths
  2562. */
  2563. public function get_stats_user($course, $user)
  2564. {
  2565. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2566. // TODO
  2567. }
  2568. /**
  2569. * Gets the status list for all LP's items
  2570. * @return array Array of [index] => [item ID => current status]
  2571. */
  2572. public function get_items_status_list()
  2573. {
  2574. if ($this->debug > 0) {
  2575. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2576. }
  2577. $list = array();
  2578. foreach ($this->ordered_items as $item_id) {
  2579. $list[] = array(
  2580. $item_id => $this->items[$item_id]->get_status()
  2581. );
  2582. }
  2583. return $list;
  2584. }
  2585. /**
  2586. * Return the number of interactions for the given learnpath Item View ID.
  2587. * This method can be used as static.
  2588. * @param integer Item View ID
  2589. * @param integer course id
  2590. * @return integer Number of interactions
  2591. */
  2592. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2593. {
  2594. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2595. $lp_iv_id = intval($lp_iv_id);
  2596. $course_id = intval($course_id);
  2597. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2598. $res = Database::query($sql);
  2599. $res = 0;
  2600. $num = 0;
  2601. if (Database::num_rows($res)) {
  2602. $row = Database::fetch_array($res);
  2603. $num = $row[0];
  2604. }
  2605. return $num;
  2606. }
  2607. /**
  2608. * Return the interactions as an array for the given lp_iv_id.
  2609. * This method can be used as static.
  2610. * @param integer Learnpath Item View ID
  2611. * @return array
  2612. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2613. */
  2614. public static function get_iv_interactions_array($lp_iv_id = 0)
  2615. {
  2616. $course_id = api_get_course_int_id();
  2617. $list = array();
  2618. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2619. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2620. $res = Database::query($sql);
  2621. $num = Database :: num_rows($res);
  2622. if ($num > 0) {
  2623. $list[] = array(
  2624. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2625. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2626. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2627. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2628. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2629. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2630. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2631. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2632. );
  2633. while ($row = Database :: fetch_array($res)) {
  2634. $list[] = array(
  2635. 'order_id' => ($row['order_id'] + 1),
  2636. 'id' => urldecode($row['interaction_id']),
  2637. //urldecode because they often have %2F or stuff like that
  2638. 'type' => $row['interaction_type'],
  2639. 'time' => $row['completion_time'],
  2640. //'correct_responses' => $row['correct_responses'],
  2641. 'correct_responses' => '',
  2642. // Hide correct responses from students.
  2643. 'student_response' => $row['student_response'],
  2644. 'result' => $row['result'],
  2645. 'latency' => $row['latency']
  2646. );
  2647. }
  2648. }
  2649. return $list;
  2650. }
  2651. /**
  2652. * Return the number of objectives for the given learnpath Item View ID.
  2653. * This method can be used as static.
  2654. * @param integer Item View ID
  2655. * @return integer Number of objectives
  2656. */
  2657. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2658. {
  2659. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2660. $course_id = intval($course_id);
  2661. $lp_iv_id = intval($lp_iv_id);
  2662. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2663. //@todo seems that this always returns 0
  2664. $res = Database::query($sql);
  2665. $res = 0;
  2666. $num = 0;
  2667. if (Database::num_rows($res)) {
  2668. $row = Database :: fetch_array($res);
  2669. $num = $row[0];
  2670. }
  2671. return $num;
  2672. }
  2673. /**
  2674. * Return the objectives as an array for the given lp_iv_id.
  2675. * This method can be used as static.
  2676. * @param integer Learnpath Item View ID
  2677. * @return array
  2678. * @todo Translate labels
  2679. */
  2680. public function get_iv_objectives_array($lp_iv_id = 0)
  2681. {
  2682. $course_id = api_get_course_int_id();
  2683. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2684. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2685. $res = Database::query($sql);
  2686. $num = Database :: num_rows($res);
  2687. $list = array();
  2688. if ($num > 0) {
  2689. $list[] = array(
  2690. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2691. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2692. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2693. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2694. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2695. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2696. );
  2697. while ($row = Database :: fetch_array($res)) {
  2698. $list[] = array(
  2699. 'order_id' => ($row['order_id'] + 1),
  2700. 'objective_id' => urldecode($row['objective_id']),
  2701. // urldecode() because they often have %2F or stuff like that.
  2702. 'score_raw' => $row['score_raw'],
  2703. 'score_max' => $row['score_max'],
  2704. 'score_min' => $row['score_min'],
  2705. 'status' => $row['status']
  2706. );
  2707. }
  2708. }
  2709. return $list;
  2710. }
  2711. /**
  2712. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2713. * used by get_html_toc() to be ready to display
  2714. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2715. */
  2716. public function get_toc()
  2717. {
  2718. if ($this->debug > 0) {
  2719. error_log('learnpath::get_toc()', 0);
  2720. }
  2721. $toc = array();
  2722. //echo "<pre>".print_r($this->items,true)."</pre>";
  2723. foreach ($this->ordered_items as $item_id) {
  2724. if ($this->debug > 2) {
  2725. error_log('learnpath::get_toc(): getting info for item '.$item_id, 0);
  2726. }
  2727. // TODO: Change this link generation and use new function instead.
  2728. $toc[] = array(
  2729. 'id' => $item_id,
  2730. 'title' => $this->items[$item_id]->get_title(),
  2731. 'status' => $this->items[$item_id]->get_status(),
  2732. 'level' => $this->items[$item_id]->get_level(),
  2733. 'type' => $this->items[$item_id]->get_type(),
  2734. 'description' => $this->items[$item_id]->get_description(),
  2735. 'path' => $this->items[$item_id]->get_path(),
  2736. );
  2737. }
  2738. if ($this->debug > 2) {
  2739. error_log('New LP - In learnpath::get_toc() - TOC array: '.print_r($toc, true), 0);
  2740. }
  2741. return $toc;
  2742. }
  2743. /**
  2744. * Generate and return the table of contents for this learnpath. The JS
  2745. * table returned is used inside of scorm_api.php
  2746. * @return string A JS array vairiable construction
  2747. */
  2748. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2749. {
  2750. if ($this->debug > 0) {
  2751. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2752. }
  2753. $toc = $varname.' = new Array();';
  2754. //echo "<pre>".print_r($this->items,true)."</pre>";
  2755. foreach ($this->ordered_items as $item_id) {
  2756. $toc .= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2757. }
  2758. if ($this->debug > 2) {
  2759. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: '.print_r($toc, true), 0);
  2760. }
  2761. return $toc;
  2762. }
  2763. /**
  2764. * Gets the learning path type
  2765. * @param boolean Return the name? If false, return the ID. Default is false.
  2766. * @return mixed Type ID or name, depending on the parameter
  2767. */
  2768. public function get_type($get_name = false)
  2769. {
  2770. $res = false;
  2771. if ($this->debug > 0) {
  2772. error_log('New LP - In learnpath::get_type()', 0);
  2773. }
  2774. if (!empty ($this->type)) {
  2775. if ($get_name) {
  2776. // Get it from the lp_type table in main db.
  2777. } else {
  2778. $res = $this->type;
  2779. }
  2780. }
  2781. if ($this->debug > 2) {
  2782. error_log('New LP - In learnpath::get_type() - Returning '.($res ? $res : 'false'), 0);
  2783. }
  2784. return $res;
  2785. }
  2786. /**
  2787. * Gets the learning path type as static method
  2788. * @param boolean Return the name? If false, return the ID. Default is false.
  2789. * @return mixed Type ID or name, depending on the parameter
  2790. */
  2791. public static function get_type_static($lp_id = 0)
  2792. {
  2793. $course_id = api_get_course_int_id();
  2794. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2795. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '".$lp_id."'";
  2796. $res = Database::query($sql);
  2797. if ($res === false) {
  2798. return null;
  2799. }
  2800. if (Database :: num_rows($res) <= 0) {
  2801. return null;
  2802. }
  2803. $row = Database :: fetch_array($res);
  2804. return $row['lp_type'];
  2805. }
  2806. /**
  2807. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2808. * This method can be used as abstract and is recursive
  2809. * @param integer Learnpath ID
  2810. * @param integer Parent ID of the items to look for
  2811. * @return mixed Ordered list of item IDs or false on error
  2812. */
  2813. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2814. {
  2815. if (empty($course_id)) {
  2816. $course_id = api_get_course_int_id();
  2817. } else {
  2818. $course_id = intval($course_id);
  2819. }
  2820. $list = array();
  2821. if (empty ($lp)) {
  2822. return false;
  2823. }
  2824. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2825. $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";
  2826. $res = Database::query($sql);
  2827. while ($row = Database :: fetch_array($res)) {
  2828. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2829. $list[] = $row['id'];
  2830. foreach ($sublist as $item) {
  2831. $list[] = $item;
  2832. }
  2833. }
  2834. return $list;
  2835. }
  2836. /**
  2837. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2838. * @return string HTML TOC ready to display
  2839. */
  2840. public function get_html_toc($toc_list = null)
  2841. {
  2842. global $_configuration;
  2843. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2844. if ($this->debug > 0) {
  2845. error_log('In learnpath::get_html_toc()', 0);
  2846. }
  2847. if (empty($toc_list)) {
  2848. $toc_list = $this->get_toc();
  2849. }
  2850. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()).'</div>';
  2851. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2852. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2853. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2854. if ($this->get_lp_session_id() == api_get_session_id()) {
  2855. $html .= '<div id="actions_lp" class="actions_lp">';
  2856. $html .= '<div class="btn-group">';
  2857. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2858. )."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2859. 'Overview'
  2860. )."</a>";
  2861. $html .= "<a class='btn' href='lp_controller.php?".api_get_cidreq(
  2862. )."&amp;action=add_item&amp;type=step&amp;lp_id=".$this->lp_id."' target='_parent'>".get_lang(
  2863. 'Edit'
  2864. )."</a>";
  2865. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq(
  2866. )."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=".$this->lp_id.'">'.get_lang('Settings').'</a>';
  2867. $html .= '</div>';
  2868. $html .= '</div>';
  2869. }
  2870. }
  2871. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2872. require_once 'resourcelinker.inc.php';
  2873. // Temporary variables.
  2874. $mycurrentitemid = $this->get_current_item_id();
  2875. $color_counter = 0;
  2876. $i = 0;
  2877. foreach ($toc_list as $item) {
  2878. // TODO: Complete this
  2879. $icon_name = array(
  2880. 'not attempted' => '../img/notattempted.gif',
  2881. 'incomplete' => '../img/incomplete.png',
  2882. 'failed' => '../img/delete.png',
  2883. 'completed' => '../img/completed.png',
  2884. 'passed' => '../img/passed.png',
  2885. 'succeeded' => '../img/succeeded.png',
  2886. 'browsed' => '../img/completed.png',
  2887. );
  2888. $style = 'scorm_item';
  2889. $scorm_color_background = 'scorm_item';
  2890. $style_item = 'scorm_item';
  2891. $current = false;
  2892. if ($item['id'] == $this->current) {
  2893. $style = 'scorm_item_highlight';
  2894. $scorm_color_background = 'scorm_item_highlight';
  2895. } else {
  2896. if ($color_counter % 2 == 0) {
  2897. $scorm_color_background = 'scorm_item_1';
  2898. } else {
  2899. $scorm_color_background = 'scorm_item_2';
  2900. }
  2901. if ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2902. $scorm_color_background = ' scorm_item_section ';
  2903. }
  2904. }
  2905. if ($scorm_color_background != '') {
  2906. $html .= '<div id="toc_'.$item['id'].'" class="'.$scorm_color_background.'">';
  2907. }
  2908. // The anchor will let us center the TOC on the currently viewed item &^D
  2909. if ($item['type'] != 'dokeos_module' && $item['type'] != 'dokeos_chapter') {
  2910. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 1.5).'em; padding-right:'.($item['level'] / 2).'em" title="'.$item['description'].'" >';
  2911. $html .= '<a name="atoc_'.$item['id'].'" />';
  2912. } else {
  2913. $html .= '<div class="'.$style_item.'" style="padding-left: '.($item['level'] * 2).'em; padding-right:'.($item['level'] * 1.5).'em" title="'.$item['description'].'" >';
  2914. }
  2915. $title = $item['title'];
  2916. if (empty ($title)) {
  2917. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2918. }
  2919. $title = Security::remove_XSS($title);
  2920. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dir' && $item['type'] != 'dokeos_module') {
  2921. //$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>" ;
  2922. $url = $this->get_link('http', $item['id'], $toc_list);
  2923. //$html .= '<a href="'.$url.'" target="content_name" onClick="top.load_item('.$item['id'].',\''.$url.'\');">'.$title.'</a>' ;
  2924. //$html .= '<a href="" onClick="top.load_item('.$item['id'].',\''.$url.'\');return false;">'.$title.'</a>' ;
  2925. //<img align="absbottom" width="13" height="13" src="../img/lp_document.png">&nbsp;background:#aaa;
  2926. $html .= '<a href="" onClick="switch_item('.$mycurrentitemid.','.$item['id'].');'.'return false;" >'.stripslashes(
  2927. $title
  2928. ).'</a>';
  2929. } elseif ($item['type'] == 'dokeos_module' || $item['type'] == 'dokeos_chapter') {
  2930. $html .= "<img align='absbottom' width='13' height='13' src='../img/lp_dokeos_module.png'>&nbsp;".stripslashes(
  2931. $title
  2932. );
  2933. } elseif ($item['type'] == 'dir') {
  2934. $html .= stripslashes($title);
  2935. }
  2936. /*$tbl_track_e_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  2937. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2938. $user_id = api_get_user_id();
  2939. $sql = "SELECT path FROM $tbl_track_e_exercises, $tbl_lp_item
  2940. WHERE c_id = $course_id AND
  2941. path = '" . $item['path'] . "' AND
  2942. exe_user_id = '$user_id' AND
  2943. exe_cours_id = '$course_code' AND
  2944. path = exe_exo_id AND
  2945. status <> 'incomplete'";
  2946. $result = Database::query($sql);
  2947. $count = Database :: num_rows($result);*/
  2948. if ($item['type'] == 'quiz') {
  2949. error_log("1-->>>>>>>>>>>>>>>>");
  2950. error_log($item['status']);
  2951. if ($item['status'] == 'completed') {
  2952. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr(
  2953. $item['status'],
  2954. 0,
  2955. 1
  2956. )."' width='14' />";
  2957. } else {
  2958. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name['not attempted']."' alt='".substr(
  2959. 'not attempted',
  2960. 0,
  2961. 1
  2962. )."' width='14' />";
  2963. }
  2964. } else {
  2965. if ($item['type'] != 'dokeos_chapter' && $item['type'] != 'dokeos_module' && $item['type'] != 'dir') {
  2966. $html .= "&nbsp;<img id='toc_img_".$item['id']."' src='".$icon_name[$item['status']]."' alt='".substr(
  2967. $item['status'],
  2968. 0,
  2969. 1
  2970. )."' width='14' />";
  2971. }
  2972. }
  2973. $html .= "</div>";
  2974. if ($scorm_color_background != '') {
  2975. $html .= '</div>';
  2976. }
  2977. $color_counter++;
  2978. }
  2979. $html .= "</div>";
  2980. return $html;
  2981. }
  2982. /**
  2983. * Gets the learnpath maker name - generally the editor's name
  2984. * @return string Learnpath maker name
  2985. */
  2986. public function get_maker()
  2987. {
  2988. if ($this->debug > 0) {
  2989. error_log('New LP - In learnpath::get_maker()', 0);
  2990. }
  2991. if (!empty ($this->maker)) {
  2992. return $this->maker;
  2993. } else {
  2994. return '';
  2995. }
  2996. }
  2997. /**
  2998. * Gets the user-friendly message stored in $this->message
  2999. * @return string Message
  3000. */
  3001. public function get_message()
  3002. {
  3003. if ($this->debug > 0) {
  3004. error_log('New LP - In learnpath::get_message()', 0);
  3005. }
  3006. return $this->message;
  3007. }
  3008. /**
  3009. * Gets the learnpath name/title
  3010. * @return string Learnpath name/title
  3011. */
  3012. public function get_name()
  3013. {
  3014. if ($this->debug > 0) {
  3015. error_log('New LP - In learnpath::get_name()', 0);
  3016. }
  3017. if (!empty ($this->name)) {
  3018. return $this->name;
  3019. } else {
  3020. return 'N/A';
  3021. }
  3022. }
  3023. /**
  3024. * Gets a link to the resource from the present location, depending on item ID.
  3025. * @param string Type of link expected
  3026. * @param integer Learnpath item ID
  3027. * @return string Link to the lp_item resource
  3028. */
  3029. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3030. {
  3031. $course_id = $this->get_course_int_id();
  3032. if ($this->debug > 0) {
  3033. error_log('New LP - In learnpath::get_link('.$type.','.$item_id.')', 0);
  3034. }
  3035. if (empty($item_id)) {
  3036. if ($this->debug > 2) {
  3037. error_log(
  3038. 'New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: '.$this->get_current_item_id(
  3039. ),
  3040. 0
  3041. );
  3042. }
  3043. $item_id = $this->get_current_item_id();
  3044. }
  3045. if (empty($item_id)) {
  3046. if ($this->debug > 2) {
  3047. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3048. }
  3049. //still empty, this means there was no item_id given and we are not in an object context or
  3050. //the object property is empty, return empty link
  3051. $item_id = $this->first();
  3052. return '';
  3053. }
  3054. $file = '';
  3055. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3056. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3057. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3058. $item_id = Database::escape_string($item_id);
  3059. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3060. FROM $lp_table l
  3061. INNER JOIN $lp_item_table li
  3062. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3063. WHERE li.id = $item_id ";
  3064. if ($this->debug > 2) {
  3065. error_log('New LP - In learnpath::get_link() - selecting item '.$sql, 0);
  3066. }
  3067. $res = Database::query($sql);
  3068. if (Database :: num_rows($res) > 0) {
  3069. $row = Database :: fetch_array($res);
  3070. $lp_type = $row['ltype'];
  3071. $lp_path = $row['lpath'];
  3072. $lp_item_type = $row['litype'];
  3073. $lp_item_path = $row['lipath'];
  3074. $lp_item_params = $row['liparams'];
  3075. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3076. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3077. }
  3078. $sys_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  3079. if ($type == 'http') {
  3080. $course_path = api_get_path(WEB_COURSE_PATH).api_get_course_path(); //web path
  3081. } else {
  3082. $course_path = $sys_course_path; //system path
  3083. }
  3084. // 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.
  3085. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3086. $lp_type = 1;
  3087. }
  3088. if ($this->debug > 2) {
  3089. error_log('New LP - In learnpath::get_link() - $lp_type '.$lp_type, 0);
  3090. error_log('New LP - In learnpath::get_link() - $lp_item_type '.$lp_item_type, 0);
  3091. }
  3092. // Now go through the specific cases to get the end of the path
  3093. // @todo Use constants instead of int values.
  3094. switch ($lp_type) {
  3095. case 1 :
  3096. if ($lp_item_type == 'dokeos_chapter') {
  3097. $file = 'lp_content.php?type=dir';
  3098. } else {
  3099. require_once 'resourcelinker.inc.php';
  3100. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3101. if ($this->debug > 0) {
  3102. error_log('rl_get_resource_link_for_learnpath - file: '.$file, 0);
  3103. }
  3104. if ($lp_item_type == 'link') {
  3105. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3106. if (is_youtube_link($file)) {
  3107. $src = get_youtube_video_id($file);
  3108. $file = 'embed.php?type=youtube&src='.$src;
  3109. }
  3110. } else {
  3111. // check how much attempts of a exercise exits in lp
  3112. $lp_item_id = $this->get_current_item_id();
  3113. $lp_view_id = $this->get_view_id();
  3114. $prevent_reinit = null;
  3115. if (isset($this->items[$this->current])) {
  3116. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3117. }
  3118. if (empty($provided_toc)) {
  3119. if ($this->debug > 0) {
  3120. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3121. }
  3122. $list = $this->get_toc();
  3123. } else {
  3124. if ($this->debug > 0) {
  3125. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3126. }
  3127. $list = $provided_toc;
  3128. }
  3129. $type_quiz = false;
  3130. foreach ($list as $toc) {
  3131. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3132. $type_quiz = true;
  3133. }
  3134. }
  3135. if ($type_quiz) {
  3136. $lp_item_id = Database::escape_string($lp_item_id);
  3137. $lp_view_id = Database::escape_string($lp_view_id);
  3138. $sql = "SELECT count(*) FROM $lp_item_view_table
  3139. WHERE c_id = $course_id AND lp_item_id='".(int)$lp_item_id."' AND lp_view_id ='".(int)$lp_view_id."' AND status='completed'";
  3140. $result = Database::query($sql);
  3141. $row_count = Database :: fetch_row($result);
  3142. $count_item_view = (int)$row_count[0];
  3143. $not_multiple_attempt = 0;
  3144. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3145. $not_multiple_attempt = 1;
  3146. }
  3147. $file .= '&not_multiple_attempt='.$not_multiple_attempt;
  3148. }
  3149. $tmp_array = explode('/', $file);
  3150. $document_name = $tmp_array[count($tmp_array) - 1];
  3151. if (strpos($document_name, '_DELETED_')) {
  3152. $file = 'blank.php?error=document_deleted';
  3153. }
  3154. }
  3155. }
  3156. break;
  3157. case 2 :
  3158. if ($this->debug > 2) {
  3159. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3160. }
  3161. if ($lp_item_type != 'dir') {
  3162. // Quite complex here:
  3163. // We want to make sure 'http://' (and similar) links can
  3164. // be loaded as is (withouth the Chamilo path in front) but
  3165. // some contents use this form: resource.htm?resource=http://blablabla
  3166. // which means we have to find a protocol at the path's start, otherwise
  3167. // it should not be considered as an external URL.
  3168. //if ($this->prerequisites_match($item_id)) {
  3169. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3170. if ($this->debug > 2) {
  3171. error_log(
  3172. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3173. 0
  3174. );
  3175. }
  3176. // Distant url, return as is.
  3177. $file = $lp_item_path;
  3178. } else {
  3179. if ($this->debug > 2) {
  3180. error_log(
  3181. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3182. 0
  3183. );
  3184. }
  3185. // Prevent getting untranslatable urls.
  3186. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3187. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3188. // Prepare the path.
  3189. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3190. // TODO: Fix this for urls with protocol header.
  3191. $file = str_replace('//', '/', $file);
  3192. $file = str_replace(':/', '://', $file);
  3193. if (substr($lp_path, -1) == '/') {
  3194. $lp_path = substr($lp_path, 0, -1);
  3195. }
  3196. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$lp_item_path))) {
  3197. // if file not found.
  3198. $decoded = html_entity_decode($lp_item_path);
  3199. list ($decoded) = explode('?', $decoded);
  3200. if (!is_file(realpath($sys_course_path.'/scorm/'.$lp_path.'/'.$decoded))) {
  3201. require_once 'resourcelinker.inc.php';
  3202. $file = rl_get_resource_link_for_learnpath($course_id, $this->get_id(), $item_id);
  3203. if (empty($file)) {
  3204. $file = 'blank.php?error=document_not_found';
  3205. } else {
  3206. $tmp_array = explode('/', $file);
  3207. $document_name = $tmp_array[count($tmp_array) - 1];
  3208. if (strpos($document_name, '_DELETED_')) {
  3209. $file = 'blank.php?error=document_deleted';
  3210. } else {
  3211. $file = 'blank.php?error=document_not_found';
  3212. }
  3213. }
  3214. } else {
  3215. $file = $course_path.'/scorm/'.$lp_path.'/'.$decoded;
  3216. }
  3217. }
  3218. }
  3219. //}else{
  3220. //prerequisites did not match
  3221. //$file = 'blank.php';
  3222. //}
  3223. // We want to use parameters if they were defined in the imsmanifest
  3224. if (strpos($file, 'blank.php') === false) {
  3225. $file .= (strstr($file, '?') === false ? '?' : '').$lp_item_params;
  3226. }
  3227. } else {
  3228. $file = 'lp_content.php?type=dir';
  3229. }
  3230. break;
  3231. case 3 :
  3232. if ($this->debug > 2) {
  3233. error_log('New LP - In learnpath::get_link() '.__LINE__.' - Item type: '.$lp_item_type, 0);
  3234. }
  3235. // Formatting AICC HACP append URL.
  3236. $aicc_append = '?aicc_sid='.urlencode(session_id()).'&aicc_url='.urlencode(
  3237. api_get_path(WEB_CODE_PATH).'newscorm/aicc_hacp.php'
  3238. ).'&';
  3239. if ($lp_item_type != 'dir') {
  3240. // Quite complex here:
  3241. // We want to make sure 'http://' (and similar) links can
  3242. // be loaded as is (withouth the Chamilo path in front) but
  3243. // some contents use this form: resource.htm?resource=http://blablabla
  3244. // which means we have to find a protocol at the path's start, otherwise
  3245. // it should not be considered as an external URL.
  3246. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3247. if ($this->debug > 2) {
  3248. error_log(
  3249. 'New LP - In learnpath::get_link() '.__LINE__.' - Found match for protocol in '.$lp_item_path,
  3250. 0
  3251. );
  3252. }
  3253. // Distant url, return as is.
  3254. $file = $lp_item_path;
  3255. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3256. /*
  3257. if (stristr($file,'<servername>') !== false) {
  3258. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3259. }
  3260. */
  3261. if (stripos($file, '<servername>') !== false) {
  3262. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3263. $web_course_path = str_replace(
  3264. 'https://',
  3265. '',
  3266. str_replace('http://', '', $course_path)
  3267. );
  3268. $file = str_replace('<servername>', $web_course_path.'/scorm/'.$lp_path, $lp_item_path);
  3269. }
  3270. //
  3271. $file .= $aicc_append;
  3272. } else {
  3273. if ($this->debug > 2) {
  3274. error_log(
  3275. 'New LP - In learnpath::get_link() '.__LINE__.' - No starting protocol in '.$lp_item_path,
  3276. 0
  3277. );
  3278. }
  3279. // Prevent getting untranslatable urls.
  3280. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3281. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3282. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3283. $file = $course_path.'/scorm/'.$lp_path.'/'.$lp_item_path;
  3284. // TODO: Fix this for urls with protocol header.
  3285. $file = str_replace('//', '/', $file);
  3286. $file = str_replace(':/', '://', $file);
  3287. $file .= $aicc_append;
  3288. }
  3289. } else {
  3290. $file = 'lp_content.php?type=dir';
  3291. }
  3292. break;
  3293. case 4 :
  3294. break;
  3295. default :
  3296. break;
  3297. }
  3298. }
  3299. if ($this->debug > 2) {
  3300. error_log('New LP - In learnpath::get_link() - returning "'.$file.'" from get_link', 0);
  3301. }
  3302. return $file;
  3303. }
  3304. /**
  3305. * Gets the latest usable view or generate a new one
  3306. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3307. * @return integer DB lp_view id
  3308. */
  3309. public function get_view($attempt_num = 0)
  3310. {
  3311. if ($this->debug > 0) {
  3312. error_log('New LP - In learnpath::get_view()', 0);
  3313. }
  3314. $search = '';
  3315. // Use $attempt_num to enable multi-views management (disabled so far).
  3316. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3317. $search = 'AND view_count = '.$attempt_num;
  3318. }
  3319. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3320. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3321. $course_id = api_get_course_int_id();
  3322. $sql = "SELECT id, view_count FROM $lp_view_table
  3323. WHERE c_id = ".$course_id." AND lp_id = ".$this->get_id()." AND user_id = ".$this->get_user_id(
  3324. )." ".$search.
  3325. " ORDER BY view_count DESC";
  3326. $res = Database::query($sql);
  3327. if (Database :: num_rows($res) > 0) {
  3328. $row = Database :: fetch_array($res);
  3329. $this->lp_view_id = $row['id'];
  3330. } else {
  3331. // There is no database record, create one.
  3332. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id,view_count) VALUES
  3333. ($course_id, ".$this->get_id().",".$this->get_user_id().",1)";
  3334. $res = Database::query($sql);
  3335. $id = Database :: insert_id();
  3336. $this->lp_view_id = $id;
  3337. }
  3338. return $this->lp_view_id;
  3339. }
  3340. /**
  3341. * Gets the current view id
  3342. * @return integer View ID (from lp_view)
  3343. */
  3344. public function get_view_id()
  3345. {
  3346. if ($this->debug > 0) {
  3347. error_log('New LP - In learnpath::get_view_id()', 0);
  3348. }
  3349. if (!empty ($this->lp_view_id)) {
  3350. return $this->lp_view_id;
  3351. } else {
  3352. return 0;
  3353. }
  3354. }
  3355. /**
  3356. * Gets the update queue
  3357. * @return array Array containing IDs of items to be updated by JavaScript
  3358. */
  3359. public function get_update_queue()
  3360. {
  3361. if ($this->debug > 0) {
  3362. error_log('New LP - In learnpath::get_update_queue()', 0);
  3363. }
  3364. return $this->update_queue;
  3365. }
  3366. /**
  3367. * Gets the user ID
  3368. * @return integer User ID
  3369. */
  3370. public function get_user_id()
  3371. {
  3372. if ($this->debug > 0) {
  3373. error_log('New LP - In learnpath::get_user_id()', 0);
  3374. }
  3375. if (!empty ($this->user_id)) {
  3376. return $this->user_id;
  3377. } else {
  3378. return false;
  3379. }
  3380. }
  3381. /**
  3382. * Checks if any of the items has an audio element attached
  3383. * @return bool True or false
  3384. */
  3385. public function has_audio()
  3386. {
  3387. if ($this->debug > 1) {
  3388. error_log('New LP - In learnpath::has_audio()', 0);
  3389. }
  3390. $has = false;
  3391. foreach ($this->items as $i => $item) {
  3392. if (!empty ($this->items[$i]->audio)) {
  3393. $has = true;
  3394. break;
  3395. }
  3396. }
  3397. return $has;
  3398. }
  3399. /**
  3400. * Logs a message into a file
  3401. * @param string Message to log
  3402. * @return boolean True on success, false on error or if msg empty
  3403. */
  3404. public function log($msg)
  3405. {
  3406. if ($this->debug > 0) {
  3407. error_log('New LP - In learnpath::log()', 0);
  3408. }
  3409. // TODO
  3410. $this->error .= $msg;
  3411. return true;
  3412. }
  3413. /**
  3414. * Moves an item up and down at its level
  3415. * @param integer Item to move up and down
  3416. * @param string Direction 'up' or 'down'
  3417. * @return integer New display order, or false on error
  3418. */
  3419. public function move_item($id, $direction)
  3420. {
  3421. $course_id = api_get_course_int_id();
  3422. if ($this->debug > 0) {
  3423. error_log('New LP - In learnpath::move_item('.$id.','.$direction.')', 0);
  3424. }
  3425. if (empty ($id) or empty ($direction)) {
  3426. return false;
  3427. }
  3428. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3429. $sql_sel = "SELECT *
  3430. FROM ".$tbl_lp_item."
  3431. WHERE c_id = ".$course_id." AND id = ".$id;
  3432. $res_sel = Database::query($sql_sel);
  3433. // Check if elem exists.
  3434. if (Database :: num_rows($res_sel) < 1) {
  3435. return false;
  3436. }
  3437. // Gather data.
  3438. $row = Database :: fetch_array($res_sel);
  3439. $previous = $row['previous_item_id'];
  3440. $next = $row['next_item_id'];
  3441. $display = $row['display_order'];
  3442. $parent = $row['parent_item_id'];
  3443. $lp = $row['lp_id'];
  3444. // Update the item (switch with previous/next one).
  3445. switch ($direction) {
  3446. case 'up' :
  3447. if ($this->debug > 2) {
  3448. error_log('Movement up detected', 0);
  3449. }
  3450. if ($display <= 1) { /*do nothing*/
  3451. } else {
  3452. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3453. WHERE c_id = ".$course_id." AND id = $previous";
  3454. if ($this->debug > 2) {
  3455. error_log('Selecting previous: '.$sql_sel2, 0);
  3456. }
  3457. $res_sel2 = Database::query($sql_sel2);
  3458. if (Database :: num_rows($res_sel2) < 1) {
  3459. $previous_previous = 0;
  3460. }
  3461. // Gather data.
  3462. $row2 = Database :: fetch_array($res_sel2);
  3463. $previous_previous = $row2['previous_item_id'];
  3464. // Update previous_previous item (switch "next" with current).
  3465. if ($previous_previous != 0) {
  3466. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $id WHERE c_id = ".$course_id." AND id = $previous_previous";
  3467. if ($this->debug > 2) {
  3468. error_log($sql_upd2, 0);
  3469. }
  3470. $res_upd2 = Database::query($sql_upd2);
  3471. }
  3472. // Update previous item (switch with current).
  3473. if ($previous != 0) {
  3474. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next, previous_item_id = $id, display_order = display_order +1
  3475. WHERE c_id = ".$course_id." AND id = $previous";
  3476. if ($this->debug > 2) {
  3477. error_log($sql_upd2, 0);
  3478. }
  3479. $res_upd2 = Database::query($sql_upd2);
  3480. }
  3481. // Update current item (switch with previous).
  3482. if ($id != 0) {
  3483. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $previous, previous_item_id = $previous_previous, display_order = display_order-1
  3484. WHERE c_id = ".$course_id." AND id = $id";
  3485. if ($this->debug > 2) {
  3486. error_log($sql_upd2, 0);
  3487. }
  3488. $res_upd2 = Database::query($sql_upd2);
  3489. }
  3490. // Update next item (new previous item).
  3491. if ($next != 0) {
  3492. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3493. WHERE c_id = ".$course_id." AND id = $next";
  3494. if ($this->debug > 2) {
  3495. error_log($sql_upd2, 0);
  3496. }
  3497. $res_upd2 = Database::query($sql_upd2);
  3498. }
  3499. $display = $display - 1;
  3500. }
  3501. break;
  3502. case 'down' :
  3503. if ($this->debug > 2) {
  3504. error_log('Movement down detected', 0);
  3505. }
  3506. if ($next == 0) { /* Do nothing. */
  3507. } else {
  3508. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3509. if ($this->debug > 2) {
  3510. error_log('Selecting next: '.$sql_sel2, 0);
  3511. }
  3512. $res_sel2 = Database::query($sql_sel2);
  3513. if (Database :: num_rows($res_sel2) < 1) {
  3514. $next_next = 0;
  3515. }
  3516. // Gather data.
  3517. $row2 = Database :: fetch_array($res_sel2);
  3518. $next_next = $row2['next_item_id'];
  3519. // Update previous item (switch with current).
  3520. if ($previous != 0) {
  3521. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3522. WHERE c_id = ".$course_id." AND id = $previous";
  3523. $res_upd2 = Database::query($sql_upd2);
  3524. }
  3525. // Update current item (switch with previous).
  3526. if ($id != 0) {
  3527. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3528. WHERE c_id = ".$course_id." AND id = $id";
  3529. $res_upd2 = Database::query($sql_upd2);
  3530. }
  3531. // Update next item (new previous item).
  3532. if ($next != 0) {
  3533. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3534. WHERE c_id = ".$course_id." AND id = $next";
  3535. $res_upd2 = Database::query($sql_upd2);
  3536. }
  3537. // Update next_next item (switch "previous" with current).
  3538. if ($next_next != 0) {
  3539. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $id
  3540. WHERE c_id = ".$course_id." AND id = $next_next";
  3541. $res_upd2 = Database::query($sql_upd2);
  3542. }
  3543. $display = $display + 1;
  3544. }
  3545. break;
  3546. default :
  3547. return false;
  3548. }
  3549. return $display;
  3550. }
  3551. /**
  3552. * Move a learnpath up (display_order)
  3553. * @param integer Learnpath ID
  3554. */
  3555. public function move_up($lp_id)
  3556. {
  3557. $course_id = api_get_course_int_id();
  3558. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3559. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3560. $res = Database::query($sql);
  3561. if ($res === false) {
  3562. return false;
  3563. }
  3564. $lps = array();
  3565. $lp_order = array();
  3566. $num = Database :: num_rows($res);
  3567. // First check the order is correct, globally (might be wrong because
  3568. // of versions < 1.8.4)
  3569. if ($num > 0) {
  3570. $i = 1;
  3571. while ($row = Database :: fetch_array($res)) {
  3572. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3573. $need_fix = true;
  3574. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3575. $res_u = Database::query($sql_u);
  3576. }
  3577. $row['display_order'] = $i;
  3578. $lps[$row['id']] = $row;
  3579. $lp_order[$i] = $row['id'];
  3580. $i++;
  3581. }
  3582. }
  3583. if ($num > 1) { // If there's only one element, no need to sort.
  3584. $order = $lps[$lp_id]['display_order'];
  3585. if ($order > 1) { // If it's the first element, no need to move up.
  3586. $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE c_id = ".$course_id." AND id = ".$lp_order[$order - 1];
  3587. $res_u1 = Database::query($sql_u1);
  3588. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order - 1)." WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3589. $res_u2 = Database::query($sql_u2);
  3590. }
  3591. }
  3592. }
  3593. /**
  3594. * Move a learnpath down (display_order)
  3595. * @param integer Learnpath ID
  3596. */
  3597. public function move_down($lp_id)
  3598. {
  3599. $course_id = api_get_course_int_id();
  3600. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3601. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3602. $res = Database::query($sql);
  3603. if ($res === false) {
  3604. return false;
  3605. }
  3606. $lps = array();
  3607. $lp_order = array();
  3608. $num = Database :: num_rows($res);
  3609. $max = 0;
  3610. // First check the order is correct, globally (might be wrong because
  3611. // of versions < 1.8.4).
  3612. if ($num > 0) {
  3613. $i = 1;
  3614. while ($row = Database :: fetch_array($res)) {
  3615. $max = $i;
  3616. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3617. $need_fix = true;
  3618. $sql_u = "UPDATE $lp_table SET display_order = $i
  3619. WHERE c_id = ".$course_id." AND id = ".$row['id'];
  3620. $res_u = Database::query($sql_u);
  3621. }
  3622. $row['display_order'] = $i;
  3623. $lps[$row['id']] = $row;
  3624. $lp_order[$i] = $row['id'];
  3625. $i++;
  3626. }
  3627. }
  3628. if ($num > 1) { // If there's only one element, no need to sort.
  3629. $order = $lps[$lp_id]['display_order'];
  3630. if ($order < $max) { // If it's the first element, no need to move up.
  3631. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3632. WHERE c_id = ".$course_id." AND id = ".$lp_order[$order + 1];
  3633. $res_u1 = Database::query($sql_u1);
  3634. $sql_u2 = "UPDATE $lp_table SET display_order = ".($order + 1)."
  3635. WHERE c_id = ".$course_id." AND id = ".$lp_id;
  3636. $res_u2 = Database::query($sql_u2);
  3637. }
  3638. }
  3639. }
  3640. /**
  3641. * Updates learnpath attributes to point to the next element
  3642. * The last part is similar to set_current_item but processing the other way around
  3643. */
  3644. public function next()
  3645. {
  3646. if ($this->debug > 0) {
  3647. error_log('New LP - In learnpath::next()', 0);
  3648. }
  3649. $this->last = $this->get_current_item_id();
  3650. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3651. $this->autocomplete_parents($this->last);
  3652. $new_index = $this->get_next_index();
  3653. if ($this->debug > 2) {
  3654. error_log('New LP - New index: '.$new_index, 0);
  3655. }
  3656. $this->index = $new_index;
  3657. if ($this->debug > 2) {
  3658. error_log('New LP - Now having orderedlist['.$new_index.'] = '.$this->ordered_items[$new_index], 0);
  3659. }
  3660. $this->current = $this->ordered_items[$new_index];
  3661. if ($this->debug > 2) {
  3662. error_log('New LP - new item id is '.$this->current.'-'.$this->get_current_item_id(), 0);
  3663. }
  3664. }
  3665. /**
  3666. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3667. * class, this might be redefined to allow several behaviours depending on the document type.
  3668. * @param integer Resource ID
  3669. * @return boolean True on success, false otherwise
  3670. */
  3671. public function open($id)
  3672. {
  3673. if ($this->debug > 0) {
  3674. error_log('New LP - In learnpath::open()', 0);
  3675. }
  3676. // TODO:
  3677. // set the current resource attribute to this resource
  3678. // switch on element type (redefine in child class?)
  3679. // set status for this item to "opened"
  3680. // start timer
  3681. // initialise score
  3682. $this->index = 0; //or = the last item seen (see $this->last)
  3683. }
  3684. /**
  3685. * Check that all prerequisites are fulfilled. Returns true and an empty string on succes, returns false
  3686. * and the prerequisite string on error.
  3687. * This function is based on the rules for aicc_script language as described in the SCORM 1.2 CAM documentation page 108.
  3688. * @param integer Optional item ID. If none given, uses the current open item.
  3689. * @return boolean True if prerequisites are matched, false otherwise - Empty string if true returned, prerequisites string otherwise.
  3690. */
  3691. public function prerequisites_match($item = null)
  3692. {
  3693. if ($this->debug > 0) {
  3694. error_log('In learnpath::prerequisites_match()', 0);
  3695. }
  3696. if (empty($item)) {
  3697. $item = $this->current;
  3698. }
  3699. if (isset($this->items[$item]) && is_object($this->items[$item])) {
  3700. if ($this->type == 2) {
  3701. //Getting prereq from scorm
  3702. $prereq_string = $this->get_scorm_prereq_string($item);
  3703. } else {
  3704. $prereq_string = $this->items[$item]->get_prereq_string();
  3705. }
  3706. if (empty($prereq_string)) {
  3707. return true;
  3708. }
  3709. // Clean spaces.
  3710. $prereq_string = str_replace(' ', '', $prereq_string);
  3711. if ($this->debug > 0) {
  3712. error_log('Found prereq_string: '.$prereq_string, 0);
  3713. }
  3714. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3715. $result = $this->items[$item]->parse_prereq(
  3716. $prereq_string,
  3717. $this->items,
  3718. $this->refs_list,
  3719. $this->get_user_id()
  3720. );
  3721. if ($result === false) {
  3722. $this->set_error_msg($this->items[$item]->prereq_alert);
  3723. }
  3724. } else {
  3725. $result = true;
  3726. if ($this->debug > 1) {
  3727. error_log('$this->items['.$item.'] was not an object', 0);
  3728. }
  3729. }
  3730. if ($this->debug > 1) {
  3731. error_log('End of prerequisites_match(). Error message is now '.$this->error, 0);
  3732. }
  3733. return $result;
  3734. }
  3735. /**
  3736. * Updates learnpath attributes to point to the previous element
  3737. * The last part is similar to set_current_item but processing the other way around
  3738. */
  3739. public function previous()
  3740. {
  3741. if ($this->debug > 0) {
  3742. error_log('New LP - In learnpath::previous()', 0);
  3743. }
  3744. $this->last = $this->get_current_item_id();
  3745. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3746. $this->autocomplete_parents($this->last);
  3747. $new_index = $this->get_previous_index();
  3748. $this->index = $new_index;
  3749. $this->current = $this->ordered_items[$new_index];
  3750. }
  3751. /**
  3752. * Publishes a learnpath. This basically means show or hide the learnpath
  3753. * to normal users.
  3754. * Can be used as abstract
  3755. * @param integer Learnpath ID
  3756. * @param string New visibility
  3757. */
  3758. public function toggle_visibility($lp_id, $set_visibility = 1)
  3759. {
  3760. //if ($this->debug > 0) { error_log('New LP - In learnpath::toggle_visibility()', 0); }
  3761. $action = 'visible';
  3762. if ($set_visibility != 1) {
  3763. $action = 'invisible';
  3764. }
  3765. return api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $lp_id, $action, api_get_user_id());
  3766. }
  3767. /**
  3768. * Publishes a learnpath. This basically means show or hide the learnpath
  3769. * on the course homepage
  3770. * Can be used as abstract
  3771. * @param integer Learnpath id
  3772. * @param string New visibility (v/s - visible/invisible)
  3773. */
  3774. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3775. {
  3776. $course_id = api_get_course_int_id();
  3777. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3778. $lp_id = Database::escape_string($lp_id);
  3779. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3780. $result = Database::query($sql);
  3781. if (Database::num_rows($result)) {
  3782. $row = Database :: fetch_array($result);
  3783. $name = Text::domesticate($row['name']);
  3784. if ($set_visibility == 'i') {
  3785. $s = $name." ".get_lang('LearnpathNotPublished');
  3786. $dialogBox = $s;
  3787. $v = 0;
  3788. }
  3789. if ($set_visibility == 'v') {
  3790. $s = $name." ".get_lang('LearnpathPublished');
  3791. $dialogBox = $s;
  3792. $v = 1;
  3793. }
  3794. } else {
  3795. return false;
  3796. }
  3797. $session_id = api_get_session_id();
  3798. $session_condition = api_get_session_condition($session_id);
  3799. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3800. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3801. $linkNoSessionId = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id;
  3802. $sql = "SELECT * FROM $tbl_tool WHERE c_id = ".$course_id." AND link='$link' and image='scormbuilder.gif' and link LIKE '$link%' $session_condition";
  3803. $result = Database::query($sql);
  3804. $num = Database :: num_rows($result);
  3805. if (($set_visibility == 'i') && ($num > 0)) {
  3806. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3807. Database::query($sql);
  3808. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$linkNoSessionId' and image='scormbuilder.gif' $session_condition)";
  3809. Database::query($sql);
  3810. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3811. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3812. ($course_id, '$name','$link','scormbuilder.gif','$v','0','pastillegris.gif',0, $session_id)";
  3813. Database::query($sql);
  3814. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3815. $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
  3816. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3817. Database::query($sql);
  3818. } else {
  3819. // Parameter and database incompatible, do nothing, exit.
  3820. return false;
  3821. }
  3822. }
  3823. /**
  3824. * Restart the whole learnpath. Return the URL of the first element.
  3825. * Make sure the results are saved with anoter method. This method should probably be
  3826. * redefined in children classes.
  3827. * To use a similar method statically, use the create_new_attempt() method
  3828. * @return string URL to load in the viewer
  3829. */
  3830. public function restart()
  3831. {
  3832. if ($this->debug > 0) {
  3833. error_log('New LP - In learnpath::restart()', 0);
  3834. }
  3835. // TODO
  3836. // Call autosave method to save the current progress.
  3837. //$this->index = 0;
  3838. $session_id = api_get_session_id();
  3839. $course_id = api_get_course_int_id();
  3840. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3841. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) ".
  3842. "VALUES ($course_id, ".$this->lp_id.",".$this->get_user_id().",".($this->attempt + 1).", $session_id)";
  3843. if ($this->debug > 2) {
  3844. error_log('New LP - Inserting new lp_view for restart: '.$sql, 0);
  3845. }
  3846. $res = Database::query($sql);
  3847. if ($view_id = Database :: insert_id($res)) {
  3848. $this->lp_view_id = $view_id;
  3849. $this->attempt = $this->attempt + 1;
  3850. } else {
  3851. $this->error = 'Could not insert into item_view table...';
  3852. return false;
  3853. }
  3854. $this->autocomplete_parents($this->current);
  3855. foreach ($this->items as $index => $dummy) {
  3856. $this->items[$index]->restart();
  3857. $this->items[$index]->set_lp_view($this->lp_view_id);
  3858. }
  3859. $this->first();
  3860. return true;
  3861. }
  3862. /**
  3863. * Saves the current item
  3864. * @return boolean
  3865. */
  3866. public function save_current()
  3867. {
  3868. if ($this->debug > 0) {
  3869. error_log('learnpath::save_current()', 0);
  3870. }
  3871. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3872. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3873. if ($this->debug > 2) {
  3874. error_log('New LP - save_current() saving item '.$this->current, 0);
  3875. }
  3876. if ($this->debug > 2) {
  3877. error_log(''.print_r($this->items, true), 0);
  3878. }
  3879. if (is_object($this->items[$this->current])) {
  3880. //$res = $this->items[$this->current]->save(false);
  3881. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3882. $this->autocomplete_parents($this->current);
  3883. $status = $this->items[$this->current]->get_status();
  3884. $this->append_message('new_item_status: '.$status);
  3885. $this->update_queue[$this->current] = $status;
  3886. return $res;
  3887. }
  3888. return false;
  3889. }
  3890. /**
  3891. * Saves the given item
  3892. * @param integer Item ID. Optional (will take from $_REQUEST if null)
  3893. * @param boolean Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3894. * @return boolean
  3895. */
  3896. public function save_item($item_id = null, $from_outside = true)
  3897. {
  3898. $debug = $this->debug;
  3899. if ($debug) {
  3900. error_log('In learnpath::save_item('.$item_id.','.intval($from_outside).')', 0);
  3901. }
  3902. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3903. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3904. if (empty($item_id)) {
  3905. $item_id = intval($_REQUEST['id']);
  3906. }
  3907. if (empty($item_id)) {
  3908. $item_id = $this->get_current_item_id();
  3909. }
  3910. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3911. if ($debug) {
  3912. error_log('Object exists');
  3913. }
  3914. $res = $this->items[$item_id]->save($from_outside, $this->prerequisites_match($item_id));
  3915. if ($debug) {
  3916. error_log('update_queue before:');
  3917. error_log(print_r($this->update_queue, 1));
  3918. }
  3919. $this->autocomplete_parents($item_id);
  3920. $status = $this->items[$item_id]->get_status();
  3921. $this->update_queue[$item_id] = $status;
  3922. if ($debug) {
  3923. error_log('get_status(): '.$status);
  3924. error_log('update_queue after:');
  3925. error_log(print_r($this->update_queue, 1));
  3926. }
  3927. return $res;
  3928. }
  3929. return false;
  3930. }
  3931. /**
  3932. * Saves the last item seen's ID only in case
  3933. */
  3934. public function save_last()
  3935. {
  3936. $course_id = api_get_course_int_id();
  3937. if ($this->debug > 0) {
  3938. error_log('New LP - In learnpath::save_last()', 0);
  3939. }
  3940. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3941. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3942. if (isset($this->current)) {
  3943. if ($this->debug > 2) {
  3944. error_log('New LP - Saving current item ('.$this->current.') for later review', 0);
  3945. }
  3946. $sql = "UPDATE $table SET last_item = ".Database::escape_string($this->get_current_item_id())."
  3947. WHERE c_id = $course_id AND
  3948. lp_id = ".$this->get_id()." AND
  3949. user_id = ".$this->get_user_id()." ".$session_condition;
  3950. if ($this->debug > 2) {
  3951. error_log('New LP - Saving last item seen : '.$sql, 0);
  3952. }
  3953. $res = Database::query($sql);
  3954. }
  3955. // Save progress.
  3956. list($progress, $text) = $this->get_progress_bar_text('%');
  3957. if ($progress >= 0 && $progress <= 100) {
  3958. $progress = (int)$progress;
  3959. $sql = "UPDATE $table SET progress = $progress
  3960. WHERE c_id = ".$course_id." AND
  3961. lp_id = ".$this->get_id()." AND
  3962. user_id = ".$this->get_user_id()." ".$session_condition;
  3963. $res = Database::query($sql); // Ignore errors as some tables might not have the progress field just yet.
  3964. $this->progress_db = $progress;
  3965. }
  3966. }
  3967. /**
  3968. * Sets the current item ID (checks if valid and authorized first)
  3969. * @param integer New item ID. If not given or not authorized, defaults to current
  3970. */
  3971. public function set_current_item($item_id = null)
  3972. {
  3973. if ($this->debug > 0) {
  3974. error_log('New LP - In learnpath::set_current_item('.$item_id.')', 0);
  3975. }
  3976. if (empty ($item_id)) {
  3977. if ($this->debug > 2) {
  3978. error_log('New LP - No new current item given, ignore...', 0);
  3979. }
  3980. // Do nothing.
  3981. } else {
  3982. if ($this->debug > 2) {
  3983. error_log('New LP - New current item given is '.$item_id.'...', 0);
  3984. }
  3985. if (is_numeric($item_id)) {
  3986. $item_id = Database::escape_string($item_id);
  3987. // TODO: Check in database here.
  3988. $this->last = $this->current;
  3989. $this->current = $item_id;
  3990. // TODO: Update $this->index as well.
  3991. foreach ($this->ordered_items as $index => $item) {
  3992. if ($item == $this->current) {
  3993. $this->index = $index;
  3994. break;
  3995. }
  3996. }
  3997. if ($this->debug > 2) {
  3998. error_log('New LP - set_current_item('.$item_id.') done. Index is now : '.$this->index, 0);
  3999. }
  4000. } else {
  4001. error_log('New LP - set_current_item('.$item_id.') failed. Not a numeric value: ', 0);
  4002. }
  4003. }
  4004. }
  4005. /**
  4006. * Sets the encoding
  4007. * @param string New encoding
  4008. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4009. */
  4010. public function set_encoding($enc = 'UTF-8')
  4011. {
  4012. if ($this->debug > 0) {
  4013. error_log('New LP - In learnpath::set_encoding()', 0);
  4014. }
  4015. $course_id = api_get_course_int_id();
  4016. /* // Deprecated code (Chamilo 1.8.8).
  4017. $enc = strtoupper($enc);
  4018. $encodings = array (
  4019. 'UTF-8',
  4020. 'ISO-8859-1',
  4021. 'ISO-8859-15',
  4022. 'cp1251',
  4023. 'cp1252',
  4024. 'KOI8-R',
  4025. 'BIG5',
  4026. 'GB2312',
  4027. 'Shift_JIS',
  4028. 'EUC-JP',
  4029. ''
  4030. );
  4031. if (in_array($enc, $encodings)) { // TODO: Incorrect comparison, fix it.
  4032. $lp = $this->get_id();
  4033. if ($lp != 0) {
  4034. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4035. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE id = " . $lp;
  4036. $res = Database::query($sql);
  4037. return $res;
  4038. }
  4039. }
  4040. return false;
  4041. */
  4042. $enc = api_refine_encoding_id($enc);
  4043. if (empty($enc)) {
  4044. $enc = api_get_system_encoding();
  4045. }
  4046. if (api_is_encoding_supported($enc)) {
  4047. $lp = $this->get_id();
  4048. if ($lp != 0) {
  4049. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4050. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = ".$lp;
  4051. $res = Database::query($sql);
  4052. return $res;
  4053. }
  4054. }
  4055. return false;
  4056. }
  4057. /**
  4058. * Sets the JS lib setting in the database directly.
  4059. * This is the JavaScript library file this lp needs to load on startup
  4060. * @param string Proximity setting
  4061. * @return boolean True on update success. False otherwise.
  4062. */
  4063. public function set_jslib($lib = '')
  4064. {
  4065. if ($this->debug > 0) {
  4066. error_log('New LP - In learnpath::set_jslib()', 0);
  4067. }
  4068. $lp = $this->get_id();
  4069. $course_id = api_get_course_int_id();
  4070. if ($lp != 0) {
  4071. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4072. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = ".$lp;
  4073. $res = Database::query($sql);
  4074. return $res;
  4075. } else {
  4076. return false;
  4077. }
  4078. }
  4079. /**
  4080. * Sets the name of the LP maker (publisher) (and save)
  4081. * @param string Optional string giving the new content_maker of this learnpath
  4082. * @return boolean True
  4083. */
  4084. public function set_maker($name = '')
  4085. {
  4086. if ($this->debug > 0) {
  4087. error_log('New LP - In learnpath::set_maker()', 0);
  4088. }
  4089. if (empty ($name)) {
  4090. return false;
  4091. }
  4092. $this->maker = Database::escape_string($name);
  4093. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4094. $course_id = api_get_course_int_id();
  4095. $lp_id = $this->get_id();
  4096. $sql = "UPDATE $lp_table SET content_maker = '".$this->maker."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4097. if ($this->debug > 2) {
  4098. error_log('New LP - lp updated with new content_maker : '.$this->maker, 0);
  4099. }
  4100. Database::query($sql);
  4101. return true;
  4102. }
  4103. /**
  4104. * Sets the name of the current learnpath (and save)
  4105. * @param string Optional string giving the new name of this learnpath
  4106. * @return boolean True/False
  4107. */
  4108. public function set_name($name = null)
  4109. {
  4110. if ($this->debug > 0) {
  4111. error_log('New LP - In learnpath::set_name()', 0);
  4112. }
  4113. if (empty($name)) {
  4114. return false;
  4115. }
  4116. $this->name = Database::escape_string($name);
  4117. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4118. $lp_id = $this->get_id();
  4119. $course_id = api_get_course_int_id();
  4120. $sql = "UPDATE $lp_table SET name = '".$this->name."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4121. if ($this->debug > 2) {
  4122. error_log('New LP - lp updated with new name : '.$this->name, 0);
  4123. }
  4124. Database::query($sql);
  4125. // If the lp is visible on the homepage, change his name there.
  4126. if (Database::affected_rows()) {
  4127. $session_id = api_get_session_id();
  4128. $session_condition = api_get_session_condition($session_id);
  4129. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4130. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  4131. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4132. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4133. Database::query($sql);
  4134. return true;
  4135. } else {
  4136. return false;
  4137. }
  4138. }
  4139. /**
  4140. * Set index specified prefix terms for all items in this path
  4141. * @param string Comma-separated list of terms
  4142. * @param char Xapian term prefix
  4143. * @return boolean False on error, true otherwise
  4144. */
  4145. public function set_terms_by_prefix($terms_string, $prefix)
  4146. {
  4147. $course_id = api_get_course_int_id();
  4148. if (api_get_setting('search_enabled') !== 'true') {
  4149. return false;
  4150. }
  4151. if (!extension_loaded('xapian')) {
  4152. return false;
  4153. }
  4154. $terms_string = trim($terms_string);
  4155. $terms = explode(',', $terms_string);
  4156. array_walk($terms, 'trim_value');
  4157. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4158. // Don't do anything if no change, verify only at DB, not the search engine.
  4159. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0)) {
  4160. return false;
  4161. }
  4162. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4163. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4164. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4165. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4166. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4167. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4168. $lp_id = intval($_POST['lp_id']);
  4169. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4170. $result = Database::query($sql);
  4171. $di = new ChamiloIndexer();
  4172. while ($lp_item = Database :: fetch_array($result)) {
  4173. // Get search_did.
  4174. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4175. $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';
  4176. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4177. //echo $sql; echo '<br>';
  4178. $res = Database::query($sql);
  4179. if (Database::num_rows($res) > 0) {
  4180. $se_ref = Database :: fetch_array($res);
  4181. // Compare terms.
  4182. $doc = $di->get_document($se_ref['search_did']);
  4183. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4184. $xterms = array();
  4185. foreach ($xapian_terms as $xapian_term) {
  4186. $xterms[] = substr($xapian_term['name'], 1);
  4187. }
  4188. $dterms = $terms;
  4189. $missing_terms = array_diff($dterms, $xterms);
  4190. $deprecated_terms = array_diff($xterms, $dterms);
  4191. // Save it to search engine.
  4192. foreach ($missing_terms as $term) {
  4193. $doc->add_term($prefix.$term, 1);
  4194. }
  4195. foreach ($deprecated_terms as $term) {
  4196. $doc->remove_term($prefix.$term);
  4197. }
  4198. $di->getDb()->replace_document((int)$se_ref['search_did'], $doc);
  4199. $di->getDb()->flush();
  4200. } else {
  4201. //@todo What we should do here?
  4202. }
  4203. }
  4204. return true;
  4205. }
  4206. /**
  4207. * Sets the theme of the LP (local/remote) (and save)
  4208. * @param string Optional string giving the new theme of this learnpath
  4209. * @return bool Returns true if theme name is not empty
  4210. */
  4211. public function set_theme($name = '')
  4212. {
  4213. $course_id = api_get_course_int_id();
  4214. if ($this->debug > 0) {
  4215. error_log('New LP - In learnpath::set_theme()', 0);
  4216. }
  4217. $this->theme = Database::escape_string($name);
  4218. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4219. $lp_id = $this->get_id();
  4220. $sql = "UPDATE $lp_table SET theme = '".$this->theme."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4221. if ($this->debug > 2) {
  4222. error_log('New LP - lp updated with new theme : '.$this->theme, 0);
  4223. }
  4224. Database::query($sql);
  4225. return true;
  4226. }
  4227. /**
  4228. * Sets the image of an LP (and save)
  4229. * @param string Optional string giving the new image of this learnpath
  4230. * @return bool Returns true if theme name is not empty
  4231. */
  4232. public function set_preview_image($name = '')
  4233. {
  4234. $course_id = api_get_course_int_id();
  4235. if ($this->debug > 0) {
  4236. error_log('New LP - In learnpath::set_preview_image()', 0);
  4237. }
  4238. $this->preview_image = Database::escape_string($name);
  4239. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4240. $lp_id = $this->get_id();
  4241. $sql = "UPDATE $lp_table SET preview_image = '".$this->preview_image."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4242. if ($this->debug > 2) {
  4243. error_log('New LP - lp updated with new preview image : '.$this->preview_image, 0);
  4244. }
  4245. Database::query($sql);
  4246. return true;
  4247. }
  4248. /**
  4249. * Sets the author of a LP (and save)
  4250. * @param string Optional string giving the new author of this learnpath
  4251. * @return bool Returns true if author's name is not empty
  4252. */
  4253. public function set_author($name = '')
  4254. {
  4255. $course_id = api_get_course_int_id();
  4256. if ($this->debug > 0) {
  4257. error_log('New LP - In learnpath::set_author()', 0);
  4258. }
  4259. $this->author = Database::escape_string($name);
  4260. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4261. $lp_id = $this->get_id();
  4262. $sql = "UPDATE $lp_table SET author = '".$this->author."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4263. if ($this->debug > 2) {
  4264. error_log('New LP - lp updated with new preview author : '.$this->author, 0);
  4265. }
  4266. Database::query($sql);
  4267. return true;
  4268. }
  4269. /**
  4270. * Sets the hide_toc_frame parameter of a LP (and save)
  4271. * @param int 1 if frame is hiddent 0 thenelse
  4272. * @return bool Returns true if author's name is not empty
  4273. */
  4274. public function set_hide_toc_frame($hide)
  4275. {
  4276. $course_id = api_get_course_int_id();
  4277. if ($this->debug > 0) {
  4278. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4279. }
  4280. if (intval($hide) == $hide) {
  4281. $this->hide_toc_frame = $hide;
  4282. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4283. $lp_id = $this->get_id();
  4284. $sql = "UPDATE $lp_table SET hide_toc_frame = '".$this->hide_toc_frame."'
  4285. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4286. if ($this->debug > 2) {
  4287. error_log('New LP - lp updated with new preview hide_toc_frame : '.$this->author, 0);
  4288. }
  4289. Database::query($sql);
  4290. return true;
  4291. } else {
  4292. return false;
  4293. }
  4294. }
  4295. /**
  4296. * Sets the prerequisite of a LP (and save)
  4297. * @param int integer giving the new prerequisite of this learnpath
  4298. * @return bool returns true if prerequisite is not empty
  4299. */
  4300. public function set_prerequisite($prerequisite)
  4301. {
  4302. $course_id = api_get_course_int_id();
  4303. if ($this->debug > 0) {
  4304. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4305. }
  4306. $this->prerequisite = intval($prerequisite);
  4307. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4308. $lp_id = $this->get_id();
  4309. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4310. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4311. if ($this->debug > 2) {
  4312. error_log('New LP - lp updated with new preview requisite : '.$this->requisite, 0);
  4313. }
  4314. Database::query($sql);
  4315. return true;
  4316. }
  4317. /**
  4318. * Sets the location/proximity of the LP (local/remote) (and save)
  4319. * @param string Optional string giving the new location of this learnpath
  4320. * @return boolean True on success / False on error
  4321. */
  4322. public function set_proximity($name = '')
  4323. {
  4324. $course_id = api_get_course_int_id();
  4325. if ($this->debug > 0) {
  4326. error_log('New LP - In learnpath::set_proximity()', 0);
  4327. }
  4328. if (empty ($name)) {
  4329. return false;
  4330. }
  4331. $this->proximity = Database::escape_string($name);
  4332. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4333. $lp_id = $this->get_id();
  4334. $sql = "UPDATE $lp_table SET content_local = '".$this->proximity."'
  4335. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4336. if ($this->debug > 2) {
  4337. error_log('New LP - lp updated with new proximity : '.$this->proximity, 0);
  4338. }
  4339. Database::query($sql);
  4340. return true;
  4341. }
  4342. /**
  4343. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4344. * @param integer DB ID of the item
  4345. */
  4346. public function set_previous_item($id)
  4347. {
  4348. if ($this->debug > 0) {
  4349. error_log('New LP - In learnpath::set_previous_item()', 0);
  4350. }
  4351. $this->last = $id;
  4352. }
  4353. public function get_max_attempts()
  4354. {
  4355. return $this->max_attempts;
  4356. }
  4357. public function get_subscribe_users()
  4358. {
  4359. return $this->subscribe_users;
  4360. }
  4361. public function check_attempts()
  4362. {
  4363. $max_attempts = $this->get_max_attempts();
  4364. switch ($max_attempts) {
  4365. case 0: //unlimited
  4366. return true;
  4367. break;
  4368. case $max_attempts >= 1:
  4369. if ($this->attempt <= $max_attempts) {
  4370. return true;
  4371. }
  4372. break;
  4373. }
  4374. return false;
  4375. }
  4376. function check_item_attempts($item_id)
  4377. {
  4378. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  4379. $count = $this->items[$item_id]->get_view_count();
  4380. $max_attempts = $this->get_max_attempts();
  4381. switch ($max_attempts) {
  4382. case 0: //unlimited
  4383. return true;
  4384. break;
  4385. case $max_attempts >= 1:
  4386. if ($count <= $max_attempts) {
  4387. return true;
  4388. }
  4389. break;
  4390. }
  4391. return false;
  4392. }
  4393. }
  4394. public function set_max_attempts($attempts)
  4395. {
  4396. $mode = 'multiple';
  4397. if ($attempts == 1) {
  4398. $mode = 'single';
  4399. }
  4400. $this->max_attempts = intval($attempts);
  4401. $lp_id = $this->get_id();
  4402. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4403. $sql = "UPDATE $lp_table SET max_attempts = '".$this->max_attempts."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4404. Database::query($sql);
  4405. $this->set_attempt_mode($mode);
  4406. }
  4407. /**
  4408. * Sets subscribe_users
  4409. * @param string $value Optional string giving the new location of this learnpath
  4410. * @return bool True on success / False on error
  4411. */
  4412. public function set_subscribe_users($value = 0)
  4413. {
  4414. if ($this->debug > 0) {
  4415. error_log('New LP - In learnpath::set_subscribe_users()', 0);
  4416. }
  4417. $this->subscribe_users = intval($value);;
  4418. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4419. $lp_id = $this->get_id();
  4420. $sql = "UPDATE $lp_table SET subscribe_users = '".$this->subscribe_users."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4421. if ($this->debug > 2) {
  4422. error_log('New LP - lp updated with new set_subscribe_users : '.$this->subscribe_users, 0);
  4423. }
  4424. Database::query($sql);
  4425. return true;
  4426. }
  4427. /**
  4428. * Sets use_max_score
  4429. * @param string Optional string giving the new location of this learnpath
  4430. * @return boolean True on success / False on error
  4431. */
  4432. public function set_use_max_score($use_max_score = 1)
  4433. {
  4434. if ($this->debug > 0) {
  4435. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4436. }
  4437. $this->use_max_score = intval($use_max_score);
  4438. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4439. $lp_id = $this->get_id();
  4440. $sql = "UPDATE $lp_table SET use_max_score = '".$this->use_max_score."' WHERE c_id = ".$this->course_int_id." AND id = '$lp_id'";
  4441. if ($this->debug > 2) {
  4442. error_log('New LP - lp updated with new use_max_score : '.$this->use_max_score, 0);
  4443. }
  4444. Database::query($sql);
  4445. return true;
  4446. }
  4447. /**
  4448. * Sets and saves the expired_on date
  4449. * @param string Optional string giving the new author of this learnpath
  4450. * @return bool Returns true if author's name is not empty
  4451. */
  4452. public function set_expired_on($expired_on)
  4453. {
  4454. $course_id = api_get_course_int_id();
  4455. if ($this->debug > 0) {
  4456. error_log('New LP - In learnpath::set_expired_on()', 0);
  4457. }
  4458. if (!empty($expired_on)) {
  4459. $this->expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  4460. } else {
  4461. $this->expired_on = '';
  4462. }
  4463. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4464. $lp_id = $this->get_id();
  4465. $sql = "UPDATE $lp_table SET expired_on = '".$this->expired_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4466. if ($this->debug > 2) {
  4467. error_log('New LP - lp updated with new expired_on : '.$this->expired_on, 0);
  4468. }
  4469. Database::query($sql);
  4470. return true;
  4471. }
  4472. /**
  4473. * Sets and saves the publicated_on date
  4474. * @param string Optional string giving the new author of this learnpath
  4475. * @return bool Returns true if author's name is not empty
  4476. */
  4477. public function set_publicated_on($publicated_on)
  4478. {
  4479. $course_id = api_get_course_int_id();
  4480. if ($this->debug > 0) {
  4481. error_log('New LP - In learnpath::set_expired_on()', 0);
  4482. }
  4483. if (!empty($publicated_on)) {
  4484. $this->publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  4485. } else {
  4486. $this->publicated_on = '';
  4487. }
  4488. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4489. $lp_id = $this->get_id();
  4490. $sql = "UPDATE $lp_table SET publicated_on = '".$this->publicated_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4491. if ($this->debug > 2) {
  4492. error_log('New LP - lp updated with new publicated_on : '.$this->publicated_on, 0);
  4493. }
  4494. Database::query($sql);
  4495. return true;
  4496. }
  4497. public function set_category_id($category_id)
  4498. {
  4499. $this->category_id = $category_id;
  4500. $course_id = api_get_course_int_id();
  4501. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4502. $lp_id = $this->get_id();
  4503. $sql = "UPDATE $lp_table SET category_id = '".intval(
  4504. $category_id
  4505. )."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4506. Database::query($sql);
  4507. return true;
  4508. }
  4509. /**
  4510. * Sets and saves the expired_on date
  4511. * @param string Optional string giving the new author of this learnpath
  4512. * @return bool Returns true if author's name is not empty
  4513. */
  4514. public function set_modified_on()
  4515. {
  4516. $course_id = api_get_course_int_id();
  4517. if ($this->debug > 0) {
  4518. error_log('New LP - In learnpath::set_expired_on()', 0);
  4519. }
  4520. $this->modified_on = api_get_utc_datetime();
  4521. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4522. $lp_id = $this->get_id();
  4523. $sql = "UPDATE $lp_table SET modified_on = '".$this->modified_on."' WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4524. if ($this->debug > 2) {
  4525. error_log('New LP - lp updated with new expired_on : '.$this->modified_on, 0);
  4526. }
  4527. Database::query($sql);
  4528. return true;
  4529. }
  4530. /**
  4531. * Sets the object's error message
  4532. * @param string Error message. If empty, reinits the error string
  4533. * @return void
  4534. */
  4535. public function set_error_msg($error = '')
  4536. {
  4537. if ($this->debug > 0) {
  4538. error_log('New LP - In learnpath::set_error_msg()', 0);
  4539. }
  4540. if (empty ($error)) {
  4541. $this->error = '';
  4542. } else {
  4543. $this->error .= $error;
  4544. }
  4545. }
  4546. /**
  4547. * Launches the current item if not 'sco' (starts timer and make sure there is a record ready in the DB)
  4548. * @param boolean Whether to allow a new attempt or not
  4549. * @return boolean True
  4550. */
  4551. public function start_current_item($allow_new_attempt = false)
  4552. {
  4553. if ($this->debug > 0) {
  4554. error_log('New LP - In learnpath::start_current_item()', 0);
  4555. }
  4556. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4557. $type = $this->get_type();
  4558. $item_type = $this->items[$this->current]->get_type();
  4559. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)) {
  4560. $this->items[$this->current]->open($allow_new_attempt);
  4561. $this->autocomplete_parents($this->current);
  4562. $prereq_check = $this->prerequisites_match($this->current);
  4563. $this->items[$this->current]->save(false, $prereq_check);
  4564. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4565. } else {
  4566. // If sco, then it is supposed to have been updated by some other call.
  4567. }
  4568. if ($item_type == 'sco') {
  4569. $this->items[$this->current]->restart();
  4570. }
  4571. }
  4572. if ($this->debug > 0) {
  4573. error_log('New LP - End of learnpath::start_current_item()', 0);
  4574. }
  4575. return true;
  4576. }
  4577. /**
  4578. * Stops the processing and counters for the old item (as held in $this->last)
  4579. * @return boolean True/False
  4580. */
  4581. public function stop_previous_item()
  4582. {
  4583. if ($this->debug > 0) {
  4584. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4585. }
  4586. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4587. if ($this->debug > 2) {
  4588. error_log('New LP - In learnpath::stop_previous_item() - '.$this->last.' is object', 0);
  4589. }
  4590. switch ($this->get_type()) {
  4591. case '3' :
  4592. if ($this->items[$this->last]->get_type() != 'au') {
  4593. if ($this->debug > 2) {
  4594. error_log(
  4595. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 3 is <> au',
  4596. 0
  4597. );
  4598. }
  4599. $this->items[$this->last]->close();
  4600. //$this->autocomplete_parents($this->last);
  4601. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4602. } else {
  4603. if ($this->debug > 2) {
  4604. error_log(
  4605. 'New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals',
  4606. 0
  4607. );
  4608. }
  4609. }
  4610. case '2' :
  4611. if ($this->items[$this->last]->get_type() != 'sco') {
  4612. if ($this->debug > 2) {
  4613. error_log(
  4614. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 2 is <> sco',
  4615. 0
  4616. );
  4617. }
  4618. $this->items[$this->last]->close();
  4619. //$this->autocomplete_parents($this->last);
  4620. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4621. } else {
  4622. if ($this->debug > 2) {
  4623. error_log(
  4624. 'New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals',
  4625. 0
  4626. );
  4627. }
  4628. }
  4629. break;
  4630. case '1' :
  4631. default :
  4632. if ($this->debug > 2) {
  4633. error_log(
  4634. 'New LP - In learnpath::stop_previous_item() - '.$this->last.' in lp_type 1 is asset',
  4635. 0
  4636. );
  4637. }
  4638. $this->items[$this->last]->close();
  4639. break;
  4640. }
  4641. } else {
  4642. if ($this->debug > 2) {
  4643. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4644. }
  4645. return false;
  4646. }
  4647. return true;
  4648. }
  4649. /**
  4650. * Updates the default view mode from fullscreen to embedded and inversely
  4651. * @return string The current default view mode ('fullscreen' or 'embedded')
  4652. */
  4653. public function update_default_view_mode()
  4654. {
  4655. $course_id = api_get_course_int_id();
  4656. if ($this->debug > 0) {
  4657. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4658. }
  4659. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4660. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4661. $res = Database::query($sql);
  4662. if (Database :: num_rows($res) > 0) {
  4663. $row = Database :: fetch_array($res);
  4664. $default_view_mode = $row['default_view_mod'];
  4665. $view_mode = $default_view_mode;
  4666. switch ($default_view_mode) {
  4667. case 'fullscreen':
  4668. $view_mode = 'embedded';
  4669. break;
  4670. case 'embedded':
  4671. $view_mode = 'embedframe';
  4672. break;
  4673. case 'embedframe':
  4674. $view_mode = 'impress';
  4675. break;
  4676. case 'impress':
  4677. $view_mode = 'fullscreen';
  4678. break;
  4679. }
  4680. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode' WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4681. );
  4682. $res = Database::query($sql);
  4683. $this->mode = $view_mode;
  4684. return $view_mode;
  4685. } else {
  4686. if ($this->debug > 2) {
  4687. error_log('New LP - Problem in update_default_view() - could not find LP '.$this->get_id().' in DB', 0);
  4688. }
  4689. }
  4690. return -1;
  4691. }
  4692. /**
  4693. * Updates the default behaviour about auto-commiting SCORM updates
  4694. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4695. */
  4696. public function update_default_scorm_commit()
  4697. {
  4698. $course_id = api_get_course_int_id();
  4699. if ($this->debug > 0) {
  4700. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4701. }
  4702. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4703. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4704. $res = Database::query($sql);
  4705. if (Database :: num_rows($res) > 0) {
  4706. $row = Database :: fetch_array($res);
  4707. $force = $row['force_commit'];
  4708. if ($force == 1) {
  4709. $force = 0;
  4710. $force_return = false;
  4711. } elseif ($force == 0) {
  4712. $force = 1;
  4713. $force_return = true;
  4714. }
  4715. $sql = "UPDATE $lp_table SET force_commit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4716. $res = Database::query($sql);
  4717. $this->force_commit = $force_return;
  4718. return $force_return;
  4719. } else {
  4720. if ($this->debug > 2) {
  4721. error_log(
  4722. 'New LP - Problem in update_default_scorm_commit() - could not find LP '.$this->get_id().' in DB',
  4723. 0
  4724. );
  4725. }
  4726. }
  4727. return -1;
  4728. }
  4729. /**
  4730. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4731. * @return bool True on success, false on failure
  4732. */
  4733. public function update_display_order()
  4734. {
  4735. $course_id = api_get_course_int_id();
  4736. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4737. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4738. $res = Database::query($sql);
  4739. if ($res === false) {
  4740. return false;
  4741. }
  4742. $lps = array();
  4743. $lp_order = array();
  4744. $num = Database :: num_rows($res);
  4745. // First check the order is correct, globally (might be wrong because
  4746. // of versions < 1.8.4).
  4747. if ($num > 0) {
  4748. $i = 1;
  4749. while ($row = Database :: fetch_array($res)) {
  4750. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4751. $need_fix = true;
  4752. $sql_u = "UPDATE $lp_table SET display_order = $i WHERE c_id = ".$course_id." AND id = ".$row['id'];
  4753. $res_u = Database::query($sql_u);
  4754. }
  4755. $i++;
  4756. }
  4757. }
  4758. return true;
  4759. }
  4760. /**
  4761. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4762. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4763. */
  4764. public function update_reinit()
  4765. {
  4766. $course_id = api_get_course_int_id();
  4767. if ($this->debug > 0) {
  4768. error_log('New LP - In learnpath::update_reinit()', 0);
  4769. }
  4770. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4771. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4772. $res = Database::query($sql);
  4773. if (Database :: num_rows($res) > 0) {
  4774. $row = Database :: fetch_array($res);
  4775. $force = $row['prevent_reinit'];
  4776. if ($force == 1) {
  4777. $force = 0;
  4778. } elseif ($force == 0) {
  4779. $force = 1;
  4780. }
  4781. $sql = "UPDATE $lp_table SET prevent_reinit = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4782. $res = Database::query($sql);
  4783. $this->prevent_reinit = $force;
  4784. return $force;
  4785. } else {
  4786. if ($this->debug > 2) {
  4787. error_log('New LP - Problem in update_reinit() - could not find LP '.$this->get_id().' in DB', 0);
  4788. }
  4789. }
  4790. return -1;
  4791. }
  4792. /**
  4793. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4794. *
  4795. * @return string 'single', 'multi' or 'seriousgame'
  4796. * @author ndiechburg <noel@cblue.be>
  4797. **/
  4798. public function get_attempt_mode()
  4799. {
  4800. if (!isset($this->seriousgame_mode)) { //Set default value for seriousgame_mode
  4801. $this->seriousgame_mode = 0;
  4802. }
  4803. if (!isset($this->prevent_reinit)) { // Set default value for prevent_reinit
  4804. $this->prevent_reinit = 1;
  4805. }
  4806. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4807. return 'seriousgame';
  4808. }
  4809. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4810. return 'single';
  4811. }
  4812. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4813. return 'multiple';
  4814. }
  4815. return 'single';
  4816. }
  4817. /**
  4818. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4819. *
  4820. * @param string 'seriousgame', 'single' or 'multiple'
  4821. * @return boolean
  4822. * @author ndiechburg <noel@cblue.be>
  4823. **/
  4824. public function set_attempt_mode($mode)
  4825. {
  4826. $course_id = api_get_course_int_id();
  4827. switch ($mode) {
  4828. case 'seriousgame' :
  4829. $sg_mode = 1;
  4830. $prevent_reinit = 1;
  4831. break;
  4832. case 'single' :
  4833. $sg_mode = 0;
  4834. $prevent_reinit = 1;
  4835. break;
  4836. case 'multiple' :
  4837. $sg_mode = 0;
  4838. $prevent_reinit = 0;
  4839. break;
  4840. default :
  4841. $sg_mode = 0;
  4842. $prevent_reinit = 0;
  4843. break;
  4844. }
  4845. $this->prevent_reinit = $prevent_reinit;
  4846. $this->seriousgame_mode = $sg_mode;
  4847. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4848. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4849. );
  4850. $res = Database::query($sql);
  4851. if ($res) {
  4852. return true;
  4853. } else {
  4854. return false;
  4855. }
  4856. }
  4857. /**
  4858. * switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4859. *
  4860. * @return boolean
  4861. * @author ndiechburg <noel@cblue.be>
  4862. **/
  4863. public function switch_attempt_mode()
  4864. {
  4865. if ($this->debug > 0) {
  4866. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4867. }
  4868. $mode = $this->get_attempt_mode();
  4869. switch ($mode) {
  4870. case 'single' :
  4871. $next_mode = 'multiple';
  4872. break;
  4873. case 'multiple' :
  4874. $next_mode = 'seriousgame';
  4875. break;
  4876. case 'seriousgame' :
  4877. $next_mode = 'single';
  4878. break;
  4879. default :
  4880. $next_mode = 'single';
  4881. break;
  4882. }
  4883. $this->set_attempt_mode($next_mode);
  4884. }
  4885. /**
  4886. * Swithc the lp in ktm mode. This is a special scorm mode with unique attempt but possibility to do again a completed item.
  4887. *
  4888. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4889. * @author ndiechburg <noel@cblue.be>
  4890. **/
  4891. public function set_seriousgame_mode()
  4892. {
  4893. $course_id = api_get_course_int_id();
  4894. if ($this->debug > 0) {
  4895. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4896. }
  4897. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4898. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4899. $res = Database::query($sql);
  4900. if (Database :: num_rows($res) > 0) {
  4901. $row = Database :: fetch_array($res);
  4902. $force = $row['seriousgame_mode'];
  4903. if ($force == 1) {
  4904. $force = 0;
  4905. } elseif ($force == 0) {
  4906. $force = 1;
  4907. }
  4908. $sql = "UPDATE $lp_table SET seriousgame_mode = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id(
  4909. );
  4910. $res = Database::query($sql);
  4911. $this->seriousgame_mode = $force;
  4912. return $force;
  4913. } else {
  4914. if ($this->debug > 2) {
  4915. error_log(
  4916. 'New LP - Problem in set_seriousgame_mode() - could not find LP '.$this->get_id().' in DB',
  4917. 0
  4918. );
  4919. }
  4920. }
  4921. return -1;
  4922. }
  4923. /**
  4924. * Updates the "scorm_debug" value that shows or hide the debug window
  4925. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4926. */
  4927. public function update_scorm_debug()
  4928. {
  4929. $course_id = api_get_course_int_id();
  4930. if ($this->debug > 0) {
  4931. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4932. }
  4933. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4934. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4935. $res = Database::query($sql);
  4936. if (Database :: num_rows($res) > 0) {
  4937. $row = Database :: fetch_array($res);
  4938. $force = $row['debug'];
  4939. if ($force == 1) {
  4940. $force = 0;
  4941. } elseif ($force == 0) {
  4942. $force = 1;
  4943. }
  4944. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = ".$this->get_id();
  4945. $res = Database::query($sql);
  4946. $this->scorm_debug = $force;
  4947. return $force;
  4948. } else {
  4949. if ($this->debug > 2) {
  4950. error_log('New LP - Problem in update_scorm_debug() - could not find LP '.$this->get_id().' in DB', 0);
  4951. }
  4952. }
  4953. return -1;
  4954. }
  4955. /**
  4956. * Function that makes a call to the function sort_tree_array and create_tree_array
  4957. * @author Kevin Van Den Haute
  4958. * @param array
  4959. */
  4960. public function tree_array($array)
  4961. {
  4962. if ($this->debug > 1) {
  4963. error_log('New LP - In learnpath::tree_array()', 0);
  4964. }
  4965. $array = $this->sort_tree_array($array);
  4966. $this->create_tree_array($array);
  4967. }
  4968. /**
  4969. * Creates an array with the elements of the learning path tree in it
  4970. *
  4971. * @author Kevin Van Den Haute
  4972. * @param array $array
  4973. * @param int $parent
  4974. * @param int $depth
  4975. * @param array $tmp
  4976. */
  4977. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array())
  4978. {
  4979. if ($this->debug > 1) {
  4980. error_log('New LP - In learnpath::create_tree_array())', 0);
  4981. }
  4982. if (is_array($array)) {
  4983. for ($i = 0; $i < count($array); $i++) {
  4984. if ($array[$i]['parent_item_id'] == $parent) {
  4985. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4986. $tmp[] = $array[$i]['parent_item_id'];
  4987. $depth++;
  4988. }
  4989. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4990. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4991. $this->arrMenu[] = array(
  4992. 'id' => $array[$i]['id'],
  4993. 'item_type' => $array[$i]['item_type'],
  4994. 'title' => $array[$i]['title'],
  4995. 'path' => $array[$i]['path'],
  4996. 'description' => $array[$i]['description'],
  4997. 'parent_item_id' => $array[$i]['parent_item_id'],
  4998. 'previous_item_id' => $array[$i]['previous_item_id'],
  4999. 'next_item_id' => $array[$i]['next_item_id'],
  5000. 'min_score' => $array[$i]['min_score'],
  5001. 'max_score' => $array[$i]['max_score'],
  5002. 'mastery_score' => $array[$i]['mastery_score'],
  5003. 'display_order' => $array[$i]['display_order'],
  5004. 'prerequisite' => $preq,
  5005. 'depth' => $depth,
  5006. 'audio' => $audio
  5007. );
  5008. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  5009. }
  5010. }
  5011. }
  5012. }
  5013. /**
  5014. * Sorts a multi dimensional array by parent id and display order
  5015. * @author Kevin Van Den Haute
  5016. *
  5017. * @param array $array (array with al the learning path items in it)
  5018. *
  5019. * @return array
  5020. */
  5021. public function sort_tree_array($array)
  5022. {
  5023. foreach ($array as $key => $row) {
  5024. $parent[$key] = $row['parent_item_id'];
  5025. $position[$key] = $row['display_order'];
  5026. }
  5027. if (count($array) > 0) {
  5028. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  5029. }
  5030. return $array;
  5031. }
  5032. /**
  5033. * Function that creates a table structure with a learning path his modules, chapters and documents.
  5034. * Also the actions for the modules, chapters and documents are in this table.
  5035. * @author Kevin Van Den Haute
  5036. * @param int $lp_id
  5037. * @return string
  5038. */
  5039. public function overview()
  5040. {
  5041. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5042. if ($this->debug > 0) {
  5043. error_log('New LP - In learnpath::overview()', 0);
  5044. }
  5045. global $_course;
  5046. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5047. $return = '';
  5048. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  5049. // we need to start a form when we want to update all the mp3 files
  5050. if ($update_audio == 'true') {
  5051. $return .= '<form action="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5052. $_GET['cidReq']
  5053. ).'&amp;updateaudio='.Security :: remove_XSS($_GET['updateaudio']).'&amp;action='.Security :: remove_XSS(
  5054. $_GET['action']
  5055. ).'&amp;lp_id='.$_SESSION['oLP']->lp_id.'" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  5056. }
  5057. $return .= '<div id="message"></div>';
  5058. $return_audio = '<table class="data_table">';
  5059. $return_audio .= '<tr>';
  5060. $return_audio .= '<th width="60%">'.get_lang('Title').'</th>';
  5061. $return_audio .= '<th>'.get_lang('Audio').'</th>';
  5062. $return_audio .= '</tr>';
  5063. if ($update_audio != 'true') {
  5064. $return .= '<div class="span12">';
  5065. $return .= self::return_new_tree($update_audio);
  5066. $return .= '</div>';
  5067. $return .= Display::div(
  5068. Display::url(get_lang('Save'), '#', array('id' => 'listSubmit', 'class' => 'btn')),
  5069. array('style' => 'float:left; margin-top:15px;width:100%')
  5070. );
  5071. } else {
  5072. $return_audio .= self::return_new_tree($update_audio);
  5073. $return .= $return_audio.'</table>';
  5074. }
  5075. // We need to close the form when we are updating the mp3 files.
  5076. if ($update_audio == 'true') {
  5077. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">'.get_lang(
  5078. 'SaveAudioAndOrganization'
  5079. ).'</button></div>'; // TODO: What kind of language variable is this?
  5080. }
  5081. // We need to close the form when we are updating the mp3 files.
  5082. if ($update_audio == 'true' && count($arrLP) != 0) {
  5083. $return .= '</form>';
  5084. }
  5085. return $return;
  5086. }
  5087. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5088. {
  5089. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  5090. echo '<script>
  5091. var newOrderData= "";
  5092. function processChildren(parentId) {
  5093. //Loop through the children of the UL element defined by the parentId
  5094. var ulParentID= "UL_" + parentId;
  5095. $("#" + ulParentID).children().each(function () {
  5096. /*Only process elements with an id attribute (in order to skip the blank,
  5097. unmovable <li> elements.*/
  5098. if ($(this).attr("id")) {
  5099. /*Build a string of data with the childs ID and parent ID,
  5100. using the "|" as a delimiter between the two IDs and the "^"
  5101. as a record delimiter (these delimiters were chosen in case the data
  5102. involved includes more common delimiters like commas within the content)
  5103. */
  5104. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  5105. //Determine if this child is a containter
  5106. if ($(this).is(".container")) {
  5107. //Process the child elements of the container
  5108. processChildren($(this).attr("id"));
  5109. }
  5110. }
  5111. }); //end of children loop
  5112. } //end of processChildren function
  5113. $(function() {
  5114. $(".item_data").live("mouseover", function(event) {
  5115. $(".button_actions", this).show();
  5116. });
  5117. $(".item_data").live("mouseout", function() {
  5118. $(".button_actions",this).hide();
  5119. });
  5120. $(".button_actions").hide();
  5121. $( ".lp_resource" ).sortable({
  5122. items: ".lp_resource_element ",
  5123. handle: ".moved", //only the class "moved"
  5124. cursor: "move",
  5125. connectWith: "#lp_item_list",
  5126. placeholder: "ui-state-highlight", //defines the yellow highlight
  5127. start: function(event, ui) {
  5128. $(ui.item).css("width", "160px");
  5129. $(ui.item).find(".item_data").attr("style", "");
  5130. },
  5131. stop: function(event, ui) {
  5132. $(ui.item).css("width", "100%");
  5133. },
  5134. });
  5135. $("#lp_item_list").sortable({
  5136. items: "li",
  5137. handle: ".moved", //only the class "moved"
  5138. cursor: "move",
  5139. placeholder: "ui-state-highlight", //defines the yellow highlight
  5140. update: function(event, ui) {
  5141. //Walk through the direct descendants of the lp_item_list <ul>
  5142. $("#lp_item_list").children().each(function () {
  5143. /*Only process elements with an id attribute (in order to skip the blank,
  5144. unmovable <li> elements.*/
  5145. if ($(this).attr("id")) {
  5146. /*Build a string of data with the child s ID and parent ID,
  5147. using the "|" as a delimiter between the two IDs and the "^"
  5148. as a record delimiter (these delimiters were chosen in case the data
  5149. involved includes more common delimiters like commas within the content)
  5150. */
  5151. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  5152. //Determine if this child is a containter
  5153. if ($(this).is(".li_container")) {
  5154. //Process the child elements of the container
  5155. processChildren($(this).attr("id"));
  5156. }
  5157. }
  5158. }); //end of lp_item_list children loop
  5159. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  5160. $.post("'.$ajax_url.'", order, function(reponse){
  5161. $("#message").html(reponse);
  5162. });
  5163. },
  5164. receive: function(event, ui) {
  5165. var id = $(ui.item).attr("data_id");
  5166. var type = $(ui.item).attr("data_type");
  5167. var title = $(ui.item).attr("title");
  5168. if (ui.item.parent()[0]) {
  5169. var parent_id = $(ui.item.parent()[0]).attr("id");
  5170. var previous_id = $(ui.item.prev()).attr("id");
  5171. if (parent_id) {
  5172. parent_id = parent_id.split("_")[1];
  5173. var params = {
  5174. "a": "add_lp_item",
  5175. "id": id,
  5176. "parent_id": parent_id,
  5177. "previous_id": previous_id,
  5178. "type": type,
  5179. "title" : title
  5180. };
  5181. $.ajax({
  5182. type: "GET",
  5183. url: "'.$ajax_url.'",
  5184. data: params,
  5185. async: false,
  5186. success: function(data) {
  5187. if (data == -1) {
  5188. } else {
  5189. $(".normal-message").hide();
  5190. $(ui.item).attr("id", data);
  5191. $(ui.item).addClass("lp_resource_element_new");
  5192. $(ui.item).find(".item_data").attr("style", "");
  5193. $(ui.item).addClass("record li_container");
  5194. $(ui.item).removeClass("lp_resource_element");
  5195. $(ui.item).removeClass("doc_resource");
  5196. }
  5197. }
  5198. });
  5199. }
  5200. }//
  5201. }//end receive
  5202. });
  5203. });
  5204. </script>';
  5205. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5206. $course_id = api_get_course_int_id();
  5207. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5208. $sql = "SELECT * FROM $tbl_lp_item
  5209. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5210. $result = Database::query($sql);
  5211. $arrLP = array();
  5212. while ($row = Database :: fetch_array($result)) {
  5213. $row['title'] = Security :: remove_XSS($row['title']);
  5214. $row['description'] = Security :: remove_XSS($row['description']);
  5215. $arrLP[] = array(
  5216. 'id' => $row['id'],
  5217. 'item_type' => $row['item_type'],
  5218. 'title' => $row['title'],
  5219. 'path' => $row['path'],
  5220. 'description' => $row['description'],
  5221. 'parent_item_id' => $row['parent_item_id'],
  5222. 'previous_item_id' => $row['previous_item_id'],
  5223. 'next_item_id' => $row['next_item_id'],
  5224. 'max_score' => $row['max_score'],
  5225. 'min_score' => $row['min_score'],
  5226. 'mastery_score' => $row['mastery_score'],
  5227. 'prerequisite' => $row['prerequisite'],
  5228. 'display_order' => $row['display_order'],
  5229. 'audio' => $row['audio']
  5230. );
  5231. }
  5232. $this->tree_array($arrLP);
  5233. $arrLP = $this->arrMenu;
  5234. unset ($this->arrMenu);
  5235. $default_data = null;
  5236. $default_content = null;
  5237. $elements = array();
  5238. $return_audio = null;
  5239. for ($i = 0; $i < count($arrLP); $i++) {
  5240. $title = $arrLP[$i]['title'];
  5241. $title_cut = Text::cut($arrLP[$i]['title'], 25);
  5242. //Link for the documents
  5243. if ($arrLP[$i]['item_type'] == 'document') {
  5244. $url = api_get_self().'?'.api_get_cidreq(
  5245. ).'&amp;action=view_item&amp;mode=preview_document&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id;
  5246. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5247. }
  5248. if (($i % 2) == 0) {
  5249. $oddclass = 'row_odd';
  5250. } else {
  5251. $oddclass = 'row_even';
  5252. }
  5253. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'].'" class="'.$oddclass.'">';
  5254. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5255. $icon = '';
  5256. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5257. $icon = '<img src="../img/lp_'.$icon_name.'.png" />';
  5258. } else {
  5259. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5260. $icon = '<img src="../img/lp_'.$icon_name.'.gif" />';
  5261. } else {
  5262. $icon = '<img src="../img/folder_document.gif" />';
  5263. }
  5264. }
  5265. // The audio column.
  5266. $return_audio .= '<td align="center">';
  5267. $audio = '';
  5268. if (!$update_audio OR $update_audio <> 'true') {
  5269. if (!empty($arrLP[$i]['audio'])) {
  5270. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5271. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5272. $audio .= '<script type="text/javascript">
  5273. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5274. s1.addParam("allowscriptaccess","always");
  5275. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5276. s1.write("container' . $i . '");
  5277. </script>';*/
  5278. } else {
  5279. $audio .= '';
  5280. }
  5281. } else {
  5282. if ($arrLP[$i]['item_type'] != 'dokeos_chapter' && $arrLP[$i]['item_type'] != 'dokeos_module' && $arrLP[$i]['item_type'] != 'dir') {
  5283. $audio .= '<input type="file" name="mp3file'.$arrLP[$i]['id'].'" id="mp3file" />';
  5284. if (!empty ($arrLP[$i]['audio'])) {
  5285. $audio .= '<br />'.Security::remove_XSS(
  5286. $arrLP[$i]['audio']
  5287. ).'<br /><input type="checkbox" name="removemp3'.$arrLP[$i]['id'].'" id="checkbox'.$arrLP[$i]['id'].'" />'.get_lang(
  5288. 'RemoveAudio'
  5289. );
  5290. }
  5291. }
  5292. }
  5293. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style' => ''));
  5294. $return_audio .= '</td>';
  5295. $move_icon = '';
  5296. $move_item_icon = '';
  5297. $edit_icon = '';
  5298. $delete_icon = '';
  5299. $audio_icon = '';
  5300. $prerequisities_icon = '';
  5301. if ($is_allowed_to_edit) {
  5302. if (!$update_audio OR $update_audio <> 'true') {
  5303. $move_icon .= '<a class="moved" href="#">';
  5304. $move_icon .= Display::return_icon(
  5305. 'move_everywhere.png',
  5306. get_lang('Move'),
  5307. array(),
  5308. ICON_SIZE_TINY
  5309. );
  5310. $move_icon .= '</a>';
  5311. }
  5312. // No edit for this item types
  5313. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5314. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5315. $edit_icon .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5316. $_GET['cidReq']
  5317. ).'&amp;action=edit_item&amp;view=build&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5318. $edit_icon .= Display::return_icon(
  5319. 'edit.png',
  5320. get_lang('LearnpathEditModule'),
  5321. array(),
  5322. ICON_SIZE_TINY
  5323. );
  5324. $edit_icon .= '</a>';
  5325. } else {
  5326. $edit_icon .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5327. $_GET['cidReq']
  5328. ).'&amp;action=edit_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'&amp;path_item='.$arrLP[$i]['path'].'">';
  5329. $edit_icon .= Display::return_icon(
  5330. 'edit.png',
  5331. get_lang('LearnpathEditModule'),
  5332. array(),
  5333. ICON_SIZE_TINY
  5334. );
  5335. $edit_icon .= '</a>';
  5336. }
  5337. }
  5338. $delete_icon .= ' <a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  5339. $_GET['cidReq']
  5340. ).'&amp;action=delete_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$this->lp_id.'" onClick="return confirmation(\''.addslashes(
  5341. $title
  5342. ).'\');">';
  5343. $delete_icon .= Display::return_icon(
  5344. 'delete.png',
  5345. get_lang('LearnpathDeleteModule'),
  5346. array(),
  5347. ICON_SIZE_TINY
  5348. );
  5349. $delete_icon .= '</a>';
  5350. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  5351. $_GET['cidReq']
  5352. ).'&view=build&id='.$arrLP[$i]['id'].'&lp_id='.$this->lp_id;
  5353. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5354. $prerequisities_icon = Display::url(
  5355. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY),
  5356. $url.'&action=edit_item_prereq'
  5357. );
  5358. $move_item_icon = Display::url(
  5359. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY),
  5360. $url.'&action=move_item'
  5361. );
  5362. $audio_icon = Display::url(
  5363. Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY),
  5364. $url.'&action=add_audio'
  5365. );
  5366. }
  5367. }
  5368. if ($update_audio != 'true') {
  5369. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span(
  5370. $audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon,
  5371. array('class' => 'button_actions')
  5372. );
  5373. } else {
  5374. $row = Display::span($title.$icon).Display::span($audio, array('class' => 'button_actions'));
  5375. }
  5376. $parent_id = $arrLP[$i]['parent_item_id'];
  5377. $default_data[$arrLP[$i]['id']] = $row;
  5378. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5379. if (empty($parent_id)) {
  5380. $elements[$arrLP[$i]['id']]['data'] = $row;
  5381. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5382. } else {
  5383. $parent_arrays = array();
  5384. if ($arrLP[$i]['depth'] > 1) {
  5385. //Getting list of parents
  5386. for ($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5387. foreach ($arrLP as $item) {
  5388. if ($item['id'] == $parent_id) {
  5389. if ($item['parent_item_id'] == 0) {
  5390. $parent_id = $item['id'];
  5391. break;
  5392. } else {
  5393. $parent_id = $item['parent_item_id'];
  5394. if (empty($parent_arrays)) {
  5395. $parent_arrays[] = intval($item['id']);
  5396. }
  5397. $parent_arrays[] = $parent_id;
  5398. break;
  5399. }
  5400. }
  5401. }
  5402. }
  5403. }
  5404. if (!empty($parent_arrays)) {
  5405. $parent_arrays = array_reverse($parent_arrays);
  5406. $val = '$elements';
  5407. $x = 0;
  5408. foreach ($parent_arrays as $item) {
  5409. if ($x != count($parent_arrays) - 1) {
  5410. $val .= '["'.$item.'"]["children"]';
  5411. } else {
  5412. $val .= '["'.$item.'"]["children"]';
  5413. }
  5414. $x++;
  5415. }
  5416. $val .= "";
  5417. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5418. eval($code_str);
  5419. } else {
  5420. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5421. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5422. }
  5423. }
  5424. }
  5425. $return = '<div class="lp_tree well">';
  5426. $return .= '<ul id="lp_item_list">';
  5427. $return .= '<h4>'.$this->name.'</h4><br>';
  5428. $tree = self::print_recursive($elements, $default_data, $default_content);
  5429. if (!empty($tree)) {
  5430. $return .= $tree;
  5431. } else {
  5432. if ($drop_element_here) {
  5433. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5434. }
  5435. }
  5436. $return .= '</ul>';
  5437. if ($update_audio == 'true') {
  5438. $return = $return_audio;
  5439. } else {
  5440. $return .= '</div>';
  5441. }
  5442. return $return;
  5443. }
  5444. function print_recursive($elements, $default_data, $default_content)
  5445. {
  5446. $return = '';
  5447. foreach ($elements as $key => $item) {
  5448. if (isset($item['load_data']) || empty($item['data'])) {
  5449. $item['data'] = $default_data[$item['load_data']];
  5450. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5451. }
  5452. $sub_list = '';
  5453. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5454. $sub_list = Display::tag('li', '', array('class' => 'sub_item empty')); // empty value
  5455. }
  5456. if (empty($item['children'])) {
  5457. $sub_list = Display::tag('ul', $sub_list, array('id' => 'UL_'.$key, 'class' => 'record li_container'));
  5458. $active = null;
  5459. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5460. $active = 'active';
  5461. }
  5462. $return .= Display::tag(
  5463. 'li',
  5464. Display::div($item['data'], array('class' => "item_data $active")).$sub_list,
  5465. array('id' => $key, 'class' => 'record li_container')
  5466. );
  5467. } else {
  5468. //sections
  5469. if (isset($item['children'])) {
  5470. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5471. }
  5472. $sub_list = Display::tag(
  5473. 'ul',
  5474. $sub_list.$data,
  5475. array('id' => 'UL_'.$key, 'class' => 'record li_container')
  5476. );
  5477. $return .= Display::tag(
  5478. 'li',
  5479. Display::div($item['data'], array('class' => 'item_data')).$sub_list,
  5480. array('id' => $key, 'class' => 'record li_container')
  5481. );
  5482. }
  5483. }
  5484. return $return;
  5485. }
  5486. /**
  5487. * This function builds the action menu
  5488. * @return void
  5489. */
  5490. public function build_action_menu()
  5491. {
  5492. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  5493. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5494. $return = '<div class="actions">';
  5495. //$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>';
  5496. //$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>';
  5497. $return .= '<a href="lp_controller.php?'.api_get_cidreq(
  5498. ).'&amp;gradebook='.$gradebook.'&action=view&lp_id='.$_SESSION['oLP']->lp_id.'&isStudentView=true">'.Display :: return_icon(
  5499. 'preview_view.png',
  5500. get_lang('Display'),
  5501. '',
  5502. ICON_SIZE_MEDIUM
  5503. ).'</a> ';
  5504. //$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>';
  5505. // 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>';
  5506. /*$return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  5507. ).'&amp;action=admin_view&amp;lp_id='.$_SESSION['oLP']->lp_id.'&amp;updateaudio=true">'.Display :: return_icon(
  5508. 'upload_audio.png',
  5509. get_lang('UpdateAllAudioFragments'),
  5510. '',
  5511. ICON_SIZE_MEDIUM
  5512. ).'</a>';*/
  5513. $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(
  5514. 'settings.png',
  5515. get_lang('CourseSettings'),
  5516. '',
  5517. ICON_SIZE_MEDIUM
  5518. ).'</a>';
  5519. $buttons = array(
  5520. array(
  5521. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5522. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5523. ).'&amp;action=set_previous_step_as_prerequisite&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5524. ),
  5525. array(
  5526. 'title' => get_lang('ClearAllPrerequisites'),
  5527. 'href' => 'lp_controller.php?'.api_get_cidreq(
  5528. ).'&amp;action=clear_prerequisites&amp;lp_id='.$_SESSION['oLP']->lp_id,
  5529. ),
  5530. );
  5531. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5532. $return .= '</div>';
  5533. echo $return;
  5534. }
  5535. /**
  5536. * This functions builds the LP tree based on data from the database.
  5537. * @return string
  5538. * @deprecated use the return_new_tree() function
  5539. * @uses dtree.js :: necessary javascript for building this tree
  5540. */
  5541. public function build_tree()
  5542. {
  5543. $course_id = api_get_course_int_id();
  5544. $return = "<script type=\"text/javascript\">\n";
  5545. $return .= "\tm = new dTree('m');\n\n";
  5546. $return .= "\tm.config.folderLinks = true;\n";
  5547. $return .= "\tm.config.useCookies = true;\n";
  5548. $return .= "\tm.config.useIcons = true;\n";
  5549. $return .= "\tm.config.useLines = true;\n";
  5550. $return .= "\tm.config.useSelection = true;\n";
  5551. $return .= "\tm.config.useStatustext = false;\n\n";
  5552. $menu = 0;
  5553. $parent = '';
  5554. $return .= "\tm.add(".$menu.", -1, '".addslashes(Security::remove_XSS(($this->name)))."');\n";
  5555. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5556. $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
  5557. FROM $tbl_lp_item
  5558. WHERE c_id = ".$course_id." AND lp_id = ".Database::escape_string($this->lp_id);
  5559. $result = Database::query($sql);
  5560. $arrLP = array();
  5561. while ($row = Database :: fetch_array($result)) {
  5562. $row['title'] = Security :: remove_XSS($row['title']);
  5563. $row['description'] = Security :: remove_XSS($row['description']);
  5564. $arrLP[] = array(
  5565. 'id' => $row['id'],
  5566. 'item_type' => $row['item_type'],
  5567. 'title' => $row['title'],
  5568. 'path' => $row['path'],
  5569. 'description' => $row['description'],
  5570. 'parent_item_id' => $row['parent_item_id'],
  5571. 'previous_item_id' => $row['previous_item_id'],
  5572. 'next_item_id' => $row['next_item_id'],
  5573. 'max_score' => $row['max_score'],
  5574. 'min_score' => $row['min_score'],
  5575. 'mastery_score' => $row['mastery_score'],
  5576. 'display_order' => $row['display_order']
  5577. );
  5578. }
  5579. $this->tree_array($arrLP);
  5580. $arrLP = $this->arrMenu;
  5581. unset ($this->arrMenu);
  5582. $title = '';
  5583. for ($i = 0; $i < count($arrLP); $i++) {
  5584. $title = addslashes($arrLP[$i]['title']);
  5585. $menu_page = api_get_self().'?cidReq='.Security :: remove_XSS(
  5586. $_GET['cidReq']
  5587. ).'&amp;action=view_item&amp;id='.$arrLP[$i]['id'].'&amp;lp_id='.$_SESSION['oLP']->lp_id;
  5588. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5589. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  5590. $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";
  5591. } else {
  5592. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  5593. $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";
  5594. } else {
  5595. $return .= "\tm.add(".$arrLP[$i]['id'].", ".$arrLP[$i]['parent_item_id'].", '".$title."', '".$menu_page."', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  5596. }
  5597. }
  5598. if ($menu < $arrLP[$i]['id']) {
  5599. $menu = $arrLP[$i]['id'];
  5600. }
  5601. }
  5602. $return .= "\n\tdocument.write(m);\n";
  5603. $return .= "\t if(!m.selectedNode) m.s(1);";
  5604. $return .= "</script>\n";
  5605. return $return;
  5606. }
  5607. /**
  5608. * Creates the default learning path folder
  5609. */
  5610. public static function generate_learning_path_folder($course)
  5611. {
  5612. //Creating learning_path folder
  5613. $dir = '/learning_path';
  5614. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5615. $folder = false;
  5616. if (!is_dir($filepath.'/'.$dir)) {
  5617. $folder = FileManager::create_unexisting_directory(
  5618. $course,
  5619. api_get_user_id(),
  5620. api_get_session_id(),
  5621. 0,
  5622. 0,
  5623. $filepath,
  5624. $dir,
  5625. get_lang('LearningPaths'),
  5626. 0
  5627. );
  5628. } else {
  5629. $folder = true;
  5630. }
  5631. return $folder;
  5632. }
  5633. public function generate_lp_folder($course, $lp_name = null)
  5634. {
  5635. $filepath = '';
  5636. $dir = '/learning_path/';
  5637. if (empty($lp_name)) {
  5638. $lp_name = $this->name;
  5639. }
  5640. $folder = self::generate_learning_path_folder($course);
  5641. //Creating LP folder
  5642. if ($folder) {
  5643. //Limits title size
  5644. $title = api_substr(replace_dangerous_char($lp_name), 0, 80);
  5645. $dir = $dir.$title;
  5646. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document';
  5647. if (!is_dir($filepath.'/'.$dir)) {
  5648. $folder = FileManager::create_unexisting_directory(
  5649. $course,
  5650. api_get_user_id(),
  5651. 0,
  5652. 0,
  5653. 0,
  5654. $filepath,
  5655. $dir,
  5656. $lp_name
  5657. );
  5658. } else {
  5659. $folder = true;
  5660. }
  5661. $dir = $dir.'/';
  5662. if ($folder) {
  5663. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$dir;
  5664. }
  5665. }
  5666. $array = array('dir' => $dir, 'filepath' => $filepath, 'folder' => $folder);
  5667. return $array;
  5668. }
  5669. /**
  5670. * Create a new document //still needs some finetuning
  5671. * @param array $_course
  5672. * @return string
  5673. */
  5674. public function create_document($_course)
  5675. {
  5676. $course_id = api_get_course_int_id();
  5677. global $charset;
  5678. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5679. if (strstr($dir, '..')) {
  5680. $dir = '/';
  5681. }
  5682. if ($dir[0] == '.') {
  5683. $dir = substr($dir, 1);
  5684. }
  5685. if ($dir[0] != '/') {
  5686. $dir = '/'.$dir;
  5687. }
  5688. if ($dir[strlen($dir) - 1] != '/') {
  5689. $dir .= '/';
  5690. }
  5691. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5692. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5693. //Generates folder
  5694. $result = $this->generate_lp_folder($_course);
  5695. $dir = $result['dir'];
  5696. $filepath = $result['filepath'];
  5697. }
  5698. if (!is_dir($filepath)) {
  5699. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5700. $dir = '/';
  5701. }
  5702. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  5703. // is already escaped twice when it gets here.
  5704. $title = replace_dangerous_char(stripslashes($_POST['title']));
  5705. $title = FileManager::disable_dangerous_file($title);
  5706. $filename = $title;
  5707. $content = $_POST['content_lp'];
  5708. $tmp_filename = $filename;
  5709. $i = 0;
  5710. while (file_exists($filepath.$tmp_filename.'.html')) {
  5711. $tmp_filename = $filename.'_'.++$i;
  5712. }
  5713. $filename = $tmp_filename.'.html';
  5714. $content = stripslashes($content);
  5715. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5716. // Change the path of mp3 to absolute.
  5717. // The first regexp deals with ../../../ urls.
  5718. $content = preg_replace(
  5719. "|(flashvars=\"file=)(\.+/)+|",
  5720. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5721. $content
  5722. );
  5723. // The second regexp deals with audio/ urls.
  5724. $content = preg_replace(
  5725. "|(flashvars=\"file=)([^/]+)/|",
  5726. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5727. $content
  5728. );
  5729. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5730. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5731. if (!file_exists($filepath.$filename)) {
  5732. if ($fp = @ fopen($filepath.$filename, 'w')) {
  5733. fputs($fp, $content);
  5734. fclose($fp);
  5735. $file_size = filesize($filepath.$filename);
  5736. $save_file_path = $dir.$filename;
  5737. $document_id = FileManager::add_document($_course, $save_file_path, 'file', $file_size, $tmp_filename);
  5738. if ($document_id) {
  5739. api_item_property_update(
  5740. $_course,
  5741. TOOL_DOCUMENT,
  5742. $document_id,
  5743. 'DocumentAdded',
  5744. api_get_user_id(),
  5745. null,
  5746. null,
  5747. null,
  5748. null,
  5749. api_get_session_id()
  5750. );
  5751. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5752. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5753. if ($new_comment || $new_title) {
  5754. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5755. $ct = '';
  5756. if ($new_comment) {
  5757. $ct .= ", comment='".Database::escape_string($new_comment)."'";
  5758. }
  5759. if ($new_title) {
  5760. $ct .= ", title='".Database::escape_string(
  5761. htmlspecialchars($new_title, ENT_QUOTES, $charset)
  5762. )."' ";
  5763. }
  5764. $sql_update = "UPDATE ".$tbl_doc." SET ".substr(
  5765. $ct,
  5766. 1
  5767. )." WHERE c_id = ".$course_id." AND id = ".$document_id;
  5768. Database::query($sql_update);
  5769. }
  5770. }
  5771. return $document_id;
  5772. }
  5773. }
  5774. }
  5775. /**
  5776. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5777. * @param array $_course array
  5778. * @return void
  5779. */
  5780. public function edit_document($_course)
  5781. {
  5782. $course_id = api_get_course_int_id();
  5783. global $_configuration;
  5784. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5785. if (strstr($dir, '..')) {
  5786. $dir = '/';
  5787. }
  5788. if ($dir[0] == '.') {
  5789. $dir = substr($dir, 1);
  5790. }
  5791. if ($dir[0] != '/') {
  5792. $dir = '/'.$dir;
  5793. }
  5794. if ($dir[strlen($dir) - 1] != '/') {
  5795. $dir .= '/';
  5796. }
  5797. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  5798. if (!is_dir($filepath)) {
  5799. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  5800. $dir = '/';
  5801. }
  5802. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5803. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5804. $document_id = intval($_POST['path']);
  5805. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = $course_id AND id = ".$document_id;
  5806. $res = Database::query($sql);
  5807. $row = Database :: fetch_array($res);
  5808. $content = stripslashes($_POST['content_lp']);
  5809. $file = $filepath.$row['path'];
  5810. if ($fp = @ fopen($file, 'w')) {
  5811. $content = str_replace(
  5812. api_get_path(WEB_COURSE_PATH),
  5813. $_configuration['url_append'].'/courses/',
  5814. $content
  5815. );
  5816. // Change the path of mp3 to absolute.
  5817. // The first regexp deals with ../../../ urls.
  5818. $content = preg_replace(
  5819. "|(flashvars=\"file=)(\.+/)+|",
  5820. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',
  5821. $content
  5822. );
  5823. // The second regexp deals with audio/ urls.
  5824. $content = preg_replace(
  5825. "|(flashvars=\"file=)([^/]+)/|",
  5826. "$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',
  5827. $content
  5828. );
  5829. fputs($fp, $content);
  5830. fclose($fp);
  5831. $sql_update = "UPDATE ".$table_doc." SET title='".Database::escape_string(
  5832. $_POST['title']
  5833. )."' WHERE c_id = ".$course_id." AND id = ".$document_id;
  5834. Database::query($sql_update);
  5835. }
  5836. }
  5837. }
  5838. /**
  5839. * Displays the selected item, with a panel for manipulating the item
  5840. * @param int $item_id
  5841. * @param string $msg
  5842. * @return string
  5843. */
  5844. public function display_item($item_id, $msg = null, $show_actions = true)
  5845. {
  5846. $course_id = api_get_course_int_id();
  5847. $return = '';
  5848. if (is_numeric($item_id)) {
  5849. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5850. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5851. $sql = "SELECT lp.* FROM ".$tbl_lp_item." as lp
  5852. WHERE c_id = ".$course_id." AND lp.id = ".Database::escape_string($item_id);
  5853. $result = Database::query($sql);
  5854. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  5855. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5856. // Prevents wrong parent selection for document, see Bug#1251.
  5857. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5858. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5859. }
  5860. if ($show_actions) {
  5861. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5862. }
  5863. $return .= '<div style="padding:10px;">';
  5864. if ($msg != '') {
  5865. $return .= $msg;
  5866. }
  5867. $return .= '<h3>'.$row['title'].'</h3>';
  5868. switch ($row['item_type']) {
  5869. case TOOL_QUIZ:
  5870. if (!empty($row['path'])) {
  5871. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5872. $exercise = new Exercise();
  5873. $exercise->read($row['path']);
  5874. $return .= $exercise->description.'<br />';
  5875. }
  5876. break;
  5877. case TOOL_DOCUMENT:
  5878. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5879. $sql_doc = "SELECT path FROM ".$tbl_doc." WHERE c_id = ".$course_id." AND id = ".Database::escape_string(
  5880. $row['path']
  5881. );
  5882. $result = Database::query($sql_doc);
  5883. $path_file = Database::result($result, 0, 0);
  5884. $path_parts = pathinfo($path_file);
  5885. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5886. if (in_array(
  5887. $path_parts['extension'],
  5888. array(
  5889. 'html',
  5890. 'txt',
  5891. 'png',
  5892. 'jpg',
  5893. 'JPG',
  5894. 'jpeg',
  5895. 'JPEG',
  5896. 'gif',
  5897. 'swf'
  5898. )
  5899. )
  5900. ) {
  5901. $return .= $this->display_document($row['path'], true, true);
  5902. }
  5903. break;
  5904. }
  5905. $return .= '</div>';
  5906. }
  5907. }
  5908. return $return;
  5909. }
  5910. /**
  5911. * Shows the needed forms for editing a specific item
  5912. * @param int $item_id
  5913. * @return string
  5914. */
  5915. public function display_edit_item($item_id)
  5916. {
  5917. global $_course; // It will disappear.
  5918. $course_id = api_get_course_int_id();
  5919. $return = '';
  5920. if (is_numeric($item_id)) {
  5921. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5922. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = ".Database::escape_string($item_id);
  5923. $res = Database::query($sql);
  5924. $row = Database::fetch_array($res);
  5925. switch ($row['item_type']) {
  5926. case 'dokeos_chapter' :
  5927. case 'dir' :
  5928. case 'asset' :
  5929. case 'sco' :
  5930. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5931. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5932. $return .= $this->display_item_form(
  5933. $row['item_type'],
  5934. get_lang('EditCurrentChapter').' :',
  5935. 'edit',
  5936. $item_id,
  5937. $row
  5938. );
  5939. } else {
  5940. $return .= $this->display_item_small_form(
  5941. $row['item_type'],
  5942. get_lang('EditCurrentChapter').' :',
  5943. $row
  5944. );
  5945. }
  5946. break;
  5947. case TOOL_DOCUMENT :
  5948. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5949. $sql_step = " SELECT lp.*, doc.path as dir
  5950. FROM ".$tbl_lp_item." as lp
  5951. LEFT JOIN ".$tbl_doc." as doc ON doc.id = lp.path
  5952. WHERE lp.c_id = $course_id AND
  5953. doc.c_id = $course_id AND
  5954. lp.id = ".Database::escape_string($item_id);
  5955. $res_step = Database::query($sql_step);
  5956. $row_step = Database :: fetch_array($res_step);
  5957. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5958. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5959. break;
  5960. case TOOL_LINK :
  5961. $link_id = (string)$row['path'];
  5962. if (ctype_digit($link_id)) {
  5963. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5964. $sql_select = 'SELECT url FROM '.$tbl_link.' WHERE c_id = '.$course_id.' AND id = '.Database::escape_string(
  5965. $link_id
  5966. );
  5967. $res_link = Database::query($sql_select);
  5968. $row_link = Database :: fetch_array($res_link);
  5969. if (is_array($row_link)) {
  5970. $row['url'] = $row_link['url'];
  5971. }
  5972. }
  5973. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5974. $return .= $this->display_link_form('edit', $item_id, $row);
  5975. break;
  5976. case 'dokeos_module' :
  5977. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5978. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5979. $return .= $this->display_item_form(
  5980. $row['item_type'],
  5981. get_lang('EditCurrentModule').' :',
  5982. 'edit',
  5983. $item_id,
  5984. $row
  5985. );
  5986. } else {
  5987. $return .= $this->display_item_small_form(
  5988. $row['item_type'],
  5989. get_lang('EditCurrentModule').' :',
  5990. $row
  5991. );
  5992. }
  5993. break;
  5994. case TOOL_QUIZ :
  5995. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5996. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5997. break;
  5998. case TOOL_HOTPOTATOES :
  5999. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6000. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  6001. break;
  6002. case TOOL_STUDENTPUBLICATION :
  6003. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6004. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  6005. break;
  6006. case TOOL_FORUM :
  6007. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6008. $return .= $this->display_forum_form('edit', $item_id, $row);
  6009. break;
  6010. case TOOL_THREAD :
  6011. $return .= $this->display_manipulate($item_id, $row['item_type']);
  6012. $return .= $this->display_thread_form('edit', $item_id, $row);
  6013. break;
  6014. }
  6015. }
  6016. return $return;
  6017. }
  6018. /**
  6019. * Function that displays a list with al the resources that could be added to the learning path
  6020. * @return string
  6021. */
  6022. public function display_resources()
  6023. {
  6024. global $_course; // TODO: Don't use globals.
  6025. $course_code = api_get_course_id();
  6026. //Get all the docs
  6027. $documents = $this->get_documents();
  6028. //Get all the exercises
  6029. $exercises = $this->get_exercises();
  6030. // Get all the links
  6031. $links = $this->get_links();
  6032. //Get al the student publications
  6033. $works = $this->get_student_publications();
  6034. //Get al the forums
  6035. $forums = $this->get_forums(null, $course_code);
  6036. $headers = array(
  6037. Display::return_icon('folder_document.png', get_lang('Documents'), array(), 64),
  6038. Display::return_icon('quiz.png', get_lang('Quiz'), array(), 64),
  6039. Display::return_icon('links.png', get_lang('Links'), array(), 64),
  6040. Display::return_icon('works.png', get_lang('Works'), array(), 64),
  6041. Display::return_icon('forum.png', get_lang('Forums'), array(), 64),
  6042. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), 64)
  6043. );
  6044. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  6045. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  6046. echo Display::tabs($headers, array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab');
  6047. return true;
  6048. }
  6049. /**
  6050. * Returns the extension of a document
  6051. * @param string filename
  6052. * @return string Extension (part after the last dot)
  6053. */
  6054. public function get_extension($filename)
  6055. {
  6056. $explode = explode('.', $filename);
  6057. return $explode[count($explode) - 1];
  6058. }
  6059. /**
  6060. * Displays a document by id
  6061. *
  6062. * @param unknown_type $id
  6063. * @return unknown
  6064. */
  6065. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  6066. {
  6067. global $_course; // It is temporary.
  6068. $course_id = api_get_course_int_id();
  6069. $return = '';
  6070. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6071. $sql_doc = "SELECT * FROM ".$tbl_doc."
  6072. WHERE c_id = ".$course_id." AND id = ".$id;
  6073. $res_doc = Database::query($sql_doc);
  6074. $row_doc = Database :: fetch_array($res_doc);
  6075. // TODO: Add a path filter.
  6076. if ($iframe) {
  6077. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="'.api_get_path(
  6078. WEB_COURSE_PATH
  6079. ).$_course['path'].'/document'.str_replace('%2F', '/', urlencode($row_doc['path'])).'?'.api_get_cidreq(
  6080. ).'"></iframe>';
  6081. } else {
  6082. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row_doc['path']);
  6083. }
  6084. return $return;
  6085. }
  6086. /**
  6087. * Return HTML form to add/edit a quiz
  6088. * @param string Action (add/edit)
  6089. * @param integer Item ID if already exists
  6090. * @param mixed Extra information (quiz ID if integer)
  6091. * @return string HTML form
  6092. */
  6093. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  6094. {
  6095. $course_id = api_get_course_int_id();
  6096. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6097. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  6098. if ($id != 0 && is_array($extra_info)) {
  6099. $item_title = $extra_info['title'];
  6100. $item_description = $extra_info['description'];
  6101. } elseif (is_numeric($extra_info)) {
  6102. $sql_quiz = "SELECT title, description FROM ".$tbl_quiz." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  6103. $result = Database::query($sql_quiz);
  6104. $row = Database :: fetch_array($result);
  6105. $item_title = $row['title'];
  6106. $item_description = $row['description'];
  6107. } else {
  6108. $item_title = '';
  6109. $item_description = '';
  6110. }
  6111. $item_title = Security::remove_XSS($item_title);
  6112. $item_description = Security::remove_XSS($item_description);
  6113. $legend = '<legend>';
  6114. if ($id != 0 && is_array($extra_info)) {
  6115. $parent = $extra_info['parent_item_id'];
  6116. } else {
  6117. $parent = 0;
  6118. }
  6119. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6120. $result = Database::query($sql);
  6121. $arrLP = array();
  6122. while ($row = Database :: fetch_array($result)) {
  6123. $arrLP[] = array(
  6124. 'id' => $row['id'],
  6125. 'item_type' => $row['item_type'],
  6126. 'title' => $row['title'],
  6127. 'path' => $row['path'],
  6128. 'description' => $row['description'],
  6129. 'parent_item_id' => $row['parent_item_id'],
  6130. 'previous_item_id' => $row['previous_item_id'],
  6131. 'next_item_id' => $row['next_item_id'],
  6132. 'display_order' => $row['display_order'],
  6133. 'max_score' => $row['max_score'],
  6134. 'min_score' => $row['min_score'],
  6135. 'mastery_score' => $row['mastery_score'],
  6136. 'prerequisite' => $row['prerequisite'],
  6137. 'max_time_allowed' => $row['max_time_allowed']
  6138. );
  6139. }
  6140. $this->tree_array($arrLP);
  6141. $arrLP = $this->arrMenu;
  6142. unset ($this->arrMenu);
  6143. if ($action == 'add') {
  6144. $legend .= get_lang('CreateTheExercise').'&nbsp;:';
  6145. } elseif ($action == 'move') {
  6146. $legend .= get_lang('MoveTheCurrentExercise').'&nbsp;:';
  6147. } else {
  6148. $legend .= get_lang('EditCurrentExecice').'&nbsp;:';
  6149. }
  6150. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6151. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6152. }
  6153. $legend .= '</legend>';
  6154. $return .= '<div class="sectioncomment">';
  6155. $return .= '<form method="POST">';
  6156. $return .= $legend;
  6157. $return .= '<table class="lp_form">';
  6158. if ($action != 'move') {
  6159. $return .= '<tr>';
  6160. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6161. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" /></td>';
  6162. $return .= '</tr>';
  6163. }
  6164. $return .= '<tr>';
  6165. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6166. $return .= '<td class="input">';
  6167. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6168. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6169. $arrHide = array(
  6170. $id
  6171. );
  6172. //$parent_item_id = $_SESSION['parent_item_id'];
  6173. for ($i = 0; $i < count($arrLP); $i++) {
  6174. if ($action != 'add') {
  6175. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6176. $arrLP[$i]['id'],
  6177. $arrHide
  6178. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6179. ) {
  6180. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6181. } else {
  6182. $arrHide[] = $arrLP[$i]['id'];
  6183. }
  6184. } else {
  6185. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6186. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6187. }
  6188. }
  6189. }
  6190. if (is_array($arrLP)) {
  6191. reset($arrLP);
  6192. }
  6193. $return .= '</select>';
  6194. $return .= '</td>';
  6195. $return .= '</tr>';
  6196. $return .= '<tr>';
  6197. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6198. $return .= '<td class="input">';
  6199. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  6200. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6201. for ($i = 0; $i < count($arrLP); $i++) {
  6202. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6203. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6204. $selected = 'selected="selected" ';
  6205. } elseif ($action == 'add') {
  6206. $selected = 'selected="selected" ';
  6207. } else {
  6208. $selected = '';
  6209. }
  6210. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6211. 'After'
  6212. ).' "'.$arrLP[$i]['title'].'"</option>';
  6213. }
  6214. }
  6215. $return .= '</select>';
  6216. $return .= '</td>';
  6217. $return .= '</tr>';
  6218. if ($action != 'move') {
  6219. $id_prerequisite = 0;
  6220. if (is_array($arrLP)) {
  6221. foreach ($arrLP as $key => $value) {
  6222. if ($value['id'] == $id) {
  6223. $id_prerequisite = $value['prerequisite'];
  6224. break;
  6225. }
  6226. }
  6227. }
  6228. $arrHide = array();
  6229. for ($i = 0; $i < count($arrLP); $i++) {
  6230. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6231. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6232. $s_selected_position = $arrLP[$i]['id'];
  6233. } elseif ($action == 'add') {
  6234. $s_selected_position = 0;
  6235. }
  6236. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6237. }
  6238. }
  6239. /*// Commented the prerequisites, only visible in edit (exercise).
  6240. $return .= '<tr>';
  6241. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  6242. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  6243. foreach($arrHide as $key => $value){
  6244. if($key==$s_selected_position && $action == 'add'){
  6245. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6246. }
  6247. elseif($key==$id_prerequisite && $action == 'edit'){
  6248. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6249. }
  6250. else{
  6251. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6252. }
  6253. }
  6254. $return .= "</select></td>";
  6255. */
  6256. $return .= '</tr>';
  6257. /*$return .= '<tr>';
  6258. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  6259. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  6260. // Remove temporarily the test description.
  6261. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  6262. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  6263. $return .= '</tr>'; */
  6264. }
  6265. $return .= '<tr>';
  6266. if ($action == 'add') {
  6267. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6268. 'AddExercise'
  6269. ).'</button></td>';
  6270. } else {
  6271. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  6272. 'EditCurrentExecice'
  6273. ).'</button></td>';
  6274. }
  6275. $return .= '</tr>';
  6276. $return .= '</table>';
  6277. if ($action == 'move') {
  6278. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6279. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6280. }
  6281. if (is_numeric($extra_info)) {
  6282. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6283. } elseif (is_array($extra_info)) {
  6284. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6285. }
  6286. $return .= '<input name="type" type="hidden" value="'.TOOL_QUIZ.'" />';
  6287. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6288. $return .= '</form>';
  6289. $return .= '</div>';
  6290. return $return;
  6291. }
  6292. /**
  6293. * Addition of Hotpotatoes tests
  6294. * @param string Action
  6295. * @param integer Internal ID of the item
  6296. * @param mixed Extra information - can be an array with title and description indexes
  6297. * @return string HTML structure to display the hotpotatoes addition formular
  6298. */
  6299. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '')
  6300. {
  6301. $course_id = api_get_course_int_id();
  6302. global $charset;
  6303. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  6304. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6305. if ($id != 0 && is_array($extra_info)) {
  6306. $item_title = stripslashes($extra_info['title']);
  6307. $item_description = stripslashes($extra_info['description']);
  6308. } elseif (is_numeric($extra_info)) {
  6309. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  6310. $sql_hot = "SELECT * FROM ".$TBL_DOCUMENT."
  6311. WHERE c_id = ".$course_id." AND
  6312. path LIKE '".$uploadPath."/%/%htm%' AND
  6313. id = ".(int)$extra_info."
  6314. ORDER BY id ASC";
  6315. $res_hot = Database::query($sql_hot);
  6316. $row = Database::fetch_array($res_hot);
  6317. $item_title = $row['title'];
  6318. $item_description = $row['description'];
  6319. if (!empty ($row['comment'])) {
  6320. $item_title = $row['comment'];
  6321. }
  6322. } else {
  6323. $item_title = '';
  6324. $item_description = '';
  6325. }
  6326. if ($id != 0 && is_array($extra_info)) {
  6327. $parent = $extra_info['parent_item_id'];
  6328. } else {
  6329. $parent = 0;
  6330. }
  6331. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6332. $result = Database::query($sql);
  6333. $arrLP = array();
  6334. while ($row = Database :: fetch_array($result)) {
  6335. $arrLP[] = array(
  6336. 'id' => $row['id'],
  6337. 'item_type' => $row['item_type'],
  6338. 'title' => $row['title'],
  6339. 'path' => $row['path'],
  6340. 'description' => $row['description'],
  6341. 'parent_item_id' => $row['parent_item_id'],
  6342. 'previous_item_id' => $row['previous_item_id'],
  6343. 'next_item_id' => $row['next_item_id'],
  6344. 'display_order' => $row['display_order'],
  6345. 'max_score' => $row['max_score'],
  6346. 'min_score' => $row['min_score'],
  6347. 'mastery_score' => $row['mastery_score'],
  6348. 'prerequisite' => $row['prerequisite'],
  6349. 'max_time_allowed' => $row['max_time_allowed']
  6350. );
  6351. }
  6352. $legend = '<legend>';
  6353. if ($action == 'add') {
  6354. $legend .= get_lang('CreateTheExercise');
  6355. } elseif ($action == 'move') {
  6356. $legend .= get_lang('MoveTheCurrentExercise');
  6357. } else {
  6358. $legend .= get_lang('EditCurrentExecice');
  6359. }
  6360. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6361. $legend .= Display :: return_warning_message(get_lang('Warning').' ! '.get_lang('WarningEditingDocument'));
  6362. }
  6363. $legend .= '</legend>';
  6364. $return .= '<form method="POST">';
  6365. $return .= $legend;
  6366. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6367. $return .= '<tr>';
  6368. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').' :</label></td>';
  6369. $return .= '<td class="input">';
  6370. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6371. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6372. $arrHide = array(
  6373. $id
  6374. );
  6375. if (count($arrLP) > 0) {
  6376. for ($i = 0; $i < count($arrLP); $i++) {
  6377. if ($action != 'add') {
  6378. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6379. $arrLP[$i]['id'],
  6380. $arrHide
  6381. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6382. ) {
  6383. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6384. } else {
  6385. $arrHide[] = $arrLP[$i]['id'];
  6386. }
  6387. } else {
  6388. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6389. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6390. }
  6391. }
  6392. }
  6393. reset($arrLP);
  6394. }
  6395. $return .= '</select>';
  6396. $return .= '</td>';
  6397. $return .= '</tr>';
  6398. $return .= '<tr>';
  6399. $return .= '<td class="label"><label for="previous">'.get_lang('Position').' :</label></td>';
  6400. $return .= '<td class="input">';
  6401. $return .= '<select id="previous" name="previous" size="1">';
  6402. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6403. for ($i = 0; $i < count($arrLP); $i++) {
  6404. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6405. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6406. $selected = 'selected="selected" ';
  6407. } elseif ($action == 'add') {
  6408. $selected = 'selected="selected" ';
  6409. } else {
  6410. $selected = '';
  6411. }
  6412. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6413. 'After'
  6414. ).' "'.$arrLP[$i]['title'].'"</option>';
  6415. }
  6416. }
  6417. $return .= '</select>';
  6418. $return .= '</td>';
  6419. $return .= '</tr>';
  6420. if ($action != 'move') {
  6421. $return .= '<tr>';
  6422. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').' :</label></td>';
  6423. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6424. $return .= '</tr>';
  6425. $id_prerequisite = 0;
  6426. if (is_array($arrLP) && count($arrLP) > 0) {
  6427. foreach ($arrLP as $key => $value) {
  6428. if ($value['id'] == $id) {
  6429. $id_prerequisite = $value['prerequisite'];
  6430. break;
  6431. }
  6432. }
  6433. $arrHide = array();
  6434. for ($i = 0; $i < count($arrLP); $i++) {
  6435. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6436. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6437. $s_selected_position = $arrLP[$i]['id'];
  6438. } elseif ($action == 'add') {
  6439. $s_selected_position = 0;
  6440. }
  6441. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6442. }
  6443. }
  6444. }
  6445. }
  6446. $return .= '<tr>';
  6447. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">'.get_lang(
  6448. 'SaveHotpotatoes'
  6449. ).'</button></td>';
  6450. $return .= '</tr>';
  6451. $return .= '</table>';
  6452. if ($action == 'move') {
  6453. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6454. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6455. }
  6456. if (is_numeric($extra_info)) {
  6457. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6458. } elseif (is_array($extra_info)) {
  6459. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6460. }
  6461. $return .= '<input name="type" type="hidden" value="'.TOOL_HOTPOTATOES.'" />';
  6462. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6463. $return .= '</form>';
  6464. return $return;
  6465. }
  6466. /**
  6467. * Return the form to display the forum edit/add option
  6468. * @param string Action (add/edit)
  6469. * @param integer ID of the lp_item if already exists
  6470. * @param mixed Forum ID or title
  6471. * @return string HTML form
  6472. */
  6473. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6474. {
  6475. $course_id = api_get_course_int_id();
  6476. global $charset;
  6477. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6478. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6479. if ($id != 0 && is_array($extra_info)) {
  6480. $item_title = stripslashes($extra_info['title']);
  6481. } elseif (is_numeric($extra_info)) {
  6482. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6483. FROM ".$tbl_forum."
  6484. WHERE c_id = ".$course_id." AND forum_id = ".$extra_info;
  6485. $result = Database::query($sql_forum);
  6486. $row = Database :: fetch_array($result);
  6487. $item_title = $row['title'];
  6488. $item_description = $row['comment'];
  6489. } else {
  6490. $item_title = '';
  6491. $item_description = '';
  6492. }
  6493. $legend = '<legend>';
  6494. if ($id != 0 && is_array($extra_info)) {
  6495. $parent = $extra_info['parent_item_id'];
  6496. } else {
  6497. $parent = 0;
  6498. }
  6499. $sql = "SELECT * FROM ".$tbl_lp_item."
  6500. WHERE c_id = ".$course_id." AND
  6501. lp_id = ".$this->lp_id;
  6502. $result = Database::query($sql);
  6503. $arrLP = array();
  6504. while ($row = Database :: fetch_array($result)) {
  6505. $arrLP[] = array(
  6506. 'id' => $row['id'],
  6507. 'item_type' => $row['item_type'],
  6508. 'title' => $row['title'],
  6509. 'path' => $row['path'],
  6510. 'description' => $row['description'],
  6511. 'parent_item_id' => $row['parent_item_id'],
  6512. 'previous_item_id' => $row['previous_item_id'],
  6513. 'next_item_id' => $row['next_item_id'],
  6514. 'display_order' => $row['display_order'],
  6515. 'max_score' => $row['max_score'],
  6516. 'min_score' => $row['min_score'],
  6517. 'mastery_score' => $row['mastery_score'],
  6518. 'prerequisite' => $row['prerequisite']
  6519. );
  6520. }
  6521. $this->tree_array($arrLP);
  6522. $arrLP = $this->arrMenu;
  6523. unset ($this->arrMenu);
  6524. if ($action == 'add') {
  6525. $legend .= get_lang('CreateTheForum').'&nbsp;:';
  6526. } elseif ($action == 'move') {
  6527. $legend .= get_lang('MoveTheCurrentForum').'&nbsp;:';
  6528. } else {
  6529. $legend .= get_lang('EditCurrentForum').'&nbsp;:';
  6530. }
  6531. $legend .= '</legend>';
  6532. $return .= '<div class="sectioncomment">';
  6533. $return .= '<form method="POST">';
  6534. $return .= $legend;
  6535. $return .= '<table class="lp_form">';
  6536. if ($action != 'move') {
  6537. $return .= '<tr>';
  6538. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6539. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  6540. $return .= '</tr>';
  6541. }
  6542. $return .= '<tr>';
  6543. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6544. $return .= '<td class="input">';
  6545. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6546. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6547. $arrHide = array(
  6548. $id
  6549. );
  6550. //$parent_item_id = $_SESSION['parent_item_id'];
  6551. for ($i = 0; $i < count($arrLP); $i++) {
  6552. if ($action != 'add') {
  6553. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6554. $arrLP[$i]['id'],
  6555. $arrHide
  6556. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6557. ) {
  6558. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6559. } else {
  6560. $arrHide[] = $arrLP[$i]['id'];
  6561. }
  6562. } else {
  6563. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6564. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6565. }
  6566. }
  6567. }
  6568. if (is_array($arrLP)) {
  6569. reset($arrLP);
  6570. }
  6571. $return .= "\t\t\t\t".'</select>';
  6572. $return .= '</td>';
  6573. $return .= '</tr>';
  6574. $return .= '<tr>';
  6575. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6576. $return .= '<td class="input">';
  6577. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6578. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6579. for ($i = 0; $i < count($arrLP); $i++) {
  6580. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6581. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6582. $selected = 'selected="selected" ';
  6583. } elseif ($action == 'add') {
  6584. $selected = 'selected="selected" ';
  6585. } else {
  6586. $selected = '';
  6587. }
  6588. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6589. 'After'
  6590. ).' "'.$arrLP[$i]['title'].'"</option>';
  6591. }
  6592. }
  6593. $return .= "\t\t\t\t".'</select>';
  6594. $return .= '</td>';
  6595. $return .= '</tr>';
  6596. if ($action != 'move') {
  6597. $return .= '<tr>';
  6598. $return .= '</tr>';
  6599. $id_prerequisite = 0;
  6600. if (is_array($arrLP)) {
  6601. foreach ($arrLP as $key => $value) {
  6602. if ($value['id'] == $id) {
  6603. $id_prerequisite = $value['prerequisite'];
  6604. break;
  6605. }
  6606. }
  6607. }
  6608. $arrHide = array();
  6609. for ($i = 0; $i < count($arrLP); $i++) {
  6610. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6611. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6612. $s_selected_position = $arrLP[$i]['id'];
  6613. } elseif ($action == 'add') {
  6614. $s_selected_position = 0;
  6615. }
  6616. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6617. }
  6618. }
  6619. $return .= '</tr>';
  6620. }
  6621. $return .= '<tr>';
  6622. if ($action == 'add') {
  6623. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6624. 'AddForumToCourse'
  6625. ).' </button></td>';
  6626. } else {
  6627. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> '.get_lang(
  6628. 'EditCurrentForum'
  6629. ).' </button></td>';
  6630. }
  6631. $return .= '</tr>';
  6632. $return .= '</table>';
  6633. if ($action == 'move') {
  6634. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6635. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6636. }
  6637. if (is_numeric($extra_info)) {
  6638. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6639. } elseif (is_array($extra_info)) {
  6640. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6641. }
  6642. $return .= '<input name="type" type="hidden" value="'.TOOL_FORUM.'" />';
  6643. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6644. $return .= '</form>';
  6645. $return .= '</div>';
  6646. return $return;
  6647. }
  6648. /**
  6649. * Return HTML form to add/edit forum threads
  6650. * @param string Action (add/edit)
  6651. * @param integer Item ID if already exists in learning path
  6652. * @param mixed Extra information (thread ID if integer)
  6653. * @return string HTML form
  6654. */
  6655. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6656. {
  6657. $course_id = api_get_course_int_id();
  6658. if (empty($course_id)) {
  6659. return null;
  6660. }
  6661. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6662. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6663. if ($id != 0 && is_array($extra_info)) {
  6664. $item_title = stripslashes($extra_info['title']);
  6665. } elseif (is_numeric($extra_info)) {
  6666. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6667. WHERE c_id = $course_id AND thread_id = ".$extra_info;
  6668. $result = Database::query($sql_forum);
  6669. $row = Database :: fetch_array($result);
  6670. $item_title = $row['title'];
  6671. $item_description = '';
  6672. } else {
  6673. $item_title = '';
  6674. $item_description = '';
  6675. }
  6676. $return = null;
  6677. if ($id != 0 && is_array($extra_info)) {
  6678. $parent = $extra_info['parent_item_id'];
  6679. } else {
  6680. $parent = 0;
  6681. }
  6682. $sql = "SELECT * FROM ".$tbl_lp_item."
  6683. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  6684. $result = Database::query($sql);
  6685. $arrLP = array();
  6686. while ($row = Database :: fetch_array($result)) {
  6687. $arrLP[] = array(
  6688. 'id' => $row['id'],
  6689. 'item_type' => $row['item_type'],
  6690. 'title' => $row['title'],
  6691. 'path' => $row['path'],
  6692. 'description' => $row['description'],
  6693. 'parent_item_id' => $row['parent_item_id'],
  6694. 'previous_item_id' => $row['previous_item_id'],
  6695. 'next_item_id' => $row['next_item_id'],
  6696. 'display_order' => $row['display_order'],
  6697. 'max_score' => $row['max_score'],
  6698. 'min_score' => $row['min_score'],
  6699. 'mastery_score' => $row['mastery_score'],
  6700. 'prerequisite' => $row['prerequisite']
  6701. );
  6702. }
  6703. $this->tree_array($arrLP);
  6704. $arrLP = $this->arrMenu;
  6705. unset ($this->arrMenu);
  6706. $return .= '<form method="POST">';
  6707. if ($action == 'add') {
  6708. $return .= '<legend>'.get_lang('CreateTheForum').'</legend>';
  6709. } elseif ($action == 'move') {
  6710. $return .= '<p class="lp_title">'.get_lang('MoveTheCurrentForum').'&nbsp;:</p>';
  6711. } else {
  6712. $return .= '<legend>'.get_lang('EditCurrentForum').'</legend>';
  6713. }
  6714. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6715. $return .= '<tr>';
  6716. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  6717. $return .= '<td class="input">';
  6718. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6719. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  6720. $arrHide = array(
  6721. $id
  6722. );
  6723. for ($i = 0; $i < count($arrLP); $i++) {
  6724. if ($action != 'add') {
  6725. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6726. $arrLP[$i]['id'],
  6727. $arrHide
  6728. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6729. ) {
  6730. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6731. } else {
  6732. $arrHide[] = $arrLP[$i]['id'];
  6733. }
  6734. } else {
  6735. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6736. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  6737. }
  6738. }
  6739. }
  6740. if ($arrLP != null) {
  6741. reset($arrLP);
  6742. }
  6743. $return .= '</select>';
  6744. $return .= '</td>';
  6745. $return .= '</tr>';
  6746. $return .= '<tr>';
  6747. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  6748. $return .= '<td class="input">';
  6749. $return .= "\t\t\t\t".'<select id="previous" name="previous" size="1">';
  6750. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  6751. for ($i = 0; $i < count($arrLP); $i++) {
  6752. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6753. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6754. $selected = 'selected="selected" ';
  6755. } elseif ($action == 'add') {
  6756. $selected = 'selected="selected" ';
  6757. } else {
  6758. $selected = '';
  6759. }
  6760. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  6761. 'After'
  6762. ).' "'.$arrLP[$i]['title'].'"</option>';
  6763. }
  6764. }
  6765. $return .= "\t\t\t\t".'</select>';
  6766. $return .= '</td>';
  6767. $return .= '</tr>';
  6768. if ($action != 'move') {
  6769. $return .= '<tr>';
  6770. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  6771. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="'.$item_title.'" /></td>';
  6772. $return .= '</tr>';
  6773. $return .= '<tr>';
  6774. $return .= '</tr>';
  6775. $id_prerequisite = 0;
  6776. if ($arrLP != null) {
  6777. foreach ($arrLP as $key => $value) {
  6778. if ($value['id'] == $id) {
  6779. $id_prerequisite = $value['prerequisite'];
  6780. break;
  6781. }
  6782. }
  6783. }
  6784. $arrHide = array();
  6785. for ($i = 0; $i < count($arrLP); $i++) {
  6786. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6787. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6788. $s_selected_position = $arrLP[$i]['id'];
  6789. } elseif ($action == 'add') {
  6790. $s_selected_position = 0;
  6791. }
  6792. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6793. }
  6794. }
  6795. $return .= '<tr>';
  6796. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang(
  6797. 'LearnpathPrerequisites'
  6798. ).'</label></td>';
  6799. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">'.get_lang(
  6800. 'NoPrerequisites'
  6801. ).'</option>';
  6802. foreach ($arrHide as $key => $value) {
  6803. if ($key == $s_selected_position && $action == 'add') {
  6804. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6805. } elseif ($key == $id_prerequisite && $action == 'edit') {
  6806. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  6807. } else {
  6808. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  6809. }
  6810. }
  6811. $return .= "</select></td>";
  6812. $return .= '</tr>';
  6813. }
  6814. $return .= '<tr>';
  6815. $return .= '<td></td><td>
  6816. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang(
  6817. 'Ok'
  6818. ).'</button></td>';
  6819. $return .= '</tr>';
  6820. $return .= '</table>';
  6821. if ($action == 'move') {
  6822. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  6823. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  6824. }
  6825. if (is_numeric($extra_info)) {
  6826. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  6827. } elseif (is_array($extra_info)) {
  6828. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  6829. }
  6830. $return .= '<input name="type" type="hidden" value="'.TOOL_THREAD.'" />';
  6831. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  6832. $return .= '</form>';
  6833. $return .= '</div>';
  6834. return $return;
  6835. }
  6836. /**
  6837. * Return the HTML form to display an item (generally a section/module item)
  6838. * @param string Item type (module/dokeos_module)
  6839. * @param string Title (optional, only when creating)
  6840. * @param string Action ('add'/'edit')
  6841. * @param integer lp_item ID
  6842. * @param mixed Extra info
  6843. * @return string HTML form
  6844. */
  6845. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6846. {
  6847. $course_id = api_get_course_int_id();
  6848. global $_course;
  6849. global $charset;
  6850. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6851. if ($id != 0 && is_array($extra_info)) {
  6852. $item_title = $extra_info['title'];
  6853. $item_description = $extra_info['description'];
  6854. $item_path = api_get_path(WEB_COURSE_PATH).$_course['path'].'/scorm/'.$this->path.'/'.stripslashes(
  6855. $extra_info['path']
  6856. );
  6857. $item_path_fck = '/scorm/'.$this->path.'/'.stripslashes($extra_info['path']);
  6858. } else {
  6859. $item_title = '';
  6860. $item_description = '';
  6861. $item_path_fck = '';
  6862. }
  6863. if ($id != 0 && is_array($extra_info)) {
  6864. $parent = $extra_info['parent_item_id'];
  6865. } else {
  6866. $parent = 0;
  6867. }
  6868. $id = intval($id);
  6869. $sql = "SELECT * FROM ".$tbl_lp_item."
  6870. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND id != $id";
  6871. if ($item_type == 'module') {
  6872. $sql .= " AND parent_item_id = 0";
  6873. }
  6874. $result = Database::query($sql);
  6875. $arrLP = array();
  6876. while ($row = Database :: fetch_array($result)) {
  6877. $arrLP[] = array(
  6878. 'id' => $row['id'],
  6879. 'item_type' => $row['item_type'],
  6880. 'title' => $row['title'],
  6881. 'path' => $row['path'],
  6882. 'description' => $row['description'],
  6883. 'parent_item_id' => $row['parent_item_id'],
  6884. 'previous_item_id' => $row['previous_item_id'],
  6885. 'next_item_id' => $row['next_item_id'],
  6886. 'max_score' => $row['max_score'],
  6887. 'min_score' => $row['min_score'],
  6888. 'mastery_score' => $row['mastery_score'],
  6889. 'prerequisite' => $row['prerequisite'],
  6890. 'display_order' => $row['display_order']
  6891. );
  6892. }
  6893. $this->tree_array($arrLP);
  6894. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6895. unset ($this->arrMenu);
  6896. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6897. $url = api_get_self().'?'.api_get_cidreq(
  6898. ).'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6899. $form = new FormValidator('form', 'POST', $url);
  6900. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6901. $defaults['description'] = $item_description;
  6902. $form->addElement('header', $title);
  6903. //$arrHide = array($id);
  6904. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6905. $arrHide[0]['padding'] = 3;
  6906. $charset = api_get_system_encoding();
  6907. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6908. for ($i = 0; $i < count($arrLP); $i++) {
  6909. if ($action != 'add') {
  6910. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  6911. $arrLP[$i]['id'],
  6912. $arrHide
  6913. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  6914. ) {
  6915. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6916. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6917. if ($parent == $arrLP[$i]['id']) {
  6918. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6919. }
  6920. }
  6921. } else {
  6922. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6923. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6924. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6925. if ($parent == $arrLP[$i]['id']) {
  6926. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6927. }
  6928. }
  6929. }
  6930. }
  6931. if ($action != 'move') {
  6932. $form->addElement(
  6933. 'text',
  6934. 'title',
  6935. get_lang('Title'),
  6936. 'id="idTitle" class="learnpath_chapter_form" size="40%"'
  6937. );
  6938. $form->applyFilter('title', 'html_filter');
  6939. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6940. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6941. } else {
  6942. $form->addElement('hidden', 'title');
  6943. }
  6944. $parent_select = $form->addElement(
  6945. 'select',
  6946. 'parent',
  6947. get_lang('Parent'),
  6948. '',
  6949. 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"'
  6950. );
  6951. foreach ($arrHide as $key => $value) {
  6952. $parent_select->addOption($value['value'], $key, 'style="padding-left:'.$value['padding'].'px;"');
  6953. }
  6954. if (!empty($s_selected_parent)) {
  6955. $parent_select->setSelected($s_selected_parent);
  6956. }
  6957. }
  6958. if (is_array($arrLP)) {
  6959. reset($arrLP);
  6960. }
  6961. $arrHide = array();
  6962. // POSITION
  6963. for ($i = 0; $i < count($arrLP); $i++) {
  6964. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6965. //this is the same!
  6966. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6967. $s_selected_position = $arrLP[$i]['id'];
  6968. } elseif ($action == 'add') {
  6969. $s_selected_position = $arrLP[$i]['id'];
  6970. }
  6971. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After').' "'.$arrLP[$i]['title'].'"';
  6972. }
  6973. }
  6974. $position = $form->addElement(
  6975. 'select',
  6976. 'previous',
  6977. get_lang('Position'),
  6978. '',
  6979. 'id="previous" class="learnpath_chapter_form" style="width:37%;"'
  6980. );
  6981. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6982. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:'.$padding.'px;"');
  6983. foreach ($arrHide as $key => $value) {
  6984. $position->addOption($value['value'].'"', $key, 'style="padding-left:'.$padding.'px;"');
  6985. }
  6986. if (!empty ($s_selected_position)) {
  6987. $position->setSelected($s_selected_position);
  6988. }
  6989. if (is_array($arrLP)) {
  6990. reset($arrLP);
  6991. }
  6992. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6993. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6994. $form->addElement('hidden', 'parent', '0');
  6995. }
  6996. //fix in order to use the tab
  6997. if ($item_type == 'chapter') {
  6998. $form->addElement('hidden', 'type', 'chapter');
  6999. }
  7000. $extension = null;
  7001. if (!empty($item_path)) {
  7002. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  7003. }
  7004. //assets can't be modified
  7005. //$item_type == 'asset' ||
  7006. if (($item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  7007. if ($item_type == 'sco') {
  7008. $form->addElement(
  7009. 'html',
  7010. '<script type="text/javascript">alert("'.get_lang('WarningWhenEditingScorm').'")</script>'
  7011. );
  7012. }
  7013. $renderer = $form->defaultRenderer();
  7014. $renderer->setElementTemplate(
  7015. '<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}',
  7016. 'content_lp'
  7017. );
  7018. $relative_prefix = '';
  7019. $editor_config = array(
  7020. 'ToolbarSet' => 'LearningPathDocuments',
  7021. 'Width' => '100%',
  7022. 'Height' => '500',
  7023. 'FullPage' => true,
  7024. 'CreateDocumentDir' => $relative_prefix,
  7025. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/scorm/',
  7026. 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().$item_path_fck
  7027. );
  7028. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  7029. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  7030. //$defaults['content_lp'] = file_get_contents($item_path);
  7031. $defaults['content_lp'] = file_get_contents($content_path);
  7032. }
  7033. $form->addElement('hidden', 'type', 'dokeos_'.$item_type);
  7034. $form->addElement('hidden', 'post_time', time());
  7035. $form->setDefaults($defaults);
  7036. return $form->return_form();
  7037. }
  7038. /**
  7039. * Returns the form to update or create a document
  7040. * @param string Action (add/edit)
  7041. * @param integer ID of the lp_item (if already exists)
  7042. * @param mixed Integer if document ID, string if info ('new')
  7043. * @return string HTML form
  7044. */
  7045. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  7046. {
  7047. $course_id = api_get_course_int_id();
  7048. global $charset;
  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. global $charset;
  7378. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7379. $tbl_link = Database :: get_course_table(TABLE_LINK);
  7380. if ($id != 0 && is_array($extra_info)) {
  7381. $item_title = stripslashes($extra_info['title']);
  7382. $item_description = stripslashes($extra_info['description']);
  7383. $item_url = stripslashes($extra_info['url']);
  7384. } elseif (is_numeric($extra_info)) {
  7385. $sql_link = "SELECT title, description, url FROM ".$tbl_link." WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7386. $result = Database::query($sql_link);
  7387. $row = Database :: fetch_array($result);
  7388. $item_title = $row['title'];
  7389. $item_description = $row['description'];
  7390. $item_url = $row['url'];
  7391. } else {
  7392. $item_title = '';
  7393. $item_description = '';
  7394. $item_url = '';
  7395. }
  7396. $legend = '<legend>';
  7397. if ($id != 0 && is_array($extra_info)) {
  7398. $parent = $extra_info['parent_item_id'];
  7399. } else {
  7400. $parent = 0;
  7401. }
  7402. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7403. $result = Database::query($sql);
  7404. $arrLP = array();
  7405. while ($row = Database :: fetch_array($result)) {
  7406. $arrLP[] = array(
  7407. 'id' => $row['id'],
  7408. 'item_type' => $row['item_type'],
  7409. 'title' => $row['title'],
  7410. 'path' => $row['path'],
  7411. 'description' => $row['description'],
  7412. 'parent_item_id' => $row['parent_item_id'],
  7413. 'previous_item_id' => $row['previous_item_id'],
  7414. 'next_item_id' => $row['next_item_id'],
  7415. 'display_order' => $row['display_order'],
  7416. 'max_score' => $row['max_score'],
  7417. 'min_score' => $row['min_score'],
  7418. 'mastery_score' => $row['mastery_score'],
  7419. 'prerequisite' => $row['prerequisite']
  7420. );
  7421. }
  7422. $this->tree_array($arrLP);
  7423. $arrLP = $this->arrMenu;
  7424. unset ($this->arrMenu);
  7425. if ($action == 'add') {
  7426. $legend .= get_lang('CreateTheLink').'&nbsp;:';
  7427. } elseif ($action == 'move') {
  7428. $legend .= get_lang('MoveCurrentLink').'&nbsp;:';
  7429. } else {
  7430. $legend .= get_lang('EditCurrentLink').'&nbsp;:';
  7431. }
  7432. $legend .= '</legend>';
  7433. $return .= '<div class="sectioncomment">';
  7434. $return .= '<form method="POST">';
  7435. $return .= $legend;
  7436. $return .= '<table>';
  7437. if ($action != 'move') {
  7438. $return .= '<tr>';
  7439. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7440. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form"/></td>';
  7441. $return .= '</tr>';
  7442. }
  7443. $return .= '<tr>';
  7444. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7445. $return .= '<td class="input">';
  7446. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7447. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7448. $arrHide = array(
  7449. $id
  7450. );
  7451. $parent_item_id = $_SESSION['parent_item_id'];
  7452. for ($i = 0; $i < count($arrLP); $i++) {
  7453. if ($action != 'add') {
  7454. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7455. $arrLP[$i]['id'],
  7456. $arrHide
  7457. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7458. ) {
  7459. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7460. } else {
  7461. $arrHide[] = $arrLP[$i]['id'];
  7462. }
  7463. } else {
  7464. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7465. $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>';
  7466. }
  7467. }
  7468. }
  7469. if (is_array($arrLP)) {
  7470. reset($arrLP);
  7471. }
  7472. $return .= '</select>';
  7473. $return .= '</td>';
  7474. $return .= '</tr>';
  7475. $return .= '<tr>';
  7476. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7477. $return .= '<td class="input">';
  7478. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7479. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7480. for ($i = 0; $i < count($arrLP); $i++) {
  7481. if ($arrLP[$i]['parent_item_id'] == $parent_item_id && $arrLP[$i]['id'] != $id) {
  7482. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7483. $selected = 'selected="selected" ';
  7484. } elseif ($action == 'add') {
  7485. $selected = 'selected="selected" ';
  7486. } else {
  7487. $selected = '';
  7488. }
  7489. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7490. 'After'
  7491. ).' "'.$arrLP[$i]['title'].'"</option>';
  7492. }
  7493. }
  7494. $return .= '</select>';
  7495. $return .= '</td>';
  7496. $return .= '</tr>';
  7497. if ($action != 'move') {
  7498. $return .= '<tr>';
  7499. $return .= '<td class="label"><label for="idURL">'.get_lang('Url').'</label></td>';
  7500. $return .= '<td class="input"><input'.(is_numeric(
  7501. $extra_info
  7502. ) ? ' disabled="disabled"' : '').' id="idURL" name="url" style="width:99%;" type="text" value="'.$item_url.'" class="learnpath_item_form" /></td>';
  7503. $return .= '</tr>';
  7504. $id_prerequisite = 0;
  7505. if (is_array($arrLP)) {
  7506. foreach ($arrLP as $key => $value) {
  7507. if ($value['id'] == $id) {
  7508. $id_prerequisite = $value['prerequisite'];
  7509. break;
  7510. }
  7511. }
  7512. }
  7513. $arrHide = array();
  7514. for ($i = 0; $i < count($arrLP); $i++) {
  7515. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7516. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7517. $s_selected_position = $arrLP[$i]['id'];
  7518. } elseif ($action == 'add') {
  7519. $s_selected_position = 0;
  7520. }
  7521. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7522. }
  7523. }
  7524. $return .= '</tr>';
  7525. }
  7526. $return .= '<tr>';
  7527. if ($action == 'add') {
  7528. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7529. 'AddLinkToCourse'
  7530. ).'</button></td>';
  7531. } else {
  7532. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7533. 'EditCurrentLink'
  7534. ).'</button></td>';
  7535. }
  7536. $return .= '</tr>';
  7537. $return .= '</table>';
  7538. if ($action == 'move') {
  7539. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7540. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7541. }
  7542. if (is_numeric($extra_info)) {
  7543. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7544. } elseif (is_array($extra_info)) {
  7545. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7546. }
  7547. $return .= '<input name="type" type="hidden" value="'.TOOL_LINK.'" />';
  7548. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7549. $return .= '</form>';
  7550. $return .= '</div>';
  7551. return $return;
  7552. }
  7553. /**
  7554. * Return HTML form to add/edit a student publication (work)
  7555. * @param string Action (add/edit)
  7556. * @param integer Item ID if already exists
  7557. * @param mixed Extra info (work ID if integer)
  7558. * @return string HTML form
  7559. */
  7560. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7561. {
  7562. $course_id = api_get_course_int_id();
  7563. global $charset;
  7564. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7565. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7566. if ($id != 0 && is_array($extra_info)) {
  7567. $item_title = stripslashes($extra_info['title']);
  7568. $item_description = stripslashes($extra_info['description']);
  7569. } elseif (is_numeric($extra_info)) {
  7570. $sql_publication = "SELECT title, description FROM ".$tbl_publication."
  7571. WHERE c_id = ".$course_id." AND id = ".$extra_info;
  7572. $result = Database::query($sql_publication);
  7573. $row = Database :: fetch_array($result);
  7574. $item_title = $row['title'];
  7575. } else {
  7576. $item_title = get_lang('Student_publication');
  7577. }
  7578. $legend = '<legend>';
  7579. if ($id != 0 && is_array($extra_info)) {
  7580. $parent = $extra_info['parent_item_id'];
  7581. } else {
  7582. $parent = 0;
  7583. }
  7584. $sql = "SELECT * FROM ".$tbl_lp_item."
  7585. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7586. $result = Database::query($sql);
  7587. $arrLP = array();
  7588. while ($row = Database :: fetch_array($result)) {
  7589. $arrLP[] = array(
  7590. 'id' => $row['id'],
  7591. 'item_type' => $row['item_type'],
  7592. 'title' => $row['title'],
  7593. 'path' => $row['path'],
  7594. 'description' => $row['description'],
  7595. 'parent_item_id' => $row['parent_item_id'],
  7596. 'previous_item_id' => $row['previous_item_id'],
  7597. 'next_item_id' => $row['next_item_id'],
  7598. 'display_order' => $row['display_order'],
  7599. 'max_score' => $row['max_score'],
  7600. 'min_score' => $row['min_score'],
  7601. 'mastery_score' => $row['mastery_score'],
  7602. 'prerequisite' => $row['prerequisite']
  7603. );
  7604. }
  7605. $this->tree_array($arrLP);
  7606. $arrLP = $this->arrMenu;
  7607. unset ($this->arrMenu);
  7608. if ($action == 'add') {
  7609. $legend .= get_lang('Student_publication').'&nbsp;:'."\n";
  7610. } elseif ($action == 'move') {
  7611. $legend .= get_lang('MoveCurrentStudentPublication').'&nbsp;:'."\n";
  7612. } else {
  7613. $legend .= get_lang('EditCurrentStudentPublication').'&nbsp;:'."\n";
  7614. }
  7615. $legend .= '</legend>';
  7616. $return .= '<div class="sectioncomment">';
  7617. $return .= '<form method="POST">';
  7618. $return .= $legend;
  7619. $return .= '<table class="lp_form">';
  7620. if ($action != 'move') {
  7621. $return .= '<tr>';
  7622. $return .= '<td class="label"><label for="idTitle">'.get_lang('Title').'</label></td>';
  7623. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="'.$item_title.'" class="learnpath_item_form" /></td>';
  7624. $return .= '</tr>';
  7625. }
  7626. $return .= '<tr>';
  7627. $return .= '<td class="label"><label for="idParent">'.get_lang('Parent').'</label></td>';
  7628. $return .= '<td class="input">';
  7629. $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">';
  7630. //$parent_item_id = $_SESSION['parent_item_id'];
  7631. $return .= '<option class="top" value="0">'.$this->name.'</option>';
  7632. $arrHide = array(
  7633. $id
  7634. );
  7635. for ($i = 0; $i < count($arrLP); $i++) {
  7636. if ($action != 'add') {
  7637. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array(
  7638. $arrLP[$i]['id'],
  7639. $arrHide
  7640. ) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)
  7641. ) {
  7642. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7643. } else {
  7644. $arrHide[] = $arrLP[$i]['id'];
  7645. }
  7646. } else {
  7647. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  7648. $return .= '<option '.(($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '').'style="padding-left:'.($arrLP[$i]['depth'] * 10).'px;" value="'.$arrLP[$i]['id'].'">'.$arrLP[$i]['title'].'</option>';
  7649. }
  7650. }
  7651. }
  7652. if (is_array($arrLP)) {
  7653. reset($arrLP);
  7654. }
  7655. $return .= "\t\t\t\t".'</select>';
  7656. $return .= '</td>';
  7657. $return .= '</tr>';
  7658. $return .= '<tr>';
  7659. $return .= '<td class="label"><label for="previous">'.get_lang('Position').'</label></td>';
  7660. $return .= '<td class="input">';
  7661. $return .= "\t\t\t\t".'<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7662. $return .= '<option class="top" value="0">'.get_lang('FirstPosition').'</option>';
  7663. for ($i = 0; $i < count($arrLP); $i++) {
  7664. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7665. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7666. $selected = 'selected="selected" ';
  7667. } elseif ($action == 'add') {
  7668. $selected = 'selected="selected" ';
  7669. } else {
  7670. $selected = '';
  7671. }
  7672. $return .= '<option '.$selected.'value="'.$arrLP[$i]['id'].'">'.get_lang(
  7673. 'After'
  7674. ).' "'.$arrLP[$i]['title'].'"</option>';
  7675. }
  7676. }
  7677. $return .= "\t\t\t\t".'</select>';
  7678. $return .= '</td>';
  7679. $return .= '</tr>';
  7680. if ($action != 'move') {
  7681. $id_prerequisite = 0;
  7682. if (is_array($arrLP)) {
  7683. foreach ($arrLP as $key => $value) {
  7684. if ($value['id'] == $id) {
  7685. $id_prerequisite = $value['prerequisite'];
  7686. break;
  7687. }
  7688. }
  7689. }
  7690. $arrHide = array();
  7691. for ($i = 0; $i < count($arrLP); $i++) {
  7692. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7693. if ($extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  7694. $s_selected_position = $arrLP[$i]['id'];
  7695. } elseif ($action == 'add') {
  7696. $s_selected_position = 0;
  7697. }
  7698. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7699. }
  7700. }
  7701. // Commented the prerequisites, only visible in edit (work).
  7702. /*
  7703. $return .= '<tr>';
  7704. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7705. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7706. foreach($arrHide as $key => $value) {
  7707. if ($key == $s_selected_position && $action == 'add') {
  7708. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7709. }
  7710. elseif ($key == $id_prerequisite && $action == 'edit') {
  7711. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7712. }
  7713. else {
  7714. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7715. }
  7716. }
  7717. $return .= "</select></td>";
  7718. */
  7719. $return .= '</tr>';
  7720. }
  7721. $return .= '<tr>';
  7722. if ($action == 'add') {
  7723. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7724. 'AddAssignmentToCourse'
  7725. ).'</button></td>';
  7726. } else {
  7727. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">'.get_lang(
  7728. 'EditCurrentStudentPublication'
  7729. ).'</button></td>';
  7730. }
  7731. $return .= '</tr>';
  7732. $return .= '</table>';
  7733. if ($action == 'move') {
  7734. $return .= '<input name="title" type="hidden" value="'.$item_title.'" />';
  7735. $return .= '<input name="description" type="hidden" value="'.$item_description.'" />';
  7736. }
  7737. if (is_numeric($extra_info)) {
  7738. $return .= '<input name="path" type="hidden" value="'.$extra_info.'" />';
  7739. } elseif (is_array($extra_info)) {
  7740. $return .= '<input name="path" type="hidden" value="'.$extra_info['path'].'" />';
  7741. }
  7742. $return .= '<input name="type" type="hidden" value="'.TOOL_STUDENTPUBLICATION.'" />';
  7743. $return .= '<input name="post_time" type="hidden" value="'.time().'" />';
  7744. $return .= '</form>';
  7745. $return .= '</div>';
  7746. return $return;
  7747. }
  7748. /**
  7749. * Displays the menu for manipulating a step
  7750. * @return string html
  7751. */
  7752. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7753. {
  7754. $course_id = api_get_course_int_id();
  7755. $course_code = api_get_course_id();
  7756. global $charset, $_course;
  7757. $return = '<div class="actions">';
  7758. switch ($item_type) {
  7759. case 'dokeos_chapter' :
  7760. case 'chapter' :
  7761. // Commented the message cause should not show it.
  7762. //$lang = get_lang('TitleManipulateChapter');
  7763. break;
  7764. case 'dokeos_module' :
  7765. case 'module' :
  7766. // Commented the message cause should not show it.
  7767. //$lang = get_lang('TitleManipulateModule');
  7768. break;
  7769. case TOOL_DOCUMENT :
  7770. // Commented the message cause should not show it.
  7771. //$lang = get_lang('TitleManipulateDocument');
  7772. break;
  7773. case TOOL_LINK :
  7774. case 'link' :
  7775. // Commented the message cause should not show it.
  7776. //$lang = get_lang('TitleManipulateLink');
  7777. break;
  7778. case TOOL_QUIZ :
  7779. // Commented the message cause should not show it.
  7780. //$lang = get_lang('TitleManipulateQuiz');
  7781. break;
  7782. case TOOL_STUDENTPUBLICATION :
  7783. // Commented the message cause should not show it.
  7784. //$lang = get_lang('TitleManipulateStudentPublication');
  7785. break;
  7786. }
  7787. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7788. $item_id = intval($item_id);
  7789. $sql = "SELECT * FROM ".$tbl_lp_item." as lp WHERE lp.c_id = ".$course_id." AND lp.id = ".$item_id;
  7790. $result = Database::query($sql);
  7791. $row = Database::fetch_assoc($result);
  7792. $audio_player = null;
  7793. // We display an audio player if needed.
  7794. if (!empty($row['audio'])) {
  7795. $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>';
  7796. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7797. $audio_player .= '<script>
  7798. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7799. s1.addParam("allowscriptaccess","always");
  7800. s1.addParam("flashvars","file=../../courses/'.$_course['path'].'/document/audio/'.$row['audio'].'&autostart=true");
  7801. s1.write("container");
  7802. </script>';
  7803. }
  7804. $url = api_get_self().'?cidReq='.Security::remove_XSS(
  7805. $_GET['cidReq']
  7806. ).'&view=build&id='.$item_id.'&lp_id='.$this->lp_id;
  7807. $return .= Display::url(
  7808. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7809. $url.'&action=edit_item&path_item='.$row['path']
  7810. );
  7811. $return .= Display::url(
  7812. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7813. $url.'&action=move_item'
  7814. );
  7815. // Commented for now as prerequisites cannot be added to chapters.
  7816. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7817. $return .= Display::url(
  7818. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7819. $url.'&action=edit_item_prereq'
  7820. );
  7821. }
  7822. $return .= Display::url(
  7823. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7824. $url.'&action=delete_item'
  7825. );
  7826. if ($item_type == TOOL_HOTPOTATOES) {
  7827. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7828. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7829. }
  7830. if ($item_type == TOOL_DOCUMENT) {
  7831. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7832. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7833. }
  7834. $return .= '</div>';
  7835. if (!empty($audio_player)) {
  7836. $return .= '<br />'.$audio_player;
  7837. }
  7838. return $return;
  7839. }
  7840. /**
  7841. * Creates the javascript needed for filling up the checkboxes without page reload
  7842. *
  7843. * @return string
  7844. */
  7845. public function get_js_dropdown_array()
  7846. {
  7847. $course_id = api_get_course_int_id();
  7848. $return = 'var child_name = new Array();'."\n";
  7849. $return .= 'var child_value = new Array();'."\n\n";
  7850. $return .= 'child_name[0] = new Array();'."\n";
  7851. $return .= 'child_value[0] = new Array();'."\n\n";
  7852. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7853. $sql_zero = "SELECT * FROM ".$tbl_lp_item."
  7854. WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id." AND parent_item_id = 0
  7855. ORDER BY display_order ASC";
  7856. $res_zero = Database::query($sql_zero);
  7857. global $charset;
  7858. $i = 0;
  7859. while ($row_zero = Database :: fetch_array($res_zero)) {
  7860. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7861. $return .= 'child_name[0]['.$i.'] = '.$js_var.' ;'."\n";
  7862. $return .= 'child_value[0]['.$i++.'] = "'.$row_zero['id'].'";'."\n";
  7863. }
  7864. $return .= "\n";
  7865. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = ".$course_id." AND lp_id = ".$this->lp_id;
  7866. $res = Database::query($sql);
  7867. while ($row = Database :: fetch_array($res)) {
  7868. $sql_parent = "
  7869. SELECT * FROM ".$tbl_lp_item."
  7870. WHERE c_id = ".$course_id." AND parent_item_id = ".$row['id']."
  7871. ORDER BY display_order ASC";
  7872. $res_parent = Database::query($sql_parent);
  7873. $i = 0;
  7874. $return .= 'child_name['.$row['id'].'] = new Array();'."\n";
  7875. $return .= 'child_value['.$row['id'].'] = new Array();'."\n\n";
  7876. while ($row_parent = Database :: fetch_array($res_parent)) {
  7877. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7878. $return .= 'child_name['.$row['id'].']['.$i.'] = '.$js_var.' ;'."\n";
  7879. $return .= 'child_value['.$row['id'].']['.$i++.'] = "'.$row_parent['id'].'";'."\n";
  7880. }
  7881. $return .= "\n";
  7882. }
  7883. return $return;
  7884. }
  7885. /**
  7886. * Display the form to allow moving an item
  7887. * @param integer Item ID
  7888. * @return string HTML form
  7889. */
  7890. public function display_move_item($item_id)
  7891. {
  7892. $course_id = api_get_course_int_id();
  7893. global $_course; //will disappear
  7894. global $charset;
  7895. $return = '';
  7896. if (is_numeric($item_id)) {
  7897. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7898. $sql = "SELECT * FROM ".$tbl_lp_item."
  7899. WHERE c_id = ".$course_id." AND id = ".$item_id;
  7900. $res = Database::query($sql);
  7901. $row = Database :: fetch_array($res);
  7902. switch ($row['item_type']) {
  7903. case 'dokeos_chapter' :
  7904. case 'dir' :
  7905. case 'asset' :
  7906. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7907. $return .= $this->display_item_form(
  7908. $row['item_type'],
  7909. get_lang('MoveCurrentChapter'),
  7910. 'move',
  7911. $item_id,
  7912. $row
  7913. );
  7914. break;
  7915. case 'dokeos_module' :
  7916. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7917. $return .= $this->display_item_form(
  7918. $row['item_type'],
  7919. 'Move th current module:',
  7920. 'move',
  7921. $item_id,
  7922. $row
  7923. );
  7924. break;
  7925. case TOOL_DOCUMENT :
  7926. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7927. $return .= $this->display_document_form('move', $item_id, $row);
  7928. break;
  7929. case TOOL_LINK :
  7930. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7931. $return .= $this->display_link_form('move', $item_id, $row);
  7932. break;
  7933. case TOOL_HOTPOTATOES :
  7934. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7935. $return .= $this->display_link_form('move', $item_id, $row);
  7936. break;
  7937. case TOOL_QUIZ :
  7938. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7939. $return .= $this->display_quiz_form('move', $item_id, $row);
  7940. break;
  7941. case TOOL_STUDENTPUBLICATION :
  7942. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7943. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7944. break;
  7945. case TOOL_FORUM :
  7946. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7947. $return .= $this->display_forum_form('move', $item_id, $row);
  7948. break;
  7949. case TOOL_THREAD :
  7950. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7951. $return .= $this->display_forum_form('move', $item_id, $row);
  7952. break;
  7953. }
  7954. }
  7955. return $return;
  7956. }
  7957. /**
  7958. * Displays a basic form on the overview page for changing the item title and the item description.
  7959. * @param string $item_type
  7960. * @param string $title
  7961. * @param array $data
  7962. * @return string
  7963. */
  7964. public function display_item_small_form($item_type, $title = '', $data = array())
  7965. {
  7966. $url = api_get_self().'?'.api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7967. $form = new FormValidator('small_form', 'post', $url);
  7968. $form->addElement('header', $title);
  7969. $form->addElement('text', 'title', get_lang('Title'));
  7970. $form->addElement('button', 'submit_button', get_lang('Save'));
  7971. $form->addElement('hidden', 'id', $data['id']);
  7972. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7973. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7974. $form->setDefaults(array('title' => $data['title']));
  7975. return $form->toHtml();
  7976. }
  7977. /**
  7978. * Return HTML form to allow prerequisites selection
  7979. * @param integer Item ID
  7980. * @return string HTML form
  7981. */
  7982. public function display_item_prerequisites_form($item_id)
  7983. {
  7984. $course_id = api_get_course_int_id();
  7985. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7986. $item_id = intval($item_id);
  7987. /* Current prerequisite */
  7988. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = ".$item_id;
  7989. $result = Database::query($sql);
  7990. $row = Database::fetch_array($result);
  7991. $preq_id = $row['prerequisite'];
  7992. $return = '<legend>';
  7993. $return .= get_lang('AddEditPrerequisites');
  7994. $return .= '</legend>';
  7995. $return .= '<form method="POST">';
  7996. $return .= '<table class="data_table">';
  7997. $return .= '<tr>';
  7998. $return .= '<th height="24">'.get_lang('LearnpathPrerequisites').'</th>';
  7999. $return .= '<th width="70" height="24">'.get_lang('Minimum').'</th>';
  8000. $return .= '<th width="70" height="24">'.get_lang('Maximum').'</th>';
  8001. $return .= '</tr>';
  8002. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  8003. $return .= '<tr >';
  8004. $return .= '<td colspan="3" class="radio">';
  8005. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  8006. $return .= '<label for="idNone">'.get_lang('None').'</label>';
  8007. $return .= '</tr>';
  8008. $sql = "SELECT * FROM ".$tbl_lp_item." WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  8009. $result = Database::query($sql);
  8010. $arrLP = array();
  8011. while ($row = Database :: fetch_array($result)) {
  8012. $arrLP[] = array(
  8013. 'id' => $row['id'],
  8014. 'item_type' => $row['item_type'],
  8015. 'title' => $row['title'],
  8016. 'ref' => $row['ref'],
  8017. 'description' => $row['description'],
  8018. 'parent_item_id' => $row['parent_item_id'],
  8019. 'previous_item_id' => $row['previous_item_id'],
  8020. 'next_item_id' => $row['next_item_id'],
  8021. 'max_score' => $row['max_score'],
  8022. 'min_score' => $row['min_score'],
  8023. 'mastery_score' => $row['mastery_score'],
  8024. 'prerequisite' => $row['prerequisite'],
  8025. 'next_item_id' => $row['next_item_id'],
  8026. 'display_order' => $row['display_order']
  8027. );
  8028. if ($row['ref'] == $preq_id) {
  8029. $preq_mastery = $row['mastery_score'];
  8030. $preq_max = $row['max_score'];
  8031. }
  8032. }
  8033. $this->tree_array($arrLP);
  8034. $arrLP = $this->arrMenu;
  8035. unset ($this->arrMenu);
  8036. for ($i = 0; $i < count($arrLP); $i++) {
  8037. if ($arrLP[$i]['id'] == $item_id) {
  8038. break;
  8039. }
  8040. $return .= '<tr>';
  8041. $return .= '<td class="radio"'.(($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '').'>';
  8042. $return .= '<label for="id'.$arrLP[$i]['id'].'">';
  8043. $depth_multi = $arrLP[$i]['depth'] * 10;
  8044. $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'].'" />';
  8045. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  8046. if (file_exists('../img/lp_'.$icon_name.'.png')) {
  8047. $return .= '<img alt="" src="../img/lp_'.$icon_name.'.png" style="margin-right:5px;" title="" />';
  8048. } else {
  8049. if (file_exists('../img/lp_'.$icon_name.'.gif')) {
  8050. $return .= '<img alt="" src="../img/lp_'.$icon_name.'.gif" style="margin-right:5px;" title="" />';
  8051. } else {
  8052. $return .= Display::return_icon(
  8053. 'folder_document.gif',
  8054. '',
  8055. array('style' => 'margin-right:5px;')
  8056. );
  8057. }
  8058. }
  8059. $return .= $arrLP[$i]['title'].'</label>';
  8060. $return .= '</td>';
  8061. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  8062. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8063. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8064. $return .= '</td>';
  8065. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8066. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8067. $return .= '</td>';
  8068. }
  8069. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  8070. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8071. $return .= '<center><input size="4" maxlength="3" name="min_'.$arrLP[$i]['id'].'" type="text" value="'.(($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0).'" /></center>';
  8072. $return .= '</td>';
  8073. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  8074. $return .= '<center><input size="4" maxlength="3" name="max_'.$arrLP[$i]['id'].'" type="text" value="'.$arrLP[$i]['max_score'].'" disabled="true" /></center>';
  8075. $return .= '</td>';
  8076. }
  8077. $return .= '</tr>';
  8078. }
  8079. $return .= '<tr>';
  8080. $return .= '</tr>';
  8081. $return .= '</table>';
  8082. $return .= '<div style="padding-top:3px;">';
  8083. $return .= '<button class="save" name="submit_button" type="submit">'.get_lang(
  8084. 'ModifyPrerequisites'
  8085. ).'</button>';
  8086. $return .= '</form>';
  8087. return $return;
  8088. }
  8089. /**
  8090. * Return HTML list to allow prerequisites selection for lp
  8091. * @param integer Item ID
  8092. * @return string HTML form
  8093. */
  8094. public function display_lp_prerequisites_list()
  8095. {
  8096. $course_id = api_get_course_int_id();
  8097. $lp_id = $this->lp_id;
  8098. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  8099. // get current prerequisite
  8100. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  8101. $result = Database::query($sql);
  8102. $row = Database :: fetch_array($result);
  8103. $preq_id = $row['prerequisite'];
  8104. $session_id = api_get_session_id();
  8105. $session_condition = api_get_session_condition($session_id);
  8106. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id $session_condition ORDER BY display_order ";
  8107. $rs = Database::query($sql);
  8108. $return = '';
  8109. $return .= '<select name="prerequisites" >';
  8110. $return .= '<option value="0">'.get_lang('None').'</option>';
  8111. if (Database::num_rows($rs) > 0) {
  8112. while ($row = Database::fetch_array($rs)) {
  8113. if ($row['id'] == $lp_id) {
  8114. continue;
  8115. }
  8116. $return .= '<option value="'.$row['id'].'" '.(($row['id'] == $preq_id) ? ' selected ' : '').'>'.$row['name'].'</option>';
  8117. }
  8118. }
  8119. $return .= '</select>';
  8120. return $return;
  8121. }
  8122. /**
  8123. * Creates a list with all the documents in it
  8124. * @return string
  8125. */
  8126. public function get_documents()
  8127. {
  8128. $course_info = api_get_course_info();
  8129. $document_tree = DocumentManager::get_document_preview($course_info, $this->lp_id, null, 0, true);
  8130. return $document_tree;
  8131. }
  8132. /**
  8133. * Creates a list with all the exercises (quiz) in it
  8134. * @return string
  8135. */
  8136. public function get_exercises()
  8137. {
  8138. $course_id = api_get_course_int_id();
  8139. // New for hotpotatoes.
  8140. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  8141. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  8142. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  8143. $session_id = api_get_session_id();
  8144. $condition_session = api_get_session_condition($session_id);
  8145. $sql_quiz = "SELECT * FROM $tbl_quiz WHERE c_id = $course_id AND active<>'-1' $condition_session ORDER BY title ASC";
  8146. $sql_hot = "SELECT * FROM $tbl_doc WHERE c_id = $course_id AND path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC";
  8147. $res_quiz = Database::query($sql_quiz);
  8148. $res_hot = Database::query($sql_hot);
  8149. $return = '<ul class="lp_resource">';
  8150. $return .= '<li class="lp_resource_element">';
  8151. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  8152. $return .= '<a href="'.api_get_path(
  8153. REL_CODE_PATH
  8154. ).'exercice/exercise_admin.php?lp_id='.$this->lp_id.'">'.get_lang('NewExercise').'</a>';
  8155. $return .= '</li>';
  8156. // Display hotpotatoes
  8157. while ($row_hot = Database :: fetch_array($res_hot)) {
  8158. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  8159. $return .= '<a class="moved" href="#">';
  8160. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8161. $return .= '</a> ';
  8162. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  8163. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8164. ).'&amp;action=add_item&amp;type='.TOOL_HOTPOTATOES.'&amp;file='.$row_hot['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8165. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS(
  8166. $row_hot['title']
  8167. )).'</a>';
  8168. $return .= '</li>';
  8169. }
  8170. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  8171. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  8172. $return .= '<a class="moved" href="#">';
  8173. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8174. $return .= '</a> ';
  8175. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  8176. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8177. ).'&amp;action=add_item&amp;type='.TOOL_QUIZ.'&amp;file='.$row_quiz['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8178. Security :: remove_XSS(Text::cut($row_quiz['title'], 80)).
  8179. '</a>';
  8180. $return .= '</li>';
  8181. }
  8182. $return .= '</ul>';
  8183. return $return;
  8184. }
  8185. /**
  8186. * Creates a list with all the links in it
  8187. * @return string
  8188. */
  8189. public function get_links()
  8190. {
  8191. $course_id = api_get_course_int_id();
  8192. $tbl_link = Database :: get_course_table(TABLE_LINK);
  8193. $session_id = api_get_session_id();
  8194. $condition_session = api_get_session_condition($session_id);
  8195. $sql_link = "SELECT id, title FROM $tbl_link WHERE c_id = ".$course_id." $condition_session ORDER BY title ASC";
  8196. $res_link = Database::query($sql_link);
  8197. $return = '<ul class="lp_resource">';
  8198. $return .= '<li class="lp_resource_element">';
  8199. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  8200. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'link/link.php?'.api_get_cidreq(
  8201. ).'&action=addlink&amp;lp_id='.$this->lp_id.'" title="'.get_lang('LinkAdd').'">'.get_lang('LinkAdd').'</a>';
  8202. $return .= '</li>';
  8203. $course_info = api_get_course_info();
  8204. while ($row_link = Database :: fetch_array($res_link)) {
  8205. $item_visibility = api_get_item_visibility($course_info, TOOL_LINK, $row_link['id'], $session_id);
  8206. if ($item_visibility != 2) {
  8207. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  8208. $return .= '<a class="moved" href="#">';
  8209. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8210. $return .= '</a> ';
  8211. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  8212. $return .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  8213. $_GET['cidReq']
  8214. ).'&amp;action=add_item&amp;type='.TOOL_LINK.'&amp;file='.$row_link['id'].'&amp;lp_id='.$this->lp_id.'">'.
  8215. $row_link['title'].
  8216. '</a>';
  8217. $return .= '</li>';
  8218. }
  8219. }
  8220. $return .= '</ul>';
  8221. return $return;
  8222. }
  8223. /**
  8224. * Creates a list with all the student publications in it
  8225. * @return unknown
  8226. */
  8227. public function get_student_publications()
  8228. {
  8229. $return = '<div class="lp_resource" >';
  8230. $return .= '<div class="lp_resource_element">';
  8231. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  8232. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  8233. ).'&amp;action=add_item&amp;type='.TOOL_STUDENTPUBLICATION.'&amp;lp_id='.$this->lp_id.'">'.get_lang(
  8234. 'AddAssignmentPage'
  8235. ).'</a>';
  8236. $return .= '</div>';
  8237. $return .= '</div>';
  8238. return $return;
  8239. }
  8240. /**
  8241. * Creates a list with all the forums in it
  8242. * @return string
  8243. */
  8244. public function get_forums()
  8245. {
  8246. require_once '../forum/forumfunction.inc.php';
  8247. require_once '../forum/forumconfig.inc.php';
  8248. $a_forums = get_forums();
  8249. $return = '<ul class="lp_resource">';
  8250. //First add link
  8251. $return .= '<li class="lp_resource_element">';
  8252. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  8253. $return .= '<a href="'.api_get_path(REL_CODE_PATH).'forum/index.php?'.api_get_cidreq(
  8254. ).'&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id='.$this->lp_id.'" title="'.get_lang(
  8255. 'CreateANewForum'
  8256. ).'">'.get_lang('CreateANewForum').'</a>';
  8257. $return .= '</li>';
  8258. $return .= '<script>
  8259. function toggle_forum(forum_id){
  8260. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  8261. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  8262. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'remove.gif";
  8263. } else {
  8264. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  8265. document.getElementById("forum_"+forum_id+"_opener").src = "'.api_get_path(WEB_IMG_PATH).'add.gif";
  8266. }
  8267. }
  8268. </script>';
  8269. foreach ($a_forums as $forum) {
  8270. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  8271. if (!empty($forum['forum_id'])) {
  8272. $return .= '<a class="moved" href="#">';
  8273. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8274. $return .= ' </a>';
  8275. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  8276. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum('.$forum['forum_id'].')" style="vertical-align:middle">
  8277. <img src="'.api_get_path(WEB_IMG_PATH).'add.gif" id="forum_'.$forum['forum_id'].'_opener" align="absbottom" />
  8278. </a>
  8279. <a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  8280. $_GET['cidReq']
  8281. ).'&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(
  8282. $forum['forum_title']
  8283. ).'</a>';
  8284. }
  8285. $return .= '</li>';
  8286. $return .= '<div style="display:none" id="forum_'.$forum['forum_id'].'_content">';
  8287. $a_threads = get_threads($forum['forum_id']);
  8288. if (is_array($a_threads)) {
  8289. foreach ($a_threads as $thread) {
  8290. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  8291. $return .= '&nbsp;<a class="moved" href="#">';
  8292. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  8293. $return .= ' </a>';
  8294. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  8295. $return .= '<a href="'.api_get_self().'?cidReq='.Security :: remove_XSS(
  8296. $_GET['cidReq']
  8297. ).'&amp;action=add_item&amp;type='.TOOL_THREAD.'&amp;thread_id='.$thread['thread_id'].'&amp;lp_id='.$this->lp_id.'">'.Security :: remove_XSS(
  8298. $thread['thread_title']
  8299. ).'</a>';
  8300. $return .= '</li>';
  8301. }
  8302. }
  8303. $return .= '</div>';
  8304. }
  8305. $return .= '</ul>';
  8306. return $return;
  8307. }
  8308. /**
  8309. * // TODO: The output encoding should be equal to the system encoding.
  8310. *
  8311. * Exports the learning path as a SCORM package. This is the main function that
  8312. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  8313. * whole thing and returns the zip.
  8314. *
  8315. * This method needs to be called in PHP5, as it will fail with non-adequate
  8316. * XML package (like the ones for PHP4), and it is *not* a static method, so
  8317. * you need to call it on a learnpath object.
  8318. * @TODO The method might be redefined later on in the scorm class itself to avoid
  8319. * creating a SCORM structure if there is one already. However, if the initial SCORM
  8320. * path has been modified, it should use the generic method here below.
  8321. * @TODO link this function with the export_lp() function in the same class
  8322. * @param string Optional name of zip file. If none, title of learnpath is
  8323. * domesticated and trailed with ".zip"
  8324. * @return string Returns the zip package string, or null if error
  8325. */
  8326. public function scorm_export()
  8327. {
  8328. global $_course;
  8329. $course_id = api_get_course_int_id();
  8330. $links_to_create = null;
  8331. // Remove memory and time limits as much as possible as this might be a long process...
  8332. if (function_exists('ini_set')) {
  8333. api_set_memory_limit('128M');
  8334. ini_set('max_execution_time', 600);
  8335. }
  8336. // Create the zip handler (this will remain available throughout the method).
  8337. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  8338. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8339. $temp_dir_short = uniqid();
  8340. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  8341. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  8342. $zip_folder = new PclZip($temp_zip_file);
  8343. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  8344. $root_path = $main_path = api_get_path(SYS_PATH);
  8345. $files_cleanup = array();
  8346. // Place to temporarily stash the zipfiles.
  8347. // create the temp dir if it doesn't exist
  8348. // or do a cleanup befor creating the zipfile.
  8349. if (!is_dir($temp_zip_dir)) {
  8350. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  8351. } else {
  8352. // Cleanup: Check the temp dir for old files and delete them.
  8353. $handle = opendir($temp_zip_dir);
  8354. while (false !== ($file = readdir($handle))) {
  8355. if ($file != '.' && $file != '..') {
  8356. unlink("$temp_zip_dir/$file");
  8357. }
  8358. }
  8359. closedir($handle);
  8360. }
  8361. $zip_files = $zip_files_abs = $zip_files_dist = array();
  8362. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file(
  8363. $current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml'
  8364. )
  8365. ) {
  8366. // Remove the possible . at the end of the path.
  8367. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  8368. $dest_path_to_scorm_folder = str_replace('//', '/', $temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  8369. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  8370. $zip_files_dist = copyr(
  8371. $current_course_path.'/scorm/'.$this->path,
  8372. $dest_path_to_scorm_folder,
  8373. array('imsmanifest'),
  8374. $zip_files
  8375. );
  8376. }
  8377. // Build a dummy imsmanifest structure. Do not add to the zip yet (we still need it).
  8378. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  8379. // Aggregation Model official document, secion "2.3 Content Packaging".
  8380. $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.
  8381. $root = $xmldoc->createElement('manifest');
  8382. $root->setAttribute('identifier', 'SingleCourseManifest');
  8383. $root->setAttribute('version', '1.1');
  8384. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  8385. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  8386. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  8387. $root->setAttribute(
  8388. 'xsi:schemaLocation',
  8389. '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'
  8390. );
  8391. // Build mandatory sub-root container elements.
  8392. $metadata = $xmldoc->createElement('metadata');
  8393. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  8394. $metadata->appendChild($md_schema);
  8395. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  8396. $metadata->appendChild($md_schemaversion);
  8397. $root->appendChild($metadata);
  8398. $organizations = $xmldoc->createElement('organizations');
  8399. $resources = $xmldoc->createElement('resources');
  8400. // Build the only organization we will use in building our learnpaths.
  8401. $organizations->setAttribute('default', 'chamilo_scorm_export');
  8402. $organization = $xmldoc->createElement('organization');
  8403. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  8404. // To set the title of the SCORM entity (=organization), we take the name given
  8405. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  8406. // learning path charset) as it is the encoding that defines how it is stored
  8407. // in the database. Then we convert it to HTML entities again as the "&" character
  8408. // alone is not authorized in XML (must be &amp;).
  8409. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  8410. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  8411. $organization->appendChild($org_title);
  8412. $folder_name = 'document';
  8413. // Removes the learning_path/scorm_folder path when exporting see #4841
  8414. $path_to_remove = null;
  8415. $result = $this->generate_lp_folder($_course);
  8416. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  8417. $path_to_remove = 'document'.$result['dir'];
  8418. $path_to_replace = $folder_name.'/';
  8419. }
  8420. //Fixes chamilo scorm exports
  8421. if ($this->ref == 'chamilo_scorm_export') {
  8422. $path_to_remove = 'scorm/'.$this->path.'/document/';
  8423. }
  8424. // For each element, add it to the imsmanifest structure, then add it to the zip.
  8425. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  8426. $link_updates = array();
  8427. foreach ($this->items as $index => $item) {
  8428. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  8429. // Get included documents from this item.
  8430. if ($item->type == 'sco') {
  8431. $inc_docs = $item->get_resources_from_source(
  8432. null,
  8433. api_get_path(SYS_COURSE_PATH).api_get_course_path(
  8434. ).'/'.'scorm/'.$this->path.'/'.$item->get_path()
  8435. );
  8436. } else {
  8437. $inc_docs = $item->get_resources_from_source();
  8438. }
  8439. // Give a child element <item> to the <organization> element.
  8440. $my_item_id = $item->get_id();
  8441. $my_item = $xmldoc->createElement('item');
  8442. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  8443. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  8444. $my_item->setAttribute('isvisible', 'true');
  8445. // Give a child element <title> to the <item> element.
  8446. $my_title = $xmldoc->createElement(
  8447. 'title',
  8448. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8449. );
  8450. $my_item->appendChild($my_title);
  8451. // Give a child element <adlcp:prerequisites> to the <item> element.
  8452. $my_prereqs = $xmldoc->createElement(
  8453. 'adlcp:prerequisites',
  8454. $this->get_scorm_prereq_string($my_item_id)
  8455. );
  8456. $my_prereqs->setAttribute('type', 'aicc_script');
  8457. $my_item->appendChild($my_prereqs);
  8458. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8459. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  8460. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8461. //$xmldoc->createElement('adlcp:timelimitaction','');
  8462. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8463. //$xmldoc->createElement('adlcp:datafromlms','');
  8464. // Give a child element <adlcp:masteryscore> to the <item> element.
  8465. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8466. $my_item->appendChild($my_masteryscore);
  8467. // Attach this item to the organization element or hits parent if there is one.
  8468. if (!empty($item->parent) && $item->parent != 0) {
  8469. $children = $organization->childNodes;
  8470. $possible_parent = & $this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  8471. if (is_object($possible_parent)) {
  8472. $possible_parent->appendChild($my_item);
  8473. } else {
  8474. if ($this->debug > 0) {
  8475. error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found');
  8476. }
  8477. }
  8478. } else {
  8479. if ($this->debug > 0) {
  8480. error_log('No parent');
  8481. }
  8482. $organization->appendChild($my_item);
  8483. }
  8484. // Get the path of the file(s) from the course directory root.
  8485. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8486. if (!empty($path_to_remove)) {
  8487. //From docs
  8488. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8489. //From quiz
  8490. if ($this->ref == 'chamilo_scorm_export') {
  8491. $path_to_remove = 'scorm/'.$this->path.'/';
  8492. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8493. }
  8494. } else {
  8495. $my_xml_file_path = $my_file_path;
  8496. }
  8497. $my_sub_dir = dirname($my_file_path);
  8498. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8499. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8500. $my_xml_sub_dir = $my_sub_dir;
  8501. // Give a <resource> child to the <resources> element
  8502. $my_resource = $xmldoc->createElement('resource');
  8503. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8504. $my_resource->setAttribute('type', 'webcontent');
  8505. $my_resource->setAttribute('href', $my_xml_file_path);
  8506. // adlcp:scormtype can be either 'sco' or 'asset'.
  8507. if ($item->type == 'sco') {
  8508. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8509. } else {
  8510. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8511. }
  8512. // xml:base is the base directory to find the files declared in this resource.
  8513. $my_resource->setAttribute('xml:base', '');
  8514. // Give a <file> child to the <resource> element.
  8515. $my_file = $xmldoc->createElement('file');
  8516. $my_file->setAttribute('href', $my_xml_file_path);
  8517. $my_resource->appendChild($my_file);
  8518. // Dependency to other files - not yet supported.
  8519. $i = 1;
  8520. foreach ($inc_docs as $doc_info) {
  8521. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8522. continue;
  8523. }
  8524. $my_dep = $xmldoc->createElement('resource');
  8525. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8526. $my_dep->setAttribute('identifier', $res_id);
  8527. $my_dep->setAttribute('type', 'webcontent');
  8528. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8529. $my_dep_file = $xmldoc->createElement('file');
  8530. // Check type of URL.
  8531. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8532. if ($doc_info[1] == 'remote') {
  8533. // Remote file. Save url as is.
  8534. $my_dep_file->setAttribute('href', $doc_info[0]);
  8535. $my_dep->setAttribute('xml:base', '');
  8536. } elseif ($doc_info[1] == 'local') {
  8537. switch ($doc_info[2]) {
  8538. case 'url': // Local URL - save path as url for now, don't zip file.
  8539. $abs_path = api_get_path(SYS_PATH).str_replace(
  8540. api_get_path(WEB_PATH),
  8541. '',
  8542. $doc_info[0]
  8543. );
  8544. $current_dir = dirname($abs_path);
  8545. $current_dir = str_replace('\\', '/', $current_dir);
  8546. $file_path = realpath($abs_path);
  8547. $file_path = str_replace('\\', '/', $file_path);
  8548. $my_dep_file->setAttribute('href', $file_path);
  8549. $my_dep->setAttribute('xml:base', '');
  8550. if (strstr($file_path, $main_path) !== false) {
  8551. // The calculated real path is really inside Chamilo's root path.
  8552. // Reduce file path to what's under the DocumentRoot.
  8553. $file_path = substr($file_path, strlen($root_path) - 1);
  8554. //echo $file_path;echo '<br /><br />';
  8555. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8556. $zip_files_abs[] = $file_path;
  8557. $link_updates[$my_file_path][] = array(
  8558. 'orig' => $doc_info[0],
  8559. 'dest' => $file_path
  8560. );
  8561. $my_dep_file->setAttribute('href', $file_path);
  8562. $my_dep->setAttribute('xml:base', '');
  8563. } elseif (empty($file_path)) {
  8564. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8565. if (strpos($document_root, -1) == '/') {
  8566. $document_root = substr(0, -1, $document_root);
  8567. }*/
  8568. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8569. $file_path = str_replace('//', '/', $file_path);
  8570. if (file_exists($file_path)) {
  8571. $file_path = substr(
  8572. $file_path,
  8573. strlen($current_dir)
  8574. ); // We get the relative path.
  8575. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8576. $link_updates[$my_file_path][] = array(
  8577. 'orig' => $doc_info[0],
  8578. 'dest' => $file_path
  8579. );
  8580. $my_dep_file->setAttribute('href', $file_path);
  8581. $my_dep->setAttribute('xml:base', '');
  8582. }
  8583. }
  8584. break;
  8585. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8586. $my_dep_file->setAttribute('href', $doc_info[0]);
  8587. $my_dep->setAttribute('xml:base', '');
  8588. //$current_dir = str_replace('\\', '/', dirname($current_course_path.'/'.$item->get_file_path())).'/';
  8589. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8590. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8591. $abs_img_path_without_subdir = $doc_info[0];
  8592. $relp = api_get_path(REL_PATH); // The url-append config param.
  8593. $pos = strpos($abs_img_path_without_subdir, $relp);
  8594. if ($pos === 0) {
  8595. $abs_img_path_without_subdir = '/'.substr(
  8596. $abs_img_path_without_subdir,
  8597. strlen($relp)
  8598. );
  8599. }
  8600. //$file_path = realpath(api_get_path(SYS_PATH).$doc_info[0]);
  8601. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8602. $file_path = str_replace('\\', '/', $file_path);
  8603. $file_path = str_replace('//', '/', $file_path);
  8604. //error_log(__LINE__.'Abs path: '.$file_path, 0);
  8605. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8606. $cur_path = substr(
  8607. $current_course_path,
  8608. -1
  8609. ) == '/' ? $current_course_path : $current_course_path.'/';
  8610. // Check if the current document is in that path.
  8611. if (strstr($file_path, $cur_path) !== false) {
  8612. // The document is in that path, now get the relative path
  8613. // to the containing document.
  8614. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8615. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8616. $relative_path = '';
  8617. if (strstr($file_path, $cur_path) !== false) {
  8618. $relative_path = substr($file_path, strlen($orig_file_path));
  8619. $file_path = substr($file_path, strlen($cur_path));
  8620. } else {
  8621. // This case is still a problem as it's difficult to calculate a relative path easily
  8622. // might still generate wrong links.
  8623. //$file_path = substr($file_path,strlen($cur_path));
  8624. // Calculate the directory path to the current file (without trailing slash).
  8625. $my_relative_path = dirname($file_path);
  8626. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8627. $my_relative_file = basename($file_path);
  8628. // Calculate the directory path to the containing file (without trailing slash).
  8629. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8630. $dotdot = '';
  8631. $subdir = '';
  8632. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen(
  8633. $my_orig_file_path
  8634. ) > 1) && (strlen($my_relative_path) > 1)) {
  8635. $my_relative_path2 = dirname($my_relative_path);
  8636. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8637. $my_orig_file_path = dirname($my_orig_file_path);
  8638. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8639. $subdir = substr(
  8640. $my_relative_path,
  8641. strlen($my_relative_path2) + 1
  8642. ).'/'.$subdir;
  8643. $dotdot += '../';
  8644. $my_relative_path = $my_relative_path2;
  8645. }
  8646. $relative_path = $dotdot.$subdir.$my_relative_file;
  8647. }
  8648. // Put the current document in the zip (this array is the array
  8649. // that will manage documents already in the course folder - relative).
  8650. $zip_files[] = $file_path;
  8651. // Update the links to the current document in the containing document (make them relative).
  8652. $link_updates[$my_file_path][] = array(
  8653. 'orig' => $doc_info[0],
  8654. 'dest' => $relative_path
  8655. );
  8656. $my_dep_file->setAttribute('href', $file_path);
  8657. $my_dep->setAttribute('xml:base', '');
  8658. } elseif (strstr($file_path, $main_path) !== false) {
  8659. // The calculated real path is really inside Chamilo's root path.
  8660. // Reduce file path to what's under the DocumentRoot.
  8661. $file_path = substr($file_path, strlen($root_path));
  8662. //echo $file_path;echo '<br /><br />';
  8663. //error_log('Reduced path: '.$file_path, 0);
  8664. $zip_files_abs[] = $file_path;
  8665. $link_updates[$my_file_path][] = array(
  8666. 'orig' => $doc_info[0],
  8667. 'dest' => $file_path
  8668. );
  8669. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8670. $my_dep->setAttribute('xml:base', '');
  8671. } elseif (empty($file_path)) {
  8672. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8673. if(strpos($document_root,-1) == '/') {
  8674. $document_root = substr(0, -1, $document_root);
  8675. }*/
  8676. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8677. $file_path = str_replace('//', '/', $file_path);
  8678. if (file_exists($file_path)) {
  8679. $file_path = substr(
  8680. $file_path,
  8681. strlen($current_dir)
  8682. ); // We get the relative path.
  8683. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8684. $link_updates[$my_file_path][] = array(
  8685. 'orig' => $doc_info[0],
  8686. 'dest' => $file_path
  8687. );
  8688. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8689. $my_dep->setAttribute('xml:base', '');
  8690. }
  8691. }
  8692. break;
  8693. 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
  8694. if (substr($doc_info[0], 0, 2) == '..') {
  8695. // Relative path going up.
  8696. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8697. $current_dir = str_replace('\\', '/', $current_dir);
  8698. $file_path = realpath($current_dir.$doc_info[0]);
  8699. $file_path = str_replace('\\', '/', $file_path);
  8700. //error_log($file_path.' <-> '.$main_path,0);
  8701. if (strstr($file_path, $main_path) !== false) {
  8702. // The calculated real path is really inside Chamilo's root path.
  8703. // Reduce file path to what's under the DocumentRoot.
  8704. $file_path = substr($file_path, strlen($root_path));
  8705. //error_log('Reduced path: '.$file_path, 0);
  8706. $zip_files_abs[] = $file_path;
  8707. $link_updates[$my_file_path][] = array(
  8708. 'orig' => $doc_info[0],
  8709. 'dest' => $file_path
  8710. );
  8711. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8712. $my_dep->setAttribute('xml:base', '');
  8713. }
  8714. } else {
  8715. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8716. $my_dep_file->setAttribute('href', $doc_info[0]);
  8717. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8718. }
  8719. break;
  8720. default:
  8721. $my_dep_file->setAttribute('href', $doc_info[0]);
  8722. $my_dep->setAttribute('xml:base', '');
  8723. break;
  8724. }
  8725. }
  8726. $my_dep->appendChild($my_dep_file);
  8727. $resources->appendChild($my_dep);
  8728. $dependency = $xmldoc->createElement('dependency');
  8729. $dependency->setAttribute('identifierref', $res_id);
  8730. $my_resource->appendChild($dependency);
  8731. $i++;
  8732. }
  8733. //$my_dependency = $xmldoc->createElement('dependency');
  8734. //$my_dependency->setAttribute('identifierref', '');
  8735. $resources->appendChild($my_resource);
  8736. $zip_files[] = $my_file_path;
  8737. //error_log('File '.$my_file_path. ' added to $zip_files', 0);
  8738. } else {
  8739. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8740. switch ($item->type) {
  8741. case TOOL_LINK:
  8742. $my_item = $xmldoc->createElement('item');
  8743. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8744. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8745. $my_item->setAttribute('isvisible', 'true');
  8746. // Give a child element <title> to the <item> element.
  8747. $my_title = $xmldoc->createElement(
  8748. 'title',
  8749. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8750. );
  8751. $my_item->appendChild($my_title);
  8752. // Give a child element <adlcp:prerequisites> to the <item> element.
  8753. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8754. $my_prereqs->setAttribute('type', 'aicc_script');
  8755. $my_item->appendChild($my_prereqs);
  8756. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8757. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8758. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8759. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8760. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8761. //$xmldoc->createElement('adlcp:datafromlms', '');
  8762. // Give a child element <adlcp:masteryscore> to the <item> element.
  8763. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8764. $my_item->appendChild($my_masteryscore);
  8765. // Attach this item to the organization element or its parent if there is one.
  8766. if (!empty($item->parent) && $item->parent != 0) {
  8767. $children = $organization->childNodes;
  8768. for ($i = 0; $i < $children->length; $i++) {
  8769. $item_temp = $children->item($i);
  8770. if ($item_temp->nodeName == 'item') {
  8771. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8772. $item_temp->appendChild($my_item);
  8773. }
  8774. }
  8775. }
  8776. } else {
  8777. $organization->appendChild($my_item);
  8778. }
  8779. $my_file_path = 'link_'.$item->get_id().'.html';
  8780. $sql = 'SELECT url, title FROM '.Database :: get_course_table(
  8781. TABLE_LINK
  8782. ).' WHERE c_id = '.$course_id.' AND id='.$item->path;
  8783. $rs = Database::query($sql);
  8784. if ($link = Database :: fetch_array($rs)) {
  8785. $url = $link['url'];
  8786. $title = stripslashes($link['title']);
  8787. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8788. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8789. $my_xml_file_path = $my_file_path;
  8790. $my_sub_dir = dirname($my_file_path);
  8791. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8792. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8793. $my_xml_sub_dir = $my_sub_dir;
  8794. // Give a <resource> child to the <resources> element.
  8795. $my_resource = $xmldoc->createElement('resource');
  8796. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8797. $my_resource->setAttribute('type', 'webcontent');
  8798. $my_resource->setAttribute('href', $my_xml_file_path);
  8799. // adlcp:scormtype can be either 'sco' or 'asset'.
  8800. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8801. // xml:base is the base directory to find the files declared in this resource.
  8802. $my_resource->setAttribute('xml:base', '');
  8803. // give a <file> child to the <resource> element.
  8804. $my_file = $xmldoc->createElement('file');
  8805. $my_file->setAttribute('href', $my_xml_file_path);
  8806. $my_resource->appendChild($my_file);
  8807. $resources->appendChild($my_resource);
  8808. }
  8809. break;
  8810. case TOOL_QUIZ:
  8811. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8812. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8813. $exe = new Exercise();
  8814. $exe->read($exe_id);
  8815. $my_item = $xmldoc->createElement('item');
  8816. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8817. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8818. $my_item->setAttribute('isvisible', 'true');
  8819. // Give a child element <title> to the <item> element.
  8820. $my_title = $xmldoc->createElement(
  8821. 'title',
  8822. htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8')
  8823. );
  8824. $my_item->appendChild($my_title);
  8825. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8826. //$my_item->appendChild($my_max_score);
  8827. // Give a child element <adlcp:prerequisites> to the <item> element.
  8828. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8829. $my_prereqs->setAttribute('type', 'aicc_script');
  8830. $my_item->appendChild($my_prereqs);
  8831. // Give a child element <adlcp:masteryscore> to the <item> element.
  8832. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8833. $my_item->appendChild($my_masteryscore);
  8834. // Attach this item to the organization element or hits parent if there is one.
  8835. if (!empty($item->parent) && $item->parent != 0) {
  8836. $children = $organization->childNodes;
  8837. for ($i = 0; $i < $children->length; $i++) {
  8838. $item_temp = $children->item($i);
  8839. if ($item_temp->nodeName == 'item') {
  8840. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8841. $item_temp->appendChild($my_item);
  8842. }
  8843. }
  8844. }
  8845. } else {
  8846. $organization->appendChild($my_item);
  8847. }
  8848. // Include export scripts.
  8849. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8850. // Get the path of the file(s) from the course directory root
  8851. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8852. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8853. // Write the contents of the exported exercise into a (big) html file
  8854. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8855. $contents = export_exercise($exe_id, true);
  8856. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8857. $res = file_put_contents($tmp_file_path, $contents);
  8858. if ($res === false) {
  8859. error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0);
  8860. }
  8861. $files_cleanup[] = $tmp_file_path;
  8862. //error_log($tmp_path); die();
  8863. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8864. $my_xml_file_path = $my_file_path;
  8865. $my_sub_dir = dirname($my_file_path);
  8866. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8867. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8868. $my_xml_sub_dir = $my_sub_dir;
  8869. // Give a <resource> child to the <resources> element.
  8870. $my_resource = $xmldoc->createElement('resource');
  8871. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8872. $my_resource->setAttribute('type', 'webcontent');
  8873. $my_resource->setAttribute('href', $my_xml_file_path);
  8874. // adlcp:scormtype can be either 'sco' or 'asset'.
  8875. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8876. // xml:base is the base directory to find the files declared in this resource.
  8877. $my_resource->setAttribute('xml:base', '');
  8878. // Give a <file> child to the <resource> element.
  8879. $my_file = $xmldoc->createElement('file');
  8880. $my_file->setAttribute('href', $my_xml_file_path);
  8881. $my_resource->appendChild($my_file);
  8882. // Get included docs.
  8883. $inc_docs = $item->get_resources_from_source(null, $tmp_file_path);
  8884. // Dependency to other files - not yet supported.
  8885. $i = 1;
  8886. foreach ($inc_docs as $doc_info) {
  8887. if (count($doc_info) < 1 || empty($doc_info[0])) {
  8888. continue;
  8889. }
  8890. $my_dep = $xmldoc->createElement('resource');
  8891. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8892. $my_dep->setAttribute('identifier', $res_id);
  8893. $my_dep->setAttribute('type', 'webcontent');
  8894. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8895. $my_dep_file = $xmldoc->createElement('file');
  8896. // Check type of URL.
  8897. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8898. if ($doc_info[1] == 'remote') {
  8899. // Remote file. Save url as is.
  8900. $my_dep_file->setAttribute('href', $doc_info[0]);
  8901. $my_dep->setAttribute('xml:base', '');
  8902. } elseif ($doc_info[1] == 'local') {
  8903. switch ($doc_info[2]) {
  8904. case 'url': // Local URL - save path as url for now, don't zip file.
  8905. // Save file but as local file (retrieve from URL).
  8906. $abs_path = api_get_path(SYS_PATH).str_replace(
  8907. api_get_path(WEB_PATH),
  8908. '',
  8909. $doc_info[0]
  8910. );
  8911. $current_dir = dirname($abs_path);
  8912. $current_dir = str_replace('\\', '/', $current_dir);
  8913. $file_path = realpath($abs_path);
  8914. $file_path = str_replace('\\', '/', $file_path);
  8915. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8916. $my_dep->setAttribute('xml:base', '');
  8917. if (strstr($file_path, $main_path) !== false) {
  8918. // The calculated real path is really inside the chamilo root path.
  8919. // Reduce file path to what's under the DocumentRoot.
  8920. $file_path = substr($file_path, strlen($root_path));
  8921. //echo $file_path;echo '<br /><br />';
  8922. //error_log('Reduced path: '.$file_path, 0);
  8923. $zip_files_abs[] = $file_path;
  8924. $link_updates[$my_file_path][] = array(
  8925. 'orig' => $doc_info[0],
  8926. 'dest' => 'document/'.$file_path
  8927. );
  8928. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8929. $my_dep->setAttribute('xml:base', '');
  8930. } elseif (empty($file_path)) {
  8931. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8932. if (strpos($document_root,-1) == '/') {
  8933. $document_root = substr(0, -1, $document_root);
  8934. }*/
  8935. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8936. $file_path = str_replace('//', '/', $file_path);
  8937. if (file_exists($file_path)) {
  8938. $file_path = substr(
  8939. $file_path,
  8940. strlen($current_dir)
  8941. ); // We get the relative path.
  8942. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8943. $link_updates[$my_file_path][] = array(
  8944. 'orig' => $doc_info[0],
  8945. 'dest' => 'document/'.$file_path
  8946. );
  8947. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8948. $my_dep->setAttribute('xml:base', '');
  8949. }
  8950. }
  8951. break;
  8952. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8953. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8954. $current_dir = str_replace('\\', '/', $current_dir);
  8955. $file_path = realpath($doc_info[0]);
  8956. $file_path = str_replace('\\', '/', $file_path);
  8957. $my_dep_file->setAttribute('href', $file_path);
  8958. $my_dep->setAttribute('xml:base', '');
  8959. if (strstr($file_path, $main_path) !== false) {
  8960. // The calculated real path is really inside the chamilo root path.
  8961. // Reduce file path to what's under the DocumentRoot.
  8962. $file_path = substr($file_path, strlen($root_path));
  8963. //echo $file_path;echo '<br /><br />';
  8964. //error_log('Reduced path: '.$file_path, 0);
  8965. $zip_files_abs[] = $file_path;
  8966. $link_updates[$my_file_path][] = array(
  8967. 'orig' => $doc_info[0],
  8968. 'dest' => $file_path
  8969. );
  8970. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8971. $my_dep->setAttribute('xml:base', '');
  8972. } elseif (empty($file_path)) {
  8973. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8974. if (strpos($document_root,-1) == '/') {
  8975. $document_root = substr(0, -1, $document_root);
  8976. }*/
  8977. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8978. $file_path = str_replace('//', '/', $file_path);
  8979. if (file_exists($file_path)) {
  8980. $file_path = substr(
  8981. $file_path,
  8982. strlen($current_dir)
  8983. ); // We get the relative path.
  8984. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8985. $link_updates[$my_file_path][] = array(
  8986. 'orig' => $doc_info[0],
  8987. 'dest' => $file_path
  8988. );
  8989. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8990. $my_dep->setAttribute('xml:base', '');
  8991. }
  8992. }
  8993. break;
  8994. 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
  8995. if (substr($doc_info[0], 0, 2) == '..') {
  8996. // Relative path going up.
  8997. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8998. $current_dir = str_replace('\\', '/', $current_dir);
  8999. $file_path = realpath($current_dir.$doc_info[0]);
  9000. $file_path = str_replace('\\', '/', $file_path);
  9001. //error_log($file_path.' <-> '.$main_path, 0);
  9002. if (strstr($file_path, $main_path) !== false) {
  9003. // The calculated real path is really inside Chamilo's root path.
  9004. // Reduce file path to what's under the DocumentRoot.
  9005. $file_path = substr($file_path, strlen($root_path));
  9006. $file_path_dest = $file_path;
  9007. // File path is courses/CHAMILO/document/....
  9008. $info_file_path = explode('/', $file_path);
  9009. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  9010. $file_path_dest = 'document/'.$file_path;
  9011. }
  9012. //error_log('Reduced path: '.$file_path, 0);
  9013. $zip_files_abs[] = $file_path;
  9014. $link_updates[$my_file_path][] = array(
  9015. 'orig' => $doc_info[0],
  9016. 'dest' => $file_path_dest
  9017. );
  9018. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  9019. $my_dep->setAttribute('xml:base', '');
  9020. }
  9021. } else {
  9022. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  9023. $my_dep_file->setAttribute('href', $doc_info[0]);
  9024. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  9025. }
  9026. break;
  9027. default:
  9028. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  9029. $my_dep->setAttribute('xml:base', '');
  9030. break;
  9031. }
  9032. }
  9033. $my_dep->appendChild($my_dep_file);
  9034. $resources->appendChild($my_dep);
  9035. $dependency = $xmldoc->createElement('dependency');
  9036. $dependency->setAttribute('identifierref', $res_id);
  9037. $my_resource->appendChild($dependency);
  9038. $i++;
  9039. }
  9040. $resources->appendChild($my_resource);
  9041. $zip_files[] = $my_file_path;
  9042. break;
  9043. default:
  9044. // Get the path of the file(s) from the course directory root
  9045. $my_file_path = 'non_exportable.html';
  9046. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  9047. $my_xml_file_path = $my_file_path;
  9048. $my_sub_dir = dirname($my_file_path);
  9049. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  9050. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  9051. $my_xml_sub_dir = $my_sub_dir;
  9052. // Give a <resource> child to the <resources> element.
  9053. $my_resource = $xmldoc->createElement('resource');
  9054. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  9055. $my_resource->setAttribute('type', 'webcontent');
  9056. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  9057. // adlcp:scormtype can be either 'sco' or 'asset'.
  9058. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  9059. // xml:base is the base directory to find the files declared in this resource.
  9060. $my_resource->setAttribute('xml:base', '');
  9061. // Give a <file> child to the <resource> element.
  9062. $my_file = $xmldoc->createElement('file');
  9063. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  9064. $my_resource->appendChild($my_file);
  9065. $resources->appendChild($my_resource);
  9066. break;
  9067. }
  9068. }
  9069. }
  9070. $organizations->appendChild($organization);
  9071. $root->appendChild($organizations);
  9072. $root->appendChild($resources);
  9073. $xmldoc->appendChild($root);
  9074. // TODO: Add a readme file here, with a short description and a link to the Reload player
  9075. // then add the file to the zip, then destroy the file (this is done automatically).
  9076. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  9077. //error_log(print_r($zip_files,true), 0);
  9078. foreach ($zip_files as $file_path) {
  9079. if (empty($file_path)) {
  9080. continue;
  9081. }
  9082. //error_log(__LINE__.'getting document from '.$sys_course_path.$_course['path'].'/'.$file_path.' removing '.$sys_course_path.$_course['path'].'/',0);
  9083. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  9084. $this->create_path($dest_file);
  9085. //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);
  9086. //echo $main_path.$file_path.'<br />';
  9087. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  9088. // Check if the file needs a link update.
  9089. if (in_array($file_path, array_keys($link_updates))) {
  9090. $string = file_get_contents($dest_file);
  9091. unlink($dest_file);
  9092. foreach ($link_updates[$file_path] as $old_new) {
  9093. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9094. // This is an ugly hack that allows .flv files to be found by the flv player that
  9095. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9096. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9097. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9098. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9099. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9100. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  9101. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  9102. }
  9103. //Fix to avoid problems with default_course_document
  9104. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  9105. $new_dest = str_replace('document/', $mult.'document/', $old_new['dest']);
  9106. } else {
  9107. //$new_dest = str_replace('main/default_course_document', $mult.'document/main/default_course_document', $old_new['dest']);
  9108. $new_dest = $old_new['dest'];
  9109. }
  9110. //$string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9111. $string = str_replace($old_new['orig'], $new_dest, $string);
  9112. //Add files inside the HTMLs
  9113. $new_path = str_replace('/courses/', '', $old_new['orig']);
  9114. //var_dump($sys_course_path.$new_path); var_dump($archive_path.$temp_dir_short.'/'.$old_new['dest']); echo '---';
  9115. if (file_exists($sys_course_path.$new_path)) {
  9116. copy($sys_course_path.$new_path, $archive_path.$temp_dir_short.'/'.$old_new['dest']);
  9117. }
  9118. }
  9119. file_put_contents($dest_file, $string);
  9120. }
  9121. }
  9122. foreach ($zip_files_abs as $file_path) {
  9123. if (empty($file_path)) {
  9124. continue;
  9125. }
  9126. //error_log(__LINE__.'checking existence of '.$main_path.$file_path.'', 0);
  9127. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  9128. continue;
  9129. }
  9130. //error_log(__LINE__.'getting document from '.$main_path.$file_path.' removing '.api_get_path(SYS_COURSE_PATH).$_course['path'].'/', 0);
  9131. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  9132. $this->create_path($dest_file);
  9133. //error_log('Created path '.api_get_path(SYS_ARCHIVE_PATH).$temp_dir_short.'/document/'.$file_path, 0);
  9134. //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);
  9135. //echo $main_path.$file_path.' - '.$dest_file.'<br />';
  9136. copy($main_path.$file_path, $dest_file);
  9137. // Check if the file needs a link update.
  9138. if (in_array($file_path, array_keys($link_updates))) {
  9139. $string = file_get_contents($dest_file);
  9140. unlink($dest_file);
  9141. foreach ($link_updates[$file_path] as $old_new) {
  9142. //error_log('Replacing '.$old_new['orig'].' by '.$old_new['dest'].' in '.$file_path, 0);
  9143. // This is an ugly hack that allows .flv files to be found by the flv player that
  9144. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  9145. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  9146. // ../../.. to return from inc/lib/flv_player to the document/main path.
  9147. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  9148. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  9149. }
  9150. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  9151. }
  9152. file_put_contents($dest_file, $string);
  9153. }
  9154. }
  9155. if (is_array($links_to_create)) {
  9156. foreach ($links_to_create as $file => $link) {
  9157. $file_content = '<!DOCTYPE html>
  9158. <head>
  9159. <meta charset="'.api_get_language_isocode().'" />
  9160. <title>'.$link['title'].'</title>
  9161. </head>
  9162. <body dir="'.api_get_text_direction().'">
  9163. <div style="text-align:center">
  9164. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  9165. </body>
  9166. </html>';
  9167. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  9168. }
  9169. }
  9170. // Add non exportable message explanation.
  9171. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  9172. $file_content = '<!DOCTYPE html>
  9173. <head>
  9174. <meta charset="'.api_get_language_isocode().'" />
  9175. <title>'.$lang_not_exportable.'</title>
  9176. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  9177. </head>
  9178. <body dir="'.api_get_text_direction().'">';
  9179. $file_content .=
  9180. <<<EOD
  9181. <style>
  9182. .error-message {
  9183. font-family: arial, verdana, helvetica, sans-serif;
  9184. border-width: 1px;
  9185. border-style: solid;
  9186. left: 50%;
  9187. margin: 10px auto;
  9188. min-height: 30px;
  9189. padding: 5px;
  9190. right: 50%;
  9191. width: 500px;
  9192. background-color: #FFD1D1;
  9193. border-color: #FF0000;
  9194. color: #000;
  9195. }
  9196. </style>
  9197. <body>
  9198. <div class="error-message">
  9199. $lang_not_exportable
  9200. </div>
  9201. </body>
  9202. </html>
  9203. EOD;
  9204. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  9205. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  9206. }
  9207. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  9208. // Add the extra files that go along with a SCORM package.
  9209. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  9210. $extra_files = scandir($main_code_path);
  9211. foreach ($extra_files as $extra_file) {
  9212. if (strpos($extra_file, '.') === 0) {
  9213. continue;
  9214. } else {
  9215. $dest_file = $archive_path.$temp_dir_short.'/'.$extra_file;
  9216. $this->create_path($dest_file);
  9217. copy($main_code_path.$extra_file, $dest_file);
  9218. }
  9219. }
  9220. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  9221. $manifest = @$xmldoc->saveXML();
  9222. $manifest = Text::api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  9223. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  9224. $zip_folder->add(
  9225. $archive_path.'/'.$temp_dir_short,
  9226. PCLZIP_OPT_REMOVE_PATH,
  9227. $archive_path.'/'.$temp_dir_short.'/'
  9228. );
  9229. // Clean possible temporary files.
  9230. foreach ($files_cleanup as $file) {
  9231. $res = unlink($file);
  9232. if ($res === false) {
  9233. error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0);
  9234. }
  9235. }
  9236. // Send file to client
  9237. $name = replace_dangerous_char($this->get_name()).'.zip';
  9238. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  9239. }
  9240. public function scorm_export_to_pdf($lp_id)
  9241. {
  9242. $lp_id = intval($lp_id);
  9243. $files_to_export = array();
  9244. $course_data = api_get_course_info($this->cc);
  9245. if (!empty($course_data)) {
  9246. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  9247. $list = self::get_flat_ordered_items_list($lp_id);
  9248. if (!empty($list)) {
  9249. foreach ($list as $item_id) {
  9250. $item = $this->items[$item_id];
  9251. switch ($item->type) {
  9252. case 'document':
  9253. //Getting documents from a LP with chamilo documents
  9254. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  9255. $file_path = api_get_path(
  9256. SYS_COURSE_PATH
  9257. ).$course_data['path'].'/document'.$file_data['path'];
  9258. if (file_exists($file_path)) {
  9259. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9260. }
  9261. break;
  9262. case 'asset': //commes from a scorm package generated by chamilo
  9263. case 'sco':
  9264. $file_path = $scorm_path.'/'.$item->path;
  9265. if (file_exists($file_path)) {
  9266. $files_to_export[] = array('title' => $item->get_title(), 'path' => $file_path);
  9267. }
  9268. break;
  9269. case 'dokeos_chapter':
  9270. case 'dir':
  9271. case 'chapter':
  9272. $files_to_export[] = array('title' => $item->get_title(), 'path' => null);
  9273. break;
  9274. }
  9275. }
  9276. }
  9277. $pdf = new PDF();
  9278. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  9279. return $result;
  9280. }
  9281. return false;
  9282. }
  9283. /**
  9284. * Temp function to be moved in main_api or the best place around for this. Creates a file path
  9285. * if it doesn't exist
  9286. */
  9287. public function create_path($path)
  9288. {
  9289. $path_bits = explode('/', dirname($path));
  9290. // IS_WINDOWS_OS has been defined in main_api.lib.php
  9291. $path_built = IS_WINDOWS_OS ? '' : '/';
  9292. foreach ($path_bits as $bit) {
  9293. if (!empty ($bit)) {
  9294. $new_path = $path_built.$bit;
  9295. if (is_dir($new_path)) {
  9296. $path_built = $new_path.'/';
  9297. } else {
  9298. mkdir($new_path, api_get_permissions_for_new_directories());
  9299. $path_built = $new_path.'/';
  9300. }
  9301. }
  9302. }
  9303. }
  9304. /**
  9305. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  9306. * @return boolean The results of the unlink function, or false if there was no image to start with
  9307. */
  9308. public function delete_lp_image()
  9309. {
  9310. $img = $this->get_preview_image();
  9311. if ($img != '') {
  9312. $del_file = $this->get_preview_image_path(null, 'sys');
  9313. if (isset($del_file) && file_exists($del_file)) {
  9314. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  9315. if (file_exists($del_file_2)) {
  9316. unlink($del_file_2);
  9317. }
  9318. $this->set_preview_image('');
  9319. return @unlink($del_file);
  9320. }
  9321. }
  9322. return false;
  9323. }
  9324. /**
  9325. * Uploads an author image to the upload/learning_path/images directory
  9326. * @param array The image array, coming from the $_FILES superglobal
  9327. * @return boolean True on success, false on error
  9328. */
  9329. public function upload_image($image_array)
  9330. {
  9331. $image_moved = false;
  9332. if (!empty ($image_array['name'])) {
  9333. $upload_ok = FileManager::process_uploaded_file($image_array);
  9334. $has_attachment = true;
  9335. } else {
  9336. $image_moved = true;
  9337. }
  9338. if ($upload_ok) {
  9339. if ($has_attachment) {
  9340. $courseDir = api_get_course_path().'/upload/learning_path/images';
  9341. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  9342. $updir = $sys_course_path.$courseDir;
  9343. // Try to add an extension to the file if it hasn't one.
  9344. $new_file_name = FileManager::add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  9345. if (!FileManager::filter_extension($new_file_name)) {
  9346. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  9347. $image_moved = false;
  9348. } else {
  9349. $file_extension = explode('.', $image_array['name']);
  9350. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  9351. $filename = uniqid('');
  9352. $new_file_name = $filename.'.'.$file_extension;
  9353. $new_path = $updir.'/'.$new_file_name;
  9354. // Resize the image.
  9355. $temp = new Image($image_array['tmp_name']);
  9356. $picture_infos = $temp->get_image_info();
  9357. if ($picture_infos['width'] > 104) {
  9358. $thumbwidth = 104;
  9359. } else {
  9360. $thumbwidth = $picture_infos['width'];
  9361. }
  9362. if ($picture_infos['height'] > 96) {
  9363. $new_height = 96;
  9364. } else {
  9365. $new_height = $picture_infos['height'];
  9366. }
  9367. $temp->resize($thumbwidth, $new_height, 0);
  9368. $result = $temp->send_image($new_path);
  9369. // Storing the image filename.
  9370. if ($result) {
  9371. $image_moved = true;
  9372. $this->set_preview_image($new_file_name);
  9373. //Resize to 32
  9374. $temp->resize(64, 64, 0);
  9375. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  9376. return true;
  9377. }
  9378. }
  9379. }
  9380. }
  9381. return false;
  9382. }
  9383. public function set_autolunch($lp_id, $status)
  9384. {
  9385. $course_id = api_get_course_int_id();
  9386. $lp_id = intval($lp_id);
  9387. $status = intval($status);
  9388. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  9389. //Setting everything to autolunch = 0
  9390. $attributes['autolunch'] = 0;
  9391. $where = array('session_id = ? AND c_id = ? ' => array(api_get_session_id(), $course_id));
  9392. Database::update($lp_table, $attributes, $where);
  9393. if ($status == 1) {
  9394. //Setting my lp_id to autolunch = 1
  9395. $attributes['autolunch'] = 1;
  9396. $where = array('id = ? AND session_id = ? AND c_id = ?' => array($lp_id, api_get_session_id(), $course_id));
  9397. Database::update($lp_table, $attributes, $where);
  9398. }
  9399. }
  9400. /**
  9401. * Gets previous_item_id for the next element of the lp_item table
  9402. * @author Isaac flores paz
  9403. * @return integer Previous item ID
  9404. */
  9405. function select_previous_item_id()
  9406. {
  9407. $course_id = api_get_course_int_id();
  9408. if ($this->debug > 0) {
  9409. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  9410. }
  9411. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  9412. // Get the max order of the items
  9413. $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."'";
  9414. $rs_max_order = Database::query($sql_max_order);
  9415. $row_max_order = Database::fetch_object($rs_max_order);
  9416. $max_order = $row_max_order->display_order;
  9417. // Get the previous item ID
  9418. $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."' ";
  9419. $rs_max = Database::query($sql_max, __FILE__, __LINE__);
  9420. $row_max = Database::fetch_object($rs_max);
  9421. // Return the previous item ID
  9422. return $row_max->previous;
  9423. }
  9424. function copy()
  9425. {
  9426. $main_path = api_get_path(SYS_CODE_PATH);
  9427. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  9428. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  9429. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  9430. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  9431. //Course builder
  9432. $cb = new CourseBuilder();
  9433. //Setting tools that will be copied
  9434. $cb->set_tools_to_build(array('learnpaths'));
  9435. //Setting elements that will be copied
  9436. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  9437. $course = $cb->build();
  9438. //Course restorer
  9439. $course_restorer = new CourseRestorer($course);
  9440. $course_restorer->set_add_text_in_items(true);
  9441. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  9442. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  9443. }
  9444. function verify_document_size($s)
  9445. {
  9446. $post_max = ini_get('post_max_size');
  9447. $upl_max = ini_get('upload_max_filesize');
  9448. $documents_total_space = DocumentManager::documents_total_space();
  9449. $course_max_space = DocumentManager::get_course_quota();
  9450. $total_size = filesize($s) + $documents_total_space;
  9451. if (filesize($s) > $post_max || filesize($s) > $upl_max || $total_size > $course_max_space) {
  9452. return true;
  9453. } else {
  9454. return false;
  9455. }
  9456. }
  9457. function clear_prerequisites()
  9458. {
  9459. $course_id = $this->get_course_int_id();
  9460. if ($this->debug > 0) {
  9461. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  9462. }
  9463. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9464. $lp_id = $this->get_id();
  9465. //Cleaning prerequisites
  9466. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  9467. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  9468. Database::query($sql);
  9469. //Cleaning mastery score for exercises
  9470. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  9471. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  9472. Database::query($sql);
  9473. }
  9474. function set_previous_step_as_prerequisite_for_all_items()
  9475. {
  9476. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  9477. $course_id = $this->get_course_int_id();
  9478. $lp_id = $this->get_id();
  9479. if (!empty($this->items)) {
  9480. $old_id = null;
  9481. $old_max = 0;
  9482. $old_type = null;
  9483. foreach ($this->items as $item) {
  9484. if (!empty($old_id)) {
  9485. $current_item_id = $item->get_id();
  9486. if ($old_type == 'quiz') {
  9487. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$old_max' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$old_id'";
  9488. Database::query($sql);
  9489. }
  9490. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$old_id' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  9491. Database::query($sql);
  9492. }
  9493. $old_id = $item->get_id();
  9494. $old_max = $item->get_max();
  9495. $old_type = $item->get_type();
  9496. }
  9497. }
  9498. }
  9499. static function create_category($params)
  9500. {
  9501. global $app;
  9502. $em = $app['orm.em'];
  9503. $item = new Entity\EntityCLpCategory();
  9504. $item->setName($params['name']);
  9505. $item->setCId($params['c_id']);
  9506. $em->persist($item);
  9507. $em->flush();
  9508. }
  9509. static function update_category($params)
  9510. {
  9511. global $app;
  9512. $em = $app['orm.em'];
  9513. $item = $em->find('Entity\EntityCLpCategory', $params['id']);
  9514. if ($item) {
  9515. $item->setName($params['name']);
  9516. $item->setCId($params['c_id']);
  9517. $em->persist($item);
  9518. $em->flush();
  9519. }
  9520. }
  9521. static function move_up_category($id)
  9522. {
  9523. global $app;
  9524. $em = $app['orm.em'];
  9525. $item = $em->find('Entity\EntityCLpCategory', $id);
  9526. if ($item) {
  9527. $position = $item->getPosition() - 1;
  9528. $item->setPosition($position);
  9529. $em->persist($item);
  9530. $em->flush();
  9531. }
  9532. }
  9533. static function move_down_category($id)
  9534. {
  9535. global $app;
  9536. $em = $app['orm.em'];
  9537. $item = $em->find('Entity\EntityCLpCategory', $id);
  9538. if ($item) {
  9539. $position = $item->getPosition() + 1;
  9540. $item->setPosition($position);
  9541. $em->persist($item);
  9542. $em->flush();
  9543. }
  9544. }
  9545. static function get_count_categories($course_id)
  9546. {
  9547. global $app;
  9548. if (empty($course_id)) {
  9549. return 0;
  9550. }
  9551. $em = $app['orm.em'];
  9552. $query = $em->createQuery('SELECT COUNT(u.id) FROM Entity\EntityCLpCategory u WHERE u.cId = :id');
  9553. $query->setParameter('id', $course_id);
  9554. return $query->getSingleScalarResult();
  9555. }
  9556. static function get_categories($course_id)
  9557. {
  9558. global $app;
  9559. $em = $app['orm.em'];
  9560. //Default behaviour
  9561. /*$items = $em->getRepository('Entity\EntityCLpCategory')->findBy(
  9562. array('cId' => $course_id),
  9563. array('name' => 'ASC')
  9564. );*/
  9565. //Using doctrine extensions
  9566. $items = $em->getRepository('Entity\EntityCLpCategory')->getBySortableGroupsQuery(
  9567. array('cId' => $course_id)
  9568. )->getResult();
  9569. return $items;
  9570. }
  9571. static function get_category($id)
  9572. {
  9573. global $app;
  9574. $em = $app['orm.em'];
  9575. $item = $em->find('Entity\EntityCLpCategory', $id);
  9576. return $item;
  9577. }
  9578. static function get_category_by_course($course_id)
  9579. {
  9580. global $app;
  9581. $items = $app['orm.em']->getRepository('Entity\EntityCLpCategory')->findBy(array('cId' => $course_id));
  9582. return $items;
  9583. }
  9584. static function delete_category($id)
  9585. {
  9586. global $app;
  9587. $em = $app['orm.em'];
  9588. $item = $em->find('Entity\EntityCLpCategory', $id);
  9589. if ($item) {
  9590. $courseId = $item->getCId();
  9591. $query = $em->createQuery('SELECT u FROM Entity\EntityCLp u WHERE u.cId = :id AND u.categoryId = :catId');
  9592. $query->setParameter('id', $courseId);
  9593. $query->setParameter('catId', $item->getId());
  9594. $lps = $query->getResult();
  9595. //Setting category = 0
  9596. if ($lps) {
  9597. foreach ($lps as $lpItem) {
  9598. $lpItem->setCategoryId(0);
  9599. }
  9600. }
  9601. //Removing category
  9602. $em->remove($item);
  9603. $em->flush();
  9604. }
  9605. }
  9606. static function get_category_from_course_into_select($course_id, $addSelectOption = false)
  9607. {
  9608. $items = self::get_category_by_course($course_id);
  9609. $cats = array();
  9610. if ($addSelectOption) {
  9611. $cats = array(get_lang('SelectACategory'));
  9612. }
  9613. if (!empty($items)) {
  9614. foreach ($items as $cat) {
  9615. $cats[$cat->getId()] = $cat->getName();
  9616. }
  9617. }
  9618. return $cats;
  9619. }
  9620. }
  9621. if (!function_exists('trim_value')) {
  9622. function trim_value(& $value)
  9623. {
  9624. $value = trim($value);
  9625. }
  9626. }