learnpath.class.php 431 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use \ChamiloSession as Session;
  4. /**
  5. * This class defines the parent attributes and methods for Chamilo learnpaths
  6. * and SCORM learnpaths. It is used by the scorm class.
  7. *
  8. * @package chamilo.learnpath
  9. * @author Yannick Warnier <ywarnier@beeznest.org>
  10. * @author Julio Montoya <gugli100@gmail.com> Several improvements and fixes
  11. */
  12. class learnpath
  13. {
  14. public $attempt = 0; // The number for the current ID view.
  15. public $cc; // Course (code) this learnpath is located in. @todo change name for something more comprensible ...
  16. public $current; // Id of the current item the user is viewing.
  17. public $current_score; // The score of the current item.
  18. public $current_time_start; // The time the user loaded this resource (this does not mean he can see it yet).
  19. public $current_time_stop; // The time the user closed this resource.
  20. public $default_status = 'not attempted';
  21. public $encoding = 'UTF-8';
  22. public $error = '';
  23. public $extra_information = ''; // This string can be used by proprietary SCORM contents to store data about the current learnpath.
  24. public $force_commit = false; // For SCORM only - if set to true, will send a scorm LMSCommit() request on each LMSSetValue().
  25. public $index; // The index of the active learnpath_item in $ordered_items array.
  26. public $items = array();
  27. public $last; // item_id of last item viewed in the learning path.
  28. public $last_item_seen = 0; // In case we have already come in this learnpath, reuse the last item seen if authorized.
  29. public $license; // Which license this course has been given - not used yet on 20060522.
  30. public $lp_id; // DB ID for this learnpath.
  31. public $lp_view_id; // DB ID for lp_view
  32. public $log_file; // File where to log learnpath API msg.
  33. public $maker; // Which maker has conceived the content (ENI, Articulate, ...).
  34. public $message = '';
  35. public $mode = 'embedded'; // Holds the video display mode (fullscreen or embedded).
  36. public $name; // Learnpath name (they generally have one).
  37. public $ordered_items = array(); // List of the learnpath items in the order they are to be read.
  38. public $path = ''; // Path inside the scorm directory (if scorm).
  39. public $theme; // The current theme of the learning path.
  40. public $preview_image; // The current image of the learning path.
  41. // Tells if all the items of the learnpath can be tried again. Defaults to "no" (=1).
  42. public $prevent_reinit = 1;
  43. // Describes the mode of progress bar display.
  44. public $seriousgame_mode = 0;
  45. public $progress_bar_mode = '%';
  46. // Percentage progress as saved in the db.
  47. public $progress_db = '0';
  48. public $proximity; // Wether the content is distant or local or unknown.
  49. public $refs_list = array (); //list of items by ref => db_id. Used only for prerequisites match.
  50. // !!!This array (refs_list) is built differently depending on the nature of the LP.
  51. // If SCORM, uses ref, if Chamilo, uses id to keep a unique value.
  52. public $type; //type of learnpath. Could be 'dokeos', 'scorm', 'scorm2004', 'aicc', ...
  53. // TODO: Check if this type variable is useful here (instead of just in the controller script).
  54. public $user_id; //ID of the user that is viewing/using the course
  55. public $update_queue = array();
  56. public $scorm_debug = 0;
  57. public $arrMenu = array(); // Array for the menu items.
  58. public $debug = 0; // Logging level.
  59. public $lp_session_id = 0;
  60. public $lp_view_session_id = 0; // The specific view might be bound to a session.
  61. public $prerequisite = 0;
  62. public $use_max_score = 1; // 1 or 0
  63. public $created_on = '';
  64. public $modified_on = '';
  65. public $publicated_on = '';
  66. public $expired_on = '';
  67. public $ref = null;
  68. public $course_int_id;
  69. public $course_info = array();
  70. /**
  71. * Constructor.
  72. * Needs a database handler, a course code and a learnpath id from the database.
  73. * Also builds the list of items into $this->items.
  74. * @param string $course Course code
  75. * @param integer $lp_id
  76. * @param integer $user_id
  77. * @return boolean True on success, false on error
  78. */
  79. public function __construct($course, $lp_id, $user_id)
  80. {
  81. $this->encoding = api_get_system_encoding();
  82. if ($this->debug > 0) {
  83. error_log('New LP - In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')', 0);
  84. }
  85. if (empty($course)) {
  86. $this->error = 'Course code is empty';
  87. return false;
  88. } else {
  89. $course_info = api_get_course_info($course);
  90. if (!empty($course_info)) {
  91. $this->cc = $course_info['code'];
  92. $this->course_info = $course_info;
  93. $course_id = $course_info['real_id'];
  94. } else {
  95. $this->error = 'Course code does not exist in database.';
  96. return false;
  97. }
  98. }
  99. $this->set_course_int_id($course_id);
  100. // Check learnpath ID.
  101. if (empty($lp_id)) {
  102. $this->error = 'Learnpath ID is empty';
  103. return false;
  104. } else {
  105. // TODO: Make it flexible to use any course_code (still using env course code here).
  106. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  107. $lp_id = intval($lp_id);
  108. $sql = "SELECT * FROM $lp_table
  109. WHERE id = '$lp_id' AND c_id = $course_id";
  110. if ($this->debug > 2) {
  111. error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
  112. }
  113. $res = Database::query($sql);
  114. if (Database::num_rows($res) > 0) {
  115. $this->lp_id = $lp_id;
  116. $row = Database::fetch_array($res);
  117. $this->type = $row['lp_type'];
  118. $this->name = stripslashes($row['name']);
  119. $this->proximity = $row['content_local'];
  120. $this->theme = $row['theme'];
  121. $this->maker = $row['content_maker'];
  122. $this->prevent_reinit = $row['prevent_reinit'];
  123. $this->seriousgame_mode = $row['seriousgame_mode'];
  124. $this->license = $row['content_license'];
  125. $this->scorm_debug = $row['debug'];
  126. $this->js_lib = $row['js_lib'];
  127. $this->path = $row['path'];
  128. $this->preview_image = $row['preview_image'];
  129. $this->author = $row['author'];
  130. $this->hide_toc_frame = $row['hide_toc_frame'];
  131. $this->lp_session_id = $row['session_id'];
  132. $this->use_max_score = $row['use_max_score'];
  133. $this->created_on = $row['created_on'];
  134. $this->modified_on = $row['modified_on'];
  135. $this->ref = $row['ref'];
  136. if ($row['publicated_on'] != '0000-00-00 00:00:00') {
  137. $this->publicated_on = $row['publicated_on'];
  138. }
  139. if ($row['expired_on'] != '0000-00-00 00:00:00') {
  140. $this->expired_on = $row['expired_on'];
  141. }
  142. if ($this->type == 2) {
  143. if ($row['force_commit'] == 1) {
  144. $this->force_commit = true;
  145. }
  146. }
  147. $this->mode = $row['default_view_mod'];
  148. } else {
  149. $this->error = 'Learnpath ID does not exist in database ('.$sql.')';
  150. return false;
  151. }
  152. }
  153. // Check user ID.
  154. if (empty($user_id)) {
  155. $this->error = 'User ID is empty';
  156. return false;
  157. } else {
  158. $user_info = api_get_user_info($user_id);
  159. if (!empty($user_info)) {
  160. $this->user_id = $user_info['user_id'];
  161. } else {
  162. $this->error = 'User ID does not exist in database ('.$sql.')';
  163. return false;
  164. }
  165. }
  166. // End of variables checking.
  167. $session_id = api_get_session_id();
  168. // Get the session condition for learning paths of the base + session.
  169. $session = api_get_session_condition($session_id);
  170. // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
  171. $lp_table = Database::get_course_table(TABLE_LP_VIEW);
  172. // Selecting by view_count descending allows to get the highest view_count first.
  173. $sql = "SELECT * FROM $lp_table
  174. WHERE c_id = $course_id AND lp_id = '$lp_id' AND user_id = '$user_id' $session
  175. ORDER BY view_count DESC";
  176. $res = Database::query($sql);
  177. if ($this->debug > 2) {
  178. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - querying lp_view: ' . $sql, 0);
  179. }
  180. if (Database :: num_rows($res) > 0) {
  181. if ($this->debug > 2) {
  182. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Found previous view', 0);
  183. }
  184. $row = Database :: fetch_array($res);
  185. $this->attempt = $row['view_count'];
  186. $this->lp_view_id = $row['id'];
  187. $this->last_item_seen = $row['last_item'];
  188. $this->progress_db = $row['progress'];
  189. $this->lp_view_session_id = $row['session_id'];
  190. } else {
  191. if ($this->debug > 2) {
  192. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
  193. }
  194. $this->attempt = 1;
  195. $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
  196. VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
  197. Database::query($sql);
  198. $this->lp_view_id = Database::insert_id();
  199. if ($this->debug > 2) {
  200. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0);
  201. }
  202. }
  203. // Initialise items.
  204. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  205. $sql = "SELECT * FROM $lp_item_table
  206. WHERE c_id = $course_id AND lp_id = '".$this->lp_id."'
  207. ORDER BY parent_item_id, display_order";
  208. $res = Database::query($sql);
  209. if ($this->debug > 2) {
  210. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - query lp items: ' . $sql, 0);
  211. error_log('-- Start while--', 0);
  212. }
  213. $lp_item_id_list = array();
  214. while ($row = Database::fetch_array($res)) {
  215. $lp_item_id_list[] = $row['id'];
  216. switch ($this->type) {
  217. case 3: //aicc
  218. $oItem = new aiccItem('db', $row['id'], $course_id);
  219. if (is_object($oItem)) {
  220. $my_item_id = $oItem->get_id();
  221. $oItem->set_lp_view($this->lp_view_id, $course_id);
  222. $oItem->set_prevent_reinit($this->prevent_reinit);
  223. // Don't use reference here as the next loop will make the pointed object change.
  224. $this->items[$my_item_id] = $oItem;
  225. $this->refs_list[$oItem->ref] = $my_item_id;
  226. if ($this->debug > 2) {
  227. error_log(
  228. 'New LP - learnpath::__construct() - ' .
  229. 'aicc object with id ' . $my_item_id .
  230. ' set in items[]', 0);
  231. }
  232. }
  233. break;
  234. case 2:
  235. require_once 'scorm.class.php';
  236. require_once 'scormItem.class.php';
  237. $oItem = new scormItem('db', $row['id'], $course_id);
  238. if (is_object($oItem)) {
  239. $my_item_id = $oItem->get_id();
  240. $oItem->set_lp_view($this->lp_view_id, $course_id);
  241. $oItem->set_prevent_reinit($this->prevent_reinit);
  242. // Don't use reference here as the next loop will make the pointed object change.
  243. $this->items[$my_item_id] = $oItem;
  244. $this->refs_list[$oItem->ref] = $my_item_id;
  245. if ($this->debug > 2) {
  246. error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
  247. }
  248. }
  249. break;
  250. case 1:
  251. default:
  252. if ($this->debug > 2) {
  253. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - calling learnpathItem', 0);
  254. }
  255. $oItem = new learnpathItem($row['id'], $user_id, $course_id, $row);
  256. if ($this->debug > 2) {
  257. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - end calling learnpathItem', 0);
  258. }
  259. if (is_object($oItem)) {
  260. $my_item_id = $oItem->get_id();
  261. //$oItem->set_lp_view($this->lp_view_id); // Moved down to when we are sure the item_view exists.
  262. $oItem->set_prevent_reinit($this->prevent_reinit);
  263. // Don't use reference here as the next loop will make the pointed object change.
  264. $this->items[$my_item_id] = $oItem;
  265. $this->refs_list[$my_item_id] = $my_item_id;
  266. if ($this->debug > 2) {
  267. error_log(
  268. 'New LP - learnpath::__construct() ' . __LINE__ .
  269. ' - object with id ' . $my_item_id . ' set in items[]',
  270. 0);
  271. }
  272. }
  273. break;
  274. }
  275. // Setting the object level with variable $this->items[$i][parent]
  276. foreach ($this->items as $itemLPObject) {
  277. $level = learnpath::get_level_for_item($this->items, $itemLPObject->db_id);
  278. $itemLPObject->level = $level;
  279. }
  280. // Setting the view in the item object.
  281. if (is_object($this->items[$row['id']])) {
  282. $this->items[$row['id']]->set_lp_view($this->lp_view_id, $course_id);
  283. if ($this->items[$row['id']]->get_type() == TOOL_HOTPOTATOES) {
  284. $this->items[$row['id']]->current_start_time = 0;
  285. $this->items[$row['id']]->current_stop_time = 0;
  286. }
  287. }
  288. }
  289. if ($this->debug > 2) {
  290. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' ----- end while ----', 0);
  291. }
  292. if (!empty($lp_item_id_list)) {
  293. $lp_item_id_list_to_string = implode("','", $lp_item_id_list);
  294. // Get last viewing vars.
  295. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  296. // This query should only return one or zero result.
  297. $sql = "SELECT lp_item_id, status
  298. FROM $lp_item_view_table
  299. WHERE
  300. c_id = $course_id AND
  301. lp_view_id = ".$this->lp_view_id." AND
  302. lp_item_id IN ('".$lp_item_id_list_to_string."')
  303. ORDER BY view_count DESC ";
  304. if ($this->debug > 2) {
  305. error_log('New LP - learnpath::__construct() - Selecting item_views: ' . $sql, 0);
  306. }
  307. $status_list = array();
  308. $res = Database::query($sql);
  309. while ($row = Database :: fetch_array($res) ) {
  310. $status_list[$row['lp_item_id']] = $row['status'];
  311. }
  312. foreach ($lp_item_id_list as $item_id) {
  313. if (isset($status_list[$item_id])) {
  314. $status = $status_list[$item_id];
  315. if (is_object($this->items[$item_id])) {
  316. $this->items[$item_id]->set_status($status);
  317. if (empty($status)) {
  318. $this->items[$item_id]->set_status($this->default_status);
  319. }
  320. }
  321. } else {
  322. if (is_object($this->items[$item_id])) {
  323. $this->items[$item_id]->set_status($this->default_status);
  324. }
  325. // Add that row to the lp_item_view table so that we have something to show in the stats page.
  326. $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
  327. VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ", 1, 'not attempted')";
  328. if ($this->debug > 2) {
  329. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql, 0);
  330. }
  331. $this->items[$item_id]->set_lp_view($this->lp_view_id, $course_id);
  332. Database::query($sql);
  333. }
  334. }
  335. }
  336. $this->ordered_items = self::get_flat_ordered_items_list(
  337. $this->get_id(),
  338. 0,
  339. $course_id
  340. );
  341. $this->max_ordered_items = 0;
  342. foreach ($this->ordered_items as $index => $dummy) {
  343. if ($index > $this->max_ordered_items && !empty($dummy)) {
  344. $this->max_ordered_items = $index;
  345. }
  346. }
  347. // TODO: Define the current item better.
  348. $this->first();
  349. if ($this->debug > 2) {
  350. error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - End of learnpath constructor for learnpath ' . $this->get_id(), 0);
  351. }
  352. }
  353. /**
  354. * @return string
  355. */
  356. public function getCourseCode()
  357. {
  358. return $this->cc;
  359. }
  360. /**
  361. * @return int
  362. */
  363. public function get_course_int_id()
  364. {
  365. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  366. }
  367. /**
  368. * @param $course_id
  369. * @return int
  370. */
  371. public function set_course_int_id($course_id)
  372. {
  373. return $this->course_int_id = intval($course_id);
  374. }
  375. /**
  376. * Get the depth level of LP item
  377. * @param $in_tab_items
  378. * @param $in_current_item_id
  379. * @return int
  380. */
  381. private static function get_level_for_item($in_tab_items, $in_current_item_id)
  382. {
  383. $parent_item_id = $in_tab_items[$in_current_item_id]->parent;
  384. if ($parent_item_id == 0) {
  385. return 0;
  386. } else {
  387. return learnpath::get_level_for_item($in_tab_items, $parent_item_id) + 1;
  388. }
  389. }
  390. /**
  391. * Function rewritten based on old_add_item() from Yannick Warnier.
  392. * Due the fact that users can decide where the item should come, I had to overlook this function and
  393. * I found it better to rewrite it. Old function is still available.
  394. * Added also the possibility to add a description.
  395. *
  396. * @param int $parent
  397. * @param int $previous
  398. * @param string $type
  399. * @param int resource ID (ref)
  400. * @param string $title
  401. * @param string $description
  402. * @param int $prerequisites
  403. * @param int $max_time_allowed
  404. * @return int
  405. */
  406. public function add_item(
  407. $parent,
  408. $previous,
  409. $type = 'dokeos_chapter',
  410. $id,
  411. $title,
  412. $description,
  413. $prerequisites = 0,
  414. $max_time_allowed = 0
  415. ) {
  416. $course_id = api_get_course_int_id();
  417. if ($this->debug > 0) {
  418. error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
  419. }
  420. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  421. $parent = intval($parent);
  422. $previous = intval($previous);
  423. $id = intval($id);
  424. $max_time_allowed = htmlentities($max_time_allowed);
  425. if (empty($max_time_allowed)) {
  426. $max_time_allowed = 0;
  427. }
  428. $sql_count = " SELECT COUNT(id) AS num
  429. FROM $tbl_lp_item
  430. WHERE
  431. c_id = $course_id AND
  432. lp_id = " . $this->get_id() . " AND
  433. parent_item_id = " . $parent;
  434. $res_count = Database::query($sql_count);
  435. $row = Database :: fetch_array($res_count);
  436. $num = $row['num'];
  437. if ($num > 0) {
  438. if ($previous == 0) {
  439. $sql = "SELECT id, next_item_id, display_order
  440. FROM " . $tbl_lp_item . "
  441. WHERE
  442. c_id = $course_id AND
  443. lp_id = " . $this->get_id() . " AND
  444. parent_item_id = " . $parent . " AND
  445. previous_item_id = 0 OR
  446. previous_item_id=" . $parent;
  447. $result = Database::query($sql);
  448. $row = Database :: fetch_array($result);
  449. $tmp_previous = 0;
  450. $next = $row['id'];
  451. $display_order = 0;
  452. } else {
  453. $previous = (int) $previous;
  454. $sql = "SELECT id, previous_item_id, next_item_id, display_order
  455. FROM $tbl_lp_item
  456. WHERE
  457. c_id = $course_id AND
  458. lp_id = " . $this->get_id() . " AND
  459. id = " . $previous;
  460. $result = Database::query($sql);
  461. $row = Database:: fetch_array($result);
  462. $tmp_previous = $row['id'];
  463. $next = $row['next_item_id'];
  464. $display_order = $row['display_order'];
  465. }
  466. } else {
  467. $tmp_previous = 0;
  468. $next = 0;
  469. $display_order = 0;
  470. }
  471. $id = intval($id);
  472. $typeCleaned = Database::escape_string($type);
  473. if ($type == 'quiz') {
  474. $sql = 'SELECT SUM(ponderation)
  475. FROM ' . Database :: get_course_table(TABLE_QUIZ_QUESTION) . ' as quiz_question
  476. INNER JOIN ' . Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION) . ' as quiz_rel_question
  477. ON quiz_question.id = quiz_rel_question.question_id
  478. WHERE quiz_rel_question.exercice_id = '.$id." AND
  479. quiz_question.c_id = $course_id AND
  480. quiz_rel_question.c_id = $course_id ";
  481. $rsQuiz = Database::query($sql);
  482. $max_score = Database :: result($rsQuiz, 0, 0);
  483. //Disabling the exercise if we add it inside a LP
  484. $exercise = new Exercise();
  485. $exercise->read($id);
  486. $exercise->disable();
  487. $exercise->save();
  488. } else {
  489. $max_score = 100;
  490. }
  491. $params = array(
  492. "c_id" => $course_id,
  493. "lp_id" => $this->get_id(),
  494. "item_type" =>$typeCleaned ,
  495. "ref" => '',
  496. "title" =>$title ,
  497. "description" => $description,
  498. "path" => $id,
  499. "max_score" => $max_score,
  500. "parent_item_id" => $parent,
  501. "previous_item_id" => $previous,
  502. "next_item_id" => $next,
  503. "display_order" => $display_order +1,
  504. "prerequisite" => $prerequisites,
  505. "max_time_allowed" => $max_time_allowed
  506. );
  507. if ($prerequisites != 0) {
  508. $params['prerequisite'] = $prerequisites;
  509. }
  510. $new_item_id = Database::insert($tbl_lp_item, $params);
  511. if ($this->debug > 2) {
  512. error_log('New LP - Inserting dokeos_chapter: ' . $new_item_id, 0);
  513. }
  514. if ($new_item_id) {
  515. // Update the item that should come after the new item.
  516. $sql_update_next = "
  517. UPDATE " . $tbl_lp_item . "
  518. SET previous_item_id = " . $new_item_id . "
  519. WHERE c_id = $course_id AND id = " . $next;
  520. Database::query($sql_update_next);
  521. // Update the item that should be before the new item.
  522. $sql_update_previous = "
  523. UPDATE " . $tbl_lp_item . "
  524. SET next_item_id = " . $new_item_id . "
  525. WHERE c_id = $course_id AND id = " . $tmp_previous;
  526. Database::query($sql_update_previous);
  527. // Update all the items after the new item.
  528. $sql_update_order = "
  529. UPDATE " . $tbl_lp_item . "
  530. SET display_order = display_order + 1
  531. WHERE
  532. c_id = $course_id AND
  533. lp_id = " . $this->get_id() . " AND
  534. id <> " . $new_item_id . " AND
  535. parent_item_id = " . $parent . " AND
  536. display_order > " . $display_order;
  537. Database::query($sql_update_order);
  538. // Update the item that should come after the new item.
  539. $sql_update_ref = "UPDATE " . $tbl_lp_item . "
  540. SET ref = " . $new_item_id . "
  541. WHERE c_id = $course_id AND id = " . $new_item_id;
  542. Database::query($sql_update_ref);
  543. // Upload audio.
  544. if (!empty($_FILES['mp3']['name'])) {
  545. // Create the audio folder if it does not exist yet.
  546. global $_course;
  547. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  548. if (!is_dir($filepath . 'audio')) {
  549. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  550. $audio_id = add_document(
  551. $_course,
  552. '/audio',
  553. 'folder',
  554. 0,
  555. 'audio'
  556. );
  557. api_item_property_update(
  558. $_course,
  559. TOOL_DOCUMENT,
  560. $audio_id,
  561. 'FolderCreated',
  562. api_get_user_id(),
  563. null,
  564. null,
  565. null,
  566. null,
  567. api_get_session_id()
  568. );
  569. api_item_property_update(
  570. $_course,
  571. TOOL_DOCUMENT,
  572. $audio_id,
  573. 'invisible',
  574. api_get_user_id(),
  575. null,
  576. null,
  577. null,
  578. null,
  579. api_get_session_id()
  580. );
  581. }
  582. // Upload the file in the documents tool.
  583. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  584. $file_path = handle_uploaded_document(
  585. $_course,
  586. $_FILES['mp3'],
  587. api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
  588. '/audio',
  589. api_get_user_id(),
  590. '',
  591. '',
  592. '',
  593. '',
  594. false
  595. );
  596. // Getting the filename only.
  597. $file_components = explode('/', $file_path);
  598. $file = $file_components[count($file_components) - 1];
  599. // Store the mp3 file in the lp_item table.
  600. $sql = "UPDATE $tbl_lp_item SET
  601. audio = '" . Database::escape_string($file) . "'
  602. WHERE id = '" . intval($new_item_id) . "'";
  603. Database::query($sql);
  604. }
  605. }
  606. return $new_item_id;
  607. }
  608. /**
  609. * Static admin function allowing addition of a learnpath to a course.
  610. * @param string Course code
  611. * @param string Learnpath name
  612. * @param string Learnpath description string, if provided
  613. * @param string Type of learnpath (default = 'guess', others = 'dokeos', 'aicc',...)
  614. * @param string Type of files origin (default = 'zip', others = 'dir','web_dir',...)
  615. * @param string Zip file containing the learnpath or directory containing the learnpath
  616. * @return integer The new learnpath ID on success, 0 on failure
  617. */
  618. public static function add_lp(
  619. $course,
  620. $name,
  621. $description = '',
  622. $learnpath = 'guess',
  623. $origin = 'zip',
  624. $zipname = '',
  625. $publicated_on = '',
  626. $expired_on = ''
  627. ) {
  628. global $charset;
  629. $course_id = api_get_course_int_id();
  630. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  631. // Check course code exists.
  632. // Check lp_name doesn't exist, otherwise append something.
  633. $i = 0;
  634. $name = Database::escape_string($name);
  635. // Session id.
  636. $session_id = api_get_session_id();
  637. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  638. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  639. $res_name = Database::query($check_name);
  640. if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
  641. //by default the publication date is the same that the creation date
  642. //The behaviour above was changed due BT#2800
  643. global $_custom;
  644. if (isset($_custom['lps_hidden_when_no_start_date']) && $_custom['lps_hidden_when_no_start_date']) {
  645. $publicated_on = '';
  646. } else {
  647. $publicated_on = api_get_utc_datetime();
  648. }
  649. } else {
  650. $publicated_on = Database::escape_string(api_get_utc_datetime($publicated_on));
  651. }
  652. if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {
  653. $expired_on = '';
  654. } else {
  655. $expired_on = Database::escape_string(api_get_utc_datetime($expired_on));
  656. }
  657. while (Database :: num_rows($res_name)) {
  658. // There is already one such name, update the current one a bit.
  659. $i++;
  660. $name = $name . ' - ' . $i;
  661. $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '$name'";
  662. //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
  663. $res_name = Database::query($check_name);
  664. }
  665. // New name does not exist yet; keep it.
  666. // Escape description.
  667. // Kevin: added htmlentities().
  668. $description = Database::escape_string(api_htmlentities($description, ENT_QUOTES, $charset));
  669. $type = 1;
  670. switch ($learnpath) {
  671. case 'guess':
  672. break;
  673. case 'dokeos':
  674. case 'chamilo':
  675. $type = 1;
  676. break;
  677. case 'aicc':
  678. break;
  679. }
  680. switch ($origin) {
  681. case 'zip':
  682. // Check zip name string. If empty, we are currently creating a new Chamilo learnpath.
  683. break;
  684. case 'manual':
  685. default:
  686. $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id";
  687. $res_max = Database::query($get_max);
  688. if (Database :: num_rows($res_max) < 1) {
  689. $dsp = 1;
  690. } else {
  691. $row = Database :: fetch_array($res_max);
  692. $dsp = $row[0] + 1;
  693. }
  694. $sql = "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) " .
  695. "VALUES ($course_id, $type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";
  696. Database::query($sql);
  697. $id = Database :: insert_id();
  698. if ($id > 0) {
  699. $course_info = api_get_course_info();
  700. // Insert into item_property.
  701. api_item_property_update($course_info, TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
  702. api_set_default_visibility($id, TOOL_LEARNPATH);
  703. return $id;
  704. }
  705. break;
  706. }
  707. }
  708. /**
  709. * Appends a message to the message attribute
  710. * @param string $string Message to append.
  711. */
  712. public function append_message($string)
  713. {
  714. if ($this->debug > 0) {
  715. error_log('New LP - In learnpath::append_message()', 0);
  716. }
  717. $this->message .= $string;
  718. }
  719. /**
  720. * Auto completes the parents of an item in case it's been completed or passed
  721. * @param integer $item Optional ID of the item from which to look for parents
  722. */
  723. public function autocomplete_parents($item)
  724. {
  725. $debug = $this->debug;
  726. if ($debug) {
  727. error_log('Learnpath::autocomplete_parents()', 0);
  728. }
  729. if (empty($item)) {
  730. $item = $this->current;
  731. }
  732. $currentItem = $this->getItem($item);
  733. if ($currentItem) {
  734. $parent_id = $currentItem->get_parent();
  735. $parent = $this->getItem($parent_id);
  736. if ($parent) {
  737. // if $item points to an object and there is a parent.
  738. if ($debug) {
  739. error_log(
  740. 'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
  741. 0
  742. );
  743. }
  744. // New experiment including failed and browsed in completed status.
  745. //$current_status = $currentItem->get_status();
  746. //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
  747. // Fixes chapter auto complete
  748. if (true) {
  749. // If the current item is completed or passes or succeeded.
  750. $updateParentStatus = true;
  751. if ($debug) {
  752. error_log('Status of current item is alright', 0);
  753. }
  754. foreach ($parent->get_children() as $childItemId) {
  755. $childItem = $this->getItem($childItemId);
  756. // If children was not set try to get the info
  757. if (empty($childItem->db_item_view_id)) {
  758. $childItem->set_lp_view($this->lp_view_id, $this->course_int_id);
  759. }
  760. // Check all his brothers (parent's children) for completion status.
  761. if ($childItemId != $item) {
  762. if ($debug) {
  763. error_log(
  764. 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
  765. 0
  766. );
  767. }
  768. // Trying completing parents of failed and browsed items as well.
  769. if ($childItem->status_is(
  770. array(
  771. 'completed',
  772. 'passed',
  773. 'succeeded',
  774. 'browsed',
  775. 'failed'
  776. )
  777. )
  778. ) {
  779. // Keep completion status to true.
  780. continue;
  781. } else {
  782. if ($debug > 2) {
  783. error_log(
  784. 'Found one incomplete child of parent #' . $parent_id . ': child #'.$childItemId . ' "' . $childItem->get_title() . '", is ' . $childItem->get_status().' db_item_view_id:#'.$childItem->db_item_view_id,
  785. 0
  786. );
  787. }
  788. $updateParentStatus = false;
  789. break;
  790. }
  791. }
  792. }
  793. if ($updateParentStatus) {
  794. // If all the children were completed:
  795. $parent->set_status('completed');
  796. $parent->save(false, $this->prerequisites_match($parent->get_id()));
  797. // Force the status to "completed"
  798. //$this->update_queue[$parent->get_id()] = $parent->get_status();
  799. $this->update_queue[$parent->get_id()] = 'completed';
  800. if ($debug) {
  801. error_log(
  802. 'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
  803. print_r($this->update_queue, 1),
  804. 0
  805. );
  806. }
  807. // Recursive call.
  808. $this->autocomplete_parents($parent->get_id());
  809. }
  810. }
  811. } else {
  812. if ($debug) {
  813. error_log("Parent #$parent_id does not exists");
  814. }
  815. }
  816. } else {
  817. if ($debug) {
  818. error_log("#$item is an item that doesn't have parents");
  819. }
  820. }
  821. }
  822. /**
  823. * Auto saves the current results into the database for the whole learnpath
  824. */
  825. public function autosave()
  826. {
  827. if ($this->debug > 0) {
  828. error_log('New LP - In learnpath::autosave()', 0);
  829. }
  830. // TODO: Add save operations for the learnpath itself.
  831. }
  832. /**
  833. * Clears the message attribute
  834. */
  835. public function clear_message()
  836. {
  837. if ($this->debug > 0) {
  838. error_log('New LP - In learnpath::clear_message()', 0);
  839. }
  840. $this->message = '';
  841. }
  842. /**
  843. * Closes the current resource
  844. *
  845. * Stops the timer
  846. * Saves into the database if required
  847. * Clears the current resource data from this object
  848. * @return boolean True on success, false on failure
  849. */
  850. public function close()
  851. {
  852. $course_id = api_get_course_int_id();
  853. if ($this->debug > 0) {
  854. error_log('New LP - In learnpath::close()', 0);
  855. }
  856. if (empty ($this->lp_id)) {
  857. $this->error = 'Trying to close this learnpath but no ID is set';
  858. return false;
  859. }
  860. $this->current_time_stop = time();
  861. if ($this->save) {
  862. $learnpath_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  863. /*
  864. $sql = "UPDATE $learnpath_view_table " .
  865. "SET " .
  866. "stop_time = ".$this->current_time_stop.", " .
  867. "score = ".$this->current_score.", ".
  868. "WHERE learnpath_id = '".$this->lp_id."'";
  869. //$res = Database::query($sql);
  870. $res = Database::query($res);
  871. if (Database::affected_rows($res) < 1) {
  872. $this->error = 'Could not update learnpath_view table while closing learnpath';
  873. return false;
  874. }
  875. */
  876. }
  877. $this->ordered_items = array();
  878. $this->index = 0;
  879. unset ($this->lp_id);
  880. //unset other stuff
  881. return true;
  882. }
  883. /**
  884. * Static admin function allowing removal of a learnpath
  885. * @param string Course code
  886. * @param integer Learnpath ID
  887. * @param string Whether to delete data or keep it (default: 'keep', others: 'remove')
  888. * @return boolean True on success, false on failure (might change that to return number of elements deleted)
  889. */
  890. public function delete($course = null, $id = null, $delete = 'keep')
  891. {
  892. $course_id = api_get_course_int_id();
  893. // TODO: Implement a way of getting this to work when the current object is not set.
  894. // In clear: implement this in the item class as well (abstract class) and use the given ID in queries.
  895. // If an ID is specifically given and the current LP is not the same, prevent delete.
  896. if (!empty ($id) && ($id != $this->lp_id)) {
  897. return false;
  898. }
  899. $lp = Database :: get_course_table(TABLE_LP_MAIN);
  900. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  901. $lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  902. $lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  903. // Delete lp item id.
  904. foreach ($this->items as $id => $dummy) {
  905. $sql = "DELETE FROM $lp_item_view WHERE c_id = $course_id AND lp_item_id = '" . $id . "'";
  906. Database::query($sql);
  907. }
  908. // Proposed by Christophe (nickname: clefevre)
  909. $sql = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  910. Database::query($sql);
  911. $sql = "DELETE FROM $lp_view WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  912. //if ($this->debug > 2) { error_log('New LP - Deleting views bound to lp '.$this->lp_id.': '.$sql_del_view, 0); }
  913. Database::query($sql);
  914. self::toggle_publish($this->lp_id, 'i');
  915. //if ($this->debug > 2) { error_log('New LP - Deleting lp '.$this->lp_id.' of type '.$this->type, 0); }
  916. if ($this->type == 2 || $this->type == 3) {
  917. // This is a scorm learning path, delete the files as well.
  918. $sql = "SELECT path FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  919. $res = Database::query($sql);
  920. if (Database :: num_rows($res) > 0) {
  921. $row = Database :: fetch_array($res);
  922. $path = $row['path'];
  923. $sql = "SELECT id FROM $lp WHERE c_id = ".$course_id." AND path = '$path' AND id != " . $this->lp_id;
  924. $res = Database::query($sql);
  925. if (Database :: num_rows($res) > 0) { // Another learning path uses this directory, so don't delete it.
  926. if ($this->debug > 2) {
  927. error_log('New LP - In learnpath::delete(), found other LP using path ' . $path . ', keeping directory', 0);
  928. }
  929. } else {
  930. // No other LP uses that directory, delete it.
  931. $course_rel_dir = api_get_course_path() . '/scorm/'; // scorm dir web path starting from /courses
  932. $course_scorm_dir = api_get_path(SYS_COURSE_PATH) . $course_rel_dir; // The absolute system path for this course.
  933. if ($delete == 'remove' && is_dir($course_scorm_dir . $path) and !empty ($course_scorm_dir)) {
  934. if ($this->debug > 2) {
  935. error_log('New LP - In learnpath::delete(), found SCORM, deleting directory: ' . $course_scorm_dir . $path, 0);
  936. }
  937. // Proposed by Christophe (clefevre).
  938. if (strcmp(substr($path, -2), "/.") == 0) {
  939. $path = substr($path, 0, -1); // Remove "." at the end.
  940. }
  941. //exec('rm -rf ' . $course_scorm_dir . $path); // See Bug #5208, this is not OS-portable way.
  942. rmdirr($course_scorm_dir . $path);
  943. }
  944. }
  945. }
  946. }
  947. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  948. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$this->lp_id;
  949. // Delete tools
  950. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link LIKE '$link%' AND image='scormbuilder.gif')";
  951. Database::query($sql);
  952. $sql = "DELETE FROM $lp WHERE c_id = ".$course_id." AND id = " . $this->lp_id;
  953. Database::query($sql);
  954. // Updates the display order of all lps.
  955. $this->update_display_order();
  956. api_item_property_update(
  957. api_get_course_info(),
  958. TOOL_LEARNPATH,
  959. $this->lp_id,
  960. 'delete',
  961. api_get_user_id()
  962. );
  963. require_once '../gradebook/lib/be.inc.php';
  964. // Delete link of gradebook tool
  965. //$tbl_grade_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  966. /*$sql = 'SELECT gl.id FROM ' . $tbl_grade_link . ' gl WHERE gl.type="4" AND gl.ref_id="' . $id . '";';
  967. $result = Database::query($sql);
  968. $row = Database :: fetch_array($result, 'ASSOC');*/
  969. // Fixing gradebook link deleted see #5229.
  970. /*
  971. if (!empty($row['id'])) {
  972. $link = LinkFactory :: load($row['id']);
  973. if ($link[0] != null) {
  974. $link[0]->delete();
  975. }
  976. }*/
  977. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  978. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 4 , $id, api_get_session_id());
  979. if ($link_info !== false) {
  980. remove_resource_from_course_gradebook($link_info['id']);
  981. }
  982. if (api_get_setting('search_enabled') == 'true') {
  983. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  984. delete_all_values_for_item($this->cc, TOOL_LEARNPATH, $this->lp_id);
  985. }
  986. }
  987. /**
  988. * Removes all the children of one item - dangerous!
  989. * @param integer $id Element ID of which children have to be removed
  990. * @return integer Total number of children removed
  991. */
  992. public function delete_children_items($id)
  993. {
  994. $course_id = api_get_course_int_id();
  995. if ($this->debug > 0) {
  996. error_log('New LP - In learnpath::delete_children_items(' . $id . ')', 0);
  997. }
  998. $num = 0;
  999. if (empty ($id) || $id != strval(intval($id))) {
  1000. return false;
  1001. }
  1002. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1003. $sql = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $id";
  1004. $res = Database::query($sql);
  1005. while ($row = Database :: fetch_array($res)) {
  1006. $num += $this->delete_children_items($row['id']);
  1007. $sql_del = "DELETE FROM $lp_item WHERE c_id = ".$course_id." AND id = " . $row['id'];
  1008. Database::query($sql_del);
  1009. $num++;
  1010. }
  1011. return $num;
  1012. }
  1013. /**
  1014. * Removes an item from the current learnpath
  1015. * @param integer $id Elem ID (0 if first)
  1016. * @param integer $remove Whether to remove the resource/data from the
  1017. * system or leave it (default: 'keep', others 'remove')
  1018. * @return integer Number of elements moved
  1019. * @todo implement resource removal
  1020. */
  1021. public function delete_item($id, $remove = 'keep')
  1022. {
  1023. $course_id = api_get_course_int_id();
  1024. if ($this->debug > 0) {
  1025. error_log('New LP - In learnpath::delete_item()', 0);
  1026. }
  1027. // TODO: Implement the resource removal.
  1028. if (empty ($id) || $id != strval(intval($id))) {
  1029. return false;
  1030. }
  1031. // First select item to get previous, next, and display order.
  1032. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1033. $sql_sel = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND id = $id";
  1034. $res_sel = Database::query($sql_sel);
  1035. if (Database :: num_rows($res_sel) < 1) {
  1036. return false;
  1037. }
  1038. $row = Database :: fetch_array($res_sel);
  1039. $previous = $row['previous_item_id'];
  1040. $next = $row['next_item_id'];
  1041. $display = $row['display_order'];
  1042. $parent = $row['parent_item_id'];
  1043. $lp = $row['lp_id'];
  1044. // Delete children items.
  1045. $num = $this->delete_children_items($id);
  1046. if ($this->debug > 2) {
  1047. error_log('New LP - learnpath::delete_item() - deleted ' . $num . ' children of element ' . $id, 0);
  1048. }
  1049. // Now delete the item.
  1050. $sql_del = "DELETE FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1051. if ($this->debug > 2) {
  1052. error_log('New LP - Deleting item: ' . $sql_del, 0);
  1053. }
  1054. Database::query($sql_del);
  1055. // Now update surrounding items.
  1056. $sql_upd = "UPDATE $lp_item SET next_item_id = $next
  1057. WHERE c_id = ".$course_id." AND id = $previous";
  1058. Database::query($sql_upd);
  1059. $sql_upd = "UPDATE $lp_item SET previous_item_id = $previous
  1060. WHERE c_id = ".$course_id." AND id = $next";
  1061. Database::query($sql_upd);
  1062. // Now update all following items with new display order.
  1063. $sql_all = "UPDATE $lp_item SET display_order = display_order-1
  1064. WHERE c_id = ".$course_id." AND lp_id = $lp AND parent_item_id = $parent AND display_order > $display";
  1065. Database::query($sql_all);
  1066. //Removing prerequisites since the item will not longer exist
  1067. $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = ".$course_id." AND prerequisite = $id";
  1068. Database::query($sql_all);
  1069. // Remove from search engine if enabled.
  1070. if (api_get_setting('search_enabled') == 'true') {
  1071. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1072. $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';
  1073. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1074. $res = Database::query($sql);
  1075. if (Database :: num_rows($res) > 0) {
  1076. $row2 = Database :: fetch_array($res);
  1077. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  1078. $di = new ChamiloIndexer();
  1079. $di->remove_document((int) $row2['search_did']);
  1080. }
  1081. $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';
  1082. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id);
  1083. Database::query($sql);
  1084. }
  1085. }
  1086. /**
  1087. * Updates an item's content in place
  1088. * @param integer Element ID
  1089. * @param integer Parent item ID
  1090. * @param integer Previous item ID
  1091. * @param string Item title
  1092. * @param string Item description
  1093. * @param string Prerequisites (optional)
  1094. * @param string Indexing terms (optional)
  1095. * @param array The array resulting of the $_FILES[mp3] element
  1096. * @return boolean True on success, false on error
  1097. */
  1098. public function edit_item(
  1099. $id,
  1100. $parent,
  1101. $previous,
  1102. $title,
  1103. $description,
  1104. $prerequisites = 0,
  1105. $audio = null,
  1106. $max_time_allowed = 0,
  1107. $url = ''
  1108. ) {
  1109. $course_id = api_get_course_int_id();
  1110. if ($this->debug > 0) {
  1111. error_log('New LP - In learnpath::edit_item()', 0);
  1112. }
  1113. if (empty ($max_time_allowed)) {
  1114. $max_time_allowed = 0;
  1115. }
  1116. if (empty ($id) || ($id != strval(intval($id))) || empty ($title)) {
  1117. return false;
  1118. }
  1119. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1120. $sql_select = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND id = " . $id;
  1121. $res_select = Database::query($sql_select);
  1122. $row_select = Database :: fetch_array($res_select);
  1123. $audio_update_sql = '';
  1124. if (is_array($audio) && !empty ($audio['tmp_name']) && $audio['error'] === 0) {
  1125. // Create the audio folder if it does not exist yet.
  1126. global $_course;
  1127. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  1128. if (!is_dir($filepath . 'audio')) {
  1129. mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
  1130. $audio_id = add_document(
  1131. $_course,
  1132. '/audio',
  1133. 'folder',
  1134. 0,
  1135. 'audio'
  1136. );
  1137. api_item_property_update(
  1138. $_course,
  1139. TOOL_DOCUMENT,
  1140. $audio_id,
  1141. 'FolderCreated',
  1142. api_get_user_id(),
  1143. null,
  1144. null,
  1145. null,
  1146. null,
  1147. api_get_session_id()
  1148. );
  1149. api_item_property_update(
  1150. $_course,
  1151. TOOL_DOCUMENT,
  1152. $audio_id,
  1153. 'invisible',
  1154. api_get_user_id(),
  1155. null,
  1156. null,
  1157. null,
  1158. null,
  1159. api_get_session_id()
  1160. );
  1161. }
  1162. // Upload file in documents.
  1163. $pi = pathinfo($audio['name']);
  1164. if ($pi['extension'] == 'mp3') {
  1165. $c_det = api_get_course_info($this->cc);
  1166. $bp = api_get_path(SYS_COURSE_PATH) . $c_det['path'] . '/document';
  1167. $path = handle_uploaded_document($c_det, $audio, $bp, '/audio', api_get_user_id(), 0, null, 0, 'rename', false, 0);
  1168. $path = substr($path, 7);
  1169. // Update reference in lp_item - audio path is the path from inside de document/audio/ dir.
  1170. $audio_update_sql = ", audio = '" . Database::escape_string($path) . "' ";
  1171. }
  1172. }
  1173. $same_parent = ($row_select['parent_item_id'] == $parent) ? true : false;
  1174. $same_previous = ($row_select['previous_item_id'] == $previous) ? true : false;
  1175. // TODO: htmlspecialchars to be checked for encoding related problems.
  1176. if ($same_parent && $same_previous) {
  1177. // Only update title and description.
  1178. $sql_update = " UPDATE " . $tbl_lp_item . "
  1179. SET title = '" . Database::escape_string($title) . "',
  1180. prerequisite = '" . $prerequisites . "',
  1181. description = '" . Database::escape_string($description) . "'
  1182. " . $audio_update_sql . ",
  1183. max_time_allowed = '" . Database::escape_string($max_time_allowed) . "'
  1184. WHERE c_id = ".$course_id." AND id = " . $id;
  1185. Database::query($sql_update);
  1186. } else {
  1187. $old_parent = $row_select['parent_item_id'];
  1188. $old_previous = $row_select['previous_item_id'];
  1189. $old_next = $row_select['next_item_id'];
  1190. $old_order = $row_select['display_order'];
  1191. $old_prerequisite = $row_select['prerequisite'];
  1192. $old_max_time_allowed = $row_select['max_time_allowed'];
  1193. /* BEGIN -- virtually remove the current item id */
  1194. /* for the next and previous item it is like the current item doesn't exist anymore */
  1195. if ($old_previous != 0) {
  1196. $sql_update_next = "
  1197. UPDATE " . $tbl_lp_item . "
  1198. SET next_item_id = " . $old_next . "
  1199. WHERE c_id = ".$course_id." AND id = " . $old_previous;
  1200. Database::query($sql_update_next);
  1201. //echo '<p>' . $sql_update_next . '</p>';
  1202. }
  1203. if ($old_next != 0) {
  1204. $sql_update_previous = "
  1205. UPDATE " . $tbl_lp_item . "
  1206. SET previous_item_id = " . $old_previous . "
  1207. WHERE c_id = ".$course_id." AND id = " . $old_next;
  1208. Database::query($sql_update_previous);
  1209. }
  1210. // display_order - 1 for every item with a display_order bigger then the display_order of the current item.
  1211. $sql_update_order = "
  1212. UPDATE " . $tbl_lp_item . "
  1213. SET display_order = display_order - 1
  1214. WHERE
  1215. c_id = ".$course_id." AND
  1216. display_order > " . $old_order . " AND lp_id = " . $this->lp_id . " AND
  1217. parent_item_id = " . $old_parent;
  1218. Database::query($sql_update_order);
  1219. /* END -- virtually remove the current item id */
  1220. /* BEGIN -- update the current item id to his new location */
  1221. if ($previous == 0) {
  1222. // Select the data of the item that should come after the current item.
  1223. $sql_select_old = "SELECT id, display_order
  1224. FROM " . $tbl_lp_item . "
  1225. WHERE
  1226. c_id = ".$course_id." AND
  1227. lp_id = " . $this->lp_id . " AND
  1228. parent_item_id = " . $parent . " AND
  1229. previous_item_id = " . $previous;
  1230. $res_select_old = Database::query($sql_select_old);
  1231. $row_select_old = Database::fetch_array($res_select_old);
  1232. //echo '<p>' . $sql_select_old . '</p>';
  1233. // If the new parent didn't have children before.
  1234. if (Database :: num_rows($res_select_old) == 0) {
  1235. $new_next = 0;
  1236. $new_order = 1;
  1237. } else {
  1238. $new_next = $row_select_old['id'];
  1239. $new_order = $row_select_old['display_order'];
  1240. }
  1241. } else {
  1242. // Select the data of the item that should come before the current item.
  1243. $sql_select_old = " SELECT next_item_id, display_order
  1244. FROM " . $tbl_lp_item . "
  1245. WHERE c_id = ".$course_id." AND id = " . $previous;
  1246. $res_select_old = Database::query($sql_select_old);
  1247. $row_select_old = Database :: fetch_array($res_select_old);
  1248. $new_next = $row_select_old['next_item_id'];
  1249. $new_order = $row_select_old['display_order'] + 1;
  1250. }
  1251. // TODO: htmlspecialchars to be checked for encoding related problems.
  1252. // Update the current item with the new data.
  1253. $sql_update = "UPDATE " . $tbl_lp_item . "
  1254. SET
  1255. title = '" . Database::escape_string($title) . "',
  1256. description = '" . Database::escape_string($description) . "',
  1257. parent_item_id = " . $parent . ",
  1258. previous_item_id = " . $previous . ",
  1259. next_item_id = " . $new_next . ",
  1260. display_order = " . $new_order . "
  1261. " . $audio_update_sql . "
  1262. WHERE c_id = ".$course_id." AND id = " . $id;
  1263. Database::query($sql_update);
  1264. if ($previous != 0) {
  1265. // Update the previous item's next_item_id.
  1266. $sql_update_previous = "
  1267. UPDATE " . $tbl_lp_item . "
  1268. SET next_item_id = " . $id . "
  1269. WHERE c_id = ".$course_id." AND id = " . $previous;
  1270. Database::query($sql_update_previous);
  1271. }
  1272. if ($new_next != 0) {
  1273. // Update the next item's previous_item_id.
  1274. $sql_update_next = "
  1275. UPDATE " . $tbl_lp_item . "
  1276. SET previous_item_id = " . $id . "
  1277. WHERE c_id = ".$course_id." AND id = " . $new_next;
  1278. Database::query($sql_update_next);
  1279. }
  1280. if ($old_prerequisite != $prerequisites) {
  1281. $sql_update_next = "
  1282. UPDATE " . $tbl_lp_item . "
  1283. SET prerequisite = " . $prerequisites . "
  1284. WHERE c_id = ".$course_id." AND id = " . $id;
  1285. Database::query($sql_update_next);
  1286. }
  1287. if ($old_max_time_allowed != $max_time_allowed) {
  1288. $sql_update_max_time_allowed = "
  1289. UPDATE " . $tbl_lp_item . "
  1290. SET max_time_allowed = " . $max_time_allowed . "
  1291. WHERE c_id = ".$course_id." AND id = " . $id;
  1292. Database::query($sql_update_max_time_allowed);
  1293. }
  1294. // Update all the items with the same or a bigger display_order than the current item.
  1295. $sql_update_order = "
  1296. UPDATE " . $tbl_lp_item . "
  1297. SET display_order = display_order + 1
  1298. WHERE
  1299. c_id = ".$course_id." AND
  1300. lp_id = " . $this->get_id() . " AND
  1301. id <> " . $id . " AND
  1302. parent_item_id = " . $parent . " AND
  1303. display_order >= " . $new_order;
  1304. Database::query($sql_update_order);
  1305. }
  1306. if ($row_select['item_type'] == 'link') {
  1307. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  1308. $link = new Link();
  1309. $linkId = $row_select['path'];
  1310. $link->updateLink($linkId, $url);
  1311. }
  1312. }
  1313. /**
  1314. * Updates an item's prereq in place
  1315. * @param integer Element ID
  1316. * @param string Prerequisite Element ID
  1317. * @param string Prerequisite item type
  1318. * @param string Prerequisite min score
  1319. * @param string Prerequisite max score
  1320. * @return boolean True on success, false on error
  1321. */
  1322. public function edit_item_prereq($id, $prerequisite_id, $mastery_score = 0, $max_score = 100)
  1323. {
  1324. $course_id = api_get_course_int_id();
  1325. if ($this->debug > 0) {
  1326. error_log('New LP - In learnpath::edit_item_prereq(' . $id . ',' . $prerequisite_id . ',' . $mastery_score . ',' . $max_score . ')', 0);
  1327. }
  1328. if (empty ($id) or ($id != strval(intval($id))) or empty ($prerequisite_id)) {
  1329. return false;
  1330. }
  1331. $prerequisite_id = intval($prerequisite_id);
  1332. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1333. if (!is_numeric($mastery_score) || $mastery_score < 0) {
  1334. $mastery_score = 0;
  1335. }
  1336. if (!is_numeric($max_score) || $max_score < 0) {
  1337. $max_score = 100;
  1338. }
  1339. if ($mastery_score > $max_score) {
  1340. $max_score = $mastery_score;
  1341. }
  1342. if (!is_numeric($prerequisite_id)) {
  1343. $prerequisite_id = 'NULL';
  1344. }
  1345. $sql = " UPDATE " . $tbl_lp_item . "
  1346. SET prerequisite = " . $prerequisite_id . "
  1347. WHERE c_id = ".$course_id." AND id = " . $id;
  1348. Database::query($sql);
  1349. if ($prerequisite_id != 'NULL' && $prerequisite_id != '') {
  1350. $sql = " UPDATE " . $tbl_lp_item . " SET
  1351. mastery_score = " . $mastery_score .
  1352. //", max_score = " . $max_score . " " . // Max score cannot be changed in the form anyway - see display_item_prerequisites_form().
  1353. " WHERE c_id = ".$course_id." AND ref = '" . $prerequisite_id . "'"; // Will this be enough to ensure unicity?
  1354. Database::query($sql);
  1355. }
  1356. // TODO: Update the item object (can be ignored for now because refreshed).
  1357. return true;
  1358. }
  1359. /**
  1360. * Escapes a string with the available database escape function
  1361. * @param string String to escape
  1362. * @return string String escaped
  1363. * @deprecated use Database::escape_string
  1364. */
  1365. public function escape_string($string)
  1366. {
  1367. return Database::escape_string($string);
  1368. }
  1369. /**
  1370. * Static admin function exporting a learnpath into a zip file
  1371. * @param string Export type (scorm, zip, cd)
  1372. * @param string Course code
  1373. * @param integer Learnpath ID
  1374. * @param string Zip file name
  1375. * @return string Zip file path (or false on error)
  1376. */
  1377. public function export_lp($type, $course, $id, $zipname)
  1378. {
  1379. if (empty($type) || empty($course) || empty($id) || empty($zipname)) {
  1380. return false;
  1381. }
  1382. $url = '';
  1383. switch ($type) {
  1384. case 'scorm':
  1385. break;
  1386. case 'zip':
  1387. break;
  1388. case 'cdrom':
  1389. break;
  1390. }
  1391. return $url;
  1392. }
  1393. /**
  1394. * Gets all the chapters belonging to the same parent as the item/chapter given
  1395. * Can also be called as abstract method
  1396. * @param integer Item ID
  1397. * @return array A list of all the "brother items" (or an empty array on failure)
  1398. */
  1399. public function get_brother_chapters($id)
  1400. {
  1401. $course_id = api_get_course_int_id();
  1402. if ($this->debug > 0) {
  1403. error_log('New LP - In learnpath::get_brother_chapters()', 0);
  1404. }
  1405. if (empty ($id) OR $id != strval(intval($id))) {
  1406. return array ();
  1407. }
  1408. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1409. $sql_parent = "SELECT * FROM $lp_item
  1410. WHERE c_id = ".$course_id." AND id = $id AND item_type='dokeos_chapter'";
  1411. $res_parent = Database::query($sql_parent);
  1412. if (Database :: num_rows($res_parent) > 0) {
  1413. $row_parent = Database :: fetch_array($res_parent);
  1414. $parent = $row_parent['parent_item_id'];
  1415. $sql_bros = "SELECT * FROM $lp_item
  1416. WHERE
  1417. c_id = ".$course_id." AND
  1418. parent_item_id = $parent AND
  1419. id = $id AND
  1420. item_type='dokeos_chapter'
  1421. ORDER BY display_order";
  1422. $res_bros = Database::query($sql_bros);
  1423. $list = array ();
  1424. while ($row_bro = Database :: fetch_array($res_bros)) {
  1425. $list[] = $row_bro;
  1426. }
  1427. return $list;
  1428. }
  1429. return array ();
  1430. }
  1431. /**
  1432. * Gets all the items belonging to the same parent as the item given
  1433. * Can also be called as abstract method
  1434. * @param integer Item ID
  1435. * @return array A list of all the "brother items" (or an empty array on failure)
  1436. */
  1437. public function get_brother_items($id) {
  1438. $course_id = api_get_course_int_id();
  1439. if ($this->debug > 0) {
  1440. error_log('New LP - In learnpath::get_brother_items(' . $id . ')', 0);
  1441. }
  1442. if (empty ($id) OR $id != strval(intval($id))) {
  1443. return array ();
  1444. }
  1445. $lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  1446. $sql_parent = "SELECT * FROM $lp_item WHERE c_id = $course_id AND id = $id";
  1447. $res_parent = Database::query($sql_parent);
  1448. if (Database :: num_rows($res_parent) > 0) {
  1449. $row_parent = Database :: fetch_array($res_parent);
  1450. $parent = $row_parent['parent_item_id'];
  1451. $sql_bros = "SELECT * FROM $lp_item WHERE c_id = ".$course_id." AND parent_item_id = $parent
  1452. ORDER BY display_order";
  1453. $res_bros = Database::query($sql_bros);
  1454. $list = array ();
  1455. while ($row_bro = Database :: fetch_array($res_bros)) {
  1456. $list[] = $row_bro;
  1457. }
  1458. return $list;
  1459. }
  1460. return array ();
  1461. }
  1462. /**
  1463. * Get the specific prefix index terms of this learning path
  1464. * @param string $prefix
  1465. * @return array Array of terms
  1466. */
  1467. public function get_common_index_terms_by_prefix($prefix)
  1468. {
  1469. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1470. $terms = get_specific_field_values_list_by_prefix(
  1471. $prefix,
  1472. $this->cc,
  1473. TOOL_LEARNPATH,
  1474. $this->lp_id
  1475. );
  1476. $prefix_terms = array();
  1477. if (!empty($terms)) {
  1478. foreach ($terms as $term) {
  1479. $prefix_terms[] = $term['value'];
  1480. }
  1481. }
  1482. return $prefix_terms;
  1483. }
  1484. /**
  1485. * Gets the number of items currently completed
  1486. * @return integer The number of items currently completed
  1487. */
  1488. public function get_complete_items_count()
  1489. {
  1490. if ($this->debug > 0) {
  1491. error_log('New LP - In learnpath::get_complete_items_count()', 0);
  1492. }
  1493. $i = 0;
  1494. $completedStatusList = array(
  1495. 'completed',
  1496. 'passed',
  1497. 'succeeded',
  1498. 'browsed',
  1499. 'failed'
  1500. );
  1501. foreach ($this->items as $id => $dummy) {
  1502. // Trying failed and browsed considered "progressed" as well.
  1503. if ($this->items[$id]->status_is($completedStatusList) &&
  1504. $this->items[$id]->get_type() != 'dokeos_chapter' &&
  1505. $this->items[$id]->get_type() != 'dir'
  1506. ) {
  1507. $i++;
  1508. }
  1509. }
  1510. return $i;
  1511. }
  1512. /**
  1513. * Gets the current item ID
  1514. * @return integer The current learnpath item id
  1515. */
  1516. public function get_current_item_id()
  1517. {
  1518. $current = 0;
  1519. if ($this->debug > 0) {
  1520. error_log('New LP - In learnpath::get_current_item_id()', 0);
  1521. }
  1522. if (!empty($this->current)) {
  1523. $current = $this->current;
  1524. }
  1525. if ($this->debug > 2) {
  1526. error_log('New LP - In learnpath::get_current_item_id() - Returning ' . $current, 0);
  1527. }
  1528. return $current;
  1529. }
  1530. /**
  1531. * Force to get the first learnpath item id
  1532. * @return integer The current learnpath item id
  1533. */
  1534. public function get_first_item_id()
  1535. {
  1536. $current = 0;
  1537. if (is_array($this->ordered_items)) {
  1538. $current = $this->ordered_items[0];
  1539. }
  1540. return $current;
  1541. }
  1542. /**
  1543. * Gets the total number of items available for viewing in this SCORM
  1544. * @return integer The total number of items
  1545. */
  1546. public function get_total_items_count()
  1547. {
  1548. if ($this->debug > 0) {
  1549. error_log('New LP - In learnpath::get_total_items_count()', 0);
  1550. }
  1551. return count($this->items);
  1552. }
  1553. /**
  1554. * Gets the total number of items available for viewing in this SCORM but without chapters
  1555. * @return integer The total no-chapters number of items
  1556. */
  1557. public function get_total_items_count_without_chapters()
  1558. {
  1559. if ($this->debug > 0) {
  1560. error_log('New LP - In learnpath::get_total_items_count_without_chapters()', 0);
  1561. }
  1562. $total = 0;
  1563. $typeListNotToCount = self::getChapterTypes();
  1564. foreach ($this->items as $temp2) {
  1565. if (!in_array($temp2->get_type(), $typeListNotToCount)) {
  1566. $total++;
  1567. }
  1568. }
  1569. return $total;
  1570. }
  1571. /**
  1572. * Gets the first element URL.
  1573. * @return string URL to load into the viewer
  1574. */
  1575. public function first()
  1576. {
  1577. if ($this->debug > 0) {
  1578. error_log('New LP - In learnpath::first()', 0);
  1579. error_log('$this->last_item_seen '.$this->last_item_seen);
  1580. }
  1581. // Test if the last_item_seen exists and is not a dir.
  1582. if (count($this->ordered_items) == 0) {
  1583. $this->index = 0;
  1584. }
  1585. if ($this->debug > 0) {
  1586. if (isset($this->items[$this->last_item_seen])) {
  1587. $status = $this->items[$this->last_item_seen]->get_status();
  1588. }
  1589. error_log('status '.$status);
  1590. }
  1591. if (!empty($this->last_item_seen) &&
  1592. !empty($this->items[$this->last_item_seen]) &&
  1593. $this->items[$this->last_item_seen]->get_type() != 'dir' &&
  1594. $this->items[$this->last_item_seen]->get_type() != 'dokeos_chapter'
  1595. //with this change (below) the LP will NOT go to the next item, it will take lp item we left
  1596. //&& !$this->items[$this->last_item_seen]->is_done()
  1597. ) {
  1598. if ($this->debug > 2) {
  1599. error_log('New LP - In learnpath::first() - Last item seen is ' . $this->last_item_seen.' of type '.$this->items[$this->last_item_seen]->get_type(), 0);
  1600. }
  1601. $index = -1;
  1602. foreach ($this->ordered_items as $myindex => $item_id) {
  1603. if ($item_id == $this->last_item_seen) {
  1604. $index = $myindex;
  1605. break;
  1606. }
  1607. }
  1608. if ($index == -1) {
  1609. // Index hasn't changed, so item not found - panic (this shouldn't happen).
  1610. if ($this->debug > 2) {
  1611. error_log('New LP - Last item (' . $this->last_item_seen . ') was found in items but not in ordered_items, panic!', 0);
  1612. }
  1613. return false;
  1614. } else {
  1615. $this->last = $this->last_item_seen;
  1616. $this->current = $this->last_item_seen;
  1617. $this->index = $index;
  1618. }
  1619. } else {
  1620. if ($this->debug > 2) {
  1621. error_log('New LP - In learnpath::first() - No last item seen', 0);
  1622. }
  1623. $index = 0;
  1624. // Loop through all ordered items and stop at the first item that is
  1625. // not a directory *and* that has not been completed yet.
  1626. while ( !empty($this->ordered_items[$index]) AND
  1627. is_a($this->items[$this->ordered_items[$index]], 'learnpathItem') AND
  1628. (
  1629. $this->items[$this->ordered_items[$index]]->get_type() == 'dir' OR
  1630. $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter' OR
  1631. $this->items[$this->ordered_items[$index]]->is_done() === true
  1632. ) AND $index < $this->max_ordered_items) {
  1633. $index++;
  1634. }
  1635. $this->last = $this->current;
  1636. // current is
  1637. $this->current = isset($this->ordered_items[$index]) ? $this->ordered_items[$index] : null;
  1638. $this->index = $index;
  1639. if ($this->debug > 2) {
  1640. error_log('$index ' . $index);
  1641. }
  1642. if ($this->debug > 2) {
  1643. error_log('New LP - In learnpath::first() - No last item seen. New last = ' . $this->last . '(' . $this->ordered_items[$index] . ')', 0);
  1644. }
  1645. }
  1646. if ($this->debug > 2) {
  1647. error_log('New LP - In learnpath::first() - First item is ' . $this->get_current_item_id());
  1648. }
  1649. }
  1650. /**
  1651. * Gets the information about an item in a format usable as JavaScript to update
  1652. * the JS API by just printing this content into the <head> section of the message frame
  1653. * @param int $item_id
  1654. * @return string
  1655. */
  1656. public function get_js_info($item_id = '')
  1657. {
  1658. if ($this->debug > 0) {
  1659. error_log('New LP - In learnpath::get_js_info(' . $item_id . ')', 0);
  1660. }
  1661. $info = '';
  1662. $item_id = intval($item_id);
  1663. if (!empty($item_id) && is_object($this->items[$item_id])) {
  1664. //if item is defined, return values from DB
  1665. $oItem = $this->items[$item_id];
  1666. $info .= '<script language="javascript">';
  1667. $info .= "top.set_score(" . $oItem->get_score() . ");\n";
  1668. $info .= "top.set_max(" . $oItem->get_max() . ");\n";
  1669. $info .= "top.set_min(" . $oItem->get_min() . ");\n";
  1670. $info .= "top.set_lesson_status('" . $oItem->get_status() . "');";
  1671. $info .= "top.set_session_time('" . $oItem->get_scorm_time('js') . "');";
  1672. $info .= "top.set_suspend_data('" . $oItem->get_suspend_data() . "');";
  1673. $info .= "top.set_saved_lesson_status('" . $oItem->get_status() . "');";
  1674. $info .= "top.set_flag_synchronized();";
  1675. $info .= '</script>';
  1676. if ($this->debug > 2) {
  1677. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1678. }
  1679. return $info;
  1680. } else {
  1681. // If item_id is empty, just update to default SCORM data.
  1682. $info .= '<script language="javascript">';
  1683. $info .= "top.set_score(" . learnpathItem :: get_score() . ");\n";
  1684. $info .= "top.set_max(" . learnpathItem :: get_max() . ");\n";
  1685. $info .= "top.set_min(" . learnpathItem :: get_min() . ");\n";
  1686. $info .= "top.set_lesson_status('" . learnpathItem :: get_status() . "');";
  1687. $info .= "top.set_session_time('" . learnpathItem :: getScormTimeFromParameter('js') . "');";
  1688. $info .= "top.set_suspend_data('" . learnpathItem :: get_suspend_data() . "');";
  1689. $info .= "top.set_saved_lesson_status('" . learnpathItem :: get_status() . "');";
  1690. $info .= "top.set_flag_synchronized();";
  1691. $info .= '</script>';
  1692. if ($this->debug > 2) {
  1693. error_log('New LP - in learnpath::get_js_info(' . $item_id . ') - returning: ' . $info, 0);
  1694. }
  1695. return $info;
  1696. }
  1697. }
  1698. /**
  1699. * Gets the js library from the database
  1700. * @return string The name of the javascript library to be used
  1701. */
  1702. public function get_js_lib()
  1703. {
  1704. $lib = '';
  1705. if (!empty ($this->js_lib)) {
  1706. $lib = $this->js_lib;
  1707. }
  1708. return $lib;
  1709. }
  1710. /**
  1711. * Gets the learnpath database ID
  1712. * @return integer Learnpath ID in the lp table
  1713. */
  1714. public function get_id()
  1715. {
  1716. if (!empty ($this->lp_id)) {
  1717. return $this->lp_id;
  1718. } else {
  1719. return 0;
  1720. }
  1721. }
  1722. /**
  1723. * Gets the last element URL.
  1724. * @return string URL to load into the viewer
  1725. */
  1726. public function get_last()
  1727. {
  1728. if ($this->debug > 0) {
  1729. error_log('New LP - In learnpath::get_last()', 0);
  1730. }
  1731. $this->index = count($this->ordered_items) - 1;
  1732. return $this->ordered_items[$this->index];
  1733. }
  1734. /**
  1735. * Gets the navigation bar for the learnpath display screen
  1736. * @return string The HTML string to use as a navigation bar
  1737. */
  1738. public function get_navigation_bar() {
  1739. if ($this->debug > 0) {
  1740. error_log('New LP - In learnpath::get_navigation_bar()', 0);
  1741. }
  1742. $navbar = null;
  1743. $lp_id = $this->lp_id;
  1744. $mycurrentitemid = $this->get_current_item_id();
  1745. if ($this->mode == 'fullscreen') {
  1746. $navbar = '
  1747. <div class="buttons">
  1748. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&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/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1749. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1750. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>.
  1751. <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('ScormExitFullScreen').'"></a>
  1752. </div>';
  1753. } else {
  1754. $navbar = '
  1755. <div class="buttons">
  1756. <a href="lp_controller.php?action=stats&'.api_get_cidreq(true).'&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/btn_stats.png" title="' . get_lang('Reporting') . '"></a>
  1757. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'previous\');return false;" title="previous"><img border="0" src="../img/btn_previous.png" title="' . get_lang('ScormPrevious') . '"></a>
  1758. <a href="" onClick="switch_item(' . $mycurrentitemid . ',\'next\');return false;" title="next" ><img border="0" src="../img/btn_next.png" title="' . get_lang('ScormNext') . '"></a>
  1759. </div>';
  1760. }
  1761. return $navbar;
  1762. }
  1763. /**
  1764. * Gets the next resource in queue (url).
  1765. * @return string URL to load into the viewer
  1766. */
  1767. public function get_next_index()
  1768. {
  1769. if ($this->debug > 0) {
  1770. error_log('New LP - In learnpath::get_next_index()', 0);
  1771. }
  1772. // TODO
  1773. $index = $this->index;
  1774. $index++;
  1775. if ($this->debug > 2) {
  1776. error_log('New LP - Now looking at ordered_items[' . ($index) . '] - type is ' . $this->items[$this->ordered_items[$index]]->type, 0);
  1777. }
  1778. while (!empty ($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') AND $index < $this->max_ordered_items) {
  1779. $index++;
  1780. if ($index == $this->max_ordered_items){
  1781. if ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter') {
  1782. return $this->index;
  1783. } else {
  1784. return $index;
  1785. }
  1786. }
  1787. }
  1788. if (empty ($this->ordered_items[$index])) {
  1789. return $this->index;
  1790. }
  1791. if ($this->debug > 2) {
  1792. error_log('New LP - index is now ' . $index, 0);
  1793. }
  1794. return $index;
  1795. }
  1796. /**
  1797. * Gets item_id for the next element
  1798. * @return integer Next item (DB) ID
  1799. */
  1800. public function get_next_item_id()
  1801. {
  1802. if ($this->debug > 0) {
  1803. error_log('New LP - In learnpath::get_next_item_id()', 0);
  1804. }
  1805. $new_index = $this->get_next_index();
  1806. if (!empty ($new_index)) {
  1807. if (isset ($this->ordered_items[$new_index])) {
  1808. if ($this->debug > 2) {
  1809. error_log('New LP - In learnpath::get_next_index() - Returning ' . $this->ordered_items[$new_index], 0);
  1810. }
  1811. return $this->ordered_items[$new_index];
  1812. }
  1813. }
  1814. if ($this->debug > 2) {
  1815. error_log('New LP - In learnpath::get_next_index() - Problem - Returning 0', 0);
  1816. }
  1817. return 0;
  1818. }
  1819. /**
  1820. * Returns the package type ('scorm','aicc','scorm2004','dokeos','ppt'...)
  1821. *
  1822. * Generally, the package provided is in the form of a zip file, so the function
  1823. * has been written to test a zip file. If not a zip, the function will return the
  1824. * default return value: ''
  1825. * @param string the path to the file
  1826. * @param string the original name of the file
  1827. * @return string 'scorm','aicc','scorm2004','dokeos' or '' if the package cannot be recognized
  1828. */
  1829. public static function get_package_type($file_path, $file_name)
  1830. {
  1831. // Get name of the zip file without the extension.
  1832. $file_info = pathinfo($file_name);
  1833. $filename = $file_info['basename']; // Name including extension.
  1834. $extension = $file_info['extension']; // Extension only.
  1835. if (!empty($_POST['ppt2lp']) && !in_array(strtolower($extension), array(
  1836. 'dll',
  1837. 'exe'
  1838. ))) {
  1839. return 'oogie';
  1840. }
  1841. if (!empty($_POST['woogie']) && !in_array(strtolower($extension), array(
  1842. 'dll',
  1843. 'exe'
  1844. ))) {
  1845. return 'woogie';
  1846. }
  1847. // Filename without its extension.
  1848. $file_base_name = str_replace('.' . $extension, '', $filename);
  1849. $zipFile = new PclZip($file_path);
  1850. // Check the zip content (real size and file extension).
  1851. $zipContentArray = $zipFile->listContent();
  1852. $package_type = '';
  1853. $at_root = false;
  1854. $manifest = '';
  1855. $aicc_match_crs = 0;
  1856. $aicc_match_au = 0;
  1857. $aicc_match_des = 0;
  1858. $aicc_match_cst = 0;
  1859. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  1860. if (is_array($zipContentArray) && count($zipContentArray) > 0) {
  1861. foreach ($zipContentArray as $thisContent) {
  1862. if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
  1863. // New behaviour: Don't do anything. These files will be removed in scorm::import_package.
  1864. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml') !== false) {
  1865. $manifest = $thisContent['filename']; // Just the relative directory inside scorm/
  1866. $package_type = 'scorm';
  1867. break; // Exit the foreach loop.
  1868. } elseif (
  1869. preg_match('/aicc\//i', $thisContent['filename']) ||
  1870. in_array(strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION)), array( 'crs','au','des','cst'))
  1871. ) {
  1872. $ext = strtolower(pathinfo($thisContent['filename'], PATHINFO_EXTENSION));
  1873. switch ($ext) {
  1874. case 'crs':
  1875. $aicc_match_crs = 1;
  1876. break;
  1877. case 'au':
  1878. $aicc_match_au = 1;
  1879. break;
  1880. case 'des':
  1881. $aicc_match_des = 1;
  1882. break;
  1883. case 'cst':
  1884. $aicc_match_cst = 1;
  1885. break;
  1886. default:
  1887. break;
  1888. }
  1889. //break; // Don't exit the loop, because if we find an imsmanifest afterwards, we want it, not the AICC.
  1890. } else {
  1891. $package_type = '';
  1892. }
  1893. }
  1894. }
  1895. if (empty($package_type) && 4 == ($aicc_match_crs + $aicc_match_au + $aicc_match_des + $aicc_match_cst)) {
  1896. // If found an aicc directory... (!= false means it cannot be false (error) or 0 (no match)).
  1897. $package_type = 'aicc';
  1898. }
  1899. return $package_type;
  1900. }
  1901. /**
  1902. * Gets the previous resource in queue (url). Also initialises time values for this viewing
  1903. * @return string URL to load into the viewer
  1904. */
  1905. public function get_previous_index()
  1906. {
  1907. if ($this->debug > 0) {
  1908. error_log('New LP - In learnpath::get_previous_index()', 0);
  1909. }
  1910. $index = $this->index;
  1911. if (isset ($this->ordered_items[$index -1])) {
  1912. $index--;
  1913. while (isset($this->ordered_items[$index]) AND ($this->items[$this->ordered_items[$index]]->get_type() == 'dir' || $this->items[$this->ordered_items[$index]]->get_type() == 'dokeos_chapter')) {
  1914. $index--;
  1915. if ($index < 0) {
  1916. return $this->index;
  1917. }
  1918. }
  1919. } else {
  1920. if ($this->debug > 2) {
  1921. error_log('New LP - get_previous_index() - there was no previous index available, reusing ' . $index, 0);
  1922. }
  1923. // There is no previous item.
  1924. }
  1925. return $index;
  1926. }
  1927. /**
  1928. * Gets item_id for the next element
  1929. * @return integer Previous item (DB) ID
  1930. */
  1931. public function get_previous_item_id()
  1932. {
  1933. if ($this->debug > 0) {
  1934. error_log('New LP - In learnpath::get_previous_item_id()', 0);
  1935. }
  1936. $new_index = $this->get_previous_index();
  1937. return $this->ordered_items[$new_index];
  1938. }
  1939. /**
  1940. * Gets the progress value from the progress_db attribute
  1941. * @return integer Current progress value
  1942. */
  1943. public function get_progress()
  1944. {
  1945. if ($this->debug > 0) {
  1946. error_log('New LP - In learnpath::get_progress()', 0);
  1947. }
  1948. if (!empty ($this->progress_db)) {
  1949. return $this->progress_db;
  1950. }
  1951. return 0;
  1952. }
  1953. /**
  1954. * Gets the progress value from the progress field in the database (allows use as abstract method)
  1955. * @param integer $lp_id Learnpath ID
  1956. * @param integer $user_id User ID
  1957. * @param string $mode Mode of display ('%','abs' or 'both')
  1958. * @param string $course_code Course code (optional, defaults to '')
  1959. * @param boolean $sincere Whether to return null if no record
  1960. * was found (true), or 0 (false) (optional, defaults to false)
  1961. * @param int $session_id
  1962. * @return integer Current progress value as found in the database
  1963. * @deprecated
  1964. */
  1965. public static function get_db_progress(
  1966. $lp_id,
  1967. $user_id,
  1968. $mode = '%',
  1969. $course_code = '',
  1970. $sincere = false,
  1971. $session_id = 0
  1972. ) {
  1973. $table = Database :: get_course_table(TABLE_LP_VIEW);
  1974. $lp_id = intval($lp_id);
  1975. $session_id = intval($session_id);
  1976. $user_id = intval($user_id);
  1977. $course_info = api_get_course_info($course_code);
  1978. $session_condition = api_get_session_condition($session_id);
  1979. $course_id = $course_info['real_id'];
  1980. $sql = "SELECT * FROM $table
  1981. WHERE c_id = ".$course_id." AND lp_id = $lp_id AND user_id = $user_id $session_condition";
  1982. $res = Database::query($sql);
  1983. $view_id = 0;
  1984. if (Database :: num_rows($res) > 0) {
  1985. $row = Database :: fetch_array($res);
  1986. $progress = $row['progress'];
  1987. $view_id = $row['id'];
  1988. } else {
  1989. if ($sincere) {
  1990. return null;
  1991. }
  1992. }
  1993. if (empty($progress)) {
  1994. $progress = '0';
  1995. }
  1996. if ($mode == '%') {
  1997. return $progress . '%';
  1998. } else {
  1999. // Get the number of items completed and the number of items total.
  2000. $tbl = Database :: get_course_table(TABLE_LP_ITEM);
  2001. $sql = "SELECT count(*) FROM $tbl
  2002. WHERE
  2003. c_id = $course_id AND
  2004. c_id = ".$course_id." AND
  2005. lp_id = " . $lp_id . " AND
  2006. item_type NOT IN('dokeos_chapter','chapter','dir')";
  2007. $res = Database::query($sql);
  2008. $row = Database :: fetch_array($res);
  2009. $total = $row[0];
  2010. $tbl_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2011. $tbl_item = Database :: get_course_table(TABLE_LP_ITEM);
  2012. // Trying as also counting browsed and failed items.
  2013. $sql = "SELECT count(distinct(lp_item_id))
  2014. FROM $tbl_item_view as item_view
  2015. INNER JOIN $tbl_item as item
  2016. ON
  2017. item.id = item_view.lp_item_id AND
  2018. item_type NOT IN('dokeos_chapter','chapter','dir')
  2019. WHERE
  2020. item_view.c_id = $course_id AND
  2021. item.c_id = $course_id AND
  2022. lp_view_id = " . $view_id . " AND
  2023. status IN ('passed','completed','succeeded','browsed','failed')";
  2024. $res = Database::query($sql);
  2025. $row = Database :: fetch_array($res);
  2026. $completed = $row[0];
  2027. if ($mode == 'abs') {
  2028. return $completed . '/' . $total;
  2029. } elseif ($mode == 'both') {
  2030. if ($progress < ($completed / ($total ? $total : 1))) {
  2031. $progress = number_format(($completed / ($total ? $total : 1)) * 100, 0);
  2032. }
  2033. return $progress . '% (' . $completed . '/' . $total . ')';
  2034. }
  2035. }
  2036. return $progress;
  2037. }
  2038. /**
  2039. * Returns the HTML necessary to print a mediaplayer block inside a page
  2040. * @return string The mediaplayer HTML
  2041. */
  2042. public function get_mediaplayer($autostart = 'true')
  2043. {
  2044. $course_id = api_get_course_int_id();
  2045. global $_course;
  2046. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2047. $tbl_lp_item_view = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  2048. // Getting all the information about the item.
  2049. $sql = "SELECT * FROM ".$tbl_lp_item." as lp
  2050. INNER JOIN ".$tbl_lp_item_view." as lp_view on lp.id = lp_view.lp_item_id
  2051. WHERE
  2052. lp.id = '".$_SESSION['oLP']->current."' AND
  2053. lp.c_id = $course_id AND
  2054. lp_view.c_id = $course_id";
  2055. $result = Database::query($sql);
  2056. $row = Database::fetch_assoc($result);
  2057. $output = '';
  2058. if (!empty ($row['audio'])) {
  2059. $list = $_SESSION['oLP']->get_toc();
  2060. $type_quiz = false;
  2061. foreach($list as $toc) {
  2062. if ($toc['id'] == $_SESSION['oLP']->current && ($toc['type']=='quiz') ) {
  2063. $type_quiz = true;
  2064. }
  2065. }
  2066. if ($type_quiz) {
  2067. if ($_SESSION['oLP']->prevent_reinit == 1) {
  2068. $row['status'] === 'completed' ? $autostart_audio = 'false' : $autostart_audio = 'true';
  2069. } else {
  2070. $autostart_audio = $autostart;
  2071. }
  2072. } else {
  2073. $autostart_audio = 'true';
  2074. }
  2075. $courseInfo = api_get_course_info();
  2076. $audio = $row['audio'];
  2077. $file = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio;
  2078. $url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document/audio/'.$audio.'?'.api_get_cidreq();
  2079. if (!file_exists($file)) {
  2080. $lpPathInfo = $_SESSION['oLP']->generate_lp_folder(api_get_course_info());
  2081. $file = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio;
  2082. $url = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$lpPathInfo['dir'].$audio.'?'.api_get_cidreq();
  2083. }
  2084. $player = Display::getMediaPlayer(
  2085. $file,
  2086. array(
  2087. 'id' => 'lp_audio_media_player',
  2088. 'url' => $url,
  2089. 'autoplay' => $autostart_audio,
  2090. 'width' => '100%'
  2091. )
  2092. );
  2093. // The mp3 player.
  2094. $output = '<div id="container">';
  2095. $output .= $player;
  2096. $output .= '</div>';
  2097. }
  2098. return $output;
  2099. }
  2100. /**
  2101. * Checks if the learning path is visible for student after the progress
  2102. * of its prerequisite is completed, considering the time availability and
  2103. * the LP visibility.
  2104. * @param int Learnpath id
  2105. * @param int Student id
  2106. * @param string Course code (optional)
  2107. * @param int $sessionId
  2108. * @return bool
  2109. */
  2110. public static function is_lp_visible_for_student(
  2111. $lp_id,
  2112. $student_id,
  2113. $courseCode = null,
  2114. $sessionId = null
  2115. ) {
  2116. $lp_id = (int)$lp_id;
  2117. $course = api_get_course_info($courseCode);
  2118. $sessionId = intval($sessionId);
  2119. if (empty($sessionId)) {
  2120. $sessionId = api_get_session_id();
  2121. }
  2122. $tbl_learnpath = Database::get_course_table(TABLE_LP_MAIN);
  2123. // Get current prerequisite
  2124. $sql = "SELECT id, prerequisite, publicated_on, expired_on
  2125. FROM $tbl_learnpath
  2126. WHERE c_id = ".$course['real_id']." AND id = $lp_id";
  2127. $itemInfo = api_get_item_property_info(
  2128. $course['real_id'],
  2129. TOOL_LEARNPATH,
  2130. $lp_id,
  2131. $sessionId
  2132. );
  2133. // If the item was deleted.
  2134. if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
  2135. return false;
  2136. }
  2137. $rs = Database::query($sql);
  2138. $now = time();
  2139. if (Database::num_rows($rs)>0) {
  2140. $row = Database::fetch_array($rs, 'ASSOC');
  2141. $prerequisite = $row['prerequisite'];
  2142. $is_visible = true;
  2143. if (!empty($prerequisite)) {
  2144. $progress = self::getProgress(
  2145. $prerequisite,
  2146. $student_id,
  2147. $course['real_id'],
  2148. $sessionId
  2149. );
  2150. $progress = intval($progress);
  2151. if ($progress < 100) {
  2152. $is_visible = false;
  2153. }
  2154. }
  2155. // Also check the time availability of the LP
  2156. if ($is_visible) {
  2157. // Adding visibility restrictions
  2158. if (!empty($row['publicated_on']) &&
  2159. $row['publicated_on'] != '0000-00-00 00:00:00'
  2160. ) {
  2161. if ($now < api_strtotime($row['publicated_on'], 'UTC')) {
  2162. //api_not_allowed();
  2163. $is_visible = false;
  2164. }
  2165. }
  2166. // Blocking empty start times see BT#2800
  2167. global $_custom;
  2168. if (isset($_custom['lps_hidden_when_no_start_date']) &&
  2169. $_custom['lps_hidden_when_no_start_date']
  2170. ) {
  2171. if (empty($row['publicated_on']) || $row['publicated_on'] == '0000-00-00 00:00:00') {
  2172. //api_not_allowed();
  2173. $is_visible = false;
  2174. }
  2175. }
  2176. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  2177. if ($now > api_strtotime($row['expired_on'], 'UTC')) {
  2178. //api_not_allowed();
  2179. $is_visible = false;
  2180. }
  2181. }
  2182. }
  2183. return $is_visible;
  2184. }
  2185. return false;
  2186. }
  2187. /**
  2188. * @param int $lpId
  2189. * @param int $userId
  2190. * @param int $courseId
  2191. * @param int $sessionId
  2192. * @return int
  2193. */
  2194. public static function getProgress($lpId, $userId, $courseId, $sessionId = 0)
  2195. {
  2196. $lpId = intval($lpId);
  2197. $userId = intval($userId);
  2198. $courseId = intval($courseId);
  2199. $sessionId = intval($sessionId);
  2200. $progress = 0;
  2201. $sessionCondition = api_get_session_condition($sessionId);
  2202. $table = Database :: get_course_table(TABLE_LP_VIEW);
  2203. $sql = "SELECT * FROM $table
  2204. WHERE
  2205. c_id = ".$courseId." AND
  2206. lp_id = $lpId AND
  2207. user_id = $userId $sessionCondition";
  2208. $res = Database::query($sql);
  2209. if (Database :: num_rows($res) > 0) {
  2210. $row = Database:: fetch_array($res);
  2211. $progress = $row['progress'];
  2212. }
  2213. return $progress;
  2214. }
  2215. /**
  2216. * Displays a progress bar
  2217. * completed so far.
  2218. * @param integer $percentage Progress value to display
  2219. * @param string $text_add Text to display near the progress value
  2220. * @return string HTML string containing the progress bar
  2221. */
  2222. public static function get_progress_bar($percentage = -1, $text_add = '')
  2223. {
  2224. $text = $percentage . $text_add;
  2225. $output = '<div class="progress">
  2226. <div id="progress_bar_value" class="bar" style="width: '.$text.';"><span id="progress_text">'. $text .'</span></div>
  2227. </div>';
  2228. return $output;
  2229. }
  2230. /**
  2231. * @param string $mode can be '%' or 'abs'
  2232. * otherwise this value will be used $this->progress_bar_mode
  2233. * @return string
  2234. */
  2235. public function getProgressBar($mode = null)
  2236. {
  2237. list($percentage, $text_add) = $this->get_progress_bar_text($mode);
  2238. return self::get_progress_bar($percentage, $text_add);
  2239. }
  2240. /**
  2241. * Gets the progress bar info to display inside the progress bar.
  2242. * Also used by scorm_api.php
  2243. * @param string $mode Mode of display (can be '%' or 'abs').abs means
  2244. * we display a number of completed elements per total elements
  2245. * @param integer $add Additional steps to fake as completed
  2246. * @return list array Percentage or number and symbol (% or /xx)
  2247. */
  2248. public function get_progress_bar_text($mode = '', $add = 0)
  2249. {
  2250. if ($this->debug > 0) {
  2251. error_log('New LP - In learnpath::get_progress_bar_text()', 0);
  2252. }
  2253. if (empty($mode)) {
  2254. $mode = $this->progress_bar_mode;
  2255. }
  2256. $total_items = $this->get_total_items_count_without_chapters();
  2257. if ($this->debug > 2) {
  2258. error_log('New LP - Total items available in this learnpath: ' . $total_items, 0);
  2259. }
  2260. $completeItems = $this->get_complete_items_count();
  2261. if ($this->debug > 2) {
  2262. error_log('New LP - Items completed so far: ' . $completeItems, 0);
  2263. }
  2264. if ($add != 0) {
  2265. $completeItems += $add;
  2266. if ($this->debug > 2) {
  2267. error_log('New LP - Items completed so far (+modifier): ' . $completeItems, 0);
  2268. }
  2269. }
  2270. $text = '';
  2271. if ($completeItems > $total_items) {
  2272. $completeItems = $total_items;
  2273. }
  2274. $percentage = 0;
  2275. if ($mode == '%') {
  2276. if ($total_items > 0) {
  2277. $percentage = ((float) $completeItems / (float) $total_items) * 100;
  2278. } else {
  2279. $percentage = 0;
  2280. }
  2281. $percentage = number_format($percentage, 0);
  2282. $text = '%';
  2283. } elseif ($mode == 'abs') {
  2284. $percentage = $completeItems;
  2285. $text = '/' . $total_items;
  2286. }
  2287. return array(
  2288. $percentage,
  2289. $text
  2290. );
  2291. }
  2292. /**
  2293. * Gets the progress bar mode
  2294. * @return string The progress bar mode attribute
  2295. */
  2296. public function get_progress_bar_mode()
  2297. {
  2298. if ($this->debug > 0) {
  2299. error_log('New LP - In learnpath::get_progress_bar_mode()', 0);
  2300. }
  2301. if (!empty ($this->progress_bar_mode)) {
  2302. return $this->progress_bar_mode;
  2303. } else {
  2304. return '%';
  2305. }
  2306. }
  2307. /**
  2308. * Gets the learnpath proximity (remote or local)
  2309. * @return string Learnpath proximity
  2310. */
  2311. public function get_proximity()
  2312. {
  2313. if ($this->debug > 0) {
  2314. error_log('New LP - In learnpath::get_proximity()', 0);
  2315. }
  2316. if (!empty ($this->proximity)) {
  2317. return $this->proximity;
  2318. } else {
  2319. return '';
  2320. }
  2321. }
  2322. /**
  2323. * Gets the learnpath theme (remote or local)
  2324. * @return string Learnpath theme
  2325. */
  2326. public function get_theme()
  2327. {
  2328. if ($this->debug > 0) {
  2329. error_log('New LP - In learnpath::get_theme()', 0);
  2330. }
  2331. if (!empty ($this->theme)) {
  2332. return $this->theme;
  2333. } else {
  2334. return '';
  2335. }
  2336. }
  2337. /**
  2338. * Gets the learnpath session id
  2339. * @return string Learnpath theme
  2340. */
  2341. public function get_lp_session_id()
  2342. {
  2343. if ($this->debug > 0) {
  2344. error_log('New LP - In learnpath::get_lp_session_id()', 0);
  2345. }
  2346. if (!empty ($this->lp_session_id)) {
  2347. return $this->lp_session_id;
  2348. } else {
  2349. return 0;
  2350. }
  2351. }
  2352. /**
  2353. * Gets the learnpath image
  2354. * @return string Web URL of the LP image
  2355. */
  2356. public function get_preview_image()
  2357. {
  2358. if ($this->debug > 0) {
  2359. error_log('New LP - In learnpath::get_preview_image()', 0);
  2360. }
  2361. if (!empty($this->preview_image)) {
  2362. return $this->preview_image;
  2363. } else {
  2364. return '';
  2365. }
  2366. }
  2367. /**
  2368. * @param string $size
  2369. * @param string $path_type
  2370. * @return bool|string
  2371. */
  2372. public function get_preview_image_path($size = null, $path_type = 'web')
  2373. {
  2374. $preview_image = $this->get_preview_image();
  2375. if (isset($preview_image) && !empty($preview_image)) {
  2376. $image_sys_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2377. $image_path = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/upload/learning_path/images/';
  2378. if (isset($size)) {
  2379. $info = pathinfo($preview_image);
  2380. $image_custom_size = $info['filename'].'.'.$size.'.'.$info['extension'];
  2381. if (file_exists($image_sys_path.$image_custom_size)) {
  2382. if ($path_type == 'web') {
  2383. return $image_path.$image_custom_size;
  2384. } else {
  2385. return $image_sys_path.$image_custom_size;
  2386. }
  2387. }
  2388. } else {
  2389. if ($path_type == 'web') {
  2390. return $image_path.$preview_image;
  2391. } else {
  2392. return $image_sys_path.$preview_image;
  2393. }
  2394. }
  2395. }
  2396. return false;
  2397. }
  2398. /**
  2399. * Gets the learnpath author
  2400. * @return string LP's author
  2401. */
  2402. public function get_author()
  2403. {
  2404. if ($this->debug > 0) {
  2405. error_log('New LP - In learnpath::get_author()', 0);
  2406. }
  2407. if (!empty ($this->author)) {
  2408. return $this->author;
  2409. } else {
  2410. return '';
  2411. }
  2412. }
  2413. /**
  2414. * Gets the learnpath author
  2415. * @return string LP's author
  2416. */
  2417. public function get_hide_toc_frame()
  2418. {
  2419. if ($this->debug > 0) {
  2420. error_log('New LP - In learnpath::get_author()', 0);
  2421. }
  2422. if (!empty ($this->hide_toc_frame)) {
  2423. return $this->hide_toc_frame;
  2424. } else {
  2425. return '';
  2426. }
  2427. }
  2428. /**
  2429. * Generate a new prerequisites string for a given item. If this item was a sco and
  2430. * its prerequisites were strings (instead of IDs), then transform those strings into
  2431. * IDs, knowing that SCORM IDs are kept in the "ref" field of the lp_item table.
  2432. * Prefix all item IDs that end-up in the prerequisites string by "ITEM_" to use the
  2433. * same rule as the scorm_export() method
  2434. * @param integer Item ID
  2435. * @return string Prerequisites string ready for the export as SCORM
  2436. */
  2437. public function get_scorm_prereq_string($item_id)
  2438. {
  2439. if ($this->debug > 0) {
  2440. error_log('New LP - In learnpath::get_scorm_prereq_string()', 0);
  2441. }
  2442. if (!is_object($this->items[$item_id])) {
  2443. return false;
  2444. }
  2445. $oItem = $this->items[$item_id];
  2446. $prereq = $oItem->get_prereq_string();
  2447. if (empty($prereq)) {
  2448. return '';
  2449. }
  2450. if (preg_match('/^\d+$/', $prereq) && is_object($this->items[$prereq])) {
  2451. // If the prerequisite is a simple integer ID and this ID exists as an item ID,
  2452. // then simply return it (with the ITEM_ prefix).
  2453. //return 'ITEM_' . $prereq;
  2454. return $this->items[$prereq]->ref;
  2455. } else {
  2456. if (isset($this->refs_list[$prereq])) {
  2457. // It's a simple string item from which the ID can be found in the refs list,
  2458. // so we can transform it directly to an ID for export.
  2459. return $this->items[$this->refs_list[$prereq]]->ref;
  2460. } else if (isset($this->refs_list['ITEM_'.$prereq])) {
  2461. return $this->items[$this->refs_list['ITEM_'.$prereq]]->ref;
  2462. } else {
  2463. // The last case, if it's a complex form, then find all the IDs (SCORM strings)
  2464. // and replace them, one by one, by the internal IDs (chamilo db)
  2465. // TODO: Modify the '*' replacement to replace the multiplier in front of it
  2466. // by a space as well.
  2467. $find = array (
  2468. '&',
  2469. '|',
  2470. '~',
  2471. '=',
  2472. '<>',
  2473. '{',
  2474. '}',
  2475. '*',
  2476. '(',
  2477. ')'
  2478. );
  2479. $replace = array (
  2480. ' ',
  2481. ' ',
  2482. ' ',
  2483. ' ',
  2484. ' ',
  2485. ' ',
  2486. ' ',
  2487. ' ',
  2488. ' ',
  2489. ' '
  2490. );
  2491. $prereq_mod = str_replace($find, $replace, $prereq);
  2492. $ids = explode(' ', $prereq_mod);
  2493. foreach ($ids as $id) {
  2494. $id = trim($id);
  2495. if (isset ($this->refs_list[$id])) {
  2496. $prereq = preg_replace('/[^a-zA-Z_0-9](' . $id . ')[^a-zA-Z_0-9]/', 'ITEM_' . $this->refs_list[$id], $prereq);
  2497. }
  2498. }
  2499. error_log('New LP - In learnpath::get_scorm_prereq_string(): returning modified string: ' . $prereq, 0);
  2500. return $prereq;
  2501. }
  2502. }
  2503. }
  2504. /**
  2505. * Returns the XML DOM document's node
  2506. * @param resource Reference to a list of objects to search for the given ITEM_*
  2507. * @param string The identifier to look for
  2508. * @return mixed The reference to the element found with that identifier. False if not found
  2509. */
  2510. public function get_scorm_xml_node(& $children, $id)
  2511. {
  2512. for ($i = 0; $i < $children->length; $i++) {
  2513. $item_temp = $children->item($i);
  2514. if ($item_temp->nodeName == 'item') {
  2515. if ($item_temp->getAttribute('identifier') == $id) {
  2516. return $item_temp;
  2517. }
  2518. }
  2519. $subchildren = $item_temp->childNodes;
  2520. if ($subchildren->length > 0) {
  2521. $val = $this->get_scorm_xml_node($subchildren, $id);
  2522. if (is_object($val)) {
  2523. return $val;
  2524. }
  2525. }
  2526. }
  2527. return false;
  2528. }
  2529. /**
  2530. * Returns a usable array of stats related to the current learnpath and user
  2531. * @return array Well-formatted array containing status for the current learnpath
  2532. */
  2533. public function get_stats()
  2534. {
  2535. if ($this->debug > 0) {
  2536. error_log('New LP - In learnpath::get_stats()', 0);
  2537. }
  2538. }
  2539. /**
  2540. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2541. * the given course (for all learnpaths and all users)
  2542. * @param string Course code
  2543. * @return array Well-formatted array containing status for the course's learnpaths
  2544. */
  2545. public function get_stats_course($course)
  2546. {
  2547. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_course()', 0); }
  2548. // TODO
  2549. }
  2550. /**
  2551. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2552. * the given course and learnpath (for all users)
  2553. * @param string Course code
  2554. * @param integer Learnpath ID
  2555. * @return array Well-formatted array containing status for the specified learnpath
  2556. */
  2557. public function get_stats_lp($course, $lp)
  2558. {
  2559. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp()', 0); }
  2560. // TODO
  2561. }
  2562. /**
  2563. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2564. * the given course, learnpath and user.
  2565. * @param string Course code
  2566. * @param integer Learnpath ID
  2567. * @param integer User ID
  2568. * @return array Well-formatted array containing status for the specified learnpath and user
  2569. */
  2570. public function get_stats_lp_user($course, $lp, $user)
  2571. {
  2572. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_lp_user()', 0); }
  2573. // TODO
  2574. }
  2575. /**
  2576. * Static method. Can be re-implemented by children. Gives an array of statistics for
  2577. * the given course and learnpath (for all users)
  2578. * @param string Course code
  2579. * @param integer User ID
  2580. * @return array Well-formatted array containing status for the user's learnpaths
  2581. */
  2582. public function get_stats_user($course, $user) {
  2583. //if ($this->debug > 0) { error_log('New LP - In learnpath::get_stats_user()', 0); }
  2584. // TODO
  2585. }
  2586. /**
  2587. * Gets the status list for all LP's items
  2588. * @return array Array of [index] => [item ID => current status]
  2589. */
  2590. public function get_items_status_list()
  2591. {
  2592. if ($this->debug > 0) {
  2593. error_log('New LP - In learnpath::get_items_status_list()', 0);
  2594. }
  2595. $list = array ();
  2596. foreach ($this->ordered_items as $item_id) {
  2597. $list[] = array (
  2598. $item_id => $this->items[$item_id]->get_status()
  2599. );
  2600. }
  2601. return $list;
  2602. }
  2603. /**
  2604. * Return the number of interactions for the given learnpath Item View ID.
  2605. * This method can be used as static.
  2606. * @param integer Item View ID
  2607. * @param integer course id
  2608. * @return integer Number of interactions
  2609. */
  2610. public static function get_interactions_count_from_db($lp_iv_id, $course_id)
  2611. {
  2612. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2613. $lp_iv_id = intval($lp_iv_id);
  2614. $course_id = intval($course_id);
  2615. $sql = "SELECT count(*) FROM $table
  2616. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2617. $res = Database::query($sql);
  2618. $num = 0;
  2619. if (Database::num_rows($res)) {
  2620. $row = Database::fetch_array($res);
  2621. $num = $row[0];
  2622. }
  2623. return $num;
  2624. }
  2625. /**
  2626. * Return the interactions as an array for the given lp_iv_id.
  2627. * This method can be used as static.
  2628. * @param integer Learnpath Item View ID
  2629. * @return array
  2630. * @todo Transcode labels instead of switching to HTML (which requires to know the encoding of the LP)
  2631. */
  2632. public static function get_iv_interactions_array($lp_iv_id = 0)
  2633. {
  2634. $course_id = api_get_course_int_id();
  2635. $list = array ();
  2636. $table = Database :: get_course_table(TABLE_LP_IV_INTERACTION);
  2637. $sql = "SELECT * FROM $table WHERE c_id = ".$course_id." AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2638. $res = Database::query($sql);
  2639. $num = Database :: num_rows($res);
  2640. if ($num > 0) {
  2641. $list[] = array (
  2642. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2643. 'id' => api_htmlentities(get_lang('InteractionID'), ENT_QUOTES),
  2644. 'type' => api_htmlentities(get_lang('Type'), ENT_QUOTES),
  2645. 'time' => api_htmlentities(get_lang('TimeFinished'), ENT_QUOTES),
  2646. 'correct_responses' => api_htmlentities(get_lang('CorrectAnswers'), ENT_QUOTES),
  2647. 'student_response' => api_htmlentities(get_lang('StudentResponse'), ENT_QUOTES),
  2648. 'result' => api_htmlentities(get_lang('Result'), ENT_QUOTES),
  2649. 'latency' => api_htmlentities(get_lang('LatencyTimeSpent'), ENT_QUOTES)
  2650. );
  2651. while ($row = Database :: fetch_array($res)) {
  2652. $list[] = array (
  2653. 'order_id' => ($row['order_id'] + 1),
  2654. 'id' => urldecode($row['interaction_id']), //urldecode because they often have %2F or stuff like that
  2655. 'type' => $row['interaction_type'],
  2656. 'time' => $row['completion_time'],
  2657. //'correct_responses' => $row['correct_responses'],
  2658. 'correct_responses' => '', // Hide correct responses from students.
  2659. 'student_response' => $row['student_response'],
  2660. 'result' => $row['result'],
  2661. 'latency' => $row['latency']
  2662. );
  2663. }
  2664. }
  2665. return $list;
  2666. }
  2667. /**
  2668. * Return the number of objectives for the given learnpath Item View ID.
  2669. * This method can be used as static.
  2670. * @param integer Item View ID
  2671. * @return integer Number of objectives
  2672. */
  2673. public static function get_objectives_count_from_db($lp_iv_id, $course_id)
  2674. {
  2675. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2676. $course_id = intval($course_id);
  2677. $lp_iv_id = intval($lp_iv_id);
  2678. $sql = "SELECT count(*) FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id";
  2679. //@todo seems that this always returns 0
  2680. $res = Database::query($sql);
  2681. $num = 0;
  2682. if (Database::num_rows($res)) {
  2683. $row = Database :: fetch_array($res);
  2684. $num = $row[0];
  2685. }
  2686. return $num;
  2687. }
  2688. /**
  2689. * Return the objectives as an array for the given lp_iv_id.
  2690. * This method can be used as static.
  2691. * @param integer Learnpath Item View ID
  2692. * @return array
  2693. * @todo Translate labels
  2694. */
  2695. public static function get_iv_objectives_array($lp_iv_id = 0)
  2696. {
  2697. $course_id = api_get_course_int_id();
  2698. $table = Database :: get_course_table(TABLE_LP_IV_OBJECTIVE);
  2699. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ORDER BY order_id ASC";
  2700. $res = Database::query($sql);
  2701. $num = Database :: num_rows($res);
  2702. $list = array();
  2703. if ($num > 0) {
  2704. $list[] = array(
  2705. 'order_id' => api_htmlentities(get_lang('Order'), ENT_QUOTES),
  2706. 'objective_id' => api_htmlentities(get_lang('ObjectiveID'), ENT_QUOTES),
  2707. 'score_raw' => api_htmlentities(get_lang('ObjectiveRawScore'), ENT_QUOTES),
  2708. 'score_max' => api_htmlentities(get_lang('ObjectiveMaxScore'), ENT_QUOTES),
  2709. 'score_min' => api_htmlentities(get_lang('ObjectiveMinScore'), ENT_QUOTES),
  2710. 'status' => api_htmlentities(get_lang('ObjectiveStatus'), ENT_QUOTES)
  2711. );
  2712. while ($row = Database :: fetch_array($res)) {
  2713. $list[] = array (
  2714. 'order_id' => ($row['order_id'] + 1),
  2715. 'objective_id' => urldecode($row['objective_id']), // urldecode() because they often have %2F or stuff like that.
  2716. 'score_raw' => $row['score_raw'],
  2717. 'score_max' => $row['score_max'],
  2718. 'score_min' => $row['score_min'],
  2719. 'status' => $row['status']
  2720. );
  2721. }
  2722. }
  2723. return $list;
  2724. }
  2725. /**
  2726. * Generate and return the table of contents for this learnpath. The (flat) table returned can be
  2727. * used by get_html_toc() to be ready to display
  2728. * @return array TOC as a table with 4 elements per row: title, link, status and level
  2729. */
  2730. public function get_toc()
  2731. {
  2732. if ($this->debug > 0) {
  2733. error_log('learnpath::get_toc()', 0);
  2734. }
  2735. $toc = array();
  2736. foreach ($this->ordered_items as $item_id) {
  2737. if ($this->debug > 2) {
  2738. error_log('learnpath::get_toc(): getting info for item ' . $item_id, 0);
  2739. }
  2740. // TODO: Change this link generation and use new function instead.
  2741. $toc[] = array (
  2742. 'id' => $item_id,
  2743. 'title' => $this->items[$item_id]->get_title(),
  2744. 'status' => $this->items[$item_id]->get_status(),
  2745. 'level' => $this->items[$item_id]->get_level(),
  2746. 'type' => $this->items[$item_id]->get_type(),
  2747. 'description' => $this->items[$item_id]->get_description(),
  2748. 'path' => $this->items[$item_id]->get_path(),
  2749. );
  2750. }
  2751. if ($this->debug > 2) {
  2752. error_log('New LP - In learnpath::get_toc() - TOC array: ' . print_r($toc, true), 0);
  2753. }
  2754. return $toc;
  2755. }
  2756. /**
  2757. * Generate and return the table of contents for this learnpath. The JS
  2758. * table returned is used inside of scorm_api.php
  2759. * @return string A JS array vairiable construction
  2760. */
  2761. public function get_items_details_as_js($varname = 'olms.lms_item_types')
  2762. {
  2763. if ($this->debug > 0) {
  2764. error_log('New LP - In learnpath::get_items_details_as_js()', 0);
  2765. }
  2766. $toc = $varname.' = new Array();';
  2767. //echo "<pre>".print_r($this->items,true)."</pre>";
  2768. foreach ($this->ordered_items as $item_id) {
  2769. $toc.= $varname."['i$item_id'] = '".$this->items[$item_id]->get_type()."';";
  2770. }
  2771. if ($this->debug > 2) {
  2772. error_log('New LP - In learnpath::get_items_details_as_js() - TOC array: ' . print_r($toc, true), 0);
  2773. }
  2774. return $toc;
  2775. }
  2776. /**
  2777. * Gets the learning path type
  2778. * @param boolean Return the name? If false, return the ID. Default is false.
  2779. * @return mixed Type ID or name, depending on the parameter
  2780. */
  2781. public function get_type($get_name = false)
  2782. {
  2783. $res = false;
  2784. if ($this->debug > 0) {
  2785. error_log('New LP - In learnpath::get_type()', 0);
  2786. }
  2787. if (!empty ($this->type)) {
  2788. if ($get_name) {
  2789. // Get it from the lp_type table in main db.
  2790. } else {
  2791. $res = $this->type;
  2792. }
  2793. }
  2794. if ($this->debug > 2) {
  2795. error_log('New LP - In learnpath::get_type() - Returning ' . ($res ? $res : 'false'), 0);
  2796. }
  2797. return $res;
  2798. }
  2799. /**
  2800. * Gets the learning path type as static method
  2801. * @param boolean Return the name? If false, return the ID. Default is false.
  2802. * @return mixed Type ID or name, depending on the parameter
  2803. */
  2804. public static function get_type_static($lp_id = 0)
  2805. {
  2806. $course_id = api_get_course_int_id();
  2807. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  2808. $lp_id = intval($lp_id);
  2809. $sql = "SELECT lp_type FROM $tbl_lp WHERE c_id = $course_id AND id = '" . $lp_id . "'";
  2810. $res = Database::query($sql);
  2811. if ($res === false) {
  2812. return null;
  2813. }
  2814. if (Database :: num_rows($res) <= 0) {
  2815. return null;
  2816. }
  2817. $row = Database :: fetch_array($res);
  2818. return $row['lp_type'];
  2819. }
  2820. /**
  2821. * Gets a flat list of item IDs ordered for display (level by level ordered by order_display)
  2822. * This method can be used as abstract and is recursive
  2823. * @param integer Learnpath ID
  2824. * @param integer Parent ID of the items to look for
  2825. * @return mixed Ordered list of item IDs or false on error
  2826. */
  2827. public static function get_flat_ordered_items_list($lp, $parent = 0, $course_id = null)
  2828. {
  2829. if (empty($course_id)) {
  2830. $course_id = api_get_course_int_id();
  2831. } else {
  2832. $course_id = intval($course_id);
  2833. }
  2834. $list = array();
  2835. if (empty($lp)) {
  2836. return false;
  2837. }
  2838. $lp = intval($lp);
  2839. $parent = intval($parent);
  2840. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  2841. $sql = "SELECT id FROM $tbl_lp_item
  2842. WHERE c_id = $course_id AND lp_id = $lp AND parent_item_id = $parent
  2843. ORDER BY display_order";
  2844. $res = Database::query($sql);
  2845. while ($row = Database :: fetch_array($res)) {
  2846. $sublist = learnpath :: get_flat_ordered_items_list($lp, $row['id'], $course_id);
  2847. $list[] = $row['id'];
  2848. foreach ($sublist as $item) {
  2849. $list[] = $item;
  2850. }
  2851. }
  2852. return $list;
  2853. }
  2854. /**
  2855. * @return array
  2856. */
  2857. public static function getChapterTypes()
  2858. {
  2859. return array(
  2860. 'dokeos_chapter',
  2861. 'dokeos_module',
  2862. 'chapter',
  2863. 'dir'
  2864. );
  2865. }
  2866. /**
  2867. * Uses the table generated by get_toc() and returns an HTML-formatted string ready to display
  2868. * @return string HTML TOC ready to display
  2869. */
  2870. public function get_html_toc($toc_list = null)
  2871. {
  2872. global $_configuration;
  2873. $is_allowed_to_edit = api_is_allowed_to_edit(null, true, false, false);
  2874. if ($this->debug > 0) {
  2875. error_log('In learnpath::get_html_toc()', 0);
  2876. }
  2877. if (empty($toc_list)) {
  2878. $toc_list = $this->get_toc();
  2879. }
  2880. $html = '<div id="scorm_title" class="scorm_title">'.Security::remove_XSS($this->get_name()) . '</div>';
  2881. $hide_teacher_icons_lp = isset($_configuration['hide_teacher_icons_lp']) ? $_configuration['hide_teacher_icons_lp'] : true;
  2882. if ($is_allowed_to_edit && $hide_teacher_icons_lp == false) {
  2883. $gradebook = Security :: remove_XSS($_GET['gradebook']);
  2884. if ($this->get_lp_session_id() == api_get_session_id()) {
  2885. $html .= '<div id="actions_lp" class="actions_lp">';
  2886. $html .= '<div class="btn-group">';
  2887. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=build&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Overview') . "</a>";
  2888. $html .= "<a class='btn' href='lp_controller.php?" . api_get_cidreq()."&amp;action=add_item&amp;type=step&amp;lp_id=" . $this->lp_id . "' target='_parent'>" . get_lang('Edit') . "</a>";
  2889. $html .= '<a class="btn" href="lp_controller.php?'.api_get_cidreq()."&amp;gradebook=$gradebook&amp;action=edit&amp;lp_id=" . $this->lp_id.'">'.get_lang('Settings').'</a>';
  2890. $html .= '</div>';
  2891. $html .= '</div>';
  2892. }
  2893. }
  2894. $html .= '<div id="inner_lp_toc" class="inner_lp_toc">';
  2895. require_once 'resourcelinker.inc.php';
  2896. // Temporary variables.
  2897. $mycurrentitemid = $this->get_current_item_id();
  2898. $color_counter = 0;
  2899. $i = 0;
  2900. foreach ($toc_list as $item) {
  2901. // TODO: Complete this
  2902. $icon_name = array (
  2903. 'not attempted' => '../img/notattempted.gif',
  2904. 'incomplete' => '../img/incomplete.png',
  2905. 'failed' => '../img/delete.png',
  2906. 'completed' => '../img/completed.png',
  2907. 'passed' => '../img/passed.png',
  2908. 'succeeded' => '../img/succeeded.png',
  2909. 'browsed' => '../img/completed.png',
  2910. );
  2911. // Style Status
  2912. $class_name = array (
  2913. 'not attempted' => 'scorm_not_attempted',
  2914. 'incomplete' => 'scorm_not_attempted',
  2915. 'failed' => 'scorm_failed',
  2916. 'completed' => 'scorm_completed',
  2917. 'passed' => 'scorm_completed',
  2918. 'succeeded' => 'scorm_completed',
  2919. 'browsed' => 'scorm_completed',
  2920. );
  2921. $scorm_color_background = 'scorm_item_2';
  2922. $style_item = '';
  2923. if ($color_counter % 2 == 0) {
  2924. $scorm_color_background = 'scorm_item_1';
  2925. }
  2926. $dirTypes = self::getChapterTypes();
  2927. if (in_array($item['type'], $dirTypes)) {
  2928. $scorm_color_background =' scorm_item_section ';
  2929. $style_item = '';
  2930. }
  2931. if ($item['id'] == $this->current) {
  2932. $scorm_color_background = 'scorm_item_normal scorm_highlight '.$scorm_color_background.' ';
  2933. } elseif (!in_array($item['type'], $dirTypes)) {
  2934. $scorm_color_background = 'scorm_item_normal '.$scorm_color_background.' ';
  2935. }
  2936. $html .= '<div id="toc_' . $item['id'] . '" class="' . $scorm_color_background . ' '.$class_name[$item['status']].' ">';
  2937. // Learning path title
  2938. $title = $item['title'];
  2939. if (empty ($title)) {
  2940. $title = rl_get_resource_name(api_get_course_id(), $this->get_id(), $item['id']);
  2941. }
  2942. $title = Security::remove_XSS($title);
  2943. // Learning path personalization
  2944. // build the LP tree
  2945. // The anchor atoc_ will let us center the TOC on the currently viewed item &^D
  2946. $description = $item['description'];
  2947. if (empty($description)) {
  2948. $description = $title;
  2949. }
  2950. if (in_array($item['type'], $dirTypes)) {
  2951. // Chapters
  2952. $html .= '<div class="'.$style_item.' scorm_section_level_'.$item['level'].'" title="'.$description.'" >';
  2953. } else {
  2954. $html .= '<div class="'.$style_item.' scorm_item_level_'.$item['level'].' scorm_type_'.learnpath::format_scorm_type_item($item['type']).'" title="'.$description.'" >';
  2955. $html .= '<a name="atoc_'.$item['id'].'" />';
  2956. }
  2957. if (in_array($item['type'], $dirTypes)) {
  2958. // Chapter
  2959. // if you want to put an image before, you should use css
  2960. $html .= stripslashes($title);
  2961. } else {
  2962. $this->get_link('http', $item['id'], $toc_list);
  2963. $html .= '<a href="" onClick="switch_item(' .$mycurrentitemid . ',' .$item['id'] . ');' .'return false;" >' . stripslashes($title) . '</a>';
  2964. }
  2965. $html .= "</div>";
  2966. if ($scorm_color_background != '') {
  2967. $html .= '</div>';
  2968. }
  2969. $color_counter++;
  2970. }
  2971. $html .= "</div>";
  2972. return $html;
  2973. }
  2974. /**
  2975. * Gets the learnpath maker name - generally the editor's name
  2976. * @return string Learnpath maker name
  2977. */
  2978. public function get_maker()
  2979. {
  2980. if ($this->debug > 0) {
  2981. error_log('New LP - In learnpath::get_maker()', 0);
  2982. }
  2983. if (!empty ($this->maker)) {
  2984. return $this->maker;
  2985. } else {
  2986. return '';
  2987. }
  2988. }
  2989. /**
  2990. * Gets the user-friendly message stored in $this->message
  2991. * @return string Message
  2992. */
  2993. public function get_message()
  2994. {
  2995. if ($this->debug > 0) {
  2996. error_log('New LP - In learnpath::get_message()', 0);
  2997. }
  2998. return $this->message;
  2999. }
  3000. /**
  3001. * Gets the learnpath name/title
  3002. * @return string Learnpath name/title
  3003. */
  3004. public function get_name()
  3005. {
  3006. if ($this->debug > 0) {
  3007. error_log('New LP - In learnpath::get_name()', 0);
  3008. }
  3009. if (!empty ($this->name)) {
  3010. return $this->name;
  3011. } else {
  3012. return 'N/A';
  3013. }
  3014. }
  3015. /**
  3016. * Gets a link to the resource from the present location, depending on item ID.
  3017. * @param string $type Type of link expected
  3018. * @param integer $item_id Learnpath item ID
  3019. * @return string $provided_toc Link to the lp_item resource
  3020. */
  3021. public function get_link($type = 'http', $item_id = null, $provided_toc = false)
  3022. {
  3023. $course_id = $this->get_course_int_id();
  3024. if ($this->debug > 0) {
  3025. error_log('New LP - In learnpath::get_link(' . $type . ',' . $item_id . ')', 0);
  3026. }
  3027. if (empty($item_id)) {
  3028. if ($this->debug > 2) {
  3029. error_log('New LP - In learnpath::get_link() - no item id given in learnpath::get_link(), using current: ' . $this->get_current_item_id(), 0);
  3030. }
  3031. $item_id = $this->get_current_item_id();
  3032. }
  3033. if (empty($item_id)) {
  3034. if ($this->debug > 2) {
  3035. error_log('New LP - In learnpath::get_link() - no current item id found in learnpath object', 0);
  3036. }
  3037. //still empty, this means there was no item_id given and we are not in an object context or
  3038. //the object property is empty, return empty link
  3039. $item_id = $this->first();
  3040. return '';
  3041. }
  3042. $file = '';
  3043. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3044. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  3045. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3046. $item_id = intval($item_id);
  3047. $sql = "SELECT l.lp_type as ltype, l.path as lpath, li.item_type as litype, li.path as lipath, li.parameters as liparams
  3048. FROM $lp_table l
  3049. INNER JOIN $lp_item_table li
  3050. ON (li.lp_id = l.id AND l.c_id = $course_id AND li.c_id = $course_id )
  3051. WHERE li.id = $item_id ";
  3052. if ($this->debug > 2) {
  3053. error_log('New LP - In learnpath::get_link() - selecting item ' . $sql, 0);
  3054. }
  3055. $res = Database::query($sql);
  3056. if (Database :: num_rows($res) > 0) {
  3057. $row = Database :: fetch_array($res);
  3058. $lp_type = $row['ltype'];
  3059. $lp_path = $row['lpath'];
  3060. $lp_item_type = $row['litype'];
  3061. $lp_item_path = $row['lipath'];
  3062. $lp_item_params = $row['liparams'];
  3063. if (empty ($lp_item_params) && strpos($lp_item_path, '?') !== false) {
  3064. list ($lp_item_path, $lp_item_params) = explode('?', $lp_item_path);
  3065. }
  3066. $sys_course_path = api_get_path(SYS_COURSE_PATH) . api_get_course_path();
  3067. if ($type == 'http') {
  3068. $course_path = api_get_path(WEB_COURSE_PATH) . api_get_course_path(); //web path
  3069. } else {
  3070. $course_path = $sys_course_path; //system path
  3071. }
  3072. // 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.
  3073. if (in_array($lp_item_type, array('quiz', 'document', 'link', 'forum', 'thread', 'student_publication'))) {
  3074. $lp_type = 1;
  3075. }
  3076. if ($this->debug > 2) {
  3077. error_log('New LP - In learnpath::get_link() - $lp_type ' . $lp_type, 0);
  3078. error_log('New LP - In learnpath::get_link() - $lp_item_type ' . $lp_item_type, 0);
  3079. }
  3080. // Now go through the specific cases to get the end of the path
  3081. // @todo Use constants instead of int values.
  3082. switch ($lp_type) {
  3083. case 1 :
  3084. if ($lp_item_type == 'dokeos_chapter') {
  3085. $file = 'lp_content.php?type=dir';
  3086. } else {
  3087. require_once 'resourcelinker.inc.php';
  3088. $file = rl_get_resource_link_for_learnpath(
  3089. $course_id,
  3090. $this->get_id(),
  3091. $item_id,
  3092. $this->get_view_id()
  3093. );
  3094. if ($this->debug > 0) {
  3095. error_log('rl_get_resource_link_for_learnpath - file: ' . $file, 0);
  3096. }
  3097. if ($lp_item_type == 'link') {
  3098. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  3099. if (is_youtube_link($file)) {
  3100. $src = get_youtube_video_id($file);
  3101. $file = 'embed.php?type=youtube&src='.$src;
  3102. }
  3103. if (isVimeoLink($file)) {
  3104. $src = getVimeoLinkId($file);
  3105. $file = 'embed.php?type=vimeo&src='.$src;
  3106. }
  3107. } else {
  3108. // Check how much attempts of a exercise exits in lp
  3109. $lp_item_id = $this->get_current_item_id();
  3110. $lp_view_id = $this->get_view_id();
  3111. $prevent_reinit = null;
  3112. if (isset($this->items[$this->current])) {
  3113. $prevent_reinit = $this->items[$this->current]->get_prevent_reinit();
  3114. }
  3115. if (empty($provided_toc)) {
  3116. if ($this->debug > 0) {
  3117. error_log('In learnpath::get_link() Loading get_toc ', 0);
  3118. }
  3119. $list = $this->get_toc();
  3120. } else {
  3121. if ($this->debug > 0) {
  3122. error_log('In learnpath::get_link() Loading get_toc from "cache" ', 0);
  3123. }
  3124. $list = $provided_toc;
  3125. }
  3126. $type_quiz = false;
  3127. foreach ($list as $toc) {
  3128. if ($toc['id'] == $lp_item_id && ($toc['type'] == 'quiz')) {
  3129. $type_quiz = true;
  3130. }
  3131. }
  3132. if ($type_quiz) {
  3133. $lp_item_id = intval($lp_item_id);
  3134. $lp_view_id = intval($lp_view_id);
  3135. $sql = "SELECT count(*) FROM $lp_item_view_table
  3136. WHERE
  3137. c_id = $course_id AND
  3138. lp_item_id='" . $lp_item_id . "' AND
  3139. lp_view_id ='" . $lp_view_id . "' AND
  3140. status='completed'";
  3141. $result = Database::query($sql);
  3142. $row_count = Database :: fetch_row($result);
  3143. $count_item_view = (int) $row_count[0];
  3144. $not_multiple_attempt = 0;
  3145. if ($prevent_reinit === 1 && $count_item_view > 0) {
  3146. $not_multiple_attempt = 1;
  3147. }
  3148. $file .= '&not_multiple_attempt=' . $not_multiple_attempt;
  3149. }
  3150. $tmp_array = explode('/', $file);
  3151. $document_name = $tmp_array[count($tmp_array) - 1];
  3152. if (strpos($document_name, '_DELETED_')) {
  3153. $file = 'blank.php?error=document_deleted';
  3154. }
  3155. }
  3156. }
  3157. break;
  3158. case 2 :
  3159. if ($this->debug > 2) {
  3160. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3161. }
  3162. if ($lp_item_type != 'dir') {
  3163. // Quite complex here:
  3164. // We want to make sure 'http://' (and similar) links can
  3165. // be loaded as is (withouth the Chamilo path in front) but
  3166. // some contents use this form: resource.htm?resource=http://blablabla
  3167. // which means we have to find a protocol at the path's start, otherwise
  3168. // it should not be considered as an external URL.
  3169. //if ($this->prerequisites_match($item_id)) {
  3170. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3171. if ($this->debug > 2) {
  3172. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3173. }
  3174. // Distant url, return as is.
  3175. $file = $lp_item_path;
  3176. } else {
  3177. if ($this->debug > 2) {
  3178. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3179. }
  3180. // Prevent getting untranslatable urls.
  3181. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3182. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3183. // Prepare the path.
  3184. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3185. // TODO: Fix this for urls with protocol header.
  3186. $file = str_replace('//', '/', $file);
  3187. $file = str_replace(':/', '://', $file);
  3188. if (substr($lp_path, -1) == '/') {
  3189. $lp_path = substr($lp_path, 0, -1);
  3190. }
  3191. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $lp_item_path))) {
  3192. // if file not found.
  3193. $decoded = html_entity_decode($lp_item_path);
  3194. list ($decoded) = explode('?', $decoded);
  3195. if (!is_file(realpath($sys_course_path . '/scorm/' . $lp_path . '/' . $decoded))) {
  3196. require_once 'resourcelinker.inc.php';
  3197. $file = rl_get_resource_link_for_learnpath(
  3198. $course_id,
  3199. $this->get_id(),
  3200. $item_id,
  3201. $this->get_view_id()
  3202. );
  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. // We want to use parameters if they were defined in the imsmanifest
  3220. if (strpos($file, 'blank.php') === false) {
  3221. $file .= (strstr($file, '?') === false ? '?' : '') . $lp_item_params;
  3222. }
  3223. } else {
  3224. $file = 'lp_content.php?type=dir';
  3225. }
  3226. break;
  3227. case 3 :
  3228. if ($this->debug > 2) {
  3229. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Item type: ' . $lp_item_type, 0);
  3230. }
  3231. // Formatting AICC HACP append URL.
  3232. $aicc_append = '?aicc_sid=' . urlencode(session_id()) . '&aicc_url=' . urlencode(api_get_path(WEB_CODE_PATH) . 'newscorm/aicc_hacp.php') . '&';
  3233. if ($lp_item_type != 'dir') {
  3234. // Quite complex here:
  3235. // We want to make sure 'http://' (and similar) links can
  3236. // be loaded as is (withouth the Chamilo path in front) but
  3237. // some contents use this form: resource.htm?resource=http://blablabla
  3238. // which means we have to find a protocol at the path's start, otherwise
  3239. // it should not be considered as an external URL.
  3240. if (preg_match('#^[a-zA-Z]{2,5}://#', $lp_item_path) != 0) {
  3241. if ($this->debug > 2) {
  3242. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - Found match for protocol in ' . $lp_item_path, 0);
  3243. }
  3244. // Distant url, return as is.
  3245. $file = $lp_item_path;
  3246. // Enabled and modified by Ivan Tcholakov, 16-OCT-2008.
  3247. /*
  3248. if (stristr($file,'<servername>') !== false) {
  3249. $file = str_replace('<servername>', $course_path.'/scorm/'.$lp_path.'/', $lp_item_path);
  3250. }
  3251. */
  3252. if (stripos($file, '<servername>') !== false) {
  3253. //$file = str_replace('<servername>',$course_path.'/scorm/'.$lp_path.'/',$lp_item_path);
  3254. $web_course_path = str_replace('https://', '', str_replace('http://', '', $course_path));
  3255. $file = str_replace('<servername>', $web_course_path . '/scorm/' . $lp_path, $lp_item_path);
  3256. }
  3257. //
  3258. $file .= $aicc_append;
  3259. } else {
  3260. if ($this->debug > 2) {
  3261. error_log('New LP - In learnpath::get_link() ' . __LINE__ . ' - No starting protocol in ' . $lp_item_path, 0);
  3262. }
  3263. // Prevent getting untranslatable urls.
  3264. $lp_item_path = preg_replace('/%2F/', '/', $lp_item_path);
  3265. $lp_item_path = preg_replace('/%3A/', ':', $lp_item_path);
  3266. // Prepare the path - lp_path might be unusable because it includes the "aicc" subdir name.
  3267. $file = $course_path . '/scorm/' . $lp_path . '/' . $lp_item_path;
  3268. // TODO: Fix this for urls with protocol header.
  3269. $file = str_replace('//', '/', $file);
  3270. $file = str_replace(':/', '://', $file);
  3271. $file .= $aicc_append;
  3272. }
  3273. } else {
  3274. $file = 'lp_content.php?type=dir';
  3275. }
  3276. break;
  3277. case 4 :
  3278. break;
  3279. default :
  3280. break;
  3281. }
  3282. // Replace &amp; by & because &amp; will break URL with params
  3283. $file = !empty($file) ? str_replace('&amp;', '&', $file) : '';
  3284. }
  3285. if ($this->debug > 2) {
  3286. error_log('New LP - In learnpath::get_link() - returning "' . $file . '" from get_link', 0);
  3287. }
  3288. return $file;
  3289. }
  3290. /**
  3291. * Gets the latest usable view or generate a new one
  3292. * @param integer Optional attempt number. If none given, takes the highest from the lp_view table
  3293. * @return integer DB lp_view id
  3294. */
  3295. public function get_view($attempt_num = 0)
  3296. {
  3297. if ($this->debug > 0) {
  3298. error_log('New LP - In learnpath::get_view()', 0);
  3299. }
  3300. $search = '';
  3301. // Use $attempt_num to enable multi-views management (disabled so far).
  3302. if ($attempt_num != 0 AND intval(strval($attempt_num)) == $attempt_num) {
  3303. $search = 'AND view_count = ' . $attempt_num;
  3304. }
  3305. // When missing $attempt_num, search for a unique lp_view record for this lp and user.
  3306. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3307. $course_id = api_get_course_int_id();
  3308. $sessionId = api_get_session_id();
  3309. $sql = "SELECT id, view_count FROM $lp_view_table
  3310. WHERE
  3311. c_id = " . $course_id . " AND
  3312. lp_id = " . $this->get_id() . " AND
  3313. user_id = " . $this->get_user_id() . " AND
  3314. session_id = $sessionId
  3315. $search
  3316. ORDER BY view_count DESC";
  3317. $res = Database::query($sql);
  3318. if (Database :: num_rows($res) > 0) {
  3319. $row = Database :: fetch_array($res);
  3320. $this->lp_view_id = $row['id'];
  3321. } else {
  3322. // There is no database record, create one.
  3323. $sql = "INSERT INTO $lp_view_table (c_id, lp_id,user_id, view_count, session_id) VALUES
  3324. ($course_id, " . $this->get_id() . "," . $this->get_user_id() . ", 1, $sessionId)";
  3325. Database::query($sql);
  3326. $id = Database :: insert_id();
  3327. $this->lp_view_id = $id;
  3328. }
  3329. return $this->lp_view_id;
  3330. }
  3331. /**
  3332. * Gets the current view id
  3333. * @return integer View ID (from lp_view)
  3334. */
  3335. public function get_view_id()
  3336. {
  3337. if ($this->debug > 0) {
  3338. error_log('New LP - In learnpath::get_view_id()', 0);
  3339. }
  3340. if (!empty ($this->lp_view_id)) {
  3341. return $this->lp_view_id;
  3342. } else {
  3343. return 0;
  3344. }
  3345. }
  3346. /**
  3347. * Gets the update queue
  3348. * @return array Array containing IDs of items to be updated by JavaScript
  3349. */
  3350. public function get_update_queue()
  3351. {
  3352. if ($this->debug > 0) {
  3353. error_log('New LP - In learnpath::get_update_queue()', 0);
  3354. }
  3355. return $this->update_queue;
  3356. }
  3357. /**
  3358. * Gets the user ID
  3359. * @return integer User ID
  3360. */
  3361. public function get_user_id()
  3362. {
  3363. if ($this->debug > 0) {
  3364. error_log('New LP - In learnpath::get_user_id()', 0);
  3365. }
  3366. if (!empty ($this->user_id)) {
  3367. return $this->user_id;
  3368. } else {
  3369. return false;
  3370. }
  3371. }
  3372. /**
  3373. * Checks if any of the items has an audio element attached
  3374. * @return bool True or false
  3375. */
  3376. public function has_audio()
  3377. {
  3378. if ($this->debug > 1) {
  3379. error_log('New LP - In learnpath::has_audio()', 0);
  3380. }
  3381. $has = false;
  3382. foreach ($this->items as $i => $item) {
  3383. if (!empty ($this->items[$i]->audio)) {
  3384. $has = true;
  3385. break;
  3386. }
  3387. }
  3388. return $has;
  3389. }
  3390. /**
  3391. * Logs a message into a file
  3392. * @param string Message to log
  3393. * @return boolean True on success, false on error or if msg empty
  3394. */
  3395. public function log($msg)
  3396. {
  3397. if ($this->debug > 0) {
  3398. error_log('New LP - In learnpath::log()', 0);
  3399. }
  3400. // TODO
  3401. $this->error .= $msg;
  3402. return true;
  3403. }
  3404. /**
  3405. * Moves an item up and down at its level
  3406. * @param integer Item to move up and down
  3407. * @param string Direction 'up' or 'down'
  3408. * @return integer New display order, or false on error
  3409. */
  3410. public function move_item($id, $direction)
  3411. {
  3412. $course_id = api_get_course_int_id();
  3413. if ($this->debug > 0) {
  3414. error_log('New LP - In learnpath::move_item(' . $id . ',' . $direction . ')', 0);
  3415. }
  3416. if (empty ($id) or empty ($direction)) {
  3417. return false;
  3418. }
  3419. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  3420. $sql_sel = "SELECT *
  3421. FROM " . $tbl_lp_item . "
  3422. WHERE c_id = ".$course_id." AND id = " . $id;
  3423. $res_sel = Database::query($sql_sel);
  3424. // Check if elem exists.
  3425. if (Database :: num_rows($res_sel) < 1) {
  3426. return false;
  3427. }
  3428. // Gather data.
  3429. $row = Database :: fetch_array($res_sel);
  3430. $previous = $row['previous_item_id'];
  3431. $next = $row['next_item_id'];
  3432. $display = $row['display_order'];
  3433. $parent = $row['parent_item_id'];
  3434. $lp = $row['lp_id'];
  3435. // Update the item (switch with previous/next one).
  3436. switch ($direction) {
  3437. case 'up' :
  3438. if ($this->debug > 2) {
  3439. error_log('Movement up detected', 0);
  3440. }
  3441. if ($display <= 1) { /*do nothing*/
  3442. } else {
  3443. $sql_sel2 = "SELECT * FROM $tbl_lp_item
  3444. WHERE c_id = ".$course_id." AND id = $previous";
  3445. if ($this->debug > 2) {
  3446. error_log('Selecting previous: ' . $sql_sel2, 0);
  3447. }
  3448. $res_sel2 = Database::query($sql_sel2);
  3449. if (Database :: num_rows($res_sel2) < 1) {
  3450. $previous_previous = 0;
  3451. }
  3452. // Gather data.
  3453. $row2 = Database :: fetch_array($res_sel2);
  3454. $previous_previous = $row2['previous_item_id'];
  3455. // Update previous_previous item (switch "next" with current).
  3456. if ($previous_previous != 0) {
  3457. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3458. next_item_id = $id
  3459. WHERE c_id = ".$course_id." AND id = $previous_previous";
  3460. if ($this->debug > 2) {
  3461. error_log($sql_upd2, 0);
  3462. }
  3463. Database::query($sql_upd2);
  3464. }
  3465. // Update previous item (switch with current).
  3466. if ($previous != 0) {
  3467. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3468. next_item_id = $next,
  3469. previous_item_id = $id,
  3470. display_order = display_order +1
  3471. WHERE c_id = ".$course_id." AND id = $previous";
  3472. if ($this->debug > 2) {
  3473. error_log($sql_upd2, 0);
  3474. }
  3475. Database::query($sql_upd2);
  3476. }
  3477. // Update current item (switch with previous).
  3478. if ($id != 0) {
  3479. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3480. next_item_id = $previous,
  3481. previous_item_id = $previous_previous,
  3482. display_order = display_order-1
  3483. WHERE c_id = ".$course_id." AND id = $id";
  3484. if ($this->debug > 2) {
  3485. error_log($sql_upd2, 0);
  3486. }
  3487. Database::query($sql_upd2);
  3488. }
  3489. // Update next item (new previous item).
  3490. if ($next != 0) {
  3491. $sql_upd2 = "UPDATE $tbl_lp_item SET previous_item_id = $previous
  3492. WHERE c_id = ".$course_id." AND id = $next";
  3493. if ($this->debug > 2) {
  3494. error_log($sql_upd2, 0);
  3495. }
  3496. Database::query($sql_upd2);
  3497. }
  3498. $display = $display -1;
  3499. }
  3500. break;
  3501. case 'down' :
  3502. if ($this->debug > 2) {
  3503. error_log('Movement down detected', 0);
  3504. }
  3505. if ($next == 0) { /* Do nothing. */
  3506. } else {
  3507. $sql_sel2 = "SELECT * FROM $tbl_lp_item WHERE c_id = ".$course_id." AND id = $next";
  3508. if ($this->debug > 2) {
  3509. error_log('Selecting next: ' . $sql_sel2, 0);
  3510. }
  3511. $res_sel2 = Database::query($sql_sel2);
  3512. if (Database :: num_rows($res_sel2) < 1) {
  3513. $next_next = 0;
  3514. }
  3515. // Gather data.
  3516. $row2 = Database :: fetch_array($res_sel2);
  3517. $next_next = $row2['next_item_id'];
  3518. // Update previous item (switch with current).
  3519. if ($previous != 0) {
  3520. $sql_upd2 = "UPDATE $tbl_lp_item SET next_item_id = $next
  3521. WHERE c_id = ".$course_id." AND id = $previous";
  3522. Database::query($sql_upd2);
  3523. }
  3524. // Update current item (switch with previous).
  3525. if ($id != 0) {
  3526. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3527. previous_item_id = $next, next_item_id = $next_next, display_order = display_order+1
  3528. WHERE c_id = ".$course_id." AND id = $id";
  3529. Database::query($sql_upd2);
  3530. }
  3531. // Update next item (new previous item).
  3532. if ($next != 0) {
  3533. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3534. previous_item_id = $previous, next_item_id = $id, display_order = display_order-1
  3535. WHERE c_id = ".$course_id." AND id = $next";
  3536. Database::query($sql_upd2);
  3537. }
  3538. // Update next_next item (switch "previous" with current).
  3539. if ($next_next != 0) {
  3540. $sql_upd2 = "UPDATE $tbl_lp_item SET
  3541. previous_item_id = $id
  3542. WHERE c_id = ".$course_id." AND id = $next_next";
  3543. Database::query($sql_upd2);
  3544. }
  3545. $display = $display +1;
  3546. }
  3547. break;
  3548. default :
  3549. return false;
  3550. }
  3551. return $display;
  3552. }
  3553. /**
  3554. * Move a learnpath up (display_order)
  3555. * @param integer Learnpath ID
  3556. */
  3557. public function move_up($lp_id)
  3558. {
  3559. $course_id = api_get_course_int_id();
  3560. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3561. $sql = "SELECT * FROM $lp_table
  3562. WHERE c_id = ".$course_id."
  3563. ORDER BY display_order";
  3564. $res = Database::query($sql);
  3565. if ($res === false)
  3566. return false;
  3567. $lps = array ();
  3568. $lp_order = array ();
  3569. $num = Database :: num_rows($res);
  3570. // First check the order is correct, globally (might be wrong because
  3571. // of versions < 1.8.4)
  3572. if ($num > 0) {
  3573. $i = 1;
  3574. while ($row = Database :: fetch_array($res)) {
  3575. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3576. $need_fix = true;
  3577. $sql_u = "UPDATE $lp_table SET display_order = $i
  3578. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3579. Database::query($sql_u);
  3580. }
  3581. $row['display_order'] = $i;
  3582. $lps[$row['id']] = $row;
  3583. $lp_order[$i] = $row['id'];
  3584. $i++;
  3585. }
  3586. }
  3587. if ($num > 1) { // If there's only one element, no need to sort.
  3588. $order = $lps[$lp_id]['display_order'];
  3589. if ($order > 1) { // If it's the first element, no need to move up.
  3590. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3591. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order - 1];
  3592. Database::query($sql_u1);
  3593. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order - 1) . "
  3594. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3595. Database::query($sql_u2);
  3596. }
  3597. }
  3598. }
  3599. /**
  3600. * Move a learnpath down (display_order)
  3601. * @param integer Learnpath ID
  3602. */
  3603. public function move_down($lp_id)
  3604. {
  3605. $course_id = api_get_course_int_id();
  3606. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  3607. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  3608. $res = Database::query($sql);
  3609. if ($res === false)
  3610. return false;
  3611. $lps = array ();
  3612. $lp_order = array ();
  3613. $num = Database :: num_rows($res);
  3614. $max = 0;
  3615. // First check the order is correct, globally (might be wrong because
  3616. // of versions < 1.8.4).
  3617. if ($num > 0) {
  3618. $i = 1;
  3619. while ($row = Database :: fetch_array($res)) {
  3620. $max = $i;
  3621. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  3622. $need_fix = true;
  3623. $sql_u = "UPDATE $lp_table SET display_order = $i
  3624. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  3625. $res_u = Database::query($sql_u);
  3626. }
  3627. $row['display_order'] = $i;
  3628. $lps[$row['id']] = $row;
  3629. $lp_order[$i] = $row['id'];
  3630. $i++;
  3631. }
  3632. }
  3633. if ($num > 1) { // If there's only one element, no need to sort.
  3634. $order = $lps[$lp_id]['display_order'];
  3635. if ($order < $max) { // If it's the first element, no need to move up.
  3636. $sql_u1 = "UPDATE $lp_table SET display_order = $order
  3637. WHERE c_id = ".$course_id." AND id = " . $lp_order[$order + 1];
  3638. Database::query($sql_u1);
  3639. $sql_u2 = "UPDATE $lp_table SET display_order = " . ($order + 1) . "
  3640. WHERE c_id = ".$course_id." AND id = " . $lp_id;
  3641. Database::query($sql_u2);
  3642. }
  3643. }
  3644. }
  3645. /**
  3646. * Updates learnpath attributes to point to the next element
  3647. * The last part is similar to set_current_item but processing the other way around
  3648. */
  3649. public function next()
  3650. {
  3651. if ($this->debug > 0) {
  3652. error_log('New LP - In learnpath::next()', 0);
  3653. }
  3654. $this->last = $this->get_current_item_id();
  3655. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3656. $this->autocomplete_parents($this->last);
  3657. $new_index = $this->get_next_index();
  3658. if ($this->debug > 2) {
  3659. error_log('New LP - New index: ' . $new_index, 0);
  3660. }
  3661. $this->index = $new_index;
  3662. if ($this->debug > 2) {
  3663. error_log('New LP - Now having orderedlist[' . $new_index . '] = ' . $this->ordered_items[$new_index], 0);
  3664. }
  3665. $this->current = $this->ordered_items[$new_index];
  3666. if ($this->debug > 2) {
  3667. error_log('New LP - new item id is ' . $this->current . '-' . $this->get_current_item_id(), 0);
  3668. }
  3669. }
  3670. /**
  3671. * Open a resource = initialise all local variables relative to this resource. Depending on the child
  3672. * class, this might be redefined to allow several behaviours depending on the document type.
  3673. * @param integer Resource ID
  3674. * @return boolean True on success, false otherwise
  3675. */
  3676. public function open($id)
  3677. {
  3678. if ($this->debug > 0) {
  3679. error_log('New LP - In learnpath::open()', 0);
  3680. }
  3681. // TODO:
  3682. // set the current resource attribute to this resource
  3683. // switch on element type (redefine in child class?)
  3684. // set status for this item to "opened"
  3685. // start timer
  3686. // initialise score
  3687. $this->index = 0; //or = the last item seen (see $this->last)
  3688. }
  3689. /**
  3690. * Check that all prerequisites are fulfilled. Returns true and an
  3691. * empty string on succes, returns false
  3692. * and the prerequisite string on error.
  3693. * This function is based on the rules for aicc_script language as
  3694. * described in the SCORM 1.2 CAM documentation page 108.
  3695. * @param integer $itemId Optional item ID. If none given, uses the current open item.
  3696. * @return boolean True if prerequisites are matched, false otherwise -
  3697. * Empty string if true returned, prerequisites string otherwise.
  3698. */
  3699. public function prerequisites_match($itemId = null)
  3700. {
  3701. $debug = $this->debug;
  3702. if ($debug > 0) {
  3703. error_log('In learnpath::prerequisites_match()', 0);
  3704. }
  3705. if (empty($itemId)) {
  3706. $itemId = $this->current;
  3707. }
  3708. $currentItem = $this->getItem($itemId);
  3709. if ($currentItem) {
  3710. if ($this->type == 2) {
  3711. // Getting prereq from scorm
  3712. $prereq_string = $this->get_scorm_prereq_string($itemId);
  3713. } else {
  3714. $prereq_string = $currentItem->get_prereq_string();
  3715. }
  3716. if (empty($prereq_string)) {
  3717. if ($debug > 0) {
  3718. error_log('Found prereq_string is empty return true');
  3719. }
  3720. return true;
  3721. }
  3722. // Clean spaces.
  3723. $prereq_string = str_replace(' ', '', $prereq_string);
  3724. if ($debug > 0) {
  3725. error_log('Found prereq_string: ' . $prereq_string, 0);
  3726. }
  3727. // Now send to the parse_prereq() function that will check this component's prerequisites.
  3728. $result = $currentItem->parse_prereq(
  3729. $prereq_string,
  3730. $this->items,
  3731. $this->refs_list,
  3732. $this->get_user_id()
  3733. );
  3734. if ($result === false) {
  3735. $this->set_error_msg($currentItem->prereq_alert);
  3736. }
  3737. } else {
  3738. $result = true;
  3739. if ($debug > 1) {
  3740. error_log('$this->items[' . $itemId . '] was not an object', 0);
  3741. }
  3742. }
  3743. if ($debug > 1) {
  3744. error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
  3745. }
  3746. return $result;
  3747. }
  3748. /**
  3749. * Updates learnpath attributes to point to the previous element
  3750. * The last part is similar to set_current_item but processing the other way around
  3751. */
  3752. public function previous()
  3753. {
  3754. if ($this->debug > 0) {
  3755. error_log('New LP - In learnpath::previous()', 0);
  3756. }
  3757. $this->last = $this->get_current_item_id();
  3758. $this->items[$this->last]->save(false, $this->prerequisites_match($this->last));
  3759. $this->autocomplete_parents($this->last);
  3760. $new_index = $this->get_previous_index();
  3761. $this->index = $new_index;
  3762. $this->current = $this->ordered_items[$new_index];
  3763. }
  3764. /**
  3765. * Publishes a learnpath. This basically means show or hide the learnpath
  3766. * to normal users.
  3767. * Can be used as abstract
  3768. * @param integer Learnpath ID
  3769. * @param string New visibility
  3770. */
  3771. public function toggle_visibility($lp_id, $set_visibility = 1)
  3772. {
  3773. $action = 'visible';
  3774. if ($set_visibility != 1) {
  3775. $action = 'invisible';
  3776. self::toggle_publish($lp_id, 'i');
  3777. }
  3778. return api_item_property_update(
  3779. api_get_course_info(),
  3780. TOOL_LEARNPATH,
  3781. $lp_id,
  3782. $action,
  3783. api_get_user_id()
  3784. );
  3785. }
  3786. /**
  3787. * Publishes a learnpath. This basically means show or hide the learnpath
  3788. * on the course homepage
  3789. * Can be used as abstract
  3790. * @param integer $lp_id Learnpath id
  3791. * @param string $set_visibility New visibility (v/i - visible/invisible)
  3792. * @return bool
  3793. */
  3794. public static function toggle_publish($lp_id, $set_visibility = 'v')
  3795. {
  3796. $course_id = api_get_course_int_id();
  3797. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  3798. $lp_id = intval($lp_id);
  3799. $sql = "SELECT * FROM $tbl_lp where c_id = ".$course_id." AND id=$lp_id";
  3800. $result = Database::query($sql);
  3801. if (Database::num_rows($result)) {
  3802. $row = Database :: fetch_array($result);
  3803. $name = domesticate($row['name']);
  3804. if ($set_visibility == 'i') {
  3805. $s = $name . " " . get_lang('LearnpathNotPublished');
  3806. $dialogBox = $s;
  3807. $v = 0;
  3808. }
  3809. if ($set_visibility == 'v') {
  3810. $s = $name . " " . get_lang('LearnpathPublished');
  3811. $dialogBox = $s;
  3812. $v = 1;
  3813. }
  3814. } else {
  3815. return false;
  3816. }
  3817. $session_id = api_get_session_id();
  3818. $session_condition = api_get_session_condition($session_id);
  3819. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  3820. $link = 'newscorm/lp_controller.php?action=view&lp_id='.$lp_id.'&id_session='.$session_id;
  3821. $sql = "SELECT * FROM $tbl_tool
  3822. WHERE
  3823. c_id = ".$course_id." AND
  3824. link='$link' and
  3825. image='scormbuilder.gif' and
  3826. link LIKE '$link%'
  3827. $session_condition
  3828. ";
  3829. $result = Database::query($sql);
  3830. $num = Database :: num_rows($result);
  3831. //if ($this->debug > 2) { error_log('New LP - '.$sql.' - '.$num, 0); }
  3832. if (($set_visibility == 'i') && ($num > 0)) {
  3833. $sql = "DELETE FROM $tbl_tool WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3834. } elseif (($set_visibility == 'v') && ($num == 0)) {
  3835. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id) VALUES
  3836. ($course_id, '$name', '$link', 'scormbuilder.gif', '$v', '0','pastillegris.gif', 0, $session_id)";
  3837. } elseif (($set_visibility == 'v') && ($num > 0)) {
  3838. $sql = "UPDATE $tbl_tool SET
  3839. c_id = $course_id,
  3840. name = '$name',
  3841. link = '$link',
  3842. image = 'scormbuilder.gif',
  3843. visibility = '$v',
  3844. admin = '0',
  3845. address = 'pastillegris.gif',
  3846. added_tool = 0,
  3847. session_id = $session_id
  3848. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  3849. } else {
  3850. // Parameter and database incompatible, do nothing, exit.
  3851. return false;
  3852. }
  3853. Database::query($sql);
  3854. }
  3855. /**
  3856. * Restart the whole learnpath. Return the URL of the first element.
  3857. * Make sure the results are saved with anoter method. This method should probably be
  3858. * redefined in children classes.
  3859. * To use a similar method statically, use the create_new_attempt() method
  3860. * @return string URL to load in the viewer
  3861. */
  3862. public function restart()
  3863. {
  3864. if ($this->debug > 0) {
  3865. error_log('New LP - In learnpath::restart()', 0);
  3866. }
  3867. // TODO
  3868. // Call autosave method to save the current progress.
  3869. //$this->index = 0;
  3870. $session_id = api_get_session_id();
  3871. $course_id = api_get_course_int_id();
  3872. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  3873. $sql = "INSERT INTO $lp_view_table (c_id, lp_id, user_id, view_count, session_id) " .
  3874. "VALUES ($course_id, " . $this->lp_id . "," . $this->get_user_id() . "," . ($this->attempt + 1) . ", $session_id)";
  3875. if ($this->debug > 2) {
  3876. error_log('New LP - Inserting new lp_view for restart: ' . $sql, 0);
  3877. }
  3878. $res = Database::query($sql);
  3879. if ($view_id = Database :: insert_id($res)) {
  3880. $this->lp_view_id = $view_id;
  3881. $this->attempt = $this->attempt + 1;
  3882. } else {
  3883. $this->error = 'Could not insert into item_view table...';
  3884. return false;
  3885. }
  3886. $this->autocomplete_parents($this->current);
  3887. foreach ($this->items as $index => $dummy) {
  3888. $this->items[$index]->restart();
  3889. $this->items[$index]->set_lp_view($this->lp_view_id);
  3890. }
  3891. $this->first();
  3892. return true;
  3893. }
  3894. /**
  3895. * Saves the current item
  3896. * @return boolean
  3897. */
  3898. public function save_current()
  3899. {
  3900. if ($this->debug > 0) {
  3901. error_log('learnpath::save_current()', 0);
  3902. }
  3903. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3904. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3905. if ($this->debug > 2) {
  3906. error_log('New LP - save_current() saving item ' . $this->current, 0);
  3907. }
  3908. if ($this->debug > 2) {
  3909. error_log('' . print_r($this->items, true), 0);
  3910. }
  3911. if (isset($this->items[$this->current]) &&
  3912. is_object($this->items[$this->current])
  3913. ) {
  3914. $res = $this->items[$this->current]->save(false, $this->prerequisites_match($this->current));
  3915. $this->autocomplete_parents($this->current);
  3916. $status = $this->items[$this->current]->get_status();
  3917. $this->append_message('new_item_status: ' . $status);
  3918. $this->update_queue[$this->current] = $status;
  3919. return $res;
  3920. }
  3921. return false;
  3922. }
  3923. /**
  3924. * Saves the given item
  3925. * @param integer $item_id. Optional (will take from $_REQUEST if null)
  3926. * @param boolean $from_outside Save from url params (true) or from current attributes (false). Optional. Defaults to true
  3927. * @return boolean
  3928. */
  3929. public function save_item($item_id = null, $from_outside = true)
  3930. {
  3931. $debug = $this->debug;
  3932. if ($debug) {
  3933. error_log('In learnpath::save_item(' . $item_id . ',' . intval($from_outside). ')', 0);
  3934. }
  3935. // TODO: Do a better check on the index pointing to the right item (it is supposed to be working
  3936. // on $ordered_items[] but not sure it's always safe to use with $items[]).
  3937. if (empty($item_id)) {
  3938. $item_id = intval($_REQUEST['id']);
  3939. }
  3940. if (empty($item_id)) {
  3941. $item_id = $this->get_current_item_id();
  3942. }
  3943. if (isset($this->items[$item_id]) && is_object($this->items[$item_id])) {
  3944. if ($debug) {
  3945. error_log('Object exists');
  3946. }
  3947. // Saving the item.
  3948. $res = $this->items[$item_id]->save(
  3949. $from_outside,
  3950. $this->prerequisites_match($item_id)
  3951. );
  3952. if ($debug) {
  3953. error_log('update_queue before:');
  3954. error_log(print_r($this->update_queue,1));
  3955. }
  3956. $this->autocomplete_parents($item_id);
  3957. $status = $this->items[$item_id]->get_status();
  3958. $this->update_queue[$item_id] = $status;
  3959. if ($debug) {
  3960. error_log('get_status(): ' . $status);
  3961. error_log('update_queue after:');
  3962. error_log(print_r($this->update_queue,1));
  3963. }
  3964. return $res;
  3965. }
  3966. return false;
  3967. }
  3968. /**
  3969. * Saves the last item seen's ID only in case
  3970. */
  3971. public function save_last()
  3972. {
  3973. $course_id = api_get_course_int_id();
  3974. if ($this->debug > 0) {
  3975. error_log('New LP - In learnpath::save_last()', 0);
  3976. }
  3977. $session_condition = api_get_session_condition(api_get_session_id(), true, false);
  3978. $table = Database :: get_course_table(TABLE_LP_VIEW);
  3979. if (isset($this->current)) {
  3980. if ($this->debug > 2) {
  3981. error_log('New LP - Saving current item (' . $this->current . ') for later review', 0);
  3982. }
  3983. $sql = "UPDATE $table SET
  3984. last_item = " . intval($this->get_current_item_id()). "
  3985. WHERE
  3986. c_id = $course_id AND
  3987. lp_id = " . $this->get_id() . " AND
  3988. user_id = " . $this->get_user_id()." ".$session_condition;
  3989. if ($this->debug > 2) {
  3990. error_log('New LP - Saving last item seen : ' . $sql, 0);
  3991. }
  3992. Database::query($sql);
  3993. }
  3994. // Save progress.
  3995. list($progress, $text) = $this->get_progress_bar_text('%');
  3996. if ($progress >= 0 && $progress <= 100) {
  3997. $progress = (int) $progress;
  3998. $sql = "UPDATE $table SET
  3999. progress = $progress
  4000. WHERE
  4001. c_id = ".$course_id." AND
  4002. lp_id = " . $this->get_id() . " AND
  4003. user_id = " . $this->get_user_id()." ".$session_condition;
  4004. // Ignore errors as some tables might not have the progress field just yet.
  4005. Database::query($sql);
  4006. $this->progress_db = $progress;
  4007. }
  4008. }
  4009. /**
  4010. * Sets the current item ID (checks if valid and authorized first)
  4011. * @param integer $item_id New item ID. If not given or not authorized, defaults to current
  4012. */
  4013. public function set_current_item($item_id = null)
  4014. {
  4015. if ($this->debug > 0) {
  4016. error_log('New LP - In learnpath::set_current_item(' . $item_id . ')', 0);
  4017. }
  4018. if (empty ($item_id)) {
  4019. if ($this->debug > 2) {
  4020. error_log('New LP - No new current item given, ignore...', 0);
  4021. }
  4022. // Do nothing.
  4023. } else {
  4024. if ($this->debug > 2) {
  4025. error_log('New LP - New current item given is ' . $item_id . '...', 0);
  4026. }
  4027. if (is_numeric($item_id)) {
  4028. $item_id = intval($item_id);
  4029. // TODO: Check in database here.
  4030. $this->last = $this->current;
  4031. $this->current = $item_id;
  4032. // TODO: Update $this->index as well.
  4033. foreach ($this->ordered_items as $index => $item) {
  4034. if ($item == $this->current) {
  4035. $this->index = $index;
  4036. break;
  4037. }
  4038. }
  4039. if ($this->debug > 2) {
  4040. error_log('New LP - set_current_item(' . $item_id . ') done. Index is now : ' . $this->index, 0);
  4041. }
  4042. } else {
  4043. error_log('New LP - set_current_item(' . $item_id . ') failed. Not a numeric value: ', 0);
  4044. }
  4045. }
  4046. }
  4047. /**
  4048. * Sets the encoding
  4049. * @param string New encoding
  4050. * TODO (as of Chamilo 1.8.8): Check in the future whether this method is needed.
  4051. */
  4052. public function set_encoding($enc = 'UTF-8')
  4053. {
  4054. if ($this->debug > 0) {
  4055. error_log('New LP - In learnpath::set_encoding()', 0);
  4056. }
  4057. $course_id = api_get_course_int_id();
  4058. $enc = api_refine_encoding_id($enc);
  4059. if (empty($enc)) {
  4060. $enc = api_get_system_encoding();
  4061. }
  4062. if (api_is_encoding_supported($enc)) {
  4063. $lp = $this->get_id();
  4064. if ($lp != 0) {
  4065. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4066. $sql = "UPDATE $tbl_lp SET default_encoding = '$enc' WHERE c_id = ".$course_id." AND id = " . $lp;
  4067. $res = Database::query($sql);
  4068. return $res;
  4069. }
  4070. }
  4071. return false;
  4072. }
  4073. /**
  4074. * Sets the JS lib setting in the database directly.
  4075. * This is the JavaScript library file this lp needs to load on startup
  4076. * @param string Proximity setting
  4077. * @return boolean True on update success. False otherwise.
  4078. */
  4079. public function set_jslib($lib = '')
  4080. {
  4081. if ($this->debug > 0) {
  4082. error_log('New LP - In learnpath::set_jslib()', 0);
  4083. }
  4084. $lp = $this->get_id();
  4085. $course_id = api_get_course_int_id();
  4086. if ($lp != 0) {
  4087. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  4088. $sql = "UPDATE $tbl_lp SET js_lib = '$lib' WHERE c_id = ".$course_id." AND id = " . $lp;
  4089. $res = Database::query($sql);
  4090. return $res;
  4091. } else {
  4092. return false;
  4093. }
  4094. }
  4095. /**
  4096. * Sets the name of the LP maker (publisher) (and save)
  4097. * @param string Optional string giving the new content_maker of this learnpath
  4098. * @return boolean True
  4099. */
  4100. public function set_maker($name = '')
  4101. {
  4102. if ($this->debug > 0) {
  4103. error_log('New LP - In learnpath::set_maker()', 0);
  4104. }
  4105. if (empty ($name))
  4106. return false;
  4107. $this->maker = $name;
  4108. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4109. $course_id = api_get_course_int_id();
  4110. $lp_id = $this->get_id();
  4111. $sql = "UPDATE $lp_table SET content_maker = '" . Database::escape_string($this->maker) . "'
  4112. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4113. if ($this->debug > 2) {
  4114. error_log('New LP - lp updated with new content_maker : ' . $this->maker, 0);
  4115. }
  4116. Database::query($sql);
  4117. return true;
  4118. }
  4119. /**
  4120. * Sets the name of the current learnpath (and save)
  4121. * @param string $name Optional string giving the new name of this learnpath
  4122. * @return boolean True/False
  4123. */
  4124. public function set_name($name = null)
  4125. {
  4126. if ($this->debug > 0) {
  4127. error_log('New LP - In learnpath::set_name()', 0);
  4128. }
  4129. if (empty($name)) {
  4130. return false;
  4131. }
  4132. $this->name = $name;
  4133. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4134. $lp_id = $this->get_id();
  4135. $course_id = api_get_course_int_id();
  4136. $sql = "UPDATE $lp_table SET name = '" . Database::escape_string($this->name). "'
  4137. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4138. if ($this->debug > 2) {
  4139. error_log('New LP - lp updated with new name : ' . $this->name, 0);
  4140. }
  4141. Database::query($sql);
  4142. // If the lp is visible on the homepage, change his name there.
  4143. if (Database::affected_rows()) {
  4144. $session_id = api_get_session_id();
  4145. $session_condition = api_get_session_condition($session_id);
  4146. $tbl_tool = Database :: get_course_table(TABLE_TOOL_LIST);
  4147. $link = 'newscorm/lp_controller.php?action=view&lp_id=' . $lp_id.'&id_session='.$session_id;
  4148. $sql = "UPDATE $tbl_tool SET name = '$this->name'
  4149. WHERE c_id = ".$course_id." AND (link='$link' and image='scormbuilder.gif' $session_condition)";
  4150. Database::query($sql);
  4151. return true;
  4152. } else {
  4153. return false;
  4154. }
  4155. }
  4156. /**
  4157. * Set index specified prefix terms for all items in this path
  4158. * @param string Comma-separated list of terms
  4159. * @param char Xapian term prefix
  4160. * @return boolean False on error, true otherwise
  4161. */
  4162. public function set_terms_by_prefix($terms_string, $prefix)
  4163. {
  4164. $course_id = api_get_course_int_id();
  4165. if (api_get_setting('search_enabled') !== 'true')
  4166. return false;
  4167. if (!extension_loaded('xapian')) {
  4168. return false;
  4169. }
  4170. $terms_string = trim($terms_string);
  4171. $terms = explode(',', $terms_string);
  4172. array_walk($terms, 'trim_value');
  4173. $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
  4174. // Don't do anything if no change, verify only at DB, not the search engine.
  4175. if ((count(array_diff($terms, $stored_terms)) == 0) && (count(array_diff($stored_terms, $terms)) == 0))
  4176. return false;
  4177. require_once 'xapian.php'; // TODO: Try catch every xapian use or make wrappers on API.
  4178. require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
  4179. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  4180. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  4181. $items_table = Database :: get_course_table(TABLE_LP_ITEM);
  4182. // TODO: Make query secure agains XSS : use member attr instead of post var.
  4183. $lp_id = intval($_POST['lp_id']);
  4184. $sql = "SELECT * FROM $items_table WHERE c_id = $course_id AND lp_id = $lp_id";
  4185. $result = Database::query($sql);
  4186. $di = new ChamiloIndexer();
  4187. while ($lp_item = Database :: fetch_array($result)) {
  4188. // Get search_did.
  4189. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  4190. $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';
  4191. $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
  4192. //echo $sql; echo '<br>';
  4193. $res = Database::query($sql);
  4194. if (Database::num_rows($res) > 0) {
  4195. $se_ref = Database :: fetch_array($res);
  4196. // Compare terms.
  4197. $doc = $di->get_document($se_ref['search_did']);
  4198. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  4199. $xterms = array();
  4200. foreach ($xapian_terms as $xapian_term) {
  4201. $xterms[] = substr($xapian_term['name'], 1);
  4202. }
  4203. $dterms = $terms;
  4204. $missing_terms = array_diff($dterms, $xterms);
  4205. $deprecated_terms = array_diff($xterms, $dterms);
  4206. // Save it to search engine.
  4207. foreach ($missing_terms as $term) {
  4208. $doc->add_term($prefix . $term, 1);
  4209. }
  4210. foreach ($deprecated_terms as $term) {
  4211. $doc->remove_term($prefix . $term);
  4212. }
  4213. $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
  4214. $di->getDb()->flush();
  4215. } else {
  4216. //@todo What we should do here?
  4217. }
  4218. }
  4219. return true;
  4220. }
  4221. /**
  4222. * Sets the theme of the LP (local/remote) (and save)
  4223. * @param string Optional string giving the new theme of this learnpath
  4224. * @return bool Returns true if theme name is not empty
  4225. */
  4226. public function set_theme($name = '')
  4227. {
  4228. $course_id = api_get_course_int_id();
  4229. if ($this->debug > 0) {
  4230. error_log('New LP - In learnpath::set_theme()', 0);
  4231. }
  4232. $this->theme = $name;
  4233. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4234. $lp_id = $this->get_id();
  4235. $sql = "UPDATE $lp_table SET theme = '" . Database::escape_string($this->theme). "'
  4236. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4237. if ($this->debug > 2) {
  4238. error_log('New LP - lp updated with new theme : ' . $this->theme, 0);
  4239. }
  4240. Database::query($sql);
  4241. return true;
  4242. }
  4243. /**
  4244. * Sets the image of an LP (and save)
  4245. * @param string Optional string giving the new image of this learnpath
  4246. * @return bool Returns true if theme name is not empty
  4247. */
  4248. public function set_preview_image($name = '')
  4249. {
  4250. $course_id = api_get_course_int_id();
  4251. if ($this->debug > 0) {
  4252. error_log('New LP - In learnpath::set_preview_image()', 0);
  4253. }
  4254. $this->preview_image = $name;
  4255. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4256. $lp_id = $this->get_id();
  4257. $sql = "UPDATE $lp_table SET
  4258. preview_image = '" . Database::escape_string($this->preview_image). "'
  4259. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4260. if ($this->debug > 2) {
  4261. error_log('New LP - lp updated with new preview image : ' . $this->preview_image, 0);
  4262. }
  4263. Database::query($sql);
  4264. return true;
  4265. }
  4266. /**
  4267. * Sets the author of a LP (and save)
  4268. * @param string Optional string giving the new author of this learnpath
  4269. * @return bool Returns true if author's name is not empty
  4270. */
  4271. public function set_author($name = '') {
  4272. $course_id = api_get_course_int_id();
  4273. if ($this->debug > 0) {
  4274. error_log('New LP - In learnpath::set_author()', 0);
  4275. }
  4276. $this->author = $name;
  4277. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4278. $lp_id = $this->get_id();
  4279. $sql = "UPDATE $lp_table SET author = '" . Database::escape_string($name). "'
  4280. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4281. if ($this->debug > 2) {
  4282. error_log('New LP - lp updated with new preview author : ' . $this->author, 0);
  4283. }
  4284. Database::query($sql);
  4285. return true;
  4286. }
  4287. /**
  4288. * Sets the hide_toc_frame parameter of a LP (and save)
  4289. * @param int 1 if frame is hidden 0 then else
  4290. * @return bool Returns true if author's name is not empty
  4291. */
  4292. public function set_hide_toc_frame($hide)
  4293. {
  4294. $course_id = api_get_course_int_id();
  4295. if ($this->debug > 0) {
  4296. error_log('New LP - In learnpath::set_hide_toc_frame()', 0);
  4297. }
  4298. if (intval($hide) == $hide){
  4299. $this->hide_toc_frame = $hide;
  4300. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4301. $lp_id = $this->get_id();
  4302. $sql = "UPDATE $lp_table SET hide_toc_frame = '" . $this->hide_toc_frame . "'
  4303. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4304. if ($this->debug > 2) {
  4305. error_log('New LP - lp updated with new preview hide_toc_frame : ' . $this->author, 0);
  4306. }
  4307. Database::query($sql);
  4308. return true;
  4309. } else {
  4310. return false;
  4311. }
  4312. }
  4313. /**
  4314. * Sets the prerequisite of a LP (and save)
  4315. * @param int integer giving the new prerequisite of this learnpath
  4316. * @return bool returns true if prerequisite is not empty
  4317. */
  4318. public function set_prerequisite($prerequisite)
  4319. {
  4320. $course_id = api_get_course_int_id();
  4321. if ($this->debug > 0) {
  4322. error_log('New LP - In learnpath::set_prerequisite()', 0);
  4323. }
  4324. $this->prerequisite = intval($prerequisite);
  4325. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4326. $lp_id = $this->get_id();
  4327. $sql = "UPDATE $lp_table SET prerequisite = '".$this->prerequisite."'
  4328. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4329. if ($this->debug > 2) {
  4330. error_log('New LP - lp updated with new preview requisite : ' . $this->requisite, 0);
  4331. }
  4332. Database::query($sql);
  4333. return true;
  4334. }
  4335. /**
  4336. * Sets the location/proximity of the LP (local/remote) (and save)
  4337. * @param string Optional string giving the new location of this learnpath
  4338. * @return boolean True on success / False on error
  4339. */
  4340. public function set_proximity($name = '')
  4341. {
  4342. $course_id = api_get_course_int_id();
  4343. if ($this->debug > 0) {
  4344. error_log('New LP - In learnpath::set_proximity()', 0);
  4345. }
  4346. if (empty ($name))
  4347. return false;
  4348. $this->proximity = $name;
  4349. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4350. $lp_id = $this->get_id();
  4351. $sql = "UPDATE $lp_table SET
  4352. content_local = '" . Database::escape_string($name) . "'
  4353. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4354. if ($this->debug > 2) {
  4355. error_log('New LP - lp updated with new proximity : ' . $this->proximity, 0);
  4356. }
  4357. Database::query($sql);
  4358. return true;
  4359. }
  4360. /**
  4361. * Sets the previous item ID to a given ID. Generally, this should be set to the previous 'current' item
  4362. * @param integer DB ID of the item
  4363. */
  4364. public function set_previous_item($id)
  4365. {
  4366. if ($this->debug > 0) {
  4367. error_log('New LP - In learnpath::set_previous_item()', 0);
  4368. }
  4369. $this->last = $id;
  4370. }
  4371. /**
  4372. * Sets use_max_score
  4373. * @param string $use_max_score Optional string giving the new location of this learnpath
  4374. * @return boolean True on success / False on error
  4375. */
  4376. public function set_use_max_score($use_max_score = 1)
  4377. {
  4378. $course_id = api_get_course_int_id();
  4379. if ($this->debug > 0) {
  4380. error_log('New LP - In learnpath::set_use_max_score()', 0);
  4381. }
  4382. $use_max_score = intval($use_max_score);
  4383. $this->use_max_score = $use_max_score;
  4384. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4385. $lp_id = $this->get_id();
  4386. $sql = "UPDATE $lp_table SET
  4387. use_max_score = '" . $this->use_max_score . "'
  4388. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4389. if ($this->debug > 2) {
  4390. error_log('New LP - lp updated with new use_max_score : ' . $this->use_max_score, 0);
  4391. }
  4392. Database::query($sql);
  4393. return true;
  4394. }
  4395. /**
  4396. * Sets and saves the expired_on date
  4397. * @param string $expired_on Optional string giving the new author of this learnpath
  4398. * @return bool Returns true if author's name is not empty
  4399. */
  4400. public function set_expired_on($expired_on)
  4401. {
  4402. $course_id = api_get_course_int_id();
  4403. if ($this->debug > 0) {
  4404. error_log('New LP - In learnpath::set_expired_on()', 0);
  4405. }
  4406. if (!empty($expired_on)) {
  4407. $this->expired_on = api_get_utc_datetime($expired_on);
  4408. } else {
  4409. $this->expired_on = '';
  4410. }
  4411. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4412. $lp_id = $this->get_id();
  4413. $sql = "UPDATE $lp_table SET
  4414. expired_on = '" . Database::escape_string($this->expired_on) . "'
  4415. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4416. if ($this->debug > 2) {
  4417. error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
  4418. }
  4419. Database::query($sql);
  4420. return true;
  4421. }
  4422. /**
  4423. * Sets and saves the publicated_on date
  4424. * @param string $publicated_on Optional string giving the new author of this learnpath
  4425. * @return bool Returns true if author's name is not empty
  4426. */
  4427. public function set_publicated_on($publicated_on)
  4428. {
  4429. $course_id = api_get_course_int_id();
  4430. if ($this->debug > 0) {
  4431. error_log('New LP - In learnpath::set_expired_on()', 0);
  4432. }
  4433. if (!empty($publicated_on)) {
  4434. $this->publicated_on = api_get_utc_datetime($publicated_on);
  4435. } else {
  4436. $this->publicated_on = '';
  4437. }
  4438. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4439. $lp_id = $this->get_id();
  4440. $sql = "UPDATE $lp_table SET
  4441. publicated_on = '" . Database::escape_string($this->publicated_on) . "'
  4442. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4443. if ($this->debug > 2) {
  4444. error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
  4445. }
  4446. Database::query($sql);
  4447. return true;
  4448. }
  4449. /**
  4450. * Sets and saves the expired_on date
  4451. * @return bool Returns true if author's name is not empty
  4452. */
  4453. public function set_modified_on()
  4454. {
  4455. $course_id = api_get_course_int_id();
  4456. if ($this->debug > 0) {
  4457. error_log('New LP - In learnpath::set_expired_on()', 0);
  4458. }
  4459. $this->modified_on = api_get_utc_datetime();
  4460. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4461. $lp_id = $this->get_id();
  4462. $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "'
  4463. WHERE c_id = ".$course_id." AND id = '$lp_id'";
  4464. if ($this->debug > 2) {
  4465. error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
  4466. }
  4467. Database::query($sql);
  4468. return true;
  4469. }
  4470. /**
  4471. * Sets the object's error message
  4472. * @param string Error message. If empty, reinits the error string
  4473. * @return void
  4474. */
  4475. public function set_error_msg($error = '')
  4476. {
  4477. if ($this->debug > 0) {
  4478. error_log('New LP - In learnpath::set_error_msg()', 0);
  4479. }
  4480. if (empty ($error)) {
  4481. $this->error = '';
  4482. } else {
  4483. $this->error .= $error;
  4484. }
  4485. }
  4486. /**
  4487. * Launches the current item if not 'sco'
  4488. * (starts timer and make sure there is a record ready in the DB)
  4489. * @param boolean $allow_new_attempt Whether to allow a new attempt or not
  4490. * @return boolean
  4491. */
  4492. public function start_current_item($allow_new_attempt = false)
  4493. {
  4494. if ($this->debug > 0) {
  4495. error_log('New LP - In learnpath::start_current_item()', 0);
  4496. }
  4497. if ($this->current != 0 AND is_object($this->items[$this->current])) {
  4498. $type = $this->get_type();
  4499. $item_type = $this->items[$this->current]->get_type();
  4500. if (($type == 2 && $item_type != 'sco') OR ($type == 3 && $item_type != 'au') OR
  4501. ($type == 1 && $item_type != TOOL_QUIZ && $item_type != TOOL_HOTPOTATOES)
  4502. ) {
  4503. $this->items[$this->current]->open($allow_new_attempt);
  4504. $this->autocomplete_parents($this->current);
  4505. $prereq_check = $this->prerequisites_match($this->current);
  4506. $this->items[$this->current]->save(false, $prereq_check);
  4507. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4508. } else {
  4509. // If sco, then it is supposed to have been updated by some other call.
  4510. }
  4511. if ($item_type == 'sco') {
  4512. $this->items[$this->current]->restart();
  4513. }
  4514. }
  4515. if ($this->debug > 0) {
  4516. error_log('New LP - End of learnpath::start_current_item()', 0);
  4517. }
  4518. return true;
  4519. }
  4520. /**
  4521. * Stops the processing and counters for the old item (as held in $this->last)
  4522. * @return boolean True/False
  4523. */
  4524. public function stop_previous_item()
  4525. {
  4526. if ($this->debug > 0) {
  4527. error_log('New LP - In learnpath::stop_previous_item()', 0);
  4528. }
  4529. if ($this->last != 0 && $this->last != $this->current && is_object($this->items[$this->last])) {
  4530. if ($this->debug > 2) {
  4531. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' is object', 0);
  4532. }
  4533. switch ($this->get_type()) {
  4534. case '3' :
  4535. if ($this->items[$this->last]->get_type() != 'au') {
  4536. if ($this->debug > 2) {
  4537. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 3 is <> au', 0);
  4538. }
  4539. $this->items[$this->last]->close();
  4540. //$this->autocomplete_parents($this->last);
  4541. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4542. } else {
  4543. if ($this->debug > 2) {
  4544. error_log('New LP - In learnpath::stop_previous_item() - Item is an AU, saving is managed by AICC signals', 0);
  4545. }
  4546. }
  4547. case '2' :
  4548. if ($this->items[$this->last]->get_type() != 'sco') {
  4549. if ($this->debug > 2) {
  4550. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 2 is <> sco', 0);
  4551. }
  4552. $this->items[$this->last]->close();
  4553. //$this->autocomplete_parents($this->last);
  4554. //$this->update_queue[$this->last] = $this->items[$this->last]->get_status();
  4555. } else {
  4556. if ($this->debug > 2) {
  4557. error_log('New LP - In learnpath::stop_previous_item() - Item is a SCO, saving is managed by SCO signals', 0);
  4558. }
  4559. }
  4560. break;
  4561. case '1' :
  4562. default :
  4563. if ($this->debug > 2) {
  4564. error_log('New LP - In learnpath::stop_previous_item() - ' . $this->last . ' in lp_type 1 is asset', 0);
  4565. }
  4566. $this->items[$this->last]->close();
  4567. break;
  4568. }
  4569. } else {
  4570. if ($this->debug > 2) {
  4571. error_log('New LP - In learnpath::stop_previous_item() - No previous element found, ignoring...', 0);
  4572. }
  4573. return false;
  4574. }
  4575. return true;
  4576. }
  4577. /**
  4578. * Updates the default view mode from fullscreen to embedded and inversely
  4579. * @return string The current default view mode ('fullscreen' or 'embedded')
  4580. */
  4581. public function update_default_view_mode()
  4582. {
  4583. $course_id = api_get_course_int_id();
  4584. if ($this->debug > 0) {
  4585. error_log('New LP - In learnpath::update_default_view_mode()', 0);
  4586. }
  4587. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4588. $sql = "SELECT * FROM $lp_table
  4589. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4590. $res = Database::query($sql);
  4591. if (Database :: num_rows($res) > 0) {
  4592. $row = Database :: fetch_array($res);
  4593. $default_view_mode = $row['default_view_mod'];
  4594. $view_mode = $default_view_mode;
  4595. switch ($default_view_mode) {
  4596. case 'fullscreen':
  4597. $view_mode = 'embedded';
  4598. break;
  4599. case 'embedded':
  4600. $view_mode = 'embedframe';
  4601. break;
  4602. case 'embedframe':
  4603. $view_mode = 'impress';
  4604. break;
  4605. case 'impress':
  4606. $view_mode = 'fullscreen';
  4607. break;
  4608. }
  4609. $sql = "UPDATE $lp_table SET default_view_mod = '$view_mode'
  4610. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4611. Database::query($sql);
  4612. $this->mode = $view_mode;
  4613. return $view_mode;
  4614. } else {
  4615. if ($this->debug > 2) {
  4616. error_log('New LP - Problem in update_default_view() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4617. }
  4618. }
  4619. return -1;
  4620. }
  4621. /**
  4622. * Updates the default behaviour about auto-commiting SCORM updates
  4623. * @return boolean True if auto-commit has been set to 'on', false otherwise
  4624. */
  4625. public function update_default_scorm_commit()
  4626. {
  4627. $course_id = api_get_course_int_id();
  4628. if ($this->debug > 0) {
  4629. error_log('New LP - In learnpath::update_default_scorm_commit()', 0);
  4630. }
  4631. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4632. $sql = "SELECT * FROM $lp_table
  4633. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4634. $res = Database::query($sql);
  4635. if (Database :: num_rows($res) > 0) {
  4636. $row = Database :: fetch_array($res);
  4637. $force = $row['force_commit'];
  4638. if ($force == 1) {
  4639. $force = 0;
  4640. $force_return = false;
  4641. } elseif ($force == 0) {
  4642. $force = 1;
  4643. $force_return = true;
  4644. }
  4645. $sql = "UPDATE $lp_table SET force_commit = $force
  4646. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4647. Database::query($sql);
  4648. $this->force_commit = $force_return;
  4649. return $force_return;
  4650. } else {
  4651. if ($this->debug > 2) {
  4652. error_log('New LP - Problem in update_default_scorm_commit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4653. }
  4654. }
  4655. return -1;
  4656. }
  4657. /**
  4658. * Updates the order of learning paths (goes through all of them by order and fills the gaps)
  4659. * @return bool True on success, false on failure
  4660. */
  4661. public function update_display_order()
  4662. {
  4663. $course_id = api_get_course_int_id();
  4664. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4665. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." ORDER BY display_order";
  4666. $res = Database::query($sql);
  4667. if ($res === false)
  4668. return false;
  4669. $num = Database :: num_rows($res);
  4670. // First check the order is correct, globally (might be wrong because
  4671. // of versions < 1.8.4).
  4672. if ($num > 0) {
  4673. $i = 1;
  4674. while ($row = Database :: fetch_array($res)) {
  4675. if ($row['display_order'] != $i) { // If we find a gap in the order, we need to fix it.
  4676. $need_fix = true;
  4677. $sql_u = "UPDATE $lp_table SET display_order = $i
  4678. WHERE c_id = ".$course_id." AND id = " . $row['id'];
  4679. Database::query($sql_u);
  4680. }
  4681. $i++;
  4682. }
  4683. }
  4684. return true;
  4685. }
  4686. /**
  4687. * Updates the "prevent_reinit" value that enables control on reinitialising items on second view
  4688. * @return boolean True if prevent_reinit has been set to 'on', false otherwise (or 1 or 0 in this case)
  4689. */
  4690. public function update_reinit()
  4691. {
  4692. $course_id = api_get_course_int_id();
  4693. if ($this->debug > 0) {
  4694. error_log('New LP - In learnpath::update_reinit()', 0);
  4695. }
  4696. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4697. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4698. $res = Database::query($sql);
  4699. if (Database :: num_rows($res) > 0) {
  4700. $row = Database :: fetch_array($res);
  4701. $force = $row['prevent_reinit'];
  4702. if ($force == 1) {
  4703. $force = 0;
  4704. } elseif ($force == 0) {
  4705. $force = 1;
  4706. }
  4707. $sql = "UPDATE $lp_table SET prevent_reinit = $force
  4708. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4709. Database::query($sql);
  4710. $this->prevent_reinit = $force;
  4711. return $force;
  4712. } else {
  4713. if ($this->debug > 2) {
  4714. error_log('New LP - Problem in update_reinit() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4715. }
  4716. }
  4717. return -1;
  4718. }
  4719. /**
  4720. * Determine the attempt_mode thanks to prevent_reinit and seriousgame_mode db flag
  4721. *
  4722. * @return string 'single', 'multi' or 'seriousgame'
  4723. * @author ndiechburg <noel@cblue.be>
  4724. **/
  4725. public function get_attempt_mode()
  4726. {
  4727. //Set default value for seriousgame_mode
  4728. if (!isset($this->seriousgame_mode)) {
  4729. $this->seriousgame_mode=0;
  4730. }
  4731. // Set default value for prevent_reinit
  4732. if (!isset($this->prevent_reinit)) {
  4733. $this->prevent_reinit =1;
  4734. }
  4735. if ($this->seriousgame_mode == 1 && $this->prevent_reinit == 1) {
  4736. return 'seriousgame';
  4737. }
  4738. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 1) {
  4739. return 'single';
  4740. }
  4741. if ($this->seriousgame_mode == 0 && $this->prevent_reinit == 0) {
  4742. return 'multiple';
  4743. }
  4744. return 'single';
  4745. }
  4746. /**
  4747. * Register the attempt mode into db thanks to flags prevent_reinit and seriousgame_mode flags
  4748. *
  4749. * @param string 'seriousgame', 'single' or 'multiple'
  4750. * @return boolean
  4751. * @author ndiechburg <noel@cblue.be>
  4752. **/
  4753. public function set_attempt_mode($mode) {
  4754. $course_id = api_get_course_int_id();
  4755. switch ($mode) {
  4756. case 'seriousgame' :
  4757. $sg_mode = 1;
  4758. $prevent_reinit = 1;
  4759. break;
  4760. case 'single' :
  4761. $sg_mode = 0;
  4762. $prevent_reinit = 1;
  4763. break;
  4764. case 'multiple' :
  4765. $sg_mode = 0;
  4766. $prevent_reinit = 0;
  4767. break;
  4768. default :
  4769. $sg_mode = 0;
  4770. $prevent_reinit = 0;
  4771. break;
  4772. }
  4773. $this->prevent_reinit = $prevent_reinit;
  4774. $this->seriousgame_mode = $sg_mode;
  4775. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4776. $sql = "UPDATE $lp_table SET prevent_reinit = $prevent_reinit , seriousgame_mode = $sg_mode
  4777. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4778. $res = Database::query($sql);
  4779. if ($res) {
  4780. return true;
  4781. } else {
  4782. return false;
  4783. }
  4784. }
  4785. /**
  4786. * Switch between multiple attempt, single attempt or serious_game mode (only for scorm)
  4787. *
  4788. * @return boolean
  4789. * @author ndiechburg <noel@cblue.be>
  4790. **/
  4791. public function switch_attempt_mode() {
  4792. if ($this->debug > 0) {
  4793. error_log('New LP - In learnpath::switch_attempt_mode()', 0);
  4794. }
  4795. $mode = $this->get_attempt_mode();
  4796. switch ($mode) {
  4797. case 'single' :
  4798. $next_mode = 'multiple';
  4799. break;
  4800. case 'multiple' :
  4801. $next_mode = 'seriousgame';
  4802. break;
  4803. case 'seriousgame' :
  4804. $next_mode = 'single';
  4805. break;
  4806. default :
  4807. $next_mode = 'single';
  4808. break;
  4809. }
  4810. $this->set_attempt_mode($next_mode);
  4811. }
  4812. /**
  4813. * Switch the lp in ktm mode. This is a special scorm mode with unique attempt
  4814. * but possibility to do again a completed item.
  4815. *
  4816. * @return boolean true if seriousgame_mode has been set to 1, false otherwise
  4817. * @author ndiechburg <noel@cblue.be>
  4818. **/
  4819. public function set_seriousgame_mode() {
  4820. $course_id = api_get_course_int_id();
  4821. if ($this->debug > 0) {
  4822. error_log('New LP - In learnpath::set_seriousgame_mode()', 0);
  4823. }
  4824. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4825. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4826. $res = Database::query($sql);
  4827. if (Database :: num_rows($res) > 0) {
  4828. $row = Database :: fetch_array($res);
  4829. $force = $row['seriousgame_mode'];
  4830. if ($force == 1) {
  4831. $force = 0;
  4832. } elseif ($force == 0) {
  4833. $force = 1;
  4834. }
  4835. $sql = "UPDATE $lp_table SET seriousgame_mode = $force
  4836. WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4837. Database::query($sql);
  4838. $this->seriousgame_mode = $force;
  4839. return $force;
  4840. } else {
  4841. if ($this->debug > 2) {
  4842. error_log('New LP - Problem in set_seriousgame_mode() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4843. }
  4844. }
  4845. return -1;
  4846. }
  4847. /**
  4848. * Updates the "scorm_debug" value that shows or hide the debug window
  4849. * @return boolean True if scorm_debug has been set to 'on', false otherwise (or 1 or 0 in this case)
  4850. */
  4851. public function update_scorm_debug()
  4852. {
  4853. $course_id = api_get_course_int_id();
  4854. if ($this->debug > 0) {
  4855. error_log('New LP - In learnpath::update_scorm_debug()', 0);
  4856. }
  4857. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  4858. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4859. $res = Database::query($sql);
  4860. if (Database :: num_rows($res) > 0) {
  4861. $row = Database :: fetch_array($res);
  4862. $force = $row['debug'];
  4863. if ($force == 1) {
  4864. $force = 0;
  4865. } elseif ($force == 0) {
  4866. $force = 1;
  4867. }
  4868. $sql = "UPDATE $lp_table SET debug = $force WHERE c_id = ".$course_id." AND id = " . $this->get_id();
  4869. $res = Database::query($sql);
  4870. $this->scorm_debug = $force;
  4871. return $force;
  4872. } else {
  4873. if ($this->debug > 2) {
  4874. error_log('New LP - Problem in update_scorm_debug() - could not find LP ' . $this->get_id() . ' in DB', 0);
  4875. }
  4876. }
  4877. return -1;
  4878. }
  4879. /**
  4880. * Function that makes a call to the function sort_tree_array and create_tree_array
  4881. * @author Kevin Van Den Haute
  4882. * @param array
  4883. */
  4884. public function tree_array($array)
  4885. {
  4886. if ($this->debug > 1) {
  4887. error_log('New LP - In learnpath::tree_array()', 0);
  4888. }
  4889. $array = $this->sort_tree_array($array);
  4890. $this->create_tree_array($array);
  4891. }
  4892. /**
  4893. * Creates an array with the elements of the learning path tree in it
  4894. *
  4895. * @author Kevin Van Den Haute
  4896. * @param array $array
  4897. * @param int $parent
  4898. * @param int $depth
  4899. * @param array $tmp
  4900. */
  4901. public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array ()) {
  4902. if ($this->debug > 1) {
  4903. error_log('New LP - In learnpath::create_tree_array())', 0);
  4904. }
  4905. if (is_array($array)) {
  4906. for ($i = 0; $i < count($array); $i++) {
  4907. if ($array[$i]['parent_item_id'] == $parent) {
  4908. if (!in_array($array[$i]['parent_item_id'], $tmp)) {
  4909. $tmp[] = $array[$i]['parent_item_id'];
  4910. $depth++;
  4911. }
  4912. $preq = (empty($array[$i]['prerequisite']) ? '' : $array[$i]['prerequisite']);
  4913. $audio = isset($array[$i]['audio']) ? $array[$i]['audio'] : null;
  4914. $path = isset($array[$i]['path']) ? $array[$i]['path'] : null;
  4915. $this->arrMenu[] = array(
  4916. 'id' => $array[$i]['id'],
  4917. 'item_type' => $array[$i]['item_type'],
  4918. 'title' => $array[$i]['title'],
  4919. 'path' => $path,
  4920. 'description' => $array[$i]['description'],
  4921. 'parent_item_id' => $array[$i]['parent_item_id'],
  4922. 'previous_item_id' => $array[$i]['previous_item_id'],
  4923. 'next_item_id' => $array[$i]['next_item_id'],
  4924. 'min_score' => $array[$i]['min_score'],
  4925. 'max_score' => $array[$i]['max_score'],
  4926. 'mastery_score' => $array[$i]['mastery_score'],
  4927. 'display_order' => $array[$i]['display_order'],
  4928. 'prerequisite' => $preq,
  4929. 'depth' => $depth,
  4930. 'audio' => $audio
  4931. );
  4932. $this->create_tree_array($array, $array[$i]['id'], $depth, $tmp);
  4933. }
  4934. }
  4935. }
  4936. }
  4937. /**
  4938. * Sorts a multi dimensional array by parent id and display order
  4939. * @author Kevin Van Den Haute
  4940. *
  4941. * @param array $array (array with al the learning path items in it)
  4942. *
  4943. * @return array
  4944. */
  4945. public function sort_tree_array($array) {
  4946. foreach ($array as $key => $row) {
  4947. $parent[$key] = $row['parent_item_id'];
  4948. $position[$key] = $row['display_order'];
  4949. }
  4950. if (count($array) > 0)
  4951. array_multisort($parent, SORT_ASC, $position, SORT_ASC, $array);
  4952. return $array;
  4953. }
  4954. /**
  4955. * Function that creates a html list of learning path items so that we can add audio files to them
  4956. * @author Kevin Van Den Haute
  4957. * @param int $lp_id
  4958. * @return string
  4959. */
  4960. public function overview()
  4961. {
  4962. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  4963. if ($this->debug > 0) {
  4964. error_log('New LP - In learnpath::overview()', 0);
  4965. }
  4966. global $_course;
  4967. $_SESSION['gradebook'] = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  4968. $return = '';
  4969. $update_audio = isset($_GET['updateaudio']) ? $_GET['updateaudio'] : null;
  4970. // we need to start a form when we want to update all the mp3 files
  4971. if ($update_audio == 'true') {
  4972. $return .= '<form action="' . api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;updateaudio=' . Security :: remove_XSS($_GET['updateaudio']) .'&amp;action=' . Security :: remove_XSS($_GET['action']) . '&amp;lp_id=' . $_SESSION['oLP']->lp_id . '" method="post" enctype="multipart/form-data" name="updatemp3" id="updatemp3">';
  4973. }
  4974. $return .= '<div id="message"></div>';
  4975. if (count($this->items) == 0) {
  4976. $return .= Display::display_normal_message(get_lang('YouShouldAddItemsBeforeAttachAudio'));
  4977. } else {
  4978. $return_audio = '<table class="data_table">';
  4979. $return_audio .= '<tr>';
  4980. $return_audio .= '<th width="60%">' . get_lang('Title') . '</th>';
  4981. $return_audio .= '<th>' . get_lang('Audio') . '</th>';
  4982. $return_audio .= '</tr>';
  4983. if ($update_audio != 'true') {
  4984. $return .= '<div class="span12">';
  4985. $return .= self::return_new_tree($update_audio);
  4986. $return .='</div>';
  4987. $return .= Display::div(Display::url(get_lang('Save'), '#', array('id'=>'listSubmit', 'class'=>'btn')), array('style'=>'float:left; margin-top:15px;width:100%'));
  4988. } else {
  4989. $return_audio .= self::return_new_tree($update_audio);
  4990. $return .= $return_audio.'</table>';
  4991. }
  4992. // We need to close the form when we are updating the mp3 files.
  4993. if ($update_audio == 'true') {
  4994. $return .= '<div style="margin:40px 0; float:right;"><button class="save" type="submit" name="save_audio" id="save_audio">' . get_lang('SaveAudioAndOrganization') . '</button></div>'; // TODO: What kind of language variable is this?
  4995. }
  4996. }
  4997. // We need to close the form when we are updating the mp3 files.
  4998. if ($update_audio == 'true' && count($this->arrMenu) != 0) {
  4999. $return .= '</form>';
  5000. }
  5001. return $return;
  5002. }
  5003. /**
  5004. * @param string string $update_audio
  5005. * @param bool $drop_element_here
  5006. * @return string
  5007. */
  5008. public function return_new_tree($update_audio = 'false', $drop_element_here = false)
  5009. {
  5010. $return = '';
  5011. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  5012. $course_id = api_get_course_int_id();
  5013. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5014. $sql = "SELECT * FROM $tbl_lp_item
  5015. WHERE c_id = $course_id AND lp_id = ".$this->lp_id;
  5016. $result = Database::query($sql);
  5017. $arrLP = array();
  5018. while ($row = Database :: fetch_array($result)) {
  5019. $arrLP[] = array(
  5020. 'id' => $row['id'],
  5021. 'item_type' => $row['item_type'],
  5022. 'title' => Security :: remove_XSS($row['title']),
  5023. 'path' => $row['path'],
  5024. 'description' => Security::remove_XSS($row['description']),
  5025. 'parent_item_id' => $row['parent_item_id'],
  5026. 'previous_item_id' => $row['previous_item_id'],
  5027. 'next_item_id' => $row['next_item_id'],
  5028. 'max_score' => $row['max_score'],
  5029. 'min_score' => $row['min_score'],
  5030. 'mastery_score' => $row['mastery_score'],
  5031. 'prerequisite' => $row['prerequisite'],
  5032. 'display_order' => $row['display_order'],
  5033. 'audio' => $row['audio']
  5034. );
  5035. }
  5036. $this->tree_array($arrLP);
  5037. $arrLP = $this->arrMenu;
  5038. unset ($this->arrMenu);
  5039. $default_data = null;
  5040. $default_content = null;
  5041. $elements = array();
  5042. $return_audio = null;
  5043. for ($i = 0; $i < count($arrLP); $i++) {
  5044. $title = $arrLP[$i]['title'];
  5045. $title_cut = cut($arrLP[$i]['title'], 25);
  5046. //Link for the documents
  5047. if ($arrLP[$i]['item_type'] == 'document') {
  5048. $url = api_get_self() . '?'.api_get_cidreq().'&amp;action=view_item&amp;mode=preview_document&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id;
  5049. $title_cut = Display::url($title_cut, $url, array('class' => 'ajax'));
  5050. }
  5051. if (($i % 2) == 0) {
  5052. $oddClass = 'row_odd';
  5053. } else {
  5054. $oddClass = 'row_even';
  5055. }
  5056. $return_audio .= '<tr id ="lp_item_'.$arrLP[$i]['id'] .'" class="' . $oddClass . '">';
  5057. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5058. $icon = '';
  5059. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5060. $icon = '<img src="../img/lp_' . $icon_name . '.png" />';
  5061. } else {
  5062. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5063. $icon = '<img src="../img/lp_' . $icon_name . '.gif" />';
  5064. } else {
  5065. $icon = '<img src="../img/folder_document.gif" />';
  5066. }
  5067. }
  5068. // The audio column.
  5069. $return_audio .= '<td align="center">';
  5070. $audio = '';
  5071. if (!$update_audio OR $update_audio <> 'true') {
  5072. if (!empty($arrLP[$i]['audio'])) {
  5073. /*$audio .= '<span id="container'.$i.'"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</span>';
  5074. $audio .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  5075. $audio .= '<script type="text/javascript">
  5076. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  5077. s1.addParam("allowscriptaccess","always");
  5078. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $arrLP[$i]['audio'] . '");
  5079. s1.write("container' . $i . '");
  5080. </script>';*/
  5081. } else {
  5082. $audio .= '';
  5083. }
  5084. } else {
  5085. $types = self::getChapterTypes();
  5086. if (!in_array($arrLP[$i]['item_type'], $types)) {
  5087. $audio .= '<input type="file" name="mp3file' . $arrLP[$i]['id'] . '" id="mp3file" />';
  5088. if (!empty ($arrLP[$i]['audio'])) {
  5089. $audio .= '<br />'.Security::remove_XSS($arrLP[$i]['audio']).'<br />
  5090. <input type="checkbox" name="removemp3' . $arrLP[$i]['id'] . '" id="checkbox' . $arrLP[$i]['id'] . '" />' . get_lang('RemoveAudio');
  5091. }
  5092. }
  5093. }
  5094. $return_audio .= Display::span($icon.' '.$title).Display::tag('td', $audio, array('style'=>''));
  5095. $return_audio .= '</td>';
  5096. $move_icon = '';
  5097. $move_item_icon = '';
  5098. $edit_icon = '';
  5099. $delete_icon = '';
  5100. $audio_icon = '';
  5101. $prerequisities_icon = '';
  5102. if ($is_allowed_to_edit) {
  5103. if (!$update_audio OR $update_audio <> 'true') {
  5104. $move_icon .= '<a class="moved" href="#">';
  5105. $move_icon .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  5106. $move_icon .= '</a>';
  5107. }
  5108. // No edit for this item types
  5109. if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
  5110. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
  5111. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=edit_item&amp;view=build&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '&amp;path_item=' . $arrLP[$i]['path'] . '">';
  5112. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5113. $edit_icon .= '</a>';
  5114. } else {
  5115. $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=edit_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '&amp;path_item=' . $arrLP[$i]['path'] . '">';
  5116. $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
  5117. $edit_icon .= '</a>';
  5118. }
  5119. }
  5120. $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=delete_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
  5121. $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
  5122. $delete_icon .= '</a>';
  5123. $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
  5124. if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
  5125. $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
  5126. $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
  5127. $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
  5128. }
  5129. }
  5130. if ($update_audio != 'true') {
  5131. $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
  5132. } else {
  5133. $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
  5134. }
  5135. $parent_id = $arrLP[$i]['parent_item_id'];
  5136. $default_data[$arrLP[$i]['id']] = $row;
  5137. $default_content[$arrLP[$i]['id']] = $arrLP[$i];
  5138. if (empty($parent_id)) {
  5139. $elements[$arrLP[$i]['id']]['data'] = $row;
  5140. $elements[$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5141. } else {
  5142. $parent_arrays = array();
  5143. if ($arrLP[$i]['depth'] > 1) {
  5144. //Getting list of parents
  5145. for($j = 0; $j < $arrLP[$i]['depth']; $j++) {
  5146. foreach($arrLP as $item) {
  5147. if ($item['id'] == $parent_id) {
  5148. if ($item['parent_item_id'] == 0) {
  5149. $parent_id = $item['id'];
  5150. break;
  5151. } else {
  5152. $parent_id = $item['parent_item_id'];
  5153. if (empty($parent_arrays)) {
  5154. $parent_arrays[] = intval($item['id']);
  5155. }
  5156. $parent_arrays[] = $parent_id;
  5157. break;
  5158. }
  5159. }
  5160. }
  5161. }
  5162. }
  5163. if (!empty($parent_arrays)) {
  5164. $parent_arrays = array_reverse($parent_arrays);
  5165. $val = '$elements';
  5166. $x = 0;
  5167. foreach($parent_arrays as $item) {
  5168. if ($x != count($parent_arrays) -1) {
  5169. $val .= '["'.$item.'"]["children"]';
  5170. } else {
  5171. $val .= '["'.$item.'"]["children"]';
  5172. }
  5173. $x++;
  5174. }
  5175. $val .= "";
  5176. $code_str = $val."[".$arrLP[$i]['id']."][\"load_data\"] = '".$arrLP[$i]['id']."' ; ";
  5177. eval($code_str);
  5178. } else {
  5179. $elements[$parent_id]['children'][$arrLP[$i]['id']]['data'] = $row;
  5180. $elements[$parent_id]['children'][$arrLP[$i]['id']]['type'] = $arrLP[$i]['item_type'];
  5181. }
  5182. }
  5183. }
  5184. $return .= '<div class="lp_tree well">';
  5185. $return .= '<ul id="lp_item_list">';
  5186. $return .='<h4>'.$this->name.'</h4><br>';
  5187. $tree = self::print_recursive($elements, $default_data, $default_content);
  5188. if (!empty($tree)) {
  5189. $return .= $tree;
  5190. } else {
  5191. if ($drop_element_here) {
  5192. $return .= Display::return_message(get_lang("DragAndDropAnElementHere"));
  5193. }
  5194. }
  5195. $return .= '</ul>';
  5196. if ($update_audio == 'true') {
  5197. $return = $return_audio;
  5198. } else {
  5199. $return .= '</div>';
  5200. }
  5201. return $return;
  5202. }
  5203. /**
  5204. * @param array $elements
  5205. * @param array $default_data
  5206. * @param array $default_content
  5207. * @return string
  5208. */
  5209. function print_recursive($elements, $default_data, $default_content)
  5210. {
  5211. $return = '';
  5212. foreach ($elements as $key => $item) {
  5213. if (isset($item['load_data']) || empty($item['data'])) {
  5214. $item['data'] = $default_data[$item['load_data']];
  5215. $item['type'] = $default_content[$item['load_data']]['item_type'];
  5216. }
  5217. $sub_list = '';
  5218. if (isset($item['type']) && $item['type'] == 'dokeos_chapter') {
  5219. $sub_list = Display::tag('li', '', array('class'=>'sub_item empty')); // empty value
  5220. }
  5221. if (empty($item['children'])) {
  5222. $sub_list = Display::tag('ul', $sub_list, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5223. $active = null;
  5224. if (isset($_REQUEST['id']) && $key == $_REQUEST['id']) {
  5225. $active = 'active';
  5226. }
  5227. $return .= Display::tag('li', Display::div($item['data'], array('class'=>"item_data $active")).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5228. } else {
  5229. //sections
  5230. if (isset($item['children'])) {
  5231. $data = self::print_recursive($item['children'], $default_data, $default_content);
  5232. }
  5233. $sub_list = Display::tag('ul', $sub_list.$data, array('id'=>'UL_'.$key, 'class'=>'record li_container'));
  5234. $return .= Display::tag('li', Display::div($item['data'], array('class'=>'item_data')).$sub_list, array('id'=>$key, 'class'=>'record li_container'));
  5235. }
  5236. }
  5237. return $return;
  5238. }
  5239. /**
  5240. * This function builds the action menu
  5241. * @param bool $returnContent
  5242. * @return void
  5243. */
  5244. public function build_action_menu($returnContent = false)
  5245. {
  5246. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  5247. $return = '<div class="actions">';
  5248. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;gradebook=' . $gradebook . '&action=view&lp_id=' . $_SESSION['oLP']->lp_id . '&isStudentView=true">' . Display :: return_icon('preview_view.png', get_lang('Display'),'',ICON_SIZE_MEDIUM).'</a> ';
  5249. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=admin_view&amp;lp_id=' . $_SESSION['oLP']->lp_id . '&amp;updateaudio=true">' . Display :: return_icon('upload_audio.png', get_lang('UpdateAllAudioFragments'),'',ICON_SIZE_MEDIUM).'</a>';
  5250. $return .= '<a href="lp_controller.php?'.api_get_cidreq().'&amp;action=edit&amp;lp_id=' . $_SESSION['oLP']->lp_id . '">' . Display :: return_icon('settings.png', get_lang('CourseSettings'),'',ICON_SIZE_MEDIUM).'</a>';
  5251. $buttons = array(
  5252. array(
  5253. 'title' => get_lang('SetPrerequisiteForEachItem'),
  5254. 'href' => 'lp_controller.php?'.api_get_cidreq().'&amp;action=set_previous_step_as_prerequisite&amp;lp_id=' . $_SESSION['oLP']->lp_id,
  5255. ),
  5256. array(
  5257. 'title' => get_lang('ClearAllPrerequisites'),
  5258. 'href' => 'lp_controller.php?'.api_get_cidreq().'&amp;action=clear_prerequisites&amp;lp_id=' . $_SESSION['oLP']->lp_id,
  5259. ),
  5260. );
  5261. $return .= Display::group_button(get_lang('PrerequisitesOptions'), $buttons);
  5262. $return .= '</div>';
  5263. if ($returnContent) {
  5264. return $return;
  5265. }
  5266. echo $return;
  5267. }
  5268. /**
  5269. * This functions builds the LP tree based on data from the database.
  5270. * @return string
  5271. * @deprecated use the return_new_tree() function
  5272. * @uses dtree.js :: necessary javascript for building this tree
  5273. */
  5274. public function build_tree()
  5275. {
  5276. $course_id = api_get_course_int_id();
  5277. $return = "<script type=\"text/javascript\">\n";
  5278. $return .= "\tm = new dTree('m');\n\n";
  5279. $return .= "\tm.config.folderLinks = true;\n";
  5280. $return .= "\tm.config.useCookies = true;\n";
  5281. $return .= "\tm.config.useIcons = true;\n";
  5282. $return .= "\tm.config.useLines = true;\n";
  5283. $return .= "\tm.config.useSelection = true;\n";
  5284. $return .= "\tm.config.useStatustext = false;\n\n";
  5285. $menu = 0;
  5286. $parent = '';
  5287. $return .= "\tm.add(" . $menu . ", -1, '" . addslashes(Security::remove_XSS(($this->name))) . "');\n";
  5288. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5289. $sql = " SELECT
  5290. id,
  5291. title,
  5292. description,
  5293. item_type,
  5294. path,
  5295. parent_item_id,
  5296. previous_item_id,
  5297. next_item_id,
  5298. max_score,
  5299. min_score,
  5300. mastery_score,
  5301. display_order
  5302. FROM $tbl_lp_item
  5303. WHERE c_id = ".$course_id." AND lp_id = " . intval($this->lp_id);
  5304. $result = Database::query($sql);
  5305. $arrLP = array ();
  5306. while ($row = Database :: fetch_array($result)) {
  5307. $row['title'] = Security :: remove_XSS($row['title']);
  5308. $row['description'] = Security :: remove_XSS($row['description']);
  5309. $arrLP[] = array(
  5310. 'id' => $row['id'],
  5311. 'item_type' => $row['item_type'],
  5312. 'title' => $row['title'],
  5313. 'path' => $row['path'],
  5314. 'description' => $row['description'],
  5315. 'parent_item_id' => $row['parent_item_id'],
  5316. 'previous_item_id' => $row['previous_item_id'],
  5317. 'next_item_id' => $row['next_item_id'],
  5318. 'max_score' => $row['max_score'],
  5319. 'min_score' => $row['min_score'],
  5320. 'mastery_score' => $row['mastery_score'],
  5321. 'display_order' => $row['display_order']
  5322. );
  5323. }
  5324. $this->tree_array($arrLP);
  5325. $arrLP = $this->arrMenu;
  5326. unset ($this->arrMenu);
  5327. for ($i = 0; $i < count($arrLP); $i++) {
  5328. $title = addslashes($arrLP[$i]['title']);
  5329. $menu_page = api_get_self() . '?cidReq=' . Security :: remove_XSS($_GET['cidReq']) . '&amp;action=view_item&amp;id=' . $arrLP[$i]['id'] . '&amp;lp_id=' . $_SESSION['oLP']->lp_id;
  5330. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  5331. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  5332. $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";
  5333. } else
  5334. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  5335. $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";
  5336. } else {
  5337. $return .= "\tm.add(" . $arrLP[$i]['id'] . ", " . $arrLP[$i]['parent_item_id'] . ", '" . $title . "', '" . $menu_page . "', '', '', '../img/folder_document.gif', '../img/folder_document.gif');\n";
  5338. }
  5339. if ($menu < $arrLP[$i]['id'])
  5340. $menu = $arrLP[$i]['id'];
  5341. }
  5342. $return .= "\n\tdocument.write(m);\n";
  5343. $return .= "\t if(!m.selectedNode) m.s(1);";
  5344. $return .= "</script>\n";
  5345. return $return;
  5346. }
  5347. /**
  5348. * Creates the default learning path folder
  5349. * @param array $course
  5350. * @return bool
  5351. */
  5352. public static function generate_learning_path_folder($course) {
  5353. //Creating learning_path folder
  5354. $dir = '/learning_path';
  5355. $filepath = api_get_path(SYS_COURSE_PATH).$course['path'] . '/document';
  5356. $folder = false;
  5357. if (!is_dir($filepath.'/'.$dir)) {
  5358. $folderData = create_unexisting_directory(
  5359. $course,
  5360. api_get_user_id(),
  5361. api_get_session_id(),
  5362. 0,
  5363. 0,
  5364. $filepath,
  5365. $dir,
  5366. get_lang('LearningPaths'),
  5367. 0
  5368. );
  5369. if (!empty($folderData)) {
  5370. $folder = true;
  5371. }
  5372. } else {
  5373. $folder = true;
  5374. }
  5375. return $folder;
  5376. }
  5377. /**
  5378. * @param array $course
  5379. * @param string $lp_name
  5380. * @return array
  5381. */
  5382. public function generate_lp_folder($course, $lp_name = null)
  5383. {
  5384. $filepath = '';
  5385. $dir = '/learning_path/';
  5386. if (empty($lp_name)) {
  5387. $lp_name = $this->name;
  5388. }
  5389. $folder = self::generate_learning_path_folder($course);
  5390. // Creating LP folder
  5391. if ($folder) {
  5392. //Limits title size
  5393. $title = api_substr(replace_dangerous_char($lp_name), 0 , 80);
  5394. $dir = $dir.$title;
  5395. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document';
  5396. if (!is_dir($filepath.'/'.$dir)) {
  5397. $folderData = create_unexisting_directory(
  5398. $course,
  5399. api_get_user_id(),
  5400. 0,
  5401. 0,
  5402. 0,
  5403. $filepath,
  5404. $dir,
  5405. $lp_name
  5406. );
  5407. if (!empty($folderData)) {
  5408. $folder = true;
  5409. }
  5410. } else {
  5411. $folder = true;
  5412. }
  5413. $dir = $dir.'/';
  5414. if ($folder) {
  5415. $filepath = api_get_path(SYS_COURSE_PATH) . $course['path'] . '/document'.$dir;
  5416. }
  5417. }
  5418. $array = array(
  5419. 'dir' => $dir,
  5420. 'filepath' => $filepath,
  5421. 'folder' => $folder
  5422. );
  5423. return $array;
  5424. }
  5425. /**
  5426. * Create a new document //still needs some finetuning
  5427. * @param array $_course
  5428. * @return string
  5429. */
  5430. public function create_document($_course) {
  5431. $course_id = api_get_course_int_id();
  5432. global $charset;
  5433. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir']; // Please, do not modify this dirname formatting.
  5434. if (strstr($dir, '..'))
  5435. $dir = '/';
  5436. if ($dir[0] == '.')
  5437. $dir = substr($dir, 1);
  5438. if ($dir[0] != '/')
  5439. $dir = '/' . $dir;
  5440. if ($dir[strlen($dir) - 1] != '/')
  5441. $dir .= '/';
  5442. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5443. if (empty($_POST['dir']) && empty($_GET['dir'])) {
  5444. //Generates folder
  5445. $result = $this->generate_lp_folder($_course);
  5446. $dir = $result['dir'];
  5447. $filepath = $result['filepath'];
  5448. }
  5449. if (!is_dir($filepath)) {
  5450. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5451. $dir = '/';
  5452. }
  5453. // stripslashes() before calling replace_dangerous_char() because $_POST['title']
  5454. // is already escaped twice when it gets here.
  5455. $title = replace_dangerous_char(stripslashes($_POST['title']));
  5456. $title = disable_dangerous_file($title);
  5457. $filename = $title;
  5458. $content = $_POST['content_lp'];
  5459. $tmp_filename = $filename;
  5460. $i = 0;
  5461. while (file_exists($filepath . $tmp_filename . '.html'))
  5462. $tmp_filename = $filename . '_' . ++ $i;
  5463. $filename = $tmp_filename . '.html';
  5464. $content = stripslashes($content);
  5465. $content = str_replace(api_get_path(WEB_COURSE_PATH), api_get_path(REL_PATH).'courses/', $content);
  5466. // Change the path of mp3 to absolute.
  5467. // The first regexp deals with :// urls.
  5468. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5469. // The second regexp deals with audio/ urls.
  5470. $content = preg_replace("|(flashvars=\"file=)([^/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5471. // For flv player: To prevent edition problem with firefox, we have to use a strange tip (don't blame me please).
  5472. $content = str_replace('</body>', '<style type="text/css">body{}</style></body>', $content);
  5473. if (!file_exists($filepath . $filename)) {
  5474. if ($fp = @ fopen($filepath . $filename, 'w')) {
  5475. fputs($fp, $content);
  5476. fclose($fp);
  5477. $file_size = filesize($filepath . $filename);
  5478. $save_file_path = $dir.$filename;
  5479. $document_id = add_document(
  5480. $_course,
  5481. $save_file_path,
  5482. 'file',
  5483. $file_size,
  5484. $tmp_filename
  5485. );
  5486. if ($document_id) {
  5487. api_item_property_update(
  5488. $_course,
  5489. TOOL_DOCUMENT,
  5490. $document_id,
  5491. 'DocumentAdded',
  5492. api_get_user_id(),
  5493. null,
  5494. null,
  5495. null,
  5496. null,
  5497. api_get_session_id()
  5498. );
  5499. $new_comment = (isset($_POST['comment'])) ? trim($_POST['comment']) : '';
  5500. $new_title = (isset($_POST['title'])) ? trim($_POST['title']) : '';
  5501. if ($new_comment || $new_title) {
  5502. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5503. $ct = '';
  5504. if ($new_comment)
  5505. $ct .= ", comment='" . Database::escape_string($new_comment). "'";
  5506. if ($new_title)
  5507. $ct .= ", title='" . Database::escape_string(htmlspecialchars($new_title, ENT_QUOTES, $charset))."' ";
  5508. $sql_update = "UPDATE " . $tbl_doc ." SET " . substr($ct, 1)." WHERE c_id = ".$course_id." AND id = " . $document_id;
  5509. Database::query($sql_update);
  5510. }
  5511. }
  5512. return $document_id;
  5513. }
  5514. }
  5515. }
  5516. /**
  5517. * Edit a document based on $_POST and $_GET parameters 'dir' and 'path'
  5518. * @param array $_course array
  5519. * @return void
  5520. */
  5521. public function edit_document($_course)
  5522. {
  5523. $course_id = api_get_course_int_id();
  5524. global $_configuration;
  5525. // Please, do not modify this dirname formatting.
  5526. $dir = isset ($_GET['dir']) ? $_GET['dir'] : $_POST['dir'];
  5527. if (strstr($dir, '..'))
  5528. $dir = '/';
  5529. if ($dir[0] == '.')
  5530. $dir = substr($dir, 1);
  5531. if ($dir[0] != '/')
  5532. $dir = '/' . $dir;
  5533. if ($dir[strlen($dir) - 1] != '/')
  5534. $dir .= '/';
  5535. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $dir;
  5536. if (!is_dir($filepath)) {
  5537. $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
  5538. $dir = '/';
  5539. }
  5540. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5541. if (isset($_POST['path']) && !empty($_POST['path'])) {
  5542. $document_id = intval($_POST['path']);
  5543. $sql = "SELECT path FROM " . $table_doc . "
  5544. WHERE c_id = $course_id AND id = " . $document_id;
  5545. $res = Database::query($sql);
  5546. $row = Database :: fetch_array($res);
  5547. $content = stripslashes($_POST['content_lp']);
  5548. $file = $filepath . $row['path'];
  5549. if ($fp = @ fopen($file, 'w')) {
  5550. $content = text_filter($content);
  5551. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'] . '/courses/', $content);
  5552. // Change the path of mp3 to absolute.
  5553. // The first regexp deals with :// urls.
  5554. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/', $content);
  5555. // The second regexp deals with audio/ urls.
  5556. $content = preg_replace("|(flashvars=\"file=)([^:/]+)/|", "$1" . api_get_path(REL_COURSE_PATH) . $_course['path'] . '/document/$2/', $content);
  5557. fputs($fp, $content);
  5558. fclose($fp);
  5559. $sql = "UPDATE " . $table_doc ." SET
  5560. title='".Database::escape_string($_POST['title'])."'
  5561. WHERE c_id = ".$course_id." AND id = " . $document_id;
  5562. Database::query($sql);
  5563. }
  5564. }
  5565. }
  5566. /**
  5567. * Displays the selected item, with a panel for manipulating the item
  5568. * @param int $item_id
  5569. * @param string $msg
  5570. * @return string
  5571. */
  5572. public function display_item($item_id, $msg = null, $show_actions = true)
  5573. {
  5574. $course_id = api_get_course_int_id();
  5575. $return = '';
  5576. if (is_numeric($item_id)) {
  5577. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5578. $sql = "SELECT lp.* FROM " . $tbl_lp_item . " as lp
  5579. WHERE c_id = ".$course_id." AND lp.id = " . intval($item_id);
  5580. $result = Database::query($sql);
  5581. while ($row = Database :: fetch_array($result,'ASSOC')) {
  5582. $_SESSION['parent_item_id'] = ($row['item_type'] == 'dokeos_chapter' || $row['item_type'] == 'dokeos_module' || $row['item_type'] == 'dir') ? $item_id : 0;
  5583. // Prevents wrong parent selection for document, see Bug#1251.
  5584. if ($row['item_type'] != 'dokeos_chapter' || $row['item_type'] != 'dokeos_module') {
  5585. $_SESSION['parent_item_id'] = $row['parent_item_id'];
  5586. }
  5587. if ($show_actions) {
  5588. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5589. }
  5590. $return .= '<div style="padding:10px;">';
  5591. if ($msg != '')
  5592. $return .= $msg;
  5593. $return .= '<h3>'.$row['title'].'</h3>';
  5594. switch ($row['item_type']) {
  5595. case TOOL_QUIZ:
  5596. if (!empty($row['path'])) {
  5597. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  5598. $exercise = new Exercise();
  5599. $exercise->read($row['path']);
  5600. $return .= $exercise->description.'<br />';
  5601. }
  5602. break;
  5603. case TOOL_DOCUMENT:
  5604. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5605. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  5606. WHERE c_id = ".$course_id." AND id = " . intval($row['path']);
  5607. $result = Database::query($sql_doc);
  5608. $path_file = Database::result($result, 0, 0);
  5609. $path_parts = pathinfo($path_file);
  5610. // TODO: Correct the following naive comparisons, also, htm extension is missing.
  5611. if (in_array($path_parts['extension'], array(
  5612. 'html',
  5613. 'txt',
  5614. 'png',
  5615. 'jpg',
  5616. 'JPG',
  5617. 'jpeg',
  5618. 'JPEG',
  5619. 'gif',
  5620. 'swf'
  5621. ))) {
  5622. $return .= $this->display_document($row['path'], true, true);
  5623. }
  5624. break;
  5625. }
  5626. $return .= '</div>';
  5627. }
  5628. }
  5629. return $return;
  5630. }
  5631. /**
  5632. * Shows the needed forms for editing a specific item
  5633. * @param int $item_id
  5634. * @return string
  5635. */
  5636. public function display_edit_item($item_id)
  5637. {
  5638. $course_id = api_get_course_int_id();
  5639. $return = '';
  5640. if (is_numeric($item_id)) {
  5641. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5642. $sql = "SELECT * FROM $tbl_lp_item
  5643. WHERE c_id = ".$course_id." AND id = " . intval($item_id);
  5644. $res = Database::query($sql);
  5645. $row = Database::fetch_array($res);
  5646. switch ($row['item_type']) {
  5647. case 'dokeos_chapter' :
  5648. case 'dir' :
  5649. case 'asset' :
  5650. case 'sco' :
  5651. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5652. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5653. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', 'edit', $item_id, $row);
  5654. } else {
  5655. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentChapter') . ' :', $row);
  5656. }
  5657. break;
  5658. case TOOL_DOCUMENT :
  5659. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5660. $sql_step = " SELECT lp.*, doc.path as dir
  5661. FROM " . $tbl_lp_item . " as lp
  5662. LEFT JOIN " . $tbl_doc . " as doc ON doc.id = lp.path
  5663. WHERE
  5664. lp.c_id = $course_id AND
  5665. doc.c_id = $course_id AND
  5666. lp.id = " . intval($item_id);
  5667. $res_step = Database::query($sql_step);
  5668. $row_step = Database :: fetch_array($res_step);
  5669. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5670. $return .= $this->display_document_form('edit', $item_id, $row_step);
  5671. break;
  5672. case TOOL_LINK :
  5673. $link_id = (string) $row['path'];
  5674. if (ctype_digit($link_id)) {
  5675. $tbl_link = Database :: get_course_table(TABLE_LINK);
  5676. $sql_select = 'SELECT url FROM ' . $tbl_link . '
  5677. WHERE c_id = '.$course_id.' AND id = ' . intval($link_id);
  5678. $res_link = Database::query($sql_select);
  5679. $row_link = Database :: fetch_array($res_link);
  5680. if (is_array($row_link)) {
  5681. $row['url'] = $row_link['url'];
  5682. }
  5683. }
  5684. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5685. $return .= $this->display_link_form('edit', $item_id, $row);
  5686. break;
  5687. case 'dokeos_module' :
  5688. if (isset ($_GET['view']) && $_GET['view'] == 'build') {
  5689. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5690. $return .= $this->display_item_form($row['item_type'], get_lang('EditCurrentModule') . ' :', 'edit', $item_id, $row);
  5691. } else {
  5692. $return .= $this->display_item_small_form($row['item_type'], get_lang('EditCurrentModule') . ' :', $row);
  5693. }
  5694. break;
  5695. case TOOL_QUIZ :
  5696. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5697. $return .= $this->display_quiz_form('edit', $item_id, $row);
  5698. break;
  5699. case TOOL_HOTPOTATOES :
  5700. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5701. $return .= $this->display_hotpotatoes_form('edit', $item_id, $row);
  5702. break;
  5703. case TOOL_STUDENTPUBLICATION :
  5704. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5705. $return .= $this->display_student_publication_form('edit', $item_id, $row);
  5706. break;
  5707. case TOOL_FORUM :
  5708. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5709. $return .= $this->display_forum_form('edit', $item_id, $row);
  5710. break;
  5711. case TOOL_THREAD :
  5712. $return .= $this->display_manipulate($item_id, $row['item_type']);
  5713. $return .= $this->display_thread_form('edit', $item_id, $row);
  5714. break;
  5715. }
  5716. }
  5717. return $return;
  5718. }
  5719. /**
  5720. * Function that displays a list with al the resources that
  5721. * could be added to the learning path
  5722. * @return string
  5723. */
  5724. public function display_resources()
  5725. {
  5726. $course_code = api_get_course_id();
  5727. // Get all the docs.
  5728. $documents = $this->get_documents(true);
  5729. // Get all the exercises.
  5730. $exercises = $this->get_exercises();
  5731. // Get all the links.
  5732. $links = $this->get_links();
  5733. // Get al the student publications.
  5734. $works = $this->get_student_publications();
  5735. // Get al the forums.
  5736. $forums = $this->get_forums(null, $course_code);
  5737. $headers = array(
  5738. Display::return_icon('folder_document.png', get_lang('Documents'), array(), ICON_SIZE_BIG),
  5739. Display::return_icon('quiz.png', get_lang('Quiz'), array(), ICON_SIZE_BIG),
  5740. Display::return_icon('links.png', get_lang('Links'), array(), ICON_SIZE_BIG),
  5741. Display::return_icon('works.png', get_lang('Works'), array(), ICON_SIZE_BIG),
  5742. Display::return_icon('forum.png', get_lang('Forums'), array(), ICON_SIZE_BIG),
  5743. Display::return_icon('add_learnpath_section.png', get_lang('NewChapter'), array(), ICON_SIZE_BIG)
  5744. );
  5745. echo Display::display_normal_message(get_lang('ClickOnTheLearnerViewToSeeYourLearningPath'));
  5746. $chapter = $_SESSION['oLP']->display_item_form('chapter', get_lang('EnterDataNewChapter'), 'add_item');
  5747. echo Display::tabs(
  5748. $headers,
  5749. array($documents, $exercises, $links, $works, $forums, $chapter), 'resource_tab'
  5750. );
  5751. return true;
  5752. }
  5753. /**
  5754. * Returns the extension of a document
  5755. * @param string filename
  5756. * @return string Extension (part after the last dot)
  5757. */
  5758. public function get_extension($filename)
  5759. {
  5760. $explode = explode('.', $filename);
  5761. return $explode[count($explode) - 1];
  5762. }
  5763. /**
  5764. * Displays a document by id
  5765. *
  5766. * @param int $id
  5767. * @return string
  5768. */
  5769. public function display_document($id, $show_title = false, $iframe = true, $edit_link = false)
  5770. {
  5771. global $_course; // It is temporary.
  5772. $course_id = api_get_course_int_id();
  5773. $return = '';
  5774. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  5775. $sql_doc = "SELECT * FROM " . $tbl_doc . "
  5776. WHERE c_id = ".$course_id." AND id = " . $id;
  5777. $res_doc = Database::query($sql_doc);
  5778. $row_doc = Database :: fetch_array($res_doc);
  5779. // TODO: Add a path filter.
  5780. if ($iframe) {
  5781. $return .= '<iframe id="learnpath_preview_frame" frameborder="0" height="400" width="100%" scrolling="auto" src="' . api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/document' . str_replace('%2F', '/', urlencode($row_doc['path'])) . '?' . api_get_cidreq() . '"></iframe>';
  5782. } else {
  5783. $return .= file_get_contents(api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document' . $row_doc['path']);
  5784. }
  5785. return $return;
  5786. }
  5787. /**
  5788. * Return HTML form to add/edit a quiz
  5789. * @param string Action (add/edit)
  5790. * @param integer Item ID if already exists
  5791. * @param mixed Extra information (quiz ID if integer)
  5792. * @return string HTML form
  5793. */
  5794. public function display_quiz_form($action = 'add', $id = 0, $extra_info = '')
  5795. {
  5796. $course_id = api_get_course_int_id();
  5797. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5798. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  5799. if ($id != 0 && is_array($extra_info)) {
  5800. $item_title = $extra_info['title'];
  5801. $item_description = $extra_info['description'];
  5802. } elseif (is_numeric($extra_info)) {
  5803. $sql_quiz = "SELECT title, description FROM " . $tbl_quiz . " WHERE c_id = ".$course_id." AND id = " . $extra_info;
  5804. $result = Database::query($sql_quiz);
  5805. $row = Database :: fetch_array($result);
  5806. $item_title = $row['title'];
  5807. $item_description = $row['description'];
  5808. } else {
  5809. $item_title = '';
  5810. $item_description = '';
  5811. }
  5812. $item_title = Security::remove_XSS($item_title);
  5813. $item_description = Security::remove_XSS($item_description);
  5814. $legend = '<legend>';
  5815. if ($id != 0 && is_array($extra_info))
  5816. $parent = $extra_info['parent_item_id'];
  5817. else
  5818. $parent = 0;
  5819. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  5820. $result = Database::query($sql);
  5821. $arrLP = array ();
  5822. while ($row = Database :: fetch_array($result)) {
  5823. $arrLP[] = array (
  5824. 'id' => $row['id'],
  5825. 'item_type' => $row['item_type'],
  5826. 'title' => $row['title'],
  5827. 'path' => $row['path'],
  5828. 'description' => $row['description'],
  5829. 'parent_item_id' => $row['parent_item_id'],
  5830. 'previous_item_id' => $row['previous_item_id'],
  5831. 'next_item_id' => $row['next_item_id'],
  5832. 'display_order' => $row['display_order'],
  5833. 'max_score' => $row['max_score'],
  5834. 'min_score' => $row['min_score'],
  5835. 'mastery_score' => $row['mastery_score'],
  5836. 'prerequisite' => $row['prerequisite'],
  5837. 'max_time_allowed' => $row['max_time_allowed']
  5838. );
  5839. }
  5840. $this->tree_array($arrLP);
  5841. $arrLP = $this->arrMenu;
  5842. unset ($this->arrMenu);
  5843. if ($action == 'add') {
  5844. $legend .= get_lang('CreateTheExercise') . '&nbsp;:';
  5845. } elseif ($action == 'move') {
  5846. $legend .= get_lang('MoveTheCurrentExercise') . '&nbsp;:';
  5847. } else {
  5848. $legend .= get_lang('EditCurrentExecice') . '&nbsp;:';
  5849. }
  5850. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  5851. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  5852. }
  5853. $legend .= '</legend>';
  5854. $return = '';
  5855. $return .= '<div class="sectioncomment">';
  5856. $return .= '<form method="POST">';
  5857. $return .= $legend;
  5858. $return .= '<table class="lp_form">';
  5859. if ($action != 'move') {
  5860. $return .= '<tr>';
  5861. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  5862. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" /></td>';
  5863. $return .= '</tr>';
  5864. }
  5865. $return .= '<tr>';
  5866. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  5867. $return .= '<td class="input">';
  5868. // Select for Parent item, root or chapter
  5869. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  5870. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  5871. $arrHide = array (
  5872. $id
  5873. );
  5874. //$parent_item_id = $_SESSION['parent_item_id'];
  5875. for ($i = 0; $i < count($arrLP); $i++) {
  5876. if ($action != 'add') {
  5877. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  5878. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5879. } else {
  5880. $arrHide[] = $arrLP[$i]['id'];
  5881. }
  5882. } else {
  5883. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  5884. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  5885. }
  5886. }
  5887. if (is_array($arrLP)) {
  5888. reset($arrLP);
  5889. }
  5890. $return .= '</select>';
  5891. $return .= '</td>';
  5892. $return .= '</tr>';
  5893. $return .= '<tr>';
  5894. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  5895. $return .= '<td class="input">';
  5896. $return .= '<select class="learnpath_item_form" style="width:100%;" id="previous" name="previous" size="1">';
  5897. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  5898. for ($i = 0; $i < count($arrLP); $i++) {
  5899. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  5900. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5901. $selected = 'selected="selected" ';
  5902. elseif ($action == 'add') $selected = 'selected="selected" ';
  5903. else
  5904. $selected = '';
  5905. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  5906. }
  5907. }
  5908. $return .= '</select>';
  5909. $return .= '</td>';
  5910. $return .= '</tr>';
  5911. if ($action != 'move') {
  5912. $id_prerequisite = 0;
  5913. if (is_array($arrLP)) {
  5914. foreach ($arrLP as $key => $value) {
  5915. if ($value['id'] == $id) {
  5916. $id_prerequisite = $value['prerequisite'];
  5917. break;
  5918. }
  5919. }
  5920. }
  5921. $arrHide = array ();
  5922. for ($i = 0; $i < count($arrLP); $i++) {
  5923. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  5924. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  5925. $s_selected_position = $arrLP[$i]['id'];
  5926. elseif ($action == 'add') $s_selected_position = 0;
  5927. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  5928. }
  5929. }
  5930. /*// Commented the prerequisites, only visible in edit (exercise).
  5931. $return .= '<tr>';
  5932. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  5933. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  5934. foreach($arrHide as $key => $value){
  5935. if($key==$s_selected_position && $action == 'add'){
  5936. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5937. }
  5938. elseif($key==$id_prerequisite && $action == 'edit'){
  5939. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  5940. }
  5941. else{
  5942. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  5943. }
  5944. }
  5945. $return .= "</select></td>";
  5946. */
  5947. $return .= '</tr>';
  5948. /*$return .= '<tr>';
  5949. $return .= '<td class="label"><label for="maxTimeAllowed">' . get_lang('MaxTimeAllowed') . '</label></td>';
  5950. $return .= '<td class="input"><input name="maxTimeAllowed" style="width:98%;" id="maxTimeAllowed" value="' . $extra_info['max_time_allowed'] . '" /></td>';
  5951. // Remove temporarily the test description.
  5952. //$return .= '<td class="label"><label for="idDescription">'.get_lang('Description').' :</label></td>';
  5953. //$return .= '<td class="input"><textarea id="idDescription" name="description" rows="4">' . $item_description . '</textarea></td>';
  5954. $return .= '</tr>'; */
  5955. }
  5956. $return .= '<tr>';
  5957. if ($action == 'add') {
  5958. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddExercise') . '</button></td>';
  5959. } else {
  5960. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentExecice') . '</button></td>';
  5961. }
  5962. $return .= '</tr>';
  5963. $return .= '</table>';
  5964. if ($action == 'move') {
  5965. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  5966. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  5967. }
  5968. if (is_numeric($extra_info)) {
  5969. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  5970. } elseif (is_array($extra_info)) {
  5971. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  5972. }
  5973. $return .= '<input name="type" type="hidden" value="' . TOOL_QUIZ . '" />';
  5974. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  5975. $return .= '</form>';
  5976. $return .= '</div>';
  5977. return $return;
  5978. }
  5979. /**
  5980. * Addition of Hotpotatoes tests
  5981. * @param string Action
  5982. * @param integer Internal ID of the item
  5983. * @param mixed Extra information - can be an array with title and description indexes
  5984. * @return string HTML structure to display the hotpotatoes addition formular
  5985. */
  5986. public function display_hotpotatoes_form($action = 'add', $id = 0, $extra_info = '') {
  5987. $course_id = api_get_course_int_id();
  5988. global $charset;
  5989. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  5990. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  5991. if ($id != 0 && is_array($extra_info)) {
  5992. $item_title = stripslashes($extra_info['title']);
  5993. $item_description = stripslashes($extra_info['description']);
  5994. } elseif (is_numeric($extra_info)) {
  5995. $TBL_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  5996. $sql_hot = "SELECT * FROM " . $TBL_DOCUMENT . "
  5997. WHERE c_id = ".$course_id." AND
  5998. path LIKE '" . $uploadPath . "/%/%htm%' AND
  5999. id = " . (int) $extra_info . "
  6000. ORDER BY id ASC";
  6001. $res_hot = Database::query($sql_hot);
  6002. $row = Database::fetch_array($res_hot);
  6003. $item_title = $row['title'];
  6004. $item_description = $row['description'];
  6005. if (!empty ($row['comment'])) {
  6006. $item_title = $row['comment'];
  6007. }
  6008. } else {
  6009. $item_title = '';
  6010. $item_description = '';
  6011. }
  6012. if ($id != 0 && is_array($extra_info))
  6013. $parent = $extra_info['parent_item_id'];
  6014. else
  6015. $parent = 0;
  6016. $sql = "SELECT * FROM $tbl_lp_item
  6017. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6018. $result = Database::query($sql);
  6019. $arrLP = array ();
  6020. while ($row = Database :: fetch_array($result)) {
  6021. $arrLP[] = array (
  6022. 'id' => $row['id'],
  6023. 'item_type' => $row['item_type'],
  6024. 'title' => $row['title'],
  6025. 'path' => $row['path'],
  6026. 'description' => $row['description'],
  6027. 'parent_item_id' => $row['parent_item_id'],
  6028. 'previous_item_id' => $row['previous_item_id'],
  6029. 'next_item_id' => $row['next_item_id'],
  6030. 'display_order' => $row['display_order'],
  6031. 'max_score' => $row['max_score'],
  6032. 'min_score' => $row['min_score'],
  6033. 'mastery_score' => $row['mastery_score'],
  6034. 'prerequisite' => $row['prerequisite'],
  6035. 'max_time_allowed' => $row['max_time_allowed']
  6036. );
  6037. }
  6038. $legend = '<legend>';
  6039. if ($action == 'add')
  6040. $legend .= get_lang('CreateTheExercise');
  6041. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentExercise');
  6042. else
  6043. $legend .= get_lang('EditCurrentExecice');
  6044. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6045. $legend .= Display :: return_warning_message(get_lang('Warning') . ' ! ' . get_lang('WarningEditingDocument'));
  6046. }
  6047. $legend .= '</legend>';
  6048. $return = '<form method="POST">';
  6049. $return .= $legend;
  6050. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6051. $return .= '<tr>';
  6052. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . ' :</label></td>';
  6053. $return .= '<td class="input">';
  6054. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6055. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6056. $arrHide = array (
  6057. $id
  6058. );
  6059. if (count($arrLP) > 0) {
  6060. for ($i = 0; $i < count($arrLP); $i++) {
  6061. if ($action != 'add') {
  6062. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6063. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6064. } else {
  6065. $arrHide[] = $arrLP[$i]['id'];
  6066. }
  6067. } else {
  6068. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6069. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6070. }
  6071. }
  6072. reset($arrLP);
  6073. }
  6074. $return .= '</select>';
  6075. $return .= '</td>';
  6076. $return .= '</tr>';
  6077. $return .= '<tr>';
  6078. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . ' :</label></td>';
  6079. $return .= '<td class="input">';
  6080. $return .= '<select id="previous" name="previous" size="1">';
  6081. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6082. for ($i = 0; $i < count($arrLP); $i++) {
  6083. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6084. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6085. $selected = 'selected="selected" ';
  6086. elseif ($action == 'add') $selected = 'selected="selected" ';
  6087. else
  6088. $selected = '';
  6089. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6090. }
  6091. }
  6092. $return .= '</select>';
  6093. $return .= '</td>';
  6094. $return .= '</tr>';
  6095. if ($action != 'move') {
  6096. $return .= '<tr>';
  6097. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . ' :</label></td>';
  6098. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6099. $return .= '</tr>';
  6100. $id_prerequisite = 0;
  6101. if (is_array($arrLP) && count($arrLP) > 0) {
  6102. foreach ($arrLP as $key => $value) {
  6103. if ($value['id'] == $id) {
  6104. $id_prerequisite = $value['prerequisite'];
  6105. break;
  6106. }
  6107. }
  6108. $arrHide = array ();
  6109. for ($i = 0; $i < count($arrLP); $i++) {
  6110. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6111. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6112. $s_selected_position = $arrLP[$i]['id'];
  6113. elseif ($action == 'add') $s_selected_position = 0;
  6114. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6115. }
  6116. }
  6117. }
  6118. }
  6119. $return .= '<tr>';
  6120. $return .= '<td>&nbsp; </td><td><button class="save" name="submit_button" action="edit" type="submit">' . get_lang('SaveHotpotatoes') . '</button></td>';
  6121. $return .= '</tr>';
  6122. $return .= '</table>';
  6123. if ($action == 'move') {
  6124. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6125. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6126. }
  6127. if (is_numeric($extra_info)) {
  6128. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6129. } elseif (is_array($extra_info)) {
  6130. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6131. }
  6132. $return .= '<input name="type" type="hidden" value="' . TOOL_HOTPOTATOES . '" />';
  6133. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6134. $return .= '</form>';
  6135. return $return;
  6136. }
  6137. /**
  6138. * Return the form to display the forum edit/add option
  6139. * @param string Action (add/edit)
  6140. * @param integer ID of the lp_item if already exists
  6141. * @param mixed Forum ID or title
  6142. * @return string HTML form
  6143. */
  6144. public function display_forum_form($action = 'add', $id = 0, $extra_info = '')
  6145. {
  6146. $course_id = api_get_course_int_id();
  6147. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6148. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  6149. if ($id != 0 && is_array($extra_info)) {
  6150. $item_title = stripslashes($extra_info['title']);
  6151. } elseif (is_numeric($extra_info)) {
  6152. $sql_forum = "SELECT forum_title as title, forum_comment as comment
  6153. FROM " . $tbl_forum . "
  6154. WHERE c_id = ".$course_id." AND forum_id = " . $extra_info;
  6155. $result = Database::query($sql_forum);
  6156. $row = Database :: fetch_array($result);
  6157. $item_title = $row['title'];
  6158. $item_description = $row['comment'];
  6159. } else {
  6160. $item_title = '';
  6161. $item_description = '';
  6162. }
  6163. $legend = '<legend>';
  6164. if ($id != 0 && is_array($extra_info))
  6165. $parent = $extra_info['parent_item_id'];
  6166. else
  6167. $parent = 0;
  6168. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6169. WHERE c_id = ".$course_id." AND
  6170. lp_id = " . $this->lp_id;
  6171. $result = Database::query($sql);
  6172. $arrLP = array();
  6173. while ($row = Database :: fetch_array($result)) {
  6174. $arrLP[] = array (
  6175. 'id' => $row['id'],
  6176. 'item_type' => $row['item_type'],
  6177. 'title' => $row['title'],
  6178. 'path' => $row['path'],
  6179. 'description' => $row['description'],
  6180. 'parent_item_id' => $row['parent_item_id'],
  6181. 'previous_item_id' => $row['previous_item_id'],
  6182. 'next_item_id' => $row['next_item_id'],
  6183. 'display_order' => $row['display_order'],
  6184. 'max_score' => $row['max_score'],
  6185. 'min_score' => $row['min_score'],
  6186. 'mastery_score' => $row['mastery_score'],
  6187. 'prerequisite' => $row['prerequisite']
  6188. );
  6189. }
  6190. $this->tree_array($arrLP);
  6191. $arrLP = $this->arrMenu;
  6192. unset ($this->arrMenu);
  6193. if ($action == 'add')
  6194. $legend .= get_lang('CreateTheForum') . '&nbsp;:';
  6195. elseif ($action == 'move') $legend .= get_lang('MoveTheCurrentForum') . '&nbsp;:';
  6196. else
  6197. $legend .= get_lang('EditCurrentForum') . '&nbsp;:';
  6198. $legend .= '</legend>';
  6199. $return = '<div class="sectioncomment">';
  6200. $return .= '<form method="POST">';
  6201. $return .= $legend;
  6202. $return .= '<table class="lp_form">';
  6203. if ($action != 'move') {
  6204. $return .= '<tr>';
  6205. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6206. $return .= '<td class="input"><input id="idTitle" size="44" name="title" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  6207. $return .= '</tr>';
  6208. }
  6209. $return .= '<tr>';
  6210. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6211. $return .= '<td class="input">';
  6212. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  6213. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6214. $arrHide = array (
  6215. $id
  6216. );
  6217. //$parent_item_id = $_SESSION['parent_item_id'];
  6218. for ($i = 0; $i < count($arrLP); $i++) {
  6219. if ($action != 'add') {
  6220. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6221. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6222. } else {
  6223. $arrHide[] = $arrLP[$i]['id'];
  6224. }
  6225. } else {
  6226. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6227. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6228. }
  6229. }
  6230. if (is_array($arrLP)) {
  6231. reset($arrLP);
  6232. }
  6233. $return .= "\t\t\t\t" . '</select>';
  6234. $return .= '</td>';
  6235. $return .= '</tr>';
  6236. $return .= '<tr>';
  6237. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6238. $return .= '<td class="input">';
  6239. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  6240. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6241. for ($i = 0; $i < count($arrLP); $i++) {
  6242. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6243. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6244. $selected = 'selected="selected" ';
  6245. elseif ($action == 'add') $selected = 'selected="selected" ';
  6246. else
  6247. $selected = '';
  6248. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6249. }
  6250. }
  6251. $return .= "\t\t\t\t" . '</select>';
  6252. $return .= '</td>';
  6253. $return .= '</tr>';
  6254. if ($action != 'move') {
  6255. $return .= '<tr>';
  6256. $return .= '</tr>';
  6257. $id_prerequisite = 0;
  6258. if (is_array($arrLP)) {
  6259. foreach ($arrLP as $key => $value) {
  6260. if ($value['id'] == $id) {
  6261. $id_prerequisite = $value['prerequisite'];
  6262. break;
  6263. }
  6264. }
  6265. }
  6266. $arrHide = array ();
  6267. for ($i = 0; $i < count($arrLP); $i++) {
  6268. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6269. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6270. $s_selected_position = $arrLP[$i]['id'];
  6271. elseif ($action == 'add') $s_selected_position = 0;
  6272. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6273. }
  6274. }
  6275. $return .= '</tr>';
  6276. }
  6277. $return .= '<tr>';
  6278. if ($action == 'add') {
  6279. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('AddForumToCourse') . ' </button></td>';
  6280. } else {
  6281. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit"> ' . get_lang('EditCurrentForum') . ' </button></td>';
  6282. }
  6283. $return .= '</tr>';
  6284. $return .= '</table>';
  6285. if ($action == 'move') {
  6286. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6287. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6288. }
  6289. if (is_numeric($extra_info)) {
  6290. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6291. }
  6292. elseif (is_array($extra_info)) {
  6293. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6294. }
  6295. $return .= '<input name="type" type="hidden" value="' . TOOL_FORUM . '" />';
  6296. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6297. $return .= '</form>';
  6298. $return .= '</div>';
  6299. return $return;
  6300. }
  6301. /**
  6302. * Return HTML form to add/edit forum threads
  6303. * @param string Action (add/edit)
  6304. * @param integer Item ID if already exists in learning path
  6305. * @param mixed Extra information (thread ID if integer)
  6306. * @return string HTML form
  6307. */
  6308. public function display_thread_form($action = 'add', $id = 0, $extra_info = '')
  6309. {
  6310. $course_id = api_get_course_int_id();
  6311. if (empty($course_id)) {
  6312. return null;
  6313. }
  6314. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6315. $tbl_forum = Database :: get_course_table(TABLE_FORUM_THREAD);
  6316. if ($id != 0 && is_array($extra_info)) {
  6317. $item_title = stripslashes($extra_info['title']);
  6318. } elseif (is_numeric($extra_info)) {
  6319. $sql_forum = "SELECT thread_title as title FROM $tbl_forum
  6320. WHERE c_id = $course_id AND thread_id = " . $extra_info;
  6321. $result = Database::query($sql_forum);
  6322. $row = Database :: fetch_array($result);
  6323. $item_title = $row['title'];
  6324. $item_description = '';
  6325. } else {
  6326. $item_title = '';
  6327. $item_description = '';
  6328. }
  6329. $return = null;
  6330. if ($id != 0 && is_array($extra_info))
  6331. $parent = $extra_info['parent_item_id'];
  6332. else
  6333. $parent = 0;
  6334. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6335. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6336. $result = Database::query($sql);
  6337. $arrLP = array ();
  6338. while ($row = Database :: fetch_array($result)) {
  6339. $arrLP[] = array (
  6340. 'id' => $row['id'],
  6341. 'item_type' => $row['item_type'],
  6342. 'title' => $row['title'],
  6343. 'path' => $row['path'],
  6344. 'description' => $row['description'],
  6345. 'parent_item_id' => $row['parent_item_id'],
  6346. 'previous_item_id' => $row['previous_item_id'],
  6347. 'next_item_id' => $row['next_item_id'],
  6348. 'display_order' => $row['display_order'],
  6349. 'max_score' => $row['max_score'],
  6350. 'min_score' => $row['min_score'],
  6351. 'mastery_score' => $row['mastery_score'],
  6352. 'prerequisite' => $row['prerequisite']
  6353. );
  6354. }
  6355. $this->tree_array($arrLP);
  6356. $arrLP = $this->arrMenu;
  6357. unset ($this->arrMenu);
  6358. $return .= '<form method="POST">';
  6359. if ($action == 'add')
  6360. $return .= '<legend>' . get_lang('CreateTheForum') . '</legend>';
  6361. elseif ($action == 'move') $return .= '<p class="lp_title">' . get_lang('MoveTheCurrentForum') . '&nbsp;:</p>';
  6362. else
  6363. $return .= '<legend>' . get_lang('EditCurrentForum') . '</legend>';
  6364. $return .= '<table cellpadding="0" cellspacing="0" class="lp_form">';
  6365. $return .= '<tr>';
  6366. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  6367. $return .= '<td class="input">';
  6368. $return .= '<select id="idParent" name="parent" onChange="javascript: load_cbo(this.value);" size="1">';
  6369. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  6370. $arrHide = array (
  6371. $id
  6372. );
  6373. for ($i = 0; $i < count($arrLP); $i++) {
  6374. if ($action != 'add') {
  6375. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6376. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6377. } else {
  6378. $arrHide[] = $arrLP[$i]['id'];
  6379. }
  6380. } else {
  6381. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  6382. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  6383. }
  6384. }
  6385. if ($arrLP != null) {
  6386. reset($arrLP);
  6387. }
  6388. $return .= '</select>';
  6389. $return .= '</td>';
  6390. $return .= '</tr>';
  6391. $return .= '<tr>';
  6392. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  6393. $return .= '<td class="input">';
  6394. $return .= '<select id="previous" name="previous" size="1">';
  6395. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  6396. for ($i = 0; $i < count($arrLP); $i++) {
  6397. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6398. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6399. $selected = 'selected="selected" ';
  6400. elseif ($action == 'add') $selected = 'selected="selected" ';
  6401. else
  6402. $selected = '';
  6403. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  6404. }
  6405. }
  6406. $return .= "\t\t\t\t" . '</select>';
  6407. $return .= '</td>';
  6408. $return .= '</tr>';
  6409. if ($action != 'move') {
  6410. $return .= '<tr>';
  6411. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6412. $return .= '<td class="input"><input id="idTitle" name="title" type="text" value="' . $item_title . '" /></td>';
  6413. $return .= '</tr>';
  6414. $return .= '<tr>';
  6415. $return .= '</tr>';
  6416. $id_prerequisite = 0;
  6417. if ($arrLP != null) {
  6418. foreach ($arrLP as $key => $value) {
  6419. if ($value['id'] == $id) {
  6420. $id_prerequisite = $value['prerequisite'];
  6421. break;
  6422. }
  6423. }
  6424. }
  6425. $arrHide = array();
  6426. for ($i = 0; $i < count($arrLP); $i++) {
  6427. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6428. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6429. $s_selected_position = $arrLP[$i]['id'];
  6430. elseif ($action == 'add') $s_selected_position = 0;
  6431. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6432. }
  6433. }
  6434. $return .= '<tr>';
  6435. $return .= '<td class="label"><label for="idPrerequisites">' . get_lang('LearnpathPrerequisites') . '</label></td>';
  6436. $return .= '<td class="input"><select name="prerequisites" id="prerequisites"><option value="0">' . get_lang('NoPrerequisites') . '</option>';
  6437. foreach ($arrHide as $key => $value) {
  6438. if ($key == $s_selected_position && $action == 'add') {
  6439. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6440. }
  6441. elseif ($key == $id_prerequisite && $action == 'edit') {
  6442. $return .= '<option value="' . $key . '" selected="selected">' . $value['value'] . '</option>';
  6443. } else {
  6444. $return .= '<option value="' . $key . '">' . $value['value'] . '</option>';
  6445. }
  6446. }
  6447. $return .= "</select></td>";
  6448. $return .= '</tr>';
  6449. }
  6450. $return .= '<tr>';
  6451. $return .= '<td></td><td>
  6452. <button class="save" name="submit_button" type="submit" value="'.get_lang('Ok').'" />'.get_lang('Ok').'</button></td>';
  6453. $return .= '</tr>';
  6454. $return .= '</table>';
  6455. if ($action == 'move') {
  6456. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  6457. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  6458. }
  6459. if (is_numeric($extra_info)) {
  6460. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  6461. }
  6462. elseif (is_array($extra_info)) {
  6463. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  6464. }
  6465. $return .= '<input name="type" type="hidden" value="' . TOOL_THREAD . '" />';
  6466. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  6467. $return .= '</form>';
  6468. $return .= '</div>';
  6469. return $return;
  6470. }
  6471. /**
  6472. * Return the HTML form to display an item (generally a section/module item)
  6473. * @param string Item type (module/dokeos_module)
  6474. * @param string Title (optional, only when creating)
  6475. * @param string Action ('add'/'edit')
  6476. * @param integer lp_item ID
  6477. * @param mixed Extra info
  6478. * @return string HTML form
  6479. */
  6480. public function display_item_form($item_type, $title = '', $action = 'add_item', $id = 0, $extra_info = 'new')
  6481. {
  6482. $course_id = api_get_course_int_id();
  6483. global $_course;
  6484. global $charset;
  6485. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6486. if ($id != 0 && is_array($extra_info)) {
  6487. $item_title = $extra_info['title'];
  6488. $item_description = $extra_info['description'];
  6489. $item_path = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6490. $item_path_fck = '/scorm/' . $this->path . '/' . stripslashes($extra_info['path']);
  6491. } else {
  6492. $item_title = '';
  6493. $item_description = '';
  6494. $item_path_fck = '';
  6495. }
  6496. if ($id != 0 && is_array($extra_info))
  6497. $parent = $extra_info['parent_item_id'];
  6498. else
  6499. $parent = 0;
  6500. $id = intval($id);
  6501. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6502. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND id != $id";
  6503. if ($item_type == 'module')
  6504. $sql .= " AND parent_item_id = 0";
  6505. $result = Database::query($sql);
  6506. $arrLP = array ();
  6507. while ($row = Database :: fetch_array($result)) {
  6508. $arrLP[] = array (
  6509. 'id' => $row['id'],
  6510. 'item_type' => $row['item_type'],
  6511. 'title' => $row['title'],
  6512. 'path' => $row['path'],
  6513. 'description' => $row['description'],
  6514. 'parent_item_id' => $row['parent_item_id'],
  6515. 'previous_item_id' => $row['previous_item_id'],
  6516. 'next_item_id' => $row['next_item_id'],
  6517. 'max_score' => $row['max_score'],
  6518. 'min_score' => $row['min_score'],
  6519. 'mastery_score' => $row['mastery_score'],
  6520. 'prerequisite' => $row['prerequisite'],
  6521. 'display_order' => $row['display_order']
  6522. );
  6523. }
  6524. $this->tree_array($arrLP);
  6525. $arrLP = isset($this->arrMenu) ? $this->arrMenu : null;
  6526. unset ($this->arrMenu);
  6527. $gradebook = isset($_GET['gradebook']) ? Security :: remove_XSS($_GET['gradebook']) : null;
  6528. $url = api_get_self() . '?' .api_get_cidreq().'&gradeboook='.$gradebook.'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
  6529. $form = new FormValidator('form', 'POST', $url);
  6530. $defaults['title'] = api_html_entity_decode($item_title, ENT_QUOTES, $charset);
  6531. $defaults['description'] = $item_description;
  6532. $form->addElement('header', $title);
  6533. //$arrHide = array($id);
  6534. $arrHide[0]['value'] = Security :: remove_XSS($this->name);
  6535. $arrHide[0]['padding'] = 3;
  6536. $charset = api_get_system_encoding();
  6537. if ($item_type != 'module' && $item_type != 'dokeos_module') {
  6538. for ($i = 0; $i < count($arrLP); $i++) {
  6539. if ($action != 'add') {
  6540. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6541. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6542. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6543. if ($parent == $arrLP[$i]['id']) {
  6544. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6545. }
  6546. }
  6547. } else {
  6548. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6549. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6550. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6551. if ($parent == $arrLP[$i]['id']) {
  6552. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6553. }
  6554. }
  6555. }
  6556. }
  6557. if ($action != 'move') {
  6558. $form->addElement('text', 'title', get_lang('Title'), 'id="idTitle" class="learnpath_chapter_form" size="40%"');
  6559. $form->applyFilter('title', 'html_filter');
  6560. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  6561. //$form->addElement('textarea', 'description', get_lang('Description').' :', 'id="idDescription"');
  6562. } else {
  6563. $form->addElement('hidden', 'title');
  6564. }
  6565. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_chapter_form" style="width:37%;" id="idParent" onchange="javascript: load_cbo(this.value);"');
  6566. foreach ($arrHide as $key => $value) {
  6567. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6568. }
  6569. if (!empty($s_selected_parent)) {
  6570. $parent_select->setSelected($s_selected_parent);
  6571. }
  6572. }
  6573. if (is_array($arrLP)) {
  6574. reset($arrLP);
  6575. }
  6576. $arrHide = array();
  6577. // POSITION
  6578. for ($i = 0; $i < count($arrLP); $i++) {
  6579. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6580. //this is the same!
  6581. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id']) {
  6582. $s_selected_position = $arrLP[$i]['id'];
  6583. } elseif ($action == 'add') {
  6584. $s_selected_position = $arrLP[$i]['id'];
  6585. }
  6586. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6587. }
  6588. }
  6589. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_chapter_form" style="width:37%;"');
  6590. $padding = isset($value['padding']) ? $value['padding'] : 0;
  6591. $position->addOption(get_lang('FirstPosition'), 0, 'style="padding-left:' . $padding . 'px;"');
  6592. foreach ($arrHide as $key => $value) {
  6593. $position->addOption($value['value'] . '"', $key, 'style="padding-left:' . $padding . 'px;"');
  6594. }
  6595. if (!empty ($s_selected_position)) {
  6596. $position->setSelected($s_selected_position);
  6597. }
  6598. if (is_array($arrLP)) {
  6599. reset($arrLP);
  6600. }
  6601. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveSection'), 'class="save"');
  6602. if ($item_type == 'module' || $item_type == 'dokeos_module') {
  6603. $form->addElement('hidden', 'parent', '0');
  6604. }
  6605. //fix in order to use the tab
  6606. if ($item_type == 'chapter') {
  6607. $form->addElement('hidden', 'type', 'chapter');
  6608. }
  6609. $extension = null;
  6610. if (!empty($item_path)) {
  6611. $extension = pathinfo($item_path, PATHINFO_EXTENSION);
  6612. }
  6613. //assets can't be modified
  6614. //$item_type == 'asset' ||
  6615. if (( $item_type == 'sco') && ($extension == 'html' || $extension == 'htm')) {
  6616. if ($item_type == 'sco') {
  6617. $form->addElement('html', '<script type="text/javascript">alert("' . get_lang('WarningWhenEditingScorm') . '")</script>');
  6618. }
  6619. $renderer = $form->defaultRenderer();
  6620. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6621. $relative_prefix = '';
  6622. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6623. 'Width' => '100%',
  6624. 'Height' => '500',
  6625. 'FullPage' => true,
  6626. 'CreateDocumentDir' => $relative_prefix,
  6627. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/scorm/',
  6628. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().$item_path_fck
  6629. );
  6630. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6631. $content_path = (api_get_path(SYS_COURSE_PATH).api_get_course_path().$item_path_fck);
  6632. //$defaults['content_lp'] = file_get_contents($item_path);
  6633. $defaults['content_lp'] = file_get_contents($content_path);
  6634. }
  6635. $form->addElement('hidden', 'type', 'dokeos_' . $item_type);
  6636. $form->addElement('hidden', 'post_time', time());
  6637. $form->setDefaults($defaults);
  6638. return $form->return_form();
  6639. }
  6640. /**
  6641. * Returns the form to update or create a document
  6642. * @param string Action (add/edit)
  6643. * @param integer ID of the lp_item (if already exists)
  6644. * @param mixed Integer if document ID, string if info ('new')
  6645. * @return string HTML form
  6646. */
  6647. public function display_document_form($action = 'add', $id = 0, $extra_info = 'new')
  6648. {
  6649. $course_id = api_get_course_int_id();
  6650. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6651. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  6652. $no_display_edit_textarea = false;
  6653. //If action==edit document
  6654. //We don't display the document form if it's not an editable document (html or txt file)
  6655. if ($action == "edit") {
  6656. if (is_array($extra_info)) {
  6657. $path_parts = pathinfo($extra_info['dir']);
  6658. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6659. $no_display_edit_textarea = true;
  6660. }
  6661. }
  6662. }
  6663. $no_display_add = false;
  6664. // If action==add an existing document
  6665. // We don't display the document form if it's not an editable document (html or txt file).
  6666. if ($action == "add") {
  6667. if (is_numeric($extra_info)) {
  6668. $sql_doc = "SELECT path FROM " . $tbl_doc . "
  6669. WHERE c_id = ".$course_id." AND id = " . intval($extra_info);
  6670. $result = Database::query($sql_doc);
  6671. $path_file = Database :: result($result, 0, 0);
  6672. $path_parts = pathinfo($path_file);
  6673. if ($path_parts['extension'] != "txt" && $path_parts['extension'] != "html") {
  6674. $no_display_add = true;
  6675. }
  6676. }
  6677. }
  6678. if ($id != 0 && is_array($extra_info)) {
  6679. $item_title = stripslashes($extra_info['title']);
  6680. $item_description = stripslashes($extra_info['description']);
  6681. $item_terms = stripslashes($extra_info['terms']);
  6682. if (empty ($item_title)) {
  6683. $path_parts = pathinfo($extra_info['path']);
  6684. $item_title = stripslashes($path_parts['filename']);
  6685. }
  6686. } elseif (is_numeric($extra_info)) {
  6687. $sql_doc = "SELECT path, title FROM " . $tbl_doc . "
  6688. WHERE
  6689. c_id = ".$course_id." AND
  6690. id = " . intval($extra_info);
  6691. $result = Database::query($sql_doc);
  6692. $row = Database::fetch_array($result);
  6693. $item_title = $row['title'];
  6694. $item_title = str_replace('_', ' ', $item_title);
  6695. if (empty ($item_title)) {
  6696. $path_parts = pathinfo($row['path']);
  6697. $item_title = stripslashes($path_parts['filename']);
  6698. }
  6699. } else {
  6700. $item_title = '';
  6701. $item_description = '';
  6702. }
  6703. $return = '<legend>';
  6704. if ($id != 0 && is_array($extra_info))
  6705. $parent = $extra_info['parent_item_id'];
  6706. else
  6707. $parent = 0;
  6708. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6709. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6710. $result = Database::query($sql);
  6711. $arrLP = array ();
  6712. while ($row = Database :: fetch_array($result)) {
  6713. $arrLP[] = array (
  6714. 'id' => $row['id'],
  6715. 'item_type' => $row['item_type'],
  6716. 'title' => $row['title'],
  6717. 'path' => $row['path'],
  6718. 'description' => $row['description'],
  6719. 'parent_item_id' => $row['parent_item_id'],
  6720. 'previous_item_id' => $row['previous_item_id'],
  6721. 'next_item_id' => $row['next_item_id'],
  6722. 'display_order' => $row['display_order'],
  6723. 'max_score' => $row['max_score'],
  6724. 'min_score' => $row['min_score'],
  6725. 'mastery_score' => $row['mastery_score'],
  6726. 'prerequisite' => $row['prerequisite']
  6727. );
  6728. }
  6729. $this->tree_array($arrLP);
  6730. $arrLP = $this->arrMenu;
  6731. unset ($this->arrMenu);
  6732. if ($action == 'add') {
  6733. $return .= get_lang('CreateTheDocument');
  6734. } elseif ($action == 'move') {
  6735. $return .= get_lang('MoveTheCurrentDocument');
  6736. } else {
  6737. $return .= get_lang('EditTheCurrentDocument');
  6738. }
  6739. $return .= '</legend>';
  6740. if (isset ($_GET['edit']) && $_GET['edit'] == 'true') {
  6741. $return .= Display :: return_warning_message('<strong>' . get_lang('Warning') . ' !</strong><br />' . get_lang('WarningEditingDocument'), false);
  6742. }
  6743. $form = new FormValidator('form', 'POST', api_get_self() . '?' .$_SERVER['QUERY_STRING'], '', array('enctype'=> "multipart/form-data"));
  6744. $defaults['title'] = Security :: remove_XSS($item_title);
  6745. if (empty($item_title)) {
  6746. $defaults['title'] = Security::remove_XSS($item_title);
  6747. }
  6748. $defaults['description'] = $item_description;
  6749. $form->addElement('html', $return);
  6750. if ($action != 'move') {
  6751. $form->addElement('text', 'title', get_lang('Title'), array('id' => 'idTitle', 'class' => 'span4'));
  6752. $form->applyFilter('title', 'html_filter');
  6753. }
  6754. $arrHide[0]['value'] = $this->name;
  6755. $arrHide[0]['padding'] = 3;
  6756. for ($i = 0; $i < count($arrLP); $i++) {
  6757. if ($action != 'add') {
  6758. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  6759. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6760. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6761. if ($parent == $arrLP[$i]['id']) {
  6762. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6763. }
  6764. }
  6765. } else {
  6766. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') {
  6767. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6768. $arrHide[$arrLP[$i]['id']]['padding'] = 3 + $arrLP[$i]['depth'] * 10;
  6769. if ($parent == $arrLP[$i]['id']) {
  6770. $s_selected_parent = $arrHide[$arrLP[$i]['id']];
  6771. }
  6772. }
  6773. }
  6774. }
  6775. $parent_select = $form->addElement('select', 'parent', get_lang('Parent'), '', 'class="learnpath_item_form" id="idParent" style="width:40%;" onchange="javascript: load_cbo(this.value);"');
  6776. $my_count=0;
  6777. foreach ($arrHide as $key => $value) {
  6778. if ($my_count!=0) {
  6779. // The LP name is also the first section and is not in the same charset like the other sections.
  6780. $value['value'] = Security :: remove_XSS($value['value']);
  6781. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6782. } else {
  6783. $value['value'] = Security :: remove_XSS($value['value']);
  6784. $parent_select->addOption($value['value'], $key, 'style="padding-left:' . $value['padding'] . 'px;"');
  6785. }
  6786. $my_count++;
  6787. }
  6788. if (!empty($id)) {
  6789. $parent_select->setSelected($parent);
  6790. } else {
  6791. $parent_item_id = $_SESSION['parent_item_id'];
  6792. $parent_select->setSelected($parent_item_id);
  6793. }
  6794. if (is_array($arrLP)) {
  6795. reset($arrLP);
  6796. }
  6797. $arrHide = array ();
  6798. //POSITION
  6799. for ($i = 0; $i < count($arrLP); $i++) {
  6800. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  6801. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6802. $s_selected_position = $arrLP[$i]['id'];
  6803. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6804. $arrHide[$arrLP[$i]['id']]['value'] = get_lang('After') . ' "' . $arrLP[$i]['title'] . '"';
  6805. }
  6806. }
  6807. $position = $form->addElement('select', 'previous', get_lang('Position'), '', 'id="previous" class="learnpath_item_form" style="width:40%;"');
  6808. $position->addOption(get_lang('FirstPosition'), 0);
  6809. foreach ($arrHide as $key => $value) {
  6810. $padding = isset($value['padding']) ? $value['padding']: 0;
  6811. $position->addOption($value['value'], $key, 'style="padding-left:' . $padding . 'px;"');
  6812. }
  6813. $position->setSelected($s_selected_position);
  6814. if (is_array($arrLP)) {
  6815. reset($arrLP);
  6816. }
  6817. if ($action != 'move') {
  6818. $id_prerequisite = 0;
  6819. if (is_array($arrLP)) {
  6820. foreach ($arrLP as $key => $value) {
  6821. if ($value['id'] == $id) {
  6822. $id_prerequisite = $value['prerequisite'];
  6823. break;
  6824. }
  6825. }
  6826. }
  6827. $arrHide = array ();
  6828. for ($i = 0; $i < count($arrLP); $i++) {
  6829. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  6830. if (isset($extra_info['previous_item_id']) && $extra_info['previous_item_id'] == $arrLP[$i]['id'])
  6831. $s_selected_position = $arrLP[$i]['id'];
  6832. elseif ($action == 'add') $s_selected_position = $arrLP[$i]['id'];
  6833. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  6834. }
  6835. }
  6836. if (!$no_display_add) {
  6837. if (($extra_info == 'new' || $extra_info['item_type'] == TOOL_DOCUMENT || $_GET['edit'] == 'true')) {
  6838. if (isset ($_POST['content']))
  6839. $content = stripslashes($_POST['content']);
  6840. elseif (is_array($extra_info)) {
  6841. //If it's an html document or a text file
  6842. if (!$no_display_edit_textarea) {
  6843. $content = $this->display_document($extra_info['path'], false, false);
  6844. }
  6845. } elseif (is_numeric($extra_info))
  6846. $content = $this->display_document($extra_info, false, false);
  6847. else
  6848. $content = '';
  6849. if (!$no_display_edit_textarea) {
  6850. // We need to calculate here some specific settings for the online editor.
  6851. // The calculated settings work for documents in the Documents tool
  6852. // (on the root or in subfolders).
  6853. // For documents in native scorm packages it is unclear whether the
  6854. // online editor should be activated or not.
  6855. // A new document, it is in the root of the repository.
  6856. $relative_path = '';
  6857. $relative_prefix = '';
  6858. if (is_array($extra_info) && $extra_info != 'new') {
  6859. // The document already exists. Whe have to determine its relative path towards the repository root.
  6860. $relative_path = explode('/', $extra_info['dir']);
  6861. $cnt = count($relative_path) - 2;
  6862. if ($cnt < 0) {
  6863. $cnt = 0;
  6864. }
  6865. $relative_prefix = str_repeat('../', $cnt);
  6866. $relative_path = array_slice($relative_path, 1, $cnt);
  6867. $relative_path = implode('/', $relative_path);
  6868. if (strlen($relative_path) > 0) {
  6869. $relative_path = $relative_path . '/';
  6870. }
  6871. } else {
  6872. global $_course;
  6873. $result = $this->generate_lp_folder($_course);
  6874. $relative_path = api_substr($result['dir'], 1, strlen($result['dir']));
  6875. $relative_prefix = '../../';
  6876. }
  6877. $editor_config = array( 'ToolbarSet' => 'LearningPathDocuments',
  6878. 'Width' => '100%',
  6879. 'Height' => '500',
  6880. 'FullPage' => true,
  6881. 'CreateDocumentDir' => $relative_prefix,
  6882. 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/',
  6883. 'BaseHref' => api_get_path(WEB_COURSE_PATH) . api_get_course_path().'/document/'.$relative_path
  6884. );
  6885. if ($_GET['action'] == 'add_item') {
  6886. $class = 'add';
  6887. $text = get_lang('LPCreateDocument');
  6888. } else
  6889. if ($_GET['action'] == 'edit_item') {
  6890. $class = 'save';
  6891. $text = get_lang('SaveDocument');
  6892. }
  6893. $form->addElement('style_submit_button', 'submit_button', $text, 'class="' . $class . '"');
  6894. $renderer = $form->defaultRenderer();
  6895. $renderer->setElementTemplate('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{label}<br />{element}', 'content_lp');
  6896. $form->addElement('html', '<div>');
  6897. $form->addElement('html_editor', 'content_lp', '', null, $editor_config);
  6898. $form->addElement('html', '</div>');
  6899. $defaults['content_lp'] = $content;
  6900. }
  6901. } elseif (is_numeric($extra_info)) {
  6902. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6903. $return = $this->display_document($extra_info, true, true, true);
  6904. $form->addElement('html', $return);
  6905. }
  6906. }
  6907. }
  6908. if ($action == 'move') {
  6909. $form->addElement('hidden', 'title', $item_title);
  6910. $form->addElement('hidden', 'description', $item_description);
  6911. }
  6912. if (is_numeric($extra_info)) {
  6913. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'value="submit_button", class="save"');
  6914. $form->addElement('hidden', 'path', $extra_info);
  6915. } elseif (is_array($extra_info)) {
  6916. $form->addElement('style_submit_button', 'submit_button', get_lang('SaveDocument'), 'class="save"');
  6917. $form->addElement('hidden', 'path', $extra_info['path']);
  6918. }
  6919. $form->addElement('hidden', 'type', TOOL_DOCUMENT);
  6920. $form->addElement('hidden', 'post_time', time());
  6921. $form->setDefaults($defaults);
  6922. return $form->return_form();
  6923. }
  6924. /**
  6925. * Return HTML form to add/edit a link item
  6926. * @param string $action (add/edit)
  6927. * @param integer $id Item ID if exists
  6928. * @param mixed $extra_info
  6929. * @return string HTML form
  6930. */
  6931. public function display_link_form($action = 'add', $id = 0, $extra_info = '')
  6932. {
  6933. $course_id = api_get_course_int_id();
  6934. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  6935. $tbl_link = Database :: get_course_table(TABLE_LINK);
  6936. if ($id != 0 && is_array($extra_info)) {
  6937. $item_title = stripslashes($extra_info['title']);
  6938. $item_description = stripslashes($extra_info['description']);
  6939. $item_url = stripslashes($extra_info['url']);
  6940. } elseif (is_numeric($extra_info)) {
  6941. $extra_info = intval($extra_info);
  6942. $sql_link = "SELECT title, description, url FROM " . $tbl_link . "
  6943. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  6944. $result = Database::query($sql_link);
  6945. $row = Database :: fetch_array($result);
  6946. $item_title = $row['title'];
  6947. $item_description = $row['description'];
  6948. $item_url = $row['url'];
  6949. } else {
  6950. $item_title = '';
  6951. $item_description = '';
  6952. $item_url = '';
  6953. }
  6954. $legend = '<legend>';
  6955. if ($id != 0 && is_array($extra_info)) {
  6956. $parent = $extra_info['parent_item_id'];
  6957. } else {
  6958. $parent = 0;
  6959. }
  6960. $sql = "SELECT * FROM " . $tbl_lp_item . "
  6961. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  6962. $result = Database::query($sql);
  6963. $arrLP = array ();
  6964. while ($row = Database :: fetch_array($result)) {
  6965. $arrLP[] = array (
  6966. 'id' => $row['id'],
  6967. 'item_type' => $row['item_type'],
  6968. 'title' => $row['title'],
  6969. 'path' => $row['path'],
  6970. 'description' => $row['description'],
  6971. 'parent_item_id' => $row['parent_item_id'],
  6972. 'previous_item_id' => $row['previous_item_id'],
  6973. 'next_item_id' => $row['next_item_id'],
  6974. 'display_order' => $row['display_order'],
  6975. 'max_score' => $row['max_score'],
  6976. 'min_score' => $row['min_score'],
  6977. 'mastery_score' => $row['mastery_score'],
  6978. 'prerequisite' => $row['prerequisite']
  6979. );
  6980. }
  6981. $this->tree_array($arrLP);
  6982. $arrLP = $this->arrMenu;
  6983. unset ($this->arrMenu);
  6984. if ($action == 'add')
  6985. $legend .= get_lang('CreateTheLink') . '&nbsp;:';
  6986. elseif ($action == 'move') $legend .= get_lang('MoveCurrentLink') . '&nbsp;:';
  6987. else
  6988. $legend .= get_lang('EditCurrentLink') . '&nbsp;:';
  6989. $legend .= '</legend>';
  6990. $return = '<div class="sectioncomment">';
  6991. $return .= '<form method="POST">';
  6992. $return .= $legend;
  6993. $return .= '<table>';
  6994. if ($action != 'move') {
  6995. $return .= '<tr>';
  6996. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  6997. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form"/></td>';
  6998. $return .= '</tr>';
  6999. }
  7000. $return .= '<tr>';
  7001. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7002. $return .= '<td class="input">';
  7003. $return .= '<select id="idParent" style="width:100%;" name="parent" onChange="javascript: load_cbo(this.value);" class="learnpath_item_form" size="1">';
  7004. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7005. $arrHide = array (
  7006. $id
  7007. );
  7008. $parent_item_id = $_SESSION['parent_item_id'];
  7009. for ($i = 0; $i < count($arrLP); $i++) {
  7010. if ($action != 'add') {
  7011. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7012. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7013. } else {
  7014. $arrHide[] = $arrLP[$i]['id'];
  7015. }
  7016. } else {
  7017. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7018. $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>';
  7019. }
  7020. }
  7021. if (is_array($arrLP)) {
  7022. reset($arrLP);
  7023. }
  7024. $return .= '</select>';
  7025. $return .= '</td>';
  7026. $return .= '</tr>';
  7027. $return .= '<tr>';
  7028. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7029. $return .= '<td class="input">';
  7030. $return .= '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7031. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7032. for ($i = 0; $i < count($arrLP); $i++) {
  7033. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7034. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7035. $selected = 'selected="selected" ';
  7036. elseif ($action == 'add')
  7037. $selected = 'selected="selected" ';
  7038. else
  7039. $selected = '';
  7040. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7041. }
  7042. }
  7043. $return .= '</select>';
  7044. $return .= '</td>';
  7045. $return .= '</tr>';
  7046. if ($action != 'move') {
  7047. $return .= '<tr>';
  7048. $return .= '<td class="label"><label for="idURL">' . get_lang('Url') . '</label></td>';
  7049. $return .= '<td class="input"><input' . (is_numeric($extra_info) ? ' disabled="disabled"' : '') . ' id="idURL" name="url" style="width:99%;" type="text" value="' . $item_url . '" class="learnpath_item_form" /></td>';
  7050. $return .= '</tr>';
  7051. $id_prerequisite = 0;
  7052. if (is_array($arrLP)) {
  7053. foreach ($arrLP as $key => $value) {
  7054. if ($value['id'] == $id) {
  7055. $id_prerequisite = $value['prerequisite'];
  7056. break;
  7057. }
  7058. }
  7059. }
  7060. $arrHide = array();
  7061. for ($i = 0; $i < count($arrLP); $i++) {
  7062. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7063. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7064. $s_selected_position = $arrLP[$i]['id'];
  7065. elseif ($action == 'add') $s_selected_position = 0;
  7066. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7067. }
  7068. }
  7069. $return .= '</tr>';
  7070. }
  7071. $return .= '<tr>';
  7072. if ($action == 'add') {
  7073. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddLinkToCourse') . '</button></td>';
  7074. } else {
  7075. $return .= '<td>&nbsp;</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentLink') . '</button></td>';
  7076. }
  7077. $return .= '</tr>';
  7078. $return .= '</table>';
  7079. if ($action == 'move') {
  7080. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7081. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7082. }
  7083. if (is_numeric($extra_info)) {
  7084. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7085. } elseif (is_array($extra_info)) {
  7086. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7087. }
  7088. $return .= '<input name="type" type="hidden" value="' . TOOL_LINK . '" />';
  7089. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7090. $return .= '</form>';
  7091. $return .= '</div>';
  7092. return $return;
  7093. }
  7094. /**
  7095. * Return HTML form to add/edit a student publication (work)
  7096. * @param string Action (add/edit)
  7097. * @param integer Item ID if already exists
  7098. * @param mixed Extra info (work ID if integer)
  7099. * @return string HTML form
  7100. */
  7101. public function display_student_publication_form($action = 'add', $id = 0, $extra_info = '')
  7102. {
  7103. $course_id = api_get_course_int_id();
  7104. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7105. $tbl_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  7106. if ($id != 0 && is_array($extra_info)) {
  7107. $item_title = stripslashes($extra_info['title']);
  7108. $item_description = stripslashes($extra_info['description']);
  7109. } elseif (is_numeric($extra_info)) {
  7110. $extra_info = intval($extra_info);
  7111. $sql_publication = "SELECT title, description FROM " . $tbl_publication . "
  7112. WHERE c_id = ".$course_id." AND id = " . $extra_info;
  7113. $result = Database::query($sql_publication);
  7114. $row = Database :: fetch_array($result);
  7115. $item_title = $row['title'];
  7116. } else {
  7117. $item_title = get_lang('Student_publication');
  7118. }
  7119. $legend = '<legend>';
  7120. if ($id != 0 && is_array($extra_info))
  7121. $parent = $extra_info['parent_item_id'];
  7122. else
  7123. $parent = 0;
  7124. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7125. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7126. $result = Database::query($sql);
  7127. $arrLP = array();
  7128. while ($row = Database :: fetch_array($result)) {
  7129. $arrLP[] = array (
  7130. 'id' => $row['id'],
  7131. 'item_type' => $row['item_type'],
  7132. 'title' => $row['title'],
  7133. 'path' => $row['path'],
  7134. 'description' => $row['description'],
  7135. 'parent_item_id' => $row['parent_item_id'],
  7136. 'previous_item_id' => $row['previous_item_id'],
  7137. 'next_item_id' => $row['next_item_id'],
  7138. 'display_order' => $row['display_order'],
  7139. 'max_score' => $row['max_score'],
  7140. 'min_score' => $row['min_score'],
  7141. 'mastery_score' => $row['mastery_score'],
  7142. 'prerequisite' => $row['prerequisite']
  7143. );
  7144. }
  7145. $this->tree_array($arrLP);
  7146. $arrLP = $this->arrMenu;
  7147. unset ($this->arrMenu);
  7148. if ($action == 'add') {
  7149. $legend .= get_lang('Student_publication') . '&nbsp;:' . "\n";
  7150. } elseif ($action == 'move') {
  7151. $legend .= get_lang('MoveCurrentStudentPublication') . '&nbsp;:' . "\n";
  7152. } else {
  7153. $legend .= get_lang('EditCurrentStudentPublication') . '&nbsp;:' . "\n";
  7154. }
  7155. $legend .= '</legend>';
  7156. $return = '<div class="sectioncomment">';
  7157. $return .= '<form method="POST">';
  7158. $return .= $legend;
  7159. $return .= '<table class="lp_form">';
  7160. if ($action != 'move') {
  7161. $return .= '<tr>';
  7162. $return .= '<td class="label"><label for="idTitle">' . get_lang('Title') . '</label></td>';
  7163. $return .= '<td class="input"><input id="idTitle" name="title" size="44" type="text" value="' . $item_title . '" class="learnpath_item_form" /></td>';
  7164. $return .= '</tr>';
  7165. }
  7166. $return .= '<tr>';
  7167. $return .= '<td class="label"><label for="idParent">' . get_lang('Parent') . '</label></td>';
  7168. $return .= '<td class="input">';
  7169. $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">';
  7170. //$parent_item_id = $_SESSION['parent_item_id'];
  7171. $return .= '<option class="top" value="0">' . $this->name . '</option>';
  7172. $arrHide = array (
  7173. $id
  7174. );
  7175. for ($i = 0; $i < count($arrLP); $i++) {
  7176. if ($action != 'add') {
  7177. if (($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir') && !in_array($arrLP[$i]['id'], $arrHide) && !in_array($arrLP[$i]['parent_item_id'], $arrHide)) {
  7178. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7179. } else {
  7180. $arrHide[] = $arrLP[$i]['id'];
  7181. }
  7182. } else {
  7183. if ($arrLP[$i]['item_type'] == 'dokeos_module' || $arrLP[$i]['item_type'] == 'dokeos_chapter' || $arrLP[$i]['item_type'] == 'dir')
  7184. $return .= '<option ' . (($parent == $arrLP[$i]['id']) ? 'selected="selected" ' : '') . 'style="padding-left:' . ($arrLP[$i]['depth'] * 10) . 'px;" value="' . $arrLP[$i]['id'] . '">' . $arrLP[$i]['title'] . '</option>';
  7185. }
  7186. }
  7187. if (is_array($arrLP)) {
  7188. reset($arrLP);
  7189. }
  7190. $return .= "\t\t\t\t" . '</select>';
  7191. $return .= '</td>';
  7192. $return .= '</tr>';
  7193. $return .= '<tr>';
  7194. $return .= '<td class="label"><label for="previous">' . get_lang('Position') . '</label></td>';
  7195. $return .= '<td class="input">';
  7196. $return .= "\t\t\t\t" . '<select id="previous" name="previous" style="width:100%;" size="1" class="learnpath_item_form">';
  7197. $return .= '<option class="top" value="0">' . get_lang('FirstPosition') . '</option>';
  7198. for ($i = 0; $i < count($arrLP); $i++) {
  7199. if ($arrLP[$i]['parent_item_id'] == $parent && $arrLP[$i]['id'] != $id) {
  7200. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7201. $selected = 'selected="selected" ';
  7202. elseif ($action == 'add') $selected = 'selected="selected" ';
  7203. else
  7204. $selected = '';
  7205. $return .= '<option ' . $selected . 'value="' . $arrLP[$i]['id'] . '">' . get_lang('After') . ' "' . $arrLP[$i]['title'] . '"</option>';
  7206. }
  7207. }
  7208. $return .= "\t\t\t\t" . '</select>';
  7209. $return .= '</td>';
  7210. $return .= '</tr>';
  7211. if ($action != 'move') {
  7212. $id_prerequisite = 0;
  7213. if (is_array($arrLP)) {
  7214. foreach ($arrLP as $key => $value) {
  7215. if ($value['id'] == $id) {
  7216. $id_prerequisite = $value['prerequisite'];
  7217. break;
  7218. }
  7219. }
  7220. }
  7221. $arrHide = array ();
  7222. for ($i = 0; $i < count($arrLP); $i++) {
  7223. if ($arrLP[$i]['id'] != $id && $arrLP[$i]['item_type'] != 'dokeos_chapter') {
  7224. if ($extra_info['previous_item_id'] == $arrLP[$i]['id'])
  7225. $s_selected_position = $arrLP[$i]['id'];
  7226. elseif ($action == 'add') $s_selected_position = 0;
  7227. $arrHide[$arrLP[$i]['id']]['value'] = $arrLP[$i]['title'];
  7228. }
  7229. }
  7230. // Commented the prerequisites, only visible in edit (work).
  7231. /*
  7232. $return .= '<tr>';
  7233. $return .= '<td class="label"><label for="idPrerequisites">'.get_lang('LearnpathPrerequisites').'</label></td>';
  7234. $return .= '<td class="input"><select name="prerequisites" id="prerequisites" class="learnpath_item_form"><option value="0">'.get_lang('NoPrerequisites').'</option>';
  7235. foreach($arrHide as $key => $value) {
  7236. if ($key == $s_selected_position && $action == 'add') {
  7237. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7238. }
  7239. elseif ($key == $id_prerequisite && $action == 'edit') {
  7240. $return .= '<option value="'.$key.'" selected="selected">'.$value['value'].'</option>';
  7241. }
  7242. else {
  7243. $return .= '<option value="'.$key.'">'.$value['value'].'</option>';
  7244. }
  7245. }
  7246. $return .= "</select></td>";
  7247. */
  7248. $return .= '</tr>';
  7249. }
  7250. $return .= '<tr>';
  7251. if ($action == 'add') {
  7252. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('AddAssignmentToCourse') . '</button></td>';
  7253. } else {
  7254. $return .= '<td>&nbsp</td><td><button class="save" name="submit_button" type="submit">' . get_lang('EditCurrentStudentPublication') . '</button></td>';
  7255. }
  7256. $return .= '</tr>';
  7257. $return .= '</table>';
  7258. if ($action == 'move') {
  7259. $return .= '<input name="title" type="hidden" value="' . $item_title . '" />';
  7260. $return .= '<input name="description" type="hidden" value="' . $item_description . '" />';
  7261. }
  7262. if (is_numeric($extra_info)) {
  7263. $return .= '<input name="path" type="hidden" value="' . $extra_info . '" />';
  7264. } elseif (is_array($extra_info)) {
  7265. $return .= '<input name="path" type="hidden" value="' . $extra_info['path'] . '" />';
  7266. }
  7267. $return .= '<input name="type" type="hidden" value="' . TOOL_STUDENTPUBLICATION . '" />';
  7268. $return .= '<input name="post_time" type="hidden" value="' . time() . '" />';
  7269. $return .= '</form>';
  7270. $return .= '</div>';
  7271. return $return;
  7272. }
  7273. /**
  7274. * Displays the menu for manipulating a step
  7275. *
  7276. * @param $item_id
  7277. * @param string $item_type
  7278. * @return string
  7279. */
  7280. public function display_manipulate($item_id, $item_type = TOOL_DOCUMENT)
  7281. {
  7282. $_course = api_get_course_info();
  7283. $course_id = api_get_course_int_id();
  7284. $course_code = api_get_course_id();
  7285. $return = '<div class="actions">';
  7286. switch ($item_type) {
  7287. case 'dokeos_chapter' :
  7288. case 'chapter' :
  7289. // Commented the message cause should not show it.
  7290. //$lang = get_lang('TitleManipulateChapter');
  7291. break;
  7292. case 'dokeos_module' :
  7293. case 'module' :
  7294. // Commented the message cause should not show it.
  7295. //$lang = get_lang('TitleManipulateModule');
  7296. break;
  7297. case TOOL_DOCUMENT :
  7298. // Commented the message cause should not show it.
  7299. //$lang = get_lang('TitleManipulateDocument');
  7300. break;
  7301. case TOOL_LINK :
  7302. case 'link' :
  7303. // Commented the message cause should not show it.
  7304. //$lang = get_lang('TitleManipulateLink');
  7305. break;
  7306. case TOOL_QUIZ :
  7307. // Commented the message cause should not show it.
  7308. //$lang = get_lang('TitleManipulateQuiz');
  7309. break;
  7310. case TOOL_STUDENTPUBLICATION :
  7311. // Commented the message cause should not show it.
  7312. //$lang = get_lang('TitleManipulateStudentPublication');
  7313. break;
  7314. }
  7315. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7316. $item_id = intval($item_id);
  7317. $sql = "SELECT * FROM " . $tbl_lp_item . " as lp
  7318. WHERE lp.c_id = ".$course_id." AND lp.id = " . $item_id;
  7319. $result = Database::query($sql);
  7320. $row = Database::fetch_assoc($result);
  7321. $audio_player = null;
  7322. // We display an audio player if needed.
  7323. if (!empty($row['audio'])) {
  7324. $audio_player .= '<div class="lp_mediaplayer" id="container">
  7325. <a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
  7326. </div>';
  7327. $audio_player .= '<script type="text/javascript" src="../inc/lib/mediaplayer/swfobject.js"></script>';
  7328. $audio_player .= '<script>
  7329. var s1 = new SWFObject("../inc/lib/mediaplayer/player.swf","ply","250","20","9","#FFFFFF");
  7330. s1.addParam("allowscriptaccess","always");
  7331. s1.addParam("flashvars","file=../../courses/' . $_course['path'] . '/document/audio/' . $row['audio'] . '&autostart=true");
  7332. s1.write("container");
  7333. </script>';
  7334. }
  7335. $url = api_get_self().'?cidReq='.Security::remove_XSS($_GET['cidReq']).'&view=build&id='.$item_id .'&lp_id='.$this->lp_id;
  7336. $return .= Display::url(
  7337. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
  7338. $url.'&action=edit_item&path_item=' . $row['path']
  7339. );
  7340. $return .= Display::url(
  7341. Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL),
  7342. $url.'&action=move_item'
  7343. );
  7344. // Commented for now as prerequisites cannot be added to chapters.
  7345. if ($item_type != 'dokeos_chapter' && $item_type != 'chapter') {
  7346. $return .= Display::url(
  7347. Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_SMALL),
  7348. $url.'&action=edit_item_prereq'
  7349. );
  7350. }
  7351. $return .= Display::url(
  7352. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL),
  7353. $url.'&action=delete_item'
  7354. );
  7355. if ($item_type == TOOL_HOTPOTATOES ) {
  7356. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7357. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7358. }
  7359. if ($item_type == TOOL_DOCUMENT ) {
  7360. $document_data = DocumentManager::get_document_data_by_id($row['path'], $course_code);
  7361. $return .= get_lang('File').': '.$document_data['absolute_path_from_document'];
  7362. }
  7363. $return .= '</div>';
  7364. if (!empty($audio_player)) {
  7365. $return .= '<br />'.$audio_player;
  7366. }
  7367. return $return;
  7368. }
  7369. /**
  7370. * Creates the javascript needed for filling up the checkboxes without page reload
  7371. * @return string
  7372. */
  7373. public function get_js_dropdown_array()
  7374. {
  7375. $course_id = api_get_course_int_id();
  7376. $return = 'var child_name = new Array();' . "\n";
  7377. $return .= 'var child_value = new Array();' . "\n\n";
  7378. $return .= 'child_name[0] = new Array();' . "\n";
  7379. $return .= 'child_value[0] = new Array();' . "\n\n";
  7380. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7381. $sql_zero = "SELECT * FROM " . $tbl_lp_item . "
  7382. WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id . " AND parent_item_id = 0
  7383. ORDER BY display_order ASC";
  7384. $res_zero = Database::query($sql_zero);
  7385. $i = 0;
  7386. while ($row_zero = Database :: fetch_array($res_zero)) {
  7387. if ($row_zero['item_type'] == TOOL_QUIZ) {
  7388. $row_zero['title'] = Exercise::get_formated_title_variable($row_zero['title']);
  7389. }
  7390. $js_var = json_encode(get_lang('After').' '.$row_zero['title']);
  7391. $return .= 'child_name[0][' . $i . '] = '.$js_var.' ;' . "\n";
  7392. $return .= 'child_value[0][' . $i++ . '] = "' . $row_zero['id'] . '";' . "\n";
  7393. }
  7394. $return .= "\n";
  7395. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = ".$course_id." AND lp_id = " . $this->lp_id;
  7396. $res = Database::query($sql);
  7397. while ($row = Database :: fetch_array($res)) {
  7398. $sql_parent = "SELECT * FROM " . $tbl_lp_item . "
  7399. WHERE
  7400. c_id = ".$course_id." AND
  7401. parent_item_id = " . $row['id'] . "
  7402. ORDER BY display_order ASC";
  7403. $res_parent = Database::query($sql_parent);
  7404. $i = 0;
  7405. $return .= 'child_name[' . $row['id'] . '] = new Array();' . "\n";
  7406. $return .= 'child_value[' . $row['id'] . '] = new Array();' . "\n\n";
  7407. while ($row_parent = Database :: fetch_array($res_parent)) {
  7408. $js_var = json_encode(get_lang('After').' '.$row_parent['title']);
  7409. $return .= 'child_name[' . $row['id'] . '][' . $i . '] = '.$js_var.' ;' . "\n";
  7410. $return .= 'child_value[' . $row['id'] . '][' . $i++ . '] = "' . $row_parent['id'] . '";' . "\n";
  7411. }
  7412. $return .= "\n";
  7413. }
  7414. return $return;
  7415. }
  7416. /**
  7417. * Display the form to allow moving an item
  7418. * @param integer Item ID
  7419. * @return string HTML form
  7420. */
  7421. public function display_move_item($item_id)
  7422. {
  7423. $course_id = api_get_course_int_id();
  7424. global $_course; //will disappear
  7425. global $charset;
  7426. $return = '';
  7427. if (is_numeric($item_id)) {
  7428. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7429. $sql = "SELECT * FROM " . $tbl_lp_item . "
  7430. WHERE c_id = ".$course_id." AND id = " . $item_id;
  7431. $res = Database::query($sql);
  7432. $row = Database :: fetch_array($res);
  7433. switch ($row['item_type']) {
  7434. case 'dokeos_chapter' :
  7435. case 'dir' :
  7436. case 'asset' :
  7437. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7438. $return .= $this->display_item_form($row['item_type'], get_lang('MoveCurrentChapter'), 'move', $item_id, $row);
  7439. break;
  7440. case 'dokeos_module' :
  7441. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7442. $return .= $this->display_item_form($row['item_type'], 'Move th current module:', 'move', $item_id, $row);
  7443. break;
  7444. case TOOL_DOCUMENT :
  7445. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7446. $return .= $this->display_document_form('move', $item_id, $row);
  7447. break;
  7448. case TOOL_LINK :
  7449. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7450. $return .= $this->display_link_form('move', $item_id, $row);
  7451. break;
  7452. case TOOL_HOTPOTATOES :
  7453. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7454. $return .= $this->display_link_form('move', $item_id, $row);
  7455. break;
  7456. case TOOL_QUIZ :
  7457. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7458. $return .= $this->display_quiz_form('move', $item_id, $row);
  7459. break;
  7460. case TOOL_STUDENTPUBLICATION :
  7461. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7462. $return .= $this->display_student_publication_form('move', $item_id, $row);
  7463. break;
  7464. case TOOL_FORUM :
  7465. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7466. $return .= $this->display_forum_form('move', $item_id, $row);
  7467. break;
  7468. case TOOL_THREAD :
  7469. $return .= $this->display_manipulate($item_id, $row['item_type']);
  7470. $return .= $this->display_forum_form('move', $item_id, $row);
  7471. break;
  7472. }
  7473. }
  7474. return $return;
  7475. }
  7476. /**
  7477. * Displays a basic form on the overview page for changing the item title and the item description.
  7478. * @param string $item_type
  7479. * @param string $title
  7480. * @param array $data
  7481. * @return string
  7482. */
  7483. public function display_item_small_form($item_type, $title = '', $data = array())
  7484. {
  7485. $url = api_get_self() . '?' .api_get_cidreq().'&action=edit_item&lp_id='.$this->lp_id;
  7486. $form = new FormValidator('small_form', 'post', $url);
  7487. $form->addElement('header', $title);
  7488. $form->addElement('text', 'title', get_lang('Title'));
  7489. $form->addElement('button', 'submit_button', get_lang('Save'));
  7490. $form->addElement('hidden', 'id', $data['id']);
  7491. $form->addElement('hidden', 'parent', $data['parent_item_id']);
  7492. $form->addElement('hidden', 'previous', $data['previous_item_id']);
  7493. $form->setDefaults(array('title' => $data['title']));
  7494. return $form->toHtml();
  7495. }
  7496. /**
  7497. * Return HTML form to allow prerequisites selection
  7498. * @param integer Item ID
  7499. * @return string HTML form
  7500. */
  7501. public function display_item_prerequisites_form($item_id)
  7502. {
  7503. $course_id = api_get_course_int_id();
  7504. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  7505. $item_id = intval($item_id);
  7506. /* Current prerequisite */
  7507. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id = " . $item_id;
  7508. $result = Database::query($sql);
  7509. $row = Database::fetch_array($result);
  7510. $preq_id = $row['prerequisite'];
  7511. $return = '<legend>';
  7512. $return .= get_lang('AddEditPrerequisites');
  7513. $return .= '</legend>';
  7514. $return .= '<form method="POST">';
  7515. $return .= '<table class="data_table">';
  7516. $return .= '<tr>';
  7517. $return .= '<th height="24">' . get_lang('LearnpathPrerequisites') . '</th>';
  7518. $return .= '<th width="70" height="24">' . get_lang('Minimum') . '</th>';
  7519. $return .= '<th width="70" height="24">' . get_lang('Maximum') . '</th>';
  7520. $return .= '</tr>';
  7521. // Adding the none option to the prerequisites see http://www.chamilo.org/es/node/146
  7522. $return .= '<tr >';
  7523. $return .= '<td colspan="3" class="radio">';
  7524. $return .= '<input checked="checked" id="idNone" name="prerequisites" style="margin-left:0px; margin-right:10px;" type="radio" />';
  7525. $return .= '<label for="idNone">' . get_lang('None') . '</label>';
  7526. $return .= '</tr>';
  7527. $sql = "SELECT * FROM " . $tbl_lp_item . " WHERE c_id = $course_id AND lp_id = " . $this->lp_id;
  7528. $result = Database::query($sql);
  7529. $arrLP = array ();
  7530. while ($row = Database :: fetch_array($result)) {
  7531. $arrLP[] = array(
  7532. 'id' => $row['id'],
  7533. 'item_type' => $row['item_type'],
  7534. 'title' => $row['title'],
  7535. 'ref' => $row['ref'],
  7536. 'description' => $row['description'],
  7537. 'parent_item_id' => $row['parent_item_id'],
  7538. 'previous_item_id' => $row['previous_item_id'],
  7539. 'next_item_id' => $row['next_item_id'],
  7540. 'max_score' => $row['max_score'],
  7541. 'min_score' => $row['min_score'],
  7542. 'mastery_score' => $row['mastery_score'],
  7543. 'prerequisite' => $row['prerequisite'],
  7544. 'next_item_id' => $row['next_item_id'],
  7545. 'display_order' => $row['display_order']
  7546. );
  7547. if ($row['ref'] == $preq_id) {
  7548. $preq_mastery = $row['mastery_score'];
  7549. $preq_max = $row['max_score'];
  7550. }
  7551. }
  7552. $this->tree_array($arrLP);
  7553. $arrLP = $this->arrMenu;
  7554. unset($this->arrMenu);
  7555. for ($i = 0; $i < count($arrLP); $i++) {
  7556. if ($arrLP[$i]['id'] == $item_id)
  7557. break;
  7558. $return .= '<tr>';
  7559. $return .= '<td class="radio"' . (($arrLP[$i]['item_type'] != TOOL_QUIZ && $arrLP[$i]['item_type'] != TOOL_HOTPOTATOES) ? ' colspan="3"' : '') . '>';
  7560. $return .= '<label for="id' . $arrLP[$i]['id'] . '">';
  7561. $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:' . $arrLP[$i]['depth'] * 10 . 'px; margin-right:10px;" type="radio" value="' . $arrLP[$i]['id'] . '" />';
  7562. $icon_name = str_replace(' ', '', $arrLP[$i]['item_type']);
  7563. if (file_exists('../img/lp_' . $icon_name . '.png')) {
  7564. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.png" style="margin-right:5px;" title="" />';
  7565. } else {
  7566. if (file_exists('../img/lp_' . $icon_name . '.gif')) {
  7567. $return .= '<img alt="" src="../img/lp_' . $icon_name . '.gif" style="margin-right:5px;" title="" />';
  7568. } else {
  7569. $return .= Display::return_icon('folder_document.gif','',array('style'=>'margin-right:5px;'));
  7570. }
  7571. }
  7572. $return .= $arrLP[$i]['title'] . '</label>';
  7573. $return .= '</td>';
  7574. if ($arrLP[$i]['item_type'] == TOOL_QUIZ) {
  7575. // lets update max_score Quiz information depending of the Quiz Advanced properties
  7576. require_once api_get_path(LIBRARY_PATH)."lp_item.lib.php";
  7577. $tmp_obj_lp_item = new LpItem($course_id, $arrLP[$i]['id']);
  7578. $tmp_obj_exercice = new Exercise();
  7579. $tmp_obj_exercice->read($tmp_obj_lp_item->path);
  7580. $tmp_obj_lp_item->max_score = $tmp_obj_exercice->get_max_score();
  7581. $tmp_obj_lp_item->update_in_bdd();
  7582. $arrLP[$i]['max_score'] = $tmp_obj_lp_item->max_score;
  7583. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7584. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7585. $return .= '</td>';
  7586. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7587. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7588. $return .= '</td>';
  7589. }
  7590. if ($arrLP[$i]['item_type'] == TOOL_HOTPOTATOES) {
  7591. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7592. $return .= '<center><input size="4" maxlength="3" name="min_' . $arrLP[$i]['id'] . '" type="text" value="' . (($arrLP[$i]['id'] == $preq_id) ? $preq_mastery : 0) . '" /></center>';
  7593. $return .= '</td>';
  7594. $return .= '<td class="exercise" style="border:1px solid #ccc;">';
  7595. $return .= '<center><input size="4" maxlength="3" name="max_' . $arrLP[$i]['id'] . '" type="text" value="' . $arrLP[$i]['max_score'] . '" disabled="true" /></center>';
  7596. $return .= '</td>';
  7597. }
  7598. $return .= '</tr>';
  7599. }
  7600. $return .= '<tr>';
  7601. $return .= '</tr>';
  7602. $return .= '</table>';
  7603. $return .= '<div style="padding-top:3px;">';
  7604. $return .= '<button class="save" name="submit_button" type="submit">' . get_lang('ModifyPrerequisites') . '</button>';
  7605. $return .= '</form>';
  7606. return $return;
  7607. }
  7608. /**
  7609. * Return HTML list to allow prerequisites selection for lp
  7610. * @param integer Item ID
  7611. * @return string HTML form
  7612. */
  7613. public function display_lp_prerequisites_list()
  7614. {
  7615. $course_id = api_get_course_int_id();
  7616. $lp_id = $this->lp_id;
  7617. $tbl_lp = Database :: get_course_table(TABLE_LP_MAIN);
  7618. // get current prerequisite
  7619. $sql = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $lp_id ";
  7620. $result = Database::query($sql);
  7621. $row = Database :: fetch_array($result);
  7622. $preq_id = $row['prerequisite'];
  7623. $session_id = api_get_session_id();
  7624. $session_condition = api_get_session_condition($session_id);
  7625. $sql = "SELECT * FROM $tbl_lp
  7626. WHERE c_id = $course_id $session_condition
  7627. ORDER BY display_order ";
  7628. $rs = Database::query($sql);
  7629. $return = '';
  7630. $return .= '<select name="prerequisites" >';
  7631. $return .= '<option value="0">'.get_lang('None').'</option>';
  7632. if (Database::num_rows($rs) > 0) {
  7633. while ($row = Database::fetch_array($rs)) {
  7634. if ($row['id'] == $lp_id) {
  7635. continue;
  7636. }
  7637. $return .= '<option value="'.$row['id'].'" '.(($row['id']==$preq_id)?' selected ' : '').'>'.$row['name'].'</option>';
  7638. }
  7639. }
  7640. $return .= '</select>';
  7641. return $return;
  7642. }
  7643. /**
  7644. * Creates a list with all the documents in it
  7645. * @param bool $showInvisibleFiles
  7646. * @return string
  7647. */
  7648. public function get_documents($showInvisibleFiles = false)
  7649. {
  7650. $course_info = api_get_course_info();
  7651. $sessionId = api_get_session_id();
  7652. $document_tree = DocumentManager::get_document_preview(
  7653. $course_info,
  7654. $this->lp_id,
  7655. null,
  7656. $sessionId,
  7657. true,
  7658. null,
  7659. null,
  7660. $showInvisibleFiles,
  7661. true
  7662. );
  7663. return $document_tree;
  7664. }
  7665. /**
  7666. * Creates a list with all the exercises (quiz) in it
  7667. * @return string
  7668. */
  7669. public function get_exercises() {
  7670. $course_id = api_get_course_int_id();
  7671. // New for hotpotatoes.
  7672. $uploadPath = DIR_HOTPOTATOES; //defined in main_api
  7673. $tbl_doc = Database :: get_course_table(TABLE_DOCUMENT);
  7674. $tbl_quiz = Database :: get_course_table(TABLE_QUIZ_TEST);
  7675. $session_id = api_get_session_id();
  7676. $condition_session = api_get_session_condition($session_id);
  7677. $sql_quiz = "SELECT * FROM $tbl_quiz
  7678. WHERE c_id = $course_id AND active<>'-1' $condition_session
  7679. ORDER BY title ASC";
  7680. $sql_hot = "SELECT * FROM $tbl_doc
  7681. WHERE c_id = $course_id AND path LIKE '" . $uploadPath . "/%/%htm%' $condition_session
  7682. ORDER BY id ASC";
  7683. $res_quiz = Database::query($sql_quiz);
  7684. $res_hot = Database::query($sql_hot);
  7685. $return = '<ul class="lp_resource">';
  7686. $return .= '<li class="lp_resource_element">';
  7687. $return .= '<img alt="" src="../img/new_test_small.gif" style="margin-right:5px;" title="" />';
  7688. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'exercice/exercise_admin.php?lp_id=' . $this->lp_id . '">' . get_lang('NewExercise') . '</a>';
  7689. $return .= '</li>';
  7690. // Display hotpotatoes
  7691. while ($row_hot = Database :: fetch_array($res_hot)) {
  7692. $return .= '<li class="lp_resource_element" data_id="'.$row_hot['id'].'" data_type="hotpotatoes" title="'.$row_hot['title'].'" >';
  7693. $return .= '<a class="moved" href="#">';
  7694. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7695. $return .= '</a> ';
  7696. $return .= '<img src="../img/hotpotatoes_s.png" style="margin-right:5px;" title="" width="16px" />';
  7697. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_HOTPOTATOES . '&amp;file=' . $row_hot['id'] . '&amp;lp_id=' . $this->lp_id . '">'.
  7698. ((!empty ($row_hot['comment'])) ? $row_hot['comment'] : Security :: remove_XSS($row_hot['title'])) . '</a>';
  7699. $return .= '</li>';
  7700. }
  7701. while ($row_quiz = Database :: fetch_array($res_quiz)) {
  7702. $return .= '<li class="lp_resource_element" data_id="'.$row_quiz['id'].'" data_type="quiz" title="'.$row_quiz['title'].'" >';
  7703. $return .= '<a class="moved" href="#">';
  7704. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7705. $return .= '</a> ';
  7706. $return .= '<img alt="" src="../img/quizz_small.gif" style="margin-right:5px;" title="" />';
  7707. $return .= '<a href="' . api_get_self() . '?'.api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_QUIZ . '&amp;file=' . $row_quiz['id'] . '&amp;lp_id=' . $this->lp_id . '">' .
  7708. Security :: remove_XSS(cut($row_quiz['title'], 80)).
  7709. '</a>';
  7710. $return .= '</li>';
  7711. }
  7712. $return .= '</ul>';
  7713. return $return;
  7714. }
  7715. /**
  7716. * Creates a list with all the links in it
  7717. * @return string
  7718. */
  7719. public function get_links()
  7720. {
  7721. require_once api_get_path(LIBRARY_PATH).'link.lib.php';
  7722. $course_id = api_get_course_int_id();
  7723. $tbl_link = Database::get_course_table(TABLE_LINK);
  7724. $session_id = api_get_session_id();
  7725. $condition_session = api_get_session_condition($session_id);
  7726. $sql = "SELECT id, title, category_id FROM $tbl_link
  7727. WHERE c_id = ".$course_id." $condition_session
  7728. ORDER BY title ASC";
  7729. $res_link = Database::query($sql);
  7730. $return = '<ul class="lp_resource">';
  7731. $return .= '<li class="lp_resource_element">';
  7732. $return .= '<img alt="" src="../img/linksnew.gif" style="margin-right:5px;width:16px" title="" />';
  7733. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'link/link.php?' . api_get_cidreq() . '&action=addlink&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('LinkAdd') . '">' . get_lang('LinkAdd') . '</a>';
  7734. $return .= '</li>';
  7735. $course_info = api_get_course_info();
  7736. $linkCategories = getLinkCategories($course_id, $session_id);
  7737. $categoryIdList = array();
  7738. if (!empty($linkCategories)) {
  7739. foreach ($linkCategories as $categoryInfo) {
  7740. $categoryIdList[] = $categoryInfo['id'];
  7741. }
  7742. }
  7743. while ($row_link = Database :: fetch_array($res_link)) {
  7744. // Check if category exists if not then consider as deleted.
  7745. if (!empty($row_link['category_id'])) {
  7746. $categoryId = $row_link['category_id'];
  7747. if (!in_array($categoryId, $categoryIdList)) {
  7748. continue;
  7749. }
  7750. }
  7751. $item_visibility = api_get_item_visibility(
  7752. $course_info,
  7753. TOOL_LINK,
  7754. $row_link['id'],
  7755. $session_id
  7756. );
  7757. if ($item_visibility != 2) {
  7758. $return .= '<li class="lp_resource_element" data_id="'.$row_link['id'].'" data_type="'.TOOL_LINK.'" title="'.$row_link['title'].'" >';
  7759. $return .= '<a class="moved" href="#">';
  7760. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7761. $return .= '</a> ';
  7762. $return .= '<img alt="" src="../img/lp_link.gif" style="margin-right:5px;" title="" />';
  7763. $return .= '<a href="' . api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_LINK . '&amp;file=' . $row_link['id'] . '&amp;lp_id=' . $this->lp_id . '">'.
  7764. $row_link['title'].
  7765. '</a>';
  7766. $return .= '</li>';
  7767. }
  7768. }
  7769. $return .= '</ul>';
  7770. return $return;
  7771. }
  7772. /**
  7773. * Creates a list with all the student publications in it
  7774. * @return unknown
  7775. */
  7776. public function get_student_publications()
  7777. {
  7778. $return = '<div class="lp_resource" >';
  7779. $return .= '<div class="lp_resource_element">';
  7780. $return .= '<img align="left" alt="" src="../img/works_small.gif" style="margin-right:5px;" title="" />';
  7781. $return .= '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;action=add_item&amp;type=' . TOOL_STUDENTPUBLICATION . '&amp;lp_id=' . $this->lp_id . '">' . get_lang('AddAssignmentPage') . '</a>';
  7782. $return .= '</div>';
  7783. $return .= '</div>';
  7784. return $return;
  7785. }
  7786. /**
  7787. * Creates a list with all the forums in it
  7788. * @return string
  7789. */
  7790. public function get_forums()
  7791. {
  7792. require_once '../forum/forumfunction.inc.php';
  7793. require_once '../forum/forumconfig.inc.php';
  7794. $a_forums = get_forums();
  7795. $return = '<ul class="lp_resource">';
  7796. //First add link
  7797. $return .= '<li class="lp_resource_element">';
  7798. $return .= '<img alt="" src="../img/forum_new_small.gif" style="margin-right:5px;" title="" />';
  7799. $return .= '<a href="' . api_get_path(REL_CODE_PATH) . 'forum/index.php?' . api_get_cidreq() . '&action=add&amp;content=forum&amp;origin=learnpath&amp;lp_id=' . $this->lp_id . '" title="' . get_lang('CreateANewForum') . '">' . get_lang('CreateANewForum') . '</a>';
  7800. $return .= '</li>';
  7801. $return .= '<script>
  7802. function toggle_forum(forum_id){
  7803. if(document.getElementById("forum_"+forum_id+"_content").style.display == "none"){
  7804. document.getElementById("forum_"+forum_id+"_content").style.display = "block";
  7805. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'remove.gif";
  7806. } else {
  7807. document.getElementById("forum_"+forum_id+"_content").style.display = "none";
  7808. document.getElementById("forum_"+forum_id+"_opener").src = "' . api_get_path(WEB_IMG_PATH) . 'add.gif";
  7809. }
  7810. }
  7811. </script>';
  7812. foreach ($a_forums as $forum) {
  7813. if (!empty($forum['forum_id'])) {
  7814. $return .= '<li class="lp_resource_element" data_id="'.$forum['forum_id'].'" data_type="'.TOOL_FORUM.'" title="'.$forum['forum_title'].'" >';
  7815. $return .= '<a class="moved" href="#">';
  7816. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7817. $return .= ' </a>';
  7818. $return .= '<img alt="" src="../img/lp_forum.gif" style="margin-right:5px;" title="" />';
  7819. $return .= '<a style="cursor:hand" onclick="javascript: toggle_forum(' . $forum['forum_id'] . ')" style="vertical-align:middle">
  7820. <img src="' . api_get_path(WEB_IMG_PATH) . 'add.gif" id="forum_' . $forum['forum_id'] . '_opener" align="absbottom" />
  7821. </a>
  7822. <a href="' . api_get_self() . '?'.api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_FORUM . '&amp;forum_id=' . $forum['forum_id'] . '&amp;lp_id=' . $this->lp_id . '" style="vertical-align:middle">' .
  7823. Security :: remove_XSS($forum['forum_title']) . '</a>';
  7824. $return .= '</li>';
  7825. $return .= '<div style="display:none" id="forum_' . $forum['forum_id'] . '_content">';
  7826. $a_threads = get_threads($forum['forum_id']);
  7827. if (is_array($a_threads)) {
  7828. foreach ($a_threads as $thread) {
  7829. $return .= '<li class="lp_resource_element" data_id="'.$thread['thread_id'].'" data_type="'.TOOL_THREAD.'" title="'.$thread['thread_title'].'" >';
  7830. $return .= '&nbsp;<a class="moved" href="#">';
  7831. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  7832. $return .= ' </a>';
  7833. $return .= Display::return_icon('forumthread.png', get_lang('Thread'), array(), ICON_SIZE_TINY);
  7834. $return .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=add_item&amp;type=' . TOOL_THREAD . '&amp;thread_id=' . $thread['thread_id'] . '&amp;lp_id=' . $this->lp_id . '">' .
  7835. Security :: remove_XSS($thread['thread_title']) . '</a>';
  7836. $return .= '</li>';
  7837. }
  7838. }
  7839. $return .= '</div>';
  7840. }
  7841. }
  7842. $return .= '</ul>';
  7843. return $return;
  7844. }
  7845. /**
  7846. * // TODO: The output encoding should be equal to the system encoding.
  7847. *
  7848. * Exports the learning path as a SCORM package. This is the main function that
  7849. * gathers the content, transforms it, writes the imsmanifest.xml file, zips the
  7850. * whole thing and returns the zip.
  7851. *
  7852. * This method needs to be called in PHP5, as it will fail with non-adequate
  7853. * XML package (like the ones for PHP4), and it is *not* a static method, so
  7854. * you need to call it on a learnpath object.
  7855. * @TODO The method might be redefined later on in the scorm class itself to avoid
  7856. * creating a SCORM structure if there is one already. However, if the initial SCORM
  7857. * path has been modified, it should use the generic method here below.
  7858. * @TODO link this function with the export_lp() function in the same class
  7859. * @param string Optional name of zip file. If none, title of learnpath is
  7860. * domesticated and trailed with ".zip"
  7861. * @return string Returns the zip package string, or null if error
  7862. */
  7863. public function scorm_export()
  7864. {
  7865. $_course = api_get_course_info();
  7866. $course_id = api_get_course_int_id();
  7867. // Remove memory and time limits as much as possible as this might be a long process...
  7868. if (function_exists('ini_set')) {
  7869. api_set_memory_limit('128M');
  7870. ini_set('max_execution_time', 600);
  7871. }
  7872. // Create the zip handler (this will remain available throughout the method).
  7873. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  7874. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  7875. $temp_dir_short = uniqid();
  7876. $temp_zip_dir = $archive_path.'/'.$temp_dir_short;
  7877. $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
  7878. $zip_folder = new PclZip($temp_zip_file);
  7879. $current_course_path = api_get_path(SYS_COURSE_PATH).api_get_course_path();
  7880. $root_path = $main_path = api_get_path(SYS_PATH);
  7881. $files_cleanup = array();
  7882. // Place to temporarily stash the zip file.
  7883. // create the temp dir if it doesn't exist
  7884. // or do a cleanup before creating the zip file.
  7885. if (!is_dir($temp_zip_dir)) {
  7886. mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
  7887. } else {
  7888. // Cleanup: Check the temp dir for old files and delete them.
  7889. $handle = opendir($temp_zip_dir);
  7890. while (false !== ($file = readdir($handle))) {
  7891. if ($file != '.' && $file != '..') {
  7892. unlink("$temp_zip_dir/$file");
  7893. }
  7894. }
  7895. closedir($handle);
  7896. }
  7897. $zip_files = $zip_files_abs = $zip_files_dist = array();
  7898. if (is_dir($current_course_path.'/scorm/'.$this->path) && is_file($current_course_path.'/scorm/'.$this->path.'/imsmanifest.xml')) {
  7899. // Remove the possible . at the end of the path.
  7900. $dest_path_to_lp = substr($this->path, -1) == '.' ? substr($this->path, 0, -1) : $this->path;
  7901. $dest_path_to_scorm_folder = str_replace('//','/',$temp_zip_dir.'/scorm/'.$dest_path_to_lp);
  7902. mkdir($dest_path_to_scorm_folder, api_get_permissions_for_new_directories(), true);
  7903. $zip_files_dist = copyr($current_course_path.'/scorm/'.$this->path, $dest_path_to_scorm_folder, array('imsmanifest'), $zip_files);
  7904. }
  7905. // Build a dummy imsmanifest structure.
  7906. // Do not add to the zip yet (we still need it).
  7907. // This structure is developed following regulations for SCORM 1.2 packaging in the SCORM 1.2 Content
  7908. // Aggregation Model official document, section "2.3 Content Packaging".
  7909. // We are going to build a UTF-8 encoded manifest. Later we will recode it to the desired (and supported) encoding.
  7910. $xmldoc = new DOMDocument('1.0');
  7911. $root = $xmldoc->createElement('manifest');
  7912. $root->setAttribute('identifier', 'SingleCourseManifest');
  7913. $root->setAttribute('version', '1.1');
  7914. $root->setAttribute('xmlns', 'http://www.imsproject.org/xsd/imscp_rootv1p1p2');
  7915. $root->setAttribute('xmlns:adlcp', 'http://www.adlnet.org/xsd/adlcp_rootv1p2');
  7916. $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  7917. $root->setAttribute('xsi:schemaLocation', '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');
  7918. // Build mandatory sub-root container elements.
  7919. $metadata = $xmldoc->createElement('metadata');
  7920. $md_schema = $xmldoc->createElement('schema', 'ADL SCORM');
  7921. $metadata->appendChild($md_schema);
  7922. $md_schemaversion = $xmldoc->createElement('schemaversion', '1.2');
  7923. $metadata->appendChild($md_schemaversion);
  7924. $root->appendChild($metadata);
  7925. $organizations = $xmldoc->createElement('organizations');
  7926. $resources = $xmldoc->createElement('resources');
  7927. // Build the only organization we will use in building our learnpaths.
  7928. $organizations->setAttribute('default', 'chamilo_scorm_export');
  7929. $organization = $xmldoc->createElement('organization');
  7930. $organization->setAttribute('identifier', 'chamilo_scorm_export');
  7931. // To set the title of the SCORM entity (=organization), we take the name given
  7932. // in Chamilo and convert it to HTML entities using the Chamilo charset (not the
  7933. // learning path charset) as it is the encoding that defines how it is stored
  7934. // in the database. Then we convert it to HTML entities again as the "&" character
  7935. // alone is not authorized in XML (must be &amp;).
  7936. // The title is then decoded twice when extracting (see scorm::parse_manifest).
  7937. $org_title = $xmldoc->createElement('title', api_utf8_encode($this->get_name()));
  7938. $organization->appendChild($org_title);
  7939. $folder_name = 'document';
  7940. // Removes the learning_path/scorm_folder path when exporting see #4841
  7941. $path_to_remove = null;
  7942. $result = $this->generate_lp_folder($_course);
  7943. if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
  7944. $path_to_remove = 'document'.$result['dir'];
  7945. $path_to_replace = $folder_name.'/';
  7946. }
  7947. //Fixes chamilo scorm exports
  7948. if ($this->ref == 'chamilo_scorm_export') {
  7949. $path_to_remove = 'scorm/'.$this->path.'/document/';
  7950. }
  7951. // For each element, add it to the imsmanifest structure, then add it to the zip.
  7952. // Always call the learnpathItem->scorm_export() method to change it to the SCORM format.
  7953. $link_updates = array();
  7954. $links_to_create = array();
  7955. foreach ($this->items as $index => $item) {
  7956. if (!in_array($item->type, array(TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION))) {
  7957. // Get included documents from this item.
  7958. if ($item->type == 'sco') {
  7959. $inc_docs = $item->get_resources_from_source(
  7960. null,
  7961. api_get_path(SYS_COURSE_PATH) . api_get_course_path() . '/' . 'scorm/' . $this->path . '/' . $item->get_path()
  7962. );
  7963. } else {
  7964. $inc_docs = $item->get_resources_from_source();
  7965. }
  7966. // Give a child element <item> to the <organization> element.
  7967. $my_item_id = $item->get_id();
  7968. $my_item = $xmldoc->createElement('item');
  7969. $my_item->setAttribute('identifier', 'ITEM_'.$my_item_id);
  7970. $my_item->setAttribute('identifierref', 'RESOURCE_'.$my_item_id);
  7971. $my_item->setAttribute('isvisible', 'true');
  7972. // Give a child element <title> to the <item> element.
  7973. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  7974. $my_item->appendChild($my_title);
  7975. // Give a child element <adlcp:prerequisites> to the <item> element.
  7976. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $this->get_scorm_prereq_string($my_item_id));
  7977. $my_prereqs->setAttribute('type', 'aicc_script');
  7978. $my_item->appendChild($my_prereqs);
  7979. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  7980. //$xmldoc->createElement('adlcp:maxtimeallowed','');
  7981. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  7982. //$xmldoc->createElement('adlcp:timelimitaction','');
  7983. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  7984. //$xmldoc->createElement('adlcp:datafromlms','');
  7985. // Give a child element <adlcp:masteryscore> to the <item> element.
  7986. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  7987. $my_item->appendChild($my_masteryscore);
  7988. // Attach this item to the organization element or hits parent if there is one.
  7989. if (!empty($item->parent) && $item->parent != 0) {
  7990. $children = $organization->childNodes;
  7991. $possible_parent = &$this->get_scorm_xml_node($children, 'ITEM_'.$item->parent);
  7992. if (is_object($possible_parent)) {
  7993. $possible_parent->appendChild($my_item);
  7994. } else {
  7995. if ($this->debug > 0) { error_log('Parent ITEM_'.$item->parent.' of item ITEM_'.$my_item_id.' not found'); }
  7996. }
  7997. } else {
  7998. if ($this->debug > 0) { error_log('No parent'); }
  7999. $organization->appendChild($my_item);
  8000. }
  8001. // Get the path of the file(s) from the course directory root.
  8002. $my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8003. if (!empty($path_to_remove)) {
  8004. //From docs
  8005. $my_xml_file_path = str_replace($path_to_remove, $path_to_replace, $my_file_path);
  8006. //From quiz
  8007. if ($this->ref == 'chamilo_scorm_export') {
  8008. $path_to_remove = 'scorm/'.$this->path.'/';
  8009. $my_xml_file_path = str_replace($path_to_remove, '', $my_file_path);
  8010. }
  8011. } else {
  8012. $my_xml_file_path = $my_file_path;
  8013. }
  8014. $my_sub_dir = dirname($my_file_path);
  8015. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8016. $my_xml_sub_dir = $my_sub_dir;
  8017. // Give a <resource> child to the <resources> element
  8018. $my_resource = $xmldoc->createElement('resource');
  8019. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8020. $my_resource->setAttribute('type', 'webcontent');
  8021. $my_resource->setAttribute('href', $my_xml_file_path);
  8022. // adlcp:scormtype can be either 'sco' or 'asset'.
  8023. if ($item->type == 'sco') {
  8024. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8025. } else {
  8026. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8027. }
  8028. // xml:base is the base directory to find the files declared in this resource.
  8029. $my_resource->setAttribute('xml:base', '');
  8030. // Give a <file> child to the <resource> element.
  8031. $my_file = $xmldoc->createElement('file');
  8032. $my_file->setAttribute('href', $my_xml_file_path);
  8033. $my_resource->appendChild($my_file);
  8034. // Dependency to other files - not yet supported.
  8035. $i = 1;
  8036. foreach ($inc_docs as $doc_info) {
  8037. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8038. $my_dep = $xmldoc->createElement('resource');
  8039. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8040. $my_dep->setAttribute('identifier', $res_id);
  8041. $my_dep->setAttribute('type', 'webcontent');
  8042. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8043. $my_dep_file = $xmldoc->createElement('file');
  8044. // Check type of URL.
  8045. //error_log(__LINE__.'Now dealing with '.$doc_info[0].' of type '.$doc_info[1].'-'.$doc_info[2], 0);
  8046. if ($doc_info[1] == 'remote') {
  8047. // Remote file. Save url as is.
  8048. $my_dep_file->setAttribute('href', $doc_info[0]);
  8049. $my_dep->setAttribute('xml:base', '');
  8050. } elseif ($doc_info[1] == 'local') {
  8051. switch ($doc_info[2]) {
  8052. case 'url': // Local URL - save path as url for now, don't zip file.
  8053. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8054. $current_dir = dirname($abs_path);
  8055. $current_dir = str_replace('\\', '/', $current_dir);
  8056. $file_path = realpath($abs_path);
  8057. $file_path = str_replace('\\', '/', $file_path);
  8058. $my_dep_file->setAttribute('href', $file_path);
  8059. $my_dep->setAttribute('xml:base', '');
  8060. if (strstr($file_path, $main_path) !== false) {
  8061. // The calculated real path is really inside Chamilo's root path.
  8062. // Reduce file path to what's under the DocumentRoot.
  8063. $file_path = substr($file_path, strlen($root_path) - 1);
  8064. //echo $file_path;echo '<br /><br />';
  8065. //error_log(__LINE__.'Reduced url path: '.$file_path, 0);
  8066. $zip_files_abs[] = $file_path;
  8067. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8068. $my_dep_file->setAttribute('href', $file_path);
  8069. $my_dep->setAttribute('xml:base', '');
  8070. } elseif (empty($file_path)) {
  8071. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8072. if (strpos($document_root, -1) == '/') {
  8073. $document_root = substr(0, -1, $document_root);
  8074. }*/
  8075. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8076. $file_path = str_replace('//', '/', $file_path);
  8077. if (file_exists($file_path)) {
  8078. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8079. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8080. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8081. $my_dep_file->setAttribute('href', $file_path);
  8082. $my_dep->setAttribute('xml:base', '');
  8083. }
  8084. }
  8085. break;
  8086. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8087. $my_dep_file->setAttribute('href', $doc_info[0]);
  8088. $my_dep->setAttribute('xml:base', '');
  8089. // The next lines fix a bug when using the "subdir" mode of Chamilo, whereas
  8090. // an image path would be constructed as /var/www/subdir/subdir/img/foo.bar
  8091. $abs_img_path_without_subdir = $doc_info[0];
  8092. $relp = api_get_path(REL_PATH); // The url-append config param.
  8093. $pos = strpos($abs_img_path_without_subdir, $relp);
  8094. if ($pos === 0) {
  8095. $abs_img_path_without_subdir = '/'.substr($abs_img_path_without_subdir, strlen($relp));
  8096. }
  8097. $file_path = realpath(api_get_path(SYS_PATH).$abs_img_path_without_subdir);
  8098. $file_path = str_replace('\\', '/', $file_path);
  8099. $file_path = str_replace('//', '/', $file_path);
  8100. // Prepare the current directory path (until just under 'document') with a trailing slash.
  8101. $cur_path = substr($current_course_path, -1) == '/' ? $current_course_path : $current_course_path.'/';
  8102. // Check if the current document is in that path.
  8103. if (strstr($file_path, $cur_path) !== false) {
  8104. // The document is in that path, now get the relative path
  8105. // to the containing document.
  8106. $orig_file_path = dirname($cur_path.$my_file_path).'/';
  8107. $orig_file_path = str_replace('\\', '/', $orig_file_path);
  8108. $relative_path = '';
  8109. if (strstr($file_path, $cur_path) !== false) {
  8110. //$relative_path = substr($file_path, strlen($orig_file_path));
  8111. $relative_path = str_replace($cur_path, '', $file_path);
  8112. $file_path = substr($file_path, strlen($cur_path));
  8113. } else {
  8114. // This case is still a problem as it's difficult to calculate a relative path easily
  8115. // might still generate wrong links.
  8116. //$file_path = substr($file_path,strlen($cur_path));
  8117. // Calculate the directory path to the current file (without trailing slash).
  8118. $my_relative_path = dirname($file_path);
  8119. $my_relative_path = str_replace('\\', '/', $my_relative_path);
  8120. $my_relative_file = basename($file_path);
  8121. // Calculate the directory path to the containing file (without trailing slash).
  8122. $my_orig_file_path = substr($orig_file_path, 0, -1);
  8123. $dotdot = '';
  8124. $subdir = '';
  8125. while (strstr($my_relative_path, $my_orig_file_path) === false && (strlen($my_orig_file_path) > 1) && (strlen($my_relative_path) > 1)) {
  8126. $my_relative_path2 = dirname($my_relative_path);
  8127. $my_relative_path2 = str_replace('\\', '/', $my_relative_path2);
  8128. $my_orig_file_path = dirname($my_orig_file_path);
  8129. $my_orig_file_path = str_replace('\\', '/', $my_orig_file_path);
  8130. $subdir = substr($my_relative_path, strlen($my_relative_path2) + 1).'/'.$subdir;
  8131. $dotdot += '../';
  8132. $my_relative_path = $my_relative_path2;
  8133. }
  8134. $relative_path = $dotdot.$subdir.$my_relative_file;
  8135. }
  8136. // Put the current document in the zip (this array is the array
  8137. // that will manage documents already in the course folder - relative).
  8138. $zip_files[] = $file_path;
  8139. // Update the links to the current document in the containing document (make them relative).
  8140. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $relative_path);
  8141. $my_dep_file->setAttribute('href', $file_path);
  8142. $my_dep->setAttribute('xml:base', '');
  8143. } elseif (strstr($file_path, $main_path) !== false) {
  8144. // The calculated real path is really inside Chamilo's root path.
  8145. // Reduce file path to what's under the DocumentRoot.
  8146. $file_path = substr($file_path, strlen($root_path));
  8147. //echo $file_path;echo '<br /><br />';
  8148. //error_log('Reduced path: '.$file_path, 0);
  8149. $zip_files_abs[] = $file_path;
  8150. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8151. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8152. $my_dep->setAttribute('xml:base', '');
  8153. } elseif (empty($file_path)) {
  8154. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8155. if(strpos($document_root,-1) == '/') {
  8156. $document_root = substr(0, -1, $document_root);
  8157. }*/
  8158. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8159. $file_path = str_replace('//', '/', $file_path);
  8160. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8161. $current_dir = dirname($abs_path);
  8162. $current_dir = str_replace('\\', '/', $current_dir);
  8163. if (file_exists($file_path)) {
  8164. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8165. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8166. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8167. $my_dep_file->setAttribute('href','document/'.$file_path);
  8168. $my_dep->setAttribute('xml:base', '');
  8169. }
  8170. }
  8171. break;
  8172. case 'rel':
  8173. // Path relative to the current document.
  8174. // Save xml:base as current document's directory and save file in zip as subdir.file_path
  8175. if (substr($doc_info[0], 0, 2) == '..') {
  8176. // Relative path going up.
  8177. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8178. $current_dir = str_replace('\\', '/', $current_dir);
  8179. $file_path = realpath($current_dir.$doc_info[0]);
  8180. $file_path = str_replace('\\', '/', $file_path);
  8181. //error_log($file_path.' <-> '.$main_path,0);
  8182. if (strstr($file_path, $main_path) !== false) {
  8183. // The calculated real path is really inside Chamilo's root path.
  8184. // Reduce file path to what's under the DocumentRoot.
  8185. $file_path = substr($file_path, strlen($root_path));
  8186. //error_log('Reduced path: '.$file_path, 0);
  8187. $zip_files_abs[] = $file_path;
  8188. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8189. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8190. $my_dep->setAttribute('xml:base', '');
  8191. }
  8192. } else {
  8193. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8194. $my_dep_file->setAttribute('href', $doc_info[0]);
  8195. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8196. }
  8197. break;
  8198. default:
  8199. $my_dep_file->setAttribute('href', $doc_info[0]);
  8200. $my_dep->setAttribute('xml:base', '');
  8201. break;
  8202. }
  8203. }
  8204. $my_dep->appendChild($my_dep_file);
  8205. $resources->appendChild($my_dep);
  8206. $dependency = $xmldoc->createElement('dependency');
  8207. $dependency->setAttribute('identifierref', $res_id);
  8208. $my_resource->appendChild($dependency);
  8209. $i++;
  8210. }
  8211. $resources->appendChild($my_resource);
  8212. $zip_files[] = $my_file_path;
  8213. } else {
  8214. // If the item is a quiz or a link or whatever non-exportable, we include a step indicating it.
  8215. switch ($item->type) {
  8216. case TOOL_LINK:
  8217. $my_item = $xmldoc->createElement('item');
  8218. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8219. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8220. $my_item->setAttribute('isvisible', 'true');
  8221. // Give a child element <title> to the <item> element.
  8222. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8223. $my_item->appendChild($my_title);
  8224. // Give a child element <adlcp:prerequisites> to the <item> element.
  8225. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8226. $my_prereqs->setAttribute('type', 'aicc_script');
  8227. $my_item->appendChild($my_prereqs);
  8228. // Give a child element <adlcp:maxtimeallowed> to the <item> element - not yet supported.
  8229. //$xmldoc->createElement('adlcp:maxtimeallowed', '');
  8230. // Give a child element <adlcp:timelimitaction> to the <item> element - not yet supported.
  8231. //$xmldoc->createElement('adlcp:timelimitaction', '');
  8232. // Give a child element <adlcp:datafromlms> to the <item> element - not yet supported.
  8233. //$xmldoc->createElement('adlcp:datafromlms', '');
  8234. // Give a child element <adlcp:masteryscore> to the <item> element.
  8235. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8236. $my_item->appendChild($my_masteryscore);
  8237. // Attach this item to the organization element or its parent if there is one.
  8238. if (!empty($item->parent) && $item->parent != 0) {
  8239. $children = $organization->childNodes;
  8240. for ($i = 0; $i < $children->length; $i++) {
  8241. $item_temp = $children->item($i);
  8242. if ($item_temp -> nodeName == 'item') {
  8243. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8244. $item_temp -> appendChild($my_item);
  8245. }
  8246. }
  8247. }
  8248. } else {
  8249. $organization->appendChild($my_item);
  8250. }
  8251. $my_file_path = 'link_'.$item->get_id().'.html';
  8252. $sql = 'SELECT url, title FROM '.Database :: get_course_table(TABLE_LINK).'
  8253. WHERE c_id = '.$course_id.' AND id='.$item->path;
  8254. $rs = Database::query($sql);
  8255. if ($link = Database :: fetch_array($rs)) {
  8256. $url = $link['url'];
  8257. $title = stripslashes($link['title']);
  8258. $links_to_create[$my_file_path] = array('title' => $title, 'url' => $url);
  8259. $my_xml_file_path = $my_file_path;
  8260. $my_sub_dir = dirname($my_file_path);
  8261. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8262. $my_xml_sub_dir = $my_sub_dir;
  8263. // Give a <resource> child to the <resources> element.
  8264. $my_resource = $xmldoc->createElement('resource');
  8265. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8266. $my_resource->setAttribute('type', 'webcontent');
  8267. $my_resource->setAttribute('href', $my_xml_file_path);
  8268. // adlcp:scormtype can be either 'sco' or 'asset'.
  8269. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8270. // xml:base is the base directory to find the files declared in this resource.
  8271. $my_resource->setAttribute('xml:base', '');
  8272. // give a <file> child to the <resource> element.
  8273. $my_file = $xmldoc->createElement('file');
  8274. $my_file->setAttribute('href', $my_xml_file_path);
  8275. $my_resource->appendChild($my_file);
  8276. $resources->appendChild($my_resource);
  8277. }
  8278. break;
  8279. case TOOL_QUIZ:
  8280. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
  8281. $exe_id = $item->path; // Should be using ref when everything will be cleaned up in this regard.
  8282. $exe = new Exercise();
  8283. $exe->read($exe_id);
  8284. $my_item = $xmldoc->createElement('item');
  8285. $my_item->setAttribute('identifier', 'ITEM_'.$item->get_id());
  8286. $my_item->setAttribute('identifierref', 'RESOURCE_'.$item->get_id());
  8287. $my_item->setAttribute('isvisible', 'true');
  8288. // Give a child element <title> to the <item> element.
  8289. $my_title = $xmldoc->createElement('title', htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES, 'UTF-8'));
  8290. $my_item->appendChild($my_title);
  8291. $my_max_score = $xmldoc->createElement('max_score', $item->get_max());
  8292. //$my_item->appendChild($my_max_score);
  8293. // Give a child element <adlcp:prerequisites> to the <item> element.
  8294. $my_prereqs = $xmldoc->createElement('adlcp:prerequisites', $item->get_prereq_string());
  8295. $my_prereqs->setAttribute('type','aicc_script');
  8296. $my_item->appendChild($my_prereqs);
  8297. // Give a child element <adlcp:masteryscore> to the <item> element.
  8298. $my_masteryscore = $xmldoc->createElement('adlcp:masteryscore', $item->get_mastery_score());
  8299. $my_item->appendChild($my_masteryscore);
  8300. // Attach this item to the organization element or hits parent if there is one.
  8301. if (!empty($item->parent) && $item->parent != 0) {
  8302. $children = $organization->childNodes;
  8303. for ($i = 0; $i < $children->length; $i++) {
  8304. $item_temp = $children->item($i);
  8305. if ($item_temp -> nodeName == 'item') {
  8306. if ($item_temp->getAttribute('identifier') == 'ITEM_'.$item->parent) {
  8307. $item_temp -> appendChild($my_item);
  8308. }
  8309. }
  8310. }
  8311. } else {
  8312. $organization->appendChild($my_item);
  8313. }
  8314. // Include export scripts.
  8315. require_once api_get_path(SYS_CODE_PATH).'exercice/export/scorm/scorm_export.php';
  8316. // Get the path of the file(s) from the course directory root
  8317. //$my_file_path = $item->get_file_path('scorm/'.$this->path.'/');
  8318. $my_file_path = 'quiz_'.$item->get_id().'.html';
  8319. // Write the contents of the exported exercise into a (big) html file
  8320. // to later pack it into the exported SCORM. The file will be removed afterwards.
  8321. $contents = export_exercise($exe_id, true);
  8322. $tmp_file_path = $archive_path.$temp_dir_short.'/'.$my_file_path;
  8323. $res = file_put_contents($tmp_file_path, $contents);
  8324. if ($res === false) { error_log('Could not write into file '.$tmp_file_path.' '.__FILE__.' '.__LINE__, 0); }
  8325. $files_cleanup[] = $tmp_file_path;
  8326. //error_log($tmp_path); die();
  8327. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_QUOTES, 'UTF-8');
  8328. $my_xml_file_path = $my_file_path;
  8329. $my_sub_dir = dirname($my_file_path);
  8330. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8331. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_QUOTES, 'UTF-8');
  8332. $my_xml_sub_dir = $my_sub_dir;
  8333. // Give a <resource> child to the <resources> element.
  8334. $my_resource = $xmldoc->createElement('resource');
  8335. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8336. $my_resource->setAttribute('type', 'webcontent');
  8337. $my_resource->setAttribute('href', $my_xml_file_path);
  8338. // adlcp:scormtype can be either 'sco' or 'asset'.
  8339. $my_resource->setAttribute('adlcp:scormtype', 'sco');
  8340. // xml:base is the base directory to find the files declared in this resource.
  8341. $my_resource->setAttribute('xml:base', '');
  8342. // Give a <file> child to the <resource> element.
  8343. $my_file = $xmldoc->createElement('file');
  8344. $my_file->setAttribute('href', $my_xml_file_path);
  8345. $my_resource->appendChild($my_file);
  8346. // Get included docs.
  8347. $inc_docs = $item->get_resources_from_source(null,$tmp_file_path);
  8348. // Dependency to other files - not yet supported.
  8349. $i = 1;
  8350. foreach ($inc_docs as $doc_info) {
  8351. if (count($doc_info) < 1 || empty($doc_info[0])) { continue; }
  8352. $my_dep = $xmldoc->createElement('resource');
  8353. $res_id = 'RESOURCE_'.$item->get_id().'_'.$i;
  8354. $my_dep->setAttribute('identifier', $res_id);
  8355. $my_dep->setAttribute('type', 'webcontent');
  8356. $my_dep->setAttribute('adlcp:scormtype', 'asset');
  8357. $my_dep_file = $xmldoc->createElement('file');
  8358. // Check type of URL.
  8359. if ($doc_info[1] == 'remote') {
  8360. // Remote file. Save url as is.
  8361. $my_dep_file->setAttribute('href', $doc_info[0]);
  8362. $my_dep->setAttribute('xml:base', '');
  8363. } elseif ($doc_info[1] == 'local') {
  8364. switch ($doc_info[2]) {
  8365. case 'url': // Local URL - save path as url for now, don't zip file.
  8366. // Save file but as local file (retrieve from URL).
  8367. $abs_path = api_get_path(SYS_PATH).str_replace(api_get_path(WEB_PATH), '', $doc_info[0]);
  8368. $current_dir = dirname($abs_path);
  8369. $current_dir = str_replace('\\', '/', $current_dir);
  8370. $file_path = realpath($abs_path);
  8371. $file_path = str_replace('\\', '/', $file_path);
  8372. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8373. $my_dep->setAttribute('xml:base', '');
  8374. if (strstr($file_path, $main_path) !== false) {
  8375. // The calculated real path is really inside the chamilo root path.
  8376. // Reduce file path to what's under the DocumentRoot.
  8377. $file_path = substr($file_path, strlen($root_path));
  8378. //echo $file_path;echo '<br /><br />';
  8379. //error_log('Reduced path: '.$file_path, 0);
  8380. $zip_files_abs[] = $file_path;
  8381. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8382. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8383. $my_dep->setAttribute('xml:base', '');
  8384. } elseif (empty($file_path)) {
  8385. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH),api_get_path(REL_PATH)));
  8386. if (strpos($document_root,-1) == '/') {
  8387. $document_root = substr(0, -1, $document_root);
  8388. }*/
  8389. $file_path = $_SERVER['DOCUMENT_ROOT'].$abs_path;
  8390. $file_path = str_replace('//', '/', $file_path);
  8391. if (file_exists($file_path)) {
  8392. $file_path = substr($file_path, strlen($current_dir)); // We get the relative path.
  8393. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8394. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => 'document/'.$file_path);
  8395. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8396. $my_dep->setAttribute('xml:base', '');
  8397. }
  8398. }
  8399. break;
  8400. case 'abs': // Absolute path from DocumentRoot. Save file and leave path as is in the zip.
  8401. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8402. $current_dir = str_replace('\\', '/', $current_dir);
  8403. $file_path = realpath($doc_info[0]);
  8404. $file_path = str_replace('\\', '/', $file_path);
  8405. $my_dep_file->setAttribute('href', $file_path);
  8406. $my_dep->setAttribute('xml:base', '');
  8407. if (strstr($file_path,$main_path) !== false) {
  8408. // The calculated real path is really inside the chamilo root path.
  8409. // Reduce file path to what's under the DocumentRoot.
  8410. $file_path = substr($file_path, strlen($root_path));
  8411. //echo $file_path;echo '<br /><br />';
  8412. //error_log('Reduced path: '.$file_path, 0);
  8413. $zip_files_abs[] = $file_path;
  8414. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8415. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8416. $my_dep->setAttribute('xml:base', '');
  8417. } elseif (empty($file_path)) {
  8418. /*$document_root = substr(api_get_path(SYS_PATH), 0, strpos(api_get_path(SYS_PATH), api_get_path(REL_PATH)));
  8419. if (strpos($document_root,-1) == '/') {
  8420. $document_root = substr(0, -1, $document_root);
  8421. }*/
  8422. $file_path = $_SERVER['DOCUMENT_ROOT'].$doc_info[0];
  8423. $file_path = str_replace('//', '/', $file_path);
  8424. if (file_exists($file_path)) {
  8425. $file_path = substr($file_path,strlen($current_dir)); // We get the relative path.
  8426. $zip_files[] = $my_sub_dir.'/'.$file_path;
  8427. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path);
  8428. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8429. $my_dep->setAttribute('xml:base', '');
  8430. }
  8431. }
  8432. break;
  8433. 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
  8434. if (substr($doc_info[0], 0, 2) == '..') {
  8435. // Relative path going up.
  8436. $current_dir = dirname($current_course_path.'/'.$item->get_file_path()).'/';
  8437. $current_dir = str_replace('\\', '/', $current_dir);
  8438. $file_path = realpath($current_dir.$doc_info[0]);
  8439. $file_path = str_replace('\\', '/', $file_path);
  8440. //error_log($file_path.' <-> '.$main_path, 0);
  8441. if (strstr($file_path, $main_path) !== false) {
  8442. // The calculated real path is really inside Chamilo's root path.
  8443. // Reduce file path to what's under the DocumentRoot.
  8444. $file_path = substr($file_path, strlen($root_path));
  8445. $file_path_dest = $file_path;
  8446. // File path is courses/CHAMILO/document/....
  8447. $info_file_path = explode('/', $file_path);
  8448. if ($info_file_path[0] == 'courses') { // Add character "/" in file path.
  8449. $file_path_dest = 'document/'.$file_path;
  8450. }
  8451. //error_log('Reduced path: '.$file_path, 0);
  8452. $zip_files_abs[] = $file_path;
  8453. $link_updates[$my_file_path][] = array('orig' => $doc_info[0], 'dest' => $file_path_dest);
  8454. $my_dep_file->setAttribute('href', 'document/'.$file_path);
  8455. $my_dep->setAttribute('xml:base', '');
  8456. }
  8457. } else {
  8458. $zip_files[] = $my_sub_dir.'/'.$doc_info[0];
  8459. $my_dep_file->setAttribute('href', $doc_info[0]);
  8460. $my_dep->setAttribute('xml:base', $my_xml_sub_dir);
  8461. }
  8462. break;
  8463. default:
  8464. $my_dep_file->setAttribute('href', $doc_info[0]); // ../../courses/
  8465. $my_dep->setAttribute('xml:base', '');
  8466. break;
  8467. }
  8468. }
  8469. $my_dep->appendChild($my_dep_file);
  8470. $resources->appendChild($my_dep);
  8471. $dependency = $xmldoc->createElement('dependency');
  8472. $dependency->setAttribute('identifierref', $res_id);
  8473. $my_resource->appendChild($dependency);
  8474. $i++;
  8475. }
  8476. $resources->appendChild($my_resource);
  8477. $zip_files[] = $my_file_path;
  8478. break;
  8479. default:
  8480. // Get the path of the file(s) from the course directory root
  8481. $my_file_path = 'non_exportable.html';
  8482. //$my_xml_file_path = api_htmlentities(api_utf8_encode($my_file_path), ENT_COMPAT, 'UTF-8');
  8483. $my_xml_file_path = $my_file_path;
  8484. $my_sub_dir = dirname($my_file_path);
  8485. $my_sub_dir = str_replace('\\', '/', $my_sub_dir);
  8486. //$my_xml_sub_dir = api_htmlentities(api_utf8_encode($my_sub_dir), ENT_COMPAT, 'UTF-8');
  8487. $my_xml_sub_dir = $my_sub_dir;
  8488. // Give a <resource> child to the <resources> element.
  8489. $my_resource = $xmldoc->createElement('resource');
  8490. $my_resource->setAttribute('identifier', 'RESOURCE_'.$item->get_id());
  8491. $my_resource->setAttribute('type', 'webcontent');
  8492. $my_resource->setAttribute('href', $folder_name.'/'.$my_xml_file_path);
  8493. // adlcp:scormtype can be either 'sco' or 'asset'.
  8494. $my_resource->setAttribute('adlcp:scormtype', 'asset');
  8495. // xml:base is the base directory to find the files declared in this resource.
  8496. $my_resource->setAttribute('xml:base', '');
  8497. // Give a <file> child to the <resource> element.
  8498. $my_file = $xmldoc->createElement('file');
  8499. $my_file->setAttribute('href', 'document/'.$my_xml_file_path);
  8500. $my_resource->appendChild($my_file);
  8501. $resources->appendChild($my_resource);
  8502. break;
  8503. }
  8504. }
  8505. }
  8506. $organizations->appendChild($organization);
  8507. $root->appendChild($organizations);
  8508. $root->appendChild($resources);
  8509. $xmldoc->appendChild($root);
  8510. // TODO: Add a readme file here, with a short description and a link to the Reload player
  8511. // then add the file to the zip, then destroy the file (this is done automatically).
  8512. // http://www.reload.ac.uk/scormplayer.html - once done, don't forget to close FS#138
  8513. global $_configuration;
  8514. $append = $_configuration['url_append'];
  8515. foreach ($zip_files as $file_path) {
  8516. if (empty($file_path)) {
  8517. continue;
  8518. }
  8519. $dest_file = $archive_path.$temp_dir_short.'/'.$file_path;
  8520. if (!empty($path_to_remove) && !empty($path_to_replace)) {
  8521. $dest_file = str_replace($path_to_remove, $path_to_replace, $dest_file);
  8522. }
  8523. $this->create_path($dest_file);
  8524. @copy($sys_course_path.$_course['path'].'/'.$file_path, $dest_file);
  8525. // Check if the file needs a link update.
  8526. if (in_array($file_path, array_keys($link_updates))) {
  8527. $string = file_get_contents($dest_file);
  8528. unlink($dest_file);
  8529. foreach ($link_updates[$file_path] as $old_new) {
  8530. // This is an ugly hack that allows .flv files to be found by the flv player that
  8531. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8532. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8533. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8534. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8535. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8536. } elseif (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 6) == 'video/') {
  8537. $old_new['dest'] = str_replace('video/', '../../../../video/', $old_new['dest']);
  8538. }
  8539. //Fix to avoid problems with default_course_document
  8540. if (strpos("main/default_course_document", $old_new['dest'] === false)) {
  8541. //$newDestination = str_replace('document/', $mult.'document/', $old_new['dest']);
  8542. $newDestination = $old_new['dest'];
  8543. } else {
  8544. $newDestination = str_replace('document/', '', $old_new['dest']);
  8545. }
  8546. $string = str_replace($old_new['orig'], $newDestination, $string);
  8547. //Add files inside the HTMLs
  8548. $new_path = str_replace('/courses/', '', $old_new['orig']);
  8549. $destinationFile = $archive_path.$temp_dir_short.'/'.$old_new['dest'];
  8550. if (file_exists($sys_course_path.$new_path)) {
  8551. copy($sys_course_path.$new_path, $destinationFile);
  8552. }
  8553. }
  8554. file_put_contents($dest_file, $string);
  8555. }
  8556. }
  8557. foreach ($zip_files_abs as $file_path) {
  8558. if (empty($file_path)) {
  8559. continue;
  8560. }
  8561. if (!is_file($main_path.$file_path) || !is_readable($main_path.$file_path)) {
  8562. continue;
  8563. }
  8564. $dest_file = $archive_path.$temp_dir_short.'/document/'.$file_path;
  8565. $this->create_path($dest_file);
  8566. copy($main_path.$file_path, $dest_file);
  8567. // Check if the file needs a link update.
  8568. if (in_array($file_path, array_keys($link_updates))) {
  8569. $string = file_get_contents($dest_file);
  8570. unlink($dest_file);
  8571. foreach ($link_updates[$file_path] as $old_new) {
  8572. // This is an ugly hack that allows .flv files to be found by the flv player that
  8573. // will be added in document/main/inc/lib/flv_player/flv_player.swf and that needs
  8574. // to find the flv to play in document/main/, so we replace main/ in the flv path by
  8575. // ../../.. to return from inc/lib/flv_player to the document/main path.
  8576. if (substr($old_new['dest'], -3) == 'flv' && substr($old_new['dest'], 0, 5) == 'main/') {
  8577. $old_new['dest'] = str_replace('main/', '../../../', $old_new['dest']);
  8578. }
  8579. $string = str_replace($old_new['orig'], $old_new['dest'], $string);
  8580. }
  8581. file_put_contents($dest_file, $string);
  8582. }
  8583. }
  8584. if (is_array($links_to_create)) {
  8585. foreach ($links_to_create as $file => $link) {
  8586. $file_content = '<!DOCTYPE html><head>
  8587. <meta charset="'.api_get_language_isocode().'" />
  8588. <title>'.$link['title'].'</title>
  8589. </head>
  8590. <body dir="'.api_get_text_direction().'">
  8591. <div style="text-align:center">
  8592. <a href="'.$link['url'].'">'.$link['title'].'</a></div>
  8593. </body>
  8594. </html>';
  8595. file_put_contents($archive_path.$temp_dir_short.'/'.$file, $file_content);
  8596. }
  8597. }
  8598. // Add non exportable message explanation.
  8599. $lang_not_exportable = get_lang('ThisItemIsNotExportable');
  8600. $file_content = '<!DOCTYPE html><head>
  8601. <meta charset="'.api_get_language_isocode().'" />
  8602. <title>'.$lang_not_exportable.'</title>
  8603. <meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />
  8604. </head>
  8605. <body dir="'.api_get_text_direction().'">';
  8606. $file_content .=
  8607. <<<EOD
  8608. <style>
  8609. .error-message {
  8610. font-family: arial, verdana, helvetica, sans-serif;
  8611. border-width: 1px;
  8612. border-style: solid;
  8613. left: 50%;
  8614. margin: 10px auto;
  8615. min-height: 30px;
  8616. padding: 5px;
  8617. right: 50%;
  8618. width: 500px;
  8619. background-color: #FFD1D1;
  8620. border-color: #FF0000;
  8621. color: #000;
  8622. }
  8623. </style>
  8624. <body>
  8625. <div class="error-message">
  8626. $lang_not_exportable
  8627. </div>
  8628. </body>
  8629. </html>
  8630. EOD;
  8631. if (!is_dir($archive_path.$temp_dir_short.'/document')) {
  8632. @mkdir($archive_path.$temp_dir_short.'/document', api_get_permissions_for_new_directories());
  8633. }
  8634. file_put_contents($archive_path.$temp_dir_short.'/document/non_exportable.html', $file_content);
  8635. // Add the extra files that go along with a SCORM package.
  8636. $main_code_path = api_get_path(SYS_CODE_PATH).'newscorm/packaging/';
  8637. $extra_files = scandir($main_code_path);
  8638. foreach ($extra_files as $extra_file) {
  8639. if (strpos($extra_file, '.') === 0)
  8640. continue;
  8641. else {
  8642. $dest_file = $archive_path . $temp_dir_short . '/' . $extra_file;
  8643. $this->create_path($dest_file);
  8644. copy($main_code_path.$extra_file, $dest_file);
  8645. }
  8646. }
  8647. // Finalize the imsmanifest structure, add to the zip, then return the zip.
  8648. $manifest = @$xmldoc->saveXML();
  8649. $manifest = api_utf8_decode_xml($manifest); // The manifest gets the system encoding now.
  8650. file_put_contents($archive_path.'/'.$temp_dir_short.'/imsmanifest.xml', $manifest);
  8651. $zip_folder->add($archive_path.'/'.$temp_dir_short, PCLZIP_OPT_REMOVE_PATH, $archive_path.'/'.$temp_dir_short.'/');
  8652. // Clean possible temporary files.
  8653. foreach ($files_cleanup as $file) {
  8654. $res = unlink($file);
  8655. if ($res === false) { error_log('Could not delete temp file '.$file.' '.__FILE__.' '.__LINE__, 0); }
  8656. }
  8657. // Send file to client.
  8658. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  8659. $name = replace_dangerous_char($this->get_name()).'.zip';
  8660. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  8661. }
  8662. /**
  8663. * @param int $lp_id
  8664. * @return bool
  8665. */
  8666. public function scorm_export_to_pdf($lp_id)
  8667. {
  8668. $lp_id = intval($lp_id);
  8669. $files_to_export = array();
  8670. $course_data = api_get_course_info($this->cc);
  8671. if (!empty($course_data)) {
  8672. $scorm_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/scorm/'.$this->path;
  8673. $list = self::get_flat_ordered_items_list($lp_id);
  8674. if (!empty($list)) {
  8675. foreach ($list as $item_id) {
  8676. $item = $this->items[$item_id];
  8677. switch ($item->type) {
  8678. case 'document':
  8679. //Getting documents from a LP with chamilo documents
  8680. $file_data = DocumentManager::get_document_data_by_id($item->path, $this->cc);
  8681. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$file_data['path'];
  8682. if (file_exists($file_path)) {
  8683. $files_to_export[] = array('title'=>$item->get_title(),'path'=>$file_path);
  8684. }
  8685. break;
  8686. case 'asset': //commes from a scorm package generated by chamilo
  8687. case 'sco':
  8688. $file_path = $scorm_path.'/'.$item->path;
  8689. if (file_exists($file_path)) {
  8690. $files_to_export[] = array('title'=>$item->get_title(), 'path' => $file_path);
  8691. }
  8692. break;
  8693. case 'dokeos_chapter':
  8694. case 'dir':
  8695. case 'chapter':
  8696. $files_to_export[] = array('title'=> $item->get_title(), 'path'=>null);
  8697. break;
  8698. }
  8699. }
  8700. }
  8701. $pdf = new PDF();
  8702. $result = $pdf->html_to_pdf($files_to_export, $this->name, $this->cc, true);
  8703. return $result;
  8704. }
  8705. return false;
  8706. }
  8707. /**
  8708. * Temp function to be moved in main_api or the best place around for this.
  8709. * Creates a file path if it doesn't exist
  8710. * @param string $path
  8711. */
  8712. public function create_path($path)
  8713. {
  8714. $path_bits = explode('/', dirname($path));
  8715. // IS_WINDOWS_OS has been defined in main_api.lib.php
  8716. $path_built = IS_WINDOWS_OS ? '' : '/';
  8717. foreach ($path_bits as $bit) {
  8718. if (!empty ($bit)) {
  8719. $new_path = $path_built . $bit;
  8720. if (is_dir($new_path)) {
  8721. $path_built = $new_path . '/';
  8722. } else {
  8723. mkdir($new_path, api_get_permissions_for_new_directories());
  8724. $path_built = $new_path . '/';
  8725. }
  8726. }
  8727. }
  8728. }
  8729. /**
  8730. * Delete the image relative to this learning path. No parameter. Only works on instanciated object.
  8731. * @return boolean The results of the unlink function, or false if there was no image to start with
  8732. */
  8733. public function delete_lp_image()
  8734. {
  8735. $img = $this->get_preview_image();
  8736. if ($img != '') {
  8737. $del_file = $this->get_preview_image_path(null, 'sys');
  8738. if (isset($del_file) && file_exists($del_file)) {
  8739. $del_file_2 = $this->get_preview_image_path(64, 'sys');
  8740. if (file_exists($del_file_2)) {
  8741. unlink($del_file_2);
  8742. }
  8743. $this->set_preview_image('');
  8744. return @unlink($del_file);
  8745. }
  8746. }
  8747. return false;
  8748. }
  8749. /**
  8750. * Uploads an author image to the upload/learning_path/images directory
  8751. * @param array The image array, coming from the $_FILES superglobal
  8752. * @return boolean True on success, false on error
  8753. */
  8754. public function upload_image($image_array)
  8755. {
  8756. $image_moved = false;
  8757. if (!empty ($image_array['name'])) {
  8758. $upload_ok = process_uploaded_file($image_array);
  8759. $has_attachment = true;
  8760. } else {
  8761. $image_moved = true;
  8762. }
  8763. if ($upload_ok) {
  8764. if ($has_attachment) {
  8765. $courseDir = api_get_course_path() . '/upload/learning_path/images';
  8766. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  8767. $updir = $sys_course_path . $courseDir;
  8768. // Try to add an extension to the file if it hasn't one.
  8769. $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
  8770. if (!filter_extension($new_file_name)) {
  8771. //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  8772. $image_moved = false;
  8773. } else {
  8774. $file_extension = explode('.', $image_array['name']);
  8775. $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
  8776. $filename = uniqid('');
  8777. $new_file_name = $filename.'.'.$file_extension;
  8778. $new_path = $updir.'/'.$new_file_name;
  8779. // Resize the image.
  8780. $temp = new Image($image_array['tmp_name']);
  8781. $picture_infos = $temp->get_image_info();
  8782. if ($picture_infos['width'] > 104) {
  8783. $thumbwidth = 104;
  8784. } else {
  8785. $thumbwidth = $picture_infos['width'];
  8786. }
  8787. if ($picture_infos['height'] > 96) {
  8788. $new_height = 96;
  8789. } else {
  8790. $new_height = $picture_infos['height'];
  8791. }
  8792. $temp->resize($thumbwidth, $new_height, 0);
  8793. $result = $temp->send_image($new_path);
  8794. // Storing the image filename.
  8795. if ($result) {
  8796. $image_moved = true;
  8797. $this->set_preview_image($new_file_name);
  8798. //Resize to 64px to use on course homepage
  8799. $temp->resize(64, 64, 0);
  8800. $temp->send_image($updir.'/'.$filename.'.64.'.$file_extension);
  8801. return true;
  8802. }
  8803. }
  8804. }
  8805. }
  8806. return false;
  8807. }
  8808. /**
  8809. * @param int $lp_id
  8810. * @param string $status
  8811. */
  8812. public function set_autolunch($lp_id, $status)
  8813. {
  8814. $course_id = api_get_course_int_id();
  8815. $lp_id = intval($lp_id);
  8816. $status = intval($status);
  8817. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  8818. // Setting everything to autolunch = 0
  8819. $attributes['autolunch'] = 0;
  8820. $where = array('session_id = ? AND c_id = ? '=> array(api_get_session_id(), $course_id));
  8821. Database::update($lp_table, $attributes, $where);
  8822. if ($status == 1) {
  8823. //Setting my lp_id to autolunch = 1
  8824. $attributes['autolunch'] = 1;
  8825. $where = array('id = ? AND session_id = ? AND c_id = ?'=> array($lp_id, api_get_session_id(), $course_id));
  8826. Database::update($lp_table, $attributes, $where );
  8827. }
  8828. }
  8829. /**
  8830. * Gets previous_item_id for the next element of the lp_item table
  8831. * @author Isaac flores paz
  8832. * @return integer Previous item ID
  8833. */
  8834. function select_previous_item_id()
  8835. {
  8836. $course_id = api_get_course_int_id();
  8837. if ($this->debug > 0) {
  8838. error_log('New LP - In learnpath::select_previous_item_id()', 0);
  8839. }
  8840. $table_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  8841. // Get the max order of the items
  8842. $sql_max_order = "SELECT max(display_order) AS display_order FROM $table_lp_item
  8843. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "'";
  8844. $rs_max_order = Database::query($sql_max_order);
  8845. $row_max_order = Database::fetch_object($rs_max_order);
  8846. $max_order = $row_max_order->display_order;
  8847. // Get the previous item ID
  8848. $sql = "SELECT id as previous FROM $table_lp_item
  8849. WHERE c_id = $course_id AND lp_id = '" . $this->lp_id . "' AND display_order = '".$max_order."' ";
  8850. $rs_max = Database::query($sql);
  8851. $row_max = Database::fetch_object($rs_max);
  8852. // Return the previous item ID
  8853. return $row_max->previous;
  8854. }
  8855. /**
  8856. * Copies an LP
  8857. */
  8858. public function copy()
  8859. {
  8860. $main_path = api_get_path(SYS_CODE_PATH);
  8861. require_once $main_path.'coursecopy/classes/CourseBuilder.class.php';
  8862. require_once $main_path.'coursecopy/classes/CourseArchiver.class.php';
  8863. require_once $main_path.'coursecopy/classes/CourseRestorer.class.php';
  8864. require_once $main_path.'coursecopy/classes/CourseSelectForm.class.php';
  8865. //Course builder
  8866. $cb = new CourseBuilder();
  8867. //Setting tools that will be copied
  8868. $cb->set_tools_to_build(array('learnpaths'));
  8869. //Setting elements that will be copied
  8870. $cb->set_tools_specific_id_list(array('learnpaths' => array($this->lp_id)));
  8871. $course = $cb->build();
  8872. //Course restorer
  8873. $course_restorer = new CourseRestorer($course);
  8874. $course_restorer->set_add_text_in_items(true);
  8875. $course_restorer->set_tool_copy_settings(array('learnpaths' => array('reset_dates' => true)));
  8876. $course_restorer->restore(api_get_course_id(), api_get_session_id(), false, false);
  8877. }
  8878. function verify_document_size($s)
  8879. {
  8880. $post_max = ini_get('post_max_size');
  8881. $upl_max = ini_get('upload_max_filesize');
  8882. $documents_total_space = DocumentManager::documents_total_space();
  8883. $course_max_space = DocumentManager::get_course_quota();
  8884. $total_size = filesize($s) + $documents_total_space;
  8885. if (filesize($s)>$post_max || filesize($s)>$upl_max || $total_size>$course_max_space ){
  8886. return true;
  8887. } else{
  8888. return false;
  8889. }
  8890. }
  8891. /**
  8892. * Clear LP prerequisites
  8893. */
  8894. function clear_prerequisites()
  8895. {
  8896. $course_id = $this->get_course_int_id();
  8897. if ($this->debug > 0) {
  8898. error_log('New LP - In learnpath::clear_prerequisites()', 0);
  8899. }
  8900. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8901. $lp_id = $this->get_id();
  8902. //Cleaning prerequisites
  8903. $sql = "UPDATE $tbl_lp_item SET prerequisite = ''
  8904. WHERE c_id = ".$course_id." AND lp_id = '$lp_id'";
  8905. Database::query($sql);
  8906. //Cleaning mastery score for exercises
  8907. $sql = "UPDATE $tbl_lp_item SET mastery_score = ''
  8908. WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND item_type = 'quiz'";
  8909. Database::query($sql);
  8910. }
  8911. function set_previous_step_as_prerequisite_for_all_items()
  8912. {
  8913. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  8914. $course_id = $this->get_course_int_id();
  8915. $lp_id = $this->get_id();
  8916. if (!empty($this->items)) {
  8917. $previous_item_id = null;
  8918. $previous_item_max = 0;
  8919. $previous_item_type = null;
  8920. $last_item_not_chapter = null;
  8921. $last_item_not_chapter_type = null;
  8922. $last_item_not_chapter_max = null;
  8923. foreach ($this->items as $item) {
  8924. // if there was a previous item... (otherwise jump to set it)
  8925. if (!empty($previous_item_id)) {
  8926. $current_item_id = $item->get_id(); //save current id
  8927. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  8928. // Current item is not a folder, so it qualifies to get a prerequisites
  8929. if ($last_item_not_chapter_type == 'quiz') {
  8930. // if previous is quiz, mark its max score as default score to be achieved
  8931. $sql = "UPDATE $tbl_lp_item SET mastery_score = '$last_item_not_chapter_max' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$last_item_not_chapter'";
  8932. Database::query($sql);
  8933. }
  8934. // now simply update the prerequisite to set it to the last non-chapter item
  8935. $sql = "UPDATE $tbl_lp_item SET prerequisite = '$last_item_not_chapter' WHERE c_id = ".$course_id." AND lp_id = '$lp_id' AND id = '$current_item_id'";
  8936. Database::query($sql);
  8937. // record item as 'non-chapter' reference
  8938. $last_item_not_chapter = $item->get_id();
  8939. $last_item_not_chapter_type = $item->get_type();
  8940. $last_item_not_chapter_max = $item->get_max();
  8941. }
  8942. } else {
  8943. if (!in_array($item->get_type(), array('dokeos_chapter', 'chapter'))) {
  8944. // Current item is not a folder (but it is the first item) so record as last "non-chapter" item
  8945. $last_item_not_chapter = $item->get_id();
  8946. $last_item_not_chapter_type = $item->get_type();
  8947. $last_item_not_chapter_max = $item->get_max();
  8948. }
  8949. }
  8950. // Saving the item as "previous item" for the next loop
  8951. $previous_item_id = $item->get_id();
  8952. $previous_item_max = $item->get_max();
  8953. $previous_item_type = $item->get_type();
  8954. }
  8955. }
  8956. }
  8957. /**
  8958. * Return the scorm item type object with spaces replaced with _
  8959. * The return result is use to build a css classname like scorm_type_$return
  8960. * @param $in_type
  8961. * @return mixed
  8962. */
  8963. private static function format_scorm_type_item($in_type)
  8964. {
  8965. return str_replace(' ', '_', $in_type);
  8966. }
  8967. /**
  8968. * @return \learnpath
  8969. */
  8970. public static function getLpFromSession($courseCode, $lp_id, $user_id)
  8971. {
  8972. $lpObject = Session::read('lpobject');
  8973. $learnPath = null;
  8974. if (isset($lpObject)) {
  8975. $learnPath = unserialize($lpObject);
  8976. }
  8977. if (!is_object($learnPath)) {
  8978. $learnPath = new learnpath($courseCode, $lp_id, $user_id);
  8979. }
  8980. return $learnPath;
  8981. }
  8982. /**
  8983. * @param int $itemId
  8984. * @return learnpathItem|false
  8985. */
  8986. public function getItem($itemId)
  8987. {
  8988. if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
  8989. return $this->items[$itemId];
  8990. }
  8991. return false;
  8992. }
  8993. }
  8994. if (!function_exists('trim_value')) {
  8995. function trim_value(& $value) {
  8996. $value = trim($value);
  8997. }
  8998. }